olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 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 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 19 | #include <linux/bitfield.h> |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 20 | #include <linux/clk.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/of_platform.h> |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 24 | #include <linux/pinctrl/consumer.h> |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 25 | #include <linux/reset.h> |
| 26 | |
| 27 | #include <sound/dmaengine_pcm.h> |
| 28 | #include <sound/core.h> |
| 29 | |
| 30 | #include "stm32_sai.h" |
| 31 | |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 32 | static const struct stm32_sai_conf stm32_sai_conf_f4 = { |
| 33 | .version = SAI_STM32F4, |
Olivier Moysan | 6eb17d7 | 2018-02-19 16:00:37 +0100 | [diff] [blame] | 34 | .has_spdif = false, |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static const struct stm32_sai_conf stm32_sai_conf_h7 = { |
| 38 | .version = SAI_STM32H7, |
Olivier Moysan | 6eb17d7 | 2018-02-19 16:00:37 +0100 | [diff] [blame] | 39 | .has_spdif = true, |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 42 | static const struct of_device_id stm32_sai_ids[] = { |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 43 | { .compatible = "st,stm32f4-sai", .data = (void *)&stm32_sai_conf_f4 }, |
| 44 | { .compatible = "st,stm32h7-sai", .data = (void *)&stm32_sai_conf_h7 }, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 45 | {} |
| 46 | }; |
| 47 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 48 | static int stm32_sai_pclk_disable(struct device *dev) |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 49 | { |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 50 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
| 51 | |
| 52 | clk_disable_unprepare(sai->pclk); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | static int stm32_sai_pclk_enable(struct device *dev) |
| 58 | { |
| 59 | struct stm32_sai_data *sai = dev_get_drvdata(dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 60 | int ret; |
| 61 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 62 | 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 Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | static 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 Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 80 | writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base); |
| 81 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 82 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 87 | 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] | 88 | { |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 89 | u32 prev_synco; |
| 90 | int ret; |
| 91 | |
| 92 | /* Enable peripheral clock to allow GCR register access */ |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 93 | ret = stm32_sai_pclk_enable(&sai->pdev->dev); |
| 94 | if (ret) |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 95 | return ret; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 96 | |
Rob Herring | dc43d3a | 2018-11-16 15:43:49 -0600 | [diff] [blame] | 97 | dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n", |
| 98 | sai->pdev->dev.of_node, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 99 | 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 Herring | dc43d3a | 2018-11-16 15:43:49 -0600 | [diff] [blame] | 103 | dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n", |
| 104 | sai->pdev->dev.of_node, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 105 | prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B"); |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 106 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 107 | return -EINVAL; |
| 108 | } |
| 109 | |
| 110 | writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base); |
| 111 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 112 | stm32_sai_pclk_disable(&sai->pdev->dev); |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 117 | static int stm32_sai_set_sync(struct stm32_sai_data *sai_client, |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 118 | struct device_node *np_provider, |
| 119 | int synco, int synci) |
| 120 | { |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 121 | struct platform_device *pdev = of_find_device_by_node(np_provider); |
| 122 | struct stm32_sai_data *sai_provider; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 123 | int ret; |
| 124 | |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 125 | if (!pdev) { |
| 126 | dev_err(&sai_client->pdev->dev, |
Rob Herring | 5d585e1 | 2018-08-28 10:44:28 -0500 | [diff] [blame] | 127 | "Device not found for node %pOFn\n", np_provider); |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 128 | of_node_put(np_provider); |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 129 | 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 Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 136 | ret = -EINVAL; |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 137 | goto error; |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 140 | /* Configure sync client */ |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 141 | ret = stm32_sai_sync_conf_client(sai_client, synci); |
| 142 | if (ret < 0) |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 143 | goto error; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 144 | |
| 145 | /* Configure sync provider */ |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 146 | ret = stm32_sai_sync_conf_provider(sai_provider, synco); |
| 147 | |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 148 | error: |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 149 | put_device(&pdev->dev); |
Olivier Moysan | d4180b4 | 2019-02-28 14:19:25 +0100 | [diff] [blame] | 150 | of_node_put(np_provider); |
Wen Yang | 1c3816a | 2019-02-09 10:41:09 +0000 | [diff] [blame] | 151 | return ret; |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 152 | } |
| 153 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 154 | static int stm32_sai_probe(struct platform_device *pdev) |
| 155 | { |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 156 | struct stm32_sai_data *sai; |
| 157 | struct reset_control *rst; |
| 158 | struct resource *res; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 159 | 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 Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 166 | sai->base = devm_ioremap_resource(&pdev->dev, res); |
| 167 | if (IS_ERR(sai->base)) |
| 168 | return PTR_ERR(sai->base); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 169 | |
| 170 | of_id = of_match_device(stm32_sai_ids, &pdev->dev); |
| 171 | if (of_id) |
olivier moysan | 03e78a2 | 2017-06-16 14:16:24 +0200 | [diff] [blame] | 172 | sai->conf = (struct stm32_sai_conf *)of_id->data; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 173 | else |
| 174 | return -EINVAL; |
| 175 | |
Olivier Moysan | 5914d28 | 2017-10-19 15:03:23 +0200 | [diff] [blame] | 176 | 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 moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 184 | 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 Moysan | 3c6f6c5 | 2017-10-19 15:03:22 +0200 | [diff] [blame] | 204 | rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 205 | if (!IS_ERR(rst)) { |
| 206 | reset_control_assert(rst); |
| 207 | udelay(2); |
| 208 | reset_control_deassert(rst); |
| 209 | } |
| 210 | |
| 211 | sai->pdev = pdev; |
olivier moysan | 7dd0d83 | 2017-11-22 16:02:26 +0100 | [diff] [blame] | 212 | sai->set_sync = &stm32_sai_set_sync; |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 213 | platform_set_drvdata(pdev, sai); |
| 214 | |
olivier moysan | 512d1bb | 2017-11-22 16:02:27 +0100 | [diff] [blame] | 215 | return devm_of_platform_populate(&pdev->dev); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 218 | #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 | */ |
| 224 | static 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 | |
| 239 | static 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 | |
| 255 | static const struct dev_pm_ops stm32_sai_pm_ops = { |
| 256 | SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume) |
| 257 | }; |
| 258 | |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 259 | MODULE_DEVICE_TABLE(of, stm32_sai_ids); |
| 260 | |
| 261 | static struct platform_driver stm32_sai_driver = { |
| 262 | .driver = { |
| 263 | .name = "st,stm32-sai", |
| 264 | .of_match_table = stm32_sai_ids, |
Olivier Moysan | cf8817733 | 2019-03-21 16:34:56 +0100 | [diff] [blame] | 265 | .pm = &stm32_sai_pm_ops, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 266 | }, |
| 267 | .probe = stm32_sai_probe, |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | module_platform_driver(stm32_sai_driver); |
| 271 | |
| 272 | MODULE_DESCRIPTION("STM32 Soc SAI Interface"); |
olivier moysan | 602fdad | 2017-06-16 14:15:30 +0200 | [diff] [blame] | 273 | MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); |
olivier moysan | 3e086ed | 2017-04-10 17:19:56 +0200 | [diff] [blame] | 274 | MODULE_ALIAS("platform:st,stm32-sai"); |
| 275 | MODULE_LICENSE("GPL v2"); |