jz4740-battery.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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/gpio.h>
  24. #include <linux/mfd/core.h>
  25. #include <linux/power_supply.h>
  26. #include <linux/power/jz4740-battery.h>
  27. #include <linux/jz4740-adc.h>
  28. struct jz_battery {
  29. struct jz_battery_platform_data *pdata;
  30. struct platform_device *pdev;
  31. struct resource *mem;
  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 delayed_work work;
  41. struct mutex lock;
  42. };
  43. static inline struct jz_battery *psy_to_jz_battery(struct power_supply *psy)
  44. {
  45. return container_of(psy, struct jz_battery, battery);
  46. }
  47. static irqreturn_t jz_battery_irq_handler(int irq, void *devid)
  48. {
  49. struct jz_battery *battery = devid;
  50. complete(&battery->read_completion);
  51. return IRQ_HANDLED;
  52. }
  53. static long jz_battery_read_voltage(struct jz_battery *battery)
  54. {
  55. unsigned long t;
  56. unsigned long val;
  57. long voltage;
  58. mutex_lock(&battery->lock);
  59. INIT_COMPLETION(battery->read_completion);
  60. enable_irq(battery->irq);
  61. battery->cell->enable(battery->pdev);
  62. t = wait_for_completion_interruptible_timeout(&battery->read_completion,
  63. HZ);
  64. if (t > 0) {
  65. val = readw(battery->base) & 0xfff;
  66. if (battery->pdata->info.voltage_max_design <= 2500000)
  67. val = (val * 78125UL) >> 7UL;
  68. else
  69. val = ((val * 924375UL) >> 9UL) + 33000;
  70. voltage = (long)val;
  71. } else {
  72. voltage = t ? t : -ETIMEDOUT;
  73. }
  74. battery->cell->disable(battery->pdev);
  75. disable_irq(battery->irq);
  76. mutex_unlock(&battery->lock);
  77. return voltage;
  78. }
  79. static int jz_battery_get_capacity(struct power_supply *psy)
  80. {
  81. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  82. struct power_supply_info *info = &jz_battery->pdata->info;
  83. long voltage;
  84. int ret;
  85. int voltage_span;
  86. voltage = jz_battery_read_voltage(jz_battery);
  87. if (voltage < 0)
  88. return voltage;
  89. voltage_span = info->voltage_max_design - info->voltage_min_design;
  90. ret = ((voltage - info->voltage_min_design) * 100) / voltage_span;
  91. if (ret > 100)
  92. ret = 100;
  93. else if (ret < 0)
  94. ret = 0;
  95. return ret;
  96. }
  97. static int jz_battery_get_property(struct power_supply *psy,
  98. enum power_supply_property psp, union power_supply_propval *val)
  99. {
  100. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  101. struct power_supply_info *info = &jz_battery->pdata->info;
  102. long voltage;
  103. switch (psp) {
  104. case POWER_SUPPLY_PROP_STATUS:
  105. val->intval = jz_battery->status;
  106. break;
  107. case POWER_SUPPLY_PROP_TECHNOLOGY:
  108. val->intval = jz_battery->pdata->info.technology;
  109. break;
  110. case POWER_SUPPLY_PROP_HEALTH:
  111. voltage = jz_battery_read_voltage(jz_battery);
  112. if (voltage < info->voltage_min_design)
  113. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  114. else
  115. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  116. break;
  117. case POWER_SUPPLY_PROP_CAPACITY:
  118. val->intval = jz_battery_get_capacity(psy);
  119. break;
  120. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  121. val->intval = jz_battery_read_voltage(jz_battery);
  122. if (val->intval < 0)
  123. return val->intval;
  124. break;
  125. case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
  126. val->intval = info->voltage_max_design;
  127. break;
  128. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  129. val->intval = info->voltage_min_design;
  130. break;
  131. case POWER_SUPPLY_PROP_PRESENT:
  132. val->intval = 1;
  133. break;
  134. default:
  135. return -EINVAL;
  136. }
  137. return 0;
  138. }
  139. static void jz_battery_external_power_changed(struct power_supply *psy)
  140. {
  141. struct jz_battery *jz_battery = psy_to_jz_battery(psy);
  142. cancel_delayed_work(&jz_battery->work);
  143. schedule_delayed_work(&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. cancel_delayed_work(&jz_battery->work);
  149. schedule_delayed_work(&jz_battery->work, 0);
  150. return IRQ_HANDLED;
  151. }
  152. static void jz_battery_update(struct jz_battery *jz_battery)
  153. {
  154. int status;
  155. long voltage;
  156. bool has_changed = false;
  157. int is_charging;
  158. if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
  159. is_charging = gpio_get_value(jz_battery->pdata->gpio_charge);
  160. is_charging ^= jz_battery->pdata->gpio_charge_active_low;
  161. if (is_charging)
  162. status = POWER_SUPPLY_STATUS_CHARGING;
  163. else
  164. status = POWER_SUPPLY_STATUS_NOT_CHARGING;
  165. if (status != jz_battery->status) {
  166. jz_battery->status = status;
  167. has_changed = true;
  168. }
  169. }
  170. voltage = jz_battery_read_voltage(jz_battery);
  171. if (abs(voltage - jz_battery->voltage) < 50000) {
  172. jz_battery->voltage = voltage;
  173. has_changed = true;
  174. }
  175. if (has_changed)
  176. power_supply_changed(&jz_battery->battery);
  177. }
  178. static enum power_supply_property jz_battery_properties[] = {
  179. POWER_SUPPLY_PROP_STATUS,
  180. POWER_SUPPLY_PROP_TECHNOLOGY,
  181. POWER_SUPPLY_PROP_HEALTH,
  182. POWER_SUPPLY_PROP_CAPACITY,
  183. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  184. POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
  185. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  186. POWER_SUPPLY_PROP_PRESENT,
  187. };
  188. static void jz_battery_work(struct work_struct *work)
  189. {
  190. /* Too small interval will increase system workload */
  191. const int interval = HZ * 30;
  192. struct jz_battery *jz_battery = container_of(work, struct jz_battery,
  193. work.work);
  194. jz_battery_update(jz_battery);
  195. schedule_delayed_work(&jz_battery->work, interval);
  196. }
  197. static int __devinit jz_battery_probe(struct platform_device *pdev)
  198. {
  199. int ret = 0;
  200. struct jz_battery_platform_data *pdata = pdev->dev.parent->platform_data;
  201. struct jz_battery *jz_battery;
  202. struct power_supply *battery;
  203. if (!pdata) {
  204. dev_err(&pdev->dev, "No platform_data supplied\n");
  205. return -ENXIO;
  206. }
  207. jz_battery = kzalloc(sizeof(*jz_battery), GFP_KERNEL);
  208. if (!jz_battery) {
  209. dev_err(&pdev->dev, "Failed to allocate driver structure\n");
  210. return -ENOMEM;
  211. }
  212. jz_battery->cell = mfd_get_cell(pdev);
  213. jz_battery->irq = platform_get_irq(pdev, 0);
  214. if (jz_battery->irq < 0) {
  215. ret = jz_battery->irq;
  216. dev_err(&pdev->dev, "Failed to get platform irq: %d\n", ret);
  217. goto err_free;
  218. }
  219. jz_battery->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  220. if (!jz_battery->mem) {
  221. ret = -ENOENT;
  222. dev_err(&pdev->dev, "Failed to get platform mmio resource\n");
  223. goto err_free;
  224. }
  225. jz_battery->mem = request_mem_region(jz_battery->mem->start,
  226. resource_size(jz_battery->mem), pdev->name);
  227. if (!jz_battery->mem) {
  228. ret = -EBUSY;
  229. dev_err(&pdev->dev, "Failed to request mmio memory region\n");
  230. goto err_free;
  231. }
  232. jz_battery->base = ioremap_nocache(jz_battery->mem->start,
  233. resource_size(jz_battery->mem));
  234. if (!jz_battery->base) {
  235. ret = -EBUSY;
  236. dev_err(&pdev->dev, "Failed to ioremap mmio memory\n");
  237. goto err_release_mem_region;
  238. }
  239. battery = &jz_battery->battery;
  240. battery->name = pdata->info.name;
  241. battery->type = POWER_SUPPLY_TYPE_BATTERY;
  242. battery->properties = jz_battery_properties;
  243. battery->num_properties = ARRAY_SIZE(jz_battery_properties);
  244. battery->get_property = jz_battery_get_property;
  245. battery->external_power_changed = jz_battery_external_power_changed;
  246. battery->use_for_apm = 1;
  247. jz_battery->pdata = pdata;
  248. jz_battery->pdev = pdev;
  249. init_completion(&jz_battery->read_completion);
  250. mutex_init(&jz_battery->lock);
  251. INIT_DELAYED_WORK(&jz_battery->work, jz_battery_work);
  252. ret = request_irq(jz_battery->irq, jz_battery_irq_handler, 0, pdev->name,
  253. jz_battery);
  254. if (ret) {
  255. dev_err(&pdev->dev, "Failed to request irq %d\n", ret);
  256. goto err_iounmap;
  257. }
  258. disable_irq(jz_battery->irq);
  259. if (gpio_is_valid(pdata->gpio_charge)) {
  260. ret = gpio_request(pdata->gpio_charge, dev_name(&pdev->dev));
  261. if (ret) {
  262. dev_err(&pdev->dev, "charger state gpio request failed.\n");
  263. goto err_free_irq;
  264. }
  265. ret = gpio_direction_input(pdata->gpio_charge);
  266. if (ret) {
  267. dev_err(&pdev->dev, "charger state gpio set direction failed.\n");
  268. goto err_free_gpio;
  269. }
  270. jz_battery->charge_irq = gpio_to_irq(pdata->gpio_charge);
  271. if (jz_battery->charge_irq >= 0) {
  272. ret = request_irq(jz_battery->charge_irq,
  273. jz_battery_charge_irq,
  274. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  275. dev_name(&pdev->dev), jz_battery);
  276. if (ret) {
  277. dev_err(&pdev->dev, "Failed to request charge irq: %d\n", ret);
  278. goto err_free_gpio;
  279. }
  280. }
  281. } else {
  282. jz_battery->charge_irq = -1;
  283. }
  284. if (jz_battery->pdata->info.voltage_max_design <= 2500000)
  285. jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB,
  286. JZ_ADC_CONFIG_BAT_MB);
  287. else
  288. jz4740_adc_set_config(pdev->dev.parent, JZ_ADC_CONFIG_BAT_MB, 0);
  289. ret = power_supply_register(&pdev->dev, &jz_battery->battery);
  290. if (ret) {
  291. dev_err(&pdev->dev, "power supply battery register failed.\n");
  292. goto err_free_charge_irq;
  293. }
  294. platform_set_drvdata(pdev, jz_battery);
  295. schedule_delayed_work(&jz_battery->work, 0);
  296. return 0;
  297. err_free_charge_irq:
  298. if (jz_battery->charge_irq >= 0)
  299. free_irq(jz_battery->charge_irq, jz_battery);
  300. err_free_gpio:
  301. if (gpio_is_valid(pdata->gpio_charge))
  302. gpio_free(jz_battery->pdata->gpio_charge);
  303. err_free_irq:
  304. free_irq(jz_battery->irq, jz_battery);
  305. err_iounmap:
  306. platform_set_drvdata(pdev, NULL);
  307. iounmap(jz_battery->base);
  308. err_release_mem_region:
  309. release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
  310. err_free:
  311. kfree(jz_battery);
  312. return ret;
  313. }
  314. static int __devexit jz_battery_remove(struct platform_device *pdev)
  315. {
  316. struct jz_battery *jz_battery = platform_get_drvdata(pdev);
  317. cancel_delayed_work_sync(&jz_battery->work);
  318. if (gpio_is_valid(jz_battery->pdata->gpio_charge)) {
  319. if (jz_battery->charge_irq >= 0)
  320. free_irq(jz_battery->charge_irq, jz_battery);
  321. gpio_free(jz_battery->pdata->gpio_charge);
  322. }
  323. power_supply_unregister(&jz_battery->battery);
  324. free_irq(jz_battery->irq, jz_battery);
  325. iounmap(jz_battery->base);
  326. release_mem_region(jz_battery->mem->start, resource_size(jz_battery->mem));
  327. kfree(jz_battery);
  328. return 0;
  329. }
  330. #ifdef CONFIG_PM
  331. static int jz_battery_suspend(struct device *dev)
  332. {
  333. struct jz_battery *jz_battery = dev_get_drvdata(dev);
  334. cancel_delayed_work_sync(&jz_battery->work);
  335. jz_battery->status = POWER_SUPPLY_STATUS_UNKNOWN;
  336. return 0;
  337. }
  338. static int jz_battery_resume(struct device *dev)
  339. {
  340. struct jz_battery *jz_battery = dev_get_drvdata(dev);
  341. schedule_delayed_work(&jz_battery->work, 0);
  342. return 0;
  343. }
  344. static const struct dev_pm_ops jz_battery_pm_ops = {
  345. .suspend = jz_battery_suspend,
  346. .resume = jz_battery_resume,
  347. };
  348. #define JZ_BATTERY_PM_OPS (&jz_battery_pm_ops)
  349. #else
  350. #define JZ_BATTERY_PM_OPS NULL
  351. #endif
  352. static struct platform_driver jz_battery_driver = {
  353. .probe = jz_battery_probe,
  354. .remove = __devexit_p(jz_battery_remove),
  355. .driver = {
  356. .name = "jz4740-battery",
  357. .owner = THIS_MODULE,
  358. .pm = JZ_BATTERY_PM_OPS,
  359. },
  360. };
  361. static int __init jz_battery_init(void)
  362. {
  363. return platform_driver_register(&jz_battery_driver);
  364. }
  365. module_init(jz_battery_init);
  366. static void __exit jz_battery_exit(void)
  367. {
  368. platform_driver_unregister(&jz_battery_driver);
  369. }
  370. module_exit(jz_battery_exit);
  371. MODULE_ALIAS("platform:jz4740-battery");
  372. MODULE_LICENSE("GPL");
  373. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  374. MODULE_DESCRIPTION("JZ4740 SoC battery driver");