s3c_adc_battery.c 11 KB

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