xref: /openbmc/linux/sound/soc/stm/stm32_sai.c (revision f519f0be)
1 /*
2  * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
3  *
4  * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5  * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
6  *
7  * License terms: GPL V2.0.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16  * details.
17  */
18 
19 #include <linux/bitfield.h>
20 #include <linux/clk.h>
21 #include <linux/delay.h>
22 #include <linux/module.h>
23 #include <linux/of_platform.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/reset.h>
26 
27 #include <sound/dmaengine_pcm.h>
28 #include <sound/core.h>
29 
30 #include "stm32_sai.h"
31 
32 static const struct stm32_sai_conf stm32_sai_conf_f4 = {
33 	.version = SAI_STM32F4,
34 	.has_spdif = false,
35 };
36 
37 static const struct stm32_sai_conf stm32_sai_conf_h7 = {
38 	.version = SAI_STM32H7,
39 	.has_spdif = true,
40 };
41 
42 static const struct of_device_id stm32_sai_ids[] = {
43 	{ .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 },
44 	{ .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 },
45 	{}
46 };
47 
48 static int stm32_sai_pclk_disable(struct device *dev)
49 {
50 	struct stm32_sai_data *sai = dev_get_drvdata(dev);
51 
52 	clk_disable_unprepare(sai->pclk);
53 
54 	return 0;
55 }
56 
57 static int stm32_sai_pclk_enable(struct device *dev)
58 {
59 	struct stm32_sai_data *sai = dev_get_drvdata(dev);
60 	int ret;
61 
62 	ret = clk_prepare_enable(sai->pclk);
63 	if (ret) {
64 		dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
65 		return ret;
66 	}
67 
68 	return 0;
69 }
70 
71 static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
72 {
73 	int ret;
74 
75 	/* Enable peripheral clock to allow GCR register access */
76 	ret = stm32_sai_pclk_enable(&sai->pdev->dev);
77 	if (ret)
78 		return ret;
79 
80 	writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
81 
82 	stm32_sai_pclk_disable(&sai->pdev->dev);
83 
84 	return 0;
85 }
86 
87 static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco)
88 {
89 	u32 prev_synco;
90 	int ret;
91 
92 	/* Enable peripheral clock to allow GCR register access */
93 	ret = stm32_sai_pclk_enable(&sai->pdev->dev);
94 	if (ret)
95 		return ret;
96 
97 	dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n",
98 		sai->pdev->dev.of_node,
99 		synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
100 
101 	prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
102 	if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
103 		dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n",
104 			sai->pdev->dev.of_node,
105 			prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
106 			stm32_sai_pclk_disable(&sai->pdev->dev);
107 		return -EINVAL;
108 	}
109 
110 	writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
111 
112 	stm32_sai_pclk_disable(&sai->pdev->dev);
113 
114 	return 0;
115 }
116 
117 static int stm32_sai_set_sync(struct stm32_sai_data *sai_client,
118 			      struct device_node *np_provider,
119 			      int synco, int synci)
120 {
121 	struct platform_device *pdev = of_find_device_by_node(np_provider);
122 	struct stm32_sai_data *sai_provider;
123 	int ret;
124 
125 	if (!pdev) {
126 		dev_err(&sai_client->pdev->dev,
127 			"Device not found for node %pOFn\n", np_provider);
128 		of_node_put(np_provider);
129 		return -ENODEV;
130 	}
131 
132 	sai_provider = platform_get_drvdata(pdev);
133 	if (!sai_provider) {
134 		dev_err(&sai_client->pdev->dev,
135 			"SAI sync provider data not found\n");
136 		ret = -EINVAL;
137 		goto error;
138 	}
139 
140 	/* Configure sync client */
141 	ret = stm32_sai_sync_conf_client(sai_client, synci);
142 	if (ret < 0)
143 		goto error;
144 
145 	/* Configure sync provider */
146 	ret = stm32_sai_sync_conf_provider(sai_provider, synco);
147 
148 error:
149 	put_device(&pdev->dev);
150 	of_node_put(np_provider);
151 	return ret;
152 }
153 
154 static int stm32_sai_probe(struct platform_device *pdev)
155 {
156 	struct stm32_sai_data *sai;
157 	struct reset_control *rst;
158 	struct resource *res;
159 	const struct of_device_id *of_id;
160 
161 	sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
162 	if (!sai)
163 		return -ENOMEM;
164 
165 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
166 	sai->base = devm_ioremap_resource(&pdev->dev, res);
167 	if (IS_ERR(sai->base))
168 		return PTR_ERR(sai->base);
169 
170 	of_id = of_match_device(stm32_sai_ids, &pdev->dev);
171 	if (of_id)
172 		sai->conf = (struct stm32_sai_conf *)of_id->data;
173 	else
174 		return -EINVAL;
175 
176 	if (!STM_SAI_IS_F4(sai)) {
177 		sai->pclk = devm_clk_get(&pdev->dev, "pclk");
178 		if (IS_ERR(sai->pclk)) {
179 			dev_err(&pdev->dev, "missing bus clock pclk\n");
180 			return PTR_ERR(sai->pclk);
181 		}
182 	}
183 
184 	sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
185 	if (IS_ERR(sai->clk_x8k)) {
186 		dev_err(&pdev->dev, "missing x8k parent clock\n");
187 		return PTR_ERR(sai->clk_x8k);
188 	}
189 
190 	sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
191 	if (IS_ERR(sai->clk_x11k)) {
192 		dev_err(&pdev->dev, "missing x11k parent clock\n");
193 		return PTR_ERR(sai->clk_x11k);
194 	}
195 
196 	/* init irqs */
197 	sai->irq = platform_get_irq(pdev, 0);
198 	if (sai->irq < 0) {
199 		dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
200 		return sai->irq;
201 	}
202 
203 	/* reset */
204 	rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
205 	if (!IS_ERR(rst)) {
206 		reset_control_assert(rst);
207 		udelay(2);
208 		reset_control_deassert(rst);
209 	}
210 
211 	sai->pdev = pdev;
212 	sai->set_sync = &stm32_sai_set_sync;
213 	platform_set_drvdata(pdev, sai);
214 
215 	return devm_of_platform_populate(&pdev->dev);
216 }
217 
218 #ifdef CONFIG_PM_SLEEP
219 /*
220  * When pins are shared by two sai sub instances, pins have to be defined
221  * in sai parent node. In this case, pins state is not managed by alsa fw.
222  * These pins are managed in suspend/resume callbacks.
223  */
224 static int stm32_sai_suspend(struct device *dev)
225 {
226 	struct stm32_sai_data *sai = dev_get_drvdata(dev);
227 	int ret;
228 
229 	ret = stm32_sai_pclk_enable(dev);
230 	if (ret)
231 		return ret;
232 
233 	sai->gcr = readl_relaxed(sai->base);
234 	stm32_sai_pclk_disable(dev);
235 
236 	return pinctrl_pm_select_sleep_state(dev);
237 }
238 
239 static int stm32_sai_resume(struct device *dev)
240 {
241 	struct stm32_sai_data *sai = dev_get_drvdata(dev);
242 	int ret;
243 
244 	ret = stm32_sai_pclk_enable(dev);
245 	if (ret)
246 		return ret;
247 
248 	writel_relaxed(sai->gcr, sai->base);
249 	stm32_sai_pclk_disable(dev);
250 
251 	return pinctrl_pm_select_default_state(dev);
252 }
253 #endif /* CONFIG_PM_SLEEP */
254 
255 static const struct dev_pm_ops stm32_sai_pm_ops = {
256 	SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume)
257 };
258 
259 MODULE_DEVICE_TABLE(of, stm32_sai_ids);
260 
261 static struct platform_driver stm32_sai_driver = {
262 	.driver = {
263 		.name = "st,stm32-sai",
264 		.of_match_table = stm32_sai_ids,
265 		.pm = &stm32_sai_pm_ops,
266 	},
267 	.probe = stm32_sai_probe,
268 };
269 
270 module_platform_driver(stm32_sai_driver);
271 
272 MODULE_DESCRIPTION("STM32 Soc SAI Interface");
273 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
274 MODULE_ALIAS("platform:st,stm32-sai");
275 MODULE_LICENSE("GPL v2");
276