1 /*
2 * Rockchip machine ASoC driver for Rockchip built-in HDMI and external codec IC
3 * which use the same i2s interface
4 *
5 * Copyright (C) 2017 Fuzhou Rockchip Electronics Co., Ltd
6 *
7 * Authors: Zhangjun <showy.zhang@rock-chips.com>,
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms and conditions of the GNU General Public License,
11 * version 2, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/soc-dapm.h>
31
32 #include "rockchip_i2s.h"
33
34 #define DRV_NAME "rk-hdmi-analog-sound"
35 #define MAX_CODECS 2
36
rk_hdmi_analog_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)37 static int rk_hdmi_analog_hw_params(struct snd_pcm_substream *substream,
38 struct snd_pcm_hw_params *params)
39 {
40 int ret = 0;
41 struct snd_soc_pcm_runtime *rtd = substream->private_data;
42 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
43 struct snd_soc_dai **codec_dais = rtd->codec_dais;
44 int mclk;
45 unsigned int i;
46
47 switch (params_rate(params)) {
48 case 8000:
49 case 16000:
50 case 24000:
51 case 32000:
52 case 48000:
53 case 64000:
54 case 96000:
55 mclk = 12288000;
56 break;
57 case 11025:
58 case 22050:
59 case 44100:
60 case 88200:
61 mclk = 11289600;
62 break;
63 case 176400:
64 mclk = 11289600 * 2;
65 break;
66 case 192000:
67 mclk = 12288000 * 2;
68 break;
69 default:
70 return -EINVAL;
71 }
72
73 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
74 SND_SOC_CLOCK_OUT);
75
76 if (ret && ret != -ENOTSUPP) {
77 dev_err(cpu_dai->dev, "Can't set cpu clock %d\n", ret);
78 return ret;
79 }
80
81 for (i = 0; i < rtd->num_codecs; i++) {
82 struct snd_soc_dai *codec_dai = codec_dais[i];
83
84 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
85 SND_SOC_CLOCK_IN);
86 if (ret && ret != -ENOTSUPP) {
87 dev_err(codec_dai->dev,
88 "Can't set codec clock %d\n", ret);
89 return ret;
90 }
91
92 }
93
94 return 0;
95 }
96
97 static struct snd_soc_ops rk_ops = {
98 .hw_params = rk_hdmi_analog_hw_params,
99 };
100
101 static struct snd_soc_dai_link rk_dailink = {
102 .name = "HDMI-ANALOG",
103 .stream_name = "HDMI-ANALOG",
104 .ops = &rk_ops,
105 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
106 SND_SOC_DAIFMT_CBS_CFS,
107 };
108
109 static struct snd_soc_card snd_soc_card_rk = {
110 .name = "rk-hdmi-analog-snd",
111 .dai_link = &rk_dailink,
112 .num_links = 1,
113 .num_aux_devs = 0,
114 };
115
rk_hdmi_analog_probe(struct platform_device * pdev)116 static int rk_hdmi_analog_probe(struct platform_device *pdev)
117 {
118 struct snd_soc_card *card = &snd_soc_card_rk;
119 struct device_node *np = pdev->dev.of_node;
120 struct snd_soc_dai_link *link = card->dai_link;
121 struct snd_soc_dai_link_component *codecs;
122 struct of_phandle_args args;
123 struct device_node *node;
124 int count;
125 int ret = 0, i = 0, idx = 0;
126
127 snd_soc_of_parse_card_name(card, "rockchip,model");
128
129 card->dev = &pdev->dev;
130 count = of_count_phandle_with_args(np, "rockchip,codec", NULL);
131 if (count < 0 || count > MAX_CODECS)
132 return -EINVAL;
133
134 /* refine codecs, remove unavailable node */
135 for (i = 0; i < count; i++) {
136 node = of_parse_phandle(np, "rockchip,codec", i);
137 if (!node)
138 return -ENODEV;
139 if (of_device_is_available(node))
140 idx++;
141 }
142
143 if (!idx)
144 return -ENODEV;
145
146 codecs = devm_kcalloc(&pdev->dev, idx,
147 sizeof(*codecs), GFP_KERNEL);
148 link->codecs = codecs;
149 link->num_codecs = idx;
150 idx = 0;
151 for (i = 0; i < count; i++) {
152 node = of_parse_phandle(np, "rockchip,codec", i);
153 if (!node)
154 return -ENODEV;
155 if (!of_device_is_available(node))
156 continue;
157
158 ret = of_parse_phandle_with_fixed_args(np, "rockchip,codec",
159 0, i, &args);
160 if (ret)
161 return ret;
162
163 codecs[idx].of_node = node;
164 ret = snd_soc_get_dai_name(&args, &codecs[idx].dai_name);
165 if (ret)
166 return ret;
167 idx++;
168 }
169
170 link->cpu_of_node = of_parse_phandle(np, "rockchip,cpu", 0);
171 if (!link->cpu_of_node)
172 return -ENODEV;
173
174 link->platform_of_node = link->cpu_of_node;
175
176 ret = snd_soc_of_parse_audio_simple_widgets(card, "rockchip,widgets");
177 if (ret) {
178 dev_err(&pdev->dev,
179 "Unable to parse 'rockchip,widget' property\n");
180 }
181
182 ret = snd_soc_of_parse_audio_routing(card, "rockchip,routing");
183 if (ret) {
184 dev_err(&pdev->dev,
185 "Unable to parse 'rockchip,routing' property\n");
186 }
187
188 ret = devm_snd_soc_register_card(&pdev->dev, card);
189 if (ret == -EPROBE_DEFER)
190 return -EPROBE_DEFER;
191 if (ret) {
192 dev_err(&pdev->dev, "card register failed %d\n", ret);
193 return ret;
194 }
195
196 platform_set_drvdata(pdev, card);
197
198 return ret;
199 }
200
201 static const struct of_device_id rockchip_sound_of_match[] = {
202 { .compatible = "rockchip,rk3368-hdmi-analog", },
203 {},
204 };
205
206 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
207
208 static struct platform_driver rockchip_sound_driver = {
209 .probe = rk_hdmi_analog_probe,
210 .driver = {
211 .name = DRV_NAME,
212 .pm = &snd_soc_pm_ops,
213 .of_match_table = rockchip_sound_of_match,
214 },
215 };
216
217 module_platform_driver(rockchip_sound_driver);
218
219 MODULE_AUTHOR("Zhangjun <showy.zhang@rock-chips.com>");
220 MODULE_DESCRIPTION("Rockchip Built-in HDMI and Codec IC machine ASoC driver");
221 MODULE_LICENSE("GPL v2");
222 MODULE_ALIAS("platform:" DRV_NAME);
223