1 From 7a13759deeaae55840571574e32e00f0e5902661 Mon Sep 17 00:00:00 2001
2 From: BabuSubashChandar <babuenir@gmail.com>
3 Date: Tue, 28 Mar 2017 20:04:42 +0530
4 Subject: [PATCH] Add support for Allo Boss DAC add-on board for
7 Signed-off-by: Baswaraj K <jaikumar@cem-solutions.net>
8 Reviewed-by: Deepak <deepak@zilogic.com>
9 Reviewed-by: BabuSubashChandar <babusubashchandar@zilogic.com>
11 Add support for new clock rate and mute gpios.
13 Signed-off-by: Baswaraj K <jaikumar@cem-solutions.net>
14 Reviewed-by: Deepak <deepak@zilogic.com>
15 Reviewed-by: BabuSubashChandar <babusubashchandar@zilogic.com>
17 ASoC: allo-boss-dac: fix S24_LE format
19 Remove set_bclk_ratio call so 24-bit data is transmitted in
22 Signed-off-by: Matthias Reichl <hias@horus.com>
24 ASoC: allo-boss-dac: transmit S24_LE with 64 BCLK cycles
26 Signed-off-by: Matthias Reichl <hias@horus.com>
28 allo-boss-dac: switch to snd_soc_dai_set_bclk_ratio
30 Signed-off-by: Matthias Reichl <hias@horus.com>
32 ASoC: allo-boss-dac: use modern dai_link style
34 Signed-off-by: Hui Wang <hui.wang@canonical.com>
36 drivers/clk/Makefile | 1 +
37 drivers/clk/clk-allo-dac.c | 161 ++++++++++++
38 sound/soc/bcm/allo-boss-dac.c | 456 ++++++++++++++++++++++++++++++++++
39 3 files changed, 618 insertions(+)
40 create mode 100644 drivers/clk/clk-allo-dac.c
41 create mode 100644 sound/soc/bcm/allo-boss-dac.c
43 --- a/drivers/clk/Makefile
44 +++ b/drivers/clk/Makefile
45 @@ -18,6 +18,7 @@ endif
47 # hardware specific clock types
48 # please keep this section sorted lexicographically by file path name
49 +obj-$(CONFIG_SND_BCM2708_SOC_ALLO_BOSS_DAC) += clk-allo-dac.o
50 obj-$(CONFIG_MACH_ASM9260) += clk-asm9260.o
51 obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN) += clk-axi-clkgen.o
52 obj-$(CONFIG_ARCH_AXXIA) += clk-axm5516.o
54 +++ b/drivers/clk/clk-allo-dac.c
57 + * Clock Driver for Allo DAC
59 + * Author: Baswaraj K <jaikumar@cem-solutions.net>
61 + * based on code by Stuart MacLean
63 + * This program is free software; you can redistribute it and/or
64 + * modify it under the terms of the GNU General Public License
65 + * version 2 as published by the Free Software Foundation.
67 + * This program is distributed in the hope that it will be useful, but
68 + * WITHOUT ANY WARRANTY; without even the implied warranty of
69 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
70 + * General Public License for more details.
73 +#include <linux/clk-provider.h>
74 +#include <linux/clkdev.h>
75 +#include <linux/kernel.h>
76 +#include <linux/module.h>
77 +#include <linux/of.h>
78 +#include <linux/slab.h>
79 +#include <linux/platform_device.h>
81 +/* Clock rate of CLK44EN attached to GPIO6 pin */
82 +#define CLK_44EN_RATE 45158400UL
83 +/* Clock rate of CLK48EN attached to GPIO3 pin */
84 +#define CLK_48EN_RATE 49152000UL
87 + * struct allo_dac_clk - Common struct to the Allo DAC
88 + * @hw: clk_hw for the common clk framework
89 + * @mode: 0 => CLK44EN, 1 => CLK48EN
96 +#define to_allo_clk(_hw) container_of(_hw, struct clk_allo_hw, hw)
98 +static const struct of_device_id clk_allo_dac_dt_ids[] = {
99 + { .compatible = "allo,dac-clk",},
102 +MODULE_DEVICE_TABLE(of, clk_allo_dac_dt_ids);
104 +static unsigned long clk_allo_dac_recalc_rate(struct clk_hw *hw,
105 + unsigned long parent_rate)
107 + return (to_allo_clk(hw)->mode == 0) ? CLK_44EN_RATE :
111 +static long clk_allo_dac_round_rate(struct clk_hw *hw,
112 + unsigned long rate, unsigned long *parent_rate)
116 + if (rate <= CLK_44EN_RATE) {
117 + actual_rate = (long)CLK_44EN_RATE;
118 + } else if (rate >= CLK_48EN_RATE) {
119 + actual_rate = (long)CLK_48EN_RATE;
121 + long diff44Rate = (long)(rate - CLK_44EN_RATE);
122 + long diff48Rate = (long)(CLK_48EN_RATE - rate);
124 + if (diff44Rate < diff48Rate)
125 + actual_rate = (long)CLK_44EN_RATE;
127 + actual_rate = (long)CLK_48EN_RATE;
129 + return actual_rate;
133 +static int clk_allo_dac_set_rate(struct clk_hw *hw,
134 + unsigned long rate, unsigned long parent_rate)
136 + unsigned long actual_rate;
137 + struct clk_allo_hw *clk = to_allo_clk(hw);
139 + actual_rate = (unsigned long)clk_allo_dac_round_rate(hw, rate,
141 + clk->mode = (actual_rate == CLK_44EN_RATE) ? 0 : 1;
146 +const struct clk_ops clk_allo_dac_rate_ops = {
147 + .recalc_rate = clk_allo_dac_recalc_rate,
148 + .round_rate = clk_allo_dac_round_rate,
149 + .set_rate = clk_allo_dac_set_rate,
152 +static int clk_allo_dac_probe(struct platform_device *pdev)
155 + struct clk_allo_hw *proclk;
157 + struct device *dev;
158 + struct clk_init_data init;
162 + proclk = kzalloc(sizeof(struct clk_allo_hw), GFP_KERNEL);
166 + init.name = "clk-allo-dac";
167 + init.ops = &clk_allo_dac_rate_ops;
169 + init.parent_names = NULL;
170 + init.num_parents = 0;
173 + proclk->hw.init = &init;
175 + clk = devm_clk_register(dev, &proclk->hw);
176 + if (!IS_ERR(clk)) {
177 + ret = of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
180 + dev_err(dev, "Fail to register clock driver\n");
182 + ret = PTR_ERR(clk);
187 +static int clk_allo_dac_remove(struct platform_device *pdev)
189 + of_clk_del_provider(pdev->dev.of_node);
193 +static struct platform_driver clk_allo_dac_driver = {
194 + .probe = clk_allo_dac_probe,
195 + .remove = clk_allo_dac_remove,
197 + .name = "clk-allo-dac",
198 + .of_match_table = clk_allo_dac_dt_ids,
202 +static int __init clk_allo_dac_init(void)
204 + return platform_driver_register(&clk_allo_dac_driver);
206 +core_initcall(clk_allo_dac_init);
208 +static void __exit clk_allo_dac_exit(void)
210 + platform_driver_unregister(&clk_allo_dac_driver);
212 +module_exit(clk_allo_dac_exit);
214 +MODULE_DESCRIPTION("Allo DAC clock driver");
215 +MODULE_LICENSE("GPL v2");
216 +MODULE_ALIAS("platform:clk-allo-dac");
218 +++ b/sound/soc/bcm/allo-boss-dac.c
221 + * ALSA ASoC Machine Driver for Allo Boss DAC
223 + * Author: Baswaraj K <jaikumar@cem-solutions.net>
225 + * based on code by Daniel Matuschek,
226 + * Stuart MacLean <stuart@hifiberry.com>
227 + * based on code by Florian Meier <florian.meier@koalo.de>
229 + * This program is free software; you can redistribute it and/or
230 + * modify it under the terms of the GNU General Public License
231 + * version 2 as published by the Free Software Foundation.
233 + * This program is distributed in the hope that it will be useful, but
234 + * WITHOUT ANY WARRANTY; without even the implied warranty of
235 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
236 + * General Public License for more details.
239 +#include <linux/module.h>
240 +#include <linux/gpio/consumer.h>
241 +#include <linux/platform_device.h>
242 +#include <linux/clk.h>
243 +#include <linux/delay.h>
245 +#include <sound/core.h>
246 +#include <sound/pcm.h>
247 +#include <sound/pcm_params.h>
248 +#include <sound/soc.h>
249 +#include "../codecs/pcm512x.h"
251 +#define ALLO_BOSS_NOCLOCK 0
252 +#define ALLO_BOSS_CLK44EN 1
253 +#define ALLO_BOSS_CLK48EN 2
255 +struct pcm512x_priv {
256 + struct regmap *regmap;
260 +static struct gpio_desc *mute_gpio;
262 +/* Clock rate of CLK44EN attached to GPIO6 pin */
263 +#define CLK_44EN_RATE 45158400UL
264 +/* Clock rate of CLK48EN attached to GPIO3 pin */
265 +#define CLK_48EN_RATE 49152000UL
268 +static bool snd_soc_allo_boss_master;
269 +static bool digital_gain_0db_limit = true;
271 +static void snd_allo_boss_select_clk(struct snd_soc_component *component,
275 + case ALLO_BOSS_NOCLOCK:
276 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
278 + case ALLO_BOSS_CLK44EN:
279 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
281 + case ALLO_BOSS_CLK48EN:
282 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
287 +static void snd_allo_boss_clk_gpio(struct snd_soc_component *component)
289 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
290 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
291 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
294 +static bool snd_allo_boss_is_sclk(struct snd_soc_component *component)
298 + snd_soc_component_read(component, PCM512x_RATE_DET_4, &sck);
299 + return (!(sck & 0x40));
302 +static bool snd_allo_boss_is_sclk_sleep(
303 + struct snd_soc_component *component)
306 + return snd_allo_boss_is_sclk(component);
309 +static bool snd_allo_boss_is_master_card(struct snd_soc_component *component)
311 + bool isClk44EN, isClk48En, isNoClk;
313 + snd_allo_boss_clk_gpio(component);
315 + snd_allo_boss_select_clk(component, ALLO_BOSS_CLK44EN);
316 + isClk44EN = snd_allo_boss_is_sclk_sleep(component);
318 + snd_allo_boss_select_clk(component, ALLO_BOSS_NOCLOCK);
319 + isNoClk = snd_allo_boss_is_sclk_sleep(component);
321 + snd_allo_boss_select_clk(component, ALLO_BOSS_CLK48EN);
322 + isClk48En = snd_allo_boss_is_sclk_sleep(component);
324 + return (isClk44EN && isClk48En && !isNoClk);
327 +static int snd_allo_boss_clk_for_rate(int sample_rate)
331 + switch (sample_rate) {
338 + type = ALLO_BOSS_CLK44EN;
341 + type = ALLO_BOSS_CLK48EN;
347 +static void snd_allo_boss_set_sclk(struct snd_soc_component *component,
350 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
352 + if (!IS_ERR(pcm512x->sclk)) {
355 + ctype = snd_allo_boss_clk_for_rate(sample_rate);
356 + clk_set_rate(pcm512x->sclk, (ctype == ALLO_BOSS_CLK44EN)
357 + ? CLK_44EN_RATE : CLK_48EN_RATE);
358 + snd_allo_boss_select_clk(component, ctype);
362 +static int snd_allo_boss_init(struct snd_soc_pcm_runtime *rtd)
364 + struct snd_soc_component *component = rtd->codec_dai->component;
365 + struct pcm512x_priv *priv = snd_soc_component_get_drvdata(component);
368 + snd_soc_allo_boss_master = false;
370 + snd_soc_allo_boss_master =
371 + snd_allo_boss_is_master_card(component);
373 + if (snd_soc_allo_boss_master) {
374 + struct snd_soc_dai_link *dai = rtd->dai_link;
376 + dai->name = "BossDAC";
377 + dai->stream_name = "Boss DAC HiFi [Master]";
378 + dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
379 + | SND_SOC_DAIFMT_CBM_CFM;
381 + snd_soc_component_update_bits(component, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
382 + snd_soc_component_update_bits(component, PCM512x_MASTER_MODE, 0x03, 0x03);
383 + snd_soc_component_update_bits(component, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
385 + * Default sclk to CLK_48EN_RATE, otherwise codec
386 + * pcm512x_dai_startup_master method could call
387 + * snd_pcm_hw_constraint_ratnums using CLK_44EN/64
388 + * which will mask 384k sample rate.
390 + if (!IS_ERR(priv->sclk))
391 + clk_set_rate(priv->sclk, CLK_48EN_RATE);
393 + priv->sclk = ERR_PTR(-ENOENT);
396 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x08, 0x08);
397 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
398 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
400 + if (digital_gain_0db_limit) {
402 + struct snd_soc_card *card = rtd->card;
404 + ret = snd_soc_limit_volume(card, "Digital Playback Volume",
407 + dev_warn(card->dev, "Failed to set volume limit: %d\n",
414 +static int snd_allo_boss_update_rate_den(
415 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
417 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
418 + struct snd_soc_component *component = rtd->codec_dai->component;
419 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
420 + struct snd_ratnum *rats_no_pll;
421 + unsigned int num = 0, den = 0;
424 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
428 + rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
429 + rats_no_pll->den_min = 1;
430 + rats_no_pll->den_max = 128;
431 + rats_no_pll->den_step = 1;
433 + err = snd_interval_ratnum(hw_param_interval(params,
434 + SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
435 + if (err >= 0 && den) {
436 + params->rate_num = num;
437 + params->rate_den = den;
440 + devm_kfree(rtd->dev, rats_no_pll);
444 +static void snd_allo_boss_gpio_mute(struct snd_soc_card *card)
447 + gpiod_set_value_cansleep(mute_gpio, 1);
450 +static void snd_allo_boss_gpio_unmute(struct snd_soc_card *card)
453 + gpiod_set_value_cansleep(mute_gpio, 0);
456 +static int snd_allo_boss_set_bias_level(struct snd_soc_card *card,
457 + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
459 + struct snd_soc_pcm_runtime *rtd;
460 + struct snd_soc_dai *codec_dai;
462 + rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
463 + codec_dai = rtd->codec_dai;
465 + if (dapm->dev != codec_dai->dev)
469 + case SND_SOC_BIAS_PREPARE:
470 + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
473 + snd_allo_boss_gpio_unmute(card);
476 + case SND_SOC_BIAS_STANDBY:
477 + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
480 + snd_allo_boss_gpio_mute(card);
490 +static int snd_allo_boss_hw_params(
491 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
494 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
495 + int channels = params_channels(params);
496 + int width = snd_pcm_format_physical_width(params_format(params));
498 + if (snd_soc_allo_boss_master) {
499 + struct snd_soc_component *component = rtd->codec_dai->component;
501 + snd_allo_boss_set_sclk(component,
502 + params_rate(params));
504 + ret = snd_allo_boss_update_rate_den(
505 + substream, params);
510 + ret = snd_soc_dai_set_bclk_ratio(rtd->cpu_dai, channels * width);
513 + ret = snd_soc_dai_set_bclk_ratio(rtd->codec_dai, channels * width);
517 +static int snd_allo_boss_startup(
518 + struct snd_pcm_substream *substream)
520 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
521 + struct snd_soc_component *component = rtd->codec_dai->component;
522 + struct snd_soc_card *card = rtd->card;
524 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
525 + snd_allo_boss_gpio_mute(card);
527 + if (snd_soc_allo_boss_master) {
528 + struct pcm512x_priv *priv = snd_soc_component_get_drvdata(component);
530 + * Default sclk to CLK_48EN_RATE, otherwise codec
531 + * pcm512x_dai_startup_master method could call
532 + * snd_pcm_hw_constraint_ratnums using CLK_44EN/64
533 + * which will mask 384k sample rate.
535 + if (!IS_ERR(priv->sclk))
536 + clk_set_rate(priv->sclk, CLK_48EN_RATE);
542 +static void snd_allo_boss_shutdown(
543 + struct snd_pcm_substream *substream)
545 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 + struct snd_soc_component *component = rtd->codec_dai->component;
548 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
551 +static int snd_allo_boss_prepare(
552 + struct snd_pcm_substream *substream)
554 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
555 + struct snd_soc_card *card = rtd->card;
557 + snd_allo_boss_gpio_unmute(card);
560 +/* machine stream operations */
561 +static struct snd_soc_ops snd_allo_boss_ops = {
562 + .hw_params = snd_allo_boss_hw_params,
563 + .startup = snd_allo_boss_startup,
564 + .shutdown = snd_allo_boss_shutdown,
565 + .prepare = snd_allo_boss_prepare,
568 +SND_SOC_DAILINK_DEFS(allo_boss,
569 + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
570 + DAILINK_COMP_ARRAY(COMP_CODEC("pcm512x.1-004d", "pcm512x-hifi")),
571 + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
573 +static struct snd_soc_dai_link snd_allo_boss_dai[] = {
575 + .name = "Boss DAC",
576 + .stream_name = "Boss DAC HiFi",
577 + .dai_fmt = SND_SOC_DAIFMT_I2S |
578 + SND_SOC_DAIFMT_NB_NF |
579 + SND_SOC_DAIFMT_CBS_CFS,
580 + .ops = &snd_allo_boss_ops,
581 + .init = snd_allo_boss_init,
582 + SND_SOC_DAILINK_REG(allo_boss),
586 +/* audio machine driver */
587 +static struct snd_soc_card snd_allo_boss = {
589 + .owner = THIS_MODULE,
590 + .dai_link = snd_allo_boss_dai,
591 + .num_links = ARRAY_SIZE(snd_allo_boss_dai),
594 +static int snd_allo_boss_probe(struct platform_device *pdev)
598 + snd_allo_boss.dev = &pdev->dev;
600 + if (pdev->dev.of_node) {
601 + struct device_node *i2s_node;
602 + struct snd_soc_dai_link *dai;
604 + dai = &snd_allo_boss_dai[0];
605 + i2s_node = of_parse_phandle(pdev->dev.of_node,
606 + "i2s-controller", 0);
609 + dai->cpus->dai_name = NULL;
610 + dai->cpus->of_node = i2s_node;
611 + dai->platforms->name = NULL;
612 + dai->platforms->of_node = i2s_node;
615 + digital_gain_0db_limit = !of_property_read_bool(
616 + pdev->dev.of_node, "allo,24db_digital_gain");
617 + slave = of_property_read_bool(pdev->dev.of_node,
620 + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
622 + if (IS_ERR(mute_gpio)) {
623 + ret = PTR_ERR(mute_gpio);
624 + dev_err(&pdev->dev,
625 + "failed to get mute gpio: %d\n", ret);
630 + snd_allo_boss.set_bias_level =
631 + snd_allo_boss_set_bias_level;
633 + ret = snd_soc_register_card(&snd_allo_boss);
635 + dev_err(&pdev->dev,
636 + "snd_soc_register_card() failed: %d\n", ret);
641 + snd_allo_boss_gpio_mute(&snd_allo_boss);
649 +static int snd_allo_boss_remove(struct platform_device *pdev)
651 + snd_allo_boss_gpio_mute(&snd_allo_boss);
652 + return snd_soc_unregister_card(&snd_allo_boss);
655 +static const struct of_device_id snd_allo_boss_of_match[] = {
656 + { .compatible = "allo,boss-dac", },
657 + { /* sentinel */ },
659 +MODULE_DEVICE_TABLE(of, snd_allo_boss_of_match);
661 +static struct platform_driver snd_allo_boss_driver = {
663 + .name = "snd-allo-boss-dac",
664 + .owner = THIS_MODULE,
665 + .of_match_table = snd_allo_boss_of_match,
667 + .probe = snd_allo_boss_probe,
668 + .remove = snd_allo_boss_remove,
671 +module_platform_driver(snd_allo_boss_driver);
673 +MODULE_AUTHOR("Baswaraj K <jaikumar@cem-solutions.net>");
674 +MODULE_DESCRIPTION("ALSA ASoC Machine Driver for Allo Boss DAC");
675 +MODULE_LICENSE("GPL v2");