blob: 614322171c79373c8fb54a17e03159aa4c42a894 [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*
2 * Asihpi soundcard
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003 * Copyright (c) by AudioScience Inc <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02004 *
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;
Eliot Blennerhassett3872f192014-11-20 16:22:49 +1300112 const char *name;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200113};
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
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300127 /* In low latency mode there is only one stream, a pointer to its
128 * private data is stored here on trigger and cleared on stop.
129 * The interrupt handler uses it as a parameter when calling
130 * snd_card_asihpi_timer_function().
131 */
132 struct snd_card_asihpi_pcm *llmode_streampriv;
133 struct tasklet_struct t;
134 void (*pcm_start)(struct snd_pcm_substream *substream);
135 void (*pcm_stop)(struct snd_pcm_substream *substream);
136
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200137 u32 h_mixer;
138 struct clk_cache cc;
139
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200140 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200141 u16 support_grouping;
142 u16 support_mrx;
143 u16 update_interval_frames;
144 u16 in_max_chans;
145 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300146 u16 in_min_chans;
147 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200148};
149
150/* Per stream data */
151struct snd_card_asihpi_pcm {
152 struct timer_list timer;
153 unsigned int respawn_timer;
154 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300155 unsigned int buffer_bytes;
156 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200157 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300158 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
159 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
160 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200161 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200162 struct snd_pcm_substream *substream;
163 u32 h_stream;
164 struct hpi_format format;
165};
166
167/* universal stream verbs work with out or in stream handles */
168
169/* Functions to allow driver to give a buffer to HPI for busmastering */
170
171static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200172 u32 h_stream, /* handle to outstream. */
173 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
174 u32 pci_address
175)
176{
177 struct hpi_message hm;
178 struct hpi_response hr;
179 unsigned int obj = hpi_handle_object(h_stream);
180
181 if (!h_stream)
182 return HPI_ERROR_INVALID_OBJ;
183 hpi_init_message_response(&hm, &hr, obj,
184 obj == HPI_OBJ_OSTREAM ?
185 HPI_OSTREAM_HOSTBUFFER_ALLOC :
186 HPI_ISTREAM_HOSTBUFFER_ALLOC);
187
188 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
189 &hm.obj_index);
190
191 hm.u.d.u.buffer.buffer_size = size_in_bytes;
192 hm.u.d.u.buffer.pci_address = pci_address;
193 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
194 hpi_send_recv(&hm, &hr);
195 return hr.error;
196}
197
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300198static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200199{
200 struct hpi_message hm;
201 struct hpi_response hr;
202 unsigned int obj = hpi_handle_object(h_stream);
203
204 if (!h_stream)
205 return HPI_ERROR_INVALID_OBJ;
206
207 hpi_init_message_response(&hm, &hr, obj,
208 obj == HPI_OBJ_OSTREAM ?
209 HPI_OSTREAM_HOSTBUFFER_FREE :
210 HPI_ISTREAM_HOSTBUFFER_FREE);
211
212 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
213 &hm.obj_index);
214 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
215 hpi_send_recv(&hm, &hr);
216 return hr.error;
217}
218
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220{
221 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300222 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200223 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300224 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225}
226
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300227static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200228{
229 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300230 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300232 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200233}
234
235static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236 u32 h_stream,
237 u16 *pw_state,
238 u32 *pbuffer_size,
239 u32 *pdata_in_buffer,
240 u32 *psample_count,
241 u32 *pauxiliary_data
242)
243{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300244 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200245 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300246 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200247 pbuffer_size, pdata_in_buffer,
248 psample_count, pauxiliary_data);
249 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300250 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251 pbuffer_size, pdata_in_buffer,
252 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300253 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254}
255
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257 u32 h_master,
258 u32 h_stream)
259{
260 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264}
265
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300266static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267{
268 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272}
273
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300274static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275 u32 h_stream, u32 *mo, u32 *mi)
276{
277 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300278 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200279 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300280 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281}
282
283static u16 handle_error(u16 err, int line, char *filename)
284{
285 if (err)
286 printk(KERN_WARNING
287 "in file %s, line %d: HPI error %d\n",
288 filename, line, err);
289 return err;
290}
291
292#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
293
294/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200295
296static void print_hwparams(struct snd_pcm_substream *substream,
297 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200298{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200299 char name[16];
300 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300301 snd_printdd("%s HWPARAMS\n", name);
302 snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
303 params_rate(p), params_channels(p),
304 params_format(p), params_subformat(p));
305 snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
306 params_buffer_bytes(p), params_period_bytes(p),
307 params_period_size(p), params_periods(p));
308 snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
309 params_buffer_size(p), params_access(p),
310 params_rate(p) * params_channels(p) *
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200311 snd_pcm_format_width(params_format(p)) / 8);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200312}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200313
314static snd_pcm_format_t hpi_to_alsa_formats[] = {
315 -1, /* INVALID */
316 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
317 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
318 -1, /* HPI_FORMAT_MPEG_L1 3 */
319 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
320 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
321 -1, /* HPI_FORMAT_DOLBY_AC2 6 */
322 -1, /* HPI_FORMAT_DOLBY_AC3 7 */
323 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
324 -1, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
325 -1, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
326 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
327 -1, /* HPI_FORMAT_RAW_BITSTREAM 12 */
328 -1, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
329 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
330#if 1
331 /* ALSA can't handle 3 byte sample size together with power-of-2
332 * constraint on buffer_bytes, so disable this format
333 */
334 -1
335#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300336 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200337#endif
338};
339
340
341static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
342 u16 *hpi_format)
343{
344 u16 format;
345
346 for (format = HPI_FORMAT_PCM8_UNSIGNED;
347 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
348 if (hpi_to_alsa_formats[format] == alsa_format) {
349 *hpi_format = format;
350 return 0;
351 }
352 }
353
354 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
355 alsa_format);
356 *hpi_format = 0;
357 return -EINVAL;
358}
359
360static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
361 struct snd_pcm_hardware *pcmhw)
362{
363 u16 err;
364 u32 h_control;
365 u32 sample_rate;
366 int idx;
367 unsigned int rate_min = 200000;
368 unsigned int rate_max = 0;
369 unsigned int rates = 0;
370
371 if (asihpi->support_mrx) {
372 rates |= SNDRV_PCM_RATE_CONTINUOUS;
373 rates |= SNDRV_PCM_RATE_8000_96000;
374 rate_min = 8000;
375 rate_max = 100000;
376 } else {
377 /* on cards without SRC,
378 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300379 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200380 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
381 HPI_CONTROL_SAMPLECLOCK, &h_control);
382 if (err) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +1300383 dev_err(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300384 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200385 }
386
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300387 for (idx = -1; idx < 100; idx++) {
388 if (idx == -1) {
389 if (hpi_sample_clock_get_sample_rate(h_control,
390 &sample_rate))
391 continue;
392 } else if (hpi_sample_clock_query_local_rate(h_control,
393 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200394 break;
395 }
396
397 rate_min = min(rate_min, sample_rate);
398 rate_max = max(rate_max, sample_rate);
399
400 switch (sample_rate) {
401 case 5512:
402 rates |= SNDRV_PCM_RATE_5512;
403 break;
404 case 8000:
405 rates |= SNDRV_PCM_RATE_8000;
406 break;
407 case 11025:
408 rates |= SNDRV_PCM_RATE_11025;
409 break;
410 case 16000:
411 rates |= SNDRV_PCM_RATE_16000;
412 break;
413 case 22050:
414 rates |= SNDRV_PCM_RATE_22050;
415 break;
416 case 32000:
417 rates |= SNDRV_PCM_RATE_32000;
418 break;
419 case 44100:
420 rates |= SNDRV_PCM_RATE_44100;
421 break;
422 case 48000:
423 rates |= SNDRV_PCM_RATE_48000;
424 break;
425 case 64000:
426 rates |= SNDRV_PCM_RATE_64000;
427 break;
428 case 88200:
429 rates |= SNDRV_PCM_RATE_88200;
430 break;
431 case 96000:
432 rates |= SNDRV_PCM_RATE_96000;
433 break;
434 case 176400:
435 rates |= SNDRV_PCM_RATE_176400;
436 break;
437 case 192000:
438 rates |= SNDRV_PCM_RATE_192000;
439 break;
440 default: /* some other rate */
441 rates |= SNDRV_PCM_RATE_KNOT;
442 }
443 }
444 }
445
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200446 pcmhw->rates = rates;
447 pcmhw->rate_min = rate_min;
448 pcmhw->rate_max = rate_max;
449}
450
451static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
452 struct snd_pcm_hw_params *params)
453{
454 struct snd_pcm_runtime *runtime = substream->runtime;
455 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
456 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
457 int err;
458 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400459 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200460 unsigned int bytes_per_sec;
461
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200462 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200463 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
464 if (err < 0)
465 return err;
466 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
467 if (err)
468 return err;
469
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200470 hpi_handle_error(hpi_format_create(&dpcm->format,
471 params_channels(params),
472 format, params_rate(params), 0, 0));
473
474 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300475 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200476 return -EINVAL;
477
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300478 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 dpcm->h_stream, &dpcm->format) != 0)
480 return -EINVAL;
481 }
482
483 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200484 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300485 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200486 params_buffer_bytes(params), runtime->dma_addr);
487 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300488 snd_printdd(
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300489 "stream_host_buffer_attach success %u %lu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200490 params_buffer_bytes(params),
491 (unsigned long)runtime->dma_addr);
492 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300493 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200494 err);
495 return -ENOMEM;
496 }
497
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300498 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300499 &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500 }
501 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400502 width = snd_pcm_format_width(params_format(params));
503 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200504 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400505 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200506 return -EINVAL;
507
508 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300509 dpcm->buffer_bytes = params_buffer_bytes(params);
510 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200511
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200512 return 0;
513}
514
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300515static int
516snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
517{
518 struct snd_pcm_runtime *runtime = substream->runtime;
519 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
520 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300521 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300522
523 snd_pcm_lib_free_pages(substream);
524 return 0;
525}
526
527static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
528{
529 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
530 kfree(dpcm);
531}
532
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200533static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
534 substream)
535{
536 struct snd_pcm_runtime *runtime = substream->runtime;
537 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
538 int expiry;
539
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300540 expiry = HZ / 200;
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +1300541
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300542 expiry = max(expiry, 1); /* don't let it be zero! */
Takashi Iwai6fec2b52015-01-19 11:32:51 +0100543 mod_timer(&dpcm->timer, jiffies + expiry);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200544 dpcm->respawn_timer = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200545}
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
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300556static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
557{
558 struct snd_card_asihpi_pcm *dpcm;
559 struct snd_card_asihpi *card;
560
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300561 dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
562 card = snd_pcm_substream_chip(substream);
563
Takashi Iwai2a0d85d2017-09-07 10:51:54 +0200564 WARN_ON(in_interrupt());
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300565 tasklet_disable(&card->t);
566 card->llmode_streampriv = dpcm;
567 tasklet_enable(&card->t);
568
569 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
570 HPI_ADAPTER_PROPERTY_IRQ_RATE,
571 card->update_interval_frames, 0));
572}
573
574static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
575{
576 struct snd_card_asihpi_pcm *dpcm;
577 struct snd_card_asihpi *card;
578
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300579 dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
580 card = snd_pcm_substream_chip(substream);
581
582 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
583 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
584
585 if (in_interrupt())
586 card->llmode_streampriv = NULL;
587 else {
588 tasklet_disable(&card->t);
589 card->llmode_streampriv = NULL;
590 tasklet_enable(&card->t);
591 }
592}
593
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200594static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
595 int cmd)
596{
597 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
598 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
599 struct snd_pcm_substream *s;
600 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200601 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200603 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200604
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200605 switch (cmd) {
606 case SNDRV_PCM_TRIGGER_START:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300607 snd_printdd("%s trigger start\n", name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200608 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300609 struct snd_pcm_runtime *runtime = s->runtime;
610 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200611
612 if (snd_pcm_substream_chip(s) != card)
613 continue;
614
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300615 /* don't link Cap and Play */
616 if (substream->stream != s->stream)
617 continue;
618
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200619 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200620 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200621 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300622 * in buffer? Must be at least one period!
623 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200624 * buffer is bigger it may contain even more
625 * data??
626 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300627 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300628 snd_printddd("%d preload %d\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200629 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300630 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300631 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200632 preload,
633 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300634 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200635 }
636
637 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200638 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300639 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200640 dpcm->h_stream,
641 ds->h_stream);
642 if (!e) {
643 snd_pcm_trigger_done(s, substream);
644 } else {
645 hpi_handle_error(e);
646 break;
647 }
648 } else
649 break;
650 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200651 /* start the master stream */
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300652 card->pcm_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300653 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200654 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300655 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 break;
657
658 case SNDRV_PCM_TRIGGER_STOP:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300659 snd_printdd("%s trigger stop\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300660 card->pcm_stop(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661 snd_pcm_group_for_each_entry(s, substream) {
662 if (snd_pcm_substream_chip(s) != card)
663 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300664 /* don't link Cap and Play */
665 if (substream->stream != s->stream)
666 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200667
668 /*? workaround linked streams don't
669 transition to SETUP 20070706*/
670 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
671
672 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200673 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674 snd_pcm_trigger_done(s, substream);
675 } else
676 break;
677 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678
679 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300680 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200681 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
682 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300683 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684
685 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300686 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200687 break;
688
689 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300690 snd_printdd("%s trigger pause release\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300691 card->pcm_start(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300692 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200693 break;
694 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300695 snd_printdd("%s trigger pause push\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300696 card->pcm_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300697 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200698 break;
699 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 return -EINVAL;
702 }
703
704 return 0;
705}
706
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707/*algorithm outline
708 Without linking degenerates to getting single stream pos etc
709 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
710*/
711/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300712pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200713for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300714 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300715 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300716 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200717}
718timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
719for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300720 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300721 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200722 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300723 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200724 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300725 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200726 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300727 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200728 }
729 }
730 snd_pcm_period_elapsed(s);
731 }
732}
733*/
734
735/** Minimum of 2 modulo values. Works correctly when the difference between
736* the values is less than half the modulus
737*/
738static inline unsigned int modulo_min(unsigned int a, unsigned int b,
739 unsigned long int modulus)
740{
741 unsigned int result;
742 if (((a-b) % modulus) < (modulus/2))
743 result = b;
744 else
745 result = a;
746
747 return result;
748}
749
750/** Timer function, equivalent to interrupt service routine for cards
751*/
Kees Cook394ca812017-10-04 17:53:40 -0700752static void snd_card_asihpi_timer_function(struct timer_list *t)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200753{
Kees Cook394ca812017-10-04 17:53:40 -0700754 struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300755 struct snd_pcm_substream *substream = dpcm->substream;
756 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200757 struct snd_pcm_runtime *runtime;
758 struct snd_pcm_substream *s;
759 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300760 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200761 unsigned int remdata, xfercount, next_jiffies;
762 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300763 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200764 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300765 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200766 char name[16];
767
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200768
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300769 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300770
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200771 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300772 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200773 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
774 runtime = s->runtime;
775
776 if (snd_pcm_substream_chip(s) != card)
777 continue;
778
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300779 /* don't link Cap and Play */
780 if (substream->stream != s->stream)
781 continue;
782
783 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200784 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300785 &buffer_size, &bytes_avail,
786 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200787
788 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300789 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200790
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200791 if (!card->can_dma)
792 on_card_bytes = bytes_avail;
793
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300794 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300795 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300796 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300797 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300798 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300799 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200800 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300801 }
802 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300803 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300804 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200805 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300806 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100807 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200808 continue;
809 }
810 } else {
811 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300812 }
813 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300814 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200815
816 if (first) {
817 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300818 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300819 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200820 first = 0;
821 } else {
822 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300823 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 newdata);
827 }
828
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300829 snd_printddd(
830 "timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300831 name, s->number, state,
832 ds->pcm_buf_elapsed_dma_ofs,
833 ds->pcm_buf_host_rw_ofs,
834 pcm_buf_dma_ofs,
835 (int)bytes_avail,
836
837 (int)on_card_bytes,
838 buffer_size-bytes_avail,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200839 (unsigned long)frames_to_bytes(runtime,
840 runtime->status->hw_ptr),
841 (unsigned long)frames_to_bytes(runtime,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300842 runtime->control->appl_ptr)
843 );
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300844 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200845 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300846 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200847
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300848 remdata = newdata % dpcm->period_bytes;
849 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300850 /* come back when on_card_bytes has decreased enough to allow
851 write to happen, or when data has been consumed to make another
852 period
853 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300854 if (xfercount && (on_card_bytes > dpcm->period_bytes))
855 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300856 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300857 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300858
859 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200860 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300861 snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300862 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
863
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300864 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200865 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300866 runtime = s->runtime;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200867
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300868 /* don't link Cap and Play */
869 if (substream->stream != s->stream)
870 continue;
871
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300872 /* Store dma offset for use by pointer callback */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300873 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
874
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200875 if (xfercount &&
876 /* Limit use of on card fifo for playback */
877 ((on_card_bytes <= ds->period_bytes) ||
878 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
879
880 {
881
882 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
883 unsigned int xfer1, xfer2;
884 char *pd = &s->runtime->dma_area[buf_ofs];
885
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200886 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200887 xfer1 = xfercount;
888 xfer2 = 0;
889 } else {
890 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
891 xfer2 = xfercount - xfer1;
892 }
893
894 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300895 snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200896 s->number, xfer1, buf_ofs);
897 hpi_handle_error(
898 hpi_outstream_write_buf(
899 ds->h_stream, pd, xfer1,
900 &ds->format));
901
902 if (xfer2) {
903 pd = s->runtime->dma_area;
904
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300905 snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200906 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200907 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200908 hpi_handle_error(
909 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200910 ds->h_stream, pd,
911 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200912 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200913 }
914 } else {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300915 snd_printddd("read1, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200916 s->number, xfer1);
917 hpi_handle_error(
918 hpi_instream_read_buf(
919 ds->h_stream,
920 pd, xfer1));
921 if (xfer2) {
922 pd = s->runtime->dma_area;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300923 snd_printddd("read2, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200924 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200925 hpi_handle_error(
926 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300927 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200928 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200929 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200930 }
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300931 /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300932 ds->pcm_buf_host_rw_ofs += xfercount;
933 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200934 snd_pcm_period_elapsed(s);
935 }
936 }
937
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300938 if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200939 add_timer(&dpcm->timer);
940}
941
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300942static void snd_card_asihpi_int_task(unsigned long data)
943{
944 struct hpi_adapter *a = (struct hpi_adapter *)data;
945 struct snd_card_asihpi *asihpi;
946
947 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
948 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
949 if (asihpi->llmode_streampriv)
950 snd_card_asihpi_timer_function(
Kees Cook394ca812017-10-04 17:53:40 -0700951 &asihpi->llmode_streampriv->timer);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300952}
953
954static void snd_card_asihpi_isr(struct hpi_adapter *a)
955{
956 struct snd_card_asihpi *asihpi;
957
958 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
959 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
960 tasklet_schedule(&asihpi->t);
961}
962
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200963/***************************** PLAYBACK OPS ****************/
964static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
965 unsigned int cmd, void *arg)
966{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300967 char name[16];
968 snd_pcm_debug_name(substream, name, sizeof(name));
969 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200970 return snd_pcm_lib_ioctl(substream, cmd, arg);
971}
972
973static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
974 substream)
975{
976 struct snd_pcm_runtime *runtime = substream->runtime;
977 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
978
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200979 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200980
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300981 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300982 dpcm->pcm_buf_host_rw_ofs = 0;
983 dpcm->pcm_buf_dma_ofs = 0;
984 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200985 return 0;
986}
987
988static snd_pcm_uframes_t
989snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
990{
991 struct snd_pcm_runtime *runtime = substream->runtime;
992 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
993 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300994 char name[16];
995 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200996
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300997 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300998 snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200999 return ptr;
1000}
1001
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001002static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
1003 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001004{
1005 struct hpi_format hpi_format;
1006 u16 format;
1007 u16 err;
1008 u32 h_control;
1009 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001010 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001011
1012 /* on cards without SRC, must query at valid rate,
1013 * maybe set by external sync
1014 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001015 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001016 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1017 HPI_CONTROL_SAMPLECLOCK, &h_control);
1018
1019 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001020 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001021 &sample_rate);
1022
1023 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1024 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001025 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
1026 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001027 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001028 err = hpi_outstream_query_format(h_stream, &hpi_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001029 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001030 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001031 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001032 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001033}
1034
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001035static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1036{
1037 struct snd_pcm_runtime *runtime = substream->runtime;
1038 struct snd_card_asihpi_pcm *dpcm;
1039 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001040 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001041 int err;
1042
1043 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1044 if (dpcm == NULL)
1045 return -ENOMEM;
1046
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001047 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001048 substream->number, &dpcm->h_stream);
1049 hpi_handle_error(err);
1050 if (err)
1051 kfree(dpcm);
1052 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1053 return -EBUSY;
1054 if (err)
1055 return -EIO;
1056
1057 /*? also check ASI5000 samplerate source
1058 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001059 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001060 */
1061
Kees Cook394ca812017-10-04 17:53:40 -07001062 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001063 dpcm->substream = substream;
1064 runtime->private_data = dpcm;
1065 runtime->private_free = snd_card_asihpi_runtime_free;
1066
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001067 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001068 if (!card->hpi->interrupt_mode) {
1069 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1070 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
1071 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1072 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1073 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1074 } else {
1075 size_t pbmin = card->update_interval_frames *
1076 card->out_max_chans;
1077 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1078 snd_card_asihpi_playback.period_bytes_min = pbmin;
1079 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1080 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1081 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
1082 }
1083
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001084 /* snd_card_asihpi_playback.fifo_size = 0; */
1085 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1086 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1087 snd_card_asihpi_playback.formats =
1088 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001089
1090 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1091
1092 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1093 SNDRV_PCM_INFO_DOUBLE |
1094 SNDRV_PCM_INFO_BATCH |
1095 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001096 SNDRV_PCM_INFO_PAUSE |
1097 SNDRV_PCM_INFO_MMAP |
1098 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001099
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001100 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001101 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001102 snd_pcm_set_sync(substream);
1103 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001104
1105 /* struct is copied, so can create initializer dynamically */
1106 runtime->hw = snd_card_asihpi_playback;
1107
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001108 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001109 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1110 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1111 if (err < 0)
1112 return err;
1113
1114 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1115 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001116
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001117 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001118 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001119
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001120 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001121
1122 return 0;
1123}
1124
1125static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1126{
1127 struct snd_pcm_runtime *runtime = substream->runtime;
1128 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1129
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001130 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001131 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001132
1133 return 0;
1134}
1135
Julia Lawall6769e9882016-09-02 00:13:10 +02001136static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001137 .open = snd_card_asihpi_playback_open,
1138 .close = snd_card_asihpi_playback_close,
1139 .ioctl = snd_card_asihpi_playback_ioctl,
1140 .hw_params = snd_card_asihpi_pcm_hw_params,
1141 .hw_free = snd_card_asihpi_hw_free,
1142 .prepare = snd_card_asihpi_playback_prepare,
1143 .trigger = snd_card_asihpi_trigger,
1144 .pointer = snd_card_asihpi_playback_pointer,
1145};
1146
1147/***************************** CAPTURE OPS ****************/
1148static snd_pcm_uframes_t
1149snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1150{
1151 struct snd_pcm_runtime *runtime = substream->runtime;
1152 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001153 char name[16];
1154 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001156 snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001157 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001158 for the capture position, because those samples aren't yet in
1159 the local buffer available for reading.
1160 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001161 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001162}
1163
1164static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1165 unsigned int cmd, void *arg)
1166{
1167 return snd_pcm_lib_ioctl(substream, cmd, arg);
1168}
1169
1170static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1171{
1172 struct snd_pcm_runtime *runtime = substream->runtime;
1173 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1174
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001175 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001176 dpcm->pcm_buf_host_rw_ofs = 0;
1177 dpcm->pcm_buf_dma_ofs = 0;
1178 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001179
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001180 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001181 return 0;
1182}
1183
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001184static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1185 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001186{
1187 struct hpi_format hpi_format;
1188 u16 format;
1189 u16 err;
1190 u32 h_control;
1191 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001192 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001193
1194 /* on cards without SRC, must query at valid rate,
1195 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001196 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001197 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1198 HPI_CONTROL_SAMPLECLOCK, &h_control);
1199
1200 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001201 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001202 &sample_rate);
1203
1204 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1205 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1206
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001207 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1208 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001209 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001210 err = hpi_instream_query_format(h_stream, &hpi_format);
Eldad Zack167d0a12013-04-23 01:00:42 +02001211 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001212 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001213 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001214 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001215}
1216
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001217static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1218{
1219 struct snd_pcm_runtime *runtime = substream->runtime;
1220 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1221 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001222 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001223 int err;
1224
1225 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1226 if (dpcm == NULL)
1227 return -ENOMEM;
1228
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001229 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001230 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001231
1232 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001233 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001234 substream->number, &dpcm->h_stream));
1235 if (err)
1236 kfree(dpcm);
1237 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1238 return -EBUSY;
1239 if (err)
1240 return -EIO;
1241
Kees Cook394ca812017-10-04 17:53:40 -07001242 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001243 dpcm->substream = substream;
1244 runtime->private_data = dpcm;
1245 runtime->private_free = snd_card_asihpi_runtime_free;
1246
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001247 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001248 if (!card->hpi->interrupt_mode) {
1249 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1250 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1251 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1252 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1253 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1254 } else {
1255 size_t pbmin = card->update_interval_frames *
1256 card->out_max_chans;
1257 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1258 snd_card_asihpi_capture.period_bytes_min = pbmin;
1259 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1260 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1261 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
1262 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001263 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001264 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001265 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001266 snd_card_asihpi_capture.formats =
1267 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001268 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001269 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1270 SNDRV_PCM_INFO_MMAP |
1271 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001272
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001273 if (card->support_grouping)
1274 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1275
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276 runtime->hw = snd_card_asihpi_capture;
1277
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001278 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001279 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1280 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1281 if (err < 0)
1282 return err;
1283
1284 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1285 card->update_interval_frames);
1286 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001287 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001288
1289 snd_pcm_set_sync(substream);
1290
1291 return 0;
1292}
1293
1294static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1295{
1296 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1297
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001298 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001299 return 0;
1300}
1301
Julia Lawall6769e9882016-09-02 00:13:10 +02001302static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001303 .open = snd_card_asihpi_capture_open,
1304 .close = snd_card_asihpi_capture_close,
1305 .ioctl = snd_card_asihpi_capture_ioctl,
1306 .hw_params = snd_card_asihpi_pcm_hw_params,
1307 .hw_free = snd_card_asihpi_hw_free,
1308 .prepare = snd_card_asihpi_capture_prepare,
1309 .trigger = snd_card_asihpi_trigger,
1310 .pointer = snd_card_asihpi_capture_pointer,
1311};
1312
Bill Pembertone23e7a12012-12-06 12:35:10 -05001313static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001314{
1315 struct snd_pcm *pcm;
1316 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001317 u16 num_instreams, num_outstreams, x16;
1318 u32 x32;
1319
1320 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1321 &num_outstreams, &num_instreams,
1322 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001324 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001325 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001326 if (err < 0)
1327 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001328
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001329 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001330 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1331 &snd_card_asihpi_playback_mmap_ops);
1332 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1333 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001334
1335 pcm->private_data = asihpi;
1336 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001337 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001338
1339 /*? do we want to emulate MMAP for non-BBM cards?
1340 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1341 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1342 snd_dma_pci_data(asihpi->pci),
1343 64*1024, BUFFER_BYTES_MAX);
1344
1345 return 0;
1346}
1347
1348/***************************** MIXER CONTROLS ****************/
1349struct hpi_control {
1350 u32 h_control;
1351 u16 control_type;
1352 u16 src_node_type;
1353 u16 src_node_index;
1354 u16 dst_node_type;
1355 u16 dst_node_index;
1356 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001357 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001358};
1359
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001360static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001361 "invalid",
1362 "AM",
1363 "FM mono",
1364 "TV NTSC-M",
1365 "FM stereo",
1366 "AUX",
1367 "TV PAL BG",
1368 "TV PAL I",
1369 "TV PAL DK",
1370 "TV SECAM",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001371 "TV DAB",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001372};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001373/* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001374compile_time_assert(
1375 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1376 (HPI_TUNER_BAND_LAST+1)),
1377 assert_tuner_band_names_size);
1378
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001379static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001380 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001381 "PCM",
1382 "Line",
1383 "Digital",
1384 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001385 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001386 "Clock",
1387 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001388 "Mic",
1389 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001390 "Analog",
1391 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001392 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001393 "Internal",
1394 "AVB",
1395 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001396};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001397/* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001398compile_time_assert(
1399 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001400 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401 assert_src_names_size);
1402
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001403static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001404 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001405 "PCM",
1406 "Line",
1407 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001408 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001409 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001410 "Net",
1411 "Analog",
1412 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001413 "AVB",
1414 "Internal",
1415 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001416};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001417/* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001418compile_time_assert(
1419 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001420 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001421 assert_dst_names_size);
1422
1423static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1424 struct snd_card_asihpi *asihpi)
1425{
1426 int err;
1427
1428 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1429 if (err < 0)
1430 return err;
1431 else if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13001432 dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001433
1434 return 0;
1435}
1436
1437/* Convert HPI control name and location into ALSA control name */
1438static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1439 struct hpi_control *hpi_ctl,
1440 char *name)
1441{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001442 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001443 memset(snd_control, 0, sizeof(*snd_control));
1444 snd_control->name = hpi_ctl->name;
1445 snd_control->private_value = hpi_ctl->h_control;
1446 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1447 snd_control->index = 0;
1448
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001449 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1450 dir = ""; /* clock is neither capture nor playback */
1451 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001452 dir = "Capture "; /* On or towards a PCM capture destination*/
1453 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1454 (!hpi_ctl->dst_node_type))
1455 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001456 else if (hpi_ctl->src_node_type &&
1457 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001458 (hpi_ctl->dst_node_type))
1459 dir = "Monitor Playback "; /* Between an input and an output */
1460 else
1461 dir = "Playback "; /* PCM Playback source, or output node */
1462
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001463 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001464 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001465 asihpi_src_names[hpi_ctl->src_node_type],
1466 hpi_ctl->src_node_index,
1467 asihpi_dst_names[hpi_ctl->dst_node_type],
1468 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001469 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001470 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001471 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001472 asihpi_dst_names[hpi_ctl->dst_node_type],
1473 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001474 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001475 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001476 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001477 asihpi_src_names[hpi_ctl->src_node_type],
1478 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001479 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001480 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001481 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1482 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001483}
1484
1485/*------------------------------------------------------------
1486 Volume controls
1487 ------------------------------------------------------------*/
1488#define VOL_STEP_mB 1
1489static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1490 struct snd_ctl_elem_info *uinfo)
1491{
1492 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001493 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001494 u16 err;
1495 /* native gains are in millibels */
1496 short min_gain_mB;
1497 short max_gain_mB;
1498 short step_gain_mB;
1499
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001500 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001501 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1502 if (err) {
1503 max_gain_mB = 0;
1504 min_gain_mB = -10000;
1505 step_gain_mB = VOL_STEP_mB;
1506 }
1507
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001508 err = hpi_meter_query_channels(h_control, &count);
1509 if (err)
1510 count = HPI_MAX_CHANNELS;
1511
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001512 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001513 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001514 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1515 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1516 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1517 return 0;
1518}
1519
1520static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_value *ucontrol)
1522{
1523 u32 h_control = kcontrol->private_value;
1524 short an_gain_mB[HPI_MAX_CHANNELS];
1525
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001526 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001527 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1528 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1529
1530 return 0;
1531}
1532
1533static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_value *ucontrol)
1535{
1536 int change;
1537 u32 h_control = kcontrol->private_value;
1538 short an_gain_mB[HPI_MAX_CHANNELS];
1539
1540 an_gain_mB[0] =
1541 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1542 an_gain_mB[1] =
1543 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1544 /* change = asihpi->mixer_volume[addr][0] != left ||
1545 asihpi->mixer_volume[addr][1] != right;
1546 */
1547 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001548 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001549 return change;
1550}
1551
1552static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1553
Takashi Iwai000477a2011-07-22 07:57:44 +02001554#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001555
1556static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1558{
1559 u32 h_control = kcontrol->private_value;
1560 u32 mute;
1561
1562 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1563 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1564
1565 return 0;
1566}
1567
1568static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
1570{
1571 u32 h_control = kcontrol->private_value;
1572 int change = 1;
1573 /* HPI currently only supports all or none muting of multichannel volume
1574 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1575 */
1576 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1577 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1578 return change;
1579}
1580
Bill Pembertone23e7a12012-12-06 12:35:10 -05001581static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1582 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001583{
1584 struct snd_card *card = asihpi->card;
1585 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001586 int err;
1587 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001588
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001589 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001590 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1591 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1592 snd_control.info = snd_asihpi_volume_info;
1593 snd_control.get = snd_asihpi_volume_get;
1594 snd_control.put = snd_asihpi_volume_put;
1595 snd_control.tlv.p = db_scale_100;
1596
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001597 err = ctl_add(card, &snd_control, asihpi);
1598 if (err)
1599 return err;
1600
1601 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1602 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1603 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1604 snd_control.info = snd_asihpi_volume_mute_info;
1605 snd_control.get = snd_asihpi_volume_mute_get;
1606 snd_control.put = snd_asihpi_volume_mute_put;
1607 err = ctl_add(card, &snd_control, asihpi);
1608 }
1609 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001610}
1611
1612/*------------------------------------------------------------
1613 Level controls
1614 ------------------------------------------------------------*/
1615static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1616 struct snd_ctl_elem_info *uinfo)
1617{
1618 u32 h_control = kcontrol->private_value;
1619 u16 err;
1620 short min_gain_mB;
1621 short max_gain_mB;
1622 short step_gain_mB;
1623
1624 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001625 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001626 &max_gain_mB, &step_gain_mB);
1627 if (err) {
1628 max_gain_mB = 2400;
1629 min_gain_mB = -1000;
1630 step_gain_mB = 100;
1631 }
1632
1633 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1634 uinfo->count = 2;
1635 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1636 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1637 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1638 return 0;
1639}
1640
1641static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1642 struct snd_ctl_elem_value *ucontrol)
1643{
1644 u32 h_control = kcontrol->private_value;
1645 short an_gain_mB[HPI_MAX_CHANNELS];
1646
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001647 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001648 ucontrol->value.integer.value[0] =
1649 an_gain_mB[0] / HPI_UNITS_PER_dB;
1650 ucontrol->value.integer.value[1] =
1651 an_gain_mB[1] / HPI_UNITS_PER_dB;
1652
1653 return 0;
1654}
1655
1656static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1657 struct snd_ctl_elem_value *ucontrol)
1658{
1659 int change;
1660 u32 h_control = kcontrol->private_value;
1661 short an_gain_mB[HPI_MAX_CHANNELS];
1662
1663 an_gain_mB[0] =
1664 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1665 an_gain_mB[1] =
1666 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1667 /* change = asihpi->mixer_level[addr][0] != left ||
1668 asihpi->mixer_level[addr][1] != right;
1669 */
1670 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001671 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001672 return change;
1673}
1674
1675static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1676
Bill Pembertone23e7a12012-12-06 12:35:10 -05001677static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1678 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001679{
1680 struct snd_card *card = asihpi->card;
1681 struct snd_kcontrol_new snd_control;
1682
1683 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001684 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001685 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1686 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1687 snd_control.info = snd_asihpi_level_info;
1688 snd_control.get = snd_asihpi_level_get;
1689 snd_control.put = snd_asihpi_level_put;
1690 snd_control.tlv.p = db_scale_level;
1691
1692 return ctl_add(card, &snd_control, asihpi);
1693}
1694
1695/*------------------------------------------------------------
1696 AESEBU controls
1697 ------------------------------------------------------------*/
1698
1699/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001700static const char * const asihpi_aesebu_format_names[] = {
1701 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001702
1703static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1704 struct snd_ctl_elem_info *uinfo)
1705{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001706 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001707}
1708
1709static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1710 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001711 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001712{
1713 u32 h_control = kcontrol->private_value;
1714 u16 source, err;
1715
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001716 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001717
1718 /* default to N/A */
1719 ucontrol->value.enumerated.item[0] = 0;
1720 /* return success but set the control to N/A */
1721 if (err)
1722 return 0;
1723 if (source == HPI_AESEBU_FORMAT_SPDIF)
1724 ucontrol->value.enumerated.item[0] = 1;
1725 if (source == HPI_AESEBU_FORMAT_AESEBU)
1726 ucontrol->value.enumerated.item[0] = 2;
1727
1728 return 0;
1729}
1730
1731static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1732 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001733 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001734{
1735 u32 h_control = kcontrol->private_value;
1736
1737 /* default to S/PDIF */
1738 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1739
1740 if (ucontrol->value.enumerated.item[0] == 1)
1741 source = HPI_AESEBU_FORMAT_SPDIF;
1742 if (ucontrol->value.enumerated.item[0] == 2)
1743 source = HPI_AESEBU_FORMAT_AESEBU;
1744
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001745 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001746 return -EINVAL;
1747
1748 return 1;
1749}
1750
1751static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1752 struct snd_ctl_elem_value *ucontrol) {
1753 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001754 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001755}
1756
1757static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1758 struct snd_ctl_elem_value *ucontrol) {
1759 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001760 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001761}
1762
1763static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1764 struct snd_ctl_elem_info *uinfo)
1765{
1766 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1767 uinfo->count = 1;
1768
1769 uinfo->value.integer.min = 0;
1770 uinfo->value.integer.max = 0X1F;
1771 uinfo->value.integer.step = 1;
1772
1773 return 0;
1774}
1775
1776static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1777 struct snd_ctl_elem_value *ucontrol) {
1778
1779 u32 h_control = kcontrol->private_value;
1780 u16 status;
1781
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001782 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1783 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001784 ucontrol->value.integer.value[0] = status;
1785 return 0;
1786}
1787
Bill Pembertone23e7a12012-12-06 12:35:10 -05001788static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1789 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001790{
1791 struct snd_card *card = asihpi->card;
1792 struct snd_kcontrol_new snd_control;
1793
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001794 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001795 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1796 snd_control.info = snd_asihpi_aesebu_format_info;
1797 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1798 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1799
1800
1801 if (ctl_add(card, &snd_control, asihpi) < 0)
1802 return -EINVAL;
1803
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001804 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001805 snd_control.access =
1806 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1807 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1808 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1809
1810 return ctl_add(card, &snd_control, asihpi);
1811}
1812
1813static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1814 struct snd_ctl_elem_value *ucontrol) {
1815 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001816 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001817}
1818
1819static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol) {
1821 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001822 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001823}
1824
1825
Bill Pembertone23e7a12012-12-06 12:35:10 -05001826static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1827 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001828{
1829 struct snd_card *card = asihpi->card;
1830 struct snd_kcontrol_new snd_control;
1831
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001832 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001833 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1834 snd_control.info = snd_asihpi_aesebu_format_info;
1835 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1836 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1837
1838 return ctl_add(card, &snd_control, asihpi);
1839}
1840
1841/*------------------------------------------------------------
1842 Tuner controls
1843 ------------------------------------------------------------*/
1844
1845/* Gain */
1846
1847static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1848 struct snd_ctl_elem_info *uinfo)
1849{
1850 u32 h_control = kcontrol->private_value;
1851 u16 err;
1852 short idx;
1853 u16 gain_range[3];
1854
1855 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001856 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001857 idx, &gain_range[idx]);
1858 if (err != 0)
1859 return err;
1860 }
1861
1862 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1863 uinfo->count = 1;
1864 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1865 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1866 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1867 return 0;
1868}
1869
1870static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1871 struct snd_ctl_elem_value *ucontrol)
1872{
1873 /*
1874 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1875 */
1876 u32 h_control = kcontrol->private_value;
1877 short gain;
1878
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001879 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001880 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1881
1882 return 0;
1883}
1884
1885static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1886 struct snd_ctl_elem_value *ucontrol)
1887{
1888 /*
1889 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1890 */
1891 u32 h_control = kcontrol->private_value;
1892 short gain;
1893
1894 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001895 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001896
1897 return 1;
1898}
1899
1900/* Band */
1901
1902static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1903 u16 *band_list, u32 len) {
1904 u32 h_control = kcontrol->private_value;
1905 u16 err = 0;
1906 u32 i;
1907
1908 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001909 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001910 h_control, i, &band_list[i]);
1911 if (err != 0)
1912 break;
1913 }
1914
1915 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1916 return -EIO;
1917
1918 return i;
1919}
1920
1921static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1922 struct snd_ctl_elem_info *uinfo)
1923{
1924 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1925 int num_bands = 0;
1926
1927 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1928 HPI_TUNER_BAND_LAST);
1929
1930 if (num_bands < 0)
1931 return num_bands;
1932
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001933 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001934}
1935
1936static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1937 struct snd_ctl_elem_value *ucontrol)
1938{
1939 u32 h_control = kcontrol->private_value;
1940 /*
1941 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1942 */
1943 u16 band, idx;
1944 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1945 u32 num_bands = 0;
1946
1947 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1948 HPI_TUNER_BAND_LAST);
1949
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001950 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001951
1952 ucontrol->value.enumerated.item[0] = -1;
1953 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1954 if (tuner_bands[idx] == band) {
1955 ucontrol->value.enumerated.item[0] = idx;
1956 break;
1957 }
1958
1959 return 0;
1960}
1961
1962static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1963 struct snd_ctl_elem_value *ucontrol)
1964{
1965 /*
1966 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1967 */
1968 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001969 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001970 u16 band;
1971 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1972 u32 num_bands = 0;
1973
1974 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1975 HPI_TUNER_BAND_LAST);
1976
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001977 idx = ucontrol->value.enumerated.item[0];
1978 if (idx >= ARRAY_SIZE(tuner_bands))
1979 idx = ARRAY_SIZE(tuner_bands) - 1;
1980 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001981 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001982
1983 return 1;
1984}
1985
1986/* Freq */
1987
1988static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1989 struct snd_ctl_elem_info *uinfo)
1990{
1991 u32 h_control = kcontrol->private_value;
1992 u16 err;
1993 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1994 u16 num_bands = 0, band_iter, idx;
1995 u32 freq_range[3], temp_freq_range[3];
1996
1997 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1998 HPI_TUNER_BAND_LAST);
1999
2000 freq_range[0] = INT_MAX;
2001 freq_range[1] = 0;
2002 freq_range[2] = INT_MAX;
2003
2004 for (band_iter = 0; band_iter < num_bands; band_iter++) {
2005 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002006 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002007 idx, tuner_bands[band_iter],
2008 &temp_freq_range[idx]);
2009 if (err != 0)
2010 return err;
2011 }
2012
2013 /* skip band with bogus stepping */
2014 if (temp_freq_range[2] <= 0)
2015 continue;
2016
2017 if (temp_freq_range[0] < freq_range[0])
2018 freq_range[0] = temp_freq_range[0];
2019 if (temp_freq_range[1] > freq_range[1])
2020 freq_range[1] = temp_freq_range[1];
2021 if (temp_freq_range[2] < freq_range[2])
2022 freq_range[2] = temp_freq_range[2];
2023 }
2024
2025 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2026 uinfo->count = 1;
2027 uinfo->value.integer.min = ((int)freq_range[0]);
2028 uinfo->value.integer.max = ((int)freq_range[1]);
2029 uinfo->value.integer.step = ((int)freq_range[2]);
2030 return 0;
2031}
2032
2033static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
2034 struct snd_ctl_elem_value *ucontrol)
2035{
2036 u32 h_control = kcontrol->private_value;
2037 u32 freq;
2038
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002039 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002040 ucontrol->value.integer.value[0] = freq;
2041
2042 return 0;
2043}
2044
2045static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2046 struct snd_ctl_elem_value *ucontrol)
2047{
2048 u32 h_control = kcontrol->private_value;
2049 u32 freq;
2050
2051 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002052 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002053
2054 return 1;
2055}
2056
2057/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002058static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2059 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002060{
2061 struct snd_card *card = asihpi->card;
2062 struct snd_kcontrol_new snd_control;
2063
2064 snd_control.private_value = hpi_ctl->h_control;
2065 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2066
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002067 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002068 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002069 snd_control.info = snd_asihpi_tuner_gain_info;
2070 snd_control.get = snd_asihpi_tuner_gain_get;
2071 snd_control.put = snd_asihpi_tuner_gain_put;
2072
2073 if (ctl_add(card, &snd_control, asihpi) < 0)
2074 return -EINVAL;
2075 }
2076
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002077 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002078 snd_control.info = snd_asihpi_tuner_band_info;
2079 snd_control.get = snd_asihpi_tuner_band_get;
2080 snd_control.put = snd_asihpi_tuner_band_put;
2081
2082 if (ctl_add(card, &snd_control, asihpi) < 0)
2083 return -EINVAL;
2084
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002085 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002086 snd_control.info = snd_asihpi_tuner_freq_info;
2087 snd_control.get = snd_asihpi_tuner_freq_get;
2088 snd_control.put = snd_asihpi_tuner_freq_put;
2089
2090 return ctl_add(card, &snd_control, asihpi);
2091}
2092
2093/*------------------------------------------------------------
2094 Meter controls
2095 ------------------------------------------------------------*/
2096static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2097 struct snd_ctl_elem_info *uinfo)
2098{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002099 u32 h_control = kcontrol->private_value;
2100 u32 count;
2101 u16 err;
2102 err = hpi_meter_query_channels(h_control, &count);
2103 if (err)
2104 count = HPI_MAX_CHANNELS;
2105
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002106 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002107 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002108 uinfo->value.integer.min = 0;
2109 uinfo->value.integer.max = 0x7FFFFFFF;
2110 return 0;
2111}
2112
2113/* linear values for 10dB steps */
2114static int log2lin[] = {
2115 0x7FFFFFFF, /* 0dB */
2116 679093956,
2117 214748365,
2118 67909396,
2119 21474837,
2120 6790940,
2121 2147484, /* -60dB */
2122 679094,
2123 214748, /* -80 */
2124 67909,
2125 21475, /* -100 */
2126 6791,
2127 2147,
2128 679,
2129 214,
2130 68,
2131 21,
2132 7,
2133 2
2134};
2135
2136static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2137 struct snd_ctl_elem_value *ucontrol)
2138{
2139 u32 h_control = kcontrol->private_value;
2140 short an_gain_mB[HPI_MAX_CHANNELS], i;
2141 u16 err;
2142
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002143 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002144
2145 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2146 if (err) {
2147 ucontrol->value.integer.value[i] = 0;
2148 } else if (an_gain_mB[i] >= 0) {
2149 ucontrol->value.integer.value[i] =
2150 an_gain_mB[i] << 16;
2151 } else {
2152 /* -ve is log value in millibels < -60dB,
2153 * convert to (roughly!) linear,
2154 */
2155 ucontrol->value.integer.value[i] =
2156 log2lin[an_gain_mB[i] / -1000];
2157 }
2158 }
2159 return 0;
2160}
2161
Bill Pembertone23e7a12012-12-06 12:35:10 -05002162static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2163 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002164{
2165 struct snd_card *card = asihpi->card;
2166 struct snd_kcontrol_new snd_control;
2167
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002168 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002169 snd_control.access =
2170 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2171 snd_control.info = snd_asihpi_meter_info;
2172 snd_control.get = snd_asihpi_meter_get;
2173
2174 snd_control.index = subidx;
2175
2176 return ctl_add(card, &snd_control, asihpi);
2177}
2178
2179/*------------------------------------------------------------
2180 Multiplexer controls
2181 ------------------------------------------------------------*/
2182static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2183{
2184 u32 h_control = snd_control->private_value;
2185 struct hpi_control hpi_ctl;
2186 int s, err;
2187 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002188 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002189 &hpi_ctl.
2190 src_node_type,
2191 &hpi_ctl.
2192 src_node_index);
2193 if (err)
2194 break;
2195 }
2196 return s;
2197}
2198
2199static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2200 struct snd_ctl_elem_info *uinfo)
2201{
2202 int err;
2203 u16 src_node_type, src_node_index;
2204 u32 h_control = kcontrol->private_value;
2205
2206 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2207 uinfo->count = 1;
2208 uinfo->value.enumerated.items =
2209 snd_card_asihpi_mux_count_sources(kcontrol);
2210
2211 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2212 uinfo->value.enumerated.item =
2213 uinfo->value.enumerated.items - 1;
2214
2215 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002216 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002217 uinfo->value.enumerated.item,
2218 &src_node_type, &src_node_index);
2219
2220 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002221 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002222 src_node_index);
2223 return 0;
2224}
2225
2226static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2227 struct snd_ctl_elem_value *ucontrol)
2228{
2229 u32 h_control = kcontrol->private_value;
2230 u16 source_type, source_index;
2231 u16 src_node_type, src_node_index;
2232 int s;
2233
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002234 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002235 &source_type, &source_index));
2236 /* Should cache this search result! */
2237 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002238 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002239 &src_node_type, &src_node_index))
2240 break;
2241
2242 if ((source_type == src_node_type)
2243 && (source_index == src_node_index)) {
2244 ucontrol->value.enumerated.item[0] = s;
2245 return 0;
2246 }
2247 }
2248 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002249 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002250 h_control, source_type, source_index);
2251 ucontrol->value.enumerated.item[0] = 0;
2252 return 0;
2253}
2254
2255static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2256 struct snd_ctl_elem_value *ucontrol)
2257{
2258 int change;
2259 u32 h_control = kcontrol->private_value;
2260 u16 source_type, source_index;
2261 u16 e;
2262
2263 change = 1;
2264
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002265 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002266 ucontrol->value.enumerated.item[0],
2267 &source_type, &source_index);
2268 if (!e)
2269 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002270 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002271 source_type, source_index));
2272 return change;
2273}
2274
2275
Bill Pembertone23e7a12012-12-06 12:35:10 -05002276static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2277 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002278{
2279 struct snd_card *card = asihpi->card;
2280 struct snd_kcontrol_new snd_control;
2281
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002282 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002283 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2284 snd_control.info = snd_asihpi_mux_info;
2285 snd_control.get = snd_asihpi_mux_get;
2286 snd_control.put = snd_asihpi_mux_put;
2287
2288 return ctl_add(card, &snd_control, asihpi);
2289
2290}
2291
2292/*------------------------------------------------------------
2293 Channel mode controls
2294 ------------------------------------------------------------*/
2295static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2296 struct snd_ctl_elem_info *uinfo)
2297{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002298 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2299 "invalid",
2300 "Normal", "Swap",
2301 "From Left", "From Right",
2302 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002303 };
2304
2305 u32 h_control = kcontrol->private_value;
2306 u16 mode;
2307 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002308 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002309 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002310
2311 /* HPI channel mode values can be from 1 to 6
2312 Some adapters only support a contiguous subset
2313 */
2314 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002315 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002316 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002317 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002318 valid_modes++;
2319 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002320
Takashi Iwai74eeb142012-01-09 18:26:05 +01002321 if (!valid_modes)
2322 return -EINVAL;
2323
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002324 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002325}
2326
2327static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2328 struct snd_ctl_elem_value *ucontrol)
2329{
2330 u32 h_control = kcontrol->private_value;
2331 u16 mode;
2332
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002333 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002334 mode = 1;
2335
2336 ucontrol->value.enumerated.item[0] = mode - 1;
2337
2338 return 0;
2339}
2340
2341static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2342 struct snd_ctl_elem_value *ucontrol)
2343{
2344 int change;
2345 u32 h_control = kcontrol->private_value;
2346
2347 change = 1;
2348
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002349 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002350 ucontrol->value.enumerated.item[0] + 1));
2351 return change;
2352}
2353
2354
Bill Pembertone23e7a12012-12-06 12:35:10 -05002355static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2356 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002357{
2358 struct snd_card *card = asihpi->card;
2359 struct snd_kcontrol_new snd_control;
2360
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002361 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002362 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2363 snd_control.info = snd_asihpi_cmode_info;
2364 snd_control.get = snd_asihpi_cmode_get;
2365 snd_control.put = snd_asihpi_cmode_put;
2366
2367 return ctl_add(card, &snd_control, asihpi);
2368}
2369
2370/*------------------------------------------------------------
2371 Sampleclock source controls
2372 ------------------------------------------------------------*/
Krzysztof Kozlowski46d212c2015-03-24 11:48:24 +01002373static const char * const sampleclock_sources[] = {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002374 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2375 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002376 "Prev Module", "BLU-Link",
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002377 "Digital2", "Digital3", "Digital4", "Digital5",
2378 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002379
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002380 /* Number of strings must match expected enumerated values */
2381 compile_time_assert(
2382 (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
2383 assert_sampleclock_sources_size);
2384
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002385static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2386 struct snd_ctl_elem_info *uinfo)
2387{
2388 struct snd_card_asihpi *asihpi =
2389 (struct snd_card_asihpi *)(kcontrol->private_data);
2390 struct clk_cache *clkcache = &asihpi->cc;
2391 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2392 uinfo->count = 1;
2393 uinfo->value.enumerated.items = clkcache->count;
2394
2395 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2396 uinfo->value.enumerated.item =
2397 uinfo->value.enumerated.items - 1;
2398
2399 strcpy(uinfo->value.enumerated.name,
2400 clkcache->s[uinfo->value.enumerated.item].name);
2401 return 0;
2402}
2403
2404static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2405 struct snd_ctl_elem_value *ucontrol)
2406{
2407 struct snd_card_asihpi *asihpi =
2408 (struct snd_card_asihpi *)(kcontrol->private_data);
2409 struct clk_cache *clkcache = &asihpi->cc;
2410 u32 h_control = kcontrol->private_value;
2411 u16 source, srcindex = 0;
2412 int i;
2413
2414 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002415 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002416 source = 0;
2417
2418 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002419 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002420 srcindex = 0;
2421
2422 for (i = 0; i < clkcache->count; i++)
2423 if ((clkcache->s[i].source == source) &&
2424 (clkcache->s[i].index == srcindex))
2425 break;
2426
2427 ucontrol->value.enumerated.item[0] = i;
2428
2429 return 0;
2430}
2431
2432static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2433 struct snd_ctl_elem_value *ucontrol)
2434{
2435 struct snd_card_asihpi *asihpi =
2436 (struct snd_card_asihpi *)(kcontrol->private_data);
2437 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002438 unsigned int item;
2439 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002440 u32 h_control = kcontrol->private_value;
2441
2442 change = 1;
2443 item = ucontrol->value.enumerated.item[0];
2444 if (item >= clkcache->count)
2445 item = clkcache->count-1;
2446
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002447 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002448 h_control, clkcache->s[item].source));
2449
2450 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002451 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002452 h_control, clkcache->s[item].index));
2453 return change;
2454}
2455
2456/*------------------------------------------------------------
2457 Clkrate controls
2458 ------------------------------------------------------------*/
2459/* Need to change this to enumerated control with list of rates */
2460static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2461 struct snd_ctl_elem_info *uinfo)
2462{
2463 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2464 uinfo->count = 1;
2465 uinfo->value.integer.min = 8000;
2466 uinfo->value.integer.max = 192000;
2467 uinfo->value.integer.step = 100;
2468
2469 return 0;
2470}
2471
2472static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2473 struct snd_ctl_elem_value *ucontrol)
2474{
2475 u32 h_control = kcontrol->private_value;
2476 u32 rate;
2477 u16 e;
2478
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002479 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002480 if (!e)
2481 ucontrol->value.integer.value[0] = rate;
2482 else
2483 ucontrol->value.integer.value[0] = 0;
2484 return 0;
2485}
2486
2487static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2488 struct snd_ctl_elem_value *ucontrol)
2489{
2490 int change;
2491 u32 h_control = kcontrol->private_value;
2492
2493 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2494 asihpi->mixer_clkrate[addr][1] != right;
2495 */
2496 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002497 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002498 ucontrol->value.integer.value[0]));
2499 return change;
2500}
2501
2502static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2503 struct snd_ctl_elem_info *uinfo)
2504{
2505 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2506 uinfo->count = 1;
2507 uinfo->value.integer.min = 8000;
2508 uinfo->value.integer.max = 192000;
2509 uinfo->value.integer.step = 100;
2510
2511 return 0;
2512}
2513
2514static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2515 struct snd_ctl_elem_value *ucontrol)
2516{
2517 u32 h_control = kcontrol->private_value;
2518 u32 rate;
2519 u16 e;
2520
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002521 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002522 if (!e)
2523 ucontrol->value.integer.value[0] = rate;
2524 else
2525 ucontrol->value.integer.value[0] = 0;
2526 return 0;
2527}
2528
Bill Pembertone23e7a12012-12-06 12:35:10 -05002529static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2530 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002531{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002532 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002533 struct snd_kcontrol_new snd_control;
2534
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002535 struct clk_cache *clkcache;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002536 u32 hSC = hpi_ctl->h_control;
2537 int has_aes_in = 0;
2538 int i, j;
2539 u16 source;
2540
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002541 if (snd_BUG_ON(!asihpi))
2542 return -EINVAL;
2543 card = asihpi->card;
2544 clkcache = &asihpi->cc;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002545 snd_control.private_value = hpi_ctl->h_control;
2546
2547 clkcache->has_local = 0;
2548
2549 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002550 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002551 i, &source))
2552 break;
2553 clkcache->s[i].source = source;
2554 clkcache->s[i].index = 0;
2555 clkcache->s[i].name = sampleclock_sources[source];
2556 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2557 has_aes_in = 1;
2558 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2559 clkcache->has_local = 1;
2560 }
2561 if (has_aes_in)
2562 /* already will have picked up index 0 above */
2563 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002564 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002565 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2566 &source))
2567 break;
2568 clkcache->s[i].source =
2569 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2570 clkcache->s[i].index = j;
2571 clkcache->s[i].name = sampleclock_sources[
2572 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2573 i++;
2574 }
2575 clkcache->count = i;
2576
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002577 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002578 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2579 snd_control.info = snd_asihpi_clksrc_info;
2580 snd_control.get = snd_asihpi_clksrc_get;
2581 snd_control.put = snd_asihpi_clksrc_put;
2582 if (ctl_add(card, &snd_control, asihpi) < 0)
2583 return -EINVAL;
2584
2585
2586 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002587 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002588 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2589 snd_control.info = snd_asihpi_clklocal_info;
2590 snd_control.get = snd_asihpi_clklocal_get;
2591 snd_control.put = snd_asihpi_clklocal_put;
2592
2593
2594 if (ctl_add(card, &snd_control, asihpi) < 0)
2595 return -EINVAL;
2596 }
2597
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002598 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002599 snd_control.access =
2600 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2601 snd_control.info = snd_asihpi_clkrate_info;
2602 snd_control.get = snd_asihpi_clkrate_get;
2603
2604 return ctl_add(card, &snd_control, asihpi);
2605}
2606/*------------------------------------------------------------
2607 Mixer
2608 ------------------------------------------------------------*/
2609
Bill Pembertone23e7a12012-12-06 12:35:10 -05002610static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002611{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002612 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002613 unsigned int idx = 0;
2614 unsigned int subindex = 0;
2615 int err;
2616 struct hpi_control hpi_ctl, prev_ctl;
2617
2618 if (snd_BUG_ON(!asihpi))
2619 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002620 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002621 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002622
2623 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002624 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002625 &asihpi->h_mixer);
2626 hpi_handle_error(err);
2627 if (err)
2628 return -err;
2629
Takashi Iwai21896bc2010-06-02 12:08:37 +02002630 memset(&prev_ctl, 0, sizeof(prev_ctl));
2631 prev_ctl.control_type = -1;
2632
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002633 for (idx = 0; idx < 2000; idx++) {
2634 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002635 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002636 idx,
2637 &hpi_ctl.src_node_type,
2638 &hpi_ctl.src_node_index,
2639 &hpi_ctl.dst_node_type,
2640 &hpi_ctl.dst_node_index,
2641 &hpi_ctl.control_type,
2642 &hpi_ctl.h_control);
2643 if (err) {
2644 if (err == HPI_ERROR_CONTROL_DISABLED) {
2645 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002646 dev_info(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002647 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002648 idx);
2649 continue;
2650 } else
2651 break;
2652
2653 }
2654
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002655 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2656 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002657
2658 /* ASI50xx in SSX mode has multiple meters on the same node.
2659 Use subindex to create distinct ALSA controls
2660 for any duplicated controls.
2661 */
2662 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2663 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2664 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2665 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2666 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2667 subindex++;
2668 else
2669 subindex = 0;
2670
2671 prev_ctl = hpi_ctl;
2672
2673 switch (hpi_ctl.control_type) {
2674 case HPI_CONTROL_VOLUME:
2675 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2676 break;
2677 case HPI_CONTROL_LEVEL:
2678 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2679 break;
2680 case HPI_CONTROL_MULTIPLEXER:
2681 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2682 break;
2683 case HPI_CONTROL_CHANNEL_MODE:
2684 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2685 break;
2686 case HPI_CONTROL_METER:
2687 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2688 break;
2689 case HPI_CONTROL_SAMPLECLOCK:
2690 err = snd_asihpi_sampleclock_add(
2691 asihpi, &hpi_ctl);
2692 break;
2693 case HPI_CONTROL_CONNECTION: /* ignore these */
2694 continue;
2695 case HPI_CONTROL_TUNER:
2696 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2697 break;
2698 case HPI_CONTROL_AESEBU_TRANSMITTER:
2699 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2700 break;
2701 case HPI_CONTROL_AESEBU_RECEIVER:
2702 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2703 break;
2704 case HPI_CONTROL_VOX:
2705 case HPI_CONTROL_BITSTREAM:
2706 case HPI_CONTROL_MICROPHONE:
2707 case HPI_CONTROL_PARAMETRIC_EQ:
2708 case HPI_CONTROL_COMPANDER:
2709 default:
2710 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002711 dev_info(&asihpi->pci->dev,
2712 "Untranslated HPI Control (%d) %d %d %d %d %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002713 idx,
2714 hpi_ctl.control_type,
2715 hpi_ctl.src_node_type,
2716 hpi_ctl.src_node_index,
2717 hpi_ctl.dst_node_type,
2718 hpi_ctl.dst_node_index);
2719 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002720 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002721 if (err < 0)
2722 return err;
2723 }
2724 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2725 hpi_handle_error(err);
2726
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002727 dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002728
2729 return 0;
2730}
2731
2732/*------------------------------------------------------------
2733 /proc interface
2734 ------------------------------------------------------------*/
2735
2736static void
2737snd_asihpi_proc_read(struct snd_info_entry *entry,
2738 struct snd_info_buffer *buffer)
2739{
2740 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002741 u32 h_control;
2742 u32 rate = 0;
2743 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002744
2745 u16 num_outstreams;
2746 u16 num_instreams;
2747 u16 version;
2748 u32 serial_number;
2749 u16 type;
2750
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002751 int err;
2752
2753 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002754
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002755 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2756 &num_outstreams, &num_instreams,
2757 &version, &serial_number, &type));
2758
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002759 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002760 "Adapter type ASI%4X\nHardware Index %d\n"
2761 "%d outstreams\n%d instreams\n",
2762 type, asihpi->hpi->adapter->index,
2763 num_outstreams, num_instreams);
2764
2765 snd_iprintf(buffer,
2766 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2767 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002768 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2769
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002770 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002771 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2772 HPI_CONTROL_SAMPLECLOCK, &h_control);
2773
2774 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002775 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002776 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002777
2778 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002779 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002780 rate, sampleclock_sources[source]);
2781 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002782}
2783
Bill Pembertone23e7a12012-12-06 12:35:10 -05002784static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002785{
2786 struct snd_info_entry *entry;
2787
2788 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2789 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2790}
2791
2792/*------------------------------------------------------------
2793 HWDEP
2794 ------------------------------------------------------------*/
2795
2796static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2797{
2798 if (enable_hpi_hwdep)
2799 return 0;
2800 else
2801 return -ENODEV;
2802
2803}
2804
2805static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2806{
2807 if (enable_hpi_hwdep)
2808 return asihpi_hpi_release(file);
2809 else
2810 return -ENODEV;
2811}
2812
2813static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2814 unsigned int cmd, unsigned long arg)
2815{
2816 if (enable_hpi_hwdep)
2817 return asihpi_hpi_ioctl(file, cmd, arg);
2818 else
2819 return -ENODEV;
2820}
2821
2822
2823/* results in /dev/snd/hwC#D0 file for each card with index #
2824 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2825*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002826static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002827{
2828 struct snd_hwdep *hw;
2829 int err;
2830
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002831 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2832 if (err < 0)
2833 return err;
2834 strcpy(hw->name, "asihpi (HPI)");
2835 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2836 hw->ops.open = snd_asihpi_hpi_open;
2837 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2838 hw->ops.release = snd_asihpi_hpi_release;
2839 hw->private_data = asihpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002840 return 0;
2841}
2842
2843/*------------------------------------------------------------
2844 CARD
2845 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002846static int snd_asihpi_probe(struct pci_dev *pci_dev,
2847 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002848{
2849 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002850 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851 struct snd_card *card;
2852 struct snd_card_asihpi *asihpi;
2853
2854 u32 h_control;
2855 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002856 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002857
2858 static int dev;
2859 if (dev >= SNDRV_CARDS)
2860 return -ENODEV;
2861
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002862 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002863 if (!enable[dev]) {
2864 dev++;
2865 return -ENOENT;
2866 }
2867
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002868 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002869 err = asihpi_adapter_probe(pci_dev, pci_id);
2870 if (err < 0)
2871 return err;
2872
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002873 hpi = pci_get_drvdata(pci_dev);
2874 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002875 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002876 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2877 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002878 if (err < 0) {
2879 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002880 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2881 THIS_MODULE, sizeof(struct snd_card_asihpi),
2882 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002883 if (err < 0)
2884 return err;
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002885 dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002886 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887 }
2888
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002889 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002890 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002891 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002892 asihpi->hpi = hpi;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002893 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002894
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002895 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002896 HPI_ADAPTER_PROPERTY_CAPS1,
2897 NULL, &asihpi->support_grouping);
2898 if (err)
2899 asihpi->support_grouping = 0;
2900
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002901 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002902 HPI_ADAPTER_PROPERTY_CAPS2,
2903 &asihpi->support_mrx, NULL);
2904 if (err)
2905 asihpi->support_mrx = 0;
2906
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002907 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002908 HPI_ADAPTER_PROPERTY_INTERVAL,
2909 NULL, &asihpi->update_interval_frames);
2910 if (err)
2911 asihpi->update_interval_frames = 512;
2912
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002913 if (hpi->interrupt_mode) {
2914 asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
2915 asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
2916 tasklet_init(&asihpi->t, snd_card_asihpi_int_task,
2917 (unsigned long)hpi);
2918 hpi->interrupt_callback = snd_card_asihpi_isr;
2919 } else {
2920 asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
2921 asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
2922 }
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002923
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002924 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002925 0, &h_stream));
2926
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002927 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002928 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002929
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002930 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002931
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002932 if (!asihpi->can_dma)
2933 asihpi->update_interval_frames *= 2;
2934
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002935 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002936 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2937 &asihpi->in_max_chans, &asihpi->out_max_chans);
2938 if (err) {
2939 asihpi->in_max_chans = 2;
2940 asihpi->out_max_chans = 2;
2941 }
2942
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002943 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2944 asihpi->out_min_chans = asihpi->out_max_chans;
2945 asihpi->in_min_chans = asihpi->in_max_chans;
2946 asihpi->support_grouping = 0;
2947 } else {
2948 asihpi->out_min_chans = 1;
2949 asihpi->in_min_chans = 1;
2950 }
2951
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002952 dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002953 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002954 asihpi->support_grouping,
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002955 asihpi->support_mrx,
2956 asihpi->update_interval_frames
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002957 );
2958
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002959 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002960 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002961 dev_err(&pci_dev->dev, "pcm_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002962 goto __nodev;
2963 }
2964 err = snd_card_asihpi_mixer_new(asihpi);
2965 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002966 dev_err(&pci_dev->dev, "mixer_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002967 goto __nodev;
2968 }
2969
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002970 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002971 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2972 HPI_CONTROL_SAMPLECLOCK, &h_control);
2973
2974 if (!err)
2975 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002976 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002977
2978 snd_asihpi_proc_init(asihpi);
2979
2980 /* always create, can be enabled or disabled dynamically
2981 by enable_hwdep module param*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002982 snd_asihpi_hpi_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002983
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002984 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002985
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002986 sprintf(card->shortname, "AudioScience ASI%4X",
2987 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002988 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002989 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002990 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002991
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002992 if (!err) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002993 dev++;
2994 return 0;
2995 }
2996__nodev:
2997 snd_card_free(card);
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002998 dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002999 return err;
3000
3001}
3002
Bill Pembertone23e7a12012-12-06 12:35:10 -05003003static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003004{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13003005 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003006 struct snd_card_asihpi *asihpi = hpi->snd_card->private_data;
3007
3008 /* Stop interrupts */
3009 if (hpi->interrupt_mode) {
3010 hpi->interrupt_callback = NULL;
3011 hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
3012 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
3013 tasklet_kill(&asihpi->t);
3014 }
3015
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13003016 snd_card_free(hpi->snd_card);
3017 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003018 asihpi_adapter_remove(pci_dev);
3019}
3020
Benoit Taine9baa3c32014-08-08 15:56:03 +02003021static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003022 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
3023 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3024 (kernel_ulong_t)HPI_6205},
3025 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
3026 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3027 (kernel_ulong_t)HPI_6000},
3028 {0,}
3029};
3030MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
3031
3032static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003033 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003034 .id_table = asihpi_pci_tbl,
3035 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003036 .remove = snd_asihpi_remove,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003037};
3038
3039static int __init snd_asihpi_init(void)
3040{
3041 asihpi_init();
3042 return pci_register_driver(&driver);
3043}
3044
3045static void __exit snd_asihpi_exit(void)
3046{
3047
3048 pci_unregister_driver(&driver);
3049 asihpi_exit();
3050}
3051
3052module_init(snd_asihpi_init)
3053module_exit(snd_asihpi_exit)
3054