Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / twl6040-core.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/err.h>
41 #include <linux/mfd/core.h>
42 #include <linux/mfd/twl6040.h>
43 #include <linux/regulator/consumer.h>
44
45 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
46 #define TWL6040_NUM_SUPPLIES    (2)
47
48 static bool twl6040_has_vibra(struct twl6040_platform_data *pdata,
49                               struct device_node *node)
50 {
51         if (pdata && pdata->vibra)
52                 return true;
53
54 #ifdef CONFIG_OF
55         if (of_find_node_by_name(node, "vibra"))
56                 return true;
57 #endif
58
59         return false;
60 }
61
62 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
63 {
64         int ret;
65         unsigned int val;
66
67         mutex_lock(&twl6040->io_mutex);
68         /* Vibra control registers from cache */
69         if (unlikely(reg == TWL6040_REG_VIBCTLL ||
70                      reg == TWL6040_REG_VIBCTLR)) {
71                 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
72         } else {
73                 ret = regmap_read(twl6040->regmap, reg, &val);
74                 if (ret < 0) {
75                         mutex_unlock(&twl6040->io_mutex);
76                         return ret;
77                 }
78         }
79         mutex_unlock(&twl6040->io_mutex);
80
81         return val;
82 }
83 EXPORT_SYMBOL(twl6040_reg_read);
84
85 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
86 {
87         int ret;
88
89         mutex_lock(&twl6040->io_mutex);
90         ret = regmap_write(twl6040->regmap, reg, val);
91         /* Cache the vibra control registers */
92         if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
93                 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
94         mutex_unlock(&twl6040->io_mutex);
95
96         return ret;
97 }
98 EXPORT_SYMBOL(twl6040_reg_write);
99
100 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
101 {
102         int ret;
103
104         mutex_lock(&twl6040->io_mutex);
105         ret = regmap_update_bits(twl6040->regmap, reg, mask, mask);
106         mutex_unlock(&twl6040->io_mutex);
107         return ret;
108 }
109 EXPORT_SYMBOL(twl6040_set_bits);
110
111 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
112 {
113         int ret;
114
115         mutex_lock(&twl6040->io_mutex);
116         ret = regmap_update_bits(twl6040->regmap, reg, mask, 0);
117         mutex_unlock(&twl6040->io_mutex);
118         return ret;
119 }
120 EXPORT_SYMBOL(twl6040_clear_bits);
121
122 /* twl6040 codec manual power-up sequence */
123 static int twl6040_power_up(struct twl6040 *twl6040)
124 {
125         u8 ldoctl, ncpctl, lppllctl;
126         int ret;
127
128         /* enable high-side LDO, reference system and internal oscillator */
129         ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
130         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
131         if (ret)
132                 return ret;
133         usleep_range(10000, 10500);
134
135         /* enable negative charge pump */
136         ncpctl = TWL6040_NCPENA;
137         ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
138         if (ret)
139                 goto ncp_err;
140         usleep_range(1000, 1500);
141
142         /* enable low-side LDO */
143         ldoctl |= TWL6040_LSLDOENA;
144         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
145         if (ret)
146                 goto lsldo_err;
147         usleep_range(1000, 1500);
148
149         /* enable low-power PLL */
150         lppllctl = TWL6040_LPLLENA;
151         ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
152         if (ret)
153                 goto lppll_err;
154         usleep_range(5000, 5500);
155
156         /* disable internal oscillator */
157         ldoctl &= ~TWL6040_OSCENA;
158         ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
159         if (ret)
160                 goto osc_err;
161
162         return 0;
163
164 osc_err:
165         lppllctl &= ~TWL6040_LPLLENA;
166         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
167 lppll_err:
168         ldoctl &= ~TWL6040_LSLDOENA;
169         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
170 lsldo_err:
171         ncpctl &= ~TWL6040_NCPENA;
172         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
173 ncp_err:
174         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
175         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
176
177         return ret;
178 }
179
180 /* twl6040 manual power-down sequence */
181 static void twl6040_power_down(struct twl6040 *twl6040)
182 {
183         u8 ncpctl, ldoctl, lppllctl;
184
185         ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
186         ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
187         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
188
189         /* enable internal oscillator */
190         ldoctl |= TWL6040_OSCENA;
191         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
192         usleep_range(1000, 1500);
193
194         /* disable low-power PLL */
195         lppllctl &= ~TWL6040_LPLLENA;
196         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
197
198         /* disable low-side LDO */
199         ldoctl &= ~TWL6040_LSLDOENA;
200         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
201
202         /* disable negative charge pump */
203         ncpctl &= ~TWL6040_NCPENA;
204         twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
205
206         /* disable high-side LDO, reference system and internal oscillator */
207         ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
208         twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
209 }
210
211 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
212 {
213         struct twl6040 *twl6040 = data;
214         u8 intid, status;
215
216         intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
217
218         if (intid & TWL6040_READYINT)
219                 complete(&twl6040->ready);
220
221         if (intid & TWL6040_THINT) {
222                 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
223                 if (status & TWL6040_TSHUTDET) {
224                         dev_warn(twl6040->dev,
225                                  "Thermal shutdown, powering-off");
226                         twl6040_power(twl6040, 0);
227                 } else {
228                         dev_warn(twl6040->dev,
229                                  "Leaving thermal shutdown, powering-on");
230                         twl6040_power(twl6040, 1);
231                 }
232         }
233
234         return IRQ_HANDLED;
235 }
236
237 static int twl6040_power_up_completion(struct twl6040 *twl6040,
238                                        int naudint)
239 {
240         int time_left;
241         u8 intid;
242
243         time_left = wait_for_completion_timeout(&twl6040->ready,
244                                                 msecs_to_jiffies(144));
245         if (!time_left) {
246                 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
247                 if (!(intid & TWL6040_READYINT)) {
248                         dev_err(twl6040->dev,
249                                 "timeout waiting for READYINT\n");
250                         return -ETIMEDOUT;
251                 }
252         }
253
254         return 0;
255 }
256
257 int twl6040_power(struct twl6040 *twl6040, int on)
258 {
259         int audpwron = twl6040->audpwron;
260         int naudint = twl6040->irq;
261         int ret = 0;
262
263         mutex_lock(&twl6040->mutex);
264
265         if (on) {
266                 /* already powered-up */
267                 if (twl6040->power_count++)
268                         goto out;
269
270                 if (gpio_is_valid(audpwron)) {
271                         /* use AUDPWRON line */
272                         gpio_set_value(audpwron, 1);
273                         /* wait for power-up completion */
274                         ret = twl6040_power_up_completion(twl6040, naudint);
275                         if (ret) {
276                                 dev_err(twl6040->dev,
277                                         "automatic power-down failed\n");
278                                 twl6040->power_count = 0;
279                                 goto out;
280                         }
281                 } else {
282                         /* use manual power-up sequence */
283                         ret = twl6040_power_up(twl6040);
284                         if (ret) {
285                                 dev_err(twl6040->dev,
286                                         "manual power-up failed\n");
287                                 twl6040->power_count = 0;
288                                 goto out;
289                         }
290                 }
291                 /* Default PLL configuration after power up */
292                 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
293                 twl6040->sysclk = 19200000;
294                 twl6040->mclk = 32768;
295         } else {
296                 /* already powered-down */
297                 if (!twl6040->power_count) {
298                         dev_err(twl6040->dev,
299                                 "device is already powered-off\n");
300                         ret = -EPERM;
301                         goto out;
302                 }
303
304                 if (--twl6040->power_count)
305                         goto out;
306
307                 if (gpio_is_valid(audpwron)) {
308                         /* use AUDPWRON line */
309                         gpio_set_value(audpwron, 0);
310
311                         /* power-down sequence latency */
312                         usleep_range(500, 700);
313                 } else {
314                         /* use manual power-down sequence */
315                         twl6040_power_down(twl6040);
316                 }
317                 twl6040->sysclk = 0;
318                 twl6040->mclk = 0;
319         }
320
321 out:
322         mutex_unlock(&twl6040->mutex);
323         return ret;
324 }
325 EXPORT_SYMBOL(twl6040_power);
326
327 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
328                     unsigned int freq_in, unsigned int freq_out)
329 {
330         u8 hppllctl, lppllctl;
331         int ret = 0;
332
333         mutex_lock(&twl6040->mutex);
334
335         hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
336         lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
337
338         /* Force full reconfiguration when switching between PLL */
339         if (pll_id != twl6040->pll) {
340                 twl6040->sysclk = 0;
341                 twl6040->mclk = 0;
342         }
343
344         switch (pll_id) {
345         case TWL6040_SYSCLK_SEL_LPPLL:
346                 /* low-power PLL divider */
347                 /* Change the sysclk configuration only if it has been canged */
348                 if (twl6040->sysclk != freq_out) {
349                         switch (freq_out) {
350                         case 17640000:
351                                 lppllctl |= TWL6040_LPLLFIN;
352                                 break;
353                         case 19200000:
354                                 lppllctl &= ~TWL6040_LPLLFIN;
355                                 break;
356                         default:
357                                 dev_err(twl6040->dev,
358                                         "freq_out %d not supported\n",
359                                         freq_out);
360                                 ret = -EINVAL;
361                                 goto pll_out;
362                         }
363                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
364                                           lppllctl);
365                 }
366
367                 /* The PLL in use has not been change, we can exit */
368                 if (twl6040->pll == pll_id)
369                         break;
370
371                 switch (freq_in) {
372                 case 32768:
373                         lppllctl |= TWL6040_LPLLENA;
374                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
375                                           lppllctl);
376                         mdelay(5);
377                         lppllctl &= ~TWL6040_HPLLSEL;
378                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
379                                           lppllctl);
380                         hppllctl &= ~TWL6040_HPLLENA;
381                         twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
382                                           hppllctl);
383                         break;
384                 default:
385                         dev_err(twl6040->dev,
386                                 "freq_in %d not supported\n", freq_in);
387                         ret = -EINVAL;
388                         goto pll_out;
389                 }
390                 break;
391         case TWL6040_SYSCLK_SEL_HPPLL:
392                 /* high-performance PLL can provide only 19.2 MHz */
393                 if (freq_out != 19200000) {
394                         dev_err(twl6040->dev,
395                                 "freq_out %d not supported\n", freq_out);
396                         ret = -EINVAL;
397                         goto pll_out;
398                 }
399
400                 if (twl6040->mclk != freq_in) {
401                         hppllctl &= ~TWL6040_MCLK_MSK;
402
403                         switch (freq_in) {
404                         case 12000000:
405                                 /* PLL enabled, active mode */
406                                 hppllctl |= TWL6040_MCLK_12000KHZ |
407                                             TWL6040_HPLLENA;
408                                 break;
409                         case 19200000:
410                                 /*
411                                 * PLL disabled
412                                 * (enable PLL if MCLK jitter quality
413                                 *  doesn't meet specification)
414                                 */
415                                 hppllctl |= TWL6040_MCLK_19200KHZ;
416                                 break;
417                         case 26000000:
418                                 /* PLL enabled, active mode */
419                                 hppllctl |= TWL6040_MCLK_26000KHZ |
420                                             TWL6040_HPLLENA;
421                                 break;
422                         case 38400000:
423                                 /* PLL enabled, active mode */
424                                 hppllctl |= TWL6040_MCLK_38400KHZ |
425                                             TWL6040_HPLLENA;
426                                 break;
427                         default:
428                                 dev_err(twl6040->dev,
429                                         "freq_in %d not supported\n", freq_in);
430                                 ret = -EINVAL;
431                                 goto pll_out;
432                         }
433
434                         /*
435                          * enable clock slicer to ensure input waveform is
436                          * square
437                          */
438                         hppllctl |= TWL6040_HPLLSQRENA;
439
440                         twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
441                                           hppllctl);
442                         usleep_range(500, 700);
443                         lppllctl |= TWL6040_HPLLSEL;
444                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
445                                           lppllctl);
446                         lppllctl &= ~TWL6040_LPLLENA;
447                         twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
448                                           lppllctl);
449                 }
450                 break;
451         default:
452                 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
453                 ret = -EINVAL;
454                 goto pll_out;
455         }
456
457         twl6040->sysclk = freq_out;
458         twl6040->mclk = freq_in;
459         twl6040->pll = pll_id;
460
461 pll_out:
462         mutex_unlock(&twl6040->mutex);
463         return ret;
464 }
465 EXPORT_SYMBOL(twl6040_set_pll);
466
467 int twl6040_get_pll(struct twl6040 *twl6040)
468 {
469         if (twl6040->power_count)
470                 return twl6040->pll;
471         else
472                 return -ENODEV;
473 }
474 EXPORT_SYMBOL(twl6040_get_pll);
475
476 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
477 {
478         return twl6040->sysclk;
479 }
480 EXPORT_SYMBOL(twl6040_get_sysclk);
481
482 /* Get the combined status of the vibra control register */
483 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
484 {
485         u8 status;
486
487         status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
488         status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
489
490         return status;
491 }
492 EXPORT_SYMBOL(twl6040_get_vibralr_status);
493
494 static struct resource twl6040_vibra_rsrc[] = {
495         {
496                 .flags = IORESOURCE_IRQ,
497         },
498 };
499
500 static struct resource twl6040_codec_rsrc[] = {
501         {
502                 .flags = IORESOURCE_IRQ,
503         },
504 };
505
506 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
507 {
508         /* Register 0 is not readable */
509         if (!reg)
510                 return false;
511         return true;
512 }
513
514 static struct regmap_config twl6040_regmap_config = {
515         .reg_bits = 8,
516         .val_bits = 8,
517         .max_register = TWL6040_REG_STATUS, /* 0x2e */
518
519         .readable_reg = twl6040_readable_reg,
520 };
521
522 static int __devinit twl6040_probe(struct i2c_client *client,
523                                      const struct i2c_device_id *id)
524 {
525         struct twl6040_platform_data *pdata = client->dev.platform_data;
526         struct device_node *node = client->dev.of_node;
527         struct twl6040 *twl6040;
528         struct mfd_cell *cell = NULL;
529         int irq, ret, children = 0;
530
531         if (!pdata && !node) {
532                 dev_err(&client->dev, "Platform data is missing\n");
533                 return -EINVAL;
534         }
535
536         /* In order to operate correctly we need valid interrupt config */
537         if (!client->irq) {
538                 dev_err(&client->dev, "Invalid IRQ configuration\n");
539                 return -EINVAL;
540         }
541
542         twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
543                                GFP_KERNEL);
544         if (!twl6040) {
545                 ret = -ENOMEM;
546                 goto err;
547         }
548
549         twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
550         if (IS_ERR(twl6040->regmap)) {
551                 ret = PTR_ERR(twl6040->regmap);
552                 goto err;
553         }
554
555         i2c_set_clientdata(client, twl6040);
556
557         twl6040->supplies[0].supply = "vio";
558         twl6040->supplies[1].supply = "v2v1";
559         ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
560                                  twl6040->supplies);
561         if (ret != 0) {
562                 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
563                 goto regulator_get_err;
564         }
565
566         ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
567         if (ret != 0) {
568                 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
569                 goto power_err;
570         }
571
572         twl6040->dev = &client->dev;
573         twl6040->irq = client->irq;
574
575         mutex_init(&twl6040->mutex);
576         mutex_init(&twl6040->io_mutex);
577         init_completion(&twl6040->ready);
578
579         twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
580
581         /* ERRATA: Automatic power-up is not possible in ES1.0 */
582         if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
583                 if (pdata)
584                         twl6040->audpwron = pdata->audpwron_gpio;
585                 else
586                         twl6040->audpwron = of_get_named_gpio(node,
587                                                 "ti,audpwron-gpio", 0);
588         } else
589                 twl6040->audpwron = -EINVAL;
590
591         if (gpio_is_valid(twl6040->audpwron)) {
592                 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
593                                        "audpwron");
594                 if (ret)
595                         goto gpio_err;
596         }
597
598         /* codec interrupt */
599         ret = twl6040_irq_init(twl6040);
600         if (ret)
601                 goto irq_init_err;
602
603         ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
604                                    NULL, twl6040_naudint_handler, 0,
605                                    "twl6040_irq_ready", twl6040);
606         if (ret) {
607                 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
608                         ret);
609                 goto irq_err;
610         }
611
612         /* dual-access registers controlled by I2C only */
613         twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
614
615         /*
616          * The main functionality of twl6040 to provide audio on OMAP4+ systems.
617          * We can add the ASoC codec child whenever this driver has been loaded.
618          * The ASoC codec can work without pdata, pass the platform_data only if
619          * it has been provided.
620          */
621         irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
622         cell = &twl6040->cells[children];
623         cell->name = "twl6040-codec";
624         twl6040_codec_rsrc[0].start = irq;
625         twl6040_codec_rsrc[0].end = irq;
626         cell->resources = twl6040_codec_rsrc;
627         cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
628         if (pdata && pdata->codec) {
629                 cell->platform_data = pdata->codec;
630                 cell->pdata_size = sizeof(*pdata->codec);
631         }
632         children++;
633
634         if (twl6040_has_vibra(pdata, node)) {
635                 irq = twl6040->irq_base + TWL6040_IRQ_VIB;
636
637                 cell = &twl6040->cells[children];
638                 cell->name = "twl6040-vibra";
639                 twl6040_vibra_rsrc[0].start = irq;
640                 twl6040_vibra_rsrc[0].end = irq;
641                 cell->resources = twl6040_vibra_rsrc;
642                 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
643
644                 if (pdata && pdata->vibra) {
645                         cell->platform_data = pdata->vibra;
646                         cell->pdata_size = sizeof(*pdata->vibra);
647                 }
648                 children++;
649         }
650
651         ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
652                               NULL, 0);
653         if (ret)
654                 goto mfd_err;
655
656         return 0;
657
658 mfd_err:
659         free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
660 irq_err:
661         twl6040_irq_exit(twl6040);
662 irq_init_err:
663         if (gpio_is_valid(twl6040->audpwron))
664                 gpio_free(twl6040->audpwron);
665 gpio_err:
666         regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
667 power_err:
668         regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
669 regulator_get_err:
670         i2c_set_clientdata(client, NULL);
671 err:
672         return ret;
673 }
674
675 static int __devexit twl6040_remove(struct i2c_client *client)
676 {
677         struct twl6040 *twl6040 = i2c_get_clientdata(client);
678
679         if (twl6040->power_count)
680                 twl6040_power(twl6040, 0);
681
682         if (gpio_is_valid(twl6040->audpwron))
683                 gpio_free(twl6040->audpwron);
684
685         free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
686         twl6040_irq_exit(twl6040);
687
688         mfd_remove_devices(&client->dev);
689         i2c_set_clientdata(client, NULL);
690
691         regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
692         regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
693
694         return 0;
695 }
696
697 static const struct i2c_device_id twl6040_i2c_id[] = {
698         { "twl6040", 0, },
699         { },
700 };
701 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
702
703 static struct i2c_driver twl6040_driver = {
704         .driver = {
705                 .name = "twl6040",
706                 .owner = THIS_MODULE,
707         },
708         .probe          = twl6040_probe,
709         .remove         = __devexit_p(twl6040_remove),
710         .id_table       = twl6040_i2c_id,
711 };
712
713 module_i2c_driver(twl6040_driver);
714
715 MODULE_DESCRIPTION("TWL6040 MFD");
716 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
717 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
718 MODULE_LICENSE("GPL");
719 MODULE_ALIAS("platform:twl6040");