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