mfd: tps80031: Update to new IRQ API
[linux-2.6.git] / drivers / mfd / tps80031.c
1 /*
2  * driver/mfd/tps80031.c
3  *
4  * Core driver for TI TPS80031
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/tps80031.h>
35
36 /* interrupt related registers */
37 #define TPS80031_INT_STS_A              0xD0
38 #define TPS80031_INT_STS_B              0xD1
39 #define TPS80031_INT_STS_C              0xD2
40 #define TPS80031_INT_MSK_LINE_A         0xD3
41 #define TPS80031_INT_MSK_LINE_B         0xD4
42 #define TPS80031_INT_MSK_LINE_C         0xD5
43 #define TPS80031_INT_MSK_STS_A          0xD6
44 #define TPS80031_INT_MSK_STS_B          0xD7
45 #define TPS80031_INT_MSK_STS_C          0xD8
46
47 /* Version number related register */
48 #define TPS80031_JTAGVERNUM             0x87
49
50 /* External control register */
51 #define REGEN1_BASE_ADD         0xAE
52 #define REGEN2_BASE_ADD         0xB1
53 #define SYSEN_BASE_ADD          0xB4
54
55 /* device control registers */
56 #define TPS80031_PHOENIX_DEV_ON 0x25
57 #define DEVOFF  1
58
59 #define CLK32KAO_BASE_ADD       0xBA
60 #define CLK32KG_BASE_ADD        0xBD
61 #define CLK32KAUDIO_BASE_ADD    0xC0
62
63 #define EXT_CONTROL_CFG_TRANS 0
64 #define EXT_CONTROL_CFG_STATE 1
65
66 #define STATE_OFF       0x00
67 #define STATE_ON        0x01
68 #define STATE_MASK      0x03
69
70 #define TPS_NUM_SLAVES  4
71
72 static u8 pmc_ext_control_base[] = {
73         REGEN1_BASE_ADD,
74         REGEN2_BASE_ADD,
75         SYSEN_BASE_ADD,
76 };
77
78 struct tps80031_irq_data {
79         u8      mask_reg;
80         u8      mask_mask;
81 };
82
83 #define TPS80031_IRQ(_reg, _mask)                               \
84         {                                                       \
85                 .mask_reg = (TPS80031_INT_MSK_LINE_##_reg) -    \
86                                 TPS80031_INT_MSK_LINE_A,        \
87                 .mask_mask = (_mask),                           \
88         }
89
90 static const struct tps80031_irq_data tps80031_irqs[] = {
91
92         [TPS80031_INT_PWRON]            = TPS80031_IRQ(A, 0),
93         [TPS80031_INT_RPWRON]           = TPS80031_IRQ(A, 1),
94         [TPS80031_INT_SYS_VLOW]         = TPS80031_IRQ(A, 2),
95         [TPS80031_INT_RTC_ALARM]        = TPS80031_IRQ(A, 3),
96         [TPS80031_INT_RTC_PERIOD]       = TPS80031_IRQ(A, 4),
97         [TPS80031_INT_HOT_DIE]          = TPS80031_IRQ(A, 5),
98         [TPS80031_INT_VXX_SHORT]        = TPS80031_IRQ(A, 6),
99         [TPS80031_INT_SPDURATION]       = TPS80031_IRQ(A, 7),
100         [TPS80031_INT_WATCHDOG]         = TPS80031_IRQ(B, 0),
101         [TPS80031_INT_BAT]              = TPS80031_IRQ(B, 1),
102         [TPS80031_INT_SIM]              = TPS80031_IRQ(B, 2),
103         [TPS80031_INT_MMC]              = TPS80031_IRQ(B, 3),
104         [TPS80031_INT_RES]              = TPS80031_IRQ(B, 4),
105         [TPS80031_INT_GPADC_RT]         = TPS80031_IRQ(B, 5),
106         [TPS80031_INT_GPADC_SW2_EOC]    = TPS80031_IRQ(B, 6),
107         [TPS80031_INT_CC_AUTOCAL]       = TPS80031_IRQ(B, 7),
108         [TPS80031_INT_ID_WKUP]          = TPS80031_IRQ(C, 0),
109         [TPS80031_INT_VBUSS_WKUP]       = TPS80031_IRQ(C, 1),
110         [TPS80031_INT_ID]               = TPS80031_IRQ(C, 2),
111         [TPS80031_INT_VBUS]             = TPS80031_IRQ(C, 3),
112         [TPS80031_INT_CHRG_CTRL]        = TPS80031_IRQ(C, 4),
113         [TPS80031_INT_EXT_CHRG]         = TPS80031_IRQ(C, 5),
114         [TPS80031_INT_INT_CHRG]         = TPS80031_IRQ(C, 6),
115         [TPS80031_INT_RES2]             = TPS80031_IRQ(C, 7),
116 };
117
118 /* Structure for TPS80031 Slaves */
119 struct tps80031_client {
120         struct i2c_client *client;
121         struct mutex lock;
122         u8 addr;
123 };
124
125 struct tps80031 {
126         struct device           *dev;
127
128         struct gpio_chip        gpio;
129         struct irq_chip         irq_chip;
130         struct mutex            irq_lock;
131         int                     irq_base;
132         u32                     irq_en;
133         u8                      mask_cache[3];
134         u8                      mask_reg[3];
135         struct tps80031_client  tps_clients[TPS_NUM_SLAVES];
136 };
137
138 static inline int __tps80031_read(struct i2c_client *client,
139                                   int reg, uint8_t *val)
140 {
141         int ret;
142
143         ret = i2c_smbus_read_byte_data(client, reg);
144         if (ret < 0) {
145                 dev_err(&client->dev,
146                         "failed reading from addr 0x%02x, reg 0x%02x\n",
147                         client->addr, reg);
148                 return ret;
149         }
150
151         *val = (uint8_t)ret;
152
153         return 0;
154 }
155
156 static inline int __tps80031_reads(struct i2c_client *client, int reg,
157                                 int len, uint8_t *val)
158 {
159         int ret;
160
161         ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
162         if (ret < 0) {
163                 dev_err(&client->dev,
164                         "failed reading from addr 0x%02x, reg   0x%02x\n",
165                          client->addr, reg);
166                 return ret;
167         }
168
169         return 0;
170 }
171
172 static inline int __tps80031_write(struct i2c_client *client,
173                                  int reg, uint8_t val)
174 {
175         int ret;
176         ret = i2c_smbus_write_byte_data(client, reg, val);
177         if (ret < 0) {
178                 dev_err(&client->dev,
179                         "failed writing 0x%02x to 0x%02x\n", val, reg);
180                 return ret;
181         }
182
183         return 0;
184 }
185
186 static inline int __tps80031_writes(struct i2c_client *client, int reg,
187                                   int len, uint8_t *val)
188 {
189         int ret;
190
191         ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
192         if (ret < 0) {
193                 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
194                 return ret;
195         }
196
197         return 0;
198 }
199
200 int tps80031_write(struct device *dev, int sid, int reg, uint8_t val)
201 {
202         struct tps80031 *tps80031 = dev_get_drvdata(dev);
203         struct tps80031_client *tps = &tps80031->tps_clients[sid];
204         int ret;
205
206         mutex_lock(&tps->lock);
207         ret = __tps80031_write(tps->client, reg, val);
208         mutex_unlock(&tps->lock);
209
210         return ret;
211 }
212 EXPORT_SYMBOL_GPL(tps80031_write);
213
214 int tps80031_writes(struct device *dev, int sid, int reg, int len, uint8_t *val)
215 {
216         struct tps80031 *tps80031 = dev_get_drvdata(dev);
217         struct tps80031_client *tps = &tps80031->tps_clients[sid];
218         int ret;
219
220         mutex_lock(&tps->lock);
221         ret = __tps80031_writes(tps->client, reg, len, val);
222         mutex_unlock(&tps->lock);
223
224         return ret;
225 }
226 EXPORT_SYMBOL_GPL(tps80031_writes);
227
228 int tps80031_read(struct device *dev, int sid, int reg, uint8_t *val)
229 {
230         struct tps80031 *tps80031 = dev_get_drvdata(dev);
231         struct tps80031_client *tps = &tps80031->tps_clients[sid];
232
233         return __tps80031_read(tps->client, reg, val);
234 }
235 EXPORT_SYMBOL_GPL(tps80031_read);
236
237 int tps80031_reads(struct device *dev, int sid, int reg, int len, uint8_t *val)
238 {
239         struct tps80031 *tps80031 = dev_get_drvdata(dev);
240         struct tps80031_client *tps = &tps80031->tps_clients[sid];
241
242         return __tps80031_reads(tps->client, reg, len, val);
243 }
244 EXPORT_SYMBOL_GPL(tps80031_reads);
245
246 int tps80031_set_bits(struct device *dev, int sid, int reg, uint8_t bit_mask)
247 {
248         struct tps80031 *tps80031 = dev_get_drvdata(dev);
249         struct tps80031_client *tps = &tps80031->tps_clients[sid];
250         uint8_t reg_val;
251         int ret = 0;
252
253         mutex_lock(&tps->lock);
254
255         ret = __tps80031_read(tps->client, reg, &reg_val);
256         if (ret)
257                 goto out;
258
259         if ((reg_val & bit_mask) != bit_mask) {
260                 reg_val |= bit_mask;
261                 ret = __tps80031_write(tps->client, reg, reg_val);
262         }
263 out:
264         mutex_unlock(&tps->lock);
265         return ret;
266 }
267 EXPORT_SYMBOL_GPL(tps80031_set_bits);
268
269 int tps80031_clr_bits(struct device *dev, int sid, int reg, uint8_t bit_mask)
270 {
271         struct tps80031 *tps80031 = dev_get_drvdata(dev);
272         struct tps80031_client *tps = &tps80031->tps_clients[sid];
273         uint8_t reg_val;
274         int ret = 0;
275
276         mutex_lock(&tps->lock);
277
278         ret = __tps80031_read(tps->client, reg, &reg_val);
279         if (ret)
280                 goto out;
281
282         if (reg_val & bit_mask) {
283                 reg_val &= ~bit_mask;
284                 ret = __tps80031_write(tps->client, reg, reg_val);
285         }
286 out:
287         mutex_unlock(&tps->lock);
288         return ret;
289 }
290 EXPORT_SYMBOL_GPL(tps80031_clr_bits);
291
292 int tps80031_update(struct device *dev, int sid, int reg, uint8_t val,
293                 uint8_t mask)
294 {
295         struct tps80031 *tps80031 = dev_get_drvdata(dev);
296         struct tps80031_client *tps = &tps80031->tps_clients[sid];
297         uint8_t reg_val;
298         int ret = 0;
299
300         mutex_lock(&tps->lock);
301
302         ret = __tps80031_read(tps->client, reg, &reg_val);
303         if (ret)
304                 goto out;
305
306         if ((reg_val & mask) != val) {
307                 reg_val = (reg_val & ~mask) | (val & mask);
308                 ret = __tps80031_write(tps->client, reg, reg_val);
309         }
310 out:
311         mutex_unlock(&tps->lock);
312         return ret;
313 }
314 EXPORT_SYMBOL_GPL(tps80031_update);
315
316 int tps80031_force_update(struct device *dev, int sid, int reg, uint8_t val,
317                           uint8_t mask)
318 {
319         struct tps80031 *tps80031 = dev_get_drvdata(dev);
320         struct tps80031_client *tps = &tps80031->tps_clients[sid];
321         uint8_t reg_val;
322         int ret = 0;
323
324         mutex_lock(&tps->lock);
325
326         ret = __tps80031_read(tps->client, reg, &reg_val);
327         if (ret)
328                 goto out;
329
330         reg_val = (reg_val & ~mask) | (val & mask);
331         ret = __tps80031_write(tps->client, reg, reg_val);
332
333 out:
334         mutex_unlock(&tps->lock);
335         return ret;
336 }
337 EXPORT_SYMBOL_GPL(tps80031_force_update);
338
339 static struct tps80031 *tps80031_dev;
340 int tps80031_power_off(void)
341 {
342         struct tps80031_client *tps = &tps80031_dev->tps_clients[SLAVE_ID1];
343
344         if (!tps->client)
345                 return -EINVAL;
346         dev_info(&tps->client->dev, "switching off PMU\n");
347         return __tps80031_write(tps->client, TPS80031_PHOENIX_DEV_ON, DEVOFF);
348 }
349
350 static int tps80031_gpio_get(struct gpio_chip *gc, unsigned offset)
351 {
352         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
353         struct tps80031_client *tps = &tps80031->tps_clients[SLAVE_ID1];
354         uint8_t state;
355         uint8_t trans;
356         int ret;
357
358         ret = __tps80031_read(tps->client,
359                         pmc_ext_control_base[offset] +
360                                 EXT_CONTROL_CFG_STATE, &state);
361         if (ret)
362                 return ret;
363
364         if (state != 0) {
365                 ret = __tps80031_read(tps->client,
366                                 pmc_ext_control_base[offset] +
367                                         EXT_CONTROL_CFG_TRANS, &trans);
368                 if (ret)
369                         return ret;
370                 return trans & 0x1;
371         }
372         return 0;
373 }
374
375 static void tps80031_gpio_set(struct gpio_chip *gc, unsigned offset,
376                         int value)
377 {
378         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
379
380         tps80031_update(tps80031->dev, SLAVE_ID1,
381                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_TRANS,
382                         value, 0x1);
383 }
384
385 static int tps80031_gpio_input(struct gpio_chip *gc, unsigned offset)
386 {
387         return -EIO;
388 }
389
390 static int tps80031_gpio_output(struct gpio_chip *gc, unsigned offset,
391                                 int value)
392 {
393         tps80031_gpio_set(gc, offset, value);
394         return 0;
395 }
396
397 static int tps80031_gpio_enable(struct gpio_chip *gc, unsigned offset)
398 {
399         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
400         int ret;
401
402         ret = tps80031_update(tps80031->dev, SLAVE_ID1,
403                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_STATE,
404                                                 STATE_ON, STATE_MASK);
405         if (ret)
406                 return ret;
407
408         return tps80031_write(tps80031->dev, SLAVE_ID1,
409                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_TRANS, 0x0);
410 }
411
412 static void tps80031_gpio_disable(struct gpio_chip *gc, unsigned offset)
413 {
414         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
415         tps80031_update(tps80031->dev, SLAVE_ID1,
416                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_STATE,
417                                                 STATE_OFF, STATE_MASK);
418 }
419
420 static void tps80031_gpio_init(struct tps80031 *tps80031,
421                         struct tps80031_platform_data *pdata)
422 {
423         int ret;
424         int gpio_base = pdata->gpio_base;
425         struct tps80031_client *tps = &tps80031->tps_clients[SLAVE_ID1];
426
427         if (gpio_base <= 0)
428                 return;
429
430         tps80031->gpio.owner            = THIS_MODULE;
431         tps80031->gpio.label            = tps->client->name;
432         tps80031->gpio.dev              = tps80031->dev;
433         tps80031->gpio.base             = gpio_base;
434         tps80031->gpio.ngpio            = 3;
435         tps80031->gpio.can_sleep        = 1;
436
437         tps80031->gpio.request          = tps80031_gpio_enable;
438         tps80031->gpio.free             = tps80031_gpio_disable;
439         tps80031->gpio.direction_input  = tps80031_gpio_input;
440         tps80031->gpio.direction_output = tps80031_gpio_output;
441         tps80031->gpio.set              = tps80031_gpio_set;
442         tps80031->gpio.get              = tps80031_gpio_get;
443
444         ret = gpiochip_add(&tps80031->gpio);
445         if (ret)
446                 dev_warn(tps80031->dev, "GPIO registration failed: %d\n", ret);
447 }
448
449 static int __remove_subdev(struct device *dev, void *unused)
450 {
451         platform_device_unregister(to_platform_device(dev));
452         return 0;
453 }
454
455 static int tps80031_remove_subdevs(struct tps80031 *tps80031)
456 {
457         return device_for_each_child(tps80031->dev, NULL, __remove_subdev);
458 }
459
460 static void tps80031_irq_lock(struct irq_data *data)
461 {
462         struct tps80031 *tps80031 = irq_data_get_irq_chip_data(data);
463
464         mutex_lock(&tps80031->irq_lock);
465 }
466
467 static void tps80031_irq_enable(struct irq_data *data)
468 {
469         struct tps80031 *tps80031 = irq_data_get_irq_chip_data(data);
470         unsigned int __irq = data->irq - tps80031->irq_base;
471         const struct tps80031_irq_data *irq_data = &tps80031_irqs[__irq];
472
473         tps80031->mask_reg[irq_data->mask_reg] &= ~(1 << irq_data->mask_mask);
474         tps80031->irq_en |= (1 << __irq);
475 }
476
477 static void tps80031_irq_disable(struct irq_data *data)
478 {
479         struct tps80031 *tps80031 = irq_data_get_irq_chip_data(data);
480
481         unsigned int __irq = data->irq - tps80031->irq_base;
482         const struct tps80031_irq_data *irq_data = &tps80031_irqs[__irq];
483
484         tps80031->mask_reg[irq_data->mask_reg] |= (1 << irq_data->mask_mask);
485         tps80031->irq_en &= ~(1 << __irq);
486 }
487
488 static void tps80031_irq_sync_unlock(struct irq_data *data)
489 {
490         struct tps80031 *tps80031 = irq_data_get_irq_chip_data(data);
491         int i;
492
493         for (i = 0; i < ARRAY_SIZE(tps80031->mask_reg); i++) {
494                 if (tps80031->mask_reg[i] != tps80031->mask_cache[i]) {
495                         if (!WARN_ON(tps80031_write(tps80031->dev, SLAVE_ID2,
496                                                 TPS80031_INT_MSK_LINE_A + i,
497                                                 tps80031->mask_reg[i])))
498                                 if (!WARN_ON(tps80031_write(tps80031->dev,
499                                                 SLAVE_ID2,
500                                                 TPS80031_INT_MSK_STS_A + i,
501                                                 tps80031->mask_reg[i])))
502                                         tps80031->mask_cache[i] =
503                                                         tps80031->mask_reg[i];
504                 }
505         }
506
507         mutex_unlock(&tps80031->irq_lock);
508 }
509
510 static irqreturn_t tps80031_irq(int irq, void *data)
511 {
512         struct tps80031 *tps80031 = data;
513         int ret = 0;
514         u8 tmp[3];
515         u32 acks;
516         int i;
517
518         for (i = 0; i < 3; i++) {
519                 ret = tps80031_read(tps80031->dev, SLAVE_ID2,
520                         TPS80031_INT_STS_A + i, &tmp[i]);
521                 if (ret < 0) {
522                         dev_err(tps80031->dev, "failed to read interrupt "
523                                                         "status\n");
524                         return IRQ_NONE;
525                 }
526                 if (tmp[i]) {
527                         ret = tps80031_write(tps80031->dev, SLAVE_ID2,
528                                         TPS80031_INT_STS_A + i, tmp[i]);
529                         if (ret < 0) {
530                                 dev_err(tps80031->dev, "failed to write "
531                                                         "interrupt status\n");
532                                 return IRQ_NONE;
533                         }
534                 }
535         }
536         acks = (tmp[2] << 16) | (tmp[1] << 8) | tmp[0];
537         while (acks) {
538                 i = __ffs(acks);
539                 if (tps80031->irq_en & (1 << i))
540                         handle_nested_irq(tps80031->irq_base + i);
541                 acks &= ~(1 << i);
542         }
543         return IRQ_HANDLED;
544 }
545
546 static int __devinit tps80031_irq_init(struct tps80031 *tps80031, int irq,
547                                 int irq_base)
548 {
549         int i, ret;
550
551         if (!irq_base) {
552                 dev_warn(tps80031->dev, "No interrupt support on IRQ base\n");
553                 return -EINVAL;
554         }
555
556         mutex_init(&tps80031->irq_lock);
557
558         for (i = 0; i < 3; i++) {
559                 tps80031->mask_reg[i] = 0xFF;
560                 tps80031->mask_cache[i] = tps80031->mask_reg[i];
561                 tps80031_write(tps80031->dev, SLAVE_ID2,
562                                         TPS80031_INT_MSK_LINE_A + i,
563                                         tps80031->mask_cache[i]);
564                 tps80031_write(tps80031->dev, SLAVE_ID2,
565                                         TPS80031_INT_MSK_STS_A + i, 0xFF);
566                 tps80031_write(tps80031->dev, SLAVE_ID2,
567                                         TPS80031_INT_STS_A + i, 0xFF);
568         }
569
570         tps80031->irq_base = irq_base;
571
572         tps80031->irq_chip.name = "tps80031";
573         tps80031->irq_chip.irq_enable = tps80031_irq_enable;
574         tps80031->irq_chip.irq_disable = tps80031_irq_disable;
575         tps80031->irq_chip.irq_bus_lock = tps80031_irq_lock;
576         tps80031->irq_chip.irq_bus_sync_unlock = tps80031_irq_sync_unlock;
577
578         for (i = 0; i < ARRAY_SIZE(tps80031_irqs); i++) {
579                 int __irq = i + tps80031->irq_base;
580                 irq_set_chip_data(__irq, tps80031);
581                 irq_set_chip_and_handler(__irq, &tps80031->irq_chip,
582                                          handle_simple_irq);
583                 irq_set_nested_thread(__irq, 1);
584 #ifdef CONFIG_ARM
585                 irq_set_status_flags(__irq, IRQF_VALID);
586 #endif
587         }
588
589         ret = request_threaded_irq(irq, NULL, tps80031_irq, IRQF_ONESHOT,
590                                 "tps80031", tps80031);
591         if (!ret) {
592                 device_init_wakeup(tps80031->dev, 1);
593                 enable_irq_wake(irq);
594         }
595
596         return ret;
597 }
598
599 static void tps80031_clk32k_enable(struct tps80031 *tps80031, int base_add)
600 {
601         int ret;
602         ret = tps80031_update(tps80031->dev, SLAVE_ID1,
603                         base_add + EXT_CONTROL_CFG_STATE, STATE_ON, STATE_MASK);
604         if (!ret)
605                 ret = tps80031_update(tps80031->dev, SLAVE_ID1,
606                                 base_add + EXT_CONTROL_CFG_TRANS,
607                                 STATE_ON, STATE_MASK);
608         if (ret < 0)
609                 dev_err(tps80031->dev, "Error in updating clock register\n");
610 }
611
612 static void tps80031_clk32k_init(struct tps80031 *tps80031,
613                         struct tps80031_platform_data *pdata)
614 {
615         struct tps80031_32kclock_plat_data *clk32k_pdata;
616
617         if (!(pdata && pdata->clk32k_pdata))
618                 return;
619
620         clk32k_pdata = pdata->clk32k_pdata;
621         if (clk32k_pdata->en_clk32kao)
622                 tps80031_clk32k_enable(tps80031, CLK32KAO_BASE_ADD);
623
624         if (clk32k_pdata->en_clk32kg)
625                 tps80031_clk32k_enable(tps80031, CLK32KG_BASE_ADD);
626
627         if (clk32k_pdata->en_clk32kaudio)
628                 tps80031_clk32k_enable(tps80031, CLK32KAUDIO_BASE_ADD);
629 }
630
631 static int __devinit tps80031_add_subdevs(struct tps80031 *tps80031,
632                                           struct tps80031_platform_data *pdata)
633 {
634         struct tps80031_subdev_info *subdev;
635         struct platform_device *pdev;
636         int i, ret = 0;
637
638         for (i = 0; i < pdata->num_subdevs; i++) {
639                 subdev = &pdata->subdevs[i];
640
641                 pdev = platform_device_alloc(subdev->name, subdev->id);
642
643                 pdev->dev.parent = tps80031->dev;
644                 pdev->dev.platform_data = subdev->platform_data;
645
646                 ret = platform_device_add(pdev);
647                 if (ret)
648                         goto failed;
649         }
650         return 0;
651
652 failed:
653         tps80031_remove_subdevs(tps80031);
654         return ret;
655 }
656
657 #ifdef CONFIG_DEBUG_FS
658 #include <linux/debugfs.h>
659 #include <linux/seq_file.h>
660 static void print_regs(const char *header, struct seq_file *s,
661                 int sid, int start_offset, int end_offset)
662 {
663         struct tps80031 *tps80031 = s->private;
664         struct tps80031_client *tps = &tps80031->tps_clients[sid];
665         uint8_t reg_val;
666         int i;
667         int ret;
668
669         seq_printf(s, "%s\n", header);
670         for (i = start_offset; i <= end_offset; ++i) {
671                 ret = __tps80031_read(tps->client, i, &reg_val);
672                 if (ret >= 0)
673                         seq_printf(s, "Addr = 0x%02x Reg 0x%02x Value 0x%02x\n",
674                                                 tps->client->addr, i, reg_val);
675         }
676         seq_printf(s, "------------------\n");
677 }
678
679 static int dbg_tps_show(struct seq_file *s, void *unused)
680 {
681         seq_printf(s, "TPS80031 Registers\n");
682         seq_printf(s, "------------------\n");
683         print_regs("VIO Regs",       s, SLAVE_ID1, 0x47, 0x49);
684         print_regs("VIO Regs",       s, SLAVE_ID0, 0x49, 0x4A);
685         print_regs("SMPS1 Regs",     s, SLAVE_ID1, 0x53, 0x54);
686         print_regs("SMPS1 Regs",     s, SLAVE_ID0, 0x55, 0x56);
687         print_regs("SMPS1 Regs",     s, SLAVE_ID1, 0x57, 0x57);
688         print_regs("SMPS2 Regs",     s, SLAVE_ID1, 0x59, 0x5B);
689         print_regs("SMPS2 Regs",     s, SLAVE_ID0, 0x5B, 0x5C);
690         print_regs("SMPS2 Regs",     s, SLAVE_ID1, 0x5C, 0x5D);
691         print_regs("SMPS3 Regs",     s, SLAVE_ID1, 0x65, 0x68);
692         print_regs("SMPS4 Regs",     s, SLAVE_ID1, 0x41, 0x44);
693         print_regs("VANA Regs",      s, SLAVE_ID1, 0x81, 0x83);
694         print_regs("VRTC Regs",      s, SLAVE_ID1, 0xC3, 0xC4);
695         print_regs("LDO1 Regs",      s, SLAVE_ID1, 0x9D, 0x9F);
696         print_regs("LDO2 Regs",      s, SLAVE_ID1, 0x85, 0x87);
697         print_regs("LDO3 Regs",      s, SLAVE_ID1, 0x8D, 0x8F);
698         print_regs("LDO4 Regs",      s, SLAVE_ID1, 0x89, 0x8B);
699         print_regs("LDO5 Regs",      s, SLAVE_ID1, 0x99, 0x9B);
700         print_regs("LDO6 Regs",      s, SLAVE_ID1, 0x91, 0x93);
701         print_regs("LDO7 Regs",      s, SLAVE_ID1, 0xA5, 0xA7);
702         print_regs("LDOUSB Regs",    s, SLAVE_ID1, 0xA1, 0xA3);
703         print_regs("LDOLN Regs",     s, SLAVE_ID1, 0x95, 0x97);
704         print_regs("REGEN1 Regs",    s, SLAVE_ID1, 0xAE, 0xAF);
705         print_regs("REGEN2 Regs",    s, SLAVE_ID1, 0xB1, 0xB2);
706         print_regs("SYSEN Regs",     s, SLAVE_ID1, 0xB4, 0xB5);
707         print_regs("CLK32KAO Regs",  s, SLAVE_ID1, 0xBA, 0xBB);
708         print_regs("CLK32KG Regs",   s, SLAVE_ID1, 0xBD, 0xBE);
709         print_regs("CLK32KAUD Regs", s, SLAVE_ID1, 0xC0, 0xC1);
710         print_regs("INT Regs",       s, SLAVE_ID2, 0xD0, 0xD8);
711         print_regs("PREQ Regs",      s, SLAVE_ID1, 0xD7, 0xDF);
712         print_regs("MASK_PH Regs",   s, SLAVE_ID1, 0x20, 0x21);
713         print_regs("VERNUM Regs",    s, SLAVE_ID1, 0x87, 0x87);
714         return 0;
715 }
716
717 static int dbg_tps_open(struct inode *inode, struct file *file)
718 {
719         return single_open(file, dbg_tps_show, inode->i_private);
720 }
721
722 static const struct file_operations debug_fops = {
723         .open           = dbg_tps_open,
724         .read           = seq_read,
725         .llseek         = seq_lseek,
726         .release        = single_release,
727 };
728
729 static void __init tps80031_debuginit(struct tps80031 *tps)
730 {
731         (void)debugfs_create_file("tps80031", S_IRUGO, NULL,
732                         tps, &debug_fops);
733 }
734 #else
735 static void __init tps80031_debuginit(struct tps80031 *tpsi)
736 {
737         return;
738 }
739 #endif
740
741 static int __devexit tps80031_i2c_remove(struct i2c_client *client)
742 {
743         struct tps80031 *tps80031 = i2c_get_clientdata(client);
744         int i;
745
746         if (client->irq)
747                 free_irq(client->irq, tps80031);
748
749         if (tps80031->gpio.owner != NULL)
750                 if (gpiochip_remove(&tps80031->gpio) < 0)
751                         dev_err(&client->dev, "Error in removing the gpio driver\n");
752
753         for (i = 0; i < TPS_NUM_SLAVES; i++) {
754                 struct tps80031_client *tps = &tps80031->tps_clients[i];
755                 if (tps->client && tps->client != client)
756                         i2c_unregister_device(tps->client);
757                 tps80031->tps_clients[i].client = NULL;
758                 mutex_destroy(&tps->lock);
759         }
760
761         kfree(tps80031);
762         return 0;
763 }
764
765 static int __devinit tps80031_i2c_probe(struct i2c_client *client,
766                                         const struct i2c_device_id *id)
767 {
768         struct tps80031_platform_data *pdata = client->dev.platform_data;
769         struct tps80031 *tps80031;
770         struct tps80031_client *tps;
771         int ret;
772         int i;
773
774         if (!pdata) {
775                 dev_err(&client->dev, "tps80031 requires platform data\n");
776                 return -ENOTSUPP;
777         }
778
779         ret = i2c_smbus_read_byte_data(client, TPS80031_JTAGVERNUM);
780         if (ret < 0) {
781                 dev_err(&client->dev, "Silicon version number read"
782                                 " failed: %d\n", ret);
783                 return -EIO;
784         }
785
786         dev_info(&client->dev, "VERNUM is %02x\n", ret);
787
788         tps80031 = kzalloc(sizeof(struct tps80031), GFP_KERNEL);
789         if (tps80031 == NULL)
790                 return -ENOMEM;
791
792         tps80031->dev = &client->dev;
793         i2c_set_clientdata(client, tps80031);
794
795         /* Set up slaves */
796         tps80031->tps_clients[SLAVE_ID0].addr = I2C_ID0_ADDR;
797         tps80031->tps_clients[SLAVE_ID1].addr = I2C_ID1_ADDR;
798         tps80031->tps_clients[SLAVE_ID2].addr = I2C_ID2_ADDR;
799         tps80031->tps_clients[SLAVE_ID3].addr = I2C_ID3_ADDR;
800         for (i = 0; i < TPS_NUM_SLAVES; i++) {
801                 tps = &tps80031->tps_clients[i];
802                 if (tps->addr == client->addr)
803                         tps->client = client;
804                 else
805                         tps->client = i2c_new_dummy(client->adapter,
806                                                 tps->addr);
807                 if (!tps->client) {
808                         dev_err(&client->dev, "can't attach client %d\n", i);
809                         ret = -ENOMEM;
810                         goto fail;
811                 }
812                 i2c_set_clientdata(tps->client, tps80031);
813                 mutex_init(&tps->lock);
814         }
815
816         if (client->irq) {
817                 ret = tps80031_irq_init(tps80031, client->irq,
818                                         pdata->irq_base);
819                 if (ret) {
820                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
821                         goto fail;
822                 }
823         }
824
825         ret = tps80031_add_subdevs(tps80031, pdata);
826         if (ret) {
827                 dev_err(&client->dev, "add devices failed: %d\n", ret);
828                 goto fail;
829         }
830
831         tps80031_gpio_init(tps80031, pdata);
832
833         tps80031_clk32k_init(tps80031, pdata);
834
835         tps80031_debuginit(tps80031);
836
837         tps80031_dev = tps80031;
838
839         return 0;
840
841 fail:
842         tps80031_i2c_remove(client);
843         return ret;
844 }
845
846 #ifdef CONFIG_PM
847 static int tps80031_i2c_suspend(struct i2c_client *client, pm_message_t state)
848 {
849         if (client->irq)
850                 disable_irq(client->irq);
851         return 0;
852 }
853
854 static int tps80031_i2c_resume(struct i2c_client *client)
855 {
856         if (client->irq)
857                 enable_irq(client->irq);
858         return 0;
859 }
860 #endif
861
862
863 static const struct i2c_device_id tps80031_id_table[] = {
864         { "tps80031", 0 },
865         { },
866 };
867 MODULE_DEVICE_TABLE(i2c, tps80031_id_table);
868
869 static struct i2c_driver tps80031_driver = {
870         .driver = {
871                 .name   = "tps80031",
872                 .owner  = THIS_MODULE,
873         },
874         .probe          = tps80031_i2c_probe,
875         .remove         = __devexit_p(tps80031_i2c_remove),
876 #ifdef CONFIG_PM
877         .suspend        = tps80031_i2c_suspend,
878         .resume         = tps80031_i2c_resume,
879 #endif
880         .id_table       = tps80031_id_table,
881 };
882
883 static int __init tps80031_init(void)
884 {
885         return i2c_add_driver(&tps80031_driver);
886 }
887 subsys_initcall(tps80031_init);
888
889 static void __exit tps80031_exit(void)
890 {
891         i2c_del_driver(&tps80031_driver);
892 }
893 module_exit(tps80031_exit);
894
895 MODULE_DESCRIPTION("TPS80031 core driver");
896 MODULE_LICENSE("GPL");