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