Commit | Line | Data |
---|---|---|
5765e78e | 1 | // SPDX-License-Identifier: GPL-2.0-only |
8cc72361 WYC |
2 | /** |
3 | * Copyright (C) 2008, Creative Technology Ltd. All Rights Reserved. | |
4 | * | |
8cc72361 WYC |
5 | * @File ctpcm.c |
6 | * | |
7 | * @Brief | |
8 | * This file contains the definition of the pcm device functions. | |
9 | * | |
10 | * @Author Liu Chun | |
11 | * @Date Apr 2 2008 | |
8cc72361 WYC |
12 | */ |
13 | ||
14 | #include "ctpcm.h" | |
b7bbf876 | 15 | #include "cttimer.h" |
5a0e3ad6 | 16 | #include <linux/slab.h> |
8cc72361 WYC |
17 | #include <sound/pcm.h> |
18 | ||
19 | /* Hardware descriptions for playback */ | |
4f8f04e8 | 20 | static const struct snd_pcm_hardware ct_pcm_playback_hw = { |
8cc72361 WYC |
21 | .info = (SNDRV_PCM_INFO_MMAP | |
22 | SNDRV_PCM_INFO_INTERLEAVED | | |
23 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
24 | SNDRV_PCM_INFO_MMAP_VALID | | |
25 | SNDRV_PCM_INFO_PAUSE), | |
26 | .formats = (SNDRV_PCM_FMTBIT_U8 | | |
8cc72361 | 27 | SNDRV_PCM_FMTBIT_S16_LE | |
8cc72361 | 28 | SNDRV_PCM_FMTBIT_S24_3LE | |
d2b9b96c TI |
29 | SNDRV_PCM_FMTBIT_S32_LE | |
30 | SNDRV_PCM_FMTBIT_FLOAT_LE), | |
8cc72361 WYC |
31 | .rates = (SNDRV_PCM_RATE_CONTINUOUS | |
32 | SNDRV_PCM_RATE_8000_192000), | |
33 | .rate_min = 8000, | |
34 | .rate_max = 192000, | |
35 | .channels_min = 1, | |
36 | .channels_max = 2, | |
37 | .buffer_bytes_max = (128*1024), | |
38 | .period_bytes_min = (64), | |
39 | .period_bytes_max = (128*1024), | |
775ffa1d | 40 | .periods_min = 2, |
8cc72361 WYC |
41 | .periods_max = 1024, |
42 | .fifo_size = 0, | |
43 | }; | |
44 | ||
4f8f04e8 | 45 | static const struct snd_pcm_hardware ct_spdif_passthru_playback_hw = { |
8cc72361 WYC |
46 | .info = (SNDRV_PCM_INFO_MMAP | |
47 | SNDRV_PCM_INFO_INTERLEAVED | | |
48 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
49 | SNDRV_PCM_INFO_MMAP_VALID | | |
50 | SNDRV_PCM_INFO_PAUSE), | |
d2b9b96c | 51 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
8cc72361 WYC |
52 | .rates = (SNDRV_PCM_RATE_48000 | |
53 | SNDRV_PCM_RATE_44100 | | |
54 | SNDRV_PCM_RATE_32000), | |
55 | .rate_min = 32000, | |
56 | .rate_max = 48000, | |
57 | .channels_min = 2, | |
58 | .channels_max = 2, | |
59 | .buffer_bytes_max = (128*1024), | |
60 | .period_bytes_min = (64), | |
61 | .period_bytes_max = (128*1024), | |
775ffa1d | 62 | .periods_min = 2, |
8cc72361 WYC |
63 | .periods_max = 1024, |
64 | .fifo_size = 0, | |
65 | }; | |
66 | ||
67 | /* Hardware descriptions for capture */ | |
4f8f04e8 | 68 | static const struct snd_pcm_hardware ct_pcm_capture_hw = { |
8cc72361 WYC |
69 | .info = (SNDRV_PCM_INFO_MMAP | |
70 | SNDRV_PCM_INFO_INTERLEAVED | | |
71 | SNDRV_PCM_INFO_BLOCK_TRANSFER | | |
72 | SNDRV_PCM_INFO_PAUSE | | |
73 | SNDRV_PCM_INFO_MMAP_VALID), | |
74 | .formats = (SNDRV_PCM_FMTBIT_U8 | | |
8cc72361 | 75 | SNDRV_PCM_FMTBIT_S16_LE | |
8cc72361 | 76 | SNDRV_PCM_FMTBIT_S24_3LE | |
d2b9b96c TI |
77 | SNDRV_PCM_FMTBIT_S32_LE | |
78 | SNDRV_PCM_FMTBIT_FLOAT_LE), | |
8cc72361 WYC |
79 | .rates = (SNDRV_PCM_RATE_CONTINUOUS | |
80 | SNDRV_PCM_RATE_8000_96000), | |
81 | .rate_min = 8000, | |
82 | .rate_max = 96000, | |
83 | .channels_min = 1, | |
84 | .channels_max = 2, | |
85 | .buffer_bytes_max = (128*1024), | |
86 | .period_bytes_min = (384), | |
87 | .period_bytes_max = (64*1024), | |
88 | .periods_min = 2, | |
89 | .periods_max = 1024, | |
90 | .fifo_size = 0, | |
91 | }; | |
92 | ||
93 | static void ct_atc_pcm_interrupt(struct ct_atc_pcm *atc_pcm) | |
94 | { | |
95 | struct ct_atc_pcm *apcm = atc_pcm; | |
96 | ||
35ebf6e7 | 97 | if (!apcm->substream) |
8cc72361 WYC |
98 | return; |
99 | ||
100 | snd_pcm_period_elapsed(apcm->substream); | |
101 | } | |
102 | ||
103 | static void ct_atc_pcm_free_substream(struct snd_pcm_runtime *runtime) | |
104 | { | |
105 | struct ct_atc_pcm *apcm = runtime->private_data; | |
106 | struct ct_atc *atc = snd_pcm_substream_chip(apcm->substream); | |
107 | ||
108 | atc->pcm_release_resources(atc, apcm); | |
b7bbf876 | 109 | ct_timer_instance_free(apcm->timer); |
8cc72361 WYC |
110 | kfree(apcm); |
111 | runtime->private_data = NULL; | |
112 | } | |
113 | ||
114 | /* pcm playback operations */ | |
115 | static int ct_pcm_playback_open(struct snd_pcm_substream *substream) | |
116 | { | |
117 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
118 | struct snd_pcm_runtime *runtime = substream->runtime; | |
119 | struct ct_atc_pcm *apcm; | |
120 | int err; | |
121 | ||
122 | apcm = kzalloc(sizeof(*apcm), GFP_KERNEL); | |
35ebf6e7 | 123 | if (!apcm) |
8cc72361 WYC |
124 | return -ENOMEM; |
125 | ||
8cc72361 WYC |
126 | apcm->substream = substream; |
127 | apcm->interrupt = ct_atc_pcm_interrupt; | |
8cc72361 WYC |
128 | if (IEC958 == substream->pcm->device) { |
129 | runtime->hw = ct_spdif_passthru_playback_hw; | |
130 | atc->spdif_out_passthru(atc, 1); | |
131 | } else { | |
132 | runtime->hw = ct_pcm_playback_hw; | |
133 | if (FRONT == substream->pcm->device) | |
134 | runtime->hw.channels_max = 8; | |
135 | } | |
136 | ||
137 | err = snd_pcm_hw_constraint_integer(runtime, | |
138 | SNDRV_PCM_HW_PARAM_PERIODS); | |
cbb7eb20 ME |
139 | if (err < 0) |
140 | goto free_pcm; | |
141 | ||
8cc72361 WYC |
142 | err = snd_pcm_hw_constraint_minmax(runtime, |
143 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
144 | 1024, UINT_MAX); | |
cbb7eb20 ME |
145 | if (err < 0) |
146 | goto free_pcm; | |
8cc72361 | 147 | |
b7bbf876 | 148 | apcm->timer = ct_timer_instance_new(atc->timer, apcm); |
fa2b30af | 149 | if (!apcm->timer) { |
cbb7eb20 ME |
150 | err = -ENOMEM; |
151 | goto free_pcm; | |
fa2b30af JL |
152 | } |
153 | runtime->private_data = apcm; | |
154 | runtime->private_free = ct_atc_pcm_free_substream; | |
b7bbf876 | 155 | |
8cc72361 | 156 | return 0; |
cbb7eb20 ME |
157 | |
158 | free_pcm: | |
159 | kfree(apcm); | |
160 | return err; | |
8cc72361 WYC |
161 | } |
162 | ||
163 | static int ct_pcm_playback_close(struct snd_pcm_substream *substream) | |
164 | { | |
165 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
166 | ||
167 | /* TODO: Notify mixer inactive. */ | |
168 | if (IEC958 == substream->pcm->device) | |
169 | atc->spdif_out_passthru(atc, 0); | |
170 | ||
171 | /* The ct_atc_pcm object will be freed by runtime->private_free */ | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | static int ct_pcm_hw_params(struct snd_pcm_substream *substream, | |
177 | struct snd_pcm_hw_params *hw_params) | |
178 | { | |
a5990dc5 TI |
179 | struct ct_atc *atc = snd_pcm_substream_chip(substream); |
180 | struct ct_atc_pcm *apcm = substream->runtime->private_data; | |
a5990dc5 | 181 | |
a5990dc5 TI |
182 | /* clear previous resources */ |
183 | atc->pcm_release_resources(atc, apcm); | |
76178cc7 | 184 | return 0; |
8cc72361 WYC |
185 | } |
186 | ||
187 | static int ct_pcm_hw_free(struct snd_pcm_substream *substream) | |
188 | { | |
a5990dc5 TI |
189 | struct ct_atc *atc = snd_pcm_substream_chip(substream); |
190 | struct ct_atc_pcm *apcm = substream->runtime->private_data; | |
191 | ||
192 | /* clear previous resources */ | |
193 | atc->pcm_release_resources(atc, apcm); | |
76178cc7 | 194 | return 0; |
8cc72361 WYC |
195 | } |
196 | ||
8cc72361 WYC |
197 | |
198 | static int ct_pcm_playback_prepare(struct snd_pcm_substream *substream) | |
199 | { | |
200 | int err; | |
201 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
202 | struct snd_pcm_runtime *runtime = substream->runtime; | |
203 | struct ct_atc_pcm *apcm = runtime->private_data; | |
204 | ||
205 | if (IEC958 == substream->pcm->device) | |
206 | err = atc->spdif_passthru_playback_prepare(atc, apcm); | |
207 | else | |
208 | err = atc->pcm_playback_prepare(atc, apcm); | |
209 | ||
210 | if (err < 0) { | |
0cae90a9 SM |
211 | dev_err(atc->card->dev, |
212 | "Preparing pcm playback failed!!!\n"); | |
8cc72361 WYC |
213 | return err; |
214 | } | |
215 | ||
8cc72361 WYC |
216 | return 0; |
217 | } | |
218 | ||
219 | static int | |
220 | ct_pcm_playback_trigger(struct snd_pcm_substream *substream, int cmd) | |
221 | { | |
222 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
223 | struct snd_pcm_runtime *runtime = substream->runtime; | |
224 | struct ct_atc_pcm *apcm = runtime->private_data; | |
225 | ||
226 | switch (cmd) { | |
227 | case SNDRV_PCM_TRIGGER_START: | |
228 | case SNDRV_PCM_TRIGGER_RESUME: | |
229 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: | |
230 | atc->pcm_playback_start(atc, apcm); | |
8cc72361 WYC |
231 | break; |
232 | case SNDRV_PCM_TRIGGER_STOP: | |
233 | case SNDRV_PCM_TRIGGER_SUSPEND: | |
234 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: | |
8cc72361 WYC |
235 | atc->pcm_playback_stop(atc, apcm); |
236 | break; | |
237 | default: | |
238 | break; | |
239 | } | |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
244 | static snd_pcm_uframes_t | |
245 | ct_pcm_playback_pointer(struct snd_pcm_substream *substream) | |
246 | { | |
247 | unsigned long position; | |
248 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
249 | struct snd_pcm_runtime *runtime = substream->runtime; | |
250 | struct ct_atc_pcm *apcm = runtime->private_data; | |
251 | ||
252 | /* Read out playback position */ | |
253 | position = atc->pcm_playback_position(atc, apcm); | |
254 | position = bytes_to_frames(runtime, position); | |
af8500bb TI |
255 | if (position >= runtime->buffer_size) |
256 | position = 0; | |
8cc72361 WYC |
257 | return position; |
258 | } | |
259 | ||
260 | /* pcm capture operations */ | |
261 | static int ct_pcm_capture_open(struct snd_pcm_substream *substream) | |
262 | { | |
263 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
264 | struct snd_pcm_runtime *runtime = substream->runtime; | |
265 | struct ct_atc_pcm *apcm; | |
266 | int err; | |
267 | ||
268 | apcm = kzalloc(sizeof(*apcm), GFP_KERNEL); | |
35ebf6e7 | 269 | if (!apcm) |
8cc72361 WYC |
270 | return -ENOMEM; |
271 | ||
8cc72361 | 272 | apcm->started = 0; |
8cc72361 WYC |
273 | apcm->substream = substream; |
274 | apcm->interrupt = ct_atc_pcm_interrupt; | |
8cc72361 WYC |
275 | runtime->hw = ct_pcm_capture_hw; |
276 | runtime->hw.rate_max = atc->rsr * atc->msr; | |
277 | ||
278 | err = snd_pcm_hw_constraint_integer(runtime, | |
279 | SNDRV_PCM_HW_PARAM_PERIODS); | |
cbb7eb20 ME |
280 | if (err < 0) |
281 | goto free_pcm; | |
282 | ||
8cc72361 WYC |
283 | err = snd_pcm_hw_constraint_minmax(runtime, |
284 | SNDRV_PCM_HW_PARAM_BUFFER_BYTES, | |
285 | 1024, UINT_MAX); | |
cbb7eb20 ME |
286 | if (err < 0) |
287 | goto free_pcm; | |
8cc72361 | 288 | |
b7bbf876 | 289 | apcm->timer = ct_timer_instance_new(atc->timer, apcm); |
fa2b30af | 290 | if (!apcm->timer) { |
cbb7eb20 ME |
291 | err = -ENOMEM; |
292 | goto free_pcm; | |
fa2b30af JL |
293 | } |
294 | runtime->private_data = apcm; | |
295 | runtime->private_free = ct_atc_pcm_free_substream; | |
b7bbf876 | 296 | |
8cc72361 | 297 | return 0; |
cbb7eb20 ME |
298 | |
299 | free_pcm: | |
300 | kfree(apcm); | |
301 | return err; | |
8cc72361 WYC |
302 | } |
303 | ||
304 | static int ct_pcm_capture_close(struct snd_pcm_substream *substream) | |
305 | { | |
306 | /* The ct_atc_pcm object will be freed by runtime->private_free */ | |
307 | /* TODO: Notify mixer inactive. */ | |
308 | return 0; | |
309 | } | |
310 | ||
311 | static int ct_pcm_capture_prepare(struct snd_pcm_substream *substream) | |
312 | { | |
313 | int err; | |
314 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
315 | struct snd_pcm_runtime *runtime = substream->runtime; | |
316 | struct ct_atc_pcm *apcm = runtime->private_data; | |
317 | ||
318 | err = atc->pcm_capture_prepare(atc, apcm); | |
319 | if (err < 0) { | |
0cae90a9 SM |
320 | dev_err(atc->card->dev, |
321 | "Preparing pcm capture failed!!!\n"); | |
8cc72361 WYC |
322 | return err; |
323 | } | |
324 | ||
8cc72361 WYC |
325 | return 0; |
326 | } | |
327 | ||
328 | static int | |
329 | ct_pcm_capture_trigger(struct snd_pcm_substream *substream, int cmd) | |
330 | { | |
331 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
332 | struct snd_pcm_runtime *runtime = substream->runtime; | |
333 | struct ct_atc_pcm *apcm = runtime->private_data; | |
334 | ||
335 | switch (cmd) { | |
336 | case SNDRV_PCM_TRIGGER_START: | |
337 | atc->pcm_capture_start(atc, apcm); | |
8cc72361 WYC |
338 | break; |
339 | case SNDRV_PCM_TRIGGER_STOP: | |
8cc72361 WYC |
340 | atc->pcm_capture_stop(atc, apcm); |
341 | break; | |
342 | default: | |
8cc72361 WYC |
343 | atc->pcm_capture_stop(atc, apcm); |
344 | break; | |
345 | } | |
346 | ||
347 | return 0; | |
348 | } | |
349 | ||
350 | static snd_pcm_uframes_t | |
351 | ct_pcm_capture_pointer(struct snd_pcm_substream *substream) | |
352 | { | |
353 | unsigned long position; | |
354 | struct ct_atc *atc = snd_pcm_substream_chip(substream); | |
355 | struct snd_pcm_runtime *runtime = substream->runtime; | |
356 | struct ct_atc_pcm *apcm = runtime->private_data; | |
357 | ||
358 | /* Read out playback position */ | |
359 | position = atc->pcm_capture_position(atc, apcm); | |
360 | position = bytes_to_frames(runtime, position); | |
af8500bb TI |
361 | if (position >= runtime->buffer_size) |
362 | position = 0; | |
8cc72361 WYC |
363 | return position; |
364 | } | |
365 | ||
366 | /* PCM operators for playback */ | |
6769e988 | 367 | static const struct snd_pcm_ops ct_pcm_playback_ops = { |
8cc72361 WYC |
368 | .open = ct_pcm_playback_open, |
369 | .close = ct_pcm_playback_close, | |
8cc72361 WYC |
370 | .hw_params = ct_pcm_hw_params, |
371 | .hw_free = ct_pcm_hw_free, | |
372 | .prepare = ct_pcm_playback_prepare, | |
373 | .trigger = ct_pcm_playback_trigger, | |
374 | .pointer = ct_pcm_playback_pointer, | |
375 | }; | |
376 | ||
377 | /* PCM operators for capture */ | |
6769e988 | 378 | static const struct snd_pcm_ops ct_pcm_capture_ops = { |
8cc72361 WYC |
379 | .open = ct_pcm_capture_open, |
380 | .close = ct_pcm_capture_close, | |
8cc72361 WYC |
381 | .hw_params = ct_pcm_hw_params, |
382 | .hw_free = ct_pcm_hw_free, | |
383 | .prepare = ct_pcm_capture_prepare, | |
384 | .trigger = ct_pcm_capture_trigger, | |
385 | .pointer = ct_pcm_capture_pointer, | |
386 | }; | |
387 | ||
8d50cdc1 TI |
388 | static const struct snd_pcm_chmap_elem surround_map[] = { |
389 | { .channels = 1, | |
7b31d009 | 390 | .map = { SNDRV_CHMAP_MONO } }, |
8d50cdc1 TI |
391 | { .channels = 2, |
392 | .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } }, | |
393 | { } | |
394 | }; | |
395 | ||
396 | static const struct snd_pcm_chmap_elem clfe_map[] = { | |
397 | { .channels = 1, | |
7b31d009 | 398 | .map = { SNDRV_CHMAP_MONO } }, |
8d50cdc1 TI |
399 | { .channels = 2, |
400 | .map = { SNDRV_CHMAP_FC, SNDRV_CHMAP_LFE } }, | |
401 | { } | |
402 | }; | |
403 | ||
404 | static const struct snd_pcm_chmap_elem side_map[] = { | |
405 | { .channels = 1, | |
7b31d009 | 406 | .map = { SNDRV_CHMAP_MONO } }, |
8d50cdc1 TI |
407 | { .channels = 2, |
408 | .map = { SNDRV_CHMAP_SL, SNDRV_CHMAP_SR } }, | |
409 | { } | |
410 | }; | |
411 | ||
8cc72361 WYC |
412 | /* Create ALSA pcm device */ |
413 | int ct_alsa_pcm_create(struct ct_atc *atc, | |
414 | enum CTALSADEVS device, | |
415 | const char *device_name) | |
416 | { | |
417 | struct snd_pcm *pcm; | |
8d50cdc1 TI |
418 | const struct snd_pcm_chmap_elem *map; |
419 | int chs; | |
8cc72361 WYC |
420 | int err; |
421 | int playback_count, capture_count; | |
8cc72361 | 422 | |
391e6914 | 423 | playback_count = (IEC958 == device) ? 1 : 256; |
8cc72361 | 424 | capture_count = (FRONT == device) ? 1 : 0; |
8372d498 | 425 | err = snd_pcm_new(atc->card, "ctxfi", device, |
8cc72361 WYC |
426 | playback_count, capture_count, &pcm); |
427 | if (err < 0) { | |
0cae90a9 SM |
428 | dev_err(atc->card->dev, "snd_pcm_new failed!! Err=%d\n", |
429 | err); | |
8cc72361 WYC |
430 | return err; |
431 | } | |
432 | ||
433 | pcm->private_data = atc; | |
434 | pcm->info_flags = 0; | |
435 | pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX; | |
8372d498 | 436 | strlcpy(pcm->name, device_name, sizeof(pcm->name)); |
8cc72361 WYC |
437 | |
438 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &ct_pcm_playback_ops); | |
439 | ||
440 | if (FRONT == device) | |
441 | snd_pcm_set_ops(pcm, | |
442 | SNDRV_PCM_STREAM_CAPTURE, &ct_pcm_capture_ops); | |
443 | ||
76178cc7 TI |
444 | snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV_SG, |
445 | &atc->pci->dev, 128*1024, 128*1024); | |
8cc72361 | 446 | |
8d50cdc1 TI |
447 | chs = 2; |
448 | switch (device) { | |
449 | case FRONT: | |
450 | chs = 8; | |
451 | map = snd_pcm_std_chmaps; | |
452 | break; | |
453 | case SURROUND: | |
454 | map = surround_map; | |
455 | break; | |
456 | case CLFE: | |
457 | map = clfe_map; | |
458 | break; | |
459 | case SIDE: | |
460 | map = side_map; | |
461 | break; | |
462 | default: | |
463 | map = snd_pcm_std_chmaps; | |
464 | break; | |
465 | } | |
466 | err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK, map, chs, | |
467 | 0, NULL); | |
468 | if (err < 0) | |
469 | return err; | |
470 | ||
c7561cd8 | 471 | #ifdef CONFIG_PM_SLEEP |
29959a09 WYC |
472 | atc->pcms[device] = pcm; |
473 | #endif | |
474 | ||
8cc72361 WYC |
475 | return 0; |
476 | } |