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);
54 static int tas2764_codec_suspend(struct snd_soc_component *component)
56 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
59 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
60 TAS2764_PWR_CTRL_MASK,
61 TAS2764_PWR_CTRL_SHUTDOWN);
66 if (tas2764->sdz_gpio)
67 gpiod_set_value_cansleep(tas2764->sdz_gpio, 0);
69 regcache_cache_only(tas2764->regmap, true);
70 regcache_mark_dirty(tas2764->regmap);
75 static int tas2764_codec_resume(struct snd_soc_component *component)
77 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
80 if (tas2764->sdz_gpio) {
81 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
82 usleep_range(1000, 2000);
85 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
86 TAS2764_PWR_CTRL_MASK,
87 TAS2764_PWR_CTRL_ACTIVE);
92 regcache_cache_only(tas2764->regmap, false);
94 return regcache_sync(tas2764->regmap);
97 #define tas2764_codec_suspend NULL
98 #define tas2764_codec_resume NULL
101 static const char * const tas2764_ASI1_src[] = {
102 "I2C offset", "Left", "Right", "LeftRightDiv2",
105 static SOC_ENUM_SINGLE_DECL(
106 tas2764_ASI1_src_enum, TAS2764_TDM_CFG2, TAS2764_TDM_CFG2_SCFG_SHIFT,
109 static const struct snd_kcontrol_new tas2764_asi1_mux =
110 SOC_DAPM_ENUM("ASI1 Source", tas2764_ASI1_src_enum);
112 static int tas2764_dac_event(struct snd_soc_dapm_widget *w,
113 struct snd_kcontrol *kcontrol, int event)
115 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
116 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
120 case SND_SOC_DAPM_POST_PMU:
121 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
122 TAS2764_PWR_CTRL_MASK,
123 TAS2764_PWR_CTRL_MUTE);
125 case SND_SOC_DAPM_PRE_PMD:
126 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
127 TAS2764_PWR_CTRL_MASK,
128 TAS2764_PWR_CTRL_SHUTDOWN);
131 dev_err(tas2764->dev, "Unsupported event\n");
141 static const struct snd_kcontrol_new isense_switch =
142 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN, 1, 1);
143 static const struct snd_kcontrol_new vsense_switch =
144 SOC_DAPM_SINGLE("Switch", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN, 1, 1);
146 static const struct snd_soc_dapm_widget tas2764_dapm_widgets[] = {
147 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
148 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2764_asi1_mux),
149 SND_SOC_DAPM_SWITCH("ISENSE", TAS2764_PWR_CTRL, TAS2764_ISENSE_POWER_EN,
151 SND_SOC_DAPM_SWITCH("VSENSE", TAS2764_PWR_CTRL, TAS2764_VSENSE_POWER_EN,
153 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2764_dac_event,
154 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
155 SND_SOC_DAPM_OUTPUT("OUT"),
156 SND_SOC_DAPM_SIGGEN("VMON"),
157 SND_SOC_DAPM_SIGGEN("IMON")
160 static const struct snd_soc_dapm_route tas2764_audio_map[] = {
161 {"ASI1 Sel", "I2C offset", "ASI1"},
162 {"ASI1 Sel", "Left", "ASI1"},
163 {"ASI1 Sel", "Right", "ASI1"},
164 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
165 {"DAC", NULL, "ASI1 Sel"},
166 {"OUT", NULL, "DAC"},
167 {"ISENSE", "Switch", "IMON"},
168 {"VSENSE", "Switch", "VMON"},
171 static int tas2764_mute(struct snd_soc_dai *dai, int mute, int direction)
173 struct snd_soc_component *component = dai->component;
176 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
177 TAS2764_PWR_CTRL_MASK,
178 mute ? TAS2764_PWR_CTRL_MUTE : 0);
186 static int tas2764_set_bitwidth(struct tas2764_priv *tas2764, int bitwidth)
188 struct snd_soc_component *component = tas2764->component;
194 case SNDRV_PCM_FORMAT_S16_LE:
195 ret = snd_soc_component_update_bits(component,
197 TAS2764_TDM_CFG2_RXW_MASK,
198 TAS2764_TDM_CFG2_RXW_16BITS);
200 case SNDRV_PCM_FORMAT_S24_LE:
201 ret = snd_soc_component_update_bits(component,
203 TAS2764_TDM_CFG2_RXW_MASK,
204 TAS2764_TDM_CFG2_RXW_24BITS);
206 case SNDRV_PCM_FORMAT_S32_LE:
207 ret = snd_soc_component_update_bits(component,
209 TAS2764_TDM_CFG2_RXW_MASK,
210 TAS2764_TDM_CFG2_RXW_32BITS);
220 val = snd_soc_component_read(tas2764->component, TAS2764_PWR_CTRL);
224 if (val & (1 << TAS2764_VSENSE_POWER_EN))
227 sense_en = TAS2764_TDM_CFG5_VSNS_ENABLE;
229 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
230 TAS2764_TDM_CFG5_VSNS_ENABLE,
235 if (val & (1 << TAS2764_ISENSE_POWER_EN))
238 sense_en = TAS2764_TDM_CFG6_ISNS_ENABLE;
240 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
241 TAS2764_TDM_CFG6_ISNS_ENABLE,
249 static int tas2764_set_samplerate(struct tas2764_priv *tas2764, int samplerate)
251 struct snd_soc_component *component = tas2764->component;
255 switch (samplerate) {
257 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
258 TAS2764_TDM_CFG0_44_1_48KHZ;
261 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
262 TAS2764_TDM_CFG0_44_1_48KHZ;
265 ramp_rate_val = TAS2764_TDM_CFG0_SMP_48KHZ |
266 TAS2764_TDM_CFG0_88_2_96KHZ;
269 ramp_rate_val = TAS2764_TDM_CFG0_SMP_44_1KHZ |
270 TAS2764_TDM_CFG0_88_2_96KHZ;
276 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
277 TAS2764_TDM_CFG0_SMP_MASK |
278 TAS2764_TDM_CFG0_MASK,
286 static int tas2764_hw_params(struct snd_pcm_substream *substream,
287 struct snd_pcm_hw_params *params,
288 struct snd_soc_dai *dai)
290 struct snd_soc_component *component = dai->component;
291 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
294 ret = tas2764_set_bitwidth(tas2764, params_format(params));
298 return tas2764_set_samplerate(tas2764, params_rate(params));
301 static int tas2764_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
303 struct snd_soc_component *component = dai->component;
304 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
305 u8 tdm_rx_start_slot = 0, asi_cfg_0 = 0, asi_cfg_1 = 0;
308 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
309 case SND_SOC_DAIFMT_NB_IF:
310 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
312 case SND_SOC_DAIFMT_NB_NF:
313 asi_cfg_1 = TAS2764_TDM_CFG1_RX_RISING;
315 case SND_SOC_DAIFMT_IB_IF:
316 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
318 case SND_SOC_DAIFMT_IB_NF:
319 asi_cfg_1 = TAS2764_TDM_CFG1_RX_FALLING;
323 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
324 TAS2764_TDM_CFG1_RX_MASK,
329 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
330 case SND_SOC_DAIFMT_I2S:
331 asi_cfg_0 ^= TAS2764_TDM_CFG0_FRAME_START;
333 case SND_SOC_DAIFMT_DSP_A:
334 tdm_rx_start_slot = 1;
336 case SND_SOC_DAIFMT_DSP_B:
337 case SND_SOC_DAIFMT_LEFT_J:
338 tdm_rx_start_slot = 0;
341 dev_err(tas2764->dev,
342 "DAI Format is not found, fmt=0x%x\n", fmt);
346 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG0,
347 TAS2764_TDM_CFG0_FRAME_START,
352 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG1,
353 TAS2764_TDM_CFG1_MASK,
354 (tdm_rx_start_slot << TAS2764_TDM_CFG1_51_SHIFT));
361 static int tas2764_set_dai_tdm_slot(struct snd_soc_dai *dai,
362 unsigned int tx_mask,
363 unsigned int rx_mask,
364 int slots, int slot_width)
366 struct snd_soc_component *component = dai->component;
367 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
368 int left_slot, right_slot;
373 if (tx_mask == 0 || rx_mask != 0)
382 left_slot = __ffs(tx_mask);
383 tx_mask &= ~(1 << left_slot);
385 right_slot = left_slot;
387 right_slot = __ffs(tx_mask);
388 tx_mask &= ~(1 << right_slot);
392 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
395 slots_cfg = (right_slot << TAS2764_TDM_CFG3_RXS_SHIFT) | left_slot;
397 ret = snd_soc_component_write(component, TAS2764_TDM_CFG3, slots_cfg);
401 switch (slot_width) {
403 slot_size = TAS2764_TDM_CFG2_RXS_16BITS;
406 slot_size = TAS2764_TDM_CFG2_RXS_24BITS;
409 slot_size = TAS2764_TDM_CFG2_RXS_32BITS;
415 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG2,
416 TAS2764_TDM_CFG2_RXS_MASK,
421 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG5,
422 TAS2764_TDM_CFG5_50_MASK,
423 tas2764->v_sense_slot);
427 ret = snd_soc_component_update_bits(component, TAS2764_TDM_CFG6,
428 TAS2764_TDM_CFG6_50_MASK,
429 tas2764->i_sense_slot);
436 static const struct snd_soc_dai_ops tas2764_dai_ops = {
437 .mute_stream = tas2764_mute,
438 .hw_params = tas2764_hw_params,
439 .set_fmt = tas2764_set_fmt,
440 .set_tdm_slot = tas2764_set_dai_tdm_slot,
441 .no_capture_mute = 1,
444 #define TAS2764_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
445 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
447 #define TAS2764_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
448 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200)
450 static struct snd_soc_dai_driver tas2764_dai_driver[] = {
452 .name = "tas2764 ASI1",
455 .stream_name = "ASI1 Playback",
458 .rates = TAS2764_RATES,
459 .formats = TAS2764_FORMATS,
462 .stream_name = "ASI1 Capture",
465 .rates = TAS2764_RATES,
466 .formats = TAS2764_FORMATS,
468 .ops = &tas2764_dai_ops,
473 static int tas2764_codec_probe(struct snd_soc_component *component)
475 struct tas2764_priv *tas2764 = snd_soc_component_get_drvdata(component);
478 tas2764->component = component;
480 if (tas2764->sdz_gpio) {
481 gpiod_set_value_cansleep(tas2764->sdz_gpio, 1);
482 usleep_range(1000, 2000);
485 tas2764_reset(tas2764);
487 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG5,
488 TAS2764_TDM_CFG5_VSNS_ENABLE, 0);
492 ret = snd_soc_component_update_bits(tas2764->component, TAS2764_TDM_CFG6,
493 TAS2764_TDM_CFG6_ISNS_ENABLE, 0);
497 ret = snd_soc_component_update_bits(component, TAS2764_PWR_CTRL,
498 TAS2764_PWR_CTRL_MASK,
499 TAS2764_PWR_CTRL_MUTE);
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");