brcm2708: update to latest patches from the RPi foundation
[openwrt/staging/lynxis.git] / target / linux / brcm2708 / patches-4.14 / 950-0073-Add-Support-for-JustBoom-Audio-boards.patch
1 From c86b5cd0c5a5ba9b9937bc9cecbc3b25a4f037ad Mon Sep 17 00:00:00 2001
2 From: Aaron Shaw <shawaj@gmail.com>
3 Date: Thu, 7 Apr 2016 21:26:21 +0100
4 Subject: [PATCH 073/454] Add Support for JustBoom Audio boards
5
6 justboom-dac: Adjust for ALSA API change
7
8 As of 4.4, snd_soc_limit_volume now takes a struct snd_soc_card *
9 rather than a struct snd_soc_codec *.
10
11 Signed-off-by: Phil Elwell <phil@raspberrypi.org>
12 ---
13 sound/soc/bcm/Kconfig | 14 +++
14 sound/soc/bcm/Makefile | 4 +
15 sound/soc/bcm/justboom-dac.c | 163 +++++++++++++++++++++++++
16 sound/soc/bcm/justboom-digi.c | 216 ++++++++++++++++++++++++++++++++++
17 4 files changed, 397 insertions(+)
18 create mode 100644 sound/soc/bcm/justboom-dac.c
19 create mode 100644 sound/soc/bcm/justboom-digi.c
20
21 --- a/sound/soc/bcm/Kconfig
22 +++ b/sound/soc/bcm/Kconfig
23 @@ -60,6 +60,20 @@ config SND_BCM2708_SOC_RPI_PROTO
24 help
25 Say Y or M if you want to add support for Audio Codec Board PROTO (WM8731).
26
27 +config SND_BCM2708_SOC_JUSTBOOM_DAC
28 + tristate "Support for JustBoom DAC"
29 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
30 + select SND_SOC_PCM512x
31 + help
32 + Say Y or M if you want to add support for JustBoom DAC.
33 +
34 +config SND_BCM2708_SOC_JUSTBOOM_DIGI
35 + tristate "Support for JustBoom Digi"
36 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
37 + select SND_SOC_WM8804
38 + help
39 + Say Y or M if you want to add support for JustBoom Digi.
40 +
41 config SND_BCM2708_SOC_IQAUDIO_DAC
42 tristate "Support for IQaudIO-DAC"
43 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
44 --- a/sound/soc/bcm/Makefile
45 +++ b/sound/soc/bcm/Makefile
46 @@ -13,6 +13,8 @@ snd-soc-hifiberry-amp-objs := hifiberry_
47 snd-soc-hifiberry-dac-objs := hifiberry_dac.o
48 snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
49 snd-soc-hifiberry-digi-objs := hifiberry_digi.o
50 +snd-soc-justboom-dac-objs := justboom-dac.o
51 +snd-soc-justboom-digi-objs := justboom-digi.o
52 snd-soc-rpi-dac-objs := rpi-dac.o
53 snd-soc-rpi-proto-objs := rpi-proto.o
54 snd-soc-iqaudio-dac-objs := iqaudio-dac.o
55 @@ -22,6 +24,8 @@ obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_A
56 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
57 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
58 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
59 +obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
60 +obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DIGI) += snd-soc-justboom-digi.o
61 obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
62 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
63 obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
64 --- /dev/null
65 +++ b/sound/soc/bcm/justboom-dac.c
66 @@ -0,0 +1,163 @@
67 +/*
68 + * ASoC Driver for JustBoom DAC Raspberry Pi HAT Sound Card
69 + *
70 + * Author: Milan Neskovic
71 + * Copyright 2016
72 + * based on code by Daniel Matuschek <info@crazy-audio.com>
73 + * based on code by Florian Meier <florian.meier@koalo.de>
74 + *
75 + * This program is free software; you can redistribute it and/or
76 + * modify it under the terms of the GNU General Public License
77 + * version 2 as published by the Free Software Foundation.
78 + *
79 + * This program is distributed in the hope that it will be useful, but
80 + * WITHOUT ANY WARRANTY; without even the implied warranty of
81 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
82 + * General Public License for more details.
83 + */
84 +
85 +#include <linux/module.h>
86 +#include <linux/platform_device.h>
87 +
88 +#include <sound/core.h>
89 +#include <sound/pcm.h>
90 +#include <sound/pcm_params.h>
91 +#include <sound/soc.h>
92 +#include <sound/jack.h>
93 +
94 +#include "../codecs/pcm512x.h"
95 +
96 +static bool digital_gain_0db_limit = true;
97 +
98 +static int snd_rpi_justboom_dac_init(struct snd_soc_pcm_runtime *rtd)
99 +{
100 + struct snd_soc_codec *codec = rtd->codec;
101 + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
102 + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
103 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
104 +
105 + if (digital_gain_0db_limit)
106 + {
107 + int ret;
108 + struct snd_soc_card *card = rtd->card;
109 +
110 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
111 + if (ret < 0)
112 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
113 + }
114 +
115 + return 0;
116 +}
117 +
118 +static int snd_rpi_justboom_dac_hw_params(struct snd_pcm_substream *substream,
119 + struct snd_pcm_hw_params *params)
120 +{
121 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
122 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
123 + /*return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);*/
124 + unsigned int sample_bits =
125 + snd_pcm_format_physical_width(params_format(params));
126 + return snd_soc_dai_set_bclk_ratio(cpu_dai, sample_bits * 2);
127 +}
128 +
129 +static int snd_rpi_justboom_dac_startup(struct snd_pcm_substream *substream) {
130 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
131 + struct snd_soc_codec *codec = rtd->codec;
132 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
133 + return 0;
134 +}
135 +
136 +static void snd_rpi_justboom_dac_shutdown(struct snd_pcm_substream *substream) {
137 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
138 + struct snd_soc_codec *codec = rtd->codec;
139 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00);
140 +}
141 +
142 +/* machine stream operations */
143 +static struct snd_soc_ops snd_rpi_justboom_dac_ops = {
144 + .hw_params = snd_rpi_justboom_dac_hw_params,
145 + .startup = snd_rpi_justboom_dac_startup,
146 + .shutdown = snd_rpi_justboom_dac_shutdown,
147 +};
148 +
149 +static struct snd_soc_dai_link snd_rpi_justboom_dac_dai[] = {
150 +{
151 + .name = "JustBoom DAC",
152 + .stream_name = "JustBoom DAC HiFi",
153 + .cpu_dai_name = "bcm2708-i2s.0",
154 + .codec_dai_name = "pcm512x-hifi",
155 + .platform_name = "bcm2708-i2s.0",
156 + .codec_name = "pcm512x.1-004d",
157 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
158 + SND_SOC_DAIFMT_CBS_CFS,
159 + .ops = &snd_rpi_justboom_dac_ops,
160 + .init = snd_rpi_justboom_dac_init,
161 +},
162 +};
163 +
164 +/* audio machine driver */
165 +static struct snd_soc_card snd_rpi_justboom_dac = {
166 + .name = "snd_rpi_justboom_dac",
167 + .driver_name = "JustBoomDac",
168 + .owner = THIS_MODULE,
169 + .dai_link = snd_rpi_justboom_dac_dai,
170 + .num_links = ARRAY_SIZE(snd_rpi_justboom_dac_dai),
171 +};
172 +
173 +static int snd_rpi_justboom_dac_probe(struct platform_device *pdev)
174 +{
175 + int ret = 0;
176 +
177 + snd_rpi_justboom_dac.dev = &pdev->dev;
178 +
179 + if (pdev->dev.of_node) {
180 + struct device_node *i2s_node;
181 + struct snd_soc_dai_link *dai = &snd_rpi_justboom_dac_dai[0];
182 + i2s_node = of_parse_phandle(pdev->dev.of_node,
183 + "i2s-controller", 0);
184 +
185 + if (i2s_node) {
186 + dai->cpu_dai_name = NULL;
187 + dai->cpu_of_node = i2s_node;
188 + dai->platform_name = NULL;
189 + dai->platform_of_node = i2s_node;
190 + }
191 +
192 + digital_gain_0db_limit = !of_property_read_bool(
193 + pdev->dev.of_node, "justboom,24db_digital_gain");
194 + }
195 +
196 + ret = snd_soc_register_card(&snd_rpi_justboom_dac);
197 + if (ret && ret != -EPROBE_DEFER)
198 + dev_err(&pdev->dev,
199 + "snd_soc_register_card() failed: %d\n", ret);
200 +
201 + return ret;
202 +}
203 +
204 +static int snd_rpi_justboom_dac_remove(struct platform_device *pdev)
205 +{
206 + return snd_soc_unregister_card(&snd_rpi_justboom_dac);
207 +}
208 +
209 +static const struct of_device_id snd_rpi_justboom_dac_of_match[] = {
210 + { .compatible = "justboom,justboom-dac", },
211 + {},
212 +};
213 +MODULE_DEVICE_TABLE(of, snd_rpi_justboom_dac_of_match);
214 +
215 +static struct platform_driver snd_rpi_justboom_dac_driver = {
216 + .driver = {
217 + .name = "snd-rpi-justboom-dac",
218 + .owner = THIS_MODULE,
219 + .of_match_table = snd_rpi_justboom_dac_of_match,
220 + },
221 + .probe = snd_rpi_justboom_dac_probe,
222 + .remove = snd_rpi_justboom_dac_remove,
223 +};
224 +
225 +module_platform_driver(snd_rpi_justboom_dac_driver);
226 +
227 +MODULE_AUTHOR("Milan Neskovic <info@justboom.co>");
228 +MODULE_DESCRIPTION("ASoC Driver for JustBoom PI DAC HAT Sound Card");
229 +MODULE_LICENSE("GPL v2");
230 --- /dev/null
231 +++ b/sound/soc/bcm/justboom-digi.c
232 @@ -0,0 +1,216 @@
233 +/*
234 + * ASoC Driver for JustBoom Raspberry Pi Digi HAT Sound Card
235 + *
236 + * Author: Milan Neskovic
237 + * Copyright 2016
238 + * based on code by Daniel Matuschek <info@crazy-audio.com>
239 + * based on code by Florian Meier <florian.meier@koalo.de>
240 + *
241 + * This program is free software; you can redistribute it and/or
242 + * modify it under the terms of the GNU General Public License
243 + * version 2 as published by the Free Software Foundation.
244 + *
245 + * This program is distributed in the hope that it will be useful, but
246 + * WITHOUT ANY WARRANTY; without even the implied warranty of
247 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
248 + * General Public License for more details.
249 + */
250 +
251 +#include <linux/module.h>
252 +#include <linux/platform_device.h>
253 +
254 +#include <sound/core.h>
255 +#include <sound/pcm.h>
256 +#include <sound/pcm_params.h>
257 +#include <sound/soc.h>
258 +#include <sound/jack.h>
259 +
260 +#include "../codecs/wm8804.h"
261 +
262 +static int snd_rpi_justboom_digi_init(struct snd_soc_pcm_runtime *rtd)
263 +{
264 + struct snd_soc_codec *codec = rtd->codec;
265 +
266 + /* enable TX output */
267 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
268 +
269 + return 0;
270 +}
271 +
272 +static int snd_rpi_justboom_digi_startup(struct snd_pcm_substream *substream) {
273 + /* turn on digital output */
274 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
275 + struct snd_soc_codec *codec = rtd->codec;
276 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
277 + return 0;
278 +}
279 +
280 +static void snd_rpi_justboom_digi_shutdown(struct snd_pcm_substream *substream) {
281 + /* turn off output */
282 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
283 + struct snd_soc_codec *codec = rtd->codec;
284 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
285 +}
286 +
287 +static int snd_rpi_justboom_digi_hw_params(struct snd_pcm_substream *substream,
288 + struct snd_pcm_hw_params *params)
289 +{
290 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
291 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
292 + struct snd_soc_codec *codec = rtd->codec;
293 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
294 +
295 + int sysclk = 27000000; /* This is fixed on this board */
296 +
297 + long mclk_freq=0;
298 + int mclk_div=1;
299 + int sampling_freq=1;
300 +
301 + int ret;
302 +
303 + int samplerate = params_rate(params);
304 +
305 + if (samplerate<=96000) {
306 + mclk_freq=samplerate*256;
307 + mclk_div=WM8804_MCLKDIV_256FS;
308 + } else {
309 + mclk_freq=samplerate*128;
310 + mclk_div=WM8804_MCLKDIV_128FS;
311 + }
312 +
313 + switch (samplerate) {
314 + case 32000:
315 + sampling_freq=0x03;
316 + break;
317 + case 44100:
318 + sampling_freq=0x00;
319 + break;
320 + case 48000:
321 + sampling_freq=0x02;
322 + break;
323 + case 88200:
324 + sampling_freq=0x08;
325 + break;
326 + case 96000:
327 + sampling_freq=0x0a;
328 + break;
329 + case 176400:
330 + sampling_freq=0x0c;
331 + break;
332 + case 192000:
333 + sampling_freq=0x0e;
334 + break;
335 + default:
336 + dev_err(codec->dev,
337 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
338 + samplerate);
339 + }
340 +
341 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
342 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
343 +
344 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
345 + sysclk, SND_SOC_CLOCK_OUT);
346 + if (ret < 0) {
347 + dev_err(codec->dev,
348 + "Failed to set WM8804 SYSCLK: %d\n", ret);
349 + return ret;
350 + }
351 +
352 + /* Enable TX output */
353 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
354 +
355 + /* Power on */
356 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
357 +
358 + /* set sampling frequency status bits */
359 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
360 +
361 + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
362 +}
363 +
364 +/* machine stream operations */
365 +static struct snd_soc_ops snd_rpi_justboom_digi_ops = {
366 + .hw_params = snd_rpi_justboom_digi_hw_params,
367 + .startup = snd_rpi_justboom_digi_startup,
368 + .shutdown = snd_rpi_justboom_digi_shutdown,
369 +};
370 +
371 +static struct snd_soc_dai_link snd_rpi_justboom_digi_dai[] = {
372 +{
373 + .name = "JustBoom Digi",
374 + .stream_name = "JustBoom Digi HiFi",
375 + .cpu_dai_name = "bcm2708-i2s.0",
376 + .codec_dai_name = "wm8804-spdif",
377 + .platform_name = "bcm2708-i2s.0",
378 + .codec_name = "wm8804.1-003b",
379 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
380 + SND_SOC_DAIFMT_CBM_CFM,
381 + .ops = &snd_rpi_justboom_digi_ops,
382 + .init = snd_rpi_justboom_digi_init,
383 +},
384 +};
385 +
386 +/* audio machine driver */
387 +static struct snd_soc_card snd_rpi_justboom_digi = {
388 + .name = "snd_rpi_justboom_digi",
389 + .driver_name = "JustBoomDigi",
390 + .owner = THIS_MODULE,
391 + .dai_link = snd_rpi_justboom_digi_dai,
392 + .num_links = ARRAY_SIZE(snd_rpi_justboom_digi_dai),
393 +};
394 +
395 +static int snd_rpi_justboom_digi_probe(struct platform_device *pdev)
396 +{
397 + int ret = 0;
398 +
399 + snd_rpi_justboom_digi.dev = &pdev->dev;
400 +
401 + if (pdev->dev.of_node) {
402 + struct device_node *i2s_node;
403 + struct snd_soc_dai_link *dai = &snd_rpi_justboom_digi_dai[0];
404 + i2s_node = of_parse_phandle(pdev->dev.of_node,
405 + "i2s-controller", 0);
406 +
407 + if (i2s_node) {
408 + dai->cpu_dai_name = NULL;
409 + dai->cpu_of_node = i2s_node;
410 + dai->platform_name = NULL;
411 + dai->platform_of_node = i2s_node;
412 + }
413 + }
414 +
415 + ret = snd_soc_register_card(&snd_rpi_justboom_digi);
416 + if (ret && ret != -EPROBE_DEFER)
417 + dev_err(&pdev->dev,
418 + "snd_soc_register_card() failed: %d\n", ret);
419 +
420 + return ret;
421 +}
422 +
423 +static int snd_rpi_justboom_digi_remove(struct platform_device *pdev)
424 +{
425 + return snd_soc_unregister_card(&snd_rpi_justboom_digi);
426 +}
427 +
428 +static const struct of_device_id snd_rpi_justboom_digi_of_match[] = {
429 + { .compatible = "justboom,justboom-digi", },
430 + {},
431 +};
432 +MODULE_DEVICE_TABLE(of, snd_rpi_justboom_digi_of_match);
433 +
434 +static struct platform_driver snd_rpi_justboom_digi_driver = {
435 + .driver = {
436 + .name = "snd-rpi-justboom-digi",
437 + .owner = THIS_MODULE,
438 + .of_match_table = snd_rpi_justboom_digi_of_match,
439 + },
440 + .probe = snd_rpi_justboom_digi_probe,
441 + .remove = snd_rpi_justboom_digi_remove,
442 +};
443 +
444 +module_platform_driver(snd_rpi_justboom_digi_driver);
445 +
446 +MODULE_AUTHOR("Milan Neskovic <info@justboom.co>");
447 +MODULE_DESCRIPTION("ASoC Driver for JustBoom PI Digi HAT Sound Card");
448 +MODULE_LICENSE("GPL v2");