collie_battery.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = container_of(psy, struct collie_bat, 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: %d\n", gpio_get_value(irq_to_gpio(irq)));
  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->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 struct collie_bat collie_bat_main = {
  189. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  190. .full_chrg = -1,
  191. .psy = {
  192. .name = "main-battery",
  193. .type = POWER_SUPPLY_TYPE_BATTERY,
  194. .properties = collie_bat_main_props,
  195. .num_properties = ARRAY_SIZE(collie_bat_main_props),
  196. .get_property = collie_bat_get_property,
  197. .external_power_changed = collie_bat_external_power_changed,
  198. .use_for_apm = 1,
  199. },
  200. .gpio_full = COLLIE_GPIO_CO,
  201. .gpio_charge_on = COLLIE_GPIO_CHARGE_ON,
  202. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  203. .gpio_bat = COLLIE_GPIO_MBAT_ON,
  204. .adc_bat = UCB_ADC_INP_AD1,
  205. .adc_bat_divider = 155,
  206. .bat_max = 4310000,
  207. .bat_min = 1551 * 1000000 / 414,
  208. .gpio_temp = COLLIE_GPIO_TMP_ON,
  209. .adc_temp = UCB_ADC_INP_AD0,
  210. .adc_temp_divider = 10000,
  211. };
  212. static struct collie_bat collie_bat_bu = {
  213. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  214. .full_chrg = -1,
  215. .psy = {
  216. .name = "backup-battery",
  217. .type = POWER_SUPPLY_TYPE_BATTERY,
  218. .properties = collie_bat_bu_props,
  219. .num_properties = ARRAY_SIZE(collie_bat_bu_props),
  220. .get_property = collie_bat_get_property,
  221. .external_power_changed = collie_bat_external_power_changed,
  222. },
  223. .gpio_full = -1,
  224. .gpio_charge_on = -1,
  225. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  226. .gpio_bat = COLLIE_GPIO_BBAT_ON,
  227. .adc_bat = UCB_ADC_INP_AD1,
  228. .adc_bat_divider = 155,
  229. .bat_max = 3000000,
  230. .bat_min = 1900000,
  231. .gpio_temp = -1,
  232. .adc_temp = -1,
  233. .adc_temp_divider = -1,
  234. };
  235. static struct {
  236. int gpio;
  237. char *name;
  238. bool output;
  239. int value;
  240. } gpios[] = {
  241. { COLLIE_GPIO_CO, "main battery full", 0, 0 },
  242. { COLLIE_GPIO_MAIN_BAT_LOW, "main battery low", 0, 0 },
  243. { COLLIE_GPIO_CHARGE_ON, "main charge on", 1, 0 },
  244. { COLLIE_GPIO_MBAT_ON, "main battery", 1, 0 },
  245. { COLLIE_GPIO_TMP_ON, "main battery temp", 1, 0 },
  246. { COLLIE_GPIO_BBAT_ON, "backup battery", 1, 0 },
  247. };
  248. #ifdef CONFIG_PM
  249. static int collie_bat_suspend(struct ucb1x00_dev *dev, pm_message_t state)
  250. {
  251. /* flush all pending status updates */
  252. flush_work_sync(&bat_work);
  253. return 0;
  254. }
  255. static int collie_bat_resume(struct ucb1x00_dev *dev)
  256. {
  257. /* things may have changed while we were away */
  258. schedule_work(&bat_work);
  259. return 0;
  260. }
  261. #else
  262. #define collie_bat_suspend NULL
  263. #define collie_bat_resume NULL
  264. #endif
  265. static int __devinit collie_bat_probe(struct ucb1x00_dev *dev)
  266. {
  267. int ret;
  268. int i;
  269. if (!machine_is_collie())
  270. return -ENODEV;
  271. ucb = dev->ucb;
  272. for (i = 0; i < ARRAY_SIZE(gpios); i++) {
  273. ret = gpio_request(gpios[i].gpio, gpios[i].name);
  274. if (ret) {
  275. i--;
  276. goto err_gpio;
  277. }
  278. if (gpios[i].output)
  279. ret = gpio_direction_output(gpios[i].gpio,
  280. gpios[i].value);
  281. else
  282. ret = gpio_direction_input(gpios[i].gpio);
  283. if (ret)
  284. goto err_gpio;
  285. }
  286. mutex_init(&collie_bat_main.work_lock);
  287. INIT_WORK(&bat_work, collie_bat_work);
  288. ret = power_supply_register(&dev->ucb->dev, &collie_bat_main.psy);
  289. if (ret)
  290. goto err_psy_reg_main;
  291. ret = power_supply_register(&dev->ucb->dev, &collie_bat_bu.psy);
  292. if (ret)
  293. goto err_psy_reg_bu;
  294. ret = request_irq(gpio_to_irq(COLLIE_GPIO_CO),
  295. collie_bat_gpio_isr,
  296. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  297. "main full", &collie_bat_main);
  298. if (!ret) {
  299. schedule_work(&bat_work);
  300. return 0;
  301. }
  302. power_supply_unregister(&collie_bat_bu.psy);
  303. err_psy_reg_bu:
  304. power_supply_unregister(&collie_bat_main.psy);
  305. err_psy_reg_main:
  306. /* see comment in collie_bat_remove */
  307. cancel_work_sync(&bat_work);
  308. i--;
  309. err_gpio:
  310. for (; i >= 0; i--)
  311. gpio_free(gpios[i].gpio);
  312. return ret;
  313. }
  314. static void __devexit collie_bat_remove(struct ucb1x00_dev *dev)
  315. {
  316. int i;
  317. free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main);
  318. power_supply_unregister(&collie_bat_bu.psy);
  319. power_supply_unregister(&collie_bat_main.psy);
  320. /*
  321. * Now cancel the bat_work. We won't get any more schedules,
  322. * since all sources (isr and external_power_changed) are
  323. * unregistered now.
  324. */
  325. cancel_work_sync(&bat_work);
  326. for (i = ARRAY_SIZE(gpios) - 1; i >= 0; i--)
  327. gpio_free(gpios[i].gpio);
  328. }
  329. static struct ucb1x00_driver collie_bat_driver = {
  330. .add = collie_bat_probe,
  331. .remove = __devexit_p(collie_bat_remove),
  332. .suspend = collie_bat_suspend,
  333. .resume = collie_bat_resume,
  334. };
  335. static int __init collie_bat_init(void)
  336. {
  337. return ucb1x00_register_driver(&collie_bat_driver);
  338. }
  339. static void __exit collie_bat_exit(void)
  340. {
  341. ucb1x00_unregister_driver(&collie_bat_driver);
  342. }
  343. module_init(collie_bat_init);
  344. module_exit(collie_bat_exit);
  345. MODULE_LICENSE("GPL");
  346. MODULE_AUTHOR("Thomas Kunze");
  347. MODULE_DESCRIPTION("Collie battery driver");