blob: 3f2a1cf02fc0697d2c56c3fcb3f84cc8106fda21 [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
Samuel Ortizca2cad62012-05-23 16:23:21 +020048static 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
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050062int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
63{
64 int ret;
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030065 unsigned int val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050066
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030067 /* 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 {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030072 ret = regmap_read(twl6040->regmap, reg, &val);
Axel Linc6000402012-07-11 10:06:34 +080073 if (ret < 0)
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030074 return ret;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050075 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050076
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
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +030085 ret = regmap_write(twl6040->regmap, reg, val);
Peter Ujfalusi31b402e2011-10-12 11:57:54 +030086 /* Cache the vibra control registers */
87 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
88 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050089
90 return ret;
91}
92EXPORT_SYMBOL(twl6040_reg_write);
93
94int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
95{
Axel Linc6000402012-07-11 10:06:34 +080096 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -050097}
98EXPORT_SYMBOL(twl6040_set_bits);
99
100int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
101{
Axel Linc6000402012-07-11 10:06:34 +0800102 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500103}
104EXPORT_SYMBOL(twl6040_clear_bits);
105
106/* twl6040 codec manual power-up sequence */
107static int twl6040_power_up(struct twl6040 *twl6040)
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
161 return ret;
162}
163
164/* twl6040 manual power-down sequence */
165static void twl6040_power_down(struct twl6040 *twl6040)
166{
167 u8 ncpctl, ldoctl, lppllctl;
168
169 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
170 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
171 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
172
173 /* enable internal oscillator */
174 ldoctl |= TWL6040_OSCENA;
175 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
176 usleep_range(1000, 1500);
177
178 /* disable low-power PLL */
179 lppllctl &= ~TWL6040_LPLLENA;
180 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
181
182 /* disable low-side LDO */
183 ldoctl &= ~TWL6040_LSLDOENA;
184 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
185
186 /* disable negative charge pump */
187 ncpctl &= ~TWL6040_NCPENA;
188 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
189
190 /* disable high-side LDO, reference system and internal oscillator */
191 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
192 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
193}
194
195static irqreturn_t twl6040_naudint_handler(int irq, void *data)
196{
197 struct twl6040 *twl6040 = data;
198 u8 intid, status;
199
200 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
201
202 if (intid & TWL6040_READYINT)
203 complete(&twl6040->ready);
204
205 if (intid & TWL6040_THINT) {
206 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
207 if (status & TWL6040_TSHUTDET) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300208 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500209 "Thermal shutdown, powering-off");
210 twl6040_power(twl6040, 0);
211 } else {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300212 dev_warn(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500213 "Leaving thermal shutdown, powering-on");
214 twl6040_power(twl6040, 1);
215 }
216 }
217
218 return IRQ_HANDLED;
219}
220
221static int twl6040_power_up_completion(struct twl6040 *twl6040,
222 int naudint)
223{
224 int time_left;
225 u8 intid;
226
227 time_left = wait_for_completion_timeout(&twl6040->ready,
228 msecs_to_jiffies(144));
229 if (!time_left) {
230 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
231 if (!(intid & TWL6040_READYINT)) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300232 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500233 "timeout waiting for READYINT\n");
234 return -ETIMEDOUT;
235 }
236 }
237
238 return 0;
239}
240
241int twl6040_power(struct twl6040 *twl6040, int on)
242{
243 int audpwron = twl6040->audpwron;
244 int naudint = twl6040->irq;
245 int ret = 0;
246
247 mutex_lock(&twl6040->mutex);
248
249 if (on) {
250 /* already powered-up */
251 if (twl6040->power_count++)
252 goto out;
253
254 if (gpio_is_valid(audpwron)) {
255 /* use AUDPWRON line */
256 gpio_set_value(audpwron, 1);
257 /* wait for power-up completion */
258 ret = twl6040_power_up_completion(twl6040, naudint);
259 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300260 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500261 "automatic power-down failed\n");
262 twl6040->power_count = 0;
263 goto out;
264 }
265 } else {
266 /* use manual power-up sequence */
267 ret = twl6040_power_up(twl6040);
268 if (ret) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300269 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500270 "manual power-up failed\n");
271 twl6040->power_count = 0;
272 goto out;
273 }
274 }
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300275 /* Default PLL configuration after power up */
276 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500277 twl6040->sysclk = 19200000;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100278 twl6040->mclk = 32768;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500279 } else {
280 /* already powered-down */
281 if (!twl6040->power_count) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300282 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500283 "device is already powered-off\n");
284 ret = -EPERM;
285 goto out;
286 }
287
288 if (--twl6040->power_count)
289 goto out;
290
291 if (gpio_is_valid(audpwron)) {
292 /* use AUDPWRON line */
293 gpio_set_value(audpwron, 0);
294
295 /* power-down sequence latency */
296 usleep_range(500, 700);
297 } else {
298 /* use manual power-down sequence */
299 twl6040_power_down(twl6040);
300 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500301 twl6040->sysclk = 0;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100302 twl6040->mclk = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500303 }
304
305out:
306 mutex_unlock(&twl6040->mutex);
307 return ret;
308}
309EXPORT_SYMBOL(twl6040_power);
310
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300311int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500312 unsigned int freq_in, unsigned int freq_out)
313{
314 u8 hppllctl, lppllctl;
315 int ret = 0;
316
317 mutex_lock(&twl6040->mutex);
318
319 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
320 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
321
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100322 /* Force full reconfiguration when switching between PLL */
323 if (pll_id != twl6040->pll) {
324 twl6040->sysclk = 0;
325 twl6040->mclk = 0;
326 }
327
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300328 switch (pll_id) {
329 case TWL6040_SYSCLK_SEL_LPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500330 /* low-power PLL divider */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100331 /* Change the sysclk configuration only if it has been canged */
332 if (twl6040->sysclk != freq_out) {
333 switch (freq_out) {
334 case 17640000:
335 lppllctl |= TWL6040_LPLLFIN;
336 break;
337 case 19200000:
338 lppllctl &= ~TWL6040_LPLLFIN;
339 break;
340 default:
341 dev_err(twl6040->dev,
342 "freq_out %d not supported\n",
343 freq_out);
344 ret = -EINVAL;
345 goto pll_out;
346 }
347 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
348 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500349 }
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100350
351 /* The PLL in use has not been change, we can exit */
352 if (twl6040->pll == pll_id)
353 break;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500354
355 switch (freq_in) {
356 case 32768:
357 lppllctl |= TWL6040_LPLLENA;
358 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
359 lppllctl);
360 mdelay(5);
361 lppllctl &= ~TWL6040_HPLLSEL;
362 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
363 lppllctl);
364 hppllctl &= ~TWL6040_HPLLENA;
365 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
366 hppllctl);
367 break;
368 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300369 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500370 "freq_in %d not supported\n", freq_in);
371 ret = -EINVAL;
372 goto pll_out;
373 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500374 break;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300375 case TWL6040_SYSCLK_SEL_HPPLL:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500376 /* high-performance PLL can provide only 19.2 MHz */
377 if (freq_out != 19200000) {
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300378 dev_err(twl6040->dev,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500379 "freq_out %d not supported\n", freq_out);
380 ret = -EINVAL;
381 goto pll_out;
382 }
383
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100384 if (twl6040->mclk != freq_in) {
385 hppllctl &= ~TWL6040_MCLK_MSK;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500386
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100387 switch (freq_in) {
388 case 12000000:
389 /* PLL enabled, active mode */
390 hppllctl |= TWL6040_MCLK_12000KHZ |
391 TWL6040_HPLLENA;
392 break;
393 case 19200000:
394 /*
395 * PLL disabled
396 * (enable PLL if MCLK jitter quality
397 * doesn't meet specification)
398 */
399 hppllctl |= TWL6040_MCLK_19200KHZ;
400 break;
401 case 26000000:
402 /* PLL enabled, active mode */
403 hppllctl |= TWL6040_MCLK_26000KHZ |
404 TWL6040_HPLLENA;
405 break;
406 case 38400000:
407 /* PLL enabled, active mode */
408 hppllctl |= TWL6040_MCLK_38400KHZ |
409 TWL6040_HPLLENA;
410 break;
411 default:
412 dev_err(twl6040->dev,
413 "freq_in %d not supported\n", freq_in);
414 ret = -EINVAL;
415 goto pll_out;
416 }
417
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500418 /*
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100419 * enable clock slicer to ensure input waveform is
420 * square
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500421 */
Peter Ujfalusi2bd05db2012-01-14 20:58:44 +0100422 hppllctl |= TWL6040_HPLLSQRENA;
423
424 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
425 hppllctl);
426 usleep_range(500, 700);
427 lppllctl |= TWL6040_HPLLSEL;
428 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
429 lppllctl);
430 lppllctl &= ~TWL6040_LPLLENA;
431 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
432 lppllctl);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500433 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500434 break;
435 default:
Peter Ujfalusi2d7c9572011-09-15 15:39:23 +0300436 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500437 ret = -EINVAL;
438 goto pll_out;
439 }
440
441 twl6040->sysclk = freq_out;
Peter Ujfalusif8447d62012-01-14 20:58:43 +0100442 twl6040->mclk = freq_in;
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300443 twl6040->pll = pll_id;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500444
445pll_out:
446 mutex_unlock(&twl6040->mutex);
447 return ret;
448}
449EXPORT_SYMBOL(twl6040_set_pll);
450
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300451int twl6040_get_pll(struct twl6040 *twl6040)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500452{
Peter Ujfalusicfb7a332011-07-04 10:28:28 +0300453 if (twl6040->power_count)
454 return twl6040->pll;
455 else
456 return -ENODEV;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500457}
458EXPORT_SYMBOL(twl6040_get_pll);
459
460unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
461{
462 return twl6040->sysclk;
463}
464EXPORT_SYMBOL(twl6040_get_sysclk);
465
Peter Ujfalusi70601ec2011-10-12 11:57:55 +0300466/* Get the combined status of the vibra control register */
467int twl6040_get_vibralr_status(struct twl6040 *twl6040)
468{
469 u8 status;
470
471 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
472 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
473
474 return status;
475}
476EXPORT_SYMBOL(twl6040_get_vibralr_status);
477
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300478static struct resource twl6040_vibra_rsrc[] = {
479 {
480 .flags = IORESOURCE_IRQ,
481 },
482};
483
484static struct resource twl6040_codec_rsrc[] = {
485 {
486 .flags = IORESOURCE_IRQ,
487 },
488};
489
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300490static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500491{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300492 /* Register 0 is not readable */
493 if (!reg)
494 return false;
495 return true;
496}
497
498static struct regmap_config twl6040_regmap_config = {
499 .reg_bits = 8,
500 .val_bits = 8,
501 .max_register = TWL6040_REG_STATUS, /* 0x2e */
502
503 .readable_reg = twl6040_readable_reg,
504};
505
506static int __devinit twl6040_probe(struct i2c_client *client,
507 const struct i2c_device_id *id)
508{
509 struct twl6040_platform_data *pdata = client->dev.platform_data;
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300510 struct device_node *node = client->dev.of_node;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500511 struct twl6040 *twl6040;
512 struct mfd_cell *cell = NULL;
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300513 int irq, ret, children = 0;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500514
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300515 if (!pdata && !node) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300516 dev_err(&client->dev, "Platform data is missing\n");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500517 return -EINVAL;
518 }
519
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300520 /* In order to operate correctly we need valid interrupt config */
Peter Ujfalusi67124192012-05-16 14:11:56 +0300521 if (!client->irq) {
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300522 dev_err(&client->dev, "Invalid IRQ configuration\n");
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300523 return -EINVAL;
524 }
525
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300526 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
527 GFP_KERNEL);
528 if (!twl6040) {
529 ret = -ENOMEM;
530 goto err;
531 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500532
Axel Linbbf6adc2012-04-25 10:09:46 +0800533 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300534 if (IS_ERR(twl6040->regmap)) {
535 ret = PTR_ERR(twl6040->regmap);
536 goto err;
537 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500538
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300539 i2c_set_clientdata(client, twl6040);
540
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300541 twl6040->supplies[0].supply = "vio";
542 twl6040->supplies[1].supply = "v2v1";
543 ret = regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
544 twl6040->supplies);
545 if (ret != 0) {
546 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
547 goto regulator_get_err;
548 }
549
550 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
551 if (ret != 0) {
552 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
553 goto power_err;
554 }
555
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300556 twl6040->dev = &client->dev;
557 twl6040->irq = client->irq;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500558
559 mutex_init(&twl6040->mutex);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500560 init_completion(&twl6040->ready);
561
562 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
563
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300564 /* ERRATA: Automatic power-up is not possible in ES1.0 */
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300565 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0) {
566 if (pdata)
567 twl6040->audpwron = pdata->audpwron_gpio;
568 else
569 twl6040->audpwron = of_get_named_gpio(node,
570 "ti,audpwron-gpio", 0);
571 } else
Peter Ujfalusi77f63e02011-09-15 15:39:26 +0300572 twl6040->audpwron = -EINVAL;
573
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500574 if (gpio_is_valid(twl6040->audpwron)) {
Axel Linb04edb92011-12-01 09:55:07 +0800575 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
576 "audpwron");
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500577 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300578 goto gpio_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500579 }
580
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300581 /* codec interrupt */
582 ret = twl6040_irq_init(twl6040);
583 if (ret)
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300584 goto irq_init_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500585
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300586 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
Fengguang Wu09e09062012-09-19 09:32:38 +0800587 NULL, twl6040_naudint_handler, IRQF_ONESHOT,
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300588 "twl6040_irq_ready", twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300589 if (ret) {
590 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
591 ret);
592 goto irq_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500593 }
594
595 /* dual-access registers controlled by I2C only */
596 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
597
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300598 /*
599 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
600 * We can add the ASoC codec child whenever this driver has been loaded.
601 * The ASoC codec can work without pdata, pass the platform_data only if
602 * it has been provided.
603 */
604 irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
605 cell = &twl6040->cells[children];
606 cell->name = "twl6040-codec";
607 twl6040_codec_rsrc[0].start = irq;
608 twl6040_codec_rsrc[0].end = irq;
609 cell->resources = twl6040_codec_rsrc;
610 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300611 if (pdata && pdata->codec) {
Peter Ujfalusi6c448632011-06-01 13:05:10 +0300612 cell->platform_data = pdata->codec;
613 cell->pdata_size = sizeof(*pdata->codec);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500614 }
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300615 children++;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500616
Samuel Ortizca2cad62012-05-23 16:23:21 +0200617 if (twl6040_has_vibra(pdata, node)) {
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300618 irq = twl6040->irq_base + TWL6040_IRQ_VIB;
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300619
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500620 cell = &twl6040->cells[children];
621 cell->name = "twl6040-vibra";
Peter Ujfalusi0f962ae2011-07-05 11:40:33 +0300622 twl6040_vibra_rsrc[0].start = irq;
623 twl6040_vibra_rsrc[0].end = irq;
624 cell->resources = twl6040_vibra_rsrc;
625 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
626
Peter Ujfalusi37e13ce2012-05-16 14:11:58 +0300627 if (pdata && pdata->vibra) {
628 cell->platform_data = pdata->vibra;
629 cell->pdata_size = sizeof(*pdata->vibra);
630 }
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500631 children++;
632 }
633
Peter Ujfalusi5cbe7862012-08-16 15:13:14 +0300634 /*
635 * Enable the GPO driver in the following cases:
636 * DT booted kernel or legacy boot with valid gpo platform_data
637 */
638 if (!pdata || (pdata && pdata->gpo)) {
639 cell = &twl6040->cells[children];
640 cell->name = "twl6040-gpo";
641
642 if (pdata) {
643 cell->platform_data = pdata->gpo;
644 cell->pdata_size = sizeof(*pdata->gpo);
645 }
646 children++;
647 }
648
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300649 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
Mark Brown55692af2012-09-11 15:16:36 +0800650 NULL, 0, NULL);
Peter Ujfalusi1f01d602012-05-16 14:11:57 +0300651 if (ret)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500652 goto mfd_err;
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500653
654 return 0;
655
656mfd_err:
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300657 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500658irq_err:
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300659 twl6040_irq_exit(twl6040);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300660irq_init_err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500661 if (gpio_is_valid(twl6040->audpwron))
662 gpio_free(twl6040->audpwron);
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300663gpio_err:
664 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
665power_err:
666 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
667regulator_get_err:
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300668 i2c_set_clientdata(client, NULL);
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300669err:
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500670 return ret;
671}
672
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300673static int __devexit twl6040_remove(struct i2c_client *client)
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500674{
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300675 struct twl6040 *twl6040 = i2c_get_clientdata(client);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500676
677 if (twl6040->power_count)
678 twl6040_power(twl6040, 0);
679
680 if (gpio_is_valid(twl6040->audpwron))
681 gpio_free(twl6040->audpwron);
682
Peter Ujfalusi1b7c4722011-07-04 20:16:23 +0300683 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
Peter Ujfalusid20e1d22011-07-04 20:15:19 +0300684 twl6040_irq_exit(twl6040);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500685
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300686 mfd_remove_devices(&client->dev);
687 i2c_set_clientdata(client, NULL);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500688
Peter Ujfalusi5af7df62012-05-02 16:54:42 +0300689 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
690 regulator_bulk_free(TWL6040_NUM_SUPPLIES, twl6040->supplies);
691
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500692 return 0;
693}
694
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300695static const struct i2c_device_id twl6040_i2c_id[] = {
696 { "twl6040", 0, },
Peter Ujfalusi1fc74ae2012-07-16 11:49:44 +0200697 { "twl6041", 0, },
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300698 { },
699};
700MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
701
702static struct i2c_driver twl6040_driver = {
703 .driver = {
704 .name = "twl6040",
705 .owner = THIS_MODULE,
706 },
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500707 .probe = twl6040_probe,
708 .remove = __devexit_p(twl6040_remove),
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300709 .id_table = twl6040_i2c_id,
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500710};
711
Peter Ujfalusi8eaeb932012-04-03 11:56:51 +0300712module_i2c_driver(twl6040_driver);
Misael Lopez Cruzf19b2822011-04-27 02:14:07 -0500713
714MODULE_DESCRIPTION("TWL6040 MFD");
715MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
716MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
717MODULE_LICENSE("GPL");
718MODULE_ALIAS("platform:twl6040");