1 From 77dce745cd500cbe65e4cbb613c27c23e26f5bbb 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 drivers/clk/Makefile | 1 +
29 drivers/clk/clk-allo-dac.c | 161 ++++++++++++
30 sound/soc/bcm/allo-boss-dac.c | 456 ++++++++++++++++++++++++++++++++++
31 3 files changed, 618 insertions(+)
32 create mode 100644 drivers/clk/clk-allo-dac.c
33 create mode 100644 sound/soc/bcm/allo-boss-dac.c
35 --- a/drivers/clk/Makefile
36 +++ b/drivers/clk/Makefile
37 @@ -18,6 +18,7 @@ endif
39 # hardware specific clock types
40 # please keep this section sorted lexicographically by file path name
41 +obj-$(CONFIG_SND_BCM2708_SOC_ALLO_BOSS_DAC) += clk-allo-dac.o
42 obj-$(CONFIG_MACH_ASM9260) += clk-asm9260.o
43 obj-$(CONFIG_COMMON_CLK_AXI_CLKGEN) += clk-axi-clkgen.o
44 obj-$(CONFIG_ARCH_AXXIA) += clk-axm5516.o
46 +++ b/drivers/clk/clk-allo-dac.c
49 + * Clock Driver for Allo DAC
51 + * Author: Baswaraj K <jaikumar@cem-solutions.net>
53 + * based on code by Stuart MacLean
55 + * This program is free software; you can redistribute it and/or
56 + * modify it under the terms of the GNU General Public License
57 + * version 2 as published by the Free Software Foundation.
59 + * This program is distributed in the hope that it will be useful, but
60 + * WITHOUT ANY WARRANTY; without even the implied warranty of
61 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
62 + * General Public License for more details.
65 +#include <linux/clk-provider.h>
66 +#include <linux/clkdev.h>
67 +#include <linux/kernel.h>
68 +#include <linux/module.h>
69 +#include <linux/of.h>
70 +#include <linux/slab.h>
71 +#include <linux/platform_device.h>
73 +/* Clock rate of CLK44EN attached to GPIO6 pin */
74 +#define CLK_44EN_RATE 45158400UL
75 +/* Clock rate of CLK48EN attached to GPIO3 pin */
76 +#define CLK_48EN_RATE 49152000UL
79 + * struct allo_dac_clk - Common struct to the Allo DAC
80 + * @hw: clk_hw for the common clk framework
81 + * @mode: 0 => CLK44EN, 1 => CLK48EN
88 +#define to_allo_clk(_hw) container_of(_hw, struct clk_allo_hw, hw)
90 +static const struct of_device_id clk_allo_dac_dt_ids[] = {
91 + { .compatible = "allo,dac-clk",},
94 +MODULE_DEVICE_TABLE(of, clk_allo_dac_dt_ids);
96 +static unsigned long clk_allo_dac_recalc_rate(struct clk_hw *hw,
97 + unsigned long parent_rate)
99 + return (to_allo_clk(hw)->mode == 0) ? CLK_44EN_RATE :
103 +static long clk_allo_dac_round_rate(struct clk_hw *hw,
104 + unsigned long rate, unsigned long *parent_rate)
108 + if (rate <= CLK_44EN_RATE) {
109 + actual_rate = (long)CLK_44EN_RATE;
110 + } else if (rate >= CLK_48EN_RATE) {
111 + actual_rate = (long)CLK_48EN_RATE;
113 + long diff44Rate = (long)(rate - CLK_44EN_RATE);
114 + long diff48Rate = (long)(CLK_48EN_RATE - rate);
116 + if (diff44Rate < diff48Rate)
117 + actual_rate = (long)CLK_44EN_RATE;
119 + actual_rate = (long)CLK_48EN_RATE;
121 + return actual_rate;
125 +static int clk_allo_dac_set_rate(struct clk_hw *hw,
126 + unsigned long rate, unsigned long parent_rate)
128 + unsigned long actual_rate;
129 + struct clk_allo_hw *clk = to_allo_clk(hw);
131 + actual_rate = (unsigned long)clk_allo_dac_round_rate(hw, rate,
133 + clk->mode = (actual_rate == CLK_44EN_RATE) ? 0 : 1;
138 +const struct clk_ops clk_allo_dac_rate_ops = {
139 + .recalc_rate = clk_allo_dac_recalc_rate,
140 + .round_rate = clk_allo_dac_round_rate,
141 + .set_rate = clk_allo_dac_set_rate,
144 +static int clk_allo_dac_probe(struct platform_device *pdev)
147 + struct clk_allo_hw *proclk;
149 + struct device *dev;
150 + struct clk_init_data init;
154 + proclk = kzalloc(sizeof(struct clk_allo_hw), GFP_KERNEL);
158 + init.name = "clk-allo-dac";
159 + init.ops = &clk_allo_dac_rate_ops;
160 + init.flags = CLK_IS_BASIC;
161 + init.parent_names = NULL;
162 + init.num_parents = 0;
165 + proclk->hw.init = &init;
167 + clk = devm_clk_register(dev, &proclk->hw);
168 + if (!IS_ERR(clk)) {
169 + ret = of_clk_add_provider(dev->of_node, of_clk_src_simple_get,
172 + dev_err(dev, "Fail to register clock driver\n");
174 + ret = PTR_ERR(clk);
179 +static int clk_allo_dac_remove(struct platform_device *pdev)
181 + of_clk_del_provider(pdev->dev.of_node);
185 +static struct platform_driver clk_allo_dac_driver = {
186 + .probe = clk_allo_dac_probe,
187 + .remove = clk_allo_dac_remove,
189 + .name = "clk-allo-dac",
190 + .of_match_table = clk_allo_dac_dt_ids,
194 +static int __init clk_allo_dac_init(void)
196 + return platform_driver_register(&clk_allo_dac_driver);
198 +core_initcall(clk_allo_dac_init);
200 +static void __exit clk_allo_dac_exit(void)
202 + platform_driver_unregister(&clk_allo_dac_driver);
204 +module_exit(clk_allo_dac_exit);
206 +MODULE_DESCRIPTION("Allo DAC clock driver");
207 +MODULE_LICENSE("GPL v2");
208 +MODULE_ALIAS("platform:clk-allo-dac");
210 +++ b/sound/soc/bcm/allo-boss-dac.c
213 + * ALSA ASoC Machine Driver for Allo Boss DAC
215 + * Author: Baswaraj K <jaikumar@cem-solutions.net>
217 + * based on code by Daniel Matuschek,
218 + * Stuart MacLean <stuart@hifiberry.com>
219 + * based on code by Florian Meier <florian.meier@koalo.de>
221 + * This program is free software; you can redistribute it and/or
222 + * modify it under the terms of the GNU General Public License
223 + * version 2 as published by the Free Software Foundation.
225 + * This program is distributed in the hope that it will be useful, but
226 + * WITHOUT ANY WARRANTY; without even the implied warranty of
227 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
228 + * General Public License for more details.
231 +#include <linux/module.h>
232 +#include <linux/gpio/consumer.h>
233 +#include <linux/platform_device.h>
234 +#include <linux/clk.h>
235 +#include <linux/delay.h>
237 +#include <sound/core.h>
238 +#include <sound/pcm.h>
239 +#include <sound/pcm_params.h>
240 +#include <sound/soc.h>
241 +#include "../codecs/pcm512x.h"
243 +#define ALLO_BOSS_NOCLOCK 0
244 +#define ALLO_BOSS_CLK44EN 1
245 +#define ALLO_BOSS_CLK48EN 2
247 +struct pcm512x_priv {
248 + struct regmap *regmap;
252 +static struct gpio_desc *mute_gpio;
254 +/* Clock rate of CLK44EN attached to GPIO6 pin */
255 +#define CLK_44EN_RATE 45158400UL
256 +/* Clock rate of CLK48EN attached to GPIO3 pin */
257 +#define CLK_48EN_RATE 49152000UL
260 +static bool snd_soc_allo_boss_master;
261 +static bool digital_gain_0db_limit = true;
263 +static void snd_allo_boss_select_clk(struct snd_soc_component *component,
267 + case ALLO_BOSS_NOCLOCK:
268 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
270 + case ALLO_BOSS_CLK44EN:
271 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
273 + case ALLO_BOSS_CLK48EN:
274 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
279 +static void snd_allo_boss_clk_gpio(struct snd_soc_component *component)
281 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
282 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
283 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
286 +static bool snd_allo_boss_is_sclk(struct snd_soc_component *component)
290 + snd_soc_component_read(component, PCM512x_RATE_DET_4, &sck);
291 + return (!(sck & 0x40));
294 +static bool snd_allo_boss_is_sclk_sleep(
295 + struct snd_soc_component *component)
298 + return snd_allo_boss_is_sclk(component);
301 +static bool snd_allo_boss_is_master_card(struct snd_soc_component *component)
303 + bool isClk44EN, isClk48En, isNoClk;
305 + snd_allo_boss_clk_gpio(component);
307 + snd_allo_boss_select_clk(component, ALLO_BOSS_CLK44EN);
308 + isClk44EN = snd_allo_boss_is_sclk_sleep(component);
310 + snd_allo_boss_select_clk(component, ALLO_BOSS_NOCLOCK);
311 + isNoClk = snd_allo_boss_is_sclk_sleep(component);
313 + snd_allo_boss_select_clk(component, ALLO_BOSS_CLK48EN);
314 + isClk48En = snd_allo_boss_is_sclk_sleep(component);
316 + return (isClk44EN && isClk48En && !isNoClk);
319 +static int snd_allo_boss_clk_for_rate(int sample_rate)
323 + switch (sample_rate) {
330 + type = ALLO_BOSS_CLK44EN;
333 + type = ALLO_BOSS_CLK48EN;
339 +static void snd_allo_boss_set_sclk(struct snd_soc_component *component,
342 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
344 + if (!IS_ERR(pcm512x->sclk)) {
347 + ctype = snd_allo_boss_clk_for_rate(sample_rate);
348 + clk_set_rate(pcm512x->sclk, (ctype == ALLO_BOSS_CLK44EN)
349 + ? CLK_44EN_RATE : CLK_48EN_RATE);
350 + snd_allo_boss_select_clk(component, ctype);
354 +static int snd_allo_boss_init(struct snd_soc_pcm_runtime *rtd)
356 + struct snd_soc_component *component = rtd->codec_dai->component;
357 + struct pcm512x_priv *priv = snd_soc_component_get_drvdata(component);
360 + snd_soc_allo_boss_master = false;
362 + snd_soc_allo_boss_master =
363 + snd_allo_boss_is_master_card(component);
365 + if (snd_soc_allo_boss_master) {
366 + struct snd_soc_dai_link *dai = rtd->dai_link;
368 + dai->name = "BossDAC";
369 + dai->stream_name = "Boss DAC HiFi [Master]";
370 + dai->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
371 + | SND_SOC_DAIFMT_CBM_CFM;
373 + snd_soc_component_update_bits(component, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
374 + snd_soc_component_update_bits(component, PCM512x_MASTER_MODE, 0x03, 0x03);
375 + snd_soc_component_update_bits(component, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
377 + * Default sclk to CLK_48EN_RATE, otherwise codec
378 + * pcm512x_dai_startup_master method could call
379 + * snd_pcm_hw_constraint_ratnums using CLK_44EN/64
380 + * which will mask 384k sample rate.
382 + if (!IS_ERR(priv->sclk))
383 + clk_set_rate(priv->sclk, CLK_48EN_RATE);
385 + priv->sclk = ERR_PTR(-ENOENT);
388 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x08, 0x08);
389 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
390 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
392 + if (digital_gain_0db_limit) {
394 + struct snd_soc_card *card = rtd->card;
396 + ret = snd_soc_limit_volume(card, "Digital Playback Volume",
399 + dev_warn(card->dev, "Failed to set volume limit: %d\n",
406 +static int snd_allo_boss_update_rate_den(
407 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
409 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
410 + struct snd_soc_component *component = rtd->codec_dai->component;
411 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
412 + struct snd_ratnum *rats_no_pll;
413 + unsigned int num = 0, den = 0;
416 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
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;
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;
432 + devm_kfree(rtd->dev, rats_no_pll);
436 +static void snd_allo_boss_gpio_mute(struct snd_soc_card *card)
439 + gpiod_set_value_cansleep(mute_gpio, 1);
442 +static void snd_allo_boss_gpio_unmute(struct snd_soc_card *card)
445 + gpiod_set_value_cansleep(mute_gpio, 0);
448 +static int snd_allo_boss_set_bias_level(struct snd_soc_card *card,
449 + struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
451 + struct snd_soc_pcm_runtime *rtd;
452 + struct snd_soc_dai *codec_dai;
454 + rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
455 + codec_dai = rtd->codec_dai;
457 + if (dapm->dev != codec_dai->dev)
461 + case SND_SOC_BIAS_PREPARE:
462 + if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
465 + snd_allo_boss_gpio_unmute(card);
468 + case SND_SOC_BIAS_STANDBY:
469 + if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
472 + snd_allo_boss_gpio_mute(card);
482 +static int snd_allo_boss_hw_params(
483 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
486 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
487 + int channels = params_channels(params);
488 + int width = snd_pcm_format_physical_width(params_format(params));
490 + if (snd_soc_allo_boss_master) {
491 + struct snd_soc_component *component = rtd->codec_dai->component;
493 + snd_allo_boss_set_sclk(component,
494 + params_rate(params));
496 + ret = snd_allo_boss_update_rate_den(
497 + substream, params);
502 + ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x03, 0x03,
506 + ret = snd_soc_dai_set_tdm_slot(rtd->codec_dai, 0x03, 0x03,
511 +static int snd_allo_boss_startup(
512 + struct snd_pcm_substream *substream)
514 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
515 + struct snd_soc_component *component = rtd->codec_dai->component;
516 + struct snd_soc_card *card = rtd->card;
518 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
519 + snd_allo_boss_gpio_mute(card);
521 + if (snd_soc_allo_boss_master) {
522 + struct pcm512x_priv *priv = snd_soc_component_get_drvdata(component);
524 + * Default sclk to CLK_48EN_RATE, otherwise codec
525 + * pcm512x_dai_startup_master method could call
526 + * snd_pcm_hw_constraint_ratnums using CLK_44EN/64
527 + * which will mask 384k sample rate.
529 + if (!IS_ERR(priv->sclk))
530 + clk_set_rate(priv->sclk, CLK_48EN_RATE);
536 +static void snd_allo_boss_shutdown(
537 + struct snd_pcm_substream *substream)
539 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
540 + struct snd_soc_component *component = rtd->codec_dai->component;
542 + snd_soc_component_update_bits(component, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
545 +static int snd_allo_boss_prepare(
546 + struct snd_pcm_substream *substream)
548 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
549 + struct snd_soc_card *card = rtd->card;
551 + snd_allo_boss_gpio_unmute(card);
554 +/* machine stream operations */
555 +static struct snd_soc_ops snd_allo_boss_ops = {
556 + .hw_params = snd_allo_boss_hw_params,
557 + .startup = snd_allo_boss_startup,
558 + .shutdown = snd_allo_boss_shutdown,
559 + .prepare = snd_allo_boss_prepare,
562 +static struct snd_soc_dai_link snd_allo_boss_dai[] = {
564 + .name = "Boss DAC",
565 + .stream_name = "Boss DAC HiFi",
566 + .cpu_dai_name = "bcm2708-i2s.0",
567 + .codec_dai_name = "pcm512x-hifi",
568 + .platform_name = "bcm2708-i2s.0",
569 + .codec_name = "pcm512x.1-004d",
570 + .dai_fmt = SND_SOC_DAIFMT_I2S |
571 + SND_SOC_DAIFMT_NB_NF |
572 + SND_SOC_DAIFMT_CBS_CFS,
573 + .ops = &snd_allo_boss_ops,
574 + .init = snd_allo_boss_init,
578 +/* audio machine driver */
579 +static struct snd_soc_card snd_allo_boss = {
581 + .owner = THIS_MODULE,
582 + .dai_link = snd_allo_boss_dai,
583 + .num_links = ARRAY_SIZE(snd_allo_boss_dai),
586 +static int snd_allo_boss_probe(struct platform_device *pdev)
590 + snd_allo_boss.dev = &pdev->dev;
592 + if (pdev->dev.of_node) {
593 + struct device_node *i2s_node;
594 + struct snd_soc_dai_link *dai;
596 + dai = &snd_allo_boss_dai[0];
597 + i2s_node = of_parse_phandle(pdev->dev.of_node,
598 + "i2s-controller", 0);
601 + dai->cpu_dai_name = NULL;
602 + dai->cpu_of_node = i2s_node;
603 + dai->platform_name = NULL;
604 + dai->platform_of_node = i2s_node;
607 + digital_gain_0db_limit = !of_property_read_bool(
608 + pdev->dev.of_node, "allo,24db_digital_gain");
609 + slave = of_property_read_bool(pdev->dev.of_node,
612 + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute",
614 + if (IS_ERR(mute_gpio)) {
615 + ret = PTR_ERR(mute_gpio);
616 + dev_err(&pdev->dev,
617 + "failed to get mute gpio: %d\n", ret);
622 + snd_allo_boss.set_bias_level =
623 + snd_allo_boss_set_bias_level;
625 + ret = snd_soc_register_card(&snd_allo_boss);
627 + dev_err(&pdev->dev,
628 + "snd_soc_register_card() failed: %d\n", ret);
633 + snd_allo_boss_gpio_mute(&snd_allo_boss);
641 +static int snd_allo_boss_remove(struct platform_device *pdev)
643 + snd_allo_boss_gpio_mute(&snd_allo_boss);
644 + return snd_soc_unregister_card(&snd_allo_boss);
647 +static const struct of_device_id snd_allo_boss_of_match[] = {
648 + { .compatible = "allo,boss-dac", },
649 + { /* sentinel */ },
651 +MODULE_DEVICE_TABLE(of, snd_allo_boss_of_match);
653 +static struct platform_driver snd_allo_boss_driver = {
655 + .name = "snd-allo-boss-dac",
656 + .owner = THIS_MODULE,
657 + .of_match_table = snd_allo_boss_of_match,
659 + .probe = snd_allo_boss_probe,
660 + .remove = snd_allo_boss_remove,
663 +module_platform_driver(snd_allo_boss_driver);
665 +MODULE_AUTHOR("Baswaraj K <jaikumar@cem-solutions.net>");
666 +MODULE_DESCRIPTION("ALSA ASoC Machine Driver for Allo Boss DAC");
667 +MODULE_LICENSE("GPL v2");