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