]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/mfd/twl-core.c
mfd: twl-core: Support for proper PWM drivers
[karo-tx-linux.git] / drivers / mfd / twl-core.c
1 /*
2  * twl_core.c - driver for TWL4030/TWL5030/TWL60X0/TPS659x0 PM
3  * and audio CODEC devices
4  *
5  * Copyright (C) 2005-2006 Texas Instruments, Inc.
6  *
7  * Modifications to defer interrupt handling to a kernel thread:
8  * Copyright (C) 2006 MontaVista Software, Inc.
9  *
10  * Based on tlv320aic23.c:
11  * Copyright (c) by Kai Svahn <kai.svahn@nokia.com>
12  *
13  * Code cleanup and modifications to IRQ handler.
14  * by syed khasim <x0khasim@ti.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
29  */
30
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/module.h>
34 #include <linux/platform_device.h>
35 #include <linux/clk.h>
36 #include <linux/err.h>
37 #include <linux/device.h>
38 #include <linux/of.h>
39 #include <linux/of_irq.h>
40 #include <linux/of_platform.h>
41 #include <linux/irq.h>
42 #include <linux/irqdomain.h>
43
44 #include <linux/regulator/machine.h>
45
46 #include <linux/i2c.h>
47 #include <linux/i2c/twl.h>
48
49 #include "twl-core.h"
50
51 /*
52  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
53  * Management and System Companion Device" chips originally designed for
54  * use in OMAP2 and OMAP 3 based systems.  Its control interfaces use I2C,
55  * often at around 3 Mbit/sec, including for interrupt handling.
56  *
57  * This driver core provides genirq support for the interrupts emitted,
58  * by the various modules, and exports register access primitives.
59  *
60  * FIXME this driver currently requires use of the first interrupt line
61  * (and associated registers).
62  */
63
64 #define DRIVER_NAME                     "twl"
65
66 /* Triton Core internal information (BEGIN) */
67
68 /* Last - for index max*/
69 #define TWL4030_MODULE_LAST             TWL4030_MODULE_SECURED_REG
70
71 #define TWL_NUM_SLAVES          4
72
73 #define SUB_CHIP_ID0 0
74 #define SUB_CHIP_ID1 1
75 #define SUB_CHIP_ID2 2
76 #define SUB_CHIP_ID3 3
77 #define SUB_CHIP_ID_INVAL 0xff
78
79 #define TWL_MODULE_LAST TWL4030_MODULE_LAST
80
81 /* Base Address defns for twl4030_map[] */
82
83 /* subchip/slave 0 - USB ID */
84 #define TWL4030_BASEADD_USB             0x0000
85
86 /* subchip/slave 1 - AUD ID */
87 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
88 #define TWL4030_BASEADD_GPIO            0x0098
89 #define TWL4030_BASEADD_INTBR           0x0085
90 #define TWL4030_BASEADD_PIH             0x0080
91 #define TWL4030_BASEADD_TEST            0x004C
92
93 /* subchip/slave 2 - AUX ID */
94 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
95 #define TWL4030_BASEADD_LED             0x00EE
96 #define TWL4030_BASEADD_MADC            0x0000
97 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
98 #define TWL4030_BASEADD_PRECHARGE       0x00AA
99 #define TWL4030_BASEADD_PWM0            0x00F8
100 #define TWL4030_BASEADD_PWM1            0x00FB
101 #define TWL4030_BASEADD_PWMA            0x00EF
102 #define TWL4030_BASEADD_PWMB            0x00F1
103 #define TWL4030_BASEADD_KEYPAD          0x00D2
104
105 #define TWL5031_BASEADD_ACCESSORY       0x0074 /* Replaces Main Charge */
106 #define TWL5031_BASEADD_INTERRUPTS      0x00B9 /* Different than TWL4030's
107                                                   one */
108
109 /* subchip/slave 3 - POWER ID */
110 #define TWL4030_BASEADD_BACKUP          0x0014
111 #define TWL4030_BASEADD_INT             0x002E
112 #define TWL4030_BASEADD_PM_MASTER       0x0036
113 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
114 #define TWL4030_BASEADD_RTC             0x001C
115 #define TWL4030_BASEADD_SECURED_REG     0x0000
116
117 /* Triton Core internal information (END) */
118
119
120 /* subchip/slave 0 0x48 - POWER */
121 #define TWL6030_BASEADD_RTC             0x0000
122 #define TWL6030_BASEADD_MEM             0x0017
123 #define TWL6030_BASEADD_PM_MASTER       0x001F
124 #define TWL6030_BASEADD_PM_SLAVE_MISC   0x0030 /* PM_RECEIVER */
125 #define TWL6030_BASEADD_PM_MISC         0x00E2
126 #define TWL6030_BASEADD_PM_PUPD         0x00F0
127
128 /* subchip/slave 1 0x49 - FEATURE */
129 #define TWL6030_BASEADD_USB             0x0000
130 #define TWL6030_BASEADD_GPADC_CTRL      0x002E
131 #define TWL6030_BASEADD_AUX             0x0090
132 #define TWL6030_BASEADD_PWM             0x00BA
133 #define TWL6030_BASEADD_GASGAUGE        0x00C0
134 #define TWL6030_BASEADD_PIH             0x00D0
135 #define TWL6030_BASEADD_CHARGER         0x00E0
136 #define TWL6025_BASEADD_CHARGER         0x00DA
137
138 /* subchip/slave 2 0x4A - DFT */
139 #define TWL6030_BASEADD_DIEID           0x00C0
140
141 /* subchip/slave 3 0x4B - AUDIO */
142 #define TWL6030_BASEADD_AUDIO           0x0000
143 #define TWL6030_BASEADD_RSV             0x0000
144 #define TWL6030_BASEADD_ZERO            0x0000
145
146 /* Few power values */
147 #define R_CFG_BOOT                      0x05
148
149 /* some fields in R_CFG_BOOT */
150 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
151 #define HFCLK_FREQ_26_MHZ               (2 << 0)
152 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
153 #define HIGH_PERF_SQ                    (1 << 3)
154 #define CK32K_LOWPWR_EN                 (1 << 7)
155
156 /*----------------------------------------------------------------------*/
157
158 /* is driver active, bound to a chip? */
159 static bool inuse;
160
161 /* TWL IDCODE Register value */
162 static u32 twl_idcode;
163
164 static unsigned int twl_id;
165 unsigned int twl_rev(void)
166 {
167         return twl_id;
168 }
169 EXPORT_SYMBOL(twl_rev);
170
171 /* Structure for each TWL4030/TWL6030 Slave */
172 struct twl_client {
173         struct i2c_client *client;
174         u8 address;
175
176         /* max numb of i2c_msg required is for read =2 */
177         struct i2c_msg xfer_msg[2];
178
179         /* To lock access to xfer_msg */
180         struct mutex xfer_lock;
181 };
182
183 static struct twl_client twl_modules[TWL_NUM_SLAVES];
184
185 /* mapping the module id to slave id and base address */
186 struct twl_mapping {
187         unsigned char sid;      /* Slave ID */
188         unsigned char base;     /* base address */
189 };
190 static struct twl_mapping *twl_map;
191
192 static struct twl_mapping twl4030_map[TWL4030_MODULE_LAST + 1] = {
193         /*
194          * NOTE:  don't change this table without updating the
195          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
196          * so they continue to match the order in this table.
197          */
198
199         { 0, TWL4030_BASEADD_USB },
200
201         { 1, TWL4030_BASEADD_AUDIO_VOICE },
202         { 1, TWL4030_BASEADD_GPIO },
203         { 1, TWL4030_BASEADD_INTBR },
204         { 1, TWL4030_BASEADD_PIH },
205         { 1, TWL4030_BASEADD_TEST },
206
207         { 2, TWL4030_BASEADD_KEYPAD },
208         { 2, TWL4030_BASEADD_MADC },
209         { 2, TWL4030_BASEADD_INTERRUPTS },
210         { 2, TWL4030_BASEADD_LED },
211         { 2, TWL4030_BASEADD_MAIN_CHARGE },
212         { 2, TWL4030_BASEADD_PRECHARGE },
213         { 2, TWL4030_BASEADD_PWM0 },
214         { 2, TWL4030_BASEADD_PWM1 },
215         { 2, TWL4030_BASEADD_PWMA },
216         { 2, TWL4030_BASEADD_PWMB },
217         { 2, TWL5031_BASEADD_ACCESSORY },
218         { 2, TWL5031_BASEADD_INTERRUPTS },
219
220         { 3, TWL4030_BASEADD_BACKUP },
221         { 3, TWL4030_BASEADD_INT },
222         { 3, TWL4030_BASEADD_PM_MASTER },
223         { 3, TWL4030_BASEADD_PM_RECEIVER },
224         { 3, TWL4030_BASEADD_RTC },
225         { 3, TWL4030_BASEADD_SECURED_REG },
226 };
227
228 static struct twl_mapping twl6030_map[] = {
229         /*
230          * NOTE:  don't change this table without updating the
231          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
232          * so they continue to match the order in this table.
233          */
234         { SUB_CHIP_ID1, TWL6030_BASEADD_USB },
235         { SUB_CHIP_ID_INVAL, TWL6030_BASEADD_AUDIO },
236         { SUB_CHIP_ID2, TWL6030_BASEADD_DIEID },
237         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
238         { SUB_CHIP_ID1, TWL6030_BASEADD_PIH },
239
240         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
241         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
242         { SUB_CHIP_ID1, TWL6030_BASEADD_GPADC_CTRL },
243         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
244         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
245
246         { SUB_CHIP_ID1, TWL6030_BASEADD_CHARGER },
247         { SUB_CHIP_ID1, TWL6030_BASEADD_GASGAUGE },
248         { SUB_CHIP_ID1, TWL6030_BASEADD_PWM },
249         { SUB_CHIP_ID0, TWL6030_BASEADD_ZERO },
250         { SUB_CHIP_ID1, TWL6030_BASEADD_ZERO },
251
252         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
253         { SUB_CHIP_ID2, TWL6030_BASEADD_ZERO },
254         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
255         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
256         { SUB_CHIP_ID2, TWL6030_BASEADD_RSV },
257         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_MASTER },
258         { SUB_CHIP_ID0, TWL6030_BASEADD_PM_SLAVE_MISC },
259
260         { SUB_CHIP_ID0, TWL6030_BASEADD_RTC },
261         { SUB_CHIP_ID0, TWL6030_BASEADD_MEM },
262         { SUB_CHIP_ID1, TWL6025_BASEADD_CHARGER },
263 };
264
265 /*----------------------------------------------------------------------*/
266
267 /* Exported Functions */
268
269 /**
270  * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
271  * @mod_no: module number
272  * @value: an array of num_bytes+1 containing data to write
273  * @reg: register address (just offset will do)
274  * @num_bytes: number of bytes to transfer
275  *
276  * IMPORTANT: for 'value' parameter: Allocate value num_bytes+1 and
277  * valid data starts at Offset 1.
278  *
279  * Returns the result of operation - 0 is success
280  */
281 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
282 {
283         int ret;
284         int sid;
285         struct twl_client *twl;
286         struct i2c_msg *msg;
287
288         if (unlikely(mod_no > TWL_MODULE_LAST)) {
289                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
290                 return -EPERM;
291         }
292         if (unlikely(!inuse)) {
293                 pr_err("%s: not initialized\n", DRIVER_NAME);
294                 return -EPERM;
295         }
296         sid = twl_map[mod_no].sid;
297         if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
298                 pr_err("%s: module %d is not part of the pmic\n",
299                        DRIVER_NAME, mod_no);
300                 return -EINVAL;
301         }
302         twl = &twl_modules[sid];
303
304         mutex_lock(&twl->xfer_lock);
305         /*
306          * [MSG1]: fill the register address data
307          * fill the data Tx buffer
308          */
309         msg = &twl->xfer_msg[0];
310         msg->addr = twl->address;
311         msg->len = num_bytes + 1;
312         msg->flags = 0;
313         msg->buf = value;
314         /* over write the first byte of buffer with the register address */
315         *value = twl_map[mod_no].base + reg;
316         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 1);
317         mutex_unlock(&twl->xfer_lock);
318
319         /* i2c_transfer returns number of messages transferred */
320         if (ret != 1) {
321                 pr_err("%s: i2c_write failed to transfer all messages\n",
322                         DRIVER_NAME);
323                 if (ret < 0)
324                         return ret;
325                 else
326                         return -EIO;
327         } else {
328                 return 0;
329         }
330 }
331 EXPORT_SYMBOL(twl_i2c_write);
332
333 /**
334  * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
335  * @mod_no: module number
336  * @value: an array of num_bytes containing data to be read
337  * @reg: register address (just offset will do)
338  * @num_bytes: number of bytes to transfer
339  *
340  * Returns result of operation - num_bytes is success else failure.
341  */
342 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
343 {
344         int ret;
345         u8 val;
346         int sid;
347         struct twl_client *twl;
348         struct i2c_msg *msg;
349
350         if (unlikely(mod_no > TWL_MODULE_LAST)) {
351                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
352                 return -EPERM;
353         }
354         if (unlikely(!inuse)) {
355                 pr_err("%s: not initialized\n", DRIVER_NAME);
356                 return -EPERM;
357         }
358         sid = twl_map[mod_no].sid;
359         if (unlikely(sid == SUB_CHIP_ID_INVAL)) {
360                 pr_err("%s: module %d is not part of the pmic\n",
361                        DRIVER_NAME, mod_no);
362                 return -EINVAL;
363         }
364         twl = &twl_modules[sid];
365
366         mutex_lock(&twl->xfer_lock);
367         /* [MSG1] fill the register address data */
368         msg = &twl->xfer_msg[0];
369         msg->addr = twl->address;
370         msg->len = 1;
371         msg->flags = 0; /* Read the register value */
372         val = twl_map[mod_no].base + reg;
373         msg->buf = &val;
374         /* [MSG2] fill the data rx buffer */
375         msg = &twl->xfer_msg[1];
376         msg->addr = twl->address;
377         msg->flags = I2C_M_RD;  /* Read the register value */
378         msg->len = num_bytes;   /* only n bytes */
379         msg->buf = value;
380         ret = i2c_transfer(twl->client->adapter, twl->xfer_msg, 2);
381         mutex_unlock(&twl->xfer_lock);
382
383         /* i2c_transfer returns number of messages transferred */
384         if (ret != 2) {
385                 pr_err("%s: i2c_read failed to transfer all messages\n",
386                         DRIVER_NAME);
387                 if (ret < 0)
388                         return ret;
389                 else
390                         return -EIO;
391         } else {
392                 return 0;
393         }
394 }
395 EXPORT_SYMBOL(twl_i2c_read);
396
397 /**
398  * twl_i2c_write_u8 - Writes a 8 bit register in TWL4030/TWL5030/TWL60X0
399  * @mod_no: module number
400  * @value: the value to be written 8 bit
401  * @reg: register address (just offset will do)
402  *
403  * Returns result of operation - 0 is success
404  */
405 int twl_i2c_write_u8(u8 mod_no, u8 value, u8 reg)
406 {
407
408         /* 2 bytes offset 1 contains the data offset 0 is used by i2c_write */
409         u8 temp_buffer[2] = { 0 };
410         /* offset 1 contains the data */
411         temp_buffer[1] = value;
412         return twl_i2c_write(mod_no, temp_buffer, reg, 1);
413 }
414 EXPORT_SYMBOL(twl_i2c_write_u8);
415
416 /**
417  * twl_i2c_read_u8 - Reads a 8 bit register from TWL4030/TWL5030/TWL60X0
418  * @mod_no: module number
419  * @value: the value read 8 bit
420  * @reg: register address (just offset will do)
421  *
422  * Returns result of operation - 0 is success
423  */
424 int twl_i2c_read_u8(u8 mod_no, u8 *value, u8 reg)
425 {
426         return twl_i2c_read(mod_no, value, reg, 1);
427 }
428 EXPORT_SYMBOL(twl_i2c_read_u8);
429
430 /*----------------------------------------------------------------------*/
431
432 /**
433  * twl_read_idcode_register - API to read the IDCODE register.
434  *
435  * Unlocks the IDCODE register and read the 32 bit value.
436  */
437 static int twl_read_idcode_register(void)
438 {
439         int err;
440
441         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
442                                                 REG_UNLOCK_TEST_REG);
443         if (err) {
444                 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
445                 goto fail;
446         }
447
448         err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_idcode),
449                                                 REG_IDCODE_7_0, 4);
450         if (err) {
451                 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
452                 goto fail;
453         }
454
455         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
456         if (err)
457                 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
458 fail:
459         return err;
460 }
461
462 /**
463  * twl_get_type - API to get TWL Si type.
464  *
465  * Api to get the TWL Si type from IDCODE value.
466  */
467 int twl_get_type(void)
468 {
469         return TWL_SIL_TYPE(twl_idcode);
470 }
471 EXPORT_SYMBOL_GPL(twl_get_type);
472
473 /**
474  * twl_get_version - API to get TWL Si version.
475  *
476  * Api to get the TWL Si version from IDCODE value.
477  */
478 int twl_get_version(void)
479 {
480         return TWL_SIL_REV(twl_idcode);
481 }
482 EXPORT_SYMBOL_GPL(twl_get_version);
483
484 /**
485  * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
486  *
487  * Api to get the TWL HFCLK rate based on BOOT_CFG register.
488  */
489 int twl_get_hfclk_rate(void)
490 {
491         u8 ctrl;
492         int rate;
493
494         twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
495
496         switch (ctrl & 0x3) {
497         case HFCLK_FREQ_19p2_MHZ:
498                 rate = 19200000;
499                 break;
500         case HFCLK_FREQ_26_MHZ:
501                 rate = 26000000;
502                 break;
503         case HFCLK_FREQ_38p4_MHZ:
504                 rate = 38400000;
505                 break;
506         default:
507                 pr_err("TWL4030: HFCLK is not configured\n");
508                 rate = -EINVAL;
509                 break;
510         }
511
512         return rate;
513 }
514 EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
515
516 static struct device *
517 add_numbered_child(unsigned chip, const char *name, int num,
518                 void *pdata, unsigned pdata_len,
519                 bool can_wakeup, int irq0, int irq1)
520 {
521         struct platform_device  *pdev;
522         struct twl_client       *twl = &twl_modules[chip];
523         int                     status;
524
525         pdev = platform_device_alloc(name, num);
526         if (!pdev) {
527                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
528                 status = -ENOMEM;
529                 goto err;
530         }
531
532         pdev->dev.parent = &twl->client->dev;
533
534         if (pdata) {
535                 status = platform_device_add_data(pdev, pdata, pdata_len);
536                 if (status < 0) {
537                         dev_dbg(&pdev->dev, "can't add platform_data\n");
538                         goto err;
539                 }
540         }
541
542         if (irq0) {
543                 struct resource r[2] = {
544                         { .start = irq0, .flags = IORESOURCE_IRQ, },
545                         { .start = irq1, .flags = IORESOURCE_IRQ, },
546                 };
547
548                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
549                 if (status < 0) {
550                         dev_dbg(&pdev->dev, "can't add irqs\n");
551                         goto err;
552                 }
553         }
554
555         status = platform_device_add(pdev);
556         if (status == 0)
557                 device_init_wakeup(&pdev->dev, can_wakeup);
558
559 err:
560         if (status < 0) {
561                 platform_device_put(pdev);
562                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
563                 return ERR_PTR(status);
564         }
565         return &pdev->dev;
566 }
567
568 static inline struct device *add_child(unsigned chip, const char *name,
569                 void *pdata, unsigned pdata_len,
570                 bool can_wakeup, int irq0, int irq1)
571 {
572         return add_numbered_child(chip, name, -1, pdata, pdata_len,
573                 can_wakeup, irq0, irq1);
574 }
575
576 static struct device *
577 add_regulator_linked(int num, struct regulator_init_data *pdata,
578                 struct regulator_consumer_supply *consumers,
579                 unsigned num_consumers, unsigned long features)
580 {
581         unsigned sub_chip_id;
582         struct twl_regulator_driver_data drv_data;
583
584         /* regulator framework demands init_data ... */
585         if (!pdata)
586                 return NULL;
587
588         if (consumers) {
589                 pdata->consumer_supplies = consumers;
590                 pdata->num_consumer_supplies = num_consumers;
591         }
592
593         if (pdata->driver_data) {
594                 /* If we have existing drv_data, just add the flags */
595                 struct twl_regulator_driver_data *tmp;
596                 tmp = pdata->driver_data;
597                 tmp->features |= features;
598         } else {
599                 /* add new driver data struct, used only during init */
600                 drv_data.features = features;
601                 drv_data.set_voltage = NULL;
602                 drv_data.get_voltage = NULL;
603                 drv_data.data = NULL;
604                 pdata->driver_data = &drv_data;
605         }
606
607         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
608         sub_chip_id = twl_map[TWL_MODULE_PM_MASTER].sid;
609         return add_numbered_child(sub_chip_id, "twl_reg", num,
610                 pdata, sizeof(*pdata), false, 0, 0);
611 }
612
613 static struct device *
614 add_regulator(int num, struct regulator_init_data *pdata,
615                 unsigned long features)
616 {
617         return add_regulator_linked(num, pdata, NULL, 0, features);
618 }
619
620 /*
621  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
622  * for the PIH, and the next are for the PWR_INT SIH, since
623  * that's how twl_init_irq() sets things up.
624  */
625
626 static int
627 add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
628                 unsigned long features)
629 {
630         struct device   *child;
631         unsigned sub_chip_id;
632
633         if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
634                 child = add_child(SUB_CHIP_ID1, "twl4030_gpio",
635                                 pdata->gpio, sizeof(*pdata->gpio),
636                                 false, irq_base + GPIO_INTR_OFFSET, 0);
637                 if (IS_ERR(child))
638                         return PTR_ERR(child);
639         }
640
641         if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
642                 child = add_child(SUB_CHIP_ID2, "twl4030_keypad",
643                                 pdata->keypad, sizeof(*pdata->keypad),
644                                 true, irq_base + KEYPAD_INTR_OFFSET, 0);
645                 if (IS_ERR(child))
646                         return PTR_ERR(child);
647         }
648
649         if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
650             twl_class_is_4030()) {
651                 child = add_child(2, "twl4030_madc",
652                                 pdata->madc, sizeof(*pdata->madc),
653                                 true, irq_base + MADC_INTR_OFFSET, 0);
654                 if (IS_ERR(child))
655                         return PTR_ERR(child);
656         }
657
658         if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
659                 /*
660                  * REVISIT platform_data here currently might expose the
661                  * "msecure" line ... but for now we just expect board
662                  * setup to tell the chip "it's always ok to SET_TIME".
663                  * Eventually, Linux might become more aware of such
664                  * HW security concerns, and "least privilege".
665                  */
666                 sub_chip_id = twl_map[TWL_MODULE_RTC].sid;
667                 child = add_child(sub_chip_id, "twl_rtc",
668                                 NULL, 0,
669                                 true, irq_base + RTC_INTR_OFFSET, 0);
670                 if (IS_ERR(child))
671                         return PTR_ERR(child);
672         }
673
674         if (IS_ENABLED(CONFIG_PWM_TWL)) {
675                 child = add_child(SUB_CHIP_ID1, "twl-pwm", NULL, 0,
676                                   false, 0, 0);
677                 if (IS_ERR(child))
678                         return PTR_ERR(child);
679         }
680
681         if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
682                 child = add_child(SUB_CHIP_ID1, "twl-pwmled", NULL, 0,
683                                   false, 0, 0);
684                 if (IS_ERR(child))
685                         return PTR_ERR(child);
686         }
687
688         if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
689             twl_class_is_4030()) {
690
691                 static struct regulator_consumer_supply usb1v5 = {
692                         .supply =       "usb1v5",
693                 };
694                 static struct regulator_consumer_supply usb1v8 = {
695                         .supply =       "usb1v8",
696                 };
697                 static struct regulator_consumer_supply usb3v1[] = {
698                         { .supply =     "usb3v1" },
699                         { .supply =     "bci3v1" },
700                 };
701
702         /* First add the regulators so that they can be used by transceiver */
703                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
704                         /* this is a template that gets copied */
705                         struct regulator_init_data usb_fixed = {
706                                 .constraints.valid_modes_mask =
707                                         REGULATOR_MODE_NORMAL
708                                         | REGULATOR_MODE_STANDBY,
709                                 .constraints.valid_ops_mask =
710                                         REGULATOR_CHANGE_MODE
711                                         | REGULATOR_CHANGE_STATUS,
712                         };
713
714                         child = add_regulator_linked(TWL4030_REG_VUSB1V5,
715                                                       &usb_fixed, &usb1v5, 1,
716                                                       features);
717                         if (IS_ERR(child))
718                                 return PTR_ERR(child);
719
720                         child = add_regulator_linked(TWL4030_REG_VUSB1V8,
721                                                       &usb_fixed, &usb1v8, 1,
722                                                       features);
723                         if (IS_ERR(child))
724                                 return PTR_ERR(child);
725
726                         child = add_regulator_linked(TWL4030_REG_VUSB3V1,
727                                                       &usb_fixed, usb3v1, 2,
728                                                       features);
729                         if (IS_ERR(child))
730                                 return PTR_ERR(child);
731
732                 }
733
734                 child = add_child(0, "twl4030_usb",
735                                 pdata->usb, sizeof(*pdata->usb),
736                                 true,
737                                 /* irq0 = USB_PRES, irq1 = USB */
738                                 irq_base + USB_PRES_INTR_OFFSET,
739                                 irq_base + USB_INTR_OFFSET);
740
741                 if (IS_ERR(child))
742                         return PTR_ERR(child);
743
744                 /* we need to connect regulators to this transceiver */
745                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
746                         usb1v5.dev_name = dev_name(child);
747                         usb1v8.dev_name = dev_name(child);
748                         usb3v1[0].dev_name = dev_name(child);
749                 }
750         }
751         if (IS_ENABLED(CONFIG_TWL6030_USB) && pdata->usb &&
752             twl_class_is_6030()) {
753
754                 static struct regulator_consumer_supply usb3v3;
755                 int regulator;
756
757                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
758                         /* this is a template that gets copied */
759                         struct regulator_init_data usb_fixed = {
760                                 .constraints.valid_modes_mask =
761                                         REGULATOR_MODE_NORMAL
762                                         | REGULATOR_MODE_STANDBY,
763                                 .constraints.valid_ops_mask =
764                                         REGULATOR_CHANGE_MODE
765                                         | REGULATOR_CHANGE_STATUS,
766                         };
767
768                         if (features & TWL6025_SUBCLASS) {
769                                 usb3v3.supply = "ldousb";
770                                 regulator = TWL6025_REG_LDOUSB;
771                         } else {
772                                 usb3v3.supply = "vusb";
773                                 regulator = TWL6030_REG_VUSB;
774                         }
775                         child = add_regulator_linked(regulator, &usb_fixed,
776                                                         &usb3v3, 1,
777                                                         features);
778                         if (IS_ERR(child))
779                                 return PTR_ERR(child);
780                 }
781
782                 pdata->usb->features = features;
783
784                 child = add_child(0, "twl6030_usb",
785                         pdata->usb, sizeof(*pdata->usb),
786                         true,
787                         /* irq1 = VBUS_PRES, irq0 = USB ID */
788                         irq_base + USBOTG_INTR_OFFSET,
789                         irq_base + USB_PRES_INTR_OFFSET);
790
791                 if (IS_ERR(child))
792                         return PTR_ERR(child);
793                 /* we need to connect regulators to this transceiver */
794                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child)
795                         usb3v3.dev_name = dev_name(child);
796         } else if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) &&
797                    twl_class_is_6030()) {
798                 if (features & TWL6025_SUBCLASS)
799                         child = add_regulator(TWL6025_REG_LDOUSB,
800                                                 pdata->ldousb, features);
801                 else
802                         child = add_regulator(TWL6030_REG_VUSB,
803                                                 pdata->vusb, features);
804
805                         if (IS_ERR(child))
806                                         return PTR_ERR(child);
807         }
808
809         if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
810                 child = add_child(0, "twl4030_wdt", NULL, 0, false, 0, 0);
811                 if (IS_ERR(child))
812                         return PTR_ERR(child);
813         }
814
815         if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
816                 child = add_child(1, "twl4030_pwrbutton",
817                                 NULL, 0, true, irq_base + 8 + 0, 0);
818                 if (IS_ERR(child))
819                         return PTR_ERR(child);
820         }
821
822         if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
823             twl_class_is_4030()) {
824                 sub_chip_id = twl_map[TWL_MODULE_AUDIO_VOICE].sid;
825                 child = add_child(sub_chip_id, "twl4030-audio",
826                                 pdata->audio, sizeof(*pdata->audio),
827                                 false, 0, 0);
828                 if (IS_ERR(child))
829                         return PTR_ERR(child);
830         }
831
832         /* twl4030 regulators */
833         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
834                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
835                                         features);
836                 if (IS_ERR(child))
837                         return PTR_ERR(child);
838
839                 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
840                                         features);
841                 if (IS_ERR(child))
842                         return PTR_ERR(child);
843
844                 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
845                                         features);
846                 if (IS_ERR(child))
847                         return PTR_ERR(child);
848
849                 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
850                                         features);
851                 if (IS_ERR(child))
852                         return PTR_ERR(child);
853
854                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
855                                         features);
856                 if (IS_ERR(child))
857                         return PTR_ERR(child);
858
859                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
860                                         features);
861                 if (IS_ERR(child))
862                         return PTR_ERR(child);
863
864                 child = add_regulator((features & TWL4030_VAUX2)
865                                         ? TWL4030_REG_VAUX2_4030
866                                         : TWL4030_REG_VAUX2,
867                                 pdata->vaux2, features);
868                 if (IS_ERR(child))
869                         return PTR_ERR(child);
870
871                 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
872                                         features);
873                 if (IS_ERR(child))
874                         return PTR_ERR(child);
875
876                 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
877                                         features);
878                 if (IS_ERR(child))
879                         return PTR_ERR(child);
880
881                 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
882                                         features);
883                 if (IS_ERR(child))
884                         return PTR_ERR(child);
885         }
886
887         /* maybe add LDOs that are omitted on cost-reduced parts */
888         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
889           && twl_class_is_4030()) {
890                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
891                                         features);
892                 if (IS_ERR(child))
893                         return PTR_ERR(child);
894
895                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
896                                         features);
897                 if (IS_ERR(child))
898                         return PTR_ERR(child);
899
900                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
901                                         features);
902                 if (IS_ERR(child))
903                         return PTR_ERR(child);
904
905                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
906                                         features);
907                 if (IS_ERR(child))
908                         return PTR_ERR(child);
909
910                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
911                                         features);
912                 if (IS_ERR(child))
913                         return PTR_ERR(child);
914
915                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
916                                         features);
917                 if (IS_ERR(child))
918                         return PTR_ERR(child);
919         }
920
921         /* twl6030 regulators */
922         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
923                         !(features & TWL6025_SUBCLASS)) {
924                 child = add_regulator(TWL6030_REG_VDD1, pdata->vdd1,
925                                         features);
926                 if (IS_ERR(child))
927                         return PTR_ERR(child);
928
929                 child = add_regulator(TWL6030_REG_VDD2, pdata->vdd2,
930                                         features);
931                 if (IS_ERR(child))
932                         return PTR_ERR(child);
933
934                 child = add_regulator(TWL6030_REG_VDD3, pdata->vdd3,
935                                         features);
936                 if (IS_ERR(child))
937                         return PTR_ERR(child);
938
939                 child = add_regulator(TWL6030_REG_V1V8, pdata->v1v8,
940                                         features);
941                 if (IS_ERR(child))
942                         return PTR_ERR(child);
943
944                 child = add_regulator(TWL6030_REG_V2V1, pdata->v2v1,
945                                         features);
946                 if (IS_ERR(child))
947                         return PTR_ERR(child);
948
949                 child = add_regulator(TWL6030_REG_VMMC, pdata->vmmc,
950                                         features);
951                 if (IS_ERR(child))
952                         return PTR_ERR(child);
953
954                 child = add_regulator(TWL6030_REG_VPP, pdata->vpp,
955                                         features);
956                 if (IS_ERR(child))
957                         return PTR_ERR(child);
958
959                 child = add_regulator(TWL6030_REG_VUSIM, pdata->vusim,
960                                         features);
961                 if (IS_ERR(child))
962                         return PTR_ERR(child);
963
964                 child = add_regulator(TWL6030_REG_VCXIO, pdata->vcxio,
965                                         features);
966                 if (IS_ERR(child))
967                         return PTR_ERR(child);
968
969                 child = add_regulator(TWL6030_REG_VDAC, pdata->vdac,
970                                         features);
971                 if (IS_ERR(child))
972                         return PTR_ERR(child);
973
974                 child = add_regulator(TWL6030_REG_VAUX1_6030, pdata->vaux1,
975                                         features);
976                 if (IS_ERR(child))
977                         return PTR_ERR(child);
978
979                 child = add_regulator(TWL6030_REG_VAUX2_6030, pdata->vaux2,
980                                         features);
981                 if (IS_ERR(child))
982                         return PTR_ERR(child);
983
984                 child = add_regulator(TWL6030_REG_VAUX3_6030, pdata->vaux3,
985                                         features);
986                 if (IS_ERR(child))
987                         return PTR_ERR(child);
988
989                 child = add_regulator(TWL6030_REG_CLK32KG, pdata->clk32kg,
990                                         features);
991                 if (IS_ERR(child))
992                         return PTR_ERR(child);
993         }
994
995         /* 6030 and 6025 share this regulator */
996         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030()) {
997                 child = add_regulator(TWL6030_REG_VANA, pdata->vana,
998                                         features);
999                 if (IS_ERR(child))
1000                         return PTR_ERR(child);
1001         }
1002
1003         /* twl6025 regulators */
1004         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_6030() &&
1005                         (features & TWL6025_SUBCLASS)) {
1006                 child = add_regulator(TWL6025_REG_LDO5, pdata->ldo5,
1007                                         features);
1008                 if (IS_ERR(child))
1009                         return PTR_ERR(child);
1010
1011                 child = add_regulator(TWL6025_REG_LDO1, pdata->ldo1,
1012                                         features);
1013                 if (IS_ERR(child))
1014                         return PTR_ERR(child);
1015
1016                 child = add_regulator(TWL6025_REG_LDO7, pdata->ldo7,
1017                                         features);
1018                 if (IS_ERR(child))
1019                         return PTR_ERR(child);
1020
1021                 child = add_regulator(TWL6025_REG_LDO6, pdata->ldo6,
1022                                         features);
1023                 if (IS_ERR(child))
1024                         return PTR_ERR(child);
1025
1026                 child = add_regulator(TWL6025_REG_LDOLN, pdata->ldoln,
1027                                         features);
1028                 if (IS_ERR(child))
1029                         return PTR_ERR(child);
1030
1031                 child = add_regulator(TWL6025_REG_LDO2, pdata->ldo2,
1032                                         features);
1033                 if (IS_ERR(child))
1034                         return PTR_ERR(child);
1035
1036                 child = add_regulator(TWL6025_REG_LDO4, pdata->ldo4,
1037                                         features);
1038                 if (IS_ERR(child))
1039                         return PTR_ERR(child);
1040
1041                 child = add_regulator(TWL6025_REG_LDO3, pdata->ldo3,
1042                                         features);
1043                 if (IS_ERR(child))
1044                         return PTR_ERR(child);
1045
1046                 child = add_regulator(TWL6025_REG_SMPS3, pdata->smps3,
1047                                         features);
1048                 if (IS_ERR(child))
1049                         return PTR_ERR(child);
1050
1051                 child = add_regulator(TWL6025_REG_SMPS4, pdata->smps4,
1052                                         features);
1053                 if (IS_ERR(child))
1054                         return PTR_ERR(child);
1055
1056                 child = add_regulator(TWL6025_REG_VIO, pdata->vio6025,
1057                                         features);
1058                 if (IS_ERR(child))
1059                         return PTR_ERR(child);
1060
1061         }
1062
1063         if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
1064                         !(features & (TPS_SUBSET | TWL5031))) {
1065                 child = add_child(3, "twl4030_bci",
1066                                 pdata->bci, sizeof(*pdata->bci), false,
1067                                 /* irq0 = CHG_PRES, irq1 = BCI */
1068                                 irq_base + BCI_PRES_INTR_OFFSET,
1069                                 irq_base + BCI_INTR_OFFSET);
1070                 if (IS_ERR(child))
1071                         return PTR_ERR(child);
1072         }
1073
1074         return 0;
1075 }
1076
1077 /*----------------------------------------------------------------------*/
1078
1079 /*
1080  * These three functions initialize the on-chip clock framework,
1081  * letting it generate the right frequencies for USB, MADC, and
1082  * other purposes.
1083  */
1084 static inline int __init protect_pm_master(void)
1085 {
1086         int e = 0;
1087
1088         e = twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER, 0,
1089                         TWL4030_PM_MASTER_PROTECT_KEY);
1090         return e;
1091 }
1092
1093 static inline int __init unprotect_pm_master(void)
1094 {
1095         int e = 0;
1096
1097         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1098                         TWL4030_PM_MASTER_KEY_CFG1,
1099                         TWL4030_PM_MASTER_PROTECT_KEY);
1100         e |= twl_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
1101                         TWL4030_PM_MASTER_KEY_CFG2,
1102                         TWL4030_PM_MASTER_PROTECT_KEY);
1103
1104         return e;
1105 }
1106
1107 static void clocks_init(struct device *dev,
1108                         struct twl4030_clock_init_data *clock)
1109 {
1110         int e = 0;
1111         struct clk *osc;
1112         u32 rate;
1113         u8 ctrl = HFCLK_FREQ_26_MHZ;
1114
1115         osc = clk_get(dev, "fck");
1116         if (IS_ERR(osc)) {
1117                 printk(KERN_WARNING "Skipping twl internal clock init and "
1118                                 "using bootloader value (unknown osc rate)\n");
1119                 return;
1120         }
1121
1122         rate = clk_get_rate(osc);
1123         clk_put(osc);
1124
1125         switch (rate) {
1126         case 19200000:
1127                 ctrl = HFCLK_FREQ_19p2_MHZ;
1128                 break;
1129         case 26000000:
1130                 ctrl = HFCLK_FREQ_26_MHZ;
1131                 break;
1132         case 38400000:
1133                 ctrl = HFCLK_FREQ_38p4_MHZ;
1134                 break;
1135         }
1136
1137         ctrl |= HIGH_PERF_SQ;
1138         if (clock && clock->ck32k_lowpwr_enable)
1139                 ctrl |= CK32K_LOWPWR_EN;
1140
1141         e |= unprotect_pm_master();
1142         /* effect->MADC+USB ck en */
1143         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
1144         e |= protect_pm_master();
1145
1146         if (e < 0)
1147                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
1148 }
1149
1150 /*----------------------------------------------------------------------*/
1151
1152
1153 static int twl_remove(struct i2c_client *client)
1154 {
1155         unsigned i, num_slaves;
1156         int status;
1157
1158         if (twl_class_is_4030()) {
1159                 status = twl4030_exit_irq();
1160                 num_slaves = TWL_NUM_SLAVES;
1161         } else {
1162                 status = twl6030_exit_irq();
1163                 num_slaves = TWL_NUM_SLAVES - 1;
1164         }
1165
1166         if (status < 0)
1167                 return status;
1168
1169         for (i = 0; i < num_slaves; i++) {
1170                 struct twl_client       *twl = &twl_modules[i];
1171
1172                 if (twl->client && twl->client != client)
1173                         i2c_unregister_device(twl->client);
1174                 twl_modules[i].client = NULL;
1175         }
1176         inuse = false;
1177         return 0;
1178 }
1179
1180 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
1181 static int __devinit
1182 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
1183 {
1184         struct twl4030_platform_data    *pdata = client->dev.platform_data;
1185         struct device_node              *node = client->dev.of_node;
1186         struct platform_device          *pdev;
1187         int                             irq_base = 0;
1188         int                             status;
1189         unsigned                        i, num_slaves;
1190
1191         pdev = platform_device_alloc(DRIVER_NAME, -1);
1192         if (!pdev) {
1193                 dev_err(&client->dev, "can't alloc pdev\n");
1194                 return -ENOMEM;
1195         }
1196
1197         status = platform_device_add(pdev);
1198         if (status) {
1199                 platform_device_put(pdev);
1200                 return status;
1201         }
1202
1203         if (node && !pdata) {
1204                 /*
1205                  * XXX: Temporary pdata until the information is correctly
1206                  * retrieved by every TWL modules from DT.
1207                  */
1208                 pdata = devm_kzalloc(&client->dev,
1209                                      sizeof(struct twl4030_platform_data),
1210                                      GFP_KERNEL);
1211                 if (!pdata) {
1212                         status = -ENOMEM;
1213                         goto free;
1214                 }
1215         }
1216
1217         if (!pdata) {
1218                 dev_dbg(&client->dev, "no platform data?\n");
1219                 status = -EINVAL;
1220                 goto free;
1221         }
1222
1223         platform_set_drvdata(pdev, pdata);
1224
1225         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
1226                 dev_dbg(&client->dev, "can't talk I2C?\n");
1227                 status = -EIO;
1228                 goto free;
1229         }
1230
1231         if (inuse) {
1232                 dev_dbg(&client->dev, "driver is already in use\n");
1233                 status = -EBUSY;
1234                 goto free;
1235         }
1236
1237         if ((id->driver_data) & TWL6030_CLASS) {
1238                 twl_id = TWL6030_CLASS_ID;
1239                 twl_map = &twl6030_map[0];
1240                 num_slaves = TWL_NUM_SLAVES - 1;
1241         } else {
1242                 twl_id = TWL4030_CLASS_ID;
1243                 twl_map = &twl4030_map[0];
1244                 num_slaves = TWL_NUM_SLAVES;
1245         }
1246
1247         for (i = 0; i < num_slaves; i++) {
1248                 struct twl_client *twl = &twl_modules[i];
1249
1250                 twl->address = client->addr + i;
1251                 if (i == 0) {
1252                         twl->client = client;
1253                 } else {
1254                         twl->client = i2c_new_dummy(client->adapter,
1255                                         twl->address);
1256                         if (!twl->client) {
1257                                 dev_err(&client->dev,
1258                                         "can't attach client %d\n", i);
1259                                 status = -ENOMEM;
1260                                 goto fail;
1261                         }
1262                 }
1263                 mutex_init(&twl->xfer_lock);
1264         }
1265
1266         inuse = true;
1267
1268         /* setup clock framework */
1269         clocks_init(&pdev->dev, pdata->clock);
1270
1271         /* read TWL IDCODE Register */
1272         if (twl_id == TWL4030_CLASS_ID) {
1273                 status = twl_read_idcode_register();
1274                 WARN(status < 0, "Error: reading twl_idcode register value\n");
1275         }
1276
1277         /* load power event scripts */
1278         if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power)
1279                 twl4030_power_init(pdata->power);
1280
1281         /* Maybe init the T2 Interrupt subsystem */
1282         if (client->irq) {
1283                 if (twl_class_is_4030()) {
1284                         twl4030_init_chip_irq(id->name);
1285                         irq_base = twl4030_init_irq(&client->dev, client->irq);
1286                 } else {
1287                         irq_base = twl6030_init_irq(&client->dev, client->irq);
1288                 }
1289
1290                 if (irq_base < 0) {
1291                         status = irq_base;
1292                         goto fail;
1293                 }
1294         }
1295
1296         /*
1297          * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1298          * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1299          * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1300          */
1301         if (twl_class_is_4030()) {
1302                 u8 temp;
1303
1304                 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1305                 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1306                         I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1307                 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1308         }
1309
1310         status = -ENODEV;
1311         if (node)
1312                 status = of_platform_populate(node, NULL, NULL, &client->dev);
1313         if (status)
1314                 status = add_children(pdata, irq_base, id->driver_data);
1315
1316 fail:
1317         if (status < 0)
1318                 twl_remove(client);
1319 free:
1320         if (status < 0)
1321                 platform_device_unregister(pdev);
1322
1323         return status;
1324 }
1325
1326 static const struct i2c_device_id twl_ids[] = {
1327         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
1328         { "twl5030", 0 },               /* T2 updated */
1329         { "twl5031", TWL5031 },         /* TWL5030 updated */
1330         { "tps65950", 0 },              /* catalog version of twl5030 */
1331         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
1332         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
1333         { "tps65921", TPS_SUBSET },     /* fewer LDOs; no codec, no LED
1334                                            and vibrator. Charger in USB module*/
1335         { "twl6030", TWL6030_CLASS },   /* "Phoenix power chip" */
1336         { "twl6025", TWL6030_CLASS | TWL6025_SUBCLASS }, /* "Phoenix lite" */
1337         { /* end of list */ },
1338 };
1339 MODULE_DEVICE_TABLE(i2c, twl_ids);
1340
1341 /* One Client Driver , 4 Clients */
1342 static struct i2c_driver twl_driver = {
1343         .driver.name    = DRIVER_NAME,
1344         .id_table       = twl_ids,
1345         .probe          = twl_probe,
1346         .remove         = twl_remove,
1347 };
1348
1349 static int __init twl_init(void)
1350 {
1351         return i2c_add_driver(&twl_driver);
1352 }
1353 subsys_initcall(twl_init);
1354
1355 static void __exit twl_exit(void)
1356 {
1357         i2c_del_driver(&twl_driver);
1358 }
1359 module_exit(twl_exit);
1360
1361 MODULE_AUTHOR("Texas Instruments, Inc.");
1362 MODULE_DESCRIPTION("I2C Core interface for TWL");
1363 MODULE_LICENSE("GPL");