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/of_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/slab.h>
18 #include <sound/initval.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dapm.h>
23 #include <sound/tlv.h>
32 #define AK5558_NUM_SUPPLIES 2
33 static const char *ak5558_supply_names[AK5558_NUM_SUPPLIES] = {
38 /* AK5558 Codec Private Data */
40 struct regulator_bulk_data supplies[AK5558_NUM_SUPPLIES];
41 struct snd_soc_component component;
42 struct regmap *regmap;
43 struct i2c_client *i2c;
44 struct gpio_desc *reset_gpiod; /* Reset & Power down GPIO */
49 /* ak5558 register cache & default register settings */
50 static const struct reg_default ak5558_reg[] = {
51 { 0x0, 0xFF }, /* 0x00 AK5558_00_POWER_MANAGEMENT1 */
52 { 0x1, 0x01 }, /* 0x01 AK5558_01_POWER_MANAGEMENT2 */
53 { 0x2, 0x01 }, /* 0x02 AK5558_02_CONTROL1 */
54 { 0x3, 0x00 }, /* 0x03 AK5558_03_CONTROL2 */
55 { 0x4, 0x00 }, /* 0x04 AK5558_04_CONTROL3 */
56 { 0x5, 0x00 } /* 0x05 AK5558_05_DSD */
59 static const char * const mono_texts[] = {
60 "8 Slot", "2 Slot", "4 Slot", "1 Slot",
63 static const struct soc_enum ak5558_mono_enum[] = {
64 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
65 ARRAY_SIZE(mono_texts), mono_texts),
68 static const char * const mono_5552_texts[] = {
69 "2 Slot", "1 Slot (Fixed)", "2 Slot", "1 Slot (Optimal)",
72 static const struct soc_enum ak5552_mono_enum[] = {
73 SOC_ENUM_SINGLE(AK5558_01_POWER_MANAGEMENT2, 1,
74 ARRAY_SIZE(mono_5552_texts), mono_5552_texts),
77 static const char * const digfil_texts[] = {
78 "Sharp Roll-Off", "Slow Roll-Off",
79 "Short Delay Sharp Roll-Off", "Short Delay Slow Roll-Off",
82 static const struct soc_enum ak5558_adcset_enum[] = {
83 SOC_ENUM_SINGLE(AK5558_04_CONTROL3, 0,
84 ARRAY_SIZE(digfil_texts), digfil_texts),
87 static const struct snd_kcontrol_new ak5558_snd_controls[] = {
88 SOC_ENUM("Monaural Mode", ak5558_mono_enum[0]),
89 SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
92 static const struct snd_kcontrol_new ak5552_snd_controls[] = {
93 SOC_ENUM("Monaural Mode", ak5552_mono_enum[0]),
94 SOC_ENUM("Digital Filter", ak5558_adcset_enum[0]),
97 static const struct snd_soc_dapm_widget ak5558_dapm_widgets[] = {
99 SND_SOC_DAPM_INPUT("AIN1"),
100 SND_SOC_DAPM_INPUT("AIN2"),
101 SND_SOC_DAPM_INPUT("AIN3"),
102 SND_SOC_DAPM_INPUT("AIN4"),
103 SND_SOC_DAPM_INPUT("AIN5"),
104 SND_SOC_DAPM_INPUT("AIN6"),
105 SND_SOC_DAPM_INPUT("AIN7"),
106 SND_SOC_DAPM_INPUT("AIN8"),
108 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
109 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
110 SND_SOC_DAPM_ADC("ADC Ch3", NULL, AK5558_00_POWER_MANAGEMENT1, 2, 0),
111 SND_SOC_DAPM_ADC("ADC Ch4", NULL, AK5558_00_POWER_MANAGEMENT1, 3, 0),
112 SND_SOC_DAPM_ADC("ADC Ch5", NULL, AK5558_00_POWER_MANAGEMENT1, 4, 0),
113 SND_SOC_DAPM_ADC("ADC Ch6", NULL, AK5558_00_POWER_MANAGEMENT1, 5, 0),
114 SND_SOC_DAPM_ADC("ADC Ch7", NULL, AK5558_00_POWER_MANAGEMENT1, 6, 0),
115 SND_SOC_DAPM_ADC("ADC Ch8", NULL, AK5558_00_POWER_MANAGEMENT1, 7, 0),
117 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
120 static const struct snd_soc_dapm_widget ak5552_dapm_widgets[] = {
122 SND_SOC_DAPM_INPUT("AIN1"),
123 SND_SOC_DAPM_INPUT("AIN2"),
125 SND_SOC_DAPM_ADC("ADC Ch1", NULL, AK5558_00_POWER_MANAGEMENT1, 0, 0),
126 SND_SOC_DAPM_ADC("ADC Ch2", NULL, AK5558_00_POWER_MANAGEMENT1, 1, 0),
128 SND_SOC_DAPM_AIF_OUT("SDTO", "Capture", 0, SND_SOC_NOPM, 0, 0),
131 static const struct snd_soc_dapm_route ak5558_intercon[] = {
132 {"ADC Ch1", NULL, "AIN1"},
133 {"SDTO", NULL, "ADC Ch1"},
135 {"ADC Ch2", NULL, "AIN2"},
136 {"SDTO", NULL, "ADC Ch2"},
138 {"ADC Ch3", NULL, "AIN3"},
139 {"SDTO", NULL, "ADC Ch3"},
141 {"ADC Ch4", NULL, "AIN4"},
142 {"SDTO", NULL, "ADC Ch4"},
144 {"ADC Ch5", NULL, "AIN5"},
145 {"SDTO", NULL, "ADC Ch5"},
147 {"ADC Ch6", NULL, "AIN6"},
148 {"SDTO", NULL, "ADC Ch6"},
150 {"ADC Ch7", NULL, "AIN7"},
151 {"SDTO", NULL, "ADC Ch7"},
153 {"ADC Ch8", NULL, "AIN8"},
154 {"SDTO", NULL, "ADC Ch8"},
157 static const struct snd_soc_dapm_route ak5552_intercon[] = {
158 {"ADC Ch1", NULL, "AIN1"},
159 {"SDTO", NULL, "ADC Ch1"},
161 {"ADC Ch2", NULL, "AIN2"},
162 {"SDTO", NULL, "ADC Ch2"},
165 static int ak5558_set_mcki(struct snd_soc_component *component)
167 return snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_CKS,
171 static int ak5558_hw_params(struct snd_pcm_substream *substream,
172 struct snd_pcm_hw_params *params,
173 struct snd_soc_dai *dai)
175 struct snd_soc_component *component = dai->component;
176 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
178 int pcm_width = max(params_physical_width(params), ak5558->slot_width);
182 bits = AK5558_DIF_24BIT_MODE;
185 bits = AK5558_DIF_32BIT_MODE;
191 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_BITS, bits);
196 static int ak5558_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
198 struct snd_soc_component *component = dai->component;
201 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
202 case SND_SOC_DAIFMT_CBC_CFC:
204 case SND_SOC_DAIFMT_CBP_CFP:
206 case SND_SOC_DAIFMT_CBC_CFP:
207 case SND_SOC_DAIFMT_CBP_CFC:
209 dev_err(dai->dev, "Clock mode unsupported");
213 /* set master/slave audio interface */
214 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
215 case SND_SOC_DAIFMT_I2S:
216 format = AK5558_DIF_I2S_MODE;
218 case SND_SOC_DAIFMT_LEFT_J:
219 format = AK5558_DIF_MSB_MODE;
221 case SND_SOC_DAIFMT_DSP_B:
222 format = AK5558_DIF_MSB_MODE;
228 snd_soc_component_update_bits(component, AK5558_02_CONTROL1, AK5558_DIF, format);
233 static int ak5558_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
234 unsigned int rx_mask, int slots,
237 struct snd_soc_component *component = dai->component;
238 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
241 ak5558->slots = slots;
242 ak5558->slot_width = slot_width;
244 switch (slots * slot_width) {
246 tdm_mode = AK5558_MODE_TDM128;
249 tdm_mode = AK5558_MODE_TDM256;
252 tdm_mode = AK5558_MODE_TDM512;
255 tdm_mode = AK5558_MODE_NORMAL;
259 snd_soc_component_update_bits(component, AK5558_03_CONTROL2, AK5558_MODE_BITS,
264 #define AK5558_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
265 SNDRV_PCM_FMTBIT_S24_LE |\
266 SNDRV_PCM_FMTBIT_S32_LE)
268 static const unsigned int ak5558_rates[] = {
269 8000, 11025, 16000, 22050,
270 32000, 44100, 48000, 88200,
271 96000, 176400, 192000, 352800,
272 384000, 705600, 768000, 1411200,
276 static const struct snd_pcm_hw_constraint_list ak5558_rate_constraints = {
277 .count = ARRAY_SIZE(ak5558_rates),
278 .list = ak5558_rates,
281 static int ak5558_startup(struct snd_pcm_substream *substream,
282 struct snd_soc_dai *dai)
284 return snd_pcm_hw_constraint_list(substream->runtime, 0,
285 SNDRV_PCM_HW_PARAM_RATE,
286 &ak5558_rate_constraints);
289 static const struct snd_soc_dai_ops ak5558_dai_ops = {
290 .startup = ak5558_startup,
291 .hw_params = ak5558_hw_params,
293 .set_fmt = ak5558_set_dai_fmt,
294 .set_tdm_slot = ak5558_set_tdm_slot,
297 static struct snd_soc_dai_driver ak5558_dai = {
298 .name = "ak5558-aif",
300 .stream_name = "Capture",
303 .rates = SNDRV_PCM_RATE_KNOT,
304 .formats = AK5558_FORMATS,
306 .ops = &ak5558_dai_ops,
309 static struct snd_soc_dai_driver ak5552_dai = {
310 .name = "ak5552-aif",
312 .stream_name = "Capture",
315 .rates = SNDRV_PCM_RATE_KNOT,
316 .formats = AK5558_FORMATS,
318 .ops = &ak5558_dai_ops,
321 static void ak5558_reset(struct ak5558_priv *ak5558, bool active)
323 if (!ak5558->reset_gpiod)
326 gpiod_set_value_cansleep(ak5558->reset_gpiod, active);
327 usleep_range(1000, 2000);
330 static int ak5558_probe(struct snd_soc_component *component)
332 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
334 ak5558_reset(ak5558, false);
335 return ak5558_set_mcki(component);
338 static void ak5558_remove(struct snd_soc_component *component)
340 struct ak5558_priv *ak5558 = snd_soc_component_get_drvdata(component);
342 ak5558_reset(ak5558, true);
345 static int __maybe_unused ak5558_runtime_suspend(struct device *dev)
347 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
349 regcache_cache_only(ak5558->regmap, true);
350 ak5558_reset(ak5558, true);
352 regulator_bulk_disable(ARRAY_SIZE(ak5558->supplies),
357 static int __maybe_unused ak5558_runtime_resume(struct device *dev)
359 struct ak5558_priv *ak5558 = dev_get_drvdata(dev);
362 ret = regulator_bulk_enable(ARRAY_SIZE(ak5558->supplies),
365 dev_err(dev, "Failed to enable supplies: %d\n", ret);
369 ak5558_reset(ak5558, true);
370 ak5558_reset(ak5558, false);
372 regcache_cache_only(ak5558->regmap, false);
373 regcache_mark_dirty(ak5558->regmap);
375 return regcache_sync(ak5558->regmap);
378 static const struct dev_pm_ops ak5558_pm = {
379 SET_RUNTIME_PM_OPS(ak5558_runtime_suspend, ak5558_runtime_resume, NULL)
380 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
381 pm_runtime_force_resume)
384 static const struct snd_soc_component_driver soc_codec_dev_ak5558 = {
385 .probe = ak5558_probe,
386 .remove = ak5558_remove,
387 .controls = ak5558_snd_controls,
388 .num_controls = ARRAY_SIZE(ak5558_snd_controls),
389 .dapm_widgets = ak5558_dapm_widgets,
390 .num_dapm_widgets = ARRAY_SIZE(ak5558_dapm_widgets),
391 .dapm_routes = ak5558_intercon,
392 .num_dapm_routes = ARRAY_SIZE(ak5558_intercon),
394 .use_pmdown_time = 1,
396 .non_legacy_dai_naming = 1,
399 static const struct snd_soc_component_driver soc_codec_dev_ak5552 = {
400 .probe = ak5558_probe,
401 .remove = ak5558_remove,
402 .controls = ak5552_snd_controls,
403 .num_controls = ARRAY_SIZE(ak5552_snd_controls),
404 .dapm_widgets = ak5552_dapm_widgets,
405 .num_dapm_widgets = ARRAY_SIZE(ak5552_dapm_widgets),
406 .dapm_routes = ak5552_intercon,
407 .num_dapm_routes = ARRAY_SIZE(ak5552_intercon),
409 .use_pmdown_time = 1,
411 .non_legacy_dai_naming = 1,
414 static const struct regmap_config ak5558_regmap = {
418 .max_register = AK5558_05_DSD,
419 .reg_defaults = ak5558_reg,
420 .num_reg_defaults = ARRAY_SIZE(ak5558_reg),
421 .cache_type = REGCACHE_RBTREE,
424 static int ak5558_i2c_probe(struct i2c_client *i2c)
426 struct ak5558_priv *ak5558;
431 ak5558 = devm_kzalloc(&i2c->dev, sizeof(*ak5558), GFP_KERNEL);
435 ak5558->regmap = devm_regmap_init_i2c(i2c, &ak5558_regmap);
436 if (IS_ERR(ak5558->regmap))
437 return PTR_ERR(ak5558->regmap);
439 i2c_set_clientdata(i2c, ak5558);
442 ak5558->reset_gpiod = devm_gpiod_get_optional(&i2c->dev, "reset",
444 if (IS_ERR(ak5558->reset_gpiod))
445 return PTR_ERR(ak5558->reset_gpiod);
447 for (i = 0; i < ARRAY_SIZE(ak5558->supplies); i++)
448 ak5558->supplies[i].supply = ak5558_supply_names[i];
450 ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(ak5558->supplies),
453 dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret);
457 dev_id = (uintptr_t)of_device_get_match_data(&i2c->dev);
460 ret = devm_snd_soc_register_component(&i2c->dev,
461 &soc_codec_dev_ak5552,
465 ret = devm_snd_soc_register_component(&i2c->dev,
466 &soc_codec_dev_ak5558,
470 dev_err(&i2c->dev, "unexpected device type\n");
474 dev_err(&i2c->dev, "failed to register component: %d\n", ret);
478 pm_runtime_enable(&i2c->dev);
479 regcache_cache_only(ak5558->regmap, true);
484 static int ak5558_i2c_remove(struct i2c_client *i2c)
486 pm_runtime_disable(&i2c->dev);
491 static const struct of_device_id ak5558_i2c_dt_ids[] __maybe_unused = {
492 { .compatible = "asahi-kasei,ak5558", .data = (void *) AK5558 },
493 { .compatible = "asahi-kasei,ak5552", .data = (void *) AK5552 },
496 MODULE_DEVICE_TABLE(of, ak5558_i2c_dt_ids);
498 static struct i2c_driver ak5558_i2c_driver = {
501 .of_match_table = of_match_ptr(ak5558_i2c_dt_ids),
504 .probe_new = ak5558_i2c_probe,
505 .remove = ak5558_i2c_remove,
508 module_i2c_driver(ak5558_i2c_driver);
510 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
511 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
512 MODULE_DESCRIPTION("ASoC AK5558 ADC driver");
513 MODULE_LICENSE("GPL v2");