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 mutex_lock(&twl6040
->io_mutex
);
68 /* Vibra control registers from cache */
69 if (unlikely(reg
== TWL6040_REG_VIBCTLL
||
70 reg
== TWL6040_REG_VIBCTLR
)) {
71 val
= twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)];
73 ret
= regmap_read(twl6040
->regmap
, reg
, &val
);
75 mutex_unlock(&twl6040
->io_mutex
);
79 mutex_unlock(&twl6040
->io_mutex
);
83 EXPORT_SYMBOL(twl6040_reg_read
);
85 int twl6040_reg_write(struct twl6040
*twl6040
, unsigned int reg
, u8 val
)
89 mutex_lock(&twl6040
->io_mutex
);
90 ret
= regmap_write(twl6040
->regmap
, reg
, val
);
91 /* Cache the vibra control registers */
92 if (reg
== TWL6040_REG_VIBCTLL
|| reg
== TWL6040_REG_VIBCTLR
)
93 twl6040
->vibra_ctrl_cache
[VIBRACTRL_MEMBER(reg
)] = val
;
94 mutex_unlock(&twl6040
->io_mutex
);
98 EXPORT_SYMBOL(twl6040_reg_write
);
100 int twl6040_set_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
104 mutex_lock(&twl6040
->io_mutex
);
105 ret
= regmap_update_bits(twl6040
->regmap
, reg
, mask
, mask
);
106 mutex_unlock(&twl6040
->io_mutex
);
109 EXPORT_SYMBOL(twl6040_set_bits
);
111 int twl6040_clear_bits(struct twl6040
*twl6040
, unsigned int reg
, u8 mask
)
115 mutex_lock(&twl6040
->io_mutex
);
116 ret
= regmap_update_bits(twl6040
->regmap
, reg
, mask
, 0);
117 mutex_unlock(&twl6040
->io_mutex
);
120 EXPORT_SYMBOL(twl6040_clear_bits
);
122 /* twl6040 codec manual power-up sequence */
123 static int twl6040_power_up(struct twl6040
*twl6040
)
125 u8 ldoctl
, ncpctl
, lppllctl
;
128 /* enable high-side LDO, reference system and internal oscillator */
129 ldoctl
= TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
;
130 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
133 usleep_range(10000, 10500);
135 /* enable negative charge pump */
136 ncpctl
= TWL6040_NCPENA
;
137 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
140 usleep_range(1000, 1500);
142 /* enable low-side LDO */
143 ldoctl
|= TWL6040_LSLDOENA
;
144 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
147 usleep_range(1000, 1500);
149 /* enable low-power PLL */
150 lppllctl
= TWL6040_LPLLENA
;
151 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
154 usleep_range(5000, 5500);
156 /* disable internal oscillator */
157 ldoctl
&= ~TWL6040_OSCENA
;
158 ret
= twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
165 lppllctl
&= ~TWL6040_LPLLENA
;
166 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
168 ldoctl
&= ~TWL6040_LSLDOENA
;
169 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
171 ncpctl
&= ~TWL6040_NCPENA
;
172 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
174 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
175 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
180 /* twl6040 manual power-down sequence */
181 static void twl6040_power_down(struct twl6040
*twl6040
)
183 u8 ncpctl
, ldoctl
, lppllctl
;
185 ncpctl
= twl6040_reg_read(twl6040
, TWL6040_REG_NCPCTL
);
186 ldoctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LDOCTL
);
187 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
189 /* enable internal oscillator */
190 ldoctl
|= TWL6040_OSCENA
;
191 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
192 usleep_range(1000, 1500);
194 /* disable low-power PLL */
195 lppllctl
&= ~TWL6040_LPLLENA
;
196 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
, lppllctl
);
198 /* disable low-side LDO */
199 ldoctl
&= ~TWL6040_LSLDOENA
;
200 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
202 /* disable negative charge pump */
203 ncpctl
&= ~TWL6040_NCPENA
;
204 twl6040_reg_write(twl6040
, TWL6040_REG_NCPCTL
, ncpctl
);
206 /* disable high-side LDO, reference system and internal oscillator */
207 ldoctl
&= ~(TWL6040_HSLDOENA
| TWL6040_REFENA
| TWL6040_OSCENA
);
208 twl6040_reg_write(twl6040
, TWL6040_REG_LDOCTL
, ldoctl
);
211 static irqreturn_t
twl6040_naudint_handler(int irq
, void *data
)
213 struct twl6040
*twl6040
= data
;
216 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
218 if (intid
& TWL6040_READYINT
)
219 complete(&twl6040
->ready
);
221 if (intid
& TWL6040_THINT
) {
222 status
= twl6040_reg_read(twl6040
, TWL6040_REG_STATUS
);
223 if (status
& TWL6040_TSHUTDET
) {
224 dev_warn(twl6040
->dev
,
225 "Thermal shutdown, powering-off");
226 twl6040_power(twl6040
, 0);
228 dev_warn(twl6040
->dev
,
229 "Leaving thermal shutdown, powering-on");
230 twl6040_power(twl6040
, 1);
237 static int twl6040_power_up_completion(struct twl6040
*twl6040
,
243 time_left
= wait_for_completion_timeout(&twl6040
->ready
,
244 msecs_to_jiffies(144));
246 intid
= twl6040_reg_read(twl6040
, TWL6040_REG_INTID
);
247 if (!(intid
& TWL6040_READYINT
)) {
248 dev_err(twl6040
->dev
,
249 "timeout waiting for READYINT\n");
257 int twl6040_power(struct twl6040
*twl6040
, int on
)
259 int audpwron
= twl6040
->audpwron
;
260 int naudint
= twl6040
->irq
;
263 mutex_lock(&twl6040
->mutex
);
266 /* already powered-up */
267 if (twl6040
->power_count
++)
270 if (gpio_is_valid(audpwron
)) {
271 /* use AUDPWRON line */
272 gpio_set_value(audpwron
, 1);
273 /* wait for power-up completion */
274 ret
= twl6040_power_up_completion(twl6040
, naudint
);
276 dev_err(twl6040
->dev
,
277 "automatic power-down failed\n");
278 twl6040
->power_count
= 0;
282 /* use manual power-up sequence */
283 ret
= twl6040_power_up(twl6040
);
285 dev_err(twl6040
->dev
,
286 "manual power-up failed\n");
287 twl6040
->power_count
= 0;
291 /* Default PLL configuration after power up */
292 twl6040
->pll
= TWL6040_SYSCLK_SEL_LPPLL
;
293 twl6040
->sysclk
= 19200000;
294 twl6040
->mclk
= 32768;
296 /* already powered-down */
297 if (!twl6040
->power_count
) {
298 dev_err(twl6040
->dev
,
299 "device is already powered-off\n");
304 if (--twl6040
->power_count
)
307 if (gpio_is_valid(audpwron
)) {
308 /* use AUDPWRON line */
309 gpio_set_value(audpwron
, 0);
311 /* power-down sequence latency */
312 usleep_range(500, 700);
314 /* use manual power-down sequence */
315 twl6040_power_down(twl6040
);
322 mutex_unlock(&twl6040
->mutex
);
325 EXPORT_SYMBOL(twl6040_power
);
327 int twl6040_set_pll(struct twl6040
*twl6040
, int pll_id
,
328 unsigned int freq_in
, unsigned int freq_out
)
330 u8 hppllctl
, lppllctl
;
333 mutex_lock(&twl6040
->mutex
);
335 hppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_HPPLLCTL
);
336 lppllctl
= twl6040_reg_read(twl6040
, TWL6040_REG_LPPLLCTL
);
338 /* Force full reconfiguration when switching between PLL */
339 if (pll_id
!= twl6040
->pll
) {
345 case TWL6040_SYSCLK_SEL_LPPLL
:
346 /* low-power PLL divider */
347 /* Change the sysclk configuration only if it has been canged */
348 if (twl6040
->sysclk
!= freq_out
) {
351 lppllctl
|= TWL6040_LPLLFIN
;
354 lppllctl
&= ~TWL6040_LPLLFIN
;
357 dev_err(twl6040
->dev
,
358 "freq_out %d not supported\n",
363 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
367 /* The PLL in use has not been change, we can exit */
368 if (twl6040
->pll
== pll_id
)
373 lppllctl
|= TWL6040_LPLLENA
;
374 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
377 lppllctl
&= ~TWL6040_HPLLSEL
;
378 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
380 hppllctl
&= ~TWL6040_HPLLENA
;
381 twl6040_reg_write(twl6040
, TWL6040_REG_HPPLLCTL
,
385 dev_err(twl6040
->dev
,
386 "freq_in %d not supported\n", freq_in
);
391 case TWL6040_SYSCLK_SEL_HPPLL
:
392 /* high-performance PLL can provide only 19.2 MHz */
393 if (freq_out
!= 19200000) {
394 dev_err(twl6040
->dev
,
395 "freq_out %d not supported\n", freq_out
);
400 if (twl6040
->mclk
!= freq_in
) {
401 hppllctl
&= ~TWL6040_MCLK_MSK
;
405 /* PLL enabled, active mode */
406 hppllctl
|= TWL6040_MCLK_12000KHZ
|
412 * (enable PLL if MCLK jitter quality
413 * doesn't meet specification)
415 hppllctl
|= TWL6040_MCLK_19200KHZ
;
418 /* PLL enabled, active mode */
419 hppllctl
|= TWL6040_MCLK_26000KHZ
|
423 /* PLL enabled, active mode */
424 hppllctl
|= TWL6040_MCLK_38400KHZ
|
428 dev_err(twl6040
->dev
,
429 "freq_in %d not supported\n", freq_in
);
435 * enable clock slicer to ensure input waveform is
438 hppllctl
|= TWL6040_HPLLSQRENA
;
440 twl6040_reg_write(twl6040
, TWL6040_REG_HPPLLCTL
,
442 usleep_range(500, 700);
443 lppllctl
|= TWL6040_HPLLSEL
;
444 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
446 lppllctl
&= ~TWL6040_LPLLENA
;
447 twl6040_reg_write(twl6040
, TWL6040_REG_LPPLLCTL
,
452 dev_err(twl6040
->dev
, "unknown pll id %d\n", pll_id
);
457 twl6040
->sysclk
= freq_out
;
458 twl6040
->mclk
= freq_in
;
459 twl6040
->pll
= pll_id
;
462 mutex_unlock(&twl6040
->mutex
);
465 EXPORT_SYMBOL(twl6040_set_pll
);
467 int twl6040_get_pll(struct twl6040
*twl6040
)
469 if (twl6040
->power_count
)
474 EXPORT_SYMBOL(twl6040_get_pll
);
476 unsigned int twl6040_get_sysclk(struct twl6040
*twl6040
)
478 return twl6040
->sysclk
;
480 EXPORT_SYMBOL(twl6040_get_sysclk
);
482 /* Get the combined status of the vibra control register */
483 int twl6040_get_vibralr_status(struct twl6040
*twl6040
)
487 status
= twl6040
->vibra_ctrl_cache
[0] | twl6040
->vibra_ctrl_cache
[1];
488 status
&= (TWL6040_VIBENA
| TWL6040_VIBSEL
);
492 EXPORT_SYMBOL(twl6040_get_vibralr_status
);
494 static struct resource twl6040_vibra_rsrc
[] = {
496 .flags
= IORESOURCE_IRQ
,
500 static struct resource twl6040_codec_rsrc
[] = {
502 .flags
= IORESOURCE_IRQ
,
506 static bool twl6040_readable_reg(struct device
*dev
, unsigned int reg
)
508 /* Register 0 is not readable */
514 static struct regmap_config twl6040_regmap_config
= {
517 .max_register
= TWL6040_REG_STATUS
, /* 0x2e */
519 .readable_reg
= twl6040_readable_reg
,
522 static int __devinit
twl6040_probe(struct i2c_client
*client
,
523 const struct i2c_device_id
*id
)
525 struct twl6040_platform_data
*pdata
= client
->dev
.platform_data
;
526 struct device_node
*node
= client
->dev
.of_node
;
527 struct twl6040
*twl6040
;
528 struct mfd_cell
*cell
= NULL
;
529 int irq
, ret
, children
= 0;
531 if (!pdata
&& !node
) {
532 dev_err(&client
->dev
, "Platform data is missing\n");
536 /* In order to operate correctly we need valid interrupt config */
538 dev_err(&client
->dev
, "Invalid IRQ configuration\n");
542 twl6040
= devm_kzalloc(&client
->dev
, sizeof(struct twl6040
),
549 twl6040
->regmap
= devm_regmap_init_i2c(client
, &twl6040_regmap_config
);
550 if (IS_ERR(twl6040
->regmap
)) {
551 ret
= PTR_ERR(twl6040
->regmap
);
555 i2c_set_clientdata(client
, twl6040
);
557 twl6040
->supplies
[0].supply
= "vio";
558 twl6040
->supplies
[1].supply
= "v2v1";
559 ret
= regulator_bulk_get(&client
->dev
, TWL6040_NUM_SUPPLIES
,
562 dev_err(&client
->dev
, "Failed to get supplies: %d\n", ret
);
563 goto regulator_get_err
;
566 ret
= regulator_bulk_enable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
568 dev_err(&client
->dev
, "Failed to enable supplies: %d\n", ret
);
572 twl6040
->dev
= &client
->dev
;
573 twl6040
->irq
= client
->irq
;
575 mutex_init(&twl6040
->mutex
);
576 mutex_init(&twl6040
->io_mutex
);
577 init_completion(&twl6040
->ready
);
579 twl6040
->rev
= twl6040_reg_read(twl6040
, TWL6040_REG_ASICREV
);
581 /* ERRATA: Automatic power-up is not possible in ES1.0 */
582 if (twl6040_get_revid(twl6040
) > TWL6040_REV_ES1_0
) {
584 twl6040
->audpwron
= pdata
->audpwron_gpio
;
586 twl6040
->audpwron
= of_get_named_gpio(node
,
587 "ti,audpwron-gpio", 0);
589 twl6040
->audpwron
= -EINVAL
;
591 if (gpio_is_valid(twl6040
->audpwron
)) {
592 ret
= gpio_request_one(twl6040
->audpwron
, GPIOF_OUT_INIT_LOW
,
598 /* codec interrupt */
599 ret
= twl6040_irq_init(twl6040
);
603 ret
= request_threaded_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
,
604 NULL
, twl6040_naudint_handler
, 0,
605 "twl6040_irq_ready", twl6040
);
607 dev_err(twl6040
->dev
, "READY IRQ request failed: %d\n",
612 /* dual-access registers controlled by I2C only */
613 twl6040_set_bits(twl6040
, TWL6040_REG_ACCCTL
, TWL6040_I2CSEL
);
616 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
617 * We can add the ASoC codec child whenever this driver has been loaded.
618 * The ASoC codec can work without pdata, pass the platform_data only if
619 * it has been provided.
621 irq
= twl6040
->irq_base
+ TWL6040_IRQ_PLUG
;
622 cell
= &twl6040
->cells
[children
];
623 cell
->name
= "twl6040-codec";
624 twl6040_codec_rsrc
[0].start
= irq
;
625 twl6040_codec_rsrc
[0].end
= irq
;
626 cell
->resources
= twl6040_codec_rsrc
;
627 cell
->num_resources
= ARRAY_SIZE(twl6040_codec_rsrc
);
628 if (pdata
&& pdata
->codec
) {
629 cell
->platform_data
= pdata
->codec
;
630 cell
->pdata_size
= sizeof(*pdata
->codec
);
634 if (twl6040_has_vibra(pdata
, node
)) {
635 irq
= twl6040
->irq_base
+ TWL6040_IRQ_VIB
;
637 cell
= &twl6040
->cells
[children
];
638 cell
->name
= "twl6040-vibra";
639 twl6040_vibra_rsrc
[0].start
= irq
;
640 twl6040_vibra_rsrc
[0].end
= irq
;
641 cell
->resources
= twl6040_vibra_rsrc
;
642 cell
->num_resources
= ARRAY_SIZE(twl6040_vibra_rsrc
);
644 if (pdata
&& pdata
->vibra
) {
645 cell
->platform_data
= pdata
->vibra
;
646 cell
->pdata_size
= sizeof(*pdata
->vibra
);
651 ret
= mfd_add_devices(&client
->dev
, -1, twl6040
->cells
, children
,
659 free_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
, twl6040
);
661 twl6040_irq_exit(twl6040
);
663 if (gpio_is_valid(twl6040
->audpwron
))
664 gpio_free(twl6040
->audpwron
);
666 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
668 regulator_bulk_free(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
670 i2c_set_clientdata(client
, NULL
);
675 static int __devexit
twl6040_remove(struct i2c_client
*client
)
677 struct twl6040
*twl6040
= i2c_get_clientdata(client
);
679 if (twl6040
->power_count
)
680 twl6040_power(twl6040
, 0);
682 if (gpio_is_valid(twl6040
->audpwron
))
683 gpio_free(twl6040
->audpwron
);
685 free_irq(twl6040
->irq_base
+ TWL6040_IRQ_READY
, twl6040
);
686 twl6040_irq_exit(twl6040
);
688 mfd_remove_devices(&client
->dev
);
689 i2c_set_clientdata(client
, NULL
);
691 regulator_bulk_disable(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
692 regulator_bulk_free(TWL6040_NUM_SUPPLIES
, twl6040
->supplies
);
697 static const struct i2c_device_id twl6040_i2c_id
[] = {
701 MODULE_DEVICE_TABLE(i2c
, twl6040_i2c_id
);
703 static struct i2c_driver twl6040_driver
= {
706 .owner
= THIS_MODULE
,
708 .probe
= twl6040_probe
,
709 .remove
= __devexit_p(twl6040_remove
),
710 .id_table
= twl6040_i2c_id
,
713 module_i2c_driver(twl6040_driver
);
715 MODULE_DESCRIPTION("TWL6040 MFD");
716 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
717 MODULE_AUTHOR("Jorge Eduardo Candelaria <jorge.candelaria@ti.com>");
718 MODULE_LICENSE("GPL");
719 MODULE_ALIAS("platform:twl6040");