pcf50633-charger.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 power_supply ac;
  35. struct delayed_work charging_restart_work;
  36. };
  37. int pcf50633_mbc_usb_curlim_set(struct pcf50633 *pcf, int ma)
  38. {
  39. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  40. int ret = 0;
  41. u8 bits;
  42. int charging_start = 1;
  43. u8 mbcs2, chgmod;
  44. unsigned int mbcc5;
  45. if (ma >= 1000) {
  46. bits = PCF50633_MBCC7_USB_1000mA;
  47. ma = 1000;
  48. } else if (ma >= 500) {
  49. bits = PCF50633_MBCC7_USB_500mA;
  50. ma = 500;
  51. } else if (ma >= 100) {
  52. bits = PCF50633_MBCC7_USB_100mA;
  53. ma = 100;
  54. } else {
  55. bits = PCF50633_MBCC7_USB_SUSPEND;
  56. charging_start = 0;
  57. ma = 0;
  58. }
  59. ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
  60. PCF50633_MBCC7_USB_MASK, bits);
  61. if (ret)
  62. dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
  63. else
  64. dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
  65. /*
  66. * We limit the charging current to be the USB current limit.
  67. * The reason is that on pcf50633, when it enters PMU Standby mode,
  68. * which it does when the device goes "off", the USB current limit
  69. * reverts to the variant default. In at least one common case, that
  70. * default is 500mA. By setting the charging current to be the same
  71. * as the USB limit we set here before PMU standby, we enforce it only
  72. * using the correct amount of current even when the USB current limit
  73. * gets reset to the wrong thing
  74. */
  75. if (mbc->pcf->pdata->charger_reference_current_ma) {
  76. mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
  77. if (mbcc5 > 255)
  78. mbcc5 = 255;
  79. pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
  80. }
  81. mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
  82. chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  83. /* If chgmod == BATFULL, setting chgena has no effect.
  84. * We need to set resume instead.
  85. */
  86. if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
  87. pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
  88. PCF50633_MBCC1_CHGENA, PCF50633_MBCC1_CHGENA);
  89. else
  90. pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1,
  91. PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
  92. mbc->usb_active = charging_start;
  93. power_supply_changed(&mbc->usb);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
  97. int pcf50633_mbc_get_status(struct pcf50633 *pcf)
  98. {
  99. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  100. int status = 0;
  101. if (mbc->usb_online)
  102. status |= PCF50633_MBC_USB_ONLINE;
  103. if (mbc->usb_active)
  104. status |= PCF50633_MBC_USB_ACTIVE;
  105. if (mbc->adapter_online)
  106. status |= PCF50633_MBC_ADAPTER_ONLINE;
  107. if (mbc->adapter_active)
  108. status |= PCF50633_MBC_ADAPTER_ACTIVE;
  109. return status;
  110. }
  111. EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
  112. static ssize_t
  113. show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
  114. {
  115. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  116. u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  117. u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  118. return sprintf(buf, "%d\n", chgmod);
  119. }
  120. static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
  121. static ssize_t
  122. show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
  123. {
  124. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  125. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  126. PCF50633_MBCC7_USB_MASK;
  127. unsigned int ma;
  128. if (usblim == PCF50633_MBCC7_USB_1000mA)
  129. ma = 1000;
  130. else if (usblim == PCF50633_MBCC7_USB_500mA)
  131. ma = 500;
  132. else if (usblim == PCF50633_MBCC7_USB_100mA)
  133. ma = 100;
  134. else
  135. ma = 0;
  136. return sprintf(buf, "%u\n", ma);
  137. }
  138. static ssize_t set_usblim(struct device *dev,
  139. struct device_attribute *attr, const char *buf, size_t count)
  140. {
  141. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  142. unsigned long ma;
  143. int ret;
  144. ret = strict_strtoul(buf, 10, &ma);
  145. if (ret)
  146. return -EINVAL;
  147. pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
  148. return count;
  149. }
  150. static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
  151. static ssize_t
  152. show_chglim(struct device *dev, struct device_attribute *attr, char *buf)
  153. {
  154. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  155. u8 mbcc5 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC5);
  156. unsigned int ma;
  157. if (!mbc->pcf->pdata->charger_reference_current_ma)
  158. return -ENODEV;
  159. ma = (mbc->pcf->pdata->charger_reference_current_ma * mbcc5) >> 8;
  160. return sprintf(buf, "%u\n", ma);
  161. }
  162. static ssize_t set_chglim(struct device *dev,
  163. struct device_attribute *attr, const char *buf, size_t count)
  164. {
  165. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  166. unsigned long ma;
  167. unsigned int mbcc5;
  168. int ret;
  169. if (!mbc->pcf->pdata->charger_reference_current_ma)
  170. return -ENODEV;
  171. ret = strict_strtoul(buf, 10, &ma);
  172. if (ret)
  173. return -EINVAL;
  174. mbcc5 = (ma << 8) / mbc->pcf->pdata->charger_reference_current_ma;
  175. if (mbcc5 > 255)
  176. mbcc5 = 255;
  177. pcf50633_reg_write(mbc->pcf, PCF50633_REG_MBCC5, mbcc5);
  178. return count;
  179. }
  180. /*
  181. * This attribute allows to change MBC charging limit on the fly
  182. * independently of usb current limit. It also gets set automatically every
  183. * time usb current limit is changed.
  184. */
  185. static DEVICE_ATTR(chg_curlim, S_IRUGO | S_IWUSR, show_chglim, set_chglim);
  186. static struct attribute *pcf50633_mbc_sysfs_entries[] = {
  187. &dev_attr_chgmode.attr,
  188. &dev_attr_usb_curlim.attr,
  189. &dev_attr_chg_curlim.attr,
  190. NULL,
  191. };
  192. static struct attribute_group mbc_attr_group = {
  193. .name = NULL, /* put in device directory */
  194. .attrs = pcf50633_mbc_sysfs_entries,
  195. };
  196. /* MBC state machine switches into charging mode when the battery voltage
  197. * falls below 96% of a battery float voltage. But the voltage drop in Li-ion
  198. * batteries is marginal(1~2 %) till about 80% of its capacity - which means,
  199. * after a BATFULL, charging won't be restarted until 80%.
  200. *
  201. * This work_struct function restarts charging at regular intervals to make
  202. * sure we don't discharge too much
  203. */
  204. static void pcf50633_mbc_charging_restart(struct work_struct *work)
  205. {
  206. struct pcf50633_mbc *mbc;
  207. u8 mbcs2, chgmod;
  208. mbc = container_of(work, struct pcf50633_mbc,
  209. charging_restart_work.work);
  210. mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  211. chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  212. if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
  213. return;
  214. /* Restart charging */
  215. pcf50633_reg_set_bit_mask(mbc->pcf, PCF50633_REG_MBCC1,
  216. PCF50633_MBCC1_RESUME, PCF50633_MBCC1_RESUME);
  217. mbc->usb_active = 1;
  218. power_supply_changed(&mbc->usb);
  219. dev_info(mbc->pcf->dev, "Charging restarted\n");
  220. }
  221. static void
  222. pcf50633_mbc_irq_handler(int irq, void *data)
  223. {
  224. struct pcf50633_mbc *mbc = data;
  225. int chg_restart_interval =
  226. mbc->pcf->pdata->charging_restart_interval;
  227. /* USB */
  228. if (irq == PCF50633_IRQ_USBINS) {
  229. mbc->usb_online = 1;
  230. } else if (irq == PCF50633_IRQ_USBREM) {
  231. mbc->usb_online = 0;
  232. mbc->usb_active = 0;
  233. pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
  234. cancel_delayed_work_sync(&mbc->charging_restart_work);
  235. }
  236. /* Adapter */
  237. if (irq == PCF50633_IRQ_ADPINS) {
  238. mbc->adapter_online = 1;
  239. mbc->adapter_active = 1;
  240. } else if (irq == PCF50633_IRQ_ADPREM) {
  241. mbc->adapter_online = 0;
  242. mbc->adapter_active = 0;
  243. }
  244. if (irq == PCF50633_IRQ_BATFULL) {
  245. mbc->usb_active = 0;
  246. mbc->adapter_active = 0;
  247. if (chg_restart_interval > 0)
  248. schedule_delayed_work(&mbc->charging_restart_work,
  249. chg_restart_interval);
  250. } else if (irq == PCF50633_IRQ_USBLIMON)
  251. mbc->usb_active = 0;
  252. else if (irq == PCF50633_IRQ_USBLIMOFF)
  253. mbc->usb_active = 1;
  254. power_supply_changed(&mbc->ac);
  255. power_supply_changed(&mbc->usb);
  256. power_supply_changed(&mbc->adapter);
  257. if (mbc->pcf->pdata->mbc_event_callback)
  258. mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
  259. }
  260. static int adapter_get_property(struct power_supply *psy,
  261. enum power_supply_property psp,
  262. union power_supply_propval *val)
  263. {
  264. struct pcf50633_mbc *mbc = container_of(psy,
  265. struct pcf50633_mbc, adapter);
  266. int ret = 0;
  267. switch (psp) {
  268. case POWER_SUPPLY_PROP_ONLINE:
  269. val->intval = mbc->adapter_online;
  270. break;
  271. default:
  272. ret = -EINVAL;
  273. break;
  274. }
  275. return ret;
  276. }
  277. static int usb_get_property(struct power_supply *psy,
  278. enum power_supply_property psp,
  279. union power_supply_propval *val)
  280. {
  281. struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
  282. int ret = 0;
  283. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  284. PCF50633_MBCC7_USB_MASK;
  285. switch (psp) {
  286. case POWER_SUPPLY_PROP_ONLINE:
  287. val->intval = mbc->usb_online &&
  288. (usblim <= PCF50633_MBCC7_USB_500mA);
  289. break;
  290. default:
  291. ret = -EINVAL;
  292. break;
  293. }
  294. return ret;
  295. }
  296. static int ac_get_property(struct power_supply *psy,
  297. enum power_supply_property psp,
  298. union power_supply_propval *val)
  299. {
  300. struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, ac);
  301. int ret = 0;
  302. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  303. PCF50633_MBCC7_USB_MASK;
  304. switch (psp) {
  305. case POWER_SUPPLY_PROP_ONLINE:
  306. val->intval = mbc->usb_online &&
  307. (usblim == PCF50633_MBCC7_USB_1000mA);
  308. break;
  309. default:
  310. ret = -EINVAL;
  311. break;
  312. }
  313. return ret;
  314. }
  315. static enum power_supply_property power_props[] = {
  316. POWER_SUPPLY_PROP_ONLINE,
  317. };
  318. static const u8 mbc_irq_handlers[] = {
  319. PCF50633_IRQ_ADPINS,
  320. PCF50633_IRQ_ADPREM,
  321. PCF50633_IRQ_USBINS,
  322. PCF50633_IRQ_USBREM,
  323. PCF50633_IRQ_BATFULL,
  324. PCF50633_IRQ_CHGHALT,
  325. PCF50633_IRQ_THLIMON,
  326. PCF50633_IRQ_THLIMOFF,
  327. PCF50633_IRQ_USBLIMON,
  328. PCF50633_IRQ_USBLIMOFF,
  329. PCF50633_IRQ_LOWSYS,
  330. PCF50633_IRQ_LOWBAT,
  331. };
  332. static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
  333. {
  334. struct pcf50633_mbc *mbc;
  335. struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
  336. int ret;
  337. int i;
  338. u8 mbcs1;
  339. mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
  340. if (!mbc)
  341. return -ENOMEM;
  342. platform_set_drvdata(pdev, mbc);
  343. mbc->pcf = pdata->pcf;
  344. /* Set up IRQ handlers */
  345. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  346. pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
  347. pcf50633_mbc_irq_handler, mbc);
  348. /* Create power supplies */
  349. mbc->adapter.name = "adapter";
  350. mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
  351. mbc->adapter.properties = power_props;
  352. mbc->adapter.num_properties = ARRAY_SIZE(power_props);
  353. mbc->adapter.get_property = &adapter_get_property;
  354. mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
  355. mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
  356. mbc->usb.name = "usb";
  357. mbc->usb.type = POWER_SUPPLY_TYPE_USB;
  358. mbc->usb.properties = power_props;
  359. mbc->usb.num_properties = ARRAY_SIZE(power_props);
  360. mbc->usb.get_property = usb_get_property;
  361. mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
  362. mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
  363. mbc->ac.name = "ac";
  364. mbc->ac.type = POWER_SUPPLY_TYPE_MAINS;
  365. mbc->ac.properties = power_props;
  366. mbc->ac.num_properties = ARRAY_SIZE(power_props);
  367. mbc->ac.get_property = ac_get_property;
  368. mbc->ac.supplied_to = mbc->pcf->pdata->batteries;
  369. mbc->ac.num_supplicants = mbc->pcf->pdata->num_batteries;
  370. ret = power_supply_register(&pdev->dev, &mbc->adapter);
  371. if (ret) {
  372. dev_err(mbc->pcf->dev, "failed to register adapter\n");
  373. kfree(mbc);
  374. return ret;
  375. }
  376. ret = power_supply_register(&pdev->dev, &mbc->usb);
  377. if (ret) {
  378. dev_err(mbc->pcf->dev, "failed to register usb\n");
  379. power_supply_unregister(&mbc->adapter);
  380. kfree(mbc);
  381. return ret;
  382. }
  383. ret = power_supply_register(&pdev->dev, &mbc->ac);
  384. if (ret) {
  385. dev_err(mbc->pcf->dev, "failed to register ac\n");
  386. power_supply_unregister(&mbc->adapter);
  387. power_supply_unregister(&mbc->usb);
  388. kfree(mbc);
  389. return ret;
  390. }
  391. INIT_DELAYED_WORK(&mbc->charging_restart_work,
  392. pcf50633_mbc_charging_restart);
  393. ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
  394. if (ret)
  395. dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
  396. mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
  397. if (mbcs1 & PCF50633_MBCS1_USBPRES)
  398. pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
  399. if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
  400. pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
  401. return 0;
  402. }
  403. static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
  404. {
  405. struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
  406. int i;
  407. /* Remove IRQ handlers */
  408. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  409. pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
  410. power_supply_unregister(&mbc->usb);
  411. power_supply_unregister(&mbc->adapter);
  412. power_supply_unregister(&mbc->ac);
  413. cancel_delayed_work_sync(&mbc->charging_restart_work);
  414. kfree(mbc);
  415. return 0;
  416. }
  417. static struct platform_driver pcf50633_mbc_driver = {
  418. .driver = {
  419. .name = "pcf50633-mbc",
  420. },
  421. .probe = pcf50633_mbc_probe,
  422. .remove = __devexit_p(pcf50633_mbc_remove),
  423. };
  424. static int __init pcf50633_mbc_init(void)
  425. {
  426. return platform_driver_register(&pcf50633_mbc_driver);
  427. }
  428. module_init(pcf50633_mbc_init);
  429. static void __exit pcf50633_mbc_exit(void)
  430. {
  431. platform_driver_unregister(&pcf50633_mbc_driver);
  432. }
  433. module_exit(pcf50633_mbc_exit);
  434. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  435. MODULE_DESCRIPTION("PCF50633 mbc driver");
  436. MODULE_LICENSE("GPL");
  437. MODULE_ALIAS("platform:pcf50633-mbc");