GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / mfd / twl6040.c
1 /*
2  * MFD driver for TWL6040 audio device
3  *
4  * Authors:     Misael Lopez Cruz <misael.lopez@ti.com>
5  *              Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
6  *              Peter Ujfalusi <peter.ujfalusi@ti.com>
7  *
8  * Copyright:   (C) 2011 Texas Instruments, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  *
24  */
25
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/kernel.h>
30 #include <linux/err.h>
31 #include <linux/platform_device.h>
32 #include <linux/of.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_gpio.h>
35 #include <linux/of_platform.h>
36 #include <linux/gpio.h>
37 #include <linux/delay.h>
38 #include <linux/i2c.h>
39 #include <linux/regmap.h>
40 #include <linux/mfd/core.h>
41 #include <linux/mfd/twl6040.h>
42 #include <linux/regulator/consumer.h>
43
44 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
45 #define TWL6040_NUM_SUPPLIES    (2)
46
47 static const struct reg_default twl6040_defaults[] = {
48         { 0x01, 0x4B }, /* REG_ASICID   (ro) */
49         { 0x02, 0x00 }, /* REG_ASICREV  (ro) */
50         { 0x03, 0x00 }, /* REG_INTID    */
51         { 0x04, 0x00 }, /* REG_INTMR    */
52         { 0x05, 0x00 }, /* REG_NCPCTRL  */
53         { 0x06, 0x00 }, /* REG_LDOCTL   */
54         { 0x07, 0x60 }, /* REG_HPPLLCTL */
55         { 0x08, 0x00 }, /* REG_LPPLLCTL */
56         { 0x09, 0x4A }, /* REG_LPPLLDIV */
57         { 0x0A, 0x00 }, /* REG_AMICBCTL */
58         { 0x0B, 0x00 }, /* REG_DMICBCTL */
59         { 0x0C, 0x00 }, /* REG_MICLCTL  */
60         { 0x0D, 0x00 }, /* REG_MICRCTL  */
61         { 0x0E, 0x00 }, /* REG_MICGAIN  */
62         { 0x0F, 0x1B }, /* REG_LINEGAIN */
63         { 0x10, 0x00 }, /* REG_HSLCTL   */
64         { 0x11, 0x00 }, /* REG_HSRCTL   */
65         { 0x12, 0x00 }, /* REG_HSGAIN   */
66         { 0x13, 0x00 }, /* REG_EARCTL   */
67         { 0x14, 0x00 }, /* REG_HFLCTL   */
68         { 0x15, 0x00 }, /* REG_HFLGAIN  */
69         { 0x16, 0x00 }, /* REG_HFRCTL   */
70         { 0x17, 0x00 }, /* REG_HFRGAIN  */
71         { 0x18, 0x00 }, /* REG_VIBCTLL  */
72         { 0x19, 0x00 }, /* REG_VIBDATL  */
73         { 0x1A, 0x00 }, /* REG_VIBCTLR  */
74         { 0x1B, 0x00 }, /* REG_VIBDATR  */
75         { 0x1C, 0x00 }, /* REG_HKCTL1   */
76         { 0x1D, 0x00 }, /* REG_HKCTL2   */
77         { 0x1E, 0x00 }, /* REG_GPOCTL   */
78         { 0x1F, 0x00 }, /* REG_ALB      */
79         { 0x20, 0x00 }, /* REG_DLB      */
80         /* 0x28, REG_TRIM1 */
81         /* 0x29, REG_TRIM2 */
82         /* 0x2A, REG_TRIM3 */
83         /* 0x2B, REG_HSOTRIM */
84         /* 0x2C, REG_HFOTRIM */
85         { 0x2D, 0x08 }, /* REG_ACCCTL   */
86         { 0x2E, 0x00 }, /* REG_STATUS   (ro) */
87 };
88
89 static struct reg_sequence twl6040_patch[] = {
90         /*
91          * Select I2C bus access to dual access registers
92          * Interrupt register is cleared on read
93          * Select fast mode for i2c (400KHz)
94          */
95         { TWL6040_REG_ACCCTL,
96                 TWL6040_I2CSEL | TWL6040_INTCLRMODE | TWL6040_I2CMODE(1) },
97 };
98
99
100 static bool twl6040_has_vibra(struct device_node *parent)
101 {
102         struct device_node *node;
103
104         node = of_get_child_by_name(parent, "vibra");
105         if (node) {
106                 of_node_put(node);
107                 return true;
108         }
109
110         return false;
111 }
112
113 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
114 {
115         int ret;
116         unsigned int val;
117
118         ret = regmap_read(twl6040->regmap, reg, &val);
119         if (ret < 0)
120                 return ret;
121
122         return val;
123 }
124 EXPORT_SYMBOL(twl6040_reg_read);
125
126 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
127 {
128         int ret;
129
130         ret = regmap_write(twl6040->regmap, reg, val);
131
132         return ret;
133 }
134 EXPORT_SYMBOL(twl6040_reg_write);
135
136 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
137 {
138         return regmap_update_bits(twl6040->regmap, reg, mask, mask);
139 }
140 EXPORT_SYMBOL(twl6040_set_bits);
141
142 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
143 {
144         return regmap_update_bits(twl6040->regmap, reg, mask, 0);
145 }
146 EXPORT_SYMBOL(twl6040_clear_bits);
147
148 /* twl6040 codec manual power-up sequence */
149 static int twl6040_power_up_manual(struct twl6040 *twl6040)
150 {
151         u8 ldoctl, ncpctl, lppllctl;
152         int ret;
153
154         /* enable high-side LDO, reference system and internal oscillator */
155         ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
156         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
157         if (ret)
158                 return ret;
159         usleep_range(10000, 10500);
160
161         /* enable negative charge pump */
162         ncpctl = TWL6040_NCPENA;
163         ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
164         if (ret)
165                 goto ncp_err;
166         usleep_range(1000, 1500);
167
168         /* enable low-side LDO */
169         ldoctl |= TWL6040_LSLDOENA;
170         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171         if (ret)
172                 goto lsldo_err;
173         usleep_range(1000, 1500);
174
175         /* enable low-power PLL */
176         lppllctl = TWL6040_LPLLENA;
177         ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
178         if (ret)
179                 goto lppll_err;
180         usleep_range(5000, 5500);
181
182         /* disable internal oscillator */
183         ldoctl &= ~TWL6040_OSCENA;
184         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
185         if (ret)
186                 goto osc_err;
187
188         return 0;
189
190 osc_err:
191         lppllctl &= ~TWL6040_LPLLENA;
192         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
193 lppll_err:
194         ldoctl &= ~TWL6040_LSLDOENA;
195         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
196 lsldo_err:
197         ncpctl &= ~TWL6040_NCPENA;
198         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
199 ncp_err:
200         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
201         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
202
203         dev_err(twl6040->dev, "manual power-up failed\n");
204         return ret;
205 }
206
207 /* twl6040 manual power-down sequence */
208 static void twl6040_power_down_manual(struct twl6040 *twl6040)
209 {
210         u8 ncpctl, ldoctl, lppllctl;
211
212         ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
213         ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
214         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
215
216         /* enable internal oscillator */
217         ldoctl |= TWL6040_OSCENA;
218         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
219         usleep_range(1000, 1500);
220
221         /* disable low-power PLL */
222         lppllctl &= ~TWL6040_LPLLENA;
223         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
224
225         /* disable low-side LDO */
226         ldoctl &= ~TWL6040_LSLDOENA;
227         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
228
229         /* disable negative charge pump */
230         ncpctl &= ~TWL6040_NCPENA;
231         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
232
233         /* disable high-side LDO, reference system and internal oscillator */
234         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
235         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
236 }
237
238 static irqreturn_t twl6040_readyint_handler(int irq, void *data)
239 {
240         struct twl6040 *twl6040 = data;
241
242         complete(&twl6040->ready);
243
244         return IRQ_HANDLED;
245 }
246
247 static irqreturn_t twl6040_thint_handler(int irq, void *data)
248 {
249         struct twl6040 *twl6040 = data;
250         u8 status;
251
252         status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
253         if (status & TWL6040_TSHUTDET) {
254                 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
255                 twl6040_power(twl6040, 0);
256         } else {
257                 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
258                 twl6040_power(twl6040, 1);
259         }
260
261         return IRQ_HANDLED;
262 }
263
264 static int twl6040_power_up_automatic(struct twl6040 *twl6040)
265 {
266         int time_left;
267
268         gpio_set_value(twl6040->audpwron, 1);
269
270         time_left = wait_for_completion_timeout(&twl6040->ready,
271                                                 msecs_to_jiffies(144));
272         if (!time_left) {
273                 u8 intid;
274
275                 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
276                 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
277                 if (!(intid & TWL6040_READYINT)) {
278                         dev_err(twl6040->dev, "automatic power-up failed\n");
279                         gpio_set_value(twl6040->audpwron, 0);
280                         return -ETIMEDOUT;
281                 }
282         }
283
284         return 0;
285 }
286
287 int twl6040_power(struct twl6040 *twl6040, int on)
288 {
289         int ret = 0;
290
291         mutex_lock(&twl6040->mutex);
292
293         if (on) {
294                 /* already powered-up */
295                 if (twl6040->power_count++)
296                         goto out;
297
298                 clk_prepare_enable(twl6040->clk32k);
299
300                 /* Allow writes to the chip */
301                 regcache_cache_only(twl6040->regmap, false);
302
303                 if (gpio_is_valid(twl6040->audpwron)) {
304                         /* use automatic power-up sequence */
305                         ret = twl6040_power_up_automatic(twl6040);
306                         if (ret) {
307                                 twl6040->power_count = 0;
308                                 goto out;
309                         }
310                 } else {
311                         /* use manual power-up sequence */
312                         ret = twl6040_power_up_manual(twl6040);
313                         if (ret) {
314                                 twl6040->power_count = 0;
315                                 goto out;
316                         }
317                 }
318
319                 /*
320                  * Register access can produce errors after power-up unless we
321                  * wait at least 8ms based on measurements on duovero.
322                  */
323                 usleep_range(10000, 12000);
324
325                 /* Sync with the HW */
326                 ret = regcache_sync(twl6040->regmap);
327                 if (ret) {
328                         dev_err(twl6040->dev, "Failed to sync with the HW: %i\n",
329                                 ret);
330                         goto out;
331                 }
332
333                 /* Default PLL configuration after power up */
334                 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
335                 twl6040->sysclk = 19200000;
336                 twl6040->mclk = 32768;
337         } else {
338                 /* already powered-down */
339                 if (!twl6040->power_count) {
340                         dev_err(twl6040->dev,
341                                 "device is already powered-off\n");
342                         ret = -EPERM;
343                         goto out;
344                 }
345
346                 if (--twl6040->power_count)
347                         goto out;
348
349                 if (gpio_is_valid(twl6040->audpwron)) {
350                         /* use AUDPWRON line */
351                         gpio_set_value(twl6040->audpwron, 0);
352
353                         /* power-down sequence latency */
354                         usleep_range(500, 700);
355                 } else {
356                         /* use manual power-down sequence */
357                         twl6040_power_down_manual(twl6040);
358                 }
359
360                 /* Set regmap to cache only and mark it as dirty */
361                 regcache_cache_only(twl6040->regmap, true);
362                 regcache_mark_dirty(twl6040->regmap);
363
364                 twl6040->sysclk = 0;
365                 twl6040->mclk = 0;
366
367                 clk_disable_unprepare(twl6040->clk32k);
368         }
369
370 out:
371         mutex_unlock(&twl6040->mutex);
372         return ret;
373 }
374 EXPORT_SYMBOL(twl6040_power);
375
376 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
377                     unsigned int freq_in, unsigned int freq_out)
378 {
379         u8 hppllctl, lppllctl;
380         int ret = 0;
381
382         mutex_lock(&twl6040->mutex);
383
384         hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
385         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
386
387         /* Force full reconfiguration when switching between PLL */
388         if (pll_id != twl6040->pll) {
389                 twl6040->sysclk = 0;
390                 twl6040->mclk = 0;
391         }
392
393         switch (pll_id) {
394         case TWL6040_SYSCLK_SEL_LPPLL:
395                 /* low-power PLL divider */
396                 /* Change the sysclk configuration only if it has been canged */
397                 if (twl6040->sysclk != freq_out) {
398                         switch (freq_out) {
399                         case 17640000:
400                                 lppllctl |= TWL6040_LPLLFIN;
401                                 break;
402                         case 19200000:
403                                 lppllctl &= ~TWL6040_LPLLFIN;
404                                 break;
405                         default:
406                                 dev_err(twl6040->dev,
407                                         "freq_out %d not supported\n",
408                                         freq_out);
409                                 ret = -EINVAL;
410                                 goto pll_out;
411                         }
412                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
413                                           lppllctl);
414                 }
415
416                 /* The PLL in use has not been change, we can exit */
417                 if (twl6040->pll == pll_id)
418                         break;
419
420                 switch (freq_in) {
421                 case 32768:
422                         lppllctl |= TWL6040_LPLLENA;
423                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
424                                           lppllctl);
425                         mdelay(5);
426                         lppllctl &= ~TWL6040_HPLLSEL;
427                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
428                                           lppllctl);
429                         hppllctl &= ~TWL6040_HPLLENA;
430                         twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
431                                           hppllctl);
432                         break;
433                 default:
434                         dev_err(twl6040->dev,
435                                 "freq_in %d not supported\n", freq_in);
436                         ret = -EINVAL;
437                         goto pll_out;
438                 }
439                 break;
440         case TWL6040_SYSCLK_SEL_HPPLL:
441                 /* high-performance PLL can provide only 19.2 MHz */
442                 if (freq_out != 19200000) {
443                         dev_err(twl6040->dev,
444                                 "freq_out %d not supported\n", freq_out);
445                         ret = -EINVAL;
446                         goto pll_out;
447                 }
448
449                 if (twl6040->mclk != freq_in) {
450                         hppllctl &= ~TWL6040_MCLK_MSK;
451
452                         switch (freq_in) {
453                         case 12000000:
454                                 /* PLL enabled, active mode */
455                                 hppllctl |= TWL6040_MCLK_12000KHZ |
456                                             TWL6040_HPLLENA;
457                                 break;
458                         case 19200000:
459                                 /* PLL enabled, bypass mode */
460                                 hppllctl |= TWL6040_MCLK_19200KHZ |
461                                             TWL6040_HPLLBP | TWL6040_HPLLENA;
462                                 break;
463                         case 26000000:
464                                 /* PLL enabled, active mode */
465                                 hppllctl |= TWL6040_MCLK_26000KHZ |
466                                             TWL6040_HPLLENA;
467                                 break;
468                         case 38400000:
469                                 /* PLL enabled, bypass mode */
470                                 hppllctl |= TWL6040_MCLK_38400KHZ |
471                                             TWL6040_HPLLBP | TWL6040_HPLLENA;
472                                 break;
473                         default:
474                                 dev_err(twl6040->dev,
475                                         "freq_in %d not supported\n", freq_in);
476                                 ret = -EINVAL;
477                                 goto pll_out;
478                         }
479
480                         /*
481                          * enable clock slicer to ensure input waveform is
482                          * square
483                          */
484                         hppllctl |= TWL6040_HPLLSQRENA;
485
486                         twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
487                                           hppllctl);
488                         usleep_range(500, 700);
489                         lppllctl |= TWL6040_HPLLSEL;
490                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
491                                           lppllctl);
492                         lppllctl &= ~TWL6040_LPLLENA;
493                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
494                                           lppllctl);
495                 }
496                 break;
497         default:
498                 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
499                 ret = -EINVAL;
500                 goto pll_out;
501         }
502
503         twl6040->sysclk = freq_out;
504         twl6040->mclk = freq_in;
505         twl6040->pll = pll_id;
506
507 pll_out:
508         mutex_unlock(&twl6040->mutex);
509         return ret;
510 }
511 EXPORT_SYMBOL(twl6040_set_pll);
512
513 int twl6040_get_pll(struct twl6040 *twl6040)
514 {
515         if (twl6040->power_count)
516                 return twl6040->pll;
517         else
518                 return -ENODEV;
519 }
520 EXPORT_SYMBOL(twl6040_get_pll);
521
522 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
523 {
524         return twl6040->sysclk;
525 }
526 EXPORT_SYMBOL(twl6040_get_sysclk);
527
528 /* Get the combined status of the vibra control register */
529 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
530 {
531         unsigned int reg;
532         int ret;
533         u8 status;
534
535         ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, &reg);
536         if (ret != 0)
537                 return ret;
538         status = reg;
539
540         ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, &reg);
541         if (ret != 0)
542                 return ret;
543         status |= reg;
544
545         status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
546
547         return status;
548 }
549 EXPORT_SYMBOL(twl6040_get_vibralr_status);
550
551 static struct resource twl6040_vibra_rsrc[] = {
552         {
553                 .flags = IORESOURCE_IRQ,
554         },
555 };
556
557 static struct resource twl6040_codec_rsrc[] = {
558         {
559                 .flags = IORESOURCE_IRQ,
560         },
561 };
562
563 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
564 {
565         /* Register 0 is not readable */
566         if (!reg)
567                 return false;
568         return true;
569 }
570
571 static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
572 {
573         switch (reg) {
574         case TWL6040_REG_ASICID:
575         case TWL6040_REG_ASICREV:
576         case TWL6040_REG_INTID:
577         case TWL6040_REG_LPPLLCTL:
578         case TWL6040_REG_HPPLLCTL:
579         case TWL6040_REG_STATUS:
580                 return true;
581         default:
582                 return false;
583         }
584 }
585
586 static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
587 {
588         switch (reg) {
589         case TWL6040_REG_ASICID:
590         case TWL6040_REG_ASICREV:
591         case TWL6040_REG_STATUS:
592                 return false;
593         default:
594                 return true;
595         }
596 }
597
598 static const struct regmap_config twl6040_regmap_config = {
599         .reg_bits = 8,
600         .val_bits = 8,
601
602         .reg_defaults = twl6040_defaults,
603         .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
604
605         .max_register = TWL6040_REG_STATUS, /* 0x2e */
606
607         .readable_reg = twl6040_readable_reg,
608         .volatile_reg = twl6040_volatile_reg,
609         .writeable_reg = twl6040_writeable_reg,
610
611         .cache_type = REGCACHE_RBTREE,
612 };
613
614 static const struct regmap_irq twl6040_irqs[] = {
615         { .reg_offset = 0, .mask = TWL6040_THINT, },
616         { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
617         { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
618         { .reg_offset = 0, .mask = TWL6040_HFINT, },
619         { .reg_offset = 0, .mask = TWL6040_VIBINT, },
620         { .reg_offset = 0, .mask = TWL6040_READYINT, },
621 };
622
623 static struct regmap_irq_chip twl6040_irq_chip = {
624         .name = "twl6040",
625         .irqs = twl6040_irqs,
626         .num_irqs = ARRAY_SIZE(twl6040_irqs),
627
628         .num_regs = 1,
629         .status_base = TWL6040_REG_INTID,
630         .mask_base = TWL6040_REG_INTMR,
631 };
632
633 static int twl6040_probe(struct i2c_client *client,
634                          const struct i2c_device_id *id)
635 {
636         struct device_node *node = client->dev.of_node;
637         struct twl6040 *twl6040;
638         struct mfd_cell *cell = NULL;
639         int irq, ret, children = 0;
640
641         if (!node) {
642                 dev_err(&client->dev, "of node is missing\n");
643                 return -EINVAL;
644         }
645
646         /* In order to operate correctly we need valid interrupt config */
647         if (!client->irq) {
648                 dev_err(&client->dev, "Invalid IRQ configuration\n");
649                 return -EINVAL;
650         }
651
652         twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
653                                GFP_KERNEL);
654         if (!twl6040)
655                 return -ENOMEM;
656
657         twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
658         if (IS_ERR(twl6040->regmap))
659                 return PTR_ERR(twl6040->regmap);
660
661         i2c_set_clientdata(client, twl6040);
662
663         twl6040->clk32k = devm_clk_get(&client->dev, "clk32k");
664         if (IS_ERR(twl6040->clk32k)) {
665                 if (PTR_ERR(twl6040->clk32k) == -EPROBE_DEFER)
666                         return -EPROBE_DEFER;
667                 dev_info(&client->dev, "clk32k is not handled\n");
668                 twl6040->clk32k = NULL;
669         }
670
671         twl6040->supplies[0].supply = "vio";
672         twl6040->supplies[1].supply = "v2v1";
673         ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
674                                       twl6040->supplies);
675         if (ret != 0) {
676                 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
677                 return ret;
678         }
679
680         ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
681         if (ret != 0) {
682                 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
683                 return ret;
684         }
685
686         twl6040->dev = &client->dev;
687         twl6040->irq = client->irq;
688
689         mutex_init(&twl6040->mutex);
690         init_completion(&twl6040->ready);
691
692         regmap_register_patch(twl6040->regmap, twl6040_patch,
693                               ARRAY_SIZE(twl6040_patch));
694
695         twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
696         if (twl6040->rev < 0) {
697                 dev_err(&client->dev, "Failed to read revision register: %d\n",
698                         twl6040->rev);
699                 ret = twl6040->rev;
700                 goto gpio_err;
701         }
702
703         /* ERRATA: Automatic power-up is not possible in ES1.0 */
704         if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
705                 twl6040->audpwron = of_get_named_gpio(node,
706                                                       "ti,audpwron-gpio", 0);
707         else
708                 twl6040->audpwron = -EINVAL;
709
710         if (gpio_is_valid(twl6040->audpwron)) {
711                 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
712                                             GPIOF_OUT_INIT_LOW, "audpwron");
713                 if (ret)
714                         goto gpio_err;
715
716                 /* Clear any pending interrupt */
717                 twl6040_reg_read(twl6040, TWL6040_REG_INTID);
718         }
719
720         ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
721                                   0, &twl6040_irq_chip, &twl6040->irq_data);
722         if (ret < 0)
723                 goto gpio_err;
724
725         twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
726                                                  TWL6040_IRQ_READY);
727         twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
728                                               TWL6040_IRQ_TH);
729
730         ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
731                                         twl6040_readyint_handler, IRQF_ONESHOT,
732                                         "twl6040_irq_ready", twl6040);
733         if (ret) {
734                 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
735                 goto readyirq_err;
736         }
737
738         ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
739                                         twl6040_thint_handler, IRQF_ONESHOT,
740                                         "twl6040_irq_th", twl6040);
741         if (ret) {
742                 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
743                 goto readyirq_err;
744         }
745
746         /*
747          * The main functionality of twl6040 to provide audio on OMAP4+ systems.
748          * We can add the ASoC codec child whenever this driver has been loaded.
749          */
750         irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
751         cell = &twl6040->cells[children];
752         cell->name = "twl6040-codec";
753         twl6040_codec_rsrc[0].start = irq;
754         twl6040_codec_rsrc[0].end = irq;
755         cell->resources = twl6040_codec_rsrc;
756         cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
757         children++;
758
759         /* Vibra input driver support */
760         if (twl6040_has_vibra(node)) {
761                 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
762
763                 cell = &twl6040->cells[children];
764                 cell->name = "twl6040-vibra";
765                 twl6040_vibra_rsrc[0].start = irq;
766                 twl6040_vibra_rsrc[0].end = irq;
767                 cell->resources = twl6040_vibra_rsrc;
768                 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
769                 children++;
770         }
771
772         /* GPO support */
773         cell = &twl6040->cells[children];
774         cell->name = "twl6040-gpo";
775         children++;
776
777         /* The chip is powered down so mark regmap to cache only and dirty */
778         regcache_cache_only(twl6040->regmap, true);
779         regcache_mark_dirty(twl6040->regmap);
780
781         ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
782                               NULL, 0, NULL);
783         if (ret)
784                 goto readyirq_err;
785
786         return 0;
787
788 readyirq_err:
789         regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
790 gpio_err:
791         regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
792         return ret;
793 }
794
795 static int twl6040_remove(struct i2c_client *client)
796 {
797         struct twl6040 *twl6040 = i2c_get_clientdata(client);
798
799         if (twl6040->power_count)
800                 twl6040_power(twl6040, 0);
801
802         regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
803
804         mfd_remove_devices(&client->dev);
805
806         regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
807
808         return 0;
809 }
810
811 static const struct i2c_device_id twl6040_i2c_id[] = {
812         { "twl6040", 0, },
813         { "twl6041", 0, },
814         { },
815 };
816 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
817
818 static struct i2c_driver twl6040_driver = {
819         .driver = {
820                 .name = "twl6040",
821         },
822         .probe          = twl6040_probe,
823         .remove         = twl6040_remove,
824         .id_table       = twl6040_i2c_id,
825 };
826
827 module_i2c_driver(twl6040_driver);
828
829 MODULE_DESCRIPTION("TWL6040 MFD");
830 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
831 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
832 MODULE_LICENSE("GPL");