blob: 720361455c60166061aa7e1921d0de147d1c42ce [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{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300576 struct snd_card_asihpi *card;
577
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300578 card = snd_pcm_substream_chip(substream);
579
580 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
581 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
582
583 if (in_interrupt())
584 card->llmode_streampriv = NULL;
585 else {
586 tasklet_disable(&card->t);
587 card->llmode_streampriv = NULL;
588 tasklet_enable(&card->t);
589 }
590}
591
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
593 int cmd)
594{
595 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
596 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
597 struct snd_pcm_substream *s;
598 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200599 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200600
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200601 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200602
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200603 switch (cmd) {
604 case SNDRV_PCM_TRIGGER_START:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300605 snd_printdd("%s trigger start\n", name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200606 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300607 struct snd_pcm_runtime *runtime = s->runtime;
608 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200609
610 if (snd_pcm_substream_chip(s) != card)
611 continue;
612
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300613 /* don't link Cap and Play */
614 if (substream->stream != s->stream)
615 continue;
616
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200617 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200618 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200619 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300620 * in buffer? Must be at least one period!
621 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200622 * buffer is bigger it may contain even more
623 * data??
624 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300625 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300626 snd_printddd("%d preload %d\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200627 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300628 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300629 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200630 preload,
631 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300632 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200633 }
634
635 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200636 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300637 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200638 dpcm->h_stream,
639 ds->h_stream);
640 if (!e) {
641 snd_pcm_trigger_done(s, substream);
642 } else {
643 hpi_handle_error(e);
644 break;
645 }
646 } else
647 break;
648 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200649 /* start the master stream */
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300650 card->pcm_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300651 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200652 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300653 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200654 break;
655
656 case SNDRV_PCM_TRIGGER_STOP:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300657 snd_printdd("%s trigger stop\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300658 card->pcm_stop(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200659 snd_pcm_group_for_each_entry(s, substream) {
660 if (snd_pcm_substream_chip(s) != card)
661 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300662 /* don't link Cap and Play */
663 if (substream->stream != s->stream)
664 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665
666 /*? workaround linked streams don't
667 transition to SETUP 20070706*/
668 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
669
670 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200671 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200672 snd_pcm_trigger_done(s, substream);
673 } else
674 break;
675 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200676
677 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300678 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200679 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
680 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300681 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200682
683 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300684 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685 break;
686
687 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300688 snd_printdd("%s trigger pause release\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300689 card->pcm_start(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300690 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200691 break;
692 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300693 snd_printdd("%s trigger pause push\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300694 card->pcm_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300695 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200696 break;
697 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300698 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200699 return -EINVAL;
700 }
701
702 return 0;
703}
704
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200705/*algorithm outline
706 Without linking degenerates to getting single stream pos etc
707 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
708*/
709/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300710pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200711for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300712 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300713 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300714 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200715}
716timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
717for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300718 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300719 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200720 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300721 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200722 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300723 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200724 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300725 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200726 }
727 }
728 snd_pcm_period_elapsed(s);
729 }
730}
731*/
732
733/** Minimum of 2 modulo values. Works correctly when the difference between
734* the values is less than half the modulus
735*/
736static inline unsigned int modulo_min(unsigned int a, unsigned int b,
737 unsigned long int modulus)
738{
739 unsigned int result;
740 if (((a-b) % modulus) < (modulus/2))
741 result = b;
742 else
743 result = a;
744
745 return result;
746}
747
748/** Timer function, equivalent to interrupt service routine for cards
749*/
Kees Cook394ca812017-10-04 17:53:40 -0700750static void snd_card_asihpi_timer_function(struct timer_list *t)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751{
Kees Cook394ca812017-10-04 17:53:40 -0700752 struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300753 struct snd_pcm_substream *substream = dpcm->substream;
754 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755 struct snd_pcm_runtime *runtime;
756 struct snd_pcm_substream *s;
757 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300758 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200759 unsigned int remdata, xfercount, next_jiffies;
760 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300761 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200762 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300763 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200764 char name[16];
765
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200766
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300767 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300768
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200769 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300770 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200771 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
772 runtime = s->runtime;
773
774 if (snd_pcm_substream_chip(s) != card)
775 continue;
776
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300777 /* don't link Cap and Play */
778 if (substream->stream != s->stream)
779 continue;
780
781 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200782 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300783 &buffer_size, &bytes_avail,
784 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200785
786 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300787 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200788
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200789 if (!card->can_dma)
790 on_card_bytes = bytes_avail;
791
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300792 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300793 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300794 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300795 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300796 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300797 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200798 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300799 }
800 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300801 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300802 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200803 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300804 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100805 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200806 continue;
807 }
808 } else {
809 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300810 }
811 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300812 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200813
814 if (first) {
815 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300816 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300817 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200818 first = 0;
819 } else {
820 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300821 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200822 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300823 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824 newdata);
825 }
826
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300827 snd_printddd(
828 "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 +1300829 name, s->number, state,
830 ds->pcm_buf_elapsed_dma_ofs,
831 ds->pcm_buf_host_rw_ofs,
832 pcm_buf_dma_ofs,
833 (int)bytes_avail,
834
835 (int)on_card_bytes,
836 buffer_size-bytes_avail,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200837 (unsigned long)frames_to_bytes(runtime,
838 runtime->status->hw_ptr),
839 (unsigned long)frames_to_bytes(runtime,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300840 runtime->control->appl_ptr)
841 );
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300842 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200843 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300844 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200845
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300846 remdata = newdata % dpcm->period_bytes;
847 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300848 /* come back when on_card_bytes has decreased enough to allow
849 write to happen, or when data has been consumed to make another
850 period
851 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300852 if (xfercount && (on_card_bytes > dpcm->period_bytes))
853 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300854 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300855 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300856
857 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200858 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300859 snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300860 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
861
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300862 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200863 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200864
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300865 /* don't link Cap and Play */
866 if (substream->stream != s->stream)
867 continue;
868
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300869 /* Store dma offset for use by pointer callback */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300870 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
871
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200872 if (xfercount &&
873 /* Limit use of on card fifo for playback */
874 ((on_card_bytes <= ds->period_bytes) ||
875 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
876
877 {
878
879 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
880 unsigned int xfer1, xfer2;
881 char *pd = &s->runtime->dma_area[buf_ofs];
882
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200883 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200884 xfer1 = xfercount;
885 xfer2 = 0;
886 } else {
887 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
888 xfer2 = xfercount - xfer1;
889 }
890
891 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300892 snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200893 s->number, xfer1, buf_ofs);
894 hpi_handle_error(
895 hpi_outstream_write_buf(
896 ds->h_stream, pd, xfer1,
897 &ds->format));
898
899 if (xfer2) {
900 pd = s->runtime->dma_area;
901
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300902 snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200903 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200904 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200905 hpi_handle_error(
906 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200907 ds->h_stream, pd,
908 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200909 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200910 }
911 } else {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300912 snd_printddd("read1, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200913 s->number, xfer1);
914 hpi_handle_error(
915 hpi_instream_read_buf(
916 ds->h_stream,
917 pd, xfer1));
918 if (xfer2) {
919 pd = s->runtime->dma_area;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300920 snd_printddd("read2, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200921 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200922 hpi_handle_error(
923 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300924 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200925 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200926 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200927 }
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300928 /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300929 ds->pcm_buf_host_rw_ofs += xfercount;
930 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200931 snd_pcm_period_elapsed(s);
932 }
933 }
934
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300935 if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200936 add_timer(&dpcm->timer);
937}
938
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300939static void snd_card_asihpi_int_task(unsigned long data)
940{
941 struct hpi_adapter *a = (struct hpi_adapter *)data;
942 struct snd_card_asihpi *asihpi;
943
944 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
945 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
946 if (asihpi->llmode_streampriv)
947 snd_card_asihpi_timer_function(
Kees Cook394ca812017-10-04 17:53:40 -0700948 &asihpi->llmode_streampriv->timer);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300949}
950
951static void snd_card_asihpi_isr(struct hpi_adapter *a)
952{
953 struct snd_card_asihpi *asihpi;
954
955 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
956 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
957 tasklet_schedule(&asihpi->t);
958}
959
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200960/***************************** PLAYBACK OPS ****************/
961static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
962 unsigned int cmd, void *arg)
963{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300964 char name[16];
965 snd_pcm_debug_name(substream, name, sizeof(name));
966 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200967 return snd_pcm_lib_ioctl(substream, cmd, arg);
968}
969
970static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
971 substream)
972{
973 struct snd_pcm_runtime *runtime = substream->runtime;
974 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
975
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200976 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200977
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300978 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300979 dpcm->pcm_buf_host_rw_ofs = 0;
980 dpcm->pcm_buf_dma_ofs = 0;
981 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200982 return 0;
983}
984
985static snd_pcm_uframes_t
986snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
987{
988 struct snd_pcm_runtime *runtime = substream->runtime;
989 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
990 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300991 char name[16];
992 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200993
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300994 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300995 snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200996 return ptr;
997}
998
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300999static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
1000 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001001{
1002 struct hpi_format hpi_format;
1003 u16 format;
1004 u16 err;
1005 u32 h_control;
1006 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001007 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001008
1009 /* on cards without SRC, must query at valid rate,
1010 * maybe set by external sync
1011 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001012 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001013 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1014 HPI_CONTROL_SAMPLECLOCK, &h_control);
1015
1016 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001017 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001018 &sample_rate);
1019
1020 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1021 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001022 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
1023 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001024 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001025 err = hpi_outstream_query_format(h_stream, &hpi_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001026 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001027 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001028 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001029 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001030}
1031
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001032static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1033{
1034 struct snd_pcm_runtime *runtime = substream->runtime;
1035 struct snd_card_asihpi_pcm *dpcm;
1036 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001037 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001038 int err;
1039
1040 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1041 if (dpcm == NULL)
1042 return -ENOMEM;
1043
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001044 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001045 substream->number, &dpcm->h_stream);
1046 hpi_handle_error(err);
1047 if (err)
1048 kfree(dpcm);
1049 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1050 return -EBUSY;
1051 if (err)
1052 return -EIO;
1053
1054 /*? also check ASI5000 samplerate source
1055 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001056 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001057 */
1058
Kees Cook394ca812017-10-04 17:53:40 -07001059 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001060 dpcm->substream = substream;
1061 runtime->private_data = dpcm;
1062 runtime->private_free = snd_card_asihpi_runtime_free;
1063
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001064 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001065 if (!card->hpi->interrupt_mode) {
1066 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1067 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
1068 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1069 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1070 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1071 } else {
1072 size_t pbmin = card->update_interval_frames *
1073 card->out_max_chans;
1074 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1075 snd_card_asihpi_playback.period_bytes_min = pbmin;
1076 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1077 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1078 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
1079 }
1080
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001081 /* snd_card_asihpi_playback.fifo_size = 0; */
1082 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1083 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1084 snd_card_asihpi_playback.formats =
1085 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001086
1087 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1088
1089 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1090 SNDRV_PCM_INFO_DOUBLE |
1091 SNDRV_PCM_INFO_BATCH |
1092 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001093 SNDRV_PCM_INFO_PAUSE |
1094 SNDRV_PCM_INFO_MMAP |
1095 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001096
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001097 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001098 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001099 snd_pcm_set_sync(substream);
1100 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001101
1102 /* struct is copied, so can create initializer dynamically */
1103 runtime->hw = snd_card_asihpi_playback;
1104
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001105 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001106 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1107 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1108 if (err < 0)
1109 return err;
1110
1111 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1112 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001113
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001114 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001115 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001116
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001117 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001118
1119 return 0;
1120}
1121
1122static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1123{
1124 struct snd_pcm_runtime *runtime = substream->runtime;
1125 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1126
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001127 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001128 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001129
1130 return 0;
1131}
1132
Julia Lawall6769e9882016-09-02 00:13:10 +02001133static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001134 .open = snd_card_asihpi_playback_open,
1135 .close = snd_card_asihpi_playback_close,
1136 .ioctl = snd_card_asihpi_playback_ioctl,
1137 .hw_params = snd_card_asihpi_pcm_hw_params,
1138 .hw_free = snd_card_asihpi_hw_free,
1139 .prepare = snd_card_asihpi_playback_prepare,
1140 .trigger = snd_card_asihpi_trigger,
1141 .pointer = snd_card_asihpi_playback_pointer,
1142};
1143
1144/***************************** CAPTURE OPS ****************/
1145static snd_pcm_uframes_t
1146snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1147{
1148 struct snd_pcm_runtime *runtime = substream->runtime;
1149 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001150 char name[16];
1151 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001152
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001153 snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001154 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001155 for the capture position, because those samples aren't yet in
1156 the local buffer available for reading.
1157 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001158 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001159}
1160
1161static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1162 unsigned int cmd, void *arg)
1163{
1164 return snd_pcm_lib_ioctl(substream, cmd, arg);
1165}
1166
1167static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1168{
1169 struct snd_pcm_runtime *runtime = substream->runtime;
1170 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1171
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001172 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001173 dpcm->pcm_buf_host_rw_ofs = 0;
1174 dpcm->pcm_buf_dma_ofs = 0;
1175 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001176
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001177 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001178 return 0;
1179}
1180
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001181static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1182 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001183{
1184 struct hpi_format hpi_format;
1185 u16 format;
1186 u16 err;
1187 u32 h_control;
1188 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001189 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001190
1191 /* on cards without SRC, must query at valid rate,
1192 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001193 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001194 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1195 HPI_CONTROL_SAMPLECLOCK, &h_control);
1196
1197 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001198 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001199 &sample_rate);
1200
1201 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1202 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1203
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001204 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1205 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001206 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001207 err = hpi_instream_query_format(h_stream, &hpi_format);
Eldad Zack167d0a12013-04-23 01:00:42 +02001208 if (!err && (hpi_to_alsa_formats[format] != -1))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001209 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001210 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001211 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001212}
1213
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001214static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1215{
1216 struct snd_pcm_runtime *runtime = substream->runtime;
1217 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1218 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001219 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001220 int err;
1221
1222 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1223 if (dpcm == NULL)
1224 return -ENOMEM;
1225
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001226 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001227 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001228
1229 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001230 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001231 substream->number, &dpcm->h_stream));
1232 if (err)
1233 kfree(dpcm);
1234 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1235 return -EBUSY;
1236 if (err)
1237 return -EIO;
1238
Kees Cook394ca812017-10-04 17:53:40 -07001239 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001240 dpcm->substream = substream;
1241 runtime->private_data = dpcm;
1242 runtime->private_free = snd_card_asihpi_runtime_free;
1243
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001244 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001245 if (!card->hpi->interrupt_mode) {
1246 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1247 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1248 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1249 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1250 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1251 } else {
1252 size_t pbmin = card->update_interval_frames *
1253 card->out_max_chans;
1254 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1255 snd_card_asihpi_capture.period_bytes_min = pbmin;
1256 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1257 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1258 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
1259 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001260 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001261 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001262 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001263 snd_card_asihpi_capture.formats =
1264 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001265 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001266 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1267 SNDRV_PCM_INFO_MMAP |
1268 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001269
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001270 if (card->support_grouping)
1271 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1272
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001273 runtime->hw = snd_card_asihpi_capture;
1274
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001275 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001276 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1277 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1278 if (err < 0)
1279 return err;
1280
1281 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1282 card->update_interval_frames);
1283 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001284 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001285
1286 snd_pcm_set_sync(substream);
1287
1288 return 0;
1289}
1290
1291static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1292{
1293 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1294
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001295 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001296 return 0;
1297}
1298
Julia Lawall6769e9882016-09-02 00:13:10 +02001299static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001300 .open = snd_card_asihpi_capture_open,
1301 .close = snd_card_asihpi_capture_close,
1302 .ioctl = snd_card_asihpi_capture_ioctl,
1303 .hw_params = snd_card_asihpi_pcm_hw_params,
1304 .hw_free = snd_card_asihpi_hw_free,
1305 .prepare = snd_card_asihpi_capture_prepare,
1306 .trigger = snd_card_asihpi_trigger,
1307 .pointer = snd_card_asihpi_capture_pointer,
1308};
1309
Bill Pembertone23e7a12012-12-06 12:35:10 -05001310static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001311{
1312 struct snd_pcm *pcm;
1313 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001314 u16 num_instreams, num_outstreams, x16;
1315 u32 x32;
1316
1317 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1318 &num_outstreams, &num_instreams,
1319 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001320
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001321 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001322 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001323 if (err < 0)
1324 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001325
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001326 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001327 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1328 &snd_card_asihpi_playback_mmap_ops);
1329 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1330 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001331
1332 pcm->private_data = asihpi;
1333 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001334 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001335
1336 /*? do we want to emulate MMAP for non-BBM cards?
1337 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1338 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1339 snd_dma_pci_data(asihpi->pci),
1340 64*1024, BUFFER_BYTES_MAX);
1341
1342 return 0;
1343}
1344
1345/***************************** MIXER CONTROLS ****************/
1346struct hpi_control {
1347 u32 h_control;
1348 u16 control_type;
1349 u16 src_node_type;
1350 u16 src_node_index;
1351 u16 dst_node_type;
1352 u16 dst_node_index;
1353 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001354 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001355};
1356
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001357static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001358 "invalid",
1359 "AM",
1360 "FM mono",
1361 "TV NTSC-M",
1362 "FM stereo",
1363 "AUX",
1364 "TV PAL BG",
1365 "TV PAL I",
1366 "TV PAL DK",
1367 "TV SECAM",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001368 "TV DAB",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001369};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001370/* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001371compile_time_assert(
1372 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1373 (HPI_TUNER_BAND_LAST+1)),
1374 assert_tuner_band_names_size);
1375
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001376static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001377 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001378 "PCM",
1379 "Line",
1380 "Digital",
1381 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001382 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001383 "Clock",
1384 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001385 "Mic",
1386 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001387 "Analog",
1388 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001389 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001390 "Internal",
1391 "AVB",
1392 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001393};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001394/* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395compile_time_assert(
1396 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001397 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001398 assert_src_names_size);
1399
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001400static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001401 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001402 "PCM",
1403 "Line",
1404 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001405 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001406 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001407 "Net",
1408 "Analog",
1409 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001410 "AVB",
1411 "Internal",
1412 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001413};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001414/* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001415compile_time_assert(
1416 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001417 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001418 assert_dst_names_size);
1419
1420static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1421 struct snd_card_asihpi *asihpi)
1422{
1423 int err;
1424
1425 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1426 if (err < 0)
1427 return err;
1428 else if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13001429 dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001430
1431 return 0;
1432}
1433
1434/* Convert HPI control name and location into ALSA control name */
1435static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1436 struct hpi_control *hpi_ctl,
1437 char *name)
1438{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001439 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001440 memset(snd_control, 0, sizeof(*snd_control));
1441 snd_control->name = hpi_ctl->name;
1442 snd_control->private_value = hpi_ctl->h_control;
1443 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1444 snd_control->index = 0;
1445
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001446 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1447 dir = ""; /* clock is neither capture nor playback */
1448 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001449 dir = "Capture "; /* On or towards a PCM capture destination*/
1450 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1451 (!hpi_ctl->dst_node_type))
1452 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001453 else if (hpi_ctl->src_node_type &&
1454 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001455 (hpi_ctl->dst_node_type))
1456 dir = "Monitor Playback "; /* Between an input and an output */
1457 else
1458 dir = "Playback "; /* PCM Playback source, or output node */
1459
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001460 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001461 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001462 asihpi_src_names[hpi_ctl->src_node_type],
1463 hpi_ctl->src_node_index,
1464 asihpi_dst_names[hpi_ctl->dst_node_type],
1465 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001466 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001467 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001468 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001469 asihpi_dst_names[hpi_ctl->dst_node_type],
1470 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001471 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001472 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001473 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001474 asihpi_src_names[hpi_ctl->src_node_type],
1475 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001476 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001477 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001478 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1479 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001480}
1481
1482/*------------------------------------------------------------
1483 Volume controls
1484 ------------------------------------------------------------*/
1485#define VOL_STEP_mB 1
1486static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1487 struct snd_ctl_elem_info *uinfo)
1488{
1489 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001490 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001491 u16 err;
1492 /* native gains are in millibels */
1493 short min_gain_mB;
1494 short max_gain_mB;
1495 short step_gain_mB;
1496
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001497 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001498 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1499 if (err) {
1500 max_gain_mB = 0;
1501 min_gain_mB = -10000;
1502 step_gain_mB = VOL_STEP_mB;
1503 }
1504
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001505 err = hpi_meter_query_channels(h_control, &count);
1506 if (err)
1507 count = HPI_MAX_CHANNELS;
1508
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001509 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001510 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001511 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1512 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1513 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1514 return 0;
1515}
1516
1517static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_value *ucontrol)
1519{
1520 u32 h_control = kcontrol->private_value;
1521 short an_gain_mB[HPI_MAX_CHANNELS];
1522
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001523 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001524 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1525 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1526
1527 return 0;
1528}
1529
1530static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1531 struct snd_ctl_elem_value *ucontrol)
1532{
1533 int change;
1534 u32 h_control = kcontrol->private_value;
1535 short an_gain_mB[HPI_MAX_CHANNELS];
1536
1537 an_gain_mB[0] =
1538 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1539 an_gain_mB[1] =
1540 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1541 /* change = asihpi->mixer_volume[addr][0] != left ||
1542 asihpi->mixer_volume[addr][1] != right;
1543 */
1544 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001545 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001546 return change;
1547}
1548
1549static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1550
Takashi Iwai000477a2011-07-22 07:57:44 +02001551#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001552
1553static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_value *ucontrol)
1555{
1556 u32 h_control = kcontrol->private_value;
1557 u32 mute;
1558
1559 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1560 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1561
1562 return 0;
1563}
1564
1565static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 u32 h_control = kcontrol->private_value;
1569 int change = 1;
1570 /* HPI currently only supports all or none muting of multichannel volume
1571 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1572 */
1573 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1574 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
1575 return change;
1576}
1577
Bill Pembertone23e7a12012-12-06 12:35:10 -05001578static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1579 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001580{
1581 struct snd_card *card = asihpi->card;
1582 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001583 int err;
1584 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001585
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001586 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001587 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1588 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1589 snd_control.info = snd_asihpi_volume_info;
1590 snd_control.get = snd_asihpi_volume_get;
1591 snd_control.put = snd_asihpi_volume_put;
1592 snd_control.tlv.p = db_scale_100;
1593
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001594 err = ctl_add(card, &snd_control, asihpi);
1595 if (err)
1596 return err;
1597
1598 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1599 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1600 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1601 snd_control.info = snd_asihpi_volume_mute_info;
1602 snd_control.get = snd_asihpi_volume_mute_get;
1603 snd_control.put = snd_asihpi_volume_mute_put;
1604 err = ctl_add(card, &snd_control, asihpi);
1605 }
1606 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001607}
1608
1609/*------------------------------------------------------------
1610 Level controls
1611 ------------------------------------------------------------*/
1612static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1613 struct snd_ctl_elem_info *uinfo)
1614{
1615 u32 h_control = kcontrol->private_value;
1616 u16 err;
1617 short min_gain_mB;
1618 short max_gain_mB;
1619 short step_gain_mB;
1620
1621 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001622 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001623 &max_gain_mB, &step_gain_mB);
1624 if (err) {
1625 max_gain_mB = 2400;
1626 min_gain_mB = -1000;
1627 step_gain_mB = 100;
1628 }
1629
1630 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1631 uinfo->count = 2;
1632 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1633 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1634 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1635 return 0;
1636}
1637
1638static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1639 struct snd_ctl_elem_value *ucontrol)
1640{
1641 u32 h_control = kcontrol->private_value;
1642 short an_gain_mB[HPI_MAX_CHANNELS];
1643
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001644 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001645 ucontrol->value.integer.value[0] =
1646 an_gain_mB[0] / HPI_UNITS_PER_dB;
1647 ucontrol->value.integer.value[1] =
1648 an_gain_mB[1] / HPI_UNITS_PER_dB;
1649
1650 return 0;
1651}
1652
1653static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1654 struct snd_ctl_elem_value *ucontrol)
1655{
1656 int change;
1657 u32 h_control = kcontrol->private_value;
1658 short an_gain_mB[HPI_MAX_CHANNELS];
1659
1660 an_gain_mB[0] =
1661 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1662 an_gain_mB[1] =
1663 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1664 /* change = asihpi->mixer_level[addr][0] != left ||
1665 asihpi->mixer_level[addr][1] != right;
1666 */
1667 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001668 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001669 return change;
1670}
1671
1672static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1673
Bill Pembertone23e7a12012-12-06 12:35:10 -05001674static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1675 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001676{
1677 struct snd_card *card = asihpi->card;
1678 struct snd_kcontrol_new snd_control;
1679
1680 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001681 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001682 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1683 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1684 snd_control.info = snd_asihpi_level_info;
1685 snd_control.get = snd_asihpi_level_get;
1686 snd_control.put = snd_asihpi_level_put;
1687 snd_control.tlv.p = db_scale_level;
1688
1689 return ctl_add(card, &snd_control, asihpi);
1690}
1691
1692/*------------------------------------------------------------
1693 AESEBU controls
1694 ------------------------------------------------------------*/
1695
1696/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001697static const char * const asihpi_aesebu_format_names[] = {
1698 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001699
1700static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1701 struct snd_ctl_elem_info *uinfo)
1702{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001703 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001704}
1705
1706static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1707 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001708 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001709{
1710 u32 h_control = kcontrol->private_value;
1711 u16 source, err;
1712
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001713 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001714
1715 /* default to N/A */
1716 ucontrol->value.enumerated.item[0] = 0;
1717 /* return success but set the control to N/A */
1718 if (err)
1719 return 0;
1720 if (source == HPI_AESEBU_FORMAT_SPDIF)
1721 ucontrol->value.enumerated.item[0] = 1;
1722 if (source == HPI_AESEBU_FORMAT_AESEBU)
1723 ucontrol->value.enumerated.item[0] = 2;
1724
1725 return 0;
1726}
1727
1728static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1729 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001730 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001731{
1732 u32 h_control = kcontrol->private_value;
1733
1734 /* default to S/PDIF */
1735 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1736
1737 if (ucontrol->value.enumerated.item[0] == 1)
1738 source = HPI_AESEBU_FORMAT_SPDIF;
1739 if (ucontrol->value.enumerated.item[0] == 2)
1740 source = HPI_AESEBU_FORMAT_AESEBU;
1741
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001742 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001743 return -EINVAL;
1744
1745 return 1;
1746}
1747
1748static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1749 struct snd_ctl_elem_value *ucontrol) {
1750 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001751 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001752}
1753
1754static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1755 struct snd_ctl_elem_value *ucontrol) {
1756 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001757 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001758}
1759
1760static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1761 struct snd_ctl_elem_info *uinfo)
1762{
1763 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1764 uinfo->count = 1;
1765
1766 uinfo->value.integer.min = 0;
1767 uinfo->value.integer.max = 0X1F;
1768 uinfo->value.integer.step = 1;
1769
1770 return 0;
1771}
1772
1773static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1774 struct snd_ctl_elem_value *ucontrol) {
1775
1776 u32 h_control = kcontrol->private_value;
1777 u16 status;
1778
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001779 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1780 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001781 ucontrol->value.integer.value[0] = status;
1782 return 0;
1783}
1784
Bill Pembertone23e7a12012-12-06 12:35:10 -05001785static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1786 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001787{
1788 struct snd_card *card = asihpi->card;
1789 struct snd_kcontrol_new snd_control;
1790
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001791 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001792 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1793 snd_control.info = snd_asihpi_aesebu_format_info;
1794 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1795 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1796
1797
1798 if (ctl_add(card, &snd_control, asihpi) < 0)
1799 return -EINVAL;
1800
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001801 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001802 snd_control.access =
1803 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1804 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1805 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1806
1807 return ctl_add(card, &snd_control, asihpi);
1808}
1809
1810static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1811 struct snd_ctl_elem_value *ucontrol) {
1812 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001813 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001814}
1815
1816static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol) {
1818 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001819 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001820}
1821
1822
Bill Pembertone23e7a12012-12-06 12:35:10 -05001823static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1824 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001825{
1826 struct snd_card *card = asihpi->card;
1827 struct snd_kcontrol_new snd_control;
1828
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001829 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001830 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1831 snd_control.info = snd_asihpi_aesebu_format_info;
1832 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1833 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1834
1835 return ctl_add(card, &snd_control, asihpi);
1836}
1837
1838/*------------------------------------------------------------
1839 Tuner controls
1840 ------------------------------------------------------------*/
1841
1842/* Gain */
1843
1844static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1845 struct snd_ctl_elem_info *uinfo)
1846{
1847 u32 h_control = kcontrol->private_value;
1848 u16 err;
1849 short idx;
1850 u16 gain_range[3];
1851
1852 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001853 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001854 idx, &gain_range[idx]);
1855 if (err != 0)
1856 return err;
1857 }
1858
1859 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1860 uinfo->count = 1;
1861 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1862 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1863 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1864 return 0;
1865}
1866
1867static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1868 struct snd_ctl_elem_value *ucontrol)
1869{
1870 /*
1871 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1872 */
1873 u32 h_control = kcontrol->private_value;
1874 short gain;
1875
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001876 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001877 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1878
1879 return 0;
1880}
1881
1882static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1883 struct snd_ctl_elem_value *ucontrol)
1884{
1885 /*
1886 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1887 */
1888 u32 h_control = kcontrol->private_value;
1889 short gain;
1890
1891 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001892 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001893
1894 return 1;
1895}
1896
1897/* Band */
1898
1899static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1900 u16 *band_list, u32 len) {
1901 u32 h_control = kcontrol->private_value;
1902 u16 err = 0;
1903 u32 i;
1904
1905 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001906 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001907 h_control, i, &band_list[i]);
1908 if (err != 0)
1909 break;
1910 }
1911
1912 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1913 return -EIO;
1914
1915 return i;
1916}
1917
1918static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1919 struct snd_ctl_elem_info *uinfo)
1920{
1921 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1922 int num_bands = 0;
1923
1924 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1925 HPI_TUNER_BAND_LAST);
1926
1927 if (num_bands < 0)
1928 return num_bands;
1929
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001930 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001931}
1932
1933static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1934 struct snd_ctl_elem_value *ucontrol)
1935{
1936 u32 h_control = kcontrol->private_value;
1937 /*
1938 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1939 */
1940 u16 band, idx;
1941 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1942 u32 num_bands = 0;
1943
1944 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1945 HPI_TUNER_BAND_LAST);
1946
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001947 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001948
1949 ucontrol->value.enumerated.item[0] = -1;
1950 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1951 if (tuner_bands[idx] == band) {
1952 ucontrol->value.enumerated.item[0] = idx;
1953 break;
1954 }
1955
1956 return 0;
1957}
1958
1959static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1960 struct snd_ctl_elem_value *ucontrol)
1961{
1962 /*
1963 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1964 */
1965 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001966 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001967 u16 band;
1968 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1969 u32 num_bands = 0;
1970
1971 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1972 HPI_TUNER_BAND_LAST);
1973
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001974 idx = ucontrol->value.enumerated.item[0];
1975 if (idx >= ARRAY_SIZE(tuner_bands))
1976 idx = ARRAY_SIZE(tuner_bands) - 1;
1977 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001978 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001979
1980 return 1;
1981}
1982
1983/* Freq */
1984
1985static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1986 struct snd_ctl_elem_info *uinfo)
1987{
1988 u32 h_control = kcontrol->private_value;
1989 u16 err;
1990 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1991 u16 num_bands = 0, band_iter, idx;
1992 u32 freq_range[3], temp_freq_range[3];
1993
1994 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1995 HPI_TUNER_BAND_LAST);
1996
1997 freq_range[0] = INT_MAX;
1998 freq_range[1] = 0;
1999 freq_range[2] = INT_MAX;
2000
2001 for (band_iter = 0; band_iter < num_bands; band_iter++) {
2002 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002003 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002004 idx, tuner_bands[band_iter],
2005 &temp_freq_range[idx]);
2006 if (err != 0)
2007 return err;
2008 }
2009
2010 /* skip band with bogus stepping */
2011 if (temp_freq_range[2] <= 0)
2012 continue;
2013
2014 if (temp_freq_range[0] < freq_range[0])
2015 freq_range[0] = temp_freq_range[0];
2016 if (temp_freq_range[1] > freq_range[1])
2017 freq_range[1] = temp_freq_range[1];
2018 if (temp_freq_range[2] < freq_range[2])
2019 freq_range[2] = temp_freq_range[2];
2020 }
2021
2022 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2023 uinfo->count = 1;
2024 uinfo->value.integer.min = ((int)freq_range[0]);
2025 uinfo->value.integer.max = ((int)freq_range[1]);
2026 uinfo->value.integer.step = ((int)freq_range[2]);
2027 return 0;
2028}
2029
2030static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
2031 struct snd_ctl_elem_value *ucontrol)
2032{
2033 u32 h_control = kcontrol->private_value;
2034 u32 freq;
2035
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002036 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002037 ucontrol->value.integer.value[0] = freq;
2038
2039 return 0;
2040}
2041
2042static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2043 struct snd_ctl_elem_value *ucontrol)
2044{
2045 u32 h_control = kcontrol->private_value;
2046 u32 freq;
2047
2048 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002049 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002050
2051 return 1;
2052}
2053
2054/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002055static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2056 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002057{
2058 struct snd_card *card = asihpi->card;
2059 struct snd_kcontrol_new snd_control;
2060
2061 snd_control.private_value = hpi_ctl->h_control;
2062 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2063
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002064 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002065 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002066 snd_control.info = snd_asihpi_tuner_gain_info;
2067 snd_control.get = snd_asihpi_tuner_gain_get;
2068 snd_control.put = snd_asihpi_tuner_gain_put;
2069
2070 if (ctl_add(card, &snd_control, asihpi) < 0)
2071 return -EINVAL;
2072 }
2073
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002074 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002075 snd_control.info = snd_asihpi_tuner_band_info;
2076 snd_control.get = snd_asihpi_tuner_band_get;
2077 snd_control.put = snd_asihpi_tuner_band_put;
2078
2079 if (ctl_add(card, &snd_control, asihpi) < 0)
2080 return -EINVAL;
2081
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002082 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002083 snd_control.info = snd_asihpi_tuner_freq_info;
2084 snd_control.get = snd_asihpi_tuner_freq_get;
2085 snd_control.put = snd_asihpi_tuner_freq_put;
2086
2087 return ctl_add(card, &snd_control, asihpi);
2088}
2089
2090/*------------------------------------------------------------
2091 Meter controls
2092 ------------------------------------------------------------*/
2093static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2094 struct snd_ctl_elem_info *uinfo)
2095{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002096 u32 h_control = kcontrol->private_value;
2097 u32 count;
2098 u16 err;
2099 err = hpi_meter_query_channels(h_control, &count);
2100 if (err)
2101 count = HPI_MAX_CHANNELS;
2102
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002103 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002104 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002105 uinfo->value.integer.min = 0;
2106 uinfo->value.integer.max = 0x7FFFFFFF;
2107 return 0;
2108}
2109
2110/* linear values for 10dB steps */
2111static int log2lin[] = {
2112 0x7FFFFFFF, /* 0dB */
2113 679093956,
2114 214748365,
2115 67909396,
2116 21474837,
2117 6790940,
2118 2147484, /* -60dB */
2119 679094,
2120 214748, /* -80 */
2121 67909,
2122 21475, /* -100 */
2123 6791,
2124 2147,
2125 679,
2126 214,
2127 68,
2128 21,
2129 7,
2130 2
2131};
2132
2133static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2134 struct snd_ctl_elem_value *ucontrol)
2135{
2136 u32 h_control = kcontrol->private_value;
2137 short an_gain_mB[HPI_MAX_CHANNELS], i;
2138 u16 err;
2139
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002140 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002141
2142 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2143 if (err) {
2144 ucontrol->value.integer.value[i] = 0;
2145 } else if (an_gain_mB[i] >= 0) {
2146 ucontrol->value.integer.value[i] =
2147 an_gain_mB[i] << 16;
2148 } else {
2149 /* -ve is log value in millibels < -60dB,
2150 * convert to (roughly!) linear,
2151 */
2152 ucontrol->value.integer.value[i] =
2153 log2lin[an_gain_mB[i] / -1000];
2154 }
2155 }
2156 return 0;
2157}
2158
Bill Pembertone23e7a12012-12-06 12:35:10 -05002159static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2160 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002161{
2162 struct snd_card *card = asihpi->card;
2163 struct snd_kcontrol_new snd_control;
2164
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002165 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002166 snd_control.access =
2167 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2168 snd_control.info = snd_asihpi_meter_info;
2169 snd_control.get = snd_asihpi_meter_get;
2170
2171 snd_control.index = subidx;
2172
2173 return ctl_add(card, &snd_control, asihpi);
2174}
2175
2176/*------------------------------------------------------------
2177 Multiplexer controls
2178 ------------------------------------------------------------*/
2179static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2180{
2181 u32 h_control = snd_control->private_value;
2182 struct hpi_control hpi_ctl;
2183 int s, err;
2184 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002185 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002186 &hpi_ctl.
2187 src_node_type,
2188 &hpi_ctl.
2189 src_node_index);
2190 if (err)
2191 break;
2192 }
2193 return s;
2194}
2195
2196static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2197 struct snd_ctl_elem_info *uinfo)
2198{
2199 int err;
2200 u16 src_node_type, src_node_index;
2201 u32 h_control = kcontrol->private_value;
2202
2203 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2204 uinfo->count = 1;
2205 uinfo->value.enumerated.items =
2206 snd_card_asihpi_mux_count_sources(kcontrol);
2207
2208 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2209 uinfo->value.enumerated.item =
2210 uinfo->value.enumerated.items - 1;
2211
2212 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002213 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002214 uinfo->value.enumerated.item,
2215 &src_node_type, &src_node_index);
2216
2217 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002218 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002219 src_node_index);
2220 return 0;
2221}
2222
2223static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2224 struct snd_ctl_elem_value *ucontrol)
2225{
2226 u32 h_control = kcontrol->private_value;
2227 u16 source_type, source_index;
2228 u16 src_node_type, src_node_index;
2229 int s;
2230
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002231 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002232 &source_type, &source_index));
2233 /* Should cache this search result! */
2234 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002235 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002236 &src_node_type, &src_node_index))
2237 break;
2238
2239 if ((source_type == src_node_type)
2240 && (source_index == src_node_index)) {
2241 ucontrol->value.enumerated.item[0] = s;
2242 return 0;
2243 }
2244 }
2245 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002246 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002247 h_control, source_type, source_index);
2248 ucontrol->value.enumerated.item[0] = 0;
2249 return 0;
2250}
2251
2252static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2253 struct snd_ctl_elem_value *ucontrol)
2254{
2255 int change;
2256 u32 h_control = kcontrol->private_value;
2257 u16 source_type, source_index;
2258 u16 e;
2259
2260 change = 1;
2261
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002262 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002263 ucontrol->value.enumerated.item[0],
2264 &source_type, &source_index);
2265 if (!e)
2266 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002267 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002268 source_type, source_index));
2269 return change;
2270}
2271
2272
Bill Pembertone23e7a12012-12-06 12:35:10 -05002273static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2274 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002275{
2276 struct snd_card *card = asihpi->card;
2277 struct snd_kcontrol_new snd_control;
2278
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002279 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002280 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2281 snd_control.info = snd_asihpi_mux_info;
2282 snd_control.get = snd_asihpi_mux_get;
2283 snd_control.put = snd_asihpi_mux_put;
2284
2285 return ctl_add(card, &snd_control, asihpi);
2286
2287}
2288
2289/*------------------------------------------------------------
2290 Channel mode controls
2291 ------------------------------------------------------------*/
2292static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2293 struct snd_ctl_elem_info *uinfo)
2294{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002295 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2296 "invalid",
2297 "Normal", "Swap",
2298 "From Left", "From Right",
2299 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002300 };
2301
2302 u32 h_control = kcontrol->private_value;
2303 u16 mode;
2304 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002305 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002306 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002307
2308 /* HPI channel mode values can be from 1 to 6
2309 Some adapters only support a contiguous subset
2310 */
2311 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002312 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002313 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002314 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002315 valid_modes++;
2316 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002317
Takashi Iwai74eeb142012-01-09 18:26:05 +01002318 if (!valid_modes)
2319 return -EINVAL;
2320
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002321 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002322}
2323
2324static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
2326{
2327 u32 h_control = kcontrol->private_value;
2328 u16 mode;
2329
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002330 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002331 mode = 1;
2332
2333 ucontrol->value.enumerated.item[0] = mode - 1;
2334
2335 return 0;
2336}
2337
2338static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2339 struct snd_ctl_elem_value *ucontrol)
2340{
2341 int change;
2342 u32 h_control = kcontrol->private_value;
2343
2344 change = 1;
2345
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002346 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002347 ucontrol->value.enumerated.item[0] + 1));
2348 return change;
2349}
2350
2351
Bill Pembertone23e7a12012-12-06 12:35:10 -05002352static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2353 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002354{
2355 struct snd_card *card = asihpi->card;
2356 struct snd_kcontrol_new snd_control;
2357
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002358 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002359 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2360 snd_control.info = snd_asihpi_cmode_info;
2361 snd_control.get = snd_asihpi_cmode_get;
2362 snd_control.put = snd_asihpi_cmode_put;
2363
2364 return ctl_add(card, &snd_control, asihpi);
2365}
2366
2367/*------------------------------------------------------------
2368 Sampleclock source controls
2369 ------------------------------------------------------------*/
Krzysztof Kozlowski46d212c2015-03-24 11:48:24 +01002370static const char * const sampleclock_sources[] = {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002371 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2372 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002373 "Prev Module", "BLU-Link",
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002374 "Digital2", "Digital3", "Digital4", "Digital5",
2375 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002376
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002377 /* Number of strings must match expected enumerated values */
2378 compile_time_assert(
2379 (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
2380 assert_sampleclock_sources_size);
2381
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002382static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2383 struct snd_ctl_elem_info *uinfo)
2384{
2385 struct snd_card_asihpi *asihpi =
2386 (struct snd_card_asihpi *)(kcontrol->private_data);
2387 struct clk_cache *clkcache = &asihpi->cc;
2388 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2389 uinfo->count = 1;
2390 uinfo->value.enumerated.items = clkcache->count;
2391
2392 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2393 uinfo->value.enumerated.item =
2394 uinfo->value.enumerated.items - 1;
2395
2396 strcpy(uinfo->value.enumerated.name,
2397 clkcache->s[uinfo->value.enumerated.item].name);
2398 return 0;
2399}
2400
2401static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2402 struct snd_ctl_elem_value *ucontrol)
2403{
2404 struct snd_card_asihpi *asihpi =
2405 (struct snd_card_asihpi *)(kcontrol->private_data);
2406 struct clk_cache *clkcache = &asihpi->cc;
2407 u32 h_control = kcontrol->private_value;
2408 u16 source, srcindex = 0;
2409 int i;
2410
2411 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002412 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002413 source = 0;
2414
2415 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002416 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002417 srcindex = 0;
2418
2419 for (i = 0; i < clkcache->count; i++)
2420 if ((clkcache->s[i].source == source) &&
2421 (clkcache->s[i].index == srcindex))
2422 break;
2423
2424 ucontrol->value.enumerated.item[0] = i;
2425
2426 return 0;
2427}
2428
2429static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2430 struct snd_ctl_elem_value *ucontrol)
2431{
2432 struct snd_card_asihpi *asihpi =
2433 (struct snd_card_asihpi *)(kcontrol->private_data);
2434 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002435 unsigned int item;
2436 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002437 u32 h_control = kcontrol->private_value;
2438
2439 change = 1;
2440 item = ucontrol->value.enumerated.item[0];
2441 if (item >= clkcache->count)
2442 item = clkcache->count-1;
2443
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002444 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002445 h_control, clkcache->s[item].source));
2446
2447 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002448 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002449 h_control, clkcache->s[item].index));
2450 return change;
2451}
2452
2453/*------------------------------------------------------------
2454 Clkrate controls
2455 ------------------------------------------------------------*/
2456/* Need to change this to enumerated control with list of rates */
2457static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2458 struct snd_ctl_elem_info *uinfo)
2459{
2460 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2461 uinfo->count = 1;
2462 uinfo->value.integer.min = 8000;
2463 uinfo->value.integer.max = 192000;
2464 uinfo->value.integer.step = 100;
2465
2466 return 0;
2467}
2468
2469static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2470 struct snd_ctl_elem_value *ucontrol)
2471{
2472 u32 h_control = kcontrol->private_value;
2473 u32 rate;
2474 u16 e;
2475
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002476 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002477 if (!e)
2478 ucontrol->value.integer.value[0] = rate;
2479 else
2480 ucontrol->value.integer.value[0] = 0;
2481 return 0;
2482}
2483
2484static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2485 struct snd_ctl_elem_value *ucontrol)
2486{
2487 int change;
2488 u32 h_control = kcontrol->private_value;
2489
2490 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2491 asihpi->mixer_clkrate[addr][1] != right;
2492 */
2493 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002494 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002495 ucontrol->value.integer.value[0]));
2496 return change;
2497}
2498
2499static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2500 struct snd_ctl_elem_info *uinfo)
2501{
2502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2503 uinfo->count = 1;
2504 uinfo->value.integer.min = 8000;
2505 uinfo->value.integer.max = 192000;
2506 uinfo->value.integer.step = 100;
2507
2508 return 0;
2509}
2510
2511static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2512 struct snd_ctl_elem_value *ucontrol)
2513{
2514 u32 h_control = kcontrol->private_value;
2515 u32 rate;
2516 u16 e;
2517
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002518 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002519 if (!e)
2520 ucontrol->value.integer.value[0] = rate;
2521 else
2522 ucontrol->value.integer.value[0] = 0;
2523 return 0;
2524}
2525
Bill Pembertone23e7a12012-12-06 12:35:10 -05002526static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2527 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002529 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002530 struct snd_kcontrol_new snd_control;
2531
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002532 struct clk_cache *clkcache;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002533 u32 hSC = hpi_ctl->h_control;
2534 int has_aes_in = 0;
2535 int i, j;
2536 u16 source;
2537
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002538 if (snd_BUG_ON(!asihpi))
2539 return -EINVAL;
2540 card = asihpi->card;
2541 clkcache = &asihpi->cc;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002542 snd_control.private_value = hpi_ctl->h_control;
2543
2544 clkcache->has_local = 0;
2545
2546 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002547 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002548 i, &source))
2549 break;
2550 clkcache->s[i].source = source;
2551 clkcache->s[i].index = 0;
2552 clkcache->s[i].name = sampleclock_sources[source];
2553 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2554 has_aes_in = 1;
2555 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2556 clkcache->has_local = 1;
2557 }
2558 if (has_aes_in)
2559 /* already will have picked up index 0 above */
2560 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002561 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002562 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2563 &source))
2564 break;
2565 clkcache->s[i].source =
2566 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2567 clkcache->s[i].index = j;
2568 clkcache->s[i].name = sampleclock_sources[
2569 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2570 i++;
2571 }
2572 clkcache->count = i;
2573
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002574 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002575 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2576 snd_control.info = snd_asihpi_clksrc_info;
2577 snd_control.get = snd_asihpi_clksrc_get;
2578 snd_control.put = snd_asihpi_clksrc_put;
2579 if (ctl_add(card, &snd_control, asihpi) < 0)
2580 return -EINVAL;
2581
2582
2583 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002584 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002585 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2586 snd_control.info = snd_asihpi_clklocal_info;
2587 snd_control.get = snd_asihpi_clklocal_get;
2588 snd_control.put = snd_asihpi_clklocal_put;
2589
2590
2591 if (ctl_add(card, &snd_control, asihpi) < 0)
2592 return -EINVAL;
2593 }
2594
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002595 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002596 snd_control.access =
2597 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2598 snd_control.info = snd_asihpi_clkrate_info;
2599 snd_control.get = snd_asihpi_clkrate_get;
2600
2601 return ctl_add(card, &snd_control, asihpi);
2602}
2603/*------------------------------------------------------------
2604 Mixer
2605 ------------------------------------------------------------*/
2606
Bill Pembertone23e7a12012-12-06 12:35:10 -05002607static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002608{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002609 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002610 unsigned int idx = 0;
2611 unsigned int subindex = 0;
2612 int err;
2613 struct hpi_control hpi_ctl, prev_ctl;
2614
2615 if (snd_BUG_ON(!asihpi))
2616 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002617 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002618 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002619
2620 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002621 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002622 &asihpi->h_mixer);
2623 hpi_handle_error(err);
2624 if (err)
2625 return -err;
2626
Takashi Iwai21896bc2010-06-02 12:08:37 +02002627 memset(&prev_ctl, 0, sizeof(prev_ctl));
2628 prev_ctl.control_type = -1;
2629
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002630 for (idx = 0; idx < 2000; idx++) {
2631 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002632 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002633 idx,
2634 &hpi_ctl.src_node_type,
2635 &hpi_ctl.src_node_index,
2636 &hpi_ctl.dst_node_type,
2637 &hpi_ctl.dst_node_index,
2638 &hpi_ctl.control_type,
2639 &hpi_ctl.h_control);
2640 if (err) {
2641 if (err == HPI_ERROR_CONTROL_DISABLED) {
2642 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002643 dev_info(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002644 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002645 idx);
2646 continue;
2647 } else
2648 break;
2649
2650 }
2651
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002652 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2653 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002654
2655 /* ASI50xx in SSX mode has multiple meters on the same node.
2656 Use subindex to create distinct ALSA controls
2657 for any duplicated controls.
2658 */
2659 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2660 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2661 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2662 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2663 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2664 subindex++;
2665 else
2666 subindex = 0;
2667
2668 prev_ctl = hpi_ctl;
2669
2670 switch (hpi_ctl.control_type) {
2671 case HPI_CONTROL_VOLUME:
2672 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2673 break;
2674 case HPI_CONTROL_LEVEL:
2675 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2676 break;
2677 case HPI_CONTROL_MULTIPLEXER:
2678 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2679 break;
2680 case HPI_CONTROL_CHANNEL_MODE:
2681 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2682 break;
2683 case HPI_CONTROL_METER:
2684 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2685 break;
2686 case HPI_CONTROL_SAMPLECLOCK:
2687 err = snd_asihpi_sampleclock_add(
2688 asihpi, &hpi_ctl);
2689 break;
2690 case HPI_CONTROL_CONNECTION: /* ignore these */
2691 continue;
2692 case HPI_CONTROL_TUNER:
2693 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2694 break;
2695 case HPI_CONTROL_AESEBU_TRANSMITTER:
2696 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2697 break;
2698 case HPI_CONTROL_AESEBU_RECEIVER:
2699 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2700 break;
2701 case HPI_CONTROL_VOX:
2702 case HPI_CONTROL_BITSTREAM:
2703 case HPI_CONTROL_MICROPHONE:
2704 case HPI_CONTROL_PARAMETRIC_EQ:
2705 case HPI_CONTROL_COMPANDER:
2706 default:
2707 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002708 dev_info(&asihpi->pci->dev,
2709 "Untranslated HPI Control (%d) %d %d %d %d %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002710 idx,
2711 hpi_ctl.control_type,
2712 hpi_ctl.src_node_type,
2713 hpi_ctl.src_node_index,
2714 hpi_ctl.dst_node_type,
2715 hpi_ctl.dst_node_index);
2716 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002717 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002718 if (err < 0)
2719 return err;
2720 }
2721 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2722 hpi_handle_error(err);
2723
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002724 dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002725
2726 return 0;
2727}
2728
2729/*------------------------------------------------------------
2730 /proc interface
2731 ------------------------------------------------------------*/
2732
2733static void
2734snd_asihpi_proc_read(struct snd_info_entry *entry,
2735 struct snd_info_buffer *buffer)
2736{
2737 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002738 u32 h_control;
2739 u32 rate = 0;
2740 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002741
2742 u16 num_outstreams;
2743 u16 num_instreams;
2744 u16 version;
2745 u32 serial_number;
2746 u16 type;
2747
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002748 int err;
2749
2750 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002751
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002752 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2753 &num_outstreams, &num_instreams,
2754 &version, &serial_number, &type));
2755
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002756 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002757 "Adapter type ASI%4X\nHardware Index %d\n"
2758 "%d outstreams\n%d instreams\n",
2759 type, asihpi->hpi->adapter->index,
2760 num_outstreams, num_instreams);
2761
2762 snd_iprintf(buffer,
2763 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2764 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002765 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2766
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002767 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002768 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2769 HPI_CONTROL_SAMPLECLOCK, &h_control);
2770
2771 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002772 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002773 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002774
2775 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002776 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002777 rate, sampleclock_sources[source]);
2778 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002779}
2780
Bill Pembertone23e7a12012-12-06 12:35:10 -05002781static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002782{
2783 struct snd_info_entry *entry;
2784
2785 if (!snd_card_proc_new(asihpi->card, "info", &entry))
2786 snd_info_set_text_ops(entry, asihpi, snd_asihpi_proc_read);
2787}
2788
2789/*------------------------------------------------------------
2790 HWDEP
2791 ------------------------------------------------------------*/
2792
2793static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2794{
2795 if (enable_hpi_hwdep)
2796 return 0;
2797 else
2798 return -ENODEV;
2799
2800}
2801
2802static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2803{
2804 if (enable_hpi_hwdep)
2805 return asihpi_hpi_release(file);
2806 else
2807 return -ENODEV;
2808}
2809
2810static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2811 unsigned int cmd, unsigned long arg)
2812{
2813 if (enable_hpi_hwdep)
2814 return asihpi_hpi_ioctl(file, cmd, arg);
2815 else
2816 return -ENODEV;
2817}
2818
2819
2820/* results in /dev/snd/hwC#D0 file for each card with index #
2821 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2822*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002823static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002824{
2825 struct snd_hwdep *hw;
2826 int err;
2827
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002828 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2829 if (err < 0)
2830 return err;
2831 strcpy(hw->name, "asihpi (HPI)");
2832 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2833 hw->ops.open = snd_asihpi_hpi_open;
2834 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2835 hw->ops.release = snd_asihpi_hpi_release;
2836 hw->private_data = asihpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002837 return 0;
2838}
2839
2840/*------------------------------------------------------------
2841 CARD
2842 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002843static int snd_asihpi_probe(struct pci_dev *pci_dev,
2844 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002845{
2846 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002847 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002848 struct snd_card *card;
2849 struct snd_card_asihpi *asihpi;
2850
2851 u32 h_control;
2852 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002853 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002854
2855 static int dev;
2856 if (dev >= SNDRV_CARDS)
2857 return -ENODEV;
2858
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002859 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002860 if (!enable[dev]) {
2861 dev++;
2862 return -ENOENT;
2863 }
2864
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002865 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002866 err = asihpi_adapter_probe(pci_dev, pci_id);
2867 if (err < 0)
2868 return err;
2869
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002870 hpi = pci_get_drvdata(pci_dev);
2871 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002872 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002873 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2874 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002875 if (err < 0) {
2876 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002877 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2878 THIS_MODULE, sizeof(struct snd_card_asihpi),
2879 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002880 if (err < 0)
2881 return err;
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002882 dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002883 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002884 }
2885
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002886 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002887 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002888 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002889 asihpi->hpi = hpi;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002890 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002891
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002892 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002893 HPI_ADAPTER_PROPERTY_CAPS1,
2894 NULL, &asihpi->support_grouping);
2895 if (err)
2896 asihpi->support_grouping = 0;
2897
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002898 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002899 HPI_ADAPTER_PROPERTY_CAPS2,
2900 &asihpi->support_mrx, NULL);
2901 if (err)
2902 asihpi->support_mrx = 0;
2903
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002904 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002905 HPI_ADAPTER_PROPERTY_INTERVAL,
2906 NULL, &asihpi->update_interval_frames);
2907 if (err)
2908 asihpi->update_interval_frames = 512;
2909
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002910 if (hpi->interrupt_mode) {
2911 asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
2912 asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
2913 tasklet_init(&asihpi->t, snd_card_asihpi_int_task,
2914 (unsigned long)hpi);
2915 hpi->interrupt_callback = snd_card_asihpi_isr;
2916 } else {
2917 asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
2918 asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
2919 }
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002920
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002921 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002922 0, &h_stream));
2923
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002924 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002925 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002926
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002927 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002928
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002929 if (!asihpi->can_dma)
2930 asihpi->update_interval_frames *= 2;
2931
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002932 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002933 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2934 &asihpi->in_max_chans, &asihpi->out_max_chans);
2935 if (err) {
2936 asihpi->in_max_chans = 2;
2937 asihpi->out_max_chans = 2;
2938 }
2939
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002940 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2941 asihpi->out_min_chans = asihpi->out_max_chans;
2942 asihpi->in_min_chans = asihpi->in_max_chans;
2943 asihpi->support_grouping = 0;
2944 } else {
2945 asihpi->out_min_chans = 1;
2946 asihpi->in_min_chans = 1;
2947 }
2948
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002949 dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002950 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002951 asihpi->support_grouping,
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002952 asihpi->support_mrx,
2953 asihpi->update_interval_frames
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002954 );
2955
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002956 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002957 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002958 dev_err(&pci_dev->dev, "pcm_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002959 goto __nodev;
2960 }
2961 err = snd_card_asihpi_mixer_new(asihpi);
2962 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002963 dev_err(&pci_dev->dev, "mixer_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002964 goto __nodev;
2965 }
2966
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002967 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002968 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2969 HPI_CONTROL_SAMPLECLOCK, &h_control);
2970
2971 if (!err)
2972 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002973 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002974
2975 snd_asihpi_proc_init(asihpi);
2976
2977 /* always create, can be enabled or disabled dynamically
2978 by enable_hwdep module param*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002979 snd_asihpi_hpi_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002980
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002981 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002982
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002983 sprintf(card->shortname, "AudioScience ASI%4X",
2984 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002985 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002986 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002987 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002988
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002989 if (!err) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002990 dev++;
2991 return 0;
2992 }
2993__nodev:
2994 snd_card_free(card);
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002995 dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002996 return err;
2997
2998}
2999
Bill Pembertone23e7a12012-12-06 12:35:10 -05003000static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003001{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13003002 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003003 struct snd_card_asihpi *asihpi = hpi->snd_card->private_data;
3004
3005 /* Stop interrupts */
3006 if (hpi->interrupt_mode) {
3007 hpi->interrupt_callback = NULL;
3008 hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
3009 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
3010 tasklet_kill(&asihpi->t);
3011 }
3012
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13003013 snd_card_free(hpi->snd_card);
3014 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003015 asihpi_adapter_remove(pci_dev);
3016}
3017
Benoit Taine9baa3c32014-08-08 15:56:03 +02003018static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003019 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
3020 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3021 (kernel_ulong_t)HPI_6205},
3022 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
3023 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3024 (kernel_ulong_t)HPI_6000},
3025 {0,}
3026};
3027MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
3028
3029static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003030 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003031 .id_table = asihpi_pci_tbl,
3032 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003033 .remove = snd_asihpi_remove,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003034};
3035
3036static int __init snd_asihpi_init(void)
3037{
3038 asihpi_init();
3039 return pci_register_driver(&driver);
3040}
3041
3042static void __exit snd_asihpi_exit(void)
3043{
3044
3045 pci_unregister_driver(&driver);
3046 asihpi_exit();
3047}
3048
3049module_init(snd_asihpi_init)
3050module_exit(snd_asihpi_exit)
3051