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.h>
34 #include <linux/regmap.h>
35 #include <linux/err.h>
36 #include <linux/mfd/core.h>
37 #include <linux/mfd/twl6040.h>
39 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
41 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
46 mutex_lock(&twl6040->io_mutex);
47 /* Vibra control registers from cache */
48 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
49 reg == TWL6040_REG_VIBCTLR)) {
50 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
52 ret = regmap_read(twl6040->regmap, reg, &val);
54 mutex_unlock(&twl6040->io_mutex);
58 mutex_unlock(&twl6040->io_mutex);
62 EXPORT_SYMBOL(twl6040_reg_read);
64 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
68 mutex_lock(&twl6040->io_mutex);
69 ret = regmap_write(twl6040->regmap, reg, val);
70 /* Cache the vibra control registers */
71 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
72 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
73 mutex_unlock(&twl6040->io_mutex);
77 EXPORT_SYMBOL(twl6040_reg_write);
79 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
83 mutex_lock(&twl6040->io_mutex);
84 ret = regmap_update_bits(twl6040->regmap, reg, mask, mask);
85 mutex_unlock(&twl6040->io_mutex);
88 EXPORT_SYMBOL(twl6040_set_bits);
90 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
94 mutex_lock(&twl6040->io_mutex);
95 ret = regmap_update_bits(twl6040->regmap, reg, mask, 0);
96 mutex_unlock(&twl6040->io_mutex);
99 EXPORT_SYMBOL(twl6040_clear_bits);
101 /* twl6040 codec manual power-up sequence */
102 static int twl6040_power_up(struct twl6040 *twl6040)
104 u8 ldoctl, ncpctl, lppllctl;
107 /* enable high-side LDO, reference system and internal oscillator */
108 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
109 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
112 usleep_range(10000, 10500);
114 /* enable negative charge pump */
115 ncpctl = TWL6040_NCPENA;
116 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
119 usleep_range(1000, 1500);
121 /* enable low-side LDO */
122 ldoctl |= TWL6040_LSLDOENA;
123 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
126 usleep_range(1000, 1500);
128 /* enable low-power PLL */
129 lppllctl = TWL6040_LPLLENA;
130 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
133 usleep_range(5000, 5500);
135 /* disable internal oscillator */
136 ldoctl &= ~TWL6040_OSCENA;
137 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
144 lppllctl &= ~TWL6040_LPLLENA;
145 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
147 ldoctl &= ~TWL6040_LSLDOENA;
148 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
150 ncpctl &= ~TWL6040_NCPENA;
151 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
153 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
154 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
159 /* twl6040 manual power-down sequence */
160 static void twl6040_power_down(struct twl6040 *twl6040)
162 u8 ncpctl, ldoctl, lppllctl;
164 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
165 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
166 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
168 /* enable internal oscillator */
169 ldoctl |= TWL6040_OSCENA;
170 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
171 usleep_range(1000, 1500);
173 /* disable low-power PLL */
174 lppllctl &= ~TWL6040_LPLLENA;
175 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
177 /* disable low-side LDO */
178 ldoctl &= ~TWL6040_LSLDOENA;
179 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
181 /* disable negative charge pump */
182 ncpctl &= ~TWL6040_NCPENA;
183 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
185 /* disable high-side LDO, reference system and internal oscillator */
186 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
187 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
190 static irqreturn_t twl6040_naudint_handler(int irq, void *data)
192 struct twl6040 *twl6040 = data;
195 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
197 if (intid & TWL6040_READYINT)
198 complete(&twl6040->ready);
200 if (intid & TWL6040_THINT) {
201 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
202 if (status & TWL6040_TSHUTDET) {
203 dev_warn(twl6040->dev,
204 "Thermal shutdown, powering-off");
205 twl6040_power(twl6040, 0);
207 dev_warn(twl6040->dev,
208 "Leaving thermal shutdown, powering-on");
209 twl6040_power(twl6040, 1);
216 static int twl6040_power_up_completion(struct twl6040 *twl6040,
222 time_left = wait_for_completion_timeout(&twl6040->ready,
223 msecs_to_jiffies(144));
225 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
226 if (!(intid & TWL6040_READYINT)) {
227 dev_err(twl6040->dev,
228 "timeout waiting for READYINT\n");
236 int twl6040_power(struct twl6040 *twl6040, int on)
238 int audpwron = twl6040->audpwron;
239 int naudint = twl6040->irq;
242 mutex_lock(&twl6040->mutex);
245 /* already powered-up */
246 if (twl6040->power_count++)
249 if (gpio_is_valid(audpwron)) {
250 /* use AUDPWRON line */
251 gpio_set_value(audpwron, 1);
252 /* wait for power-up completion */
253 ret = twl6040_power_up_completion(twl6040, naudint);
255 dev_err(twl6040->dev,
256 "automatic power-down failed\n");
257 twl6040->power_count = 0;
261 /* use manual power-up sequence */
262 ret = twl6040_power_up(twl6040);
264 dev_err(twl6040->dev,
265 "manual power-up failed\n");
266 twl6040->power_count = 0;
270 /* Default PLL configuration after power up */
271 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
272 twl6040->sysclk = 19200000;
273 twl6040->mclk = 32768;
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);
301 mutex_unlock(&twl6040->mutex);
304 EXPORT_SYMBOL(twl6040_power);
306 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
307 unsigned int freq_in, unsigned int freq_out)
309 u8 hppllctl, lppllctl;
312 mutex_lock(&twl6040->mutex);
314 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
315 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
317 /* Force full reconfiguration when switching between PLL */
318 if (pll_id != twl6040->pll) {
324 case TWL6040_SYSCLK_SEL_LPPLL:
325 /* low-power PLL divider */
326 /* Change the sysclk configuration only if it has been canged */
327 if (twl6040->sysclk != freq_out) {
330 lppllctl |= TWL6040_LPLLFIN;
333 lppllctl &= ~TWL6040_LPLLFIN;
336 dev_err(twl6040->dev,
337 "freq_out %d not supported\n",
342 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
346 /* The PLL in use has not been change, we can exit */
347 if (twl6040->pll == pll_id)
352 lppllctl |= TWL6040_LPLLENA;
353 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
356 lppllctl &= ~TWL6040_HPLLSEL;
357 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
359 hppllctl &= ~TWL6040_HPLLENA;
360 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
364 dev_err(twl6040->dev,
365 "freq_in %d not supported\n", freq_in);
370 case TWL6040_SYSCLK_SEL_HPPLL:
371 /* high-performance PLL can provide only 19.2 MHz */
372 if (freq_out != 19200000) {
373 dev_err(twl6040->dev,
374 "freq_out %d not supported\n", freq_out);
379 if (twl6040->mclk != freq_in) {
380 hppllctl &= ~TWL6040_MCLK_MSK;
384 /* PLL enabled, active mode */
385 hppllctl |= TWL6040_MCLK_12000KHZ |
391 * (enable PLL if MCLK jitter quality
392 * doesn't meet specification)
394 hppllctl |= TWL6040_MCLK_19200KHZ;
397 /* PLL enabled, active mode */
398 hppllctl |= TWL6040_MCLK_26000KHZ |
402 /* PLL enabled, active mode */
403 hppllctl |= TWL6040_MCLK_38400KHZ |
407 dev_err(twl6040->dev,
408 "freq_in %d not supported\n", freq_in);
414 * enable clock slicer to ensure input waveform is
417 hppllctl |= TWL6040_HPLLSQRENA;
419 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
421 usleep_range(500, 700);
422 lppllctl |= TWL6040_HPLLSEL;
423 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
425 lppllctl &= ~TWL6040_LPLLENA;
426 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
431 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
436 twl6040->sysclk = freq_out;
437 twl6040->mclk = freq_in;
438 twl6040->pll = pll_id;
441 mutex_unlock(&twl6040->mutex);
444 EXPORT_SYMBOL(twl6040_set_pll);
446 int twl6040_get_pll(struct twl6040 *twl6040)
448 if (twl6040->power_count)
453 EXPORT_SYMBOL(twl6040_get_pll);
455 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
457 return twl6040->sysclk;
459 EXPORT_SYMBOL(twl6040_get_sysclk);
461 /* Get the combined status of the vibra control register */
462 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
466 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
467 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
471 EXPORT_SYMBOL(twl6040_get_vibralr_status);
473 static struct resource twl6040_vibra_rsrc[] = {
475 .flags = IORESOURCE_IRQ,
479 static struct resource twl6040_codec_rsrc[] = {
481 .flags = IORESOURCE_IRQ,
485 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
487 /* Register 0 is not readable */
493 static struct regmap_config twl6040_regmap_config = {
496 .max_register = TWL6040_REG_STATUS, /* 0x2e */
498 .readable_reg = twl6040_readable_reg,
501 static int __devinit twl6040_probe(struct i2c_client *client,
502 const struct i2c_device_id *id)
504 struct twl6040_platform_data *pdata = client->dev.platform_data;
505 struct twl6040 *twl6040;
506 struct mfd_cell *cell = NULL;
507 int ret, children = 0;
510 dev_err(&client->dev, "Platform data is missing\n");
514 /* In order to operate correctly we need valid interrupt config */
515 if (!client->irq || !pdata->irq_base) {
516 dev_err(&client->dev, "Invalid IRQ configuration\n");
520 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
527 twl6040->regmap = regmap_init_i2c(client, &twl6040_regmap_config);
528 if (IS_ERR(twl6040->regmap)) {
529 ret = PTR_ERR(twl6040->regmap);
533 i2c_set_clientdata(client, twl6040);
535 twl6040->dev = &client->dev;
536 twl6040->irq = client->irq;
537 twl6040->irq_base = pdata->irq_base;
539 mutex_init(&twl6040->mutex);
540 mutex_init(&twl6040->io_mutex);
541 init_completion(&twl6040->ready);
543 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
545 /* ERRATA: Automatic power-up is not possible in ES1.0 */
546 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
547 twl6040->audpwron = pdata->audpwron_gpio;
549 twl6040->audpwron = -EINVAL;
551 if (gpio_is_valid(twl6040->audpwron)) {
552 ret = gpio_request_one(twl6040->audpwron, GPIOF_OUT_INIT_LOW,
558 /* codec interrupt */
559 ret = twl6040_irq_init(twl6040);
563 ret = request_threaded_irq(twl6040->irq_base + TWL6040_IRQ_READY,
564 NULL, twl6040_naudint_handler, 0,
565 "twl6040_irq_ready", twl6040);
567 dev_err(twl6040->dev, "READY IRQ request failed: %d\n",
572 /* dual-access registers controlled by I2C only */
573 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
576 int irq = twl6040->irq_base + TWL6040_IRQ_PLUG;
578 cell = &twl6040->cells[children];
579 cell->name = "twl6040-codec";
580 twl6040_codec_rsrc[0].start = irq;
581 twl6040_codec_rsrc[0].end = irq;
582 cell->resources = twl6040_codec_rsrc;
583 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
584 cell->platform_data = pdata->codec;
585 cell->pdata_size = sizeof(*pdata->codec);
590 int irq = twl6040->irq_base + TWL6040_IRQ_VIB;
592 cell = &twl6040->cells[children];
593 cell->name = "twl6040-vibra";
594 twl6040_vibra_rsrc[0].start = irq;
595 twl6040_vibra_rsrc[0].end = irq;
596 cell->resources = twl6040_vibra_rsrc;
597 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
599 cell->platform_data = pdata->vibra;
600 cell->pdata_size = sizeof(*pdata->vibra);
605 ret = mfd_add_devices(&client->dev, -1, twl6040->cells,
610 dev_err(&client->dev, "No platform data found for children\n");
618 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
620 twl6040_irq_exit(twl6040);
622 if (gpio_is_valid(twl6040->audpwron))
623 gpio_free(twl6040->audpwron);
625 i2c_set_clientdata(client, NULL);
626 regmap_exit(twl6040->regmap);
631 static int __devexit twl6040_remove(struct i2c_client *client)
633 struct twl6040 *twl6040 = i2c_get_clientdata(client);
635 if (twl6040->power_count)
636 twl6040_power(twl6040, 0);
638 if (gpio_is_valid(twl6040->audpwron))
639 gpio_free(twl6040->audpwron);
641 free_irq(twl6040->irq_base + TWL6040_IRQ_READY, twl6040);
642 twl6040_irq_exit(twl6040);
644 mfd_remove_devices(&client->dev);
645 i2c_set_clientdata(client, NULL);
646 regmap_exit(twl6040->regmap);
651 static const struct i2c_device_id twl6040_i2c_id[] = {
655 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
657 static struct i2c_driver twl6040_driver = {
660 .owner = THIS_MODULE,
662 .probe = twl6040_probe,
663 .remove = __devexit_p(twl6040_remove),
664 .id_table = twl6040_i2c_id,
667 module_i2c_driver(twl6040_driver);
669 MODULE_DESCRIPTION("TWL6040 MFD");
670 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
671 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:twl6040");