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