1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 // Copyright (c) 2018 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 /* This driver implements the frontend capture DAI of AXG based SoCs */
9 #include <linux/regmap.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <sound/pcm_params.h>
13 #include <sound/soc.h>
14 #include <sound/soc-dai.h>
18 #define CTRL0_TODDR_SEL_RESAMPLE BIT(30)
19 #define CTRL0_TODDR_EXT_SIGNED BIT(29)
20 #define CTRL0_TODDR_PP_MODE BIT(28)
21 #define CTRL0_TODDR_TYPE_MASK GENMASK(15, 13)
22 #define CTRL0_TODDR_TYPE(x) ((x) << 13)
23 #define CTRL0_TODDR_MSB_POS_MASK GENMASK(12, 8)
24 #define CTRL0_TODDR_MSB_POS(x) ((x) << 8)
25 #define CTRL0_TODDR_LSB_POS_MASK GENMASK(7, 3)
26 #define CTRL0_TODDR_LSB_POS(x) ((x) << 3)
28 static int axg_toddr_pcm_new(struct snd_soc_pcm_runtime *rtd,
29 struct snd_soc_dai *dai)
31 return axg_fifo_pcm_new(rtd, SNDRV_PCM_STREAM_CAPTURE);
34 static int axg_toddr_dai_hw_params(struct snd_pcm_substream *substream,
35 struct snd_pcm_hw_params *params,
36 struct snd_soc_dai *dai)
38 struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
39 unsigned int type, width, msb = 31;
43 * Almost all backend will place the MSB at bit 31, except SPDIF Input
44 * which will put it at index 28. When adding support for the SPDIF
45 * Input, we'll need to find which type of backend we are connected to.
48 switch (params_physical_width(params)) {
50 type = 0; /* 8 samples of 8 bits */
53 type = 2; /* 4 samples of 16 bits - right justified */
56 type = 4; /* 2 samples of 32 bits - right justified */
62 width = params_width(params);
64 regmap_update_bits(fifo->map, FIFO_CTRL0,
65 CTRL0_TODDR_TYPE_MASK |
66 CTRL0_TODDR_MSB_POS_MASK |
67 CTRL0_TODDR_LSB_POS_MASK,
68 CTRL0_TODDR_TYPE(type) |
69 CTRL0_TODDR_MSB_POS(msb) |
70 CTRL0_TODDR_LSB_POS(msb - (width - 1)));
75 static int axg_toddr_dai_startup(struct snd_pcm_substream *substream,
76 struct snd_soc_dai *dai)
78 struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
79 unsigned int fifo_threshold;
82 /* Enable pclk to access registers and clock the fifo ip */
83 ret = clk_prepare_enable(fifo->pclk);
87 /* Select orginal data - resampling not supported ATM */
88 regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_SEL_RESAMPLE, 0);
90 /* Only signed format are supported ATM */
91 regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_EXT_SIGNED,
92 CTRL0_TODDR_EXT_SIGNED);
94 /* Apply single buffer mode to the interface */
95 regmap_update_bits(fifo->map, FIFO_CTRL0, CTRL0_TODDR_PP_MODE, 0);
97 /* TODDR does not have a configurable fifo depth */
98 fifo_threshold = AXG_FIFO_MIN_CNT - 1;
99 regmap_update_bits(fifo->map, FIFO_CTRL1, CTRL1_THRESHOLD_MASK,
100 CTRL1_THRESHOLD(fifo_threshold));
105 static void axg_toddr_dai_shutdown(struct snd_pcm_substream *substream,
106 struct snd_soc_dai *dai)
108 struct axg_fifo *fifo = snd_soc_dai_get_drvdata(dai);
110 clk_disable_unprepare(fifo->pclk);
113 static const struct snd_soc_dai_ops axg_toddr_ops = {
114 .hw_params = axg_toddr_dai_hw_params,
115 .startup = axg_toddr_dai_startup,
116 .shutdown = axg_toddr_dai_shutdown,
119 static struct snd_soc_dai_driver axg_toddr_dai_drv = {
122 .stream_name = "Capture",
124 .channels_max = AXG_FIFO_CH_MAX,
125 .rates = AXG_FIFO_RATES,
126 .formats = AXG_FIFO_FORMATS,
128 .ops = &axg_toddr_ops,
129 .pcm_new = axg_toddr_pcm_new,
132 static const char * const axg_toddr_sel_texts[] = {
133 "IN 0", "IN 1", "IN 2", "IN 3", "IN 4", "IN 6"
136 static const unsigned int axg_toddr_sel_values[] = {
140 static SOC_VALUE_ENUM_SINGLE_DECL(axg_toddr_sel_enum, FIFO_CTRL0,
141 CTRL0_SEL_SHIFT, CTRL0_SEL_MASK,
142 axg_toddr_sel_texts, axg_toddr_sel_values);
144 static const struct snd_kcontrol_new axg_toddr_in_mux =
145 SOC_DAPM_ENUM("Input Source", axg_toddr_sel_enum);
147 static const struct snd_soc_dapm_widget axg_toddr_dapm_widgets[] = {
148 SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_toddr_in_mux),
149 SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0),
150 SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0),
151 SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0),
152 SND_SOC_DAPM_AIF_IN("IN 3", NULL, 0, SND_SOC_NOPM, 0, 0),
153 SND_SOC_DAPM_AIF_IN("IN 4", NULL, 0, SND_SOC_NOPM, 0, 0),
154 SND_SOC_DAPM_AIF_IN("IN 6", NULL, 0, SND_SOC_NOPM, 0, 0),
157 static const struct snd_soc_dapm_route axg_toddr_dapm_routes[] = {
158 { "Capture", NULL, "SRC SEL" },
159 { "SRC SEL", "IN 0", "IN 0" },
160 { "SRC SEL", "IN 1", "IN 1" },
161 { "SRC SEL", "IN 2", "IN 2" },
162 { "SRC SEL", "IN 3", "IN 3" },
163 { "SRC SEL", "IN 4", "IN 4" },
164 { "SRC SEL", "IN 6", "IN 6" },
167 static const struct snd_soc_component_driver axg_toddr_component_drv = {
168 .dapm_widgets = axg_toddr_dapm_widgets,
169 .num_dapm_widgets = ARRAY_SIZE(axg_toddr_dapm_widgets),
170 .dapm_routes = axg_toddr_dapm_routes,
171 .num_dapm_routes = ARRAY_SIZE(axg_toddr_dapm_routes),
172 .ops = &axg_fifo_pcm_ops
175 static const struct axg_fifo_match_data axg_toddr_match_data = {
176 .component_drv = &axg_toddr_component_drv,
177 .dai_drv = &axg_toddr_dai_drv
180 static const struct of_device_id axg_toddr_of_match[] = {
182 .compatible = "amlogic,axg-toddr",
183 .data = &axg_toddr_match_data,
186 MODULE_DEVICE_TABLE(of, axg_toddr_of_match);
188 static struct platform_driver axg_toddr_pdrv = {
189 .probe = axg_fifo_probe,
192 .of_match_table = axg_toddr_of_match,
195 module_platform_driver(axg_toddr_pdrv);
197 MODULE_DESCRIPTION("Amlogic AXG capture fifo driver");
198 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
199 MODULE_LICENSE("GPL v2");