brcm2708: add kernel 4.14 support
[openwrt/staging/chunkeey.git] / target / linux / brcm2708 / patches-4.14 / 950-0115-ASoC-bcm2835-Add-support-for-TDM-modes.patch
1 From c2b82810c0272bb29a9b426b017b196436957c76 Mon Sep 17 00:00:00 2001
2 From: Matthias Reichl <hias@horus.com>
3 Date: Sun, 7 May 2017 11:34:26 +0200
4 Subject: [PATCH 115/454] ASoC: bcm2835: Add support for TDM modes
5
6 bcm2835 supports arbitrary positioning of channel data within
7 a frame and thus is capable of supporting TDM modes. Since
8 the driver is limited to 2-channel operations only TDM setups
9 with exactly 2 active slots are supported.
10
11 Logical TDM slot numbering follows the usual convention:
12
13 For I2S-like modes, with a 50% duty-cycle frame clock,
14 slots 0, 2, ... are transmitted in the first half of a frame,
15 slots 1, 3, ... are transmitted in the second half.
16
17 For DSP modes slot numbering is ascending: 0, 1, 2, 3, ...
18
19 Channel position calculation has been refactored to use
20 TDM info and moved out of hw_params.
21
22 set_tdm_slot, set_bclk_ratio and hw_params now check more
23 strictly if the configuration is valid. Illegal configurations
24 like odd number of slots in I2S mode, data lengths exceeding
25 slot width or frame sizes larger than the hardware limit of
26 1024 are rejected. Also hw_params now properly checks for
27 errors from clk_set_rate.
28
29 Allowed PCM formats are already guarded by stream constraints,
30 thus the formats check in hw_params has been removed and
31 data_length is now retrieved via params_width().
32
33 Also standard functions like snd_soc_params_to_bclk are now
34 being used instead of manual calculations to make the code
35 more readable.
36
37 Special care has been taken to ensure that set_bclk_ratio works
38 as before. The bclk ratio is mapped to a 2-channel TDM config
39 with a slot width of half the ratio. In order to support odd ratios,
40 which can't be expressed via a TDM config, the ratio (frame length)
41 is stored and used by hw_params.
42
43 Signed-off-by: Matthias Reichl <hias@horus.com>
44 ---
45 sound/soc/bcm/bcm2835-i2s.c | 243 ++++++++++++++++++++++++++++--------
46 1 file changed, 190 insertions(+), 53 deletions(-)
47
48 --- a/sound/soc/bcm/bcm2835-i2s.c
49 +++ b/sound/soc/bcm/bcm2835-i2s.c
50 @@ -31,6 +31,7 @@
51 * General Public License for more details.
52 */
53
54 +#include <linux/bitops.h>
55 #include <linux/clk.h>
56 #include <linux/delay.h>
57 #include <linux/device.h>
58 @@ -99,6 +100,8 @@
59 #define BCM2835_I2S_CHWID(v) (v)
60 #define BCM2835_I2S_CH1(v) ((v) << 16)
61 #define BCM2835_I2S_CH2(v) (v)
62 +#define BCM2835_I2S_CH1_POS(v) BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(v))
63 +#define BCM2835_I2S_CH2_POS(v) BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(v))
64
65 #define BCM2835_I2S_TX_PANIC(v) ((v) << 24)
66 #define BCM2835_I2S_RX_PANIC(v) ((v) << 16)
67 @@ -110,12 +113,19 @@
68 #define BCM2835_I2S_INT_RXR BIT(1)
69 #define BCM2835_I2S_INT_TXW BIT(0)
70
71 +/* Frame length register is 10 bit, maximum length 1024 */
72 +#define BCM2835_I2S_MAX_FRAME_LENGTH 1024
73 +
74 /* General device struct */
75 struct bcm2835_i2s_dev {
76 struct device *dev;
77 struct snd_dmaengine_dai_dma_data dma_data[2];
78 unsigned int fmt;
79 - unsigned int bclk_ratio;
80 + unsigned int tdm_slots;
81 + unsigned int rx_mask;
82 + unsigned int tx_mask;
83 + unsigned int slot_width;
84 + unsigned int frame_length;
85
86 struct regmap *i2s_regmap;
87 struct clk *clk;
88 @@ -225,19 +235,117 @@ static int bcm2835_i2s_set_dai_bclk_rati
89 unsigned int ratio)
90 {
91 struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
92 - dev->bclk_ratio = ratio;
93 +
94 + if (!ratio) {
95 + dev->tdm_slots = 0;
96 + return 0;
97 + }
98 +
99 + if (ratio > BCM2835_I2S_MAX_FRAME_LENGTH)
100 + return -EINVAL;
101 +
102 + dev->tdm_slots = 2;
103 + dev->rx_mask = 0x03;
104 + dev->tx_mask = 0x03;
105 + dev->slot_width = ratio / 2;
106 + dev->frame_length = ratio;
107 +
108 return 0;
109 }
110
111 +static int bcm2835_i2s_set_dai_tdm_slot(struct snd_soc_dai *dai,
112 + unsigned int tx_mask, unsigned int rx_mask,
113 + int slots, int width)
114 +{
115 + struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
116 +
117 + if (slots) {
118 + if (slots < 0 || width < 0)
119 + return -EINVAL;
120 +
121 + /* Limit masks to available slots */
122 + rx_mask &= GENMASK(slots - 1, 0);
123 + tx_mask &= GENMASK(slots - 1, 0);
124 +
125 + /*
126 + * The driver is limited to 2-channel setups.
127 + * Check that exactly 2 bits are set in the masks.
128 + */
129 + if (hweight_long((unsigned long) rx_mask) != 2
130 + || hweight_long((unsigned long) tx_mask) != 2)
131 + return -EINVAL;
132 +
133 + if (slots * width > BCM2835_I2S_MAX_FRAME_LENGTH)
134 + return -EINVAL;
135 + }
136 +
137 + dev->tdm_slots = slots;
138 +
139 + dev->rx_mask = rx_mask;
140 + dev->tx_mask = tx_mask;
141 + dev->slot_width = width;
142 + dev->frame_length = slots * width;
143 +
144 + return 0;
145 +}
146 +
147 +/*
148 + * Convert logical slot number into physical slot number.
149 + *
150 + * If odd_offset is 0 sequential number is identical to logical number.
151 + * This is used for DSP modes with slot numbering 0 1 2 3 ...
152 + *
153 + * Otherwise odd_offset defines the physical offset for odd numbered
154 + * slots. This is used for I2S and left/right justified modes to
155 + * translate from logical slot numbers 0 1 2 3 ... into physical slot
156 + * numbers 0 2 ... 3 4 ...
157 + */
158 +static int bcm2835_i2s_convert_slot(unsigned int slot, unsigned int odd_offset)
159 +{
160 + if (!odd_offset)
161 + return slot;
162 +
163 + if (slot & 1)
164 + return (slot >> 1) + odd_offset;
165 +
166 + return slot >> 1;
167 +}
168 +
169 +/*
170 + * Calculate channel position from mask and slot width.
171 + *
172 + * Mask must contain exactly 2 set bits.
173 + * Lowest set bit is channel 1 position, highest set bit channel 2.
174 + * The constant offset is added to both channel positions.
175 + *
176 + * If odd_offset is > 0 slot positions are translated to
177 + * I2S-style TDM slot numbering ( 0 2 ... 3 4 ...) with odd
178 + * logical slot numbers starting at physical slot odd_offset.
179 + */
180 +static void bcm2835_i2s_calc_channel_pos(
181 + unsigned int *ch1_pos, unsigned int *ch2_pos,
182 + unsigned int mask, unsigned int width,
183 + unsigned int bit_offset, unsigned int odd_offset)
184 +{
185 + *ch1_pos = bcm2835_i2s_convert_slot((ffs(mask) - 1), odd_offset)
186 + * width + bit_offset;
187 + *ch2_pos = bcm2835_i2s_convert_slot((fls(mask) - 1), odd_offset)
188 + * width + bit_offset;
189 +}
190 +
191 static int bcm2835_i2s_hw_params(struct snd_pcm_substream *substream,
192 struct snd_pcm_hw_params *params,
193 struct snd_soc_dai *dai)
194 {
195 struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
196 - unsigned int sampling_rate = params_rate(params);
197 - unsigned int data_length, data_delay, bclk_ratio;
198 - unsigned int ch1pos, ch2pos, mode, format;
199 + unsigned int data_length, data_delay, framesync_length;
200 + unsigned int slots, slot_width, odd_slot_offset;
201 + int frame_length, bclk_rate;
202 + unsigned int rx_mask, tx_mask;
203 + unsigned int rx_ch1_pos, rx_ch2_pos, tx_ch1_pos, tx_ch2_pos;
204 + unsigned int mode, format;
205 uint32_t csreg;
206 + int ret = 0;
207
208 /*
209 * If a stream is already enabled,
210 @@ -248,39 +356,44 @@ static int bcm2835_i2s_hw_params(struct
211 if (csreg & (BCM2835_I2S_TXON | BCM2835_I2S_RXON))
212 return 0;
213
214 - /*
215 - * Adjust the data length according to the format.
216 - * We prefill the half frame length with an integer
217 - * divider of 2400 as explained at the clock settings.
218 - * Maybe it is overwritten there, if the Integer mode
219 - * does not apply.
220 - */
221 - switch (params_format(params)) {
222 - case SNDRV_PCM_FORMAT_S16_LE:
223 - data_length = 16;
224 - break;
225 - case SNDRV_PCM_FORMAT_S24_LE:
226 - data_length = 24;
227 - break;
228 - case SNDRV_PCM_FORMAT_S32_LE:
229 - data_length = 32;
230 - break;
231 - default:
232 - return -EINVAL;
233 + data_length = params_width(params);
234 + data_delay = 0;
235 + odd_slot_offset = 0;
236 + mode = 0;
237 +
238 + if (dev->tdm_slots) {
239 + slots = dev->tdm_slots;
240 + slot_width = dev->slot_width;
241 + frame_length = dev->frame_length;
242 + rx_mask = dev->rx_mask;
243 + tx_mask = dev->tx_mask;
244 + bclk_rate = dev->frame_length * params_rate(params);
245 + } else {
246 + slots = 2;
247 + slot_width = params_width(params);
248 + rx_mask = 0x03;
249 + tx_mask = 0x03;
250 +
251 + frame_length = snd_soc_params_to_frame_size(params);
252 + if (frame_length < 0)
253 + return frame_length;
254 +
255 + bclk_rate = snd_soc_params_to_bclk(params);
256 + if (bclk_rate < 0)
257 + return bclk_rate;
258 }
259
260 - /* If bclk_ratio already set, use that one. */
261 - if (dev->bclk_ratio)
262 - bclk_ratio = dev->bclk_ratio;
263 - else
264 - /* otherwise calculate a fitting block ratio */
265 - bclk_ratio = 2 * data_length;
266 + /* Check if data fits into slots */
267 + if (data_length > slot_width)
268 + return -EINVAL;
269
270 /* Clock should only be set up here if CPU is clock master */
271 switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
272 case SND_SOC_DAIFMT_CBS_CFS:
273 case SND_SOC_DAIFMT_CBS_CFM:
274 - clk_set_rate(dev->clk, sampling_rate * bclk_ratio);
275 + ret = clk_set_rate(dev->clk, bclk_rate);
276 + if (ret)
277 + return ret;
278 break;
279 default:
280 break;
281 @@ -294,9 +407,26 @@ static int bcm2835_i2s_hw_params(struct
282
283 format |= BCM2835_I2S_CHWID((data_length-8)&0xf);
284
285 + /* CH2 format is the same as for CH1 */
286 + format = BCM2835_I2S_CH1(format) | BCM2835_I2S_CH2(format);
287 +
288 switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
289 case SND_SOC_DAIFMT_I2S:
290 + /* I2S mode needs an even number of slots */
291 + if (slots & 1)
292 + return -EINVAL;
293 +
294 + /*
295 + * Use I2S-style logical slot numbering: even slots
296 + * are in first half of frame, odd slots in second half.
297 + */
298 + odd_slot_offset = slots >> 1;
299 +
300 + /* MSB starts one cycle after frame start */
301 data_delay = 1;
302 +
303 + /* Setup frame sync signal for 50% duty cycle */
304 + framesync_length = frame_length / 2;
305 break;
306 default:
307 /*
308 @@ -307,19 +437,10 @@ static int bcm2835_i2s_hw_params(struct
309 return -EINVAL;
310 }
311
312 - ch1pos = data_delay;
313 - ch2pos = bclk_ratio / 2 + data_delay;
314 -
315 - switch (params_channels(params)) {
316 - case 2:
317 - case 8:
318 - format = BCM2835_I2S_CH1(format) | BCM2835_I2S_CH2(format);
319 - format |= BCM2835_I2S_CH1(BCM2835_I2S_CHPOS(ch1pos));
320 - format |= BCM2835_I2S_CH2(BCM2835_I2S_CHPOS(ch2pos));
321 - break;
322 - default:
323 - return -EINVAL;
324 - }
325 + bcm2835_i2s_calc_channel_pos(&rx_ch1_pos, &rx_ch2_pos,
326 + rx_mask, slot_width, data_delay, odd_slot_offset);
327 + bcm2835_i2s_calc_channel_pos(&tx_ch1_pos, &tx_ch2_pos,
328 + tx_mask, slot_width, data_delay, odd_slot_offset);
329
330 /*
331 * Set format for both streams.
332 @@ -327,11 +448,16 @@ static int bcm2835_i2s_hw_params(struct
333 * (and therefore word length) anyway,
334 * so the format will be the same.
335 */
336 - regmap_write(dev->i2s_regmap, BCM2835_I2S_RXC_A_REG, format);
337 - regmap_write(dev->i2s_regmap, BCM2835_I2S_TXC_A_REG, format);
338 + regmap_write(dev->i2s_regmap, BCM2835_I2S_RXC_A_REG,
339 + format
340 + | BCM2835_I2S_CH1_POS(rx_ch1_pos)
341 + | BCM2835_I2S_CH2_POS(rx_ch2_pos));
342 + regmap_write(dev->i2s_regmap, BCM2835_I2S_TXC_A_REG,
343 + format
344 + | BCM2835_I2S_CH1_POS(tx_ch1_pos)
345 + | BCM2835_I2S_CH2_POS(tx_ch2_pos));
346
347 /* Setup the I2S mode */
348 - mode = 0;
349
350 if (data_length <= 16) {
351 /*
352 @@ -343,8 +469,8 @@ static int bcm2835_i2s_hw_params(struct
353 mode |= BCM2835_I2S_FTXP | BCM2835_I2S_FRXP;
354 }
355
356 - mode |= BCM2835_I2S_FLEN(bclk_ratio - 1);
357 - mode |= BCM2835_I2S_FSLEN(bclk_ratio / 2);
358 + mode |= BCM2835_I2S_FLEN(frame_length - 1);
359 + mode |= BCM2835_I2S_FSLEN(framesync_length);
360
361 /* Master or slave? */
362 switch (dev->fmt & SND_SOC_DAIFMT_MASTER_MASK) {
363 @@ -424,7 +550,20 @@ static int bcm2835_i2s_hw_params(struct
364 /* Clear FIFOs */
365 bcm2835_i2s_clear_fifos(dev, true, true);
366
367 - return 0;
368 + dev_dbg(dev->dev,
369 + "slots: %d width: %d rx mask: 0x%02x tx_mask: 0x%02x\n",
370 + slots, slot_width, rx_mask, tx_mask);
371 +
372 + dev_dbg(dev->dev, "frame len: %d sync len: %d data len: %d\n",
373 + frame_length, framesync_length, data_length);
374 +
375 + dev_dbg(dev->dev, "rx pos: %d,%d tx pos: %d,%d\n",
376 + rx_ch1_pos, rx_ch2_pos, tx_ch1_pos, tx_ch2_pos);
377 +
378 + dev_dbg(dev->dev, "sampling rate: %d bclk rate: %d\n",
379 + params_rate(params), bclk_rate);
380 +
381 + return ret;
382 }
383
384 static int bcm2835_i2s_prepare(struct snd_pcm_substream *substream,
385 @@ -560,6 +699,7 @@ static const struct snd_soc_dai_ops bcm2
386 .hw_params = bcm2835_i2s_hw_params,
387 .set_fmt = bcm2835_i2s_set_dai_fmt,
388 .set_bclk_ratio = bcm2835_i2s_set_dai_bclk_ratio,
389 + .set_tdm_slot = bcm2835_i2s_set_dai_tdm_slot,
390 };
391
392 static int bcm2835_i2s_dai_probe(struct snd_soc_dai *dai)
393 @@ -700,9 +840,6 @@ static int bcm2835_i2s_probe(struct plat
394 dev->dma_data[SNDRV_PCM_STREAM_CAPTURE].flags =
395 SND_DMAENGINE_PCM_DAI_FLAG_PACK;
396
397 - /* BCLK ratio - use default */
398 - dev->bclk_ratio = 0;
399 -
400 /* Store the pdev */
401 dev->dev = &pdev->dev;
402 dev_set_drvdata(&pdev->dev, dev);