Commit | Line | Data |
---|---|---|
08660086 CGS |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Audio driver for AK4458 DAC | |
4 | * | |
5 | * Copyright (C) 2016 Asahi Kasei Microdevices Corporation | |
6 | * Copyright 2018 NXP | |
7 | */ | |
8 | ||
9 | #include <linux/module.h> | |
10 | #include <linux/delay.h> | |
11 | #include <linux/slab.h> | |
12 | #include <linux/gpio/consumer.h> | |
13 | #include <linux/of_device.h> | |
14 | #include <linux/of_gpio.h> | |
15 | #include <linux/pm_runtime.h> | |
16 | #include <linux/i2c.h> | |
17 | #include <sound/soc.h> | |
18 | #include <sound/soc-dapm.h> | |
19 | #include <sound/initval.h> | |
20 | #include <sound/tlv.h> | |
21 | #include <sound/pcm_params.h> | |
22 | ||
23 | #include "ak4458.h" | |
24 | ||
25 | /* AK4458 Codec Private Data */ | |
26 | struct ak4458_priv { | |
27 | struct device *dev; | |
28 | struct regmap *regmap; | |
29 | struct gpio_desc *reset_gpiod; | |
30 | struct gpio_desc *mute_gpiod; | |
31 | int digfil; /* SSLOW, SD, SLOW bits */ | |
32 | int fs; /* sampling rate */ | |
33 | int fmt; | |
34 | int slots; | |
35 | int slot_width; | |
36 | }; | |
37 | ||
38 | static const struct reg_default ak4458_reg_defaults[] = { | |
39 | { 0x00, 0x0C }, /* 0x00 AK4458_00_CONTROL1 */ | |
40 | { 0x01, 0x22 }, /* 0x01 AK4458_01_CONTROL2 */ | |
41 | { 0x02, 0x00 }, /* 0x02 AK4458_02_CONTROL3 */ | |
42 | { 0x03, 0xFF }, /* 0x03 AK4458_03_LCHATT */ | |
43 | { 0x04, 0xFF }, /* 0x04 AK4458_04_RCHATT */ | |
44 | { 0x05, 0x00 }, /* 0x05 AK4458_05_CONTROL4 */ | |
45 | { 0x06, 0x00 }, /* 0x06 AK4458_06_DSD1 */ | |
46 | { 0x07, 0x03 }, /* 0x07 AK4458_07_CONTROL5 */ | |
47 | { 0x08, 0x00 }, /* 0x08 AK4458_08_SOUND_CONTROL */ | |
48 | { 0x09, 0x00 }, /* 0x09 AK4458_09_DSD2 */ | |
49 | { 0x0A, 0x0D }, /* 0x0A AK4458_0A_CONTROL6 */ | |
50 | { 0x0B, 0x0C }, /* 0x0B AK4458_0B_CONTROL7 */ | |
51 | { 0x0C, 0x00 }, /* 0x0C AK4458_0C_CONTROL8 */ | |
52 | { 0x0D, 0x00 }, /* 0x0D AK4458_0D_CONTROL9 */ | |
53 | { 0x0E, 0x50 }, /* 0x0E AK4458_0E_CONTROL10 */ | |
54 | { 0x0F, 0xFF }, /* 0x0F AK4458_0F_L2CHATT */ | |
55 | { 0x10, 0xFF }, /* 0x10 AK4458_10_R2CHATT */ | |
56 | { 0x11, 0xFF }, /* 0x11 AK4458_11_L3CHATT */ | |
57 | { 0x12, 0xFF }, /* 0x12 AK4458_12_R3CHATT */ | |
58 | { 0x13, 0xFF }, /* 0x13 AK4458_13_L4CHATT */ | |
59 | { 0x14, 0xFF }, /* 0x14 AK4458_14_R4CHATT */ | |
60 | }; | |
61 | ||
62 | /* | |
63 | * Volume control: | |
64 | * from -127 to 0 dB in 0.5 dB steps (mute instead of -127.5 dB) | |
65 | */ | |
66 | static DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1); | |
67 | ||
68 | /* | |
69 | * DEM1 bit DEM0 bit Mode | |
70 | * 0 0 44.1kHz | |
71 | * 0 1 OFF (default) | |
72 | * 1 0 48kHz | |
73 | * 1 1 32kHz | |
74 | */ | |
75 | static const char * const ak4458_dem_select_texts[] = { | |
76 | "44.1kHz", "OFF", "48kHz", "32kHz" | |
77 | }; | |
78 | ||
79 | /* | |
80 | * SSLOW, SD, SLOW bits Digital Filter Setting | |
81 | * 0, 0, 0 : Sharp Roll-Off Filter | |
82 | * 0, 0, 1 : Slow Roll-Off Filter | |
83 | * 0, 1, 0 : Short delay Sharp Roll-Off Filter | |
84 | * 0, 1, 1 : Short delay Slow Roll-Off Filter | |
85 | * 1, *, * : Super Slow Roll-Off Filter | |
86 | */ | |
87 | static const char * const ak4458_digfil_select_texts[] = { | |
88 | "Sharp Roll-Off Filter", | |
89 | "Slow Roll-Off Filter", | |
90 | "Short delay Sharp Roll-Off Filter", | |
91 | "Short delay Slow Roll-Off Filter", | |
92 | "Super Slow Roll-Off Filter" | |
93 | }; | |
94 | ||
95 | /* | |
96 | * DZFB: Inverting Enable of DZF | |
97 | * 0: DZF goes H at Zero Detection | |
98 | * 1: DZF goes L at Zero Detection | |
99 | */ | |
100 | static const char * const ak4458_dzfb_select_texts[] = {"H", "L"}; | |
101 | ||
102 | /* | |
103 | * SC1-0 bits: Sound Mode Setting | |
104 | * 0 0 : Sound Mode 0 | |
105 | * 0 1 : Sound Mode 1 | |
106 | * 1 0 : Sound Mode 2 | |
107 | * 1 1 : Reserved | |
108 | */ | |
109 | static const char * const ak4458_sc_select_texts[] = { | |
110 | "Sound Mode 0", "Sound Mode 1", "Sound Mode 2" | |
111 | }; | |
112 | ||
113 | /* FIR2-0 bits: FIR Filter Mode Setting */ | |
114 | static const char * const ak4458_fir_select_texts[] = { | |
115 | "Mode 0", "Mode 1", "Mode 2", "Mode 3", | |
116 | "Mode 4", "Mode 5", "Mode 6", "Mode 7", | |
117 | }; | |
118 | ||
119 | /* ATS1-0 bits Attenuation Speed */ | |
120 | static const char * const ak4458_ats_select_texts[] = { | |
121 | "4080/fs", "2040/fs", "510/fs", "255/fs", | |
122 | }; | |
123 | ||
124 | /* DIF2 bit Audio Interface Format Setting(BICK fs) */ | |
125 | static const char * const ak4458_dif_select_texts[] = {"32fs,48fs", "64fs",}; | |
126 | ||
127 | static const struct soc_enum ak4458_dac1_dem_enum = | |
128 | SOC_ENUM_SINGLE(AK4458_01_CONTROL2, 1, | |
129 | ARRAY_SIZE(ak4458_dem_select_texts), | |
130 | ak4458_dem_select_texts); | |
131 | static const struct soc_enum ak4458_dac2_dem_enum = | |
132 | SOC_ENUM_SINGLE(AK4458_0A_CONTROL6, 0, | |
133 | ARRAY_SIZE(ak4458_dem_select_texts), | |
134 | ak4458_dem_select_texts); | |
135 | static const struct soc_enum ak4458_dac3_dem_enum = | |
136 | SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 4, | |
137 | ARRAY_SIZE(ak4458_dem_select_texts), | |
138 | ak4458_dem_select_texts); | |
139 | static const struct soc_enum ak4458_dac4_dem_enum = | |
140 | SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 6, | |
141 | ARRAY_SIZE(ak4458_dem_select_texts), | |
142 | ak4458_dem_select_texts); | |
143 | static const struct soc_enum ak4458_digfil_enum = | |
144 | SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ak4458_digfil_select_texts), | |
145 | ak4458_digfil_select_texts); | |
146 | static const struct soc_enum ak4458_dzfb_enum = | |
147 | SOC_ENUM_SINGLE(AK4458_02_CONTROL3, 2, | |
148 | ARRAY_SIZE(ak4458_dzfb_select_texts), | |
149 | ak4458_dzfb_select_texts); | |
150 | static const struct soc_enum ak4458_sm_enum = | |
151 | SOC_ENUM_SINGLE(AK4458_08_SOUND_CONTROL, 0, | |
152 | ARRAY_SIZE(ak4458_sc_select_texts), | |
153 | ak4458_sc_select_texts); | |
154 | static const struct soc_enum ak4458_fir_enum = | |
155 | SOC_ENUM_SINGLE(AK4458_0C_CONTROL8, 0, | |
156 | ARRAY_SIZE(ak4458_fir_select_texts), | |
157 | ak4458_fir_select_texts); | |
158 | static const struct soc_enum ak4458_ats_enum = | |
159 | SOC_ENUM_SINGLE(AK4458_0B_CONTROL7, 6, | |
160 | ARRAY_SIZE(ak4458_ats_select_texts), | |
161 | ak4458_ats_select_texts); | |
162 | static const struct soc_enum ak4458_dif_enum = | |
163 | SOC_ENUM_SINGLE(AK4458_00_CONTROL1, 3, | |
164 | ARRAY_SIZE(ak4458_dif_select_texts), | |
165 | ak4458_dif_select_texts); | |
166 | ||
167 | static int get_digfil(struct snd_kcontrol *kcontrol, | |
168 | struct snd_ctl_elem_value *ucontrol) | |
169 | { | |
170 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); | |
171 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
172 | ||
173 | ucontrol->value.enumerated.item[0] = ak4458->digfil; | |
174 | ||
175 | return 0; | |
176 | } | |
177 | ||
178 | static int set_digfil(struct snd_kcontrol *kcontrol, | |
179 | struct snd_ctl_elem_value *ucontrol) | |
180 | { | |
181 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); | |
182 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
183 | int num; | |
184 | ||
185 | num = ucontrol->value.enumerated.item[0]; | |
186 | if (num > 4) | |
187 | return -EINVAL; | |
188 | ||
189 | ak4458->digfil = num; | |
190 | ||
191 | /* write SD bit */ | |
192 | snd_soc_update_bits(codec, AK4458_01_CONTROL2, | |
193 | AK4458_SD_MASK, | |
194 | ((ak4458->digfil & 0x02) << 4)); | |
195 | ||
196 | /* write SLOW bit */ | |
197 | snd_soc_update_bits(codec, AK4458_02_CONTROL3, | |
198 | AK4458_SLOW_MASK, | |
199 | (ak4458->digfil & 0x01)); | |
200 | ||
201 | /* write SSLOW bit */ | |
202 | snd_soc_update_bits(codec, AK4458_05_CONTROL4, | |
203 | AK4458_SSLOW_MASK, | |
204 | ((ak4458->digfil & 0x04) >> 2)); | |
205 | ||
206 | return 0; | |
207 | } | |
208 | ||
209 | static const struct snd_kcontrol_new ak4458_snd_controls[] = { | |
210 | SOC_DOUBLE_R_TLV("DAC1 Playback Volume", AK4458_03_LCHATT, | |
211 | AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv), | |
212 | SOC_DOUBLE_R_TLV("DAC2 Playback Volume", AK4458_0F_L2CHATT, | |
213 | AK4458_10_R2CHATT, 0, 0xFF, 0, dac_tlv), | |
214 | SOC_DOUBLE_R_TLV("DAC3 Playback Volume", AK4458_11_L3CHATT, | |
215 | AK4458_12_R3CHATT, 0, 0xFF, 0, dac_tlv), | |
216 | SOC_DOUBLE_R_TLV("DAC4 Playback Volume", AK4458_13_L4CHATT, | |
217 | AK4458_14_R4CHATT, 0, 0xFF, 0, dac_tlv), | |
218 | SOC_ENUM("AK4458 De-emphasis Response DAC1", ak4458_dac1_dem_enum), | |
219 | SOC_ENUM("AK4458 De-emphasis Response DAC2", ak4458_dac2_dem_enum), | |
220 | SOC_ENUM("AK4458 De-emphasis Response DAC3", ak4458_dac3_dem_enum), | |
221 | SOC_ENUM("AK4458 De-emphasis Response DAC4", ak4458_dac4_dem_enum), | |
222 | SOC_ENUM_EXT("AK4458 Digital Filter Setting", ak4458_digfil_enum, | |
223 | get_digfil, set_digfil), | |
224 | SOC_ENUM("AK4458 Inverting Enable of DZFB", ak4458_dzfb_enum), | |
225 | SOC_ENUM("AK4458 Sound Mode", ak4458_sm_enum), | |
226 | SOC_ENUM("AK4458 FIR Filter Mode Setting", ak4458_fir_enum), | |
227 | SOC_ENUM("AK4458 Attenuation transition Time Setting", | |
228 | ak4458_ats_enum), | |
229 | SOC_ENUM("AK4458 BICK fs Setting", ak4458_dif_enum), | |
230 | }; | |
231 | ||
232 | /* ak4458 dapm widgets */ | |
233 | static const struct snd_soc_dapm_widget ak4458_dapm_widgets[] = { | |
234 | SND_SOC_DAPM_DAC("AK4458 DAC1", NULL, AK4458_0A_CONTROL6, 2, 0),/*pw*/ | |
235 | SND_SOC_DAPM_AIF_IN("AK4458 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0), | |
236 | SND_SOC_DAPM_OUTPUT("AK4458 AOUTA"), | |
237 | ||
238 | SND_SOC_DAPM_DAC("AK4458 DAC2", NULL, AK4458_0A_CONTROL6, 3, 0),/*pw*/ | |
239 | SND_SOC_DAPM_OUTPUT("AK4458 AOUTB"), | |
240 | ||
241 | SND_SOC_DAPM_DAC("AK4458 DAC3", NULL, AK4458_0B_CONTROL7, 2, 0),/*pw*/ | |
242 | SND_SOC_DAPM_OUTPUT("AK4458 AOUTC"), | |
243 | ||
244 | SND_SOC_DAPM_DAC("AK4458 DAC4", NULL, AK4458_0B_CONTROL7, 3, 0),/*pw*/ | |
245 | SND_SOC_DAPM_OUTPUT("AK4458 AOUTD"), | |
246 | }; | |
247 | ||
248 | static const struct snd_soc_dapm_route ak4458_intercon[] = { | |
249 | {"AK4458 DAC1", NULL, "AK4458 SDTI"}, | |
250 | {"AK4458 AOUTA", NULL, "AK4458 DAC1"}, | |
251 | ||
252 | {"AK4458 DAC2", NULL, "AK4458 SDTI"}, | |
253 | {"AK4458 AOUTB", NULL, "AK4458 DAC2"}, | |
254 | ||
255 | {"AK4458 DAC3", NULL, "AK4458 SDTI"}, | |
256 | {"AK4458 AOUTC", NULL, "AK4458 DAC3"}, | |
257 | ||
258 | {"AK4458 DAC4", NULL, "AK4458 SDTI"}, | |
259 | {"AK4458 AOUTD", NULL, "AK4458 DAC4"}, | |
260 | }; | |
261 | ||
262 | static int ak4458_rstn_control(struct snd_soc_codec *codec, int bit) | |
263 | { | |
264 | int ret; | |
265 | ||
266 | if (bit) | |
267 | ret = snd_soc_update_bits(codec, | |
268 | AK4458_00_CONTROL1, | |
269 | AK4458_RSTN_MASK, | |
270 | 0x1); | |
271 | else | |
272 | ret = snd_soc_update_bits(codec, | |
273 | AK4458_00_CONTROL1, | |
274 | AK4458_RSTN_MASK, | |
275 | 0x0); | |
276 | return ret; | |
277 | } | |
278 | ||
279 | static int ak4458_hw_params(struct snd_pcm_substream *substream, | |
280 | struct snd_pcm_hw_params *params, | |
281 | struct snd_soc_dai *dai) | |
282 | { | |
283 | struct snd_soc_codec *codec = dai->codec; | |
284 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
285 | int pcm_width = max(params_physical_width(params), ak4458->slot_width); | |
286 | int nfs1; | |
287 | u8 format; | |
288 | ||
289 | nfs1 = params_rate(params); | |
290 | ak4458->fs = nfs1; | |
291 | ||
292 | /* Master Clock Frequency Auto Setting Mode Enable */ | |
293 | snd_soc_update_bits(codec, AK4458_00_CONTROL1, 0x80, 0x80); | |
294 | ||
295 | switch (pcm_width) { | |
296 | case 16: | |
297 | if (ak4458->fmt == SND_SOC_DAIFMT_I2S) | |
298 | format = AK4458_DIF_24BIT_I2S; | |
299 | else | |
300 | format = AK4458_DIF_16BIT_LSB; | |
301 | break; | |
302 | case 32: | |
303 | switch (ak4458->fmt) { | |
304 | case SND_SOC_DAIFMT_I2S: | |
305 | format = AK4458_DIF_32BIT_I2S; | |
306 | break; | |
307 | case SND_SOC_DAIFMT_LEFT_J: | |
308 | format = AK4458_DIF_32BIT_MSB; | |
309 | break; | |
310 | case SND_SOC_DAIFMT_RIGHT_J: | |
311 | format = AK4458_DIF_32BIT_LSB; | |
312 | break; | |
313 | case SND_SOC_DAIFMT_DSP_B: | |
314 | format = AK4458_DIF_32BIT_MSB; | |
315 | break; | |
316 | default: | |
317 | return -EINVAL; | |
318 | } | |
319 | break; | |
320 | default: | |
321 | return -EINVAL; | |
322 | } | |
323 | ||
324 | snd_soc_update_bits(codec, AK4458_00_CONTROL1, | |
325 | AK4458_DIF_MASK, format); | |
326 | ||
327 | ak4458_rstn_control(codec, 0); | |
328 | ak4458_rstn_control(codec, 1); | |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | static int ak4458_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) | |
334 | { | |
335 | struct snd_soc_codec *codec = dai->codec; | |
336 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
337 | ||
338 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { | |
339 | case SND_SOC_DAIFMT_CBS_CFS: /* Slave Mode */ | |
340 | break; | |
341 | case SND_SOC_DAIFMT_CBM_CFM: /* Master Mode is not supported */ | |
342 | case SND_SOC_DAIFMT_CBS_CFM: | |
343 | case SND_SOC_DAIFMT_CBM_CFS: | |
344 | default: | |
345 | dev_err(codec->dev, "Master mode unsupported\n"); | |
346 | return -EINVAL; | |
347 | } | |
348 | ||
349 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { | |
350 | case SND_SOC_DAIFMT_I2S: | |
351 | case SND_SOC_DAIFMT_LEFT_J: | |
352 | case SND_SOC_DAIFMT_RIGHT_J: | |
353 | case SND_SOC_DAIFMT_DSP_B: | |
354 | ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; | |
355 | break; | |
356 | default: | |
357 | dev_err(codec->dev, "Audio format 0x%02X unsupported\n", | |
358 | fmt & SND_SOC_DAIFMT_FORMAT_MASK); | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | ak4458_rstn_control(codec, 0); | |
363 | ak4458_rstn_control(codec, 1); | |
364 | ||
365 | return 0; | |
366 | } | |
367 | ||
368 | static const int att_speed[] = { 4080, 2040, 510, 255 }; | |
369 | ||
370 | static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute) | |
371 | { | |
372 | struct snd_soc_codec *codec = dai->codec; | |
373 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
374 | int nfs, ndt, ret, reg; | |
375 | int ats; | |
376 | ||
377 | nfs = ak4458->fs; | |
378 | ||
379 | reg = snd_soc_read(codec, AK4458_0B_CONTROL7); | |
380 | ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT; | |
381 | ||
382 | ndt = att_speed[ats] / (nfs / 1000); | |
383 | ||
384 | if (mute) { | |
385 | ret = snd_soc_update_bits(codec, AK4458_01_CONTROL2, 0x01, 1); | |
386 | mdelay(ndt); | |
387 | if (ak4458->mute_gpiod) | |
388 | gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); | |
389 | } else { | |
390 | if (ak4458->mute_gpiod) | |
391 | gpiod_set_value_cansleep(ak4458->mute_gpiod, 0); | |
392 | ret = snd_soc_update_bits(codec, AK4458_01_CONTROL2, 0x01, 0); | |
393 | mdelay(ndt); | |
394 | } | |
395 | ||
396 | return 0; | |
397 | } | |
398 | ||
399 | static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, | |
400 | unsigned int rx_mask, int slots, int slot_width) | |
401 | { | |
402 | struct snd_soc_codec *codec = dai->codec; | |
403 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
404 | int mode; | |
405 | ||
406 | ak4458->slots = slots; | |
407 | ak4458->slot_width = slot_width; | |
408 | ||
409 | switch (slots * slot_width) { | |
410 | case 128: | |
411 | mode = AK4458_MODE_TDM128; | |
412 | break; | |
413 | case 256: | |
414 | mode = AK4458_MODE_TDM256; | |
415 | break; | |
416 | case 512: | |
417 | mode = AK4458_MODE_TDM512; | |
418 | break; | |
419 | default: | |
420 | mode = AK4458_MODE_NORMAL; | |
421 | break; | |
422 | } | |
423 | ||
424 | snd_soc_update_bits(codec, AK4458_0A_CONTROL6, | |
425 | AK4458_MODE_MASK, | |
426 | mode); | |
427 | ||
428 | return 0; | |
429 | } | |
430 | ||
431 | #define AK4458_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\ | |
432 | SNDRV_PCM_FMTBIT_S24_LE |\ | |
433 | SNDRV_PCM_FMTBIT_S32_LE) | |
434 | ||
435 | static const unsigned int ak4458_rates[] = { | |
436 | 8000, 11025, 16000, 22050, | |
437 | 32000, 44100, 48000, 88200, | |
438 | 96000, 176400, 192000, 352800, | |
439 | 384000, 705600, 768000, 1411200, | |
440 | 2822400, | |
441 | }; | |
442 | ||
443 | static const struct snd_pcm_hw_constraint_list ak4458_rate_constraints = { | |
444 | .count = ARRAY_SIZE(ak4458_rates), | |
445 | .list = ak4458_rates, | |
446 | }; | |
447 | ||
448 | static int ak4458_startup(struct snd_pcm_substream *substream, | |
449 | struct snd_soc_dai *dai) | |
450 | { | |
451 | int ret; | |
452 | ||
453 | ret = snd_pcm_hw_constraint_list(substream->runtime, 0, | |
454 | SNDRV_PCM_HW_PARAM_RATE, | |
455 | &ak4458_rate_constraints); | |
456 | ||
457 | return ret; | |
458 | } | |
459 | ||
460 | static struct snd_soc_dai_ops ak4458_dai_ops = { | |
461 | .startup = ak4458_startup, | |
462 | .hw_params = ak4458_hw_params, | |
463 | .set_fmt = ak4458_set_dai_fmt, | |
464 | .digital_mute = ak4458_set_dai_mute, | |
465 | .set_tdm_slot = ak4458_set_tdm_slot, | |
466 | }; | |
467 | ||
468 | static struct snd_soc_dai_driver ak4458_dai = { | |
469 | .name = "ak4458-aif", | |
470 | .playback = { | |
471 | .stream_name = "Playback", | |
472 | .channels_min = 1, | |
473 | .channels_max = 8, | |
474 | .rates = SNDRV_PCM_RATE_KNOT, | |
475 | .formats = AK4458_FORMATS, | |
476 | }, | |
477 | .ops = &ak4458_dai_ops, | |
478 | }; | |
479 | ||
480 | static void ak4458_power_off(struct ak4458_priv *ak4458) | |
481 | { | |
482 | if (ak4458->reset_gpiod) { | |
483 | gpiod_set_value_cansleep(ak4458->reset_gpiod, 0); | |
484 | usleep_range(1000, 2000); | |
485 | } | |
486 | } | |
487 | ||
488 | static void ak4458_power_on(struct ak4458_priv *ak4458) | |
489 | { | |
490 | if (ak4458->reset_gpiod) { | |
491 | gpiod_set_value_cansleep(ak4458->reset_gpiod, 1); | |
492 | usleep_range(1000, 2000); | |
493 | } | |
494 | } | |
495 | ||
496 | static void ak4458_init(struct snd_soc_codec *codec) | |
497 | { | |
498 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
499 | ||
500 | /* External Mute ON */ | |
501 | if (ak4458->mute_gpiod) | |
502 | gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); | |
503 | ||
504 | ak4458_power_on(ak4458); | |
505 | ||
506 | snd_soc_update_bits(codec, AK4458_00_CONTROL1, | |
507 | 0x80, 0x80); /* ACKS bit = 1; 10000000 */ | |
508 | ||
509 | ak4458_rstn_control(codec, 1); | |
510 | } | |
511 | ||
512 | static int ak4458_probe(struct snd_soc_codec *codec) | |
513 | { | |
514 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
515 | ||
516 | ak4458_init(codec); | |
517 | ||
518 | ak4458->fs = 48000; | |
519 | ||
520 | return 0; | |
521 | } | |
522 | ||
523 | static int ak4458_remove(struct snd_soc_codec *codec) | |
524 | { | |
525 | struct ak4458_priv *ak4458 = snd_soc_codec_get_drvdata(codec); | |
526 | ||
527 | ak4458_power_off(ak4458); | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | #ifdef CONFIG_PM | |
533 | static int __maybe_unused ak4458_runtime_suspend(struct device *dev) | |
534 | { | |
535 | struct ak4458_priv *ak4458 = dev_get_drvdata(dev); | |
536 | ||
537 | regcache_cache_only(ak4458->regmap, true); | |
538 | ||
539 | ak4458_power_off(ak4458); | |
540 | ||
541 | if (ak4458->mute_gpiod) | |
542 | gpiod_set_value_cansleep(ak4458->mute_gpiod, 0); | |
543 | ||
544 | return 0; | |
545 | } | |
546 | ||
547 | static int __maybe_unused ak4458_runtime_resume(struct device *dev) | |
548 | { | |
549 | struct ak4458_priv *ak4458 = dev_get_drvdata(dev); | |
550 | ||
551 | if (ak4458->mute_gpiod) | |
552 | gpiod_set_value_cansleep(ak4458->mute_gpiod, 1); | |
553 | ||
554 | ak4458_power_off(ak4458); | |
555 | ak4458_power_on(ak4458); | |
556 | ||
557 | regcache_cache_only(ak4458->regmap, false); | |
558 | regcache_mark_dirty(ak4458->regmap); | |
559 | ||
560 | return regcache_sync(ak4458->regmap); | |
561 | } | |
562 | #endif /* CONFIG_PM */ | |
563 | ||
564 | struct snd_soc_codec_driver soc_codec_dev_ak4458 = { | |
565 | .probe = ak4458_probe, | |
566 | .remove = ak4458_remove, | |
567 | .component_driver = { | |
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), | |
574 | }, | |
575 | }; | |
576 | ||
71ca54b6 | 577 | static const struct regmap_config ak4458_regmap = { |
08660086 CGS |
578 | .reg_bits = 8, |
579 | .val_bits = 8, | |
580 | ||
581 | .max_register = AK4458_14_R4CHATT, | |
582 | .reg_defaults = ak4458_reg_defaults, | |
583 | .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults), | |
584 | .cache_type = REGCACHE_RBTREE, | |
585 | }; | |
586 | ||
71ca54b6 | 587 | static const struct dev_pm_ops ak4458_pm = { |
08660086 CGS |
588 | SET_RUNTIME_PM_OPS(ak4458_runtime_suspend, ak4458_runtime_resume, NULL) |
589 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, | |
590 | pm_runtime_force_resume) | |
591 | }; | |
592 | ||
593 | static int ak4458_i2c_probe(struct i2c_client *i2c) | |
594 | { | |
595 | struct ak4458_priv *ak4458; | |
596 | int ret; | |
597 | ||
598 | ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL); | |
599 | if (!ak4458) | |
600 | return -ENOMEM; | |
601 | ||
602 | ak4458->regmap = devm_regmap_init_i2c(i2c, &ak4458_regmap); | |
603 | if (IS_ERR(ak4458->regmap)) | |
604 | return PTR_ERR(ak4458->regmap); | |
605 | ||
606 | i2c_set_clientdata(i2c, ak4458); | |
607 | ak4458->dev = &i2c->dev; | |
608 | ||
609 | ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset", | |
610 | GPIOD_OUT_LOW); | |
611 | if (IS_ERR(ak4458->reset_gpiod)) | |
612 | return PTR_ERR(ak4458->reset_gpiod); | |
613 | ||
614 | ak4458->mute_gpiod = devm_gpiod_get_optional(ak4458->dev, "mute", | |
615 | GPIOD_OUT_LOW); | |
616 | if (IS_ERR(ak4458->mute_gpiod)) | |
617 | return PTR_ERR(ak4458->mute_gpiod); | |
618 | ||
619 | ret = snd_soc_register_codec(ak4458->dev, &soc_codec_dev_ak4458, | |
620 | &ak4458_dai, 1); | |
621 | if (ret < 0) { | |
622 | dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | pm_runtime_enable(&i2c->dev); | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
631 | static int ak4458_i2c_remove(struct i2c_client *i2c) | |
632 | { | |
633 | snd_soc_unregister_codec(&i2c->dev); | |
634 | pm_runtime_disable(&i2c->dev); | |
635 | ||
636 | return 0; | |
637 | } | |
638 | ||
639 | static const struct of_device_id ak4458_of_match[] = { | |
640 | { .compatible = "asahi-kasei,ak4458", }, | |
641 | { }, | |
642 | }; | |
643 | ||
644 | static struct i2c_driver ak4458_i2c_driver = { | |
645 | .driver = { | |
646 | .name = "ak4458", | |
647 | .pm = &ak4458_pm, | |
648 | .of_match_table = ak4458_of_match, | |
649 | }, | |
650 | .probe_new = ak4458_i2c_probe, | |
651 | .remove = ak4458_i2c_remove, | |
652 | }; | |
653 | ||
654 | module_i2c_driver(ak4458_i2c_driver); | |
655 | ||
656 | MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>"); | |
657 | MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>"); | |
658 | MODULE_DESCRIPTION("ASoC AK4458 DAC driver"); | |
659 | MODULE_LICENSE("GPL"); |