1 // SPDX-License-Identifier: GPL-2.0
3 // ALSA SoC Texas Instruments TAS2770 20-W Digital Input Mono Class-D
4 // Audio Amplifier with Speaker I/V Sense
6 // Copyright (C) 2016-2017 Texas Instruments Incorporated - https://www.ti.com/
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/firmware.h>
20 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 #include <sound/soc.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/initval.h>
27 #include <sound/tlv.h>
31 #define TAS2770_MDELAY 0xFFFFFFFE
33 static void tas2770_reset(struct tas2770_priv *tas2770)
35 if (tas2770->reset_gpio) {
36 gpiod_set_value_cansleep(tas2770->reset_gpio, 0);
38 gpiod_set_value_cansleep(tas2770->reset_gpio, 1);
39 usleep_range(1000, 2000);
42 snd_soc_component_write(tas2770->component, TAS2770_SW_RST,
44 usleep_range(1000, 2000);
47 static int tas2770_update_pwr_ctrl(struct tas2770_priv *tas2770)
49 struct snd_soc_component *component = tas2770->component;
53 if (tas2770->dac_powered)
54 val = tas2770->unmuted ?
55 TAS2770_PWR_CTRL_ACTIVE : TAS2770_PWR_CTRL_MUTE;
57 val = TAS2770_PWR_CTRL_SHUTDOWN;
59 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
60 TAS2770_PWR_CTRL_MASK, val);
68 static int tas2770_codec_suspend(struct snd_soc_component *component)
70 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
73 regcache_cache_only(tas2770->regmap, true);
74 regcache_mark_dirty(tas2770->regmap);
76 if (tas2770->sdz_gpio) {
77 gpiod_set_value_cansleep(tas2770->sdz_gpio, 0);
79 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
80 TAS2770_PWR_CTRL_MASK,
81 TAS2770_PWR_CTRL_SHUTDOWN);
83 regcache_cache_only(tas2770->regmap, false);
84 regcache_sync(tas2770->regmap);
94 static int tas2770_codec_resume(struct snd_soc_component *component)
96 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
99 if (tas2770->sdz_gpio) {
100 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
101 usleep_range(1000, 2000);
103 ret = tas2770_update_pwr_ctrl(tas2770);
108 regcache_cache_only(tas2770->regmap, false);
110 return regcache_sync(tas2770->regmap);
113 #define tas2770_codec_suspend NULL
114 #define tas2770_codec_resume NULL
117 static const char * const tas2770_ASI1_src[] = {
118 "I2C offset", "Left", "Right", "LeftRightDiv2",
121 static SOC_ENUM_SINGLE_DECL(
122 tas2770_ASI1_src_enum, TAS2770_TDM_CFG_REG2,
123 4, tas2770_ASI1_src);
125 static const struct snd_kcontrol_new tas2770_asi1_mux =
126 SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum);
128 static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
129 struct snd_kcontrol *kcontrol, int event)
131 struct snd_soc_component *component =
132 snd_soc_dapm_to_component(w->dapm);
133 struct tas2770_priv *tas2770 =
134 snd_soc_component_get_drvdata(component);
138 case SND_SOC_DAPM_POST_PMU:
139 tas2770->dac_powered = 1;
140 ret = tas2770_update_pwr_ctrl(tas2770);
142 case SND_SOC_DAPM_PRE_PMD:
143 tas2770->dac_powered = 0;
144 ret = tas2770_update_pwr_ctrl(tas2770);
147 dev_err(tas2770->dev, "Not supported evevt\n");
154 static const struct snd_kcontrol_new isense_switch =
155 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 3, 1, 1);
156 static const struct snd_kcontrol_new vsense_switch =
157 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 2, 1, 1);
159 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = {
160 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
161 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2770_asi1_mux),
162 SND_SOC_DAPM_SWITCH("ISENSE", TAS2770_PWR_CTRL, 3, 1, &isense_switch),
163 SND_SOC_DAPM_SWITCH("VSENSE", TAS2770_PWR_CTRL, 2, 1, &vsense_switch),
164 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event,
165 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
166 SND_SOC_DAPM_OUTPUT("OUT"),
167 SND_SOC_DAPM_SIGGEN("VMON"),
168 SND_SOC_DAPM_SIGGEN("IMON")
171 static const struct snd_soc_dapm_route tas2770_audio_map[] = {
172 {"ASI1 Sel", "I2C offset", "ASI1"},
173 {"ASI1 Sel", "Left", "ASI1"},
174 {"ASI1 Sel", "Right", "ASI1"},
175 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
176 {"DAC", NULL, "ASI1 Sel"},
177 {"OUT", NULL, "DAC"},
178 {"ISENSE", "Switch", "IMON"},
179 {"VSENSE", "Switch", "VMON"},
182 static int tas2770_mute(struct snd_soc_dai *dai, int mute, int direction)
184 struct snd_soc_component *component = dai->component;
185 struct tas2770_priv *tas2770 =
186 snd_soc_component_get_drvdata(component);
188 tas2770->unmuted = !mute;
189 return tas2770_update_pwr_ctrl(tas2770);
192 static int tas2770_set_bitwidth(struct tas2770_priv *tas2770, int bitwidth)
195 struct snd_soc_component *component = tas2770->component;
198 case SNDRV_PCM_FORMAT_S16_LE:
199 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
200 TAS2770_TDM_CFG_REG2_RXW_MASK,
201 TAS2770_TDM_CFG_REG2_RXW_16BITS);
202 tas2770->v_sense_slot = tas2770->i_sense_slot + 2;
204 case SNDRV_PCM_FORMAT_S24_LE:
205 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
206 TAS2770_TDM_CFG_REG2_RXW_MASK,
207 TAS2770_TDM_CFG_REG2_RXW_24BITS);
208 tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
210 case SNDRV_PCM_FORMAT_S32_LE:
211 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
212 TAS2770_TDM_CFG_REG2_RXW_MASK,
213 TAS2770_TDM_CFG_REG2_RXW_32BITS);
214 tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
224 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG5,
225 TAS2770_TDM_CFG_REG5_VSNS_MASK |
226 TAS2770_TDM_CFG_REG5_50_MASK,
227 TAS2770_TDM_CFG_REG5_VSNS_ENABLE |
228 tas2770->v_sense_slot);
232 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG6,
233 TAS2770_TDM_CFG_REG6_ISNS_MASK |
234 TAS2770_TDM_CFG_REG6_50_MASK,
235 TAS2770_TDM_CFG_REG6_ISNS_ENABLE |
236 tas2770->i_sense_slot);
243 static int tas2770_set_samplerate(struct tas2770_priv *tas2770, int samplerate)
245 struct snd_soc_component *component = tas2770->component;
249 switch (samplerate) {
251 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
252 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
255 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
256 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
259 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
260 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
263 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
264 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
267 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
268 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
271 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
272 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
278 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
279 TAS2770_TDM_CFG_REG0_SMP_MASK |
280 TAS2770_TDM_CFG_REG0_31_MASK,
288 static int tas2770_hw_params(struct snd_pcm_substream *substream,
289 struct snd_pcm_hw_params *params,
290 struct snd_soc_dai *dai)
292 struct snd_soc_component *component = dai->component;
293 struct tas2770_priv *tas2770 =
294 snd_soc_component_get_drvdata(component);
297 ret = tas2770_set_bitwidth(tas2770, params_format(params));
301 return tas2770_set_samplerate(tas2770, params_rate(params));
304 static int tas2770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
306 struct snd_soc_component *component = dai->component;
307 struct tas2770_priv *tas2770 =
308 snd_soc_component_get_drvdata(component);
309 u8 tdm_rx_start_slot = 0, invert_fpol = 0, fpol_preinv = 0, asi_cfg_1 = 0;
312 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
313 case SND_SOC_DAIFMT_CBC_CFC:
316 dev_err(tas2770->dev, "ASI invalid DAI clocking\n");
320 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
321 case SND_SOC_DAIFMT_NB_IF:
324 case SND_SOC_DAIFMT_NB_NF:
325 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_RSING;
327 case SND_SOC_DAIFMT_IB_IF:
330 case SND_SOC_DAIFMT_IB_NF:
331 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_FALING;
334 dev_err(tas2770->dev, "ASI format Inverse is not found\n");
338 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
339 TAS2770_TDM_CFG_REG1_RX_MASK,
344 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
345 case SND_SOC_DAIFMT_I2S:
346 tdm_rx_start_slot = 1;
349 case SND_SOC_DAIFMT_DSP_A:
350 tdm_rx_start_slot = 0;
353 case SND_SOC_DAIFMT_DSP_B:
354 tdm_rx_start_slot = 1;
357 case SND_SOC_DAIFMT_LEFT_J:
358 tdm_rx_start_slot = 0;
362 dev_err(tas2770->dev,
363 "DAI Format is not found, fmt=0x%x\n", fmt);
367 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
368 TAS2770_TDM_CFG_REG1_MASK,
369 (tdm_rx_start_slot << TAS2770_TDM_CFG_REG1_51_SHIFT));
373 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
374 TAS2770_TDM_CFG_REG0_FPOL_MASK,
375 (fpol_preinv ^ invert_fpol)
376 ? TAS2770_TDM_CFG_REG0_FPOL_RSING
377 : TAS2770_TDM_CFG_REG0_FPOL_FALING);
384 static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai,
385 unsigned int tx_mask,
386 unsigned int rx_mask,
387 int slots, int slot_width)
389 struct snd_soc_component *component = dai->component;
390 int left_slot, right_slot;
393 if (tx_mask == 0 || rx_mask != 0)
396 left_slot = __ffs(tx_mask);
397 tx_mask &= ~(1 << left_slot);
399 right_slot = left_slot;
401 right_slot = __ffs(tx_mask);
402 tx_mask &= ~(1 << right_slot);
405 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
408 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
409 TAS2770_TDM_CFG_REG3_30_MASK,
410 (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT));
413 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
414 TAS2770_TDM_CFG_REG3_RXS_MASK,
415 (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT));
419 switch (slot_width) {
421 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
422 TAS2770_TDM_CFG_REG2_RXS_MASK,
423 TAS2770_TDM_CFG_REG2_RXS_16BITS);
426 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
427 TAS2770_TDM_CFG_REG2_RXS_MASK,
428 TAS2770_TDM_CFG_REG2_RXS_24BITS);
431 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
432 TAS2770_TDM_CFG_REG2_RXS_MASK,
433 TAS2770_TDM_CFG_REG2_RXS_32BITS);
436 /* Do not change slot width */
449 static const struct snd_soc_dai_ops tas2770_dai_ops = {
450 .mute_stream = tas2770_mute,
451 .hw_params = tas2770_hw_params,
452 .set_fmt = tas2770_set_fmt,
453 .set_tdm_slot = tas2770_set_dai_tdm_slot,
454 .no_capture_mute = 1,
457 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
458 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
460 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
461 SNDRV_PCM_RATE_96000 |\
462 SNDRV_PCM_RATE_192000\
465 static struct snd_soc_dai_driver tas2770_dai_driver[] = {
467 .name = "tas2770 ASI1",
470 .stream_name = "ASI1 Playback",
473 .rates = TAS2770_RATES,
474 .formats = TAS2770_FORMATS,
477 .stream_name = "ASI1 Capture",
480 .rates = TAS2770_RATES,
481 .formats = TAS2770_FORMATS,
483 .ops = &tas2770_dai_ops,
488 static const struct regmap_config tas2770_i2c_regmap;
490 static int tas2770_codec_probe(struct snd_soc_component *component)
492 struct tas2770_priv *tas2770 =
493 snd_soc_component_get_drvdata(component);
495 tas2770->component = component;
497 if (tas2770->sdz_gpio) {
498 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
499 usleep_range(1000, 2000);
502 tas2770_reset(tas2770);
503 regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap);
508 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
509 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -12750, 50, 0);
511 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
512 SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2,
513 0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume),
514 SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0,
515 tas2770_digital_tlv),
518 static const struct snd_soc_component_driver soc_component_driver_tas2770 = {
519 .probe = tas2770_codec_probe,
520 .suspend = tas2770_codec_suspend,
521 .resume = tas2770_codec_resume,
522 .controls = tas2770_snd_controls,
523 .num_controls = ARRAY_SIZE(tas2770_snd_controls),
524 .dapm_widgets = tas2770_dapm_widgets,
525 .num_dapm_widgets = ARRAY_SIZE(tas2770_dapm_widgets),
526 .dapm_routes = tas2770_audio_map,
527 .num_dapm_routes = ARRAY_SIZE(tas2770_audio_map),
532 static int tas2770_register_codec(struct tas2770_priv *tas2770)
534 return devm_snd_soc_register_component(tas2770->dev,
535 &soc_component_driver_tas2770,
536 tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
539 static const struct reg_default tas2770_reg_defaults[] = {
540 { TAS2770_PAGE, 0x00 },
541 { TAS2770_SW_RST, 0x00 },
542 { TAS2770_PWR_CTRL, 0x0e },
543 { TAS2770_PLAY_CFG_REG0, 0x10 },
544 { TAS2770_PLAY_CFG_REG1, 0x01 },
545 { TAS2770_PLAY_CFG_REG2, 0x00 },
546 { TAS2770_MSC_CFG_REG0, 0x07 },
547 { TAS2770_TDM_CFG_REG1, 0x02 },
548 { TAS2770_TDM_CFG_REG2, 0x0a },
549 { TAS2770_TDM_CFG_REG3, 0x10 },
550 { TAS2770_INT_MASK_REG0, 0xfc },
551 { TAS2770_INT_MASK_REG1, 0xb1 },
552 { TAS2770_INT_CFG, 0x05 },
553 { TAS2770_MISC_IRQ, 0x81 },
554 { TAS2770_CLK_CGF, 0x0c },
558 static bool tas2770_volatile(struct device *dev, unsigned int reg)
561 case TAS2770_PAGE: /* regmap implementation requires this */
562 case TAS2770_SW_RST: /* always clears after write */
563 case TAS2770_BO_PRV_REG0:/* has a self clearing bit */
564 case TAS2770_LVE_INT_REG0:
565 case TAS2770_LVE_INT_REG1:
566 case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */
567 case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */
568 case TAS2770_VBAT_MSB:
569 case TAS2770_VBAT_LSB:
570 case TAS2770_TEMP_MSB:
571 case TAS2770_TEMP_LSB:
578 static bool tas2770_writeable(struct device *dev, unsigned int reg)
581 case TAS2770_LVE_INT_REG0:
582 case TAS2770_LVE_INT_REG1:
583 case TAS2770_LAT_INT_REG0:
584 case TAS2770_LAT_INT_REG1:
585 case TAS2770_VBAT_MSB:
586 case TAS2770_VBAT_LSB:
587 case TAS2770_TEMP_MSB:
588 case TAS2770_TEMP_LSB:
589 case TAS2770_TDM_CLK_DETC:
590 case TAS2770_REV_AND_GPID:
597 static const struct regmap_range_cfg tas2770_regmap_ranges[] = {
600 .range_max = 1 * 128,
601 .selector_reg = TAS2770_PAGE,
602 .selector_mask = 0xff,
609 static const struct regmap_config tas2770_i2c_regmap = {
612 .writeable_reg = tas2770_writeable,
613 .volatile_reg = tas2770_volatile,
614 .reg_defaults = tas2770_reg_defaults,
615 .num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults),
616 .cache_type = REGCACHE_RBTREE,
617 .ranges = tas2770_regmap_ranges,
618 .num_ranges = ARRAY_SIZE(tas2770_regmap_ranges),
619 .max_register = 1 * 128,
622 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770)
626 rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
627 &tas2770->i_sense_slot);
629 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
632 tas2770->i_sense_slot = 0;
635 rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
636 &tas2770->v_sense_slot);
638 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
641 tas2770->v_sense_slot = 2;
644 tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
645 if (IS_ERR(tas2770->sdz_gpio)) {
646 if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER)
647 return -EPROBE_DEFER;
649 tas2770->sdz_gpio = NULL;
655 static int tas2770_i2c_probe(struct i2c_client *client)
657 struct tas2770_priv *tas2770;
660 tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv),
665 tas2770->dev = &client->dev;
666 i2c_set_clientdata(client, tas2770);
667 dev_set_drvdata(&client->dev, tas2770);
669 tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap);
670 if (IS_ERR(tas2770->regmap)) {
671 result = PTR_ERR(tas2770->regmap);
672 dev_err(&client->dev, "Failed to allocate register map: %d\n",
677 if (client->dev.of_node) {
678 result = tas2770_parse_dt(&client->dev, tas2770);
680 dev_err(tas2770->dev, "%s: Failed to parse devicetree\n",
686 tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset",
688 if (IS_ERR(tas2770->reset_gpio)) {
689 if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) {
690 tas2770->reset_gpio = NULL;
691 return -EPROBE_DEFER;
695 result = tas2770_register_codec(tas2770);
697 dev_err(tas2770->dev, "Register codec failed.\n");
702 static const struct i2c_device_id tas2770_i2c_id[] = {
706 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id);
708 #if defined(CONFIG_OF)
709 static const struct of_device_id tas2770_of_match[] = {
710 { .compatible = "ti,tas2770" },
713 MODULE_DEVICE_TABLE(of, tas2770_of_match);
716 static struct i2c_driver tas2770_i2c_driver = {
719 .of_match_table = of_match_ptr(tas2770_of_match),
721 .probe = tas2770_i2c_probe,
722 .id_table = tas2770_i2c_id,
724 module_i2c_driver(tas2770_i2c_driver);
727 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver");
728 MODULE_LICENSE("GPL v2");