Merge tag 'powerpc-5.11-3' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[linux/fpc-iii.git] / sound / pci / asihpi / asihpi.c
blob5e1f9f10051bcb01d78451ddfd39079d93665f45
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Asihpi soundcard
4 * Copyright (c) by AudioScience Inc <support@audioscience.com>
6 * The following is not a condition of use, merely a request:
7 * If you modify this program, particularly if you fix errors, AudioScience Inc
8 * would appreciate it if you grant us the right to use those modifications
9 * for any purpose including commercial applications.
12 #include "hpi_internal.h"
13 #include "hpi_version.h"
14 #include "hpimsginit.h"
15 #include "hpioctl.h"
16 #include "hpicmn.h"
18 #include <linux/pci.h>
19 #include <linux/init.h>
20 #include <linux/jiffies.h>
21 #include <linux/slab.h>
22 #include <linux/time.h>
23 #include <linux/wait.h>
24 #include <linux/module.h>
25 #include <sound/core.h>
26 #include <sound/control.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/info.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32 #include <sound/hwdep.h>
34 MODULE_LICENSE("GPL");
35 MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
36 MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
37 HPI_VER_STRING);
39 #if defined CONFIG_SND_DEBUG_VERBOSE
40 /**
41 * snd_printddd - very verbose debug printk
42 * @format: format string
44 * Works like snd_printk() for debugging purposes.
45 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
46 * Must set snd module debug parameter to 3 to enable at runtime.
48 #define snd_printddd(format, args...) \
49 __snd_printk(3, __FILE__, __LINE__, format, ##args)
50 #else
51 #define snd_printddd(format, args...) do { } while (0)
52 #endif
54 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
55 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
56 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
57 static bool enable_hpi_hwdep = 1;
59 module_param_array(index, int, NULL, 0444);
60 MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
62 module_param_array(id, charp, NULL, 0444);
63 MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
65 module_param_array(enable, bool, NULL, 0444);
66 MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
68 module_param(enable_hpi_hwdep, bool, 0644);
69 MODULE_PARM_DESC(enable_hpi_hwdep,
70 "ALSA enable HPI hwdep for AudioScience soundcard ");
72 /* identify driver */
73 #ifdef KERNEL_ALSA_BUILD
74 static char *build_info = "Built using headers from kernel source";
75 module_param(build_info, charp, 0444);
76 MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
77 #else
78 static char *build_info = "Built within ALSA source";
79 module_param(build_info, charp, 0444);
80 MODULE_PARM_DESC(build_info, "Built within ALSA source");
81 #endif
83 /* set to 1 to dump every control from adapter to log */
84 static const int mixer_dump;
86 #define DEFAULT_SAMPLERATE 44100
87 static int adapter_fs = DEFAULT_SAMPLERATE;
89 /* defaults */
90 #define PERIODS_MIN 2
91 #define PERIOD_BYTES_MIN 2048
92 #define BUFFER_BYTES_MAX (512 * 1024)
94 #define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
96 struct clk_source {
97 int source;
98 int index;
99 const char *name;
102 struct clk_cache {
103 int count;
104 int has_local;
105 struct clk_source s[MAX_CLOCKSOURCES];
108 /* Per card data */
109 struct snd_card_asihpi {
110 struct snd_card *card;
111 struct pci_dev *pci;
112 struct hpi_adapter *hpi;
114 /* In low latency mode there is only one stream, a pointer to its
115 * private data is stored here on trigger and cleared on stop.
116 * The interrupt handler uses it as a parameter when calling
117 * snd_card_asihpi_timer_function().
119 struct snd_card_asihpi_pcm *llmode_streampriv;
120 void (*pcm_start)(struct snd_pcm_substream *substream);
121 void (*pcm_stop)(struct snd_pcm_substream *substream);
123 u32 h_mixer;
124 struct clk_cache cc;
126 u16 can_dma;
127 u16 support_grouping;
128 u16 support_mrx;
129 u16 update_interval_frames;
130 u16 in_max_chans;
131 u16 out_max_chans;
132 u16 in_min_chans;
133 u16 out_min_chans;
136 /* Per stream data */
137 struct snd_card_asihpi_pcm {
138 struct timer_list timer;
139 unsigned int respawn_timer;
140 unsigned int hpi_buffer_attached;
141 unsigned int buffer_bytes;
142 unsigned int period_bytes;
143 unsigned int bytes_per_sec;
144 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
145 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
146 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
147 unsigned int drained_count;
148 struct snd_pcm_substream *substream;
149 u32 h_stream;
150 struct hpi_format format;
153 /* universal stream verbs work with out or in stream handles */
155 /* Functions to allow driver to give a buffer to HPI for busmastering */
157 static u16 hpi_stream_host_buffer_attach(
158 u32 h_stream, /* handle to outstream. */
159 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
160 u32 pci_address
163 struct hpi_message hm;
164 struct hpi_response hr;
165 unsigned int obj = hpi_handle_object(h_stream);
167 if (!h_stream)
168 return HPI_ERROR_INVALID_OBJ;
169 hpi_init_message_response(&hm, &hr, obj,
170 obj == HPI_OBJ_OSTREAM ?
171 HPI_OSTREAM_HOSTBUFFER_ALLOC :
172 HPI_ISTREAM_HOSTBUFFER_ALLOC);
174 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
175 &hm.obj_index);
177 hm.u.d.u.buffer.buffer_size = size_in_bytes;
178 hm.u.d.u.buffer.pci_address = pci_address;
179 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
180 hpi_send_recv(&hm, &hr);
181 return hr.error;
184 static u16 hpi_stream_host_buffer_detach(u32 h_stream)
186 struct hpi_message hm;
187 struct hpi_response hr;
188 unsigned int obj = hpi_handle_object(h_stream);
190 if (!h_stream)
191 return HPI_ERROR_INVALID_OBJ;
193 hpi_init_message_response(&hm, &hr, obj,
194 obj == HPI_OBJ_OSTREAM ?
195 HPI_OSTREAM_HOSTBUFFER_FREE :
196 HPI_ISTREAM_HOSTBUFFER_FREE);
198 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
199 &hm.obj_index);
200 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
201 hpi_send_recv(&hm, &hr);
202 return hr.error;
205 static inline u16 hpi_stream_start(u32 h_stream)
207 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
208 return hpi_outstream_start(h_stream);
209 else
210 return hpi_instream_start(h_stream);
213 static inline u16 hpi_stream_stop(u32 h_stream)
215 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
216 return hpi_outstream_stop(h_stream);
217 else
218 return hpi_instream_stop(h_stream);
221 static inline u16 hpi_stream_get_info_ex(
222 u32 h_stream,
223 u16 *pw_state,
224 u32 *pbuffer_size,
225 u32 *pdata_in_buffer,
226 u32 *psample_count,
227 u32 *pauxiliary_data
230 u16 e;
231 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
232 e = hpi_outstream_get_info_ex(h_stream, pw_state,
233 pbuffer_size, pdata_in_buffer,
234 psample_count, pauxiliary_data);
235 else
236 e = hpi_instream_get_info_ex(h_stream, pw_state,
237 pbuffer_size, pdata_in_buffer,
238 psample_count, pauxiliary_data);
239 return e;
242 static inline u16 hpi_stream_group_add(
243 u32 h_master,
244 u32 h_stream)
246 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
247 return hpi_outstream_group_add(h_master, h_stream);
248 else
249 return hpi_instream_group_add(h_master, h_stream);
252 static inline u16 hpi_stream_group_reset(u32 h_stream)
254 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
255 return hpi_outstream_group_reset(h_stream);
256 else
257 return hpi_instream_group_reset(h_stream);
260 static u16 handle_error(u16 err, int line, char *filename)
262 if (err)
263 printk(KERN_WARNING
264 "in file %s, line %d: HPI error %d\n",
265 filename, line, err);
266 return err;
269 #define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
271 /***************************** GENERAL PCM ****************/
273 static void print_hwparams(struct snd_pcm_substream *substream,
274 struct snd_pcm_hw_params *p)
276 char name[16];
277 snd_pcm_debug_name(substream, name, sizeof(name));
278 snd_printdd("%s HWPARAMS\n", name);
279 snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
280 params_rate(p), params_channels(p),
281 params_format(p), params_subformat(p));
282 snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
283 params_buffer_bytes(p), params_period_bytes(p),
284 params_period_size(p), params_periods(p));
285 snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
286 params_buffer_size(p), params_access(p),
287 params_rate(p) * params_channels(p) *
288 snd_pcm_format_width(params_format(p)) / 8);
291 #define INVALID_FORMAT (__force snd_pcm_format_t)(-1)
293 static const snd_pcm_format_t hpi_to_alsa_formats[] = {
294 INVALID_FORMAT, /* INVALID */
295 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
296 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
297 INVALID_FORMAT, /* HPI_FORMAT_MPEG_L1 3 */
298 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
299 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
300 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC2 6 */
301 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC3 7 */
302 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
303 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
304 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
305 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
306 INVALID_FORMAT, /* HPI_FORMAT_RAW_BITSTREAM 12 */
307 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
308 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
309 #if 1
310 /* ALSA can't handle 3 byte sample size together with power-of-2
311 * constraint on buffer_bytes, so disable this format
313 INVALID_FORMAT
314 #else
315 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
316 #endif
320 static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
321 u16 *hpi_format)
323 u16 format;
325 for (format = HPI_FORMAT_PCM8_UNSIGNED;
326 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
327 if (hpi_to_alsa_formats[format] == alsa_format) {
328 *hpi_format = format;
329 return 0;
333 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
334 alsa_format);
335 *hpi_format = 0;
336 return -EINVAL;
339 static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
340 struct snd_pcm_hardware *pcmhw)
342 u16 err;
343 u32 h_control;
344 u32 sample_rate;
345 int idx;
346 unsigned int rate_min = 200000;
347 unsigned int rate_max = 0;
348 unsigned int rates = 0;
350 if (asihpi->support_mrx) {
351 rates |= SNDRV_PCM_RATE_CONTINUOUS;
352 rates |= SNDRV_PCM_RATE_8000_96000;
353 rate_min = 8000;
354 rate_max = 100000;
355 } else {
356 /* on cards without SRC,
357 valid rates are determined by sampleclock */
358 err = hpi_mixer_get_control(asihpi->h_mixer,
359 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
360 HPI_CONTROL_SAMPLECLOCK, &h_control);
361 if (err) {
362 dev_err(&asihpi->pci->dev,
363 "No local sampleclock, err %d\n", err);
366 for (idx = -1; idx < 100; idx++) {
367 if (idx == -1) {
368 if (hpi_sample_clock_get_sample_rate(h_control,
369 &sample_rate))
370 continue;
371 } else if (hpi_sample_clock_query_local_rate(h_control,
372 idx, &sample_rate)) {
373 break;
376 rate_min = min(rate_min, sample_rate);
377 rate_max = max(rate_max, sample_rate);
379 switch (sample_rate) {
380 case 5512:
381 rates |= SNDRV_PCM_RATE_5512;
382 break;
383 case 8000:
384 rates |= SNDRV_PCM_RATE_8000;
385 break;
386 case 11025:
387 rates |= SNDRV_PCM_RATE_11025;
388 break;
389 case 16000:
390 rates |= SNDRV_PCM_RATE_16000;
391 break;
392 case 22050:
393 rates |= SNDRV_PCM_RATE_22050;
394 break;
395 case 32000:
396 rates |= SNDRV_PCM_RATE_32000;
397 break;
398 case 44100:
399 rates |= SNDRV_PCM_RATE_44100;
400 break;
401 case 48000:
402 rates |= SNDRV_PCM_RATE_48000;
403 break;
404 case 64000:
405 rates |= SNDRV_PCM_RATE_64000;
406 break;
407 case 88200:
408 rates |= SNDRV_PCM_RATE_88200;
409 break;
410 case 96000:
411 rates |= SNDRV_PCM_RATE_96000;
412 break;
413 case 176400:
414 rates |= SNDRV_PCM_RATE_176400;
415 break;
416 case 192000:
417 rates |= SNDRV_PCM_RATE_192000;
418 break;
419 default: /* some other rate */
420 rates |= SNDRV_PCM_RATE_KNOT;
425 pcmhw->rates = rates;
426 pcmhw->rate_min = rate_min;
427 pcmhw->rate_max = rate_max;
430 static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
431 struct snd_pcm_hw_params *params)
433 struct snd_pcm_runtime *runtime = substream->runtime;
434 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
435 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
436 int err;
437 u16 format;
438 int width;
439 unsigned int bytes_per_sec;
441 print_hwparams(substream, params);
442 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
443 if (err)
444 return err;
446 hpi_handle_error(hpi_format_create(&dpcm->format,
447 params_channels(params),
448 format, params_rate(params), 0, 0));
450 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
451 if (hpi_instream_reset(dpcm->h_stream) != 0)
452 return -EINVAL;
454 if (hpi_instream_set_format(
455 dpcm->h_stream, &dpcm->format) != 0)
456 return -EINVAL;
459 dpcm->hpi_buffer_attached = 0;
460 if (card->can_dma) {
461 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
462 params_buffer_bytes(params), runtime->dma_addr);
463 if (err == 0) {
464 snd_printdd(
465 "stream_host_buffer_attach success %u %lu\n",
466 params_buffer_bytes(params),
467 (unsigned long)runtime->dma_addr);
468 } else {
469 snd_printd("stream_host_buffer_attach error %d\n",
470 err);
471 return -ENOMEM;
474 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
475 &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
477 bytes_per_sec = params_rate(params) * params_channels(params);
478 width = snd_pcm_format_width(params_format(params));
479 bytes_per_sec *= width;
480 bytes_per_sec /= 8;
481 if (width < 0 || bytes_per_sec == 0)
482 return -EINVAL;
484 dpcm->bytes_per_sec = bytes_per_sec;
485 dpcm->buffer_bytes = params_buffer_bytes(params);
486 dpcm->period_bytes = params_period_bytes(params);
488 return 0;
491 static int
492 snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
494 struct snd_pcm_runtime *runtime = substream->runtime;
495 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
496 if (dpcm->hpi_buffer_attached)
497 hpi_stream_host_buffer_detach(dpcm->h_stream);
499 return 0;
502 static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
504 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
505 kfree(dpcm);
508 static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
509 substream)
511 struct snd_pcm_runtime *runtime = substream->runtime;
512 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
513 int expiry;
515 expiry = HZ / 200;
517 expiry = max(expiry, 1); /* don't let it be zero! */
518 mod_timer(&dpcm->timer, jiffies + expiry);
519 dpcm->respawn_timer = 1;
522 static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
524 struct snd_pcm_runtime *runtime = substream->runtime;
525 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
527 dpcm->respawn_timer = 0;
528 del_timer(&dpcm->timer);
531 static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
533 struct snd_card_asihpi_pcm *dpcm;
534 struct snd_card_asihpi *card;
536 dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
537 card = snd_pcm_substream_chip(substream);
539 WARN_ON(in_interrupt());
540 card->llmode_streampriv = dpcm;
542 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
543 HPI_ADAPTER_PROPERTY_IRQ_RATE,
544 card->update_interval_frames, 0));
547 static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
549 struct snd_card_asihpi *card;
551 card = snd_pcm_substream_chip(substream);
553 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
554 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
556 card->llmode_streampriv = NULL;
559 static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
560 int cmd)
562 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
563 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
564 struct snd_pcm_substream *s;
565 u16 e;
566 char name[16];
568 snd_pcm_debug_name(substream, name, sizeof(name));
570 switch (cmd) {
571 case SNDRV_PCM_TRIGGER_START:
572 snd_printdd("%s trigger start\n", name);
573 snd_pcm_group_for_each_entry(s, substream) {
574 struct snd_pcm_runtime *runtime = s->runtime;
575 struct snd_card_asihpi_pcm *ds = runtime->private_data;
577 if (snd_pcm_substream_chip(s) != card)
578 continue;
580 /* don't link Cap and Play */
581 if (substream->stream != s->stream)
582 continue;
584 ds->drained_count = 0;
585 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
586 /* How do I know how much valid data is present
587 * in buffer? Must be at least one period!
588 * Guessing 2 periods, but if
589 * buffer is bigger it may contain even more
590 * data??
592 unsigned int preload = ds->period_bytes * 1;
593 snd_printddd("%d preload %d\n", s->number, preload);
594 hpi_handle_error(hpi_outstream_write_buf(
595 ds->h_stream,
596 &runtime->dma_area[0],
597 preload,
598 &ds->format));
599 ds->pcm_buf_host_rw_ofs = preload;
602 if (card->support_grouping) {
603 snd_printdd("%d group\n", s->number);
604 e = hpi_stream_group_add(
605 dpcm->h_stream,
606 ds->h_stream);
607 if (!e) {
608 snd_pcm_trigger_done(s, substream);
609 } else {
610 hpi_handle_error(e);
611 break;
613 } else
614 break;
616 /* start the master stream */
617 card->pcm_start(substream);
618 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
619 !card->can_dma)
620 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
621 break;
623 case SNDRV_PCM_TRIGGER_STOP:
624 snd_printdd("%s trigger stop\n", name);
625 card->pcm_stop(substream);
626 snd_pcm_group_for_each_entry(s, substream) {
627 if (snd_pcm_substream_chip(s) != card)
628 continue;
629 /* don't link Cap and Play */
630 if (substream->stream != s->stream)
631 continue;
633 /*? workaround linked streams don't
634 transition to SETUP 20070706*/
635 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
637 if (card->support_grouping) {
638 snd_printdd("%d group\n", s->number);
639 snd_pcm_trigger_done(s, substream);
640 } else
641 break;
644 /* _prepare and _hwparams reset the stream */
645 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
646 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
647 hpi_handle_error(
648 hpi_outstream_reset(dpcm->h_stream));
650 if (card->support_grouping)
651 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
652 break;
654 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
655 snd_printdd("%s trigger pause release\n", name);
656 card->pcm_start(substream);
657 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
658 break;
659 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
660 snd_printdd("%s trigger pause push\n", name);
661 card->pcm_stop(substream);
662 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
663 break;
664 default:
665 snd_printd(KERN_ERR "\tINVALID\n");
666 return -EINVAL;
669 return 0;
672 /*algorithm outline
673 Without linking degenerates to getting single stream pos etc
674 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
677 pcm_buf_dma_ofs=get_buf_pos(s);
678 for_each_linked_stream(s) {
679 pcm_buf_dma_ofs=get_buf_pos(s);
680 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
681 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
683 timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
684 for_each_linked_stream(s) {
685 s->pcm_buf_dma_ofs = min_buf_pos;
686 if (new_data > period_bytes) {
687 if (mmap) {
688 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
689 if (playback) {
690 write(period_bytes);
691 } else {
692 read(period_bytes);
695 snd_pcm_period_elapsed(s);
700 /** Minimum of 2 modulo values. Works correctly when the difference between
701 * the values is less than half the modulus
703 static inline unsigned int modulo_min(unsigned int a, unsigned int b,
704 unsigned long int modulus)
706 unsigned int result;
707 if (((a-b) % modulus) < (modulus/2))
708 result = b;
709 else
710 result = a;
712 return result;
715 /** Timer function, equivalent to interrupt service routine for cards
717 static void snd_card_asihpi_timer_function(struct timer_list *t)
719 struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
720 struct snd_pcm_substream *substream = dpcm->substream;
721 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
722 struct snd_pcm_runtime *runtime;
723 struct snd_pcm_substream *s;
724 unsigned int newdata = 0;
725 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
726 unsigned int remdata, xfercount, next_jiffies;
727 int first = 1;
728 int loops = 0;
729 u16 state;
730 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
731 char name[16];
734 snd_pcm_debug_name(substream, name, sizeof(name));
736 /* find minimum newdata and buffer pos in group */
737 snd_pcm_group_for_each_entry(s, substream) {
738 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
739 runtime = s->runtime;
741 if (snd_pcm_substream_chip(s) != card)
742 continue;
744 /* don't link Cap and Play */
745 if (substream->stream != s->stream)
746 continue;
748 hpi_handle_error(hpi_stream_get_info_ex(
749 ds->h_stream, &state,
750 &buffer_size, &bytes_avail,
751 &samples_played, &on_card_bytes));
753 /* number of bytes in on-card buffer */
754 runtime->delay = on_card_bytes;
756 if (!card->can_dma)
757 on_card_bytes = bytes_avail;
759 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
760 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
761 if (state == HPI_STATE_STOPPED) {
762 if (bytes_avail == 0) {
763 hpi_handle_error(hpi_stream_start(ds->h_stream));
764 snd_printdd("P%d start\n", s->number);
765 ds->drained_count = 0;
767 } else if (state == HPI_STATE_DRAINED) {
768 snd_printd(KERN_WARNING "P%d drained\n",
769 s->number);
770 ds->drained_count++;
771 if (ds->drained_count > 20) {
772 snd_pcm_stop_xrun(s);
773 continue;
775 } else {
776 ds->drained_count = 0;
778 } else
779 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
781 if (first) {
782 /* can't statically init min when wrap is involved */
783 min_buf_pos = pcm_buf_dma_ofs;
784 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
785 first = 0;
786 } else {
787 min_buf_pos =
788 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
789 newdata = min(
790 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
791 newdata);
794 snd_printddd(
795 "timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
796 name, s->number, state,
797 ds->pcm_buf_elapsed_dma_ofs,
798 ds->pcm_buf_host_rw_ofs,
799 pcm_buf_dma_ofs,
800 (int)bytes_avail,
802 (int)on_card_bytes,
803 buffer_size-bytes_avail,
804 (unsigned long)frames_to_bytes(runtime,
805 runtime->status->hw_ptr),
806 (unsigned long)frames_to_bytes(runtime,
807 runtime->control->appl_ptr)
809 loops++;
811 pcm_buf_dma_ofs = min_buf_pos;
813 remdata = newdata % dpcm->period_bytes;
814 xfercount = newdata - remdata; /* a multiple of period_bytes */
815 /* come back when on_card_bytes has decreased enough to allow
816 write to happen, or when data has been consumed to make another
817 period
819 if (xfercount && (on_card_bytes > dpcm->period_bytes))
820 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
821 else
822 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
824 next_jiffies = max(next_jiffies, 1U);
825 dpcm->timer.expires = jiffies + next_jiffies;
826 snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
827 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
829 snd_pcm_group_for_each_entry(s, substream) {
830 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
832 /* don't link Cap and Play */
833 if (substream->stream != s->stream)
834 continue;
836 /* Store dma offset for use by pointer callback */
837 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
839 if (xfercount &&
840 /* Limit use of on card fifo for playback */
841 ((on_card_bytes <= ds->period_bytes) ||
842 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
846 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
847 unsigned int xfer1, xfer2;
848 char *pd = &s->runtime->dma_area[buf_ofs];
850 if (card->can_dma) { /* buffer wrap is handled at lower level */
851 xfer1 = xfercount;
852 xfer2 = 0;
853 } else {
854 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
855 xfer2 = xfercount - xfer1;
858 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
859 snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
860 s->number, xfer1, buf_ofs);
861 hpi_handle_error(
862 hpi_outstream_write_buf(
863 ds->h_stream, pd, xfer1,
864 &ds->format));
866 if (xfer2) {
867 pd = s->runtime->dma_area;
869 snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
870 s->number,
871 xfercount - xfer1, buf_ofs);
872 hpi_handle_error(
873 hpi_outstream_write_buf(
874 ds->h_stream, pd,
875 xfercount - xfer1,
876 &ds->format));
878 } else {
879 snd_printddd("read1, C=%d, xfer=%d\n",
880 s->number, xfer1);
881 hpi_handle_error(
882 hpi_instream_read_buf(
883 ds->h_stream,
884 pd, xfer1));
885 if (xfer2) {
886 pd = s->runtime->dma_area;
887 snd_printddd("read2, C=%d, xfer=%d\n",
888 s->number, xfer2);
889 hpi_handle_error(
890 hpi_instream_read_buf(
891 ds->h_stream,
892 pd, xfer2));
895 /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
896 ds->pcm_buf_host_rw_ofs += xfercount;
897 ds->pcm_buf_elapsed_dma_ofs += xfercount;
898 snd_pcm_period_elapsed(s);
902 if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
903 add_timer(&dpcm->timer);
906 static void snd_card_asihpi_isr(struct hpi_adapter *a)
908 struct snd_card_asihpi *asihpi;
910 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
911 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
912 if (asihpi->llmode_streampriv)
913 snd_card_asihpi_timer_function(
914 &asihpi->llmode_streampriv->timer);
917 /***************************** PLAYBACK OPS ****************/
918 static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
919 substream)
921 struct snd_pcm_runtime *runtime = substream->runtime;
922 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
924 snd_printdd("P%d prepare\n", substream->number);
926 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
927 dpcm->pcm_buf_host_rw_ofs = 0;
928 dpcm->pcm_buf_dma_ofs = 0;
929 dpcm->pcm_buf_elapsed_dma_ofs = 0;
930 return 0;
933 static snd_pcm_uframes_t
934 snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
936 struct snd_pcm_runtime *runtime = substream->runtime;
937 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
938 snd_pcm_uframes_t ptr;
939 char name[16];
940 snd_pcm_debug_name(substream, name, sizeof(name));
942 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
943 snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
944 return ptr;
947 static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
948 u32 h_stream)
950 struct hpi_format hpi_format;
951 u16 format;
952 u16 err;
953 u32 h_control;
954 u32 sample_rate = 48000;
955 u64 formats = 0;
957 /* on cards without SRC, must query at valid rate,
958 * maybe set by external sync
960 err = hpi_mixer_get_control(asihpi->h_mixer,
961 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
962 HPI_CONTROL_SAMPLECLOCK, &h_control);
964 if (!err)
965 err = hpi_sample_clock_get_sample_rate(h_control,
966 &sample_rate);
968 for (format = HPI_FORMAT_PCM8_UNSIGNED;
969 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
970 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
971 format, sample_rate, 128000, 0);
972 if (!err)
973 err = hpi_outstream_query_format(h_stream, &hpi_format);
974 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
975 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
977 return formats;
980 static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
982 struct snd_pcm_runtime *runtime = substream->runtime;
983 struct snd_card_asihpi_pcm *dpcm;
984 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
985 struct snd_pcm_hardware snd_card_asihpi_playback;
986 int err;
988 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
989 if (dpcm == NULL)
990 return -ENOMEM;
992 err = hpi_outstream_open(card->hpi->adapter->index,
993 substream->number, &dpcm->h_stream);
994 hpi_handle_error(err);
995 if (err)
996 kfree(dpcm);
997 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
998 return -EBUSY;
999 if (err)
1000 return -EIO;
1002 /*? also check ASI5000 samplerate source
1003 If external, only support external rate.
1004 If internal and other stream playing, can't switch
1007 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
1008 dpcm->substream = substream;
1009 runtime->private_data = dpcm;
1010 runtime->private_free = snd_card_asihpi_runtime_free;
1012 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
1013 if (!card->hpi->interrupt_mode) {
1014 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1015 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
1016 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1017 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1018 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1019 } else {
1020 size_t pbmin = card->update_interval_frames *
1021 card->out_max_chans;
1022 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1023 snd_card_asihpi_playback.period_bytes_min = pbmin;
1024 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1025 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1026 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
1029 /* snd_card_asihpi_playback.fifo_size = 0; */
1030 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1031 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1032 snd_card_asihpi_playback.formats =
1033 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
1035 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1037 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1038 SNDRV_PCM_INFO_DOUBLE |
1039 SNDRV_PCM_INFO_BATCH |
1040 SNDRV_PCM_INFO_BLOCK_TRANSFER |
1041 SNDRV_PCM_INFO_PAUSE |
1042 SNDRV_PCM_INFO_MMAP |
1043 SNDRV_PCM_INFO_MMAP_VALID;
1045 if (card->support_grouping) {
1046 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1047 snd_pcm_set_sync(substream);
1050 /* struct is copied, so can create initializer dynamically */
1051 runtime->hw = snd_card_asihpi_playback;
1053 if (card->can_dma)
1054 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1055 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1056 if (err < 0)
1057 return err;
1059 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1060 card->update_interval_frames);
1062 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1063 card->update_interval_frames, UINT_MAX);
1065 snd_printdd("playback open\n");
1067 return 0;
1070 static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1072 struct snd_pcm_runtime *runtime = substream->runtime;
1073 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1075 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
1076 snd_printdd("playback close\n");
1078 return 0;
1081 static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1082 .open = snd_card_asihpi_playback_open,
1083 .close = snd_card_asihpi_playback_close,
1084 .hw_params = snd_card_asihpi_pcm_hw_params,
1085 .hw_free = snd_card_asihpi_hw_free,
1086 .prepare = snd_card_asihpi_playback_prepare,
1087 .trigger = snd_card_asihpi_trigger,
1088 .pointer = snd_card_asihpi_playback_pointer,
1091 /***************************** CAPTURE OPS ****************/
1092 static snd_pcm_uframes_t
1093 snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1095 struct snd_pcm_runtime *runtime = substream->runtime;
1096 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1097 char name[16];
1098 snd_pcm_debug_name(substream, name, sizeof(name));
1100 snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
1101 /* NOTE Unlike playback can't use actual samples_played
1102 for the capture position, because those samples aren't yet in
1103 the local buffer available for reading.
1105 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
1108 static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1110 struct snd_pcm_runtime *runtime = substream->runtime;
1111 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1113 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
1114 dpcm->pcm_buf_host_rw_ofs = 0;
1115 dpcm->pcm_buf_dma_ofs = 0;
1116 dpcm->pcm_buf_elapsed_dma_ofs = 0;
1118 snd_printdd("Capture Prepare %d\n", substream->number);
1119 return 0;
1122 static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1123 u32 h_stream)
1125 struct hpi_format hpi_format;
1126 u16 format;
1127 u16 err;
1128 u32 h_control;
1129 u32 sample_rate = 48000;
1130 u64 formats = 0;
1132 /* on cards without SRC, must query at valid rate,
1133 maybe set by external sync */
1134 err = hpi_mixer_get_control(asihpi->h_mixer,
1135 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1136 HPI_CONTROL_SAMPLECLOCK, &h_control);
1138 if (!err)
1139 err = hpi_sample_clock_get_sample_rate(h_control,
1140 &sample_rate);
1142 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1143 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1145 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1146 format, sample_rate, 128000, 0);
1147 if (!err)
1148 err = hpi_instream_query_format(h_stream, &hpi_format);
1149 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
1150 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
1152 return formats;
1155 static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1157 struct snd_pcm_runtime *runtime = substream->runtime;
1158 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1159 struct snd_card_asihpi_pcm *dpcm;
1160 struct snd_pcm_hardware snd_card_asihpi_capture;
1161 int err;
1163 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1164 if (dpcm == NULL)
1165 return -ENOMEM;
1167 snd_printdd("capture open adapter %d stream %d\n",
1168 card->hpi->adapter->index, substream->number);
1170 err = hpi_handle_error(
1171 hpi_instream_open(card->hpi->adapter->index,
1172 substream->number, &dpcm->h_stream));
1173 if (err)
1174 kfree(dpcm);
1175 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1176 return -EBUSY;
1177 if (err)
1178 return -EIO;
1180 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
1181 dpcm->substream = substream;
1182 runtime->private_data = dpcm;
1183 runtime->private_free = snd_card_asihpi_runtime_free;
1185 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
1186 if (!card->hpi->interrupt_mode) {
1187 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1188 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1189 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1190 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1191 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1192 } else {
1193 size_t pbmin = card->update_interval_frames *
1194 card->out_max_chans;
1195 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1196 snd_card_asihpi_capture.period_bytes_min = pbmin;
1197 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1198 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1199 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
1201 /* snd_card_asihpi_capture.fifo_size = 0; */
1202 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1203 snd_card_asihpi_capture.channels_min = card->in_min_chans;
1204 snd_card_asihpi_capture.formats =
1205 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
1206 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
1207 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1208 SNDRV_PCM_INFO_MMAP |
1209 SNDRV_PCM_INFO_MMAP_VALID;
1211 if (card->support_grouping)
1212 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1214 runtime->hw = snd_card_asihpi_capture;
1216 if (card->can_dma)
1217 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1218 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1219 if (err < 0)
1220 return err;
1222 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1223 card->update_interval_frames);
1224 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1225 card->update_interval_frames, UINT_MAX);
1227 snd_pcm_set_sync(substream);
1229 return 0;
1232 static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1234 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1236 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
1237 return 0;
1240 static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1241 .open = snd_card_asihpi_capture_open,
1242 .close = snd_card_asihpi_capture_close,
1243 .hw_params = snd_card_asihpi_pcm_hw_params,
1244 .hw_free = snd_card_asihpi_hw_free,
1245 .prepare = snd_card_asihpi_capture_prepare,
1246 .trigger = snd_card_asihpi_trigger,
1247 .pointer = snd_card_asihpi_capture_pointer,
1250 static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
1252 struct snd_pcm *pcm;
1253 int err;
1254 u16 num_instreams, num_outstreams, x16;
1255 u32 x32;
1257 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1258 &num_outstreams, &num_instreams,
1259 &x16, &x32, &x16);
1261 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
1262 num_outstreams, num_instreams, &pcm);
1263 if (err < 0)
1264 return err;
1266 /* pointer to ops struct is stored, dont change ops afterwards! */
1267 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1268 &snd_card_asihpi_playback_mmap_ops);
1269 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1270 &snd_card_asihpi_capture_mmap_ops);
1272 pcm->private_data = asihpi;
1273 pcm->info_flags = 0;
1274 strcpy(pcm->name, "Asihpi PCM");
1276 /*? do we want to emulate MMAP for non-BBM cards?
1277 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1278 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
1279 &asihpi->pci->dev,
1280 64*1024, BUFFER_BYTES_MAX);
1282 return 0;
1285 /***************************** MIXER CONTROLS ****************/
1286 struct hpi_control {
1287 u32 h_control;
1288 u16 control_type;
1289 u16 src_node_type;
1290 u16 src_node_index;
1291 u16 dst_node_type;
1292 u16 dst_node_index;
1293 u16 band;
1294 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
1297 static const char * const asihpi_tuner_band_names[] = {
1298 "invalid",
1299 "AM",
1300 "FM mono",
1301 "TV NTSC-M",
1302 "FM stereo",
1303 "AUX",
1304 "TV PAL BG",
1305 "TV PAL I",
1306 "TV PAL DK",
1307 "TV SECAM",
1308 "TV DAB",
1310 /* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
1311 compile_time_assert(
1312 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1313 (HPI_TUNER_BAND_LAST+1)),
1314 assert_tuner_band_names_size);
1316 static const char * const asihpi_src_names[] = {
1317 "no source",
1318 "PCM",
1319 "Line",
1320 "Digital",
1321 "Tuner",
1322 "RF",
1323 "Clock",
1324 "Bitstream",
1325 "Mic",
1326 "Net",
1327 "Analog",
1328 "Adapter",
1329 "RTP",
1330 "Internal",
1331 "AVB",
1332 "BLU-Link"
1334 /* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
1335 compile_time_assert(
1336 (ARRAY_SIZE(asihpi_src_names) ==
1337 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
1338 assert_src_names_size);
1340 static const char * const asihpi_dst_names[] = {
1341 "no destination",
1342 "PCM",
1343 "Line",
1344 "Digital",
1345 "RF",
1346 "Speaker",
1347 "Net",
1348 "Analog",
1349 "RTP",
1350 "AVB",
1351 "Internal",
1352 "BLU-Link"
1354 /* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
1355 compile_time_assert(
1356 (ARRAY_SIZE(asihpi_dst_names) ==
1357 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
1358 assert_dst_names_size);
1360 static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1361 struct snd_card_asihpi *asihpi)
1363 int err;
1365 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1366 if (err < 0)
1367 return err;
1368 else if (mixer_dump)
1369 dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
1371 return 0;
1374 /* Convert HPI control name and location into ALSA control name */
1375 static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1376 struct hpi_control *hpi_ctl,
1377 char *name)
1379 char *dir;
1380 memset(snd_control, 0, sizeof(*snd_control));
1381 snd_control->name = hpi_ctl->name;
1382 snd_control->private_value = hpi_ctl->h_control;
1383 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1384 snd_control->index = 0;
1386 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1387 dir = ""; /* clock is neither capture nor playback */
1388 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
1389 dir = "Capture "; /* On or towards a PCM capture destination*/
1390 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1391 (!hpi_ctl->dst_node_type))
1392 dir = "Capture "; /* On a source node that is not PCM playback */
1393 else if (hpi_ctl->src_node_type &&
1394 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1395 (hpi_ctl->dst_node_type))
1396 dir = "Monitor Playback "; /* Between an input and an output */
1397 else
1398 dir = "Playback "; /* PCM Playback source, or output node */
1400 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
1401 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
1402 asihpi_src_names[hpi_ctl->src_node_type],
1403 hpi_ctl->src_node_index,
1404 asihpi_dst_names[hpi_ctl->dst_node_type],
1405 hpi_ctl->dst_node_index,
1406 dir, name);
1407 else if (hpi_ctl->dst_node_type) {
1408 sprintf(hpi_ctl->name, "%s %d %s%s",
1409 asihpi_dst_names[hpi_ctl->dst_node_type],
1410 hpi_ctl->dst_node_index,
1411 dir, name);
1412 } else {
1413 sprintf(hpi_ctl->name, "%s %d %s%s",
1414 asihpi_src_names[hpi_ctl->src_node_type],
1415 hpi_ctl->src_node_index,
1416 dir, name);
1418 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1419 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
1422 /*------------------------------------------------------------
1423 Volume controls
1424 ------------------------------------------------------------*/
1425 #define VOL_STEP_mB 1
1426 static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1427 struct snd_ctl_elem_info *uinfo)
1429 u32 h_control = kcontrol->private_value;
1430 u32 count;
1431 u16 err;
1432 /* native gains are in millibels */
1433 short min_gain_mB;
1434 short max_gain_mB;
1435 short step_gain_mB;
1437 err = hpi_volume_query_range(h_control,
1438 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1439 if (err) {
1440 max_gain_mB = 0;
1441 min_gain_mB = -10000;
1442 step_gain_mB = VOL_STEP_mB;
1445 err = hpi_meter_query_channels(h_control, &count);
1446 if (err)
1447 count = HPI_MAX_CHANNELS;
1449 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1450 uinfo->count = count;
1451 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1452 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1453 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1454 return 0;
1457 static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
1460 u32 h_control = kcontrol->private_value;
1461 short an_gain_mB[HPI_MAX_CHANNELS];
1463 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
1464 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1465 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1467 return 0;
1470 static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_value *ucontrol)
1473 u32 h_control = kcontrol->private_value;
1474 short an_gain_mB[HPI_MAX_CHANNELS];
1476 an_gain_mB[0] =
1477 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1478 an_gain_mB[1] =
1479 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1480 /* change = asihpi->mixer_volume[addr][0] != left ||
1481 asihpi->mixer_volume[addr][1] != right;
1483 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
1484 return 1;
1487 static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1489 #define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
1491 static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_value *ucontrol)
1494 u32 h_control = kcontrol->private_value;
1495 u32 mute;
1497 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1498 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1500 return 0;
1503 static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1506 u32 h_control = kcontrol->private_value;
1507 /* HPI currently only supports all or none muting of multichannel volume
1508 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1510 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1511 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1512 return 1;
1515 static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1516 struct hpi_control *hpi_ctl)
1518 struct snd_card *card = asihpi->card;
1519 struct snd_kcontrol_new snd_control;
1520 int err;
1521 u32 mute;
1523 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
1524 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1525 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1526 snd_control.info = snd_asihpi_volume_info;
1527 snd_control.get = snd_asihpi_volume_get;
1528 snd_control.put = snd_asihpi_volume_put;
1529 snd_control.tlv.p = db_scale_100;
1531 err = ctl_add(card, &snd_control, asihpi);
1532 if (err)
1533 return err;
1535 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1536 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1537 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1538 snd_control.info = snd_asihpi_volume_mute_info;
1539 snd_control.get = snd_asihpi_volume_mute_get;
1540 snd_control.put = snd_asihpi_volume_mute_put;
1541 err = ctl_add(card, &snd_control, asihpi);
1543 return err;
1546 /*------------------------------------------------------------
1547 Level controls
1548 ------------------------------------------------------------*/
1549 static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_info *uinfo)
1552 u32 h_control = kcontrol->private_value;
1553 u16 err;
1554 short min_gain_mB;
1555 short max_gain_mB;
1556 short step_gain_mB;
1558 err =
1559 hpi_level_query_range(h_control, &min_gain_mB,
1560 &max_gain_mB, &step_gain_mB);
1561 if (err) {
1562 max_gain_mB = 2400;
1563 min_gain_mB = -1000;
1564 step_gain_mB = 100;
1567 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1568 uinfo->count = 2;
1569 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1570 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1571 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1572 return 0;
1575 static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1578 u32 h_control = kcontrol->private_value;
1579 short an_gain_mB[HPI_MAX_CHANNELS];
1581 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
1582 ucontrol->value.integer.value[0] =
1583 an_gain_mB[0] / HPI_UNITS_PER_dB;
1584 ucontrol->value.integer.value[1] =
1585 an_gain_mB[1] / HPI_UNITS_PER_dB;
1587 return 0;
1590 static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1591 struct snd_ctl_elem_value *ucontrol)
1593 int change;
1594 u32 h_control = kcontrol->private_value;
1595 short an_gain_mB[HPI_MAX_CHANNELS];
1597 an_gain_mB[0] =
1598 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1599 an_gain_mB[1] =
1600 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1601 /* change = asihpi->mixer_level[addr][0] != left ||
1602 asihpi->mixer_level[addr][1] != right;
1604 change = 1;
1605 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
1606 return change;
1609 static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1611 static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1612 struct hpi_control *hpi_ctl)
1614 struct snd_card *card = asihpi->card;
1615 struct snd_kcontrol_new snd_control;
1617 /* can't use 'volume' cos some nodes have volume as well */
1618 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
1619 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1620 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1621 snd_control.info = snd_asihpi_level_info;
1622 snd_control.get = snd_asihpi_level_get;
1623 snd_control.put = snd_asihpi_level_put;
1624 snd_control.tlv.p = db_scale_level;
1626 return ctl_add(card, &snd_control, asihpi);
1629 /*------------------------------------------------------------
1630 AESEBU controls
1631 ------------------------------------------------------------*/
1633 /* AESEBU format */
1634 static const char * const asihpi_aesebu_format_names[] = {
1635 "N/A", "S/PDIF", "AES/EBU" };
1637 static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_info *uinfo)
1640 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
1643 static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1644 struct snd_ctl_elem_value *ucontrol,
1645 u16 (*func)(u32, u16 *))
1647 u32 h_control = kcontrol->private_value;
1648 u16 source, err;
1650 err = func(h_control, &source);
1652 /* default to N/A */
1653 ucontrol->value.enumerated.item[0] = 0;
1654 /* return success but set the control to N/A */
1655 if (err)
1656 return 0;
1657 if (source == HPI_AESEBU_FORMAT_SPDIF)
1658 ucontrol->value.enumerated.item[0] = 1;
1659 if (source == HPI_AESEBU_FORMAT_AESEBU)
1660 ucontrol->value.enumerated.item[0] = 2;
1662 return 0;
1665 static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1666 struct snd_ctl_elem_value *ucontrol,
1667 u16 (*func)(u32, u16))
1669 u32 h_control = kcontrol->private_value;
1671 /* default to S/PDIF */
1672 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1674 if (ucontrol->value.enumerated.item[0] == 1)
1675 source = HPI_AESEBU_FORMAT_SPDIF;
1676 if (ucontrol->value.enumerated.item[0] == 2)
1677 source = HPI_AESEBU_FORMAT_AESEBU;
1679 if (func(h_control, source) != 0)
1680 return -EINVAL;
1682 return 1;
1685 static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1686 struct snd_ctl_elem_value *ucontrol) {
1687 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1688 hpi_aesebu_receiver_get_format);
1691 static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1692 struct snd_ctl_elem_value *ucontrol) {
1693 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1694 hpi_aesebu_receiver_set_format);
1697 static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_info *uinfo)
1700 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1701 uinfo->count = 1;
1703 uinfo->value.integer.min = 0;
1704 uinfo->value.integer.max = 0X1F;
1705 uinfo->value.integer.step = 1;
1707 return 0;
1710 static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1711 struct snd_ctl_elem_value *ucontrol) {
1713 u32 h_control = kcontrol->private_value;
1714 u16 status;
1716 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1717 h_control, &status));
1718 ucontrol->value.integer.value[0] = status;
1719 return 0;
1722 static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1723 struct hpi_control *hpi_ctl)
1725 struct snd_card *card = asihpi->card;
1726 struct snd_kcontrol_new snd_control;
1728 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1729 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1730 snd_control.info = snd_asihpi_aesebu_format_info;
1731 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1732 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1735 if (ctl_add(card, &snd_control, asihpi) < 0)
1736 return -EINVAL;
1738 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
1739 snd_control.access =
1740 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1741 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1742 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1744 return ctl_add(card, &snd_control, asihpi);
1747 static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol) {
1749 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
1750 hpi_aesebu_transmitter_get_format);
1753 static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_value *ucontrol) {
1755 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
1756 hpi_aesebu_transmitter_set_format);
1760 static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1761 struct hpi_control *hpi_ctl)
1763 struct snd_card *card = asihpi->card;
1764 struct snd_kcontrol_new snd_control;
1766 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
1767 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1768 snd_control.info = snd_asihpi_aesebu_format_info;
1769 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1770 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1772 return ctl_add(card, &snd_control, asihpi);
1775 /*------------------------------------------------------------
1776 Tuner controls
1777 ------------------------------------------------------------*/
1779 /* Gain */
1781 static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1782 struct snd_ctl_elem_info *uinfo)
1784 u32 h_control = kcontrol->private_value;
1785 u16 err;
1786 short idx;
1787 u16 gain_range[3];
1789 for (idx = 0; idx < 3; idx++) {
1790 err = hpi_tuner_query_gain(h_control,
1791 idx, &gain_range[idx]);
1792 if (err != 0)
1793 return err;
1796 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1797 uinfo->count = 1;
1798 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1799 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1800 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1801 return 0;
1804 static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
1808 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1810 u32 h_control = kcontrol->private_value;
1811 short gain;
1813 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
1814 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1816 return 0;
1819 static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1823 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1825 u32 h_control = kcontrol->private_value;
1826 short gain;
1828 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1829 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
1831 return 1;
1834 /* Band */
1836 static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1837 u16 *band_list, u32 len) {
1838 u32 h_control = kcontrol->private_value;
1839 u16 err = 0;
1840 u32 i;
1842 for (i = 0; i < len; i++) {
1843 err = hpi_tuner_query_band(
1844 h_control, i, &band_list[i]);
1845 if (err != 0)
1846 break;
1849 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1850 return -EIO;
1852 return i;
1855 static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_info *uinfo)
1858 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1859 int num_bands = 0;
1861 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1862 HPI_TUNER_BAND_LAST);
1864 if (num_bands < 0)
1865 return num_bands;
1867 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
1870 static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1873 u32 h_control = kcontrol->private_value;
1875 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1877 u16 band, idx;
1878 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1879 __always_unused u32 num_bands;
1881 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1882 HPI_TUNER_BAND_LAST);
1884 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
1886 ucontrol->value.enumerated.item[0] = -1;
1887 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1888 if (tuner_bands[idx] == band) {
1889 ucontrol->value.enumerated.item[0] = idx;
1890 break;
1893 return 0;
1896 static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1897 struct snd_ctl_elem_value *ucontrol)
1900 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1902 u32 h_control = kcontrol->private_value;
1903 unsigned int idx;
1904 u16 band;
1905 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1906 __always_unused u32 num_bands;
1908 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1909 HPI_TUNER_BAND_LAST);
1911 idx = ucontrol->value.enumerated.item[0];
1912 if (idx >= ARRAY_SIZE(tuner_bands))
1913 idx = ARRAY_SIZE(tuner_bands) - 1;
1914 band = tuner_bands[idx];
1915 hpi_handle_error(hpi_tuner_set_band(h_control, band));
1917 return 1;
1920 /* Freq */
1922 static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1923 struct snd_ctl_elem_info *uinfo)
1925 u32 h_control = kcontrol->private_value;
1926 u16 err;
1927 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1928 u16 num_bands = 0, band_iter, idx;
1929 u32 freq_range[3], temp_freq_range[3];
1931 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1932 HPI_TUNER_BAND_LAST);
1934 freq_range[0] = INT_MAX;
1935 freq_range[1] = 0;
1936 freq_range[2] = INT_MAX;
1938 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1939 for (idx = 0; idx < 3; idx++) {
1940 err = hpi_tuner_query_frequency(h_control,
1941 idx, tuner_bands[band_iter],
1942 &temp_freq_range[idx]);
1943 if (err != 0)
1944 return err;
1947 /* skip band with bogus stepping */
1948 if (temp_freq_range[2] <= 0)
1949 continue;
1951 if (temp_freq_range[0] < freq_range[0])
1952 freq_range[0] = temp_freq_range[0];
1953 if (temp_freq_range[1] > freq_range[1])
1954 freq_range[1] = temp_freq_range[1];
1955 if (temp_freq_range[2] < freq_range[2])
1956 freq_range[2] = temp_freq_range[2];
1959 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1960 uinfo->count = 1;
1961 uinfo->value.integer.min = ((int)freq_range[0]);
1962 uinfo->value.integer.max = ((int)freq_range[1]);
1963 uinfo->value.integer.step = ((int)freq_range[2]);
1964 return 0;
1967 static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1968 struct snd_ctl_elem_value *ucontrol)
1970 u32 h_control = kcontrol->private_value;
1971 u32 freq;
1973 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
1974 ucontrol->value.integer.value[0] = freq;
1976 return 0;
1979 static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1982 u32 h_control = kcontrol->private_value;
1983 u32 freq;
1985 freq = ucontrol->value.integer.value[0];
1986 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
1988 return 1;
1991 /* Tuner control group initializer */
1992 static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1993 struct hpi_control *hpi_ctl)
1995 struct snd_card *card = asihpi->card;
1996 struct snd_kcontrol_new snd_control;
1998 snd_control.private_value = hpi_ctl->h_control;
1999 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2001 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
2002 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
2003 snd_control.info = snd_asihpi_tuner_gain_info;
2004 snd_control.get = snd_asihpi_tuner_gain_get;
2005 snd_control.put = snd_asihpi_tuner_gain_put;
2007 if (ctl_add(card, &snd_control, asihpi) < 0)
2008 return -EINVAL;
2011 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
2012 snd_control.info = snd_asihpi_tuner_band_info;
2013 snd_control.get = snd_asihpi_tuner_band_get;
2014 snd_control.put = snd_asihpi_tuner_band_put;
2016 if (ctl_add(card, &snd_control, asihpi) < 0)
2017 return -EINVAL;
2019 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
2020 snd_control.info = snd_asihpi_tuner_freq_info;
2021 snd_control.get = snd_asihpi_tuner_freq_get;
2022 snd_control.put = snd_asihpi_tuner_freq_put;
2024 return ctl_add(card, &snd_control, asihpi);
2027 /*------------------------------------------------------------
2028 Meter controls
2029 ------------------------------------------------------------*/
2030 static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2031 struct snd_ctl_elem_info *uinfo)
2033 u32 h_control = kcontrol->private_value;
2034 u32 count;
2035 u16 err;
2036 err = hpi_meter_query_channels(h_control, &count);
2037 if (err)
2038 count = HPI_MAX_CHANNELS;
2040 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2041 uinfo->count = count;
2042 uinfo->value.integer.min = 0;
2043 uinfo->value.integer.max = 0x7FFFFFFF;
2044 return 0;
2047 /* linear values for 10dB steps */
2048 static const int log2lin[] = {
2049 0x7FFFFFFF, /* 0dB */
2050 679093956,
2051 214748365,
2052 67909396,
2053 21474837,
2054 6790940,
2055 2147484, /* -60dB */
2056 679094,
2057 214748, /* -80 */
2058 67909,
2059 21475, /* -100 */
2060 6791,
2061 2147,
2062 679,
2063 214,
2070 static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2071 struct snd_ctl_elem_value *ucontrol)
2073 u32 h_control = kcontrol->private_value;
2074 short an_gain_mB[HPI_MAX_CHANNELS], i;
2075 u16 err;
2077 err = hpi_meter_get_peak(h_control, an_gain_mB);
2079 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2080 if (err) {
2081 ucontrol->value.integer.value[i] = 0;
2082 } else if (an_gain_mB[i] >= 0) {
2083 ucontrol->value.integer.value[i] =
2084 an_gain_mB[i] << 16;
2085 } else {
2086 /* -ve is log value in millibels < -60dB,
2087 * convert to (roughly!) linear,
2089 ucontrol->value.integer.value[i] =
2090 log2lin[an_gain_mB[i] / -1000];
2093 return 0;
2096 static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2097 struct hpi_control *hpi_ctl, int subidx)
2099 struct snd_card *card = asihpi->card;
2100 struct snd_kcontrol_new snd_control;
2102 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
2103 snd_control.access =
2104 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2105 snd_control.info = snd_asihpi_meter_info;
2106 snd_control.get = snd_asihpi_meter_get;
2108 snd_control.index = subidx;
2110 return ctl_add(card, &snd_control, asihpi);
2113 /*------------------------------------------------------------
2114 Multiplexer controls
2115 ------------------------------------------------------------*/
2116 static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2118 u32 h_control = snd_control->private_value;
2119 struct hpi_control hpi_ctl;
2120 int s, err;
2121 for (s = 0; s < 32; s++) {
2122 err = hpi_multiplexer_query_source(h_control, s,
2123 &hpi_ctl.
2124 src_node_type,
2125 &hpi_ctl.
2126 src_node_index);
2127 if (err)
2128 break;
2130 return s;
2133 static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_info *uinfo)
2136 u16 src_node_type, src_node_index;
2137 u32 h_control = kcontrol->private_value;
2139 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2140 uinfo->count = 1;
2141 uinfo->value.enumerated.items =
2142 snd_card_asihpi_mux_count_sources(kcontrol);
2144 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2145 uinfo->value.enumerated.item =
2146 uinfo->value.enumerated.items - 1;
2148 hpi_multiplexer_query_source(h_control,
2149 uinfo->value.enumerated.item,
2150 &src_node_type, &src_node_index);
2152 sprintf(uinfo->value.enumerated.name, "%s %d",
2153 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
2154 src_node_index);
2155 return 0;
2158 static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2159 struct snd_ctl_elem_value *ucontrol)
2161 u32 h_control = kcontrol->private_value;
2162 u16 source_type, source_index;
2163 u16 src_node_type, src_node_index;
2164 int s;
2166 hpi_handle_error(hpi_multiplexer_get_source(h_control,
2167 &source_type, &source_index));
2168 /* Should cache this search result! */
2169 for (s = 0; s < 256; s++) {
2170 if (hpi_multiplexer_query_source(h_control, s,
2171 &src_node_type, &src_node_index))
2172 break;
2174 if ((source_type == src_node_type)
2175 && (source_index == src_node_index)) {
2176 ucontrol->value.enumerated.item[0] = s;
2177 return 0;
2180 snd_printd(KERN_WARNING
2181 "Control %x failed to match mux source %hu %hu\n",
2182 h_control, source_type, source_index);
2183 ucontrol->value.enumerated.item[0] = 0;
2184 return 0;
2187 static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2188 struct snd_ctl_elem_value *ucontrol)
2190 int change;
2191 u32 h_control = kcontrol->private_value;
2192 u16 source_type, source_index;
2193 u16 e;
2195 change = 1;
2197 e = hpi_multiplexer_query_source(h_control,
2198 ucontrol->value.enumerated.item[0],
2199 &source_type, &source_index);
2200 if (!e)
2201 hpi_handle_error(
2202 hpi_multiplexer_set_source(h_control,
2203 source_type, source_index));
2204 return change;
2208 static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2209 struct hpi_control *hpi_ctl)
2211 struct snd_card *card = asihpi->card;
2212 struct snd_kcontrol_new snd_control;
2214 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
2215 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2216 snd_control.info = snd_asihpi_mux_info;
2217 snd_control.get = snd_asihpi_mux_get;
2218 snd_control.put = snd_asihpi_mux_put;
2220 return ctl_add(card, &snd_control, asihpi);
2224 /*------------------------------------------------------------
2225 Channel mode controls
2226 ------------------------------------------------------------*/
2227 static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2228 struct snd_ctl_elem_info *uinfo)
2230 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2231 "invalid",
2232 "Normal", "Swap",
2233 "From Left", "From Right",
2234 "To Left", "To Right"
2237 u32 h_control = kcontrol->private_value;
2238 u16 mode;
2239 int i;
2240 const char *mapped_names[6];
2241 int valid_modes = 0;
2243 /* HPI channel mode values can be from 1 to 6
2244 Some adapters only support a contiguous subset
2246 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
2247 if (!hpi_channel_mode_query_mode(
2248 h_control, i, &mode)) {
2249 mapped_names[valid_modes] = mode_names[mode];
2250 valid_modes++;
2253 if (!valid_modes)
2254 return -EINVAL;
2256 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
2259 static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_value *ucontrol)
2262 u32 h_control = kcontrol->private_value;
2263 u16 mode;
2265 if (hpi_channel_mode_get(h_control, &mode))
2266 mode = 1;
2268 ucontrol->value.enumerated.item[0] = mode - 1;
2270 return 0;
2273 static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2274 struct snd_ctl_elem_value *ucontrol)
2276 int change;
2277 u32 h_control = kcontrol->private_value;
2279 change = 1;
2281 hpi_handle_error(hpi_channel_mode_set(h_control,
2282 ucontrol->value.enumerated.item[0] + 1));
2283 return change;
2287 static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2288 struct hpi_control *hpi_ctl)
2290 struct snd_card *card = asihpi->card;
2291 struct snd_kcontrol_new snd_control;
2293 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
2294 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2295 snd_control.info = snd_asihpi_cmode_info;
2296 snd_control.get = snd_asihpi_cmode_get;
2297 snd_control.put = snd_asihpi_cmode_put;
2299 return ctl_add(card, &snd_control, asihpi);
2302 /*------------------------------------------------------------
2303 Sampleclock source controls
2304 ------------------------------------------------------------*/
2305 static const char * const sampleclock_sources[] = {
2306 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2307 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2308 "Prev Module", "BLU-Link",
2309 "Digital2", "Digital3", "Digital4", "Digital5",
2310 "Digital6", "Digital7", "Digital8"};
2312 /* Number of strings must match expected enumerated values */
2313 compile_time_assert(
2314 (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
2315 assert_sampleclock_sources_size);
2317 static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2318 struct snd_ctl_elem_info *uinfo)
2320 struct snd_card_asihpi *asihpi =
2321 (struct snd_card_asihpi *)(kcontrol->private_data);
2322 struct clk_cache *clkcache = &asihpi->cc;
2323 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2324 uinfo->count = 1;
2325 uinfo->value.enumerated.items = clkcache->count;
2327 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2328 uinfo->value.enumerated.item =
2329 uinfo->value.enumerated.items - 1;
2331 strcpy(uinfo->value.enumerated.name,
2332 clkcache->s[uinfo->value.enumerated.item].name);
2333 return 0;
2336 static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2337 struct snd_ctl_elem_value *ucontrol)
2339 struct snd_card_asihpi *asihpi =
2340 (struct snd_card_asihpi *)(kcontrol->private_data);
2341 struct clk_cache *clkcache = &asihpi->cc;
2342 u32 h_control = kcontrol->private_value;
2343 u16 source, srcindex = 0;
2344 int i;
2346 ucontrol->value.enumerated.item[0] = 0;
2347 if (hpi_sample_clock_get_source(h_control, &source))
2348 source = 0;
2350 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2351 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
2352 srcindex = 0;
2354 for (i = 0; i < clkcache->count; i++)
2355 if ((clkcache->s[i].source == source) &&
2356 (clkcache->s[i].index == srcindex))
2357 break;
2359 ucontrol->value.enumerated.item[0] = i;
2361 return 0;
2364 static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2365 struct snd_ctl_elem_value *ucontrol)
2367 struct snd_card_asihpi *asihpi =
2368 (struct snd_card_asihpi *)(kcontrol->private_data);
2369 struct clk_cache *clkcache = &asihpi->cc;
2370 unsigned int item;
2371 int change;
2372 u32 h_control = kcontrol->private_value;
2374 change = 1;
2375 item = ucontrol->value.enumerated.item[0];
2376 if (item >= clkcache->count)
2377 item = clkcache->count-1;
2379 hpi_handle_error(hpi_sample_clock_set_source(
2380 h_control, clkcache->s[item].source));
2382 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2383 hpi_handle_error(hpi_sample_clock_set_source_index(
2384 h_control, clkcache->s[item].index));
2385 return change;
2388 /*------------------------------------------------------------
2389 Clkrate controls
2390 ------------------------------------------------------------*/
2391 /* Need to change this to enumerated control with list of rates */
2392 static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2393 struct snd_ctl_elem_info *uinfo)
2395 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2396 uinfo->count = 1;
2397 uinfo->value.integer.min = 8000;
2398 uinfo->value.integer.max = 192000;
2399 uinfo->value.integer.step = 100;
2401 return 0;
2404 static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_value *ucontrol)
2407 u32 h_control = kcontrol->private_value;
2408 u32 rate;
2409 u16 e;
2411 e = hpi_sample_clock_get_local_rate(h_control, &rate);
2412 if (!e)
2413 ucontrol->value.integer.value[0] = rate;
2414 else
2415 ucontrol->value.integer.value[0] = 0;
2416 return 0;
2419 static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
2422 int change;
2423 u32 h_control = kcontrol->private_value;
2425 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2426 asihpi->mixer_clkrate[addr][1] != right;
2428 change = 1;
2429 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
2430 ucontrol->value.integer.value[0]));
2431 return change;
2434 static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_info *uinfo)
2437 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2438 uinfo->count = 1;
2439 uinfo->value.integer.min = 8000;
2440 uinfo->value.integer.max = 192000;
2441 uinfo->value.integer.step = 100;
2443 return 0;
2446 static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2447 struct snd_ctl_elem_value *ucontrol)
2449 u32 h_control = kcontrol->private_value;
2450 u32 rate;
2451 u16 e;
2453 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
2454 if (!e)
2455 ucontrol->value.integer.value[0] = rate;
2456 else
2457 ucontrol->value.integer.value[0] = 0;
2458 return 0;
2461 static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2462 struct hpi_control *hpi_ctl)
2464 struct snd_card *card;
2465 struct snd_kcontrol_new snd_control;
2467 struct clk_cache *clkcache;
2468 u32 hSC = hpi_ctl->h_control;
2469 int has_aes_in = 0;
2470 int i, j;
2471 u16 source;
2473 if (snd_BUG_ON(!asihpi))
2474 return -EINVAL;
2475 card = asihpi->card;
2476 clkcache = &asihpi->cc;
2477 snd_control.private_value = hpi_ctl->h_control;
2479 clkcache->has_local = 0;
2481 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
2482 if (hpi_sample_clock_query_source(hSC,
2483 i, &source))
2484 break;
2485 clkcache->s[i].source = source;
2486 clkcache->s[i].index = 0;
2487 clkcache->s[i].name = sampleclock_sources[source];
2488 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2489 has_aes_in = 1;
2490 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2491 clkcache->has_local = 1;
2493 if (has_aes_in)
2494 /* already will have picked up index 0 above */
2495 for (j = 1; j < 8; j++) {
2496 if (hpi_sample_clock_query_source_index(hSC,
2497 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2498 &source))
2499 break;
2500 clkcache->s[i].source =
2501 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2502 clkcache->s[i].index = j;
2503 clkcache->s[i].name = sampleclock_sources[
2504 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2505 i++;
2507 clkcache->count = i;
2509 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
2510 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2511 snd_control.info = snd_asihpi_clksrc_info;
2512 snd_control.get = snd_asihpi_clksrc_get;
2513 snd_control.put = snd_asihpi_clksrc_put;
2514 if (ctl_add(card, &snd_control, asihpi) < 0)
2515 return -EINVAL;
2518 if (clkcache->has_local) {
2519 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
2520 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2521 snd_control.info = snd_asihpi_clklocal_info;
2522 snd_control.get = snd_asihpi_clklocal_get;
2523 snd_control.put = snd_asihpi_clklocal_put;
2526 if (ctl_add(card, &snd_control, asihpi) < 0)
2527 return -EINVAL;
2530 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
2531 snd_control.access =
2532 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2533 snd_control.info = snd_asihpi_clkrate_info;
2534 snd_control.get = snd_asihpi_clkrate_get;
2536 return ctl_add(card, &snd_control, asihpi);
2538 /*------------------------------------------------------------
2539 Mixer
2540 ------------------------------------------------------------*/
2542 static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2544 struct snd_card *card;
2545 unsigned int idx = 0;
2546 unsigned int subindex = 0;
2547 int err;
2548 struct hpi_control hpi_ctl, prev_ctl;
2550 if (snd_BUG_ON(!asihpi))
2551 return -EINVAL;
2552 card = asihpi->card;
2553 strcpy(card->mixername, "Asihpi Mixer");
2555 err =
2556 hpi_mixer_open(asihpi->hpi->adapter->index,
2557 &asihpi->h_mixer);
2558 hpi_handle_error(err);
2559 if (err)
2560 return -err;
2562 memset(&prev_ctl, 0, sizeof(prev_ctl));
2563 prev_ctl.control_type = -1;
2565 for (idx = 0; idx < 2000; idx++) {
2566 err = hpi_mixer_get_control_by_index(
2567 asihpi->h_mixer,
2568 idx,
2569 &hpi_ctl.src_node_type,
2570 &hpi_ctl.src_node_index,
2571 &hpi_ctl.dst_node_type,
2572 &hpi_ctl.dst_node_index,
2573 &hpi_ctl.control_type,
2574 &hpi_ctl.h_control);
2575 if (err) {
2576 if (err == HPI_ERROR_CONTROL_DISABLED) {
2577 if (mixer_dump)
2578 dev_info(&asihpi->pci->dev,
2579 "Disabled HPI Control(%d)\n",
2580 idx);
2581 continue;
2582 } else
2583 break;
2587 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2588 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
2590 /* ASI50xx in SSX mode has multiple meters on the same node.
2591 Use subindex to create distinct ALSA controls
2592 for any duplicated controls.
2594 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2595 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2596 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2597 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2598 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2599 subindex++;
2600 else
2601 subindex = 0;
2603 prev_ctl = hpi_ctl;
2605 switch (hpi_ctl.control_type) {
2606 case HPI_CONTROL_VOLUME:
2607 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2608 break;
2609 case HPI_CONTROL_LEVEL:
2610 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2611 break;
2612 case HPI_CONTROL_MULTIPLEXER:
2613 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2614 break;
2615 case HPI_CONTROL_CHANNEL_MODE:
2616 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2617 break;
2618 case HPI_CONTROL_METER:
2619 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2620 break;
2621 case HPI_CONTROL_SAMPLECLOCK:
2622 err = snd_asihpi_sampleclock_add(
2623 asihpi, &hpi_ctl);
2624 break;
2625 case HPI_CONTROL_CONNECTION: /* ignore these */
2626 continue;
2627 case HPI_CONTROL_TUNER:
2628 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2629 break;
2630 case HPI_CONTROL_AESEBU_TRANSMITTER:
2631 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2632 break;
2633 case HPI_CONTROL_AESEBU_RECEIVER:
2634 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2635 break;
2636 case HPI_CONTROL_VOX:
2637 case HPI_CONTROL_BITSTREAM:
2638 case HPI_CONTROL_MICROPHONE:
2639 case HPI_CONTROL_PARAMETRIC_EQ:
2640 case HPI_CONTROL_COMPANDER:
2641 default:
2642 if (mixer_dump)
2643 dev_info(&asihpi->pci->dev,
2644 "Untranslated HPI Control (%d) %d %d %d %d %d\n",
2645 idx,
2646 hpi_ctl.control_type,
2647 hpi_ctl.src_node_type,
2648 hpi_ctl.src_node_index,
2649 hpi_ctl.dst_node_type,
2650 hpi_ctl.dst_node_index);
2651 continue;
2653 if (err < 0)
2654 return err;
2656 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2657 hpi_handle_error(err);
2659 dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
2661 return 0;
2664 /*------------------------------------------------------------
2665 /proc interface
2666 ------------------------------------------------------------*/
2668 static void
2669 snd_asihpi_proc_read(struct snd_info_entry *entry,
2670 struct snd_info_buffer *buffer)
2672 struct snd_card_asihpi *asihpi = entry->private_data;
2673 u32 h_control;
2674 u32 rate = 0;
2675 u16 source = 0;
2677 u16 num_outstreams;
2678 u16 num_instreams;
2679 u16 version;
2680 u32 serial_number;
2681 u16 type;
2683 int err;
2685 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2687 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2688 &num_outstreams, &num_instreams,
2689 &version, &serial_number, &type));
2691 snd_iprintf(buffer,
2692 "Adapter type ASI%4X\nHardware Index %d\n"
2693 "%d outstreams\n%d instreams\n",
2694 type, asihpi->hpi->adapter->index,
2695 num_outstreams, num_instreams);
2697 snd_iprintf(buffer,
2698 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2699 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
2700 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2702 err = hpi_mixer_get_control(asihpi->h_mixer,
2703 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2704 HPI_CONTROL_SAMPLECLOCK, &h_control);
2706 if (!err) {
2707 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
2708 err += hpi_sample_clock_get_source(h_control, &source);
2710 if (!err)
2711 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
2712 rate, sampleclock_sources[source]);
2716 static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2718 snd_card_ro_proc_new(asihpi->card, "info", asihpi,
2719 snd_asihpi_proc_read);
2722 /*------------------------------------------------------------
2723 HWDEP
2724 ------------------------------------------------------------*/
2726 static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2728 if (enable_hpi_hwdep)
2729 return 0;
2730 else
2731 return -ENODEV;
2735 static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2737 if (enable_hpi_hwdep)
2738 return asihpi_hpi_release(file);
2739 else
2740 return -ENODEV;
2743 static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2744 unsigned int cmd, unsigned long arg)
2746 if (enable_hpi_hwdep)
2747 return asihpi_hpi_ioctl(file, cmd, arg);
2748 else
2749 return -ENODEV;
2753 /* results in /dev/snd/hwC#D0 file for each card with index #
2754 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2756 static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
2758 struct snd_hwdep *hw;
2759 int err;
2761 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2762 if (err < 0)
2763 return err;
2764 strcpy(hw->name, "asihpi (HPI)");
2765 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2766 hw->ops.open = snd_asihpi_hpi_open;
2767 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2768 hw->ops.release = snd_asihpi_hpi_release;
2769 hw->private_data = asihpi;
2770 return 0;
2773 /*------------------------------------------------------------
2774 CARD
2775 ------------------------------------------------------------*/
2776 static int snd_asihpi_probe(struct pci_dev *pci_dev,
2777 const struct pci_device_id *pci_id)
2779 int err;
2780 struct hpi_adapter *hpi;
2781 struct snd_card *card;
2782 struct snd_card_asihpi *asihpi;
2784 u32 h_control;
2785 u32 h_stream;
2786 u32 adapter_index;
2788 static int dev;
2789 if (dev >= SNDRV_CARDS)
2790 return -ENODEV;
2792 /* Should this be enable[hpi->index] ? */
2793 if (!enable[dev]) {
2794 dev++;
2795 return -ENOENT;
2798 /* Initialise low-level HPI driver */
2799 err = asihpi_adapter_probe(pci_dev, pci_id);
2800 if (err < 0)
2801 return err;
2803 hpi = pci_get_drvdata(pci_dev);
2804 adapter_index = hpi->adapter->index;
2805 /* first try to give the card the same index as its hardware index */
2806 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2807 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
2808 if (err < 0) {
2809 /* if that fails, try the default index==next available */
2810 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2811 THIS_MODULE, sizeof(struct snd_card_asihpi),
2812 &card);
2813 if (err < 0)
2814 return err;
2815 dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
2816 adapter_index, card->number);
2819 asihpi = card->private_data;
2820 asihpi->card = card;
2821 asihpi->pci = pci_dev;
2822 asihpi->hpi = hpi;
2823 hpi->snd_card = card;
2825 err = hpi_adapter_get_property(adapter_index,
2826 HPI_ADAPTER_PROPERTY_CAPS1,
2827 NULL, &asihpi->support_grouping);
2828 if (err)
2829 asihpi->support_grouping = 0;
2831 err = hpi_adapter_get_property(adapter_index,
2832 HPI_ADAPTER_PROPERTY_CAPS2,
2833 &asihpi->support_mrx, NULL);
2834 if (err)
2835 asihpi->support_mrx = 0;
2837 err = hpi_adapter_get_property(adapter_index,
2838 HPI_ADAPTER_PROPERTY_INTERVAL,
2839 NULL, &asihpi->update_interval_frames);
2840 if (err)
2841 asihpi->update_interval_frames = 512;
2843 if (hpi->interrupt_mode) {
2844 asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
2845 asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
2846 hpi->interrupt_callback = snd_card_asihpi_isr;
2847 } else {
2848 asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
2849 asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
2852 hpi_handle_error(hpi_instream_open(adapter_index,
2853 0, &h_stream));
2855 err = hpi_instream_host_buffer_free(h_stream);
2856 asihpi->can_dma = (!err);
2858 hpi_handle_error(hpi_instream_close(h_stream));
2860 if (!asihpi->can_dma)
2861 asihpi->update_interval_frames *= 2;
2863 err = hpi_adapter_get_property(adapter_index,
2864 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2865 &asihpi->in_max_chans, &asihpi->out_max_chans);
2866 if (err) {
2867 asihpi->in_max_chans = 2;
2868 asihpi->out_max_chans = 2;
2871 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2872 asihpi->out_min_chans = asihpi->out_max_chans;
2873 asihpi->in_min_chans = asihpi->in_max_chans;
2874 asihpi->support_grouping = 0;
2875 } else {
2876 asihpi->out_min_chans = 1;
2877 asihpi->in_min_chans = 1;
2880 dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
2881 asihpi->can_dma,
2882 asihpi->support_grouping,
2883 asihpi->support_mrx,
2884 asihpi->update_interval_frames
2887 err = snd_card_asihpi_pcm_new(asihpi, 0);
2888 if (err < 0) {
2889 dev_err(&pci_dev->dev, "pcm_new failed\n");
2890 goto __nodev;
2892 err = snd_card_asihpi_mixer_new(asihpi);
2893 if (err < 0) {
2894 dev_err(&pci_dev->dev, "mixer_new failed\n");
2895 goto __nodev;
2898 err = hpi_mixer_get_control(asihpi->h_mixer,
2899 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2900 HPI_CONTROL_SAMPLECLOCK, &h_control);
2902 if (!err)
2903 err = hpi_sample_clock_set_local_rate(
2904 h_control, adapter_fs);
2906 snd_asihpi_proc_init(asihpi);
2908 /* always create, can be enabled or disabled dynamically
2909 by enable_hwdep module param*/
2910 snd_asihpi_hpi_new(asihpi, 0);
2912 strcpy(card->driver, "ASIHPI");
2914 sprintf(card->shortname, "AudioScience ASI%4X",
2915 asihpi->hpi->adapter->type);
2916 sprintf(card->longname, "%s %i",
2917 card->shortname, adapter_index);
2918 err = snd_card_register(card);
2920 if (!err) {
2921 dev++;
2922 return 0;
2924 __nodev:
2925 snd_card_free(card);
2926 dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
2927 return err;
2931 static void snd_asihpi_remove(struct pci_dev *pci_dev)
2933 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
2935 /* Stop interrupts */
2936 if (hpi->interrupt_mode) {
2937 hpi->interrupt_callback = NULL;
2938 hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
2939 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
2942 snd_card_free(hpi->snd_card);
2943 hpi->snd_card = NULL;
2944 asihpi_adapter_remove(pci_dev);
2947 static const struct pci_device_id asihpi_pci_tbl[] = {
2948 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2949 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2950 (kernel_ulong_t)HPI_6205},
2951 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2952 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2953 (kernel_ulong_t)HPI_6000},
2954 {0,}
2956 MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2958 static struct pci_driver driver = {
2959 .name = KBUILD_MODNAME,
2960 .id_table = asihpi_pci_tbl,
2961 .probe = snd_asihpi_probe,
2962 .remove = snd_asihpi_remove,
2965 static int __init snd_asihpi_init(void)
2967 asihpi_init();
2968 return pci_register_driver(&driver);
2971 static void __exit snd_asihpi_exit(void)
2974 pci_unregister_driver(&driver);
2975 asihpi_exit();
2978 module_init(snd_asihpi_init)
2979 module_exit(snd_asihpi_exit)