blob: 2d6bedadca096d68eec4ce61332b57d6f08140a8 [file] [log] [blame]
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -05001/*
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/platform_device.h>
31#include <linux/gpio.h>
32#include <linux/delay.h>
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030033#include <linux/i2c.h>
34#include <linux/regmap.h>
35#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050036#include <linux/mfd/core.h>
37#include <linux/mfd/twl6040.h>
38
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030039#define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
40
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050041int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
42{
43 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030044 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050045
46 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030047 /* Vibra control registers from cache */
48 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
49 reg == TWL6040_REG_VIBCTLR)) {
50 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
51 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030052 ret = regmap_read(twl6040->regmap, reg, &val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030053 if (ret < 0) {
54 mutex_unlock(&twl6040->io_mutex);
55 return ret;
56 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050057 }
58 mutex_unlock(&twl6040->io_mutex);
59
60 return val;
61}
62EXPORT_SYMBOL(twl6040_reg_read);
63
64int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
65{
66 int ret;
67
68 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030069 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030070 /* Cache the vibra control registers */
71 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
72 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050073 mutex_unlock(&twl6040->io_mutex);
74
75 return ret;
76}
77EXPORT_SYMBOL(twl6040_reg_write);
78
79int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
80{
81 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050082
83 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030084 ret = regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050085 mutex_unlock(&twl6040->io_mutex);
86 return ret;
87}
88EXPORT_SYMBOL(twl6040_set_bits);
89
90int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
91{
92 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050093
94 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030095 ret = regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050096 mutex_unlock(&twl6040->io_mutex);
97 return ret;
98}
99EXPORT_SYMBOL(twl6040_clear_bits);
100
101/* twl6040 codec manual power-up sequence */
102static int twl6040_power_up(struct twl6040 *twl6040)
103{
104 u8 ldoctl, ncpctl, lppllctl;
105 int ret;
106
107 /* enable high-side LDO, reference system and internal oscillator */
108 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
109 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
110 if (ret)
111 return ret;
112 usleep_range(10000, 10500);
113
114 /* enable negative charge pump */
115 ncpctl = TWL6040_NCPENA;
116 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
117 if (ret)
118 goto ncp_err;
119 usleep_range(1000, 1500);
120
121 /* enable low-side LDO */
122 ldoctl |= TWL6040_LSLDOENA;
123 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
124 if (ret)
125 goto lsldo_err;
126 usleep_range(1000, 1500);
127
128 /* enable low-power PLL */
129 lppllctl = TWL6040_LPLLENA;
130 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
131 if (ret)
132 goto lppll_err;
133 usleep_range(5000, 5500);
134
135 /* disable internal oscillator */
136 ldoctl &= ~TWL6040_OSCENA;
137 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
138 if (ret)
139 goto osc_err;
140
141 return 0;
142
143osc_err:
144 lppllctl &= ~TWL6040_LPLLENA;
145 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
146lppll_err:
147 ldoctl &= ~TWL6040_LSLDOENA;
148 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
149lsldo_err:
150 ncpctl &= ~TWL6040_NCPENA;
151 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
152ncp_err:
153 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
154 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
155
156 return ret;
157}
158
159/* twl6040 manual power-down sequence */
160static void twl6040_power_down(struct twl6040 *twl6040)
161{
162 u8 ncpctl, ldoctl, lppllctl;
163
164 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
165 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
166 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
167
168 /* enable internal oscillator */
169 ldoctl |= TWL6040_OSCENA;
170 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171 usleep_range(1000, 1500);
172
173 /* disable low-power PLL */
174 lppllctl &= ~TWL6040_LPLLENA;
175 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
176
177 /* disable low-side LDO */
178 ldoctl &= ~TWL6040_LSLDOENA;
179 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
180
181 /* disable negative charge pump */
182 ncpctl &= ~TWL6040_NCPENA;
183 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
184
185 /* disable high-side LDO, reference system and internal oscillator */
186 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
187 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
188}
189
190static irqreturn_t twl6040_naudint_handler(int irq, void *data)
191{
192 struct twl6040 *twl6040 = data;
193 u8 intid, status;
194
195 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
196
197 if (intid & TWL6040_READYINT)
198 complete(&twl6040->ready);
199
200 if (intid & TWL6040_THINT) {
201 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
202 if (status & TWL6040_TSHUTDET) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300203 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500204 "Thermal shutdown, powering-off");
205 twl6040_power(twl6040, 0);
206 } else {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300207 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500208 "Leaving thermal shutdown, powering-on");
209 twl6040_power(twl6040, 1);
210 }
211 }
212
213 return IRQ_HANDLED;
214}
215
216static int twl6040_power_up_completion(struct twl6040 *twl6040,
217 int naudint)
218{
219 int time_left;
220 u8 intid;
221
222 time_left = wait_for_completion_timeout(&twl6040->ready,
223 msecs_to_jiffies(144));
224 if (!time_left) {
225 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
226 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300227 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500228 "timeout waiting for READYINT\n");
229 return -ETIMEDOUT;
230 }
231 }
232
233 return 0;
234}
235
236int twl6040_power(struct twl6040 *twl6040, int on)
237{
238 int audpwron = twl6040->audpwron;
239 int naudint = twl6040->irq;
240 int ret = 0;
241
242 mutex_lock(&twl6040->mutex);
243
244 if (on) {
245 /* already powered-up */
246 if (twl6040->power_count++)
247 goto out;
248
249 if (gpio_is_valid(audpwron)) {
250 /* use AUDPWRON line */
251 gpio_set_value(audpwron, 1);
252 /* wait for power-up completion */
253 ret = twl6040_power_up_completion(twl6040, naudint);
254 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300255 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500256 "automatic power-down failed\n");
257 twl6040->power_count = 0;
258 goto out;
259 }
260 } else {
261 /* use manual power-up sequence */
262 ret = twl6040_power_up(twl6040);
263 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300264 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500265 "manual power-up failed\n");
266 twl6040->power_count = 0;
267 goto out;
268 }
269 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300270 /* Default PLL configuration after power up */
271 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500272 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100273 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500274 } else {
275 /* already powered-down */
276 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300277 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500278 "device is already powered-off\n");
279 ret = -EPERM;
280 goto out;
281 }
282
283 if (--twl6040->power_count)
284 goto out;
285
286 if (gpio_is_valid(audpwron)) {
287 /* use AUDPWRON line */
288 gpio_set_value(audpwron, 0);
289
290 /* power-down sequence latency */
291 usleep_range(500, 700);
292 } else {
293 /* use manual power-down sequence */
294 twl6040_power_down(twl6040);
295 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500296 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100297 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500298 }
299
300out:
301 mutex_unlock(&twl6040->mutex);
302 return ret;
303}
304EXPORT_SYMBOL(twl6040_power);
305
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300306int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500307 unsigned int freq_in, unsigned int freq_out)
308{
309 u8 hppllctl, lppllctl;
310 int ret = 0;
311
312 mutex_lock(&twl6040->mutex);
313
314 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
315 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
316
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100317 /* Force full reconfiguration when switching between PLL */
318 if (pll_id != twl6040->pll) {
319 twl6040->sysclk = 0;
320 twl6040->mclk = 0;
321 }
322
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300323 switch (pll_id) {
324 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500325 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100326 /* Change the sysclk configuration only if it has been canged */
327 if (twl6040->sysclk != freq_out) {
328 switch (freq_out) {
329 case 17640000:
330 lppllctl |= TWL6040_LPLLFIN;
331 break;
332 case 19200000:
333 lppllctl &= ~TWL6040_LPLLFIN;
334 break;
335 default:
336 dev_err(twl6040->dev,
337 "freq_out %d not supported\n",
338 freq_out);
339 ret = -EINVAL;
340 goto pll_out;
341 }
342 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
343 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500344 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100345
346 /* The PLL in use has not been change, we can exit */
347 if (twl6040->pll == pll_id)
348 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500349
350 switch (freq_in) {
351 case 32768:
352 lppllctl |= TWL6040_LPLLENA;
353 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
354 lppllctl);
355 mdelay(5);
356 lppllctl &= ~TWL6040_HPLLSEL;
357 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
358 lppllctl);
359 hppllctl &= ~TWL6040_HPLLENA;
360 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
361 hppllctl);
362 break;
363 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300364 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500365 "freq_in %d not supported\n", freq_in);
366 ret = -EINVAL;
367 goto pll_out;
368 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500369 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300370 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500371 /* high-performance PLL can provide only 19.2 MHz */
372 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300373 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500374 "freq_out %d not supported\n", freq_out);
375 ret = -EINVAL;
376 goto pll_out;
377 }
378
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100379 if (twl6040->mclk != freq_in) {
380 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500381
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100382 switch (freq_in) {
383 case 12000000:
384 /* PLL enabled, active mode */
385 hppllctl |= TWL6040_MCLK_12000KHZ |
386 TWL6040_HPLLENA;
387 break;
388 case 19200000:
389 /*
390 * PLL disabled
391 * (enable PLL if MCLK jitter quality
392 * doesn't meet specification)
393 */
394 hppllctl |= TWL6040_MCLK_19200KHZ;
395 break;
396 case 26000000:
397 /* PLL enabled, active mode */
398 hppllctl |= TWL6040_MCLK_26000KHZ |
399 TWL6040_HPLLENA;
400 break;
401 case 38400000:
402 /* PLL enabled, active mode */
403 hppllctl |= TWL6040_MCLK_38400KHZ |
404 TWL6040_HPLLENA;
405 break;
406 default:
407 dev_err(twl6040->dev,
408 "freq_in %d not supported\n", freq_in);
409 ret = -EINVAL;
410 goto pll_out;
411 }
412
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500413 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100414 * enable clock slicer to ensure input waveform is
415 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500416 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100417 hppllctl |= TWL6040_HPLLSQRENA;
418
419 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
420 hppllctl);
421 usleep_range(500, 700);
422 lppllctl |= TWL6040_HPLLSEL;
423 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
424 lppllctl);
425 lppllctl &= ~TWL6040_LPLLENA;
426 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
427 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500428 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500429 break;
430 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300431 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500432 ret = -EINVAL;
433 goto pll_out;
434 }
435
436 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100437 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300438 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500439
440pll_out:
441 mutex_unlock(&twl6040->mutex);
442 return ret;
443}
444EXPORT_SYMBOL(twl6040_set_pll);
445
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300446int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500447{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300448 if (twl6040->power_count)
449 return twl6040->pll;
450 else
451 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500452}
453EXPORT_SYMBOL(twl6040_get_pll);
454
455unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
456{
457 return twl6040->sysclk;
458}
459EXPORT_SYMBOL(twl6040_get_sysclk);
460
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300461/* Get the combined status of the vibra control register */
462int twl6040_get_vibralr_status(struct twl6040 *twl6040)
463{
464 u8 status;
465
466 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
467 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
468
469 return status;
470}
471EXPORT_SYMBOL(twl6040_get_vibralr_status);
472
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300473static struct resource twl6040_vibra_rsrc[] = {
474 {
475 .flags = IORESOURCE_IRQ,
476 },
477};
478
479static struct resource twl6040_codec_rsrc[] = {
480 {
481 .flags = IORESOURCE_IRQ,
482 },
483};
484
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300485static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500486{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300487 /* Register 0 is not readable */
488 if (!reg)
489 return false;
490 return true;
491}
492
493static struct regmap_config twl6040_regmap_config = {
494 .reg_bits = 8,
495 .val_bits = 8,
496 .max_register = TWL6040_REG_STATUS, /* 0x2e */
497
498 .readable_reg = twl6040_readable_reg,
499};
500
501static int __devinit twl6040_probe(struct i2c_client *client,
502 const struct i2c_device_id *id)
503{
504 struct twl6040_platform_data *pdata = client->dev.platform_data;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500505 struct twl6040 *twl6040;
506 struct mfd_cell *cell = NULL;
507 int ret, children = 0;
508
509 if (!pdata) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300510 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500511 return -EINVAL;
512 }
513
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300514 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300515 if (!client->irq || !pdata->irq_base) {
516 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300517 return -EINVAL;
518 }
519
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300520 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
521 GFP_KERNEL);
522 if (!twl6040) {
523 ret = -ENOMEM;
524 goto err;
525 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500526
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300527 twl6040->regmap = regmap_init_i2c(client, &twl6040_regmap_config);
528 if (IS_ERR(twl6040->regmap)) {
529 ret = PTR_ERR(twl6040->regmap);
530 goto err;
531 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500532
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300533 i2c_set_clientdata(client, twl6040);
534
535 twl6040->dev = &client->dev;
536 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500537 twl6040->irq_base = pdata->irq_base;
538
539 mutex_init(&twl6040->mutex);
540 mutex_init(&twl6040->io_mutex);
541 init_completion(&twl6040->ready);
542
543 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
544
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300545 /* ERRATA: Automatic power-up is not possible in ES1.0 */
546 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
547 twl6040->audpwron = pdata->audpwron_gpio;
548 else
549 twl6040->audpwron = -EINVAL;
550
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500551 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800552 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
553 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500554 if (ret)
555 goto gpio1_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500556 }
557
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300558 /* codec interrupt */
559 ret = twl6040_irq_init(twl6040);
560 if (ret)
561 goto gpio2_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500562
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300563 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
564 NULL, twl6040_naudint_handler, 0,
565 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300566 if (ret) {
567 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
568 ret);
569 goto irq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500570 }
571
572 /* dual-access registers controlled by I2C only */
573 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
574
575 if (pdata->codec) {
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300576 int irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
577
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500578 cell = &twl6040->cells[children];
579 cell->name = "twl6040-codec";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300580 twl6040_codec_rsrc[0].start = irq;
581 twl6040_codec_rsrc[0].end = irq;
582 cell->resources = twl6040_codec_rsrc;
583 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300584 cell->platform_data = pdata->codec;
585 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500586 children++;
587 }
588
589 if (pdata->vibra) {
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300590 int irq = twl6040->irq_base + TWL6040_IRQ_VIB;
591
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500592 cell = &twl6040->cells[children];
593 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300594 twl6040_vibra_rsrc[0].start = irq;
595 twl6040_vibra_rsrc[0].end = irq;
596 cell->resources = twl6040_vibra_rsrc;
597 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
598
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500599 cell->platform_data = pdata->vibra;
600 cell->pdata_size = sizeof(*pdata->vibra);
601 children++;
602 }
603
604 if (children) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300605 ret = mfd_add_devices(&client->dev, -1, twl6040->cells,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500606 children, NULL, 0);
607 if (ret)
608 goto mfd_err;
609 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300610 dev_err(&client->dev, "No platform data found for children\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500611 ret = -ENODEV;
612 goto mfd_err;
613 }
614
615 return 0;
616
617mfd_err:
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300618 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500619irq_err:
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300620 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500621gpio2_err:
622 if (gpio_is_valid(twl6040->audpwron))
623 gpio_free(twl6040->audpwron);
624gpio1_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300625 i2c_set_clientdata(client, NULL);
626 regmap_exit(twl6040->regmap);
627err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500628 return ret;
629}
630
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300631static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500632{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300633 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500634
635 if (twl6040->power_count)
636 twl6040_power(twl6040, 0);
637
638 if (gpio_is_valid(twl6040->audpwron))
639 gpio_free(twl6040->audpwron);
640
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300641 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300642 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500643
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300644 mfd_remove_devices(&client->dev);
645 i2c_set_clientdata(client, NULL);
646 regmap_exit(twl6040->regmap);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500647
648 return 0;
649}
650
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300651static const struct i2c_device_id twl6040_i2c_id[] = {
652 { "twl6040", 0, },
653 { },
654};
655MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
656
657static struct i2c_driver twl6040_driver = {
658 .driver = {
659 .name = "twl6040",
660 .owner = THIS_MODULE,
661 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500662 .probe = twl6040_probe,
663 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300664 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500665};
666
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300667module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500668
669MODULE_DESCRIPTION("TWL6040 MFD");
670MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
671MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
672MODULE_LICENSE("GPL");
673MODULE_ALIAS("platform:twl6040");