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