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