pcf50633-charger.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. static ssize_t
  92. show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
  93. {
  94. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  95. u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  96. u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  97. return sprintf(buf, "%d\n", chgmod);
  98. }
  99. static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
  100. static ssize_t
  101. show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
  102. {
  103. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  104. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  105. PCF50633_MBCC7_USB_MASK;
  106. unsigned int ma;
  107. if (usblim == PCF50633_MBCC7_USB_1000mA)
  108. ma = 1000;
  109. else if (usblim == PCF50633_MBCC7_USB_500mA)
  110. ma = 500;
  111. else if (usblim == PCF50633_MBCC7_USB_100mA)
  112. ma = 100;
  113. else
  114. ma = 0;
  115. return sprintf(buf, "%u\n", ma);
  116. }
  117. static ssize_t set_usblim(struct device *dev,
  118. struct device_attribute *attr, const char *buf, size_t count)
  119. {
  120. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  121. unsigned long ma;
  122. int ret;
  123. ret = strict_strtoul(buf, 10, &ma);
  124. if (ret)
  125. return -EINVAL;
  126. pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
  127. return count;
  128. }
  129. static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
  130. static struct attribute *pcf50633_mbc_sysfs_entries[] = {
  131. &dev_attr_chgmode.attr,
  132. &dev_attr_usb_curlim.attr,
  133. NULL,
  134. };
  135. static struct attribute_group mbc_attr_group = {
  136. .name = NULL, /* put in device directory */
  137. .attrs = pcf50633_mbc_sysfs_entries,
  138. };
  139. /* MBC state machine switches into charging mode when the battery voltage
  140. * falls below 96% of a battery float voltage. But the voltage drop in Li-ion
  141. * batteries is marginal(1~2 %) till about 80% of its capacity - which means,
  142. * after a BATFULL, charging won't be restarted until 80%.
  143. *
  144. * This work_struct function restarts charging at regular intervals to make
  145. * sure we don't discharge too much
  146. */
  147. static void pcf50633_mbc_charging_restart(struct work_struct *work)
  148. {
  149. struct pcf50633_mbc *mbc;
  150. u8 mbcs2, chgmod;
  151. mbc = container_of(work, struct pcf50633_mbc,
  152. charging_restart_work.work);
  153. mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  154. chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  155. if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
  156. return;
  157. /* Restart charging */
  158. pcf50633_reg_set_bit_mask(mbc->pcf, PCF50633_REG_MBCC1,
  159. PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
  160. mbc->usb_active = 1;
  161. power_supply_changed(&mbc->usb);
  162. dev_info(mbc->pcf->dev, "Charging restarted\n");
  163. }
  164. static void
  165. pcf50633_mbc_irq_handler(int irq, void *data)
  166. {
  167. struct pcf50633_mbc *mbc = data;
  168. int chg_restart_interval =
  169. mbc->pcf->pdata->charging_restart_interval;
  170. /* USB */
  171. if (irq == PCF50633_IRQ_USBINS) {
  172. mbc->usb_online = 1;
  173. } else if (irq == PCF50633_IRQ_USBREM) {
  174. mbc->usb_online = 0;
  175. mbc->usb_active = 0;
  176. pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
  177. cancel_delayed_work_sync(&mbc->charging_restart_work);
  178. }
  179. /* Adapter */
  180. if (irq == PCF50633_IRQ_ADPINS) {
  181. mbc->adapter_online = 1;
  182. mbc->adapter_active = 1;
  183. } else if (irq == PCF50633_IRQ_ADPREM) {
  184. mbc->adapter_online = 0;
  185. mbc->adapter_active = 0;
  186. }
  187. if (irq == PCF50633_IRQ_BATFULL) {
  188. mbc->usb_active = 0;
  189. mbc->adapter_active = 0;
  190. if (chg_restart_interval > 0)
  191. schedule_delayed_work(&mbc->charging_restart_work,
  192. chg_restart_interval);
  193. } else if (irq == PCF50633_IRQ_USBLIMON)
  194. mbc->usb_active = 0;
  195. else if (irq == PCF50633_IRQ_USBLIMOFF)
  196. mbc->usb_active = 1;
  197. power_supply_changed(&mbc->usb);
  198. power_supply_changed(&mbc->adapter);
  199. if (mbc->pcf->pdata->mbc_event_callback)
  200. mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
  201. }
  202. static int adapter_get_property(struct power_supply *psy,
  203. enum power_supply_property psp,
  204. union power_supply_propval *val)
  205. {
  206. struct pcf50633_mbc *mbc = container_of(psy,
  207. struct pcf50633_mbc, adapter);
  208. int ret = 0;
  209. switch (psp) {
  210. case POWER_SUPPLY_PROP_ONLINE:
  211. val->intval = mbc->adapter_online;
  212. break;
  213. default:
  214. ret = -EINVAL;
  215. break;
  216. }
  217. return ret;
  218. }
  219. static int usb_get_property(struct power_supply *psy,
  220. enum power_supply_property psp,
  221. union power_supply_propval *val)
  222. {
  223. struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
  224. int ret = 0;
  225. switch (psp) {
  226. case POWER_SUPPLY_PROP_ONLINE:
  227. val->intval = mbc->usb_online;
  228. break;
  229. default:
  230. ret = -EINVAL;
  231. break;
  232. }
  233. return ret;
  234. }
  235. static enum power_supply_property power_props[] = {
  236. POWER_SUPPLY_PROP_ONLINE,
  237. };
  238. static const u8 mbc_irq_handlers[] = {
  239. PCF50633_IRQ_ADPINS,
  240. PCF50633_IRQ_ADPREM,
  241. PCF50633_IRQ_USBINS,
  242. PCF50633_IRQ_USBREM,
  243. PCF50633_IRQ_BATFULL,
  244. PCF50633_IRQ_CHGHALT,
  245. PCF50633_IRQ_THLIMON,
  246. PCF50633_IRQ_THLIMOFF,
  247. PCF50633_IRQ_USBLIMON,
  248. PCF50633_IRQ_USBLIMOFF,
  249. PCF50633_IRQ_LOWSYS,
  250. PCF50633_IRQ_LOWBAT,
  251. };
  252. static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
  253. {
  254. struct pcf50633_mbc *mbc;
  255. struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
  256. int ret;
  257. int i;
  258. u8 mbcs1;
  259. mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
  260. if (!mbc)
  261. return -ENOMEM;
  262. platform_set_drvdata(pdev, mbc);
  263. mbc->pcf = pdata->pcf;
  264. /* Set up IRQ handlers */
  265. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  266. pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
  267. pcf50633_mbc_irq_handler, mbc);
  268. /* Create power supplies */
  269. mbc->adapter.name = "adapter";
  270. mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
  271. mbc->adapter.properties = power_props;
  272. mbc->adapter.num_properties = ARRAY_SIZE(power_props);
  273. mbc->adapter.get_property = &adapter_get_property;
  274. mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
  275. mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
  276. mbc->usb.name = "usb";
  277. mbc->usb.type = POWER_SUPPLY_TYPE_USB;
  278. mbc->usb.properties = power_props;
  279. mbc->usb.num_properties = ARRAY_SIZE(power_props);
  280. mbc->usb.get_property = usb_get_property;
  281. mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
  282. mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
  283. ret = power_supply_register(&pdev->dev, &mbc->adapter);
  284. if (ret) {
  285. dev_err(mbc->pcf->dev, "failed to register adapter\n");
  286. kfree(mbc);
  287. return ret;
  288. }
  289. ret = power_supply_register(&pdev->dev, &mbc->usb);
  290. if (ret) {
  291. dev_err(mbc->pcf->dev, "failed to register usb\n");
  292. power_supply_unregister(&mbc->adapter);
  293. kfree(mbc);
  294. return ret;
  295. }
  296. INIT_DELAYED_WORK(&mbc->charging_restart_work,
  297. pcf50633_mbc_charging_restart);
  298. ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
  299. if (ret)
  300. dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
  301. mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
  302. if (mbcs1 & PCF50633_MBCS1_USBPRES)
  303. pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
  304. if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
  305. pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
  306. return 0;
  307. }
  308. static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
  309. {
  310. struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
  311. int i;
  312. /* Remove IRQ handlers */
  313. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  314. pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
  315. power_supply_unregister(&mbc->usb);
  316. power_supply_unregister(&mbc->adapter);
  317. cancel_delayed_work_sync(&mbc->charging_restart_work);
  318. kfree(mbc);
  319. return 0;
  320. }
  321. static struct platform_driver pcf50633_mbc_driver = {
  322. .driver = {
  323. .name = "pcf50633-mbc",
  324. },
  325. .probe = pcf50633_mbc_probe,
  326. .remove = __devexit_p(pcf50633_mbc_remove),
  327. };
  328. static int __init pcf50633_mbc_init(void)
  329. {
  330. return platform_driver_register(&pcf50633_mbc_driver);
  331. }
  332. module_init(pcf50633_mbc_init);
  333. static void __exit pcf50633_mbc_exit(void)
  334. {
  335. platform_driver_unregister(&pcf50633_mbc_driver);
  336. }
  337. module_exit(pcf50633_mbc_exit);
  338. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  339. MODULE_DESCRIPTION("PCF50633 mbc driver");
  340. MODULE_LICENSE("GPL");
  341. MODULE_ALIAS("platform:pcf50633-mbc");