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 ret
= regmap_read(twl6040
->regmap
, reg
, &val
);
67 EXPORT_SYMBOL(twl6040_reg_read
);
69 int twl6040_reg_write(struct twl6040
*twl6040
, unsigned int reg
, u8 val
)
73 ret
= regmap_write(twl6040
->regmap
, reg
, val
);
77 EXPORT_SYMBOL(twl6040_reg_write
);
79 int twl6040_set_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
81 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, mask
);
83 EXPORT_SYMBOL(twl6040_set_bits
);
85 int twl6040_clear_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
87 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, 0);
89 EXPORT_SYMBOL(twl6040_clear_bits
);
91 /* twl6040 codec manual power-up sequence */
92 static int twl6040_power_up_manual(struct twl6040
*twl6040
)
94 u8 ldoctl
, ncpctl
, lppllctl
;
97 /* enable high-side LDO, reference system and internal oscillator */
98 ldoctl
= TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
;
99 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
102 usleep_range(10000, 10500);
104 /* enable negative charge pump */
105 ncpctl
= TWL6040_NCPENA
;
106 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
109 usleep_range(1000, 1500);
111 /* enable low-side LDO */
112 ldoctl
|= TWL6040_LSLDOENA
;
113 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
116 usleep_range(1000, 1500);
118 /* enable low-power PLL */
119 lppllctl
= TWL6040_LPLLENA
;
120 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
123 usleep_range(5000, 5500);
125 /* disable internal oscillator */
126 ldoctl
&= ~TWL6040_OSCENA
;
127 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
134 lppllctl
&= ~TWL6040_LPLLENA
;
135 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
137 ldoctl
&= ~TWL6040_LSLDOENA
;
138 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
140 ncpctl
&= ~TWL6040_NCPENA
;
141 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
143 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
144 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
146 dev_err(twl6040
->dev
, "manual power-up failed\n");
150 /* twl6040 manual power-down sequence */
151 static void twl6040_power_down_manual(struct twl6040
*twl6040
)
153 u8 ncpctl
, ldoctl
, lppllctl
;
155 ncpctl
= twl6040_reg_read(twl6040
, TWL6040_REG_NCPCTL
);
156 ldoctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LDOCTL
);
157 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
159 /* enable internal oscillator */
160 ldoctl
|= TWL6040_OSCENA
;
161 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
162 usleep_range(1000, 1500);
164 /* disable low-power PLL */
165 lppllctl
&= ~TWL6040_LPLLENA
;
166 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
168 /* disable low-side LDO */
169 ldoctl
&= ~TWL6040_LSLDOENA
;
170 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
172 /* disable negative charge pump */
173 ncpctl
&= ~TWL6040_NCPENA
;
174 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
176 /* disable high-side LDO, reference system and internal oscillator */
177 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
178 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
181 static irqreturn_t
twl6040_readyint_handler(int irq
, void *data
)
183 struct twl6040
*twl6040
= data
;
185 complete(&twl6040
->ready
);
190 static irqreturn_t
twl6040_thint_handler(int irq
, void *data
)
192 struct twl6040
*twl6040
= data
;
195 status
= twl6040_reg_read(twl6040
, TWL6040_REG_STATUS
);
196 if (status
& TWL6040_TSHUTDET
) {
197 dev_warn(twl6040
->dev
, "Thermal shutdown, powering-off");
198 twl6040_power(twl6040
, 0);
200 dev_warn(twl6040
->dev
, "Leaving thermal shutdown, powering-on");
201 twl6040_power(twl6040
, 1);
207 static int twl6040_power_up_automatic(struct twl6040
*twl6040
)
211 gpio_set_value(twl6040
->audpwron
, 1);
213 time_left
= wait_for_completion_timeout(&twl6040
->ready
,
214 msecs_to_jiffies(144));
218 dev_warn(twl6040
->dev
, "timeout waiting for READYINT\n");
219 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
220 if (!(intid
& TWL6040_READYINT
)) {
221 dev_err(twl6040
->dev
, "automatic power-up failed\n");
222 gpio_set_value(twl6040
->audpwron
, 0);
230 int twl6040_power(struct twl6040
*twl6040
, int on
)
234 mutex_lock(&twl6040
->mutex
);
237 /* already powered-up */
238 if (twl6040
->power_count
++)
241 if (gpio_is_valid(twl6040
->audpwron
)) {
242 /* use automatic power-up sequence */
243 ret
= twl6040_power_up_automatic(twl6040
);
245 twl6040
->power_count
= 0;
249 /* use manual power-up sequence */
250 ret
= twl6040_power_up_manual(twl6040
);
252 twl6040
->power_count
= 0;
256 /* Default PLL configuration after power up */
257 twl6040
->pll
= TWL6040_SYSCLK_SEL_LPPLL
;
258 twl6040
->sysclk
= 19200000;
259 twl6040
->mclk
= 32768;
261 /* already powered-down */
262 if (!twl6040
->power_count
) {
263 dev_err(twl6040
->dev
,
264 "device is already powered-off\n");
269 if (--twl6040
->power_count
)
272 if (gpio_is_valid(twl6040
->audpwron
)) {
273 /* use AUDPWRON line */
274 gpio_set_value(twl6040
->audpwron
, 0);
276 /* power-down sequence latency */
277 usleep_range(500, 700);
279 /* use manual power-down sequence */
280 twl6040_power_down_manual(twl6040
);
287 mutex_unlock(&twl6040
->mutex
);
290 EXPORT_SYMBOL(twl6040_power
);
292 int twl6040_set_pll(struct twl6040
*twl6040
, int pll_id
,
293 unsigned int freq_in
, unsigned int freq_out
)
295 u8 hppllctl
, lppllctl
;
298 mutex_lock(&twl6040
->mutex
);
300 hppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_HPPLLCTL
);
301 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
303 /* Force full reconfiguration when switching between PLL */
304 if (pll_id
!= twl6040
->pll
) {
310 case TWL6040_SYSCLK_SEL_LPPLL
:
311 /* low-power PLL divider */
312 /* Change the sysclk configuration only if it has been canged */
313 if (twl6040
->sysclk
!= freq_out
) {
316 lppllctl
|= TWL6040_LPLLFIN
;
319 lppllctl
&= ~TWL6040_LPLLFIN
;
322 dev_err(twl6040
->dev
,
323 "freq_out %d not supported\n",
328 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
332 /* The PLL in use has not been change, we can exit */
333 if (twl6040
->pll
== pll_id
)
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
,
351 "freq_in %d not supported\n", freq_in
);
356 case TWL6040_SYSCLK_SEL_HPPLL
:
357 /* high-performance PLL can provide only 19.2 MHz */
358 if (freq_out
!= 19200000) {
359 dev_err(twl6040
->dev
,
360 "freq_out %d not supported\n", freq_out
);
365 if (twl6040
->mclk
!= freq_in
) {
366 hppllctl
&= ~TWL6040_MCLK_MSK
;
370 /* PLL enabled, active mode */
371 hppllctl
|= TWL6040_MCLK_12000KHZ
|
377 * (enable PLL if MCLK jitter quality
378 * doesn't meet specification)
380 hppllctl
|= TWL6040_MCLK_19200KHZ
;
383 /* PLL enabled, active mode */
384 hppllctl
|= TWL6040_MCLK_26000KHZ
|
388 /* PLL enabled, active mode */
389 hppllctl
|= TWL6040_MCLK_38400KHZ
|
393 dev_err(twl6040
->dev
,
394 "freq_in %d not supported\n", freq_in
);
400 * enable clock slicer to ensure input waveform is
403 hppllctl
|= TWL6040_HPLLSQRENA
;
405 twl6040_reg_write(twl6040
, TWL6040_REG_HPPLLCTL
,
407 usleep_range(500, 700);
408 lppllctl
|= TWL6040_HPLLSEL
;
409 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
411 lppllctl
&= ~TWL6040_LPLLENA
;
412 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
417 dev_err(twl6040
->dev
, "unknown pll id %d\n", pll_id
);
422 twl6040
->sysclk
= freq_out
;
423 twl6040
->mclk
= freq_in
;
424 twl6040
->pll
= pll_id
;
427 mutex_unlock(&twl6040
->mutex
);
430 EXPORT_SYMBOL(twl6040_set_pll
);
432 int twl6040_get_pll(struct twl6040
*twl6040
)
434 if (twl6040
->power_count
)
439 EXPORT_SYMBOL(twl6040_get_pll
);
441 unsigned int twl6040_get_sysclk(struct twl6040
*twl6040
)
443 return twl6040
->sysclk
;
445 EXPORT_SYMBOL(twl6040_get_sysclk
);
447 /* Get the combined status of the vibra control register */
448 int twl6040_get_vibralr_status(struct twl6040
*twl6040
)
454 ret
= regmap_read(twl6040
->regmap
, TWL6040_REG_VIBCTLL
, ®
);
459 ret
= regmap_read(twl6040
->regmap
, TWL6040_REG_VIBCTLR
, ®
);
464 status
&= (TWL6040_VIBENA
| TWL6040_VIBSEL
);
468 EXPORT_SYMBOL(twl6040_get_vibralr_status
);
470 static struct resource twl6040_vibra_rsrc
[] = {
472 .flags
= IORESOURCE_IRQ
,
476 static struct resource twl6040_codec_rsrc
[] = {
478 .flags
= IORESOURCE_IRQ
,
482 static bool twl6040_readable_reg(struct device
*dev
, unsigned int reg
)
484 /* Register 0 is not readable */
490 static bool twl6040_volatile_reg(struct device
*dev
, unsigned int reg
)
493 case TWL6040_REG_VIBCTLL
:
494 case TWL6040_REG_VIBCTLR
:
495 case TWL6040_REG_INTMR
:
502 static struct regmap_config twl6040_regmap_config
= {
505 .max_register
= TWL6040_REG_STATUS
, /* 0x2e */
507 .readable_reg
= twl6040_readable_reg
,
508 .volatile_reg
= twl6040_volatile_reg
,
510 .cache_type
= REGCACHE_RBTREE
,
513 static const struct regmap_irq twl6040_irqs
[] = {
514 { .reg_offset
= 0, .mask
= TWL6040_THINT
, },
515 { .reg_offset
= 0, .mask
= TWL6040_PLUGINT
| TWL6040_UNPLUGINT
, },
516 { .reg_offset
= 0, .mask
= TWL6040_HOOKINT
, },
517 { .reg_offset
= 0, .mask
= TWL6040_HFINT
, },
518 { .reg_offset
= 0, .mask
= TWL6040_VIBINT
, },
519 { .reg_offset
= 0, .mask
= TWL6040_READYINT
, },
522 static struct regmap_irq_chip twl6040_irq_chip
= {
524 .irqs
= twl6040_irqs
,
525 .num_irqs
= ARRAY_SIZE(twl6040_irqs
),
528 .status_base
= TWL6040_REG_INTID
,
529 .mask_base
= TWL6040_REG_INTMR
,
532 static int twl6040_probe(struct i2c_client
*client
,
533 const struct i2c_device_id
*id
)
535 struct device_node
*node
= client
->dev
.of_node
;
536 struct twl6040
*twl6040
;
537 struct mfd_cell
*cell
= NULL
;
538 int irq
, ret
, children
= 0;
541 dev_err(&client
->dev
, "of node is missing\n");
545 /* In order to operate correctly we need valid interrupt config */
547 dev_err(&client
->dev
, "Invalid IRQ configuration\n");
551 twl6040
= devm_kzalloc(&client
->dev
, sizeof(struct twl6040
),
556 twl6040
->regmap
= devm_regmap_init_i2c(client
, &twl6040_regmap_config
);
557 if (IS_ERR(twl6040
->regmap
))
558 return PTR_ERR(twl6040
->regmap
);
560 i2c_set_clientdata(client
, twl6040
);
562 twl6040
->supplies
[0].supply
= "vio";
563 twl6040
->supplies
[1].supply
= "v2v1";
564 ret
= devm_regulator_bulk_get(&client
->dev
, TWL6040_NUM_SUPPLIES
,
567 dev_err(&client
->dev
, "Failed to get supplies: %d\n", ret
);
568 goto regulator_get_err
;
571 ret
= regulator_bulk_enable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
573 dev_err(&client
->dev
, "Failed to enable supplies: %d\n", ret
);
574 goto regulator_get_err
;
577 twl6040
->dev
= &client
->dev
;
578 twl6040
->irq
= client
->irq
;
580 mutex_init(&twl6040
->mutex
);
581 init_completion(&twl6040
->ready
);
583 twl6040
->rev
= twl6040_reg_read(twl6040
, TWL6040_REG_ASICREV
);
585 /* ERRATA: Automatic power-up is not possible in ES1.0 */
586 if (twl6040_get_revid(twl6040
) > TWL6040_REV_ES1_0
)
587 twl6040
->audpwron
= of_get_named_gpio(node
,
588 "ti,audpwron-gpio", 0);
590 twl6040
->audpwron
= -EINVAL
;
592 if (gpio_is_valid(twl6040
->audpwron
)) {
593 ret
= devm_gpio_request_one(&client
->dev
, twl6040
->audpwron
,
594 GPIOF_OUT_INIT_LOW
, "audpwron");
599 ret
= regmap_add_irq_chip(twl6040
->regmap
, twl6040
->irq
, IRQF_ONESHOT
,
600 0, &twl6040_irq_chip
,&twl6040
->irq_data
);
604 twl6040
->irq_ready
= regmap_irq_get_virq(twl6040
->irq_data
,
606 twl6040
->irq_th
= regmap_irq_get_virq(twl6040
->irq_data
,
609 ret
= devm_request_threaded_irq(twl6040
->dev
, twl6040
->irq_ready
, NULL
,
610 twl6040_readyint_handler
, IRQF_ONESHOT
,
611 "twl6040_irq_ready", twl6040
);
613 dev_err(twl6040
->dev
, "READY IRQ request failed: %d\n", ret
);
617 ret
= devm_request_threaded_irq(twl6040
->dev
, twl6040
->irq_th
, NULL
,
618 twl6040_thint_handler
, IRQF_ONESHOT
,
619 "twl6040_irq_th", twl6040
);
621 dev_err(twl6040
->dev
, "Thermal IRQ request failed: %d\n", ret
);
625 /* dual-access registers controlled by I2C only */
626 twl6040_set_bits(twl6040
, TWL6040_REG_ACCCTL
, TWL6040_I2CSEL
);
629 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
630 * We can add the ASoC codec child whenever this driver has been loaded.
632 irq
= regmap_irq_get_virq(twl6040
->irq_data
, TWL6040_IRQ_PLUG
);
633 cell
= &twl6040
->cells
[children
];
634 cell
->name
= "twl6040-codec";
635 twl6040_codec_rsrc
[0].start
= irq
;
636 twl6040_codec_rsrc
[0].end
= irq
;
637 cell
->resources
= twl6040_codec_rsrc
;
638 cell
->num_resources
= ARRAY_SIZE(twl6040_codec_rsrc
);
641 /* Vibra input driver support */
642 if (twl6040_has_vibra(node
)) {
643 irq
= regmap_irq_get_virq(twl6040
->irq_data
, TWL6040_IRQ_VIB
);
645 cell
= &twl6040
->cells
[children
];
646 cell
->name
= "twl6040-vibra";
647 twl6040_vibra_rsrc
[0].start
= irq
;
648 twl6040_vibra_rsrc
[0].end
= irq
;
649 cell
->resources
= twl6040_vibra_rsrc
;
650 cell
->num_resources
= ARRAY_SIZE(twl6040_vibra_rsrc
);
655 cell
= &twl6040
->cells
[children
];
656 cell
->name
= "twl6040-gpo";
659 ret
= mfd_add_devices(&client
->dev
, -1, twl6040
->cells
, children
,
667 devm_free_irq(&client
->dev
, twl6040
->irq_th
, twl6040
);
669 devm_free_irq(&client
->dev
, twl6040
->irq_ready
, twl6040
);
671 regmap_del_irq_chip(twl6040
->irq
, twl6040
->irq_data
);
673 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
675 i2c_set_clientdata(client
, NULL
);
680 static int twl6040_remove(struct i2c_client
*client
)
682 struct twl6040
*twl6040
= i2c_get_clientdata(client
);
684 if (twl6040
->power_count
)
685 twl6040_power(twl6040
, 0);
687 devm_free_irq(&client
->dev
, twl6040
->irq_ready
, twl6040
);
688 devm_free_irq(&client
->dev
, twl6040
->irq_th
, twl6040
);
689 regmap_del_irq_chip(twl6040
->irq
, twl6040
->irq_data
);
691 mfd_remove_devices(&client
->dev
);
692 i2c_set_clientdata(client
, NULL
);
694 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
699 static const struct i2c_device_id twl6040_i2c_id
[] = {
704 MODULE_DEVICE_TABLE(i2c
, twl6040_i2c_id
);
706 static struct i2c_driver twl6040_driver
= {
709 .owner
= THIS_MODULE
,
711 .probe
= twl6040_probe
,
712 .remove
= twl6040_remove
,
713 .id_table
= twl6040_i2c_id
,
716 module_i2c_driver(twl6040_driver
);
718 MODULE_DESCRIPTION("TWL6040 MFD");
719 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
720 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
721 MODULE_LICENSE("GPL");
722 MODULE_ALIAS("platform:twl6040");