421b18abc1877fc9cece4ce5ba04f7cce17eb0e2
[openwrt/svn-archive/archive.git] / target / linux / brcm2708 / patches-4.4 / 0110-Add-support-for-the-HiFiBerry-DAC-Pro.patch
1 From 92a639f7362cb40bce4eb0019967e7d84aed90d1 Mon Sep 17 00:00:00 2001
2 From: Stuart MacLean <stuart@hifiberry.com>
3 Date: Fri, 2 Oct 2015 15:12:59 +0100
4 Subject: [PATCH 110/156] Add support for the HiFiBerry DAC+ Pro.
5
6 The HiFiBerry DAC+ and DAC+ Pro products both use the existing bcm sound driver with the DAC+ Pro having a special clock device driver representing the two high precision oscillators.
7
8 An addition bug fix is included for the PCM512x codec where by the physical size of the sample frame is used in the calculation of the LRCK divisor as it was found to be wrong when using 24-bit depth sample contained in a little endian 4-byte sample frame.
9 ---
10 .../dts/overlays/hifiberry-dacplus-overlay.dts | 15 +-
11 drivers/clk/Makefile | 1 +
12 drivers/clk/clk-hifiberry-dacpro.c | 160 ++++++++++++++
13 sound/soc/bcm/hifiberry_dacplus.c | 244 +++++++++++++++++++--
14 sound/soc/codecs/pcm512x.c | 3 +-
15 5 files changed, 396 insertions(+), 27 deletions(-)
16 create mode 100644 drivers/clk/clk-hifiberry-dacpro.c
17
18 --- a/arch/arm/boot/dts/overlays/hifiberry-dacplus-overlay.dts
19 +++ b/arch/arm/boot/dts/overlays/hifiberry-dacplus-overlay.dts
20 @@ -6,6 +6,16 @@
21 compatible = "brcm,bcm2708";
22
23 fragment@0 {
24 + target-path = "/clocks";
25 + __overlay__ {
26 + dacpro_osc: dacpro_osc {
27 + compatible = "hifiberry,dacpro-clk";
28 + #clock-cells = <0>;
29 + };
30 + };
31 + };
32 +
33 + fragment@1 {
34 target = <&sound>;
35 __overlay__ {
36 compatible = "hifiberry,hifiberry-dacplus";
37 @@ -14,14 +24,14 @@
38 };
39 };
40
41 - fragment@1 {
42 + fragment@2 {
43 target = <&i2s>;
44 __overlay__ {
45 status = "okay";
46 };
47 };
48
49 - fragment@2 {
50 + fragment@3 {
51 target = <&i2c1>;
52 __overlay__ {
53 #address-cells = <1>;
54 @@ -32,6 +42,7 @@
55 #sound-dai-cells = <0>;
56 compatible = "ti,pcm5122";
57 reg = <0x4d>;
58 + clocks = <&dacpro_osc>;
59 status = "okay";
60 };
61 };
62 --- a/drivers/clk/Makefile
63 +++ b/drivers/clk/Makefile
64 @@ -24,6 +24,7 @@ obj-$(CONFIG_COMMON_CLK_CDCE706) += clk-
65 obj-$(CONFIG_ARCH_CLPS711X) += clk-clps711x.o
66 obj-$(CONFIG_ARCH_EFM32) += clk-efm32gg.o
67 obj-$(CONFIG_ARCH_HIGHBANK) += clk-highbank.o
68 +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += clk-hifiberry-dacpro.o
69 obj-$(CONFIG_MACH_LOONGSON32) += clk-ls1x.o
70 obj-$(CONFIG_COMMON_CLK_MAX_GEN) += clk-max-gen.o
71 obj-$(CONFIG_COMMON_CLK_MAX77686) += clk-max77686.o
72 --- /dev/null
73 +++ b/drivers/clk/clk-hifiberry-dacpro.c
74 @@ -0,0 +1,160 @@
75 +/*
76 + * Clock Driver for HiFiBerry DAC Pro
77 + *
78 + * Author: Stuart MacLean
79 + * Copyright 2015
80 + *
81 + * This program is free software; you can redistribute it and/or
82 + * modify it under the terms of the GNU General Public License
83 + * version 2 as published by the Free Software Foundation.
84 + *
85 + * This program is distributed in the hope that it will be useful, but
86 + * WITHOUT ANY WARRANTY; without even the implied warranty of
87 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
88 + * General Public License for more details.
89 + */
90 +
91 +#include <linux/clk-provider.h>
92 +#include <linux/clkdev.h>
93 +#include <linux/kernel.h>
94 +#include <linux/module.h>
95 +#include <linux/of.h>
96 +#include <linux/slab.h>
97 +#include <linux/platform_device.h>
98 +
99 +/* Clock rate of CLK44EN attached to GPIO6 pin */
100 +#define CLK_44EN_RATE 22579200UL
101 +/* Clock rate of CLK48EN attached to GPIO3 pin */
102 +#define CLK_48EN_RATE 24576000UL
103 +
104 +/**
105 + * struct hifiberry_dacpro_clk - Common struct to the HiFiBerry DAC Pro
106 + * @hw: clk_hw for the common clk framework
107 + * @mode: 0 => CLK44EN, 1 => CLK48EN
108 + */
109 +struct clk_hifiberry_hw {
110 + struct clk_hw hw;
111 + uint8_t mode;
112 +};
113 +
114 +#define to_hifiberry_clk(_hw) container_of(_hw, struct clk_hifiberry_hw, hw)
115 +
116 +static const struct of_device_id clk_hifiberry_dacpro_dt_ids[] = {
117 + { .compatible = "hifiberry,dacpro-clk",},
118 + { }
119 +};
120 +MODULE_DEVICE_TABLE(of, clk_hifiberry_dacpro_dt_ids);
121 +
122 +static unsigned long clk_hifiberry_dacpro_recalc_rate(struct clk_hw *hw,
123 + unsigned long parent_rate)
124 +{
125 + return (to_hifiberry_clk(hw)->mode == 0) ? CLK_44EN_RATE :
126 + CLK_48EN_RATE;
127 +}
128 +
129 +static long clk_hifiberry_dacpro_round_rate(struct clk_hw *hw,
130 + unsigned long rate, unsigned long *parent_rate)
131 +{
132 + long actual_rate;
133 +
134 + if (rate <= CLK_44EN_RATE) {
135 + actual_rate = (long)CLK_44EN_RATE;
136 + } else if (rate >= CLK_48EN_RATE) {
137 + actual_rate = (long)CLK_48EN_RATE;
138 + } else {
139 + long diff44Rate = (long)(rate - CLK_44EN_RATE);
140 + long diff48Rate = (long)(CLK_48EN_RATE - rate);
141 +
142 + if (diff44Rate < diff48Rate)
143 + actual_rate = (long)CLK_44EN_RATE;
144 + else
145 + actual_rate = (long)CLK_48EN_RATE;
146 + }
147 + return actual_rate;
148 +}
149 +
150 +
151 +static int clk_hifiberry_dacpro_set_rate(struct clk_hw *hw,
152 + unsigned long rate, unsigned long parent_rate)
153 +{
154 + unsigned long actual_rate;
155 + struct clk_hifiberry_hw *clk = to_hifiberry_clk(hw);
156 +
157 + actual_rate = (unsigned long)clk_hifiberry_dacpro_round_rate(hw, rate,
158 + &parent_rate);
159 + clk->mode = (actual_rate == CLK_44EN_RATE) ? 0 : 1;
160 + return 0;
161 +}
162 +
163 +
164 +const struct clk_ops clk_hifiberry_dacpro_rate_ops = {
165 + .recalc_rate = clk_hifiberry_dacpro_recalc_rate,
166 + .round_rate = clk_hifiberry_dacpro_round_rate,
167 + .set_rate = clk_hifiberry_dacpro_set_rate,
168 +};
169 +
170 +static int clk_hifiberry_dacpro_probe(struct platform_device *pdev)
171 +{
172 + int ret;
173 + struct clk_hifiberry_hw *proclk;
174 + struct clk *clk;
175 + struct device *dev;
176 + struct clk_init_data init;
177 +
178 + dev = &pdev->dev;
179 +
180 + proclk = kzalloc(sizeof(struct clk_hifiberry_hw), GFP_KERNEL);
181 + if (!proclk)
182 + return -ENOMEM;
183 +
184 + init.name = "clk-hifiberry-dacpro";
185 + init.ops = &clk_hifiberry_dacpro_rate_ops;
186 + init.flags = CLK_IS_ROOT | CLK_IS_BASIC;
187 + init.parent_names = NULL;
188 + init.num_parents = 0;
189 +
190 + proclk->mode = 0;
191 + proclk->hw.init = &init;
192 +
193 + clk = devm_clk_register(dev, &proclk->hw);
194 + if (!IS_ERR(clk)) {
195 + ret = of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
196 + clk);
197 + } else {
198 + dev_err(dev, "Fail to register clock driver\n");
199 + kfree(proclk);
200 + ret = PTR_ERR(clk);
201 + }
202 + return ret;
203 +}
204 +
205 +static int clk_hifiberry_dacpro_remove(struct platform_device *pdev)
206 +{
207 + of_clk_del_provider(pdev->dev.of_node);
208 + return 0;
209 +}
210 +
211 +static struct platform_driver clk_hifiberry_dacpro_driver = {
212 + .probe = clk_hifiberry_dacpro_probe,
213 + .remove = clk_hifiberry_dacpro_remove,
214 + .driver = {
215 + .name = "clk-hifiberry-dacpro",
216 + .of_match_table = clk_hifiberry_dacpro_dt_ids,
217 + },
218 +};
219 +
220 +static int __init clk_hifiberry_dacpro_init(void)
221 +{
222 + return platform_driver_register(&clk_hifiberry_dacpro_driver);
223 +}
224 +core_initcall(clk_hifiberry_dacpro_init);
225 +
226 +static void __exit clk_hifiberry_dacpro_exit(void)
227 +{
228 + platform_driver_unregister(&clk_hifiberry_dacpro_driver);
229 +}
230 +module_exit(clk_hifiberry_dacpro_exit);
231 +
232 +MODULE_DESCRIPTION("HiFiBerry DAC Pro clock driver");
233 +MODULE_LICENSE("GPL v2");
234 +MODULE_ALIAS("platform:clk-hifiberry-dacpro");
235 --- a/sound/soc/bcm/hifiberry_dacplus.c
236 +++ b/sound/soc/bcm/hifiberry_dacplus.c
237 @@ -1,8 +1,8 @@
238 /*
239 - * ASoC Driver for HiFiBerry DAC+
240 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro
241 *
242 - * Author: Daniel Matuschek
243 - * Copyright 2014
244 + * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
245 + * Copyright 2014-2015
246 * based on code by Florian Meier <florian.meier@koalo.de>
247 *
248 * This program is free software; you can redistribute it and/or
249 @@ -17,6 +17,13 @@
250
251 #include <linux/module.h>
252 #include <linux/platform_device.h>
253 +#include <linux/kernel.h>
254 +#include <linux/clk.h>
255 +#include <linux/kernel.h>
256 +#include <linux/module.h>
257 +#include <linux/of.h>
258 +#include <linux/slab.h>
259 +#include <linux/delay.h>
260
261 #include <sound/core.h>
262 #include <sound/pcm.h>
263 @@ -26,34 +33,222 @@
264
265 #include "../codecs/pcm512x.h"
266
267 +#define HIFIBERRY_DACPRO_NOCLOCK 0
268 +#define HIFIBERRY_DACPRO_CLK44EN 1
269 +#define HIFIBERRY_DACPRO_CLK48EN 2
270 +
271 +struct pcm512x_priv {
272 + struct regmap *regmap;
273 + struct clk *sclk;
274 +};
275 +
276 +/* Clock rate of CLK44EN attached to GPIO6 pin */
277 +#define CLK_44EN_RATE 22579200UL
278 +/* Clock rate of CLK48EN attached to GPIO3 pin */
279 +#define CLK_48EN_RATE 24576000UL
280 +
281 +static bool snd_rpi_hifiberry_is_dacpro;
282 +
283 +static void snd_rpi_hifiberry_dacplus_select_clk(struct snd_soc_codec *codec,
284 + int clk_id)
285 +{
286 + switch (clk_id) {
287 + case HIFIBERRY_DACPRO_NOCLOCK:
288 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
289 + break;
290 + case HIFIBERRY_DACPRO_CLK44EN:
291 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
292 + break;
293 + case HIFIBERRY_DACPRO_CLK48EN:
294 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
295 + break;
296 + }
297 +}
298 +
299 +static void snd_rpi_hifiberry_dacplus_clk_gpio(struct snd_soc_codec *codec)
300 +{
301 + snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x24, 0x24);
302 + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
303 + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
304 +}
305 +
306 +static bool snd_rpi_hifiberry_dacplus_is_sclk(struct snd_soc_codec *codec)
307 +{
308 + int sck;
309 +
310 + sck = snd_soc_read(codec, PCM512x_RATE_DET_4);
311 + return (!(sck & 0x40));
312 +}
313 +
314 +static bool snd_rpi_hifiberry_dacplus_is_sclk_sleep(
315 + struct snd_soc_codec *codec)
316 +{
317 + msleep(2);
318 + return snd_rpi_hifiberry_dacplus_is_sclk(codec);
319 +}
320 +
321 +static bool snd_rpi_hifiberry_dacplus_is_pro_card(struct snd_soc_codec *codec)
322 +{
323 + bool isClk44EN, isClk48En, isNoClk;
324 +
325 + snd_rpi_hifiberry_dacplus_clk_gpio(codec);
326 +
327 + snd_rpi_hifiberry_dacplus_select_clk(codec, HIFIBERRY_DACPRO_CLK44EN);
328 + isClk44EN = snd_rpi_hifiberry_dacplus_is_sclk_sleep(codec);
329 +
330 + snd_rpi_hifiberry_dacplus_select_clk(codec, HIFIBERRY_DACPRO_NOCLOCK);
331 + isNoClk = snd_rpi_hifiberry_dacplus_is_sclk_sleep(codec);
332 +
333 + snd_rpi_hifiberry_dacplus_select_clk(codec, HIFIBERRY_DACPRO_CLK48EN);
334 + isClk48En = snd_rpi_hifiberry_dacplus_is_sclk_sleep(codec);
335 +
336 + return (isClk44EN && isClk48En && !isNoClk);
337 +}
338 +
339 +static int snd_rpi_hifiberry_dacplus_clk_for_rate(int sample_rate)
340 +{
341 + int type;
342 +
343 + switch (sample_rate) {
344 + case 11025:
345 + case 22050:
346 + case 44100:
347 + case 88200:
348 + case 176400:
349 + type = HIFIBERRY_DACPRO_CLK44EN;
350 + break;
351 + default:
352 + type = HIFIBERRY_DACPRO_CLK48EN;
353 + break;
354 + }
355 + return type;
356 +}
357 +
358 +static void snd_rpi_hifiberry_dacplus_set_sclk(struct snd_soc_codec *codec,
359 + int sample_rate)
360 +{
361 + struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
362 +
363 + if (!IS_ERR(pcm512x->sclk)) {
364 + int ctype;
365 +
366 + ctype = snd_rpi_hifiberry_dacplus_clk_for_rate(sample_rate);
367 + clk_set_rate(pcm512x->sclk, (ctype == HIFIBERRY_DACPRO_CLK44EN)
368 + ? CLK_44EN_RATE : CLK_48EN_RATE);
369 + snd_rpi_hifiberry_dacplus_select_clk(codec, ctype);
370 + }
371 +}
372 +
373 static int snd_rpi_hifiberry_dacplus_init(struct snd_soc_pcm_runtime *rtd)
374 {
375 struct snd_soc_codec *codec = rtd->codec;
376 + struct pcm512x_priv *priv;
377 +
378 + snd_rpi_hifiberry_is_dacpro
379 + = snd_rpi_hifiberry_dacplus_is_pro_card(codec);
380 +
381 + if (snd_rpi_hifiberry_is_dacpro) {
382 + struct snd_soc_dai_link *dai = rtd->dai_link;
383 +
384 + dai->name = "HiFiBerry DAC+ Pro";
385 + dai->stream_name = "HiFiBerry DAC+ Pro HiFi";
386 + dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
387 + | SND_SOC_DAIFMT_CBM_CFM;
388 +
389 + snd_soc_update_bits(codec, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
390 + snd_soc_update_bits(codec, PCM512x_MASTER_MODE, 0x03, 0x03);
391 + snd_soc_update_bits(codec, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
392 + } else {
393 + priv = snd_soc_codec_get_drvdata(codec);
394 + priv->sclk = ERR_PTR(-ENOENT);
395 + }
396 +
397 snd_soc_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
398 - snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0xf, 0x02);
399 - snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
400 + snd_soc_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
401 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
402 +
403 + return 0;
404 +}
405 +
406 +static int snd_rpi_hifiberry_dacplus_update_rate_den(
407 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
408 +{
409 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
410 + struct snd_soc_codec *codec = rtd->codec;
411 + struct pcm512x_priv *pcm512x = snd_soc_codec_get_drvdata(codec);
412 + struct snd_ratnum *rats_no_pll;
413 + unsigned int num = 0, den = 0;
414 + int err;
415 +
416 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
417 + if (!rats_no_pll)
418 + return -ENOMEM;
419 +
420 + rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
421 + rats_no_pll->den_min = 1;
422 + rats_no_pll->den_max = 128;
423 + rats_no_pll->den_step = 1;
424 +
425 + err = snd_interval_ratnum(hw_param_interval(params,
426 + SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
427 + if (err >= 0 && den) {
428 + params->rate_num = num;
429 + params->rate_den = den;
430 + }
431 +
432 + devm_kfree(rtd->dev, rats_no_pll);
433 return 0;
434 }
435
436 -static int snd_rpi_hifiberry_dacplus_hw_params(struct snd_pcm_substream *substream,
437 - struct snd_pcm_hw_params *params)
438 +static int snd_rpi_hifiberry_dacplus_set_bclk_ratio_pro(
439 + struct snd_soc_dai *cpu_dai, struct snd_pcm_hw_params *params)
440 +{
441 + int bratio = snd_pcm_format_physical_width(params_format(params))
442 + * params_channels(params);
443 + return snd_soc_dai_set_bclk_ratio(cpu_dai, bratio);
444 +}
445 +
446 +static int snd_rpi_hifiberry_dacplus_hw_params(
447 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
448 {
449 + int ret;
450 struct snd_soc_pcm_runtime *rtd = substream->private_data;
451 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
452 - return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
453 +
454 + if (snd_rpi_hifiberry_is_dacpro) {
455 + struct snd_soc_codec *codec = rtd->codec;
456 +
457 + snd_rpi_hifiberry_dacplus_set_sclk(codec,
458 + params_rate(params));
459 +
460 + ret = snd_rpi_hifiberry_dacplus_set_bclk_ratio_pro(cpu_dai,
461 + params);
462 + if (!ret)
463 + ret = snd_rpi_hifiberry_dacplus_update_rate_den(
464 + substream, params);
465 + } else {
466 + ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
467 + }
468 + return ret;
469 }
470
471 -static int snd_rpi_hifiberry_dacplus_startup(struct snd_pcm_substream *substream) {
472 +static int snd_rpi_hifiberry_dacplus_startup(
473 + struct snd_pcm_substream *substream)
474 +{
475 struct snd_soc_pcm_runtime *rtd = substream->private_data;
476 struct snd_soc_codec *codec = rtd->codec;
477 - snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x08);
478 +
479 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
480 return 0;
481 }
482
483 -static void snd_rpi_hifiberry_dacplus_shutdown(struct snd_pcm_substream *substream) {
484 +static void snd_rpi_hifiberry_dacplus_shutdown(
485 + struct snd_pcm_substream *substream)
486 +{
487 struct snd_soc_pcm_runtime *rtd = substream->private_data;
488 struct snd_soc_codec *codec = rtd->codec;
489 - snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08,0x00);
490 +
491 + snd_soc_update_bits(codec, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
492 }
493
494 /* machine stream operations */
495 @@ -90,19 +285,20 @@ static int snd_rpi_hifiberry_dacplus_pro
496 int ret = 0;
497
498 snd_rpi_hifiberry_dacplus.dev = &pdev->dev;
499 -
500 if (pdev->dev.of_node) {
501 - struct device_node *i2s_node;
502 - struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_dacplus_dai[0];
503 - i2s_node = of_parse_phandle(pdev->dev.of_node,
504 - "i2s-controller", 0);
505 -
506 - if (i2s_node) {
507 - dai->cpu_dai_name = NULL;
508 - dai->cpu_of_node = i2s_node;
509 - dai->platform_name = NULL;
510 - dai->platform_of_node = i2s_node;
511 - }
512 + struct device_node *i2s_node;
513 + struct snd_soc_dai_link *dai;
514 +
515 + dai = &snd_rpi_hifiberry_dacplus_dai[0];
516 + i2s_node = of_parse_phandle(pdev->dev.of_node,
517 + "i2s-controller", 0);
518 +
519 + if (i2s_node) {
520 + dai->cpu_dai_name = NULL;
521 + dai->cpu_of_node = i2s_node;
522 + dai->platform_name = NULL;
523 + dai->platform_of_node = i2s_node;
524 + }
525 }
526
527 ret = snd_soc_register_card(&snd_rpi_hifiberry_dacplus);
528 --- a/sound/soc/codecs/pcm512x.c
529 +++ b/sound/soc/codecs/pcm512x.c
530 @@ -854,7 +854,8 @@ static int pcm512x_set_dividers(struct s
531 int fssp;
532 int gpio;
533
534 - lrclk_div = snd_soc_params_to_frame_size(params);
535 + lrclk_div = snd_pcm_format_physical_width(params_format(params))
536 + * params_channels(params);
537 if (lrclk_div == 0) {
538 dev_err(dev, "No LRCLK?\n");
539 return -EINVAL;