blob: eab7c76cfcd931bd06dd050da94bc23e67d567c4 [file] [log] [blame]
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +02001// SPDX-License-Identifier: GPL-2.0
Cosmin-Gabriel Samoila05aa6162018-02-20 10:56:07 +02002//
3// Audio driver for AK4458 DAC
4//
5// Copyright (C) 2016 Asahi Kasei Microdevices Corporation
6// Copyright 2018 NXP
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +02007
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +02008#include <linux/delay.h>
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +02009#include <linux/gpio/consumer.h>
Cosmin-Gabriel Samoilade03af52018-02-20 10:56:08 +020010#include <linux/i2c.h>
11#include <linux/module.h>
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +020012#include <linux/of_device.h>
13#include <linux/of_gpio.h>
14#include <linux/pm_runtime.h>
Cosmin-Gabriel Samoilade03af52018-02-20 10:56:08 +020015#include <linux/slab.h>
16#include <sound/initval.h>
17#include <sound/pcm_params.h>
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +020018#include <sound/soc.h>
19#include <sound/soc-dapm.h>
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +020020#include <sound/tlv.h>
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +020021
22#include "ak4458.h"
23
Daniel Balutaee6047b2019-01-04 16:27:03 +000024struct ak4458_drvdata {
25 struct snd_soc_dai_driver *dai_drv;
26 const struct snd_soc_component_driver *comp_drv;
27};
28
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +020029/* AK4458 Codec Private Data */
30struct ak4458_priv {
31 struct device *dev;
32 struct regmap *regmap;
33 struct gpio_desc *reset_gpiod;
34 struct gpio_desc *mute_gpiod;
35 int digfil; /* SSLOW, SD, SLOW bits */
36 int fs; /* sampling rate */
37 int fmt;
38 int slots;
39 int slot_width;
40};
41
42static const struct reg_default ak4458_reg_defaults[] = {
43 { 0x00, 0x0C }, /* 0x00 AK4458_00_CONTROL1 */
44 { 0x01, 0x22 }, /* 0x01 AK4458_01_CONTROL2 */
45 { 0x02, 0x00 }, /* 0x02 AK4458_02_CONTROL3 */
46 { 0x03, 0xFF }, /* 0x03 AK4458_03_LCHATT */
47 { 0x04, 0xFF }, /* 0x04 AK4458_04_RCHATT */
48 { 0x05, 0x00 }, /* 0x05 AK4458_05_CONTROL4 */
49 { 0x06, 0x00 }, /* 0x06 AK4458_06_DSD1 */
50 { 0x07, 0x03 }, /* 0x07 AK4458_07_CONTROL5 */
51 { 0x08, 0x00 }, /* 0x08 AK4458_08_SOUND_CONTROL */
52 { 0x09, 0x00 }, /* 0x09 AK4458_09_DSD2 */
53 { 0x0A, 0x0D }, /* 0x0A AK4458_0A_CONTROL6 */
54 { 0x0B, 0x0C }, /* 0x0B AK4458_0B_CONTROL7 */
55 { 0x0C, 0x00 }, /* 0x0C AK4458_0C_CONTROL8 */
56 { 0x0D, 0x00 }, /* 0x0D AK4458_0D_CONTROL9 */
57 { 0x0E, 0x50 }, /* 0x0E AK4458_0E_CONTROL10 */
58 { 0x0F, 0xFF }, /* 0x0F AK4458_0F_L2CHATT */
59 { 0x10, 0xFF }, /* 0x10 AK4458_10_R2CHATT */
60 { 0x11, 0xFF }, /* 0x11 AK4458_11_L3CHATT */
61 { 0x12, 0xFF }, /* 0x12 AK4458_12_R3CHATT */
62 { 0x13, 0xFF }, /* 0x13 AK4458_13_L4CHATT */
63 { 0x14, 0xFF }, /* 0x14 AK4458_14_R4CHATT */
64};
65
66/*
67 * Volume control:
68 * from -127 to 0 dB in 0.5 dB steps (mute instead of -127.5 dB)
69 */
70static DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
71
72/*
73 * DEM1 bit DEM0 bit Mode
74 * 0 0 44.1kHz
75 * 0 1 OFF (default)
76 * 1 0 48kHz
77 * 1 1 32kHz
78 */
79static const char * const ak4458_dem_select_texts[] = {
80 "44.1kHz", "OFF", "48kHz", "32kHz"
81};
82
83/*
84 * SSLOW, SD, SLOW bits Digital Filter Setting
85 * 0, 0, 0 : Sharp Roll-Off Filter
86 * 0, 0, 1 : Slow Roll-Off Filter
87 * 0, 1, 0 : Short delay Sharp Roll-Off Filter
88 * 0, 1, 1 : Short delay Slow Roll-Off Filter
89 * 1, *, * : Super Slow Roll-Off Filter
90 */
91static const char * const ak4458_digfil_select_texts[] = {
92 "Sharp Roll-Off Filter",
93 "Slow Roll-Off Filter",
94 "Short delay Sharp Roll-Off Filter",
95 "Short delay Slow Roll-Off Filter",
96 "Super Slow Roll-Off Filter"
97};
98
99/*
100 * DZFB: Inverting Enable of DZF
101 * 0: DZF goes H at Zero Detection
102 * 1: DZF goes L at Zero Detection
103 */
104static const char * const ak4458_dzfb_select_texts[] = {"H", "L"};
105
106/*
107 * SC1-0 bits: Sound Mode Setting
108 * 0 0 : Sound Mode 0
109 * 0 1 : Sound Mode 1
110 * 1 0 : Sound Mode 2
111 * 1 1 : Reserved
112 */
113static const char * const ak4458_sc_select_texts[] = {
114 "Sound Mode 0", "Sound Mode 1", "Sound Mode 2"
115};
116
117/* FIR2-0 bits: FIR Filter Mode Setting */
118static const char * const ak4458_fir_select_texts[] = {
119 "Mode 0", "Mode 1", "Mode 2", "Mode 3",
120 "Mode 4", "Mode 5", "Mode 6", "Mode 7",
121};
122
123/* ATS1-0 bits Attenuation Speed */
124static const char * const ak4458_ats_select_texts[] = {
125 "4080/fs", "2040/fs", "510/fs", "255/fs",
126};
127
128/* DIF2 bit Audio Interface Format Setting(BICK fs) */
129static const char * const ak4458_dif_select_texts[] = {"32fs,48fs", "64fs",};
130
131static const struct soc_enum ak4458_dac1_dem_enum =
132 SOC_ENUM_SINGLE(AK4458_01_CONTROL2, 1,
133 ARRAY_SIZE(ak4458_dem_select_texts),
134 ak4458_dem_select_texts);
135static const struct soc_enum ak4458_dac2_dem_enum =
136 SOC_ENUM_SINGLE(AK4458_0A_CONTROL6, 0,
137 ARRAY_SIZE(ak4458_dem_select_texts),
138 ak4458_dem_select_texts);
139static const struct soc_enum ak4458_dac3_dem_enum =
140 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 4,
141 ARRAY_SIZE(ak4458_dem_select_texts),
142 ak4458_dem_select_texts);
143static const struct soc_enum ak4458_dac4_dem_enum =
144 SOC_ENUM_SINGLE(AK4458_0E_CONTROL10, 6,
145 ARRAY_SIZE(ak4458_dem_select_texts),
146 ak4458_dem_select_texts);
147static const struct soc_enum ak4458_digfil_enum =
148 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(ak4458_digfil_select_texts),
149 ak4458_digfil_select_texts);
150static const struct soc_enum ak4458_dzfb_enum =
151 SOC_ENUM_SINGLE(AK4458_02_CONTROL3, 2,
152 ARRAY_SIZE(ak4458_dzfb_select_texts),
153 ak4458_dzfb_select_texts);
154static const struct soc_enum ak4458_sm_enum =
155 SOC_ENUM_SINGLE(AK4458_08_SOUND_CONTROL, 0,
156 ARRAY_SIZE(ak4458_sc_select_texts),
157 ak4458_sc_select_texts);
158static const struct soc_enum ak4458_fir_enum =
159 SOC_ENUM_SINGLE(AK4458_0C_CONTROL8, 0,
160 ARRAY_SIZE(ak4458_fir_select_texts),
161 ak4458_fir_select_texts);
162static const struct soc_enum ak4458_ats_enum =
163 SOC_ENUM_SINGLE(AK4458_0B_CONTROL7, 6,
164 ARRAY_SIZE(ak4458_ats_select_texts),
165 ak4458_ats_select_texts);
166static const struct soc_enum ak4458_dif_enum =
167 SOC_ENUM_SINGLE(AK4458_00_CONTROL1, 3,
168 ARRAY_SIZE(ak4458_dif_select_texts),
169 ak4458_dif_select_texts);
170
171static int get_digfil(struct snd_kcontrol *kcontrol,
172 struct snd_ctl_elem_value *ucontrol)
173{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000174 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
175 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200176
177 ucontrol->value.enumerated.item[0] = ak4458->digfil;
178
179 return 0;
180}
181
182static int set_digfil(struct snd_kcontrol *kcontrol,
183 struct snd_ctl_elem_value *ucontrol)
184{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000185 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
186 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200187 int num;
188
189 num = ucontrol->value.enumerated.item[0];
190 if (num > 4)
191 return -EINVAL;
192
193 ak4458->digfil = num;
194
195 /* write SD bit */
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000196 snd_soc_component_update_bits(component, AK4458_01_CONTROL2,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200197 AK4458_SD_MASK,
198 ((ak4458->digfil & 0x02) << 4));
199
200 /* write SLOW bit */
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000201 snd_soc_component_update_bits(component, AK4458_02_CONTROL3,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200202 AK4458_SLOW_MASK,
203 (ak4458->digfil & 0x01));
204
205 /* write SSLOW bit */
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000206 snd_soc_component_update_bits(component, AK4458_05_CONTROL4,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200207 AK4458_SSLOW_MASK,
208 ((ak4458->digfil & 0x04) >> 2));
209
210 return 0;
211}
212
213static const struct snd_kcontrol_new ak4458_snd_controls[] = {
214 SOC_DOUBLE_R_TLV("DAC1 Playback Volume", AK4458_03_LCHATT,
215 AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
216 SOC_DOUBLE_R_TLV("DAC2 Playback Volume", AK4458_0F_L2CHATT,
217 AK4458_10_R2CHATT, 0, 0xFF, 0, dac_tlv),
218 SOC_DOUBLE_R_TLV("DAC3 Playback Volume", AK4458_11_L3CHATT,
219 AK4458_12_R3CHATT, 0, 0xFF, 0, dac_tlv),
220 SOC_DOUBLE_R_TLV("DAC4 Playback Volume", AK4458_13_L4CHATT,
221 AK4458_14_R4CHATT, 0, 0xFF, 0, dac_tlv),
222 SOC_ENUM("AK4458 De-emphasis Response DAC1", ak4458_dac1_dem_enum),
223 SOC_ENUM("AK4458 De-emphasis Response DAC2", ak4458_dac2_dem_enum),
224 SOC_ENUM("AK4458 De-emphasis Response DAC3", ak4458_dac3_dem_enum),
225 SOC_ENUM("AK4458 De-emphasis Response DAC4", ak4458_dac4_dem_enum),
226 SOC_ENUM_EXT("AK4458 Digital Filter Setting", ak4458_digfil_enum,
227 get_digfil, set_digfil),
228 SOC_ENUM("AK4458 Inverting Enable of DZFB", ak4458_dzfb_enum),
229 SOC_ENUM("AK4458 Sound Mode", ak4458_sm_enum),
230 SOC_ENUM("AK4458 FIR Filter Mode Setting", ak4458_fir_enum),
231 SOC_ENUM("AK4458 Attenuation transition Time Setting",
232 ak4458_ats_enum),
233 SOC_ENUM("AK4458 BICK fs Setting", ak4458_dif_enum),
234};
235
236/* ak4458 dapm widgets */
237static const struct snd_soc_dapm_widget ak4458_dapm_widgets[] = {
238 SND_SOC_DAPM_DAC("AK4458 DAC1", NULL, AK4458_0A_CONTROL6, 2, 0),/*pw*/
239 SND_SOC_DAPM_AIF_IN("AK4458 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
240 SND_SOC_DAPM_OUTPUT("AK4458 AOUTA"),
241
242 SND_SOC_DAPM_DAC("AK4458 DAC2", NULL, AK4458_0A_CONTROL6, 3, 0),/*pw*/
243 SND_SOC_DAPM_OUTPUT("AK4458 AOUTB"),
244
245 SND_SOC_DAPM_DAC("AK4458 DAC3", NULL, AK4458_0B_CONTROL7, 2, 0),/*pw*/
246 SND_SOC_DAPM_OUTPUT("AK4458 AOUTC"),
247
248 SND_SOC_DAPM_DAC("AK4458 DAC4", NULL, AK4458_0B_CONTROL7, 3, 0),/*pw*/
249 SND_SOC_DAPM_OUTPUT("AK4458 AOUTD"),
250};
251
252static const struct snd_soc_dapm_route ak4458_intercon[] = {
253 {"AK4458 DAC1", NULL, "AK4458 SDTI"},
254 {"AK4458 AOUTA", NULL, "AK4458 DAC1"},
255
256 {"AK4458 DAC2", NULL, "AK4458 SDTI"},
257 {"AK4458 AOUTB", NULL, "AK4458 DAC2"},
258
259 {"AK4458 DAC3", NULL, "AK4458 SDTI"},
260 {"AK4458 AOUTC", NULL, "AK4458 DAC3"},
261
262 {"AK4458 DAC4", NULL, "AK4458 SDTI"},
263 {"AK4458 AOUTD", NULL, "AK4458 DAC4"},
264};
265
Daniel Balutaee6047b2019-01-04 16:27:03 +0000266/* ak4497 controls */
267static const struct snd_kcontrol_new ak4497_snd_controls[] = {
268 SOC_DOUBLE_R_TLV("DAC Playback Volume", AK4458_03_LCHATT,
269 AK4458_04_RCHATT, 0, 0xFF, 0, dac_tlv),
270 SOC_ENUM("AK4497 De-emphasis Response DAC", ak4458_dac1_dem_enum),
271 SOC_ENUM_EXT("AK4497 Digital Filter Setting", ak4458_digfil_enum,
272 get_digfil, set_digfil),
273 SOC_ENUM("AK4497 Inverting Enable of DZFB", ak4458_dzfb_enum),
274 SOC_ENUM("AK4497 Sound Mode", ak4458_sm_enum),
275 SOC_ENUM("AK4497 Attenuation transition Time Setting",
276 ak4458_ats_enum),
277};
278
279/* ak4497 dapm widgets */
280static const struct snd_soc_dapm_widget ak4497_dapm_widgets[] = {
281 SND_SOC_DAPM_DAC("AK4497 DAC", NULL, AK4458_0A_CONTROL6, 2, 0),
282 SND_SOC_DAPM_AIF_IN("AK4497 SDTI", "Playback", 0, SND_SOC_NOPM, 0, 0),
283 SND_SOC_DAPM_OUTPUT("AK4497 AOUT"),
284};
285
286/* ak4497 dapm routes */
287static const struct snd_soc_dapm_route ak4497_intercon[] = {
288 {"AK4497 DAC", NULL, "AK4497 SDTI"},
289 {"AK4497 AOUT", NULL, "AK4497 DAC"},
290
291};
292
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000293static int ak4458_rstn_control(struct snd_soc_component *component, int bit)
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200294{
295 int ret;
296
297 if (bit)
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000298 ret = snd_soc_component_update_bits(component,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200299 AK4458_00_CONTROL1,
300 AK4458_RSTN_MASK,
301 0x1);
302 else
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000303 ret = snd_soc_component_update_bits(component,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200304 AK4458_00_CONTROL1,
305 AK4458_RSTN_MASK,
306 0x0);
307 return ret;
308}
309
310static int ak4458_hw_params(struct snd_pcm_substream *substream,
311 struct snd_pcm_hw_params *params,
312 struct snd_soc_dai *dai)
313{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000314 struct snd_soc_component *component = dai->component;
315 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200316 int pcm_width = max(params_physical_width(params), ak4458->slot_width);
317 int nfs1;
318 u8 format;
319
320 nfs1 = params_rate(params);
321 ak4458->fs = nfs1;
322
323 /* Master Clock Frequency Auto Setting Mode Enable */
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000324 snd_soc_component_update_bits(component, AK4458_00_CONTROL1, 0x80, 0x80);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200325
326 switch (pcm_width) {
327 case 16:
328 if (ak4458->fmt == SND_SOC_DAIFMT_I2S)
329 format = AK4458_DIF_24BIT_I2S;
330 else
331 format = AK4458_DIF_16BIT_LSB;
332 break;
333 case 32:
334 switch (ak4458->fmt) {
335 case SND_SOC_DAIFMT_I2S:
336 format = AK4458_DIF_32BIT_I2S;
337 break;
338 case SND_SOC_DAIFMT_LEFT_J:
339 format = AK4458_DIF_32BIT_MSB;
340 break;
341 case SND_SOC_DAIFMT_RIGHT_J:
342 format = AK4458_DIF_32BIT_LSB;
343 break;
344 case SND_SOC_DAIFMT_DSP_B:
345 format = AK4458_DIF_32BIT_MSB;
346 break;
347 default:
348 return -EINVAL;
349 }
350 break;
351 default:
352 return -EINVAL;
353 }
354
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000355 snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200356 AK4458_DIF_MASK, format);
357
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000358 ak4458_rstn_control(component, 0);
359 ak4458_rstn_control(component, 1);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200360
361 return 0;
362}
363
364static int ak4458_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
365{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000366 struct snd_soc_component *component = dai->component;
367 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200368
369 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
370 case SND_SOC_DAIFMT_CBS_CFS: /* Slave Mode */
371 break;
372 case SND_SOC_DAIFMT_CBM_CFM: /* Master Mode is not supported */
373 case SND_SOC_DAIFMT_CBS_CFM:
374 case SND_SOC_DAIFMT_CBM_CFS:
375 default:
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000376 dev_err(component->dev, "Master mode unsupported\n");
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200377 return -EINVAL;
378 }
379
380 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
381 case SND_SOC_DAIFMT_I2S:
382 case SND_SOC_DAIFMT_LEFT_J:
383 case SND_SOC_DAIFMT_RIGHT_J:
384 case SND_SOC_DAIFMT_DSP_B:
385 ak4458->fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
386 break;
387 default:
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000388 dev_err(component->dev, "Audio format 0x%02X unsupported\n",
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200389 fmt & SND_SOC_DAIFMT_FORMAT_MASK);
390 return -EINVAL;
391 }
392
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000393 ak4458_rstn_control(component, 0);
394 ak4458_rstn_control(component, 1);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200395
396 return 0;
397}
398
399static const int att_speed[] = { 4080, 2040, 510, 255 };
400
401static int ak4458_set_dai_mute(struct snd_soc_dai *dai, int mute)
402{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000403 struct snd_soc_component *component = dai->component;
404 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200405 int nfs, ndt, ret, reg;
406 int ats;
407
408 nfs = ak4458->fs;
409
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000410 reg = snd_soc_component_read32(component, AK4458_0B_CONTROL7);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200411 ats = (reg & AK4458_ATS_MASK) >> AK4458_ATS_SHIFT;
412
413 ndt = att_speed[ats] / (nfs / 1000);
414
415 if (mute) {
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000416 ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 1);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200417 mdelay(ndt);
418 if (ak4458->mute_gpiod)
419 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
420 } else {
421 if (ak4458->mute_gpiod)
422 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000423 ret = snd_soc_component_update_bits(component, AK4458_01_CONTROL2, 0x01, 0);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200424 mdelay(ndt);
425 }
426
427 return 0;
428}
429
430static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
431 unsigned int rx_mask, int slots, int slot_width)
432{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000433 struct snd_soc_component *component = dai->component;
434 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200435 int mode;
436
437 ak4458->slots = slots;
438 ak4458->slot_width = slot_width;
439
440 switch (slots * slot_width) {
441 case 128:
442 mode = AK4458_MODE_TDM128;
443 break;
444 case 256:
445 mode = AK4458_MODE_TDM256;
446 break;
447 case 512:
448 mode = AK4458_MODE_TDM512;
449 break;
450 default:
451 mode = AK4458_MODE_NORMAL;
452 break;
453 }
454
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000455 snd_soc_component_update_bits(component, AK4458_0A_CONTROL6,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200456 AK4458_MODE_MASK,
457 mode);
458
459 return 0;
460}
461
462#define AK4458_FORMATS (SNDRV_PCM_FMTBIT_S16_LE |\
463 SNDRV_PCM_FMTBIT_S24_LE |\
464 SNDRV_PCM_FMTBIT_S32_LE)
465
466static const unsigned int ak4458_rates[] = {
467 8000, 11025, 16000, 22050,
468 32000, 44100, 48000, 88200,
469 96000, 176400, 192000, 352800,
470 384000, 705600, 768000, 1411200,
471 2822400,
472};
473
474static const struct snd_pcm_hw_constraint_list ak4458_rate_constraints = {
475 .count = ARRAY_SIZE(ak4458_rates),
476 .list = ak4458_rates,
477};
478
479static int ak4458_startup(struct snd_pcm_substream *substream,
480 struct snd_soc_dai *dai)
481{
482 int ret;
483
484 ret = snd_pcm_hw_constraint_list(substream->runtime, 0,
485 SNDRV_PCM_HW_PARAM_RATE,
486 &ak4458_rate_constraints);
487
488 return ret;
489}
490
Julia Lawall704a9fc2018-10-27 15:34:44 +0200491static const struct snd_soc_dai_ops ak4458_dai_ops = {
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200492 .startup = ak4458_startup,
493 .hw_params = ak4458_hw_params,
494 .set_fmt = ak4458_set_dai_fmt,
495 .digital_mute = ak4458_set_dai_mute,
496 .set_tdm_slot = ak4458_set_tdm_slot,
497};
498
499static struct snd_soc_dai_driver ak4458_dai = {
500 .name = "ak4458-aif",
501 .playback = {
502 .stream_name = "Playback",
503 .channels_min = 1,
504 .channels_max = 8,
505 .rates = SNDRV_PCM_RATE_KNOT,
506 .formats = AK4458_FORMATS,
507 },
508 .ops = &ak4458_dai_ops,
509};
510
Daniel Balutaee6047b2019-01-04 16:27:03 +0000511static struct snd_soc_dai_driver ak4497_dai = {
512 .name = "ak4497-aif",
513 .playback = {
514 .stream_name = "Playback",
515 .channels_min = 1,
516 .channels_max = 2,
517 .rates = SNDRV_PCM_RATE_KNOT,
518 .formats = AK4458_FORMATS,
519 },
520 .ops = &ak4458_dai_ops,
521};
522
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200523static void ak4458_power_off(struct ak4458_priv *ak4458)
524{
525 if (ak4458->reset_gpiod) {
526 gpiod_set_value_cansleep(ak4458->reset_gpiod, 0);
527 usleep_range(1000, 2000);
528 }
529}
530
531static void ak4458_power_on(struct ak4458_priv *ak4458)
532{
533 if (ak4458->reset_gpiod) {
534 gpiod_set_value_cansleep(ak4458->reset_gpiod, 1);
535 usleep_range(1000, 2000);
536 }
537}
538
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000539static void ak4458_init(struct snd_soc_component *component)
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200540{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000541 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200542
543 /* External Mute ON */
544 if (ak4458->mute_gpiod)
545 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
546
547 ak4458_power_on(ak4458);
548
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000549 snd_soc_component_update_bits(component, AK4458_00_CONTROL1,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200550 0x80, 0x80); /* ACKS bit = 1; 10000000 */
551
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000552 ak4458_rstn_control(component, 1);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200553}
554
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000555static int ak4458_probe(struct snd_soc_component *component)
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200556{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000557 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200558
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000559 ak4458_init(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200560
561 ak4458->fs = 48000;
562
563 return 0;
564}
565
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000566static void ak4458_remove(struct snd_soc_component *component)
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200567{
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000568 struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200569
570 ak4458_power_off(ak4458);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200571}
572
573#ifdef CONFIG_PM
574static int __maybe_unused ak4458_runtime_suspend(struct device *dev)
575{
576 struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
577
578 regcache_cache_only(ak4458->regmap, true);
579
580 ak4458_power_off(ak4458);
581
582 if (ak4458->mute_gpiod)
583 gpiod_set_value_cansleep(ak4458->mute_gpiod, 0);
584
585 return 0;
586}
587
588static int __maybe_unused ak4458_runtime_resume(struct device *dev)
589{
590 struct ak4458_priv *ak4458 = dev_get_drvdata(dev);
591
592 if (ak4458->mute_gpiod)
593 gpiod_set_value_cansleep(ak4458->mute_gpiod, 1);
594
595 ak4458_power_off(ak4458);
596 ak4458_power_on(ak4458);
597
598 regcache_cache_only(ak4458->regmap, false);
599 regcache_mark_dirty(ak4458->regmap);
600
601 return regcache_sync(ak4458->regmap);
602}
603#endif /* CONFIG_PM */
604
Colin Ian King62624f72018-06-06 11:31:23 +0100605static const struct snd_soc_component_driver soc_codec_dev_ak4458 = {
Kuninori Morimotoc988e672018-02-19 04:16:42 +0000606 .probe = ak4458_probe,
607 .remove = ak4458_remove,
608 .controls = ak4458_snd_controls,
609 .num_controls = ARRAY_SIZE(ak4458_snd_controls),
610 .dapm_widgets = ak4458_dapm_widgets,
611 .num_dapm_widgets = ARRAY_SIZE(ak4458_dapm_widgets),
612 .dapm_routes = ak4458_intercon,
613 .num_dapm_routes = ARRAY_SIZE(ak4458_intercon),
614 .idle_bias_on = 1,
615 .use_pmdown_time = 1,
616 .endianness = 1,
617 .non_legacy_dai_naming = 1,
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200618};
619
Daniel Balutaee6047b2019-01-04 16:27:03 +0000620static const struct snd_soc_component_driver soc_codec_dev_ak4497 = {
621 .probe = ak4458_probe,
622 .remove = ak4458_remove,
623 .controls = ak4497_snd_controls,
624 .num_controls = ARRAY_SIZE(ak4497_snd_controls),
625 .dapm_widgets = ak4497_dapm_widgets,
626 .num_dapm_widgets = ARRAY_SIZE(ak4497_dapm_widgets),
627 .dapm_routes = ak4497_intercon,
628 .num_dapm_routes = ARRAY_SIZE(ak4497_intercon),
629 .idle_bias_on = 1,
630 .use_pmdown_time = 1,
631 .endianness = 1,
632 .non_legacy_dai_naming = 1,
633};
634
kbuild test robot71ca54b2018-02-16 21:55:28 +0800635static const struct regmap_config ak4458_regmap = {
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200636 .reg_bits = 8,
637 .val_bits = 8,
638
639 .max_register = AK4458_14_R4CHATT,
640 .reg_defaults = ak4458_reg_defaults,
641 .num_reg_defaults = ARRAY_SIZE(ak4458_reg_defaults),
642 .cache_type = REGCACHE_RBTREE,
643};
644
Daniel Balutaee6047b2019-01-04 16:27:03 +0000645static const struct ak4458_drvdata ak4458_drvdata = {
646 .dai_drv = &ak4458_dai,
647 .comp_drv = &soc_codec_dev_ak4458,
648};
649
650static const struct ak4458_drvdata ak4497_drvdata = {
651 .dai_drv = &ak4497_dai,
652 .comp_drv = &soc_codec_dev_ak4497,
653};
654
kbuild test robot71ca54b2018-02-16 21:55:28 +0800655static const struct dev_pm_ops ak4458_pm = {
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200656 SET_RUNTIME_PM_OPS(ak4458_runtime_suspend, ak4458_runtime_resume, NULL)
657 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
658 pm_runtime_force_resume)
659};
660
661static int ak4458_i2c_probe(struct i2c_client *i2c)
662{
663 struct ak4458_priv *ak4458;
Daniel Balutaee6047b2019-01-04 16:27:03 +0000664 const struct ak4458_drvdata *drvdata;
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200665 int ret;
666
667 ak4458 = devm_kzalloc(&i2c->dev, sizeof(*ak4458), GFP_KERNEL);
668 if (!ak4458)
669 return -ENOMEM;
670
671 ak4458->regmap = devm_regmap_init_i2c(i2c, &ak4458_regmap);
672 if (IS_ERR(ak4458->regmap))
673 return PTR_ERR(ak4458->regmap);
674
675 i2c_set_clientdata(i2c, ak4458);
676 ak4458->dev = &i2c->dev;
677
Daniel Balutaee6047b2019-01-04 16:27:03 +0000678 drvdata = of_device_get_match_data(&i2c->dev);
679
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200680 ak4458->reset_gpiod = devm_gpiod_get_optional(ak4458->dev, "reset",
681 GPIOD_OUT_LOW);
682 if (IS_ERR(ak4458->reset_gpiod))
683 return PTR_ERR(ak4458->reset_gpiod);
684
685 ak4458->mute_gpiod = devm_gpiod_get_optional(ak4458->dev, "mute",
686 GPIOD_OUT_LOW);
687 if (IS_ERR(ak4458->mute_gpiod))
688 return PTR_ERR(ak4458->mute_gpiod);
689
Daniel Balutaee6047b2019-01-04 16:27:03 +0000690 ret = devm_snd_soc_register_component(ak4458->dev, drvdata->comp_drv,
691 drvdata->dai_drv, 1);
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200692 if (ret < 0) {
693 dev_err(ak4458->dev, "Failed to register CODEC: %d\n", ret);
694 return ret;
695 }
696
697 pm_runtime_enable(&i2c->dev);
698
699 return 0;
700}
701
702static int ak4458_i2c_remove(struct i2c_client *i2c)
703{
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200704 pm_runtime_disable(&i2c->dev);
705
706 return 0;
707}
708
709static const struct of_device_id ak4458_of_match[] = {
Daniel Balutaee6047b2019-01-04 16:27:03 +0000710 { .compatible = "asahi-kasei,ak4458", .data = &ak4458_drvdata},
711 { .compatible = "asahi-kasei,ak4497", .data = &ak4497_drvdata},
Cosmin-Gabriel Samoila08660082018-02-14 15:21:06 +0200712 { },
713};
714
715static struct i2c_driver ak4458_i2c_driver = {
716 .driver = {
717 .name = "ak4458",
718 .pm = &ak4458_pm,
719 .of_match_table = ak4458_of_match,
720 },
721 .probe_new = ak4458_i2c_probe,
722 .remove = ak4458_i2c_remove,
723};
724
725module_i2c_driver(ak4458_i2c_driver);
726
727MODULE_AUTHOR("Junichi Wakasugi <wakasugi.jb@om.asahi-kasei.co.jp>");
728MODULE_AUTHOR("Mihai Serban <mihai.serban@nxp.com>");
729MODULE_DESCRIPTION("ASoC AK4458 DAC driver");
Cosmin-Gabriel Samoila05aa6162018-02-20 10:56:07 +0200730MODULE_LICENSE("GPL v2");