tosa_battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 gpio tosa_bat_gpios[] = {
  257. { TOSA_GPIO_CHARGE_OFF, GPIOF_OUT_INIT_HIGH, "main charge off" },
  258. { TOSA_GPIO_CHARGE_OFF_JC, GPIOF_OUT_INIT_HIGH, "jacket charge off" },
  259. { TOSA_GPIO_BAT_SW_ON, GPIOF_OUT_INIT_LOW, "battery switch" },
  260. { TOSA_GPIO_BAT0_V_ON, GPIOF_OUT_INIT_LOW, "main battery" },
  261. { TOSA_GPIO_BAT1_V_ON, GPIOF_OUT_INIT_LOW, "jacket battery" },
  262. { TOSA_GPIO_BAT1_TH_ON, GPIOF_OUT_INIT_LOW, "main battery temp" },
  263. { TOSA_GPIO_BAT0_TH_ON, GPIOF_OUT_INIT_LOW, "jacket battery temp" },
  264. { TOSA_GPIO_BU_CHRG_ON, GPIOF_OUT_INIT_LOW, "backup battery" },
  265. { TOSA_GPIO_BAT0_CRG, GPIOF_IN, "main battery full" },
  266. { TOSA_GPIO_BAT1_CRG, GPIOF_IN, "jacket battery full" },
  267. { TOSA_GPIO_BAT0_LOW, GPIOF_IN, "main battery low" },
  268. { TOSA_GPIO_BAT1_LOW, GPIOF_IN, "jacket battery low" },
  269. { TOSA_GPIO_JACKET_DETECT, GPIOF_IN, "jacket detect" },
  270. };
  271. #ifdef CONFIG_PM
  272. static int tosa_bat_suspend(struct platform_device *dev, pm_message_t state)
  273. {
  274. /* flush all pending status updates */
  275. flush_work_sync(&bat_work);
  276. return 0;
  277. }
  278. static int tosa_bat_resume(struct platform_device *dev)
  279. {
  280. /* things may have changed while we were away */
  281. schedule_work(&bat_work);
  282. return 0;
  283. }
  284. #else
  285. #define tosa_bat_suspend NULL
  286. #define tosa_bat_resume NULL
  287. #endif
  288. static int __devinit tosa_bat_probe(struct platform_device *dev)
  289. {
  290. int ret;
  291. if (!machine_is_tosa())
  292. return -ENODEV;
  293. ret = gpio_request_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  294. if (ret)
  295. return ret;
  296. mutex_init(&tosa_bat_main.work_lock);
  297. mutex_init(&tosa_bat_jacket.work_lock);
  298. INIT_WORK(&bat_work, tosa_bat_work);
  299. ret = power_supply_register(&dev->dev, &tosa_bat_main.psy);
  300. if (ret)
  301. goto err_psy_reg_main;
  302. ret = power_supply_register(&dev->dev, &tosa_bat_jacket.psy);
  303. if (ret)
  304. goto err_psy_reg_jacket;
  305. ret = power_supply_register(&dev->dev, &tosa_bat_bu.psy);
  306. if (ret)
  307. goto err_psy_reg_bu;
  308. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG),
  309. tosa_bat_gpio_isr,
  310. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  311. "main full", &tosa_bat_main);
  312. if (ret)
  313. goto err_req_main;
  314. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG),
  315. tosa_bat_gpio_isr,
  316. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  317. "jacket full", &tosa_bat_jacket);
  318. if (ret)
  319. goto err_req_jacket;
  320. ret = request_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT),
  321. tosa_bat_gpio_isr,
  322. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  323. "jacket detect", &tosa_bat_jacket);
  324. if (!ret) {
  325. schedule_work(&bat_work);
  326. return 0;
  327. }
  328. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  329. err_req_jacket:
  330. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  331. err_req_main:
  332. power_supply_unregister(&tosa_bat_bu.psy);
  333. err_psy_reg_bu:
  334. power_supply_unregister(&tosa_bat_jacket.psy);
  335. err_psy_reg_jacket:
  336. power_supply_unregister(&tosa_bat_main.psy);
  337. err_psy_reg_main:
  338. /* see comment in tosa_bat_remove */
  339. cancel_work_sync(&bat_work);
  340. gpio_free_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  341. return ret;
  342. }
  343. static int __devexit tosa_bat_remove(struct platform_device *dev)
  344. {
  345. free_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT), &tosa_bat_jacket);
  346. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  347. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  348. power_supply_unregister(&tosa_bat_bu.psy);
  349. power_supply_unregister(&tosa_bat_jacket.psy);
  350. power_supply_unregister(&tosa_bat_main.psy);
  351. /*
  352. * Now cancel the bat_work. We won't get any more schedules,
  353. * since all sources (isr and external_power_changed) are
  354. * unregistered now.
  355. */
  356. cancel_work_sync(&bat_work);
  357. gpio_free_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  358. return 0;
  359. }
  360. static struct platform_driver tosa_bat_driver = {
  361. .driver.name = "wm97xx-battery",
  362. .driver.owner = THIS_MODULE,
  363. .probe = tosa_bat_probe,
  364. .remove = __devexit_p(tosa_bat_remove),
  365. .suspend = tosa_bat_suspend,
  366. .resume = tosa_bat_resume,
  367. };
  368. module_platform_driver(tosa_bat_driver);
  369. MODULE_LICENSE("GPL");
  370. MODULE_AUTHOR("Dmitry Baryshkov");
  371. MODULE_DESCRIPTION("Tosa battery driver");
  372. MODULE_ALIAS("platform:wm97xx-battery");