generic-adc-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 = 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. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  292. int irq;
  293. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  294. if (ret)
  295. goto gpio_req_fail;
  296. irq = gpio_to_irq(pdata->gpio_charge_finished);
  297. ret = request_any_context_irq(irq, gab_charged,
  298. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  299. "battery charged", adc_bat);
  300. if (ret < 0)
  301. goto err_gpio;
  302. }
  303. platform_set_drvdata(pdev, adc_bat);
  304. /* Schedule timer to check current status */
  305. schedule_delayed_work(&adc_bat->bat_work,
  306. msecs_to_jiffies(0));
  307. return 0;
  308. err_gpio:
  309. gpio_free(pdata->gpio_charge_finished);
  310. gpio_req_fail:
  311. power_supply_unregister(adc_bat->psy);
  312. err_reg_fail:
  313. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  314. if (adc_bat->channel[chan])
  315. iio_channel_release(adc_bat->channel[chan]);
  316. }
  317. second_mem_fail:
  318. kfree(properties);
  319. first_mem_fail:
  320. return ret;
  321. }
  322. static int gab_remove(struct platform_device *pdev)
  323. {
  324. int chan;
  325. struct gab *adc_bat = platform_get_drvdata(pdev);
  326. struct gab_platform_data *pdata = adc_bat->pdata;
  327. power_supply_unregister(adc_bat->psy);
  328. if (gpio_is_valid(pdata->gpio_charge_finished)) {
  329. free_irq(gpio_to_irq(pdata->gpio_charge_finished), adc_bat);
  330. gpio_free(pdata->gpio_charge_finished);
  331. }
  332. for (chan = 0; chan < ARRAY_SIZE(gab_chan_name); chan++) {
  333. if (adc_bat->channel[chan])
  334. iio_channel_release(adc_bat->channel[chan]);
  335. }
  336. kfree(adc_bat->psy_desc.properties);
  337. cancel_delayed_work_sync(&adc_bat->bat_work);
  338. return 0;
  339. }
  340. static int __maybe_unused gab_suspend(struct device *dev)
  341. {
  342. struct gab *adc_bat = dev_get_drvdata(dev);
  343. cancel_delayed_work_sync(&adc_bat->bat_work);
  344. adc_bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  345. return 0;
  346. }
  347. static int __maybe_unused gab_resume(struct device *dev)
  348. {
  349. struct gab *adc_bat = dev_get_drvdata(dev);
  350. struct gab_platform_data *pdata = adc_bat->pdata;
  351. int delay;
  352. delay = pdata->jitter_delay ? pdata->jitter_delay : JITTER_DEFAULT;
  353. /* Schedule timer to check current status */
  354. schedule_delayed_work(&adc_bat->bat_work,
  355. msecs_to_jiffies(delay));
  356. return 0;
  357. }
  358. static SIMPLE_DEV_PM_OPS(gab_pm_ops, gab_suspend, gab_resume);
  359. static struct platform_driver gab_driver = {
  360. .driver = {
  361. .name = "generic-adc-battery",
  362. .pm = &gab_pm_ops,
  363. },
  364. .probe = gab_probe,
  365. .remove = gab_remove,
  366. };
  367. module_platform_driver(gab_driver);
  368. MODULE_AUTHOR("anish kumar <anish198519851985@gmail.com>");
  369. MODULE_DESCRIPTION("generic battery driver using IIO");
  370. MODULE_LICENSE("GPL");