tosa_battery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 = power_supply_get_drvdata(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\n");
  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->desc->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 const struct power_supply_desc tosa_bat_main_desc = {
  188. .name = "main-battery",
  189. .type = POWER_SUPPLY_TYPE_BATTERY,
  190. .properties = tosa_bat_main_props,
  191. .num_properties = ARRAY_SIZE(tosa_bat_main_props),
  192. .get_property = tosa_bat_get_property,
  193. .external_power_changed = tosa_bat_external_power_changed,
  194. .use_for_apm = 1,
  195. };
  196. static const struct power_supply_desc tosa_bat_jacket_desc = {
  197. .name = "jacket-battery",
  198. .type = POWER_SUPPLY_TYPE_BATTERY,
  199. .properties = tosa_bat_main_props,
  200. .num_properties = ARRAY_SIZE(tosa_bat_main_props),
  201. .get_property = tosa_bat_get_property,
  202. .external_power_changed = tosa_bat_external_power_changed,
  203. };
  204. static const struct power_supply_desc tosa_bat_bu_desc = {
  205. .name = "backup-battery",
  206. .type = POWER_SUPPLY_TYPE_BATTERY,
  207. .properties = tosa_bat_bu_props,
  208. .num_properties = ARRAY_SIZE(tosa_bat_bu_props),
  209. .get_property = tosa_bat_get_property,
  210. .external_power_changed = tosa_bat_external_power_changed,
  211. };
  212. static struct tosa_bat tosa_bat_main = {
  213. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  214. .full_chrg = -1,
  215. .psy = NULL,
  216. .gpio_full = TOSA_GPIO_BAT0_CRG,
  217. .gpio_charge_off = TOSA_GPIO_CHARGE_OFF,
  218. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  219. .gpio_bat = TOSA_GPIO_BAT0_V_ON,
  220. .adc_bat = WM97XX_AUX_ID3,
  221. .adc_bat_divider = 414,
  222. .bat_max = 4310000,
  223. .bat_min = 1551 * 1000000 / 414,
  224. .gpio_temp = TOSA_GPIO_BAT1_TH_ON,
  225. .adc_temp = WM97XX_AUX_ID2,
  226. .adc_temp_divider = 10000,
  227. };
  228. static struct tosa_bat tosa_bat_jacket = {
  229. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  230. .full_chrg = -1,
  231. .psy = NULL,
  232. .is_present = tosa_jacket_bat_is_present,
  233. .gpio_full = TOSA_GPIO_BAT1_CRG,
  234. .gpio_charge_off = TOSA_GPIO_CHARGE_OFF_JC,
  235. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  236. .gpio_bat = TOSA_GPIO_BAT1_V_ON,
  237. .adc_bat = WM97XX_AUX_ID3,
  238. .adc_bat_divider = 414,
  239. .bat_max = 4310000,
  240. .bat_min = 1551 * 1000000 / 414,
  241. .gpio_temp = TOSA_GPIO_BAT0_TH_ON,
  242. .adc_temp = WM97XX_AUX_ID2,
  243. .adc_temp_divider = 10000,
  244. };
  245. static struct tosa_bat tosa_bat_bu = {
  246. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  247. .full_chrg = -1,
  248. .psy = NULL,
  249. .gpio_full = -1,
  250. .gpio_charge_off = -1,
  251. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  252. .gpio_bat = TOSA_GPIO_BU_CHRG_ON,
  253. .adc_bat = WM97XX_AUX_ID4,
  254. .adc_bat_divider = 1266,
  255. .gpio_temp = -1,
  256. .adc_temp = -1,
  257. .adc_temp_divider = -1,
  258. };
  259. static struct gpio tosa_bat_gpios[] = {
  260. { TOSA_GPIO_CHARGE_OFF, GPIOF_OUT_INIT_HIGH, "main charge off" },
  261. { TOSA_GPIO_CHARGE_OFF_JC, GPIOF_OUT_INIT_HIGH, "jacket charge off" },
  262. { TOSA_GPIO_BAT_SW_ON, GPIOF_OUT_INIT_LOW, "battery switch" },
  263. { TOSA_GPIO_BAT0_V_ON, GPIOF_OUT_INIT_LOW, "main battery" },
  264. { TOSA_GPIO_BAT1_V_ON, GPIOF_OUT_INIT_LOW, "jacket battery" },
  265. { TOSA_GPIO_BAT1_TH_ON, GPIOF_OUT_INIT_LOW, "main battery temp" },
  266. { TOSA_GPIO_BAT0_TH_ON, GPIOF_OUT_INIT_LOW, "jacket battery temp" },
  267. { TOSA_GPIO_BU_CHRG_ON, GPIOF_OUT_INIT_LOW, "backup battery" },
  268. { TOSA_GPIO_BAT0_CRG, GPIOF_IN, "main battery full" },
  269. { TOSA_GPIO_BAT1_CRG, GPIOF_IN, "jacket battery full" },
  270. { TOSA_GPIO_BAT0_LOW, GPIOF_IN, "main battery low" },
  271. { TOSA_GPIO_BAT1_LOW, GPIOF_IN, "jacket battery low" },
  272. { TOSA_GPIO_JACKET_DETECT, GPIOF_IN, "jacket detect" },
  273. };
  274. #ifdef CONFIG_PM
  275. static int tosa_bat_suspend(struct platform_device *dev, pm_message_t state)
  276. {
  277. /* flush all pending status updates */
  278. flush_work(&bat_work);
  279. return 0;
  280. }
  281. static int tosa_bat_resume(struct platform_device *dev)
  282. {
  283. /* things may have changed while we were away */
  284. schedule_work(&bat_work);
  285. return 0;
  286. }
  287. #else
  288. #define tosa_bat_suspend NULL
  289. #define tosa_bat_resume NULL
  290. #endif
  291. static int tosa_bat_probe(struct platform_device *dev)
  292. {
  293. int ret;
  294. struct power_supply_config main_psy_cfg = {},
  295. jacket_psy_cfg = {},
  296. bu_psy_cfg = {};
  297. if (!machine_is_tosa())
  298. return -ENODEV;
  299. ret = gpio_request_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  300. if (ret)
  301. return ret;
  302. mutex_init(&tosa_bat_main.work_lock);
  303. mutex_init(&tosa_bat_jacket.work_lock);
  304. INIT_WORK(&bat_work, tosa_bat_work);
  305. main_psy_cfg.drv_data = &tosa_bat_main;
  306. tosa_bat_main.psy = power_supply_register(&dev->dev,
  307. &tosa_bat_main_desc,
  308. &main_psy_cfg);
  309. if (IS_ERR(tosa_bat_main.psy)) {
  310. ret = PTR_ERR(tosa_bat_main.psy);
  311. goto err_psy_reg_main;
  312. }
  313. jacket_psy_cfg.drv_data = &tosa_bat_jacket;
  314. tosa_bat_jacket.psy = power_supply_register(&dev->dev,
  315. &tosa_bat_jacket_desc,
  316. &jacket_psy_cfg);
  317. if (IS_ERR(tosa_bat_jacket.psy)) {
  318. ret = PTR_ERR(tosa_bat_jacket.psy);
  319. goto err_psy_reg_jacket;
  320. }
  321. bu_psy_cfg.drv_data = &tosa_bat_bu;
  322. tosa_bat_bu.psy = power_supply_register(&dev->dev, &tosa_bat_bu_desc,
  323. &bu_psy_cfg);
  324. if (IS_ERR(tosa_bat_bu.psy)) {
  325. ret = PTR_ERR(tosa_bat_bu.psy);
  326. goto err_psy_reg_bu;
  327. }
  328. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG),
  329. tosa_bat_gpio_isr,
  330. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  331. "main full", &tosa_bat_main);
  332. if (ret)
  333. goto err_req_main;
  334. ret = request_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG),
  335. tosa_bat_gpio_isr,
  336. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  337. "jacket full", &tosa_bat_jacket);
  338. if (ret)
  339. goto err_req_jacket;
  340. ret = request_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT),
  341. tosa_bat_gpio_isr,
  342. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  343. "jacket detect", &tosa_bat_jacket);
  344. if (!ret) {
  345. schedule_work(&bat_work);
  346. return 0;
  347. }
  348. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  349. err_req_jacket:
  350. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  351. err_req_main:
  352. power_supply_unregister(tosa_bat_bu.psy);
  353. err_psy_reg_bu:
  354. power_supply_unregister(tosa_bat_jacket.psy);
  355. err_psy_reg_jacket:
  356. power_supply_unregister(tosa_bat_main.psy);
  357. err_psy_reg_main:
  358. /* see comment in tosa_bat_remove */
  359. cancel_work_sync(&bat_work);
  360. gpio_free_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  361. return ret;
  362. }
  363. static int tosa_bat_remove(struct platform_device *dev)
  364. {
  365. free_irq(gpio_to_irq(TOSA_GPIO_JACKET_DETECT), &tosa_bat_jacket);
  366. free_irq(gpio_to_irq(TOSA_GPIO_BAT1_CRG), &tosa_bat_jacket);
  367. free_irq(gpio_to_irq(TOSA_GPIO_BAT0_CRG), &tosa_bat_main);
  368. power_supply_unregister(tosa_bat_bu.psy);
  369. power_supply_unregister(tosa_bat_jacket.psy);
  370. power_supply_unregister(tosa_bat_main.psy);
  371. /*
  372. * Now cancel the bat_work. We won't get any more schedules,
  373. * since all sources (isr and external_power_changed) are
  374. * unregistered now.
  375. */
  376. cancel_work_sync(&bat_work);
  377. gpio_free_array(tosa_bat_gpios, ARRAY_SIZE(tosa_bat_gpios));
  378. return 0;
  379. }
  380. static struct platform_driver tosa_bat_driver = {
  381. .driver.name = "wm97xx-battery",
  382. .driver.owner = THIS_MODULE,
  383. .probe = tosa_bat_probe,
  384. .remove = tosa_bat_remove,
  385. .suspend = tosa_bat_suspend,
  386. .resume = tosa_bat_resume,
  387. };
  388. module_platform_driver(tosa_bat_driver);
  389. MODULE_LICENSE("GPL");
  390. MODULE_AUTHOR("Dmitry Baryshkov");
  391. MODULE_DESCRIPTION("Tosa battery driver");
  392. MODULE_ALIAS("platform:wm97xx-battery");