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 twl6040_platform_data
*pdata
,
48 struct device_node
*node
)
50 if (pdata
&& pdata
->vibra
)
54 if (of_find_node_by_name(node
, "vibra"))
61 int twl6040_reg_read(struct twl6040
*twl6040
, unsigned int reg
)
66 /* Vibra control registers from cache */
67 if (unlikely(reg
== TWL6040_REG_VIBCTLL
||
68 reg
== TWL6040_REG_VIBCTLR
)) {
69 val
= twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)];
71 ret
= regmap_read(twl6040
->regmap
, reg
, &val
);
78 EXPORT_SYMBOL(twl6040_reg_read
);
80 int twl6040_reg_write(struct twl6040
*twl6040
, unsigned int reg
, u8 val
)
84 ret
= regmap_write(twl6040
->regmap
, reg
, val
);
85 /* Cache the vibra control registers */
86 if (reg
== TWL6040_REG_VIBCTLL
|| reg
== TWL6040_REG_VIBCTLR
)
87 twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)] = val
;
91 EXPORT_SYMBOL(twl6040_reg_write
);
93 int twl6040_set_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
95 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, mask
);
97 EXPORT_SYMBOL(twl6040_set_bits
);
99 int twl6040_clear_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
101 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, 0);
103 EXPORT_SYMBOL(twl6040_clear_bits
);
105 /* twl6040 codec manual power-up sequence */
106 static int twl6040_power_up_manual(struct twl6040
*twl6040
)
108 u8 ldoctl
, ncpctl
, lppllctl
;
111 /* enable high-side LDO, reference system and internal oscillator */
112 ldoctl
= TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
;
113 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
116 usleep_range(10000, 10500);
118 /* enable negative charge pump */
119 ncpctl
= TWL6040_NCPENA
;
120 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
123 usleep_range(1000, 1500);
125 /* enable low-side LDO */
126 ldoctl
|= TWL6040_LSLDOENA
;
127 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
130 usleep_range(1000, 1500);
132 /* enable low-power PLL */
133 lppllctl
= TWL6040_LPLLENA
;
134 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
137 usleep_range(5000, 5500);
139 /* disable internal oscillator */
140 ldoctl
&= ~TWL6040_OSCENA
;
141 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
148 lppllctl
&= ~TWL6040_LPLLENA
;
149 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
151 ldoctl
&= ~TWL6040_LSLDOENA
;
152 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
154 ncpctl
&= ~TWL6040_NCPENA
;
155 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
157 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
158 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
160 dev_err(twl6040
->dev
, "manual power-up failed\n");
164 /* twl6040 manual power-down sequence */
165 static void twl6040_power_down_manual(struct twl6040
*twl6040
)
167 u8 ncpctl
, ldoctl
, lppllctl
;
169 ncpctl
= twl6040_reg_read(twl6040
, TWL6040_REG_NCPCTL
);
170 ldoctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LDOCTL
);
171 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
173 /* enable internal oscillator */
174 ldoctl
|= TWL6040_OSCENA
;
175 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
176 usleep_range(1000, 1500);
178 /* disable low-power PLL */
179 lppllctl
&= ~TWL6040_LPLLENA
;
180 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
182 /* disable low-side LDO */
183 ldoctl
&= ~TWL6040_LSLDOENA
;
184 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
186 /* disable negative charge pump */
187 ncpctl
&= ~TWL6040_NCPENA
;
188 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
190 /* disable high-side LDO, reference system and internal oscillator */
191 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
192 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
195 static irqreturn_t
twl6040_readyint_handler(int irq
, void *data
)
197 struct twl6040
*twl6040
= data
;
199 complete(&twl6040
->ready
);
204 static irqreturn_t
twl6040_thint_handler(int irq
, void *data
)
206 struct twl6040
*twl6040
= data
;
209 status
= twl6040_reg_read(twl6040
, TWL6040_REG_STATUS
);
210 if (status
& TWL6040_TSHUTDET
) {
211 dev_warn(twl6040
->dev
, "Thermal shutdown, powering-off");
212 twl6040_power(twl6040
, 0);
214 dev_warn(twl6040
->dev
, "Leaving thermal shutdown, powering-on");
215 twl6040_power(twl6040
, 1);
221 static int twl6040_power_up_automatic(struct twl6040
*twl6040
)
225 gpio_set_value(twl6040
->audpwron
, 1);
227 time_left
= wait_for_completion_timeout(&twl6040
->ready
,
228 msecs_to_jiffies(144));
232 dev_warn(twl6040
->dev
, "timeout waiting for READYINT\n");
233 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
234 if (!(intid
& TWL6040_READYINT
)) {
235 dev_err(twl6040
->dev
, "automatic power-up failed\n");
236 gpio_set_value(twl6040
->audpwron
, 0);
244 int twl6040_power(struct twl6040
*twl6040
, int on
)
248 mutex_lock(&twl6040
->mutex
);
251 /* already powered-up */
252 if (twl6040
->power_count
++)
255 if (gpio_is_valid(twl6040
->audpwron
)) {
256 /* use automatic power-up sequence */
257 ret
= twl6040_power_up_automatic(twl6040
);
259 twl6040
->power_count
= 0;
263 /* use manual power-up sequence */
264 ret
= twl6040_power_up_manual(twl6040
);
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(twl6040
->audpwron
)) {
287 /* use AUDPWRON line */
288 gpio_set_value(twl6040
->audpwron
, 0);
290 /* power-down sequence latency */
291 usleep_range(500, 700);
293 /* use manual power-down sequence */
294 twl6040_power_down_manual(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 const struct regmap_irq twl6040_irqs
[] = {
502 { .reg_offset
= 0, .mask
= TWL6040_THINT
, },
503 { .reg_offset
= 0, .mask
= TWL6040_PLUGINT
| TWL6040_UNPLUGINT
, },
504 { .reg_offset
= 0, .mask
= TWL6040_HOOKINT
, },
505 { .reg_offset
= 0, .mask
= TWL6040_HFINT
, },
506 { .reg_offset
= 0, .mask
= TWL6040_VIBINT
, },
507 { .reg_offset
= 0, .mask
= TWL6040_READYINT
, },
510 static struct regmap_irq_chip twl6040_irq_chip
= {
512 .irqs
= twl6040_irqs
,
513 .num_irqs
= ARRAY_SIZE(twl6040_irqs
),
516 .status_base
= TWL6040_REG_INTID
,
517 .mask_base
= TWL6040_REG_INTMR
,
520 static int twl6040_probe(struct i2c_client
*client
,
521 const struct i2c_device_id
*id
)
523 struct twl6040_platform_data
*pdata
= client
->dev
.platform_data
;
524 struct device_node
*node
= client
->dev
.of_node
;
525 struct twl6040
*twl6040
;
526 struct mfd_cell
*cell
= NULL
;
527 int irq
, ret
, children
= 0;
529 if (!pdata
&& !node
) {
530 dev_err(&client
->dev
, "Platform data is missing\n");
534 /* In order to operate correctly we need valid interrupt config */
536 dev_err(&client
->dev
, "Invalid IRQ configuration\n");
540 twl6040
= devm_kzalloc(&client
->dev
, sizeof(struct twl6040
),
547 twl6040
->regmap
= devm_regmap_init_i2c(client
, &twl6040_regmap_config
);
548 if (IS_ERR(twl6040
->regmap
)) {
549 ret
= PTR_ERR(twl6040
->regmap
);
553 i2c_set_clientdata(client
, twl6040
);
555 twl6040
->supplies
[0].supply
= "vio";
556 twl6040
->supplies
[1].supply
= "v2v1";
557 ret
= devm_regulator_bulk_get(&client
->dev
, TWL6040_NUM_SUPPLIES
,
560 dev_err(&client
->dev
, "Failed to get supplies: %d\n", ret
);
561 goto regulator_get_err
;
564 ret
= regulator_bulk_enable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
566 dev_err(&client
->dev
, "Failed to enable supplies: %d\n", ret
);
567 goto regulator_get_err
;
570 twl6040
->dev
= &client
->dev
;
571 twl6040
->irq
= client
->irq
;
573 mutex_init(&twl6040
->mutex
);
574 init_completion(&twl6040
->ready
);
576 twl6040
->rev
= twl6040_reg_read(twl6040
, TWL6040_REG_ASICREV
);
578 /* ERRATA: Automatic power-up is not possible in ES1.0 */
579 if (twl6040_get_revid(twl6040
) > TWL6040_REV_ES1_0
) {
581 twl6040
->audpwron
= pdata
->audpwron_gpio
;
583 twl6040
->audpwron
= of_get_named_gpio(node
,
584 "ti,audpwron-gpio", 0);
586 twl6040
->audpwron
= -EINVAL
;
588 if (gpio_is_valid(twl6040
->audpwron
)) {
589 ret
= devm_gpio_request_one(&client
->dev
, twl6040
->audpwron
,
590 GPIOF_OUT_INIT_LOW
, "audpwron");
595 ret
= regmap_add_irq_chip(twl6040
->regmap
, twl6040
->irq
,
596 IRQF_ONESHOT
, 0, &twl6040_irq_chip
,
601 twl6040
->irq_ready
= regmap_irq_get_virq(twl6040
->irq_data
,
603 twl6040
->irq_th
= regmap_irq_get_virq(twl6040
->irq_data
,
606 ret
= devm_request_threaded_irq(twl6040
->dev
, twl6040
->irq_ready
, NULL
,
607 twl6040_readyint_handler
, IRQF_ONESHOT
,
608 "twl6040_irq_ready", twl6040
);
610 dev_err(twl6040
->dev
, "READY IRQ request failed: %d\n", ret
);
614 ret
= devm_request_threaded_irq(twl6040
->dev
, twl6040
->irq_th
, NULL
,
615 twl6040_thint_handler
, IRQF_ONESHOT
,
616 "twl6040_irq_th", twl6040
);
618 dev_err(twl6040
->dev
, "Thermal IRQ request failed: %d\n", ret
);
622 /* dual-access registers controlled by I2C only */
623 twl6040_set_bits(twl6040
, TWL6040_REG_ACCCTL
, TWL6040_I2CSEL
);
626 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
627 * We can add the ASoC codec child whenever this driver has been loaded.
628 * The ASoC codec can work without pdata, pass the platform_data only if
629 * it has been provided.
631 irq
= regmap_irq_get_virq(twl6040
->irq_data
, TWL6040_IRQ_PLUG
);
632 cell
= &twl6040
->cells
[children
];
633 cell
->name
= "twl6040-codec";
634 twl6040_codec_rsrc
[0].start
= irq
;
635 twl6040_codec_rsrc
[0].end
= irq
;
636 cell
->resources
= twl6040_codec_rsrc
;
637 cell
->num_resources
= ARRAY_SIZE(twl6040_codec_rsrc
);
638 if (pdata
&& pdata
->codec
) {
639 cell
->platform_data
= pdata
->codec
;
640 cell
->pdata_size
= sizeof(*pdata
->codec
);
644 if (twl6040_has_vibra(pdata
, node
)) {
645 irq
= regmap_irq_get_virq(twl6040
->irq_data
, TWL6040_IRQ_VIB
);
647 cell
= &twl6040
->cells
[children
];
648 cell
->name
= "twl6040-vibra";
649 twl6040_vibra_rsrc
[0].start
= irq
;
650 twl6040_vibra_rsrc
[0].end
= irq
;
651 cell
->resources
= twl6040_vibra_rsrc
;
652 cell
->num_resources
= ARRAY_SIZE(twl6040_vibra_rsrc
);
654 if (pdata
&& pdata
->vibra
) {
655 cell
->platform_data
= pdata
->vibra
;
656 cell
->pdata_size
= sizeof(*pdata
->vibra
);
662 * Enable the GPO driver in the following cases:
663 * DT booted kernel or legacy boot with valid gpo platform_data
665 if (!pdata
|| (pdata
&& pdata
->gpo
)) {
666 cell
= &twl6040
->cells
[children
];
667 cell
->name
= "twl6040-gpo";
670 cell
->platform_data
= pdata
->gpo
;
671 cell
->pdata_size
= sizeof(*pdata
->gpo
);
676 ret
= mfd_add_devices(&client
->dev
, -1, twl6040
->cells
, children
,
684 devm_free_irq(&client
->dev
, twl6040
->irq_th
, twl6040
);
686 devm_free_irq(&client
->dev
, twl6040
->irq_ready
, twl6040
);
688 regmap_del_irq_chip(twl6040
->irq
, twl6040
->irq_data
);
690 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
692 i2c_set_clientdata(client
, NULL
);
697 static int twl6040_remove(struct i2c_client
*client
)
699 struct twl6040
*twl6040
= i2c_get_clientdata(client
);
701 if (twl6040
->power_count
)
702 twl6040_power(twl6040
, 0);
704 devm_free_irq(&client
->dev
, twl6040
->irq_ready
, twl6040
);
705 devm_free_irq(&client
->dev
, twl6040
->irq_th
, twl6040
);
706 regmap_del_irq_chip(twl6040
->irq
, twl6040
->irq_data
);
708 mfd_remove_devices(&client
->dev
);
709 i2c_set_clientdata(client
, NULL
);
711 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
716 static const struct i2c_device_id twl6040_i2c_id
[] = {
721 MODULE_DEVICE_TABLE(i2c
, twl6040_i2c_id
);
723 static struct i2c_driver twl6040_driver
= {
726 .owner
= THIS_MODULE
,
728 .probe
= twl6040_probe
,
729 .remove
= twl6040_remove
,
730 .id_table
= twl6040_i2c_id
,
733 module_i2c_driver(twl6040_driver
);
735 MODULE_DESCRIPTION("TWL6040 MFD");
736 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
737 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
738 MODULE_LICENSE("GPL");
739 MODULE_ALIAS("platform:twl6040");