2 * ASoC Driver for AudioInjector Pi add on soundcard
4 * Created on: 13-May-2016
5 * Author: flatmax@flatmax.org
6 * based on code by Cliff Cai <Cliff.Cai@analog.com> for the ssm2602 machine blackfin.
7 * with help from Lars-Peter Clausen for simplifying the original code to use the dai_fmt field.
8 * i2s_node code taken from the other sound/soc/bcm machine drivers.
10 * Copyright (C) 2016 Flatmax Pty. Ltd.
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
22 #include <linux/module.h>
23 #include <linux/types.h>
25 #include <sound/core.h>
26 #include <sound/soc.h>
27 #include <sound/pcm_params.h>
28 #include <sound/control.h>
30 #include "../codecs/wm8731.h"
32 static const unsigned int bcm2835_rates_12000000[] = {
33 8000, 16000, 32000, 44100, 48000, 96000, 88200,
36 static struct snd_pcm_hw_constraint_list bcm2835_constraints_12000000 = {
37 .list = bcm2835_rates_12000000,
38 .count = ARRAY_SIZE(bcm2835_rates_12000000),
41 static int snd_audioinjector_pi_soundcard_startup(struct snd_pcm_substream *substream)
43 /* Setup constraints, because there is a 12 MHz XTAL on the board */
44 snd_pcm_hw_constraint_list(substream->runtime, 0,
45 SNDRV_PCM_HW_PARAM_RATE,
46 &bcm2835_constraints_12000000);
50 static int snd_audioinjector_pi_soundcard_hw_params(struct snd_pcm_substream *substream,
51 struct snd_pcm_hw_params *params)
53 struct snd_soc_pcm_runtime *rtd = substream->private_data;
54 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
56 switch (params_rate(params)){
58 return snd_soc_dai_set_bclk_ratio(cpu_dai, 1);
60 return snd_soc_dai_set_bclk_ratio(cpu_dai, 750);
62 return snd_soc_dai_set_bclk_ratio(cpu_dai, 375);
64 return snd_soc_dai_set_bclk_ratio(cpu_dai, 272);
66 return snd_soc_dai_set_bclk_ratio(cpu_dai, 250);
68 return snd_soc_dai_set_bclk_ratio(cpu_dai, 136);
70 return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
72 return snd_soc_dai_set_bclk_ratio(cpu_dai, 125);
76 /* machine stream operations */
77 static struct snd_soc_ops snd_audioinjector_pi_soundcard_ops = {
78 .startup = snd_audioinjector_pi_soundcard_startup,
79 .hw_params = snd_audioinjector_pi_soundcard_hw_params,
82 static int audioinjector_pi_soundcard_dai_init(struct snd_soc_pcm_runtime *rtd)
84 return snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0), WM8731_SYSCLK_XTAL, 12000000, SND_SOC_CLOCK_IN);
87 SND_SOC_DAILINK_DEFS(audioinjector_pi,
88 DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
89 DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.1-001a", "wm8731-hifi")),
90 DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
92 static struct snd_soc_dai_link audioinjector_pi_soundcard_dai[] = {
94 .name = "AudioInjector audio",
95 .stream_name = "AudioInjector audio",
96 .ops = &snd_audioinjector_pi_soundcard_ops,
97 .init = audioinjector_pi_soundcard_dai_init,
98 .dai_fmt = SND_SOC_DAIFMT_CBM_CFM|SND_SOC_DAIFMT_I2S|SND_SOC_DAIFMT_NB_NF,
99 SND_SOC_DAILINK_REG(audioinjector_pi),
103 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
104 SND_SOC_DAPM_HP("Headphone Jack", NULL),
105 SND_SOC_DAPM_SPK("Ext Spk", NULL),
106 SND_SOC_DAPM_LINE("Line In Jacks", NULL),
107 SND_SOC_DAPM_MIC("Microphone", NULL),
110 static const struct snd_soc_dapm_route audioinjector_audio_map[] = {
111 /* headphone connected to LHPOUT, RHPOUT */
112 {"Headphone Jack", NULL, "LHPOUT"},
113 {"Headphone Jack", NULL, "RHPOUT"},
115 /* speaker connected to LOUT, ROUT */
116 {"Ext Spk", NULL, "ROUT"},
117 {"Ext Spk", NULL, "LOUT"},
120 {"Line In Jacks", NULL, "Line Input"},
122 /* mic is connected to Mic Jack, with WM8731 Mic Bias */
123 {"Microphone", NULL, "Mic Bias"},
126 static struct snd_soc_card snd_soc_audioinjector = {
127 .name = "audioinjector-pi-soundcard",
128 .dai_link = audioinjector_pi_soundcard_dai,
129 .num_links = ARRAY_SIZE(audioinjector_pi_soundcard_dai),
131 .dapm_widgets = wm8731_dapm_widgets,
132 .num_dapm_widgets = ARRAY_SIZE(wm8731_dapm_widgets),
133 .dapm_routes = audioinjector_audio_map,
134 .num_dapm_routes = ARRAY_SIZE(audioinjector_audio_map),
137 static int audioinjector_pi_soundcard_probe(struct platform_device *pdev)
139 struct snd_soc_card *card = &snd_soc_audioinjector;
142 card->dev = &pdev->dev;
144 if (pdev->dev.of_node) {
145 struct snd_soc_dai_link *dai = &audioinjector_pi_soundcard_dai[0];
146 struct device_node *i2s_node = of_parse_phandle(pdev->dev.of_node,
147 "i2s-controller", 0);
150 dai->cpus->dai_name = NULL;
151 dai->cpus->of_node = i2s_node;
152 dai->platforms->name = NULL;
153 dai->platforms->of_node = i2s_node;
155 if (!dai->cpus->of_node) {
156 dev_err(&pdev->dev, "Property 'i2s-controller' missing or invalid\n");
161 if ((ret = devm_snd_soc_register_card(&pdev->dev, card)))
162 return dev_err_probe(&pdev->dev, ret, "%s\n", __func__);
164 dev_info(&pdev->dev, "successfully loaded\n");
169 static const struct of_device_id audioinjector_pi_soundcard_of_match[] = {
170 { .compatible = "ai,audioinjector-pi-soundcard", },
173 MODULE_DEVICE_TABLE(of, audioinjector_pi_soundcard_of_match);
175 static struct platform_driver audioinjector_pi_soundcard_driver = {
177 .name = "audioinjector-stereo",
178 .owner = THIS_MODULE,
179 .of_match_table = audioinjector_pi_soundcard_of_match,
181 .probe = audioinjector_pi_soundcard_probe,
184 module_platform_driver(audioinjector_pi_soundcard_driver);
185 MODULE_AUTHOR("Matt Flax <flatmax@flatmax.org>");
186 MODULE_DESCRIPTION("AudioInjector.net Pi Soundcard");
187 MODULE_LICENSE("GPL v2");
188 MODULE_ALIAS("platform:audioinjector-pi-soundcard");