blob: b941d2541ddab492b2b1ac97f36c6fda938f77eb [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#include "hpi_internal.h"
26#include "hpimsginit.h"
27#include "hpioctl.h"
28
29#include <linux/pci.h>
30#include <linux/init.h>
31#include <linux/jiffies.h>
32#include <linux/slab.h>
33#include <linux/time.h>
34#include <linux/wait.h>
35#include <sound/core.h>
36#include <sound/control.h>
37#include <sound/pcm.h>
38#include <sound/pcm_params.h>
39#include <sound/info.h>
40#include <sound/initval.h>
41#include <sound/tlv.h>
42#include <sound/hwdep.h>
43
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +120044
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020045MODULE_LICENSE("GPL");
46MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
48
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120049#if defined CONFIG_SND_DEBUG
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120050/* copied from pcm_lib.c, hope later patch will make that version public
51and this copy can be removed */
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120052static inline void
53snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120054{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120055 snprintf(buf, size, "pcmC%dD%d%c:%d",
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120056 substream->pcm->card->number,
57 substream->pcm->device,
58 substream->stream ? 'c' : 'p',
59 substream->number);
60}
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130061#else
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +120062static inline void
63snd_pcm_debug_name(struct snd_pcm_substream *substream, char *buf, size_t size)
64{
65 *buf = 0;
66}
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120067#endif
Eliot Blennerhassetta6477132011-04-05 20:55:42 +120068
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130069#if defined CONFIG_SND_DEBUG_VERBOSE
70/**
71 * snd_printddd - very verbose debug printk
72 * @format: format string
73 *
74 * Works like snd_printk() for debugging purposes.
75 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
76 * Must set snd module debug parameter to 3 to enable at runtime.
77 */
78#define snd_printddd(format, args...) \
79 __snd_printk(3, __FILE__, __LINE__, format, ##args)
80#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120081#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130082#endif
83
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020084static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
85static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
86static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
87static int enable_hpi_hwdep = 1;
88
89module_param_array(index, int, NULL, S_IRUGO);
90MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
91
92module_param_array(id, charp, NULL, S_IRUGO);
93MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
94
95module_param_array(enable, bool, NULL, S_IRUGO);
96MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
97
98module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
99MODULE_PARM_DESC(enable_hpi_hwdep,
100 "ALSA enable HPI hwdep for AudioScience soundcard ");
101
102/* identify driver */
103#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300104static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200105module_param(build_info, charp, S_IRUGO);
106MODULE_PARM_DESC(build_info, "built using headers from kernel source");
107#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300108static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200109module_param(build_info, charp, S_IRUGO);
110MODULE_PARM_DESC(build_info, "built within ALSA source");
111#endif
112
113/* set to 1 to dump every control from adapter to log */
114static const int mixer_dump;
115
116#define DEFAULT_SAMPLERATE 44100
117static int adapter_fs = DEFAULT_SAMPLERATE;
118
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200119/* defaults */
120#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300121#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200122#define BUFFER_BYTES_MAX (512 * 1024)
123
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200124#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
125
126struct clk_source {
127 int source;
128 int index;
129 char *name;
130};
131
132struct clk_cache {
133 int count;
134 int has_local;
135 struct clk_source s[MAX_CLOCKSOURCES];
136};
137
138/* Per card data */
139struct snd_card_asihpi {
140 struct snd_card *card;
141 struct pci_dev *pci;
142 u16 adapter_index;
143 u32 serial_number;
144 u16 type;
145 u16 version;
146 u16 num_outstreams;
147 u16 num_instreams;
148
149 u32 h_mixer;
150 struct clk_cache cc;
151
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200152 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200153 u16 support_grouping;
154 u16 support_mrx;
155 u16 update_interval_frames;
156 u16 in_max_chans;
157 u16 out_max_chans;
158};
159
160/* Per stream data */
161struct snd_card_asihpi_pcm {
162 struct timer_list timer;
163 unsigned int respawn_timer;
164 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300165 unsigned int buffer_bytes;
166 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200167 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300168 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
169 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
170 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200171 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200172 struct snd_pcm_substream *substream;
173 u32 h_stream;
174 struct hpi_format format;
175};
176
177/* universal stream verbs work with out or in stream handles */
178
179/* Functions to allow driver to give a buffer to HPI for busmastering */
180
181static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200182 u32 h_stream, /* handle to outstream. */
183 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
184 u32 pci_address
185)
186{
187 struct hpi_message hm;
188 struct hpi_response hr;
189 unsigned int obj = hpi_handle_object(h_stream);
190
191 if (!h_stream)
192 return HPI_ERROR_INVALID_OBJ;
193 hpi_init_message_response(&hm, &hr, obj,
194 obj == HPI_OBJ_OSTREAM ?
195 HPI_OSTREAM_HOSTBUFFER_ALLOC :
196 HPI_ISTREAM_HOSTBUFFER_ALLOC);
197
198 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
199 &hm.obj_index);
200
201 hm.u.d.u.buffer.buffer_size = size_in_bytes;
202 hm.u.d.u.buffer.pci_address = pci_address;
203 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
204 hpi_send_recv(&hm, &hr);
205 return hr.error;
206}
207
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300208static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200209{
210 struct hpi_message hm;
211 struct hpi_response hr;
212 unsigned int obj = hpi_handle_object(h_stream);
213
214 if (!h_stream)
215 return HPI_ERROR_INVALID_OBJ;
216
217 hpi_init_message_response(&hm, &hr, obj,
218 obj == HPI_OBJ_OSTREAM ?
219 HPI_OSTREAM_HOSTBUFFER_FREE :
220 HPI_ISTREAM_HOSTBUFFER_FREE);
221
222 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
223 &hm.obj_index);
224 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
225 hpi_send_recv(&hm, &hr);
226 return hr.error;
227}
228
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300229static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200230{
231 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300232 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200233 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300234 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200235}
236
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300237static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200238{
239 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300240 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200241 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300242 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243}
244
245static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200246 u32 h_stream,
247 u16 *pw_state,
248 u32 *pbuffer_size,
249 u32 *pdata_in_buffer,
250 u32 *psample_count,
251 u32 *pauxiliary_data
252)
253{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300254 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200255 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257 pbuffer_size, pdata_in_buffer,
258 psample_count, pauxiliary_data);
259 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300260 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261 pbuffer_size, pdata_in_buffer,
262 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300263 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264}
265
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300266static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267 u32 h_master,
268 u32 h_stream)
269{
270 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300273 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200274}
275
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300276static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200277{
278 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300279 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200280 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300281 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200282}
283
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300284static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200285 u32 h_stream, u32 *mo, u32 *mi)
286{
287 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300288 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200289 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300290 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200291}
292
293static u16 handle_error(u16 err, int line, char *filename)
294{
295 if (err)
296 printk(KERN_WARNING
297 "in file %s, line %d: HPI error %d\n",
298 filename, line, err);
299 return err;
300}
301
302#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
303
304/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200305
306static void print_hwparams(struct snd_pcm_substream *substream,
307 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200308{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200309 char name[16];
310 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200311 snd_printd("%s HWPARAMS\n", name);
312 snd_printd(" samplerate %d Hz\n", params_rate(p));
313 snd_printd(" channels %d\n", params_channels(p));
314 snd_printd(" format %d\n", params_format(p));
315 snd_printd(" subformat %d\n", params_subformat(p));
316 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
317 snd_printd(" period %d B\n", params_period_bytes(p));
318 snd_printd(" access %d\n", params_access(p));
319 snd_printd(" period_size %d\n", params_period_size(p));
320 snd_printd(" periods %d\n", params_periods(p));
321 snd_printd(" buffer_size %d\n", params_buffer_size(p));
322 snd_printd(" %d B/s\n", params_rate(p) *
323 params_channels(p) *
324 snd_pcm_format_width(params_format(p)) / 8);
325
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200326}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200327
328static snd_pcm_format_t hpi_to_alsa_formats[] = {
329 -1, /* INVALID */
330 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
331 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
332 -1, /* HPI_FORMAT_MPEG_L1 3 */
333 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
334 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
335 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
336 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
337 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
338 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
339 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
340 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
341 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
342 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
343 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
344#if 1
345 /* ALSA can't handle 3 byte sample size together with power-of-2
346 * constraint on buffer_bytes, so disable this format
347 */
348 -1
349#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300350 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200351#endif
352};
353
354
355static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
356 u16 *hpi_format)
357{
358 u16 format;
359
360 for (format = HPI_FORMAT_PCM8_UNSIGNED;
361 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
362 if (hpi_to_alsa_formats[format] == alsa_format) {
363 *hpi_format = format;
364 return 0;
365 }
366 }
367
368 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
369 alsa_format);
370 *hpi_format = 0;
371 return -EINVAL;
372}
373
374static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
375 struct snd_pcm_hardware *pcmhw)
376{
377 u16 err;
378 u32 h_control;
379 u32 sample_rate;
380 int idx;
381 unsigned int rate_min = 200000;
382 unsigned int rate_max = 0;
383 unsigned int rates = 0;
384
385 if (asihpi->support_mrx) {
386 rates |= SNDRV_PCM_RATE_CONTINUOUS;
387 rates |= SNDRV_PCM_RATE_8000_96000;
388 rate_min = 8000;
389 rate_max = 100000;
390 } else {
391 /* on cards without SRC,
392 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300393 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200394 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
395 HPI_CONTROL_SAMPLECLOCK, &h_control);
396 if (err) {
397 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300398 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200399 }
400
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300401 for (idx = -1; idx < 100; idx++) {
402 if (idx == -1) {
403 if (hpi_sample_clock_get_sample_rate(h_control,
404 &sample_rate))
405 continue;
406 } else if (hpi_sample_clock_query_local_rate(h_control,
407 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200408 break;
409 }
410
411 rate_min = min(rate_min, sample_rate);
412 rate_max = max(rate_max, sample_rate);
413
414 switch (sample_rate) {
415 case 5512:
416 rates |= SNDRV_PCM_RATE_5512;
417 break;
418 case 8000:
419 rates |= SNDRV_PCM_RATE_8000;
420 break;
421 case 11025:
422 rates |= SNDRV_PCM_RATE_11025;
423 break;
424 case 16000:
425 rates |= SNDRV_PCM_RATE_16000;
426 break;
427 case 22050:
428 rates |= SNDRV_PCM_RATE_22050;
429 break;
430 case 32000:
431 rates |= SNDRV_PCM_RATE_32000;
432 break;
433 case 44100:
434 rates |= SNDRV_PCM_RATE_44100;
435 break;
436 case 48000:
437 rates |= SNDRV_PCM_RATE_48000;
438 break;
439 case 64000:
440 rates |= SNDRV_PCM_RATE_64000;
441 break;
442 case 88200:
443 rates |= SNDRV_PCM_RATE_88200;
444 break;
445 case 96000:
446 rates |= SNDRV_PCM_RATE_96000;
447 break;
448 case 176400:
449 rates |= SNDRV_PCM_RATE_176400;
450 break;
451 case 192000:
452 rates |= SNDRV_PCM_RATE_192000;
453 break;
454 default: /* some other rate */
455 rates |= SNDRV_PCM_RATE_KNOT;
456 }
457 }
458 }
459
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200460 pcmhw->rates = rates;
461 pcmhw->rate_min = rate_min;
462 pcmhw->rate_max = rate_max;
463}
464
465static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
466 struct snd_pcm_hw_params *params)
467{
468 struct snd_pcm_runtime *runtime = substream->runtime;
469 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
470 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
471 int err;
472 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400473 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200474 unsigned int bytes_per_sec;
475
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200476 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200477 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
478 if (err < 0)
479 return err;
480 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
481 if (err)
482 return err;
483
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200484 hpi_handle_error(hpi_format_create(&dpcm->format,
485 params_channels(params),
486 format, params_rate(params), 0, 0));
487
488 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300489 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200490 return -EINVAL;
491
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300492 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200493 dpcm->h_stream, &dpcm->format) != 0)
494 return -EINVAL;
495 }
496
497 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200498 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300499 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500 params_buffer_bytes(params), runtime->dma_addr);
501 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300502 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200503 "stream_host_buffer_attach succeeded %u %lu\n",
504 params_buffer_bytes(params),
505 (unsigned long)runtime->dma_addr);
506 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300507 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200508 err);
509 return -ENOMEM;
510 }
511
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300512 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200513 &dpcm->hpi_buffer_attached,
514 NULL, NULL, NULL);
515
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300516 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200517 dpcm->hpi_buffer_attached);
518 }
519 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400520 width = snd_pcm_format_width(params_format(params));
521 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200522 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400523 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200524 return -EINVAL;
525
526 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300527 dpcm->buffer_bytes = params_buffer_bytes(params);
528 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200529
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200530 return 0;
531}
532
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300533static int
534snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
535{
536 struct snd_pcm_runtime *runtime = substream->runtime;
537 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
538 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300539 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300540
541 snd_pcm_lib_free_pages(substream);
542 return 0;
543}
544
545static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
546{
547 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
548 kfree(dpcm);
549}
550
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200551static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
552 substream)
553{
554 struct snd_pcm_runtime *runtime = substream->runtime;
555 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
556 int expiry;
557
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300558 expiry = HZ / 200;
559 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300560 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200561 dpcm->timer.expires = jiffies + expiry;
562 dpcm->respawn_timer = 1;
563 add_timer(&dpcm->timer);
564}
565
566static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
567{
568 struct snd_pcm_runtime *runtime = substream->runtime;
569 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
570
571 dpcm->respawn_timer = 0;
572 del_timer(&dpcm->timer);
573}
574
575static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
576 int cmd)
577{
578 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
579 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
580 struct snd_pcm_substream *s;
581 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200582 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200584 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200585 snd_printdd("%s trigger\n", name);
586
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200587 switch (cmd) {
588 case SNDRV_PCM_TRIGGER_START:
589 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300590 struct snd_pcm_runtime *runtime = s->runtime;
591 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592
593 if (snd_pcm_substream_chip(s) != card)
594 continue;
595
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300596 /* don't link Cap and Play */
597 if (substream->stream != s->stream)
598 continue;
599
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200600 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200601 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300603 * in buffer? Must be at least one period!
604 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200605 * buffer is bigger it may contain even more
606 * data??
607 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300608 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300609 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200610 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300611 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300612 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200613 preload,
614 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300615 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200616 }
617
618 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200619 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300620 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200621 dpcm->h_stream,
622 ds->h_stream);
623 if (!e) {
624 snd_pcm_trigger_done(s, substream);
625 } else {
626 hpi_handle_error(e);
627 break;
628 }
629 } else
630 break;
631 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300632 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200633 /* start the master stream */
634 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300635 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200636 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300637 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200638 break;
639
640 case SNDRV_PCM_TRIGGER_STOP:
641 snd_card_asihpi_pcm_timer_stop(substream);
642 snd_pcm_group_for_each_entry(s, substream) {
643 if (snd_pcm_substream_chip(s) != card)
644 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300645 /* don't link Cap and Play */
646 if (substream->stream != s->stream)
647 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200648
649 /*? workaround linked streams don't
650 transition to SETUP 20070706*/
651 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
652
653 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200654 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200655 snd_pcm_trigger_done(s, substream);
656 } else
657 break;
658 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300659 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660
661 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300662 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200663 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
664 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300665 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200666
667 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300668 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200669 break;
670
671 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300672 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300673 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674 snd_card_asihpi_pcm_timer_start(substream);
675 break;
676 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300677 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200680 break;
681 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300682 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200683 return -EINVAL;
684 }
685
686 return 0;
687}
688
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200689/*algorithm outline
690 Without linking degenerates to getting single stream pos etc
691 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
692*/
693/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300694pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200695for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300696 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300697 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300698 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200699}
700timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
701for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300702 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300703 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200704 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300705 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200706 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300707 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200708 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300709 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200710 }
711 }
712 snd_pcm_period_elapsed(s);
713 }
714}
715*/
716
717/** Minimum of 2 modulo values. Works correctly when the difference between
718* the values is less than half the modulus
719*/
720static inline unsigned int modulo_min(unsigned int a, unsigned int b,
721 unsigned long int modulus)
722{
723 unsigned int result;
724 if (((a-b) % modulus) < (modulus/2))
725 result = b;
726 else
727 result = a;
728
729 return result;
730}
731
732/** Timer function, equivalent to interrupt service routine for cards
733*/
734static void snd_card_asihpi_timer_function(unsigned long data)
735{
736 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300737 struct snd_pcm_substream *substream = dpcm->substream;
738 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200739 struct snd_pcm_runtime *runtime;
740 struct snd_pcm_substream *s;
741 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300742 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200743 unsigned int remdata, xfercount, next_jiffies;
744 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300745 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200746 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300747 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200748 char name[16];
749
750 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200752 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300753
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200754 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300755 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200756 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
757 runtime = s->runtime;
758
759 if (snd_pcm_substream_chip(s) != card)
760 continue;
761
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300762 /* don't link Cap and Play */
763 if (substream->stream != s->stream)
764 continue;
765
766 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200767 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300768 &buffer_size, &bytes_avail,
769 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200770
771 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300772 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200773
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200774 if (!card->can_dma)
775 on_card_bytes = bytes_avail;
776
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300777 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300778 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300779 if (state == HPI_STATE_STOPPED) {
780 if ((bytes_avail == 0) &&
781 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
782 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300783 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200784 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300785 }
786 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300787 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300788 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200789 ds->drained_count++;
790 if (ds->drained_count > 2) {
791 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
792 continue;
793 }
794 } else {
795 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300796 }
797 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300798 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200799
800 if (first) {
801 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300802 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300803 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200804 first = 0;
805 } else {
806 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300807 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200808 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300809 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200810 newdata);
811 }
812
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200813 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200814 (unsigned long)frames_to_bytes(runtime,
815 runtime->status->hw_ptr),
816 (unsigned long)frames_to_bytes(runtime,
817 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300818
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200819 snd_printdd("%d S=%d, "
820 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
821 "aux=0x%04X space=0x%04X\n",
822 s->number, state,
823 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
824 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300825 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300826 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200827 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300828 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200829
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300830 remdata = newdata % dpcm->period_bytes;
831 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300832 /* come back when on_card_bytes has decreased enough to allow
833 write to happen, or when data has been consumed to make another
834 period
835 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300836 if (xfercount && (on_card_bytes > dpcm->period_bytes))
837 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300838 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300839 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300840
841 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200842 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200843 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300844 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
845
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300846 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200847 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200848
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300849 /* don't link Cap and Play */
850 if (substream->stream != s->stream)
851 continue;
852
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300853 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
854
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200855 if (xfercount &&
856 /* Limit use of on card fifo for playback */
857 ((on_card_bytes <= ds->period_bytes) ||
858 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
859
860 {
861
862 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
863 unsigned int xfer1, xfer2;
864 char *pd = &s->runtime->dma_area[buf_ofs];
865
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200866 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200867 xfer1 = xfercount;
868 xfer2 = 0;
869 } else {
870 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
871 xfer2 = xfercount - xfer1;
872 }
873
874 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
875 snd_printddd("P%d write1 0x%04X 0x%04X\n",
876 s->number, xfer1, buf_ofs);
877 hpi_handle_error(
878 hpi_outstream_write_buf(
879 ds->h_stream, pd, xfer1,
880 &ds->format));
881
882 if (xfer2) {
883 pd = s->runtime->dma_area;
884
885 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200886 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200887 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200888 hpi_handle_error(
889 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200890 ds->h_stream, pd,
891 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200892 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200893 }
894 } else {
895 snd_printddd("C%d read1 0x%04x\n",
896 s->number, xfer1);
897 hpi_handle_error(
898 hpi_instream_read_buf(
899 ds->h_stream,
900 pd, xfer1));
901 if (xfer2) {
902 pd = s->runtime->dma_area;
903 snd_printddd("C%d read2 0x%04x\n",
904 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200905 hpi_handle_error(
906 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300907 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200908 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200909 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200910 }
911 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300912 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200913 snd_pcm_period_elapsed(s);
914 }
915 }
916
917 if (dpcm->respawn_timer)
918 add_timer(&dpcm->timer);
919}
920
921/***************************** PLAYBACK OPS ****************/
922static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
923 unsigned int cmd, void *arg)
924{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200925 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200926 return snd_pcm_lib_ioctl(substream, cmd, arg);
927}
928
929static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
930 substream)
931{
932 struct snd_pcm_runtime *runtime = substream->runtime;
933 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
934
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200935 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200936
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300937 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300938 dpcm->pcm_buf_host_rw_ofs = 0;
939 dpcm->pcm_buf_dma_ofs = 0;
940 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200941 return 0;
942}
943
944static snd_pcm_uframes_t
945snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
946{
947 struct snd_pcm_runtime *runtime = substream->runtime;
948 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
949 snd_pcm_uframes_t ptr;
950
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300951 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200952 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200953 return ptr;
954}
955
956static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
957 u32 h_stream,
958 struct snd_pcm_hardware *pcmhw)
959{
960 struct hpi_format hpi_format;
961 u16 format;
962 u16 err;
963 u32 h_control;
964 u32 sample_rate = 48000;
965
966 /* on cards without SRC, must query at valid rate,
967 * maybe set by external sync
968 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300969 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200970 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
971 HPI_CONTROL_SAMPLECLOCK, &h_control);
972
973 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300974 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200975 &sample_rate);
976
977 for (format = HPI_FORMAT_PCM8_UNSIGNED;
978 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
979 err = hpi_format_create(&hpi_format,
980 2, format, sample_rate, 128000, 0);
981 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300982 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200983 &hpi_format);
984 if (!err && (hpi_to_alsa_formats[format] != -1))
985 pcmhw->formats |=
986 (1ULL << hpi_to_alsa_formats[format]);
987 }
988}
989
990static struct snd_pcm_hardware snd_card_asihpi_playback = {
991 .channels_min = 1,
992 .channels_max = 2,
993 .buffer_bytes_max = BUFFER_BYTES_MAX,
994 .period_bytes_min = PERIOD_BYTES_MIN,
995 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
996 .periods_min = PERIODS_MIN,
997 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
998 .fifo_size = 0,
999};
1000
1001static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1002{
1003 struct snd_pcm_runtime *runtime = substream->runtime;
1004 struct snd_card_asihpi_pcm *dpcm;
1005 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1006 int err;
1007
1008 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1009 if (dpcm == NULL)
1010 return -ENOMEM;
1011
1012 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001013 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001014 substream->number, &dpcm->h_stream);
1015 hpi_handle_error(err);
1016 if (err)
1017 kfree(dpcm);
1018 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1019 return -EBUSY;
1020 if (err)
1021 return -EIO;
1022
1023 /*? also check ASI5000 samplerate source
1024 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001025 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001026 */
1027
1028 init_timer(&dpcm->timer);
1029 dpcm->timer.data = (unsigned long) dpcm;
1030 dpcm->timer.function = snd_card_asihpi_timer_function;
1031 dpcm->substream = substream;
1032 runtime->private_data = dpcm;
1033 runtime->private_free = snd_card_asihpi_runtime_free;
1034
1035 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1036 /*?snd_card_asihpi_playback.period_bytes_min =
1037 card->out_max_chans * 4096; */
1038
1039 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1040 &snd_card_asihpi_playback);
1041
1042 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1043
1044 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1045 SNDRV_PCM_INFO_DOUBLE |
1046 SNDRV_PCM_INFO_BATCH |
1047 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001048 SNDRV_PCM_INFO_PAUSE |
1049 SNDRV_PCM_INFO_MMAP |
1050 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001051
1052 if (card->support_grouping)
1053 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1054
1055 /* struct is copied, so can create initializer dynamically */
1056 runtime->hw = snd_card_asihpi_playback;
1057
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001058 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001059 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1060 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1061 if (err < 0)
1062 return err;
1063
1064 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1065 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001066
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001067 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001068 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001069
1070 snd_pcm_set_sync(substream);
1071
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001072 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001073
1074 return 0;
1075}
1076
1077static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1078{
1079 struct snd_pcm_runtime *runtime = substream->runtime;
1080 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1081
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001082 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001083 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001084
1085 return 0;
1086}
1087
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001088static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1089 .open = snd_card_asihpi_playback_open,
1090 .close = snd_card_asihpi_playback_close,
1091 .ioctl = snd_card_asihpi_playback_ioctl,
1092 .hw_params = snd_card_asihpi_pcm_hw_params,
1093 .hw_free = snd_card_asihpi_hw_free,
1094 .prepare = snd_card_asihpi_playback_prepare,
1095 .trigger = snd_card_asihpi_trigger,
1096 .pointer = snd_card_asihpi_playback_pointer,
1097};
1098
1099/***************************** CAPTURE OPS ****************/
1100static snd_pcm_uframes_t
1101snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1102{
1103 struct snd_pcm_runtime *runtime = substream->runtime;
1104 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1105
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001106 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001107 substream->number, dpcm->pcm_buf_dma_ofs);
1108 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001109 for the capture position, because those samples aren't yet in
1110 the local buffer available for reading.
1111 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001112 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001113}
1114
1115static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1116 unsigned int cmd, void *arg)
1117{
1118 return snd_pcm_lib_ioctl(substream, cmd, arg);
1119}
1120
1121static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1122{
1123 struct snd_pcm_runtime *runtime = substream->runtime;
1124 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1125
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001126 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001127 dpcm->pcm_buf_host_rw_ofs = 0;
1128 dpcm->pcm_buf_dma_ofs = 0;
1129 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001130
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001131 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001132 return 0;
1133}
1134
1135
1136
1137static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1138 u32 h_stream,
1139 struct snd_pcm_hardware *pcmhw)
1140{
1141 struct hpi_format hpi_format;
1142 u16 format;
1143 u16 err;
1144 u32 h_control;
1145 u32 sample_rate = 48000;
1146
1147 /* on cards without SRC, must query at valid rate,
1148 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001149 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001150 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1151 HPI_CONTROL_SAMPLECLOCK, &h_control);
1152
1153 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001154 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155 &sample_rate);
1156
1157 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1158 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1159
1160 err = hpi_format_create(&hpi_format, 2, format,
1161 sample_rate, 128000, 0);
1162 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001163 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001164 &hpi_format);
1165 if (!err)
1166 pcmhw->formats |=
1167 (1ULL << hpi_to_alsa_formats[format]);
1168 }
1169}
1170
1171
1172static struct snd_pcm_hardware snd_card_asihpi_capture = {
1173 .channels_min = 1,
1174 .channels_max = 2,
1175 .buffer_bytes_max = BUFFER_BYTES_MAX,
1176 .period_bytes_min = PERIOD_BYTES_MIN,
1177 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1178 .periods_min = PERIODS_MIN,
1179 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1180 .fifo_size = 0,
1181};
1182
1183static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1184{
1185 struct snd_pcm_runtime *runtime = substream->runtime;
1186 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1187 struct snd_card_asihpi_pcm *dpcm;
1188 int err;
1189
1190 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1191 if (dpcm == NULL)
1192 return -ENOMEM;
1193
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001194 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001195 card->adapter_index, substream->number);
1196
1197 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001198 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001199 substream->number, &dpcm->h_stream));
1200 if (err)
1201 kfree(dpcm);
1202 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1203 return -EBUSY;
1204 if (err)
1205 return -EIO;
1206
1207
1208 init_timer(&dpcm->timer);
1209 dpcm->timer.data = (unsigned long) dpcm;
1210 dpcm->timer.function = snd_card_asihpi_timer_function;
1211 dpcm->substream = substream;
1212 runtime->private_data = dpcm;
1213 runtime->private_free = snd_card_asihpi_runtime_free;
1214
1215 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1216 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1217 &snd_card_asihpi_capture);
1218 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001219 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1220 SNDRV_PCM_INFO_MMAP |
1221 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001222
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001223 if (card->support_grouping)
1224 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1225
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001226 runtime->hw = snd_card_asihpi_capture;
1227
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001228 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001229 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1230 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1231 if (err < 0)
1232 return err;
1233
1234 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1235 card->update_interval_frames);
1236 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1237 card->update_interval_frames * 2, UINT_MAX);
1238
1239 snd_pcm_set_sync(substream);
1240
1241 return 0;
1242}
1243
1244static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1245{
1246 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1247
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001248 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001249 return 0;
1250}
1251
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001252static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1253 .open = snd_card_asihpi_capture_open,
1254 .close = snd_card_asihpi_capture_close,
1255 .ioctl = snd_card_asihpi_capture_ioctl,
1256 .hw_params = snd_card_asihpi_pcm_hw_params,
1257 .hw_free = snd_card_asihpi_hw_free,
1258 .prepare = snd_card_asihpi_capture_prepare,
1259 .trigger = snd_card_asihpi_trigger,
1260 .pointer = snd_card_asihpi_capture_pointer,
1261};
1262
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001263static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1264 int device, int substreams)
1265{
1266 struct snd_pcm *pcm;
1267 int err;
1268
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001269 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001270 asihpi->num_outstreams, asihpi->num_instreams,
1271 &pcm);
1272 if (err < 0)
1273 return err;
1274 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001275 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1276 &snd_card_asihpi_playback_mmap_ops);
1277 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1278 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001279
1280 pcm->private_data = asihpi;
1281 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001282 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001283
1284 /*? do we want to emulate MMAP for non-BBM cards?
1285 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1286 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1287 snd_dma_pci_data(asihpi->pci),
1288 64*1024, BUFFER_BYTES_MAX);
1289
1290 return 0;
1291}
1292
1293/***************************** MIXER CONTROLS ****************/
1294struct hpi_control {
1295 u32 h_control;
1296 u16 control_type;
1297 u16 src_node_type;
1298 u16 src_node_index;
1299 u16 dst_node_type;
1300 u16 dst_node_index;
1301 u16 band;
1302 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1303};
1304
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001305static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001306 "invalid",
1307 "AM",
1308 "FM mono",
1309 "TV NTSC-M",
1310 "FM stereo",
1311 "AUX",
1312 "TV PAL BG",
1313 "TV PAL I",
1314 "TV PAL DK",
1315 "TV SECAM",
1316};
1317
1318compile_time_assert(
1319 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1320 (HPI_TUNER_BAND_LAST+1)),
1321 assert_tuner_band_names_size);
1322
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001323static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001324 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001325 "PCM",
1326 "Line",
1327 "Digital",
1328 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001329 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001330 "Clock",
1331 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001332 "Mic",
1333 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001334 "Analog",
1335 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001336 "RTP",
1337 "GPI",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001338};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001339
1340compile_time_assert(
1341 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001342 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001343 assert_src_names_size);
1344
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001345static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001346 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001347 "PCM",
1348 "Line",
1349 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001350 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001351 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001352 "Net",
1353 "Analog",
1354 "RTP",
1355 "GPO",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001356};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001357
1358compile_time_assert(
1359 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001360 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001361 assert_dst_names_size);
1362
1363static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1364 struct snd_card_asihpi *asihpi)
1365{
1366 int err;
1367
1368 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1369 if (err < 0)
1370 return err;
1371 else if (mixer_dump)
1372 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1373
1374 return 0;
1375}
1376
1377/* Convert HPI control name and location into ALSA control name */
1378static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1379 struct hpi_control *hpi_ctl,
1380 char *name)
1381{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001382 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001383 memset(snd_control, 0, sizeof(*snd_control));
1384 snd_control->name = hpi_ctl->name;
1385 snd_control->private_value = hpi_ctl->h_control;
1386 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1387 snd_control->index = 0;
1388
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001389 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1390 dir = ""; /* clock is neither capture nor playback */
1391 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001392 dir = "Capture "; /* On or towards a PCM capture destination*/
1393 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1394 (!hpi_ctl->dst_node_type))
1395 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001396 else if (hpi_ctl->src_node_type &&
1397 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001398 (hpi_ctl->dst_node_type))
1399 dir = "Monitor Playback "; /* Between an input and an output */
1400 else
1401 dir = "Playback "; /* PCM Playback source, or output node */
1402
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001403 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001404 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001405 asihpi_src_names[hpi_ctl->src_node_type],
1406 hpi_ctl->src_node_index,
1407 asihpi_dst_names[hpi_ctl->dst_node_type],
1408 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001409 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001410 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001411 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001412 asihpi_dst_names[hpi_ctl->dst_node_type],
1413 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001414 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001415 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001416 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001417 asihpi_src_names[hpi_ctl->src_node_type],
1418 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001419 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001420 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001421 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1422 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001423}
1424
1425/*------------------------------------------------------------
1426 Volume controls
1427 ------------------------------------------------------------*/
1428#define VOL_STEP_mB 1
1429static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_info *uinfo)
1431{
1432 u32 h_control = kcontrol->private_value;
1433 u16 err;
1434 /* native gains are in millibels */
1435 short min_gain_mB;
1436 short max_gain_mB;
1437 short step_gain_mB;
1438
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001439 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001440 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1441 if (err) {
1442 max_gain_mB = 0;
1443 min_gain_mB = -10000;
1444 step_gain_mB = VOL_STEP_mB;
1445 }
1446
1447 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1448 uinfo->count = 2;
1449 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1450 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1451 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1452 return 0;
1453}
1454
1455static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1456 struct snd_ctl_elem_value *ucontrol)
1457{
1458 u32 h_control = kcontrol->private_value;
1459 short an_gain_mB[HPI_MAX_CHANNELS];
1460
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001461 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001462 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1463 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1464
1465 return 0;
1466}
1467
1468static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
1470{
1471 int change;
1472 u32 h_control = kcontrol->private_value;
1473 short an_gain_mB[HPI_MAX_CHANNELS];
1474
1475 an_gain_mB[0] =
1476 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1477 an_gain_mB[1] =
1478 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1479 /* change = asihpi->mixer_volume[addr][0] != left ||
1480 asihpi->mixer_volume[addr][1] != right;
1481 */
1482 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001483 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001484 return change;
1485}
1486
1487static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1488
Takashi Iwai000477a2011-07-22 07:57:44 +02001489#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001490
1491static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1492 struct snd_ctl_elem_value *ucontrol)
1493{
1494 u32 h_control = kcontrol->private_value;
1495 u32 mute;
1496
1497 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1498 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1499
1500 return 0;
1501}
1502
1503static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1505{
1506 u32 h_control = kcontrol->private_value;
1507 int change = 1;
1508 /* HPI currently only supports all or none muting of multichannel volume
1509 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1510 */
1511 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1512 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1513 return change;
1514}
1515
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001516static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1517 struct hpi_control *hpi_ctl)
1518{
1519 struct snd_card *card = asihpi->card;
1520 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001521 int err;
1522 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001523
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001524 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001525 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1526 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1527 snd_control.info = snd_asihpi_volume_info;
1528 snd_control.get = snd_asihpi_volume_get;
1529 snd_control.put = snd_asihpi_volume_put;
1530 snd_control.tlv.p = db_scale_100;
1531
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001532 err = ctl_add(card, &snd_control, asihpi);
1533 if (err)
1534 return err;
1535
1536 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1537 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1538 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1539 snd_control.info = snd_asihpi_volume_mute_info;
1540 snd_control.get = snd_asihpi_volume_mute_get;
1541 snd_control.put = snd_asihpi_volume_mute_put;
1542 err = ctl_add(card, &snd_control, asihpi);
1543 }
1544 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001545}
1546
1547/*------------------------------------------------------------
1548 Level controls
1549 ------------------------------------------------------------*/
1550static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1551 struct snd_ctl_elem_info *uinfo)
1552{
1553 u32 h_control = kcontrol->private_value;
1554 u16 err;
1555 short min_gain_mB;
1556 short max_gain_mB;
1557 short step_gain_mB;
1558
1559 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001560 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001561 &max_gain_mB, &step_gain_mB);
1562 if (err) {
1563 max_gain_mB = 2400;
1564 min_gain_mB = -1000;
1565 step_gain_mB = 100;
1566 }
1567
1568 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1569 uinfo->count = 2;
1570 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1571 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1572 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1573 return 0;
1574}
1575
1576static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
1578{
1579 u32 h_control = kcontrol->private_value;
1580 short an_gain_mB[HPI_MAX_CHANNELS];
1581
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001582 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001583 ucontrol->value.integer.value[0] =
1584 an_gain_mB[0] / HPI_UNITS_PER_dB;
1585 ucontrol->value.integer.value[1] =
1586 an_gain_mB[1] / HPI_UNITS_PER_dB;
1587
1588 return 0;
1589}
1590
1591static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1592 struct snd_ctl_elem_value *ucontrol)
1593{
1594 int change;
1595 u32 h_control = kcontrol->private_value;
1596 short an_gain_mB[HPI_MAX_CHANNELS];
1597
1598 an_gain_mB[0] =
1599 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1600 an_gain_mB[1] =
1601 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1602 /* change = asihpi->mixer_level[addr][0] != left ||
1603 asihpi->mixer_level[addr][1] != right;
1604 */
1605 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001606 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001607 return change;
1608}
1609
1610static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1611
1612static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1613 struct hpi_control *hpi_ctl)
1614{
1615 struct snd_card *card = asihpi->card;
1616 struct snd_kcontrol_new snd_control;
1617
1618 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001619 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001620 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1621 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1622 snd_control.info = snd_asihpi_level_info;
1623 snd_control.get = snd_asihpi_level_get;
1624 snd_control.put = snd_asihpi_level_put;
1625 snd_control.tlv.p = db_scale_level;
1626
1627 return ctl_add(card, &snd_control, asihpi);
1628}
1629
1630/*------------------------------------------------------------
1631 AESEBU controls
1632 ------------------------------------------------------------*/
1633
1634/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001635static const char * const asihpi_aesebu_format_names[] = {
1636 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001637
1638static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_info *uinfo)
1640{
1641 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1642 uinfo->count = 1;
1643 uinfo->value.enumerated.items = 3;
1644
1645 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1646 uinfo->value.enumerated.item =
1647 uinfo->value.enumerated.items - 1;
1648
1649 strcpy(uinfo->value.enumerated.name,
1650 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1651
1652 return 0;
1653}
1654
1655static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1656 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001657 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001658{
1659 u32 h_control = kcontrol->private_value;
1660 u16 source, err;
1661
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001662 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001663
1664 /* default to N/A */
1665 ucontrol->value.enumerated.item[0] = 0;
1666 /* return success but set the control to N/A */
1667 if (err)
1668 return 0;
1669 if (source == HPI_AESEBU_FORMAT_SPDIF)
1670 ucontrol->value.enumerated.item[0] = 1;
1671 if (source == HPI_AESEBU_FORMAT_AESEBU)
1672 ucontrol->value.enumerated.item[0] = 2;
1673
1674 return 0;
1675}
1676
1677static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1678 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001679 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001680{
1681 u32 h_control = kcontrol->private_value;
1682
1683 /* default to S/PDIF */
1684 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1685
1686 if (ucontrol->value.enumerated.item[0] == 1)
1687 source = HPI_AESEBU_FORMAT_SPDIF;
1688 if (ucontrol->value.enumerated.item[0] == 2)
1689 source = HPI_AESEBU_FORMAT_AESEBU;
1690
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001691 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001692 return -EINVAL;
1693
1694 return 1;
1695}
1696
1697static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1698 struct snd_ctl_elem_value *ucontrol) {
1699 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001700 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001701}
1702
1703static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_value *ucontrol) {
1705 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001706 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001707}
1708
1709static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_info *uinfo)
1711{
1712 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1713 uinfo->count = 1;
1714
1715 uinfo->value.integer.min = 0;
1716 uinfo->value.integer.max = 0X1F;
1717 uinfo->value.integer.step = 1;
1718
1719 return 0;
1720}
1721
1722static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1723 struct snd_ctl_elem_value *ucontrol) {
1724
1725 u32 h_control = kcontrol->private_value;
1726 u16 status;
1727
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001728 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1729 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001730 ucontrol->value.integer.value[0] = status;
1731 return 0;
1732}
1733
1734static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1735 struct hpi_control *hpi_ctl)
1736{
1737 struct snd_card *card = asihpi->card;
1738 struct snd_kcontrol_new snd_control;
1739
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001740 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001741 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1742 snd_control.info = snd_asihpi_aesebu_format_info;
1743 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1744 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1745
1746
1747 if (ctl_add(card, &snd_control, asihpi) < 0)
1748 return -EINVAL;
1749
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001750 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001751 snd_control.access =
1752 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1753 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1754 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1755
1756 return ctl_add(card, &snd_control, asihpi);
1757}
1758
1759static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_value *ucontrol) {
1761 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001762 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001763}
1764
1765static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_value *ucontrol) {
1767 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001768 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001769}
1770
1771
1772static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1773 struct hpi_control *hpi_ctl)
1774{
1775 struct snd_card *card = asihpi->card;
1776 struct snd_kcontrol_new snd_control;
1777
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001778 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001779 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1780 snd_control.info = snd_asihpi_aesebu_format_info;
1781 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1782 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1783
1784 return ctl_add(card, &snd_control, asihpi);
1785}
1786
1787/*------------------------------------------------------------
1788 Tuner controls
1789 ------------------------------------------------------------*/
1790
1791/* Gain */
1792
1793static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1794 struct snd_ctl_elem_info *uinfo)
1795{
1796 u32 h_control = kcontrol->private_value;
1797 u16 err;
1798 short idx;
1799 u16 gain_range[3];
1800
1801 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001802 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001803 idx, &gain_range[idx]);
1804 if (err != 0)
1805 return err;
1806 }
1807
1808 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1809 uinfo->count = 1;
1810 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1811 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1812 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1813 return 0;
1814}
1815
1816static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 /*
1820 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1821 */
1822 u32 h_control = kcontrol->private_value;
1823 short gain;
1824
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001825 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001826 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1827
1828 return 0;
1829}
1830
1831static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1832 struct snd_ctl_elem_value *ucontrol)
1833{
1834 /*
1835 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1836 */
1837 u32 h_control = kcontrol->private_value;
1838 short gain;
1839
1840 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001841 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001842
1843 return 1;
1844}
1845
1846/* Band */
1847
1848static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1849 u16 *band_list, u32 len) {
1850 u32 h_control = kcontrol->private_value;
1851 u16 err = 0;
1852 u32 i;
1853
1854 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001855 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001856 h_control, i, &band_list[i]);
1857 if (err != 0)
1858 break;
1859 }
1860
1861 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1862 return -EIO;
1863
1864 return i;
1865}
1866
1867static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_info *uinfo)
1869{
1870 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1871 int num_bands = 0;
1872
1873 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1874 HPI_TUNER_BAND_LAST);
1875
1876 if (num_bands < 0)
1877 return num_bands;
1878
1879 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1880 uinfo->count = 1;
1881 uinfo->value.enumerated.items = num_bands;
1882
1883 if (num_bands > 0) {
1884 if (uinfo->value.enumerated.item >=
1885 uinfo->value.enumerated.items)
1886 uinfo->value.enumerated.item =
1887 uinfo->value.enumerated.items - 1;
1888
1889 strcpy(uinfo->value.enumerated.name,
1890 asihpi_tuner_band_names[
1891 tuner_bands[uinfo->value.enumerated.item]]);
1892
1893 }
1894 return 0;
1895}
1896
1897static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1898 struct snd_ctl_elem_value *ucontrol)
1899{
1900 u32 h_control = kcontrol->private_value;
1901 /*
1902 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1903 */
1904 u16 band, idx;
1905 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1906 u32 num_bands = 0;
1907
1908 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1909 HPI_TUNER_BAND_LAST);
1910
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001911 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001912
1913 ucontrol->value.enumerated.item[0] = -1;
1914 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1915 if (tuner_bands[idx] == band) {
1916 ucontrol->value.enumerated.item[0] = idx;
1917 break;
1918 }
1919
1920 return 0;
1921}
1922
1923static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1924 struct snd_ctl_elem_value *ucontrol)
1925{
1926 /*
1927 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1928 */
1929 u32 h_control = kcontrol->private_value;
1930 u16 band;
1931 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1932 u32 num_bands = 0;
1933
1934 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1935 HPI_TUNER_BAND_LAST);
1936
1937 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001938 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001939
1940 return 1;
1941}
1942
1943/* Freq */
1944
1945static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_info *uinfo)
1947{
1948 u32 h_control = kcontrol->private_value;
1949 u16 err;
1950 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1951 u16 num_bands = 0, band_iter, idx;
1952 u32 freq_range[3], temp_freq_range[3];
1953
1954 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1955 HPI_TUNER_BAND_LAST);
1956
1957 freq_range[0] = INT_MAX;
1958 freq_range[1] = 0;
1959 freq_range[2] = INT_MAX;
1960
1961 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1962 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001963 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001964 idx, tuner_bands[band_iter],
1965 &temp_freq_range[idx]);
1966 if (err != 0)
1967 return err;
1968 }
1969
1970 /* skip band with bogus stepping */
1971 if (temp_freq_range[2] <= 0)
1972 continue;
1973
1974 if (temp_freq_range[0] < freq_range[0])
1975 freq_range[0] = temp_freq_range[0];
1976 if (temp_freq_range[1] > freq_range[1])
1977 freq_range[1] = temp_freq_range[1];
1978 if (temp_freq_range[2] < freq_range[2])
1979 freq_range[2] = temp_freq_range[2];
1980 }
1981
1982 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1983 uinfo->count = 1;
1984 uinfo->value.integer.min = ((int)freq_range[0]);
1985 uinfo->value.integer.max = ((int)freq_range[1]);
1986 uinfo->value.integer.step = ((int)freq_range[2]);
1987 return 0;
1988}
1989
1990static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1991 struct snd_ctl_elem_value *ucontrol)
1992{
1993 u32 h_control = kcontrol->private_value;
1994 u32 freq;
1995
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001996 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001997 ucontrol->value.integer.value[0] = freq;
1998
1999 return 0;
2000}
2001
2002static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2003 struct snd_ctl_elem_value *ucontrol)
2004{
2005 u32 h_control = kcontrol->private_value;
2006 u32 freq;
2007
2008 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002009 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002010
2011 return 1;
2012}
2013
2014/* Tuner control group initializer */
2015static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2016 struct hpi_control *hpi_ctl)
2017{
2018 struct snd_card *card = asihpi->card;
2019 struct snd_kcontrol_new snd_control;
2020
2021 snd_control.private_value = hpi_ctl->h_control;
2022 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2023
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002024 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002025 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002026 snd_control.info = snd_asihpi_tuner_gain_info;
2027 snd_control.get = snd_asihpi_tuner_gain_get;
2028 snd_control.put = snd_asihpi_tuner_gain_put;
2029
2030 if (ctl_add(card, &snd_control, asihpi) < 0)
2031 return -EINVAL;
2032 }
2033
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002034 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002035 snd_control.info = snd_asihpi_tuner_band_info;
2036 snd_control.get = snd_asihpi_tuner_band_get;
2037 snd_control.put = snd_asihpi_tuner_band_put;
2038
2039 if (ctl_add(card, &snd_control, asihpi) < 0)
2040 return -EINVAL;
2041
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002042 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002043 snd_control.info = snd_asihpi_tuner_freq_info;
2044 snd_control.get = snd_asihpi_tuner_freq_get;
2045 snd_control.put = snd_asihpi_tuner_freq_put;
2046
2047 return ctl_add(card, &snd_control, asihpi);
2048}
2049
2050/*------------------------------------------------------------
2051 Meter controls
2052 ------------------------------------------------------------*/
2053static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2054 struct snd_ctl_elem_info *uinfo)
2055{
2056 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2057 uinfo->count = HPI_MAX_CHANNELS;
2058 uinfo->value.integer.min = 0;
2059 uinfo->value.integer.max = 0x7FFFFFFF;
2060 return 0;
2061}
2062
2063/* linear values for 10dB steps */
2064static int log2lin[] = {
2065 0x7FFFFFFF, /* 0dB */
2066 679093956,
2067 214748365,
2068 67909396,
2069 21474837,
2070 6790940,
2071 2147484, /* -60dB */
2072 679094,
2073 214748, /* -80 */
2074 67909,
2075 21475, /* -100 */
2076 6791,
2077 2147,
2078 679,
2079 214,
2080 68,
2081 21,
2082 7,
2083 2
2084};
2085
2086static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2087 struct snd_ctl_elem_value *ucontrol)
2088{
2089 u32 h_control = kcontrol->private_value;
2090 short an_gain_mB[HPI_MAX_CHANNELS], i;
2091 u16 err;
2092
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002093 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002094
2095 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2096 if (err) {
2097 ucontrol->value.integer.value[i] = 0;
2098 } else if (an_gain_mB[i] >= 0) {
2099 ucontrol->value.integer.value[i] =
2100 an_gain_mB[i] << 16;
2101 } else {
2102 /* -ve is log value in millibels < -60dB,
2103 * convert to (roughly!) linear,
2104 */
2105 ucontrol->value.integer.value[i] =
2106 log2lin[an_gain_mB[i] / -1000];
2107 }
2108 }
2109 return 0;
2110}
2111
2112static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2113 struct hpi_control *hpi_ctl, int subidx)
2114{
2115 struct snd_card *card = asihpi->card;
2116 struct snd_kcontrol_new snd_control;
2117
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002118 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002119 snd_control.access =
2120 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2121 snd_control.info = snd_asihpi_meter_info;
2122 snd_control.get = snd_asihpi_meter_get;
2123
2124 snd_control.index = subidx;
2125
2126 return ctl_add(card, &snd_control, asihpi);
2127}
2128
2129/*------------------------------------------------------------
2130 Multiplexer controls
2131 ------------------------------------------------------------*/
2132static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2133{
2134 u32 h_control = snd_control->private_value;
2135 struct hpi_control hpi_ctl;
2136 int s, err;
2137 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002138 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002139 &hpi_ctl.
2140 src_node_type,
2141 &hpi_ctl.
2142 src_node_index);
2143 if (err)
2144 break;
2145 }
2146 return s;
2147}
2148
2149static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2150 struct snd_ctl_elem_info *uinfo)
2151{
2152 int err;
2153 u16 src_node_type, src_node_index;
2154 u32 h_control = kcontrol->private_value;
2155
2156 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2157 uinfo->count = 1;
2158 uinfo->value.enumerated.items =
2159 snd_card_asihpi_mux_count_sources(kcontrol);
2160
2161 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2162 uinfo->value.enumerated.item =
2163 uinfo->value.enumerated.items - 1;
2164
2165 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002166 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002167 uinfo->value.enumerated.item,
2168 &src_node_type, &src_node_index);
2169
2170 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002171 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002172 src_node_index);
2173 return 0;
2174}
2175
2176static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2177 struct snd_ctl_elem_value *ucontrol)
2178{
2179 u32 h_control = kcontrol->private_value;
2180 u16 source_type, source_index;
2181 u16 src_node_type, src_node_index;
2182 int s;
2183
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002184 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002185 &source_type, &source_index));
2186 /* Should cache this search result! */
2187 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002188 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002189 &src_node_type, &src_node_index))
2190 break;
2191
2192 if ((source_type == src_node_type)
2193 && (source_index == src_node_index)) {
2194 ucontrol->value.enumerated.item[0] = s;
2195 return 0;
2196 }
2197 }
2198 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002199 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002200 h_control, source_type, source_index);
2201 ucontrol->value.enumerated.item[0] = 0;
2202 return 0;
2203}
2204
2205static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2206 struct snd_ctl_elem_value *ucontrol)
2207{
2208 int change;
2209 u32 h_control = kcontrol->private_value;
2210 u16 source_type, source_index;
2211 u16 e;
2212
2213 change = 1;
2214
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002215 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002216 ucontrol->value.enumerated.item[0],
2217 &source_type, &source_index);
2218 if (!e)
2219 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002220 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002221 source_type, source_index));
2222 return change;
2223}
2224
2225
2226static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2227 struct hpi_control *hpi_ctl)
2228{
2229 struct snd_card *card = asihpi->card;
2230 struct snd_kcontrol_new snd_control;
2231
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002232 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002233 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2234 snd_control.info = snd_asihpi_mux_info;
2235 snd_control.get = snd_asihpi_mux_get;
2236 snd_control.put = snd_asihpi_mux_put;
2237
2238 return ctl_add(card, &snd_control, asihpi);
2239
2240}
2241
2242/*------------------------------------------------------------
2243 Channel mode controls
2244 ------------------------------------------------------------*/
2245static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2246 struct snd_ctl_elem_info *uinfo)
2247{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002248 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2249 "invalid",
2250 "Normal", "Swap",
2251 "From Left", "From Right",
2252 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002253 };
2254
2255 u32 h_control = kcontrol->private_value;
2256 u16 mode;
2257 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002258 u16 mode_map[6];
2259 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002260
2261 /* HPI channel mode values can be from 1 to 6
2262 Some adapters only support a contiguous subset
2263 */
2264 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002265 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002266 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002267 mode_map[valid_modes] = mode;
2268 valid_modes++;
2269 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002270
2271 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2272 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002273 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002274
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002275 if (uinfo->value.enumerated.item >= valid_modes)
2276 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002277
2278 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002279 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002280
2281 return 0;
2282}
2283
2284static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2285 struct snd_ctl_elem_value *ucontrol)
2286{
2287 u32 h_control = kcontrol->private_value;
2288 u16 mode;
2289
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002290 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002291 mode = 1;
2292
2293 ucontrol->value.enumerated.item[0] = mode - 1;
2294
2295 return 0;
2296}
2297
2298static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_value *ucontrol)
2300{
2301 int change;
2302 u32 h_control = kcontrol->private_value;
2303
2304 change = 1;
2305
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002306 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002307 ucontrol->value.enumerated.item[0] + 1));
2308 return change;
2309}
2310
2311
2312static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2313 struct hpi_control *hpi_ctl)
2314{
2315 struct snd_card *card = asihpi->card;
2316 struct snd_kcontrol_new snd_control;
2317
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002318 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002319 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2320 snd_control.info = snd_asihpi_cmode_info;
2321 snd_control.get = snd_asihpi_cmode_get;
2322 snd_control.put = snd_asihpi_cmode_put;
2323
2324 return ctl_add(card, &snd_control, asihpi);
2325}
2326
2327/*------------------------------------------------------------
2328 Sampleclock source controls
2329 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002330static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2331 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2332 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2333 "Prev Module",
2334 "Digital2", "Digital3", "Digital4", "Digital5",
2335 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002336
2337static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_info *uinfo)
2339{
2340 struct snd_card_asihpi *asihpi =
2341 (struct snd_card_asihpi *)(kcontrol->private_data);
2342 struct clk_cache *clkcache = &asihpi->cc;
2343 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2344 uinfo->count = 1;
2345 uinfo->value.enumerated.items = clkcache->count;
2346
2347 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2348 uinfo->value.enumerated.item =
2349 uinfo->value.enumerated.items - 1;
2350
2351 strcpy(uinfo->value.enumerated.name,
2352 clkcache->s[uinfo->value.enumerated.item].name);
2353 return 0;
2354}
2355
2356static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2357 struct snd_ctl_elem_value *ucontrol)
2358{
2359 struct snd_card_asihpi *asihpi =
2360 (struct snd_card_asihpi *)(kcontrol->private_data);
2361 struct clk_cache *clkcache = &asihpi->cc;
2362 u32 h_control = kcontrol->private_value;
2363 u16 source, srcindex = 0;
2364 int i;
2365
2366 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002367 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002368 source = 0;
2369
2370 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002371 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002372 srcindex = 0;
2373
2374 for (i = 0; i < clkcache->count; i++)
2375 if ((clkcache->s[i].source == source) &&
2376 (clkcache->s[i].index == srcindex))
2377 break;
2378
2379 ucontrol->value.enumerated.item[0] = i;
2380
2381 return 0;
2382}
2383
2384static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2385 struct snd_ctl_elem_value *ucontrol)
2386{
2387 struct snd_card_asihpi *asihpi =
2388 (struct snd_card_asihpi *)(kcontrol->private_data);
2389 struct clk_cache *clkcache = &asihpi->cc;
2390 int change, item;
2391 u32 h_control = kcontrol->private_value;
2392
2393 change = 1;
2394 item = ucontrol->value.enumerated.item[0];
2395 if (item >= clkcache->count)
2396 item = clkcache->count-1;
2397
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002398 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002399 h_control, clkcache->s[item].source));
2400
2401 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002402 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002403 h_control, clkcache->s[item].index));
2404 return change;
2405}
2406
2407/*------------------------------------------------------------
2408 Clkrate controls
2409 ------------------------------------------------------------*/
2410/* Need to change this to enumerated control with list of rates */
2411static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2412 struct snd_ctl_elem_info *uinfo)
2413{
2414 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2415 uinfo->count = 1;
2416 uinfo->value.integer.min = 8000;
2417 uinfo->value.integer.max = 192000;
2418 uinfo->value.integer.step = 100;
2419
2420 return 0;
2421}
2422
2423static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2424 struct snd_ctl_elem_value *ucontrol)
2425{
2426 u32 h_control = kcontrol->private_value;
2427 u32 rate;
2428 u16 e;
2429
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002430 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002431 if (!e)
2432 ucontrol->value.integer.value[0] = rate;
2433 else
2434 ucontrol->value.integer.value[0] = 0;
2435 return 0;
2436}
2437
2438static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2439 struct snd_ctl_elem_value *ucontrol)
2440{
2441 int change;
2442 u32 h_control = kcontrol->private_value;
2443
2444 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2445 asihpi->mixer_clkrate[addr][1] != right;
2446 */
2447 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002448 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002449 ucontrol->value.integer.value[0]));
2450 return change;
2451}
2452
2453static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2454 struct snd_ctl_elem_info *uinfo)
2455{
2456 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2457 uinfo->count = 1;
2458 uinfo->value.integer.min = 8000;
2459 uinfo->value.integer.max = 192000;
2460 uinfo->value.integer.step = 100;
2461
2462 return 0;
2463}
2464
2465static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2466 struct snd_ctl_elem_value *ucontrol)
2467{
2468 u32 h_control = kcontrol->private_value;
2469 u32 rate;
2470 u16 e;
2471
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002472 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002473 if (!e)
2474 ucontrol->value.integer.value[0] = rate;
2475 else
2476 ucontrol->value.integer.value[0] = 0;
2477 return 0;
2478}
2479
2480static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2481 struct hpi_control *hpi_ctl)
2482{
2483 struct snd_card *card = asihpi->card;
2484 struct snd_kcontrol_new snd_control;
2485
2486 struct clk_cache *clkcache = &asihpi->cc;
2487 u32 hSC = hpi_ctl->h_control;
2488 int has_aes_in = 0;
2489 int i, j;
2490 u16 source;
2491
2492 snd_control.private_value = hpi_ctl->h_control;
2493
2494 clkcache->has_local = 0;
2495
2496 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002497 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002498 i, &source))
2499 break;
2500 clkcache->s[i].source = source;
2501 clkcache->s[i].index = 0;
2502 clkcache->s[i].name = sampleclock_sources[source];
2503 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2504 has_aes_in = 1;
2505 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2506 clkcache->has_local = 1;
2507 }
2508 if (has_aes_in)
2509 /* already will have picked up index 0 above */
2510 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002511 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002512 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2513 &source))
2514 break;
2515 clkcache->s[i].source =
2516 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2517 clkcache->s[i].index = j;
2518 clkcache->s[i].name = sampleclock_sources[
2519 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2520 i++;
2521 }
2522 clkcache->count = i;
2523
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002524 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002525 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2526 snd_control.info = snd_asihpi_clksrc_info;
2527 snd_control.get = snd_asihpi_clksrc_get;
2528 snd_control.put = snd_asihpi_clksrc_put;
2529 if (ctl_add(card, &snd_control, asihpi) < 0)
2530 return -EINVAL;
2531
2532
2533 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002534 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002535 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2536 snd_control.info = snd_asihpi_clklocal_info;
2537 snd_control.get = snd_asihpi_clklocal_get;
2538 snd_control.put = snd_asihpi_clklocal_put;
2539
2540
2541 if (ctl_add(card, &snd_control, asihpi) < 0)
2542 return -EINVAL;
2543 }
2544
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002545 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002546 snd_control.access =
2547 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2548 snd_control.info = snd_asihpi_clkrate_info;
2549 snd_control.get = snd_asihpi_clkrate_get;
2550
2551 return ctl_add(card, &snd_control, asihpi);
2552}
2553/*------------------------------------------------------------
2554 Mixer
2555 ------------------------------------------------------------*/
2556
2557static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2558{
2559 struct snd_card *card = asihpi->card;
2560 unsigned int idx = 0;
2561 unsigned int subindex = 0;
2562 int err;
2563 struct hpi_control hpi_ctl, prev_ctl;
2564
2565 if (snd_BUG_ON(!asihpi))
2566 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002567 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002568
2569 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002570 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002571 &asihpi->h_mixer);
2572 hpi_handle_error(err);
2573 if (err)
2574 return -err;
2575
Takashi Iwai21896bc2010-06-02 12:08:37 +02002576 memset(&prev_ctl, 0, sizeof(prev_ctl));
2577 prev_ctl.control_type = -1;
2578
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002579 for (idx = 0; idx < 2000; idx++) {
2580 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002581 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582 idx,
2583 &hpi_ctl.src_node_type,
2584 &hpi_ctl.src_node_index,
2585 &hpi_ctl.dst_node_type,
2586 &hpi_ctl.dst_node_index,
2587 &hpi_ctl.control_type,
2588 &hpi_ctl.h_control);
2589 if (err) {
2590 if (err == HPI_ERROR_CONTROL_DISABLED) {
2591 if (mixer_dump)
2592 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002593 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002594 idx);
2595 continue;
2596 } else
2597 break;
2598
2599 }
2600
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002601 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2602 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002603
2604 /* ASI50xx in SSX mode has multiple meters on the same node.
2605 Use subindex to create distinct ALSA controls
2606 for any duplicated controls.
2607 */
2608 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2609 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2610 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2611 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2612 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2613 subindex++;
2614 else
2615 subindex = 0;
2616
2617 prev_ctl = hpi_ctl;
2618
2619 switch (hpi_ctl.control_type) {
2620 case HPI_CONTROL_VOLUME:
2621 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2622 break;
2623 case HPI_CONTROL_LEVEL:
2624 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2625 break;
2626 case HPI_CONTROL_MULTIPLEXER:
2627 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2628 break;
2629 case HPI_CONTROL_CHANNEL_MODE:
2630 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2631 break;
2632 case HPI_CONTROL_METER:
2633 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2634 break;
2635 case HPI_CONTROL_SAMPLECLOCK:
2636 err = snd_asihpi_sampleclock_add(
2637 asihpi, &hpi_ctl);
2638 break;
2639 case HPI_CONTROL_CONNECTION: /* ignore these */
2640 continue;
2641 case HPI_CONTROL_TUNER:
2642 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2643 break;
2644 case HPI_CONTROL_AESEBU_TRANSMITTER:
2645 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2646 break;
2647 case HPI_CONTROL_AESEBU_RECEIVER:
2648 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2649 break;
2650 case HPI_CONTROL_VOX:
2651 case HPI_CONTROL_BITSTREAM:
2652 case HPI_CONTROL_MICROPHONE:
2653 case HPI_CONTROL_PARAMETRIC_EQ:
2654 case HPI_CONTROL_COMPANDER:
2655 default:
2656 if (mixer_dump)
2657 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002658 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002659 "(%d) %d %d %d %d %d\n",
2660 idx,
2661 hpi_ctl.control_type,
2662 hpi_ctl.src_node_type,
2663 hpi_ctl.src_node_index,
2664 hpi_ctl.dst_node_type,
2665 hpi_ctl.dst_node_index);
2666 continue;
2667 };
2668 if (err < 0)
2669 return err;
2670 }
2671 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2672 hpi_handle_error(err);
2673
2674 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2675
2676 return 0;
2677}
2678
2679/*------------------------------------------------------------
2680 /proc interface
2681 ------------------------------------------------------------*/
2682
2683static void
2684snd_asihpi_proc_read(struct snd_info_entry *entry,
2685 struct snd_info_buffer *buffer)
2686{
2687 struct snd_card_asihpi *asihpi = entry->private_data;
2688 u16 version;
2689 u32 h_control;
2690 u32 rate = 0;
2691 u16 source = 0;
2692 int err;
2693
2694 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2695 snd_iprintf(buffer,
2696 "adapter ID=%4X\n_index=%d\n"
2697 "num_outstreams=%d\n_num_instreams=%d\n",
2698 asihpi->type, asihpi->adapter_index,
2699 asihpi->num_outstreams, asihpi->num_instreams);
2700
2701 version = asihpi->version;
2702 snd_iprintf(buffer,
2703 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2704 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2705 version & 0x7,
2706 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2707
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002708 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002709 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2710 HPI_CONTROL_SAMPLECLOCK, &h_control);
2711
2712 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002713 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002714 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002715 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002716
2717 if (!err)
2718 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2719 rate, sampleclock_sources[source]);
2720 }
2721
2722}
2723
2724
2725static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2726{
2727 struct snd_info_entry *entry;
2728
2729 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2730 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2731}
2732
2733/*------------------------------------------------------------
2734 HWDEP
2735 ------------------------------------------------------------*/
2736
2737static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2738{
2739 if (enable_hpi_hwdep)
2740 return 0;
2741 else
2742 return -ENODEV;
2743
2744}
2745
2746static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2747{
2748 if (enable_hpi_hwdep)
2749 return asihpi_hpi_release(file);
2750 else
2751 return -ENODEV;
2752}
2753
2754static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2755 unsigned int cmd, unsigned long arg)
2756{
2757 if (enable_hpi_hwdep)
2758 return asihpi_hpi_ioctl(file, cmd, arg);
2759 else
2760 return -ENODEV;
2761}
2762
2763
2764/* results in /dev/snd/hwC#D0 file for each card with index #
2765 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2766*/
2767static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2768 int device, struct snd_hwdep **rhwdep)
2769{
2770 struct snd_hwdep *hw;
2771 int err;
2772
2773 if (rhwdep)
2774 *rhwdep = NULL;
2775 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2776 if (err < 0)
2777 return err;
2778 strcpy(hw->name, "asihpi (HPI)");
2779 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2780 hw->ops.open = snd_asihpi_hpi_open;
2781 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2782 hw->ops.release = snd_asihpi_hpi_release;
2783 hw->private_data = asihpi;
2784 if (rhwdep)
2785 *rhwdep = hw;
2786 return 0;
2787}
2788
2789/*------------------------------------------------------------
2790 CARD
2791 ------------------------------------------------------------*/
2792static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2793 const struct pci_device_id *pci_id)
2794{
2795 int err;
2796
2797 u16 version;
2798 int pcm_substreams;
2799
2800 struct hpi_adapter *hpi_card;
2801 struct snd_card *card;
2802 struct snd_card_asihpi *asihpi;
2803
2804 u32 h_control;
2805 u32 h_stream;
2806
2807 static int dev;
2808 if (dev >= SNDRV_CARDS)
2809 return -ENODEV;
2810
2811 /* Should this be enable[hpi_card->index] ? */
2812 if (!enable[dev]) {
2813 dev++;
2814 return -ENOENT;
2815 }
2816
2817 err = asihpi_adapter_probe(pci_dev, pci_id);
2818 if (err < 0)
2819 return err;
2820
2821 hpi_card = pci_get_drvdata(pci_dev);
2822 /* first try to give the card the same index as its hardware index */
2823 err = snd_card_create(hpi_card->index,
2824 id[hpi_card->index], THIS_MODULE,
2825 sizeof(struct snd_card_asihpi),
2826 &card);
2827 if (err < 0) {
2828 /* if that fails, try the default index==next available */
2829 err =
2830 snd_card_create(index[dev], id[dev],
2831 THIS_MODULE,
2832 sizeof(struct snd_card_asihpi),
2833 &card);
2834 if (err < 0)
2835 return err;
2836 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002837 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002838 hpi_card->index, card->number);
2839 }
2840
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002841 snd_card_set_dev(card, &pci_dev->dev);
2842
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002843 asihpi = (struct snd_card_asihpi *) card->private_data;
2844 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002845 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002846 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002847 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002848 asihpi->adapter_index,
2849 &asihpi->num_outstreams,
2850 &asihpi->num_instreams,
2851 &asihpi->version,
2852 &asihpi->serial_number, &asihpi->type));
2853
2854 version = asihpi->version;
2855 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2856 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002857 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002858 asihpi->type, asihpi->adapter_index,
2859 asihpi->num_outstreams,
2860 asihpi->num_instreams, asihpi->serial_number,
2861 ((version >> 3) & 0xf) + 'A',
2862 version & 0x7,
2863 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2864
2865 pcm_substreams = asihpi->num_outstreams;
2866 if (pcm_substreams < asihpi->num_instreams)
2867 pcm_substreams = asihpi->num_instreams;
2868
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002869 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002870 HPI_ADAPTER_PROPERTY_CAPS1,
2871 NULL, &asihpi->support_grouping);
2872 if (err)
2873 asihpi->support_grouping = 0;
2874
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002875 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002876 HPI_ADAPTER_PROPERTY_CAPS2,
2877 &asihpi->support_mrx, NULL);
2878 if (err)
2879 asihpi->support_mrx = 0;
2880
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002881 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002882 HPI_ADAPTER_PROPERTY_INTERVAL,
2883 NULL, &asihpi->update_interval_frames);
2884 if (err)
2885 asihpi->update_interval_frames = 512;
2886
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002887 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002888 asihpi->update_interval_frames *= 2;
2889
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002890 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891 0, &h_stream));
2892
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002893 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002894 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002895
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002896 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002897
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002898 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002899 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2900 &asihpi->in_max_chans, &asihpi->out_max_chans);
2901 if (err) {
2902 asihpi->in_max_chans = 2;
2903 asihpi->out_max_chans = 2;
2904 }
2905
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002906 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2907 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002908 asihpi->support_grouping,
2909 asihpi->support_mrx
2910 );
2911
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002912 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2913 if (err < 0) {
2914 snd_printk(KERN_ERR "pcm_new failed\n");
2915 goto __nodev;
2916 }
2917 err = snd_card_asihpi_mixer_new(asihpi);
2918 if (err < 0) {
2919 snd_printk(KERN_ERR "mixer_new failed\n");
2920 goto __nodev;
2921 }
2922
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002923 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002924 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2925 HPI_CONTROL_SAMPLECLOCK, &h_control);
2926
2927 if (!err)
2928 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002929 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002930
2931 snd_asihpi_proc_init(asihpi);
2932
2933 /* always create, can be enabled or disabled dynamically
2934 by enable_hwdep module param*/
2935 snd_asihpi_hpi_new(asihpi, 0, NULL);
2936
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002937 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002938
2939 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2940 sprintf(card->longname, "%s %i",
2941 card->shortname, asihpi->adapter_index);
2942 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002943
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002944 if (!err) {
2945 hpi_card->snd_card_asihpi = card;
2946 dev++;
2947 return 0;
2948 }
2949__nodev:
2950 snd_card_free(card);
2951 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2952 return err;
2953
2954}
2955
2956static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2957{
2958 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2959
2960 snd_card_free(hpi_card->snd_card_asihpi);
2961 hpi_card->snd_card_asihpi = NULL;
2962 asihpi_adapter_remove(pci_dev);
2963}
2964
2965static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2966 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2967 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2968 (kernel_ulong_t)HPI_6205},
2969 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2970 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2971 (kernel_ulong_t)HPI_6000},
2972 {0,}
2973};
2974MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2975
2976static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002977 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002978 .id_table = asihpi_pci_tbl,
2979 .probe = snd_asihpi_probe,
2980 .remove = __devexit_p(snd_asihpi_remove),
2981#ifdef CONFIG_PM
2982/* .suspend = snd_asihpi_suspend,
2983 .resume = snd_asihpi_resume, */
2984#endif
2985};
2986
2987static int __init snd_asihpi_init(void)
2988{
2989 asihpi_init();
2990 return pci_register_driver(&driver);
2991}
2992
2993static void __exit snd_asihpi_exit(void)
2994{
2995
2996 pci_unregister_driver(&driver);
2997 asihpi_exit();
2998}
2999
3000module_init(snd_asihpi_init)
3001module_exit(snd_asihpi_exit)
3002