1 // SPDX-License-Identifier: GPL-2.0
3 // Audio driver for AK4458 DAC
5 // Copyright (C) 2016 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/of_gpio.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/slab.h>
16 #include <sound/initval.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
20 #include <sound/tlv.h>
24 /* AK4458 Codec Private Data */
27 struct regmap *regmap;
28 struct gpio_desc *reset_gpiod;
29 struct gpio_desc *mute_gpiod;
30 int digfil; /* SSLOW, SD, SLOW bits */
31 int fs; /* sampling rate */
37 static const struct reg_default ak4458_reg_defaults[] = {
38 { 0x00, 0x0C }, /* 0x00 AK4458_00_CONTROL1 */
39 { 0x01, 0x22 }, /* 0x01 AK4458_01_CONTROL2 */
40 { 0x02, 0x00 }, /* 0x02 AK4458_02_CONTROL3 */
41 { 0x03, 0xFF }, /* 0x03 AK4458_03_LCHATT */
42 { 0x04, 0xFF }, /* 0x04 AK4458_04_RCHATT */
43 { 0x05, 0x00 }, /* 0x05 AK4458_05_CONTROL4 */
44 { 0x06, 0x00 }, /* 0x06 AK4458_06_DSD1 */
45 { 0x07, 0x03 }, /* 0x07 AK4458_07_CONTROL5 */
46 { 0x08, 0x00 }, /* 0x08 AK4458_08_SOUND_CONTROL */
47 { 0x09, 0x00 }, /* 0x09 AK4458_09_DSD2 */
48 { 0x0A, 0x0D }, /* 0x0A AK4458_0A_CONTROL6 */
49 { 0x0B, 0x0C }, /* 0x0B AK4458_0B_CONTROL7 */
50 { 0x0C, 0x00 }, /* 0x0C AK4458_0C_CONTROL8 */
51 { 0x0D, 0x00 }, /* 0x0D AK4458_0D_CONTROL9 */
52 { 0x0E, 0x50 }, /* 0x0E AK4458_0E_CONTROL10 */
53 { 0x0F, 0xFF }, /* 0x0F AK4458_0F_L2CHATT */
54 { 0x10, 0xFF }, /* 0x10 AK4458_10_R2CHATT */
55 { 0x11, 0xFF }, /* 0x11 AK4458_11_L3CHATT */
56 { 0x12, 0xFF }, /* 0x12 AK4458_12_R3CHATT */
57 { 0x13, 0xFF }, /* 0x13 AK4458_13_L4CHATT */
58 { 0x14, 0xFF }, /* 0x14 AK4458_14_R4CHATT */
63 * from -127 to 0 dB in 0.5 dB steps (mute instead of -127.5 dB)
65 static DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
68 * DEM1 bit DEM0 bit Mode
74 static const char * const ak4458_dem_select_texts[] = {
75 "44.1kHz", "OFF", "48kHz", "32kHz"
79 * SSLOW, SD, SLOW bits Digital Filter Setting
80 * 0, 0, 0 : Sharp Roll-Off Filter
81 * 0, 0, 1 : Slow Roll-Off Filter
82 * 0, 1, 0 : Short delay Sharp Roll-Off Filter
83 * 0, 1, 1 : Short delay Slow Roll-Off Filter
84 * 1, *, * : Super Slow Roll-Off Filter
86 static const char * const ak4458_digfil_select_texts[] = {
87 "Sharp Roll-Off Filter",
88 "Slow Roll-Off Filter",
89 "Short delay Sharp Roll-Off Filter",
90 "Short delay Slow Roll-Off Filter",
91 "Super Slow Roll-Off Filter"
95 * DZFB: Inverting Enable of DZF
96 * 0: DZF goes H at Zero Detection
97 * 1: DZF goes L at Zero Detection
99 static const char * const ak4458_dzfb_select_texts[] = {"H", "L"};
102 * SC1-0 bits: Sound Mode Setting
108 static const char * const ak4458_sc_select_texts[] = {
109 "Sound Mode 0", "Sound Mode 1", "Sound Mode 2"
112 /* FIR2-0 bits: FIR Filter Mode Setting */
113 static const char * const ak4458_fir_select_texts[] = {
114 "Mode 0", "Mode 1", "Mode 2", "Mode 3",
115 "Mode 4", "Mode 5", "Mode 6", "Mode 7",
118 /* ATS1-0 bits Attenuation Speed */
119 static const char * const ak4458_ats_select_texts[] = {
120 "4080/fs", "2040/fs", "510/fs", "255/fs",
123 /* DIF2 bit Audio Interface Format Setting(BICK fs) */
124 static const char * const ak4458_dif_select_texts[] = {"32fs,48fs", "64fs",};
126 static const struct soc_enum ak4458_dac1_dem_enum =
127 SOC_ENUM_SINGLE(AK4458_01_CONTROL2, 1,
128 ARRAY_SIZE(ak4458_dem_select_texts),
129 ak4458_dem_select_texts);
130 static const struct soc_enum ak4458_dac2_dem_enum =
131 SOC_ENUM_SINGLE(AK4458_0A_CONTROL6, 0,
132 ARRAY_SIZE(ak4458_dem_select_texts),
133 ak4458_dem_select_texts);
134 static const struct soc_enum ak4458_dac3_dem_enum =
135 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 4,
136 ARRAY_SIZE(ak4458_dem_select_texts),
137 ak4458_dem_select_texts);
138 static const struct soc_enum ak4458_dac4_dem_enum =
139 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 6,
140 ARRAY_SIZE(ak4458_dem_select_texts),
141 ak4458_dem_select_texts);
142 static const struct soc_enum ak4458_digfil_enum =
143 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ak4458_digfil_select_texts),
144 ak4458_digfil_select_texts);
145 static const struct soc_enum ak4458_dzfb_enum =
146 SOC_ENUM_SINGLE(AK4458_02_CONTROL3, 2,
147 ARRAY_SIZE(ak4458_dzfb_select_texts),
148 ak4458_dzfb_select_texts);
149 static const struct soc_enum ak4458_sm_enum =
150 SOC_ENUM_SINGLE(AK4458_08_SOUND_CONTROL, 0,
151 ARRAY_SIZE(ak4458_sc_select_texts),
152 ak4458_sc_select_texts);
153 static const struct soc_enum ak4458_fir_enum =
154 SOC_ENUM_SINGLE(AK4458_0C_CONTROL8, 0,
155 ARRAY_SIZE(ak4458_fir_select_texts),
156 ak4458_fir_select_texts);
157 static const struct soc_enum ak4458_ats_enum =
158 SOC_ENUM_SINGLE(AK4458_0B_CONTROL7, 6,
159 ARRAY_SIZE(ak4458_ats_select_texts),
160 ak4458_ats_select_texts);
161 static const struct soc_enum ak4458_dif_enum =
162 SOC_ENUM_SINGLE(AK4458_00_CONTROL1, 3,
163 ARRAY_SIZE(ak4458_dif_select_texts),
164 ak4458_dif_select_texts);
166 static int get_digfil(struct snd_kcontrol *kcontrol,
167 struct snd_ctl_elem_value *ucontrol)
169 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
170 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
172 ucontrol->value.enumerated.item[0] = ak4458->digfil;
177 static int set_digfil(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_value *ucontrol)
180 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
181 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
184 num = ucontrol->value.enumerated.item[0];
188 ak4458->digfil = num;
191 snd_soc_component_update_bits(component, AK4458_01_CONTROL2,
193 ((ak4458->digfil & 0x02) << 4));
196 snd_soc_component_update_bits(component, AK4458_02_CONTROL3,
198 (ak4458->digfil & 0x01));
200 /* write SSLOW bit */
201 snd_soc_component_update_bits(component, AK4458_05_CONTROL4,
203 ((ak4458->digfil & 0x04) >> 2));
208 static const struct snd_kcontrol_new ak4458_snd_controls[] = {
209 SOC_DOUBLE_R_TLV("DAC1 Playback Volume", AK4458_03_LCHATT,
210 AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
211 SOC_DOUBLE_R_TLV("DAC2 Playback Volume", AK4458_0F_L2CHATT,
212 AK4458_10_R2CHATT, 0, 0xFF, 0, dac_tlv),
213 SOC_DOUBLE_R_TLV("DAC3 Playback Volume", AK4458_11_L3CHATT,
214 AK4458_12_R3CHATT, 0, 0xFF, 0, dac_tlv),
215 SOC_DOUBLE_R_TLV("DAC4 Playback Volume", AK4458_13_L4CHATT,
216 AK4458_14_R4CHATT, 0, 0xFF, 0, dac_tlv),
217 SOC_ENUM("AK4458 De-emphasis Response DAC1", ak4458_dac1_dem_enum),
218 SOC_ENUM("AK4458 De-emphasis Response DAC2", ak4458_dac2_dem_enum),
219 SOC_ENUM("AK4458 De-emphasis Response DAC3", ak4458_dac3_dem_enum),
220 SOC_ENUM("AK4458 De-emphasis Response DAC4", ak4458_dac4_dem_enum),
221 SOC_ENUM_EXT("AK4458 Digital Filter Setting", ak4458_digfil_enum,
222 get_digfil, set_digfil),
223 SOC_ENUM("AK4458 Inverting Enable of DZFB", ak4458_dzfb_enum),
224 SOC_ENUM("AK4458 Sound Mode", ak4458_sm_enum),
225 SOC_ENUM("AK4458 FIR Filter Mode Setting", ak4458_fir_enum),
226 SOC_ENUM("AK4458 Attenuation transition Time Setting",
228 SOC_ENUM("AK4458 BICK fs Setting", ak4458_dif_enum),
231 /* ak4458 dapm widgets */
232 static const struct snd_soc_dapm_widget ak4458_dapm_widgets[] = {
233 SND_SOC_DAPM_DAC("AK4458 DAC1", NULL, AK4458_0A_CONTROL6, 2, 0),/*pw*/
234 SND_SOC_DAPM_AIF_IN("AK4458 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
235 SND_SOC_DAPM_OUTPUT("AK4458 AOUTA"),
237 SND_SOC_DAPM_DAC("AK4458 DAC2", NULL, AK4458_0A_CONTROL6, 3, 0),/*pw*/
238 SND_SOC_DAPM_OUTPUT("AK4458 AOUTB"),
240 SND_SOC_DAPM_DAC("AK4458 DAC3", NULL, AK4458_0B_CONTROL7, 2, 0),/*pw*/
241 SND_SOC_DAPM_OUTPUT("AK4458 AOUTC"),
243 SND_SOC_DAPM_DAC("AK4458 DAC4", NULL, AK4458_0B_CONTROL7, 3, 0),/*pw*/
244 SND_SOC_DAPM_OUTPUT("AK4458 AOUTD"),
247 static const struct snd_soc_dapm_route ak4458_intercon[] = {
248 {"AK4458 DAC1", NULL, "AK4458 SDTI"},
249 {"AK4458 AOUTA", NULL, "AK4458 DAC1"},
251 {"AK4458 DAC2", NULL, "AK4458 SDTI"},
252 {"AK4458 AOUTB", NULL, "AK4458 DAC2"},
254 {"AK4458 DAC3", NULL, "AK4458 SDTI"},
255 {"AK4458 AOUTC", NULL, "AK4458 DAC3"},
257 {"AK4458 DAC4", NULL, "AK4458 SDTI"},
258 {"AK4458 AOUTD", NULL, "AK4458 DAC4"},
261 static int ak4458_rstn_control(struct snd_soc_component *component, int bit)
266 ret = snd_soc_component_update_bits(component,
271 ret = snd_soc_component_update_bits(component,
281 static int ak4458_hw_params(struct snd_pcm_substream *substream,
282 struct snd_pcm_hw_params *params,
283 struct snd_soc_dai *dai)
285 struct snd_soc_component *component = dai->component;
286 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
287 int pcm_width = max(params_physical_width(params), ak4458->slot_width);
291 nfs1 = params_rate(params);
294 /* Master Clock Frequency Auto Setting Mode Enable */
295 snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 0x80, 0x80);
299 if (ak4458->fmt == SND_SOC_DAIFMT_I2S)
300 format = AK4458_DIF_24BIT_I2S;
302 format = AK4458_DIF_16BIT_LSB;
305 switch (ak4458->fmt) {
306 case SND_SOC_DAIFMT_I2S:
307 format = AK4458_DIF_32BIT_I2S;
309 case SND_SOC_DAIFMT_LEFT_J:
310 format = AK4458_DIF_32BIT_MSB;
312 case SND_SOC_DAIFMT_RIGHT_J:
313 format = AK4458_DIF_32BIT_LSB;
315 case SND_SOC_DAIFMT_DSP_B:
316 format = AK4458_DIF_32BIT_MSB;
326 snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
327 AK4458_DIF_MASK, format);
329 ak4458_rstn_control(component, 0);
330 ak4458_rstn_control(component, 1);
335 static int ak4458_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
337 struct snd_soc_component *component = dai->component;
338 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
340 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
341 case SND_SOC_DAIFMT_CBS_CFS: /* Slave Mode */
343 case SND_SOC_DAIFMT_CBM_CFM: /* Master Mode is not supported */
344 case SND_SOC_DAIFMT_CBS_CFM:
345 case SND_SOC_DAIFMT_CBM_CFS:
347 dev_err(component->dev, "Master mode unsupported\n");
351 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
352 case SND_SOC_DAIFMT_I2S:
353 case SND_SOC_DAIFMT_LEFT_J:
354 case SND_SOC_DAIFMT_RIGHT_J:
355 case SND_SOC_DAIFMT_DSP_B:
356 ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
359 dev_err(component->dev, "Audio format 0x%02X unsupported\n",
360 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
364 ak4458_rstn_control(component, 0);
365 ak4458_rstn_control(component, 1);
370 static const int att_speed[] = { 4080, 2040, 510, 255 };
372 static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute)
374 struct snd_soc_component *component = dai->component;
375 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
376 int nfs, ndt, ret, reg;
381 reg = snd_soc_component_read32(component, AK4458_0B_CONTROL7);
382 ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT;
384 ndt = att_speed[ats] / (nfs / 1000);
387 ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 1);
389 if (ak4458->mute_gpiod)
390 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
392 if (ak4458->mute_gpiod)
393 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
394 ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0);
401 static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
402 unsigned int rx_mask, int slots, int slot_width)
404 struct snd_soc_component *component = dai->component;
405 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
408 ak4458->slots = slots;
409 ak4458->slot_width = slot_width;
411 switch (slots * slot_width) {
413 mode = AK4458_MODE_TDM128;
416 mode = AK4458_MODE_TDM256;
419 mode = AK4458_MODE_TDM512;
422 mode = AK4458_MODE_NORMAL;
426 snd_soc_component_update_bits(component, AK4458_0A_CONTROL6,
433 #define AK4458_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
434 SNDRV_PCM_FMTBIT_S24_LE |\
435 SNDRV_PCM_FMTBIT_S32_LE)
437 static const unsigned int ak4458_rates[] = {
438 8000, 11025, 16000, 22050,
439 32000, 44100, 48000, 88200,
440 96000, 176400, 192000, 352800,
441 384000, 705600, 768000, 1411200,
445 static const struct snd_pcm_hw_constraint_list ak4458_rate_constraints = {
446 .count = ARRAY_SIZE(ak4458_rates),
447 .list = ak4458_rates,
450 static int ak4458_startup(struct snd_pcm_substream *substream,
451 struct snd_soc_dai *dai)
455 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
456 SNDRV_PCM_HW_PARAM_RATE,
457 &ak4458_rate_constraints);
462 static struct snd_soc_dai_ops ak4458_dai_ops = {
463 .startup = ak4458_startup,
464 .hw_params = ak4458_hw_params,
465 .set_fmt = ak4458_set_dai_fmt,
466 .digital_mute = ak4458_set_dai_mute,
467 .set_tdm_slot = ak4458_set_tdm_slot,
470 static struct snd_soc_dai_driver ak4458_dai = {
471 .name = "ak4458-aif",
473 .stream_name = "Playback",
476 .rates = SNDRV_PCM_RATE_KNOT,
477 .formats = AK4458_FORMATS,
479 .ops = &ak4458_dai_ops,
482 static void ak4458_power_off(struct ak4458_priv *ak4458)
484 if (ak4458->reset_gpiod) {
485 gpiod_set_value_cansleep(ak4458->reset_gpiod, 0);
486 usleep_range(1000, 2000);
490 static void ak4458_power_on(struct ak4458_priv *ak4458)
492 if (ak4458->reset_gpiod) {
493 gpiod_set_value_cansleep(ak4458->reset_gpiod, 1);
494 usleep_range(1000, 2000);
498 static int ak4458_init(struct snd_soc_component *component)
500 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
503 /* External Mute ON */
504 if (ak4458->mute_gpiod)
505 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
507 ak4458_power_on(ak4458);
509 ret = snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
510 0x80, 0x80); /* ACKS bit = 1; 10000000 */
514 return ak4458_rstn_control(component, 1);
517 static int ak4458_probe(struct snd_soc_component *component)
519 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
523 return ak4458_init(component);
526 static void ak4458_remove(struct snd_soc_component *component)
528 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
530 ak4458_power_off(ak4458);
534 static int __maybe_unused ak4458_runtime_suspend(struct device *dev)
536 struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
538 regcache_cache_only(ak4458->regmap, true);
540 ak4458_power_off(ak4458);
542 if (ak4458->mute_gpiod)
543 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
548 static int __maybe_unused ak4458_runtime_resume(struct device *dev)
550 struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
552 if (ak4458->mute_gpiod)
553 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
555 ak4458_power_off(ak4458);
556 ak4458_power_on(ak4458);
558 regcache_cache_only(ak4458->regmap, false);
559 regcache_mark_dirty(ak4458->regmap);
561 return regcache_sync(ak4458->regmap);
563 #endif /* CONFIG_PM */
565 static const struct snd_soc_component_driver soc_codec_dev_ak4458 = {
566 .probe = ak4458_probe,
567 .remove = ak4458_remove,
568 .controls = ak4458_snd_controls,
569 .num_controls = ARRAY_SIZE(ak4458_snd_controls),
570 .dapm_widgets = ak4458_dapm_widgets,
571 .num_dapm_widgets = ARRAY_SIZE(ak4458_dapm_widgets),
572 .dapm_routes = ak4458_intercon,
573 .num_dapm_routes = ARRAY_SIZE(ak4458_intercon),
575 .use_pmdown_time = 1,
577 .non_legacy_dai_naming = 1,
580 static const struct regmap_config ak4458_regmap = {
584 .max_register = AK4458_14_R4CHATT,
585 .reg_defaults = ak4458_reg_defaults,
586 .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults),
587 .cache_type = REGCACHE_RBTREE,
590 static const struct dev_pm_ops ak4458_pm = {
591 SET_RUNTIME_PM_OPS(ak4458_runtime_suspend, ak4458_runtime_resume, NULL)
592 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
593 pm_runtime_force_resume)
596 static int ak4458_i2c_probe(struct i2c_client *i2c)
598 struct ak4458_priv *ak4458;
601 ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL);
605 ak4458->regmap = devm_regmap_init_i2c(i2c, &ak4458_regmap);
606 if (IS_ERR(ak4458->regmap))
607 return PTR_ERR(ak4458->regmap);
609 i2c_set_clientdata(i2c, ak4458);
610 ak4458->dev = &i2c->dev;
612 ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset",
614 if (IS_ERR(ak4458->reset_gpiod))
615 return PTR_ERR(ak4458->reset_gpiod);
617 ak4458->mute_gpiod = devm_gpiod_get_optional(ak4458->dev, "mute",
619 if (IS_ERR(ak4458->mute_gpiod))
620 return PTR_ERR(ak4458->mute_gpiod);
622 ret = devm_snd_soc_register_component(ak4458->dev, &soc_codec_dev_ak4458,
625 dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret);
629 pm_runtime_enable(&i2c->dev);
634 static int ak4458_i2c_remove(struct i2c_client *i2c)
636 pm_runtime_disable(&i2c->dev);
641 static const struct of_device_id ak4458_of_match[] = {
642 { .compatible = "asahi-kasei,ak4458", },
645 MODULE_DEVICE_TABLE(of, ak4458_of_match);
647 static struct i2c_driver ak4458_i2c_driver = {
651 .of_match_table = ak4458_of_match,
653 .probe_new = ak4458_i2c_probe,
654 .remove = ak4458_i2c_remove,
657 module_i2c_driver(ak4458_i2c_driver);
659 MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
660 MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
661 MODULE_DESCRIPTION("ASoC AK4458 DAC driver");
662 MODULE_LICENSE("GPL v2");