pcf50633-charger.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* NXP PCF50633 Main Battery Charger Driver
  2. *
  3. * (C) 2006-2008 by Openmoko, Inc.
  4. * Author: Balaji Rao <balajirrao@openmoko.org>
  5. * All rights reserved.
  6. *
  7. * Broken down from monstrous PCF50633 driver mainly by
  8. * Harald Welte, Andy Green and Werner Almesberger
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/types.h>
  20. #include <linux/device.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/power_supply.h>
  24. #include <linux/mfd/pcf50633/core.h>
  25. #include <linux/mfd/pcf50633/mbc.h>
  26. struct pcf50633_mbc {
  27. struct pcf50633 *pcf;
  28. int adapter_active;
  29. int adapter_online;
  30. int usb_active;
  31. int usb_online;
  32. struct power_supply usb;
  33. struct power_supply adapter;
  34. struct delayed_work charging_restart_work;
  35. };
  36. int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
  37. {
  38. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  39. int ret = 0;
  40. u8 bits;
  41. int charging_start = 1;
  42. u8 mbcs2, chgmod;
  43. if (ma >= 1000)
  44. bits = PCF50633_MBCC7_USB_1000mA;
  45. else if (ma >= 500)
  46. bits = PCF50633_MBCC7_USB_500mA;
  47. else if (ma >= 100)
  48. bits = PCF50633_MBCC7_USB_100mA;
  49. else {
  50. bits = PCF50633_MBCC7_USB_SUSPEND;
  51. charging_start = 0;
  52. }
  53. ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
  54. PCF50633_MBCC7_USB_MASK, bits);
  55. if (ret)
  56. dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
  57. else
  58. dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
  59. /* Manual charging start */
  60. mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
  61. chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  62. /* If chgmod == BATFULL, setting chgena has no effect.
  63. * We need to set resume instead.
  64. */
  65. if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
  66. pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
  67. PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
  68. else
  69. pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
  70. PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
  71. mbc->usb_active = charging_start;
  72. power_supply_changed(&mbc->usb);
  73. return ret;
  74. }
  75. EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
  76. int pcf50633_mbc_get_status(struct pcf50633 *pcf)
  77. {
  78. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  79. int status = 0;
  80. if (mbc->usb_online)
  81. status |= PCF50633_MBC_USB_ONLINE;
  82. if (mbc->usb_active)
  83. status |= PCF50633_MBC_USB_ACTIVE;
  84. if (mbc->adapter_online)
  85. status |= PCF50633_MBC_ADAPTER_ONLINE;
  86. if (mbc->adapter_active)
  87. status |= PCF50633_MBC_ADAPTER_ACTIVE;
  88. return status;
  89. }
  90. EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
  91. void pcf50633_mbc_set_status(struct pcf50633 *pcf, int what, int status)
  92. {
  93. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  94. if (what & PCF50633_MBC_USB_ONLINE)
  95. mbc->usb_online = !!status;
  96. if (what & PCF50633_MBC_USB_ACTIVE)
  97. mbc->usb_active = !!status;
  98. if (what & PCF50633_MBC_ADAPTER_ONLINE)
  99. mbc->adapter_online = !!status;
  100. if (what & PCF50633_MBC_ADAPTER_ACTIVE)
  101. mbc->adapter_active = !!status;
  102. }
  103. EXPORT_SYMBOL_GPL(pcf50633_mbc_set_status);
  104. static ssize_t
  105. show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
  106. {
  107. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  108. u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  109. u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  110. return sprintf(buf, "%d\n", chgmod);
  111. }
  112. static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
  113. static ssize_t
  114. show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
  115. {
  116. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  117. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  118. PCF50633_MBCC7_USB_MASK;
  119. unsigned int ma;
  120. if (usblim == PCF50633_MBCC7_USB_1000mA)
  121. ma = 1000;
  122. else if (usblim == PCF50633_MBCC7_USB_500mA)
  123. ma = 500;
  124. else if (usblim == PCF50633_MBCC7_USB_100mA)
  125. ma = 100;
  126. else
  127. ma = 0;
  128. return sprintf(buf, "%u\n", ma);
  129. }
  130. static ssize_t set_usblim(struct device *dev,
  131. struct device_attribute *attr, const char *buf, size_t count)
  132. {
  133. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  134. unsigned long ma;
  135. int ret;
  136. ret = strict_strtoul(buf, 10, &ma);
  137. if (ret)
  138. return -EINVAL;
  139. pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
  140. return count;
  141. }
  142. static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
  143. static struct attribute *pcf50633_mbc_sysfs_entries[] = {
  144. &dev_attr_chgmode.attr,
  145. &dev_attr_usb_curlim.attr,
  146. NULL,
  147. };
  148. static struct attribute_group mbc_attr_group = {
  149. .name = NULL, /* put in device directory */
  150. .attrs = pcf50633_mbc_sysfs_entries,
  151. };
  152. /* MBC state machine switches into charging mode when the battery voltage
  153. * falls below 96% of a battery float voltage. But the voltage drop in Li-ion
  154. * batteries is marginal(1~2 %) till about 80% of its capacity - which means,
  155. * after a BATFULL, charging won't be restarted until 80%.
  156. *
  157. * This work_struct function restarts charging at regular intervals to make
  158. * sure we don't discharge too much
  159. */
  160. static void pcf50633_mbc_charging_restart(struct work_struct *work)
  161. {
  162. struct pcf50633_mbc *mbc;
  163. u8 mbcs2, chgmod;
  164. mbc = container_of(work, struct pcf50633_mbc,
  165. charging_restart_work.work);
  166. mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  167. chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  168. if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
  169. return;
  170. /* Restart charging */
  171. pcf50633_reg_set_bit_mask(mbc->pcf, PCF50633_REG_MBCC1,
  172. PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
  173. mbc->usb_active = 1;
  174. power_supply_changed(&mbc->usb);
  175. dev_info(mbc->pcf->dev, "Charging restarted\n");
  176. }
  177. static void
  178. pcf50633_mbc_irq_handler(int irq, void *data)
  179. {
  180. struct pcf50633_mbc *mbc = data;
  181. int chg_restart_interval =
  182. mbc->pcf->pdata->charging_restart_interval;
  183. /* USB */
  184. if (irq == PCF50633_IRQ_USBINS) {
  185. mbc->usb_online = 1;
  186. } else if (irq == PCF50633_IRQ_USBREM) {
  187. mbc->usb_online = 0;
  188. mbc->usb_active = 0;
  189. pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
  190. cancel_delayed_work_sync(&mbc->charging_restart_work);
  191. }
  192. /* Adapter */
  193. if (irq == PCF50633_IRQ_ADPINS) {
  194. mbc->adapter_online = 1;
  195. mbc->adapter_active = 1;
  196. } else if (irq == PCF50633_IRQ_ADPREM) {
  197. mbc->adapter_online = 0;
  198. mbc->adapter_active = 0;
  199. }
  200. if (irq == PCF50633_IRQ_BATFULL) {
  201. mbc->usb_active = 0;
  202. mbc->adapter_active = 0;
  203. if (chg_restart_interval > 0)
  204. schedule_delayed_work(&mbc->charging_restart_work,
  205. chg_restart_interval);
  206. } else if (irq == PCF50633_IRQ_USBLIMON)
  207. mbc->usb_active = 0;
  208. else if (irq == PCF50633_IRQ_USBLIMOFF)
  209. mbc->usb_active = 1;
  210. power_supply_changed(&mbc->usb);
  211. power_supply_changed(&mbc->adapter);
  212. if (mbc->pcf->pdata->mbc_event_callback)
  213. mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
  214. }
  215. static int adapter_get_property(struct power_supply *psy,
  216. enum power_supply_property psp,
  217. union power_supply_propval *val)
  218. {
  219. struct pcf50633_mbc *mbc = container_of(psy,
  220. struct pcf50633_mbc, adapter);
  221. int ret = 0;
  222. switch (psp) {
  223. case POWER_SUPPLY_PROP_ONLINE:
  224. val->intval = mbc->adapter_online;
  225. break;
  226. default:
  227. ret = -EINVAL;
  228. break;
  229. }
  230. return ret;
  231. }
  232. static int usb_get_property(struct power_supply *psy,
  233. enum power_supply_property psp,
  234. union power_supply_propval *val)
  235. {
  236. struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
  237. int ret = 0;
  238. switch (psp) {
  239. case POWER_SUPPLY_PROP_ONLINE:
  240. val->intval = mbc->usb_online;
  241. break;
  242. default:
  243. ret = -EINVAL;
  244. break;
  245. }
  246. return ret;
  247. }
  248. static enum power_supply_property power_props[] = {
  249. POWER_SUPPLY_PROP_ONLINE,
  250. };
  251. static const u8 mbc_irq_handlers[] = {
  252. PCF50633_IRQ_ADPINS,
  253. PCF50633_IRQ_ADPREM,
  254. PCF50633_IRQ_USBINS,
  255. PCF50633_IRQ_USBREM,
  256. PCF50633_IRQ_BATFULL,
  257. PCF50633_IRQ_CHGHALT,
  258. PCF50633_IRQ_THLIMON,
  259. PCF50633_IRQ_THLIMOFF,
  260. PCF50633_IRQ_USBLIMON,
  261. PCF50633_IRQ_USBLIMOFF,
  262. PCF50633_IRQ_LOWSYS,
  263. PCF50633_IRQ_LOWBAT,
  264. };
  265. static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
  266. {
  267. struct pcf50633_mbc *mbc;
  268. struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
  269. int ret;
  270. int i;
  271. u8 mbcs1;
  272. mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
  273. if (!mbc)
  274. return -ENOMEM;
  275. platform_set_drvdata(pdev, mbc);
  276. mbc->pcf = pdata->pcf;
  277. /* Set up IRQ handlers */
  278. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  279. pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
  280. pcf50633_mbc_irq_handler, mbc);
  281. /* Create power supplies */
  282. mbc->adapter.name = "adapter";
  283. mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
  284. mbc->adapter.properties = power_props;
  285. mbc->adapter.num_properties = ARRAY_SIZE(power_props);
  286. mbc->adapter.get_property = &adapter_get_property;
  287. mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
  288. mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
  289. mbc->usb.name = "usb";
  290. mbc->usb.type = POWER_SUPPLY_TYPE_USB;
  291. mbc->usb.properties = power_props;
  292. mbc->usb.num_properties = ARRAY_SIZE(power_props);
  293. mbc->usb.get_property = usb_get_property;
  294. mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
  295. mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
  296. ret = power_supply_register(&pdev->dev, &mbc->adapter);
  297. if (ret) {
  298. dev_err(mbc->pcf->dev, "failed to register adapter\n");
  299. kfree(mbc);
  300. return ret;
  301. }
  302. ret = power_supply_register(&pdev->dev, &mbc->usb);
  303. if (ret) {
  304. dev_err(mbc->pcf->dev, "failed to register usb\n");
  305. power_supply_unregister(&mbc->adapter);
  306. kfree(mbc);
  307. return ret;
  308. }
  309. INIT_DELAYED_WORK(&mbc->charging_restart_work,
  310. pcf50633_mbc_charging_restart);
  311. ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
  312. if (ret)
  313. dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
  314. mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
  315. if (mbcs1 & PCF50633_MBCS1_USBPRES)
  316. pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
  317. if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
  318. pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
  319. return 0;
  320. }
  321. static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
  322. {
  323. struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
  324. int i;
  325. /* Remove IRQ handlers */
  326. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  327. pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
  328. power_supply_unregister(&mbc->usb);
  329. power_supply_unregister(&mbc->adapter);
  330. cancel_delayed_work_sync(&mbc->charging_restart_work);
  331. kfree(mbc);
  332. return 0;
  333. }
  334. static struct platform_driver pcf50633_mbc_driver = {
  335. .driver = {
  336. .name = "pcf50633-mbc",
  337. },
  338. .probe = pcf50633_mbc_probe,
  339. .remove = __devexit_p(pcf50633_mbc_remove),
  340. };
  341. static int __init pcf50633_mbc_init(void)
  342. {
  343. return platform_driver_register(&pcf50633_mbc_driver);
  344. }
  345. module_init(pcf50633_mbc_init);
  346. static void __exit pcf50633_mbc_exit(void)
  347. {
  348. platform_driver_unregister(&pcf50633_mbc_driver);
  349. }
  350. module_exit(pcf50633_mbc_exit);
  351. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  352. MODULE_DESCRIPTION("PCF50633 mbc driver");
  353. MODULE_LICENSE("GPL");
  354. MODULE_ALIAS("platform:pcf50633-mbc");