s3c_adc_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 = container_of(psy, struct s3c_adc_bat, 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 struct s3c_adc_bat backup_bat = {
  93. .psy = {
  94. .name = "backup-battery",
  95. .type = POWER_SUPPLY_TYPE_BATTERY,
  96. .properties = s3c_adc_backup_bat_props,
  97. .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
  98. .get_property = s3c_adc_backup_bat_get_property,
  99. .use_for_apm = 1,
  100. },
  101. };
  102. static enum power_supply_property s3c_adc_main_bat_props[] = {
  103. POWER_SUPPLY_PROP_STATUS,
  104. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  105. POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
  106. POWER_SUPPLY_PROP_CHARGE_NOW,
  107. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  108. POWER_SUPPLY_PROP_CURRENT_NOW,
  109. };
  110. static int calc_full_volt(int volt_val, int cur_val, int impedance)
  111. {
  112. return volt_val + cur_val * impedance / 1000;
  113. }
  114. static int charge_finished(struct s3c_adc_bat *bat)
  115. {
  116. return bat->pdata->gpio_inverted ?
  117. !gpio_get_value(bat->pdata->gpio_charge_finished) :
  118. gpio_get_value(bat->pdata->gpio_charge_finished);
  119. }
  120. static int s3c_adc_bat_get_property(struct power_supply *psy,
  121. enum power_supply_property psp,
  122. union power_supply_propval *val)
  123. {
  124. struct s3c_adc_bat *bat = container_of(psy, struct s3c_adc_bat, psy);
  125. int new_level;
  126. int full_volt;
  127. const struct s3c_adc_bat_thresh *lut = bat->pdata->lut_noac;
  128. unsigned int lut_size = bat->pdata->lut_noac_cnt;
  129. if (!bat) {
  130. dev_err(psy->dev, "no battery infos ?!\n");
  131. return -EINVAL;
  132. }
  133. if (bat->volt_value < 0 || bat->cur_value < 0 ||
  134. jiffies_to_msecs(jiffies - bat->timestamp) >
  135. BAT_POLL_INTERVAL) {
  136. bat->volt_value = gather_samples(bat->client,
  137. bat->pdata->volt_samples,
  138. bat->pdata->volt_channel) * bat->pdata->volt_mult;
  139. bat->cur_value = gather_samples(bat->client,
  140. bat->pdata->current_samples,
  141. bat->pdata->current_channel) * bat->pdata->current_mult;
  142. bat->timestamp = jiffies;
  143. }
  144. if (bat->cable_plugged &&
  145. ((bat->pdata->gpio_charge_finished < 0) ||
  146. !charge_finished(bat))) {
  147. lut = bat->pdata->lut_acin;
  148. lut_size = bat->pdata->lut_acin_cnt;
  149. }
  150. new_level = 100000;
  151. full_volt = calc_full_volt((bat->volt_value / 1000),
  152. (bat->cur_value / 1000), bat->pdata->internal_impedance);
  153. if (full_volt < calc_full_volt(lut->volt, lut->cur,
  154. bat->pdata->internal_impedance)) {
  155. lut_size--;
  156. while (lut_size--) {
  157. int lut_volt1;
  158. int lut_volt2;
  159. lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
  160. bat->pdata->internal_impedance);
  161. lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
  162. bat->pdata->internal_impedance);
  163. if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
  164. new_level = (lut[1].level +
  165. (lut[0].level - lut[1].level) *
  166. (full_volt - lut_volt2) /
  167. (lut_volt1 - lut_volt2)) * 1000;
  168. break;
  169. }
  170. new_level = lut[1].level * 1000;
  171. lut++;
  172. }
  173. }
  174. bat->level = new_level;
  175. switch (psp) {
  176. case POWER_SUPPLY_PROP_STATUS:
  177. if (bat->pdata->gpio_charge_finished < 0)
  178. val->intval = bat->level == 100000 ?
  179. POWER_SUPPLY_STATUS_FULL : bat->status;
  180. else
  181. val->intval = bat->status;
  182. return 0;
  183. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  184. val->intval = 100000;
  185. return 0;
  186. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  187. val->intval = 0;
  188. return 0;
  189. case POWER_SUPPLY_PROP_CHARGE_NOW:
  190. val->intval = bat->level;
  191. return 0;
  192. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  193. val->intval = bat->volt_value;
  194. return 0;
  195. case POWER_SUPPLY_PROP_CURRENT_NOW:
  196. val->intval = bat->cur_value;
  197. return 0;
  198. default:
  199. return -EINVAL;
  200. }
  201. }
  202. static struct s3c_adc_bat main_bat = {
  203. .psy = {
  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. };
  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 __devinit 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. ret = power_supply_register(&pdev->dev, &main_bat.psy);
  272. if (ret)
  273. goto err_reg_main;
  274. if (pdata->backup_volt_mult) {
  275. backup_bat.client = client;
  276. backup_bat.pdata = pdev->dev.platform_data;
  277. backup_bat.volt_value = -1;
  278. ret = power_supply_register(&pdev->dev, &backup_bat.psy);
  279. if (ret)
  280. goto err_reg_backup;
  281. }
  282. INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
  283. if (pdata->gpio_charge_finished >= 0) {
  284. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  285. if (ret)
  286. goto err_gpio;
  287. ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
  288. s3c_adc_bat_charged,
  289. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  290. "battery charged", NULL);
  291. if (ret)
  292. goto err_irq;
  293. }
  294. if (pdata->init) {
  295. ret = pdata->init();
  296. if (ret)
  297. goto err_platform;
  298. }
  299. dev_info(&pdev->dev, "successfully loaded\n");
  300. device_init_wakeup(&pdev->dev, 1);
  301. /* Schedule timer to check current status */
  302. schedule_delayed_work(&bat_work,
  303. msecs_to_jiffies(JITTER_DELAY));
  304. return 0;
  305. err_platform:
  306. if (pdata->gpio_charge_finished >= 0)
  307. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  308. err_irq:
  309. if (pdata->gpio_charge_finished >= 0)
  310. gpio_free(pdata->gpio_charge_finished);
  311. err_gpio:
  312. if (pdata->backup_volt_mult)
  313. power_supply_unregister(&backup_bat.psy);
  314. err_reg_backup:
  315. power_supply_unregister(&main_bat.psy);
  316. err_reg_main:
  317. return ret;
  318. }
  319. static int s3c_adc_bat_remove(struct platform_device *pdev)
  320. {
  321. struct s3c_adc_client *client = platform_get_drvdata(pdev);
  322. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  323. power_supply_unregister(&main_bat.psy);
  324. if (pdata->backup_volt_mult)
  325. power_supply_unregister(&backup_bat.psy);
  326. s3c_adc_release(client);
  327. if (pdata->gpio_charge_finished >= 0) {
  328. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  329. gpio_free(pdata->gpio_charge_finished);
  330. }
  331. cancel_delayed_work(&bat_work);
  332. if (pdata->exit)
  333. pdata->exit();
  334. return 0;
  335. }
  336. #ifdef CONFIG_PM
  337. static int s3c_adc_bat_suspend(struct platform_device *pdev,
  338. pm_message_t state)
  339. {
  340. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  341. if (pdata->gpio_charge_finished >= 0) {
  342. if (device_may_wakeup(&pdev->dev))
  343. enable_irq_wake(
  344. gpio_to_irq(pdata->gpio_charge_finished));
  345. else {
  346. disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  347. main_bat.pdata->disable_charger();
  348. }
  349. }
  350. return 0;
  351. }
  352. static int s3c_adc_bat_resume(struct platform_device *pdev)
  353. {
  354. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  355. if (pdata->gpio_charge_finished >= 0) {
  356. if (device_may_wakeup(&pdev->dev))
  357. disable_irq_wake(
  358. gpio_to_irq(pdata->gpio_charge_finished));
  359. else
  360. enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  361. }
  362. /* Schedule timer to check current status */
  363. schedule_delayed_work(&bat_work,
  364. msecs_to_jiffies(JITTER_DELAY));
  365. return 0;
  366. }
  367. #else
  368. #define s3c_adc_bat_suspend NULL
  369. #define s3c_adc_bat_resume NULL
  370. #endif
  371. static struct platform_driver s3c_adc_bat_driver = {
  372. .driver = {
  373. .name = "s3c-adc-battery",
  374. },
  375. .probe = s3c_adc_bat_probe,
  376. .remove = s3c_adc_bat_remove,
  377. .suspend = s3c_adc_bat_suspend,
  378. .resume = s3c_adc_bat_resume,
  379. };
  380. module_platform_driver(s3c_adc_bat_driver);
  381. MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
  382. MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
  383. MODULE_LICENSE("GPL");