blob: c16c9969d1a06698a5abdb507b4b5d656513f54c [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
103static const struct snd_soc_dapm_route audio_paths[] = {
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +0100104 { "DAC", NULL, "SYSCLK" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100105 { "DAC Output", NULL, "DAC" },
106 { "DAC Output", NULL, "VMID" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100107 { "DAC1OUT", NULL, "DAC Output" },
108 { "DAC2OUT", NULL, "DAC Output" },
109 { "DAC3OUT", NULL, "DAC Output" },
110 { "DAC4OUT", NULL, "DAC Output" },
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +0100111 { "SYSCLK", NULL, "PLL_PWR" },
Barry Songa1533d92010-03-19 14:48:33 +0800112};
113
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200114static const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = {
115 { "ADC", NULL, "SYSCLK" },
116 { "ADC", NULL, "ADC_PWR" },
117 { "ADC", NULL, "ADC1IN" },
118 { "ADC", NULL, "ADC2IN" },
119};
120
121static inline bool ad193x_has_adc(const struct ad193x_priv *ad193x)
122{
123 switch (ad193x->type) {
124 case AD1933:
125 case AD1934:
126 return false;
127 default:
128 break;
129 }
130
131 return true;
132}
133
Barry Songa1533d92010-03-19 14:48:33 +0800134/*
135 * DAI ops entries
136 */
137
138static int ad193x_mute(struct snd_soc_dai *dai, int mute)
139{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000140 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Barry Songa1533d92010-03-19 14:48:33 +0800141
Axel Lin54c96cf2011-10-18 06:25:08 +0800142 if (mute)
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100143 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800144 AD193X_DAC_MASTER_MUTE,
145 AD193X_DAC_MASTER_MUTE);
146 else
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100147 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800148 AD193X_DAC_MASTER_MUTE, 0);
Barry Songa1533d92010-03-19 14:48:33 +0800149
150 return 0;
151}
152
153static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
154 unsigned int rx_mask, int slots, int width)
155{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000156 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100157 unsigned int channels;
Barry Songa1533d92010-03-19 14:48:33 +0800158
159 switch (slots) {
160 case 2:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100161 channels = AD193X_2_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800162 break;
163 case 4:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100164 channels = AD193X_4_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800165 break;
166 case 8:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100167 channels = AD193X_8_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800168 break;
169 case 16:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100170 channels = AD193X_16_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800171 break;
172 default:
173 return -EINVAL;
174 }
175
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100176 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
177 AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200178 if (ad193x_has_adc(ad193x))
179 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
180 AD193X_ADC_CHAN_MASK,
181 channels << AD193X_ADC_CHAN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800182
183 return 0;
184}
185
186static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
187 unsigned int fmt)
188{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000189 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100190 unsigned int adc_serfmt = 0;
191 unsigned int adc_fmt = 0;
192 unsigned int dac_fmt = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800193
194 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
195 * with TDM) and ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A)
196 */
197 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
198 case SND_SOC_DAIFMT_I2S:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100199 adc_serfmt |= AD193X_ADC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800200 break;
201 case SND_SOC_DAIFMT_DSP_A:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100202 adc_serfmt |= AD193X_ADC_SERFMT_AUX;
Barry Songa1533d92010-03-19 14:48:33 +0800203 break;
204 default:
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200205 if (ad193x_has_adc(ad193x))
206 return -EINVAL;
207 break;
Barry Songa1533d92010-03-19 14:48:33 +0800208 }
209
210 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
211 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songa1533d92010-03-19 14:48:33 +0800212 break;
213 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100214 adc_fmt |= AD193X_ADC_LEFT_HIGH;
215 dac_fmt |= AD193X_DAC_LEFT_HIGH;
Barry Songa1533d92010-03-19 14:48:33 +0800216 break;
217 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100218 adc_fmt |= AD193X_ADC_BCLK_INV;
219 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800220 break;
Barry Songa1533d92010-03-19 14:48:33 +0800221 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100222 adc_fmt |= AD193X_ADC_LEFT_HIGH;
223 adc_fmt |= AD193X_ADC_BCLK_INV;
224 dac_fmt |= AD193X_DAC_LEFT_HIGH;
225 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800226 break;
227 default:
228 return -EINVAL;
229 }
230
231 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
232 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100233 adc_fmt |= AD193X_ADC_LCR_MASTER;
234 adc_fmt |= AD193X_ADC_BCLK_MASTER;
235 dac_fmt |= AD193X_DAC_LCR_MASTER;
236 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800237 break;
238 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100239 adc_fmt |= AD193X_ADC_LCR_MASTER;
240 dac_fmt |= AD193X_DAC_LCR_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800241 break;
242 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100243 adc_fmt |= AD193X_ADC_BCLK_MASTER;
244 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800245 break;
246 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songa1533d92010-03-19 14:48:33 +0800247 break;
248 default:
249 return -EINVAL;
250 }
251
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200252 if (ad193x_has_adc(ad193x)) {
253 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
254 AD193X_ADC_SERFMT_MASK, adc_serfmt);
255 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
256 AD193X_ADC_FMT_MASK, adc_fmt);
257 }
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100258 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
259 AD193X_DAC_FMT_MASK, dac_fmt);
Barry Songa1533d92010-03-19 14:48:33 +0800260
261 return 0;
262}
263
Barry Songfab90aa2010-05-21 11:57:01 +0800264static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
265 int clk_id, unsigned int freq, int dir)
266{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000267 struct snd_soc_component *component = codec_dai->component;
268 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Barry Songfab90aa2010-05-21 11:57:01 +0800269 switch (freq) {
270 case 12288000:
271 case 18432000:
272 case 24576000:
273 case 36864000:
274 ad193x->sysclk = freq;
275 return 0;
276 }
277 return -EINVAL;
278}
279
Barry Songa1533d92010-03-19 14:48:33 +0800280static int ad193x_hw_params(struct snd_pcm_substream *substream,
281 struct snd_pcm_hw_params *params,
282 struct snd_soc_dai *dai)
283{
Axel Lin54c96cf2011-10-18 06:25:08 +0800284 int word_len = 0, master_rate = 0;
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000285 struct snd_soc_component *component = dai->component;
286 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Barry Songa1533d92010-03-19 14:48:33 +0800287
288 /* bit size */
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000289 switch (params_width(params)) {
290 case 16:
Barry Songa1533d92010-03-19 14:48:33 +0800291 word_len = 3;
292 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000293 case 20:
Barry Songa1533d92010-03-19 14:48:33 +0800294 word_len = 1;
295 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000296 case 24:
297 case 32:
Barry Songa1533d92010-03-19 14:48:33 +0800298 word_len = 0;
299 break;
300 }
301
Barry Songfab90aa2010-05-21 11:57:01 +0800302 switch (ad193x->sysclk) {
303 case 12288000:
304 master_rate = AD193X_PLL_INPUT_256;
305 break;
306 case 18432000:
307 master_rate = AD193X_PLL_INPUT_384;
308 break;
309 case 24576000:
310 master_rate = AD193X_PLL_INPUT_512;
311 break;
312 case 36864000:
313 master_rate = AD193X_PLL_INPUT_768;
314 break;
315 }
316
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100317 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
Axel Lin54c96cf2011-10-18 06:25:08 +0800318 AD193X_PLL_INPUT_MASK, master_rate);
Barry Songfab90aa2010-05-21 11:57:01 +0800319
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100320 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800321 AD193X_DAC_WORD_LEN_MASK,
322 word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800323
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200324 if (ad193x_has_adc(ad193x))
325 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
326 AD193X_ADC_WORD_LEN_MASK, word_len);
Barry Songa1533d92010-03-19 14:48:33 +0800327
328 return 0;
329}
330
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000331static int ad193x_startup(struct snd_pcm_substream *substream,
332 struct snd_soc_dai *dai)
333{
334 return snd_pcm_hw_constraint_list(substream->runtime, 0,
335 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
336 &constr);
337}
338
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100339static const struct snd_soc_dai_ops ad193x_dai_ops = {
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000340 .startup = ad193x_startup,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000341 .hw_params = ad193x_hw_params,
342 .digital_mute = ad193x_mute,
343 .set_tdm_slot = ad193x_set_tdm_slot,
344 .set_sysclk = ad193x_set_dai_sysclk,
345 .set_fmt = ad193x_set_dai_fmt,
346};
347
348/* codec DAI instance */
349static struct snd_soc_dai_driver ad193x_dai = {
350 .name = "ad193x-hifi",
351 .playback = {
352 .stream_name = "Playback",
353 .channels_min = 2,
354 .channels_max = 8,
355 .rates = SNDRV_PCM_RATE_48000,
356 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
357 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
358 },
359 .capture = {
360 .stream_name = "Capture",
361 .channels_min = 2,
362 .channels_max = 4,
363 .rates = SNDRV_PCM_RATE_48000,
364 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
365 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
366 },
367 .ops = &ad193x_dai_ops,
368};
369
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000370/* codec DAI instance for DAC only */
371static struct snd_soc_dai_driver ad193x_no_adc_dai = {
372 .name = "ad193x-hifi",
373 .playback = {
374 .stream_name = "Playback",
375 .channels_min = 2,
376 .channels_max = 8,
377 .rates = SNDRV_PCM_RATE_48000,
378 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE |
379 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE,
380 },
381 .ops = &ad193x_dai_ops,
382};
383
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000384static int ad193x_component_probe(struct snd_soc_component *component)
Barry Songa1533d92010-03-19 14:48:33 +0800385{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000386 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
387 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200388 int num, ret;
Barry Songa1533d92010-03-19 14:48:33 +0800389
390 /* default setting for ad193x */
391
392 /* unmute dac channels */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100393 regmap_write(ad193x->regmap, AD193X_DAC_CHNL_MUTE, 0x0);
Barry Songa1533d92010-03-19 14:48:33 +0800394 /* de-emphasis: 48kHz, powedown dac */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100395 regmap_write(ad193x->regmap, AD193X_DAC_CTRL2, 0x1A);
Lars-Peter Clausenbdb2c742015-01-12 13:54:13 +0100396 /* dac in tdm mode */
397 regmap_write(ad193x->regmap, AD193X_DAC_CTRL0, 0x40);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200398
399 /* adc only */
400 if (ad193x_has_adc(ad193x)) {
401 /* high-pass filter enable */
402 regmap_write(ad193x->regmap, AD193X_ADC_CTRL0, 0x3);
403 /* sata delay=1, adc aux mode */
404 regmap_write(ad193x->regmap, AD193X_ADC_CTRL1, 0x43);
405 }
406
Barry Songa1533d92010-03-19 14:48:33 +0800407 /* pll input: mclki/xi */
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100408 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL0, 0x99); /* mclk=24.576Mhz: 0x9D; mclk=12.288Mhz: 0x99 */
409 regmap_write(ad193x->regmap, AD193X_PLL_CLK_CTRL1, 0x04);
Barry Songa1533d92010-03-19 14:48:33 +0800410
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200411 /* adc only */
412 if (ad193x_has_adc(ad193x)) {
413 /* add adc controls */
414 num = ARRAY_SIZE(ad193x_adc_snd_controls);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000415 ret = snd_soc_add_component_controls(component,
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200416 ad193x_adc_snd_controls,
417 num);
418 if (ret)
419 return ret;
420
421 /* add adc widgets */
422 num = ARRAY_SIZE(ad193x_adc_widgets);
423 ret = snd_soc_dapm_new_controls(dapm,
424 ad193x_adc_widgets,
425 num);
426 if (ret)
427 return ret;
428
429 /* add adc routes */
430 num = ARRAY_SIZE(ad193x_adc_audio_paths);
431 ret = snd_soc_dapm_add_routes(dapm,
432 ad193x_adc_audio_paths,
433 num);
434 if (ret)
435 return ret;
436 }
437
Xiubo Li5d6be5a2014-03-11 12:43:20 +0800438 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800439}
440
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000441static const struct snd_soc_component_driver soc_component_dev_ad193x = {
442 .probe = ad193x_component_probe,
443 .controls = ad193x_snd_controls,
444 .num_controls = ARRAY_SIZE(ad193x_snd_controls),
445 .dapm_widgets = ad193x_dapm_widgets,
446 .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
447 .dapm_routes = audio_paths,
448 .num_dapm_routes = ARRAY_SIZE(audio_paths),
449 .idle_bias_on = 1,
450 .use_pmdown_time = 1,
451 .endianness = 1,
452 .non_legacy_dai_naming = 1,
Barry Songa1533d92010-03-19 14:48:33 +0800453};
Barry Songa1533d92010-03-19 14:48:33 +0800454
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100455const struct regmap_config ad193x_regmap_config = {
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100456 .max_register = AD193X_NUM_REGS - 1,
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200457};
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100458EXPORT_SYMBOL_GPL(ad193x_regmap_config);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200459
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200460int ad193x_probe(struct device *dev, struct regmap *regmap,
461 enum ad193x_type type)
Barry Songa1533d92010-03-19 14:48:33 +0800462{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000463 struct ad193x_priv *ad193x;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000464
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100465 if (IS_ERR(regmap))
466 return PTR_ERR(regmap);
467
468 ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000469 if (ad193x == NULL)
470 return -ENOMEM;
471
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100472 ad193x->regmap = regmap;
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200473 ad193x->type = type;
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200474
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100475 dev_set_drvdata(dev, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000476
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000477 if (ad193x_has_adc(ad193x))
478 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
479 &ad193x_dai, 1);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000480 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000481 &ad193x_no_adc_dai, 1);
Barry Songa1533d92010-03-19 14:48:33 +0800482}
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100483EXPORT_SYMBOL_GPL(ad193x_probe);
Barry Songa1533d92010-03-19 14:48:33 +0800484
485MODULE_DESCRIPTION("ASoC ad193x driver");
486MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
487MODULE_LICENSE("GPL");