1 From 2e0deecb1e303b2808c9c84486d20039741b773b 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 +
7 ASoC: digidac1-soundcard: use modern dai_link style
9 Signed-off-by: Hui Wang <hui.wang@canonical.com>
11 sound/soc/bcm/digidac1-soundcard.c | 421 +++++++++++++++++++++++++++++
12 1 file changed, 421 insertions(+)
13 create mode 100644 sound/soc/bcm/digidac1-soundcard.c
16 +++ b/sound/soc/bcm/digidac1-soundcard.c
19 + * ASoC Driver for RRA DigiDAC1
21 + * Author: José M. Tasende <vintage@redrocksaudio.es>
22 + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
23 + * and the Wolfson card driver by Nikesh Oswal, <Nikesh.Oswal@wolfsonmicro.com>
24 + * This program is free software; you can redistribute it and/or
25 + * modify it under the terms of the GNU General Public License
26 + * version 2 as published by the Free Software Foundation.
28 + * This program is distributed in the hope that it will be useful, but
29 + * WITHOUT ANY WARRANTY; without even the implied warranty of
30 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 + * General Public License for more details.
34 +#include <linux/module.h>
35 +#include <linux/platform_device.h>
36 +#include <linux/i2c.h>
37 +#include <sound/core.h>
38 +#include <sound/pcm.h>
39 +#include <sound/pcm_params.h>
40 +#include <sound/soc.h>
41 +#include <sound/jack.h>
42 +#include <sound/soc-dapm.h>
43 +#include <sound/tlv.h>
44 +#include <linux/regulator/consumer.h>
46 +#include "../codecs/wm8804.h"
47 +#include "../codecs/wm8741.h"
49 +#define WM8741_NUM_SUPPLIES 2
51 +/* codec private data */
53 + struct wm8741_platform_data pdata;
54 + struct regmap *regmap;
55 + struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES];
56 + unsigned int sysclk;
57 + const struct snd_pcm_hw_constraint_list *sysclk_constraints;
60 +static int samplerate = 44100;
62 +/* New Alsa Controls not exposed by original wm8741 codec driver */
63 +/* in actual driver the att. adjustment is wrong because */
64 +/* this DAC has a coarse attenuation register with 4dB steps */
65 +/* and a fine level register with 0.125dB steps */
66 +/* each register has 32 steps so combining both we have 1024 steps */
68 +/* The original level controls from driver are removed at startup */
69 +/* and replaced by the corrected ones. */
70 +/* The same wm8741 driver can be used for wm8741 and wm8742 devices */
72 +static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, 0, 13, 0);
73 +static const DECLARE_TLV_DB_SCALE(dac_tlv_coarse, -12700, 400, 1);
74 +static const char *w8741_dither[4] = {"Off", "RPDF", "TPDF", "HPDF"};
75 +static const char *w8741_filter[5] = {
76 + "Type 1", "Type 2", "Type 3", "Type 4", "Type 5"};
77 +static const char *w8741_switch[2] = {"Off", "On"};
78 +static const struct soc_enum w8741_enum[] = {
79 +SOC_ENUM_SINGLE(WM8741_MODE_CONTROL_2, 0, 4, w8741_dither),/* dithering type */
80 +SOC_ENUM_SINGLE(WM8741_FILTER_CONTROL, 0, 5, w8741_filter),/* filter type */
81 +SOC_ENUM_SINGLE(WM8741_FORMAT_CONTROL, 6, 2, w8741_switch),/* phase invert */
82 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 0, 2, w8741_switch),/* volume ramp */
83 +SOC_ENUM_SINGLE(WM8741_VOLUME_CONTROL, 3, 2, w8741_switch),/* soft mute */
86 +static const struct snd_kcontrol_new w8741_snd_controls_stereo[] = {
87 +SOC_DOUBLE_R_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
88 + WM8741_DACRLSB_ATTENUATION, 0, 31, 1, dac_tlv_fine),
89 +SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
90 + WM8741_DACRMSB_ATTENUATION, 0, 31, 1, dac_tlv_coarse),
91 +SOC_ENUM("DAC Dither", w8741_enum[0]),
92 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
93 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
94 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
95 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
98 +static const struct snd_kcontrol_new w8741_snd_controls_mono_left[] = {
99 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACLLSB_ATTENUATION,
100 + 0, 31, 0, dac_tlv_fine),
101 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACLMSB_ATTENUATION,
102 + 0, 31, 1, dac_tlv_coarse),
103 +SOC_ENUM("DAC Dither", w8741_enum[0]),
104 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
105 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
106 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
107 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
110 +static const struct snd_kcontrol_new w8741_snd_controls_mono_right[] = {
111 +SOC_SINGLE_TLV("DAC Fine Playback Volume", WM8741_DACRLSB_ATTENUATION,
112 + 0, 31, 0, dac_tlv_fine),
113 +SOC_SINGLE_TLV("Digital Playback Volume", WM8741_DACRMSB_ATTENUATION,
114 + 0, 31, 1, dac_tlv_coarse),
115 +SOC_ENUM("DAC Dither", w8741_enum[0]),
116 +SOC_ENUM("DAC Digital Filter", w8741_enum[1]),
117 +SOC_ENUM("DAC Phase Invert", w8741_enum[2]),
118 +SOC_ENUM("DAC Volume Ramp", w8741_enum[3]),
119 +SOC_ENUM("DAC Soft Mute", w8741_enum[4]),
122 +static int w8741_add_controls(struct snd_soc_component *component)
124 + struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
126 + switch (wm8741->pdata.diff_mode) {
127 + case WM8741_DIFF_MODE_STEREO:
128 + case WM8741_DIFF_MODE_STEREO_REVERSED:
129 + snd_soc_add_component_controls(component,
130 + w8741_snd_controls_stereo,
131 + ARRAY_SIZE(w8741_snd_controls_stereo));
133 + case WM8741_DIFF_MODE_MONO_LEFT:
134 + snd_soc_add_component_controls(component,
135 + w8741_snd_controls_mono_left,
136 + ARRAY_SIZE(w8741_snd_controls_mono_left));
138 + case WM8741_DIFF_MODE_MONO_RIGHT:
139 + snd_soc_add_component_controls(component,
140 + w8741_snd_controls_mono_right,
141 + ARRAY_SIZE(w8741_snd_controls_mono_right));
150 +static int digidac1_soundcard_init(struct snd_soc_pcm_runtime *rtd)
152 + struct snd_soc_component *component = rtd->codec_dai->component;
153 + struct snd_soc_card *card = rtd->card;
154 + struct snd_soc_pcm_runtime *wm8741_rtd;
155 + struct snd_soc_component *wm8741_component;
156 + struct snd_card *sound_card = card->snd_card;
157 + struct snd_kcontrol *kctl;
160 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
162 + dev_warn(card->dev, "digidac1_soundcard_init: couldn't get wm8741 rtd\n");
165 + wm8741_component = wm8741_rtd->codec_dai->component;
166 + ret = w8741_add_controls(wm8741_component);
168 + dev_warn(card->dev, "Failed to add new wm8741 controls: %d\n",
171 + /* enable TX output */
172 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
174 + kctl = snd_soc_card_get_kcontrol(card,
175 + "Playback Volume");
177 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
178 + snd_ctl_remove(sound_card, kctl);
180 + kctl = snd_soc_card_get_kcontrol(card,
181 + "Fine Playback Volume");
183 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
184 + snd_ctl_remove(sound_card, kctl);
189 +static int digidac1_soundcard_startup(struct snd_pcm_substream *substream)
191 + /* turn on wm8804 digital output */
192 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
193 + struct snd_soc_component *component = rtd->codec_dai->component;
194 + struct snd_soc_card *card = rtd->card;
195 + struct snd_soc_pcm_runtime *wm8741_rtd;
196 + struct snd_soc_component *wm8741_component;
198 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x3c, 0x00);
199 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
201 + dev_warn(card->dev, "digidac1_soundcard_startup: couldn't get WM8741 rtd\n");
204 + wm8741_component = wm8741_rtd->codec_dai->component;
206 + /* latch wm8741 level */
207 + snd_soc_component_update_bits(wm8741_component, WM8741_DACLLSB_ATTENUATION,
208 + WM8741_UPDATELL, WM8741_UPDATELL);
209 + snd_soc_component_update_bits(wm8741_component, WM8741_DACLMSB_ATTENUATION,
210 + WM8741_UPDATELM, WM8741_UPDATELM);
211 + snd_soc_component_update_bits(wm8741_component, WM8741_DACRLSB_ATTENUATION,
212 + WM8741_UPDATERL, WM8741_UPDATERL);
213 + snd_soc_component_update_bits(wm8741_component, WM8741_DACRMSB_ATTENUATION,
214 + WM8741_UPDATERM, WM8741_UPDATERM);
219 +static void digidac1_soundcard_shutdown(struct snd_pcm_substream *substream)
221 + /* turn off wm8804 digital output */
222 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
223 + struct snd_soc_component *component = rtd->codec_dai->component;
225 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x3c, 0x3c);
228 +static int digidac1_soundcard_hw_params(struct snd_pcm_substream *substream,
229 + struct snd_pcm_hw_params *params)
231 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
232 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
233 + struct snd_soc_component *component = rtd->codec_dai->component;
234 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
235 + struct snd_soc_card *card = rtd->card;
236 + struct snd_soc_pcm_runtime *wm8741_rtd;
237 + struct snd_soc_component *wm8741_component;
239 + int sysclk = 27000000;
240 + long mclk_freq = 0;
242 + int sampling_freq = 1;
245 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
247 + dev_warn(card->dev, "digidac1_soundcard_hw_params: couldn't get WM8741 rtd\n");
250 + wm8741_component = wm8741_rtd->codec_dai->component;
251 + samplerate = params_rate(params);
253 + if (samplerate <= 96000) {
254 + mclk_freq = samplerate*256;
255 + mclk_div = WM8804_MCLKDIV_256FS;
257 + mclk_freq = samplerate*128;
258 + mclk_div = WM8804_MCLKDIV_128FS;
261 + switch (samplerate) {
263 + sampling_freq = 0x03;
266 + sampling_freq = 0x00;
269 + sampling_freq = 0x02;
272 + sampling_freq = 0x08;
275 + sampling_freq = 0x0a;
278 + sampling_freq = 0x0c;
281 + sampling_freq = 0x0e;
285 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
289 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
290 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
292 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
293 + sysclk, SND_SOC_CLOCK_OUT);
296 + "Failed to set WM8804 SYSCLK: %d\n", ret);
299 + /* Enable wm8804 TX output */
300 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
302 + /* wm8804 Power on */
303 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x9, 0);
305 + /* wm8804 set sampling frequency status bits */
306 + snd_soc_component_update_bits(component, WM8804_SPDTX4, 0x0f, sampling_freq);
308 + /* Now update wm8741 registers for the correct oversampling */
309 + if (samplerate <= 48000)
310 + snd_soc_component_update_bits(wm8741_component, WM8741_MODE_CONTROL_1,
311 + WM8741_OSR_MASK, 0x00);
312 + else if (samplerate <= 96000)
313 + snd_soc_component_update_bits(wm8741_component, WM8741_MODE_CONTROL_1,
314 + WM8741_OSR_MASK, 0x20);
316 + snd_soc_component_update_bits(wm8741_component, WM8741_MODE_CONTROL_1,
317 + WM8741_OSR_MASK, 0x40);
319 + /* wm8741 bit size */
320 + switch (params_width(params)) {
322 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
323 + WM8741_IWL_MASK, 0x00);
326 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
327 + WM8741_IWL_MASK, 0x01);
330 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
331 + WM8741_IWL_MASK, 0x02);
334 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
335 + WM8741_IWL_MASK, 0x03);
338 + dev_dbg(card->dev, "wm8741_hw_params: Unsupported bit size param = %d",
339 + params_width(params));
343 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
345 +/* machine stream operations */
346 +static struct snd_soc_ops digidac1_soundcard_ops = {
347 + .hw_params = digidac1_soundcard_hw_params,
348 + .startup = digidac1_soundcard_startup,
349 + .shutdown = digidac1_soundcard_shutdown,
352 +SND_SOC_DAILINK_DEFS(digidac1,
353 + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
354 + DAILINK_COMP_ARRAY(COMP_CODEC("wm8804.1-003b", "wm8804-spdif")),
355 + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
357 +SND_SOC_DAILINK_DEFS(digidac11,
358 + DAILINK_COMP_ARRAY(COMP_CPU("wm8804-spdif")),
359 + DAILINK_COMP_ARRAY(COMP_CODEC("wm8741.1-001a", "wm8741")),
360 + DAILINK_COMP_ARRAY(COMP_EMPTY()));
362 +static struct snd_soc_dai_link digidac1_soundcard_dai[] = {
364 + .name = "RRA DigiDAC1",
365 + .stream_name = "RRA DigiDAC1 HiFi",
366 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
367 + SND_SOC_DAIFMT_CBM_CFM,
368 + .ops = &digidac1_soundcard_ops,
369 + .init = digidac1_soundcard_init,
370 + SND_SOC_DAILINK_REG(digidac1),
373 + .name = "RRA DigiDAC11",
374 + .stream_name = "RRA DigiDAC11 HiFi",
375 + .dai_fmt = SND_SOC_DAIFMT_I2S
376 + | SND_SOC_DAIFMT_NB_NF
377 + | SND_SOC_DAIFMT_CBS_CFS,
378 + SND_SOC_DAILINK_REG(digidac11),
382 +/* audio machine driver */
383 +static struct snd_soc_card digidac1_soundcard = {
384 + .name = "digidac1-soundcard",
385 + .owner = THIS_MODULE,
386 + .dai_link = digidac1_soundcard_dai,
387 + .num_links = ARRAY_SIZE(digidac1_soundcard_dai),
390 +static int digidac1_soundcard_probe(struct platform_device *pdev)
394 + digidac1_soundcard.dev = &pdev->dev;
396 + if (pdev->dev.of_node) {
397 + struct device_node *i2s_node;
398 + struct snd_soc_dai_link *dai = &digidac1_soundcard_dai[0];
400 + i2s_node = of_parse_phandle(pdev->dev.of_node,
401 + "i2s-controller", 0);
404 + dai->cpus->dai_name = NULL;
405 + dai->cpus->of_node = i2s_node;
406 + dai->platforms->name = NULL;
407 + dai->platforms->of_node = i2s_node;
411 + ret = devm_snd_soc_register_card(&pdev->dev, &digidac1_soundcard);
412 + if (ret && ret != -EPROBE_DEFER)
413 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
419 +static const struct of_device_id digidac1_soundcard_of_match[] = {
420 + { .compatible = "rra,digidac1-soundcard", },
423 +MODULE_DEVICE_TABLE(of, digidac1_soundcard_of_match);
425 +static struct platform_driver digidac1_soundcard_driver = {
427 + .name = "digidac1-audio",
428 + .owner = THIS_MODULE,
429 + .of_match_table = digidac1_soundcard_of_match,
431 + .probe = digidac1_soundcard_probe,
434 +module_platform_driver(digidac1_soundcard_driver);
436 +MODULE_AUTHOR("José M. Tasende <vintage@redrocksaudio.es>");
437 +MODULE_DESCRIPTION("ASoC Driver for RRA DigiDAC1");
438 +MODULE_LICENSE("GPL v2");