generic-adc-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Generic battery driver code using IIO
  3. * Copyright (C) 2012, Anish Kumar <[email protected]>
  4. * based on jz4740-battery.c
  5. * based on s3c_adc_battery.c
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file COPYING in the main directory of this archive for
  9. * more details.
  10. *
  11. */
  12. #include <linux/interrupt.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/power_supply.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/err.h>
  17. #include <linux/timer.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/iio/consumer.h>
  24. #include <linux/iio/types.h>
  25. #include <linux/power/generic-adc-battery.h>
  26. #define JITTER_DEFAULT 10 /* hope 10ms is enough */
  27. enum gab_chan_type {
  28. GAB_VOLTAGE = 0,
  29. GAB_CURRENT,
  30. GAB_POWER,
  31. GAB_MAX_CHAN_TYPE
  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. };
  42. struct gab {
  43. struct power_supply *psy;
  44. struct power_supply_desc psy_desc;
  45. struct iio_channel *channel[GAB_MAX_CHAN_TYPE];
  46. struct gab_platform_data *pdata;
  47. struct delayed_work bat_work;
  48. int level;
  49. int status;
  50. bool cable_plugged;
  51. struct gpio_desc *charge_finished;
  52. };
  53. static struct gab *to_generic_bat(struct power_supply *psy)
  54. {
  55. return power_supply_get_drvdata(psy);
  56. }
  57. static void gab_ext_power_changed(struct power_supply *psy)
  58. {
  59. struct gab *adc_bat = to_generic_bat(psy);
  60. schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
  61. }
  62. static const enum power_supply_property gab_props[] = {
  63. POWER_SUPPLY_PROP_STATUS,
  64. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  65. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  66. POWER_SUPPLY_PROP_CHARGE_NOW,
  67. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  68. POWER_SUPPLY_PROP_CURRENT_NOW,
  69. POWER_SUPPLY_PROP_TECHNOLOGY,
  70. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  71. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  72. POWER_SUPPLY_PROP_MODEL_NAME,
  73. };
  74. /*
  75. * This properties are set based on the received platform data and this
  76. * should correspond one-to-one with enum chan_type.
  77. */
  78. static const enum power_supply_property gab_dyn_props[] = {
  79. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  80. POWER_SUPPLY_PROP_CURRENT_NOW,
  81. POWER_SUPPLY_PROP_POWER_NOW,
  82. };
  83. static bool gab_charge_finished(struct gab *adc_bat)
  84. {
  85. if (!adc_bat->charge_finished)
  86. return false;
  87. return gpiod_get_value(adc_bat->charge_finished);
  88. }
  89. static int gab_get_status(struct gab *adc_bat)
  90. {
  91. struct gab_platform_data *pdata = adc_bat->pdata;
  92. struct power_supply_info *bat_info;
  93. bat_info = &pdata->battery_info;
  94. if (adc_bat->level == bat_info->charge_full_design)
  95. return POWER_SUPPLY_STATUS_FULL;
  96. return adc_bat->status;
  97. }
  98. static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
  99. {
  100. switch (psp) {
  101. case POWER_SUPPLY_PROP_POWER_NOW:
  102. return GAB_POWER;
  103. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  104. return GAB_VOLTAGE;
  105. case POWER_SUPPLY_PROP_CURRENT_NOW:
  106. return GAB_CURRENT;
  107. default:
  108. WARN_ON(1);
  109. break;
  110. }
  111. return GAB_POWER;
  112. }
  113. static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
  114. int *result)
  115. {
  116. int ret;
  117. int chan_index;
  118. chan_index = gab_prop_to_chan(psp);
  119. ret = iio_read_channel_processed(adc_bat->channel[chan_index],
  120. result);
  121. if (ret < 0)
  122. pr_err("read channel error\n");
  123. else
  124. *result *= 1000;
  125. return ret;
  126. }
  127. static int gab_get_property(struct power_supply *psy,
  128. enum power_supply_property psp, union power_supply_propval *val)
  129. {
  130. struct gab *adc_bat;
  131. struct gab_platform_data *pdata;
  132. struct power_supply_info *bat_info;
  133. int result = 0;
  134. int ret = 0;
  135. adc_bat = to_generic_bat(psy);
  136. if (!adc_bat) {
  137. dev_err(&psy->dev, "no battery infos ?!\n");
  138. return -EINVAL;
  139. }
  140. pdata = adc_bat->pdata;
  141. bat_info = &pdata->battery_info;
  142. switch (psp) {
  143. case POWER_SUPPLY_PROP_STATUS:
  144. val->intval = gab_get_status(adc_bat);
  145. break;
  146. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  147. val->intval = 0;
  148. break;
  149. case POWER_SUPPLY_PROP_CHARGE_NOW:
  150. val->intval = pdata->cal_charge(result);
  151. break;
  152. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  153. case POWER_SUPPLY_PROP_CURRENT_NOW:
  154. case POWER_SUPPLY_PROP_POWER_NOW:
  155. ret = read_channel(adc_bat, psp, &result);
  156. if (ret < 0)
  157. goto err;
  158. val->intval = result;
  159. break;
  160. case POWER_SUPPLY_PROP_TECHNOLOGY:
  161. val->intval = bat_info->technology;
  162. break;
  163. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  164. val->intval = bat_info->voltage_min_design;
  165. break;
  166. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  167. val->intval = bat_info->voltage_max_design;
  168. break;
  169. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  170. val->intval = bat_info->charge_full_design;
  171. break;
  172. case POWER_SUPPLY_PROP_MODEL_NAME:
  173. val->strval = bat_info->name;
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. err:
  179. return ret;
  180. }
  181. static void gab_work(struct work_struct *work)
  182. {
  183. struct gab *adc_bat;
  184. struct delayed_work *delayed_work;
  185. bool is_plugged;
  186. int status;
  187. delayed_work = to_delayed_work(work);
  188. adc_bat = container_of(delayed_work, struct gab, bat_work);
  189. status = adc_bat->status;
  190. is_plugged = power_supply_am_i_supplied(adc_bat->psy);
  191. adc_bat->cable_plugged = is_plugged;
  192. if (!is_plugged)
  193. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  194. else if (gab_charge_finished(adc_bat))
  195. adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  196. else
  197. adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
  198. if (status != adc_bat->status)
  199. power_supply_changed(adc_bat->psy);
  200. }
  201. static irqreturn_t gab_charged(int irq, void *dev_id)
  202. {
  203. struct gab *adc_bat = dev_id;
  204. struct gab_platform_data *pdata = adc_bat->pdata;
  205. int delay;
  206. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  207. schedule_delayed_work(&adc_bat->bat_work,
  208. msecs_to_jiffies(delay));
  209. return IRQ_HANDLED;
  210. }
  211. static int gab_probe(struct platform_device *pdev)
  212. {
  213. struct gab *adc_bat;
  214. struct power_supply_desc *psy_desc;
  215. struct power_supply_config psy_cfg = {};
  216. struct gab_platform_data *pdata = pdev->dev.platform_data;
  217. enum power_supply_property *properties;
  218. int ret = 0;
  219. int chan;
  220. int index = ARRAY_SIZE(gab_props);
  221. bool any = false;
  222. adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
  223. if (!adc_bat) {
  224. dev_err(&pdev->dev, "failed to allocate memory\n");
  225. return -ENOMEM;
  226. }
  227. psy_cfg.drv_data = adc_bat;
  228. psy_desc = &adc_bat->psy_desc;
  229. psy_desc->name = pdata->battery_info.name;
  230. /* bootup default values for the battery */
  231. adc_bat->cable_plugged = false;
  232. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  233. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  234. psy_desc->get_property = gab_get_property;
  235. psy_desc->external_power_changed = gab_ext_power_changed;
  236. adc_bat->pdata = pdata;
  237. /*
  238. * copying the static properties and allocating extra memory for holding
  239. * the extra configurable properties received from platform data.
  240. */
  241. properties = kcalloc(ARRAY_SIZE(gab_props) +
  242. ARRAY_SIZE(gab_chan_name),
  243. sizeof(*properties),
  244. GFP_KERNEL);
  245. if (!properties) {
  246. ret = -ENOMEM;
  247. goto first_mem_fail;
  248. }
  249. memcpy(properties, gab_props, sizeof(gab_props));
  250. /*
  251. * getting channel from iio and copying the battery properties
  252. * based on the channel supported by consumer device.
  253. */
  254. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  255. adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
  256. gab_chan_name[chan]);
  257. if (IS_ERR(adc_bat->channel[chan])) {
  258. ret = PTR_ERR(adc_bat->channel[chan]);
  259. adc_bat->channel[chan] = NULL;
  260. } else {
  261. /* copying properties for supported channels only */
  262. int index2;
  263. for (index2 = 0; index2 < index; index2++) {
  264. if (properties[index2] == gab_dyn_props[chan])
  265. break; /* already known */
  266. }
  267. if (index2 == index) /* really new */
  268. properties[index++] = gab_dyn_props[chan];
  269. any = true;
  270. }
  271. }
  272. /* none of the channels are supported so let's bail out */
  273. if (!any) {
  274. ret = -ENODEV;
  275. goto second_mem_fail;
  276. }
  277. /*
  278. * Total number of properties is equal to static properties
  279. * plus the dynamic properties.Some properties may not be set
  280. * as come channels may be not be supported by the device.So
  281. * we need to take care of that.
  282. */
  283. psy_desc->properties = properties;
  284. psy_desc->num_properties = index;
  285. adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
  286. if (IS_ERR(adc_bat->psy)) {
  287. ret = PTR_ERR(adc_bat->psy);
  288. goto err_reg_fail;
  289. }
  290. INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
  291. adc_bat->charge_finished = devm_gpiod_get_optional(&pdev->dev,
  292. "charged", GPIOD_IN);
  293. if (adc_bat->charge_finished) {
  294. int irq;
  295. irq = gpiod_to_irq(adc_bat->charge_finished);
  296. ret = request_any_context_irq(irq, gab_charged,
  297. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  298. "battery charged", adc_bat);
  299. if (ret < 0)
  300. goto gpio_req_fail;
  301. }
  302. platform_set_drvdata(pdev, adc_bat);
  303. /* Schedule timer to check current status */
  304. schedule_delayed_work(&adc_bat->bat_work,
  305. msecs_to_jiffies(0));
  306. return 0;
  307. gpio_req_fail:
  308. power_supply_unregister(adc_bat->psy);
  309. err_reg_fail:
  310. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  311. if (adc_bat->channel[chan])
  312. iio_channel_release(adc_bat->channel[chan]);
  313. }
  314. second_mem_fail:
  315. kfree(properties);
  316. first_mem_fail:
  317. return ret;
  318. }
  319. static int gab_remove(struct platform_device *pdev)
  320. {
  321. int chan;
  322. struct gab *adc_bat = platform_get_drvdata(pdev);
  323. power_supply_unregister(adc_bat->psy);
  324. if (adc_bat->charge_finished)
  325. free_irq(gpiod_to_irq(adc_bat->charge_finished), adc_bat);
  326. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  327. if (adc_bat->channel[chan])
  328. iio_channel_release(adc_bat->channel[chan]);
  329. }
  330. kfree(adc_bat->psy_desc.properties);
  331. cancel_delayed_work_sync(&adc_bat->bat_work);
  332. return 0;
  333. }
  334. static int __maybe_unused gab_suspend(struct device *dev)
  335. {
  336. struct gab *adc_bat = dev_get_drvdata(dev);
  337. cancel_delayed_work_sync(&adc_bat->bat_work);
  338. adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  339. return 0;
  340. }
  341. static int __maybe_unused gab_resume(struct device *dev)
  342. {
  343. struct gab *adc_bat = dev_get_drvdata(dev);
  344. struct gab_platform_data *pdata = adc_bat->pdata;
  345. int delay;
  346. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  347. /* Schedule timer to check current status */
  348. schedule_delayed_work(&adc_bat->bat_work,
  349. msecs_to_jiffies(delay));
  350. return 0;
  351. }
  352. static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume);
  353. static struct platform_driver gab_driver = {
  354. .driver = {
  355. .name = "generic-adc-battery",
  356. .pm = &gab_pm_ops,
  357. },
  358. .probe = gab_probe,
  359. .remove = gab_remove,
  360. };
  361. module_platform_driver(gab_driver);
  362. MODULE_AUTHOR("anish kumar <[email protected]>");
  363. MODULE_DESCRIPTION("generic battery driver using IIO");
  364. MODULE_LICENSE("GPL");