34b40b8b82997ffc360f2c1e3bc1fedd55c2f047
[platform/kernel/linux-starfive.git] / sound / soc / meson / aiu.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2020 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
5
6 #include <linux/bitfield.h>
7 #include <linux/clk.h>
8 #include <linux/module.h>
9 #include <linux/of_platform.h>
10 #include <linux/regmap.h>
11 #include <linux/reset.h>
12 #include <sound/soc.h>
13 #include <sound/soc-dai.h>
14
15 #include <dt-bindings/sound/meson-aiu.h>
16 #include "aiu.h"
17 #include "aiu-fifo.h"
18
19 #define AIU_I2S_MISC_958_SRC_SHIFT 3
20
21 static const char * const aiu_spdif_encode_sel_texts[] = {
22         "SPDIF", "I2S",
23 };
24
25 static SOC_ENUM_SINGLE_DECL(aiu_spdif_encode_sel_enum, AIU_I2S_MISC,
26                             AIU_I2S_MISC_958_SRC_SHIFT,
27                             aiu_spdif_encode_sel_texts);
28
29 static const struct snd_kcontrol_new aiu_spdif_encode_mux =
30         SOC_DAPM_ENUM("SPDIF Buffer Src", aiu_spdif_encode_sel_enum);
31
32 static const struct snd_soc_dapm_widget aiu_cpu_dapm_widgets[] = {
33         SND_SOC_DAPM_MUX("SPDIF SRC SEL", SND_SOC_NOPM, 0, 0,
34                          &aiu_spdif_encode_mux),
35 };
36
37 static const struct snd_soc_dapm_route aiu_cpu_dapm_routes[] = {
38         { "I2S Encoder Playback", NULL, "I2S FIFO Playback" },
39         { "SPDIF SRC SEL", "SPDIF", "SPDIF FIFO Playback" },
40         { "SPDIF SRC SEL", "I2S", "I2S FIFO Playback" },
41         { "SPDIF Encoder Playback", NULL, "SPDIF SRC SEL" },
42 };
43
44 int aiu_of_xlate_dai_name(struct snd_soc_component *component,
45                           struct of_phandle_args *args,
46                           const char **dai_name,
47                           unsigned int component_id)
48 {
49         struct snd_soc_dai *dai;
50         int id;
51
52         if (args->args_count != 2)
53                 return -EINVAL;
54
55         if (args->args[0] != component_id)
56                 return -EINVAL;
57
58         id = args->args[1];
59
60         if (id < 0 || id >= component->num_dai)
61                 return -EINVAL;
62
63         for_each_component_dais(component, dai) {
64                 if (id == 0)
65                         break;
66                 id--;
67         }
68
69         *dai_name = dai->driver->name;
70
71         return 0;
72 }
73
74 int aiu_add_component(struct device *dev,
75                       const struct snd_soc_component_driver *component_driver,
76                       struct snd_soc_dai_driver *dai_drv,
77                       int num_dai,
78                       const char *debugfs_prefix)
79 {
80         struct snd_soc_component *component;
81
82         component = devm_kzalloc(dev, sizeof(*component), GFP_KERNEL);
83         if (!component)
84                 return -ENOMEM;
85
86 #ifdef CONFIG_DEBUG_FS
87         component->debugfs_prefix = debugfs_prefix;
88 #endif
89
90         return snd_soc_add_component(dev, component, component_driver,
91                                      dai_drv, num_dai);
92 }
93
94 static int aiu_cpu_of_xlate_dai_name(struct snd_soc_component *component,
95                                      struct of_phandle_args *args,
96                                      const char **dai_name)
97 {
98         return aiu_of_xlate_dai_name(component, args, dai_name, AIU_CPU);
99 }
100
101 static int aiu_cpu_component_probe(struct snd_soc_component *component)
102 {
103         struct aiu *aiu = snd_soc_component_get_drvdata(component);
104
105         /* Required for the SPDIF Source control operation */
106         return clk_prepare_enable(aiu->i2s.clks[PCLK].clk);
107 }
108
109 static void aiu_cpu_component_remove(struct snd_soc_component *component)
110 {
111         struct aiu *aiu = snd_soc_component_get_drvdata(component);
112
113         clk_disable_unprepare(aiu->i2s.clks[PCLK].clk);
114 }
115
116 static const struct snd_soc_component_driver aiu_cpu_component = {
117         .name                   = "AIU CPU",
118         .dapm_widgets           = aiu_cpu_dapm_widgets,
119         .num_dapm_widgets       = ARRAY_SIZE(aiu_cpu_dapm_widgets),
120         .dapm_routes            = aiu_cpu_dapm_routes,
121         .num_dapm_routes        = ARRAY_SIZE(aiu_cpu_dapm_routes),
122         .of_xlate_dai_name      = aiu_cpu_of_xlate_dai_name,
123         .pointer                = aiu_fifo_pointer,
124         .probe                  = aiu_cpu_component_probe,
125         .remove                 = aiu_cpu_component_remove,
126 };
127
128 static struct snd_soc_dai_driver aiu_cpu_dai_drv[] = {
129         [CPU_I2S_FIFO] = {
130                 .name = "I2S FIFO",
131                 .playback = {
132                         .stream_name    = "I2S FIFO Playback",
133                         .channels_min   = 2,
134                         .channels_max   = 8,
135                         .rates          = SNDRV_PCM_RATE_CONTINUOUS,
136                         .rate_min       = 5512,
137                         .rate_max       = 192000,
138                         .formats        = AIU_FORMATS,
139                 },
140                 .ops            = &aiu_fifo_i2s_dai_ops,
141                 .pcm_new        = aiu_fifo_pcm_new,
142                 .probe          = aiu_fifo_i2s_dai_probe,
143                 .remove         = aiu_fifo_dai_remove,
144         },
145         [CPU_SPDIF_FIFO] = {
146                 .name = "SPDIF FIFO",
147                 .playback = {
148                         .stream_name    = "SPDIF FIFO Playback",
149                         .channels_min   = 2,
150                         .channels_max   = 2,
151                         .rates          = SNDRV_PCM_RATE_CONTINUOUS,
152                         .rate_min       = 5512,
153                         .rate_max       = 192000,
154                         .formats        = AIU_FORMATS,
155                 },
156                 .ops            = &aiu_fifo_spdif_dai_ops,
157                 .pcm_new        = aiu_fifo_pcm_new,
158                 .probe          = aiu_fifo_spdif_dai_probe,
159                 .remove         = aiu_fifo_dai_remove,
160         },
161         [CPU_I2S_ENCODER] = {
162                 .name = "I2S Encoder",
163                 .playback = {
164                         .stream_name = "I2S Encoder Playback",
165                         .channels_min = 2,
166                         .channels_max = 8,
167                         .rates = SNDRV_PCM_RATE_8000_192000,
168                         .formats = AIU_FORMATS,
169                 },
170                 .ops = &aiu_encoder_i2s_dai_ops,
171         },
172         [CPU_SPDIF_ENCODER] = {
173                 .name = "SPDIF Encoder",
174                 .playback = {
175                         .stream_name = "SPDIF Encoder Playback",
176                         .channels_min = 2,
177                         .channels_max = 2,
178                         .rates = (SNDRV_PCM_RATE_32000  |
179                                   SNDRV_PCM_RATE_44100  |
180                                   SNDRV_PCM_RATE_48000  |
181                                   SNDRV_PCM_RATE_88200  |
182                                   SNDRV_PCM_RATE_96000  |
183                                   SNDRV_PCM_RATE_176400 |
184                                   SNDRV_PCM_RATE_192000),
185                         .formats = AIU_FORMATS,
186                 },
187                 .ops = &aiu_encoder_spdif_dai_ops,
188         }
189 };
190
191 static const struct regmap_config aiu_regmap_cfg = {
192         .reg_bits       = 32,
193         .val_bits       = 32,
194         .reg_stride     = 4,
195         .max_register   = 0x2ac,
196 };
197
198 static int aiu_clk_bulk_get(struct device *dev,
199                             const char * const *ids,
200                             unsigned int num,
201                             struct aiu_interface *interface)
202 {
203         struct clk_bulk_data *clks;
204         int i, ret;
205
206         clks = devm_kcalloc(dev, num, sizeof(*clks), GFP_KERNEL);
207         if (!clks)
208                 return -ENOMEM;
209
210         for (i = 0; i < num; i++)
211                 clks[i].id = ids[i];
212
213         ret = devm_clk_bulk_get(dev, num, clks);
214         if (ret < 0)
215                 return ret;
216
217         interface->clks = clks;
218         interface->clk_num = num;
219         return 0;
220 }
221
222 static const char * const aiu_i2s_ids[] = {
223         [PCLK]  = "i2s_pclk",
224         [AOCLK] = "i2s_aoclk",
225         [MCLK]  = "i2s_mclk",
226         [MIXER] = "i2s_mixer",
227 };
228
229 static const char * const aiu_spdif_ids[] = {
230         [PCLK]  = "spdif_pclk",
231         [AOCLK] = "spdif_aoclk",
232         [MCLK]  = "spdif_mclk_sel"
233 };
234
235 static int aiu_clk_get(struct device *dev)
236 {
237         struct aiu *aiu = dev_get_drvdata(dev);
238         int ret;
239
240         aiu->pclk = devm_clk_get(dev, "pclk");
241         if (IS_ERR(aiu->pclk)) {
242                 if (PTR_ERR(aiu->pclk) != -EPROBE_DEFER)
243                         dev_err(dev, "Can't get the aiu pclk\n");
244                 return PTR_ERR(aiu->pclk);
245         }
246
247         aiu->spdif_mclk = devm_clk_get(dev, "spdif_mclk");
248         if (IS_ERR(aiu->spdif_mclk)) {
249                 if (PTR_ERR(aiu->spdif_mclk) != -EPROBE_DEFER)
250                         dev_err(dev, "Can't get the aiu spdif master clock\n");
251                 return PTR_ERR(aiu->spdif_mclk);
252         }
253
254         ret = aiu_clk_bulk_get(dev, aiu_i2s_ids, ARRAY_SIZE(aiu_i2s_ids),
255                                &aiu->i2s);
256         if (ret) {
257                 if (ret != -EPROBE_DEFER)
258                         dev_err(dev, "Can't get the i2s clocks\n");
259                 return ret;
260         }
261
262         ret = aiu_clk_bulk_get(dev, aiu_spdif_ids, ARRAY_SIZE(aiu_spdif_ids),
263                                &aiu->spdif);
264         if (ret) {
265                 if (ret != -EPROBE_DEFER)
266                         dev_err(dev, "Can't get the spdif clocks\n");
267                 return ret;
268         }
269
270         ret = clk_prepare_enable(aiu->pclk);
271         if (ret) {
272                 dev_err(dev, "peripheral clock enable failed\n");
273                 return ret;
274         }
275
276         ret = devm_add_action_or_reset(dev,
277                                        (void(*)(void *))clk_disable_unprepare,
278                                        aiu->pclk);
279         if (ret)
280                 dev_err(dev, "failed to add reset action on pclk");
281
282         return ret;
283 }
284
285 static int aiu_probe(struct platform_device *pdev)
286 {
287         struct device *dev = &pdev->dev;
288         void __iomem *regs;
289         struct regmap *map;
290         struct aiu *aiu;
291         int ret;
292
293         aiu = devm_kzalloc(dev, sizeof(*aiu), GFP_KERNEL);
294         if (!aiu)
295                 return -ENOMEM;
296         platform_set_drvdata(pdev, aiu);
297
298         ret = device_reset(dev);
299         if (ret) {
300                 if (ret != -EPROBE_DEFER)
301                         dev_err(dev, "Failed to reset device\n");
302                 return ret;
303         }
304
305         regs = devm_platform_ioremap_resource(pdev, 0);
306         if (IS_ERR(regs))
307                 return PTR_ERR(regs);
308
309         map = devm_regmap_init_mmio(dev, regs, &aiu_regmap_cfg);
310         if (IS_ERR(map)) {
311                 dev_err(dev, "failed to init regmap: %ld\n",
312                         PTR_ERR(map));
313                 return PTR_ERR(map);
314         }
315
316         aiu->i2s.irq = platform_get_irq_byname(pdev, "i2s");
317         if (aiu->i2s.irq < 0)
318                 return aiu->i2s.irq;
319
320         aiu->spdif.irq = platform_get_irq_byname(pdev, "spdif");
321         if (aiu->spdif.irq < 0)
322                 return aiu->spdif.irq;
323
324         ret = aiu_clk_get(dev);
325         if (ret)
326                 return ret;
327
328         /* Register the cpu component of the aiu */
329         ret = snd_soc_register_component(dev, &aiu_cpu_component,
330                                          aiu_cpu_dai_drv,
331                                          ARRAY_SIZE(aiu_cpu_dai_drv));
332         if (ret) {
333                 dev_err(dev, "Failed to register cpu component\n");
334                 return ret;
335         }
336
337         /* Register the hdmi codec control component */
338         ret = aiu_hdmi_ctrl_register_component(dev);
339         if (ret) {
340                 dev_err(dev, "Failed to register hdmi control component\n");
341                 goto err;
342         }
343
344         /* Register the internal dac control component on gxl */
345         if (of_device_is_compatible(dev->of_node, "amlogic,aiu-gxl")) {
346                 ret = aiu_acodec_ctrl_register_component(dev);
347                 if (ret) {
348                         dev_err(dev,
349                             "Failed to register acodec control component\n");
350                         goto err;
351                 }
352         }
353
354         return 0;
355 err:
356         snd_soc_unregister_component(dev);
357         return ret;
358 }
359
360 static int aiu_remove(struct platform_device *pdev)
361 {
362         snd_soc_unregister_component(&pdev->dev);
363
364         return 0;
365 }
366
367 static const struct of_device_id aiu_of_match[] = {
368         { .compatible = "amlogic,aiu-gxbb", },
369         { .compatible = "amlogic,aiu-gxl", },
370         {}
371 };
372 MODULE_DEVICE_TABLE(of, aiu_of_match);
373
374 static struct platform_driver aiu_pdrv = {
375         .probe = aiu_probe,
376         .remove = aiu_remove,
377         .driver = {
378                 .name = "meson-aiu",
379                 .of_match_table = aiu_of_match,
380         },
381 };
382 module_platform_driver(aiu_pdrv);
383
384 MODULE_DESCRIPTION("Meson AIU Driver");
385 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
386 MODULE_LICENSE("GPL v2");