s3c_adc_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 <plat/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. int ret;
  259. client = s3c_adc_register(pdev, NULL, NULL, 0);
  260. if (IS_ERR(client)) {
  261. dev_err(&pdev->dev, "cannot register adc\n");
  262. return PTR_ERR(client);
  263. }
  264. platform_set_drvdata(pdev, client);
  265. main_bat.client = client;
  266. main_bat.pdata = pdata;
  267. main_bat.volt_value = -1;
  268. main_bat.cur_value = -1;
  269. main_bat.cable_plugged = 0;
  270. main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
  271. main_bat.psy = power_supply_register(&pdev->dev, &main_bat_desc, NULL);
  272. if (IS_ERR(main_bat.psy)) {
  273. ret = PTR_ERR(main_bat.psy);
  274. goto err_reg_main;
  275. }
  276. if (pdata->backup_volt_mult) {
  277. const struct power_supply_config psy_cfg
  278. = { .drv_data = &backup_bat, };
  279. backup_bat.client = client;
  280. backup_bat.pdata = pdev->dev.platform_data;
  281. backup_bat.volt_value = -1;
  282. backup_bat.psy = power_supply_register(&pdev->dev,
  283. &backup_bat_desc,
  284. &psy_cfg);
  285. if (IS_ERR(backup_bat.psy)) {
  286. ret = PTR_ERR(backup_bat.psy);
  287. goto err_reg_backup;
  288. }
  289. }
  290. INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
  291. if (pdata->gpio_charge_finished >= 0) {
  292. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  293. if (ret)
  294. goto err_gpio;
  295. ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
  296. s3c_adc_bat_charged,
  297. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  298. "battery charged", NULL);
  299. if (ret)
  300. goto err_irq;
  301. }
  302. if (pdata->init) {
  303. ret = pdata->init();
  304. if (ret)
  305. goto err_platform;
  306. }
  307. dev_info(&pdev->dev, "successfully loaded\n");
  308. device_init_wakeup(&pdev->dev, 1);
  309. /* Schedule timer to check current status */
  310. schedule_delayed_work(&bat_work,
  311. msecs_to_jiffies(JITTER_DELAY));
  312. return 0;
  313. err_platform:
  314. if (pdata->gpio_charge_finished >= 0)
  315. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  316. err_irq:
  317. if (pdata->gpio_charge_finished >= 0)
  318. gpio_free(pdata->gpio_charge_finished);
  319. err_gpio:
  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 (pdata->gpio_charge_finished >= 0) {
  336. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  337. gpio_free(pdata->gpio_charge_finished);
  338. }
  339. cancel_delayed_work(&bat_work);
  340. if (pdata->exit)
  341. pdata->exit();
  342. return 0;
  343. }
  344. #ifdef CONFIG_PM
  345. static int s3c_adc_bat_suspend(struct platform_device *pdev,
  346. pm_message_t state)
  347. {
  348. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  349. if (pdata->gpio_charge_finished >= 0) {
  350. if (device_may_wakeup(&pdev->dev))
  351. enable_irq_wake(
  352. gpio_to_irq(pdata->gpio_charge_finished));
  353. else {
  354. disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  355. main_bat.pdata->disable_charger();
  356. }
  357. }
  358. return 0;
  359. }
  360. static int s3c_adc_bat_resume(struct platform_device *pdev)
  361. {
  362. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  363. if (pdata->gpio_charge_finished >= 0) {
  364. if (device_may_wakeup(&pdev->dev))
  365. disable_irq_wake(
  366. gpio_to_irq(pdata->gpio_charge_finished));
  367. else
  368. enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  369. }
  370. /* Schedule timer to check current status */
  371. schedule_delayed_work(&bat_work,
  372. msecs_to_jiffies(JITTER_DELAY));
  373. return 0;
  374. }
  375. #else
  376. #define s3c_adc_bat_suspend NULL
  377. #define s3c_adc_bat_resume NULL
  378. #endif
  379. static struct platform_driver s3c_adc_bat_driver = {
  380. .driver = {
  381. .name = "s3c-adc-battery",
  382. },
  383. .probe = s3c_adc_bat_probe,
  384. .remove = s3c_adc_bat_remove,
  385. .suspend = s3c_adc_bat_suspend,
  386. .resume = s3c_adc_bat_resume,
  387. };
  388. module_platform_driver(s3c_adc_bat_driver);
  389. MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
  390. MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
  391. MODULE_LICENSE("GPL");