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