• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
cpcap_charger_battery_found(struct cpcap_charger_ddata * ddata)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 
cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata * ddata)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 
cpcap_charger_get_charge_current(struct cpcap_charger_ddata * ddata)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 
cpcap_charger_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)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 
cpcap_charger_match_voltage(int voltage)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
cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata * ddata)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 		power_supply_put(battery);
306 	}
307 
308 	return voltage;
309 }
310 
cpcap_charger_set_property(struct power_supply * psy,enum power_supply_property psp,const union power_supply_propval * val)311 static int cpcap_charger_set_property(struct power_supply *psy,
312 				      enum power_supply_property psp,
313 				      const union power_supply_propval *val)
314 {
315 	struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
316 	int voltage, batvolt;
317 
318 	switch (psp) {
319 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
320 		voltage = cpcap_charger_match_voltage(val->intval);
321 		batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
322 		if (voltage > batvolt)
323 			voltage = batvolt;
324 		ddata->voltage = voltage;
325 		schedule_delayed_work(&ddata->detect_work, 0);
326 		break;
327 	default:
328 		return -EINVAL;
329 	}
330 
331 	return 0;
332 }
333 
cpcap_charger_property_is_writeable(struct power_supply * psy,enum power_supply_property psp)334 static int cpcap_charger_property_is_writeable(struct power_supply *psy,
335 					       enum power_supply_property psp)
336 {
337 	switch (psp) {
338 	case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
339 		return 1;
340 	default:
341 		return 0;
342 	}
343 }
344 
cpcap_charger_set_cable_path(struct cpcap_charger_ddata * ddata,bool enabled)345 static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
346 					 bool enabled)
347 {
348 	if (!ddata->gpio[0])
349 		return;
350 
351 	gpiod_set_value(ddata->gpio[0], enabled);
352 }
353 
cpcap_charger_set_inductive_path(struct cpcap_charger_ddata * ddata,bool enabled)354 static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
355 					     bool enabled)
356 {
357 	if (!ddata->gpio[1])
358 		return;
359 
360 	gpiod_set_value(ddata->gpio[1], enabled);
361 }
362 
cpcap_charger_set_state(struct cpcap_charger_ddata * ddata,int max_voltage,int charge_current,int trickle_current)363 static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
364 				   int max_voltage, int charge_current,
365 				   int trickle_current)
366 {
367 	bool enable;
368 	int error;
369 
370 	enable = (charge_current || trickle_current);
371 	dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
372 
373 	if (!enable) {
374 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
375 					   0x3fff,
376 					   CPCAP_REG_CRM_FET_OVRD |
377 					   CPCAP_REG_CRM_FET_CTRL);
378 		if (error) {
379 			ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
380 			goto out_err;
381 		}
382 
383 		ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
384 
385 		return 0;
386 	}
387 
388 	error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
389 				   CPCAP_REG_CRM_CHRG_LED_EN |
390 				   trickle_current |
391 				   CPCAP_REG_CRM_FET_OVRD |
392 				   CPCAP_REG_CRM_FET_CTRL |
393 				   max_voltage |
394 				   charge_current);
395 	if (error) {
396 		ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
397 		goto out_err;
398 	}
399 
400 	ddata->status = POWER_SUPPLY_STATUS_CHARGING;
401 
402 	return 0;
403 
404 out_err:
405 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
406 
407 	return error;
408 }
409 
cpcap_charger_vbus_valid(struct cpcap_charger_ddata * ddata)410 static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
411 {
412 	int error, value = 0;
413 	struct iio_channel *channel =
414 		ddata->channels[CPCAP_CHARGER_IIO_VBUS];
415 
416 	error = iio_read_channel_processed(channel, &value);
417 	if (error >= 0)
418 		return value > 3900 ? true : false;
419 
420 	dev_err(ddata->dev, "error reading VBUS: %i\n", error);
421 
422 	return false;
423 }
424 
425 /* VBUS control functions for the USB PHY companion */
cpcap_charger_vbus_work(struct work_struct * work)426 static void cpcap_charger_vbus_work(struct work_struct *work)
427 {
428 	struct cpcap_charger_ddata *ddata;
429 	bool vbus = false;
430 	int error;
431 
432 	ddata = container_of(work, struct cpcap_charger_ddata,
433 			     vbus_work.work);
434 
435 	if (ddata->vbus_enabled) {
436 		vbus = cpcap_charger_vbus_valid(ddata);
437 		if (vbus) {
438 			dev_info(ddata->dev, "VBUS already provided\n");
439 
440 			return;
441 		}
442 
443 		ddata->feeding_vbus = true;
444 		cpcap_charger_set_cable_path(ddata, false);
445 		cpcap_charger_set_inductive_path(ddata, false);
446 
447 		error = cpcap_charger_set_state(ddata, 0, 0, 0);
448 		if (error)
449 			goto out_err;
450 
451 		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
452 					   CPCAP_BIT_VBUS_SWITCH,
453 					   CPCAP_BIT_VBUS_SWITCH);
454 		if (error)
455 			goto out_err;
456 
457 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
458 					   CPCAP_REG_CRM_RVRSMODE,
459 					   CPCAP_REG_CRM_RVRSMODE);
460 		if (error)
461 			goto out_err;
462 	} else {
463 		error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
464 					   CPCAP_BIT_VBUS_SWITCH, 0);
465 		if (error)
466 			goto out_err;
467 
468 		error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
469 					   CPCAP_REG_CRM_RVRSMODE, 0);
470 		if (error)
471 			goto out_err;
472 
473 		cpcap_charger_set_cable_path(ddata, true);
474 		cpcap_charger_set_inductive_path(ddata, true);
475 		ddata->feeding_vbus = false;
476 	}
477 
478 	return;
479 
480 out_err:
481 	dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
482 		ddata->vbus_enabled ? "enable" : "disable", error);
483 }
484 
cpcap_charger_set_vbus(struct phy_companion * comparator,bool enabled)485 static int cpcap_charger_set_vbus(struct phy_companion *comparator,
486 				  bool enabled)
487 {
488 	struct cpcap_charger_ddata *ddata =
489 		container_of(comparator, struct cpcap_charger_ddata,
490 			     comparator);
491 
492 	ddata->vbus_enabled = enabled;
493 	schedule_delayed_work(&ddata->vbus_work, 0);
494 
495 	return 0;
496 }
497 
498 /* Charger interrupt handling functions */
499 
cpcap_charger_get_ints_state(struct cpcap_charger_ddata * ddata,struct cpcap_charger_ints_state * s)500 static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
501 					struct cpcap_charger_ints_state *s)
502 {
503 	int val, error;
504 
505 	error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
506 	if (error)
507 		return error;
508 
509 	s->chrg_det = val & BIT(13);
510 	s->rvrs_chrg = val & BIT(12);
511 	s->vbusov = val & BIT(11);
512 
513 	error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
514 	if (error)
515 		return error;
516 
517 	s->chrg_se1b = val & BIT(13);
518 	s->rvrs_mode = val & BIT(6);
519 	s->chrgcurr2 = val & BIT(5);
520 	s->chrgcurr1 = val & BIT(4);
521 	s->vbusvld = val & BIT(3);
522 
523 	error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
524 	if (error)
525 		return error;
526 
527 	s->battdetb = val & BIT(6);
528 
529 	return 0;
530 }
531 
cpcap_charger_update_state(struct cpcap_charger_ddata * ddata,int state)532 static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
533 				       int state)
534 {
535 	const char *status;
536 
537 	if (state > CPCAP_CHARGER_DONE) {
538 		dev_warn(ddata->dev, "unknown state: %i\n", state);
539 
540 		return;
541 	}
542 
543 	ddata->state = state;
544 
545 	switch (state) {
546 	case CPCAP_CHARGER_DISCONNECTED:
547 		status = "DISCONNECTED";
548 		break;
549 	case CPCAP_CHARGER_DETECTING:
550 		status = "DETECTING";
551 		break;
552 	case CPCAP_CHARGER_CHARGING:
553 		status = "CHARGING";
554 		break;
555 	case CPCAP_CHARGER_DONE:
556 		status = "DONE";
557 		break;
558 	default:
559 		return;
560 	}
561 
562 	dev_dbg(ddata->dev, "state: %s\n", status);
563 }
564 
cpcap_charger_voltage_to_regval(int voltage)565 static int cpcap_charger_voltage_to_regval(int voltage)
566 {
567 	int offset;
568 
569 	switch (voltage) {
570 	case 0 ... 4100000 - 1:
571 		return 0;
572 	case 4100000 ... 4200000 - 1:
573 		offset = 1;
574 		break;
575 	case 4200000 ... 4300000 - 1:
576 		offset = 0;
577 		break;
578 	case 4300000 ... 4380000 - 1:
579 		offset = -1;
580 		break;
581 	case 4380000 ... 4440000:
582 		offset = -2;
583 		break;
584 	default:
585 		return 0;
586 	}
587 
588 	return ((voltage - 4100000) / 20000) + offset;
589 }
590 
cpcap_charger_disconnect(struct cpcap_charger_ddata * ddata,int state,unsigned long delay)591 static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
592 				     int state, unsigned long delay)
593 {
594 	int error;
595 
596 	error = cpcap_charger_set_state(ddata, 0, 0, 0);
597 	if (error)
598 		return;
599 
600 	cpcap_charger_update_state(ddata, state);
601 	power_supply_changed(ddata->usb);
602 	schedule_delayed_work(&ddata->detect_work, delay);
603 }
604 
cpcap_usb_detect(struct work_struct * work)605 static void cpcap_usb_detect(struct work_struct *work)
606 {
607 	struct cpcap_charger_ddata *ddata;
608 	struct cpcap_charger_ints_state s;
609 	int error;
610 
611 	ddata = container_of(work, struct cpcap_charger_ddata,
612 			     detect_work.work);
613 
614 	error = cpcap_charger_get_ints_state(ddata, &s);
615 	if (error)
616 		return;
617 
618 	/* Just init the state if a charger is connected with no chrg_det set */
619 	if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
620 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
621 
622 		return;
623 	}
624 
625 	/*
626 	 * If battery voltage is higher than charge voltage, it may have been
627 	 * charged to 4.35V by Android. Try again in 10 minutes.
628 	 */
629 	if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
630 		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
631 					 HZ * 60 * 10);
632 
633 		return;
634 	}
635 
636 	/* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */
637 	usleep_range(80000, 120000);
638 
639 	/* Throttle chrgcurr2 interrupt for charger done and retry */
640 	switch (ddata->state) {
641 	case CPCAP_CHARGER_CHARGING:
642 		if (s.chrgcurr2)
643 			break;
644 		if (s.chrgcurr1 && s.vbusvld) {
645 			cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
646 						 HZ * 5);
647 			return;
648 		}
649 		break;
650 	case CPCAP_CHARGER_DONE:
651 		if (!s.chrgcurr2)
652 			break;
653 		cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
654 					 HZ * 5);
655 		return;
656 	default:
657 		break;
658 	}
659 
660 	if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
661 	    s.chrgcurr1) {
662 		int max_current;
663 		int vchrg;
664 
665 		if (cpcap_charger_battery_found(ddata))
666 			max_current = CPCAP_REG_CRM_ICHRG_1A596;
667 		else
668 			max_current = CPCAP_REG_CRM_ICHRG_0A532;
669 
670 		vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
671 		error = cpcap_charger_set_state(ddata,
672 						CPCAP_REG_CRM_VCHRG(vchrg),
673 						max_current, 0);
674 		if (error)
675 			goto out_err;
676 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
677 	} else {
678 		error = cpcap_charger_set_state(ddata, 0, 0, 0);
679 		if (error)
680 			goto out_err;
681 		cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
682 	}
683 
684 	power_supply_changed(ddata->usb);
685 	return;
686 
687 out_err:
688 	dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
689 }
690 
cpcap_charger_irq_thread(int irq,void * data)691 static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
692 {
693 	struct cpcap_charger_ddata *ddata = data;
694 
695 	if (!atomic_read(&ddata->active))
696 		return IRQ_NONE;
697 
698 	schedule_delayed_work(&ddata->detect_work, 0);
699 
700 	return IRQ_HANDLED;
701 }
702 
cpcap_usb_init_irq(struct platform_device * pdev,struct cpcap_charger_ddata * ddata,const char * name)703 static int cpcap_usb_init_irq(struct platform_device *pdev,
704 			      struct cpcap_charger_ddata *ddata,
705 			      const char *name)
706 {
707 	struct cpcap_interrupt_desc *d;
708 	int irq, error;
709 
710 	irq = platform_get_irq_byname(pdev, name);
711 	if (irq < 0)
712 		return -ENODEV;
713 
714 	error = devm_request_threaded_irq(ddata->dev, irq, NULL,
715 					  cpcap_charger_irq_thread,
716 					  IRQF_SHARED | IRQF_ONESHOT,
717 					  name, ddata);
718 	if (error) {
719 		dev_err(ddata->dev, "could not get irq %s: %i\n",
720 			name, error);
721 
722 		return error;
723 	}
724 
725 	d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
726 	if (!d)
727 		return -ENOMEM;
728 
729 	d->name = name;
730 	d->irq = irq;
731 	list_add(&d->node, &ddata->irq_list);
732 
733 	return 0;
734 }
735 
736 static const char * const cpcap_charger_irqs[] = {
737 	/* REG_INT_0 */
738 	"chrg_det", "rvrs_chrg",
739 
740 	/* REG_INT1 */
741 	"chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
742 
743 	/* REG_INT_3 */
744 	"battdetb",
745 };
746 
cpcap_usb_init_interrupts(struct platform_device * pdev,struct cpcap_charger_ddata * ddata)747 static int cpcap_usb_init_interrupts(struct platform_device *pdev,
748 				     struct cpcap_charger_ddata *ddata)
749 {
750 	int i, error;
751 
752 	for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
753 		error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
754 		if (error)
755 			return error;
756 	}
757 
758 	return 0;
759 }
760 
cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata * ddata)761 static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
762 {
763 	int i;
764 
765 	for (i = 0; i < 2; i++) {
766 		ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
767 						      i, GPIOD_OUT_HIGH);
768 		if (IS_ERR(ddata->gpio[i])) {
769 			dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
770 				 i, PTR_ERR(ddata->gpio[i]));
771 			ddata->gpio[i] = NULL;
772 		}
773 	}
774 }
775 
cpcap_charger_init_iio(struct cpcap_charger_ddata * ddata)776 static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
777 {
778 	const char * const names[CPCAP_CHARGER_IIO_NR] = {
779 		"battdetb", "battp", "vbus", "chg_isense", "batti",
780 	};
781 	int error, i;
782 
783 	for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
784 		ddata->channels[i] = devm_iio_channel_get(ddata->dev,
785 							  names[i]);
786 		if (IS_ERR(ddata->channels[i])) {
787 			error = PTR_ERR(ddata->channels[i]);
788 			goto out_err;
789 		}
790 
791 		if (!ddata->channels[i]->indio_dev) {
792 			error = -ENXIO;
793 			goto out_err;
794 		}
795 	}
796 
797 	return 0;
798 
799 out_err:
800 	if (error != -EPROBE_DEFER)
801 		dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
802 			error);
803 
804 	return error;
805 }
806 
807 static const struct power_supply_desc cpcap_charger_usb_desc = {
808 	.name		= "usb",
809 	.type		= POWER_SUPPLY_TYPE_USB,
810 	.properties	= cpcap_charger_props,
811 	.num_properties	= ARRAY_SIZE(cpcap_charger_props),
812 	.get_property	= cpcap_charger_get_property,
813 	.set_property	= cpcap_charger_set_property,
814 	.property_is_writeable = cpcap_charger_property_is_writeable,
815 };
816 
817 #ifdef CONFIG_OF
818 static const struct of_device_id cpcap_charger_id_table[] = {
819 	{
820 		.compatible = "motorola,mapphone-cpcap-charger",
821 	},
822 	{},
823 };
824 MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
825 #endif
826 
cpcap_charger_probe(struct platform_device * pdev)827 static int cpcap_charger_probe(struct platform_device *pdev)
828 {
829 	struct cpcap_charger_ddata *ddata;
830 	const struct of_device_id *of_id;
831 	struct power_supply_config psy_cfg = {};
832 	int error;
833 
834 	of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
835 				&pdev->dev);
836 	if (!of_id)
837 		return -EINVAL;
838 
839 	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
840 	if (!ddata)
841 		return -ENOMEM;
842 
843 	ddata->dev = &pdev->dev;
844 	ddata->voltage = 4200000;
845 
846 	ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
847 	if (!ddata->reg)
848 		return -ENODEV;
849 
850 	INIT_LIST_HEAD(&ddata->irq_list);
851 	INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
852 	INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
853 	platform_set_drvdata(pdev, ddata);
854 
855 	error = cpcap_charger_init_iio(ddata);
856 	if (error)
857 		return error;
858 
859 	atomic_set(&ddata->active, 1);
860 
861 	psy_cfg.of_node = pdev->dev.of_node;
862 	psy_cfg.drv_data = ddata;
863 
864 	ddata->usb = devm_power_supply_register(ddata->dev,
865 						&cpcap_charger_usb_desc,
866 						&psy_cfg);
867 	if (IS_ERR(ddata->usb)) {
868 		error = PTR_ERR(ddata->usb);
869 		dev_err(ddata->dev, "failed to register USB charger: %i\n",
870 			error);
871 
872 		return error;
873 	}
874 
875 	error = cpcap_usb_init_interrupts(pdev, ddata);
876 	if (error)
877 		return error;
878 
879 	ddata->comparator.set_vbus = cpcap_charger_set_vbus;
880 	error = omap_usb2_set_comparator(&ddata->comparator);
881 	if (error == -ENODEV) {
882 		dev_info(ddata->dev, "charger needs phy, deferring probe\n");
883 		return -EPROBE_DEFER;
884 	}
885 
886 	cpcap_charger_init_optional_gpios(ddata);
887 
888 	schedule_delayed_work(&ddata->detect_work, 0);
889 
890 	return 0;
891 }
892 
cpcap_charger_remove(struct platform_device * pdev)893 static int cpcap_charger_remove(struct platform_device *pdev)
894 {
895 	struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
896 	int error;
897 
898 	atomic_set(&ddata->active, 0);
899 	error = omap_usb2_set_comparator(NULL);
900 	if (error)
901 		dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
902 			 error);
903 
904 	error = cpcap_charger_set_state(ddata, 0, 0, 0);
905 	if (error)
906 		dev_warn(ddata->dev, "could not clear charger: %i\n",
907 			 error);
908 	cancel_delayed_work_sync(&ddata->vbus_work);
909 	cancel_delayed_work_sync(&ddata->detect_work);
910 
911 	return 0;
912 }
913 
914 static struct platform_driver cpcap_charger_driver = {
915 	.probe = cpcap_charger_probe,
916 	.driver	= {
917 		.name	= "cpcap-charger",
918 		.of_match_table = of_match_ptr(cpcap_charger_id_table),
919 	},
920 	.remove	= cpcap_charger_remove,
921 };
922 module_platform_driver(cpcap_charger_driver);
923 
924 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
925 MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
926 MODULE_LICENSE("GPL v2");
927 MODULE_ALIAS("platform:cpcap-charger");
928