69d4221d79f712329ebe5eca527276f8e24ff2f2
[openwrt/staging/chunkeey.git] /
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 +
5 WM8804
6
7 ASoC: digidac1-soundcard: use modern dai_link style
8
9 Signed-off-by: Hui Wang <hui.wang@canonical.com>
10 ---
11 sound/soc/bcm/digidac1-soundcard.c | 421 +++++++++++++++++++++++++++++
12 1 file changed, 421 insertions(+)
13 create mode 100644 sound/soc/bcm/digidac1-soundcard.c
14
15 --- /dev/null
16 +++ b/sound/soc/bcm/digidac1-soundcard.c
17 @@ -0,0 +1,421 @@
18 +/*
19 + * ASoC Driver for RRA DigiDAC1
20 + * Copyright 2016
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.
27 + *
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.
32 + */
33 +
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>
45 +
46 +#include "../codecs/wm8804.h"
47 +#include "../codecs/wm8741.h"
48 +
49 +#define WM8741_NUM_SUPPLIES 2
50 +
51 +/* codec private data */
52 +struct wm8741_priv {
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;
58 +};
59 +
60 +static int samplerate = 44100;
61 +
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 */
67 +/* of 0.125 dB. */
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 */
71 +
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 */
84 +};
85 +
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]),
96 +};
97 +
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]),
108 +};
109 +
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]),
120 +};
121 +
122 +static int w8741_add_controls(struct snd_soc_component *component)
123 +{
124 + struct wm8741_priv *wm8741 = snd_soc_component_get_drvdata(component);
125 +
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));
132 + break;
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));
137 + break;
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));
142 + break;
143 + default:
144 + return -EINVAL;
145 + }
146 +
147 + return 0;
148 +}
149 +
150 +static int digidac1_soundcard_init(struct snd_soc_pcm_runtime *rtd)
151 +{
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;
158 + int ret;
159 +
160 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
161 + if (!wm8741_rtd) {
162 + dev_warn(card->dev, "digidac1_soundcard_init: couldn't get wm8741 rtd\n");
163 + return -EFAULT;
164 + }
165 + wm8741_component = wm8741_rtd->codec_dai->component;
166 + ret = w8741_add_controls(wm8741_component);
167 + if (ret < 0)
168 + dev_warn(card->dev, "Failed to add new wm8741 controls: %d\n",
169 + ret);
170 +
171 + /* enable TX output */
172 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
173 +
174 + kctl = snd_soc_card_get_kcontrol(card,
175 + "Playback Volume");
176 + if (kctl) {
177 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
178 + snd_ctl_remove(sound_card, kctl);
179 + }
180 + kctl = snd_soc_card_get_kcontrol(card,
181 + "Fine Playback Volume");
182 + if (kctl) {
183 + kctl->vd[0].access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
184 + snd_ctl_remove(sound_card, kctl);
185 + }
186 + return 0;
187 +}
188 +
189 +static int digidac1_soundcard_startup(struct snd_pcm_substream *substream)
190 +{
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;
197 +
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);
200 + if (!wm8741_rtd) {
201 + dev_warn(card->dev, "digidac1_soundcard_startup: couldn't get WM8741 rtd\n");
202 + return -EFAULT;
203 + }
204 + wm8741_component = wm8741_rtd->codec_dai->component;
205 +
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);
215 +
216 + return 0;
217 +}
218 +
219 +static void digidac1_soundcard_shutdown(struct snd_pcm_substream *substream)
220 +{
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;
224 +
225 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x3c, 0x3c);
226 +}
227 +
228 +static int digidac1_soundcard_hw_params(struct snd_pcm_substream *substream,
229 + struct snd_pcm_hw_params *params)
230 +{
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;
238 +
239 + int sysclk = 27000000;
240 + long mclk_freq = 0;
241 + int mclk_div = 1;
242 + int sampling_freq = 1;
243 + int ret;
244 +
245 + wm8741_rtd = snd_soc_get_pcm_runtime(card, card->dai_link[1].name);
246 + if (!wm8741_rtd) {
247 + dev_warn(card->dev, "digidac1_soundcard_hw_params: couldn't get WM8741 rtd\n");
248 + return -EFAULT;
249 + }
250 + wm8741_component = wm8741_rtd->codec_dai->component;
251 + samplerate = params_rate(params);
252 +
253 + if (samplerate <= 96000) {
254 + mclk_freq = samplerate*256;
255 + mclk_div = WM8804_MCLKDIV_256FS;
256 + } else {
257 + mclk_freq = samplerate*128;
258 + mclk_div = WM8804_MCLKDIV_128FS;
259 + }
260 +
261 + switch (samplerate) {
262 + case 32000:
263 + sampling_freq = 0x03;
264 + break;
265 + case 44100:
266 + sampling_freq = 0x00;
267 + break;
268 + case 48000:
269 + sampling_freq = 0x02;
270 + break;
271 + case 88200:
272 + sampling_freq = 0x08;
273 + break;
274 + case 96000:
275 + sampling_freq = 0x0a;
276 + break;
277 + case 176400:
278 + sampling_freq = 0x0c;
279 + break;
280 + case 192000:
281 + sampling_freq = 0x0e;
282 + break;
283 + default:
284 + dev_err(card->dev,
285 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
286 + samplerate);
287 + }
288 +
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);
291 +
292 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
293 + sysclk, SND_SOC_CLOCK_OUT);
294 + if (ret < 0) {
295 + dev_err(card->dev,
296 + "Failed to set WM8804 SYSCLK: %d\n", ret);
297 + return ret;
298 + }
299 + /* Enable wm8804 TX output */
300 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x4, 0x0);
301 +
302 + /* wm8804 Power on */
303 + snd_soc_component_update_bits(component, WM8804_PWRDN, 0x9, 0);
304 +
305 + /* wm8804 set sampling frequency status bits */
306 + snd_soc_component_update_bits(component, WM8804_SPDTX4, 0x0f, sampling_freq);
307 +
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);
315 + else
316 + snd_soc_component_update_bits(wm8741_component, WM8741_MODE_CONTROL_1,
317 + WM8741_OSR_MASK, 0x40);
318 +
319 + /* wm8741 bit size */
320 + switch (params_width(params)) {
321 + case 16:
322 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
323 + WM8741_IWL_MASK, 0x00);
324 + break;
325 + case 20:
326 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
327 + WM8741_IWL_MASK, 0x01);
328 + break;
329 + case 24:
330 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
331 + WM8741_IWL_MASK, 0x02);
332 + break;
333 + case 32:
334 + snd_soc_component_update_bits(wm8741_component, WM8741_FORMAT_CONTROL,
335 + WM8741_IWL_MASK, 0x03);
336 + break;
337 + default:
338 + dev_dbg(card->dev, "wm8741_hw_params: Unsupported bit size param = %d",
339 + params_width(params));
340 + return -EINVAL;
341 + }
342 +
343 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
344 +}
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,
350 +};
351 +
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")));
356 +
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()));
361 +
362 +static struct snd_soc_dai_link digidac1_soundcard_dai[] = {
363 + {
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),
371 + },
372 + {
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),
379 + },
380 +};
381 +
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),
388 +};
389 +
390 +static int digidac1_soundcard_probe(struct platform_device *pdev)
391 +{
392 + int ret = 0;
393 +
394 + digidac1_soundcard.dev = &pdev->dev;
395 +
396 + if (pdev->dev.of_node) {
397 + struct device_node *i2s_node;
398 + struct snd_soc_dai_link *dai = &digidac1_soundcard_dai[0];
399 +
400 + i2s_node = of_parse_phandle(pdev->dev.of_node,
401 + "i2s-controller", 0);
402 +
403 + if (i2s_node) {
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;
408 + }
409 + }
410 +
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",
414 + ret);
415 +
416 + return ret;
417 +}
418 +
419 +static const struct of_device_id digidac1_soundcard_of_match[] = {
420 + { .compatible = "rra,digidac1-soundcard", },
421 + {},
422 +};
423 +MODULE_DEVICE_TABLE(of, digidac1_soundcard_of_match);
424 +
425 +static struct platform_driver digidac1_soundcard_driver = {
426 + .driver = {
427 + .name = "digidac1-audio",
428 + .owner = THIS_MODULE,
429 + .of_match_table = digidac1_soundcard_of_match,
430 + },
431 + .probe = digidac1_soundcard_probe,
432 +};
433 +
434 +module_platform_driver(digidac1_soundcard_driver);
435 +
436 +MODULE_AUTHOR("José M. Tasende <vintage@redrocksaudio.es>");
437 +MODULE_DESCRIPTION("ASoC Driver for RRA DigiDAC1");
438 +MODULE_LICENSE("GPL v2");