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/err.h>
41 #include <linux/mfd/core.h>
42 #include <linux/mfd/twl6040.h>
43 #include <linux/regulator/consumer.h>
45 #define VIBRACTRL_MEMBER(reg) ((reg == TWL6040_REG_VIBCTLL) ? 0 : 1)
46 #define TWL6040_NUM_SUPPLIES (2)
48 static bool twl6040_has_vibra(struct twl6040_platform_data
*pdata
,
49 struct device_node
*node
)
51 if (pdata
&& pdata
->vibra
)
55 if (of_find_node_by_name(node
, "vibra"))
62 int twl6040_reg_read(struct twl6040
*twl6040
, unsigned int reg
)
67 /* Vibra control registers from cache */
68 if (unlikely(reg
== TWL6040_REG_VIBCTLL
||
69 reg
== TWL6040_REG_VIBCTLR
)) {
70 val
= twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)];
72 ret
= regmap_read(twl6040
->regmap
, reg
, &val
);
79 EXPORT_SYMBOL(twl6040_reg_read
);
81 int twl6040_reg_write(struct twl6040
*twl6040
, unsigned int reg
, u8 val
)
85 ret
= regmap_write(twl6040
->regmap
, reg
, val
);
86 /* Cache the vibra control registers */
87 if (reg
== TWL6040_REG_VIBCTLL
|| reg
== TWL6040_REG_VIBCTLR
)
88 twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)] = val
;
92 EXPORT_SYMBOL(twl6040_reg_write
);
94 int twl6040_set_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
96 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, mask
);
98 EXPORT_SYMBOL(twl6040_set_bits
);
100 int twl6040_clear_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
102 return regmap_update_bits(twl6040
->regmap
, reg
, mask
, 0);
104 EXPORT_SYMBOL(twl6040_clear_bits
);
106 /* twl6040 codec manual power-up sequence */
107 static int twl6040_power_up(struct twl6040
*twl6040
)
109 u8 ldoctl
, ncpctl
, lppllctl
;
112 /* enable high-side LDO, reference system and internal oscillator */
113 ldoctl
= TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
;
114 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
117 usleep_range(10000, 10500);
119 /* enable negative charge pump */
120 ncpctl
= TWL6040_NCPENA
;
121 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
124 usleep_range(1000, 1500);
126 /* enable low-side LDO */
127 ldoctl
|= TWL6040_LSLDOENA
;
128 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
131 usleep_range(1000, 1500);
133 /* enable low-power PLL */
134 lppllctl
= TWL6040_LPLLENA
;
135 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
138 usleep_range(5000, 5500);
140 /* disable internal oscillator */
141 ldoctl
&= ~TWL6040_OSCENA
;
142 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
149 lppllctl
&= ~TWL6040_LPLLENA
;
150 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
152 ldoctl
&= ~TWL6040_LSLDOENA
;
153 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
155 ncpctl
&= ~TWL6040_NCPENA
;
156 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
158 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
159 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
164 /* twl6040 manual power-down sequence */
165 static void twl6040_power_down(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_naudint_handler(int irq
, void *data
)
197 struct twl6040
*twl6040
= data
;
200 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
202 if (intid
& TWL6040_READYINT
)
203 complete(&twl6040
->ready
);
205 if (intid
& TWL6040_THINT
) {
206 status
= twl6040_reg_read(twl6040
, TWL6040_REG_STATUS
);
207 if (status
& TWL6040_TSHUTDET
) {
208 dev_warn(twl6040
->dev
,
209 "Thermal shutdown, powering-off");
210 twl6040_power(twl6040
, 0);
212 dev_warn(twl6040
->dev
,
213 "Leaving thermal shutdown, powering-on");
214 twl6040_power(twl6040
, 1);
221 static int twl6040_power_up_completion(struct twl6040
*twl6040
,
227 time_left
= wait_for_completion_timeout(&twl6040
->ready
,
228 msecs_to_jiffies(144));
230 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
231 if (!(intid
& TWL6040_READYINT
)) {
232 dev_err(twl6040
->dev
,
233 "timeout waiting for READYINT\n");
241 int twl6040_power(struct twl6040
*twl6040
, int on
)
243 int audpwron
= twl6040
->audpwron
;
244 int naudint
= twl6040
->irq
;
247 mutex_lock(&twl6040
->mutex
);
250 /* already powered-up */
251 if (twl6040
->power_count
++)
254 if (gpio_is_valid(audpwron
)) {
255 /* use AUDPWRON line */
256 gpio_set_value(audpwron
, 1);
257 /* wait for power-up completion */
258 ret
= twl6040_power_up_completion(twl6040
, naudint
);
260 dev_err(twl6040
->dev
,
261 "automatic power-down failed\n");
262 twl6040
->power_count
= 0;
266 /* use manual power-up sequence */
267 ret
= twl6040_power_up(twl6040
);
269 dev_err(twl6040
->dev
,
270 "manual power-up failed\n");
271 twl6040
->power_count
= 0;
275 /* Default PLL configuration after power up */
276 twl6040
->pll
= TWL6040_SYSCLK_SEL_LPPLL
;
277 twl6040
->sysclk
= 19200000;
278 twl6040
->mclk
= 32768;
280 /* already powered-down */
281 if (!twl6040
->power_count
) {
282 dev_err(twl6040
->dev
,
283 "device is already powered-off\n");
288 if (--twl6040
->power_count
)
291 if (gpio_is_valid(audpwron
)) {
292 /* use AUDPWRON line */
293 gpio_set_value(audpwron
, 0);
295 /* power-down sequence latency */
296 usleep_range(500, 700);
298 /* use manual power-down sequence */
299 twl6040_power_down(twl6040
);
306 mutex_unlock(&twl6040
->mutex
);
309 EXPORT_SYMBOL(twl6040_power
);
311 int twl6040_set_pll(struct twl6040
*twl6040
, int pll_id
,
312 unsigned int freq_in
, unsigned int freq_out
)
314 u8 hppllctl
, lppllctl
;
317 mutex_lock(&twl6040
->mutex
);
319 hppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_HPPLLCTL
);
320 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
322 /* Force full reconfiguration when switching between PLL */
323 if (pll_id
!= twl6040
->pll
) {
329 case TWL6040_SYSCLK_SEL_LPPLL
:
330 /* low-power PLL divider */
331 /* Change the sysclk configuration only if it has been canged */
332 if (twl6040
->sysclk
!= freq_out
) {
335 lppllctl
|= TWL6040_LPLLFIN
;
338 lppllctl
&= ~TWL6040_LPLLFIN
;
341 dev_err(twl6040
->dev
,
342 "freq_out %d not supported\n",
347 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
351 /* The PLL in use has not been change, we can exit */
352 if (twl6040
->pll
== pll_id
)
357 lppllctl
|= TWL6040_LPLLENA
;
358 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
361 lppllctl
&= ~TWL6040_HPLLSEL
;
362 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
364 hppllctl
&= ~TWL6040_HPLLENA
;
365 twl6040_reg_write(twl6040
, TWL6040_REG_HPPLLCTL
,
369 dev_err(twl6040
->dev
,
370 "freq_in %d not supported\n", freq_in
);
375 case TWL6040_SYSCLK_SEL_HPPLL
:
376 /* high-performance PLL can provide only 19.2 MHz */
377 if (freq_out
!= 19200000) {
378 dev_err(twl6040
->dev
,
379 "freq_out %d not supported\n", freq_out
);
384 if (twl6040
->mclk
!= freq_in
) {
385 hppllctl
&= ~TWL6040_MCLK_MSK
;
389 /* PLL enabled, active mode */
390 hppllctl
|= TWL6040_MCLK_12000KHZ
|
396 * (enable PLL if MCLK jitter quality
397 * doesn't meet specification)
399 hppllctl
|= TWL6040_MCLK_19200KHZ
;
402 /* PLL enabled, active mode */
403 hppllctl
|= TWL6040_MCLK_26000KHZ
|
407 /* PLL enabled, active mode */
408 hppllctl
|= TWL6040_MCLK_38400KHZ
|
412 dev_err(twl6040
->dev
,
413 "freq_in %d not supported\n", freq_in
);
419 * enable clock slicer to ensure input waveform is
422 hppllctl
|= TWL6040_HPLLSQRENA
;
424 twl6040_reg_write(twl6040
, TWL6040_REG_HPPLLCTL
,
426 usleep_range(500, 700);
427 lppllctl
|= TWL6040_HPLLSEL
;
428 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
430 lppllctl
&= ~TWL6040_LPLLENA
;
431 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
436 dev_err(twl6040
->dev
, "unknown pll id %d\n", pll_id
);
441 twl6040
->sysclk
= freq_out
;
442 twl6040
->mclk
= freq_in
;
443 twl6040
->pll
= pll_id
;
446 mutex_unlock(&twl6040
->mutex
);
449 EXPORT_SYMBOL(twl6040_set_pll
);
451 int twl6040_get_pll(struct twl6040
*twl6040
)
453 if (twl6040
->power_count
)
458 EXPORT_SYMBOL(twl6040_get_pll
);
460 unsigned int twl6040_get_sysclk(struct twl6040
*twl6040
)
462 return twl6040
->sysclk
;
464 EXPORT_SYMBOL(twl6040_get_sysclk
);
466 /* Get the combined status of the vibra control register */
467 int twl6040_get_vibralr_status(struct twl6040
*twl6040
)
471 status
= twl6040
->vibra_ctrl_cache
[0] | twl6040
->vibra_ctrl_cache
[1];
472 status
&= (TWL6040_VIBENA
| TWL6040_VIBSEL
);
476 EXPORT_SYMBOL(twl6040_get_vibralr_status
);
478 static struct resource twl6040_vibra_rsrc
[] = {
480 .flags
= IORESOURCE_IRQ
,
484 static struct resource twl6040_codec_rsrc
[] = {
486 .flags
= IORESOURCE_IRQ
,
490 static bool twl6040_readable_reg(struct device
*dev
, unsigned int reg
)
492 /* Register 0 is not readable */
498 static struct regmap_config twl6040_regmap_config
= {
501 .max_register
= TWL6040_REG_STATUS
, /* 0x2e */
503 .readable_reg
= twl6040_readable_reg
,
506 static int __devinit
twl6040_probe(struct i2c_client
*client
,
507 const struct i2c_device_id
*id
)
509 struct twl6040_platform_data
*pdata
= client
->dev
.platform_data
;
510 struct device_node
*node
= client
->dev
.of_node
;
511 struct twl6040
*twl6040
;
512 struct mfd_cell
*cell
= NULL
;
513 int irq
, ret
, children
= 0;
515 if (!pdata
&& !node
) {
516 dev_err(&client
->dev
, "Platform data is missing\n");
520 /* In order to operate correctly we need valid interrupt config */
522 dev_err(&client
->dev
, "Invalid IRQ configuration\n");
526 twl6040
= devm_kzalloc(&client
->dev
, sizeof(struct twl6040
),
533 twl6040
->regmap
= devm_regmap_init_i2c(client
, &twl6040_regmap_config
);
534 if (IS_ERR(twl6040
->regmap
)) {
535 ret
= PTR_ERR(twl6040
->regmap
);
539 i2c_set_clientdata(client
, twl6040
);
541 twl6040
->supplies
[0].supply
= "vio";
542 twl6040
->supplies
[1].supply
= "v2v1";
543 ret
= regulator_bulk_get(&client
->dev
, TWL6040_NUM_SUPPLIES
,
546 dev_err(&client
->dev
, "Failed to get supplies: %d\n", ret
);
547 goto regulator_get_err
;
550 ret
= regulator_bulk_enable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
552 dev_err(&client
->dev
, "Failed to enable supplies: %d\n", ret
);
556 twl6040
->dev
= &client
->dev
;
557 twl6040
->irq
= client
->irq
;
559 mutex_init(&twl6040
->mutex
);
560 init_completion(&twl6040
->ready
);
562 twl6040
->rev
= twl6040_reg_read(twl6040
, TWL6040_REG_ASICREV
);
564 /* ERRATA: Automatic power-up is not possible in ES1.0 */
565 if (twl6040_get_revid(twl6040
) > TWL6040_REV_ES1_0
) {
567 twl6040
->audpwron
= pdata
->audpwron_gpio
;
569 twl6040
->audpwron
= of_get_named_gpio(node
,
570 "ti,audpwron-gpio", 0);
572 twl6040
->audpwron
= -EINVAL
;
574 if (gpio_is_valid(twl6040
->audpwron
)) {
575 ret
= gpio_request_one(twl6040
->audpwron
, GPIOF_OUT_INIT_LOW
,
581 /* codec interrupt */
582 ret
= twl6040_irq_init(twl6040
);
586 ret
= request_threaded_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
,
587 NULL
, twl6040_naudint_handler
, 0,
588 "twl6040_irq_ready", twl6040
);
590 dev_err(twl6040
->dev
, "READY IRQ request failed: %d\n",
595 /* dual-access registers controlled by I2C only */
596 twl6040_set_bits(twl6040
, TWL6040_REG_ACCCTL
, TWL6040_I2CSEL
);
599 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
600 * We can add the ASoC codec child whenever this driver has been loaded.
601 * The ASoC codec can work without pdata, pass the platform_data only if
602 * it has been provided.
604 irq
= twl6040
->irq_base
+ TWL6040_IRQ_PLUG
;
605 cell
= &twl6040
->cells
[children
];
606 cell
->name
= "twl6040-codec";
607 twl6040_codec_rsrc
[0].start
= irq
;
608 twl6040_codec_rsrc
[0].end
= irq
;
609 cell
->resources
= twl6040_codec_rsrc
;
610 cell
->num_resources
= ARRAY_SIZE(twl6040_codec_rsrc
);
611 if (pdata
&& pdata
->codec
) {
612 cell
->platform_data
= pdata
->codec
;
613 cell
->pdata_size
= sizeof(*pdata
->codec
);
617 if (twl6040_has_vibra(pdata
, node
)) {
618 irq
= twl6040
->irq_base
+ TWL6040_IRQ_VIB
;
620 cell
= &twl6040
->cells
[children
];
621 cell
->name
= "twl6040-vibra";
622 twl6040_vibra_rsrc
[0].start
= irq
;
623 twl6040_vibra_rsrc
[0].end
= irq
;
624 cell
->resources
= twl6040_vibra_rsrc
;
625 cell
->num_resources
= ARRAY_SIZE(twl6040_vibra_rsrc
);
627 if (pdata
&& pdata
->vibra
) {
628 cell
->platform_data
= pdata
->vibra
;
629 cell
->pdata_size
= sizeof(*pdata
->vibra
);
634 ret
= mfd_add_devices(&client
->dev
, -1, twl6040
->cells
, children
,
642 free_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
, twl6040
);
644 twl6040_irq_exit(twl6040
);
646 if (gpio_is_valid(twl6040
->audpwron
))
647 gpio_free(twl6040
->audpwron
);
649 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
651 regulator_bulk_free(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
653 i2c_set_clientdata(client
, NULL
);
658 static int __devexit
twl6040_remove(struct i2c_client
*client
)
660 struct twl6040
*twl6040
= i2c_get_clientdata(client
);
662 if (twl6040
->power_count
)
663 twl6040_power(twl6040
, 0);
665 if (gpio_is_valid(twl6040
->audpwron
))
666 gpio_free(twl6040
->audpwron
);
668 free_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
, twl6040
);
669 twl6040_irq_exit(twl6040
);
671 mfd_remove_devices(&client
->dev
);
672 i2c_set_clientdata(client
, NULL
);
674 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
675 regulator_bulk_free(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
680 static const struct i2c_device_id twl6040_i2c_id
[] = {
685 MODULE_DEVICE_TABLE(i2c
, twl6040_i2c_id
);
687 static struct i2c_driver twl6040_driver
= {
690 .owner
= THIS_MODULE
,
692 .probe
= twl6040_probe
,
693 .remove
= __devexit_p(twl6040_remove
),
694 .id_table
= twl6040_i2c_id
,
697 module_i2c_driver(twl6040_driver
);
699 MODULE_DESCRIPTION("TWL6040 MFD");
700 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
701 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
702 MODULE_LICENSE("GPL");
703 MODULE_ALIAS("platform:twl6040");