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 +
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
14 --- a/sound/soc/bcm/Kconfig
15 +++ b/sound/soc/bcm/Kconfig
16 @@ -109,3 +109,11 @@ config SND_AUDIOINJECTOR_PI_SOUNDCARD
19 Say Y or M if you want to add support for audioinjector.net Pi Hat
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
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
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
45 +++ b/sound/soc/bcm/digidac1-soundcard.c
48 + * ASoC Driver for RRA DigiDAC1
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.
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.
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>
75 +#include "../codecs/wm8804.h"
76 +#include "../codecs/wm8741.h"
78 +#define WM8741_NUM_SUPPLIES 2
80 +/* codec private data */
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;
89 +static int samplerate = 44100;
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 */
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 */
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 */
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]),
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]),
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]),
151 +static int w8741_add_controls(struct snd_soc_codec *codec)
153 + struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec);
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));
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));
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));
179 +static int digidac1_soundcard_init(struct snd_soc_pcm_runtime *rtd)
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;
189 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
191 + dev_warn(card->dev, "digidac1_soundcard_init: couldn't get wm8741 rtd\n");
194 + wm8741_codec = wm8741_rtd->codec;
195 + ret = w8741_add_controls(wm8741_codec);
197 + dev_warn(card->dev, "Failed to add new wm8741 controls: %d\n",
200 + /* enable TX output */
201 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
203 + kctl = snd_soc_card_get_kcontrol(card,
204 + "Playback Volume");
206 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
207 + snd_ctl_remove(sound_card, kctl);
209 + kctl = snd_soc_card_get_kcontrol(card,
210 + "Fine Playback Volume");
212 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
213 + snd_ctl_remove(sound_card, kctl);
218 +static int digidac1_soundcard_startup(struct snd_pcm_substream *substream)
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;
227 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
228 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
230 + dev_warn(card->dev, "digidac1_soundcard_startup: couldn't get WM8741 rtd\n");
233 + wm8741_codec = wm8741_rtd->codec;
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);
248 +static void digidac1_soundcard_shutdown(struct snd_pcm_substream *substream)
250 + /* turn off wm8804 digital output */
251 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
252 + struct snd_soc_codec *codec = rtd->codec;
254 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
257 +static int digidac1_soundcard_hw_params(struct snd_pcm_substream *substream,
258 + struct snd_pcm_hw_params *params)
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;
268 + int sysclk = 27000000;
269 + long mclk_freq = 0;
271 + int sampling_freq = 1;
274 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
276 + dev_warn(card->dev, "digidac1_soundcard_hw_params: couldn't get WM8741 rtd\n");
279 + wm8741_codec = wm8741_rtd->codec;
280 + samplerate = params_rate(params);
282 + if (samplerate <= 96000) {
283 + mclk_freq = samplerate*256;
284 + mclk_div = WM8804_MCLKDIV_256FS;
286 + mclk_freq = samplerate*128;
287 + mclk_div = WM8804_MCLKDIV_128FS;
290 + switch (samplerate) {
292 + sampling_freq = 0x03;
295 + sampling_freq = 0x00;
298 + sampling_freq = 0x02;
301 + sampling_freq = 0x08;
304 + sampling_freq = 0x0a;
307 + sampling_freq = 0x0c;
310 + sampling_freq = 0x0e;
313 + dev_err(codec->dev,
314 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
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);
321 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
322 + sysclk, SND_SOC_CLOCK_OUT);
324 + dev_err(codec->dev,
325 + "Failed to set WM8804 SYSCLK: %d\n", ret);
328 + /* Enable wm8804 TX output */
329 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
331 + /* wm8804 Power on */
332 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
334 + /* wm8804 set sampling frequency status bits */
335 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
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);
345 + snd_soc_update_bits(wm8741_codec, WM8741_MODE_CONTROL_1,
346 + WM8741_OSR_MASK, 0x40);
348 + /* wm8741 bit size */
349 + switch (params_width(params)) {
351 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
352 + WM8741_IWL_MASK, 0x00);
355 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
356 + WM8741_IWL_MASK, 0x01);
359 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
360 + WM8741_IWL_MASK, 0x02);
363 + snd_soc_update_bits(wm8741_codec, WM8741_FORMAT_CONTROL,
364 + WM8741_IWL_MASK, 0x03);
367 + dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d",
368 + params_width(params));
372 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
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,
381 +static struct snd_soc_dai_link digidac1_soundcard_dai[] = {
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,
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,
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),
414 +static int digidac1_soundcard_probe(struct platform_device *pdev)
418 + digidac1_soundcard.dev = &pdev->dev;
420 + if (pdev->dev.of_node) {
421 + struct device_node *i2s_node;
422 + struct snd_soc_dai_link *dai = &digidac1_soundcard_dai[0];
424 + i2s_node = of_parse_phandle(pdev->dev.of_node,
425 + "i2s-controller", 0);
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;
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",
443 +static int digidac1_soundcard_remove(struct platform_device *pdev)
445 + return snd_soc_unregister_card(&digidac1_soundcard);
448 +static const struct of_device_id digidac1_soundcard_of_match[] = {
449 + { .compatible = "rra,digidac1-soundcard", },
452 +MODULE_DEVICE_TABLE(of, digidac1_soundcard_of_match);
454 +static struct platform_driver digidac1_soundcard_driver = {
456 + .name = "digidac1-audio",
457 + .owner = THIS_MODULE,
458 + .of_match_table = digidac1_soundcard_of_match,
460 + .probe = digidac1_soundcard_probe,
461 + .remove = digidac1_soundcard_remove,
464 +module_platform_driver(digidac1_soundcard_driver);
466 +MODULE_AUTHOR("José M. Tasende <vintage@redrocksaudio.es>");
467 +MODULE_DESCRIPTION("ASoC Driver for RRA DigiDAC1");
468 +MODULE_LICENSE("GPL v2");