1 // SPDX-License-Identifier: GPL-2.0-only 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. 7 */ 8 9 #include <linux/bitfield.h> 10 #include <linux/clk.h> 11 #include <linux/delay.h> 12 #include <linux/module.h> 13 #include <linux/of_platform.h> 14 #include <linux/pinctrl/consumer.h> 15 #include <linux/reset.h> 16 17 #include <sound/dmaengine_pcm.h> 18 #include <sound/core.h> 19 20 #include "stm32_sai.h" 21 22 static const struct stm32_sai_conf stm32_sai_conf_f4 = { 23 .version = STM_SAI_STM32F4, 24 .fifo_size = 8, 25 .has_spdif_pdm = false, 26 }; 27 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 */ 33 static const struct stm32_sai_conf stm32_sai_conf_h7 = { 34 .version = STM_SAI_STM32H7, 35 .fifo_size = 8, 36 .has_spdif_pdm = true, 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_pclk_disable(struct device *dev) 46 { 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); 57 int ret; 58 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 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 77 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCIN_MASK, (synci - 1)), sai->base); 78 79 stm32_sai_pclk_disable(&sai->pdev->dev); 80 81 return 0; 82 } 83 84 static int stm32_sai_sync_conf_provider(struct stm32_sai_data *sai, int synco) 85 { 86 u32 prev_synco; 87 int ret; 88 89 /* Enable peripheral clock to allow GCR register access */ 90 ret = stm32_sai_pclk_enable(&sai->pdev->dev); 91 if (ret) 92 return ret; 93 94 dev_dbg(&sai->pdev->dev, "Set %pOFn%s as synchro provider\n", 95 sai->pdev->dev.of_node, 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) { 100 dev_err(&sai->pdev->dev, "%pOFn%s already set as sync provider\n", 101 sai->pdev->dev.of_node, 102 prev_synco == STM_SAI_SYNC_OUT_A ? "A" : "B"); 103 stm32_sai_pclk_disable(&sai->pdev->dev); 104 return -EINVAL; 105 } 106 107 writel_relaxed(FIELD_PREP(SAI_GCR_SYNCOUT_MASK, synco), sai->base); 108 109 stm32_sai_pclk_disable(&sai->pdev->dev); 110 111 return 0; 112 } 113 114 static int stm32_sai_set_sync(struct stm32_sai_data *sai_client, 115 struct device_node *np_provider, 116 int synco, int synci) 117 { 118 struct platform_device *pdev = of_find_device_by_node(np_provider); 119 struct stm32_sai_data *sai_provider; 120 int ret; 121 122 if (!pdev) { 123 dev_err(&sai_client->pdev->dev, 124 "Device not found for node %pOFn\n", np_provider); 125 of_node_put(np_provider); 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"); 133 ret = -EINVAL; 134 goto error; 135 } 136 137 /* Configure sync client */ 138 ret = stm32_sai_sync_conf_client(sai_client, synci); 139 if (ret < 0) 140 goto error; 141 142 /* Configure sync provider */ 143 ret = stm32_sai_sync_conf_provider(sai_provider, synco); 144 145 error: 146 put_device(&pdev->dev); 147 of_node_put(np_provider); 148 return ret; 149 } 150 151 static int stm32_sai_probe(struct platform_device *pdev) 152 { 153 struct stm32_sai_data *sai; 154 struct reset_control *rst; 155 struct resource *res; 156 const struct of_device_id *of_id; 157 u32 val; 158 int ret; 159 160 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 161 if (!sai) 162 return -ENOMEM; 163 164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 165 sai->base = devm_ioremap_resource(&pdev->dev, res); 166 if (IS_ERR(sai->base)) 167 return PTR_ERR(sai->base); 168 169 of_id = of_match_device(stm32_sai_ids, &pdev->dev); 170 if (of_id) 171 memcpy(&sai->conf, (const struct stm32_sai_conf *)of_id->data, 172 sizeof(struct stm32_sai_conf)); 173 else 174 return -EINVAL; 175 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 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 */ 204 rst = devm_reset_control_get_exclusive(&pdev->dev, NULL); 205 if (!IS_ERR(rst)) { 206 reset_control_assert(rst); 207 udelay(2); 208 reset_control_deassert(rst); 209 } 210 211 /* Enable peripheral clock to allow register access */ 212 ret = clk_prepare_enable(sai->pclk); 213 if (ret) { 214 dev_err(&pdev->dev, "failed to enable clock: %d\n", ret); 215 return ret; 216 } 217 218 val = FIELD_GET(SAI_IDR_ID_MASK, 219 readl_relaxed(sai->base + STM_SAI_IDR)); 220 if (val == SAI_IPIDR_NUMBER) { 221 val = readl_relaxed(sai->base + STM_SAI_HWCFGR); 222 sai->conf.fifo_size = FIELD_GET(SAI_HWCFGR_FIFO_SIZE, val); 223 sai->conf.has_spdif_pdm = !!FIELD_GET(SAI_HWCFGR_SPDIF_PDM, 224 val); 225 226 val = readl_relaxed(sai->base + STM_SAI_VERR); 227 sai->conf.version = val; 228 229 dev_dbg(&pdev->dev, "SAI version: %lu.%lu registered\n", 230 FIELD_GET(SAI_VERR_MAJ_MASK, val), 231 FIELD_GET(SAI_VERR_MIN_MASK, val)); 232 } 233 clk_disable_unprepare(sai->pclk); 234 235 sai->pdev = pdev; 236 sai->set_sync = &stm32_sai_set_sync; 237 platform_set_drvdata(pdev, sai); 238 239 return devm_of_platform_populate(&pdev->dev); 240 } 241 242 #ifdef CONFIG_PM_SLEEP 243 /* 244 * When pins are shared by two sai sub instances, pins have to be defined 245 * in sai parent node. In this case, pins state is not managed by alsa fw. 246 * These pins are managed in suspend/resume callbacks. 247 */ 248 static int stm32_sai_suspend(struct device *dev) 249 { 250 struct stm32_sai_data *sai = dev_get_drvdata(dev); 251 int ret; 252 253 ret = stm32_sai_pclk_enable(dev); 254 if (ret) 255 return ret; 256 257 sai->gcr = readl_relaxed(sai->base); 258 stm32_sai_pclk_disable(dev); 259 260 return pinctrl_pm_select_sleep_state(dev); 261 } 262 263 static int stm32_sai_resume(struct device *dev) 264 { 265 struct stm32_sai_data *sai = dev_get_drvdata(dev); 266 int ret; 267 268 ret = stm32_sai_pclk_enable(dev); 269 if (ret) 270 return ret; 271 272 writel_relaxed(sai->gcr, sai->base); 273 stm32_sai_pclk_disable(dev); 274 275 return pinctrl_pm_select_default_state(dev); 276 } 277 #endif /* CONFIG_PM_SLEEP */ 278 279 static const struct dev_pm_ops stm32_sai_pm_ops = { 280 SET_SYSTEM_SLEEP_PM_OPS(stm32_sai_suspend, stm32_sai_resume) 281 }; 282 283 MODULE_DEVICE_TABLE(of, stm32_sai_ids); 284 285 static struct platform_driver stm32_sai_driver = { 286 .driver = { 287 .name = "st,stm32-sai", 288 .of_match_table = stm32_sai_ids, 289 .pm = &stm32_sai_pm_ops, 290 }, 291 .probe = stm32_sai_probe, 292 }; 293 294 module_platform_driver(stm32_sai_driver); 295 296 MODULE_DESCRIPTION("STM32 Soc SAI Interface"); 297 MODULE_AUTHOR("Olivier Moysan <olivier.moysan@st.com>"); 298 MODULE_ALIAS("platform:st,stm32-sai"); 299 MODULE_LICENSE("GPL v2"); 300