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 void
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
460 static int
tps65010_output(struct gpio_chip * chip,unsigned offset,int value)461 tps65010_output(struct gpio_chip *chip, unsigned offset, int value)
462 {
463 /* GPIOs may be input-only */
464 if (offset < 4) {
465 struct tps65010 *tps;
466
467 tps = gpiochip_get_data(chip);
468 if (!(tps->outmask & (1 << offset)))
469 return -EINVAL;
470 tps65010_set_gpio_out_value(offset + 1, value);
471 } else if (offset < 6)
472 tps65010_set_led(offset - 3, value ? ON : OFF);
473 else
474 tps65010_set_vib(value);
475
476 return 0;
477 }
478
tps65010_gpio_get(struct gpio_chip * chip,unsigned offset)479 static int tps65010_gpio_get(struct gpio_chip *chip, unsigned offset)
480 {
481 int value;
482 struct tps65010 *tps;
483
484 tps = gpiochip_get_data(chip);
485
486 if (offset < 4) {
487 value = i2c_smbus_read_byte_data(tps->client, TPS_DEFGPIO);
488 if (value < 0)
489 return value;
490 if (value & (1 << (offset + 4))) /* output */
491 return !(value & (1 << offset));
492 else /* input */
493 return !!(value & (1 << offset));
494 }
495
496 /* REVISIT we *could* report LED1/nPG and LED2 state ... */
497 return 0;
498 }
499
500
501 /*-------------------------------------------------------------------------*/
502
503 static struct tps65010 *the_tps;
504
tps65010_remove(struct i2c_client * client)505 static void tps65010_remove(struct i2c_client *client)
506 {
507 struct tps65010 *tps = i2c_get_clientdata(client);
508 struct tps65010_board *board = dev_get_platdata(&client->dev);
509
510 if (board && board->teardown)
511 board->teardown(client, &tps->chip);
512 if (client->irq > 0)
513 free_irq(client->irq, tps);
514 cancel_delayed_work_sync(&tps->work);
515 debugfs_remove(tps->file);
516 the_tps = NULL;
517 }
518
tps65010_probe(struct i2c_client * client)519 static int tps65010_probe(struct i2c_client *client)
520 {
521 const struct i2c_device_id *id = i2c_client_get_device_id(client);
522 struct tps65010 *tps;
523 int status;
524 struct tps65010_board *board = dev_get_platdata(&client->dev);
525
526 if (the_tps) {
527 dev_dbg(&client->dev, "only one tps6501x chip allowed\n");
528 return -ENODEV;
529 }
530
531 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
532 return -EINVAL;
533
534 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
535 if (!tps)
536 return -ENOMEM;
537
538 mutex_init(&tps->lock);
539 INIT_DELAYED_WORK(&tps->work, tps65010_work);
540 tps->client = client;
541 tps->model = id->driver_data;
542
543 /* the IRQ is active low, but many gpio lines can't support that
544 * so this driver uses falling-edge triggers instead.
545 */
546 if (client->irq > 0) {
547 status = request_irq(client->irq, tps65010_irq,
548 IRQF_TRIGGER_FALLING | IRQF_NO_AUTOEN,
549 DRIVER_NAME, tps);
550 if (status < 0) {
551 dev_dbg(&client->dev, "can't get IRQ %d, err %d\n",
552 client->irq, status);
553 return status;
554 }
555 set_bit(FLAG_IRQ_ENABLE, &tps->flags);
556 } else
557 dev_warn(&client->dev, "IRQ not configured!\n");
558
559
560 switch (tps->model) {
561 case TPS65010:
562 case TPS65012:
563 tps->por = 1;
564 break;
565 /* else CHGCONFIG.POR is replaced by AUA, enabling a WAIT mode */
566 }
567 tps->chgconf = i2c_smbus_read_byte_data(client, TPS_CHGCONFIG);
568 show_chgconfig(tps->por, "conf/init", tps->chgconf);
569
570 show_chgstatus("chg/init",
571 i2c_smbus_read_byte_data(client, TPS_CHGSTATUS));
572 show_regstatus("reg/init",
573 i2c_smbus_read_byte_data(client, TPS_REGSTATUS));
574
575 pr_debug("%s: vdcdc1 0x%02x, vdcdc2 %02x, vregs1 %02x\n", DRIVER_NAME,
576 i2c_smbus_read_byte_data(client, TPS_VDCDC1),
577 i2c_smbus_read_byte_data(client, TPS_VDCDC2),
578 i2c_smbus_read_byte_data(client, TPS_VREGS1));
579 pr_debug("%s: defgpio 0x%02x, mask3 0x%02x\n", DRIVER_NAME,
580 i2c_smbus_read_byte_data(client, TPS_DEFGPIO),
581 i2c_smbus_read_byte_data(client, TPS_MASK3));
582
583 i2c_set_clientdata(client, tps);
584 the_tps = tps;
585
586 #if defined(CONFIG_USB_GADGET) && !defined(CONFIG_USB_OTG)
587 /* USB hosts can't draw VBUS. OTG devices could, later
588 * when OTG infrastructure enables it. USB peripherals
589 * could be relying on VBUS while booting, though.
590 */
591 tps->vbus = 100;
592 #endif
593
594 /* unmask the "interesting" irqs, then poll once to
595 * kickstart monitoring, initialize shadowed status
596 * registers, and maybe disable VBUS draw.
597 */
598 tps->nmask1 = ~0;
599 (void) i2c_smbus_write_byte_data(client, TPS_MASK1, ~tps->nmask1);
600
601 tps->nmask2 = TPS_REG_ONOFF;
602 if (tps->model == TPS65013)
603 tps->nmask2 |= TPS_REG_NO_CHG;
604 (void) i2c_smbus_write_byte_data(client, TPS_MASK2, ~tps->nmask2);
605
606 (void) i2c_smbus_write_byte_data(client, TPS_MASK3, 0x0f
607 | i2c_smbus_read_byte_data(client, TPS_MASK3));
608
609 tps65010_work(&tps->work.work);
610
611 tps->file = debugfs_create_file(DRIVER_NAME, S_IRUGO, NULL,
612 tps, DEBUG_FOPS);
613
614 /* optionally register GPIOs */
615 if (board) {
616 tps->outmask = board->outmask;
617
618 tps->chip.label = client->name;
619 tps->chip.parent = &client->dev;
620 tps->chip.owner = THIS_MODULE;
621
622 tps->chip.set = tps65010_gpio_set;
623 tps->chip.direction_output = tps65010_output;
624
625 /* NOTE: only partial support for inputs; nyet IRQs */
626 tps->chip.get = tps65010_gpio_get;
627
628 tps->chip.base = -1;
629 tps->chip.ngpio = 7;
630 tps->chip.can_sleep = 1;
631
632 status = gpiochip_add_data(&tps->chip, tps);
633 if (status < 0)
634 dev_err(&client->dev, "can't add gpiochip, err %d\n",
635 status);
636 else if (board->setup) {
637 status = board->setup(client, &tps->chip);
638 if (status < 0) {
639 dev_dbg(&client->dev,
640 "board %s %s err %d\n",
641 "setup", client->name, status);
642 status = 0;
643 }
644 }
645 }
646
647 return 0;
648 }
649
650 static const struct i2c_device_id tps65010_id[] = {
651 { "tps65010", TPS65010 },
652 { "tps65011", TPS65011 },
653 { "tps65012", TPS65012 },
654 { "tps65013", TPS65013 },
655 { "tps65014", TPS65011 }, /* tps65011 charging at 6.5V max */
656 { }
657 };
658 MODULE_DEVICE_TABLE(i2c, tps65010_id);
659
660 static struct i2c_driver tps65010_driver = {
661 .driver = {
662 .name = "tps65010",
663 },
664 .probe = tps65010_probe,
665 .remove = tps65010_remove,
666 .id_table = tps65010_id,
667 };
668
669 /*-------------------------------------------------------------------------*/
670
671 /* Draw from VBUS:
672 * 0 mA -- DON'T DRAW (might supply power instead)
673 * 100 mA -- usb unit load (slowest charge rate)
674 * 500 mA -- usb high power (fast battery charge)
675 */
tps65010_set_vbus_draw(unsigned mA)676 int tps65010_set_vbus_draw(unsigned mA)
677 {
678 unsigned long flags;
679
680 if (!the_tps)
681 return -ENODEV;
682
683 /* assumes non-SMP */
684 local_irq_save(flags);
685 if (mA >= 500)
686 mA = 500;
687 else if (mA >= 100)
688 mA = 100;
689 else
690 mA = 0;
691 the_tps->vbus = mA;
692 if ((the_tps->chgstatus & TPS_CHG_USB)
693 && test_and_set_bit(
694 FLAG_VBUS_CHANGED, &the_tps->flags)) {
695 /* gadget drivers call this in_irq() */
696 queue_delayed_work(system_power_efficient_wq, &the_tps->work,
697 0);
698 }
699 local_irq_restore(flags);
700
701 return 0;
702 }
703 EXPORT_SYMBOL(tps65010_set_vbus_draw);
704
705 /*-------------------------------------------------------------------------*/
706 /* tps65010_set_gpio_out_value parameter:
707 * gpio: GPIO1, GPIO2, GPIO3 or GPIO4
708 * value: LOW or HIGH
709 */
tps65010_set_gpio_out_value(unsigned gpio,unsigned value)710 int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
711 {
712 int status;
713 unsigned defgpio;
714
715 if (!the_tps)
716 return -ENODEV;
717 if ((gpio < GPIO1) || (gpio > GPIO4))
718 return -EINVAL;
719
720 mutex_lock(&the_tps->lock);
721
722 defgpio = i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO);
723
724 /* Configure GPIO for output */
725 defgpio |= 1 << (gpio + 3);
726
727 /* Writing 1 forces a logic 0 on that GPIO and vice versa */
728 switch (value) {
729 case LOW:
730 defgpio |= 1 << (gpio - 1); /* set GPIO low by writing 1 */
731 break;
732 /* case HIGH: */
733 default:
734 defgpio &= ~(1 << (gpio - 1)); /* set GPIO high by writing 0 */
735 break;
736 }
737
738 status = i2c_smbus_write_byte_data(the_tps->client,
739 TPS_DEFGPIO, defgpio);
740
741 pr_debug("%s: gpio%dout = %s, defgpio 0x%02x\n", DRIVER_NAME,
742 gpio, str_high_low(value),
743 i2c_smbus_read_byte_data(the_tps->client, TPS_DEFGPIO));
744
745 mutex_unlock(&the_tps->lock);
746 return status;
747 }
748 EXPORT_SYMBOL(tps65010_set_gpio_out_value);
749
750 /*-------------------------------------------------------------------------*/
751 /* tps65010_set_led parameter:
752 * led: LED1 or LED2
753 * mode: ON, OFF or BLINK
754 */
tps65010_set_led(unsigned led,unsigned mode)755 int tps65010_set_led(unsigned led, unsigned mode)
756 {
757 int status;
758 unsigned led_on, led_per, offs;
759
760 if (!the_tps)
761 return -ENODEV;
762
763 if (led == LED1)
764 offs = 0;
765 else {
766 offs = 2;
767 led = LED2;
768 }
769
770 mutex_lock(&the_tps->lock);
771
772 pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
773 i2c_smbus_read_byte_data(the_tps->client,
774 TPS_LED1_ON + offs));
775
776 pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
777 i2c_smbus_read_byte_data(the_tps->client,
778 TPS_LED1_PER + offs));
779
780 switch (mode) {
781 case OFF:
782 led_on = 1 << 7;
783 led_per = 0 << 7;
784 break;
785 case ON:
786 led_on = 1 << 7;
787 led_per = 1 << 7;
788 break;
789 case BLINK:
790 led_on = 0x30 | (0 << 7);
791 led_per = 0x08 | (1 << 7);
792 break;
793 default:
794 printk(KERN_ERR "%s: Wrong mode parameter for set_led()\n",
795 DRIVER_NAME);
796 mutex_unlock(&the_tps->lock);
797 return -EINVAL;
798 }
799
800 status = i2c_smbus_write_byte_data(the_tps->client,
801 TPS_LED1_ON + offs, led_on);
802
803 if (status != 0) {
804 printk(KERN_ERR "%s: Failed to write led%i_on register\n",
805 DRIVER_NAME, led);
806 mutex_unlock(&the_tps->lock);
807 return status;
808 }
809
810 pr_debug("%s: led%i_on 0x%02x\n", DRIVER_NAME, led,
811 i2c_smbus_read_byte_data(the_tps->client, TPS_LED1_ON + offs));
812
813 status = i2c_smbus_write_byte_data(the_tps->client,
814 TPS_LED1_PER + offs, led_per);
815
816 if (status != 0) {
817 printk(KERN_ERR "%s: Failed to write led%i_per register\n",
818 DRIVER_NAME, led);
819 mutex_unlock(&the_tps->lock);
820 return status;
821 }
822
823 pr_debug("%s: led%i_per 0x%02x\n", DRIVER_NAME, led,
824 i2c_smbus_read_byte_data(the_tps->client,
825 TPS_LED1_PER + offs));
826
827 mutex_unlock(&the_tps->lock);
828
829 return status;
830 }
831 EXPORT_SYMBOL(tps65010_set_led);
832
833 /*-------------------------------------------------------------------------*/
834 /* tps65010_set_vib parameter:
835 * value: ON or OFF
836 */
tps65010_set_vib(unsigned value)837 int tps65010_set_vib(unsigned value)
838 {
839 int status;
840 unsigned vdcdc2;
841
842 if (!the_tps)
843 return -ENODEV;
844
845 mutex_lock(&the_tps->lock);
846
847 vdcdc2 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC2);
848 vdcdc2 &= ~(1 << 1);
849 if (value)
850 vdcdc2 |= (1 << 1);
851 status = i2c_smbus_write_byte_data(the_tps->client,
852 TPS_VDCDC2, vdcdc2);
853
854 pr_debug("%s: vibrator %s\n", DRIVER_NAME, str_on_off(value));
855
856 mutex_unlock(&the_tps->lock);
857 return status;
858 }
859 EXPORT_SYMBOL(tps65010_set_vib);
860
861 /*-------------------------------------------------------------------------*/
862 /* tps65010_set_low_pwr parameter:
863 * mode: ON or OFF
864 */
tps65010_set_low_pwr(unsigned mode)865 int tps65010_set_low_pwr(unsigned mode)
866 {
867 int status;
868 unsigned vdcdc1;
869
870 if (!the_tps)
871 return -ENODEV;
872
873 mutex_lock(&the_tps->lock);
874
875 pr_debug("%s: %s low_pwr, vdcdc1 0x%02x\n", DRIVER_NAME,
876 str_enable_disable(mode),
877 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
878
879 vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
880
881 switch (mode) {
882 case OFF:
883 vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
884 break;
885 /* case ON: */
886 default:
887 vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
888 break;
889 }
890
891 status = i2c_smbus_write_byte_data(the_tps->client,
892 TPS_VDCDC1, vdcdc1);
893
894 if (status != 0)
895 printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
896 DRIVER_NAME);
897 else
898 pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
899 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
900
901 mutex_unlock(&the_tps->lock);
902
903 return status;
904 }
905 EXPORT_SYMBOL(tps65010_set_low_pwr);
906
907 /*-------------------------------------------------------------------------*/
908 /* tps65010_config_vregs1 parameter:
909 * value to be written to VREGS1 register
910 * Note: The complete register is written, set all bits you need
911 */
tps65010_config_vregs1(unsigned value)912 int tps65010_config_vregs1(unsigned value)
913 {
914 int status;
915
916 if (!the_tps)
917 return -ENODEV;
918
919 mutex_lock(&the_tps->lock);
920
921 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
922 i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
923
924 status = i2c_smbus_write_byte_data(the_tps->client,
925 TPS_VREGS1, value);
926
927 if (status != 0)
928 printk(KERN_ERR "%s: Failed to write vregs1 register\n",
929 DRIVER_NAME);
930 else
931 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
932 i2c_smbus_read_byte_data(the_tps->client, TPS_VREGS1));
933
934 mutex_unlock(&the_tps->lock);
935
936 return status;
937 }
938 EXPORT_SYMBOL(tps65010_config_vregs1);
939
tps65010_config_vdcdc2(unsigned value)940 int tps65010_config_vdcdc2(unsigned value)
941 {
942 struct i2c_client *c;
943 int status;
944
945 if (!the_tps)
946 return -ENODEV;
947
948 c = the_tps->client;
949 mutex_lock(&the_tps->lock);
950
951 pr_debug("%s: vdcdc2 0x%02x\n", DRIVER_NAME,
952 i2c_smbus_read_byte_data(c, TPS_VDCDC2));
953
954 status = i2c_smbus_write_byte_data(c, TPS_VDCDC2, value);
955
956 if (status != 0)
957 printk(KERN_ERR "%s: Failed to write vdcdc2 register\n",
958 DRIVER_NAME);
959 else
960 pr_debug("%s: vregs1 0x%02x\n", DRIVER_NAME,
961 i2c_smbus_read_byte_data(c, TPS_VDCDC2));
962
963 mutex_unlock(&the_tps->lock);
964 return status;
965 }
966 EXPORT_SYMBOL(tps65010_config_vdcdc2);
967
968 /*-------------------------------------------------------------------------*/
969 /* tps65013_set_low_pwr parameter:
970 * mode: ON or OFF
971 */
972
973 /* FIXME: Assumes AC or USB power is present. Setting AUA bit is not
974 required if power supply is through a battery */
975
tps65013_set_low_pwr(unsigned mode)976 int tps65013_set_low_pwr(unsigned mode)
977 {
978 int status;
979 unsigned vdcdc1, chgconfig;
980
981 if (!the_tps || the_tps->por)
982 return -ENODEV;
983
984 mutex_lock(&the_tps->lock);
985
986 pr_debug("%s: %s low_pwr, chgconfig 0x%02x vdcdc1 0x%02x\n",
987 DRIVER_NAME,
988 str_enable_disable(mode),
989 i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG),
990 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
991
992 chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
993 vdcdc1 = i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1);
994
995 switch (mode) {
996 case OFF:
997 chgconfig &= ~TPS65013_AUA; /* disable AUA bit */
998 vdcdc1 &= ~TPS_ENABLE_LP; /* disable ENABLE_LP bit */
999 break;
1000 /* case ON: */
1001 default:
1002 chgconfig |= TPS65013_AUA; /* enable AUA bit */
1003 vdcdc1 |= TPS_ENABLE_LP; /* enable ENABLE_LP bit */
1004 break;
1005 }
1006
1007 status = i2c_smbus_write_byte_data(the_tps->client,
1008 TPS_CHGCONFIG, chgconfig);
1009 if (status != 0) {
1010 printk(KERN_ERR "%s: Failed to write chconfig register\n",
1011 DRIVER_NAME);
1012 mutex_unlock(&the_tps->lock);
1013 return status;
1014 }
1015
1016 chgconfig = i2c_smbus_read_byte_data(the_tps->client, TPS_CHGCONFIG);
1017 the_tps->chgconf = chgconfig;
1018 show_chgconfig(0, "chgconf", chgconfig);
1019
1020 status = i2c_smbus_write_byte_data(the_tps->client,
1021 TPS_VDCDC1, vdcdc1);
1022
1023 if (status != 0)
1024 printk(KERN_ERR "%s: Failed to write vdcdc1 register\n",
1025 DRIVER_NAME);
1026 else
1027 pr_debug("%s: vdcdc1 0x%02x\n", DRIVER_NAME,
1028 i2c_smbus_read_byte_data(the_tps->client, TPS_VDCDC1));
1029
1030 mutex_unlock(&the_tps->lock);
1031
1032 return status;
1033 }
1034 EXPORT_SYMBOL(tps65013_set_low_pwr);
1035
1036 /*-------------------------------------------------------------------------*/
1037
tps_init(void)1038 static int __init tps_init(void)
1039 {
1040 return i2c_add_driver(&tps65010_driver);
1041 }
1042 /* NOTE: this MUST be initialized before the other parts of the system
1043 * that rely on it ... but after the i2c bus on which this relies.
1044 * That is, much earlier than on PC-type systems, which don't often use
1045 * I2C as a core system bus.
1046 */
1047 subsys_initcall(tps_init);
1048
tps_exit(void)1049 static void __exit tps_exit(void)
1050 {
1051 i2c_del_driver(&tps65010_driver);
1052 }
1053 module_exit(tps_exit);
1054
1055