generic-adc-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 gab_platform_data *pdata;
  185. struct delayed_work *delayed_work;
  186. bool is_plugged;
  187. int status;
  188. delayed_work = to_delayed_work(work);
  189. adc_bat = container_of(delayed_work, struct gab, bat_work);
  190. pdata = adc_bat->pdata;
  191. status = adc_bat->status;
  192. is_plugged = power_supply_am_i_supplied(adc_bat->psy);
  193. adc_bat->cable_plugged = is_plugged;
  194. if (!is_plugged)
  195. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  196. else if (gab_charge_finished(adc_bat))
  197. adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  198. else
  199. adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
  200. if (status != adc_bat->status)
  201. power_supply_changed(adc_bat->psy);
  202. }
  203. static irqreturn_t gab_charged(int irq, void *dev_id)
  204. {
  205. struct gab *adc_bat = dev_id;
  206. struct gab_platform_data *pdata = adc_bat->pdata;
  207. int delay;
  208. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  209. schedule_delayed_work(&adc_bat->bat_work,
  210. msecs_to_jiffies(delay));
  211. return IRQ_HANDLED;
  212. }
  213. static int gab_probe(struct platform_device *pdev)
  214. {
  215. struct gab *adc_bat;
  216. struct power_supply_desc *psy_desc;
  217. struct power_supply_config psy_cfg = {};
  218. struct gab_platform_data *pdata = pdev->dev.platform_data;
  219. enum power_supply_property *properties;
  220. int ret = 0;
  221. int chan;
  222. int index = 0;
  223. adc_bat = devm_kzalloc(&pdev->dev, sizeof(*adc_bat), GFP_KERNEL);
  224. if (!adc_bat) {
  225. dev_err(&pdev->dev, "failed to allocate memory\n");
  226. return -ENOMEM;
  227. }
  228. psy_cfg.drv_data = adc_bat;
  229. psy_desc = &adc_bat->psy_desc;
  230. psy_desc->name = pdata->battery_info.name;
  231. /* bootup default values for the battery */
  232. adc_bat->cable_plugged = false;
  233. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  234. psy_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  235. psy_desc->get_property = gab_get_property;
  236. psy_desc->external_power_changed = gab_ext_power_changed;
  237. adc_bat->pdata = pdata;
  238. /*
  239. * copying the static properties and allocating extra memory for holding
  240. * the extra configurable properties received from platform data.
  241. */
  242. psy_desc->properties = kcalloc(ARRAY_SIZE(gab_props) +
  243. ARRAY_SIZE(gab_chan_name),
  244. sizeof(*psy_desc->properties),
  245. GFP_KERNEL);
  246. if (!psy_desc->properties) {
  247. ret = -ENOMEM;
  248. goto first_mem_fail;
  249. }
  250. memcpy(psy_desc->properties, gab_props, sizeof(gab_props));
  251. properties = (enum power_supply_property *)
  252. ((char *)psy_desc->properties + sizeof(gab_props));
  253. /*
  254. * getting channel from iio and copying the battery properties
  255. * based on the channel supported by consumer device.
  256. */
  257. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  258. adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
  259. gab_chan_name[chan]);
  260. if (IS_ERR(adc_bat->channel[chan])) {
  261. ret = PTR_ERR(adc_bat->channel[chan]);
  262. adc_bat->channel[chan] = NULL;
  263. } else {
  264. /* copying properties for supported channels only */
  265. memcpy(properties + sizeof(*(psy_desc->properties)) * index,
  266. &gab_dyn_props[chan],
  267. sizeof(gab_dyn_props[chan]));
  268. index++;
  269. }
  270. }
  271. /* none of the channels are supported so let's bail out */
  272. if (index == 0) {
  273. ret = -ENODEV;
  274. goto second_mem_fail;
  275. }
  276. /*
  277. * Total number of properties is equal to static properties
  278. * plus the dynamic properties.Some properties may not be set
  279. * as come channels may be not be supported by the device.So
  280. * we need to take care of that.
  281. */
  282. psy_desc->num_properties = ARRAY_SIZE(gab_props) + index;
  283. adc_bat->psy = power_supply_register(&pdev->dev, psy_desc, &psy_cfg);
  284. if (IS_ERR(adc_bat->psy)) {
  285. ret = PTR_ERR(adc_bat->psy);
  286. goto err_reg_fail;
  287. }
  288. INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
  289. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  290. int irq;
  291. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  292. if (ret)
  293. goto gpio_req_fail;
  294. irq = gpio_to_irq(pdata->gpio_charge_finished);
  295. ret = request_any_context_irq(irq, gab_charged,
  296. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  297. "battery charged", adc_bat);
  298. if (ret < 0)
  299. goto err_gpio;
  300. }
  301. platform_set_drvdata(pdev, adc_bat);
  302. /* Schedule timer to check current status */
  303. schedule_delayed_work(&adc_bat->bat_work,
  304. msecs_to_jiffies(0));
  305. return 0;
  306. err_gpio:
  307. gpio_free(pdata->gpio_charge_finished);
  308. gpio_req_fail:
  309. power_supply_unregister(adc_bat->psy);
  310. err_reg_fail:
  311. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  312. if (adc_bat->channel[chan])
  313. iio_channel_release(adc_bat->channel[chan]);
  314. }
  315. second_mem_fail:
  316. kfree(psy_desc->properties);
  317. first_mem_fail:
  318. return ret;
  319. }
  320. static int gab_remove(struct platform_device *pdev)
  321. {
  322. int chan;
  323. struct gab *adc_bat = platform_get_drvdata(pdev);
  324. struct gab_platform_data *pdata = adc_bat->pdata;
  325. power_supply_unregister(adc_bat->psy);
  326. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  327. free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
  328. gpio_free(pdata->gpio_charge_finished);
  329. }
  330. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  331. if (adc_bat->channel[chan])
  332. iio_channel_release(adc_bat->channel[chan]);
  333. }
  334. kfree(adc_bat->psy_desc.properties);
  335. cancel_delayed_work(&adc_bat->bat_work);
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int gab_suspend(struct device *dev)
  340. {
  341. struct gab *adc_bat = dev_get_drvdata(dev);
  342. cancel_delayed_work_sync(&adc_bat->bat_work);
  343. adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  344. return 0;
  345. }
  346. static int gab_resume(struct device *dev)
  347. {
  348. struct gab *adc_bat = dev_get_drvdata(dev);
  349. struct gab_platform_data *pdata = adc_bat->pdata;
  350. int delay;
  351. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  352. /* Schedule timer to check current status */
  353. schedule_delayed_work(&adc_bat->bat_work,
  354. msecs_to_jiffies(delay));
  355. return 0;
  356. }
  357. static const struct dev_pm_ops gab_pm_ops = {
  358. .suspend = gab_suspend,
  359. .resume = gab_resume,
  360. };
  361. #define GAB_PM_OPS (&gab_pm_ops)
  362. #else
  363. #define GAB_PM_OPS (NULL)
  364. #endif
  365. static struct platform_driver gab_driver = {
  366. .driver = {
  367. .name = "generic-adc-battery",
  368. .pm = GAB_PM_OPS
  369. },
  370. .probe = gab_probe,
  371. .remove = gab_remove,
  372. };
  373. module_platform_driver(gab_driver);
  374. MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
  375. MODULE_DESCRIPTION("generic battery driver using IIO");
  376. MODULE_LICENSE("GPL");