max8903_charger.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * max8903_charger.c - Maxim 8903 USB/Adapter Charger Driver
  3. *
  4. * Copyright (C) 2011 Samsung Electronics
  5. * MyungJoo Ham <myungjoo.ham@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <linux/gpio.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/power_supply.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/power/max8903_charger.h>
  29. struct max8903_data {
  30. struct max8903_pdata pdata;
  31. struct device *dev;
  32. struct power_supply psy;
  33. bool fault;
  34. bool usb_in;
  35. bool ta_in;
  36. };
  37. static enum power_supply_property max8903_charger_props[] = {
  38. POWER_SUPPLY_PROP_STATUS, /* Charger status output */
  39. POWER_SUPPLY_PROP_ONLINE, /* External power source */
  40. POWER_SUPPLY_PROP_HEALTH, /* Fault or OK */
  41. };
  42. static int max8903_get_property(struct power_supply *psy,
  43. enum power_supply_property psp,
  44. union power_supply_propval *val)
  45. {
  46. struct max8903_data *data = container_of(psy,
  47. struct max8903_data, psy);
  48. switch (psp) {
  49. case POWER_SUPPLY_PROP_STATUS:
  50. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  51. if (data->pdata.chg) {
  52. if (gpio_get_value(data->pdata.chg) == 0)
  53. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  54. else if (data->usb_in || data->ta_in)
  55. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  56. else
  57. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  58. }
  59. break;
  60. case POWER_SUPPLY_PROP_ONLINE:
  61. val->intval = 0;
  62. if (data->usb_in || data->ta_in)
  63. val->intval = 1;
  64. break;
  65. case POWER_SUPPLY_PROP_HEALTH:
  66. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  67. if (data->fault)
  68. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  69. break;
  70. default:
  71. return -EINVAL;
  72. }
  73. return 0;
  74. }
  75. static irqreturn_t max8903_dcin(int irq, void *_data)
  76. {
  77. struct max8903_data *data = _data;
  78. struct max8903_pdata *pdata = &data->pdata;
  79. bool ta_in;
  80. enum power_supply_type old_type;
  81. ta_in = gpio_get_value(pdata->dok) ? false : true;
  82. if (ta_in == data->ta_in)
  83. return IRQ_HANDLED;
  84. data->ta_in = ta_in;
  85. /* Set Current-Limit-Mode 1:DC 0:USB */
  86. if (pdata->dcm)
  87. gpio_set_value(pdata->dcm, ta_in ? 1 : 0);
  88. /* Charger Enable / Disable (cen is negated) */
  89. if (pdata->cen)
  90. gpio_set_value(pdata->cen, ta_in ? 0 :
  91. (data->usb_in ? 0 : 1));
  92. dev_dbg(data->dev, "TA(DC-IN) Charger %s.\n", ta_in ?
  93. "Connected" : "Disconnected");
  94. old_type = data->psy.type;
  95. if (data->ta_in)
  96. data->psy.type = POWER_SUPPLY_TYPE_MAINS;
  97. else if (data->usb_in)
  98. data->psy.type = POWER_SUPPLY_TYPE_USB;
  99. else
  100. data->psy.type = POWER_SUPPLY_TYPE_BATTERY;
  101. if (old_type != data->psy.type)
  102. power_supply_changed(&data->psy);
  103. return IRQ_HANDLED;
  104. }
  105. static irqreturn_t max8903_usbin(int irq, void *_data)
  106. {
  107. struct max8903_data *data = _data;
  108. struct max8903_pdata *pdata = &data->pdata;
  109. bool usb_in;
  110. enum power_supply_type old_type;
  111. usb_in = gpio_get_value(pdata->uok) ? false : true;
  112. if (usb_in == data->usb_in)
  113. return IRQ_HANDLED;
  114. data->usb_in = usb_in;
  115. /* Do not touch Current-Limit-Mode */
  116. /* Charger Enable / Disable (cen is negated) */
  117. if (pdata->cen)
  118. gpio_set_value(pdata->cen, usb_in ? 0 :
  119. (data->ta_in ? 0 : 1));
  120. dev_dbg(data->dev, "USB Charger %s.\n", usb_in ?
  121. "Connected" : "Disconnected");
  122. old_type = data->psy.type;
  123. if (data->ta_in)
  124. data->psy.type = POWER_SUPPLY_TYPE_MAINS;
  125. else if (data->usb_in)
  126. data->psy.type = POWER_SUPPLY_TYPE_USB;
  127. else
  128. data->psy.type = POWER_SUPPLY_TYPE_BATTERY;
  129. if (old_type != data->psy.type)
  130. power_supply_changed(&data->psy);
  131. return IRQ_HANDLED;
  132. }
  133. static irqreturn_t max8903_fault(int irq, void *_data)
  134. {
  135. struct max8903_data *data = _data;
  136. struct max8903_pdata *pdata = &data->pdata;
  137. bool fault;
  138. fault = gpio_get_value(pdata->flt) ? false : true;
  139. if (fault == data->fault)
  140. return IRQ_HANDLED;
  141. data->fault = fault;
  142. if (fault)
  143. dev_err(data->dev, "Charger suffers a fault and stops.\n");
  144. else
  145. dev_err(data->dev, "Charger recovered from a fault.\n");
  146. return IRQ_HANDLED;
  147. }
  148. static __devinit int max8903_probe(struct platform_device *pdev)
  149. {
  150. struct max8903_data *data;
  151. struct device *dev = &pdev->dev;
  152. struct max8903_pdata *pdata = pdev->dev.platform_data;
  153. int ret = 0;
  154. int gpio;
  155. int ta_in = 0;
  156. int usb_in = 0;
  157. data = kzalloc(sizeof(struct max8903_data), GFP_KERNEL);
  158. if (data == NULL) {
  159. dev_err(dev, "Cannot allocate memory.\n");
  160. return -ENOMEM;
  161. }
  162. memcpy(&data->pdata, pdata, sizeof(struct max8903_pdata));
  163. data->dev = dev;
  164. platform_set_drvdata(pdev, data);
  165. if (pdata->dc_valid == false && pdata->usb_valid == false) {
  166. dev_err(dev, "No valid power sources.\n");
  167. ret = -EINVAL;
  168. goto err;
  169. }
  170. if (pdata->dc_valid) {
  171. if (pdata->dok && gpio_is_valid(pdata->dok) &&
  172. pdata->dcm && gpio_is_valid(pdata->dcm)) {
  173. gpio = pdata->dok; /* PULL_UPed Interrupt */
  174. ta_in = gpio_get_value(gpio) ? 0 : 1;
  175. gpio = pdata->dcm; /* Output */
  176. gpio_set_value(gpio, ta_in);
  177. } else {
  178. dev_err(dev, "When DC is wired, DOK and DCM should"
  179. " be wired as well.\n");
  180. ret = -EINVAL;
  181. goto err;
  182. }
  183. } else {
  184. if (pdata->dcm) {
  185. if (gpio_is_valid(pdata->dcm))
  186. gpio_set_value(pdata->dcm, 0);
  187. else {
  188. dev_err(dev, "Invalid pin: dcm.\n");
  189. ret = -EINVAL;
  190. goto err;
  191. }
  192. }
  193. }
  194. if (pdata->usb_valid) {
  195. if (pdata->uok && gpio_is_valid(pdata->uok)) {
  196. gpio = pdata->uok;
  197. usb_in = gpio_get_value(gpio) ? 0 : 1;
  198. } else {
  199. dev_err(dev, "When USB is wired, UOK should be wired."
  200. "as well.\n");
  201. ret = -EINVAL;
  202. goto err;
  203. }
  204. }
  205. if (pdata->cen) {
  206. if (gpio_is_valid(pdata->cen)) {
  207. gpio_set_value(pdata->cen, (ta_in || usb_in) ? 0 : 1);
  208. } else {
  209. dev_err(dev, "Invalid pin: cen.\n");
  210. ret = -EINVAL;
  211. goto err;
  212. }
  213. }
  214. if (pdata->chg) {
  215. if (!gpio_is_valid(pdata->chg)) {
  216. dev_err(dev, "Invalid pin: chg.\n");
  217. ret = -EINVAL;
  218. goto err;
  219. }
  220. }
  221. if (pdata->flt) {
  222. if (!gpio_is_valid(pdata->flt)) {
  223. dev_err(dev, "Invalid pin: flt.\n");
  224. ret = -EINVAL;
  225. goto err;
  226. }
  227. }
  228. if (pdata->usus) {
  229. if (!gpio_is_valid(pdata->usus)) {
  230. dev_err(dev, "Invalid pin: usus.\n");
  231. ret = -EINVAL;
  232. goto err;
  233. }
  234. }
  235. data->fault = false;
  236. data->ta_in = ta_in;
  237. data->usb_in = usb_in;
  238. data->psy.name = "max8903_charger";
  239. data->psy.type = (ta_in) ? POWER_SUPPLY_TYPE_MAINS :
  240. ((usb_in) ? POWER_SUPPLY_TYPE_USB :
  241. POWER_SUPPLY_TYPE_BATTERY);
  242. data->psy.get_property = max8903_get_property;
  243. data->psy.properties = max8903_charger_props;
  244. data->psy.num_properties = ARRAY_SIZE(max8903_charger_props);
  245. ret = power_supply_register(dev, &data->psy);
  246. if (ret) {
  247. dev_err(dev, "failed: power supply register.\n");
  248. goto err;
  249. }
  250. if (pdata->dc_valid) {
  251. ret = request_threaded_irq(gpio_to_irq(pdata->dok),
  252. NULL, max8903_dcin,
  253. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  254. "MAX8903 DC IN", data);
  255. if (ret) {
  256. dev_err(dev, "Cannot request irq %d for DC (%d)\n",
  257. gpio_to_irq(pdata->dok), ret);
  258. goto err_psy;
  259. }
  260. }
  261. if (pdata->usb_valid) {
  262. ret = request_threaded_irq(gpio_to_irq(pdata->uok),
  263. NULL, max8903_usbin,
  264. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  265. "MAX8903 USB IN", data);
  266. if (ret) {
  267. dev_err(dev, "Cannot request irq %d for USB (%d)\n",
  268. gpio_to_irq(pdata->uok), ret);
  269. goto err_dc_irq;
  270. }
  271. }
  272. if (pdata->flt) {
  273. ret = request_threaded_irq(gpio_to_irq(pdata->flt),
  274. NULL, max8903_fault,
  275. IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
  276. "MAX8903 Fault", data);
  277. if (ret) {
  278. dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
  279. gpio_to_irq(pdata->flt), ret);
  280. goto err_usb_irq;
  281. }
  282. }
  283. return 0;
  284. err_usb_irq:
  285. if (pdata->usb_valid)
  286. free_irq(gpio_to_irq(pdata->uok), data);
  287. err_dc_irq:
  288. if (pdata->dc_valid)
  289. free_irq(gpio_to_irq(pdata->dok), data);
  290. err_psy:
  291. power_supply_unregister(&data->psy);
  292. err:
  293. kfree(data);
  294. return ret;
  295. }
  296. static __devexit int max8903_remove(struct platform_device *pdev)
  297. {
  298. struct max8903_data *data = platform_get_drvdata(pdev);
  299. if (data) {
  300. struct max8903_pdata *pdata = &data->pdata;
  301. if (pdata->flt)
  302. free_irq(gpio_to_irq(pdata->flt), data);
  303. if (pdata->usb_valid)
  304. free_irq(gpio_to_irq(pdata->uok), data);
  305. if (pdata->dc_valid)
  306. free_irq(gpio_to_irq(pdata->dok), data);
  307. power_supply_unregister(&data->psy);
  308. kfree(data);
  309. }
  310. return 0;
  311. }
  312. static struct platform_driver max8903_driver = {
  313. .probe = max8903_probe,
  314. .remove = __devexit_p(max8903_remove),
  315. .driver = {
  316. .name = "max8903-charger",
  317. .owner = THIS_MODULE,
  318. },
  319. };
  320. module_platform_driver(max8903_driver);
  321. MODULE_LICENSE("GPL");
  322. MODULE_DESCRIPTION("MAX8903 Charger Driver");
  323. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  324. MODULE_ALIAS("platform:max8903-charger");