pcf50633-charger.c 13 KB

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