blob: f37ab7eda615533fcf5570d0b4f1af6c479d4cab [file] [log] [blame]
Thomas Gleixner80503b22019-05-24 12:04:09 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Barry Songa1533d92010-03-19 14:48:33 +08002/*
3 * AD193X Audio Codec driver supporting AD1936/7/8/9
4 *
5 * Copyright 2010 Analog Devices Inc.
Barry Songa1533d92010-03-19 14:48:33 +08006 */
7
Barry Songa1533d92010-03-19 14:48:33 +08008#include <linux/module.h>
9#include <linux/kernel.h>
10#include <linux/device.h>
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +010011#include <linux/regmap.h>
Stephen Rothwell1b132ea2010-03-29 15:32:18 +110012#include <linux/slab.h>
Barry Songa1533d92010-03-19 14:48:33 +080013#include <sound/core.h>
14#include <sound/pcm.h>
15#include <sound/pcm_params.h>
16#include <sound/initval.h>
17#include <sound/soc.h>
18#include <sound/tlv.h>
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +010019
Barry Songa1533d92010-03-19 14:48:33 +080020#include "ad193x.h"
21
22/* codec private data */
23struct ad193x_priv {
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +020024 struct regmap *regmap;
Cyrille Pitchene5224f52015-09-29 16:41:43 +020025 enum ad193x_type type;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000026 int sysclk;
Barry Songa1533d92010-03-19 14:48:33 +080027};
28
Barry Songa1533d92010-03-19 14:48:33 +080029/*
30 * AD193X volume/mute/de-emphasis etc. controls
31 */
Lars-Peter Clausenc4e7a4a2011-11-28 17:28:08 +010032static const char * const ad193x_deemp[] = {"None", "48kHz", "44.1kHz", "32kHz"};
Barry Songa1533d92010-03-19 14:48:33 +080033
Takashi Iwaib6592d82014-02-18 09:55:27 +010034static SOC_ENUM_SINGLE_DECL(ad193x_deemp_enum, AD193X_DAC_CTRL2, 1,
35 ad193x_deemp);
Barry Songa1533d92010-03-19 14:48:33 +080036
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010037static const DECLARE_TLV_DB_MINMAX(adau193x_tlv, -9563, 0);
38
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +000039static const unsigned int ad193x_sb[] = {32};
40
41static struct snd_pcm_hw_constraint_list constr = {
42 .list = ad193x_sb,
43 .count = ARRAY_SIZE(ad193x_sb),
44};
45
Barry Songa1533d92010-03-19 14:48:33 +080046static const struct snd_kcontrol_new ad193x_snd_controls[] = {
47 /* DAC volume control */
Lars-Peter Clausen591c0342011-11-28 17:28:07 +010048 SOC_DOUBLE_R_TLV("DAC1 Volume", AD193X_DAC_L1_VOL,
49 AD193X_DAC_R1_VOL, 0, 0xFF, 1, adau193x_tlv),
50 SOC_DOUBLE_R_TLV("DAC2 Volume", AD193X_DAC_L2_VOL,
51 AD193X_DAC_R2_VOL, 0, 0xFF, 1, adau193x_tlv),
52 SOC_DOUBLE_R_TLV("DAC3 Volume", AD193X_DAC_L3_VOL,
53 AD193X_DAC_R3_VOL, 0, 0xFF, 1, adau193x_tlv),
54 SOC_DOUBLE_R_TLV("DAC4 Volume", AD193X_DAC_L4_VOL,
55 AD193X_DAC_R4_VOL, 0, 0xFF, 1, adau193x_tlv),
Barry Songa1533d92010-03-19 14:48:33 +080056
Barry Songa1533d92010-03-19 14:48:33 +080057 /* DAC switch control */
58 SOC_DOUBLE("DAC1 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL1_MUTE,
59 AD193X_DACR1_MUTE, 1, 1),
60 SOC_DOUBLE("DAC2 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL2_MUTE,
61 AD193X_DACR2_MUTE, 1, 1),
62 SOC_DOUBLE("DAC3 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL3_MUTE,
63 AD193X_DACR3_MUTE, 1, 1),
64 SOC_DOUBLE("DAC4 Switch", AD193X_DAC_CHNL_MUTE, AD193X_DACL4_MUTE,
65 AD193X_DACR4_MUTE, 1, 1),
66
Cyrille Pitchene5224f52015-09-29 16:41:43 +020067 /* DAC de-emphasis */
68 SOC_ENUM("Playback Deemphasis", ad193x_deemp_enum),
69};
70
71static const struct snd_kcontrol_new ad193x_adc_snd_controls[] = {
72 /* ADC switch control */
73 SOC_DOUBLE("ADC1 Switch", AD193X_ADC_CTRL0, AD193X_ADCL1_MUTE,
74 AD193X_ADCR1_MUTE, 1, 1),
75 SOC_DOUBLE("ADC2 Switch", AD193X_ADC_CTRL0, AD193X_ADCL2_MUTE,
76 AD193X_ADCR2_MUTE, 1, 1),
77
Barry Songa1533d92010-03-19 14:48:33 +080078 /* ADC high-pass filter */
79 SOC_SINGLE("ADC High Pass Filter Switch", AD193X_ADC_CTRL0,
80 AD193X_ADC_HIGHPASS_FILTER, 1, 0),
Barry Songa1533d92010-03-19 14:48:33 +080081};
82
83static const struct snd_soc_dapm_widget ad193x_dapm_widgets[] = {
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +010084 SND_SOC_DAPM_DAC("DAC", "Playback", SND_SOC_NOPM, 0, 0),
85 SND_SOC_DAPM_PGA("DAC Output", AD193X_DAC_CTRL0, 0, 1, NULL, 0),
Barry Songa1533d92010-03-19 14:48:33 +080086 SND_SOC_DAPM_SUPPLY("PLL_PWR", AD193X_PLL_CLK_CTRL0, 0, 1, NULL, 0),
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +010087 SND_SOC_DAPM_SUPPLY("SYSCLK", AD193X_PLL_CLK_CTRL0, 7, 0, NULL, 0),
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +010088 SND_SOC_DAPM_VMID("VMID"),
Barry Songa1533d92010-03-19 14:48:33 +080089 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
90 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
91 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
92 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
Cyrille Pitchene5224f52015-09-29 16:41:43 +020093};
94
95static const struct snd_soc_dapm_widget ad193x_adc_widgets[] = {
96 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
97 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD193X_ADC_CTRL0, 0, 1, NULL, 0),
Barry Songa1533d92010-03-19 14:48:33 +080098 SND_SOC_DAPM_INPUT("ADC1IN"),
99 SND_SOC_DAPM_INPUT("ADC2IN"),
100};
101
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000102static int ad193x_check_pll(struct snd_soc_dapm_widget *source,
103 struct snd_soc_dapm_widget *sink)
104{
105 struct snd_soc_component *component = snd_soc_dapm_to_component(source->dapm);
106 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
107
108 return !!ad193x->sysclk;
109}
110
Barry Songa1533d92010-03-19 14:48:33 +0800111static const struct snd_soc_dapm_route audio_paths[] = {
Lars-Peter Clausen0718fd22011-11-28 17:28:10 +0100112 { "DAC", NULL, "SYSCLK" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100113 { "DAC Output", NULL, "DAC" },
114 { "DAC Output", NULL, "VMID" },
Lars-Peter Clausen1b86a3f2014-11-05 17:19:53 +0100115 { "DAC1OUT", NULL, "DAC Output" },
116 { "DAC2OUT", NULL, "DAC Output" },
117 { "DAC3OUT", NULL, "DAC Output" },
118 { "DAC4OUT", NULL, "DAC Output" },
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000119 { "SYSCLK", NULL, "PLL_PWR", &ad193x_check_pll },
Barry Songa1533d92010-03-19 14:48:33 +0800120};
121
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200122static const struct snd_soc_dapm_route ad193x_adc_audio_paths[] = {
123 { "ADC", NULL, "SYSCLK" },
124 { "ADC", NULL, "ADC_PWR" },
125 { "ADC", NULL, "ADC1IN" },
126 { "ADC", NULL, "ADC2IN" },
127};
128
129static inline bool ad193x_has_adc(const struct ad193x_priv *ad193x)
130{
131 switch (ad193x->type) {
132 case AD1933:
133 case AD1934:
134 return false;
135 default:
136 break;
137 }
138
139 return true;
140}
141
Barry Songa1533d92010-03-19 14:48:33 +0800142/*
143 * DAI ops entries
144 */
145
Kuninori Morimoto54b59272020-07-09 10:56:20 +0900146static int ad193x_mute(struct snd_soc_dai *dai, int mute, int direction)
Barry Songa1533d92010-03-19 14:48:33 +0800147{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000148 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Barry Songa1533d92010-03-19 14:48:33 +0800149
Axel Lin54c96cf2011-10-18 06:25:08 +0800150 if (mute)
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100151 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800152 AD193X_DAC_MASTER_MUTE,
153 AD193X_DAC_MASTER_MUTE);
154 else
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100155 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800156 AD193X_DAC_MASTER_MUTE, 0);
Barry Songa1533d92010-03-19 14:48:33 +0800157
158 return 0;
159}
160
161static int ad193x_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
162 unsigned int rx_mask, int slots, int width)
163{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000164 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100165 unsigned int channels;
Barry Songa1533d92010-03-19 14:48:33 +0800166
167 switch (slots) {
168 case 2:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100169 channels = AD193X_2_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800170 break;
171 case 4:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100172 channels = AD193X_4_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800173 break;
174 case 8:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100175 channels = AD193X_8_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800176 break;
177 case 16:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100178 channels = AD193X_16_CHANNELS;
Barry Songa1533d92010-03-19 14:48:33 +0800179 break;
180 default:
181 return -EINVAL;
182 }
183
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100184 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
185 AD193X_DAC_CHAN_MASK, channels << AD193X_DAC_CHAN_SHFT);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200186 if (ad193x_has_adc(ad193x))
187 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
188 AD193X_ADC_CHAN_MASK,
189 channels << AD193X_ADC_CHAN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800190
191 return 0;
192}
193
194static int ad193x_set_dai_fmt(struct snd_soc_dai *codec_dai,
195 unsigned int fmt)
196{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000197 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(codec_dai->component);
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100198 unsigned int adc_serfmt = 0;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000199 unsigned int dac_serfmt = 0;
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100200 unsigned int adc_fmt = 0;
201 unsigned int dac_fmt = 0;
Barry Songa1533d92010-03-19 14:48:33 +0800202
203 /* At present, the driver only support AUX ADC mode(SND_SOC_DAIFMT_I2S
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000204 * with TDM), ADC&DAC TDM mode(SND_SOC_DAIFMT_DSP_A) and DAC I2S mode
205 * (SND_SOC_DAIFMT_I2S)
Barry Songa1533d92010-03-19 14:48:33 +0800206 */
207 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
208 case SND_SOC_DAIFMT_I2S:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100209 adc_serfmt |= AD193X_ADC_SERFMT_TDM;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000210 dac_serfmt |= AD193X_DAC_SERFMT_STEREO;
Barry Songa1533d92010-03-19 14:48:33 +0800211 break;
212 case SND_SOC_DAIFMT_DSP_A:
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100213 adc_serfmt |= AD193X_ADC_SERFMT_AUX;
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000214 dac_serfmt |= AD193X_DAC_SERFMT_TDM;
Barry Songa1533d92010-03-19 14:48:33 +0800215 break;
216 default:
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200217 if (ad193x_has_adc(ad193x))
218 return -EINVAL;
Barry Songa1533d92010-03-19 14:48:33 +0800219 }
220
221 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
222 case SND_SOC_DAIFMT_NB_NF: /* normal bit clock + frame */
Barry Songa1533d92010-03-19 14:48:33 +0800223 break;
224 case SND_SOC_DAIFMT_NB_IF: /* normal bclk + invert frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100225 adc_fmt |= AD193X_ADC_LEFT_HIGH;
226 dac_fmt |= AD193X_DAC_LEFT_HIGH;
Barry Songa1533d92010-03-19 14:48:33 +0800227 break;
228 case SND_SOC_DAIFMT_IB_NF: /* invert bclk + normal frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100229 adc_fmt |= AD193X_ADC_BCLK_INV;
230 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800231 break;
Barry Songa1533d92010-03-19 14:48:33 +0800232 case SND_SOC_DAIFMT_IB_IF: /* invert bclk + frm */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100233 adc_fmt |= AD193X_ADC_LEFT_HIGH;
234 adc_fmt |= AD193X_ADC_BCLK_INV;
235 dac_fmt |= AD193X_DAC_LEFT_HIGH;
236 dac_fmt |= AD193X_DAC_BCLK_INV;
Barry Songa1533d92010-03-19 14:48:33 +0800237 break;
238 default:
239 return -EINVAL;
240 }
241
Codrin Ciubotariu90f6e682019-02-18 16:10:32 +0000242 /* For DSP_*, LRCLK's polarity must be inverted */
Codrin Ciubotariuda7260c2019-06-26 13:49:46 +0300243 if (fmt & SND_SOC_DAIFMT_DSP_A)
244 dac_fmt ^= AD193X_DAC_LEFT_HIGH;
Codrin Ciubotariu90f6e682019-02-18 16:10:32 +0000245
Barry Songa1533d92010-03-19 14:48:33 +0800246 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
247 case SND_SOC_DAIFMT_CBM_CFM: /* codec clk & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100248 adc_fmt |= AD193X_ADC_LCR_MASTER;
249 adc_fmt |= AD193X_ADC_BCLK_MASTER;
250 dac_fmt |= AD193X_DAC_LCR_MASTER;
251 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800252 break;
253 case SND_SOC_DAIFMT_CBS_CFM: /* codec clk slave & frm master */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100254 adc_fmt |= AD193X_ADC_LCR_MASTER;
255 dac_fmt |= AD193X_DAC_LCR_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800256 break;
257 case SND_SOC_DAIFMT_CBM_CFS: /* codec clk master & frame slave */
Lars-Peter Clausenb82ca572011-11-28 17:28:11 +0100258 adc_fmt |= AD193X_ADC_BCLK_MASTER;
259 dac_fmt |= AD193X_DAC_BCLK_MASTER;
Barry Songa1533d92010-03-19 14:48:33 +0800260 break;
261 case SND_SOC_DAIFMT_CBS_CFS: /* codec clk & frm slave */
Barry Songa1533d92010-03-19 14:48:33 +0800262 break;
263 default:
264 return -EINVAL;
265 }
266
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200267 if (ad193x_has_adc(ad193x)) {
268 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
269 AD193X_ADC_SERFMT_MASK, adc_serfmt);
270 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL2,
271 AD193X_ADC_FMT_MASK, adc_fmt);
272 }
Codrin Ciubotariubccf9c72019-02-18 16:10:34 +0000273 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL0,
274 AD193X_DAC_SERFMT_MASK, dac_serfmt);
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100275 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL1,
276 AD193X_DAC_FMT_MASK, dac_fmt);
Barry Songa1533d92010-03-19 14:48:33 +0800277
278 return 0;
279}
280
Barry Songfab90aa2010-05-21 11:57:01 +0800281static int ad193x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
282 int clk_id, unsigned int freq, int dir)
283{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000284 struct snd_soc_component *component = codec_dai->component;
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000285 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000286 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Codrin Ciubotariu59529472019-02-18 16:10:36 +0000287
288 if (clk_id == AD193X_SYSCLK_MCLK) {
289 /* MCLK must be 512 x fs */
290 if (dir == SND_SOC_CLOCK_OUT || freq != 24576000)
291 return -EINVAL;
292
293 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL1,
294 AD193X_PLL_SRC_MASK,
295 AD193X_PLL_DAC_SRC_MCLK |
296 AD193X_PLL_CLK_SRC_MCLK);
297
298 snd_soc_dapm_sync(dapm);
299 return 0;
300 }
Barry Songfab90aa2010-05-21 11:57:01 +0800301 switch (freq) {
302 case 12288000:
303 case 18432000:
304 case 24576000:
305 case 36864000:
306 ad193x->sysclk = freq;
307 return 0;
308 }
309 return -EINVAL;
310}
311
Barry Songa1533d92010-03-19 14:48:33 +0800312static int ad193x_hw_params(struct snd_pcm_substream *substream,
313 struct snd_pcm_hw_params *params,
314 struct snd_soc_dai *dai)
315{
Axel Lin54c96cf2011-10-18 06:25:08 +0800316 int word_len = 0, master_rate = 0;
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000317 struct snd_soc_component *component = dai->component;
318 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
Barry Songa1533d92010-03-19 14:48:33 +0800319
320 /* bit size */
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000321 switch (params_width(params)) {
322 case 16:
Barry Songa1533d92010-03-19 14:48:33 +0800323 word_len = 3;
324 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000325 case 20:
Barry Songa1533d92010-03-19 14:48:33 +0800326 word_len = 1;
327 break;
Mark Brownd4dd1fd2014-01-08 18:38:20 +0000328 case 24:
329 case 32:
Barry Songa1533d92010-03-19 14:48:33 +0800330 word_len = 0;
331 break;
332 }
333
Barry Songfab90aa2010-05-21 11:57:01 +0800334 switch (ad193x->sysclk) {
335 case 12288000:
336 master_rate = AD193X_PLL_INPUT_256;
337 break;
338 case 18432000:
339 master_rate = AD193X_PLL_INPUT_384;
340 break;
341 case 24576000:
342 master_rate = AD193X_PLL_INPUT_512;
343 break;
344 case 36864000:
345 master_rate = AD193X_PLL_INPUT_768;
346 break;
347 }
348
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100349 regmap_update_bits(ad193x->regmap, AD193X_PLL_CLK_CTRL0,
Axel Lin54c96cf2011-10-18 06:25:08 +0800350 AD193X_PLL_INPUT_MASK, master_rate);
Barry Songfab90aa2010-05-21 11:57:01 +0800351
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100352 regmap_update_bits(ad193x->regmap, AD193X_DAC_CTRL2,
Axel Lin54c96cf2011-10-18 06:25:08 +0800353 AD193X_DAC_WORD_LEN_MASK,
354 word_len << AD193X_DAC_WORD_LEN_SHFT);
Barry Songa1533d92010-03-19 14:48:33 +0800355
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200356 if (ad193x_has_adc(ad193x))
357 regmap_update_bits(ad193x->regmap, AD193X_ADC_CTRL1,
358 AD193X_ADC_WORD_LEN_MASK, word_len);
Barry Songa1533d92010-03-19 14:48:33 +0800359
360 return 0;
361}
362
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000363static int ad193x_startup(struct snd_pcm_substream *substream,
364 struct snd_soc_dai *dai)
365{
366 return snd_pcm_hw_constraint_list(substream->runtime, 0,
367 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
368 &constr);
369}
370
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100371static const struct snd_soc_dai_ops ad193x_dai_ops = {
Codrin Ciubotariu75c2ecb2019-02-18 16:10:30 +0000372 .startup = ad193x_startup,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000373 .hw_params = ad193x_hw_params,
Kuninori Morimoto54b59272020-07-09 10:56:20 +0900374 .mute_stream = ad193x_mute,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000375 .set_tdm_slot = ad193x_set_tdm_slot,
376 .set_sysclk = ad193x_set_dai_sysclk,
377 .set_fmt = ad193x_set_dai_fmt,
Kuninori Morimoto54b59272020-07-09 10:56:20 +0900378 .no_capture_mute = 1,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000379};
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
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300417/* codec register values to set after reset */
418static void ad193x_reg_default_init(struct ad193x_priv *ad193x)
419{
Colin Ian King78b93b02019-09-06 17:14:04 +0100420 static const struct reg_sequence reg_init[] = {
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300421 { 0, 0x99 }, /* PLL_CLK_CTRL0: pll input: mclki/xi 12.288Mhz */
422 { 1, 0x04 }, /* PLL_CLK_CTRL1: no on-chip Vref */
423 { 2, 0x40 }, /* DAC_CTRL0: TDM mode */
Codrin Ciubotariu8af6b222019-06-27 15:02:08 +0300424 { 3, 0x00 }, /* DAC_CTRL1: reset */
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300425 { 4, 0x1A }, /* DAC_CTRL2: 48kHz de-emphasis, unmute dac */
426 { 5, 0x00 }, /* DAC_CHNL_MUTE: unmute DAC channels */
Codrin Ciubotariu8af6b222019-06-27 15:02:08 +0300427 { 6, 0x00 }, /* DAC_L1_VOL: no attenuation */
428 { 7, 0x00 }, /* DAC_R1_VOL: no attenuation */
429 { 8, 0x00 }, /* DAC_L2_VOL: no attenuation */
430 { 9, 0x00 }, /* DAC_R2_VOL: no attenuation */
431 { 10, 0x00 }, /* DAC_L3_VOL: no attenuation */
432 { 11, 0x00 }, /* DAC_R3_VOL: no attenuation */
433 { 12, 0x00 }, /* DAC_L4_VOL: no attenuation */
434 { 13, 0x00 }, /* DAC_R4_VOL: no attenuation */
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300435 };
Colin Ian King78b93b02019-09-06 17:14:04 +0100436 static const struct reg_sequence reg_adc_init[] = {
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300437 { 14, 0x03 }, /* ADC_CTRL0: high-pass filter enable */
438 { 15, 0x43 }, /* ADC_CTRL1: sata delay=1, adc aux mode */
Codrin Ciubotariu8af6b222019-06-27 15:02:08 +0300439 { 16, 0x00 }, /* ADC_CTRL2: reset */
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300440 };
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300441
Codrin Ciubotariueaba5582019-07-10 13:51:19 +0300442 regmap_multi_reg_write(ad193x->regmap, reg_init, ARRAY_SIZE(reg_init));
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300443
444 if (ad193x_has_adc(ad193x)) {
Codrin Ciubotariueaba5582019-07-10 13:51:19 +0300445 regmap_multi_reg_write(ad193x->regmap, reg_adc_init,
446 ARRAY_SIZE(reg_adc_init));
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300447 }
448}
449
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000450static int ad193x_component_probe(struct snd_soc_component *component)
Barry Songa1533d92010-03-19 14:48:33 +0800451{
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000452 struct ad193x_priv *ad193x = snd_soc_component_get_drvdata(component);
453 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200454 int num, ret;
Barry Songa1533d92010-03-19 14:48:33 +0800455
456 /* default setting for ad193x */
Codrin Ciubotariubc0a5f42019-06-27 15:02:07 +0300457 ad193x_reg_default_init(ad193x);
Barry Songa1533d92010-03-19 14:48:33 +0800458
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200459 /* adc only */
460 if (ad193x_has_adc(ad193x)) {
461 /* add adc controls */
462 num = ARRAY_SIZE(ad193x_adc_snd_controls);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000463 ret = snd_soc_add_component_controls(component,
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200464 ad193x_adc_snd_controls,
465 num);
466 if (ret)
467 return ret;
468
469 /* add adc widgets */
470 num = ARRAY_SIZE(ad193x_adc_widgets);
471 ret = snd_soc_dapm_new_controls(dapm,
472 ad193x_adc_widgets,
473 num);
474 if (ret)
475 return ret;
476
477 /* add adc routes */
478 num = ARRAY_SIZE(ad193x_adc_audio_paths);
479 ret = snd_soc_dapm_add_routes(dapm,
480 ad193x_adc_audio_paths,
481 num);
482 if (ret)
483 return ret;
484 }
485
Xiubo Li5d6be5a2014-03-11 12:43:20 +0800486 return 0;
Barry Songa1533d92010-03-19 14:48:33 +0800487}
488
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000489static const struct snd_soc_component_driver soc_component_dev_ad193x = {
490 .probe = ad193x_component_probe,
491 .controls = ad193x_snd_controls,
492 .num_controls = ARRAY_SIZE(ad193x_snd_controls),
493 .dapm_widgets = ad193x_dapm_widgets,
494 .num_dapm_widgets = ARRAY_SIZE(ad193x_dapm_widgets),
495 .dapm_routes = audio_paths,
496 .num_dapm_routes = ARRAY_SIZE(audio_paths),
497 .idle_bias_on = 1,
498 .use_pmdown_time = 1,
499 .endianness = 1,
500 .non_legacy_dai_naming = 1,
Barry Songa1533d92010-03-19 14:48:33 +0800501};
Barry Songa1533d92010-03-19 14:48:33 +0800502
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100503const struct regmap_config ad193x_regmap_config = {
Lars-Peter Clausen34cbe162011-11-28 17:28:12 +0100504 .max_register = AD193X_NUM_REGS - 1,
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200505};
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100506EXPORT_SYMBOL_GPL(ad193x_regmap_config);
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200507
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200508int ad193x_probe(struct device *dev, struct regmap *regmap,
509 enum ad193x_type type)
Barry Songa1533d92010-03-19 14:48:33 +0800510{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000511 struct ad193x_priv *ad193x;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000512
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100513 if (IS_ERR(regmap))
514 return PTR_ERR(regmap);
515
516 ad193x = devm_kzalloc(dev, sizeof(*ad193x), GFP_KERNEL);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000517 if (ad193x == NULL)
518 return -ENOMEM;
519
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100520 ad193x->regmap = regmap;
Cyrille Pitchene5224f52015-09-29 16:41:43 +0200521 ad193x->type = type;
Lars-Peter Clausen30ab1e72011-09-05 20:46:33 +0200522
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100523 dev_set_drvdata(dev, ad193x);
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000524
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000525 if (ad193x_has_adc(ad193x))
526 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
527 &ad193x_dai, 1);
Kuninori Morimoto89cea5c2018-01-29 04:25:29 +0000528 return devm_snd_soc_register_component(dev, &soc_component_dev_ad193x,
Codrin Ciubotariu7aac8d132019-02-18 16:10:28 +0000529 &ad193x_no_adc_dai, 1);
Barry Songa1533d92010-03-19 14:48:33 +0800530}
Lars-Peter Clausen6c3d7132014-02-17 13:16:54 +0100531EXPORT_SYMBOL_GPL(ad193x_probe);
Barry Songa1533d92010-03-19 14:48:33 +0800532
533MODULE_DESCRIPTION("ASoC ad193x driver");
534MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
535MODULE_LICENSE("GPL");