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