generic-adc-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 iio_channel *channel[GAB_MAX_CHAN_TYPE];
  45. struct gab_platform_data *pdata;
  46. struct delayed_work bat_work;
  47. int level;
  48. int status;
  49. bool cable_plugged;
  50. };
  51. static struct gab *to_generic_bat(struct power_supply *psy)
  52. {
  53. return container_of(psy, struct gab, psy);
  54. }
  55. static void gab_ext_power_changed(struct power_supply *psy)
  56. {
  57. struct gab *adc_bat = to_generic_bat(psy);
  58. schedule_delayed_work(&adc_bat->bat_work, msecs_to_jiffies(0));
  59. }
  60. static const enum power_supply_property gab_props[] = {
  61. POWER_SUPPLY_PROP_STATUS,
  62. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  63. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  64. POWER_SUPPLY_PROP_CHARGE_NOW,
  65. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  66. POWER_SUPPLY_PROP_CURRENT_NOW,
  67. POWER_SUPPLY_PROP_TECHNOLOGY,
  68. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  69. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  70. POWER_SUPPLY_PROP_MODEL_NAME,
  71. };
  72. /*
  73. * This properties are set based on the received platform data and this
  74. * should correspond one-to-one with enum chan_type.
  75. */
  76. static const enum power_supply_property gab_dyn_props[] = {
  77. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  78. POWER_SUPPLY_PROP_CURRENT_NOW,
  79. POWER_SUPPLY_PROP_POWER_NOW,
  80. };
  81. static bool gab_charge_finished(struct gab *adc_bat)
  82. {
  83. struct gab_platform_data *pdata = adc_bat->pdata;
  84. bool ret = gpio_get_value(pdata->gpio_charge_finished);
  85. bool inv = pdata->gpio_inverted;
  86. if (!gpio_is_valid(pdata->gpio_charge_finished))
  87. return false;
  88. return ret ^ inv;
  89. }
  90. static int gab_get_status(struct gab *adc_bat)
  91. {
  92. struct gab_platform_data *pdata = adc_bat->pdata;
  93. struct power_supply_info *bat_info;
  94. bat_info = &pdata->battery_info;
  95. if (adc_bat->level == bat_info->charge_full_design)
  96. return POWER_SUPPLY_STATUS_FULL;
  97. return adc_bat->status;
  98. }
  99. static enum gab_chan_type gab_prop_to_chan(enum power_supply_property psp)
  100. {
  101. switch (psp) {
  102. case POWER_SUPPLY_PROP_POWER_NOW:
  103. return GAB_POWER;
  104. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  105. return GAB_VOLTAGE;
  106. case POWER_SUPPLY_PROP_CURRENT_NOW:
  107. return GAB_CURRENT;
  108. default:
  109. WARN_ON(1);
  110. break;
  111. }
  112. return GAB_POWER;
  113. }
  114. static int read_channel(struct gab *adc_bat, enum power_supply_property psp,
  115. int *result)
  116. {
  117. int ret;
  118. int chan_index;
  119. chan_index = gab_prop_to_chan(psp);
  120. ret = iio_read_channel_processed(adc_bat->channel[chan_index],
  121. result);
  122. if (ret < 0)
  123. pr_err("read channel error\n");
  124. return ret;
  125. }
  126. static int gab_get_property(struct power_supply *psy,
  127. enum power_supply_property psp, union power_supply_propval *val)
  128. {
  129. struct gab *adc_bat;
  130. struct gab_platform_data *pdata;
  131. struct power_supply_info *bat_info;
  132. int result = 0;
  133. int ret = 0;
  134. adc_bat = to_generic_bat(psy);
  135. if (!adc_bat) {
  136. dev_err(psy->dev, "no battery infos ?!\n");
  137. return -EINVAL;
  138. }
  139. pdata = adc_bat->pdata;
  140. bat_info = &pdata->battery_info;
  141. switch (psp) {
  142. case POWER_SUPPLY_PROP_STATUS:
  143. gab_get_status(adc_bat);
  144. break;
  145. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  146. val->intval = 0;
  147. break;
  148. case POWER_SUPPLY_PROP_CHARGE_NOW:
  149. val->intval = pdata->cal_charge(result);
  150. break;
  151. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  152. case POWER_SUPPLY_PROP_CURRENT_NOW:
  153. case POWER_SUPPLY_PROP_POWER_NOW:
  154. ret = read_channel(adc_bat, psp, &result);
  155. if (ret < 0)
  156. goto err;
  157. val->intval = result;
  158. break;
  159. case POWER_SUPPLY_PROP_TECHNOLOGY:
  160. val->intval = bat_info->technology;
  161. break;
  162. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  163. val->intval = bat_info->voltage_min_design;
  164. break;
  165. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  166. val->intval = bat_info->voltage_max_design;
  167. break;
  168. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  169. val->intval = bat_info->charge_full_design;
  170. break;
  171. case POWER_SUPPLY_PROP_MODEL_NAME:
  172. val->strval = bat_info->name;
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. err:
  178. return ret;
  179. }
  180. static void gab_work(struct work_struct *work)
  181. {
  182. struct gab *adc_bat;
  183. struct gab_platform_data *pdata;
  184. struct delayed_work *delayed_work;
  185. bool is_plugged;
  186. int status;
  187. delayed_work = container_of(work, struct delayed_work, work);
  188. adc_bat = container_of(delayed_work, struct gab, bat_work);
  189. pdata = adc_bat->pdata;
  190. status = adc_bat->status;
  191. is_plugged = power_supply_am_i_supplied(&adc_bat->psy);
  192. adc_bat->cable_plugged = is_plugged;
  193. if (!is_plugged)
  194. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  195. else if (gab_charge_finished(adc_bat))
  196. adc_bat->status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  197. else
  198. adc_bat->status = POWER_SUPPLY_STATUS_CHARGING;
  199. if (status != adc_bat->status)
  200. power_supply_changed(&adc_bat->psy);
  201. }
  202. static irqreturn_t gab_charged(int irq, void *dev_id)
  203. {
  204. struct gab *adc_bat = dev_id;
  205. struct gab_platform_data *pdata = adc_bat->pdata;
  206. int delay;
  207. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  208. schedule_delayed_work(&adc_bat->bat_work,
  209. msecs_to_jiffies(delay));
  210. return IRQ_HANDLED;
  211. }
  212. static int gab_probe(struct platform_device *pdev)
  213. {
  214. struct gab *adc_bat;
  215. struct power_supply *psy;
  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 = &adc_bat->psy;
  227. psy->name = pdata->battery_info.name;
  228. /* bootup default values for the battery */
  229. adc_bat->cable_plugged = false;
  230. adc_bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  231. psy->type = POWER_SUPPLY_TYPE_BATTERY;
  232. psy->get_property = gab_get_property;
  233. psy->external_power_changed = gab_ext_power_changed;
  234. adc_bat->pdata = pdata;
  235. /*
  236. * copying the static properties and allocating extra memory for holding
  237. * the extra configurable properties received from platform data.
  238. */
  239. psy->properties = kcalloc(ARRAY_SIZE(gab_props) +
  240. ARRAY_SIZE(gab_chan_name),
  241. sizeof(*psy->properties), GFP_KERNEL);
  242. if (!psy->properties) {
  243. ret = -ENOMEM;
  244. goto first_mem_fail;
  245. }
  246. memcpy(psy->properties, gab_props, sizeof(gab_props));
  247. properties = (enum power_supply_property *)
  248. ((char *)psy->properties + sizeof(gab_props));
  249. /*
  250. * getting channel from iio and copying the battery properties
  251. * based on the channel supported by consumer device.
  252. */
  253. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  254. adc_bat->channel[chan] = iio_channel_get(&pdev->dev,
  255. gab_chan_name[chan]);
  256. if (IS_ERR(adc_bat->channel[chan])) {
  257. ret = PTR_ERR(adc_bat->channel[chan]);
  258. adc_bat->channel[chan] = NULL;
  259. } else {
  260. /* copying properties for supported channels only */
  261. memcpy(properties + sizeof(*(psy->properties)) * index,
  262. &gab_dyn_props[chan],
  263. sizeof(gab_dyn_props[chan]));
  264. index++;
  265. }
  266. }
  267. /* none of the channels are supported so let's bail out */
  268. if (index == ARRAY_SIZE(gab_chan_name))
  269. goto second_mem_fail;
  270. /*
  271. * Total number of properties is equal to static properties
  272. * plus the dynamic properties.Some properties may not be set
  273. * as come channels may be not be supported by the device.So
  274. * we need to take care of that.
  275. */
  276. psy->num_properties = ARRAY_SIZE(gab_props) + index;
  277. ret = power_supply_register(&pdev->dev, psy);
  278. if (ret)
  279. goto err_reg_fail;
  280. INIT_DELAYED_WORK(&adc_bat->bat_work, gab_work);
  281. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  282. int irq;
  283. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  284. if (ret)
  285. goto gpio_req_fail;
  286. irq = gpio_to_irq(pdata->gpio_charge_finished);
  287. ret = request_any_context_irq(irq, gab_charged,
  288. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  289. "battery charged", adc_bat);
  290. if (ret < 0)
  291. goto err_gpio;
  292. }
  293. platform_set_drvdata(pdev, adc_bat);
  294. /* Schedule timer to check current status */
  295. schedule_delayed_work(&adc_bat->bat_work,
  296. msecs_to_jiffies(0));
  297. return 0;
  298. err_gpio:
  299. gpio_free(pdata->gpio_charge_finished);
  300. gpio_req_fail:
  301. power_supply_unregister(psy);
  302. err_reg_fail:
  303. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  304. if (adc_bat->channel[chan])
  305. iio_channel_release(adc_bat->channel[chan]);
  306. }
  307. second_mem_fail:
  308. kfree(psy->properties);
  309. first_mem_fail:
  310. return ret;
  311. }
  312. static int gab_remove(struct platform_device *pdev)
  313. {
  314. int chan;
  315. struct gab *adc_bat = platform_get_drvdata(pdev);
  316. struct gab_platform_data *pdata = adc_bat->pdata;
  317. power_supply_unregister(&adc_bat->psy);
  318. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  319. free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
  320. gpio_free(pdata->gpio_charge_finished);
  321. }
  322. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  323. if (adc_bat->channel[chan])
  324. iio_channel_release(adc_bat->channel[chan]);
  325. }
  326. kfree(adc_bat->psy.properties);
  327. cancel_delayed_work(&adc_bat->bat_work);
  328. return 0;
  329. }
  330. #ifdef CONFIG_PM
  331. static int gab_suspend(struct device *dev)
  332. {
  333. struct gab *adc_bat = dev_get_drvdata(dev);
  334. cancel_delayed_work_sync(&adc_bat->bat_work);
  335. adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  336. return 0;
  337. }
  338. static int gab_resume(struct device *dev)
  339. {
  340. struct gab *adc_bat = dev_get_drvdata(dev);
  341. struct gab_platform_data *pdata = adc_bat->pdata;
  342. int delay;
  343. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  344. /* Schedule timer to check current status */
  345. schedule_delayed_work(&adc_bat->bat_work,
  346. msecs_to_jiffies(delay));
  347. return 0;
  348. }
  349. static const struct dev_pm_ops gab_pm_ops = {
  350. .suspend = gab_suspend,
  351. .resume = gab_resume,
  352. };
  353. #define GAB_PM_OPS (&gab_pm_ops)
  354. #else
  355. #define GAB_PM_OPS (NULL)
  356. #endif
  357. static struct platform_driver gab_driver = {
  358. .driver = {
  359. .name = "generic-adc-battery",
  360. .owner = THIS_MODULE,
  361. .pm = GAB_PM_OPS
  362. },
  363. .probe = gab_probe,
  364. .remove = gab_remove,
  365. };
  366. module_platform_driver(gab_driver);
  367. MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
  368. MODULE_DESCRIPTION("generic battery driver using IIO");
  369. MODULE_LICENSE("GPL");