generic-adc-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Generic battery driver code using IIO
  3. * Copyright (C) 2012, Anish Kumar <anish198519851985@gmail.com>
  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.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. };
  52. static struct gab *to_generic_bat(struct power_supply *psy)
  53. {
  54. return power_supply_get_drvdata(psy);
  55. }
  56. static void gab_ext_power_changed(struct power_supply *psy)
  57. {
  58. struct gab *adc_bat = to_generic_bat(psy);
  59. schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
  60. }
  61. static const enum power_supply_property gab_props[] = {
  62. POWER_SUPPLY_PROP_STATUS,
  63. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  64. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  65. POWER_SUPPLY_PROP_CHARGE_NOW,
  66. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  67. POWER_SUPPLY_PROP_CURRENT_NOW,
  68. POWER_SUPPLY_PROP_TECHNOLOGY,
  69. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  70. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  71. POWER_SUPPLY_PROP_MODEL_NAME,
  72. };
  73. /*
  74. * This properties are set based on the received platform data and this
  75. * should correspond one-to-one with enum chan_type.
  76. */
  77. static const enum power_supply_property gab_dyn_props[] = {
  78. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  79. POWER_SUPPLY_PROP_CURRENT_NOW,
  80. POWER_SUPPLY_PROP_POWER_NOW,
  81. };
  82. static bool gab_charge_finished(struct gab *adc_bat)
  83. {
  84. struct gab_platform_data *pdata = adc_bat->pdata;
  85. bool ret = gpio_get_value(pdata->gpio_charge_finished);
  86. bool inv = pdata->gpio_inverted;
  87. if (!gpio_is_valid(pdata->gpio_charge_finished))
  88. return false;
  89. return ret ^ inv;
  90. }
  91. static int gab_get_status(struct gab *adc_bat)
  92. {
  93. struct gab_platform_data *pdata = adc_bat->pdata;
  94. struct power_supply_info *bat_info;
  95. bat_info = &pdata->battery_info;
  96. if (adc_bat->level == bat_info->charge_full_design)
  97. return POWER_SUPPLY_STATUS_FULL;
  98. return adc_bat->status;
  99. }
  100. static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
  101. {
  102. switch (psp) {
  103. case POWER_SUPPLY_PROP_POWER_NOW:
  104. return GAB_POWER;
  105. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  106. return GAB_VOLTAGE;
  107. case POWER_SUPPLY_PROP_CURRENT_NOW:
  108. return GAB_CURRENT;
  109. default:
  110. WARN_ON(1);
  111. break;
  112. }
  113. return GAB_POWER;
  114. }
  115. static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
  116. int *result)
  117. {
  118. int ret;
  119. int chan_index;
  120. chan_index = gab_prop_to_chan(psp);
  121. ret = iio_read_channel_processed(adc_bat->channel[chan_index],
  122. result);
  123. if (ret < 0)
  124. pr_err("read channel error\n");
  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 = 0;
  221. adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
  222. if (!adc_bat) {
  223. dev_err(&pdev->dev, "failed to allocate memory\n");
  224. return -ENOMEM;
  225. }
  226. psy_cfg.drv_data = adc_bat;
  227. psy_desc = &adc_bat->psy_desc;
  228. psy_desc->name = pdata->battery_info.name;
  229. /* bootup default values for the battery */
  230. adc_bat->cable_plugged = false;
  231. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  232. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  233. psy_desc->get_property = gab_get_property;
  234. psy_desc->external_power_changed = gab_ext_power_changed;
  235. adc_bat->pdata = pdata;
  236. /*
  237. * copying the static properties and allocating extra memory for holding
  238. * the extra configurable properties received from platform data.
  239. */
  240. psy_desc->properties = kcalloc(ARRAY_SIZE(gab_props) +
  241. ARRAY_SIZE(gab_chan_name),
  242. sizeof(*psy_desc->properties),
  243. GFP_KERNEL);
  244. if (!psy_desc->properties) {
  245. ret = -ENOMEM;
  246. goto first_mem_fail;
  247. }
  248. memcpy(psy_desc->properties, gab_props, sizeof(gab_props));
  249. properties = (enum power_supply_property *)
  250. ((char *)psy_desc->properties + sizeof(gab_props));
  251. /*
  252. * getting channel from iio and copying the battery properties
  253. * based on the channel supported by consumer device.
  254. */
  255. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  256. adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
  257. gab_chan_name[chan]);
  258. if (IS_ERR(adc_bat->channel[chan])) {
  259. ret = PTR_ERR(adc_bat->channel[chan]);
  260. adc_bat->channel[chan] = NULL;
  261. } else {
  262. /* copying properties for supported channels only */
  263. memcpy(properties + sizeof(*(psy_desc->properties)) * index,
  264. &gab_dyn_props[chan],
  265. sizeof(gab_dyn_props[chan]));
  266. index++;
  267. }
  268. }
  269. /* none of the channels are supported so let's bail out */
  270. if (index == 0) {
  271. ret = -ENODEV;
  272. goto second_mem_fail;
  273. }
  274. /*
  275. * Total number of properties is equal to static properties
  276. * plus the dynamic properties.Some properties may not be set
  277. * as come channels may be not be supported by the device.So
  278. * we need to take care of that.
  279. */
  280. psy_desc->num_properties = ARRAY_SIZE(gab_props) + index;
  281. adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
  282. if (IS_ERR(adc_bat->psy)) {
  283. ret = PTR_ERR(adc_bat->psy);
  284. goto err_reg_fail;
  285. }
  286. INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
  287. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  288. int irq;
  289. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  290. if (ret)
  291. goto gpio_req_fail;
  292. irq = gpio_to_irq(pdata->gpio_charge_finished);
  293. ret = request_any_context_irq(irq, gab_charged,
  294. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  295. "battery charged", adc_bat);
  296. if (ret < 0)
  297. goto err_gpio;
  298. }
  299. platform_set_drvdata(pdev, adc_bat);
  300. /* Schedule timer to check current status */
  301. schedule_delayed_work(&adc_bat->bat_work,
  302. msecs_to_jiffies(0));
  303. return 0;
  304. err_gpio:
  305. gpio_free(pdata->gpio_charge_finished);
  306. gpio_req_fail:
  307. power_supply_unregister(adc_bat->psy);
  308. err_reg_fail:
  309. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  310. if (adc_bat->channel[chan])
  311. iio_channel_release(adc_bat->channel[chan]);
  312. }
  313. second_mem_fail:
  314. kfree(psy_desc->properties);
  315. first_mem_fail:
  316. return ret;
  317. }
  318. static int gab_remove(struct platform_device *pdev)
  319. {
  320. int chan;
  321. struct gab *adc_bat = platform_get_drvdata(pdev);
  322. struct gab_platform_data *pdata = adc_bat->pdata;
  323. power_supply_unregister(adc_bat->psy);
  324. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  325. free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
  326. gpio_free(pdata->gpio_charge_finished);
  327. }
  328. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  329. if (adc_bat->channel[chan])
  330. iio_channel_release(adc_bat->channel[chan]);
  331. }
  332. kfree(adc_bat->psy_desc.properties);
  333. cancel_delayed_work(&adc_bat->bat_work);
  334. return 0;
  335. }
  336. static int __maybe_unused gab_suspend(struct device *dev)
  337. {
  338. struct gab *adc_bat = dev_get_drvdata(dev);
  339. cancel_delayed_work_sync(&adc_bat->bat_work);
  340. adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  341. return 0;
  342. }
  343. static int __maybe_unused gab_resume(struct device *dev)
  344. {
  345. struct gab *adc_bat = dev_get_drvdata(dev);
  346. struct gab_platform_data *pdata = adc_bat->pdata;
  347. int delay;
  348. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  349. /* Schedule timer to check current status */
  350. schedule_delayed_work(&adc_bat->bat_work,
  351. msecs_to_jiffies(delay));
  352. return 0;
  353. }
  354. static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume);
  355. static struct platform_driver gab_driver = {
  356. .driver = {
  357. .name = "generic-adc-battery",
  358. .pm = &gab_pm_ops,
  359. },
  360. .probe = gab_probe,
  361. .remove = gab_remove,
  362. };
  363. module_platform_driver(gab_driver);
  364. MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
  365. MODULE_DESCRIPTION("generic battery driver using IIO");
  366. MODULE_LICENSE("GPL");