1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for the Texas Instruments TAS2562 CODEC
4 // Copyright (C) 2019 Texas Instruments Inc.
7 #include <linux/module.h>
8 #include <linux/errno.h>
9 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/regmap.h>
13 #include <linux/slab.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/delay.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
22 #include <sound/tlv.h>
26 #define TAS2562_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |\
27 SNDRV_PCM_FORMAT_S32_LE)
30 struct snd_soc_component *component;
31 struct gpio_desc *sdz_gpio;
32 struct regmap *regmap;
34 struct i2c_client *client;
39 static int tas2562_set_bias_level(struct snd_soc_component *component,
40 enum snd_soc_bias_level level)
42 struct tas2562_data *tas2562 =
43 snd_soc_component_get_drvdata(component);
47 snd_soc_component_update_bits(component,
49 TAS2562_MODE_MASK, TAS2562_ACTIVE);
51 case SND_SOC_BIAS_STANDBY:
52 case SND_SOC_BIAS_PREPARE:
53 snd_soc_component_update_bits(component,
55 TAS2562_MODE_MASK, TAS2562_MUTE);
57 case SND_SOC_BIAS_OFF:
58 snd_soc_component_update_bits(component,
60 TAS2562_MODE_MASK, TAS2562_SHUTDOWN);
65 "wrong power level setting %d\n", level);
72 static int tas2562_set_samplerate(struct tas2562_data *tas2562, int samplerate)
79 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
80 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
84 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_7305_8KHZ;
87 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
88 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
92 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_14_7_16KHZ;
95 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
96 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
100 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_22_05_24KHZ;
103 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
104 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
108 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_29_4_32KHZ;
111 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
112 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
116 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_44_1_48KHZ;
119 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
120 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
124 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_88_2_96KHZ;
127 ramp_rate = TAS2562_TDM_CFG0_RAMPRATE_44_1;
128 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
132 samp_rate = TAS2562_TDM_CFG0_SAMPRATE_176_4_192KHZ;
135 dev_info(tas2562->dev, "%s, unsupported sample rate, %d\n",
136 __func__, samplerate);
140 snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
141 TAS2562_TDM_CFG0_RAMPRATE_MASK, ramp_rate);
142 snd_soc_component_update_bits(tas2562->component, TAS2562_TDM_CFG0,
143 TAS2562_TDM_CFG0_SAMPRATE_MASK, samp_rate);
148 static int tas2562_set_dai_tdm_slot(struct snd_soc_dai *dai,
149 unsigned int tx_mask, unsigned int rx_mask,
150 int slots, int slot_width)
152 struct snd_soc_component *component = dai->component;
153 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
156 switch (slot_width) {
158 ret = snd_soc_component_update_bits(component,
160 TAS2562_TDM_CFG2_RXLEN_MASK,
161 TAS2562_TDM_CFG2_RXLEN_16B);
164 ret = snd_soc_component_update_bits(component,
166 TAS2562_TDM_CFG2_RXLEN_MASK,
167 TAS2562_TDM_CFG2_RXLEN_24B);
170 ret = snd_soc_component_update_bits(component,
172 TAS2562_TDM_CFG2_RXLEN_MASK,
173 TAS2562_TDM_CFG2_RXLEN_32B);
177 /* Do not change slot width */
180 dev_err(tas2562->dev, "slot width not supported");
190 static int tas2562_set_bitwidth(struct tas2562_data *tas2562, int bitwidth)
195 case SNDRV_PCM_FORMAT_S16_LE:
196 snd_soc_component_update_bits(tas2562->component,
198 TAS2562_TDM_CFG2_RXWLEN_MASK,
199 TAS2562_TDM_CFG2_RXWLEN_16B);
200 tas2562->v_sense_slot = tas2562->i_sense_slot + 2;
202 case SNDRV_PCM_FORMAT_S24_LE:
203 snd_soc_component_update_bits(tas2562->component,
205 TAS2562_TDM_CFG2_RXWLEN_MASK,
206 TAS2562_TDM_CFG2_RXWLEN_24B);
207 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
209 case SNDRV_PCM_FORMAT_S32_LE:
210 snd_soc_component_update_bits(tas2562->component,
212 TAS2562_TDM_CFG2_RXWLEN_MASK,
213 TAS2562_TDM_CFG2_RXWLEN_32B);
214 tas2562->v_sense_slot = tas2562->i_sense_slot + 4;
218 dev_info(tas2562->dev, "Unsupported bitwidth format\n");
222 ret = snd_soc_component_update_bits(tas2562->component,
224 TAS2562_TDM_CFG5_VSNS_EN | TAS2562_TDM_CFG5_VSNS_SLOT_MASK,
225 TAS2562_TDM_CFG5_VSNS_EN | tas2562->v_sense_slot);
229 ret = snd_soc_component_update_bits(tas2562->component,
231 TAS2562_TDM_CFG6_ISNS_EN | TAS2562_TDM_CFG6_ISNS_SLOT_MASK,
232 TAS2562_TDM_CFG6_ISNS_EN | tas2562->i_sense_slot);
239 static int tas2562_hw_params(struct snd_pcm_substream *substream,
240 struct snd_pcm_hw_params *params,
241 struct snd_soc_dai *dai)
243 struct snd_soc_component *component = dai->component;
244 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
247 ret = tas2562_set_bitwidth(tas2562, params_format(params));
249 dev_err(tas2562->dev, "set bitwidth failed, %d\n", ret);
253 ret = tas2562_set_samplerate(tas2562, params_rate(params));
255 dev_err(tas2562->dev, "set sample rate failed, %d\n", ret);
260 static int tas2562_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
262 struct snd_soc_component *component = dai->component;
263 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
264 u8 tdm_rx_start_slot = 0, asi_cfg_1 = 0;
267 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
268 case SND_SOC_DAIFMT_NB_NF:
271 case SND_SOC_DAIFMT_IB_NF:
272 asi_cfg_1 |= TAS2562_TDM_CFG1_RX_FALLING;
275 dev_err(tas2562->dev, "ASI format Inverse is not found\n");
279 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
280 TAS2562_TDM_CFG1_RX_EDGE_MASK,
283 dev_err(tas2562->dev, "Failed to set RX edge\n");
287 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
288 case (SND_SOC_DAIFMT_I2S):
289 case (SND_SOC_DAIFMT_DSP_A):
290 case (SND_SOC_DAIFMT_DSP_B):
291 tdm_rx_start_slot = BIT(1);
293 case (SND_SOC_DAIFMT_LEFT_J):
294 tdm_rx_start_slot = 0;
297 dev_err(tas2562->dev, "DAI Format is not found, fmt=0x%x\n",
303 ret = snd_soc_component_update_bits(component, TAS2562_TDM_CFG1,
304 TAS2562_TDM_CFG1_RX_OFFSET_MASK,
313 static int tas2562_mute(struct snd_soc_dai *dai, int mute)
315 struct snd_soc_component *component = dai->component;
317 return snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
319 mute ? TAS2562_MUTE : 0);
322 static int tas2562_codec_probe(struct snd_soc_component *component)
324 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
327 tas2562->component = component;
329 if (tas2562->sdz_gpio)
330 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
332 ret = snd_soc_component_update_bits(component, TAS2562_PWR_CTRL,
333 TAS2562_MODE_MASK, TAS2562_MUTE);
341 static int tas2562_suspend(struct snd_soc_component *component)
343 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
345 regcache_cache_only(tas2562->regmap, true);
346 regcache_mark_dirty(tas2562->regmap);
348 if (tas2562->sdz_gpio)
349 gpiod_set_value_cansleep(tas2562->sdz_gpio, 0);
354 static int tas2562_resume(struct snd_soc_component *component)
356 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
358 if (tas2562->sdz_gpio)
359 gpiod_set_value_cansleep(tas2562->sdz_gpio, 1);
361 regcache_cache_only(tas2562->regmap, false);
363 return regcache_sync(tas2562->regmap);
366 #define tas2562_suspend NULL
367 #define tas2562_resume NULL
370 static const char * const tas2562_ASI1_src[] = {
371 "I2C offset", "Left", "Right", "LeftRightDiv2",
374 static SOC_ENUM_SINGLE_DECL(tas2562_ASI1_src_enum, TAS2562_TDM_CFG2, 4,
377 static const struct snd_kcontrol_new tas2562_asi1_mux =
378 SOC_DAPM_ENUM("ASI1 Source", tas2562_ASI1_src_enum);
380 static int tas2562_dac_event(struct snd_soc_dapm_widget *w,
381 struct snd_kcontrol *kcontrol, int event)
383 struct snd_soc_component *component =
384 snd_soc_dapm_to_component(w->dapm);
385 struct tas2562_data *tas2562 = snd_soc_component_get_drvdata(component);
388 case SND_SOC_DAPM_POST_PMU:
389 dev_info(tas2562->dev, "SND_SOC_DAPM_POST_PMU\n");
391 case SND_SOC_DAPM_PRE_PMD:
392 dev_info(tas2562->dev, "SND_SOC_DAPM_PRE_PMD\n");
401 static DECLARE_TLV_DB_SCALE(tas2562_dac_tlv, 850, 50, 0);
403 static const struct snd_kcontrol_new isense_switch =
404 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_ISENSE_POWER_EN,
407 static const struct snd_kcontrol_new vsense_switch =
408 SOC_DAPM_SINGLE("Switch", TAS2562_PWR_CTRL, TAS2562_VSENSE_POWER_EN,
411 static const struct snd_kcontrol_new tas2562_snd_controls[] = {
412 SOC_SINGLE_TLV("Amp Gain Volume", TAS2562_PB_CFG1, 1, 0x1c, 0,
416 static const struct snd_soc_dapm_widget tas2562_dapm_widgets[] = {
417 SND_SOC_DAPM_AIF_IN("ASI1", "ASI1 Playback", 0, SND_SOC_NOPM, 0, 0),
418 SND_SOC_DAPM_MUX("ASI1 Sel", SND_SOC_NOPM, 0, 0, &tas2562_asi1_mux),
419 SND_SOC_DAPM_AIF_IN("DAC IN", "Playback", 0, SND_SOC_NOPM, 0, 0),
420 SND_SOC_DAPM_DAC_E("DAC", NULL, SND_SOC_NOPM, 0, 0, tas2562_dac_event,
421 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
422 SND_SOC_DAPM_SWITCH("ISENSE", TAS2562_PWR_CTRL, 3, 1, &isense_switch),
423 SND_SOC_DAPM_SWITCH("VSENSE", TAS2562_PWR_CTRL, 2, 1, &vsense_switch),
424 SND_SOC_DAPM_SIGGEN("VMON"),
425 SND_SOC_DAPM_SIGGEN("IMON"),
426 SND_SOC_DAPM_OUTPUT("OUT"),
429 static const struct snd_soc_dapm_route tas2562_audio_map[] = {
430 {"ASI1 Sel", "I2C offset", "ASI1"},
431 {"ASI1 Sel", "Left", "ASI1"},
432 {"ASI1 Sel", "Right", "ASI1"},
433 {"ASI1 Sel", "LeftRightDiv2", "ASI1"},
434 { "DAC", NULL, "DAC IN" },
435 { "OUT", NULL, "DAC" },
436 {"ISENSE", "Switch", "IMON"},
437 {"VSENSE", "Switch", "VMON"},
440 static const struct snd_soc_component_driver soc_component_dev_tas2562 = {
441 .probe = tas2562_codec_probe,
442 .suspend = tas2562_suspend,
443 .resume = tas2562_resume,
444 .set_bias_level = tas2562_set_bias_level,
445 .controls = tas2562_snd_controls,
446 .num_controls = ARRAY_SIZE(tas2562_snd_controls),
447 .dapm_widgets = tas2562_dapm_widgets,
448 .num_dapm_widgets = ARRAY_SIZE(tas2562_dapm_widgets),
449 .dapm_routes = tas2562_audio_map,
450 .num_dapm_routes = ARRAY_SIZE(tas2562_audio_map),
452 .use_pmdown_time = 1,
454 .non_legacy_dai_naming = 1,
457 static const struct snd_soc_dai_ops tas2562_speaker_dai_ops = {
458 .hw_params = tas2562_hw_params,
459 .set_fmt = tas2562_set_dai_fmt,
460 .set_tdm_slot = tas2562_set_dai_tdm_slot,
461 .digital_mute = tas2562_mute,
464 static struct snd_soc_dai_driver tas2562_dai[] = {
466 .name = "tas2562-amplifier",
469 .stream_name = "ASI1 Playback",
472 .rates = SNDRV_PCM_RATE_8000_192000,
473 .formats = TAS2562_FORMATS,
475 .ops = &tas2562_speaker_dai_ops,
479 static const struct regmap_range_cfg tas2562_ranges[] = {
482 .range_max = 5 * 128,
483 .selector_reg = TAS2562_PAGE_CTRL,
484 .selector_mask = 0xff,
491 static const struct reg_default tas2562_reg_defaults[] = {
492 { TAS2562_PAGE_CTRL, 0x00 },
493 { TAS2562_SW_RESET, 0x00 },
494 { TAS2562_PWR_CTRL, 0x0e },
495 { TAS2562_PB_CFG1, 0x20 },
496 { TAS2562_TDM_CFG0, 0x09 },
497 { TAS2562_TDM_CFG1, 0x02 },
500 static const struct regmap_config tas2562_regmap_config = {
504 .max_register = 5 * 128,
505 .cache_type = REGCACHE_RBTREE,
506 .reg_defaults = tas2562_reg_defaults,
507 .num_reg_defaults = ARRAY_SIZE(tas2562_reg_defaults),
508 .ranges = tas2562_ranges,
509 .num_ranges = ARRAY_SIZE(tas2562_ranges),
512 static int tas2562_parse_dt(struct tas2562_data *tas2562)
514 struct device *dev = tas2562->dev;
517 tas2562->sdz_gpio = devm_gpiod_get_optional(dev, "shut-down-gpio",
519 if (IS_ERR(tas2562->sdz_gpio)) {
520 if (PTR_ERR(tas2562->sdz_gpio) == -EPROBE_DEFER) {
521 tas2562->sdz_gpio = NULL;
522 return -EPROBE_DEFER;
526 ret = fwnode_property_read_u32(dev->fwnode, "ti,imon-slot-no",
527 &tas2562->i_sense_slot);
529 dev_err(dev, "Looking up %s property failed %d\n",
530 "ti,imon-slot-no", ret);
535 static int tas2562_probe(struct i2c_client *client,
536 const struct i2c_device_id *id)
538 struct device *dev = &client->dev;
539 struct tas2562_data *data;
542 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
546 data->client = client;
547 data->dev = &client->dev;
549 tas2562_parse_dt(data);
551 data->regmap = devm_regmap_init_i2c(client, &tas2562_regmap_config);
552 if (IS_ERR(data->regmap)) {
553 ret = PTR_ERR(data->regmap);
554 dev_err(dev, "failed to allocate register map: %d\n", ret);
558 dev_set_drvdata(&client->dev, data);
560 return devm_snd_soc_register_component(dev, &soc_component_dev_tas2562,
562 ARRAY_SIZE(tas2562_dai));
566 static const struct i2c_device_id tas2562_id[] = {
570 MODULE_DEVICE_TABLE(i2c, tas2562_id);
572 static const struct of_device_id tas2562_of_match[] = {
573 { .compatible = "ti,tas2562", },
576 MODULE_DEVICE_TABLE(of, tas2562_of_match);
578 static struct i2c_driver tas2562_i2c_driver = {
581 .of_match_table = of_match_ptr(tas2562_of_match),
583 .probe = tas2562_probe,
584 .id_table = tas2562_id,
587 module_i2c_driver(tas2562_i2c_driver);
589 MODULE_AUTHOR("Dan Murphy <dmurphy@ti.com>");
590 MODULE_DESCRIPTION("TAS2562 Audio amplifier driver");
591 MODULE_LICENSE("GPL");