blob: 6e89e5b83a49b28b890b4c600d2c37db0cf046d9 [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>
Paul Gortmakerda155d52011-07-15 12:38:28 -040035#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020036#include <sound/core.h>
37#include <sound/control.h>
38#include <sound/pcm.h>
39#include <sound/pcm_params.h>
40#include <sound/info.h>
41#include <sound/initval.h>
42#include <sound/tlv.h>
43#include <sound/hwdep.h>
44
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020045MODULE_LICENSE("GPL");
46MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
47MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
48
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130049#if defined CONFIG_SND_DEBUG_VERBOSE
50/**
51 * snd_printddd - very verbose debug printk
52 * @format: format string
53 *
54 * Works like snd_printk() for debugging purposes.
55 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
56 * Must set snd module debug parameter to 3 to enable at runtime.
57 */
58#define snd_printddd(format, args...) \
59 __snd_printk(3, __FILE__, __LINE__, format, ##args)
60#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120061#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130062#endif
63
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020064static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
65static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103066static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
67static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020068
69module_param_array(index, int, NULL, S_IRUGO);
70MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
71
72module_param_array(id, charp, NULL, S_IRUGO);
73MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
74
75module_param_array(enable, bool, NULL, S_IRUGO);
76MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
77
78module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
79MODULE_PARM_DESC(enable_hpi_hwdep,
80 "ALSA enable HPI hwdep for AudioScience soundcard ");
81
82/* identify driver */
83#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130084static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020085module_param(build_info, charp, S_IRUGO);
86MODULE_PARM_DESC(build_info, "built using headers from kernel source");
87#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130088static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020089module_param(build_info, charp, S_IRUGO);
90MODULE_PARM_DESC(build_info, "built within ALSA source");
91#endif
92
93/* set to 1 to dump every control from adapter to log */
94static const int mixer_dump;
95
96#define DEFAULT_SAMPLERATE 44100
97static int adapter_fs = DEFAULT_SAMPLERATE;
98
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020099/* defaults */
100#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300101#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200102#define BUFFER_BYTES_MAX (512 * 1024)
103
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200104#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
105
106struct clk_source {
107 int source;
108 int index;
109 char *name;
110};
111
112struct clk_cache {
113 int count;
114 int has_local;
115 struct clk_source s[MAX_CLOCKSOURCES];
116};
117
118/* Per card data */
119struct snd_card_asihpi {
120 struct snd_card *card;
121 struct pci_dev *pci;
122 u16 adapter_index;
123 u32 serial_number;
124 u16 type;
125 u16 version;
126 u16 num_outstreams;
127 u16 num_instreams;
128
129 u32 h_mixer;
130 struct clk_cache cc;
131
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200132 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200133 u16 support_grouping;
134 u16 support_mrx;
135 u16 update_interval_frames;
136 u16 in_max_chans;
137 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300138 u16 in_min_chans;
139 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200140};
141
142/* Per stream data */
143struct snd_card_asihpi_pcm {
144 struct timer_list timer;
145 unsigned int respawn_timer;
146 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300147 unsigned int buffer_bytes;
148 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200149 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300150 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
151 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
152 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200153 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200154 struct snd_pcm_substream *substream;
155 u32 h_stream;
156 struct hpi_format format;
157};
158
159/* universal stream verbs work with out or in stream handles */
160
161/* Functions to allow driver to give a buffer to HPI for busmastering */
162
163static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200164 u32 h_stream, /* handle to outstream. */
165 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
166 u32 pci_address
167)
168{
169 struct hpi_message hm;
170 struct hpi_response hr;
171 unsigned int obj = hpi_handle_object(h_stream);
172
173 if (!h_stream)
174 return HPI_ERROR_INVALID_OBJ;
175 hpi_init_message_response(&hm, &hr, obj,
176 obj == HPI_OBJ_OSTREAM ?
177 HPI_OSTREAM_HOSTBUFFER_ALLOC :
178 HPI_ISTREAM_HOSTBUFFER_ALLOC);
179
180 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
181 &hm.obj_index);
182
183 hm.u.d.u.buffer.buffer_size = size_in_bytes;
184 hm.u.d.u.buffer.pci_address = pci_address;
185 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
186 hpi_send_recv(&hm, &hr);
187 return hr.error;
188}
189
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300190static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200191{
192 struct hpi_message hm;
193 struct hpi_response hr;
194 unsigned int obj = hpi_handle_object(h_stream);
195
196 if (!h_stream)
197 return HPI_ERROR_INVALID_OBJ;
198
199 hpi_init_message_response(&hm, &hr, obj,
200 obj == HPI_OBJ_OSTREAM ?
201 HPI_OSTREAM_HOSTBUFFER_FREE :
202 HPI_ISTREAM_HOSTBUFFER_FREE);
203
204 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
205 &hm.obj_index);
206 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
207 hpi_send_recv(&hm, &hr);
208 return hr.error;
209}
210
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300211static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200212{
213 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300214 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200215 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300216 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200217}
218
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220{
221 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300222 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200223 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300224 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225}
226
227static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200228 u32 h_stream,
229 u16 *pw_state,
230 u32 *pbuffer_size,
231 u32 *pdata_in_buffer,
232 u32 *psample_count,
233 u32 *pauxiliary_data
234)
235{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300236 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200237 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300238 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200239 pbuffer_size, pdata_in_buffer,
240 psample_count, pauxiliary_data);
241 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300242 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243 pbuffer_size, pdata_in_buffer,
244 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300245 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200246}
247
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300248static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200249 u32 h_master,
250 u32 h_stream)
251{
252 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300253 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300255 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256}
257
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259{
260 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264}
265
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300266static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267 u32 h_stream, u32 *mo, u32 *mi)
268{
269 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300270 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300272 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200273}
274
275static u16 handle_error(u16 err, int line, char *filename)
276{
277 if (err)
278 printk(KERN_WARNING
279 "in file %s, line %d: HPI error %d\n",
280 filename, line, err);
281 return err;
282}
283
284#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
285
286/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200287
288static void print_hwparams(struct snd_pcm_substream *substream,
289 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200290{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200291 char name[16];
292 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200293 snd_printd("%s HWPARAMS\n", name);
294 snd_printd(" samplerate %d Hz\n", params_rate(p));
295 snd_printd(" channels %d\n", params_channels(p));
296 snd_printd(" format %d\n", params_format(p));
297 snd_printd(" subformat %d\n", params_subformat(p));
298 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
299 snd_printd(" period %d B\n", params_period_bytes(p));
300 snd_printd(" access %d\n", params_access(p));
301 snd_printd(" period_size %d\n", params_period_size(p));
302 snd_printd(" periods %d\n", params_periods(p));
303 snd_printd(" buffer_size %d\n", params_buffer_size(p));
304 snd_printd(" %d B/s\n", params_rate(p) *
305 params_channels(p) *
306 snd_pcm_format_width(params_format(p)) / 8);
307
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200308}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200309
310static snd_pcm_format_t hpi_to_alsa_formats[] = {
311 -1, /* INVALID */
312 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
313 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
314 -1, /* HPI_FORMAT_MPEG_L1 3 */
315 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
316 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
317 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
318 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
319 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
320 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
321 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
322 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
323 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
324 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
325 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
326#if 1
327 /* ALSA can't handle 3 byte sample size together with power-of-2
328 * constraint on buffer_bytes, so disable this format
329 */
330 -1
331#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300332 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200333#endif
334};
335
336
337static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
338 u16 *hpi_format)
339{
340 u16 format;
341
342 for (format = HPI_FORMAT_PCM8_UNSIGNED;
343 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
344 if (hpi_to_alsa_formats[format] == alsa_format) {
345 *hpi_format = format;
346 return 0;
347 }
348 }
349
350 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
351 alsa_format);
352 *hpi_format = 0;
353 return -EINVAL;
354}
355
356static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
357 struct snd_pcm_hardware *pcmhw)
358{
359 u16 err;
360 u32 h_control;
361 u32 sample_rate;
362 int idx;
363 unsigned int rate_min = 200000;
364 unsigned int rate_max = 0;
365 unsigned int rates = 0;
366
367 if (asihpi->support_mrx) {
368 rates |= SNDRV_PCM_RATE_CONTINUOUS;
369 rates |= SNDRV_PCM_RATE_8000_96000;
370 rate_min = 8000;
371 rate_max = 100000;
372 } else {
373 /* on cards without SRC,
374 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300375 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200376 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
377 HPI_CONTROL_SAMPLECLOCK, &h_control);
378 if (err) {
379 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300380 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200381 }
382
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300383 for (idx = -1; idx < 100; idx++) {
384 if (idx == -1) {
385 if (hpi_sample_clock_get_sample_rate(h_control,
386 &sample_rate))
387 continue;
388 } else if (hpi_sample_clock_query_local_rate(h_control,
389 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200390 break;
391 }
392
393 rate_min = min(rate_min, sample_rate);
394 rate_max = max(rate_max, sample_rate);
395
396 switch (sample_rate) {
397 case 5512:
398 rates |= SNDRV_PCM_RATE_5512;
399 break;
400 case 8000:
401 rates |= SNDRV_PCM_RATE_8000;
402 break;
403 case 11025:
404 rates |= SNDRV_PCM_RATE_11025;
405 break;
406 case 16000:
407 rates |= SNDRV_PCM_RATE_16000;
408 break;
409 case 22050:
410 rates |= SNDRV_PCM_RATE_22050;
411 break;
412 case 32000:
413 rates |= SNDRV_PCM_RATE_32000;
414 break;
415 case 44100:
416 rates |= SNDRV_PCM_RATE_44100;
417 break;
418 case 48000:
419 rates |= SNDRV_PCM_RATE_48000;
420 break;
421 case 64000:
422 rates |= SNDRV_PCM_RATE_64000;
423 break;
424 case 88200:
425 rates |= SNDRV_PCM_RATE_88200;
426 break;
427 case 96000:
428 rates |= SNDRV_PCM_RATE_96000;
429 break;
430 case 176400:
431 rates |= SNDRV_PCM_RATE_176400;
432 break;
433 case 192000:
434 rates |= SNDRV_PCM_RATE_192000;
435 break;
436 default: /* some other rate */
437 rates |= SNDRV_PCM_RATE_KNOT;
438 }
439 }
440 }
441
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200442 pcmhw->rates = rates;
443 pcmhw->rate_min = rate_min;
444 pcmhw->rate_max = rate_max;
445}
446
447static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
448 struct snd_pcm_hw_params *params)
449{
450 struct snd_pcm_runtime *runtime = substream->runtime;
451 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
452 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
453 int err;
454 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400455 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456 unsigned int bytes_per_sec;
457
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200458 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200459 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
460 if (err < 0)
461 return err;
462 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
463 if (err)
464 return err;
465
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200466 hpi_handle_error(hpi_format_create(&dpcm->format,
467 params_channels(params),
468 format, params_rate(params), 0, 0));
469
470 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300471 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 return -EINVAL;
473
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300474 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200475 dpcm->h_stream, &dpcm->format) != 0)
476 return -EINVAL;
477 }
478
479 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200480 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300481 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200482 params_buffer_bytes(params), runtime->dma_addr);
483 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300484 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200485 "stream_host_buffer_attach succeeded %u %lu\n",
486 params_buffer_bytes(params),
487 (unsigned long)runtime->dma_addr);
488 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300489 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200490 err);
491 return -ENOMEM;
492 }
493
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300494 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200495 &dpcm->hpi_buffer_attached,
496 NULL, NULL, NULL);
497
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300498 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200499 dpcm->hpi_buffer_attached);
500 }
501 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400502 width = snd_pcm_format_width(params_format(params));
503 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400505 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200506 return -EINVAL;
507
508 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300509 dpcm->buffer_bytes = params_buffer_bytes(params);
510 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200511
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200512 return 0;
513}
514
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300515static int
516snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
517{
518 struct snd_pcm_runtime *runtime = substream->runtime;
519 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
520 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300521 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300522
523 snd_pcm_lib_free_pages(substream);
524 return 0;
525}
526
527static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
528{
529 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
530 kfree(dpcm);
531}
532
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200533static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
534 substream)
535{
536 struct snd_pcm_runtime *runtime = substream->runtime;
537 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
538 int expiry;
539
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300540 expiry = HZ / 200;
541 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300542 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200543 dpcm->timer.expires = jiffies + expiry;
544 dpcm->respawn_timer = 1;
545 add_timer(&dpcm->timer);
546}
547
548static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
549{
550 struct snd_pcm_runtime *runtime = substream->runtime;
551 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
552
553 dpcm->respawn_timer = 0;
554 del_timer(&dpcm->timer);
555}
556
557static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
558 int cmd)
559{
560 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
561 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
562 struct snd_pcm_substream *s;
563 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200564 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200565
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200566 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200567 snd_printdd("%s trigger\n", name);
568
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200569 switch (cmd) {
570 case SNDRV_PCM_TRIGGER_START:
571 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300572 struct snd_pcm_runtime *runtime = s->runtime;
573 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200574
575 if (snd_pcm_substream_chip(s) != card)
576 continue;
577
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300578 /* don't link Cap and Play */
579 if (substream->stream != s->stream)
580 continue;
581
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200582 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200583 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200584 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300585 * in buffer? Must be at least one period!
586 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200587 * buffer is bigger it may contain even more
588 * data??
589 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300590 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300591 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300593 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300594 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200595 preload,
596 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300597 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200598 }
599
600 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200601 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300602 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200603 dpcm->h_stream,
604 ds->h_stream);
605 if (!e) {
606 snd_pcm_trigger_done(s, substream);
607 } else {
608 hpi_handle_error(e);
609 break;
610 }
611 } else
612 break;
613 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300614 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200615 /* start the master stream */
616 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300617 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200618 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300619 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200620 break;
621
622 case SNDRV_PCM_TRIGGER_STOP:
623 snd_card_asihpi_pcm_timer_stop(substream);
624 snd_pcm_group_for_each_entry(s, substream) {
625 if (snd_pcm_substream_chip(s) != card)
626 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300627 /* don't link Cap and Play */
628 if (substream->stream != s->stream)
629 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200630
631 /*? workaround linked streams don't
632 transition to SETUP 20070706*/
633 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
634
635 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200636 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200637 snd_pcm_trigger_done(s, substream);
638 } else
639 break;
640 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300641 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200642
643 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300644 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
646 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300647 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200648
649 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300650 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200651 break;
652
653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300654 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300655 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 snd_card_asihpi_pcm_timer_start(substream);
657 break;
658 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300659 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300661 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200662 break;
663 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300664 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665 return -EINVAL;
666 }
667
668 return 0;
669}
670
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200671/*algorithm outline
672 Without linking degenerates to getting single stream pos etc
673 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
674*/
675/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300676pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200677for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300678 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300680 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200681}
682timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
683for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300684 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300685 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300687 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200688 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300689 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300691 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200692 }
693 }
694 snd_pcm_period_elapsed(s);
695 }
696}
697*/
698
699/** Minimum of 2 modulo values. Works correctly when the difference between
700* the values is less than half the modulus
701*/
702static inline unsigned int modulo_min(unsigned int a, unsigned int b,
703 unsigned long int modulus)
704{
705 unsigned int result;
706 if (((a-b) % modulus) < (modulus/2))
707 result = b;
708 else
709 result = a;
710
711 return result;
712}
713
714/** Timer function, equivalent to interrupt service routine for cards
715*/
716static void snd_card_asihpi_timer_function(unsigned long data)
717{
718 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300719 struct snd_pcm_substream *substream = dpcm->substream;
720 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200721 struct snd_pcm_runtime *runtime;
722 struct snd_pcm_substream *s;
723 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300724 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200725 unsigned int remdata, xfercount, next_jiffies;
726 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300727 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200728 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300729 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200730 char name[16];
731
732 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200733
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200734 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300735
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200736 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300737 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200738 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
739 runtime = s->runtime;
740
741 if (snd_pcm_substream_chip(s) != card)
742 continue;
743
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300744 /* don't link Cap and Play */
745 if (substream->stream != s->stream)
746 continue;
747
748 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300750 &buffer_size, &bytes_avail,
751 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200752
753 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300754 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200756 if (!card->can_dma)
757 on_card_bytes = bytes_avail;
758
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300759 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300760 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300761 if (state == HPI_STATE_STOPPED) {
762 if ((bytes_avail == 0) &&
763 (on_card_bytes < ds->pcm_buf_host_rw_ofs)) {
764 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300765 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200766 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300767 }
768 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300769 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300770 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200771 ds->drained_count++;
772 if (ds->drained_count > 2) {
773 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
774 continue;
775 }
776 } else {
777 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300778 }
779 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300780 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200781
782 if (first) {
783 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300784 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300785 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200786 first = 0;
787 } else {
788 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300789 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200790 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300791 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200792 newdata);
793 }
794
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200795 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200796 (unsigned long)frames_to_bytes(runtime,
797 runtime->status->hw_ptr),
798 (unsigned long)frames_to_bytes(runtime,
799 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300800
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200801 snd_printdd("%d S=%d, "
802 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
803 "aux=0x%04X space=0x%04X\n",
804 s->number, state,
805 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
806 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300807 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300808 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200809 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300810 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200811
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300812 remdata = newdata % dpcm->period_bytes;
813 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300814 /* come back when on_card_bytes has decreased enough to allow
815 write to happen, or when data has been consumed to make another
816 period
817 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300818 if (xfercount && (on_card_bytes > dpcm->period_bytes))
819 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300820 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300821 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300822
823 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200825 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300826 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
827
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300828 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200829 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200830
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 /* don't link Cap and Play */
832 if (substream->stream != s->stream)
833 continue;
834
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300835 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
836
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200837 if (xfercount &&
838 /* Limit use of on card fifo for playback */
839 ((on_card_bytes <= ds->period_bytes) ||
840 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
841
842 {
843
844 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
845 unsigned int xfer1, xfer2;
846 char *pd = &s->runtime->dma_area[buf_ofs];
847
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200848 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200849 xfer1 = xfercount;
850 xfer2 = 0;
851 } else {
852 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
853 xfer2 = xfercount - xfer1;
854 }
855
856 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
857 snd_printddd("P%d write1 0x%04X 0x%04X\n",
858 s->number, xfer1, buf_ofs);
859 hpi_handle_error(
860 hpi_outstream_write_buf(
861 ds->h_stream, pd, xfer1,
862 &ds->format));
863
864 if (xfer2) {
865 pd = s->runtime->dma_area;
866
867 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200868 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200869 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200870 hpi_handle_error(
871 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200872 ds->h_stream, pd,
873 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200874 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200875 }
876 } else {
877 snd_printddd("C%d read1 0x%04x\n",
878 s->number, xfer1);
879 hpi_handle_error(
880 hpi_instream_read_buf(
881 ds->h_stream,
882 pd, xfer1));
883 if (xfer2) {
884 pd = s->runtime->dma_area;
885 snd_printddd("C%d read2 0x%04x\n",
886 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200887 hpi_handle_error(
888 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300889 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200890 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200891 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200892 }
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300893 ds->pcm_buf_host_rw_ofs += xfercount;
894 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200895 snd_pcm_period_elapsed(s);
896 }
897 }
898
899 if (dpcm->respawn_timer)
900 add_timer(&dpcm->timer);
901}
902
903/***************************** PLAYBACK OPS ****************/
904static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
905 unsigned int cmd, void *arg)
906{
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200907 snd_printddd(KERN_INFO "P%d ioctl %d\n", substream->number, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200908 return snd_pcm_lib_ioctl(substream, cmd, arg);
909}
910
911static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
912 substream)
913{
914 struct snd_pcm_runtime *runtime = substream->runtime;
915 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
916
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200917 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200918
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300919 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300920 dpcm->pcm_buf_host_rw_ofs = 0;
921 dpcm->pcm_buf_dma_ofs = 0;
922 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200923 return 0;
924}
925
926static snd_pcm_uframes_t
927snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
928{
929 struct snd_pcm_runtime *runtime = substream->runtime;
930 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
931 snd_pcm_uframes_t ptr;
932
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300933 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200934 snd_printddd("P%d pointer = 0x%04lx\n", substream->number, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200935 return ptr;
936}
937
938static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
939 u32 h_stream,
940 struct snd_pcm_hardware *pcmhw)
941{
942 struct hpi_format hpi_format;
943 u16 format;
944 u16 err;
945 u32 h_control;
946 u32 sample_rate = 48000;
947
948 /* on cards without SRC, must query at valid rate,
949 * maybe set by external sync
950 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300951 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200952 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
953 HPI_CONTROL_SAMPLECLOCK, &h_control);
954
955 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300956 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200957 &sample_rate);
958
959 for (format = HPI_FORMAT_PCM8_UNSIGNED;
960 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
961 err = hpi_format_create(&hpi_format,
962 2, format, sample_rate, 128000, 0);
963 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300964 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200965 &hpi_format);
966 if (!err && (hpi_to_alsa_formats[format] != -1))
967 pcmhw->formats |=
968 (1ULL << hpi_to_alsa_formats[format]);
969 }
970}
971
972static struct snd_pcm_hardware snd_card_asihpi_playback = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200973 .buffer_bytes_max = BUFFER_BYTES_MAX,
974 .period_bytes_min = PERIOD_BYTES_MIN,
975 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
976 .periods_min = PERIODS_MIN,
977 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
978 .fifo_size = 0,
979};
980
981static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
982{
983 struct snd_pcm_runtime *runtime = substream->runtime;
984 struct snd_card_asihpi_pcm *dpcm;
985 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
986 int err;
987
988 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
989 if (dpcm == NULL)
990 return -ENOMEM;
991
992 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300993 hpi_outstream_open(card->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200994 substream->number, &dpcm->h_stream);
995 hpi_handle_error(err);
996 if (err)
997 kfree(dpcm);
998 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
999 return -EBUSY;
1000 if (err)
1001 return -EIO;
1002
1003 /*? also check ASI5000 samplerate source
1004 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001005 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001006 */
1007
1008 init_timer(&dpcm->timer);
1009 dpcm->timer.data = (unsigned long) dpcm;
1010 dpcm->timer.function = snd_card_asihpi_timer_function;
1011 dpcm->substream = substream;
1012 runtime->private_data = dpcm;
1013 runtime->private_free = snd_card_asihpi_runtime_free;
1014
1015 snd_card_asihpi_playback.channels_max = card->out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001016 snd_card_asihpi_playback.channels_min = card->out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001017 /*?snd_card_asihpi_playback.period_bytes_min =
1018 card->out_max_chans * 4096; */
1019
1020 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1021 &snd_card_asihpi_playback);
1022
1023 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1024
1025 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1026 SNDRV_PCM_INFO_DOUBLE |
1027 SNDRV_PCM_INFO_BATCH |
1028 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001029 SNDRV_PCM_INFO_PAUSE |
1030 SNDRV_PCM_INFO_MMAP |
1031 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001032
1033 if (card->support_grouping)
1034 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
1035
1036 /* struct is copied, so can create initializer dynamically */
1037 runtime->hw = snd_card_asihpi_playback;
1038
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001039 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001040 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1041 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1042 if (err < 0)
1043 return err;
1044
1045 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1046 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001047
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001048 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001049 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001050
1051 snd_pcm_set_sync(substream);
1052
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001053 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001054
1055 return 0;
1056}
1057
1058static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1059{
1060 struct snd_pcm_runtime *runtime = substream->runtime;
1061 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1062
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001063 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001064 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001065
1066 return 0;
1067}
1068
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001069static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1070 .open = snd_card_asihpi_playback_open,
1071 .close = snd_card_asihpi_playback_close,
1072 .ioctl = snd_card_asihpi_playback_ioctl,
1073 .hw_params = snd_card_asihpi_pcm_hw_params,
1074 .hw_free = snd_card_asihpi_hw_free,
1075 .prepare = snd_card_asihpi_playback_prepare,
1076 .trigger = snd_card_asihpi_trigger,
1077 .pointer = snd_card_asihpi_playback_pointer,
1078};
1079
1080/***************************** CAPTURE OPS ****************/
1081static snd_pcm_uframes_t
1082snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1083{
1084 struct snd_pcm_runtime *runtime = substream->runtime;
1085 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1086
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001087 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001088 substream->number, dpcm->pcm_buf_dma_ofs);
1089 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001090 for the capture position, because those samples aren't yet in
1091 the local buffer available for reading.
1092 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001093 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001094}
1095
1096static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1097 unsigned int cmd, void *arg)
1098{
1099 return snd_pcm_lib_ioctl(substream, cmd, arg);
1100}
1101
1102static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1103{
1104 struct snd_pcm_runtime *runtime = substream->runtime;
1105 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1106
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001107 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001108 dpcm->pcm_buf_host_rw_ofs = 0;
1109 dpcm->pcm_buf_dma_ofs = 0;
1110 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001111
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001112 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001113 return 0;
1114}
1115
1116
1117
1118static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1119 u32 h_stream,
1120 struct snd_pcm_hardware *pcmhw)
1121{
1122 struct hpi_format hpi_format;
1123 u16 format;
1124 u16 err;
1125 u32 h_control;
1126 u32 sample_rate = 48000;
1127
1128 /* on cards without SRC, must query at valid rate,
1129 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001130 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001131 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1132 HPI_CONTROL_SAMPLECLOCK, &h_control);
1133
1134 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001135 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001136 &sample_rate);
1137
1138 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1139 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1140
1141 err = hpi_format_create(&hpi_format, 2, format,
1142 sample_rate, 128000, 0);
1143 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001144 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001145 &hpi_format);
1146 if (!err)
1147 pcmhw->formats |=
1148 (1ULL << hpi_to_alsa_formats[format]);
1149 }
1150}
1151
1152
1153static struct snd_pcm_hardware snd_card_asihpi_capture = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001154 .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;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001195 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001196 snd_card_asihpi_capture_format(card, dpcm->h_stream,
1197 &snd_card_asihpi_capture);
1198 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001199 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1200 SNDRV_PCM_INFO_MMAP |
1201 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001202
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001203 if (card->support_grouping)
1204 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1205
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001206 runtime->hw = snd_card_asihpi_capture;
1207
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001208 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001209 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1210 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1211 if (err < 0)
1212 return err;
1213
1214 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1215 card->update_interval_frames);
1216 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1217 card->update_interval_frames * 2, UINT_MAX);
1218
1219 snd_pcm_set_sync(substream);
1220
1221 return 0;
1222}
1223
1224static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1225{
1226 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1227
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001228 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001229 return 0;
1230}
1231
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001232static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1233 .open = snd_card_asihpi_capture_open,
1234 .close = snd_card_asihpi_capture_close,
1235 .ioctl = snd_card_asihpi_capture_ioctl,
1236 .hw_params = snd_card_asihpi_pcm_hw_params,
1237 .hw_free = snd_card_asihpi_hw_free,
1238 .prepare = snd_card_asihpi_capture_prepare,
1239 .trigger = snd_card_asihpi_trigger,
1240 .pointer = snd_card_asihpi_capture_pointer,
1241};
1242
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001243static int __devinit snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi,
1244 int device, int substreams)
1245{
1246 struct snd_pcm *pcm;
1247 int err;
1248
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001249 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001250 asihpi->num_outstreams, asihpi->num_instreams,
1251 &pcm);
1252 if (err < 0)
1253 return err;
1254 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001255 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1256 &snd_card_asihpi_playback_mmap_ops);
1257 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1258 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001259
1260 pcm->private_data = asihpi;
1261 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001262 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001263
1264 /*? do we want to emulate MMAP for non-BBM cards?
1265 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1266 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1267 snd_dma_pci_data(asihpi->pci),
1268 64*1024, BUFFER_BYTES_MAX);
1269
1270 return 0;
1271}
1272
1273/***************************** MIXER CONTROLS ****************/
1274struct hpi_control {
1275 u32 h_control;
1276 u16 control_type;
1277 u16 src_node_type;
1278 u16 src_node_index;
1279 u16 dst_node_type;
1280 u16 dst_node_index;
1281 u16 band;
1282 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1283};
1284
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001285static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001286 "invalid",
1287 "AM",
1288 "FM mono",
1289 "TV NTSC-M",
1290 "FM stereo",
1291 "AUX",
1292 "TV PAL BG",
1293 "TV PAL I",
1294 "TV PAL DK",
1295 "TV SECAM",
1296};
1297
1298compile_time_assert(
1299 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1300 (HPI_TUNER_BAND_LAST+1)),
1301 assert_tuner_band_names_size);
1302
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001303static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001304 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001305 "PCM",
1306 "Line",
1307 "Digital",
1308 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001309 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001310 "Clock",
1311 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001312 "Mic",
1313 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001314 "Analog",
1315 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001316 "RTP",
1317 "GPI",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001318};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001319
1320compile_time_assert(
1321 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001322 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323 assert_src_names_size);
1324
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001325static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001326 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001327 "PCM",
1328 "Line",
1329 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001330 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001331 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001332 "Net",
1333 "Analog",
1334 "RTP",
1335 "GPO",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001336};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001337
1338compile_time_assert(
1339 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001340 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001341 assert_dst_names_size);
1342
1343static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1344 struct snd_card_asihpi *asihpi)
1345{
1346 int err;
1347
1348 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1349 if (err < 0)
1350 return err;
1351 else if (mixer_dump)
1352 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1353
1354 return 0;
1355}
1356
1357/* Convert HPI control name and location into ALSA control name */
1358static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1359 struct hpi_control *hpi_ctl,
1360 char *name)
1361{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001362 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001363 memset(snd_control, 0, sizeof(*snd_control));
1364 snd_control->name = hpi_ctl->name;
1365 snd_control->private_value = hpi_ctl->h_control;
1366 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1367 snd_control->index = 0;
1368
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001369 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1370 dir = ""; /* clock is neither capture nor playback */
1371 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001372 dir = "Capture "; /* On or towards a PCM capture destination*/
1373 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1374 (!hpi_ctl->dst_node_type))
1375 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001376 else if (hpi_ctl->src_node_type &&
1377 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001378 (hpi_ctl->dst_node_type))
1379 dir = "Monitor Playback "; /* Between an input and an output */
1380 else
1381 dir = "Playback "; /* PCM Playback source, or output node */
1382
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001383 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001384 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001385 asihpi_src_names[hpi_ctl->src_node_type],
1386 hpi_ctl->src_node_index,
1387 asihpi_dst_names[hpi_ctl->dst_node_type],
1388 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001389 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001390 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001391 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001392 asihpi_dst_names[hpi_ctl->dst_node_type],
1393 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001394 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001396 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001397 asihpi_src_names[hpi_ctl->src_node_type],
1398 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001399 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001401 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1402 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001403}
1404
1405/*------------------------------------------------------------
1406 Volume controls
1407 ------------------------------------------------------------*/
1408#define VOL_STEP_mB 1
1409static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_info *uinfo)
1411{
1412 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001413 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001414 u16 err;
1415 /* native gains are in millibels */
1416 short min_gain_mB;
1417 short max_gain_mB;
1418 short step_gain_mB;
1419
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001420 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001421 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1422 if (err) {
1423 max_gain_mB = 0;
1424 min_gain_mB = -10000;
1425 step_gain_mB = VOL_STEP_mB;
1426 }
1427
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001428 err = hpi_meter_query_channels(h_control, &count);
1429 if (err)
1430 count = HPI_MAX_CHANNELS;
1431
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001432 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001433 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001434 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1435 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1436 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1437 return 0;
1438}
1439
1440static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1442{
1443 u32 h_control = kcontrol->private_value;
1444 short an_gain_mB[HPI_MAX_CHANNELS];
1445
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001446 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001447 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1448 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1449
1450 return 0;
1451}
1452
1453static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_value *ucontrol)
1455{
1456 int change;
1457 u32 h_control = kcontrol->private_value;
1458 short an_gain_mB[HPI_MAX_CHANNELS];
1459
1460 an_gain_mB[0] =
1461 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1462 an_gain_mB[1] =
1463 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1464 /* change = asihpi->mixer_volume[addr][0] != left ||
1465 asihpi->mixer_volume[addr][1] != right;
1466 */
1467 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001468 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001469 return change;
1470}
1471
1472static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1473
Takashi Iwai000477a2011-07-22 07:57:44 +02001474#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001475
1476static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1477 struct snd_ctl_elem_value *ucontrol)
1478{
1479 u32 h_control = kcontrol->private_value;
1480 u32 mute;
1481
1482 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1483 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1484
1485 return 0;
1486}
1487
1488static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_value *ucontrol)
1490{
1491 u32 h_control = kcontrol->private_value;
1492 int change = 1;
1493 /* HPI currently only supports all or none muting of multichannel volume
1494 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1495 */
1496 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1497 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1498 return change;
1499}
1500
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001501static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1502 struct hpi_control *hpi_ctl)
1503{
1504 struct snd_card *card = asihpi->card;
1505 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001506 int err;
1507 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001508
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001509 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001510 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1511 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1512 snd_control.info = snd_asihpi_volume_info;
1513 snd_control.get = snd_asihpi_volume_get;
1514 snd_control.put = snd_asihpi_volume_put;
1515 snd_control.tlv.p = db_scale_100;
1516
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001517 err = ctl_add(card, &snd_control, asihpi);
1518 if (err)
1519 return err;
1520
1521 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1522 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1523 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1524 snd_control.info = snd_asihpi_volume_mute_info;
1525 snd_control.get = snd_asihpi_volume_mute_get;
1526 snd_control.put = snd_asihpi_volume_mute_put;
1527 err = ctl_add(card, &snd_control, asihpi);
1528 }
1529 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001530}
1531
1532/*------------------------------------------------------------
1533 Level controls
1534 ------------------------------------------------------------*/
1535static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1536 struct snd_ctl_elem_info *uinfo)
1537{
1538 u32 h_control = kcontrol->private_value;
1539 u16 err;
1540 short min_gain_mB;
1541 short max_gain_mB;
1542 short step_gain_mB;
1543
1544 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001545 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001546 &max_gain_mB, &step_gain_mB);
1547 if (err) {
1548 max_gain_mB = 2400;
1549 min_gain_mB = -1000;
1550 step_gain_mB = 100;
1551 }
1552
1553 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1554 uinfo->count = 2;
1555 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1556 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1557 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1558 return 0;
1559}
1560
1561static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1562 struct snd_ctl_elem_value *ucontrol)
1563{
1564 u32 h_control = kcontrol->private_value;
1565 short an_gain_mB[HPI_MAX_CHANNELS];
1566
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001567 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001568 ucontrol->value.integer.value[0] =
1569 an_gain_mB[0] / HPI_UNITS_PER_dB;
1570 ucontrol->value.integer.value[1] =
1571 an_gain_mB[1] / HPI_UNITS_PER_dB;
1572
1573 return 0;
1574}
1575
1576static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1577 struct snd_ctl_elem_value *ucontrol)
1578{
1579 int change;
1580 u32 h_control = kcontrol->private_value;
1581 short an_gain_mB[HPI_MAX_CHANNELS];
1582
1583 an_gain_mB[0] =
1584 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1585 an_gain_mB[1] =
1586 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1587 /* change = asihpi->mixer_level[addr][0] != left ||
1588 asihpi->mixer_level[addr][1] != right;
1589 */
1590 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001591 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001592 return change;
1593}
1594
1595static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1596
1597static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1598 struct hpi_control *hpi_ctl)
1599{
1600 struct snd_card *card = asihpi->card;
1601 struct snd_kcontrol_new snd_control;
1602
1603 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001604 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001605 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1606 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1607 snd_control.info = snd_asihpi_level_info;
1608 snd_control.get = snd_asihpi_level_get;
1609 snd_control.put = snd_asihpi_level_put;
1610 snd_control.tlv.p = db_scale_level;
1611
1612 return ctl_add(card, &snd_control, asihpi);
1613}
1614
1615/*------------------------------------------------------------
1616 AESEBU controls
1617 ------------------------------------------------------------*/
1618
1619/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001620static const char * const asihpi_aesebu_format_names[] = {
1621 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001622
1623static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1624 struct snd_ctl_elem_info *uinfo)
1625{
1626 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1627 uinfo->count = 1;
1628 uinfo->value.enumerated.items = 3;
1629
1630 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1631 uinfo->value.enumerated.item =
1632 uinfo->value.enumerated.items - 1;
1633
1634 strcpy(uinfo->value.enumerated.name,
1635 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1636
1637 return 0;
1638}
1639
1640static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001642 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001643{
1644 u32 h_control = kcontrol->private_value;
1645 u16 source, err;
1646
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001647 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001648
1649 /* default to N/A */
1650 ucontrol->value.enumerated.item[0] = 0;
1651 /* return success but set the control to N/A */
1652 if (err)
1653 return 0;
1654 if (source == HPI_AESEBU_FORMAT_SPDIF)
1655 ucontrol->value.enumerated.item[0] = 1;
1656 if (source == HPI_AESEBU_FORMAT_AESEBU)
1657 ucontrol->value.enumerated.item[0] = 2;
1658
1659 return 0;
1660}
1661
1662static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1663 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001664 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001665{
1666 u32 h_control = kcontrol->private_value;
1667
1668 /* default to S/PDIF */
1669 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1670
1671 if (ucontrol->value.enumerated.item[0] == 1)
1672 source = HPI_AESEBU_FORMAT_SPDIF;
1673 if (ucontrol->value.enumerated.item[0] == 2)
1674 source = HPI_AESEBU_FORMAT_AESEBU;
1675
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001676 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001677 return -EINVAL;
1678
1679 return 1;
1680}
1681
1682static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_value *ucontrol) {
1684 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001685 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001686}
1687
1688static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1689 struct snd_ctl_elem_value *ucontrol) {
1690 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001691 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001692}
1693
1694static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_info *uinfo)
1696{
1697 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1698 uinfo->count = 1;
1699
1700 uinfo->value.integer.min = 0;
1701 uinfo->value.integer.max = 0X1F;
1702 uinfo->value.integer.step = 1;
1703
1704 return 0;
1705}
1706
1707static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1708 struct snd_ctl_elem_value *ucontrol) {
1709
1710 u32 h_control = kcontrol->private_value;
1711 u16 status;
1712
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001713 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1714 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001715 ucontrol->value.integer.value[0] = status;
1716 return 0;
1717}
1718
1719static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1720 struct hpi_control *hpi_ctl)
1721{
1722 struct snd_card *card = asihpi->card;
1723 struct snd_kcontrol_new snd_control;
1724
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001725 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001726 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1727 snd_control.info = snd_asihpi_aesebu_format_info;
1728 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1729 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1730
1731
1732 if (ctl_add(card, &snd_control, asihpi) < 0)
1733 return -EINVAL;
1734
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001735 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001736 snd_control.access =
1737 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1738 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1739 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1740
1741 return ctl_add(card, &snd_control, asihpi);
1742}
1743
1744static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1745 struct snd_ctl_elem_value *ucontrol) {
1746 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001747 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001748}
1749
1750static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1751 struct snd_ctl_elem_value *ucontrol) {
1752 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001753 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001754}
1755
1756
1757static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1758 struct hpi_control *hpi_ctl)
1759{
1760 struct snd_card *card = asihpi->card;
1761 struct snd_kcontrol_new snd_control;
1762
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001763 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001764 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1765 snd_control.info = snd_asihpi_aesebu_format_info;
1766 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1767 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1768
1769 return ctl_add(card, &snd_control, asihpi);
1770}
1771
1772/*------------------------------------------------------------
1773 Tuner controls
1774 ------------------------------------------------------------*/
1775
1776/* Gain */
1777
1778static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1779 struct snd_ctl_elem_info *uinfo)
1780{
1781 u32 h_control = kcontrol->private_value;
1782 u16 err;
1783 short idx;
1784 u16 gain_range[3];
1785
1786 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001787 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001788 idx, &gain_range[idx]);
1789 if (err != 0)
1790 return err;
1791 }
1792
1793 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1794 uinfo->count = 1;
1795 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1796 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1797 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1798 return 0;
1799}
1800
1801static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1802 struct snd_ctl_elem_value *ucontrol)
1803{
1804 /*
1805 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1806 */
1807 u32 h_control = kcontrol->private_value;
1808 short gain;
1809
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001810 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001811 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1812
1813 return 0;
1814}
1815
1816static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 /*
1820 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1821 */
1822 u32 h_control = kcontrol->private_value;
1823 short gain;
1824
1825 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001826 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001827
1828 return 1;
1829}
1830
1831/* Band */
1832
1833static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1834 u16 *band_list, u32 len) {
1835 u32 h_control = kcontrol->private_value;
1836 u16 err = 0;
1837 u32 i;
1838
1839 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001840 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001841 h_control, i, &band_list[i]);
1842 if (err != 0)
1843 break;
1844 }
1845
1846 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1847 return -EIO;
1848
1849 return i;
1850}
1851
1852static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_info *uinfo)
1854{
1855 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1856 int num_bands = 0;
1857
1858 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1859 HPI_TUNER_BAND_LAST);
1860
1861 if (num_bands < 0)
1862 return num_bands;
1863
1864 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1865 uinfo->count = 1;
1866 uinfo->value.enumerated.items = num_bands;
1867
1868 if (num_bands > 0) {
1869 if (uinfo->value.enumerated.item >=
1870 uinfo->value.enumerated.items)
1871 uinfo->value.enumerated.item =
1872 uinfo->value.enumerated.items - 1;
1873
1874 strcpy(uinfo->value.enumerated.name,
1875 asihpi_tuner_band_names[
1876 tuner_bands[uinfo->value.enumerated.item]]);
1877
1878 }
1879 return 0;
1880}
1881
1882static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *ucontrol)
1884{
1885 u32 h_control = kcontrol->private_value;
1886 /*
1887 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1888 */
1889 u16 band, idx;
1890 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1891 u32 num_bands = 0;
1892
1893 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1894 HPI_TUNER_BAND_LAST);
1895
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001896 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001897
1898 ucontrol->value.enumerated.item[0] = -1;
1899 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1900 if (tuner_bands[idx] == band) {
1901 ucontrol->value.enumerated.item[0] = idx;
1902 break;
1903 }
1904
1905 return 0;
1906}
1907
1908static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1909 struct snd_ctl_elem_value *ucontrol)
1910{
1911 /*
1912 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1913 */
1914 u32 h_control = kcontrol->private_value;
1915 u16 band;
1916 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1917 u32 num_bands = 0;
1918
1919 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1920 HPI_TUNER_BAND_LAST);
1921
1922 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001923 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001924
1925 return 1;
1926}
1927
1928/* Freq */
1929
1930static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1931 struct snd_ctl_elem_info *uinfo)
1932{
1933 u32 h_control = kcontrol->private_value;
1934 u16 err;
1935 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1936 u16 num_bands = 0, band_iter, idx;
1937 u32 freq_range[3], temp_freq_range[3];
1938
1939 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1940 HPI_TUNER_BAND_LAST);
1941
1942 freq_range[0] = INT_MAX;
1943 freq_range[1] = 0;
1944 freq_range[2] = INT_MAX;
1945
1946 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1947 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001948 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001949 idx, tuner_bands[band_iter],
1950 &temp_freq_range[idx]);
1951 if (err != 0)
1952 return err;
1953 }
1954
1955 /* skip band with bogus stepping */
1956 if (temp_freq_range[2] <= 0)
1957 continue;
1958
1959 if (temp_freq_range[0] < freq_range[0])
1960 freq_range[0] = temp_freq_range[0];
1961 if (temp_freq_range[1] > freq_range[1])
1962 freq_range[1] = temp_freq_range[1];
1963 if (temp_freq_range[2] < freq_range[2])
1964 freq_range[2] = temp_freq_range[2];
1965 }
1966
1967 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1968 uinfo->count = 1;
1969 uinfo->value.integer.min = ((int)freq_range[0]);
1970 uinfo->value.integer.max = ((int)freq_range[1]);
1971 uinfo->value.integer.step = ((int)freq_range[2]);
1972 return 0;
1973}
1974
1975static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1976 struct snd_ctl_elem_value *ucontrol)
1977{
1978 u32 h_control = kcontrol->private_value;
1979 u32 freq;
1980
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001981 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001982 ucontrol->value.integer.value[0] = freq;
1983
1984 return 0;
1985}
1986
1987static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1988 struct snd_ctl_elem_value *ucontrol)
1989{
1990 u32 h_control = kcontrol->private_value;
1991 u32 freq;
1992
1993 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001994 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001995
1996 return 1;
1997}
1998
1999/* Tuner control group initializer */
2000static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2001 struct hpi_control *hpi_ctl)
2002{
2003 struct snd_card *card = asihpi->card;
2004 struct snd_kcontrol_new snd_control;
2005
2006 snd_control.private_value = hpi_ctl->h_control;
2007 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2008
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002009 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002010 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002011 snd_control.info = snd_asihpi_tuner_gain_info;
2012 snd_control.get = snd_asihpi_tuner_gain_get;
2013 snd_control.put = snd_asihpi_tuner_gain_put;
2014
2015 if (ctl_add(card, &snd_control, asihpi) < 0)
2016 return -EINVAL;
2017 }
2018
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002019 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002020 snd_control.info = snd_asihpi_tuner_band_info;
2021 snd_control.get = snd_asihpi_tuner_band_get;
2022 snd_control.put = snd_asihpi_tuner_band_put;
2023
2024 if (ctl_add(card, &snd_control, asihpi) < 0)
2025 return -EINVAL;
2026
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002027 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002028 snd_control.info = snd_asihpi_tuner_freq_info;
2029 snd_control.get = snd_asihpi_tuner_freq_get;
2030 snd_control.put = snd_asihpi_tuner_freq_put;
2031
2032 return ctl_add(card, &snd_control, asihpi);
2033}
2034
2035/*------------------------------------------------------------
2036 Meter controls
2037 ------------------------------------------------------------*/
2038static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2039 struct snd_ctl_elem_info *uinfo)
2040{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002041 u32 h_control = kcontrol->private_value;
2042 u32 count;
2043 u16 err;
2044 err = hpi_meter_query_channels(h_control, &count);
2045 if (err)
2046 count = HPI_MAX_CHANNELS;
2047
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002048 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002049 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002050 uinfo->value.integer.min = 0;
2051 uinfo->value.integer.max = 0x7FFFFFFF;
2052 return 0;
2053}
2054
2055/* linear values for 10dB steps */
2056static int log2lin[] = {
2057 0x7FFFFFFF, /* 0dB */
2058 679093956,
2059 214748365,
2060 67909396,
2061 21474837,
2062 6790940,
2063 2147484, /* -60dB */
2064 679094,
2065 214748, /* -80 */
2066 67909,
2067 21475, /* -100 */
2068 6791,
2069 2147,
2070 679,
2071 214,
2072 68,
2073 21,
2074 7,
2075 2
2076};
2077
2078static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2079 struct snd_ctl_elem_value *ucontrol)
2080{
2081 u32 h_control = kcontrol->private_value;
2082 short an_gain_mB[HPI_MAX_CHANNELS], i;
2083 u16 err;
2084
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002085 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002086
2087 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2088 if (err) {
2089 ucontrol->value.integer.value[i] = 0;
2090 } else if (an_gain_mB[i] >= 0) {
2091 ucontrol->value.integer.value[i] =
2092 an_gain_mB[i] << 16;
2093 } else {
2094 /* -ve is log value in millibels < -60dB,
2095 * convert to (roughly!) linear,
2096 */
2097 ucontrol->value.integer.value[i] =
2098 log2lin[an_gain_mB[i] / -1000];
2099 }
2100 }
2101 return 0;
2102}
2103
2104static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2105 struct hpi_control *hpi_ctl, int subidx)
2106{
2107 struct snd_card *card = asihpi->card;
2108 struct snd_kcontrol_new snd_control;
2109
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002110 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002111 snd_control.access =
2112 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2113 snd_control.info = snd_asihpi_meter_info;
2114 snd_control.get = snd_asihpi_meter_get;
2115
2116 snd_control.index = subidx;
2117
2118 return ctl_add(card, &snd_control, asihpi);
2119}
2120
2121/*------------------------------------------------------------
2122 Multiplexer controls
2123 ------------------------------------------------------------*/
2124static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2125{
2126 u32 h_control = snd_control->private_value;
2127 struct hpi_control hpi_ctl;
2128 int s, err;
2129 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002130 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002131 &hpi_ctl.
2132 src_node_type,
2133 &hpi_ctl.
2134 src_node_index);
2135 if (err)
2136 break;
2137 }
2138 return s;
2139}
2140
2141static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2142 struct snd_ctl_elem_info *uinfo)
2143{
2144 int err;
2145 u16 src_node_type, src_node_index;
2146 u32 h_control = kcontrol->private_value;
2147
2148 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2149 uinfo->count = 1;
2150 uinfo->value.enumerated.items =
2151 snd_card_asihpi_mux_count_sources(kcontrol);
2152
2153 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2154 uinfo->value.enumerated.item =
2155 uinfo->value.enumerated.items - 1;
2156
2157 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002158 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002159 uinfo->value.enumerated.item,
2160 &src_node_type, &src_node_index);
2161
2162 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002163 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002164 src_node_index);
2165 return 0;
2166}
2167
2168static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2169 struct snd_ctl_elem_value *ucontrol)
2170{
2171 u32 h_control = kcontrol->private_value;
2172 u16 source_type, source_index;
2173 u16 src_node_type, src_node_index;
2174 int s;
2175
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002176 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002177 &source_type, &source_index));
2178 /* Should cache this search result! */
2179 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002180 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002181 &src_node_type, &src_node_index))
2182 break;
2183
2184 if ((source_type == src_node_type)
2185 && (source_index == src_node_index)) {
2186 ucontrol->value.enumerated.item[0] = s;
2187 return 0;
2188 }
2189 }
2190 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002191 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002192 h_control, source_type, source_index);
2193 ucontrol->value.enumerated.item[0] = 0;
2194 return 0;
2195}
2196
2197static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2198 struct snd_ctl_elem_value *ucontrol)
2199{
2200 int change;
2201 u32 h_control = kcontrol->private_value;
2202 u16 source_type, source_index;
2203 u16 e;
2204
2205 change = 1;
2206
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002207 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002208 ucontrol->value.enumerated.item[0],
2209 &source_type, &source_index);
2210 if (!e)
2211 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002212 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002213 source_type, source_index));
2214 return change;
2215}
2216
2217
2218static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2219 struct hpi_control *hpi_ctl)
2220{
2221 struct snd_card *card = asihpi->card;
2222 struct snd_kcontrol_new snd_control;
2223
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002224 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002225 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2226 snd_control.info = snd_asihpi_mux_info;
2227 snd_control.get = snd_asihpi_mux_get;
2228 snd_control.put = snd_asihpi_mux_put;
2229
2230 return ctl_add(card, &snd_control, asihpi);
2231
2232}
2233
2234/*------------------------------------------------------------
2235 Channel mode controls
2236 ------------------------------------------------------------*/
2237static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2238 struct snd_ctl_elem_info *uinfo)
2239{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002240 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2241 "invalid",
2242 "Normal", "Swap",
2243 "From Left", "From Right",
2244 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002245 };
2246
2247 u32 h_control = kcontrol->private_value;
2248 u16 mode;
2249 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002250 u16 mode_map[6];
2251 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002252
2253 /* HPI channel mode values can be from 1 to 6
2254 Some adapters only support a contiguous subset
2255 */
2256 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002257 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002258 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002259 mode_map[valid_modes] = mode;
2260 valid_modes++;
2261 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002262
2263 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2264 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002265 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002266
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002267 if (uinfo->value.enumerated.item >= valid_modes)
2268 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002269
2270 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002271 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002272
2273 return 0;
2274}
2275
2276static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2277 struct snd_ctl_elem_value *ucontrol)
2278{
2279 u32 h_control = kcontrol->private_value;
2280 u16 mode;
2281
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002282 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002283 mode = 1;
2284
2285 ucontrol->value.enumerated.item[0] = mode - 1;
2286
2287 return 0;
2288}
2289
2290static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2291 struct snd_ctl_elem_value *ucontrol)
2292{
2293 int change;
2294 u32 h_control = kcontrol->private_value;
2295
2296 change = 1;
2297
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002298 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002299 ucontrol->value.enumerated.item[0] + 1));
2300 return change;
2301}
2302
2303
2304static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2305 struct hpi_control *hpi_ctl)
2306{
2307 struct snd_card *card = asihpi->card;
2308 struct snd_kcontrol_new snd_control;
2309
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002310 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002311 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2312 snd_control.info = snd_asihpi_cmode_info;
2313 snd_control.get = snd_asihpi_cmode_get;
2314 snd_control.put = snd_asihpi_cmode_put;
2315
2316 return ctl_add(card, &snd_control, asihpi);
2317}
2318
2319/*------------------------------------------------------------
2320 Sampleclock source controls
2321 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002322static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2323 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2324 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2325 "Prev Module",
2326 "Digital2", "Digital3", "Digital4", "Digital5",
2327 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002328
2329static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2330 struct snd_ctl_elem_info *uinfo)
2331{
2332 struct snd_card_asihpi *asihpi =
2333 (struct snd_card_asihpi *)(kcontrol->private_data);
2334 struct clk_cache *clkcache = &asihpi->cc;
2335 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2336 uinfo->count = 1;
2337 uinfo->value.enumerated.items = clkcache->count;
2338
2339 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2340 uinfo->value.enumerated.item =
2341 uinfo->value.enumerated.items - 1;
2342
2343 strcpy(uinfo->value.enumerated.name,
2344 clkcache->s[uinfo->value.enumerated.item].name);
2345 return 0;
2346}
2347
2348static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2349 struct snd_ctl_elem_value *ucontrol)
2350{
2351 struct snd_card_asihpi *asihpi =
2352 (struct snd_card_asihpi *)(kcontrol->private_data);
2353 struct clk_cache *clkcache = &asihpi->cc;
2354 u32 h_control = kcontrol->private_value;
2355 u16 source, srcindex = 0;
2356 int i;
2357
2358 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002359 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002360 source = 0;
2361
2362 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002363 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002364 srcindex = 0;
2365
2366 for (i = 0; i < clkcache->count; i++)
2367 if ((clkcache->s[i].source == source) &&
2368 (clkcache->s[i].index == srcindex))
2369 break;
2370
2371 ucontrol->value.enumerated.item[0] = i;
2372
2373 return 0;
2374}
2375
2376static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2377 struct snd_ctl_elem_value *ucontrol)
2378{
2379 struct snd_card_asihpi *asihpi =
2380 (struct snd_card_asihpi *)(kcontrol->private_data);
2381 struct clk_cache *clkcache = &asihpi->cc;
2382 int change, item;
2383 u32 h_control = kcontrol->private_value;
2384
2385 change = 1;
2386 item = ucontrol->value.enumerated.item[0];
2387 if (item >= clkcache->count)
2388 item = clkcache->count-1;
2389
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002390 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002391 h_control, clkcache->s[item].source));
2392
2393 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002394 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002395 h_control, clkcache->s[item].index));
2396 return change;
2397}
2398
2399/*------------------------------------------------------------
2400 Clkrate controls
2401 ------------------------------------------------------------*/
2402/* Need to change this to enumerated control with list of rates */
2403static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2404 struct snd_ctl_elem_info *uinfo)
2405{
2406 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2407 uinfo->count = 1;
2408 uinfo->value.integer.min = 8000;
2409 uinfo->value.integer.max = 192000;
2410 uinfo->value.integer.step = 100;
2411
2412 return 0;
2413}
2414
2415static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2417{
2418 u32 h_control = kcontrol->private_value;
2419 u32 rate;
2420 u16 e;
2421
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002422 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002423 if (!e)
2424 ucontrol->value.integer.value[0] = rate;
2425 else
2426 ucontrol->value.integer.value[0] = 0;
2427 return 0;
2428}
2429
2430static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2431 struct snd_ctl_elem_value *ucontrol)
2432{
2433 int change;
2434 u32 h_control = kcontrol->private_value;
2435
2436 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2437 asihpi->mixer_clkrate[addr][1] != right;
2438 */
2439 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002440 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002441 ucontrol->value.integer.value[0]));
2442 return change;
2443}
2444
2445static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2446 struct snd_ctl_elem_info *uinfo)
2447{
2448 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2449 uinfo->count = 1;
2450 uinfo->value.integer.min = 8000;
2451 uinfo->value.integer.max = 192000;
2452 uinfo->value.integer.step = 100;
2453
2454 return 0;
2455}
2456
2457static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_value *ucontrol)
2459{
2460 u32 h_control = kcontrol->private_value;
2461 u32 rate;
2462 u16 e;
2463
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002464 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002465 if (!e)
2466 ucontrol->value.integer.value[0] = rate;
2467 else
2468 ucontrol->value.integer.value[0] = 0;
2469 return 0;
2470}
2471
2472static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2473 struct hpi_control *hpi_ctl)
2474{
2475 struct snd_card *card = asihpi->card;
2476 struct snd_kcontrol_new snd_control;
2477
2478 struct clk_cache *clkcache = &asihpi->cc;
2479 u32 hSC = hpi_ctl->h_control;
2480 int has_aes_in = 0;
2481 int i, j;
2482 u16 source;
2483
2484 snd_control.private_value = hpi_ctl->h_control;
2485
2486 clkcache->has_local = 0;
2487
2488 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002489 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002490 i, &source))
2491 break;
2492 clkcache->s[i].source = source;
2493 clkcache->s[i].index = 0;
2494 clkcache->s[i].name = sampleclock_sources[source];
2495 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2496 has_aes_in = 1;
2497 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2498 clkcache->has_local = 1;
2499 }
2500 if (has_aes_in)
2501 /* already will have picked up index 0 above */
2502 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002503 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002504 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2505 &source))
2506 break;
2507 clkcache->s[i].source =
2508 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2509 clkcache->s[i].index = j;
2510 clkcache->s[i].name = sampleclock_sources[
2511 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2512 i++;
2513 }
2514 clkcache->count = i;
2515
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002516 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002517 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2518 snd_control.info = snd_asihpi_clksrc_info;
2519 snd_control.get = snd_asihpi_clksrc_get;
2520 snd_control.put = snd_asihpi_clksrc_put;
2521 if (ctl_add(card, &snd_control, asihpi) < 0)
2522 return -EINVAL;
2523
2524
2525 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002526 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002527 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2528 snd_control.info = snd_asihpi_clklocal_info;
2529 snd_control.get = snd_asihpi_clklocal_get;
2530 snd_control.put = snd_asihpi_clklocal_put;
2531
2532
2533 if (ctl_add(card, &snd_control, asihpi) < 0)
2534 return -EINVAL;
2535 }
2536
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002537 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002538 snd_control.access =
2539 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2540 snd_control.info = snd_asihpi_clkrate_info;
2541 snd_control.get = snd_asihpi_clkrate_get;
2542
2543 return ctl_add(card, &snd_control, asihpi);
2544}
2545/*------------------------------------------------------------
2546 Mixer
2547 ------------------------------------------------------------*/
2548
2549static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2550{
2551 struct snd_card *card = asihpi->card;
2552 unsigned int idx = 0;
2553 unsigned int subindex = 0;
2554 int err;
2555 struct hpi_control hpi_ctl, prev_ctl;
2556
2557 if (snd_BUG_ON(!asihpi))
2558 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002559 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002560
2561 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002562 hpi_mixer_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002563 &asihpi->h_mixer);
2564 hpi_handle_error(err);
2565 if (err)
2566 return -err;
2567
Takashi Iwai21896bc2010-06-02 12:08:37 +02002568 memset(&prev_ctl, 0, sizeof(prev_ctl));
2569 prev_ctl.control_type = -1;
2570
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002571 for (idx = 0; idx < 2000; idx++) {
2572 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002573 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002574 idx,
2575 &hpi_ctl.src_node_type,
2576 &hpi_ctl.src_node_index,
2577 &hpi_ctl.dst_node_type,
2578 &hpi_ctl.dst_node_index,
2579 &hpi_ctl.control_type,
2580 &hpi_ctl.h_control);
2581 if (err) {
2582 if (err == HPI_ERROR_CONTROL_DISABLED) {
2583 if (mixer_dump)
2584 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002585 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002586 idx);
2587 continue;
2588 } else
2589 break;
2590
2591 }
2592
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002593 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2594 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002595
2596 /* ASI50xx in SSX mode has multiple meters on the same node.
2597 Use subindex to create distinct ALSA controls
2598 for any duplicated controls.
2599 */
2600 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2601 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2602 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2603 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2604 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2605 subindex++;
2606 else
2607 subindex = 0;
2608
2609 prev_ctl = hpi_ctl;
2610
2611 switch (hpi_ctl.control_type) {
2612 case HPI_CONTROL_VOLUME:
2613 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2614 break;
2615 case HPI_CONTROL_LEVEL:
2616 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2617 break;
2618 case HPI_CONTROL_MULTIPLEXER:
2619 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2620 break;
2621 case HPI_CONTROL_CHANNEL_MODE:
2622 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2623 break;
2624 case HPI_CONTROL_METER:
2625 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2626 break;
2627 case HPI_CONTROL_SAMPLECLOCK:
2628 err = snd_asihpi_sampleclock_add(
2629 asihpi, &hpi_ctl);
2630 break;
2631 case HPI_CONTROL_CONNECTION: /* ignore these */
2632 continue;
2633 case HPI_CONTROL_TUNER:
2634 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2635 break;
2636 case HPI_CONTROL_AESEBU_TRANSMITTER:
2637 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2638 break;
2639 case HPI_CONTROL_AESEBU_RECEIVER:
2640 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2641 break;
2642 case HPI_CONTROL_VOX:
2643 case HPI_CONTROL_BITSTREAM:
2644 case HPI_CONTROL_MICROPHONE:
2645 case HPI_CONTROL_PARAMETRIC_EQ:
2646 case HPI_CONTROL_COMPANDER:
2647 default:
2648 if (mixer_dump)
2649 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002650 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002651 "(%d) %d %d %d %d %d\n",
2652 idx,
2653 hpi_ctl.control_type,
2654 hpi_ctl.src_node_type,
2655 hpi_ctl.src_node_index,
2656 hpi_ctl.dst_node_type,
2657 hpi_ctl.dst_node_index);
2658 continue;
2659 };
2660 if (err < 0)
2661 return err;
2662 }
2663 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2664 hpi_handle_error(err);
2665
2666 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2667
2668 return 0;
2669}
2670
2671/*------------------------------------------------------------
2672 /proc interface
2673 ------------------------------------------------------------*/
2674
2675static void
2676snd_asihpi_proc_read(struct snd_info_entry *entry,
2677 struct snd_info_buffer *buffer)
2678{
2679 struct snd_card_asihpi *asihpi = entry->private_data;
2680 u16 version;
2681 u32 h_control;
2682 u32 rate = 0;
2683 u16 source = 0;
2684 int err;
2685
2686 snd_iprintf(buffer, "ASIHPI driver proc file\n");
2687 snd_iprintf(buffer,
2688 "adapter ID=%4X\n_index=%d\n"
2689 "num_outstreams=%d\n_num_instreams=%d\n",
2690 asihpi->type, asihpi->adapter_index,
2691 asihpi->num_outstreams, asihpi->num_instreams);
2692
2693 version = asihpi->version;
2694 snd_iprintf(buffer,
2695 "serial#=%d\n_hw version %c%d\nDSP code version %03d\n",
2696 asihpi->serial_number, ((version >> 3) & 0xf) + 'A',
2697 version & 0x7,
2698 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2699
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002700 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002701 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2702 HPI_CONTROL_SAMPLECLOCK, &h_control);
2703
2704 if (!err) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002705 err = hpi_sample_clock_get_sample_rate(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002706 h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002707 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002708
2709 if (!err)
2710 snd_iprintf(buffer, "sample_clock=%d_hz, source %s\n",
2711 rate, sampleclock_sources[source]);
2712 }
2713
2714}
2715
2716
2717static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2718{
2719 struct snd_info_entry *entry;
2720
2721 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2722 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2723}
2724
2725/*------------------------------------------------------------
2726 HWDEP
2727 ------------------------------------------------------------*/
2728
2729static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2730{
2731 if (enable_hpi_hwdep)
2732 return 0;
2733 else
2734 return -ENODEV;
2735
2736}
2737
2738static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2739{
2740 if (enable_hpi_hwdep)
2741 return asihpi_hpi_release(file);
2742 else
2743 return -ENODEV;
2744}
2745
2746static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2747 unsigned int cmd, unsigned long arg)
2748{
2749 if (enable_hpi_hwdep)
2750 return asihpi_hpi_ioctl(file, cmd, arg);
2751 else
2752 return -ENODEV;
2753}
2754
2755
2756/* results in /dev/snd/hwC#D0 file for each card with index #
2757 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2758*/
2759static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2760 int device, struct snd_hwdep **rhwdep)
2761{
2762 struct snd_hwdep *hw;
2763 int err;
2764
2765 if (rhwdep)
2766 *rhwdep = NULL;
2767 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2768 if (err < 0)
2769 return err;
2770 strcpy(hw->name, "asihpi (HPI)");
2771 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2772 hw->ops.open = snd_asihpi_hpi_open;
2773 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2774 hw->ops.release = snd_asihpi_hpi_release;
2775 hw->private_data = asihpi;
2776 if (rhwdep)
2777 *rhwdep = hw;
2778 return 0;
2779}
2780
2781/*------------------------------------------------------------
2782 CARD
2783 ------------------------------------------------------------*/
2784static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2785 const struct pci_device_id *pci_id)
2786{
2787 int err;
2788
2789 u16 version;
2790 int pcm_substreams;
2791
2792 struct hpi_adapter *hpi_card;
2793 struct snd_card *card;
2794 struct snd_card_asihpi *asihpi;
2795
2796 u32 h_control;
2797 u32 h_stream;
2798
2799 static int dev;
2800 if (dev >= SNDRV_CARDS)
2801 return -ENODEV;
2802
2803 /* Should this be enable[hpi_card->index] ? */
2804 if (!enable[dev]) {
2805 dev++;
2806 return -ENOENT;
2807 }
2808
2809 err = asihpi_adapter_probe(pci_dev, pci_id);
2810 if (err < 0)
2811 return err;
2812
2813 hpi_card = pci_get_drvdata(pci_dev);
2814 /* first try to give the card the same index as its hardware index */
2815 err = snd_card_create(hpi_card->index,
2816 id[hpi_card->index], THIS_MODULE,
2817 sizeof(struct snd_card_asihpi),
2818 &card);
2819 if (err < 0) {
2820 /* if that fails, try the default index==next available */
2821 err =
2822 snd_card_create(index[dev], id[dev],
2823 THIS_MODULE,
2824 sizeof(struct snd_card_asihpi),
2825 &card);
2826 if (err < 0)
2827 return err;
2828 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002829 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002830 hpi_card->index, card->number);
2831 }
2832
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002833 snd_card_set_dev(card, &pci_dev->dev);
2834
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002835 asihpi = (struct snd_card_asihpi *) card->private_data;
2836 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002837 asihpi->pci = pci_dev;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002838 asihpi->adapter_index = hpi_card->index;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002839 hpi_handle_error(hpi_adapter_get_info(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002840 asihpi->adapter_index,
2841 &asihpi->num_outstreams,
2842 &asihpi->num_instreams,
2843 &asihpi->version,
2844 &asihpi->serial_number, &asihpi->type));
2845
2846 version = asihpi->version;
2847 snd_printk(KERN_INFO "adapter ID=%4X index=%d num_outstreams=%d "
2848 "num_instreams=%d S/N=%d\n"
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002849 "Hw Version %c%d DSP code version %03d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002850 asihpi->type, asihpi->adapter_index,
2851 asihpi->num_outstreams,
2852 asihpi->num_instreams, asihpi->serial_number,
2853 ((version >> 3) & 0xf) + 'A',
2854 version & 0x7,
2855 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2856
2857 pcm_substreams = asihpi->num_outstreams;
2858 if (pcm_substreams < asihpi->num_instreams)
2859 pcm_substreams = asihpi->num_instreams;
2860
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002861 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002862 HPI_ADAPTER_PROPERTY_CAPS1,
2863 NULL, &asihpi->support_grouping);
2864 if (err)
2865 asihpi->support_grouping = 0;
2866
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002867 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002868 HPI_ADAPTER_PROPERTY_CAPS2,
2869 &asihpi->support_mrx, NULL);
2870 if (err)
2871 asihpi->support_mrx = 0;
2872
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002873 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002874 HPI_ADAPTER_PROPERTY_INTERVAL,
2875 NULL, &asihpi->update_interval_frames);
2876 if (err)
2877 asihpi->update_interval_frames = 512;
2878
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002879 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002880 asihpi->update_interval_frames *= 2;
2881
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002882 hpi_handle_error(hpi_instream_open(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002883 0, &h_stream));
2884
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002885 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002886 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002888 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002889
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002890 err = hpi_adapter_get_property(asihpi->adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2892 &asihpi->in_max_chans, &asihpi->out_max_chans);
2893 if (err) {
2894 asihpi->in_max_chans = 2;
2895 asihpi->out_max_chans = 2;
2896 }
2897
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002898 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2899 asihpi->out_min_chans = asihpi->out_max_chans;
2900 asihpi->in_min_chans = asihpi->in_max_chans;
2901 asihpi->support_grouping = 0;
2902 } else {
2903 asihpi->out_min_chans = 1;
2904 asihpi->in_min_chans = 1;
2905 }
2906
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002907 snd_printk(KERN_INFO "has dma:%d, grouping:%d, mrx:%d\n",
2908 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002909 asihpi->support_grouping,
2910 asihpi->support_mrx
2911 );
2912
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002913 err = snd_card_asihpi_pcm_new(asihpi, 0, pcm_substreams);
2914 if (err < 0) {
2915 snd_printk(KERN_ERR "pcm_new failed\n");
2916 goto __nodev;
2917 }
2918 err = snd_card_asihpi_mixer_new(asihpi);
2919 if (err < 0) {
2920 snd_printk(KERN_ERR "mixer_new failed\n");
2921 goto __nodev;
2922 }
2923
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002924 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002925 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2926 HPI_CONTROL_SAMPLECLOCK, &h_control);
2927
2928 if (!err)
2929 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002930 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002931
2932 snd_asihpi_proc_init(asihpi);
2933
2934 /* always create, can be enabled or disabled dynamically
2935 by enable_hwdep module param*/
2936 snd_asihpi_hpi_new(asihpi, 0, NULL);
2937
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002938 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002939
2940 sprintf(card->shortname, "AudioScience ASI%4X", asihpi->type);
2941 sprintf(card->longname, "%s %i",
2942 card->shortname, asihpi->adapter_index);
2943 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002944
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002945 if (!err) {
2946 hpi_card->snd_card_asihpi = card;
2947 dev++;
2948 return 0;
2949 }
2950__nodev:
2951 snd_card_free(card);
2952 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2953 return err;
2954
2955}
2956
2957static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2958{
2959 struct hpi_adapter *hpi_card = pci_get_drvdata(pci_dev);
2960
2961 snd_card_free(hpi_card->snd_card_asihpi);
2962 hpi_card->snd_card_asihpi = NULL;
2963 asihpi_adapter_remove(pci_dev);
2964}
2965
2966static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2967 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2968 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2969 (kernel_ulong_t)HPI_6205},
2970 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2971 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2972 (kernel_ulong_t)HPI_6000},
2973 {0,}
2974};
2975MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2976
2977static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002978 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002979 .id_table = asihpi_pci_tbl,
2980 .probe = snd_asihpi_probe,
2981 .remove = __devexit_p(snd_asihpi_remove),
2982#ifdef CONFIG_PM
2983/* .suspend = snd_asihpi_suspend,
2984 .resume = snd_asihpi_resume, */
2985#endif
2986};
2987
2988static int __init snd_asihpi_init(void)
2989{
2990 asihpi_init();
2991 return pci_register_driver(&driver);
2992}
2993
2994static void __exit snd_asihpi_exit(void)
2995{
2996
2997 pci_unregister_driver(&driver);
2998 asihpi_exit();
2999}
3000
3001module_init(snd_asihpi_init)
3002module_exit(snd_asihpi_exit)
3003