collie_battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Battery and Power Management code for the Sharp SL-5x00
  3. *
  4. * Copyright (C) 2009 Thomas Kunze
  5. *
  6. * based on tosa_battery.c
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/power_supply.h>
  16. #include <linux/delay.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/gpio.h>
  20. #include <linux/mfd/ucb1x00.h>
  21. #include <asm/mach/sharpsl_param.h>
  22. #include <asm/mach-types.h>
  23. #include <mach/collie.h>
  24. static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
  25. static struct work_struct bat_work;
  26. static struct ucb1x00 *ucb;
  27. struct collie_bat {
  28. int status;
  29. struct power_supply *psy;
  30. int full_chrg;
  31. struct mutex work_lock; /* protects data */
  32. bool (*is_present)(struct collie_bat *bat);
  33. int gpio_full;
  34. int gpio_charge_on;
  35. int technology;
  36. int gpio_bat;
  37. int adc_bat;
  38. int adc_bat_divider;
  39. int bat_max;
  40. int bat_min;
  41. int gpio_temp;
  42. int adc_temp;
  43. int adc_temp_divider;
  44. };
  45. static struct collie_bat collie_bat_main;
  46. static unsigned long collie_read_bat(struct collie_bat *bat)
  47. {
  48. unsigned long value = 0;
  49. if (bat->gpio_bat < 0 || bat->adc_bat < 0)
  50. return 0;
  51. mutex_lock(&bat_lock);
  52. gpio_set_value(bat->gpio_bat, 1);
  53. msleep(5);
  54. ucb1x00_adc_enable(ucb);
  55. value = ucb1x00_adc_read(ucb, bat->adc_bat, UCB_SYNC);
  56. ucb1x00_adc_disable(ucb);
  57. gpio_set_value(bat->gpio_bat, 0);
  58. mutex_unlock(&bat_lock);
  59. value = value * 1000000 / bat->adc_bat_divider;
  60. return value;
  61. }
  62. static unsigned long collie_read_temp(struct collie_bat *bat)
  63. {
  64. unsigned long value = 0;
  65. if (bat->gpio_temp < 0 || bat->adc_temp < 0)
  66. return 0;
  67. mutex_lock(&bat_lock);
  68. gpio_set_value(bat->gpio_temp, 1);
  69. msleep(5);
  70. ucb1x00_adc_enable(ucb);
  71. value = ucb1x00_adc_read(ucb, bat->adc_temp, UCB_SYNC);
  72. ucb1x00_adc_disable(ucb);
  73. gpio_set_value(bat->gpio_temp, 0);
  74. mutex_unlock(&bat_lock);
  75. value = value * 10000 / bat->adc_temp_divider;
  76. return value;
  77. }
  78. static int collie_bat_get_property(struct power_supply *psy,
  79. enum power_supply_property psp,
  80. union power_supply_propval *val)
  81. {
  82. int ret = 0;
  83. struct collie_bat *bat = power_supply_get_drvdata(psy);
  84. if (bat->is_present && !bat->is_present(bat)
  85. && psp != POWER_SUPPLY_PROP_PRESENT) {
  86. return -ENODEV;
  87. }
  88. switch (psp) {
  89. case POWER_SUPPLY_PROP_STATUS:
  90. val->intval = bat->status;
  91. break;
  92. case POWER_SUPPLY_PROP_TECHNOLOGY:
  93. val->intval = bat->technology;
  94. break;
  95. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  96. val->intval = collie_read_bat(bat);
  97. break;
  98. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  99. if (bat->full_chrg == -1)
  100. val->intval = bat->bat_max;
  101. else
  102. val->intval = bat->full_chrg;
  103. break;
  104. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  105. val->intval = bat->bat_max;
  106. break;
  107. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  108. val->intval = bat->bat_min;
  109. break;
  110. case POWER_SUPPLY_PROP_TEMP:
  111. val->intval = collie_read_temp(bat);
  112. break;
  113. case POWER_SUPPLY_PROP_PRESENT:
  114. val->intval = bat->is_present ? bat->is_present(bat) : 1;
  115. break;
  116. default:
  117. ret = -EINVAL;
  118. break;
  119. }
  120. return ret;
  121. }
  122. static void collie_bat_external_power_changed(struct power_supply *psy)
  123. {
  124. schedule_work(&bat_work);
  125. }
  126. static irqreturn_t collie_bat_gpio_isr(int irq, void *data)
  127. {
  128. pr_info("collie_bat_gpio irq\n");
  129. schedule_work(&bat_work);
  130. return IRQ_HANDLED;
  131. }
  132. static void collie_bat_update(struct collie_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_on, 1);
  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 = collie_read_bat(bat);
  151. gpio_set_value(bat->gpio_charge_on, 0);
  152. bat->status = POWER_SUPPLY_STATUS_FULL;
  153. } else {
  154. gpio_set_value(bat->gpio_charge_on, 1);
  155. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  156. }
  157. } else {
  158. gpio_set_value(bat->gpio_charge_on, 0);
  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 collie_bat_work(struct work_struct *work)
  166. {
  167. collie_bat_update(&collie_bat_main);
  168. }
  169. static enum power_supply_property collie_bat_main_props[] = {
  170. POWER_SUPPLY_PROP_STATUS,
  171. POWER_SUPPLY_PROP_TECHNOLOGY,
  172. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  173. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  174. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  175. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  176. POWER_SUPPLY_PROP_PRESENT,
  177. POWER_SUPPLY_PROP_TEMP,
  178. };
  179. static enum power_supply_property collie_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_VOLTAGE_MAX,
  186. POWER_SUPPLY_PROP_PRESENT,
  187. };
  188. static const struct power_supply_desc collie_bat_main_desc = {
  189. .name = "main-battery",
  190. .type = POWER_SUPPLY_TYPE_BATTERY,
  191. .properties = collie_bat_main_props,
  192. .num_properties = ARRAY_SIZE(collie_bat_main_props),
  193. .get_property = collie_bat_get_property,
  194. .external_power_changed = collie_bat_external_power_changed,
  195. .use_for_apm = 1,
  196. };
  197. static struct collie_bat collie_bat_main = {
  198. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  199. .full_chrg = -1,
  200. .psy = NULL,
  201. .gpio_full = COLLIE_GPIO_CO,
  202. .gpio_charge_on = COLLIE_GPIO_CHARGE_ON,
  203. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  204. .gpio_bat = COLLIE_GPIO_MBAT_ON,
  205. .adc_bat = UCB_ADC_INP_AD1,
  206. .adc_bat_divider = 155,
  207. .bat_max = 4310000,
  208. .bat_min = 1551 * 1000000 / 414,
  209. .gpio_temp = COLLIE_GPIO_TMP_ON,
  210. .adc_temp = UCB_ADC_INP_AD0,
  211. .adc_temp_divider = 10000,
  212. };
  213. static const struct power_supply_desc collie_bat_bu_desc = {
  214. .name = "backup-battery",
  215. .type = POWER_SUPPLY_TYPE_BATTERY,
  216. .properties = collie_bat_bu_props,
  217. .num_properties = ARRAY_SIZE(collie_bat_bu_props),
  218. .get_property = collie_bat_get_property,
  219. .external_power_changed = collie_bat_external_power_changed,
  220. };
  221. static struct collie_bat collie_bat_bu = {
  222. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  223. .full_chrg = -1,
  224. .psy = NULL,
  225. .gpio_full = -1,
  226. .gpio_charge_on = -1,
  227. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  228. .gpio_bat = COLLIE_GPIO_BBAT_ON,
  229. .adc_bat = UCB_ADC_INP_AD1,
  230. .adc_bat_divider = 155,
  231. .bat_max = 3000000,
  232. .bat_min = 1900000,
  233. .gpio_temp = -1,
  234. .adc_temp = -1,
  235. .adc_temp_divider = -1,
  236. };
  237. static struct gpio collie_batt_gpios[] = {
  238. { COLLIE_GPIO_CO, GPIOF_IN, "main battery full" },
  239. { COLLIE_GPIO_MAIN_BAT_LOW, GPIOF_IN, "main battery low" },
  240. { COLLIE_GPIO_CHARGE_ON, GPIOF_OUT_INIT_LOW, "main charge on" },
  241. { COLLIE_GPIO_MBAT_ON, GPIOF_OUT_INIT_LOW, "main battery" },
  242. { COLLIE_GPIO_TMP_ON, GPIOF_OUT_INIT_LOW, "main battery temp" },
  243. { COLLIE_GPIO_BBAT_ON, GPIOF_OUT_INIT_LOW, "backup battery" },
  244. };
  245. #ifdef CONFIG_PM
  246. static int wakeup_enabled;
  247. static int collie_bat_suspend(struct ucb1x00_dev *dev)
  248. {
  249. /* flush all pending status updates */
  250. flush_work(&bat_work);
  251. if (device_may_wakeup(&dev->ucb->dev) &&
  252. collie_bat_main.status == POWER_SUPPLY_STATUS_CHARGING)
  253. wakeup_enabled = !enable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
  254. else
  255. wakeup_enabled = 0;
  256. return 0;
  257. }
  258. static int collie_bat_resume(struct ucb1x00_dev *dev)
  259. {
  260. if (wakeup_enabled)
  261. disable_irq_wake(gpio_to_irq(COLLIE_GPIO_CO));
  262. /* things may have changed while we were away */
  263. schedule_work(&bat_work);
  264. return 0;
  265. }
  266. #else
  267. #define collie_bat_suspend NULL
  268. #define collie_bat_resume NULL
  269. #endif
  270. static int collie_bat_probe(struct ucb1x00_dev *dev)
  271. {
  272. int ret;
  273. struct power_supply_config psy_main_cfg = {}, psy_bu_cfg = {};
  274. if (!machine_is_collie())
  275. return -ENODEV;
  276. ucb = dev->ucb;
  277. ret = gpio_request_array(collie_batt_gpios,
  278. ARRAY_SIZE(collie_batt_gpios));
  279. if (ret)
  280. return ret;
  281. mutex_init(&collie_bat_main.work_lock);
  282. INIT_WORK(&bat_work, collie_bat_work);
  283. psy_main_cfg.drv_data = &collie_bat_main;
  284. collie_bat_main.psy = power_supply_register(&dev->ucb->dev,
  285. &collie_bat_main_desc,
  286. &psy_main_cfg);
  287. if (IS_ERR(collie_bat_main.psy)) {
  288. ret = PTR_ERR(collie_bat_main.psy);
  289. goto err_psy_reg_main;
  290. }
  291. psy_bu_cfg.drv_data = &collie_bat_bu;
  292. collie_bat_bu.psy = power_supply_register(&dev->ucb->dev,
  293. &collie_bat_bu_desc,
  294. &psy_bu_cfg);
  295. if (IS_ERR(collie_bat_bu.psy)) {
  296. ret = PTR_ERR(collie_bat_bu.psy);
  297. goto err_psy_reg_bu;
  298. }
  299. ret = request_irq(gpio_to_irq(COLLIE_GPIO_CO),
  300. collie_bat_gpio_isr,
  301. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  302. "main full", &collie_bat_main);
  303. if (ret)
  304. goto err_irq;
  305. device_init_wakeup(&ucb->dev, 1);
  306. schedule_work(&bat_work);
  307. return 0;
  308. err_irq:
  309. power_supply_unregister(collie_bat_bu.psy);
  310. err_psy_reg_bu:
  311. power_supply_unregister(collie_bat_main.psy);
  312. err_psy_reg_main:
  313. /* see comment in collie_bat_remove */
  314. cancel_work_sync(&bat_work);
  315. gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
  316. return ret;
  317. }
  318. static void collie_bat_remove(struct ucb1x00_dev *dev)
  319. {
  320. free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main);
  321. power_supply_unregister(collie_bat_bu.psy);
  322. power_supply_unregister(collie_bat_main.psy);
  323. /*
  324. * Now cancel the bat_work. We won't get any more schedules,
  325. * since all sources (isr and external_power_changed) are
  326. * unregistered now.
  327. */
  328. cancel_work_sync(&bat_work);
  329. gpio_free_array(collie_batt_gpios, ARRAY_SIZE(collie_batt_gpios));
  330. }
  331. static struct ucb1x00_driver collie_bat_driver = {
  332. .add = collie_bat_probe,
  333. .remove = collie_bat_remove,
  334. .suspend = collie_bat_suspend,
  335. .resume = collie_bat_resume,
  336. };
  337. static int __init collie_bat_init(void)
  338. {
  339. return ucb1x00_register_driver(&collie_bat_driver);
  340. }
  341. static void __exit collie_bat_exit(void)
  342. {
  343. ucb1x00_unregister_driver(&collie_bat_driver);
  344. }
  345. module_init(collie_bat_init);
  346. module_exit(collie_bat_exit);
  347. MODULE_LICENSE("GPL");
  348. MODULE_AUTHOR("Thomas Kunze");
  349. MODULE_DESCRIPTION("Collie battery driver");