pcf50633-charger.c 12 KB

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