pcf50633-charger.c 12 KB

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