1
2
3
4
5
6
7
8
9
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/device.h>
15#include <sound/core.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/initval.h>
19#include <sound/soc.h>
20#include <sound/tlv.h>
21#include <linux/spi/spi.h>
22#include <linux/regmap.h>
23
24#include "ad1836.h"
25
26enum ad1836_type {
27 AD1835,
28 AD1836,
29 AD1838,
30};
31
32
33struct ad1836_priv {
34 enum ad1836_type type;
35 struct regmap *regmap;
36};
37
38
39
40
41static const char *ad1836_deemp[] = {"None", "44.1kHz", "32kHz", "48kHz"};
42
43static SOC_ENUM_SINGLE_DECL(ad1836_deemp_enum,
44 AD1836_DAC_CTRL1, 8, ad1836_deemp);
45
46#define AD1836_DAC_VOLUME(x) \
47 SOC_DOUBLE_R("DAC" #x " Playback Volume", AD1836_DAC_L_VOL(x), \
48 AD1836_DAC_R_VOL(x), 0, 0x3FF, 0)
49
50#define AD1836_DAC_SWITCH(x) \
51 SOC_DOUBLE("DAC" #x " Playback Switch", AD1836_DAC_CTRL2, \
52 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
53
54#define AD1836_ADC_SWITCH(x) \
55 SOC_DOUBLE("ADC" #x " Capture Switch", AD1836_ADC_CTRL2, \
56 AD1836_MUTE_LEFT(x), AD1836_MUTE_RIGHT(x), 1, 1)
57
58static const struct snd_kcontrol_new ad183x_dac_controls[] = {
59 AD1836_DAC_VOLUME(1),
60 AD1836_DAC_SWITCH(1),
61 AD1836_DAC_VOLUME(2),
62 AD1836_DAC_SWITCH(2),
63 AD1836_DAC_VOLUME(3),
64 AD1836_DAC_SWITCH(3),
65 AD1836_DAC_VOLUME(4),
66 AD1836_DAC_SWITCH(4),
67};
68
69static const struct snd_soc_dapm_widget ad183x_dac_dapm_widgets[] = {
70 SND_SOC_DAPM_OUTPUT("DAC1OUT"),
71 SND_SOC_DAPM_OUTPUT("DAC2OUT"),
72 SND_SOC_DAPM_OUTPUT("DAC3OUT"),
73 SND_SOC_DAPM_OUTPUT("DAC4OUT"),
74};
75
76static const struct snd_soc_dapm_route ad183x_dac_routes[] = {
77 { "DAC1OUT", NULL, "DAC" },
78 { "DAC2OUT", NULL, "DAC" },
79 { "DAC3OUT", NULL, "DAC" },
80 { "DAC4OUT", NULL, "DAC" },
81};
82
83static const struct snd_kcontrol_new ad183x_adc_controls[] = {
84 AD1836_ADC_SWITCH(1),
85 AD1836_ADC_SWITCH(2),
86 AD1836_ADC_SWITCH(3),
87};
88
89static const struct snd_soc_dapm_widget ad183x_adc_dapm_widgets[] = {
90 SND_SOC_DAPM_INPUT("ADC1IN"),
91 SND_SOC_DAPM_INPUT("ADC2IN"),
92};
93
94static const struct snd_soc_dapm_route ad183x_adc_routes[] = {
95 { "ADC", NULL, "ADC1IN" },
96 { "ADC", NULL, "ADC2IN" },
97};
98
99static const struct snd_kcontrol_new ad183x_controls[] = {
100
101 SOC_SINGLE("ADC High Pass Filter Switch", AD1836_ADC_CTRL1,
102 AD1836_ADC_HIGHPASS_FILTER, 1, 0),
103
104
105 SOC_ENUM("Playback Deemphasis", ad1836_deemp_enum),
106};
107
108static const struct snd_soc_dapm_widget ad183x_dapm_widgets[] = {
109 SND_SOC_DAPM_DAC("DAC", "Playback", AD1836_DAC_CTRL1,
110 AD1836_DAC_POWERDOWN, 1),
111 SND_SOC_DAPM_ADC("ADC", "Capture", SND_SOC_NOPM, 0, 0),
112 SND_SOC_DAPM_SUPPLY("ADC_PWR", AD1836_ADC_CTRL1,
113 AD1836_ADC_POWERDOWN, 1, NULL, 0),
114};
115
116static const struct snd_soc_dapm_route ad183x_dapm_routes[] = {
117 { "DAC", NULL, "ADC_PWR" },
118 { "ADC", NULL, "ADC_PWR" },
119};
120
121static const DECLARE_TLV_DB_SCALE(ad1836_in_tlv, 0, 300, 0);
122
123static const struct snd_kcontrol_new ad1836_controls[] = {
124 SOC_DOUBLE_TLV("ADC2 Capture Volume", AD1836_ADC_CTRL1, 3, 0, 4, 0,
125 ad1836_in_tlv),
126};
127
128
129
130
131
132static int ad1836_set_dai_fmt(struct snd_soc_dai *codec_dai,
133 unsigned int fmt)
134{
135 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
136
137
138
139 case SND_SOC_DAIFMT_DSP_A:
140 break;
141 default:
142 return -EINVAL;
143 }
144
145 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
146 case SND_SOC_DAIFMT_IB_IF:
147 break;
148 default:
149 return -EINVAL;
150 }
151
152 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
153
154 case SND_SOC_DAIFMT_CBM_CFM:
155 break;
156 default:
157 return -EINVAL;
158 }
159
160 return 0;
161}
162
163static int ad1836_hw_params(struct snd_pcm_substream *substream,
164 struct snd_pcm_hw_params *params,
165 struct snd_soc_dai *dai)
166{
167 struct ad1836_priv *ad1836 = snd_soc_component_get_drvdata(dai->component);
168 int word_len = 0;
169
170
171 switch (params_width(params)) {
172 case 16:
173 word_len = AD1836_WORD_LEN_16;
174 break;
175 case 20:
176 word_len = AD1836_WORD_LEN_20;
177 break;
178 case 24:
179 case 32:
180 word_len = AD1836_WORD_LEN_24;
181 break;
182 default:
183 return -EINVAL;
184 }
185
186 regmap_update_bits(ad1836->regmap, AD1836_DAC_CTRL1,
187 AD1836_DAC_WORD_LEN_MASK,
188 word_len << AD1836_DAC_WORD_LEN_OFFSET);
189
190 regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
191 AD1836_ADC_WORD_LEN_MASK,
192 word_len << AD1836_ADC_WORD_OFFSET);
193
194 return 0;
195}
196
197static const struct snd_soc_dai_ops ad1836_dai_ops = {
198 .hw_params = ad1836_hw_params,
199 .set_fmt = ad1836_set_dai_fmt,
200};
201
202#define AD183X_DAI(_name, num_dacs, num_adcs) \
203{ \
204 .name = _name "-hifi", \
205 .playback = { \
206 .stream_name = "Playback", \
207 .channels_min = 2, \
208 .channels_max = (num_dacs) * 2, \
209 .rates = SNDRV_PCM_RATE_48000, \
210 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
211 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
212 }, \
213 .capture = { \
214 .stream_name = "Capture", \
215 .channels_min = 2, \
216 .channels_max = (num_adcs) * 2, \
217 .rates = SNDRV_PCM_RATE_48000, \
218 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE | \
219 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE, \
220 }, \
221 .ops = &ad1836_dai_ops, \
222}
223
224static struct snd_soc_dai_driver ad183x_dais[] = {
225 [AD1835] = AD183X_DAI("ad1835", 4, 1),
226 [AD1836] = AD183X_DAI("ad1836", 3, 2),
227 [AD1838] = AD183X_DAI("ad1838", 3, 1),
228};
229
230#ifdef CONFIG_PM
231static int ad1836_suspend(struct snd_soc_component *component)
232{
233 struct ad1836_priv *ad1836 = snd_soc_component_get_drvdata(component);
234
235 return regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
236 AD1836_ADC_SERFMT_MASK, 0);
237}
238
239static int ad1836_resume(struct snd_soc_component *component)
240{
241 struct ad1836_priv *ad1836 = snd_soc_component_get_drvdata(component);
242
243 return regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
244 AD1836_ADC_SERFMT_MASK, AD1836_ADC_AUX);
245}
246#else
247#define ad1836_suspend NULL
248#define ad1836_resume NULL
249#endif
250
251static int ad1836_probe(struct snd_soc_component *component)
252{
253 struct ad1836_priv *ad1836 = snd_soc_component_get_drvdata(component);
254 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
255 int num_dacs, num_adcs;
256 int ret = 0;
257 int i;
258
259 num_dacs = ad183x_dais[ad1836->type].playback.channels_max / 2;
260 num_adcs = ad183x_dais[ad1836->type].capture.channels_max / 2;
261
262
263
264 regmap_write(ad1836->regmap, AD1836_DAC_CTRL1, 0x300);
265
266 regmap_write(ad1836->regmap, AD1836_DAC_CTRL2, 0x0);
267
268 regmap_write(ad1836->regmap, AD1836_ADC_CTRL1, 0x100);
269
270 regmap_write(ad1836->regmap, AD1836_ADC_CTRL2, 0x180);
271
272 for (i = 1; i <= num_dacs; ++i) {
273 regmap_write(ad1836->regmap, AD1836_DAC_L_VOL(i), 0x3FF);
274 regmap_write(ad1836->regmap, AD1836_DAC_R_VOL(i), 0x3FF);
275 }
276
277 if (ad1836->type == AD1836) {
278
279 regmap_write(ad1836->regmap, AD1836_ADC_CTRL3, 0x3A);
280 ret = snd_soc_add_component_controls(component, ad1836_controls,
281 ARRAY_SIZE(ad1836_controls));
282 if (ret)
283 return ret;
284 } else {
285 regmap_write(ad1836->regmap, AD1836_ADC_CTRL3, 0x00);
286 }
287
288 ret = snd_soc_add_component_controls(component, ad183x_dac_controls, num_dacs * 2);
289 if (ret)
290 return ret;
291
292 ret = snd_soc_add_component_controls(component, ad183x_adc_controls, num_adcs);
293 if (ret)
294 return ret;
295
296 ret = snd_soc_dapm_new_controls(dapm, ad183x_dac_dapm_widgets, num_dacs);
297 if (ret)
298 return ret;
299
300 ret = snd_soc_dapm_new_controls(dapm, ad183x_adc_dapm_widgets, num_adcs);
301 if (ret)
302 return ret;
303
304 ret = snd_soc_dapm_add_routes(dapm, ad183x_dac_routes, num_dacs);
305 if (ret)
306 return ret;
307
308 ret = snd_soc_dapm_add_routes(dapm, ad183x_adc_routes, num_adcs);
309 if (ret)
310 return ret;
311
312 return ret;
313}
314
315
316static void ad1836_remove(struct snd_soc_component *component)
317{
318 struct ad1836_priv *ad1836 = snd_soc_component_get_drvdata(component);
319
320 regmap_update_bits(ad1836->regmap, AD1836_ADC_CTRL2,
321 AD1836_ADC_SERFMT_MASK, 0);
322}
323
324static const struct snd_soc_component_driver soc_component_dev_ad1836 = {
325 .probe = ad1836_probe,
326 .remove = ad1836_remove,
327 .suspend = ad1836_suspend,
328 .resume = ad1836_resume,
329 .controls = ad183x_controls,
330 .num_controls = ARRAY_SIZE(ad183x_controls),
331 .dapm_widgets = ad183x_dapm_widgets,
332 .num_dapm_widgets = ARRAY_SIZE(ad183x_dapm_widgets),
333 .dapm_routes = ad183x_dapm_routes,
334 .num_dapm_routes = ARRAY_SIZE(ad183x_dapm_routes),
335 .idle_bias_on = 1,
336 .use_pmdown_time = 1,
337 .endianness = 1,
338 .non_legacy_dai_naming = 1,
339};
340
341static const struct reg_default ad1836_reg_defaults[] = {
342 { AD1836_DAC_CTRL1, 0x0000 },
343 { AD1836_DAC_CTRL2, 0x0000 },
344 { AD1836_DAC_L_VOL(0), 0x0000 },
345 { AD1836_DAC_R_VOL(0), 0x0000 },
346 { AD1836_DAC_L_VOL(1), 0x0000 },
347 { AD1836_DAC_R_VOL(1), 0x0000 },
348 { AD1836_DAC_L_VOL(2), 0x0000 },
349 { AD1836_DAC_R_VOL(2), 0x0000 },
350 { AD1836_DAC_L_VOL(3), 0x0000 },
351 { AD1836_DAC_R_VOL(3), 0x0000 },
352 { AD1836_ADC_CTRL1, 0x0000 },
353 { AD1836_ADC_CTRL2, 0x0000 },
354 { AD1836_ADC_CTRL3, 0x0000 },
355};
356
357static const struct regmap_config ad1836_regmap_config = {
358 .val_bits = 12,
359 .reg_bits = 4,
360 .read_flag_mask = 0x08,
361
362 .max_register = AD1836_ADC_CTRL3,
363 .reg_defaults = ad1836_reg_defaults,
364 .num_reg_defaults = ARRAY_SIZE(ad1836_reg_defaults),
365 .cache_type = REGCACHE_RBTREE,
366};
367
368static int ad1836_spi_probe(struct spi_device *spi)
369{
370 struct ad1836_priv *ad1836;
371 int ret;
372
373 ad1836 = devm_kzalloc(&spi->dev, sizeof(struct ad1836_priv),
374 GFP_KERNEL);
375 if (ad1836 == NULL)
376 return -ENOMEM;
377
378 ad1836->regmap = devm_regmap_init_spi(spi, &ad1836_regmap_config);
379 if (IS_ERR(ad1836->regmap))
380 return PTR_ERR(ad1836->regmap);
381
382 ad1836->type = spi_get_device_id(spi)->driver_data;
383
384 spi_set_drvdata(spi, ad1836);
385
386 ret = devm_snd_soc_register_component(&spi->dev,
387 &soc_component_dev_ad1836, &ad183x_dais[ad1836->type], 1);
388 return ret;
389}
390
391static const struct spi_device_id ad1836_ids[] = {
392 { "ad1835", AD1835 },
393 { "ad1836", AD1836 },
394 { "ad1837", AD1835 },
395 { "ad1838", AD1838 },
396 { "ad1839", AD1838 },
397 { },
398};
399MODULE_DEVICE_TABLE(spi, ad1836_ids);
400
401static struct spi_driver ad1836_spi_driver = {
402 .driver = {
403 .name = "ad1836",
404 },
405 .probe = ad1836_spi_probe,
406 .id_table = ad1836_ids,
407};
408
409module_spi_driver(ad1836_spi_driver);
410
411MODULE_DESCRIPTION("ASoC ad1836 driver");
412MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
413MODULE_LICENSE("GPL");
414