1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Motorola CPCAP PMIC battery charger driver
4  *
5  * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
6  *
7  * Rewritten for Linux power framework with some parts based on
8  * on earlier driver found in the Motorola Linux kernel:
9  *
10  * Copyright (C) 2009-2010 Motorola, Inc.
11  */
12 
13 #include <linux/atomic.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
19 #include <linux/notifier.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/power_supply.h>
24 #include <linux/regmap.h>
25 
26 #include <linux/gpio/consumer.h>
27 #include <linux/usb/phy_companion.h>
28 #include <linux/phy/omap_usb.h>
29 #include <linux/usb/otg.h>
30 #include <linux/iio/consumer.h>
31 #include <linux/mfd/motorola-cpcap.h>
32 
33 /*
34  * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
35  * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
36  * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
37  * and values for CPCAP are different, but some of the internal components seem
38  * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
39  * bits that seem to describe the CRM register.
40  */
41 #define CPCAP_REG_CRM_UNUSED_641_15	BIT(15)	/* 641 = register number */
42 #define CPCAP_REG_CRM_UNUSED_641_14	BIT(14)	/* 641 = register number */
43 #define CPCAP_REG_CRM_CHRG_LED_EN	BIT(13)	/* Charger LED */
44 #define CPCAP_REG_CRM_RVRSMODE		BIT(12)	/* USB VBUS output enable */
45 #define CPCAP_REG_CRM_ICHRG_TR1		BIT(11)	/* Trickle charge current */
46 #define CPCAP_REG_CRM_ICHRG_TR0		BIT(10)
47 #define CPCAP_REG_CRM_FET_OVRD		BIT(9)	/* 0 = hardware, 1 = FET_CTRL */
48 #define CPCAP_REG_CRM_FET_CTRL		BIT(8)	/* BPFET 1 if FET_OVRD set */
49 #define CPCAP_REG_CRM_VCHRG3		BIT(7)	/* Charge voltage bits */
50 #define CPCAP_REG_CRM_VCHRG2		BIT(6)
51 #define CPCAP_REG_CRM_VCHRG1		BIT(5)
52 #define CPCAP_REG_CRM_VCHRG0		BIT(4)
53 #define CPCAP_REG_CRM_ICHRG3		BIT(3)	/* Charge current bits */
54 #define CPCAP_REG_CRM_ICHRG2		BIT(2)
55 #define CPCAP_REG_CRM_ICHRG1		BIT(1)
56 #define CPCAP_REG_CRM_ICHRG0		BIT(0)
57 
58 /* CPCAP_REG_CRM trickle charge voltages */
59 #define CPCAP_REG_CRM_TR(val)		(((val) & 0x3) << 10)
60 #define CPCAP_REG_CRM_TR_0A00		CPCAP_REG_CRM_TR(0x0)
61 #define CPCAP_REG_CRM_TR_0A24		CPCAP_REG_CRM_TR(0x1)
62 #define CPCAP_REG_CRM_TR_0A48		CPCAP_REG_CRM_TR(0x2)
63 #define CPCAP_REG_CRM_TR_0A72		CPCAP_REG_CRM_TR(0x4)
64 
65 /*
66  * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
67  * Note that these register bits don't match MC13783UG.pdf VCHRG
68  * register bits.
69  */
70 #define CPCAP_REG_CRM_VCHRG(val)	(((val) & 0xf) << 4)
71 #define CPCAP_REG_CRM_VCHRG_3V80	CPCAP_REG_CRM_VCHRG(0x0)
72 #define CPCAP_REG_CRM_VCHRG_4V10	CPCAP_REG_CRM_VCHRG(0x1)
73 #define CPCAP_REG_CRM_VCHRG_4V12	CPCAP_REG_CRM_VCHRG(0x2)
74 #define CPCAP_REG_CRM_VCHRG_4V15	CPCAP_REG_CRM_VCHRG(0x3)
75 #define CPCAP_REG_CRM_VCHRG_4V17	CPCAP_REG_CRM_VCHRG(0x4)
76 #define CPCAP_REG_CRM_VCHRG_4V20	CPCAP_REG_CRM_VCHRG(0x5)
77 #define CPCAP_REG_CRM_VCHRG_4V23	CPCAP_REG_CRM_VCHRG(0x6)
78 #define CPCAP_REG_CRM_VCHRG_4V25	CPCAP_REG_CRM_VCHRG(0x7)
79 #define CPCAP_REG_CRM_VCHRG_4V27	CPCAP_REG_CRM_VCHRG(0x8)
80 #define CPCAP_REG_CRM_VCHRG_4V30	CPCAP_REG_CRM_VCHRG(0x9)
81 #define CPCAP_REG_CRM_VCHRG_4V33	CPCAP_REG_CRM_VCHRG(0xa)
82 #define CPCAP_REG_CRM_VCHRG_4V35	CPCAP_REG_CRM_VCHRG(0xb)
83 #define CPCAP_REG_CRM_VCHRG_4V38	CPCAP_REG_CRM_VCHRG(0xc)
84 #define CPCAP_REG_CRM_VCHRG_4V40	CPCAP_REG_CRM_VCHRG(0xd)
85 #define CPCAP_REG_CRM_VCHRG_4V42	CPCAP_REG_CRM_VCHRG(0xe)
86 #define CPCAP_REG_CRM_VCHRG_4V44	CPCAP_REG_CRM_VCHRG(0xf)
87 
88 /*
89  * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
90  * values in "Table 8-3. Charge Path Regulator Current Limit
91  * Characteristics" for the nominal values.
92  */
93 #define CPCAP_REG_CRM_ICHRG(val)	(((val) & 0xf) << 0)
94 #define CPCAP_REG_CRM_ICHRG_0A000	CPCAP_REG_CRM_ICHRG(0x0)
95 #define CPCAP_REG_CRM_ICHRG_0A070	CPCAP_REG_CRM_ICHRG(0x1)
96 #define CPCAP_REG_CRM_ICHRG_0A177	CPCAP_REG_CRM_ICHRG(0x2)
97 #define CPCAP_REG_CRM_ICHRG_0A266	CPCAP_REG_CRM_ICHRG(0x3)
98 #define CPCAP_REG_CRM_ICHRG_0A355	CPCAP_REG_CRM_ICHRG(0x4)
99 #define CPCAP_REG_CRM_ICHRG_0A443	CPCAP_REG_CRM_ICHRG(0x5)
100 #define CPCAP_REG_CRM_ICHRG_0A532	CPCAP_REG_CRM_ICHRG(0x6)
101 #define CPCAP_REG_CRM_ICHRG_0A621	CPCAP_REG_CRM_ICHRG(0x7)
102 #define CPCAP_REG_CRM_ICHRG_0A709	CPCAP_REG_CRM_ICHRG(0x8)
103 #define CPCAP_REG_CRM_ICHRG_0A798	CPCAP_REG_CRM_ICHRG(0x9)
104 #define CPCAP_REG_CRM_ICHRG_0A886	CPCAP_REG_CRM_ICHRG(0xa)
105 #define CPCAP_REG_CRM_ICHRG_0A975	CPCAP_REG_CRM_ICHRG(0xb)
106 #define CPCAP_REG_CRM_ICHRG_1A064	CPCAP_REG_CRM_ICHRG(0xc)
107 #define CPCAP_REG_CRM_ICHRG_1A152	CPCAP_REG_CRM_ICHRG(0xd)
108 #define CPCAP_REG_CRM_ICHRG_1A596	CPCAP_REG_CRM_ICHRG(0xe)
109 #define CPCAP_REG_CRM_ICHRG_NO_LIMIT	CPCAP_REG_CRM_ICHRG(0xf)
110 
111 /* CPCAP_REG_VUSBC register bits needed for VBUS */
112 #define CPCAP_BIT_VBUS_SWITCH		BIT(0)	/* VBUS boost to 5V */
113 
114 enum {
115 	CPCAP_CHARGER_IIO_BATTDET,
116 	CPCAP_CHARGER_IIO_VOLTAGE,
117 	CPCAP_CHARGER_IIO_VBUS,
118 	CPCAP_CHARGER_IIO_CHRG_CURRENT,
119 	CPCAP_CHARGER_IIO_BATT_CURRENT,
120 	CPCAP_CHARGER_IIO_NR,
121 };
122 
123 enum {
124 	CPCAP_CHARGER_DISCONNECTED,
125 	CPCAP_CHARGER_DETECTING,
126 	CPCAP_CHARGER_CHARGING,
127 	CPCAP_CHARGER_DONE,
128 };
129 
130 struct cpcap_charger_ddata {
131 	struct device *dev;
132 	struct regmap *reg;
133 	struct list_head irq_list;
134 	struct delayed_work detect_work;
135 	struct delayed_work vbus_work;
136 	struct gpio_desc *gpio[2];		/* gpio_reven0 & 1 */
137 
138 	struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
139 
140 	struct power_supply *usb;
141 
142 	struct phy_companion comparator;	/* For USB VBUS */
143 	unsigned int vbus_enabled:1;
144 	unsigned int feeding_vbus:1;
145 	atomic_t active;
146 
147 	int status;
148 	int state;
149 	int voltage;
150 };
151 
152 struct cpcap_interrupt_desc {
153 	int irq;
154 	struct list_head node;
155 	const char *name;
156 };
157 
158 struct cpcap_charger_ints_state {
159 	bool chrg_det;
160 	bool rvrs_chrg;
161 	bool vbusov;
162 
163 	bool chrg_se1b;
164 	bool rvrs_mode;
165 	bool chrgcurr2;
166 	bool chrgcurr1;
167 	bool vbusvld;
168 
169 	bool battdetb;
170 };
171 
172 static enum power_supply_property cpcap_charger_props[] = {
173 	POWER_SUPPLY_PROP_STATUS,
174 	POWER_SUPPLY_PROP_ONLINE,
175 	POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
176 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
177 	POWER_SUPPLY_PROP_CURRENT_NOW,
178 };
179 
180 /* No battery always shows temperature of -40000 */
181 static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
182 {
183 	struct iio_channel *channel;
184 	int error, temperature;
185 
186 	channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
187 	error = iio_read_channel_processed(channel, &temperature);
188 	if (error < 0) {
189 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
190 
191 		return false;
192 	}
193 
194 	return temperature > -20000 && temperature < 60000;
195 }
196 
197 static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
198 {
199 	struct iio_channel *channel;
200 	int error, value = 0;
201 
202 	channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
203 	error = iio_read_channel_processed(channel, &value);
204 	if (error < 0) {
205 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
206 
207 		return 0;
208 	}
209 
210 	return value;
211 }
212 
213 static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
214 {
215 	struct iio_channel *channel;
216 	int error, value = 0;
217 
218 	channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
219 	error = iio_read_channel_processed(channel, &value);
220 	if (error < 0) {
221 		dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
222 
223 		return 0;
224 	}
225 
226 	return value;
227 }
228 
229 static int cpcap_charger_get_property(struct power_supply *psy,
230 				      enum power_supply_property psp,
231 				      union power_supply_propval *val)
232 {
233 	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
234 
235 	switch (psp) {
236 	case POWER_SUPPLY_PROP_STATUS:
237 		val->intval = ddata->status;
238 		break;
239 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
240 		val->intval = ddata->voltage;
241 		break;
242 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
243 		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
244 			val->intval = cpcap_charger_get_charge_voltage(ddata) *
245 				1000;
246 		else
247 			val->intval = 0;
248 		break;
249 	case POWER_SUPPLY_PROP_CURRENT_NOW:
250 		if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
251 			val->intval = cpcap_charger_get_charge_current(ddata) *
252 				1000;
253 		else
254 			val->intval = 0;
255 		break;
256 	case POWER_SUPPLY_PROP_ONLINE:
257 		val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
258 		break;
259 	default:
260 		return -EINVAL;
261 	}
262 
263 	return 0;
264 }
265 
266 static int cpcap_charger_match_voltage(int voltage)
267 {
268 	switch (voltage) {
269 	case 0 ... 4100000 - 1: return 3800000;
270 	case 4100000 ... 4120000 - 1: return 4100000;
271 	case 4120000 ... 4150000 - 1: return 4120000;
272 	case 4150000 ... 4170000 - 1: return 4150000;
273 	case 4170000 ... 4200000 - 1: return 4170000;
274 	case 4200000 ... 4230000 - 1: return 4200000;
275 	case 4230000 ... 4250000 - 1: return 4230000;
276 	case 4250000 ... 4270000 - 1: return 4250000;
277 	case 4270000 ... 4300000 - 1: return 4270000;
278 	case 4300000 ... 4330000 - 1: return 4300000;
279 	case 4330000 ... 4350000 - 1: return 4330000;
280 	case 4350000 ... 4380000 - 1: return 4350000;
281 	case 4380000 ... 4400000 - 1: return 4380000;
282 	case 4400000 ... 4420000 - 1: return 4400000;
283 	case 4420000 ... 4440000 - 1: return 4420000;
284 	case 4440000: return 4440000;
285 	default: return 0;
286 	}
287 }
288 
289 static int
290 cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata)
291 {
292 	union power_supply_propval prop;
293 	struct power_supply *battery;
294 	int voltage = ddata->voltage;
295 	int error;
296 
297 	battery = power_supply_get_by_name("battery");
298 	if (battery) {
299 		error = power_supply_get_property(battery,
300 				POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
301 				&prop);
302 		if (!error)
303 			voltage = prop.intval;
304 	}
305 
306 	return voltage;
307 }
308 
309 static int cpcap_charger_set_property(struct power_supply *psy,
310 				      enum power_supply_property psp,
311 				      const union power_supply_propval *val)
312 {
313 	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
314 	int voltage, batvolt;
315 
316 	switch (psp) {
317 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
318 		voltage = cpcap_charger_match_voltage(val->intval);
319 		batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
320 		if (voltage > batvolt)
321 			voltage = batvolt;
322 		ddata->voltage = voltage;
323 		schedule_delayed_work(&ddata->detect_work, 0);
324 		break;
325 	default:
326 		return -EINVAL;
327 	}
328 
329 	return 0;
330 }
331 
332 static int cpcap_charger_property_is_writeable(struct power_supply *psy,
333 					       enum power_supply_property psp)
334 {
335 	switch (psp) {
336 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
337 		return 1;
338 	default:
339 		return 0;
340 	}
341 }
342 
343 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
344 					 bool enabled)
345 {
346 	if (!ddata->gpio[0])
347 		return;
348 
349 	gpiod_set_value(ddata->gpio[0], enabled);
350 }
351 
352 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
353 					     bool enabled)
354 {
355 	if (!ddata->gpio[1])
356 		return;
357 
358 	gpiod_set_value(ddata->gpio[1], enabled);
359 }
360 
361 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
362 				   int max_voltage, int charge_current,
363 				   int trickle_current)
364 {
365 	bool enable;
366 	int error;
367 
368 	enable = (charge_current || trickle_current);
369 	dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
370 
371 	if (!enable) {
372 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
373 					   0x3fff,
374 					   CPCAP_REG_CRM_FET_OVRD |
375 					   CPCAP_REG_CRM_FET_CTRL);
376 		if (error) {
377 			ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
378 			goto out_err;
379 		}
380 
381 		ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
382 
383 		return 0;
384 	}
385 
386 	error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
387 				   CPCAP_REG_CRM_CHRG_LED_EN |
388 				   trickle_current |
389 				   CPCAP_REG_CRM_FET_OVRD |
390 				   CPCAP_REG_CRM_FET_CTRL |
391 				   max_voltage |
392 				   charge_current);
393 	if (error) {
394 		ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
395 		goto out_err;
396 	}
397 
398 	ddata->status = POWER_SUPPLY_STATUS_CHARGING;
399 
400 	return 0;
401 
402 out_err:
403 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
404 
405 	return error;
406 }
407 
408 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
409 {
410 	int error, value = 0;
411 	struct iio_channel *channel =
412 		ddata->channels[CPCAP_CHARGER_IIO_VBUS];
413 
414 	error = iio_read_channel_processed(channel, &value);
415 	if (error >= 0)
416 		return value > 3900 ? true : false;
417 
418 	dev_err(ddata->dev, "error reading VBUS: %i\n", error);
419 
420 	return false;
421 }
422 
423 /* VBUS control functions for the USB PHY companion */
424 static void cpcap_charger_vbus_work(struct work_struct *work)
425 {
426 	struct cpcap_charger_ddata *ddata;
427 	bool vbus = false;
428 	int error;
429 
430 	ddata = container_of(work, struct cpcap_charger_ddata,
431 			     vbus_work.work);
432 
433 	if (ddata->vbus_enabled) {
434 		vbus = cpcap_charger_vbus_valid(ddata);
435 		if (vbus) {
436 			dev_info(ddata->dev, "VBUS already provided\n");
437 
438 			return;
439 		}
440 
441 		ddata->feeding_vbus = true;
442 		cpcap_charger_set_cable_path(ddata, false);
443 		cpcap_charger_set_inductive_path(ddata, false);
444 
445 		error = cpcap_charger_set_state(ddata, 0, 0, 0);
446 		if (error)
447 			goto out_err;
448 
449 		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
450 					   CPCAP_BIT_VBUS_SWITCH,
451 					   CPCAP_BIT_VBUS_SWITCH);
452 		if (error)
453 			goto out_err;
454 
455 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
456 					   CPCAP_REG_CRM_RVRSMODE,
457 					   CPCAP_REG_CRM_RVRSMODE);
458 		if (error)
459 			goto out_err;
460 	} else {
461 		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
462 					   CPCAP_BIT_VBUS_SWITCH, 0);
463 		if (error)
464 			goto out_err;
465 
466 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
467 					   CPCAP_REG_CRM_RVRSMODE, 0);
468 		if (error)
469 			goto out_err;
470 
471 		cpcap_charger_set_cable_path(ddata, true);
472 		cpcap_charger_set_inductive_path(ddata, true);
473 		ddata->feeding_vbus = false;
474 	}
475 
476 	return;
477 
478 out_err:
479 	dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
480 		ddata->vbus_enabled ? "enable" : "disable", error);
481 }
482 
483 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
484 				  bool enabled)
485 {
486 	struct cpcap_charger_ddata *ddata =
487 		container_of(comparator, struct cpcap_charger_ddata,
488 			     comparator);
489 
490 	ddata->vbus_enabled = enabled;
491 	schedule_delayed_work(&ddata->vbus_work, 0);
492 
493 	return 0;
494 }
495 
496 /* Charger interrupt handling functions */
497 
498 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
499 					struct cpcap_charger_ints_state *s)
500 {
501 	int val, error;
502 
503 	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
504 	if (error)
505 		return error;
506 
507 	s->chrg_det = val & BIT(13);
508 	s->rvrs_chrg = val & BIT(12);
509 	s->vbusov = val & BIT(11);
510 
511 	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
512 	if (error)
513 		return error;
514 
515 	s->chrg_se1b = val & BIT(13);
516 	s->rvrs_mode = val & BIT(6);
517 	s->chrgcurr2 = val & BIT(5);
518 	s->chrgcurr1 = val & BIT(4);
519 	s->vbusvld = val & BIT(3);
520 
521 	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
522 	if (error)
523 		return error;
524 
525 	s->battdetb = val & BIT(6);
526 
527 	return 0;
528 }
529 
530 static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
531 				       int state)
532 {
533 	const char *status;
534 
535 	if (state > CPCAP_CHARGER_DONE) {
536 		dev_warn(ddata->dev, "unknown state: %i\n", state);
537 
538 		return;
539 	}
540 
541 	ddata->state = state;
542 
543 	switch (state) {
544 	case CPCAP_CHARGER_DISCONNECTED:
545 		status = "DISCONNECTED";
546 		break;
547 	case CPCAP_CHARGER_DETECTING:
548 		status = "DETECTING";
549 		break;
550 	case CPCAP_CHARGER_CHARGING:
551 		status = "CHARGING";
552 		break;
553 	case CPCAP_CHARGER_DONE:
554 		status = "DONE";
555 		break;
556 	default:
557 		return;
558 	}
559 
560 	dev_dbg(ddata->dev, "state: %s\n", status);
561 }
562 
563 static int cpcap_charger_voltage_to_regval(int voltage)
564 {
565 	int offset;
566 
567 	switch (voltage) {
568 	case 0 ... 4100000 - 1:
569 		return 0;
570 	case 4100000 ... 4200000 - 1:
571 		offset = 1;
572 		break;
573 	case 4200000 ... 4300000 - 1:
574 		offset = 0;
575 		break;
576 	case 4300000 ... 4380000 - 1:
577 		offset = -1;
578 		break;
579 	case 4380000 ... 4440000:
580 		offset = -2;
581 		break;
582 	default:
583 		return 0;
584 	}
585 
586 	return ((voltage - 4100000) / 20000) + offset;
587 }
588 
589 static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
590 				     int state, unsigned long delay)
591 {
592 	int error;
593 
594 	error = cpcap_charger_set_state(ddata, 0, 0, 0);
595 	if (error)
596 		return;
597 
598 	cpcap_charger_update_state(ddata, state);
599 	power_supply_changed(ddata->usb);
600 	schedule_delayed_work(&ddata->detect_work, delay);
601 }
602 
603 static void cpcap_usb_detect(struct work_struct *work)
604 {
605 	struct cpcap_charger_ddata *ddata;
606 	struct cpcap_charger_ints_state s;
607 	int error;
608 
609 	ddata = container_of(work, struct cpcap_charger_ddata,
610 			     detect_work.work);
611 
612 	error = cpcap_charger_get_ints_state(ddata, &s);
613 	if (error)
614 		return;
615 
616 	/* Just init the state if a charger is connected with no chrg_det set */
617 	if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
618 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
619 
620 		return;
621 	}
622 
623 	/*
624 	 * If battery voltage is higher than charge voltage, it may have been
625 	 * charged to 4.35V by Android. Try again in 10 minutes.
626 	 */
627 	if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
628 		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
629 					 HZ * 60 * 10);
630 
631 		return;
632 	}
633 
634 	/* Throttle chrgcurr2 interrupt for charger done and retry */
635 	switch (ddata->state) {
636 	case CPCAP_CHARGER_CHARGING:
637 		if (s.chrgcurr2)
638 			break;
639 		if (s.chrgcurr1 && s.vbusvld) {
640 			cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
641 						 HZ * 5);
642 			return;
643 		}
644 		break;
645 	case CPCAP_CHARGER_DONE:
646 		if (!s.chrgcurr2)
647 			break;
648 		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
649 					 HZ * 5);
650 		return;
651 	default:
652 		break;
653 	}
654 
655 	if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
656 	    s.chrgcurr1) {
657 		int max_current;
658 		int vchrg;
659 
660 		if (cpcap_charger_battery_found(ddata))
661 			max_current = CPCAP_REG_CRM_ICHRG_1A596;
662 		else
663 			max_current = CPCAP_REG_CRM_ICHRG_0A532;
664 
665 		vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
666 		error = cpcap_charger_set_state(ddata,
667 						CPCAP_REG_CRM_VCHRG(vchrg),
668 						max_current, 0);
669 		if (error)
670 			goto out_err;
671 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
672 	} else {
673 		error = cpcap_charger_set_state(ddata, 0, 0, 0);
674 		if (error)
675 			goto out_err;
676 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
677 	}
678 
679 	power_supply_changed(ddata->usb);
680 	return;
681 
682 out_err:
683 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
684 }
685 
686 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
687 {
688 	struct cpcap_charger_ddata *ddata = data;
689 
690 	if (!atomic_read(&ddata->active))
691 		return IRQ_NONE;
692 
693 	schedule_delayed_work(&ddata->detect_work, 0);
694 
695 	return IRQ_HANDLED;
696 }
697 
698 static int cpcap_usb_init_irq(struct platform_device *pdev,
699 			      struct cpcap_charger_ddata *ddata,
700 			      const char *name)
701 {
702 	struct cpcap_interrupt_desc *d;
703 	int irq, error;
704 
705 	irq = platform_get_irq_byname(pdev, name);
706 	if (irq < 0)
707 		return -ENODEV;
708 
709 	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
710 					  cpcap_charger_irq_thread,
711 					  IRQF_SHARED,
712 					  name, ddata);
713 	if (error) {
714 		dev_err(ddata->dev, "could not get irq %s: %i\n",
715 			name, error);
716 
717 		return error;
718 	}
719 
720 	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
721 	if (!d)
722 		return -ENOMEM;
723 
724 	d->name = name;
725 	d->irq = irq;
726 	list_add(&d->node, &ddata->irq_list);
727 
728 	return 0;
729 }
730 
731 static const char * const cpcap_charger_irqs[] = {
732 	/* REG_INT_0 */
733 	"chrg_det", "rvrs_chrg",
734 
735 	/* REG_INT1 */
736 	"chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
737 
738 	/* REG_INT_3 */
739 	"battdetb",
740 };
741 
742 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
743 				     struct cpcap_charger_ddata *ddata)
744 {
745 	int i, error;
746 
747 	for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
748 		error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
749 		if (error)
750 			return error;
751 	}
752 
753 	return 0;
754 }
755 
756 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
757 {
758 	int i;
759 
760 	for (i = 0; i < 2; i++) {
761 		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
762 						      i, GPIOD_OUT_HIGH);
763 		if (IS_ERR(ddata->gpio[i])) {
764 			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
765 				 i, PTR_ERR(ddata->gpio[i]));
766 			ddata->gpio[i] = NULL;
767 		}
768 	}
769 }
770 
771 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
772 {
773 	const char * const names[CPCAP_CHARGER_IIO_NR] = {
774 		"battdetb", "battp", "vbus", "chg_isense", "batti",
775 	};
776 	int error, i;
777 
778 	for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
779 		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
780 							  names[i]);
781 		if (IS_ERR(ddata->channels[i])) {
782 			error = PTR_ERR(ddata->channels[i]);
783 			goto out_err;
784 		}
785 
786 		if (!ddata->channels[i]->indio_dev) {
787 			error = -ENXIO;
788 			goto out_err;
789 		}
790 	}
791 
792 	return 0;
793 
794 out_err:
795 	if (error != -EPROBE_DEFER)
796 		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
797 			error);
798 
799 	return error;
800 }
801 
802 static const struct power_supply_desc cpcap_charger_usb_desc = {
803 	.name		= "usb",
804 	.type		= POWER_SUPPLY_TYPE_USB,
805 	.properties	= cpcap_charger_props,
806 	.num_properties	= ARRAY_SIZE(cpcap_charger_props),
807 	.get_property	= cpcap_charger_get_property,
808 	.set_property	= cpcap_charger_set_property,
809 	.property_is_writeable = cpcap_charger_property_is_writeable,
810 };
811 
812 #ifdef CONFIG_OF
813 static const struct of_device_id cpcap_charger_id_table[] = {
814 	{
815 		.compatible = "motorola,mapphone-cpcap-charger",
816 	},
817 	{},
818 };
819 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
820 #endif
821 
822 static int cpcap_charger_probe(struct platform_device *pdev)
823 {
824 	struct cpcap_charger_ddata *ddata;
825 	const struct of_device_id *of_id;
826 	struct power_supply_config psy_cfg = {};
827 	int error;
828 
829 	of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
830 				&pdev->dev);
831 	if (!of_id)
832 		return -EINVAL;
833 
834 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
835 	if (!ddata)
836 		return -ENOMEM;
837 
838 	ddata->dev = &pdev->dev;
839 	ddata->voltage = 4200000;
840 
841 	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
842 	if (!ddata->reg)
843 		return -ENODEV;
844 
845 	INIT_LIST_HEAD(&ddata->irq_list);
846 	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
847 	INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
848 	platform_set_drvdata(pdev, ddata);
849 
850 	error = cpcap_charger_init_iio(ddata);
851 	if (error)
852 		return error;
853 
854 	atomic_set(&ddata->active, 1);
855 
856 	psy_cfg.of_node = pdev->dev.of_node;
857 	psy_cfg.drv_data = ddata;
858 
859 	ddata->usb = devm_power_supply_register(ddata->dev,
860 						&cpcap_charger_usb_desc,
861 						&psy_cfg);
862 	if (IS_ERR(ddata->usb)) {
863 		error = PTR_ERR(ddata->usb);
864 		dev_err(ddata->dev, "failed to register USB charger: %i\n",
865 			error);
866 
867 		return error;
868 	}
869 
870 	error = cpcap_usb_init_interrupts(pdev, ddata);
871 	if (error)
872 		return error;
873 
874 	ddata->comparator.set_vbus = cpcap_charger_set_vbus;
875 	error = omap_usb2_set_comparator(&ddata->comparator);
876 	if (error == -ENODEV) {
877 		dev_info(ddata->dev, "charger needs phy, deferring probe\n");
878 		return -EPROBE_DEFER;
879 	}
880 
881 	cpcap_charger_init_optional_gpios(ddata);
882 
883 	schedule_delayed_work(&ddata->detect_work, 0);
884 
885 	return 0;
886 }
887 
888 static int cpcap_charger_remove(struct platform_device *pdev)
889 {
890 	struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
891 	int error;
892 
893 	atomic_set(&ddata->active, 0);
894 	error = omap_usb2_set_comparator(NULL);
895 	if (error)
896 		dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
897 			 error);
898 
899 	error = cpcap_charger_set_state(ddata, 0, 0, 0);
900 	if (error)
901 		dev_warn(ddata->dev, "could not clear charger: %i\n",
902 			 error);
903 	cancel_delayed_work_sync(&ddata->vbus_work);
904 	cancel_delayed_work_sync(&ddata->detect_work);
905 
906 	return 0;
907 }
908 
909 static struct platform_driver cpcap_charger_driver = {
910 	.probe = cpcap_charger_probe,
911 	.driver	= {
912 		.name	= "cpcap-charger",
913 		.of_match_table = of_match_ptr(cpcap_charger_id_table),
914 	},
915 	.remove	= cpcap_charger_remove,
916 };
917 module_platform_driver(cpcap_charger_driver);
918 
919 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
920 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
921 MODULE_LICENSE("GPL v2");
922 MODULE_ALIAS("platform:cpcap-charger");
923