collie_battery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Battery and Power Management code for the Sharp SL-5x00
  4. *
  5. * Copyright (C) 2009 Thomas Kunze
  6. *
  7. * based on tosa_battery.c
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/power_supply.h>
  12. #include <linux/delay.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/gpio/driver.h>
  16. #include <linux/gpio/machine.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/mfd/ucb1x00.h>
  19. #include <asm/mach/sharpsl_param.h>
  20. #include <asm/mach-types.h>
  21. #include <mach/collie.h>
  22. static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
  23. static struct work_struct bat_work;
  24. static struct ucb1x00 *ucb;
  25. struct collie_bat {
  26. int status;
  27. struct power_supply *psy;
  28. int full_chrg;
  29. struct mutex work_lock; /* protects data */
  30. bool (*is_present)(struct collie_bat *bat);
  31. struct gpio_desc *gpio_full;
  32. struct gpio_desc *gpio_charge_on;
  33. int technology;
  34. struct gpio_desc *gpio_bat;
  35. int adc_bat;
  36. int adc_bat_divider;
  37. int bat_max;
  38. int bat_min;
  39. struct gpio_desc *gpio_temp;
  40. int adc_temp;
  41. int adc_temp_divider;
  42. };
  43. static struct collie_bat collie_bat_main;
  44. static unsigned long collie_read_bat(struct collie_bat *bat)
  45. {
  46. unsigned long value = 0;
  47. if (!bat->gpio_bat || bat->adc_bat < 0)
  48. return 0;
  49. mutex_lock(&bat_lock);
  50. gpiod_set_value(bat->gpio_bat, 1);
  51. msleep(5);
  52. ucb1x00_adc_enable(ucb);
  53. value = ucb1x00_adc_read(ucb, bat->adc_bat, UCB_SYNC);
  54. ucb1x00_adc_disable(ucb);
  55. gpiod_set_value(bat->gpio_bat, 0);
  56. mutex_unlock(&bat_lock);
  57. value = value * 1000000 / bat->adc_bat_divider;
  58. return value;
  59. }
  60. static unsigned long collie_read_temp(struct collie_bat *bat)
  61. {
  62. unsigned long value = 0;
  63. if (!bat->gpio_temp || bat->adc_temp < 0)
  64. return 0;
  65. mutex_lock(&bat_lock);
  66. gpiod_set_value(bat->gpio_temp, 1);
  67. msleep(5);
  68. ucb1x00_adc_enable(ucb);
  69. value = ucb1x00_adc_read(ucb, bat->adc_temp, UCB_SYNC);
  70. ucb1x00_adc_disable(ucb);
  71. gpiod_set_value(bat->gpio_temp, 0);
  72. mutex_unlock(&bat_lock);
  73. value = value * 10000 / bat->adc_temp_divider;
  74. return value;
  75. }
  76. static int collie_bat_get_property(struct power_supply *psy,
  77. enum power_supply_property psp,
  78. union power_supply_propval *val)
  79. {
  80. int ret = 0;
  81. struct collie_bat *bat = power_supply_get_drvdata(psy);
  82. if (bat->is_present && !bat->is_present(bat)
  83. && psp != POWER_SUPPLY_PROP_PRESENT) {
  84. return -ENODEV;
  85. }
  86. switch (psp) {
  87. case POWER_SUPPLY_PROP_STATUS:
  88. val->intval = bat->status;
  89. break;
  90. case POWER_SUPPLY_PROP_TECHNOLOGY:
  91. val->intval = bat->technology;
  92. break;
  93. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  94. val->intval = collie_read_bat(bat);
  95. break;
  96. case POWER_SUPPLY_PROP_VOLTAGE_MAX:
  97. if (bat->full_chrg == -1)
  98. val->intval = bat->bat_max;
  99. else
  100. val->intval = bat->full_chrg;
  101. break;
  102. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  103. val->intval = bat->bat_max;
  104. break;
  105. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  106. val->intval = bat->bat_min;
  107. break;
  108. case POWER_SUPPLY_PROP_TEMP:
  109. val->intval = collie_read_temp(bat);
  110. break;
  111. case POWER_SUPPLY_PROP_PRESENT:
  112. val->intval = bat->is_present ? bat->is_present(bat) : 1;
  113. break;
  114. default:
  115. ret = -EINVAL;
  116. break;
  117. }
  118. return ret;
  119. }
  120. static void collie_bat_external_power_changed(struct power_supply *psy)
  121. {
  122. schedule_work(&bat_work);
  123. }
  124. static irqreturn_t collie_bat_gpio_isr(int irq, void *data)
  125. {
  126. pr_info("collie_bat_gpio irq\n");
  127. schedule_work(&bat_work);
  128. return IRQ_HANDLED;
  129. }
  130. static void collie_bat_update(struct collie_bat *bat)
  131. {
  132. int old;
  133. struct power_supply *psy = bat->psy;
  134. mutex_lock(&bat->work_lock);
  135. old = bat->status;
  136. if (bat->is_present && !bat->is_present(bat)) {
  137. printk(KERN_NOTICE "%s not present\n", psy->desc->name);
  138. bat->status = POWER_SUPPLY_STATUS_UNKNOWN;
  139. bat->full_chrg = -1;
  140. } else if (power_supply_am_i_supplied(psy)) {
  141. if (bat->status == POWER_SUPPLY_STATUS_DISCHARGING) {
  142. gpiod_set_value(bat->gpio_charge_on, 1);
  143. mdelay(15);
  144. }
  145. if (gpiod_get_value(bat->gpio_full)) {
  146. if (old == POWER_SUPPLY_STATUS_CHARGING ||
  147. bat->full_chrg == -1)
  148. bat->full_chrg = collie_read_bat(bat);
  149. gpiod_set_value(bat->gpio_charge_on, 0);
  150. bat->status = POWER_SUPPLY_STATUS_FULL;
  151. } else {
  152. gpiod_set_value(bat->gpio_charge_on, 1);
  153. bat->status = POWER_SUPPLY_STATUS_CHARGING;
  154. }
  155. } else {
  156. gpiod_set_value(bat->gpio_charge_on, 0);
  157. bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
  158. }
  159. if (old != bat->status)
  160. power_supply_changed(psy);
  161. mutex_unlock(&bat->work_lock);
  162. }
  163. static void collie_bat_work(struct work_struct *work)
  164. {
  165. collie_bat_update(&collie_bat_main);
  166. }
  167. static enum power_supply_property collie_bat_main_props[] = {
  168. POWER_SUPPLY_PROP_STATUS,
  169. POWER_SUPPLY_PROP_TECHNOLOGY,
  170. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  171. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  172. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  173. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  174. POWER_SUPPLY_PROP_PRESENT,
  175. POWER_SUPPLY_PROP_TEMP,
  176. };
  177. static enum power_supply_property collie_bat_bu_props[] = {
  178. POWER_SUPPLY_PROP_STATUS,
  179. POWER_SUPPLY_PROP_TECHNOLOGY,
  180. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  181. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  182. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  183. POWER_SUPPLY_PROP_VOLTAGE_MAX,
  184. POWER_SUPPLY_PROP_PRESENT,
  185. };
  186. static const struct power_supply_desc collie_bat_main_desc = {
  187. .name = "main-battery",
  188. .type = POWER_SUPPLY_TYPE_BATTERY,
  189. .properties = collie_bat_main_props,
  190. .num_properties = ARRAY_SIZE(collie_bat_main_props),
  191. .get_property = collie_bat_get_property,
  192. .external_power_changed = collie_bat_external_power_changed,
  193. .use_for_apm = 1,
  194. };
  195. static struct collie_bat collie_bat_main = {
  196. .status = POWER_SUPPLY_STATUS_DISCHARGING,
  197. .full_chrg = -1,
  198. .psy = NULL,
  199. .gpio_full = NULL,
  200. .gpio_charge_on = NULL,
  201. .technology = POWER_SUPPLY_TECHNOLOGY_LIPO,
  202. .gpio_bat = NULL,
  203. .adc_bat = UCB_ADC_INP_AD1,
  204. .adc_bat_divider = 155,
  205. .bat_max = 4310000,
  206. .bat_min = 1551 * 1000000 / 414,
  207. .gpio_temp = NULL,
  208. .adc_temp = UCB_ADC_INP_AD0,
  209. .adc_temp_divider = 10000,
  210. };
  211. static const struct power_supply_desc collie_bat_bu_desc = {
  212. .name = "backup-battery",
  213. .type = POWER_SUPPLY_TYPE_BATTERY,
  214. .properties = collie_bat_bu_props,
  215. .num_properties = ARRAY_SIZE(collie_bat_bu_props),
  216. .get_property = collie_bat_get_property,
  217. .external_power_changed = collie_bat_external_power_changed,
  218. };
  219. static struct collie_bat collie_bat_bu = {
  220. .status = POWER_SUPPLY_STATUS_UNKNOWN,
  221. .full_chrg = -1,
  222. .psy = NULL,
  223. .gpio_full = NULL,
  224. .gpio_charge_on = NULL,
  225. .technology = POWER_SUPPLY_TECHNOLOGY_LiMn,
  226. .gpio_bat = NULL,
  227. .adc_bat = UCB_ADC_INP_AD1,
  228. .adc_bat_divider = 155,
  229. .bat_max = 3000000,
  230. .bat_min = 1900000,
  231. .gpio_temp = NULL,
  232. .adc_temp = -1,
  233. .adc_temp_divider = -1,
  234. };
  235. /* Obtained but unused GPIO */
  236. static struct gpio_desc *collie_mbat_low;
  237. #ifdef CONFIG_PM
  238. static int wakeup_enabled;
  239. static int collie_bat_suspend(struct ucb1x00_dev *dev)
  240. {
  241. /* flush all pending status updates */
  242. flush_work(&bat_work);
  243. if (device_may_wakeup(&dev->ucb->dev) &&
  244. collie_bat_main.status == POWER_SUPPLY_STATUS_CHARGING)
  245. wakeup_enabled = !enable_irq_wake(gpiod_to_irq(collie_bat_main.gpio_full));
  246. else
  247. wakeup_enabled = 0;
  248. return 0;
  249. }
  250. static int collie_bat_resume(struct ucb1x00_dev *dev)
  251. {
  252. if (wakeup_enabled)
  253. disable_irq_wake(gpiod_to_irq(collie_bat_main.gpio_full));
  254. /* things may have changed while we were away */
  255. schedule_work(&bat_work);
  256. return 0;
  257. }
  258. #else
  259. #define collie_bat_suspend NULL
  260. #define collie_bat_resume NULL
  261. #endif
  262. static int collie_bat_probe(struct ucb1x00_dev *dev)
  263. {
  264. int ret;
  265. struct power_supply_config psy_main_cfg = {}, psy_bu_cfg = {};
  266. struct gpio_chip *gc = &dev->ucb->gpio;
  267. if (!machine_is_collie())
  268. return -ENODEV;
  269. ucb = dev->ucb;
  270. /* Obtain all the main battery GPIOs */
  271. collie_bat_main.gpio_full = gpiod_get(&dev->ucb->dev,
  272. "main battery full",
  273. GPIOD_IN);
  274. if (IS_ERR(collie_bat_main.gpio_full))
  275. return PTR_ERR(collie_bat_main.gpio_full);
  276. collie_mbat_low = gpiod_get(&dev->ucb->dev,
  277. "main battery low",
  278. GPIOD_IN);
  279. if (IS_ERR(collie_mbat_low)) {
  280. ret = PTR_ERR(collie_mbat_low);
  281. goto err_put_gpio_full;
  282. }
  283. collie_bat_main.gpio_charge_on = gpiod_get(&dev->ucb->dev,
  284. "main charge on",
  285. GPIOD_OUT_LOW);
  286. if (IS_ERR(collie_bat_main.gpio_charge_on)) {
  287. ret = PTR_ERR(collie_bat_main.gpio_charge_on);
  288. goto err_put_mbat_low;
  289. }
  290. /* COLLIE_GPIO_MBAT_ON = GPIO 7 on the UCB (TC35143) */
  291. collie_bat_main.gpio_bat = gpiochip_request_own_desc(gc,
  292. 7,
  293. "main battery",
  294. GPIO_ACTIVE_HIGH,
  295. GPIOD_OUT_LOW);
  296. if (IS_ERR(collie_bat_main.gpio_bat)) {
  297. ret = PTR_ERR(collie_bat_main.gpio_bat);
  298. goto err_put_gpio_charge_on;
  299. }
  300. /* COLLIE_GPIO_TMP_ON = GPIO 9 on the UCB (TC35143) */
  301. collie_bat_main.gpio_temp = gpiochip_request_own_desc(gc,
  302. 9,
  303. "main battery temp",
  304. GPIO_ACTIVE_HIGH,
  305. GPIOD_OUT_LOW);
  306. if (IS_ERR(collie_bat_main.gpio_temp)) {
  307. ret = PTR_ERR(collie_bat_main.gpio_temp);
  308. goto err_free_gpio_bat;
  309. }
  310. /*
  311. * Obtain the backup battery COLLIE_GPIO_BBAT_ON which is
  312. * GPIO 8 on the UCB (TC35143)
  313. */
  314. collie_bat_bu.gpio_bat = gpiochip_request_own_desc(gc,
  315. 8,
  316. "backup battery",
  317. GPIO_ACTIVE_HIGH,
  318. GPIOD_OUT_LOW);
  319. if (IS_ERR(collie_bat_bu.gpio_bat)) {
  320. ret = PTR_ERR(collie_bat_bu.gpio_bat);
  321. goto err_free_gpio_temp;
  322. }
  323. mutex_init(&collie_bat_main.work_lock);
  324. INIT_WORK(&bat_work, collie_bat_work);
  325. psy_main_cfg.drv_data = &collie_bat_main;
  326. collie_bat_main.psy = power_supply_register(&dev->ucb->dev,
  327. &collie_bat_main_desc,
  328. &psy_main_cfg);
  329. if (IS_ERR(collie_bat_main.psy)) {
  330. ret = PTR_ERR(collie_bat_main.psy);
  331. goto err_psy_reg_main;
  332. }
  333. psy_bu_cfg.drv_data = &collie_bat_bu;
  334. collie_bat_bu.psy = power_supply_register(&dev->ucb->dev,
  335. &collie_bat_bu_desc,
  336. &psy_bu_cfg);
  337. if (IS_ERR(collie_bat_bu.psy)) {
  338. ret = PTR_ERR(collie_bat_bu.psy);
  339. goto err_psy_reg_bu;
  340. }
  341. ret = request_irq(gpio_to_irq(COLLIE_GPIO_CO),
  342. collie_bat_gpio_isr,
  343. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  344. "main full", &collie_bat_main);
  345. if (ret)
  346. goto err_irq;
  347. device_init_wakeup(&ucb->dev, 1);
  348. schedule_work(&bat_work);
  349. return 0;
  350. err_irq:
  351. power_supply_unregister(collie_bat_bu.psy);
  352. err_psy_reg_bu:
  353. power_supply_unregister(collie_bat_main.psy);
  354. err_psy_reg_main:
  355. /* see comment in collie_bat_remove */
  356. cancel_work_sync(&bat_work);
  357. gpiochip_free_own_desc(collie_bat_bu.gpio_bat);
  358. err_free_gpio_temp:
  359. gpiochip_free_own_desc(collie_bat_main.gpio_temp);
  360. err_free_gpio_bat:
  361. gpiochip_free_own_desc(collie_bat_main.gpio_bat);
  362. err_put_gpio_charge_on:
  363. gpiod_put(collie_bat_main.gpio_charge_on);
  364. err_put_mbat_low:
  365. gpiod_put(collie_mbat_low);
  366. err_put_gpio_full:
  367. gpiod_put(collie_bat_main.gpio_full);
  368. return ret;
  369. }
  370. static void collie_bat_remove(struct ucb1x00_dev *dev)
  371. {
  372. free_irq(gpio_to_irq(COLLIE_GPIO_CO), &collie_bat_main);
  373. power_supply_unregister(collie_bat_bu.psy);
  374. power_supply_unregister(collie_bat_main.psy);
  375. /* These are obtained from the machine */
  376. gpiod_put(collie_bat_main.gpio_full);
  377. gpiod_put(collie_mbat_low);
  378. gpiod_put(collie_bat_main.gpio_charge_on);
  379. /* These are directly from the UCB so let's free them */
  380. gpiochip_free_own_desc(collie_bat_main.gpio_bat);
  381. gpiochip_free_own_desc(collie_bat_main.gpio_temp);
  382. gpiochip_free_own_desc(collie_bat_bu.gpio_bat);
  383. /*
  384. * Now cancel the bat_work. We won't get any more schedules,
  385. * since all sources (isr and external_power_changed) are
  386. * unregistered now.
  387. */
  388. cancel_work_sync(&bat_work);
  389. }
  390. static struct ucb1x00_driver collie_bat_driver = {
  391. .add = collie_bat_probe,
  392. .remove = collie_bat_remove,
  393. .suspend = collie_bat_suspend,
  394. .resume = collie_bat_resume,
  395. };
  396. static int __init collie_bat_init(void)
  397. {
  398. return ucb1x00_register_driver(&collie_bat_driver);
  399. }
  400. static void __exit collie_bat_exit(void)
  401. {
  402. ucb1x00_unregister_driver(&collie_bat_driver);
  403. }
  404. module_init(collie_bat_init);
  405. module_exit(collie_bat_exit);
  406. MODULE_LICENSE("GPL");
  407. MODULE_AUTHOR("Thomas Kunze");
  408. MODULE_DESCRIPTION("Collie battery driver");