blob: 7550d5f08be3d9c0c2bfc82a06adf00974e77f00 [file] [log] [blame]
olivier moysan3e086ed2017-04-10 17:19:56 +02001/*
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
Olivier Moysan5914d282017-10-19 15:03:23 +020019#include <linux/bitfield.h>
olivier moysan3e086ed2017-04-10 17:19:56 +020020#include <linux/clk.h>
21#include <linux/delay.h>
22#include <linux/module.h>
23#include <linux/of_platform.h>
Olivier Moysancf88177332019-03-21 16:34:56 +010024#include <linux/pinctrl/consumer.h>
olivier moysan3e086ed2017-04-10 17:19:56 +020025#include <linux/reset.h>
26
27#include <sound/dmaengine_pcm.h>
28#include <sound/core.h>
29
30#include "stm32_sai.h"
31
olivier moysan03e78a22017-06-16 14:16:24 +020032static const struct stm32_sai_conf stm32_sai_conf_f4 = {
33 .version = SAI_STM32F4,
Olivier Moysan6eb17d72018-02-19 16:00:37 +010034 .has_spdif = false,
olivier moysan03e78a22017-06-16 14:16:24 +020035};
36
37static const struct stm32_sai_conf stm32_sai_conf_h7 = {
38 .version = SAI_STM32H7,
Olivier Moysan6eb17d72018-02-19 16:00:37 +010039 .has_spdif = true,
olivier moysan03e78a22017-06-16 14:16:24 +020040};
41
olivier moysan3e086ed2017-04-10 17:19:56 +020042static const struct of_device_id stm32_sai_ids[] = {
olivier moysan03e78a22017-06-16 14:16:24 +020043 { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 },
44 { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 },
olivier moysan3e086ed2017-04-10 17:19:56 +020045 {}
46};
47
Olivier Moysancf88177332019-03-21 16:34:56 +010048static int stm32_sai_pclk_disable(struct device *dev)
Olivier Moysan5914d282017-10-19 15:03:23 +020049{
Olivier Moysancf88177332019-03-21 16:34:56 +010050 struct stm32_sai_data *sai = dev_get_drvdata(dev);
51
52 clk_disable_unprepare(sai->pclk);
53
54 return 0;
55}
56
57static int stm32_sai_pclk_enable(struct device *dev)
58{
59 struct stm32_sai_data *sai = dev_get_drvdata(dev);
Olivier Moysan5914d282017-10-19 15:03:23 +020060 int ret;
61
Olivier Moysan5914d282017-10-19 15:03:23 +020062 ret = clk_prepare_enable(sai->pclk);
63 if (ret) {
64 dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret);
65 return ret;
66 }
67
Olivier Moysancf88177332019-03-21 16:34:56 +010068 return 0;
69}
70
71static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci)
72{
73 int ret;
74
75 /* Enable peripheral clock to allow GCR register access */
76 ret = stm32_sai_pclk_enable(&sai->pdev->dev);
77 if (ret)
78 return ret;
79
Olivier Moysan5914d282017-10-19 15:03:23 +020080 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base);
81
Olivier Moysancf88177332019-03-21 16:34:56 +010082 stm32_sai_pclk_disable(&sai->pdev->dev);
Olivier Moysan5914d282017-10-19 15:03:23 +020083
84 return 0;
85}
86
olivier moysan7dd0d832017-11-22 16:02:26 +010087static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco)
Olivier Moysan5914d282017-10-19 15:03:23 +020088{
Olivier Moysan5914d282017-10-19 15:03:23 +020089 u32 prev_synco;
90 int ret;
91
92 /* Enable peripheral clock to allow GCR register access */
Olivier Moysancf88177332019-03-21 16:34:56 +010093 ret = stm32_sai_pclk_enable(&sai->pdev->dev);
94 if (ret)
Olivier Moysan5914d282017-10-19 15:03:23 +020095 return ret;
Olivier Moysan5914d282017-10-19 15:03:23 +020096
Rob Herringdc43d3a2018-11-16 15:43:49 -060097 dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n",
98 sai->pdev->dev.of_node,
Olivier Moysan5914d282017-10-19 15:03:23 +020099 synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
100
101 prev_synco = FIELD_GET(SAI_GCR_SYNCOUT_MASK, readl_relaxed(sai->base));
102 if (prev_synco != STM_SAI_SYNC_OUT_NONE && synco != prev_synco) {
Rob Herringdc43d3a2018-11-16 15:43:49 -0600103 dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n",
104 sai->pdev->dev.of_node,
Olivier Moysan5914d282017-10-19 15:03:23 +0200105 prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B");
Olivier Moysancf88177332019-03-21 16:34:56 +0100106 stm32_sai_pclk_disable(&sai->pdev->dev);
Olivier Moysan5914d282017-10-19 15:03:23 +0200107 return -EINVAL;
108 }
109
110 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base);
111
Olivier Moysancf88177332019-03-21 16:34:56 +0100112 stm32_sai_pclk_disable(&sai->pdev->dev);
Olivier Moysan5914d282017-10-19 15:03:23 +0200113
114 return 0;
115}
116
olivier moysan7dd0d832017-11-22 16:02:26 +0100117static int stm32_sai_set_sync(struct stm32_sai_data *sai_client,
Olivier Moysan5914d282017-10-19 15:03:23 +0200118 struct device_node *np_provider,
119 int synco, int synci)
120{
olivier moysan7dd0d832017-11-22 16:02:26 +0100121 struct platform_device *pdev = of_find_device_by_node(np_provider);
122 struct stm32_sai_data *sai_provider;
Olivier Moysan5914d282017-10-19 15:03:23 +0200123 int ret;
124
olivier moysan7dd0d832017-11-22 16:02:26 +0100125 if (!pdev) {
126 dev_err(&sai_client->pdev->dev,
Rob Herring5d585e12018-08-28 10:44:28 -0500127 "Device not found for node %pOFn\n", np_provider);
Olivier Moysand4180b42019-02-28 14:19:25 +0100128 of_node_put(np_provider);
olivier moysan7dd0d832017-11-22 16:02:26 +0100129 return -ENODEV;
130 }
131
132 sai_provider = platform_get_drvdata(pdev);
133 if (!sai_provider) {
134 dev_err(&sai_client->pdev->dev,
135 "SAI sync provider data not found\n");
Wen Yang1c3816a2019-02-09 10:41:09 +0000136 ret = -EINVAL;
Olivier Moysand4180b42019-02-28 14:19:25 +0100137 goto error;
olivier moysan7dd0d832017-11-22 16:02:26 +0100138 }
139
Olivier Moysan5914d282017-10-19 15:03:23 +0200140 /* Configure sync client */
olivier moysan7dd0d832017-11-22 16:02:26 +0100141 ret = stm32_sai_sync_conf_client(sai_client, synci);
142 if (ret < 0)
Olivier Moysand4180b42019-02-28 14:19:25 +0100143 goto error;
Olivier Moysan5914d282017-10-19 15:03:23 +0200144
145 /* Configure sync provider */
Wen Yang1c3816a2019-02-09 10:41:09 +0000146 ret = stm32_sai_sync_conf_provider(sai_provider, synco);
147
Olivier Moysand4180b42019-02-28 14:19:25 +0100148error:
Wen Yang1c3816a2019-02-09 10:41:09 +0000149 put_device(&pdev->dev);
Olivier Moysand4180b42019-02-28 14:19:25 +0100150 of_node_put(np_provider);
Wen Yang1c3816a2019-02-09 10:41:09 +0000151 return ret;
Olivier Moysan5914d282017-10-19 15:03:23 +0200152}
153
olivier moysan3e086ed2017-04-10 17:19:56 +0200154static int stm32_sai_probe(struct platform_device *pdev)
155{
olivier moysan3e086ed2017-04-10 17:19:56 +0200156 struct stm32_sai_data *sai;
157 struct reset_control *rst;
158 struct resource *res;
olivier moysan3e086ed2017-04-10 17:19:56 +0200159 const struct of_device_id *of_id;
160
161 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL);
162 if (!sai)
163 return -ENOMEM;
164
165 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Olivier Moysan5914d282017-10-19 15:03:23 +0200166 sai->base = devm_ioremap_resource(&pdev->dev, res);
167 if (IS_ERR(sai->base))
168 return PTR_ERR(sai->base);
olivier moysan3e086ed2017-04-10 17:19:56 +0200169
170 of_id = of_match_device(stm32_sai_ids, &pdev->dev);
171 if (of_id)
olivier moysan03e78a22017-06-16 14:16:24 +0200172 sai->conf = (struct stm32_sai_conf *)of_id->data;
olivier moysan3e086ed2017-04-10 17:19:56 +0200173 else
174 return -EINVAL;
175
Olivier Moysan5914d282017-10-19 15:03:23 +0200176 if (!STM_SAI_IS_F4(sai)) {
177 sai->pclk = devm_clk_get(&pdev->dev, "pclk");
178 if (IS_ERR(sai->pclk)) {
179 dev_err(&pdev->dev, "missing bus clock pclk\n");
180 return PTR_ERR(sai->pclk);
181 }
182 }
183
olivier moysan3e086ed2017-04-10 17:19:56 +0200184 sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k");
185 if (IS_ERR(sai->clk_x8k)) {
186 dev_err(&pdev->dev, "missing x8k parent clock\n");
187 return PTR_ERR(sai->clk_x8k);
188 }
189
190 sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k");
191 if (IS_ERR(sai->clk_x11k)) {
192 dev_err(&pdev->dev, "missing x11k parent clock\n");
193 return PTR_ERR(sai->clk_x11k);
194 }
195
196 /* init irqs */
197 sai->irq = platform_get_irq(pdev, 0);
198 if (sai->irq < 0) {
199 dev_err(&pdev->dev, "no irq for node %s\n", pdev->name);
200 return sai->irq;
201 }
202
203 /* reset */
Olivier Moysan3c6f6c52017-10-19 15:03:22 +0200204 rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
olivier moysan3e086ed2017-04-10 17:19:56 +0200205 if (!IS_ERR(rst)) {
206 reset_control_assert(rst);
207 udelay(2);
208 reset_control_deassert(rst);
209 }
210
211 sai->pdev = pdev;
olivier moysan7dd0d832017-11-22 16:02:26 +0100212 sai->set_sync = &stm32_sai_set_sync;
olivier moysan3e086ed2017-04-10 17:19:56 +0200213 platform_set_drvdata(pdev, sai);
214
olivier moysan512d1bb2017-11-22 16:02:27 +0100215 return devm_of_platform_populate(&pdev->dev);
olivier moysan3e086ed2017-04-10 17:19:56 +0200216}
217
Olivier Moysancf88177332019-03-21 16:34:56 +0100218#ifdef CONFIG_PM_SLEEP
219/*
220 * When pins are shared by two sai sub instances, pins have to be defined
221 * in sai parent node. In this case, pins state is not managed by alsa fw.
222 * These pins are managed in suspend/resume callbacks.
223 */
224static int stm32_sai_suspend(struct device *dev)
225{
226 struct stm32_sai_data *sai = dev_get_drvdata(dev);
227 int ret;
228
229 ret = stm32_sai_pclk_enable(dev);
230 if (ret)
231 return ret;
232
233 sai->gcr = readl_relaxed(sai->base);
234 stm32_sai_pclk_disable(dev);
235
236 return pinctrl_pm_select_sleep_state(dev);
237}
238
239static int stm32_sai_resume(struct device *dev)
240{
241 struct stm32_sai_data *sai = dev_get_drvdata(dev);
242 int ret;
243
244 ret = stm32_sai_pclk_enable(dev);
245 if (ret)
246 return ret;
247
248 writel_relaxed(sai->gcr, sai->base);
249 stm32_sai_pclk_disable(dev);
250
251 return pinctrl_pm_select_default_state(dev);
252}
253#endif /* CONFIG_PM_SLEEP */
254
255static const struct dev_pm_ops stm32_sai_pm_ops = {
256 SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume)
257};
258
olivier moysan3e086ed2017-04-10 17:19:56 +0200259MODULE_DEVICE_TABLE(of, stm32_sai_ids);
260
261static struct platform_driver stm32_sai_driver = {
262 .driver = {
263 .name = "st,stm32-sai",
264 .of_match_table = stm32_sai_ids,
Olivier Moysancf88177332019-03-21 16:34:56 +0100265 .pm = &stm32_sai_pm_ops,
olivier moysan3e086ed2017-04-10 17:19:56 +0200266 },
267 .probe = stm32_sai_probe,
olivier moysan3e086ed2017-04-10 17:19:56 +0200268};
269
270module_platform_driver(stm32_sai_driver);
271
272MODULE_DESCRIPTION("STM32 Soc SAI Interface");
olivier moysan602fdad2017-06-16 14:15:30 +0200273MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>");
olivier moysan3e086ed2017-04-10 17:19:56 +0200274MODULE_ALIAS("platform:st,stm32-sai");
275MODULE_LICENSE("GPL v2");