ASoC: meson: g12a-toacodec: use regmap fields to prepare SM1 support
[linux-2.6-microblaze.git] / sound / soc / meson / g12a-toacodec.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 <sound/pcm_params.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/reset.h>
13 #include <sound/soc.h>
14 #include <sound/soc-dai.h>
15
16 #include <dt-bindings/sound/meson-g12a-toacodec.h>
17 #include "axg-tdm.h"
18 #include "meson-codec-glue.h"
19
20 #define G12A_TOACODEC_DRV_NAME "g12a-toacodec"
21
22 #define TOACODEC_CTRL0                  0x0
23 #define  CTRL0_ENABLE_SHIFT             31
24 #define  CTRL0_DAT_SEL_MSB              15
25 #define  CTRL0_DAT_SEL_LSB              14
26 #define  CTRL0_LANE_SEL                 12
27 #define  CTRL0_LRCLK_SEL_MSB            9
28 #define  CTRL0_LRCLK_SEL_LSB            8
29 #define  CTRL0_BLK_CAP_INV              BIT(7)
30 #define  CTRL0_BCLK_O_INV               BIT(6)
31 #define  CTRL0_BCLK_SEL_MSB             5
32 #define  CTRL0_BCLK_SEL_LSB             4
33 #define  CTRL0_MCLK_SEL                 GENMASK(2, 0)
34
35 #define TOACODEC_OUT_CHMAX              2
36
37 struct g12a_toacodec {
38         struct regmap_field *field_dat_sel;
39         struct regmap_field *field_lrclk_sel;
40         struct regmap_field *field_bclk_sel;
41 };
42
43 struct g12a_toacodec_match_data {
44         struct reg_field field_dat_sel;
45         struct reg_field field_lrclk_sel;
46         struct reg_field field_bclk_sel;
47 };
48
49 static const char * const g12a_toacodec_mux_texts[] = {
50         "I2S A", "I2S B", "I2S C",
51 };
52
53 static int g12a_toacodec_mux_put_enum(struct snd_kcontrol *kcontrol,
54                                       struct snd_ctl_elem_value *ucontrol)
55 {
56         struct snd_soc_component *component =
57                 snd_soc_dapm_kcontrol_component(kcontrol);
58         struct g12a_toacodec *priv = snd_soc_component_get_drvdata(component);
59         struct snd_soc_dapm_context *dapm =
60                 snd_soc_dapm_kcontrol_dapm(kcontrol);
61         struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
62         unsigned int mux, reg;
63
64         mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
65         regmap_field_read(priv->field_dat_sel, &reg);
66
67         if (mux == reg)
68                 return 0;
69
70         /* Force disconnect of the mux while updating */
71         snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
72
73         regmap_field_write(priv->field_dat_sel, mux);
74         regmap_field_write(priv->field_lrclk_sel, mux);
75         regmap_field_write(priv->field_bclk_sel, mux);
76
77         /*
78          * FIXME:
79          * On this soc, the glue gets the MCLK directly from the clock
80          * controller instead of going the through the TDM interface.
81          *
82          * Here we assume interface A uses clock A, etc ... While it is
83          * true for now, it could be different. Instead the glue should
84          * find out the clock used by the interface and select the same
85          * source. For that, we will need regmap backed clock mux which
86          * is a work in progress
87          */
88         snd_soc_component_update_bits(component, e->reg,
89                                       CTRL0_MCLK_SEL,
90                                       FIELD_PREP(CTRL0_MCLK_SEL, mux));
91
92         snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
93
94         return 0;
95 }
96
97 static SOC_ENUM_SINGLE_DECL(g12a_toacodec_mux_enum, TOACODEC_CTRL0,
98                             CTRL0_DAT_SEL_LSB,
99                             g12a_toacodec_mux_texts);
100
101 static const struct snd_kcontrol_new g12a_toacodec_mux =
102         SOC_DAPM_ENUM_EXT("Source", g12a_toacodec_mux_enum,
103                           snd_soc_dapm_get_enum_double,
104                           g12a_toacodec_mux_put_enum);
105
106 static const struct snd_kcontrol_new g12a_toacodec_out_enable =
107         SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOACODEC_CTRL0,
108                                     CTRL0_ENABLE_SHIFT, 1, 0);
109
110 static const struct snd_soc_dapm_widget g12a_toacodec_widgets[] = {
111         SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
112                          &g12a_toacodec_mux),
113         SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
114                             &g12a_toacodec_out_enable),
115 };
116
117 static int g12a_toacodec_input_hw_params(struct snd_pcm_substream *substream,
118                                          struct snd_pcm_hw_params *params,
119                                          struct snd_soc_dai *dai)
120 {
121         struct meson_codec_glue_input *data;
122         int ret;
123
124         ret = meson_codec_glue_input_hw_params(substream, params, dai);
125         if (ret)
126                 return ret;
127
128         /* The glue will provide 1 lane out of the 4 to the output */
129         data = meson_codec_glue_input_get_data(dai);
130         data->params.channels_min = min_t(unsigned int, TOACODEC_OUT_CHMAX,
131                                         data->params.channels_min);
132         data->params.channels_max = min_t(unsigned int, TOACODEC_OUT_CHMAX,
133                                         data->params.channels_max);
134
135         return 0;
136 }
137
138 static const struct snd_soc_dai_ops g12a_toacodec_input_ops = {
139         .hw_params      = g12a_toacodec_input_hw_params,
140         .set_fmt        = meson_codec_glue_input_set_fmt,
141 };
142
143 static const struct snd_soc_dai_ops g12a_toacodec_output_ops = {
144         .startup        = meson_codec_glue_output_startup,
145 };
146
147 #define TOACODEC_STREAM(xname, xsuffix, xchmax)                 \
148 {                                                               \
149         .stream_name    = xname " " xsuffix,                    \
150         .channels_min   = 1,                                    \
151         .channels_max   = (xchmax),                             \
152         .rate_min       = 5512,                                 \
153         .rate_max       = 192000,                               \
154         .formats        = AXG_TDM_FORMATS,                      \
155 }
156
157 #define TOACODEC_INPUT(xname, xid) {                                    \
158         .name = xname,                                                  \
159         .id = (xid),                                                    \
160         .playback = TOACODEC_STREAM(xname, "Playback", 8),              \
161         .ops = &g12a_toacodec_input_ops,                                \
162         .probe = meson_codec_glue_input_dai_probe,                      \
163         .remove = meson_codec_glue_input_dai_remove,                    \
164 }
165
166 #define TOACODEC_OUTPUT(xname, xid) {                                   \
167         .name = xname,                                                  \
168         .id = (xid),                                                    \
169         .capture = TOACODEC_STREAM(xname, "Capture", TOACODEC_OUT_CHMAX), \
170         .ops = &g12a_toacodec_output_ops,                               \
171 }
172
173 static struct snd_soc_dai_driver g12a_toacodec_dai_drv[] = {
174         TOACODEC_INPUT("IN A", TOACODEC_IN_A),
175         TOACODEC_INPUT("IN B", TOACODEC_IN_B),
176         TOACODEC_INPUT("IN C", TOACODEC_IN_C),
177         TOACODEC_OUTPUT("OUT", TOACODEC_OUT),
178 };
179
180 static int g12a_toacodec_component_probe(struct snd_soc_component *c)
181 {
182         /* Initialize the static clock parameters */
183         return snd_soc_component_write(c, TOACODEC_CTRL0,
184                                        CTRL0_BLK_CAP_INV);
185 }
186
187 static const struct snd_soc_dapm_route g12a_toacodec_routes[] = {
188         { "SRC", "I2S A", "IN A Playback" },
189         { "SRC", "I2S B", "IN B Playback" },
190         { "SRC", "I2S C", "IN C Playback" },
191         { "OUT EN", "Switch", "SRC" },
192         { "OUT Capture", NULL, "OUT EN" },
193 };
194
195 static const struct snd_kcontrol_new g12a_toacodec_controls[] = {
196         SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL, 3, 0),
197 };
198
199 static const struct snd_soc_component_driver g12a_toacodec_component_drv = {
200         .probe                  = g12a_toacodec_component_probe,
201         .controls               = g12a_toacodec_controls,
202         .num_controls           = ARRAY_SIZE(g12a_toacodec_controls),
203         .dapm_widgets           = g12a_toacodec_widgets,
204         .num_dapm_widgets       = ARRAY_SIZE(g12a_toacodec_widgets),
205         .dapm_routes            = g12a_toacodec_routes,
206         .num_dapm_routes        = ARRAY_SIZE(g12a_toacodec_routes),
207         .endianness             = 1,
208         .non_legacy_dai_naming  = 1,
209 };
210
211 static const struct regmap_config g12a_toacodec_regmap_cfg = {
212         .reg_bits       = 32,
213         .val_bits       = 32,
214         .reg_stride     = 4,
215 };
216
217 static const struct g12a_toacodec_match_data g12a_toacodec_match_data = {
218         .field_dat_sel  = REG_FIELD(TOACODEC_CTRL0, 14, 15),
219         .field_lrclk_sel = REG_FIELD(TOACODEC_CTRL0, 8, 9),
220         .field_bclk_sel = REG_FIELD(TOACODEC_CTRL0, 4, 5),
221 };
222
223 static const struct of_device_id g12a_toacodec_of_match[] = {
224         {
225                 .compatible = "amlogic,g12a-toacodec",
226                 .data = &g12a_toacodec_match_data,
227         },
228         {}
229 };
230 MODULE_DEVICE_TABLE(of, g12a_toacodec_of_match);
231
232 static int g12a_toacodec_probe(struct platform_device *pdev)
233 {
234         const struct g12a_toacodec_match_data *data;
235         struct device *dev = &pdev->dev;
236         struct g12a_toacodec *priv;
237         void __iomem *regs;
238         struct regmap *map;
239         int ret;
240
241         data = device_get_match_data(dev);
242         if (!data) {
243                 dev_err(dev, "failed to match device\n");
244                 return -ENODEV;
245         }
246
247         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
248         if (!priv)
249                 return -ENOMEM;
250
251         platform_set_drvdata(pdev, priv);
252
253         ret = device_reset(dev);
254         if (ret)
255                 return ret;
256
257         regs = devm_platform_ioremap_resource(pdev, 0);
258         if (IS_ERR(regs))
259                 return PTR_ERR(regs);
260
261         map = devm_regmap_init_mmio(dev, regs, &g12a_toacodec_regmap_cfg);
262         if (IS_ERR(map)) {
263                 dev_err(dev, "failed to init regmap: %ld\n",
264                         PTR_ERR(map));
265                 return PTR_ERR(map);
266         }
267
268         priv->field_dat_sel = devm_regmap_field_alloc(dev, map, data->field_dat_sel);
269         if (IS_ERR(priv->field_dat_sel))
270                 return PTR_ERR(priv->field_dat_sel);
271
272         priv->field_lrclk_sel = devm_regmap_field_alloc(dev, map, data->field_lrclk_sel);
273         if (IS_ERR(priv->field_lrclk_sel))
274                 return PTR_ERR(priv->field_lrclk_sel);
275
276         priv->field_bclk_sel = devm_regmap_field_alloc(dev, map, data->field_bclk_sel);
277         if (IS_ERR(priv->field_bclk_sel))
278                 return PTR_ERR(priv->field_bclk_sel);
279
280         return devm_snd_soc_register_component(dev,
281                         &g12a_toacodec_component_drv, g12a_toacodec_dai_drv,
282                         ARRAY_SIZE(g12a_toacodec_dai_drv));
283 }
284
285 static struct platform_driver g12a_toacodec_pdrv = {
286         .driver = {
287                 .name = G12A_TOACODEC_DRV_NAME,
288                 .of_match_table = g12a_toacodec_of_match,
289         },
290         .probe = g12a_toacodec_probe,
291 };
292 module_platform_driver(g12a_toacodec_pdrv);
293
294 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
295 MODULE_DESCRIPTION("Amlogic G12a To Internal DAC Codec Driver");
296 MODULE_LICENSE("GPL v2");