1 // SPDX-License-Identifier: GPL-2.0
3 // ak4613.c -- Asahi Kasei ALSA Soc Audio driver
5 // Copyright (C) 2015 Renesas Electronics Corporation
8 // Based on ak4642.c by Kuninori Morimoto
9 // Based on wm8731.c by Richard Purdie
10 // Based on ak4535.c by Richard Purdie
11 // Based on wm8753.c by Liam Girdwood
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/slab.h>
17 #include <linux/of_device.h>
18 #include <linux/module.h>
19 #include <linux/regmap.h>
20 #include <sound/soc.h>
21 #include <sound/pcm_params.h>
22 #include <sound/tlv.h>
24 #define PW_MGMT1 0x00 /* Power Management 1 */
25 #define PW_MGMT2 0x01 /* Power Management 2 */
26 #define PW_MGMT3 0x02 /* Power Management 3 */
27 #define CTRL1 0x03 /* Control 1 */
28 #define CTRL2 0x04 /* Control 2 */
29 #define DEMP1 0x05 /* De-emphasis1 */
30 #define DEMP2 0x06 /* De-emphasis2 */
31 #define OFD 0x07 /* Overflow Detect */
32 #define ZRD 0x08 /* Zero Detect */
33 #define ICTRL 0x09 /* Input Control */
34 #define OCTRL 0x0a /* Output Control */
35 #define LOUT1 0x0b /* LOUT1 Volume Control */
36 #define ROUT1 0x0c /* ROUT1 Volume Control */
37 #define LOUT2 0x0d /* LOUT2 Volume Control */
38 #define ROUT2 0x0e /* ROUT2 Volume Control */
39 #define LOUT3 0x0f /* LOUT3 Volume Control */
40 #define ROUT3 0x10 /* ROUT3 Volume Control */
41 #define LOUT4 0x11 /* LOUT4 Volume Control */
42 #define ROUT4 0x12 /* ROUT4 Volume Control */
43 #define LOUT5 0x13 /* LOUT5 Volume Control */
44 #define ROUT5 0x14 /* ROUT5 Volume Control */
45 #define LOUT6 0x15 /* LOUT6 Volume Control */
46 #define ROUT6 0x16 /* ROUT6 Volume Control */
67 #define FMT_MASK (0xf8)
70 #define DFS_MASK (3 << 2)
71 #define DFS_NORMAL_SPEED (0 << 2)
72 #define DFS_DOUBLE_SPEED (1 << 2)
73 #define DFS_QUAD_SPEED (2 << 2)
76 #define ICTRL_MASK (0x3)
79 #define OCTRL_MASK (0x3F)
81 struct ak4613_formats {
86 struct ak4613_interface {
87 struct ak4613_formats capture;
88 struct ak4613_formats playback;
93 const struct ak4613_interface *iface;
94 struct snd_pcm_hw_constraint_list constraint;
95 struct work_struct dummy_write_work;
96 struct snd_soc_component *component;
111 * min : 0xFE : -127.0 dB
114 static const DECLARE_TLV_DB_SCALE(out_tlv, -12750, 50, 1);
116 static const struct snd_kcontrol_new ak4613_snd_controls[] = {
117 SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1, ROUT1,
118 0, 0xFF, 1, out_tlv),
119 SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2, ROUT2,
120 0, 0xFF, 1, out_tlv),
121 SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3, ROUT3,
122 0, 0xFF, 1, out_tlv),
123 SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4, ROUT4,
124 0, 0xFF, 1, out_tlv),
125 SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5, ROUT5,
126 0, 0xFF, 1, out_tlv),
127 SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6, ROUT6,
128 0, 0xFF, 1, out_tlv),
131 static const struct reg_default ak4613_reg[] = {
132 { 0x0, 0x0f }, { 0x1, 0x07 }, { 0x2, 0x3f }, { 0x3, 0x20 },
133 { 0x4, 0x20 }, { 0x5, 0x55 }, { 0x6, 0x05 }, { 0x7, 0x07 },
134 { 0x8, 0x0f }, { 0x9, 0x07 }, { 0xa, 0x3f }, { 0xb, 0x00 },
135 { 0xc, 0x00 }, { 0xd, 0x00 }, { 0xe, 0x00 }, { 0xf, 0x00 },
136 { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
137 { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
140 #define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
141 #define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
142 static const struct ak4613_interface ak4613_iface[] = {
143 /* capture */ /* playback */
144 /* [0] - [2] are not supported */
145 [3] = { AUDIO_IFACE(24, LEFT_J), AUDIO_IFACE(24, LEFT_J) },
146 [4] = { AUDIO_IFACE(24, I2S), AUDIO_IFACE(24, I2S) },
149 static const struct regmap_config ak4613_regmap_cfg = {
152 .max_register = 0x16,
153 .reg_defaults = ak4613_reg,
154 .num_reg_defaults = ARRAY_SIZE(ak4613_reg),
155 .cache_type = REGCACHE_RBTREE,
158 static const struct of_device_id ak4613_of_match[] = {
159 { .compatible = "asahi-kasei,ak4613", .data = &ak4613_regmap_cfg },
162 MODULE_DEVICE_TABLE(of, ak4613_of_match);
164 static const struct i2c_device_id ak4613_i2c_id[] = {
165 { "ak4613", (kernel_ulong_t)&ak4613_regmap_cfg },
168 MODULE_DEVICE_TABLE(i2c, ak4613_i2c_id);
170 static const struct snd_soc_dapm_widget ak4613_dapm_widgets[] = {
173 SND_SOC_DAPM_OUTPUT("LOUT1"),
174 SND_SOC_DAPM_OUTPUT("LOUT2"),
175 SND_SOC_DAPM_OUTPUT("LOUT3"),
176 SND_SOC_DAPM_OUTPUT("LOUT4"),
177 SND_SOC_DAPM_OUTPUT("LOUT5"),
178 SND_SOC_DAPM_OUTPUT("LOUT6"),
180 SND_SOC_DAPM_OUTPUT("ROUT1"),
181 SND_SOC_DAPM_OUTPUT("ROUT2"),
182 SND_SOC_DAPM_OUTPUT("ROUT3"),
183 SND_SOC_DAPM_OUTPUT("ROUT4"),
184 SND_SOC_DAPM_OUTPUT("ROUT5"),
185 SND_SOC_DAPM_OUTPUT("ROUT6"),
188 SND_SOC_DAPM_INPUT("LIN1"),
189 SND_SOC_DAPM_INPUT("LIN2"),
191 SND_SOC_DAPM_INPUT("RIN1"),
192 SND_SOC_DAPM_INPUT("RIN2"),
195 SND_SOC_DAPM_DAC("DAC1", NULL, PW_MGMT3, 0, 0),
196 SND_SOC_DAPM_DAC("DAC2", NULL, PW_MGMT3, 1, 0),
197 SND_SOC_DAPM_DAC("DAC3", NULL, PW_MGMT3, 2, 0),
198 SND_SOC_DAPM_DAC("DAC4", NULL, PW_MGMT3, 3, 0),
199 SND_SOC_DAPM_DAC("DAC5", NULL, PW_MGMT3, 4, 0),
200 SND_SOC_DAPM_DAC("DAC6", NULL, PW_MGMT3, 5, 0),
203 SND_SOC_DAPM_ADC("ADC1", NULL, PW_MGMT2, 0, 0),
204 SND_SOC_DAPM_ADC("ADC2", NULL, PW_MGMT2, 1, 0),
207 static const struct snd_soc_dapm_route ak4613_intercon[] = {
208 {"LOUT1", NULL, "DAC1"},
209 {"LOUT2", NULL, "DAC2"},
210 {"LOUT3", NULL, "DAC3"},
211 {"LOUT4", NULL, "DAC4"},
212 {"LOUT5", NULL, "DAC5"},
213 {"LOUT6", NULL, "DAC6"},
215 {"ROUT1", NULL, "DAC1"},
216 {"ROUT2", NULL, "DAC2"},
217 {"ROUT3", NULL, "DAC3"},
218 {"ROUT4", NULL, "DAC4"},
219 {"ROUT5", NULL, "DAC5"},
220 {"ROUT6", NULL, "DAC6"},
222 {"DAC1", NULL, "Playback"},
223 {"DAC2", NULL, "Playback"},
224 {"DAC3", NULL, "Playback"},
225 {"DAC4", NULL, "Playback"},
226 {"DAC5", NULL, "Playback"},
227 {"DAC6", NULL, "Playback"},
229 {"Capture", NULL, "ADC1"},
230 {"Capture", NULL, "ADC2"},
232 {"ADC1", NULL, "LIN1"},
233 {"ADC2", NULL, "LIN2"},
235 {"ADC1", NULL, "RIN1"},
236 {"ADC2", NULL, "RIN2"},
239 static void ak4613_dai_shutdown(struct snd_pcm_substream *substream,
240 struct snd_soc_dai *dai)
242 struct snd_soc_component *component = dai->component;
243 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
244 struct device *dev = component->dev;
246 mutex_lock(&priv->lock);
249 dev_err(dev, "unexpected counter error\n");
254 mutex_unlock(&priv->lock);
257 static void ak4613_hw_constraints(struct ak4613_priv *priv,
258 struct snd_pcm_runtime *runtime)
260 static const unsigned int ak4613_rates[] = {
270 struct snd_pcm_hw_constraint_list *constraint = &priv->constraint;
274 constraint->list = ak4613_rates;
275 constraint->mask = 0;
276 constraint->count = 0;
280 * Normal: [32kHz, 48kHz] : 256fs,384fs or 512fs
281 * Double: [64kHz, 96kHz] : 256fs
282 * Quad : [128kHz,192kHz]: 128fs
285 * Normal: [32kHz, 48kHz] : 256fs or 512fs
286 * Double: [64kHz, 96kHz] : 256fs
287 * Quad : [128kHz,192kHz]: 128fs
289 for (i = 0; i < ARRAY_SIZE(ak4613_rates); i++) {
290 /* minimum fs on each range */
291 fs = (ak4613_rates[i] <= 96000) ? 256 : 128;
293 if (priv->sysclk >= ak4613_rates[i] * fs)
294 constraint->count = i + 1;
297 snd_pcm_hw_constraint_list(runtime, 0,
298 SNDRV_PCM_HW_PARAM_RATE, constraint);
301 static int ak4613_dai_startup(struct snd_pcm_substream *substream,
302 struct snd_soc_dai *dai)
304 struct snd_soc_component *component = dai->component;
305 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
309 ak4613_hw_constraints(priv, substream->runtime);
314 static int ak4613_dai_set_sysclk(struct snd_soc_dai *codec_dai,
315 int clk_id, unsigned int freq, int dir)
317 struct snd_soc_component *component = codec_dai->component;
318 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
325 static int ak4613_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
327 struct snd_soc_component *component = dai->component;
328 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
330 fmt &= SND_SOC_DAIFMT_FORMAT_MASK;
333 case SND_SOC_DAIFMT_LEFT_J:
334 case SND_SOC_DAIFMT_I2S:
344 static bool ak4613_dai_fmt_matching(const struct ak4613_interface *iface,
346 unsigned int fmt, unsigned int width)
348 const struct ak4613_formats *fmts;
350 fmts = (is_play) ? &iface->playback : &iface->capture;
352 if (fmts->fmt != fmt)
355 if (fmts->width != width)
361 static int ak4613_dai_hw_params(struct snd_pcm_substream *substream,
362 struct snd_pcm_hw_params *params,
363 struct snd_soc_dai *dai)
365 struct snd_soc_component *component = dai->component;
366 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
367 const struct ak4613_interface *iface;
368 struct device *dev = component->dev;
369 unsigned int width = params_width(params);
370 unsigned int fmt = priv->fmt;
372 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
376 rate = params_rate(params);
381 ctrl2 = DFS_NORMAL_SPEED;
386 ctrl2 = DFS_DOUBLE_SPEED;
390 ctrl2 = DFS_QUAD_SPEED;
400 * It doesn't support TDM at this point
406 mutex_lock(&priv->lock);
408 if (ak4613_dai_fmt_matching(priv->iface, is_play, fmt, width))
411 for (i = ARRAY_SIZE(ak4613_iface) - 1; i >= 0; i--) {
412 if (!ak4613_dai_fmt_matching(ak4613_iface + i,
416 iface = ak4613_iface + i;
421 if ((priv->iface == NULL) ||
422 (priv->iface == iface)) {
426 mutex_unlock(&priv->lock);
431 fmt_ctrl = AUDIO_IFACE_TO_VAL(iface);
433 snd_soc_component_update_bits(component, CTRL1, FMT_MASK, fmt_ctrl);
434 snd_soc_component_update_bits(component, CTRL2, DFS_MASK, ctrl2);
436 snd_soc_component_update_bits(component, ICTRL, ICTRL_MASK, priv->ic);
437 snd_soc_component_update_bits(component, OCTRL, OCTRL_MASK, priv->oc);
441 dev_warn(dev, "unsupported data width/format combination\n");
446 static int ak4613_set_bias_level(struct snd_soc_component *component,
447 enum snd_soc_bias_level level)
452 case SND_SOC_BIAS_ON:
455 case SND_SOC_BIAS_PREPARE:
456 mgmt1 |= PMADC | PMDAC;
458 case SND_SOC_BIAS_STANDBY:
461 case SND_SOC_BIAS_OFF:
466 snd_soc_component_write(component, PW_MGMT1, mgmt1);
471 static void ak4613_dummy_write(struct work_struct *work)
473 struct ak4613_priv *priv = container_of(work,
476 struct snd_soc_component *component = priv->component;
481 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
485 * To avoid extra delay, we want to avoid preemption here,
486 * but we can't. Because it uses I2C access which is using IRQ
487 * and sleep. Thus, delay might be more than 5 LR clocks
489 * ak4613_dai_trigger()
491 udelay(5000000 / priv->rate);
493 mgmt1 = snd_soc_component_read(component, PW_MGMT1);
494 mgmt3 = snd_soc_component_read(component, PW_MGMT3);
496 snd_soc_component_write(component, PW_MGMT1, mgmt1);
497 snd_soc_component_write(component, PW_MGMT3, mgmt3);
500 static int ak4613_dai_trigger(struct snd_pcm_substream *substream, int cmd,
501 struct snd_soc_dai *dai)
503 struct snd_soc_component *component = dai->component;
504 struct ak4613_priv *priv = snd_soc_component_get_drvdata(component);
509 * PW_MGMT1 / PW_MGMT3 needs dummy write at least after 5 LR clocks
510 * from Power Down Release. Otherwise, Playback volume will be 0dB.
511 * To avoid complex multiple delay/dummy_write method from
512 * ak4613_set_bias_level() / SND_SOC_DAPM_DAC_E("DACx", ...),
515 * But, unfortunately, we can't "write" here because here is atomic
516 * context (It uses I2C access for writing).
517 * Thus, use schedule_work() to switching to normal context
522 * Calling ak4613_dummy_write() function might be delayed.
523 * In such case, ak4613 volume might be temporarily 0dB when
524 * beggining of playback.
526 * ak4613_dummy_write()
529 if ((cmd != SNDRV_PCM_TRIGGER_START) &&
530 (cmd != SNDRV_PCM_TRIGGER_RESUME))
533 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
536 priv->component = component;
537 schedule_work(&priv->dummy_write_work);
542 static const struct snd_soc_dai_ops ak4613_dai_ops = {
543 .startup = ak4613_dai_startup,
544 .shutdown = ak4613_dai_shutdown,
545 .set_sysclk = ak4613_dai_set_sysclk,
546 .set_fmt = ak4613_dai_set_fmt,
547 .trigger = ak4613_dai_trigger,
548 .hw_params = ak4613_dai_hw_params,
551 #define AK4613_PCM_RATE (SNDRV_PCM_RATE_32000 |\
552 SNDRV_PCM_RATE_44100 |\
553 SNDRV_PCM_RATE_48000 |\
554 SNDRV_PCM_RATE_64000 |\
555 SNDRV_PCM_RATE_88200 |\
556 SNDRV_PCM_RATE_96000 |\
557 SNDRV_PCM_RATE_176400 |\
558 SNDRV_PCM_RATE_192000)
559 #define AK4613_PCM_FMTBIT (SNDRV_PCM_FMTBIT_S24_LE)
561 static struct snd_soc_dai_driver ak4613_dai = {
562 .name = "ak4613-hifi",
564 .stream_name = "Playback",
567 .rates = AK4613_PCM_RATE,
568 .formats = AK4613_PCM_FMTBIT,
571 .stream_name = "Capture",
574 .rates = AK4613_PCM_RATE,
575 .formats = AK4613_PCM_FMTBIT,
577 .ops = &ak4613_dai_ops,
578 .symmetric_rates = 1,
581 static int ak4613_suspend(struct snd_soc_component *component)
583 struct regmap *regmap = dev_get_regmap(component->dev, NULL);
585 regcache_cache_only(regmap, true);
586 regcache_mark_dirty(regmap);
590 static int ak4613_resume(struct snd_soc_component *component)
592 struct regmap *regmap = dev_get_regmap(component->dev, NULL);
594 regcache_cache_only(regmap, false);
595 return regcache_sync(regmap);
598 static const struct snd_soc_component_driver soc_component_dev_ak4613 = {
599 .suspend = ak4613_suspend,
600 .resume = ak4613_resume,
601 .set_bias_level = ak4613_set_bias_level,
602 .controls = ak4613_snd_controls,
603 .num_controls = ARRAY_SIZE(ak4613_snd_controls),
604 .dapm_widgets = ak4613_dapm_widgets,
605 .num_dapm_widgets = ARRAY_SIZE(ak4613_dapm_widgets),
606 .dapm_routes = ak4613_intercon,
607 .num_dapm_routes = ARRAY_SIZE(ak4613_intercon),
610 .non_legacy_dai_naming = 1,
613 static void ak4613_parse_of(struct ak4613_priv *priv,
616 struct device_node *np = dev->of_node;
621 for (i = 0; i < 2; i++) {
622 snprintf(prop, sizeof(prop), "asahi-kasei,in%d-single-end", i + 1);
623 if (!of_get_property(np, prop, NULL))
628 for (i = 0; i < 6; i++) {
629 snprintf(prop, sizeof(prop), "asahi-kasei,out%d-single-end", i + 1);
630 if (!of_get_property(np, prop, NULL))
635 static int ak4613_i2c_probe(struct i2c_client *i2c,
636 const struct i2c_device_id *id)
638 struct device *dev = &i2c->dev;
639 struct device_node *np = dev->of_node;
640 const struct regmap_config *regmap_cfg;
641 struct regmap *regmap;
642 struct ak4613_priv *priv;
646 const struct of_device_id *of_id;
648 of_id = of_match_device(ak4613_of_match, dev);
650 regmap_cfg = of_id->data;
652 regmap_cfg = (const struct regmap_config *)id->driver_data;
658 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
662 ak4613_parse_of(priv, dev);
667 INIT_WORK(&priv->dummy_write_work, ak4613_dummy_write);
669 mutex_init(&priv->lock);
671 i2c_set_clientdata(i2c, priv);
673 regmap = devm_regmap_init_i2c(i2c, regmap_cfg);
675 return PTR_ERR(regmap);
677 return devm_snd_soc_register_component(dev, &soc_component_dev_ak4613,
681 static int ak4613_i2c_remove(struct i2c_client *client)
686 static struct i2c_driver ak4613_i2c_driver = {
688 .name = "ak4613-codec",
689 .of_match_table = ak4613_of_match,
691 .probe = ak4613_i2c_probe,
692 .remove = ak4613_i2c_remove,
693 .id_table = ak4613_i2c_id,
696 module_i2c_driver(ak4613_i2c_driver);
698 MODULE_DESCRIPTION("Soc AK4613 driver");
700 MODULE_LICENSE("GPL v2");