s3c_adc_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // iPAQ h1930/h1940/rx1950 battery controller driver
  4. // Copyright (c) Vasily Khoruzhick
  5. // Based on h1940_battery.c by Arnaud Patard
  6. #include <linux/interrupt.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/power_supply.h>
  9. #include <linux/leds.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/err.h>
  12. #include <linux/timer.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/s3c_adc_battery.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/soc/samsung/s3c-adc.h>
  19. #define BAT_POLL_INTERVAL 10000 /* ms */
  20. #define JITTER_DELAY 500 /* ms */
  21. struct s3c_adc_bat {
  22. struct power_supply *psy;
  23. struct s3c_adc_client *client;
  24. struct s3c_adc_bat_pdata *pdata;
  25. struct gpio_desc *charge_finished;
  26. int volt_value;
  27. int cur_value;
  28. unsigned int timestamp;
  29. int level;
  30. int status;
  31. int cable_plugged:1;
  32. };
  33. static struct delayed_work bat_work;
  34. static void s3c_adc_bat_ext_power_changed(struct power_supply *psy)
  35. {
  36. schedule_delayed_work(&bat_work,
  37. msecs_to_jiffies(JITTER_DELAY));
  38. }
  39. static int gather_samples(struct s3c_adc_client *client, int num, int channel)
  40. {
  41. int value, i;
  42. /* default to 1 if nothing is set */
  43. if (num < 1)
  44. num = 1;
  45. value = 0;
  46. for (i = 0; i < num; i++)
  47. value += s3c_adc_read(client, channel);
  48. value /= num;
  49. return value;
  50. }
  51. static enum power_supply_property s3c_adc_backup_bat_props[] = {
  52. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  53. POWER_SUPPLY_PROP_VOLTAGE_MIN,
  54. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  55. };
  56. static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
  57. enum power_supply_property psp,
  58. union power_supply_propval *val)
  59. {
  60. struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
  61. if (!bat) {
  62. dev_err(&psy->dev, "%s: no battery infos ?!\n", __func__);
  63. return -EINVAL;
  64. }
  65. if (bat->volt_value < 0 ||
  66. jiffies_to_msecs(jiffies - bat->timestamp) >
  67. BAT_POLL_INTERVAL) {
  68. bat->volt_value = gather_samples(bat->client,
  69. bat->pdata->backup_volt_samples,
  70. bat->pdata->backup_volt_channel);
  71. bat->volt_value *= bat->pdata->backup_volt_mult;
  72. bat->timestamp = jiffies;
  73. }
  74. switch (psp) {
  75. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  76. val->intval = bat->volt_value;
  77. return 0;
  78. case POWER_SUPPLY_PROP_VOLTAGE_MIN:
  79. val->intval = bat->pdata->backup_volt_min;
  80. return 0;
  81. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  82. val->intval = bat->pdata->backup_volt_max;
  83. return 0;
  84. default:
  85. return -EINVAL;
  86. }
  87. }
  88. static const struct power_supply_desc backup_bat_desc = {
  89. .name = "backup-battery",
  90. .type = POWER_SUPPLY_TYPE_BATTERY,
  91. .properties = s3c_adc_backup_bat_props,
  92. .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
  93. .get_property = s3c_adc_backup_bat_get_property,
  94. .use_for_apm = 1,
  95. };
  96. static struct s3c_adc_bat backup_bat;
  97. static enum power_supply_property s3c_adc_main_bat_props[] = {
  98. POWER_SUPPLY_PROP_STATUS,
  99. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  100. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  101. POWER_SUPPLY_PROP_CHARGE_NOW,
  102. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  103. POWER_SUPPLY_PROP_CURRENT_NOW,
  104. };
  105. static int calc_full_volt(int volt_val, int cur_val, int impedance)
  106. {
  107. return volt_val + cur_val * impedance / 1000;
  108. }
  109. static int charge_finished(struct s3c_adc_bat *bat)
  110. {
  111. return gpiod_get_value(bat->charge_finished);
  112. }
  113. static int s3c_adc_bat_get_property(struct power_supply *psy,
  114. enum power_supply_property psp,
  115. union power_supply_propval *val)
  116. {
  117. struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
  118. int new_level;
  119. int full_volt;
  120. const struct s3c_adc_bat_thresh *lut;
  121. unsigned int lut_size;
  122. if (!bat) {
  123. dev_err(&psy->dev, "no battery infos ?!\n");
  124. return -EINVAL;
  125. }
  126. lut = bat->pdata->lut_noac;
  127. lut_size = bat->pdata->lut_noac_cnt;
  128. if (bat->volt_value < 0 || bat->cur_value < 0 ||
  129. jiffies_to_msecs(jiffies - bat->timestamp) >
  130. BAT_POLL_INTERVAL) {
  131. bat->volt_value = gather_samples(bat->client,
  132. bat->pdata->volt_samples,
  133. bat->pdata->volt_channel) * bat->pdata->volt_mult;
  134. bat->cur_value = gather_samples(bat->client,
  135. bat->pdata->current_samples,
  136. bat->pdata->current_channel) * bat->pdata->current_mult;
  137. bat->timestamp = jiffies;
  138. }
  139. if (bat->cable_plugged &&
  140. (!bat->charge_finished ||
  141. !charge_finished(bat))) {
  142. lut = bat->pdata->lut_acin;
  143. lut_size = bat->pdata->lut_acin_cnt;
  144. }
  145. new_level = 100000;
  146. full_volt = calc_full_volt((bat->volt_value / 1000),
  147. (bat->cur_value / 1000), bat->pdata->internal_impedance);
  148. if (full_volt < calc_full_volt(lut->volt, lut->cur,
  149. bat->pdata->internal_impedance)) {
  150. lut_size--;
  151. while (lut_size--) {
  152. int lut_volt1;
  153. int lut_volt2;
  154. lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
  155. bat->pdata->internal_impedance);
  156. lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
  157. bat->pdata->internal_impedance);
  158. if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
  159. new_level = (lut[1].level +
  160. (lut[0].level - lut[1].level) *
  161. (full_volt - lut_volt2) /
  162. (lut_volt1 - lut_volt2)) * 1000;
  163. break;
  164. }
  165. new_level = lut[1].level * 1000;
  166. lut++;
  167. }
  168. }
  169. bat->level = new_level;
  170. switch (psp) {
  171. case POWER_SUPPLY_PROP_STATUS:
  172. if (!bat->charge_finished)
  173. val->intval = bat->level == 100000 ?
  174. POWER_SUPPLY_STATUS_FULL : bat->status;
  175. else
  176. val->intval = bat->status;
  177. return 0;
  178. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  179. val->intval = 100000;
  180. return 0;
  181. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  182. val->intval = 0;
  183. return 0;
  184. case POWER_SUPPLY_PROP_CHARGE_NOW:
  185. val->intval = bat->level;
  186. return 0;
  187. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  188. val->intval = bat->volt_value;
  189. return 0;
  190. case POWER_SUPPLY_PROP_CURRENT_NOW:
  191. val->intval = bat->cur_value;
  192. return 0;
  193. default:
  194. return -EINVAL;
  195. }
  196. }
  197. static const struct power_supply_desc main_bat_desc = {
  198. .name = "main-battery",
  199. .type = POWER_SUPPLY_TYPE_BATTERY,
  200. .properties = s3c_adc_main_bat_props,
  201. .num_properties = ARRAY_SIZE(s3c_adc_main_bat_props),
  202. .get_property = s3c_adc_bat_get_property,
  203. .external_power_changed = s3c_adc_bat_ext_power_changed,
  204. .use_for_apm = 1,
  205. };
  206. static struct s3c_adc_bat main_bat;
  207. static void s3c_adc_bat_work(struct work_struct *work)
  208. {
  209. struct s3c_adc_bat *bat = &main_bat;
  210. int is_charged;
  211. int is_plugged;
  212. static int was_plugged;
  213. is_plugged = power_supply_am_i_supplied(bat->psy);
  214. bat->cable_plugged = is_plugged;
  215. if (is_plugged != was_plugged) {
  216. was_plugged = is_plugged;
  217. if (is_plugged) {
  218. if (bat->pdata->enable_charger)
  219. bat->pdata->enable_charger();
  220. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  221. } else {
  222. if (bat->pdata->disable_charger)
  223. bat->pdata->disable_charger();
  224. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  225. }
  226. } else {
  227. if (bat->charge_finished && is_plugged) {
  228. is_charged = charge_finished(&main_bat);
  229. if (is_charged) {
  230. if (bat->pdata->disable_charger)
  231. bat->pdata->disable_charger();
  232. bat->status = POWER_SUPPLY_STATUS_FULL;
  233. } else {
  234. if (bat->pdata->enable_charger)
  235. bat->pdata->enable_charger();
  236. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  237. }
  238. }
  239. }
  240. power_supply_changed(bat->psy);
  241. }
  242. static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
  243. {
  244. schedule_delayed_work(&bat_work,
  245. msecs_to_jiffies(JITTER_DELAY));
  246. return IRQ_HANDLED;
  247. }
  248. static int s3c_adc_bat_probe(struct platform_device *pdev)
  249. {
  250. struct s3c_adc_client *client;
  251. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  252. struct power_supply_config psy_cfg = {};
  253. struct gpio_desc *gpiod;
  254. int ret;
  255. client = s3c_adc_register(pdev, NULL, NULL, 0);
  256. if (IS_ERR(client)) {
  257. dev_err(&pdev->dev, "cannot register adc\n");
  258. return PTR_ERR(client);
  259. }
  260. platform_set_drvdata(pdev, client);
  261. gpiod = devm_gpiod_get_optional(&pdev->dev, "charge-status", GPIOD_IN);
  262. if (IS_ERR(gpiod)) {
  263. /* Could be probe deferral etc */
  264. ret = PTR_ERR(gpiod);
  265. dev_err(&pdev->dev, "no GPIO %d\n", ret);
  266. return ret;
  267. }
  268. main_bat.client = client;
  269. main_bat.pdata = pdata;
  270. main_bat.charge_finished = gpiod;
  271. main_bat.volt_value = -1;
  272. main_bat.cur_value = -1;
  273. main_bat.cable_plugged = 0;
  274. main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
  275. psy_cfg.drv_data = &main_bat;
  276. main_bat.psy = power_supply_register(&pdev->dev, &main_bat_desc, &psy_cfg);
  277. if (IS_ERR(main_bat.psy)) {
  278. ret = PTR_ERR(main_bat.psy);
  279. goto err_reg_main;
  280. }
  281. if (pdata->backup_volt_mult) {
  282. const struct power_supply_config backup_psy_cfg
  283. = { .drv_data = &backup_bat, };
  284. backup_bat.client = client;
  285. backup_bat.pdata = pdev->dev.platform_data;
  286. backup_bat.charge_finished = gpiod;
  287. backup_bat.volt_value = -1;
  288. backup_bat.psy = power_supply_register(&pdev->dev,
  289. &backup_bat_desc,
  290. &backup_psy_cfg);
  291. if (IS_ERR(backup_bat.psy)) {
  292. ret = PTR_ERR(backup_bat.psy);
  293. goto err_reg_backup;
  294. }
  295. }
  296. INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
  297. if (gpiod) {
  298. ret = request_irq(gpiod_to_irq(gpiod),
  299. s3c_adc_bat_charged,
  300. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  301. "battery charged", NULL);
  302. if (ret)
  303. goto err_irq;
  304. }
  305. if (pdata->init) {
  306. ret = pdata->init();
  307. if (ret)
  308. goto err_platform;
  309. }
  310. dev_info(&pdev->dev, "successfully loaded\n");
  311. device_init_wakeup(&pdev->dev, 1);
  312. /* Schedule timer to check current status */
  313. schedule_delayed_work(&bat_work,
  314. msecs_to_jiffies(JITTER_DELAY));
  315. return 0;
  316. err_platform:
  317. if (gpiod)
  318. free_irq(gpiod_to_irq(gpiod), NULL);
  319. err_irq:
  320. if (pdata->backup_volt_mult)
  321. power_supply_unregister(backup_bat.psy);
  322. err_reg_backup:
  323. power_supply_unregister(main_bat.psy);
  324. err_reg_main:
  325. return ret;
  326. }
  327. static int s3c_adc_bat_remove(struct platform_device *pdev)
  328. {
  329. struct s3c_adc_client *client = platform_get_drvdata(pdev);
  330. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  331. power_supply_unregister(main_bat.psy);
  332. if (pdata->backup_volt_mult)
  333. power_supply_unregister(backup_bat.psy);
  334. s3c_adc_release(client);
  335. if (main_bat.charge_finished)
  336. free_irq(gpiod_to_irq(main_bat.charge_finished), NULL);
  337. cancel_delayed_work_sync(&bat_work);
  338. if (pdata->exit)
  339. pdata->exit();
  340. return 0;
  341. }
  342. #ifdef CONFIG_PM
  343. static int s3c_adc_bat_suspend(struct platform_device *pdev,
  344. pm_message_t state)
  345. {
  346. if (main_bat.charge_finished) {
  347. if (device_may_wakeup(&pdev->dev))
  348. enable_irq_wake(
  349. gpiod_to_irq(main_bat.charge_finished));
  350. else {
  351. disable_irq(gpiod_to_irq(main_bat.charge_finished));
  352. main_bat.pdata->disable_charger();
  353. }
  354. }
  355. return 0;
  356. }
  357. static int s3c_adc_bat_resume(struct platform_device *pdev)
  358. {
  359. if (main_bat.charge_finished) {
  360. if (device_may_wakeup(&pdev->dev))
  361. disable_irq_wake(
  362. gpiod_to_irq(main_bat.charge_finished));
  363. else
  364. enable_irq(gpiod_to_irq(main_bat.charge_finished));
  365. }
  366. /* Schedule timer to check current status */
  367. schedule_delayed_work(&bat_work,
  368. msecs_to_jiffies(JITTER_DELAY));
  369. return 0;
  370. }
  371. #else
  372. #define s3c_adc_bat_suspend NULL
  373. #define s3c_adc_bat_resume NULL
  374. #endif
  375. static struct platform_driver s3c_adc_bat_driver = {
  376. .driver = {
  377. .name = "s3c-adc-battery",
  378. },
  379. .probe = s3c_adc_bat_probe,
  380. .remove = s3c_adc_bat_remove,
  381. .suspend = s3c_adc_bat_suspend,
  382. .resume = s3c_adc_bat_resume,
  383. };
  384. module_platform_driver(s3c_adc_bat_driver);
  385. MODULE_AUTHOR("Vasily Khoruzhick <[email protected]>");
  386. MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
  387. MODULE_LICENSE("GPL");