cpcap-charger.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Motorola CPCAP PMIC battery charger driver
  3. *
  4. * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
  5. *
  6. * Rewritten for Linux power framework with some parts based on
  7. * on earlier driver found in the Motorola Linux kernel:
  8. *
  9. * Copyright (C) 2009-2010 Motorola, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/atomic.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/notifier.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/power_supply.h>
  31. #include <linux/regmap.h>
  32. #include <linux/gpio/consumer.h>
  33. #include <linux/usb/phy_companion.h>
  34. #include <linux/phy/omap_usb.h>
  35. #include <linux/usb/otg.h>
  36. #include <linux/iio/consumer.h>
  37. #include <linux/mfd/motorola-cpcap.h>
  38. /*
  39. * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
  40. * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
  41. * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
  42. * and values for CPCAP are different, but some of the internal components seem
  43. * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
  44. * bits that seem to describe the CRM register.
  45. */
  46. #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
  47. #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
  48. #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
  49. #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
  50. #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
  51. #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
  52. #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
  53. #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
  54. #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
  55. #define CPCAP_REG_CRM_VCHRG2 BIT(6)
  56. #define CPCAP_REG_CRM_VCHRG1 BIT(5)
  57. #define CPCAP_REG_CRM_VCHRG0 BIT(4)
  58. #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
  59. #define CPCAP_REG_CRM_ICHRG2 BIT(2)
  60. #define CPCAP_REG_CRM_ICHRG1 BIT(1)
  61. #define CPCAP_REG_CRM_ICHRG0 BIT(0)
  62. /* CPCAP_REG_CRM trickle charge voltages */
  63. #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
  64. #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
  65. #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
  66. #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
  67. #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
  68. /*
  69. * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
  70. * Note that these register bits don't match MC13783UG.pdf VCHRG
  71. * register bits.
  72. */
  73. #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
  74. #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
  75. #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
  76. #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
  77. #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
  78. #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
  79. #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
  80. #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
  81. #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
  82. #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
  83. #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
  84. #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
  85. #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
  86. #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
  87. #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
  88. #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
  89. #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
  90. /*
  91. * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
  92. * values in "Table 8-3. Charge Path Regulator Current Limit
  93. * Characteristics" for the nominal values.
  94. */
  95. #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
  96. #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
  97. #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
  98. #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
  99. #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
  100. #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
  101. #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
  102. #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
  103. #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
  104. #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
  105. #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
  106. #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
  107. #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
  108. #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
  109. #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
  110. #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
  111. #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
  112. enum {
  113. CPCAP_CHARGER_IIO_BATTDET,
  114. CPCAP_CHARGER_IIO_VOLTAGE,
  115. CPCAP_CHARGER_IIO_VBUS,
  116. CPCAP_CHARGER_IIO_CHRG_CURRENT,
  117. CPCAP_CHARGER_IIO_BATT_CURRENT,
  118. CPCAP_CHARGER_IIO_NR,
  119. };
  120. struct cpcap_charger_ddata {
  121. struct device *dev;
  122. struct regmap *reg;
  123. struct list_head irq_list;
  124. struct delayed_work detect_work;
  125. struct delayed_work vbus_work;
  126. struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
  127. struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
  128. struct power_supply *usb;
  129. struct phy_companion comparator; /* For USB VBUS */
  130. bool vbus_enabled;
  131. atomic_t active;
  132. int status;
  133. };
  134. struct cpcap_interrupt_desc {
  135. int irq;
  136. struct list_head node;
  137. const char *name;
  138. };
  139. struct cpcap_charger_ints_state {
  140. bool chrg_det;
  141. bool rvrs_chrg;
  142. bool vbusov;
  143. bool chrg_se1b;
  144. bool rvrs_mode;
  145. bool chrgcurr1;
  146. bool vbusvld;
  147. bool battdetb;
  148. };
  149. static enum power_supply_property cpcap_charger_props[] = {
  150. POWER_SUPPLY_PROP_STATUS,
  151. POWER_SUPPLY_PROP_ONLINE,
  152. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  153. POWER_SUPPLY_PROP_CURRENT_NOW,
  154. };
  155. static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
  156. {
  157. struct iio_channel *channel;
  158. int error, value;
  159. channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
  160. error = iio_read_channel_raw(channel, &value);
  161. if (error < 0) {
  162. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  163. return false;
  164. }
  165. return value == 1;
  166. }
  167. static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
  168. {
  169. struct iio_channel *channel;
  170. int error, value = 0;
  171. channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
  172. error = iio_read_channel_processed(channel, &value);
  173. if (error < 0) {
  174. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  175. return 0;
  176. }
  177. return value;
  178. }
  179. static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
  180. {
  181. struct iio_channel *channel;
  182. int error, value = 0;
  183. channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
  184. error = iio_read_channel_processed(channel, &value);
  185. if (error < 0) {
  186. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  187. return 0;
  188. }
  189. return value;
  190. }
  191. static int cpcap_charger_get_property(struct power_supply *psy,
  192. enum power_supply_property psp,
  193. union power_supply_propval *val)
  194. {
  195. struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
  196. switch (psp) {
  197. case POWER_SUPPLY_PROP_STATUS:
  198. val->intval = ddata->status;
  199. break;
  200. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  201. if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
  202. val->intval = cpcap_charger_get_charge_voltage(ddata) *
  203. 1000;
  204. else
  205. val->intval = 0;
  206. break;
  207. case POWER_SUPPLY_PROP_CURRENT_NOW:
  208. if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
  209. val->intval = cpcap_charger_get_charge_current(ddata) *
  210. 1000;
  211. else
  212. val->intval = 0;
  213. break;
  214. case POWER_SUPPLY_PROP_ONLINE:
  215. val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
  216. break;
  217. default:
  218. return -EINVAL;
  219. }
  220. return 0;
  221. }
  222. static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
  223. bool enabled)
  224. {
  225. if (!ddata->gpio[0])
  226. return;
  227. gpiod_set_value(ddata->gpio[0], enabled);
  228. }
  229. static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
  230. bool enabled)
  231. {
  232. if (!ddata->gpio[1])
  233. return;
  234. gpiod_set_value(ddata->gpio[1], enabled);
  235. }
  236. static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
  237. int max_voltage, int charge_current,
  238. int trickle_current)
  239. {
  240. bool enable;
  241. int error;
  242. enable = (charge_current || trickle_current);
  243. dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
  244. if (!enable) {
  245. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  246. 0x3fff,
  247. CPCAP_REG_CRM_FET_OVRD |
  248. CPCAP_REG_CRM_FET_CTRL);
  249. if (error) {
  250. ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
  251. goto out_err;
  252. }
  253. ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
  254. return 0;
  255. }
  256. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
  257. CPCAP_REG_CRM_CHRG_LED_EN |
  258. trickle_current |
  259. CPCAP_REG_CRM_FET_OVRD |
  260. CPCAP_REG_CRM_FET_CTRL |
  261. max_voltage |
  262. charge_current);
  263. if (error) {
  264. ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
  265. goto out_err;
  266. }
  267. ddata->status = POWER_SUPPLY_STATUS_CHARGING;
  268. return 0;
  269. out_err:
  270. dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
  271. return error;
  272. }
  273. static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
  274. {
  275. int error, value = 0;
  276. struct iio_channel *channel =
  277. ddata->channels[CPCAP_CHARGER_IIO_VBUS];
  278. error = iio_read_channel_processed(channel, &value);
  279. if (error >= 0)
  280. return value > 3900 ? true : false;
  281. dev_err(ddata->dev, "error reading VBUS: %i\n", error);
  282. return false;
  283. }
  284. /* VBUS control functions for the USB PHY companion */
  285. static void cpcap_charger_vbus_work(struct work_struct *work)
  286. {
  287. struct cpcap_charger_ddata *ddata;
  288. bool vbus = false;
  289. int error;
  290. ddata = container_of(work, struct cpcap_charger_ddata,
  291. vbus_work.work);
  292. if (ddata->vbus_enabled) {
  293. vbus = cpcap_charger_vbus_valid(ddata);
  294. if (vbus) {
  295. dev_info(ddata->dev, "VBUS already provided\n");
  296. return;
  297. }
  298. cpcap_charger_set_cable_path(ddata, false);
  299. cpcap_charger_set_inductive_path(ddata, false);
  300. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  301. if (error)
  302. goto out_err;
  303. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  304. CPCAP_REG_CRM_RVRSMODE,
  305. CPCAP_REG_CRM_RVRSMODE);
  306. if (error)
  307. goto out_err;
  308. } else {
  309. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  310. CPCAP_REG_CRM_RVRSMODE, 0);
  311. if (error)
  312. goto out_err;
  313. cpcap_charger_set_cable_path(ddata, true);
  314. cpcap_charger_set_inductive_path(ddata, true);
  315. }
  316. return;
  317. out_err:
  318. dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
  319. ddata->vbus_enabled ? "enable" : "disable", error);
  320. }
  321. static int cpcap_charger_set_vbus(struct phy_companion *comparator,
  322. bool enabled)
  323. {
  324. struct cpcap_charger_ddata *ddata =
  325. container_of(comparator, struct cpcap_charger_ddata,
  326. comparator);
  327. ddata->vbus_enabled = enabled;
  328. schedule_delayed_work(&ddata->vbus_work, 0);
  329. return 0;
  330. }
  331. /* Charger interrupt handling functions */
  332. static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
  333. struct cpcap_charger_ints_state *s)
  334. {
  335. int val, error;
  336. error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
  337. if (error)
  338. return error;
  339. s->chrg_det = val & BIT(13);
  340. s->rvrs_chrg = val & BIT(12);
  341. s->vbusov = val & BIT(11);
  342. error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
  343. if (error)
  344. return error;
  345. s->chrg_se1b = val & BIT(13);
  346. s->rvrs_mode = val & BIT(6);
  347. s->chrgcurr1 = val & BIT(4);
  348. s->vbusvld = val & BIT(3);
  349. error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
  350. if (error)
  351. return error;
  352. s->battdetb = val & BIT(6);
  353. return 0;
  354. }
  355. static void cpcap_usb_detect(struct work_struct *work)
  356. {
  357. struct cpcap_charger_ddata *ddata;
  358. struct cpcap_charger_ints_state s;
  359. int error;
  360. ddata = container_of(work, struct cpcap_charger_ddata,
  361. detect_work.work);
  362. error = cpcap_charger_get_ints_state(ddata, &s);
  363. if (error)
  364. return;
  365. if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
  366. int max_current;
  367. if (cpcap_charger_battery_found(ddata))
  368. max_current = CPCAP_REG_CRM_ICHRG_1A596;
  369. else
  370. max_current = CPCAP_REG_CRM_ICHRG_0A532;
  371. error = cpcap_charger_set_state(ddata,
  372. CPCAP_REG_CRM_VCHRG_4V35,
  373. max_current, 0);
  374. if (error)
  375. goto out_err;
  376. } else {
  377. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  378. if (error)
  379. goto out_err;
  380. }
  381. power_supply_changed(ddata->usb);
  382. return;
  383. out_err:
  384. dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
  385. }
  386. static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
  387. {
  388. struct cpcap_charger_ddata *ddata = data;
  389. if (!atomic_read(&ddata->active))
  390. return IRQ_NONE;
  391. schedule_delayed_work(&ddata->detect_work, 0);
  392. return IRQ_HANDLED;
  393. }
  394. static int cpcap_usb_init_irq(struct platform_device *pdev,
  395. struct cpcap_charger_ddata *ddata,
  396. const char *name)
  397. {
  398. struct cpcap_interrupt_desc *d;
  399. int irq, error;
  400. irq = platform_get_irq_byname(pdev, name);
  401. if (!irq)
  402. return -ENODEV;
  403. error = devm_request_threaded_irq(ddata->dev, irq, NULL,
  404. cpcap_charger_irq_thread,
  405. IRQF_SHARED,
  406. name, ddata);
  407. if (error) {
  408. dev_err(ddata->dev, "could not get irq %s: %i\n",
  409. name, error);
  410. return error;
  411. }
  412. d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
  413. if (!d)
  414. return -ENOMEM;
  415. d->name = name;
  416. d->irq = irq;
  417. list_add(&d->node, &ddata->irq_list);
  418. return 0;
  419. }
  420. static const char * const cpcap_charger_irqs[] = {
  421. /* REG_INT_0 */
  422. "chrg_det", "rvrs_chrg",
  423. /* REG_INT1 */
  424. "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
  425. /* REG_INT_3 */
  426. "battdetb",
  427. };
  428. static int cpcap_usb_init_interrupts(struct platform_device *pdev,
  429. struct cpcap_charger_ddata *ddata)
  430. {
  431. int i, error;
  432. for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
  433. error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
  434. if (error)
  435. return error;
  436. }
  437. return 0;
  438. }
  439. static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
  440. {
  441. int i;
  442. for (i = 0; i < 2; i++) {
  443. ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
  444. i, GPIOD_OUT_HIGH);
  445. if (IS_ERR(ddata->gpio[i])) {
  446. dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
  447. i, PTR_ERR(ddata->gpio[i]));
  448. ddata->gpio[i] = NULL;
  449. }
  450. }
  451. }
  452. static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
  453. {
  454. const char * const names[CPCAP_CHARGER_IIO_NR] = {
  455. "battdetb", "battp", "vbus", "chg_isense", "batti",
  456. };
  457. int error, i;
  458. for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
  459. ddata->channels[i] = devm_iio_channel_get(ddata->dev,
  460. names[i]);
  461. if (IS_ERR(ddata->channels[i])) {
  462. error = PTR_ERR(ddata->channels[i]);
  463. goto out_err;
  464. }
  465. if (!ddata->channels[i]->indio_dev) {
  466. error = -ENXIO;
  467. goto out_err;
  468. }
  469. }
  470. return 0;
  471. out_err:
  472. dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
  473. error);
  474. return error;
  475. }
  476. static const struct power_supply_desc cpcap_charger_usb_desc = {
  477. .name = "usb",
  478. .type = POWER_SUPPLY_TYPE_USB,
  479. .properties = cpcap_charger_props,
  480. .num_properties = ARRAY_SIZE(cpcap_charger_props),
  481. .get_property = cpcap_charger_get_property,
  482. };
  483. #ifdef CONFIG_OF
  484. static const struct of_device_id cpcap_charger_id_table[] = {
  485. {
  486. .compatible = "motorola,mapphone-cpcap-charger",
  487. },
  488. {},
  489. };
  490. MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
  491. #endif
  492. static int cpcap_charger_probe(struct platform_device *pdev)
  493. {
  494. struct cpcap_charger_ddata *ddata;
  495. const struct of_device_id *of_id;
  496. struct power_supply_config psy_cfg = {};
  497. int error;
  498. of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
  499. &pdev->dev);
  500. if (!of_id)
  501. return -EINVAL;
  502. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  503. if (!ddata)
  504. return -ENOMEM;
  505. ddata->dev = &pdev->dev;
  506. ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
  507. if (!ddata->reg)
  508. return -ENODEV;
  509. INIT_LIST_HEAD(&ddata->irq_list);
  510. INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
  511. INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
  512. platform_set_drvdata(pdev, ddata);
  513. error = cpcap_charger_init_iio(ddata);
  514. if (error)
  515. return error;
  516. atomic_set(&ddata->active, 1);
  517. psy_cfg.of_node = pdev->dev.of_node;
  518. psy_cfg.drv_data = ddata;
  519. ddata->usb = devm_power_supply_register(ddata->dev,
  520. &cpcap_charger_usb_desc,
  521. &psy_cfg);
  522. if (IS_ERR(ddata->usb)) {
  523. error = PTR_ERR(ddata->usb);
  524. dev_err(ddata->dev, "failed to register USB charger: %i\n",
  525. error);
  526. return error;
  527. }
  528. error = cpcap_usb_init_interrupts(pdev, ddata);
  529. if (error)
  530. return error;
  531. ddata->comparator.set_vbus = cpcap_charger_set_vbus;
  532. error = omap_usb2_set_comparator(&ddata->comparator);
  533. if (error == -ENODEV) {
  534. dev_info(ddata->dev, "charger needs phy, deferring probe\n");
  535. return -EPROBE_DEFER;
  536. }
  537. cpcap_charger_init_optional_gpios(ddata);
  538. schedule_delayed_work(&ddata->detect_work, 0);
  539. return 0;
  540. }
  541. static int cpcap_charger_remove(struct platform_device *pdev)
  542. {
  543. struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
  544. int error;
  545. atomic_set(&ddata->active, 0);
  546. error = omap_usb2_set_comparator(NULL);
  547. if (error)
  548. dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
  549. error);
  550. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  551. if (error)
  552. dev_warn(ddata->dev, "could not clear charger: %i\n",
  553. error);
  554. cancel_delayed_work_sync(&ddata->vbus_work);
  555. cancel_delayed_work_sync(&ddata->detect_work);
  556. return 0;
  557. }
  558. static struct platform_driver cpcap_charger_driver = {
  559. .probe = cpcap_charger_probe,
  560. .driver = {
  561. .name = "cpcap-charger",
  562. .of_match_table = of_match_ptr(cpcap_charger_id_table),
  563. },
  564. .remove = cpcap_charger_remove,
  565. };
  566. module_platform_driver(cpcap_charger_driver);
  567. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  568. MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
  569. MODULE_LICENSE("GPL v2");
  570. MODULE_ALIAS("platform:cpcap-charger");