1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * TWL4030/TPS65950 BCI (Battery Charger Interface) driver
4  *
5  * Copyright (C) 2010 Gražvydas Ignotas <notasas@gmail.com>
6  *
7  * based on twl4030_bci_battery.c by TI
8  * Copyright (C) 2008 Texas Instruments, Inc.
9  */
10 
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/mfd/twl.h>
18 #include <linux/power_supply.h>
19 #include <linux/notifier.h>
20 #include <linux/usb/otg.h>
21 #include <linux/iio/consumer.h>
22 
23 #define TWL4030_BCIMDEN		0x00
24 #define TWL4030_BCIMDKEY	0x01
25 #define TWL4030_BCIMSTATEC	0x02
26 #define TWL4030_BCIICHG		0x08
27 #define TWL4030_BCIVAC		0x0a
28 #define TWL4030_BCIVBUS		0x0c
29 #define TWL4030_BCIMFSTS3	0x0F
30 #define TWL4030_BCIMFSTS4	0x10
31 #define TWL4030_BCICTL1		0x23
32 #define TWL4030_BB_CFG		0x12
33 #define TWL4030_BCIIREF1	0x27
34 #define TWL4030_BCIIREF2	0x28
35 #define TWL4030_BCIMFKEY	0x11
36 #define TWL4030_BCIMFEN3	0x14
37 #define TWL4030_BCIMFTH8	0x1d
38 #define TWL4030_BCIMFTH9	0x1e
39 #define TWL4030_BCIWDKEY	0x21
40 
41 #define TWL4030_BCIMFSTS1	0x01
42 
43 #define TWL4030_BCIAUTOWEN	BIT(5)
44 #define TWL4030_CONFIG_DONE	BIT(4)
45 #define TWL4030_CVENAC		BIT(2)
46 #define TWL4030_BCIAUTOUSB	BIT(1)
47 #define TWL4030_BCIAUTOAC	BIT(0)
48 #define TWL4030_CGAIN		BIT(5)
49 #define TWL4030_USBFASTMCHG	BIT(2)
50 #define TWL4030_STS_VBUS	BIT(7)
51 #define TWL4030_STS_USB_ID	BIT(2)
52 #define TWL4030_BBCHEN		BIT(4)
53 #define TWL4030_BBSEL_MASK	0x0c
54 #define TWL4030_BBSEL_2V5	0x00
55 #define TWL4030_BBSEL_3V0	0x04
56 #define TWL4030_BBSEL_3V1	0x08
57 #define TWL4030_BBSEL_3V2	0x0c
58 #define TWL4030_BBISEL_MASK	0x03
59 #define TWL4030_BBISEL_25uA	0x00
60 #define TWL4030_BBISEL_150uA	0x01
61 #define TWL4030_BBISEL_500uA	0x02
62 #define TWL4030_BBISEL_1000uA	0x03
63 
64 #define TWL4030_BATSTSPCHG	BIT(2)
65 #define TWL4030_BATSTSMCHG	BIT(6)
66 
67 /* BCI interrupts */
68 #define TWL4030_WOVF		BIT(0) /* Watchdog overflow */
69 #define TWL4030_TMOVF		BIT(1) /* Timer overflow */
70 #define TWL4030_ICHGHIGH	BIT(2) /* Battery charge current high */
71 #define TWL4030_ICHGLOW		BIT(3) /* Battery cc. low / FSM state change */
72 #define TWL4030_ICHGEOC		BIT(4) /* Battery current end-of-charge */
73 #define TWL4030_TBATOR2		BIT(5) /* Battery temperature out of range 2 */
74 #define TWL4030_TBATOR1		BIT(6) /* Battery temperature out of range 1 */
75 #define TWL4030_BATSTS		BIT(7) /* Battery status */
76 
77 #define TWL4030_VBATLVL		BIT(0) /* VBAT level */
78 #define TWL4030_VBATOV		BIT(1) /* VBAT overvoltage */
79 #define TWL4030_VBUSOV		BIT(2) /* VBUS overvoltage */
80 #define TWL4030_ACCHGOV		BIT(3) /* Ac charger overvoltage */
81 
82 #define TWL4030_MSTATEC_USB		BIT(4)
83 #define TWL4030_MSTATEC_AC		BIT(5)
84 #define TWL4030_MSTATEC_MASK		0x0f
85 #define TWL4030_MSTATEC_QUICK1		0x02
86 #define TWL4030_MSTATEC_QUICK7		0x07
87 #define TWL4030_MSTATEC_COMPLETE1	0x0b
88 #define TWL4030_MSTATEC_COMPLETE4	0x0e
89 
90 /*
91  * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
92  * then AC is available.
93  */
94 static inline int ac_available(struct iio_channel *channel_vac)
95 {
96 	int val, err;
97 
98 	if (!channel_vac)
99 		return 0;
100 
101 	err = iio_read_channel_processed(channel_vac, &val);
102 	if (err < 0)
103 		return 0;
104 	return val > 4500;
105 }
106 
107 static bool allow_usb;
108 module_param(allow_usb, bool, 0644);
109 MODULE_PARM_DESC(allow_usb, "Allow USB charge drawing default current");
110 
111 struct twl4030_bci {
112 	struct device		*dev;
113 	struct power_supply	*ac;
114 	struct power_supply	*usb;
115 	struct usb_phy		*transceiver;
116 	struct notifier_block	usb_nb;
117 	struct work_struct	work;
118 	int			irq_chg;
119 	int			irq_bci;
120 	int			usb_enabled;
121 
122 	/*
123 	 * ichg_* and *_cur values in uA. If any are 'large', we set
124 	 * CGAIN to '1' which doubles the range for half the
125 	 * precision.
126 	 */
127 	unsigned int		ichg_eoc, ichg_lo, ichg_hi;
128 	unsigned int		usb_cur, ac_cur;
129 	struct iio_channel	*channel_vac;
130 	bool			ac_is_active;
131 	int			usb_mode, ac_mode; /* charging mode requested */
132 #define	CHARGE_OFF	0
133 #define	CHARGE_AUTO	1
134 #define	CHARGE_LINEAR	2
135 
136 	/* When setting the USB current we slowly increase the
137 	 * requested current until target is reached or the voltage
138 	 * drops below 4.75V.  In the latter case we step back one
139 	 * step.
140 	 */
141 	unsigned int		usb_cur_target;
142 	struct delayed_work	current_worker;
143 #define	USB_CUR_STEP	20000	/* 20mA at a time */
144 #define	USB_MIN_VOLT	4750000	/* 4.75V */
145 #define	USB_CUR_DELAY	msecs_to_jiffies(100)
146 #define	USB_MAX_CURRENT	1700000 /* TWL4030 caps at 1.7A */
147 
148 	unsigned long		event;
149 };
150 
151 /* strings for 'usb_mode' values */
152 static const char *modes[] = { "off", "auto", "continuous" };
153 
154 /*
155  * clear and set bits on an given register on a given module
156  */
157 static int twl4030_clear_set(u8 mod_no, u8 clear, u8 set, u8 reg)
158 {
159 	u8 val = 0;
160 	int ret;
161 
162 	ret = twl_i2c_read_u8(mod_no, &val, reg);
163 	if (ret)
164 		return ret;
165 
166 	val &= ~clear;
167 	val |= set;
168 
169 	return twl_i2c_write_u8(mod_no, val, reg);
170 }
171 
172 static int twl4030_bci_read(u8 reg, u8 *val)
173 {
174 	return twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, val, reg);
175 }
176 
177 static int twl4030_clear_set_boot_bci(u8 clear, u8 set)
178 {
179 	return twl4030_clear_set(TWL_MODULE_PM_MASTER, clear,
180 			TWL4030_CONFIG_DONE | TWL4030_BCIAUTOWEN | set,
181 			TWL4030_PM_MASTER_BOOT_BCI);
182 }
183 
184 static int twl4030bci_read_adc_val(u8 reg)
185 {
186 	int ret, temp;
187 	u8 val;
188 
189 	/* read MSB */
190 	ret = twl4030_bci_read(reg + 1, &val);
191 	if (ret)
192 		return ret;
193 
194 	temp = (int)(val & 0x03) << 8;
195 
196 	/* read LSB */
197 	ret = twl4030_bci_read(reg, &val);
198 	if (ret)
199 		return ret;
200 
201 	return temp | val;
202 }
203 
204 /*
205  * TI provided formulas:
206  * CGAIN == 0: ICHG = (BCIICHG * 1.7) / (2^10 - 1) - 0.85
207  * CGAIN == 1: ICHG = (BCIICHG * 3.4) / (2^10 - 1) - 1.7
208  * Here we use integer approximation of:
209  * CGAIN == 0: val * 1.6618 - 0.85 * 1000
210  * CGAIN == 1: (val * 1.6618 - 0.85 * 1000) * 2
211  */
212 /*
213  * convert twl register value for currents into uA
214  */
215 static int regval2ua(int regval, bool cgain)
216 {
217 	if (cgain)
218 		return (regval * 16618 - 8500 * 1000) / 5;
219 	else
220 		return (regval * 16618 - 8500 * 1000) / 10;
221 }
222 
223 /*
224  * convert uA currents into twl register value
225  */
226 static int ua2regval(int ua, bool cgain)
227 {
228 	int ret;
229 	if (cgain)
230 		ua /= 2;
231 	ret = (ua * 10 + 8500 * 1000) / 16618;
232 	/* rounding problems */
233 	if (ret < 512)
234 		ret = 512;
235 	return ret;
236 }
237 
238 static int twl4030_charger_update_current(struct twl4030_bci *bci)
239 {
240 	int status;
241 	int cur;
242 	unsigned reg, cur_reg;
243 	u8 bcictl1, oldreg, fullreg;
244 	bool cgain = false;
245 	u8 boot_bci;
246 
247 	/*
248 	 * If AC (Accessory Charger) voltage exceeds 4.5V (MADC 11)
249 	 * and AC is enabled, set current for 'ac'
250 	 */
251 	if (ac_available(bci->channel_vac)) {
252 		cur = bci->ac_cur;
253 		bci->ac_is_active = true;
254 	} else {
255 		cur = bci->usb_cur;
256 		bci->ac_is_active = false;
257 		if (cur > bci->usb_cur_target) {
258 			cur = bci->usb_cur_target;
259 			bci->usb_cur = cur;
260 		}
261 		if (cur < bci->usb_cur_target)
262 			schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
263 	}
264 
265 	/* First, check thresholds and see if cgain is needed */
266 	if (bci->ichg_eoc >= 200000)
267 		cgain = true;
268 	if (bci->ichg_lo >= 400000)
269 		cgain = true;
270 	if (bci->ichg_hi >= 820000)
271 		cgain = true;
272 	if (cur > 852000)
273 		cgain = true;
274 
275 	status = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
276 	if (status < 0)
277 		return status;
278 	if (twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &boot_bci,
279 			    TWL4030_PM_MASTER_BOOT_BCI) < 0)
280 		boot_bci = 0;
281 	boot_bci &= 7;
282 
283 	if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN))
284 		/* Need to turn for charging while we change the
285 		 * CGAIN bit.  Leave it off while everything is
286 		 * updated.
287 		 */
288 		twl4030_clear_set_boot_bci(boot_bci, 0);
289 
290 	/*
291 	 * For ichg_eoc, the hardware only supports reg values matching
292 	 * 100XXXX000, and requires the XXXX be stored in the high nibble
293 	 * of TWL4030_BCIMFTH8.
294 	 */
295 	reg = ua2regval(bci->ichg_eoc, cgain);
296 	if (reg > 0x278)
297 		reg = 0x278;
298 	if (reg < 0x200)
299 		reg = 0x200;
300 	reg = (reg >> 3) & 0xf;
301 	fullreg = reg << 4;
302 
303 	/*
304 	 * For ichg_lo, reg value must match 10XXXX0000.
305 	 * XXXX is stored in low nibble of TWL4030_BCIMFTH8.
306 	 */
307 	reg = ua2regval(bci->ichg_lo, cgain);
308 	if (reg > 0x2F0)
309 		reg = 0x2F0;
310 	if (reg < 0x200)
311 		reg = 0x200;
312 	reg = (reg >> 4) & 0xf;
313 	fullreg |= reg;
314 
315 	/* ichg_eoc and ichg_lo live in same register */
316 	status = twl4030_bci_read(TWL4030_BCIMFTH8, &oldreg);
317 	if (status < 0)
318 		return status;
319 	if (oldreg != fullreg) {
320 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xF4,
321 					  TWL4030_BCIMFKEY);
322 		if (status < 0)
323 			return status;
324 		twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
325 				 fullreg, TWL4030_BCIMFTH8);
326 	}
327 
328 	/* ichg_hi threshold must be 1XXXX01100 (I think) */
329 	reg = ua2regval(bci->ichg_hi, cgain);
330 	if (reg > 0x3E0)
331 		reg = 0x3E0;
332 	if (reg < 0x200)
333 		reg = 0x200;
334 	fullreg = (reg >> 5) & 0xF;
335 	fullreg <<= 4;
336 	status = twl4030_bci_read(TWL4030_BCIMFTH9, &oldreg);
337 	if (status < 0)
338 		return status;
339 	if ((oldreg & 0xF0) != fullreg) {
340 		fullreg |= (oldreg & 0x0F);
341 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
342 					  TWL4030_BCIMFKEY);
343 		if (status < 0)
344 			return status;
345 		twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
346 				 fullreg, TWL4030_BCIMFTH9);
347 	}
348 
349 	/*
350 	 * And finally, set the current.  This is stored in
351 	 * two registers.
352 	 */
353 	reg = ua2regval(cur, cgain);
354 	/* we have only 10 bits */
355 	if (reg > 0x3ff)
356 		reg = 0x3ff;
357 	status = twl4030_bci_read(TWL4030_BCIIREF1, &oldreg);
358 	if (status < 0)
359 		return status;
360 	cur_reg = oldreg;
361 	status = twl4030_bci_read(TWL4030_BCIIREF2, &oldreg);
362 	if (status < 0)
363 		return status;
364 	cur_reg |= oldreg << 8;
365 	if (reg != oldreg) {
366 		/* disable write protection for one write access for
367 		 * BCIIREF */
368 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
369 					  TWL4030_BCIMFKEY);
370 		if (status < 0)
371 			return status;
372 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
373 					  (reg & 0x100) ? 3 : 2,
374 					  TWL4030_BCIIREF2);
375 		if (status < 0)
376 			return status;
377 		/* disable write protection for one write access for
378 		 * BCIIREF */
379 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xE7,
380 					  TWL4030_BCIMFKEY);
381 		if (status < 0)
382 			return status;
383 		status = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
384 					  reg & 0xff,
385 					  TWL4030_BCIIREF1);
386 	}
387 	if ((!!cgain) != !!(bcictl1 & TWL4030_CGAIN)) {
388 		/* Flip CGAIN and re-enable charging */
389 		bcictl1 ^= TWL4030_CGAIN;
390 		twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
391 				 bcictl1, TWL4030_BCICTL1);
392 		twl4030_clear_set_boot_bci(0, boot_bci);
393 	}
394 	return 0;
395 }
396 
397 static int twl4030_charger_get_current(void);
398 
399 static void twl4030_current_worker(struct work_struct *data)
400 {
401 	int v, curr;
402 	int res;
403 	struct twl4030_bci *bci = container_of(data, struct twl4030_bci,
404 					       current_worker.work);
405 
406 	res = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
407 	if (res < 0)
408 		v = 0;
409 	else
410 		/* BCIVBUS uses ADCIN8, 7/1023 V/step */
411 		v = res * 6843;
412 	curr = twl4030_charger_get_current();
413 
414 	dev_dbg(bci->dev, "v=%d cur=%d limit=%d target=%d\n", v, curr,
415 		bci->usb_cur, bci->usb_cur_target);
416 
417 	if (v < USB_MIN_VOLT) {
418 		/* Back up and stop adjusting. */
419 		if (bci->usb_cur >= USB_CUR_STEP)
420 			bci->usb_cur -= USB_CUR_STEP;
421 		bci->usb_cur_target = bci->usb_cur;
422 	} else if (bci->usb_cur >= bci->usb_cur_target ||
423 		   bci->usb_cur + USB_CUR_STEP > USB_MAX_CURRENT) {
424 		/* Reached target and voltage is OK - stop */
425 		return;
426 	} else {
427 		bci->usb_cur += USB_CUR_STEP;
428 		schedule_delayed_work(&bci->current_worker, USB_CUR_DELAY);
429 	}
430 	twl4030_charger_update_current(bci);
431 }
432 
433 /*
434  * Enable/Disable USB Charge functionality.
435  */
436 static int twl4030_charger_enable_usb(struct twl4030_bci *bci, bool enable)
437 {
438 	int ret;
439 	u32 reg;
440 
441 	if (bci->usb_mode == CHARGE_OFF)
442 		enable = false;
443 	if (enable && !IS_ERR_OR_NULL(bci->transceiver)) {
444 
445 		twl4030_charger_update_current(bci);
446 
447 		/* Need to keep phy powered */
448 		if (!bci->usb_enabled) {
449 			pm_runtime_get_sync(bci->transceiver->dev);
450 			bci->usb_enabled = 1;
451 		}
452 
453 		if (bci->usb_mode == CHARGE_AUTO) {
454 			/* Enable interrupts now. */
455 			reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC |
456 					TWL4030_TBATOR2 | TWL4030_TBATOR1 |
457 					TWL4030_BATSTS);
458 			ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
459 				       TWL4030_INTERRUPTS_BCIIMR1A);
460 			if (ret < 0) {
461 				dev_err(bci->dev,
462 					"failed to unmask interrupts: %d\n",
463 					ret);
464 				return ret;
465 			}
466 			/* forcing the field BCIAUTOUSB (BOOT_BCI[1]) to 1 */
467 			ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOUSB);
468 		}
469 
470 		/* forcing USBFASTMCHG(BCIMFSTS4[2]) to 1 */
471 		ret = twl4030_clear_set(TWL_MODULE_MAIN_CHARGE, 0,
472 			TWL4030_USBFASTMCHG, TWL4030_BCIMFSTS4);
473 		if (bci->usb_mode == CHARGE_LINEAR) {
474 			/* Enable interrupts now. */
475 			reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_TBATOR2 |
476 					TWL4030_TBATOR1 | TWL4030_BATSTS);
477 			ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
478 				       TWL4030_INTERRUPTS_BCIIMR1A);
479 			if (ret < 0) {
480 				dev_err(bci->dev,
481 					"failed to unmask interrupts: %d\n",
482 					ret);
483 				return ret;
484 			}
485 			twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC|TWL4030_CVENAC, 0);
486 			/* Watch dog key: WOVF acknowledge */
487 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x33,
488 					       TWL4030_BCIWDKEY);
489 			/* 0x24 + EKEY6: off mode */
490 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
491 					       TWL4030_BCIMDKEY);
492 			/* EKEY2: Linear charge: USB path */
493 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x26,
494 					       TWL4030_BCIMDKEY);
495 			/* WDKEY5: stop watchdog count */
496 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf3,
497 					       TWL4030_BCIWDKEY);
498 			/* enable MFEN3 access */
499 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x9c,
500 					       TWL4030_BCIMFKEY);
501 			 /* ICHGEOCEN - end-of-charge monitor (current < 80mA)
502 			  *                      (charging continues)
503 			  * ICHGLOWEN - current level monitor (charge continues)
504 			  * don't monitor over-current or heat save
505 			  */
506 			ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0xf0,
507 					       TWL4030_BCIMFEN3);
508 		}
509 	} else {
510 		ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOUSB, 0);
511 		ret |= twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE, 0x2a,
512 					TWL4030_BCIMDKEY);
513 		if (bci->usb_enabled) {
514 			pm_runtime_mark_last_busy(bci->transceiver->dev);
515 			pm_runtime_put_autosuspend(bci->transceiver->dev);
516 			bci->usb_enabled = 0;
517 		}
518 		bci->usb_cur = 0;
519 	}
520 
521 	return ret;
522 }
523 
524 /*
525  * Enable/Disable AC Charge funtionality.
526  */
527 static int twl4030_charger_enable_ac(struct twl4030_bci *bci, bool enable)
528 {
529 	int ret;
530 
531 	if (bci->ac_mode == CHARGE_OFF)
532 		enable = false;
533 
534 	if (enable)
535 		ret = twl4030_clear_set_boot_bci(0, TWL4030_BCIAUTOAC);
536 	else
537 		ret = twl4030_clear_set_boot_bci(TWL4030_BCIAUTOAC, 0);
538 
539 	return ret;
540 }
541 
542 /*
543  * Enable/Disable charging of Backup Battery.
544  */
545 static int twl4030_charger_enable_backup(int uvolt, int uamp)
546 {
547 	int ret;
548 	u8 flags;
549 
550 	if (uvolt < 2500000 ||
551 	    uamp < 25) {
552 		/* disable charging of backup battery */
553 		ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
554 					TWL4030_BBCHEN, 0, TWL4030_BB_CFG);
555 		return ret;
556 	}
557 
558 	flags = TWL4030_BBCHEN;
559 	if (uvolt >= 3200000)
560 		flags |= TWL4030_BBSEL_3V2;
561 	else if (uvolt >= 3100000)
562 		flags |= TWL4030_BBSEL_3V1;
563 	else if (uvolt >= 3000000)
564 		flags |= TWL4030_BBSEL_3V0;
565 	else
566 		flags |= TWL4030_BBSEL_2V5;
567 
568 	if (uamp >= 1000)
569 		flags |= TWL4030_BBISEL_1000uA;
570 	else if (uamp >= 500)
571 		flags |= TWL4030_BBISEL_500uA;
572 	else if (uamp >= 150)
573 		flags |= TWL4030_BBISEL_150uA;
574 	else
575 		flags |= TWL4030_BBISEL_25uA;
576 
577 	ret = twl4030_clear_set(TWL_MODULE_PM_RECEIVER,
578 				TWL4030_BBSEL_MASK | TWL4030_BBISEL_MASK,
579 				flags,
580 				TWL4030_BB_CFG);
581 
582 	return ret;
583 }
584 
585 /*
586  * TWL4030 CHG_PRES (AC charger presence) events
587  */
588 static irqreturn_t twl4030_charger_interrupt(int irq, void *arg)
589 {
590 	struct twl4030_bci *bci = arg;
591 
592 	dev_dbg(bci->dev, "CHG_PRES irq\n");
593 	/* reset current on each 'plug' event */
594 	bci->ac_cur = 500000;
595 	twl4030_charger_update_current(bci);
596 	power_supply_changed(bci->ac);
597 	power_supply_changed(bci->usb);
598 
599 	return IRQ_HANDLED;
600 }
601 
602 /*
603  * TWL4030 BCI monitoring events
604  */
605 static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
606 {
607 	struct twl4030_bci *bci = arg;
608 	u8 irqs1, irqs2;
609 	int ret;
610 
611 	ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs1,
612 			      TWL4030_INTERRUPTS_BCIISR1A);
613 	if (ret < 0)
614 		return IRQ_HANDLED;
615 
616 	ret = twl_i2c_read_u8(TWL4030_MODULE_INTERRUPTS, &irqs2,
617 			      TWL4030_INTERRUPTS_BCIISR2A);
618 	if (ret < 0)
619 		return IRQ_HANDLED;
620 
621 	dev_dbg(bci->dev, "BCI irq %02x %02x\n", irqs2, irqs1);
622 
623 	if (irqs1 & (TWL4030_ICHGLOW | TWL4030_ICHGEOC)) {
624 		/* charger state change, inform the core */
625 		power_supply_changed(bci->ac);
626 		power_supply_changed(bci->usb);
627 	}
628 	twl4030_charger_update_current(bci);
629 
630 	/* various monitoring events, for now we just log them here */
631 	if (irqs1 & (TWL4030_TBATOR2 | TWL4030_TBATOR1))
632 		dev_warn(bci->dev, "battery temperature out of range\n");
633 
634 	if (irqs1 & TWL4030_BATSTS)
635 		dev_crit(bci->dev, "battery disconnected\n");
636 
637 	if (irqs2 & TWL4030_VBATOV)
638 		dev_crit(bci->dev, "VBAT overvoltage\n");
639 
640 	if (irqs2 & TWL4030_VBUSOV)
641 		dev_crit(bci->dev, "VBUS overvoltage\n");
642 
643 	if (irqs2 & TWL4030_ACCHGOV)
644 		dev_crit(bci->dev, "Ac charger overvoltage\n");
645 
646 	return IRQ_HANDLED;
647 }
648 
649 static void twl4030_bci_usb_work(struct work_struct *data)
650 {
651 	struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
652 
653 	switch (bci->event) {
654 	case USB_EVENT_VBUS:
655 	case USB_EVENT_CHARGER:
656 		twl4030_charger_enable_usb(bci, true);
657 		break;
658 	case USB_EVENT_NONE:
659 		twl4030_charger_enable_usb(bci, false);
660 		break;
661 	}
662 }
663 
664 static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
665 			       void *priv)
666 {
667 	struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, usb_nb);
668 
669 	dev_dbg(bci->dev, "OTG notify %lu\n", val);
670 
671 	/* reset current on each 'plug' event */
672 	if (allow_usb)
673 		bci->usb_cur_target = 500000;
674 	else
675 		bci->usb_cur_target = 100000;
676 
677 	bci->event = val;
678 	schedule_work(&bci->work);
679 
680 	return NOTIFY_OK;
681 }
682 
683 /*
684  * sysfs charger enabled store
685  */
686 static ssize_t
687 twl4030_bci_mode_store(struct device *dev, struct device_attribute *attr,
688 			  const char *buf, size_t n)
689 {
690 	struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
691 	int mode;
692 	int status;
693 
694 	mode = sysfs_match_string(modes, buf);
695 	if (mode < 0)
696 		return mode;
697 
698 	if (dev == &bci->ac->dev) {
699 		if (mode == 2)
700 			return -EINVAL;
701 		twl4030_charger_enable_ac(bci, false);
702 		bci->ac_mode = mode;
703 		status = twl4030_charger_enable_ac(bci, true);
704 	} else {
705 		twl4030_charger_enable_usb(bci, false);
706 		bci->usb_mode = mode;
707 		status = twl4030_charger_enable_usb(bci, true);
708 	}
709 	return (status == 0) ? n : status;
710 }
711 
712 /*
713  * sysfs charger enabled show
714  */
715 static ssize_t
716 twl4030_bci_mode_show(struct device *dev,
717 			     struct device_attribute *attr, char *buf)
718 {
719 	struct twl4030_bci *bci = dev_get_drvdata(dev->parent);
720 	int len = 0;
721 	int i;
722 	int mode = bci->usb_mode;
723 
724 	if (dev == &bci->ac->dev)
725 		mode = bci->ac_mode;
726 
727 	for (i = 0; i < ARRAY_SIZE(modes); i++)
728 		if (mode == i)
729 			len += sysfs_emit_at(buf, len, "[%s] ", modes[i]);
730 		else
731 			len += sysfs_emit_at(buf, len, "%s ", modes[i]);
732 	buf[len-1] = '\n';
733 	return len;
734 }
735 static DEVICE_ATTR(mode, 0644, twl4030_bci_mode_show,
736 		   twl4030_bci_mode_store);
737 
738 static int twl4030_charger_get_current(void)
739 {
740 	int curr;
741 	int ret;
742 	u8 bcictl1;
743 
744 	curr = twl4030bci_read_adc_val(TWL4030_BCIICHG);
745 	if (curr < 0)
746 		return curr;
747 
748 	ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
749 	if (ret)
750 		return ret;
751 
752 	return regval2ua(curr, bcictl1 & TWL4030_CGAIN);
753 }
754 
755 /*
756  * Returns the main charge FSM state
757  * Or < 0 on failure.
758  */
759 static int twl4030bci_state(struct twl4030_bci *bci)
760 {
761 	int ret;
762 	u8 state;
763 
764 	ret = twl4030_bci_read(TWL4030_BCIMSTATEC, &state);
765 	if (ret) {
766 		dev_err(bci->dev, "error reading BCIMSTATEC\n");
767 		return ret;
768 	}
769 
770 	dev_dbg(bci->dev, "state: %02x\n", state);
771 
772 	return state;
773 }
774 
775 static int twl4030_bci_state_to_status(int state)
776 {
777 	state &= TWL4030_MSTATEC_MASK;
778 	if (TWL4030_MSTATEC_QUICK1 <= state && state <= TWL4030_MSTATEC_QUICK7)
779 		return POWER_SUPPLY_STATUS_CHARGING;
780 	else if (TWL4030_MSTATEC_COMPLETE1 <= state &&
781 					state <= TWL4030_MSTATEC_COMPLETE4)
782 		return POWER_SUPPLY_STATUS_FULL;
783 	else
784 		return POWER_SUPPLY_STATUS_NOT_CHARGING;
785 }
786 
787 static int twl4030_bci_get_property(struct power_supply *psy,
788 				    enum power_supply_property psp,
789 				    union power_supply_propval *val)
790 {
791 	struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
792 	int is_charging;
793 	int state;
794 	int ret;
795 
796 	state = twl4030bci_state(bci);
797 	if (state < 0)
798 		return state;
799 
800 	if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
801 		is_charging = state & TWL4030_MSTATEC_USB;
802 	else
803 		is_charging = state & TWL4030_MSTATEC_AC;
804 	if (!is_charging) {
805 		u8 s;
806 		ret = twl4030_bci_read(TWL4030_BCIMDEN, &s);
807 		if (ret < 0)
808 			return ret;
809 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
810 			is_charging = s & 1;
811 		else
812 			is_charging = s & 2;
813 		if (is_charging)
814 			/* A little white lie */
815 			state = TWL4030_MSTATEC_QUICK1;
816 	}
817 
818 	switch (psp) {
819 	case POWER_SUPPLY_PROP_STATUS:
820 		if (is_charging)
821 			val->intval = twl4030_bci_state_to_status(state);
822 		else
823 			val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
824 		break;
825 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
826 		/* charging must be active for meaningful result */
827 		if (!is_charging)
828 			return -ENODATA;
829 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB) {
830 			ret = twl4030bci_read_adc_val(TWL4030_BCIVBUS);
831 			if (ret < 0)
832 				return ret;
833 			/* BCIVBUS uses ADCIN8, 7/1023 V/step */
834 			val->intval = ret * 6843;
835 		} else {
836 			ret = twl4030bci_read_adc_val(TWL4030_BCIVAC);
837 			if (ret < 0)
838 				return ret;
839 			/* BCIVAC uses ADCIN11, 10/1023 V/step */
840 			val->intval = ret * 9775;
841 		}
842 		break;
843 	case POWER_SUPPLY_PROP_CURRENT_NOW:
844 		if (!is_charging)
845 			return -ENODATA;
846 		/* current measurement is shared between AC and USB */
847 		ret = twl4030_charger_get_current();
848 		if (ret < 0)
849 			return ret;
850 		val->intval = ret;
851 		break;
852 	case POWER_SUPPLY_PROP_ONLINE:
853 		val->intval = is_charging &&
854 			twl4030_bci_state_to_status(state) !=
855 				POWER_SUPPLY_STATUS_NOT_CHARGING;
856 		break;
857 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
858 		val->intval = -1;
859 		if (psy->desc->type != POWER_SUPPLY_TYPE_USB) {
860 			if (!bci->ac_is_active)
861 				val->intval = bci->ac_cur;
862 		} else {
863 			if (bci->ac_is_active)
864 				val->intval = bci->usb_cur_target;
865 		}
866 		if (val->intval < 0) {
867 			u8 bcictl1;
868 
869 			val->intval = twl4030bci_read_adc_val(TWL4030_BCIIREF1);
870 			if (val->intval < 0)
871 				return val->intval;
872 			ret = twl4030_bci_read(TWL4030_BCICTL1, &bcictl1);
873 			if (ret < 0)
874 				return ret;
875 			val->intval = regval2ua(val->intval, bcictl1 &
876 							TWL4030_CGAIN);
877 		}
878 		break;
879 	default:
880 		return -EINVAL;
881 	}
882 
883 	return 0;
884 }
885 
886 static int twl4030_bci_set_property(struct power_supply *psy,
887 				    enum power_supply_property psp,
888 				    const union power_supply_propval *val)
889 {
890 	struct twl4030_bci *bci = dev_get_drvdata(psy->dev.parent);
891 
892 	switch (psp) {
893 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
894 		if (psy->desc->type == POWER_SUPPLY_TYPE_USB)
895 			bci->usb_cur_target = val->intval;
896 		else
897 			bci->ac_cur = val->intval;
898 		twl4030_charger_update_current(bci);
899 		break;
900 	default:
901 		return -EINVAL;
902 	}
903 
904 	return 0;
905 }
906 
907 static int twl4030_bci_property_is_writeable(struct power_supply *psy,
908 				      enum power_supply_property psp)
909 {
910 	switch (psp) {
911 	case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
912 		return true;
913 	default:
914 		return false;
915 	}
916 }
917 
918 static enum power_supply_property twl4030_charger_props[] = {
919 	POWER_SUPPLY_PROP_STATUS,
920 	POWER_SUPPLY_PROP_ONLINE,
921 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
922 	POWER_SUPPLY_PROP_CURRENT_NOW,
923 	POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
924 };
925 
926 #ifdef CONFIG_OF
927 static const struct twl4030_bci_platform_data *
928 twl4030_bci_parse_dt(struct device *dev)
929 {
930 	struct device_node *np = dev->of_node;
931 	struct twl4030_bci_platform_data *pdata;
932 	u32 num;
933 
934 	if (!np)
935 		return NULL;
936 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
937 	if (!pdata)
938 		return pdata;
939 
940 	if (of_property_read_u32(np, "ti,bb-uvolt", &num) == 0)
941 		pdata->bb_uvolt = num;
942 	if (of_property_read_u32(np, "ti,bb-uamp", &num) == 0)
943 		pdata->bb_uamp = num;
944 	return pdata;
945 }
946 #else
947 static inline const struct twl4030_bci_platform_data *
948 twl4030_bci_parse_dt(struct device *dev)
949 {
950 	return NULL;
951 }
952 #endif
953 
954 static const struct power_supply_desc twl4030_bci_ac_desc = {
955 	.name		= "twl4030_ac",
956 	.type		= POWER_SUPPLY_TYPE_MAINS,
957 	.properties	= twl4030_charger_props,
958 	.num_properties	= ARRAY_SIZE(twl4030_charger_props),
959 	.get_property	= twl4030_bci_get_property,
960 	.set_property	= twl4030_bci_set_property,
961 	.property_is_writeable	= twl4030_bci_property_is_writeable,
962 };
963 
964 static const struct power_supply_desc twl4030_bci_usb_desc = {
965 	.name		= "twl4030_usb",
966 	.type		= POWER_SUPPLY_TYPE_USB,
967 	.properties	= twl4030_charger_props,
968 	.num_properties	= ARRAY_SIZE(twl4030_charger_props),
969 	.get_property	= twl4030_bci_get_property,
970 	.set_property	= twl4030_bci_set_property,
971 	.property_is_writeable	= twl4030_bci_property_is_writeable,
972 };
973 
974 static int twl4030_bci_probe(struct platform_device *pdev)
975 {
976 	struct twl4030_bci *bci;
977 	const struct twl4030_bci_platform_data *pdata = pdev->dev.platform_data;
978 	int ret;
979 	u32 reg;
980 
981 	bci = devm_kzalloc(&pdev->dev, sizeof(*bci), GFP_KERNEL);
982 	if (bci == NULL)
983 		return -ENOMEM;
984 
985 	if (!pdata)
986 		pdata = twl4030_bci_parse_dt(&pdev->dev);
987 
988 	bci->ichg_eoc = 80100; /* Stop charging when current drops to here */
989 	bci->ichg_lo = 241000; /* Low threshold */
990 	bci->ichg_hi = 500000; /* High threshold */
991 	bci->ac_cur = 500000; /* 500mA */
992 	if (allow_usb)
993 		bci->usb_cur_target = 500000;  /* 500mA */
994 	else
995 		bci->usb_cur_target = 100000;  /* 100mA */
996 	bci->usb_mode = CHARGE_AUTO;
997 	bci->ac_mode = CHARGE_AUTO;
998 
999 	bci->dev = &pdev->dev;
1000 	bci->irq_chg = platform_get_irq(pdev, 0);
1001 	bci->irq_bci = platform_get_irq(pdev, 1);
1002 
1003 	platform_set_drvdata(pdev, bci);
1004 
1005 	INIT_WORK(&bci->work, twl4030_bci_usb_work);
1006 	INIT_DELAYED_WORK(&bci->current_worker, twl4030_current_worker);
1007 
1008 	bci->channel_vac = devm_iio_channel_get(&pdev->dev, "vac");
1009 	if (IS_ERR(bci->channel_vac)) {
1010 		ret = PTR_ERR(bci->channel_vac);
1011 		if (ret == -EPROBE_DEFER)
1012 			return ret;	/* iio not ready */
1013 		dev_warn(&pdev->dev, "could not request vac iio channel (%d)",
1014 			ret);
1015 		bci->channel_vac = NULL;
1016 	}
1017 
1018 	if (bci->dev->of_node) {
1019 		struct device_node *phynode;
1020 
1021 		phynode = of_get_compatible_child(bci->dev->of_node->parent,
1022 						  "ti,twl4030-usb");
1023 		if (phynode) {
1024 			bci->usb_nb.notifier_call = twl4030_bci_usb_ncb;
1025 			bci->transceiver = devm_usb_get_phy_by_node(
1026 				bci->dev, phynode, &bci->usb_nb);
1027 			of_node_put(phynode);
1028 			if (IS_ERR(bci->transceiver)) {
1029 				ret = PTR_ERR(bci->transceiver);
1030 				if (ret == -EPROBE_DEFER)
1031 					return ret;	/* phy not ready */
1032 				dev_warn(&pdev->dev, "could not request transceiver (%d)",
1033 					ret);
1034 				bci->transceiver = NULL;
1035 			}
1036 		}
1037 	}
1038 
1039 	bci->ac = devm_power_supply_register(&pdev->dev, &twl4030_bci_ac_desc,
1040 					     NULL);
1041 	if (IS_ERR(bci->ac)) {
1042 		ret = PTR_ERR(bci->ac);
1043 		dev_err(&pdev->dev, "failed to register ac: %d\n", ret);
1044 		return ret;
1045 	}
1046 
1047 	bci->usb = devm_power_supply_register(&pdev->dev, &twl4030_bci_usb_desc,
1048 					      NULL);
1049 	if (IS_ERR(bci->usb)) {
1050 		ret = PTR_ERR(bci->usb);
1051 		dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
1052 		return ret;
1053 	}
1054 
1055 	ret = devm_request_threaded_irq(&pdev->dev, bci->irq_chg, NULL,
1056 			twl4030_charger_interrupt, IRQF_ONESHOT, pdev->name,
1057 			bci);
1058 	if (ret < 0) {
1059 		dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1060 			bci->irq_chg, ret);
1061 		return ret;
1062 	}
1063 
1064 	ret = devm_request_threaded_irq(&pdev->dev, bci->irq_bci, NULL,
1065 			twl4030_bci_interrupt, IRQF_ONESHOT, pdev->name, bci);
1066 	if (ret < 0) {
1067 		dev_err(&pdev->dev, "could not request irq %d, status %d\n",
1068 			bci->irq_bci, ret);
1069 		return ret;
1070 	}
1071 
1072 	/* Enable interrupts now. */
1073 	reg = ~(u32)(TWL4030_ICHGLOW | TWL4030_ICHGEOC | TWL4030_TBATOR2 |
1074 		TWL4030_TBATOR1 | TWL4030_BATSTS);
1075 	ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1076 			       TWL4030_INTERRUPTS_BCIIMR1A);
1077 	if (ret < 0) {
1078 		dev_err(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1079 		return ret;
1080 	}
1081 
1082 	reg = ~(u32)(TWL4030_VBATOV | TWL4030_VBUSOV | TWL4030_ACCHGOV);
1083 	ret = twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, reg,
1084 			       TWL4030_INTERRUPTS_BCIIMR2A);
1085 	if (ret < 0)
1086 		dev_warn(&pdev->dev, "failed to unmask interrupts: %d\n", ret);
1087 
1088 	twl4030_charger_update_current(bci);
1089 	if (device_create_file(&bci->usb->dev, &dev_attr_mode))
1090 		dev_warn(&pdev->dev, "could not create sysfs file\n");
1091 	if (device_create_file(&bci->ac->dev, &dev_attr_mode))
1092 		dev_warn(&pdev->dev, "could not create sysfs file\n");
1093 
1094 	twl4030_charger_enable_ac(bci, true);
1095 	if (!IS_ERR_OR_NULL(bci->transceiver))
1096 		twl4030_bci_usb_ncb(&bci->usb_nb,
1097 				    bci->transceiver->last_event,
1098 				    NULL);
1099 	else
1100 		twl4030_charger_enable_usb(bci, false);
1101 	if (pdata)
1102 		twl4030_charger_enable_backup(pdata->bb_uvolt,
1103 					      pdata->bb_uamp);
1104 	else
1105 		twl4030_charger_enable_backup(0, 0);
1106 
1107 	return 0;
1108 }
1109 
1110 static int twl4030_bci_remove(struct platform_device *pdev)
1111 {
1112 	struct twl4030_bci *bci = platform_get_drvdata(pdev);
1113 
1114 	twl4030_charger_enable_ac(bci, false);
1115 	twl4030_charger_enable_usb(bci, false);
1116 	twl4030_charger_enable_backup(0, 0);
1117 
1118 	device_remove_file(&bci->usb->dev, &dev_attr_mode);
1119 	device_remove_file(&bci->ac->dev, &dev_attr_mode);
1120 	/* mask interrupts */
1121 	twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1122 			 TWL4030_INTERRUPTS_BCIIMR1A);
1123 	twl_i2c_write_u8(TWL4030_MODULE_INTERRUPTS, 0xff,
1124 			 TWL4030_INTERRUPTS_BCIIMR2A);
1125 
1126 	return 0;
1127 }
1128 
1129 static const struct of_device_id twl_bci_of_match[] __maybe_unused = {
1130 	{.compatible = "ti,twl4030-bci", },
1131 	{ }
1132 };
1133 MODULE_DEVICE_TABLE(of, twl_bci_of_match);
1134 
1135 static struct platform_driver twl4030_bci_driver = {
1136 	.probe = twl4030_bci_probe,
1137 	.remove	= twl4030_bci_remove,
1138 	.driver	= {
1139 		.name	= "twl4030_bci",
1140 		.of_match_table = of_match_ptr(twl_bci_of_match),
1141 	},
1142 };
1143 module_platform_driver(twl4030_bci_driver);
1144 
1145 MODULE_AUTHOR("Gražvydas Ignotas");
1146 MODULE_DESCRIPTION("TWL4030 Battery Charger Interface driver");
1147 MODULE_LICENSE("GPL");
1148 MODULE_ALIAS("platform:twl4030_bci");
1149