1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Andrea Venturi
4 * Andrea Venturi <be17068@iperbole.bo.it>
6 * Copyright (C) 2016 Maxime Ripard
7 * Maxime Ripard <maxime.ripard@free-electrons.com>
10 #include <linux/clk.h>
11 #include <linux/dmaengine.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/regmap.h>
17 #include <linux/reset.h>
19 #include <sound/dmaengine_pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <sound/soc-dai.h>
24 #define SUN4I_I2S_CTRL_REG 0x00
25 #define SUN4I_I2S_CTRL_SDO_EN_MASK GENMASK(11, 8)
26 #define SUN4I_I2S_CTRL_SDO_EN(sdo) BIT(8 + (sdo))
27 #define SUN4I_I2S_CTRL_MODE_MASK BIT(5)
28 #define SUN4I_I2S_CTRL_MODE_SLAVE (1 << 5)
29 #define SUN4I_I2S_CTRL_MODE_MASTER (0 << 5)
30 #define SUN4I_I2S_CTRL_TX_EN BIT(2)
31 #define SUN4I_I2S_CTRL_RX_EN BIT(1)
32 #define SUN4I_I2S_CTRL_GL_EN BIT(0)
34 #define SUN4I_I2S_FMT0_REG 0x04
35 #define SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(7)
36 #define SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 7)
37 #define SUN4I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 7)
38 #define SUN4I_I2S_FMT0_BCLK_POLARITY_MASK BIT(6)
39 #define SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 6)
40 #define SUN4I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 6)
41 #define SUN4I_I2S_FMT0_SR_MASK GENMASK(5, 4)
42 #define SUN4I_I2S_FMT0_SR(sr) ((sr) << 4)
43 #define SUN4I_I2S_FMT0_WSS_MASK GENMASK(3, 2)
44 #define SUN4I_I2S_FMT0_WSS(wss) ((wss) << 2)
45 #define SUN4I_I2S_FMT0_FMT_MASK GENMASK(1, 0)
46 #define SUN4I_I2S_FMT0_FMT_RIGHT_J (2 << 0)
47 #define SUN4I_I2S_FMT0_FMT_LEFT_J (1 << 0)
48 #define SUN4I_I2S_FMT0_FMT_I2S (0 << 0)
50 #define SUN4I_I2S_FMT1_REG 0x08
51 #define SUN4I_I2S_FMT1_REG_SEXT_MASK BIT(8)
52 #define SUN4I_I2S_FMT1_REG_SEXT(sext) ((sext) << 8)
54 #define SUN4I_I2S_FIFO_TX_REG 0x0c
55 #define SUN4I_I2S_FIFO_RX_REG 0x10
57 #define SUN4I_I2S_FIFO_CTRL_REG 0x14
58 #define SUN4I_I2S_FIFO_CTRL_FLUSH_TX BIT(25)
59 #define SUN4I_I2S_FIFO_CTRL_FLUSH_RX BIT(24)
60 #define SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK BIT(2)
61 #define SUN4I_I2S_FIFO_CTRL_TX_MODE(mode) ((mode) << 2)
62 #define SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK GENMASK(1, 0)
63 #define SUN4I_I2S_FIFO_CTRL_RX_MODE(mode) (mode)
65 #define SUN4I_I2S_FIFO_STA_REG 0x18
67 #define SUN4I_I2S_DMA_INT_CTRL_REG 0x1c
68 #define SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN BIT(7)
69 #define SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN BIT(3)
71 #define SUN4I_I2S_INT_STA_REG 0x20
73 #define SUN4I_I2S_CLK_DIV_REG 0x24
74 #define SUN4I_I2S_CLK_DIV_MCLK_EN BIT(7)
75 #define SUN4I_I2S_CLK_DIV_BCLK_MASK GENMASK(6, 4)
76 #define SUN4I_I2S_CLK_DIV_BCLK(bclk) ((bclk) << 4)
77 #define SUN4I_I2S_CLK_DIV_MCLK_MASK GENMASK(3, 0)
78 #define SUN4I_I2S_CLK_DIV_MCLK(mclk) ((mclk) << 0)
80 #define SUN4I_I2S_TX_CNT_REG 0x28
81 #define SUN4I_I2S_RX_CNT_REG 0x2c
83 #define SUN4I_I2S_TX_CHAN_SEL_REG 0x30
84 #define SUN4I_I2S_CHAN_SEL_MASK GENMASK(2, 0)
85 #define SUN4I_I2S_CHAN_SEL(num_chan) (((num_chan) - 1) << 0)
87 #define SUN4I_I2S_TX_CHAN_MAP_REG 0x34
88 #define SUN4I_I2S_TX_CHAN_MAP(chan, sample) ((sample) << (chan << 2))
90 #define SUN4I_I2S_RX_CHAN_SEL_REG 0x38
91 #define SUN4I_I2S_RX_CHAN_MAP_REG 0x3c
93 /* Defines required for sun8i-h3 support */
94 #define SUN8I_I2S_CTRL_BCLK_OUT BIT(18)
95 #define SUN8I_I2S_CTRL_LRCK_OUT BIT(17)
97 #define SUN8I_I2S_CTRL_MODE_MASK GENMASK(5, 4)
98 #define SUN8I_I2S_CTRL_MODE_RIGHT (2 << 4)
99 #define SUN8I_I2S_CTRL_MODE_LEFT (1 << 4)
100 #define SUN8I_I2S_CTRL_MODE_PCM (0 << 4)
102 #define SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK BIT(19)
103 #define SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED (1 << 19)
104 #define SUN8I_I2S_FMT0_LRCLK_POLARITY_NORMAL (0 << 19)
105 #define SUN8I_I2S_FMT0_LRCK_PERIOD_MASK GENMASK(17, 8)
106 #define SUN8I_I2S_FMT0_LRCK_PERIOD(period) ((period - 1) << 8)
107 #define SUN8I_I2S_FMT0_BCLK_POLARITY_MASK BIT(7)
108 #define SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED (1 << 7)
109 #define SUN8I_I2S_FMT0_BCLK_POLARITY_NORMAL (0 << 7)
111 #define SUN8I_I2S_FMT1_REG_SEXT_MASK GENMASK(5, 4)
112 #define SUN8I_I2S_FMT1_REG_SEXT(sext) ((sext) << 4)
114 #define SUN8I_I2S_INT_STA_REG 0x0c
115 #define SUN8I_I2S_FIFO_TX_REG 0x20
117 #define SUN8I_I2S_CHAN_CFG_REG 0x30
118 #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK GENMASK(7, 4)
119 #define SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(chan) ((chan - 1) << 4)
120 #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK GENMASK(3, 0)
121 #define SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(chan) (chan - 1)
123 #define SUN8I_I2S_TX_CHAN_MAP_REG 0x44
124 #define SUN8I_I2S_TX_CHAN_SEL_REG 0x34
125 #define SUN8I_I2S_TX_CHAN_OFFSET_MASK GENMASK(13, 12)
126 #define SUN8I_I2S_TX_CHAN_OFFSET(offset) (offset << 12)
127 #define SUN8I_I2S_TX_CHAN_EN_MASK GENMASK(11, 4)
128 #define SUN8I_I2S_TX_CHAN_EN(num_chan) (((1 << num_chan) - 1) << 4)
130 #define SUN8I_I2S_RX_CHAN_SEL_REG 0x54
131 #define SUN8I_I2S_RX_CHAN_MAP_REG 0x58
133 /* Defines required for sun50i-h6 support */
134 #define SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK GENMASK(21, 20)
135 #define SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset) ((offset) << 20)
136 #define SUN50I_H6_I2S_TX_CHAN_SEL_MASK GENMASK(19, 16)
137 #define SUN50I_H6_I2S_TX_CHAN_SEL(chan) ((chan - 1) << 16)
138 #define SUN50I_H6_I2S_TX_CHAN_EN_MASK GENMASK(15, 0)
139 #define SUN50I_H6_I2S_TX_CHAN_EN(num_chan) (((1 << num_chan) - 1))
141 #define SUN50I_H6_I2S_TX_CHAN_SEL_REG(pin) (0x34 + 4 * (pin))
142 #define SUN50I_H6_I2S_TX_CHAN_MAP0_REG(pin) (0x44 + 8 * (pin))
143 #define SUN50I_H6_I2S_TX_CHAN_MAP1_REG(pin) (0x48 + 8 * (pin))
145 #define SUN50I_H6_I2S_RX_CHAN_SEL_REG 0x64
146 #define SUN50I_H6_I2S_RX_CHAN_MAP0_REG 0x68
147 #define SUN50I_H6_I2S_RX_CHAN_MAP1_REG 0x6C
149 #define SUN50I_R329_I2S_RX_CHAN_MAP0_REG 0x68
150 #define SUN50I_R329_I2S_RX_CHAN_MAP1_REG 0x6c
151 #define SUN50I_R329_I2S_RX_CHAN_MAP2_REG 0x70
152 #define SUN50I_R329_I2S_RX_CHAN_MAP3_REG 0x74
157 * struct sun4i_i2s_quirks - Differences between SoC variants.
158 * @has_reset: SoC needs reset deasserted.
159 * @reg_offset_txdata: offset of the tx fifo.
160 * @sun4i_i2s_regmap: regmap config to use.
161 * @field_clkdiv_mclk_en: regmap field to enable mclk output.
162 * @field_fmt_wss: regmap field to set word select size.
163 * @field_fmt_sr: regmap field to set sample resolution.
164 * @bclk_dividers: bit clock dividers array
165 * @num_bclk_dividers: number of bit clock dividers
166 * @mclk_dividers: mclk dividers array
167 * @num_mclk_dividers: number of mclk dividers
168 * @get_bclk_parent_rate: callback to get bclk parent rate
169 * @get_sr: callback to get sample resolution
170 * @get_wss: callback to get word select size
171 * @set_chan_cfg: callback to set channel configuration
172 * @set_fmt: callback to set format
174 struct sun4i_i2s_quirks {
176 unsigned int reg_offset_txdata; /* TX FIFO */
177 const struct regmap_config *sun4i_i2s_regmap;
179 /* Register fields for i2s */
180 struct reg_field field_clkdiv_mclk_en;
181 struct reg_field field_fmt_wss;
182 struct reg_field field_fmt_sr;
184 unsigned int num_din_pins;
185 unsigned int num_dout_pins;
187 const struct sun4i_i2s_clk_div *bclk_dividers;
188 unsigned int num_bclk_dividers;
189 const struct sun4i_i2s_clk_div *mclk_dividers;
190 unsigned int num_mclk_dividers;
192 unsigned long (*get_bclk_parent_rate)(const struct sun4i_i2s *i2s);
193 int (*get_sr)(unsigned int width);
194 int (*get_wss)(unsigned int width);
197 * In the set_chan_cfg() function pointer:
198 * @slots: channels per frame + padding slots, regardless of format
199 * @slot_width: bits per sample + padding bits, regardless of format
201 int (*set_chan_cfg)(const struct sun4i_i2s *i2s,
202 unsigned int channels, unsigned int slots,
203 unsigned int slot_width);
204 int (*set_fmt)(const struct sun4i_i2s *i2s, unsigned int fmt);
210 struct regmap *regmap;
211 struct reset_control *rst;
214 unsigned int mclk_freq;
216 unsigned int slot_width;
218 struct snd_dmaengine_dai_dma_data capture_dma_data;
219 struct snd_dmaengine_dai_dma_data playback_dma_data;
221 /* Register fields for i2s */
222 struct regmap_field *field_clkdiv_mclk_en;
223 struct regmap_field *field_fmt_wss;
224 struct regmap_field *field_fmt_sr;
226 const struct sun4i_i2s_quirks *variant;
229 struct sun4i_i2s_clk_div {
234 static const struct sun4i_i2s_clk_div sun4i_i2s_bclk_div[] = {
235 { .div = 2, .val = 0 },
236 { .div = 4, .val = 1 },
237 { .div = 6, .val = 2 },
238 { .div = 8, .val = 3 },
239 { .div = 12, .val = 4 },
240 { .div = 16, .val = 5 },
241 /* TODO - extend divide ratio supported by newer SoCs */
244 static const struct sun4i_i2s_clk_div sun4i_i2s_mclk_div[] = {
245 { .div = 1, .val = 0 },
246 { .div = 2, .val = 1 },
247 { .div = 4, .val = 2 },
248 { .div = 6, .val = 3 },
249 { .div = 8, .val = 4 },
250 { .div = 12, .val = 5 },
251 { .div = 16, .val = 6 },
252 { .div = 24, .val = 7 },
253 /* TODO - extend divide ratio supported by newer SoCs */
256 static const struct sun4i_i2s_clk_div sun8i_i2s_clk_div[] = {
257 { .div = 1, .val = 1 },
258 { .div = 2, .val = 2 },
259 { .div = 4, .val = 3 },
260 { .div = 6, .val = 4 },
261 { .div = 8, .val = 5 },
262 { .div = 12, .val = 6 },
263 { .div = 16, .val = 7 },
264 { .div = 24, .val = 8 },
265 { .div = 32, .val = 9 },
266 { .div = 48, .val = 10 },
267 { .div = 64, .val = 11 },
268 { .div = 96, .val = 12 },
269 { .div = 128, .val = 13 },
270 { .div = 176, .val = 14 },
271 { .div = 192, .val = 15 },
274 static unsigned long sun4i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
276 return i2s->mclk_freq;
279 static unsigned long sun8i_i2s_get_bclk_parent_rate(const struct sun4i_i2s *i2s)
281 return clk_get_rate(i2s->mod_clk);
284 static int sun4i_i2s_get_bclk_div(struct sun4i_i2s *i2s,
285 unsigned long parent_rate,
286 unsigned int sampling_rate,
287 unsigned int channels,
288 unsigned int word_size)
290 const struct sun4i_i2s_clk_div *dividers = i2s->variant->bclk_dividers;
291 int div = parent_rate / sampling_rate / word_size / channels;
294 for (i = 0; i < i2s->variant->num_bclk_dividers; i++) {
295 const struct sun4i_i2s_clk_div *bdiv = ÷rs[i];
297 if (bdiv->div == div)
304 static int sun4i_i2s_get_mclk_div(struct sun4i_i2s *i2s,
305 unsigned long parent_rate,
306 unsigned long mclk_rate)
308 const struct sun4i_i2s_clk_div *dividers = i2s->variant->mclk_dividers;
309 int div = parent_rate / mclk_rate;
312 for (i = 0; i < i2s->variant->num_mclk_dividers; i++) {
313 const struct sun4i_i2s_clk_div *mdiv = ÷rs[i];
315 if (mdiv->div == div)
322 static int sun4i_i2s_oversample_rates[] = { 128, 192, 256, 384, 512, 768 };
323 static bool sun4i_i2s_oversample_is_valid(unsigned int oversample)
327 for (i = 0; i < ARRAY_SIZE(sun4i_i2s_oversample_rates); i++)
328 if (sun4i_i2s_oversample_rates[i] == oversample)
334 static int sun4i_i2s_set_clk_rate(struct snd_soc_dai *dai,
337 unsigned int slot_width)
339 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
340 unsigned int oversample_rate, clk_rate, bclk_parent_rate;
341 int bclk_div, mclk_div;
367 dev_err(dai->dev, "Unsupported sample rate: %u\n", rate);
371 ret = clk_set_rate(i2s->mod_clk, clk_rate);
375 oversample_rate = i2s->mclk_freq / rate;
376 if (!sun4i_i2s_oversample_is_valid(oversample_rate)) {
377 dev_err(dai->dev, "Unsupported oversample rate: %d\n",
382 bclk_parent_rate = i2s->variant->get_bclk_parent_rate(i2s);
383 bclk_div = sun4i_i2s_get_bclk_div(i2s, bclk_parent_rate,
384 rate, slots, slot_width);
386 dev_err(dai->dev, "Unsupported BCLK divider: %d\n", bclk_div);
390 mclk_div = sun4i_i2s_get_mclk_div(i2s, clk_rate, i2s->mclk_freq);
392 dev_err(dai->dev, "Unsupported MCLK divider: %d\n", mclk_div);
396 regmap_write(i2s->regmap, SUN4I_I2S_CLK_DIV_REG,
397 SUN4I_I2S_CLK_DIV_BCLK(bclk_div) |
398 SUN4I_I2S_CLK_DIV_MCLK(mclk_div));
400 regmap_field_write(i2s->field_clkdiv_mclk_en, 1);
405 static int sun4i_i2s_get_sr(unsigned int width)
419 static int sun4i_i2s_get_wss(unsigned int width)
435 static int sun8i_i2s_get_sr_wss(unsigned int width)
457 static int sun4i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
458 unsigned int channels, unsigned int slots,
459 unsigned int slot_width)
461 /* Map the channels for playback and capture */
462 regmap_write(i2s->regmap, SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210);
463 regmap_write(i2s->regmap, SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210);
465 /* Configure the channels */
466 regmap_update_bits(i2s->regmap, SUN4I_I2S_TX_CHAN_SEL_REG,
467 SUN4I_I2S_CHAN_SEL_MASK,
468 SUN4I_I2S_CHAN_SEL(channels));
469 regmap_update_bits(i2s->regmap, SUN4I_I2S_RX_CHAN_SEL_REG,
470 SUN4I_I2S_CHAN_SEL_MASK,
471 SUN4I_I2S_CHAN_SEL(channels));
476 static int sun8i_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
477 unsigned int channels, unsigned int slots,
478 unsigned int slot_width)
480 unsigned int lrck_period;
482 /* Map the channels for playback and capture */
483 regmap_write(i2s->regmap, SUN8I_I2S_TX_CHAN_MAP_REG, 0x76543210);
484 regmap_write(i2s->regmap, SUN8I_I2S_RX_CHAN_MAP_REG, 0x76543210);
486 /* Configure the channels */
487 regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
488 SUN4I_I2S_CHAN_SEL_MASK,
489 SUN4I_I2S_CHAN_SEL(channels));
490 regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
491 SUN4I_I2S_CHAN_SEL_MASK,
492 SUN4I_I2S_CHAN_SEL(channels));
494 regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
495 SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
496 SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
497 regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
498 SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
499 SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
501 switch (i2s->format & SND_SOC_DAIFMT_FORMAT_MASK) {
502 case SND_SOC_DAIFMT_DSP_A:
503 case SND_SOC_DAIFMT_DSP_B:
504 lrck_period = slot_width * slots;
507 case SND_SOC_DAIFMT_LEFT_J:
508 case SND_SOC_DAIFMT_RIGHT_J:
509 case SND_SOC_DAIFMT_I2S:
510 lrck_period = slot_width;
517 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
518 SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
519 SUN8I_I2S_FMT0_LRCK_PERIOD(lrck_period));
521 regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
522 SUN8I_I2S_TX_CHAN_EN_MASK,
523 SUN8I_I2S_TX_CHAN_EN(channels));
528 static int sun50i_h6_i2s_set_chan_cfg(const struct sun4i_i2s *i2s,
529 unsigned int channels, unsigned int slots,
530 unsigned int slot_width)
532 unsigned int lrck_period;
534 /* Map the channels for playback and capture */
535 regmap_write(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_MAP0_REG(0), 0xFEDCBA98);
536 regmap_write(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_MAP1_REG(0), 0x76543210);
537 if (i2s->variant->num_din_pins > 1) {
538 regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP0_REG, 0x0F0E0D0C);
539 regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP1_REG, 0x0B0A0908);
540 regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP2_REG, 0x07060504);
541 regmap_write(i2s->regmap, SUN50I_R329_I2S_RX_CHAN_MAP3_REG, 0x03020100);
543 regmap_write(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_MAP0_REG, 0xFEDCBA98);
544 regmap_write(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_MAP1_REG, 0x76543210);
547 /* Configure the channels */
548 regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_SEL_REG(0),
549 SUN50I_H6_I2S_TX_CHAN_SEL_MASK,
550 SUN50I_H6_I2S_TX_CHAN_SEL(channels));
551 regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_SEL_REG,
552 SUN50I_H6_I2S_TX_CHAN_SEL_MASK,
553 SUN50I_H6_I2S_TX_CHAN_SEL(channels));
555 regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
556 SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM_MASK,
557 SUN8I_I2S_CHAN_CFG_TX_SLOT_NUM(channels));
558 regmap_update_bits(i2s->regmap, SUN8I_I2S_CHAN_CFG_REG,
559 SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM_MASK,
560 SUN8I_I2S_CHAN_CFG_RX_SLOT_NUM(channels));
562 switch (i2s->format & SND_SOC_DAIFMT_FORMAT_MASK) {
563 case SND_SOC_DAIFMT_DSP_A:
564 case SND_SOC_DAIFMT_DSP_B:
565 lrck_period = slot_width * slots;
568 case SND_SOC_DAIFMT_LEFT_J:
569 case SND_SOC_DAIFMT_RIGHT_J:
570 case SND_SOC_DAIFMT_I2S:
571 lrck_period = slot_width;
578 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
579 SUN8I_I2S_FMT0_LRCK_PERIOD_MASK,
580 SUN8I_I2S_FMT0_LRCK_PERIOD(lrck_period));
582 regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_TX_CHAN_SEL_REG(0),
583 SUN50I_H6_I2S_TX_CHAN_EN_MASK,
584 SUN50I_H6_I2S_TX_CHAN_EN(channels));
589 static int sun4i_i2s_hw_params(struct snd_pcm_substream *substream,
590 struct snd_pcm_hw_params *params,
591 struct snd_soc_dai *dai)
593 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
594 unsigned int word_size = params_width(params);
595 unsigned int slot_width = params_physical_width(params);
596 unsigned int channels = params_channels(params);
598 unsigned int slots = channels;
607 slot_width = i2s->slot_width;
609 ret = i2s->variant->set_chan_cfg(i2s, channels, slots, slot_width);
611 dev_err(dai->dev, "Invalid channel configuration\n");
615 /* Set significant bits in our FIFOs */
616 regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
617 SUN4I_I2S_FIFO_CTRL_TX_MODE_MASK |
618 SUN4I_I2S_FIFO_CTRL_RX_MODE_MASK,
619 SUN4I_I2S_FIFO_CTRL_TX_MODE(1) |
620 SUN4I_I2S_FIFO_CTRL_RX_MODE(1));
622 switch (params_physical_width(params)) {
624 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
627 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
630 dev_err(dai->dev, "Unsupported physical sample width: %d\n",
631 params_physical_width(params));
634 i2s->playback_dma_data.addr_width = width;
636 sr = i2s->variant->get_sr(word_size);
640 wss = i2s->variant->get_wss(slot_width);
644 regmap_field_write(i2s->field_fmt_wss, wss);
645 regmap_field_write(i2s->field_fmt_sr, sr);
647 return sun4i_i2s_set_clk_rate(dai, params_rate(params),
651 static int sun4i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
656 /* DAI clock polarity */
657 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
658 case SND_SOC_DAIFMT_IB_IF:
659 /* Invert both clocks */
660 val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED |
661 SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
663 case SND_SOC_DAIFMT_IB_NF:
664 /* Invert bit clock */
665 val = SUN4I_I2S_FMT0_BCLK_POLARITY_INVERTED;
667 case SND_SOC_DAIFMT_NB_IF:
668 /* Invert frame clock */
669 val = SUN4I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
671 case SND_SOC_DAIFMT_NB_NF:
678 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
679 SUN4I_I2S_FMT0_LRCLK_POLARITY_MASK |
680 SUN4I_I2S_FMT0_BCLK_POLARITY_MASK,
684 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
685 case SND_SOC_DAIFMT_I2S:
686 val = SUN4I_I2S_FMT0_FMT_I2S;
689 case SND_SOC_DAIFMT_LEFT_J:
690 val = SUN4I_I2S_FMT0_FMT_LEFT_J;
693 case SND_SOC_DAIFMT_RIGHT_J:
694 val = SUN4I_I2S_FMT0_FMT_RIGHT_J;
701 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
702 SUN4I_I2S_FMT0_FMT_MASK, val);
704 /* DAI clock master masks */
705 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
706 case SND_SOC_DAIFMT_CBS_CFS:
707 /* BCLK and LRCLK master */
708 val = SUN4I_I2S_CTRL_MODE_MASTER;
711 case SND_SOC_DAIFMT_CBM_CFM:
712 /* BCLK and LRCLK slave */
713 val = SUN4I_I2S_CTRL_MODE_SLAVE;
719 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
720 SUN4I_I2S_CTRL_MODE_MASK, val);
725 static int sun8i_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
734 * The setup for LRCK contradicts the datasheet, but under a
735 * scope it's clear that the LRCK polarity is reversed
736 * compared to the expected polarity on the bus.
738 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
739 case SND_SOC_DAIFMT_IB_IF:
740 /* Invert both clocks */
741 val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED;
743 case SND_SOC_DAIFMT_IB_NF:
744 /* Invert bit clock */
745 val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED |
746 SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
748 case SND_SOC_DAIFMT_NB_IF:
749 /* Invert frame clock */
752 case SND_SOC_DAIFMT_NB_NF:
753 val = SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
759 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
760 SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK |
761 SUN8I_I2S_FMT0_BCLK_POLARITY_MASK,
765 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
766 case SND_SOC_DAIFMT_DSP_A:
767 mode = SUN8I_I2S_CTRL_MODE_PCM;
771 case SND_SOC_DAIFMT_DSP_B:
772 mode = SUN8I_I2S_CTRL_MODE_PCM;
776 case SND_SOC_DAIFMT_I2S:
777 mode = SUN8I_I2S_CTRL_MODE_LEFT;
781 case SND_SOC_DAIFMT_LEFT_J:
782 mode = SUN8I_I2S_CTRL_MODE_LEFT;
786 case SND_SOC_DAIFMT_RIGHT_J:
787 mode = SUN8I_I2S_CTRL_MODE_RIGHT;
795 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
796 SUN8I_I2S_CTRL_MODE_MASK, mode);
797 regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
798 SUN8I_I2S_TX_CHAN_OFFSET_MASK,
799 SUN8I_I2S_TX_CHAN_OFFSET(offset));
800 regmap_update_bits(i2s->regmap, SUN8I_I2S_RX_CHAN_SEL_REG,
801 SUN8I_I2S_TX_CHAN_OFFSET_MASK,
802 SUN8I_I2S_TX_CHAN_OFFSET(offset));
804 /* DAI clock master masks */
805 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
806 case SND_SOC_DAIFMT_CBS_CFS:
807 /* BCLK and LRCLK master */
808 val = SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT;
811 case SND_SOC_DAIFMT_CBM_CFM:
812 /* BCLK and LRCLK slave */
820 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
821 SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT,
824 /* Set sign extension to pad out LSB with 0 */
825 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT1_REG,
826 SUN8I_I2S_FMT1_REG_SEXT_MASK,
827 SUN8I_I2S_FMT1_REG_SEXT(0));
832 static int sun50i_h6_i2s_set_soc_fmt(const struct sun4i_i2s *i2s,
841 * The setup for LRCK contradicts the datasheet, but under a
842 * scope it's clear that the LRCK polarity is reversed
843 * compared to the expected polarity on the bus.
845 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
846 case SND_SOC_DAIFMT_IB_IF:
847 /* Invert both clocks */
848 val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED;
850 case SND_SOC_DAIFMT_IB_NF:
851 /* Invert bit clock */
852 val = SUN8I_I2S_FMT0_BCLK_POLARITY_INVERTED |
853 SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
855 case SND_SOC_DAIFMT_NB_IF:
856 /* Invert frame clock */
859 case SND_SOC_DAIFMT_NB_NF:
860 val = SUN8I_I2S_FMT0_LRCLK_POLARITY_INVERTED;
866 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT0_REG,
867 SUN8I_I2S_FMT0_LRCLK_POLARITY_MASK |
868 SUN8I_I2S_FMT0_BCLK_POLARITY_MASK,
872 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
873 case SND_SOC_DAIFMT_DSP_A:
874 mode = SUN8I_I2S_CTRL_MODE_PCM;
878 case SND_SOC_DAIFMT_DSP_B:
879 mode = SUN8I_I2S_CTRL_MODE_PCM;
883 case SND_SOC_DAIFMT_I2S:
884 mode = SUN8I_I2S_CTRL_MODE_LEFT;
888 case SND_SOC_DAIFMT_LEFT_J:
889 mode = SUN8I_I2S_CTRL_MODE_LEFT;
893 case SND_SOC_DAIFMT_RIGHT_J:
894 mode = SUN8I_I2S_CTRL_MODE_RIGHT;
902 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
903 SUN8I_I2S_CTRL_MODE_MASK, mode);
904 regmap_update_bits(i2s->regmap, SUN8I_I2S_TX_CHAN_SEL_REG,
905 SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK,
906 SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset));
907 regmap_update_bits(i2s->regmap, SUN50I_H6_I2S_RX_CHAN_SEL_REG,
908 SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET_MASK,
909 SUN50I_H6_I2S_TX_CHAN_SEL_OFFSET(offset));
911 /* DAI clock master masks */
912 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
913 case SND_SOC_DAIFMT_CBS_CFS:
914 /* BCLK and LRCLK master */
915 val = SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT;
918 case SND_SOC_DAIFMT_CBM_CFM:
919 /* BCLK and LRCLK slave */
927 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
928 SUN8I_I2S_CTRL_BCLK_OUT | SUN8I_I2S_CTRL_LRCK_OUT,
931 /* Set sign extension to pad out LSB with 0 */
932 regmap_update_bits(i2s->regmap, SUN4I_I2S_FMT1_REG,
933 SUN8I_I2S_FMT1_REG_SEXT_MASK,
934 SUN8I_I2S_FMT1_REG_SEXT(0));
939 static int sun4i_i2s_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
941 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
944 ret = i2s->variant->set_fmt(i2s, fmt);
946 dev_err(dai->dev, "Unsupported format configuration\n");
955 static void sun4i_i2s_start_capture(struct sun4i_i2s *i2s)
958 regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
959 SUN4I_I2S_FIFO_CTRL_FLUSH_RX,
960 SUN4I_I2S_FIFO_CTRL_FLUSH_RX);
962 /* Clear RX counter */
963 regmap_write(i2s->regmap, SUN4I_I2S_RX_CNT_REG, 0);
965 /* Enable RX Block */
966 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
967 SUN4I_I2S_CTRL_RX_EN,
968 SUN4I_I2S_CTRL_RX_EN);
971 regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
972 SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
973 SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN);
976 static void sun4i_i2s_start_playback(struct sun4i_i2s *i2s)
979 regmap_update_bits(i2s->regmap, SUN4I_I2S_FIFO_CTRL_REG,
980 SUN4I_I2S_FIFO_CTRL_FLUSH_TX,
981 SUN4I_I2S_FIFO_CTRL_FLUSH_TX);
983 /* Clear TX counter */
984 regmap_write(i2s->regmap, SUN4I_I2S_TX_CNT_REG, 0);
986 /* Enable TX Block */
987 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
988 SUN4I_I2S_CTRL_TX_EN,
989 SUN4I_I2S_CTRL_TX_EN);
992 regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
993 SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
994 SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN);
997 static void sun4i_i2s_stop_capture(struct sun4i_i2s *i2s)
999 /* Disable RX Block */
1000 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1001 SUN4I_I2S_CTRL_RX_EN,
1004 /* Disable RX DRQ */
1005 regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
1006 SUN4I_I2S_DMA_INT_CTRL_RX_DRQ_EN,
1010 static void sun4i_i2s_stop_playback(struct sun4i_i2s *i2s)
1012 /* Disable TX Block */
1013 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1014 SUN4I_I2S_CTRL_TX_EN,
1017 /* Disable TX DRQ */
1018 regmap_update_bits(i2s->regmap, SUN4I_I2S_DMA_INT_CTRL_REG,
1019 SUN4I_I2S_DMA_INT_CTRL_TX_DRQ_EN,
1023 static int sun4i_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
1024 struct snd_soc_dai *dai)
1026 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
1029 case SNDRV_PCM_TRIGGER_START:
1030 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1031 case SNDRV_PCM_TRIGGER_RESUME:
1032 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1033 sun4i_i2s_start_playback(i2s);
1035 sun4i_i2s_start_capture(i2s);
1038 case SNDRV_PCM_TRIGGER_STOP:
1039 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1040 case SNDRV_PCM_TRIGGER_SUSPEND:
1041 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
1042 sun4i_i2s_stop_playback(i2s);
1044 sun4i_i2s_stop_capture(i2s);
1054 static int sun4i_i2s_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1055 unsigned int freq, int dir)
1057 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
1062 i2s->mclk_freq = freq;
1067 static int sun4i_i2s_set_tdm_slot(struct snd_soc_dai *dai,
1068 unsigned int tx_mask, unsigned int rx_mask,
1069 int slots, int slot_width)
1071 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
1077 i2s->slot_width = slot_width;
1082 static const struct snd_soc_dai_ops sun4i_i2s_dai_ops = {
1083 .hw_params = sun4i_i2s_hw_params,
1084 .set_fmt = sun4i_i2s_set_fmt,
1085 .set_sysclk = sun4i_i2s_set_sysclk,
1086 .set_tdm_slot = sun4i_i2s_set_tdm_slot,
1087 .trigger = sun4i_i2s_trigger,
1090 static int sun4i_i2s_dai_probe(struct snd_soc_dai *dai)
1092 struct sun4i_i2s *i2s = snd_soc_dai_get_drvdata(dai);
1094 snd_soc_dai_init_dma_data(dai,
1095 &i2s->playback_dma_data,
1096 &i2s->capture_dma_data);
1101 #define SUN4I_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \
1102 SNDRV_PCM_FMTBIT_S20_LE | \
1103 SNDRV_PCM_FMTBIT_S24_LE)
1105 static struct snd_soc_dai_driver sun4i_i2s_dai = {
1106 .probe = sun4i_i2s_dai_probe,
1108 .stream_name = "Capture",
1111 .rates = SNDRV_PCM_RATE_8000_192000,
1112 .formats = SUN4I_FORMATS,
1115 .stream_name = "Playback",
1118 .rates = SNDRV_PCM_RATE_8000_192000,
1119 .formats = SUN4I_FORMATS,
1121 .ops = &sun4i_i2s_dai_ops,
1122 .symmetric_rate = 1,
1125 static const struct snd_soc_component_driver sun4i_i2s_component = {
1126 .name = "sun4i-dai",
1129 static bool sun4i_i2s_rd_reg(struct device *dev, unsigned int reg)
1132 case SUN4I_I2S_FIFO_TX_REG:
1140 static bool sun4i_i2s_wr_reg(struct device *dev, unsigned int reg)
1143 case SUN4I_I2S_FIFO_RX_REG:
1144 case SUN4I_I2S_FIFO_STA_REG:
1152 static bool sun4i_i2s_volatile_reg(struct device *dev, unsigned int reg)
1155 case SUN4I_I2S_FIFO_RX_REG:
1156 case SUN4I_I2S_INT_STA_REG:
1157 case SUN4I_I2S_RX_CNT_REG:
1158 case SUN4I_I2S_TX_CNT_REG:
1166 static bool sun8i_i2s_rd_reg(struct device *dev, unsigned int reg)
1169 case SUN8I_I2S_FIFO_TX_REG:
1177 static bool sun8i_i2s_volatile_reg(struct device *dev, unsigned int reg)
1180 case SUN4I_I2S_FIFO_CTRL_REG:
1181 case SUN4I_I2S_FIFO_RX_REG:
1182 case SUN4I_I2S_FIFO_STA_REG:
1183 case SUN4I_I2S_RX_CNT_REG:
1184 case SUN4I_I2S_TX_CNT_REG:
1185 case SUN8I_I2S_FIFO_TX_REG:
1186 case SUN8I_I2S_INT_STA_REG:
1194 static const struct reg_default sun4i_i2s_reg_defaults[] = {
1195 { SUN4I_I2S_CTRL_REG, 0x00000000 },
1196 { SUN4I_I2S_FMT0_REG, 0x0000000c },
1197 { SUN4I_I2S_FMT1_REG, 0x00004020 },
1198 { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
1199 { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
1200 { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
1201 { SUN4I_I2S_TX_CHAN_SEL_REG, 0x00000001 },
1202 { SUN4I_I2S_TX_CHAN_MAP_REG, 0x76543210 },
1203 { SUN4I_I2S_RX_CHAN_SEL_REG, 0x00000001 },
1204 { SUN4I_I2S_RX_CHAN_MAP_REG, 0x00003210 },
1207 static const struct reg_default sun8i_i2s_reg_defaults[] = {
1208 { SUN4I_I2S_CTRL_REG, 0x00060000 },
1209 { SUN4I_I2S_FMT0_REG, 0x00000033 },
1210 { SUN4I_I2S_FMT1_REG, 0x00000030 },
1211 { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
1212 { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
1213 { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
1214 { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
1215 { SUN8I_I2S_TX_CHAN_SEL_REG, 0x00000000 },
1216 { SUN8I_I2S_TX_CHAN_MAP_REG, 0x00000000 },
1217 { SUN8I_I2S_RX_CHAN_SEL_REG, 0x00000000 },
1218 { SUN8I_I2S_RX_CHAN_MAP_REG, 0x00000000 },
1221 static const struct reg_default sun50i_h6_i2s_reg_defaults[] = {
1222 { SUN4I_I2S_CTRL_REG, 0x00060000 },
1223 { SUN4I_I2S_FMT0_REG, 0x00000033 },
1224 { SUN4I_I2S_FMT1_REG, 0x00000030 },
1225 { SUN4I_I2S_FIFO_CTRL_REG, 0x000400f0 },
1226 { SUN4I_I2S_DMA_INT_CTRL_REG, 0x00000000 },
1227 { SUN4I_I2S_CLK_DIV_REG, 0x00000000 },
1228 { SUN8I_I2S_CHAN_CFG_REG, 0x00000000 },
1229 { SUN50I_H6_I2S_TX_CHAN_SEL_REG(0), 0x00000000 },
1230 { SUN50I_H6_I2S_TX_CHAN_MAP0_REG(0), 0x00000000 },
1231 { SUN50I_H6_I2S_TX_CHAN_MAP1_REG(0), 0x00000000 },
1232 { SUN50I_H6_I2S_RX_CHAN_SEL_REG, 0x00000000 },
1233 { SUN50I_H6_I2S_RX_CHAN_MAP0_REG, 0x00000000 },
1234 { SUN50I_H6_I2S_RX_CHAN_MAP1_REG, 0x00000000 },
1237 static const struct regmap_config sun4i_i2s_regmap_config = {
1241 .max_register = SUN4I_I2S_RX_CHAN_MAP_REG,
1243 .cache_type = REGCACHE_FLAT,
1244 .reg_defaults = sun4i_i2s_reg_defaults,
1245 .num_reg_defaults = ARRAY_SIZE(sun4i_i2s_reg_defaults),
1246 .writeable_reg = sun4i_i2s_wr_reg,
1247 .readable_reg = sun4i_i2s_rd_reg,
1248 .volatile_reg = sun4i_i2s_volatile_reg,
1251 static const struct regmap_config sun8i_i2s_regmap_config = {
1255 .max_register = SUN8I_I2S_RX_CHAN_MAP_REG,
1256 .cache_type = REGCACHE_FLAT,
1257 .reg_defaults = sun8i_i2s_reg_defaults,
1258 .num_reg_defaults = ARRAY_SIZE(sun8i_i2s_reg_defaults),
1259 .writeable_reg = sun4i_i2s_wr_reg,
1260 .readable_reg = sun8i_i2s_rd_reg,
1261 .volatile_reg = sun8i_i2s_volatile_reg,
1264 static const struct regmap_config sun50i_h6_i2s_regmap_config = {
1268 .max_register = SUN50I_R329_I2S_RX_CHAN_MAP3_REG,
1269 .cache_type = REGCACHE_FLAT,
1270 .reg_defaults = sun50i_h6_i2s_reg_defaults,
1271 .num_reg_defaults = ARRAY_SIZE(sun50i_h6_i2s_reg_defaults),
1272 .writeable_reg = sun4i_i2s_wr_reg,
1273 .readable_reg = sun8i_i2s_rd_reg,
1274 .volatile_reg = sun8i_i2s_volatile_reg,
1277 static int sun4i_i2s_runtime_resume(struct device *dev)
1279 struct sun4i_i2s *i2s = dev_get_drvdata(dev);
1282 ret = clk_prepare_enable(i2s->bus_clk);
1284 dev_err(dev, "Failed to enable bus clock\n");
1288 regcache_cache_only(i2s->regmap, false);
1289 regcache_mark_dirty(i2s->regmap);
1291 ret = regcache_sync(i2s->regmap);
1293 dev_err(dev, "Failed to sync regmap cache\n");
1294 goto err_disable_clk;
1297 /* Enable the whole hardware block */
1298 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1299 SUN4I_I2S_CTRL_GL_EN, SUN4I_I2S_CTRL_GL_EN);
1301 /* Enable the first output line */
1302 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1303 SUN4I_I2S_CTRL_SDO_EN_MASK,
1304 SUN4I_I2S_CTRL_SDO_EN(0));
1306 ret = clk_prepare_enable(i2s->mod_clk);
1308 dev_err(dev, "Failed to enable module clock\n");
1309 goto err_disable_clk;
1315 clk_disable_unprepare(i2s->bus_clk);
1319 static int sun4i_i2s_runtime_suspend(struct device *dev)
1321 struct sun4i_i2s *i2s = dev_get_drvdata(dev);
1323 clk_disable_unprepare(i2s->mod_clk);
1325 /* Disable our output lines */
1326 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1327 SUN4I_I2S_CTRL_SDO_EN_MASK, 0);
1329 /* Disable the whole hardware block */
1330 regmap_update_bits(i2s->regmap, SUN4I_I2S_CTRL_REG,
1331 SUN4I_I2S_CTRL_GL_EN, 0);
1333 regcache_cache_only(i2s->regmap, true);
1335 clk_disable_unprepare(i2s->bus_clk);
1340 static const struct sun4i_i2s_quirks sun4i_a10_i2s_quirks = {
1342 .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
1343 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
1344 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
1345 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
1346 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
1347 .bclk_dividers = sun4i_i2s_bclk_div,
1348 .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
1349 .mclk_dividers = sun4i_i2s_mclk_div,
1350 .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
1351 .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
1352 .get_sr = sun4i_i2s_get_sr,
1353 .get_wss = sun4i_i2s_get_wss,
1354 .set_chan_cfg = sun4i_i2s_set_chan_cfg,
1355 .set_fmt = sun4i_i2s_set_soc_fmt,
1358 static const struct sun4i_i2s_quirks sun6i_a31_i2s_quirks = {
1360 .reg_offset_txdata = SUN4I_I2S_FIFO_TX_REG,
1361 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
1362 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
1363 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
1364 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
1365 .bclk_dividers = sun4i_i2s_bclk_div,
1366 .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
1367 .mclk_dividers = sun4i_i2s_mclk_div,
1368 .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
1369 .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
1370 .get_sr = sun4i_i2s_get_sr,
1371 .get_wss = sun4i_i2s_get_wss,
1372 .set_chan_cfg = sun4i_i2s_set_chan_cfg,
1373 .set_fmt = sun4i_i2s_set_soc_fmt,
1377 * This doesn't describe the TDM controller documented in the A83t
1378 * datasheet, but the three undocumented I2S controller that use the
1381 static const struct sun4i_i2s_quirks sun8i_a83t_i2s_quirks = {
1383 .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
1384 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
1385 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
1386 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
1387 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
1388 .bclk_dividers = sun4i_i2s_bclk_div,
1389 .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
1390 .mclk_dividers = sun4i_i2s_mclk_div,
1391 .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
1392 .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
1393 .get_sr = sun4i_i2s_get_sr,
1394 .get_wss = sun4i_i2s_get_wss,
1395 .set_chan_cfg = sun4i_i2s_set_chan_cfg,
1396 .set_fmt = sun4i_i2s_set_soc_fmt,
1399 static const struct sun4i_i2s_quirks sun8i_h3_i2s_quirks = {
1401 .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
1402 .sun4i_i2s_regmap = &sun8i_i2s_regmap_config,
1403 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
1404 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
1405 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
1406 .bclk_dividers = sun8i_i2s_clk_div,
1407 .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1408 .mclk_dividers = sun8i_i2s_clk_div,
1409 .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1410 .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
1411 .get_sr = sun8i_i2s_get_sr_wss,
1412 .get_wss = sun8i_i2s_get_sr_wss,
1413 .set_chan_cfg = sun8i_i2s_set_chan_cfg,
1414 .set_fmt = sun8i_i2s_set_soc_fmt,
1417 static const struct sun4i_i2s_quirks sun50i_a64_codec_i2s_quirks = {
1419 .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
1420 .sun4i_i2s_regmap = &sun4i_i2s_regmap_config,
1421 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 7, 7),
1422 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 2, 3),
1423 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 5),
1424 .bclk_dividers = sun4i_i2s_bclk_div,
1425 .num_bclk_dividers = ARRAY_SIZE(sun4i_i2s_bclk_div),
1426 .mclk_dividers = sun4i_i2s_mclk_div,
1427 .num_mclk_dividers = ARRAY_SIZE(sun4i_i2s_mclk_div),
1428 .get_bclk_parent_rate = sun4i_i2s_get_bclk_parent_rate,
1429 .get_sr = sun4i_i2s_get_sr,
1430 .get_wss = sun4i_i2s_get_wss,
1431 .set_chan_cfg = sun4i_i2s_set_chan_cfg,
1432 .set_fmt = sun4i_i2s_set_soc_fmt,
1435 static const struct sun4i_i2s_quirks sun50i_h6_i2s_quirks = {
1437 .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
1438 .sun4i_i2s_regmap = &sun50i_h6_i2s_regmap_config,
1439 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
1440 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
1441 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
1442 .bclk_dividers = sun8i_i2s_clk_div,
1443 .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1444 .mclk_dividers = sun8i_i2s_clk_div,
1445 .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1446 .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
1447 .get_sr = sun8i_i2s_get_sr_wss,
1448 .get_wss = sun8i_i2s_get_sr_wss,
1449 .set_chan_cfg = sun50i_h6_i2s_set_chan_cfg,
1450 .set_fmt = sun50i_h6_i2s_set_soc_fmt,
1453 static const struct sun4i_i2s_quirks sun50i_r329_i2s_quirks = {
1455 .reg_offset_txdata = SUN8I_I2S_FIFO_TX_REG,
1456 .sun4i_i2s_regmap = &sun50i_h6_i2s_regmap_config,
1457 .field_clkdiv_mclk_en = REG_FIELD(SUN4I_I2S_CLK_DIV_REG, 8, 8),
1458 .field_fmt_wss = REG_FIELD(SUN4I_I2S_FMT0_REG, 0, 2),
1459 .field_fmt_sr = REG_FIELD(SUN4I_I2S_FMT0_REG, 4, 6),
1462 .bclk_dividers = sun8i_i2s_clk_div,
1463 .num_bclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1464 .mclk_dividers = sun8i_i2s_clk_div,
1465 .num_mclk_dividers = ARRAY_SIZE(sun8i_i2s_clk_div),
1466 .get_bclk_parent_rate = sun8i_i2s_get_bclk_parent_rate,
1467 .get_sr = sun8i_i2s_get_sr_wss,
1468 .get_wss = sun8i_i2s_get_sr_wss,
1469 .set_chan_cfg = sun50i_h6_i2s_set_chan_cfg,
1470 .set_fmt = sun50i_h6_i2s_set_soc_fmt,
1473 static int sun4i_i2s_init_regmap_fields(struct device *dev,
1474 struct sun4i_i2s *i2s)
1476 i2s->field_clkdiv_mclk_en =
1477 devm_regmap_field_alloc(dev, i2s->regmap,
1478 i2s->variant->field_clkdiv_mclk_en);
1479 if (IS_ERR(i2s->field_clkdiv_mclk_en))
1480 return PTR_ERR(i2s->field_clkdiv_mclk_en);
1482 i2s->field_fmt_wss =
1483 devm_regmap_field_alloc(dev, i2s->regmap,
1484 i2s->variant->field_fmt_wss);
1485 if (IS_ERR(i2s->field_fmt_wss))
1486 return PTR_ERR(i2s->field_fmt_wss);
1489 devm_regmap_field_alloc(dev, i2s->regmap,
1490 i2s->variant->field_fmt_sr);
1491 if (IS_ERR(i2s->field_fmt_sr))
1492 return PTR_ERR(i2s->field_fmt_sr);
1497 static int sun4i_i2s_probe(struct platform_device *pdev)
1499 struct sun4i_i2s *i2s;
1500 struct resource *res;
1504 i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL);
1507 platform_set_drvdata(pdev, i2s);
1509 regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
1511 return PTR_ERR(regs);
1513 irq = platform_get_irq(pdev, 0);
1517 i2s->variant = of_device_get_match_data(&pdev->dev);
1518 if (!i2s->variant) {
1519 dev_err(&pdev->dev, "Failed to determine the quirks to use\n");
1523 i2s->bus_clk = devm_clk_get(&pdev->dev, "apb");
1524 if (IS_ERR(i2s->bus_clk)) {
1525 dev_err(&pdev->dev, "Can't get our bus clock\n");
1526 return PTR_ERR(i2s->bus_clk);
1529 i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs,
1530 i2s->variant->sun4i_i2s_regmap);
1531 if (IS_ERR(i2s->regmap)) {
1532 dev_err(&pdev->dev, "Regmap initialisation failed\n");
1533 return PTR_ERR(i2s->regmap);
1536 i2s->mod_clk = devm_clk_get(&pdev->dev, "mod");
1537 if (IS_ERR(i2s->mod_clk)) {
1538 dev_err(&pdev->dev, "Can't get our mod clock\n");
1539 return PTR_ERR(i2s->mod_clk);
1542 if (i2s->variant->has_reset) {
1543 i2s->rst = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1544 if (IS_ERR(i2s->rst)) {
1545 dev_err(&pdev->dev, "Failed to get reset control\n");
1546 return PTR_ERR(i2s->rst);
1550 if (!IS_ERR(i2s->rst)) {
1551 ret = reset_control_deassert(i2s->rst);
1554 "Failed to deassert the reset control\n");
1559 i2s->playback_dma_data.addr = res->start +
1560 i2s->variant->reg_offset_txdata;
1561 i2s->playback_dma_data.maxburst = 8;
1563 i2s->capture_dma_data.addr = res->start + SUN4I_I2S_FIFO_RX_REG;
1564 i2s->capture_dma_data.maxburst = 8;
1566 pm_runtime_enable(&pdev->dev);
1567 if (!pm_runtime_enabled(&pdev->dev)) {
1568 ret = sun4i_i2s_runtime_resume(&pdev->dev);
1570 goto err_pm_disable;
1573 ret = sun4i_i2s_init_regmap_fields(&pdev->dev, i2s);
1575 dev_err(&pdev->dev, "Could not initialise regmap fields\n");
1579 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0);
1581 dev_err(&pdev->dev, "Could not register PCM\n");
1585 ret = devm_snd_soc_register_component(&pdev->dev,
1586 &sun4i_i2s_component,
1589 dev_err(&pdev->dev, "Could not register DAI\n");
1596 if (!pm_runtime_status_suspended(&pdev->dev))
1597 sun4i_i2s_runtime_suspend(&pdev->dev);
1599 pm_runtime_disable(&pdev->dev);
1600 if (!IS_ERR(i2s->rst))
1601 reset_control_assert(i2s->rst);
1606 static int sun4i_i2s_remove(struct platform_device *pdev)
1608 struct sun4i_i2s *i2s = dev_get_drvdata(&pdev->dev);
1610 pm_runtime_disable(&pdev->dev);
1611 if (!pm_runtime_status_suspended(&pdev->dev))
1612 sun4i_i2s_runtime_suspend(&pdev->dev);
1614 if (!IS_ERR(i2s->rst))
1615 reset_control_assert(i2s->rst);
1620 static const struct of_device_id sun4i_i2s_match[] = {
1622 .compatible = "allwinner,sun4i-a10-i2s",
1623 .data = &sun4i_a10_i2s_quirks,
1626 .compatible = "allwinner,sun6i-a31-i2s",
1627 .data = &sun6i_a31_i2s_quirks,
1630 .compatible = "allwinner,sun8i-a83t-i2s",
1631 .data = &sun8i_a83t_i2s_quirks,
1634 .compatible = "allwinner,sun8i-h3-i2s",
1635 .data = &sun8i_h3_i2s_quirks,
1638 .compatible = "allwinner,sun50i-a64-codec-i2s",
1639 .data = &sun50i_a64_codec_i2s_quirks,
1642 .compatible = "allwinner,sun50i-h6-i2s",
1643 .data = &sun50i_h6_i2s_quirks,
1646 .compatible = "allwinner,sun50i-r329-i2s",
1647 .data = &sun50i_r329_i2s_quirks,
1651 MODULE_DEVICE_TABLE(of, sun4i_i2s_match);
1653 static const struct dev_pm_ops sun4i_i2s_pm_ops = {
1654 .runtime_resume = sun4i_i2s_runtime_resume,
1655 .runtime_suspend = sun4i_i2s_runtime_suspend,
1658 static struct platform_driver sun4i_i2s_driver = {
1659 .probe = sun4i_i2s_probe,
1660 .remove = sun4i_i2s_remove,
1662 .name = "sun4i-i2s",
1663 .of_match_table = sun4i_i2s_match,
1664 .pm = &sun4i_i2s_pm_ops,
1667 module_platform_driver(sun4i_i2s_driver);
1669 MODULE_AUTHOR("Andrea Venturi <be17068@iperbole.bo.it>");
1670 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
1671 MODULE_DESCRIPTION("Allwinner A10 I2S driver");
1672 MODULE_LICENSE("GPL");