da9150-charger.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * DA9150 Charger Driver
  3. *
  4. * Copyright (c) 2014 Dialog Semiconductor
  5. *
  6. * Author: Adam Thomson <Adam.Thomson.Opensource@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/power_supply.h>
  21. #include <linux/notifier.h>
  22. #include <linux/usb/phy.h>
  23. #include <linux/iio/consumer.h>
  24. #include <linux/mfd/da9150/core.h>
  25. #include <linux/mfd/da9150/registers.h>
  26. /* Private data */
  27. struct da9150_charger {
  28. struct da9150 *da9150;
  29. struct device *dev;
  30. struct power_supply *usb;
  31. struct power_supply *battery;
  32. struct power_supply *supply_online;
  33. struct usb_phy *usb_phy;
  34. struct notifier_block otg_nb;
  35. struct work_struct otg_work;
  36. unsigned long usb_event;
  37. struct iio_channel *ibus_chan;
  38. struct iio_channel *vbus_chan;
  39. struct iio_channel *tjunc_chan;
  40. struct iio_channel *vbat_chan;
  41. };
  42. static inline int da9150_charger_supply_online(struct da9150_charger *charger,
  43. struct power_supply *psy,
  44. union power_supply_propval *val)
  45. {
  46. val->intval = (psy == charger->supply_online) ? 1 : 0;
  47. return 0;
  48. }
  49. /* Charger Properties */
  50. static int da9150_charger_vbus_voltage_now(struct da9150_charger *charger,
  51. union power_supply_propval *val)
  52. {
  53. int v_val, ret;
  54. /* Read processed value - mV units */
  55. ret = iio_read_channel_processed(charger->vbus_chan, &v_val);
  56. if (ret < 0)
  57. return ret;
  58. /* Convert voltage to expected uV units */
  59. val->intval = v_val * 1000;
  60. return 0;
  61. }
  62. static int da9150_charger_ibus_current_avg(struct da9150_charger *charger,
  63. union power_supply_propval *val)
  64. {
  65. int i_val, ret;
  66. /* Read processed value - mA units */
  67. ret = iio_read_channel_processed(charger->ibus_chan, &i_val);
  68. if (ret < 0)
  69. return ret;
  70. /* Convert current to expected uA units */
  71. val->intval = i_val * 1000;
  72. return 0;
  73. }
  74. static int da9150_charger_tjunc_temp(struct da9150_charger *charger,
  75. union power_supply_propval *val)
  76. {
  77. int t_val, ret;
  78. /* Read processed value - 0.001 degrees C units */
  79. ret = iio_read_channel_processed(charger->tjunc_chan, &t_val);
  80. if (ret < 0)
  81. return ret;
  82. /* Convert temp to expect 0.1 degrees C units */
  83. val->intval = t_val / 100;
  84. return 0;
  85. }
  86. static enum power_supply_property da9150_charger_props[] = {
  87. POWER_SUPPLY_PROP_ONLINE,
  88. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  89. POWER_SUPPLY_PROP_CURRENT_AVG,
  90. POWER_SUPPLY_PROP_TEMP,
  91. };
  92. static int da9150_charger_get_prop(struct power_supply *psy,
  93. enum power_supply_property psp,
  94. union power_supply_propval *val)
  95. {
  96. struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent);
  97. int ret;
  98. switch (psp) {
  99. case POWER_SUPPLY_PROP_ONLINE:
  100. ret = da9150_charger_supply_online(charger, psy, val);
  101. break;
  102. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  103. ret = da9150_charger_vbus_voltage_now(charger, val);
  104. break;
  105. case POWER_SUPPLY_PROP_CURRENT_AVG:
  106. ret = da9150_charger_ibus_current_avg(charger, val);
  107. break;
  108. case POWER_SUPPLY_PROP_TEMP:
  109. ret = da9150_charger_tjunc_temp(charger, val);
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. break;
  114. }
  115. return ret;
  116. }
  117. /* Battery Properties */
  118. static int da9150_charger_battery_status(struct da9150_charger *charger,
  119. union power_supply_propval *val)
  120. {
  121. u8 reg;
  122. /* Check to see if battery is discharging */
  123. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H);
  124. if (((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_OFF) ||
  125. ((reg & DA9150_VBUS_STAT_MASK) == DA9150_VBUS_STAT_WAIT)) {
  126. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  127. return 0;
  128. }
  129. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
  130. /* Now check for other states */
  131. switch (reg & DA9150_CHG_STAT_MASK) {
  132. case DA9150_CHG_STAT_ACT:
  133. case DA9150_CHG_STAT_PRE:
  134. case DA9150_CHG_STAT_CC:
  135. case DA9150_CHG_STAT_CV:
  136. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  137. break;
  138. case DA9150_CHG_STAT_OFF:
  139. case DA9150_CHG_STAT_SUSP:
  140. case DA9150_CHG_STAT_TEMP:
  141. case DA9150_CHG_STAT_TIME:
  142. case DA9150_CHG_STAT_BAT:
  143. val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
  144. break;
  145. case DA9150_CHG_STAT_FULL:
  146. val->intval = POWER_SUPPLY_STATUS_FULL;
  147. break;
  148. default:
  149. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  150. break;
  151. }
  152. return 0;
  153. }
  154. static int da9150_charger_battery_health(struct da9150_charger *charger,
  155. union power_supply_propval *val)
  156. {
  157. u8 reg;
  158. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
  159. /* Check if temperature limit reached */
  160. switch (reg & DA9150_CHG_TEMP_MASK) {
  161. case DA9150_CHG_TEMP_UNDER:
  162. val->intval = POWER_SUPPLY_HEALTH_COLD;
  163. return 0;
  164. case DA9150_CHG_TEMP_OVER:
  165. val->intval = POWER_SUPPLY_HEALTH_OVERHEAT;
  166. return 0;
  167. default:
  168. break;
  169. }
  170. /* Check for other health states */
  171. switch (reg & DA9150_CHG_STAT_MASK) {
  172. case DA9150_CHG_STAT_ACT:
  173. case DA9150_CHG_STAT_PRE:
  174. val->intval = POWER_SUPPLY_HEALTH_DEAD;
  175. break;
  176. case DA9150_CHG_STAT_TIME:
  177. val->intval = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
  178. break;
  179. default:
  180. val->intval = POWER_SUPPLY_HEALTH_GOOD;
  181. break;
  182. }
  183. return 0;
  184. }
  185. static int da9150_charger_battery_present(struct da9150_charger *charger,
  186. union power_supply_propval *val)
  187. {
  188. u8 reg;
  189. /* Check if battery present or removed */
  190. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
  191. if ((reg & DA9150_CHG_STAT_MASK) == DA9150_CHG_STAT_BAT)
  192. val->intval = 0;
  193. else
  194. val->intval = 1;
  195. return 0;
  196. }
  197. static int da9150_charger_battery_charge_type(struct da9150_charger *charger,
  198. union power_supply_propval *val)
  199. {
  200. u8 reg;
  201. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_J);
  202. switch (reg & DA9150_CHG_STAT_MASK) {
  203. case DA9150_CHG_STAT_CC:
  204. val->intval = POWER_SUPPLY_CHARGE_TYPE_FAST;
  205. break;
  206. case DA9150_CHG_STAT_ACT:
  207. case DA9150_CHG_STAT_PRE:
  208. case DA9150_CHG_STAT_CV:
  209. val->intval = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  210. break;
  211. default:
  212. val->intval = POWER_SUPPLY_CHARGE_TYPE_NONE;
  213. break;
  214. }
  215. return 0;
  216. }
  217. static int da9150_charger_battery_voltage_min(struct da9150_charger *charger,
  218. union power_supply_propval *val)
  219. {
  220. u8 reg;
  221. reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_C);
  222. /* Value starts at 2500 mV, 50 mV increments, presented in uV */
  223. val->intval = ((reg & DA9150_CHG_VFAULT_MASK) * 50000) + 2500000;
  224. return 0;
  225. }
  226. static int da9150_charger_battery_voltage_now(struct da9150_charger *charger,
  227. union power_supply_propval *val)
  228. {
  229. int v_val, ret;
  230. /* Read processed value - mV units */
  231. ret = iio_read_channel_processed(charger->vbat_chan, &v_val);
  232. if (ret < 0)
  233. return ret;
  234. val->intval = v_val * 1000;
  235. return 0;
  236. }
  237. static int da9150_charger_battery_current_max(struct da9150_charger *charger,
  238. union power_supply_propval *val)
  239. {
  240. int reg;
  241. reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_D);
  242. /* 25mA increments */
  243. val->intval = reg * 25000;
  244. return 0;
  245. }
  246. static int da9150_charger_battery_voltage_max(struct da9150_charger *charger,
  247. union power_supply_propval *val)
  248. {
  249. u8 reg;
  250. reg = da9150_reg_read(charger->da9150, DA9150_PPR_CHGCTRL_B);
  251. /* Value starts at 3650 mV, 25 mV increments, presented in uV */
  252. val->intval = ((reg & DA9150_CHG_VBAT_MASK) * 25000) + 3650000;
  253. return 0;
  254. }
  255. static enum power_supply_property da9150_charger_bat_props[] = {
  256. POWER_SUPPLY_PROP_STATUS,
  257. POWER_SUPPLY_PROP_ONLINE,
  258. POWER_SUPPLY_PROP_HEALTH,
  259. POWER_SUPPLY_PROP_PRESENT,
  260. POWER_SUPPLY_PROP_CHARGE_TYPE,
  261. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  262. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  263. POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
  264. POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
  265. };
  266. static int da9150_charger_battery_get_prop(struct power_supply *psy,
  267. enum power_supply_property psp,
  268. union power_supply_propval *val)
  269. {
  270. struct da9150_charger *charger = dev_get_drvdata(psy->dev.parent);
  271. int ret;
  272. switch (psp) {
  273. case POWER_SUPPLY_PROP_STATUS:
  274. ret = da9150_charger_battery_status(charger, val);
  275. break;
  276. case POWER_SUPPLY_PROP_ONLINE:
  277. ret = da9150_charger_supply_online(charger, psy, val);
  278. break;
  279. case POWER_SUPPLY_PROP_HEALTH:
  280. ret = da9150_charger_battery_health(charger, val);
  281. break;
  282. case POWER_SUPPLY_PROP_PRESENT:
  283. ret = da9150_charger_battery_present(charger, val);
  284. break;
  285. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  286. ret = da9150_charger_battery_charge_type(charger, val);
  287. break;
  288. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  289. ret = da9150_charger_battery_voltage_min(charger, val);
  290. break;
  291. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  292. ret = da9150_charger_battery_voltage_now(charger, val);
  293. break;
  294. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
  295. ret = da9150_charger_battery_current_max(charger, val);
  296. break;
  297. case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
  298. ret = da9150_charger_battery_voltage_max(charger, val);
  299. break;
  300. default:
  301. ret = -EINVAL;
  302. break;
  303. }
  304. return ret;
  305. }
  306. static irqreturn_t da9150_charger_chg_irq(int irq, void *data)
  307. {
  308. struct da9150_charger *charger = data;
  309. power_supply_changed(charger->battery);
  310. return IRQ_HANDLED;
  311. }
  312. static irqreturn_t da9150_charger_tjunc_irq(int irq, void *data)
  313. {
  314. struct da9150_charger *charger = data;
  315. /* Nothing we can really do except report this. */
  316. dev_crit(charger->dev, "TJunc over temperature!!!\n");
  317. power_supply_changed(charger->usb);
  318. return IRQ_HANDLED;
  319. }
  320. static irqreturn_t da9150_charger_vfault_irq(int irq, void *data)
  321. {
  322. struct da9150_charger *charger = data;
  323. /* Nothing we can really do except report this. */
  324. dev_crit(charger->dev, "VSYS under voltage!!!\n");
  325. power_supply_changed(charger->usb);
  326. power_supply_changed(charger->battery);
  327. return IRQ_HANDLED;
  328. }
  329. static irqreturn_t da9150_charger_vbus_irq(int irq, void *data)
  330. {
  331. struct da9150_charger *charger = data;
  332. u8 reg;
  333. reg = da9150_reg_read(charger->da9150, DA9150_STATUS_H);
  334. /* Charger plugged in or battery only */
  335. switch (reg & DA9150_VBUS_STAT_MASK) {
  336. case DA9150_VBUS_STAT_OFF:
  337. case DA9150_VBUS_STAT_WAIT:
  338. charger->supply_online = charger->battery;
  339. break;
  340. case DA9150_VBUS_STAT_CHG:
  341. charger->supply_online = charger->usb;
  342. break;
  343. default:
  344. dev_warn(charger->dev, "Unknown VBUS state - reg = 0x%x\n",
  345. reg);
  346. charger->supply_online = NULL;
  347. break;
  348. }
  349. power_supply_changed(charger->usb);
  350. power_supply_changed(charger->battery);
  351. return IRQ_HANDLED;
  352. }
  353. static void da9150_charger_otg_work(struct work_struct *data)
  354. {
  355. struct da9150_charger *charger =
  356. container_of(data, struct da9150_charger, otg_work);
  357. switch (charger->usb_event) {
  358. case USB_EVENT_ID:
  359. /* Enable OTG Boost */
  360. da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A,
  361. DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_OTG);
  362. break;
  363. case USB_EVENT_NONE:
  364. /* Revert to charge mode */
  365. power_supply_changed(charger->usb);
  366. power_supply_changed(charger->battery);
  367. da9150_set_bits(charger->da9150, DA9150_PPR_BKCTRL_A,
  368. DA9150_VBUS_MODE_MASK, DA9150_VBUS_MODE_CHG);
  369. break;
  370. }
  371. }
  372. static int da9150_charger_otg_ncb(struct notifier_block *nb, unsigned long val,
  373. void *priv)
  374. {
  375. struct da9150_charger *charger =
  376. container_of(nb, struct da9150_charger, otg_nb);
  377. dev_dbg(charger->dev, "DA9150 OTG notify %lu\n", val);
  378. charger->usb_event = val;
  379. schedule_work(&charger->otg_work);
  380. return NOTIFY_OK;
  381. }
  382. static int da9150_charger_register_irq(struct platform_device *pdev,
  383. irq_handler_t handler,
  384. const char *irq_name)
  385. {
  386. struct device *dev = &pdev->dev;
  387. struct da9150_charger *charger = platform_get_drvdata(pdev);
  388. int irq, ret;
  389. irq = platform_get_irq_byname(pdev, irq_name);
  390. if (irq < 0) {
  391. dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq);
  392. return irq;
  393. }
  394. ret = request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, irq_name,
  395. charger);
  396. if (ret)
  397. dev_err(dev, "Failed to request IRQ %d: %d\n", irq, ret);
  398. return ret;
  399. }
  400. static void da9150_charger_unregister_irq(struct platform_device *pdev,
  401. const char *irq_name)
  402. {
  403. struct device *dev = &pdev->dev;
  404. struct da9150_charger *charger = platform_get_drvdata(pdev);
  405. int irq;
  406. irq = platform_get_irq_byname(pdev, irq_name);
  407. if (irq < 0) {
  408. dev_err(dev, "Failed to get IRQ CHG_STATUS: %d\n", irq);
  409. return;
  410. }
  411. free_irq(irq, charger);
  412. }
  413. static const struct power_supply_desc usb_desc = {
  414. .name = "da9150-usb",
  415. .type = POWER_SUPPLY_TYPE_USB,
  416. .properties = da9150_charger_props,
  417. .num_properties = ARRAY_SIZE(da9150_charger_props),
  418. .get_property = da9150_charger_get_prop,
  419. };
  420. static const struct power_supply_desc battery_desc = {
  421. .name = "da9150-battery",
  422. .type = POWER_SUPPLY_TYPE_BATTERY,
  423. .properties = da9150_charger_bat_props,
  424. .num_properties = ARRAY_SIZE(da9150_charger_bat_props),
  425. .get_property = da9150_charger_battery_get_prop,
  426. };
  427. static int da9150_charger_probe(struct platform_device *pdev)
  428. {
  429. struct device *dev = &pdev->dev;
  430. struct da9150 *da9150 = dev_get_drvdata(dev->parent);
  431. struct da9150_charger *charger;
  432. u8 reg;
  433. int ret;
  434. charger = devm_kzalloc(dev, sizeof(struct da9150_charger), GFP_KERNEL);
  435. if (!charger)
  436. return -ENOMEM;
  437. platform_set_drvdata(pdev, charger);
  438. charger->da9150 = da9150;
  439. charger->dev = dev;
  440. /* Acquire ADC channels */
  441. charger->ibus_chan = iio_channel_get(dev, "CHAN_IBUS");
  442. if (IS_ERR(charger->ibus_chan)) {
  443. ret = PTR_ERR(charger->ibus_chan);
  444. goto ibus_chan_fail;
  445. }
  446. charger->vbus_chan = iio_channel_get(dev, "CHAN_VBUS");
  447. if (IS_ERR(charger->vbus_chan)) {
  448. ret = PTR_ERR(charger->vbus_chan);
  449. goto vbus_chan_fail;
  450. }
  451. charger->tjunc_chan = iio_channel_get(dev, "CHAN_TJUNC");
  452. if (IS_ERR(charger->tjunc_chan)) {
  453. ret = PTR_ERR(charger->tjunc_chan);
  454. goto tjunc_chan_fail;
  455. }
  456. charger->vbat_chan = iio_channel_get(dev, "CHAN_VBAT");
  457. if (IS_ERR(charger->vbat_chan)) {
  458. ret = PTR_ERR(charger->vbat_chan);
  459. goto vbat_chan_fail;
  460. }
  461. /* Register power supplies */
  462. charger->usb = power_supply_register(dev, &usb_desc, NULL);
  463. if (IS_ERR(charger->usb)) {
  464. ret = PTR_ERR(charger->usb);
  465. goto usb_fail;
  466. }
  467. charger->battery = power_supply_register(dev, &battery_desc, NULL);
  468. if (IS_ERR(charger->battery)) {
  469. ret = PTR_ERR(charger->battery);
  470. goto battery_fail;
  471. }
  472. /* Get initial online supply */
  473. reg = da9150_reg_read(da9150, DA9150_STATUS_H);
  474. switch (reg & DA9150_VBUS_STAT_MASK) {
  475. case DA9150_VBUS_STAT_OFF:
  476. case DA9150_VBUS_STAT_WAIT:
  477. charger->supply_online = charger->battery;
  478. break;
  479. case DA9150_VBUS_STAT_CHG:
  480. charger->supply_online = charger->usb;
  481. break;
  482. default:
  483. dev_warn(dev, "Unknown VBUS state - reg = 0x%x\n", reg);
  484. charger->supply_online = NULL;
  485. break;
  486. }
  487. /* Setup OTG reporting & configuration */
  488. charger->usb_phy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  489. if (!IS_ERR_OR_NULL(charger->usb_phy)) {
  490. INIT_WORK(&charger->otg_work, da9150_charger_otg_work);
  491. charger->otg_nb.notifier_call = da9150_charger_otg_ncb;
  492. usb_register_notifier(charger->usb_phy, &charger->otg_nb);
  493. }
  494. /* Register IRQs */
  495. ret = da9150_charger_register_irq(pdev, da9150_charger_chg_irq,
  496. "CHG_STATUS");
  497. if (ret < 0)
  498. goto chg_irq_fail;
  499. ret = da9150_charger_register_irq(pdev, da9150_charger_tjunc_irq,
  500. "CHG_TJUNC");
  501. if (ret < 0)
  502. goto tjunc_irq_fail;
  503. ret = da9150_charger_register_irq(pdev, da9150_charger_vfault_irq,
  504. "CHG_VFAULT");
  505. if (ret < 0)
  506. goto vfault_irq_fail;
  507. ret = da9150_charger_register_irq(pdev, da9150_charger_vbus_irq,
  508. "CHG_VBUS");
  509. if (ret < 0)
  510. goto vbus_irq_fail;
  511. return 0;
  512. vbus_irq_fail:
  513. da9150_charger_unregister_irq(pdev, "CHG_VFAULT");
  514. vfault_irq_fail:
  515. da9150_charger_unregister_irq(pdev, "CHG_TJUNC");
  516. tjunc_irq_fail:
  517. da9150_charger_unregister_irq(pdev, "CHG_STATUS");
  518. chg_irq_fail:
  519. if (!IS_ERR_OR_NULL(charger->usb_phy))
  520. usb_unregister_notifier(charger->usb_phy, &charger->otg_nb);
  521. battery_fail:
  522. power_supply_unregister(charger->usb);
  523. usb_fail:
  524. iio_channel_release(charger->vbat_chan);
  525. vbat_chan_fail:
  526. iio_channel_release(charger->tjunc_chan);
  527. tjunc_chan_fail:
  528. iio_channel_release(charger->vbus_chan);
  529. vbus_chan_fail:
  530. iio_channel_release(charger->ibus_chan);
  531. ibus_chan_fail:
  532. return ret;
  533. }
  534. static int da9150_charger_remove(struct platform_device *pdev)
  535. {
  536. struct da9150_charger *charger = platform_get_drvdata(pdev);
  537. int irq;
  538. /* Make sure IRQs are released before unregistering power supplies */
  539. irq = platform_get_irq_byname(pdev, "CHG_VBUS");
  540. free_irq(irq, charger);
  541. irq = platform_get_irq_byname(pdev, "CHG_VFAULT");
  542. free_irq(irq, charger);
  543. irq = platform_get_irq_byname(pdev, "CHG_TJUNC");
  544. free_irq(irq, charger);
  545. irq = platform_get_irq_byname(pdev, "CHG_STATUS");
  546. free_irq(irq, charger);
  547. if (!IS_ERR_OR_NULL(charger->usb_phy))
  548. usb_unregister_notifier(charger->usb_phy, &charger->otg_nb);
  549. power_supply_unregister(charger->battery);
  550. power_supply_unregister(charger->usb);
  551. /* Release ADC channels */
  552. iio_channel_release(charger->ibus_chan);
  553. iio_channel_release(charger->vbus_chan);
  554. iio_channel_release(charger->tjunc_chan);
  555. iio_channel_release(charger->vbat_chan);
  556. return 0;
  557. }
  558. static struct platform_driver da9150_charger_driver = {
  559. .driver = {
  560. .name = "da9150-charger",
  561. },
  562. .probe = da9150_charger_probe,
  563. .remove = da9150_charger_remove,
  564. };
  565. module_platform_driver(da9150_charger_driver);
  566. MODULE_DESCRIPTION("Charger Driver for DA9150");
  567. MODULE_AUTHOR("Adam Thomson <Adam.Thomson.Opensource@diasemi.com>");
  568. MODULE_LICENSE("GPL");