blob: 450a28fe8fc2cac8fbe3c596c3a525336f65cc6c [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>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030030#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050031#include <linux/platform_device.h>
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +030032#include <linux/of.h>
33#include <linux/of_irq.h>
34#include <linux/of_gpio.h>
35#include <linux/of_platform.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050036#include <linux/gpio.h>
37#include <linux/delay.h>
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030038#include <linux/i2c.h>
39#include <linux/regmap.h>
40#include <linux/err.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050041#include <linux/mfd/core.h>
42#include <linux/mfd/twl6040.h>
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030043#include <linux/regulator/consumer.h>
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050044
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030045#define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +030046#define TWL6040_NUM_SUPPLIES (2)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030047
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050048int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
49{
50 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030051 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050052
53 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030054 /* Vibra control registers from cache */
55 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
56 reg == TWL6040_REG_VIBCTLR)) {
57 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
58 } else {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030059 ret = regmap_read(twl6040->regmap, reg, &val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030060 if (ret < 0) {
61 mutex_unlock(&twl6040->io_mutex);
62 return ret;
63 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050064 }
65 mutex_unlock(&twl6040->io_mutex);
66
67 return val;
68}
69EXPORT_SYMBOL(twl6040_reg_read);
70
71int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
72{
73 int ret;
74
75 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030076 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030077 /* Cache the vibra control registers */
78 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
79 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050080 mutex_unlock(&twl6040->io_mutex);
81
82 return ret;
83}
84EXPORT_SYMBOL(twl6040_reg_write);
85
86int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
87{
88 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050089
90 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030091 ret = regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050092 mutex_unlock(&twl6040->io_mutex);
93 return ret;
94}
95EXPORT_SYMBOL(twl6040_set_bits);
96
97int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
98{
99 int ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500100
101 mutex_lock(&twl6040->io_mutex);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300102 ret = regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500103 mutex_unlock(&twl6040->io_mutex);
104 return ret;
105}
106EXPORT_SYMBOL(twl6040_clear_bits);
107
108/* twl6040 codec manual power-up sequence */
109static int twl6040_power_up(struct twl6040 *twl6040)
110{
111 u8 ldoctl, ncpctl, lppllctl;
112 int ret;
113
114 /* enable high-side LDO, reference system and internal oscillator */
115 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
116 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
117 if (ret)
118 return ret;
119 usleep_range(10000, 10500);
120
121 /* enable negative charge pump */
122 ncpctl = TWL6040_NCPENA;
123 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
124 if (ret)
125 goto ncp_err;
126 usleep_range(1000, 1500);
127
128 /* enable low-side LDO */
129 ldoctl |= TWL6040_LSLDOENA;
130 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
131 if (ret)
132 goto lsldo_err;
133 usleep_range(1000, 1500);
134
135 /* enable low-power PLL */
136 lppllctl = TWL6040_LPLLENA;
137 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
138 if (ret)
139 goto lppll_err;
140 usleep_range(5000, 5500);
141
142 /* disable internal oscillator */
143 ldoctl &= ~TWL6040_OSCENA;
144 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
145 if (ret)
146 goto osc_err;
147
148 return 0;
149
150osc_err:
151 lppllctl &= ~TWL6040_LPLLENA;
152 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
153lppll_err:
154 ldoctl &= ~TWL6040_LSLDOENA;
155 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
156lsldo_err:
157 ncpctl &= ~TWL6040_NCPENA;
158 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
159ncp_err:
160 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
161 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
162
163 return ret;
164}
165
166/* twl6040 manual power-down sequence */
167static void twl6040_power_down(struct twl6040 *twl6040)
168{
169 u8 ncpctl, ldoctl, lppllctl;
170
171 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
172 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
173 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
174
175 /* enable internal oscillator */
176 ldoctl |= TWL6040_OSCENA;
177 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
178 usleep_range(1000, 1500);
179
180 /* disable low-power PLL */
181 lppllctl &= ~TWL6040_LPLLENA;
182 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
183
184 /* disable low-side LDO */
185 ldoctl &= ~TWL6040_LSLDOENA;
186 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
187
188 /* disable negative charge pump */
189 ncpctl &= ~TWL6040_NCPENA;
190 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
191
192 /* disable high-side LDO, reference system and internal oscillator */
193 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
194 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
195}
196
197static irqreturn_t twl6040_naudint_handler(int irq, void *data)
198{
199 struct twl6040 *twl6040 = data;
200 u8 intid, status;
201
202 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
203
204 if (intid & TWL6040_READYINT)
205 complete(&twl6040->ready);
206
207 if (intid & TWL6040_THINT) {
208 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
209 if (status & TWL6040_TSHUTDET) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300210 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500211 "Thermal shutdown, powering-off");
212 twl6040_power(twl6040, 0);
213 } else {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300214 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500215 "Leaving thermal shutdown, powering-on");
216 twl6040_power(twl6040, 1);
217 }
218 }
219
220 return IRQ_HANDLED;
221}
222
223static int twl6040_power_up_completion(struct twl6040 *twl6040,
224 int naudint)
225{
226 int time_left;
227 u8 intid;
228
229 time_left = wait_for_completion_timeout(&twl6040->ready,
230 msecs_to_jiffies(144));
231 if (!time_left) {
232 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
233 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300234 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500235 "timeout waiting for READYINT\n");
236 return -ETIMEDOUT;
237 }
238 }
239
240 return 0;
241}
242
243int twl6040_power(struct twl6040 *twl6040, int on)
244{
245 int audpwron = twl6040->audpwron;
246 int naudint = twl6040->irq;
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
256 if (gpio_is_valid(audpwron)) {
257 /* use AUDPWRON line */
258 gpio_set_value(audpwron, 1);
259 /* wait for power-up completion */
260 ret = twl6040_power_up_completion(twl6040, naudint);
261 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300262 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500263 "automatic power-down failed\n");
264 twl6040->power_count = 0;
265 goto out;
266 }
267 } else {
268 /* use manual power-up sequence */
269 ret = twl6040_power_up(twl6040);
270 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300271 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500272 "manual power-up failed\n");
273 twl6040->power_count = 0;
274 goto out;
275 }
276 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300277 /* Default PLL configuration after power up */
278 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500279 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100280 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500281 } else {
282 /* already powered-down */
283 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300284 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500285 "device is already powered-off\n");
286 ret = -EPERM;
287 goto out;
288 }
289
290 if (--twl6040->power_count)
291 goto out;
292
293 if (gpio_is_valid(audpwron)) {
294 /* use AUDPWRON line */
295 gpio_set_value(audpwron, 0);
296
297 /* power-down sequence latency */
298 usleep_range(500, 700);
299 } else {
300 /* use manual power-down sequence */
301 twl6040_power_down(twl6040);
302 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500303 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100304 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500305 }
306
307out:
308 mutex_unlock(&twl6040->mutex);
309 return ret;
310}
311EXPORT_SYMBOL(twl6040_power);
312
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300313int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500314 unsigned int freq_in, unsigned int freq_out)
315{
316 u8 hppllctl, lppllctl;
317 int ret = 0;
318
319 mutex_lock(&twl6040->mutex);
320
321 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
322 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
323
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100324 /* Force full reconfiguration when switching between PLL */
325 if (pll_id != twl6040->pll) {
326 twl6040->sysclk = 0;
327 twl6040->mclk = 0;
328 }
329
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300330 switch (pll_id) {
331 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500332 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100333 /* Change the sysclk configuration only if it has been canged */
334 if (twl6040->sysclk != freq_out) {
335 switch (freq_out) {
336 case 17640000:
337 lppllctl |= TWL6040_LPLLFIN;
338 break;
339 case 19200000:
340 lppllctl &= ~TWL6040_LPLLFIN;
341 break;
342 default:
343 dev_err(twl6040->dev,
344 "freq_out %d not supported\n",
345 freq_out);
346 ret = -EINVAL;
347 goto pll_out;
348 }
349 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
350 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500351 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100352
353 /* The PLL in use has not been change, we can exit */
354 if (twl6040->pll == pll_id)
355 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500356
357 switch (freq_in) {
358 case 32768:
359 lppllctl |= TWL6040_LPLLENA;
360 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
361 lppllctl);
362 mdelay(5);
363 lppllctl &= ~TWL6040_HPLLSEL;
364 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
365 lppllctl);
366 hppllctl &= ~TWL6040_HPLLENA;
367 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
368 hppllctl);
369 break;
370 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300371 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500372 "freq_in %d not supported\n", freq_in);
373 ret = -EINVAL;
374 goto pll_out;
375 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500376 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300377 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500378 /* high-performance PLL can provide only 19.2 MHz */
379 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300380 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500381 "freq_out %d not supported\n", freq_out);
382 ret = -EINVAL;
383 goto pll_out;
384 }
385
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100386 if (twl6040->mclk != freq_in) {
387 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500388
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100389 switch (freq_in) {
390 case 12000000:
391 /* PLL enabled, active mode */
392 hppllctl |= TWL6040_MCLK_12000KHZ |
393 TWL6040_HPLLENA;
394 break;
395 case 19200000:
396 /*
397 * PLL disabled
398 * (enable PLL if MCLK jitter quality
399 * doesn't meet specification)
400 */
401 hppllctl |= TWL6040_MCLK_19200KHZ;
402 break;
403 case 26000000:
404 /* PLL enabled, active mode */
405 hppllctl |= TWL6040_MCLK_26000KHZ |
406 TWL6040_HPLLENA;
407 break;
408 case 38400000:
409 /* PLL enabled, active mode */
410 hppllctl |= TWL6040_MCLK_38400KHZ |
411 TWL6040_HPLLENA;
412 break;
413 default:
414 dev_err(twl6040->dev,
415 "freq_in %d not supported\n", freq_in);
416 ret = -EINVAL;
417 goto pll_out;
418 }
419
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500420 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100421 * enable clock slicer to ensure input waveform is
422 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500423 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100424 hppllctl |= TWL6040_HPLLSQRENA;
425
426 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
427 hppllctl);
428 usleep_range(500, 700);
429 lppllctl |= TWL6040_HPLLSEL;
430 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
431 lppllctl);
432 lppllctl &= ~TWL6040_LPLLENA;
433 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
434 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500435 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500436 break;
437 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300438 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500439 ret = -EINVAL;
440 goto pll_out;
441 }
442
443 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100444 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300445 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500446
447pll_out:
448 mutex_unlock(&twl6040->mutex);
449 return ret;
450}
451EXPORT_SYMBOL(twl6040_set_pll);
452
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300453int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500454{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300455 if (twl6040->power_count)
456 return twl6040->pll;
457 else
458 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500459}
460EXPORT_SYMBOL(twl6040_get_pll);
461
462unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
463{
464 return twl6040->sysclk;
465}
466EXPORT_SYMBOL(twl6040_get_sysclk);
467
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300468/* Get the combined status of the vibra control register */
469int twl6040_get_vibralr_status(struct twl6040 *twl6040)
470{
471 u8 status;
472
473 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
474 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
475
476 return status;
477}
478EXPORT_SYMBOL(twl6040_get_vibralr_status);
479
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300480static struct resource twl6040_vibra_rsrc[] = {
481 {
482 .flags = IORESOURCE_IRQ,
483 },
484};
485
486static struct resource twl6040_codec_rsrc[] = {
487 {
488 .flags = IORESOURCE_IRQ,
489 },
490};
491
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300492static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500493{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300494 /* Register 0 is not readable */
495 if (!reg)
496 return false;
497 return true;
498}
499
500static struct regmap_config twl6040_regmap_config = {
501 .reg_bits = 8,
502 .val_bits = 8,
503 .max_register = TWL6040_REG_STATUS, /* 0x2e */
504
505 .readable_reg = twl6040_readable_reg,
506};
507
508static int __devinit twl6040_probe(struct i2c_client *client,
509 const struct i2c_device_id *id)
510{
511 struct twl6040_platform_data *pdata = client->dev.platform_data;
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300512 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500513 struct twl6040 *twl6040;
514 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300515 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500516
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300517 if (!pdata && !node) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300518 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500519 return -EINVAL;
520 }
521
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300522 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300523 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300524 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300525 return -EINVAL;
526 }
527
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300528 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
529 GFP_KERNEL);
530 if (!twl6040) {
531 ret = -ENOMEM;
532 goto err;
533 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500534
Axel Linbbf6adc2012-04-25 10:09:46 +0800535 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300536 if (IS_ERR(twl6040->regmap)) {
537 ret = PTR_ERR(twl6040->regmap);
538 goto err;
539 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500540
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300541 i2c_set_clientdata(client, twl6040);
542
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300543 twl6040->supplies[0].supply = "vio";
544 twl6040->supplies[1].supply = "v2v1";
545 ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
546 twl6040->supplies);
547 if (ret != 0) {
548 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
549 goto regulator_get_err;
550 }
551
552 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
553 if (ret != 0) {
554 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
555 goto power_err;
556 }
557
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300558 twl6040->dev = &client->dev;
559 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500560
561 mutex_init(&twl6040->mutex);
562 mutex_init(&twl6040->io_mutex);
563 init_completion(&twl6040->ready);
564
565 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
566
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300567 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300568 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
569 if (pdata)
570 twl6040->audpwron = pdata->audpwron_gpio;
571 else
572 twl6040->audpwron = of_get_named_gpio(node,
573 "ti,audpwron-gpio", 0);
574 } else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300575 twl6040->audpwron = -EINVAL;
576
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500577 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800578 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
579 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500580 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300581 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500582 }
583
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300584 /* codec interrupt */
585 ret = twl6040_irq_init(twl6040);
586 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300587 goto irq_init_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500588
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300589 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
590 NULL, twl6040_naudint_handler, 0,
591 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300592 if (ret) {
593 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
594 ret);
595 goto irq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500596 }
597
598 /* dual-access registers controlled by I2C only */
599 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
600
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300601 /*
602 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
603 * We can add the ASoC codec child whenever this driver has been loaded.
604 * The ASoC codec can work without pdata, pass the platform_data only if
605 * it has been provided.
606 */
607 irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
608 cell = &twl6040->cells[children];
609 cell->name = "twl6040-codec";
610 twl6040_codec_rsrc[0].start = irq;
611 twl6040_codec_rsrc[0].end = irq;
612 cell->resources = twl6040_codec_rsrc;
613 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300614 if (pdata && pdata->codec) {
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300615 cell->platform_data = pdata->codec;
616 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500617 }
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300618 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500619
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300620 if ((pdata && pdata->vibra) || of_find_node_by_name(node, "vibra")) {
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300621 irq = twl6040->irq_base + TWL6040_IRQ_VIB;
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300622
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500623 cell = &twl6040->cells[children];
624 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300625 twl6040_vibra_rsrc[0].start = irq;
626 twl6040_vibra_rsrc[0].end = irq;
627 cell->resources = twl6040_vibra_rsrc;
628 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
629
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300630 if (pdata && pdata->vibra) {
631 cell->platform_data = pdata->vibra;
632 cell->pdata_size = sizeof(*pdata->vibra);
633 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500634 children++;
635 }
636
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300637 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
638 NULL, 0);
639 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500640 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500641
642 return 0;
643
644mfd_err:
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300645 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500646irq_err:
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300647 twl6040_irq_exit(twl6040);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300648irq_init_err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500649 if (gpio_is_valid(twl6040->audpwron))
650 gpio_free(twl6040->audpwron);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300651gpio_err:
652 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
653power_err:
654 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
655regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300656 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300657err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500658 return ret;
659}
660
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300661static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500662{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300663 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500664
665 if (twl6040->power_count)
666 twl6040_power(twl6040, 0);
667
668 if (gpio_is_valid(twl6040->audpwron))
669 gpio_free(twl6040->audpwron);
670
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300671 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300672 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500673
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300674 mfd_remove_devices(&client->dev);
675 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500676
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300677 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
678 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
679
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500680 return 0;
681}
682
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300683static const struct i2c_device_id twl6040_i2c_id[] = {
684 { "twl6040", 0, },
685 { },
686};
687MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
688
689static struct i2c_driver twl6040_driver = {
690 .driver = {
691 .name = "twl6040",
692 .owner = THIS_MODULE,
693 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500694 .probe = twl6040_probe,
695 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300696 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500697};
698
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300699module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500700
701MODULE_DESCRIPTION("TWL6040 MFD");
702MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
703MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
704MODULE_LICENSE("GPL");
705MODULE_ALIAS("platform:twl6040");