1 // SPDX-License-Identifier: GPL-2.0
3 // Audio driver for AK5558 ADC
5 // Copyright (C) 2015 Asahi Kasei Microdevices Corporation
8 #include <linux/delay.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/regmap.h>
14 #include <linux/slab.h>
16 #include <sound/initval.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/soc-dapm.h>
21 #include <sound/tlv.h>
25 /* AK5558 Codec Private Data */
27 struct snd_soc_component component
;
28 struct regmap
*regmap
;
29 struct i2c_client
*i2c
;
30 struct gpio_desc
*reset_gpiod
; /* Reset & Power down GPIO */
35 /* ak5558 register cache & default register settings */
36 static const struct reg_default ak5558_reg
[] = {
37 { 0x0, 0xFF }, /* 0x00 AK5558_00_POWER_MANAGEMENT1 */
38 { 0x1, 0x01 }, /* 0x01 AK5558_01_POWER_MANAGEMENT2 */
39 { 0x2, 0x01 }, /* 0x02 AK5558_02_CONTROL1 */
40 { 0x3, 0x00 }, /* 0x03 AK5558_03_CONTROL2 */
41 { 0x4, 0x00 }, /* 0x04 AK5558_04_CONTROL3 */
42 { 0x5, 0x00 } /* 0x05 AK5558_05_DSD */
45 static const char * const mono_texts
[] = {
46 "8 Slot", "2 Slot", "4 Slot", "1 Slot",
49 static const struct soc_enum ak5558_mono_enum
[] = {
50 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2
, 1,
51 ARRAY_SIZE(mono_texts
), mono_texts
),
54 static const char * const digfil_texts
[] = {
55 "Sharp Roll-Off", "Show Roll-Off",
56 "Short Delay Sharp Roll-Off", "Short Delay Show Roll-Off",
59 static const struct soc_enum ak5558_adcset_enum
[] = {
60 SOC_ENUM_SINGLE(AK5558_04_CONTROL3
, 0,
61 ARRAY_SIZE(digfil_texts
), digfil_texts
),
64 static const struct snd_kcontrol_new ak5558_snd_controls
[] = {
65 SOC_ENUM("AK5558 Monaural Mode", ak5558_mono_enum
[0]),
66 SOC_ENUM("AK5558 Digital Filter", ak5558_adcset_enum
[0]),
69 static const struct snd_soc_dapm_widget ak5558_dapm_widgets
[] = {
71 SND_SOC_DAPM_INPUT("AIN1"),
72 SND_SOC_DAPM_INPUT("AIN2"),
73 SND_SOC_DAPM_INPUT("AIN3"),
74 SND_SOC_DAPM_INPUT("AIN4"),
75 SND_SOC_DAPM_INPUT("AIN5"),
76 SND_SOC_DAPM_INPUT("AIN6"),
77 SND_SOC_DAPM_INPUT("AIN7"),
78 SND_SOC_DAPM_INPUT("AIN8"),
80 SND_SOC_DAPM_ADC("ADC Ch1", NULL
, AK5558_00_POWER_MANAGEMENT1
, 0, 0),
81 SND_SOC_DAPM_ADC("ADC Ch2", NULL
, AK5558_00_POWER_MANAGEMENT1
, 1, 0),
82 SND_SOC_DAPM_ADC("ADC Ch3", NULL
, AK5558_00_POWER_MANAGEMENT1
, 2, 0),
83 SND_SOC_DAPM_ADC("ADC Ch4", NULL
, AK5558_00_POWER_MANAGEMENT1
, 3, 0),
84 SND_SOC_DAPM_ADC("ADC Ch5", NULL
, AK5558_00_POWER_MANAGEMENT1
, 4, 0),
85 SND_SOC_DAPM_ADC("ADC Ch6", NULL
, AK5558_00_POWER_MANAGEMENT1
, 5, 0),
86 SND_SOC_DAPM_ADC("ADC Ch7", NULL
, AK5558_00_POWER_MANAGEMENT1
, 6, 0),
87 SND_SOC_DAPM_ADC("ADC Ch8", NULL
, AK5558_00_POWER_MANAGEMENT1
, 7, 0),
89 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM
, 0, 0),
92 static const struct snd_soc_dapm_route ak5558_intercon
[] = {
93 {"ADC Ch1", NULL
, "AIN1"},
94 {"SDTO", NULL
, "ADC Ch1"},
96 {"ADC Ch2", NULL
, "AIN2"},
97 {"SDTO", NULL
, "ADC Ch2"},
99 {"ADC Ch3", NULL
, "AIN3"},
100 {"SDTO", NULL
, "ADC Ch3"},
102 {"ADC Ch4", NULL
, "AIN4"},
103 {"SDTO", NULL
, "ADC Ch4"},
105 {"ADC Ch5", NULL
, "AIN5"},
106 {"SDTO", NULL
, "ADC Ch5"},
108 {"ADC Ch6", NULL
, "AIN6"},
109 {"SDTO", NULL
, "ADC Ch6"},
111 {"ADC Ch7", NULL
, "AIN7"},
112 {"SDTO", NULL
, "ADC Ch7"},
114 {"ADC Ch8", NULL
, "AIN8"},
115 {"SDTO", NULL
, "ADC Ch8"},
118 static int ak5558_set_mcki(struct snd_soc_component
*component
)
120 return snd_soc_component_update_bits(component
, AK5558_02_CONTROL1
, AK5558_CKS
,
124 static int ak5558_hw_params(struct snd_pcm_substream
*substream
,
125 struct snd_pcm_hw_params
*params
,
126 struct snd_soc_dai
*dai
)
128 struct snd_soc_component
*component
= dai
->component
;
129 struct ak5558_priv
*ak5558
= snd_soc_component_get_drvdata(component
);
131 int pcm_width
= max(params_physical_width(params
), ak5558
->slot_width
);
135 bits
= AK5558_DIF_24BIT_MODE
;
138 bits
= AK5558_DIF_32BIT_MODE
;
144 snd_soc_component_update_bits(component
, AK5558_02_CONTROL1
, AK5558_BITS
, bits
);
149 static int ak5558_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
151 struct snd_soc_component
*component
= dai
->component
;
154 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
155 case SND_SOC_DAIFMT_CBS_CFS
:
157 case SND_SOC_DAIFMT_CBM_CFM
:
159 case SND_SOC_DAIFMT_CBS_CFM
:
160 case SND_SOC_DAIFMT_CBM_CFS
:
162 dev_err(dai
->dev
, "Clock mode unsupported");
166 /* set master/slave audio interface */
167 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
168 case SND_SOC_DAIFMT_I2S
:
169 format
= AK5558_DIF_I2S_MODE
;
171 case SND_SOC_DAIFMT_LEFT_J
:
172 format
= AK5558_DIF_MSB_MODE
;
174 case SND_SOC_DAIFMT_DSP_B
:
175 format
= AK5558_DIF_MSB_MODE
;
181 snd_soc_component_update_bits(component
, AK5558_02_CONTROL1
, AK5558_DIF
, format
);
186 static int ak5558_set_tdm_slot(struct snd_soc_dai
*dai
, unsigned int tx_mask
,
187 unsigned int rx_mask
, int slots
,
190 struct snd_soc_component
*component
= dai
->component
;
191 struct ak5558_priv
*ak5558
= snd_soc_component_get_drvdata(component
);
194 ak5558
->slots
= slots
;
195 ak5558
->slot_width
= slot_width
;
197 switch (slots
* slot_width
) {
199 tdm_mode
= AK5558_MODE_TDM128
;
202 tdm_mode
= AK5558_MODE_TDM256
;
205 tdm_mode
= AK5558_MODE_TDM512
;
208 tdm_mode
= AK5558_MODE_NORMAL
;
212 snd_soc_component_update_bits(component
, AK5558_03_CONTROL2
, AK5558_MODE_BITS
,
217 #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
218 SNDRV_PCM_FMTBIT_S24_LE |\
219 SNDRV_PCM_FMTBIT_S32_LE)
221 static const unsigned int ak5558_rates
[] = {
222 8000, 11025, 16000, 22050,
223 32000, 44100, 48000, 88200,
224 96000, 176400, 192000, 352800,
225 384000, 705600, 768000, 1411200,
229 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints
= {
230 .count
= ARRAY_SIZE(ak5558_rates
),
231 .list
= ak5558_rates
,
234 static int ak5558_startup(struct snd_pcm_substream
*substream
,
235 struct snd_soc_dai
*dai
)
237 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
238 SNDRV_PCM_HW_PARAM_RATE
,
239 &ak5558_rate_constraints
);
242 static const struct snd_soc_dai_ops ak5558_dai_ops
= {
243 .startup
= ak5558_startup
,
244 .hw_params
= ak5558_hw_params
,
246 .set_fmt
= ak5558_set_dai_fmt
,
247 .set_tdm_slot
= ak5558_set_tdm_slot
,
250 static struct snd_soc_dai_driver ak5558_dai
= {
251 .name
= "ak5558-aif",
253 .stream_name
= "Capture",
256 .rates
= SNDRV_PCM_RATE_KNOT
,
257 .formats
= AK5558_FORMATS
,
259 .ops
= &ak5558_dai_ops
,
262 static void ak5558_power_off(struct ak5558_priv
*ak5558
)
264 if (!ak5558
->reset_gpiod
)
267 gpiod_set_value_cansleep(ak5558
->reset_gpiod
, 0);
268 usleep_range(1000, 2000);
271 static void ak5558_power_on(struct ak5558_priv
*ak5558
)
273 if (!ak5558
->reset_gpiod
)
276 gpiod_set_value_cansleep(ak5558
->reset_gpiod
, 1);
277 usleep_range(1000, 2000);
280 static int ak5558_probe(struct snd_soc_component
*component
)
282 struct ak5558_priv
*ak5558
= snd_soc_component_get_drvdata(component
);
284 ak5558_power_on(ak5558
);
285 return ak5558_set_mcki(component
);
288 static void ak5558_remove(struct snd_soc_component
*component
)
290 struct ak5558_priv
*ak5558
= snd_soc_component_get_drvdata(component
);
292 ak5558_power_off(ak5558
);
295 static int __maybe_unused
ak5558_runtime_suspend(struct device
*dev
)
297 struct ak5558_priv
*ak5558
= dev_get_drvdata(dev
);
299 regcache_cache_only(ak5558
->regmap
, true);
300 ak5558_power_off(ak5558
);
305 static int __maybe_unused
ak5558_runtime_resume(struct device
*dev
)
307 struct ak5558_priv
*ak5558
= dev_get_drvdata(dev
);
309 ak5558_power_off(ak5558
);
310 ak5558_power_on(ak5558
);
312 regcache_cache_only(ak5558
->regmap
, false);
313 regcache_mark_dirty(ak5558
->regmap
);
315 return regcache_sync(ak5558
->regmap
);
318 static const struct dev_pm_ops ak5558_pm
= {
319 SET_RUNTIME_PM_OPS(ak5558_runtime_suspend
, ak5558_runtime_resume
, NULL
)
320 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
321 pm_runtime_force_resume
)
324 static const struct snd_soc_component_driver soc_codec_dev_ak5558
= {
325 .probe
= ak5558_probe
,
326 .remove
= ak5558_remove
,
327 .controls
= ak5558_snd_controls
,
328 .num_controls
= ARRAY_SIZE(ak5558_snd_controls
),
329 .dapm_widgets
= ak5558_dapm_widgets
,
330 .num_dapm_widgets
= ARRAY_SIZE(ak5558_dapm_widgets
),
331 .dapm_routes
= ak5558_intercon
,
332 .num_dapm_routes
= ARRAY_SIZE(ak5558_intercon
),
334 .use_pmdown_time
= 1,
336 .non_legacy_dai_naming
= 1,
339 static const struct regmap_config ak5558_regmap
= {
343 .max_register
= AK5558_05_DSD
,
344 .reg_defaults
= ak5558_reg
,
345 .num_reg_defaults
= ARRAY_SIZE(ak5558_reg
),
346 .cache_type
= REGCACHE_RBTREE
,
349 static int ak5558_i2c_probe(struct i2c_client
*i2c
)
351 struct ak5558_priv
*ak5558
;
354 ak5558
= devm_kzalloc(&i2c
->dev
, sizeof(*ak5558
), GFP_KERNEL
);
358 ak5558
->regmap
= devm_regmap_init_i2c(i2c
, &ak5558_regmap
);
359 if (IS_ERR(ak5558
->regmap
))
360 return PTR_ERR(ak5558
->regmap
);
362 i2c_set_clientdata(i2c
, ak5558
);
365 ak5558
->reset_gpiod
= devm_gpiod_get_optional(&i2c
->dev
, "reset",
367 if (IS_ERR(ak5558
->reset_gpiod
))
368 return PTR_ERR(ak5558
->reset_gpiod
);
370 ret
= devm_snd_soc_register_component(&i2c
->dev
,
371 &soc_codec_dev_ak5558
,
376 pm_runtime_enable(&i2c
->dev
);
381 static int ak5558_i2c_remove(struct i2c_client
*i2c
)
383 pm_runtime_disable(&i2c
->dev
);
388 static const struct of_device_id ak5558_i2c_dt_ids
[] = {
389 { .compatible
= "asahi-kasei,ak5558"},
393 static struct i2c_driver ak5558_i2c_driver
= {
396 .of_match_table
= of_match_ptr(ak5558_i2c_dt_ids
),
399 .probe_new
= ak5558_i2c_probe
,
400 .remove
= ak5558_i2c_remove
,
403 module_i2c_driver(ak5558_i2c_driver
);
405 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
406 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
407 MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
408 MODULE_LICENSE("GPL v2");