]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - sound/soc/sti/uniperif_reader.c
UBUNTU: Ubuntu-4.10.0-37.41
[mirror_ubuntu-zesty-kernel.git] / sound / soc / sti / uniperif_reader.c
1 /*
2 * Copyright (C) STMicroelectronics SA 2015
3 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
4 * for STMicroelectronics.
5 * License terms: GNU General Public License (GPL), version 2
6 */
7
8 #include <sound/soc.h>
9
10 #include "uniperif.h"
11
12 #define UNIPERIF_READER_I2S_IN 0 /* reader id connected to I2S/TDM TX bus */
13 /*
14 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
15 * integrate unireader capability in term of rate and supported channels
16 */
17 static const struct snd_pcm_hardware uni_reader_pcm_hw = {
18 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
19 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
20 SNDRV_PCM_INFO_MMAP_VALID,
21 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
22
23 .rates = SNDRV_PCM_RATE_CONTINUOUS,
24 .rate_min = 8000,
25 .rate_max = 96000,
26
27 .channels_min = 2,
28 .channels_max = 8,
29
30 .periods_min = 2,
31 .periods_max = 48,
32
33 .period_bytes_min = 128,
34 .period_bytes_max = 64 * PAGE_SIZE,
35 .buffer_bytes_max = 256 * PAGE_SIZE
36 };
37
38 /*
39 * uni_reader_irq_handler
40 * In case of error audio stream is stopped; stop action is protected via PCM
41 * stream lock to avoid race condition with trigger callback.
42 */
43 static irqreturn_t uni_reader_irq_handler(int irq, void *dev_id)
44 {
45 irqreturn_t ret = IRQ_NONE;
46 struct uniperif *reader = dev_id;
47 unsigned int status;
48
49 if (reader->state == UNIPERIF_STATE_STOPPED) {
50 /* Unexpected IRQ: do nothing */
51 dev_warn(reader->dev, "unexpected IRQ\n");
52 return IRQ_HANDLED;
53 }
54
55 /* Get interrupt status & clear them immediately */
56 status = GET_UNIPERIF_ITS(reader);
57 SET_UNIPERIF_ITS_BCLR(reader, status);
58
59 /* Check for fifo overflow error */
60 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(reader))) {
61 dev_err(reader->dev, "FIFO error detected\n");
62
63 snd_pcm_stream_lock(reader->substream);
64 snd_pcm_stop(reader->substream, SNDRV_PCM_STATE_XRUN);
65 snd_pcm_stream_unlock(reader->substream);
66
67 return IRQ_HANDLED;
68 }
69
70 return ret;
71 }
72
73 static int uni_reader_prepare_pcm(struct snd_pcm_runtime *runtime,
74 struct uniperif *reader)
75 {
76 int slot_width;
77
78 /* Force slot width to 32 in I2S mode */
79 if ((reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK)
80 == SND_SOC_DAIFMT_I2S) {
81 slot_width = 32;
82 } else {
83 switch (runtime->format) {
84 case SNDRV_PCM_FORMAT_S16_LE:
85 slot_width = 16;
86 break;
87 default:
88 slot_width = 32;
89 break;
90 }
91 }
92
93 /* Number of bits per subframe (i.e one channel sample) on input. */
94 switch (slot_width) {
95 case 32:
96 SET_UNIPERIF_I2S_FMT_NBIT_32(reader);
97 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader);
98 break;
99 case 16:
100 SET_UNIPERIF_I2S_FMT_NBIT_16(reader);
101 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(reader);
102 break;
103 default:
104 dev_err(reader->dev, "subframe format not supported\n");
105 return -EINVAL;
106 }
107
108 /* Configure data memory format */
109 switch (runtime->format) {
110 case SNDRV_PCM_FORMAT_S16_LE:
111 /* One data word contains two samples */
112 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(reader);
113 break;
114
115 case SNDRV_PCM_FORMAT_S32_LE:
116 /*
117 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
118 * on the MSB then zeros (if less than 32 bytes)"...
119 */
120 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader);
121 break;
122
123 default:
124 dev_err(reader->dev, "format not supported\n");
125 return -EINVAL;
126 }
127
128 /* Number of channels must be even */
129 if ((runtime->channels % 2) || (runtime->channels < 2) ||
130 (runtime->channels > 10)) {
131 dev_err(reader->dev, "%s: invalid nb of channels\n", __func__);
132 return -EINVAL;
133 }
134
135 SET_UNIPERIF_I2S_FMT_NUM_CH(reader, runtime->channels / 2);
136 SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader);
137
138 return 0;
139 }
140
141 static int uni_reader_prepare_tdm(struct snd_pcm_runtime *runtime,
142 struct uniperif *reader)
143 {
144 int frame_size; /* user tdm frame size in bytes */
145 /* default unip TDM_WORD_POS_X_Y */
146 unsigned int word_pos[4] = {
147 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
148
149 frame_size = sti_uniperiph_get_user_frame_size(runtime);
150
151 /* fix 16/0 format */
152 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader);
153 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader);
154
155 /* number of words inserted on the TDM line */
156 SET_UNIPERIF_I2S_FMT_NUM_CH(reader, frame_size / 4 / 2);
157
158 SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader);
159 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
160 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(reader);
161
162 /*
163 * set the timeslots allocation for words in FIFO
164 *
165 * HW bug: (LSB word < MSB word) => this config is not possible
166 * So if we want (LSB word < MSB) word, then it shall be
167 * handled by user
168 */
169 sti_uniperiph_get_tdm_word_pos(reader, word_pos);
170 SET_UNIPERIF_TDM_WORD_POS(reader, 1_2, word_pos[WORD_1_2]);
171 SET_UNIPERIF_TDM_WORD_POS(reader, 3_4, word_pos[WORD_3_4]);
172 SET_UNIPERIF_TDM_WORD_POS(reader, 5_6, word_pos[WORD_5_6]);
173 SET_UNIPERIF_TDM_WORD_POS(reader, 7_8, word_pos[WORD_7_8]);
174
175 return 0;
176 }
177
178 static int uni_reader_prepare(struct snd_pcm_substream *substream,
179 struct snd_soc_dai *dai)
180 {
181 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
182 struct uniperif *reader = priv->dai_data.uni;
183 struct snd_pcm_runtime *runtime = substream->runtime;
184 int transfer_size, trigger_limit, ret;
185
186 /* The reader should be stopped */
187 if (reader->state != UNIPERIF_STATE_STOPPED) {
188 dev_err(reader->dev, "%s: invalid reader state %d\n", __func__,
189 reader->state);
190 return -EINVAL;
191 }
192
193 /* Calculate transfer size (in fifo cells and bytes) for frame count */
194 if (reader->type == SND_ST_UNIPERIF_TYPE_TDM) {
195 /* transfer size = unip frame size (in 32 bits FIFO cell) */
196 transfer_size =
197 sti_uniperiph_get_user_frame_size(runtime) / 4;
198 } else {
199 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
200 }
201
202 /* Calculate number of empty cells available before asserting DREQ */
203 if (reader->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
204 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
205 else
206 /*
207 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
208 * FDMA_TRIGGER_LIMIT also controls when the state switches
209 * from OFF or STANDBY to AUDIO DATA.
210 */
211 trigger_limit = transfer_size;
212
213 /* Trigger limit must be an even number */
214 if ((!trigger_limit % 2) ||
215 (trigger_limit != 1 && transfer_size % 2) ||
216 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(reader))) {
217 dev_err(reader->dev, "invalid trigger limit %d\n",
218 trigger_limit);
219 return -EINVAL;
220 }
221
222 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(reader, trigger_limit);
223
224 if (UNIPERIF_TYPE_IS_TDM(reader))
225 ret = uni_reader_prepare_tdm(runtime, reader);
226 else
227 ret = uni_reader_prepare_pcm(runtime, reader);
228 if (ret)
229 return ret;
230
231 switch (reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
232 case SND_SOC_DAIFMT_I2S:
233 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
234 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(reader);
235 break;
236 case SND_SOC_DAIFMT_LEFT_J:
237 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
238 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
239 break;
240 case SND_SOC_DAIFMT_RIGHT_J:
241 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(reader);
242 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
243 break;
244 default:
245 dev_err(reader->dev, "format not supported\n");
246 return -EINVAL;
247 }
248
249 /* Data clocking (changing) on the rising/falling edge */
250 switch (reader->daifmt & SND_SOC_DAIFMT_INV_MASK) {
251 case SND_SOC_DAIFMT_NB_NF:
252 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader);
253 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader);
254 break;
255 case SND_SOC_DAIFMT_NB_IF:
256 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader);
257 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader);
258 break;
259 case SND_SOC_DAIFMT_IB_NF:
260 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader);
261 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader);
262 break;
263 case SND_SOC_DAIFMT_IB_IF:
264 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader);
265 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader);
266 break;
267 }
268
269 /* Clear any pending interrupts */
270 SET_UNIPERIF_ITS_BCLR(reader, GET_UNIPERIF_ITS(reader));
271
272 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(reader, 0);
273
274 /* Set the interrupt mask */
275 SET_UNIPERIF_ITM_BSET_DMA_ERROR(reader);
276 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
277 SET_UNIPERIF_ITM_BSET_MEM_BLK_READ(reader);
278
279 /* Enable underflow recovery interrupts */
280 if (reader->underflow_enabled) {
281 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(reader);
282 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(reader);
283 }
284
285 /* Reset uniperipheral reader */
286 return sti_uniperiph_reset(reader);
287 }
288
289 static int uni_reader_start(struct uniperif *reader)
290 {
291 /* The reader should be stopped */
292 if (reader->state != UNIPERIF_STATE_STOPPED) {
293 dev_err(reader->dev, "%s: invalid reader state\n", __func__);
294 return -EINVAL;
295 }
296
297 /* Enable reader interrupts (and clear possible stalled ones) */
298 SET_UNIPERIF_ITS_BCLR_FIFO_ERROR(reader);
299 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
300
301 /* Launch the reader */
302 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(reader);
303
304 /* Update state to started */
305 reader->state = UNIPERIF_STATE_STARTED;
306 return 0;
307 }
308
309 static int uni_reader_stop(struct uniperif *reader)
310 {
311 /* The reader should not be in stopped state */
312 if (reader->state == UNIPERIF_STATE_STOPPED) {
313 dev_err(reader->dev, "%s: invalid reader state\n", __func__);
314 return -EINVAL;
315 }
316
317 /* Turn the reader off */
318 SET_UNIPERIF_CTRL_OPERATION_OFF(reader);
319
320 /* Disable interrupts */
321 SET_UNIPERIF_ITM_BCLR(reader, GET_UNIPERIF_ITM(reader));
322
323 /* Update state to stopped and return */
324 reader->state = UNIPERIF_STATE_STOPPED;
325
326 return 0;
327 }
328
329 static int uni_reader_trigger(struct snd_pcm_substream *substream,
330 int cmd, struct snd_soc_dai *dai)
331 {
332 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
333 struct uniperif *reader = priv->dai_data.uni;
334
335 switch (cmd) {
336 case SNDRV_PCM_TRIGGER_START:
337 return uni_reader_start(reader);
338 case SNDRV_PCM_TRIGGER_STOP:
339 return uni_reader_stop(reader);
340 default:
341 return -EINVAL;
342 }
343 }
344
345 static int uni_reader_startup(struct snd_pcm_substream *substream,
346 struct snd_soc_dai *dai)
347 {
348 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
349 struct uniperif *reader = priv->dai_data.uni;
350 int ret;
351
352 if (!UNIPERIF_TYPE_IS_TDM(reader))
353 return 0;
354
355 /* refine hw constraint in tdm mode */
356 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
357 SNDRV_PCM_HW_PARAM_CHANNELS,
358 sti_uniperiph_fix_tdm_chan,
359 reader, SNDRV_PCM_HW_PARAM_CHANNELS,
360 -1);
361 if (ret < 0)
362 return ret;
363
364 return snd_pcm_hw_rule_add(substream->runtime, 0,
365 SNDRV_PCM_HW_PARAM_FORMAT,
366 sti_uniperiph_fix_tdm_format,
367 reader, SNDRV_PCM_HW_PARAM_FORMAT,
368 -1);
369 }
370
371 static void uni_reader_shutdown(struct snd_pcm_substream *substream,
372 struct snd_soc_dai *dai)
373 {
374 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
375 struct uniperif *reader = priv->dai_data.uni;
376
377 if (reader->state != UNIPERIF_STATE_STOPPED) {
378 /* Stop the reader */
379 uni_reader_stop(reader);
380 }
381 }
382
383 static const struct snd_soc_dai_ops uni_reader_dai_ops = {
384 .startup = uni_reader_startup,
385 .shutdown = uni_reader_shutdown,
386 .prepare = uni_reader_prepare,
387 .trigger = uni_reader_trigger,
388 .hw_params = sti_uniperiph_dai_hw_params,
389 .set_fmt = sti_uniperiph_dai_set_fmt,
390 .set_tdm_slot = sti_uniperiph_set_tdm_slot
391 };
392
393 int uni_reader_init(struct platform_device *pdev,
394 struct uniperif *reader)
395 {
396 int ret = 0;
397
398 reader->dev = &pdev->dev;
399 reader->state = UNIPERIF_STATE_STOPPED;
400 reader->dai_ops = &uni_reader_dai_ops;
401
402 if (UNIPERIF_TYPE_IS_TDM(reader))
403 reader->hw = &uni_tdm_hw;
404 else
405 reader->hw = &uni_reader_pcm_hw;
406
407 ret = devm_request_irq(&pdev->dev, reader->irq,
408 uni_reader_irq_handler, IRQF_SHARED,
409 dev_name(&pdev->dev), reader);
410 if (ret < 0) {
411 dev_err(&pdev->dev, "Failed to request IRQ\n");
412 return -EBUSY;
413 }
414
415 return 0;
416 }
417 EXPORT_SYMBOL_GPL(uni_reader_init);