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