tosa_battery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Battery and Power Management code for the Sharp SL-6000x
  3. *
  4. * Copyright (c) 2005 Dirk Opfer
  5. * Copyright (c) 2008 Dmitry Baryshkov
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/power_supply.h>
  15. #include <linux/wm97xx.h>
  16. #include <linux/delay.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/gpio.h>
  20. #include <asm/mach-types.h>
  21. #include <mach/tosa.h>
  22. static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
  23. static struct work_struct bat_work;
  24. struct tosa_bat {
  25. int status;
  26. struct power_supply psy;
  27. int full_chrg;
  28. struct mutex work_lock; /* protects data */
  29. bool (*is_present)(struct tosa_bat *bat);
  30. int gpio_full;
  31. int gpio_charge_off;
  32. int technology;
  33. int gpio_bat;
  34. int adc_bat;
  35. int adc_bat_divider;
  36. int bat_max;
  37. int bat_min;
  38. int gpio_temp;
  39. int adc_temp;
  40. int adc_temp_divider;
  41. };
  42. static struct tosa_bat tosa_bat_main;
  43. static struct tosa_bat tosa_bat_jacket;
  44. static unsigned long tosa_read_bat(struct tosa_bat *bat)
  45. {
  46. unsigned long value = 0;
  47. if (bat->gpio_bat < 0 || bat->adc_bat < 0)
  48. return 0;
  49. mutex_lock(&bat_lock);
  50. gpio_set_value(bat->gpio_bat, 1);
  51. msleep(5);
  52. value = wm97xx_read_aux_adc(dev_get_drvdata(bat->psy.dev->parent),
  53. bat->adc_bat);
  54. gpio_set_value(bat->gpio_bat, 0);
  55. mutex_unlock(&bat_lock);
  56. value = value * 1000000 / bat->adc_bat_divider;
  57. return value;
  58. }
  59. static unsigned long tosa_read_temp(struct tosa_bat *bat)
  60. {
  61. unsigned long value = 0;
  62. if (bat->gpio_temp < 0 || bat->adc_temp < 0)
  63. return 0;
  64. mutex_lock(&bat_lock);
  65. gpio_set_value(bat->gpio_temp, 1);
  66. msleep(5);
  67. value = wm97xx_read_aux_adc(dev_get_drvdata(bat->psy.dev->parent),
  68. bat->adc_temp);
  69. gpio_set_value(bat->gpio_temp, 0);
  70. mutex_unlock(&bat_lock);
  71. value = value * 10000 / bat->adc_temp_divider;
  72. return value;
  73. }
  74. static int tosa_bat_get_property(struct power_supply *psy,
  75. enum power_supply_property psp,
  76. union power_supply_propval *val)
  77. {
  78. int ret = 0;
  79. struct tosa_bat *bat = container_of(psy, struct tosa_bat, psy);
  80. if (bat->is_present && !bat->is_present(bat)
  81. && psp != POWER_SUPPLY_PROP_PRESENT) {
  82. return -ENODEV;
  83. }
  84. switch (psp) {
  85. case POWER_SUPPLY_PROP_STATUS:
  86. val->intval = bat->status;
  87. break;
  88. case POWER_SUPPLY_PROP_TECHNOLOGY:
  89. val->intval = bat->technology;
  90. break;
  91. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  92. val->intval = tosa_read_bat(bat);
  93. break;
  94. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  95. if (bat->full_chrg == -1)
  96. val->intval = bat->bat_max;
  97. else
  98. val->intval = bat->full_chrg;
  99. break;
  100. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  101. val->intval = bat->bat_max;
  102. break;
  103. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  104. val->intval = bat->bat_min;
  105. break;
  106. case POWER_SUPPLY_PROP_TEMP:
  107. val->intval = tosa_read_temp(bat);
  108. break;
  109. case POWER_SUPPLY_PROP_PRESENT:
  110. val->intval = bat->is_present ? bat->is_present(bat) : 1;
  111. break;
  112. default:
  113. ret = -EINVAL;
  114. break;
  115. }
  116. return ret;
  117. }
  118. static bool tosa_jacket_bat_is_present(struct tosa_bat *bat)
  119. {
  120. return gpio_get_value(TOSA_GPIO_JACKET_DETECT) == 0;
  121. }
  122. static void tosa_bat_external_power_changed(struct power_supply *psy)
  123. {
  124. schedule_work(&bat_work);
  125. }
  126. static irqreturn_t tosa_bat_gpio_isr(int irq, void *data)
  127. {
  128. pr_info("tosa_bat_gpio irq: %d\n", gpio_get_value(irq_to_gpio(irq)));
  129. schedule_work(&bat_work);
  130. return IRQ_HANDLED;
  131. }
  132. static void tosa_bat_update(struct tosa_bat *bat)
  133. {
  134. int old;
  135. struct power_supply *psy = &bat->psy;
  136. mutex_lock(&bat->work_lock);
  137. old = bat->status;
  138. if (bat->is_present && !bat->is_present(bat)) {
  139. printk(KERN_NOTICE "%s not present\n", psy->name);
  140. bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  141. bat->full_chrg = -1;
  142. } else if (power_supply_am_i_supplied(psy)) {
  143. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
  144. gpio_set_value(bat->gpio_charge_off, 0);
  145. mdelay(15);
  146. }
  147. if (gpio_get_value(bat->gpio_full)) {
  148. if (old == POWER_SUPPLY_STATUS_CHARGING ||
  149. bat->full_chrg == -1)
  150. bat->full_chrg = tosa_read_bat(bat);
  151. gpio_set_value(bat->gpio_charge_off, 1);
  152. bat->status = POWER_SUPPLY_STATUS_FULL;
  153. } else {
  154. gpio_set_value(bat->gpio_charge_off, 0);
  155. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  156. }
  157. } else {
  158. gpio_set_value(bat->gpio_charge_off, 1);
  159. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  160. }
  161. if (old != bat->status)
  162. power_supply_changed(psy);
  163. mutex_unlock(&bat->work_lock);
  164. }
  165. static void tosa_bat_work(struct work_struct *work)
  166. {
  167. tosa_bat_update(&tosa_bat_main);
  168. tosa_bat_update(&tosa_bat_jacket);
  169. }
  170. static enum power_supply_property tosa_bat_main_props[] = {
  171. POWER_SUPPLY_PROP_STATUS,
  172. POWER_SUPPLY_PROP_TECHNOLOGY,
  173. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  174. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  175. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  176. POWER_SUPPLY_PROP_TEMP,
  177. POWER_SUPPLY_PROP_PRESENT,
  178. };
  179. static enum power_supply_property tosa_bat_bu_props[] = {
  180. POWER_SUPPLY_PROP_STATUS,
  181. POWER_SUPPLY_PROP_TECHNOLOGY,
  182. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  183. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  184. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  185. POWER_SUPPLY_PROP_PRESENT,
  186. };
  187. static struct tosa_bat tosa_bat_main = {
  188. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  189. .full_chrg = -1,
  190. .psy = {
  191. .name = "main-battery",
  192. .type = POWER_SUPPLY_TYPE_BATTERY,
  193. .properties = tosa_bat_main_props,
  194. .num_properties = ARRAY_SIZE(tosa_bat_main_props),
  195. .get_property = tosa_bat_get_property,
  196. .external_power_changed = tosa_bat_external_power_changed,
  197. .use_for_apm = 1,
  198. },
  199. .gpio_full = TOSA_GPIO_BAT0_CRG,
  200. .gpio_charge_off = TOSA_GPIO_CHARGE_OFF,
  201. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  202. .gpio_bat = TOSA_GPIO_BAT0_V_ON,
  203. .adc_bat = WM97XX_AUX_ID3,
  204. .adc_bat_divider = 414,
  205. .bat_max = 4310000,
  206. .bat_min = 1551 * 1000000 / 414,
  207. .gpio_temp = TOSA_GPIO_BAT1_TH_ON,
  208. .adc_temp = WM97XX_AUX_ID2,
  209. .adc_temp_divider = 10000,
  210. };
  211. static struct tosa_bat tosa_bat_jacket = {
  212. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  213. .full_chrg = -1,
  214. .psy = {
  215. .name = "jacket-battery",
  216. .type = POWER_SUPPLY_TYPE_BATTERY,
  217. .properties = tosa_bat_main_props,
  218. .num_properties = ARRAY_SIZE(tosa_bat_main_props),
  219. .get_property = tosa_bat_get_property,
  220. .external_power_changed = tosa_bat_external_power_changed,
  221. },
  222. .is_present = tosa_jacket_bat_is_present,
  223. .gpio_full = TOSA_GPIO_BAT1_CRG,
  224. .gpio_charge_off = TOSA_GPIO_CHARGE_OFF_JC,
  225. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  226. .gpio_bat = TOSA_GPIO_BAT1_V_ON,
  227. .adc_bat = WM97XX_AUX_ID3,
  228. .adc_bat_divider = 414,
  229. .bat_max = 4310000,
  230. .bat_min = 1551 * 1000000 / 414,
  231. .gpio_temp = TOSA_GPIO_BAT0_TH_ON,
  232. .adc_temp = WM97XX_AUX_ID2,
  233. .adc_temp_divider = 10000,
  234. };
  235. static struct tosa_bat tosa_bat_bu = {
  236. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  237. .full_chrg = -1,
  238. .psy = {
  239. .name = "backup-battery",
  240. .type = POWER_SUPPLY_TYPE_BATTERY,
  241. .properties = tosa_bat_bu_props,
  242. .num_properties = ARRAY_SIZE(tosa_bat_bu_props),
  243. .get_property = tosa_bat_get_property,
  244. .external_power_changed = tosa_bat_external_power_changed,
  245. },
  246. .gpio_full = -1,
  247. .gpio_charge_off = -1,
  248. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  249. .gpio_bat = TOSA_GPIO_BU_CHRG_ON,
  250. .adc_bat = WM97XX_AUX_ID4,
  251. .adc_bat_divider = 1266,
  252. .gpio_temp = -1,
  253. .adc_temp = -1,
  254. .adc_temp_divider = -1,
  255. };
  256. static struct {
  257. int gpio;
  258. char *name;
  259. bool output;
  260. int value;
  261. } gpios[] = {
  262. { TOSA_GPIO_CHARGE_OFF, "main charge off", 1, 1 },
  263. { TOSA_GPIO_CHARGE_OFF_JC, "jacket charge off", 1, 1 },
  264. { TOSA_GPIO_BAT_SW_ON, "battery switch", 1, 0 },
  265. { TOSA_GPIO_BAT0_V_ON, "main battery", 1, 0 },
  266. { TOSA_GPIO_BAT1_V_ON, "jacket battery", 1, 0 },
  267. { TOSA_GPIO_BAT1_TH_ON, "main battery temp", 1, 0 },
  268. { TOSA_GPIO_BAT0_TH_ON, "jacket battery temp", 1, 0 },
  269. { TOSA_GPIO_BU_CHRG_ON, "backup battery", 1, 0 },
  270. { TOSA_GPIO_BAT0_CRG, "main battery full", 0, 0 },
  271. { TOSA_GPIO_BAT1_CRG, "jacket battery full", 0, 0 },
  272. { TOSA_GPIO_BAT0_LOW, "main battery low", 0, 0 },
  273. { TOSA_GPIO_BAT1_LOW, "jacket battery low", 0, 0 },
  274. { TOSA_GPIO_JACKET_DETECT, "jacket detect", 0, 0 },
  275. };
  276. #ifdef CONFIG_PM
  277. static int tosa_bat_suspend(struct platform_device *dev, pm_message_t state)
  278. {
  279. /* flush all pending status updates */
  280. flush_work_sync(&bat_work);
  281. return 0;
  282. }
  283. static int tosa_bat_resume(struct platform_device *dev)
  284. {
  285. /* things may have changed while we were away */
  286. schedule_work(&bat_work);
  287. return 0;
  288. }
  289. #else
  290. #define tosa_bat_suspend NULL
  291. #define tosa_bat_resume NULL
  292. #endif
  293. static int __devinit tosa_bat_probe(struct platform_device *dev)
  294. {
  295. int ret;
  296. int i;
  297. if (!machine_is_tosa())
  298. return -ENODEV;
  299. for (i = 0; i < ARRAY_SIZE(gpios); i++) {
  300. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  301. if (ret) {
  302. i--;
  303. goto err_gpio;
  304. }
  305. if (gpios[i].output)
  306. ret = gpio_direction_output(gpios[i].gpio,
  307. gpios[i].value);
  308. else
  309. ret = gpio_direction_input(gpios[i].gpio);
  310. if (ret)
  311. goto err_gpio;
  312. }
  313. mutex_init(&tosa_bat_main.work_lock);
  314. mutex_init(&tosa_bat_jacket.work_lock);
  315. INIT_WORK(&bat_work, tosa_bat_work);
  316. ret = power_supply_register(&dev->dev, &tosa_bat_main.psy);
  317. if (ret)
  318. goto err_psy_reg_main;
  319. ret = power_supply_register(&dev->dev, &tosa_bat_jacket.psy);
  320. if (ret)
  321. goto err_psy_reg_jacket;
  322. ret = power_supply_register(&dev->dev, &tosa_bat_bu.psy);
  323. if (ret)
  324. goto err_psy_reg_bu;
  325. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG),
  326. tosa_bat_gpio_isr,
  327. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  328. "main full", &tosa_bat_main);
  329. if (ret)
  330. goto err_req_main;
  331. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG),
  332. tosa_bat_gpio_isr,
  333. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  334. "jacket full", &tosa_bat_jacket);
  335. if (ret)
  336. goto err_req_jacket;
  337. ret = request_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT),
  338. tosa_bat_gpio_isr,
  339. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  340. "jacket detect", &tosa_bat_jacket);
  341. if (!ret) {
  342. schedule_work(&bat_work);
  343. return 0;
  344. }
  345. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  346. err_req_jacket:
  347. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  348. err_req_main:
  349. power_supply_unregister(&tosa_bat_bu.psy);
  350. err_psy_reg_bu:
  351. power_supply_unregister(&tosa_bat_jacket.psy);
  352. err_psy_reg_jacket:
  353. power_supply_unregister(&tosa_bat_main.psy);
  354. err_psy_reg_main:
  355. /* see comment in tosa_bat_remove */
  356. cancel_work_sync(&bat_work);
  357. i--;
  358. err_gpio:
  359. for (; i >= 0; i--)
  360. gpio_free(gpios[i].gpio);
  361. return ret;
  362. }
  363. static int __devexit tosa_bat_remove(struct platform_device *dev)
  364. {
  365. int i;
  366. free_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT), &tosa_bat_jacket);
  367. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  368. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  369. power_supply_unregister(&tosa_bat_bu.psy);
  370. power_supply_unregister(&tosa_bat_jacket.psy);
  371. power_supply_unregister(&tosa_bat_main.psy);
  372. /*
  373. * Now cancel the bat_work. We won't get any more schedules,
  374. * since all sources (isr and external_power_changed) are
  375. * unregistered now.
  376. */
  377. cancel_work_sync(&bat_work);
  378. for (i = ARRAY_SIZE(gpios) - 1; i >= 0; i--)
  379. gpio_free(gpios[i].gpio);
  380. return 0;
  381. }
  382. static struct platform_driver tosa_bat_driver = {
  383. .driver.name = "wm97xx-battery",
  384. .driver.owner = THIS_MODULE,
  385. .probe = tosa_bat_probe,
  386. .remove = __devexit_p(tosa_bat_remove),
  387. .suspend = tosa_bat_suspend,
  388. .resume = tosa_bat_resume,
  389. };
  390. static int __init tosa_bat_init(void)
  391. {
  392. return platform_driver_register(&tosa_bat_driver);
  393. }
  394. static void __exit tosa_bat_exit(void)
  395. {
  396. platform_driver_unregister(&tosa_bat_driver);
  397. }
  398. module_init(tosa_bat_init);
  399. module_exit(tosa_bat_exit);
  400. MODULE_LICENSE("GPL");
  401. MODULE_AUTHOR("Dmitry Baryshkov");
  402. MODULE_DESCRIPTION("Tosa battery driver");
  403. MODULE_ALIAS("platform:wm97xx-battery");