max8903_charger.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. struct power_supply_desc psy_desc;
  34. bool fault;
  35. bool usb_in;
  36. bool ta_in;
  37. };
  38. static enum power_supply_property max8903_charger_props[] = {
  39. POWER_SUPPLY_PROP_STATUS, /* Charger status output */
  40. POWER_SUPPLY_PROP_ONLINE, /* External power source */
  41. POWER_SUPPLY_PROP_HEALTH, /* Fault or OK */
  42. };
  43. static int max8903_get_property(struct power_supply *psy,
  44. enum power_supply_property psp,
  45. union power_supply_propval *val)
  46. {
  47. struct max8903_data *data = power_supply_get_drvdata(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_desc.type;
  95. if (data->ta_in)
  96. data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
  97. else if (data->usb_in)
  98. data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  99. else
  100. data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  101. if (old_type != data->psy_desc.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_desc.type;
  123. if (data->ta_in)
  124. data->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
  125. else if (data->usb_in)
  126. data->psy_desc.type = POWER_SUPPLY_TYPE_USB;
  127. else
  128. data->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
  129. if (old_type != data->psy_desc.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 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. struct power_supply_config psy_cfg = {};
  154. int ret = 0;
  155. int gpio;
  156. int ta_in = 0;
  157. int usb_in = 0;
  158. data = devm_kzalloc(dev, sizeof(struct max8903_data), GFP_KERNEL);
  159. if (data == NULL) {
  160. dev_err(dev, "Cannot allocate memory.\n");
  161. return -ENOMEM;
  162. }
  163. memcpy(&data->pdata, pdata, sizeof(struct max8903_pdata));
  164. data->dev = dev;
  165. platform_set_drvdata(pdev, data);
  166. if (pdata->dc_valid == false && pdata->usb_valid == false) {
  167. dev_err(dev, "No valid power sources.\n");
  168. return -EINVAL;
  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. return -EINVAL;
  181. }
  182. } else {
  183. if (pdata->dcm) {
  184. if (gpio_is_valid(pdata->dcm))
  185. gpio_set_value(pdata->dcm, 0);
  186. else {
  187. dev_err(dev, "Invalid pin: dcm.\n");
  188. return -EINVAL;
  189. }
  190. }
  191. }
  192. if (pdata->usb_valid) {
  193. if (pdata->uok && gpio_is_valid(pdata->uok)) {
  194. gpio = pdata->uok;
  195. usb_in = gpio_get_value(gpio) ? 0 : 1;
  196. } else {
  197. dev_err(dev, "When USB is wired, UOK should be wired."
  198. "as well.\n");
  199. return -EINVAL;
  200. }
  201. }
  202. if (pdata->cen) {
  203. if (gpio_is_valid(pdata->cen)) {
  204. gpio_set_value(pdata->cen, (ta_in || usb_in) ? 0 : 1);
  205. } else {
  206. dev_err(dev, "Invalid pin: cen.\n");
  207. return -EINVAL;
  208. }
  209. }
  210. if (pdata->chg) {
  211. if (!gpio_is_valid(pdata->chg)) {
  212. dev_err(dev, "Invalid pin: chg.\n");
  213. return -EINVAL;
  214. }
  215. }
  216. if (pdata->flt) {
  217. if (!gpio_is_valid(pdata->flt)) {
  218. dev_err(dev, "Invalid pin: flt.\n");
  219. return -EINVAL;
  220. }
  221. }
  222. if (pdata->usus) {
  223. if (!gpio_is_valid(pdata->usus)) {
  224. dev_err(dev, "Invalid pin: usus.\n");
  225. return -EINVAL;
  226. }
  227. }
  228. data->fault = false;
  229. data->ta_in = ta_in;
  230. data->usb_in = usb_in;
  231. data->psy_desc.name = "max8903_charger";
  232. data->psy_desc.type = (ta_in) ? POWER_SUPPLY_TYPE_MAINS :
  233. ((usb_in) ? POWER_SUPPLY_TYPE_USB :
  234. POWER_SUPPLY_TYPE_BATTERY);
  235. data->psy_desc.get_property = max8903_get_property;
  236. data->psy_desc.properties = max8903_charger_props;
  237. data->psy_desc.num_properties = ARRAY_SIZE(max8903_charger_props);
  238. psy_cfg.drv_data = data;
  239. data->psy = devm_power_supply_register(dev, &data->psy_desc, &psy_cfg);
  240. if (IS_ERR(data->psy)) {
  241. dev_err(dev, "failed: power supply register.\n");
  242. return PTR_ERR(data->psy);
  243. }
  244. if (pdata->dc_valid) {
  245. ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->dok),
  246. NULL, max8903_dcin,
  247. IRQF_TRIGGER_FALLING |
  248. IRQF_TRIGGER_RISING,
  249. "MAX8903 DC IN", data);
  250. if (ret) {
  251. dev_err(dev, "Cannot request irq %d for DC (%d)\n",
  252. gpio_to_irq(pdata->dok), ret);
  253. return ret;
  254. }
  255. }
  256. if (pdata->usb_valid) {
  257. ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->uok),
  258. NULL, max8903_usbin,
  259. IRQF_TRIGGER_FALLING |
  260. IRQF_TRIGGER_RISING,
  261. "MAX8903 USB IN", data);
  262. if (ret) {
  263. dev_err(dev, "Cannot request irq %d for USB (%d)\n",
  264. gpio_to_irq(pdata->uok), ret);
  265. return ret;
  266. }
  267. }
  268. if (pdata->flt) {
  269. ret = devm_request_threaded_irq(dev, gpio_to_irq(pdata->flt),
  270. NULL, max8903_fault,
  271. IRQF_TRIGGER_FALLING |
  272. IRQF_TRIGGER_RISING,
  273. "MAX8903 Fault", data);
  274. if (ret) {
  275. dev_err(dev, "Cannot request irq %d for Fault (%d)\n",
  276. gpio_to_irq(pdata->flt), ret);
  277. return ret;
  278. }
  279. }
  280. return 0;
  281. }
  282. static struct platform_driver max8903_driver = {
  283. .probe = max8903_probe,
  284. .driver = {
  285. .name = "max8903-charger",
  286. },
  287. };
  288. module_platform_driver(max8903_driver);
  289. MODULE_LICENSE("GPL");
  290. MODULE_DESCRIPTION("MAX8903 Charger Driver");
  291. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  292. MODULE_ALIAS("platform:max8903-charger");