1 /*
2 * Rockchip machine ASoC driver for boards using a DA7219 CODEC.
3 *
4 * Copyright (c) 2016, ROCKCHIP CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/gpio.h>
24 #include <linux/of_gpio.h>
25 #include <linux/delay.h>
26 #include <sound/core.h>
27 #include <sound/jack.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include "rockchip_i2s.h"
32 #include "../codecs/da7219.h"
33 #include "../codecs/da7219-aad.h"
34
35 #define DRV_NAME "rockchip-snd-da7219"
36
37 static struct snd_soc_jack mach_da7219_jack;
38
39 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
40 SND_SOC_DAPM_HP("Headphones", NULL),
41 SND_SOC_DAPM_SPK("Speakers", NULL),
42 SND_SOC_DAPM_MIC("Headset Mic", NULL),
43 SND_SOC_DAPM_MIC("Int Mic", NULL),
44 };
45
46 static const struct snd_soc_dapm_route rk_audio_map[] = {
47 /* Input Lines */
48 {"MIC", NULL, "Headset Mic"},
49
50 /* Output Lines */
51 {"Headphones", NULL, "HPL"},
52 {"Headphones", NULL, "HPR"},
53 };
54
55 static const struct snd_kcontrol_new rk_mc_controls[] = {
56 SOC_DAPM_PIN_SWITCH("Headphones"),
57 SOC_DAPM_PIN_SWITCH("Speakers"),
58 SOC_DAPM_PIN_SWITCH("Headset Mic"),
59 SOC_DAPM_PIN_SWITCH("Int Mic"),
60 };
61
rk_aif1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)62 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
63 struct snd_pcm_hw_params *params)
64 {
65 struct snd_soc_pcm_runtime *rtd = substream->private_data;
66 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
67 struct snd_soc_dai *codec_dai = rtd->codec_dai;
68 int mclk;
69 int ret = 0;
70
71 switch (params_rate(params)) {
72 case 8000:
73 case 16000:
74 case 32000:
75 case 48000:
76 case 96000:
77 mclk = 12288000;
78 break;
79 case 11025:
80 case 22050:
81 case 44100:
82 mclk = 11289600;
83 break;
84 default:
85 return -EINVAL;
86 }
87
88 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
89 SND_SOC_CLOCK_OUT);
90 if (ret < 0) {
91 dev_err(codec_dai->dev, "Can't set cpu clock out %d\n", ret);
92 return ret;
93 }
94
95 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
96 SND_SOC_CLOCK_IN);
97 if (ret < 0) {
98 dev_err(codec_dai->dev, "Can't set codec clock in %d\n", ret);
99 return ret;
100 }
101
102 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
103 if (ret < 0) {
104 dev_err(codec_dai->dev, "Can't set pll sysclk mclk %d\n", ret);
105 return ret;
106 }
107
108 return ret;
109 }
110
rk_init(struct snd_soc_pcm_runtime * runtime)111 static int rk_init(struct snd_soc_pcm_runtime *runtime)
112 {
113 struct snd_soc_card *card = runtime->card;
114 struct snd_soc_dapm_context *dapm = &card->dapm;
115 int ret = 0;
116
117 card->dapm.idle_bias_off = true;
118
119 snd_soc_dapm_enable_pin(dapm, "Headphones");
120 snd_soc_dapm_enable_pin(dapm, "Speakers");
121 snd_soc_dapm_enable_pin(dapm, "Headset Mic");
122 snd_soc_dapm_enable_pin(dapm, "Int Mic");
123
124 snd_soc_dapm_sync(dapm);
125
126 /* Enable Headset and 4 Buttons Jack detection */
127 ret = snd_soc_card_jack_new(card, "Headset Jack",
128 SND_JACK_HEADSET |
129 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
130 SND_JACK_BTN_2 | SND_JACK_BTN_3,
131 &mach_da7219_jack, NULL, 0);
132
133 if (ret) {
134 dev_err(card->dev, "New Headset Jack failed! (%d)\n", ret);
135 return ret;
136 }
137
138 da7219_aad_jack_det(runtime->codec, &mach_da7219_jack);
139
140 return ret;
141 }
142
143 static struct snd_soc_ops rk_aif1_ops = {
144 .hw_params = rk_aif1_hw_params,
145 };
146
147 static struct snd_soc_dai_link rk_dailink = {
148 .name = "da7219",
149 .stream_name = "da7219 PCM",
150 .codec_dai_name = "da7219-hifi",
151 .init = rk_init,
152 .ops = &rk_aif1_ops,
153 /* set da7219 as slave */
154 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
155 SND_SOC_DAIFMT_CBS_CFS,
156 };
157
158 static struct snd_soc_card snd_soc_card_rk = {
159 .name = "audio-da7219",
160 .dai_link = &rk_dailink,
161 .num_links = 1,
162 .dapm_widgets = rk_dapm_widgets,
163 .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
164 .dapm_routes = rk_audio_map,
165 .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
166 .controls = rk_mc_controls,
167 .num_controls = ARRAY_SIZE(rk_mc_controls),
168 };
169
snd_rk_mc_probe(struct platform_device * pdev)170 static int snd_rk_mc_probe(struct platform_device *pdev)
171 {
172 int ret = 0;
173 struct snd_soc_card *card = &snd_soc_card_rk;
174 struct device_node *np = pdev->dev.of_node;
175
176 /* register the soc card */
177 card->dev = &pdev->dev;
178 platform_set_drvdata(pdev, card);
179
180 rk_dailink.codec_of_node = of_parse_phandle(np,
181 "rockchip,audio-codec", 0);
182 if (!rk_dailink.codec_of_node) {
183 dev_err(&pdev->dev,
184 "Property 'rockchip,audio-codec' missing or invalid\n");
185 return -EINVAL;
186 }
187
188 rk_dailink.cpu_of_node = of_parse_phandle(np,
189 "rockchip,i2s-controller", 0);
190 if (!rk_dailink.cpu_of_node) {
191 dev_err(&pdev->dev,
192 "Property 'rockchip,i2s-controller' missing or invalid\n");
193 return -EINVAL;
194 }
195
196 rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
197
198 ret = snd_soc_of_parse_card_name(card, "rockchip,model");
199 if (ret) {
200 dev_err(&pdev->dev,
201 "snd_soc_of_parse_card_name failed %d\n", ret);
202 return ret;
203 }
204
205 ret = devm_snd_soc_register_card(&pdev->dev, card);
206 if (ret) {
207 dev_err(&pdev->dev,
208 "snd_soc_register_card failed %d\n", ret);
209 return ret;
210 }
211
212 return ret;
213 }
214
215 static const struct of_device_id rockchip_da7219_of_match[] = {
216 { .compatible = "rockchip,rockchip-audio-da7219", },
217 {},
218 };
219
220 static struct platform_driver snd_rk_mc_driver = {
221 .probe = snd_rk_mc_probe,
222 .driver = {
223 .name = DRV_NAME,
224 .pm = &snd_soc_pm_ops,
225 .of_match_table = rockchip_da7219_of_match,
226 },
227 };
228
229 module_platform_driver(snd_rk_mc_driver);
230
231 MODULE_AUTHOR("Xing Zheng <zhengxing@rock-chips.com>");
232 MODULE_DESCRIPTION("Rockchip da7219 machine ASoC driver");
233 MODULE_LICENSE("GPL v2");
234 MODULE_ALIAS("platform:" DRV_NAME);
235 MODULE_DEVICE_TABLE(of, rockchip_da7219_of_match);
236