blob: 335979a753fefa7555adb0b15b8f3959d2e4fc1c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17/*
18 * Vortex PCM ALSA driver.
19 *
20 * Supports ADB and WT DMA. Unfortunately, WT channels do not run yet.
21 * It remains stuck,and DMA transfers do not happen.
22 */
23#include <sound/asoundef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/time.h>
25#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include "au88x0.h"
29
30#define VORTEX_PCM_TYPE(x) (x->name[40])
31
32/* hardware definition */
Takashi Iwai2fd16872005-11-17 14:55:19 +010033static struct snd_pcm_hardware snd_vortex_playback_hw_adb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .info =
Jaroslav Kysela41e48452005-08-18 13:43:12 +020035 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
37 SNDRV_PCM_INFO_MMAP_VALID),
38 .formats =
39 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
40 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
41 .rates = SNDRV_PCM_RATE_CONTINUOUS,
42 .rate_min = 5000,
43 .rate_max = 48000,
44 .channels_min = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .channels_max = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .buffer_bytes_max = 0x10000,
Raymond Yau54a96da2011-04-25 12:05:45 +080047 .period_bytes_min = 0x20,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .period_bytes_max = 0x1000,
49 .periods_min = 2,
Raymond Yau54a96da2011-04-25 12:05:45 +080050 .periods_max = 1024,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
53#ifndef CHIP_AU8820
Takashi Iwai2fd16872005-11-17 14:55:19 +010054static struct snd_pcm_hardware snd_vortex_playback_hw_a3d = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .info =
Jaroslav Kysela41e48452005-08-18 13:43:12 +020056 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
58 SNDRV_PCM_INFO_MMAP_VALID),
59 .formats =
60 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
61 SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW,
62 .rates = SNDRV_PCM_RATE_CONTINUOUS,
63 .rate_min = 5000,
64 .rate_max = 48000,
65 .channels_min = 1,
66 .channels_max = 1,
67 .buffer_bytes_max = 0x10000,
68 .period_bytes_min = 0x100,
69 .period_bytes_max = 0x1000,
70 .periods_min = 2,
71 .periods_max = 64,
72};
73#endif
Takashi Iwai2fd16872005-11-17 14:55:19 +010074static struct snd_pcm_hardware snd_vortex_playback_hw_spdif = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 .info =
Jaroslav Kysela41e48452005-08-18 13:43:12 +020076 (SNDRV_PCM_INFO_MMAP | /* SNDRV_PCM_INFO_RESUME | */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_INTERLEAVED |
78 SNDRV_PCM_INFO_MMAP_VALID),
79 .formats =
80 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U8 |
81 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE | SNDRV_PCM_FMTBIT_MU_LAW |
82 SNDRV_PCM_FMTBIT_A_LAW,
83 .rates =
84 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
85 .rate_min = 32000,
86 .rate_max = 48000,
87 .channels_min = 1,
88 .channels_max = 2,
89 .buffer_bytes_max = 0x10000,
90 .period_bytes_min = 0x100,
91 .period_bytes_max = 0x1000,
92 .periods_min = 2,
93 .periods_max = 64,
94};
95
96#ifndef CHIP_AU8810
Takashi Iwai2fd16872005-11-17 14:55:19 +010097static struct snd_pcm_hardware snd_vortex_playback_hw_wt = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .info = (SNDRV_PCM_INFO_MMAP |
99 SNDRV_PCM_INFO_INTERLEAVED |
100 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID),
101 .formats = SNDRV_PCM_FMTBIT_S16_LE,
102 .rates = SNDRV_PCM_RATE_8000_48000 | SNDRV_PCM_RATE_CONTINUOUS, // SNDRV_PCM_RATE_48000,
103 .rate_min = 8000,
104 .rate_max = 48000,
105 .channels_min = 1,
106 .channels_max = 2,
107 .buffer_bytes_max = 0x10000,
108 .period_bytes_min = 0x0400,
109 .period_bytes_max = 0x1000,
110 .periods_min = 2,
111 .periods_max = 64,
112};
113#endif
Raymond Yaud9ab3442011-01-16 10:55:54 +0800114#ifdef CHIP_AU8830
115static unsigned int au8830_channels[3] = {
116 1, 2, 4,
117};
118
119static struct snd_pcm_hw_constraint_list hw_constraints_au8830_channels = {
120 .count = ARRAY_SIZE(au8830_channels),
121 .list = au8830_channels,
122 .mask = 0,
123};
124#endif
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800125
126static void vortex_notify_pcm_vol_change(struct snd_card *card,
127 struct snd_kcontrol *kctl, int activate)
128{
129 if (activate)
130 kctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
131 else
132 kctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
133 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
134 SNDRV_CTL_EVENT_MASK_INFO, &(kctl->id));
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* open callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100138static int snd_vortex_pcm_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 vortex_t *vortex = snd_pcm_substream_chip(substream);
Takashi Iwai2fd16872005-11-17 14:55:19 +0100141 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int err;
143
144 /* Force equal size periods */
145 if ((err =
146 snd_pcm_hw_constraint_integer(runtime,
147 SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
148 return err;
149 /* Avoid PAGE_SIZE boundary to fall inside of a period. */
150 if ((err =
151 snd_pcm_hw_constraint_pow2(runtime, 0,
152 SNDRV_PCM_HW_PARAM_PERIOD_BYTES)) < 0)
153 return err;
154
Raymond Yau54a96da2011-04-25 12:05:45 +0800155 snd_pcm_hw_constraint_step(runtime, 0,
156 SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 64);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
159#ifndef CHIP_AU8820
160 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_A3D) {
161 runtime->hw = snd_vortex_playback_hw_a3d;
162 }
163#endif
164 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_SPDIF) {
165 runtime->hw = snd_vortex_playback_hw_spdif;
166 switch (vortex->spdif_sr) {
167 case 32000:
168 runtime->hw.rates = SNDRV_PCM_RATE_32000;
169 break;
170 case 44100:
171 runtime->hw.rates = SNDRV_PCM_RATE_44100;
172 break;
173 case 48000:
174 runtime->hw.rates = SNDRV_PCM_RATE_48000;
175 break;
176 }
177 }
178 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB
179 || VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_I2S)
180 runtime->hw = snd_vortex_playback_hw_adb;
Raymond Yaud9ab3442011-01-16 10:55:54 +0800181#ifdef CHIP_AU8830
182 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
Raymond Yau7b324862012-01-13 10:30:08 +0800183 VORTEX_IS_QUAD(vortex) &&
Raymond Yaud9ab3442011-01-16 10:55:54 +0800184 VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
185 runtime->hw.channels_max = 4;
186 snd_pcm_hw_constraint_list(runtime, 0,
187 SNDRV_PCM_HW_PARAM_CHANNELS,
188 &hw_constraints_au8830_channels);
189 }
190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 substream->runtime->private_data = NULL;
192 }
193#ifndef CHIP_AU8810
194 else {
195 runtime->hw = snd_vortex_playback_hw_wt;
196 substream->runtime->private_data = NULL;
197 }
198#endif
199 return 0;
200}
201
202/* close callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100203static int snd_vortex_pcm_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 //vortex_t *chip = snd_pcm_substream_chip(substream);
206 stream_t *stream = (stream_t *) substream->runtime->private_data;
207
208 // the hardware-specific codes will be here
209 if (stream != NULL) {
210 stream->substream = NULL;
211 stream->nr_ch = 0;
212 }
213 substream->runtime->private_data = NULL;
214 return 0;
215}
216
217/* hw_params callback */
218static int
Takashi Iwai2fd16872005-11-17 14:55:19 +0100219snd_vortex_pcm_hw_params(struct snd_pcm_substream *substream,
220 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 vortex_t *chip = snd_pcm_substream_chip(substream);
223 stream_t *stream = (stream_t *) (substream->runtime->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int err;
225
226 // Alloc buffer memory.
227 err =
228 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
229 if (err < 0) {
Sudip Mukherjee70c84412014-10-13 11:37:19 +0530230 dev_err(chip->card->dev, "Vortex: pcm page alloc failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return err;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /*
Sudip Mukherjeee7e692652014-09-08 22:48:03 +0530234 pr_info( "Vortex: periods %d, period_bytes %d, channels = %d\n", params_periods(hw_params),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 params_period_bytes(hw_params), params_channels(hw_params));
236 */
237 spin_lock_irq(&chip->lock);
238 // Make audio routes and config buffer DMA.
239 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
240 int dma, type = VORTEX_PCM_TYPE(substream->pcm);
241 /* Dealloc any routes. */
242 if (stream != NULL)
243 vortex_adb_allocroute(chip, stream->dma,
244 stream->nr_ch, stream->dir,
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800245 stream->type,
246 substream->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* Alloc routes. */
248 dma =
249 vortex_adb_allocroute(chip, -1,
250 params_channels(hw_params),
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800251 substream->stream, type,
252 substream->number);
Takashi Iwaia2786552005-08-24 17:57:25 +0200253 if (dma < 0) {
254 spin_unlock_irq(&chip->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return dma;
Takashi Iwaia2786552005-08-24 17:57:25 +0200256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 stream = substream->runtime->private_data = &chip->dma_adb[dma];
258 stream->substream = substream;
259 /* Setup Buffers. */
Takashi Iwai77a23f22008-08-21 13:00:13 +0200260 vortex_adbdma_setbuffers(chip, dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 params_period_bytes(hw_params),
262 params_periods(hw_params));
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800263 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
264 chip->pcm_vol[substream->number].active = 1;
265 vortex_notify_pcm_vol_change(chip->card,
266 chip->pcm_vol[substream->number].kctl, 1);
267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269#ifndef CHIP_AU8810
270 else {
271 /* if (stream != NULL)
272 vortex_wt_allocroute(chip, substream->number, 0); */
273 vortex_wt_allocroute(chip, substream->number,
274 params_channels(hw_params));
275 stream = substream->runtime->private_data =
276 &chip->dma_wt[substream->number];
277 stream->dma = substream->number;
278 stream->substream = substream;
Takashi Iwai77a23f22008-08-21 13:00:13 +0200279 vortex_wtdma_setbuffers(chip, substream->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 params_period_bytes(hw_params),
281 params_periods(hw_params));
282 }
283#endif
284 spin_unlock_irq(&chip->lock);
285 return 0;
286}
287
288/* hw_free callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100289static int snd_vortex_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 vortex_t *chip = snd_pcm_substream_chip(substream);
292 stream_t *stream = (stream_t *) (substream->runtime->private_data);
293
294 spin_lock_irq(&chip->lock);
295 // Delete audio routes.
296 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800297 if (stream != NULL) {
298 if (VORTEX_PCM_TYPE(substream->pcm) == VORTEX_PCM_ADB) {
299 chip->pcm_vol[substream->number].active = 0;
300 vortex_notify_pcm_vol_change(chip->card,
301 chip->pcm_vol[substream->number].kctl,
302 0);
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 vortex_adb_allocroute(chip, stream->dma,
305 stream->nr_ch, stream->dir,
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800306 stream->type,
307 substream->number);
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310#ifndef CHIP_AU8810
311 else {
312 if (stream != NULL)
313 vortex_wt_allocroute(chip, stream->dma, 0);
314 }
315#endif
316 substream->runtime->private_data = NULL;
317 spin_unlock_irq(&chip->lock);
318
319 return snd_pcm_lib_free_pages(substream);
320}
321
322/* prepare callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100323static int snd_vortex_pcm_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 vortex_t *chip = snd_pcm_substream_chip(substream);
Takashi Iwai2fd16872005-11-17 14:55:19 +0100326 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 stream_t *stream = (stream_t *) substream->runtime->private_data;
328 int dma = stream->dma, fmt, dir;
329
330 // set up the hardware with the current configuration.
331 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
332 dir = 1;
333 else
334 dir = 0;
Sudip Mukherjee6a40dc52014-10-13 11:37:18 +0530335 fmt = vortex_alsafmt_aspfmt(runtime->format, chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock_irq(&chip->lock);
337 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
Raymond Yau3ae4e1f2012-01-06 09:19:29 +0800338 vortex_adbdma_setmode(chip, dma, 1, dir, fmt,
339 runtime->channels == 1 ? 0 : 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 vortex_adbdma_setstartbuffer(chip, dma, 0);
341 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_SPDIF)
342 vortex_adb_setsrc(chip, dma, runtime->rate, dir);
343 }
344#ifndef CHIP_AU8810
345 else {
346 vortex_wtdma_setmode(chip, dma, 1, fmt, 0, 0);
347 // FIXME: Set rate (i guess using vortex_wt_writereg() somehow).
348 vortex_wtdma_setstartbuffer(chip, dma, 0);
349 }
350#endif
351 spin_unlock_irq(&chip->lock);
352 return 0;
353}
354
355/* trigger callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100356static int snd_vortex_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 vortex_t *chip = snd_pcm_substream_chip(substream);
359 stream_t *stream = (stream_t *) substream->runtime->private_data;
360 int dma = stream->dma;
361
362 spin_lock(&chip->lock);
363 switch (cmd) {
364 case SNDRV_PCM_TRIGGER_START:
365 // do something to start the PCM engine
366 //printk(KERN_INFO "vortex: start %d\n", dma);
367 stream->fifo_enabled = 1;
368 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT) {
369 vortex_adbdma_resetup(chip, dma);
370 vortex_adbdma_startfifo(chip, dma);
371 }
372#ifndef CHIP_AU8810
373 else {
Sudip Mukherjee70c84412014-10-13 11:37:19 +0530374 dev_info(chip->card->dev, "wt start %d\n", dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 vortex_wtdma_startfifo(chip, dma);
376 }
377#endif
378 break;
379 case SNDRV_PCM_TRIGGER_STOP:
380 // do something to stop the PCM engine
381 //printk(KERN_INFO "vortex: stop %d\n", dma);
382 stream->fifo_enabled = 0;
383 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
Raymond Yaufb65c2d2012-01-07 14:35:17 +0800384 vortex_adbdma_stopfifo(chip, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifndef CHIP_AU8810
386 else {
Sudip Mukherjee70c84412014-10-13 11:37:19 +0530387 dev_info(chip->card->dev, "wt stop %d\n", dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 vortex_wtdma_stopfifo(chip, dma);
389 }
390#endif
391 break;
392 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
393 //printk(KERN_INFO "vortex: pause %d\n", dma);
394 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
395 vortex_adbdma_pausefifo(chip, dma);
396#ifndef CHIP_AU8810
397 else
398 vortex_wtdma_pausefifo(chip, dma);
399#endif
400 break;
401 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
402 //printk(KERN_INFO "vortex: resume %d\n", dma);
403 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
404 vortex_adbdma_resumefifo(chip, dma);
405#ifndef CHIP_AU8810
406 else
407 vortex_wtdma_resumefifo(chip, dma);
408#endif
409 break;
410 default:
411 spin_unlock(&chip->lock);
412 return -EINVAL;
413 }
414 spin_unlock(&chip->lock);
415 return 0;
416}
417
418/* pointer callback */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100419static snd_pcm_uframes_t snd_vortex_pcm_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 vortex_t *chip = snd_pcm_substream_chip(substream);
422 stream_t *stream = (stream_t *) substream->runtime->private_data;
423 int dma = stream->dma;
424 snd_pcm_uframes_t current_ptr = 0;
425
426 spin_lock(&chip->lock);
427 if (VORTEX_PCM_TYPE(substream->pcm) != VORTEX_PCM_WT)
428 current_ptr = vortex_adbdma_getlinearpos(chip, dma);
429#ifndef CHIP_AU8810
430 else
431 current_ptr = vortex_wtdma_getlinearpos(chip, dma);
432#endif
433 //printk(KERN_INFO "vortex: pointer = 0x%x\n", current_ptr);
434 spin_unlock(&chip->lock);
Takashi Iwai58a87382016-03-10 21:03:09 +0100435 current_ptr = bytes_to_frames(substream->runtime, current_ptr);
436 if (current_ptr >= substream->runtime->buffer_size)
437 current_ptr = 0;
438 return current_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/* operators */
Takashi Iwai2fd16872005-11-17 14:55:19 +0100442static struct snd_pcm_ops snd_vortex_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .open = snd_vortex_pcm_open,
444 .close = snd_vortex_pcm_close,
445 .ioctl = snd_pcm_lib_ioctl,
446 .hw_params = snd_vortex_pcm_hw_params,
447 .hw_free = snd_vortex_pcm_hw_free,
448 .prepare = snd_vortex_pcm_prepare,
449 .trigger = snd_vortex_pcm_trigger,
450 .pointer = snd_vortex_pcm_pointer,
451 .page = snd_pcm_sgbuf_ops_page,
452};
453
454/*
455* definitions of capture are omitted here...
456*/
457
458static char *vortex_pcm_prettyname[VORTEX_PCM_LAST] = {
Raymond Yau13eb4ab2011-04-26 12:15:23 +0800459 CARD_NAME " ADB",
460 CARD_NAME " SPDIF",
461 CARD_NAME " A3D",
462 CARD_NAME " WT",
463 CARD_NAME " I2S",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465static char *vortex_pcm_name[VORTEX_PCM_LAST] = {
466 "adb",
467 "spdif",
468 "a3d",
469 "wt",
470 "i2s",
471};
472
473/* SPDIF kcontrol */
474
Takashi Iwai2fd16872005-11-17 14:55:19 +0100475static int snd_vortex_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
478 uinfo->count = 1;
479 return 0;
480}
481
Takashi Iwai2fd16872005-11-17 14:55:19 +0100482static int snd_vortex_spdif_mask_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 ucontrol->value.iec958.status[0] = 0xff;
485 ucontrol->value.iec958.status[1] = 0xff;
486 ucontrol->value.iec958.status[2] = 0xff;
487 ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS;
488 return 0;
489}
490
Takashi Iwai2fd16872005-11-17 14:55:19 +0100491static int snd_vortex_spdif_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
494 ucontrol->value.iec958.status[0] = 0x00;
495 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_ORIGINAL|IEC958_AES1_CON_DIGDIGCONV_ID;
496 ucontrol->value.iec958.status[2] = 0x00;
497 switch (vortex->spdif_sr) {
498 case 32000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_32000; break;
499 case 44100: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_44100; break;
500 case 48000: ucontrol->value.iec958.status[3] = IEC958_AES3_CON_FS_48000; break;
501 }
502 return 0;
503}
504
Takashi Iwai2fd16872005-11-17 14:55:19 +0100505static int snd_vortex_spdif_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
508 int spdif_sr = 48000;
509 switch (ucontrol->value.iec958.status[3] & IEC958_AES3_CON_FS) {
510 case IEC958_AES3_CON_FS_32000: spdif_sr = 32000; break;
511 case IEC958_AES3_CON_FS_44100: spdif_sr = 44100; break;
512 case IEC958_AES3_CON_FS_48000: spdif_sr = 48000; break;
513 }
514 if (spdif_sr == vortex->spdif_sr)
515 return 0;
516 vortex->spdif_sr = spdif_sr;
517 vortex_spdif_init(vortex, vortex->spdif_sr, 1);
518 return 1;
519}
520
521/* spdif controls */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500522static struct snd_kcontrol_new snd_vortex_mixer_spdif[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 {
524 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
525 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
526 .info = snd_vortex_spdif_info,
527 .get = snd_vortex_spdif_get,
528 .put = snd_vortex_spdif_put,
529 },
530 {
531 .access = SNDRV_CTL_ELEM_ACCESS_READ,
532 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
533 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
534 .info = snd_vortex_spdif_info,
535 .get = snd_vortex_spdif_mask_get
536 },
537};
538
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800539/* subdevice PCM Volume control */
540
541static int snd_vortex_pcm_vol_info(struct snd_kcontrol *kcontrol,
542 struct snd_ctl_elem_info *uinfo)
543{
544 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
546 uinfo->count = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
547 uinfo->value.integer.min = -128;
548 uinfo->value.integer.max = 32;
549 return 0;
550}
551
552static int snd_vortex_pcm_vol_get(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_value *ucontrol)
554{
555 int i;
556 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
557 int subdev = kcontrol->id.subdevice;
558 struct pcm_vol *p = &vortex->pcm_vol[subdev];
559 int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
560 for (i = 0; i < max_chn; i++)
561 ucontrol->value.integer.value[i] = p->vol[i];
562 return 0;
563}
564
565static int snd_vortex_pcm_vol_put(struct snd_kcontrol *kcontrol,
566 struct snd_ctl_elem_value *ucontrol)
567{
568 int i;
569 int changed = 0;
570 int mixin;
571 unsigned char vol;
572 vortex_t *vortex = snd_kcontrol_chip(kcontrol);
573 int subdev = kcontrol->id.subdevice;
574 struct pcm_vol *p = &vortex->pcm_vol[subdev];
575 int max_chn = (VORTEX_IS_QUAD(vortex) ? 4 : 2);
576 for (i = 0; i < max_chn; i++) {
577 if (p->vol[i] != ucontrol->value.integer.value[i]) {
578 p->vol[i] = ucontrol->value.integer.value[i];
579 if (p->active) {
580 switch (vortex->dma_adb[p->dma].nr_ch) {
581 case 1:
582 mixin = p->mixin[0];
583 break;
584 case 2:
585 default:
586 mixin = p->mixin[(i < 2) ? i : (i - 2)];
587 break;
588 case 4:
589 mixin = p->mixin[i];
590 break;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +0200591 }
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800592 vol = p->vol[i];
593 vortex_mix_setinputvolumebyte(vortex,
594 vortex->mixplayb[i], mixin, vol);
595 }
596 changed = 1;
597 }
598 }
599 return changed;
600}
601
602static const DECLARE_TLV_DB_MINMAX(vortex_pcm_vol_db_scale, -9600, 2400);
603
Bhumika Goyalf3b827e2017-02-20 00:18:09 +0530604static const struct snd_kcontrol_new snd_vortex_pcm_vol = {
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800605 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
606 .name = "PCM Playback Volume",
607 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
608 SNDRV_CTL_ELEM_ACCESS_TLV_READ |
609 SNDRV_CTL_ELEM_ACCESS_INACTIVE,
610 .info = snd_vortex_pcm_vol_info,
611 .get = snd_vortex_pcm_vol_get,
612 .put = snd_vortex_pcm_vol_put,
613 .tlv = { .p = vortex_pcm_vol_db_scale },
614};
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/* create a pcm device */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500617static int snd_vortex_new_pcm(vortex_t *chip, int idx, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Takashi Iwai2fd16872005-11-17 14:55:19 +0100619 struct snd_pcm *pcm;
620 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 int i;
622 int err, nr_capt;
623
Harvey Harrison3fa4a902008-02-29 11:41:56 +0100624 if (!chip || idx < 0 || idx >= VORTEX_PCM_LAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -ENODEV;
626
627 /* idx indicates which kind of PCM device. ADB, SPDIF, I2S and A3D share the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300628 * same dma engine. WT uses it own separate dma engine which can't capture. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (idx == VORTEX_PCM_ADB)
630 nr_capt = nr;
631 else
632 nr_capt = 0;
Harvey Harrison3fa4a902008-02-29 11:41:56 +0100633 err = snd_pcm_new(chip->card, vortex_pcm_prettyname[idx], idx, nr,
634 nr_capt, &pcm);
635 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return err;
Raymond Yau13eb4ab2011-04-26 12:15:23 +0800637 snprintf(pcm->name, sizeof(pcm->name),
638 "%s %s", CARD_NAME_SHORT, vortex_pcm_name[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 chip->pcm[idx] = pcm;
640 // This is an evil hack, but it saves a lot of duplicated code.
641 VORTEX_PCM_TYPE(pcm) = idx;
642 pcm->private_data = chip;
643 /* set operators */
644 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
645 &snd_vortex_playback_ops);
646 if (idx == VORTEX_PCM_ADB)
647 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
648 &snd_vortex_playback_ops);
649
650 /* pre-allocation of Scatter-Gather buffers */
651
652 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV_SG,
653 snd_dma_pci_data(chip->pci_dev),
654 0x10000, 0x10000);
655
Raymond Yauf3c90242013-02-14 20:37:22 +0800656 switch (VORTEX_PCM_TYPE(pcm)) {
657 case VORTEX_PCM_ADB:
658 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
659 snd_pcm_std_chmaps,
660 VORTEX_IS_QUAD(chip) ? 4 : 2,
661 0, NULL);
662 if (err < 0)
663 return err;
664 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
665 snd_pcm_std_chmaps, 2, 0, NULL);
666 if (err < 0)
667 return err;
668 break;
669#ifdef CHIP_AU8830
670 case VORTEX_PCM_A3D:
671 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
672 snd_pcm_std_chmaps, 1, 0, NULL);
673 if (err < 0)
674 return err;
675 break;
676#endif
Joe Perches1d198f22013-10-08 15:55:45 -0700677 }
Raymond Yauf3c90242013-02-14 20:37:22 +0800678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_SPDIF) {
680 for (i = 0; i < ARRAY_SIZE(snd_vortex_mixer_spdif); i++) {
681 kctl = snd_ctl_new1(&snd_vortex_mixer_spdif[i], chip);
682 if (!kctl)
683 return -ENOMEM;
684 if ((err = snd_ctl_add(chip->card, kctl)) < 0)
685 return err;
686 }
687 }
Raymond Yaubb92b7c2012-01-17 11:32:17 +0800688 if (VORTEX_PCM_TYPE(pcm) == VORTEX_PCM_ADB) {
689 for (i = 0; i < NR_PCM; i++) {
690 chip->pcm_vol[i].active = 0;
691 chip->pcm_vol[i].dma = -1;
692 kctl = snd_ctl_new1(&snd_vortex_pcm_vol, chip);
693 if (!kctl)
694 return -ENOMEM;
695 chip->pcm_vol[i].kctl = kctl;
696 kctl->id.device = 0;
697 kctl->id.subdevice = i;
698 err = snd_ctl_add(chip->card, kctl);
699 if (err < 0)
700 return err;
701 }
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return 0;
704}