1
2
3
4
5
6
7
8#include <sound/soc.h>
9
10#include "uniperif.h"
11
12#define UNIPERIF_READER_I2S_IN 0
13
14
15
16
17static 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
40
41
42
43static 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 spin_lock(&reader->irq_lock);
50 if (!reader->substream)
51 goto irq_spin_unlock;
52
53 snd_pcm_stream_lock(reader->substream);
54 if (reader->state == UNIPERIF_STATE_STOPPED) {
55
56 dev_warn(reader->dev, "unexpected IRQ\n");
57 goto stream_unlock;
58 }
59
60
61 status = GET_UNIPERIF_ITS(reader);
62 SET_UNIPERIF_ITS_BCLR(reader, status);
63
64
65 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(reader))) {
66 dev_err(reader->dev, "FIFO error detected\n");
67
68 snd_pcm_stop_xrun(reader->substream);
69
70 ret = IRQ_HANDLED;
71 }
72
73stream_unlock:
74 snd_pcm_stream_unlock(reader->substream);
75irq_spin_unlock:
76 spin_unlock(&reader->irq_lock);
77
78 return ret;
79}
80
81static int uni_reader_prepare_pcm(struct snd_pcm_runtime *runtime,
82 struct uniperif *reader)
83{
84 int slot_width;
85
86
87 if ((reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK)
88 == SND_SOC_DAIFMT_I2S) {
89 slot_width = 32;
90 } else {
91 switch (runtime->format) {
92 case SNDRV_PCM_FORMAT_S16_LE:
93 slot_width = 16;
94 break;
95 default:
96 slot_width = 32;
97 break;
98 }
99 }
100
101
102 switch (slot_width) {
103 case 32:
104 SET_UNIPERIF_I2S_FMT_NBIT_32(reader);
105 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader);
106 break;
107 case 16:
108 SET_UNIPERIF_I2S_FMT_NBIT_16(reader);
109 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(reader);
110 break;
111 default:
112 dev_err(reader->dev, "subframe format not supported\n");
113 return -EINVAL;
114 }
115
116
117 switch (runtime->format) {
118 case SNDRV_PCM_FORMAT_S16_LE:
119
120 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(reader);
121 break;
122
123 case SNDRV_PCM_FORMAT_S32_LE:
124
125
126
127
128 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader);
129 break;
130
131 default:
132 dev_err(reader->dev, "format not supported\n");
133 return -EINVAL;
134 }
135
136
137 if ((runtime->channels % 2) || (runtime->channels < 2) ||
138 (runtime->channels > 10)) {
139 dev_err(reader->dev, "%s: invalid nb of channels\n", __func__);
140 return -EINVAL;
141 }
142
143 SET_UNIPERIF_I2S_FMT_NUM_CH(reader, runtime->channels / 2);
144 SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader);
145
146 return 0;
147}
148
149static int uni_reader_prepare_tdm(struct snd_pcm_runtime *runtime,
150 struct uniperif *reader)
151{
152 int frame_size;
153
154 unsigned int word_pos[4] = {
155 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
156
157 frame_size = sti_uniperiph_get_user_frame_size(runtime);
158
159
160 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(reader);
161 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(reader);
162
163
164 SET_UNIPERIF_I2S_FMT_NUM_CH(reader, frame_size / 4 / 2);
165
166 SET_UNIPERIF_I2S_FMT_ORDER_MSB(reader);
167 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
168 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(reader);
169
170
171
172
173
174
175
176
177 sti_uniperiph_get_tdm_word_pos(reader, word_pos);
178 SET_UNIPERIF_TDM_WORD_POS(reader, 1_2, word_pos[WORD_1_2]);
179 SET_UNIPERIF_TDM_WORD_POS(reader, 3_4, word_pos[WORD_3_4]);
180 SET_UNIPERIF_TDM_WORD_POS(reader, 5_6, word_pos[WORD_5_6]);
181 SET_UNIPERIF_TDM_WORD_POS(reader, 7_8, word_pos[WORD_7_8]);
182
183 return 0;
184}
185
186static int uni_reader_prepare(struct snd_pcm_substream *substream,
187 struct snd_soc_dai *dai)
188{
189 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
190 struct uniperif *reader = priv->dai_data.uni;
191 struct snd_pcm_runtime *runtime = substream->runtime;
192 int transfer_size, trigger_limit, ret;
193
194
195 if (reader->state != UNIPERIF_STATE_STOPPED) {
196 dev_err(reader->dev, "%s: invalid reader state %d\n", __func__,
197 reader->state);
198 return -EINVAL;
199 }
200
201
202 if (reader->type == SND_ST_UNIPERIF_TYPE_TDM) {
203
204 transfer_size =
205 sti_uniperiph_get_user_frame_size(runtime) / 4;
206 } else {
207 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
208 }
209
210
211 if (reader->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
212 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
213 else
214
215
216
217
218
219 trigger_limit = transfer_size;
220
221
222 if ((!trigger_limit % 2) ||
223 (trigger_limit != 1 && transfer_size % 2) ||
224 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(reader))) {
225 dev_err(reader->dev, "invalid trigger limit %d\n",
226 trigger_limit);
227 return -EINVAL;
228 }
229
230 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(reader, trigger_limit);
231
232 if (UNIPERIF_TYPE_IS_TDM(reader))
233 ret = uni_reader_prepare_tdm(runtime, reader);
234 else
235 ret = uni_reader_prepare_pcm(runtime, reader);
236 if (ret)
237 return ret;
238
239 switch (reader->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
240 case SND_SOC_DAIFMT_I2S:
241 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
242 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(reader);
243 break;
244 case SND_SOC_DAIFMT_LEFT_J:
245 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(reader);
246 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
247 break;
248 case SND_SOC_DAIFMT_RIGHT_J:
249 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(reader);
250 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(reader);
251 break;
252 default:
253 dev_err(reader->dev, "format not supported\n");
254 return -EINVAL;
255 }
256
257
258 switch (reader->daifmt & SND_SOC_DAIFMT_INV_MASK) {
259 case SND_SOC_DAIFMT_NB_NF:
260 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader);
261 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader);
262 break;
263 case SND_SOC_DAIFMT_NB_IF:
264 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader);
265 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(reader);
266 break;
267 case SND_SOC_DAIFMT_IB_NF:
268 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(reader);
269 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader);
270 break;
271 case SND_SOC_DAIFMT_IB_IF:
272 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(reader);
273 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(reader);
274 break;
275 }
276
277
278 SET_UNIPERIF_ITS_BCLR(reader, GET_UNIPERIF_ITS(reader));
279
280 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(reader, 0);
281
282
283 SET_UNIPERIF_ITM_BSET_DMA_ERROR(reader);
284 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
285 SET_UNIPERIF_ITM_BSET_MEM_BLK_READ(reader);
286
287
288 if (reader->underflow_enabled) {
289 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(reader);
290 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(reader);
291 }
292
293
294 return sti_uniperiph_reset(reader);
295}
296
297static int uni_reader_start(struct uniperif *reader)
298{
299
300 if (reader->state != UNIPERIF_STATE_STOPPED) {
301 dev_err(reader->dev, "%s: invalid reader state\n", __func__);
302 return -EINVAL;
303 }
304
305
306 SET_UNIPERIF_ITS_BCLR_FIFO_ERROR(reader);
307 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(reader);
308
309
310 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(reader);
311
312
313 reader->state = UNIPERIF_STATE_STARTED;
314 return 0;
315}
316
317static int uni_reader_stop(struct uniperif *reader)
318{
319
320 if (reader->state == UNIPERIF_STATE_STOPPED) {
321 dev_err(reader->dev, "%s: invalid reader state\n", __func__);
322 return -EINVAL;
323 }
324
325
326 SET_UNIPERIF_CTRL_OPERATION_OFF(reader);
327
328
329 SET_UNIPERIF_ITM_BCLR(reader, GET_UNIPERIF_ITM(reader));
330
331
332 reader->state = UNIPERIF_STATE_STOPPED;
333
334 return 0;
335}
336
337static int uni_reader_trigger(struct snd_pcm_substream *substream,
338 int cmd, struct snd_soc_dai *dai)
339{
340 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
341 struct uniperif *reader = priv->dai_data.uni;
342
343 switch (cmd) {
344 case SNDRV_PCM_TRIGGER_START:
345 return uni_reader_start(reader);
346 case SNDRV_PCM_TRIGGER_STOP:
347 return uni_reader_stop(reader);
348 default:
349 return -EINVAL;
350 }
351}
352
353static int uni_reader_startup(struct snd_pcm_substream *substream,
354 struct snd_soc_dai *dai)
355{
356 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
357 struct uniperif *reader = priv->dai_data.uni;
358 unsigned long flags;
359 int ret;
360
361 spin_lock_irqsave(&reader->irq_lock, flags);
362 reader->substream = substream;
363 spin_unlock_irqrestore(&reader->irq_lock, flags);
364
365 if (!UNIPERIF_TYPE_IS_TDM(reader))
366 return 0;
367
368
369 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
370 SNDRV_PCM_HW_PARAM_CHANNELS,
371 sti_uniperiph_fix_tdm_chan,
372 reader, SNDRV_PCM_HW_PARAM_CHANNELS,
373 -1);
374 if (ret < 0)
375 return ret;
376
377 return snd_pcm_hw_rule_add(substream->runtime, 0,
378 SNDRV_PCM_HW_PARAM_FORMAT,
379 sti_uniperiph_fix_tdm_format,
380 reader, SNDRV_PCM_HW_PARAM_FORMAT,
381 -1);
382}
383
384static void uni_reader_shutdown(struct snd_pcm_substream *substream,
385 struct snd_soc_dai *dai)
386{
387 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
388 struct uniperif *reader = priv->dai_data.uni;
389 unsigned long flags;
390
391 spin_lock_irqsave(&reader->irq_lock, flags);
392 if (reader->state != UNIPERIF_STATE_STOPPED) {
393
394 uni_reader_stop(reader);
395 }
396 reader->substream = NULL;
397 spin_unlock_irqrestore(&reader->irq_lock, flags);
398}
399
400static const struct snd_soc_dai_ops uni_reader_dai_ops = {
401 .startup = uni_reader_startup,
402 .shutdown = uni_reader_shutdown,
403 .prepare = uni_reader_prepare,
404 .trigger = uni_reader_trigger,
405 .hw_params = sti_uniperiph_dai_hw_params,
406 .set_fmt = sti_uniperiph_dai_set_fmt,
407 .set_tdm_slot = sti_uniperiph_set_tdm_slot
408};
409
410int uni_reader_init(struct platform_device *pdev,
411 struct uniperif *reader)
412{
413 int ret = 0;
414
415 reader->dev = &pdev->dev;
416 reader->state = UNIPERIF_STATE_STOPPED;
417 reader->dai_ops = &uni_reader_dai_ops;
418
419 if (UNIPERIF_TYPE_IS_TDM(reader))
420 reader->hw = &uni_tdm_hw;
421 else
422 reader->hw = &uni_reader_pcm_hw;
423
424 ret = devm_request_irq(&pdev->dev, reader->irq,
425 uni_reader_irq_handler, IRQF_SHARED,
426 dev_name(&pdev->dev), reader);
427 if (ret < 0) {
428 dev_err(&pdev->dev, "Failed to request IRQ\n");
429 return -EBUSY;
430 }
431
432 spin_lock_init(&reader->irq_lock);
433
434 return 0;
435}
436EXPORT_SYMBOL_GPL(uni_reader_init);
437