blob: 2a21a3d997196283f3107929fa1f6847e10724f8 [file] [log] [blame]
Thomas Gleixner07d7fe72019-05-29 07:12:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002/*
3 * Asihpi soundcard
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13004 * Copyright (c) by AudioScience Inc <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02005 *
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02006 * The following is not a condition of use, merely a request:
7 * If you modify this program, particularly if you fix errors, AudioScience Inc
8 * would appreciate it if you grant us the right to use those modifications
9 * for any purpose including commercial applications.
10 */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130011
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020012#include "hpi_internal.h"
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130013#include "hpi_version.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020014#include "hpimsginit.h"
15#include "hpioctl.h"
Eliot Blennerhassett7036b922011-12-22 13:38:43 +130016#include "hpicmn.h"
17
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020018#include <linux/pci.h>
19#include <linux/init.h>
20#include <linux/jiffies.h>
21#include <linux/slab.h>
22#include <linux/time.h>
23#include <linux/wait.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020025#include <sound/core.h>
26#include <sound/control.h>
27#include <sound/pcm.h>
28#include <sound/pcm_params.h>
29#include <sound/info.h>
30#include <sound/initval.h>
31#include <sound/tlv.h>
32#include <sound/hwdep.h>
33
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020034MODULE_LICENSE("GPL");
35MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130036MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130037 HPI_VER_STRING);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130039#if defined CONFIG_SND_DEBUG_VERBOSE
40/**
41 * snd_printddd - very verbose debug printk
42 * @format: format string
43 *
44 * Works like snd_printk() for debugging purposes.
45 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
46 * Must set snd module debug parameter to 3 to enable at runtime.
47 */
48#define snd_printddd(format, args...) \
49 __snd_printk(3, __FILE__, __LINE__, format, ##args)
50#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120051#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130052#endif
53
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020054static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
55static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103056static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
57static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020058
Joe Perches6a73cf42018-05-23 12:20:59 -070059module_param_array(index, int, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020060MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
61
Joe Perches6a73cf42018-05-23 12:20:59 -070062module_param_array(id, charp, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020063MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
64
Joe Perches6a73cf42018-05-23 12:20:59 -070065module_param_array(enable, bool, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020066MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
67
Joe Perches6a73cf42018-05-23 12:20:59 -070068module_param(enable_hpi_hwdep, bool, 0644);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020069MODULE_PARM_DESC(enable_hpi_hwdep,
70 "ALSA enable HPI hwdep for AudioScience soundcard ");
71
72/* identify driver */
73#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130074static char *build_info = "Built using headers from kernel source";
Joe Perches6a73cf42018-05-23 12:20:59 -070075module_param(build_info, charp, 0444);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130076MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020077#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130078static char *build_info = "Built within ALSA source";
Joe Perches6a73cf42018-05-23 12:20:59 -070079module_param(build_info, charp, 0444);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130080MODULE_PARM_DESC(build_info, "Built within ALSA source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020081#endif
82
83/* set to 1 to dump every control from adapter to log */
84static const int mixer_dump;
85
86#define DEFAULT_SAMPLERATE 44100
87static int adapter_fs = DEFAULT_SAMPLERATE;
88
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020089/* defaults */
90#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130091#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020092#define BUFFER_BYTES_MAX (512 * 1024)
93
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020094#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
95
96struct clk_source {
97 int source;
98 int index;
Eliot Blennerhassett3872f192014-11-20 16:22:49 +130099 const char *name;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200100};
101
102struct clk_cache {
103 int count;
104 int has_local;
105 struct clk_source s[MAX_CLOCKSOURCES];
106};
107
108/* Per card data */
109struct snd_card_asihpi {
110 struct snd_card *card;
111 struct pci_dev *pci;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300112 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200113
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300114 /* In low latency mode there is only one stream, a pointer to its
115 * private data is stored here on trigger and cleared on stop.
116 * The interrupt handler uses it as a parameter when calling
117 * snd_card_asihpi_timer_function().
118 */
119 struct snd_card_asihpi_pcm *llmode_streampriv;
120 struct tasklet_struct t;
121 void (*pcm_start)(struct snd_pcm_substream *substream);
122 void (*pcm_stop)(struct snd_pcm_substream *substream);
123
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200124 u32 h_mixer;
125 struct clk_cache cc;
126
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200127 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200128 u16 support_grouping;
129 u16 support_mrx;
130 u16 update_interval_frames;
131 u16 in_max_chans;
132 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300133 u16 in_min_chans;
134 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200135};
136
137/* Per stream data */
138struct snd_card_asihpi_pcm {
139 struct timer_list timer;
140 unsigned int respawn_timer;
141 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300142 unsigned int buffer_bytes;
143 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200144 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300145 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
146 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
147 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200148 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200149 struct snd_pcm_substream *substream;
150 u32 h_stream;
151 struct hpi_format format;
152};
153
154/* universal stream verbs work with out or in stream handles */
155
156/* Functions to allow driver to give a buffer to HPI for busmastering */
157
158static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200159 u32 h_stream, /* handle to outstream. */
160 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
161 u32 pci_address
162)
163{
164 struct hpi_message hm;
165 struct hpi_response hr;
166 unsigned int obj = hpi_handle_object(h_stream);
167
168 if (!h_stream)
169 return HPI_ERROR_INVALID_OBJ;
170 hpi_init_message_response(&hm, &hr, obj,
171 obj == HPI_OBJ_OSTREAM ?
172 HPI_OSTREAM_HOSTBUFFER_ALLOC :
173 HPI_ISTREAM_HOSTBUFFER_ALLOC);
174
175 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
176 &hm.obj_index);
177
178 hm.u.d.u.buffer.buffer_size = size_in_bytes;
179 hm.u.d.u.buffer.pci_address = pci_address;
180 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
181 hpi_send_recv(&hm, &hr);
182 return hr.error;
183}
184
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300185static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200186{
187 struct hpi_message hm;
188 struct hpi_response hr;
189 unsigned int obj = hpi_handle_object(h_stream);
190
191 if (!h_stream)
192 return HPI_ERROR_INVALID_OBJ;
193
194 hpi_init_message_response(&hm, &hr, obj,
195 obj == HPI_OBJ_OSTREAM ?
196 HPI_OSTREAM_HOSTBUFFER_FREE :
197 HPI_ISTREAM_HOSTBUFFER_FREE);
198
199 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
200 &hm.obj_index);
201 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
202 hpi_send_recv(&hm, &hr);
203 return hr.error;
204}
205
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300206static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200207{
208 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300209 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200210 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300211 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200212}
213
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300214static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200215{
216 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300217 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200218 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220}
221
222static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200223 u32 h_stream,
224 u16 *pw_state,
225 u32 *pbuffer_size,
226 u32 *pdata_in_buffer,
227 u32 *psample_count,
228 u32 *pauxiliary_data
229)
230{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300231 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200232 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300233 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200234 pbuffer_size, pdata_in_buffer,
235 psample_count, pauxiliary_data);
236 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300237 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200238 pbuffer_size, pdata_in_buffer,
239 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300240 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200241}
242
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300243static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200244 u32 h_master,
245 u32 h_stream)
246{
247 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300248 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200249 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300250 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251}
252
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300253static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254{
255 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300258 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200259}
260
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262 u32 h_stream, u32 *mo, u32 *mi)
263{
264 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300265 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200266 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300267 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200268}
269
270static u16 handle_error(u16 err, int line, char *filename)
271{
272 if (err)
273 printk(KERN_WARNING
274 "in file %s, line %d: HPI error %d\n",
275 filename, line, err);
276 return err;
277}
278
279#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
280
281/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200282
283static void print_hwparams(struct snd_pcm_substream *substream,
284 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200285{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200286 char name[16];
287 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300288 snd_printdd("%s HWPARAMS\n", name);
289 snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
290 params_rate(p), params_channels(p),
291 params_format(p), params_subformat(p));
292 snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
293 params_buffer_bytes(p), params_period_bytes(p),
294 params_period_size(p), params_periods(p));
295 snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
296 params_buffer_size(p), params_access(p),
297 params_rate(p) * params_channels(p) *
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200298 snd_pcm_format_width(params_format(p)) / 8);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200299}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200300
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200301#define INVALID_FORMAT (__force snd_pcm_format_t)(-1)
302
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200303static snd_pcm_format_t hpi_to_alsa_formats[] = {
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200304 INVALID_FORMAT, /* INVALID */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200305 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
306 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200307 INVALID_FORMAT, /* HPI_FORMAT_MPEG_L1 3 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200308 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
309 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200310 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC2 6 */
311 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC3 7 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200312 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200313 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
314 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200315 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200316 INVALID_FORMAT, /* HPI_FORMAT_RAW_BITSTREAM 12 */
317 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200318 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
319#if 1
320 /* ALSA can't handle 3 byte sample size together with power-of-2
321 * constraint on buffer_bytes, so disable this format
322 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200323 INVALID_FORMAT
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200324#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300325 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200326#endif
327};
328
329
330static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
331 u16 *hpi_format)
332{
333 u16 format;
334
335 for (format = HPI_FORMAT_PCM8_UNSIGNED;
336 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
337 if (hpi_to_alsa_formats[format] == alsa_format) {
338 *hpi_format = format;
339 return 0;
340 }
341 }
342
343 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
344 alsa_format);
345 *hpi_format = 0;
346 return -EINVAL;
347}
348
349static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
350 struct snd_pcm_hardware *pcmhw)
351{
352 u16 err;
353 u32 h_control;
354 u32 sample_rate;
355 int idx;
356 unsigned int rate_min = 200000;
357 unsigned int rate_max = 0;
358 unsigned int rates = 0;
359
360 if (asihpi->support_mrx) {
361 rates |= SNDRV_PCM_RATE_CONTINUOUS;
362 rates |= SNDRV_PCM_RATE_8000_96000;
363 rate_min = 8000;
364 rate_max = 100000;
365 } else {
366 /* on cards without SRC,
367 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300368 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200369 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
370 HPI_CONTROL_SAMPLECLOCK, &h_control);
371 if (err) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +1300372 dev_err(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300373 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200374 }
375
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300376 for (idx = -1; idx < 100; idx++) {
377 if (idx == -1) {
378 if (hpi_sample_clock_get_sample_rate(h_control,
379 &sample_rate))
380 continue;
381 } else if (hpi_sample_clock_query_local_rate(h_control,
382 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200383 break;
384 }
385
386 rate_min = min(rate_min, sample_rate);
387 rate_max = max(rate_max, sample_rate);
388
389 switch (sample_rate) {
390 case 5512:
391 rates |= SNDRV_PCM_RATE_5512;
392 break;
393 case 8000:
394 rates |= SNDRV_PCM_RATE_8000;
395 break;
396 case 11025:
397 rates |= SNDRV_PCM_RATE_11025;
398 break;
399 case 16000:
400 rates |= SNDRV_PCM_RATE_16000;
401 break;
402 case 22050:
403 rates |= SNDRV_PCM_RATE_22050;
404 break;
405 case 32000:
406 rates |= SNDRV_PCM_RATE_32000;
407 break;
408 case 44100:
409 rates |= SNDRV_PCM_RATE_44100;
410 break;
411 case 48000:
412 rates |= SNDRV_PCM_RATE_48000;
413 break;
414 case 64000:
415 rates |= SNDRV_PCM_RATE_64000;
416 break;
417 case 88200:
418 rates |= SNDRV_PCM_RATE_88200;
419 break;
420 case 96000:
421 rates |= SNDRV_PCM_RATE_96000;
422 break;
423 case 176400:
424 rates |= SNDRV_PCM_RATE_176400;
425 break;
426 case 192000:
427 rates |= SNDRV_PCM_RATE_192000;
428 break;
429 default: /* some other rate */
430 rates |= SNDRV_PCM_RATE_KNOT;
431 }
432 }
433 }
434
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200435 pcmhw->rates = rates;
436 pcmhw->rate_min = rate_min;
437 pcmhw->rate_max = rate_max;
438}
439
440static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
441 struct snd_pcm_hw_params *params)
442{
443 struct snd_pcm_runtime *runtime = substream->runtime;
444 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
445 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
446 int err;
447 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400448 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200449 unsigned int bytes_per_sec;
450
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200451 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200452 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
453 if (err < 0)
454 return err;
455 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
456 if (err)
457 return err;
458
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200459 hpi_handle_error(hpi_format_create(&dpcm->format,
460 params_channels(params),
461 format, params_rate(params), 0, 0));
462
463 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300464 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200465 return -EINVAL;
466
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300467 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200468 dpcm->h_stream, &dpcm->format) != 0)
469 return -EINVAL;
470 }
471
472 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200473 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300474 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200475 params_buffer_bytes(params), runtime->dma_addr);
476 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300477 snd_printdd(
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300478 "stream_host_buffer_attach success %u %lu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200479 params_buffer_bytes(params),
480 (unsigned long)runtime->dma_addr);
481 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300482 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200483 err);
484 return -ENOMEM;
485 }
486
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300487 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300488 &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200489 }
490 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400491 width = snd_pcm_format_width(params_format(params));
492 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200493 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400494 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200495 return -EINVAL;
496
497 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300498 dpcm->buffer_bytes = params_buffer_bytes(params);
499 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200500
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200501 return 0;
502}
503
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300504static int
505snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
506{
507 struct snd_pcm_runtime *runtime = substream->runtime;
508 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
509 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300510 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300511
512 snd_pcm_lib_free_pages(substream);
513 return 0;
514}
515
516static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
517{
518 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
519 kfree(dpcm);
520}
521
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200522static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
523 substream)
524{
525 struct snd_pcm_runtime *runtime = substream->runtime;
526 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
527 int expiry;
528
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300529 expiry = HZ / 200;
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +1300530
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300531 expiry = max(expiry, 1); /* don't let it be zero! */
Takashi Iwai6fec2b52015-01-19 11:32:51 +0100532 mod_timer(&dpcm->timer, jiffies + expiry);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200533 dpcm->respawn_timer = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200534}
535
536static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
537{
538 struct snd_pcm_runtime *runtime = substream->runtime;
539 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
540
541 dpcm->respawn_timer = 0;
542 del_timer(&dpcm->timer);
543}
544
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300545static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
546{
547 struct snd_card_asihpi_pcm *dpcm;
548 struct snd_card_asihpi *card;
549
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300550 dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
551 card = snd_pcm_substream_chip(substream);
552
Takashi Iwai2a0d85d2017-09-07 10:51:54 +0200553 WARN_ON(in_interrupt());
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300554 tasklet_disable(&card->t);
555 card->llmode_streampriv = dpcm;
556 tasklet_enable(&card->t);
557
558 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
559 HPI_ADAPTER_PROPERTY_IRQ_RATE,
560 card->update_interval_frames, 0));
561}
562
563static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
564{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300565 struct snd_card_asihpi *card;
566
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300567 card = snd_pcm_substream_chip(substream);
568
569 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
570 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
571
572 if (in_interrupt())
573 card->llmode_streampriv = NULL;
574 else {
575 tasklet_disable(&card->t);
576 card->llmode_streampriv = NULL;
577 tasklet_enable(&card->t);
578 }
579}
580
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200581static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
582 int cmd)
583{
584 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
585 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
586 struct snd_pcm_substream *s;
587 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200588 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200589
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200590 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200591
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200592 switch (cmd) {
593 case SNDRV_PCM_TRIGGER_START:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300594 snd_printdd("%s trigger start\n", name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200595 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300596 struct snd_pcm_runtime *runtime = s->runtime;
597 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200598
599 if (snd_pcm_substream_chip(s) != card)
600 continue;
601
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300602 /* don't link Cap and Play */
603 if (substream->stream != s->stream)
604 continue;
605
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200606 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200607 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200608 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300609 * in buffer? Must be at least one period!
610 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200611 * buffer is bigger it may contain even more
612 * data??
613 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300614 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300615 snd_printddd("%d preload %d\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200616 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300617 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300618 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200619 preload,
620 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300621 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200622 }
623
624 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200625 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300626 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200627 dpcm->h_stream,
628 ds->h_stream);
629 if (!e) {
630 snd_pcm_trigger_done(s, substream);
631 } else {
632 hpi_handle_error(e);
633 break;
634 }
635 } else
636 break;
637 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200638 /* start the master stream */
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300639 card->pcm_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300640 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200641 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300642 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200643 break;
644
645 case SNDRV_PCM_TRIGGER_STOP:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300646 snd_printdd("%s trigger stop\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300647 card->pcm_stop(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200648 snd_pcm_group_for_each_entry(s, substream) {
649 if (snd_pcm_substream_chip(s) != card)
650 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300651 /* don't link Cap and Play */
652 if (substream->stream != s->stream)
653 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200654
655 /*? workaround linked streams don't
656 transition to SETUP 20070706*/
657 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
658
659 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200660 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661 snd_pcm_trigger_done(s, substream);
662 } else
663 break;
664 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200665
666 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300667 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200668 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
669 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300670 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200671
672 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300673 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674 break;
675
676 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300677 snd_printdd("%s trigger pause release\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300678 card->pcm_start(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300679 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200680 break;
681 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300682 snd_printdd("%s trigger pause push\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300683 card->pcm_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300684 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200685 break;
686 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300687 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200688 return -EINVAL;
689 }
690
691 return 0;
692}
693
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200694/*algorithm outline
695 Without linking degenerates to getting single stream pos etc
696 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
697*/
698/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300699pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200700for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300701 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300702 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300703 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200704}
705timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
706for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300707 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300708 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200709 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300710 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200711 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300712 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200713 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300714 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200715 }
716 }
717 snd_pcm_period_elapsed(s);
718 }
719}
720*/
721
722/** Minimum of 2 modulo values. Works correctly when the difference between
723* the values is less than half the modulus
724*/
725static inline unsigned int modulo_min(unsigned int a, unsigned int b,
726 unsigned long int modulus)
727{
728 unsigned int result;
729 if (((a-b) % modulus) < (modulus/2))
730 result = b;
731 else
732 result = a;
733
734 return result;
735}
736
737/** Timer function, equivalent to interrupt service routine for cards
738*/
Kees Cook394ca812017-10-04 17:53:40 -0700739static void snd_card_asihpi_timer_function(struct timer_list *t)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200740{
Kees Cook394ca812017-10-04 17:53:40 -0700741 struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300742 struct snd_pcm_substream *substream = dpcm->substream;
743 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200744 struct snd_pcm_runtime *runtime;
745 struct snd_pcm_substream *s;
746 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300747 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200748 unsigned int remdata, xfercount, next_jiffies;
749 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300750 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200751 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300752 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200753 char name[16];
754
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200755
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300756 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300757
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200758 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300759 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200760 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
761 runtime = s->runtime;
762
763 if (snd_pcm_substream_chip(s) != card)
764 continue;
765
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300766 /* don't link Cap and Play */
767 if (substream->stream != s->stream)
768 continue;
769
770 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200771 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300772 &buffer_size, &bytes_avail,
773 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200774
775 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300776 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200777
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200778 if (!card->can_dma)
779 on_card_bytes = bytes_avail;
780
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300781 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300782 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300783 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300784 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300785 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300786 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200787 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300788 }
789 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300790 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300791 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200792 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300793 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100794 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200795 continue;
796 }
797 } else {
798 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300799 }
800 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300801 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200802
803 if (first) {
804 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300805 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300806 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200807 first = 0;
808 } else {
809 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300810 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200811 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300812 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200813 newdata);
814 }
815
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300816 snd_printddd(
817 "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 +1300818 name, s->number, state,
819 ds->pcm_buf_elapsed_dma_ofs,
820 ds->pcm_buf_host_rw_ofs,
821 pcm_buf_dma_ofs,
822 (int)bytes_avail,
823
824 (int)on_card_bytes,
825 buffer_size-bytes_avail,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 (unsigned long)frames_to_bytes(runtime,
827 runtime->status->hw_ptr),
828 (unsigned long)frames_to_bytes(runtime,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300829 runtime->control->appl_ptr)
830 );
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300831 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200832 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300833 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200834
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300835 remdata = newdata % dpcm->period_bytes;
836 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300837 /* come back when on_card_bytes has decreased enough to allow
838 write to happen, or when data has been consumed to make another
839 period
840 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300841 if (xfercount && (on_card_bytes > dpcm->period_bytes))
842 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300843 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300844 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300845
846 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200847 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300848 snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300849 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
850
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300851 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200852 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200853
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300854 /* don't link Cap and Play */
855 if (substream->stream != s->stream)
856 continue;
857
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300858 /* Store dma offset for use by pointer callback */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300859 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
860
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200861 if (xfercount &&
862 /* Limit use of on card fifo for playback */
863 ((on_card_bytes <= ds->period_bytes) ||
864 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
865
866 {
867
868 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
869 unsigned int xfer1, xfer2;
870 char *pd = &s->runtime->dma_area[buf_ofs];
871
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200872 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200873 xfer1 = xfercount;
874 xfer2 = 0;
875 } else {
876 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
877 xfer2 = xfercount - xfer1;
878 }
879
880 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300881 snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200882 s->number, xfer1, buf_ofs);
883 hpi_handle_error(
884 hpi_outstream_write_buf(
885 ds->h_stream, pd, xfer1,
886 &ds->format));
887
888 if (xfer2) {
889 pd = s->runtime->dma_area;
890
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300891 snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200892 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200893 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200894 hpi_handle_error(
895 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200896 ds->h_stream, pd,
897 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200898 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200899 }
900 } else {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300901 snd_printddd("read1, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200902 s->number, xfer1);
903 hpi_handle_error(
904 hpi_instream_read_buf(
905 ds->h_stream,
906 pd, xfer1));
907 if (xfer2) {
908 pd = s->runtime->dma_area;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300909 snd_printddd("read2, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200910 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200911 hpi_handle_error(
912 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300913 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200914 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200915 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200916 }
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300917 /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300918 ds->pcm_buf_host_rw_ofs += xfercount;
919 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200920 snd_pcm_period_elapsed(s);
921 }
922 }
923
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300924 if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200925 add_timer(&dpcm->timer);
926}
927
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300928static void snd_card_asihpi_int_task(unsigned long data)
929{
930 struct hpi_adapter *a = (struct hpi_adapter *)data;
931 struct snd_card_asihpi *asihpi;
932
933 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
934 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
935 if (asihpi->llmode_streampriv)
936 snd_card_asihpi_timer_function(
Kees Cook394ca812017-10-04 17:53:40 -0700937 &asihpi->llmode_streampriv->timer);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300938}
939
940static void snd_card_asihpi_isr(struct hpi_adapter *a)
941{
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 tasklet_schedule(&asihpi->t);
947}
948
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200949/***************************** PLAYBACK OPS ****************/
950static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
951 unsigned int cmd, void *arg)
952{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300953 char name[16];
954 snd_pcm_debug_name(substream, name, sizeof(name));
955 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200956 return snd_pcm_lib_ioctl(substream, cmd, arg);
957}
958
959static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
960 substream)
961{
962 struct snd_pcm_runtime *runtime = substream->runtime;
963 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
964
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200965 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200966
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300967 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300968 dpcm->pcm_buf_host_rw_ofs = 0;
969 dpcm->pcm_buf_dma_ofs = 0;
970 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200971 return 0;
972}
973
974static snd_pcm_uframes_t
975snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
976{
977 struct snd_pcm_runtime *runtime = substream->runtime;
978 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
979 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300980 char name[16];
981 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200982
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300983 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300984 snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200985 return ptr;
986}
987
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300988static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
989 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200990{
991 struct hpi_format hpi_format;
992 u16 format;
993 u16 err;
994 u32 h_control;
995 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +1300996 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200997
998 /* on cards without SRC, must query at valid rate,
999 * maybe set by external sync
1000 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001001 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001002 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1003 HPI_CONTROL_SAMPLECLOCK, &h_control);
1004
1005 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001006 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001007 &sample_rate);
1008
1009 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1010 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001011 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
1012 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001013 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001014 err = hpi_outstream_query_format(h_stream, &hpi_format);
Takashi Iwaia91a0e72018-07-25 23:19:41 +02001015 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001016 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001017 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001018 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001019}
1020
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001021static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1022{
1023 struct snd_pcm_runtime *runtime = substream->runtime;
1024 struct snd_card_asihpi_pcm *dpcm;
1025 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001026 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001027 int err;
1028
1029 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1030 if (dpcm == NULL)
1031 return -ENOMEM;
1032
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001033 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001034 substream->number, &dpcm->h_stream);
1035 hpi_handle_error(err);
1036 if (err)
1037 kfree(dpcm);
1038 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1039 return -EBUSY;
1040 if (err)
1041 return -EIO;
1042
1043 /*? also check ASI5000 samplerate source
1044 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001045 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001046 */
1047
Kees Cook394ca812017-10-04 17:53:40 -07001048 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001049 dpcm->substream = substream;
1050 runtime->private_data = dpcm;
1051 runtime->private_free = snd_card_asihpi_runtime_free;
1052
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001053 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001054 if (!card->hpi->interrupt_mode) {
1055 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1056 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
1057 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1058 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1059 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1060 } else {
1061 size_t pbmin = card->update_interval_frames *
1062 card->out_max_chans;
1063 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1064 snd_card_asihpi_playback.period_bytes_min = pbmin;
1065 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1066 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1067 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
1068 }
1069
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001070 /* snd_card_asihpi_playback.fifo_size = 0; */
1071 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1072 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1073 snd_card_asihpi_playback.formats =
1074 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001075
1076 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1077
1078 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1079 SNDRV_PCM_INFO_DOUBLE |
1080 SNDRV_PCM_INFO_BATCH |
1081 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001082 SNDRV_PCM_INFO_PAUSE |
1083 SNDRV_PCM_INFO_MMAP |
1084 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001085
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001086 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001087 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001088 snd_pcm_set_sync(substream);
1089 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001090
1091 /* struct is copied, so can create initializer dynamically */
1092 runtime->hw = snd_card_asihpi_playback;
1093
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001094 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001095 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1096 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1097 if (err < 0)
1098 return err;
1099
1100 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1101 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001102
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001103 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001104 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001105
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001106 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001107
1108 return 0;
1109}
1110
1111static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1112{
1113 struct snd_pcm_runtime *runtime = substream->runtime;
1114 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1115
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001116 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001117 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001118
1119 return 0;
1120}
1121
Julia Lawall6769e9882016-09-02 00:13:10 +02001122static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001123 .open = snd_card_asihpi_playback_open,
1124 .close = snd_card_asihpi_playback_close,
1125 .ioctl = snd_card_asihpi_playback_ioctl,
1126 .hw_params = snd_card_asihpi_pcm_hw_params,
1127 .hw_free = snd_card_asihpi_hw_free,
1128 .prepare = snd_card_asihpi_playback_prepare,
1129 .trigger = snd_card_asihpi_trigger,
1130 .pointer = snd_card_asihpi_playback_pointer,
1131};
1132
1133/***************************** CAPTURE OPS ****************/
1134static snd_pcm_uframes_t
1135snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1136{
1137 struct snd_pcm_runtime *runtime = substream->runtime;
1138 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001139 char name[16];
1140 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001141
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001142 snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001143 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001144 for the capture position, because those samples aren't yet in
1145 the local buffer available for reading.
1146 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001147 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001148}
1149
1150static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1151 unsigned int cmd, void *arg)
1152{
1153 return snd_pcm_lib_ioctl(substream, cmd, arg);
1154}
1155
1156static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1157{
1158 struct snd_pcm_runtime *runtime = substream->runtime;
1159 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1160
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001161 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001162 dpcm->pcm_buf_host_rw_ofs = 0;
1163 dpcm->pcm_buf_dma_ofs = 0;
1164 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001165
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001166 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001167 return 0;
1168}
1169
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001170static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1171 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001172{
Colin Ian King4593f2d2018-11-16 15:06:32 +00001173 struct hpi_format hpi_format;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001174 u16 format;
1175 u16 err;
1176 u32 h_control;
1177 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001178 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001179
1180 /* on cards without SRC, must query at valid rate,
1181 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001182 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001183 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1184 HPI_CONTROL_SAMPLECLOCK, &h_control);
1185
1186 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001187 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001188 &sample_rate);
1189
1190 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1191 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1192
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001193 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1194 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001195 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001196 err = hpi_instream_query_format(h_stream, &hpi_format);
Takashi Iwaia91a0e72018-07-25 23:19:41 +02001197 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001198 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001199 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001200 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001201}
1202
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001203static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1204{
1205 struct snd_pcm_runtime *runtime = substream->runtime;
1206 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1207 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001208 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001209 int err;
1210
1211 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1212 if (dpcm == NULL)
1213 return -ENOMEM;
1214
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001215 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001216 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001217
1218 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001219 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001220 substream->number, &dpcm->h_stream));
1221 if (err)
1222 kfree(dpcm);
1223 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1224 return -EBUSY;
1225 if (err)
1226 return -EIO;
1227
Kees Cook394ca812017-10-04 17:53:40 -07001228 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001229 dpcm->substream = substream;
1230 runtime->private_data = dpcm;
1231 runtime->private_free = snd_card_asihpi_runtime_free;
1232
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001233 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001234 if (!card->hpi->interrupt_mode) {
1235 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1236 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1237 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1238 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1239 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1240 } else {
1241 size_t pbmin = card->update_interval_frames *
1242 card->out_max_chans;
1243 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1244 snd_card_asihpi_capture.period_bytes_min = pbmin;
1245 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1246 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1247 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
1248 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001249 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001250 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001251 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001252 snd_card_asihpi_capture.formats =
1253 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001254 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001255 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1256 SNDRV_PCM_INFO_MMAP |
1257 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001258
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001259 if (card->support_grouping)
1260 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1261
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001262 runtime->hw = snd_card_asihpi_capture;
1263
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001264 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001265 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1266 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1267 if (err < 0)
1268 return err;
1269
1270 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1271 card->update_interval_frames);
1272 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001273 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001274
1275 snd_pcm_set_sync(substream);
1276
1277 return 0;
1278}
1279
1280static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1281{
1282 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1283
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001284 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001285 return 0;
1286}
1287
Julia Lawall6769e9882016-09-02 00:13:10 +02001288static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001289 .open = snd_card_asihpi_capture_open,
1290 .close = snd_card_asihpi_capture_close,
1291 .ioctl = snd_card_asihpi_capture_ioctl,
1292 .hw_params = snd_card_asihpi_pcm_hw_params,
1293 .hw_free = snd_card_asihpi_hw_free,
1294 .prepare = snd_card_asihpi_capture_prepare,
1295 .trigger = snd_card_asihpi_trigger,
1296 .pointer = snd_card_asihpi_capture_pointer,
1297};
1298
Bill Pembertone23e7a12012-12-06 12:35:10 -05001299static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001300{
1301 struct snd_pcm *pcm;
1302 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001303 u16 num_instreams, num_outstreams, x16;
1304 u32 x32;
1305
1306 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1307 &num_outstreams, &num_instreams,
1308 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001309
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001310 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001311 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001312 if (err < 0)
1313 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001314
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001315 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001316 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1317 &snd_card_asihpi_playback_mmap_ops);
1318 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1319 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001320
1321 pcm->private_data = asihpi;
1322 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001323 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001324
1325 /*? do we want to emulate MMAP for non-BBM cards?
1326 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1327 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1328 snd_dma_pci_data(asihpi->pci),
1329 64*1024, BUFFER_BYTES_MAX);
1330
1331 return 0;
1332}
1333
1334/***************************** MIXER CONTROLS ****************/
1335struct hpi_control {
1336 u32 h_control;
1337 u16 control_type;
1338 u16 src_node_type;
1339 u16 src_node_index;
1340 u16 dst_node_type;
1341 u16 dst_node_index;
1342 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001343 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001344};
1345
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001346static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001347 "invalid",
1348 "AM",
1349 "FM mono",
1350 "TV NTSC-M",
1351 "FM stereo",
1352 "AUX",
1353 "TV PAL BG",
1354 "TV PAL I",
1355 "TV PAL DK",
1356 "TV SECAM",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001357 "TV DAB",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001358};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001359/* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001360compile_time_assert(
1361 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1362 (HPI_TUNER_BAND_LAST+1)),
1363 assert_tuner_band_names_size);
1364
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001365static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001366 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001367 "PCM",
1368 "Line",
1369 "Digital",
1370 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001371 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001372 "Clock",
1373 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001374 "Mic",
1375 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001376 "Analog",
1377 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001378 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001379 "Internal",
1380 "AVB",
1381 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001382};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001383/* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001384compile_time_assert(
1385 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001386 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001387 assert_src_names_size);
1388
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001389static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001390 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001391 "PCM",
1392 "Line",
1393 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001394 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001395 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001396 "Net",
1397 "Analog",
1398 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001399 "AVB",
1400 "Internal",
1401 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001402};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001403/* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001404compile_time_assert(
1405 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001406 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407 assert_dst_names_size);
1408
1409static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1410 struct snd_card_asihpi *asihpi)
1411{
1412 int err;
1413
1414 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1415 if (err < 0)
1416 return err;
1417 else if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13001418 dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001419
1420 return 0;
1421}
1422
1423/* Convert HPI control name and location into ALSA control name */
1424static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1425 struct hpi_control *hpi_ctl,
1426 char *name)
1427{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001428 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001429 memset(snd_control, 0, sizeof(*snd_control));
1430 snd_control->name = hpi_ctl->name;
1431 snd_control->private_value = hpi_ctl->h_control;
1432 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1433 snd_control->index = 0;
1434
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001435 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1436 dir = ""; /* clock is neither capture nor playback */
1437 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001438 dir = "Capture "; /* On or towards a PCM capture destination*/
1439 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1440 (!hpi_ctl->dst_node_type))
1441 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001442 else if (hpi_ctl->src_node_type &&
1443 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001444 (hpi_ctl->dst_node_type))
1445 dir = "Monitor Playback "; /* Between an input and an output */
1446 else
1447 dir = "Playback "; /* PCM Playback source, or output node */
1448
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001449 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001450 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001451 asihpi_src_names[hpi_ctl->src_node_type],
1452 hpi_ctl->src_node_index,
1453 asihpi_dst_names[hpi_ctl->dst_node_type],
1454 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001455 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001456 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001457 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001458 asihpi_dst_names[hpi_ctl->dst_node_type],
1459 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001460 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001461 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001462 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001463 asihpi_src_names[hpi_ctl->src_node_type],
1464 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001465 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001466 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001467 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1468 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001469}
1470
1471/*------------------------------------------------------------
1472 Volume controls
1473 ------------------------------------------------------------*/
1474#define VOL_STEP_mB 1
1475static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_info *uinfo)
1477{
1478 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001479 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001480 u16 err;
1481 /* native gains are in millibels */
1482 short min_gain_mB;
1483 short max_gain_mB;
1484 short step_gain_mB;
1485
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001486 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001487 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1488 if (err) {
1489 max_gain_mB = 0;
1490 min_gain_mB = -10000;
1491 step_gain_mB = VOL_STEP_mB;
1492 }
1493
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001494 err = hpi_meter_query_channels(h_control, &count);
1495 if (err)
1496 count = HPI_MAX_CHANNELS;
1497
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001498 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001499 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001500 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1501 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1502 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1503 return 0;
1504}
1505
1506static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1507 struct snd_ctl_elem_value *ucontrol)
1508{
1509 u32 h_control = kcontrol->private_value;
1510 short an_gain_mB[HPI_MAX_CHANNELS];
1511
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001512 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001513 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1514 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1515
1516 return 0;
1517}
1518
1519static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1520 struct snd_ctl_elem_value *ucontrol)
1521{
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001522 u32 h_control = kcontrol->private_value;
1523 short an_gain_mB[HPI_MAX_CHANNELS];
1524
1525 an_gain_mB[0] =
1526 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1527 an_gain_mB[1] =
1528 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1529 /* change = asihpi->mixer_volume[addr][0] != left ||
1530 asihpi->mixer_volume[addr][1] != right;
1531 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001532 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Hariprasad Kelambb26e0c2019-07-05 08:27:33 +05301533 return 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001534}
1535
1536static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1537
Takashi Iwai000477a2011-07-22 07:57:44 +02001538#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001539
1540static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1542{
1543 u32 h_control = kcontrol->private_value;
1544 u32 mute;
1545
1546 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1547 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1548
1549 return 0;
1550}
1551
1552static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1553 struct snd_ctl_elem_value *ucontrol)
1554{
1555 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001556 /* HPI currently only supports all or none muting of multichannel volume
1557 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1558 */
1559 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1560 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
Hariprasad Kelambb26e0c2019-07-05 08:27:33 +05301561 return 1;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001562}
1563
Bill Pembertone23e7a12012-12-06 12:35:10 -05001564static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1565 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001566{
1567 struct snd_card *card = asihpi->card;
1568 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001569 int err;
1570 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001571
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001572 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001573 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1574 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1575 snd_control.info = snd_asihpi_volume_info;
1576 snd_control.get = snd_asihpi_volume_get;
1577 snd_control.put = snd_asihpi_volume_put;
1578 snd_control.tlv.p = db_scale_100;
1579
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001580 err = ctl_add(card, &snd_control, asihpi);
1581 if (err)
1582 return err;
1583
1584 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1585 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1586 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1587 snd_control.info = snd_asihpi_volume_mute_info;
1588 snd_control.get = snd_asihpi_volume_mute_get;
1589 snd_control.put = snd_asihpi_volume_mute_put;
1590 err = ctl_add(card, &snd_control, asihpi);
1591 }
1592 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001593}
1594
1595/*------------------------------------------------------------
1596 Level controls
1597 ------------------------------------------------------------*/
1598static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1599 struct snd_ctl_elem_info *uinfo)
1600{
1601 u32 h_control = kcontrol->private_value;
1602 u16 err;
1603 short min_gain_mB;
1604 short max_gain_mB;
1605 short step_gain_mB;
1606
1607 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001608 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001609 &max_gain_mB, &step_gain_mB);
1610 if (err) {
1611 max_gain_mB = 2400;
1612 min_gain_mB = -1000;
1613 step_gain_mB = 100;
1614 }
1615
1616 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1617 uinfo->count = 2;
1618 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1619 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1620 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1621 return 0;
1622}
1623
1624static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1625 struct snd_ctl_elem_value *ucontrol)
1626{
1627 u32 h_control = kcontrol->private_value;
1628 short an_gain_mB[HPI_MAX_CHANNELS];
1629
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001630 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001631 ucontrol->value.integer.value[0] =
1632 an_gain_mB[0] / HPI_UNITS_PER_dB;
1633 ucontrol->value.integer.value[1] =
1634 an_gain_mB[1] / HPI_UNITS_PER_dB;
1635
1636 return 0;
1637}
1638
1639static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1640 struct snd_ctl_elem_value *ucontrol)
1641{
1642 int change;
1643 u32 h_control = kcontrol->private_value;
1644 short an_gain_mB[HPI_MAX_CHANNELS];
1645
1646 an_gain_mB[0] =
1647 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1648 an_gain_mB[1] =
1649 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1650 /* change = asihpi->mixer_level[addr][0] != left ||
1651 asihpi->mixer_level[addr][1] != right;
1652 */
1653 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001654 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001655 return change;
1656}
1657
1658static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1659
Bill Pembertone23e7a12012-12-06 12:35:10 -05001660static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1661 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001662{
1663 struct snd_card *card = asihpi->card;
1664 struct snd_kcontrol_new snd_control;
1665
1666 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001667 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001668 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1669 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1670 snd_control.info = snd_asihpi_level_info;
1671 snd_control.get = snd_asihpi_level_get;
1672 snd_control.put = snd_asihpi_level_put;
1673 snd_control.tlv.p = db_scale_level;
1674
1675 return ctl_add(card, &snd_control, asihpi);
1676}
1677
1678/*------------------------------------------------------------
1679 AESEBU controls
1680 ------------------------------------------------------------*/
1681
1682/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001683static const char * const asihpi_aesebu_format_names[] = {
1684 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001685
1686static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1687 struct snd_ctl_elem_info *uinfo)
1688{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001689 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001690}
1691
1692static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1693 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001694 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001695{
1696 u32 h_control = kcontrol->private_value;
1697 u16 source, err;
1698
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001699 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001700
1701 /* default to N/A */
1702 ucontrol->value.enumerated.item[0] = 0;
1703 /* return success but set the control to N/A */
1704 if (err)
1705 return 0;
1706 if (source == HPI_AESEBU_FORMAT_SPDIF)
1707 ucontrol->value.enumerated.item[0] = 1;
1708 if (source == HPI_AESEBU_FORMAT_AESEBU)
1709 ucontrol->value.enumerated.item[0] = 2;
1710
1711 return 0;
1712}
1713
1714static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1715 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001716 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001717{
1718 u32 h_control = kcontrol->private_value;
1719
1720 /* default to S/PDIF */
1721 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1722
1723 if (ucontrol->value.enumerated.item[0] == 1)
1724 source = HPI_AESEBU_FORMAT_SPDIF;
1725 if (ucontrol->value.enumerated.item[0] == 2)
1726 source = HPI_AESEBU_FORMAT_AESEBU;
1727
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001728 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001729 return -EINVAL;
1730
1731 return 1;
1732}
1733
1734static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1735 struct snd_ctl_elem_value *ucontrol) {
1736 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001737 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001738}
1739
1740static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1741 struct snd_ctl_elem_value *ucontrol) {
1742 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001743 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001744}
1745
1746static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1747 struct snd_ctl_elem_info *uinfo)
1748{
1749 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1750 uinfo->count = 1;
1751
1752 uinfo->value.integer.min = 0;
1753 uinfo->value.integer.max = 0X1F;
1754 uinfo->value.integer.step = 1;
1755
1756 return 0;
1757}
1758
1759static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_value *ucontrol) {
1761
1762 u32 h_control = kcontrol->private_value;
1763 u16 status;
1764
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001765 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1766 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001767 ucontrol->value.integer.value[0] = status;
1768 return 0;
1769}
1770
Bill Pembertone23e7a12012-12-06 12:35:10 -05001771static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1772 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001773{
1774 struct snd_card *card = asihpi->card;
1775 struct snd_kcontrol_new snd_control;
1776
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001777 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001778 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1779 snd_control.info = snd_asihpi_aesebu_format_info;
1780 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1781 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1782
1783
1784 if (ctl_add(card, &snd_control, asihpi) < 0)
1785 return -EINVAL;
1786
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001787 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001788 snd_control.access =
1789 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1790 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1791 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1792
1793 return ctl_add(card, &snd_control, asihpi);
1794}
1795
1796static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1797 struct snd_ctl_elem_value *ucontrol) {
1798 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001799 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001800}
1801
1802static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1803 struct snd_ctl_elem_value *ucontrol) {
1804 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001805 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001806}
1807
1808
Bill Pembertone23e7a12012-12-06 12:35:10 -05001809static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1810 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001811{
1812 struct snd_card *card = asihpi->card;
1813 struct snd_kcontrol_new snd_control;
1814
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001815 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001816 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1817 snd_control.info = snd_asihpi_aesebu_format_info;
1818 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1819 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1820
1821 return ctl_add(card, &snd_control, asihpi);
1822}
1823
1824/*------------------------------------------------------------
1825 Tuner controls
1826 ------------------------------------------------------------*/
1827
1828/* Gain */
1829
1830static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1831 struct snd_ctl_elem_info *uinfo)
1832{
1833 u32 h_control = kcontrol->private_value;
1834 u16 err;
1835 short idx;
1836 u16 gain_range[3];
1837
1838 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001839 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001840 idx, &gain_range[idx]);
1841 if (err != 0)
1842 return err;
1843 }
1844
1845 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1846 uinfo->count = 1;
1847 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1848 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1849 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1850 return 0;
1851}
1852
1853static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1854 struct snd_ctl_elem_value *ucontrol)
1855{
1856 /*
1857 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1858 */
1859 u32 h_control = kcontrol->private_value;
1860 short gain;
1861
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001862 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001863 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1864
1865 return 0;
1866}
1867
1868static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1869 struct snd_ctl_elem_value *ucontrol)
1870{
1871 /*
1872 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1873 */
1874 u32 h_control = kcontrol->private_value;
1875 short gain;
1876
1877 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001878 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001879
1880 return 1;
1881}
1882
1883/* Band */
1884
1885static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1886 u16 *band_list, u32 len) {
1887 u32 h_control = kcontrol->private_value;
1888 u16 err = 0;
1889 u32 i;
1890
1891 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001892 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001893 h_control, i, &band_list[i]);
1894 if (err != 0)
1895 break;
1896 }
1897
1898 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1899 return -EIO;
1900
1901 return i;
1902}
1903
1904static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1905 struct snd_ctl_elem_info *uinfo)
1906{
1907 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1908 int num_bands = 0;
1909
1910 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1911 HPI_TUNER_BAND_LAST);
1912
1913 if (num_bands < 0)
1914 return num_bands;
1915
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001916 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001917}
1918
1919static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1920 struct snd_ctl_elem_value *ucontrol)
1921{
1922 u32 h_control = kcontrol->private_value;
1923 /*
1924 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1925 */
1926 u16 band, idx;
1927 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1928 u32 num_bands = 0;
1929
1930 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1931 HPI_TUNER_BAND_LAST);
1932
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001933 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001934
1935 ucontrol->value.enumerated.item[0] = -1;
1936 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1937 if (tuner_bands[idx] == band) {
1938 ucontrol->value.enumerated.item[0] = idx;
1939 break;
1940 }
1941
1942 return 0;
1943}
1944
1945static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1946 struct snd_ctl_elem_value *ucontrol)
1947{
1948 /*
1949 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1950 */
1951 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001952 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001953 u16 band;
1954 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1955 u32 num_bands = 0;
1956
1957 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1958 HPI_TUNER_BAND_LAST);
1959
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001960 idx = ucontrol->value.enumerated.item[0];
1961 if (idx >= ARRAY_SIZE(tuner_bands))
1962 idx = ARRAY_SIZE(tuner_bands) - 1;
1963 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001964 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001965
1966 return 1;
1967}
1968
1969/* Freq */
1970
1971static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1972 struct snd_ctl_elem_info *uinfo)
1973{
1974 u32 h_control = kcontrol->private_value;
1975 u16 err;
1976 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1977 u16 num_bands = 0, band_iter, idx;
1978 u32 freq_range[3], temp_freq_range[3];
1979
1980 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1981 HPI_TUNER_BAND_LAST);
1982
1983 freq_range[0] = INT_MAX;
1984 freq_range[1] = 0;
1985 freq_range[2] = INT_MAX;
1986
1987 for (band_iter = 0; band_iter < num_bands; band_iter++) {
1988 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001989 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001990 idx, tuner_bands[band_iter],
1991 &temp_freq_range[idx]);
1992 if (err != 0)
1993 return err;
1994 }
1995
1996 /* skip band with bogus stepping */
1997 if (temp_freq_range[2] <= 0)
1998 continue;
1999
2000 if (temp_freq_range[0] < freq_range[0])
2001 freq_range[0] = temp_freq_range[0];
2002 if (temp_freq_range[1] > freq_range[1])
2003 freq_range[1] = temp_freq_range[1];
2004 if (temp_freq_range[2] < freq_range[2])
2005 freq_range[2] = temp_freq_range[2];
2006 }
2007
2008 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2009 uinfo->count = 1;
2010 uinfo->value.integer.min = ((int)freq_range[0]);
2011 uinfo->value.integer.max = ((int)freq_range[1]);
2012 uinfo->value.integer.step = ((int)freq_range[2]);
2013 return 0;
2014}
2015
2016static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
2018{
2019 u32 h_control = kcontrol->private_value;
2020 u32 freq;
2021
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002022 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002023 ucontrol->value.integer.value[0] = freq;
2024
2025 return 0;
2026}
2027
2028static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2029 struct snd_ctl_elem_value *ucontrol)
2030{
2031 u32 h_control = kcontrol->private_value;
2032 u32 freq;
2033
2034 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002035 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002036
2037 return 1;
2038}
2039
2040/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002041static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2042 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002043{
2044 struct snd_card *card = asihpi->card;
2045 struct snd_kcontrol_new snd_control;
2046
2047 snd_control.private_value = hpi_ctl->h_control;
2048 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2049
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002050 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002051 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002052 snd_control.info = snd_asihpi_tuner_gain_info;
2053 snd_control.get = snd_asihpi_tuner_gain_get;
2054 snd_control.put = snd_asihpi_tuner_gain_put;
2055
2056 if (ctl_add(card, &snd_control, asihpi) < 0)
2057 return -EINVAL;
2058 }
2059
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002060 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002061 snd_control.info = snd_asihpi_tuner_band_info;
2062 snd_control.get = snd_asihpi_tuner_band_get;
2063 snd_control.put = snd_asihpi_tuner_band_put;
2064
2065 if (ctl_add(card, &snd_control, asihpi) < 0)
2066 return -EINVAL;
2067
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002068 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002069 snd_control.info = snd_asihpi_tuner_freq_info;
2070 snd_control.get = snd_asihpi_tuner_freq_get;
2071 snd_control.put = snd_asihpi_tuner_freq_put;
2072
2073 return ctl_add(card, &snd_control, asihpi);
2074}
2075
2076/*------------------------------------------------------------
2077 Meter controls
2078 ------------------------------------------------------------*/
2079static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2080 struct snd_ctl_elem_info *uinfo)
2081{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002082 u32 h_control = kcontrol->private_value;
2083 u32 count;
2084 u16 err;
2085 err = hpi_meter_query_channels(h_control, &count);
2086 if (err)
2087 count = HPI_MAX_CHANNELS;
2088
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002089 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002090 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002091 uinfo->value.integer.min = 0;
2092 uinfo->value.integer.max = 0x7FFFFFFF;
2093 return 0;
2094}
2095
2096/* linear values for 10dB steps */
2097static int log2lin[] = {
2098 0x7FFFFFFF, /* 0dB */
2099 679093956,
2100 214748365,
2101 67909396,
2102 21474837,
2103 6790940,
2104 2147484, /* -60dB */
2105 679094,
2106 214748, /* -80 */
2107 67909,
2108 21475, /* -100 */
2109 6791,
2110 2147,
2111 679,
2112 214,
2113 68,
2114 21,
2115 7,
2116 2
2117};
2118
2119static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2120 struct snd_ctl_elem_value *ucontrol)
2121{
2122 u32 h_control = kcontrol->private_value;
2123 short an_gain_mB[HPI_MAX_CHANNELS], i;
2124 u16 err;
2125
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002126 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002127
2128 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2129 if (err) {
2130 ucontrol->value.integer.value[i] = 0;
2131 } else if (an_gain_mB[i] >= 0) {
2132 ucontrol->value.integer.value[i] =
2133 an_gain_mB[i] << 16;
2134 } else {
2135 /* -ve is log value in millibels < -60dB,
2136 * convert to (roughly!) linear,
2137 */
2138 ucontrol->value.integer.value[i] =
2139 log2lin[an_gain_mB[i] / -1000];
2140 }
2141 }
2142 return 0;
2143}
2144
Bill Pembertone23e7a12012-12-06 12:35:10 -05002145static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2146 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002147{
2148 struct snd_card *card = asihpi->card;
2149 struct snd_kcontrol_new snd_control;
2150
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002151 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002152 snd_control.access =
2153 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2154 snd_control.info = snd_asihpi_meter_info;
2155 snd_control.get = snd_asihpi_meter_get;
2156
2157 snd_control.index = subidx;
2158
2159 return ctl_add(card, &snd_control, asihpi);
2160}
2161
2162/*------------------------------------------------------------
2163 Multiplexer controls
2164 ------------------------------------------------------------*/
2165static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2166{
2167 u32 h_control = snd_control->private_value;
2168 struct hpi_control hpi_ctl;
2169 int s, err;
2170 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002171 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002172 &hpi_ctl.
2173 src_node_type,
2174 &hpi_ctl.
2175 src_node_index);
2176 if (err)
2177 break;
2178 }
2179 return s;
2180}
2181
2182static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2183 struct snd_ctl_elem_info *uinfo)
2184{
2185 int err;
2186 u16 src_node_type, src_node_index;
2187 u32 h_control = kcontrol->private_value;
2188
2189 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2190 uinfo->count = 1;
2191 uinfo->value.enumerated.items =
2192 snd_card_asihpi_mux_count_sources(kcontrol);
2193
2194 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2195 uinfo->value.enumerated.item =
2196 uinfo->value.enumerated.items - 1;
2197
2198 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002199 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002200 uinfo->value.enumerated.item,
2201 &src_node_type, &src_node_index);
2202
2203 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002204 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002205 src_node_index);
2206 return 0;
2207}
2208
2209static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2210 struct snd_ctl_elem_value *ucontrol)
2211{
2212 u32 h_control = kcontrol->private_value;
2213 u16 source_type, source_index;
2214 u16 src_node_type, src_node_index;
2215 int s;
2216
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002217 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002218 &source_type, &source_index));
2219 /* Should cache this search result! */
2220 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002221 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002222 &src_node_type, &src_node_index))
2223 break;
2224
2225 if ((source_type == src_node_type)
2226 && (source_index == src_node_index)) {
2227 ucontrol->value.enumerated.item[0] = s;
2228 return 0;
2229 }
2230 }
2231 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002232 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002233 h_control, source_type, source_index);
2234 ucontrol->value.enumerated.item[0] = 0;
2235 return 0;
2236}
2237
2238static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2239 struct snd_ctl_elem_value *ucontrol)
2240{
2241 int change;
2242 u32 h_control = kcontrol->private_value;
2243 u16 source_type, source_index;
2244 u16 e;
2245
2246 change = 1;
2247
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002248 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002249 ucontrol->value.enumerated.item[0],
2250 &source_type, &source_index);
2251 if (!e)
2252 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002253 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002254 source_type, source_index));
2255 return change;
2256}
2257
2258
Bill Pembertone23e7a12012-12-06 12:35:10 -05002259static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2260 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002261{
2262 struct snd_card *card = asihpi->card;
2263 struct snd_kcontrol_new snd_control;
2264
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002265 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002266 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2267 snd_control.info = snd_asihpi_mux_info;
2268 snd_control.get = snd_asihpi_mux_get;
2269 snd_control.put = snd_asihpi_mux_put;
2270
2271 return ctl_add(card, &snd_control, asihpi);
2272
2273}
2274
2275/*------------------------------------------------------------
2276 Channel mode controls
2277 ------------------------------------------------------------*/
2278static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2279 struct snd_ctl_elem_info *uinfo)
2280{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002281 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2282 "invalid",
2283 "Normal", "Swap",
2284 "From Left", "From Right",
2285 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002286 };
2287
2288 u32 h_control = kcontrol->private_value;
2289 u16 mode;
2290 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002291 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002292 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002293
2294 /* HPI channel mode values can be from 1 to 6
2295 Some adapters only support a contiguous subset
2296 */
2297 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002298 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002299 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002300 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002301 valid_modes++;
2302 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002303
Takashi Iwai74eeb142012-01-09 18:26:05 +01002304 if (!valid_modes)
2305 return -EINVAL;
2306
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002307 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002308}
2309
2310static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2311 struct snd_ctl_elem_value *ucontrol)
2312{
2313 u32 h_control = kcontrol->private_value;
2314 u16 mode;
2315
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002316 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002317 mode = 1;
2318
2319 ucontrol->value.enumerated.item[0] = mode - 1;
2320
2321 return 0;
2322}
2323
2324static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2325 struct snd_ctl_elem_value *ucontrol)
2326{
2327 int change;
2328 u32 h_control = kcontrol->private_value;
2329
2330 change = 1;
2331
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002332 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002333 ucontrol->value.enumerated.item[0] + 1));
2334 return change;
2335}
2336
2337
Bill Pembertone23e7a12012-12-06 12:35:10 -05002338static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2339 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002340{
2341 struct snd_card *card = asihpi->card;
2342 struct snd_kcontrol_new snd_control;
2343
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002344 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002345 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2346 snd_control.info = snd_asihpi_cmode_info;
2347 snd_control.get = snd_asihpi_cmode_get;
2348 snd_control.put = snd_asihpi_cmode_put;
2349
2350 return ctl_add(card, &snd_control, asihpi);
2351}
2352
2353/*------------------------------------------------------------
2354 Sampleclock source controls
2355 ------------------------------------------------------------*/
Krzysztof Kozlowski46d212c2015-03-24 11:48:24 +01002356static const char * const sampleclock_sources[] = {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002357 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2358 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002359 "Prev Module", "BLU-Link",
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002360 "Digital2", "Digital3", "Digital4", "Digital5",
2361 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002362
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002363 /* Number of strings must match expected enumerated values */
2364 compile_time_assert(
2365 (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
2366 assert_sampleclock_sources_size);
2367
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002368static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2369 struct snd_ctl_elem_info *uinfo)
2370{
2371 struct snd_card_asihpi *asihpi =
2372 (struct snd_card_asihpi *)(kcontrol->private_data);
2373 struct clk_cache *clkcache = &asihpi->cc;
2374 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2375 uinfo->count = 1;
2376 uinfo->value.enumerated.items = clkcache->count;
2377
2378 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2379 uinfo->value.enumerated.item =
2380 uinfo->value.enumerated.items - 1;
2381
2382 strcpy(uinfo->value.enumerated.name,
2383 clkcache->s[uinfo->value.enumerated.item].name);
2384 return 0;
2385}
2386
2387static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2388 struct snd_ctl_elem_value *ucontrol)
2389{
2390 struct snd_card_asihpi *asihpi =
2391 (struct snd_card_asihpi *)(kcontrol->private_data);
2392 struct clk_cache *clkcache = &asihpi->cc;
2393 u32 h_control = kcontrol->private_value;
2394 u16 source, srcindex = 0;
2395 int i;
2396
2397 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002398 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002399 source = 0;
2400
2401 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002402 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002403 srcindex = 0;
2404
2405 for (i = 0; i < clkcache->count; i++)
2406 if ((clkcache->s[i].source == source) &&
2407 (clkcache->s[i].index == srcindex))
2408 break;
2409
2410 ucontrol->value.enumerated.item[0] = i;
2411
2412 return 0;
2413}
2414
2415static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2416 struct snd_ctl_elem_value *ucontrol)
2417{
2418 struct snd_card_asihpi *asihpi =
2419 (struct snd_card_asihpi *)(kcontrol->private_data);
2420 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002421 unsigned int item;
2422 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002423 u32 h_control = kcontrol->private_value;
2424
2425 change = 1;
2426 item = ucontrol->value.enumerated.item[0];
2427 if (item >= clkcache->count)
2428 item = clkcache->count-1;
2429
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002430 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002431 h_control, clkcache->s[item].source));
2432
2433 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002434 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002435 h_control, clkcache->s[item].index));
2436 return change;
2437}
2438
2439/*------------------------------------------------------------
2440 Clkrate controls
2441 ------------------------------------------------------------*/
2442/* Need to change this to enumerated control with list of rates */
2443static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2444 struct snd_ctl_elem_info *uinfo)
2445{
2446 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2447 uinfo->count = 1;
2448 uinfo->value.integer.min = 8000;
2449 uinfo->value.integer.max = 192000;
2450 uinfo->value.integer.step = 100;
2451
2452 return 0;
2453}
2454
2455static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2456 struct snd_ctl_elem_value *ucontrol)
2457{
2458 u32 h_control = kcontrol->private_value;
2459 u32 rate;
2460 u16 e;
2461
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002462 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002463 if (!e)
2464 ucontrol->value.integer.value[0] = rate;
2465 else
2466 ucontrol->value.integer.value[0] = 0;
2467 return 0;
2468}
2469
2470static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2471 struct snd_ctl_elem_value *ucontrol)
2472{
2473 int change;
2474 u32 h_control = kcontrol->private_value;
2475
2476 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2477 asihpi->mixer_clkrate[addr][1] != right;
2478 */
2479 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002480 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002481 ucontrol->value.integer.value[0]));
2482 return change;
2483}
2484
2485static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2486 struct snd_ctl_elem_info *uinfo)
2487{
2488 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2489 uinfo->count = 1;
2490 uinfo->value.integer.min = 8000;
2491 uinfo->value.integer.max = 192000;
2492 uinfo->value.integer.step = 100;
2493
2494 return 0;
2495}
2496
2497static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2498 struct snd_ctl_elem_value *ucontrol)
2499{
2500 u32 h_control = kcontrol->private_value;
2501 u32 rate;
2502 u16 e;
2503
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002504 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002505 if (!e)
2506 ucontrol->value.integer.value[0] = rate;
2507 else
2508 ucontrol->value.integer.value[0] = 0;
2509 return 0;
2510}
2511
Bill Pembertone23e7a12012-12-06 12:35:10 -05002512static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2513 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002514{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002515 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002516 struct snd_kcontrol_new snd_control;
2517
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002518 struct clk_cache *clkcache;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002519 u32 hSC = hpi_ctl->h_control;
2520 int has_aes_in = 0;
2521 int i, j;
2522 u16 source;
2523
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002524 if (snd_BUG_ON(!asihpi))
2525 return -EINVAL;
2526 card = asihpi->card;
2527 clkcache = &asihpi->cc;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002528 snd_control.private_value = hpi_ctl->h_control;
2529
2530 clkcache->has_local = 0;
2531
2532 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002533 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002534 i, &source))
2535 break;
2536 clkcache->s[i].source = source;
2537 clkcache->s[i].index = 0;
2538 clkcache->s[i].name = sampleclock_sources[source];
2539 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2540 has_aes_in = 1;
2541 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2542 clkcache->has_local = 1;
2543 }
2544 if (has_aes_in)
2545 /* already will have picked up index 0 above */
2546 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002547 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002548 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2549 &source))
2550 break;
2551 clkcache->s[i].source =
2552 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2553 clkcache->s[i].index = j;
2554 clkcache->s[i].name = sampleclock_sources[
2555 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2556 i++;
2557 }
2558 clkcache->count = i;
2559
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002560 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002561 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2562 snd_control.info = snd_asihpi_clksrc_info;
2563 snd_control.get = snd_asihpi_clksrc_get;
2564 snd_control.put = snd_asihpi_clksrc_put;
2565 if (ctl_add(card, &snd_control, asihpi) < 0)
2566 return -EINVAL;
2567
2568
2569 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002570 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002571 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2572 snd_control.info = snd_asihpi_clklocal_info;
2573 snd_control.get = snd_asihpi_clklocal_get;
2574 snd_control.put = snd_asihpi_clklocal_put;
2575
2576
2577 if (ctl_add(card, &snd_control, asihpi) < 0)
2578 return -EINVAL;
2579 }
2580
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002581 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002582 snd_control.access =
2583 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2584 snd_control.info = snd_asihpi_clkrate_info;
2585 snd_control.get = snd_asihpi_clkrate_get;
2586
2587 return ctl_add(card, &snd_control, asihpi);
2588}
2589/*------------------------------------------------------------
2590 Mixer
2591 ------------------------------------------------------------*/
2592
Bill Pembertone23e7a12012-12-06 12:35:10 -05002593static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002594{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002595 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002596 unsigned int idx = 0;
2597 unsigned int subindex = 0;
2598 int err;
2599 struct hpi_control hpi_ctl, prev_ctl;
2600
2601 if (snd_BUG_ON(!asihpi))
2602 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002603 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002604 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002605
2606 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002607 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002608 &asihpi->h_mixer);
2609 hpi_handle_error(err);
2610 if (err)
2611 return -err;
2612
Takashi Iwai21896bc2010-06-02 12:08:37 +02002613 memset(&prev_ctl, 0, sizeof(prev_ctl));
2614 prev_ctl.control_type = -1;
2615
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002616 for (idx = 0; idx < 2000; idx++) {
2617 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002618 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002619 idx,
2620 &hpi_ctl.src_node_type,
2621 &hpi_ctl.src_node_index,
2622 &hpi_ctl.dst_node_type,
2623 &hpi_ctl.dst_node_index,
2624 &hpi_ctl.control_type,
2625 &hpi_ctl.h_control);
2626 if (err) {
2627 if (err == HPI_ERROR_CONTROL_DISABLED) {
2628 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002629 dev_info(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002630 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002631 idx);
2632 continue;
2633 } else
2634 break;
2635
2636 }
2637
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002638 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2639 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002640
2641 /* ASI50xx in SSX mode has multiple meters on the same node.
2642 Use subindex to create distinct ALSA controls
2643 for any duplicated controls.
2644 */
2645 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2646 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2647 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2648 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2649 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2650 subindex++;
2651 else
2652 subindex = 0;
2653
2654 prev_ctl = hpi_ctl;
2655
2656 switch (hpi_ctl.control_type) {
2657 case HPI_CONTROL_VOLUME:
2658 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2659 break;
2660 case HPI_CONTROL_LEVEL:
2661 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2662 break;
2663 case HPI_CONTROL_MULTIPLEXER:
2664 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2665 break;
2666 case HPI_CONTROL_CHANNEL_MODE:
2667 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2668 break;
2669 case HPI_CONTROL_METER:
2670 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2671 break;
2672 case HPI_CONTROL_SAMPLECLOCK:
2673 err = snd_asihpi_sampleclock_add(
2674 asihpi, &hpi_ctl);
2675 break;
2676 case HPI_CONTROL_CONNECTION: /* ignore these */
2677 continue;
2678 case HPI_CONTROL_TUNER:
2679 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2680 break;
2681 case HPI_CONTROL_AESEBU_TRANSMITTER:
2682 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2683 break;
2684 case HPI_CONTROL_AESEBU_RECEIVER:
2685 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2686 break;
2687 case HPI_CONTROL_VOX:
2688 case HPI_CONTROL_BITSTREAM:
2689 case HPI_CONTROL_MICROPHONE:
2690 case HPI_CONTROL_PARAMETRIC_EQ:
2691 case HPI_CONTROL_COMPANDER:
2692 default:
2693 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002694 dev_info(&asihpi->pci->dev,
2695 "Untranslated HPI Control (%d) %d %d %d %d %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002696 idx,
2697 hpi_ctl.control_type,
2698 hpi_ctl.src_node_type,
2699 hpi_ctl.src_node_index,
2700 hpi_ctl.dst_node_type,
2701 hpi_ctl.dst_node_index);
2702 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002703 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002704 if (err < 0)
2705 return err;
2706 }
2707 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2708 hpi_handle_error(err);
2709
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002710 dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002711
2712 return 0;
2713}
2714
2715/*------------------------------------------------------------
2716 /proc interface
2717 ------------------------------------------------------------*/
2718
2719static void
2720snd_asihpi_proc_read(struct snd_info_entry *entry,
2721 struct snd_info_buffer *buffer)
2722{
2723 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002724 u32 h_control;
2725 u32 rate = 0;
2726 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002727
2728 u16 num_outstreams;
2729 u16 num_instreams;
2730 u16 version;
2731 u32 serial_number;
2732 u16 type;
2733
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002734 int err;
2735
2736 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002737
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002738 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2739 &num_outstreams, &num_instreams,
2740 &version, &serial_number, &type));
2741
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002742 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002743 "Adapter type ASI%4X\nHardware Index %d\n"
2744 "%d outstreams\n%d instreams\n",
2745 type, asihpi->hpi->adapter->index,
2746 num_outstreams, num_instreams);
2747
2748 snd_iprintf(buffer,
2749 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2750 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002751 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2752
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002753 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002754 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2755 HPI_CONTROL_SAMPLECLOCK, &h_control);
2756
2757 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002758 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002759 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002760
2761 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002762 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002763 rate, sampleclock_sources[source]);
2764 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002765}
2766
Bill Pembertone23e7a12012-12-06 12:35:10 -05002767static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002768{
Takashi Iwai47f27692019-02-04 16:01:39 +01002769 snd_card_ro_proc_new(asihpi->card, "info", asihpi,
2770 snd_asihpi_proc_read);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002771}
2772
2773/*------------------------------------------------------------
2774 HWDEP
2775 ------------------------------------------------------------*/
2776
2777static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2778{
2779 if (enable_hpi_hwdep)
2780 return 0;
2781 else
2782 return -ENODEV;
2783
2784}
2785
2786static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2787{
2788 if (enable_hpi_hwdep)
2789 return asihpi_hpi_release(file);
2790 else
2791 return -ENODEV;
2792}
2793
2794static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2795 unsigned int cmd, unsigned long arg)
2796{
2797 if (enable_hpi_hwdep)
2798 return asihpi_hpi_ioctl(file, cmd, arg);
2799 else
2800 return -ENODEV;
2801}
2802
2803
2804/* results in /dev/snd/hwC#D0 file for each card with index #
2805 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2806*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002807static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002808{
2809 struct snd_hwdep *hw;
2810 int err;
2811
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002812 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2813 if (err < 0)
2814 return err;
2815 strcpy(hw->name, "asihpi (HPI)");
2816 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2817 hw->ops.open = snd_asihpi_hpi_open;
2818 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2819 hw->ops.release = snd_asihpi_hpi_release;
2820 hw->private_data = asihpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002821 return 0;
2822}
2823
2824/*------------------------------------------------------------
2825 CARD
2826 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002827static int snd_asihpi_probe(struct pci_dev *pci_dev,
2828 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002829{
2830 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002831 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002832 struct snd_card *card;
2833 struct snd_card_asihpi *asihpi;
2834
2835 u32 h_control;
2836 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002837 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002838
2839 static int dev;
2840 if (dev >= SNDRV_CARDS)
2841 return -ENODEV;
2842
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002843 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002844 if (!enable[dev]) {
2845 dev++;
2846 return -ENOENT;
2847 }
2848
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002849 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002850 err = asihpi_adapter_probe(pci_dev, pci_id);
2851 if (err < 0)
2852 return err;
2853
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002854 hpi = pci_get_drvdata(pci_dev);
2855 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002856 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002857 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2858 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002859 if (err < 0) {
2860 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002861 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2862 THIS_MODULE, sizeof(struct snd_card_asihpi),
2863 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002864 if (err < 0)
2865 return err;
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002866 dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002867 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002868 }
2869
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002870 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002871 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002872 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002873 asihpi->hpi = hpi;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002874 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002875
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002876 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002877 HPI_ADAPTER_PROPERTY_CAPS1,
2878 NULL, &asihpi->support_grouping);
2879 if (err)
2880 asihpi->support_grouping = 0;
2881
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002882 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002883 HPI_ADAPTER_PROPERTY_CAPS2,
2884 &asihpi->support_mrx, NULL);
2885 if (err)
2886 asihpi->support_mrx = 0;
2887
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002888 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002889 HPI_ADAPTER_PROPERTY_INTERVAL,
2890 NULL, &asihpi->update_interval_frames);
2891 if (err)
2892 asihpi->update_interval_frames = 512;
2893
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002894 if (hpi->interrupt_mode) {
2895 asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
2896 asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
2897 tasklet_init(&asihpi->t, snd_card_asihpi_int_task,
2898 (unsigned long)hpi);
2899 hpi->interrupt_callback = snd_card_asihpi_isr;
2900 } else {
2901 asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
2902 asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
2903 }
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002904
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002905 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002906 0, &h_stream));
2907
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002908 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002909 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002910
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002911 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002912
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002913 if (!asihpi->can_dma)
2914 asihpi->update_interval_frames *= 2;
2915
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002916 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002917 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2918 &asihpi->in_max_chans, &asihpi->out_max_chans);
2919 if (err) {
2920 asihpi->in_max_chans = 2;
2921 asihpi->out_max_chans = 2;
2922 }
2923
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002924 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2925 asihpi->out_min_chans = asihpi->out_max_chans;
2926 asihpi->in_min_chans = asihpi->in_max_chans;
2927 asihpi->support_grouping = 0;
2928 } else {
2929 asihpi->out_min_chans = 1;
2930 asihpi->in_min_chans = 1;
2931 }
2932
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002933 dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002934 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002935 asihpi->support_grouping,
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002936 asihpi->support_mrx,
2937 asihpi->update_interval_frames
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002938 );
2939
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002940 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002941 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002942 dev_err(&pci_dev->dev, "pcm_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002943 goto __nodev;
2944 }
2945 err = snd_card_asihpi_mixer_new(asihpi);
2946 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002947 dev_err(&pci_dev->dev, "mixer_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002948 goto __nodev;
2949 }
2950
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002951 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002952 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2953 HPI_CONTROL_SAMPLECLOCK, &h_control);
2954
2955 if (!err)
2956 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002957 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002958
2959 snd_asihpi_proc_init(asihpi);
2960
2961 /* always create, can be enabled or disabled dynamically
2962 by enable_hwdep module param*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002963 snd_asihpi_hpi_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002964
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002965 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002966
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002967 sprintf(card->shortname, "AudioScience ASI%4X",
2968 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002969 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002970 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002971 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002972
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002973 if (!err) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002974 dev++;
2975 return 0;
2976 }
2977__nodev:
2978 snd_card_free(card);
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002979 dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002980 return err;
2981
2982}
2983
Bill Pembertone23e7a12012-12-06 12:35:10 -05002984static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002985{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002986 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002987 struct snd_card_asihpi *asihpi = hpi->snd_card->private_data;
2988
2989 /* Stop interrupts */
2990 if (hpi->interrupt_mode) {
2991 hpi->interrupt_callback = NULL;
2992 hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
2993 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
2994 tasklet_kill(&asihpi->t);
2995 }
2996
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002997 snd_card_free(hpi->snd_card);
2998 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002999 asihpi_adapter_remove(pci_dev);
3000}
3001
Benoit Taine9baa3c32014-08-08 15:56:03 +02003002static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003003 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
3004 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3005 (kernel_ulong_t)HPI_6205},
3006 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
3007 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3008 (kernel_ulong_t)HPI_6000},
3009 {0,}
3010};
3011MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
3012
3013static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003014 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003015 .id_table = asihpi_pci_tbl,
3016 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003017 .remove = snd_asihpi_remove,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003018};
3019
3020static int __init snd_asihpi_init(void)
3021{
3022 asihpi_init();
3023 return pci_register_driver(&driver);
3024}
3025
3026static void __exit snd_asihpi_exit(void)
3027{
3028
3029 pci_unregister_driver(&driver);
3030 asihpi_exit();
3031}
3032
3033module_init(snd_asihpi_init)
3034module_exit(snd_asihpi_exit)
3035