2 * tas2552.c - ALSA SoC Texas Instruments TAS2552 Mono Audio Amplifier
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Dan Murphy <dmurphy@ti.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/device.h>
21 #include <linux/i2c.h>
22 #include <linux/gpio.h>
23 #include <linux/of_gpio.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/regmap.h>
26 #include <linux/slab.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/regulator/consumer.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/tlv.h>
36 #include <sound/tas2552-plat.h>
37 #include <dt-bindings/sound/tas2552.h>
41 static struct reg_default tas2552_reg_defs[] = {
42 {TAS2552_CFG_1, 0x22},
43 {TAS2552_CFG_3, 0x80},
45 {TAS2552_OUTPUT_DATA, 0xc0},
46 {TAS2552_PDM_CFG, 0x01},
47 {TAS2552_PGA_GAIN, 0x00},
48 {TAS2552_BOOST_PT_CTRL, 0x0f},
49 {TAS2552_RESERVED_0D, 0x00},
50 {TAS2552_LIMIT_RATE_HYS, 0x08},
51 {TAS2552_CFG_2, 0xef},
52 {TAS2552_SER_CTRL_1, 0x00},
53 {TAS2552_SER_CTRL_2, 0x00},
54 {TAS2552_PLL_CTRL_1, 0x10},
55 {TAS2552_PLL_CTRL_2, 0x00},
56 {TAS2552_PLL_CTRL_3, 0x00},
58 {TAS2552_BTS_CTRL, 0x80},
59 {TAS2552_LIMIT_RELEASE, 0x04},
60 {TAS2552_LIMIT_INT_COUNT, 0x00},
61 {TAS2552_EDGE_RATE_CTRL, 0x40},
62 {TAS2552_VBAT_DATA, 0x00},
65 #define TAS2552_NUM_SUPPLIES 3
66 static const char *tas2552_supply_names[TAS2552_NUM_SUPPLIES] = {
67 "vbat", /* vbat voltage */
68 "iovdd", /* I/O Voltage */
69 "avdd", /* Analog DAC Voltage */
73 struct snd_soc_codec *codec;
74 struct regmap *regmap;
75 struct i2c_client *tas2552_client;
76 struct regulator_bulk_data supplies[TAS2552_NUM_SUPPLIES];
77 struct gpio_desc *enable_gpio;
78 unsigned char regs[TAS2552_VBAT_DATA];
79 unsigned int pll_clkin;
83 unsigned int tdm_delay;
86 /* Input mux controls */
87 static const char *tas2552_input_texts[] = {
91 static SOC_ENUM_SINGLE_DECL(tas2552_input_mux_enum, TAS2552_CFG_3, 7,
94 static const struct snd_kcontrol_new tas2552_input_mux_control[] = {
95 SOC_DAPM_ENUM("Input selection", tas2552_input_mux_enum)
98 static const struct snd_soc_dapm_widget tas2552_dapm_widgets[] =
100 SND_SOC_DAPM_INPUT("IN"),
103 SND_SOC_DAPM_MUX("Input selection", SND_SOC_NOPM, 0, 0,
104 tas2552_input_mux_control),
106 SND_SOC_DAPM_AIF_IN("DAC IN", "DAC Playback", 0, SND_SOC_NOPM, 0, 0),
107 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
108 SND_SOC_DAPM_OUT_DRV("ClassD", TAS2552_CFG_2, 7, 0, NULL, 0),
109 SND_SOC_DAPM_SUPPLY("PLL", TAS2552_CFG_2, 3, 0, NULL, 0),
111 SND_SOC_DAPM_OUTPUT("OUT")
114 static const struct snd_soc_dapm_route tas2552_audio_map[] = {
115 {"DAC", NULL, "DAC IN"},
116 {"Input selection", "Digital", "DAC"},
117 {"Input selection", "Analog", "IN"},
118 {"ClassD", NULL, "Input selection"},
119 {"OUT", NULL, "ClassD"},
120 {"ClassD", NULL, "PLL"},
124 static void tas2552_sw_shutdown(struct tas2552_data *tas_data, int sw_shutdown)
128 if (!tas_data->codec)
132 cfg1_reg = TAS2552_SWS;
134 snd_soc_update_bits(tas_data->codec, TAS2552_CFG_1, TAS2552_SWS,
139 static int tas2552_hw_params(struct snd_pcm_substream *substream,
140 struct snd_pcm_hw_params *params,
141 struct snd_soc_dai *dai)
143 struct snd_soc_codec *codec = dai->codec;
144 struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
145 int sample_rate, pll_clk;
149 if (!tas2552->pll_clkin)
152 snd_soc_update_bits(codec, TAS2552_CFG_2, TAS2552_PLL_ENABLE, 0);
154 if (tas2552->pll_clkin == TAS2552_245MHZ_CLK ||
155 tas2552->pll_clkin == TAS2552_225MHZ_CLK) {
156 /* By pass the PLL configuration */
157 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_2,
158 TAS2552_PLL_BYPASS_MASK,
161 /* Fill in the PLL control registers for J & D
162 * PLL_CLK = (.5 * freq * J.D) / 2^p
163 * Need to fill in J and D here based on incoming freq
165 p = snd_soc_read(codec, TAS2552_PLL_CTRL_1);
167 sample_rate = params_rate(params);
169 if (sample_rate == 48000)
170 pll_clk = TAS2552_245MHZ_CLK;
171 else if (sample_rate == 44100)
172 pll_clk = TAS2552_225MHZ_CLK;
174 dev_vdbg(codec->dev, "Substream sample rate is not found %i\n",
175 params_rate(params));
179 j = (pll_clk * 2 * (1 << p)) / tas2552->pll_clkin;
180 d = (pll_clk * 2 * (1 << p)) % tas2552->pll_clkin;
182 snd_soc_update_bits(codec, TAS2552_PLL_CTRL_1,
183 TAS2552_PLL_J_MASK, j);
184 snd_soc_write(codec, TAS2552_PLL_CTRL_2,
185 (d >> 7) & TAS2552_PLL_D_UPPER_MASK);
186 snd_soc_write(codec, TAS2552_PLL_CTRL_3,
187 d & TAS2552_PLL_D_LOWER_MASK);
194 #define TAS2552_DAI_FMT_MASK (TAS2552_BCLKDIR | \
196 TAS2552_DATAFORMAT_MASK)
197 static int tas2552_prepare(struct snd_pcm_substream *substream,
198 struct snd_soc_dai *dai)
200 struct snd_soc_codec *codec = dai->codec;
201 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
204 /* TDM slot selection only valid in DSP_A/_B mode */
205 if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_A)
206 delay += (tas2552->tdm_delay + 1);
207 else if (tas2552->dai_fmt == SND_SOC_DAIFMT_DSP_B)
208 delay += tas2552->tdm_delay;
210 /* Configure data delay */
211 snd_soc_write(codec, TAS2552_SER_CTRL_2, delay);
216 static int tas2552_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
218 struct snd_soc_codec *codec = dai->codec;
219 struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
222 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
223 case SND_SOC_DAIFMT_CBS_CFS:
224 serial_format = 0x00;
226 case SND_SOC_DAIFMT_CBS_CFM:
227 serial_format = TAS2552_WCLKDIR;
229 case SND_SOC_DAIFMT_CBM_CFS:
230 serial_format = TAS2552_BCLKDIR;
232 case SND_SOC_DAIFMT_CBM_CFM:
233 serial_format = (TAS2552_BCLKDIR | TAS2552_WCLKDIR);
236 dev_vdbg(codec->dev, "DAI Format master is not found\n");
240 switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
241 SND_SOC_DAIFMT_INV_MASK)) {
242 case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
244 case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
245 case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
246 serial_format |= TAS2552_DATAFORMAT_DSP;
248 case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
249 serial_format |= TAS2552_DATAFORMAT_RIGHT_J;
251 case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
252 serial_format |= TAS2552_DATAFORMAT_LEFT_J;
255 dev_vdbg(codec->dev, "DAI Format is not found\n");
258 tas2552->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
260 snd_soc_update_bits(codec, TAS2552_SER_CTRL_1, TAS2552_DAI_FMT_MASK,
265 static int tas2552_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id,
266 unsigned int freq, int dir)
268 struct snd_soc_codec *codec = dai->codec;
269 struct tas2552_data *tas2552 = dev_get_drvdata(codec->dev);
273 case TAS2552_PLL_CLKIN_MCLK:
274 case TAS2552_PLL_CLKIN_BCLK:
275 case TAS2552_PLL_CLKIN_IVCLKIN:
276 case TAS2552_PLL_CLKIN_1_8_FIXED:
277 mask = TAS2552_PLL_SRC_MASK;
278 val = (clk_id << 3) & mask; /* bit 4:5 in the register */
280 tas2552->pll_clkin = freq;
282 case TAS2552_PDM_CLK_PLL:
283 case TAS2552_PDM_CLK_IVCLKIN:
284 case TAS2552_PDM_CLK_BCLK:
285 case TAS2552_PDM_CLK_MCLK:
286 mask = TAS2552_PDM_CLK_SEL_MASK;
287 val = (clk_id >> 1) & mask; /* bit 0:1 in the register */
288 reg = TAS2552_PDM_CFG;
289 tas2552->pdm_clk = freq;
292 dev_err(codec->dev, "Invalid clk id: %d\n", clk_id);
296 snd_soc_update_bits(codec, reg, mask, val);
301 static int tas2552_set_dai_tdm_slot(struct snd_soc_dai *dai,
302 unsigned int tx_mask, unsigned int rx_mask,
303 int slots, int slot_width)
305 struct snd_soc_codec *codec = dai->codec;
306 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
309 if (unlikely(!tx_mask)) {
310 dev_err(codec->dev, "tx masks need to be non 0\n");
314 /* TDM based on DSP mode requires slots to be adjacent */
315 lsb = __ffs(tx_mask);
316 if ((lsb + 1) != __fls(tx_mask)) {
317 dev_err(codec->dev, "Invalid mask, slots must be adjacent\n");
321 tas2552->tdm_delay = lsb * slot_width;
323 /* DOUT in high-impedance on inactive bit clocks */
324 snd_soc_update_bits(codec, TAS2552_DOUT,
325 TAS2552_SDOUT_TRISTATE, TAS2552_SDOUT_TRISTATE);
330 static int tas2552_mute(struct snd_soc_dai *dai, int mute)
333 struct snd_soc_codec *codec = dai->codec;
336 cfg1_reg |= TAS2552_MUTE;
338 snd_soc_update_bits(codec, TAS2552_CFG_1, TAS2552_MUTE, cfg1_reg);
344 static int tas2552_runtime_suspend(struct device *dev)
346 struct tas2552_data *tas2552 = dev_get_drvdata(dev);
348 tas2552_sw_shutdown(tas2552, 1);
350 regcache_cache_only(tas2552->regmap, true);
351 regcache_mark_dirty(tas2552->regmap);
353 if (tas2552->enable_gpio)
354 gpiod_set_value(tas2552->enable_gpio, 0);
359 static int tas2552_runtime_resume(struct device *dev)
361 struct tas2552_data *tas2552 = dev_get_drvdata(dev);
363 if (tas2552->enable_gpio)
364 gpiod_set_value(tas2552->enable_gpio, 1);
366 tas2552_sw_shutdown(tas2552, 0);
368 regcache_cache_only(tas2552->regmap, false);
369 regcache_sync(tas2552->regmap);
375 static const struct dev_pm_ops tas2552_pm = {
376 SET_RUNTIME_PM_OPS(tas2552_runtime_suspend, tas2552_runtime_resume,
380 static struct snd_soc_dai_ops tas2552_speaker_dai_ops = {
381 .hw_params = tas2552_hw_params,
382 .prepare = tas2552_prepare,
383 .set_sysclk = tas2552_set_dai_sysclk,
384 .set_fmt = tas2552_set_dai_fmt,
385 .set_tdm_slot = tas2552_set_dai_tdm_slot,
386 .digital_mute = tas2552_mute,
389 /* Formats supported by TAS2552 driver. */
390 #define TAS2552_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
391 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
393 /* TAS2552 dai structure. */
394 static struct snd_soc_dai_driver tas2552_dai[] = {
396 .name = "tas2552-amplifier",
398 .stream_name = "Playback",
401 .rates = SNDRV_PCM_RATE_8000_192000,
402 .formats = TAS2552_FORMATS,
404 .ops = &tas2552_speaker_dai_ops,
409 * DAC digital volumes. From -7 to 24 dB in 1 dB steps
411 static DECLARE_TLV_DB_SCALE(dac_tlv, -7, 100, 24);
413 static const struct snd_kcontrol_new tas2552_snd_controls[] = {
414 SOC_SINGLE_TLV("Speaker Driver Playback Volume",
415 TAS2552_PGA_GAIN, 0, 0x1f, 1, dac_tlv),
418 static const struct reg_default tas2552_init_regs[] = {
419 { TAS2552_RESERVED_0D, 0xc0 },
422 static int tas2552_codec_probe(struct snd_soc_codec *codec)
424 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
427 tas2552->codec = codec;
429 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
433 dev_err(codec->dev, "Failed to enable supplies: %d\n",
438 if (tas2552->enable_gpio)
439 gpiod_set_value(tas2552->enable_gpio, 1);
441 ret = pm_runtime_get_sync(codec->dev);
443 dev_err(codec->dev, "Enabling device failed: %d\n",
448 snd_soc_write(codec, TAS2552_CFG_1, TAS2552_MUTE);
449 snd_soc_write(codec, TAS2552_CFG_3, TAS2552_I2S_OUT_SEL |
450 TAS2552_DIN_SRC_SEL_AVG_L_R | TAS2552_88_96KHZ);
451 snd_soc_write(codec, TAS2552_DOUT, TAS2552_PDM_DATA_I);
452 snd_soc_write(codec, TAS2552_OUTPUT_DATA, TAS2552_PDM_DATA_V_I | 0x8);
453 snd_soc_write(codec, TAS2552_BOOST_PT_CTRL, TAS2552_APT_DELAY_200 |
454 TAS2552_APT_THRESH_2_1_7);
456 ret = regmap_register_patch(tas2552->regmap, tas2552_init_regs,
457 ARRAY_SIZE(tas2552_init_regs));
459 dev_err(codec->dev, "Failed to write init registers: %d\n",
464 snd_soc_write(codec, TAS2552_CFG_2, TAS2552_BOOST_EN |
465 TAS2552_APT_EN | TAS2552_LIM_EN);
470 pm_runtime_put(codec->dev);
472 if (tas2552->enable_gpio)
473 gpiod_set_value(tas2552->enable_gpio, 0);
475 regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
480 static int tas2552_codec_remove(struct snd_soc_codec *codec)
482 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
484 pm_runtime_put(codec->dev);
486 if (tas2552->enable_gpio)
487 gpiod_set_value(tas2552->enable_gpio, 0);
493 static int tas2552_suspend(struct snd_soc_codec *codec)
495 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
498 ret = regulator_bulk_disable(ARRAY_SIZE(tas2552->supplies),
502 dev_err(codec->dev, "Failed to disable supplies: %d\n",
507 static int tas2552_resume(struct snd_soc_codec *codec)
509 struct tas2552_data *tas2552 = snd_soc_codec_get_drvdata(codec);
512 ret = regulator_bulk_enable(ARRAY_SIZE(tas2552->supplies),
516 dev_err(codec->dev, "Failed to enable supplies: %d\n",
523 #define tas2552_suspend NULL
524 #define tas2552_resume NULL
527 static struct snd_soc_codec_driver soc_codec_dev_tas2552 = {
528 .probe = tas2552_codec_probe,
529 .remove = tas2552_codec_remove,
530 .suspend = tas2552_suspend,
531 .resume = tas2552_resume,
532 .controls = tas2552_snd_controls,
533 .num_controls = ARRAY_SIZE(tas2552_snd_controls),
534 .dapm_widgets = tas2552_dapm_widgets,
535 .num_dapm_widgets = ARRAY_SIZE(tas2552_dapm_widgets),
536 .dapm_routes = tas2552_audio_map,
537 .num_dapm_routes = ARRAY_SIZE(tas2552_audio_map),
540 static const struct regmap_config tas2552_regmap_config = {
544 .max_register = TAS2552_MAX_REG,
545 .reg_defaults = tas2552_reg_defs,
546 .num_reg_defaults = ARRAY_SIZE(tas2552_reg_defs),
547 .cache_type = REGCACHE_RBTREE,
550 static int tas2552_probe(struct i2c_client *client,
551 const struct i2c_device_id *id)
554 struct tas2552_data *data;
559 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
563 data->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_LOW);
564 if (IS_ERR(data->enable_gpio)) {
565 if (PTR_ERR(data->enable_gpio) == -EPROBE_DEFER)
566 return -EPROBE_DEFER;
568 data->enable_gpio = NULL;;
571 data->tas2552_client = client;
572 data->regmap = devm_regmap_init_i2c(client, &tas2552_regmap_config);
573 if (IS_ERR(data->regmap)) {
574 ret = PTR_ERR(data->regmap);
575 dev_err(&client->dev, "Failed to allocate register map: %d\n",
580 for (i = 0; i < ARRAY_SIZE(data->supplies); i++)
581 data->supplies[i].supply = tas2552_supply_names[i];
583 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
586 dev_err(dev, "Failed to request supplies: %d\n", ret);
590 pm_runtime_set_active(&client->dev);
591 pm_runtime_set_autosuspend_delay(&client->dev, 1000);
592 pm_runtime_use_autosuspend(&client->dev);
593 pm_runtime_enable(&client->dev);
594 pm_runtime_mark_last_busy(&client->dev);
595 pm_runtime_put_sync_autosuspend(&client->dev);
597 dev_set_drvdata(&client->dev, data);
599 ret = snd_soc_register_codec(&client->dev,
600 &soc_codec_dev_tas2552,
601 tas2552_dai, ARRAY_SIZE(tas2552_dai));
603 dev_err(&client->dev, "Failed to register codec: %d\n", ret);
608 static int tas2552_i2c_remove(struct i2c_client *client)
610 snd_soc_unregister_codec(&client->dev);
614 static const struct i2c_device_id tas2552_id[] = {
618 MODULE_DEVICE_TABLE(i2c, tas2552_id);
620 #if IS_ENABLED(CONFIG_OF)
621 static const struct of_device_id tas2552_of_match[] = {
622 { .compatible = "ti,tas2552", },
625 MODULE_DEVICE_TABLE(of, tas2552_of_match);
628 static struct i2c_driver tas2552_i2c_driver = {
631 .owner = THIS_MODULE,
632 .of_match_table = of_match_ptr(tas2552_of_match),
635 .probe = tas2552_probe,
636 .remove = tas2552_i2c_remove,
637 .id_table = tas2552_id,
640 module_i2c_driver(tas2552_i2c_driver);
642 MODULE_AUTHOR("Dan Muprhy <dmurphy@ti.com>");
643 MODULE_DESCRIPTION("TAS2552 Audio amplifier driver");
644 MODULE_LICENSE("GPL");