2 * ASoC Driver for Dacberry400 soundcard
4 * Ashish Vara<ashishhvara@gmail.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20 #include <sound/core.h>
21 #include <sound/pcm.h>
22 #include <sound/pcm_params.h>
23 #include <sound/soc.h>
24 #include <sound/jack.h>
25 #include <linux/i2c.h>
26 #include <linux/acpi.h>
27 #include <linux/slab.h>
28 #include "../sound/soc/codecs/tlv320aic3x.h"
30 static const struct snd_kcontrol_new dacberry400_controls[] = {
31 SOC_DAPM_PIN_SWITCH("MIC Jack"),
32 SOC_DAPM_PIN_SWITCH("Line In"),
33 SOC_DAPM_PIN_SWITCH("Line Out"),
34 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
37 static const struct snd_soc_dapm_widget dacberry400_widgets[] = {
38 SND_SOC_DAPM_HP("Headphone Jack", NULL),
39 SND_SOC_DAPM_MIC("MIC Jack", NULL),
40 SND_SOC_DAPM_LINE("Line In", NULL),
41 SND_SOC_DAPM_LINE("Line Out", NULL),
44 static const struct snd_soc_dapm_route dacberry400_audio_map[] = {
45 {"Headphone Jack", NULL, "HPLOUT"},
46 {"Headphone Jack", NULL, "HPROUT"},
48 {"LINE1L", NULL, "Line In"},
49 {"LINE1R", NULL, "Line In"},
51 {"Line Out", NULL, "LLOUT"},
52 {"Line Out", NULL, "RLOUT"},
54 {"MIC3L", NULL, "MIC Jack"},
55 {"MIC3R", NULL, "MIC Jack"},
58 static int snd_rpi_dacberry400_init(struct snd_soc_pcm_runtime *rtd)
60 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
61 struct snd_soc_component *component = codec_dai->component;
64 ret = snd_soc_dai_set_sysclk(codec_dai, 2, 12000000,
67 if (ret && ret != -ENOTSUPP)
70 snd_soc_component_write(component, HPRCOM_CFG, 0x20);
71 snd_soc_component_write(component, DACL1_2_HPLOUT_VOL, 0x80);
72 snd_soc_component_write(component, DACR1_2_HPROUT_VOL, 0x80);
77 static int snd_rpi_dacberry400_set_bias_level(struct snd_soc_card *card,
78 struct snd_soc_dapm_context *dapm, enum snd_soc_bias_level level)
80 struct snd_soc_pcm_runtime *rtd;
81 struct snd_soc_dai *codec_dai;
82 struct snd_soc_component *component;
83 struct dacberry_priv *aic3x;
86 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
87 codec_dai = asoc_rtd_to_codec(rtd, 0);
88 component = codec_dai->component;
89 aic3x = snd_soc_component_get_drvdata(component);
90 if (dapm->dev != codec_dai->dev)
94 case SND_SOC_BIAS_PREPARE:
95 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
98 hpcom_reg = snd_soc_component_read(component, HPLCOM_CFG);
99 snd_soc_component_write(component, HPLCOM_CFG, hpcom_reg | 0x20);
100 snd_soc_component_write(component, LINE1R_2_RADC_CTRL, 0x04);
101 snd_soc_component_write(component, LINE1L_2_LADC_CTRL, 0x04);
102 snd_soc_component_write(component, LADC_VOL, 0x00);
103 snd_soc_component_write(component, RADC_VOL, 0x00);
104 pr_info("%s: unmute ADC/DAC\n", __func__);
107 case SND_SOC_BIAS_STANDBY:
108 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
111 snd_soc_component_write(component, LDAC_VOL, 0x80);
112 snd_soc_component_write(component, RDAC_VOL, 0x80);
113 snd_soc_component_write(component, LADC_VOL, 0x80);
114 snd_soc_component_write(component, RADC_VOL, 0x80);
115 snd_soc_component_write(component, LINE1R_2_RADC_CTRL, 0x00);
116 snd_soc_component_write(component, LINE1L_2_LADC_CTRL, 0x00);
117 snd_soc_component_write(component, HPLCOM_CFG, 0x00);
118 pr_info("%s: mute ADC/DAC\n", __func__);
127 static int snd_rpi_dacberry400_hw_params(struct snd_pcm_substream *substream,
128 struct snd_pcm_hw_params *params)
132 struct snd_soc_pcm_runtime *rtd = substream->private_data;
133 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
134 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
135 struct snd_soc_component *component = codec_dai->component;
136 int fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
137 int channels = params_channels(params);
141 data = (LDAC2LCH | RDAC2RCH);
142 data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
143 if (params_rate(params) >= 64000)
144 data |= DUAL_RATE_MODE;
145 ret = snd_soc_component_write(component, 0x7, data);
146 width = params_width(params);
148 clock = snd_soc_component_read(component, 2);
150 ret = snd_soc_dai_set_bclk_ratio(cpu_dai, channels*width);
155 static const struct snd_soc_ops snd_rpi_dacberry400_ops = {
156 .hw_params = snd_rpi_dacberry400_hw_params,
160 SND_SOC_DAILINK_DEFS(rpi_dacberry400,
161 DAILINK_COMP_ARRAY(COMP_CPU("bcm2835-i2s.0")),
162 DAILINK_COMP_ARRAY(COMP_CODEC("tlv320aic3x.1-0018", "tlv320aic3x-hifi")),
163 DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
165 static struct snd_soc_dai_link snd_rpi_dacberry400_dai[] = {
167 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
168 SND_SOC_DAIFMT_CBS_CFS,
169 .init = snd_rpi_dacberry400_init,
170 .ops = &snd_rpi_dacberry400_ops,
172 SND_SOC_DAILINK_REG(rpi_dacberry400),
176 static struct snd_soc_card snd_rpi_dacberry400 = {
177 .owner = THIS_MODULE,
178 .dai_link = snd_rpi_dacberry400_dai,
179 .num_links = ARRAY_SIZE(snd_rpi_dacberry400_dai),
180 .controls = dacberry400_controls,
181 .num_controls = ARRAY_SIZE(dacberry400_controls),
182 .dapm_widgets = dacberry400_widgets,
183 .num_dapm_widgets = ARRAY_SIZE(dacberry400_widgets),
184 .dapm_routes = dacberry400_audio_map,
185 .num_dapm_routes = ARRAY_SIZE(dacberry400_audio_map),
186 .set_bias_level = snd_rpi_dacberry400_set_bias_level,
189 static int snd_rpi_dacberry400_probe(struct platform_device *pdev)
193 snd_rpi_dacberry400.dev = &pdev->dev;
195 if (pdev->dev.of_node) {
196 struct device_node *i2s_node;
197 struct snd_soc_card *card = &snd_rpi_dacberry400;
198 struct snd_soc_dai_link *dai = &snd_rpi_dacberry400_dai[0];
200 i2s_node = of_parse_phandle(pdev->dev.of_node,
201 "i2s-controller", 0);
203 dai->cpus->dai_name = NULL;
204 dai->cpus->of_node = i2s_node;
205 dai->platforms->name = NULL;
206 dai->platforms->of_node = i2s_node;
207 of_node_put(i2s_node);
210 if (of_property_read_string(pdev->dev.of_node, "card_name",
212 card->name = "tlvaudioCODEC";
214 if (of_property_read_string(pdev->dev.of_node, "dai_name",
216 dai->name = "tlvaudio CODEC";
220 ret = snd_soc_register_card(&snd_rpi_dacberry400);
222 if (ret != -EPROBE_DEFER)
224 "snd_soc_register_card() failed: %d\n", ret);
231 static int snd_rpi_dacberry400_remove(struct platform_device *pdev)
233 return snd_soc_unregister_card(&snd_rpi_dacberry400);
236 static const struct of_device_id dacberry400_match_id[] = {
237 { .compatible = "osaelectronics,dacberry400",},
240 MODULE_DEVICE_TABLE(of, dacberry400_match_id);
242 static struct platform_driver snd_rpi_dacberry400_driver = {
244 .name = "snd-rpi-dacberry400",
245 .owner = THIS_MODULE,
246 .of_match_table = dacberry400_match_id,
248 .probe = snd_rpi_dacberry400_probe,
249 .remove = snd_rpi_dacberry400_remove,
252 module_platform_driver(snd_rpi_dacberry400_driver);
254 MODULE_AUTHOR("Ashish Vara");
255 MODULE_DESCRIPTION("Dacberry400 sound card driver");
256 MODULE_LICENSE("GPL");
257 MODULE_ALIAS("platform:dacberry400");
258 MODULE_SOFTDEP("pre: snd-soc-tlv320aic3x");