c1c4604e362a652066696d0542a8a9ce7997477b
[openwrt.git] / target / linux / brcm2708 / patches-3.14 / 0044-ASoC-BCM-Add-support-for-HiFiBerry-Digi.-Driver-is-b.patch
1 From dad63ece33a339a45c992915eae9d7d8885e98c7 Mon Sep 17 00:00:00 2001
2 From: Daniel Matuschek <info@crazy-audio.com>
3 Date: Wed, 15 Jan 2014 21:42:08 +0100
4 Subject: [PATCH 44/54] ASoC: BCM:Add support for HiFiBerry Digi. Driver is
5  based on the patched WM8804 driver.
6
7 Signed-off-by: Daniel Matuschek <daniel@matuschek.net>
8 ---
9  sound/soc/bcm/Kconfig          |   7 ++
10  sound/soc/bcm/Makefile         |   2 +
11  sound/soc/bcm/hifiberry_digi.c | 153 +++++++++++++++++++++++++++++++++++++++++
12  3 files changed, 162 insertions(+)
13  create mode 100644 sound/soc/bcm/hifiberry_digi.c
14
15 diff --git a/sound/soc/bcm/Kconfig b/sound/soc/bcm/Kconfig
16 index 714841d..e563dbc 100644
17 --- a/sound/soc/bcm/Kconfig
18 +++ b/sound/soc/bcm/Kconfig
19 @@ -26,6 +26,13 @@ config SND_BCM2708_SOC_HIFIBERRY_DAC
20          help
21           Say Y or M if you want to add support for HifiBerry DAC.
22  
23 +config SND_BCM2708_SOC_HIFIBERRY_DIGI
24 +        tristate "Support for HifiBerry Digi"
25 +        depends on SND_BCM2708_SOC_I2S
26 +        select SND_SOC_WM8804
27 +        help
28 +         Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
29 +
30  config SND_BCM2708_SOC_RPI_DAC
31          tristate "Support for RPi-DAC"
32          depends on SND_BCM2708_SOC_I2S
33 diff --git a/sound/soc/bcm/Makefile b/sound/soc/bcm/Makefile
34 index ccc9809..826df7d 100644
35 --- a/sound/soc/bcm/Makefile
36 +++ b/sound/soc/bcm/Makefile
37 @@ -10,7 +10,9 @@ obj-$(CONFIG_SND_BCM2708_SOC_I2S) += snd-soc-bcm2708-i2s.o
38  
39  # BCM2708 Machine Support
40  snd-soc-hifiberry-dac-objs := hifiberry_dac.o
41 +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
42  snd-soc-rpi-dac-objs := rpi-dac.o
43  
44  obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
45 +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
46  obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
47 diff --git a/sound/soc/bcm/hifiberry_digi.c b/sound/soc/bcm/hifiberry_digi.c
48 new file mode 100644
49 index 0000000..e4f769d
50 --- /dev/null
51 +++ b/sound/soc/bcm/hifiberry_digi.c
52 @@ -0,0 +1,153 @@
53 +/*
54 + * ASoC Driver for HifiBerry Digi
55 + *
56 + * Author: Daniel Matuschek <info@crazy-audio.com>
57 + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
58 + *     Copyright 2013
59 + *
60 + * This program is free software; you can redistribute it and/or
61 + * modify it under the terms of the GNU General Public License
62 + * version 2 as published by the Free Software Foundation.
63 + *
64 + * This program is distributed in the hope that it will be useful, but
65 + * WITHOUT ANY WARRANTY; without even the implied warranty of
66 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
67 + * General Public License for more details.
68 + */
69 +
70 +#include <linux/module.h>
71 +#include <linux/platform_device.h>
72 +
73 +#include <sound/core.h>
74 +#include <sound/pcm.h>
75 +#include <sound/pcm_params.h>
76 +#include <sound/soc.h>
77 +#include <sound/jack.h>
78 +
79 +#include "../codecs/wm8804.h"
80 +
81 +static int samplerate=44100;
82 +
83 +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
84 +{
85 +       struct snd_soc_codec *codec = rtd->codec;
86 +
87 +       /* enable TX output */
88 +       snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
89 +
90 +       return 0;
91 +}
92 +
93 +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
94 +                                      struct snd_pcm_hw_params *params)
95 +{
96 +       struct snd_soc_pcm_runtime *rtd = substream->private_data;
97 +       struct snd_soc_dai *codec_dai = rtd->codec_dai;
98 +       struct snd_soc_codec *codec = rtd->codec;
99 +       struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
100 +
101 +       int sysclk = 27000000; /* This is fixed on this board */
102 +
103 +       long mclk_freq=0;
104 +       int mclk_div=1;
105 +
106 +       int ret;
107 +
108 +       samplerate = params_rate(params);
109 +
110 +       switch (samplerate) {
111 +               case 44100:
112 +               case 48000:
113 +               case 88200:
114 +               case 96000:
115 +                       mclk_freq=samplerate*256;
116 +                       mclk_div=WM8804_MCLKDIV_256FS;
117 +                       break;
118 +               case 176400:
119 +               case 192000:
120 +                       mclk_freq=samplerate*128;
121 +                       mclk_div=WM8804_MCLKDIV_128FS;
122 +                       break;
123 +               default:
124 +                       dev_err(substream->pcm->dev,
125 +                       "Failed to set WM8804 SYSCLK, unsupported samplerate\n");
126 +       }
127 +
128 +       snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
129 +       snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
130 +
131 +       ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
132 +                                       sysclk, SND_SOC_CLOCK_OUT);
133 +       if (ret < 0) {
134 +               dev_err(substream->pcm->dev,
135 +               "Failed to set WM8804 SYSCLK: %d\n", ret);
136 +               return ret;
137 +       }
138 +
139 +       /* Enable TX output */
140 +       snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
141 +
142 +       /* Power on */
143 +       snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
144 +
145 +       return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
146 +}
147 +
148 +/* machine stream operations */
149 +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
150 +       .hw_params = snd_rpi_hifiberry_digi_hw_params,
151 +};
152 +
153 +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
154 +{
155 +       .name           = "HifiBerry Digi",
156 +       .stream_name    = "HifiBerry Digi HiFi",
157 +       .cpu_dai_name   = "bcm2708-i2s.0",
158 +       .codec_dai_name = "wm8804-spdif",
159 +       .platform_name  = "bcm2708-i2s.0",
160 +       .codec_name     = "wm8804.1-003b",
161 +       .dai_fmt        = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
162 +                               SND_SOC_DAIFMT_CBM_CFM,
163 +       .ops            = &snd_rpi_hifiberry_digi_ops,
164 +       .init           = snd_rpi_hifiberry_digi_init,
165 +},
166 +};
167 +
168 +/* audio machine driver */
169 +static struct snd_soc_card snd_rpi_hifiberry_digi = {
170 +       .name         = "snd_rpi_hifiberry_digi",
171 +       .dai_link     = snd_rpi_hifiberry_digi_dai,
172 +       .num_links    = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
173 +};
174 +
175 +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
176 +{
177 +       int ret = 0;
178 +
179 +       snd_rpi_hifiberry_digi.dev = &pdev->dev;
180 +       ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
181 +       if (ret)
182 +               dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
183 +
184 +       return ret;
185 +}
186 +
187 +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
188 +{
189 +       return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
190 +}
191 +
192 +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
193 +       .driver = {
194 +               .name   = "snd-hifiberry-digi",
195 +               .owner  = THIS_MODULE,
196 +       },
197 +       .probe          = snd_rpi_hifiberry_digi_probe,
198 +       .remove         = snd_rpi_hifiberry_digi_remove,
199 +};
200 +
201 +module_platform_driver(snd_rpi_hifiberry_digi_driver);
202 +
203 +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
204 +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
205 +MODULE_LICENSE("GPL v2");
206 -- 
207 1.9.1
208