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