mfd: twl-core: Clean up module by removing twl603x pdata handling
[platform/adaptation/renesas_rcar/renesas_kernel.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/regmap.h>
36 #include <linux/clk.h>
37 #include <linux/err.h>
38 #include <linux/device.h>
39 #include <linux/of.h>
40 #include <linux/of_irq.h>
41 #include <linux/of_platform.h>
42 #include <linux/irq.h>
43 #include <linux/irqdomain.h>
44
45 #include <linux/regulator/machine.h>
46
47 #include <linux/i2c.h>
48 #include <linux/i2c/twl.h>
49
50 #include "twl-core.h"
51
52 /*
53  * The TWL4030 "Triton 2" is one of a family of a multi-function "Power
54  * Management and System Companion Device" chips originally designed for
55  * use in OMAP2 and OMAP 3 based systems.  Its control interfaces use I2C,
56  * often at around 3 Mbit/sec, including for interrupt handling.
57  *
58  * This driver core provides genirq support for the interrupts emitted,
59  * by the various modules, and exports register access primitives.
60  *
61  * FIXME this driver currently requires use of the first interrupt line
62  * (and associated registers).
63  */
64
65 #define DRIVER_NAME                     "twl"
66
67 /* Triton Core internal information (BEGIN) */
68
69 /* Base Address defns for twl4030_map[] */
70
71 /* subchip/slave 0 - USB ID */
72 #define TWL4030_BASEADD_USB             0x0000
73
74 /* subchip/slave 1 - AUD ID */
75 #define TWL4030_BASEADD_AUDIO_VOICE     0x0000
76 #define TWL4030_BASEADD_GPIO            0x0098
77 #define TWL4030_BASEADD_INTBR           0x0085
78 #define TWL4030_BASEADD_PIH             0x0080
79 #define TWL4030_BASEADD_TEST            0x004C
80
81 /* subchip/slave 2 - AUX ID */
82 #define TWL4030_BASEADD_INTERRUPTS      0x00B9
83 #define TWL4030_BASEADD_LED             0x00EE
84 #define TWL4030_BASEADD_MADC            0x0000
85 #define TWL4030_BASEADD_MAIN_CHARGE     0x0074
86 #define TWL4030_BASEADD_PRECHARGE       0x00AA
87 #define TWL4030_BASEADD_PWM             0x00F8
88 #define TWL4030_BASEADD_KEYPAD          0x00D2
89
90 #define TWL5031_BASEADD_ACCESSORY       0x0074 /* Replaces Main Charge */
91 #define TWL5031_BASEADD_INTERRUPTS      0x00B9 /* Different than TWL4030's
92                                                   one */
93
94 /* subchip/slave 3 - POWER ID */
95 #define TWL4030_BASEADD_BACKUP          0x0014
96 #define TWL4030_BASEADD_INT             0x002E
97 #define TWL4030_BASEADD_PM_MASTER       0x0036
98 #define TWL4030_BASEADD_PM_RECEIVER     0x005B
99 #define TWL4030_BASEADD_RTC             0x001C
100 #define TWL4030_BASEADD_SECURED_REG     0x0000
101
102 /* Triton Core internal information (END) */
103
104
105 /* subchip/slave 0 0x48 - POWER */
106 #define TWL6030_BASEADD_RTC             0x0000
107 #define TWL6030_BASEADD_SECURED_REG     0x0017
108 #define TWL6030_BASEADD_PM_MASTER       0x001F
109 #define TWL6030_BASEADD_PM_SLAVE_MISC   0x0030 /* PM_RECEIVER */
110 #define TWL6030_BASEADD_PM_MISC         0x00E2
111 #define TWL6030_BASEADD_PM_PUPD         0x00F0
112
113 /* subchip/slave 1 0x49 - FEATURE */
114 #define TWL6030_BASEADD_USB             0x0000
115 #define TWL6030_BASEADD_GPADC_CTRL      0x002E
116 #define TWL6030_BASEADD_AUX             0x0090
117 #define TWL6030_BASEADD_PWM             0x00BA
118 #define TWL6030_BASEADD_GASGAUGE        0x00C0
119 #define TWL6030_BASEADD_PIH             0x00D0
120 #define TWL6030_BASEADD_CHARGER         0x00E0
121 #define TWL6032_BASEADD_CHARGER         0x00DA
122 #define TWL6030_BASEADD_LED             0x00F4
123
124 /* subchip/slave 2 0x4A - DFT */
125 #define TWL6030_BASEADD_DIEID           0x00C0
126
127 /* subchip/slave 3 0x4B - AUDIO */
128 #define TWL6030_BASEADD_AUDIO           0x0000
129 #define TWL6030_BASEADD_RSV             0x0000
130 #define TWL6030_BASEADD_ZERO            0x0000
131
132 /* Few power values */
133 #define R_CFG_BOOT                      0x05
134
135 /* some fields in R_CFG_BOOT */
136 #define HFCLK_FREQ_19p2_MHZ             (1 << 0)
137 #define HFCLK_FREQ_26_MHZ               (2 << 0)
138 #define HFCLK_FREQ_38p4_MHZ             (3 << 0)
139 #define HIGH_PERF_SQ                    (1 << 3)
140 #define CK32K_LOWPWR_EN                 (1 << 7)
141
142 /*----------------------------------------------------------------------*/
143
144 /* Structure for each TWL4030/TWL6030 Slave */
145 struct twl_client {
146         struct i2c_client *client;
147         struct regmap *regmap;
148 };
149
150 /* mapping the module id to slave id and base address */
151 struct twl_mapping {
152         unsigned char sid;      /* Slave ID */
153         unsigned char base;     /* base address */
154 };
155
156 struct twl_private {
157         bool ready; /* The core driver is ready to be used */
158         u32 twl_idcode; /* TWL IDCODE Register value */
159         unsigned int twl_id;
160
161         struct twl_mapping *twl_map;
162         struct twl_client *twl_modules;
163 };
164
165 static struct twl_private *twl_priv;
166
167 static struct twl_mapping twl4030_map[] = {
168         /*
169          * NOTE:  don't change this table without updating the
170          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
171          * so they continue to match the order in this table.
172          */
173
174         /* Common IPs */
175         { 0, TWL4030_BASEADD_USB },
176         { 1, TWL4030_BASEADD_PIH },
177         { 2, TWL4030_BASEADD_MAIN_CHARGE },
178         { 3, TWL4030_BASEADD_PM_MASTER },
179         { 3, TWL4030_BASEADD_PM_RECEIVER },
180
181         { 3, TWL4030_BASEADD_RTC },
182         { 2, TWL4030_BASEADD_PWM },
183         { 2, TWL4030_BASEADD_LED },
184         { 3, TWL4030_BASEADD_SECURED_REG },
185
186         /* TWL4030 specific IPs */
187         { 1, TWL4030_BASEADD_AUDIO_VOICE },
188         { 1, TWL4030_BASEADD_GPIO },
189         { 1, TWL4030_BASEADD_INTBR },
190         { 1, TWL4030_BASEADD_TEST },
191         { 2, TWL4030_BASEADD_KEYPAD },
192
193         { 2, TWL4030_BASEADD_MADC },
194         { 2, TWL4030_BASEADD_INTERRUPTS },
195         { 2, TWL4030_BASEADD_PRECHARGE },
196         { 3, TWL4030_BASEADD_BACKUP },
197         { 3, TWL4030_BASEADD_INT },
198
199         { 2, TWL5031_BASEADD_ACCESSORY },
200         { 2, TWL5031_BASEADD_INTERRUPTS },
201 };
202
203 static struct regmap_config twl4030_regmap_config[4] = {
204         {
205                 /* Address 0x48 */
206                 .reg_bits = 8,
207                 .val_bits = 8,
208                 .max_register = 0xff,
209         },
210         {
211                 /* Address 0x49 */
212                 .reg_bits = 8,
213                 .val_bits = 8,
214                 .max_register = 0xff,
215         },
216         {
217                 /* Address 0x4a */
218                 .reg_bits = 8,
219                 .val_bits = 8,
220                 .max_register = 0xff,
221         },
222         {
223                 /* Address 0x4b */
224                 .reg_bits = 8,
225                 .val_bits = 8,
226                 .max_register = 0xff,
227         },
228 };
229
230 static struct twl_mapping twl6030_map[] = {
231         /*
232          * NOTE:  don't change this table without updating the
233          * <linux/i2c/twl.h> defines for TWL4030_MODULE_*
234          * so they continue to match the order in this table.
235          */
236
237         /* Common IPs */
238         { 1, TWL6030_BASEADD_USB },
239         { 1, TWL6030_BASEADD_PIH },
240         { 1, TWL6030_BASEADD_CHARGER },
241         { 0, TWL6030_BASEADD_PM_MASTER },
242         { 0, TWL6030_BASEADD_PM_SLAVE_MISC },
243
244         { 0, TWL6030_BASEADD_RTC },
245         { 1, TWL6030_BASEADD_PWM },
246         { 1, TWL6030_BASEADD_LED },
247         { 0, TWL6030_BASEADD_SECURED_REG },
248
249         /* TWL6030 specific IPs */
250         { 0, TWL6030_BASEADD_ZERO },
251         { 1, TWL6030_BASEADD_ZERO },
252         { 2, TWL6030_BASEADD_ZERO },
253         { 1, TWL6030_BASEADD_GPADC_CTRL },
254         { 1, TWL6030_BASEADD_GASGAUGE },
255 };
256
257 static struct regmap_config twl6030_regmap_config[3] = {
258         {
259                 /* Address 0x48 */
260                 .reg_bits = 8,
261                 .val_bits = 8,
262                 .max_register = 0xff,
263         },
264         {
265                 /* Address 0x49 */
266                 .reg_bits = 8,
267                 .val_bits = 8,
268                 .max_register = 0xff,
269         },
270         {
271                 /* Address 0x4a */
272                 .reg_bits = 8,
273                 .val_bits = 8,
274                 .max_register = 0xff,
275         },
276 };
277
278 /*----------------------------------------------------------------------*/
279
280 static inline int twl_get_num_slaves(void)
281 {
282         if (twl_class_is_4030())
283                 return 4; /* TWL4030 class have four slave address */
284         else
285                 return 3; /* TWL6030 class have three slave address */
286 }
287
288 static inline int twl_get_last_module(void)
289 {
290         if (twl_class_is_4030())
291                 return TWL4030_MODULE_LAST;
292         else
293                 return TWL6030_MODULE_LAST;
294 }
295
296 /* Exported Functions */
297
298 unsigned int twl_rev(void)
299 {
300         return twl_priv ? twl_priv->twl_id : 0;
301 }
302 EXPORT_SYMBOL(twl_rev);
303
304 /**
305  * twl_i2c_write - Writes a n bit register in TWL4030/TWL5030/TWL60X0
306  * @mod_no: module number
307  * @value: an array of num_bytes+1 containing data to write
308  * @reg: register address (just offset will do)
309  * @num_bytes: number of bytes to transfer
310  *
311  * Returns the result of operation - 0 is success
312  */
313 int twl_i2c_write(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
314 {
315         int ret;
316         int sid;
317         struct twl_client *twl;
318
319         if (unlikely(!twl_priv || !twl_priv->ready)) {
320                 pr_err("%s: not initialized\n", DRIVER_NAME);
321                 return -EPERM;
322         }
323         if (unlikely(mod_no >= twl_get_last_module())) {
324                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
325                 return -EPERM;
326         }
327
328         sid = twl_priv->twl_map[mod_no].sid;
329         twl = &twl_priv->twl_modules[sid];
330
331         ret = regmap_bulk_write(twl->regmap,
332                                 twl_priv->twl_map[mod_no].base + reg, value,
333                                 num_bytes);
334
335         if (ret)
336                 pr_err("%s: Write failed (mod %d, reg 0x%02x count %d)\n",
337                        DRIVER_NAME, mod_no, reg, num_bytes);
338
339         return ret;
340 }
341 EXPORT_SYMBOL(twl_i2c_write);
342
343 /**
344  * twl_i2c_read - Reads a n bit register in TWL4030/TWL5030/TWL60X0
345  * @mod_no: module number
346  * @value: an array of num_bytes containing data to be read
347  * @reg: register address (just offset will do)
348  * @num_bytes: number of bytes to transfer
349  *
350  * Returns result of operation - num_bytes is success else failure.
351  */
352 int twl_i2c_read(u8 mod_no, u8 *value, u8 reg, unsigned num_bytes)
353 {
354         int ret;
355         int sid;
356         struct twl_client *twl;
357
358         if (unlikely(!twl_priv || !twl_priv->ready)) {
359                 pr_err("%s: not initialized\n", DRIVER_NAME);
360                 return -EPERM;
361         }
362         if (unlikely(mod_no >= twl_get_last_module())) {
363                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
364                 return -EPERM;
365         }
366
367         sid = twl_priv->twl_map[mod_no].sid;
368         twl = &twl_priv->twl_modules[sid];
369
370         ret = regmap_bulk_read(twl->regmap,
371                                twl_priv->twl_map[mod_no].base + reg, value,
372                                num_bytes);
373
374         if (ret)
375                 pr_err("%s: Read failed (mod %d, reg 0x%02x count %d)\n",
376                        DRIVER_NAME, mod_no, reg, num_bytes);
377
378         return ret;
379 }
380 EXPORT_SYMBOL(twl_i2c_read);
381
382 /*----------------------------------------------------------------------*/
383
384 /**
385  * twl_read_idcode_register - API to read the IDCODE register.
386  *
387  * Unlocks the IDCODE register and read the 32 bit value.
388  */
389 static int twl_read_idcode_register(void)
390 {
391         int err;
392
393         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, TWL_EEPROM_R_UNLOCK,
394                                                 REG_UNLOCK_TEST_REG);
395         if (err) {
396                 pr_err("TWL4030 Unable to unlock IDCODE registers -%d\n", err);
397                 goto fail;
398         }
399
400         err = twl_i2c_read(TWL4030_MODULE_INTBR, (u8 *)(&twl_priv->twl_idcode),
401                                                 REG_IDCODE_7_0, 4);
402         if (err) {
403                 pr_err("TWL4030: unable to read IDCODE -%d\n", err);
404                 goto fail;
405         }
406
407         err = twl_i2c_write_u8(TWL4030_MODULE_INTBR, 0x0, REG_UNLOCK_TEST_REG);
408         if (err)
409                 pr_err("TWL4030 Unable to relock IDCODE registers -%d\n", err);
410 fail:
411         return err;
412 }
413
414 /**
415  * twl_get_type - API to get TWL Si type.
416  *
417  * Api to get the TWL Si type from IDCODE value.
418  */
419 int twl_get_type(void)
420 {
421         return TWL_SIL_TYPE(twl_priv->twl_idcode);
422 }
423 EXPORT_SYMBOL_GPL(twl_get_type);
424
425 /**
426  * twl_get_version - API to get TWL Si version.
427  *
428  * Api to get the TWL Si version from IDCODE value.
429  */
430 int twl_get_version(void)
431 {
432         return TWL_SIL_REV(twl_priv->twl_idcode);
433 }
434 EXPORT_SYMBOL_GPL(twl_get_version);
435
436 /**
437  * twl_get_hfclk_rate - API to get TWL external HFCLK clock rate.
438  *
439  * Api to get the TWL HFCLK rate based on BOOT_CFG register.
440  */
441 int twl_get_hfclk_rate(void)
442 {
443         u8 ctrl;
444         int rate;
445
446         twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &ctrl, R_CFG_BOOT);
447
448         switch (ctrl & 0x3) {
449         case HFCLK_FREQ_19p2_MHZ:
450                 rate = 19200000;
451                 break;
452         case HFCLK_FREQ_26_MHZ:
453                 rate = 26000000;
454                 break;
455         case HFCLK_FREQ_38p4_MHZ:
456                 rate = 38400000;
457                 break;
458         default:
459                 pr_err("TWL4030: HFCLK is not configured\n");
460                 rate = -EINVAL;
461                 break;
462         }
463
464         return rate;
465 }
466 EXPORT_SYMBOL_GPL(twl_get_hfclk_rate);
467
468 static struct device *
469 add_numbered_child(unsigned mod_no, const char *name, int num,
470                 void *pdata, unsigned pdata_len,
471                 bool can_wakeup, int irq0, int irq1)
472 {
473         struct platform_device  *pdev;
474         struct twl_client       *twl;
475         int                     status, sid;
476
477         if (unlikely(mod_no >= twl_get_last_module())) {
478                 pr_err("%s: invalid module number %d\n", DRIVER_NAME, mod_no);
479                 return ERR_PTR(-EPERM);
480         }
481         sid = twl_priv->twl_map[mod_no].sid;
482         twl = &twl_priv->twl_modules[sid];
483
484         pdev = platform_device_alloc(name, num);
485         if (!pdev) {
486                 dev_dbg(&twl->client->dev, "can't alloc dev\n");
487                 status = -ENOMEM;
488                 goto err;
489         }
490
491         pdev->dev.parent = &twl->client->dev;
492
493         if (pdata) {
494                 status = platform_device_add_data(pdev, pdata, pdata_len);
495                 if (status < 0) {
496                         dev_dbg(&pdev->dev, "can't add platform_data\n");
497                         goto err;
498                 }
499         }
500
501         if (irq0) {
502                 struct resource r[2] = {
503                         { .start = irq0, .flags = IORESOURCE_IRQ, },
504                         { .start = irq1, .flags = IORESOURCE_IRQ, },
505                 };
506
507                 status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
508                 if (status < 0) {
509                         dev_dbg(&pdev->dev, "can't add irqs\n");
510                         goto err;
511                 }
512         }
513
514         status = platform_device_add(pdev);
515         if (status == 0)
516                 device_init_wakeup(&pdev->dev, can_wakeup);
517
518 err:
519         if (status < 0) {
520                 platform_device_put(pdev);
521                 dev_err(&twl->client->dev, "can't add %s dev\n", name);
522                 return ERR_PTR(status);
523         }
524         return &pdev->dev;
525 }
526
527 static inline struct device *add_child(unsigned mod_no, const char *name,
528                 void *pdata, unsigned pdata_len,
529                 bool can_wakeup, int irq0, int irq1)
530 {
531         return add_numbered_child(mod_no, name, -1, pdata, pdata_len,
532                 can_wakeup, irq0, irq1);
533 }
534
535 static struct device *
536 add_regulator_linked(int num, struct regulator_init_data *pdata,
537                 struct regulator_consumer_supply *consumers,
538                 unsigned num_consumers, unsigned long features)
539 {
540         struct twl_regulator_driver_data drv_data;
541
542         /* regulator framework demands init_data ... */
543         if (!pdata)
544                 return NULL;
545
546         if (consumers) {
547                 pdata->consumer_supplies = consumers;
548                 pdata->num_consumer_supplies = num_consumers;
549         }
550
551         if (pdata->driver_data) {
552                 /* If we have existing drv_data, just add the flags */
553                 struct twl_regulator_driver_data *tmp;
554                 tmp = pdata->driver_data;
555                 tmp->features |= features;
556         } else {
557                 /* add new driver data struct, used only during init */
558                 drv_data.features = features;
559                 drv_data.set_voltage = NULL;
560                 drv_data.get_voltage = NULL;
561                 drv_data.data = NULL;
562                 pdata->driver_data = &drv_data;
563         }
564
565         /* NOTE:  we currently ignore regulator IRQs, e.g. for short circuits */
566         return add_numbered_child(TWL_MODULE_PM_MASTER, "twl_reg", num,
567                 pdata, sizeof(*pdata), false, 0, 0);
568 }
569
570 static struct device *
571 add_regulator(int num, struct regulator_init_data *pdata,
572                 unsigned long features)
573 {
574         return add_regulator_linked(num, pdata, NULL, 0, features);
575 }
576
577 /*
578  * NOTE:  We know the first 8 IRQs after pdata->base_irq are
579  * for the PIH, and the next are for the PWR_INT SIH, since
580  * that's how twl_init_irq() sets things up.
581  */
582
583 static int
584 add_children(struct twl4030_platform_data *pdata, unsigned irq_base,
585                 unsigned long features)
586 {
587         struct device   *child;
588
589         if (IS_ENABLED(CONFIG_GPIO_TWL4030) && pdata->gpio) {
590                 child = add_child(TWL4030_MODULE_GPIO, "twl4030_gpio",
591                                 pdata->gpio, sizeof(*pdata->gpio),
592                                 false, irq_base + GPIO_INTR_OFFSET, 0);
593                 if (IS_ERR(child))
594                         return PTR_ERR(child);
595         }
596
597         if (IS_ENABLED(CONFIG_KEYBOARD_TWL4030) && pdata->keypad) {
598                 child = add_child(TWL4030_MODULE_KEYPAD, "twl4030_keypad",
599                                 pdata->keypad, sizeof(*pdata->keypad),
600                                 true, irq_base + KEYPAD_INTR_OFFSET, 0);
601                 if (IS_ERR(child))
602                         return PTR_ERR(child);
603         }
604
605         if (IS_ENABLED(CONFIG_TWL4030_MADC) && pdata->madc &&
606             twl_class_is_4030()) {
607                 child = add_child(TWL4030_MODULE_MADC, "twl4030_madc",
608                                 pdata->madc, sizeof(*pdata->madc),
609                                 true, irq_base + MADC_INTR_OFFSET, 0);
610                 if (IS_ERR(child))
611                         return PTR_ERR(child);
612         }
613
614         if (IS_ENABLED(CONFIG_RTC_DRV_TWL4030)) {
615                 /*
616                  * REVISIT platform_data here currently might expose the
617                  * "msecure" line ... but for now we just expect board
618                  * setup to tell the chip "it's always ok to SET_TIME".
619                  * Eventually, Linux might become more aware of such
620                  * HW security concerns, and "least privilege".
621                  */
622                 child = add_child(TWL_MODULE_RTC, "twl_rtc", NULL, 0,
623                                 true, irq_base + RTC_INTR_OFFSET, 0);
624                 if (IS_ERR(child))
625                         return PTR_ERR(child);
626         }
627
628         if (IS_ENABLED(CONFIG_PWM_TWL)) {
629                 child = add_child(TWL_MODULE_PWM, "twl-pwm", NULL, 0,
630                                   false, 0, 0);
631                 if (IS_ERR(child))
632                         return PTR_ERR(child);
633         }
634
635         if (IS_ENABLED(CONFIG_PWM_TWL_LED)) {
636                 child = add_child(TWL_MODULE_LED, "twl-pwmled", NULL, 0,
637                                   false, 0, 0);
638                 if (IS_ERR(child))
639                         return PTR_ERR(child);
640         }
641
642         if (IS_ENABLED(CONFIG_TWL4030_USB) && pdata->usb &&
643             twl_class_is_4030()) {
644
645                 static struct regulator_consumer_supply usb1v5 = {
646                         .supply =       "usb1v5",
647                 };
648                 static struct regulator_consumer_supply usb1v8 = {
649                         .supply =       "usb1v8",
650                 };
651                 static struct regulator_consumer_supply usb3v1[] = {
652                         { .supply =     "usb3v1" },
653                         { .supply =     "bci3v1" },
654                 };
655
656         /* First add the regulators so that they can be used by transceiver */
657                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030)) {
658                         /* this is a template that gets copied */
659                         struct regulator_init_data usb_fixed = {
660                                 .constraints.valid_modes_mask =
661                                         REGULATOR_MODE_NORMAL
662                                         | REGULATOR_MODE_STANDBY,
663                                 .constraints.valid_ops_mask =
664                                         REGULATOR_CHANGE_MODE
665                                         | REGULATOR_CHANGE_STATUS,
666                         };
667
668                         child = add_regulator_linked(TWL4030_REG_VUSB1V5,
669                                                       &usb_fixed, &usb1v5, 1,
670                                                       features);
671                         if (IS_ERR(child))
672                                 return PTR_ERR(child);
673
674                         child = add_regulator_linked(TWL4030_REG_VUSB1V8,
675                                                       &usb_fixed, &usb1v8, 1,
676                                                       features);
677                         if (IS_ERR(child))
678                                 return PTR_ERR(child);
679
680                         child = add_regulator_linked(TWL4030_REG_VUSB3V1,
681                                                       &usb_fixed, usb3v1, 2,
682                                                       features);
683                         if (IS_ERR(child))
684                                 return PTR_ERR(child);
685
686                 }
687
688                 child = add_child(TWL_MODULE_USB, "twl4030_usb",
689                                 pdata->usb, sizeof(*pdata->usb), true,
690                                 /* irq0 = USB_PRES, irq1 = USB */
691                                 irq_base + USB_PRES_INTR_OFFSET,
692                                 irq_base + USB_INTR_OFFSET);
693
694                 if (IS_ERR(child))
695                         return PTR_ERR(child);
696
697                 /* we need to connect regulators to this transceiver */
698                 if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && child) {
699                         usb1v5.dev_name = dev_name(child);
700                         usb1v8.dev_name = dev_name(child);
701                         usb3v1[0].dev_name = dev_name(child);
702                 }
703         }
704
705         if (IS_ENABLED(CONFIG_TWL4030_WATCHDOG) && twl_class_is_4030()) {
706                 child = add_child(TWL_MODULE_PM_RECEIVER, "twl4030_wdt", NULL,
707                                   0, false, 0, 0);
708                 if (IS_ERR(child))
709                         return PTR_ERR(child);
710         }
711
712         if (IS_ENABLED(CONFIG_INPUT_TWL4030_PWRBUTTON) && twl_class_is_4030()) {
713                 child = add_child(TWL_MODULE_PM_MASTER, "twl4030_pwrbutton",
714                                   NULL, 0, true, irq_base + 8 + 0, 0);
715                 if (IS_ERR(child))
716                         return PTR_ERR(child);
717         }
718
719         if (IS_ENABLED(CONFIG_MFD_TWL4030_AUDIO) && pdata->audio &&
720             twl_class_is_4030()) {
721                 child = add_child(TWL4030_MODULE_AUDIO_VOICE, "twl4030-audio",
722                                 pdata->audio, sizeof(*pdata->audio),
723                                 false, 0, 0);
724                 if (IS_ERR(child))
725                         return PTR_ERR(child);
726         }
727
728         /* twl4030 regulators */
729         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && twl_class_is_4030()) {
730                 child = add_regulator(TWL4030_REG_VPLL1, pdata->vpll1,
731                                         features);
732                 if (IS_ERR(child))
733                         return PTR_ERR(child);
734
735                 child = add_regulator(TWL4030_REG_VIO, pdata->vio,
736                                         features);
737                 if (IS_ERR(child))
738                         return PTR_ERR(child);
739
740                 child = add_regulator(TWL4030_REG_VDD1, pdata->vdd1,
741                                         features);
742                 if (IS_ERR(child))
743                         return PTR_ERR(child);
744
745                 child = add_regulator(TWL4030_REG_VDD2, pdata->vdd2,
746                                         features);
747                 if (IS_ERR(child))
748                         return PTR_ERR(child);
749
750                 child = add_regulator(TWL4030_REG_VMMC1, pdata->vmmc1,
751                                         features);
752                 if (IS_ERR(child))
753                         return PTR_ERR(child);
754
755                 child = add_regulator(TWL4030_REG_VDAC, pdata->vdac,
756                                         features);
757                 if (IS_ERR(child))
758                         return PTR_ERR(child);
759
760                 child = add_regulator((features & TWL4030_VAUX2)
761                                         ? TWL4030_REG_VAUX2_4030
762                                         : TWL4030_REG_VAUX2,
763                                 pdata->vaux2, features);
764                 if (IS_ERR(child))
765                         return PTR_ERR(child);
766
767                 child = add_regulator(TWL4030_REG_VINTANA1, pdata->vintana1,
768                                         features);
769                 if (IS_ERR(child))
770                         return PTR_ERR(child);
771
772                 child = add_regulator(TWL4030_REG_VINTANA2, pdata->vintana2,
773                                         features);
774                 if (IS_ERR(child))
775                         return PTR_ERR(child);
776
777                 child = add_regulator(TWL4030_REG_VINTDIG, pdata->vintdig,
778                                         features);
779                 if (IS_ERR(child))
780                         return PTR_ERR(child);
781         }
782
783         /* maybe add LDOs that are omitted on cost-reduced parts */
784         if (IS_ENABLED(CONFIG_REGULATOR_TWL4030) && !(features & TPS_SUBSET)
785           && twl_class_is_4030()) {
786                 child = add_regulator(TWL4030_REG_VPLL2, pdata->vpll2,
787                                         features);
788                 if (IS_ERR(child))
789                         return PTR_ERR(child);
790
791                 child = add_regulator(TWL4030_REG_VMMC2, pdata->vmmc2,
792                                         features);
793                 if (IS_ERR(child))
794                         return PTR_ERR(child);
795
796                 child = add_regulator(TWL4030_REG_VSIM, pdata->vsim,
797                                         features);
798                 if (IS_ERR(child))
799                         return PTR_ERR(child);
800
801                 child = add_regulator(TWL4030_REG_VAUX1, pdata->vaux1,
802                                         features);
803                 if (IS_ERR(child))
804                         return PTR_ERR(child);
805
806                 child = add_regulator(TWL4030_REG_VAUX3, pdata->vaux3,
807                                         features);
808                 if (IS_ERR(child))
809                         return PTR_ERR(child);
810
811                 child = add_regulator(TWL4030_REG_VAUX4, pdata->vaux4,
812                                         features);
813                 if (IS_ERR(child))
814                         return PTR_ERR(child);
815         }
816
817         if (IS_ENABLED(CONFIG_CHARGER_TWL4030) && pdata->bci &&
818                         !(features & (TPS_SUBSET | TWL5031))) {
819                 child = add_child(TWL_MODULE_MAIN_CHARGE, "twl4030_bci",
820                                 pdata->bci, sizeof(*pdata->bci), false,
821                                 /* irq0 = CHG_PRES, irq1 = BCI */
822                                 irq_base + BCI_PRES_INTR_OFFSET,
823                                 irq_base + BCI_INTR_OFFSET);
824                 if (IS_ERR(child))
825                         return PTR_ERR(child);
826         }
827
828         if (IS_ENABLED(CONFIG_TWL4030_POWER) && pdata->power) {
829                 child = add_child(TWL_MODULE_PM_MASTER, "twl4030_power",
830                                   pdata->power, sizeof(*pdata->power), false,
831                                   0, 0);
832                 if (IS_ERR(child))
833                         return PTR_ERR(child);
834         }
835
836         return 0;
837 }
838
839 /*----------------------------------------------------------------------*/
840
841 /*
842  * These three functions initialize the on-chip clock framework,
843  * letting it generate the right frequencies for USB, MADC, and
844  * other purposes.
845  */
846 static inline int __init protect_pm_master(void)
847 {
848         int e = 0;
849
850         e = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, 0,
851                              TWL4030_PM_MASTER_PROTECT_KEY);
852         return e;
853 }
854
855 static inline int __init unprotect_pm_master(void)
856 {
857         int e = 0;
858
859         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG1,
860                               TWL4030_PM_MASTER_PROTECT_KEY);
861         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, TWL4030_PM_MASTER_KEY_CFG2,
862                               TWL4030_PM_MASTER_PROTECT_KEY);
863
864         return e;
865 }
866
867 static void clocks_init(struct device *dev,
868                         struct twl4030_clock_init_data *clock)
869 {
870         int e = 0;
871         struct clk *osc;
872         u32 rate;
873         u8 ctrl = HFCLK_FREQ_26_MHZ;
874
875         osc = clk_get(dev, "fck");
876         if (IS_ERR(osc)) {
877                 printk(KERN_WARNING "Skipping twl internal clock init and "
878                                 "using bootloader value (unknown osc rate)\n");
879                 return;
880         }
881
882         rate = clk_get_rate(osc);
883         clk_put(osc);
884
885         switch (rate) {
886         case 19200000:
887                 ctrl = HFCLK_FREQ_19p2_MHZ;
888                 break;
889         case 26000000:
890                 ctrl = HFCLK_FREQ_26_MHZ;
891                 break;
892         case 38400000:
893                 ctrl = HFCLK_FREQ_38p4_MHZ;
894                 break;
895         }
896
897         ctrl |= HIGH_PERF_SQ;
898         if (clock && clock->ck32k_lowpwr_enable)
899                 ctrl |= CK32K_LOWPWR_EN;
900
901         e |= unprotect_pm_master();
902         /* effect->MADC+USB ck en */
903         e |= twl_i2c_write_u8(TWL_MODULE_PM_MASTER, ctrl, R_CFG_BOOT);
904         e |= protect_pm_master();
905
906         if (e < 0)
907                 pr_err("%s: clock init err [%d]\n", DRIVER_NAME, e);
908 }
909
910 /*----------------------------------------------------------------------*/
911
912
913 static int twl_remove(struct i2c_client *client)
914 {
915         unsigned i, num_slaves;
916         int status;
917
918         if (twl_class_is_4030())
919                 status = twl4030_exit_irq();
920         else
921                 status = twl6030_exit_irq();
922
923         if (status < 0)
924                 return status;
925
926         num_slaves = twl_get_num_slaves();
927         for (i = 0; i < num_slaves; i++) {
928                 struct twl_client       *twl = &twl_priv->twl_modules[i];
929
930                 if (twl->client && twl->client != client)
931                         i2c_unregister_device(twl->client);
932                 twl->client = NULL;
933         }
934         twl_priv->ready = false;
935         return 0;
936 }
937
938 /* NOTE: This driver only handles a single twl4030/tps659x0 chip */
939 static int
940 twl_probe(struct i2c_client *client, const struct i2c_device_id *id)
941 {
942         struct twl4030_platform_data    *pdata = dev_get_platdata(&client->dev);
943         struct device_node              *node = client->dev.of_node;
944         struct platform_device          *pdev;
945         struct regmap_config            *twl_regmap_config;
946         int                             irq_base = 0;
947         int                             status;
948         unsigned                        i, num_slaves;
949
950         if (!node && !pdata) {
951                 dev_err(&client->dev, "no platform data\n");
952                 return -EINVAL;
953         }
954
955         if (twl_priv) {
956                 dev_dbg(&client->dev, "only one instance of %s allowed\n",
957                         DRIVER_NAME);
958                 return -EBUSY;
959         }
960
961         pdev = platform_device_alloc(DRIVER_NAME, -1);
962         if (!pdev) {
963                 dev_err(&client->dev, "can't alloc pdev\n");
964                 return -ENOMEM;
965         }
966
967         status = platform_device_add(pdev);
968         if (status) {
969                 platform_device_put(pdev);
970                 return status;
971         }
972
973         if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
974                 dev_dbg(&client->dev, "can't talk I2C?\n");
975                 status = -EIO;
976                 goto free;
977         }
978
979         twl_priv = devm_kzalloc(&client->dev, sizeof(struct twl_private),
980                                 GFP_KERNEL);
981         if (!twl_priv) {
982                 status = -ENOMEM;
983                 goto free;
984         }
985
986         if ((id->driver_data) & TWL6030_CLASS) {
987                 twl_priv->twl_id = TWL6030_CLASS_ID;
988                 twl_priv->twl_map = &twl6030_map[0];
989                 /* The charger base address is different in twl6032 */
990                 if ((id->driver_data) & TWL6032_SUBCLASS)
991                         twl_priv->twl_map[TWL_MODULE_MAIN_CHARGE].base =
992                                                         TWL6032_BASEADD_CHARGER;
993                 twl_regmap_config = twl6030_regmap_config;
994         } else {
995                 twl_priv->twl_id = TWL4030_CLASS_ID;
996                 twl_priv->twl_map = &twl4030_map[0];
997                 twl_regmap_config = twl4030_regmap_config;
998         }
999
1000         num_slaves = twl_get_num_slaves();
1001         twl_priv->twl_modules = devm_kzalloc(&client->dev,
1002                                          sizeof(struct twl_client) * num_slaves,
1003                                          GFP_KERNEL);
1004         if (!twl_priv->twl_modules) {
1005                 status = -ENOMEM;
1006                 goto free;
1007         }
1008
1009         for (i = 0; i < num_slaves; i++) {
1010                 struct twl_client *twl = &twl_priv->twl_modules[i];
1011
1012                 if (i == 0) {
1013                         twl->client = client;
1014                 } else {
1015                         twl->client = i2c_new_dummy(client->adapter,
1016                                                     client->addr + i);
1017                         if (!twl->client) {
1018                                 dev_err(&client->dev,
1019                                         "can't attach client %d\n", i);
1020                                 status = -ENOMEM;
1021                                 goto fail;
1022                         }
1023                 }
1024
1025                 twl->regmap = devm_regmap_init_i2c(twl->client,
1026                                                    &twl_regmap_config[i]);
1027                 if (IS_ERR(twl->regmap)) {
1028                         status = PTR_ERR(twl->regmap);
1029                         dev_err(&client->dev,
1030                                 "Failed to allocate regmap %d, err: %d\n", i,
1031                                 status);
1032                         goto fail;
1033                 }
1034         }
1035
1036         twl_priv->ready = true;
1037
1038         /* setup clock framework */
1039         clocks_init(&pdev->dev, pdata ? pdata->clock : NULL);
1040
1041         /* read TWL IDCODE Register */
1042         if (twl_class_is_4030()) {
1043                 status = twl_read_idcode_register();
1044                 WARN(status < 0, "Error: reading twl_idcode register value\n");
1045         }
1046
1047         /* Maybe init the T2 Interrupt subsystem */
1048         if (client->irq) {
1049                 if (twl_class_is_4030()) {
1050                         twl4030_init_chip_irq(id->name);
1051                         irq_base = twl4030_init_irq(&client->dev, client->irq);
1052                 } else {
1053                         irq_base = twl6030_init_irq(&client->dev, client->irq);
1054                 }
1055
1056                 if (irq_base < 0) {
1057                         status = irq_base;
1058                         goto fail;
1059                 }
1060         }
1061
1062         /*
1063          * Disable TWL4030/TWL5030 I2C Pull-up on I2C1 and I2C4(SR) interface.
1064          * Program I2C_SCL_CTRL_PU(bit 0)=0, I2C_SDA_CTRL_PU (bit 2)=0,
1065          * SR_I2C_SCL_CTRL_PU(bit 4)=0 and SR_I2C_SDA_CTRL_PU(bit 6)=0.
1066          */
1067         if (twl_class_is_4030()) {
1068                 u8 temp;
1069
1070                 twl_i2c_read_u8(TWL4030_MODULE_INTBR, &temp, REG_GPPUPDCTR1);
1071                 temp &= ~(SR_I2C_SDA_CTRL_PU | SR_I2C_SCL_CTRL_PU | \
1072                         I2C_SDA_CTRL_PU | I2C_SCL_CTRL_PU);
1073                 twl_i2c_write_u8(TWL4030_MODULE_INTBR, temp, REG_GPPUPDCTR1);
1074         }
1075
1076         if (node)
1077                 status = of_platform_populate(node, NULL, NULL, &client->dev);
1078         else
1079                 status = add_children(pdata, irq_base, id->driver_data);
1080
1081 fail:
1082         if (status < 0)
1083                 twl_remove(client);
1084 free:
1085         if (status < 0)
1086                 platform_device_unregister(pdev);
1087
1088         return status;
1089 }
1090
1091 static const struct i2c_device_id twl_ids[] = {
1092         { "twl4030", TWL4030_VAUX2 },   /* "Triton 2" */
1093         { "twl5030", 0 },               /* T2 updated */
1094         { "twl5031", TWL5031 },         /* TWL5030 updated */
1095         { "tps65950", 0 },              /* catalog version of twl5030 */
1096         { "tps65930", TPS_SUBSET },     /* fewer LDOs and DACs; no charger */
1097         { "tps65920", TPS_SUBSET },     /* fewer LDOs; no codec or charger */
1098         { "tps65921", TPS_SUBSET },     /* fewer LDOs; no codec, no LED
1099                                            and vibrator. Charger in USB module*/
1100         { "twl6030", TWL6030_CLASS },   /* "Phoenix power chip" */
1101         { "twl6032", TWL6030_CLASS | TWL6032_SUBCLASS }, /* "Phoenix lite" */
1102         { /* end of list */ },
1103 };
1104 MODULE_DEVICE_TABLE(i2c, twl_ids);
1105
1106 /* One Client Driver , 4 Clients */
1107 static struct i2c_driver twl_driver = {
1108         .driver.name    = DRIVER_NAME,
1109         .id_table       = twl_ids,
1110         .probe          = twl_probe,
1111         .remove         = twl_remove,
1112 };
1113
1114 module_i2c_driver(twl_driver);
1115
1116 MODULE_AUTHOR("Texas Instruments, Inc.");
1117 MODULE_DESCRIPTION("I2C Core interface for TWL");
1118 MODULE_LICENSE("GPL");