blob: 2402801388c9eccf204e04da073d58b418516451 [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"
Eliot Blennerhassett7036b922011-12-22 13:38:43 +130028#include "hpicmn.h"
29
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020030
31#include <linux/pci.h>
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/wait.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040037#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038#include <sound/core.h>
39#include <sound/control.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/info.h>
43#include <sound/initval.h>
44#include <sound/tlv.h>
45#include <sound/hwdep.h>
46
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020047MODULE_LICENSE("GPL");
48MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
49MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx");
50
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130051#if defined CONFIG_SND_DEBUG_VERBOSE
52/**
53 * snd_printddd - very verbose debug printk
54 * @format: format string
55 *
56 * Works like snd_printk() for debugging purposes.
57 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
58 * Must set snd module debug parameter to 3 to enable at runtime.
59 */
60#define snd_printddd(format, args...) \
61 __snd_printk(3, __FILE__, __LINE__, format, ##args)
62#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120063#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130064#endif
65
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020066static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
67static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103068static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
69static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020070
71module_param_array(index, int, NULL, S_IRUGO);
72MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
73
74module_param_array(id, charp, NULL, S_IRUGO);
75MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
76
77module_param_array(enable, bool, NULL, S_IRUGO);
78MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
79
80module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
81MODULE_PARM_DESC(enable_hpi_hwdep,
82 "ALSA enable HPI hwdep for AudioScience soundcard ");
83
84/* identify driver */
85#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130086static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020087module_param(build_info, charp, S_IRUGO);
88MODULE_PARM_DESC(build_info, "built using headers from kernel source");
89#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130090static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020091module_param(build_info, charp, S_IRUGO);
92MODULE_PARM_DESC(build_info, "built within ALSA source");
93#endif
94
95/* set to 1 to dump every control from adapter to log */
96static const int mixer_dump;
97
98#define DEFAULT_SAMPLERATE 44100
99static int adapter_fs = DEFAULT_SAMPLERATE;
100
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200101/* defaults */
102#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300103#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200104#define BUFFER_BYTES_MAX (512 * 1024)
105
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200106#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
107
108struct clk_source {
109 int source;
110 int index;
111 char *name;
112};
113
114struct clk_cache {
115 int count;
116 int has_local;
117 struct clk_source s[MAX_CLOCKSOURCES];
118};
119
120/* Per card data */
121struct snd_card_asihpi {
122 struct snd_card *card;
123 struct pci_dev *pci;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300124 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200125
126 u32 h_mixer;
127 struct clk_cache cc;
128
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200129 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200130 u16 support_grouping;
131 u16 support_mrx;
132 u16 update_interval_frames;
133 u16 in_max_chans;
134 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300135 u16 in_min_chans;
136 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200137};
138
139/* Per stream data */
140struct snd_card_asihpi_pcm {
141 struct timer_list timer;
142 unsigned int respawn_timer;
143 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300144 unsigned int buffer_bytes;
145 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200146 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300147 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
148 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
149 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200150 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200151 struct snd_pcm_substream *substream;
152 u32 h_stream;
153 struct hpi_format format;
154};
155
156/* universal stream verbs work with out or in stream handles */
157
158/* Functions to allow driver to give a buffer to HPI for busmastering */
159
160static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200161 u32 h_stream, /* handle to outstream. */
162 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
163 u32 pci_address
164)
165{
166 struct hpi_message hm;
167 struct hpi_response hr;
168 unsigned int obj = hpi_handle_object(h_stream);
169
170 if (!h_stream)
171 return HPI_ERROR_INVALID_OBJ;
172 hpi_init_message_response(&hm, &hr, obj,
173 obj == HPI_OBJ_OSTREAM ?
174 HPI_OSTREAM_HOSTBUFFER_ALLOC :
175 HPI_ISTREAM_HOSTBUFFER_ALLOC);
176
177 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
178 &hm.obj_index);
179
180 hm.u.d.u.buffer.buffer_size = size_in_bytes;
181 hm.u.d.u.buffer.pci_address = pci_address;
182 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
183 hpi_send_recv(&hm, &hr);
184 return hr.error;
185}
186
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300187static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200188{
189 struct hpi_message hm;
190 struct hpi_response hr;
191 unsigned int obj = hpi_handle_object(h_stream);
192
193 if (!h_stream)
194 return HPI_ERROR_INVALID_OBJ;
195
196 hpi_init_message_response(&hm, &hr, obj,
197 obj == HPI_OBJ_OSTREAM ?
198 HPI_OSTREAM_HOSTBUFFER_FREE :
199 HPI_ISTREAM_HOSTBUFFER_FREE);
200
201 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
202 &hm.obj_index);
203 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
204 hpi_send_recv(&hm, &hr);
205 return hr.error;
206}
207
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300208static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200209{
210 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300211 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200212 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300213 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200214}
215
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300216static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200217{
218 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300221 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200222}
223
224static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225 u32 h_stream,
226 u16 *pw_state,
227 u32 *pbuffer_size,
228 u32 *pdata_in_buffer,
229 u32 *psample_count,
230 u32 *pauxiliary_data
231)
232{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300233 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300235 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236 pbuffer_size, pdata_in_buffer,
237 psample_count, pauxiliary_data);
238 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300239 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200240 pbuffer_size, pdata_in_buffer,
241 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300242 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200243}
244
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300245static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200246 u32 h_master,
247 u32 h_stream)
248{
249 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300250 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300252 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253}
254
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300255static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200256{
257 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300260 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261}
262
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264 u32 h_stream, u32 *mo, u32 *mi)
265{
266 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270}
271
272static u16 handle_error(u16 err, int line, char *filename)
273{
274 if (err)
275 printk(KERN_WARNING
276 "in file %s, line %d: HPI error %d\n",
277 filename, line, err);
278 return err;
279}
280
281#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
282
283/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200284
285static void print_hwparams(struct snd_pcm_substream *substream,
286 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200287{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200288 char name[16];
289 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200290 snd_printd("%s HWPARAMS\n", name);
291 snd_printd(" samplerate %d Hz\n", params_rate(p));
292 snd_printd(" channels %d\n", params_channels(p));
293 snd_printd(" format %d\n", params_format(p));
294 snd_printd(" subformat %d\n", params_subformat(p));
295 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
296 snd_printd(" period %d B\n", params_period_bytes(p));
297 snd_printd(" access %d\n", params_access(p));
298 snd_printd(" period_size %d\n", params_period_size(p));
299 snd_printd(" periods %d\n", params_periods(p));
300 snd_printd(" buffer_size %d\n", params_buffer_size(p));
301 snd_printd(" %d B/s\n", params_rate(p) *
302 params_channels(p) *
303 snd_pcm_format_width(params_format(p)) / 8);
304
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200305}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200306
307static snd_pcm_format_t hpi_to_alsa_formats[] = {
308 -1, /* INVALID */
309 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
310 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
311 -1, /* HPI_FORMAT_MPEG_L1 3 */
312 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
313 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
314 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
315 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
316 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
317 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
318 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
319 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
320 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
321 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
322 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
323#if 1
324 /* ALSA can't handle 3 byte sample size together with power-of-2
325 * constraint on buffer_bytes, so disable this format
326 */
327 -1
328#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300329 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200330#endif
331};
332
333
334static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
335 u16 *hpi_format)
336{
337 u16 format;
338
339 for (format = HPI_FORMAT_PCM8_UNSIGNED;
340 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
341 if (hpi_to_alsa_formats[format] == alsa_format) {
342 *hpi_format = format;
343 return 0;
344 }
345 }
346
347 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
348 alsa_format);
349 *hpi_format = 0;
350 return -EINVAL;
351}
352
353static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
354 struct snd_pcm_hardware *pcmhw)
355{
356 u16 err;
357 u32 h_control;
358 u32 sample_rate;
359 int idx;
360 unsigned int rate_min = 200000;
361 unsigned int rate_max = 0;
362 unsigned int rates = 0;
363
364 if (asihpi->support_mrx) {
365 rates |= SNDRV_PCM_RATE_CONTINUOUS;
366 rates |= SNDRV_PCM_RATE_8000_96000;
367 rate_min = 8000;
368 rate_max = 100000;
369 } else {
370 /* on cards without SRC,
371 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300372 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200373 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
374 HPI_CONTROL_SAMPLECLOCK, &h_control);
375 if (err) {
376 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300377 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200378 }
379
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300380 for (idx = -1; idx < 100; idx++) {
381 if (idx == -1) {
382 if (hpi_sample_clock_get_sample_rate(h_control,
383 &sample_rate))
384 continue;
385 } else if (hpi_sample_clock_query_local_rate(h_control,
386 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200387 break;
388 }
389
390 rate_min = min(rate_min, sample_rate);
391 rate_max = max(rate_max, sample_rate);
392
393 switch (sample_rate) {
394 case 5512:
395 rates |= SNDRV_PCM_RATE_5512;
396 break;
397 case 8000:
398 rates |= SNDRV_PCM_RATE_8000;
399 break;
400 case 11025:
401 rates |= SNDRV_PCM_RATE_11025;
402 break;
403 case 16000:
404 rates |= SNDRV_PCM_RATE_16000;
405 break;
406 case 22050:
407 rates |= SNDRV_PCM_RATE_22050;
408 break;
409 case 32000:
410 rates |= SNDRV_PCM_RATE_32000;
411 break;
412 case 44100:
413 rates |= SNDRV_PCM_RATE_44100;
414 break;
415 case 48000:
416 rates |= SNDRV_PCM_RATE_48000;
417 break;
418 case 64000:
419 rates |= SNDRV_PCM_RATE_64000;
420 break;
421 case 88200:
422 rates |= SNDRV_PCM_RATE_88200;
423 break;
424 case 96000:
425 rates |= SNDRV_PCM_RATE_96000;
426 break;
427 case 176400:
428 rates |= SNDRV_PCM_RATE_176400;
429 break;
430 case 192000:
431 rates |= SNDRV_PCM_RATE_192000;
432 break;
433 default: /* some other rate */
434 rates |= SNDRV_PCM_RATE_KNOT;
435 }
436 }
437 }
438
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200439 pcmhw->rates = rates;
440 pcmhw->rate_min = rate_min;
441 pcmhw->rate_max = rate_max;
442}
443
444static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *params)
446{
447 struct snd_pcm_runtime *runtime = substream->runtime;
448 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
449 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
450 int err;
451 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400452 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200453 unsigned int bytes_per_sec;
454
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200455 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200456 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
457 if (err < 0)
458 return err;
459 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
460 if (err)
461 return err;
462
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200463 hpi_handle_error(hpi_format_create(&dpcm->format,
464 params_channels(params),
465 format, params_rate(params), 0, 0));
466
467 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300468 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200469 return -EINVAL;
470
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300471 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 dpcm->h_stream, &dpcm->format) != 0)
473 return -EINVAL;
474 }
475
476 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200477 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300478 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 params_buffer_bytes(params), runtime->dma_addr);
480 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300481 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200482 "stream_host_buffer_attach succeeded %u %lu\n",
483 params_buffer_bytes(params),
484 (unsigned long)runtime->dma_addr);
485 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300486 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200487 err);
488 return -ENOMEM;
489 }
490
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300491 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200492 &dpcm->hpi_buffer_attached,
493 NULL, NULL, NULL);
494
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300495 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200496 dpcm->hpi_buffer_attached);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300497
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200498 }
499 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400500 width = snd_pcm_format_width(params_format(params));
501 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200502 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400503 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 return -EINVAL;
505
506 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300507 dpcm->buffer_bytes = params_buffer_bytes(params);
508 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200509
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200510 return 0;
511}
512
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300513static int
514snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
515{
516 struct snd_pcm_runtime *runtime = substream->runtime;
517 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
518 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300519 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300520
521 snd_pcm_lib_free_pages(substream);
522 return 0;
523}
524
525static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
526{
527 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
528 kfree(dpcm);
529}
530
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200531static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
532 substream)
533{
534 struct snd_pcm_runtime *runtime = substream->runtime;
535 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
536 int expiry;
537
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300538 expiry = HZ / 200;
539 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300540 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200541 dpcm->timer.expires = jiffies + expiry;
542 dpcm->respawn_timer = 1;
543 add_timer(&dpcm->timer);
544}
545
546static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
547{
548 struct snd_pcm_runtime *runtime = substream->runtime;
549 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
550
551 dpcm->respawn_timer = 0;
552 del_timer(&dpcm->timer);
553}
554
555static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
556 int cmd)
557{
558 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
559 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
560 struct snd_pcm_substream *s;
561 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200562 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200563
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200564 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200565 snd_printdd("%s trigger\n", name);
566
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200567 switch (cmd) {
568 case SNDRV_PCM_TRIGGER_START:
569 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300570 struct snd_pcm_runtime *runtime = s->runtime;
571 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200572
573 if (snd_pcm_substream_chip(s) != card)
574 continue;
575
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300576 /* don't link Cap and Play */
577 if (substream->stream != s->stream)
578 continue;
579
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200580 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200581 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200582 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300583 * in buffer? Must be at least one period!
584 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200585 * buffer is bigger it may contain even more
586 * data??
587 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300588 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300589 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200590 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300591 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300592 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200593 preload,
594 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300595 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200596 }
597
598 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200599 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300600 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200601 dpcm->h_stream,
602 ds->h_stream);
603 if (!e) {
604 snd_pcm_trigger_done(s, substream);
605 } else {
606 hpi_handle_error(e);
607 break;
608 }
609 } else
610 break;
611 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300612 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200613 /* start the master stream */
614 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300615 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200616 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300617 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200618 break;
619
620 case SNDRV_PCM_TRIGGER_STOP:
621 snd_card_asihpi_pcm_timer_stop(substream);
622 snd_pcm_group_for_each_entry(s, substream) {
623 if (snd_pcm_substream_chip(s) != card)
624 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300625 /* don't link Cap and Play */
626 if (substream->stream != s->stream)
627 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200628
629 /*? workaround linked streams don't
630 transition to SETUP 20070706*/
631 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
632
633 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200634 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200635 snd_pcm_trigger_done(s, substream);
636 } else
637 break;
638 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300639 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200640
641 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300642 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200643 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
644 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300645 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200646
647 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300648 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200649 break;
650
651 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300652 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300653 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200654 snd_card_asihpi_pcm_timer_start(substream);
655 break;
656 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300657 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200658 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300659 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660 break;
661 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300662 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200663 return -EINVAL;
664 }
665
666 return 0;
667}
668
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200669/*algorithm outline
670 Without linking degenerates to getting single stream pos etc
671 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
672*/
673/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300674pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200675for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300676 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300677 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300678 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200679}
680timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
681for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300682 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300683 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300685 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300687 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200688 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300689 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690 }
691 }
692 snd_pcm_period_elapsed(s);
693 }
694}
695*/
696
697/** Minimum of 2 modulo values. Works correctly when the difference between
698* the values is less than half the modulus
699*/
700static inline unsigned int modulo_min(unsigned int a, unsigned int b,
701 unsigned long int modulus)
702{
703 unsigned int result;
704 if (((a-b) % modulus) < (modulus/2))
705 result = b;
706 else
707 result = a;
708
709 return result;
710}
711
712/** Timer function, equivalent to interrupt service routine for cards
713*/
714static void snd_card_asihpi_timer_function(unsigned long data)
715{
716 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300717 struct snd_pcm_substream *substream = dpcm->substream;
718 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200719 struct snd_pcm_runtime *runtime;
720 struct snd_pcm_substream *s;
721 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300722 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200723 unsigned int remdata, xfercount, next_jiffies;
724 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300725 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200726 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300727 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200728 char name[16];
729
730 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200731
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200732 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300733
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200734 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300735 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200736 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
737 runtime = s->runtime;
738
739 if (snd_pcm_substream_chip(s) != card)
740 continue;
741
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300742 /* don't link Cap and Play */
743 if (substream->stream != s->stream)
744 continue;
745
746 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200747 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300748 &buffer_size, &bytes_avail,
749 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200750
751 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300752 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200753
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200754 if (!card->can_dma)
755 on_card_bytes = bytes_avail;
756
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300757 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300758 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300759 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300760 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300761 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300762 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200763 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300764 }
765 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300766 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300767 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200768 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300769 if (ds->drained_count > 20) {
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200770 snd_pcm_stop(s, SNDRV_PCM_STATE_XRUN);
771 continue;
772 }
773 } else {
774 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300775 }
776 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300777 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200778
779 if (first) {
780 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300781 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300782 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200783 first = 0;
784 } else {
785 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300786 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200787 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300788 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200789 newdata);
790 }
791
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200792 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200793 (unsigned long)frames_to_bytes(runtime,
794 runtime->status->hw_ptr),
795 (unsigned long)frames_to_bytes(runtime,
796 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300797
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200798 snd_printdd("%d S=%d, "
799 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
800 "aux=0x%04X space=0x%04X\n",
801 s->number, state,
802 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
803 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300804 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300805 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200806 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300807 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200808
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300809 remdata = newdata % dpcm->period_bytes;
810 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300811 /* come back when on_card_bytes has decreased enough to allow
812 write to happen, or when data has been consumed to make another
813 period
814 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300815 if (xfercount && (on_card_bytes > dpcm->period_bytes))
816 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300817 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300818 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300819
820 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200821 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200822 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300823 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
824
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200827
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300828 /* don't link Cap and Play */
829 if (substream->stream != s->stream)
830 continue;
831
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300832 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
833
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200834 if (xfercount &&
835 /* Limit use of on card fifo for playback */
836 ((on_card_bytes <= ds->period_bytes) ||
837 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
838
839 {
840
841 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
842 unsigned int xfer1, xfer2;
843 char *pd = &s->runtime->dma_area[buf_ofs];
844
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200845 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200846 xfer1 = xfercount;
847 xfer2 = 0;
848 } else {
849 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
850 xfer2 = xfercount - xfer1;
851 }
852
853 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
854 snd_printddd("P%d write1 0x%04X 0x%04X\n",
855 s->number, xfer1, buf_ofs);
856 hpi_handle_error(
857 hpi_outstream_write_buf(
858 ds->h_stream, pd, xfer1,
859 &ds->format));
860
861 if (xfer2) {
862 pd = s->runtime->dma_area;
863
864 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200865 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200866 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867 hpi_handle_error(
868 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200869 ds->h_stream, pd,
870 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200871 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200872 }
873 } else {
874 snd_printddd("C%d read1 0x%04x\n",
875 s->number, xfer1);
876 hpi_handle_error(
877 hpi_instream_read_buf(
878 ds->h_stream,
879 pd, xfer1));
880 if (xfer2) {
881 pd = s->runtime->dma_area;
882 snd_printddd("C%d read2 0x%04x\n",
883 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200884 hpi_handle_error(
885 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300886 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200887 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200888 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200889 }
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300890 ds->pcm_buf_host_rw_ofs += xfercount;
891 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200892 snd_pcm_period_elapsed(s);
893 }
894 }
895
896 if (dpcm->respawn_timer)
897 add_timer(&dpcm->timer);
898}
899
900/***************************** PLAYBACK OPS ****************/
901static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
902 unsigned int cmd, void *arg)
903{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300904 char name[16];
905 snd_pcm_debug_name(substream, name, sizeof(name));
906 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200907 return snd_pcm_lib_ioctl(substream, cmd, arg);
908}
909
910static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
911 substream)
912{
913 struct snd_pcm_runtime *runtime = substream->runtime;
914 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
915
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200916 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200917
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300918 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300919 dpcm->pcm_buf_host_rw_ofs = 0;
920 dpcm->pcm_buf_dma_ofs = 0;
921 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200922 return 0;
923}
924
925static snd_pcm_uframes_t
926snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
927{
928 struct snd_pcm_runtime *runtime = substream->runtime;
929 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
930 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300931 char name[16];
932 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200933
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300934 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300935 snd_printddd("%s pointer = 0x%04lx\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200936 return ptr;
937}
938
939static void snd_card_asihpi_playback_format(struct snd_card_asihpi *asihpi,
940 u32 h_stream,
941 struct snd_pcm_hardware *pcmhw)
942{
943 struct hpi_format hpi_format;
944 u16 format;
945 u16 err;
946 u32 h_control;
947 u32 sample_rate = 48000;
948
949 /* on cards without SRC, must query at valid rate,
950 * maybe set by external sync
951 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300952 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200953 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
954 HPI_CONTROL_SAMPLECLOCK, &h_control);
955
956 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300957 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200958 &sample_rate);
959
960 for (format = HPI_FORMAT_PCM8_UNSIGNED;
961 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
962 err = hpi_format_create(&hpi_format,
963 2, format, sample_rate, 128000, 0);
964 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300965 err = hpi_outstream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200966 &hpi_format);
967 if (!err && (hpi_to_alsa_formats[format] != -1))
968 pcmhw->formats |=
969 (1ULL << hpi_to_alsa_formats[format]);
970 }
971}
972
973static struct snd_pcm_hardware snd_card_asihpi_playback = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200974 .buffer_bytes_max = BUFFER_BYTES_MAX,
975 .period_bytes_min = PERIOD_BYTES_MIN,
976 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
977 .periods_min = PERIODS_MIN,
978 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
979 .fifo_size = 0,
980};
981
982static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
983{
984 struct snd_pcm_runtime *runtime = substream->runtime;
985 struct snd_card_asihpi_pcm *dpcm;
986 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
987 int err;
988
989 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
990 if (dpcm == NULL)
991 return -ENOMEM;
992
993 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300994 hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200995 substream->number, &dpcm->h_stream);
996 hpi_handle_error(err);
997 if (err)
998 kfree(dpcm);
999 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1000 return -EBUSY;
1001 if (err)
1002 return -EIO;
1003
1004 /*? also check ASI5000 samplerate source
1005 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001006 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001007 */
1008
1009 init_timer(&dpcm->timer);
1010 dpcm->timer.data = (unsigned long) dpcm;
1011 dpcm->timer.function = snd_card_asihpi_timer_function;
1012 dpcm->substream = substream;
1013 runtime->private_data = dpcm;
1014 runtime->private_free = snd_card_asihpi_runtime_free;
1015
1016 snd_card_asihpi_playback.channels_max = card->out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001017 snd_card_asihpi_playback.channels_min = card->out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001018 /*?snd_card_asihpi_playback.period_bytes_min =
1019 card->out_max_chans * 4096; */
1020
1021 snd_card_asihpi_playback_format(card, dpcm->h_stream,
1022 &snd_card_asihpi_playback);
1023
1024 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1025
1026 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1027 SNDRV_PCM_INFO_DOUBLE |
1028 SNDRV_PCM_INFO_BATCH |
1029 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001030 SNDRV_PCM_INFO_PAUSE |
1031 SNDRV_PCM_INFO_MMAP |
1032 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001033
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001034 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001035 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001036 snd_pcm_set_sync(substream);
1037 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001038
1039 /* struct is copied, so can create initializer dynamically */
1040 runtime->hw = snd_card_asihpi_playback;
1041
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001042 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001043 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1044 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1045 if (err < 0)
1046 return err;
1047
1048 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1049 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001050
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001051 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001052 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001053
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001054 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001055
1056 return 0;
1057}
1058
1059static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1060{
1061 struct snd_pcm_runtime *runtime = substream->runtime;
1062 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1063
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001064 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001065 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001066
1067 return 0;
1068}
1069
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001070static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1071 .open = snd_card_asihpi_playback_open,
1072 .close = snd_card_asihpi_playback_close,
1073 .ioctl = snd_card_asihpi_playback_ioctl,
1074 .hw_params = snd_card_asihpi_pcm_hw_params,
1075 .hw_free = snd_card_asihpi_hw_free,
1076 .prepare = snd_card_asihpi_playback_prepare,
1077 .trigger = snd_card_asihpi_trigger,
1078 .pointer = snd_card_asihpi_playback_pointer,
1079};
1080
1081/***************************** CAPTURE OPS ****************/
1082static snd_pcm_uframes_t
1083snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1084{
1085 struct snd_pcm_runtime *runtime = substream->runtime;
1086 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1087
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001088 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001089 substream->number, dpcm->pcm_buf_dma_ofs);
1090 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001091 for the capture position, because those samples aren't yet in
1092 the local buffer available for reading.
1093 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001094 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001095}
1096
1097static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1098 unsigned int cmd, void *arg)
1099{
1100 return snd_pcm_lib_ioctl(substream, cmd, arg);
1101}
1102
1103static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1104{
1105 struct snd_pcm_runtime *runtime = substream->runtime;
1106 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1107
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001108 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001109 dpcm->pcm_buf_host_rw_ofs = 0;
1110 dpcm->pcm_buf_dma_ofs = 0;
1111 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001112
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001113 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001114 return 0;
1115}
1116
1117
1118
1119static void snd_card_asihpi_capture_format(struct snd_card_asihpi *asihpi,
1120 u32 h_stream,
1121 struct snd_pcm_hardware *pcmhw)
1122{
1123 struct hpi_format hpi_format;
1124 u16 format;
1125 u16 err;
1126 u32 h_control;
1127 u32 sample_rate = 48000;
1128
1129 /* on cards without SRC, must query at valid rate,
1130 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001131 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001132 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1133 HPI_CONTROL_SAMPLECLOCK, &h_control);
1134
1135 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001136 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001137 &sample_rate);
1138
1139 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1140 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1141
1142 err = hpi_format_create(&hpi_format, 2, format,
1143 sample_rate, 128000, 0);
1144 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001145 err = hpi_instream_query_format(h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001146 &hpi_format);
1147 if (!err)
1148 pcmhw->formats |=
1149 (1ULL << hpi_to_alsa_formats[format]);
1150 }
1151}
1152
1153
1154static struct snd_pcm_hardware snd_card_asihpi_capture = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155 .buffer_bytes_max = BUFFER_BYTES_MAX,
1156 .period_bytes_min = PERIOD_BYTES_MIN,
1157 .period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN,
1158 .periods_min = PERIODS_MIN,
1159 .periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN,
1160 .fifo_size = 0,
1161};
1162
1163static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1164{
1165 struct snd_pcm_runtime *runtime = substream->runtime;
1166 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1167 struct snd_card_asihpi_pcm *dpcm;
1168 int err;
1169
1170 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1171 if (dpcm == NULL)
1172 return -ENOMEM;
1173
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001174 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001175 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001176
1177 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001178 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001179 substream->number, &dpcm->h_stream));
1180 if (err)
1181 kfree(dpcm);
1182 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1183 return -EBUSY;
1184 if (err)
1185 return -EIO;
1186
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001187 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 Blennerhassett7036b922011-12-22 13:38:43 +13001243static int __devinit snd_card_asihpi_pcm_new(
1244 struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001245{
1246 struct snd_pcm *pcm;
1247 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001248 u16 num_instreams, num_outstreams, x16;
1249 u32 x32;
1250
1251 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1252 &num_outstreams, &num_instreams,
1253 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001254
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001255 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001256 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001257 if (err < 0)
1258 return err;
1259 /* pointer to ops struct is stored, dont change ops afterwards! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001260 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1261 &snd_card_asihpi_playback_mmap_ops);
1262 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1263 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264
1265 pcm->private_data = asihpi;
1266 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001267 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001268
1269 /*? do we want to emulate MMAP for non-BBM cards?
1270 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1271 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1272 snd_dma_pci_data(asihpi->pci),
1273 64*1024, BUFFER_BYTES_MAX);
1274
1275 return 0;
1276}
1277
1278/***************************** MIXER CONTROLS ****************/
1279struct hpi_control {
1280 u32 h_control;
1281 u16 control_type;
1282 u16 src_node_type;
1283 u16 src_node_index;
1284 u16 dst_node_type;
1285 u16 dst_node_index;
1286 u16 band;
1287 char name[44]; /* copied to snd_ctl_elem_id.name[44]; */
1288};
1289
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001290static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001291 "invalid",
1292 "AM",
1293 "FM mono",
1294 "TV NTSC-M",
1295 "FM stereo",
1296 "AUX",
1297 "TV PAL BG",
1298 "TV PAL I",
1299 "TV PAL DK",
1300 "TV SECAM",
1301};
1302
1303compile_time_assert(
1304 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1305 (HPI_TUNER_BAND_LAST+1)),
1306 assert_tuner_band_names_size);
1307
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001308static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001309 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001310 "PCM",
1311 "Line",
1312 "Digital",
1313 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001314 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001315 "Clock",
1316 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001317 "Mic",
1318 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001319 "Analog",
1320 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001321 "RTP",
Eliot Blennerhassett502f2712011-12-22 13:38:39 +13001322 "Internal"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001324
1325compile_time_assert(
1326 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001327 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001328 assert_src_names_size);
1329
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001330static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001331 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001332 "PCM",
1333 "Line",
1334 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001336 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001337 "Net",
1338 "Analog",
1339 "RTP",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001340};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001341
1342compile_time_assert(
1343 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001344 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001345 assert_dst_names_size);
1346
1347static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1348 struct snd_card_asihpi *asihpi)
1349{
1350 int err;
1351
1352 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1353 if (err < 0)
1354 return err;
1355 else if (mixer_dump)
1356 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1357
1358 return 0;
1359}
1360
1361/* Convert HPI control name and location into ALSA control name */
1362static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1363 struct hpi_control *hpi_ctl,
1364 char *name)
1365{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001366 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001367 memset(snd_control, 0, sizeof(*snd_control));
1368 snd_control->name = hpi_ctl->name;
1369 snd_control->private_value = hpi_ctl->h_control;
1370 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1371 snd_control->index = 0;
1372
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001373 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1374 dir = ""; /* clock is neither capture nor playback */
1375 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001376 dir = "Capture "; /* On or towards a PCM capture destination*/
1377 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1378 (!hpi_ctl->dst_node_type))
1379 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001380 else if (hpi_ctl->src_node_type &&
1381 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001382 (hpi_ctl->dst_node_type))
1383 dir = "Monitor Playback "; /* Between an input and an output */
1384 else
1385 dir = "Playback "; /* PCM Playback source, or output node */
1386
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001387 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001388 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001389 asihpi_src_names[hpi_ctl->src_node_type],
1390 hpi_ctl->src_node_index,
1391 asihpi_dst_names[hpi_ctl->dst_node_type],
1392 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001393 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396 asihpi_dst_names[hpi_ctl->dst_node_type],
1397 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001398 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001399 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001400 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401 asihpi_src_names[hpi_ctl->src_node_type],
1402 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001403 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001404 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001405 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1406 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407}
1408
1409/*------------------------------------------------------------
1410 Volume controls
1411 ------------------------------------------------------------*/
1412#define VOL_STEP_mB 1
1413static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1414 struct snd_ctl_elem_info *uinfo)
1415{
1416 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001417 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001418 u16 err;
1419 /* native gains are in millibels */
1420 short min_gain_mB;
1421 short max_gain_mB;
1422 short step_gain_mB;
1423
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001424 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001425 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1426 if (err) {
1427 max_gain_mB = 0;
1428 min_gain_mB = -10000;
1429 step_gain_mB = VOL_STEP_mB;
1430 }
1431
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001432 err = hpi_meter_query_channels(h_control, &count);
1433 if (err)
1434 count = HPI_MAX_CHANNELS;
1435
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001436 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001437 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001438 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1439 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1440 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1441 return 0;
1442}
1443
1444static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1445 struct snd_ctl_elem_value *ucontrol)
1446{
1447 u32 h_control = kcontrol->private_value;
1448 short an_gain_mB[HPI_MAX_CHANNELS];
1449
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001450 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001451 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1452 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1453
1454 return 0;
1455}
1456
1457static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1458 struct snd_ctl_elem_value *ucontrol)
1459{
1460 int change;
1461 u32 h_control = kcontrol->private_value;
1462 short an_gain_mB[HPI_MAX_CHANNELS];
1463
1464 an_gain_mB[0] =
1465 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1466 an_gain_mB[1] =
1467 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1468 /* change = asihpi->mixer_volume[addr][0] != left ||
1469 asihpi->mixer_volume[addr][1] != right;
1470 */
1471 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001472 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001473 return change;
1474}
1475
1476static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1477
Takashi Iwai000477a2011-07-22 07:57:44 +02001478#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001479
1480static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1481 struct snd_ctl_elem_value *ucontrol)
1482{
1483 u32 h_control = kcontrol->private_value;
1484 u32 mute;
1485
1486 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1487 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1488
1489 return 0;
1490}
1491
1492static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1493 struct snd_ctl_elem_value *ucontrol)
1494{
1495 u32 h_control = kcontrol->private_value;
1496 int change = 1;
1497 /* HPI currently only supports all or none muting of multichannel volume
1498 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1499 */
1500 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1501 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1502 return change;
1503}
1504
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001505static int __devinit snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1506 struct hpi_control *hpi_ctl)
1507{
1508 struct snd_card *card = asihpi->card;
1509 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001510 int err;
1511 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001512
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001513 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001514 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1515 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1516 snd_control.info = snd_asihpi_volume_info;
1517 snd_control.get = snd_asihpi_volume_get;
1518 snd_control.put = snd_asihpi_volume_put;
1519 snd_control.tlv.p = db_scale_100;
1520
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001521 err = ctl_add(card, &snd_control, asihpi);
1522 if (err)
1523 return err;
1524
1525 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1526 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1527 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1528 snd_control.info = snd_asihpi_volume_mute_info;
1529 snd_control.get = snd_asihpi_volume_mute_get;
1530 snd_control.put = snd_asihpi_volume_mute_put;
1531 err = ctl_add(card, &snd_control, asihpi);
1532 }
1533 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001534}
1535
1536/*------------------------------------------------------------
1537 Level controls
1538 ------------------------------------------------------------*/
1539static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1540 struct snd_ctl_elem_info *uinfo)
1541{
1542 u32 h_control = kcontrol->private_value;
1543 u16 err;
1544 short min_gain_mB;
1545 short max_gain_mB;
1546 short step_gain_mB;
1547
1548 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001549 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001550 &max_gain_mB, &step_gain_mB);
1551 if (err) {
1552 max_gain_mB = 2400;
1553 min_gain_mB = -1000;
1554 step_gain_mB = 100;
1555 }
1556
1557 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1558 uinfo->count = 2;
1559 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1560 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1561 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1562 return 0;
1563}
1564
1565static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 u32 h_control = kcontrol->private_value;
1569 short an_gain_mB[HPI_MAX_CHANNELS];
1570
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001571 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001572 ucontrol->value.integer.value[0] =
1573 an_gain_mB[0] / HPI_UNITS_PER_dB;
1574 ucontrol->value.integer.value[1] =
1575 an_gain_mB[1] / HPI_UNITS_PER_dB;
1576
1577 return 0;
1578}
1579
1580static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_value *ucontrol)
1582{
1583 int change;
1584 u32 h_control = kcontrol->private_value;
1585 short an_gain_mB[HPI_MAX_CHANNELS];
1586
1587 an_gain_mB[0] =
1588 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1589 an_gain_mB[1] =
1590 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1591 /* change = asihpi->mixer_level[addr][0] != left ||
1592 asihpi->mixer_level[addr][1] != right;
1593 */
1594 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001595 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001596 return change;
1597}
1598
1599static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1600
1601static int __devinit snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1602 struct hpi_control *hpi_ctl)
1603{
1604 struct snd_card *card = asihpi->card;
1605 struct snd_kcontrol_new snd_control;
1606
1607 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001608 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001609 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1610 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1611 snd_control.info = snd_asihpi_level_info;
1612 snd_control.get = snd_asihpi_level_get;
1613 snd_control.put = snd_asihpi_level_put;
1614 snd_control.tlv.p = db_scale_level;
1615
1616 return ctl_add(card, &snd_control, asihpi);
1617}
1618
1619/*------------------------------------------------------------
1620 AESEBU controls
1621 ------------------------------------------------------------*/
1622
1623/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001624static const char * const asihpi_aesebu_format_names[] = {
1625 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001626
1627static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_info *uinfo)
1629{
1630 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1631 uinfo->count = 1;
1632 uinfo->value.enumerated.items = 3;
1633
1634 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
1635 uinfo->value.enumerated.item =
1636 uinfo->value.enumerated.items - 1;
1637
1638 strcpy(uinfo->value.enumerated.name,
1639 asihpi_aesebu_format_names[uinfo->value.enumerated.item]);
1640
1641 return 0;
1642}
1643
1644static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1645 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001646 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001647{
1648 u32 h_control = kcontrol->private_value;
1649 u16 source, err;
1650
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001651 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001652
1653 /* default to N/A */
1654 ucontrol->value.enumerated.item[0] = 0;
1655 /* return success but set the control to N/A */
1656 if (err)
1657 return 0;
1658 if (source == HPI_AESEBU_FORMAT_SPDIF)
1659 ucontrol->value.enumerated.item[0] = 1;
1660 if (source == HPI_AESEBU_FORMAT_AESEBU)
1661 ucontrol->value.enumerated.item[0] = 2;
1662
1663 return 0;
1664}
1665
1666static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1667 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001668 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001669{
1670 u32 h_control = kcontrol->private_value;
1671
1672 /* default to S/PDIF */
1673 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1674
1675 if (ucontrol->value.enumerated.item[0] == 1)
1676 source = HPI_AESEBU_FORMAT_SPDIF;
1677 if (ucontrol->value.enumerated.item[0] == 2)
1678 source = HPI_AESEBU_FORMAT_AESEBU;
1679
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001680 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001681 return -EINVAL;
1682
1683 return 1;
1684}
1685
1686static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_value *ucontrol) {
1688 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001689 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001690}
1691
1692static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol) {
1694 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001695 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001696}
1697
1698static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1699 struct snd_ctl_elem_info *uinfo)
1700{
1701 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1702 uinfo->count = 1;
1703
1704 uinfo->value.integer.min = 0;
1705 uinfo->value.integer.max = 0X1F;
1706 uinfo->value.integer.step = 1;
1707
1708 return 0;
1709}
1710
1711static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1712 struct snd_ctl_elem_value *ucontrol) {
1713
1714 u32 h_control = kcontrol->private_value;
1715 u16 status;
1716
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001717 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1718 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001719 ucontrol->value.integer.value[0] = status;
1720 return 0;
1721}
1722
1723static int __devinit snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1724 struct hpi_control *hpi_ctl)
1725{
1726 struct snd_card *card = asihpi->card;
1727 struct snd_kcontrol_new snd_control;
1728
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001729 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001730 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1731 snd_control.info = snd_asihpi_aesebu_format_info;
1732 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1733 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1734
1735
1736 if (ctl_add(card, &snd_control, asihpi) < 0)
1737 return -EINVAL;
1738
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001739 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001740 snd_control.access =
1741 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1742 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1743 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1744
1745 return ctl_add(card, &snd_control, asihpi);
1746}
1747
1748static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *ucontrol) {
1750 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001751 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001752}
1753
1754static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1755 struct snd_ctl_elem_value *ucontrol) {
1756 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001757 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001758}
1759
1760
1761static int __devinit snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1762 struct hpi_control *hpi_ctl)
1763{
1764 struct snd_card *card = asihpi->card;
1765 struct snd_kcontrol_new snd_control;
1766
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001767 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001768 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1769 snd_control.info = snd_asihpi_aesebu_format_info;
1770 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1771 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1772
1773 return ctl_add(card, &snd_control, asihpi);
1774}
1775
1776/*------------------------------------------------------------
1777 Tuner controls
1778 ------------------------------------------------------------*/
1779
1780/* Gain */
1781
1782static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1783 struct snd_ctl_elem_info *uinfo)
1784{
1785 u32 h_control = kcontrol->private_value;
1786 u16 err;
1787 short idx;
1788 u16 gain_range[3];
1789
1790 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001791 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001792 idx, &gain_range[idx]);
1793 if (err != 0)
1794 return err;
1795 }
1796
1797 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1798 uinfo->count = 1;
1799 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1800 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1801 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1802 return 0;
1803}
1804
1805static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1806 struct snd_ctl_elem_value *ucontrol)
1807{
1808 /*
1809 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1810 */
1811 u32 h_control = kcontrol->private_value;
1812 short gain;
1813
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001814 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001815 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1816
1817 return 0;
1818}
1819
1820static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1821 struct snd_ctl_elem_value *ucontrol)
1822{
1823 /*
1824 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1825 */
1826 u32 h_control = kcontrol->private_value;
1827 short gain;
1828
1829 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001830 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001831
1832 return 1;
1833}
1834
1835/* Band */
1836
1837static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1838 u16 *band_list, u32 len) {
1839 u32 h_control = kcontrol->private_value;
1840 u16 err = 0;
1841 u32 i;
1842
1843 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001844 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001845 h_control, i, &band_list[i]);
1846 if (err != 0)
1847 break;
1848 }
1849
1850 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1851 return -EIO;
1852
1853 return i;
1854}
1855
1856static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_info *uinfo)
1858{
1859 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1860 int num_bands = 0;
1861
1862 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1863 HPI_TUNER_BAND_LAST);
1864
1865 if (num_bands < 0)
1866 return num_bands;
1867
1868 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1869 uinfo->count = 1;
1870 uinfo->value.enumerated.items = num_bands;
1871
1872 if (num_bands > 0) {
1873 if (uinfo->value.enumerated.item >=
1874 uinfo->value.enumerated.items)
1875 uinfo->value.enumerated.item =
1876 uinfo->value.enumerated.items - 1;
1877
1878 strcpy(uinfo->value.enumerated.name,
1879 asihpi_tuner_band_names[
1880 tuner_bands[uinfo->value.enumerated.item]]);
1881
1882 }
1883 return 0;
1884}
1885
1886static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1887 struct snd_ctl_elem_value *ucontrol)
1888{
1889 u32 h_control = kcontrol->private_value;
1890 /*
1891 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1892 */
1893 u16 band, idx;
1894 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1895 u32 num_bands = 0;
1896
1897 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1898 HPI_TUNER_BAND_LAST);
1899
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001900 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001901
1902 ucontrol->value.enumerated.item[0] = -1;
1903 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1904 if (tuner_bands[idx] == band) {
1905 ucontrol->value.enumerated.item[0] = idx;
1906 break;
1907 }
1908
1909 return 0;
1910}
1911
1912static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1913 struct snd_ctl_elem_value *ucontrol)
1914{
1915 /*
1916 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1917 */
1918 u32 h_control = kcontrol->private_value;
1919 u16 band;
1920 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1921 u32 num_bands = 0;
1922
1923 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1924 HPI_TUNER_BAND_LAST);
1925
1926 band = tuner_bands[ucontrol->value.enumerated.item[0]];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001927 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001928
1929 return 1;
1930}
1931
1932/* Freq */
1933
1934static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1935 struct snd_ctl_elem_info *uinfo)
1936{
1937 u32 h_control = kcontrol->private_value;
1938 u16 err;
1939 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1940 u16 num_bands = 0, band_iter, idx;
1941 u32 freq_range[3], temp_freq_range[3];
1942
1943 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1944 HPI_TUNER_BAND_LAST);
1945
1946 freq_range[0] = INT_MAX;
1947 freq_range[1] = 0;
1948 freq_range[2] = INT_MAX;
1949
1950 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1951 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001952 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001953 idx, tuner_bands[band_iter],
1954 &temp_freq_range[idx]);
1955 if (err != 0)
1956 return err;
1957 }
1958
1959 /* skip band with bogus stepping */
1960 if (temp_freq_range[2] <= 0)
1961 continue;
1962
1963 if (temp_freq_range[0] < freq_range[0])
1964 freq_range[0] = temp_freq_range[0];
1965 if (temp_freq_range[1] > freq_range[1])
1966 freq_range[1] = temp_freq_range[1];
1967 if (temp_freq_range[2] < freq_range[2])
1968 freq_range[2] = temp_freq_range[2];
1969 }
1970
1971 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1972 uinfo->count = 1;
1973 uinfo->value.integer.min = ((int)freq_range[0]);
1974 uinfo->value.integer.max = ((int)freq_range[1]);
1975 uinfo->value.integer.step = ((int)freq_range[2]);
1976 return 0;
1977}
1978
1979static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
1981{
1982 u32 h_control = kcontrol->private_value;
1983 u32 freq;
1984
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001985 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001986 ucontrol->value.integer.value[0] = freq;
1987
1988 return 0;
1989}
1990
1991static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1992 struct snd_ctl_elem_value *ucontrol)
1993{
1994 u32 h_control = kcontrol->private_value;
1995 u32 freq;
1996
1997 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001998 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001999
2000 return 1;
2001}
2002
2003/* Tuner control group initializer */
2004static int __devinit snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2005 struct hpi_control *hpi_ctl)
2006{
2007 struct snd_card *card = asihpi->card;
2008 struct snd_kcontrol_new snd_control;
2009
2010 snd_control.private_value = hpi_ctl->h_control;
2011 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2012
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002013 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002014 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002015 snd_control.info = snd_asihpi_tuner_gain_info;
2016 snd_control.get = snd_asihpi_tuner_gain_get;
2017 snd_control.put = snd_asihpi_tuner_gain_put;
2018
2019 if (ctl_add(card, &snd_control, asihpi) < 0)
2020 return -EINVAL;
2021 }
2022
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002023 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002024 snd_control.info = snd_asihpi_tuner_band_info;
2025 snd_control.get = snd_asihpi_tuner_band_get;
2026 snd_control.put = snd_asihpi_tuner_band_put;
2027
2028 if (ctl_add(card, &snd_control, asihpi) < 0)
2029 return -EINVAL;
2030
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002031 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002032 snd_control.info = snd_asihpi_tuner_freq_info;
2033 snd_control.get = snd_asihpi_tuner_freq_get;
2034 snd_control.put = snd_asihpi_tuner_freq_put;
2035
2036 return ctl_add(card, &snd_control, asihpi);
2037}
2038
2039/*------------------------------------------------------------
2040 Meter controls
2041 ------------------------------------------------------------*/
2042static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_info *uinfo)
2044{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002045 u32 h_control = kcontrol->private_value;
2046 u32 count;
2047 u16 err;
2048 err = hpi_meter_query_channels(h_control, &count);
2049 if (err)
2050 count = HPI_MAX_CHANNELS;
2051
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002052 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002053 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002054 uinfo->value.integer.min = 0;
2055 uinfo->value.integer.max = 0x7FFFFFFF;
2056 return 0;
2057}
2058
2059/* linear values for 10dB steps */
2060static int log2lin[] = {
2061 0x7FFFFFFF, /* 0dB */
2062 679093956,
2063 214748365,
2064 67909396,
2065 21474837,
2066 6790940,
2067 2147484, /* -60dB */
2068 679094,
2069 214748, /* -80 */
2070 67909,
2071 21475, /* -100 */
2072 6791,
2073 2147,
2074 679,
2075 214,
2076 68,
2077 21,
2078 7,
2079 2
2080};
2081
2082static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2083 struct snd_ctl_elem_value *ucontrol)
2084{
2085 u32 h_control = kcontrol->private_value;
2086 short an_gain_mB[HPI_MAX_CHANNELS], i;
2087 u16 err;
2088
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002089 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002090
2091 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2092 if (err) {
2093 ucontrol->value.integer.value[i] = 0;
2094 } else if (an_gain_mB[i] >= 0) {
2095 ucontrol->value.integer.value[i] =
2096 an_gain_mB[i] << 16;
2097 } else {
2098 /* -ve is log value in millibels < -60dB,
2099 * convert to (roughly!) linear,
2100 */
2101 ucontrol->value.integer.value[i] =
2102 log2lin[an_gain_mB[i] / -1000];
2103 }
2104 }
2105 return 0;
2106}
2107
2108static int __devinit snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2109 struct hpi_control *hpi_ctl, int subidx)
2110{
2111 struct snd_card *card = asihpi->card;
2112 struct snd_kcontrol_new snd_control;
2113
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002114 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002115 snd_control.access =
2116 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2117 snd_control.info = snd_asihpi_meter_info;
2118 snd_control.get = snd_asihpi_meter_get;
2119
2120 snd_control.index = subidx;
2121
2122 return ctl_add(card, &snd_control, asihpi);
2123}
2124
2125/*------------------------------------------------------------
2126 Multiplexer controls
2127 ------------------------------------------------------------*/
2128static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2129{
2130 u32 h_control = snd_control->private_value;
2131 struct hpi_control hpi_ctl;
2132 int s, err;
2133 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002134 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002135 &hpi_ctl.
2136 src_node_type,
2137 &hpi_ctl.
2138 src_node_index);
2139 if (err)
2140 break;
2141 }
2142 return s;
2143}
2144
2145static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_info *uinfo)
2147{
2148 int err;
2149 u16 src_node_type, src_node_index;
2150 u32 h_control = kcontrol->private_value;
2151
2152 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2153 uinfo->count = 1;
2154 uinfo->value.enumerated.items =
2155 snd_card_asihpi_mux_count_sources(kcontrol);
2156
2157 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2158 uinfo->value.enumerated.item =
2159 uinfo->value.enumerated.items - 1;
2160
2161 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002162 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002163 uinfo->value.enumerated.item,
2164 &src_node_type, &src_node_index);
2165
2166 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002167 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002168 src_node_index);
2169 return 0;
2170}
2171
2172static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2173 struct snd_ctl_elem_value *ucontrol)
2174{
2175 u32 h_control = kcontrol->private_value;
2176 u16 source_type, source_index;
2177 u16 src_node_type, src_node_index;
2178 int s;
2179
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002180 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002181 &source_type, &source_index));
2182 /* Should cache this search result! */
2183 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002184 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002185 &src_node_type, &src_node_index))
2186 break;
2187
2188 if ((source_type == src_node_type)
2189 && (source_index == src_node_index)) {
2190 ucontrol->value.enumerated.item[0] = s;
2191 return 0;
2192 }
2193 }
2194 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002195 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002196 h_control, source_type, source_index);
2197 ucontrol->value.enumerated.item[0] = 0;
2198 return 0;
2199}
2200
2201static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2202 struct snd_ctl_elem_value *ucontrol)
2203{
2204 int change;
2205 u32 h_control = kcontrol->private_value;
2206 u16 source_type, source_index;
2207 u16 e;
2208
2209 change = 1;
2210
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002211 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002212 ucontrol->value.enumerated.item[0],
2213 &source_type, &source_index);
2214 if (!e)
2215 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002216 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002217 source_type, source_index));
2218 return change;
2219}
2220
2221
2222static int __devinit snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2223 struct hpi_control *hpi_ctl)
2224{
2225 struct snd_card *card = asihpi->card;
2226 struct snd_kcontrol_new snd_control;
2227
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002228 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002229 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2230 snd_control.info = snd_asihpi_mux_info;
2231 snd_control.get = snd_asihpi_mux_get;
2232 snd_control.put = snd_asihpi_mux_put;
2233
2234 return ctl_add(card, &snd_control, asihpi);
2235
2236}
2237
2238/*------------------------------------------------------------
2239 Channel mode controls
2240 ------------------------------------------------------------*/
2241static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2242 struct snd_ctl_elem_info *uinfo)
2243{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002244 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2245 "invalid",
2246 "Normal", "Swap",
2247 "From Left", "From Right",
2248 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002249 };
2250
2251 u32 h_control = kcontrol->private_value;
2252 u16 mode;
2253 int i;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002254 u16 mode_map[6];
2255 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002256
2257 /* HPI channel mode values can be from 1 to 6
2258 Some adapters only support a contiguous subset
2259 */
2260 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002261 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002262 h_control, i, &mode)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002263 mode_map[valid_modes] = mode;
2264 valid_modes++;
2265 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002266
2267 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2268 uinfo->count = 1;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002269 uinfo->value.enumerated.items = valid_modes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002270
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002271 if (uinfo->value.enumerated.item >= valid_modes)
2272 uinfo->value.enumerated.item = valid_modes - 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002273
2274 strcpy(uinfo->value.enumerated.name,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002275 mode_names[mode_map[uinfo->value.enumerated.item]]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002276
2277 return 0;
2278}
2279
2280static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2281 struct snd_ctl_elem_value *ucontrol)
2282{
2283 u32 h_control = kcontrol->private_value;
2284 u16 mode;
2285
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002286 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002287 mode = 1;
2288
2289 ucontrol->value.enumerated.item[0] = mode - 1;
2290
2291 return 0;
2292}
2293
2294static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2295 struct snd_ctl_elem_value *ucontrol)
2296{
2297 int change;
2298 u32 h_control = kcontrol->private_value;
2299
2300 change = 1;
2301
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002302 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002303 ucontrol->value.enumerated.item[0] + 1));
2304 return change;
2305}
2306
2307
2308static int __devinit snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2309 struct hpi_control *hpi_ctl)
2310{
2311 struct snd_card *card = asihpi->card;
2312 struct snd_kcontrol_new snd_control;
2313
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002314 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002315 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2316 snd_control.info = snd_asihpi_cmode_info;
2317 snd_control.get = snd_asihpi_cmode_get;
2318 snd_control.put = snd_asihpi_cmode_put;
2319
2320 return ctl_add(card, &snd_control, asihpi);
2321}
2322
2323/*------------------------------------------------------------
2324 Sampleclock source controls
2325 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002326static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2327 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2328 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2329 "Prev Module",
2330 "Digital2", "Digital3", "Digital4", "Digital5",
2331 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002332
2333static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2334 struct snd_ctl_elem_info *uinfo)
2335{
2336 struct snd_card_asihpi *asihpi =
2337 (struct snd_card_asihpi *)(kcontrol->private_data);
2338 struct clk_cache *clkcache = &asihpi->cc;
2339 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2340 uinfo->count = 1;
2341 uinfo->value.enumerated.items = clkcache->count;
2342
2343 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2344 uinfo->value.enumerated.item =
2345 uinfo->value.enumerated.items - 1;
2346
2347 strcpy(uinfo->value.enumerated.name,
2348 clkcache->s[uinfo->value.enumerated.item].name);
2349 return 0;
2350}
2351
2352static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2353 struct snd_ctl_elem_value *ucontrol)
2354{
2355 struct snd_card_asihpi *asihpi =
2356 (struct snd_card_asihpi *)(kcontrol->private_data);
2357 struct clk_cache *clkcache = &asihpi->cc;
2358 u32 h_control = kcontrol->private_value;
2359 u16 source, srcindex = 0;
2360 int i;
2361
2362 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002363 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002364 source = 0;
2365
2366 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002367 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002368 srcindex = 0;
2369
2370 for (i = 0; i < clkcache->count; i++)
2371 if ((clkcache->s[i].source == source) &&
2372 (clkcache->s[i].index == srcindex))
2373 break;
2374
2375 ucontrol->value.enumerated.item[0] = i;
2376
2377 return 0;
2378}
2379
2380static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2381 struct snd_ctl_elem_value *ucontrol)
2382{
2383 struct snd_card_asihpi *asihpi =
2384 (struct snd_card_asihpi *)(kcontrol->private_data);
2385 struct clk_cache *clkcache = &asihpi->cc;
2386 int change, item;
2387 u32 h_control = kcontrol->private_value;
2388
2389 change = 1;
2390 item = ucontrol->value.enumerated.item[0];
2391 if (item >= clkcache->count)
2392 item = clkcache->count-1;
2393
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002394 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002395 h_control, clkcache->s[item].source));
2396
2397 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002398 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002399 h_control, clkcache->s[item].index));
2400 return change;
2401}
2402
2403/*------------------------------------------------------------
2404 Clkrate controls
2405 ------------------------------------------------------------*/
2406/* Need to change this to enumerated control with list of rates */
2407static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2408 struct snd_ctl_elem_info *uinfo)
2409{
2410 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2411 uinfo->count = 1;
2412 uinfo->value.integer.min = 8000;
2413 uinfo->value.integer.max = 192000;
2414 uinfo->value.integer.step = 100;
2415
2416 return 0;
2417}
2418
2419static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2420 struct snd_ctl_elem_value *ucontrol)
2421{
2422 u32 h_control = kcontrol->private_value;
2423 u32 rate;
2424 u16 e;
2425
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002426 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002427 if (!e)
2428 ucontrol->value.integer.value[0] = rate;
2429 else
2430 ucontrol->value.integer.value[0] = 0;
2431 return 0;
2432}
2433
2434static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2435 struct snd_ctl_elem_value *ucontrol)
2436{
2437 int change;
2438 u32 h_control = kcontrol->private_value;
2439
2440 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2441 asihpi->mixer_clkrate[addr][1] != right;
2442 */
2443 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002444 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002445 ucontrol->value.integer.value[0]));
2446 return change;
2447}
2448
2449static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2450 struct snd_ctl_elem_info *uinfo)
2451{
2452 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2453 uinfo->count = 1;
2454 uinfo->value.integer.min = 8000;
2455 uinfo->value.integer.max = 192000;
2456 uinfo->value.integer.step = 100;
2457
2458 return 0;
2459}
2460
2461static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2462 struct snd_ctl_elem_value *ucontrol)
2463{
2464 u32 h_control = kcontrol->private_value;
2465 u32 rate;
2466 u16 e;
2467
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002468 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002469 if (!e)
2470 ucontrol->value.integer.value[0] = rate;
2471 else
2472 ucontrol->value.integer.value[0] = 0;
2473 return 0;
2474}
2475
2476static int __devinit snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2477 struct hpi_control *hpi_ctl)
2478{
2479 struct snd_card *card = asihpi->card;
2480 struct snd_kcontrol_new snd_control;
2481
2482 struct clk_cache *clkcache = &asihpi->cc;
2483 u32 hSC = hpi_ctl->h_control;
2484 int has_aes_in = 0;
2485 int i, j;
2486 u16 source;
2487
2488 snd_control.private_value = hpi_ctl->h_control;
2489
2490 clkcache->has_local = 0;
2491
2492 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002493 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002494 i, &source))
2495 break;
2496 clkcache->s[i].source = source;
2497 clkcache->s[i].index = 0;
2498 clkcache->s[i].name = sampleclock_sources[source];
2499 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2500 has_aes_in = 1;
2501 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2502 clkcache->has_local = 1;
2503 }
2504 if (has_aes_in)
2505 /* already will have picked up index 0 above */
2506 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002507 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002508 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2509 &source))
2510 break;
2511 clkcache->s[i].source =
2512 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2513 clkcache->s[i].index = j;
2514 clkcache->s[i].name = sampleclock_sources[
2515 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2516 i++;
2517 }
2518 clkcache->count = i;
2519
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002520 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002521 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2522 snd_control.info = snd_asihpi_clksrc_info;
2523 snd_control.get = snd_asihpi_clksrc_get;
2524 snd_control.put = snd_asihpi_clksrc_put;
2525 if (ctl_add(card, &snd_control, asihpi) < 0)
2526 return -EINVAL;
2527
2528
2529 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002530 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002531 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2532 snd_control.info = snd_asihpi_clklocal_info;
2533 snd_control.get = snd_asihpi_clklocal_get;
2534 snd_control.put = snd_asihpi_clklocal_put;
2535
2536
2537 if (ctl_add(card, &snd_control, asihpi) < 0)
2538 return -EINVAL;
2539 }
2540
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002541 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002542 snd_control.access =
2543 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2544 snd_control.info = snd_asihpi_clkrate_info;
2545 snd_control.get = snd_asihpi_clkrate_get;
2546
2547 return ctl_add(card, &snd_control, asihpi);
2548}
2549/*------------------------------------------------------------
2550 Mixer
2551 ------------------------------------------------------------*/
2552
2553static int __devinit snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
2554{
2555 struct snd_card *card = asihpi->card;
2556 unsigned int idx = 0;
2557 unsigned int subindex = 0;
2558 int err;
2559 struct hpi_control hpi_ctl, prev_ctl;
2560
2561 if (snd_BUG_ON(!asihpi))
2562 return -EINVAL;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002563 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002564
2565 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002566 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002567 &asihpi->h_mixer);
2568 hpi_handle_error(err);
2569 if (err)
2570 return -err;
2571
Takashi Iwai21896bc2010-06-02 12:08:37 +02002572 memset(&prev_ctl, 0, sizeof(prev_ctl));
2573 prev_ctl.control_type = -1;
2574
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002575 for (idx = 0; idx < 2000; idx++) {
2576 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002577 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002578 idx,
2579 &hpi_ctl.src_node_type,
2580 &hpi_ctl.src_node_index,
2581 &hpi_ctl.dst_node_type,
2582 &hpi_ctl.dst_node_index,
2583 &hpi_ctl.control_type,
2584 &hpi_ctl.h_control);
2585 if (err) {
2586 if (err == HPI_ERROR_CONTROL_DISABLED) {
2587 if (mixer_dump)
2588 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002589 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002590 idx);
2591 continue;
2592 } else
2593 break;
2594
2595 }
2596
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002597 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2598 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002599
2600 /* ASI50xx in SSX mode has multiple meters on the same node.
2601 Use subindex to create distinct ALSA controls
2602 for any duplicated controls.
2603 */
2604 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2605 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2606 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2607 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2608 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2609 subindex++;
2610 else
2611 subindex = 0;
2612
2613 prev_ctl = hpi_ctl;
2614
2615 switch (hpi_ctl.control_type) {
2616 case HPI_CONTROL_VOLUME:
2617 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2618 break;
2619 case HPI_CONTROL_LEVEL:
2620 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2621 break;
2622 case HPI_CONTROL_MULTIPLEXER:
2623 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2624 break;
2625 case HPI_CONTROL_CHANNEL_MODE:
2626 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2627 break;
2628 case HPI_CONTROL_METER:
2629 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2630 break;
2631 case HPI_CONTROL_SAMPLECLOCK:
2632 err = snd_asihpi_sampleclock_add(
2633 asihpi, &hpi_ctl);
2634 break;
2635 case HPI_CONTROL_CONNECTION: /* ignore these */
2636 continue;
2637 case HPI_CONTROL_TUNER:
2638 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2639 break;
2640 case HPI_CONTROL_AESEBU_TRANSMITTER:
2641 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2642 break;
2643 case HPI_CONTROL_AESEBU_RECEIVER:
2644 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2645 break;
2646 case HPI_CONTROL_VOX:
2647 case HPI_CONTROL_BITSTREAM:
2648 case HPI_CONTROL_MICROPHONE:
2649 case HPI_CONTROL_PARAMETRIC_EQ:
2650 case HPI_CONTROL_COMPANDER:
2651 default:
2652 if (mixer_dump)
2653 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002654 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002655 "(%d) %d %d %d %d %d\n",
2656 idx,
2657 hpi_ctl.control_type,
2658 hpi_ctl.src_node_type,
2659 hpi_ctl.src_node_index,
2660 hpi_ctl.dst_node_type,
2661 hpi_ctl.dst_node_index);
2662 continue;
2663 };
2664 if (err < 0)
2665 return err;
2666 }
2667 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2668 hpi_handle_error(err);
2669
2670 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2671
2672 return 0;
2673}
2674
2675/*------------------------------------------------------------
2676 /proc interface
2677 ------------------------------------------------------------*/
2678
2679static void
2680snd_asihpi_proc_read(struct snd_info_entry *entry,
2681 struct snd_info_buffer *buffer)
2682{
2683 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002684 u32 h_control;
2685 u32 rate = 0;
2686 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002687
2688 u16 num_outstreams;
2689 u16 num_instreams;
2690 u16 version;
2691 u32 serial_number;
2692 u16 type;
2693
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002694 int err;
2695
2696 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002697
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002698 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2699 &num_outstreams, &num_instreams,
2700 &version, &serial_number, &type));
2701
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002702 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002703 "Adapter type ASI%4X\nHardware Index %d\n"
2704 "%d outstreams\n%d instreams\n",
2705 type, asihpi->hpi->adapter->index,
2706 num_outstreams, num_instreams);
2707
2708 snd_iprintf(buffer,
2709 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2710 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002711 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2712
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002713 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002714 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2715 HPI_CONTROL_SAMPLECLOCK, &h_control);
2716
2717 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002718 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002719 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002720
2721 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002722 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002723 rate, sampleclock_sources[source]);
2724 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002725}
2726
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002727static void __devinit snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
2728{
2729 struct snd_info_entry *entry;
2730
2731 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2732 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2733}
2734
2735/*------------------------------------------------------------
2736 HWDEP
2737 ------------------------------------------------------------*/
2738
2739static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2740{
2741 if (enable_hpi_hwdep)
2742 return 0;
2743 else
2744 return -ENODEV;
2745
2746}
2747
2748static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2749{
2750 if (enable_hpi_hwdep)
2751 return asihpi_hpi_release(file);
2752 else
2753 return -ENODEV;
2754}
2755
2756static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2757 unsigned int cmd, unsigned long arg)
2758{
2759 if (enable_hpi_hwdep)
2760 return asihpi_hpi_ioctl(file, cmd, arg);
2761 else
2762 return -ENODEV;
2763}
2764
2765
2766/* results in /dev/snd/hwC#D0 file for each card with index #
2767 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2768*/
2769static int __devinit snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2770 int device, struct snd_hwdep **rhwdep)
2771{
2772 struct snd_hwdep *hw;
2773 int err;
2774
2775 if (rhwdep)
2776 *rhwdep = NULL;
2777 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2778 if (err < 0)
2779 return err;
2780 strcpy(hw->name, "asihpi (HPI)");
2781 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2782 hw->ops.open = snd_asihpi_hpi_open;
2783 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2784 hw->ops.release = snd_asihpi_hpi_release;
2785 hw->private_data = asihpi;
2786 if (rhwdep)
2787 *rhwdep = hw;
2788 return 0;
2789}
2790
2791/*------------------------------------------------------------
2792 CARD
2793 ------------------------------------------------------------*/
2794static int __devinit snd_asihpi_probe(struct pci_dev *pci_dev,
2795 const struct pci_device_id *pci_id)
2796{
2797 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002798 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002799 struct snd_card *card;
2800 struct snd_card_asihpi *asihpi;
2801
2802 u32 h_control;
2803 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002804 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002805
2806 static int dev;
2807 if (dev >= SNDRV_CARDS)
2808 return -ENODEV;
2809
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002810 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002811 if (!enable[dev]) {
2812 dev++;
2813 return -ENOENT;
2814 }
2815
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002816 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002817 err = asihpi_adapter_probe(pci_dev, pci_id);
2818 if (err < 0)
2819 return err;
2820
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002821 hpi = pci_get_drvdata(pci_dev);
2822 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002823 /* first try to give the card the same index as its hardware index */
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002824 err = snd_card_create(adapter_index,
2825 id[adapter_index], THIS_MODULE,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002826 sizeof(struct snd_card_asihpi),
2827 &card);
2828 if (err < 0) {
2829 /* if that fails, try the default index==next available */
2830 err =
2831 snd_card_create(index[dev], id[dev],
2832 THIS_MODULE,
2833 sizeof(struct snd_card_asihpi),
2834 &card);
2835 if (err < 0)
2836 return err;
2837 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002838 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002839 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002840 }
2841
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002842 snd_card_set_dev(card, &pci_dev->dev);
2843
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002844 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002845 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002846 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002847 asihpi->hpi = hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002848
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002849 snd_printk(KERN_INFO "adapter ID=%4X index=%d\n",
2850 asihpi->hpi->adapter->type, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002852 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002853 HPI_ADAPTER_PROPERTY_CAPS1,
2854 NULL, &asihpi->support_grouping);
2855 if (err)
2856 asihpi->support_grouping = 0;
2857
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002858 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002859 HPI_ADAPTER_PROPERTY_CAPS2,
2860 &asihpi->support_mrx, NULL);
2861 if (err)
2862 asihpi->support_mrx = 0;
2863
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002864 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002865 HPI_ADAPTER_PROPERTY_INTERVAL,
2866 NULL, &asihpi->update_interval_frames);
2867 if (err)
2868 asihpi->update_interval_frames = 512;
2869
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002870 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002871 asihpi->update_interval_frames *= 2;
2872
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002873 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002874 0, &h_stream));
2875
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002876 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002877 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002878
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002879 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002880
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002881 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002882 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2883 &asihpi->in_max_chans, &asihpi->out_max_chans);
2884 if (err) {
2885 asihpi->in_max_chans = 2;
2886 asihpi->out_max_chans = 2;
2887 }
2888
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002889 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2890 asihpi->out_min_chans = asihpi->out_max_chans;
2891 asihpi->in_min_chans = asihpi->in_max_chans;
2892 asihpi->support_grouping = 0;
2893 } else {
2894 asihpi->out_min_chans = 1;
2895 asihpi->in_min_chans = 1;
2896 }
2897
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002898 snd_printk(KERN_INFO "Has dma:%d, grouping:%d, mrx:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002899 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002900 asihpi->support_grouping,
2901 asihpi->support_mrx
2902 );
2903
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002904 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002905 if (err < 0) {
2906 snd_printk(KERN_ERR "pcm_new failed\n");
2907 goto __nodev;
2908 }
2909 err = snd_card_asihpi_mixer_new(asihpi);
2910 if (err < 0) {
2911 snd_printk(KERN_ERR "mixer_new failed\n");
2912 goto __nodev;
2913 }
2914
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002915 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002916 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2917 HPI_CONTROL_SAMPLECLOCK, &h_control);
2918
2919 if (!err)
2920 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002921 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002922
2923 snd_asihpi_proc_init(asihpi);
2924
2925 /* always create, can be enabled or disabled dynamically
2926 by enable_hwdep module param*/
2927 snd_asihpi_hpi_new(asihpi, 0, NULL);
2928
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002929 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002930
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002931 sprintf(card->shortname, "AudioScience ASI%4X",
2932 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002933 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002934 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002935 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002936
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002937 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002938 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002939 dev++;
2940 return 0;
2941 }
2942__nodev:
2943 snd_card_free(card);
2944 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2945 return err;
2946
2947}
2948
2949static void __devexit snd_asihpi_remove(struct pci_dev *pci_dev)
2950{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002951 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
2952 snd_card_free(hpi->snd_card);
2953 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002954 asihpi_adapter_remove(pci_dev);
2955}
2956
2957static DEFINE_PCI_DEVICE_TABLE(asihpi_pci_tbl) = {
2958 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2959 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2960 (kernel_ulong_t)HPI_6205},
2961 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2962 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2963 (kernel_ulong_t)HPI_6000},
2964 {0,}
2965};
2966MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2967
2968static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002969 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002970 .id_table = asihpi_pci_tbl,
2971 .probe = snd_asihpi_probe,
2972 .remove = __devexit_p(snd_asihpi_remove),
2973#ifdef CONFIG_PM
2974/* .suspend = snd_asihpi_suspend,
2975 .resume = snd_asihpi_resume, */
2976#endif
2977};
2978
2979static int __init snd_asihpi_init(void)
2980{
2981 asihpi_init();
2982 return pci_register_driver(&driver);
2983}
2984
2985static void __exit snd_asihpi_exit(void)
2986{
2987
2988 pci_unregister_driver(&driver);
2989 asihpi_exit();
2990}
2991
2992module_init(snd_asihpi_init)
2993module_exit(snd_asihpi_exit)
2994