1 From e5d85d533e7ea8fd5c68d4601ff2b8034bc4aaa9 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] New driver for RRA DigiDAC1 soundcard using WM8741 + WM8804
7 sound/soc/bcm/Kconfig | 8 +
8 sound/soc/bcm/Makefile | 2 +
9 sound/soc/bcm/digidac1-soundcard.c | 422 +++++++++++++++++++++++++++++++++++++
10 3 files changed, 432 insertions(+)
11 create mode 100644 sound/soc/bcm/digidac1-soundcard.c
13 --- a/sound/soc/bcm/Kconfig
14 +++ b/sound/soc/bcm/Kconfig
15 @@ -108,3 +108,11 @@ config SND_AUDIOINJECTOR_PI_SOUNDCARD
18 Say Y or M if you want to add support for audioinjector.net Pi Hat
20 +config SND_DIGIDAC1_SOUNDCARD
21 + tristate "Support for Red Rocks Audio DigiDAC1"
22 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
23 + select SND_SOC_WM8804
24 + select SND_SOC_WM8741
26 + Say Y or M if you want to add support for Red Rocks Audio DigiDAC1 board.
27 --- a/sound/soc/bcm/Makefile
28 +++ b/sound/soc/bcm/Makefile
29 @@ -22,6 +22,7 @@ snd-soc-iqaudio-dac-objs := iqaudio-dac.
30 snd-soc-iqaudio-digi-objs := iqaudio_digi.o
31 snd-soc-raspidac3-objs := raspidac3.o
32 snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
33 +snd-soc-digidac1-soundcard-objs := digidac1-soundcard.o
35 obj-$(CONFIG_SND_BCM2708_SOC_ADAU1977_ADC) += snd-soc-adau1977-adc.o
36 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP) += snd-soc-hifiberry-amp.o
37 @@ -36,4 +37,5 @@ obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC
38 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DIGI) += snd-soc-iqaudio-digi.o
39 obj-$(CONFIG_SND_BCM2708_SOC_RASPIDAC3) += snd-soc-raspidac3.o
40 obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
41 +obj-$(CONFIG_SND_DIGIDAC1_SOUNDCARD) += snd-soc-digidac1-soundcard.o
44 +++ b/sound/soc/bcm/digidac1-soundcard.c
47 + * ASoC Driver for RRA DigiDAC1
49 + * Author: José M. Tasende <vintage@redrocksaudio.es>
50 + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
51 + * and the Wolfson card driver by Nikesh Oswal, <Nikesh.Oswal@wolfsonmicro.com>
52 + * This program is free software; you can redistribute it and/or
53 + * modify it under the terms of the GNU General Public License
54 + * version 2 as published by the Free Software Foundation.
56 + * This program is distributed in the hope that it will be useful, but
57 + * WITHOUT ANY WARRANTY; without even the implied warranty of
58 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
59 + * General Public License for more details.
62 +#include <linux/module.h>
63 +#include <linux/platform_device.h>
64 +#include <linux/i2c.h>
65 +#include <sound/core.h>
66 +#include <sound/pcm.h>
67 +#include <sound/pcm_params.h>
68 +#include <sound/soc.h>
69 +#include <sound/jack.h>
70 +#include <sound/soc-dapm.h>
71 +#include <sound/tlv.h>
72 +#include <linux/regulator/consumer.h>
74 +#include "../codecs/wm8804.h"
75 +#include "../codecs/wm8741.h"
77 +#define WM8741_NUM_SUPPLIES 2
79 +/* codec private data */
81 + struct wm8741_platform_data pdata;
82 + struct regmap *regmap;
83 + struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
84 + unsigned int sysclk;
85 + const struct snd_pcm_hw_constraint_list *sysclk_constraints;
88 +static int samplerate = 44100;
90 +/* New Alsa Controls not exposed by original wm8741 codec driver */
91 +/* in actual driver the att. adjustment is wrong because */
92 +/* this DAC has a coarse attenuation register with 4dB steps */
93 +/* and a fine level register with 0.125dB steps */
94 +/* each register has 32 steps so combining both we have 1024 steps */
96 +/* The original level controls from driver are removed at startup */
97 +/* and replaced by the corrected ones. */
98 +/* The same wm8741 driver can be used for wm8741 and wm8742 devices */
100 +static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, 0, 13, 0);
101 +static const DECLARE_TLV_DB_SCALE(dac_tlv_coarse, -12700, 400, 1);
102 +static const char *w8741_dither[4] = {"Off", "RPDF", "TPDF", "HPDF"};
103 +static const char *w8741_filter[5] = {
104 + "Type 1", "Type 2", "Type 3", "Type 4", "Type 5"};
105 +static const char *w8741_switch[2] = {"Off", "On"};
106 +static const struct soc_enum w8741_enum[] = {
107 +SOC_ENUM_SINGLE(WM8741_MODE_CONTROL_2, 0, 4, w8741_dither),/* dithering type */
108 +SOC_ENUM_SINGLE(WM8741_FILTER_CONTROL, 0, 5, w8741_filter),/* filter type */
109 +SOC_ENUM_SINGLE(WM8741_FORMAT_CONTROL, 6, 2, w8741_switch),/* phase invert */
110 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 0, 2, w8741_switch),/* volume ramp */
111 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 3, 2, w8741_switch),/* soft mute */
114 +static const struct snd_kcontrol_new w8741_snd_controls_stereo[] = {
115 +SOC_DOUBLE_R_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
116 + WM8741_DACRLSB_ATTENUATION, 0, 31, 1, dac_tlv_fine),
117 +SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
118 + WM8741_DACRMSB_ATTENUATION, 0, 31, 1, dac_tlv_coarse),
119 +SOC_ENUM("DAC Dither", w8741_enum[0]),
120 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
121 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
122 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
123 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
126 +static const struct snd_kcontrol_new w8741_snd_controls_mono_left[] = {
127 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
128 + 0, 31, 0, dac_tlv_fine),
129 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
130 + 0, 31, 1, dac_tlv_coarse),
131 +SOC_ENUM("DAC Dither", w8741_enum[0]),
132 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
133 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
134 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
135 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
138 +static const struct snd_kcontrol_new w8741_snd_controls_mono_right[] = {
139 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
140 + 0, 31, 0, dac_tlv_fine),
141 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACRMSB_ATTENUATION,
142 + 0, 31, 1, dac_tlv_coarse),
143 +SOC_ENUM("DAC Dither", w8741_enum[0]),
144 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
145 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
146 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
147 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
150 +static int w8741_add_controls(struct snd_soc_codec *codec)
152 + struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
154 + switch (wm8741->pdata.diff_mode) {
155 + case WM8741_DIFF_MODE_STEREO:
156 + case WM8741_DIFF_MODE_STEREO_REVERSED:
157 + snd_soc_add_codec_controls(codec,
158 + w8741_snd_controls_stereo,
159 + ARRAY_SIZE(w8741_snd_controls_stereo));
161 + case WM8741_DIFF_MODE_MONO_LEFT:
162 + snd_soc_add_codec_controls(codec,
163 + w8741_snd_controls_mono_left,
164 + ARRAY_SIZE(w8741_snd_controls_mono_left));
166 + case WM8741_DIFF_MODE_MONO_RIGHT:
167 + snd_soc_add_codec_controls(codec,
168 + w8741_snd_controls_mono_right,
169 + ARRAY_SIZE(w8741_snd_controls_mono_right));
178 +static int digidac1_soundcard_init(struct snd_soc_pcm_runtime *rtd)
180 + struct snd_soc_codec *codec = rtd->codec;
181 + struct snd_soc_card *card = rtd->card;
182 + struct snd_soc_pcm_runtime *wm8741_rtd;
183 + struct snd_soc_codec *wm8741_codec;
184 + struct snd_card *sound_card = card->snd_card;
185 + struct snd_kcontrol *kctl;
188 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
190 + dev_warn(card->dev, "digidac1_soundcard_init: couldn't get wm8741 rtd\n");
193 + wm8741_codec = wm8741_rtd->codec;
194 + ret = w8741_add_controls(wm8741_codec);
196 + dev_warn(card->dev, "Failed to add new wm8741 controls: %d\n",
199 + /* enable TX output */
200 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
202 + kctl = snd_soc_card_get_kcontrol(card,
203 + "Playback Volume");
205 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
206 + snd_ctl_remove(sound_card, kctl);
208 + kctl = snd_soc_card_get_kcontrol(card,
209 + "Fine Playback Volume");
211 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
212 + snd_ctl_remove(sound_card, kctl);
217 +static int digidac1_soundcard_startup(struct snd_pcm_substream *substream)
219 + /* turn on wm8804 digital output */
220 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
221 + struct snd_soc_codec *codec = rtd->codec;
222 + struct snd_soc_card *card = rtd->card;
223 + struct snd_soc_pcm_runtime *wm8741_rtd;
224 + struct snd_soc_codec *wm8741_codec;
226 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
227 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
229 + dev_warn(card->dev, "digidac1_soundcard_startup: couldn't get WM8741 rtd\n");
232 + wm8741_codec = wm8741_rtd->codec;
234 + /* latch wm8741 level */
235 + snd_soc_update_bits(wm8741_codec, WM8741_DACLLSB_ATTENUATION,
236 + WM8741_UPDATELL, WM8741_UPDATELL);
237 + snd_soc_update_bits(wm8741_codec, WM8741_DACLMSB_ATTENUATION,
238 + WM8741_UPDATELM, WM8741_UPDATELM);
239 + snd_soc_update_bits(wm8741_codec, WM8741_DACRLSB_ATTENUATION,
240 + WM8741_UPDATERL, WM8741_UPDATERL);
241 + snd_soc_update_bits(wm8741_codec, WM8741_DACRMSB_ATTENUATION,
242 + WM8741_UPDATERM, WM8741_UPDATERM);
247 +static void digidac1_soundcard_shutdown(struct snd_pcm_substream *substream)
249 + /* turn off wm8804 digital output */
250 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
251 + struct snd_soc_codec *codec = rtd->codec;
253 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
256 +static int digidac1_soundcard_hw_params(struct snd_pcm_substream *substream,
257 + struct snd_pcm_hw_params *params)
259 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
260 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
261 + struct snd_soc_codec *codec = rtd->codec;
262 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
263 + struct snd_soc_card *card = rtd->card;
264 + struct snd_soc_pcm_runtime *wm8741_rtd;
265 + struct snd_soc_codec *wm8741_codec;
267 + int sysclk = 27000000;
268 + long mclk_freq = 0;
270 + int sampling_freq = 1;
273 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
275 + dev_warn(card->dev, "digidac1_soundcard_hw_params: couldn't get WM8741 rtd\n");
278 + wm8741_codec = wm8741_rtd->codec;
279 + samplerate = params_rate(params);
281 + if (samplerate <= 96000) {
282 + mclk_freq = samplerate*256;
283 + mclk_div = WM8804_MCLKDIV_256FS;
285 + mclk_freq = samplerate*128;
286 + mclk_div = WM8804_MCLKDIV_128FS;
289 + switch (samplerate) {
291 + sampling_freq = 0x03;
294 + sampling_freq = 0x00;
297 + sampling_freq = 0x02;
300 + sampling_freq = 0x08;
303 + sampling_freq = 0x0a;
306 + sampling_freq = 0x0c;
309 + sampling_freq = 0x0e;
312 + dev_err(codec->dev,
313 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
317 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
318 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
320 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
321 + sysclk, SND_SOC_CLOCK_OUT);
323 + dev_err(codec->dev,
324 + "Failed to set WM8804 SYSCLK: %d\n", ret);
327 + /* Enable wm8804 TX output */
328 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
330 + /* wm8804 Power on */
331 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
333 + /* wm8804 set sampling frequency status bits */
334 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
336 + /* Now update wm8741 registers for the correct oversampling */
337 + if (samplerate <= 48000)
338 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
339 + WM8741_OSR_MASK, 0x00);
340 + else if (samplerate <= 96000)
341 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
342 + WM8741_OSR_MASK, 0x20);
344 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
345 + WM8741_OSR_MASK, 0x40);
347 + /* wm8741 bit size */
348 + switch (params_width(params)) {
350 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
351 + WM8741_IWL_MASK, 0x00);
354 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
355 + WM8741_IWL_MASK, 0x01);
358 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
359 + WM8741_IWL_MASK, 0x02);
362 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
363 + WM8741_IWL_MASK, 0x03);
366 + dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
367 + params_width(params));
371 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
373 +/* machine stream operations */
374 +static struct snd_soc_ops digidac1_soundcard_ops = {
375 + .hw_params = digidac1_soundcard_hw_params,
376 + .startup = digidac1_soundcard_startup,
377 + .shutdown = digidac1_soundcard_shutdown,
380 +static struct snd_soc_dai_link digidac1_soundcard_dai[] = {
382 + .name = "RRA DigiDAC1",
383 + .stream_name = "RRA DigiDAC1 HiFi",
384 + .cpu_dai_name = "bcm2708-i2s.0",
385 + .codec_dai_name = "wm8804-spdif",
386 + .platform_name = "bcm2708-i2s.0",
387 + .codec_name = "wm8804.1-003b",
388 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
389 + SND_SOC_DAIFMT_CBM_CFM,
390 + .ops = &digidac1_soundcard_ops,
391 + .init = digidac1_soundcard_init,
394 + .name = "RRA DigiDAC11",
395 + .stream_name = "RRA DigiDAC11 HiFi",
396 + .cpu_dai_name = "wm8804-spdif",
397 + .codec_dai_name = "wm8741",
398 + .codec_name = "wm8741.1-001a",
399 + .dai_fmt = SND_SOC_DAIFMT_I2S
400 + | SND_SOC_DAIFMT_NB_NF
401 + | SND_SOC_DAIFMT_CBS_CFS,
405 +/* audio machine driver */
406 +static struct snd_soc_card digidac1_soundcard = {
407 + .name = "digidac1-soundcard",
408 + .owner = THIS_MODULE,
409 + .dai_link = digidac1_soundcard_dai,
410 + .num_links = ARRAY_SIZE(digidac1_soundcard_dai),
413 +static int digidac1_soundcard_probe(struct platform_device *pdev)
417 + digidac1_soundcard.dev = &pdev->dev;
419 + if (pdev->dev.of_node) {
420 + struct device_node *i2s_node;
421 + struct snd_soc_dai_link *dai = &digidac1_soundcard_dai[0];
423 + i2s_node = of_parse_phandle(pdev->dev.of_node,
424 + "i2s-controller", 0);
427 + dai->cpu_dai_name = NULL;
428 + dai->cpu_of_node = i2s_node;
429 + dai->platform_name = NULL;
430 + dai->platform_of_node = i2s_node;
434 + ret = snd_soc_register_card(&digidac1_soundcard);
436 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
442 +static int digidac1_soundcard_remove(struct platform_device *pdev)
444 + return snd_soc_unregister_card(&digidac1_soundcard);
447 +static const struct of_device_id digidac1_soundcard_of_match[] = {
448 + { .compatible = "rra,digidac1-soundcard", },
451 +MODULE_DEVICE_TABLE(of, digidac1_soundcard_of_match);
453 +static struct platform_driver digidac1_soundcard_driver = {
455 + .name = "digidac1-audio",
456 + .owner = THIS_MODULE,
457 + .of_match_table = digidac1_soundcard_of_match,
459 + .probe = digidac1_soundcard_probe,
460 + .remove = digidac1_soundcard_remove,
463 +module_platform_driver(digidac1_soundcard_driver);
465 +MODULE_AUTHOR("José M. Tasende <vintage@redrocksaudio.es>");
466 +MODULE_DESCRIPTION("ASoC Driver for RRA DigiDAC1");
467 +MODULE_LICENSE("GPL v2");