2 * ak4613.c -- Asahi Kasei ALSA Soc Audio driver
4 * Copyright (C) 2015 Renesas Electronics Corporation
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * Based on ak4642.c by Kuninori Morimoto
8 * Based on wm8731.c by Richard Purdie
9 * Based on ak4535.c by Richard Purdie
10 * Based on wm8753.c by Liam Girdwood
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
17 #include <linux/clk.h>
18 #include <linux/i2c.h>
19 #include <linux/slab.h>
20 #include <linux/of_device.h>
21 #include <linux/module.h>
22 #include <linux/regmap.h>
23 #include <sound/soc.h>
24 #include <sound/pcm_params.h>
25 #include <sound/tlv.h>
27 #define PW_MGMT1 0x00 /* Power Management 1 */
28 #define PW_MGMT2 0x01 /* Power Management 2 */
29 #define PW_MGMT3 0x02 /* Power Management 3 */
30 #define CTRL1 0x03 /* Control 1 */
31 #define CTRL2 0x04 /* Control 2 */
32 #define DEMP1 0x05 /* De-emphasis1 */
33 #define DEMP2 0x06 /* De-emphasis2 */
34 #define OFD 0x07 /* Overflow Detect */
35 #define ZRD 0x08 /* Zero Detect */
36 #define ICTRL 0x09 /* Input Control */
37 #define OCTRL 0x0a /* Output Control */
38 #define LOUT1 0x0b /* LOUT1 Volume Control */
39 #define ROUT1 0x0c /* ROUT1 Volume Control */
40 #define LOUT2 0x0d /* LOUT2 Volume Control */
41 #define ROUT2 0x0e /* ROUT2 Volume Control */
42 #define LOUT3 0x0f /* LOUT3 Volume Control */
43 #define ROUT3 0x10 /* ROUT3 Volume Control */
44 #define LOUT4 0x11 /* LOUT4 Volume Control */
45 #define ROUT4 0x12 /* ROUT4 Volume Control */
46 #define LOUT5 0x13 /* LOUT5 Volume Control */
47 #define ROUT5 0x14 /* ROUT5 Volume Control */
48 #define LOUT6 0x15 /* LOUT6 Volume Control */
49 #define ROUT6 0x16 /* ROUT6 Volume Control */
70 #define FMT_MASK (0xf8)
73 #define DFS_MASK (3 << 2)
74 #define DFS_NORMAL_SPEED (0 << 2)
75 #define DFS_DOUBLE_SPEED (1 << 2)
76 #define DFS_QUAD_SPEED (2 << 2)
78 struct ak4613_formats
{
83 struct ak4613_interface
{
84 struct ak4613_formats capture
;
85 struct ak4613_formats playback
;
90 const struct ak4613_interface
*iface
;
103 * min : 0xFE : -127.0 dB
106 static const DECLARE_TLV_DB_SCALE(out_tlv
, -12750, 50, 1);
108 static const struct snd_kcontrol_new ak4613_snd_controls
[] = {
109 SOC_DOUBLE_R_TLV("Digital Playback Volume1", LOUT1
, ROUT1
,
110 0, 0xFF, 1, out_tlv
),
111 SOC_DOUBLE_R_TLV("Digital Playback Volume2", LOUT2
, ROUT2
,
112 0, 0xFF, 1, out_tlv
),
113 SOC_DOUBLE_R_TLV("Digital Playback Volume3", LOUT3
, ROUT3
,
114 0, 0xFF, 1, out_tlv
),
115 SOC_DOUBLE_R_TLV("Digital Playback Volume4", LOUT4
, ROUT4
,
116 0, 0xFF, 1, out_tlv
),
117 SOC_DOUBLE_R_TLV("Digital Playback Volume5", LOUT5
, ROUT5
,
118 0, 0xFF, 1, out_tlv
),
119 SOC_DOUBLE_R_TLV("Digital Playback Volume6", LOUT6
, ROUT6
,
120 0, 0xFF, 1, out_tlv
),
123 static const struct reg_default ak4613_reg
[] = {
124 { 0x0, 0x0f }, { 0x1, 0x07 }, { 0x2, 0x3f }, { 0x3, 0x20 },
125 { 0x4, 0x20 }, { 0x5, 0x55 }, { 0x6, 0x05 }, { 0x7, 0x07 },
126 { 0x8, 0x0f }, { 0x9, 0x07 }, { 0xa, 0x3f }, { 0xb, 0x00 },
127 { 0xc, 0x00 }, { 0xd, 0x00 }, { 0xe, 0x00 }, { 0xf, 0x00 },
128 { 0x10, 0x00 }, { 0x11, 0x00 }, { 0x12, 0x00 }, { 0x13, 0x00 },
129 { 0x14, 0x00 }, { 0x15, 0x00 }, { 0x16, 0x00 },
132 #define AUDIO_IFACE_TO_VAL(fmts) ((fmts - ak4613_iface) << 3)
133 #define AUDIO_IFACE(b, fmt) { b, SND_SOC_DAIFMT_##fmt }
134 static const struct ak4613_interface ak4613_iface
[] = {
135 /* capture */ /* playback */
136 [0] = { AUDIO_IFACE(24, LEFT_J
), AUDIO_IFACE(16, RIGHT_J
) },
137 [1] = { AUDIO_IFACE(24, LEFT_J
), AUDIO_IFACE(20, RIGHT_J
) },
138 [2] = { AUDIO_IFACE(24, LEFT_J
), AUDIO_IFACE(24, RIGHT_J
) },
139 [3] = { AUDIO_IFACE(24, LEFT_J
), AUDIO_IFACE(24, LEFT_J
) },
140 [4] = { AUDIO_IFACE(24, I2S
), AUDIO_IFACE(24, I2S
) },
143 static const struct regmap_config ak4613_regmap_cfg
= {
146 .max_register
= 0x16,
147 .reg_defaults
= ak4613_reg
,
148 .num_reg_defaults
= ARRAY_SIZE(ak4613_reg
),
149 .cache_type
= REGCACHE_RBTREE
,
152 static const struct of_device_id ak4613_of_match
[] = {
153 { .compatible
= "asahi-kasei,ak4613", .data
= &ak4613_regmap_cfg
},
156 MODULE_DEVICE_TABLE(of
, ak4613_of_match
);
158 static const struct i2c_device_id ak4613_i2c_id
[] = {
159 { "ak4613", (kernel_ulong_t
)&ak4613_regmap_cfg
},
162 MODULE_DEVICE_TABLE(i2c
, ak4613_i2c_id
);
164 static const struct snd_soc_dapm_widget ak4613_dapm_widgets
[] = {
167 SND_SOC_DAPM_OUTPUT("LOUT1"),
168 SND_SOC_DAPM_OUTPUT("LOUT2"),
169 SND_SOC_DAPM_OUTPUT("LOUT3"),
170 SND_SOC_DAPM_OUTPUT("LOUT4"),
171 SND_SOC_DAPM_OUTPUT("LOUT5"),
172 SND_SOC_DAPM_OUTPUT("LOUT6"),
174 SND_SOC_DAPM_OUTPUT("ROUT1"),
175 SND_SOC_DAPM_OUTPUT("ROUT2"),
176 SND_SOC_DAPM_OUTPUT("ROUT3"),
177 SND_SOC_DAPM_OUTPUT("ROUT4"),
178 SND_SOC_DAPM_OUTPUT("ROUT5"),
179 SND_SOC_DAPM_OUTPUT("ROUT6"),
182 SND_SOC_DAPM_INPUT("LIN1"),
183 SND_SOC_DAPM_INPUT("LIN2"),
185 SND_SOC_DAPM_INPUT("RIN1"),
186 SND_SOC_DAPM_INPUT("RIN2"),
189 SND_SOC_DAPM_DAC("DAC1", NULL
, PW_MGMT3
, 0, 0),
190 SND_SOC_DAPM_DAC("DAC2", NULL
, PW_MGMT3
, 1, 0),
191 SND_SOC_DAPM_DAC("DAC3", NULL
, PW_MGMT3
, 2, 0),
192 SND_SOC_DAPM_DAC("DAC4", NULL
, PW_MGMT3
, 3, 0),
193 SND_SOC_DAPM_DAC("DAC5", NULL
, PW_MGMT3
, 4, 0),
194 SND_SOC_DAPM_DAC("DAC6", NULL
, PW_MGMT3
, 5, 0),
197 SND_SOC_DAPM_ADC("ADC1", NULL
, PW_MGMT2
, 0, 0),
198 SND_SOC_DAPM_ADC("ADC2", NULL
, PW_MGMT2
, 1, 0),
201 static const struct snd_soc_dapm_route ak4613_intercon
[] = {
202 {"LOUT1", NULL
, "DAC1"},
203 {"LOUT2", NULL
, "DAC2"},
204 {"LOUT3", NULL
, "DAC3"},
205 {"LOUT4", NULL
, "DAC4"},
206 {"LOUT5", NULL
, "DAC5"},
207 {"LOUT6", NULL
, "DAC6"},
209 {"ROUT1", NULL
, "DAC1"},
210 {"ROUT2", NULL
, "DAC2"},
211 {"ROUT3", NULL
, "DAC3"},
212 {"ROUT4", NULL
, "DAC4"},
213 {"ROUT5", NULL
, "DAC5"},
214 {"ROUT6", NULL
, "DAC6"},
216 {"DAC1", NULL
, "Playback"},
217 {"DAC2", NULL
, "Playback"},
218 {"DAC3", NULL
, "Playback"},
219 {"DAC4", NULL
, "Playback"},
220 {"DAC5", NULL
, "Playback"},
221 {"DAC6", NULL
, "Playback"},
223 {"Capture", NULL
, "ADC1"},
224 {"Capture", NULL
, "ADC2"},
226 {"ADC1", NULL
, "LIN1"},
227 {"ADC2", NULL
, "LIN2"},
229 {"ADC1", NULL
, "RIN1"},
230 {"ADC2", NULL
, "RIN2"},
233 static void ak4613_dai_shutdown(struct snd_pcm_substream
*substream
,
234 struct snd_soc_dai
*dai
)
236 struct snd_soc_codec
*codec
= dai
->codec
;
237 struct ak4613_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
238 struct device
*dev
= codec
->dev
;
240 mutex_lock(&priv
->lock
);
243 dev_err(dev
, "unexpected counter error\n");
248 mutex_unlock(&priv
->lock
);
251 static int ak4613_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
253 struct snd_soc_codec
*codec
= dai
->codec
;
254 struct ak4613_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
256 fmt
&= SND_SOC_DAIFMT_FORMAT_MASK
;
259 case SND_SOC_DAIFMT_RIGHT_J
:
260 case SND_SOC_DAIFMT_LEFT_J
:
261 case SND_SOC_DAIFMT_I2S
:
272 static bool ak4613_dai_fmt_matching(const struct ak4613_interface
*iface
,
274 unsigned int fmt
, unsigned int width
)
276 const struct ak4613_formats
*fmts
;
278 fmts
= (is_play
) ? &iface
->playback
: &iface
->capture
;
280 if (fmts
->fmt
!= fmt
)
283 if (fmt
== SND_SOC_DAIFMT_RIGHT_J
) {
284 if (fmts
->width
!= width
)
287 if (fmts
->width
< width
)
294 static int ak4613_dai_hw_params(struct snd_pcm_substream
*substream
,
295 struct snd_pcm_hw_params
*params
,
296 struct snd_soc_dai
*dai
)
298 struct snd_soc_codec
*codec
= dai
->codec
;
299 struct ak4613_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
300 const struct ak4613_interface
*iface
;
301 struct device
*dev
= codec
->dev
;
302 unsigned int width
= params_width(params
);
303 unsigned int fmt
= priv
->fmt
;
305 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
309 rate
= params_rate(params
);
314 ctrl2
= DFS_NORMAL_SPEED
;
318 ctrl2
= DFS_DOUBLE_SPEED
;
322 ctrl2
= DFS_QUAD_SPEED
;
331 * It doesn't support TDM at this point
337 mutex_lock(&priv
->lock
);
339 if (ak4613_dai_fmt_matching(priv
->iface
, is_play
, fmt
, width
))
342 for (i
= ARRAY_SIZE(ak4613_iface
); i
>= 0; i
--) {
343 if (!ak4613_dai_fmt_matching(ak4613_iface
+ i
,
347 iface
= ak4613_iface
+ i
;
352 if ((priv
->iface
== NULL
) ||
353 (priv
->iface
== iface
)) {
358 mutex_unlock(&priv
->lock
);
363 fmt_ctrl
= AUDIO_IFACE_TO_VAL(iface
);
365 snd_soc_update_bits(codec
, CTRL1
, FMT_MASK
, fmt_ctrl
);
366 snd_soc_update_bits(codec
, CTRL2
, DFS_MASK
, ctrl2
);
368 snd_soc_write(codec
, ICTRL
, priv
->ic
);
369 snd_soc_write(codec
, OCTRL
, priv
->oc
);
373 dev_warn(dev
, "unsupported data width/format combination\n");
378 static int ak4613_set_bias_level(struct snd_soc_codec
*codec
,
379 enum snd_soc_bias_level level
)
384 case SND_SOC_BIAS_ON
:
387 case SND_SOC_BIAS_PREPARE
:
388 mgmt1
|= PMADC
| PMDAC
;
390 case SND_SOC_BIAS_STANDBY
:
393 case SND_SOC_BIAS_OFF
:
398 snd_soc_write(codec
, PW_MGMT1
, mgmt1
);
403 static const struct snd_soc_dai_ops ak4613_dai_ops
= {
404 .shutdown
= ak4613_dai_shutdown
,
405 .set_fmt
= ak4613_dai_set_fmt
,
406 .hw_params
= ak4613_dai_hw_params
,
409 #define AK4613_PCM_RATE (SNDRV_PCM_RATE_32000 |\
410 SNDRV_PCM_RATE_44100 |\
411 SNDRV_PCM_RATE_48000 |\
412 SNDRV_PCM_RATE_64000 |\
413 SNDRV_PCM_RATE_88200 |\
414 SNDRV_PCM_RATE_96000 |\
415 SNDRV_PCM_RATE_176400 |\
416 SNDRV_PCM_RATE_192000)
417 #define AK4613_PCM_FMTBIT (SNDRV_PCM_FMTBIT_S16_LE |\
418 SNDRV_PCM_FMTBIT_S24_LE)
420 static struct snd_soc_dai_driver ak4613_dai
= {
421 .name
= "ak4613-hifi",
423 .stream_name
= "Playback",
426 .rates
= AK4613_PCM_RATE
,
427 .formats
= AK4613_PCM_FMTBIT
,
430 .stream_name
= "Capture",
433 .rates
= AK4613_PCM_RATE
,
434 .formats
= AK4613_PCM_FMTBIT
,
436 .ops
= &ak4613_dai_ops
,
437 .symmetric_rates
= 1,
440 static int ak4613_suspend(struct snd_soc_codec
*codec
)
442 struct regmap
*regmap
= dev_get_regmap(codec
->dev
, NULL
);
444 regcache_cache_only(regmap
, true);
445 regcache_mark_dirty(regmap
);
449 static int ak4613_resume(struct snd_soc_codec
*codec
)
451 struct regmap
*regmap
= dev_get_regmap(codec
->dev
, NULL
);
453 regcache_cache_only(regmap
, false);
454 return regcache_sync(regmap
);
457 static struct snd_soc_codec_driver soc_codec_dev_ak4613
= {
458 .suspend
= ak4613_suspend
,
459 .resume
= ak4613_resume
,
460 .set_bias_level
= ak4613_set_bias_level
,
461 .component_driver
= {
462 .controls
= ak4613_snd_controls
,
463 .num_controls
= ARRAY_SIZE(ak4613_snd_controls
),
464 .dapm_widgets
= ak4613_dapm_widgets
,
465 .num_dapm_widgets
= ARRAY_SIZE(ak4613_dapm_widgets
),
466 .dapm_routes
= ak4613_intercon
,
467 .num_dapm_routes
= ARRAY_SIZE(ak4613_intercon
),
471 static void ak4613_parse_of(struct ak4613_priv
*priv
,
474 struct device_node
*np
= dev
->of_node
;
479 for (i
= 0; i
< 2; i
++) {
480 snprintf(prop
, sizeof(prop
), "asahi-kasei,in%d-single-end", i
+ 1);
481 if (!of_get_property(np
, prop
, NULL
))
486 for (i
= 0; i
< 6; i
++) {
487 snprintf(prop
, sizeof(prop
), "asahi-kasei,out%d-single-end", i
+ 1);
488 if (!of_get_property(np
, prop
, NULL
))
493 static int ak4613_i2c_probe(struct i2c_client
*i2c
,
494 const struct i2c_device_id
*id
)
496 struct device
*dev
= &i2c
->dev
;
497 struct device_node
*np
= dev
->of_node
;
498 const struct regmap_config
*regmap_cfg
;
499 struct regmap
*regmap
;
500 struct ak4613_priv
*priv
;
504 const struct of_device_id
*of_id
;
506 of_id
= of_match_device(ak4613_of_match
, dev
);
508 regmap_cfg
= of_id
->data
;
510 regmap_cfg
= (const struct regmap_config
*)id
->driver_data
;
516 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
520 ak4613_parse_of(priv
, dev
);
525 mutex_init(&priv
->lock
);
527 i2c_set_clientdata(i2c
, priv
);
529 regmap
= devm_regmap_init_i2c(i2c
, regmap_cfg
);
531 return PTR_ERR(regmap
);
533 return snd_soc_register_codec(dev
, &soc_codec_dev_ak4613
,
537 static int ak4613_i2c_remove(struct i2c_client
*client
)
539 snd_soc_unregister_codec(&client
->dev
);
543 static struct i2c_driver ak4613_i2c_driver
= {
545 .name
= "ak4613-codec",
546 .of_match_table
= ak4613_of_match
,
548 .probe
= ak4613_i2c_probe
,
549 .remove
= ak4613_i2c_remove
,
550 .id_table
= ak4613_i2c_id
,
553 module_i2c_driver(ak4613_i2c_driver
);
555 MODULE_DESCRIPTION("Soc AK4613 driver");
556 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
557 MODULE_LICENSE("GPL v2");