2 * MFD driver for TWL6040 audio device
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 struct reg_default twl6040_defaults[] = {
48 { 0x01, 0x4B }, /* REG_ASICID (ro) */
49 { 0x02, 0x00 }, /* REG_ASICREV (ro) */
50 { 0x03, 0x00 }, /* REG_INTID */
51 { 0x04, 0x00 }, /* REG_INTMR */
52 { 0x05, 0x00 }, /* REG_NCPCTRL */
53 { 0x06, 0x00 }, /* REG_LDOCTL */
54 { 0x07, 0x60 }, /* REG_HPPLLCTL */
55 { 0x08, 0x00 }, /* REG_LPPLLCTL */
56 { 0x09, 0x4A }, /* REG_LPPLLDIV */
57 { 0x0A, 0x00 }, /* REG_AMICBCTL */
58 { 0x0B, 0x00 }, /* REG_DMICBCTL */
59 { 0x0C, 0x00 }, /* REG_MICLCTL */
60 { 0x0D, 0x00 }, /* REG_MICRCTL */
61 { 0x0E, 0x00 }, /* REG_MICGAIN */
62 { 0x0F, 0x1B }, /* REG_LINEGAIN */
63 { 0x10, 0x00 }, /* REG_HSLCTL */
64 { 0x11, 0x00 }, /* REG_HSRCTL */
65 { 0x12, 0x00 }, /* REG_HSGAIN */
66 { 0x13, 0x00 }, /* REG_EARCTL */
67 { 0x14, 0x00 }, /* REG_HFLCTL */
68 { 0x15, 0x00 }, /* REG_HFLGAIN */
69 { 0x16, 0x00 }, /* REG_HFRCTL */
70 { 0x17, 0x00 }, /* REG_HFRGAIN */
71 { 0x18, 0x00 }, /* REG_VIBCTLL */
72 { 0x19, 0x00 }, /* REG_VIBDATL */
73 { 0x1A, 0x00 }, /* REG_VIBCTLR */
74 { 0x1B, 0x00 }, /* REG_VIBDATR */
75 { 0x1C, 0x00 }, /* REG_HKCTL1 */
76 { 0x1D, 0x00 }, /* REG_HKCTL2 */
77 { 0x1E, 0x00 }, /* REG_GPOCTL */
78 { 0x1F, 0x00 }, /* REG_ALB */
79 { 0x20, 0x00 }, /* REG_DLB */
83 /* 0x2B, REG_HSOTRIM */
84 /* 0x2C, REG_HFOTRIM */
85 { 0x2D, 0x08 }, /* REG_ACCCTL */
86 { 0x2E, 0x00 }, /* REG_STATUS (ro) */
89 static struct reg_default twl6040_patch[] = {
90 /* Select I2C bus access to dual access registers */
91 { TWL6040_REG_ACCCTL, 0x09 },
95 static bool twl6040_has_vibra(struct device_node *node)
98 if (of_find_node_by_name(node, "vibra"))
104 int twl6040_reg_read(struct twl6040 *twl6040, unsigned int reg)
109 ret = regmap_read(twl6040->regmap, reg, &val);
115 EXPORT_SYMBOL(twl6040_reg_read);
117 int twl6040_reg_write(struct twl6040 *twl6040, unsigned int reg, u8 val)
121 ret = regmap_write(twl6040->regmap, reg, val);
125 EXPORT_SYMBOL(twl6040_reg_write);
127 int twl6040_set_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
129 return regmap_update_bits(twl6040->regmap, reg, mask, mask);
131 EXPORT_SYMBOL(twl6040_set_bits);
133 int twl6040_clear_bits(struct twl6040 *twl6040, unsigned int reg, u8 mask)
135 return regmap_update_bits(twl6040->regmap, reg, mask, 0);
137 EXPORT_SYMBOL(twl6040_clear_bits);
139 /* twl6040 codec manual power-up sequence */
140 static int twl6040_power_up_manual(struct twl6040 *twl6040)
142 u8 ldoctl, ncpctl, lppllctl;
145 /* enable high-side LDO, reference system and internal oscillator */
146 ldoctl = TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA;
147 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
150 usleep_range(10000, 10500);
152 /* enable negative charge pump */
153 ncpctl = TWL6040_NCPENA;
154 ret = twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
157 usleep_range(1000, 1500);
159 /* enable low-side LDO */
160 ldoctl |= TWL6040_LSLDOENA;
161 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
164 usleep_range(1000, 1500);
166 /* enable low-power PLL */
167 lppllctl = TWL6040_LPLLENA;
168 ret = twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
171 usleep_range(5000, 5500);
173 /* disable internal oscillator */
174 ldoctl &= ~TWL6040_OSCENA;
175 ret = twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
182 lppllctl &= ~TWL6040_LPLLENA;
183 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
185 ldoctl &= ~TWL6040_LSLDOENA;
186 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
188 ncpctl &= ~TWL6040_NCPENA;
189 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
191 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
192 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
194 dev_err(twl6040->dev, "manual power-up failed\n");
198 /* twl6040 manual power-down sequence */
199 static void twl6040_power_down_manual(struct twl6040 *twl6040)
201 u8 ncpctl, ldoctl, lppllctl;
203 ncpctl = twl6040_reg_read(twl6040, TWL6040_REG_NCPCTL);
204 ldoctl = twl6040_reg_read(twl6040, TWL6040_REG_LDOCTL);
205 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
207 /* enable internal oscillator */
208 ldoctl |= TWL6040_OSCENA;
209 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
210 usleep_range(1000, 1500);
212 /* disable low-power PLL */
213 lppllctl &= ~TWL6040_LPLLENA;
214 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL, lppllctl);
216 /* disable low-side LDO */
217 ldoctl &= ~TWL6040_LSLDOENA;
218 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
220 /* disable negative charge pump */
221 ncpctl &= ~TWL6040_NCPENA;
222 twl6040_reg_write(twl6040, TWL6040_REG_NCPCTL, ncpctl);
224 /* disable high-side LDO, reference system and internal oscillator */
225 ldoctl &= ~(TWL6040_HSLDOENA | TWL6040_REFENA | TWL6040_OSCENA);
226 twl6040_reg_write(twl6040, TWL6040_REG_LDOCTL, ldoctl);
229 static irqreturn_t twl6040_readyint_handler(int irq, void *data)
231 struct twl6040 *twl6040 = data;
233 complete(&twl6040->ready);
238 static irqreturn_t twl6040_thint_handler(int irq, void *data)
240 struct twl6040 *twl6040 = data;
243 status = twl6040_reg_read(twl6040, TWL6040_REG_STATUS);
244 if (status & TWL6040_TSHUTDET) {
245 dev_warn(twl6040->dev, "Thermal shutdown, powering-off");
246 twl6040_power(twl6040, 0);
248 dev_warn(twl6040->dev, "Leaving thermal shutdown, powering-on");
249 twl6040_power(twl6040, 1);
255 static int twl6040_power_up_automatic(struct twl6040 *twl6040)
259 gpio_set_value(twl6040->audpwron, 1);
261 time_left = wait_for_completion_timeout(&twl6040->ready,
262 msecs_to_jiffies(144));
266 dev_warn(twl6040->dev, "timeout waiting for READYINT\n");
267 intid = twl6040_reg_read(twl6040, TWL6040_REG_INTID);
268 if (!(intid & TWL6040_READYINT)) {
269 dev_err(twl6040->dev, "automatic power-up failed\n");
270 gpio_set_value(twl6040->audpwron, 0);
278 int twl6040_power(struct twl6040 *twl6040, int on)
282 mutex_lock(&twl6040->mutex);
285 /* already powered-up */
286 if (twl6040->power_count++)
289 /* Allow writes to the chip */
290 regcache_cache_only(twl6040->regmap, false);
292 if (gpio_is_valid(twl6040->audpwron)) {
293 /* use automatic power-up sequence */
294 ret = twl6040_power_up_automatic(twl6040);
296 twl6040->power_count = 0;
300 /* use manual power-up sequence */
301 ret = twl6040_power_up_manual(twl6040);
303 twl6040->power_count = 0;
308 /* Sync with the HW */
309 regcache_sync(twl6040->regmap);
311 /* Default PLL configuration after power up */
312 twl6040->pll = TWL6040_SYSCLK_SEL_LPPLL;
313 twl6040->sysclk = 19200000;
314 twl6040->mclk = 32768;
316 /* already powered-down */
317 if (!twl6040->power_count) {
318 dev_err(twl6040->dev,
319 "device is already powered-off\n");
324 if (--twl6040->power_count)
327 if (gpio_is_valid(twl6040->audpwron)) {
328 /* use AUDPWRON line */
329 gpio_set_value(twl6040->audpwron, 0);
331 /* power-down sequence latency */
332 usleep_range(500, 700);
334 /* use manual power-down sequence */
335 twl6040_power_down_manual(twl6040);
338 /* Set regmap to cache only and mark it as dirty */
339 regcache_cache_only(twl6040->regmap, true);
340 regcache_mark_dirty(twl6040->regmap);
347 mutex_unlock(&twl6040->mutex);
350 EXPORT_SYMBOL(twl6040_power);
352 int twl6040_set_pll(struct twl6040 *twl6040, int pll_id,
353 unsigned int freq_in, unsigned int freq_out)
355 u8 hppllctl, lppllctl;
358 mutex_lock(&twl6040->mutex);
360 hppllctl = twl6040_reg_read(twl6040, TWL6040_REG_HPPLLCTL);
361 lppllctl = twl6040_reg_read(twl6040, TWL6040_REG_LPPLLCTL);
363 /* Force full reconfiguration when switching between PLL */
364 if (pll_id != twl6040->pll) {
370 case TWL6040_SYSCLK_SEL_LPPLL:
371 /* low-power PLL divider */
372 /* Change the sysclk configuration only if it has been canged */
373 if (twl6040->sysclk != freq_out) {
376 lppllctl |= TWL6040_LPLLFIN;
379 lppllctl &= ~TWL6040_LPLLFIN;
382 dev_err(twl6040->dev,
383 "freq_out %d not supported\n",
388 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
392 /* The PLL in use has not been change, we can exit */
393 if (twl6040->pll == pll_id)
398 lppllctl |= TWL6040_LPLLENA;
399 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
402 lppllctl &= ~TWL6040_HPLLSEL;
403 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
405 hppllctl &= ~TWL6040_HPLLENA;
406 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
410 dev_err(twl6040->dev,
411 "freq_in %d not supported\n", freq_in);
416 case TWL6040_SYSCLK_SEL_HPPLL:
417 /* high-performance PLL can provide only 19.2 MHz */
418 if (freq_out != 19200000) {
419 dev_err(twl6040->dev,
420 "freq_out %d not supported\n", freq_out);
425 if (twl6040->mclk != freq_in) {
426 hppllctl &= ~TWL6040_MCLK_MSK;
430 /* PLL enabled, active mode */
431 hppllctl |= TWL6040_MCLK_12000KHZ |
437 * (enable PLL if MCLK jitter quality
438 * doesn't meet specification)
440 hppllctl |= TWL6040_MCLK_19200KHZ;
443 /* PLL enabled, active mode */
444 hppllctl |= TWL6040_MCLK_26000KHZ |
448 /* PLL enabled, active mode */
449 hppllctl |= TWL6040_MCLK_38400KHZ |
453 dev_err(twl6040->dev,
454 "freq_in %d not supported\n", freq_in);
460 * enable clock slicer to ensure input waveform is
463 hppllctl |= TWL6040_HPLLSQRENA;
465 twl6040_reg_write(twl6040, TWL6040_REG_HPPLLCTL,
467 usleep_range(500, 700);
468 lppllctl |= TWL6040_HPLLSEL;
469 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
471 lppllctl &= ~TWL6040_LPLLENA;
472 twl6040_reg_write(twl6040, TWL6040_REG_LPPLLCTL,
477 dev_err(twl6040->dev, "unknown pll id %d\n", pll_id);
482 twl6040->sysclk = freq_out;
483 twl6040->mclk = freq_in;
484 twl6040->pll = pll_id;
487 mutex_unlock(&twl6040->mutex);
490 EXPORT_SYMBOL(twl6040_set_pll);
492 int twl6040_get_pll(struct twl6040 *twl6040)
494 if (twl6040->power_count)
499 EXPORT_SYMBOL(twl6040_get_pll);
501 unsigned int twl6040_get_sysclk(struct twl6040 *twl6040)
503 return twl6040->sysclk;
505 EXPORT_SYMBOL(twl6040_get_sysclk);
507 /* Get the combined status of the vibra control register */
508 int twl6040_get_vibralr_status(struct twl6040 *twl6040)
514 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLL, ®);
519 ret = regmap_read(twl6040->regmap, TWL6040_REG_VIBCTLR, ®);
524 status &= (TWL6040_VIBENA | TWL6040_VIBSEL);
528 EXPORT_SYMBOL(twl6040_get_vibralr_status);
530 static struct resource twl6040_vibra_rsrc[] = {
532 .flags = IORESOURCE_IRQ,
536 static struct resource twl6040_codec_rsrc[] = {
538 .flags = IORESOURCE_IRQ,
542 static bool twl6040_readable_reg(struct device *dev, unsigned int reg)
544 /* Register 0 is not readable */
550 static bool twl6040_volatile_reg(struct device *dev, unsigned int reg)
553 case TWL6040_REG_ASICID:
554 case TWL6040_REG_ASICREV:
555 case TWL6040_REG_INTID:
556 case TWL6040_REG_LPPLLCTL:
557 case TWL6040_REG_HPPLLCTL:
558 case TWL6040_REG_STATUS:
565 static bool twl6040_writeable_reg(struct device *dev, unsigned int reg)
568 case TWL6040_REG_ASICID:
569 case TWL6040_REG_ASICREV:
570 case TWL6040_REG_STATUS:
577 static struct regmap_config twl6040_regmap_config = {
581 .reg_defaults = twl6040_defaults,
582 .num_reg_defaults = ARRAY_SIZE(twl6040_defaults),
584 .max_register = TWL6040_REG_STATUS, /* 0x2e */
586 .readable_reg = twl6040_readable_reg,
587 .volatile_reg = twl6040_volatile_reg,
588 .writeable_reg = twl6040_writeable_reg,
590 .cache_type = REGCACHE_RBTREE,
593 static const struct regmap_irq twl6040_irqs[] = {
594 { .reg_offset = 0, .mask = TWL6040_THINT, },
595 { .reg_offset = 0, .mask = TWL6040_PLUGINT | TWL6040_UNPLUGINT, },
596 { .reg_offset = 0, .mask = TWL6040_HOOKINT, },
597 { .reg_offset = 0, .mask = TWL6040_HFINT, },
598 { .reg_offset = 0, .mask = TWL6040_VIBINT, },
599 { .reg_offset = 0, .mask = TWL6040_READYINT, },
602 static struct regmap_irq_chip twl6040_irq_chip = {
604 .irqs = twl6040_irqs,
605 .num_irqs = ARRAY_SIZE(twl6040_irqs),
608 .status_base = TWL6040_REG_INTID,
609 .mask_base = TWL6040_REG_INTMR,
612 static int twl6040_probe(struct i2c_client *client,
613 const struct i2c_device_id *id)
615 struct device_node *node = client->dev.of_node;
616 struct twl6040 *twl6040;
617 struct mfd_cell *cell = NULL;
618 int irq, ret, children = 0;
621 dev_err(&client->dev, "of node is missing\n");
625 /* In order to operate correctly we need valid interrupt config */
627 dev_err(&client->dev, "Invalid IRQ configuration\n");
631 twl6040 = devm_kzalloc(&client->dev, sizeof(struct twl6040),
636 twl6040->regmap = devm_regmap_init_i2c(client, &twl6040_regmap_config);
637 if (IS_ERR(twl6040->regmap))
638 return PTR_ERR(twl6040->regmap);
640 i2c_set_clientdata(client, twl6040);
642 twl6040->supplies[0].supply = "vio";
643 twl6040->supplies[1].supply = "v2v1";
644 ret = devm_regulator_bulk_get(&client->dev, TWL6040_NUM_SUPPLIES,
647 dev_err(&client->dev, "Failed to get supplies: %d\n", ret);
651 ret = regulator_bulk_enable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
653 dev_err(&client->dev, "Failed to enable supplies: %d\n", ret);
657 twl6040->dev = &client->dev;
658 twl6040->irq = client->irq;
660 mutex_init(&twl6040->mutex);
661 init_completion(&twl6040->ready);
663 twl6040->rev = twl6040_reg_read(twl6040, TWL6040_REG_ASICREV);
664 if (twl6040->rev < 0) {
665 dev_err(&client->dev, "Failed to read revision register: %d\n",
670 /* ERRATA: Automatic power-up is not possible in ES1.0 */
671 if (twl6040_get_revid(twl6040) > TWL6040_REV_ES1_0)
672 twl6040->audpwron = of_get_named_gpio(node,
673 "ti,audpwron-gpio", 0);
675 twl6040->audpwron = -EINVAL;
677 if (gpio_is_valid(twl6040->audpwron)) {
678 ret = devm_gpio_request_one(&client->dev, twl6040->audpwron,
679 GPIOF_OUT_INIT_LOW, "audpwron");
684 ret = regmap_add_irq_chip(twl6040->regmap, twl6040->irq, IRQF_ONESHOT,
685 0, &twl6040_irq_chip,&twl6040->irq_data);
689 twl6040->irq_ready = regmap_irq_get_virq(twl6040->irq_data,
691 twl6040->irq_th = regmap_irq_get_virq(twl6040->irq_data,
694 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_ready, NULL,
695 twl6040_readyint_handler, IRQF_ONESHOT,
696 "twl6040_irq_ready", twl6040);
698 dev_err(twl6040->dev, "READY IRQ request failed: %d\n", ret);
702 ret = devm_request_threaded_irq(twl6040->dev, twl6040->irq_th, NULL,
703 twl6040_thint_handler, IRQF_ONESHOT,
704 "twl6040_irq_th", twl6040);
706 dev_err(twl6040->dev, "Thermal IRQ request failed: %d\n", ret);
710 /* dual-access registers controlled by I2C only */
711 regmap_register_patch(twl6040->regmap, twl6040_patch,
712 ARRAY_SIZE(twl6040_patch));
715 * The main functionality of twl6040 to provide audio on OMAP4+ systems.
716 * We can add the ASoC codec child whenever this driver has been loaded.
718 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_PLUG);
719 cell = &twl6040->cells[children];
720 cell->name = "twl6040-codec";
721 twl6040_codec_rsrc[0].start = irq;
722 twl6040_codec_rsrc[0].end = irq;
723 cell->resources = twl6040_codec_rsrc;
724 cell->num_resources = ARRAY_SIZE(twl6040_codec_rsrc);
727 /* Vibra input driver support */
728 if (twl6040_has_vibra(node)) {
729 irq = regmap_irq_get_virq(twl6040->irq_data, TWL6040_IRQ_VIB);
731 cell = &twl6040->cells[children];
732 cell->name = "twl6040-vibra";
733 twl6040_vibra_rsrc[0].start = irq;
734 twl6040_vibra_rsrc[0].end = irq;
735 cell->resources = twl6040_vibra_rsrc;
736 cell->num_resources = ARRAY_SIZE(twl6040_vibra_rsrc);
741 cell = &twl6040->cells[children];
742 cell->name = "twl6040-gpo";
745 /* The chip is powered down so mark regmap to cache only and dirty */
746 regcache_cache_only(twl6040->regmap, true);
747 regcache_mark_dirty(twl6040->regmap);
749 ret = mfd_add_devices(&client->dev, -1, twl6040->cells, children,
757 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
759 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
763 static int twl6040_remove(struct i2c_client *client)
765 struct twl6040 *twl6040 = i2c_get_clientdata(client);
767 if (twl6040->power_count)
768 twl6040_power(twl6040, 0);
770 regmap_del_irq_chip(twl6040->irq, twl6040->irq_data);
772 mfd_remove_devices(&client->dev);
774 regulator_bulk_disable(TWL6040_NUM_SUPPLIES, twl6040->supplies);
779 static const struct i2c_device_id twl6040_i2c_id[] = {
784 MODULE_DEVICE_TABLE(i2c, twl6040_i2c_id);
786 static struct i2c_driver twl6040_driver = {
789 .owner = THIS_MODULE,
791 .probe = twl6040_probe,
792 .remove = twl6040_remove,
793 .id_table = twl6040_i2c_id,
796 module_i2c_driver(twl6040_driver);
798 MODULE_DESCRIPTION("TWL6040 MFD");
801 MODULE_LICENSE("GPL");
802 MODULE_ALIAS("platform:twl6040");