s3c_adc_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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;
  128. unsigned int lut_size;
  129. if (!bat) {
  130. dev_err(psy->dev, "no battery infos ?!\n");
  131. return -EINVAL;
  132. }
  133. lut = bat->pdata->lut_noac;
  134. lut_size = bat->pdata->lut_noac_cnt;
  135. if (bat->volt_value < 0 || bat->cur_value < 0 ||
  136. jiffies_to_msecs(jiffies - bat->timestamp) >
  137. BAT_POLL_INTERVAL) {
  138. bat->volt_value = gather_samples(bat->client,
  139. bat->pdata->volt_samples,
  140. bat->pdata->volt_channel) * bat->pdata->volt_mult;
  141. bat->cur_value = gather_samples(bat->client,
  142. bat->pdata->current_samples,
  143. bat->pdata->current_channel) * bat->pdata->current_mult;
  144. bat->timestamp = jiffies;
  145. }
  146. if (bat->cable_plugged &&
  147. ((bat->pdata->gpio_charge_finished < 0) ||
  148. !charge_finished(bat))) {
  149. lut = bat->pdata->lut_acin;
  150. lut_size = bat->pdata->lut_acin_cnt;
  151. }
  152. new_level = 100000;
  153. full_volt = calc_full_volt((bat->volt_value / 1000),
  154. (bat->cur_value / 1000), bat->pdata->internal_impedance);
  155. if (full_volt < calc_full_volt(lut->volt, lut->cur,
  156. bat->pdata->internal_impedance)) {
  157. lut_size--;
  158. while (lut_size--) {
  159. int lut_volt1;
  160. int lut_volt2;
  161. lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
  162. bat->pdata->internal_impedance);
  163. lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
  164. bat->pdata->internal_impedance);
  165. if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
  166. new_level = (lut[1].level +
  167. (lut[0].level - lut[1].level) *
  168. (full_volt - lut_volt2) /
  169. (lut_volt1 - lut_volt2)) * 1000;
  170. break;
  171. }
  172. new_level = lut[1].level * 1000;
  173. lut++;
  174. }
  175. }
  176. bat->level = new_level;
  177. switch (psp) {
  178. case POWER_SUPPLY_PROP_STATUS:
  179. if (bat->pdata->gpio_charge_finished < 0)
  180. val->intval = bat->level == 100000 ?
  181. POWER_SUPPLY_STATUS_FULL : bat->status;
  182. else
  183. val->intval = bat->status;
  184. return 0;
  185. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  186. val->intval = 100000;
  187. return 0;
  188. case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
  189. val->intval = 0;
  190. return 0;
  191. case POWER_SUPPLY_PROP_CHARGE_NOW:
  192. val->intval = bat->level;
  193. return 0;
  194. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  195. val->intval = bat->volt_value;
  196. return 0;
  197. case POWER_SUPPLY_PROP_CURRENT_NOW:
  198. val->intval = bat->cur_value;
  199. return 0;
  200. default:
  201. return -EINVAL;
  202. }
  203. }
  204. static struct s3c_adc_bat main_bat = {
  205. .psy = {
  206. .name = "main-battery",
  207. .type = POWER_SUPPLY_TYPE_BATTERY,
  208. .properties = s3c_adc_main_bat_props,
  209. .num_properties = ARRAY_SIZE(s3c_adc_main_bat_props),
  210. .get_property = s3c_adc_bat_get_property,
  211. .external_power_changed = s3c_adc_bat_ext_power_changed,
  212. .use_for_apm = 1,
  213. },
  214. };
  215. static void s3c_adc_bat_work(struct work_struct *work)
  216. {
  217. struct s3c_adc_bat *bat = &main_bat;
  218. int is_charged;
  219. int is_plugged;
  220. static int was_plugged;
  221. is_plugged = power_supply_am_i_supplied(&bat->psy);
  222. bat->cable_plugged = is_plugged;
  223. if (is_plugged != was_plugged) {
  224. was_plugged = is_plugged;
  225. if (is_plugged) {
  226. if (bat->pdata->enable_charger)
  227. bat->pdata->enable_charger();
  228. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  229. } else {
  230. if (bat->pdata->disable_charger)
  231. bat->pdata->disable_charger();
  232. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  233. }
  234. } else {
  235. if ((bat->pdata->gpio_charge_finished >= 0) && is_plugged) {
  236. is_charged = charge_finished(&main_bat);
  237. if (is_charged) {
  238. if (bat->pdata->disable_charger)
  239. bat->pdata->disable_charger();
  240. bat->status = POWER_SUPPLY_STATUS_FULL;
  241. } else {
  242. if (bat->pdata->enable_charger)
  243. bat->pdata->enable_charger();
  244. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  245. }
  246. }
  247. }
  248. power_supply_changed(&bat->psy);
  249. }
  250. static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
  251. {
  252. schedule_delayed_work(&bat_work,
  253. msecs_to_jiffies(JITTER_DELAY));
  254. return IRQ_HANDLED;
  255. }
  256. static int s3c_adc_bat_probe(struct platform_device *pdev)
  257. {
  258. struct s3c_adc_client *client;
  259. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  260. int ret;
  261. client = s3c_adc_register(pdev, NULL, NULL, 0);
  262. if (IS_ERR(client)) {
  263. dev_err(&pdev->dev, "cannot register adc\n");
  264. return PTR_ERR(client);
  265. }
  266. platform_set_drvdata(pdev, client);
  267. main_bat.client = client;
  268. main_bat.pdata = pdata;
  269. main_bat.volt_value = -1;
  270. main_bat.cur_value = -1;
  271. main_bat.cable_plugged = 0;
  272. main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
  273. ret = power_supply_register(&pdev->dev, &main_bat.psy);
  274. if (ret)
  275. goto err_reg_main;
  276. if (pdata->backup_volt_mult) {
  277. backup_bat.client = client;
  278. backup_bat.pdata = pdev->dev.platform_data;
  279. backup_bat.volt_value = -1;
  280. ret = power_supply_register(&pdev->dev, &backup_bat.psy);
  281. if (ret)
  282. goto err_reg_backup;
  283. }
  284. INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
  285. if (pdata->gpio_charge_finished >= 0) {
  286. ret = gpio_request(pdata->gpio_charge_finished, "charged");
  287. if (ret)
  288. goto err_gpio;
  289. ret = request_irq(gpio_to_irq(pdata->gpio_charge_finished),
  290. s3c_adc_bat_charged,
  291. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  292. "battery charged", NULL);
  293. if (ret)
  294. goto err_irq;
  295. }
  296. if (pdata->init) {
  297. ret = pdata->init();
  298. if (ret)
  299. goto err_platform;
  300. }
  301. dev_info(&pdev->dev, "successfully loaded\n");
  302. device_init_wakeup(&pdev->dev, 1);
  303. /* Schedule timer to check current status */
  304. schedule_delayed_work(&bat_work,
  305. msecs_to_jiffies(JITTER_DELAY));
  306. return 0;
  307. err_platform:
  308. if (pdata->gpio_charge_finished >= 0)
  309. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  310. err_irq:
  311. if (pdata->gpio_charge_finished >= 0)
  312. gpio_free(pdata->gpio_charge_finished);
  313. err_gpio:
  314. if (pdata->backup_volt_mult)
  315. power_supply_unregister(&backup_bat.psy);
  316. err_reg_backup:
  317. power_supply_unregister(&main_bat.psy);
  318. err_reg_main:
  319. return ret;
  320. }
  321. static int s3c_adc_bat_remove(struct platform_device *pdev)
  322. {
  323. struct s3c_adc_client *client = platform_get_drvdata(pdev);
  324. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  325. power_supply_unregister(&main_bat.psy);
  326. if (pdata->backup_volt_mult)
  327. power_supply_unregister(&backup_bat.psy);
  328. s3c_adc_release(client);
  329. if (pdata->gpio_charge_finished >= 0) {
  330. free_irq(gpio_to_irq(pdata->gpio_charge_finished), NULL);
  331. gpio_free(pdata->gpio_charge_finished);
  332. }
  333. cancel_delayed_work(&bat_work);
  334. if (pdata->exit)
  335. pdata->exit();
  336. return 0;
  337. }
  338. #ifdef CONFIG_PM
  339. static int s3c_adc_bat_suspend(struct platform_device *pdev,
  340. pm_message_t state)
  341. {
  342. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  343. if (pdata->gpio_charge_finished >= 0) {
  344. if (device_may_wakeup(&pdev->dev))
  345. enable_irq_wake(
  346. gpio_to_irq(pdata->gpio_charge_finished));
  347. else {
  348. disable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  349. main_bat.pdata->disable_charger();
  350. }
  351. }
  352. return 0;
  353. }
  354. static int s3c_adc_bat_resume(struct platform_device *pdev)
  355. {
  356. struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
  357. if (pdata->gpio_charge_finished >= 0) {
  358. if (device_may_wakeup(&pdev->dev))
  359. disable_irq_wake(
  360. gpio_to_irq(pdata->gpio_charge_finished));
  361. else
  362. enable_irq(gpio_to_irq(pdata->gpio_charge_finished));
  363. }
  364. /* Schedule timer to check current status */
  365. schedule_delayed_work(&bat_work,
  366. msecs_to_jiffies(JITTER_DELAY));
  367. return 0;
  368. }
  369. #else
  370. #define s3c_adc_bat_suspend NULL
  371. #define s3c_adc_bat_resume NULL
  372. #endif
  373. static struct platform_driver s3c_adc_bat_driver = {
  374. .driver = {
  375. .name = "s3c-adc-battery",
  376. },
  377. .probe = s3c_adc_bat_probe,
  378. .remove = s3c_adc_bat_remove,
  379. .suspend = s3c_adc_bat_suspend,
  380. .resume = s3c_adc_bat_resume,
  381. };
  382. module_platform_driver(s3c_adc_bat_driver);
  383. MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
  384. MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
  385. MODULE_LICENSE("GPL");