xref: /openbmc/linux/sound/soc/samsung/odroid.c (revision 55eafeb5)
1 /*
2  * Copyright (C) 2017 Samsung Electronics Co., Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/module.h>
13 #include <sound/soc.h>
14 #include <sound/pcm_params.h>
15 #include "i2s.h"
16 #include "i2s-regs.h"
17 
18 struct odroid_priv {
19 	struct snd_soc_card card;
20 	struct snd_soc_dai_link dai_link;
21 
22 	struct clk *clk_i2s_bus;
23 	struct clk *sclk_i2s;
24 };
25 
26 static int odroid_card_startup(struct snd_pcm_substream *substream)
27 {
28 	struct snd_pcm_runtime *runtime = substream->runtime;
29 
30 	snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, 2);
31 	return 0;
32 }
33 
34 static int odroid_card_hw_params(struct snd_pcm_substream *substream,
35 				      struct snd_pcm_hw_params *params)
36 {
37 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
38 	struct odroid_priv *priv = snd_soc_card_get_drvdata(rtd->card);
39 	unsigned int pll_freq, rclk_freq;
40 	int ret;
41 
42 	switch (params_rate(params)) {
43 	case 32000:
44 	case 64000:
45 		pll_freq = 131072006U;
46 		break;
47 	case 44100:
48 	case 88200:
49 	case 176400:
50 		pll_freq = 180633609U;
51 		break;
52 	case 48000:
53 	case 96000:
54 	case 192000:
55 		pll_freq = 196608001U;
56 		break;
57 	default:
58 		return -EINVAL;
59 	}
60 
61 	ret = clk_set_rate(priv->clk_i2s_bus, pll_freq / 2 + 1);
62 	if (ret < 0)
63 		return ret;
64 
65 	/*
66 	 *  We add 1 to the rclk_freq value in order to avoid too low clock
67 	 *  frequency values due to the EPLL output frequency not being exact
68 	 *  multiple of the audio sampling rate.
69 	 */
70 	rclk_freq = params_rate(params) * 256 + 1;
71 
72 	ret = clk_set_rate(priv->sclk_i2s, rclk_freq);
73 	if (ret < 0)
74 		return ret;
75 
76 	if (rtd->num_codecs > 1) {
77 		struct snd_soc_dai *codec_dai = rtd->codec_dais[1];
78 
79 		ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
80 					     SND_SOC_CLOCK_IN);
81 		if (ret < 0)
82 			return ret;
83 	}
84 
85 	return 0;
86 }
87 
88 static const struct snd_soc_ops odroid_card_ops = {
89 	.startup = odroid_card_startup,
90 	.hw_params = odroid_card_hw_params,
91 };
92 
93 static void odroid_put_codec_of_nodes(struct snd_soc_dai_link *link)
94 {
95 	struct snd_soc_dai_link_component *component = link->codecs;
96 	int i;
97 
98 	for (i = 0; i < link->num_codecs; i++, component++) {
99 		if (!component->of_node)
100 			break;
101 		of_node_put(component->of_node);
102 	}
103 }
104 
105 static int odroid_audio_probe(struct platform_device *pdev)
106 {
107 	struct device *dev = &pdev->dev;
108 	struct device_node *cpu, *codec;
109 	struct odroid_priv *priv;
110 	struct snd_soc_dai_link *link;
111 	struct snd_soc_card *card;
112 	int ret;
113 
114 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
115 	if (!priv)
116 		return -ENOMEM;
117 
118 	card = &priv->card;
119 	card->dev = dev;
120 
121 	card->owner = THIS_MODULE;
122 	card->fully_routed = true;
123 
124 	snd_soc_card_set_drvdata(card, priv);
125 
126 	ret = snd_soc_of_parse_card_name(card, "model");
127 	if (ret < 0)
128 		return ret;
129 
130 	if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
131 		ret = snd_soc_of_parse_audio_simple_widgets(card,
132 						"samsung,audio-widgets");
133 		if (ret < 0)
134 			return ret;
135 	}
136 
137 	if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
138 		ret = snd_soc_of_parse_audio_routing(card,
139 						"samsung,audio-routing");
140 		if (ret < 0)
141 			return ret;
142 	}
143 
144 	link = &priv->dai_link;
145 
146 	link->ops = &odroid_card_ops;
147 	link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
148 			SND_SOC_DAIFMT_CBS_CFS;
149 
150 	card->dai_link = &priv->dai_link;
151 	card->num_links = 1;
152 
153 	cpu = of_get_child_by_name(dev->of_node, "cpu");
154 	codec = of_get_child_by_name(dev->of_node, "codec");
155 
156 	link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
157 	if (!link->cpu_of_node) {
158 		dev_err(dev, "Failed parsing cpu/sound-dai property\n");
159 		return -EINVAL;
160 	}
161 
162 	ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
163 	if (ret < 0)
164 		goto err_put_codec_n;
165 
166 	link->platform_of_node = link->cpu_of_node;
167 
168 	link->name = "Primary";
169 	link->stream_name = link->name;
170 
171 
172 	priv->sclk_i2s = of_clk_get_by_name(link->cpu_of_node, "i2s_opclk1");
173 	if (IS_ERR(priv->sclk_i2s)) {
174 		ret = PTR_ERR(priv->sclk_i2s);
175 		goto err_put_i2s_n;
176 	}
177 
178 	priv->clk_i2s_bus = of_clk_get_by_name(link->cpu_of_node, "iis");
179 	if (IS_ERR(priv->clk_i2s_bus)) {
180 		ret = PTR_ERR(priv->clk_i2s_bus);
181 		goto err_put_sclk;
182 	}
183 
184 	ret = devm_snd_soc_register_card(dev, card);
185 	if (ret < 0) {
186 		dev_err(dev, "snd_soc_register_card() failed: %d\n", ret);
187 		goto err_put_clk_i2s;
188 	}
189 
190 	return 0;
191 
192 err_put_clk_i2s:
193 	clk_put(priv->clk_i2s_bus);
194 err_put_sclk:
195 	clk_put(priv->sclk_i2s);
196 err_put_i2s_n:
197 	of_node_put(link->cpu_of_node);
198 err_put_codec_n:
199 	odroid_put_codec_of_nodes(link);
200 	return ret;
201 }
202 
203 static int odroid_audio_remove(struct platform_device *pdev)
204 {
205 	struct odroid_priv *priv = platform_get_drvdata(pdev);
206 
207 	of_node_put(priv->dai_link.cpu_of_node);
208 	odroid_put_codec_of_nodes(&priv->dai_link);
209 	clk_put(priv->sclk_i2s);
210 	clk_put(priv->clk_i2s_bus);
211 
212 	return 0;
213 }
214 
215 static const struct of_device_id odroid_audio_of_match[] = {
216 	{ .compatible	= "samsung,odroid-xu3-audio" },
217 	{ .compatible	= "samsung,odroid-xu4-audio"},
218 	{ },
219 };
220 MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
221 
222 static struct platform_driver odroid_audio_driver = {
223 	.driver = {
224 		.name		= "odroid-audio",
225 		.of_match_table	= odroid_audio_of_match,
226 		.pm		= &snd_soc_pm_ops,
227 	},
228 	.probe	= odroid_audio_probe,
229 	.remove	= odroid_audio_remove,
230 };
231 module_platform_driver(odroid_audio_driver);
232 
233 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
234 MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
235 MODULE_LICENSE("GPL v2");
236