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) <= 0) 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_obj(&pdev->dev, *adc_bat); 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_kzalloc_objs(&pdev->dev, *properties, 190 ARRAY_SIZE(gab_props) + ARRAY_SIZE(gab_chan_name)); 191 if (!properties) 192 return -ENOMEM; 193 194 memcpy(properties, gab_props, sizeof(gab_props)); 195 196 /* 197 * getting channel from iio and copying the battery properties 198 * based on the channel supported by consumer device. 199 */ 200 for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) { 201 adc_bat->channel[chan] = devm_iio_channel_get(&pdev->dev, gab_chan_name[chan]); 202 if (IS_ERR(adc_bat->channel[chan])) { 203 ret = PTR_ERR(adc_bat->channel[chan]); 204 if (ret != -ENODEV) 205 return dev_err_probe(&pdev->dev, ret, "Failed to get ADC channel %s\n", gab_chan_name[chan]); 206 adc_bat->channel[chan] = NULL; 207 } else if (adc_bat->channel[chan]) { 208 /* copying properties for supported channels only */ 209 int index2; 210 211 for (index2 = 0; index2 < index; index2++) { 212 if (properties[index2] == gab_dyn_props[chan]) 213 break; /* already known */ 214 } 215 if (index2 == index) /* really new */ 216 properties[index++] = gab_dyn_props[chan]; 217 any = true; 218 } 219 } 220 221 /* none of the channels are supported so let's bail out */ 222 if (!any) 223 return dev_err_probe(&pdev->dev, -ENODEV, "Failed to get any ADC channel\n"); 224 225 /* 226 * Total number of properties is equal to static properties 227 * plus the dynamic properties.Some properties may not be set 228 * as come channels may be not be supported by the device.So 229 * we need to take care of that. 230 */ 231 psy_desc->properties = properties; 232 psy_desc->num_properties = index; 233 234 adc_bat->psy = devm_power_supply_register(&pdev->dev, psy_desc, &psy_cfg); 235 if (IS_ERR(adc_bat->psy)) 236 return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->psy), "Failed to register power-supply device\n"); 237 238 ret = devm_delayed_work_autocancel(&pdev->dev, &adc_bat->bat_work, gab_work); 239 if (ret) 240 return dev_err_probe(&pdev->dev, ret, "Failed to register delayed work\n"); 241 242 adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev, "charged", GPIOD_IN); 243 if (IS_ERR(adc_bat->charge_finished)) 244 return dev_err_probe(&pdev->dev, PTR_ERR(adc_bat->charge_finished), 245 "failed to get charged GPIO\n"); 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"); 307