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 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
42 mutex_lock(&twl6040->io_mutex);
43 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
45 mutex_unlock(&twl6040->io_mutex);
48 mutex_unlock(&twl6040->io_mutex);
52 EXPORT_SYMBOL(twl6040_reg_read);
54 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
58 mutex_lock(&twl6040->io_mutex);
59 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
60 mutex_unlock(&twl6040->io_mutex);
64 EXPORT_SYMBOL(twl6040_reg_write);
66 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
71 mutex_lock(&twl6040->io_mutex);
72 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
77 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
79 mutex_unlock(&twl6040->io_mutex);
82 EXPORT_SYMBOL(twl6040_set_bits);
84 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
89 mutex_lock(&twl6040->io_mutex);
90 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
95 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
97 mutex_unlock(&twl6040->io_mutex);
100 EXPORT_SYMBOL(twl6040_clear_bits);
102 /* twl6040 codec manual power-up sequence */
103 static int twl6040_power_up(struct twl6040 *twl6040)
105 u8 ldoctl, ncpctl, lppllctl;
108 /* enable high-side LDO, reference system and internal oscillator */
109 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
110 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
113 usleep_range(10000, 10500);
115 /* enable negative charge pump */
116 ncpctl = TWL6040_NCPENA;
117 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
120 usleep_range(1000, 1500);
122 /* enable low-side LDO */
123 ldoctl |= TWL6040_LSLDOENA;
124 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
127 usleep_range(1000, 1500);
129 /* enable low-power PLL */
130 lppllctl = TWL6040_LPLLENA;
131 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
134 usleep_range(5000, 5500);
136 /* disable internal oscillator */
137 ldoctl &= ~TWL6040_OSCENA;
138 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
145 lppllctl &= ~TWL6040_LPLLENA;
146 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
148 ldoctl &= ~TWL6040_LSLDOENA;
149 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
151 ncpctl &= ~TWL6040_NCPENA;
152 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
154 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
155 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
160 /* twl6040 manual power-down sequence */
161 static void twl6040_power_down(struct twl6040 *twl6040)
163 u8 ncpctl, ldoctl, lppllctl;
165 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
166 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
167 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
169 /* enable internal oscillator */
170 ldoctl |= TWL6040_OSCENA;
171 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
172 usleep_range(1000, 1500);
174 /* disable low-power PLL */
175 lppllctl &= ~TWL6040_LPLLENA;
176 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
178 /* disable low-side LDO */
179 ldoctl &= ~TWL6040_LSLDOENA;
180 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
182 /* disable negative charge pump */
183 ncpctl &= ~TWL6040_NCPENA;
184 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
186 /* disable high-side LDO, reference system and internal oscillator */
187 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
188 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
191 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
193 struct twl6040 *twl6040 = data;
196 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
198 if (intid & TWL6040_READYINT)
199 complete(&twl6040->ready);
201 if (intid & TWL6040_THINT) {
202 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
203 if (status & TWL6040_TSHUTDET) {
204 dev_warn(twl6040->dev,
205 "Thermal shutdown, powering-off");
206 twl6040_power(twl6040, 0);
208 dev_warn(twl6040->dev,
209 "Leaving thermal shutdown, powering-on");
210 twl6040_power(twl6040, 1);
217 static int twl6040_power_up_completion(struct twl6040 *twl6040,
223 time_left = wait_for_completion_timeout(&twl6040->ready,
224 msecs_to_jiffies(144));
226 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
227 if (!(intid & TWL6040_READYINT)) {
228 dev_err(twl6040->dev,
229 "timeout waiting for READYINT\n");
237 int twl6040_power(struct twl6040 *twl6040, int on)
239 int audpwron = twl6040->audpwron;
240 int naudint = twl6040->irq;
243 mutex_lock(&twl6040->mutex);
246 /* already powered-up */
247 if (twl6040->power_count++)
250 if (gpio_is_valid(audpwron)) {
251 /* use AUDPWRON line */
252 gpio_set_value(audpwron, 1);
253 /* wait for power-up completion */
254 ret = twl6040_power_up_completion(twl6040, naudint);
256 dev_err(twl6040->dev,
257 "automatic power-down failed\n");
258 twl6040->power_count = 0;
262 /* use manual power-up sequence */
263 ret = twl6040_power_up(twl6040);
265 dev_err(twl6040->dev,
266 "manual power-up failed\n");
267 twl6040->power_count = 0;
271 /* Default PLL configuration after power up */
272 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
273 twl6040->sysclk = 19200000;
275 /* already powered-down */
276 if (!twl6040->power_count) {
277 dev_err(twl6040->dev,
278 "device is already powered-off\n");
283 if (--twl6040->power_count)
286 if (gpio_is_valid(audpwron)) {
287 /* use AUDPWRON line */
288 gpio_set_value(audpwron, 0);
290 /* power-down sequence latency */
291 usleep_range(500, 700);
293 /* use manual power-down sequence */
294 twl6040_power_down(twl6040);
300 mutex_unlock(&twl6040->mutex);
303 EXPORT_SYMBOL(twl6040_power);
305 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
306 unsigned int freq_in, unsigned int freq_out)
308 u8 hppllctl, lppllctl;
311 mutex_lock(&twl6040->mutex);
313 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
314 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
317 case TWL6040_SYSCLK_SEL_LPPLL:
318 /* low-power PLL divider */
321 lppllctl |= TWL6040_LPLLFIN;
324 lppllctl &= ~TWL6040_LPLLFIN;
327 dev_err(twl6040->dev,
328 "freq_out %d not supported\n", freq_out);
332 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
336 lppllctl |= TWL6040_LPLLENA;
337 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
340 lppllctl &= ~TWL6040_HPLLSEL;
341 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
343 hppllctl &= ~TWL6040_HPLLENA;
344 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
348 dev_err(twl6040->dev,
349 "freq_in %d not supported\n", freq_in);
354 case TWL6040_SYSCLK_SEL_HPPLL:
355 /* high-performance PLL can provide only 19.2 MHz */
356 if (freq_out != 19200000) {
357 dev_err(twl6040->dev,
358 "freq_out %d not supported\n", freq_out);
363 hppllctl &= ~TWL6040_MCLK_MSK;
367 /* PLL enabled, active mode */
368 hppllctl |= TWL6040_MCLK_12000KHZ |
374 * (enable PLL if MCLK jitter quality
375 * doesn't meet specification)
377 hppllctl |= TWL6040_MCLK_19200KHZ;
380 /* PLL enabled, active mode */
381 hppllctl |= TWL6040_MCLK_26000KHZ |
385 /* PLL enabled, active mode */
386 hppllctl |= TWL6040_MCLK_38400KHZ |
390 dev_err(twl6040->dev,
391 "freq_in %d not supported\n", freq_in);
396 /* enable clock slicer to ensure input waveform is square */
397 hppllctl |= TWL6040_HPLLSQRENA;
399 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, hppllctl);
400 usleep_range(500, 700);
401 lppllctl |= TWL6040_HPLLSEL;
402 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
403 lppllctl &= ~TWL6040_LPLLENA;
404 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
407 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
412 twl6040->sysclk = freq_out;
413 twl6040->pll = pll_id;
416 mutex_unlock(&twl6040->mutex);
419 EXPORT_SYMBOL(twl6040_set_pll);
421 int twl6040_get_pll(struct twl6040 *twl6040)
423 if (twl6040->power_count)
428 EXPORT_SYMBOL(twl6040_get_pll);
430 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
432 return twl6040->sysclk;
434 EXPORT_SYMBOL(twl6040_get_sysclk);
436 static struct resource twl6040_vibra_rsrc[] = {
438 .flags = IORESOURCE_IRQ,
442 static struct resource twl6040_codec_rsrc[] = {
444 .flags = IORESOURCE_IRQ,
448 static int __devinit twl6040_probe(struct platform_device *pdev)
450 struct twl4030_audio_data *pdata = pdev->dev.platform_data;
451 struct twl6040 *twl6040;
452 struct mfd_cell *cell = NULL;
453 int ret, children = 0;
456 dev_err(&pdev->dev, "Platform data is missing\n");
460 /* In order to operate correctly we need valid interrupt config */
461 if (!pdata->naudint_irq || !pdata->irq_base) {
462 dev_err(&pdev->dev, "Invalid IRQ configuration\n");
466 twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL);
470 platform_set_drvdata(pdev, twl6040);
472 twl6040->dev = &pdev->dev;
473 twl6040->irq = pdata->naudint_irq;
474 twl6040->irq_base = pdata->irq_base;
476 mutex_init(&twl6040->mutex);
477 mutex_init(&twl6040->io_mutex);
478 init_completion(&twl6040->ready);
480 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
482 /* ERRATA: Automatic power-up is not possible in ES1.0 */
483 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
484 twl6040->audpwron = pdata->audpwron_gpio;
486 twl6040->audpwron = -EINVAL;
488 if (gpio_is_valid(twl6040->audpwron)) {
489 ret = gpio_request(twl6040->audpwron, "audpwron");
493 ret = gpio_direction_output(twl6040->audpwron, 0);
498 /* codec interrupt */
499 ret = twl6040_irq_init(twl6040);
503 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
504 NULL, twl6040_naudint_handler, 0,
505 "twl6040_irq_ready", twl6040);
507 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
512 /* dual-access registers controlled by I2C only */
513 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
516 int irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
518 cell = &twl6040->cells[children];
519 cell->name = "twl6040-codec";
520 twl6040_codec_rsrc[0].start = irq;
521 twl6040_codec_rsrc[0].end = irq;
522 cell->resources = twl6040_codec_rsrc;
523 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
524 cell->platform_data = pdata->codec;
525 cell->pdata_size = sizeof(*pdata->codec);
530 int irq = twl6040->irq_base + TWL6040_IRQ_VIB;
532 cell = &twl6040->cells[children];
533 cell->name = "twl6040-vibra";
534 twl6040_vibra_rsrc[0].start = irq;
535 twl6040_vibra_rsrc[0].end = irq;
536 cell->resources = twl6040_vibra_rsrc;
537 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
539 cell->platform_data = pdata->vibra;
540 cell->pdata_size = sizeof(*pdata->vibra);
545 ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells,
550 dev_err(&pdev->dev, "No platform data found for children\n");
558 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
560 twl6040_irq_exit(twl6040);
562 if (gpio_is_valid(twl6040->audpwron))
563 gpio_free(twl6040->audpwron);
565 platform_set_drvdata(pdev, NULL);
570 static int __devexit twl6040_remove(struct platform_device *pdev)
572 struct twl6040 *twl6040 = platform_get_drvdata(pdev);
574 if (twl6040->power_count)
575 twl6040_power(twl6040, 0);
577 if (gpio_is_valid(twl6040->audpwron))
578 gpio_free(twl6040->audpwron);
580 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
581 twl6040_irq_exit(twl6040);
583 mfd_remove_devices(&pdev->dev);
584 platform_set_drvdata(pdev, NULL);
590 static struct platform_driver twl6040_driver = {
591 .probe = twl6040_probe,
592 .remove = __devexit_p(twl6040_remove),
594 .owner = THIS_MODULE,
599 static int __devinit twl6040_init(void)
601 return platform_driver_register(&twl6040_driver);
603 module_init(twl6040_init);
605 static void __devexit twl6040_exit(void)
607 platform_driver_unregister(&twl6040_driver);
610 module_exit(twl6040_exit);
612 MODULE_DESCRIPTION("TWL6040 MFD");
613 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
614 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
615 MODULE_LICENSE("GPL");
616 MODULE_ALIAS("platform:twl6040");