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