blob: 0e130dd8732e1f2032d84014a5f4b609aafd4b52 [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#include <linux/pci.h>
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/wait.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040037#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038#include <sound/core.h>
39#include <sound/control.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/info.h>
43#include <sound/initval.h>
44#include <sound/tlv.h>
45#include <sound/hwdep.h>
46
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020047MODULE_LICENSE("GPL");
48MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130049MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130050 HPI_VER_STRING);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020051
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130052#if defined CONFIG_SND_DEBUG_VERBOSE
53/**
54 * snd_printddd - very verbose debug printk
55 * @format: format string
56 *
57 * Works like snd_printk() for debugging purposes.
58 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
59 * Must set snd module debug parameter to 3 to enable at runtime.
60 */
61#define snd_printddd(format, args...) \
62 __snd_printk(3, __FILE__, __LINE__, format, ##args)
63#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120064#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130065#endif
66
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020067static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
68static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103069static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
70static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020071
72module_param_array(index, int, NULL, S_IRUGO);
73MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
74
75module_param_array(id, charp, NULL, S_IRUGO);
76MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
77
78module_param_array(enable, bool, NULL, S_IRUGO);
79MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
80
81module_param(enable_hpi_hwdep, bool, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(enable_hpi_hwdep,
83 "ALSA enable HPI hwdep for AudioScience soundcard ");
84
85/* identify driver */
86#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130087static char *build_info = "Built using headers from kernel source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020088module_param(build_info, charp, S_IRUGO);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130089MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020090#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130091static char *build_info = "Built within ALSA source";
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020092module_param(build_info, charp, S_IRUGO);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130093MODULE_PARM_DESC(build_info, "Built within ALSA source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020094#endif
95
96/* set to 1 to dump every control from adapter to log */
97static const int mixer_dump;
98
99#define DEFAULT_SAMPLERATE 44100
100static int adapter_fs = DEFAULT_SAMPLERATE;
101
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200102/* defaults */
103#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300104#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200105#define BUFFER_BYTES_MAX (512 * 1024)
106
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200107#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
108
109struct clk_source {
110 int source;
111 int index;
112 char *name;
113};
114
115struct clk_cache {
116 int count;
117 int has_local;
118 struct clk_source s[MAX_CLOCKSOURCES];
119};
120
121/* Per card data */
122struct snd_card_asihpi {
123 struct snd_card *card;
124 struct pci_dev *pci;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300125 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200126
127 u32 h_mixer;
128 struct clk_cache cc;
129
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200130 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200131 u16 support_grouping;
132 u16 support_mrx;
133 u16 update_interval_frames;
134 u16 in_max_chans;
135 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300136 u16 in_min_chans;
137 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200138};
139
140/* Per stream data */
141struct snd_card_asihpi_pcm {
142 struct timer_list timer;
143 unsigned int respawn_timer;
144 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300145 unsigned int buffer_bytes;
146 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200147 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300148 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
149 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
150 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200151 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200152 struct snd_pcm_substream *substream;
153 u32 h_stream;
154 struct hpi_format format;
155};
156
157/* universal stream verbs work with out or in stream handles */
158
159/* Functions to allow driver to give a buffer to HPI for busmastering */
160
161static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200162 u32 h_stream, /* handle to outstream. */
163 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
164 u32 pci_address
165)
166{
167 struct hpi_message hm;
168 struct hpi_response hr;
169 unsigned int obj = hpi_handle_object(h_stream);
170
171 if (!h_stream)
172 return HPI_ERROR_INVALID_OBJ;
173 hpi_init_message_response(&hm, &hr, obj,
174 obj == HPI_OBJ_OSTREAM ?
175 HPI_OSTREAM_HOSTBUFFER_ALLOC :
176 HPI_ISTREAM_HOSTBUFFER_ALLOC);
177
178 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
179 &hm.obj_index);
180
181 hm.u.d.u.buffer.buffer_size = size_in_bytes;
182 hm.u.d.u.buffer.pci_address = pci_address;
183 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
184 hpi_send_recv(&hm, &hr);
185 return hr.error;
186}
187
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300188static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200189{
190 struct hpi_message hm;
191 struct hpi_response hr;
192 unsigned int obj = hpi_handle_object(h_stream);
193
194 if (!h_stream)
195 return HPI_ERROR_INVALID_OBJ;
196
197 hpi_init_message_response(&hm, &hr, obj,
198 obj == HPI_OBJ_OSTREAM ?
199 HPI_OSTREAM_HOSTBUFFER_FREE :
200 HPI_ISTREAM_HOSTBUFFER_FREE);
201
202 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
203 &hm.obj_index);
204 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
205 hpi_send_recv(&hm, &hr);
206 return hr.error;
207}
208
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300209static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200210{
211 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300212 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200213 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300214 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200215}
216
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300217static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200218{
219 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300220 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200221 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300222 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200223}
224
225static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200226 u32 h_stream,
227 u16 *pw_state,
228 u32 *pbuffer_size,
229 u32 *pdata_in_buffer,
230 u32 *psample_count,
231 u32 *pauxiliary_data
232)
233{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300234 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200235 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300236 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200237 pbuffer_size, pdata_in_buffer,
238 psample_count, pauxiliary_data);
239 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300240 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200241 pbuffer_size, pdata_in_buffer,
242 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300243 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244}
245
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300246static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200247 u32 h_master,
248 u32 h_stream)
249{
250 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300251 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200252 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300253 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254}
255
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257{
258 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300259 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200260 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262}
263
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300264static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200265 u32 h_stream, u32 *mo, u32 *mi)
266{
267 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300268 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200269 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300270 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200271}
272
273static u16 handle_error(u16 err, int line, char *filename)
274{
275 if (err)
276 printk(KERN_WARNING
277 "in file %s, line %d: HPI error %d\n",
278 filename, line, err);
279 return err;
280}
281
282#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
283
284/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200285
286static void print_hwparams(struct snd_pcm_substream *substream,
287 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200288{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200289 char name[16];
290 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200291 snd_printd("%s HWPARAMS\n", name);
292 snd_printd(" samplerate %d Hz\n", params_rate(p));
293 snd_printd(" channels %d\n", params_channels(p));
294 snd_printd(" format %d\n", params_format(p));
295 snd_printd(" subformat %d\n", params_subformat(p));
296 snd_printd(" buffer %d B\n", params_buffer_bytes(p));
297 snd_printd(" period %d B\n", params_period_bytes(p));
298 snd_printd(" access %d\n", params_access(p));
299 snd_printd(" period_size %d\n", params_period_size(p));
300 snd_printd(" periods %d\n", params_periods(p));
301 snd_printd(" buffer_size %d\n", params_buffer_size(p));
302 snd_printd(" %d B/s\n", params_rate(p) *
303 params_channels(p) *
304 snd_pcm_format_width(params_format(p)) / 8);
305
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200306}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200307
308static snd_pcm_format_t hpi_to_alsa_formats[] = {
309 -1, /* INVALID */
310 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
311 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
312 -1, /* HPI_FORMAT_MPEG_L1 3 */
313 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
314 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
315 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
316 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
317 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
318 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
319 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
320 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
321 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
322 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
323 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
324#if 1
325 /* ALSA can't handle 3 byte sample size together with power-of-2
326 * constraint on buffer_bytes, so disable this format
327 */
328 -1
329#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300330 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200331#endif
332};
333
334
335static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
336 u16 *hpi_format)
337{
338 u16 format;
339
340 for (format = HPI_FORMAT_PCM8_UNSIGNED;
341 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
342 if (hpi_to_alsa_formats[format] == alsa_format) {
343 *hpi_format = format;
344 return 0;
345 }
346 }
347
348 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
349 alsa_format);
350 *hpi_format = 0;
351 return -EINVAL;
352}
353
354static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
355 struct snd_pcm_hardware *pcmhw)
356{
357 u16 err;
358 u32 h_control;
359 u32 sample_rate;
360 int idx;
361 unsigned int rate_min = 200000;
362 unsigned int rate_max = 0;
363 unsigned int rates = 0;
364
365 if (asihpi->support_mrx) {
366 rates |= SNDRV_PCM_RATE_CONTINUOUS;
367 rates |= SNDRV_PCM_RATE_8000_96000;
368 rate_min = 8000;
369 rate_max = 100000;
370 } else {
371 /* on cards without SRC,
372 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300373 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200374 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
375 HPI_CONTROL_SAMPLECLOCK, &h_control);
376 if (err) {
377 snd_printk(KERN_ERR
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300378 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200379 }
380
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300381 for (idx = -1; idx < 100; idx++) {
382 if (idx == -1) {
383 if (hpi_sample_clock_get_sample_rate(h_control,
384 &sample_rate))
385 continue;
386 } else if (hpi_sample_clock_query_local_rate(h_control,
387 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200388 break;
389 }
390
391 rate_min = min(rate_min, sample_rate);
392 rate_max = max(rate_max, sample_rate);
393
394 switch (sample_rate) {
395 case 5512:
396 rates |= SNDRV_PCM_RATE_5512;
397 break;
398 case 8000:
399 rates |= SNDRV_PCM_RATE_8000;
400 break;
401 case 11025:
402 rates |= SNDRV_PCM_RATE_11025;
403 break;
404 case 16000:
405 rates |= SNDRV_PCM_RATE_16000;
406 break;
407 case 22050:
408 rates |= SNDRV_PCM_RATE_22050;
409 break;
410 case 32000:
411 rates |= SNDRV_PCM_RATE_32000;
412 break;
413 case 44100:
414 rates |= SNDRV_PCM_RATE_44100;
415 break;
416 case 48000:
417 rates |= SNDRV_PCM_RATE_48000;
418 break;
419 case 64000:
420 rates |= SNDRV_PCM_RATE_64000;
421 break;
422 case 88200:
423 rates |= SNDRV_PCM_RATE_88200;
424 break;
425 case 96000:
426 rates |= SNDRV_PCM_RATE_96000;
427 break;
428 case 176400:
429 rates |= SNDRV_PCM_RATE_176400;
430 break;
431 case 192000:
432 rates |= SNDRV_PCM_RATE_192000;
433 break;
434 default: /* some other rate */
435 rates |= SNDRV_PCM_RATE_KNOT;
436 }
437 }
438 }
439
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200440 pcmhw->rates = rates;
441 pcmhw->rate_min = rate_min;
442 pcmhw->rate_max = rate_max;
443}
444
445static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
446 struct snd_pcm_hw_params *params)
447{
448 struct snd_pcm_runtime *runtime = substream->runtime;
449 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
450 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
451 int err;
452 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400453 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200454 unsigned int bytes_per_sec;
455
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200456 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200457 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
458 if (err < 0)
459 return err;
460 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
461 if (err)
462 return err;
463
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200464 hpi_handle_error(hpi_format_create(&dpcm->format,
465 params_channels(params),
466 format, params_rate(params), 0, 0));
467
468 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300469 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200470 return -EINVAL;
471
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300472 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200473 dpcm->h_stream, &dpcm->format) != 0)
474 return -EINVAL;
475 }
476
477 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200478 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300479 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200480 params_buffer_bytes(params), runtime->dma_addr);
481 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300482 snd_printdd(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200483 "stream_host_buffer_attach succeeded %u %lu\n",
484 params_buffer_bytes(params),
485 (unsigned long)runtime->dma_addr);
486 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300487 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200488 err);
489 return -ENOMEM;
490 }
491
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300492 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200493 &dpcm->hpi_buffer_attached,
494 NULL, NULL, NULL);
495
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300496 snd_printdd("stream_host_buffer_attach status 0x%x\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200497 dpcm->hpi_buffer_attached);
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300498
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200499 }
500 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400501 width = snd_pcm_format_width(params_format(params));
502 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200503 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400504 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200505 return -EINVAL;
506
507 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300508 dpcm->buffer_bytes = params_buffer_bytes(params);
509 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200510
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200511 return 0;
512}
513
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300514static int
515snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
516{
517 struct snd_pcm_runtime *runtime = substream->runtime;
518 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
519 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300520 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300521
522 snd_pcm_lib_free_pages(substream);
523 return 0;
524}
525
526static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
527{
528 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
529 kfree(dpcm);
530}
531
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200532static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
533 substream)
534{
535 struct snd_pcm_runtime *runtime = substream->runtime;
536 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
537 int expiry;
538
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300539 expiry = HZ / 200;
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +1300540
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300541 expiry = max(expiry, 1); /* don't let it be zero! */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200542 dpcm->timer.expires = jiffies + expiry;
543 dpcm->respawn_timer = 1;
544 add_timer(&dpcm->timer);
545}
546
547static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
548{
549 struct snd_pcm_runtime *runtime = substream->runtime;
550 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
551
552 dpcm->respawn_timer = 0;
553 del_timer(&dpcm->timer);
554}
555
556static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
557 int cmd)
558{
559 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
560 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
561 struct snd_pcm_substream *s;
562 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200563 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200564
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200565 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200566 snd_printdd("%s trigger\n", name);
567
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200568 switch (cmd) {
569 case SNDRV_PCM_TRIGGER_START:
570 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300571 struct snd_pcm_runtime *runtime = s->runtime;
572 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200573
574 if (snd_pcm_substream_chip(s) != card)
575 continue;
576
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300577 /* don't link Cap and Play */
578 if (substream->stream != s->stream)
579 continue;
580
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200581 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200582 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200583 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300584 * in buffer? Must be at least one period!
585 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200586 * buffer is bigger it may contain even more
587 * data??
588 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300589 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300590 snd_printddd("%d preload x%x\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200591 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300592 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300593 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200594 preload,
595 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300596 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200597 }
598
599 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200600 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300601 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602 dpcm->h_stream,
603 ds->h_stream);
604 if (!e) {
605 snd_pcm_trigger_done(s, substream);
606 } else {
607 hpi_handle_error(e);
608 break;
609 }
610 } else
611 break;
612 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300613 snd_printdd("start\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200614 /* start the master stream */
615 snd_card_asihpi_pcm_timer_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300616 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200617 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300618 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200619 break;
620
621 case SNDRV_PCM_TRIGGER_STOP:
622 snd_card_asihpi_pcm_timer_stop(substream);
623 snd_pcm_group_for_each_entry(s, substream) {
624 if (snd_pcm_substream_chip(s) != card)
625 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300626 /* don't link Cap and Play */
627 if (substream->stream != s->stream)
628 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200629
630 /*? workaround linked streams don't
631 transition to SETUP 20070706*/
632 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
633
634 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200635 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200636 snd_pcm_trigger_done(s, substream);
637 } else
638 break;
639 }
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300640 snd_printdd("stop\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200641
642 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300643 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200644 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
645 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300646 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200647
648 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300649 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200650 break;
651
652 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300653 snd_printdd("pause release\n");
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300654 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200655 snd_card_asihpi_pcm_timer_start(substream);
656 break;
657 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300658 snd_printdd("pause\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200659 snd_card_asihpi_pcm_timer_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300660 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661 break;
662 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300663 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200664 return -EINVAL;
665 }
666
667 return 0;
668}
669
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200670/*algorithm outline
671 Without linking degenerates to getting single stream pos etc
672 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
673*/
674/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300675pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200676for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300677 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300678 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300679 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200680}
681timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
682for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300683 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300684 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300686 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200687 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300688 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200689 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300690 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200691 }
692 }
693 snd_pcm_period_elapsed(s);
694 }
695}
696*/
697
698/** Minimum of 2 modulo values. Works correctly when the difference between
699* the values is less than half the modulus
700*/
701static inline unsigned int modulo_min(unsigned int a, unsigned int b,
702 unsigned long int modulus)
703{
704 unsigned int result;
705 if (((a-b) % modulus) < (modulus/2))
706 result = b;
707 else
708 result = a;
709
710 return result;
711}
712
713/** Timer function, equivalent to interrupt service routine for cards
714*/
715static void snd_card_asihpi_timer_function(unsigned long data)
716{
717 struct snd_card_asihpi_pcm *dpcm = (struct snd_card_asihpi_pcm *)data;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300718 struct snd_pcm_substream *substream = dpcm->substream;
719 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200720 struct snd_pcm_runtime *runtime;
721 struct snd_pcm_substream *s;
722 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300723 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200724 unsigned int remdata, xfercount, next_jiffies;
725 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300726 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200727 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300728 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200729 char name[16];
730
731 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200732
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200733 snd_printdd("%s snd_card_asihpi_timer_function\n", name);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300734
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200735 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300736 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200737 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
738 runtime = s->runtime;
739
740 if (snd_pcm_substream_chip(s) != card)
741 continue;
742
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300743 /* don't link Cap and Play */
744 if (substream->stream != s->stream)
745 continue;
746
747 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200748 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300749 &buffer_size, &bytes_avail,
750 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751
752 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300753 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200754
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200755 if (!card->can_dma)
756 on_card_bytes = bytes_avail;
757
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300758 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300759 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300760 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300761 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300762 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300763 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200764 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300765 }
766 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300767 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300768 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200769 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300770 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100771 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200772 continue;
773 }
774 } else {
775 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300776 }
777 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300778 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200779
780 if (first) {
781 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300782 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300783 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200784 first = 0;
785 } else {
786 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300787 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200788 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300789 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200790 newdata);
791 }
792
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200793 snd_printdd("hw_ptr 0x%04lX, appl_ptr 0x%04lX\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200794 (unsigned long)frames_to_bytes(runtime,
795 runtime->status->hw_ptr),
796 (unsigned long)frames_to_bytes(runtime,
797 runtime->control->appl_ptr));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300798
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200799 snd_printdd("%d S=%d, "
800 "rw=0x%04X, dma=0x%04X, left=0x%04X, "
801 "aux=0x%04X space=0x%04X\n",
802 s->number, state,
803 ds->pcm_buf_host_rw_ofs, pcm_buf_dma_ofs,
804 (int)bytes_avail,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300805 (int)on_card_bytes, buffer_size-bytes_avail);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300806 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200807 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300808 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200809
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300810 remdata = newdata % dpcm->period_bytes;
811 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300812 /* come back when on_card_bytes has decreased enough to allow
813 write to happen, or when data has been consumed to make another
814 period
815 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300816 if (xfercount && (on_card_bytes > dpcm->period_bytes))
817 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300818 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300819 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300820
821 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200822 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200823 snd_printdd("jif %d buf pos 0x%04X newdata 0x%04X xfer 0x%04X\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300824 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
825
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300826 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200827 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200828
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300829 /* don't link Cap and Play */
830 if (substream->stream != s->stream)
831 continue;
832
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300833 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
834
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200835 if (xfercount &&
836 /* Limit use of on card fifo for playback */
837 ((on_card_bytes <= ds->period_bytes) ||
838 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
839
840 {
841
842 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
843 unsigned int xfer1, xfer2;
844 char *pd = &s->runtime->dma_area[buf_ofs];
845
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200846 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200847 xfer1 = xfercount;
848 xfer2 = 0;
849 } else {
850 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
851 xfer2 = xfercount - xfer1;
852 }
853
854 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
855 snd_printddd("P%d write1 0x%04X 0x%04X\n",
856 s->number, xfer1, buf_ofs);
857 hpi_handle_error(
858 hpi_outstream_write_buf(
859 ds->h_stream, pd, xfer1,
860 &ds->format));
861
862 if (xfer2) {
863 pd = s->runtime->dma_area;
864
865 snd_printddd("P%d write2 0x%04X 0x%04X\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200866 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200867 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200868 hpi_handle_error(
869 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200870 ds->h_stream, pd,
871 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200872 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200873 }
874 } else {
875 snd_printddd("C%d read1 0x%04x\n",
876 s->number, xfer1);
877 hpi_handle_error(
878 hpi_instream_read_buf(
879 ds->h_stream,
880 pd, xfer1));
881 if (xfer2) {
882 pd = s->runtime->dma_area;
883 snd_printddd("C%d read2 0x%04x\n",
884 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200885 hpi_handle_error(
886 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300887 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200888 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200889 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200890 }
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300891 ds->pcm_buf_host_rw_ofs += xfercount;
892 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200893 snd_pcm_period_elapsed(s);
894 }
895 }
896
897 if (dpcm->respawn_timer)
898 add_timer(&dpcm->timer);
899}
900
901/***************************** PLAYBACK OPS ****************/
902static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
903 unsigned int cmd, void *arg)
904{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300905 char name[16];
906 snd_pcm_debug_name(substream, name, sizeof(name));
907 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200908 return snd_pcm_lib_ioctl(substream, cmd, arg);
909}
910
911static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
912 substream)
913{
914 struct snd_pcm_runtime *runtime = substream->runtime;
915 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
916
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200917 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200918
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300919 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300920 dpcm->pcm_buf_host_rw_ofs = 0;
921 dpcm->pcm_buf_dma_ofs = 0;
922 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200923 return 0;
924}
925
926static snd_pcm_uframes_t
927snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
928{
929 struct snd_pcm_runtime *runtime = substream->runtime;
930 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
931 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300932 char name[16];
933 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200934
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300935 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300936 snd_printddd("%s pointer = 0x%04lx\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200937 return ptr;
938}
939
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300940static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
941 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200942{
943 struct hpi_format hpi_format;
944 u16 format;
945 u16 err;
946 u32 h_control;
947 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300948 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200949
950 /* on cards without SRC, must query at valid rate,
951 * maybe set by external sync
952 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300953 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200954 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
955 HPI_CONTROL_SAMPLECLOCK, &h_control);
956
957 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300958 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200959 &sample_rate);
960
961 for (format = HPI_FORMAT_PCM8_UNSIGNED;
962 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +1300963 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
964 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200965 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +1300966 err = hpi_outstream_query_format(h_stream, &hpi_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200967 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +0200968 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200969 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300970 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200971}
972
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200973static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
974{
975 struct snd_pcm_runtime *runtime = substream->runtime;
976 struct snd_card_asihpi_pcm *dpcm;
977 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300978 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200979 int err;
980
981 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
982 if (dpcm == NULL)
983 return -ENOMEM;
984
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300985 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200986 substream->number, &dpcm->h_stream);
987 hpi_handle_error(err);
988 if (err)
989 kfree(dpcm);
990 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
991 return -EBUSY;
992 if (err)
993 return -EIO;
994
995 /*? also check ASI5000 samplerate source
996 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300997 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200998 */
999
1000 init_timer(&dpcm->timer);
1001 dpcm->timer.data = (unsigned long) dpcm;
1002 dpcm->timer.function = snd_card_asihpi_timer_function;
1003 dpcm->substream = substream;
1004 runtime->private_data = dpcm;
1005 runtime->private_free = snd_card_asihpi_runtime_free;
1006
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001007 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
1008 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1009 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001010 /*?snd_card_asihpi_playback.period_bytes_min =
1011 card->out_max_chans * 4096; */
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001012 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1013 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1014 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1015 /* snd_card_asihpi_playback.fifo_size = 0; */
1016 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1017 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1018 snd_card_asihpi_playback.formats =
1019 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001020
1021 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1022
1023 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1024 SNDRV_PCM_INFO_DOUBLE |
1025 SNDRV_PCM_INFO_BATCH |
1026 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001027 SNDRV_PCM_INFO_PAUSE |
1028 SNDRV_PCM_INFO_MMAP |
1029 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001030
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001031 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001032 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001033 snd_pcm_set_sync(substream);
1034 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001035
1036 /* struct is copied, so can create initializer dynamically */
1037 runtime->hw = snd_card_asihpi_playback;
1038
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001039 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001040 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1041 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1042 if (err < 0)
1043 return err;
1044
1045 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1046 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001047
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001048 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001049 card->update_interval_frames * 2, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001050
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001051 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001052
1053 return 0;
1054}
1055
1056static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1057{
1058 struct snd_pcm_runtime *runtime = substream->runtime;
1059 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1060
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001061 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001062 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001063
1064 return 0;
1065}
1066
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001067static struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
1068 .open = snd_card_asihpi_playback_open,
1069 .close = snd_card_asihpi_playback_close,
1070 .ioctl = snd_card_asihpi_playback_ioctl,
1071 .hw_params = snd_card_asihpi_pcm_hw_params,
1072 .hw_free = snd_card_asihpi_hw_free,
1073 .prepare = snd_card_asihpi_playback_prepare,
1074 .trigger = snd_card_asihpi_trigger,
1075 .pointer = snd_card_asihpi_playback_pointer,
1076};
1077
1078/***************************** CAPTURE OPS ****************/
1079static snd_pcm_uframes_t
1080snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1081{
1082 struct snd_pcm_runtime *runtime = substream->runtime;
1083 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1084
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001085 snd_printddd("capture pointer %d=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001086 substream->number, dpcm->pcm_buf_dma_ofs);
1087 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001088 for the capture position, because those samples aren't yet in
1089 the local buffer available for reading.
1090 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001091 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001092}
1093
1094static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1095 unsigned int cmd, void *arg)
1096{
1097 return snd_pcm_lib_ioctl(substream, cmd, arg);
1098}
1099
1100static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1101{
1102 struct snd_pcm_runtime *runtime = substream->runtime;
1103 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1104
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001105 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001106 dpcm->pcm_buf_host_rw_ofs = 0;
1107 dpcm->pcm_buf_dma_ofs = 0;
1108 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001109
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001110 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001111 return 0;
1112}
1113
1114
1115
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001116static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1117 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001118{
1119 struct hpi_format hpi_format;
1120 u16 format;
1121 u16 err;
1122 u32 h_control;
1123 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001124 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001125
1126 /* on cards without SRC, must query at valid rate,
1127 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001128 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001129 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1130 HPI_CONTROL_SAMPLECLOCK, &h_control);
1131
1132 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001133 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001134 &sample_rate);
1135
1136 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1137 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1138
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001139 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1140 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001141 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001142 err = hpi_instream_query_format(h_stream, &hpi_format);
Eldad Zack167d0a12013-04-23 01:00:42 +02001143 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001144 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001145 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001146 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001147}
1148
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001149static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1150{
1151 struct snd_pcm_runtime *runtime = substream->runtime;
1152 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1153 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001154 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155 int err;
1156
1157 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1158 if (dpcm == NULL)
1159 return -ENOMEM;
1160
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001161 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001162 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001163
1164 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001165 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001166 substream->number, &dpcm->h_stream));
1167 if (err)
1168 kfree(dpcm);
1169 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1170 return -EBUSY;
1171 if (err)
1172 return -EIO;
1173
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001174 init_timer(&dpcm->timer);
1175 dpcm->timer.data = (unsigned long) dpcm;
1176 dpcm->timer.function = snd_card_asihpi_timer_function;
1177 dpcm->substream = substream;
1178 runtime->private_data = dpcm;
1179 runtime->private_free = snd_card_asihpi_runtime_free;
1180
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001181 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
1182 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1183 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1184 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1185 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1186 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1187 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001188 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001189 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001190 snd_card_asihpi_capture.formats =
1191 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001192 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001193 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1194 SNDRV_PCM_INFO_MMAP |
1195 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001196
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001197 if (card->support_grouping)
1198 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1199
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001200 runtime->hw = snd_card_asihpi_capture;
1201
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001202 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001203 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1204 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1205 if (err < 0)
1206 return err;
1207
1208 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1209 card->update_interval_frames);
1210 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1211 card->update_interval_frames * 2, UINT_MAX);
1212
1213 snd_pcm_set_sync(substream);
1214
1215 return 0;
1216}
1217
1218static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1219{
1220 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1221
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001222 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001223 return 0;
1224}
1225
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001226static struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
1227 .open = snd_card_asihpi_capture_open,
1228 .close = snd_card_asihpi_capture_close,
1229 .ioctl = snd_card_asihpi_capture_ioctl,
1230 .hw_params = snd_card_asihpi_pcm_hw_params,
1231 .hw_free = snd_card_asihpi_hw_free,
1232 .prepare = snd_card_asihpi_capture_prepare,
1233 .trigger = snd_card_asihpi_trigger,
1234 .pointer = snd_card_asihpi_capture_pointer,
1235};
1236
Bill Pembertone23e7a12012-12-06 12:35:10 -05001237static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001238{
1239 struct snd_pcm *pcm;
1240 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001241 u16 num_instreams, num_outstreams, x16;
1242 u32 x32;
1243
1244 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1245 &num_outstreams, &num_instreams,
1246 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001247
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001248 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001249 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001250 if (err < 0)
1251 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001252
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001253 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001254 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1255 &snd_card_asihpi_playback_mmap_ops);
1256 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1257 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258
1259 pcm->private_data = asihpi;
1260 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001261 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001262
1263 /*? do we want to emulate MMAP for non-BBM cards?
1264 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1265 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1266 snd_dma_pci_data(asihpi->pci),
1267 64*1024, BUFFER_BYTES_MAX);
1268
1269 return 0;
1270}
1271
1272/***************************** MIXER CONTROLS ****************/
1273struct hpi_control {
1274 u32 h_control;
1275 u16 control_type;
1276 u16 src_node_type;
1277 u16 src_node_index;
1278 u16 dst_node_type;
1279 u16 dst_node_index;
1280 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001281 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001282};
1283
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001284static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001285 "invalid",
1286 "AM",
1287 "FM mono",
1288 "TV NTSC-M",
1289 "FM stereo",
1290 "AUX",
1291 "TV PAL BG",
1292 "TV PAL I",
1293 "TV PAL DK",
1294 "TV SECAM",
1295};
1296
1297compile_time_assert(
1298 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1299 (HPI_TUNER_BAND_LAST+1)),
1300 assert_tuner_band_names_size);
1301
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001302static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001303 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001304 "PCM",
1305 "Line",
1306 "Digital",
1307 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001308 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001309 "Clock",
1310 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001311 "Mic",
1312 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001313 "Analog",
1314 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001315 "RTP",
Eliot Blennerhassett502f2712011-12-22 13:38:39 +13001316 "Internal"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001317};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001318
1319compile_time_assert(
1320 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001321 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001322 assert_src_names_size);
1323
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001324static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001325 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001326 "PCM",
1327 "Line",
1328 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001329 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001330 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001331 "Net",
1332 "Analog",
1333 "RTP",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001334};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335
1336compile_time_assert(
1337 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001338 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001339 assert_dst_names_size);
1340
1341static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1342 struct snd_card_asihpi *asihpi)
1343{
1344 int err;
1345
1346 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1347 if (err < 0)
1348 return err;
1349 else if (mixer_dump)
1350 snd_printk(KERN_INFO "added %s(%d)\n", ctl->name, ctl->index);
1351
1352 return 0;
1353}
1354
1355/* Convert HPI control name and location into ALSA control name */
1356static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1357 struct hpi_control *hpi_ctl,
1358 char *name)
1359{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001360 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001361 memset(snd_control, 0, sizeof(*snd_control));
1362 snd_control->name = hpi_ctl->name;
1363 snd_control->private_value = hpi_ctl->h_control;
1364 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1365 snd_control->index = 0;
1366
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001367 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1368 dir = ""; /* clock is neither capture nor playback */
1369 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001370 dir = "Capture "; /* On or towards a PCM capture destination*/
1371 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1372 (!hpi_ctl->dst_node_type))
1373 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001374 else if (hpi_ctl->src_node_type &&
1375 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001376 (hpi_ctl->dst_node_type))
1377 dir = "Monitor Playback "; /* Between an input and an output */
1378 else
1379 dir = "Playback "; /* PCM Playback source, or output node */
1380
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001381 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001382 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001383 asihpi_src_names[hpi_ctl->src_node_type],
1384 hpi_ctl->src_node_index,
1385 asihpi_dst_names[hpi_ctl->dst_node_type],
1386 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001387 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001388 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001389 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001390 asihpi_dst_names[hpi_ctl->dst_node_type],
1391 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001392 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001393 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001394 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395 asihpi_src_names[hpi_ctl->src_node_type],
1396 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001397 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001398 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001399 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1400 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401}
1402
1403/*------------------------------------------------------------
1404 Volume controls
1405 ------------------------------------------------------------*/
1406#define VOL_STEP_mB 1
1407static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1408 struct snd_ctl_elem_info *uinfo)
1409{
1410 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001411 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001412 u16 err;
1413 /* native gains are in millibels */
1414 short min_gain_mB;
1415 short max_gain_mB;
1416 short step_gain_mB;
1417
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001418 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001419 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1420 if (err) {
1421 max_gain_mB = 0;
1422 min_gain_mB = -10000;
1423 step_gain_mB = VOL_STEP_mB;
1424 }
1425
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001426 err = hpi_meter_query_channels(h_control, &count);
1427 if (err)
1428 count = HPI_MAX_CHANNELS;
1429
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001430 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001431 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001432 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1433 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1434 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1435 return 0;
1436}
1437
1438static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1439 struct snd_ctl_elem_value *ucontrol)
1440{
1441 u32 h_control = kcontrol->private_value;
1442 short an_gain_mB[HPI_MAX_CHANNELS];
1443
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001444 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001445 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1446 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1447
1448 return 0;
1449}
1450
1451static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1453{
1454 int change;
1455 u32 h_control = kcontrol->private_value;
1456 short an_gain_mB[HPI_MAX_CHANNELS];
1457
1458 an_gain_mB[0] =
1459 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1460 an_gain_mB[1] =
1461 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1462 /* change = asihpi->mixer_volume[addr][0] != left ||
1463 asihpi->mixer_volume[addr][1] != right;
1464 */
1465 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001466 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001467 return change;
1468}
1469
1470static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1471
Takashi Iwai000477a2011-07-22 07:57:44 +02001472#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001473
1474static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1475 struct snd_ctl_elem_value *ucontrol)
1476{
1477 u32 h_control = kcontrol->private_value;
1478 u32 mute;
1479
1480 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1481 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1482
1483 return 0;
1484}
1485
1486static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1487 struct snd_ctl_elem_value *ucontrol)
1488{
1489 u32 h_control = kcontrol->private_value;
1490 int change = 1;
1491 /* HPI currently only supports all or none muting of multichannel volume
1492 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1493 */
1494 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1495 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1496 return change;
1497}
1498
Bill Pembertone23e7a12012-12-06 12:35:10 -05001499static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1500 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001501{
1502 struct snd_card *card = asihpi->card;
1503 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001504 int err;
1505 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001506
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001507 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001508 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1509 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1510 snd_control.info = snd_asihpi_volume_info;
1511 snd_control.get = snd_asihpi_volume_get;
1512 snd_control.put = snd_asihpi_volume_put;
1513 snd_control.tlv.p = db_scale_100;
1514
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001515 err = ctl_add(card, &snd_control, asihpi);
1516 if (err)
1517 return err;
1518
1519 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1520 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1521 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1522 snd_control.info = snd_asihpi_volume_mute_info;
1523 snd_control.get = snd_asihpi_volume_mute_get;
1524 snd_control.put = snd_asihpi_volume_mute_put;
1525 err = ctl_add(card, &snd_control, asihpi);
1526 }
1527 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001528}
1529
1530/*------------------------------------------------------------
1531 Level controls
1532 ------------------------------------------------------------*/
1533static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_info *uinfo)
1535{
1536 u32 h_control = kcontrol->private_value;
1537 u16 err;
1538 short min_gain_mB;
1539 short max_gain_mB;
1540 short step_gain_mB;
1541
1542 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001543 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001544 &max_gain_mB, &step_gain_mB);
1545 if (err) {
1546 max_gain_mB = 2400;
1547 min_gain_mB = -1000;
1548 step_gain_mB = 100;
1549 }
1550
1551 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1552 uinfo->count = 2;
1553 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1554 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1555 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1556 return 0;
1557}
1558
1559static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1560 struct snd_ctl_elem_value *ucontrol)
1561{
1562 u32 h_control = kcontrol->private_value;
1563 short an_gain_mB[HPI_MAX_CHANNELS];
1564
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001565 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001566 ucontrol->value.integer.value[0] =
1567 an_gain_mB[0] / HPI_UNITS_PER_dB;
1568 ucontrol->value.integer.value[1] =
1569 an_gain_mB[1] / HPI_UNITS_PER_dB;
1570
1571 return 0;
1572}
1573
1574static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1575 struct snd_ctl_elem_value *ucontrol)
1576{
1577 int change;
1578 u32 h_control = kcontrol->private_value;
1579 short an_gain_mB[HPI_MAX_CHANNELS];
1580
1581 an_gain_mB[0] =
1582 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1583 an_gain_mB[1] =
1584 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1585 /* change = asihpi->mixer_level[addr][0] != left ||
1586 asihpi->mixer_level[addr][1] != right;
1587 */
1588 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001589 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001590 return change;
1591}
1592
1593static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1594
Bill Pembertone23e7a12012-12-06 12:35:10 -05001595static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1596 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001597{
1598 struct snd_card *card = asihpi->card;
1599 struct snd_kcontrol_new snd_control;
1600
1601 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001602 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001603 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1604 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1605 snd_control.info = snd_asihpi_level_info;
1606 snd_control.get = snd_asihpi_level_get;
1607 snd_control.put = snd_asihpi_level_put;
1608 snd_control.tlv.p = db_scale_level;
1609
1610 return ctl_add(card, &snd_control, asihpi);
1611}
1612
1613/*------------------------------------------------------------
1614 AESEBU controls
1615 ------------------------------------------------------------*/
1616
1617/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001618static const char * const asihpi_aesebu_format_names[] = {
1619 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001620
1621static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1622 struct snd_ctl_elem_info *uinfo)
1623{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001624 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001625}
1626
1627static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1628 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001629 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001630{
1631 u32 h_control = kcontrol->private_value;
1632 u16 source, err;
1633
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001634 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001635
1636 /* default to N/A */
1637 ucontrol->value.enumerated.item[0] = 0;
1638 /* return success but set the control to N/A */
1639 if (err)
1640 return 0;
1641 if (source == HPI_AESEBU_FORMAT_SPDIF)
1642 ucontrol->value.enumerated.item[0] = 1;
1643 if (source == HPI_AESEBU_FORMAT_AESEBU)
1644 ucontrol->value.enumerated.item[0] = 2;
1645
1646 return 0;
1647}
1648
1649static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1650 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001651 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001652{
1653 u32 h_control = kcontrol->private_value;
1654
1655 /* default to S/PDIF */
1656 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1657
1658 if (ucontrol->value.enumerated.item[0] == 1)
1659 source = HPI_AESEBU_FORMAT_SPDIF;
1660 if (ucontrol->value.enumerated.item[0] == 2)
1661 source = HPI_AESEBU_FORMAT_AESEBU;
1662
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001663 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001664 return -EINVAL;
1665
1666 return 1;
1667}
1668
1669static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1670 struct snd_ctl_elem_value *ucontrol) {
1671 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001672 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001673}
1674
1675static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1676 struct snd_ctl_elem_value *ucontrol) {
1677 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001678 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001679}
1680
1681static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1682 struct snd_ctl_elem_info *uinfo)
1683{
1684 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1685 uinfo->count = 1;
1686
1687 uinfo->value.integer.min = 0;
1688 uinfo->value.integer.max = 0X1F;
1689 uinfo->value.integer.step = 1;
1690
1691 return 0;
1692}
1693
1694static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1695 struct snd_ctl_elem_value *ucontrol) {
1696
1697 u32 h_control = kcontrol->private_value;
1698 u16 status;
1699
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001700 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1701 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001702 ucontrol->value.integer.value[0] = status;
1703 return 0;
1704}
1705
Bill Pembertone23e7a12012-12-06 12:35:10 -05001706static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1707 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001708{
1709 struct snd_card *card = asihpi->card;
1710 struct snd_kcontrol_new snd_control;
1711
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001712 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001713 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1714 snd_control.info = snd_asihpi_aesebu_format_info;
1715 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1716 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1717
1718
1719 if (ctl_add(card, &snd_control, asihpi) < 0)
1720 return -EINVAL;
1721
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001722 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001723 snd_control.access =
1724 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1725 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1726 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1727
1728 return ctl_add(card, &snd_control, asihpi);
1729}
1730
1731static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_value *ucontrol) {
1733 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001734 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001735}
1736
1737static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1738 struct snd_ctl_elem_value *ucontrol) {
1739 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001740 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001741}
1742
1743
Bill Pembertone23e7a12012-12-06 12:35:10 -05001744static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1745 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001746{
1747 struct snd_card *card = asihpi->card;
1748 struct snd_kcontrol_new snd_control;
1749
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001750 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001751 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1752 snd_control.info = snd_asihpi_aesebu_format_info;
1753 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1754 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1755
1756 return ctl_add(card, &snd_control, asihpi);
1757}
1758
1759/*------------------------------------------------------------
1760 Tuner controls
1761 ------------------------------------------------------------*/
1762
1763/* Gain */
1764
1765static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1766 struct snd_ctl_elem_info *uinfo)
1767{
1768 u32 h_control = kcontrol->private_value;
1769 u16 err;
1770 short idx;
1771 u16 gain_range[3];
1772
1773 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001774 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001775 idx, &gain_range[idx]);
1776 if (err != 0)
1777 return err;
1778 }
1779
1780 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1781 uinfo->count = 1;
1782 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1783 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1784 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1785 return 0;
1786}
1787
1788static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1789 struct snd_ctl_elem_value *ucontrol)
1790{
1791 /*
1792 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1793 */
1794 u32 h_control = kcontrol->private_value;
1795 short gain;
1796
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001797 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001798 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1799
1800 return 0;
1801}
1802
1803static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *ucontrol)
1805{
1806 /*
1807 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1808 */
1809 u32 h_control = kcontrol->private_value;
1810 short gain;
1811
1812 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001813 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001814
1815 return 1;
1816}
1817
1818/* Band */
1819
1820static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1821 u16 *band_list, u32 len) {
1822 u32 h_control = kcontrol->private_value;
1823 u16 err = 0;
1824 u32 i;
1825
1826 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001827 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001828 h_control, i, &band_list[i]);
1829 if (err != 0)
1830 break;
1831 }
1832
1833 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1834 return -EIO;
1835
1836 return i;
1837}
1838
1839static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1840 struct snd_ctl_elem_info *uinfo)
1841{
1842 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1843 int num_bands = 0;
1844
1845 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1846 HPI_TUNER_BAND_LAST);
1847
1848 if (num_bands < 0)
1849 return num_bands;
1850
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001851 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001852}
1853
1854static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1855 struct snd_ctl_elem_value *ucontrol)
1856{
1857 u32 h_control = kcontrol->private_value;
1858 /*
1859 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1860 */
1861 u16 band, idx;
1862 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1863 u32 num_bands = 0;
1864
1865 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1866 HPI_TUNER_BAND_LAST);
1867
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001868 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001869
1870 ucontrol->value.enumerated.item[0] = -1;
1871 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1872 if (tuner_bands[idx] == band) {
1873 ucontrol->value.enumerated.item[0] = idx;
1874 break;
1875 }
1876
1877 return 0;
1878}
1879
1880static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1881 struct snd_ctl_elem_value *ucontrol)
1882{
1883 /*
1884 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1885 */
1886 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001887 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001888 u16 band;
1889 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1890 u32 num_bands = 0;
1891
1892 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1893 HPI_TUNER_BAND_LAST);
1894
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001895 idx = ucontrol->value.enumerated.item[0];
1896 if (idx >= ARRAY_SIZE(tuner_bands))
1897 idx = ARRAY_SIZE(tuner_bands) - 1;
1898 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001899 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001900
1901 return 1;
1902}
1903
1904/* Freq */
1905
1906static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1907 struct snd_ctl_elem_info *uinfo)
1908{
1909 u32 h_control = kcontrol->private_value;
1910 u16 err;
1911 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1912 u16 num_bands = 0, band_iter, idx;
1913 u32 freq_range[3], temp_freq_range[3];
1914
1915 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1916 HPI_TUNER_BAND_LAST);
1917
1918 freq_range[0] = INT_MAX;
1919 freq_range[1] = 0;
1920 freq_range[2] = INT_MAX;
1921
1922 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1923 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001924 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001925 idx, tuner_bands[band_iter],
1926 &temp_freq_range[idx]);
1927 if (err != 0)
1928 return err;
1929 }
1930
1931 /* skip band with bogus stepping */
1932 if (temp_freq_range[2] <= 0)
1933 continue;
1934
1935 if (temp_freq_range[0] < freq_range[0])
1936 freq_range[0] = temp_freq_range[0];
1937 if (temp_freq_range[1] > freq_range[1])
1938 freq_range[1] = temp_freq_range[1];
1939 if (temp_freq_range[2] < freq_range[2])
1940 freq_range[2] = temp_freq_range[2];
1941 }
1942
1943 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1944 uinfo->count = 1;
1945 uinfo->value.integer.min = ((int)freq_range[0]);
1946 uinfo->value.integer.max = ((int)freq_range[1]);
1947 uinfo->value.integer.step = ((int)freq_range[2]);
1948 return 0;
1949}
1950
1951static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
1952 struct snd_ctl_elem_value *ucontrol)
1953{
1954 u32 h_control = kcontrol->private_value;
1955 u32 freq;
1956
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001957 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001958 ucontrol->value.integer.value[0] = freq;
1959
1960 return 0;
1961}
1962
1963static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
1964 struct snd_ctl_elem_value *ucontrol)
1965{
1966 u32 h_control = kcontrol->private_value;
1967 u32 freq;
1968
1969 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001970 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001971
1972 return 1;
1973}
1974
1975/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05001976static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
1977 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001978{
1979 struct snd_card *card = asihpi->card;
1980 struct snd_kcontrol_new snd_control;
1981
1982 snd_control.private_value = hpi_ctl->h_control;
1983 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1984
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001985 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001986 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001987 snd_control.info = snd_asihpi_tuner_gain_info;
1988 snd_control.get = snd_asihpi_tuner_gain_get;
1989 snd_control.put = snd_asihpi_tuner_gain_put;
1990
1991 if (ctl_add(card, &snd_control, asihpi) < 0)
1992 return -EINVAL;
1993 }
1994
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001995 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001996 snd_control.info = snd_asihpi_tuner_band_info;
1997 snd_control.get = snd_asihpi_tuner_band_get;
1998 snd_control.put = snd_asihpi_tuner_band_put;
1999
2000 if (ctl_add(card, &snd_control, asihpi) < 0)
2001 return -EINVAL;
2002
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002003 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002004 snd_control.info = snd_asihpi_tuner_freq_info;
2005 snd_control.get = snd_asihpi_tuner_freq_get;
2006 snd_control.put = snd_asihpi_tuner_freq_put;
2007
2008 return ctl_add(card, &snd_control, asihpi);
2009}
2010
2011/*------------------------------------------------------------
2012 Meter controls
2013 ------------------------------------------------------------*/
2014static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2015 struct snd_ctl_elem_info *uinfo)
2016{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002017 u32 h_control = kcontrol->private_value;
2018 u32 count;
2019 u16 err;
2020 err = hpi_meter_query_channels(h_control, &count);
2021 if (err)
2022 count = HPI_MAX_CHANNELS;
2023
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002024 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002025 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002026 uinfo->value.integer.min = 0;
2027 uinfo->value.integer.max = 0x7FFFFFFF;
2028 return 0;
2029}
2030
2031/* linear values for 10dB steps */
2032static int log2lin[] = {
2033 0x7FFFFFFF, /* 0dB */
2034 679093956,
2035 214748365,
2036 67909396,
2037 21474837,
2038 6790940,
2039 2147484, /* -60dB */
2040 679094,
2041 214748, /* -80 */
2042 67909,
2043 21475, /* -100 */
2044 6791,
2045 2147,
2046 679,
2047 214,
2048 68,
2049 21,
2050 7,
2051 2
2052};
2053
2054static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2055 struct snd_ctl_elem_value *ucontrol)
2056{
2057 u32 h_control = kcontrol->private_value;
2058 short an_gain_mB[HPI_MAX_CHANNELS], i;
2059 u16 err;
2060
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002061 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002062
2063 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2064 if (err) {
2065 ucontrol->value.integer.value[i] = 0;
2066 } else if (an_gain_mB[i] >= 0) {
2067 ucontrol->value.integer.value[i] =
2068 an_gain_mB[i] << 16;
2069 } else {
2070 /* -ve is log value in millibels < -60dB,
2071 * convert to (roughly!) linear,
2072 */
2073 ucontrol->value.integer.value[i] =
2074 log2lin[an_gain_mB[i] / -1000];
2075 }
2076 }
2077 return 0;
2078}
2079
Bill Pembertone23e7a12012-12-06 12:35:10 -05002080static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2081 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002082{
2083 struct snd_card *card = asihpi->card;
2084 struct snd_kcontrol_new snd_control;
2085
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002086 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002087 snd_control.access =
2088 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2089 snd_control.info = snd_asihpi_meter_info;
2090 snd_control.get = snd_asihpi_meter_get;
2091
2092 snd_control.index = subidx;
2093
2094 return ctl_add(card, &snd_control, asihpi);
2095}
2096
2097/*------------------------------------------------------------
2098 Multiplexer controls
2099 ------------------------------------------------------------*/
2100static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2101{
2102 u32 h_control = snd_control->private_value;
2103 struct hpi_control hpi_ctl;
2104 int s, err;
2105 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002106 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002107 &hpi_ctl.
2108 src_node_type,
2109 &hpi_ctl.
2110 src_node_index);
2111 if (err)
2112 break;
2113 }
2114 return s;
2115}
2116
2117static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2118 struct snd_ctl_elem_info *uinfo)
2119{
2120 int err;
2121 u16 src_node_type, src_node_index;
2122 u32 h_control = kcontrol->private_value;
2123
2124 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2125 uinfo->count = 1;
2126 uinfo->value.enumerated.items =
2127 snd_card_asihpi_mux_count_sources(kcontrol);
2128
2129 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2130 uinfo->value.enumerated.item =
2131 uinfo->value.enumerated.items - 1;
2132
2133 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002134 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002135 uinfo->value.enumerated.item,
2136 &src_node_type, &src_node_index);
2137
2138 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002139 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002140 src_node_index);
2141 return 0;
2142}
2143
2144static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2145 struct snd_ctl_elem_value *ucontrol)
2146{
2147 u32 h_control = kcontrol->private_value;
2148 u16 source_type, source_index;
2149 u16 src_node_type, src_node_index;
2150 int s;
2151
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002152 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002153 &source_type, &source_index));
2154 /* Should cache this search result! */
2155 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002156 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002157 &src_node_type, &src_node_index))
2158 break;
2159
2160 if ((source_type == src_node_type)
2161 && (source_index == src_node_index)) {
2162 ucontrol->value.enumerated.item[0] = s;
2163 return 0;
2164 }
2165 }
2166 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002167 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002168 h_control, source_type, source_index);
2169 ucontrol->value.enumerated.item[0] = 0;
2170 return 0;
2171}
2172
2173static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2174 struct snd_ctl_elem_value *ucontrol)
2175{
2176 int change;
2177 u32 h_control = kcontrol->private_value;
2178 u16 source_type, source_index;
2179 u16 e;
2180
2181 change = 1;
2182
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002183 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002184 ucontrol->value.enumerated.item[0],
2185 &source_type, &source_index);
2186 if (!e)
2187 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002188 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002189 source_type, source_index));
2190 return change;
2191}
2192
2193
Bill Pembertone23e7a12012-12-06 12:35:10 -05002194static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2195 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002196{
2197 struct snd_card *card = asihpi->card;
2198 struct snd_kcontrol_new snd_control;
2199
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002200 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002201 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2202 snd_control.info = snd_asihpi_mux_info;
2203 snd_control.get = snd_asihpi_mux_get;
2204 snd_control.put = snd_asihpi_mux_put;
2205
2206 return ctl_add(card, &snd_control, asihpi);
2207
2208}
2209
2210/*------------------------------------------------------------
2211 Channel mode controls
2212 ------------------------------------------------------------*/
2213static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2214 struct snd_ctl_elem_info *uinfo)
2215{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002216 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2217 "invalid",
2218 "Normal", "Swap",
2219 "From Left", "From Right",
2220 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002221 };
2222
2223 u32 h_control = kcontrol->private_value;
2224 u16 mode;
2225 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002226 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002227 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002228
2229 /* HPI channel mode values can be from 1 to 6
2230 Some adapters only support a contiguous subset
2231 */
2232 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002233 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002234 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002235 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002236 valid_modes++;
2237 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002238
Takashi Iwai74eeb142012-01-09 18:26:05 +01002239 if (!valid_modes)
2240 return -EINVAL;
2241
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002242 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002243}
2244
2245static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2246 struct snd_ctl_elem_value *ucontrol)
2247{
2248 u32 h_control = kcontrol->private_value;
2249 u16 mode;
2250
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002251 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002252 mode = 1;
2253
2254 ucontrol->value.enumerated.item[0] = mode - 1;
2255
2256 return 0;
2257}
2258
2259static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2260 struct snd_ctl_elem_value *ucontrol)
2261{
2262 int change;
2263 u32 h_control = kcontrol->private_value;
2264
2265 change = 1;
2266
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002267 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002268 ucontrol->value.enumerated.item[0] + 1));
2269 return change;
2270}
2271
2272
Bill Pembertone23e7a12012-12-06 12:35:10 -05002273static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2274 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002275{
2276 struct snd_card *card = asihpi->card;
2277 struct snd_kcontrol_new snd_control;
2278
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002279 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002280 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2281 snd_control.info = snd_asihpi_cmode_info;
2282 snd_control.get = snd_asihpi_cmode_get;
2283 snd_control.put = snd_asihpi_cmode_put;
2284
2285 return ctl_add(card, &snd_control, asihpi);
2286}
2287
2288/*------------------------------------------------------------
2289 Sampleclock source controls
2290 ------------------------------------------------------------*/
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002291static char *sampleclock_sources[MAX_CLOCKSOURCES] = {
2292 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2293 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
2294 "Prev Module",
2295 "Digital2", "Digital3", "Digital4", "Digital5",
2296 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002297
2298static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2299 struct snd_ctl_elem_info *uinfo)
2300{
2301 struct snd_card_asihpi *asihpi =
2302 (struct snd_card_asihpi *)(kcontrol->private_data);
2303 struct clk_cache *clkcache = &asihpi->cc;
2304 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2305 uinfo->count = 1;
2306 uinfo->value.enumerated.items = clkcache->count;
2307
2308 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2309 uinfo->value.enumerated.item =
2310 uinfo->value.enumerated.items - 1;
2311
2312 strcpy(uinfo->value.enumerated.name,
2313 clkcache->s[uinfo->value.enumerated.item].name);
2314 return 0;
2315}
2316
2317static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2318 struct snd_ctl_elem_value *ucontrol)
2319{
2320 struct snd_card_asihpi *asihpi =
2321 (struct snd_card_asihpi *)(kcontrol->private_data);
2322 struct clk_cache *clkcache = &asihpi->cc;
2323 u32 h_control = kcontrol->private_value;
2324 u16 source, srcindex = 0;
2325 int i;
2326
2327 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002328 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002329 source = 0;
2330
2331 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002332 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002333 srcindex = 0;
2334
2335 for (i = 0; i < clkcache->count; i++)
2336 if ((clkcache->s[i].source == source) &&
2337 (clkcache->s[i].index == srcindex))
2338 break;
2339
2340 ucontrol->value.enumerated.item[0] = i;
2341
2342 return 0;
2343}
2344
2345static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2346 struct snd_ctl_elem_value *ucontrol)
2347{
2348 struct snd_card_asihpi *asihpi =
2349 (struct snd_card_asihpi *)(kcontrol->private_data);
2350 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002351 unsigned int item;
2352 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002353 u32 h_control = kcontrol->private_value;
2354
2355 change = 1;
2356 item = ucontrol->value.enumerated.item[0];
2357 if (item >= clkcache->count)
2358 item = clkcache->count-1;
2359
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002360 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002361 h_control, clkcache->s[item].source));
2362
2363 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002364 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002365 h_control, clkcache->s[item].index));
2366 return change;
2367}
2368
2369/*------------------------------------------------------------
2370 Clkrate controls
2371 ------------------------------------------------------------*/
2372/* Need to change this to enumerated control with list of rates */
2373static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2374 struct snd_ctl_elem_info *uinfo)
2375{
2376 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2377 uinfo->count = 1;
2378 uinfo->value.integer.min = 8000;
2379 uinfo->value.integer.max = 192000;
2380 uinfo->value.integer.step = 100;
2381
2382 return 0;
2383}
2384
2385static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_value *ucontrol)
2387{
2388 u32 h_control = kcontrol->private_value;
2389 u32 rate;
2390 u16 e;
2391
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002392 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002393 if (!e)
2394 ucontrol->value.integer.value[0] = rate;
2395 else
2396 ucontrol->value.integer.value[0] = 0;
2397 return 0;
2398}
2399
2400static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2401 struct snd_ctl_elem_value *ucontrol)
2402{
2403 int change;
2404 u32 h_control = kcontrol->private_value;
2405
2406 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2407 asihpi->mixer_clkrate[addr][1] != right;
2408 */
2409 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002410 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002411 ucontrol->value.integer.value[0]));
2412 return change;
2413}
2414
2415static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_info *uinfo)
2417{
2418 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2419 uinfo->count = 1;
2420 uinfo->value.integer.min = 8000;
2421 uinfo->value.integer.max = 192000;
2422 uinfo->value.integer.step = 100;
2423
2424 return 0;
2425}
2426
2427static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2428 struct snd_ctl_elem_value *ucontrol)
2429{
2430 u32 h_control = kcontrol->private_value;
2431 u32 rate;
2432 u16 e;
2433
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002434 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002435 if (!e)
2436 ucontrol->value.integer.value[0] = rate;
2437 else
2438 ucontrol->value.integer.value[0] = 0;
2439 return 0;
2440}
2441
Bill Pembertone23e7a12012-12-06 12:35:10 -05002442static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2443 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002444{
2445 struct snd_card *card = asihpi->card;
2446 struct snd_kcontrol_new snd_control;
2447
2448 struct clk_cache *clkcache = &asihpi->cc;
2449 u32 hSC = hpi_ctl->h_control;
2450 int has_aes_in = 0;
2451 int i, j;
2452 u16 source;
2453
2454 snd_control.private_value = hpi_ctl->h_control;
2455
2456 clkcache->has_local = 0;
2457
2458 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002459 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002460 i, &source))
2461 break;
2462 clkcache->s[i].source = source;
2463 clkcache->s[i].index = 0;
2464 clkcache->s[i].name = sampleclock_sources[source];
2465 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2466 has_aes_in = 1;
2467 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2468 clkcache->has_local = 1;
2469 }
2470 if (has_aes_in)
2471 /* already will have picked up index 0 above */
2472 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002473 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002474 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2475 &source))
2476 break;
2477 clkcache->s[i].source =
2478 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2479 clkcache->s[i].index = j;
2480 clkcache->s[i].name = sampleclock_sources[
2481 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2482 i++;
2483 }
2484 clkcache->count = i;
2485
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002486 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002487 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2488 snd_control.info = snd_asihpi_clksrc_info;
2489 snd_control.get = snd_asihpi_clksrc_get;
2490 snd_control.put = snd_asihpi_clksrc_put;
2491 if (ctl_add(card, &snd_control, asihpi) < 0)
2492 return -EINVAL;
2493
2494
2495 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002496 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002497 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2498 snd_control.info = snd_asihpi_clklocal_info;
2499 snd_control.get = snd_asihpi_clklocal_get;
2500 snd_control.put = snd_asihpi_clklocal_put;
2501
2502
2503 if (ctl_add(card, &snd_control, asihpi) < 0)
2504 return -EINVAL;
2505 }
2506
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002507 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002508 snd_control.access =
2509 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2510 snd_control.info = snd_asihpi_clkrate_info;
2511 snd_control.get = snd_asihpi_clkrate_get;
2512
2513 return ctl_add(card, &snd_control, asihpi);
2514}
2515/*------------------------------------------------------------
2516 Mixer
2517 ------------------------------------------------------------*/
2518
Bill Pembertone23e7a12012-12-06 12:35:10 -05002519static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002520{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002521 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002522 unsigned int idx = 0;
2523 unsigned int subindex = 0;
2524 int err;
2525 struct hpi_control hpi_ctl, prev_ctl;
2526
2527 if (snd_BUG_ON(!asihpi))
2528 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002529 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002530 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002531
2532 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002533 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002534 &asihpi->h_mixer);
2535 hpi_handle_error(err);
2536 if (err)
2537 return -err;
2538
Takashi Iwai21896bc2010-06-02 12:08:37 +02002539 memset(&prev_ctl, 0, sizeof(prev_ctl));
2540 prev_ctl.control_type = -1;
2541
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002542 for (idx = 0; idx < 2000; idx++) {
2543 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002544 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002545 idx,
2546 &hpi_ctl.src_node_type,
2547 &hpi_ctl.src_node_index,
2548 &hpi_ctl.dst_node_type,
2549 &hpi_ctl.dst_node_index,
2550 &hpi_ctl.control_type,
2551 &hpi_ctl.h_control);
2552 if (err) {
2553 if (err == HPI_ERROR_CONTROL_DISABLED) {
2554 if (mixer_dump)
2555 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002556 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002557 idx);
2558 continue;
2559 } else
2560 break;
2561
2562 }
2563
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002564 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2565 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002566
2567 /* ASI50xx in SSX mode has multiple meters on the same node.
2568 Use subindex to create distinct ALSA controls
2569 for any duplicated controls.
2570 */
2571 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2572 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2573 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2574 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2575 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2576 subindex++;
2577 else
2578 subindex = 0;
2579
2580 prev_ctl = hpi_ctl;
2581
2582 switch (hpi_ctl.control_type) {
2583 case HPI_CONTROL_VOLUME:
2584 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2585 break;
2586 case HPI_CONTROL_LEVEL:
2587 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2588 break;
2589 case HPI_CONTROL_MULTIPLEXER:
2590 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2591 break;
2592 case HPI_CONTROL_CHANNEL_MODE:
2593 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2594 break;
2595 case HPI_CONTROL_METER:
2596 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2597 break;
2598 case HPI_CONTROL_SAMPLECLOCK:
2599 err = snd_asihpi_sampleclock_add(
2600 asihpi, &hpi_ctl);
2601 break;
2602 case HPI_CONTROL_CONNECTION: /* ignore these */
2603 continue;
2604 case HPI_CONTROL_TUNER:
2605 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2606 break;
2607 case HPI_CONTROL_AESEBU_TRANSMITTER:
2608 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2609 break;
2610 case HPI_CONTROL_AESEBU_RECEIVER:
2611 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2612 break;
2613 case HPI_CONTROL_VOX:
2614 case HPI_CONTROL_BITSTREAM:
2615 case HPI_CONTROL_MICROPHONE:
2616 case HPI_CONTROL_PARAMETRIC_EQ:
2617 case HPI_CONTROL_COMPANDER:
2618 default:
2619 if (mixer_dump)
2620 snd_printk(KERN_INFO
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002621 "Untranslated HPI Control"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002622 "(%d) %d %d %d %d %d\n",
2623 idx,
2624 hpi_ctl.control_type,
2625 hpi_ctl.src_node_type,
2626 hpi_ctl.src_node_index,
2627 hpi_ctl.dst_node_type,
2628 hpi_ctl.dst_node_index);
2629 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002630 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002631 if (err < 0)
2632 return err;
2633 }
2634 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2635 hpi_handle_error(err);
2636
2637 snd_printk(KERN_INFO "%d mixer controls found\n", idx);
2638
2639 return 0;
2640}
2641
2642/*------------------------------------------------------------
2643 /proc interface
2644 ------------------------------------------------------------*/
2645
2646static void
2647snd_asihpi_proc_read(struct snd_info_entry *entry,
2648 struct snd_info_buffer *buffer)
2649{
2650 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002651 u32 h_control;
2652 u32 rate = 0;
2653 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002654
2655 u16 num_outstreams;
2656 u16 num_instreams;
2657 u16 version;
2658 u32 serial_number;
2659 u16 type;
2660
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002661 int err;
2662
2663 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002664
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002665 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2666 &num_outstreams, &num_instreams,
2667 &version, &serial_number, &type));
2668
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002669 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002670 "Adapter type ASI%4X\nHardware Index %d\n"
2671 "%d outstreams\n%d instreams\n",
2672 type, asihpi->hpi->adapter->index,
2673 num_outstreams, num_instreams);
2674
2675 snd_iprintf(buffer,
2676 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2677 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002678 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2679
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002680 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002681 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2682 HPI_CONTROL_SAMPLECLOCK, &h_control);
2683
2684 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002685 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002686 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002687
2688 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002689 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002690 rate, sampleclock_sources[source]);
2691 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002692}
2693
Bill Pembertone23e7a12012-12-06 12:35:10 -05002694static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002695{
2696 struct snd_info_entry *entry;
2697
2698 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2699 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2700}
2701
2702/*------------------------------------------------------------
2703 HWDEP
2704 ------------------------------------------------------------*/
2705
2706static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2707{
2708 if (enable_hpi_hwdep)
2709 return 0;
2710 else
2711 return -ENODEV;
2712
2713}
2714
2715static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2716{
2717 if (enable_hpi_hwdep)
2718 return asihpi_hpi_release(file);
2719 else
2720 return -ENODEV;
2721}
2722
2723static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2724 unsigned int cmd, unsigned long arg)
2725{
2726 if (enable_hpi_hwdep)
2727 return asihpi_hpi_ioctl(file, cmd, arg);
2728 else
2729 return -ENODEV;
2730}
2731
2732
2733/* results in /dev/snd/hwC#D0 file for each card with index #
2734 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2735*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002736static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi,
2737 int device, struct snd_hwdep **rhwdep)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002738{
2739 struct snd_hwdep *hw;
2740 int err;
2741
2742 if (rhwdep)
2743 *rhwdep = NULL;
2744 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2745 if (err < 0)
2746 return err;
2747 strcpy(hw->name, "asihpi (HPI)");
2748 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2749 hw->ops.open = snd_asihpi_hpi_open;
2750 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2751 hw->ops.release = snd_asihpi_hpi_release;
2752 hw->private_data = asihpi;
2753 if (rhwdep)
2754 *rhwdep = hw;
2755 return 0;
2756}
2757
2758/*------------------------------------------------------------
2759 CARD
2760 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002761static int snd_asihpi_probe(struct pci_dev *pci_dev,
2762 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002763{
2764 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002765 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002766 struct snd_card *card;
2767 struct snd_card_asihpi *asihpi;
2768
2769 u32 h_control;
2770 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002771 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002772
2773 static int dev;
2774 if (dev >= SNDRV_CARDS)
2775 return -ENODEV;
2776
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002777 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002778 if (!enable[dev]) {
2779 dev++;
2780 return -ENOENT;
2781 }
2782
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002783 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002784 err = asihpi_adapter_probe(pci_dev, pci_id);
2785 if (err < 0)
2786 return err;
2787
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002788 hpi = pci_get_drvdata(pci_dev);
2789 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002790 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002791 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2792 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002793 if (err < 0) {
2794 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002795 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2796 THIS_MODULE, sizeof(struct snd_card_asihpi),
2797 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002798 if (err < 0)
2799 return err;
2800 snd_printk(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002801 "**** WARNING **** Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002802 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002803 }
2804
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002805 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002806 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002807 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002808 asihpi->hpi = hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002809
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002810 snd_printk(KERN_INFO "adapter ID=%4X index=%d\n",
2811 asihpi->hpi->adapter->type, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002812
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002813 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002814 HPI_ADAPTER_PROPERTY_CAPS1,
2815 NULL, &asihpi->support_grouping);
2816 if (err)
2817 asihpi->support_grouping = 0;
2818
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002819 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002820 HPI_ADAPTER_PROPERTY_CAPS2,
2821 &asihpi->support_mrx, NULL);
2822 if (err)
2823 asihpi->support_mrx = 0;
2824
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002825 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002826 HPI_ADAPTER_PROPERTY_INTERVAL,
2827 NULL, &asihpi->update_interval_frames);
2828 if (err)
2829 asihpi->update_interval_frames = 512;
2830
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002831 if (!asihpi->can_dma)
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002832 asihpi->update_interval_frames *= 2;
2833
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002834 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002835 0, &h_stream));
2836
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002837 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002838 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002839
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002840 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002841
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002842 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002843 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2844 &asihpi->in_max_chans, &asihpi->out_max_chans);
2845 if (err) {
2846 asihpi->in_max_chans = 2;
2847 asihpi->out_max_chans = 2;
2848 }
2849
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002850 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2851 asihpi->out_min_chans = asihpi->out_max_chans;
2852 asihpi->in_min_chans = asihpi->in_max_chans;
2853 asihpi->support_grouping = 0;
2854 } else {
2855 asihpi->out_min_chans = 1;
2856 asihpi->in_min_chans = 1;
2857 }
2858
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002859 snd_printk(KERN_INFO "Has dma:%d, grouping:%d, mrx:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002860 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002861 asihpi->support_grouping,
2862 asihpi->support_mrx
2863 );
2864
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002865 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002866 if (err < 0) {
2867 snd_printk(KERN_ERR "pcm_new failed\n");
2868 goto __nodev;
2869 }
2870 err = snd_card_asihpi_mixer_new(asihpi);
2871 if (err < 0) {
2872 snd_printk(KERN_ERR "mixer_new failed\n");
2873 goto __nodev;
2874 }
2875
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002876 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002877 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2878 HPI_CONTROL_SAMPLECLOCK, &h_control);
2879
2880 if (!err)
2881 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002882 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002883
2884 snd_asihpi_proc_init(asihpi);
2885
2886 /* always create, can be enabled or disabled dynamically
2887 by enable_hwdep module param*/
2888 snd_asihpi_hpi_new(asihpi, 0, NULL);
2889
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002890 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002892 sprintf(card->shortname, "AudioScience ASI%4X",
2893 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002895 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002896 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002897
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002898 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002899 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002900 dev++;
2901 return 0;
2902 }
2903__nodev:
2904 snd_card_free(card);
2905 snd_printk(KERN_ERR "snd_asihpi_probe error %d\n", err);
2906 return err;
2907
2908}
2909
Bill Pembertone23e7a12012-12-06 12:35:10 -05002910static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002911{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002912 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
2913 snd_card_free(hpi->snd_card);
2914 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002915 asihpi_adapter_remove(pci_dev);
2916}
2917
Benoit Taine9baa3c32014-08-08 15:56:03 +02002918static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002919 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
2920 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2921 (kernel_ulong_t)HPI_6205},
2922 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
2923 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
2924 (kernel_ulong_t)HPI_6000},
2925 {0,}
2926};
2927MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
2928
2929static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02002930 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002931 .id_table = asihpi_pci_tbl,
2932 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05002933 .remove = snd_asihpi_remove,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002934};
2935
2936static int __init snd_asihpi_init(void)
2937{
2938 asihpi_init();
2939 return pci_register_driver(&driver);
2940}
2941
2942static void __exit snd_asihpi_exit(void)
2943{
2944
2945 pci_unregister_driver(&driver);
2946 asihpi_exit();
2947}
2948
2949module_init(snd_asihpi_init)
2950module_exit(snd_asihpi_exit)
2951