1 // SPDX-License-Identifier: GPL-2.0-only
3 * SSM2518 amplifier audio driver
5 * Copyright 2013 Analog Devices Inc.
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
15 #include <linux/gpio/consumer.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/initval.h>
21 #include <sound/tlv.h>
25 #define SSM2518_REG_POWER1 0x00
26 #define SSM2518_REG_CLOCK 0x01
27 #define SSM2518_REG_SAI_CTRL1 0x02
28 #define SSM2518_REG_SAI_CTRL2 0x03
29 #define SSM2518_REG_CHAN_MAP 0x04
30 #define SSM2518_REG_LEFT_VOL 0x05
31 #define SSM2518_REG_RIGHT_VOL 0x06
32 #define SSM2518_REG_MUTE_CTRL 0x07
33 #define SSM2518_REG_FAULT_CTRL 0x08
34 #define SSM2518_REG_POWER2 0x09
35 #define SSM2518_REG_DRC_1 0x0a
36 #define SSM2518_REG_DRC_2 0x0b
37 #define SSM2518_REG_DRC_3 0x0c
38 #define SSM2518_REG_DRC_4 0x0d
39 #define SSM2518_REG_DRC_5 0x0e
40 #define SSM2518_REG_DRC_6 0x0f
41 #define SSM2518_REG_DRC_7 0x10
42 #define SSM2518_REG_DRC_8 0x11
43 #define SSM2518_REG_DRC_9 0x12
45 #define SSM2518_POWER1_RESET BIT(7)
46 #define SSM2518_POWER1_NO_BCLK BIT(5)
47 #define SSM2518_POWER1_MCS_MASK (0xf << 1)
48 #define SSM2518_POWER1_MCS_64FS (0x0 << 1)
49 #define SSM2518_POWER1_MCS_128FS (0x1 << 1)
50 #define SSM2518_POWER1_MCS_256FS (0x2 << 1)
51 #define SSM2518_POWER1_MCS_384FS (0x3 << 1)
52 #define SSM2518_POWER1_MCS_512FS (0x4 << 1)
53 #define SSM2518_POWER1_MCS_768FS (0x5 << 1)
54 #define SSM2518_POWER1_MCS_100FS (0x6 << 1)
55 #define SSM2518_POWER1_MCS_200FS (0x7 << 1)
56 #define SSM2518_POWER1_MCS_400FS (0x8 << 1)
57 #define SSM2518_POWER1_SPWDN BIT(0)
59 #define SSM2518_CLOCK_ASR BIT(0)
61 #define SSM2518_SAI_CTRL1_FMT_MASK (0x3 << 5)
62 #define SSM2518_SAI_CTRL1_FMT_I2S (0x0 << 5)
63 #define SSM2518_SAI_CTRL1_FMT_LJ (0x1 << 5)
64 #define SSM2518_SAI_CTRL1_FMT_RJ_24BIT (0x2 << 5)
65 #define SSM2518_SAI_CTRL1_FMT_RJ_16BIT (0x3 << 5)
67 #define SSM2518_SAI_CTRL1_SAI_MASK (0x7 << 2)
68 #define SSM2518_SAI_CTRL1_SAI_I2S (0x0 << 2)
69 #define SSM2518_SAI_CTRL1_SAI_TDM_2 (0x1 << 2)
70 #define SSM2518_SAI_CTRL1_SAI_TDM_4 (0x2 << 2)
71 #define SSM2518_SAI_CTRL1_SAI_TDM_8 (0x3 << 2)
72 #define SSM2518_SAI_CTRL1_SAI_TDM_16 (0x4 << 2)
73 #define SSM2518_SAI_CTRL1_SAI_MONO (0x5 << 2)
75 #define SSM2518_SAI_CTRL1_FS_MASK (0x3)
76 #define SSM2518_SAI_CTRL1_FS_8000_12000 (0x0)
77 #define SSM2518_SAI_CTRL1_FS_16000_24000 (0x1)
78 #define SSM2518_SAI_CTRL1_FS_32000_48000 (0x2)
79 #define SSM2518_SAI_CTRL1_FS_64000_96000 (0x3)
81 #define SSM2518_SAI_CTRL2_BCLK_INTERAL BIT(7)
82 #define SSM2518_SAI_CTRL2_LRCLK_PULSE BIT(6)
83 #define SSM2518_SAI_CTRL2_LRCLK_INVERT BIT(5)
84 #define SSM2518_SAI_CTRL2_MSB BIT(4)
85 #define SSM2518_SAI_CTRL2_SLOT_WIDTH_MASK (0x3 << 2)
86 #define SSM2518_SAI_CTRL2_SLOT_WIDTH_32 (0x0 << 2)
87 #define SSM2518_SAI_CTRL2_SLOT_WIDTH_24 (0x1 << 2)
88 #define SSM2518_SAI_CTRL2_SLOT_WIDTH_16 (0x2 << 2)
89 #define SSM2518_SAI_CTRL2_BCLK_INVERT BIT(1)
91 #define SSM2518_CHAN_MAP_RIGHT_SLOT_OFFSET 4
92 #define SSM2518_CHAN_MAP_RIGHT_SLOT_MASK 0xf0
93 #define SSM2518_CHAN_MAP_LEFT_SLOT_OFFSET 0
94 #define SSM2518_CHAN_MAP_LEFT_SLOT_MASK 0x0f
96 #define SSM2518_MUTE_CTRL_ANA_GAIN BIT(5)
97 #define SSM2518_MUTE_CTRL_MUTE_MASTER BIT(0)
99 #define SSM2518_POWER2_APWDN BIT(0)
101 #define SSM2518_DAC_MUTE BIT(6)
102 #define SSM2518_DAC_FS_MASK 0x07
103 #define SSM2518_DAC_FS_8000 0x00
104 #define SSM2518_DAC_FS_16000 0x01
105 #define SSM2518_DAC_FS_32000 0x02
106 #define SSM2518_DAC_FS_64000 0x03
107 #define SSM2518_DAC_FS_128000 0x04
110 struct regmap *regmap;
114 const struct snd_pcm_hw_constraint_list *constraints;
116 struct gpio_desc *enable_gpio;
119 static const struct reg_default ssm2518_reg_defaults[] = {
141 static const DECLARE_TLV_DB_MINMAX_MUTE(ssm2518_vol_tlv, -7125, 2400);
142 static const DECLARE_TLV_DB_SCALE(ssm2518_compressor_tlv, -3400, 200, 0);
143 static const DECLARE_TLV_DB_SCALE(ssm2518_expander_tlv, -8100, 300, 0);
144 static const DECLARE_TLV_DB_SCALE(ssm2518_noise_gate_tlv, -9600, 300, 0);
145 static const DECLARE_TLV_DB_SCALE(ssm2518_post_drc_tlv, -2400, 300, 0);
147 static const DECLARE_TLV_DB_RANGE(ssm2518_limiter_tlv,
148 0, 7, TLV_DB_SCALE_ITEM(-2200, 200, 0),
149 7, 15, TLV_DB_SCALE_ITEM(-800, 100, 0),
152 static const char * const ssm2518_drc_peak_detector_attack_time_text[] = {
153 "0 ms", "0.1 ms", "0.19 ms", "0.37 ms", "0.75 ms", "1.5 ms", "3 ms",
154 "6 ms", "12 ms", "24 ms", "48 ms", "96 ms", "192 ms", "384 ms",
158 static const char * const ssm2518_drc_peak_detector_release_time_text[] = {
159 "0 ms", "1.5 ms", "3 ms", "6 ms", "12 ms", "24 ms", "48 ms", "96 ms",
160 "192 ms", "384 ms", "768 ms", "1536 ms", "3072 ms", "6144 ms",
161 "12288 ms", "24576 ms"
164 static const char * const ssm2518_drc_hold_time_text[] = {
165 "0 ms", "0.67 ms", "1.33 ms", "2.67 ms", "5.33 ms", "10.66 ms",
166 "21.32 ms", "42.64 ms", "85.28 ms", "170.56 ms", "341.12 ms",
167 "682.24 ms", "1364 ms",
170 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_peak_detector_attack_time_enum,
171 SSM2518_REG_DRC_2, 4, ssm2518_drc_peak_detector_attack_time_text);
172 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_peak_detector_release_time_enum,
173 SSM2518_REG_DRC_2, 0, ssm2518_drc_peak_detector_release_time_text);
174 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_attack_time_enum,
175 SSM2518_REG_DRC_6, 4, ssm2518_drc_peak_detector_attack_time_text);
176 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_decay_time_enum,
177 SSM2518_REG_DRC_6, 0, ssm2518_drc_peak_detector_release_time_text);
178 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_hold_time_enum,
179 SSM2518_REG_DRC_7, 4, ssm2518_drc_hold_time_text);
180 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_noise_gate_hold_time_enum,
181 SSM2518_REG_DRC_7, 0, ssm2518_drc_hold_time_text);
182 static SOC_ENUM_SINGLE_DECL(ssm2518_drc_rms_averaging_time_enum,
183 SSM2518_REG_DRC_9, 0, ssm2518_drc_peak_detector_release_time_text);
185 static const struct snd_kcontrol_new ssm2518_snd_controls[] = {
186 SOC_SINGLE("Playback De-emphasis Switch", SSM2518_REG_MUTE_CTRL,
188 SOC_DOUBLE_R_TLV("Master Playback Volume", SSM2518_REG_LEFT_VOL,
189 SSM2518_REG_RIGHT_VOL, 0, 0xff, 1, ssm2518_vol_tlv),
190 SOC_DOUBLE("Master Playback Switch", SSM2518_REG_MUTE_CTRL, 2, 1, 1, 1),
192 SOC_SINGLE("Amp Low Power Mode Switch", SSM2518_REG_POWER2, 4, 1, 0),
193 SOC_SINGLE("DAC Low Power Mode Switch", SSM2518_REG_POWER2, 3, 1, 0),
195 SOC_SINGLE("DRC Limiter Switch", SSM2518_REG_DRC_1, 5, 1, 0),
196 SOC_SINGLE("DRC Compressor Switch", SSM2518_REG_DRC_1, 4, 1, 0),
197 SOC_SINGLE("DRC Expander Switch", SSM2518_REG_DRC_1, 3, 1, 0),
198 SOC_SINGLE("DRC Noise Gate Switch", SSM2518_REG_DRC_1, 2, 1, 0),
199 SOC_DOUBLE("DRC Switch", SSM2518_REG_DRC_1, 0, 1, 1, 0),
201 SOC_SINGLE_TLV("DRC Limiter Threshold Volume",
202 SSM2518_REG_DRC_3, 4, 15, 1, ssm2518_limiter_tlv),
203 SOC_SINGLE_TLV("DRC Compressor Lower Threshold Volume",
204 SSM2518_REG_DRC_3, 0, 15, 1, ssm2518_compressor_tlv),
205 SOC_SINGLE_TLV("DRC Expander Upper Threshold Volume", SSM2518_REG_DRC_4,
206 4, 15, 1, ssm2518_expander_tlv),
207 SOC_SINGLE_TLV("DRC Noise Gate Threshold Volume",
208 SSM2518_REG_DRC_4, 0, 15, 1, ssm2518_noise_gate_tlv),
209 SOC_SINGLE_TLV("DRC Upper Output Threshold Volume",
210 SSM2518_REG_DRC_5, 4, 15, 1, ssm2518_limiter_tlv),
211 SOC_SINGLE_TLV("DRC Lower Output Threshold Volume",
212 SSM2518_REG_DRC_5, 0, 15, 1, ssm2518_noise_gate_tlv),
213 SOC_SINGLE_TLV("DRC Post Volume", SSM2518_REG_DRC_8,
214 2, 15, 1, ssm2518_post_drc_tlv),
216 SOC_ENUM("DRC Peak Detector Attack Time",
217 ssm2518_drc_peak_detector_attack_time_enum),
218 SOC_ENUM("DRC Peak Detector Release Time",
219 ssm2518_drc_peak_detector_release_time_enum),
220 SOC_ENUM("DRC Attack Time", ssm2518_drc_attack_time_enum),
221 SOC_ENUM("DRC Decay Time", ssm2518_drc_decay_time_enum),
222 SOC_ENUM("DRC Hold Time", ssm2518_drc_hold_time_enum),
223 SOC_ENUM("DRC Noise Gate Hold Time",
224 ssm2518_drc_noise_gate_hold_time_enum),
225 SOC_ENUM("DRC RMS Averaging Time", ssm2518_drc_rms_averaging_time_enum),
228 static const struct snd_soc_dapm_widget ssm2518_dapm_widgets[] = {
229 SND_SOC_DAPM_DAC("DACL", "HiFi Playback", SSM2518_REG_POWER2, 1, 1),
230 SND_SOC_DAPM_DAC("DACR", "HiFi Playback", SSM2518_REG_POWER2, 2, 1),
232 SND_SOC_DAPM_OUTPUT("OUTL"),
233 SND_SOC_DAPM_OUTPUT("OUTR"),
236 static const struct snd_soc_dapm_route ssm2518_routes[] = {
237 { "OUTL", NULL, "DACL" },
238 { "OUTR", NULL, "DACR" },
241 struct ssm2518_mcs_lut {
243 const unsigned int *sysclks;
246 static const unsigned int ssm2518_sysclks_2048000[] = {
247 2048000, 4096000, 8192000, 12288000, 16384000, 24576000,
248 3200000, 6400000, 12800000, 0
251 static const unsigned int ssm2518_sysclks_2822000[] = {
252 2822000, 5644800, 11289600, 16934400, 22579200, 33868800,
253 4410000, 8820000, 17640000, 0
256 static const unsigned int ssm2518_sysclks_3072000[] = {
257 3072000, 6144000, 12288000, 16384000, 24576000, 38864000,
258 4800000, 9600000, 19200000, 0
261 static const struct ssm2518_mcs_lut ssm2518_mcs_lut[] = {
262 { 8000, ssm2518_sysclks_2048000, },
263 { 11025, ssm2518_sysclks_2822000, },
264 { 12000, ssm2518_sysclks_3072000, },
265 { 16000, ssm2518_sysclks_2048000, },
266 { 24000, ssm2518_sysclks_3072000, },
267 { 22050, ssm2518_sysclks_2822000, },
268 { 32000, ssm2518_sysclks_2048000, },
269 { 44100, ssm2518_sysclks_2822000, },
270 { 48000, ssm2518_sysclks_3072000, },
271 { 96000, ssm2518_sysclks_3072000, },
274 static const unsigned int ssm2518_rates_2048000[] = {
278 static const struct snd_pcm_hw_constraint_list ssm2518_constraints_2048000 = {
279 .list = ssm2518_rates_2048000,
280 .count = ARRAY_SIZE(ssm2518_rates_2048000),
283 static const unsigned int ssm2518_rates_2822000[] = {
287 static const struct snd_pcm_hw_constraint_list ssm2518_constraints_2822000 = {
288 .list = ssm2518_rates_2822000,
289 .count = ARRAY_SIZE(ssm2518_rates_2822000),
292 static const unsigned int ssm2518_rates_3072000[] = {
293 12000, 24000, 48000, 96000,
296 static const struct snd_pcm_hw_constraint_list ssm2518_constraints_3072000 = {
297 .list = ssm2518_rates_3072000,
298 .count = ARRAY_SIZE(ssm2518_rates_3072000),
301 static const unsigned int ssm2518_rates_12288000[] = {
302 8000, 12000, 16000, 24000, 32000, 48000, 96000,
305 static const struct snd_pcm_hw_constraint_list ssm2518_constraints_12288000 = {
306 .list = ssm2518_rates_12288000,
307 .count = ARRAY_SIZE(ssm2518_rates_12288000),
310 static int ssm2518_lookup_mcs(struct ssm2518 *ssm2518,
313 const unsigned int *sysclks = NULL;
316 for (i = 0; i < ARRAY_SIZE(ssm2518_mcs_lut); i++) {
317 if (ssm2518_mcs_lut[i].rate == rate) {
318 sysclks = ssm2518_mcs_lut[i].sysclks;
326 for (i = 0; sysclks[i]; i++) {
327 if (sysclks[i] == ssm2518->sysclk)
334 static int ssm2518_hw_params(struct snd_pcm_substream *substream,
335 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
337 struct snd_soc_component *component = dai->component;
338 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
339 unsigned int rate = params_rate(params);
340 unsigned int ctrl1, ctrl1_mask;
344 mcs = ssm2518_lookup_mcs(ssm2518, rate);
348 ctrl1_mask = SSM2518_SAI_CTRL1_FS_MASK;
350 if (rate >= 8000 && rate <= 12000)
351 ctrl1 = SSM2518_SAI_CTRL1_FS_8000_12000;
352 else if (rate >= 16000 && rate <= 24000)
353 ctrl1 = SSM2518_SAI_CTRL1_FS_16000_24000;
354 else if (rate >= 32000 && rate <= 48000)
355 ctrl1 = SSM2518_SAI_CTRL1_FS_32000_48000;
356 else if (rate >= 64000 && rate <= 96000)
357 ctrl1 = SSM2518_SAI_CTRL1_FS_64000_96000;
361 if (ssm2518->right_j) {
362 switch (params_width(params)) {
364 ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_16BIT;
367 ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_24BIT;
372 ctrl1_mask |= SSM2518_SAI_CTRL1_FMT_MASK;
375 /* Disable auto samplerate detection */
376 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_CLOCK,
377 SSM2518_CLOCK_ASR, SSM2518_CLOCK_ASR);
381 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL1,
386 return regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
387 SSM2518_POWER1_MCS_MASK, mcs << 1);
390 static int ssm2518_mute(struct snd_soc_dai *dai, int mute, int direction)
392 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
396 val = SSM2518_MUTE_CTRL_MUTE_MASTER;
400 return regmap_update_bits(ssm2518->regmap, SSM2518_REG_MUTE_CTRL,
401 SSM2518_MUTE_CTRL_MUTE_MASTER, val);
404 static int ssm2518_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
406 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
407 unsigned int ctrl1 = 0, ctrl2 = 0;
411 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
412 case SND_SOC_DAIFMT_CBC_CFC:
418 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
419 case SND_SOC_DAIFMT_NB_NF:
422 case SND_SOC_DAIFMT_IB_NF:
423 ctrl2 |= SSM2518_SAI_CTRL2_BCLK_INVERT;
426 case SND_SOC_DAIFMT_NB_IF:
429 case SND_SOC_DAIFMT_IB_IF:
430 ctrl2 |= SSM2518_SAI_CTRL2_BCLK_INVERT;
437 ssm2518->right_j = false;
438 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
439 case SND_SOC_DAIFMT_I2S:
440 ctrl1 |= SSM2518_SAI_CTRL1_FMT_I2S;
442 case SND_SOC_DAIFMT_LEFT_J:
443 ctrl1 |= SSM2518_SAI_CTRL1_FMT_LJ;
444 invert_fclk = !invert_fclk;
446 case SND_SOC_DAIFMT_RIGHT_J:
447 ctrl1 |= SSM2518_SAI_CTRL1_FMT_RJ_24BIT;
448 ssm2518->right_j = true;
449 invert_fclk = !invert_fclk;
451 case SND_SOC_DAIFMT_DSP_A:
452 ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_PULSE;
453 ctrl1 |= SSM2518_SAI_CTRL1_FMT_I2S;
456 case SND_SOC_DAIFMT_DSP_B:
457 ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_PULSE;
458 ctrl1 |= SSM2518_SAI_CTRL1_FMT_LJ;
466 ctrl2 |= SSM2518_SAI_CTRL2_LRCLK_INVERT;
468 ret = regmap_write(ssm2518->regmap, SSM2518_REG_SAI_CTRL1, ctrl1);
472 return regmap_write(ssm2518->regmap, SSM2518_REG_SAI_CTRL2, ctrl2);
475 static int ssm2518_set_power(struct ssm2518 *ssm2518, bool enable)
480 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
481 SSM2518_POWER1_SPWDN, SSM2518_POWER1_SPWDN);
482 regcache_mark_dirty(ssm2518->regmap);
485 if (ssm2518->enable_gpio)
486 gpiod_set_value_cansleep(ssm2518->enable_gpio, enable);
488 regcache_cache_only(ssm2518->regmap, !enable);
491 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
492 SSM2518_POWER1_SPWDN | SSM2518_POWER1_RESET, 0x00);
493 regcache_sync(ssm2518->regmap);
499 static int ssm2518_set_bias_level(struct snd_soc_component *component,
500 enum snd_soc_bias_level level)
502 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
506 case SND_SOC_BIAS_ON:
508 case SND_SOC_BIAS_PREPARE:
510 case SND_SOC_BIAS_STANDBY:
511 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF)
512 ret = ssm2518_set_power(ssm2518, true);
514 case SND_SOC_BIAS_OFF:
515 ret = ssm2518_set_power(ssm2518, false);
522 static int ssm2518_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
523 unsigned int rx_mask, int slots, int width)
525 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
526 unsigned int ctrl1, ctrl2;
527 int left_slot, right_slot;
531 return regmap_update_bits(ssm2518->regmap,
532 SSM2518_REG_SAI_CTRL1, SSM2518_SAI_CTRL1_SAI_MASK,
533 SSM2518_SAI_CTRL1_SAI_I2S);
535 if (tx_mask == 0 || rx_mask != 0)
544 /* We assume the left channel < right channel */
545 left_slot = __ffs(tx_mask);
546 tx_mask &= ~(1 << left_slot);
548 right_slot = left_slot;
550 right_slot = __ffs(tx_mask);
551 tx_mask &= ~(1 << right_slot);
555 if (tx_mask != 0 || left_slot >= slots || right_slot >= slots)
560 ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_16;
563 ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_24;
566 ctrl2 = SSM2518_SAI_CTRL2_SLOT_WIDTH_32;
574 ctrl1 = SSM2518_SAI_CTRL1_SAI_MONO;
577 ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_2;
580 ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_4;
583 ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_8;
586 ctrl1 = SSM2518_SAI_CTRL1_SAI_TDM_16;
592 ret = regmap_write(ssm2518->regmap, SSM2518_REG_CHAN_MAP,
593 (left_slot << SSM2518_CHAN_MAP_LEFT_SLOT_OFFSET) |
594 (right_slot << SSM2518_CHAN_MAP_RIGHT_SLOT_OFFSET));
598 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL1,
599 SSM2518_SAI_CTRL1_SAI_MASK, ctrl1);
603 return regmap_update_bits(ssm2518->regmap, SSM2518_REG_SAI_CTRL2,
604 SSM2518_SAI_CTRL2_SLOT_WIDTH_MASK, ctrl2);
607 static int ssm2518_startup(struct snd_pcm_substream *substream,
608 struct snd_soc_dai *dai)
610 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(dai->component);
612 if (ssm2518->constraints)
613 snd_pcm_hw_constraint_list(substream->runtime, 0,
614 SNDRV_PCM_HW_PARAM_RATE, ssm2518->constraints);
619 #define SSM2518_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE | \
620 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32)
622 static const struct snd_soc_dai_ops ssm2518_dai_ops = {
623 .startup = ssm2518_startup,
624 .hw_params = ssm2518_hw_params,
625 .mute_stream = ssm2518_mute,
626 .set_fmt = ssm2518_set_dai_fmt,
627 .set_tdm_slot = ssm2518_set_tdm_slot,
628 .no_capture_mute = 1,
631 static struct snd_soc_dai_driver ssm2518_dai = {
632 .name = "ssm2518-hifi",
634 .stream_name = "Playback",
637 .rates = SNDRV_PCM_RATE_8000_96000,
638 .formats = SSM2518_FORMATS,
640 .ops = &ssm2518_dai_ops,
643 static int ssm2518_set_sysclk(struct snd_soc_component *component, int clk_id,
644 int source, unsigned int freq, int dir)
646 struct ssm2518 *ssm2518 = snd_soc_component_get_drvdata(component);
649 if (clk_id != SSM2518_SYSCLK)
653 case SSM2518_SYSCLK_SRC_MCLK:
656 case SSM2518_SYSCLK_SRC_BCLK:
657 /* In this case the bitclock is used as the system clock, and
658 * the bitclock signal needs to be connected to the MCLK pin and
659 * the BCLK pin is left unconnected */
660 val = SSM2518_POWER1_NO_BCLK;
668 ssm2518->constraints = NULL;
676 ssm2518->constraints = &ssm2518_constraints_2048000;
687 ssm2518->constraints = &ssm2518_constraints_2822000;
695 ssm2518->constraints = &ssm2518_constraints_3072000;
700 ssm2518->constraints = &ssm2518_constraints_12288000;
706 ssm2518->sysclk = freq;
708 return regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER1,
709 SSM2518_POWER1_NO_BCLK, val);
712 static const struct snd_soc_component_driver ssm2518_component_driver = {
713 .set_bias_level = ssm2518_set_bias_level,
714 .set_sysclk = ssm2518_set_sysclk,
715 .controls = ssm2518_snd_controls,
716 .num_controls = ARRAY_SIZE(ssm2518_snd_controls),
717 .dapm_widgets = ssm2518_dapm_widgets,
718 .num_dapm_widgets = ARRAY_SIZE(ssm2518_dapm_widgets),
719 .dapm_routes = ssm2518_routes,
720 .num_dapm_routes = ARRAY_SIZE(ssm2518_routes),
721 .use_pmdown_time = 1,
725 static const struct regmap_config ssm2518_regmap_config = {
729 .max_register = SSM2518_REG_DRC_9,
731 .cache_type = REGCACHE_RBTREE,
732 .reg_defaults = ssm2518_reg_defaults,
733 .num_reg_defaults = ARRAY_SIZE(ssm2518_reg_defaults),
736 static int ssm2518_i2c_probe(struct i2c_client *i2c)
738 struct ssm2518 *ssm2518;
741 ssm2518 = devm_kzalloc(&i2c->dev, sizeof(*ssm2518), GFP_KERNEL);
745 /* Start with enabling the chip */
746 ssm2518->enable_gpio = devm_gpiod_get_optional(&i2c->dev, NULL,
748 ret = PTR_ERR_OR_ZERO(ssm2518->enable_gpio);
752 gpiod_set_consumer_name(ssm2518->enable_gpio, "SSM2518 nSD");
754 i2c_set_clientdata(i2c, ssm2518);
756 ssm2518->regmap = devm_regmap_init_i2c(i2c, &ssm2518_regmap_config);
757 if (IS_ERR(ssm2518->regmap))
758 return PTR_ERR(ssm2518->regmap);
761 * The reset bit is obviously volatile, but we need to be able to cache
762 * the other bits in the register, so we can't just mark the whole
763 * register as volatile. Since this is the only place where we'll ever
764 * touch the reset bit just bypass the cache for this operation.
766 regcache_cache_bypass(ssm2518->regmap, true);
767 ret = regmap_write(ssm2518->regmap, SSM2518_REG_POWER1,
768 SSM2518_POWER1_RESET);
769 regcache_cache_bypass(ssm2518->regmap, false);
773 ret = regmap_update_bits(ssm2518->regmap, SSM2518_REG_POWER2,
774 SSM2518_POWER2_APWDN, 0x00);
778 ret = ssm2518_set_power(ssm2518, false);
782 return devm_snd_soc_register_component(&i2c->dev,
783 &ssm2518_component_driver,
788 static const struct of_device_id ssm2518_dt_ids[] = {
789 { .compatible = "adi,ssm2518", },
792 MODULE_DEVICE_TABLE(of, ssm2518_dt_ids);
795 static const struct i2c_device_id ssm2518_i2c_ids[] = {
799 MODULE_DEVICE_TABLE(i2c, ssm2518_i2c_ids);
801 static struct i2c_driver ssm2518_driver = {
804 .of_match_table = of_match_ptr(ssm2518_dt_ids),
806 .probe = ssm2518_i2c_probe,
807 .id_table = ssm2518_i2c_ids,
809 module_i2c_driver(ssm2518_driver);
811 MODULE_DESCRIPTION("ASoC SSM2518 driver");
813 MODULE_LICENSE("GPL");