blob: eae62ebbd2952d3c0559a08dc7989247c52d56c9 [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 Blennerhassett719f82d2010-04-21 18:17:39 +020044MODULE_LICENSE("GPL");
45MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
46MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
47
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130048#if defined CONFIG_SND_DEBUG_VERBOSE
49/**
50 * snd_printddd - very verbose debug printk
51 * @format: format string
52 *
53 * Works like snd_printk() for debugging purposes.
54 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
55 * Must set snd module debug parameter to 3 to enable at runtime.
56 */
57#define snd_printddd(format, args...) \
58 __snd_printk(3, __FILE__, __LINE__, format, ##args)
59#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120060#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130061#endif
62
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020063static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
64static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
65static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
66static int enable_hpi_hwdep = 1;
67
68module_param_array(index, int, NULL, S_IRUGO);
69MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
70
71module_param_array(id, charp, NULL, S_IRUGO);
72MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
73
74module_param_array(enable, bool, NULL, S_IRUGO);
75MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
76
77module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
78MODULE_PARM_DESC(enable_hpi_hwdep,
79 "ALSA enable HPI hwdep for AudioScience soundcard ");
80
81/* identify driver */
82#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130083static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020084module_param(build_info, charp, S_IRUGO);
85MODULE_PARM_DESC(build_info, "built using headers from kernel source");
86#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130087static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020088module_param(build_info, charp, S_IRUGO);
89MODULE_PARM_DESC(build_info, "built within ALSA source");
90#endif
91
92/* set to 1 to dump every control from adapter to log */
93static const int mixer_dump;
94
95#define DEFAULT_SAMPLERATE 44100
96static int adapter_fs = DEFAULT_SAMPLERATE;
97
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020098/* defaults */
99#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300100#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200101#define BUFFER_BYTES_MAX (512 * 1024)
102
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200103#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
104
105struct clk_source {
106 int source;
107 int index;
108 char *name;
109};
110
111struct clk_cache {
112 int count;
113 int has_local;
114 struct clk_source s[MAX_CLOCKSOURCES];
115};
116
117/* Per card data */
118struct snd_card_asihpi {
119 struct snd_card *card;
120 struct pci_dev *pci;
121 u16 adapter_index;
122 u32 serial_number;
123 u16 type;
124 u16 version;
125 u16 num_outstreams;
126 u16 num_instreams;
127
128 u32 h_mixer;
129 struct clk_cache cc;
130
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200131 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200132 u16 support_grouping;
133 u16 support_mrx;
134 u16 update_interval_frames;
135 u16 in_max_chans;
136 u16 out_max_chans;
137};
138
139/* Per stream data */
140struct snd_card_asihpi_pcm {
141 struct timer_list timer;
142 unsigned int respawn_timer;
143 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300144 unsigned int buffer_bytes;
145 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200146 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300147 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
148 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
149 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200150 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200151 struct snd_pcm_substream *substream;
152 u32 h_stream;
153 struct hpi_format format;
154};
155
156/* universal stream verbs work with out or in stream handles */
157
158/* Functions to allow driver to give a buffer to HPI for busmastering */
159
160static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200161 u32 h_stream, /* handle to outstream. */
162 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
163 u32 pci_address
164)
165{
166 struct hpi_message hm;
167 struct hpi_response hr;
168 unsigned int obj = hpi_handle_object(h_stream);
169
170 if (!h_stream)
171 return HPI_ERROR_INVALID_OBJ;
172 hpi_init_message_response(&hm, &hr, obj,
173 obj == HPI_OBJ_OSTREAM ?
174 HPI_OSTREAM_HOSTBUFFER_ALLOC :
175 HPI_ISTREAM_HOSTBUFFER_ALLOC);
176
177 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
178 &hm.obj_index);
179
180 hm.u.d.u.buffer.buffer_size = size_in_bytes;
181 hm.u.d.u.buffer.pci_address = pci_address;
182 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
183 hpi_send_recv(&hm, &hr);
184 return hr.error;
185}
186
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300187static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200188{
189 struct hpi_message hm;
190 struct hpi_response hr;
191 unsigned int obj = hpi_handle_object(h_stream);
192
193 if (!h_stream)
194 return HPI_ERROR_INVALID_OBJ;
195
196 hpi_init_message_response(&hm, &hr, obj,
197 obj == HPI_OBJ_OSTREAM ?
198 HPI_OSTREAM_HOSTBUFFER_FREE :
199 HPI_ISTREAM_HOSTBUFFER_FREE);
200
201 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
202 &hm.obj_index);
203 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
204 hpi_send_recv(&hm, &hr);
205 return hr.error;
206}
207
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300208static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200209{
210 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300211 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200212 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300213 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200214}
215
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300216static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200217{
218 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300221 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200222}
223
224static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225 u32 h_stream,
226 u16 *pw_state,
227 u32 *pbuffer_size,
228 u32 *pdata_in_buffer,
229 u32 *psample_count,
230 u32 *pauxiliary_data
231)
232{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300233 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300235 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236 pbuffer_size, pdata_in_buffer,
237 psample_count, pauxiliary_data);
238 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300239 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200240 pbuffer_size, pdata_in_buffer,
241 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300242 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243}
244
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300245static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200246 u32 h_master,
247 u32 h_stream)
248{
249 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300250 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300252 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253}
254
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300255static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256{
257 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300260 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261}
262
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264 u32 h_stream, u32 *mo, u32 *mi)
265{
266 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270}
271
272static u16 handle_error(u16 err, int line, char *filename)
273{
274 if (err)
275 printk(KERN_WARNING
276 "in file %s, line %d: HPI error %d\n",
277 filename, line, err);
278 return err;
279}
280
281#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
282
283/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200284
285static void print_hwparams(struct snd_pcm_substream *substream,
286 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200287{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200288 char name[16];
289 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200290 snd_printd("%s HWPARAMS\n", name);
291 snd_printd(" samplerate %d Hz\n", params_rate(p));
292 snd_printd(" channels %d\n", params_channels(p));
293 snd_printd(" format %d\n", params_format(p));
294 snd_printd(" subformat %d\n", params_subformat(p));
295 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
296 snd_printd(" period %d B\n", params_period_bytes(p));
297 snd_printd(" access %d\n", params_access(p));
298 snd_printd(" period_size %d\n", params_period_size(p));
299 snd_printd(" periods %d\n", params_periods(p));
300 snd_printd(" buffer_size %d\n", params_buffer_size(p));
301 snd_printd(" %d B/s\n", params_rate(p) *
302 params_channels(p) *
303 snd_pcm_format_width(params_format(p)) / 8);
304
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200305}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200306
307static snd_pcm_format_t hpi_to_alsa_formats[] = {
308 -1, /* INVALID */
309 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
310 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
311 -1, /* HPI_FORMAT_MPEG_L1 3 */
312 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
313 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
314 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
315 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
316 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
317 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
318 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
319 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
320 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
321 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
322 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
323#if 1
324 /* ALSA can't handle 3 byte sample size together with power-of-2
325 * constraint on buffer_bytes, so disable this format
326 */
327 -1
328#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300329 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200330#endif
331};
332
333
334static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
335 u16 *hpi_format)
336{
337 u16 format;
338
339 for (format = HPI_FORMAT_PCM8_UNSIGNED;
340 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
341 if (hpi_to_alsa_formats[format] == alsa_format) {
342 *hpi_format = format;
343 return 0;
344 }
345 }
346
347 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
348 alsa_format);
349 *hpi_format = 0;
350 return -EINVAL;
351}
352
353static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
354 struct snd_pcm_hardware *pcmhw)
355{
356 u16 err;
357 u32 h_control;
358 u32 sample_rate;
359 int idx;
360 unsigned int rate_min = 200000;
361 unsigned int rate_max = 0;
362 unsigned int rates = 0;
363
364 if (asihpi->support_mrx) {
365 rates |= SNDRV_PCM_RATE_CONTINUOUS;
366 rates |= SNDRV_PCM_RATE_8000_96000;
367 rate_min = 8000;
368 rate_max = 100000;
369 } else {
370 /* on cards without SRC,
371 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300372 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200373 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
374 HPI_CONTROL_SAMPLECLOCK, &h_control);
375 if (err) {
376 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300377 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200378 }
379
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300380 for (idx = -1; idx < 100; idx++) {
381 if (idx == -1) {
382 if (hpi_sample_clock_get_sample_rate(h_control,
383 &sample_rate))
384 continue;
385 } else if (hpi_sample_clock_query_local_rate(h_control,
386 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200387 break;
388 }
389
390 rate_min = min(rate_min, sample_rate);
391 rate_max = max(rate_max, sample_rate);
392
393 switch (sample_rate) {
394 case 5512:
395 rates |= SNDRV_PCM_RATE_5512;
396 break;
397 case 8000:
398 rates |= SNDRV_PCM_RATE_8000;
399 break;
400 case 11025:
401 rates |= SNDRV_PCM_RATE_11025;
402 break;
403 case 16000:
404 rates |= SNDRV_PCM_RATE_16000;
405 break;
406 case 22050:
407 rates |= SNDRV_PCM_RATE_22050;
408 break;
409 case 32000:
410 rates |= SNDRV_PCM_RATE_32000;
411 break;
412 case 44100:
413 rates |= SNDRV_PCM_RATE_44100;
414 break;
415 case 48000:
416 rates |= SNDRV_PCM_RATE_48000;
417 break;
418 case 64000:
419 rates |= SNDRV_PCM_RATE_64000;
420 break;
421 case 88200:
422 rates |= SNDRV_PCM_RATE_88200;
423 break;
424 case 96000:
425 rates |= SNDRV_PCM_RATE_96000;
426 break;
427 case 176400:
428 rates |= SNDRV_PCM_RATE_176400;
429 break;
430 case 192000:
431 rates |= SNDRV_PCM_RATE_192000;
432 break;
433 default: /* some other rate */
434 rates |= SNDRV_PCM_RATE_KNOT;
435 }
436 }
437 }
438
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200439 pcmhw->rates = rates;
440 pcmhw->rate_min = rate_min;
441 pcmhw->rate_max = rate_max;
442}
443
444static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *params)
446{
447 struct snd_pcm_runtime *runtime = substream->runtime;
448 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
449 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
450 int err;
451 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400452 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200453 unsigned int bytes_per_sec;
454
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200455 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
457 if (err < 0)
458 return err;
459 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
460 if (err)
461 return err;
462
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200463 hpi_handle_error(hpi_format_create(&dpcm->format,
464 params_channels(params),
465 format, params_rate(params), 0, 0));
466
467 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300468 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200469 return -EINVAL;
470
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300471 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 dpcm->h_stream, &dpcm->format) != 0)
473 return -EINVAL;
474 }
475
476 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200477 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300478 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 params_buffer_bytes(params), runtime->dma_addr);
480 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300481 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200482 "stream_host_buffer_attach succeeded %u %lu\n",
483 params_buffer_bytes(params),
484 (unsigned long)runtime->dma_addr);
485 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300486 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200487 err);
488 return -ENOMEM;
489 }
490
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300491 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200492 &dpcm->hpi_buffer_attached,
493 NULL, NULL, NULL);
494
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300495 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200496 dpcm->hpi_buffer_attached);
497 }
498 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400499 width = snd_pcm_format_width(params_format(params));
500 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200501 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400502 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200503 return -EINVAL;
504
505 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300506 dpcm->buffer_bytes = params_buffer_bytes(params);
507 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200508
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200509 return 0;
510}
511
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300512static int
513snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
514{
515 struct snd_pcm_runtime *runtime = substream->runtime;
516 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
517 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300518 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300519
520 snd_pcm_lib_free_pages(substream);
521 return 0;
522}
523
524static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
525{
526 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
527 kfree(dpcm);
528}
529
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200530static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
531 substream)
532{
533 struct snd_pcm_runtime *runtime = substream->runtime;
534 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
535 int expiry;
536
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300537 expiry = HZ / 200;
538 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300539 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200540 dpcm->timer.expires = jiffies + expiry;
541 dpcm->respawn_timer = 1;
542 add_timer(&dpcm->timer);
543}
544
545static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
546{
547 struct snd_pcm_runtime *runtime = substream->runtime;
548 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
549
550 dpcm->respawn_timer = 0;
551 del_timer(&dpcm->timer);
552}
553
554static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
555 int cmd)
556{
557 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
558 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
559 struct snd_pcm_substream *s;
560 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200561 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200562
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200563 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200564 snd_printdd("%s trigger\n", name);
565
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200566 switch (cmd) {
567 case SNDRV_PCM_TRIGGER_START:
568 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300569 struct snd_pcm_runtime *runtime = s->runtime;
570 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200571
572 if (snd_pcm_substream_chip(s) != card)
573 continue;
574
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300575 /* don't link Cap and Play */
576 if (substream->stream != s->stream)
577 continue;
578
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200579 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200580 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200581 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300582 * in buffer? Must be at least one period!
583 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200584 * buffer is bigger it may contain even more
585 * data??
586 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300587 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300588 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200589 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300590 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300591 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592 preload,
593 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300594 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200595 }
596
597 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200598 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300599 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200600 dpcm->h_stream,
601 ds->h_stream);
602 if (!e) {
603 snd_pcm_trigger_done(s, substream);
604 } else {
605 hpi_handle_error(e);
606 break;
607 }
608 } else
609 break;
610 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300611 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200612 /* start the master stream */
613 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300614 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200615 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300616 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200617 break;
618
619 case SNDRV_PCM_TRIGGER_STOP:
620 snd_card_asihpi_pcm_timer_stop(substream);
621 snd_pcm_group_for_each_entry(s, substream) {
622 if (snd_pcm_substream_chip(s) != card)
623 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300624 /* don't link Cap and Play */
625 if (substream->stream != s->stream)
626 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200627
628 /*? workaround linked streams don't
629 transition to SETUP 20070706*/
630 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
631
632 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200633 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200634 snd_pcm_trigger_done(s, substream);
635 } else
636 break;
637 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300638 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200639
640 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300641 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200642 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
643 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300644 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200645
646 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300647 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200648 break;
649
650 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300651 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300652 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200653 snd_card_asihpi_pcm_timer_start(substream);
654 break;
655 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300656 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200657 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300658 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200659 break;
660 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300661 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200662 return -EINVAL;
663 }
664
665 return 0;
666}
667
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200668/*algorithm outline
669 Without linking degenerates to getting single stream pos etc
670 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
671*/
672/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300673pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300675 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300676 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300677 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678}
679timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
680for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300681 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300682 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200683 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300684 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300686 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200687 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300688 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200689 }
690 }
691 snd_pcm_period_elapsed(s);
692 }
693}
694*/
695
696/** Minimum of 2 modulo values. Works correctly when the difference between
697* the values is less than half the modulus
698*/
699static inline unsigned int modulo_min(unsigned int a, unsigned int b,
700 unsigned long int modulus)
701{
702 unsigned int result;
703 if (((a-b) % modulus) < (modulus/2))
704 result = b;
705 else
706 result = a;
707
708 return result;
709}
710
711/** Timer function, equivalent to interrupt service routine for cards
712*/
713static void snd_card_asihpi_timer_function(unsigned long data)
714{
715 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300716 struct snd_pcm_substream *substream = dpcm->substream;
717 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200718 struct snd_pcm_runtime *runtime;
719 struct snd_pcm_substream *s;
720 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300721 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200722 unsigned int remdata, xfercount, next_jiffies;
723 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300724 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200725 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300726 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200727 char name[16];
728
729 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200730
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200731 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300732
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200733 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300734 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200735 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
736 runtime = s->runtime;
737
738 if (snd_pcm_substream_chip(s) != card)
739 continue;
740
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300741 /* don't link Cap and Play */
742 if (substream->stream != s->stream)
743 continue;
744
745 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200746 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300747 &buffer_size, &bytes_avail,
748 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749
750 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300751 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200752
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200753 if (!card->can_dma)
754 on_card_bytes = bytes_avail;
755
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300756 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300757 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300758 if (state == HPI_STATE_STOPPED) {
759 if ((bytes_avail == 0) &&
760 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
761 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300762 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200763 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300764 }
765 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300766 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300767 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200768 ds->drained_count++;
769 if (ds->drained_count > 2) {
770 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
771 continue;
772 }
773 } else {
774 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300775 }
776 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300777 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200778
779 if (first) {
780 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300781 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300782 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200783 first = 0;
784 } else {
785 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300786 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200787 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300788 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200789 newdata);
790 }
791
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200792 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200793 (unsigned long)frames_to_bytes(runtime,
794 runtime->status->hw_ptr),
795 (unsigned long)frames_to_bytes(runtime,
796 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300797
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200798 snd_printdd("%d S=%d, "
799 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
800 "aux=0x%04X space=0x%04X\n",
801 s->number, state,
802 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
803 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300804 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300805 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200806 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300807 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200808
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300809 remdata = newdata % dpcm->period_bytes;
810 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300811 /* come back when on_card_bytes has decreased enough to allow
812 write to happen, or when data has been consumed to make another
813 period
814 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300815 if (xfercount && (on_card_bytes > dpcm->period_bytes))
816 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300817 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300818 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300819
820 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200821 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200822 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300823 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
824
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200827
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300828 /* don't link Cap and Play */
829 if (substream->stream != s->stream)
830 continue;
831
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300832 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
833
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200834 if (xfercount &&
835 /* Limit use of on card fifo for playback */
836 ((on_card_bytes <= ds->period_bytes) ||
837 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
838
839 {
840
841 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
842 unsigned int xfer1, xfer2;
843 char *pd = &s->runtime->dma_area[buf_ofs];
844
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200845 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200846 xfer1 = xfercount;
847 xfer2 = 0;
848 } else {
849 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
850 xfer2 = xfercount - xfer1;
851 }
852
853 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
854 snd_printddd("P%d write1 0x%04X 0x%04X\n",
855 s->number, xfer1, buf_ofs);
856 hpi_handle_error(
857 hpi_outstream_write_buf(
858 ds->h_stream, pd, xfer1,
859 &ds->format));
860
861 if (xfer2) {
862 pd = s->runtime->dma_area;
863
864 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200865 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200866 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867 hpi_handle_error(
868 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200869 ds->h_stream, pd,
870 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200871 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200872 }
873 } else {
874 snd_printddd("C%d read1 0x%04x\n",
875 s->number, xfer1);
876 hpi_handle_error(
877 hpi_instream_read_buf(
878 ds->h_stream,
879 pd, xfer1));
880 if (xfer2) {
881 pd = s->runtime->dma_area;
882 snd_printddd("C%d read2 0x%04x\n",
883 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200884 hpi_handle_error(
885 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300886 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200887 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200888 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200889 }
890 ds->pcm_buf_host_rw_ofs = ds->pcm_buf_host_rw_ofs + xfercount;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300891 ds->pcm_buf_elapsed_dma_ofs = pcm_buf_dma_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200892 snd_pcm_period_elapsed(s);
893 }
894 }
895
896 if (dpcm->respawn_timer)
897 add_timer(&dpcm->timer);
898}
899
900/***************************** PLAYBACK OPS ****************/
901static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
902 unsigned int cmd, void *arg)
903{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200904 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200905 return snd_pcm_lib_ioctl(substream, cmd, arg);
906}
907
908static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
909 substream)
910{
911 struct snd_pcm_runtime *runtime = substream->runtime;
912 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
913
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200914 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200915
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300916 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300917 dpcm->pcm_buf_host_rw_ofs = 0;
918 dpcm->pcm_buf_dma_ofs = 0;
919 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200920 return 0;
921}
922
923static snd_pcm_uframes_t
924snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
925{
926 struct snd_pcm_runtime *runtime = substream->runtime;
927 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
928 snd_pcm_uframes_t ptr;
929
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300930 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200931 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200932 return ptr;
933}
934
935static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
936 u32 h_stream,
937 struct snd_pcm_hardware *pcmhw)
938{
939 struct hpi_format hpi_format;
940 u16 format;
941 u16 err;
942 u32 h_control;
943 u32 sample_rate = 48000;
944
945 /* on cards without SRC, must query at valid rate,
946 * maybe set by external sync
947 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300948 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200949 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
950 HPI_CONTROL_SAMPLECLOCK, &h_control);
951
952 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300953 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200954 &sample_rate);
955
956 for (format = HPI_FORMAT_PCM8_UNSIGNED;
957 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
958 err = hpi_format_create(&hpi_format,
959 2, format, sample_rate, 128000, 0);
960 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300961 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200962 &hpi_format);
963 if (!err && (hpi_to_alsa_formats[format] != -1))
964 pcmhw->formats |=
965 (1ULL << hpi_to_alsa_formats[format]);
966 }
967}
968
969static struct snd_pcm_hardware snd_card_asihpi_playback = {
970 .channels_min = 1,
971 .channels_max = 2,
972 .buffer_bytes_max = BUFFER_BYTES_MAX,
973 .period_bytes_min = PERIOD_BYTES_MIN,
974 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
975 .periods_min = PERIODS_MIN,
976 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
977 .fifo_size = 0,
978};
979
980static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
981{
982 struct snd_pcm_runtime *runtime = substream->runtime;
983 struct snd_card_asihpi_pcm *dpcm;
984 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
985 int err;
986
987 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
988 if (dpcm == NULL)
989 return -ENOMEM;
990
991 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300992 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200993 substream->number, &dpcm->h_stream);
994 hpi_handle_error(err);
995 if (err)
996 kfree(dpcm);
997 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
998 return -EBUSY;
999 if (err)
1000 return -EIO;
1001
1002 /*? also check ASI5000 samplerate source
1003 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001004 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001005 */
1006
1007 init_timer(&dpcm->timer);
1008 dpcm->timer.data = (unsigned long) dpcm;
1009 dpcm->timer.function = snd_card_asihpi_timer_function;
1010 dpcm->substream = substream;
1011 runtime->private_data = dpcm;
1012 runtime->private_free = snd_card_asihpi_runtime_free;
1013
1014 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1015 /*?snd_card_asihpi_playback.period_bytes_min =
1016 card->out_max_chans * 4096; */
1017
1018 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1019 &snd_card_asihpi_playback);
1020
1021 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1022
1023 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1024 SNDRV_PCM_INFO_DOUBLE |
1025 SNDRV_PCM_INFO_BATCH |
1026 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001027 SNDRV_PCM_INFO_PAUSE |
1028 SNDRV_PCM_INFO_MMAP |
1029 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001030
1031 if (card->support_grouping)
1032 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1033
1034 /* struct is copied, so can create initializer dynamically */
1035 runtime->hw = snd_card_asihpi_playback;
1036
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001037 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001038 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1039 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1040 if (err < 0)
1041 return err;
1042
1043 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1044 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001045
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001046 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001047 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001048
1049 snd_pcm_set_sync(substream);
1050
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001051 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001052
1053 return 0;
1054}
1055
1056static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1057{
1058 struct snd_pcm_runtime *runtime = substream->runtime;
1059 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1060
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001061 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001062 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001063
1064 return 0;
1065}
1066
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001067static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1068 .open = snd_card_asihpi_playback_open,
1069 .close = snd_card_asihpi_playback_close,
1070 .ioctl = snd_card_asihpi_playback_ioctl,
1071 .hw_params = snd_card_asihpi_pcm_hw_params,
1072 .hw_free = snd_card_asihpi_hw_free,
1073 .prepare = snd_card_asihpi_playback_prepare,
1074 .trigger = snd_card_asihpi_trigger,
1075 .pointer = snd_card_asihpi_playback_pointer,
1076};
1077
1078/***************************** CAPTURE OPS ****************/
1079static snd_pcm_uframes_t
1080snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1081{
1082 struct snd_pcm_runtime *runtime = substream->runtime;
1083 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1084
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001085 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001086 substream->number, dpcm->pcm_buf_dma_ofs);
1087 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001088 for the capture position, because those samples aren't yet in
1089 the local buffer available for reading.
1090 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001091 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001092}
1093
1094static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1095 unsigned int cmd, void *arg)
1096{
1097 return snd_pcm_lib_ioctl(substream, cmd, arg);
1098}
1099
1100static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1101{
1102 struct snd_pcm_runtime *runtime = substream->runtime;
1103 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1104
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001105 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001106 dpcm->pcm_buf_host_rw_ofs = 0;
1107 dpcm->pcm_buf_dma_ofs = 0;
1108 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001109
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001110 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001111 return 0;
1112}
1113
1114
1115
1116static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1117 u32 h_stream,
1118 struct snd_pcm_hardware *pcmhw)
1119{
1120 struct hpi_format hpi_format;
1121 u16 format;
1122 u16 err;
1123 u32 h_control;
1124 u32 sample_rate = 48000;
1125
1126 /* on cards without SRC, must query at valid rate,
1127 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001128 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001129 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1130 HPI_CONTROL_SAMPLECLOCK, &h_control);
1131
1132 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001133 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001134 &sample_rate);
1135
1136 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1137 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1138
1139 err = hpi_format_create(&hpi_format, 2, format,
1140 sample_rate, 128000, 0);
1141 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001142 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001143 &hpi_format);
1144 if (!err)
1145 pcmhw->formats |=
1146 (1ULL << hpi_to_alsa_formats[format]);
1147 }
1148}
1149
1150
1151static struct snd_pcm_hardware snd_card_asihpi_capture = {
1152 .channels_min = 1,
1153 .channels_max = 2,
1154 .buffer_bytes_max = BUFFER_BYTES_MAX,
1155 .period_bytes_min = PERIOD_BYTES_MIN,
1156 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1157 .periods_min = PERIODS_MIN,
1158 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1159 .fifo_size = 0,
1160};
1161
1162static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1163{
1164 struct snd_pcm_runtime *runtime = substream->runtime;
1165 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1166 struct snd_card_asihpi_pcm *dpcm;
1167 int err;
1168
1169 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1170 if (dpcm == NULL)
1171 return -ENOMEM;
1172
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001173 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001174 card->adapter_index, substream->number);
1175
1176 err = hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001177 hpi_instream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001178 substream->number, &dpcm->h_stream));
1179 if (err)
1180 kfree(dpcm);
1181 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1182 return -EBUSY;
1183 if (err)
1184 return -EIO;
1185
1186
1187 init_timer(&dpcm->timer);
1188 dpcm->timer.data = (unsigned long) dpcm;
1189 dpcm->timer.function = snd_card_asihpi_timer_function;
1190 dpcm->substream = substream;
1191 runtime->private_data = dpcm;
1192 runtime->private_free = snd_card_asihpi_runtime_free;
1193
1194 snd_card_asihpi_capture.channels_max = card->in_max_chans;
1195 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1196 &snd_card_asihpi_capture);
1197 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001198 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1199 SNDRV_PCM_INFO_MMAP |
1200 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001201
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001202 if (card->support_grouping)
1203 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1204
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001205 runtime->hw = snd_card_asihpi_capture;
1206
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001207 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001208 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1209 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1210 if (err < 0)
1211 return err;
1212
1213 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1214 card->update_interval_frames);
1215 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1216 card->update_interval_frames * 2, UINT_MAX);
1217
1218 snd_pcm_set_sync(substream);
1219
1220 return 0;
1221}
1222
1223static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1224{
1225 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1226
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001227 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001228 return 0;
1229}
1230
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001231static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1232 .open = snd_card_asihpi_capture_open,
1233 .close = snd_card_asihpi_capture_close,
1234 .ioctl = snd_card_asihpi_capture_ioctl,
1235 .hw_params = snd_card_asihpi_pcm_hw_params,
1236 .hw_free = snd_card_asihpi_hw_free,
1237 .prepare = snd_card_asihpi_capture_prepare,
1238 .trigger = snd_card_asihpi_trigger,
1239 .pointer = snd_card_asihpi_capture_pointer,
1240};
1241
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001242static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1243 int device, int substreams)
1244{
1245 struct snd_pcm *pcm;
1246 int err;
1247
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001248 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001249 asihpi->num_outstreams, asihpi->num_instreams,
1250 &pcm);
1251 if (err < 0)
1252 return err;
1253 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001254 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1255 &snd_card_asihpi_playback_mmap_ops);
1256 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1257 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258
1259 pcm->private_data = asihpi;
1260 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001261 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001262
1263 /*? do we want to emulate MMAP for non-BBM cards?
1264 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1265 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1266 snd_dma_pci_data(asihpi->pci),
1267 64*1024, BUFFER_BYTES_MAX);
1268
1269 return 0;
1270}
1271
1272/***************************** MIXER CONTROLS ****************/
1273struct hpi_control {
1274 u32 h_control;
1275 u16 control_type;
1276 u16 src_node_type;
1277 u16 src_node_index;
1278 u16 dst_node_type;
1279 u16 dst_node_index;
1280 u16 band;
1281 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1282};
1283
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001284static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001285 "invalid",
1286 "AM",
1287 "FM mono",
1288 "TV NTSC-M",
1289 "FM stereo",
1290 "AUX",
1291 "TV PAL BG",
1292 "TV PAL I",
1293 "TV PAL DK",
1294 "TV SECAM",
1295};
1296
1297compile_time_assert(
1298 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1299 (HPI_TUNER_BAND_LAST+1)),
1300 assert_tuner_band_names_size);
1301
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001302static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001303 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001304 "PCM",
1305 "Line",
1306 "Digital",
1307 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001308 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001309 "Clock",
1310 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001311 "Mic",
1312 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001313 "Analog",
1314 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001315 "RTP",
1316 "GPI",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001317};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001318
1319compile_time_assert(
1320 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001321 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001322 assert_src_names_size);
1323
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001324static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001325 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001326 "PCM",
1327 "Line",
1328 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001329 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001330 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001331 "Net",
1332 "Analog",
1333 "RTP",
1334 "GPO",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001336
1337compile_time_assert(
1338 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001339 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001340 assert_dst_names_size);
1341
1342static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1343 struct snd_card_asihpi *asihpi)
1344{
1345 int err;
1346
1347 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1348 if (err < 0)
1349 return err;
1350 else if (mixer_dump)
1351 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1352
1353 return 0;
1354}
1355
1356/* Convert HPI control name and location into ALSA control name */
1357static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1358 struct hpi_control *hpi_ctl,
1359 char *name)
1360{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001361 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001362 memset(snd_control, 0, sizeof(*snd_control));
1363 snd_control->name = hpi_ctl->name;
1364 snd_control->private_value = hpi_ctl->h_control;
1365 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1366 snd_control->index = 0;
1367
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001368 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1369 dir = ""; /* clock is neither capture nor playback */
1370 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001371 dir = "Capture "; /* On or towards a PCM capture destination*/
1372 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1373 (!hpi_ctl->dst_node_type))
1374 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001375 else if (hpi_ctl->src_node_type &&
1376 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001377 (hpi_ctl->dst_node_type))
1378 dir = "Monitor Playback "; /* Between an input and an output */
1379 else
1380 dir = "Playback "; /* PCM Playback source, or output node */
1381
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001382 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001383 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001384 asihpi_src_names[hpi_ctl->src_node_type],
1385 hpi_ctl->src_node_index,
1386 asihpi_dst_names[hpi_ctl->dst_node_type],
1387 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001388 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001389 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001390 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001391 asihpi_dst_names[hpi_ctl->dst_node_type],
1392 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001393 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396 asihpi_src_names[hpi_ctl->src_node_type],
1397 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001398 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001399 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001400 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1401 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402}
1403
1404/*------------------------------------------------------------
1405 Volume controls
1406 ------------------------------------------------------------*/
1407#define VOL_STEP_mB 1
1408static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_info *uinfo)
1410{
1411 u32 h_control = kcontrol->private_value;
1412 u16 err;
1413 /* native gains are in millibels */
1414 short min_gain_mB;
1415 short max_gain_mB;
1416 short step_gain_mB;
1417
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001418 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001419 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1420 if (err) {
1421 max_gain_mB = 0;
1422 min_gain_mB = -10000;
1423 step_gain_mB = VOL_STEP_mB;
1424 }
1425
1426 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1427 uinfo->count = 2;
1428 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1429 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1430 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1431 return 0;
1432}
1433
1434static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_value *ucontrol)
1436{
1437 u32 h_control = kcontrol->private_value;
1438 short an_gain_mB[HPI_MAX_CHANNELS];
1439
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001440 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001441 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1442 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1443
1444 return 0;
1445}
1446
1447static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_value *ucontrol)
1449{
1450 int change;
1451 u32 h_control = kcontrol->private_value;
1452 short an_gain_mB[HPI_MAX_CHANNELS];
1453
1454 an_gain_mB[0] =
1455 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1456 an_gain_mB[1] =
1457 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1458 /* change = asihpi->mixer_volume[addr][0] != left ||
1459 asihpi->mixer_volume[addr][1] != right;
1460 */
1461 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001462 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001463 return change;
1464}
1465
1466static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1467
Takashi Iwai000477a2011-07-22 07:57:44 +02001468#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001469
1470static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_value *ucontrol)
1472{
1473 u32 h_control = kcontrol->private_value;
1474 u32 mute;
1475
1476 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1477 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1478
1479 return 0;
1480}
1481
1482static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1483 struct snd_ctl_elem_value *ucontrol)
1484{
1485 u32 h_control = kcontrol->private_value;
1486 int change = 1;
1487 /* HPI currently only supports all or none muting of multichannel volume
1488 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1489 */
1490 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1491 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1492 return change;
1493}
1494
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001495static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1496 struct hpi_control *hpi_ctl)
1497{
1498 struct snd_card *card = asihpi->card;
1499 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001500 int err;
1501 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001502
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001503 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001504 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1505 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1506 snd_control.info = snd_asihpi_volume_info;
1507 snd_control.get = snd_asihpi_volume_get;
1508 snd_control.put = snd_asihpi_volume_put;
1509 snd_control.tlv.p = db_scale_100;
1510
Eliot Blennerhassettfe0aa882011-07-22 15:53:03 +12001511 err = ctl_add(card, &snd_control, asihpi);
1512 if (err)
1513 return err;
1514
1515 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1516 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1517 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1518 snd_control.info = snd_asihpi_volume_mute_info;
1519 snd_control.get = snd_asihpi_volume_mute_get;
1520 snd_control.put = snd_asihpi_volume_mute_put;
1521 err = ctl_add(card, &snd_control, asihpi);
1522 }
1523 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001524}
1525
1526/*------------------------------------------------------------
1527 Level controls
1528 ------------------------------------------------------------*/
1529static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1530 struct snd_ctl_elem_info *uinfo)
1531{
1532 u32 h_control = kcontrol->private_value;
1533 u16 err;
1534 short min_gain_mB;
1535 short max_gain_mB;
1536 short step_gain_mB;
1537
1538 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001539 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001540 &max_gain_mB, &step_gain_mB);
1541 if (err) {
1542 max_gain_mB = 2400;
1543 min_gain_mB = -1000;
1544 step_gain_mB = 100;
1545 }
1546
1547 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1548 uinfo->count = 2;
1549 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1550 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1551 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1552 return 0;
1553}
1554
1555static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1556 struct snd_ctl_elem_value *ucontrol)
1557{
1558 u32 h_control = kcontrol->private_value;
1559 short an_gain_mB[HPI_MAX_CHANNELS];
1560
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001561 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001562 ucontrol->value.integer.value[0] =
1563 an_gain_mB[0] / HPI_UNITS_PER_dB;
1564 ucontrol->value.integer.value[1] =
1565 an_gain_mB[1] / HPI_UNITS_PER_dB;
1566
1567 return 0;
1568}
1569
1570static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1571 struct snd_ctl_elem_value *ucontrol)
1572{
1573 int change;
1574 u32 h_control = kcontrol->private_value;
1575 short an_gain_mB[HPI_MAX_CHANNELS];
1576
1577 an_gain_mB[0] =
1578 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1579 an_gain_mB[1] =
1580 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1581 /* change = asihpi->mixer_level[addr][0] != left ||
1582 asihpi->mixer_level[addr][1] != right;
1583 */
1584 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001585 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001586 return change;
1587}
1588
1589static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1590
1591static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1592 struct hpi_control *hpi_ctl)
1593{
1594 struct snd_card *card = asihpi->card;
1595 struct snd_kcontrol_new snd_control;
1596
1597 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001598 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001599 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1600 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1601 snd_control.info = snd_asihpi_level_info;
1602 snd_control.get = snd_asihpi_level_get;
1603 snd_control.put = snd_asihpi_level_put;
1604 snd_control.tlv.p = db_scale_level;
1605
1606 return ctl_add(card, &snd_control, asihpi);
1607}
1608
1609/*------------------------------------------------------------
1610 AESEBU controls
1611 ------------------------------------------------------------*/
1612
1613/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001614static const char * const asihpi_aesebu_format_names[] = {
1615 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001616
1617static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1618 struct snd_ctl_elem_info *uinfo)
1619{
1620 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1621 uinfo->count = 1;
1622 uinfo->value.enumerated.items = 3;
1623
1624 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1625 uinfo->value.enumerated.item =
1626 uinfo->value.enumerated.items - 1;
1627
1628 strcpy(uinfo->value.enumerated.name,
1629 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1630
1631 return 0;
1632}
1633
1634static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1635 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001636 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001637{
1638 u32 h_control = kcontrol->private_value;
1639 u16 source, err;
1640
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001641 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001642
1643 /* default to N/A */
1644 ucontrol->value.enumerated.item[0] = 0;
1645 /* return success but set the control to N/A */
1646 if (err)
1647 return 0;
1648 if (source == HPI_AESEBU_FORMAT_SPDIF)
1649 ucontrol->value.enumerated.item[0] = 1;
1650 if (source == HPI_AESEBU_FORMAT_AESEBU)
1651 ucontrol->value.enumerated.item[0] = 2;
1652
1653 return 0;
1654}
1655
1656static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001658 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001659{
1660 u32 h_control = kcontrol->private_value;
1661
1662 /* default to S/PDIF */
1663 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1664
1665 if (ucontrol->value.enumerated.item[0] == 1)
1666 source = HPI_AESEBU_FORMAT_SPDIF;
1667 if (ucontrol->value.enumerated.item[0] == 2)
1668 source = HPI_AESEBU_FORMAT_AESEBU;
1669
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001670 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001671 return -EINVAL;
1672
1673 return 1;
1674}
1675
1676static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol) {
1678 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001679 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001680}
1681
1682static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_value *ucontrol) {
1684 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001685 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001686}
1687
1688static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_info *uinfo)
1690{
1691 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1692 uinfo->count = 1;
1693
1694 uinfo->value.integer.min = 0;
1695 uinfo->value.integer.max = 0X1F;
1696 uinfo->value.integer.step = 1;
1697
1698 return 0;
1699}
1700
1701static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1702 struct snd_ctl_elem_value *ucontrol) {
1703
1704 u32 h_control = kcontrol->private_value;
1705 u16 status;
1706
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001707 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1708 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001709 ucontrol->value.integer.value[0] = status;
1710 return 0;
1711}
1712
1713static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1714 struct hpi_control *hpi_ctl)
1715{
1716 struct snd_card *card = asihpi->card;
1717 struct snd_kcontrol_new snd_control;
1718
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001719 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001720 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1721 snd_control.info = snd_asihpi_aesebu_format_info;
1722 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1723 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1724
1725
1726 if (ctl_add(card, &snd_control, asihpi) < 0)
1727 return -EINVAL;
1728
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001729 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001730 snd_control.access =
1731 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1732 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1733 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1734
1735 return ctl_add(card, &snd_control, asihpi);
1736}
1737
1738static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol) {
1740 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001741 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001742}
1743
1744static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1745 struct snd_ctl_elem_value *ucontrol) {
1746 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001747 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001748}
1749
1750
1751static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1752 struct hpi_control *hpi_ctl)
1753{
1754 struct snd_card *card = asihpi->card;
1755 struct snd_kcontrol_new snd_control;
1756
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001757 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001758 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1759 snd_control.info = snd_asihpi_aesebu_format_info;
1760 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1761 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1762
1763 return ctl_add(card, &snd_control, asihpi);
1764}
1765
1766/*------------------------------------------------------------
1767 Tuner controls
1768 ------------------------------------------------------------*/
1769
1770/* Gain */
1771
1772static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_info *uinfo)
1774{
1775 u32 h_control = kcontrol->private_value;
1776 u16 err;
1777 short idx;
1778 u16 gain_range[3];
1779
1780 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001781 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001782 idx, &gain_range[idx]);
1783 if (err != 0)
1784 return err;
1785 }
1786
1787 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1788 uinfo->count = 1;
1789 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1790 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1791 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1792 return 0;
1793}
1794
1795static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_value *ucontrol)
1797{
1798 /*
1799 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1800 */
1801 u32 h_control = kcontrol->private_value;
1802 short gain;
1803
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001804 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001805 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1806
1807 return 0;
1808}
1809
1810static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_value *ucontrol)
1812{
1813 /*
1814 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1815 */
1816 u32 h_control = kcontrol->private_value;
1817 short gain;
1818
1819 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001820 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001821
1822 return 1;
1823}
1824
1825/* Band */
1826
1827static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1828 u16 *band_list, u32 len) {
1829 u32 h_control = kcontrol->private_value;
1830 u16 err = 0;
1831 u32 i;
1832
1833 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001834 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001835 h_control, i, &band_list[i]);
1836 if (err != 0)
1837 break;
1838 }
1839
1840 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1841 return -EIO;
1842
1843 return i;
1844}
1845
1846static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1847 struct snd_ctl_elem_info *uinfo)
1848{
1849 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1850 int num_bands = 0;
1851
1852 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1853 HPI_TUNER_BAND_LAST);
1854
1855 if (num_bands < 0)
1856 return num_bands;
1857
1858 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1859 uinfo->count = 1;
1860 uinfo->value.enumerated.items = num_bands;
1861
1862 if (num_bands > 0) {
1863 if (uinfo->value.enumerated.item >=
1864 uinfo->value.enumerated.items)
1865 uinfo->value.enumerated.item =
1866 uinfo->value.enumerated.items - 1;
1867
1868 strcpy(uinfo->value.enumerated.name,
1869 asihpi_tuner_band_names[
1870 tuner_bands[uinfo->value.enumerated.item]]);
1871
1872 }
1873 return 0;
1874}
1875
1876static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1877 struct snd_ctl_elem_value *ucontrol)
1878{
1879 u32 h_control = kcontrol->private_value;
1880 /*
1881 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1882 */
1883 u16 band, idx;
1884 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1885 u32 num_bands = 0;
1886
1887 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1888 HPI_TUNER_BAND_LAST);
1889
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001890 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001891
1892 ucontrol->value.enumerated.item[0] = -1;
1893 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1894 if (tuner_bands[idx] == band) {
1895 ucontrol->value.enumerated.item[0] = idx;
1896 break;
1897 }
1898
1899 return 0;
1900}
1901
1902static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1903 struct snd_ctl_elem_value *ucontrol)
1904{
1905 /*
1906 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1907 */
1908 u32 h_control = kcontrol->private_value;
1909 u16 band;
1910 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1911 u32 num_bands = 0;
1912
1913 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1914 HPI_TUNER_BAND_LAST);
1915
1916 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001917 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001918
1919 return 1;
1920}
1921
1922/* Freq */
1923
1924static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1925 struct snd_ctl_elem_info *uinfo)
1926{
1927 u32 h_control = kcontrol->private_value;
1928 u16 err;
1929 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1930 u16 num_bands = 0, band_iter, idx;
1931 u32 freq_range[3], temp_freq_range[3];
1932
1933 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1934 HPI_TUNER_BAND_LAST);
1935
1936 freq_range[0] = INT_MAX;
1937 freq_range[1] = 0;
1938 freq_range[2] = INT_MAX;
1939
1940 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1941 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001942 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001943 idx, tuner_bands[band_iter],
1944 &temp_freq_range[idx]);
1945 if (err != 0)
1946 return err;
1947 }
1948
1949 /* skip band with bogus stepping */
1950 if (temp_freq_range[2] <= 0)
1951 continue;
1952
1953 if (temp_freq_range[0] < freq_range[0])
1954 freq_range[0] = temp_freq_range[0];
1955 if (temp_freq_range[1] > freq_range[1])
1956 freq_range[1] = temp_freq_range[1];
1957 if (temp_freq_range[2] < freq_range[2])
1958 freq_range[2] = temp_freq_range[2];
1959 }
1960
1961 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1962 uinfo->count = 1;
1963 uinfo->value.integer.min = ((int)freq_range[0]);
1964 uinfo->value.integer.max = ((int)freq_range[1]);
1965 uinfo->value.integer.step = ((int)freq_range[2]);
1966 return 0;
1967}
1968
1969static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1970 struct snd_ctl_elem_value *ucontrol)
1971{
1972 u32 h_control = kcontrol->private_value;
1973 u32 freq;
1974
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001975 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001976 ucontrol->value.integer.value[0] = freq;
1977
1978 return 0;
1979}
1980
1981static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1982 struct snd_ctl_elem_value *ucontrol)
1983{
1984 u32 h_control = kcontrol->private_value;
1985 u32 freq;
1986
1987 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001988 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001989
1990 return 1;
1991}
1992
1993/* Tuner control group initializer */
1994static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1995 struct hpi_control *hpi_ctl)
1996{
1997 struct snd_card *card = asihpi->card;
1998 struct snd_kcontrol_new snd_control;
1999
2000 snd_control.private_value = hpi_ctl->h_control;
2001 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2002
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002003 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002004 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002005 snd_control.info = snd_asihpi_tuner_gain_info;
2006 snd_control.get = snd_asihpi_tuner_gain_get;
2007 snd_control.put = snd_asihpi_tuner_gain_put;
2008
2009 if (ctl_add(card, &snd_control, asihpi) < 0)
2010 return -EINVAL;
2011 }
2012
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002013 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002014 snd_control.info = snd_asihpi_tuner_band_info;
2015 snd_control.get = snd_asihpi_tuner_band_get;
2016 snd_control.put = snd_asihpi_tuner_band_put;
2017
2018 if (ctl_add(card, &snd_control, asihpi) < 0)
2019 return -EINVAL;
2020
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002021 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002022 snd_control.info = snd_asihpi_tuner_freq_info;
2023 snd_control.get = snd_asihpi_tuner_freq_get;
2024 snd_control.put = snd_asihpi_tuner_freq_put;
2025
2026 return ctl_add(card, &snd_control, asihpi);
2027}
2028
2029/*------------------------------------------------------------
2030 Meter controls
2031 ------------------------------------------------------------*/
2032static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2033 struct snd_ctl_elem_info *uinfo)
2034{
2035 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2036 uinfo->count = HPI_MAX_CHANNELS;
2037 uinfo->value.integer.min = 0;
2038 uinfo->value.integer.max = 0x7FFFFFFF;
2039 return 0;
2040}
2041
2042/* linear values for 10dB steps */
2043static int log2lin[] = {
2044 0x7FFFFFFF, /* 0dB */
2045 679093956,
2046 214748365,
2047 67909396,
2048 21474837,
2049 6790940,
2050 2147484, /* -60dB */
2051 679094,
2052 214748, /* -80 */
2053 67909,
2054 21475, /* -100 */
2055 6791,
2056 2147,
2057 679,
2058 214,
2059 68,
2060 21,
2061 7,
2062 2
2063};
2064
2065static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2066 struct snd_ctl_elem_value *ucontrol)
2067{
2068 u32 h_control = kcontrol->private_value;
2069 short an_gain_mB[HPI_MAX_CHANNELS], i;
2070 u16 err;
2071
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002072 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002073
2074 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2075 if (err) {
2076 ucontrol->value.integer.value[i] = 0;
2077 } else if (an_gain_mB[i] >= 0) {
2078 ucontrol->value.integer.value[i] =
2079 an_gain_mB[i] << 16;
2080 } else {
2081 /* -ve is log value in millibels < -60dB,
2082 * convert to (roughly!) linear,
2083 */
2084 ucontrol->value.integer.value[i] =
2085 log2lin[an_gain_mB[i] / -1000];
2086 }
2087 }
2088 return 0;
2089}
2090
2091static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2092 struct hpi_control *hpi_ctl, int subidx)
2093{
2094 struct snd_card *card = asihpi->card;
2095 struct snd_kcontrol_new snd_control;
2096
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002097 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002098 snd_control.access =
2099 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2100 snd_control.info = snd_asihpi_meter_info;
2101 snd_control.get = snd_asihpi_meter_get;
2102
2103 snd_control.index = subidx;
2104
2105 return ctl_add(card, &snd_control, asihpi);
2106}
2107
2108/*------------------------------------------------------------
2109 Multiplexer controls
2110 ------------------------------------------------------------*/
2111static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2112{
2113 u32 h_control = snd_control->private_value;
2114 struct hpi_control hpi_ctl;
2115 int s, err;
2116 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002117 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002118 &hpi_ctl.
2119 src_node_type,
2120 &hpi_ctl.
2121 src_node_index);
2122 if (err)
2123 break;
2124 }
2125 return s;
2126}
2127
2128static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2129 struct snd_ctl_elem_info *uinfo)
2130{
2131 int err;
2132 u16 src_node_type, src_node_index;
2133 u32 h_control = kcontrol->private_value;
2134
2135 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2136 uinfo->count = 1;
2137 uinfo->value.enumerated.items =
2138 snd_card_asihpi_mux_count_sources(kcontrol);
2139
2140 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2141 uinfo->value.enumerated.item =
2142 uinfo->value.enumerated.items - 1;
2143
2144 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002145 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002146 uinfo->value.enumerated.item,
2147 &src_node_type, &src_node_index);
2148
2149 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002150 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002151 src_node_index);
2152 return 0;
2153}
2154
2155static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2156 struct snd_ctl_elem_value *ucontrol)
2157{
2158 u32 h_control = kcontrol->private_value;
2159 u16 source_type, source_index;
2160 u16 src_node_type, src_node_index;
2161 int s;
2162
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002163 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002164 &source_type, &source_index));
2165 /* Should cache this search result! */
2166 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002167 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002168 &src_node_type, &src_node_index))
2169 break;
2170
2171 if ((source_type == src_node_type)
2172 && (source_index == src_node_index)) {
2173 ucontrol->value.enumerated.item[0] = s;
2174 return 0;
2175 }
2176 }
2177 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002178 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002179 h_control, source_type, source_index);
2180 ucontrol->value.enumerated.item[0] = 0;
2181 return 0;
2182}
2183
2184static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2185 struct snd_ctl_elem_value *ucontrol)
2186{
2187 int change;
2188 u32 h_control = kcontrol->private_value;
2189 u16 source_type, source_index;
2190 u16 e;
2191
2192 change = 1;
2193
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002194 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002195 ucontrol->value.enumerated.item[0],
2196 &source_type, &source_index);
2197 if (!e)
2198 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002199 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002200 source_type, source_index));
2201 return change;
2202}
2203
2204
2205static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2206 struct hpi_control *hpi_ctl)
2207{
2208 struct snd_card *card = asihpi->card;
2209 struct snd_kcontrol_new snd_control;
2210
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002211 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002212 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2213 snd_control.info = snd_asihpi_mux_info;
2214 snd_control.get = snd_asihpi_mux_get;
2215 snd_control.put = snd_asihpi_mux_put;
2216
2217 return ctl_add(card, &snd_control, asihpi);
2218
2219}
2220
2221/*------------------------------------------------------------
2222 Channel mode controls
2223 ------------------------------------------------------------*/
2224static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2225 struct snd_ctl_elem_info *uinfo)
2226{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002227 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2228 "invalid",
2229 "Normal", "Swap",
2230 "From Left", "From Right",
2231 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002232 };
2233
2234 u32 h_control = kcontrol->private_value;
2235 u16 mode;
2236 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002237 u16 mode_map[6];
2238 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002239
2240 /* HPI channel mode values can be from 1 to 6
2241 Some adapters only support a contiguous subset
2242 */
2243 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002244 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002245 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002246 mode_map[valid_modes] = mode;
2247 valid_modes++;
2248 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002249
2250 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2251 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002252 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002253
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002254 if (uinfo->value.enumerated.item >= valid_modes)
2255 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002256
2257 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002258 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002259
2260 return 0;
2261}
2262
2263static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2264 struct snd_ctl_elem_value *ucontrol)
2265{
2266 u32 h_control = kcontrol->private_value;
2267 u16 mode;
2268
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002269 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002270 mode = 1;
2271
2272 ucontrol->value.enumerated.item[0] = mode - 1;
2273
2274 return 0;
2275}
2276
2277static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2278 struct snd_ctl_elem_value *ucontrol)
2279{
2280 int change;
2281 u32 h_control = kcontrol->private_value;
2282
2283 change = 1;
2284
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002285 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002286 ucontrol->value.enumerated.item[0] + 1));
2287 return change;
2288}
2289
2290
2291static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2292 struct hpi_control *hpi_ctl)
2293{
2294 struct snd_card *card = asihpi->card;
2295 struct snd_kcontrol_new snd_control;
2296
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002297 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002298 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2299 snd_control.info = snd_asihpi_cmode_info;
2300 snd_control.get = snd_asihpi_cmode_get;
2301 snd_control.put = snd_asihpi_cmode_put;
2302
2303 return ctl_add(card, &snd_control, asihpi);
2304}
2305
2306/*------------------------------------------------------------
2307 Sampleclock source controls
2308 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002309static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2310 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2311 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2312 "Prev Module",
2313 "Digital2", "Digital3", "Digital4", "Digital5",
2314 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002315
2316static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2317 struct snd_ctl_elem_info *uinfo)
2318{
2319 struct snd_card_asihpi *asihpi =
2320 (struct snd_card_asihpi *)(kcontrol->private_data);
2321 struct clk_cache *clkcache = &asihpi->cc;
2322 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2323 uinfo->count = 1;
2324 uinfo->value.enumerated.items = clkcache->count;
2325
2326 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2327 uinfo->value.enumerated.item =
2328 uinfo->value.enumerated.items - 1;
2329
2330 strcpy(uinfo->value.enumerated.name,
2331 clkcache->s[uinfo->value.enumerated.item].name);
2332 return 0;
2333}
2334
2335static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2336 struct snd_ctl_elem_value *ucontrol)
2337{
2338 struct snd_card_asihpi *asihpi =
2339 (struct snd_card_asihpi *)(kcontrol->private_data);
2340 struct clk_cache *clkcache = &asihpi->cc;
2341 u32 h_control = kcontrol->private_value;
2342 u16 source, srcindex = 0;
2343 int i;
2344
2345 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002346 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002347 source = 0;
2348
2349 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002350 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002351 srcindex = 0;
2352
2353 for (i = 0; i < clkcache->count; i++)
2354 if ((clkcache->s[i].source == source) &&
2355 (clkcache->s[i].index == srcindex))
2356 break;
2357
2358 ucontrol->value.enumerated.item[0] = i;
2359
2360 return 0;
2361}
2362
2363static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2364 struct snd_ctl_elem_value *ucontrol)
2365{
2366 struct snd_card_asihpi *asihpi =
2367 (struct snd_card_asihpi *)(kcontrol->private_data);
2368 struct clk_cache *clkcache = &asihpi->cc;
2369 int change, item;
2370 u32 h_control = kcontrol->private_value;
2371
2372 change = 1;
2373 item = ucontrol->value.enumerated.item[0];
2374 if (item >= clkcache->count)
2375 item = clkcache->count-1;
2376
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002377 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002378 h_control, clkcache->s[item].source));
2379
2380 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002381 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002382 h_control, clkcache->s[item].index));
2383 return change;
2384}
2385
2386/*------------------------------------------------------------
2387 Clkrate controls
2388 ------------------------------------------------------------*/
2389/* Need to change this to enumerated control with list of rates */
2390static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2391 struct snd_ctl_elem_info *uinfo)
2392{
2393 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2394 uinfo->count = 1;
2395 uinfo->value.integer.min = 8000;
2396 uinfo->value.integer.max = 192000;
2397 uinfo->value.integer.step = 100;
2398
2399 return 0;
2400}
2401
2402static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2403 struct snd_ctl_elem_value *ucontrol)
2404{
2405 u32 h_control = kcontrol->private_value;
2406 u32 rate;
2407 u16 e;
2408
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002409 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002410 if (!e)
2411 ucontrol->value.integer.value[0] = rate;
2412 else
2413 ucontrol->value.integer.value[0] = 0;
2414 return 0;
2415}
2416
2417static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2418 struct snd_ctl_elem_value *ucontrol)
2419{
2420 int change;
2421 u32 h_control = kcontrol->private_value;
2422
2423 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2424 asihpi->mixer_clkrate[addr][1] != right;
2425 */
2426 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002427 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002428 ucontrol->value.integer.value[0]));
2429 return change;
2430}
2431
2432static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_info *uinfo)
2434{
2435 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2436 uinfo->count = 1;
2437 uinfo->value.integer.min = 8000;
2438 uinfo->value.integer.max = 192000;
2439 uinfo->value.integer.step = 100;
2440
2441 return 0;
2442}
2443
2444static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2445 struct snd_ctl_elem_value *ucontrol)
2446{
2447 u32 h_control = kcontrol->private_value;
2448 u32 rate;
2449 u16 e;
2450
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002451 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002452 if (!e)
2453 ucontrol->value.integer.value[0] = rate;
2454 else
2455 ucontrol->value.integer.value[0] = 0;
2456 return 0;
2457}
2458
2459static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2460 struct hpi_control *hpi_ctl)
2461{
2462 struct snd_card *card = asihpi->card;
2463 struct snd_kcontrol_new snd_control;
2464
2465 struct clk_cache *clkcache = &asihpi->cc;
2466 u32 hSC = hpi_ctl->h_control;
2467 int has_aes_in = 0;
2468 int i, j;
2469 u16 source;
2470
2471 snd_control.private_value = hpi_ctl->h_control;
2472
2473 clkcache->has_local = 0;
2474
2475 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002476 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002477 i, &source))
2478 break;
2479 clkcache->s[i].source = source;
2480 clkcache->s[i].index = 0;
2481 clkcache->s[i].name = sampleclock_sources[source];
2482 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2483 has_aes_in = 1;
2484 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2485 clkcache->has_local = 1;
2486 }
2487 if (has_aes_in)
2488 /* already will have picked up index 0 above */
2489 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002490 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002491 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2492 &source))
2493 break;
2494 clkcache->s[i].source =
2495 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2496 clkcache->s[i].index = j;
2497 clkcache->s[i].name = sampleclock_sources[
2498 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2499 i++;
2500 }
2501 clkcache->count = i;
2502
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002503 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002504 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2505 snd_control.info = snd_asihpi_clksrc_info;
2506 snd_control.get = snd_asihpi_clksrc_get;
2507 snd_control.put = snd_asihpi_clksrc_put;
2508 if (ctl_add(card, &snd_control, asihpi) < 0)
2509 return -EINVAL;
2510
2511
2512 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002513 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002514 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2515 snd_control.info = snd_asihpi_clklocal_info;
2516 snd_control.get = snd_asihpi_clklocal_get;
2517 snd_control.put = snd_asihpi_clklocal_put;
2518
2519
2520 if (ctl_add(card, &snd_control, asihpi) < 0)
2521 return -EINVAL;
2522 }
2523
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002524 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002525 snd_control.access =
2526 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2527 snd_control.info = snd_asihpi_clkrate_info;
2528 snd_control.get = snd_asihpi_clkrate_get;
2529
2530 return ctl_add(card, &snd_control, asihpi);
2531}
2532/*------------------------------------------------------------
2533 Mixer
2534 ------------------------------------------------------------*/
2535
2536static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2537{
2538 struct snd_card *card = asihpi->card;
2539 unsigned int idx = 0;
2540 unsigned int subindex = 0;
2541 int err;
2542 struct hpi_control hpi_ctl, prev_ctl;
2543
2544 if (snd_BUG_ON(!asihpi))
2545 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002546 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002547
2548 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002549 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002550 &asihpi->h_mixer);
2551 hpi_handle_error(err);
2552 if (err)
2553 return -err;
2554
Takashi Iwai21896bc2010-06-02 12:08:37 +02002555 memset(&prev_ctl, 0, sizeof(prev_ctl));
2556 prev_ctl.control_type = -1;
2557
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002558 for (idx = 0; idx < 2000; idx++) {
2559 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002560 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002561 idx,
2562 &hpi_ctl.src_node_type,
2563 &hpi_ctl.src_node_index,
2564 &hpi_ctl.dst_node_type,
2565 &hpi_ctl.dst_node_index,
2566 &hpi_ctl.control_type,
2567 &hpi_ctl.h_control);
2568 if (err) {
2569 if (err == HPI_ERROR_CONTROL_DISABLED) {
2570 if (mixer_dump)
2571 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002572 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002573 idx);
2574 continue;
2575 } else
2576 break;
2577
2578 }
2579
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002580 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2581 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582
2583 /* ASI50xx in SSX mode has multiple meters on the same node.
2584 Use subindex to create distinct ALSA controls
2585 for any duplicated controls.
2586 */
2587 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2588 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2589 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2590 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2591 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2592 subindex++;
2593 else
2594 subindex = 0;
2595
2596 prev_ctl = hpi_ctl;
2597
2598 switch (hpi_ctl.control_type) {
2599 case HPI_CONTROL_VOLUME:
2600 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2601 break;
2602 case HPI_CONTROL_LEVEL:
2603 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2604 break;
2605 case HPI_CONTROL_MULTIPLEXER:
2606 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2607 break;
2608 case HPI_CONTROL_CHANNEL_MODE:
2609 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2610 break;
2611 case HPI_CONTROL_METER:
2612 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2613 break;
2614 case HPI_CONTROL_SAMPLECLOCK:
2615 err = snd_asihpi_sampleclock_add(
2616 asihpi, &hpi_ctl);
2617 break;
2618 case HPI_CONTROL_CONNECTION: /* ignore these */
2619 continue;
2620 case HPI_CONTROL_TUNER:
2621 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2622 break;
2623 case HPI_CONTROL_AESEBU_TRANSMITTER:
2624 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2625 break;
2626 case HPI_CONTROL_AESEBU_RECEIVER:
2627 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2628 break;
2629 case HPI_CONTROL_VOX:
2630 case HPI_CONTROL_BITSTREAM:
2631 case HPI_CONTROL_MICROPHONE:
2632 case HPI_CONTROL_PARAMETRIC_EQ:
2633 case HPI_CONTROL_COMPANDER:
2634 default:
2635 if (mixer_dump)
2636 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002637 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002638 "(%d) %d %d %d %d %d\n",
2639 idx,
2640 hpi_ctl.control_type,
2641 hpi_ctl.src_node_type,
2642 hpi_ctl.src_node_index,
2643 hpi_ctl.dst_node_type,
2644 hpi_ctl.dst_node_index);
2645 continue;
2646 };
2647 if (err < 0)
2648 return err;
2649 }
2650 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2651 hpi_handle_error(err);
2652
2653 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2654
2655 return 0;
2656}
2657
2658/*------------------------------------------------------------
2659 /proc interface
2660 ------------------------------------------------------------*/
2661
2662static void
2663snd_asihpi_proc_read(struct snd_info_entry *entry,
2664 struct snd_info_buffer *buffer)
2665{
2666 struct snd_card_asihpi *asihpi = entry->private_data;
2667 u16 version;
2668 u32 h_control;
2669 u32 rate = 0;
2670 u16 source = 0;
2671 int err;
2672
2673 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2674 snd_iprintf(buffer,
2675 "adapter ID=%4X\n_index=%d\n"
2676 "num_outstreams=%d\n_num_instreams=%d\n",
2677 asihpi->type, asihpi->adapter_index,
2678 asihpi->num_outstreams, asihpi->num_instreams);
2679
2680 version = asihpi->version;
2681 snd_iprintf(buffer,
2682 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2683 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2684 version & 0x7,
2685 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2686
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002687 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002688 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2689 HPI_CONTROL_SAMPLECLOCK, &h_control);
2690
2691 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002692 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002693 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002694 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002695
2696 if (!err)
2697 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2698 rate, sampleclock_sources[source]);
2699 }
2700
2701}
2702
2703
2704static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2705{
2706 struct snd_info_entry *entry;
2707
2708 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2709 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2710}
2711
2712/*------------------------------------------------------------
2713 HWDEP
2714 ------------------------------------------------------------*/
2715
2716static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2717{
2718 if (enable_hpi_hwdep)
2719 return 0;
2720 else
2721 return -ENODEV;
2722
2723}
2724
2725static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2726{
2727 if (enable_hpi_hwdep)
2728 return asihpi_hpi_release(file);
2729 else
2730 return -ENODEV;
2731}
2732
2733static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2734 unsigned int cmd, unsigned long arg)
2735{
2736 if (enable_hpi_hwdep)
2737 return asihpi_hpi_ioctl(file, cmd, arg);
2738 else
2739 return -ENODEV;
2740}
2741
2742
2743/* results in /dev/snd/hwC#D0 file for each card with index #
2744 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2745*/
2746static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2747 int device, struct snd_hwdep **rhwdep)
2748{
2749 struct snd_hwdep *hw;
2750 int err;
2751
2752 if (rhwdep)
2753 *rhwdep = NULL;
2754 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2755 if (err < 0)
2756 return err;
2757 strcpy(hw->name, "asihpi (HPI)");
2758 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2759 hw->ops.open = snd_asihpi_hpi_open;
2760 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2761 hw->ops.release = snd_asihpi_hpi_release;
2762 hw->private_data = asihpi;
2763 if (rhwdep)
2764 *rhwdep = hw;
2765 return 0;
2766}
2767
2768/*------------------------------------------------------------
2769 CARD
2770 ------------------------------------------------------------*/
2771static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2772 const struct pci_device_id *pci_id)
2773{
2774 int err;
2775
2776 u16 version;
2777 int pcm_substreams;
2778
2779 struct hpi_adapter *hpi_card;
2780 struct snd_card *card;
2781 struct snd_card_asihpi *asihpi;
2782
2783 u32 h_control;
2784 u32 h_stream;
2785
2786 static int dev;
2787 if (dev >= SNDRV_CARDS)
2788 return -ENODEV;
2789
2790 /* Should this be enable[hpi_card->index] ? */
2791 if (!enable[dev]) {
2792 dev++;
2793 return -ENOENT;
2794 }
2795
2796 err = asihpi_adapter_probe(pci_dev, pci_id);
2797 if (err < 0)
2798 return err;
2799
2800 hpi_card = pci_get_drvdata(pci_dev);
2801 /* first try to give the card the same index as its hardware index */
2802 err = snd_card_create(hpi_card->index,
2803 id[hpi_card->index], THIS_MODULE,
2804 sizeof(struct snd_card_asihpi),
2805 &card);
2806 if (err < 0) {
2807 /* if that fails, try the default index==next available */
2808 err =
2809 snd_card_create(index[dev], id[dev],
2810 THIS_MODULE,
2811 sizeof(struct snd_card_asihpi),
2812 &card);
2813 if (err < 0)
2814 return err;
2815 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002816 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002817 hpi_card->index, card->number);
2818 }
2819
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002820 snd_card_set_dev(card, &pci_dev->dev);
2821
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002822 asihpi = (struct snd_card_asihpi *) card->private_data;
2823 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002824 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002825 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002826 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002827 asihpi->adapter_index,
2828 &asihpi->num_outstreams,
2829 &asihpi->num_instreams,
2830 &asihpi->version,
2831 &asihpi->serial_number, &asihpi->type));
2832
2833 version = asihpi->version;
2834 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2835 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002836 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002837 asihpi->type, asihpi->adapter_index,
2838 asihpi->num_outstreams,
2839 asihpi->num_instreams, asihpi->serial_number,
2840 ((version >> 3) & 0xf) + 'A',
2841 version & 0x7,
2842 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2843
2844 pcm_substreams = asihpi->num_outstreams;
2845 if (pcm_substreams < asihpi->num_instreams)
2846 pcm_substreams = asihpi->num_instreams;
2847
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002848 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002849 HPI_ADAPTER_PROPERTY_CAPS1,
2850 NULL, &asihpi->support_grouping);
2851 if (err)
2852 asihpi->support_grouping = 0;
2853
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002854 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002855 HPI_ADAPTER_PROPERTY_CAPS2,
2856 &asihpi->support_mrx, NULL);
2857 if (err)
2858 asihpi->support_mrx = 0;
2859
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002860 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002861 HPI_ADAPTER_PROPERTY_INTERVAL,
2862 NULL, &asihpi->update_interval_frames);
2863 if (err)
2864 asihpi->update_interval_frames = 512;
2865
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002866 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002867 asihpi->update_interval_frames *= 2;
2868
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002869 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002870 0, &h_stream));
2871
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002872 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002873 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002874
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002875 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002876
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002877 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002878 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2879 &asihpi->in_max_chans, &asihpi->out_max_chans);
2880 if (err) {
2881 asihpi->in_max_chans = 2;
2882 asihpi->out_max_chans = 2;
2883 }
2884
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002885 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2886 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887 asihpi->support_grouping,
2888 asihpi->support_mrx
2889 );
2890
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2892 if (err < 0) {
2893 snd_printk(KERN_ERR "pcm_new failed\n");
2894 goto __nodev;
2895 }
2896 err = snd_card_asihpi_mixer_new(asihpi);
2897 if (err < 0) {
2898 snd_printk(KERN_ERR "mixer_new failed\n");
2899 goto __nodev;
2900 }
2901
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002902 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002903 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2904 HPI_CONTROL_SAMPLECLOCK, &h_control);
2905
2906 if (!err)
2907 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002908 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002909
2910 snd_asihpi_proc_init(asihpi);
2911
2912 /* always create, can be enabled or disabled dynamically
2913 by enable_hwdep module param*/
2914 snd_asihpi_hpi_new(asihpi, 0, NULL);
2915
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002916 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002917
2918 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2919 sprintf(card->longname, "%s %i",
2920 card->shortname, asihpi->adapter_index);
2921 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002922
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002923 if (!err) {
2924 hpi_card->snd_card_asihpi = card;
2925 dev++;
2926 return 0;
2927 }
2928__nodev:
2929 snd_card_free(card);
2930 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2931 return err;
2932
2933}
2934
2935static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2936{
2937 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2938
2939 snd_card_free(hpi_card->snd_card_asihpi);
2940 hpi_card->snd_card_asihpi = NULL;
2941 asihpi_adapter_remove(pci_dev);
2942}
2943
2944static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2945 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2946 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2947 (kernel_ulong_t)HPI_6205},
2948 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2949 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2950 (kernel_ulong_t)HPI_6000},
2951 {0,}
2952};
2953MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2954
2955static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002956 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002957 .id_table = asihpi_pci_tbl,
2958 .probe = snd_asihpi_probe,
2959 .remove = __devexit_p(snd_asihpi_remove),
2960#ifdef CONFIG_PM
2961/* .suspend = snd_asihpi_suspend,
2962 .resume = snd_asihpi_resume, */
2963#endif
2964};
2965
2966static int __init snd_asihpi_init(void)
2967{
2968 asihpi_init();
2969 return pci_register_driver(&driver);
2970}
2971
2972static void __exit snd_asihpi_exit(void)
2973{
2974
2975 pci_unregister_driver(&driver);
2976 asihpi_exit();
2977}
2978
2979module_init(snd_asihpi_init)
2980module_exit(snd_asihpi_exit)
2981