3 * Copyright (c) by AudioScience Inc <alsa@audioscience.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation;
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * The following is not a condition of use, merely a request:
20 * If you modify this program, particularly if you fix errors, AudioScience Inc
21 * would appreciate it if you grant us the right to use those modifications
22 * for any purpose including commercial applications.
25 #include "hpi_internal.h"
26 #include "hpimsginit.h"
29 #include <linux/pci.h>
30 #include <linux/init.h>
31 #include <linux/jiffies.h>
32 #include <linux/slab.h>
33 #include <linux/time.h>
34 #include <linux/wait.h>
35 #include <linux/module.h>
36 #include <sound/core.h>
37 #include <sound/control.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/info.h>
41 #include <sound/initval.h>
42 #include <sound/tlv.h>
43 #include <sound/hwdep.h>
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
49 #if defined CONFIG_SND_DEBUG_VERBOSE
51 * snd_printddd - very verbose debug printk
52 * @format: format string
54 * Works like snd_printk() for debugging purposes.
55 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
56 * Must set snd module debug parameter to 3 to enable at runtime.
58 #define snd_printddd(format, args...) \
59 __snd_printk(3, __FILE__, __LINE__, format, ##args)
61 #define snd_printddd(format, args...) do { } while (0)
64 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
65 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
66 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
67 static int enable_hpi_hwdep = 1;
69 module_param_array(index, int, NULL, S_IRUGO);
70 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
72 module_param_array(id, charp, NULL, S_IRUGO);
73 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
75 module_param_array(enable, bool, NULL, S_IRUGO);
76 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
78 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
79 MODULE_PARM_DESC(enable_hpi_hwdep,
80 "ALSA enable HPI hwdep for AudioScience soundcard ");
83 #ifdef KERNEL_ALSA_BUILD
84 static char *build_info = "Built using headers from kernel source";
85 module_param(build_info, charp, S_IRUGO);
86 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
88 static char *build_info = "Built within ALSA source";
89 module_param(build_info, charp, S_IRUGO);
90 MODULE_PARM_DESC(build_info, "built within ALSA source");
93 /* set to 1 to dump every control from adapter to log */
94 static const int mixer_dump;
96 #define DEFAULT_SAMPLERATE 44100
97 static int adapter_fs = DEFAULT_SAMPLERATE;
100 #define PERIODS_MIN 2
101 #define PERIOD_BYTES_MIN 2048
102 #define BUFFER_BYTES_MAX (512 * 1024)
104 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
115 struct clk_source s[MAX_CLOCKSOURCES];
119 struct snd_card_asihpi {
120 struct snd_card *card;
133 u16 support_grouping;
135 u16 update_interval_frames;
140 /* Per stream data */
141 struct snd_card_asihpi_pcm {
142 struct timer_list timer;
143 unsigned int respawn_timer;
144 unsigned int hpi_buffer_attached;
145 unsigned int buffer_bytes;
146 unsigned int period_bytes;
147 unsigned int bytes_per_sec;
148 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
149 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
150 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
151 unsigned int drained_count;
152 struct snd_pcm_substream *substream;
154 struct hpi_format format;
157 /* universal stream verbs work with out or in stream handles */
159 /* Functions to allow driver to give a buffer to HPI for busmastering */
161 static u16 hpi_stream_host_buffer_attach(
162 u32 h_stream, /* handle to outstream. */
163 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
167 struct hpi_message hm;
168 struct hpi_response hr;
169 unsigned int obj = hpi_handle_object(h_stream);
172 return HPI_ERROR_INVALID_OBJ;
173 hpi_init_message_response(&hm, &hr, obj,
174 obj == HPI_OBJ_OSTREAM ?
175 HPI_OSTREAM_HOSTBUFFER_ALLOC :
176 HPI_ISTREAM_HOSTBUFFER_ALLOC);
178 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
181 hm.u.d.u.buffer.buffer_size = size_in_bytes;
182 hm.u.d.u.buffer.pci_address = pci_address;
183 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
184 hpi_send_recv(&hm, &hr);
188 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
190 struct hpi_message hm;
191 struct hpi_response hr;
192 unsigned int obj = hpi_handle_object(h_stream);
195 return HPI_ERROR_INVALID_OBJ;
197 hpi_init_message_response(&hm, &hr, obj,
198 obj == HPI_OBJ_OSTREAM ?
199 HPI_OSTREAM_HOSTBUFFER_FREE :
200 HPI_ISTREAM_HOSTBUFFER_FREE);
202 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
204 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
205 hpi_send_recv(&hm, &hr);
209 static inline u16 hpi_stream_start(u32 h_stream)
211 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
212 return hpi_outstream_start(h_stream);
214 return hpi_instream_start(h_stream);
217 static inline u16 hpi_stream_stop(u32 h_stream)
219 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
220 return hpi_outstream_stop(h_stream);
222 return hpi_instream_stop(h_stream);
225 static inline u16 hpi_stream_get_info_ex(
229 u32 *pdata_in_buffer,
235 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
236 e = hpi_outstream_get_info_ex(h_stream, pw_state,
237 pbuffer_size, pdata_in_buffer,
238 psample_count, pauxiliary_data);
240 e = hpi_instream_get_info_ex(h_stream, pw_state,
241 pbuffer_size, pdata_in_buffer,
242 psample_count, pauxiliary_data);
246 static inline u16 hpi_stream_group_add(
250 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
251 return hpi_outstream_group_add(h_master, h_stream);
253 return hpi_instream_group_add(h_master, h_stream);
256 static inline u16 hpi_stream_group_reset(u32 h_stream)
258 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
259 return hpi_outstream_group_reset(h_stream);
261 return hpi_instream_group_reset(h_stream);
264 static inline u16 hpi_stream_group_get_map(
265 u32 h_stream, u32 *mo, u32 *mi)
267 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
268 return hpi_outstream_group_get_map(h_stream, mo, mi);
270 return hpi_instream_group_get_map(h_stream, mo, mi);
273 static u16 handle_error(u16 err, int line, char *filename)
277 "in file %s, line %d: HPI error %d\n",
278 filename, line, err);
282 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
284 /***************************** GENERAL PCM ****************/
286 static void print_hwparams(struct snd_pcm_substream *substream,
287 struct snd_pcm_hw_params *p)
290 snd_pcm_debug_name(substream, name, sizeof(name));
291 snd_printd("%s HWPARAMS\n", name);
292 snd_printd(" samplerate %d Hz\n", params_rate(p));
293 snd_printd(" channels %d\n", params_channels(p));
294 snd_printd(" format %d\n", params_format(p));
295 snd_printd(" subformat %d\n", params_subformat(p));
296 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
297 snd_printd(" period %d B\n", params_period_bytes(p));
298 snd_printd(" access %d\n", params_access(p));
299 snd_printd(" period_size %d\n", params_period_size(p));
300 snd_printd(" periods %d\n", params_periods(p));
301 snd_printd(" buffer_size %d\n", params_buffer_size(p));
302 snd_printd(" %d B/s\n", params_rate(p) *
304 snd_pcm_format_width(params_format(p)) / 8);
308 static snd_pcm_format_t hpi_to_alsa_formats[] = {
310 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
311 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
312 -1, /* HPI_FORMAT_MPEG_L1 3 */
313 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
314 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
315 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
316 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
317 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
318 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
319 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
320 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
321 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
322 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
323 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
325 /* ALSA can't handle 3 byte sample size together with power-of-2
326 * constraint on buffer_bytes, so disable this format
330 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
335 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
340 for (format = HPI_FORMAT_PCM8_UNSIGNED;
341 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
342 if (hpi_to_alsa_formats[format] == alsa_format) {
343 *hpi_format = format;
348 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
354 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
355 struct snd_pcm_hardware *pcmhw)
361 unsigned int rate_min = 200000;
362 unsigned int rate_max = 0;
363 unsigned int rates = 0;
365 if (asihpi->support_mrx) {
366 rates |= SNDRV_PCM_RATE_CONTINUOUS;
367 rates |= SNDRV_PCM_RATE_8000_96000;
371 /* on cards without SRC,
372 valid rates are determined by sampleclock */
373 err = hpi_mixer_get_control(asihpi->h_mixer,
374 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
375 HPI_CONTROL_SAMPLECLOCK, &h_control);
378 "No local sampleclock, err %d\n", err);
381 for (idx = -1; idx < 100; idx++) {
383 if (hpi_sample_clock_get_sample_rate(h_control,
386 } else if (hpi_sample_clock_query_local_rate(h_control,
387 idx, &sample_rate)) {
391 rate_min = min(rate_min, sample_rate);
392 rate_max = max(rate_max, sample_rate);
394 switch (sample_rate) {
396 rates |= SNDRV_PCM_RATE_5512;
399 rates |= SNDRV_PCM_RATE_8000;
402 rates |= SNDRV_PCM_RATE_11025;
405 rates |= SNDRV_PCM_RATE_16000;
408 rates |= SNDRV_PCM_RATE_22050;
411 rates |= SNDRV_PCM_RATE_32000;
414 rates |= SNDRV_PCM_RATE_44100;
417 rates |= SNDRV_PCM_RATE_48000;
420 rates |= SNDRV_PCM_RATE_64000;
423 rates |= SNDRV_PCM_RATE_88200;
426 rates |= SNDRV_PCM_RATE_96000;
429 rates |= SNDRV_PCM_RATE_176400;
432 rates |= SNDRV_PCM_RATE_192000;
434 default: /* some other rate */
435 rates |= SNDRV_PCM_RATE_KNOT;
440 pcmhw->rates = rates;
441 pcmhw->rate_min = rate_min;
442 pcmhw->rate_max = rate_max;
445 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
446 struct snd_pcm_hw_params *params)
448 struct snd_pcm_runtime *runtime = substream->runtime;
449 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
450 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
454 unsigned int bytes_per_sec;
456 print_hwparams(substream, params);
457 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
460 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
464 hpi_handle_error(hpi_format_create(&dpcm->format,
465 params_channels(params),
466 format, params_rate(params), 0, 0));
468 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
469 if (hpi_instream_reset(dpcm->h_stream) != 0)
472 if (hpi_instream_set_format(
473 dpcm->h_stream, &dpcm->format) != 0)
477 dpcm->hpi_buffer_attached = 0;
479 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
480 params_buffer_bytes(params), runtime->dma_addr);
483 "stream_host_buffer_attach succeeded %u %lu\n",
484 params_buffer_bytes(params),
485 (unsigned long)runtime->dma_addr);
487 snd_printd("stream_host_buffer_attach error %d\n",
492 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
493 &dpcm->hpi_buffer_attached,
496 snd_printdd("stream_host_buffer_attach status 0x%x\n",
497 dpcm->hpi_buffer_attached);
499 bytes_per_sec = params_rate(params) * params_channels(params);
500 width = snd_pcm_format_width(params_format(params));
501 bytes_per_sec *= width;
503 if (width < 0 || bytes_per_sec == 0)
506 dpcm->bytes_per_sec = bytes_per_sec;
507 dpcm->buffer_bytes = params_buffer_bytes(params);
508 dpcm->period_bytes = params_period_bytes(params);
514 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
516 struct snd_pcm_runtime *runtime = substream->runtime;
517 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
518 if (dpcm->hpi_buffer_attached)
519 hpi_stream_host_buffer_detach(dpcm->h_stream);
521 snd_pcm_lib_free_pages(substream);
525 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
527 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
531 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
534 struct snd_pcm_runtime *runtime = substream->runtime;
535 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
539 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
540 expiry = max(expiry, 1); /* don't let it be zero! */
541 dpcm->timer.expires = jiffies + expiry;
542 dpcm->respawn_timer = 1;
543 add_timer(&dpcm->timer);
546 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
548 struct snd_pcm_runtime *runtime = substream->runtime;
549 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
551 dpcm->respawn_timer = 0;
552 del_timer(&dpcm->timer);
555 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
558 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
559 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
560 struct snd_pcm_substream *s;
564 snd_pcm_debug_name(substream, name, sizeof(name));
565 snd_printdd("%s trigger\n", name);
568 case SNDRV_PCM_TRIGGER_START:
569 snd_pcm_group_for_each_entry(s, substream) {
570 struct snd_pcm_runtime *runtime = s->runtime;
571 struct snd_card_asihpi_pcm *ds = runtime->private_data;
573 if (snd_pcm_substream_chip(s) != card)
576 /* don't link Cap and Play */
577 if (substream->stream != s->stream)
580 ds->drained_count = 0;
581 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
582 /* How do I know how much valid data is present
583 * in buffer? Must be at least one period!
584 * Guessing 2 periods, but if
585 * buffer is bigger it may contain even more
588 unsigned int preload = ds->period_bytes * 1;
589 snd_printddd("%d preload x%x\n", s->number, preload);
590 hpi_handle_error(hpi_outstream_write_buf(
592 &runtime->dma_area[0],
595 ds->pcm_buf_host_rw_ofs = preload;
598 if (card->support_grouping) {
599 snd_printdd("%d group\n", s->number);
600 e = hpi_stream_group_add(
604 snd_pcm_trigger_done(s, substream);
612 snd_printdd("start\n");
613 /* start the master stream */
614 snd_card_asihpi_pcm_timer_start(substream);
615 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
617 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
620 case SNDRV_PCM_TRIGGER_STOP:
621 snd_card_asihpi_pcm_timer_stop(substream);
622 snd_pcm_group_for_each_entry(s, substream) {
623 if (snd_pcm_substream_chip(s) != card)
625 /* don't link Cap and Play */
626 if (substream->stream != s->stream)
629 /*? workaround linked streams don't
630 transition to SETUP 20070706*/
631 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
633 if (card->support_grouping) {
634 snd_printdd("%d group\n", s->number);
635 snd_pcm_trigger_done(s, substream);
639 snd_printdd("stop\n");
641 /* _prepare and _hwparams reset the stream */
642 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
643 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
645 hpi_outstream_reset(dpcm->h_stream));
647 if (card->support_grouping)
648 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
651 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
652 snd_printdd("pause release\n");
653 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
654 snd_card_asihpi_pcm_timer_start(substream);
656 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
657 snd_printdd("pause\n");
658 snd_card_asihpi_pcm_timer_stop(substream);
659 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
662 snd_printd(KERN_ERR "\tINVALID\n");
670 Without linking degenerates to getting single stream pos etc
671 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
674 pcm_buf_dma_ofs=get_buf_pos(s);
675 for_each_linked_stream(s) {
676 pcm_buf_dma_ofs=get_buf_pos(s);
677 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
678 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
680 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
681 for_each_linked_stream(s) {
682 s->pcm_buf_dma_ofs = min_buf_pos;
683 if (new_data > period_bytes) {
685 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
692 snd_pcm_period_elapsed(s);
697 /** Minimum of 2 modulo values. Works correctly when the difference between
698 * the values is less than half the modulus
700 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
701 unsigned long int modulus)
704 if (((a-b) % modulus) < (modulus/2))
712 /** Timer function, equivalent to interrupt service routine for cards
714 static void snd_card_asihpi_timer_function(unsigned long data)
716 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
717 struct snd_pcm_substream *substream = dpcm->substream;
718 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
719 struct snd_pcm_runtime *runtime;
720 struct snd_pcm_substream *s;
721 unsigned int newdata = 0;
722 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
723 unsigned int remdata, xfercount, next_jiffies;
727 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
730 snd_pcm_debug_name(substream, name, sizeof(name));
732 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
734 /* find minimum newdata and buffer pos in group */
735 snd_pcm_group_for_each_entry(s, substream) {
736 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
737 runtime = s->runtime;
739 if (snd_pcm_substream_chip(s) != card)
742 /* don't link Cap and Play */
743 if (substream->stream != s->stream)
746 hpi_handle_error(hpi_stream_get_info_ex(
747 ds->h_stream, &state,
748 &buffer_size, &bytes_avail,
749 &samples_played, &on_card_bytes));
751 /* number of bytes in on-card buffer */
752 runtime->delay = on_card_bytes;
755 on_card_bytes = bytes_avail;
757 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
758 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
759 if (state == HPI_STATE_STOPPED) {
760 if ((bytes_avail == 0) &&
761 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
762 hpi_handle_error(hpi_stream_start(ds->h_stream));
763 snd_printdd("P%d start\n", s->number);
764 ds->drained_count = 0;
766 } else if (state == HPI_STATE_DRAINED) {
767 snd_printd(KERN_WARNING "P%d drained\n",
770 if (ds->drained_count > 2) {
772 snd_pcm_stream_lock_irqsave(s, flags);
773 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
774 snd_pcm_stream_unlock_irqrestore(s, flags);
778 ds->drained_count = 0;
781 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
784 /* can't statically init min when wrap is involved */
785 min_buf_pos = pcm_buf_dma_ofs;
786 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
790 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
792 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
796 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
797 (unsigned long)frames_to_bytes(runtime,
798 runtime->status->hw_ptr),
799 (unsigned long)frames_to_bytes(runtime,
800 runtime->control->appl_ptr));
802 snd_printdd("%d S=%d, "
803 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
804 "aux=0x%04X space=0x%04X\n",
806 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
808 (int)on_card_bytes, buffer_size-bytes_avail);
811 pcm_buf_dma_ofs = min_buf_pos;
813 remdata = newdata % dpcm->period_bytes;
814 xfercount = newdata - remdata; /* a multiple of period_bytes */
815 /* come back when on_card_bytes has decreased enough to allow
816 write to happen, or when data has been consumed to make another
819 if (xfercount && (on_card_bytes > dpcm->period_bytes))
820 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
822 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
824 next_jiffies = max(next_jiffies, 1U);
825 dpcm->timer.expires = jiffies + next_jiffies;
826 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
827 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
829 snd_pcm_group_for_each_entry(s, substream) {
830 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
832 /* don't link Cap and Play */
833 if (substream->stream != s->stream)
836 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
839 /* Limit use of on card fifo for playback */
840 ((on_card_bytes <= ds->period_bytes) ||
841 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
845 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
846 unsigned int xfer1, xfer2;
847 char *pd = &s->runtime->dma_area[buf_ofs];
849 if (card->can_dma) { /* buffer wrap is handled at lower level */
853 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
854 xfer2 = xfercount - xfer1;
857 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
858 snd_printddd("P%d write1 0x%04X 0x%04X\n",
859 s->number, xfer1, buf_ofs);
861 hpi_outstream_write_buf(
862 ds->h_stream, pd, xfer1,
866 pd = s->runtime->dma_area;
868 snd_printddd("P%d write2 0x%04X 0x%04X\n",
870 xfercount - xfer1, buf_ofs);
872 hpi_outstream_write_buf(
878 snd_printddd("C%d read1 0x%04x\n",
881 hpi_instream_read_buf(
885 pd = s->runtime->dma_area;
886 snd_printddd("C%d read2 0x%04x\n",
889 hpi_instream_read_buf(
894 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
895 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
896 snd_pcm_period_elapsed(s);
900 if (dpcm->respawn_timer)
901 add_timer(&dpcm->timer);
904 /***************************** PLAYBACK OPS ****************/
905 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
906 unsigned int cmd, void *arg)
908 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
909 return snd_pcm_lib_ioctl(substream, cmd, arg);
912 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
915 struct snd_pcm_runtime *runtime = substream->runtime;
916 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
918 snd_printdd("P%d prepare\n", substream->number);
920 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
921 dpcm->pcm_buf_host_rw_ofs = 0;
922 dpcm->pcm_buf_dma_ofs = 0;
923 dpcm->pcm_buf_elapsed_dma_ofs = 0;
927 static snd_pcm_uframes_t
928 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
930 struct snd_pcm_runtime *runtime = substream->runtime;
931 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
932 snd_pcm_uframes_t ptr;
934 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
935 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
939 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
941 struct snd_pcm_hardware *pcmhw)
943 struct hpi_format hpi_format;
947 u32 sample_rate = 48000;
949 /* on cards without SRC, must query at valid rate,
950 * maybe set by external sync
952 err = hpi_mixer_get_control(asihpi->h_mixer,
953 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
954 HPI_CONTROL_SAMPLECLOCK, &h_control);
957 err = hpi_sample_clock_get_sample_rate(h_control,
960 for (format = HPI_FORMAT_PCM8_UNSIGNED;
961 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
962 err = hpi_format_create(&hpi_format,
963 2, format, sample_rate, 128000, 0);
965 err = hpi_outstream_query_format(h_stream,
967 if (!err && (hpi_to_alsa_formats[format] != -1))
969 (1ULL << hpi_to_alsa_formats[format]);
973 static struct snd_pcm_hardware snd_card_asihpi_playback = {
976 .buffer_bytes_max = BUFFER_BYTES_MAX,
977 .period_bytes_min = PERIOD_BYTES_MIN,
978 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
979 .periods_min = PERIODS_MIN,
980 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
984 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
986 struct snd_pcm_runtime *runtime = substream->runtime;
987 struct snd_card_asihpi_pcm *dpcm;
988 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
991 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
996 hpi_outstream_open(card->adapter_index,
997 substream->number, &dpcm->h_stream);
998 hpi_handle_error(err);
1001 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1006 /*? also check ASI5000 samplerate source
1007 If external, only support external rate.
1008 If internal and other stream playing, can't switch
1011 init_timer(&dpcm->timer);
1012 dpcm->timer.data = (unsigned long) dpcm;
1013 dpcm->timer.function = snd_card_asihpi_timer_function;
1014 dpcm->substream = substream;
1015 runtime->private_data = dpcm;
1016 runtime->private_free = snd_card_asihpi_runtime_free;
1018 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1019 /*?snd_card_asihpi_playback.period_bytes_min =
1020 card->out_max_chans * 4096; */
1022 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1023 &snd_card_asihpi_playback);
1025 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1027 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1028 SNDRV_PCM_INFO_DOUBLE |
1029 SNDRV_PCM_INFO_BATCH |
1030 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1031 SNDRV_PCM_INFO_PAUSE |
1032 SNDRV_PCM_INFO_MMAP |
1033 SNDRV_PCM_INFO_MMAP_VALID;
1035 if (card->support_grouping)
1036 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1038 /* struct is copied, so can create initializer dynamically */
1039 runtime->hw = snd_card_asihpi_playback;
1042 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1043 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1047 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1048 card->update_interval_frames);
1050 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1051 card->update_interval_frames * 2, UINT_MAX);
1053 snd_pcm_set_sync(substream);
1055 snd_printdd("playback open\n");
1060 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1062 struct snd_pcm_runtime *runtime = substream->runtime;
1063 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1065 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1066 snd_printdd("playback close\n");
1071 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1072 .open = snd_card_asihpi_playback_open,
1073 .close = snd_card_asihpi_playback_close,
1074 .ioctl = snd_card_asihpi_playback_ioctl,
1075 .hw_params = snd_card_asihpi_pcm_hw_params,
1076 .hw_free = snd_card_asihpi_hw_free,
1077 .prepare = snd_card_asihpi_playback_prepare,
1078 .trigger = snd_card_asihpi_trigger,
1079 .pointer = snd_card_asihpi_playback_pointer,
1082 /***************************** CAPTURE OPS ****************/
1083 static snd_pcm_uframes_t
1084 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1086 struct snd_pcm_runtime *runtime = substream->runtime;
1087 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1089 snd_printddd("capture pointer %d=%d\n",
1090 substream->number, dpcm->pcm_buf_dma_ofs);
1091 /* NOTE Unlike playback can't use actual samples_played
1092 for the capture position, because those samples aren't yet in
1093 the local buffer available for reading.
1095 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1098 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1099 unsigned int cmd, void *arg)
1101 return snd_pcm_lib_ioctl(substream, cmd, arg);
1104 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1106 struct snd_pcm_runtime *runtime = substream->runtime;
1107 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1109 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1110 dpcm->pcm_buf_host_rw_ofs = 0;
1111 dpcm->pcm_buf_dma_ofs = 0;
1112 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1114 snd_printdd("Capture Prepare %d\n", substream->number);
1120 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1122 struct snd_pcm_hardware *pcmhw)
1124 struct hpi_format hpi_format;
1128 u32 sample_rate = 48000;
1130 /* on cards without SRC, must query at valid rate,
1131 maybe set by external sync */
1132 err = hpi_mixer_get_control(asihpi->h_mixer,
1133 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1134 HPI_CONTROL_SAMPLECLOCK, &h_control);
1137 err = hpi_sample_clock_get_sample_rate(h_control,
1140 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1141 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1143 err = hpi_format_create(&hpi_format, 2, format,
1144 sample_rate, 128000, 0);
1146 err = hpi_instream_query_format(h_stream,
1150 (1ULL << hpi_to_alsa_formats[format]);
1155 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1158 .buffer_bytes_max = BUFFER_BYTES_MAX,
1159 .period_bytes_min = PERIOD_BYTES_MIN,
1160 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1161 .periods_min = PERIODS_MIN,
1162 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1166 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1168 struct snd_pcm_runtime *runtime = substream->runtime;
1169 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1170 struct snd_card_asihpi_pcm *dpcm;
1173 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1177 snd_printdd("capture open adapter %d stream %d\n",
1178 card->adapter_index, substream->number);
1180 err = hpi_handle_error(
1181 hpi_instream_open(card->adapter_index,
1182 substream->number, &dpcm->h_stream));
1185 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1191 init_timer(&dpcm->timer);
1192 dpcm->timer.data = (unsigned long) dpcm;
1193 dpcm->timer.function = snd_card_asihpi_timer_function;
1194 dpcm->substream = substream;
1195 runtime->private_data = dpcm;
1196 runtime->private_free = snd_card_asihpi_runtime_free;
1198 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1199 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1200 &snd_card_asihpi_capture);
1201 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1202 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1203 SNDRV_PCM_INFO_MMAP |
1204 SNDRV_PCM_INFO_MMAP_VALID;
1206 if (card->support_grouping)
1207 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1209 runtime->hw = snd_card_asihpi_capture;
1212 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1213 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1217 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1218 card->update_interval_frames);
1219 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1220 card->update_interval_frames * 2, UINT_MAX);
1222 snd_pcm_set_sync(substream);
1227 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1229 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1231 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1235 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1236 .open = snd_card_asihpi_capture_open,
1237 .close = snd_card_asihpi_capture_close,
1238 .ioctl = snd_card_asihpi_capture_ioctl,
1239 .hw_params = snd_card_asihpi_pcm_hw_params,
1240 .hw_free = snd_card_asihpi_hw_free,
1241 .prepare = snd_card_asihpi_capture_prepare,
1242 .trigger = snd_card_asihpi_trigger,
1243 .pointer = snd_card_asihpi_capture_pointer,
1246 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1247 int device, int substreams)
1249 struct snd_pcm *pcm;
1252 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1253 asihpi->num_outstreams, asihpi->num_instreams,
1257 /* pointer to ops struct is stored, dont change ops afterwards! */
1258 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1259 &snd_card_asihpi_playback_mmap_ops);
1260 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1261 &snd_card_asihpi_capture_mmap_ops);
1263 pcm->private_data = asihpi;
1264 pcm->info_flags = 0;
1265 strcpy(pcm->name, "Asihpi PCM");
1267 /*? do we want to emulate MMAP for non-BBM cards?
1268 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1269 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1270 snd_dma_pci_data(asihpi->pci),
1271 64*1024, BUFFER_BYTES_MAX);
1276 /***************************** MIXER CONTROLS ****************/
1277 struct hpi_control {
1285 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1288 static const char * const asihpi_tuner_band_names[] = {
1301 compile_time_assert(
1302 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1303 (HPI_TUNER_BAND_LAST+1)),
1304 assert_tuner_band_names_size);
1306 static const char * const asihpi_src_names[] = {
1323 compile_time_assert(
1324 (ARRAY_SIZE(asihpi_src_names) ==
1325 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1326 assert_src_names_size);
1328 static const char * const asihpi_dst_names[] = {
1341 compile_time_assert(
1342 (ARRAY_SIZE(asihpi_dst_names) ==
1343 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1344 assert_dst_names_size);
1346 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1347 struct snd_card_asihpi *asihpi)
1351 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1354 else if (mixer_dump)
1355 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1360 /* Convert HPI control name and location into ALSA control name */
1361 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1362 struct hpi_control *hpi_ctl,
1366 memset(snd_control, 0, sizeof(*snd_control));
1367 snd_control->name = hpi_ctl->name;
1368 snd_control->private_value = hpi_ctl->h_control;
1369 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1370 snd_control->index = 0;
1372 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1373 dir = ""; /* clock is neither capture nor playback */
1374 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1375 dir = "Capture "; /* On or towards a PCM capture destination*/
1376 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1377 (!hpi_ctl->dst_node_type))
1378 dir = "Capture "; /* On a source node that is not PCM playback */
1379 else if (hpi_ctl->src_node_type &&
1380 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1381 (hpi_ctl->dst_node_type))
1382 dir = "Monitor Playback "; /* Between an input and an output */
1384 dir = "Playback "; /* PCM Playback source, or output node */
1386 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1387 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1388 asihpi_src_names[hpi_ctl->src_node_type],
1389 hpi_ctl->src_node_index,
1390 asihpi_dst_names[hpi_ctl->dst_node_type],
1391 hpi_ctl->dst_node_index,
1393 else if (hpi_ctl->dst_node_type) {
1394 sprintf(hpi_ctl->name, "%s %d %s%s",
1395 asihpi_dst_names[hpi_ctl->dst_node_type],
1396 hpi_ctl->dst_node_index,
1399 sprintf(hpi_ctl->name, "%s %d %s%s",
1400 asihpi_src_names[hpi_ctl->src_node_type],
1401 hpi_ctl->src_node_index,
1404 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1405 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1408 /*------------------------------------------------------------
1410 ------------------------------------------------------------*/
1411 #define VOL_STEP_mB 1
1412 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1413 struct snd_ctl_elem_info *uinfo)
1415 u32 h_control = kcontrol->private_value;
1417 /* native gains are in millibels */
1422 err = hpi_volume_query_range(h_control,
1423 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1426 min_gain_mB = -10000;
1427 step_gain_mB = VOL_STEP_mB;
1430 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1432 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1433 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1434 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1438 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
1441 u32 h_control = kcontrol->private_value;
1442 short an_gain_mB[HPI_MAX_CHANNELS];
1444 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1445 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1446 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1451 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1455 u32 h_control = kcontrol->private_value;
1456 short an_gain_mB[HPI_MAX_CHANNELS];
1459 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1461 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1462 /* change = asihpi->mixer_volume[addr][0] != left ||
1463 asihpi->mixer_volume[addr][1] != right;
1466 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1470 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1472 #define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
1474 static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1475 struct snd_ctl_elem_value *ucontrol)
1477 u32 h_control = kcontrol->private_value;
1480 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1481 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1486 static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1487 struct snd_ctl_elem_value *ucontrol)
1489 u32 h_control = kcontrol->private_value;
1491 /* HPI currently only supports all or none muting of multichannel volume
1492 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1494 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1495 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1499 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1500 struct hpi_control *hpi_ctl)
1502 struct snd_card *card = asihpi->card;
1503 struct snd_kcontrol_new snd_control;
1507 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1508 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1509 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1510 snd_control.info = snd_asihpi_volume_info;
1511 snd_control.get = snd_asihpi_volume_get;
1512 snd_control.put = snd_asihpi_volume_put;
1513 snd_control.tlv.p = db_scale_100;
1515 err = ctl_add(card, &snd_control, asihpi);
1519 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1520 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1521 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1522 snd_control.info = snd_asihpi_volume_mute_info;
1523 snd_control.get = snd_asihpi_volume_mute_get;
1524 snd_control.put = snd_asihpi_volume_mute_put;
1525 err = ctl_add(card, &snd_control, asihpi);
1530 /*------------------------------------------------------------
1532 ------------------------------------------------------------*/
1533 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_info *uinfo)
1536 u32 h_control = kcontrol->private_value;
1543 hpi_level_query_range(h_control, &min_gain_mB,
1544 &max_gain_mB, &step_gain_mB);
1547 min_gain_mB = -1000;
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1553 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1554 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1555 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1559 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1562 u32 h_control = kcontrol->private_value;
1563 short an_gain_mB[HPI_MAX_CHANNELS];
1565 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1566 ucontrol->value.integer.value[0] =
1567 an_gain_mB[0] / HPI_UNITS_PER_dB;
1568 ucontrol->value.integer.value[1] =
1569 an_gain_mB[1] / HPI_UNITS_PER_dB;
1574 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1578 u32 h_control = kcontrol->private_value;
1579 short an_gain_mB[HPI_MAX_CHANNELS];
1582 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1584 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1585 /* change = asihpi->mixer_level[addr][0] != left ||
1586 asihpi->mixer_level[addr][1] != right;
1589 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1593 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1595 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1596 struct hpi_control *hpi_ctl)
1598 struct snd_card *card = asihpi->card;
1599 struct snd_kcontrol_new snd_control;
1601 /* can't use 'volume' cos some nodes have volume as well */
1602 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1603 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1604 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1605 snd_control.info = snd_asihpi_level_info;
1606 snd_control.get = snd_asihpi_level_get;
1607 snd_control.put = snd_asihpi_level_put;
1608 snd_control.tlv.p = db_scale_level;
1610 return ctl_add(card, &snd_control, asihpi);
1613 /*------------------------------------------------------------
1615 ------------------------------------------------------------*/
1618 static const char * const asihpi_aesebu_format_names[] = {
1619 "N/A", "S/PDIF", "AES/EBU" };
1621 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_info *uinfo)
1624 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1626 uinfo->value.enumerated.items = 3;
1628 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1629 uinfo->value.enumerated.item =
1630 uinfo->value.enumerated.items - 1;
1632 strcpy(uinfo->value.enumerated.name,
1633 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1638 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol,
1640 u16 (*func)(u32, u16 *))
1642 u32 h_control = kcontrol->private_value;
1645 err = func(h_control, &source);
1647 /* default to N/A */
1648 ucontrol->value.enumerated.item[0] = 0;
1649 /* return success but set the control to N/A */
1652 if (source == HPI_AESEBU_FORMAT_SPDIF)
1653 ucontrol->value.enumerated.item[0] = 1;
1654 if (source == HPI_AESEBU_FORMAT_AESEBU)
1655 ucontrol->value.enumerated.item[0] = 2;
1660 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1661 struct snd_ctl_elem_value *ucontrol,
1662 u16 (*func)(u32, u16))
1664 u32 h_control = kcontrol->private_value;
1666 /* default to S/PDIF */
1667 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1669 if (ucontrol->value.enumerated.item[0] == 1)
1670 source = HPI_AESEBU_FORMAT_SPDIF;
1671 if (ucontrol->value.enumerated.item[0] == 2)
1672 source = HPI_AESEBU_FORMAT_AESEBU;
1674 if (func(h_control, source) != 0)
1680 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol) {
1682 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1683 hpi_aesebu_receiver_get_format);
1686 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol) {
1688 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1689 hpi_aesebu_receiver_set_format);
1692 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_info *uinfo)
1695 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1698 uinfo->value.integer.min = 0;
1699 uinfo->value.integer.max = 0X1F;
1700 uinfo->value.integer.step = 1;
1705 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol) {
1708 u32 h_control = kcontrol->private_value;
1711 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1712 h_control, &status));
1713 ucontrol->value.integer.value[0] = status;
1717 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1718 struct hpi_control *hpi_ctl)
1720 struct snd_card *card = asihpi->card;
1721 struct snd_kcontrol_new snd_control;
1723 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1724 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1725 snd_control.info = snd_asihpi_aesebu_format_info;
1726 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1727 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1730 if (ctl_add(card, &snd_control, asihpi) < 0)
1733 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1734 snd_control.access =
1735 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1736 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1737 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1739 return ctl_add(card, &snd_control, asihpi);
1742 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_value *ucontrol) {
1744 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1745 hpi_aesebu_transmitter_get_format);
1748 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *ucontrol) {
1750 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1751 hpi_aesebu_transmitter_set_format);
1755 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1756 struct hpi_control *hpi_ctl)
1758 struct snd_card *card = asihpi->card;
1759 struct snd_kcontrol_new snd_control;
1761 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1762 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1763 snd_control.info = snd_asihpi_aesebu_format_info;
1764 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1765 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1767 return ctl_add(card, &snd_control, asihpi);
1770 /*------------------------------------------------------------
1772 ------------------------------------------------------------*/
1776 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_info *uinfo)
1779 u32 h_control = kcontrol->private_value;
1784 for (idx = 0; idx < 3; idx++) {
1785 err = hpi_tuner_query_gain(h_control,
1786 idx, &gain_range[idx]);
1791 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1793 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1794 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1795 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1799 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1800 struct snd_ctl_elem_value *ucontrol)
1803 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1805 u32 h_control = kcontrol->private_value;
1808 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1809 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1814 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1815 struct snd_ctl_elem_value *ucontrol)
1818 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1820 u32 h_control = kcontrol->private_value;
1823 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1824 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1831 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1832 u16 *band_list, u32 len) {
1833 u32 h_control = kcontrol->private_value;
1837 for (i = 0; i < len; i++) {
1838 err = hpi_tuner_query_band(
1839 h_control, i, &band_list[i]);
1844 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1850 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1851 struct snd_ctl_elem_info *uinfo)
1853 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1856 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1857 HPI_TUNER_BAND_LAST);
1862 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1864 uinfo->value.enumerated.items = num_bands;
1866 if (num_bands > 0) {
1867 if (uinfo->value.enumerated.item >=
1868 uinfo->value.enumerated.items)
1869 uinfo->value.enumerated.item =
1870 uinfo->value.enumerated.items - 1;
1872 strcpy(uinfo->value.enumerated.name,
1873 asihpi_tuner_band_names[
1874 tuner_bands[uinfo->value.enumerated.item]]);
1880 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *ucontrol)
1883 u32 h_control = kcontrol->private_value;
1885 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1888 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1891 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1892 HPI_TUNER_BAND_LAST);
1894 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1896 ucontrol->value.enumerated.item[0] = -1;
1897 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1898 if (tuner_bands[idx] == band) {
1899 ucontrol->value.enumerated.item[0] = idx;
1906 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_value *ucontrol)
1910 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1912 u32 h_control = kcontrol->private_value;
1914 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1917 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1918 HPI_TUNER_BAND_LAST);
1920 band = tuner_bands[ucontrol->value.enumerated.item[0]];
1921 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1928 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1929 struct snd_ctl_elem_info *uinfo)
1931 u32 h_control = kcontrol->private_value;
1933 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1934 u16 num_bands = 0, band_iter, idx;
1935 u32 freq_range[3], temp_freq_range[3];
1937 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1938 HPI_TUNER_BAND_LAST);
1940 freq_range[0] = INT_MAX;
1942 freq_range[2] = INT_MAX;
1944 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1945 for (idx = 0; idx < 3; idx++) {
1946 err = hpi_tuner_query_frequency(h_control,
1947 idx, tuner_bands[band_iter],
1948 &temp_freq_range[idx]);
1953 /* skip band with bogus stepping */
1954 if (temp_freq_range[2] <= 0)
1957 if (temp_freq_range[0] < freq_range[0])
1958 freq_range[0] = temp_freq_range[0];
1959 if (temp_freq_range[1] > freq_range[1])
1960 freq_range[1] = temp_freq_range[1];
1961 if (temp_freq_range[2] < freq_range[2])
1962 freq_range[2] = temp_freq_range[2];
1965 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1967 uinfo->value.integer.min = ((int)freq_range[0]);
1968 uinfo->value.integer.max = ((int)freq_range[1]);
1969 uinfo->value.integer.step = ((int)freq_range[2]);
1973 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1974 struct snd_ctl_elem_value *ucontrol)
1976 u32 h_control = kcontrol->private_value;
1979 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1980 ucontrol->value.integer.value[0] = freq;
1985 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_value *ucontrol)
1988 u32 h_control = kcontrol->private_value;
1991 freq = ucontrol->value.integer.value[0];
1992 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1997 /* Tuner control group initializer */
1998 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1999 struct hpi_control *hpi_ctl)
2001 struct snd_card *card = asihpi->card;
2002 struct snd_kcontrol_new snd_control;
2004 snd_control.private_value = hpi_ctl->h_control;
2005 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2007 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
2008 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
2009 snd_control.info = snd_asihpi_tuner_gain_info;
2010 snd_control.get = snd_asihpi_tuner_gain_get;
2011 snd_control.put = snd_asihpi_tuner_gain_put;
2013 if (ctl_add(card, &snd_control, asihpi) < 0)
2017 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
2018 snd_control.info = snd_asihpi_tuner_band_info;
2019 snd_control.get = snd_asihpi_tuner_band_get;
2020 snd_control.put = snd_asihpi_tuner_band_put;
2022 if (ctl_add(card, &snd_control, asihpi) < 0)
2025 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
2026 snd_control.info = snd_asihpi_tuner_freq_info;
2027 snd_control.get = snd_asihpi_tuner_freq_get;
2028 snd_control.put = snd_asihpi_tuner_freq_put;
2030 return ctl_add(card, &snd_control, asihpi);
2033 /*------------------------------------------------------------
2035 ------------------------------------------------------------*/
2036 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2037 struct snd_ctl_elem_info *uinfo)
2039 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2040 uinfo->count = HPI_MAX_CHANNELS;
2041 uinfo->value.integer.min = 0;
2042 uinfo->value.integer.max = 0x7FFFFFFF;
2046 /* linear values for 10dB steps */
2047 static int log2lin[] = {
2048 0x7FFFFFFF, /* 0dB */
2054 2147484, /* -60dB */
2069 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2070 struct snd_ctl_elem_value *ucontrol)
2072 u32 h_control = kcontrol->private_value;
2073 short an_gain_mB[HPI_MAX_CHANNELS], i;
2076 err = hpi_meter_get_peak(h_control, an_gain_mB);
2078 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2080 ucontrol->value.integer.value[i] = 0;
2081 } else if (an_gain_mB[i] >= 0) {
2082 ucontrol->value.integer.value[i] =
2083 an_gain_mB[i] << 16;
2085 /* -ve is log value in millibels < -60dB,
2086 * convert to (roughly!) linear,
2088 ucontrol->value.integer.value[i] =
2089 log2lin[an_gain_mB[i] / -1000];
2095 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2096 struct hpi_control *hpi_ctl, int subidx)
2098 struct snd_card *card = asihpi->card;
2099 struct snd_kcontrol_new snd_control;
2101 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2102 snd_control.access =
2103 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2104 snd_control.info = snd_asihpi_meter_info;
2105 snd_control.get = snd_asihpi_meter_get;
2107 snd_control.index = subidx;
2109 return ctl_add(card, &snd_control, asihpi);
2112 /*------------------------------------------------------------
2113 Multiplexer controls
2114 ------------------------------------------------------------*/
2115 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2117 u32 h_control = snd_control->private_value;
2118 struct hpi_control hpi_ctl;
2120 for (s = 0; s < 32; s++) {
2121 err = hpi_multiplexer_query_source(h_control, s,
2132 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2133 struct snd_ctl_elem_info *uinfo)
2136 u16 src_node_type, src_node_index;
2137 u32 h_control = kcontrol->private_value;
2139 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2141 uinfo->value.enumerated.items =
2142 snd_card_asihpi_mux_count_sources(kcontrol);
2144 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2145 uinfo->value.enumerated.item =
2146 uinfo->value.enumerated.items - 1;
2149 hpi_multiplexer_query_source(h_control,
2150 uinfo->value.enumerated.item,
2151 &src_node_type, &src_node_index);
2153 sprintf(uinfo->value.enumerated.name, "%s %d",
2154 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2159 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2160 struct snd_ctl_elem_value *ucontrol)
2162 u32 h_control = kcontrol->private_value;
2163 u16 source_type, source_index;
2164 u16 src_node_type, src_node_index;
2167 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2168 &source_type, &source_index));
2169 /* Should cache this search result! */
2170 for (s = 0; s < 256; s++) {
2171 if (hpi_multiplexer_query_source(h_control, s,
2172 &src_node_type, &src_node_index))
2175 if ((source_type == src_node_type)
2176 && (source_index == src_node_index)) {
2177 ucontrol->value.enumerated.item[0] = s;
2181 snd_printd(KERN_WARNING
2182 "Control %x failed to match mux source %hu %hu\n",
2183 h_control, source_type, source_index);
2184 ucontrol->value.enumerated.item[0] = 0;
2188 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2189 struct snd_ctl_elem_value *ucontrol)
2192 u32 h_control = kcontrol->private_value;
2193 u16 source_type, source_index;
2198 e = hpi_multiplexer_query_source(h_control,
2199 ucontrol->value.enumerated.item[0],
2200 &source_type, &source_index);
2203 hpi_multiplexer_set_source(h_control,
2204 source_type, source_index));
2209 static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2210 struct hpi_control *hpi_ctl)
2212 struct snd_card *card = asihpi->card;
2213 struct snd_kcontrol_new snd_control;
2215 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2216 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2217 snd_control.info = snd_asihpi_mux_info;
2218 snd_control.get = snd_asihpi_mux_get;
2219 snd_control.put = snd_asihpi_mux_put;
2221 return ctl_add(card, &snd_control, asihpi);
2225 /*------------------------------------------------------------
2226 Channel mode controls
2227 ------------------------------------------------------------*/
2228 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2229 struct snd_ctl_elem_info *uinfo)
2231 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2234 "From Left", "From Right",
2235 "To Left", "To Right"
2238 u32 h_control = kcontrol->private_value;
2242 int valid_modes = 0;
2244 /* HPI channel mode values can be from 1 to 6
2245 Some adapters only support a contiguous subset
2247 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2248 if (!hpi_channel_mode_query_mode(
2249 h_control, i, &mode)) {
2250 mode_map[valid_modes] = mode;
2254 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2256 uinfo->value.enumerated.items = valid_modes;
2258 if (uinfo->value.enumerated.item >= valid_modes)
2259 uinfo->value.enumerated.item = valid_modes - 1;
2261 strcpy(uinfo->value.enumerated.name,
2262 mode_names[mode_map[uinfo->value.enumerated.item]]);
2267 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2268 struct snd_ctl_elem_value *ucontrol)
2270 u32 h_control = kcontrol->private_value;
2273 if (hpi_channel_mode_get(h_control, &mode))
2276 ucontrol->value.enumerated.item[0] = mode - 1;
2281 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2282 struct snd_ctl_elem_value *ucontrol)
2285 u32 h_control = kcontrol->private_value;
2289 hpi_handle_error(hpi_channel_mode_set(h_control,
2290 ucontrol->value.enumerated.item[0] + 1));
2295 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2296 struct hpi_control *hpi_ctl)
2298 struct snd_card *card = asihpi->card;
2299 struct snd_kcontrol_new snd_control;
2301 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2302 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2303 snd_control.info = snd_asihpi_cmode_info;
2304 snd_control.get = snd_asihpi_cmode_get;
2305 snd_control.put = snd_asihpi_cmode_put;
2307 return ctl_add(card, &snd_control, asihpi);
2310 /*------------------------------------------------------------
2311 Sampleclock source controls
2312 ------------------------------------------------------------*/
2313 static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2314 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2315 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2317 "Digital2", "Digital3", "Digital4", "Digital5",
2318 "Digital6", "Digital7", "Digital8"};
2320 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2321 struct snd_ctl_elem_info *uinfo)
2323 struct snd_card_asihpi *asihpi =
2324 (struct snd_card_asihpi *)(kcontrol->private_data);
2325 struct clk_cache *clkcache = &asihpi->cc;
2326 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2328 uinfo->value.enumerated.items = clkcache->count;
2330 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2331 uinfo->value.enumerated.item =
2332 uinfo->value.enumerated.items - 1;
2334 strcpy(uinfo->value.enumerated.name,
2335 clkcache->s[uinfo->value.enumerated.item].name);
2339 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2340 struct snd_ctl_elem_value *ucontrol)
2342 struct snd_card_asihpi *asihpi =
2343 (struct snd_card_asihpi *)(kcontrol->private_data);
2344 struct clk_cache *clkcache = &asihpi->cc;
2345 u32 h_control = kcontrol->private_value;
2346 u16 source, srcindex = 0;
2349 ucontrol->value.enumerated.item[0] = 0;
2350 if (hpi_sample_clock_get_source(h_control, &source))
2353 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2354 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2357 for (i = 0; i < clkcache->count; i++)
2358 if ((clkcache->s[i].source == source) &&
2359 (clkcache->s[i].index == srcindex))
2362 ucontrol->value.enumerated.item[0] = i;
2367 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2368 struct snd_ctl_elem_value *ucontrol)
2370 struct snd_card_asihpi *asihpi =
2371 (struct snd_card_asihpi *)(kcontrol->private_data);
2372 struct clk_cache *clkcache = &asihpi->cc;
2374 u32 h_control = kcontrol->private_value;
2377 item = ucontrol->value.enumerated.item[0];
2378 if (item >= clkcache->count)
2379 item = clkcache->count-1;
2381 hpi_handle_error(hpi_sample_clock_set_source(
2382 h_control, clkcache->s[item].source));
2384 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2385 hpi_handle_error(hpi_sample_clock_set_source_index(
2386 h_control, clkcache->s[item].index));
2390 /*------------------------------------------------------------
2392 ------------------------------------------------------------*/
2393 /* Need to change this to enumerated control with list of rates */
2394 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2395 struct snd_ctl_elem_info *uinfo)
2397 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2399 uinfo->value.integer.min = 8000;
2400 uinfo->value.integer.max = 192000;
2401 uinfo->value.integer.step = 100;
2406 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2407 struct snd_ctl_elem_value *ucontrol)
2409 u32 h_control = kcontrol->private_value;
2413 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2415 ucontrol->value.integer.value[0] = rate;
2417 ucontrol->value.integer.value[0] = 0;
2421 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2422 struct snd_ctl_elem_value *ucontrol)
2425 u32 h_control = kcontrol->private_value;
2427 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2428 asihpi->mixer_clkrate[addr][1] != right;
2431 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2432 ucontrol->value.integer.value[0]));
2436 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2437 struct snd_ctl_elem_info *uinfo)
2439 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2441 uinfo->value.integer.min = 8000;
2442 uinfo->value.integer.max = 192000;
2443 uinfo->value.integer.step = 100;
2448 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2449 struct snd_ctl_elem_value *ucontrol)
2451 u32 h_control = kcontrol->private_value;
2455 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2457 ucontrol->value.integer.value[0] = rate;
2459 ucontrol->value.integer.value[0] = 0;
2463 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2464 struct hpi_control *hpi_ctl)
2466 struct snd_card *card = asihpi->card;
2467 struct snd_kcontrol_new snd_control;
2469 struct clk_cache *clkcache = &asihpi->cc;
2470 u32 hSC = hpi_ctl->h_control;
2475 snd_control.private_value = hpi_ctl->h_control;
2477 clkcache->has_local = 0;
2479 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2480 if (hpi_sample_clock_query_source(hSC,
2483 clkcache->s[i].source = source;
2484 clkcache->s[i].index = 0;
2485 clkcache->s[i].name = sampleclock_sources[source];
2486 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2488 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2489 clkcache->has_local = 1;
2492 /* already will have picked up index 0 above */
2493 for (j = 1; j < 8; j++) {
2494 if (hpi_sample_clock_query_source_index(hSC,
2495 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2498 clkcache->s[i].source =
2499 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2500 clkcache->s[i].index = j;
2501 clkcache->s[i].name = sampleclock_sources[
2502 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2505 clkcache->count = i;
2507 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2508 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2509 snd_control.info = snd_asihpi_clksrc_info;
2510 snd_control.get = snd_asihpi_clksrc_get;
2511 snd_control.put = snd_asihpi_clksrc_put;
2512 if (ctl_add(card, &snd_control, asihpi) < 0)
2516 if (clkcache->has_local) {
2517 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2518 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2519 snd_control.info = snd_asihpi_clklocal_info;
2520 snd_control.get = snd_asihpi_clklocal_get;
2521 snd_control.put = snd_asihpi_clklocal_put;
2524 if (ctl_add(card, &snd_control, asihpi) < 0)
2528 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2529 snd_control.access =
2530 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2531 snd_control.info = snd_asihpi_clkrate_info;
2532 snd_control.get = snd_asihpi_clkrate_get;
2534 return ctl_add(card, &snd_control, asihpi);
2536 /*------------------------------------------------------------
2538 ------------------------------------------------------------*/
2540 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2542 struct snd_card *card = asihpi->card;
2543 unsigned int idx = 0;
2544 unsigned int subindex = 0;
2546 struct hpi_control hpi_ctl, prev_ctl;
2548 if (snd_BUG_ON(!asihpi))
2550 strcpy(card->mixername, "Asihpi Mixer");
2553 hpi_mixer_open(asihpi->adapter_index,
2555 hpi_handle_error(err);
2559 memset(&prev_ctl, 0, sizeof(prev_ctl));
2560 prev_ctl.control_type = -1;
2562 for (idx = 0; idx < 2000; idx++) {
2563 err = hpi_mixer_get_control_by_index(
2566 &hpi_ctl.src_node_type,
2567 &hpi_ctl.src_node_index,
2568 &hpi_ctl.dst_node_type,
2569 &hpi_ctl.dst_node_index,
2570 &hpi_ctl.control_type,
2571 &hpi_ctl.h_control);
2573 if (err == HPI_ERROR_CONTROL_DISABLED) {
2575 snd_printk(KERN_INFO
2576 "Disabled HPI Control(%d)\n",
2584 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2585 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2587 /* ASI50xx in SSX mode has multiple meters on the same node.
2588 Use subindex to create distinct ALSA controls
2589 for any duplicated controls.
2591 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2592 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2593 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2594 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2595 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2602 switch (hpi_ctl.control_type) {
2603 case HPI_CONTROL_VOLUME:
2604 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2606 case HPI_CONTROL_LEVEL:
2607 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2609 case HPI_CONTROL_MULTIPLEXER:
2610 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2612 case HPI_CONTROL_CHANNEL_MODE:
2613 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2615 case HPI_CONTROL_METER:
2616 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2618 case HPI_CONTROL_SAMPLECLOCK:
2619 err = snd_asihpi_sampleclock_add(
2622 case HPI_CONTROL_CONNECTION: /* ignore these */
2624 case HPI_CONTROL_TUNER:
2625 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2627 case HPI_CONTROL_AESEBU_TRANSMITTER:
2628 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2630 case HPI_CONTROL_AESEBU_RECEIVER:
2631 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2633 case HPI_CONTROL_VOX:
2634 case HPI_CONTROL_BITSTREAM:
2635 case HPI_CONTROL_MICROPHONE:
2636 case HPI_CONTROL_PARAMETRIC_EQ:
2637 case HPI_CONTROL_COMPANDER:
2640 snd_printk(KERN_INFO
2641 "Untranslated HPI Control"
2642 "(%d) %d %d %d %d %d\n",
2644 hpi_ctl.control_type,
2645 hpi_ctl.src_node_type,
2646 hpi_ctl.src_node_index,
2647 hpi_ctl.dst_node_type,
2648 hpi_ctl.dst_node_index);
2654 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2655 hpi_handle_error(err);
2657 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2662 /*------------------------------------------------------------
2664 ------------------------------------------------------------*/
2667 snd_asihpi_proc_read(struct snd_info_entry *entry,
2668 struct snd_info_buffer *buffer)
2670 struct snd_card_asihpi *asihpi = entry->private_data;
2677 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2679 "adapter ID=%4X\n_index=%d\n"
2680 "num_outstreams=%d\n_num_instreams=%d\n",
2681 asihpi->type, asihpi->adapter_index,
2682 asihpi->num_outstreams, asihpi->num_instreams);
2684 version = asihpi->version;
2686 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2687 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2689 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2691 err = hpi_mixer_get_control(asihpi->h_mixer,
2692 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2693 HPI_CONTROL_SAMPLECLOCK, &h_control);
2696 err = hpi_sample_clock_get_sample_rate(
2698 err += hpi_sample_clock_get_source(h_control, &source);
2701 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2702 rate, sampleclock_sources[source]);
2708 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2710 struct snd_info_entry *entry;
2712 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2713 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2716 /*------------------------------------------------------------
2718 ------------------------------------------------------------*/
2720 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2722 if (enable_hpi_hwdep)
2729 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2731 if (enable_hpi_hwdep)
2732 return asihpi_hpi_release(file);
2737 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2738 unsigned int cmd, unsigned long arg)
2740 if (enable_hpi_hwdep)
2741 return asihpi_hpi_ioctl(file, cmd, arg);
2747 /* results in /dev/snd/hwC#D0 file for each card with index #
2748 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2750 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2751 int device, struct snd_hwdep **rhwdep)
2753 struct snd_hwdep *hw;
2758 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2761 strcpy(hw->name, "asihpi (HPI)");
2762 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2763 hw->ops.open = snd_asihpi_hpi_open;
2764 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2765 hw->ops.release = snd_asihpi_hpi_release;
2766 hw->private_data = asihpi;
2772 /*------------------------------------------------------------
2774 ------------------------------------------------------------*/
2775 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2776 const struct pci_device_id *pci_id)
2783 struct hpi_adapter *hpi_card;
2784 struct snd_card *card;
2785 struct snd_card_asihpi *asihpi;
2791 if (dev >= SNDRV_CARDS)
2794 /* Should this be enable[hpi_card->index] ? */
2800 err = asihpi_adapter_probe(pci_dev, pci_id);
2804 hpi_card = pci_get_drvdata(pci_dev);
2805 /* first try to give the card the same index as its hardware index */
2806 err = snd_card_create(hpi_card->index,
2807 id[hpi_card->index], THIS_MODULE,
2808 sizeof(struct snd_card_asihpi),
2811 /* if that fails, try the default index==next available */
2813 snd_card_create(index[dev], id[dev],
2815 sizeof(struct snd_card_asihpi),
2819 snd_printk(KERN_WARNING
2820 "**** WARNING **** Adapter index %d->ALSA index %d\n",
2821 hpi_card->index, card->number);
2824 snd_card_set_dev(card, &pci_dev->dev);
2826 asihpi = (struct snd_card_asihpi *) card->private_data;
2827 asihpi->card = card;
2828 asihpi->pci = pci_dev;
2829 asihpi->adapter_index = hpi_card->index;
2830 hpi_handle_error(hpi_adapter_get_info(
2831 asihpi->adapter_index,
2832 &asihpi->num_outstreams,
2833 &asihpi->num_instreams,
2835 &asihpi->serial_number, &asihpi->type));
2837 version = asihpi->version;
2838 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2839 "num_instreams=%d S/N=%d\n"
2840 "Hw Version %c%d DSP code version %03d\n",
2841 asihpi->type, asihpi->adapter_index,
2842 asihpi->num_outstreams,
2843 asihpi->num_instreams, asihpi->serial_number,
2844 ((version >> 3) & 0xf) + 'A',
2846 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2848 pcm_substreams = asihpi->num_outstreams;
2849 if (pcm_substreams < asihpi->num_instreams)
2850 pcm_substreams = asihpi->num_instreams;
2852 err = hpi_adapter_get_property(asihpi->adapter_index,
2853 HPI_ADAPTER_PROPERTY_CAPS1,
2854 NULL, &asihpi->support_grouping);
2856 asihpi->support_grouping = 0;
2858 err = hpi_adapter_get_property(asihpi->adapter_index,
2859 HPI_ADAPTER_PROPERTY_CAPS2,
2860 &asihpi->support_mrx, NULL);
2862 asihpi->support_mrx = 0;
2864 err = hpi_adapter_get_property(asihpi->adapter_index,
2865 HPI_ADAPTER_PROPERTY_INTERVAL,
2866 NULL, &asihpi->update_interval_frames);
2868 asihpi->update_interval_frames = 512;
2870 if (!asihpi->can_dma)
2871 asihpi->update_interval_frames *= 2;
2873 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2876 err = hpi_instream_host_buffer_free(h_stream);
2877 asihpi->can_dma = (!err);
2879 hpi_handle_error(hpi_instream_close(h_stream));
2881 err = hpi_adapter_get_property(asihpi->adapter_index,
2882 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2883 &asihpi->in_max_chans, &asihpi->out_max_chans);
2885 asihpi->in_max_chans = 2;
2886 asihpi->out_max_chans = 2;
2889 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2891 asihpi->support_grouping,
2895 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2897 snd_printk(KERN_ERR "pcm_new failed\n");
2900 err = snd_card_asihpi_mixer_new(asihpi);
2902 snd_printk(KERN_ERR "mixer_new failed\n");
2906 err = hpi_mixer_get_control(asihpi->h_mixer,
2907 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2908 HPI_CONTROL_SAMPLECLOCK, &h_control);
2911 err = hpi_sample_clock_set_local_rate(
2912 h_control, adapter_fs);
2914 snd_asihpi_proc_init(asihpi);
2916 /* always create, can be enabled or disabled dynamically
2917 by enable_hwdep module param*/
2918 snd_asihpi_hpi_new(asihpi, 0, NULL);
2920 strcpy(card->driver, "ASIHPI");
2922 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2923 sprintf(card->longname, "%s %i",
2924 card->shortname, asihpi->adapter_index);
2925 err = snd_card_register(card);
2928 hpi_card->snd_card_asihpi = card;
2933 snd_card_free(card);
2934 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2939 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2941 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2943 snd_card_free(hpi_card->snd_card_asihpi);
2944 hpi_card->snd_card_asihpi = NULL;
2945 asihpi_adapter_remove(pci_dev);
2948 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2949 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2950 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2951 (kernel_ulong_t)HPI_6205},
2952 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2953 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2954 (kernel_ulong_t)HPI_6000},
2957 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2959 static struct pci_driver driver = {
2960 .name = KBUILD_MODNAME,
2961 .id_table = asihpi_pci_tbl,
2962 .probe = snd_asihpi_probe,
2963 .remove = __devexit_p(snd_asihpi_remove),
2965 /* .suspend = snd_asihpi_suspend,
2966 .resume = snd_asihpi_resume, */
2970 static int __init snd_asihpi_init(void)
2973 return pci_register_driver(&driver);
2976 static void __exit snd_asihpi_exit(void)
2979 pci_unregister_driver(&driver);
2983 module_init(snd_asihpi_init)
2984 module_exit(snd_asihpi_exit)