pcf50633-charger.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = kstrtoul(buf, 10, &ma);
  165. if (ret)
  166. return ret;
  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 = kstrtoul(buf, 10, &ma);
  192. if (ret)
  193. return ret;
  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 = power_supply_get_drvdata(psy);
  243. int ret = 0;
  244. switch (psp) {
  245. case POWER_SUPPLY_PROP_ONLINE:
  246. val->intval = mbc->adapter_online;
  247. break;
  248. default:
  249. ret = -EINVAL;
  250. break;
  251. }
  252. return ret;
  253. }
  254. static int usb_get_property(struct power_supply *psy,
  255. enum power_supply_property psp,
  256. union power_supply_propval *val)
  257. {
  258. struct pcf50633_mbc *mbc = power_supply_get_drvdata(psy);
  259. int ret = 0;
  260. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  261. PCF50633_MBCC7_USB_MASK;
  262. switch (psp) {
  263. case POWER_SUPPLY_PROP_ONLINE:
  264. val->intval = mbc->usb_online &&
  265. (usblim <= PCF50633_MBCC7_USB_500mA);
  266. break;
  267. default:
  268. ret = -EINVAL;
  269. break;
  270. }
  271. return ret;
  272. }
  273. static int ac_get_property(struct power_supply *psy,
  274. enum power_supply_property psp,
  275. union power_supply_propval *val)
  276. {
  277. struct pcf50633_mbc *mbc = power_supply_get_drvdata(psy);
  278. int ret = 0;
  279. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  280. PCF50633_MBCC7_USB_MASK;
  281. switch (psp) {
  282. case POWER_SUPPLY_PROP_ONLINE:
  283. val->intval = mbc->usb_online &&
  284. (usblim == PCF50633_MBCC7_USB_1000mA);
  285. break;
  286. default:
  287. ret = -EINVAL;
  288. break;
  289. }
  290. return ret;
  291. }
  292. static enum power_supply_property power_props[] = {
  293. POWER_SUPPLY_PROP_ONLINE,
  294. };
  295. static const u8 mbc_irq_handlers[] = {
  296. PCF50633_IRQ_ADPINS,
  297. PCF50633_IRQ_ADPREM,
  298. PCF50633_IRQ_USBINS,
  299. PCF50633_IRQ_USBREM,
  300. PCF50633_IRQ_BATFULL,
  301. PCF50633_IRQ_CHGHALT,
  302. PCF50633_IRQ_THLIMON,
  303. PCF50633_IRQ_THLIMOFF,
  304. PCF50633_IRQ_USBLIMON,
  305. PCF50633_IRQ_USBLIMOFF,
  306. PCF50633_IRQ_LOWSYS,
  307. PCF50633_IRQ_LOWBAT,
  308. };
  309. static const struct power_supply_desc pcf50633_mbc_adapter_desc = {
  310. .name = "adapter",
  311. .type = POWER_SUPPLY_TYPE_MAINS,
  312. .properties = power_props,
  313. .num_properties = ARRAY_SIZE(power_props),
  314. .get_property = &adapter_get_property,
  315. };
  316. static const struct power_supply_desc pcf50633_mbc_usb_desc = {
  317. .name = "usb",
  318. .type = POWER_SUPPLY_TYPE_USB,
  319. .properties = power_props,
  320. .num_properties = ARRAY_SIZE(power_props),
  321. .get_property = usb_get_property,
  322. };
  323. static const struct power_supply_desc pcf50633_mbc_ac_desc = {
  324. .name = "ac",
  325. .type = POWER_SUPPLY_TYPE_MAINS,
  326. .properties = power_props,
  327. .num_properties = ARRAY_SIZE(power_props),
  328. .get_property = ac_get_property,
  329. };
  330. static int pcf50633_mbc_probe(struct platform_device *pdev)
  331. {
  332. struct power_supply_config psy_cfg = {};
  333. struct pcf50633_mbc *mbc;
  334. int ret;
  335. int i;
  336. u8 mbcs1;
  337. mbc = devm_kzalloc(&pdev->dev, sizeof(*mbc), GFP_KERNEL);
  338. if (!mbc)
  339. return -ENOMEM;
  340. platform_set_drvdata(pdev, mbc);
  341. mbc->pcf = dev_to_pcf50633(pdev->dev.parent);
  342. /* Set up IRQ handlers */
  343. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  344. pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
  345. pcf50633_mbc_irq_handler, mbc);
  346. psy_cfg.supplied_to = mbc->pcf->pdata->batteries;
  347. psy_cfg.num_supplicants = mbc->pcf->pdata->num_batteries;
  348. psy_cfg.drv_data = mbc;
  349. /* Create power supplies */
  350. mbc->adapter = power_supply_register(&pdev->dev,
  351. &pcf50633_mbc_adapter_desc,
  352. &psy_cfg);
  353. if (IS_ERR(mbc->adapter)) {
  354. dev_err(mbc->pcf->dev, "failed to register adapter\n");
  355. ret = PTR_ERR(mbc->adapter);
  356. return ret;
  357. }
  358. mbc->usb = power_supply_register(&pdev->dev, &pcf50633_mbc_usb_desc,
  359. &psy_cfg);
  360. if (IS_ERR(mbc->usb)) {
  361. dev_err(mbc->pcf->dev, "failed to register usb\n");
  362. power_supply_unregister(mbc->adapter);
  363. ret = PTR_ERR(mbc->usb);
  364. return ret;
  365. }
  366. mbc->ac = power_supply_register(&pdev->dev, &pcf50633_mbc_ac_desc,
  367. &psy_cfg);
  368. if (IS_ERR(mbc->ac)) {
  369. dev_err(mbc->pcf->dev, "failed to register ac\n");
  370. power_supply_unregister(mbc->adapter);
  371. power_supply_unregister(mbc->usb);
  372. ret = PTR_ERR(mbc->ac);
  373. return ret;
  374. }
  375. ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
  376. if (ret)
  377. dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
  378. mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
  379. if (mbcs1 & PCF50633_MBCS1_USBPRES)
  380. pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
  381. if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
  382. pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
  383. return 0;
  384. }
  385. static int pcf50633_mbc_remove(struct platform_device *pdev)
  386. {
  387. struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
  388. int i;
  389. /* Remove IRQ handlers */
  390. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  391. pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
  392. sysfs_remove_group(&pdev->dev.kobj, &mbc_attr_group);
  393. power_supply_unregister(mbc->usb);
  394. power_supply_unregister(mbc->adapter);
  395. power_supply_unregister(mbc->ac);
  396. return 0;
  397. }
  398. static struct platform_driver pcf50633_mbc_driver = {
  399. .driver = {
  400. .name = "pcf50633-mbc",
  401. },
  402. .probe = pcf50633_mbc_probe,
  403. .remove = pcf50633_mbc_remove,
  404. };
  405. module_platform_driver(pcf50633_mbc_driver);
  406. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  407. MODULE_DESCRIPTION("PCF50633 mbc driver");
  408. MODULE_LICENSE("GPL");
  409. MODULE_ALIAS("platform:pcf50633-mbc");