xref: /openbmc/linux/sound/soc/samsung/odroid.c (revision 9d154e42)
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 *pll;
23 	struct clk *rclk;
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->pll, pll_freq + 1);
62 	if (ret < 0)
63 		return ret;
64 
65 	rclk_freq = params_rate(params) * 256 * 4;
66 
67 	ret = clk_set_rate(priv->rclk, rclk_freq);
68 	if (ret < 0)
69 		return ret;
70 
71 	if (rtd->num_codecs > 1) {
72 		struct snd_soc_dai *codec_dai = rtd->codec_dais[1];
73 
74 		ret = snd_soc_dai_set_sysclk(codec_dai, 0, rclk_freq,
75 					     SND_SOC_CLOCK_IN);
76 		if (ret < 0)
77 			return ret;
78 	}
79 
80 	return 0;
81 }
82 
83 static const struct snd_soc_ops odroid_card_ops = {
84 	.startup = odroid_card_startup,
85 	.hw_params = odroid_card_hw_params,
86 };
87 
88 static void odroid_put_codec_of_nodes(struct snd_soc_dai_link *link)
89 {
90 	struct snd_soc_dai_link_component *component = link->codecs;
91 	int i;
92 
93 	for (i = 0; i < link->num_codecs; i++, component++) {
94 		if (!component->of_node)
95 			break;
96 		of_node_put(component->of_node);
97 	}
98 }
99 
100 static int odroid_audio_probe(struct platform_device *pdev)
101 {
102 	struct device *dev = &pdev->dev;
103 	struct device_node *cpu, *codec;
104 	struct odroid_priv *priv;
105 	struct snd_soc_dai_link *link;
106 	struct snd_soc_card *card;
107 	int ret;
108 
109 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
110 	if (!priv)
111 		return -ENOMEM;
112 
113 	card = &priv->card;
114 	card->dev = dev;
115 
116 	card->owner = THIS_MODULE;
117 	card->fully_routed = true;
118 
119 	snd_soc_card_set_drvdata(card, priv);
120 
121 	priv->pll = devm_clk_get(dev, "epll");
122 	if (IS_ERR(priv->pll))
123 		return PTR_ERR(priv->pll);
124 
125 	priv->rclk = devm_clk_get(dev, "i2s_rclk");
126 	if (IS_ERR(priv->rclk))
127 		return PTR_ERR(priv->rclk);
128 
129 	ret = snd_soc_of_parse_card_name(card, "model");
130 	if (ret < 0)
131 		return ret;
132 
133 	if (of_property_read_bool(dev->of_node, "samsung,audio-widgets")) {
134 		ret = snd_soc_of_parse_audio_simple_widgets(card,
135 						"samsung,audio-widgets");
136 		if (ret < 0)
137 			return ret;
138 	}
139 
140 	if (of_property_read_bool(dev->of_node, "samsung,audio-routing")) {
141 		ret = snd_soc_of_parse_audio_routing(card,
142 						"samsung,audio-routing");
143 		if (ret < 0)
144 			return ret;
145 	}
146 
147 	link = &priv->dai_link;
148 
149 	link->ops = &odroid_card_ops;
150 	link->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
151 			SND_SOC_DAIFMT_CBS_CFS;
152 
153 	card->dai_link = &priv->dai_link;
154 	card->num_links = 1;
155 
156 	cpu = of_get_child_by_name(dev->of_node, "cpu");
157 	codec = of_get_child_by_name(dev->of_node, "codec");
158 
159 	link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
160 	if (!link->cpu_of_node) {
161 		dev_err(dev, "Failed parsing cpu/sound-dai property\n");
162 		return -EINVAL;
163 	}
164 
165 	ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
166 	if (ret < 0)
167 		goto err_put_codec_n;
168 
169 	link->platform_of_node = link->cpu_of_node;
170 
171 	link->name = "Primary";
172 	link->stream_name = link->name;
173 
174 	ret = devm_snd_soc_register_card(dev, card);
175 	if (ret < 0) {
176 		dev_err(dev, "snd_soc_register_card() failed: %d\n", ret);
177 		goto err_put_i2s_n;
178 	}
179 
180 	return 0;
181 
182 err_put_i2s_n:
183 	of_node_put(link->cpu_of_node);
184 err_put_codec_n:
185 	odroid_put_codec_of_nodes(link);
186 	return ret;
187 }
188 
189 static int odroid_audio_remove(struct platform_device *pdev)
190 {
191 	struct odroid_priv *priv = platform_get_drvdata(pdev);
192 
193 	of_node_put(priv->dai_link.cpu_of_node);
194 	odroid_put_codec_of_nodes(&priv->dai_link);
195 
196 	return 0;
197 }
198 
199 static const struct of_device_id odroid_audio_of_match[] = {
200 	{ .compatible	= "samsung,odroid-xu3-audio" },
201 	{ .compatible	= "samsung,odroid-xu4-audio"},
202 	{ },
203 };
204 MODULE_DEVICE_TABLE(of, odroid_audio_of_match);
205 
206 static struct platform_driver odroid_audio_driver = {
207 	.driver = {
208 		.name		= "odroid-audio",
209 		.of_match_table	= odroid_audio_of_match,
210 		.pm		= &snd_soc_pm_ops,
211 	},
212 	.probe	= odroid_audio_probe,
213 	.remove	= odroid_audio_remove,
214 };
215 module_platform_driver(odroid_audio_driver);
216 
217 MODULE_AUTHOR("Sylwester Nawrocki <s.nawrocki@samsung.com>");
218 MODULE_DESCRIPTION("Odroid XU3/XU4 audio support");
219 MODULE_LICENSE("GPL v2");
220