1 /*
2  * Rockchip machine ASoC driver for RK3288 boards that have an HDMI and analog
3  * audio output
4  *
5  * Copyright (c) 2016, Collabora Ltd.
6  *
7  * Authors: Sjoerd Simons <sjoerd.simons@collabora.com>,
8  *	    Romain Perier <romain.perier@collabora.com>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms and conditions of the GNU General Public License,
12  * version 2, as published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17  * more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/of_gpio.h>
29 #include <sound/core.h>
30 #include <sound/jack.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 
36 #include "rockchip_i2s.h"
37 
38 #define DRV_NAME "rk3288-snd-hdmi-analog"
39 
40 struct rk_drvdata {
41 	int gpio_hp_en;
42 	int gpio_hp_det;
43 };
44 
45 static int rk_hp_power(struct snd_soc_dapm_widget *w,
46 		       struct snd_kcontrol *k, int event)
47 {
48 	struct rk_drvdata *machine = snd_soc_card_get_drvdata(w->dapm->card);
49 
50 	if (!gpio_is_valid(machine->gpio_hp_en))
51 		return 0;
52 
53 	gpio_set_value_cansleep(machine->gpio_hp_en,
54 				SND_SOC_DAPM_EVENT_ON(event));
55 
56 	return 0;
57 }
58 
59 static struct snd_soc_jack headphone_jack;
60 static struct snd_soc_jack_pin headphone_jack_pins[] = {
61 	{
62 		.pin = "Analog",
63 		.mask = SND_JACK_HEADPHONE
64 	},
65 };
66 
67 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
68 	SND_SOC_DAPM_HP("Analog", rk_hp_power),
69 	SND_SOC_DAPM_LINE("HDMI", NULL),
70 };
71 
72 static const struct snd_kcontrol_new rk_mc_controls[] = {
73 	SOC_DAPM_PIN_SWITCH("Analog"),
74 	SOC_DAPM_PIN_SWITCH("HDMI"),
75 };
76 
77 static int rk_hw_params(struct snd_pcm_substream *substream,
78 			struct snd_pcm_hw_params *params)
79 {
80 	int ret = 0;
81 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
82 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
83 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
84 	int mclk;
85 
86 	switch (params_rate(params)) {
87 	case 8000:
88 	case 16000:
89 	case 24000:
90 	case 32000:
91 	case 48000:
92 	case 64000:
93 	case 96000:
94 		mclk = 12288000;
95 		break;
96 	case 192000:
97 		mclk = 24576000;
98 		break;
99 	case 11025:
100 	case 22050:
101 	case 44100:
102 	case 88200:
103 		mclk = 11289600;
104 		break;
105 	default:
106 		return -EINVAL;
107 	}
108 
109 	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
110 				     SND_SOC_CLOCK_OUT);
111 
112 	if (ret && ret != -ENOTSUPP) {
113 		dev_err(codec_dai->dev, "Can't set cpu clock %d\n", ret);
114 		return ret;
115 	}
116 
117 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
118 				     SND_SOC_CLOCK_IN);
119 	if (ret && ret != -ENOTSUPP) {
120 		dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
121 		return ret;
122 	}
123 
124 	return 0;
125 }
126 
127 static struct snd_soc_jack_gpio rk_hp_jack_gpio = {
128 	.name = "Headphone detection",
129 	.report = SND_JACK_HEADPHONE,
130 	.debounce_time = 150
131 };
132 
133 static int rk_init(struct snd_soc_pcm_runtime *runtime)
134 {
135 	struct rk_drvdata *machine = snd_soc_card_get_drvdata(runtime->card);
136 
137 	/* Enable Headset Jack detection */
138 	if (gpio_is_valid(machine->gpio_hp_det)) {
139 		snd_soc_card_jack_new(runtime->card, "Headphone Jack",
140 				      SND_JACK_HEADPHONE, &headphone_jack,
141 				      headphone_jack_pins,
142 				      ARRAY_SIZE(headphone_jack_pins));
143 		rk_hp_jack_gpio.gpio = machine->gpio_hp_det;
144 		snd_soc_jack_add_gpios(&headphone_jack, 1, &rk_hp_jack_gpio);
145 	}
146 
147 	return 0;
148 }
149 
150 static const struct snd_soc_ops rk_ops = {
151 	.hw_params = rk_hw_params,
152 };
153 
154 static struct snd_soc_dai_link_component rk_codecs[] = {
155 	{ },
156 	{
157 		.name = "hdmi-audio-codec.2.auto",
158 		.dai_name = "i2s-hifi",
159 	},
160 };
161 
162 static struct snd_soc_dai_link rk_dailink = {
163 	.name = "Codecs",
164 	.stream_name = "Audio",
165 	.init = rk_init,
166 	.ops = &rk_ops,
167 	.codecs = rk_codecs,
168 	.num_codecs = ARRAY_SIZE(rk_codecs),
169 	/* Set codecs as slave */
170 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
171 		SND_SOC_DAIFMT_CBS_CFS,
172 };
173 
174 static struct snd_soc_card snd_soc_card_rk = {
175 	.name = "ROCKCHIP-I2S",
176 	.dai_link = &rk_dailink,
177 	.num_links = 1,
178 	.num_aux_devs = 0,
179 	.dapm_widgets = rk_dapm_widgets,
180 	.num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
181 	.controls = rk_mc_controls,
182 	.num_controls = ARRAY_SIZE(rk_mc_controls),
183 };
184 
185 static int snd_rk_mc_probe(struct platform_device *pdev)
186 {
187 	int ret = 0;
188 	struct snd_soc_card *card = &snd_soc_card_rk;
189 	struct device_node *np = pdev->dev.of_node;
190 	struct rk_drvdata *machine;
191 	struct of_phandle_args args;
192 
193 	machine = devm_kzalloc(&pdev->dev, sizeof(struct rk_drvdata),
194 			       GFP_KERNEL);
195 	if (!machine)
196 		return -ENOMEM;
197 
198 	card->dev = &pdev->dev;
199 
200 	machine->gpio_hp_det = of_get_named_gpio(np,
201 		"rockchip,hp-det-gpios", 0);
202 	if (!gpio_is_valid(machine->gpio_hp_det) && machine->gpio_hp_det != -ENODEV)
203 		return machine->gpio_hp_det;
204 
205 	machine->gpio_hp_en = of_get_named_gpio(np,
206 		"rockchip,hp-en-gpios", 0);
207 	if (!gpio_is_valid(machine->gpio_hp_en) && machine->gpio_hp_en != -ENODEV)
208 		return machine->gpio_hp_en;
209 
210 	if (gpio_is_valid(machine->gpio_hp_en)) {
211 		ret = devm_gpio_request_one(&pdev->dev, machine->gpio_hp_en,
212 					    GPIOF_OUT_INIT_LOW, "hp_en");
213 		if (ret) {
214 			dev_err(card->dev, "cannot get hp_en gpio\n");
215 			return ret;
216 		}
217 	}
218 
219 	ret = snd_soc_of_parse_card_name(card, "rockchip,model");
220 	if (ret) {
221 		dev_err(card->dev, "SoC parse card name failed %d\n", ret);
222 		return ret;
223 	}
224 
225 	rk_dailink.codecs[0].of_node = of_parse_phandle(np,
226 							"rockchip,audio-codec",
227 							0);
228 	if (!rk_dailink.codecs[0].of_node) {
229 		dev_err(&pdev->dev,
230 			"Property 'rockchip,audio-codec' missing or invalid\n");
231 		return -EINVAL;
232 	}
233 	ret = of_parse_phandle_with_fixed_args(np, "rockchip,audio-codec",
234 					       0, 0, &args);
235 	if (ret) {
236 		dev_err(&pdev->dev,
237 			"Unable to parse property 'rockchip,audio-codec'\n");
238 		return ret;
239 	}
240 
241 	ret = snd_soc_get_dai_name(&args, &rk_dailink.codecs[0].dai_name);
242 	if (ret) {
243 		dev_err(&pdev->dev, "Unable to get codec_dai_name\n");
244 		return ret;
245 	}
246 
247 	rk_dailink.cpu_of_node = of_parse_phandle(np, "rockchip,i2s-controller",
248 						  0);
249 	if (!rk_dailink.cpu_of_node) {
250 		dev_err(&pdev->dev,
251 			"Property 'rockchip,i2s-controller' missing or invalid\n");
252 		return -EINVAL;
253 	}
254 
255 	rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
256 
257 	ret = snd_soc_of_parse_audio_routing(card, "rockchip,routing");
258 	if (ret) {
259 		dev_err(&pdev->dev,
260 			"Unable to parse 'rockchip,routing' property\n");
261 		return ret;
262 	}
263 
264 	snd_soc_card_set_drvdata(card, machine);
265 
266 	ret = devm_snd_soc_register_card(&pdev->dev, card);
267 	if (ret == -EPROBE_DEFER)
268 		return -EPROBE_DEFER;
269 	if (ret) {
270 		dev_err(&pdev->dev,
271 			"Soc register card failed %d\n", ret);
272 		return ret;
273 	}
274 
275 	return ret;
276 }
277 
278 static const struct of_device_id rockchip_sound_of_match[] = {
279 	{ .compatible = "rockchip,rk3288-hdmi-analog", },
280 	{},
281 };
282 
283 MODULE_DEVICE_TABLE(of, rockchip_sound_of_match);
284 
285 static struct platform_driver rockchip_sound_driver = {
286 	.probe = snd_rk_mc_probe,
287 	.driver = {
288 		.name = DRV_NAME,
289 		.pm = &snd_soc_pm_ops,
290 		.of_match_table = rockchip_sound_of_match,
291 	},
292 };
293 
294 module_platform_driver(rockchip_sound_driver);
295 
296 MODULE_AUTHOR("Sjoerd Simons <sjoerd.simons@collabora.com>");
297 MODULE_DESCRIPTION("Rockchip RK3288 machine ASoC driver");
298 MODULE_LICENSE("GPL v2");
299 MODULE_ALIAS("platform:" DRV_NAME);
300