0b6168b4f280eb6ca83252ad1c76e7b111d8ebb3
[openwrt/staging/jow.git] /
1 From b41def5d536b4389e2648286c03e1354a3cc1523 Mon Sep 17 00:00:00 2001
2 From: escalator2015 <jmtasende@gmail.com>
3 Date: Tue, 24 May 2016 16:20:09 +0100
4 Subject: [PATCH 077/454] New driver for RRA DigiDAC1 soundcard using WM8741 +
5 WM8804
6
7 ---
8 sound/soc/bcm/Kconfig | 8 +
9 sound/soc/bcm/Makefile | 2 +
10 sound/soc/bcm/digidac1-soundcard.c | 422 +++++++++++++++++++++++++++++
11 3 files changed, 432 insertions(+)
12 create mode 100644 sound/soc/bcm/digidac1-soundcard.c
13
14 --- a/sound/soc/bcm/Kconfig
15 +++ b/sound/soc/bcm/Kconfig
16 @@ -109,3 +109,11 @@ config SND_AUDIOINJECTOR_PI_SOUNDCARD
17 select SND_SOC_WM8731
18 help
19 Say Y or M if you want to add support for audioinjector.net Pi Hat
20 +
21 +config SND_DIGIDAC1_SOUNDCARD
22 + tristate "Support for Red Rocks Audio DigiDAC1"
23 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
24 + select SND_SOC_WM8804
25 + select SND_SOC_WM8741
26 + help
27 + Say Y or M if you want to add support for Red Rocks Audio DigiDAC1 board.
28 --- a/sound/soc/bcm/Makefile
29 +++ b/sound/soc/bcm/Makefile
30 @@ -22,6 +22,7 @@ snd-soc-iqaudio-dac-objs := iqaudio-dac.
31 snd-soc-iqaudio-digi-objs := iqaudio_digi.o
32 snd-soc-raspidac3-objs := raspidac3.o
33 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
34 +snd-soc-digidac1-soundcard-objs := digidac1-soundcard.o
35
36 obj-$(CONFIG_SND_BCM2708_SOC_ADAU1977_ADC) += snd-soc-adau1977-adc.o
37 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
38 @@ -36,4 +37,5 @@ obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC
39 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI) += snd-soc-iqaudio-digi.o
40 obj-$(CONFIG_SND_BCM2708_SOC_RASPIDAC3) += snd-soc-raspidac3.o
41 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
42 +obj-$(CONFIG_SND_DIGIDAC1_SOUNDCARD) += snd-soc-digidac1-soundcard.o
43
44 --- /dev/null
45 +++ b/sound/soc/bcm/digidac1-soundcard.c
46 @@ -0,0 +1,422 @@
47 +/*
48 + * ASoC Driver for RRA DigiDAC1
49 + * Copyright 2016
50 + * Author: José M. Tasende <vintage@redrocksaudio.es>
51 + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
52 + * and the Wolfson card driver by Nikesh Oswal, <Nikesh.Oswal@wolfsonmicro.com>
53 + * This program is free software; you can redistribute it and/or
54 + * modify it under the terms of the GNU General Public License
55 + * version 2 as published by the Free Software Foundation.
56 + *
57 + * This program is distributed in the hope that it will be useful, but
58 + * WITHOUT ANY WARRANTY; without even the implied warranty of
59 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
60 + * General Public License for more details.
61 + */
62 +
63 +#include <linux/module.h>
64 +#include <linux/platform_device.h>
65 +#include <linux/i2c.h>
66 +#include <sound/core.h>
67 +#include <sound/pcm.h>
68 +#include <sound/pcm_params.h>
69 +#include <sound/soc.h>
70 +#include <sound/jack.h>
71 +#include <sound/soc-dapm.h>
72 +#include <sound/tlv.h>
73 +#include <linux/regulator/consumer.h>
74 +
75 +#include "../codecs/wm8804.h"
76 +#include "../codecs/wm8741.h"
77 +
78 +#define WM8741_NUM_SUPPLIES 2
79 +
80 +/* codec private data */
81 +struct wm8741_priv {
82 + struct wm8741_platform_data pdata;
83 + struct regmap *regmap;
84 + struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
85 + unsigned int sysclk;
86 + const struct snd_pcm_hw_constraint_list *sysclk_constraints;
87 +};
88 +
89 +static int samplerate = 44100;
90 +
91 +/* New Alsa Controls not exposed by original wm8741 codec driver */
92 +/* in actual driver the att. adjustment is wrong because */
93 +/* this DAC has a coarse attenuation register with 4dB steps */
94 +/* and a fine level register with 0.125dB steps */
95 +/* each register has 32 steps so combining both we have 1024 steps */
96 +/* of 0.125 dB. */
97 +/* The original level controls from driver are removed at startup */
98 +/* and replaced by the corrected ones. */
99 +/* The same wm8741 driver can be used for wm8741 and wm8742 devices */
100 +
101 +static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, 0, 13, 0);
102 +static const DECLARE_TLV_DB_SCALE(dac_tlv_coarse, -12700, 400, 1);
103 +static const char *w8741_dither[4] = {"Off", "RPDF", "TPDF", "HPDF"};
104 +static const char *w8741_filter[5] = {
105 + "Type 1", "Type 2", "Type 3", "Type 4", "Type 5"};
106 +static const char *w8741_switch[2] = {"Off", "On"};
107 +static const struct soc_enum w8741_enum[] = {
108 +SOC_ENUM_SINGLE(WM8741_MODE_CONTROL_2, 0, 4, w8741_dither),/* dithering type */
109 +SOC_ENUM_SINGLE(WM8741_FILTER_CONTROL, 0, 5, w8741_filter),/* filter type */
110 +SOC_ENUM_SINGLE(WM8741_FORMAT_CONTROL, 6, 2, w8741_switch),/* phase invert */
111 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 0, 2, w8741_switch),/* volume ramp */
112 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 3, 2, w8741_switch),/* soft mute */
113 +};
114 +
115 +static const struct snd_kcontrol_new w8741_snd_controls_stereo[] = {
116 +SOC_DOUBLE_R_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
117 + WM8741_DACRLSB_ATTENUATION, 0, 31, 1, dac_tlv_fine),
118 +SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
119 + WM8741_DACRMSB_ATTENUATION, 0, 31, 1, dac_tlv_coarse),
120 +SOC_ENUM("DAC Dither", w8741_enum[0]),
121 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
122 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
123 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
124 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
125 +};
126 +
127 +static const struct snd_kcontrol_new w8741_snd_controls_mono_left[] = {
128 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
129 + 0, 31, 0, dac_tlv_fine),
130 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
131 + 0, 31, 1, dac_tlv_coarse),
132 +SOC_ENUM("DAC Dither", w8741_enum[0]),
133 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
134 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
135 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
136 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
137 +};
138 +
139 +static const struct snd_kcontrol_new w8741_snd_controls_mono_right[] = {
140 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
141 + 0, 31, 0, dac_tlv_fine),
142 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACRMSB_ATTENUATION,
143 + 0, 31, 1, dac_tlv_coarse),
144 +SOC_ENUM("DAC Dither", w8741_enum[0]),
145 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
146 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
147 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
148 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
149 +};
150 +
151 +static int w8741_add_controls(struct snd_soc_codec *codec)
152 +{
153 + struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
154 +
155 + switch (wm8741->pdata.diff_mode) {
156 + case WM8741_DIFF_MODE_STEREO:
157 + case WM8741_DIFF_MODE_STEREO_REVERSED:
158 + snd_soc_add_codec_controls(codec,
159 + w8741_snd_controls_stereo,
160 + ARRAY_SIZE(w8741_snd_controls_stereo));
161 + break;
162 + case WM8741_DIFF_MODE_MONO_LEFT:
163 + snd_soc_add_codec_controls(codec,
164 + w8741_snd_controls_mono_left,
165 + ARRAY_SIZE(w8741_snd_controls_mono_left));
166 + break;
167 + case WM8741_DIFF_MODE_MONO_RIGHT:
168 + snd_soc_add_codec_controls(codec,
169 + w8741_snd_controls_mono_right,
170 + ARRAY_SIZE(w8741_snd_controls_mono_right));
171 + break;
172 + default:
173 + return -EINVAL;
174 + }
175 +
176 + return 0;
177 +}
178 +
179 +static int digidac1_soundcard_init(struct snd_soc_pcm_runtime *rtd)
180 +{
181 + struct snd_soc_codec *codec = rtd->codec;
182 + struct snd_soc_card *card = rtd->card;
183 + struct snd_soc_pcm_runtime *wm8741_rtd;
184 + struct snd_soc_codec *wm8741_codec;
185 + struct snd_card *sound_card = card->snd_card;
186 + struct snd_kcontrol *kctl;
187 + int ret;
188 +
189 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
190 + if (!wm8741_rtd) {
191 + dev_warn(card->dev, "digidac1_soundcard_init: couldn't get wm8741 rtd\n");
192 + return -EFAULT;
193 + }
194 + wm8741_codec = wm8741_rtd->codec;
195 + ret = w8741_add_controls(wm8741_codec);
196 + if (ret < 0)
197 + dev_warn(card->dev, "Failed to add new wm8741 controls: %d\n",
198 + ret);
199 +
200 + /* enable TX output */
201 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
202 +
203 + kctl = snd_soc_card_get_kcontrol(card,
204 + "Playback Volume");
205 + if (kctl) {
206 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
207 + snd_ctl_remove(sound_card, kctl);
208 + }
209 + kctl = snd_soc_card_get_kcontrol(card,
210 + "Fine Playback Volume");
211 + if (kctl) {
212 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
213 + snd_ctl_remove(sound_card, kctl);
214 + }
215 + return 0;
216 +}
217 +
218 +static int digidac1_soundcard_startup(struct snd_pcm_substream *substream)
219 +{
220 + /* turn on wm8804 digital output */
221 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
222 + struct snd_soc_codec *codec = rtd->codec;
223 + struct snd_soc_card *card = rtd->card;
224 + struct snd_soc_pcm_runtime *wm8741_rtd;
225 + struct snd_soc_codec *wm8741_codec;
226 +
227 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
228 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
229 + if (!wm8741_rtd) {
230 + dev_warn(card->dev, "digidac1_soundcard_startup: couldn't get WM8741 rtd\n");
231 + return -EFAULT;
232 + }
233 + wm8741_codec = wm8741_rtd->codec;
234 +
235 + /* latch wm8741 level */
236 + snd_soc_update_bits(wm8741_codec, WM8741_DACLLSB_ATTENUATION,
237 + WM8741_UPDATELL, WM8741_UPDATELL);
238 + snd_soc_update_bits(wm8741_codec, WM8741_DACLMSB_ATTENUATION,
239 + WM8741_UPDATELM, WM8741_UPDATELM);
240 + snd_soc_update_bits(wm8741_codec, WM8741_DACRLSB_ATTENUATION,
241 + WM8741_UPDATERL, WM8741_UPDATERL);
242 + snd_soc_update_bits(wm8741_codec, WM8741_DACRMSB_ATTENUATION,
243 + WM8741_UPDATERM, WM8741_UPDATERM);
244 +
245 + return 0;
246 +}
247 +
248 +static void digidac1_soundcard_shutdown(struct snd_pcm_substream *substream)
249 +{
250 + /* turn off wm8804 digital output */
251 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
252 + struct snd_soc_codec *codec = rtd->codec;
253 +
254 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
255 +}
256 +
257 +static int digidac1_soundcard_hw_params(struct snd_pcm_substream *substream,
258 + struct snd_pcm_hw_params *params)
259 +{
260 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
262 + struct snd_soc_codec *codec = rtd->codec;
263 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
264 + struct snd_soc_card *card = rtd->card;
265 + struct snd_soc_pcm_runtime *wm8741_rtd;
266 + struct snd_soc_codec *wm8741_codec;
267 +
268 + int sysclk = 27000000;
269 + long mclk_freq = 0;
270 + int mclk_div = 1;
271 + int sampling_freq = 1;
272 + int ret;
273 +
274 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
275 + if (!wm8741_rtd) {
276 + dev_warn(card->dev, "digidac1_soundcard_hw_params: couldn't get WM8741 rtd\n");
277 + return -EFAULT;
278 + }
279 + wm8741_codec = wm8741_rtd->codec;
280 + samplerate = params_rate(params);
281 +
282 + if (samplerate <= 96000) {
283 + mclk_freq = samplerate*256;
284 + mclk_div = WM8804_MCLKDIV_256FS;
285 + } else {
286 + mclk_freq = samplerate*128;
287 + mclk_div = WM8804_MCLKDIV_128FS;
288 + }
289 +
290 + switch (samplerate) {
291 + case 32000:
292 + sampling_freq = 0x03;
293 + break;
294 + case 44100:
295 + sampling_freq = 0x00;
296 + break;
297 + case 48000:
298 + sampling_freq = 0x02;
299 + break;
300 + case 88200:
301 + sampling_freq = 0x08;
302 + break;
303 + case 96000:
304 + sampling_freq = 0x0a;
305 + break;
306 + case 176400:
307 + sampling_freq = 0x0c;
308 + break;
309 + case 192000:
310 + sampling_freq = 0x0e;
311 + break;
312 + default:
313 + dev_err(codec->dev,
314 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
315 + samplerate);
316 + }
317 +
318 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
319 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
320 +
321 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
322 + sysclk, SND_SOC_CLOCK_OUT);
323 + if (ret < 0) {
324 + dev_err(codec->dev,
325 + "Failed to set WM8804 SYSCLK: %d\n", ret);
326 + return ret;
327 + }
328 + /* Enable wm8804 TX output */
329 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
330 +
331 + /* wm8804 Power on */
332 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
333 +
334 + /* wm8804 set sampling frequency status bits */
335 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
336 +
337 + /* Now update wm8741 registers for the correct oversampling */
338 + if (samplerate <= 48000)
339 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
340 + WM8741_OSR_MASK, 0x00);
341 + else if (samplerate <= 96000)
342 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
343 + WM8741_OSR_MASK, 0x20);
344 + else
345 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
346 + WM8741_OSR_MASK, 0x40);
347 +
348 + /* wm8741 bit size */
349 + switch (params_width(params)) {
350 + case 16:
351 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
352 + WM8741_IWL_MASK, 0x00);
353 + break;
354 + case 20:
355 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
356 + WM8741_IWL_MASK, 0x01);
357 + break;
358 + case 24:
359 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
360 + WM8741_IWL_MASK, 0x02);
361 + break;
362 + case 32:
363 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
364 + WM8741_IWL_MASK, 0x03);
365 + break;
366 + default:
367 + dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
368 + params_width(params));
369 + return -EINVAL;
370 + }
371 +
372 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
373 +}
374 +/* machine stream operations */
375 +static struct snd_soc_ops digidac1_soundcard_ops = {
376 + .hw_params = digidac1_soundcard_hw_params,
377 + .startup = digidac1_soundcard_startup,
378 + .shutdown = digidac1_soundcard_shutdown,
379 +};
380 +
381 +static struct snd_soc_dai_link digidac1_soundcard_dai[] = {
382 + {
383 + .name = "RRA DigiDAC1",
384 + .stream_name = "RRA DigiDAC1 HiFi",
385 + .cpu_dai_name = "bcm2708-i2s.0",
386 + .codec_dai_name = "wm8804-spdif",
387 + .platform_name = "bcm2708-i2s.0",
388 + .codec_name = "wm8804.1-003b",
389 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
390 + SND_SOC_DAIFMT_CBM_CFM,
391 + .ops = &digidac1_soundcard_ops,
392 + .init = digidac1_soundcard_init,
393 + },
394 + {
395 + .name = "RRA DigiDAC11",
396 + .stream_name = "RRA DigiDAC11 HiFi",
397 + .cpu_dai_name = "wm8804-spdif",
398 + .codec_dai_name = "wm8741",
399 + .codec_name = "wm8741.1-001a",
400 + .dai_fmt = SND_SOC_DAIFMT_I2S
401 + | SND_SOC_DAIFMT_NB_NF
402 + | SND_SOC_DAIFMT_CBS_CFS,
403 + },
404 +};
405 +
406 +/* audio machine driver */
407 +static struct snd_soc_card digidac1_soundcard = {
408 + .name = "digidac1-soundcard",
409 + .owner = THIS_MODULE,
410 + .dai_link = digidac1_soundcard_dai,
411 + .num_links = ARRAY_SIZE(digidac1_soundcard_dai),
412 +};
413 +
414 +static int digidac1_soundcard_probe(struct platform_device *pdev)
415 +{
416 + int ret = 0;
417 +
418 + digidac1_soundcard.dev = &pdev->dev;
419 +
420 + if (pdev->dev.of_node) {
421 + struct device_node *i2s_node;
422 + struct snd_soc_dai_link *dai = &digidac1_soundcard_dai[0];
423 +
424 + i2s_node = of_parse_phandle(pdev->dev.of_node,
425 + "i2s-controller", 0);
426 +
427 + if (i2s_node) {
428 + dai->cpu_dai_name = NULL;
429 + dai->cpu_of_node = i2s_node;
430 + dai->platform_name = NULL;
431 + dai->platform_of_node = i2s_node;
432 + }
433 + }
434 +
435 + ret = snd_soc_register_card(&digidac1_soundcard);
436 + if (ret && ret != -EPROBE_DEFER)
437 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
438 + ret);
439 +
440 + return ret;
441 +}
442 +
443 +static int digidac1_soundcard_remove(struct platform_device *pdev)
444 +{
445 + return snd_soc_unregister_card(&digidac1_soundcard);
446 +}
447 +
448 +static const struct of_device_id digidac1_soundcard_of_match[] = {
449 + { .compatible = "rra,digidac1-soundcard", },
450 + {},
451 +};
452 +MODULE_DEVICE_TABLE(of, digidac1_soundcard_of_match);
453 +
454 +static struct platform_driver digidac1_soundcard_driver = {
455 + .driver = {
456 + .name = "digidac1-audio",
457 + .owner = THIS_MODULE,
458 + .of_match_table = digidac1_soundcard_of_match,
459 + },
460 + .probe = digidac1_soundcard_probe,
461 + .remove = digidac1_soundcard_remove,
462 +};
463 +
464 +module_platform_driver(digidac1_soundcard_driver);
465 +
466 +MODULE_AUTHOR("José M. Tasende <vintage@redrocksaudio.es>");
467 +MODULE_DESCRIPTION("ASoC Driver for RRA DigiDAC1");
468 +MODULE_LICENSE("GPL v2");