blob: ef4273361d0d8dfe98b53c92ece167b05ec7fde4 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
4 *
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
8
9#include <linux/bitfield.h>
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/module.h>
13#include <linux/of_platform.h>
David Brazdil0f672f62019-12-10 10:32:29 +000014#include <linux/pinctrl/consumer.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include <linux/reset.h>
16
17#include <sound/dmaengine_pcm.h>
18#include <sound/core.h>
19
20#include "stm32_sai.h"
21
22static const struct stm32_sai_conf stm32_sai_conf_f4 = {
David Brazdil0f672f62019-12-10 10:32:29 +000023 .version = STM_SAI_STM32F4,
24 .fifo_size = 8,
25 .has_spdif_pdm = false,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000026};
27
David Brazdil0f672f62019-12-10 10:32:29 +000028/*
29 * Default settings for stm32 H7 socs and next.
30 * These default settings will be overridden if the soc provides
31 * support of hardware configuration registers.
32 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000033static const struct stm32_sai_conf stm32_sai_conf_h7 = {
David Brazdil0f672f62019-12-10 10:32:29 +000034 .version = STM_SAI_STM32H7,
35 .fifo_size = 8,
36 .has_spdif_pdm = true,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037};
38
39static const struct of_device_id stm32_sai_ids[] = {
40 { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 },
41 { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 },
42 {}
43};
44
David Brazdil0f672f62019-12-10 10:32:29 +000045static int stm32_sai_pclk_disable(struct device *dev)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046{
David Brazdil0f672f62019-12-10 10:32:29 +000047 struct stm32_sai_data *sai = dev_get_drvdata(dev);
48
49 clk_disable_unprepare(sai->pclk);
50
51 return 0;
52}
53
54static int stm32_sai_pclk_enable(struct device *dev)
55{
56 struct stm32_sai_data *sai = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057 int ret;
58
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059 ret = clk_prepare_enable(sai->pclk);
60 if (ret) {
61 dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
62 return ret;
63 }
64
David Brazdil0f672f62019-12-10 10:32:29 +000065 return 0;
66}
67
68static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
69{
70 int ret;
71
72 /* Enable peripheral clock to allow GCR register access */
73 ret = stm32_sai_pclk_enable(&sai->pdev->dev);
74 if (ret)
75 return ret;
76
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000077 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
78
David Brazdil0f672f62019-12-10 10:32:29 +000079 stm32_sai_pclk_disable(&sai->pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000080
81 return 0;
82}
83
84static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco)
85{
86 u32 prev_synco;
87 int ret;
88
89 /* Enable peripheral clock to allow GCR register access */
David Brazdil0f672f62019-12-10 10:32:29 +000090 ret = stm32_sai_pclk_enable(&sai->pdev->dev);
91 if (ret)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000093
David Brazdil0f672f62019-12-10 10:32:29 +000094 dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n",
95 sai->pdev->dev.of_node,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
97
98 prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
99 if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
David Brazdil0f672f62019-12-10 10:32:29 +0000100 dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n",
101 sai->pdev->dev.of_node,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102 prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
David Brazdil0f672f62019-12-10 10:32:29 +0000103 stm32_sai_pclk_disable(&sai->pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 return -EINVAL;
105 }
106
107 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
108
David Brazdil0f672f62019-12-10 10:32:29 +0000109 stm32_sai_pclk_disable(&sai->pdev->dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000110
111 return 0;
112}
113
114static int stm32_sai_set_sync(struct stm32_sai_data *sai_client,
115 struct device_node *np_provider,
116 int synco, int synci)
117{
118 struct platform_device *pdev = of_find_device_by_node(np_provider);
119 struct stm32_sai_data *sai_provider;
120 int ret;
121
122 if (!pdev) {
123 dev_err(&sai_client->pdev->dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000124 "Device not found for node %pOFn\n", np_provider);
125 of_node_put(np_provider);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126 return -ENODEV;
127 }
128
129 sai_provider = platform_get_drvdata(pdev);
130 if (!sai_provider) {
131 dev_err(&sai_client->pdev->dev,
132 "SAI sync provider data not found\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000133 ret = -EINVAL;
134 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000135 }
136
137 /* Configure sync client */
138 ret = stm32_sai_sync_conf_client(sai_client, synci);
139 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000140 goto error;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141
142 /* Configure sync provider */
David Brazdil0f672f62019-12-10 10:32:29 +0000143 ret = stm32_sai_sync_conf_provider(sai_provider, synco);
144
145error:
146 put_device(&pdev->dev);
147 of_node_put(np_provider);
148 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000149}
150
151static int stm32_sai_probe(struct platform_device *pdev)
152{
153 struct stm32_sai_data *sai;
154 struct reset_control *rst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000155 const struct of_device_id *of_id;
David Brazdil0f672f62019-12-10 10:32:29 +0000156 u32 val;
157 int ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158
159 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
160 if (!sai)
161 return -ENOMEM;
162
David Brazdil0f672f62019-12-10 10:32:29 +0000163 sai->base = devm_platform_ioremap_resource(pdev, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000164 if (IS_ERR(sai->base))
165 return PTR_ERR(sai->base);
166
167 of_id = of_match_device(stm32_sai_ids, &pdev->dev);
168 if (of_id)
David Brazdil0f672f62019-12-10 10:32:29 +0000169 memcpy(&sai->conf, (const struct stm32_sai_conf *)of_id->data,
170 sizeof(struct stm32_sai_conf));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171 else
172 return -EINVAL;
173
174 if (!STM_SAI_IS_F4(sai)) {
175 sai->pclk = devm_clk_get(&pdev->dev, "pclk");
176 if (IS_ERR(sai->pclk)) {
177 dev_err(&pdev->dev, "missing bus clock pclk\n");
178 return PTR_ERR(sai->pclk);
179 }
180 }
181
182 sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
183 if (IS_ERR(sai->clk_x8k)) {
184 dev_err(&pdev->dev, "missing x8k parent clock\n");
185 return PTR_ERR(sai->clk_x8k);
186 }
187
188 sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
189 if (IS_ERR(sai->clk_x11k)) {
190 dev_err(&pdev->dev, "missing x11k parent clock\n");
191 return PTR_ERR(sai->clk_x11k);
192 }
193
194 /* init irqs */
195 sai->irq = platform_get_irq(pdev, 0);
David Brazdil0f672f62019-12-10 10:32:29 +0000196 if (sai->irq < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000197 return sai->irq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000198
199 /* reset */
200 rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
201 if (!IS_ERR(rst)) {
202 reset_control_assert(rst);
203 udelay(2);
204 reset_control_deassert(rst);
205 }
206
David Brazdil0f672f62019-12-10 10:32:29 +0000207 /* Enable peripheral clock to allow register access */
208 ret = clk_prepare_enable(sai->pclk);
209 if (ret) {
210 dev_err(&pdev->dev, "failed to enable clock: %d\n", ret);
211 return ret;
212 }
213
214 val = FIELD_GET(SAI_IDR_ID_MASK,
215 readl_relaxed(sai->base + STM_SAI_IDR));
216 if (val == SAI_IPIDR_NUMBER) {
217 val = readl_relaxed(sai->base + STM_SAI_HWCFGR);
218 sai->conf.fifo_size = FIELD_GET(SAI_HWCFGR_FIFO_SIZE, val);
219 sai->conf.has_spdif_pdm = !!FIELD_GET(SAI_HWCFGR_SPDIF_PDM,
220 val);
221
222 val = readl_relaxed(sai->base + STM_SAI_VERR);
223 sai->conf.version = val;
224
225 dev_dbg(&pdev->dev, "SAI version: %lu.%lu registered\n",
226 FIELD_GET(SAI_VERR_MAJ_MASK, val),
227 FIELD_GET(SAI_VERR_MIN_MASK, val));
228 }
229 clk_disable_unprepare(sai->pclk);
230
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231 sai->pdev = pdev;
232 sai->set_sync = &stm32_sai_set_sync;
233 platform_set_drvdata(pdev, sai);
234
235 return devm_of_platform_populate(&pdev->dev);
236}
237
David Brazdil0f672f62019-12-10 10:32:29 +0000238#ifdef CONFIG_PM_SLEEP
239/*
240 * When pins are shared by two sai sub instances, pins have to be defined
241 * in sai parent node. In this case, pins state is not managed by alsa fw.
242 * These pins are managed in suspend/resume callbacks.
243 */
244static int stm32_sai_suspend(struct device *dev)
245{
246 struct stm32_sai_data *sai = dev_get_drvdata(dev);
247 int ret;
248
249 ret = stm32_sai_pclk_enable(dev);
250 if (ret)
251 return ret;
252
253 sai->gcr = readl_relaxed(sai->base);
254 stm32_sai_pclk_disable(dev);
255
256 return pinctrl_pm_select_sleep_state(dev);
257}
258
259static int stm32_sai_resume(struct device *dev)
260{
261 struct stm32_sai_data *sai = dev_get_drvdata(dev);
262 int ret;
263
264 ret = stm32_sai_pclk_enable(dev);
265 if (ret)
266 return ret;
267
268 writel_relaxed(sai->gcr, sai->base);
269 stm32_sai_pclk_disable(dev);
270
271 return pinctrl_pm_select_default_state(dev);
272}
273#endif /* CONFIG_PM_SLEEP */
274
275static const struct dev_pm_ops stm32_sai_pm_ops = {
276 SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume)
277};
278
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279MODULE_DEVICE_TABLE(of, stm32_sai_ids);
280
281static struct platform_driver stm32_sai_driver = {
282 .driver = {
283 .name = "st,stm32-sai",
284 .of_match_table = stm32_sai_ids,
David Brazdil0f672f62019-12-10 10:32:29 +0000285 .pm = &stm32_sai_pm_ops,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286 },
287 .probe = stm32_sai_probe,
288};
289
290module_platform_driver(stm32_sai_driver);
291
292MODULE_DESCRIPTION("STM32 Soc SAI Interface");
293MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
294MODULE_ALIAS("platform:st,stm32-sai");
295MODULE_LICENSE("GPL v2");