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