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);
133 /* set master/slave audio interface */
134 bits = snd_soc_component_read32(component, AK5558_02_CONTROL1);
135 bits &= ~AK5558_BITS;
139 bits |= AK5558_DIF_24BIT_MODE;
142 bits |= AK5558_DIF_32BIT_MODE;
148 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
153 static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
155 struct snd_soc_component *component = dai->component;
158 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
159 case SND_SOC_DAIFMT_CBS_CFS:
161 case SND_SOC_DAIFMT_CBM_CFM:
163 case SND_SOC_DAIFMT_CBS_CFM:
164 case SND_SOC_DAIFMT_CBM_CFS:
166 dev_err(dai->dev, "Clock mode unsupported");
170 /* set master/slave audio interface */
171 format = snd_soc_component_read32(component, AK5558_02_CONTROL1);
172 format &= ~AK5558_DIF;
174 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
175 case SND_SOC_DAIFMT_I2S:
176 format |= AK5558_DIF_I2S_MODE;
178 case SND_SOC_DAIFMT_LEFT_J:
179 format |= AK5558_DIF_MSB_MODE;
181 case SND_SOC_DAIFMT_DSP_B:
182 format |= AK5558_DIF_MSB_MODE;
188 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
193 static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
194 unsigned int rx_mask, int slots,
197 struct snd_soc_component *component = dai->component;
198 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
201 ak5558->slots = slots;
202 ak5558->slot_width = slot_width;
204 switch (slots * slot_width) {
206 tdm_mode = AK5558_MODE_TDM128;
209 tdm_mode = AK5558_MODE_TDM256;
212 tdm_mode = AK5558_MODE_TDM512;
215 tdm_mode = AK5558_MODE_NORMAL;
219 snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
224 #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
225 SNDRV_PCM_FMTBIT_S24_LE |\
226 SNDRV_PCM_FMTBIT_S32_LE)
228 static const unsigned int ak5558_rates[] = {
229 8000, 11025, 16000, 22050,
230 32000, 44100, 48000, 88200,
231 96000, 176400, 192000, 352800,
232 384000, 705600, 768000, 1411200,
236 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
237 .count = ARRAY_SIZE(ak5558_rates),
238 .list = ak5558_rates,
241 static int ak5558_startup(struct snd_pcm_substream *substream,
242 struct snd_soc_dai *dai)
244 return snd_pcm_hw_constraint_list(substream->runtime, 0,
245 SNDRV_PCM_HW_PARAM_RATE,
246 &ak5558_rate_constraints);
249 static struct snd_soc_dai_ops ak5558_dai_ops = {
250 .startup = ak5558_startup,
251 .hw_params = ak5558_hw_params,
253 .set_fmt = ak5558_set_dai_fmt,
254 .set_tdm_slot = ak5558_set_tdm_slot,
257 static struct snd_soc_dai_driver ak5558_dai = {
258 .name = "ak5558-aif",
260 .stream_name = "Capture",
263 .rates = SNDRV_PCM_RATE_KNOT,
264 .formats = AK5558_FORMATS,
266 .ops = &ak5558_dai_ops,
269 static void ak5558_power_off(struct ak5558_priv *ak5558)
271 if (!ak5558->reset_gpiod)
274 gpiod_set_value_cansleep(ak5558->reset_gpiod, 0);
275 usleep_range(1000, 2000);
278 static void ak5558_power_on(struct ak5558_priv *ak5558)
280 if (!ak5558->reset_gpiod)
283 gpiod_set_value_cansleep(ak5558->reset_gpiod, 1);
284 usleep_range(1000, 2000);
287 static int ak5558_probe(struct snd_soc_component *component)
289 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
291 ak5558_power_on(ak5558);
292 return ak5558_set_mcki(component);
295 static void ak5558_remove(struct snd_soc_component *component)
297 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
299 ak5558_power_off(ak5558);
302 static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
304 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
306 regcache_cache_only(ak5558->regmap, true);
307 ak5558_power_off(ak5558);
312 static int __maybe_unused ak5558_runtime_resume(struct device *dev)
314 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
316 ak5558_power_off(ak5558);
317 ak5558_power_on(ak5558);
319 regcache_cache_only(ak5558->regmap, false);
320 regcache_mark_dirty(ak5558->regmap);
322 return regcache_sync(ak5558->regmap);
325 static const struct dev_pm_ops ak5558_pm = {
326 SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
327 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
328 pm_runtime_force_resume)
331 static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
332 .probe = ak5558_probe,
333 .remove = ak5558_remove,
334 .controls = ak5558_snd_controls,
335 .num_controls = ARRAY_SIZE(ak5558_snd_controls),
336 .dapm_widgets = ak5558_dapm_widgets,
337 .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets),
338 .dapm_routes = ak5558_intercon,
339 .num_dapm_routes = ARRAY_SIZE(ak5558_intercon),
341 .use_pmdown_time = 1,
343 .non_legacy_dai_naming = 1,
346 static const struct regmap_config ak5558_regmap = {
350 .max_register = AK5558_05_DSD,
351 .reg_defaults = ak5558_reg,
352 .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
353 .cache_type = REGCACHE_RBTREE,
356 static int ak5558_i2c_probe(struct i2c_client *i2c)
358 struct ak5558_priv *ak5558;
361 ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
365 ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
366 if (IS_ERR(ak5558->regmap))
367 return PTR_ERR(ak5558->regmap);
369 i2c_set_clientdata(i2c, ak5558);
372 ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
374 if (IS_ERR(ak5558->reset_gpiod))
375 return PTR_ERR(ak5558->reset_gpiod);
377 ret = devm_snd_soc_register_component(&i2c->dev,
378 &soc_codec_dev_ak5558,
383 pm_runtime_enable(&i2c->dev);
388 static int ak5558_i2c_remove(struct i2c_client *i2c)
390 pm_runtime_disable(&i2c->dev);
395 static const struct of_device_id ak5558_i2c_dt_ids[] = {
396 { .compatible = "asahi-kasei,ak5558"},
400 static struct i2c_driver ak5558_i2c_driver = {
403 .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
406 .probe_new = ak5558_i2c_probe,
407 .remove = ak5558_i2c_remove,
410 module_i2c_driver(ak5558_i2c_driver);
412 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
413 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
414 MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
415 MODULE_LICENSE("GPL v2");