collie_battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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. static int wakeup_enabled;
  28. struct collie_bat {
  29. int status;
  30. struct power_supply *psy;
  31. int full_chrg;
  32. struct mutex work_lock; /* protects data */
  33. bool (*is_present)(struct collie_bat *bat);
  34. int gpio_full;
  35. int gpio_charge_on;
  36. int technology;
  37. int gpio_bat;
  38. int adc_bat;
  39. int adc_bat_divider;
  40. int bat_max;
  41. int bat_min;
  42. int gpio_temp;
  43. int adc_temp;
  44. int adc_temp_divider;
  45. };
  46. static struct collie_bat collie_bat_main;
  47. static unsigned long collie_read_bat(struct collie_bat *bat)
  48. {
  49. unsigned long value = 0;
  50. if (bat->gpio_bat < 0 || bat->adc_bat < 0)
  51. return 0;
  52. mutex_lock(&bat_lock);
  53. gpio_set_value(bat->gpio_bat, 1);
  54. msleep(5);
  55. ucb1x00_adc_enable(ucb);
  56. value = ucb1x00_adc_read(ucb, bat->adc_bat, UCB_SYNC);
  57. ucb1x00_adc_disable(ucb);
  58. gpio_set_value(bat->gpio_bat, 0);
  59. mutex_unlock(&bat_lock);
  60. value = value * 1000000 / bat->adc_bat_divider;
  61. return value;
  62. }
  63. static unsigned long collie_read_temp(struct collie_bat *bat)
  64. {
  65. unsigned long value = 0;
  66. if (bat->gpio_temp < 0 || bat->adc_temp < 0)
  67. return 0;
  68. mutex_lock(&bat_lock);
  69. gpio_set_value(bat->gpio_temp, 1);
  70. msleep(5);
  71. ucb1x00_adc_enable(ucb);
  72. value = ucb1x00_adc_read(ucb, bat->adc_temp, UCB_SYNC);
  73. ucb1x00_adc_disable(ucb);
  74. gpio_set_value(bat->gpio_temp, 0);
  75. mutex_unlock(&bat_lock);
  76. value = value * 10000 / bat->adc_temp_divider;
  77. return value;
  78. }
  79. static int collie_bat_get_property(struct power_supply *psy,
  80. enum power_supply_property psp,
  81. union power_supply_propval *val)
  82. {
  83. int ret = 0;
  84. struct collie_bat *bat = power_supply_get_drvdata(psy);
  85. if (bat->is_present && !bat->is_present(bat)
  86. && psp != POWER_SUPPLY_PROP_PRESENT) {
  87. return -ENODEV;
  88. }
  89. switch (psp) {
  90. case POWER_SUPPLY_PROP_STATUS:
  91. val->intval = bat->status;
  92. break;
  93. case POWER_SUPPLY_PROP_TECHNOLOGY:
  94. val->intval = bat->technology;
  95. break;
  96. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  97. val->intval = collie_read_bat(bat);
  98. break;
  99. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  100. if (bat->full_chrg == -1)
  101. val->intval = bat->bat_max;
  102. else
  103. val->intval = bat->full_chrg;
  104. break;
  105. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  106. val->intval = bat->bat_max;
  107. break;
  108. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  109. val->intval = bat->bat_min;
  110. break;
  111. case POWER_SUPPLY_PROP_TEMP:
  112. val->intval = collie_read_temp(bat);
  113. break;
  114. case POWER_SUPPLY_PROP_PRESENT:
  115. val->intval = bat->is_present ? bat->is_present(bat) : 1;
  116. break;
  117. default:
  118. ret = -EINVAL;
  119. break;
  120. }
  121. return ret;
  122. }
  123. static void collie_bat_external_power_changed(struct power_supply *psy)
  124. {
  125. schedule_work(&bat_work);
  126. }
  127. static irqreturn_t collie_bat_gpio_isr(int irq, void *data)
  128. {
  129. pr_info("collie_bat_gpio irq\n");
  130. schedule_work(&bat_work);
  131. return IRQ_HANDLED;
  132. }
  133. static void collie_bat_update(struct collie_bat *bat)
  134. {
  135. int old;
  136. struct power_supply *psy = bat->psy;
  137. mutex_lock(&bat->work_lock);
  138. old = bat->status;
  139. if (bat->is_present && !bat->is_present(bat)) {
  140. printk(KERN_NOTICE "%s not present\n", psy->desc->name);
  141. bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  142. bat->full_chrg = -1;
  143. } else if (power_supply_am_i_supplied(psy)) {
  144. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
  145. gpio_set_value(bat->gpio_charge_on, 1);
  146. mdelay(15);
  147. }
  148. if (gpio_get_value(bat->gpio_full)) {
  149. if (old == POWER_SUPPLY_STATUS_CHARGING ||
  150. bat->full_chrg == -1)
  151. bat->full_chrg = collie_read_bat(bat);
  152. gpio_set_value(bat->gpio_charge_on, 0);
  153. bat->status = POWER_SUPPLY_STATUS_FULL;
  154. } else {
  155. gpio_set_value(bat->gpio_charge_on, 1);
  156. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  157. }
  158. } else {
  159. gpio_set_value(bat->gpio_charge_on, 0);
  160. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  161. }
  162. if (old != bat->status)
  163. power_supply_changed(psy);
  164. mutex_unlock(&bat->work_lock);
  165. }
  166. static void collie_bat_work(struct work_struct *work)
  167. {
  168. collie_bat_update(&collie_bat_main);
  169. }
  170. static enum power_supply_property collie_bat_main_props[] = {
  171. POWER_SUPPLY_PROP_STATUS,
  172. POWER_SUPPLY_PROP_TECHNOLOGY,
  173. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  174. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  175. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  176. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  177. POWER_SUPPLY_PROP_PRESENT,
  178. POWER_SUPPLY_PROP_TEMP,
  179. };
  180. static enum power_supply_property collie_bat_bu_props[] = {
  181. POWER_SUPPLY_PROP_STATUS,
  182. POWER_SUPPLY_PROP_TECHNOLOGY,
  183. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  184. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  185. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  186. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  187. POWER_SUPPLY_PROP_PRESENT,
  188. };
  189. static const struct power_supply_desc collie_bat_main_desc = {
  190. .name = "main-battery",
  191. .type = POWER_SUPPLY_TYPE_BATTERY,
  192. .properties = collie_bat_main_props,
  193. .num_properties = ARRAY_SIZE(collie_bat_main_props),
  194. .get_property = collie_bat_get_property,
  195. .external_power_changed = collie_bat_external_power_changed,
  196. .use_for_apm = 1,
  197. };
  198. static struct collie_bat collie_bat_main = {
  199. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  200. .full_chrg = -1,
  201. .psy = NULL,
  202. .gpio_full = COLLIE_GPIO_CO,
  203. .gpio_charge_on = COLLIE_GPIO_CHARGE_ON,
  204. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  205. .gpio_bat = COLLIE_GPIO_MBAT_ON,
  206. .adc_bat = UCB_ADC_INP_AD1,
  207. .adc_bat_divider = 155,
  208. .bat_max = 4310000,
  209. .bat_min = 1551 * 1000000 / 414,
  210. .gpio_temp = COLLIE_GPIO_TMP_ON,
  211. .adc_temp = UCB_ADC_INP_AD0,
  212. .adc_temp_divider = 10000,
  213. };
  214. static const struct power_supply_desc collie_bat_bu_desc = {
  215. .name = "backup-battery",
  216. .type = POWER_SUPPLY_TYPE_BATTERY,
  217. .properties = collie_bat_bu_props,
  218. .num_properties = ARRAY_SIZE(collie_bat_bu_props),
  219. .get_property = collie_bat_get_property,
  220. .external_power_changed = collie_bat_external_power_changed,
  221. };
  222. static struct collie_bat collie_bat_bu = {
  223. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  224. .full_chrg = -1,
  225. .psy = NULL,
  226. .gpio_full = -1,
  227. .gpio_charge_on = -1,
  228. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  229. .gpio_bat = COLLIE_GPIO_BBAT_ON,
  230. .adc_bat = UCB_ADC_INP_AD1,
  231. .adc_bat_divider = 155,
  232. .bat_max = 3000000,
  233. .bat_min = 1900000,
  234. .gpio_temp = -1,
  235. .adc_temp = -1,
  236. .adc_temp_divider = -1,
  237. };
  238. static struct gpio collie_batt_gpios[] = {
  239. { COLLIE_GPIO_CO, GPIOF_IN, "main battery full" },
  240. { COLLIE_GPIO_MAIN_BAT_LOW, GPIOF_IN, "main battery low" },
  241. { COLLIE_GPIO_CHARGE_ON, GPIOF_OUT_INIT_LOW, "main charge on" },
  242. { COLLIE_GPIO_MBAT_ON, GPIOF_OUT_INIT_LOW, "main battery" },
  243. { COLLIE_GPIO_TMP_ON, GPIOF_OUT_INIT_LOW, "main battery temp" },
  244. { COLLIE_GPIO_BBAT_ON, GPIOF_OUT_INIT_LOW, "backup battery" },
  245. };
  246. #ifdef CONFIG_PM
  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");