at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Generic battery driver using IIO |
| 4 | * Copyright (C) 2012, Anish Kumar <yesanishhere@gmail.com> |
| 5 | * Copyright (c) 2023, Sebastian Reichel <sre@kernel.org> |
| 6 | */ |
| 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/platform_device.h> |
| 9 | #include <linux/power_supply.h> |
| 10 | #include <linux/gpio/consumer.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/timer.h> |
| 13 | #include <linux/jiffies.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/iio/consumer.h> |
| 19 | #include <linux/iio/types.h> |
| 20 | #include <linux/of.h> |
| 21 | #include <linux/devm-helpers.h> |
| 22 | |
| 23 | #define JITTER_DEFAULT 10 /* hope 10ms is enough */ |
| 24 | |
| 25 | enum gab_chan_type { |
| 26 | GAB_VOLTAGE = 0, |
| 27 | GAB_CURRENT, |
| 28 | GAB_POWER, |
| 29 | GAB_TEMP, |
| 30 | GAB_MAX_CHAN_TYPE |
| 31 | }; |
| 32 | |
| 33 | /* |
| 34 | * gab_chan_name suggests the standard channel names for commonly used |
| 35 | * channel types. |
| 36 | */ |
| 37 | static const char *const gab_chan_name[] = { |
| 38 | [GAB_VOLTAGE] = "voltage", |
| 39 | [GAB_CURRENT] = "current", |
| 40 | [GAB_POWER] = "power", |
| 41 | [GAB_TEMP] = "temperature", |
| 42 | }; |
| 43 | |
| 44 | struct gab { |
| 45 | struct power_supply *psy; |
| 46 | struct power_supply_desc psy_desc; |
| 47 | struct iio_channel *channel[GAB_MAX_CHAN_TYPE]; |
| 48 | struct delayed_work bat_work; |
| 49 | int status; |
| 50 | struct gpio_desc *charge_finished; |
| 51 | }; |
| 52 | |
| 53 | static struct gab *to_generic_bat(struct power_supply *psy) |
| 54 | { |
| 55 | return power_supply_get_drvdata(psy); |
| 56 | } |
| 57 | |
| 58 | static void gab_ext_power_changed(struct power_supply *psy) |
| 59 | { |
| 60 | struct gab *adc_bat = to_generic_bat(psy); |
| 61 | |
| 62 | schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0)); |
| 63 | } |
| 64 | |
| 65 | static const enum power_supply_property gab_props[] = { |
| 66 | POWER_SUPPLY_PROP_STATUS, |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | * This properties are set based on the received platform data and this |
| 71 | * should correspond one-to-one with enum chan_type. |
| 72 | */ |
| 73 | static const enum power_supply_property gab_dyn_props[] = { |
| 74 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 75 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 76 | POWER_SUPPLY_PROP_POWER_NOW, |
| 77 | POWER_SUPPLY_PROP_TEMP, |
| 78 | }; |
| 79 | |
| 80 | static bool gab_charge_finished(struct gab *adc_bat) |
| 81 | { |
| 82 | if (!adc_bat->charge_finished) |
| 83 | return false; |
| 84 | return gpiod_get_value(adc_bat->charge_finished); |
| 85 | } |
| 86 | |
| 87 | static int gab_read_channel(struct gab *adc_bat, enum gab_chan_type channel, |
| 88 | int *result) |
| 89 | { |
| 90 | int ret; |
| 91 | |
| 92 | ret = iio_read_channel_processed(adc_bat->channel[channel], result); |
| 93 | if (ret < 0) { |
| 94 | dev_err(&adc_bat->psy->dev, "read channel error: %d\n", ret); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | if (channel == GAB_TEMP) |
| 99 | *result /= 100; /* Convert from milli- to deci-degree */ |
| 100 | else |
| 101 | *result *= 1000; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static int gab_get_property(struct power_supply *psy, |
| 107 | enum power_supply_property psp, union power_supply_propval *val) |
| 108 | { |
| 109 | struct gab *adc_bat = to_generic_bat(psy); |
| 110 | |
| 111 | switch (psp) { |
| 112 | case POWER_SUPPLY_PROP_STATUS: |
| 113 | val->intval = adc_bat->status; |
| 114 | return 0; |
| 115 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
| 116 | return gab_read_channel(adc_bat, GAB_VOLTAGE, &val->intval); |
| 117 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
| 118 | return gab_read_channel(adc_bat, GAB_CURRENT, &val->intval); |
| 119 | case POWER_SUPPLY_PROP_POWER_NOW: |
| 120 | return gab_read_channel(adc_bat, GAB_POWER, &val->intval); |
| 121 | case POWER_SUPPLY_PROP_TEMP: |
| 122 | return gab_read_channel(adc_bat, GAB_TEMP, &val->intval); |
| 123 | default: |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static void gab_work(struct work_struct *work) |
| 129 | { |
| 130 | struct gab *adc_bat; |
| 131 | struct delayed_work *delayed_work; |
| 132 | int status; |
| 133 | |
| 134 | delayed_work = to_delayed_work(work); |
| 135 | adc_bat = container_of(delayed_work, struct gab, bat_work); |
| 136 | status = adc_bat->status; |
| 137 | |
| 138 | if (!power_supply_am_i_supplied(adc_bat->psy)) |
| 139 | adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 140 | else if (gab_charge_finished(adc_bat)) |
| 141 | adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
| 142 | else |
| 143 | adc_bat->status = POWER_SUPPLY_STATUS_CHARGING; |
| 144 | |
| 145 | if (status != adc_bat->status) |
| 146 | power_supply_changed(adc_bat->psy); |
| 147 | } |
| 148 | |
| 149 | static irqreturn_t gab_charged(int irq, void *dev_id) |
| 150 | { |
| 151 | struct gab *adc_bat = dev_id; |
| 152 | |
| 153 | schedule_delayed_work(&adc_bat->bat_work, |
| 154 | msecs_to_jiffies(JITTER_DEFAULT)); |
| 155 | |
| 156 | return IRQ_HANDLED; |
| 157 | } |
| 158 | |
| 159 | static int gab_probe(struct platform_device *pdev) |
| 160 | { |
| 161 | struct gab *adc_bat; |
| 162 | struct power_supply_desc *psy_desc; |
| 163 | struct power_supply_config psy_cfg = {}; |
| 164 | enum power_supply_property *properties; |
| 165 | int ret = 0; |
| 166 | int chan; |
| 167 | int index = ARRAY_SIZE(gab_props); |
| 168 | bool any = false; |
| 169 | |
| 170 | adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL); |
| 171 | if (!adc_bat) |
| 172 | return -ENOMEM; |
| 173 | |
| 174 | psy_cfg.fwnode = dev_fwnode(&pdev->dev); |
| 175 | psy_cfg.drv_data = adc_bat; |
| 176 | psy_desc = &adc_bat->psy_desc; |
| 177 | psy_desc->name = dev_name(&pdev->dev); |
| 178 | |
| 179 | /* bootup default values for the battery */ |
| 180 | adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 181 | psy_desc->type = POWER_SUPPLY_TYPE_BATTERY; |
| 182 | psy_desc->get_property = gab_get_property; |
| 183 | psy_desc->external_power_changed = gab_ext_power_changed; |
| 184 | |
| 185 | /* |
| 186 | * copying the static properties and allocating extra memory for holding |
| 187 | * the extra configurable properties received from platform data. |
| 188 | */ |
| 189 | properties = devm_kcalloc(&pdev->dev, |
| 190 | ARRAY_SIZE(gab_props) + |
| 191 | ARRAY_SIZE(gab_chan_name), |
| 192 | sizeof(*properties), |
| 193 | GFP_KERNEL); |
| 194 | if (!properties) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | memcpy(properties, gab_props, sizeof(gab_props)); |
| 198 | |
| 199 | /* |
| 200 | * getting channel from iio and copying the battery properties |
| 201 | * based on the channel supported by consumer device. |
| 202 | */ |
| 203 | for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) { |
| 204 | adc_bat->channel[chan] = devm_iio_channel_get(&pdev->dev, gab_chan_name[chan]); |
| 205 | if (IS_ERR(adc_bat->channel[chan])) { |
| 206 | ret = PTR_ERR(adc_bat->channel[chan]); |
| 207 | if (ret != -ENODEV) |
| 208 | return dev_err_probe(&pdev->dev, ret, "Failed to get ADC channel %s\n", gab_chan_name[chan]); |
| 209 | adc_bat->channel[chan] = NULL; |
| 210 | } else if (adc_bat->channel[chan]) { |
| 211 | /* copying properties for supported channels only */ |
| 212 | int index2; |
| 213 | |
| 214 | for (index2 = 0; index2 < index; index2++) { |
| 215 | if (properties[index2] == gab_dyn_props[chan]) |
| 216 | break; /* already known */ |
| 217 | } |
| 218 | if (index2 == index) /* really new */ |
| 219 | properties[index++] = gab_dyn_props[chan]; |
| 220 | any = true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* none of the channels are supported so let's bail out */ |
| 225 | if (!any) |
| 226 | return dev_err_probe(&pdev->dev, -ENODEV, "Failed to get any ADC channel\n"); |
| 227 | |
| 228 | /* |
| 229 | * Total number of properties is equal to static properties |
| 230 | * plus the dynamic properties.Some properties may not be set |
| 231 | * as come channels may be not be supported by the device.So |
| 232 | * we need to take care of that. |
| 233 | */ |
| 234 | psy_desc->properties = properties; |
| 235 | psy_desc->num_properties = index; |
| 236 | |
| 237 | adc_bat->psy = devm_power_supply_register(&pdev->dev, psy_desc, &psy_cfg); |
| 238 | if (IS_ERR(adc_bat->psy)) |
| 239 | return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->psy), "Failed to register power-supply device\n"); |
| 240 | |
| 241 | ret = devm_delayed_work_autocancel(&pdev->dev, &adc_bat->bat_work, gab_work); |
| 242 | if (ret) |
| 243 | return dev_err_probe(&pdev->dev, ret, "Failed to register delayed work\n"); |
| 244 | |
| 245 | adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev, "charged", GPIOD_IN); |
| 246 | if (adc_bat->charge_finished) { |
| 247 | int irq; |
| 248 | |
| 249 | irq = gpiod_to_irq(adc_bat->charge_finished); |
| 250 | ret = devm_request_any_context_irq(&pdev->dev, irq, gab_charged, |
| 251 | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, |
| 252 | "battery charged", adc_bat); |
| 253 | if (ret < 0) |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | platform_set_drvdata(pdev, adc_bat); |
| 258 | |
| 259 | /* Schedule timer to check current status */ |
| 260 | schedule_delayed_work(&adc_bat->bat_work, |
| 261 | msecs_to_jiffies(0)); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int __maybe_unused gab_suspend(struct device *dev) |
| 266 | { |
| 267 | struct gab *adc_bat = dev_get_drvdata(dev); |
| 268 | |
| 269 | cancel_delayed_work_sync(&adc_bat->bat_work); |
| 270 | adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN; |
| 271 | return 0; |
| 272 | } |
| 273 | |
| 274 | static int __maybe_unused gab_resume(struct device *dev) |
| 275 | { |
| 276 | struct gab *adc_bat = dev_get_drvdata(dev); |
| 277 | |
| 278 | /* Schedule timer to check current status */ |
| 279 | schedule_delayed_work(&adc_bat->bat_work, |
| 280 | msecs_to_jiffies(JITTER_DEFAULT)); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume); |
| 286 | |
| 287 | static const struct of_device_id gab_match[] = { |
| 288 | { .compatible = "adc-battery" }, |
| 289 | { } |
| 290 | }; |
| 291 | MODULE_DEVICE_TABLE(of, gab_match); |
| 292 | |
| 293 | static struct platform_driver gab_driver = { |
| 294 | .driver = { |
| 295 | .name = "generic-adc-battery", |
| 296 | .pm = &gab_pm_ops, |
| 297 | .of_match_table = gab_match, |
| 298 | }, |
| 299 | .probe = gab_probe, |
| 300 | }; |
| 301 | module_platform_driver(gab_driver); |
| 302 | |
| 303 | MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>"); |
| 304 | MODULE_DESCRIPTION("generic battery driver using IIO"); |
| 305 | MODULE_LICENSE("GPL"); |
| 306 | MODULE_IMPORT_NS("IIO_CONSUMER"); |