cpcap-charger.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. /* CPCAP_REG_CRM register bits */
  39. #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
  40. #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
  41. #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13)
  42. #define CPCAP_REG_CRM_RVRSMODE BIT(12)
  43. #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11)
  44. #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
  45. #define CPCAP_REG_CRM_FET_OVRD BIT(9)
  46. #define CPCAP_REG_CRM_FET_CTRL BIT(8)
  47. #define CPCAP_REG_CRM_VCHRG3 BIT(7)
  48. #define CPCAP_REG_CRM_VCHRG2 BIT(6)
  49. #define CPCAP_REG_CRM_VCHRG1 BIT(5)
  50. #define CPCAP_REG_CRM_VCHRG0 BIT(4)
  51. #define CPCAP_REG_CRM_ICHRG3 BIT(3)
  52. #define CPCAP_REG_CRM_ICHRG2 BIT(2)
  53. #define CPCAP_REG_CRM_ICHRG1 BIT(1)
  54. #define CPCAP_REG_CRM_ICHRG0 BIT(0)
  55. /* CPCAP_REG_CRM trickle charge voltages */
  56. #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
  57. #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
  58. #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
  59. #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
  60. #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
  61. /* CPCAP_REG_CRM charge voltages */
  62. #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
  63. #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
  64. #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
  65. #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x2)
  66. #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x3)
  67. #define CPCAP_REG_CRM_VCHRG_4V22 CPCAP_REG_CRM_VCHRG(0x4)
  68. #define CPCAP_REG_CRM_VCHRG_4V24 CPCAP_REG_CRM_VCHRG(0x5)
  69. #define CPCAP_REG_CRM_VCHRG_4V26 CPCAP_REG_CRM_VCHRG(0x6)
  70. #define CPCAP_REG_CRM_VCHRG_4V28 CPCAP_REG_CRM_VCHRG(0x7)
  71. #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x8)
  72. #define CPCAP_REG_CRM_VCHRG_4V32 CPCAP_REG_CRM_VCHRG(0x9)
  73. #define CPCAP_REG_CRM_VCHRG_4V34 CPCAP_REG_CRM_VCHRG(0xa)
  74. #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
  75. #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
  76. #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
  77. #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
  78. #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
  79. /* CPCAP_REG_CRM charge currents */
  80. #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
  81. #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
  82. #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
  83. #define CPCAP_REG_CRM_ICHRG_0A176 CPCAP_REG_CRM_ICHRG(0x2)
  84. #define CPCAP_REG_CRM_ICHRG_0A264 CPCAP_REG_CRM_ICHRG(0x3)
  85. #define CPCAP_REG_CRM_ICHRG_0A352 CPCAP_REG_CRM_ICHRG(0x4)
  86. #define CPCAP_REG_CRM_ICHRG_0A440 CPCAP_REG_CRM_ICHRG(0x5)
  87. #define CPCAP_REG_CRM_ICHRG_0A528 CPCAP_REG_CRM_ICHRG(0x6)
  88. #define CPCAP_REG_CRM_ICHRG_0A616 CPCAP_REG_CRM_ICHRG(0x7)
  89. #define CPCAP_REG_CRM_ICHRG_0A704 CPCAP_REG_CRM_ICHRG(0x8)
  90. #define CPCAP_REG_CRM_ICHRG_0A792 CPCAP_REG_CRM_ICHRG(0x9)
  91. #define CPCAP_REG_CRM_ICHRG_0A880 CPCAP_REG_CRM_ICHRG(0xa)
  92. #define CPCAP_REG_CRM_ICHRG_0A968 CPCAP_REG_CRM_ICHRG(0xb)
  93. #define CPCAP_REG_CRM_ICHRG_1A056 CPCAP_REG_CRM_ICHRG(0xc)
  94. #define CPCAP_REG_CRM_ICHRG_1A144 CPCAP_REG_CRM_ICHRG(0xd)
  95. #define CPCAP_REG_CRM_ICHRG_1A584 CPCAP_REG_CRM_ICHRG(0xe)
  96. #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
  97. enum {
  98. CPCAP_CHARGER_IIO_BATTDET,
  99. CPCAP_CHARGER_IIO_VOLTAGE,
  100. CPCAP_CHARGER_IIO_VBUS,
  101. CPCAP_CHARGER_IIO_CHRG_CURRENT,
  102. CPCAP_CHARGER_IIO_BATT_CURRENT,
  103. CPCAP_CHARGER_IIO_NR,
  104. };
  105. struct cpcap_charger_ddata {
  106. struct device *dev;
  107. struct regmap *reg;
  108. struct list_head irq_list;
  109. struct delayed_work detect_work;
  110. struct delayed_work vbus_work;
  111. struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
  112. struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
  113. struct power_supply *usb;
  114. struct phy_companion comparator; /* For USB VBUS */
  115. bool vbus_enabled;
  116. atomic_t active;
  117. int status;
  118. };
  119. struct cpcap_interrupt_desc {
  120. int irq;
  121. struct list_head node;
  122. const char *name;
  123. };
  124. struct cpcap_charger_ints_state {
  125. bool chrg_det;
  126. bool rvrs_chrg;
  127. bool vbusov;
  128. bool chrg_se1b;
  129. bool rvrs_mode;
  130. bool chrgcurr1;
  131. bool vbusvld;
  132. bool battdetb;
  133. };
  134. static enum power_supply_property cpcap_charger_props[] = {
  135. POWER_SUPPLY_PROP_STATUS,
  136. POWER_SUPPLY_PROP_ONLINE,
  137. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  138. POWER_SUPPLY_PROP_CURRENT_NOW,
  139. };
  140. static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
  141. {
  142. struct iio_channel *channel;
  143. int error, value;
  144. channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
  145. error = iio_read_channel_raw(channel, &value);
  146. if (error < 0) {
  147. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  148. return false;
  149. }
  150. return value == 1;
  151. }
  152. static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
  153. {
  154. struct iio_channel *channel;
  155. int error, value = 0;
  156. channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
  157. error = iio_read_channel_processed(channel, &value);
  158. if (error < 0) {
  159. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  160. return 0;
  161. }
  162. return value;
  163. }
  164. static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
  165. {
  166. struct iio_channel *channel;
  167. int error, value = 0;
  168. channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
  169. error = iio_read_channel_processed(channel, &value);
  170. if (error < 0) {
  171. dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
  172. return 0;
  173. }
  174. return value;
  175. }
  176. static int cpcap_charger_get_property(struct power_supply *psy,
  177. enum power_supply_property psp,
  178. union power_supply_propval *val)
  179. {
  180. struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
  181. switch (psp) {
  182. case POWER_SUPPLY_PROP_STATUS:
  183. val->intval = ddata->status;
  184. break;
  185. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  186. if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
  187. val->intval = cpcap_charger_get_charge_voltage(ddata) *
  188. 1000;
  189. else
  190. val->intval = 0;
  191. break;
  192. case POWER_SUPPLY_PROP_CURRENT_NOW:
  193. if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
  194. val->intval = cpcap_charger_get_charge_current(ddata) *
  195. 1000;
  196. else
  197. val->intval = 0;
  198. break;
  199. case POWER_SUPPLY_PROP_ONLINE:
  200. val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. return 0;
  206. }
  207. static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
  208. bool enabled)
  209. {
  210. if (!ddata->gpio[0])
  211. return;
  212. gpiod_set_value(ddata->gpio[0], enabled);
  213. }
  214. static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
  215. bool enabled)
  216. {
  217. if (!ddata->gpio[1])
  218. return;
  219. gpiod_set_value(ddata->gpio[1], enabled);
  220. }
  221. static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
  222. int max_voltage, int charge_current,
  223. int trickle_current)
  224. {
  225. bool enable;
  226. int error;
  227. enable = (charge_current || trickle_current);
  228. dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
  229. if (!enable) {
  230. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  231. 0x3fff,
  232. CPCAP_REG_CRM_FET_OVRD |
  233. CPCAP_REG_CRM_FET_CTRL);
  234. if (error) {
  235. ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
  236. goto out_err;
  237. }
  238. ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
  239. return 0;
  240. }
  241. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
  242. CPCAP_REG_CRM_CHRG_LED_EN |
  243. trickle_current |
  244. CPCAP_REG_CRM_FET_OVRD |
  245. CPCAP_REG_CRM_FET_CTRL |
  246. max_voltage |
  247. charge_current);
  248. if (error) {
  249. ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
  250. goto out_err;
  251. }
  252. ddata->status = POWER_SUPPLY_STATUS_CHARGING;
  253. return 0;
  254. out_err:
  255. dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
  256. return error;
  257. }
  258. static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
  259. {
  260. int error, value = 0;
  261. struct iio_channel *channel =
  262. ddata->channels[CPCAP_CHARGER_IIO_VBUS];
  263. error = iio_read_channel_processed(channel, &value);
  264. if (error >= 0)
  265. return value > 3900 ? true : false;
  266. dev_err(ddata->dev, "error reading VBUS: %i\n", error);
  267. return false;
  268. }
  269. /* VBUS control functions for the USB PHY companion */
  270. static void cpcap_charger_vbus_work(struct work_struct *work)
  271. {
  272. struct cpcap_charger_ddata *ddata;
  273. bool vbus = false;
  274. int error;
  275. ddata = container_of(work, struct cpcap_charger_ddata,
  276. vbus_work.work);
  277. if (ddata->vbus_enabled) {
  278. vbus = cpcap_charger_vbus_valid(ddata);
  279. if (vbus) {
  280. dev_info(ddata->dev, "VBUS already provided\n");
  281. return;
  282. }
  283. cpcap_charger_set_cable_path(ddata, false);
  284. cpcap_charger_set_inductive_path(ddata, false);
  285. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  286. if (error)
  287. goto out_err;
  288. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  289. CPCAP_REG_CRM_RVRSMODE,
  290. CPCAP_REG_CRM_RVRSMODE);
  291. if (error)
  292. goto out_err;
  293. } else {
  294. error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
  295. CPCAP_REG_CRM_RVRSMODE, 0);
  296. if (error)
  297. goto out_err;
  298. cpcap_charger_set_cable_path(ddata, true);
  299. cpcap_charger_set_inductive_path(ddata, true);
  300. }
  301. return;
  302. out_err:
  303. dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
  304. ddata->vbus_enabled ? "enable" : "disable", error);
  305. }
  306. static int cpcap_charger_set_vbus(struct phy_companion *comparator,
  307. bool enabled)
  308. {
  309. struct cpcap_charger_ddata *ddata =
  310. container_of(comparator, struct cpcap_charger_ddata,
  311. comparator);
  312. ddata->vbus_enabled = enabled;
  313. schedule_delayed_work(&ddata->vbus_work, 0);
  314. return 0;
  315. }
  316. /* Charger interrupt handling functions */
  317. static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
  318. struct cpcap_charger_ints_state *s)
  319. {
  320. int val, error;
  321. error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
  322. if (error)
  323. return error;
  324. s->chrg_det = val & BIT(13);
  325. s->rvrs_chrg = val & BIT(12);
  326. s->vbusov = val & BIT(11);
  327. error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
  328. if (error)
  329. return error;
  330. s->chrg_se1b = val & BIT(13);
  331. s->rvrs_mode = val & BIT(6);
  332. s->chrgcurr1 = val & BIT(4);
  333. s->vbusvld = val & BIT(3);
  334. error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
  335. if (error)
  336. return error;
  337. s->battdetb = val & BIT(6);
  338. return 0;
  339. }
  340. static void cpcap_usb_detect(struct work_struct *work)
  341. {
  342. struct cpcap_charger_ddata *ddata;
  343. struct cpcap_charger_ints_state s;
  344. int error;
  345. ddata = container_of(work, struct cpcap_charger_ddata,
  346. detect_work.work);
  347. error = cpcap_charger_get_ints_state(ddata, &s);
  348. if (error)
  349. return;
  350. if (cpcap_charger_vbus_valid(ddata) && s.chrgcurr1) {
  351. int max_current;
  352. if (cpcap_charger_battery_found(ddata))
  353. max_current = CPCAP_REG_CRM_ICHRG_1A584;
  354. else
  355. max_current = CPCAP_REG_CRM_ICHRG_0A528;
  356. error = cpcap_charger_set_state(ddata,
  357. CPCAP_REG_CRM_VCHRG_4V35,
  358. max_current, 0);
  359. if (error)
  360. goto out_err;
  361. } else {
  362. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  363. if (error)
  364. goto out_err;
  365. }
  366. return;
  367. out_err:
  368. dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
  369. }
  370. static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
  371. {
  372. struct cpcap_charger_ddata *ddata = data;
  373. if (!atomic_read(&ddata->active))
  374. return IRQ_NONE;
  375. schedule_delayed_work(&ddata->detect_work, 0);
  376. return IRQ_HANDLED;
  377. }
  378. static int cpcap_usb_init_irq(struct platform_device *pdev,
  379. struct cpcap_charger_ddata *ddata,
  380. const char *name)
  381. {
  382. struct cpcap_interrupt_desc *d;
  383. int irq, error;
  384. irq = platform_get_irq_byname(pdev, name);
  385. if (!irq)
  386. return -ENODEV;
  387. error = devm_request_threaded_irq(ddata->dev, irq, NULL,
  388. cpcap_charger_irq_thread,
  389. IRQF_SHARED,
  390. name, ddata);
  391. if (error) {
  392. dev_err(ddata->dev, "could not get irq %s: %i\n",
  393. name, error);
  394. return error;
  395. }
  396. d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
  397. if (!d)
  398. return -ENOMEM;
  399. d->name = name;
  400. d->irq = irq;
  401. list_add(&d->node, &ddata->irq_list);
  402. return 0;
  403. }
  404. static const char * const cpcap_charger_irqs[] = {
  405. /* REG_INT_0 */
  406. "chrg_det", "rvrs_chrg",
  407. /* REG_INT1 */
  408. "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr1", "vbusvld",
  409. /* REG_INT_3 */
  410. "battdetb",
  411. };
  412. static int cpcap_usb_init_interrupts(struct platform_device *pdev,
  413. struct cpcap_charger_ddata *ddata)
  414. {
  415. int i, error;
  416. for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
  417. error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
  418. if (error)
  419. return error;
  420. }
  421. return 0;
  422. }
  423. static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
  424. {
  425. int i;
  426. for (i = 0; i < 2; i++) {
  427. ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
  428. i, GPIOD_OUT_HIGH);
  429. if (IS_ERR(ddata->gpio[i])) {
  430. dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
  431. i, PTR_ERR(ddata->gpio[i]));
  432. ddata->gpio[i] = NULL;
  433. }
  434. }
  435. }
  436. static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
  437. {
  438. const char * const names[CPCAP_CHARGER_IIO_NR] = {
  439. "battdetb", "battp", "vbus", "chg_isense", "batti",
  440. };
  441. int error, i;
  442. for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
  443. ddata->channels[i] = devm_iio_channel_get(ddata->dev,
  444. names[i]);
  445. if (IS_ERR(ddata->channels[i])) {
  446. error = PTR_ERR(ddata->channels[i]);
  447. goto out_err;
  448. }
  449. if (!ddata->channels[i]->indio_dev) {
  450. error = -ENXIO;
  451. goto out_err;
  452. }
  453. }
  454. return 0;
  455. out_err:
  456. dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
  457. error);
  458. return error;
  459. }
  460. static const struct power_supply_desc cpcap_charger_usb_desc = {
  461. .name = "usb",
  462. .type = POWER_SUPPLY_TYPE_USB,
  463. .properties = cpcap_charger_props,
  464. .num_properties = ARRAY_SIZE(cpcap_charger_props),
  465. .get_property = cpcap_charger_get_property,
  466. };
  467. #ifdef CONFIG_OF
  468. static const struct of_device_id cpcap_charger_id_table[] = {
  469. {
  470. .compatible = "motorola,mapphone-cpcap-charger",
  471. },
  472. {},
  473. };
  474. MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
  475. #endif
  476. static int cpcap_charger_probe(struct platform_device *pdev)
  477. {
  478. struct cpcap_charger_ddata *ddata;
  479. const struct of_device_id *of_id;
  480. int error;
  481. of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
  482. &pdev->dev);
  483. if (!of_id)
  484. return -EINVAL;
  485. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  486. if (!ddata)
  487. return -ENOMEM;
  488. ddata->dev = &pdev->dev;
  489. ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
  490. if (!ddata->reg)
  491. return -ENODEV;
  492. INIT_LIST_HEAD(&ddata->irq_list);
  493. INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
  494. INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
  495. platform_set_drvdata(pdev, ddata);
  496. error = cpcap_charger_init_iio(ddata);
  497. if (error)
  498. return error;
  499. atomic_set(&ddata->active, 1);
  500. ddata->usb = devm_power_supply_register(ddata->dev,
  501. &cpcap_charger_usb_desc,
  502. NULL);
  503. if (IS_ERR(ddata->usb)) {
  504. error = PTR_ERR(ddata->usb);
  505. dev_err(ddata->dev, "failed to register USB charger: %i\n",
  506. error);
  507. return error;
  508. }
  509. error = cpcap_usb_init_interrupts(pdev, ddata);
  510. if (error)
  511. return error;
  512. ddata->comparator.set_vbus = cpcap_charger_set_vbus;
  513. error = omap_usb2_set_comparator(&ddata->comparator);
  514. if (error == -ENODEV) {
  515. dev_info(ddata->dev, "charger needs phy, deferring probe\n");
  516. return -EPROBE_DEFER;
  517. }
  518. cpcap_charger_init_optional_gpios(ddata);
  519. schedule_delayed_work(&ddata->detect_work, 0);
  520. return 0;
  521. }
  522. static int cpcap_charger_remove(struct platform_device *pdev)
  523. {
  524. struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
  525. int error;
  526. atomic_set(&ddata->active, 0);
  527. error = omap_usb2_set_comparator(NULL);
  528. if (error)
  529. dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
  530. error);
  531. error = cpcap_charger_set_state(ddata, 0, 0, 0);
  532. if (error)
  533. dev_warn(ddata->dev, "could not clear charger: %i\n",
  534. error);
  535. cancel_delayed_work_sync(&ddata->vbus_work);
  536. cancel_delayed_work_sync(&ddata->detect_work);
  537. return 0;
  538. }
  539. static struct platform_driver cpcap_charger_driver = {
  540. .probe = cpcap_charger_probe,
  541. .driver = {
  542. .name = "cpcap-charger",
  543. .of_match_table = of_match_ptr(cpcap_charger_id_table),
  544. },
  545. .remove = cpcap_charger_remove,
  546. };
  547. module_platform_driver(cpcap_charger_driver);
  548. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  549. MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
  550. MODULE_LICENSE("GPL v2");
  551. MODULE_ALIAS("platform:cpcap-charger");