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.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/firmware.h>
21 #include <linux/regmap.h>
23 #include <linux/slab.h>
24 #include <sound/soc.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
32 #define TAS2770_MDELAY 0xFFFFFFFE
34 static void tas2770_reset(struct tas2770_priv *tas2770)
36 if (tas2770->reset_gpio) {
37 gpiod_set_value_cansleep(tas2770->reset_gpio, 0);
39 gpiod_set_value_cansleep(tas2770->reset_gpio, 1);
40 usleep_range(1000, 2000);
43 snd_soc_component_write(tas2770->component, TAS2770_SW_RST,
45 usleep_range(1000, 2000);
48 static int tas2770_update_pwr_ctrl(struct tas2770_priv *tas2770)
50 struct snd_soc_component *component = tas2770->component;
54 if (tas2770->dac_powered)
55 val = tas2770->unmuted ?
56 TAS2770_PWR_CTRL_ACTIVE : TAS2770_PWR_CTRL_MUTE;
58 val = TAS2770_PWR_CTRL_SHUTDOWN;
60 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
61 TAS2770_PWR_CTRL_MASK, val);
69 static int tas2770_codec_suspend(struct snd_soc_component *component)
71 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
74 regcache_cache_only(tas2770->regmap, true);
75 regcache_mark_dirty(tas2770->regmap);
77 if (tas2770->sdz_gpio) {
78 gpiod_set_value_cansleep(tas2770->sdz_gpio, 0);
80 ret = snd_soc_component_update_bits(component, TAS2770_PWR_CTRL,
81 TAS2770_PWR_CTRL_MASK,
82 TAS2770_PWR_CTRL_SHUTDOWN);
84 regcache_cache_only(tas2770->regmap, false);
85 regcache_sync(tas2770->regmap);
95 static int tas2770_codec_resume(struct snd_soc_component *component)
97 struct tas2770_priv *tas2770 = snd_soc_component_get_drvdata(component);
100 if (tas2770->sdz_gpio) {
101 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
102 usleep_range(1000, 2000);
104 ret = tas2770_update_pwr_ctrl(tas2770);
109 regcache_cache_only(tas2770->regmap, false);
111 return regcache_sync(tas2770->regmap);
114 #define tas2770_codec_suspend NULL
115 #define tas2770_codec_resume NULL
118 static const char * const tas2770_ASI1_src[] = {
119 "I2C offset", "Left", "Right", "LeftRightDiv2",
122 static SOC_ENUM_SINGLE_DECL(
123 tas2770_ASI1_src_enum, TAS2770_TDM_CFG_REG2,
124 4, tas2770_ASI1_src);
126 static const struct snd_kcontrol_new tas2770_asi1_mux =
127 SOC_DAPM_ENUM("ASI1 Source", tas2770_ASI1_src_enum);
129 static int tas2770_dac_event(struct snd_soc_dapm_widget *w,
130 struct snd_kcontrol *kcontrol, int event)
132 struct snd_soc_component *component =
133 snd_soc_dapm_to_component(w->dapm);
134 struct tas2770_priv *tas2770 =
135 snd_soc_component_get_drvdata(component);
139 case SND_SOC_DAPM_POST_PMU:
140 tas2770->dac_powered = 1;
141 ret = tas2770_update_pwr_ctrl(tas2770);
143 case SND_SOC_DAPM_PRE_PMD:
144 tas2770->dac_powered = 0;
145 ret = tas2770_update_pwr_ctrl(tas2770);
148 dev_err(tas2770->dev, "Not supported evevt\n");
155 static const struct snd_kcontrol_new isense_switch =
156 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 3, 1, 1);
157 static const struct snd_kcontrol_new vsense_switch =
158 SOC_DAPM_SINGLE("Switch", TAS2770_PWR_CTRL, 2, 1, 1);
160 static const struct snd_soc_dapm_widget tas2770_dapm_widgets[] = {
161 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
162 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2770_asi1_mux),
163 SND_SOC_DAPM_SWITCH("ISENSE", TAS2770_PWR_CTRL, 3, 1, &isense_switch),
164 SND_SOC_DAPM_SWITCH("VSENSE", TAS2770_PWR_CTRL, 2, 1, &vsense_switch),
165 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2770_dac_event,
166 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
167 SND_SOC_DAPM_OUTPUT("OUT"),
168 SND_SOC_DAPM_SIGGEN("VMON"),
169 SND_SOC_DAPM_SIGGEN("IMON")
172 static const struct snd_soc_dapm_route tas2770_audio_map[] = {
173 {"ASI1 Sel", "I2C offset", "ASI1"},
174 {"ASI1 Sel", "Left", "ASI1"},
175 {"ASI1 Sel", "Right", "ASI1"},
176 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
177 {"DAC", NULL, "ASI1 Sel"},
178 {"OUT", NULL, "DAC"},
179 {"ISENSE", "Switch", "IMON"},
180 {"VSENSE", "Switch", "VMON"},
183 static int tas2770_mute(struct snd_soc_dai *dai, int mute, int direction)
185 struct snd_soc_component *component = dai->component;
186 struct tas2770_priv *tas2770 =
187 snd_soc_component_get_drvdata(component);
189 tas2770->unmuted = !mute;
190 return tas2770_update_pwr_ctrl(tas2770);
193 static int tas2770_set_bitwidth(struct tas2770_priv *tas2770, int bitwidth)
196 struct snd_soc_component *component = tas2770->component;
199 case SNDRV_PCM_FORMAT_S16_LE:
200 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
201 TAS2770_TDM_CFG_REG2_RXW_MASK,
202 TAS2770_TDM_CFG_REG2_RXW_16BITS);
203 tas2770->v_sense_slot = tas2770->i_sense_slot + 2;
205 case SNDRV_PCM_FORMAT_S24_LE:
206 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
207 TAS2770_TDM_CFG_REG2_RXW_MASK,
208 TAS2770_TDM_CFG_REG2_RXW_24BITS);
209 tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
211 case SNDRV_PCM_FORMAT_S32_LE:
212 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
213 TAS2770_TDM_CFG_REG2_RXW_MASK,
214 TAS2770_TDM_CFG_REG2_RXW_32BITS);
215 tas2770->v_sense_slot = tas2770->i_sense_slot + 4;
225 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG5,
226 TAS2770_TDM_CFG_REG5_VSNS_MASK |
227 TAS2770_TDM_CFG_REG5_50_MASK,
228 TAS2770_TDM_CFG_REG5_VSNS_ENABLE |
229 tas2770->v_sense_slot);
233 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG6,
234 TAS2770_TDM_CFG_REG6_ISNS_MASK |
235 TAS2770_TDM_CFG_REG6_50_MASK,
236 TAS2770_TDM_CFG_REG6_ISNS_ENABLE |
237 tas2770->i_sense_slot);
244 static int tas2770_set_samplerate(struct tas2770_priv *tas2770, int samplerate)
246 struct snd_soc_component *component = tas2770->component;
250 switch (samplerate) {
252 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
253 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
256 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
257 TAS2770_TDM_CFG_REG0_31_44_1_48KHZ;
260 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
261 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
264 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
265 TAS2770_TDM_CFG_REG0_31_88_2_96KHZ;
268 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_48KHZ |
269 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
272 ramp_rate_val = TAS2770_TDM_CFG_REG0_SMP_44_1KHZ |
273 TAS2770_TDM_CFG_REG0_31_176_4_192KHZ;
279 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
280 TAS2770_TDM_CFG_REG0_SMP_MASK |
281 TAS2770_TDM_CFG_REG0_31_MASK,
289 static int tas2770_hw_params(struct snd_pcm_substream *substream,
290 struct snd_pcm_hw_params *params,
291 struct snd_soc_dai *dai)
293 struct snd_soc_component *component = dai->component;
294 struct tas2770_priv *tas2770 =
295 snd_soc_component_get_drvdata(component);
298 ret = tas2770_set_bitwidth(tas2770, params_format(params));
302 return tas2770_set_samplerate(tas2770, params_rate(params));
305 static int tas2770_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
307 struct snd_soc_component *component = dai->component;
308 struct tas2770_priv *tas2770 =
309 snd_soc_component_get_drvdata(component);
310 u8 tdm_rx_start_slot = 0, invert_fpol = 0, fpol_preinv = 0, asi_cfg_1 = 0;
313 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
314 case SND_SOC_DAIFMT_CBC_CFC:
317 dev_err(tas2770->dev, "ASI invalid DAI clocking\n");
321 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
322 case SND_SOC_DAIFMT_NB_IF:
325 case SND_SOC_DAIFMT_NB_NF:
326 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_RSING;
328 case SND_SOC_DAIFMT_IB_IF:
331 case SND_SOC_DAIFMT_IB_NF:
332 asi_cfg_1 |= TAS2770_TDM_CFG_REG1_RX_FALING;
335 dev_err(tas2770->dev, "ASI format Inverse is not found\n");
339 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
340 TAS2770_TDM_CFG_REG1_RX_MASK,
345 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
346 case SND_SOC_DAIFMT_I2S:
347 tdm_rx_start_slot = 1;
350 case SND_SOC_DAIFMT_DSP_A:
351 tdm_rx_start_slot = 0;
354 case SND_SOC_DAIFMT_DSP_B:
355 tdm_rx_start_slot = 1;
358 case SND_SOC_DAIFMT_LEFT_J:
359 tdm_rx_start_slot = 0;
363 dev_err(tas2770->dev,
364 "DAI Format is not found, fmt=0x%x\n", fmt);
368 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG1,
369 TAS2770_TDM_CFG_REG1_MASK,
370 (tdm_rx_start_slot << TAS2770_TDM_CFG_REG1_51_SHIFT));
374 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG0,
375 TAS2770_TDM_CFG_REG0_FPOL_MASK,
376 (fpol_preinv ^ invert_fpol)
377 ? TAS2770_TDM_CFG_REG0_FPOL_RSING
378 : TAS2770_TDM_CFG_REG0_FPOL_FALING);
385 static int tas2770_set_dai_tdm_slot(struct snd_soc_dai *dai,
386 unsigned int tx_mask,
387 unsigned int rx_mask,
388 int slots, int slot_width)
390 struct snd_soc_component *component = dai->component;
391 int left_slot, right_slot;
394 if (tx_mask == 0 || rx_mask != 0)
397 left_slot = __ffs(tx_mask);
398 tx_mask &= ~(1 << left_slot);
400 right_slot = left_slot;
402 right_slot = __ffs(tx_mask);
403 tx_mask &= ~(1 << right_slot);
406 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
409 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
410 TAS2770_TDM_CFG_REG3_30_MASK,
411 (left_slot << TAS2770_TDM_CFG_REG3_30_SHIFT));
414 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG3,
415 TAS2770_TDM_CFG_REG3_RXS_MASK,
416 (right_slot << TAS2770_TDM_CFG_REG3_RXS_SHIFT));
420 switch (slot_width) {
422 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
423 TAS2770_TDM_CFG_REG2_RXS_MASK,
424 TAS2770_TDM_CFG_REG2_RXS_16BITS);
427 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
428 TAS2770_TDM_CFG_REG2_RXS_MASK,
429 TAS2770_TDM_CFG_REG2_RXS_24BITS);
432 ret = snd_soc_component_update_bits(component, TAS2770_TDM_CFG_REG2,
433 TAS2770_TDM_CFG_REG2_RXS_MASK,
434 TAS2770_TDM_CFG_REG2_RXS_32BITS);
437 /* Do not change slot width */
450 static const struct snd_soc_dai_ops tas2770_dai_ops = {
451 .mute_stream = tas2770_mute,
452 .hw_params = tas2770_hw_params,
453 .set_fmt = tas2770_set_fmt,
454 .set_tdm_slot = tas2770_set_dai_tdm_slot,
455 .no_capture_mute = 1,
458 #define TAS2770_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
459 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
461 #define TAS2770_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
462 SNDRV_PCM_RATE_96000 |\
463 SNDRV_PCM_RATE_192000\
466 static struct snd_soc_dai_driver tas2770_dai_driver[] = {
468 .name = "tas2770 ASI1",
471 .stream_name = "ASI1 Playback",
474 .rates = TAS2770_RATES,
475 .formats = TAS2770_FORMATS,
478 .stream_name = "ASI1 Capture",
481 .rates = TAS2770_RATES,
482 .formats = TAS2770_FORMATS,
484 .ops = &tas2770_dai_ops,
489 static const struct regmap_config tas2770_i2c_regmap;
491 static int tas2770_codec_probe(struct snd_soc_component *component)
493 struct tas2770_priv *tas2770 =
494 snd_soc_component_get_drvdata(component);
496 tas2770->component = component;
498 if (tas2770->sdz_gpio) {
499 gpiod_set_value_cansleep(tas2770->sdz_gpio, 1);
500 usleep_range(1000, 2000);
503 tas2770_reset(tas2770);
504 regmap_reinit_cache(tas2770->regmap, &tas2770_i2c_regmap);
509 static DECLARE_TLV_DB_SCALE(tas2770_digital_tlv, 1100, 50, 0);
510 static DECLARE_TLV_DB_SCALE(tas2770_playback_volume, -12750, 50, 0);
512 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
513 SOC_SINGLE_TLV("Speaker Playback Volume", TAS2770_PLAY_CFG_REG2,
514 0, TAS2770_PLAY_CFG_REG2_VMAX, 1, tas2770_playback_volume),
515 SOC_SINGLE_TLV("Amp Gain Volume", TAS2770_PLAY_CFG_REG0, 0, 0x14, 0,
516 tas2770_digital_tlv),
519 static const struct snd_soc_component_driver soc_component_driver_tas2770 = {
520 .probe = tas2770_codec_probe,
521 .suspend = tas2770_codec_suspend,
522 .resume = tas2770_codec_resume,
523 .controls = tas2770_snd_controls,
524 .num_controls = ARRAY_SIZE(tas2770_snd_controls),
525 .dapm_widgets = tas2770_dapm_widgets,
526 .num_dapm_widgets = ARRAY_SIZE(tas2770_dapm_widgets),
527 .dapm_routes = tas2770_audio_map,
528 .num_dapm_routes = ARRAY_SIZE(tas2770_audio_map),
533 static int tas2770_register_codec(struct tas2770_priv *tas2770)
535 return devm_snd_soc_register_component(tas2770->dev,
536 &soc_component_driver_tas2770,
537 tas2770_dai_driver, ARRAY_SIZE(tas2770_dai_driver));
540 static const struct reg_default tas2770_reg_defaults[] = {
541 { TAS2770_PAGE, 0x00 },
542 { TAS2770_SW_RST, 0x00 },
543 { TAS2770_PWR_CTRL, 0x0e },
544 { TAS2770_PLAY_CFG_REG0, 0x10 },
545 { TAS2770_PLAY_CFG_REG1, 0x01 },
546 { TAS2770_PLAY_CFG_REG2, 0x00 },
547 { TAS2770_MSC_CFG_REG0, 0x07 },
548 { TAS2770_TDM_CFG_REG1, 0x02 },
549 { TAS2770_TDM_CFG_REG2, 0x0a },
550 { TAS2770_TDM_CFG_REG3, 0x10 },
551 { TAS2770_INT_MASK_REG0, 0xfc },
552 { TAS2770_INT_MASK_REG1, 0xb1 },
553 { TAS2770_INT_CFG, 0x05 },
554 { TAS2770_MISC_IRQ, 0x81 },
555 { TAS2770_CLK_CGF, 0x0c },
559 static bool tas2770_volatile(struct device *dev, unsigned int reg)
562 case TAS2770_PAGE: /* regmap implementation requires this */
563 case TAS2770_SW_RST: /* always clears after write */
564 case TAS2770_BO_PRV_REG0:/* has a self clearing bit */
565 case TAS2770_LVE_INT_REG0:
566 case TAS2770_LVE_INT_REG1:
567 case TAS2770_LAT_INT_REG0:/* Sticky interrupt flags */
568 case TAS2770_LAT_INT_REG1:/* Sticky interrupt flags */
569 case TAS2770_VBAT_MSB:
570 case TAS2770_VBAT_LSB:
571 case TAS2770_TEMP_MSB:
572 case TAS2770_TEMP_LSB:
579 static bool tas2770_writeable(struct device *dev, unsigned int reg)
582 case TAS2770_LVE_INT_REG0:
583 case TAS2770_LVE_INT_REG1:
584 case TAS2770_LAT_INT_REG0:
585 case TAS2770_LAT_INT_REG1:
586 case TAS2770_VBAT_MSB:
587 case TAS2770_VBAT_LSB:
588 case TAS2770_TEMP_MSB:
589 case TAS2770_TEMP_LSB:
590 case TAS2770_TDM_CLK_DETC:
591 case TAS2770_REV_AND_GPID:
598 static const struct regmap_range_cfg tas2770_regmap_ranges[] = {
601 .range_max = 1 * 128,
602 .selector_reg = TAS2770_PAGE,
603 .selector_mask = 0xff,
610 static const struct regmap_config tas2770_i2c_regmap = {
613 .writeable_reg = tas2770_writeable,
614 .volatile_reg = tas2770_volatile,
615 .reg_defaults = tas2770_reg_defaults,
616 .num_reg_defaults = ARRAY_SIZE(tas2770_reg_defaults),
617 .cache_type = REGCACHE_RBTREE,
618 .ranges = tas2770_regmap_ranges,
619 .num_ranges = ARRAY_SIZE(tas2770_regmap_ranges),
620 .max_register = 1 * 128,
623 static int tas2770_parse_dt(struct device *dev, struct tas2770_priv *tas2770)
627 rc = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
628 &tas2770->i_sense_slot);
630 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
633 tas2770->i_sense_slot = 0;
636 rc = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
637 &tas2770->v_sense_slot);
639 dev_info(tas2770->dev, "Property %s is missing setting default slot\n",
642 tas2770->v_sense_slot = 2;
645 tas2770->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
646 if (IS_ERR(tas2770->sdz_gpio)) {
647 if (PTR_ERR(tas2770->sdz_gpio) == -EPROBE_DEFER)
648 return -EPROBE_DEFER;
650 tas2770->sdz_gpio = NULL;
656 static int tas2770_i2c_probe(struct i2c_client *client)
658 struct tas2770_priv *tas2770;
661 tas2770 = devm_kzalloc(&client->dev, sizeof(struct tas2770_priv),
666 tas2770->dev = &client->dev;
667 i2c_set_clientdata(client, tas2770);
668 dev_set_drvdata(&client->dev, tas2770);
670 tas2770->regmap = devm_regmap_init_i2c(client, &tas2770_i2c_regmap);
671 if (IS_ERR(tas2770->regmap)) {
672 result = PTR_ERR(tas2770->regmap);
673 dev_err(&client->dev, "Failed to allocate register map: %d\n",
678 if (client->dev.of_node) {
679 result = tas2770_parse_dt(&client->dev, tas2770);
681 dev_err(tas2770->dev, "%s: Failed to parse devicetree\n",
687 tas2770->reset_gpio = devm_gpiod_get_optional(tas2770->dev, "reset",
689 if (IS_ERR(tas2770->reset_gpio)) {
690 if (PTR_ERR(tas2770->reset_gpio) == -EPROBE_DEFER) {
691 tas2770->reset_gpio = NULL;
692 return -EPROBE_DEFER;
696 result = tas2770_register_codec(tas2770);
698 dev_err(tas2770->dev, "Register codec failed.\n");
703 static const struct i2c_device_id tas2770_i2c_id[] = {
707 MODULE_DEVICE_TABLE(i2c, tas2770_i2c_id);
709 #if defined(CONFIG_OF)
710 static const struct of_device_id tas2770_of_match[] = {
711 { .compatible = "ti,tas2770" },
714 MODULE_DEVICE_TABLE(of, tas2770_of_match);
717 static struct i2c_driver tas2770_i2c_driver = {
720 .of_match_table = of_match_ptr(tas2770_of_match),
722 .probe = tas2770_i2c_probe,
723 .id_table = tas2770_i2c_id,
725 module_i2c_driver(tas2770_i2c_driver);
728 MODULE_DESCRIPTION("TAS2770 I2C Smart Amplifier driver");
729 MODULE_LICENSE("GPL v2");