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
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.
11 Logical TDM slot numbering follows the usual convention:
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.
17 For DSP modes slot numbering is ascending: 0, 1, 2, 3, ...
19 Channel position calculation has been refactored to use
20 TDM info and moved out of hw_params.
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.
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().
33 Also standard functions like snd_soc_params_to_bclk are now
34 being used instead of manual calculations to make the code
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.
43 Signed-off-by: Matthias Reichl <hias@horus.com>
45 sound/soc/bcm/bcm2835-i2s.c | 243 ++++++++++++++++++++++++++++--------
46 1 file changed, 190 insertions(+), 53 deletions(-)
48 --- a/sound/soc/bcm/bcm2835-i2s.c
49 +++ b/sound/soc/bcm/bcm2835-i2s.c
51 * General Public License for more details.
54 +#include <linux/bitops.h>
55 #include <linux/clk.h>
56 #include <linux/delay.h>
57 #include <linux/device.h>
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))
65 #define BCM2835_I2S_TX_PANIC(v) ((v) << 24)
66 #define BCM2835_I2S_RX_PANIC(v) ((v) << 16)
68 #define BCM2835_I2S_INT_RXR BIT(1)
69 #define BCM2835_I2S_INT_TXW BIT(0)
71 +/* Frame length register is 10 bit, maximum length 1024 */
72 +#define BCM2835_I2S_MAX_FRAME_LENGTH 1024
74 /* General device struct */
75 struct bcm2835_i2s_dev {
77 struct snd_dmaengine_dai_dma_data dma_data[2];
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;
86 struct regmap *i2s_regmap;
88 @@ -225,19 +235,117 @@ static int bcm2835_i2s_set_dai_bclk_rati
91 struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
92 - dev->bclk_ratio = ratio;
99 + if (ratio > BCM2835_I2S_MAX_FRAME_LENGTH)
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;
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)
115 + struct bcm2835_i2s_dev *dev = snd_soc_dai_get_drvdata(dai);
118 + if (slots < 0 || width < 0)
121 + /* Limit masks to available slots */
122 + rx_mask &= GENMASK(slots - 1, 0);
123 + tx_mask &= GENMASK(slots - 1, 0);
126 + * The driver is limited to 2-channel setups.
127 + * Check that exactly 2 bits are set in the masks.
129 + if (hweight_long((unsigned long) rx_mask) != 2
130 + || hweight_long((unsigned long) tx_mask) != 2)
133 + if (slots * width > BCM2835_I2S_MAX_FRAME_LENGTH)
137 + dev->tdm_slots = slots;
139 + dev->rx_mask = rx_mask;
140 + dev->tx_mask = tx_mask;
141 + dev->slot_width = width;
142 + dev->frame_length = slots * width;
148 + * Convert logical slot number into physical slot number.
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 ...
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 ...
158 +static int bcm2835_i2s_convert_slot(unsigned int slot, unsigned int odd_offset)
164 + return (slot >> 1) + odd_offset;
170 + * Calculate channel position from mask and slot width.
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.
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.
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)
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;
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)
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;
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))
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
221 - switch (params_format(params)) {
222 - case SNDRV_PCM_FORMAT_S16_LE:
225 - case SNDRV_PCM_FORMAT_S24_LE:
228 - case SNDRV_PCM_FORMAT_S32_LE:
233 + data_length = params_width(params);
235 + odd_slot_offset = 0;
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);
247 + slot_width = params_width(params);
251 + frame_length = snd_soc_params_to_frame_size(params);
252 + if (frame_length < 0)
253 + return frame_length;
255 + bclk_rate = snd_soc_params_to_bclk(params);
260 - /* If bclk_ratio already set, use that one. */
261 - if (dev->bclk_ratio)
262 - bclk_ratio = dev->bclk_ratio;
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)
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);
281 @@ -294,9 +407,26 @@ static int bcm2835_i2s_hw_params(struct
283 format |= BCM2835_I2S_CHWID((data_length-8)&0xf);
285 + /* CH2 format is the same as for CH1 */
286 + format = BCM2835_I2S_CH1(format) | BCM2835_I2S_CH2(format);
288 switch (dev->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
289 case SND_SOC_DAIFMT_I2S:
290 + /* I2S mode needs an even number of slots */
295 + * Use I2S-style logical slot numbering: even slots
296 + * are in first half of frame, odd slots in second half.
298 + odd_slot_offset = slots >> 1;
300 + /* MSB starts one cycle after frame start */
303 + /* Setup frame sync signal for 50% duty cycle */
304 + framesync_length = frame_length / 2;
308 @@ -307,19 +437,10 @@ static int bcm2835_i2s_hw_params(struct
312 - ch1pos = data_delay;
313 - ch2pos = bclk_ratio / 2 + data_delay;
315 - switch (params_channels(params)) {
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));
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);
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.
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,
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,
344 + | BCM2835_I2S_CH1_POS(tx_ch1_pos)
345 + | BCM2835_I2S_CH2_POS(tx_ch2_pos));
347 /* Setup the I2S mode */
350 if (data_length <= 16) {
352 @@ -343,8 +469,8 @@ static int bcm2835_i2s_hw_params(struct
353 mode |= BCM2835_I2S_FTXP | BCM2835_I2S_FRXP;
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);
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
365 bcm2835_i2s_clear_fifos(dev, true, true);
369 + "slots: %d width: %d rx mask: 0x%02x tx_mask: 0x%02x\n",
370 + slots, slot_width, rx_mask, tx_mask);
372 + dev_dbg(dev->dev, "frame len: %d sync len: %d data len: %d\n",
373 + frame_length, framesync_length, data_length);
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);
378 + dev_dbg(dev->dev, "sampling rate: %d bclk rate: %d\n",
379 + params_rate(params), bclk_rate);
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,
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;
397 - /* BCLK ratio - use default */
398 - dev->bclk_ratio = 0;
401 dev->dev = &pdev->dev;
402 dev_set_drvdata(&pdev->dev, dev);