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/err.h>
31 #include <linux/platform_device.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_gpio.h>
35 #include <linux/of_platform.h>
36 #include <linux/gpio.h>
37 #include <linux/delay.h>
38 #include <linux/i2c.h>
39 #include <linux/regmap.h>
40 #include <linux/mfd/core.h>
41 #include <linux/mfd/twl6040.h>
42 #include <linux/regulator/consumer.h>
44 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
45 #define TWL6040_NUM_SUPPLIES (2)
47 static bool twl6040_has_vibra(struct device_node *node)
50 if (of_find_node_by_name(node, "vibra"))
56 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
61 /* Vibra control registers from cache */
62 if (unlikely(reg == TWL6040_REG_VIBCTLL ||
63 reg == TWL6040_REG_VIBCTLR)) {
64 val = twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)];
66 ret = regmap_read(twl6040->regmap, reg, &val);
73 EXPORT_SYMBOL(twl6040_reg_read);
75 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
79 ret = regmap_write(twl6040->regmap, reg, val);
80 /* Cache the vibra control registers */
81 if (reg == TWL6040_REG_VIBCTLL || reg == TWL6040_REG_VIBCTLR)
82 twl6040->vibra_ctrl_cache[VIBRACTRL_MEMBER(reg)] = val;
86 EXPORT_SYMBOL(twl6040_reg_write);
88 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
90 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
92 EXPORT_SYMBOL(twl6040_set_bits);
94 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
96 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
98 EXPORT_SYMBOL(twl6040_clear_bits);
100 /* twl6040 codec manual power-up sequence */
101 static int twl6040_power_up_manual(struct twl6040 *twl6040)
103 u8 ldoctl, ncpctl, lppllctl;
106 /* enable high-side LDO, reference system and internal oscillator */
107 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
108 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
111 usleep_range(10000, 10500);
113 /* enable negative charge pump */
114 ncpctl = TWL6040_NCPENA;
115 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
118 usleep_range(1000, 1500);
120 /* enable low-side LDO */
121 ldoctl |= TWL6040_LSLDOENA;
122 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
125 usleep_range(1000, 1500);
127 /* enable low-power PLL */
128 lppllctl = TWL6040_LPLLENA;
129 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
132 usleep_range(5000, 5500);
134 /* disable internal oscillator */
135 ldoctl &= ~TWL6040_OSCENA;
136 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
143 lppllctl &= ~TWL6040_LPLLENA;
144 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
146 ldoctl &= ~TWL6040_LSLDOENA;
147 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
149 ncpctl &= ~TWL6040_NCPENA;
150 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
152 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
153 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
155 dev_err(twl6040->dev, "manual power-up failed\n");
159 /* twl6040 manual power-down sequence */
160 static void twl6040_power_down_manual(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_readyint_handler(int irq, void *data)
192 struct twl6040 *twl6040 = data;
194 complete(&twl6040->ready);
199 static irqreturn_t twl6040_thint_handler(int irq, void *data)
201 struct twl6040 *twl6040 = data;
204 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
205 if (status & TWL6040_TSHUTDET) {
206 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
207 twl6040_power(twl6040, 0);
209 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
210 twl6040_power(twl6040, 1);
216 static int twl6040_power_up_automatic(struct twl6040 *twl6040)
220 gpio_set_value(twl6040->audpwron, 1);
222 time_left = wait_for_completion_timeout(&twl6040->ready,
223 msecs_to_jiffies(144));
227 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
228 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
229 if (!(intid & TWL6040_READYINT)) {
230 dev_err(twl6040->dev, "automatic power-up failed\n");
231 gpio_set_value(twl6040->audpwron, 0);
239 int twl6040_power(struct twl6040 *twl6040, int on)
243 mutex_lock(&twl6040->mutex);
246 /* already powered-up */
247 if (twl6040->power_count++)
250 if (gpio_is_valid(twl6040->audpwron)) {
251 /* use automatic power-up sequence */
252 ret = twl6040_power_up_automatic(twl6040);
254 twl6040->power_count = 0;
258 /* use manual power-up sequence */
259 ret = twl6040_power_up_manual(twl6040);
261 twl6040->power_count = 0;
265 /* Default PLL configuration after power up */
266 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
267 twl6040->sysclk = 19200000;
268 twl6040->mclk = 32768;
270 /* already powered-down */
271 if (!twl6040->power_count) {
272 dev_err(twl6040->dev,
273 "device is already powered-off\n");
278 if (--twl6040->power_count)
281 if (gpio_is_valid(twl6040->audpwron)) {
282 /* use AUDPWRON line */
283 gpio_set_value(twl6040->audpwron, 0);
285 /* power-down sequence latency */
286 usleep_range(500, 700);
288 /* use manual power-down sequence */
289 twl6040_power_down_manual(twl6040);
296 mutex_unlock(&twl6040->mutex);
299 EXPORT_SYMBOL(twl6040_power);
301 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
302 unsigned int freq_in, unsigned int freq_out)
304 u8 hppllctl, lppllctl;
307 mutex_lock(&twl6040->mutex);
309 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
310 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
312 /* Force full reconfiguration when switching between PLL */
313 if (pll_id != twl6040->pll) {
319 case TWL6040_SYSCLK_SEL_LPPLL:
320 /* low-power PLL divider */
321 /* Change the sysclk configuration only if it has been canged */
322 if (twl6040->sysclk != freq_out) {
325 lppllctl |= TWL6040_LPLLFIN;
328 lppllctl &= ~TWL6040_LPLLFIN;
331 dev_err(twl6040->dev,
332 "freq_out %d not supported\n",
337 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
341 /* The PLL in use has not been change, we can exit */
342 if (twl6040->pll == pll_id)
347 lppllctl |= TWL6040_LPLLENA;
348 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
351 lppllctl &= ~TWL6040_HPLLSEL;
352 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
354 hppllctl &= ~TWL6040_HPLLENA;
355 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
359 dev_err(twl6040->dev,
360 "freq_in %d not supported\n", freq_in);
365 case TWL6040_SYSCLK_SEL_HPPLL:
366 /* high-performance PLL can provide only 19.2 MHz */
367 if (freq_out != 19200000) {
368 dev_err(twl6040->dev,
369 "freq_out %d not supported\n", freq_out);
374 if (twl6040->mclk != freq_in) {
375 hppllctl &= ~TWL6040_MCLK_MSK;
379 /* PLL enabled, active mode */
380 hppllctl |= TWL6040_MCLK_12000KHZ |
386 * (enable PLL if MCLK jitter quality
387 * doesn't meet specification)
389 hppllctl |= TWL6040_MCLK_19200KHZ;
392 /* PLL enabled, active mode */
393 hppllctl |= TWL6040_MCLK_26000KHZ |
397 /* PLL enabled, active mode */
398 hppllctl |= TWL6040_MCLK_38400KHZ |
402 dev_err(twl6040->dev,
403 "freq_in %d not supported\n", freq_in);
409 * enable clock slicer to ensure input waveform is
412 hppllctl |= TWL6040_HPLLSQRENA;
414 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
416 usleep_range(500, 700);
417 lppllctl |= TWL6040_HPLLSEL;
418 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
420 lppllctl &= ~TWL6040_LPLLENA;
421 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
426 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
431 twl6040->sysclk = freq_out;
432 twl6040->mclk = freq_in;
433 twl6040->pll = pll_id;
436 mutex_unlock(&twl6040->mutex);
439 EXPORT_SYMBOL(twl6040_set_pll);
441 int twl6040_get_pll(struct twl6040 *twl6040)
443 if (twl6040->power_count)
448 EXPORT_SYMBOL(twl6040_get_pll);
450 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
452 return twl6040->sysclk;
454 EXPORT_SYMBOL(twl6040_get_sysclk);
456 /* Get the combined status of the vibra control register */
457 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
461 status = twl6040->vibra_ctrl_cache[0] | twl6040->vibra_ctrl_cache[1];
462 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
466 EXPORT_SYMBOL(twl6040_get_vibralr_status);
468 static struct resource twl6040_vibra_rsrc[] = {
470 .flags = IORESOURCE_IRQ,
474 static struct resource twl6040_codec_rsrc[] = {
476 .flags = IORESOURCE_IRQ,
480 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
482 /* Register 0 is not readable */
488 static struct regmap_config twl6040_regmap_config = {
491 .max_register = TWL6040_REG_STATUS, /* 0x2e */
493 .readable_reg = twl6040_readable_reg,
496 static const struct regmap_irq twl6040_irqs[] = {
497 { .reg_offset = 0, .mask = TWL6040_THINT, },
498 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
499 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
500 { .reg_offset = 0, .mask = TWL6040_HFINT, },
501 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
502 { .reg_offset = 0, .mask = TWL6040_READYINT, },
505 static struct regmap_irq_chip twl6040_irq_chip = {
507 .irqs = twl6040_irqs,
508 .num_irqs = ARRAY_SIZE(twl6040_irqs),
511 .status_base = TWL6040_REG_INTID,
512 .mask_base = TWL6040_REG_INTMR,
515 static int twl6040_probe(struct i2c_client *client,
516 const struct i2c_device_id *id)
518 struct device_node *node = client->dev.of_node;
519 struct twl6040 *twl6040;
520 struct mfd_cell *cell = NULL;
521 int irq, ret, children = 0;
524 dev_err(&client->dev, "of node is missing\n");
528 /* In order to operate correctly we need valid interrupt config */
530 dev_err(&client->dev, "Invalid IRQ configuration\n");
534 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
539 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
540 if (IS_ERR(twl6040->regmap))
541 return PTR_ERR(twl6040->regmap);
543 i2c_set_clientdata(client, twl6040);
545 twl6040->supplies[0].supply = "vio";
546 twl6040->supplies[1].supply = "v2v1";
547 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
550 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
551 goto regulator_get_err;
554 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
556 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
557 goto regulator_get_err;
560 twl6040->dev = &client->dev;
561 twl6040->irq = client->irq;
563 mutex_init(&twl6040->mutex);
564 init_completion(&twl6040->ready);
566 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
568 /* ERRATA: Automatic power-up is not possible in ES1.0 */
569 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
570 twl6040->audpwron = of_get_named_gpio(node,
571 "ti,audpwron-gpio", 0);
573 twl6040->audpwron = -EINVAL;
575 if (gpio_is_valid(twl6040->audpwron)) {
576 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
577 GPIOF_OUT_INIT_LOW, "audpwron");
582 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
583 0, &twl6040_irq_chip,&twl6040->irq_data);
587 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
589 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
592 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
593 twl6040_readyint_handler, IRQF_ONESHOT,
594 "twl6040_irq_ready", twl6040);
596 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
600 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
601 twl6040_thint_handler, IRQF_ONESHOT,
602 "twl6040_irq_th", twl6040);
604 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
608 /* dual-access registers controlled by I2C only */
609 twl6040_set_bits(twl6040, TWL6040_REG_ACCCTL, TWL6040_I2CSEL);
612 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
613 * We can add the ASoC codec child whenever this driver has been loaded.
615 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
616 cell = &twl6040->cells[children];
617 cell->name = "twl6040-codec";
618 twl6040_codec_rsrc[0].start = irq;
619 twl6040_codec_rsrc[0].end = irq;
620 cell->resources = twl6040_codec_rsrc;
621 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
624 /* Vibra input driver support */
625 if (twl6040_has_vibra(node)) {
626 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
628 cell = &twl6040->cells[children];
629 cell->name = "twl6040-vibra";
630 twl6040_vibra_rsrc[0].start = irq;
631 twl6040_vibra_rsrc[0].end = irq;
632 cell->resources = twl6040_vibra_rsrc;
633 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
638 cell = &twl6040->cells[children];
639 cell->name = "twl6040-gpo";
642 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
650 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
652 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
654 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
656 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
658 i2c_set_clientdata(client, NULL);
663 static int twl6040_remove(struct i2c_client *client)
665 struct twl6040 *twl6040 = i2c_get_clientdata(client);
667 if (twl6040->power_count)
668 twl6040_power(twl6040, 0);
670 devm_free_irq(&client->dev, twl6040->irq_ready, twl6040);
671 devm_free_irq(&client->dev, twl6040->irq_th, twl6040);
672 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
674 mfd_remove_devices(&client->dev);
675 i2c_set_clientdata(client, NULL);
677 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
682 static const struct i2c_device_id twl6040_i2c_id[] = {
687 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
689 static struct i2c_driver twl6040_driver = {
692 .owner = THIS_MODULE,
694 .probe = twl6040_probe,
695 .remove = twl6040_remove,
696 .id_table = twl6040_i2c_id,
699 module_i2c_driver(twl6040_driver);
701 MODULE_DESCRIPTION("TWL6040 MFD");
702 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
703 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
704 MODULE_LICENSE("GPL");
705 MODULE_ALIAS("platform:twl6040");