generic-adc-battery.c 8.1 KB

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