1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
3 // Analog Devices' SSM3515 audio amp driver
5 // Copyright (C) The Asahi Linux Contributors
7 #include <linux/bits.h>
8 #include <linux/bitfield.h>
9 #include <linux/device.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
13 #include <linux/regmap.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/tlv.h>
21 #define SSM3515_PWR 0x00
22 #define SSM3515_PWR_APWDN_EN BIT(7)
23 #define SSM3515_PWR_BSNS_PWDN BIT(6)
24 #define SSM3515_PWR_S_RST BIT(1)
25 #define SSM3515_PWR_SPWDN BIT(0)
27 #define SSM3515_GEC 0x01
28 #define SSM3515_GEC_EDGE BIT(4)
29 #define SSM3515_GEC_EDGE_SHIFT 4
30 #define SSM3515_GEC_ANA_GAIN GENMASK(1, 0)
32 #define SSM3515_DAC 0x02
33 #define SSM3515_DAC_HV BIT(7)
34 #define SSM3515_DAC_MUTE BIT(6)
35 #define SSM3515_DAC_HPF BIT(5)
36 #define SSM3515_DAC_LPM BIT(4)
37 #define SSM3515_DAC_FS GENMASK(2, 0)
39 #define SSM3515_DAC_VOL 0x03
41 #define SSM3515_SAI1 0x04
42 #define SSM3515_SAI1_DAC_POL BIT(7)
43 #define SSM3515_SAI1_BCLK_POL BIT(6)
44 #define SSM3515_SAI1_TDM_BCLKS GENMASK(5, 3)
45 #define SSM3515_SAI1_FSYNC_MODE BIT(2)
46 #define SSM3515_SAI1_SDATA_FMT BIT(1)
47 #define SSM3515_SAI1_SAI_MODE BIT(0)
49 #define SSM3515_SAI2 0x05
50 #define SSM3515_SAI2_DATA_WIDTH BIT(7)
51 #define SSM3515_SAI2_AUTO_SLOT BIT(4)
52 #define SSM3515_SAI2_TDM_SLOT GENMASK(3, 0)
54 #define SSM3515_VBAT_OUT 0x06
56 #define SSM3515_STATUS 0x0a
57 #define SSM3515_STATUS_UVLO_REG BIT(6)
58 #define SSM3515_STATUS_LIM_EG BIT(5)
59 #define SSM3515_STATUS_CLIP BIT(4)
60 #define SSM3515_STATUS_AMP_OC BIT(3)
61 #define SSM3515_STATUS_OTF BIT(2)
62 #define SSM3515_STATUS_OTW BIT(1)
63 #define SSM3515_STATUS_BAT_WARN BIT(0)
65 static bool ssm3515_volatile_reg(struct device *dev, unsigned int reg)
69 case SSM3515_VBAT_OUT:
77 static const struct reg_default ssm3515_reg_defaults[] = {
78 { SSM3515_PWR, 0x81 },
79 { SSM3515_GEC, 0x01 },
80 { SSM3515_DAC, 0x32 },
81 { SSM3515_DAC_VOL, 0x40 },
82 { SSM3515_SAI1, 0x11 },
83 { SSM3515_SAI2, 0x00 },
86 static const struct regmap_config ssm3515_i2c_regmap = {
89 .volatile_reg = ssm3515_volatile_reg,
91 .reg_defaults = ssm3515_reg_defaults,
92 .num_reg_defaults = ARRAY_SIZE(ssm3515_reg_defaults),
93 .cache_type = REGCACHE_FLAT,
98 struct regmap *regmap;
101 // The specced range is -71.25...24.00 dB with step size of 0.375 dB,
102 // and a mute item below that. This is represented by -71.62...24.00 dB
103 // with the mute item mapped onto the low end.
104 static DECLARE_TLV_DB_MINMAX_MUTE(ssm3515_dac_volume, -7162, 2400);
106 static const char * const ssm3515_ana_gain_text[] = {
107 "8.4 V Span", "12.6 V Span", "14 V Span", "15 V Span",
110 static SOC_ENUM_SINGLE_DECL(ssm3515_ana_gain_enum, SSM3515_GEC,
111 __bf_shf(SSM3515_GEC_ANA_GAIN),
112 ssm3515_ana_gain_text);
114 static const struct snd_kcontrol_new ssm3515_snd_controls[] = {
115 SOC_SINGLE_TLV("DAC Playback Volume", SSM3515_DAC_VOL,
116 0, 255, 1, ssm3515_dac_volume),
117 SOC_SINGLE("Low EMI Mode Switch", SSM3515_GEC,
118 __bf_shf(SSM3515_GEC_EDGE), 1, 0),
119 SOC_SINGLE("Soft Volume Ramping Switch", SSM3515_DAC,
120 __bf_shf(SSM3515_DAC_HV), 1, 1),
121 SOC_SINGLE("HPF Switch", SSM3515_DAC,
122 __bf_shf(SSM3515_DAC_HPF), 1, 0),
123 SOC_SINGLE("DAC Invert Switch", SSM3515_SAI1,
124 __bf_shf(SSM3515_SAI1_DAC_POL), 1, 0),
125 SOC_ENUM("DAC Analog Gain Select", ssm3515_ana_gain_enum),
128 static void ssm3515_read_faults(struct snd_soc_component *component)
132 ret = snd_soc_component_read(component, SSM3515_STATUS);
135 * If the read was erroneous, ASoC core has printed a message,
136 * and that's all that's appropriate in handling the error here.
141 dev_err(component->dev, "device reports:%s%s%s%s%s%s%s\n",
142 FIELD_GET(SSM3515_STATUS_UVLO_REG, ret) ? " voltage regulator fault" : "",
143 FIELD_GET(SSM3515_STATUS_LIM_EG, ret) ? " limiter engaged" : "",
144 FIELD_GET(SSM3515_STATUS_CLIP, ret) ? " clipping detected" : "",
145 FIELD_GET(SSM3515_STATUS_AMP_OC, ret) ? " amp over-current fault" : "",
146 FIELD_GET(SSM3515_STATUS_OTF, ret) ? " overtemperature fault" : "",
147 FIELD_GET(SSM3515_STATUS_OTW, ret) ? " overtemperature warning" : "",
148 FIELD_GET(SSM3515_STATUS_BAT_WARN, ret) ? " bat voltage low warning" : "");
151 static int ssm3515_probe(struct snd_soc_component *component)
155 /* Start out muted */
156 ret = snd_soc_component_update_bits(component, SSM3515_DAC,
157 SSM3515_DAC_MUTE, SSM3515_DAC_MUTE);
161 /* Disable the 'master power-down' */
162 ret = snd_soc_component_update_bits(component, SSM3515_PWR,
163 SSM3515_PWR_SPWDN, 0);
170 static int ssm3515_mute(struct snd_soc_dai *dai, int mute, int direction)
174 ret = snd_soc_component_update_bits(dai->component,
177 FIELD_PREP(SSM3515_DAC_MUTE, mute));
183 static int ssm3515_hw_params(struct snd_pcm_substream *substream,
184 struct snd_pcm_hw_params *params,
185 struct snd_soc_dai *dai)
187 struct snd_soc_component *component = dai->component;
190 switch (params_format(params)) {
191 case SNDRV_PCM_FORMAT_S16:
192 case SNDRV_PCM_FORMAT_S24:
193 ret = snd_soc_component_update_bits(component,
194 SSM3515_SAI2, SSM3515_SAI2_DATA_WIDTH,
195 FIELD_PREP(SSM3515_SAI2_DATA_WIDTH,
196 params_width(params) == 16));
205 switch (params_rate(params)) {
209 case 16000 ... 24000:
212 case 32000 ... 48000:
215 case 64000 ... 96000:
218 case 128000 ... 192000:
221 case 48001 ... 63999: /* this is ...72000 but overlaps */
228 ret = snd_soc_component_update_bits(component,
229 SSM3515_DAC, SSM3515_DAC_FS,
230 FIELD_PREP(SSM3515_DAC_FS, rateval));
237 static int ssm3515_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
239 struct snd_soc_component *component = dai->component;
240 bool fpol_inv = false; /* non-inverted: frame starts with low-to-high FSYNC */
244 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
245 case SND_SOC_DAIFMT_IB_NF:
246 case SND_SOC_DAIFMT_IB_IF:
247 sai1 |= SSM3515_SAI1_BCLK_POL;
251 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
252 case SND_SOC_DAIFMT_I2S:
254 sai1 &= ~SSM3515_SAI1_SDATA_FMT; /* 1 bit start delay */
256 case SND_SOC_DAIFMT_LEFT_J:
258 sai1 |= SSM3515_SAI1_SDATA_FMT; /* no start delay */
264 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
265 case SND_SOC_DAIFMT_NB_IF:
266 case SND_SOC_DAIFMT_IB_IF:
271 /* Set the serial input to 'TDM mode' */
272 sai1 |= SSM3515_SAI1_SAI_MODE;
276 * We configure the codec in a 'TDM mode', in which the
277 * FSYNC_MODE bit of SAI1 is supposed to select between
278 * what the datasheet calls 'Pulsed FSYNC mode' and '50%
281 * Experiments suggest that this bit in fact simply selects
282 * the FSYNC polarity, so go with that.
284 sai1 |= SSM3515_SAI1_FSYNC_MODE;
287 ret = snd_soc_component_update_bits(component, SSM3515_SAI1,
288 SSM3515_SAI1_BCLK_POL | SSM3515_SAI1_SDATA_FMT |
289 SSM3515_SAI1_SAI_MODE | SSM3515_SAI1_FSYNC_MODE, sai1);
296 static int ssm3515_set_tdm_slot(struct snd_soc_dai *dai,
297 unsigned int tx_mask,
298 unsigned int rx_mask,
299 int slots, int slot_width)
301 struct snd_soc_component *component = dai->component;
302 int slot, tdm_bclks_val, ret;
304 if (tx_mask == 0 || rx_mask != 0)
307 slot = __ffs(tx_mask);
309 if (tx_mask & ~BIT(slot))
312 switch (slot_width) {
332 ret = snd_soc_component_update_bits(component, SSM3515_SAI1,
333 SSM3515_SAI1_TDM_BCLKS,
334 FIELD_PREP(SSM3515_SAI1_TDM_BCLKS, tdm_bclks_val));
338 ret = snd_soc_component_update_bits(component, SSM3515_SAI2,
339 SSM3515_SAI2_TDM_SLOT,
340 FIELD_PREP(SSM3515_SAI2_TDM_SLOT, slot));
347 static int ssm3515_hw_free(struct snd_pcm_substream *substream,
348 struct snd_soc_dai *dai)
351 * We don't get live notification of faults, so at least at
352 * this time, when playback is over, check if we have tripped
353 * over anything and if so, log it.
355 ssm3515_read_faults(dai->component);
359 static const struct snd_soc_dai_ops ssm3515_dai_ops = {
360 .mute_stream = ssm3515_mute,
361 .hw_params = ssm3515_hw_params,
362 .set_fmt = ssm3515_set_fmt,
363 .set_tdm_slot = ssm3515_set_tdm_slot,
364 .hw_free = ssm3515_hw_free,
367 static struct snd_soc_dai_driver ssm3515_dai_driver = {
368 .name = "SSM3515 SAI",
371 .stream_name = "Playback",
374 .rates = SNDRV_PCM_RATE_CONTINUOUS,
375 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
377 .ops = &ssm3515_dai_ops,
380 static const struct snd_soc_dapm_widget ssm3515_dapm_widgets[] = {
381 SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
382 SND_SOC_DAPM_OUTPUT("OUT"),
385 static const struct snd_soc_dapm_route ssm3515_dapm_routes[] = {
386 {"OUT", NULL, "DAC"},
387 {"DAC", NULL, "Playback"},
390 static const struct snd_soc_component_driver ssm3515_asoc_component = {
391 .probe = ssm3515_probe,
392 .controls = ssm3515_snd_controls,
393 .num_controls = ARRAY_SIZE(ssm3515_snd_controls),
394 .dapm_widgets = ssm3515_dapm_widgets,
395 .num_dapm_widgets = ARRAY_SIZE(ssm3515_dapm_widgets),
396 .dapm_routes = ssm3515_dapm_routes,
397 .num_dapm_routes = ARRAY_SIZE(ssm3515_dapm_routes),
401 static int ssm3515_i2c_probe(struct i2c_client *client)
403 struct ssm3515_data *data;
406 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
410 data->dev = &client->dev;
411 i2c_set_clientdata(client, data);
413 data->regmap = devm_regmap_init_i2c(client, &ssm3515_i2c_regmap);
414 if (IS_ERR(data->regmap))
415 return dev_err_probe(data->dev, PTR_ERR(data->regmap),
416 "initializing register map\n");
418 /* Perform a reset */
419 ret = regmap_update_bits(data->regmap, SSM3515_PWR,
420 SSM3515_PWR_S_RST, SSM3515_PWR_S_RST);
422 return dev_err_probe(data->dev, ret,
423 "performing software reset\n");
424 regmap_reinit_cache(data->regmap, &ssm3515_i2c_regmap);
426 return devm_snd_soc_register_component(data->dev,
427 &ssm3515_asoc_component,
428 &ssm3515_dai_driver, 1);
431 static const struct of_device_id ssm3515_of_match[] = {
432 { .compatible = "adi,ssm3515" },
435 MODULE_DEVICE_TABLE(of, ssm3515_of_match);
437 static struct i2c_driver ssm3515_i2c_driver = {
440 .of_match_table = ssm3515_of_match,
442 .probe = ssm3515_i2c_probe,
444 module_i2c_driver(ssm3515_i2c_driver);
447 MODULE_DESCRIPTION("ASoC SSM3515 audio amp driver");
448 MODULE_LICENSE("Dual MIT/GPL");