2 * STM32 ALSA SoC Digital Audio Interface (SAI) driver.
4 * Copyright (C) 2016, STMicroelectronics - All Rights Reserved
5 * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics.
7 * License terms: GPL V2.0.
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.
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
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/reset.h>
26 #include <sound/dmaengine_pcm.h>
27 #include <sound/core.h>
29 #include "stm32_sai.h"
31 static const struct stm32_sai_conf stm32_sai_conf_f4
= {
32 .version
= SAI_STM32F4
,
36 static const struct stm32_sai_conf stm32_sai_conf_h7
= {
37 .version
= SAI_STM32H7
,
41 static const struct of_device_id stm32_sai_ids
[] = {
42 { .compatible
= "st,stm32f4-sai", .data
= (void *)&stm32_sai_conf_f4
},
43 { .compatible
= "st,stm32h7-sai", .data
= (void *)&stm32_sai_conf_h7
},
47 static int stm32_sai_sync_conf_client(struct stm32_sai_data
*sai
, int synci
)
51 /* Enable peripheral clock to allow GCR register access */
52 ret
= clk_prepare_enable(sai
->pclk
);
54 dev_err(&sai
->pdev
->dev
, "failed to enable clock: %d\n", ret
);
58 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK
, (synci
- 1)), sai
->base
);
60 clk_disable_unprepare(sai
->pclk
);
65 static int stm32_sai_sync_conf_provider(struct stm32_sai_data
*sai
, int synco
)
70 /* Enable peripheral clock to allow GCR register access */
71 ret
= clk_prepare_enable(sai
->pclk
);
73 dev_err(&sai
->pdev
->dev
, "failed to enable clock: %d\n", ret
);
77 dev_dbg(&sai
->pdev
->dev
, "Set %pOFn%s as synchro provider\n",
78 sai
->pdev
->dev
.of_node
,
79 synco
== STM_SAI_SYNC_OUT_A
? "A" : "B");
81 prev_synco
= FIELD_GET(SAI_GCR_SYNCOUT_MASK
, readl_relaxed(sai
->base
));
82 if (prev_synco
!= STM_SAI_SYNC_OUT_NONE
&& synco
!= prev_synco
) {
83 dev_err(&sai
->pdev
->dev
, "%pOFn%s already set as sync provider\n",
84 sai
->pdev
->dev
.of_node
,
85 prev_synco
== STM_SAI_SYNC_OUT_A
? "A" : "B");
86 clk_disable_unprepare(sai
->pclk
);
90 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK
, synco
), sai
->base
);
92 clk_disable_unprepare(sai
->pclk
);
97 static int stm32_sai_set_sync(struct stm32_sai_data
*sai_client
,
98 struct device_node
*np_provider
,
101 struct platform_device
*pdev
= of_find_device_by_node(np_provider
);
102 struct stm32_sai_data
*sai_provider
;
106 dev_err(&sai_client
->pdev
->dev
,
107 "Device not found for node %pOFn\n", np_provider
);
111 sai_provider
= platform_get_drvdata(pdev
);
113 dev_err(&sai_client
->pdev
->dev
,
114 "SAI sync provider data not found\n");
118 /* Configure sync client */
119 ret
= stm32_sai_sync_conf_client(sai_client
, synci
);
123 /* Configure sync provider */
124 return stm32_sai_sync_conf_provider(sai_provider
, synco
);
127 static int stm32_sai_probe(struct platform_device
*pdev
)
129 struct stm32_sai_data
*sai
;
130 struct reset_control
*rst
;
131 struct resource
*res
;
132 const struct of_device_id
*of_id
;
134 sai
= devm_kzalloc(&pdev
->dev
, sizeof(*sai
), GFP_KERNEL
);
138 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
139 sai
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
140 if (IS_ERR(sai
->base
))
141 return PTR_ERR(sai
->base
);
143 of_id
= of_match_device(stm32_sai_ids
, &pdev
->dev
);
145 sai
->conf
= (struct stm32_sai_conf
*)of_id
->data
;
149 if (!STM_SAI_IS_F4(sai
)) {
150 sai
->pclk
= devm_clk_get(&pdev
->dev
, "pclk");
151 if (IS_ERR(sai
->pclk
)) {
152 dev_err(&pdev
->dev
, "missing bus clock pclk\n");
153 return PTR_ERR(sai
->pclk
);
157 sai
->clk_x8k
= devm_clk_get(&pdev
->dev
, "x8k");
158 if (IS_ERR(sai
->clk_x8k
)) {
159 dev_err(&pdev
->dev
, "missing x8k parent clock\n");
160 return PTR_ERR(sai
->clk_x8k
);
163 sai
->clk_x11k
= devm_clk_get(&pdev
->dev
, "x11k");
164 if (IS_ERR(sai
->clk_x11k
)) {
165 dev_err(&pdev
->dev
, "missing x11k parent clock\n");
166 return PTR_ERR(sai
->clk_x11k
);
170 sai
->irq
= platform_get_irq(pdev
, 0);
172 dev_err(&pdev
->dev
, "no irq for node %s\n", pdev
->name
);
177 rst
= devm_reset_control_get_exclusive(&pdev
->dev
, NULL
);
179 reset_control_assert(rst
);
181 reset_control_deassert(rst
);
185 sai
->set_sync
= &stm32_sai_set_sync
;
186 platform_set_drvdata(pdev
, sai
);
188 return devm_of_platform_populate(&pdev
->dev
);
191 MODULE_DEVICE_TABLE(of
, stm32_sai_ids
);
193 static struct platform_driver stm32_sai_driver
= {
195 .name
= "st,stm32-sai",
196 .of_match_table
= stm32_sai_ids
,
198 .probe
= stm32_sai_probe
,
201 module_platform_driver(stm32_sai_driver
);
203 MODULE_DESCRIPTION("STM32 Soc SAI Interface");
204 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
205 MODULE_ALIAS("platform:st,stm32-sai");
206 MODULE_LICENSE("GPL v2");