blob: ff9f9f1c0391ff845c11c153ed2083353c895c52 [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"
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130026#include "hpi_version.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020027#include "hpimsginit.h"
28#include "hpioctl.h"
Eliot Blennerhassett7036b922011-12-22 13:38:43 +130029#include "hpicmn.h"
30
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020031
32#include <linux/pci.h>
33#include <linux/init.h>
34#include <linux/jiffies.h>
35#include <linux/slab.h>
36#include <linux/time.h>
37#include <linux/wait.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040038#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020039#include <sound/core.h>
40#include <sound/control.h>
41#include <sound/pcm.h>
42#include <sound/pcm_params.h>
43#include <sound/info.h>
44#include <sound/initval.h>
45#include <sound/tlv.h>
46#include <sound/hwdep.h>
47
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020048MODULE_LICENSE("GPL");
49MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130050MODULE_DESCRIPTION("AudioScience ALSA ASI5000 ASI6000 ASI87xx ASI89xx "
51 HPI_VER_STRING);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020052
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130053#if defined CONFIG_SND_DEBUG_VERBOSE
54/**
55 * snd_printddd - very verbose debug printk
56 * @format: format string
57 *
58 * Works like snd_printk() for debugging purposes.
59 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
60 * Must set snd module debug parameter to 3 to enable at runtime.
61 */
62#define snd_printddd(format, args...) \
63 __snd_printk(3, __FILE__, __LINE__, format, ##args)
64#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120065#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130066#endif
67
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020068static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
69static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103070static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
71static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020072
73module_param_array(index, int, NULL, S_IRUGO);
74MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
75
76module_param_array(id, charp, NULL, S_IRUGO);
77MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
78
79module_param_array(enable, bool, NULL, S_IRUGO);
80MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
81
82module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
83MODULE_PARM_DESC(enable_hpi_hwdep,
84 "ALSA enable HPI hwdep for AudioScience soundcard ");
85
86/* identify driver */
87#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130088static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020089module_param(build_info, charp, S_IRUGO);
90MODULE_PARM_DESC(build_info, "built using headers from kernel source");
91#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130092static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020093module_param(build_info, charp, S_IRUGO);
94MODULE_PARM_DESC(build_info, "built within ALSA source");
95#endif
96
97/* set to 1 to dump every control from adapter to log */
98static const int mixer_dump;
99
100#define DEFAULT_SAMPLERATE 44100
101static int adapter_fs = DEFAULT_SAMPLERATE;
102
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200103/* defaults */
104#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300105#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200106#define BUFFER_BYTES_MAX (512 * 1024)
107
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200108#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
109
110struct clk_source {
111 int source;
112 int index;
113 char *name;
114};
115
116struct clk_cache {
117 int count;
118 int has_local;
119 struct clk_source s[MAX_CLOCKSOURCES];
120};
121
122/* Per card data */
123struct snd_card_asihpi {
124 struct snd_card *card;
125 struct pci_dev *pci;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300126 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200127
128 u32 h_mixer;
129 struct clk_cache cc;
130
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200131 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200132 u16 support_grouping;
133 u16 support_mrx;
134 u16 update_interval_frames;
135 u16 in_max_chans;
136 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300137 u16 in_min_chans;
138 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200139};
140
141/* Per stream data */
142struct snd_card_asihpi_pcm {
143 struct timer_list timer;
144 unsigned int respawn_timer;
145 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300146 unsigned int buffer_bytes;
147 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200148 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300149 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
150 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
151 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200152 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200153 struct snd_pcm_substream *substream;
154 u32 h_stream;
155 struct hpi_format format;
156};
157
158/* universal stream verbs work with out or in stream handles */
159
160/* Functions to allow driver to give a buffer to HPI for busmastering */
161
162static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200163 u32 h_stream, /* handle to outstream. */
164 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
165 u32 pci_address
166)
167{
168 struct hpi_message hm;
169 struct hpi_response hr;
170 unsigned int obj = hpi_handle_object(h_stream);
171
172 if (!h_stream)
173 return HPI_ERROR_INVALID_OBJ;
174 hpi_init_message_response(&hm, &hr, obj,
175 obj == HPI_OBJ_OSTREAM ?
176 HPI_OSTREAM_HOSTBUFFER_ALLOC :
177 HPI_ISTREAM_HOSTBUFFER_ALLOC);
178
179 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
180 &hm.obj_index);
181
182 hm.u.d.u.buffer.buffer_size = size_in_bytes;
183 hm.u.d.u.buffer.pci_address = pci_address;
184 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
185 hpi_send_recv(&hm, &hr);
186 return hr.error;
187}
188
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300189static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200190{
191 struct hpi_message hm;
192 struct hpi_response hr;
193 unsigned int obj = hpi_handle_object(h_stream);
194
195 if (!h_stream)
196 return HPI_ERROR_INVALID_OBJ;
197
198 hpi_init_message_response(&hm, &hr, obj,
199 obj == HPI_OBJ_OSTREAM ?
200 HPI_OSTREAM_HOSTBUFFER_FREE :
201 HPI_ISTREAM_HOSTBUFFER_FREE);
202
203 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
204 &hm.obj_index);
205 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
206 hpi_send_recv(&hm, &hr);
207 return hr.error;
208}
209
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300210static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200211{
212 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300213 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200214 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300215 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200216}
217
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300218static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200219{
220 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300221 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200222 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300223 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200224}
225
226static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200227 u32 h_stream,
228 u16 *pw_state,
229 u32 *pbuffer_size,
230 u32 *pdata_in_buffer,
231 u32 *psample_count,
232 u32 *pauxiliary_data
233)
234{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300235 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300237 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200238 pbuffer_size, pdata_in_buffer,
239 psample_count, pauxiliary_data);
240 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300241 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200242 pbuffer_size, pdata_in_buffer,
243 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300244 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200245}
246
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300247static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200248 u32 h_master,
249 u32 h_stream)
250{
251 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300252 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200253 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300254 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200255}
256
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300257static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200258{
259 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300260 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200261 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300262 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200263}
264
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300265static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200266 u32 h_stream, u32 *mo, u32 *mi)
267{
268 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272}
273
274static u16 handle_error(u16 err, int line, char *filename)
275{
276 if (err)
277 printk(KERN_WARNING
278 "in file %s, line %d: HPI error %d\n",
279 filename, line, err);
280 return err;
281}
282
283#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
284
285/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200286
287static void print_hwparams(struct snd_pcm_substream *substream,
288 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200289{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200290 char name[16];
291 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200292 snd_printd("%s HWPARAMS\n", name);
293 snd_printd(" samplerate %d Hz\n", params_rate(p));
294 snd_printd(" channels %d\n", params_channels(p));
295 snd_printd(" format %d\n", params_format(p));
296 snd_printd(" subformat %d\n", params_subformat(p));
297 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
298 snd_printd(" period %d B\n", params_period_bytes(p));
299 snd_printd(" access %d\n", params_access(p));
300 snd_printd(" period_size %d\n", params_period_size(p));
301 snd_printd(" periods %d\n", params_periods(p));
302 snd_printd(" buffer_size %d\n", params_buffer_size(p));
303 snd_printd(" %d B/s\n", params_rate(p) *
304 params_channels(p) *
305 snd_pcm_format_width(params_format(p)) / 8);
306
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200307}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200308
309static snd_pcm_format_t hpi_to_alsa_formats[] = {
310 -1, /* INVALID */
311 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
312 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
313 -1, /* HPI_FORMAT_MPEG_L1 3 */
314 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
315 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
316 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
317 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
318 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
319 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
320 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
321 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
322 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
323 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
324 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
325#if 1
326 /* ALSA can't handle 3 byte sample size together with power-of-2
327 * constraint on buffer_bytes, so disable this format
328 */
329 -1
330#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300331 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200332#endif
333};
334
335
336static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
337 u16 *hpi_format)
338{
339 u16 format;
340
341 for (format = HPI_FORMAT_PCM8_UNSIGNED;
342 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
343 if (hpi_to_alsa_formats[format] == alsa_format) {
344 *hpi_format = format;
345 return 0;
346 }
347 }
348
349 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
350 alsa_format);
351 *hpi_format = 0;
352 return -EINVAL;
353}
354
355static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
356 struct snd_pcm_hardware *pcmhw)
357{
358 u16 err;
359 u32 h_control;
360 u32 sample_rate;
361 int idx;
362 unsigned int rate_min = 200000;
363 unsigned int rate_max = 0;
364 unsigned int rates = 0;
365
366 if (asihpi->support_mrx) {
367 rates |= SNDRV_PCM_RATE_CONTINUOUS;
368 rates |= SNDRV_PCM_RATE_8000_96000;
369 rate_min = 8000;
370 rate_max = 100000;
371 } else {
372 /* on cards without SRC,
373 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300374 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200375 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
376 HPI_CONTROL_SAMPLECLOCK, &h_control);
377 if (err) {
378 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300379 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200380 }
381
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300382 for (idx = -1; idx < 100; idx++) {
383 if (idx == -1) {
384 if (hpi_sample_clock_get_sample_rate(h_control,
385 &sample_rate))
386 continue;
387 } else if (hpi_sample_clock_query_local_rate(h_control,
388 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200389 break;
390 }
391
392 rate_min = min(rate_min, sample_rate);
393 rate_max = max(rate_max, sample_rate);
394
395 switch (sample_rate) {
396 case 5512:
397 rates |= SNDRV_PCM_RATE_5512;
398 break;
399 case 8000:
400 rates |= SNDRV_PCM_RATE_8000;
401 break;
402 case 11025:
403 rates |= SNDRV_PCM_RATE_11025;
404 break;
405 case 16000:
406 rates |= SNDRV_PCM_RATE_16000;
407 break;
408 case 22050:
409 rates |= SNDRV_PCM_RATE_22050;
410 break;
411 case 32000:
412 rates |= SNDRV_PCM_RATE_32000;
413 break;
414 case 44100:
415 rates |= SNDRV_PCM_RATE_44100;
416 break;
417 case 48000:
418 rates |= SNDRV_PCM_RATE_48000;
419 break;
420 case 64000:
421 rates |= SNDRV_PCM_RATE_64000;
422 break;
423 case 88200:
424 rates |= SNDRV_PCM_RATE_88200;
425 break;
426 case 96000:
427 rates |= SNDRV_PCM_RATE_96000;
428 break;
429 case 176400:
430 rates |= SNDRV_PCM_RATE_176400;
431 break;
432 case 192000:
433 rates |= SNDRV_PCM_RATE_192000;
434 break;
435 default: /* some other rate */
436 rates |= SNDRV_PCM_RATE_KNOT;
437 }
438 }
439 }
440
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200441 pcmhw->rates = rates;
442 pcmhw->rate_min = rate_min;
443 pcmhw->rate_max = rate_max;
444}
445
446static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
447 struct snd_pcm_hw_params *params)
448{
449 struct snd_pcm_runtime *runtime = substream->runtime;
450 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
451 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
452 int err;
453 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400454 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200455 unsigned int bytes_per_sec;
456
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200457 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200458 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
459 if (err < 0)
460 return err;
461 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
462 if (err)
463 return err;
464
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200465 hpi_handle_error(hpi_format_create(&dpcm->format,
466 params_channels(params),
467 format, params_rate(params), 0, 0));
468
469 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300470 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200471 return -EINVAL;
472
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300473 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200474 dpcm->h_stream, &dpcm->format) != 0)
475 return -EINVAL;
476 }
477
478 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200479 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300480 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200481 params_buffer_bytes(params), runtime->dma_addr);
482 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300483 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200484 "stream_host_buffer_attach succeeded %u %lu\n",
485 params_buffer_bytes(params),
486 (unsigned long)runtime->dma_addr);
487 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300488 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200489 err);
490 return -ENOMEM;
491 }
492
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300493 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200494 &dpcm->hpi_buffer_attached,
495 NULL, NULL, NULL);
496
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300497 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200498 dpcm->hpi_buffer_attached);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300499
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500 }
501 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400502 width = snd_pcm_format_width(params_format(params));
503 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400505 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200506 return -EINVAL;
507
508 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300509 dpcm->buffer_bytes = params_buffer_bytes(params);
510 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200511
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200512 return 0;
513}
514
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300515static int
516snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
517{
518 struct snd_pcm_runtime *runtime = substream->runtime;
519 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
520 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300521 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300522
523 snd_pcm_lib_free_pages(substream);
524 return 0;
525}
526
527static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
528{
529 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
530 kfree(dpcm);
531}
532
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200533static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
534 substream)
535{
536 struct snd_pcm_runtime *runtime = substream->runtime;
537 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
538 int expiry;
539
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300540 expiry = HZ / 200;
541 /*? (dpcm->period_bytes * HZ / dpcm->bytes_per_sec); */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300542 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200543 dpcm->timer.expires = jiffies + expiry;
544 dpcm->respawn_timer = 1;
545 add_timer(&dpcm->timer);
546}
547
548static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
549{
550 struct snd_pcm_runtime *runtime = substream->runtime;
551 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
552
553 dpcm->respawn_timer = 0;
554 del_timer(&dpcm->timer);
555}
556
557static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
558 int cmd)
559{
560 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
561 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
562 struct snd_pcm_substream *s;
563 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200564 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200565
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200566 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200567 snd_printdd("%s trigger\n", name);
568
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200569 switch (cmd) {
570 case SNDRV_PCM_TRIGGER_START:
571 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300572 struct snd_pcm_runtime *runtime = s->runtime;
573 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200574
575 if (snd_pcm_substream_chip(s) != card)
576 continue;
577
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300578 /* don't link Cap and Play */
579 if (substream->stream != s->stream)
580 continue;
581
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200582 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200583 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200584 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300585 * in buffer? Must be at least one period!
586 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200587 * buffer is bigger it may contain even more
588 * data??
589 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300590 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300591 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300593 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300594 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200595 preload,
596 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300597 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200598 }
599
600 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200601 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300602 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200603 dpcm->h_stream,
604 ds->h_stream);
605 if (!e) {
606 snd_pcm_trigger_done(s, substream);
607 } else {
608 hpi_handle_error(e);
609 break;
610 }
611 } else
612 break;
613 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300614 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200615 /* start the master stream */
616 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300617 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200618 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300619 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200620 break;
621
622 case SNDRV_PCM_TRIGGER_STOP:
623 snd_card_asihpi_pcm_timer_stop(substream);
624 snd_pcm_group_for_each_entry(s, substream) {
625 if (snd_pcm_substream_chip(s) != card)
626 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300627 /* don't link Cap and Play */
628 if (substream->stream != s->stream)
629 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200630
631 /*? workaround linked streams don't
632 transition to SETUP 20070706*/
633 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
634
635 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200636 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200637 snd_pcm_trigger_done(s, substream);
638 } else
639 break;
640 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300641 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200642
643 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300644 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200645 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
646 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300647 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200648
649 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300650 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200651 break;
652
653 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300654 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300655 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 snd_card_asihpi_pcm_timer_start(substream);
657 break;
658 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300659 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200660 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300661 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200662 break;
663 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300664 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665 return -EINVAL;
666 }
667
668 return 0;
669}
670
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200671/*algorithm outline
672 Without linking degenerates to getting single stream pos etc
673 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
674*/
675/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300676pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200677for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300678 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300680 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200681}
682timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
683for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300684 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300685 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200686 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300687 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200688 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300689 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200690 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300691 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200692 }
693 }
694 snd_pcm_period_elapsed(s);
695 }
696}
697*/
698
699/** Minimum of 2 modulo values. Works correctly when the difference between
700* the values is less than half the modulus
701*/
702static inline unsigned int modulo_min(unsigned int a, unsigned int b,
703 unsigned long int modulus)
704{
705 unsigned int result;
706 if (((a-b) % modulus) < (modulus/2))
707 result = b;
708 else
709 result = a;
710
711 return result;
712}
713
714/** Timer function, equivalent to interrupt service routine for cards
715*/
716static void snd_card_asihpi_timer_function(unsigned long data)
717{
718 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300719 struct snd_pcm_substream *substream = dpcm->substream;
720 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200721 struct snd_pcm_runtime *runtime;
722 struct snd_pcm_substream *s;
723 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300724 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200725 unsigned int remdata, xfercount, next_jiffies;
726 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300727 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200728 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300729 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200730 char name[16];
731
732 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200733
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200734 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300735
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200736 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300737 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200738 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
739 runtime = s->runtime;
740
741 if (snd_pcm_substream_chip(s) != card)
742 continue;
743
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300744 /* don't link Cap and Play */
745 if (substream->stream != s->stream)
746 continue;
747
748 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200749 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300750 &buffer_size, &bytes_avail,
751 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200752
753 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300754 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200756 if (!card->can_dma)
757 on_card_bytes = bytes_avail;
758
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300759 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300760 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300761 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300762 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300763 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300764 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200765 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300766 }
767 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300768 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300769 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200770 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300771 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100772 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200773 continue;
774 }
775 } else {
776 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300777 }
778 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300779 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200780
781 if (first) {
782 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300783 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300784 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200785 first = 0;
786 } else {
787 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300788 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200789 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300790 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200791 newdata);
792 }
793
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200794 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200795 (unsigned long)frames_to_bytes(runtime,
796 runtime->status->hw_ptr),
797 (unsigned long)frames_to_bytes(runtime,
798 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300799
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200800 snd_printdd("%d S=%d, "
801 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
802 "aux=0x%04X space=0x%04X\n",
803 s->number, state,
804 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
805 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300806 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300807 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200808 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300809 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200810
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300811 remdata = newdata % dpcm->period_bytes;
812 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300813 /* come back when on_card_bytes has decreased enough to allow
814 write to happen, or when data has been consumed to make another
815 period
816 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300817 if (xfercount && (on_card_bytes > dpcm->period_bytes))
818 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300819 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300820 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300821
822 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200823 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200824 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300825 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
826
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300827 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200828 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200829
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300830 /* don't link Cap and Play */
831 if (substream->stream != s->stream)
832 continue;
833
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300834 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
835
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200836 if (xfercount &&
837 /* Limit use of on card fifo for playback */
838 ((on_card_bytes <= ds->period_bytes) ||
839 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
840
841 {
842
843 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
844 unsigned int xfer1, xfer2;
845 char *pd = &s->runtime->dma_area[buf_ofs];
846
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200847 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200848 xfer1 = xfercount;
849 xfer2 = 0;
850 } else {
851 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
852 xfer2 = xfercount - xfer1;
853 }
854
855 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
856 snd_printddd("P%d write1 0x%04X 0x%04X\n",
857 s->number, xfer1, buf_ofs);
858 hpi_handle_error(
859 hpi_outstream_write_buf(
860 ds->h_stream, pd, xfer1,
861 &ds->format));
862
863 if (xfer2) {
864 pd = s->runtime->dma_area;
865
866 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200868 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200869 hpi_handle_error(
870 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200871 ds->h_stream, pd,
872 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200873 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200874 }
875 } else {
876 snd_printddd("C%d read1 0x%04x\n",
877 s->number, xfer1);
878 hpi_handle_error(
879 hpi_instream_read_buf(
880 ds->h_stream,
881 pd, xfer1));
882 if (xfer2) {
883 pd = s->runtime->dma_area;
884 snd_printddd("C%d read2 0x%04x\n",
885 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200886 hpi_handle_error(
887 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300888 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200889 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200890 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200891 }
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300892 ds->pcm_buf_host_rw_ofs += xfercount;
893 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200894 snd_pcm_period_elapsed(s);
895 }
896 }
897
898 if (dpcm->respawn_timer)
899 add_timer(&dpcm->timer);
900}
901
902/***************************** PLAYBACK OPS ****************/
903static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
904 unsigned int cmd, void *arg)
905{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300906 char name[16];
907 snd_pcm_debug_name(substream, name, sizeof(name));
908 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200909 return snd_pcm_lib_ioctl(substream, cmd, arg);
910}
911
912static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
913 substream)
914{
915 struct snd_pcm_runtime *runtime = substream->runtime;
916 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
917
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200918 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200919
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300920 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300921 dpcm->pcm_buf_host_rw_ofs = 0;
922 dpcm->pcm_buf_dma_ofs = 0;
923 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200924 return 0;
925}
926
927static snd_pcm_uframes_t
928snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
929{
930 struct snd_pcm_runtime *runtime = substream->runtime;
931 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
932 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300933 char name[16];
934 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200935
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300936 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300937 snd_printddd("%s pointer = 0x%04lx\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200938 return ptr;
939}
940
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300941static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
942 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200943{
944 struct hpi_format hpi_format;
945 u16 format;
946 u16 err;
947 u32 h_control;
948 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300949 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200950
951 /* on cards without SRC, must query at valid rate,
952 * maybe set by external sync
953 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300954 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200955 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
956 HPI_CONTROL_SAMPLECLOCK, &h_control);
957
958 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300959 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200960 &sample_rate);
961
962 for (format = HPI_FORMAT_PCM8_UNSIGNED;
963 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +1300964 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
965 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200966 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +1300967 err = hpi_outstream_query_format(h_stream, &hpi_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200968 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +0200969 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200970 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300971 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200972}
973
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200974static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
975{
976 struct snd_pcm_runtime *runtime = substream->runtime;
977 struct snd_card_asihpi_pcm *dpcm;
978 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300979 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200980 int err;
981
982 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
983 if (dpcm == NULL)
984 return -ENOMEM;
985
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300986 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200987 substream->number, &dpcm->h_stream);
988 hpi_handle_error(err);
989 if (err)
990 kfree(dpcm);
991 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
992 return -EBUSY;
993 if (err)
994 return -EIO;
995
996 /*? also check ASI5000 samplerate source
997 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300998 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200999 */
1000
1001 init_timer(&dpcm->timer);
1002 dpcm->timer.data = (unsigned long) dpcm;
1003 dpcm->timer.function = snd_card_asihpi_timer_function;
1004 dpcm->substream = substream;
1005 runtime->private_data = dpcm;
1006 runtime->private_free = snd_card_asihpi_runtime_free;
1007
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001008 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
1009 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1010 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001011 /*?snd_card_asihpi_playback.period_bytes_min =
1012 card->out_max_chans * 4096; */
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001013 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1014 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1015 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1016 /* snd_card_asihpi_playback.fifo_size = 0; */
1017 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1018 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1019 snd_card_asihpi_playback.formats =
1020 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001021
1022 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1023
1024 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1025 SNDRV_PCM_INFO_DOUBLE |
1026 SNDRV_PCM_INFO_BATCH |
1027 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001028 SNDRV_PCM_INFO_PAUSE |
1029 SNDRV_PCM_INFO_MMAP |
1030 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001031
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001032 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001033 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001034 snd_pcm_set_sync(substream);
1035 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001036
1037 /* struct is copied, so can create initializer dynamically */
1038 runtime->hw = snd_card_asihpi_playback;
1039
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001040 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001041 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1042 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1043 if (err < 0)
1044 return err;
1045
1046 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1047 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001049 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001050 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001051
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001052 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001053
1054 return 0;
1055}
1056
1057static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1058{
1059 struct snd_pcm_runtime *runtime = substream->runtime;
1060 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1061
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001062 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001063 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001064
1065 return 0;
1066}
1067
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001068static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1069 .open = snd_card_asihpi_playback_open,
1070 .close = snd_card_asihpi_playback_close,
1071 .ioctl = snd_card_asihpi_playback_ioctl,
1072 .hw_params = snd_card_asihpi_pcm_hw_params,
1073 .hw_free = snd_card_asihpi_hw_free,
1074 .prepare = snd_card_asihpi_playback_prepare,
1075 .trigger = snd_card_asihpi_trigger,
1076 .pointer = snd_card_asihpi_playback_pointer,
1077};
1078
1079/***************************** CAPTURE OPS ****************/
1080static snd_pcm_uframes_t
1081snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1082{
1083 struct snd_pcm_runtime *runtime = substream->runtime;
1084 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1085
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001086 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001087 substream->number, dpcm->pcm_buf_dma_ofs);
1088 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001089 for the capture position, because those samples aren't yet in
1090 the local buffer available for reading.
1091 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001092 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001093}
1094
1095static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1096 unsigned int cmd, void *arg)
1097{
1098 return snd_pcm_lib_ioctl(substream, cmd, arg);
1099}
1100
1101static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1102{
1103 struct snd_pcm_runtime *runtime = substream->runtime;
1104 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1105
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001106 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001107 dpcm->pcm_buf_host_rw_ofs = 0;
1108 dpcm->pcm_buf_dma_ofs = 0;
1109 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001110
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001111 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001112 return 0;
1113}
1114
1115
1116
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001117static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1118 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001119{
1120 struct hpi_format hpi_format;
1121 u16 format;
1122 u16 err;
1123 u32 h_control;
1124 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001125 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001126
1127 /* on cards without SRC, must query at valid rate,
1128 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001129 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001130 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1131 HPI_CONTROL_SAMPLECLOCK, &h_control);
1132
1133 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001134 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001135 &sample_rate);
1136
1137 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1138 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1139
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001140 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1141 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001142 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001143 err = hpi_instream_query_format(h_stream, &hpi_format);
Eldad Zack167d0a12013-04-23 01:00:42 +02001144 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001145 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001146 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001147 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001148}
1149
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001150static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1151{
1152 struct snd_pcm_runtime *runtime = substream->runtime;
1153 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1154 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001155 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001156 int err;
1157
1158 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1159 if (dpcm == NULL)
1160 return -ENOMEM;
1161
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001162 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001163 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001164
1165 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001166 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001167 substream->number, &dpcm->h_stream));
1168 if (err)
1169 kfree(dpcm);
1170 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1171 return -EBUSY;
1172 if (err)
1173 return -EIO;
1174
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001175 init_timer(&dpcm->timer);
1176 dpcm->timer.data = (unsigned long) dpcm;
1177 dpcm->timer.function = snd_card_asihpi_timer_function;
1178 dpcm->substream = substream;
1179 runtime->private_data = dpcm;
1180 runtime->private_free = snd_card_asihpi_runtime_free;
1181
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001182 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
1183 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1184 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1185 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1186 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1187 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1188 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001189 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001190 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001191 snd_card_asihpi_capture.formats =
1192 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001193 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001194 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1195 SNDRV_PCM_INFO_MMAP |
1196 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001197
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001198 if (card->support_grouping)
1199 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1200
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001201 runtime->hw = snd_card_asihpi_capture;
1202
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001203 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001204 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1205 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1206 if (err < 0)
1207 return err;
1208
1209 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1210 card->update_interval_frames);
1211 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1212 card->update_interval_frames * 2, UINT_MAX);
1213
1214 snd_pcm_set_sync(substream);
1215
1216 return 0;
1217}
1218
1219static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1220{
1221 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1222
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001223 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001224 return 0;
1225}
1226
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001227static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1228 .open = snd_card_asihpi_capture_open,
1229 .close = snd_card_asihpi_capture_close,
1230 .ioctl = snd_card_asihpi_capture_ioctl,
1231 .hw_params = snd_card_asihpi_pcm_hw_params,
1232 .hw_free = snd_card_asihpi_hw_free,
1233 .prepare = snd_card_asihpi_capture_prepare,
1234 .trigger = snd_card_asihpi_trigger,
1235 .pointer = snd_card_asihpi_capture_pointer,
1236};
1237
Bill Pembertone23e7a12012-12-06 12:35:10 -05001238static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001239{
1240 struct snd_pcm *pcm;
1241 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001242 u16 num_instreams, num_outstreams, x16;
1243 u32 x32;
1244
1245 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1246 &num_outstreams, &num_instreams,
1247 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001248
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001249 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001250 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001251 if (err < 0)
1252 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001253
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001254 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001255 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1256 &snd_card_asihpi_playback_mmap_ops);
1257 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1258 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001259
1260 pcm->private_data = asihpi;
1261 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001262 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001263
1264 /*? do we want to emulate MMAP for non-BBM cards?
1265 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1266 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1267 snd_dma_pci_data(asihpi->pci),
1268 64*1024, BUFFER_BYTES_MAX);
1269
1270 return 0;
1271}
1272
1273/***************************** MIXER CONTROLS ****************/
1274struct hpi_control {
1275 u32 h_control;
1276 u16 control_type;
1277 u16 src_node_type;
1278 u16 src_node_index;
1279 u16 dst_node_type;
1280 u16 dst_node_index;
1281 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001282 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001283};
1284
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001285static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001286 "invalid",
1287 "AM",
1288 "FM mono",
1289 "TV NTSC-M",
1290 "FM stereo",
1291 "AUX",
1292 "TV PAL BG",
1293 "TV PAL I",
1294 "TV PAL DK",
1295 "TV SECAM",
1296};
1297
1298compile_time_assert(
1299 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1300 (HPI_TUNER_BAND_LAST+1)),
1301 assert_tuner_band_names_size);
1302
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001303static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001304 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001305 "PCM",
1306 "Line",
1307 "Digital",
1308 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001309 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001310 "Clock",
1311 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001312 "Mic",
1313 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001314 "Analog",
1315 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001316 "RTP",
Eliot Blennerhassett502f2712011-12-22 13:38:39 +13001317 "Internal"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001318};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001319
1320compile_time_assert(
1321 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001322 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323 assert_src_names_size);
1324
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001325static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001326 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001327 "PCM",
1328 "Line",
1329 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001330 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001331 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001332 "Net",
1333 "Analog",
1334 "RTP",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001336
1337compile_time_assert(
1338 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001339 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001340 assert_dst_names_size);
1341
1342static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1343 struct snd_card_asihpi *asihpi)
1344{
1345 int err;
1346
1347 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1348 if (err < 0)
1349 return err;
1350 else if (mixer_dump)
1351 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1352
1353 return 0;
1354}
1355
1356/* Convert HPI control name and location into ALSA control name */
1357static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1358 struct hpi_control *hpi_ctl,
1359 char *name)
1360{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001361 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001362 memset(snd_control, 0, sizeof(*snd_control));
1363 snd_control->name = hpi_ctl->name;
1364 snd_control->private_value = hpi_ctl->h_control;
1365 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1366 snd_control->index = 0;
1367
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001368 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1369 dir = ""; /* clock is neither capture nor playback */
1370 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001371 dir = "Capture "; /* On or towards a PCM capture destination*/
1372 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1373 (!hpi_ctl->dst_node_type))
1374 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001375 else if (hpi_ctl->src_node_type &&
1376 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001377 (hpi_ctl->dst_node_type))
1378 dir = "Monitor Playback "; /* Between an input and an output */
1379 else
1380 dir = "Playback "; /* PCM Playback source, or output node */
1381
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001382 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001383 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001384 asihpi_src_names[hpi_ctl->src_node_type],
1385 hpi_ctl->src_node_index,
1386 asihpi_dst_names[hpi_ctl->dst_node_type],
1387 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001388 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001389 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001390 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001391 asihpi_dst_names[hpi_ctl->dst_node_type],
1392 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001393 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396 asihpi_src_names[hpi_ctl->src_node_type],
1397 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001398 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001399 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001400 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1401 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402}
1403
1404/*------------------------------------------------------------
1405 Volume controls
1406 ------------------------------------------------------------*/
1407#define VOL_STEP_mB 1
1408static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_info *uinfo)
1410{
1411 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001412 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001413 u16 err;
1414 /* native gains are in millibels */
1415 short min_gain_mB;
1416 short max_gain_mB;
1417 short step_gain_mB;
1418
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001419 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001420 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1421 if (err) {
1422 max_gain_mB = 0;
1423 min_gain_mB = -10000;
1424 step_gain_mB = VOL_STEP_mB;
1425 }
1426
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001427 err = hpi_meter_query_channels(h_control, &count);
1428 if (err)
1429 count = HPI_MAX_CHANNELS;
1430
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001431 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001432 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001433 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1434 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1435 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1436 return 0;
1437}
1438
1439static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1440 struct snd_ctl_elem_value *ucontrol)
1441{
1442 u32 h_control = kcontrol->private_value;
1443 short an_gain_mB[HPI_MAX_CHANNELS];
1444
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001445 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001446 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1447 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1448
1449 return 0;
1450}
1451
1452static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_value *ucontrol)
1454{
1455 int change;
1456 u32 h_control = kcontrol->private_value;
1457 short an_gain_mB[HPI_MAX_CHANNELS];
1458
1459 an_gain_mB[0] =
1460 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1461 an_gain_mB[1] =
1462 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1463 /* change = asihpi->mixer_volume[addr][0] != left ||
1464 asihpi->mixer_volume[addr][1] != right;
1465 */
1466 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001467 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001468 return change;
1469}
1470
1471static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1472
Takashi Iwai000477a2011-07-22 07:57:44 +02001473#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001474
1475static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
1477{
1478 u32 h_control = kcontrol->private_value;
1479 u32 mute;
1480
1481 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1482 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1483
1484 return 0;
1485}
1486
1487static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1488 struct snd_ctl_elem_value *ucontrol)
1489{
1490 u32 h_control = kcontrol->private_value;
1491 int change = 1;
1492 /* HPI currently only supports all or none muting of multichannel volume
1493 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1494 */
1495 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1496 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1497 return change;
1498}
1499
Bill Pembertone23e7a12012-12-06 12:35:10 -05001500static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1501 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001502{
1503 struct snd_card *card = asihpi->card;
1504 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001505 int err;
1506 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001507
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001508 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001509 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1510 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1511 snd_control.info = snd_asihpi_volume_info;
1512 snd_control.get = snd_asihpi_volume_get;
1513 snd_control.put = snd_asihpi_volume_put;
1514 snd_control.tlv.p = db_scale_100;
1515
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001516 err = ctl_add(card, &snd_control, asihpi);
1517 if (err)
1518 return err;
1519
1520 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1521 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1522 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1523 snd_control.info = snd_asihpi_volume_mute_info;
1524 snd_control.get = snd_asihpi_volume_mute_get;
1525 snd_control.put = snd_asihpi_volume_mute_put;
1526 err = ctl_add(card, &snd_control, asihpi);
1527 }
1528 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001529}
1530
1531/*------------------------------------------------------------
1532 Level controls
1533 ------------------------------------------------------------*/
1534static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1535 struct snd_ctl_elem_info *uinfo)
1536{
1537 u32 h_control = kcontrol->private_value;
1538 u16 err;
1539 short min_gain_mB;
1540 short max_gain_mB;
1541 short step_gain_mB;
1542
1543 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001544 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001545 &max_gain_mB, &step_gain_mB);
1546 if (err) {
1547 max_gain_mB = 2400;
1548 min_gain_mB = -1000;
1549 step_gain_mB = 100;
1550 }
1551
1552 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1553 uinfo->count = 2;
1554 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1555 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1556 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1557 return 0;
1558}
1559
1560static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1561 struct snd_ctl_elem_value *ucontrol)
1562{
1563 u32 h_control = kcontrol->private_value;
1564 short an_gain_mB[HPI_MAX_CHANNELS];
1565
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001566 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001567 ucontrol->value.integer.value[0] =
1568 an_gain_mB[0] / HPI_UNITS_PER_dB;
1569 ucontrol->value.integer.value[1] =
1570 an_gain_mB[1] / HPI_UNITS_PER_dB;
1571
1572 return 0;
1573}
1574
1575static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1576 struct snd_ctl_elem_value *ucontrol)
1577{
1578 int change;
1579 u32 h_control = kcontrol->private_value;
1580 short an_gain_mB[HPI_MAX_CHANNELS];
1581
1582 an_gain_mB[0] =
1583 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1584 an_gain_mB[1] =
1585 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1586 /* change = asihpi->mixer_level[addr][0] != left ||
1587 asihpi->mixer_level[addr][1] != right;
1588 */
1589 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001590 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001591 return change;
1592}
1593
1594static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1595
Bill Pembertone23e7a12012-12-06 12:35:10 -05001596static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1597 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001598{
1599 struct snd_card *card = asihpi->card;
1600 struct snd_kcontrol_new snd_control;
1601
1602 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001603 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001604 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1605 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1606 snd_control.info = snd_asihpi_level_info;
1607 snd_control.get = snd_asihpi_level_get;
1608 snd_control.put = snd_asihpi_level_put;
1609 snd_control.tlv.p = db_scale_level;
1610
1611 return ctl_add(card, &snd_control, asihpi);
1612}
1613
1614/*------------------------------------------------------------
1615 AESEBU controls
1616 ------------------------------------------------------------*/
1617
1618/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001619static const char * const asihpi_aesebu_format_names[] = {
1620 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001621
1622static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1623 struct snd_ctl_elem_info *uinfo)
1624{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001625 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001626}
1627
1628static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1629 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001630 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001631{
1632 u32 h_control = kcontrol->private_value;
1633 u16 source, err;
1634
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001635 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001636
1637 /* default to N/A */
1638 ucontrol->value.enumerated.item[0] = 0;
1639 /* return success but set the control to N/A */
1640 if (err)
1641 return 0;
1642 if (source == HPI_AESEBU_FORMAT_SPDIF)
1643 ucontrol->value.enumerated.item[0] = 1;
1644 if (source == HPI_AESEBU_FORMAT_AESEBU)
1645 ucontrol->value.enumerated.item[0] = 2;
1646
1647 return 0;
1648}
1649
1650static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1651 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001652 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001653{
1654 u32 h_control = kcontrol->private_value;
1655
1656 /* default to S/PDIF */
1657 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1658
1659 if (ucontrol->value.enumerated.item[0] == 1)
1660 source = HPI_AESEBU_FORMAT_SPDIF;
1661 if (ucontrol->value.enumerated.item[0] == 2)
1662 source = HPI_AESEBU_FORMAT_AESEBU;
1663
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001664 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001665 return -EINVAL;
1666
1667 return 1;
1668}
1669
1670static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1671 struct snd_ctl_elem_value *ucontrol) {
1672 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001673 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001674}
1675
1676static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1677 struct snd_ctl_elem_value *ucontrol) {
1678 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001679 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001680}
1681
1682static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1683 struct snd_ctl_elem_info *uinfo)
1684{
1685 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1686 uinfo->count = 1;
1687
1688 uinfo->value.integer.min = 0;
1689 uinfo->value.integer.max = 0X1F;
1690 uinfo->value.integer.step = 1;
1691
1692 return 0;
1693}
1694
1695static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1696 struct snd_ctl_elem_value *ucontrol) {
1697
1698 u32 h_control = kcontrol->private_value;
1699 u16 status;
1700
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001701 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1702 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001703 ucontrol->value.integer.value[0] = status;
1704 return 0;
1705}
1706
Bill Pembertone23e7a12012-12-06 12:35:10 -05001707static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1708 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001709{
1710 struct snd_card *card = asihpi->card;
1711 struct snd_kcontrol_new snd_control;
1712
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001713 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001714 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1715 snd_control.info = snd_asihpi_aesebu_format_info;
1716 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1717 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1718
1719
1720 if (ctl_add(card, &snd_control, asihpi) < 0)
1721 return -EINVAL;
1722
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001723 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001724 snd_control.access =
1725 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1726 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1727 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1728
1729 return ctl_add(card, &snd_control, asihpi);
1730}
1731
1732static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1733 struct snd_ctl_elem_value *ucontrol) {
1734 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001735 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001736}
1737
1738static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1739 struct snd_ctl_elem_value *ucontrol) {
1740 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001741 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001742}
1743
1744
Bill Pembertone23e7a12012-12-06 12:35:10 -05001745static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1746 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001747{
1748 struct snd_card *card = asihpi->card;
1749 struct snd_kcontrol_new snd_control;
1750
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001751 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001752 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1753 snd_control.info = snd_asihpi_aesebu_format_info;
1754 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1755 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1756
1757 return ctl_add(card, &snd_control, asihpi);
1758}
1759
1760/*------------------------------------------------------------
1761 Tuner controls
1762 ------------------------------------------------------------*/
1763
1764/* Gain */
1765
1766static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1767 struct snd_ctl_elem_info *uinfo)
1768{
1769 u32 h_control = kcontrol->private_value;
1770 u16 err;
1771 short idx;
1772 u16 gain_range[3];
1773
1774 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001775 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001776 idx, &gain_range[idx]);
1777 if (err != 0)
1778 return err;
1779 }
1780
1781 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1782 uinfo->count = 1;
1783 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1784 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1785 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1786 return 0;
1787}
1788
1789static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1790 struct snd_ctl_elem_value *ucontrol)
1791{
1792 /*
1793 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1794 */
1795 u32 h_control = kcontrol->private_value;
1796 short gain;
1797
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001798 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001799 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1800
1801 return 0;
1802}
1803
1804static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1805 struct snd_ctl_elem_value *ucontrol)
1806{
1807 /*
1808 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1809 */
1810 u32 h_control = kcontrol->private_value;
1811 short gain;
1812
1813 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001814 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001815
1816 return 1;
1817}
1818
1819/* Band */
1820
1821static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1822 u16 *band_list, u32 len) {
1823 u32 h_control = kcontrol->private_value;
1824 u16 err = 0;
1825 u32 i;
1826
1827 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001828 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001829 h_control, i, &band_list[i]);
1830 if (err != 0)
1831 break;
1832 }
1833
1834 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1835 return -EIO;
1836
1837 return i;
1838}
1839
1840static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1841 struct snd_ctl_elem_info *uinfo)
1842{
1843 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1844 int num_bands = 0;
1845
1846 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1847 HPI_TUNER_BAND_LAST);
1848
1849 if (num_bands < 0)
1850 return num_bands;
1851
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001852 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001853}
1854
1855static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1856 struct snd_ctl_elem_value *ucontrol)
1857{
1858 u32 h_control = kcontrol->private_value;
1859 /*
1860 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1861 */
1862 u16 band, idx;
1863 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1864 u32 num_bands = 0;
1865
1866 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1867 HPI_TUNER_BAND_LAST);
1868
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001869 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001870
1871 ucontrol->value.enumerated.item[0] = -1;
1872 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1873 if (tuner_bands[idx] == band) {
1874 ucontrol->value.enumerated.item[0] = idx;
1875 break;
1876 }
1877
1878 return 0;
1879}
1880
1881static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *ucontrol)
1883{
1884 /*
1885 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1886 */
1887 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001888 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001889 u16 band;
1890 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1891 u32 num_bands = 0;
1892
1893 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1894 HPI_TUNER_BAND_LAST);
1895
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001896 idx = ucontrol->value.enumerated.item[0];
1897 if (idx >= ARRAY_SIZE(tuner_bands))
1898 idx = ARRAY_SIZE(tuner_bands) - 1;
1899 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001900 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001901
1902 return 1;
1903}
1904
1905/* Freq */
1906
1907static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1908 struct snd_ctl_elem_info *uinfo)
1909{
1910 u32 h_control = kcontrol->private_value;
1911 u16 err;
1912 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1913 u16 num_bands = 0, band_iter, idx;
1914 u32 freq_range[3], temp_freq_range[3];
1915
1916 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1917 HPI_TUNER_BAND_LAST);
1918
1919 freq_range[0] = INT_MAX;
1920 freq_range[1] = 0;
1921 freq_range[2] = INT_MAX;
1922
1923 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1924 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001925 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001926 idx, tuner_bands[band_iter],
1927 &temp_freq_range[idx]);
1928 if (err != 0)
1929 return err;
1930 }
1931
1932 /* skip band with bogus stepping */
1933 if (temp_freq_range[2] <= 0)
1934 continue;
1935
1936 if (temp_freq_range[0] < freq_range[0])
1937 freq_range[0] = temp_freq_range[0];
1938 if (temp_freq_range[1] > freq_range[1])
1939 freq_range[1] = temp_freq_range[1];
1940 if (temp_freq_range[2] < freq_range[2])
1941 freq_range[2] = temp_freq_range[2];
1942 }
1943
1944 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1945 uinfo->count = 1;
1946 uinfo->value.integer.min = ((int)freq_range[0]);
1947 uinfo->value.integer.max = ((int)freq_range[1]);
1948 uinfo->value.integer.step = ((int)freq_range[2]);
1949 return 0;
1950}
1951
1952static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1953 struct snd_ctl_elem_value *ucontrol)
1954{
1955 u32 h_control = kcontrol->private_value;
1956 u32 freq;
1957
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001958 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001959 ucontrol->value.integer.value[0] = freq;
1960
1961 return 0;
1962}
1963
1964static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1965 struct snd_ctl_elem_value *ucontrol)
1966{
1967 u32 h_control = kcontrol->private_value;
1968 u32 freq;
1969
1970 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001971 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001972
1973 return 1;
1974}
1975
1976/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001977static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1978 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001979{
1980 struct snd_card *card = asihpi->card;
1981 struct snd_kcontrol_new snd_control;
1982
1983 snd_control.private_value = hpi_ctl->h_control;
1984 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1985
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001986 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001987 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001988 snd_control.info = snd_asihpi_tuner_gain_info;
1989 snd_control.get = snd_asihpi_tuner_gain_get;
1990 snd_control.put = snd_asihpi_tuner_gain_put;
1991
1992 if (ctl_add(card, &snd_control, asihpi) < 0)
1993 return -EINVAL;
1994 }
1995
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001996 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001997 snd_control.info = snd_asihpi_tuner_band_info;
1998 snd_control.get = snd_asihpi_tuner_band_get;
1999 snd_control.put = snd_asihpi_tuner_band_put;
2000
2001 if (ctl_add(card, &snd_control, asihpi) < 0)
2002 return -EINVAL;
2003
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002004 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002005 snd_control.info = snd_asihpi_tuner_freq_info;
2006 snd_control.get = snd_asihpi_tuner_freq_get;
2007 snd_control.put = snd_asihpi_tuner_freq_put;
2008
2009 return ctl_add(card, &snd_control, asihpi);
2010}
2011
2012/*------------------------------------------------------------
2013 Meter controls
2014 ------------------------------------------------------------*/
2015static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2016 struct snd_ctl_elem_info *uinfo)
2017{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002018 u32 h_control = kcontrol->private_value;
2019 u32 count;
2020 u16 err;
2021 err = hpi_meter_query_channels(h_control, &count);
2022 if (err)
2023 count = HPI_MAX_CHANNELS;
2024
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002025 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002026 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002027 uinfo->value.integer.min = 0;
2028 uinfo->value.integer.max = 0x7FFFFFFF;
2029 return 0;
2030}
2031
2032/* linear values for 10dB steps */
2033static int log2lin[] = {
2034 0x7FFFFFFF, /* 0dB */
2035 679093956,
2036 214748365,
2037 67909396,
2038 21474837,
2039 6790940,
2040 2147484, /* -60dB */
2041 679094,
2042 214748, /* -80 */
2043 67909,
2044 21475, /* -100 */
2045 6791,
2046 2147,
2047 679,
2048 214,
2049 68,
2050 21,
2051 7,
2052 2
2053};
2054
2055static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2056 struct snd_ctl_elem_value *ucontrol)
2057{
2058 u32 h_control = kcontrol->private_value;
2059 short an_gain_mB[HPI_MAX_CHANNELS], i;
2060 u16 err;
2061
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002062 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002063
2064 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2065 if (err) {
2066 ucontrol->value.integer.value[i] = 0;
2067 } else if (an_gain_mB[i] >= 0) {
2068 ucontrol->value.integer.value[i] =
2069 an_gain_mB[i] << 16;
2070 } else {
2071 /* -ve is log value in millibels < -60dB,
2072 * convert to (roughly!) linear,
2073 */
2074 ucontrol->value.integer.value[i] =
2075 log2lin[an_gain_mB[i] / -1000];
2076 }
2077 }
2078 return 0;
2079}
2080
Bill Pembertone23e7a12012-12-06 12:35:10 -05002081static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2082 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002083{
2084 struct snd_card *card = asihpi->card;
2085 struct snd_kcontrol_new snd_control;
2086
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002087 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002088 snd_control.access =
2089 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2090 snd_control.info = snd_asihpi_meter_info;
2091 snd_control.get = snd_asihpi_meter_get;
2092
2093 snd_control.index = subidx;
2094
2095 return ctl_add(card, &snd_control, asihpi);
2096}
2097
2098/*------------------------------------------------------------
2099 Multiplexer controls
2100 ------------------------------------------------------------*/
2101static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2102{
2103 u32 h_control = snd_control->private_value;
2104 struct hpi_control hpi_ctl;
2105 int s, err;
2106 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002107 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002108 &hpi_ctl.
2109 src_node_type,
2110 &hpi_ctl.
2111 src_node_index);
2112 if (err)
2113 break;
2114 }
2115 return s;
2116}
2117
2118static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2119 struct snd_ctl_elem_info *uinfo)
2120{
2121 int err;
2122 u16 src_node_type, src_node_index;
2123 u32 h_control = kcontrol->private_value;
2124
2125 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2126 uinfo->count = 1;
2127 uinfo->value.enumerated.items =
2128 snd_card_asihpi_mux_count_sources(kcontrol);
2129
2130 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2131 uinfo->value.enumerated.item =
2132 uinfo->value.enumerated.items - 1;
2133
2134 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002135 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002136 uinfo->value.enumerated.item,
2137 &src_node_type, &src_node_index);
2138
2139 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002140 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002141 src_node_index);
2142 return 0;
2143}
2144
2145static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2146 struct snd_ctl_elem_value *ucontrol)
2147{
2148 u32 h_control = kcontrol->private_value;
2149 u16 source_type, source_index;
2150 u16 src_node_type, src_node_index;
2151 int s;
2152
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002153 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002154 &source_type, &source_index));
2155 /* Should cache this search result! */
2156 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002157 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002158 &src_node_type, &src_node_index))
2159 break;
2160
2161 if ((source_type == src_node_type)
2162 && (source_index == src_node_index)) {
2163 ucontrol->value.enumerated.item[0] = s;
2164 return 0;
2165 }
2166 }
2167 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002168 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002169 h_control, source_type, source_index);
2170 ucontrol->value.enumerated.item[0] = 0;
2171 return 0;
2172}
2173
2174static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2175 struct snd_ctl_elem_value *ucontrol)
2176{
2177 int change;
2178 u32 h_control = kcontrol->private_value;
2179 u16 source_type, source_index;
2180 u16 e;
2181
2182 change = 1;
2183
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002184 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002185 ucontrol->value.enumerated.item[0],
2186 &source_type, &source_index);
2187 if (!e)
2188 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002189 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002190 source_type, source_index));
2191 return change;
2192}
2193
2194
Bill Pembertone23e7a12012-12-06 12:35:10 -05002195static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2196 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002197{
2198 struct snd_card *card = asihpi->card;
2199 struct snd_kcontrol_new snd_control;
2200
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002201 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002202 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2203 snd_control.info = snd_asihpi_mux_info;
2204 snd_control.get = snd_asihpi_mux_get;
2205 snd_control.put = snd_asihpi_mux_put;
2206
2207 return ctl_add(card, &snd_control, asihpi);
2208
2209}
2210
2211/*------------------------------------------------------------
2212 Channel mode controls
2213 ------------------------------------------------------------*/
2214static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2215 struct snd_ctl_elem_info *uinfo)
2216{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002217 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2218 "invalid",
2219 "Normal", "Swap",
2220 "From Left", "From Right",
2221 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002222 };
2223
2224 u32 h_control = kcontrol->private_value;
2225 u16 mode;
2226 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002227 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002228 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002229
2230 /* HPI channel mode values can be from 1 to 6
2231 Some adapters only support a contiguous subset
2232 */
2233 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002234 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002235 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002236 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002237 valid_modes++;
2238 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002239
Takashi Iwai74eeb142012-01-09 18:26:05 +01002240 if (!valid_modes)
2241 return -EINVAL;
2242
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002243 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002244}
2245
2246static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2247 struct snd_ctl_elem_value *ucontrol)
2248{
2249 u32 h_control = kcontrol->private_value;
2250 u16 mode;
2251
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002252 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002253 mode = 1;
2254
2255 ucontrol->value.enumerated.item[0] = mode - 1;
2256
2257 return 0;
2258}
2259
2260static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2261 struct snd_ctl_elem_value *ucontrol)
2262{
2263 int change;
2264 u32 h_control = kcontrol->private_value;
2265
2266 change = 1;
2267
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002268 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002269 ucontrol->value.enumerated.item[0] + 1));
2270 return change;
2271}
2272
2273
Bill Pembertone23e7a12012-12-06 12:35:10 -05002274static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2275 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002276{
2277 struct snd_card *card = asihpi->card;
2278 struct snd_kcontrol_new snd_control;
2279
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002280 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002281 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2282 snd_control.info = snd_asihpi_cmode_info;
2283 snd_control.get = snd_asihpi_cmode_get;
2284 snd_control.put = snd_asihpi_cmode_put;
2285
2286 return ctl_add(card, &snd_control, asihpi);
2287}
2288
2289/*------------------------------------------------------------
2290 Sampleclock source controls
2291 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002292static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2293 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2294 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2295 "Prev Module",
2296 "Digital2", "Digital3", "Digital4", "Digital5",
2297 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002298
2299static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2300 struct snd_ctl_elem_info *uinfo)
2301{
2302 struct snd_card_asihpi *asihpi =
2303 (struct snd_card_asihpi *)(kcontrol->private_data);
2304 struct clk_cache *clkcache = &asihpi->cc;
2305 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2306 uinfo->count = 1;
2307 uinfo->value.enumerated.items = clkcache->count;
2308
2309 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2310 uinfo->value.enumerated.item =
2311 uinfo->value.enumerated.items - 1;
2312
2313 strcpy(uinfo->value.enumerated.name,
2314 clkcache->s[uinfo->value.enumerated.item].name);
2315 return 0;
2316}
2317
2318static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2319 struct snd_ctl_elem_value *ucontrol)
2320{
2321 struct snd_card_asihpi *asihpi =
2322 (struct snd_card_asihpi *)(kcontrol->private_data);
2323 struct clk_cache *clkcache = &asihpi->cc;
2324 u32 h_control = kcontrol->private_value;
2325 u16 source, srcindex = 0;
2326 int i;
2327
2328 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002329 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002330 source = 0;
2331
2332 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002333 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002334 srcindex = 0;
2335
2336 for (i = 0; i < clkcache->count; i++)
2337 if ((clkcache->s[i].source == source) &&
2338 (clkcache->s[i].index == srcindex))
2339 break;
2340
2341 ucontrol->value.enumerated.item[0] = i;
2342
2343 return 0;
2344}
2345
2346static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2347 struct snd_ctl_elem_value *ucontrol)
2348{
2349 struct snd_card_asihpi *asihpi =
2350 (struct snd_card_asihpi *)(kcontrol->private_data);
2351 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002352 unsigned int item;
2353 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002354 u32 h_control = kcontrol->private_value;
2355
2356 change = 1;
2357 item = ucontrol->value.enumerated.item[0];
2358 if (item >= clkcache->count)
2359 item = clkcache->count-1;
2360
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002361 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002362 h_control, clkcache->s[item].source));
2363
2364 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002365 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002366 h_control, clkcache->s[item].index));
2367 return change;
2368}
2369
2370/*------------------------------------------------------------
2371 Clkrate controls
2372 ------------------------------------------------------------*/
2373/* Need to change this to enumerated control with list of rates */
2374static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2375 struct snd_ctl_elem_info *uinfo)
2376{
2377 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2378 uinfo->count = 1;
2379 uinfo->value.integer.min = 8000;
2380 uinfo->value.integer.max = 192000;
2381 uinfo->value.integer.step = 100;
2382
2383 return 0;
2384}
2385
2386static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2387 struct snd_ctl_elem_value *ucontrol)
2388{
2389 u32 h_control = kcontrol->private_value;
2390 u32 rate;
2391 u16 e;
2392
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002393 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002394 if (!e)
2395 ucontrol->value.integer.value[0] = rate;
2396 else
2397 ucontrol->value.integer.value[0] = 0;
2398 return 0;
2399}
2400
2401static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2403{
2404 int change;
2405 u32 h_control = kcontrol->private_value;
2406
2407 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2408 asihpi->mixer_clkrate[addr][1] != right;
2409 */
2410 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002411 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002412 ucontrol->value.integer.value[0]));
2413 return change;
2414}
2415
2416static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2417 struct snd_ctl_elem_info *uinfo)
2418{
2419 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2420 uinfo->count = 1;
2421 uinfo->value.integer.min = 8000;
2422 uinfo->value.integer.max = 192000;
2423 uinfo->value.integer.step = 100;
2424
2425 return 0;
2426}
2427
2428static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 u32 h_control = kcontrol->private_value;
2432 u32 rate;
2433 u16 e;
2434
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002435 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002436 if (!e)
2437 ucontrol->value.integer.value[0] = rate;
2438 else
2439 ucontrol->value.integer.value[0] = 0;
2440 return 0;
2441}
2442
Bill Pembertone23e7a12012-12-06 12:35:10 -05002443static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2444 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002445{
2446 struct snd_card *card = asihpi->card;
2447 struct snd_kcontrol_new snd_control;
2448
2449 struct clk_cache *clkcache = &asihpi->cc;
2450 u32 hSC = hpi_ctl->h_control;
2451 int has_aes_in = 0;
2452 int i, j;
2453 u16 source;
2454
2455 snd_control.private_value = hpi_ctl->h_control;
2456
2457 clkcache->has_local = 0;
2458
2459 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002460 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002461 i, &source))
2462 break;
2463 clkcache->s[i].source = source;
2464 clkcache->s[i].index = 0;
2465 clkcache->s[i].name = sampleclock_sources[source];
2466 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2467 has_aes_in = 1;
2468 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2469 clkcache->has_local = 1;
2470 }
2471 if (has_aes_in)
2472 /* already will have picked up index 0 above */
2473 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002474 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002475 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2476 &source))
2477 break;
2478 clkcache->s[i].source =
2479 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2480 clkcache->s[i].index = j;
2481 clkcache->s[i].name = sampleclock_sources[
2482 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2483 i++;
2484 }
2485 clkcache->count = i;
2486
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002487 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002488 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2489 snd_control.info = snd_asihpi_clksrc_info;
2490 snd_control.get = snd_asihpi_clksrc_get;
2491 snd_control.put = snd_asihpi_clksrc_put;
2492 if (ctl_add(card, &snd_control, asihpi) < 0)
2493 return -EINVAL;
2494
2495
2496 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002497 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002498 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2499 snd_control.info = snd_asihpi_clklocal_info;
2500 snd_control.get = snd_asihpi_clklocal_get;
2501 snd_control.put = snd_asihpi_clklocal_put;
2502
2503
2504 if (ctl_add(card, &snd_control, asihpi) < 0)
2505 return -EINVAL;
2506 }
2507
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002508 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002509 snd_control.access =
2510 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2511 snd_control.info = snd_asihpi_clkrate_info;
2512 snd_control.get = snd_asihpi_clkrate_get;
2513
2514 return ctl_add(card, &snd_control, asihpi);
2515}
2516/*------------------------------------------------------------
2517 Mixer
2518 ------------------------------------------------------------*/
2519
Bill Pembertone23e7a12012-12-06 12:35:10 -05002520static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002521{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002522 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002523 unsigned int idx = 0;
2524 unsigned int subindex = 0;
2525 int err;
2526 struct hpi_control hpi_ctl, prev_ctl;
2527
2528 if (snd_BUG_ON(!asihpi))
2529 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002530 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002531 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002532
2533 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002534 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002535 &asihpi->h_mixer);
2536 hpi_handle_error(err);
2537 if (err)
2538 return -err;
2539
Takashi Iwai21896bc2010-06-02 12:08:37 +02002540 memset(&prev_ctl, 0, sizeof(prev_ctl));
2541 prev_ctl.control_type = -1;
2542
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002543 for (idx = 0; idx < 2000; idx++) {
2544 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002545 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002546 idx,
2547 &hpi_ctl.src_node_type,
2548 &hpi_ctl.src_node_index,
2549 &hpi_ctl.dst_node_type,
2550 &hpi_ctl.dst_node_index,
2551 &hpi_ctl.control_type,
2552 &hpi_ctl.h_control);
2553 if (err) {
2554 if (err == HPI_ERROR_CONTROL_DISABLED) {
2555 if (mixer_dump)
2556 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002557 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002558 idx);
2559 continue;
2560 } else
2561 break;
2562
2563 }
2564
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002565 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2566 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002567
2568 /* ASI50xx in SSX mode has multiple meters on the same node.
2569 Use subindex to create distinct ALSA controls
2570 for any duplicated controls.
2571 */
2572 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2573 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2574 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2575 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2576 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2577 subindex++;
2578 else
2579 subindex = 0;
2580
2581 prev_ctl = hpi_ctl;
2582
2583 switch (hpi_ctl.control_type) {
2584 case HPI_CONTROL_VOLUME:
2585 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2586 break;
2587 case HPI_CONTROL_LEVEL:
2588 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2589 break;
2590 case HPI_CONTROL_MULTIPLEXER:
2591 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2592 break;
2593 case HPI_CONTROL_CHANNEL_MODE:
2594 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2595 break;
2596 case HPI_CONTROL_METER:
2597 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2598 break;
2599 case HPI_CONTROL_SAMPLECLOCK:
2600 err = snd_asihpi_sampleclock_add(
2601 asihpi, &hpi_ctl);
2602 break;
2603 case HPI_CONTROL_CONNECTION: /* ignore these */
2604 continue;
2605 case HPI_CONTROL_TUNER:
2606 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2607 break;
2608 case HPI_CONTROL_AESEBU_TRANSMITTER:
2609 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2610 break;
2611 case HPI_CONTROL_AESEBU_RECEIVER:
2612 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2613 break;
2614 case HPI_CONTROL_VOX:
2615 case HPI_CONTROL_BITSTREAM:
2616 case HPI_CONTROL_MICROPHONE:
2617 case HPI_CONTROL_PARAMETRIC_EQ:
2618 case HPI_CONTROL_COMPANDER:
2619 default:
2620 if (mixer_dump)
2621 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002622 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002623 "(%d) %d %d %d %d %d\n",
2624 idx,
2625 hpi_ctl.control_type,
2626 hpi_ctl.src_node_type,
2627 hpi_ctl.src_node_index,
2628 hpi_ctl.dst_node_type,
2629 hpi_ctl.dst_node_index);
2630 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002631 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002632 if (err < 0)
2633 return err;
2634 }
2635 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2636 hpi_handle_error(err);
2637
2638 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2639
2640 return 0;
2641}
2642
2643/*------------------------------------------------------------
2644 /proc interface
2645 ------------------------------------------------------------*/
2646
2647static void
2648snd_asihpi_proc_read(struct snd_info_entry *entry,
2649 struct snd_info_buffer *buffer)
2650{
2651 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002652 u32 h_control;
2653 u32 rate = 0;
2654 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002655
2656 u16 num_outstreams;
2657 u16 num_instreams;
2658 u16 version;
2659 u32 serial_number;
2660 u16 type;
2661
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002662 int err;
2663
2664 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002665
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002666 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2667 &num_outstreams, &num_instreams,
2668 &version, &serial_number, &type));
2669
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002670 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002671 "Adapter type ASI%4X\nHardware Index %d\n"
2672 "%d outstreams\n%d instreams\n",
2673 type, asihpi->hpi->adapter->index,
2674 num_outstreams, num_instreams);
2675
2676 snd_iprintf(buffer,
2677 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2678 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002679 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2680
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002681 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002682 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2683 HPI_CONTROL_SAMPLECLOCK, &h_control);
2684
2685 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002686 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002687 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002688
2689 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002690 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002691 rate, sampleclock_sources[source]);
2692 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002693}
2694
Bill Pembertone23e7a12012-12-06 12:35:10 -05002695static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002696{
2697 struct snd_info_entry *entry;
2698
2699 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2700 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2701}
2702
2703/*------------------------------------------------------------
2704 HWDEP
2705 ------------------------------------------------------------*/
2706
2707static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2708{
2709 if (enable_hpi_hwdep)
2710 return 0;
2711 else
2712 return -ENODEV;
2713
2714}
2715
2716static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2717{
2718 if (enable_hpi_hwdep)
2719 return asihpi_hpi_release(file);
2720 else
2721 return -ENODEV;
2722}
2723
2724static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2725 unsigned int cmd, unsigned long arg)
2726{
2727 if (enable_hpi_hwdep)
2728 return asihpi_hpi_ioctl(file, cmd, arg);
2729 else
2730 return -ENODEV;
2731}
2732
2733
2734/* results in /dev/snd/hwC#D0 file for each card with index #
2735 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2736*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002737static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2738 int device, struct snd_hwdep **rhwdep)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002739{
2740 struct snd_hwdep *hw;
2741 int err;
2742
2743 if (rhwdep)
2744 *rhwdep = NULL;
2745 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2746 if (err < 0)
2747 return err;
2748 strcpy(hw->name, "asihpi (HPI)");
2749 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2750 hw->ops.open = snd_asihpi_hpi_open;
2751 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2752 hw->ops.release = snd_asihpi_hpi_release;
2753 hw->private_data = asihpi;
2754 if (rhwdep)
2755 *rhwdep = hw;
2756 return 0;
2757}
2758
2759/*------------------------------------------------------------
2760 CARD
2761 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002762static int snd_asihpi_probe(struct pci_dev *pci_dev,
2763 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002764{
2765 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002766 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002767 struct snd_card *card;
2768 struct snd_card_asihpi *asihpi;
2769
2770 u32 h_control;
2771 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002772 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002773
2774 static int dev;
2775 if (dev >= SNDRV_CARDS)
2776 return -ENODEV;
2777
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002778 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002779 if (!enable[dev]) {
2780 dev++;
2781 return -ENOENT;
2782 }
2783
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002784 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002785 err = asihpi_adapter_probe(pci_dev, pci_id);
2786 if (err < 0)
2787 return err;
2788
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002789 hpi = pci_get_drvdata(pci_dev);
2790 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002791 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002792 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2793 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002794 if (err < 0) {
2795 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002796 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2797 THIS_MODULE, sizeof(struct snd_card_asihpi),
2798 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002799 if (err < 0)
2800 return err;
2801 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002802 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002803 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002804 }
2805
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002806 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002807 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002808 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002809 asihpi->hpi = hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002810
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002811 snd_printk(KERN_INFO "adapter ID=%4X index=%d\n",
2812 asihpi->hpi->adapter->type, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002813
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002814 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002815 HPI_ADAPTER_PROPERTY_CAPS1,
2816 NULL, &asihpi->support_grouping);
2817 if (err)
2818 asihpi->support_grouping = 0;
2819
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002820 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002821 HPI_ADAPTER_PROPERTY_CAPS2,
2822 &asihpi->support_mrx, NULL);
2823 if (err)
2824 asihpi->support_mrx = 0;
2825
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002826 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002827 HPI_ADAPTER_PROPERTY_INTERVAL,
2828 NULL, &asihpi->update_interval_frames);
2829 if (err)
2830 asihpi->update_interval_frames = 512;
2831
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002832 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002833 asihpi->update_interval_frames *= 2;
2834
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002835 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002836 0, &h_stream));
2837
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002838 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002839 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002840
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002841 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002842
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002843 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002844 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2845 &asihpi->in_max_chans, &asihpi->out_max_chans);
2846 if (err) {
2847 asihpi->in_max_chans = 2;
2848 asihpi->out_max_chans = 2;
2849 }
2850
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002851 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2852 asihpi->out_min_chans = asihpi->out_max_chans;
2853 asihpi->in_min_chans = asihpi->in_max_chans;
2854 asihpi->support_grouping = 0;
2855 } else {
2856 asihpi->out_min_chans = 1;
2857 asihpi->in_min_chans = 1;
2858 }
2859
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002860 snd_printk(KERN_INFO "Has dma:%d, grouping:%d, mrx:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002861 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002862 asihpi->support_grouping,
2863 asihpi->support_mrx
2864 );
2865
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002866 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002867 if (err < 0) {
2868 snd_printk(KERN_ERR "pcm_new failed\n");
2869 goto __nodev;
2870 }
2871 err = snd_card_asihpi_mixer_new(asihpi);
2872 if (err < 0) {
2873 snd_printk(KERN_ERR "mixer_new failed\n");
2874 goto __nodev;
2875 }
2876
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002877 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002878 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2879 HPI_CONTROL_SAMPLECLOCK, &h_control);
2880
2881 if (!err)
2882 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002883 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002884
2885 snd_asihpi_proc_init(asihpi);
2886
2887 /* always create, can be enabled or disabled dynamically
2888 by enable_hwdep module param*/
2889 snd_asihpi_hpi_new(asihpi, 0, NULL);
2890
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002891 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002892
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002893 sprintf(card->shortname, "AudioScience ASI%4X",
2894 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002895 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002896 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002897 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002898
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002899 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002900 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002901 dev++;
2902 return 0;
2903 }
2904__nodev:
2905 snd_card_free(card);
2906 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2907 return err;
2908
2909}
2910
Bill Pembertone23e7a12012-12-06 12:35:10 -05002911static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002912{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002913 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
2914 snd_card_free(hpi->snd_card);
2915 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002916 asihpi_adapter_remove(pci_dev);
2917}
2918
Benoit Taine9baa3c32014-08-08 15:56:03 +02002919static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002920 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2921 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2922 (kernel_ulong_t)HPI_6205},
2923 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2924 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2925 (kernel_ulong_t)HPI_6000},
2926 {0,}
2927};
2928MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2929
2930static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002931 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002932 .id_table = asihpi_pci_tbl,
2933 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002934 .remove = snd_asihpi_remove,
Takashi Iwaic7561cd2012-08-14 18:12:04 +02002935#ifdef CONFIG_PM_SLEEP
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002936/* .suspend = snd_asihpi_suspend,
2937 .resume = snd_asihpi_resume, */
2938#endif
2939};
2940
2941static int __init snd_asihpi_init(void)
2942{
2943 asihpi_init();
2944 return pci_register_driver(&driver);
2945}
2946
2947static void __exit snd_asihpi_exit(void)
2948{
2949
2950 pci_unregister_driver(&driver);
2951 asihpi_exit();
2952}
2953
2954module_init(snd_asihpi_init)
2955module_exit(snd_asihpi_exit)
2956