1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for the Texas Instruments TAS2764 CODEC
4 // Copyright (C) 2020 Texas Instruments Inc.
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
9 #include <linux/init.h>
10 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/gpio.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/regmap.h>
18 #include <linux/of_gpio.h>
19 #include <linux/slab.h>
20 #include <sound/soc.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/initval.h>
24 #include <sound/tlv.h>
29 struct snd_soc_component *component;
30 struct gpio_desc *reset_gpio;
31 struct gpio_desc *sdz_gpio;
32 struct regmap *regmap;
39 static void tas2764_reset(struct tas2764_priv *tas2764)
41 if (tas2764->reset_gpio) {
42 gpiod_set_value_cansleep(tas2764->reset_gpio, 0);
44 gpiod_set_value_cansleep(tas2764->reset_gpio, 1);
45 usleep_range(1000, 2000);
48 snd_soc_component_write(tas2764->component, TAS2764_SW_RST,
50 usleep_range(1000, 2000);
53 static int tas2764_set_bias_level(struct snd_soc_component *component,
54 enum snd_soc_bias_level level)
56 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
60 snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
61 TAS2764_PWR_CTRL_MASK,
62 TAS2764_PWR_CTRL_ACTIVE);
64 case SND_SOC_BIAS_STANDBY:
65 case SND_SOC_BIAS_PREPARE:
66 snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
67 TAS2764_PWR_CTRL_MASK,
68 TAS2764_PWR_CTRL_MUTE);
70 case SND_SOC_BIAS_OFF:
71 snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
72 TAS2764_PWR_CTRL_MASK,
73 TAS2764_PWR_CTRL_SHUTDOWN);
78 "wrong power level setting %d\n", level);
86 static int tas2764_codec_suspend(struct snd_soc_component *component)
88 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
91 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
92 TAS2764_PWR_CTRL_MASK,
93 TAS2764_PWR_CTRL_SHUTDOWN);
98 if (tas2764->sdz_gpio)
99 gpiod_set_value_cansleep(tas2764->sdz_gpio, 0);
101 regcache_cache_only(tas2764->regmap, true);
102 regcache_mark_dirty(tas2764->regmap);
107 static int tas2764_codec_resume(struct snd_soc_component *component)
109 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
112 if (tas2764->sdz_gpio) {
113 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
114 usleep_range(1000, 2000);
117 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
118 TAS2764_PWR_CTRL_MASK,
119 TAS2764_PWR_CTRL_ACTIVE);
124 regcache_cache_only(tas2764->regmap, false);
126 return regcache_sync(tas2764->regmap);
129 #define tas2764_codec_suspend NULL
130 #define tas2764_codec_resume NULL
133 static const char * const tas2764_ASI1_src[] = {
134 "I2C offset", "Left", "Right", "LeftRightDiv2",
137 static SOC_ENUM_SINGLE_DECL(
138 tas2764_ASI1_src_enum, TAS2764_TDM_CFG2, TAS2764_TDM_CFG2_SCFG_SHIFT,
141 static const struct snd_kcontrol_new tas2764_asi1_mux =
142 SOC_DAPM_ENUM("ASI1 Source", tas2764_ASI1_src_enum);
144 static int tas2764_dac_event(struct snd_soc_dapm_widget *w,
145 struct snd_kcontrol *kcontrol, int event)
147 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
148 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
152 case SND_SOC_DAPM_POST_PMU:
153 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
154 TAS2764_PWR_CTRL_MASK,
155 TAS2764_PWR_CTRL_MUTE);
157 case SND_SOC_DAPM_PRE_PMD:
158 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
159 TAS2764_PWR_CTRL_MASK,
160 TAS2764_PWR_CTRL_SHUTDOWN);
163 dev_err(tas2764->dev, "Unsupported event\n");
173 static const struct snd_kcontrol_new isense_switch =
174 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN, 1, 1);
175 static const struct snd_kcontrol_new vsense_switch =
176 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN, 1, 1);
178 static const struct snd_soc_dapm_widget tas2764_dapm_widgets[] = {
179 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
180 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2764_asi1_mux),
181 SND_SOC_DAPM_SWITCH("ISENSE", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN,
183 SND_SOC_DAPM_SWITCH("VSENSE", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN,
185 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2764_dac_event,
186 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
187 SND_SOC_DAPM_OUTPUT("OUT"),
188 SND_SOC_DAPM_SIGGEN("VMON"),
189 SND_SOC_DAPM_SIGGEN("IMON")
192 static const struct snd_soc_dapm_route tas2764_audio_map[] = {
193 {"ASI1 Sel", "I2C offset", "ASI1"},
194 {"ASI1 Sel", "Left", "ASI1"},
195 {"ASI1 Sel", "Right", "ASI1"},
196 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
197 {"DAC", NULL, "ASI1 Sel"},
198 {"OUT", NULL, "DAC"},
199 {"ISENSE", "Switch", "IMON"},
200 {"VSENSE", "Switch", "VMON"},
203 static int tas2764_mute(struct snd_soc_dai *dai, int mute, int direction)
205 struct snd_soc_component *component = dai->component;
208 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
209 TAS2764_PWR_CTRL_MASK,
210 mute ? TAS2764_PWR_CTRL_MUTE : 0);
218 static int tas2764_set_bitwidth(struct tas2764_priv *tas2764, int bitwidth)
220 struct snd_soc_component *component = tas2764->component;
226 case SNDRV_PCM_FORMAT_S16_LE:
227 ret = snd_soc_component_update_bits(component,
229 TAS2764_TDM_CFG2_RXW_MASK,
230 TAS2764_TDM_CFG2_RXW_16BITS);
232 case SNDRV_PCM_FORMAT_S24_LE:
233 ret = snd_soc_component_update_bits(component,
235 TAS2764_TDM_CFG2_RXW_MASK,
236 TAS2764_TDM_CFG2_RXW_24BITS);
238 case SNDRV_PCM_FORMAT_S32_LE:
239 ret = snd_soc_component_update_bits(component,
241 TAS2764_TDM_CFG2_RXW_MASK,
242 TAS2764_TDM_CFG2_RXW_32BITS);
252 val = snd_soc_component_read(tas2764->component, TAS2764_PWR_CTRL);
256 if (val & (1 << TAS2764_VSENSE_POWER_EN))
259 sense_en = TAS2764_TDM_CFG5_VSNS_ENABLE;
261 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
262 TAS2764_TDM_CFG5_VSNS_ENABLE,
267 if (val & (1 << TAS2764_ISENSE_POWER_EN))
270 sense_en = TAS2764_TDM_CFG6_ISNS_ENABLE;
272 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
273 TAS2764_TDM_CFG6_ISNS_ENABLE,
281 static int tas2764_set_samplerate(struct tas2764_priv *tas2764, int samplerate)
283 struct snd_soc_component *component = tas2764->component;
287 switch (samplerate) {
289 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
290 TAS2764_TDM_CFG0_44_1_48KHZ;
293 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
294 TAS2764_TDM_CFG0_44_1_48KHZ;
297 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
298 TAS2764_TDM_CFG0_88_2_96KHZ;
301 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
302 TAS2764_TDM_CFG0_88_2_96KHZ;
308 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
309 TAS2764_TDM_CFG0_SMP_MASK |
310 TAS2764_TDM_CFG0_MASK,
318 static int tas2764_hw_params(struct snd_pcm_substream *substream,
319 struct snd_pcm_hw_params *params,
320 struct snd_soc_dai *dai)
322 struct snd_soc_component *component = dai->component;
323 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
326 ret = tas2764_set_bitwidth(tas2764, params_format(params));
330 return tas2764_set_samplerate(tas2764, params_rate(params));
333 static int tas2764_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
335 struct snd_soc_component *component = dai->component;
336 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
337 u8 tdm_rx_start_slot = 0, asi_cfg_0 = 0, asi_cfg_1 = 0;
340 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
341 case SND_SOC_DAIFMT_NB_IF:
342 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
344 case SND_SOC_DAIFMT_NB_NF:
345 asi_cfg_1 = TAS2764_TDM_CFG1_RX_RISING;
347 case SND_SOC_DAIFMT_IB_IF:
348 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
350 case SND_SOC_DAIFMT_IB_NF:
351 asi_cfg_1 = TAS2764_TDM_CFG1_RX_FALLING;
355 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
356 TAS2764_TDM_CFG1_RX_MASK,
361 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
362 case SND_SOC_DAIFMT_I2S:
363 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
365 case SND_SOC_DAIFMT_DSP_A:
366 tdm_rx_start_slot = 1;
368 case SND_SOC_DAIFMT_DSP_B:
369 case SND_SOC_DAIFMT_LEFT_J:
370 tdm_rx_start_slot = 0;
373 dev_err(tas2764->dev,
374 "DAI Format is not found, fmt=0x%x\n", fmt);
378 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
379 TAS2764_TDM_CFG0_FRAME_START,
384 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
385 TAS2764_TDM_CFG1_MASK,
386 (tdm_rx_start_slot << TAS2764_TDM_CFG1_51_SHIFT));
393 static int tas2764_set_dai_tdm_slot(struct snd_soc_dai *dai,
394 unsigned int tx_mask,
395 unsigned int rx_mask,
396 int slots, int slot_width)
398 struct snd_soc_component *component = dai->component;
399 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
400 int left_slot, right_slot;
405 if (tx_mask == 0 || rx_mask != 0)
414 left_slot = __ffs(tx_mask);
415 tx_mask &= ~(1 << left_slot);
417 right_slot = left_slot;
419 right_slot = __ffs(tx_mask);
420 tx_mask &= ~(1 << right_slot);
424 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
427 slots_cfg = (right_slot << TAS2764_TDM_CFG3_RXS_SHIFT) | left_slot;
429 ret = snd_soc_component_write(component, TAS2764_TDM_CFG3, slots_cfg);
433 switch (slot_width) {
435 slot_size = TAS2764_TDM_CFG2_RXS_16BITS;
438 slot_size = TAS2764_TDM_CFG2_RXS_24BITS;
441 slot_size = TAS2764_TDM_CFG2_RXS_32BITS;
447 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG2,
448 TAS2764_TDM_CFG2_RXS_MASK,
453 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG5,
454 TAS2764_TDM_CFG5_50_MASK,
455 tas2764->v_sense_slot);
459 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG6,
460 TAS2764_TDM_CFG6_50_MASK,
461 tas2764->i_sense_slot);
468 static const struct snd_soc_dai_ops tas2764_dai_ops = {
469 .mute_stream = tas2764_mute,
470 .hw_params = tas2764_hw_params,
471 .set_fmt = tas2764_set_fmt,
472 .set_tdm_slot = tas2764_set_dai_tdm_slot,
473 .no_capture_mute = 1,
476 #define TAS2764_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
477 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
479 #define TAS2764_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
480 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
482 static struct snd_soc_dai_driver tas2764_dai_driver[] = {
484 .name = "tas2764 ASI1",
487 .stream_name = "ASI1 Playback",
490 .rates = TAS2764_RATES,
491 .formats = TAS2764_FORMATS,
494 .stream_name = "ASI1 Capture",
497 .rates = TAS2764_RATES,
498 .formats = TAS2764_FORMATS,
500 .ops = &tas2764_dai_ops,
505 static int tas2764_codec_probe(struct snd_soc_component *component)
507 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
510 tas2764->component = component;
512 if (tas2764->sdz_gpio) {
513 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
514 usleep_range(1000, 2000);
517 tas2764_reset(tas2764);
519 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
520 TAS2764_TDM_CFG5_VSNS_ENABLE, 0);
524 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
525 TAS2764_TDM_CFG6_ISNS_ENABLE, 0);
529 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
530 TAS2764_PWR_CTRL_MASK,
531 TAS2764_PWR_CTRL_MUTE);
538 static DECLARE_TLV_DB_SCALE(tas2764_digital_tlv, 1100, 50, 0);
539 static DECLARE_TLV_DB_SCALE(tas2764_playback_volume, -10050, 50, 1);
541 static const struct snd_kcontrol_new tas2764_snd_controls[] = {
542 SOC_SINGLE_TLV("Speaker Volume", TAS2764_DVC, 0,
543 TAS2764_DVC_MAX, 1, tas2764_playback_volume),
544 SOC_SINGLE_TLV("Amp Gain Volume", TAS2764_CHNL_0, 1, 0x14, 0,
545 tas2764_digital_tlv),
548 static const struct snd_soc_component_driver soc_component_driver_tas2764 = {
549 .probe = tas2764_codec_probe,
550 .suspend = tas2764_codec_suspend,
551 .resume = tas2764_codec_resume,
552 .set_bias_level = tas2764_set_bias_level,
553 .controls = tas2764_snd_controls,
554 .num_controls = ARRAY_SIZE(tas2764_snd_controls),
555 .dapm_widgets = tas2764_dapm_widgets,
556 .num_dapm_widgets = ARRAY_SIZE(tas2764_dapm_widgets),
557 .dapm_routes = tas2764_audio_map,
558 .num_dapm_routes = ARRAY_SIZE(tas2764_audio_map),
561 .non_legacy_dai_naming = 1,
564 static const struct reg_default tas2764_reg_defaults[] = {
565 { TAS2764_PAGE, 0x00 },
566 { TAS2764_SW_RST, 0x00 },
567 { TAS2764_PWR_CTRL, 0x1a },
568 { TAS2764_DVC, 0x00 },
569 { TAS2764_CHNL_0, 0x28 },
570 { TAS2764_TDM_CFG0, 0x09 },
571 { TAS2764_TDM_CFG1, 0x02 },
572 { TAS2764_TDM_CFG2, 0x0a },
573 { TAS2764_TDM_CFG3, 0x10 },
574 { TAS2764_TDM_CFG5, 0x42 },
577 static const struct regmap_range_cfg tas2764_regmap_ranges[] = {
580 .range_max = 1 * 128,
581 .selector_reg = TAS2764_PAGE,
582 .selector_mask = 0xff,
589 static const struct regmap_config tas2764_i2c_regmap = {
592 .reg_defaults = tas2764_reg_defaults,
593 .num_reg_defaults = ARRAY_SIZE(tas2764_reg_defaults),
594 .cache_type = REGCACHE_RBTREE,
595 .ranges = tas2764_regmap_ranges,
596 .num_ranges = ARRAY_SIZE(tas2764_regmap_ranges),
597 .max_register = 1 * 128,
600 static int tas2764_parse_dt(struct device *dev, struct tas2764_priv *tas2764)
604 tas2764->reset_gpio = devm_gpiod_get_optional(tas2764->dev, "reset",
606 if (IS_ERR(tas2764->reset_gpio)) {
607 if (PTR_ERR(tas2764->reset_gpio) == -EPROBE_DEFER) {
608 tas2764->reset_gpio = NULL;
609 return -EPROBE_DEFER;
613 tas2764->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
614 if (IS_ERR(tas2764->sdz_gpio)) {
615 if (PTR_ERR(tas2764->sdz_gpio) == -EPROBE_DEFER)
616 return -EPROBE_DEFER;
618 tas2764->sdz_gpio = NULL;
621 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
622 &tas2764->i_sense_slot);
624 tas2764->i_sense_slot = 0;
626 ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
627 &tas2764->v_sense_slot);
629 tas2764->v_sense_slot = 2;
634 static int tas2764_i2c_probe(struct i2c_client *client)
636 struct tas2764_priv *tas2764;
639 tas2764 = devm_kzalloc(&client->dev, sizeof(struct tas2764_priv),
644 tas2764->dev = &client->dev;
645 i2c_set_clientdata(client, tas2764);
646 dev_set_drvdata(&client->dev, tas2764);
648 tas2764->regmap = devm_regmap_init_i2c(client, &tas2764_i2c_regmap);
649 if (IS_ERR(tas2764->regmap)) {
650 result = PTR_ERR(tas2764->regmap);
651 dev_err(&client->dev, "Failed to allocate register map: %d\n",
656 if (client->dev.of_node) {
657 result = tas2764_parse_dt(&client->dev, tas2764);
659 dev_err(tas2764->dev, "%s: Failed to parse devicetree\n",
665 return devm_snd_soc_register_component(tas2764->dev,
666 &soc_component_driver_tas2764,
668 ARRAY_SIZE(tas2764_dai_driver));
671 static const struct i2c_device_id tas2764_i2c_id[] = {
675 MODULE_DEVICE_TABLE(i2c, tas2764_i2c_id);
677 #if defined(CONFIG_OF)
678 static const struct of_device_id tas2764_of_match[] = {
679 { .compatible = "ti,tas2764" },
682 MODULE_DEVICE_TABLE(of, tas2764_of_match);
685 static struct i2c_driver tas2764_i2c_driver = {
688 .of_match_table = of_match_ptr(tas2764_of_match),
690 .probe_new = tas2764_i2c_probe,
691 .id_table = tas2764_i2c_id,
693 module_i2c_driver(tas2764_i2c_driver);
695 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
696 MODULE_DESCRIPTION("TAS2764 I2C Smart Amplifier driver");
697 MODULE_LICENSE("GPL v2");