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;
42 static void tas2764_reset(struct tas2764_priv *tas2764)
44 if (tas2764->reset_gpio) {
45 gpiod_set_value_cansleep(tas2764->reset_gpio, 0);
47 gpiod_set_value_cansleep(tas2764->reset_gpio, 1);
48 usleep_range(1000, 2000);
51 snd_soc_component_write(tas2764->component, TAS2764_SW_RST,
53 usleep_range(1000, 2000);
56 static int tas2764_update_pwr_ctrl(struct tas2764_priv *tas2764)
58 struct snd_soc_component *component = tas2764->component;
62 if (tas2764->dac_powered)
63 val = tas2764->unmuted ?
64 TAS2764_PWR_CTRL_ACTIVE : TAS2764_PWR_CTRL_MUTE;
66 val = TAS2764_PWR_CTRL_SHUTDOWN;
68 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
69 TAS2764_PWR_CTRL_MASK, val);
77 static int tas2764_codec_suspend(struct snd_soc_component *component)
79 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
82 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
83 TAS2764_PWR_CTRL_MASK,
84 TAS2764_PWR_CTRL_SHUTDOWN);
89 if (tas2764->sdz_gpio)
90 gpiod_set_value_cansleep(tas2764->sdz_gpio, 0);
92 regcache_cache_only(tas2764->regmap, true);
93 regcache_mark_dirty(tas2764->regmap);
98 static int tas2764_codec_resume(struct snd_soc_component *component)
100 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
103 if (tas2764->sdz_gpio) {
104 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
105 usleep_range(1000, 2000);
108 ret = tas2764_update_pwr_ctrl(tas2764);
113 regcache_cache_only(tas2764->regmap, false);
115 return regcache_sync(tas2764->regmap);
118 #define tas2764_codec_suspend NULL
119 #define tas2764_codec_resume NULL
122 static const char * const tas2764_ASI1_src[] = {
123 "I2C offset", "Left", "Right", "LeftRightDiv2",
126 static SOC_ENUM_SINGLE_DECL(
127 tas2764_ASI1_src_enum, TAS2764_TDM_CFG2, TAS2764_TDM_CFG2_SCFG_SHIFT,
130 static const struct snd_kcontrol_new tas2764_asi1_mux =
131 SOC_DAPM_ENUM("ASI1 Source", tas2764_ASI1_src_enum);
133 static int tas2764_dac_event(struct snd_soc_dapm_widget *w,
134 struct snd_kcontrol *kcontrol, int event)
136 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
137 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
141 case SND_SOC_DAPM_POST_PMU:
142 tas2764->dac_powered = true;
143 ret = tas2764_update_pwr_ctrl(tas2764);
145 case SND_SOC_DAPM_PRE_PMD:
146 tas2764->dac_powered = false;
147 ret = tas2764_update_pwr_ctrl(tas2764);
150 dev_err(tas2764->dev, "Unsupported event\n");
160 static const struct snd_kcontrol_new isense_switch =
161 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN, 1, 1);
162 static const struct snd_kcontrol_new vsense_switch =
163 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN, 1, 1);
165 static const struct snd_soc_dapm_widget tas2764_dapm_widgets[] = {
166 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
167 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2764_asi1_mux),
168 SND_SOC_DAPM_SWITCH("ISENSE", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN,
170 SND_SOC_DAPM_SWITCH("VSENSE", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN,
172 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2764_dac_event,
173 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
174 SND_SOC_DAPM_OUTPUT("OUT"),
175 SND_SOC_DAPM_SIGGEN("VMON"),
176 SND_SOC_DAPM_SIGGEN("IMON")
179 static const struct snd_soc_dapm_route tas2764_audio_map[] = {
180 {"ASI1 Sel", "I2C offset", "ASI1"},
181 {"ASI1 Sel", "Left", "ASI1"},
182 {"ASI1 Sel", "Right", "ASI1"},
183 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
184 {"DAC", NULL, "ASI1 Sel"},
185 {"OUT", NULL, "DAC"},
186 {"ISENSE", "Switch", "IMON"},
187 {"VSENSE", "Switch", "VMON"},
190 static int tas2764_mute(struct snd_soc_dai *dai, int mute, int direction)
192 struct tas2764_priv *tas2764 =
193 snd_soc_component_get_drvdata(dai->component);
195 tas2764->unmuted = !mute;
196 return tas2764_update_pwr_ctrl(tas2764);
199 static int tas2764_set_bitwidth(struct tas2764_priv *tas2764, int bitwidth)
201 struct snd_soc_component *component = tas2764->component;
207 case SNDRV_PCM_FORMAT_S16_LE:
208 ret = snd_soc_component_update_bits(component,
210 TAS2764_TDM_CFG2_RXW_MASK,
211 TAS2764_TDM_CFG2_RXW_16BITS);
213 case SNDRV_PCM_FORMAT_S24_LE:
214 ret = snd_soc_component_update_bits(component,
216 TAS2764_TDM_CFG2_RXW_MASK,
217 TAS2764_TDM_CFG2_RXW_24BITS);
219 case SNDRV_PCM_FORMAT_S32_LE:
220 ret = snd_soc_component_update_bits(component,
222 TAS2764_TDM_CFG2_RXW_MASK,
223 TAS2764_TDM_CFG2_RXW_32BITS);
233 val = snd_soc_component_read(tas2764->component, TAS2764_PWR_CTRL);
237 if (val & (1 << TAS2764_VSENSE_POWER_EN))
240 sense_en = TAS2764_TDM_CFG5_VSNS_ENABLE;
242 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
243 TAS2764_TDM_CFG5_VSNS_ENABLE,
248 if (val & (1 << TAS2764_ISENSE_POWER_EN))
251 sense_en = TAS2764_TDM_CFG6_ISNS_ENABLE;
253 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
254 TAS2764_TDM_CFG6_ISNS_ENABLE,
262 static int tas2764_set_samplerate(struct tas2764_priv *tas2764, int samplerate)
264 struct snd_soc_component *component = tas2764->component;
268 switch (samplerate) {
270 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
271 TAS2764_TDM_CFG0_44_1_48KHZ;
274 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
275 TAS2764_TDM_CFG0_44_1_48KHZ;
278 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
279 TAS2764_TDM_CFG0_88_2_96KHZ;
282 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
283 TAS2764_TDM_CFG0_88_2_96KHZ;
289 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
290 TAS2764_TDM_CFG0_SMP_MASK |
291 TAS2764_TDM_CFG0_MASK,
299 static int tas2764_hw_params(struct snd_pcm_substream *substream,
300 struct snd_pcm_hw_params *params,
301 struct snd_soc_dai *dai)
303 struct snd_soc_component *component = dai->component;
304 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
307 ret = tas2764_set_bitwidth(tas2764, params_format(params));
311 return tas2764_set_samplerate(tas2764, params_rate(params));
314 static int tas2764_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
316 struct snd_soc_component *component = dai->component;
317 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
318 u8 tdm_rx_start_slot = 0, asi_cfg_0 = 0, asi_cfg_1 = 0;
321 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
322 case SND_SOC_DAIFMT_NB_IF:
323 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
325 case SND_SOC_DAIFMT_NB_NF:
326 asi_cfg_1 = TAS2764_TDM_CFG1_RX_RISING;
328 case SND_SOC_DAIFMT_IB_IF:
329 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
331 case SND_SOC_DAIFMT_IB_NF:
332 asi_cfg_1 = TAS2764_TDM_CFG1_RX_FALLING;
336 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
337 TAS2764_TDM_CFG1_RX_MASK,
342 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
343 case SND_SOC_DAIFMT_I2S:
344 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
346 case SND_SOC_DAIFMT_DSP_A:
347 tdm_rx_start_slot = 1;
349 case SND_SOC_DAIFMT_DSP_B:
350 case SND_SOC_DAIFMT_LEFT_J:
351 tdm_rx_start_slot = 0;
354 dev_err(tas2764->dev,
355 "DAI Format is not found, fmt=0x%x\n", fmt);
359 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
360 TAS2764_TDM_CFG0_FRAME_START,
365 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
366 TAS2764_TDM_CFG1_MASK,
367 (tdm_rx_start_slot << TAS2764_TDM_CFG1_51_SHIFT));
374 static int tas2764_set_dai_tdm_slot(struct snd_soc_dai *dai,
375 unsigned int tx_mask,
376 unsigned int rx_mask,
377 int slots, int slot_width)
379 struct snd_soc_component *component = dai->component;
380 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
381 int left_slot, right_slot;
386 if (tx_mask == 0 || rx_mask != 0)
389 left_slot = __ffs(tx_mask);
390 tx_mask &= ~(1 << left_slot);
392 right_slot = left_slot;
394 right_slot = __ffs(tx_mask);
395 tx_mask &= ~(1 << right_slot);
398 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
401 slots_cfg = (right_slot << TAS2764_TDM_CFG3_RXS_SHIFT) | left_slot;
403 ret = snd_soc_component_write(component, TAS2764_TDM_CFG3, slots_cfg);
407 switch (slot_width) {
409 slot_size = TAS2764_TDM_CFG2_RXS_16BITS;
412 slot_size = TAS2764_TDM_CFG2_RXS_24BITS;
415 slot_size = TAS2764_TDM_CFG2_RXS_32BITS;
421 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG2,
422 TAS2764_TDM_CFG2_RXS_MASK,
427 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG5,
428 TAS2764_TDM_CFG5_50_MASK,
429 tas2764->v_sense_slot);
433 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG6,
434 TAS2764_TDM_CFG6_50_MASK,
435 tas2764->i_sense_slot);
442 static struct snd_soc_dai_ops tas2764_dai_ops = {
443 .mute_stream = tas2764_mute,
444 .hw_params = tas2764_hw_params,
445 .set_fmt = tas2764_set_fmt,
446 .set_tdm_slot = tas2764_set_dai_tdm_slot,
447 .no_capture_mute = 1,
450 #define TAS2764_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
451 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
453 #define TAS2764_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
454 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
456 static struct snd_soc_dai_driver tas2764_dai_driver[] = {
458 .name = "tas2764 ASI1",
461 .stream_name = "ASI1 Playback",
464 .rates = TAS2764_RATES,
465 .formats = TAS2764_FORMATS,
468 .stream_name = "ASI1 Capture",
471 .rates = TAS2764_RATES,
472 .formats = TAS2764_FORMATS,
474 .ops = &tas2764_dai_ops,
475 .symmetric_rates = 1,
479 static int tas2764_codec_probe(struct snd_soc_component *component)
481 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
484 tas2764->component = component;
486 if (tas2764->sdz_gpio) {
487 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
488 usleep_range(1000, 2000);
491 tas2764_reset(tas2764);
493 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
494 TAS2764_TDM_CFG5_VSNS_ENABLE, 0);
498 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
499 TAS2764_TDM_CFG6_ISNS_ENABLE, 0);
506 static DECLARE_TLV_DB_SCALE(tas2764_digital_tlv, 1100, 50, 0);
507 static DECLARE_TLV_DB_SCALE(tas2764_playback_volume, -10050, 50, 1);
509 static const struct snd_kcontrol_new tas2764_snd_controls[] = {
510 SOC_SINGLE_TLV("Speaker Volume", TAS2764_DVC, 0,
511 TAS2764_DVC_MAX, 1, tas2764_playback_volume),
512 SOC_SINGLE_TLV("Amp Gain Volume", TAS2764_CHNL_0, 1, 0x14, 0,
513 tas2764_digital_tlv),
516 static const struct snd_soc_component_driver soc_component_driver_tas2764 = {
517 .probe = tas2764_codec_probe,
518 .suspend = tas2764_codec_suspend,
519 .resume = tas2764_codec_resume,
520 .controls = tas2764_snd_controls,
521 .num_controls = ARRAY_SIZE(tas2764_snd_controls),
522 .dapm_widgets = tas2764_dapm_widgets,
523 .num_dapm_widgets = ARRAY_SIZE(tas2764_dapm_widgets),
524 .dapm_routes = tas2764_audio_map,
525 .num_dapm_routes = ARRAY_SIZE(tas2764_audio_map),
528 .non_legacy_dai_naming = 1,
531 static const struct reg_default tas2764_reg_defaults[] = {
532 { TAS2764_PAGE, 0x00 },
533 { TAS2764_SW_RST, 0x00 },
534 { TAS2764_PWR_CTRL, 0x1a },
535 { TAS2764_DVC, 0x00 },
536 { TAS2764_CHNL_0, 0x28 },
537 { TAS2764_TDM_CFG0, 0x09 },
538 { TAS2764_TDM_CFG1, 0x02 },
539 { TAS2764_TDM_CFG2, 0x0a },
540 { TAS2764_TDM_CFG3, 0x10 },
541 { TAS2764_TDM_CFG5, 0x42 },
544 static const struct regmap_range_cfg tas2764_regmap_ranges[] = {
547 .range_max = 1 * 128,
548 .selector_reg = TAS2764_PAGE,
549 .selector_mask = 0xff,
556 static const struct regmap_config tas2764_i2c_regmap = {
559 .reg_defaults = tas2764_reg_defaults,
560 .num_reg_defaults = ARRAY_SIZE(tas2764_reg_defaults),
561 .cache_type = REGCACHE_RBTREE,
562 .ranges = tas2764_regmap_ranges,
563 .num_ranges = ARRAY_SIZE(tas2764_regmap_ranges),
564 .max_register = 1 * 128,
567 static int tas2764_parse_dt(struct device *dev, struct tas2764_priv *tas2764)
571 tas2764->reset_gpio = devm_gpiod_get_optional(tas2764->dev, "reset",
573 if (IS_ERR(tas2764->reset_gpio)) {
574 if (PTR_ERR(tas2764->reset_gpio) == -EPROBE_DEFER) {
575 tas2764->reset_gpio = NULL;
576 return -EPROBE_DEFER;
580 tas2764->sdz_gpio = devm_gpiod_get_optional(dev, "shutdown", GPIOD_OUT_HIGH);
581 if (IS_ERR(tas2764->sdz_gpio)) {
582 if (PTR_ERR(tas2764->sdz_gpio) == -EPROBE_DEFER)
583 return -EPROBE_DEFER;
585 tas2764->sdz_gpio = NULL;
588 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
589 &tas2764->i_sense_slot);
591 tas2764->i_sense_slot = 0;
593 ret = fwnode_property_read_u32(dev->fwnode, "ti,vmon-slot-no",
594 &tas2764->v_sense_slot);
596 tas2764->v_sense_slot = 2;
601 static int tas2764_i2c_probe(struct i2c_client *client,
602 const struct i2c_device_id *id)
604 struct tas2764_priv *tas2764;
607 tas2764 = devm_kzalloc(&client->dev, sizeof(struct tas2764_priv),
612 tas2764->dev = &client->dev;
613 i2c_set_clientdata(client, tas2764);
614 dev_set_drvdata(&client->dev, tas2764);
616 tas2764->regmap = devm_regmap_init_i2c(client, &tas2764_i2c_regmap);
617 if (IS_ERR(tas2764->regmap)) {
618 result = PTR_ERR(tas2764->regmap);
619 dev_err(&client->dev, "Failed to allocate register map: %d\n",
624 if (client->dev.of_node) {
625 result = tas2764_parse_dt(&client->dev, tas2764);
627 dev_err(tas2764->dev, "%s: Failed to parse devicetree\n",
633 return devm_snd_soc_register_component(tas2764->dev,
634 &soc_component_driver_tas2764,
636 ARRAY_SIZE(tas2764_dai_driver));
639 static const struct i2c_device_id tas2764_i2c_id[] = {
643 MODULE_DEVICE_TABLE(i2c, tas2764_i2c_id);
645 #if defined(CONFIG_OF)
646 static const struct of_device_id tas2764_of_match[] = {
647 { .compatible = "ti,tas2764" },
650 MODULE_DEVICE_TABLE(of, tas2764_of_match);
653 static struct i2c_driver tas2764_i2c_driver = {
656 .of_match_table = of_match_ptr(tas2764_of_match),
658 .probe = tas2764_i2c_probe,
659 .id_table = tas2764_i2c_id,
661 module_i2c_driver(tas2764_i2c_driver);
663 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
664 MODULE_DESCRIPTION("TAS2764 I2C Smart Amplifier driver");
665 MODULE_LICENSE("GPL v2");