Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[zen-stable.git] / sound / pci / asihpi / asihpi.c
blobe3569bdd3b64e704f82c9e621110abe8e0853f8d
1 /*
2 * Asihpi soundcard
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"
27 #include "hpioctl.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 <sound/core.h>
36 #include <sound/control.h>
37 #include <sound/pcm.h>
38 #include <sound/pcm_params.h>
39 #include <sound/info.h>
40 #include <sound/initval.h>
41 #include <sound/tlv.h>
42 #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
50 /* copied from pcm_lib.c, hope later patch will make that version public
51 and this copy can be removed */
52 static void pcm_debug_name(struct snd_pcm_substream *substream,
53 char *name, size_t len)
55 snprintf(name, len, "pcmC%dD%d%c:%d",
56 substream->pcm->card->number,
57 substream->pcm->device,
58 substream->stream ? 'c' : 'p',
59 substream->number);
61 #define DEBUG_NAME(substream, name) char name[16]; pcm_debug_name(substream, name, sizeof(name))
62 #else
63 #define pcm_debug_name(s, n, l) do { } while (0)
64 #define DEBUG_NAME(name, substream) do { } while (0)
65 #endif
67 #if defined CONFIG_SND_DEBUG_VERBOSE
68 /**
69 * snd_printddd - very verbose debug printk
70 * @format: format string
72 * Works like snd_printk() for debugging purposes.
73 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
74 * Must set snd module debug parameter to 3 to enable at runtime.
76 #define snd_printddd(format, args...) \
77 __snd_printk(3, __FILE__, __LINE__, format, ##args)
78 #else
79 #define snd_printddd(format, args...) do { } while (0)
80 #endif
82 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
83 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
84 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
85 static int enable_hpi_hwdep = 1;
87 module_param_array(index, int, NULL, S_IRUGO);
88 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
90 module_param_array(id, charp, NULL, S_IRUGO);
91 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
93 module_param_array(enable, bool, NULL, S_IRUGO);
94 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
96 module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
97 MODULE_PARM_DESC(enable_hpi_hwdep,
98 "ALSA enable HPI hwdep for AudioScience soundcard ");
100 /* identify driver */
101 #ifdef KERNEL_ALSA_BUILD
102 static char *build_info = "Built using headers from kernel source";
103 module_param(build_info, charp, S_IRUGO);
104 MODULE_PARM_DESC(build_info, "built using headers from kernel source");
105 #else
106 static char *build_info = "Built within ALSA source";
107 module_param(build_info, charp, S_IRUGO);
108 MODULE_PARM_DESC(build_info, "built within ALSA source");
109 #endif
111 /* set to 1 to dump every control from adapter to log */
112 static const int mixer_dump;
114 #define DEFAULT_SAMPLERATE 44100
115 static int adapter_fs = DEFAULT_SAMPLERATE;
117 /* defaults */
118 #define PERIODS_MIN 2
119 #define PERIOD_BYTES_MIN 2048
120 #define BUFFER_BYTES_MAX (512 * 1024)
122 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
124 struct clk_source {
125 int source;
126 int index;
127 char *name;
130 struct clk_cache {
131 int count;
132 int has_local;
133 struct clk_source s[MAX_CLOCKSOURCES];
136 /* Per card data */
137 struct snd_card_asihpi {
138 struct snd_card *card;
139 struct pci_dev *pci;
140 u16 adapter_index;
141 u32 serial_number;
142 u16 type;
143 u16 version;
144 u16 num_outstreams;
145 u16 num_instreams;
147 u32 h_mixer;
148 struct clk_cache cc;
150 u16 can_dma;
151 u16 support_grouping;
152 u16 support_mrx;
153 u16 update_interval_frames;
154 u16 in_max_chans;
155 u16 out_max_chans;
158 /* Per stream data */
159 struct snd_card_asihpi_pcm {
160 struct timer_list timer;
161 unsigned int respawn_timer;
162 unsigned int hpi_buffer_attached;
163 unsigned int buffer_bytes;
164 unsigned int period_bytes;
165 unsigned int bytes_per_sec;
166 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
167 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
168 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
169 unsigned int drained_count;
170 struct snd_pcm_substream *substream;
171 u32 h_stream;
172 struct hpi_format format;
175 /* universal stream verbs work with out or in stream handles */
177 /* Functions to allow driver to give a buffer to HPI for busmastering */
179 static u16 hpi_stream_host_buffer_attach(
180 u32 h_stream, /* handle to outstream. */
181 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
182 u32 pci_address
185 struct hpi_message hm;
186 struct hpi_response hr;
187 unsigned int obj = hpi_handle_object(h_stream);
189 if (!h_stream)
190 return HPI_ERROR_INVALID_OBJ;
191 hpi_init_message_response(&hm, &hr, obj,
192 obj == HPI_OBJ_OSTREAM ?
193 HPI_OSTREAM_HOSTBUFFER_ALLOC :
194 HPI_ISTREAM_HOSTBUFFER_ALLOC);
196 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
197 &hm.obj_index);
199 hm.u.d.u.buffer.buffer_size = size_in_bytes;
200 hm.u.d.u.buffer.pci_address = pci_address;
201 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
202 hpi_send_recv(&hm, &hr);
203 return hr.error;
206 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
208 struct hpi_message hm;
209 struct hpi_response hr;
210 unsigned int obj = hpi_handle_object(h_stream);
212 if (!h_stream)
213 return HPI_ERROR_INVALID_OBJ;
215 hpi_init_message_response(&hm, &hr, obj,
216 obj == HPI_OBJ_OSTREAM ?
217 HPI_OSTREAM_HOSTBUFFER_FREE :
218 HPI_ISTREAM_HOSTBUFFER_FREE);
220 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
221 &hm.obj_index);
222 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
223 hpi_send_recv(&hm, &hr);
224 return hr.error;
227 static inline u16 hpi_stream_start(u32 h_stream)
229 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
230 return hpi_outstream_start(h_stream);
231 else
232 return hpi_instream_start(h_stream);
235 static inline u16 hpi_stream_stop(u32 h_stream)
237 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
238 return hpi_outstream_stop(h_stream);
239 else
240 return hpi_instream_stop(h_stream);
243 static inline u16 hpi_stream_get_info_ex(
244 u32 h_stream,
245 u16 *pw_state,
246 u32 *pbuffer_size,
247 u32 *pdata_in_buffer,
248 u32 *psample_count,
249 u32 *pauxiliary_data
252 u16 e;
253 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
254 e = hpi_outstream_get_info_ex(h_stream, pw_state,
255 pbuffer_size, pdata_in_buffer,
256 psample_count, pauxiliary_data);
257 else
258 e = hpi_instream_get_info_ex(h_stream, pw_state,
259 pbuffer_size, pdata_in_buffer,
260 psample_count, pauxiliary_data);
261 return e;
264 static inline u16 hpi_stream_group_add(
265 u32 h_master,
266 u32 h_stream)
268 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
269 return hpi_outstream_group_add(h_master, h_stream);
270 else
271 return hpi_instream_group_add(h_master, h_stream);
274 static inline u16 hpi_stream_group_reset(u32 h_stream)
276 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
277 return hpi_outstream_group_reset(h_stream);
278 else
279 return hpi_instream_group_reset(h_stream);
282 static inline u16 hpi_stream_group_get_map(
283 u32 h_stream, u32 *mo, u32 *mi)
285 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
286 return hpi_outstream_group_get_map(h_stream, mo, mi);
287 else
288 return hpi_instream_group_get_map(h_stream, mo, mi);
291 static u16 handle_error(u16 err, int line, char *filename)
293 if (err)
294 printk(KERN_WARNING
295 "in file %s, line %d: HPI error %d\n",
296 filename, line, err);
297 return err;
300 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
302 /***************************** GENERAL PCM ****************/
304 static void print_hwparams(struct snd_pcm_substream *substream,
305 struct snd_pcm_hw_params *p)
307 DEBUG_NAME(substream, name);
308 snd_printd("%s HWPARAMS\n", name);
309 snd_printd(" samplerate %d Hz\n", params_rate(p));
310 snd_printd(" channels %d\n", params_channels(p));
311 snd_printd(" format %d\n", params_format(p));
312 snd_printd(" subformat %d\n", params_subformat(p));
313 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
314 snd_printd(" period %d B\n", params_period_bytes(p));
315 snd_printd(" access %d\n", params_access(p));
316 snd_printd(" period_size %d\n", params_period_size(p));
317 snd_printd(" periods %d\n", params_periods(p));
318 snd_printd(" buffer_size %d\n", params_buffer_size(p));
319 snd_printd(" %d B/s\n", params_rate(p) *
320 params_channels(p) *
321 snd_pcm_format_width(params_format(p)) / 8);
325 static snd_pcm_format_t hpi_to_alsa_formats[] = {
326 -1, /* INVALID */
327 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
328 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
329 -1, /* HPI_FORMAT_MPEG_L1 3 */
330 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
331 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
332 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
333 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
334 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
335 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
336 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
337 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
338 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
339 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
340 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
341 #if 1
342 /* ALSA can't handle 3 byte sample size together with power-of-2
343 * constraint on buffer_bytes, so disable this format
346 #else
347 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
348 #endif
352 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
353 u16 *hpi_format)
355 u16 format;
357 for (format = HPI_FORMAT_PCM8_UNSIGNED;
358 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
359 if (hpi_to_alsa_formats[format] == alsa_format) {
360 *hpi_format = format;
361 return 0;
365 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
366 alsa_format);
367 *hpi_format = 0;
368 return -EINVAL;
371 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
372 struct snd_pcm_hardware *pcmhw)
374 u16 err;
375 u32 h_control;
376 u32 sample_rate;
377 int idx;
378 unsigned int rate_min = 200000;
379 unsigned int rate_max = 0;
380 unsigned int rates = 0;
382 if (asihpi->support_mrx) {
383 rates |= SNDRV_PCM_RATE_CONTINUOUS;
384 rates |= SNDRV_PCM_RATE_8000_96000;
385 rate_min = 8000;
386 rate_max = 100000;
387 } else {
388 /* on cards without SRC,
389 valid rates are determined by sampleclock */
390 err = hpi_mixer_get_control(asihpi->h_mixer,
391 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
392 HPI_CONTROL_SAMPLECLOCK, &h_control);
393 if (err) {
394 snd_printk(KERN_ERR
395 "No local sampleclock, err %d\n", err);
398 for (idx = -1; idx < 100; idx++) {
399 if (idx == -1) {
400 if (hpi_sample_clock_get_sample_rate(h_control,
401 &sample_rate))
402 continue;
403 } else if (hpi_sample_clock_query_local_rate(h_control,
404 idx, &sample_rate)) {
405 break;
408 rate_min = min(rate_min, sample_rate);
409 rate_max = max(rate_max, sample_rate);
411 switch (sample_rate) {
412 case 5512:
413 rates |= SNDRV_PCM_RATE_5512;
414 break;
415 case 8000:
416 rates |= SNDRV_PCM_RATE_8000;
417 break;
418 case 11025:
419 rates |= SNDRV_PCM_RATE_11025;
420 break;
421 case 16000:
422 rates |= SNDRV_PCM_RATE_16000;
423 break;
424 case 22050:
425 rates |= SNDRV_PCM_RATE_22050;
426 break;
427 case 32000:
428 rates |= SNDRV_PCM_RATE_32000;
429 break;
430 case 44100:
431 rates |= SNDRV_PCM_RATE_44100;
432 break;
433 case 48000:
434 rates |= SNDRV_PCM_RATE_48000;
435 break;
436 case 64000:
437 rates |= SNDRV_PCM_RATE_64000;
438 break;
439 case 88200:
440 rates |= SNDRV_PCM_RATE_88200;
441 break;
442 case 96000:
443 rates |= SNDRV_PCM_RATE_96000;
444 break;
445 case 176400:
446 rates |= SNDRV_PCM_RATE_176400;
447 break;
448 case 192000:
449 rates |= SNDRV_PCM_RATE_192000;
450 break;
451 default: /* some other rate */
452 rates |= SNDRV_PCM_RATE_KNOT;
457 pcmhw->rates = rates;
458 pcmhw->rate_min = rate_min;
459 pcmhw->rate_max = rate_max;
462 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
463 struct snd_pcm_hw_params *params)
465 struct snd_pcm_runtime *runtime = substream->runtime;
466 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
467 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
468 int err;
469 u16 format;
470 int width;
471 unsigned int bytes_per_sec;
473 print_hwparams(substream, params);
474 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
475 if (err < 0)
476 return err;
477 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
478 if (err)
479 return err;
481 hpi_handle_error(hpi_format_create(&dpcm->format,
482 params_channels(params),
483 format, params_rate(params), 0, 0));
485 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
486 if (hpi_instream_reset(dpcm->h_stream) != 0)
487 return -EINVAL;
489 if (hpi_instream_set_format(
490 dpcm->h_stream, &dpcm->format) != 0)
491 return -EINVAL;
494 dpcm->hpi_buffer_attached = 0;
495 if (card->can_dma) {
496 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
497 params_buffer_bytes(params), runtime->dma_addr);
498 if (err == 0) {
499 snd_printdd(
500 "stream_host_buffer_attach succeeded %u %lu\n",
501 params_buffer_bytes(params),
502 (unsigned long)runtime->dma_addr);
503 } else {
504 snd_printd("stream_host_buffer_attach error %d\n",
505 err);
506 return -ENOMEM;
509 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
510 &dpcm->hpi_buffer_attached,
511 NULL, NULL, NULL);
513 snd_printdd("stream_host_buffer_attach status 0x%x\n",
514 dpcm->hpi_buffer_attached);
516 bytes_per_sec = params_rate(params) * params_channels(params);
517 width = snd_pcm_format_width(params_format(params));
518 bytes_per_sec *= width;
519 bytes_per_sec /= 8;
520 if (width < 0 || bytes_per_sec == 0)
521 return -EINVAL;
523 dpcm->bytes_per_sec = bytes_per_sec;
524 dpcm->buffer_bytes = params_buffer_bytes(params);
525 dpcm->period_bytes = params_period_bytes(params);
527 return 0;
530 static int
531 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
533 struct snd_pcm_runtime *runtime = substream->runtime;
534 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
535 if (dpcm->hpi_buffer_attached)
536 hpi_stream_host_buffer_detach(dpcm->h_stream);
538 snd_pcm_lib_free_pages(substream);
539 return 0;
542 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
544 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
545 kfree(dpcm);
548 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
549 substream)
551 struct snd_pcm_runtime *runtime = substream->runtime;
552 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
553 int expiry;
555 expiry = HZ / 200;
556 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
557 expiry = max(expiry, 1); /* don't let it be zero! */
558 dpcm->timer.expires = jiffies + expiry;
559 dpcm->respawn_timer = 1;
560 add_timer(&dpcm->timer);
563 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
565 struct snd_pcm_runtime *runtime = substream->runtime;
566 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
568 dpcm->respawn_timer = 0;
569 del_timer(&dpcm->timer);
572 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
573 int cmd)
575 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
576 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
577 struct snd_pcm_substream *s;
578 u16 e;
579 DEBUG_NAME(substream, name);
581 snd_printdd("%s trigger\n", name);
583 switch (cmd) {
584 case SNDRV_PCM_TRIGGER_START:
585 snd_pcm_group_for_each_entry(s, substream) {
586 struct snd_pcm_runtime *runtime = s->runtime;
587 struct snd_card_asihpi_pcm *ds = runtime->private_data;
589 if (snd_pcm_substream_chip(s) != card)
590 continue;
592 /* don't link Cap and Play */
593 if (substream->stream != s->stream)
594 continue;
596 ds->drained_count = 0;
597 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
598 /* How do I know how much valid data is present
599 * in buffer? Must be at least one period!
600 * Guessing 2 periods, but if
601 * buffer is bigger it may contain even more
602 * data??
604 unsigned int preload = ds->period_bytes * 1;
605 snd_printddd("%d preload x%x\n", s->number, preload);
606 hpi_handle_error(hpi_outstream_write_buf(
607 ds->h_stream,
608 &runtime->dma_area[0],
609 preload,
610 &ds->format));
611 ds->pcm_buf_host_rw_ofs = preload;
614 if (card->support_grouping) {
615 snd_printdd("%d group\n", s->number);
616 e = hpi_stream_group_add(
617 dpcm->h_stream,
618 ds->h_stream);
619 if (!e) {
620 snd_pcm_trigger_done(s, substream);
621 } else {
622 hpi_handle_error(e);
623 break;
625 } else
626 break;
628 snd_printdd("start\n");
629 /* start the master stream */
630 snd_card_asihpi_pcm_timer_start(substream);
631 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
632 !card->can_dma)
633 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
634 break;
636 case SNDRV_PCM_TRIGGER_STOP:
637 snd_card_asihpi_pcm_timer_stop(substream);
638 snd_pcm_group_for_each_entry(s, substream) {
639 if (snd_pcm_substream_chip(s) != card)
640 continue;
641 /* don't link Cap and Play */
642 if (substream->stream != s->stream)
643 continue;
645 /*? workaround linked streams don't
646 transition to SETUP 20070706*/
647 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
649 if (card->support_grouping) {
650 snd_printdd("%d group\n", s->number);
651 snd_pcm_trigger_done(s, substream);
652 } else
653 break;
655 snd_printdd("stop\n");
657 /* _prepare and _hwparams reset the stream */
658 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
659 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
660 hpi_handle_error(
661 hpi_outstream_reset(dpcm->h_stream));
663 if (card->support_grouping)
664 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
665 break;
667 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
668 snd_printdd("pause release\n");
669 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
670 snd_card_asihpi_pcm_timer_start(substream);
671 break;
672 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
673 snd_printdd("pause\n");
674 snd_card_asihpi_pcm_timer_stop(substream);
675 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
676 break;
677 default:
678 snd_printd(KERN_ERR "\tINVALID\n");
679 return -EINVAL;
682 return 0;
685 /*algorithm outline
686 Without linking degenerates to getting single stream pos etc
687 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
690 pcm_buf_dma_ofs=get_buf_pos(s);
691 for_each_linked_stream(s) {
692 pcm_buf_dma_ofs=get_buf_pos(s);
693 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
694 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
696 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
697 for_each_linked_stream(s) {
698 s->pcm_buf_dma_ofs = min_buf_pos;
699 if (new_data > period_bytes) {
700 if (mmap) {
701 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
702 if (playback) {
703 write(period_bytes);
704 } else {
705 read(period_bytes);
708 snd_pcm_period_elapsed(s);
713 /** Minimum of 2 modulo values. Works correctly when the difference between
714 * the values is less than half the modulus
716 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
717 unsigned long int modulus)
719 unsigned int result;
720 if (((a-b) % modulus) < (modulus/2))
721 result = b;
722 else
723 result = a;
725 return result;
728 /** Timer function, equivalent to interrupt service routine for cards
730 static void snd_card_asihpi_timer_function(unsigned long data)
732 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
733 struct snd_pcm_substream *substream = dpcm->substream;
734 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
735 struct snd_pcm_runtime *runtime;
736 struct snd_pcm_substream *s;
737 unsigned int newdata = 0;
738 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
739 unsigned int remdata, xfercount, next_jiffies;
740 int first = 1;
741 int loops = 0;
742 u16 state;
743 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
744 DEBUG_NAME(substream, name);
746 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
748 /* find minimum newdata and buffer pos in group */
749 snd_pcm_group_for_each_entry(s, substream) {
750 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
751 runtime = s->runtime;
753 if (snd_pcm_substream_chip(s) != card)
754 continue;
756 /* don't link Cap and Play */
757 if (substream->stream != s->stream)
758 continue;
760 hpi_handle_error(hpi_stream_get_info_ex(
761 ds->h_stream, &state,
762 &buffer_size, &bytes_avail,
763 &samples_played, &on_card_bytes));
765 /* number of bytes in on-card buffer */
766 runtime->delay = on_card_bytes;
768 if (!card->can_dma)
769 on_card_bytes = bytes_avail;
771 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
772 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
773 if (state == HPI_STATE_STOPPED) {
774 if ((bytes_avail == 0) &&
775 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
776 hpi_handle_error(hpi_stream_start(ds->h_stream));
777 snd_printdd("P%d start\n", s->number);
778 ds->drained_count = 0;
780 } else if (state == HPI_STATE_DRAINED) {
781 snd_printd(KERN_WARNING "P%d drained\n",
782 s->number);
783 ds->drained_count++;
784 if (ds->drained_count > 2) {
785 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
786 continue;
788 } else {
789 ds->drained_count = 0;
791 } else
792 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
794 if (first) {
795 /* can't statically init min when wrap is involved */
796 min_buf_pos = pcm_buf_dma_ofs;
797 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
798 first = 0;
799 } else {
800 min_buf_pos =
801 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
802 newdata = min(
803 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
804 newdata);
807 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
808 (unsigned long)frames_to_bytes(runtime,
809 runtime->status->hw_ptr),
810 (unsigned long)frames_to_bytes(runtime,
811 runtime->control->appl_ptr));
813 snd_printdd("%d S=%d, "
814 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
815 "aux=0x%04X space=0x%04X\n",
816 s->number, state,
817 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
818 (int)bytes_avail,
819 (int)on_card_bytes, buffer_size-bytes_avail);
820 loops++;
822 pcm_buf_dma_ofs = min_buf_pos;
824 remdata = newdata % dpcm->period_bytes;
825 xfercount = newdata - remdata; /* a multiple of period_bytes */
826 /* come back when on_card_bytes has decreased enough to allow
827 write to happen, or when data has been consumed to make another
828 period
830 if (xfercount && (on_card_bytes > dpcm->period_bytes))
831 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
832 else
833 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
835 next_jiffies = max(next_jiffies, 1U);
836 dpcm->timer.expires = jiffies + next_jiffies;
837 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
838 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
840 snd_pcm_group_for_each_entry(s, substream) {
841 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
843 /* don't link Cap and Play */
844 if (substream->stream != s->stream)
845 continue;
847 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
849 if (xfercount &&
850 /* Limit use of on card fifo for playback */
851 ((on_card_bytes <= ds->period_bytes) ||
852 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
856 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
857 unsigned int xfer1, xfer2;
858 char *pd = &s->runtime->dma_area[buf_ofs];
860 if (card->can_dma) { /* buffer wrap is handled at lower level */
861 xfer1 = xfercount;
862 xfer2 = 0;
863 } else {
864 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
865 xfer2 = xfercount - xfer1;
868 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
869 snd_printddd("P%d write1 0x%04X 0x%04X\n",
870 s->number, xfer1, buf_ofs);
871 hpi_handle_error(
872 hpi_outstream_write_buf(
873 ds->h_stream, pd, xfer1,
874 &ds->format));
876 if (xfer2) {
877 pd = s->runtime->dma_area;
879 snd_printddd("P%d write2 0x%04X 0x%04X\n",
880 s->number,
881 xfercount - xfer1, buf_ofs);
882 hpi_handle_error(
883 hpi_outstream_write_buf(
884 ds->h_stream, pd,
885 xfercount - xfer1,
886 &ds->format));
888 } else {
889 snd_printddd("C%d read1 0x%04x\n",
890 s->number, xfer1);
891 hpi_handle_error(
892 hpi_instream_read_buf(
893 ds->h_stream,
894 pd, xfer1));
895 if (xfer2) {
896 pd = s->runtime->dma_area;
897 snd_printddd("C%d read2 0x%04x\n",
898 s->number, xfer2);
899 hpi_handle_error(
900 hpi_instream_read_buf(
901 ds->h_stream,
902 pd, xfer2));
905 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
906 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
907 snd_pcm_period_elapsed(s);
911 if (dpcm->respawn_timer)
912 add_timer(&dpcm->timer);
915 /***************************** PLAYBACK OPS ****************/
916 static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
917 unsigned int cmd, void *arg)
919 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
920 return snd_pcm_lib_ioctl(substream, cmd, arg);
923 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
924 substream)
926 struct snd_pcm_runtime *runtime = substream->runtime;
927 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
929 snd_printdd("P%d prepare\n", substream->number);
931 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
932 dpcm->pcm_buf_host_rw_ofs = 0;
933 dpcm->pcm_buf_dma_ofs = 0;
934 dpcm->pcm_buf_elapsed_dma_ofs = 0;
935 return 0;
938 static snd_pcm_uframes_t
939 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
941 struct snd_pcm_runtime *runtime = substream->runtime;
942 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
943 snd_pcm_uframes_t ptr;
945 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
946 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
947 return ptr;
950 static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
951 u32 h_stream,
952 struct snd_pcm_hardware *pcmhw)
954 struct hpi_format hpi_format;
955 u16 format;
956 u16 err;
957 u32 h_control;
958 u32 sample_rate = 48000;
960 /* on cards without SRC, must query at valid rate,
961 * maybe set by external sync
963 err = hpi_mixer_get_control(asihpi->h_mixer,
964 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
965 HPI_CONTROL_SAMPLECLOCK, &h_control);
967 if (!err)
968 err = hpi_sample_clock_get_sample_rate(h_control,
969 &sample_rate);
971 for (format = HPI_FORMAT_PCM8_UNSIGNED;
972 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
973 err = hpi_format_create(&hpi_format,
974 2, format, sample_rate, 128000, 0);
975 if (!err)
976 err = hpi_outstream_query_format(h_stream,
977 &hpi_format);
978 if (!err && (hpi_to_alsa_formats[format] != -1))
979 pcmhw->formats |=
980 (1ULL << hpi_to_alsa_formats[format]);
984 static struct snd_pcm_hardware snd_card_asihpi_playback = {
985 .channels_min = 1,
986 .channels_max = 2,
987 .buffer_bytes_max = BUFFER_BYTES_MAX,
988 .period_bytes_min = PERIOD_BYTES_MIN,
989 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
990 .periods_min = PERIODS_MIN,
991 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
992 .fifo_size = 0,
995 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
997 struct snd_pcm_runtime *runtime = substream->runtime;
998 struct snd_card_asihpi_pcm *dpcm;
999 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1000 int err;
1002 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1003 if (dpcm == NULL)
1004 return -ENOMEM;
1006 err =
1007 hpi_outstream_open(card->adapter_index,
1008 substream->number, &dpcm->h_stream);
1009 hpi_handle_error(err);
1010 if (err)
1011 kfree(dpcm);
1012 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1013 return -EBUSY;
1014 if (err)
1015 return -EIO;
1017 /*? also check ASI5000 samplerate source
1018 If external, only support external rate.
1019 If internal and other stream playing, can't switch
1022 init_timer(&dpcm->timer);
1023 dpcm->timer.data = (unsigned long) dpcm;
1024 dpcm->timer.function = snd_card_asihpi_timer_function;
1025 dpcm->substream = substream;
1026 runtime->private_data = dpcm;
1027 runtime->private_free = snd_card_asihpi_runtime_free;
1029 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1030 /*?snd_card_asihpi_playback.period_bytes_min =
1031 card->out_max_chans * 4096; */
1033 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1034 &snd_card_asihpi_playback);
1036 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1038 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1039 SNDRV_PCM_INFO_DOUBLE |
1040 SNDRV_PCM_INFO_BATCH |
1041 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1042 SNDRV_PCM_INFO_PAUSE |
1043 SNDRV_PCM_INFO_MMAP |
1044 SNDRV_PCM_INFO_MMAP_VALID;
1046 if (card->support_grouping)
1047 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1049 /* struct is copied, so can create initializer dynamically */
1050 runtime->hw = snd_card_asihpi_playback;
1052 if (card->can_dma)
1053 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1054 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1055 if (err < 0)
1056 return err;
1058 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1059 card->update_interval_frames);
1061 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1062 card->update_interval_frames * 2, UINT_MAX);
1064 snd_pcm_set_sync(substream);
1066 snd_printdd("playback open\n");
1068 return 0;
1071 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1073 struct snd_pcm_runtime *runtime = substream->runtime;
1074 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1076 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1077 snd_printdd("playback close\n");
1079 return 0;
1082 static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1083 .open = snd_card_asihpi_playback_open,
1084 .close = snd_card_asihpi_playback_close,
1085 .ioctl = snd_card_asihpi_playback_ioctl,
1086 .hw_params = snd_card_asihpi_pcm_hw_params,
1087 .hw_free = snd_card_asihpi_hw_free,
1088 .prepare = snd_card_asihpi_playback_prepare,
1089 .trigger = snd_card_asihpi_trigger,
1090 .pointer = snd_card_asihpi_playback_pointer,
1093 /***************************** CAPTURE OPS ****************/
1094 static snd_pcm_uframes_t
1095 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1097 struct snd_pcm_runtime *runtime = substream->runtime;
1098 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1100 snd_printddd("capture pointer %d=%d\n",
1101 substream->number, dpcm->pcm_buf_dma_ofs);
1102 /* NOTE Unlike playback can't use actual samples_played
1103 for the capture position, because those samples aren't yet in
1104 the local buffer available for reading.
1106 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1109 static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1110 unsigned int cmd, void *arg)
1112 return snd_pcm_lib_ioctl(substream, cmd, arg);
1115 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1117 struct snd_pcm_runtime *runtime = substream->runtime;
1118 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1120 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1121 dpcm->pcm_buf_host_rw_ofs = 0;
1122 dpcm->pcm_buf_dma_ofs = 0;
1123 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1125 snd_printdd("Capture Prepare %d\n", substream->number);
1126 return 0;
1131 static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1132 u32 h_stream,
1133 struct snd_pcm_hardware *pcmhw)
1135 struct hpi_format hpi_format;
1136 u16 format;
1137 u16 err;
1138 u32 h_control;
1139 u32 sample_rate = 48000;
1141 /* on cards without SRC, must query at valid rate,
1142 maybe set by external sync */
1143 err = hpi_mixer_get_control(asihpi->h_mixer,
1144 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1145 HPI_CONTROL_SAMPLECLOCK, &h_control);
1147 if (!err)
1148 err = hpi_sample_clock_get_sample_rate(h_control,
1149 &sample_rate);
1151 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1152 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1154 err = hpi_format_create(&hpi_format, 2, format,
1155 sample_rate, 128000, 0);
1156 if (!err)
1157 err = hpi_instream_query_format(h_stream,
1158 &hpi_format);
1159 if (!err)
1160 pcmhw->formats |=
1161 (1ULL << hpi_to_alsa_formats[format]);
1166 static struct snd_pcm_hardware snd_card_asihpi_capture = {
1167 .channels_min = 1,
1168 .channels_max = 2,
1169 .buffer_bytes_max = BUFFER_BYTES_MAX,
1170 .period_bytes_min = PERIOD_BYTES_MIN,
1171 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1172 .periods_min = PERIODS_MIN,
1173 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1174 .fifo_size = 0,
1177 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1179 struct snd_pcm_runtime *runtime = substream->runtime;
1180 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1181 struct snd_card_asihpi_pcm *dpcm;
1182 int err;
1184 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1185 if (dpcm == NULL)
1186 return -ENOMEM;
1188 snd_printdd("capture open adapter %d stream %d\n",
1189 card->adapter_index, substream->number);
1191 err = hpi_handle_error(
1192 hpi_instream_open(card->adapter_index,
1193 substream->number, &dpcm->h_stream));
1194 if (err)
1195 kfree(dpcm);
1196 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1197 return -EBUSY;
1198 if (err)
1199 return -EIO;
1202 init_timer(&dpcm->timer);
1203 dpcm->timer.data = (unsigned long) dpcm;
1204 dpcm->timer.function = snd_card_asihpi_timer_function;
1205 dpcm->substream = substream;
1206 runtime->private_data = dpcm;
1207 runtime->private_free = snd_card_asihpi_runtime_free;
1209 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1210 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1211 &snd_card_asihpi_capture);
1212 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1213 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1214 SNDRV_PCM_INFO_MMAP |
1215 SNDRV_PCM_INFO_MMAP_VALID;
1217 if (card->support_grouping)
1218 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1220 runtime->hw = snd_card_asihpi_capture;
1222 if (card->can_dma)
1223 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1224 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1225 if (err < 0)
1226 return err;
1228 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1229 card->update_interval_frames);
1230 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1231 card->update_interval_frames * 2, UINT_MAX);
1233 snd_pcm_set_sync(substream);
1235 return 0;
1238 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1240 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1242 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1243 return 0;
1246 static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1247 .open = snd_card_asihpi_capture_open,
1248 .close = snd_card_asihpi_capture_close,
1249 .ioctl = snd_card_asihpi_capture_ioctl,
1250 .hw_params = snd_card_asihpi_pcm_hw_params,
1251 .hw_free = snd_card_asihpi_hw_free,
1252 .prepare = snd_card_asihpi_capture_prepare,
1253 .trigger = snd_card_asihpi_trigger,
1254 .pointer = snd_card_asihpi_capture_pointer,
1257 static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1258 int device, int substreams)
1260 struct snd_pcm *pcm;
1261 int err;
1263 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1264 asihpi->num_outstreams, asihpi->num_instreams,
1265 &pcm);
1266 if (err < 0)
1267 return err;
1268 /* pointer to ops struct is stored, dont change ops afterwards! */
1269 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1270 &snd_card_asihpi_playback_mmap_ops);
1271 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1272 &snd_card_asihpi_capture_mmap_ops);
1274 pcm->private_data = asihpi;
1275 pcm->info_flags = 0;
1276 strcpy(pcm->name, "Asihpi PCM");
1278 /*? do we want to emulate MMAP for non-BBM cards?
1279 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1280 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1281 snd_dma_pci_data(asihpi->pci),
1282 64*1024, BUFFER_BYTES_MAX);
1284 return 0;
1287 /***************************** MIXER CONTROLS ****************/
1288 struct hpi_control {
1289 u32 h_control;
1290 u16 control_type;
1291 u16 src_node_type;
1292 u16 src_node_index;
1293 u16 dst_node_type;
1294 u16 dst_node_index;
1295 u16 band;
1296 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1299 static const char * const asihpi_tuner_band_names[] = {
1300 "invalid",
1301 "AM",
1302 "FM mono",
1303 "TV NTSC-M",
1304 "FM stereo",
1305 "AUX",
1306 "TV PAL BG",
1307 "TV PAL I",
1308 "TV PAL DK",
1309 "TV SECAM",
1312 compile_time_assert(
1313 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1314 (HPI_TUNER_BAND_LAST+1)),
1315 assert_tuner_band_names_size);
1317 static const char * const asihpi_src_names[] = {
1318 "no source",
1319 "PCM",
1320 "Line",
1321 "Digital",
1322 "Tuner",
1323 "RF",
1324 "Clock",
1325 "Bitstream",
1326 "Microphone",
1327 "Cobranet",
1328 "Analog",
1329 "Adapter",
1332 compile_time_assert(
1333 (ARRAY_SIZE(asihpi_src_names) ==
1334 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1335 assert_src_names_size);
1337 static const char * const asihpi_dst_names[] = {
1338 "no destination",
1339 "PCM",
1340 "Line",
1341 "Digital",
1342 "RF",
1343 "Speaker",
1344 "Cobranet Out",
1345 "Analog"
1348 compile_time_assert(
1349 (ARRAY_SIZE(asihpi_dst_names) ==
1350 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1351 assert_dst_names_size);
1353 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1354 struct snd_card_asihpi *asihpi)
1356 int err;
1358 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1359 if (err < 0)
1360 return err;
1361 else if (mixer_dump)
1362 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1364 return 0;
1367 /* Convert HPI control name and location into ALSA control name */
1368 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1369 struct hpi_control *hpi_ctl,
1370 char *name)
1372 char *dir;
1373 memset(snd_control, 0, sizeof(*snd_control));
1374 snd_control->name = hpi_ctl->name;
1375 snd_control->private_value = hpi_ctl->h_control;
1376 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1377 snd_control->index = 0;
1379 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1380 dir = ""; /* clock is neither capture nor playback */
1381 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1382 dir = "Capture "; /* On or towards a PCM capture destination*/
1383 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1384 (!hpi_ctl->dst_node_type))
1385 dir = "Capture "; /* On a source node that is not PCM playback */
1386 else if (hpi_ctl->src_node_type &&
1387 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1388 (hpi_ctl->dst_node_type))
1389 dir = "Monitor Playback "; /* Between an input and an output */
1390 else
1391 dir = "Playback "; /* PCM Playback source, or output node */
1393 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1394 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1395 asihpi_src_names[hpi_ctl->src_node_type],
1396 hpi_ctl->src_node_index,
1397 asihpi_dst_names[hpi_ctl->dst_node_type],
1398 hpi_ctl->dst_node_index,
1399 dir, name);
1400 else if (hpi_ctl->dst_node_type) {
1401 sprintf(hpi_ctl->name, "%s %d %s%s",
1402 asihpi_dst_names[hpi_ctl->dst_node_type],
1403 hpi_ctl->dst_node_index,
1404 dir, name);
1405 } else {
1406 sprintf(hpi_ctl->name, "%s %d %s%s",
1407 asihpi_src_names[hpi_ctl->src_node_type],
1408 hpi_ctl->src_node_index,
1409 dir, name);
1411 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1412 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1415 /*------------------------------------------------------------
1416 Volume controls
1417 ------------------------------------------------------------*/
1418 #define VOL_STEP_mB 1
1419 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_info *uinfo)
1422 u32 h_control = kcontrol->private_value;
1423 u16 err;
1424 /* native gains are in millibels */
1425 short min_gain_mB;
1426 short max_gain_mB;
1427 short step_gain_mB;
1429 err = hpi_volume_query_range(h_control,
1430 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1431 if (err) {
1432 max_gain_mB = 0;
1433 min_gain_mB = -10000;
1434 step_gain_mB = VOL_STEP_mB;
1437 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1438 uinfo->count = 2;
1439 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1440 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1441 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1442 return 0;
1445 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
1448 u32 h_control = kcontrol->private_value;
1449 short an_gain_mB[HPI_MAX_CHANNELS];
1451 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1452 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1453 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1455 return 0;
1458 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
1461 int change;
1462 u32 h_control = kcontrol->private_value;
1463 short an_gain_mB[HPI_MAX_CHANNELS];
1465 an_gain_mB[0] =
1466 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1467 an_gain_mB[1] =
1468 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1469 /* change = asihpi->mixer_volume[addr][0] != left ||
1470 asihpi->mixer_volume[addr][1] != right;
1472 change = 1;
1473 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1474 return change;
1477 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1479 static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1480 struct hpi_control *hpi_ctl)
1482 struct snd_card *card = asihpi->card;
1483 struct snd_kcontrol_new snd_control;
1485 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1486 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1487 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1488 snd_control.info = snd_asihpi_volume_info;
1489 snd_control.get = snd_asihpi_volume_get;
1490 snd_control.put = snd_asihpi_volume_put;
1491 snd_control.tlv.p = db_scale_100;
1493 return ctl_add(card, &snd_control, asihpi);
1496 /*------------------------------------------------------------
1497 Level controls
1498 ------------------------------------------------------------*/
1499 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_info *uinfo)
1502 u32 h_control = kcontrol->private_value;
1503 u16 err;
1504 short min_gain_mB;
1505 short max_gain_mB;
1506 short step_gain_mB;
1508 err =
1509 hpi_level_query_range(h_control, &min_gain_mB,
1510 &max_gain_mB, &step_gain_mB);
1511 if (err) {
1512 max_gain_mB = 2400;
1513 min_gain_mB = -1000;
1514 step_gain_mB = 100;
1517 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1518 uinfo->count = 2;
1519 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1520 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1521 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1522 return 0;
1525 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1528 u32 h_control = kcontrol->private_value;
1529 short an_gain_mB[HPI_MAX_CHANNELS];
1531 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1532 ucontrol->value.integer.value[0] =
1533 an_gain_mB[0] / HPI_UNITS_PER_dB;
1534 ucontrol->value.integer.value[1] =
1535 an_gain_mB[1] / HPI_UNITS_PER_dB;
1537 return 0;
1540 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1543 int change;
1544 u32 h_control = kcontrol->private_value;
1545 short an_gain_mB[HPI_MAX_CHANNELS];
1547 an_gain_mB[0] =
1548 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1549 an_gain_mB[1] =
1550 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1551 /* change = asihpi->mixer_level[addr][0] != left ||
1552 asihpi->mixer_level[addr][1] != right;
1554 change = 1;
1555 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1556 return change;
1559 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1561 static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1562 struct hpi_control *hpi_ctl)
1564 struct snd_card *card = asihpi->card;
1565 struct snd_kcontrol_new snd_control;
1567 /* can't use 'volume' cos some nodes have volume as well */
1568 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1569 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1570 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1571 snd_control.info = snd_asihpi_level_info;
1572 snd_control.get = snd_asihpi_level_get;
1573 snd_control.put = snd_asihpi_level_put;
1574 snd_control.tlv.p = db_scale_level;
1576 return ctl_add(card, &snd_control, asihpi);
1579 /*------------------------------------------------------------
1580 AESEBU controls
1581 ------------------------------------------------------------*/
1583 /* AESEBU format */
1584 static const char * const asihpi_aesebu_format_names[] = {
1585 "N/A", "S/PDIF", "AES/EBU" };
1587 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1588 struct snd_ctl_elem_info *uinfo)
1590 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1591 uinfo->count = 1;
1592 uinfo->value.enumerated.items = 3;
1594 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1595 uinfo->value.enumerated.item =
1596 uinfo->value.enumerated.items - 1;
1598 strcpy(uinfo->value.enumerated.name,
1599 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1601 return 0;
1604 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol,
1606 u16 (*func)(u32, u16 *))
1608 u32 h_control = kcontrol->private_value;
1609 u16 source, err;
1611 err = func(h_control, &source);
1613 /* default to N/A */
1614 ucontrol->value.enumerated.item[0] = 0;
1615 /* return success but set the control to N/A */
1616 if (err)
1617 return 0;
1618 if (source == HPI_AESEBU_FORMAT_SPDIF)
1619 ucontrol->value.enumerated.item[0] = 1;
1620 if (source == HPI_AESEBU_FORMAT_AESEBU)
1621 ucontrol->value.enumerated.item[0] = 2;
1623 return 0;
1626 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol,
1628 u16 (*func)(u32, u16))
1630 u32 h_control = kcontrol->private_value;
1632 /* default to S/PDIF */
1633 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1635 if (ucontrol->value.enumerated.item[0] == 1)
1636 source = HPI_AESEBU_FORMAT_SPDIF;
1637 if (ucontrol->value.enumerated.item[0] == 2)
1638 source = HPI_AESEBU_FORMAT_AESEBU;
1640 if (func(h_control, source) != 0)
1641 return -EINVAL;
1643 return 1;
1646 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1647 struct snd_ctl_elem_value *ucontrol) {
1648 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1649 hpi_aesebu_receiver_get_format);
1652 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol) {
1654 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1655 hpi_aesebu_receiver_set_format);
1658 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1659 struct snd_ctl_elem_info *uinfo)
1661 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1662 uinfo->count = 1;
1664 uinfo->value.integer.min = 0;
1665 uinfo->value.integer.max = 0X1F;
1666 uinfo->value.integer.step = 1;
1668 return 0;
1671 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1672 struct snd_ctl_elem_value *ucontrol) {
1674 u32 h_control = kcontrol->private_value;
1675 u16 status;
1677 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1678 h_control, &status));
1679 ucontrol->value.integer.value[0] = status;
1680 return 0;
1683 static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1684 struct hpi_control *hpi_ctl)
1686 struct snd_card *card = asihpi->card;
1687 struct snd_kcontrol_new snd_control;
1689 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1690 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1691 snd_control.info = snd_asihpi_aesebu_format_info;
1692 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1693 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1696 if (ctl_add(card, &snd_control, asihpi) < 0)
1697 return -EINVAL;
1699 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1700 snd_control.access =
1701 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1702 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1703 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1705 return ctl_add(card, &snd_control, asihpi);
1708 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1709 struct snd_ctl_elem_value *ucontrol) {
1710 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1711 hpi_aesebu_transmitter_get_format);
1714 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol) {
1716 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1717 hpi_aesebu_transmitter_set_format);
1721 static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1722 struct hpi_control *hpi_ctl)
1724 struct snd_card *card = asihpi->card;
1725 struct snd_kcontrol_new snd_control;
1727 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1728 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1729 snd_control.info = snd_asihpi_aesebu_format_info;
1730 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1731 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1733 return ctl_add(card, &snd_control, asihpi);
1736 /*------------------------------------------------------------
1737 Tuner controls
1738 ------------------------------------------------------------*/
1740 /* Gain */
1742 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1743 struct snd_ctl_elem_info *uinfo)
1745 u32 h_control = kcontrol->private_value;
1746 u16 err;
1747 short idx;
1748 u16 gain_range[3];
1750 for (idx = 0; idx < 3; idx++) {
1751 err = hpi_tuner_query_gain(h_control,
1752 idx, &gain_range[idx]);
1753 if (err != 0)
1754 return err;
1757 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1758 uinfo->count = 1;
1759 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1760 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1761 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1762 return 0;
1765 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol)
1769 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1771 u32 h_control = kcontrol->private_value;
1772 short gain;
1774 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1775 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1777 return 0;
1780 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1781 struct snd_ctl_elem_value *ucontrol)
1784 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1786 u32 h_control = kcontrol->private_value;
1787 short gain;
1789 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1790 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1792 return 1;
1795 /* Band */
1797 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1798 u16 *band_list, u32 len) {
1799 u32 h_control = kcontrol->private_value;
1800 u16 err = 0;
1801 u32 i;
1803 for (i = 0; i < len; i++) {
1804 err = hpi_tuner_query_band(
1805 h_control, i, &band_list[i]);
1806 if (err != 0)
1807 break;
1810 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1811 return -EIO;
1813 return i;
1816 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_info *uinfo)
1819 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1820 int num_bands = 0;
1822 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1823 HPI_TUNER_BAND_LAST);
1825 if (num_bands < 0)
1826 return num_bands;
1828 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1829 uinfo->count = 1;
1830 uinfo->value.enumerated.items = num_bands;
1832 if (num_bands > 0) {
1833 if (uinfo->value.enumerated.item >=
1834 uinfo->value.enumerated.items)
1835 uinfo->value.enumerated.item =
1836 uinfo->value.enumerated.items - 1;
1838 strcpy(uinfo->value.enumerated.name,
1839 asihpi_tuner_band_names[
1840 tuner_bands[uinfo->value.enumerated.item]]);
1843 return 0;
1846 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_value *ucontrol)
1849 u32 h_control = kcontrol->private_value;
1851 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1853 u16 band, idx;
1854 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1855 u32 num_bands = 0;
1857 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1858 HPI_TUNER_BAND_LAST);
1860 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1862 ucontrol->value.enumerated.item[0] = -1;
1863 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1864 if (tuner_bands[idx] == band) {
1865 ucontrol->value.enumerated.item[0] = idx;
1866 break;
1869 return 0;
1872 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1873 struct snd_ctl_elem_value *ucontrol)
1876 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1878 u32 h_control = kcontrol->private_value;
1879 u16 band;
1880 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1881 u32 num_bands = 0;
1883 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1884 HPI_TUNER_BAND_LAST);
1886 band = tuner_bands[ucontrol->value.enumerated.item[0]];
1887 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1889 return 1;
1892 /* Freq */
1894 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1895 struct snd_ctl_elem_info *uinfo)
1897 u32 h_control = kcontrol->private_value;
1898 u16 err;
1899 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1900 u16 num_bands = 0, band_iter, idx;
1901 u32 freq_range[3], temp_freq_range[3];
1903 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1904 HPI_TUNER_BAND_LAST);
1906 freq_range[0] = INT_MAX;
1907 freq_range[1] = 0;
1908 freq_range[2] = INT_MAX;
1910 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1911 for (idx = 0; idx < 3; idx++) {
1912 err = hpi_tuner_query_frequency(h_control,
1913 idx, tuner_bands[band_iter],
1914 &temp_freq_range[idx]);
1915 if (err != 0)
1916 return err;
1919 /* skip band with bogus stepping */
1920 if (temp_freq_range[2] <= 0)
1921 continue;
1923 if (temp_freq_range[0] < freq_range[0])
1924 freq_range[0] = temp_freq_range[0];
1925 if (temp_freq_range[1] > freq_range[1])
1926 freq_range[1] = temp_freq_range[1];
1927 if (temp_freq_range[2] < freq_range[2])
1928 freq_range[2] = temp_freq_range[2];
1931 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1932 uinfo->count = 1;
1933 uinfo->value.integer.min = ((int)freq_range[0]);
1934 uinfo->value.integer.max = ((int)freq_range[1]);
1935 uinfo->value.integer.step = ((int)freq_range[2]);
1936 return 0;
1939 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1940 struct snd_ctl_elem_value *ucontrol)
1942 u32 h_control = kcontrol->private_value;
1943 u32 freq;
1945 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1946 ucontrol->value.integer.value[0] = freq;
1948 return 0;
1951 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
1954 u32 h_control = kcontrol->private_value;
1955 u32 freq;
1957 freq = ucontrol->value.integer.value[0];
1958 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1960 return 1;
1963 /* Tuner control group initializer */
1964 static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1965 struct hpi_control *hpi_ctl)
1967 struct snd_card *card = asihpi->card;
1968 struct snd_kcontrol_new snd_control;
1970 snd_control.private_value = hpi_ctl->h_control;
1971 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1973 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
1974 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
1975 snd_control.info = snd_asihpi_tuner_gain_info;
1976 snd_control.get = snd_asihpi_tuner_gain_get;
1977 snd_control.put = snd_asihpi_tuner_gain_put;
1979 if (ctl_add(card, &snd_control, asihpi) < 0)
1980 return -EINVAL;
1983 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
1984 snd_control.info = snd_asihpi_tuner_band_info;
1985 snd_control.get = snd_asihpi_tuner_band_get;
1986 snd_control.put = snd_asihpi_tuner_band_put;
1988 if (ctl_add(card, &snd_control, asihpi) < 0)
1989 return -EINVAL;
1991 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
1992 snd_control.info = snd_asihpi_tuner_freq_info;
1993 snd_control.get = snd_asihpi_tuner_freq_get;
1994 snd_control.put = snd_asihpi_tuner_freq_put;
1996 return ctl_add(card, &snd_control, asihpi);
1999 /*------------------------------------------------------------
2000 Meter controls
2001 ------------------------------------------------------------*/
2002 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_info *uinfo)
2005 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2006 uinfo->count = HPI_MAX_CHANNELS;
2007 uinfo->value.integer.min = 0;
2008 uinfo->value.integer.max = 0x7FFFFFFF;
2009 return 0;
2012 /* linear values for 10dB steps */
2013 static int log2lin[] = {
2014 0x7FFFFFFF, /* 0dB */
2015 679093956,
2016 214748365,
2017 67909396,
2018 21474837,
2019 6790940,
2020 2147484, /* -60dB */
2021 679094,
2022 214748, /* -80 */
2023 67909,
2024 21475, /* -100 */
2025 6791,
2026 2147,
2027 679,
2028 214,
2035 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2036 struct snd_ctl_elem_value *ucontrol)
2038 u32 h_control = kcontrol->private_value;
2039 short an_gain_mB[HPI_MAX_CHANNELS], i;
2040 u16 err;
2042 err = hpi_meter_get_peak(h_control, an_gain_mB);
2044 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2045 if (err) {
2046 ucontrol->value.integer.value[i] = 0;
2047 } else if (an_gain_mB[i] >= 0) {
2048 ucontrol->value.integer.value[i] =
2049 an_gain_mB[i] << 16;
2050 } else {
2051 /* -ve is log value in millibels < -60dB,
2052 * convert to (roughly!) linear,
2054 ucontrol->value.integer.value[i] =
2055 log2lin[an_gain_mB[i] / -1000];
2058 return 0;
2061 static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2062 struct hpi_control *hpi_ctl, int subidx)
2064 struct snd_card *card = asihpi->card;
2065 struct snd_kcontrol_new snd_control;
2067 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2068 snd_control.access =
2069 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2070 snd_control.info = snd_asihpi_meter_info;
2071 snd_control.get = snd_asihpi_meter_get;
2073 snd_control.index = subidx;
2075 return ctl_add(card, &snd_control, asihpi);
2078 /*------------------------------------------------------------
2079 Multiplexer controls
2080 ------------------------------------------------------------*/
2081 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2083 u32 h_control = snd_control->private_value;
2084 struct hpi_control hpi_ctl;
2085 int s, err;
2086 for (s = 0; s < 32; s++) {
2087 err = hpi_multiplexer_query_source(h_control, s,
2088 &hpi_ctl.
2089 src_node_type,
2090 &hpi_ctl.
2091 src_node_index);
2092 if (err)
2093 break;
2095 return s;
2098 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2099 struct snd_ctl_elem_info *uinfo)
2101 int err;
2102 u16 src_node_type, src_node_index;
2103 u32 h_control = kcontrol->private_value;
2105 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2106 uinfo->count = 1;
2107 uinfo->value.enumerated.items =
2108 snd_card_asihpi_mux_count_sources(kcontrol);
2110 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2111 uinfo->value.enumerated.item =
2112 uinfo->value.enumerated.items - 1;
2114 err =
2115 hpi_multiplexer_query_source(h_control,
2116 uinfo->value.enumerated.item,
2117 &src_node_type, &src_node_index);
2119 sprintf(uinfo->value.enumerated.name, "%s %d",
2120 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2121 src_node_index);
2122 return 0;
2125 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2126 struct snd_ctl_elem_value *ucontrol)
2128 u32 h_control = kcontrol->private_value;
2129 u16 source_type, source_index;
2130 u16 src_node_type, src_node_index;
2131 int s;
2133 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2134 &source_type, &source_index));
2135 /* Should cache this search result! */
2136 for (s = 0; s < 256; s++) {
2137 if (hpi_multiplexer_query_source(h_control, s,
2138 &src_node_type, &src_node_index))
2139 break;
2141 if ((source_type == src_node_type)
2142 && (source_index == src_node_index)) {
2143 ucontrol->value.enumerated.item[0] = s;
2144 return 0;
2147 snd_printd(KERN_WARNING
2148 "Control %x failed to match mux source %hu %hu\n",
2149 h_control, source_type, source_index);
2150 ucontrol->value.enumerated.item[0] = 0;
2151 return 0;
2154 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2155 struct snd_ctl_elem_value *ucontrol)
2157 int change;
2158 u32 h_control = kcontrol->private_value;
2159 u16 source_type, source_index;
2160 u16 e;
2162 change = 1;
2164 e = hpi_multiplexer_query_source(h_control,
2165 ucontrol->value.enumerated.item[0],
2166 &source_type, &source_index);
2167 if (!e)
2168 hpi_handle_error(
2169 hpi_multiplexer_set_source(h_control,
2170 source_type, source_index));
2171 return change;
2175 static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2176 struct hpi_control *hpi_ctl)
2178 struct snd_card *card = asihpi->card;
2179 struct snd_kcontrol_new snd_control;
2181 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2182 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2183 snd_control.info = snd_asihpi_mux_info;
2184 snd_control.get = snd_asihpi_mux_get;
2185 snd_control.put = snd_asihpi_mux_put;
2187 return ctl_add(card, &snd_control, asihpi);
2191 /*------------------------------------------------------------
2192 Channel mode controls
2193 ------------------------------------------------------------*/
2194 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2195 struct snd_ctl_elem_info *uinfo)
2197 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2198 "invalid",
2199 "Normal", "Swap",
2200 "From Left", "From Right",
2201 "To Left", "To Right"
2204 u32 h_control = kcontrol->private_value;
2205 u16 mode;
2206 int i;
2207 u16 mode_map[6];
2208 int valid_modes = 0;
2210 /* HPI channel mode values can be from 1 to 6
2211 Some adapters only support a contiguous subset
2213 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2214 if (!hpi_channel_mode_query_mode(
2215 h_control, i, &mode)) {
2216 mode_map[valid_modes] = mode;
2217 valid_modes++;
2220 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2221 uinfo->count = 1;
2222 uinfo->value.enumerated.items = valid_modes;
2224 if (uinfo->value.enumerated.item >= valid_modes)
2225 uinfo->value.enumerated.item = valid_modes - 1;
2227 strcpy(uinfo->value.enumerated.name,
2228 mode_names[mode_map[uinfo->value.enumerated.item]]);
2230 return 0;
2233 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2234 struct snd_ctl_elem_value *ucontrol)
2236 u32 h_control = kcontrol->private_value;
2237 u16 mode;
2239 if (hpi_channel_mode_get(h_control, &mode))
2240 mode = 1;
2242 ucontrol->value.enumerated.item[0] = mode - 1;
2244 return 0;
2247 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2248 struct snd_ctl_elem_value *ucontrol)
2250 int change;
2251 u32 h_control = kcontrol->private_value;
2253 change = 1;
2255 hpi_handle_error(hpi_channel_mode_set(h_control,
2256 ucontrol->value.enumerated.item[0] + 1));
2257 return change;
2261 static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2262 struct hpi_control *hpi_ctl)
2264 struct snd_card *card = asihpi->card;
2265 struct snd_kcontrol_new snd_control;
2267 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2268 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2269 snd_control.info = snd_asihpi_cmode_info;
2270 snd_control.get = snd_asihpi_cmode_get;
2271 snd_control.put = snd_asihpi_cmode_put;
2273 return ctl_add(card, &snd_control, asihpi);
2276 /*------------------------------------------------------------
2277 Sampleclock source controls
2278 ------------------------------------------------------------*/
2279 static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2280 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2281 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2282 "Prev Module",
2283 "Digital2", "Digital3", "Digital4", "Digital5",
2284 "Digital6", "Digital7", "Digital8"};
2286 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2287 struct snd_ctl_elem_info *uinfo)
2289 struct snd_card_asihpi *asihpi =
2290 (struct snd_card_asihpi *)(kcontrol->private_data);
2291 struct clk_cache *clkcache = &asihpi->cc;
2292 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2293 uinfo->count = 1;
2294 uinfo->value.enumerated.items = clkcache->count;
2296 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2297 uinfo->value.enumerated.item =
2298 uinfo->value.enumerated.items - 1;
2300 strcpy(uinfo->value.enumerated.name,
2301 clkcache->s[uinfo->value.enumerated.item].name);
2302 return 0;
2305 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2306 struct snd_ctl_elem_value *ucontrol)
2308 struct snd_card_asihpi *asihpi =
2309 (struct snd_card_asihpi *)(kcontrol->private_data);
2310 struct clk_cache *clkcache = &asihpi->cc;
2311 u32 h_control = kcontrol->private_value;
2312 u16 source, srcindex = 0;
2313 int i;
2315 ucontrol->value.enumerated.item[0] = 0;
2316 if (hpi_sample_clock_get_source(h_control, &source))
2317 source = 0;
2319 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2320 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2321 srcindex = 0;
2323 for (i = 0; i < clkcache->count; i++)
2324 if ((clkcache->s[i].source == source) &&
2325 (clkcache->s[i].index == srcindex))
2326 break;
2328 ucontrol->value.enumerated.item[0] = i;
2330 return 0;
2333 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_value *ucontrol)
2336 struct snd_card_asihpi *asihpi =
2337 (struct snd_card_asihpi *)(kcontrol->private_data);
2338 struct clk_cache *clkcache = &asihpi->cc;
2339 int change, item;
2340 u32 h_control = kcontrol->private_value;
2342 change = 1;
2343 item = ucontrol->value.enumerated.item[0];
2344 if (item >= clkcache->count)
2345 item = clkcache->count-1;
2347 hpi_handle_error(hpi_sample_clock_set_source(
2348 h_control, clkcache->s[item].source));
2350 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2351 hpi_handle_error(hpi_sample_clock_set_source_index(
2352 h_control, clkcache->s[item].index));
2353 return change;
2356 /*------------------------------------------------------------
2357 Clkrate controls
2358 ------------------------------------------------------------*/
2359 /* Need to change this to enumerated control with list of rates */
2360 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2361 struct snd_ctl_elem_info *uinfo)
2363 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2364 uinfo->count = 1;
2365 uinfo->value.integer.min = 8000;
2366 uinfo->value.integer.max = 192000;
2367 uinfo->value.integer.step = 100;
2369 return 0;
2372 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2373 struct snd_ctl_elem_value *ucontrol)
2375 u32 h_control = kcontrol->private_value;
2376 u32 rate;
2377 u16 e;
2379 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2380 if (!e)
2381 ucontrol->value.integer.value[0] = rate;
2382 else
2383 ucontrol->value.integer.value[0] = 0;
2384 return 0;
2387 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2390 int change;
2391 u32 h_control = kcontrol->private_value;
2393 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2394 asihpi->mixer_clkrate[addr][1] != right;
2396 change = 1;
2397 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2398 ucontrol->value.integer.value[0]));
2399 return change;
2402 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_info *uinfo)
2405 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2406 uinfo->count = 1;
2407 uinfo->value.integer.min = 8000;
2408 uinfo->value.integer.max = 192000;
2409 uinfo->value.integer.step = 100;
2411 return 0;
2414 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2415 struct snd_ctl_elem_value *ucontrol)
2417 u32 h_control = kcontrol->private_value;
2418 u32 rate;
2419 u16 e;
2421 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2422 if (!e)
2423 ucontrol->value.integer.value[0] = rate;
2424 else
2425 ucontrol->value.integer.value[0] = 0;
2426 return 0;
2429 static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2430 struct hpi_control *hpi_ctl)
2432 struct snd_card *card = asihpi->card;
2433 struct snd_kcontrol_new snd_control;
2435 struct clk_cache *clkcache = &asihpi->cc;
2436 u32 hSC = hpi_ctl->h_control;
2437 int has_aes_in = 0;
2438 int i, j;
2439 u16 source;
2441 snd_control.private_value = hpi_ctl->h_control;
2443 clkcache->has_local = 0;
2445 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2446 if (hpi_sample_clock_query_source(hSC,
2447 i, &source))
2448 break;
2449 clkcache->s[i].source = source;
2450 clkcache->s[i].index = 0;
2451 clkcache->s[i].name = sampleclock_sources[source];
2452 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2453 has_aes_in = 1;
2454 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2455 clkcache->has_local = 1;
2457 if (has_aes_in)
2458 /* already will have picked up index 0 above */
2459 for (j = 1; j < 8; j++) {
2460 if (hpi_sample_clock_query_source_index(hSC,
2461 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2462 &source))
2463 break;
2464 clkcache->s[i].source =
2465 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2466 clkcache->s[i].index = j;
2467 clkcache->s[i].name = sampleclock_sources[
2468 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2469 i++;
2471 clkcache->count = i;
2473 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2474 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2475 snd_control.info = snd_asihpi_clksrc_info;
2476 snd_control.get = snd_asihpi_clksrc_get;
2477 snd_control.put = snd_asihpi_clksrc_put;
2478 if (ctl_add(card, &snd_control, asihpi) < 0)
2479 return -EINVAL;
2482 if (clkcache->has_local) {
2483 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2484 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2485 snd_control.info = snd_asihpi_clklocal_info;
2486 snd_control.get = snd_asihpi_clklocal_get;
2487 snd_control.put = snd_asihpi_clklocal_put;
2490 if (ctl_add(card, &snd_control, asihpi) < 0)
2491 return -EINVAL;
2494 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2495 snd_control.access =
2496 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2497 snd_control.info = snd_asihpi_clkrate_info;
2498 snd_control.get = snd_asihpi_clkrate_get;
2500 return ctl_add(card, &snd_control, asihpi);
2502 /*------------------------------------------------------------
2503 Mixer
2504 ------------------------------------------------------------*/
2506 static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2508 struct snd_card *card = asihpi->card;
2509 unsigned int idx = 0;
2510 unsigned int subindex = 0;
2511 int err;
2512 struct hpi_control hpi_ctl, prev_ctl;
2514 if (snd_BUG_ON(!asihpi))
2515 return -EINVAL;
2516 strcpy(card->mixername, "Asihpi Mixer");
2518 err =
2519 hpi_mixer_open(asihpi->adapter_index,
2520 &asihpi->h_mixer);
2521 hpi_handle_error(err);
2522 if (err)
2523 return -err;
2525 memset(&prev_ctl, 0, sizeof(prev_ctl));
2526 prev_ctl.control_type = -1;
2528 for (idx = 0; idx < 2000; idx++) {
2529 err = hpi_mixer_get_control_by_index(
2530 asihpi->h_mixer,
2531 idx,
2532 &hpi_ctl.src_node_type,
2533 &hpi_ctl.src_node_index,
2534 &hpi_ctl.dst_node_type,
2535 &hpi_ctl.dst_node_index,
2536 &hpi_ctl.control_type,
2537 &hpi_ctl.h_control);
2538 if (err) {
2539 if (err == HPI_ERROR_CONTROL_DISABLED) {
2540 if (mixer_dump)
2541 snd_printk(KERN_INFO
2542 "Disabled HPI Control(%d)\n",
2543 idx);
2544 continue;
2545 } else
2546 break;
2550 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2551 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2553 /* ASI50xx in SSX mode has multiple meters on the same node.
2554 Use subindex to create distinct ALSA controls
2555 for any duplicated controls.
2557 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2558 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2559 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2560 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2561 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2562 subindex++;
2563 else
2564 subindex = 0;
2566 prev_ctl = hpi_ctl;
2568 switch (hpi_ctl.control_type) {
2569 case HPI_CONTROL_VOLUME:
2570 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2571 break;
2572 case HPI_CONTROL_LEVEL:
2573 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2574 break;
2575 case HPI_CONTROL_MULTIPLEXER:
2576 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2577 break;
2578 case HPI_CONTROL_CHANNEL_MODE:
2579 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2580 break;
2581 case HPI_CONTROL_METER:
2582 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2583 break;
2584 case HPI_CONTROL_SAMPLECLOCK:
2585 err = snd_asihpi_sampleclock_add(
2586 asihpi, &hpi_ctl);
2587 break;
2588 case HPI_CONTROL_CONNECTION: /* ignore these */
2589 continue;
2590 case HPI_CONTROL_TUNER:
2591 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2592 break;
2593 case HPI_CONTROL_AESEBU_TRANSMITTER:
2594 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2595 break;
2596 case HPI_CONTROL_AESEBU_RECEIVER:
2597 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2598 break;
2599 case HPI_CONTROL_VOX:
2600 case HPI_CONTROL_BITSTREAM:
2601 case HPI_CONTROL_MICROPHONE:
2602 case HPI_CONTROL_PARAMETRIC_EQ:
2603 case HPI_CONTROL_COMPANDER:
2604 default:
2605 if (mixer_dump)
2606 snd_printk(KERN_INFO
2607 "Untranslated HPI Control"
2608 "(%d) %d %d %d %d %d\n",
2609 idx,
2610 hpi_ctl.control_type,
2611 hpi_ctl.src_node_type,
2612 hpi_ctl.src_node_index,
2613 hpi_ctl.dst_node_type,
2614 hpi_ctl.dst_node_index);
2615 continue;
2617 if (err < 0)
2618 return err;
2620 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2621 hpi_handle_error(err);
2623 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2625 return 0;
2628 /*------------------------------------------------------------
2629 /proc interface
2630 ------------------------------------------------------------*/
2632 static void
2633 snd_asihpi_proc_read(struct snd_info_entry *entry,
2634 struct snd_info_buffer *buffer)
2636 struct snd_card_asihpi *asihpi = entry->private_data;
2637 u16 version;
2638 u32 h_control;
2639 u32 rate = 0;
2640 u16 source = 0;
2641 int err;
2643 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2644 snd_iprintf(buffer,
2645 "adapter ID=%4X\n_index=%d\n"
2646 "num_outstreams=%d\n_num_instreams=%d\n",
2647 asihpi->type, asihpi->adapter_index,
2648 asihpi->num_outstreams, asihpi->num_instreams);
2650 version = asihpi->version;
2651 snd_iprintf(buffer,
2652 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2653 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2654 version & 0x7,
2655 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2657 err = hpi_mixer_get_control(asihpi->h_mixer,
2658 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2659 HPI_CONTROL_SAMPLECLOCK, &h_control);
2661 if (!err) {
2662 err = hpi_sample_clock_get_sample_rate(
2663 h_control, &rate);
2664 err += hpi_sample_clock_get_source(h_control, &source);
2666 if (!err)
2667 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2668 rate, sampleclock_sources[source]);
2674 static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2676 struct snd_info_entry *entry;
2678 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2679 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2682 /*------------------------------------------------------------
2683 HWDEP
2684 ------------------------------------------------------------*/
2686 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2688 if (enable_hpi_hwdep)
2689 return 0;
2690 else
2691 return -ENODEV;
2695 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2697 if (enable_hpi_hwdep)
2698 return asihpi_hpi_release(file);
2699 else
2700 return -ENODEV;
2703 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2704 unsigned int cmd, unsigned long arg)
2706 if (enable_hpi_hwdep)
2707 return asihpi_hpi_ioctl(file, cmd, arg);
2708 else
2709 return -ENODEV;
2713 /* results in /dev/snd/hwC#D0 file for each card with index #
2714 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2716 static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2717 int device, struct snd_hwdep **rhwdep)
2719 struct snd_hwdep *hw;
2720 int err;
2722 if (rhwdep)
2723 *rhwdep = NULL;
2724 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2725 if (err < 0)
2726 return err;
2727 strcpy(hw->name, "asihpi (HPI)");
2728 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2729 hw->ops.open = snd_asihpi_hpi_open;
2730 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2731 hw->ops.release = snd_asihpi_hpi_release;
2732 hw->private_data = asihpi;
2733 if (rhwdep)
2734 *rhwdep = hw;
2735 return 0;
2738 /*------------------------------------------------------------
2739 CARD
2740 ------------------------------------------------------------*/
2741 static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2742 const struct pci_device_id *pci_id)
2744 int err;
2746 u16 version;
2747 int pcm_substreams;
2749 struct hpi_adapter *hpi_card;
2750 struct snd_card *card;
2751 struct snd_card_asihpi *asihpi;
2753 u32 h_control;
2754 u32 h_stream;
2756 static int dev;
2757 if (dev >= SNDRV_CARDS)
2758 return -ENODEV;
2760 /* Should this be enable[hpi_card->index] ? */
2761 if (!enable[dev]) {
2762 dev++;
2763 return -ENOENT;
2766 err = asihpi_adapter_probe(pci_dev, pci_id);
2767 if (err < 0)
2768 return err;
2770 hpi_card = pci_get_drvdata(pci_dev);
2771 /* first try to give the card the same index as its hardware index */
2772 err = snd_card_create(hpi_card->index,
2773 id[hpi_card->index], THIS_MODULE,
2774 sizeof(struct snd_card_asihpi),
2775 &card);
2776 if (err < 0) {
2777 /* if that fails, try the default index==next available */
2778 err =
2779 snd_card_create(index[dev], id[dev],
2780 THIS_MODULE,
2781 sizeof(struct snd_card_asihpi),
2782 &card);
2783 if (err < 0)
2784 return err;
2785 snd_printk(KERN_WARNING
2786 "**** WARNING **** Adapter index %d->ALSA index %d\n",
2787 hpi_card->index, card->number);
2790 snd_card_set_dev(card, &pci_dev->dev);
2792 asihpi = (struct snd_card_asihpi *) card->private_data;
2793 asihpi->card = card;
2794 asihpi->pci = pci_dev;
2795 asihpi->adapter_index = hpi_card->index;
2796 hpi_handle_error(hpi_adapter_get_info(
2797 asihpi->adapter_index,
2798 &asihpi->num_outstreams,
2799 &asihpi->num_instreams,
2800 &asihpi->version,
2801 &asihpi->serial_number, &asihpi->type));
2803 version = asihpi->version;
2804 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2805 "num_instreams=%d S/N=%d\n"
2806 "Hw Version %c%d DSP code version %03d\n",
2807 asihpi->type, asihpi->adapter_index,
2808 asihpi->num_outstreams,
2809 asihpi->num_instreams, asihpi->serial_number,
2810 ((version >> 3) & 0xf) + 'A',
2811 version & 0x7,
2812 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2814 pcm_substreams = asihpi->num_outstreams;
2815 if (pcm_substreams < asihpi->num_instreams)
2816 pcm_substreams = asihpi->num_instreams;
2818 err = hpi_adapter_get_property(asihpi->adapter_index,
2819 HPI_ADAPTER_PROPERTY_CAPS1,
2820 NULL, &asihpi->support_grouping);
2821 if (err)
2822 asihpi->support_grouping = 0;
2824 err = hpi_adapter_get_property(asihpi->adapter_index,
2825 HPI_ADAPTER_PROPERTY_CAPS2,
2826 &asihpi->support_mrx, NULL);
2827 if (err)
2828 asihpi->support_mrx = 0;
2830 err = hpi_adapter_get_property(asihpi->adapter_index,
2831 HPI_ADAPTER_PROPERTY_INTERVAL,
2832 NULL, &asihpi->update_interval_frames);
2833 if (err)
2834 asihpi->update_interval_frames = 512;
2836 if (!asihpi->can_dma)
2837 asihpi->update_interval_frames *= 2;
2839 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
2840 0, &h_stream));
2842 err = hpi_instream_host_buffer_free(h_stream);
2843 asihpi->can_dma = (!err);
2845 hpi_handle_error(hpi_instream_close(h_stream));
2847 err = hpi_adapter_get_property(asihpi->adapter_index,
2848 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2849 &asihpi->in_max_chans, &asihpi->out_max_chans);
2850 if (err) {
2851 asihpi->in_max_chans = 2;
2852 asihpi->out_max_chans = 2;
2855 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2856 asihpi->can_dma,
2857 asihpi->support_grouping,
2858 asihpi->support_mrx
2861 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2862 if (err < 0) {
2863 snd_printk(KERN_ERR "pcm_new failed\n");
2864 goto __nodev;
2866 err = snd_card_asihpi_mixer_new(asihpi);
2867 if (err < 0) {
2868 snd_printk(KERN_ERR "mixer_new failed\n");
2869 goto __nodev;
2872 err = hpi_mixer_get_control(asihpi->h_mixer,
2873 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2874 HPI_CONTROL_SAMPLECLOCK, &h_control);
2876 if (!err)
2877 err = hpi_sample_clock_set_local_rate(
2878 h_control, adapter_fs);
2880 snd_asihpi_proc_init(asihpi);
2882 /* always create, can be enabled or disabled dynamically
2883 by enable_hwdep module param*/
2884 snd_asihpi_hpi_new(asihpi, 0, NULL);
2886 strcpy(card->driver, "ASIHPI");
2888 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2889 sprintf(card->longname, "%s %i",
2890 card->shortname, asihpi->adapter_index);
2891 err = snd_card_register(card);
2893 if (!err) {
2894 hpi_card->snd_card_asihpi = card;
2895 dev++;
2896 return 0;
2898 __nodev:
2899 snd_card_free(card);
2900 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2901 return err;
2905 static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2907 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2909 snd_card_free(hpi_card->snd_card_asihpi);
2910 hpi_card->snd_card_asihpi = NULL;
2911 asihpi_adapter_remove(pci_dev);
2914 static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2915 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2916 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2917 (kernel_ulong_t)HPI_6205},
2918 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2919 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2920 (kernel_ulong_t)HPI_6000},
2921 {0,}
2923 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2925 static struct pci_driver driver = {
2926 .name = "asihpi",
2927 .id_table = asihpi_pci_tbl,
2928 .probe = snd_asihpi_probe,
2929 .remove = __devexit_p(snd_asihpi_remove),
2930 #ifdef CONFIG_PM
2931 /* .suspend = snd_asihpi_suspend,
2932 .resume = snd_asihpi_resume, */
2933 #endif
2936 static int __init snd_asihpi_init(void)
2938 asihpi_init();
2939 return pci_register_driver(&driver);
2942 static void __exit snd_asihpi_exit(void)
2945 pci_unregister_driver(&driver);
2946 asihpi_exit();
2949 module_init(snd_asihpi_init)
2950 module_exit(snd_asihpi_exit)