blob: 16e2d334bbe0294ee13c759647d99ddceec4f663 [file] [log] [blame]
Barry Songa1533d92010-03-19 14:48:33 +08001/*
2 * AD193X Audio Codec driver supporting AD1936/7/8/9
3 *
4 * Copyright 2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
Barry Songa1533d92010-03-19 14:48:33 +08009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/device.h>
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +010012#include <linux/regmap.h>
Stephen Rothwell1b132ea2010-03-29 15:32:18 +110013#include <linux/slab.h>
Barry Songa1533d92010-03-19 14:48:33 +080014#include <sound/core.h>
15#include <sound/pcm.h>
16#include <sound/pcm_params.h>
17#include <sound/initval.h>
18#include <sound/soc.h>
19#include <sound/tlv.h>
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +010020
Barry Songa1533d92010-03-19 14:48:33 +080021#include "ad193x.h"
22
23/* codec private data */
24struct ad193x_priv {
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +020025 struct regmap *regmap;
Cyrille Pitchene5224f52015-09-29 16:41:43 +020026 enum ad193x_type type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000027 int sysclk;
Barry Songa1533d92010-03-19 14:48:33 +080028};
29
Barry Songa1533d92010-03-19 14:48:33 +080030/*
31 * AD193X volume/mute/de-emphasis etc. controls
32 */
Lars-Peter Clausenc4e7a4a2011-11-28 17:28:08 +010033static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
Barry Songa1533d92010-03-19 14:48:33 +080034
Takashi Iwaib6592d82014-02-18 09:55:27 +010035static SOC_ENUM_SINGLE_DECL(ad193x_deemp_enum, AD193X_DAC_CTRL2, 1,
36 ad193x_deemp);
Barry Songa1533d92010-03-19 14:48:33 +080037
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010038static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);
39
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +000040static const unsigned int ad193x_sb[] = {32};
41
42static struct snd_pcm_hw_constraint_list constr = {
43 .list = ad193x_sb,
44 .count = ARRAY_SIZE(ad193x_sb),
45};
46
Barry Songa1533d92010-03-19 14:48:33 +080047static const struct snd_kcontrol_new ad193x_snd_controls[] = {
48 /* DAC volume control */
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010049 SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
50 AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
51 SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
52 AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
53 SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
54 AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
55 SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
56 AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
Barry Songa1533d92010-03-19 14:48:33 +080057
Barry Songa1533d92010-03-19 14:48:33 +080058 /* DAC switch control */
59 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
60 AD193X_DACR1_MUTE, 1, 1),
61 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
62 AD193X_DACR2_MUTE, 1, 1),
63 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
64 AD193X_DACR3_MUTE, 1, 1),
65 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
66 AD193X_DACR4_MUTE, 1, 1),
67
Cyrille Pitchene5224f52015-09-29 16:41:43 +020068 /* DAC de-emphasis */
69 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
70};
71
72static const struct snd_kcontrol_new ad193x_adc_snd_controls[] = {
73 /* ADC switch control */
74 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
75 AD193X_ADCR1_MUTE, 1, 1),
76 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
77 AD193X_ADCR2_MUTE, 1, 1),
78
Barry Songa1533d92010-03-19 14:48:33 +080079 /* ADC high-pass filter */
80 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
81 AD193X_ADC_HIGHPASS_FILTER, 1, 0),
Barry Songa1533d92010-03-19 14:48:33 +080082};
83
84static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +010085 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
86 SND_SOC_DAPM_PGA("DAC Output", AD193X_DAC_CTRL0, 0, 1, NULL, 0),
Barry Songa1533d92010-03-19 14:48:33 +080087 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +010088 SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +010089 SND_SOC_DAPM_VMID("VMID"),
Barry Songa1533d92010-03-19 14:48:33 +080090 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
91 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
92 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
93 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
Cyrille Pitchene5224f52015-09-29 16:41:43 +020094};
95
96static const struct snd_soc_dapm_widget ad193x_adc_widgets[] = {
97 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
98 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
Barry Songa1533d92010-03-19 14:48:33 +080099 SND_SOC_DAPM_INPUT("ADC1IN"),
100 SND_SOC_DAPM_INPUT("ADC2IN"),
101};
102
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000103static int ad193x_check_pll(struct snd_soc_dapm_widget *source,
104 struct snd_soc_dapm_widget *sink)
105{
106 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
107 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
108
109 return !!ad193x->sysclk;
110}
111
Barry Songa1533d92010-03-19 14:48:33 +0800112static const struct snd_soc_dapm_route audio_paths[] = {
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +0100113 { "DAC", NULL, "SYSCLK" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100114 { "DAC Output", NULL, "DAC" },
115 { "DAC Output", NULL, "VMID" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100116 { "DAC1OUT", NULL, "DAC Output" },
117 { "DAC2OUT", NULL, "DAC Output" },
118 { "DAC3OUT", NULL, "DAC Output" },
119 { "DAC4OUT", NULL, "DAC Output" },
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000120 { "SYSCLK", NULL, "PLL_PWR", &ad193x_check_pll },
Barry Songa1533d92010-03-19 14:48:33 +0800121};
122
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200123static const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = {
124 { "ADC", NULL, "SYSCLK" },
125 { "ADC", NULL, "ADC_PWR" },
126 { "ADC", NULL, "ADC1IN" },
127 { "ADC", NULL, "ADC2IN" },
128};
129
130static inline bool ad193x_has_adc(const struct ad193x_priv *ad193x)
131{
132 switch (ad193x->type) {
133 case AD1933:
134 case AD1934:
135 return false;
136 default:
137 break;
138 }
139
140 return true;
141}
142
Barry Songa1533d92010-03-19 14:48:33 +0800143/*
144 * DAI ops entries
145 */
146
147static int ad193x_mute(struct snd_soc_dai *dai, int mute)
148{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000149 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Barry Songa1533d92010-03-19 14:48:33 +0800150
Axel Lin54c96cf2011-10-18 06:25:08 +0800151 if (mute)
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100152 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800153 AD193X_DAC_MASTER_MUTE,
154 AD193X_DAC_MASTER_MUTE);
155 else
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100156 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800157 AD193X_DAC_MASTER_MUTE, 0);
Barry Songa1533d92010-03-19 14:48:33 +0800158
159 return 0;
160}
161
162static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
163 unsigned int rx_mask, int slots, int width)
164{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000165 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100166 unsigned int channels;
Barry Songa1533d92010-03-19 14:48:33 +0800167
168 switch (slots) {
169 case 2:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100170 channels = AD193X_2_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800171 break;
172 case 4:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100173 channels = AD193X_4_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800174 break;
175 case 8:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100176 channels = AD193X_8_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800177 break;
178 case 16:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100179 channels = AD193X_16_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800180 break;
181 default:
182 return -EINVAL;
183 }
184
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100185 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
186 AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200187 if (ad193x_has_adc(ad193x))
188 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
189 AD193X_ADC_CHAN_MASK,
190 channels << AD193X_ADC_CHAN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800191
192 return 0;
193}
194
195static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
196 unsigned int fmt)
197{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000198 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100199 unsigned int adc_serfmt = 0;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000200 unsigned int dac_serfmt = 0;
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100201 unsigned int adc_fmt = 0;
202 unsigned int dac_fmt = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800203
204 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000205 * with TDM), ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) and DAC I2S mode
206 * (SND_SOC_DAIFMT_I2S)
Barry Songa1533d92010-03-19 14:48:33 +0800207 */
208 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
209 case SND_SOC_DAIFMT_I2S:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100210 adc_serfmt |= AD193X_ADC_SERFMT_TDM;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000211 dac_serfmt |= AD193X_DAC_SERFMT_STEREO;
Barry Songa1533d92010-03-19 14:48:33 +0800212 break;
213 case SND_SOC_DAIFMT_DSP_A:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100214 adc_serfmt |= AD193X_ADC_SERFMT_AUX;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000215 dac_serfmt |= AD193X_DAC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800216 break;
217 default:
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200218 if (ad193x_has_adc(ad193x))
219 return -EINVAL;
Barry Songa1533d92010-03-19 14:48:33 +0800220 }
221
222 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
223 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songa1533d92010-03-19 14:48:33 +0800224 break;
225 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100226 adc_fmt |= AD193X_ADC_LEFT_HIGH;
227 dac_fmt |= AD193X_DAC_LEFT_HIGH;
Barry Songa1533d92010-03-19 14:48:33 +0800228 break;
229 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100230 adc_fmt |= AD193X_ADC_BCLK_INV;
231 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800232 break;
Barry Songa1533d92010-03-19 14:48:33 +0800233 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100234 adc_fmt |= AD193X_ADC_LEFT_HIGH;
235 adc_fmt |= AD193X_ADC_BCLK_INV;
236 dac_fmt |= AD193X_DAC_LEFT_HIGH;
237 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800238 break;
239 default:
240 return -EINVAL;
241 }
242
Codrin Ciubotariu90f6e682019-02-18 16:10:32 +0000243 /* For DSP_*, LRCLK's polarity must be inverted */
Codrin Ciubotariuda7260c2019-06-26 13:49:46 +0300244 if (fmt & SND_SOC_DAIFMT_DSP_A)
245 dac_fmt ^= AD193X_DAC_LEFT_HIGH;
Codrin Ciubotariu90f6e682019-02-18 16:10:32 +0000246
Barry Songa1533d92010-03-19 14:48:33 +0800247 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
248 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100249 adc_fmt |= AD193X_ADC_LCR_MASTER;
250 adc_fmt |= AD193X_ADC_BCLK_MASTER;
251 dac_fmt |= AD193X_DAC_LCR_MASTER;
252 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800253 break;
254 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100255 adc_fmt |= AD193X_ADC_LCR_MASTER;
256 dac_fmt |= AD193X_DAC_LCR_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800257 break;
258 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100259 adc_fmt |= AD193X_ADC_BCLK_MASTER;
260 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800261 break;
262 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songa1533d92010-03-19 14:48:33 +0800263 break;
264 default:
265 return -EINVAL;
266 }
267
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200268 if (ad193x_has_adc(ad193x)) {
269 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
270 AD193X_ADC_SERFMT_MASK, adc_serfmt);
271 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
272 AD193X_ADC_FMT_MASK, adc_fmt);
273 }
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000274 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL0,
275 AD193X_DAC_SERFMT_MASK, dac_serfmt);
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100276 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
277 AD193X_DAC_FMT_MASK, dac_fmt);
Barry Songa1533d92010-03-19 14:48:33 +0800278
279 return 0;
280}
281
Barry Songfab90aa2010-05-21 11:57:01 +0800282static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
283 int clk_id, unsigned int freq, int dir)
284{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000285 struct snd_soc_component *component = codec_dai->component;
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000286 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000287 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000288
289 if (clk_id == AD193X_SYSCLK_MCLK) {
290 /* MCLK must be 512 x fs */
291 if (dir == SND_SOC_CLOCK_OUT || freq != 24576000)
292 return -EINVAL;
293
294 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL1,
295 AD193X_PLL_SRC_MASK,
296 AD193X_PLL_DAC_SRC_MCLK |
297 AD193X_PLL_CLK_SRC_MCLK);
298
299 snd_soc_dapm_sync(dapm);
300 return 0;
301 }
Barry Songfab90aa2010-05-21 11:57:01 +0800302 switch (freq) {
303 case 12288000:
304 case 18432000:
305 case 24576000:
306 case 36864000:
307 ad193x->sysclk = freq;
308 return 0;
309 }
310 return -EINVAL;
311}
312
Barry Songa1533d92010-03-19 14:48:33 +0800313static int ad193x_hw_params(struct snd_pcm_substream *substream,
314 struct snd_pcm_hw_params *params,
315 struct snd_soc_dai *dai)
316{
Axel Lin54c96cf2011-10-18 06:25:08 +0800317 int word_len = 0, master_rate = 0;
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000318 struct snd_soc_component *component = dai->component;
319 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Barry Songa1533d92010-03-19 14:48:33 +0800320
321 /* bit size */
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000322 switch (params_width(params)) {
323 case 16:
Barry Songa1533d92010-03-19 14:48:33 +0800324 word_len = 3;
325 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000326 case 20:
Barry Songa1533d92010-03-19 14:48:33 +0800327 word_len = 1;
328 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000329 case 24:
330 case 32:
Barry Songa1533d92010-03-19 14:48:33 +0800331 word_len = 0;
332 break;
333 }
334
Barry Songfab90aa2010-05-21 11:57:01 +0800335 switch (ad193x->sysclk) {
336 case 12288000:
337 master_rate = AD193X_PLL_INPUT_256;
338 break;
339 case 18432000:
340 master_rate = AD193X_PLL_INPUT_384;
341 break;
342 case 24576000:
343 master_rate = AD193X_PLL_INPUT_512;
344 break;
345 case 36864000:
346 master_rate = AD193X_PLL_INPUT_768;
347 break;
348 }
349
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100350 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
Axel Lin54c96cf2011-10-18 06:25:08 +0800351 AD193X_PLL_INPUT_MASK, master_rate);
Barry Songfab90aa2010-05-21 11:57:01 +0800352
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100353 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800354 AD193X_DAC_WORD_LEN_MASK,
355 word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800356
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200357 if (ad193x_has_adc(ad193x))
358 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
359 AD193X_ADC_WORD_LEN_MASK, word_len);
Barry Songa1533d92010-03-19 14:48:33 +0800360
361 return 0;
362}
363
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000364static int ad193x_startup(struct snd_pcm_substream *substream,
365 struct snd_soc_dai *dai)
366{
367 return snd_pcm_hw_constraint_list(substream->runtime, 0,
368 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
369 &constr);
370}
371
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100372static const struct snd_soc_dai_ops ad193x_dai_ops = {
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000373 .startup = ad193x_startup,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000374 .hw_params = ad193x_hw_params,
375 .digital_mute = ad193x_mute,
376 .set_tdm_slot = ad193x_set_tdm_slot,
377 .set_sysclk = ad193x_set_dai_sysclk,
378 .set_fmt = ad193x_set_dai_fmt,
379};
380
381/* codec DAI instance */
382static struct snd_soc_dai_driver ad193x_dai = {
383 .name = "ad193x-hifi",
384 .playback = {
385 .stream_name = "Playback",
386 .channels_min = 2,
387 .channels_max = 8,
388 .rates = SNDRV_PCM_RATE_48000,
389 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
390 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
391 },
392 .capture = {
393 .stream_name = "Capture",
394 .channels_min = 2,
395 .channels_max = 4,
396 .rates = SNDRV_PCM_RATE_48000,
397 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
398 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
399 },
400 .ops = &ad193x_dai_ops,
401};
402
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000403/* codec DAI instance for DAC only */
404static struct snd_soc_dai_driver ad193x_no_adc_dai = {
405 .name = "ad193x-hifi",
406 .playback = {
407 .stream_name = "Playback",
408 .channels_min = 2,
409 .channels_max = 8,
410 .rates = SNDRV_PCM_RATE_48000,
411 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
412 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
413 },
414 .ops = &ad193x_dai_ops,
415};
416
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000417static int ad193x_component_probe(struct snd_soc_component *component)
Barry Songa1533d92010-03-19 14:48:33 +0800418{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000419 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
420 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200421 int num, ret;
Barry Songa1533d92010-03-19 14:48:33 +0800422
423 /* default setting for ad193x */
424
425 /* unmute dac channels */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100426 regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0);
Barry Songa1533d92010-03-19 14:48:33 +0800427 /* de-emphasis: 48kHz, powedown dac */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100428 regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A);
Lars-Peter Clausenbdb2c742015-01-12 13:54:13 +0100429 /* dac in tdm mode */
430 regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x40);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200431
432 /* adc only */
433 if (ad193x_has_adc(ad193x)) {
434 /* high-pass filter enable */
435 regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3);
436 /* sata delay=1, adc aux mode */
437 regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43);
438 }
439
Barry Songa1533d92010-03-19 14:48:33 +0800440 /* pll input: mclki/xi */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100441 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
442 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800443
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200444 /* adc only */
445 if (ad193x_has_adc(ad193x)) {
446 /* add adc controls */
447 num = ARRAY_SIZE(ad193x_adc_snd_controls);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000448 ret = snd_soc_add_component_controls(component,
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200449 ad193x_adc_snd_controls,
450 num);
451 if (ret)
452 return ret;
453
454 /* add adc widgets */
455 num = ARRAY_SIZE(ad193x_adc_widgets);
456 ret = snd_soc_dapm_new_controls(dapm,
457 ad193x_adc_widgets,
458 num);
459 if (ret)
460 return ret;
461
462 /* add adc routes */
463 num = ARRAY_SIZE(ad193x_adc_audio_paths);
464 ret = snd_soc_dapm_add_routes(dapm,
465 ad193x_adc_audio_paths,
466 num);
467 if (ret)
468 return ret;
469 }
470
Xiubo Li5d6be5a2014-03-11 12:43:20 +0800471 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800472}
473
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000474static const struct snd_soc_component_driver soc_component_dev_ad193x = {
475 .probe = ad193x_component_probe,
476 .controls = ad193x_snd_controls,
477 .num_controls = ARRAY_SIZE(ad193x_snd_controls),
478 .dapm_widgets = ad193x_dapm_widgets,
479 .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
480 .dapm_routes = audio_paths,
481 .num_dapm_routes = ARRAY_SIZE(audio_paths),
482 .idle_bias_on = 1,
483 .use_pmdown_time = 1,
484 .endianness = 1,
485 .non_legacy_dai_naming = 1,
Barry Songa1533d92010-03-19 14:48:33 +0800486};
Barry Songa1533d92010-03-19 14:48:33 +0800487
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100488const struct regmap_config ad193x_regmap_config = {
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100489 .max_register = AD193X_NUM_REGS - 1,
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200490};
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100491EXPORT_SYMBOL_GPL(ad193x_regmap_config);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200492
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200493int ad193x_probe(struct device *dev, struct regmap *regmap,
494 enum ad193x_type type)
Barry Songa1533d92010-03-19 14:48:33 +0800495{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000496 struct ad193x_priv *ad193x;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000497
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100498 if (IS_ERR(regmap))
499 return PTR_ERR(regmap);
500
501 ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000502 if (ad193x == NULL)
503 return -ENOMEM;
504
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100505 ad193x->regmap = regmap;
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200506 ad193x->type = type;
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200507
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100508 dev_set_drvdata(dev, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000509
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000510 if (ad193x_has_adc(ad193x))
511 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
512 &ad193x_dai, 1);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000513 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000514 &ad193x_no_adc_dai, 1);
Barry Songa1533d92010-03-19 14:48:33 +0800515}
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100516EXPORT_SYMBOL_GPL(ad193x_probe);
Barry Songa1533d92010-03-19 14:48:33 +0800517
518MODULE_DESCRIPTION("ASoC ad193x driver");
519MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
520MODULE_LICENSE("GPL");