^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Battery class driver for Apple PMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/adb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/pmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) static struct pmu_battery_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct power_supply *bat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct power_supply_desc bat_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct pmu_battery_info *pbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) char name[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int propval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) } *pbats[PMU_MAX_BATTERIES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define to_pmu_battery_dev(x) power_supply_get_drvdata(x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static int pmu_get_ac_prop(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) val->intval = (!!(pmu_power_flags & PMU_PWR_AC_PRESENT)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) (pmu_battery_count == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static enum power_supply_property pmu_ac_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static const struct power_supply_desc pmu_ac_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .name = "pmu-ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .type = POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .properties = pmu_ac_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .num_properties = ARRAY_SIZE(pmu_ac_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .get_property = pmu_get_ac_prop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static struct power_supply *pmu_ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Battery properties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static char *pmu_batt_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) "Smart", "Comet", "Hooper", "Unknown"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static char *pmu_bat_get_model_name(struct pmu_battery_info *pbi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) switch (pbi->flags & PMU_BATT_TYPE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case PMU_BATT_TYPE_SMART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return pmu_batt_types[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case PMU_BATT_TYPE_COMET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return pmu_batt_types[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case PMU_BATT_TYPE_HOOPER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return pmu_batt_types[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) default: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return pmu_batt_types[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static int pmu_bat_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct pmu_battery_dev *pbat = to_pmu_battery_dev(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct pmu_battery_info *pbi = pbat->pbi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (pbi->flags & PMU_BATT_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) else if (pmu_power_flags & PMU_PWR_AC_PRESENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) val->intval = POWER_SUPPLY_STATUS_FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case POWER_SUPPLY_PROP_PRESENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) val->intval = !!(pbi->flags & PMU_BATT_PRESENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case POWER_SUPPLY_PROP_MODEL_NAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) val->strval = pmu_bat_get_model_name(pbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) case POWER_SUPPLY_PROP_ENERGY_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) val->intval = pbi->charge * 1000; /* mWh -> µWh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case POWER_SUPPLY_PROP_ENERGY_FULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) val->intval = pbi->max_charge * 1000; /* mWh -> µWh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case POWER_SUPPLY_PROP_CURRENT_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) val->intval = pbi->amperage * 1000; /* mA -> µA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) case POWER_SUPPLY_PROP_VOLTAGE_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) val->intval = pbi->voltage * 1000; /* mV -> µV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) val->intval = pbi->time_remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static enum power_supply_property pmu_bat_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) POWER_SUPPLY_PROP_PRESENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) POWER_SUPPLY_PROP_MODEL_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) POWER_SUPPLY_PROP_ENERGY_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) POWER_SUPPLY_PROP_ENERGY_FULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) POWER_SUPPLY_PROP_CURRENT_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) POWER_SUPPLY_PROP_VOLTAGE_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Initialisation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static struct platform_device *bat_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int __init pmu_bat_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bat_pdev = platform_device_register_simple("pmu-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 0, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (IS_ERR(bat_pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = PTR_ERR(bat_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto pdev_register_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) pmu_ac = power_supply_register(&bat_pdev->dev, &pmu_ac_desc, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (IS_ERR(pmu_ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret = PTR_ERR(pmu_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) goto ac_register_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) for (i = 0; i < pmu_battery_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct pmu_battery_dev *pbat = kzalloc(sizeof(*pbat),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!pbat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) sprintf(pbat->name, "PMU_battery_%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) pbat->bat_desc.name = pbat->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) pbat->bat_desc.properties = pmu_bat_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) pbat->bat_desc.num_properties = ARRAY_SIZE(pmu_bat_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) pbat->bat_desc.get_property = pmu_bat_get_property;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) pbat->pbi = &pmu_batteries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) psy_cfg.drv_data = pbat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) pbat->bat = power_supply_register(&bat_pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) &pbat->bat_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (IS_ERR(pbat->bat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = PTR_ERR(pbat->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) kfree(pbat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto battery_register_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) pbats[i] = pbat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto success;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) battery_register_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) while (i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (!pbats[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) power_supply_unregister(pbats[i]->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) kfree(pbats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) power_supply_unregister(pmu_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ac_register_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) platform_device_unregister(bat_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) pdev_register_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) success:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static void __exit pmu_bat_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) for (i = 0; i < PMU_MAX_BATTERIES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!pbats[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) power_supply_unregister(pbats[i]->bat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) kfree(pbats[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) power_supply_unregister(pmu_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) platform_device_unregister(bat_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) module_init(pmu_bat_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) module_exit(pmu_bat_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) MODULE_DESCRIPTION("PMU battery driver");