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 LIST_HEAD(sync_providers);
32 static DEFINE_MUTEX(sync_mutex);
34 struct sync_provider {
35 struct list_head link;
36 struct device_node *node;
37 int (*sync_conf)(void *data, int synco);
41 static const struct stm32_sai_conf stm32_sai_conf_f4 = {
42 .version = SAI_STM32F4,
45 static const struct stm32_sai_conf stm32_sai_conf_h7 = {
46 .version = SAI_STM32H7,
49 static const struct of_device_id stm32_sai_ids[] = {
50 { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 },
51 { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 },
55 static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
59 /* Enable peripheral clock to allow GCR register access */
60 ret = clk_prepare_enable(sai->pclk);
62 dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
66 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
68 clk_disable_unprepare(sai->pclk);
73 static int stm32_sai_sync_conf_provider(void *data, int synco)
75 struct stm32_sai_data *sai = (struct stm32_sai_data *)data;
79 /* Enable peripheral clock to allow GCR register access */
80 ret = clk_prepare_enable(sai->pclk);
82 dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
86 dev_dbg(&sai->pdev->dev, "Set %s%s as synchro provider\n",
87 sai->pdev->dev.of_node->name,
88 synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
90 prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
91 if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
92 dev_err(&sai->pdev->dev, "%s%s already set as sync provider\n",
93 sai->pdev->dev.of_node->name,
94 prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
95 clk_disable_unprepare(sai->pclk);
99 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
101 clk_disable_unprepare(sai->pclk);
106 static int stm32_sai_set_sync_provider(struct device_node *np, int synco)
108 struct sync_provider *provider;
111 mutex_lock(&sync_mutex);
112 list_for_each_entry(provider, &sync_providers, link) {
113 if (provider->node == np) {
114 ret = provider->sync_conf(provider->data, synco);
115 mutex_unlock(&sync_mutex);
119 mutex_unlock(&sync_mutex);
121 /* SAI sync provider not found */
125 static int stm32_sai_set_sync(struct stm32_sai_data *sai,
126 struct device_node *np_provider,
127 int synco, int synci)
131 /* Configure sync client */
132 stm32_sai_sync_conf_client(sai, synci);
134 /* Configure sync provider */
135 ret = stm32_sai_set_sync_provider(np_provider, synco);
140 static int stm32_sai_sync_add_provider(struct platform_device *pdev,
143 struct sync_provider *sp;
145 sp = devm_kzalloc(&pdev->dev, sizeof(*sp), GFP_KERNEL);
149 sp->node = of_node_get(pdev->dev.of_node);
151 sp->sync_conf = &stm32_sai_sync_conf_provider;
153 mutex_lock(&sync_mutex);
154 list_add(&sp->link, &sync_providers);
155 mutex_unlock(&sync_mutex);
160 static void stm32_sai_sync_del_provider(struct device_node *np)
162 struct sync_provider *sp;
164 mutex_lock(&sync_mutex);
165 list_for_each_entry(sp, &sync_providers, link) {
166 if (sp->node == np) {
168 of_node_put(sp->node);
172 mutex_unlock(&sync_mutex);
175 static int stm32_sai_probe(struct platform_device *pdev)
177 struct device_node *np = pdev->dev.of_node;
178 struct stm32_sai_data *sai;
179 struct reset_control *rst;
180 struct resource *res;
181 const struct of_device_id *of_id;
184 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
188 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
189 sai->base = devm_ioremap_resource(&pdev->dev, res);
190 if (IS_ERR(sai->base))
191 return PTR_ERR(sai->base);
193 of_id = of_match_device(stm32_sai_ids, &pdev->dev);
195 sai->conf = (struct stm32_sai_conf *)of_id->data;
199 if (!STM_SAI_IS_F4(sai)) {
200 sai->pclk = devm_clk_get(&pdev->dev, "pclk");
201 if (IS_ERR(sai->pclk)) {
202 dev_err(&pdev->dev, "missing bus clock pclk\n");
203 return PTR_ERR(sai->pclk);
207 sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
208 if (IS_ERR(sai->clk_x8k)) {
209 dev_err(&pdev->dev, "missing x8k parent clock\n");
210 return PTR_ERR(sai->clk_x8k);
213 sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
214 if (IS_ERR(sai->clk_x11k)) {
215 dev_err(&pdev->dev, "missing x11k parent clock\n");
216 return PTR_ERR(sai->clk_x11k);
220 sai->irq = platform_get_irq(pdev, 0);
222 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
227 rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
229 reset_control_assert(rst);
231 reset_control_deassert(rst);
234 ret = stm32_sai_sync_add_provider(pdev, sai);
237 sai->set_sync = &stm32_sai_set_sync;
240 platform_set_drvdata(pdev, sai);
242 ret = of_platform_populate(np, NULL, NULL, &pdev->dev);
244 stm32_sai_sync_del_provider(np);
249 static int stm32_sai_remove(struct platform_device *pdev)
251 of_platform_depopulate(&pdev->dev);
253 stm32_sai_sync_del_provider(pdev->dev.of_node);
258 MODULE_DEVICE_TABLE(of, stm32_sai_ids);
260 static struct platform_driver stm32_sai_driver = {
262 .name = "st,stm32-sai",
263 .of_match_table = stm32_sai_ids,
265 .probe = stm32_sai_probe,
266 .remove = stm32_sai_remove,
269 module_platform_driver(stm32_sai_driver);
271 MODULE_DESCRIPTION("STM32 Soc SAI Interface");
272 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
273 MODULE_ALIAS("platform:st,stm32-sai");
274 MODULE_LICENSE("GPL v2");