Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 2 | /* |
| 3 | * STM32 ALSA SoC Digital Audio Interface (SAI) driver. |
| 4 | * |
| 5 | * Copyright (C) 2016, STMicroelectronics - All Rights Reserved |
| 6 | * Author(s): Olivier Moysan <olivier.moysan@st.com> for STMicroelectronics. |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 9 | #include <linux/bitfield.h> |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_platform.h> |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 14 | #include <linux/pinctrl/consumer.h> |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 15 | #include <linux/reset.h> |
| 16 | |
| 17 | #include <sound/dmaengine_pcm.h> |
| 18 | #include <sound/core.h> |
| 19 | |
| 20 | #include "stm32_sai.h" |
| 21 | |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 22 | static const struct stm32_sai_conf stm32_sai_conf_f4 = { |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 23 | .version = STM_SAI_STM32F4, |
| 24 | .fifo_size = 8, |
| 25 | .has_spdif_pdm = false, |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 28 | /* |
| 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. |
| 32 | */ |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 33 | static const struct stm32_sai_conf stm32_sai_conf_h7 = { |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 34 | .version = STM_SAI_STM32H7, |
| 35 | .fifo_size = 8, |
| 36 | .has_spdif_pdm = true, |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 39 | static const struct of_device_id stm32_sai_ids[] = { |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 40 | { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 }, |
| 41 | { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 }, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 42 | {} |
| 43 | }; |
| 44 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 45 | static int stm32_sai_pclk_disable(struct device *dev) |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 46 | { |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 47 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
| 48 | |
| 49 | clk_disable_unprepare(sai->pclk); |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int stm32_sai_pclk_enable(struct device *dev) |
| 55 | { |
| 56 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 57 | int ret; |
| 58 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 59 | ret = clk_prepare_enable(sai->pclk); |
| 60 | if (ret) { |
| 61 | dev_err(&sai->pdev->dev, "failed to enable clock: %d\n", ret); |
| 62 | return ret; |
| 63 | } |
| 64 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int stm32_sai_sync_conf_client(struct stm32_sai_data *sai, int synci) |
| 69 | { |
| 70 | int ret; |
| 71 | |
| 72 | /* Enable peripheral clock to allow GCR register access */ |
| 73 | ret = stm32_sai_pclk_enable(&sai->pdev->dev); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 77 | writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base); |
| 78 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 79 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 84 | static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco) |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 85 | { |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 86 | u32 prev_synco; |
| 87 | int ret; |
| 88 | |
| 89 | /* Enable peripheral clock to allow GCR register access */ |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 90 | ret = stm32_sai_pclk_enable(&sai->pdev->dev); |
| 91 | if (ret) |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 92 | return ret; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 93 | |
Rob Herring | dc43d3a | 2018-11-16 15:43:49 -0600 | [diff] [blame] | 94 | dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n", |
| 95 | sai->pdev->dev.of_node, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 96 | synco == STM_SAI_SYNC_OUT_A ? "A" : "B"); |
| 97 | |
| 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) { |
Rob Herring | dc43d3a | 2018-11-16 15:43:49 -0600 | [diff] [blame] | 100 | dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n", |
| 101 | sai->pdev->dev.of_node, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 102 | prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B"); |
Colin Ian King | 423013f | 2019-09-25 12:26:21 +0100 | [diff] [blame] | 103 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base); |
| 108 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 109 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 114 | static int stm32_sai_set_sync(struct stm32_sai_data *sai_client, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 115 | struct device_node *np_provider, |
| 116 | int synco, int synci) |
| 117 | { |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 118 | struct platform_device *pdev = of_find_device_by_node(np_provider); |
| 119 | struct stm32_sai_data *sai_provider; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 120 | int ret; |
| 121 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 122 | if (!pdev) { |
| 123 | dev_err(&sai_client->pdev->dev, |
Rob Herring | 5d585e1 | 2018-08-28 10:44:28 -0500 | [diff] [blame] | 124 | "Device not found for node %pOFn\n", np_provider); |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 125 | of_node_put(np_provider); |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 126 | return -ENODEV; |
| 127 | } |
| 128 | |
| 129 | sai_provider = platform_get_drvdata(pdev); |
| 130 | if (!sai_provider) { |
| 131 | dev_err(&sai_client->pdev->dev, |
| 132 | "SAI sync provider data not found\n"); |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 133 | ret = -EINVAL; |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 134 | goto error; |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 137 | /* Configure sync client */ |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 138 | ret = stm32_sai_sync_conf_client(sai_client, synci); |
| 139 | if (ret < 0) |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 140 | goto error; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 141 | |
| 142 | /* Configure sync provider */ |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 143 | ret = stm32_sai_sync_conf_provider(sai_provider, synco); |
| 144 | |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 145 | error: |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 146 | put_device(&pdev->dev); |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 147 | of_node_put(np_provider); |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 148 | return ret; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 149 | } |
| 150 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 151 | static int stm32_sai_probe(struct platform_device *pdev) |
| 152 | { |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 153 | struct stm32_sai_data *sai; |
| 154 | struct reset_control *rst; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 155 | const struct of_device_id *of_id; |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 156 | u32 val; |
| 157 | int ret; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 158 | |
| 159 | sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); |
| 160 | if (!sai) |
| 161 | return -ENOMEM; |
| 162 | |
YueHaibing | 1c63223 | 2019-07-27 23:07:19 +0800 | [diff] [blame] | 163 | sai->base = devm_platform_ioremap_resource(pdev, 0); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 164 | if (IS_ERR(sai->base)) |
| 165 | return PTR_ERR(sai->base); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 166 | |
| 167 | of_id = of_match_device(stm32_sai_ids, &pdev->dev); |
| 168 | if (of_id) |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 169 | memcpy(&sai->conf, (const struct stm32_sai_conf *)of_id->data, |
| 170 | sizeof(struct stm32_sai_conf)); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 171 | else |
| 172 | return -EINVAL; |
| 173 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 174 | if (!STM_SAI_IS_F4(sai)) { |
| 175 | sai->pclk = devm_clk_get(&pdev->dev, "pclk"); |
Kuninori Morimoto | efc162c | 2021-12-14 11:08:42 +0900 | [diff] [blame] | 176 | if (IS_ERR(sai->pclk)) |
| 177 | return dev_err_probe(&pdev->dev, PTR_ERR(sai->pclk), |
| 178 | "missing bus clock pclk\n"); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 179 | } |
| 180 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 181 | sai->clk_x8k = devm_clk_get(&pdev->dev, "x8k"); |
Kuninori Morimoto | efc162c | 2021-12-14 11:08:42 +0900 | [diff] [blame] | 182 | if (IS_ERR(sai->clk_x8k)) |
| 183 | return dev_err_probe(&pdev->dev, PTR_ERR(sai->clk_x8k), |
| 184 | "missing x8k parent clock\n"); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 185 | |
| 186 | sai->clk_x11k = devm_clk_get(&pdev->dev, "x11k"); |
Kuninori Morimoto | efc162c | 2021-12-14 11:08:42 +0900 | [diff] [blame] | 187 | if (IS_ERR(sai->clk_x11k)) |
| 188 | return dev_err_probe(&pdev->dev, PTR_ERR(sai->clk_x11k), |
| 189 | "missing x11k parent clock\n"); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 190 | |
| 191 | /* init irqs */ |
| 192 | sai->irq = platform_get_irq(pdev, 0); |
Stephen Boyd | cf9441a | 2019-07-30 11:15:49 -0700 | [diff] [blame] | 193 | if (sai->irq < 0) |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 194 | return sai->irq; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 195 | |
| 196 | /* reset */ |
Olivier Moysan | 028476c | 2020-02-03 11:08:09 +0100 | [diff] [blame] | 197 | rst = devm_reset_control_get_optional_exclusive(&pdev->dev, NULL); |
Kuninori Morimoto | efc162c | 2021-12-14 11:08:42 +0900 | [diff] [blame] | 198 | if (IS_ERR(rst)) |
| 199 | return dev_err_probe(&pdev->dev, PTR_ERR(rst), |
| 200 | "Reset controller error\n"); |
| 201 | |
Olivier Moysan | 028476c | 2020-02-03 11:08:09 +0100 | [diff] [blame] | 202 | reset_control_assert(rst); |
| 203 | udelay(2); |
| 204 | reset_control_deassert(rst); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 205 | |
Olivier Moysan | 1d9c95c | 2019-06-03 10:16:34 +0200 | [diff] [blame] | 206 | /* Enable peripheral clock to allow register access */ |
| 207 | ret = clk_prepare_enable(sai->pclk); |
| 208 | if (ret) { |
| 209 | dev_err(&pdev->dev, "failed to enable clock: %d\n", ret); |
| 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | val = FIELD_GET(SAI_IDR_ID_MASK, |
| 214 | readl_relaxed(sai->base + STM_SAI_IDR)); |
| 215 | if (val == SAI_IPIDR_NUMBER) { |
| 216 | val = readl_relaxed(sai->base + STM_SAI_HWCFGR); |
| 217 | sai->conf.fifo_size = FIELD_GET(SAI_HWCFGR_FIFO_SIZE, val); |
| 218 | sai->conf.has_spdif_pdm = !!FIELD_GET(SAI_HWCFGR_SPDIF_PDM, |
| 219 | val); |
| 220 | |
| 221 | val = readl_relaxed(sai->base + STM_SAI_VERR); |
| 222 | sai->conf.version = val; |
| 223 | |
| 224 | dev_dbg(&pdev->dev, "SAI version: %lu.%lu registered\n", |
| 225 | FIELD_GET(SAI_VERR_MAJ_MASK, val), |
| 226 | FIELD_GET(SAI_VERR_MIN_MASK, val)); |
| 227 | } |
| 228 | clk_disable_unprepare(sai->pclk); |
| 229 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 230 | sai->pdev = pdev; |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 231 | sai->set_sync = &stm32_sai_set_sync; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 232 | platform_set_drvdata(pdev, sai); |
| 233 | |
olivier moysan | 512d1bb | 2017-11-22 16:02:27 +0100 | [diff] [blame] | 234 | return devm_of_platform_populate(&pdev->dev); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 235 | } |
| 236 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 237 | #ifdef CONFIG_PM_SLEEP |
| 238 | /* |
| 239 | * When pins are shared by two sai sub instances, pins have to be defined |
| 240 | * in sai parent node. In this case, pins state is not managed by alsa fw. |
| 241 | * These pins are managed in suspend/resume callbacks. |
| 242 | */ |
| 243 | static int stm32_sai_suspend(struct device *dev) |
| 244 | { |
| 245 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
| 246 | int ret; |
| 247 | |
| 248 | ret = stm32_sai_pclk_enable(dev); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | sai->gcr = readl_relaxed(sai->base); |
| 253 | stm32_sai_pclk_disable(dev); |
| 254 | |
| 255 | return pinctrl_pm_select_sleep_state(dev); |
| 256 | } |
| 257 | |
| 258 | static int stm32_sai_resume(struct device *dev) |
| 259 | { |
| 260 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
| 261 | int ret; |
| 262 | |
| 263 | ret = stm32_sai_pclk_enable(dev); |
| 264 | if (ret) |
| 265 | return ret; |
| 266 | |
| 267 | writel_relaxed(sai->gcr, sai->base); |
| 268 | stm32_sai_pclk_disable(dev); |
| 269 | |
| 270 | return pinctrl_pm_select_default_state(dev); |
| 271 | } |
| 272 | #endif /* CONFIG_PM_SLEEP */ |
| 273 | |
| 274 | static const struct dev_pm_ops stm32_sai_pm_ops = { |
| 275 | SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume) |
| 276 | }; |
| 277 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 278 | MODULE_DEVICE_TABLE(of, stm32_sai_ids); |
| 279 | |
| 280 | static struct platform_driver stm32_sai_driver = { |
| 281 | .driver = { |
| 282 | .name = "st,stm32-sai", |
| 283 | .of_match_table = stm32_sai_ids, |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 284 | .pm = &stm32_sai_pm_ops, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 285 | }, |
| 286 | .probe = stm32_sai_probe, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 287 | }; |
| 288 | |
| 289 | module_platform_driver(stm32_sai_driver); |
| 290 | |
| 291 | MODULE_DESCRIPTION("STM32 Soc SAI Interface"); |
olivier moysan | 602fdad | 2017-06-16 14:15:30 +0200 | [diff] [blame] | 292 | MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 293 | MODULE_ALIAS("platform:st,stm32-sai"); |
| 294 | MODULE_LICENSE("GPL v2"); |