pcf50633-charger.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. };
  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. if (ma >= 1000)
  41. bits = PCF50633_MBCC7_USB_1000mA;
  42. else if (ma >= 500)
  43. bits = PCF50633_MBCC7_USB_500mA;
  44. else if (ma >= 100)
  45. bits = PCF50633_MBCC7_USB_100mA;
  46. else
  47. bits = PCF50633_MBCC7_USB_SUSPEND;
  48. ret = pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC7,
  49. PCF50633_MBCC7_USB_MASK, bits);
  50. if (ret)
  51. dev_err(pcf->dev, "error setting usb curlim to %d mA\n", ma);
  52. else
  53. dev_info(pcf->dev, "usb curlim to %d mA\n", ma);
  54. power_supply_changed(&mbc->usb);
  55. return ret;
  56. }
  57. EXPORT_SYMBOL_GPL(pcf50633_mbc_usb_curlim_set);
  58. int pcf50633_mbc_get_status(struct pcf50633 *pcf)
  59. {
  60. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  61. int status = 0;
  62. if (mbc->usb_online)
  63. status |= PCF50633_MBC_USB_ONLINE;
  64. if (mbc->usb_active)
  65. status |= PCF50633_MBC_USB_ACTIVE;
  66. if (mbc->adapter_online)
  67. status |= PCF50633_MBC_ADAPTER_ONLINE;
  68. if (mbc->adapter_active)
  69. status |= PCF50633_MBC_ADAPTER_ACTIVE;
  70. return status;
  71. }
  72. EXPORT_SYMBOL_GPL(pcf50633_mbc_get_status);
  73. void pcf50633_mbc_set_status(struct pcf50633 *pcf, int what, int status)
  74. {
  75. struct pcf50633_mbc *mbc = platform_get_drvdata(pcf->mbc_pdev);
  76. if (what & PCF50633_MBC_USB_ONLINE)
  77. mbc->usb_online = !!status;
  78. if (what & PCF50633_MBC_USB_ACTIVE)
  79. mbc->usb_active = !!status;
  80. if (what & PCF50633_MBC_ADAPTER_ONLINE)
  81. mbc->adapter_online = !!status;
  82. if (what & PCF50633_MBC_ADAPTER_ACTIVE)
  83. mbc->adapter_active = !!status;
  84. }
  85. EXPORT_SYMBOL_GPL(pcf50633_mbc_set_status);
  86. static ssize_t
  87. show_chgmode(struct device *dev, struct device_attribute *attr, char *buf)
  88. {
  89. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  90. u8 mbcs2 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS2);
  91. u8 chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
  92. return sprintf(buf, "%d\n", chgmod);
  93. }
  94. static DEVICE_ATTR(chgmode, S_IRUGO, show_chgmode, NULL);
  95. static ssize_t
  96. show_usblim(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  99. u8 usblim = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCC7) &
  100. PCF50633_MBCC7_USB_MASK;
  101. unsigned int ma;
  102. if (usblim == PCF50633_MBCC7_USB_1000mA)
  103. ma = 1000;
  104. else if (usblim == PCF50633_MBCC7_USB_500mA)
  105. ma = 500;
  106. else if (usblim == PCF50633_MBCC7_USB_100mA)
  107. ma = 100;
  108. else
  109. ma = 0;
  110. return sprintf(buf, "%u\n", ma);
  111. }
  112. static ssize_t set_usblim(struct device *dev,
  113. struct device_attribute *attr, const char *buf, size_t count)
  114. {
  115. struct pcf50633_mbc *mbc = dev_get_drvdata(dev);
  116. unsigned long ma;
  117. int ret;
  118. ret = strict_strtoul(buf, 10, &ma);
  119. if (ret)
  120. return -EINVAL;
  121. pcf50633_mbc_usb_curlim_set(mbc->pcf, ma);
  122. return count;
  123. }
  124. static DEVICE_ATTR(usb_curlim, S_IRUGO | S_IWUSR, show_usblim, set_usblim);
  125. static struct attribute *pcf50633_mbc_sysfs_entries[] = {
  126. &dev_attr_chgmode.attr,
  127. &dev_attr_usb_curlim.attr,
  128. NULL,
  129. };
  130. static struct attribute_group mbc_attr_group = {
  131. .name = NULL, /* put in device directory */
  132. .attrs = pcf50633_mbc_sysfs_entries,
  133. };
  134. static void
  135. pcf50633_mbc_irq_handler(int irq, void *data)
  136. {
  137. struct pcf50633_mbc *mbc = data;
  138. /* USB */
  139. if (irq == PCF50633_IRQ_USBINS) {
  140. mbc->usb_online = 1;
  141. } else if (irq == PCF50633_IRQ_USBREM) {
  142. mbc->usb_online = 0;
  143. mbc->usb_active = 0;
  144. pcf50633_mbc_usb_curlim_set(mbc->pcf, 0);
  145. }
  146. /* Adapter */
  147. if (irq == PCF50633_IRQ_ADPINS) {
  148. mbc->adapter_online = 1;
  149. mbc->adapter_active = 1;
  150. } else if (irq == PCF50633_IRQ_ADPREM) {
  151. mbc->adapter_online = 0;
  152. mbc->adapter_active = 0;
  153. }
  154. if (irq == PCF50633_IRQ_BATFULL) {
  155. mbc->usb_active = 0;
  156. mbc->adapter_active = 0;
  157. }
  158. power_supply_changed(&mbc->usb);
  159. power_supply_changed(&mbc->adapter);
  160. if (mbc->pcf->pdata->mbc_event_callback)
  161. mbc->pcf->pdata->mbc_event_callback(mbc->pcf, irq);
  162. }
  163. static int adapter_get_property(struct power_supply *psy,
  164. enum power_supply_property psp,
  165. union power_supply_propval *val)
  166. {
  167. struct pcf50633_mbc *mbc = container_of(psy,
  168. struct pcf50633_mbc, adapter);
  169. int ret = 0;
  170. switch (psp) {
  171. case POWER_SUPPLY_PROP_ONLINE:
  172. val->intval = mbc->adapter_online;
  173. break;
  174. default:
  175. ret = -EINVAL;
  176. break;
  177. }
  178. return ret;
  179. }
  180. static int usb_get_property(struct power_supply *psy,
  181. enum power_supply_property psp,
  182. union power_supply_propval *val)
  183. {
  184. struct pcf50633_mbc *mbc = container_of(psy, struct pcf50633_mbc, usb);
  185. int ret = 0;
  186. switch (psp) {
  187. case POWER_SUPPLY_PROP_ONLINE:
  188. val->intval = mbc->usb_online;
  189. break;
  190. default:
  191. ret = -EINVAL;
  192. break;
  193. }
  194. return ret;
  195. }
  196. static enum power_supply_property power_props[] = {
  197. POWER_SUPPLY_PROP_ONLINE,
  198. };
  199. static const u8 mbc_irq_handlers[] = {
  200. PCF50633_IRQ_ADPINS,
  201. PCF50633_IRQ_ADPREM,
  202. PCF50633_IRQ_USBINS,
  203. PCF50633_IRQ_USBREM,
  204. PCF50633_IRQ_BATFULL,
  205. PCF50633_IRQ_CHGHALT,
  206. PCF50633_IRQ_THLIMON,
  207. PCF50633_IRQ_THLIMOFF,
  208. PCF50633_IRQ_USBLIMON,
  209. PCF50633_IRQ_USBLIMOFF,
  210. PCF50633_IRQ_LOWSYS,
  211. PCF50633_IRQ_LOWBAT,
  212. };
  213. static int __devinit pcf50633_mbc_probe(struct platform_device *pdev)
  214. {
  215. struct pcf50633_mbc *mbc;
  216. struct pcf50633_subdev_pdata *pdata = pdev->dev.platform_data;
  217. int ret;
  218. int i;
  219. u8 mbcs1;
  220. mbc = kzalloc(sizeof(*mbc), GFP_KERNEL);
  221. if (!mbc)
  222. return -ENOMEM;
  223. platform_set_drvdata(pdev, mbc);
  224. mbc->pcf = pdata->pcf;
  225. /* Set up IRQ handlers */
  226. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  227. pcf50633_register_irq(mbc->pcf, mbc_irq_handlers[i],
  228. pcf50633_mbc_irq_handler, mbc);
  229. /* Create power supplies */
  230. mbc->adapter.name = "adapter";
  231. mbc->adapter.type = POWER_SUPPLY_TYPE_MAINS;
  232. mbc->adapter.properties = power_props;
  233. mbc->adapter.num_properties = ARRAY_SIZE(power_props);
  234. mbc->adapter.get_property = &adapter_get_property;
  235. mbc->adapter.supplied_to = mbc->pcf->pdata->batteries;
  236. mbc->adapter.num_supplicants = mbc->pcf->pdata->num_batteries;
  237. mbc->usb.name = "usb";
  238. mbc->usb.type = POWER_SUPPLY_TYPE_USB;
  239. mbc->usb.properties = power_props;
  240. mbc->usb.num_properties = ARRAY_SIZE(power_props);
  241. mbc->usb.get_property = usb_get_property;
  242. mbc->usb.supplied_to = mbc->pcf->pdata->batteries;
  243. mbc->usb.num_supplicants = mbc->pcf->pdata->num_batteries;
  244. ret = power_supply_register(&pdev->dev, &mbc->adapter);
  245. if (ret) {
  246. dev_err(mbc->pcf->dev, "failed to register adapter\n");
  247. kfree(mbc);
  248. return ret;
  249. }
  250. ret = power_supply_register(&pdev->dev, &mbc->usb);
  251. if (ret) {
  252. dev_err(mbc->pcf->dev, "failed to register usb\n");
  253. power_supply_unregister(&mbc->adapter);
  254. kfree(mbc);
  255. return ret;
  256. }
  257. ret = sysfs_create_group(&pdev->dev.kobj, &mbc_attr_group);
  258. if (ret)
  259. dev_err(mbc->pcf->dev, "failed to create sysfs entries\n");
  260. mbcs1 = pcf50633_reg_read(mbc->pcf, PCF50633_REG_MBCS1);
  261. if (mbcs1 & PCF50633_MBCS1_USBPRES)
  262. pcf50633_mbc_irq_handler(PCF50633_IRQ_USBINS, mbc);
  263. if (mbcs1 & PCF50633_MBCS1_ADAPTPRES)
  264. pcf50633_mbc_irq_handler(PCF50633_IRQ_ADPINS, mbc);
  265. return 0;
  266. }
  267. static int __devexit pcf50633_mbc_remove(struct platform_device *pdev)
  268. {
  269. struct pcf50633_mbc *mbc = platform_get_drvdata(pdev);
  270. int i;
  271. /* Remove IRQ handlers */
  272. for (i = 0; i < ARRAY_SIZE(mbc_irq_handlers); i++)
  273. pcf50633_free_irq(mbc->pcf, mbc_irq_handlers[i]);
  274. power_supply_unregister(&mbc->usb);
  275. power_supply_unregister(&mbc->adapter);
  276. kfree(mbc);
  277. return 0;
  278. }
  279. static struct platform_driver pcf50633_mbc_driver = {
  280. .driver = {
  281. .name = "pcf50633-mbc",
  282. },
  283. .probe = pcf50633_mbc_probe,
  284. .remove = __devexit_p(pcf50633_mbc_remove),
  285. };
  286. static int __init pcf50633_mbc_init(void)
  287. {
  288. return platform_driver_register(&pcf50633_mbc_driver);
  289. }
  290. module_init(pcf50633_mbc_init);
  291. static void __exit pcf50633_mbc_exit(void)
  292. {
  293. platform_driver_unregister(&pcf50633_mbc_driver);
  294. }
  295. module_exit(pcf50633_mbc_exit);
  296. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  297. MODULE_DESCRIPTION("PCF50633 mbc driver");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS("platform:pcf50633-mbc");