s3c_adc_battery.c 11 KB

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