xref: /openbmc/linux/sound/soc/stm/stm32_sai.c (revision 7dd0d835)
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/reset.h>
25 
26 #include <sound/dmaengine_pcm.h>
27 #include <sound/core.h>
28 
29 #include "stm32_sai.h"
30 
31 static const struct stm32_sai_conf stm32_sai_conf_f4 = {
32 	.version = SAI_STM32F4,
33 };
34 
35 static const struct stm32_sai_conf stm32_sai_conf_h7 = {
36 	.version = SAI_STM32H7,
37 };
38 
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 },
42 	{}
43 };
44 
45 static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
46 {
47 	int ret;
48 
49 	/* Enable peripheral clock to allow GCR register access */
50 	ret = clk_prepare_enable(sai->pclk);
51 	if (ret) {
52 		dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
53 		return ret;
54 	}
55 
56 	writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
57 
58 	clk_disable_unprepare(sai->pclk);
59 
60 	return 0;
61 }
62 
63 static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco)
64 {
65 	u32 prev_synco;
66 	int ret;
67 
68 	/* Enable peripheral clock to allow GCR register access */
69 	ret = clk_prepare_enable(sai->pclk);
70 	if (ret) {
71 		dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
72 		return ret;
73 	}
74 
75 	dev_dbg(&sai->pdev->dev, "Set %s%s as synchro provider\n",
76 		sai->pdev->dev.of_node->name,
77 		synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
78 
79 	prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
80 	if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
81 		dev_err(&sai->pdev->dev, "%s%s already set as sync provider\n",
82 			sai->pdev->dev.of_node->name,
83 			prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
84 		clk_disable_unprepare(sai->pclk);
85 		return -EINVAL;
86 	}
87 
88 	writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
89 
90 	clk_disable_unprepare(sai->pclk);
91 
92 	return 0;
93 }
94 
95 static int stm32_sai_set_sync(struct stm32_sai_data *sai_client,
96 			      struct device_node *np_provider,
97 			      int synco, int synci)
98 {
99 	struct platform_device *pdev = of_find_device_by_node(np_provider);
100 	struct stm32_sai_data *sai_provider;
101 	int ret;
102 
103 	if (!pdev) {
104 		dev_err(&sai_client->pdev->dev,
105 			"Device not found for node %s\n", np_provider->name);
106 		return -ENODEV;
107 	}
108 
109 	sai_provider = platform_get_drvdata(pdev);
110 	if (!sai_provider) {
111 		dev_err(&sai_client->pdev->dev,
112 			"SAI sync provider data not found\n");
113 		return -EINVAL;
114 	}
115 
116 	/* Configure sync client */
117 	ret = stm32_sai_sync_conf_client(sai_client, synci);
118 	if (ret < 0)
119 		return ret;
120 
121 	/* Configure sync provider */
122 	return stm32_sai_sync_conf_provider(sai_provider, synco);
123 }
124 
125 static int stm32_sai_probe(struct platform_device *pdev)
126 {
127 	struct device_node *np = pdev->dev.of_node;
128 	struct stm32_sai_data *sai;
129 	struct reset_control *rst;
130 	struct resource *res;
131 	const struct of_device_id *of_id;
132 
133 	sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
134 	if (!sai)
135 		return -ENOMEM;
136 
137 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 	sai->base = devm_ioremap_resource(&pdev->dev, res);
139 	if (IS_ERR(sai->base))
140 		return PTR_ERR(sai->base);
141 
142 	of_id = of_match_device(stm32_sai_ids, &pdev->dev);
143 	if (of_id)
144 		sai->conf = (struct stm32_sai_conf *)of_id->data;
145 	else
146 		return -EINVAL;
147 
148 	if (!STM_SAI_IS_F4(sai)) {
149 		sai->pclk = devm_clk_get(&pdev->dev, "pclk");
150 		if (IS_ERR(sai->pclk)) {
151 			dev_err(&pdev->dev, "missing bus clock pclk\n");
152 			return PTR_ERR(sai->pclk);
153 		}
154 	}
155 
156 	sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
157 	if (IS_ERR(sai->clk_x8k)) {
158 		dev_err(&pdev->dev, "missing x8k parent clock\n");
159 		return PTR_ERR(sai->clk_x8k);
160 	}
161 
162 	sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
163 	if (IS_ERR(sai->clk_x11k)) {
164 		dev_err(&pdev->dev, "missing x11k parent clock\n");
165 		return PTR_ERR(sai->clk_x11k);
166 	}
167 
168 	/* init irqs */
169 	sai->irq = platform_get_irq(pdev, 0);
170 	if (sai->irq < 0) {
171 		dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
172 		return sai->irq;
173 	}
174 
175 	/* reset */
176 	rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
177 	if (!IS_ERR(rst)) {
178 		reset_control_assert(rst);
179 		udelay(2);
180 		reset_control_deassert(rst);
181 	}
182 
183 	sai->pdev = pdev;
184 	sai->set_sync = &stm32_sai_set_sync;
185 	platform_set_drvdata(pdev, sai);
186 
187 	return of_platform_populate(np, NULL, NULL, &pdev->dev);
188 }
189 
190 static int stm32_sai_remove(struct platform_device *pdev)
191 {
192 	of_platform_depopulate(&pdev->dev);
193 
194 	return 0;
195 }
196 
197 MODULE_DEVICE_TABLE(of, stm32_sai_ids);
198 
199 static struct platform_driver stm32_sai_driver = {
200 	.driver = {
201 		.name = "st,stm32-sai",
202 		.of_match_table = stm32_sai_ids,
203 	},
204 	.probe = stm32_sai_probe,
205 	.remove = stm32_sai_remove,
206 };
207 
208 module_platform_driver(stm32_sai_driver);
209 
210 MODULE_DESCRIPTION("STM32 Soc SAI Interface");
211 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
212 MODULE_ALIAS("platform:st,stm32-sai");
213 MODULE_LICENSE("GPL v2");
214