2 * MFD driver for TWL6040 audio device
4 * Authors: Misael Lopez Cruz <misael.lopez@ti.com>
5 * Jorge Eduardo Candelaria <jorge.candelaria@ti.com>
6 * Peter Ujfalusi <peter.ujfalusi@ti.com>
8 * Copyright: (C) 2011 Texas Instruments, Inc.
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.
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.
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
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>
33 #include <linux/i2c/twl.h>
34 #include <linux/mfd/core.h>
35 #include <linux/mfd/twl6040.h>
37 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
39 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
44 mutex_lock(&twl6040->io_mutex);
45 /* Vibra control registers from cache */
46 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
47 reg == TWL6040_REG_VIBCTLR)) {
48 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
50 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
52 mutex_unlock(&twl6040->io_mutex);
56 mutex_unlock(&twl6040->io_mutex);
60 EXPORT_SYMBOL(twl6040_reg_read);
62 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
66 mutex_lock(&twl6040->io_mutex);
67 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
68 /* Cache the vibra control registers */
69 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
70 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
71 mutex_unlock(&twl6040->io_mutex);
75 EXPORT_SYMBOL(twl6040_reg_write);
77 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
82 mutex_lock(&twl6040->io_mutex);
83 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
88 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
90 mutex_unlock(&twl6040->io_mutex);
93 EXPORT_SYMBOL(twl6040_set_bits);
95 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
100 mutex_lock(&twl6040->io_mutex);
101 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
106 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
108 mutex_unlock(&twl6040->io_mutex);
111 EXPORT_SYMBOL(twl6040_clear_bits);
113 /* twl6040 codec manual power-up sequence */
114 static int twl6040_power_up(struct twl6040 *twl6040)
116 u8 ldoctl, ncpctl, lppllctl;
119 /* enable high-side LDO, reference system and internal oscillator */
120 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
121 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
124 usleep_range(10000, 10500);
126 /* enable negative charge pump */
127 ncpctl = TWL6040_NCPENA;
128 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
131 usleep_range(1000, 1500);
133 /* enable low-side LDO */
134 ldoctl |= TWL6040_LSLDOENA;
135 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
138 usleep_range(1000, 1500);
140 /* enable low-power PLL */
141 lppllctl = TWL6040_LPLLENA;
142 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
145 usleep_range(5000, 5500);
147 /* disable internal oscillator */
148 ldoctl &= ~TWL6040_OSCENA;
149 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
156 lppllctl &= ~TWL6040_LPLLENA;
157 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
159 ldoctl &= ~TWL6040_LSLDOENA;
160 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
162 ncpctl &= ~TWL6040_NCPENA;
163 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
165 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
166 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171 /* twl6040 manual power-down sequence */
172 static void twl6040_power_down(struct twl6040 *twl6040)
174 u8 ncpctl, ldoctl, lppllctl;
176 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
177 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
178 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
180 /* enable internal oscillator */
181 ldoctl |= TWL6040_OSCENA;
182 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
183 usleep_range(1000, 1500);
185 /* disable low-power PLL */
186 lppllctl &= ~TWL6040_LPLLENA;
187 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
189 /* disable low-side LDO */
190 ldoctl &= ~TWL6040_LSLDOENA;
191 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
193 /* disable negative charge pump */
194 ncpctl &= ~TWL6040_NCPENA;
195 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
197 /* disable high-side LDO, reference system and internal oscillator */
198 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
199 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
202 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
204 struct twl6040 *twl6040 = data;
207 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
209 if (intid & TWL6040_READYINT)
210 complete(&twl6040->ready);
212 if (intid & TWL6040_THINT) {
213 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
214 if (status & TWL6040_TSHUTDET) {
215 dev_warn(twl6040->dev,
216 "Thermal shutdown, powering-off");
217 twl6040_power(twl6040, 0);
219 dev_warn(twl6040->dev,
220 "Leaving thermal shutdown, powering-on");
221 twl6040_power(twl6040, 1);
228 static int twl6040_power_up_completion(struct twl6040 *twl6040,
234 time_left = wait_for_completion_timeout(&twl6040->ready,
235 msecs_to_jiffies(144));
237 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
238 if (!(intid & TWL6040_READYINT)) {
239 dev_err(twl6040->dev,
240 "timeout waiting for READYINT\n");
248 int twl6040_power(struct twl6040 *twl6040, int on)
250 int audpwron = twl6040->audpwron;
251 int naudint = twl6040->irq;
254 mutex_lock(&twl6040->mutex);
257 /* already powered-up */
258 if (twl6040->power_count++)
261 if (gpio_is_valid(audpwron)) {
262 /* use AUDPWRON line */
263 gpio_set_value(audpwron, 1);
264 /* wait for power-up completion */
265 ret = twl6040_power_up_completion(twl6040, naudint);
267 dev_err(twl6040->dev,
268 "automatic power-down failed\n");
269 twl6040->power_count = 0;
273 /* use manual power-up sequence */
274 ret = twl6040_power_up(twl6040);
276 dev_err(twl6040->dev,
277 "manual power-up failed\n");
278 twl6040->power_count = 0;
282 /* Default PLL configuration after power up */
283 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
284 twl6040->sysclk = 19200000;
285 twl6040->mclk = 32768;
287 /* already powered-down */
288 if (!twl6040->power_count) {
289 dev_err(twl6040->dev,
290 "device is already powered-off\n");
295 if (--twl6040->power_count)
298 if (gpio_is_valid(audpwron)) {
299 /* use AUDPWRON line */
300 gpio_set_value(audpwron, 0);
302 /* power-down sequence latency */
303 usleep_range(500, 700);
305 /* use manual power-down sequence */
306 twl6040_power_down(twl6040);
313 mutex_unlock(&twl6040->mutex);
316 EXPORT_SYMBOL(twl6040_power);
318 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
319 unsigned int freq_in, unsigned int freq_out)
321 u8 hppllctl, lppllctl;
324 mutex_lock(&twl6040->mutex);
326 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
327 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
330 case TWL6040_SYSCLK_SEL_LPPLL:
331 /* low-power PLL divider */
334 lppllctl |= TWL6040_LPLLFIN;
337 lppllctl &= ~TWL6040_LPLLFIN;
340 dev_err(twl6040->dev,
341 "freq_out %d not supported\n", freq_out);
345 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
349 lppllctl |= TWL6040_LPLLENA;
350 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
353 lppllctl &= ~TWL6040_HPLLSEL;
354 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
356 hppllctl &= ~TWL6040_HPLLENA;
357 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
361 dev_err(twl6040->dev,
362 "freq_in %d not supported\n", freq_in);
367 case TWL6040_SYSCLK_SEL_HPPLL:
368 /* high-performance PLL can provide only 19.2 MHz */
369 if (freq_out != 19200000) {
370 dev_err(twl6040->dev,
371 "freq_out %d not supported\n", freq_out);
376 hppllctl &= ~TWL6040_MCLK_MSK;
380 /* PLL enabled, active mode */
381 hppllctl |= TWL6040_MCLK_12000KHZ |
387 * (enable PLL if MCLK jitter quality
388 * doesn't meet specification)
390 hppllctl |= TWL6040_MCLK_19200KHZ;
393 /* PLL enabled, active mode */
394 hppllctl |= TWL6040_MCLK_26000KHZ |
398 /* PLL enabled, active mode */
399 hppllctl |= TWL6040_MCLK_38400KHZ |
403 dev_err(twl6040->dev,
404 "freq_in %d not supported\n", freq_in);
409 /* enable clock slicer to ensure input waveform is square */
410 hppllctl |= TWL6040_HPLLSQRENA;
412 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, hppllctl);
413 usleep_range(500, 700);
414 lppllctl |= TWL6040_HPLLSEL;
415 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
416 lppllctl &= ~TWL6040_LPLLENA;
417 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
420 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
425 twl6040->sysclk = freq_out;
426 twl6040->mclk = freq_in;
427 twl6040->pll = pll_id;
430 mutex_unlock(&twl6040->mutex);
433 EXPORT_SYMBOL(twl6040_set_pll);
435 int twl6040_get_pll(struct twl6040 *twl6040)
437 if (twl6040->power_count)
442 EXPORT_SYMBOL(twl6040_get_pll);
444 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
446 return twl6040->sysclk;
448 EXPORT_SYMBOL(twl6040_get_sysclk);
450 /* Get the combined status of the vibra control register */
451 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
455 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
456 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
460 EXPORT_SYMBOL(twl6040_get_vibralr_status);
462 static struct resource twl6040_vibra_rsrc[] = {
464 .flags = IORESOURCE_IRQ,
468 static struct resource twl6040_codec_rsrc[] = {
470 .flags = IORESOURCE_IRQ,
474 static int __devinit twl6040_probe(struct platform_device *pdev)
476 struct twl4030_audio_data *pdata = pdev->dev.platform_data;
477 struct twl6040 *twl6040;
478 struct mfd_cell *cell = NULL;
479 int ret, children = 0;
482 dev_err(&pdev->dev, "Platform data is missing\n");
486 /* In order to operate correctly we need valid interrupt config */
487 if (!pdata->naudint_irq || !pdata->irq_base) {
488 dev_err(&pdev->dev, "Invalid IRQ configuration\n");
492 twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL);
496 platform_set_drvdata(pdev, twl6040);
498 twl6040->dev = &pdev->dev;
499 twl6040->irq = pdata->naudint_irq;
500 twl6040->irq_base = pdata->irq_base;
502 mutex_init(&twl6040->mutex);
503 mutex_init(&twl6040->io_mutex);
504 init_completion(&twl6040->ready);
506 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
508 /* ERRATA: Automatic power-up is not possible in ES1.0 */
509 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
510 twl6040->audpwron = pdata->audpwron_gpio;
512 twl6040->audpwron = -EINVAL;
514 if (gpio_is_valid(twl6040->audpwron)) {
515 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
521 /* codec interrupt */
522 ret = twl6040_irq_init(twl6040);
526 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
527 NULL, twl6040_naudint_handler, 0,
528 "twl6040_irq_ready", twl6040);
530 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
535 /* dual-access registers controlled by I2C only */
536 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
539 int irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
541 cell = &twl6040->cells[children];
542 cell->name = "twl6040-codec";
543 twl6040_codec_rsrc[0].start = irq;
544 twl6040_codec_rsrc[0].end = irq;
545 cell->resources = twl6040_codec_rsrc;
546 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
547 cell->platform_data = pdata->codec;
548 cell->pdata_size = sizeof(*pdata->codec);
553 int irq = twl6040->irq_base + TWL6040_IRQ_VIB;
555 cell = &twl6040->cells[children];
556 cell->name = "twl6040-vibra";
557 twl6040_vibra_rsrc[0].start = irq;
558 twl6040_vibra_rsrc[0].end = irq;
559 cell->resources = twl6040_vibra_rsrc;
560 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
562 cell->platform_data = pdata->vibra;
563 cell->pdata_size = sizeof(*pdata->vibra);
568 ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells,
573 dev_err(&pdev->dev, "No platform data found for children\n");
581 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
583 twl6040_irq_exit(twl6040);
585 if (gpio_is_valid(twl6040->audpwron))
586 gpio_free(twl6040->audpwron);
588 platform_set_drvdata(pdev, NULL);
593 static int __devexit twl6040_remove(struct platform_device *pdev)
595 struct twl6040 *twl6040 = platform_get_drvdata(pdev);
597 if (twl6040->power_count)
598 twl6040_power(twl6040, 0);
600 if (gpio_is_valid(twl6040->audpwron))
601 gpio_free(twl6040->audpwron);
603 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
604 twl6040_irq_exit(twl6040);
606 mfd_remove_devices(&pdev->dev);
607 platform_set_drvdata(pdev, NULL);
613 static struct platform_driver twl6040_driver = {
614 .probe = twl6040_probe,
615 .remove = __devexit_p(twl6040_remove),
617 .owner = THIS_MODULE,
622 module_platform_driver(twl6040_driver);
624 MODULE_DESCRIPTION("TWL6040 MFD");
625 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
626 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
627 MODULE_LICENSE("GPL");
628 MODULE_ALIAS("platform:twl6040");