xref: /linux/drivers/mfd/tps65010.c (revision 0227b49b50276657243e54f5609e65c4f0eaaf4d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tps65010 - driver for tps6501x power management chips
4  *
5  * Copyright (C) 2004 Texas Instruments
6  * Copyright (C) 2004-2005 David Brownell
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
16 #include <linux/workqueue.h>
17 #include <linux/debugfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/string_choices.h>
20 #include <linux/mutex.h>
21 #include <linux/platform_device.h>
22 
23 #include <linux/mfd/tps65010.h>
24 
25 #include <linux/gpio/driver.h>
26 
27 
28 /*-------------------------------------------------------------------------*/
29 
30 #define	DRIVER_VERSION	"2 May 2005"
31 #define	DRIVER_NAME	(tps65010_driver.driver.name)
32 
33 MODULE_DESCRIPTION("TPS6501x Power Management Driver");
34 MODULE_LICENSE("GPL");
35 
36 static struct i2c_driver tps65010_driver;
37 
38 /*-------------------------------------------------------------------------*/
39 
40 /* This driver handles a family of multipurpose chips, which incorporate
41  * voltage regulators, lithium ion/polymer battery charging, GPIOs, LEDs,
42  * and other features often needed in portable devices like cell phones
43  * or digital cameras.
44  *
45  * The tps65011 and tps65013 have different voltage settings compared
46  * to tps65010 and tps65012.  The tps65013 has a NO_CHG status/irq.
47  * All except tps65010 have "wait" mode, possibly defaulted so that
48  * battery-insert != device-on.
49  *
50  * We could distinguish between some models by checking VDCDC1.UVLO or
51  * other registers, unless they've been changed already after powerup
52  * as part of board setup by a bootloader.
53  */
54 enum tps_model {
55 	TPS65010,
56 	TPS65011,
57 	TPS65012,
58 	TPS65013,
59 };
60 
61 struct tps65010 {
62 	struct i2c_client	*client;
63 	struct mutex		lock;
64 	struct delayed_work	work;
65 	struct dentry		*file;
66 	unsigned		charging:1;
67 	unsigned		por:1;
68 	unsigned		model:8;
69 	u16			vbus;
70 	unsigned long		flags;
71 #define	FLAG_VBUS_CHANGED	0
72 #define	FLAG_IRQ_ENABLE		1
73 
74 	/* copies of last register state */
75 	u8			chgstatus, regstatus, chgconf;
76 	u8			nmask1, nmask2;
77 
78 	u8			outmask;
79 	struct gpio_chip	chip;
80 	struct platform_device	*leds;
81 };
82 
83 #define	POWER_POLL_DELAY	msecs_to_jiffies(5000)
84 
85 /*-------------------------------------------------------------------------*/
86 
87 #if	defined(DEBUG) || defined(CONFIG_DEBUG_FS)
88 
dbg_chgstat(char * buf,size_t len,u8 chgstatus)89 static void dbg_chgstat(char *buf, size_t len, u8 chgstatus)
90 {
91 	snprintf(buf, len, "%02x%s%s%s%s%s%s%s%s\n",
92 		chgstatus,
93 		(chgstatus & TPS_CHG_USB) ? " USB" : "",
94 		(chgstatus & TPS_CHG_AC) ? " AC" : "",
95 		(chgstatus & TPS_CHG_THERM) ? " therm" : "",
96 		(chgstatus & TPS_CHG_TERM) ? " done" :
97 			((chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
98 				? " (charging)" : ""),
99 		(chgstatus & TPS_CHG_TAPER_TMO) ? " taper_tmo" : "",
100 		(chgstatus & TPS_CHG_CHG_TMO) ? " charge_tmo" : "",
101 		(chgstatus & TPS_CHG_PRECHG_TMO) ? " prechg_tmo" : "",
102 		(chgstatus & TPS_CHG_TEMP_ERR) ? " temp_err" : "");
103 }
104 
dbg_regstat(char * buf,size_t len,u8 regstatus)105 static void dbg_regstat(char *buf, size_t len, u8 regstatus)
106 {
107 	snprintf(buf, len, "%02x %s%s%s%s%s%s%s%s\n",
108 		regstatus,
109 		(regstatus & TPS_REG_ONOFF) ? "off" : "(on)",
110 		(regstatus & TPS_REG_COVER) ? " uncover" : "",
111 		(regstatus & TPS_REG_UVLO) ? " UVLO" : "",
112 		(regstatus & TPS_REG_NO_CHG) ? " NO_CHG" : "",
113 		(regstatus & TPS_REG_PG_LD02) ? " ld02_bad" : "",
114 		(regstatus & TPS_REG_PG_LD01) ? " ld01_bad" : "",
115 		(regstatus & TPS_REG_PG_MAIN) ? " main_bad" : "",
116 		(regstatus & TPS_REG_PG_CORE) ? " core_bad" : "");
117 }
118 
dbg_chgconf(int por,char * buf,size_t len,u8 chgconfig)119 static void dbg_chgconf(int por, char *buf, size_t len, u8 chgconfig)
120 {
121 	const char *hibit;
122 
123 	if (por)
124 		hibit = (chgconfig & TPS_CHARGE_POR)
125 				? "POR=69ms" : "POR=1sec";
126 	else
127 		hibit = (chgconfig & TPS65013_AUA) ? "AUA" : "";
128 
129 	snprintf(buf, len, "%02x %s%s%s AC=%d%% USB=%dmA %sCharge\n",
130 		chgconfig, hibit,
131 		(chgconfig & TPS_CHARGE_RESET) ? " reset" : "",
132 		(chgconfig & TPS_CHARGE_FAST) ? " fast" : "",
133 		({int p; switch ((chgconfig >> 3) & 3) {
134 		case 3:		p = 100; break;
135 		case 2:		p = 75; break;
136 		case 1:		p = 50; break;
137 		default:	p = 25; break;
138 		}; p; }),
139 		(chgconfig & TPS_VBUS_CHARGING)
140 			? ((chgconfig & TPS_VBUS_500MA) ? 500 : 100)
141 			: 0,
142 		(chgconfig & TPS_CHARGE_ENABLE) ? "" : "No");
143 }
144 
145 #endif
146 
147 #ifdef	DEBUG
148 
show_chgstatus(const char * label,u8 chgstatus)149 static void show_chgstatus(const char *label, u8 chgstatus)
150 {
151 	char buf [100];
152 
153 	dbg_chgstat(buf, sizeof buf, chgstatus);
154 	pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
155 }
156 
show_regstatus(const char * label,u8 regstatus)157 static void show_regstatus(const char *label, u8 regstatus)
158 {
159 	char buf [100];
160 
161 	dbg_regstat(buf, sizeof buf, regstatus);
162 	pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
163 }
164 
show_chgconfig(int por,const char * label,u8 chgconfig)165 static void show_chgconfig(int por, const char *label, u8 chgconfig)
166 {
167 	char buf [100];
168 
169 	dbg_chgconf(por, buf, sizeof buf, chgconfig);
170 	pr_debug("%s: %s %s", DRIVER_NAME, label, buf);
171 }
172 
173 #else
174 
show_chgstatus(const char * label,u8 chgstatus)175 static inline void show_chgstatus(const char *label, u8 chgstatus) { }
show_regstatus(const char * label,u8 chgstatus)176 static inline void show_regstatus(const char *label, u8 chgstatus) { }
show_chgconfig(int por,const char * label,u8 chgconfig)177 static inline void show_chgconfig(int por, const char *label, u8 chgconfig) { }
178 
179 #endif
180 
181 #ifdef	CONFIG_DEBUG_FS
182 
dbg_show(struct seq_file * s,void * _)183 static int dbg_show(struct seq_file *s, void *_)
184 {
185 	struct tps65010	*tps = s->private;
186 	u8		value, v2;
187 	unsigned	i;
188 	char		buf[100];
189 	const char	*chip;
190 
191 	switch (tps->model) {
192 	case TPS65010:	chip = "tps65010"; break;
193 	case TPS65011:	chip = "tps65011"; break;
194 	case TPS65012:	chip = "tps65012"; break;
195 	case TPS65013:	chip = "tps65013"; break;
196 	default:	chip = NULL; break;
197 	}
198 	seq_printf(s, "driver  %s\nversion %s\nchip    %s\n\n",
199 			DRIVER_NAME, DRIVER_VERSION, chip);
200 
201 	mutex_lock(&tps->lock);
202 
203 	/* FIXME how can we tell whether a battery is present?
204 	 * likely involves a charge gauging chip (like BQ26501).
205 	 */
206 
207 	seq_printf(s, "%scharging\n\n", tps->charging ? "" : "(not) ");
208 
209 
210 	/* registers for monitoring battery charging and status; note
211 	 * that reading chgstat and regstat may ack IRQs...
212 	 */
213 	value = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG);
214 	dbg_chgconf(tps->por, buf, sizeof buf, value);
215 	seq_printf(s, "chgconfig %s", buf);
216 
217 	value = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS);
218 	dbg_chgstat(buf, sizeof buf, value);
219 	seq_printf(s, "chgstat   %s", buf);
220 	value = i2c_smbus_read_byte_data(tps->client, TPS_MASK1);
221 	dbg_chgstat(buf, sizeof buf, value);
222 	seq_printf(s, "mask1     %s", buf);
223 	/* ignore ackint1 */
224 
225 	value = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS);
226 	dbg_regstat(buf, sizeof buf, value);
227 	seq_printf(s, "regstat   %s", buf);
228 	value = i2c_smbus_read_byte_data(tps->client, TPS_MASK2);
229 	dbg_regstat(buf, sizeof buf, value);
230 	seq_printf(s, "mask2     %s\n", buf);
231 	/* ignore ackint2 */
232 
233 	queue_delayed_work(system_power_efficient_wq, &tps->work,
234 			   POWER_POLL_DELAY);
235 
236 	/* VMAIN voltage, enable lowpower, etc */
237 	value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC1);
238 	seq_printf(s, "vdcdc1    %02x\n", value);
239 
240 	/* VCORE voltage, vibrator on/off */
241 	value = i2c_smbus_read_byte_data(tps->client, TPS_VDCDC2);
242 	seq_printf(s, "vdcdc2    %02x\n", value);
243 
244 	/* both LD0s, and their lowpower behavior */
245 	value = i2c_smbus_read_byte_data(tps->client, TPS_VREGS1);
246 	seq_printf(s, "vregs1    %02x\n\n", value);
247 
248 
249 	/* LEDs and GPIOs */
250 	value = i2c_smbus_read_byte_data(tps->client, TPS_LED1_ON);
251 	v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED1_PER);
252 	seq_printf(s, "led1 %s, on=%02x, per=%02x, %d/%d msec\n",
253 		(value & 0x80)
254 			? str_on_off(v2 & 0x80)
255 			: ((v2 & 0x80) ? "blink" : "(nPG)"),
256 		value, v2,
257 		(value & 0x7f) * 10, (v2 & 0x7f) * 100);
258 
259 	value = i2c_smbus_read_byte_data(tps->client, TPS_LED2_ON);
260 	v2 = i2c_smbus_read_byte_data(tps->client, TPS_LED2_PER);
261 	seq_printf(s, "led2 %s, on=%02x, per=%02x, %d/%d msec\n",
262 		(value & 0x80)
263 			? str_on_off(v2 & 0x80)
264 			: ((v2 & 0x80) ? "blink" : "off"),
265 		value, v2,
266 		(value & 0x7f) * 10, (v2 & 0x7f) * 100);
267 
268 	value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
269 	v2 = i2c_smbus_read_byte_data(tps->client, TPS_MASK3);
270 	seq_printf(s, "defgpio %02x mask3 %02x\n", value, v2);
271 
272 	for (i = 0; i < 4; i++) {
273 		if (value & (1 << (4 + i)))
274 			seq_printf(s, "  gpio%d-out %s\n", i + 1,
275 				(value & (1 << i)) ? "low" : "hi ");
276 		else
277 			seq_printf(s, "  gpio%d-in  %s %s %s\n", i + 1,
278 				(value & (1 << i)) ? "hi " : "low",
279 				(v2 & (1 << i)) ? "no-irq" : "irq",
280 				(v2 & (1 << (4 + i))) ? "rising" : "falling");
281 	}
282 
283 	mutex_unlock(&tps->lock);
284 	return 0;
285 }
286 
dbg_tps_open(struct inode * inode,struct file * file)287 static int dbg_tps_open(struct inode *inode, struct file *file)
288 {
289 	return single_open(file, dbg_show, inode->i_private);
290 }
291 
292 static const struct file_operations debug_fops = {
293 	.open		= dbg_tps_open,
294 	.read		= seq_read,
295 	.llseek		= seq_lseek,
296 	.release	= single_release,
297 };
298 
299 #define	DEBUG_FOPS	&debug_fops
300 
301 #else
302 #define	DEBUG_FOPS	NULL
303 #endif
304 
305 /*-------------------------------------------------------------------------*/
306 
307 /* handle IRQS in a task context, so we can use I2C calls */
tps65010_interrupt(struct tps65010 * tps)308 static void tps65010_interrupt(struct tps65010 *tps)
309 {
310 	u8 tmp = 0, mask, poll;
311 
312 	/* IRQs won't trigger for certain events, but we can get
313 	 * others by polling (normally, with external power applied).
314 	 */
315 	poll = 0;
316 
317 	/* regstatus irqs */
318 	if (tps->nmask2) {
319 		tmp = i2c_smbus_read_byte_data(tps->client, TPS_REGSTATUS);
320 		mask = tmp ^ tps->regstatus;
321 		tps->regstatus = tmp;
322 		mask &= tps->nmask2;
323 	} else
324 		mask = 0;
325 	if (mask) {
326 		tps->regstatus =  tmp;
327 		/* may need to shut something down ... */
328 
329 		/* "off" usually means deep sleep */
330 		if (tmp & TPS_REG_ONOFF) {
331 			pr_info("%s: power off button\n", DRIVER_NAME);
332 #if 0
333 			/* REVISIT:  this might need its own workqueue
334 			 * plus tweaks including deadlock avoidance ...
335 			 * also needs to get error handling and probably
336 			 * an #ifdef CONFIG_HIBERNATION
337 			 */
338 			hibernate();
339 #endif
340 			poll = 1;
341 		}
342 	}
343 
344 	/* chgstatus irqs */
345 	if (tps->nmask1) {
346 		tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGSTATUS);
347 		mask = tmp ^ tps->chgstatus;
348 		tps->chgstatus = tmp;
349 		mask &= tps->nmask1;
350 	} else
351 		mask = 0;
352 	if (mask) {
353 		unsigned	charging = 0;
354 
355 		show_chgstatus("chg/irq", tmp);
356 		if (tmp & (TPS_CHG_USB|TPS_CHG_AC))
357 			show_chgconfig(tps->por, "conf", tps->chgconf);
358 
359 		/* Unless it was turned off or disabled, we charge any
360 		 * battery whenever there's power available for it
361 		 * and the charger hasn't been disabled.
362 		 */
363 		if (!(tps->chgstatus & ~(TPS_CHG_USB|TPS_CHG_AC))
364 				&& (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
365 				&& (tps->chgconf & TPS_CHARGE_ENABLE)
366 				) {
367 			if (tps->chgstatus & TPS_CHG_USB) {
368 				/* VBUS options are readonly until reconnect */
369 				if (mask & TPS_CHG_USB)
370 					set_bit(FLAG_VBUS_CHANGED, &tps->flags);
371 				charging = 1;
372 			} else if (tps->chgstatus & TPS_CHG_AC)
373 				charging = 1;
374 		}
375 		if (charging != tps->charging) {
376 			tps->charging = charging;
377 			pr_info("%s: battery %scharging\n",
378 				DRIVER_NAME, charging ? "" :
379 				((tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC))
380 					? "NOT " : "dis"));
381 		}
382 	}
383 
384 	/* always poll to detect (a) power removal, without tps65013
385 	 * NO_CHG IRQ; or (b) restart of charging after stop.
386 	 */
387 	if ((tps->model != TPS65013 || !tps->charging)
388 			&& (tps->chgstatus & (TPS_CHG_USB|TPS_CHG_AC)))
389 		poll = 1;
390 	if (poll)
391 		queue_delayed_work(system_power_efficient_wq, &tps->work,
392 				   POWER_POLL_DELAY);
393 
394 	/* also potentially gpio-in rise or fall */
395 }
396 
397 /* handle IRQs and polling using keventd for now */
tps65010_work(struct work_struct * work)398 static void tps65010_work(struct work_struct *work)
399 {
400 	struct tps65010		*tps;
401 
402 	tps = container_of(to_delayed_work(work), struct tps65010, work);
403 	mutex_lock(&tps->lock);
404 
405 	tps65010_interrupt(tps);
406 
407 	if (test_and_clear_bit(FLAG_VBUS_CHANGED, &tps->flags)) {
408 		u8	chgconfig, tmp;
409 
410 		chgconfig = i2c_smbus_read_byte_data(tps->client,
411 					TPS_CHGCONFIG);
412 		chgconfig &= ~(TPS_VBUS_500MA | TPS_VBUS_CHARGING);
413 		if (tps->vbus == 500)
414 			chgconfig |= TPS_VBUS_500MA | TPS_VBUS_CHARGING;
415 		else if (tps->vbus >= 100)
416 			chgconfig |= TPS_VBUS_CHARGING;
417 
418 		i2c_smbus_write_byte_data(tps->client,
419 					  TPS_CHGCONFIG, chgconfig);
420 
421 		/* vbus update fails unless VBUS is connected! */
422 		tmp = i2c_smbus_read_byte_data(tps->client, TPS_CHGCONFIG);
423 		tps->chgconf = tmp;
424 		show_chgconfig(tps->por, "update vbus", tmp);
425 	}
426 
427 	if (test_and_clear_bit(FLAG_IRQ_ENABLE, &tps->flags))
428 		enable_irq(tps->client->irq);
429 
430 	mutex_unlock(&tps->lock);
431 }
432 
tps65010_irq(int irq,void * _tps)433 static irqreturn_t tps65010_irq(int irq, void *_tps)
434 {
435 	struct tps65010		*tps = _tps;
436 
437 	disable_irq_nosync(irq);
438 	set_bit(FLAG_IRQ_ENABLE, &tps->flags);
439 	queue_delayed_work(system_power_efficient_wq, &tps->work, 0);
440 	return IRQ_HANDLED;
441 }
442 
443 /*-------------------------------------------------------------------------*/
444 
445 /* offsets 0..3 == GPIO1..GPIO4
446  * offsets 4..5 == LED1/nPG, LED2 (we set one of the non-BLINK modes)
447  * offset 6 == vibrator motor driver
448  */
449 static int
tps65010_gpio_set(struct gpio_chip * chip,unsigned offset,int value)450 tps65010_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
451 {
452 	if (offset < 4)
453 		tps65010_set_gpio_out_value(offset + 1, value);
454 	else if (offset < 6)
455 		tps65010_set_led(offset - 3, value ? ON : OFF);
456 	else
457 		tps65010_set_vib(value);
458 
459 	return 0;
460 }
461 
462 static int
tps65010_output(struct gpio_chip * chip,unsigned offset,int value)463 tps65010_output(struct gpio_chip *chip, unsigned offset, int value)
464 {
465 	/* GPIOs may be input-only */
466 	if (offset < 4) {
467 		struct tps65010		*tps;
468 
469 		tps = gpiochip_get_data(chip);
470 		if (!(tps->outmask & (1 << offset)))
471 			return -EINVAL;
472 		tps65010_set_gpio_out_value(offset + 1, value);
473 	} else if (offset < 6)
474 		tps65010_set_led(offset - 3, value ? ON : OFF);
475 	else
476 		tps65010_set_vib(value);
477 
478 	return 0;
479 }
480 
tps65010_gpio_get(struct gpio_chip * chip,unsigned offset)481 static int tps65010_gpio_get(struct gpio_chip *chip, unsigned offset)
482 {
483 	int			value;
484 	struct tps65010		*tps;
485 
486 	tps = gpiochip_get_data(chip);
487 
488 	if (offset < 4) {
489 		value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
490 		if (value < 0)
491 			return value;
492 		if (value & (1 << (offset + 4)))	/* output */
493 			return !(value & (1 << offset));
494 		else					/* input */
495 			return !!(value & (1 << offset));
496 	}
497 
498 	/* REVISIT we *could* report LED1/nPG and LED2 state ... */
499 	return 0;
500 }
501 
502 
503 /*-------------------------------------------------------------------------*/
504 
505 static struct tps65010 *the_tps;
506 
tps65010_remove(struct i2c_client * client)507 static void tps65010_remove(struct i2c_client *client)
508 {
509 	struct tps65010		*tps = i2c_get_clientdata(client);
510 	struct tps65010_board	*board = dev_get_platdata(&client->dev);
511 
512 	if (board && board->teardown)
513 		board->teardown(client, &tps->chip);
514 	if (client->irq > 0)
515 		free_irq(client->irq, tps);
516 	cancel_delayed_work_sync(&tps->work);
517 	the_tps = NULL;
518 }
519 
tps65010_probe(struct i2c_client * client)520 static int tps65010_probe(struct i2c_client *client)
521 {
522 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
523 	struct tps65010		*tps;
524 	int			status;
525 	struct tps65010_board	*board = dev_get_platdata(&client->dev);
526 
527 	if (the_tps) {
528 		dev_dbg(&client->dev, "only one tps6501x chip allowed\n");
529 		return -ENODEV;
530 	}
531 
532 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
533 		return -EINVAL;
534 
535 	tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
536 	if (!tps)
537 		return -ENOMEM;
538 
539 	mutex_init(&tps->lock);
540 	INIT_DELAYED_WORK(&tps->work, tps65010_work);
541 	tps->client = client;
542 	tps->model = id->driver_data;
543 
544 	/* the IRQ is active low, but many gpio lines can't support that
545 	 * so this driver uses falling-edge triggers instead.
546 	 */
547 	if (client->irq > 0) {
548 		status = request_irq(client->irq, tps65010_irq,
549 				     IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
550 				     DRIVER_NAME, tps);
551 		if (status < 0) {
552 			dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
553 					client->irq, status);
554 			return status;
555 		}
556 		set_bit(FLAG_IRQ_ENABLE, &tps->flags);
557 	} else
558 		dev_warn(&client->dev, "IRQ not configured!\n");
559 
560 
561 	switch (tps->model) {
562 	case TPS65010:
563 	case TPS65012:
564 		tps->por = 1;
565 		break;
566 	/* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
567 	}
568 	tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG);
569 	show_chgconfig(tps->por, "conf/init", tps->chgconf);
570 
571 	show_chgstatus("chg/init",
572 		i2c_smbus_read_byte_data(client, TPS_CHGSTATUS));
573 	show_regstatus("reg/init",
574 		i2c_smbus_read_byte_data(client, TPS_REGSTATUS));
575 
576 	pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
577 		i2c_smbus_read_byte_data(client, TPS_VDCDC1),
578 		i2c_smbus_read_byte_data(client, TPS_VDCDC2),
579 		i2c_smbus_read_byte_data(client, TPS_VREGS1));
580 	pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
581 		i2c_smbus_read_byte_data(client, TPS_DEFGPIO),
582 		i2c_smbus_read_byte_data(client, TPS_MASK3));
583 
584 	i2c_set_clientdata(client, tps);
585 	the_tps = tps;
586 
587 #if	defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
588 	/* USB hosts can't draw VBUS.  OTG devices could, later
589 	 * when OTG infrastructure enables it.  USB peripherals
590 	 * could be relying on VBUS while booting, though.
591 	 */
592 	tps->vbus = 100;
593 #endif
594 
595 	/* unmask the "interesting" irqs, then poll once to
596 	 * kickstart monitoring, initialize shadowed status
597 	 * registers, and maybe disable VBUS draw.
598 	 */
599 	tps->nmask1 = ~0;
600 	(void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1);
601 
602 	tps->nmask2 = TPS_REG_ONOFF;
603 	if (tps->model == TPS65013)
604 		tps->nmask2 |= TPS_REG_NO_CHG;
605 	(void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2);
606 
607 	(void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f
608 		| i2c_smbus_read_byte_data(client, TPS_MASK3));
609 
610 	tps65010_work(&tps->work.work);
611 
612 	tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, client->debugfs,
613 				tps, DEBUG_FOPS);
614 
615 	/* optionally register GPIOs */
616 	if (board) {
617 		tps->outmask = board->outmask;
618 
619 		tps->chip.label = client->name;
620 		tps->chip.parent = &client->dev;
621 		tps->chip.owner = THIS_MODULE;
622 
623 		tps->chip.set = tps65010_gpio_set;
624 		tps->chip.direction_output = tps65010_output;
625 
626 		/* NOTE:  only partial support for inputs; nyet IRQs */
627 		tps->chip.get = tps65010_gpio_get;
628 
629 		tps->chip.base = -1;
630 		tps->chip.ngpio = 7;
631 		tps->chip.can_sleep = 1;
632 
633 		status = gpiochip_add_data(&tps->chip, tps);
634 		if (status < 0)
635 			dev_err(&client->dev, "can't add gpiochip, err %d\n",
636 					status);
637 		else if (board->setup) {
638 			status = board->setup(client, &tps->chip);
639 			if (status < 0) {
640 				dev_dbg(&client->dev,
641 					"board %s %s err %d\n",
642 					"setup", client->name, status);
643 				status = 0;
644 			}
645 		}
646 	}
647 
648 	return 0;
649 }
650 
651 static const struct i2c_device_id tps65010_id[] = {
652 	{ "tps65010", TPS65010 },
653 	{ "tps65011", TPS65011 },
654 	{ "tps65012", TPS65012 },
655 	{ "tps65013", TPS65013 },
656 	{ "tps65014", TPS65011 },	/* tps65011 charging at 6.5V max */
657 	{ }
658 };
659 MODULE_DEVICE_TABLE(i2c, tps65010_id);
660 
661 static struct i2c_driver tps65010_driver = {
662 	.driver = {
663 		.name	= "tps65010",
664 	},
665 	.probe = tps65010_probe,
666 	.remove	= tps65010_remove,
667 	.id_table = tps65010_id,
668 };
669 
670 /*-------------------------------------------------------------------------*/
671 
672 /* Draw from VBUS:
673  *   0 mA -- DON'T DRAW (might supply power instead)
674  * 100 mA -- usb unit load (slowest charge rate)
675  * 500 mA -- usb high power (fast battery charge)
676  */
tps65010_set_vbus_draw(unsigned mA)677 int tps65010_set_vbus_draw(unsigned mA)
678 {
679 	unsigned long	flags;
680 
681 	if (!the_tps)
682 		return -ENODEV;
683 
684 	/* assumes non-SMP */
685 	local_irq_save(flags);
686 	if (mA >= 500)
687 		mA = 500;
688 	else if (mA >= 100)
689 		mA = 100;
690 	else
691 		mA = 0;
692 	the_tps->vbus = mA;
693 	if ((the_tps->chgstatus & TPS_CHG_USB)
694 			&& test_and_set_bit(
695 				FLAG_VBUS_CHANGED, &the_tps->flags)) {
696 		/* gadget drivers call this in_irq() */
697 		queue_delayed_work(system_power_efficient_wq, &the_tps->work,
698 				   0);
699 	}
700 	local_irq_restore(flags);
701 
702 	return 0;
703 }
704 EXPORT_SYMBOL(tps65010_set_vbus_draw);
705 
706 /*-------------------------------------------------------------------------*/
707 /* tps65010_set_gpio_out_value parameter:
708  * gpio:  GPIO1, GPIO2, GPIO3 or GPIO4
709  * value: LOW or HIGH
710  */
tps65010_set_gpio_out_value(unsigned gpio,unsigned value)711 int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
712 {
713 	int	 status;
714 	unsigned defgpio;
715 
716 	if (!the_tps)
717 		return -ENODEV;
718 	if ((gpio < GPIO1) || (gpio > GPIO4))
719 		return -EINVAL;
720 
721 	mutex_lock(&the_tps->lock);
722 
723 	defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO);
724 
725 	/* Configure GPIO for output */
726 	defgpio |= 1 << (gpio + 3);
727 
728 	/* Writing 1 forces a logic 0 on that GPIO and vice versa */
729 	switch (value) {
730 	case LOW:
731 		defgpio |= 1 << (gpio - 1);    /* set GPIO low by writing 1 */
732 		break;
733 	/* case HIGH: */
734 	default:
735 		defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
736 		break;
737 	}
738 
739 	status = i2c_smbus_write_byte_data(the_tps->client,
740 		TPS_DEFGPIO, defgpio);
741 
742 	pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
743 		gpio, str_high_low(value),
744 		i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO));
745 
746 	mutex_unlock(&the_tps->lock);
747 	return status;
748 }
749 EXPORT_SYMBOL(tps65010_set_gpio_out_value);
750 
751 /*-------------------------------------------------------------------------*/
752 /* tps65010_set_led parameter:
753  * led:  LED1 or LED2
754  * mode: ON, OFF or BLINK
755  */
tps65010_set_led(unsigned led,unsigned mode)756 int tps65010_set_led(unsigned led, unsigned mode)
757 {
758 	int	 status;
759 	unsigned led_on, led_per, offs;
760 
761 	if (!the_tps)
762 		return -ENODEV;
763 
764 	if (led == LED1)
765 		offs = 0;
766 	else {
767 		offs = 2;
768 		led = LED2;
769 	}
770 
771 	mutex_lock(&the_tps->lock);
772 
773 	pr_debug("%s: led%i_on   0x%02x\n", DRIVER_NAME, led,
774 		i2c_smbus_read_byte_data(the_tps->client,
775 				TPS_LED1_ON + offs));
776 
777 	pr_debug("%s: led%i_per  0x%02x\n", DRIVER_NAME, led,
778 		i2c_smbus_read_byte_data(the_tps->client,
779 				TPS_LED1_PER + offs));
780 
781 	switch (mode) {
782 	case OFF:
783 		led_on  = 1 << 7;
784 		led_per = 0 << 7;
785 		break;
786 	case ON:
787 		led_on  = 1 << 7;
788 		led_per = 1 << 7;
789 		break;
790 	case BLINK:
791 		led_on  = 0x30 | (0 << 7);
792 		led_per = 0x08 | (1 << 7);
793 		break;
794 	default:
795 		printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
796 		       DRIVER_NAME);
797 		mutex_unlock(&the_tps->lock);
798 		return -EINVAL;
799 	}
800 
801 	status = i2c_smbus_write_byte_data(the_tps->client,
802 			TPS_LED1_ON + offs, led_on);
803 
804 	if (status != 0) {
805 		printk(KERN_ERR "%s: Failed to write led%i_on register\n",
806 		       DRIVER_NAME, led);
807 		mutex_unlock(&the_tps->lock);
808 		return status;
809 	}
810 
811 	pr_debug("%s: led%i_on   0x%02x\n", DRIVER_NAME, led,
812 		i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs));
813 
814 	status = i2c_smbus_write_byte_data(the_tps->client,
815 			TPS_LED1_PER + offs, led_per);
816 
817 	if (status != 0) {
818 		printk(KERN_ERR "%s: Failed to write led%i_per register\n",
819 		       DRIVER_NAME, led);
820 		mutex_unlock(&the_tps->lock);
821 		return status;
822 	}
823 
824 	pr_debug("%s: led%i_per  0x%02x\n", DRIVER_NAME, led,
825 		i2c_smbus_read_byte_data(the_tps->client,
826 				TPS_LED1_PER + offs));
827 
828 	mutex_unlock(&the_tps->lock);
829 
830 	return status;
831 }
832 EXPORT_SYMBOL(tps65010_set_led);
833 
834 /*-------------------------------------------------------------------------*/
835 /* tps65010_set_vib parameter:
836  * value: ON or OFF
837  */
tps65010_set_vib(unsigned value)838 int tps65010_set_vib(unsigned value)
839 {
840 	int	 status;
841 	unsigned vdcdc2;
842 
843 	if (!the_tps)
844 		return -ENODEV;
845 
846 	mutex_lock(&the_tps->lock);
847 
848 	vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2);
849 	vdcdc2 &= ~(1 << 1);
850 	if (value)
851 		vdcdc2 |= (1 << 1);
852 	status = i2c_smbus_write_byte_data(the_tps->client,
853 		TPS_VDCDC2, vdcdc2);
854 
855 	pr_debug("%s: vibrator %s\n", DRIVER_NAME, str_on_off(value));
856 
857 	mutex_unlock(&the_tps->lock);
858 	return status;
859 }
860 EXPORT_SYMBOL(tps65010_set_vib);
861 
862 /*-------------------------------------------------------------------------*/
863 /* tps65010_set_low_pwr parameter:
864  * mode: ON or OFF
865  */
tps65010_set_low_pwr(unsigned mode)866 int tps65010_set_low_pwr(unsigned mode)
867 {
868 	int	 status;
869 	unsigned vdcdc1;
870 
871 	if (!the_tps)
872 		return -ENODEV;
873 
874 	mutex_lock(&the_tps->lock);
875 
876 	pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
877 		str_enable_disable(mode),
878 		i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
879 
880 	vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
881 
882 	switch (mode) {
883 	case OFF:
884 		vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
885 		break;
886 	/* case ON: */
887 	default:
888 		vdcdc1 |= TPS_ENABLE_LP;  /* enable ENABLE_LP bit */
889 		break;
890 	}
891 
892 	status = i2c_smbus_write_byte_data(the_tps->client,
893 			TPS_VDCDC1, vdcdc1);
894 
895 	if (status != 0)
896 		printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
897 			DRIVER_NAME);
898 	else
899 		pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
900 			i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
901 
902 	mutex_unlock(&the_tps->lock);
903 
904 	return status;
905 }
906 EXPORT_SYMBOL(tps65010_set_low_pwr);
907 
908 /*-------------------------------------------------------------------------*/
909 /* tps65010_config_vregs1 parameter:
910  * value to be written to VREGS1 register
911  * Note: The complete register is written, set all bits you need
912  */
tps65010_config_vregs1(unsigned value)913 int tps65010_config_vregs1(unsigned value)
914 {
915 	int	 status;
916 
917 	if (!the_tps)
918 		return -ENODEV;
919 
920 	mutex_lock(&the_tps->lock);
921 
922 	pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
923 			i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
924 
925 	status = i2c_smbus_write_byte_data(the_tps->client,
926 			TPS_VREGS1, value);
927 
928 	if (status != 0)
929 		printk(KERN_ERR "%s: Failed to write vregs1 register\n",
930 			DRIVER_NAME);
931 	else
932 		pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
933 			i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
934 
935 	mutex_unlock(&the_tps->lock);
936 
937 	return status;
938 }
939 EXPORT_SYMBOL(tps65010_config_vregs1);
940 
tps65010_config_vdcdc2(unsigned value)941 int tps65010_config_vdcdc2(unsigned value)
942 {
943 	struct i2c_client *c;
944 	int	 status;
945 
946 	if (!the_tps)
947 		return -ENODEV;
948 
949 	c = the_tps->client;
950 	mutex_lock(&the_tps->lock);
951 
952 	pr_debug("%s: vdcdc2 0x%02x\n", DRIVER_NAME,
953 		 i2c_smbus_read_byte_data(c, TPS_VDCDC2));
954 
955 	status = i2c_smbus_write_byte_data(c, TPS_VDCDC2, value);
956 
957 	if (status != 0)
958 		printk(KERN_ERR "%s: Failed to write vdcdc2 register\n",
959 			DRIVER_NAME);
960 	else
961 		pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
962 			 i2c_smbus_read_byte_data(c, TPS_VDCDC2));
963 
964 	mutex_unlock(&the_tps->lock);
965 	return status;
966 }
967 EXPORT_SYMBOL(tps65010_config_vdcdc2);
968 
969 /*-------------------------------------------------------------------------*/
970 /* tps65013_set_low_pwr parameter:
971  * mode: ON or OFF
972  */
973 
974 /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
975 	required if power supply is through a battery */
976 
tps65013_set_low_pwr(unsigned mode)977 int tps65013_set_low_pwr(unsigned mode)
978 {
979 	int	 status;
980 	unsigned vdcdc1, chgconfig;
981 
982 	if (!the_tps || the_tps->por)
983 		return -ENODEV;
984 
985 	mutex_lock(&the_tps->lock);
986 
987 	pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
988 		DRIVER_NAME,
989 		str_enable_disable(mode),
990 		i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG),
991 		i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
992 
993 	chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
994 	vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
995 
996 	switch (mode) {
997 	case OFF:
998 		chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
999 		vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
1000 		break;
1001 	/* case ON: */
1002 	default:
1003 		chgconfig |= TPS65013_AUA;  /* enable AUA bit */
1004 		vdcdc1 |= TPS_ENABLE_LP;  /* enable ENABLE_LP bit */
1005 		break;
1006 	}
1007 
1008 	status = i2c_smbus_write_byte_data(the_tps->client,
1009 			TPS_CHGCONFIG, chgconfig);
1010 	if (status != 0) {
1011 		printk(KERN_ERR "%s: Failed to write chconfig register\n",
1012 	 DRIVER_NAME);
1013 		mutex_unlock(&the_tps->lock);
1014 		return status;
1015 	}
1016 
1017 	chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
1018 	the_tps->chgconf = chgconfig;
1019 	show_chgconfig(0, "chgconf", chgconfig);
1020 
1021 	status = i2c_smbus_write_byte_data(the_tps->client,
1022 			TPS_VDCDC1, vdcdc1);
1023 
1024 	if (status != 0)
1025 		printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
1026 	 DRIVER_NAME);
1027 	else
1028 		pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
1029 			i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
1030 
1031 	mutex_unlock(&the_tps->lock);
1032 
1033 	return status;
1034 }
1035 EXPORT_SYMBOL(tps65013_set_low_pwr);
1036 
1037 /*-------------------------------------------------------------------------*/
1038 
tps_init(void)1039 static int __init tps_init(void)
1040 {
1041 	return i2c_add_driver(&tps65010_driver);
1042 }
1043 /* NOTE:  this MUST be initialized before the other parts of the system
1044  * that rely on it ... but after the i2c bus on which this relies.
1045  * That is, much earlier than on PC-type systems, which don't often use
1046  * I2C as a core system bus.
1047  */
1048 subsys_initcall(tps_init);
1049 
tps_exit(void)1050 static void __exit tps_exit(void)
1051 {
1052 	i2c_del_driver(&tps65010_driver);
1053 }
1054 module_exit(tps_exit);
1055 
1056