1 // SPDX-License-Identifier: GPL-2.0-only
3 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
5 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
6 * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
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>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/reset.h>
17 #include <sound/dmaengine_pcm.h>
18 #include <sound/core.h>
20 #include "stm32_sai.h"
22 static const struct stm32_sai_conf stm32_sai_conf_f4
= {
23 .version
= STM_SAI_STM32F4
,
25 .has_spdif_pdm
= false,
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.
33 static const struct stm32_sai_conf stm32_sai_conf_h7
= {
34 .version
= STM_SAI_STM32H7
,
36 .has_spdif_pdm
= true,
39 static 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
},
45 static int stm32_sai_pclk_disable(struct device
*dev
)
47 struct stm32_sai_data
*sai
= dev_get_drvdata(dev
);
49 clk_disable_unprepare(sai
->pclk
);
54 static int stm32_sai_pclk_enable(struct device
*dev
)
56 struct stm32_sai_data
*sai
= dev_get_drvdata(dev
);
59 ret
= clk_prepare_enable(sai
->pclk
);
61 dev_err(&sai
->pdev
->dev
, "failed to enable clock: %d\n", ret
);
68 static int stm32_sai_sync_conf_client(struct stm32_sai_data
*sai
, int synci
)
72 /* Enable peripheral clock to allow GCR register access */
73 ret
= stm32_sai_pclk_enable(&sai
->pdev
->dev
);
77 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK
, (synci
- 1)), sai
->base
);
79 stm32_sai_pclk_disable(&sai
->pdev
->dev
);
84 static int stm32_sai_sync_conf_provider(struct stm32_sai_data
*sai
, int synco
)
89 /* Enable peripheral clock to allow GCR register access */
90 ret
= stm32_sai_pclk_enable(&sai
->pdev
->dev
);
94 dev_dbg(&sai
->pdev
->dev
, "Set %pOFn%s as synchro provider\n",
95 sai
->pdev
->dev
.of_node
,
96 synco
== STM_SAI_SYNC_OUT_A
? "A" : "B");
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
) {
100 dev_err(&sai
->pdev
->dev
, "%pOFn%s already set as sync provider\n",
101 sai
->pdev
->dev
.of_node
,
102 prev_synco
== STM_SAI_SYNC_OUT_A
? "A" : "B");
103 stm32_sai_pclk_disable(&sai
->pdev
->dev
);
107 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK
, synco
), sai
->base
);
109 stm32_sai_pclk_disable(&sai
->pdev
->dev
);
114 static int stm32_sai_set_sync(struct stm32_sai_data
*sai_client
,
115 struct device_node
*np_provider
,
116 int synco
, int synci
)
118 struct platform_device
*pdev
= of_find_device_by_node(np_provider
);
119 struct stm32_sai_data
*sai_provider
;
123 dev_err(&sai_client
->pdev
->dev
,
124 "Device not found for node %pOFn\n", np_provider
);
125 of_node_put(np_provider
);
129 sai_provider
= platform_get_drvdata(pdev
);
131 dev_err(&sai_client
->pdev
->dev
,
132 "SAI sync provider data not found\n");
137 /* Configure sync client */
138 ret
= stm32_sai_sync_conf_client(sai_client
, synci
);
142 /* Configure sync provider */
143 ret
= stm32_sai_sync_conf_provider(sai_provider
, synco
);
146 put_device(&pdev
->dev
);
147 of_node_put(np_provider
);
151 static int stm32_sai_probe(struct platform_device
*pdev
)
153 struct stm32_sai_data
*sai
;
154 struct reset_control
*rst
;
155 const struct of_device_id
*of_id
;
159 sai
= devm_kzalloc(&pdev
->dev
, sizeof(*sai
), GFP_KERNEL
);
163 sai
->base
= devm_platform_ioremap_resource(pdev
, 0);
164 if (IS_ERR(sai
->base
))
165 return PTR_ERR(sai
->base
);
167 of_id
= of_match_device(stm32_sai_ids
, &pdev
->dev
);
169 memcpy(&sai
->conf
, (const struct stm32_sai_conf
*)of_id
->data
,
170 sizeof(struct stm32_sai_conf
));
174 if (!STM_SAI_IS_F4(sai
)) {
175 sai
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
176 if (IS_ERR(sai
->pclk
)) {
177 if (PTR_ERR(sai
->pclk
) != -EPROBE_DEFER
)
178 dev_err(&pdev
->dev
, "missing bus clock pclk: %ld\n",
180 return PTR_ERR(sai
->pclk
);
184 sai
->clk_x8k
= devm_clk_get(&pdev
->dev
, "x8k");
185 if (IS_ERR(sai
->clk_x8k
)) {
186 if (PTR_ERR(sai
->clk_x8k
) != -EPROBE_DEFER
)
187 dev_err(&pdev
->dev
, "missing x8k parent clock: %ld\n",
188 PTR_ERR(sai
->clk_x8k
));
189 return PTR_ERR(sai
->clk_x8k
);
192 sai
->clk_x11k
= devm_clk_get(&pdev
->dev
, "x11k");
193 if (IS_ERR(sai
->clk_x11k
)) {
194 if (PTR_ERR(sai
->clk_x11k
) != -EPROBE_DEFER
)
195 dev_err(&pdev
->dev
, "missing x11k parent clock: %ld\n",
196 PTR_ERR(sai
->clk_x11k
));
197 return PTR_ERR(sai
->clk_x11k
);
201 sai
->irq
= platform_get_irq(pdev
, 0);
206 rst
= devm_reset_control_get_optional_exclusive(&pdev
->dev
, NULL
);
208 if (PTR_ERR(rst
) != -EPROBE_DEFER
)
209 dev_err(&pdev
->dev
, "Reset controller error %ld\n",
213 reset_control_assert(rst
);
215 reset_control_deassert(rst
);
217 /* Enable peripheral clock to allow register access */
218 ret
= clk_prepare_enable(sai
->pclk
);
220 dev_err(&pdev
->dev
, "failed to enable clock: %d\n", ret
);
224 val
= FIELD_GET(SAI_IDR_ID_MASK
,
225 readl_relaxed(sai
->base
+ STM_SAI_IDR
));
226 if (val
== SAI_IPIDR_NUMBER
) {
227 val
= readl_relaxed(sai
->base
+ STM_SAI_HWCFGR
);
228 sai
->conf
.fifo_size
= FIELD_GET(SAI_HWCFGR_FIFO_SIZE
, val
);
229 sai
->conf
.has_spdif_pdm
= !!FIELD_GET(SAI_HWCFGR_SPDIF_PDM
,
232 val
= readl_relaxed(sai
->base
+ STM_SAI_VERR
);
233 sai
->conf
.version
= val
;
235 dev_dbg(&pdev
->dev
, "SAI version: %lu.%lu registered\n",
236 FIELD_GET(SAI_VERR_MAJ_MASK
, val
),
237 FIELD_GET(SAI_VERR_MIN_MASK
, val
));
239 clk_disable_unprepare(sai
->pclk
);
242 sai
->set_sync
= &stm32_sai_set_sync
;
243 platform_set_drvdata(pdev
, sai
);
245 return devm_of_platform_populate(&pdev
->dev
);
248 #ifdef CONFIG_PM_SLEEP
250 * When pins are shared by two sai sub instances, pins have to be defined
251 * in sai parent node. In this case, pins state is not managed by alsa fw.
252 * These pins are managed in suspend/resume callbacks.
254 static int stm32_sai_suspend(struct device
*dev
)
256 struct stm32_sai_data
*sai
= dev_get_drvdata(dev
);
259 ret
= stm32_sai_pclk_enable(dev
);
263 sai
->gcr
= readl_relaxed(sai
->base
);
264 stm32_sai_pclk_disable(dev
);
266 return pinctrl_pm_select_sleep_state(dev
);
269 static int stm32_sai_resume(struct device
*dev
)
271 struct stm32_sai_data
*sai
= dev_get_drvdata(dev
);
274 ret
= stm32_sai_pclk_enable(dev
);
278 writel_relaxed(sai
->gcr
, sai
->base
);
279 stm32_sai_pclk_disable(dev
);
281 return pinctrl_pm_select_default_state(dev
);
283 #endif /* CONFIG_PM_SLEEP */
285 static const struct dev_pm_ops stm32_sai_pm_ops
= {
286 SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend
, stm32_sai_resume
)
289 MODULE_DEVICE_TABLE(of
, stm32_sai_ids
);
291 static struct platform_driver stm32_sai_driver
= {
293 .name
= "st,stm32-sai",
294 .of_match_table
= stm32_sai_ids
,
295 .pm
= &stm32_sai_pm_ops
,
297 .probe
= stm32_sai_probe
,
300 module_platform_driver(stm32_sai_driver
);
302 MODULE_DESCRIPTION("STM32 Soc SAI Interface");
303 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
304 MODULE_ALIAS("platform:st,stm32-sai");
305 MODULE_LICENSE("GPL v2");