config: tegra3: enable /dev mount with ACL
[linux-2.6.git] / drivers / mfd / tps6591x.c
1 /*
2  * driver/mfd/tps6591x.c
3  *
4  * Core driver for TI TPS6591x PMIC family
5  *
6  * Copyright (C) 2011 NVIDIA Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  */
23
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/mutex.h>
29 #include <linux/slab.h>
30 #include <linux/gpio.h>
31 #include <linux/i2c.h>
32
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/tps6591x.h>
35
36 /* device control registers */
37 #define TPS6591X_DEVCTRL        0x3F
38 #define DEVCTRL_PWR_OFF_SEQ     (1 << 7)
39 #define DEVCTRL_DEV_ON          (1 << 2)
40 #define DEVCTRL_DEV_SLP         (1 << 1)
41 #define TPS6591X_DEVCTRL2       0x40
42
43 /* device sleep on registers */
44 #define TPS6591X_SLEEP_KEEP_ON  0x42
45 #define SLEEP_KEEP_ON_THERM     (1 << 7)
46 #define SLEEP_KEEP_ON_CLKOUT32K (1 << 6)
47 #define SLEEP_KEEP_ON_VRTC      (1 << 5)
48 #define SLEEP_KEEP_ON_I2CHS     (1 << 4)
49
50 /* interrupt status registers */
51 #define TPS6591X_INT_STS        0x50
52 #define TPS6591X_INT_STS2       0x52
53 #define TPS6591X_INT_STS3       0x54
54
55 /* interrupt mask registers */
56 #define TPS6591X_INT_MSK        0x51
57 #define TPS6591X_INT_MSK2       0x53
58 #define TPS6591X_INT_MSK3       0x55
59
60 /* GPIO register base address */
61 #define TPS6591X_GPIO_BASE_ADDR 0x60
62
63 /* silicon version number */
64 #define TPS6591X_VERNUM         0x80
65
66 #define TPS6591X_GPIO_SLEEP     7
67 #define TPS6591X_GPIO_PDEN      3
68 #define TPS6591X_GPIO_DIR       2
69
70 enum irq_type {
71         EVENT,
72         GPIO,
73 };
74
75 struct tps6591x_irq_data {
76         u8              mask_reg;
77         u8              mask_pos;
78         enum irq_type   type;
79 };
80
81 #define TPS6591X_IRQ(_reg, _mask_pos, _type)    \
82         {                                       \
83                 .mask_reg       = (_reg),       \
84                 .mask_pos       = (_mask_pos),  \
85                 .type           = (_type),      \
86         }
87
88 static const struct tps6591x_irq_data tps6591x_irqs[] = {
89         [TPS6591X_INT_PWRHOLD_F]        = TPS6591X_IRQ(0, 0, EVENT),
90         [TPS6591X_INT_VMBHI]            = TPS6591X_IRQ(0, 1, EVENT),
91         [TPS6591X_INT_PWRON]            = TPS6591X_IRQ(0, 2, EVENT),
92         [TPS6591X_INT_PWRON_LP]         = TPS6591X_IRQ(0, 3, EVENT),
93         [TPS6591X_INT_PWRHOLD_R]        = TPS6591X_IRQ(0, 4, EVENT),
94         [TPS6591X_INT_HOTDIE]           = TPS6591X_IRQ(0, 5, EVENT),
95         [TPS6591X_INT_RTC_ALARM]        = TPS6591X_IRQ(0, 6, EVENT),
96         [TPS6591X_INT_RTC_PERIOD]       = TPS6591X_IRQ(0, 7, EVENT),
97         [TPS6591X_INT_GPIO0]            = TPS6591X_IRQ(1, 0, GPIO),
98         [TPS6591X_INT_GPIO1]            = TPS6591X_IRQ(1, 2, GPIO),
99         [TPS6591X_INT_GPIO2]            = TPS6591X_IRQ(1, 4, GPIO),
100         [TPS6591X_INT_GPIO3]            = TPS6591X_IRQ(1, 6, GPIO),
101         [TPS6591X_INT_GPIO4]            = TPS6591X_IRQ(2, 0, GPIO),
102         [TPS6591X_INT_GPIO5]            = TPS6591X_IRQ(2, 2, GPIO),
103         [TPS6591X_INT_WTCHDG]           = TPS6591X_IRQ(2, 4, EVENT),
104         [TPS6591X_INT_VMBCH2_H]         = TPS6591X_IRQ(2, 5, EVENT),
105         [TPS6591X_INT_VMBCH2_L]         = TPS6591X_IRQ(2, 6, EVENT),
106         [TPS6591X_INT_PWRDN]            = TPS6591X_IRQ(2, 7, EVENT),
107 };
108
109 struct tps6591x {
110         struct mutex            lock;
111         struct device           *dev;
112         struct i2c_client       *client;
113
114         struct gpio_chip        gpio;
115         struct irq_chip         irq_chip;
116         struct mutex            irq_lock;
117         int                     irq_base;
118         int                     irq_main;
119         u32                     irq_en;
120         u8                      mask_cache[3];
121         u8                      mask_reg[3];
122 };
123
124 static inline int __tps6591x_read(struct i2c_client *client,
125                                   int reg, uint8_t *val)
126 {
127         int ret;
128
129         ret = i2c_smbus_read_byte_data(client, reg);
130         if (ret < 0) {
131                 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
132                 return ret;
133         }
134
135         *val = (uint8_t)ret;
136
137         return 0;
138 }
139
140 static inline int __tps6591x_reads(struct i2c_client *client, int reg,
141                                 int len, uint8_t *val)
142 {
143         int ret;
144
145         ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
146         if (ret < 0) {
147                 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
148                 return ret;
149         }
150
151         return 0;
152 }
153
154 static inline int __tps6591x_write(struct i2c_client *client,
155                                  int reg, uint8_t val)
156 {
157         int ret;
158         ret = i2c_smbus_write_byte_data(client, reg, val);
159         if (ret < 0) {
160                 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
161                                 val, reg);
162                 return ret;
163         }
164
165         return 0;
166 }
167
168 static inline int __tps6591x_writes(struct i2c_client *client, int reg,
169                                   int len, uint8_t *val)
170 {
171         int ret;
172
173         ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
174         if (ret < 0) {
175                 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
176                 return ret;
177         }
178
179         return 0;
180 }
181
182 int tps6591x_write(struct device *dev, int reg, uint8_t val)
183 {
184         struct tps6591x *tps6591x = dev_get_drvdata(dev);
185         int ret = 0;
186
187         mutex_lock(&tps6591x->lock);
188         ret = __tps6591x_write(to_i2c_client(dev), reg, val);
189         mutex_unlock(&tps6591x->lock);
190
191         return ret;
192 }
193 EXPORT_SYMBOL_GPL(tps6591x_write);
194
195 int tps6591x_writes(struct device *dev, int reg, int len, uint8_t *val)
196 {
197         struct tps6591x *tps6591x = dev_get_drvdata(dev);
198         int ret = 0;
199
200         mutex_lock(&tps6591x->lock);
201         ret = __tps6591x_writes(to_i2c_client(dev), reg, len, val);
202         mutex_unlock(&tps6591x->lock);
203
204         return ret;
205 }
206 EXPORT_SYMBOL_GPL(tps6591x_writes);
207
208 int tps6591x_read(struct device *dev, int reg, uint8_t *val)
209 {
210         return __tps6591x_read(to_i2c_client(dev), reg, val);
211 }
212 EXPORT_SYMBOL_GPL(tps6591x_read);
213
214 int tps6591x_reads(struct device *dev, int reg, int len, uint8_t *val)
215 {
216         return __tps6591x_reads(to_i2c_client(dev), reg, len, val);
217 }
218 EXPORT_SYMBOL_GPL(tps6591x_reads);
219
220 int tps6591x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
221 {
222         struct tps6591x *tps6591x = dev_get_drvdata(dev);
223         uint8_t reg_val;
224         int ret = 0;
225
226         mutex_lock(&tps6591x->lock);
227
228         ret = __tps6591x_read(to_i2c_client(dev), reg, &reg_val);
229         if (ret)
230                 goto out;
231
232         if ((reg_val & bit_mask) != bit_mask) {
233                 reg_val |= bit_mask;
234                 ret = __tps6591x_write(to_i2c_client(dev), reg, reg_val);
235         }
236 out:
237         mutex_unlock(&tps6591x->lock);
238         return ret;
239 }
240 EXPORT_SYMBOL_GPL(tps6591x_set_bits);
241
242 int tps6591x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
243 {
244         struct tps6591x *tps6591x = dev_get_drvdata(dev);
245         uint8_t reg_val;
246         int ret = 0;
247
248         mutex_lock(&tps6591x->lock);
249
250         ret = __tps6591x_read(to_i2c_client(dev), reg, &reg_val);
251         if (ret)
252                 goto out;
253
254         if (reg_val & bit_mask) {
255                 reg_val &= ~bit_mask;
256                 ret = __tps6591x_write(to_i2c_client(dev), reg, reg_val);
257         }
258 out:
259         mutex_unlock(&tps6591x->lock);
260         return ret;
261 }
262 EXPORT_SYMBOL_GPL(tps6591x_clr_bits);
263
264 int tps6591x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
265 {
266         struct tps6591x *tps6591x = dev_get_drvdata(dev);
267         uint8_t reg_val;
268         int ret = 0;
269
270         mutex_lock(&tps6591x->lock);
271
272         ret = __tps6591x_read(tps6591x->client, reg, &reg_val);
273         if (ret)
274                 goto out;
275
276         if ((reg_val & mask) != val) {
277                 reg_val = (reg_val & ~mask) | (val & mask);
278                 ret = __tps6591x_write(tps6591x->client, reg, reg_val);
279         }
280 out:
281         mutex_unlock(&tps6591x->lock);
282         return ret;
283 }
284 EXPORT_SYMBOL_GPL(tps6591x_update);
285
286 static struct i2c_client *tps6591x_i2c_client;
287 static void tps6591x_power_off(void)
288 {
289         struct device *dev = NULL;
290
291         if (!tps6591x_i2c_client)
292                 return;
293
294         dev = &tps6591x_i2c_client->dev;
295
296         if (tps6591x_set_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_PWR_OFF_SEQ) < 0)
297                 return;
298
299         tps6591x_clr_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_ON);
300 }
301
302 static int tps6591x_gpio_get(struct gpio_chip *gc, unsigned offset)
303 {
304         struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio);
305         uint8_t val;
306         int ret;
307
308         ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR +
309                         offset, &val);
310         if (ret)
311                 return ret;
312
313         if (val & 0x4)
314                 return val & 0x1;
315         else
316                 return (val & 0x2) ? 1 : 0;
317 }
318
319 static void tps6591x_gpio_set(struct gpio_chip *chip, unsigned offset,
320                         int value)
321 {
322
323         struct tps6591x *tps6591x = container_of(chip, struct tps6591x, gpio);
324
325         tps6591x_update(tps6591x->dev, TPS6591X_GPIO_BASE_ADDR + offset,
326                         value, 0x1);
327 }
328
329 static int tps6591x_gpio_input(struct gpio_chip *gc, unsigned offset)
330 {
331         struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio);
332         uint8_t reg_val;
333         int ret;
334
335         ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR +
336                         offset, &reg_val);
337         if (ret)
338                 return ret;
339
340         reg_val &= ~0x4;
341         return __tps6591x_write(tps6591x->client, TPS6591X_GPIO_BASE_ADDR +
342                         offset, reg_val);
343 }
344
345 static int tps6591x_gpio_output(struct gpio_chip *gc, unsigned offset,
346                                 int value)
347 {
348         struct tps6591x *tps6591x = container_of(gc, struct tps6591x, gpio);
349         uint8_t reg_val, val;
350         int ret;
351
352         ret = __tps6591x_read(tps6591x->client, TPS6591X_GPIO_BASE_ADDR +
353                         offset, &reg_val);
354         if (ret)
355                 return ret;
356
357         reg_val &= ~0x1;
358         val = (value & 0x1) | 0x4;
359         reg_val = reg_val | val;
360         return __tps6591x_write(tps6591x->client, TPS6591X_GPIO_BASE_ADDR +
361                         offset, reg_val);
362 }
363
364 static int tps6591x_gpio_to_irq(struct gpio_chip *gc, unsigned off)
365 {
366         struct tps6591x *tps6591x;
367         tps6591x = container_of(gc, struct tps6591x, gpio);
368
369         if ((off >= 0) && (off <= TPS6591X_INT_GPIO5 - TPS6591X_INT_GPIO0))
370                 return tps6591x->irq_base + TPS6591X_INT_GPIO0 + off;
371
372         return -EIO;
373 }
374
375 static void tps6591x_gpio_init(struct tps6591x *tps6591x,
376                         struct tps6591x_platform_data *pdata)
377 {
378         int ret;
379         int gpio_base = pdata->gpio_base;
380         int i;
381         u8 gpio_reg;
382         struct tps6591x_gpio_init_data *ginit;
383
384         if (gpio_base <= 0)
385                 return;
386
387         for (i = 0; i < pdata->num_gpioinit_data; ++i) {
388                 ginit = &pdata->gpio_init_data[i];
389                 if (!ginit->init_apply)
390                         continue;
391                 gpio_reg = (ginit->sleep_en << TPS6591X_GPIO_SLEEP) |
392                                 (ginit->pulldn_en << TPS6591X_GPIO_PDEN) |
393                                 (ginit->output_mode_en << TPS6591X_GPIO_DIR);
394
395                 if (ginit->output_mode_en)
396                         gpio_reg |= ginit->output_val;
397
398                 ret =  __tps6591x_write(tps6591x->client,
399                                 TPS6591X_GPIO_BASE_ADDR + i, gpio_reg);
400                 if (ret < 0)
401                         dev_err(&tps6591x->client->dev, "Gpio %d init "
402                                 "configuration failed: %d\n", i, ret);
403         }
404
405         tps6591x->gpio.owner            = THIS_MODULE;
406         tps6591x->gpio.label            = tps6591x->client->name;
407         tps6591x->gpio.dev              = tps6591x->dev;
408         tps6591x->gpio.base             = gpio_base;
409         tps6591x->gpio.ngpio            = TPS6591X_GPIO_NR;
410         tps6591x->gpio.can_sleep        = 1;
411
412         tps6591x->gpio.direction_input  = tps6591x_gpio_input;
413         tps6591x->gpio.direction_output = tps6591x_gpio_output;
414         tps6591x->gpio.set              = tps6591x_gpio_set;
415         tps6591x->gpio.get              = tps6591x_gpio_get;
416         tps6591x->gpio.to_irq           = tps6591x_gpio_to_irq;
417
418         ret = gpiochip_add(&tps6591x->gpio);
419         if (ret)
420                 dev_warn(tps6591x->dev, "GPIO registration failed: %d\n", ret);
421 }
422
423 static int __remove_subdev(struct device *dev, void *unused)
424 {
425         platform_device_unregister(to_platform_device(dev));
426         return 0;
427 }
428
429 static int tps6591x_remove_subdevs(struct tps6591x *tps6591x)
430 {
431         return device_for_each_child(tps6591x->dev, NULL, __remove_subdev);
432 }
433
434 static void tps6591x_irq_lock(struct irq_data *data)
435 {
436         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(data);
437
438         mutex_lock(&tps6591x->irq_lock);
439 }
440
441 static void tps6591x_irq_mask(struct irq_data *irq_data)
442 {
443         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data);
444         unsigned int __irq = irq_data->irq - tps6591x->irq_base;
445         const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq];
446
447         if (data->type == EVENT)
448                 tps6591x->mask_reg[data->mask_reg] |= (1 << data->mask_pos);
449         else
450                 tps6591x->mask_reg[data->mask_reg] |= (3 << data->mask_pos);
451
452         tps6591x->irq_en &= ~(1 << __irq);
453 }
454
455 static void tps6591x_irq_unmask(struct irq_data *irq_data)
456 {
457         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data);
458
459         unsigned int __irq = irq_data->irq - tps6591x->irq_base;
460         const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq];
461
462         if (data->type == EVENT) {
463                 tps6591x->mask_reg[data->mask_reg] &= ~(1 << data->mask_pos);
464                 tps6591x->irq_en |= (1 << __irq);
465         }
466 }
467
468 static void tps6591x_irq_sync_unlock(struct irq_data *data)
469 {
470         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(data);
471         int i;
472
473         for (i = 0; i < ARRAY_SIZE(tps6591x->mask_reg); i++) {
474                 if (tps6591x->mask_reg[i] != tps6591x->mask_cache[i]) {
475                         if (!WARN_ON(tps6591x_write(tps6591x->dev,
476                                                 TPS6591X_INT_MSK + 2*i,
477                                                 tps6591x->mask_reg[i])))
478                                 tps6591x->mask_cache[i] = tps6591x->mask_reg[i];
479                 }
480         }
481
482         mutex_unlock(&tps6591x->irq_lock);
483 }
484
485 static int tps6591x_irq_set_type(struct irq_data *irq_data, unsigned int type)
486 {
487         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data);
488
489         unsigned int __irq = irq_data->irq - tps6591x->irq_base;
490         const struct tps6591x_irq_data *data = &tps6591x_irqs[__irq];
491
492         if (data->type == GPIO) {
493                 if (type & IRQ_TYPE_EDGE_FALLING)
494                         tps6591x->mask_reg[data->mask_reg]
495                                 &= ~(1 << data->mask_pos);
496                 else
497                         tps6591x->mask_reg[data->mask_reg]
498                                 |= (1 << data->mask_pos);
499
500                 if (type & IRQ_TYPE_EDGE_RISING)
501                         tps6591x->mask_reg[data->mask_reg]
502                                 &= ~(2 << data->mask_pos);
503                 else
504                         tps6591x->mask_reg[data->mask_reg]
505                                 |= (2 << data->mask_pos);
506
507                 tps6591x->irq_en |= (1 << __irq);
508         }
509
510         return 0;
511 }
512
513 #ifdef CONFIG_PM_SLEEP
514 static int tps6591x_irq_set_wake(struct irq_data *irq_data, unsigned int on)
515 {
516         struct tps6591x *tps6591x = irq_data_get_irq_chip_data(irq_data);
517         return irq_set_irq_wake(tps6591x->irq_main, on);
518 }
519 #else
520 #define tps6591x_irq_set_wake NULL
521 #endif
522
523
524 static irqreturn_t tps6591x_irq(int irq, void *data)
525 {
526         struct tps6591x *tps6591x = data;
527         int ret = 0;
528         u8 tmp[3];
529         u8 int_ack;
530         u32 acks, mask = 0;
531         int i;
532
533         for (i = 0; i < 3; i++) {
534                 ret = tps6591x_read(tps6591x->dev, TPS6591X_INT_STS + 2*i,
535                                 &tmp[i]);
536                 if (ret < 0) {
537                         dev_err(tps6591x->dev,
538                                 "failed to read interrupt status\n");
539                         return IRQ_NONE;
540                 }
541                 if (tmp[i]) {
542                         /* Ack only those interrupts which are enabled */
543                         int_ack = tmp[i] & (~(tps6591x->mask_cache[i]));
544                         ret = tps6591x_write(tps6591x->dev,
545                                         TPS6591X_INT_STS + 2*i, int_ack);
546                         if (ret < 0) {
547                                 dev_err(tps6591x->dev,
548                                         "failed to write interrupt status\n");
549                                 return IRQ_NONE;
550                         }
551                 }
552         }
553
554         acks = (tmp[2] << 16) | (tmp[1] << 8) | tmp[0];
555
556         for (i = 0; i < ARRAY_SIZE(tps6591x_irqs); i++) {
557                 if (tps6591x_irqs[i].type == GPIO)
558                         mask = (3 << (tps6591x_irqs[i].mask_pos
559                                         + tps6591x_irqs[i].mask_reg*8));
560                 else if (tps6591x_irqs[i].type == EVENT)
561                         mask = (1 << (tps6591x_irqs[i].mask_pos
562                                         + tps6591x_irqs[i].mask_reg*8));
563
564                 if ((acks & mask) && (tps6591x->irq_en & (1 << i)))
565                         handle_nested_irq(tps6591x->irq_base + i);
566         }
567         return IRQ_HANDLED;
568 }
569
570 static int __devinit tps6591x_irq_init(struct tps6591x *tps6591x, int irq,
571                                 int irq_base)
572 {
573         int i, ret;
574
575         if (!irq_base) {
576                 dev_warn(tps6591x->dev, "No interrupt support on IRQ base\n");
577                 return -EINVAL;
578         }
579
580         mutex_init(&tps6591x->irq_lock);
581
582         tps6591x->mask_reg[0] = 0xFF;
583         tps6591x->mask_reg[1] = 0xFF;
584         tps6591x->mask_reg[2] = 0xFF;
585         for (i = 0; i < 3; i++) {
586                 tps6591x->mask_cache[i] = tps6591x->mask_reg[i];
587                 tps6591x_write(tps6591x->dev, TPS6591X_INT_MSK + 2*i,
588                                  tps6591x->mask_cache[i]);
589         }
590
591         for (i = 0; i < 3; i++)
592                 tps6591x_write(tps6591x->dev, TPS6591X_INT_STS + 2*i, 0xff);
593
594         tps6591x->irq_base = irq_base;
595         tps6591x->irq_main = irq;
596
597         tps6591x->irq_chip.name = "tps6591x";
598         tps6591x->irq_chip.irq_mask = tps6591x_irq_mask;
599         tps6591x->irq_chip.irq_unmask = tps6591x_irq_unmask;
600         tps6591x->irq_chip.irq_bus_lock = tps6591x_irq_lock;
601         tps6591x->irq_chip.irq_bus_sync_unlock = tps6591x_irq_sync_unlock;
602         tps6591x->irq_chip.irq_set_type = tps6591x_irq_set_type;
603         tps6591x->irq_chip.irq_set_wake = tps6591x_irq_set_wake;
604
605         for (i = 0; i < ARRAY_SIZE(tps6591x_irqs); i++) {
606                 int __irq = i + tps6591x->irq_base;
607                 irq_set_chip_data(__irq, tps6591x);
608                 irq_set_chip_and_handler(__irq, &tps6591x->irq_chip,
609                                          handle_simple_irq);
610                 irq_set_nested_thread(__irq, 1);
611 #ifdef CONFIG_ARM
612                 set_irq_flags(__irq, IRQF_VALID);
613 #endif
614         }
615
616         ret = request_threaded_irq(irq, NULL, tps6591x_irq, IRQF_ONESHOT,
617                                 "tps6591x", tps6591x);
618         if (!ret) {
619                 device_init_wakeup(tps6591x->dev, 1);
620                 enable_irq_wake(irq);
621         }
622
623         return ret;
624 }
625
626 static int __devinit tps6591x_add_subdevs(struct tps6591x *tps6591x,
627                                           struct tps6591x_platform_data *pdata)
628 {
629         struct tps6591x_subdev_info *subdev;
630         struct platform_device *pdev;
631         int i, ret = 0;
632
633         for (i = 0; i < pdata->num_subdevs; i++) {
634                 subdev = &pdata->subdevs[i];
635
636                 pdev = platform_device_alloc(subdev->name, subdev->id);
637
638                 pdev->dev.parent = tps6591x->dev;
639                 pdev->dev.platform_data = subdev->platform_data;
640
641                 ret = platform_device_add(pdev);
642                 if (ret)
643                         goto failed;
644         }
645         return 0;
646
647 failed:
648         tps6591x_remove_subdevs(tps6591x);
649         return ret;
650 }
651 #ifdef CONFIG_DEBUG_FS
652 #include <linux/debugfs.h>
653 #include <linux/seq_file.h>
654 static void print_regs(const char *header, struct seq_file *s,
655                 struct i2c_client *client, int start_offset,
656                 int end_offset)
657 {
658         uint8_t reg_val;
659         int i;
660         int ret;
661
662         seq_printf(s, "%s\n", header);
663         for (i = start_offset; i <= end_offset; ++i) {
664                 ret = __tps6591x_read(client, i, &reg_val);
665                 if (ret >= 0)
666                         seq_printf(s, "Reg 0x%02x Value 0x%02x\n", i, reg_val);
667         }
668         seq_printf(s, "------------------\n");
669 }
670
671 static int dbg_tps_show(struct seq_file *s, void *unused)
672 {
673         struct tps6591x *tps = s->private;
674         struct i2c_client *client = tps->client;
675
676         seq_printf(s, "TPS6591x Registers\n");
677         seq_printf(s, "------------------\n");
678
679         print_regs("Timing Regs",    s, client, 0x0, 0x6);
680         print_regs("Alarm Regs",     s, client, 0x8, 0xD);
681         print_regs("RTC Regs",       s, client, 0x10, 0x16);
682         print_regs("BCK Regs",       s, client, 0x17, 0x1B);
683         print_regs("PUADEN Regs",    s, client, 0x18, 0x18);
684         print_regs("REF Regs",       s, client, 0x1D, 0x1D);
685         print_regs("VDD Regs",       s, client, 0x1E, 0x29);
686         print_regs("LDO Regs",       s, client, 0x30, 0x37);
687         print_regs("THERM Regs",     s, client, 0x38, 0x38);
688         print_regs("BBCH Regs",      s, client, 0x39, 0x39);
689         print_regs("DCDCCNTRL Regs", s, client, 0x3E, 0x3E);
690         print_regs("DEV_CNTRL Regs", s, client, 0x3F, 0x40);
691         print_regs("SLEEP Regs",     s, client, 0x41, 0x44);
692         print_regs("EN1 Regs",       s, client, 0x45, 0x48);
693         print_regs("INT Regs",       s, client, 0x50, 0x55);
694         print_regs("GPIO Regs",      s, client, 0x60, 0x68);
695         print_regs("WATCHDOG Regs",  s, client, 0x69, 0x69);
696         print_regs("VMBCH Regs",     s, client, 0x6A, 0x6B);
697         print_regs("LED_CTRL Regs",  s, client, 0x6c, 0x6D);
698         print_regs("PWM_CTRL Regs",  s, client, 0x6E, 0x6F);
699         print_regs("SPARE Regs",     s, client, 0x70, 0x70);
700         print_regs("VERNUM Regs",    s, client, 0x80, 0x80);
701         return 0;
702 }
703
704 static int dbg_tps_open(struct inode *inode, struct file *file)
705 {
706         return single_open(file, dbg_tps_show, inode->i_private);
707 }
708
709 static const struct file_operations debug_fops = {
710         .open           = dbg_tps_open,
711         .read           = seq_read,
712         .llseek         = seq_lseek,
713         .release        = single_release,
714 };
715
716 static void __init tps6591x_debuginit(struct tps6591x *tps)
717 {
718         (void)debugfs_create_file("tps6591x", S_IRUGO, NULL,
719                         tps, &debug_fops);
720 }
721 #else
722 static void __init tps6591x_debuginit(struct tps6591x *tpsi)
723 {
724         return;
725 }
726 #endif
727
728 static int __init tps6591x_sleepinit(struct tps6591x *tpsi,
729                                         struct tps6591x_platform_data *pdata)
730 {
731         struct device *dev = NULL;
732         int ret = 0;
733
734         dev = tpsi->dev;
735
736         if (!pdata->dev_slp_en)
737                 goto no_err_return;
738
739         /* pmu dev_slp_en is set. Make sure slp_keepon is available before
740          * allowing SLEEP device state */
741         if (!pdata->slp_keepon) {
742                 dev_err(dev, "slp_keepon_data required for slp_en\n");
743                 goto err_sleep_init;
744         }
745
746         /* enabling SLEEP device state */
747         ret = tps6591x_set_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_SLP);
748         if (ret < 0) {
749                 dev_err(dev, "set dev_slp failed: %d\n", ret);
750                 goto err_sleep_init;
751         }
752
753         if (pdata->slp_keepon->therm_keepon) {
754                 ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON,
755                                                 SLEEP_KEEP_ON_THERM);
756                 if (ret < 0) {
757                         dev_err(dev, "set therm_keepon failed: %d\n", ret);
758                         goto disable_dev_slp;
759                 }
760         }
761
762         if (pdata->slp_keepon->clkout32k_keepon) {
763                 ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON,
764                                                 SLEEP_KEEP_ON_CLKOUT32K);
765                 if (ret < 0) {
766                         dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
767                         goto disable_dev_slp;
768                 }
769         }
770
771
772         if (pdata->slp_keepon->vrtc_keepon) {
773                 ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON,
774                                                 SLEEP_KEEP_ON_VRTC);
775                 if (ret < 0) {
776                         dev_err(dev, "set vrtc_keepon failed: %d\n", ret);
777                         goto disable_dev_slp;
778                 }
779         }
780
781         if (pdata->slp_keepon->i2chs_keepon) {
782                 ret = tps6591x_set_bits(dev, TPS6591X_SLEEP_KEEP_ON,
783                                                 SLEEP_KEEP_ON_I2CHS);
784                 if (ret < 0) {
785                         dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
786                         goto disable_dev_slp;
787                 }
788         }
789
790 no_err_return:
791         return 0;
792
793 disable_dev_slp:
794         tps6591x_clr_bits(dev, TPS6591X_DEVCTRL, DEVCTRL_DEV_SLP);
795
796 err_sleep_init:
797         return ret;
798 }
799
800 static int __devinit tps6591x_i2c_probe(struct i2c_client *client,
801                                         const struct i2c_device_id *id)
802 {
803         struct tps6591x_platform_data *pdata = client->dev.platform_data;
804         struct tps6591x *tps6591x;
805         int ret;
806
807         if (!pdata) {
808                 dev_err(&client->dev, "tps6591x requires platform data\n");
809                 return -ENOTSUPP;
810         }
811
812         ret = i2c_smbus_read_byte_data(client, TPS6591X_VERNUM);
813         if (ret < 0) {
814                 dev_err(&client->dev, "Silicon version number read"
815                                 " failed: %d\n", ret);
816                 return -EIO;
817         }
818
819         dev_info(&client->dev, "VERNUM is %02x\n", ret);
820
821         tps6591x = kzalloc(sizeof(struct tps6591x), GFP_KERNEL);
822         if (tps6591x == NULL)
823                 return -ENOMEM;
824
825         tps6591x->client = client;
826         tps6591x->dev = &client->dev;
827         i2c_set_clientdata(client, tps6591x);
828
829         mutex_init(&tps6591x->lock);
830
831         if (client->irq) {
832                 ret = tps6591x_irq_init(tps6591x, client->irq,
833                                         pdata->irq_base);
834                 if (ret) {
835                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
836                         goto err_irq_init;
837                 }
838         }
839
840         ret = tps6591x_add_subdevs(tps6591x, pdata);
841         if (ret) {
842                 dev_err(&client->dev, "add devices failed: %d\n", ret);
843                 goto err_add_devs;
844         }
845
846         tps6591x_gpio_init(tps6591x, pdata);
847
848         tps6591x_debuginit(tps6591x);
849
850         tps6591x_sleepinit(tps6591x, pdata);
851
852         if (pdata->use_power_off && !pm_power_off)
853                 pm_power_off = tps6591x_power_off;
854
855         tps6591x_i2c_client = client;
856
857         return 0;
858
859 err_add_devs:
860         if (client->irq)
861                 free_irq(client->irq, tps6591x);
862 err_irq_init:
863         kfree(tps6591x);
864         return ret;
865 }
866
867 static int __devexit tps6591x_i2c_remove(struct i2c_client *client)
868 {
869         struct tps6591x *tps6591x = i2c_get_clientdata(client);
870
871         if (client->irq)
872                 free_irq(client->irq, tps6591x);
873
874         if (gpiochip_remove(&tps6591x->gpio) < 0)
875                 dev_err(&client->dev, "Error in removing the gpio driver\n");
876
877         kfree(tps6591x);
878         return 0;
879 }
880 #ifdef CONFIG_PM
881 static int tps6591x_i2c_suspend(struct i2c_client *client, pm_message_t state)
882 {
883         if (client->irq)
884                 disable_irq(client->irq);
885         return 0;
886 }
887
888 static int tps6591x_i2c_resume(struct i2c_client *client)
889 {
890         if (client->irq)
891                 enable_irq(client->irq);
892         return 0;
893 }
894 #endif
895
896
897 static const struct i2c_device_id tps6591x_id_table[] = {
898         { "tps6591x", 0 },
899         { },
900 };
901 MODULE_DEVICE_TABLE(i2c, tps6591x_id_table);
902
903 static struct i2c_driver tps6591x_driver = {
904         .driver = {
905                 .name   = "tps6591x",
906                 .owner  = THIS_MODULE,
907         },
908         .probe          = tps6591x_i2c_probe,
909         .remove         = __devexit_p(tps6591x_i2c_remove),
910 #ifdef CONFIG_PM
911         .suspend        = tps6591x_i2c_suspend,
912         .resume         = tps6591x_i2c_resume,
913 #endif
914         .id_table       = tps6591x_id_table,
915 };
916
917 static int __init tps6591x_init(void)
918 {
919         return i2c_add_driver(&tps6591x_driver);
920 }
921 subsys_initcall(tps6591x_init);
922
923 static void __exit tps6591x_exit(void)
924 {
925         i2c_del_driver(&tps6591x_driver);
926 }
927 module_exit(tps6591x_exit);
928
929 MODULE_DESCRIPTION("TPS6591X core driver");
930 MODULE_LICENSE("GPL");