jz4740-battery.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Battery measurement code for Ingenic JZ SOC.
  3. *
  4. * Copyright (C) 2009 Jiejing Zhang <kzjeef@gmail.com>
  5. * Copyright (C) 2010, Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * based on tosa_battery.c
  8. *
  9. * Copyright (C) 2008 Marek Vasut <marek.vasut@gmail.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. */
  16. #include <linux/interrupt.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/io.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/gpio.h>
  25. #include <linux/mfd/core.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/power/jz4740-battery.h>
  28. #include <linux/jz4740-adc.h>
  29. struct jz_battery {
  30. struct jz_battery_platform_data *pdata;
  31. struct platform_device *pdev;
  32. void __iomem *base;
  33. int irq;
  34. int charge_irq;
  35. const struct mfd_cell *cell;
  36. int status;
  37. long voltage;
  38. struct completion read_completion;
  39. struct power_supply *battery;
  40. struct power_supply_desc battery_desc;
  41. struct delayed_work work;
  42. struct mutex lock;
  43. };
  44. static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
  45. {
  46. return power_supply_get_drvdata(psy);
  47. }
  48. static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
  49. {
  50. struct jz_battery *battery = devid;
  51. complete(&battery->read_completion);
  52. return IRQ_HANDLED;
  53. }
  54. static long jz_battery_read_voltage(struct jz_battery *battery)
  55. {
  56. long t;
  57. unsigned long val;
  58. long voltage;
  59. mutex_lock(&battery->lock);
  60. reinit_completion(&battery->read_completion);
  61. enable_irq(battery->irq);
  62. battery->cell->enable(battery->pdev);
  63. t = wait_for_completion_interruptible_timeout(&battery->read_completion,
  64. HZ);
  65. if (t > 0) {
  66. val = readw(battery->base) & 0xfff;
  67. if (battery->pdata->info.voltage_max_design <= 2500000)
  68. val = (val * 78125UL) >> 7UL;
  69. else
  70. val = ((val * 924375UL) >> 9UL) + 33000;
  71. voltage = (long)val;
  72. } else {
  73. voltage = t ? t : -ETIMEDOUT;
  74. }
  75. battery->cell->disable(battery->pdev);
  76. disable_irq(battery->irq);
  77. mutex_unlock(&battery->lock);
  78. return voltage;
  79. }
  80. static int jz_battery_get_capacity(struct power_supply *psy)
  81. {
  82. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  83. struct power_supply_info *info = &jz_battery->pdata->info;
  84. long voltage;
  85. int ret;
  86. int voltage_span;
  87. voltage = jz_battery_read_voltage(jz_battery);
  88. if (voltage < 0)
  89. return voltage;
  90. voltage_span = info->voltage_max_design - info->voltage_min_design;
  91. ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
  92. if (ret > 100)
  93. ret = 100;
  94. else if (ret < 0)
  95. ret = 0;
  96. return ret;
  97. }
  98. static int jz_battery_get_property(struct power_supply *psy,
  99. enum power_supply_property psp, union power_supply_propval *val)
  100. {
  101. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  102. struct power_supply_info *info = &jz_battery->pdata->info;
  103. long voltage;
  104. switch (psp) {
  105. case POWER_SUPPLY_PROP_STATUS:
  106. val->intval = jz_battery->status;
  107. break;
  108. case POWER_SUPPLY_PROP_TECHNOLOGY:
  109. val->intval = jz_battery->pdata->info.technology;
  110. break;
  111. case POWER_SUPPLY_PROP_HEALTH:
  112. voltage = jz_battery_read_voltage(jz_battery);
  113. if (voltage < info->voltage_min_design)
  114. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  115. else
  116. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  117. break;
  118. case POWER_SUPPLY_PROP_CAPACITY:
  119. val->intval = jz_battery_get_capacity(psy);
  120. break;
  121. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  122. val->intval = jz_battery_read_voltage(jz_battery);
  123. if (val->intval < 0)
  124. return val->intval;
  125. break;
  126. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  127. val->intval = info->voltage_max_design;
  128. break;
  129. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  130. val->intval = info->voltage_min_design;
  131. break;
  132. case POWER_SUPPLY_PROP_PRESENT:
  133. val->intval = 1;
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return 0;
  139. }
  140. static void jz_battery_external_power_changed(struct power_supply *psy)
  141. {
  142. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  143. mod_delayed_work(system_wq, &jz_battery->work, 0);
  144. }
  145. static irqreturn_t jz_battery_charge_irq(int irq, void *data)
  146. {
  147. struct jz_battery *jz_battery = data;
  148. mod_delayed_work(system_wq, &jz_battery->work, 0);
  149. return IRQ_HANDLED;
  150. }
  151. static void jz_battery_update(struct jz_battery *jz_battery)
  152. {
  153. int status;
  154. long voltage;
  155. bool has_changed = false;
  156. int is_charging;
  157. if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
  158. is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
  159. is_charging ^= jz_battery->pdata->gpio_charge_active_low;
  160. if (is_charging)
  161. status = POWER_SUPPLY_STATUS_CHARGING;
  162. else
  163. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  164. if (status != jz_battery->status) {
  165. jz_battery->status = status;
  166. has_changed = true;
  167. }
  168. }
  169. voltage = jz_battery_read_voltage(jz_battery);
  170. if (abs(voltage - jz_battery->voltage) < 50000) {
  171. jz_battery->voltage = voltage;
  172. has_changed = true;
  173. }
  174. if (has_changed)
  175. power_supply_changed(jz_battery->battery);
  176. }
  177. static enum power_supply_property jz_battery_properties[] = {
  178. POWER_SUPPLY_PROP_STATUS,
  179. POWER_SUPPLY_PROP_TECHNOLOGY,
  180. POWER_SUPPLY_PROP_HEALTH,
  181. POWER_SUPPLY_PROP_CAPACITY,
  182. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  183. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  184. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  185. POWER_SUPPLY_PROP_PRESENT,
  186. };
  187. static void jz_battery_work(struct work_struct *work)
  188. {
  189. /* Too small interval will increase system workload */
  190. const int interval = HZ * 30;
  191. struct jz_battery *jz_battery = container_of(work, struct jz_battery,
  192. work.work);
  193. jz_battery_update(jz_battery);
  194. schedule_delayed_work(&jz_battery->work, interval);
  195. }
  196. static int jz_battery_probe(struct platform_device *pdev)
  197. {
  198. int ret = 0;
  199. struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
  200. struct power_supply_config psy_cfg = {};
  201. struct jz_battery *jz_battery;
  202. struct power_supply_desc *battery_desc;
  203. struct resource *mem;
  204. if (!pdata) {
  205. dev_err(&pdev->dev, "No platform_data supplied\n");
  206. return -ENXIO;
  207. }
  208. jz_battery = devm_kzalloc(&pdev->dev, sizeof(*jz_battery), GFP_KERNEL);
  209. if (!jz_battery) {
  210. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  211. return -ENOMEM;
  212. }
  213. jz_battery->cell = mfd_get_cell(pdev);
  214. jz_battery->irq = platform_get_irq(pdev, 0);
  215. if (jz_battery->irq < 0) {
  216. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  217. return jz_battery->irq;
  218. }
  219. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  220. jz_battery->base = devm_ioremap_resource(&pdev->dev, mem);
  221. if (IS_ERR(jz_battery->base))
  222. return PTR_ERR(jz_battery->base);
  223. battery_desc = &jz_battery->battery_desc;
  224. battery_desc->name = pdata->info.name;
  225. battery_desc->type = POWER_SUPPLY_TYPE_BATTERY;
  226. battery_desc->properties = jz_battery_properties;
  227. battery_desc->num_properties = ARRAY_SIZE(jz_battery_properties);
  228. battery_desc->get_property = jz_battery_get_property;
  229. battery_desc->external_power_changed =
  230. jz_battery_external_power_changed;
  231. battery_desc->use_for_apm = 1;
  232. psy_cfg.drv_data = jz_battery;
  233. jz_battery->pdata = pdata;
  234. jz_battery->pdev = pdev;
  235. init_completion(&jz_battery->read_completion);
  236. mutex_init(&jz_battery->lock);
  237. INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
  238. ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
  239. jz_battery);
  240. if (ret) {
  241. dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
  242. return ret;
  243. }
  244. disable_irq(jz_battery->irq);
  245. if (gpio_is_valid(pdata->gpio_charge)) {
  246. ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
  247. if (ret) {
  248. dev_err(&pdev->dev, "charger state gpio request failed.\n");
  249. goto err_free_irq;
  250. }
  251. ret = gpio_direction_input(pdata->gpio_charge);
  252. if (ret) {
  253. dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
  254. goto err_free_gpio;
  255. }
  256. jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
  257. if (jz_battery->charge_irq >= 0) {
  258. ret = request_irq(jz_battery->charge_irq,
  259. jz_battery_charge_irq,
  260. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  261. dev_name(&pdev->dev), jz_battery);
  262. if (ret) {
  263. dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
  264. goto err_free_gpio;
  265. }
  266. }
  267. } else {
  268. jz_battery->charge_irq = -1;
  269. }
  270. if (jz_battery->pdata->info.voltage_max_design <= 2500000)
  271. jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
  272. JZ_ADC_CONFIG_BAT_MB);
  273. else
  274. jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
  275. jz_battery->battery = power_supply_register(&pdev->dev, battery_desc,
  276. &psy_cfg);
  277. if (IS_ERR(jz_battery->battery)) {
  278. dev_err(&pdev->dev, "power supply battery register failed.\n");
  279. ret = PTR_ERR(jz_battery->battery);
  280. goto err_free_charge_irq;
  281. }
  282. platform_set_drvdata(pdev, jz_battery);
  283. schedule_delayed_work(&jz_battery->work, 0);
  284. return 0;
  285. err_free_charge_irq:
  286. if (jz_battery->charge_irq >= 0)
  287. free_irq(jz_battery->charge_irq, jz_battery);
  288. err_free_gpio:
  289. if (gpio_is_valid(pdata->gpio_charge))
  290. gpio_free(jz_battery->pdata->gpio_charge);
  291. err_free_irq:
  292. free_irq(jz_battery->irq, jz_battery);
  293. return ret;
  294. }
  295. static int jz_battery_remove(struct platform_device *pdev)
  296. {
  297. struct jz_battery *jz_battery = platform_get_drvdata(pdev);
  298. cancel_delayed_work_sync(&jz_battery->work);
  299. if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
  300. if (jz_battery->charge_irq >= 0)
  301. free_irq(jz_battery->charge_irq, jz_battery);
  302. gpio_free(jz_battery->pdata->gpio_charge);
  303. }
  304. power_supply_unregister(jz_battery->battery);
  305. free_irq(jz_battery->irq, jz_battery);
  306. return 0;
  307. }
  308. #ifdef CONFIG_PM
  309. static int jz_battery_suspend(struct device *dev)
  310. {
  311. struct jz_battery *jz_battery = dev_get_drvdata(dev);
  312. cancel_delayed_work_sync(&jz_battery->work);
  313. jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
  314. return 0;
  315. }
  316. static int jz_battery_resume(struct device *dev)
  317. {
  318. struct jz_battery *jz_battery = dev_get_drvdata(dev);
  319. schedule_delayed_work(&jz_battery->work, 0);
  320. return 0;
  321. }
  322. static const struct dev_pm_ops jz_battery_pm_ops = {
  323. .suspend = jz_battery_suspend,
  324. .resume = jz_battery_resume,
  325. };
  326. #define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
  327. #else
  328. #define JZ_BATTERY_PM_OPS NULL
  329. #endif
  330. static struct platform_driver jz_battery_driver = {
  331. .probe = jz_battery_probe,
  332. .remove = jz_battery_remove,
  333. .driver = {
  334. .name = "jz4740-battery",
  335. .pm = JZ_BATTERY_PM_OPS,
  336. },
  337. };
  338. module_platform_driver(jz_battery_driver);
  339. MODULE_ALIAS("platform:jz4740-battery");
  340. MODULE_LICENSE("GPL");
  341. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  342. MODULE_DESCRIPTION("JZ4740 SoC battery driver");