1 // SPDX-License-Identifier: GPL-2.0
2 // Driver for the Texas Instruments TAS2780 Mono
4 // Copyright (C) 2022 Texas Instruments Inc.
6 #include <linux/module.h>
10 #include <linux/gpio.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/regmap.h>
14 #include <linux/of_gpio.h>
15 #include <sound/soc.h>
16 #include <sound/pcm.h>
17 #include <sound/pcm_params.h>
18 #include <sound/tlv.h>
23 struct snd_soc_component *component;
24 struct gpio_desc *reset_gpio;
25 struct regmap *regmap;
31 static void tas2780_reset(struct tas2780_priv *tas2780)
35 if (tas2780->reset_gpio) {
36 gpiod_set_value_cansleep(tas2780->reset_gpio, 0);
37 usleep_range(2000, 2050);
38 gpiod_set_value_cansleep(tas2780->reset_gpio, 1);
39 usleep_range(2000, 2050);
42 snd_soc_component_write(tas2780->component, TAS2780_SW_RST,
45 dev_err(tas2780->dev, "%s:errCode:0x%x Reset error!\n",
50 static int tas2780_codec_suspend(struct snd_soc_component *component)
52 struct tas2780_priv *tas2780 =
53 snd_soc_component_get_drvdata(component);
56 ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
57 TAS2780_PWR_CTRL_MASK, TAS2780_PWR_CTRL_SHUTDOWN);
59 dev_err(tas2780->dev, "%s:errCode:0x%0x:power down error\n",
64 regcache_cache_only(tas2780->regmap, true);
65 regcache_mark_dirty(tas2780->regmap);
70 static int tas2780_codec_resume(struct snd_soc_component *component)
72 struct tas2780_priv *tas2780 =
73 snd_soc_component_get_drvdata(component);
76 ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
77 TAS2780_PWR_CTRL_MASK, TAS2780_PWR_CTRL_ACTIVE);
80 dev_err(tas2780->dev, "%s:errCode:0x%0x:power down error\n",
85 regcache_cache_only(tas2780->regmap, false);
86 ret = regcache_sync(tas2780->regmap);
92 static const char * const tas2780_ASI1_src[] = {
93 "I2C offset", "Left", "Right", "LeftRightDiv2",
96 static SOC_ENUM_SINGLE_DECL(
97 tas2780_ASI1_src_enum, TAS2780_TDM_CFG2, 4, tas2780_ASI1_src);
99 static const struct snd_kcontrol_new tas2780_asi1_mux =
100 SOC_DAPM_ENUM("ASI1 Source", tas2780_ASI1_src_enum);
102 static const struct snd_kcontrol_new isense_switch =
103 SOC_DAPM_SINGLE("Switch", TAS2780_PWR_CTRL,
104 TAS2780_ISENSE_POWER_EN, 1, 1);
105 static const struct snd_kcontrol_new vsense_switch =
106 SOC_DAPM_SINGLE("Switch", TAS2780_PWR_CTRL,
107 TAS2780_VSENSE_POWER_EN, 1, 1);
109 static const struct snd_soc_dapm_widget tas2780_dapm_widgets[] = {
110 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
111 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2780_asi1_mux),
112 SND_SOC_DAPM_SWITCH("ISENSE", TAS2780_PWR_CTRL,
113 TAS2780_ISENSE_POWER_EN, 1, &isense_switch),
114 SND_SOC_DAPM_SWITCH("VSENSE", TAS2780_PWR_CTRL,
115 TAS2780_VSENSE_POWER_EN, 1, &vsense_switch),
116 SND_SOC_DAPM_OUTPUT("OUT"),
117 SND_SOC_DAPM_SIGGEN("VMON"),
118 SND_SOC_DAPM_SIGGEN("IMON")
121 static const struct snd_soc_dapm_route tas2780_audio_map[] = {
122 {"ASI1 Sel", "I2C offset", "ASI1"},
123 {"ASI1 Sel", "Left", "ASI1"},
124 {"ASI1 Sel", "Right", "ASI1"},
125 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
126 {"OUT", NULL, "ASI1 Sel"},
127 {"ISENSE", "Switch", "IMON"},
128 {"VSENSE", "Switch", "VMON"},
131 static int tas2780_mute(struct snd_soc_dai *dai, int mute, int direction)
133 struct snd_soc_component *component = dai->component;
134 struct tas2780_priv *tas2780 =
135 snd_soc_component_get_drvdata(component);
138 ret = snd_soc_component_update_bits(component, TAS2780_PWR_CTRL,
139 TAS2780_PWR_CTRL_MASK,
140 mute ? TAS2780_PWR_CTRL_MUTE : 0);
142 dev_err(tas2780->dev, "%s: Failed to set powercontrol\n",
151 static int tas2780_set_bitwidth(struct tas2780_priv *tas2780, int bitwidth)
153 struct snd_soc_component *component = tas2780->component;
160 case SNDRV_PCM_FORMAT_S16_LE:
161 ret = snd_soc_component_update_bits(component,
163 TAS2780_TDM_CFG2_RXW_MASK,
164 TAS2780_TDM_CFG2_RXW_16BITS);
165 slot_size = TAS2780_TDM_CFG2_RXS_16BITS;
167 case SNDRV_PCM_FORMAT_S24_LE:
168 ret = snd_soc_component_update_bits(component,
170 TAS2780_TDM_CFG2_RXW_MASK,
171 TAS2780_TDM_CFG2_RXW_24BITS);
172 slot_size = TAS2780_TDM_CFG2_RXS_24BITS;
174 case SNDRV_PCM_FORMAT_S32_LE:
175 ret = snd_soc_component_update_bits(component,
177 TAS2780_TDM_CFG2_RXW_MASK,
178 TAS2780_TDM_CFG2_RXW_32BITS);
179 slot_size = TAS2780_TDM_CFG2_RXS_32BITS;
187 dev_err(tas2780->dev, "%s:errCode:0x%x set bitwidth error\n",
192 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
193 TAS2780_TDM_CFG2_RXS_MASK, slot_size);
195 dev_err(tas2780->dev,
196 "%s:errCode:0x%x set RX slot size error\n",
201 val = snd_soc_component_read(tas2780->component, TAS2780_PWR_CTRL);
203 dev_err(tas2780->dev, "%s:errCode:0x%x read PWR_CTRL error\n",
209 if (val & (1 << TAS2780_VSENSE_POWER_EN))
212 sense_en = TAS2780_TDM_CFG5_VSNS_ENABLE;
214 ret = snd_soc_component_update_bits(tas2780->component,
215 TAS2780_TDM_CFG5, TAS2780_TDM_CFG5_VSNS_ENABLE, sense_en);
217 dev_err(tas2780->dev, "%s:errCode:0x%x enable vSNS error\n",
222 if (val & (1 << TAS2780_ISENSE_POWER_EN))
225 sense_en = TAS2780_TDM_CFG6_ISNS_ENABLE;
227 ret = snd_soc_component_update_bits(tas2780->component,
228 TAS2780_TDM_CFG6, TAS2780_TDM_CFG6_ISNS_ENABLE, sense_en);
230 dev_err(tas2780->dev, "%s:errCode:0x%x enable iSNS error\n",
239 static int tas2780_set_samplerate(
240 struct tas2780_priv *tas2780, int samplerate)
242 struct snd_soc_component *component = tas2780->component;
246 switch (samplerate) {
248 ramp_rate_val = TAS2780_TDM_CFG0_SMP_48KHZ |
249 TAS2780_TDM_CFG0_44_1_48KHZ;
252 ramp_rate_val = TAS2780_TDM_CFG0_SMP_44_1KHZ |
253 TAS2780_TDM_CFG0_44_1_48KHZ;
256 ramp_rate_val = TAS2780_TDM_CFG0_SMP_48KHZ |
257 TAS2780_TDM_CFG0_88_2_96KHZ;
260 ramp_rate_val = TAS2780_TDM_CFG0_SMP_44_1KHZ |
261 TAS2780_TDM_CFG0_88_2_96KHZ;
266 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG0,
267 TAS2780_TDM_CFG0_SMP_MASK | TAS2780_TDM_CFG0_MASK,
270 dev_err(tas2780->dev,
271 "%s:errCode:0x%x Failed to set ramp_rate_val\n",
280 static int tas2780_hw_params(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
283 struct snd_soc_component *component = dai->component;
284 struct tas2780_priv *tas2780 =
285 snd_soc_component_get_drvdata(component);
288 ret = tas2780_set_bitwidth(tas2780, params_format(params));
292 return tas2780_set_samplerate(tas2780, params_rate(params));
295 static int tas2780_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
297 struct snd_soc_component *component = dai->component;
298 struct tas2780_priv *tas2780 =
299 snd_soc_component_get_drvdata(component);
300 u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
304 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
305 case SND_SOC_DAIFMT_NB_NF:
306 asi_cfg_1 = TAS2780_TDM_CFG1_RX_RISING;
308 case SND_SOC_DAIFMT_IB_NF:
309 asi_cfg_1 = TAS2780_TDM_CFG1_RX_FALLING;
312 dev_err(tas2780->dev, "ASI format Inverse is not found\n");
316 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG1,
317 TAS2780_TDM_CFG1_RX_MASK, asi_cfg_1);
319 dev_err(tas2780->dev,
320 "%s:errCode:0x%x Failed to set asi_cfg_1\n",
325 if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_I2S)
326 || ((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
327 == SND_SOC_DAIFMT_DSP_A)){
328 iface = TAS2780_TDM_CFG2_SCFG_I2S;
329 tdm_rx_start_slot = 1;
331 if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
332 == SND_SOC_DAIFMT_DSP_B)
333 || ((fmt & SND_SOC_DAIFMT_FORMAT_MASK)
334 == SND_SOC_DAIFMT_LEFT_J)) {
335 iface = TAS2780_TDM_CFG2_SCFG_LEFT_J;
336 tdm_rx_start_slot = 0;
338 dev_err(tas2780->dev,
339 "%s:DAI Format is not found, fmt=0x%x\n",
345 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG1,
346 TAS2780_TDM_CFG1_MASK,
347 (tdm_rx_start_slot << TAS2780_TDM_CFG1_51_SHIFT));
349 dev_err(tas2780->dev,
350 "%s:errCode:0x%x Failed to set tdm_rx_start_slot\n",
355 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
356 TAS2780_TDM_CFG2_SCFG_MASK, iface);
358 dev_err(tas2780->dev, "%s:errCode:0x%x Failed to set iface\n",
367 static int tas2780_set_dai_tdm_slot(struct snd_soc_dai *dai,
368 unsigned int tx_mask,
369 unsigned int rx_mask,
370 int slots, int slot_width)
372 struct snd_soc_component *component = dai->component;
373 struct tas2780_priv *tas2780 =
374 snd_soc_component_get_drvdata(component);
375 int left_slot, right_slot;
380 if (tx_mask == 0 || rx_mask != 0)
383 left_slot = __ffs(tx_mask);
384 tx_mask &= ~(1 << left_slot);
386 right_slot = left_slot;
388 right_slot = __ffs(tx_mask);
389 tx_mask &= ~(1 << right_slot);
392 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
395 slots_cfg = (right_slot << TAS2780_TDM_CFG3_RXS_SHIFT) | left_slot;
396 ret = snd_soc_component_write(component, TAS2780_TDM_CFG3, slots_cfg);
398 dev_err(tas2780->dev,
399 "%s:errCode:0x%x Failed to set slots_cfg\n",
404 switch (slot_width) {
406 slot_size = TAS2780_TDM_CFG2_RXS_16BITS;
409 slot_size = TAS2780_TDM_CFG2_RXS_24BITS;
412 slot_size = TAS2780_TDM_CFG2_RXS_32BITS;
419 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG2,
420 TAS2780_TDM_CFG2_RXS_MASK, slot_size);
422 dev_err(tas2780->dev,
423 "%s:errCode:0x%x Failed to set slot_size\n",
428 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG5,
429 TAS2780_TDM_CFG5_50_MASK, tas2780->v_sense_slot);
431 dev_err(tas2780->dev,
432 "%s:errCode:0x%x Failed to set v_sense_slot\n",
437 ret = snd_soc_component_update_bits(component, TAS2780_TDM_CFG6,
438 TAS2780_TDM_CFG6_50_MASK, tas2780->i_sense_slot);
440 dev_err(tas2780->dev,
441 "%s:errCode:0x%x Failed to set i_sense_slot\n",
450 static const struct snd_soc_dai_ops tas2780_dai_ops = {
451 .mute_stream = tas2780_mute,
452 .hw_params = tas2780_hw_params,
453 .set_fmt = tas2780_set_fmt,
454 .set_tdm_slot = tas2780_set_dai_tdm_slot,
455 .no_capture_mute = 1,
458 #define TAS2780_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
459 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
461 #define TAS2780_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
462 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
464 static struct snd_soc_dai_driver tas2780_dai_driver[] = {
466 .name = "tas2780 ASI1",
469 .stream_name = "ASI1 Playback",
472 .rates = TAS2780_RATES,
473 .formats = TAS2780_FORMATS,
476 .stream_name = "ASI1 Capture",
479 .rates = TAS2780_RATES,
480 .formats = TAS2780_FORMATS,
482 .ops = &tas2780_dai_ops,
487 static int tas2780_codec_probe(struct snd_soc_component *component)
489 struct tas2780_priv *tas2780 =
490 snd_soc_component_get_drvdata(component);
493 tas2780->component = component;
495 tas2780_reset(tas2780);
496 ret = snd_soc_component_update_bits(component,
497 TAS2780_IC_CFG, TAS2780_IC_CFG_MASK,
498 TAS2780_IC_CFG_ENABLE);
500 dev_err(tas2780->dev, "%s:errCode:0x%0x\n",
506 static DECLARE_TLV_DB_SCALE(tas2780_digital_tlv, 1100, 50, 0);
507 static DECLARE_TLV_DB_SCALE(tas2780_playback_volume, -10000, 50, 0);
509 static const struct snd_kcontrol_new tas2780_snd_controls[] = {
510 SOC_SINGLE_TLV("Speaker Volume", TAS2780_DVC, 0,
511 TAS2780_DVC_MAX, 1, tas2780_playback_volume),
512 SOC_SINGLE_TLV("Amp Gain Volume", TAS2780_CHNL_0, 0, 0x14, 0,
513 tas2780_digital_tlv),
516 static const struct snd_soc_component_driver soc_component_driver_tas2780 = {
517 .probe = tas2780_codec_probe,
519 .suspend = tas2780_codec_suspend,
520 .resume = tas2780_codec_resume,
522 .controls = tas2780_snd_controls,
523 .num_controls = ARRAY_SIZE(tas2780_snd_controls),
524 .dapm_widgets = tas2780_dapm_widgets,
525 .num_dapm_widgets = ARRAY_SIZE(tas2780_dapm_widgets),
526 .dapm_routes = tas2780_audio_map,
527 .num_dapm_routes = ARRAY_SIZE(tas2780_audio_map),
532 static const struct reg_default tas2780_reg_defaults[] = {
533 { TAS2780_PAGE, 0x00 },
534 { TAS2780_SW_RST, 0x00 },
535 { TAS2780_PWR_CTRL, 0x1a },
536 { TAS2780_DVC, 0x00 },
537 { TAS2780_CHNL_0, 0x00 },
538 { TAS2780_TDM_CFG0, 0x09 },
539 { TAS2780_TDM_CFG1, 0x02 },
540 { TAS2780_TDM_CFG2, 0x0a },
541 { TAS2780_TDM_CFG3, 0x10 },
542 { TAS2780_TDM_CFG5, 0x42 },
545 static const struct regmap_range_cfg tas2780_regmap_ranges[] = {
548 .range_max = 1 * 128,
549 .selector_reg = TAS2780_PAGE,
550 .selector_mask = 0xff,
557 static const struct regmap_config tas2780_i2c_regmap = {
560 .reg_defaults = tas2780_reg_defaults,
561 .num_reg_defaults = ARRAY_SIZE(tas2780_reg_defaults),
562 .cache_type = REGCACHE_RBTREE,
563 .ranges = tas2780_regmap_ranges,
564 .num_ranges = ARRAY_SIZE(tas2780_regmap_ranges),
565 .max_register = 1 * 128,
568 static int tas2780_parse_dt(struct device *dev, struct tas2780_priv *tas2780)
572 tas2780->reset_gpio = devm_gpiod_get_optional(tas2780->dev, "reset",
574 if (IS_ERR(tas2780->reset_gpio)) {
575 if (PTR_ERR(tas2780->reset_gpio) == -EPROBE_DEFER) {
576 tas2780->reset_gpio = NULL;
577 return -EPROBE_DEFER;
581 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
582 &tas2780->i_sense_slot);
584 tas2780->i_sense_slot = 0;
586 ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
587 &tas2780->v_sense_slot);
589 tas2780->v_sense_slot = 2;
594 static int tas2780_i2c_probe(struct i2c_client *client)
596 struct tas2780_priv *tas2780;
599 tas2780 = devm_kzalloc(&client->dev, sizeof(struct tas2780_priv),
603 tas2780->dev = &client->dev;
604 i2c_set_clientdata(client, tas2780);
605 dev_set_drvdata(&client->dev, tas2780);
607 tas2780->regmap = devm_regmap_init_i2c(client, &tas2780_i2c_regmap);
608 if (IS_ERR(tas2780->regmap)) {
609 result = PTR_ERR(tas2780->regmap);
610 dev_err(&client->dev, "Failed to allocate register map: %d\n",
615 if (client->dev.of_node) {
616 result = tas2780_parse_dt(&client->dev, tas2780);
618 dev_err(tas2780->dev,
619 "%s: Failed to parse devicetree\n", __func__);
624 return devm_snd_soc_register_component(tas2780->dev,
625 &soc_component_driver_tas2780, tas2780_dai_driver,
626 ARRAY_SIZE(tas2780_dai_driver));
629 static const struct i2c_device_id tas2780_i2c_id[] = {
633 MODULE_DEVICE_TABLE(i2c, tas2780_i2c_id);
635 #if defined(CONFIG_OF)
636 static const struct of_device_id tas2780_of_match[] = {
637 { .compatible = "ti,tas2780" },
640 MODULE_DEVICE_TABLE(of, tas2780_of_match);
643 static struct i2c_driver tas2780_i2c_driver = {
646 .of_match_table = of_match_ptr(tas2780_of_match),
648 .probe = tas2780_i2c_probe,
649 .id_table = tas2780_i2c_id,
651 module_i2c_driver(tas2780_i2c_driver);
653 MODULE_AUTHOR("Raphael Xu <raphael-xu@ti.com>");
654 MODULE_DESCRIPTION("TAS2780 I2C Smart Amplifier driver");
655 MODULE_LICENSE("GPL");