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 static struct platform_device *twl6040_dev;
39 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
44 mutex_lock(&twl6040->io_mutex);
45 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
47 mutex_unlock(&twl6040->io_mutex);
50 mutex_unlock(&twl6040->io_mutex);
54 EXPORT_SYMBOL(twl6040_reg_read);
56 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
60 mutex_lock(&twl6040->io_mutex);
61 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
62 mutex_unlock(&twl6040->io_mutex);
66 EXPORT_SYMBOL(twl6040_reg_write);
68 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
73 mutex_lock(&twl6040->io_mutex);
74 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
79 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
81 mutex_unlock(&twl6040->io_mutex);
84 EXPORT_SYMBOL(twl6040_set_bits);
86 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
91 mutex_lock(&twl6040->io_mutex);
92 ret = twl_i2c_read_u8(TWL_MODULE_AUDIO_VOICE, &val, reg);
97 ret = twl_i2c_write_u8(TWL_MODULE_AUDIO_VOICE, val, reg);
99 mutex_unlock(&twl6040->io_mutex);
102 EXPORT_SYMBOL(twl6040_clear_bits);
104 /* twl6040 codec manual power-up sequence */
105 static int twl6040_power_up(struct twl6040 *twl6040)
107 u8 ldoctl, ncpctl, lppllctl;
110 /* enable high-side LDO, reference system and internal oscillator */
111 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
112 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
115 usleep_range(10000, 10500);
117 /* enable negative charge pump */
118 ncpctl = TWL6040_NCPENA;
119 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
122 usleep_range(1000, 1500);
124 /* enable low-side LDO */
125 ldoctl |= TWL6040_LSLDOENA;
126 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
129 usleep_range(1000, 1500);
131 /* enable low-power PLL */
132 lppllctl = TWL6040_LPLLENA;
133 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
136 usleep_range(5000, 5500);
138 /* disable internal oscillator */
139 ldoctl &= ~TWL6040_OSCENA;
140 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
147 lppllctl &= ~TWL6040_LPLLENA;
148 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
150 ldoctl &= ~TWL6040_LSLDOENA;
151 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
153 ncpctl &= ~TWL6040_NCPENA;
154 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
156 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
157 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
162 /* twl6040 manual power-down sequence */
163 static void twl6040_power_down(struct twl6040 *twl6040)
165 u8 ncpctl, ldoctl, lppllctl;
167 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
168 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
169 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
171 /* enable internal oscillator */
172 ldoctl |= TWL6040_OSCENA;
173 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
174 usleep_range(1000, 1500);
176 /* disable low-power PLL */
177 lppllctl &= ~TWL6040_LPLLENA;
178 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
180 /* disable low-side LDO */
181 ldoctl &= ~TWL6040_LSLDOENA;
182 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
184 /* disable negative charge pump */
185 ncpctl &= ~TWL6040_NCPENA;
186 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
188 /* disable high-side LDO, reference system and internal oscillator */
189 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
190 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
193 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
195 struct twl6040 *twl6040 = data;
198 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
200 if (intid & TWL6040_READYINT)
201 complete(&twl6040->ready);
203 if (intid & TWL6040_THINT) {
204 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
205 if (status & TWL6040_TSHUTDET) {
206 dev_warn(&twl6040_dev->dev,
207 "Thermal shutdown, powering-off");
208 twl6040_power(twl6040, 0);
210 dev_warn(&twl6040_dev->dev,
211 "Leaving thermal shutdown, powering-on");
212 twl6040_power(twl6040, 1);
219 static int twl6040_power_up_completion(struct twl6040 *twl6040,
225 time_left = wait_for_completion_timeout(&twl6040->ready,
226 msecs_to_jiffies(144));
228 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
229 if (!(intid & TWL6040_READYINT)) {
230 dev_err(&twl6040_dev->dev,
231 "timeout waiting for READYINT\n");
239 int twl6040_power(struct twl6040 *twl6040, int on)
241 int audpwron = twl6040->audpwron;
242 int naudint = twl6040->irq;
245 mutex_lock(&twl6040->mutex);
248 /* already powered-up */
249 if (twl6040->power_count++)
252 if (gpio_is_valid(audpwron)) {
253 /* use AUDPWRON line */
254 gpio_set_value(audpwron, 1);
255 /* wait for power-up completion */
256 ret = twl6040_power_up_completion(twl6040, naudint);
258 dev_err(&twl6040_dev->dev,
259 "automatic power-down failed\n");
260 twl6040->power_count = 0;
264 /* use manual power-up sequence */
265 ret = twl6040_power_up(twl6040);
267 dev_err(&twl6040_dev->dev,
268 "manual power-up failed\n");
269 twl6040->power_count = 0;
273 twl6040->pll = TWL6040_LPPLL_ID;
274 twl6040->sysclk = 19200000;
276 /* already powered-down */
277 if (!twl6040->power_count) {
278 dev_err(&twl6040_dev->dev,
279 "device is already powered-off\n");
284 if (--twl6040->power_count)
287 if (gpio_is_valid(audpwron)) {
288 /* use AUDPWRON line */
289 gpio_set_value(audpwron, 0);
291 /* power-down sequence latency */
292 usleep_range(500, 700);
294 /* use manual power-down sequence */
295 twl6040_power_down(twl6040);
297 twl6040->pll = TWL6040_NOPLL_ID;
302 mutex_unlock(&twl6040->mutex);
305 EXPORT_SYMBOL(twl6040_power);
307 int twl6040_set_pll(struct twl6040 *twl6040, enum twl6040_pll_id id,
308 unsigned int freq_in, unsigned int freq_out)
310 u8 hppllctl, lppllctl;
313 mutex_lock(&twl6040->mutex);
315 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
316 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
319 case TWL6040_LPPLL_ID:
320 /* low-power PLL divider */
323 lppllctl |= TWL6040_LPLLFIN;
326 lppllctl &= ~TWL6040_LPLLFIN;
329 dev_err(&twl6040_dev->dev,
330 "freq_out %d not supported\n", freq_out);
334 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
338 lppllctl |= TWL6040_LPLLENA;
339 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
342 lppllctl &= ~TWL6040_HPLLSEL;
343 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
345 hppllctl &= ~TWL6040_HPLLENA;
346 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
350 dev_err(&twl6040_dev->dev,
351 "freq_in %d not supported\n", freq_in);
356 twl6040->pll = TWL6040_LPPLL_ID;
358 case TWL6040_HPPLL_ID:
359 /* high-performance PLL can provide only 19.2 MHz */
360 if (freq_out != 19200000) {
361 dev_err(&twl6040_dev->dev,
362 "freq_out %d not supported\n", freq_out);
367 hppllctl &= ~TWL6040_MCLK_MSK;
371 /* PLL enabled, active mode */
372 hppllctl |= TWL6040_MCLK_12000KHZ |
378 * (enable PLL if MCLK jitter quality
379 * doesn't meet specification)
381 hppllctl |= TWL6040_MCLK_19200KHZ;
384 /* PLL enabled, active mode */
385 hppllctl |= TWL6040_MCLK_26000KHZ |
389 /* PLL enabled, active mode */
390 hppllctl |= TWL6040_MCLK_38400KHZ |
394 dev_err(&twl6040_dev->dev,
395 "freq_in %d not supported\n", freq_in);
400 /* enable clock slicer to ensure input waveform is square */
401 hppllctl |= TWL6040_HPLLSQRENA;
403 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL, hppllctl);
404 usleep_range(500, 700);
405 lppllctl |= TWL6040_HPLLSEL;
406 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
407 lppllctl &= ~TWL6040_LPLLENA;
408 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
410 twl6040->pll = TWL6040_HPPLL_ID;
413 dev_err(&twl6040_dev->dev, "unknown pll id %d\n", id);
418 twl6040->sysclk = freq_out;
421 mutex_unlock(&twl6040->mutex);
424 EXPORT_SYMBOL(twl6040_set_pll);
426 enum twl6040_pll_id twl6040_get_pll(struct twl6040 *twl6040)
430 EXPORT_SYMBOL(twl6040_get_pll);
432 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
434 return twl6040->sysclk;
436 EXPORT_SYMBOL(twl6040_get_sysclk);
438 static int __devinit twl6040_probe(struct platform_device *pdev)
440 struct twl4030_audio_data *pdata = pdev->dev.platform_data;
441 struct twl6040 *twl6040;
442 struct mfd_cell *cell = NULL;
443 int ret, children = 0;
446 dev_err(&pdev->dev, "Platform data is missing\n");
450 twl6040 = kzalloc(sizeof(struct twl6040), GFP_KERNEL);
454 platform_set_drvdata(pdev, twl6040);
457 twl6040->dev = &pdev->dev;
458 twl6040->audpwron = pdata->audpwron_gpio;
459 twl6040->irq = pdata->naudint_irq;
460 twl6040->irq_base = pdata->irq_base;
462 mutex_init(&twl6040->mutex);
463 mutex_init(&twl6040->io_mutex);
464 init_completion(&twl6040->ready);
466 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
468 if (gpio_is_valid(twl6040->audpwron)) {
469 ret = gpio_request(twl6040->audpwron, "audpwron");
473 ret = gpio_direction_output(twl6040->audpwron, 0);
478 /* ERRATA: Automatic power-up is not possible in ES1.0 */
479 if (twl6040->rev == TWL6040_REV_ES1_0)
480 twl6040->audpwron = -EINVAL;
483 /* codec interrupt */
484 ret = twl6040_irq_init(twl6040);
488 ret = twl6040_request_irq(twl6040, TWL6040_IRQ_READY,
489 twl6040_naudint_handler, 0,
490 "twl6040_irq_ready", twl6040);
492 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
498 /* dual-access registers controlled by I2C only */
499 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
502 cell = &twl6040->cells[children];
503 cell->name = "twl6040-codec";
504 cell->platform_data = pdata->codec;
505 cell->pdata_size = sizeof(*pdata->codec);
510 cell = &twl6040->cells[children];
511 cell->name = "twl6040-vibra";
512 cell->platform_data = pdata->vibra;
513 cell->pdata_size = sizeof(*pdata->vibra);
518 ret = mfd_add_devices(&pdev->dev, pdev->id, twl6040->cells,
523 dev_err(&pdev->dev, "No platform data found for children\n");
532 twl6040_free_irq(twl6040, TWL6040_IRQ_READY, twl6040);
535 twl6040_irq_exit(twl6040);
537 if (gpio_is_valid(twl6040->audpwron))
538 gpio_free(twl6040->audpwron);
540 platform_set_drvdata(pdev, NULL);
546 static int __devexit twl6040_remove(struct platform_device *pdev)
548 struct twl6040 *twl6040 = platform_get_drvdata(pdev);
550 if (twl6040->power_count)
551 twl6040_power(twl6040, 0);
553 if (gpio_is_valid(twl6040->audpwron))
554 gpio_free(twl6040->audpwron);
556 twl6040_free_irq(twl6040, TWL6040_IRQ_READY, twl6040);
559 twl6040_irq_exit(twl6040);
561 mfd_remove_devices(&pdev->dev);
562 platform_set_drvdata(pdev, NULL);
569 static struct platform_driver twl6040_driver = {
570 .probe = twl6040_probe,
571 .remove = __devexit_p(twl6040_remove),
573 .owner = THIS_MODULE,
578 static int __devinit twl6040_init(void)
580 return platform_driver_register(&twl6040_driver);
582 module_init(twl6040_init);
584 static void __devexit twl6040_exit(void)
586 platform_driver_unregister(&twl6040_driver);
589 module_exit(twl6040_exit);
591 MODULE_DESCRIPTION("TWL6040 MFD");
592 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
593 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
594 MODULE_LICENSE("GPL");
595 MODULE_ALIAS("platform:twl6040");