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/version.h>
31 #include <linux/init.h>
32 #include <linux/jiffies.h>
33 #include <linux/slab.h>
34 #include <linux/time.h>
35 #include <linux/wait.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>
46 MODULE_LICENSE("GPL");
47 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
48 MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
50 #if defined CONFIG_SND_DEBUG
51 /* copied from pcm_lib.c, hope later patch will make that version public
52 and this copy can be removed */
54 snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
56 snprintf(buf, size, "pcmC%dD%d%c:%d",
57 substream->pcm->card->number,
58 substream->pcm->device,
59 substream->stream ? 'c' : 'p',
64 snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
70 #if defined CONFIG_SND_DEBUG_VERBOSE
72 * snd_printddd - very verbose debug printk
73 * @format: format string
75 * Works like snd_printk() for debugging purposes.
76 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
77 * Must set snd module debug parameter to 3 to enable at runtime.
79 #define snd_printddd(format, args...) \
80 __snd_printk(3, __FILE__, __LINE__, format, ##args)
82 #define snd_printddd(format, args...) do { } while (0)
85 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
86 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
87 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
88 static int enable_hpi_hwdep = 1;
90 module_param_array(index, int, NULL, S_IRUGO);
91 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
93 module_param_array(id, charp, NULL, S_IRUGO);
94 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
96 module_param_array(enable, bool, NULL, S_IRUGO);
97 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
99 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(enable_hpi_hwdep,
101 "ALSA enable HPI hwdep for AudioScience soundcard ");
103 /* identify driver */
104 #ifdef KERNEL_ALSA_BUILD
105 static char *build_info = "Built using headers from kernel source";
106 module_param(build_info, charp, S_IRUGO);
107 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
109 static char *build_info = "Built within ALSA source";
110 module_param(build_info, charp, S_IRUGO);
111 MODULE_PARM_DESC(build_info, "built within ALSA source");
114 /* set to 1 to dump every control from adapter to log */
115 static const int mixer_dump;
117 #define DEFAULT_SAMPLERATE 44100
118 static int adapter_fs = DEFAULT_SAMPLERATE;
121 #define PERIODS_MIN 2
122 #define PERIOD_BYTES_MIN 2048
123 #define BUFFER_BYTES_MAX (512 * 1024)
125 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
136 struct clk_source s[MAX_CLOCKSOURCES];
140 struct snd_card_asihpi {
141 struct snd_card *card;
154 u16 support_grouping;
156 u16 update_interval_frames;
161 /* Per stream data */
162 struct snd_card_asihpi_pcm {
163 struct timer_list timer;
164 unsigned int respawn_timer;
165 unsigned int hpi_buffer_attached;
166 unsigned int buffer_bytes;
167 unsigned int period_bytes;
168 unsigned int bytes_per_sec;
169 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
170 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
171 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
172 unsigned int drained_count;
173 struct snd_pcm_substream *substream;
175 struct hpi_format format;
178 /* universal stream verbs work with out or in stream handles */
180 /* Functions to allow driver to give a buffer to HPI for busmastering */
182 static u16 hpi_stream_host_buffer_attach(
183 u32 h_stream, /* handle to outstream. */
184 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
188 struct hpi_message hm;
189 struct hpi_response hr;
190 unsigned int obj = hpi_handle_object(h_stream);
193 return HPI_ERROR_INVALID_OBJ;
194 hpi_init_message_response(&hm, &hr, obj,
195 obj == HPI_OBJ_OSTREAM ?
196 HPI_OSTREAM_HOSTBUFFER_ALLOC :
197 HPI_ISTREAM_HOSTBUFFER_ALLOC);
199 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
202 hm.u.d.u.buffer.buffer_size = size_in_bytes;
203 hm.u.d.u.buffer.pci_address = pci_address;
204 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
205 hpi_send_recv(&hm, &hr);
209 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
211 struct hpi_message hm;
212 struct hpi_response hr;
213 unsigned int obj = hpi_handle_object(h_stream);
216 return HPI_ERROR_INVALID_OBJ;
218 hpi_init_message_response(&hm, &hr, obj,
219 obj == HPI_OBJ_OSTREAM ?
220 HPI_OSTREAM_HOSTBUFFER_FREE :
221 HPI_ISTREAM_HOSTBUFFER_FREE);
223 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
225 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
226 hpi_send_recv(&hm, &hr);
230 static inline u16 hpi_stream_start(u32 h_stream)
232 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
233 return hpi_outstream_start(h_stream);
235 return hpi_instream_start(h_stream);
238 static inline u16 hpi_stream_stop(u32 h_stream)
240 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
241 return hpi_outstream_stop(h_stream);
243 return hpi_instream_stop(h_stream);
246 static inline u16 hpi_stream_get_info_ex(
250 u32 *pdata_in_buffer,
256 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
257 e = hpi_outstream_get_info_ex(h_stream, pw_state,
258 pbuffer_size, pdata_in_buffer,
259 psample_count, pauxiliary_data);
261 e = hpi_instream_get_info_ex(h_stream, pw_state,
262 pbuffer_size, pdata_in_buffer,
263 psample_count, pauxiliary_data);
267 static inline u16 hpi_stream_group_add(
271 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
272 return hpi_outstream_group_add(h_master, h_stream);
274 return hpi_instream_group_add(h_master, h_stream);
277 static inline u16 hpi_stream_group_reset(u32 h_stream)
279 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
280 return hpi_outstream_group_reset(h_stream);
282 return hpi_instream_group_reset(h_stream);
285 static inline u16 hpi_stream_group_get_map(
286 u32 h_stream, u32 *mo, u32 *mi)
288 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
289 return hpi_outstream_group_get_map(h_stream, mo, mi);
291 return hpi_instream_group_get_map(h_stream, mo, mi);
294 static u16 handle_error(u16 err, int line, char *filename)
298 "in file %s, line %d: HPI error %d\n",
299 filename, line, err);
303 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
305 /***************************** GENERAL PCM ****************/
307 static void print_hwparams(struct snd_pcm_substream *substream,
308 struct snd_pcm_hw_params *p)
311 snd_pcm_debug_name(substream, name, sizeof(name));
312 snd_printd("%s HWPARAMS\n", name);
313 snd_printd(" samplerate %d Hz\n", params_rate(p));
314 snd_printd(" channels %d\n", params_channels(p));
315 snd_printd(" format %d\n", params_format(p));
316 snd_printd(" subformat %d\n", params_subformat(p));
317 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
318 snd_printd(" period %d B\n", params_period_bytes(p));
319 snd_printd(" access %d\n", params_access(p));
320 snd_printd(" period_size %d\n", params_period_size(p));
321 snd_printd(" periods %d\n", params_periods(p));
322 snd_printd(" buffer_size %d\n", params_buffer_size(p));
323 snd_printd(" %d B/s\n", params_rate(p) *
325 snd_pcm_format_width(params_format(p)) / 8);
329 static snd_pcm_format_t hpi_to_alsa_formats[] = {
331 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
332 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
333 -1, /* HPI_FORMAT_MPEG_L1 3 */
334 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
335 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
336 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
337 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
338 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
339 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
340 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
341 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
342 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
343 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
344 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
346 /* ALSA can't handle 3 byte sample size together with power-of-2
347 * constraint on buffer_bytes, so disable this format
351 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
356 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
361 for (format = HPI_FORMAT_PCM8_UNSIGNED;
362 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
363 if (hpi_to_alsa_formats[format] == alsa_format) {
364 *hpi_format = format;
369 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
375 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
376 struct snd_pcm_hardware *pcmhw)
382 unsigned int rate_min = 200000;
383 unsigned int rate_max = 0;
384 unsigned int rates = 0;
386 if (asihpi->support_mrx) {
387 rates |= SNDRV_PCM_RATE_CONTINUOUS;
388 rates |= SNDRV_PCM_RATE_8000_96000;
392 /* on cards without SRC,
393 valid rates are determined by sampleclock */
394 err = hpi_mixer_get_control(asihpi->h_mixer,
395 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
396 HPI_CONTROL_SAMPLECLOCK, &h_control);
399 "No local sampleclock, err %d\n", err);
402 for (idx = -1; idx < 100; idx++) {
404 if (hpi_sample_clock_get_sample_rate(h_control,
407 } else if (hpi_sample_clock_query_local_rate(h_control,
408 idx, &sample_rate)) {
412 rate_min = min(rate_min, sample_rate);
413 rate_max = max(rate_max, sample_rate);
415 switch (sample_rate) {
417 rates |= SNDRV_PCM_RATE_5512;
420 rates |= SNDRV_PCM_RATE_8000;
423 rates |= SNDRV_PCM_RATE_11025;
426 rates |= SNDRV_PCM_RATE_16000;
429 rates |= SNDRV_PCM_RATE_22050;
432 rates |= SNDRV_PCM_RATE_32000;
435 rates |= SNDRV_PCM_RATE_44100;
438 rates |= SNDRV_PCM_RATE_48000;
441 rates |= SNDRV_PCM_RATE_64000;
444 rates |= SNDRV_PCM_RATE_88200;
447 rates |= SNDRV_PCM_RATE_96000;
450 rates |= SNDRV_PCM_RATE_176400;
453 rates |= SNDRV_PCM_RATE_192000;
455 default: /* some other rate */
456 rates |= SNDRV_PCM_RATE_KNOT;
461 pcmhw->rates = rates;
462 pcmhw->rate_min = rate_min;
463 pcmhw->rate_max = rate_max;
466 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
467 struct snd_pcm_hw_params *params)
469 struct snd_pcm_runtime *runtime = substream->runtime;
470 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
471 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
475 unsigned int bytes_per_sec;
477 print_hwparams(substream, params);
478 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
481 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
485 hpi_handle_error(hpi_format_create(&dpcm->format,
486 params_channels(params),
487 format, params_rate(params), 0, 0));
489 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
490 if (hpi_instream_reset(dpcm->h_stream) != 0)
493 if (hpi_instream_set_format(
494 dpcm->h_stream, &dpcm->format) != 0)
498 dpcm->hpi_buffer_attached = 0;
500 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
501 params_buffer_bytes(params), runtime->dma_addr);
504 "stream_host_buffer_attach succeeded %u %lu\n",
505 params_buffer_bytes(params),
506 (unsigned long)runtime->dma_addr);
508 snd_printd("stream_host_buffer_attach error %d\n",
513 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
514 &dpcm->hpi_buffer_attached,
517 snd_printdd("stream_host_buffer_attach status 0x%x\n",
518 dpcm->hpi_buffer_attached);
520 bytes_per_sec = params_rate(params) * params_channels(params);
521 width = snd_pcm_format_width(params_format(params));
522 bytes_per_sec *= width;
524 if (width < 0 || bytes_per_sec == 0)
527 dpcm->bytes_per_sec = bytes_per_sec;
528 dpcm->buffer_bytes = params_buffer_bytes(params);
529 dpcm->period_bytes = params_period_bytes(params);
535 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
537 struct snd_pcm_runtime *runtime = substream->runtime;
538 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
539 if (dpcm->hpi_buffer_attached)
540 hpi_stream_host_buffer_detach(dpcm->h_stream);
542 snd_pcm_lib_free_pages(substream);
546 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
548 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
552 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
555 struct snd_pcm_runtime *runtime = substream->runtime;
556 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
560 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
561 expiry = max(expiry, 1); /* don't let it be zero! */
562 dpcm->timer.expires = jiffies + expiry;
563 dpcm->respawn_timer = 1;
564 add_timer(&dpcm->timer);
567 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
569 struct snd_pcm_runtime *runtime = substream->runtime;
570 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
572 dpcm->respawn_timer = 0;
573 del_timer(&dpcm->timer);
576 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
579 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
580 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
581 struct snd_pcm_substream *s;
585 snd_pcm_debug_name(substream, name, sizeof(name));
586 snd_printdd("%s trigger\n", name);
589 case SNDRV_PCM_TRIGGER_START:
590 snd_pcm_group_for_each_entry(s, substream) {
591 struct snd_pcm_runtime *runtime = s->runtime;
592 struct snd_card_asihpi_pcm *ds = runtime->private_data;
594 if (snd_pcm_substream_chip(s) != card)
597 /* don't link Cap and Play */
598 if (substream->stream != s->stream)
601 ds->drained_count = 0;
602 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
603 /* How do I know how much valid data is present
604 * in buffer? Must be at least one period!
605 * Guessing 2 periods, but if
606 * buffer is bigger it may contain even more
609 unsigned int preload = ds->period_bytes * 1;
610 snd_printddd("%d preload x%x\n", s->number, preload);
611 hpi_handle_error(hpi_outstream_write_buf(
613 &runtime->dma_area[0],
616 ds->pcm_buf_host_rw_ofs = preload;
619 if (card->support_grouping) {
620 snd_printdd("%d group\n", s->number);
621 e = hpi_stream_group_add(
625 snd_pcm_trigger_done(s, substream);
633 snd_printdd("start\n");
634 /* start the master stream */
635 snd_card_asihpi_pcm_timer_start(substream);
636 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
638 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
641 case SNDRV_PCM_TRIGGER_STOP:
642 snd_card_asihpi_pcm_timer_stop(substream);
643 snd_pcm_group_for_each_entry(s, substream) {
644 if (snd_pcm_substream_chip(s) != card)
646 /* don't link Cap and Play */
647 if (substream->stream != s->stream)
650 /*? workaround linked streams don't
651 transition to SETUP 20070706*/
652 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
654 if (card->support_grouping) {
655 snd_printdd("%d group\n", s->number);
656 snd_pcm_trigger_done(s, substream);
660 snd_printdd("stop\n");
662 /* _prepare and _hwparams reset the stream */
663 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
664 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
666 hpi_outstream_reset(dpcm->h_stream));
668 if (card->support_grouping)
669 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
672 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
673 snd_printdd("pause release\n");
674 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
675 snd_card_asihpi_pcm_timer_start(substream);
677 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
678 snd_printdd("pause\n");
679 snd_card_asihpi_pcm_timer_stop(substream);
680 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
683 snd_printd(KERN_ERR "\tINVALID\n");
691 Without linking degenerates to getting single stream pos etc
692 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
695 pcm_buf_dma_ofs=get_buf_pos(s);
696 for_each_linked_stream(s) {
697 pcm_buf_dma_ofs=get_buf_pos(s);
698 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
699 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
701 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
702 for_each_linked_stream(s) {
703 s->pcm_buf_dma_ofs = min_buf_pos;
704 if (new_data > period_bytes) {
706 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
713 snd_pcm_period_elapsed(s);
718 /** Minimum of 2 modulo values. Works correctly when the difference between
719 * the values is less than half the modulus
721 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
722 unsigned long int modulus)
725 if (((a-b) % modulus) < (modulus/2))
733 /** Timer function, equivalent to interrupt service routine for cards
735 static void snd_card_asihpi_timer_function(unsigned long data)
737 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
738 struct snd_pcm_substream *substream = dpcm->substream;
739 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
740 struct snd_pcm_runtime *runtime;
741 struct snd_pcm_substream *s;
742 unsigned int newdata = 0;
743 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
744 unsigned int remdata, xfercount, next_jiffies;
748 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
751 snd_pcm_debug_name(substream, name, sizeof(name));
753 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
755 /* find minimum newdata and buffer pos in group */
756 snd_pcm_group_for_each_entry(s, substream) {
757 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
758 runtime = s->runtime;
760 if (snd_pcm_substream_chip(s) != card)
763 /* don't link Cap and Play */
764 if (substream->stream != s->stream)
767 hpi_handle_error(hpi_stream_get_info_ex(
768 ds->h_stream, &state,
769 &buffer_size, &bytes_avail,
770 &samples_played, &on_card_bytes));
772 /* number of bytes in on-card buffer */
773 runtime->delay = on_card_bytes;
776 on_card_bytes = bytes_avail;
778 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
779 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
780 if (state == HPI_STATE_STOPPED) {
781 if ((bytes_avail == 0) &&
782 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
783 hpi_handle_error(hpi_stream_start(ds->h_stream));
784 snd_printdd("P%d start\n", s->number);
785 ds->drained_count = 0;
787 } else if (state == HPI_STATE_DRAINED) {
788 snd_printd(KERN_WARNING "P%d drained\n",
791 if (ds->drained_count > 2) {
792 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
796 ds->drained_count = 0;
799 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
802 /* can't statically init min when wrap is involved */
803 min_buf_pos = pcm_buf_dma_ofs;
804 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
808 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
810 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
814 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
815 (unsigned long)frames_to_bytes(runtime,
816 runtime->status->hw_ptr),
817 (unsigned long)frames_to_bytes(runtime,
818 runtime->control->appl_ptr));
820 snd_printdd("%d S=%d, "
821 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
822 "aux=0x%04X space=0x%04X\n",
824 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
826 (int)on_card_bytes, buffer_size-bytes_avail);
829 pcm_buf_dma_ofs = min_buf_pos;
831 remdata = newdata % dpcm->period_bytes;
832 xfercount = newdata - remdata; /* a multiple of period_bytes */
833 /* come back when on_card_bytes has decreased enough to allow
834 write to happen, or when data has been consumed to make another
837 if (xfercount && (on_card_bytes > dpcm->period_bytes))
838 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
840 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
842 next_jiffies = max(next_jiffies, 1U);
843 dpcm->timer.expires = jiffies + next_jiffies;
844 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
845 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
847 snd_pcm_group_for_each_entry(s, substream) {
848 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
850 /* don't link Cap and Play */
851 if (substream->stream != s->stream)
854 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
857 /* Limit use of on card fifo for playback */
858 ((on_card_bytes <= ds->period_bytes) ||
859 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
863 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
864 unsigned int xfer1, xfer2;
865 char *pd = &s->runtime->dma_area[buf_ofs];
867 if (card->can_dma) { /* buffer wrap is handled at lower level */
871 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
872 xfer2 = xfercount - xfer1;
875 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
876 snd_printddd("P%d write1 0x%04X 0x%04X\n",
877 s->number, xfer1, buf_ofs);
879 hpi_outstream_write_buf(
880 ds->h_stream, pd, xfer1,
884 pd = s->runtime->dma_area;
886 snd_printddd("P%d write2 0x%04X 0x%04X\n",
888 xfercount - xfer1, buf_ofs);
890 hpi_outstream_write_buf(
896 snd_printddd("C%d read1 0x%04x\n",
899 hpi_instream_read_buf(
903 pd = s->runtime->dma_area;
904 snd_printddd("C%d read2 0x%04x\n",
907 hpi_instream_read_buf(
912 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
913 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
914 snd_pcm_period_elapsed(s);
918 if (dpcm->respawn_timer)
919 add_timer(&dpcm->timer);
922 /***************************** PLAYBACK OPS ****************/
923 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
924 unsigned int cmd, void *arg)
926 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
927 return snd_pcm_lib_ioctl(substream, cmd, arg);
930 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
933 struct snd_pcm_runtime *runtime = substream->runtime;
934 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
936 snd_printdd("P%d prepare\n", substream->number);
938 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
939 dpcm->pcm_buf_host_rw_ofs = 0;
940 dpcm->pcm_buf_dma_ofs = 0;
941 dpcm->pcm_buf_elapsed_dma_ofs = 0;
945 static snd_pcm_uframes_t
946 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
948 struct snd_pcm_runtime *runtime = substream->runtime;
949 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
950 snd_pcm_uframes_t ptr;
952 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
953 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
957 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
959 struct snd_pcm_hardware *pcmhw)
961 struct hpi_format hpi_format;
965 u32 sample_rate = 48000;
967 /* on cards without SRC, must query at valid rate,
968 * maybe set by external sync
970 err = hpi_mixer_get_control(asihpi->h_mixer,
971 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
972 HPI_CONTROL_SAMPLECLOCK, &h_control);
975 err = hpi_sample_clock_get_sample_rate(h_control,
978 for (format = HPI_FORMAT_PCM8_UNSIGNED;
979 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
980 err = hpi_format_create(&hpi_format,
981 2, format, sample_rate, 128000, 0);
983 err = hpi_outstream_query_format(h_stream,
985 if (!err && (hpi_to_alsa_formats[format] != -1))
987 (1ULL << hpi_to_alsa_formats[format]);
991 static struct snd_pcm_hardware snd_card_asihpi_playback = {
994 .buffer_bytes_max = BUFFER_BYTES_MAX,
995 .period_bytes_min = PERIOD_BYTES_MIN,
996 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
997 .periods_min = PERIODS_MIN,
998 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1002 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1004 struct snd_pcm_runtime *runtime = substream->runtime;
1005 struct snd_card_asihpi_pcm *dpcm;
1006 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1009 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1014 hpi_outstream_open(card->adapter_index,
1015 substream->number, &dpcm->h_stream);
1016 hpi_handle_error(err);
1019 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1024 /*? also check ASI5000 samplerate source
1025 If external, only support external rate.
1026 If internal and other stream playing, can't switch
1029 init_timer(&dpcm->timer);
1030 dpcm->timer.data = (unsigned long) dpcm;
1031 dpcm->timer.function = snd_card_asihpi_timer_function;
1032 dpcm->substream = substream;
1033 runtime->private_data = dpcm;
1034 runtime->private_free = snd_card_asihpi_runtime_free;
1036 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1037 /*?snd_card_asihpi_playback.period_bytes_min =
1038 card->out_max_chans * 4096; */
1040 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1041 &snd_card_asihpi_playback);
1043 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1045 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1046 SNDRV_PCM_INFO_DOUBLE |
1047 SNDRV_PCM_INFO_BATCH |
1048 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1049 SNDRV_PCM_INFO_PAUSE |
1050 SNDRV_PCM_INFO_MMAP |
1051 SNDRV_PCM_INFO_MMAP_VALID;
1053 if (card->support_grouping)
1054 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1056 /* struct is copied, so can create initializer dynamically */
1057 runtime->hw = snd_card_asihpi_playback;
1060 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1061 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1065 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1066 card->update_interval_frames);
1068 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1069 card->update_interval_frames * 2, UINT_MAX);
1071 snd_pcm_set_sync(substream);
1073 snd_printdd("playback open\n");
1078 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1080 struct snd_pcm_runtime *runtime = substream->runtime;
1081 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1083 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1084 snd_printdd("playback close\n");
1089 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1090 .open = snd_card_asihpi_playback_open,
1091 .close = snd_card_asihpi_playback_close,
1092 .ioctl = snd_card_asihpi_playback_ioctl,
1093 .hw_params = snd_card_asihpi_pcm_hw_params,
1094 .hw_free = snd_card_asihpi_hw_free,
1095 .prepare = snd_card_asihpi_playback_prepare,
1096 .trigger = snd_card_asihpi_trigger,
1097 .pointer = snd_card_asihpi_playback_pointer,
1100 /***************************** CAPTURE OPS ****************/
1101 static snd_pcm_uframes_t
1102 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1104 struct snd_pcm_runtime *runtime = substream->runtime;
1105 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1107 snd_printddd("capture pointer %d=%d\n",
1108 substream->number, dpcm->pcm_buf_dma_ofs);
1109 /* NOTE Unlike playback can't use actual samples_played
1110 for the capture position, because those samples aren't yet in
1111 the local buffer available for reading.
1113 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1116 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1117 unsigned int cmd, void *arg)
1119 return snd_pcm_lib_ioctl(substream, cmd, arg);
1122 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1127 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1128 dpcm->pcm_buf_host_rw_ofs = 0;
1129 dpcm->pcm_buf_dma_ofs = 0;
1130 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1132 snd_printdd("Capture Prepare %d\n", substream->number);
1138 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1140 struct snd_pcm_hardware *pcmhw)
1142 struct hpi_format hpi_format;
1146 u32 sample_rate = 48000;
1148 /* on cards without SRC, must query at valid rate,
1149 maybe set by external sync */
1150 err = hpi_mixer_get_control(asihpi->h_mixer,
1151 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1152 HPI_CONTROL_SAMPLECLOCK, &h_control);
1155 err = hpi_sample_clock_get_sample_rate(h_control,
1158 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1159 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1161 err = hpi_format_create(&hpi_format, 2, format,
1162 sample_rate, 128000, 0);
1164 err = hpi_instream_query_format(h_stream,
1168 (1ULL << hpi_to_alsa_formats[format]);
1173 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1176 .buffer_bytes_max = BUFFER_BYTES_MAX,
1177 .period_bytes_min = PERIOD_BYTES_MIN,
1178 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1179 .periods_min = PERIODS_MIN,
1180 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1184 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1186 struct snd_pcm_runtime *runtime = substream->runtime;
1187 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1188 struct snd_card_asihpi_pcm *dpcm;
1191 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1195 snd_printdd("capture open adapter %d stream %d\n",
1196 card->adapter_index, substream->number);
1198 err = hpi_handle_error(
1199 hpi_instream_open(card->adapter_index,
1200 substream->number, &dpcm->h_stream));
1203 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1209 init_timer(&dpcm->timer);
1210 dpcm->timer.data = (unsigned long) dpcm;
1211 dpcm->timer.function = snd_card_asihpi_timer_function;
1212 dpcm->substream = substream;
1213 runtime->private_data = dpcm;
1214 runtime->private_free = snd_card_asihpi_runtime_free;
1216 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1217 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1218 &snd_card_asihpi_capture);
1219 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1220 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1221 SNDRV_PCM_INFO_MMAP |
1222 SNDRV_PCM_INFO_MMAP_VALID;
1224 if (card->support_grouping)
1225 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1227 runtime->hw = snd_card_asihpi_capture;
1230 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1231 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1235 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1236 card->update_interval_frames);
1237 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1238 card->update_interval_frames * 2, UINT_MAX);
1240 snd_pcm_set_sync(substream);
1245 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1247 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1249 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1253 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1254 .open = snd_card_asihpi_capture_open,
1255 .close = snd_card_asihpi_capture_close,
1256 .ioctl = snd_card_asihpi_capture_ioctl,
1257 .hw_params = snd_card_asihpi_pcm_hw_params,
1258 .hw_free = snd_card_asihpi_hw_free,
1259 .prepare = snd_card_asihpi_capture_prepare,
1260 .trigger = snd_card_asihpi_trigger,
1261 .pointer = snd_card_asihpi_capture_pointer,
1264 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1265 int device, int substreams)
1267 struct snd_pcm *pcm;
1270 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1271 asihpi->num_outstreams, asihpi->num_instreams,
1275 /* pointer to ops struct is stored, dont change ops afterwards! */
1276 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1277 &snd_card_asihpi_playback_mmap_ops);
1278 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1279 &snd_card_asihpi_capture_mmap_ops);
1281 pcm->private_data = asihpi;
1282 pcm->info_flags = 0;
1283 strcpy(pcm->name, "Asihpi PCM");
1285 /*? do we want to emulate MMAP for non-BBM cards?
1286 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1287 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1288 snd_dma_pci_data(asihpi->pci),
1289 64*1024, BUFFER_BYTES_MAX);
1294 /***************************** MIXER CONTROLS ****************/
1295 struct hpi_control {
1303 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1306 static const char * const asihpi_tuner_band_names[] = {
1319 compile_time_assert(
1320 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1321 (HPI_TUNER_BAND_LAST+1)),
1322 assert_tuner_band_names_size);
1324 static const char * const asihpi_src_names[] = {
1341 compile_time_assert(
1342 (ARRAY_SIZE(asihpi_src_names) ==
1343 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1344 assert_src_names_size);
1346 static const char * const asihpi_dst_names[] = {
1359 compile_time_assert(
1360 (ARRAY_SIZE(asihpi_dst_names) ==
1361 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1362 assert_dst_names_size);
1364 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1365 struct snd_card_asihpi *asihpi)
1369 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1372 else if (mixer_dump)
1373 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1378 /* Convert HPI control name and location into ALSA control name */
1379 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1380 struct hpi_control *hpi_ctl,
1384 memset(snd_control, 0, sizeof(*snd_control));
1385 snd_control->name = hpi_ctl->name;
1386 snd_control->private_value = hpi_ctl->h_control;
1387 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1388 snd_control->index = 0;
1390 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1391 dir = ""; /* clock is neither capture nor playback */
1392 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1393 dir = "Capture "; /* On or towards a PCM capture destination*/
1394 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1395 (!hpi_ctl->dst_node_type))
1396 dir = "Capture "; /* On a source node that is not PCM playback */
1397 else if (hpi_ctl->src_node_type &&
1398 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1399 (hpi_ctl->dst_node_type))
1400 dir = "Monitor Playback "; /* Between an input and an output */
1402 dir = "Playback "; /* PCM Playback source, or output node */
1404 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1405 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1406 asihpi_src_names[hpi_ctl->src_node_type],
1407 hpi_ctl->src_node_index,
1408 asihpi_dst_names[hpi_ctl->dst_node_type],
1409 hpi_ctl->dst_node_index,
1411 else if (hpi_ctl->dst_node_type) {
1412 sprintf(hpi_ctl->name, "%s %d %s%s",
1413 asihpi_dst_names[hpi_ctl->dst_node_type],
1414 hpi_ctl->dst_node_index,
1417 sprintf(hpi_ctl->name, "%s %d %s%s",
1418 asihpi_src_names[hpi_ctl->src_node_type],
1419 hpi_ctl->src_node_index,
1422 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1423 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1426 /*------------------------------------------------------------
1428 ------------------------------------------------------------*/
1429 #define VOL_STEP_mB 1
1430 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1431 struct snd_ctl_elem_info *uinfo)
1433 u32 h_control = kcontrol->private_value;
1435 /* native gains are in millibels */
1440 err = hpi_volume_query_range(h_control,
1441 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1444 min_gain_mB = -10000;
1445 step_gain_mB = VOL_STEP_mB;
1448 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1450 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1451 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1452 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1456 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1457 struct snd_ctl_elem_value *ucontrol)
1459 u32 h_control = kcontrol->private_value;
1460 short an_gain_mB[HPI_MAX_CHANNELS];
1462 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1463 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1464 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1469 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1470 struct snd_ctl_elem_value *ucontrol)
1473 u32 h_control = kcontrol->private_value;
1474 short an_gain_mB[HPI_MAX_CHANNELS];
1477 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1479 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1480 /* change = asihpi->mixer_volume[addr][0] != left ||
1481 asihpi->mixer_volume[addr][1] != right;
1484 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1488 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1490 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1491 struct hpi_control *hpi_ctl)
1493 struct snd_card *card = asihpi->card;
1494 struct snd_kcontrol_new snd_control;
1496 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1497 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1498 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1499 snd_control.info = snd_asihpi_volume_info;
1500 snd_control.get = snd_asihpi_volume_get;
1501 snd_control.put = snd_asihpi_volume_put;
1502 snd_control.tlv.p = db_scale_100;
1504 return ctl_add(card, &snd_control, asihpi);
1507 /*------------------------------------------------------------
1509 ------------------------------------------------------------*/
1510 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1511 struct snd_ctl_elem_info *uinfo)
1513 u32 h_control = kcontrol->private_value;
1520 hpi_level_query_range(h_control, &min_gain_mB,
1521 &max_gain_mB, &step_gain_mB);
1524 min_gain_mB = -1000;
1528 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1530 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1531 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1532 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1536 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
1539 u32 h_control = kcontrol->private_value;
1540 short an_gain_mB[HPI_MAX_CHANNELS];
1542 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1543 ucontrol->value.integer.value[0] =
1544 an_gain_mB[0] / HPI_UNITS_PER_dB;
1545 ucontrol->value.integer.value[1] =
1546 an_gain_mB[1] / HPI_UNITS_PER_dB;
1551 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1552 struct snd_ctl_elem_value *ucontrol)
1555 u32 h_control = kcontrol->private_value;
1556 short an_gain_mB[HPI_MAX_CHANNELS];
1559 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1561 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1562 /* change = asihpi->mixer_level[addr][0] != left ||
1563 asihpi->mixer_level[addr][1] != right;
1566 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1570 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1572 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1573 struct hpi_control *hpi_ctl)
1575 struct snd_card *card = asihpi->card;
1576 struct snd_kcontrol_new snd_control;
1578 /* can't use 'volume' cos some nodes have volume as well */
1579 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1580 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1581 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1582 snd_control.info = snd_asihpi_level_info;
1583 snd_control.get = snd_asihpi_level_get;
1584 snd_control.put = snd_asihpi_level_put;
1585 snd_control.tlv.p = db_scale_level;
1587 return ctl_add(card, &snd_control, asihpi);
1590 /*------------------------------------------------------------
1592 ------------------------------------------------------------*/
1595 static const char * const asihpi_aesebu_format_names[] = {
1596 "N/A", "S/PDIF", "AES/EBU" };
1598 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1599 struct snd_ctl_elem_info *uinfo)
1601 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1603 uinfo->value.enumerated.items = 3;
1605 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1606 uinfo->value.enumerated.item =
1607 uinfo->value.enumerated.items - 1;
1609 strcpy(uinfo->value.enumerated.name,
1610 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1615 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_value *ucontrol,
1617 u16 (*func)(u32, u16 *))
1619 u32 h_control = kcontrol->private_value;
1622 err = func(h_control, &source);
1624 /* default to N/A */
1625 ucontrol->value.enumerated.item[0] = 0;
1626 /* return success but set the control to N/A */
1629 if (source == HPI_AESEBU_FORMAT_SPDIF)
1630 ucontrol->value.enumerated.item[0] = 1;
1631 if (source == HPI_AESEBU_FORMAT_AESEBU)
1632 ucontrol->value.enumerated.item[0] = 2;
1637 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol,
1639 u16 (*func)(u32, u16))
1641 u32 h_control = kcontrol->private_value;
1643 /* default to S/PDIF */
1644 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1646 if (ucontrol->value.enumerated.item[0] == 1)
1647 source = HPI_AESEBU_FORMAT_SPDIF;
1648 if (ucontrol->value.enumerated.item[0] == 2)
1649 source = HPI_AESEBU_FORMAT_AESEBU;
1651 if (func(h_control, source) != 0)
1657 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1658 struct snd_ctl_elem_value *ucontrol) {
1659 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1660 hpi_aesebu_receiver_get_format);
1663 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1664 struct snd_ctl_elem_value *ucontrol) {
1665 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1666 hpi_aesebu_receiver_set_format);
1669 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_info *uinfo)
1672 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1675 uinfo->value.integer.min = 0;
1676 uinfo->value.integer.max = 0X1F;
1677 uinfo->value.integer.step = 1;
1682 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_value *ucontrol) {
1685 u32 h_control = kcontrol->private_value;
1688 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1689 h_control, &status));
1690 ucontrol->value.integer.value[0] = status;
1694 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1695 struct hpi_control *hpi_ctl)
1697 struct snd_card *card = asihpi->card;
1698 struct snd_kcontrol_new snd_control;
1700 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1701 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1702 snd_control.info = snd_asihpi_aesebu_format_info;
1703 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1704 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1707 if (ctl_add(card, &snd_control, asihpi) < 0)
1710 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1711 snd_control.access =
1712 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1713 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1714 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1716 return ctl_add(card, &snd_control, asihpi);
1719 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1720 struct snd_ctl_elem_value *ucontrol) {
1721 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1722 hpi_aesebu_transmitter_get_format);
1725 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1726 struct snd_ctl_elem_value *ucontrol) {
1727 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1728 hpi_aesebu_transmitter_set_format);
1732 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1733 struct hpi_control *hpi_ctl)
1735 struct snd_card *card = asihpi->card;
1736 struct snd_kcontrol_new snd_control;
1738 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1739 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1740 snd_control.info = snd_asihpi_aesebu_format_info;
1741 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1742 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1744 return ctl_add(card, &snd_control, asihpi);
1747 /*------------------------------------------------------------
1749 ------------------------------------------------------------*/
1753 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_info *uinfo)
1756 u32 h_control = kcontrol->private_value;
1761 for (idx = 0; idx < 3; idx++) {
1762 err = hpi_tuner_query_gain(h_control,
1763 idx, &gain_range[idx]);
1768 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1770 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1771 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1772 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1776 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_value *ucontrol)
1780 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1782 u32 h_control = kcontrol->private_value;
1785 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1786 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1791 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1792 struct snd_ctl_elem_value *ucontrol)
1795 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1797 u32 h_control = kcontrol->private_value;
1800 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1801 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1808 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1809 u16 *band_list, u32 len) {
1810 u32 h_control = kcontrol->private_value;
1814 for (i = 0; i < len; i++) {
1815 err = hpi_tuner_query_band(
1816 h_control, i, &band_list[i]);
1821 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1827 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1828 struct snd_ctl_elem_info *uinfo)
1830 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1833 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1834 HPI_TUNER_BAND_LAST);
1839 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1841 uinfo->value.enumerated.items = num_bands;
1843 if (num_bands > 0) {
1844 if (uinfo->value.enumerated.item >=
1845 uinfo->value.enumerated.items)
1846 uinfo->value.enumerated.item =
1847 uinfo->value.enumerated.items - 1;
1849 strcpy(uinfo->value.enumerated.name,
1850 asihpi_tuner_band_names[
1851 tuner_bands[uinfo->value.enumerated.item]]);
1857 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1858 struct snd_ctl_elem_value *ucontrol)
1860 u32 h_control = kcontrol->private_value;
1862 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1865 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1868 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1869 HPI_TUNER_BAND_LAST);
1871 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1873 ucontrol->value.enumerated.item[0] = -1;
1874 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1875 if (tuner_bands[idx] == band) {
1876 ucontrol->value.enumerated.item[0] = idx;
1883 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1884 struct snd_ctl_elem_value *ucontrol)
1887 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1889 u32 h_control = kcontrol->private_value;
1891 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1894 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1895 HPI_TUNER_BAND_LAST);
1897 band = tuner_bands[ucontrol->value.enumerated.item[0]];
1898 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1905 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1906 struct snd_ctl_elem_info *uinfo)
1908 u32 h_control = kcontrol->private_value;
1910 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1911 u16 num_bands = 0, band_iter, idx;
1912 u32 freq_range[3], temp_freq_range[3];
1914 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1915 HPI_TUNER_BAND_LAST);
1917 freq_range[0] = INT_MAX;
1919 freq_range[2] = INT_MAX;
1921 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1922 for (idx = 0; idx < 3; idx++) {
1923 err = hpi_tuner_query_frequency(h_control,
1924 idx, tuner_bands[band_iter],
1925 &temp_freq_range[idx]);
1930 /* skip band with bogus stepping */
1931 if (temp_freq_range[2] <= 0)
1934 if (temp_freq_range[0] < freq_range[0])
1935 freq_range[0] = temp_freq_range[0];
1936 if (temp_freq_range[1] > freq_range[1])
1937 freq_range[1] = temp_freq_range[1];
1938 if (temp_freq_range[2] < freq_range[2])
1939 freq_range[2] = temp_freq_range[2];
1942 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1944 uinfo->value.integer.min = ((int)freq_range[0]);
1945 uinfo->value.integer.max = ((int)freq_range[1]);
1946 uinfo->value.integer.step = ((int)freq_range[2]);
1950 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1951 struct snd_ctl_elem_value *ucontrol)
1953 u32 h_control = kcontrol->private_value;
1956 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1957 ucontrol->value.integer.value[0] = freq;
1962 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1963 struct snd_ctl_elem_value *ucontrol)
1965 u32 h_control = kcontrol->private_value;
1968 freq = ucontrol->value.integer.value[0];
1969 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1974 /* Tuner control group initializer */
1975 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1976 struct hpi_control *hpi_ctl)
1978 struct snd_card *card = asihpi->card;
1979 struct snd_kcontrol_new snd_control;
1981 snd_control.private_value = hpi_ctl->h_control;
1982 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1984 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
1985 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
1986 snd_control.info = snd_asihpi_tuner_gain_info;
1987 snd_control.get = snd_asihpi_tuner_gain_get;
1988 snd_control.put = snd_asihpi_tuner_gain_put;
1990 if (ctl_add(card, &snd_control, asihpi) < 0)
1994 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
1995 snd_control.info = snd_asihpi_tuner_band_info;
1996 snd_control.get = snd_asihpi_tuner_band_get;
1997 snd_control.put = snd_asihpi_tuner_band_put;
1999 if (ctl_add(card, &snd_control, asihpi) < 0)
2002 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
2003 snd_control.info = snd_asihpi_tuner_freq_info;
2004 snd_control.get = snd_asihpi_tuner_freq_get;
2005 snd_control.put = snd_asihpi_tuner_freq_put;
2007 return ctl_add(card, &snd_control, asihpi);
2010 /*------------------------------------------------------------
2012 ------------------------------------------------------------*/
2013 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2014 struct snd_ctl_elem_info *uinfo)
2016 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2017 uinfo->count = HPI_MAX_CHANNELS;
2018 uinfo->value.integer.min = 0;
2019 uinfo->value.integer.max = 0x7FFFFFFF;
2023 /* linear values for 10dB steps */
2024 static int log2lin[] = {
2025 0x7FFFFFFF, /* 0dB */
2031 2147484, /* -60dB */
2046 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2047 struct snd_ctl_elem_value *ucontrol)
2049 u32 h_control = kcontrol->private_value;
2050 short an_gain_mB[HPI_MAX_CHANNELS], i;
2053 err = hpi_meter_get_peak(h_control, an_gain_mB);
2055 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2057 ucontrol->value.integer.value[i] = 0;
2058 } else if (an_gain_mB[i] >= 0) {
2059 ucontrol->value.integer.value[i] =
2060 an_gain_mB[i] << 16;
2062 /* -ve is log value in millibels < -60dB,
2063 * convert to (roughly!) linear,
2065 ucontrol->value.integer.value[i] =
2066 log2lin[an_gain_mB[i] / -1000];
2072 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2073 struct hpi_control *hpi_ctl, int subidx)
2075 struct snd_card *card = asihpi->card;
2076 struct snd_kcontrol_new snd_control;
2078 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2079 snd_control.access =
2080 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2081 snd_control.info = snd_asihpi_meter_info;
2082 snd_control.get = snd_asihpi_meter_get;
2084 snd_control.index = subidx;
2086 return ctl_add(card, &snd_control, asihpi);
2089 /*------------------------------------------------------------
2090 Multiplexer controls
2091 ------------------------------------------------------------*/
2092 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2094 u32 h_control = snd_control->private_value;
2095 struct hpi_control hpi_ctl;
2097 for (s = 0; s < 32; s++) {
2098 err = hpi_multiplexer_query_source(h_control, s,
2109 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2110 struct snd_ctl_elem_info *uinfo)
2113 u16 src_node_type, src_node_index;
2114 u32 h_control = kcontrol->private_value;
2116 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2118 uinfo->value.enumerated.items =
2119 snd_card_asihpi_mux_count_sources(kcontrol);
2121 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2122 uinfo->value.enumerated.item =
2123 uinfo->value.enumerated.items - 1;
2126 hpi_multiplexer_query_source(h_control,
2127 uinfo->value.enumerated.item,
2128 &src_node_type, &src_node_index);
2130 sprintf(uinfo->value.enumerated.name, "%s %d",
2131 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2136 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
2139 u32 h_control = kcontrol->private_value;
2140 u16 source_type, source_index;
2141 u16 src_node_type, src_node_index;
2144 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2145 &source_type, &source_index));
2146 /* Should cache this search result! */
2147 for (s = 0; s < 256; s++) {
2148 if (hpi_multiplexer_query_source(h_control, s,
2149 &src_node_type, &src_node_index))
2152 if ((source_type == src_node_type)
2153 && (source_index == src_node_index)) {
2154 ucontrol->value.enumerated.item[0] = s;
2158 snd_printd(KERN_WARNING
2159 "Control %x failed to match mux source %hu %hu\n",
2160 h_control, source_type, source_index);
2161 ucontrol->value.enumerated.item[0] = 0;
2165 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2166 struct snd_ctl_elem_value *ucontrol)
2169 u32 h_control = kcontrol->private_value;
2170 u16 source_type, source_index;
2175 e = hpi_multiplexer_query_source(h_control,
2176 ucontrol->value.enumerated.item[0],
2177 &source_type, &source_index);
2180 hpi_multiplexer_set_source(h_control,
2181 source_type, source_index));
2186 static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2187 struct hpi_control *hpi_ctl)
2189 struct snd_card *card = asihpi->card;
2190 struct snd_kcontrol_new snd_control;
2192 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2193 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2194 snd_control.info = snd_asihpi_mux_info;
2195 snd_control.get = snd_asihpi_mux_get;
2196 snd_control.put = snd_asihpi_mux_put;
2198 return ctl_add(card, &snd_control, asihpi);
2202 /*------------------------------------------------------------
2203 Channel mode controls
2204 ------------------------------------------------------------*/
2205 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_info *uinfo)
2208 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2211 "From Left", "From Right",
2212 "To Left", "To Right"
2215 u32 h_control = kcontrol->private_value;
2219 int valid_modes = 0;
2221 /* HPI channel mode values can be from 1 to 6
2222 Some adapters only support a contiguous subset
2224 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2225 if (!hpi_channel_mode_query_mode(
2226 h_control, i, &mode)) {
2227 mode_map[valid_modes] = mode;
2231 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2233 uinfo->value.enumerated.items = valid_modes;
2235 if (uinfo->value.enumerated.item >= valid_modes)
2236 uinfo->value.enumerated.item = valid_modes - 1;
2238 strcpy(uinfo->value.enumerated.name,
2239 mode_names[mode_map[uinfo->value.enumerated.item]]);
2244 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2245 struct snd_ctl_elem_value *ucontrol)
2247 u32 h_control = kcontrol->private_value;
2250 if (hpi_channel_mode_get(h_control, &mode))
2253 ucontrol->value.enumerated.item[0] = mode - 1;
2258 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2259 struct snd_ctl_elem_value *ucontrol)
2262 u32 h_control = kcontrol->private_value;
2266 hpi_handle_error(hpi_channel_mode_set(h_control,
2267 ucontrol->value.enumerated.item[0] + 1));
2272 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2273 struct hpi_control *hpi_ctl)
2275 struct snd_card *card = asihpi->card;
2276 struct snd_kcontrol_new snd_control;
2278 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2279 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2280 snd_control.info = snd_asihpi_cmode_info;
2281 snd_control.get = snd_asihpi_cmode_get;
2282 snd_control.put = snd_asihpi_cmode_put;
2284 return ctl_add(card, &snd_control, asihpi);
2287 /*------------------------------------------------------------
2288 Sampleclock source controls
2289 ------------------------------------------------------------*/
2290 static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2291 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2292 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2294 "Digital2", "Digital3", "Digital4", "Digital5",
2295 "Digital6", "Digital7", "Digital8"};
2297 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2298 struct snd_ctl_elem_info *uinfo)
2300 struct snd_card_asihpi *asihpi =
2301 (struct snd_card_asihpi *)(kcontrol->private_data);
2302 struct clk_cache *clkcache = &asihpi->cc;
2303 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2305 uinfo->value.enumerated.items = clkcache->count;
2307 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2308 uinfo->value.enumerated.item =
2309 uinfo->value.enumerated.items - 1;
2311 strcpy(uinfo->value.enumerated.name,
2312 clkcache->s[uinfo->value.enumerated.item].name);
2316 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_value *ucontrol)
2319 struct snd_card_asihpi *asihpi =
2320 (struct snd_card_asihpi *)(kcontrol->private_data);
2321 struct clk_cache *clkcache = &asihpi->cc;
2322 u32 h_control = kcontrol->private_value;
2323 u16 source, srcindex = 0;
2326 ucontrol->value.enumerated.item[0] = 0;
2327 if (hpi_sample_clock_get_source(h_control, &source))
2330 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2331 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2334 for (i = 0; i < clkcache->count; i++)
2335 if ((clkcache->s[i].source == source) &&
2336 (clkcache->s[i].index == srcindex))
2339 ucontrol->value.enumerated.item[0] = i;
2344 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2345 struct snd_ctl_elem_value *ucontrol)
2347 struct snd_card_asihpi *asihpi =
2348 (struct snd_card_asihpi *)(kcontrol->private_data);
2349 struct clk_cache *clkcache = &asihpi->cc;
2351 u32 h_control = kcontrol->private_value;
2354 item = ucontrol->value.enumerated.item[0];
2355 if (item >= clkcache->count)
2356 item = clkcache->count-1;
2358 hpi_handle_error(hpi_sample_clock_set_source(
2359 h_control, clkcache->s[item].source));
2361 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2362 hpi_handle_error(hpi_sample_clock_set_source_index(
2363 h_control, clkcache->s[item].index));
2367 /*------------------------------------------------------------
2369 ------------------------------------------------------------*/
2370 /* Need to change this to enumerated control with list of rates */
2371 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2372 struct snd_ctl_elem_info *uinfo)
2374 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2376 uinfo->value.integer.min = 8000;
2377 uinfo->value.integer.max = 192000;
2378 uinfo->value.integer.step = 100;
2383 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2384 struct snd_ctl_elem_value *ucontrol)
2386 u32 h_control = kcontrol->private_value;
2390 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2392 ucontrol->value.integer.value[0] = rate;
2394 ucontrol->value.integer.value[0] = 0;
2398 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2399 struct snd_ctl_elem_value *ucontrol)
2402 u32 h_control = kcontrol->private_value;
2404 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2405 asihpi->mixer_clkrate[addr][1] != right;
2408 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2409 ucontrol->value.integer.value[0]));
2413 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2414 struct snd_ctl_elem_info *uinfo)
2416 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2418 uinfo->value.integer.min = 8000;
2419 uinfo->value.integer.max = 192000;
2420 uinfo->value.integer.step = 100;
2425 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2426 struct snd_ctl_elem_value *ucontrol)
2428 u32 h_control = kcontrol->private_value;
2432 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2434 ucontrol->value.integer.value[0] = rate;
2436 ucontrol->value.integer.value[0] = 0;
2440 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2441 struct hpi_control *hpi_ctl)
2443 struct snd_card *card = asihpi->card;
2444 struct snd_kcontrol_new snd_control;
2446 struct clk_cache *clkcache = &asihpi->cc;
2447 u32 hSC = hpi_ctl->h_control;
2452 snd_control.private_value = hpi_ctl->h_control;
2454 clkcache->has_local = 0;
2456 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2457 if (hpi_sample_clock_query_source(hSC,
2460 clkcache->s[i].source = source;
2461 clkcache->s[i].index = 0;
2462 clkcache->s[i].name = sampleclock_sources[source];
2463 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2465 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2466 clkcache->has_local = 1;
2469 /* already will have picked up index 0 above */
2470 for (j = 1; j < 8; j++) {
2471 if (hpi_sample_clock_query_source_index(hSC,
2472 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2475 clkcache->s[i].source =
2476 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2477 clkcache->s[i].index = j;
2478 clkcache->s[i].name = sampleclock_sources[
2479 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2482 clkcache->count = i;
2484 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2485 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2486 snd_control.info = snd_asihpi_clksrc_info;
2487 snd_control.get = snd_asihpi_clksrc_get;
2488 snd_control.put = snd_asihpi_clksrc_put;
2489 if (ctl_add(card, &snd_control, asihpi) < 0)
2493 if (clkcache->has_local) {
2494 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2495 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2496 snd_control.info = snd_asihpi_clklocal_info;
2497 snd_control.get = snd_asihpi_clklocal_get;
2498 snd_control.put = snd_asihpi_clklocal_put;
2501 if (ctl_add(card, &snd_control, asihpi) < 0)
2505 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2506 snd_control.access =
2507 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2508 snd_control.info = snd_asihpi_clkrate_info;
2509 snd_control.get = snd_asihpi_clkrate_get;
2511 return ctl_add(card, &snd_control, asihpi);
2513 /*------------------------------------------------------------
2515 ------------------------------------------------------------*/
2517 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2519 struct snd_card *card = asihpi->card;
2520 unsigned int idx = 0;
2521 unsigned int subindex = 0;
2523 struct hpi_control hpi_ctl, prev_ctl;
2525 if (snd_BUG_ON(!asihpi))
2527 strcpy(card->mixername, "Asihpi Mixer");
2530 hpi_mixer_open(asihpi->adapter_index,
2532 hpi_handle_error(err);
2536 memset(&prev_ctl, 0, sizeof(prev_ctl));
2537 prev_ctl.control_type = -1;
2539 for (idx = 0; idx < 2000; idx++) {
2540 err = hpi_mixer_get_control_by_index(
2543 &hpi_ctl.src_node_type,
2544 &hpi_ctl.src_node_index,
2545 &hpi_ctl.dst_node_type,
2546 &hpi_ctl.dst_node_index,
2547 &hpi_ctl.control_type,
2548 &hpi_ctl.h_control);
2550 if (err == HPI_ERROR_CONTROL_DISABLED) {
2552 snd_printk(KERN_INFO
2553 "Disabled HPI Control(%d)\n",
2561 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2562 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2564 /* ASI50xx in SSX mode has multiple meters on the same node.
2565 Use subindex to create distinct ALSA controls
2566 for any duplicated controls.
2568 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2569 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2570 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2571 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2572 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2579 switch (hpi_ctl.control_type) {
2580 case HPI_CONTROL_VOLUME:
2581 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2583 case HPI_CONTROL_LEVEL:
2584 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2586 case HPI_CONTROL_MULTIPLEXER:
2587 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2589 case HPI_CONTROL_CHANNEL_MODE:
2590 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2592 case HPI_CONTROL_METER:
2593 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2595 case HPI_CONTROL_SAMPLECLOCK:
2596 err = snd_asihpi_sampleclock_add(
2599 case HPI_CONTROL_CONNECTION: /* ignore these */
2601 case HPI_CONTROL_TUNER:
2602 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2604 case HPI_CONTROL_AESEBU_TRANSMITTER:
2605 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2607 case HPI_CONTROL_AESEBU_RECEIVER:
2608 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2610 case HPI_CONTROL_VOX:
2611 case HPI_CONTROL_BITSTREAM:
2612 case HPI_CONTROL_MICROPHONE:
2613 case HPI_CONTROL_PARAMETRIC_EQ:
2614 case HPI_CONTROL_COMPANDER:
2617 snd_printk(KERN_INFO
2618 "Untranslated HPI Control"
2619 "(%d) %d %d %d %d %d\n",
2621 hpi_ctl.control_type,
2622 hpi_ctl.src_node_type,
2623 hpi_ctl.src_node_index,
2624 hpi_ctl.dst_node_type,
2625 hpi_ctl.dst_node_index);
2631 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2632 hpi_handle_error(err);
2634 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2639 /*------------------------------------------------------------
2641 ------------------------------------------------------------*/
2644 snd_asihpi_proc_read(struct snd_info_entry *entry,
2645 struct snd_info_buffer *buffer)
2647 struct snd_card_asihpi *asihpi = entry->private_data;
2654 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2656 "adapter ID=%4X\n_index=%d\n"
2657 "num_outstreams=%d\n_num_instreams=%d\n",
2658 asihpi->type, asihpi->adapter_index,
2659 asihpi->num_outstreams, asihpi->num_instreams);
2661 version = asihpi->version;
2663 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2664 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2666 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2668 err = hpi_mixer_get_control(asihpi->h_mixer,
2669 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2670 HPI_CONTROL_SAMPLECLOCK, &h_control);
2673 err = hpi_sample_clock_get_sample_rate(
2675 err += hpi_sample_clock_get_source(h_control, &source);
2678 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2679 rate, sampleclock_sources[source]);
2685 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2687 struct snd_info_entry *entry;
2689 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2690 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2693 /*------------------------------------------------------------
2695 ------------------------------------------------------------*/
2697 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2699 if (enable_hpi_hwdep)
2706 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2708 if (enable_hpi_hwdep)
2709 return asihpi_hpi_release(file);
2714 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2715 unsigned int cmd, unsigned long arg)
2717 if (enable_hpi_hwdep)
2718 return asihpi_hpi_ioctl(file, cmd, arg);
2724 /* results in /dev/snd/hwC#D0 file for each card with index #
2725 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2727 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2728 int device, struct snd_hwdep **rhwdep)
2730 struct snd_hwdep *hw;
2735 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2738 strcpy(hw->name, "asihpi (HPI)");
2739 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2740 hw->ops.open = snd_asihpi_hpi_open;
2741 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2742 hw->ops.release = snd_asihpi_hpi_release;
2743 hw->private_data = asihpi;
2749 /*------------------------------------------------------------
2751 ------------------------------------------------------------*/
2752 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2753 const struct pci_device_id *pci_id)
2760 struct hpi_adapter *hpi_card;
2761 struct snd_card *card;
2762 struct snd_card_asihpi *asihpi;
2768 if (dev >= SNDRV_CARDS)
2771 /* Should this be enable[hpi_card->index] ? */
2777 err = asihpi_adapter_probe(pci_dev, pci_id);
2781 hpi_card = pci_get_drvdata(pci_dev);
2782 /* first try to give the card the same index as its hardware index */
2783 err = snd_card_create(hpi_card->index,
2784 id[hpi_card->index], THIS_MODULE,
2785 sizeof(struct snd_card_asihpi),
2788 /* if that fails, try the default index==next available */
2790 snd_card_create(index[dev], id[dev],
2792 sizeof(struct snd_card_asihpi),
2796 snd_printk(KERN_WARNING
2797 "**** WARNING **** Adapter index %d->ALSA index %d\n",
2798 hpi_card->index, card->number);
2801 snd_card_set_dev(card, &pci_dev->dev);
2803 asihpi = (struct snd_card_asihpi *) card->private_data;
2804 asihpi->card = card;
2805 asihpi->pci = pci_dev;
2806 asihpi->adapter_index = hpi_card->index;
2807 hpi_handle_error(hpi_adapter_get_info(
2808 asihpi->adapter_index,
2809 &asihpi->num_outstreams,
2810 &asihpi->num_instreams,
2812 &asihpi->serial_number, &asihpi->type));
2814 version = asihpi->version;
2815 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2816 "num_instreams=%d S/N=%d\n"
2817 "Hw Version %c%d DSP code version %03d\n",
2818 asihpi->type, asihpi->adapter_index,
2819 asihpi->num_outstreams,
2820 asihpi->num_instreams, asihpi->serial_number,
2821 ((version >> 3) & 0xf) + 'A',
2823 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2825 pcm_substreams = asihpi->num_outstreams;
2826 if (pcm_substreams < asihpi->num_instreams)
2827 pcm_substreams = asihpi->num_instreams;
2829 err = hpi_adapter_get_property(asihpi->adapter_index,
2830 HPI_ADAPTER_PROPERTY_CAPS1,
2831 NULL, &asihpi->support_grouping);
2833 asihpi->support_grouping = 0;
2835 err = hpi_adapter_get_property(asihpi->adapter_index,
2836 HPI_ADAPTER_PROPERTY_CAPS2,
2837 &asihpi->support_mrx, NULL);
2839 asihpi->support_mrx = 0;
2841 err = hpi_adapter_get_property(asihpi->adapter_index,
2842 HPI_ADAPTER_PROPERTY_INTERVAL,
2843 NULL, &asihpi->update_interval_frames);
2845 asihpi->update_interval_frames = 512;
2847 if (!asihpi->can_dma)
2848 asihpi->update_interval_frames *= 2;
2850 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2853 err = hpi_instream_host_buffer_free(h_stream);
2854 asihpi->can_dma = (!err);
2856 hpi_handle_error(hpi_instream_close(h_stream));
2858 err = hpi_adapter_get_property(asihpi->adapter_index,
2859 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2860 &asihpi->in_max_chans, &asihpi->out_max_chans);
2862 asihpi->in_max_chans = 2;
2863 asihpi->out_max_chans = 2;
2866 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2868 asihpi->support_grouping,
2872 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2874 snd_printk(KERN_ERR "pcm_new failed\n");
2877 err = snd_card_asihpi_mixer_new(asihpi);
2879 snd_printk(KERN_ERR "mixer_new failed\n");
2883 err = hpi_mixer_get_control(asihpi->h_mixer,
2884 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2885 HPI_CONTROL_SAMPLECLOCK, &h_control);
2888 err = hpi_sample_clock_set_local_rate(
2889 h_control, adapter_fs);
2891 snd_asihpi_proc_init(asihpi);
2893 /* always create, can be enabled or disabled dynamically
2894 by enable_hwdep module param*/
2895 snd_asihpi_hpi_new(asihpi, 0, NULL);
2897 strcpy(card->driver, "ASIHPI");
2899 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2900 sprintf(card->longname, "%s %i",
2901 card->shortname, asihpi->adapter_index);
2902 err = snd_card_register(card);
2905 hpi_card->snd_card_asihpi = card;
2910 snd_card_free(card);
2911 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2916 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2918 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2920 snd_card_free(hpi_card->snd_card_asihpi);
2921 hpi_card->snd_card_asihpi = NULL;
2922 asihpi_adapter_remove(pci_dev);
2925 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2926 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2927 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2928 (kernel_ulong_t)HPI_6205},
2929 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2930 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2931 (kernel_ulong_t)HPI_6000},
2934 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2936 static struct pci_driver driver = {
2937 .name = KBUILD_MODNAME,
2938 .id_table = asihpi_pci_tbl,
2939 .probe = snd_asihpi_probe,
2940 .remove = __devexit_p(snd_asihpi_remove),
2942 /* .suspend = snd_asihpi_suspend,
2943 .resume = snd_asihpi_resume, */
2947 static int __init snd_asihpi_init(void)
2950 return pci_register_driver(&driver);
2953 static void __exit snd_asihpi_exit(void)
2956 pci_unregister_driver(&driver);
2960 module_init(snd_asihpi_init)
2961 module_exit(snd_asihpi_exit)