mfd: tps80031: Adding Power off functionality
[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 static struct tps80031 *tps80031_dev;
317 int tps80031_power_off(void)
318 {
319         struct tps80031_client *tps = &tps80031_dev->tps_clients[SLAVE_ID1];
320
321         if (!tps->client)
322                 return -EINVAL;
323         dev_info(&tps->client->dev, "switching off PMU\n");
324         return __tps80031_write(tps->client, TPS80031_PHOENIX_DEV_ON, DEVOFF);
325 }
326
327 static int tps80031_gpio_get(struct gpio_chip *gc, unsigned offset)
328 {
329         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
330         struct tps80031_client *tps = &tps80031->tps_clients[SLAVE_ID1];
331         uint8_t state;
332         uint8_t trans;
333         int ret;
334
335         ret = __tps80031_read(tps->client,
336                         pmc_ext_control_base[offset] +
337                                 EXT_CONTROL_CFG_STATE, &state);
338         if (ret)
339                 return ret;
340
341         if (state != 0) {
342                 ret = __tps80031_read(tps->client,
343                                 pmc_ext_control_base[offset] +
344                                         EXT_CONTROL_CFG_TRANS, &trans);
345                 if (ret)
346                         return ret;
347                 return trans & 0x1;
348         }
349         return 0;
350 }
351
352 static void tps80031_gpio_set(struct gpio_chip *gc, unsigned offset,
353                         int value)
354 {
355         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
356
357         tps80031_update(tps80031->dev, SLAVE_ID1,
358                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_TRANS,
359                         value, 0x1);
360 }
361
362 static int tps80031_gpio_input(struct gpio_chip *gc, unsigned offset)
363 {
364         return -EIO;
365 }
366
367 static int tps80031_gpio_output(struct gpio_chip *gc, unsigned offset,
368                                 int value)
369 {
370         tps80031_gpio_set(gc, offset, value);
371         return 0;
372 }
373
374 static int tps80031_gpio_enable(struct gpio_chip *gc, unsigned offset)
375 {
376         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
377         int ret;
378
379         ret = tps80031_update(tps80031->dev, SLAVE_ID1,
380                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_STATE,
381                                                 STATE_ON, STATE_MASK);
382         if (ret)
383                 return ret;
384
385         return tps80031_write(tps80031->dev, SLAVE_ID1,
386                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_TRANS, 0x0);
387 }
388
389 static void tps80031_gpio_disable(struct gpio_chip *gc, unsigned offset)
390 {
391         struct tps80031 *tps80031 = container_of(gc, struct tps80031, gpio);
392         tps80031_update(tps80031->dev, SLAVE_ID1,
393                 pmc_ext_control_base[offset] + EXT_CONTROL_CFG_STATE,
394                                                 STATE_OFF, STATE_MASK);
395 }
396
397 static void tps80031_gpio_init(struct tps80031 *tps80031,
398                         struct tps80031_platform_data *pdata)
399 {
400         int ret;
401         int gpio_base = pdata->gpio_base;
402         struct tps80031_client *tps = &tps80031->tps_clients[SLAVE_ID1];
403
404         if (gpio_base <= 0)
405                 return;
406
407         tps80031->gpio.owner            = THIS_MODULE;
408         tps80031->gpio.label            = tps->client->name;
409         tps80031->gpio.dev              = tps80031->dev;
410         tps80031->gpio.base             = gpio_base;
411         tps80031->gpio.ngpio            = 3;
412         tps80031->gpio.can_sleep        = 1;
413
414         tps80031->gpio.request          = tps80031_gpio_enable;
415         tps80031->gpio.free             = tps80031_gpio_disable;
416         tps80031->gpio.direction_input  = tps80031_gpio_input;
417         tps80031->gpio.direction_output = tps80031_gpio_output;
418         tps80031->gpio.set              = tps80031_gpio_set;
419         tps80031->gpio.get              = tps80031_gpio_get;
420
421         ret = gpiochip_add(&tps80031->gpio);
422         if (ret)
423                 dev_warn(tps80031->dev, "GPIO registration failed: %d\n", ret);
424 }
425
426 static int __remove_subdev(struct device *dev, void *unused)
427 {
428         platform_device_unregister(to_platform_device(dev));
429         return 0;
430 }
431
432 static int tps80031_remove_subdevs(struct tps80031 *tps80031)
433 {
434         return device_for_each_child(tps80031->dev, NULL, __remove_subdev);
435 }
436
437 static void tps80031_irq_lock(unsigned int irq)
438 {
439         struct tps80031 *tps80031 = get_irq_chip_data(irq);
440
441         mutex_lock(&tps80031->irq_lock);
442 }
443
444 static void tps80031_irq_enable(unsigned int irq)
445 {
446         struct tps80031 *tps80031 = get_irq_chip_data(irq);
447         unsigned int __irq = irq - tps80031->irq_base;
448         const struct tps80031_irq_data *data = &tps80031_irqs[__irq];
449
450         tps80031->mask_reg[data->mask_reg] &= ~(1 << data->mask_mask);
451         tps80031->irq_en |= (1 << __irq);
452 }
453
454 static void tps80031_irq_disable(unsigned int irq)
455 {
456         struct tps80031 *tps80031 = get_irq_chip_data(irq);
457
458         unsigned int __irq = irq - tps80031->irq_base;
459         const struct tps80031_irq_data *data = &tps80031_irqs[__irq];
460
461         tps80031->mask_reg[data->mask_reg] |= (1 << data->mask_mask);
462         tps80031->irq_en &= ~(1 << __irq);
463 }
464
465 static void tps80031_irq_sync_unlock(unsigned int irq)
466 {
467         struct tps80031 *tps80031 = get_irq_chip_data(irq);
468         int i;
469
470         for (i = 0; i < ARRAY_SIZE(tps80031->mask_reg); i++) {
471                 if (tps80031->mask_reg[i] != tps80031->mask_cache[i]) {
472                         if (!WARN_ON(tps80031_write(tps80031->dev, SLAVE_ID2,
473                                                 TPS80031_INT_MSK_LINE_A + i,
474                                                 tps80031->mask_reg[i])))
475                                 if (!WARN_ON(tps80031_write(tps80031->dev,
476                                                 SLAVE_ID2,
477                                                 TPS80031_INT_MSK_STS_A + i,
478                                                 tps80031->mask_reg[i])))
479                                         tps80031->mask_cache[i] =
480                                                         tps80031->mask_reg[i];
481                 }
482         }
483
484         mutex_unlock(&tps80031->irq_lock);
485 }
486
487 static irqreturn_t tps80031_irq(int irq, void *data)
488 {
489         struct tps80031 *tps80031 = data;
490         int ret = 0;
491         u8 tmp[3];
492         u32 acks;
493         int i;
494
495         for (i = 0; i < 3; i++) {
496                 ret = tps80031_read(tps80031->dev, SLAVE_ID2,
497                         TPS80031_INT_STS_A + i, &tmp[i]);
498                 if (ret < 0) {
499                         dev_err(tps80031->dev, "failed to read interrupt "
500                                                         "status\n");
501                         return IRQ_NONE;
502                 }
503                 if (tmp[i]) {
504                         ret = tps80031_write(tps80031->dev, SLAVE_ID2,
505                                         TPS80031_INT_STS_A + i, tmp[i]);
506                         if (ret < 0) {
507                                 dev_err(tps80031->dev, "failed to write "
508                                                         "interrupt status\n");
509                                 return IRQ_NONE;
510                         }
511                 }
512         }
513         acks = (tmp[2] << 16) | (tmp[1] << 8) | tmp[0];
514         while (acks) {
515                 i = __ffs(acks);
516                 if (tps80031->irq_en & (1 << i))
517                         handle_nested_irq(tps80031->irq_base + i);
518                 acks &= ~(1 << i);
519         }
520         return IRQ_HANDLED;
521 }
522
523 static int __devinit tps80031_irq_init(struct tps80031 *tps80031, int irq,
524                                 int irq_base)
525 {
526         int i, ret;
527
528         if (!irq_base) {
529                 dev_warn(tps80031->dev, "No interrupt support on IRQ base\n");
530                 return -EINVAL;
531         }
532
533         mutex_init(&tps80031->irq_lock);
534
535         for (i = 0; i < 3; i++) {
536                 tps80031->mask_reg[i] = 0xFF;
537                 tps80031->mask_cache[i] = tps80031->mask_reg[i];
538                 tps80031_write(tps80031->dev, SLAVE_ID2,
539                                         TPS80031_INT_MSK_LINE_A + i,
540                                         tps80031->mask_cache[i]);
541                 tps80031_write(tps80031->dev, SLAVE_ID2,
542                                         TPS80031_INT_MSK_STS_A + i, 0xFF);
543                 tps80031_write(tps80031->dev, SLAVE_ID2,
544                                         TPS80031_INT_STS_A + i, 0xFF);
545         }
546
547         tps80031->irq_base = irq_base;
548
549         tps80031->irq_chip.name = "tps80031";
550         tps80031->irq_chip.enable = tps80031_irq_enable;
551         tps80031->irq_chip.disable = tps80031_irq_disable;
552         tps80031->irq_chip.bus_lock = tps80031_irq_lock;
553         tps80031->irq_chip.bus_sync_unlock = tps80031_irq_sync_unlock;
554
555         for (i = 0; i < ARRAY_SIZE(tps80031_irqs); i++) {
556                 int __irq = i + tps80031->irq_base;
557                 set_irq_chip_data(__irq, tps80031);
558                 set_irq_chip_and_handler(__irq, &tps80031->irq_chip,
559                                          handle_simple_irq);
560                 set_irq_nested_thread(__irq, 1);
561 #ifdef CONFIG_ARM
562                 set_irq_flags(__irq, IRQF_VALID);
563 #endif
564         }
565
566         ret = request_threaded_irq(irq, NULL, tps80031_irq, IRQF_ONESHOT,
567                                 "tps80031", tps80031);
568         if (!ret) {
569                 device_init_wakeup(tps80031->dev, 1);
570                 enable_irq_wake(irq);
571         }
572
573         return ret;
574 }
575
576 static void tps80031_clk32k_enable(struct tps80031 *tps80031, int base_add)
577 {
578         int ret;
579         ret = tps80031_update(tps80031->dev, SLAVE_ID1,
580                         base_add + EXT_CONTROL_CFG_STATE, STATE_ON, STATE_MASK);
581         if (!ret)
582                 ret = tps80031_update(tps80031->dev, SLAVE_ID1,
583                                 base_add + EXT_CONTROL_CFG_TRANS,
584                                 STATE_ON, STATE_MASK);
585         if (ret < 0)
586                 dev_err(tps80031->dev, "Error in updating clock register\n");
587 }
588
589 static void tps80031_clk32k_init(struct tps80031 *tps80031,
590                         struct tps80031_platform_data *pdata)
591 {
592         struct tps80031_32kclock_plat_data *clk32k_pdata;
593
594         if (!(pdata && pdata->clk32k_pdata))
595                 return;
596
597         clk32k_pdata = pdata->clk32k_pdata;
598         if (clk32k_pdata->en_clk32kao)
599                 tps80031_clk32k_enable(tps80031, CLK32KAO_BASE_ADD);
600
601         if (clk32k_pdata->en_clk32kg)
602                 tps80031_clk32k_enable(tps80031, CLK32KG_BASE_ADD);
603
604         if (clk32k_pdata->en_clk32kaudio)
605                 tps80031_clk32k_enable(tps80031, CLK32KAUDIO_BASE_ADD);
606 }
607
608 static int __devinit tps80031_add_subdevs(struct tps80031 *tps80031,
609                                           struct tps80031_platform_data *pdata)
610 {
611         struct tps80031_subdev_info *subdev;
612         struct platform_device *pdev;
613         int i, ret = 0;
614
615         for (i = 0; i < pdata->num_subdevs; i++) {
616                 subdev = &pdata->subdevs[i];
617
618                 pdev = platform_device_alloc(subdev->name, subdev->id);
619
620                 pdev->dev.parent = tps80031->dev;
621                 pdev->dev.platform_data = subdev->platform_data;
622
623                 ret = platform_device_add(pdev);
624                 if (ret)
625                         goto failed;
626         }
627         return 0;
628
629 failed:
630         tps80031_remove_subdevs(tps80031);
631         return ret;
632 }
633
634 #ifdef CONFIG_DEBUG_FS
635 #include <linux/debugfs.h>
636 #include <linux/seq_file.h>
637 static void print_regs(const char *header, struct seq_file *s,
638                 int sid, int start_offset, int end_offset)
639 {
640         struct tps80031 *tps80031 = s->private;
641         struct tps80031_client *tps = &tps80031->tps_clients[sid];
642         uint8_t reg_val;
643         int i;
644         int ret;
645
646         seq_printf(s, "%s\n", header);
647         for (i = start_offset; i <= end_offset; ++i) {
648                 ret = __tps80031_read(tps->client, i, &reg_val);
649                 if (ret >= 0)
650                         seq_printf(s, "Addr = 0x%02x Reg 0x%02x Value 0x%02x\n",
651                                                 tps->client->addr, i, reg_val);
652         }
653         seq_printf(s, "------------------\n");
654 }
655
656 static int dbg_tps_show(struct seq_file *s, void *unused)
657 {
658         seq_printf(s, "TPS80031 Registers\n");
659         seq_printf(s, "------------------\n");
660         print_regs("VIO Regs",       s, SLAVE_ID1, 0x47, 0x49);
661         print_regs("VIO Regs",       s, SLAVE_ID0, 0x49, 0x4A);
662         print_regs("SMPS1 Regs",     s, SLAVE_ID1, 0x53, 0x54);
663         print_regs("SMPS1 Regs",     s, SLAVE_ID0, 0x55, 0x56);
664         print_regs("SMPS1 Regs",     s, SLAVE_ID1, 0x57, 0x57);
665         print_regs("SMPS2 Regs",     s, SLAVE_ID1, 0x59, 0x5B);
666         print_regs("SMPS2 Regs",     s, SLAVE_ID0, 0x5B, 0x5C);
667         print_regs("SMPS2 Regs",     s, SLAVE_ID1, 0x5C, 0x5D);
668         print_regs("SMPS3 Regs",     s, SLAVE_ID1, 0x65, 0x68);
669         print_regs("SMPS4 Regs",     s, SLAVE_ID1, 0x41, 0x44);
670         print_regs("VANA Regs",      s, SLAVE_ID1, 0x81, 0x83);
671         print_regs("VRTC Regs",      s, SLAVE_ID1, 0xC3, 0xC4);
672         print_regs("LDO1 Regs",      s, SLAVE_ID1, 0x9D, 0x9F);
673         print_regs("LDO2 Regs",      s, SLAVE_ID1, 0x85, 0x87);
674         print_regs("LDO3 Regs",      s, SLAVE_ID1, 0x8D, 0x8F);
675         print_regs("LDO4 Regs",      s, SLAVE_ID1, 0x89, 0x8B);
676         print_regs("LDO5 Regs",      s, SLAVE_ID1, 0x99, 0x9B);
677         print_regs("LDO6 Regs",      s, SLAVE_ID1, 0x91, 0x93);
678         print_regs("LDO7 Regs",      s, SLAVE_ID1, 0xA5, 0xA7);
679         print_regs("LDOUSB Regs",    s, SLAVE_ID1, 0xA1, 0xA3);
680         print_regs("LDOLN Regs",     s, SLAVE_ID1, 0x95, 0x97);
681         print_regs("REGEN1 Regs",    s, SLAVE_ID1, 0xAE, 0xAF);
682         print_regs("REGEN2 Regs",    s, SLAVE_ID1, 0xB1, 0xB2);
683         print_regs("SYSEN Regs",     s, SLAVE_ID1, 0xB4, 0xB5);
684         print_regs("CLK32KAO Regs",  s, SLAVE_ID1, 0xBA, 0xBB);
685         print_regs("CLK32KG Regs",   s, SLAVE_ID1, 0xBD, 0xBE);
686         print_regs("CLK32KAUD Regs", s, SLAVE_ID1, 0xC0, 0xC1);
687         print_regs("INT Regs",       s, SLAVE_ID2, 0xD0, 0xD8);
688         print_regs("VERNUM Regs",    s, SLAVE_ID1, 0x87, 0x87);
689         return 0;
690 }
691
692 static int dbg_tps_open(struct inode *inode, struct file *file)
693 {
694         return single_open(file, dbg_tps_show, inode->i_private);
695 }
696
697 static const struct file_operations debug_fops = {
698         .open           = dbg_tps_open,
699         .read           = seq_read,
700         .llseek         = seq_lseek,
701         .release        = single_release,
702 };
703
704 static void __init tps80031_debuginit(struct tps80031 *tps)
705 {
706         (void)debugfs_create_file("tps80031", S_IRUGO, NULL,
707                         tps, &debug_fops);
708 }
709 #else
710 static void __init tps80031_debuginit(struct tps80031 *tpsi)
711 {
712         return;
713 }
714 #endif
715
716 static int __devexit tps80031_i2c_remove(struct i2c_client *client)
717 {
718         struct tps80031 *tps80031 = i2c_get_clientdata(client);
719         int i;
720
721         if (client->irq)
722                 free_irq(client->irq, tps80031);
723
724         if (tps80031->gpio.owner != NULL)
725                 if (gpiochip_remove(&tps80031->gpio) < 0)
726                         dev_err(&client->dev, "Error in removing the gpio driver\n");
727
728         for (i = 0; i < TPS_NUM_SLAVES; i++) {
729                 struct tps80031_client *tps = &tps80031->tps_clients[i];
730                 if (tps->client && tps->client != client)
731                         i2c_unregister_device(tps->client);
732                 tps80031->tps_clients[i].client = NULL;
733                 mutex_destroy(&tps->lock);
734         }
735
736         kfree(tps80031);
737         return 0;
738 }
739
740 static int __devinit tps80031_i2c_probe(struct i2c_client *client,
741                                         const struct i2c_device_id *id)
742 {
743         struct tps80031_platform_data *pdata = client->dev.platform_data;
744         struct tps80031 *tps80031;
745         struct tps80031_client *tps;
746         int ret;
747         int i;
748
749         if (!pdata) {
750                 dev_err(&client->dev, "tps80031 requires platform data\n");
751                 return -ENOTSUPP;
752         }
753
754         ret = i2c_smbus_read_byte_data(client, TPS80031_JTAGVERNUM);
755         if (ret < 0) {
756                 dev_err(&client->dev, "Silicon version number read"
757                                 " failed: %d\n", ret);
758                 return -EIO;
759         }
760
761         dev_info(&client->dev, "VERNUM is %02x\n", ret);
762
763         tps80031 = kzalloc(sizeof(struct tps80031), GFP_KERNEL);
764         if (tps80031 == NULL)
765                 return -ENOMEM;
766
767         tps80031->dev = &client->dev;
768         i2c_set_clientdata(client, tps80031);
769
770         /* Set up slaves */
771         tps80031->tps_clients[SLAVE_ID0].addr = I2C_ID0_ADDR;
772         tps80031->tps_clients[SLAVE_ID1].addr = I2C_ID1_ADDR;
773         tps80031->tps_clients[SLAVE_ID2].addr = I2C_ID2_ADDR;
774         tps80031->tps_clients[SLAVE_ID3].addr = I2C_ID3_ADDR;
775         for (i = 0; i < TPS_NUM_SLAVES; i++) {
776                 tps = &tps80031->tps_clients[i];
777                 if (tps->addr == client->addr)
778                         tps->client = client;
779                 else
780                         tps->client = i2c_new_dummy(client->adapter,
781                                                 tps->addr);
782                 if (!tps->client) {
783                         dev_err(&client->dev, "can't attach client %d\n", i);
784                         ret = -ENOMEM;
785                         goto fail;
786                 }
787                 i2c_set_clientdata(tps->client, tps80031);
788                 mutex_init(&tps->lock);
789         }
790
791         if (client->irq) {
792                 ret = tps80031_irq_init(tps80031, client->irq,
793                                         pdata->irq_base);
794                 if (ret) {
795                         dev_err(&client->dev, "IRQ init failed: %d\n", ret);
796                         goto fail;
797                 }
798         }
799
800         ret = tps80031_add_subdevs(tps80031, pdata);
801         if (ret) {
802                 dev_err(&client->dev, "add devices failed: %d\n", ret);
803                 goto fail;
804         }
805
806         tps80031_gpio_init(tps80031, pdata);
807
808         tps80031_clk32k_init(tps80031, pdata);
809
810         tps80031_debuginit(tps80031);
811
812         tps80031_dev = tps80031;
813
814         return 0;
815
816 fail:
817         tps80031_i2c_remove(client);
818         return ret;
819 }
820
821 #ifdef CONFIG_PM
822 static int tps80031_i2c_suspend(struct i2c_client *client, pm_message_t state)
823 {
824         if (client->irq)
825                 disable_irq(client->irq);
826         return 0;
827 }
828
829 static int tps80031_i2c_resume(struct i2c_client *client)
830 {
831         if (client->irq)
832                 enable_irq(client->irq);
833         return 0;
834 }
835 #endif
836
837
838 static const struct i2c_device_id tps80031_id_table[] = {
839         { "tps80031", 0 },
840         { },
841 };
842 MODULE_DEVICE_TABLE(i2c, tps80031_id_table);
843
844 static struct i2c_driver tps80031_driver = {
845         .driver = {
846                 .name   = "tps80031",
847                 .owner  = THIS_MODULE,
848         },
849         .probe          = tps80031_i2c_probe,
850         .remove         = __devexit_p(tps80031_i2c_remove),
851 #ifdef CONFIG_PM
852         .suspend        = tps80031_i2c_suspend,
853         .resume         = tps80031_i2c_resume,
854 #endif
855         .id_table       = tps80031_id_table,
856 };
857
858 static int __init tps80031_init(void)
859 {
860         return i2c_add_driver(&tps80031_driver);
861 }
862 subsys_initcall(tps80031_init);
863
864 static void __exit tps80031_exit(void)
865 {
866         i2c_del_driver(&tps80031_driver);
867 }
868 module_exit(tps80031_exit);
869
870 MODULE_DESCRIPTION("TPS80031 core driver");
871 MODULE_LICENSE("GPL");