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