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