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