blob: 0b35fb7e7a785fddd41cf5eda24827324240d06b [file] [log] [blame]
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001/*
2 * Asihpi soundcard
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003 * Copyright (c) by AudioScience Inc <support@audioscience.com>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 *
19 * The following is not a condition of use, merely a request:
20 * If you modify this program, particularly if you fix errors, AudioScience Inc
21 * would appreciate it if you grant us the right to use those modifications
22 * for any purpose including commercial applications.
23 */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130024
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020025#include "hpi_internal.h"
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130026#include "hpi_version.h"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020027#include "hpimsginit.h"
28#include "hpioctl.h"
Eliot Blennerhassett7036b922011-12-22 13:38:43 +130029#include "hpicmn.h"
30
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020031#include <linux/pci.h>
32#include <linux/init.h>
33#include <linux/jiffies.h>
34#include <linux/slab.h>
35#include <linux/time.h>
36#include <linux/wait.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040037#include <linux/module.h>
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020038#include <sound/core.h>
39#include <sound/control.h>
40#include <sound/pcm.h>
41#include <sound/pcm_params.h>
42#include <sound/info.h>
43#include <sound/initval.h>
44#include <sound/tlv.h>
45#include <sound/hwdep.h>
46
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020047MODULE_LICENSE("GPL");
48MODULE_AUTHOR("AudioScience inc. <support@audioscience.com>");
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130049MODULE_DESCRIPTION("AudioScience ALSA ASI5xxx ASI6xxx ASI87xx ASI89xx "
Eliot Blennerhassettf50efa22011-12-22 13:38:48 +130050 HPI_VER_STRING);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020051
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130052#if defined CONFIG_SND_DEBUG_VERBOSE
53/**
54 * snd_printddd - very verbose debug printk
55 * @format: format string
56 *
57 * Works like snd_printk() for debugging purposes.
58 * Ignored when CONFIG_SND_DEBUG_VERBOSE is not set.
59 * Must set snd module debug parameter to 3 to enable at runtime.
60 */
61#define snd_printddd(format, args...) \
62 __snd_printk(3, __FILE__, __LINE__, format, ##args)
63#else
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +120064#define snd_printddd(format, args...) do { } while (0)
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +130065#endif
66
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020067static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* index 0-MAX */
68static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103069static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
70static bool enable_hpi_hwdep = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020071
Joe Perches6a73cf42018-05-23 12:20:59 -070072module_param_array(index, int, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020073MODULE_PARM_DESC(index, "ALSA index value for AudioScience soundcard.");
74
Joe Perches6a73cf42018-05-23 12:20:59 -070075module_param_array(id, charp, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020076MODULE_PARM_DESC(id, "ALSA ID string for AudioScience soundcard.");
77
Joe Perches6a73cf42018-05-23 12:20:59 -070078module_param_array(enable, bool, NULL, 0444);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020079MODULE_PARM_DESC(enable, "ALSA enable AudioScience soundcard.");
80
Joe Perches6a73cf42018-05-23 12:20:59 -070081module_param(enable_hpi_hwdep, bool, 0644);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020082MODULE_PARM_DESC(enable_hpi_hwdep,
83 "ALSA enable HPI hwdep for AudioScience soundcard ");
84
85/* identify driver */
86#ifdef KERNEL_ALSA_BUILD
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130087static char *build_info = "Built using headers from kernel source";
Joe Perches6a73cf42018-05-23 12:20:59 -070088module_param(build_info, charp, 0444);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130089MODULE_PARM_DESC(build_info, "Built using headers from kernel source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020090#else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +130091static char *build_info = "Built within ALSA source";
Joe Perches6a73cf42018-05-23 12:20:59 -070092module_param(build_info, charp, 0444);
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +130093MODULE_PARM_DESC(build_info, "Built within ALSA source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +020094#endif
95
96/* set to 1 to dump every control from adapter to log */
97static const int mixer_dump;
98
99#define DEFAULT_SAMPLERATE 44100
100static int adapter_fs = DEFAULT_SAMPLERATE;
101
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200102/* defaults */
103#define PERIODS_MIN 2
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300104#define PERIOD_BYTES_MIN 2048
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200105#define BUFFER_BYTES_MAX (512 * 1024)
106
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200107#define MAX_CLOCKSOURCES (HPI_SAMPLECLOCK_SOURCE_LAST + 1 + 7)
108
109struct clk_source {
110 int source;
111 int index;
Eliot Blennerhassett3872f192014-11-20 16:22:49 +1300112 const char *name;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200113};
114
115struct clk_cache {
116 int count;
117 int has_local;
118 struct clk_source s[MAX_CLOCKSOURCES];
119};
120
121/* Per card data */
122struct snd_card_asihpi {
123 struct snd_card *card;
124 struct pci_dev *pci;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +1300125 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200126
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300127 /* In low latency mode there is only one stream, a pointer to its
128 * private data is stored here on trigger and cleared on stop.
129 * The interrupt handler uses it as a parameter when calling
130 * snd_card_asihpi_timer_function().
131 */
132 struct snd_card_asihpi_pcm *llmode_streampriv;
133 struct tasklet_struct t;
134 void (*pcm_start)(struct snd_pcm_substream *substream);
135 void (*pcm_stop)(struct snd_pcm_substream *substream);
136
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200137 u32 h_mixer;
138 struct clk_cache cc;
139
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200140 u16 can_dma;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200141 u16 support_grouping;
142 u16 support_mrx;
143 u16 update_interval_frames;
144 u16 in_max_chans;
145 u16 out_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +1300146 u16 in_min_chans;
147 u16 out_min_chans;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200148};
149
150/* Per stream data */
151struct snd_card_asihpi_pcm {
152 struct timer_list timer;
153 unsigned int respawn_timer;
154 unsigned int hpi_buffer_attached;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300155 unsigned int buffer_bytes;
156 unsigned int period_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200157 unsigned int bytes_per_sec;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300158 unsigned int pcm_buf_host_rw_ofs; /* Host R/W pos */
159 unsigned int pcm_buf_dma_ofs; /* DMA R/W offset in buffer */
160 unsigned int pcm_buf_elapsed_dma_ofs; /* DMA R/W offset in buffer */
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200161 unsigned int drained_count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200162 struct snd_pcm_substream *substream;
163 u32 h_stream;
164 struct hpi_format format;
165};
166
167/* universal stream verbs work with out or in stream handles */
168
169/* Functions to allow driver to give a buffer to HPI for busmastering */
170
171static u16 hpi_stream_host_buffer_attach(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200172 u32 h_stream, /* handle to outstream. */
173 u32 size_in_bytes, /* size in bytes of bus mastering buffer */
174 u32 pci_address
175)
176{
177 struct hpi_message hm;
178 struct hpi_response hr;
179 unsigned int obj = hpi_handle_object(h_stream);
180
181 if (!h_stream)
182 return HPI_ERROR_INVALID_OBJ;
183 hpi_init_message_response(&hm, &hr, obj,
184 obj == HPI_OBJ_OSTREAM ?
185 HPI_OSTREAM_HOSTBUFFER_ALLOC :
186 HPI_ISTREAM_HOSTBUFFER_ALLOC);
187
188 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
189 &hm.obj_index);
190
191 hm.u.d.u.buffer.buffer_size = size_in_bytes;
192 hm.u.d.u.buffer.pci_address = pci_address;
193 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_GRANTADAPTER;
194 hpi_send_recv(&hm, &hr);
195 return hr.error;
196}
197
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300198static u16 hpi_stream_host_buffer_detach(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200199{
200 struct hpi_message hm;
201 struct hpi_response hr;
202 unsigned int obj = hpi_handle_object(h_stream);
203
204 if (!h_stream)
205 return HPI_ERROR_INVALID_OBJ;
206
207 hpi_init_message_response(&hm, &hr, obj,
208 obj == HPI_OBJ_OSTREAM ?
209 HPI_OSTREAM_HOSTBUFFER_FREE :
210 HPI_ISTREAM_HOSTBUFFER_FREE);
211
212 hpi_handle_to_indexes(h_stream, &hm.adapter_index,
213 &hm.obj_index);
214 hm.u.d.u.buffer.command = HPI_BUFFER_CMD_INTERNAL_REVOKEADAPTER;
215 hpi_send_recv(&hm, &hr);
216 return hr.error;
217}
218
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300219static inline u16 hpi_stream_start(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200220{
221 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300222 return hpi_outstream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200223 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300224 return hpi_instream_start(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200225}
226
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300227static inline u16 hpi_stream_stop(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200228{
229 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300230 return hpi_outstream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200231 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300232 return hpi_instream_stop(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200233}
234
235static inline u16 hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200236 u32 h_stream,
237 u16 *pw_state,
238 u32 *pbuffer_size,
239 u32 *pdata_in_buffer,
240 u32 *psample_count,
241 u32 *pauxiliary_data
242)
243{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300244 u16 e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200245 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300246 e = hpi_outstream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200247 pbuffer_size, pdata_in_buffer,
248 psample_count, pauxiliary_data);
249 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300250 e = hpi_instream_get_info_ex(h_stream, pw_state,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200251 pbuffer_size, pdata_in_buffer,
252 psample_count, pauxiliary_data);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300253 return e;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200254}
255
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300256static inline u16 hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200257 u32 h_master,
258 u32 h_stream)
259{
260 if (hpi_handle_object(h_master) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300261 return hpi_outstream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200262 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300263 return hpi_instream_group_add(h_master, h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200264}
265
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300266static inline u16 hpi_stream_group_reset(u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200267{
268 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300269 return hpi_outstream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200270 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300271 return hpi_instream_group_reset(h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200272}
273
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300274static inline u16 hpi_stream_group_get_map(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200275 u32 h_stream, u32 *mo, u32 *mi)
276{
277 if (hpi_handle_object(h_stream) == HPI_OBJ_OSTREAM)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300278 return hpi_outstream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200279 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300280 return hpi_instream_group_get_map(h_stream, mo, mi);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200281}
282
283static u16 handle_error(u16 err, int line, char *filename)
284{
285 if (err)
286 printk(KERN_WARNING
287 "in file %s, line %d: HPI error %d\n",
288 filename, line, err);
289 return err;
290}
291
292#define hpi_handle_error(x) handle_error(x, __LINE__, __FILE__)
293
294/***************************** GENERAL PCM ****************/
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200295
296static void print_hwparams(struct snd_pcm_substream *substream,
297 struct snd_pcm_hw_params *p)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200298{
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200299 char name[16];
300 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300301 snd_printdd("%s HWPARAMS\n", name);
302 snd_printdd(" samplerate=%dHz channels=%d format=%d subformat=%d\n",
303 params_rate(p), params_channels(p),
304 params_format(p), params_subformat(p));
305 snd_printdd(" buffer=%dB period=%dB period_size=%dB periods=%d\n",
306 params_buffer_bytes(p), params_period_bytes(p),
307 params_period_size(p), params_periods(p));
308 snd_printdd(" buffer_size=%d access=%d data_rate=%dB/s\n",
309 params_buffer_size(p), params_access(p),
310 params_rate(p) * params_channels(p) *
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200311 snd_pcm_format_width(params_format(p)) / 8);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200312}
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200313
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200314#define INVALID_FORMAT (__force snd_pcm_format_t)(-1)
315
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200316static snd_pcm_format_t hpi_to_alsa_formats[] = {
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200317 INVALID_FORMAT, /* INVALID */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200318 SNDRV_PCM_FORMAT_U8, /* HPI_FORMAT_PCM8_UNSIGNED 1 */
319 SNDRV_PCM_FORMAT_S16, /* HPI_FORMAT_PCM16_SIGNED 2 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200320 INVALID_FORMAT, /* HPI_FORMAT_MPEG_L1 3 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200321 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L2 4 */
322 SNDRV_PCM_FORMAT_MPEG, /* HPI_FORMAT_MPEG_L3 5 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200323 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC2 6 */
324 INVALID_FORMAT, /* HPI_FORMAT_DOLBY_AC3 7 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200325 SNDRV_PCM_FORMAT_S16_BE,/* HPI_FORMAT_PCM16_BIGENDIAN 8 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200326 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS 9 */
327 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_INSERTS 10 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200328 SNDRV_PCM_FORMAT_S32, /* HPI_FORMAT_PCM32_SIGNED 11 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200329 INVALID_FORMAT, /* HPI_FORMAT_RAW_BITSTREAM 12 */
330 INVALID_FORMAT, /* HPI_FORMAT_AA_TAGIT1_HITS_EX1 13 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200331 SNDRV_PCM_FORMAT_FLOAT, /* HPI_FORMAT_PCM32_FLOAT 14 */
332#if 1
333 /* ALSA can't handle 3 byte sample size together with power-of-2
334 * constraint on buffer_bytes, so disable this format
335 */
Takashi Iwaia91a0e72018-07-25 23:19:41 +0200336 INVALID_FORMAT
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200337#else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300338 /* SNDRV_PCM_FORMAT_S24_3LE */ /* HPI_FORMAT_PCM24_SIGNED 15 */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200339#endif
340};
341
342
343static int snd_card_asihpi_format_alsa2hpi(snd_pcm_format_t alsa_format,
344 u16 *hpi_format)
345{
346 u16 format;
347
348 for (format = HPI_FORMAT_PCM8_UNSIGNED;
349 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
350 if (hpi_to_alsa_formats[format] == alsa_format) {
351 *hpi_format = format;
352 return 0;
353 }
354 }
355
356 snd_printd(KERN_WARNING "failed match for alsa format %d\n",
357 alsa_format);
358 *hpi_format = 0;
359 return -EINVAL;
360}
361
362static void snd_card_asihpi_pcm_samplerates(struct snd_card_asihpi *asihpi,
363 struct snd_pcm_hardware *pcmhw)
364{
365 u16 err;
366 u32 h_control;
367 u32 sample_rate;
368 int idx;
369 unsigned int rate_min = 200000;
370 unsigned int rate_max = 0;
371 unsigned int rates = 0;
372
373 if (asihpi->support_mrx) {
374 rates |= SNDRV_PCM_RATE_CONTINUOUS;
375 rates |= SNDRV_PCM_RATE_8000_96000;
376 rate_min = 8000;
377 rate_max = 100000;
378 } else {
379 /* on cards without SRC,
380 valid rates are determined by sampleclock */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300381 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200382 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
383 HPI_CONTROL_SAMPLECLOCK, &h_control);
384 if (err) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +1300385 dev_err(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300386 "No local sampleclock, err %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200387 }
388
Eliot Blennerhassett7bf76c32011-03-25 15:25:46 +1300389 for (idx = -1; idx < 100; idx++) {
390 if (idx == -1) {
391 if (hpi_sample_clock_get_sample_rate(h_control,
392 &sample_rate))
393 continue;
394 } else if (hpi_sample_clock_query_local_rate(h_control,
395 idx, &sample_rate)) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200396 break;
397 }
398
399 rate_min = min(rate_min, sample_rate);
400 rate_max = max(rate_max, sample_rate);
401
402 switch (sample_rate) {
403 case 5512:
404 rates |= SNDRV_PCM_RATE_5512;
405 break;
406 case 8000:
407 rates |= SNDRV_PCM_RATE_8000;
408 break;
409 case 11025:
410 rates |= SNDRV_PCM_RATE_11025;
411 break;
412 case 16000:
413 rates |= SNDRV_PCM_RATE_16000;
414 break;
415 case 22050:
416 rates |= SNDRV_PCM_RATE_22050;
417 break;
418 case 32000:
419 rates |= SNDRV_PCM_RATE_32000;
420 break;
421 case 44100:
422 rates |= SNDRV_PCM_RATE_44100;
423 break;
424 case 48000:
425 rates |= SNDRV_PCM_RATE_48000;
426 break;
427 case 64000:
428 rates |= SNDRV_PCM_RATE_64000;
429 break;
430 case 88200:
431 rates |= SNDRV_PCM_RATE_88200;
432 break;
433 case 96000:
434 rates |= SNDRV_PCM_RATE_96000;
435 break;
436 case 176400:
437 rates |= SNDRV_PCM_RATE_176400;
438 break;
439 case 192000:
440 rates |= SNDRV_PCM_RATE_192000;
441 break;
442 default: /* some other rate */
443 rates |= SNDRV_PCM_RATE_KNOT;
444 }
445 }
446 }
447
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200448 pcmhw->rates = rates;
449 pcmhw->rate_min = rate_min;
450 pcmhw->rate_max = rate_max;
451}
452
453static int snd_card_asihpi_pcm_hw_params(struct snd_pcm_substream *substream,
454 struct snd_pcm_hw_params *params)
455{
456 struct snd_pcm_runtime *runtime = substream->runtime;
457 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
458 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
459 int err;
460 u16 format;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400461 int width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200462 unsigned int bytes_per_sec;
463
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200464 print_hwparams(substream, params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200465 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
466 if (err < 0)
467 return err;
468 err = snd_card_asihpi_format_alsa2hpi(params_format(params), &format);
469 if (err)
470 return err;
471
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200472 hpi_handle_error(hpi_format_create(&dpcm->format,
473 params_channels(params),
474 format, params_rate(params), 0, 0));
475
476 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300477 if (hpi_instream_reset(dpcm->h_stream) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200478 return -EINVAL;
479
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300480 if (hpi_instream_set_format(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200481 dpcm->h_stream, &dpcm->format) != 0)
482 return -EINVAL;
483 }
484
485 dpcm->hpi_buffer_attached = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200486 if (card->can_dma) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300487 err = hpi_stream_host_buffer_attach(dpcm->h_stream,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200488 params_buffer_bytes(params), runtime->dma_addr);
489 if (err == 0) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300490 snd_printdd(
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300491 "stream_host_buffer_attach success %u %lu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200492 params_buffer_bytes(params),
493 (unsigned long)runtime->dma_addr);
494 } else {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300495 snd_printd("stream_host_buffer_attach error %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200496 err);
497 return -ENOMEM;
498 }
499
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300500 err = hpi_stream_get_info_ex(dpcm->h_stream, NULL,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300501 &dpcm->hpi_buffer_attached, NULL, NULL, NULL);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200502 }
503 bytes_per_sec = params_rate(params) * params_channels(params);
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400504 width = snd_pcm_format_width(params_format(params));
505 bytes_per_sec *= width;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200506 bytes_per_sec /= 8;
Kulikov Vasiliy315e8f72010-07-15 22:48:19 +0400507 if (width < 0 || bytes_per_sec == 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200508 return -EINVAL;
509
510 dpcm->bytes_per_sec = bytes_per_sec;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300511 dpcm->buffer_bytes = params_buffer_bytes(params);
512 dpcm->period_bytes = params_period_bytes(params);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200513
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200514 return 0;
515}
516
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300517static int
518snd_card_asihpi_hw_free(struct snd_pcm_substream *substream)
519{
520 struct snd_pcm_runtime *runtime = substream->runtime;
521 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
522 if (dpcm->hpi_buffer_attached)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300523 hpi_stream_host_buffer_detach(dpcm->h_stream);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300524
525 snd_pcm_lib_free_pages(substream);
526 return 0;
527}
528
529static void snd_card_asihpi_runtime_free(struct snd_pcm_runtime *runtime)
530{
531 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
532 kfree(dpcm);
533}
534
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200535static void snd_card_asihpi_pcm_timer_start(struct snd_pcm_substream *
536 substream)
537{
538 struct snd_pcm_runtime *runtime = substream->runtime;
539 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
540 int expiry;
541
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300542 expiry = HZ / 200;
Eliot Blennerhassette9886ab2014-11-20 16:22:48 +1300543
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300544 expiry = max(expiry, 1); /* don't let it be zero! */
Takashi Iwai6fec2b52015-01-19 11:32:51 +0100545 mod_timer(&dpcm->timer, jiffies + expiry);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200546 dpcm->respawn_timer = 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200547}
548
549static void snd_card_asihpi_pcm_timer_stop(struct snd_pcm_substream *substream)
550{
551 struct snd_pcm_runtime *runtime = substream->runtime;
552 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
553
554 dpcm->respawn_timer = 0;
555 del_timer(&dpcm->timer);
556}
557
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300558static void snd_card_asihpi_pcm_int_start(struct snd_pcm_substream *substream)
559{
560 struct snd_card_asihpi_pcm *dpcm;
561 struct snd_card_asihpi *card;
562
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300563 dpcm = (struct snd_card_asihpi_pcm *)substream->runtime->private_data;
564 card = snd_pcm_substream_chip(substream);
565
Takashi Iwai2a0d85d2017-09-07 10:51:54 +0200566 WARN_ON(in_interrupt());
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300567 tasklet_disable(&card->t);
568 card->llmode_streampriv = dpcm;
569 tasklet_enable(&card->t);
570
571 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
572 HPI_ADAPTER_PROPERTY_IRQ_RATE,
573 card->update_interval_frames, 0));
574}
575
576static void snd_card_asihpi_pcm_int_stop(struct snd_pcm_substream *substream)
577{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300578 struct snd_card_asihpi *card;
579
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300580 card = snd_pcm_substream_chip(substream);
581
582 hpi_handle_error(hpi_adapter_set_property(card->hpi->adapter->index,
583 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
584
585 if (in_interrupt())
586 card->llmode_streampriv = NULL;
587 else {
588 tasklet_disable(&card->t);
589 card->llmode_streampriv = NULL;
590 tasklet_enable(&card->t);
591 }
592}
593
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200594static int snd_card_asihpi_trigger(struct snd_pcm_substream *substream,
595 int cmd)
596{
597 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
598 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
599 struct snd_pcm_substream *s;
600 u16 e;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200601 char name[16];
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200602
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200603 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200604
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200605 switch (cmd) {
606 case SNDRV_PCM_TRIGGER_START:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300607 snd_printdd("%s trigger start\n", name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200608 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300609 struct snd_pcm_runtime *runtime = s->runtime;
610 struct snd_card_asihpi_pcm *ds = runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200611
612 if (snd_pcm_substream_chip(s) != card)
613 continue;
614
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300615 /* don't link Cap and Play */
616 if (substream->stream != s->stream)
617 continue;
618
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200619 ds->drained_count = 0;
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200620 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200621 /* How do I know how much valid data is present
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300622 * in buffer? Must be at least one period!
623 * Guessing 2 periods, but if
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200624 * buffer is bigger it may contain even more
625 * data??
626 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300627 unsigned int preload = ds->period_bytes * 1;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300628 snd_printddd("%d preload %d\n", s->number, preload);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200629 hpi_handle_error(hpi_outstream_write_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300630 ds->h_stream,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300631 &runtime->dma_area[0],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200632 preload,
633 &ds->format));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300634 ds->pcm_buf_host_rw_ofs = preload;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200635 }
636
637 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200638 snd_printdd("%d group\n", s->number);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300639 e = hpi_stream_group_add(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200640 dpcm->h_stream,
641 ds->h_stream);
642 if (!e) {
643 snd_pcm_trigger_done(s, substream);
644 } else {
645 hpi_handle_error(e);
646 break;
647 }
648 } else
649 break;
650 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200651 /* start the master stream */
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300652 card->pcm_start(substream);
Eliot Blennerhassettc4ed97d2011-02-10 17:26:20 +1300653 if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE) ||
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200654 !card->can_dma)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300655 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200656 break;
657
658 case SNDRV_PCM_TRIGGER_STOP:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300659 snd_printdd("%s trigger stop\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300660 card->pcm_stop(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200661 snd_pcm_group_for_each_entry(s, substream) {
662 if (snd_pcm_substream_chip(s) != card)
663 continue;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300664 /* don't link Cap and Play */
665 if (substream->stream != s->stream)
666 continue;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200667
668 /*? workaround linked streams don't
669 transition to SETUP 20070706*/
670 s->runtime->status->state = SNDRV_PCM_STATE_SETUP;
671
672 if (card->support_grouping) {
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200673 snd_printdd("%d group\n", s->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200674 snd_pcm_trigger_done(s, substream);
675 } else
676 break;
677 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200678
679 /* _prepare and _hwparams reset the stream */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300680 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200681 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
682 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300683 hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200684
685 if (card->support_grouping)
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300686 hpi_handle_error(hpi_stream_group_reset(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200687 break;
688
689 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300690 snd_printdd("%s trigger pause release\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300691 card->pcm_start(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300692 hpi_handle_error(hpi_stream_start(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200693 break;
694 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300695 snd_printdd("%s trigger pause push\n", name);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300696 card->pcm_stop(substream);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300697 hpi_handle_error(hpi_stream_stop(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200698 break;
699 default:
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300700 snd_printd(KERN_ERR "\tINVALID\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200701 return -EINVAL;
702 }
703
704 return 0;
705}
706
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200707/*algorithm outline
708 Without linking degenerates to getting single stream pos etc
709 Without mmap 2nd loop degenerates to snd_pcm_period_elapsed
710*/
711/*
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300712pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200713for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300714 pcm_buf_dma_ofs=get_buf_pos(s);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300715 min_buf_pos = modulo_min(min_buf_pos, pcm_buf_dma_ofs, buffer_bytes)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300716 new_data = min(new_data, calc_new_data(pcm_buf_dma_ofs,irq_pos)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200717}
718timer.expires = jiffies + predict_next_period_ready(min_buf_pos);
719for_each_linked_stream(s) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300720 s->pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300721 if (new_data > period_bytes) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200722 if (mmap) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300723 irq_pos = (irq_pos + period_bytes) % buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200724 if (playback) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300725 write(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200726 } else {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300727 read(period_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200728 }
729 }
730 snd_pcm_period_elapsed(s);
731 }
732}
733*/
734
735/** Minimum of 2 modulo values. Works correctly when the difference between
736* the values is less than half the modulus
737*/
738static inline unsigned int modulo_min(unsigned int a, unsigned int b,
739 unsigned long int modulus)
740{
741 unsigned int result;
742 if (((a-b) % modulus) < (modulus/2))
743 result = b;
744 else
745 result = a;
746
747 return result;
748}
749
750/** Timer function, equivalent to interrupt service routine for cards
751*/
Kees Cook394ca812017-10-04 17:53:40 -0700752static void snd_card_asihpi_timer_function(struct timer_list *t)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200753{
Kees Cook394ca812017-10-04 17:53:40 -0700754 struct snd_card_asihpi_pcm *dpcm = from_timer(dpcm, t, timer);
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300755 struct snd_pcm_substream *substream = dpcm->substream;
756 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200757 struct snd_pcm_runtime *runtime;
758 struct snd_pcm_substream *s;
759 unsigned int newdata = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300760 unsigned int pcm_buf_dma_ofs, min_buf_pos = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200761 unsigned int remdata, xfercount, next_jiffies;
762 int first = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300763 int loops = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200764 u16 state;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300765 u32 buffer_size, bytes_avail, samples_played, on_card_bytes;
Eliot Blennerhassett0a17e992011-07-22 15:52:44 +1200766 char name[16];
767
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200768
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300769 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300770
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200771 /* find minimum newdata and buffer pos in group */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300772 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200773 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
774 runtime = s->runtime;
775
776 if (snd_pcm_substream_chip(s) != card)
777 continue;
778
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300779 /* don't link Cap and Play */
780 if (substream->stream != s->stream)
781 continue;
782
783 hpi_handle_error(hpi_stream_get_info_ex(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200784 ds->h_stream, &state,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300785 &buffer_size, &bytes_avail,
786 &samples_played, &on_card_bytes));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200787
788 /* number of bytes in on-card buffer */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300789 runtime->delay = on_card_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200790
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200791 if (!card->can_dma)
792 on_card_bytes = bytes_avail;
793
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300794 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300795 pcm_buf_dma_ofs = ds->pcm_buf_host_rw_ofs - bytes_avail;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300796 if (state == HPI_STATE_STOPPED) {
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300797 if (bytes_avail == 0) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300798 hpi_handle_error(hpi_stream_start(ds->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300799 snd_printdd("P%d start\n", s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200800 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300801 }
802 } else if (state == HPI_STATE_DRAINED) {
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +1300803 snd_printd(KERN_WARNING "P%d drained\n",
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300804 s->number);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200805 ds->drained_count++;
Eliot Blennerhassett0be55c42011-12-22 13:38:37 +1300806 if (ds->drained_count > 20) {
Takashi Iwai1fb85102014-11-07 17:08:28 +0100807 snd_pcm_stop_xrun(s);
Eliot Blennerhassett0b7ce9e2011-04-05 20:55:43 +1200808 continue;
809 }
810 } else {
811 ds->drained_count = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300812 }
813 } else
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300814 pcm_buf_dma_ofs = bytes_avail + ds->pcm_buf_host_rw_ofs;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200815
816 if (first) {
817 /* can't statically init min when wrap is involved */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300818 min_buf_pos = pcm_buf_dma_ofs;
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300819 newdata = (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200820 first = 0;
821 } else {
822 min_buf_pos =
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300823 modulo_min(min_buf_pos, pcm_buf_dma_ofs, UINT_MAX+1L);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200824 newdata = min(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300825 (pcm_buf_dma_ofs - ds->pcm_buf_elapsed_dma_ofs) % ds->buffer_bytes,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200826 newdata);
827 }
828
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300829 snd_printddd(
830 "timer1, %s, %d, S=%d, elap=%d, rw=%d, dsp=%d, left=%d, aux=%d, space=%d, hw_ptr=%ld, appl_ptr=%ld\n",
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300831 name, s->number, state,
832 ds->pcm_buf_elapsed_dma_ofs,
833 ds->pcm_buf_host_rw_ofs,
834 pcm_buf_dma_ofs,
835 (int)bytes_avail,
836
837 (int)on_card_bytes,
838 buffer_size-bytes_avail,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200839 (unsigned long)frames_to_bytes(runtime,
840 runtime->status->hw_ptr),
841 (unsigned long)frames_to_bytes(runtime,
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300842 runtime->control->appl_ptr)
843 );
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300844 loops++;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200845 }
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300846 pcm_buf_dma_ofs = min_buf_pos;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200847
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300848 remdata = newdata % dpcm->period_bytes;
849 xfercount = newdata - remdata; /* a multiple of period_bytes */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300850 /* come back when on_card_bytes has decreased enough to allow
851 write to happen, or when data has been consumed to make another
852 period
853 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300854 if (xfercount && (on_card_bytes > dpcm->period_bytes))
855 next_jiffies = ((on_card_bytes - dpcm->period_bytes) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300856 else
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300857 next_jiffies = ((dpcm->period_bytes - remdata) * HZ / dpcm->bytes_per_sec);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300858
859 next_jiffies = max(next_jiffies, 1U);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200860 dpcm->timer.expires = jiffies + next_jiffies;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300861 snd_printddd("timer2, jif=%d, buf_pos=%d, newdata=%d, xfer=%d\n",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300862 next_jiffies, pcm_buf_dma_ofs, newdata, xfercount);
863
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300864 snd_pcm_group_for_each_entry(s, substream) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200865 struct snd_card_asihpi_pcm *ds = s->runtime->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200866
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300867 /* don't link Cap and Play */
868 if (substream->stream != s->stream)
869 continue;
870
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300871 /* Store dma offset for use by pointer callback */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300872 ds->pcm_buf_dma_ofs = pcm_buf_dma_ofs;
873
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200874 if (xfercount &&
875 /* Limit use of on card fifo for playback */
876 ((on_card_bytes <= ds->period_bytes) ||
877 (s->stream == SNDRV_PCM_STREAM_CAPTURE)))
878
879 {
880
881 unsigned int buf_ofs = ds->pcm_buf_host_rw_ofs % ds->buffer_bytes;
882 unsigned int xfer1, xfer2;
883 char *pd = &s->runtime->dma_area[buf_ofs];
884
Eliot Blennerhassettb0096a62011-04-05 20:55:46 +1200885 if (card->can_dma) { /* buffer wrap is handled at lower level */
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200886 xfer1 = xfercount;
887 xfer2 = 0;
888 } else {
889 xfer1 = min(xfercount, ds->buffer_bytes - buf_ofs);
890 xfer2 = xfercount - xfer1;
891 }
892
893 if (s->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300894 snd_printddd("write1, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200895 s->number, xfer1, buf_ofs);
896 hpi_handle_error(
897 hpi_outstream_write_buf(
898 ds->h_stream, pd, xfer1,
899 &ds->format));
900
901 if (xfer2) {
902 pd = s->runtime->dma_area;
903
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300904 snd_printddd("write2, P=%d, xfer=%d, buf_ofs=%d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200905 s->number,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200906 xfercount - xfer1, buf_ofs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200907 hpi_handle_error(
908 hpi_outstream_write_buf(
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200909 ds->h_stream, pd,
910 xfercount - xfer1,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200911 &ds->format));
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200912 }
913 } else {
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300914 snd_printddd("read1, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200915 s->number, xfer1);
916 hpi_handle_error(
917 hpi_instream_read_buf(
918 ds->h_stream,
919 pd, xfer1));
920 if (xfer2) {
921 pd = s->runtime->dma_area;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300922 snd_printddd("read2, C=%d, xfer=%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200923 s->number, xfer2);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200924 hpi_handle_error(
925 hpi_instream_read_buf(
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300926 ds->h_stream,
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200927 pd, xfer2));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200928 }
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +1200929 }
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300930 /* ? host_rw_ofs always ahead of elapsed_dma_ofs by preload size? */
Eliot Blennerhassett47a74a52011-12-22 11:54:02 +1300931 ds->pcm_buf_host_rw_ofs += xfercount;
932 ds->pcm_buf_elapsed_dma_ofs += xfercount;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200933 snd_pcm_period_elapsed(s);
934 }
935 }
936
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300937 if (!card->hpi->interrupt_mode && dpcm->respawn_timer)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200938 add_timer(&dpcm->timer);
939}
940
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300941static void snd_card_asihpi_int_task(unsigned long data)
942{
943 struct hpi_adapter *a = (struct hpi_adapter *)data;
944 struct snd_card_asihpi *asihpi;
945
946 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
947 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
948 if (asihpi->llmode_streampriv)
949 snd_card_asihpi_timer_function(
Kees Cook394ca812017-10-04 17:53:40 -0700950 &asihpi->llmode_streampriv->timer);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +1300951}
952
953static void snd_card_asihpi_isr(struct hpi_adapter *a)
954{
955 struct snd_card_asihpi *asihpi;
956
957 WARN_ON(!a || !a->snd_card || !a->snd_card->private_data);
958 asihpi = (struct snd_card_asihpi *)a->snd_card->private_data;
959 tasklet_schedule(&asihpi->t);
960}
961
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200962/***************************** PLAYBACK OPS ****************/
963static int snd_card_asihpi_playback_ioctl(struct snd_pcm_substream *substream,
964 unsigned int cmd, void *arg)
965{
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300966 char name[16];
967 snd_pcm_debug_name(substream, name, sizeof(name));
968 snd_printddd(KERN_INFO "%s ioctl %d\n", name, cmd);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200969 return snd_pcm_lib_ioctl(substream, cmd, arg);
970}
971
972static int snd_card_asihpi_playback_prepare(struct snd_pcm_substream *
973 substream)
974{
975 struct snd_pcm_runtime *runtime = substream->runtime;
976 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
977
Eliot Blennerhassetta6477132011-04-05 20:55:42 +1200978 snd_printdd("P%d prepare\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200979
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300980 hpi_handle_error(hpi_outstream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +1300981 dpcm->pcm_buf_host_rw_ofs = 0;
982 dpcm->pcm_buf_dma_ofs = 0;
983 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200984 return 0;
985}
986
987static snd_pcm_uframes_t
988snd_card_asihpi_playback_pointer(struct snd_pcm_substream *substream)
989{
990 struct snd_pcm_runtime *runtime = substream->runtime;
991 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
992 snd_pcm_uframes_t ptr;
Eliot Blennerhassettcbd757d2011-12-22 13:38:35 +1300993 char name[16];
994 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200995
Eliot Blennerhassettba944552011-02-10 17:26:04 +1300996 ptr = bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +1300997 snd_printddd("%s, pointer=%ld\n", name, (unsigned long)ptr);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +0200998 return ptr;
999}
1000
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001001static u64 snd_card_asihpi_playback_formats(struct snd_card_asihpi *asihpi,
1002 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001003{
1004 struct hpi_format hpi_format;
1005 u16 format;
1006 u16 err;
1007 u32 h_control;
1008 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001009 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001010
1011 /* on cards without SRC, must query at valid rate,
1012 * maybe set by external sync
1013 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001014 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001015 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1016 HPI_CONTROL_SAMPLECLOCK, &h_control);
1017
1018 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001019 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001020 &sample_rate);
1021
1022 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1023 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001024 err = hpi_format_create(&hpi_format, asihpi->out_max_chans,
1025 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001026 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001027 err = hpi_outstream_query_format(h_stream, &hpi_format);
Takashi Iwaia91a0e72018-07-25 23:19:41 +02001028 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001029 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001030 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001031 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001032}
1033
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001034static int snd_card_asihpi_playback_open(struct snd_pcm_substream *substream)
1035{
1036 struct snd_pcm_runtime *runtime = substream->runtime;
1037 struct snd_card_asihpi_pcm *dpcm;
1038 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001039 struct snd_pcm_hardware snd_card_asihpi_playback;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001040 int err;
1041
1042 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1043 if (dpcm == NULL)
1044 return -ENOMEM;
1045
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001046 err = hpi_outstream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001047 substream->number, &dpcm->h_stream);
1048 hpi_handle_error(err);
1049 if (err)
1050 kfree(dpcm);
1051 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1052 return -EBUSY;
1053 if (err)
1054 return -EIO;
1055
1056 /*? also check ASI5000 samplerate source
1057 If external, only support external rate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001058 If internal and other stream playing, can't switch
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001059 */
1060
Kees Cook394ca812017-10-04 17:53:40 -07001061 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001062 dpcm->substream = substream;
1063 runtime->private_data = dpcm;
1064 runtime->private_free = snd_card_asihpi_runtime_free;
1065
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001066 memset(&snd_card_asihpi_playback, 0, sizeof(snd_card_asihpi_playback));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001067 if (!card->hpi->interrupt_mode) {
1068 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1069 snd_card_asihpi_playback.period_bytes_min = PERIOD_BYTES_MIN;
1070 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1071 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1072 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1073 } else {
1074 size_t pbmin = card->update_interval_frames *
1075 card->out_max_chans;
1076 snd_card_asihpi_playback.buffer_bytes_max = BUFFER_BYTES_MAX;
1077 snd_card_asihpi_playback.period_bytes_min = pbmin;
1078 snd_card_asihpi_playback.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1079 snd_card_asihpi_playback.periods_min = PERIODS_MIN;
1080 snd_card_asihpi_playback.periods_max = BUFFER_BYTES_MAX / pbmin;
1081 }
1082
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001083 /* snd_card_asihpi_playback.fifo_size = 0; */
1084 snd_card_asihpi_playback.channels_max = card->out_max_chans;
1085 snd_card_asihpi_playback.channels_min = card->out_min_chans;
1086 snd_card_asihpi_playback.formats =
1087 snd_card_asihpi_playback_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001088
1089 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_playback);
1090
1091 snd_card_asihpi_playback.info = SNDRV_PCM_INFO_INTERLEAVED |
1092 SNDRV_PCM_INFO_DOUBLE |
1093 SNDRV_PCM_INFO_BATCH |
1094 SNDRV_PCM_INFO_BLOCK_TRANSFER |
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001095 SNDRV_PCM_INFO_PAUSE |
1096 SNDRV_PCM_INFO_MMAP |
1097 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001098
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001099 if (card->support_grouping) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001100 snd_card_asihpi_playback.info |= SNDRV_PCM_INFO_SYNC_START;
Eliot Blennerhassett09c728a2011-12-22 13:38:38 +13001101 snd_pcm_set_sync(substream);
1102 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001103
1104 /* struct is copied, so can create initializer dynamically */
1105 runtime->hw = snd_card_asihpi_playback;
1106
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001107 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001108 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1109 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1110 if (err < 0)
1111 return err;
1112
1113 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1114 card->update_interval_frames);
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13001115
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001116 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001117 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001118
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001119 snd_printdd("playback open\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001120
1121 return 0;
1122}
1123
1124static int snd_card_asihpi_playback_close(struct snd_pcm_substream *substream)
1125{
1126 struct snd_pcm_runtime *runtime = substream->runtime;
1127 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1128
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001129 hpi_handle_error(hpi_outstream_close(dpcm->h_stream));
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001130 snd_printdd("playback close\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001131
1132 return 0;
1133}
1134
Julia Lawall6769e9882016-09-02 00:13:10 +02001135static const struct snd_pcm_ops snd_card_asihpi_playback_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001136 .open = snd_card_asihpi_playback_open,
1137 .close = snd_card_asihpi_playback_close,
1138 .ioctl = snd_card_asihpi_playback_ioctl,
1139 .hw_params = snd_card_asihpi_pcm_hw_params,
1140 .hw_free = snd_card_asihpi_hw_free,
1141 .prepare = snd_card_asihpi_playback_prepare,
1142 .trigger = snd_card_asihpi_trigger,
1143 .pointer = snd_card_asihpi_playback_pointer,
1144};
1145
1146/***************************** CAPTURE OPS ****************/
1147static snd_pcm_uframes_t
1148snd_card_asihpi_capture_pointer(struct snd_pcm_substream *substream)
1149{
1150 struct snd_pcm_runtime *runtime = substream->runtime;
1151 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001152 char name[16];
1153 snd_pcm_debug_name(substream, name, sizeof(name));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001154
Eliot Blennerhassett35a8dc12014-11-20 16:22:50 +13001155 snd_printddd("%s, pointer=%d\n", name, dpcm->pcm_buf_dma_ofs);
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001156 /* NOTE Unlike playback can't use actual samples_played
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001157 for the capture position, because those samples aren't yet in
1158 the local buffer available for reading.
1159 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001160 return bytes_to_frames(runtime, dpcm->pcm_buf_dma_ofs % dpcm->buffer_bytes);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001161}
1162
1163static int snd_card_asihpi_capture_ioctl(struct snd_pcm_substream *substream,
1164 unsigned int cmd, void *arg)
1165{
1166 return snd_pcm_lib_ioctl(substream, cmd, arg);
1167}
1168
1169static int snd_card_asihpi_capture_prepare(struct snd_pcm_substream *substream)
1170{
1171 struct snd_pcm_runtime *runtime = substream->runtime;
1172 struct snd_card_asihpi_pcm *dpcm = runtime->private_data;
1173
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001174 hpi_handle_error(hpi_instream_reset(dpcm->h_stream));
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001175 dpcm->pcm_buf_host_rw_ofs = 0;
1176 dpcm->pcm_buf_dma_ofs = 0;
1177 dpcm->pcm_buf_elapsed_dma_ofs = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001178
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001179 snd_printdd("Capture Prepare %d\n", substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001180 return 0;
1181}
1182
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001183static u64 snd_card_asihpi_capture_formats(struct snd_card_asihpi *asihpi,
1184 u32 h_stream)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001185{
Colin Ian King4593f2d2018-11-16 15:06:32 +00001186 struct hpi_format hpi_format;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001187 u16 format;
1188 u16 err;
1189 u32 h_control;
1190 u32 sample_rate = 48000;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001191 u64 formats = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001192
1193 /* on cards without SRC, must query at valid rate,
1194 maybe set by external sync */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001195 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001196 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
1197 HPI_CONTROL_SAMPLECLOCK, &h_control);
1198
1199 if (!err)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001200 err = hpi_sample_clock_get_sample_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001201 &sample_rate);
1202
1203 for (format = HPI_FORMAT_PCM8_UNSIGNED;
1204 format <= HPI_FORMAT_PCM24_SIGNED; format++) {
1205
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001206 err = hpi_format_create(&hpi_format, asihpi->in_max_chans,
1207 format, sample_rate, 128000, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001208 if (!err)
Eliot Blennerhassettc1d70dd2011-12-22 13:38:50 +13001209 err = hpi_instream_query_format(h_stream, &hpi_format);
Takashi Iwaia91a0e72018-07-25 23:19:41 +02001210 if (!err && (hpi_to_alsa_formats[format] != INVALID_FORMAT))
Eldad Zack74c34ca2013-04-23 01:00:41 +02001211 formats |= pcm_format_to_bits(hpi_to_alsa_formats[format]);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001212 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001213 return formats;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001214}
1215
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001216static int snd_card_asihpi_capture_open(struct snd_pcm_substream *substream)
1217{
1218 struct snd_pcm_runtime *runtime = substream->runtime;
1219 struct snd_card_asihpi *card = snd_pcm_substream_chip(substream);
1220 struct snd_card_asihpi_pcm *dpcm;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001221 struct snd_pcm_hardware snd_card_asihpi_capture;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001222 int err;
1223
1224 dpcm = kzalloc(sizeof(*dpcm), GFP_KERNEL);
1225 if (dpcm == NULL)
1226 return -ENOMEM;
1227
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13001228 snd_printdd("capture open adapter %d stream %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001229 card->hpi->adapter->index, substream->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001230
1231 err = hpi_handle_error(
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001232 hpi_instream_open(card->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001233 substream->number, &dpcm->h_stream));
1234 if (err)
1235 kfree(dpcm);
1236 if (err == HPI_ERROR_OBJ_ALREADY_OPEN)
1237 return -EBUSY;
1238 if (err)
1239 return -EIO;
1240
Kees Cook394ca812017-10-04 17:53:40 -07001241 timer_setup(&dpcm->timer, snd_card_asihpi_timer_function, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001242 dpcm->substream = substream;
1243 runtime->private_data = dpcm;
1244 runtime->private_free = snd_card_asihpi_runtime_free;
1245
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001246 memset(&snd_card_asihpi_capture, 0, sizeof(snd_card_asihpi_capture));
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001247 if (!card->hpi->interrupt_mode) {
1248 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1249 snd_card_asihpi_capture.period_bytes_min = PERIOD_BYTES_MIN;
1250 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1251 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1252 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / PERIOD_BYTES_MIN;
1253 } else {
1254 size_t pbmin = card->update_interval_frames *
1255 card->out_max_chans;
1256 snd_card_asihpi_capture.buffer_bytes_max = BUFFER_BYTES_MAX;
1257 snd_card_asihpi_capture.period_bytes_min = pbmin;
1258 snd_card_asihpi_capture.period_bytes_max = BUFFER_BYTES_MAX / PERIODS_MIN;
1259 snd_card_asihpi_capture.periods_min = PERIODS_MIN;
1260 snd_card_asihpi_capture.periods_max = BUFFER_BYTES_MAX / pbmin;
1261 }
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001262 /* snd_card_asihpi_capture.fifo_size = 0; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001263 snd_card_asihpi_capture.channels_max = card->in_max_chans;
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13001264 snd_card_asihpi_capture.channels_min = card->in_min_chans;
Eliot Blennerhassett68d53392011-12-22 13:38:51 +13001265 snd_card_asihpi_capture.formats =
1266 snd_card_asihpi_capture_formats(card, dpcm->h_stream);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001267 snd_card_asihpi_pcm_samplerates(card, &snd_card_asihpi_capture);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001268 snd_card_asihpi_capture.info = SNDRV_PCM_INFO_INTERLEAVED |
1269 SNDRV_PCM_INFO_MMAP |
1270 SNDRV_PCM_INFO_MMAP_VALID;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001271
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001272 if (card->support_grouping)
1273 snd_card_asihpi_capture.info |= SNDRV_PCM_INFO_SYNC_START;
1274
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001275 runtime->hw = snd_card_asihpi_capture;
1276
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12001277 if (card->can_dma)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001278 err = snd_pcm_hw_constraint_pow2(runtime, 0,
1279 SNDRV_PCM_HW_PARAM_BUFFER_BYTES);
1280 if (err < 0)
1281 return err;
1282
1283 snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
1284 card->update_interval_frames);
1285 snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13001286 card->update_interval_frames, UINT_MAX);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001287
1288 snd_pcm_set_sync(substream);
1289
1290 return 0;
1291}
1292
1293static int snd_card_asihpi_capture_close(struct snd_pcm_substream *substream)
1294{
1295 struct snd_card_asihpi_pcm *dpcm = substream->runtime->private_data;
1296
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001297 hpi_handle_error(hpi_instream_close(dpcm->h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001298 return 0;
1299}
1300
Julia Lawall6769e9882016-09-02 00:13:10 +02001301static const struct snd_pcm_ops snd_card_asihpi_capture_mmap_ops = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001302 .open = snd_card_asihpi_capture_open,
1303 .close = snd_card_asihpi_capture_close,
1304 .ioctl = snd_card_asihpi_capture_ioctl,
1305 .hw_params = snd_card_asihpi_pcm_hw_params,
1306 .hw_free = snd_card_asihpi_hw_free,
1307 .prepare = snd_card_asihpi_capture_prepare,
1308 .trigger = snd_card_asihpi_trigger,
1309 .pointer = snd_card_asihpi_capture_pointer,
1310};
1311
Bill Pembertone23e7a12012-12-06 12:35:10 -05001312static int snd_card_asihpi_pcm_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001313{
1314 struct snd_pcm *pcm;
1315 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001316 u16 num_instreams, num_outstreams, x16;
1317 u32 x32;
1318
1319 err = hpi_adapter_get_info(asihpi->hpi->adapter->index,
1320 &num_outstreams, &num_instreams,
1321 &x16, &x32, &x16);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001322
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001323 err = snd_pcm_new(asihpi->card, "Asihpi PCM", device,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13001324 num_outstreams, num_instreams, &pcm);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001325 if (err < 0)
1326 return err;
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001327
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001328 /* pointer to ops struct is stored, dont change ops afterwards! */
Dan Carpenterc687c9b2014-03-31 10:49:15 +03001329 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1330 &snd_card_asihpi_playback_mmap_ops);
1331 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1332 &snd_card_asihpi_capture_mmap_ops);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001333
1334 pcm->private_data = asihpi;
1335 pcm->info_flags = 0;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001336 strcpy(pcm->name, "Asihpi PCM");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001337
1338 /*? do we want to emulate MMAP for non-BBM cards?
1339 Jack doesn't work with ALSAs MMAP emulation - WHY NOT? */
1340 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1341 snd_dma_pci_data(asihpi->pci),
1342 64*1024, BUFFER_BYTES_MAX);
1343
1344 return 0;
1345}
1346
1347/***************************** MIXER CONTROLS ****************/
1348struct hpi_control {
1349 u32 h_control;
1350 u16 control_type;
1351 u16 src_node_type;
1352 u16 src_node_index;
1353 u16 dst_node_type;
1354 u16 dst_node_index;
1355 u16 band;
Takashi Iwai975cc022013-06-28 11:56:49 +02001356 char name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; /* copied to snd_ctl_elem_id.name[44]; */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001357};
1358
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001359static const char * const asihpi_tuner_band_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001360 "invalid",
1361 "AM",
1362 "FM mono",
1363 "TV NTSC-M",
1364 "FM stereo",
1365 "AUX",
1366 "TV PAL BG",
1367 "TV PAL I",
1368 "TV PAL DK",
1369 "TV SECAM",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001370 "TV DAB",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001371};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001372/* Number of strings must match the enumerations for HPI_TUNER_BAND in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001373compile_time_assert(
1374 (ARRAY_SIZE(asihpi_tuner_band_names) ==
1375 (HPI_TUNER_BAND_LAST+1)),
1376 assert_tuner_band_names_size);
1377
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001378static const char * const asihpi_src_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001379 "no source",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001380 "PCM",
1381 "Line",
1382 "Digital",
1383 "Tuner",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001384 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001385 "Clock",
1386 "Bitstream",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001387 "Mic",
1388 "Net",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001389 "Analog",
1390 "Adapter",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001391 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001392 "Internal",
1393 "AVB",
1394 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001395};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001396/* Number of strings must match the enumerations for HPI_SOURCENODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001397compile_time_assert(
1398 (ARRAY_SIZE(asihpi_src_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001399 (HPI_SOURCENODE_LAST_INDEX-HPI_SOURCENODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001400 assert_src_names_size);
1401
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001402static const char * const asihpi_dst_names[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001403 "no destination",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001404 "PCM",
1405 "Line",
1406 "Digital",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001407 "RF",
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001408 "Speaker",
Eliot Blennerhassettc8306132011-07-22 15:53:00 +12001409 "Net",
1410 "Analog",
1411 "RTP",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001412 "AVB",
1413 "Internal",
1414 "BLU-Link"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001415};
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13001416/* Number of strings must match the enumerations for HPI_DESTNODES in hpi.h */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001417compile_time_assert(
1418 (ARRAY_SIZE(asihpi_dst_names) ==
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12001419 (HPI_DESTNODE_LAST_INDEX-HPI_DESTNODE_NONE+1)),
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001420 assert_dst_names_size);
1421
1422static inline int ctl_add(struct snd_card *card, struct snd_kcontrol_new *ctl,
1423 struct snd_card_asihpi *asihpi)
1424{
1425 int err;
1426
1427 err = snd_ctl_add(card, snd_ctl_new1(ctl, asihpi));
1428 if (err < 0)
1429 return err;
1430 else if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13001431 dev_info(&asihpi->pci->dev, "added %s(%d)\n", ctl->name, ctl->index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001432
1433 return 0;
1434}
1435
1436/* Convert HPI control name and location into ALSA control name */
1437static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
1438 struct hpi_control *hpi_ctl,
1439 char *name)
1440{
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001441 char *dir;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001442 memset(snd_control, 0, sizeof(*snd_control));
1443 snd_control->name = hpi_ctl->name;
1444 snd_control->private_value = hpi_ctl->h_control;
1445 snd_control->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1446 snd_control->index = 0;
1447
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001448 if (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE == HPI_SOURCENODE_CLOCK_SOURCE)
1449 dir = ""; /* clock is neither capture nor playback */
1450 else if (hpi_ctl->dst_node_type + HPI_DESTNODE_NONE == HPI_DESTNODE_ISTREAM)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001451 dir = "Capture "; /* On or towards a PCM capture destination*/
1452 else if ((hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
1453 (!hpi_ctl->dst_node_type))
1454 dir = "Capture "; /* On a source node that is not PCM playback */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001455 else if (hpi_ctl->src_node_type &&
1456 (hpi_ctl->src_node_type + HPI_SOURCENODE_NONE != HPI_SOURCENODE_OSTREAM) &&
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001457 (hpi_ctl->dst_node_type))
1458 dir = "Monitor Playback "; /* Between an input and an output */
1459 else
1460 dir = "Playback "; /* PCM Playback source, or output node */
1461
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001462 if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
Eliot Blennerhassett550ac6b2011-04-05 20:55:41 +12001463 sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001464 asihpi_src_names[hpi_ctl->src_node_type],
1465 hpi_ctl->src_node_index,
1466 asihpi_dst_names[hpi_ctl->dst_node_type],
1467 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001468 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001469 else if (hpi_ctl->dst_node_type) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001470 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001471 asihpi_dst_names[hpi_ctl->dst_node_type],
1472 hpi_ctl->dst_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001473 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001474 } else {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001475 sprintf(hpi_ctl->name, "%s %d %s%s",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001476 asihpi_src_names[hpi_ctl->src_node_type],
1477 hpi_ctl->src_node_index,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001478 dir, name);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001479 }
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001480 /* printk(KERN_INFO "Adding %s %d to %d ", hpi_ctl->name,
1481 hpi_ctl->wSrcNodeType, hpi_ctl->wDstNodeType); */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001482}
1483
1484/*------------------------------------------------------------
1485 Volume controls
1486 ------------------------------------------------------------*/
1487#define VOL_STEP_mB 1
1488static int snd_asihpi_volume_info(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_info *uinfo)
1490{
1491 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001492 u32 count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001493 u16 err;
1494 /* native gains are in millibels */
1495 short min_gain_mB;
1496 short max_gain_mB;
1497 short step_gain_mB;
1498
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001499 err = hpi_volume_query_range(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001500 &min_gain_mB, &max_gain_mB, &step_gain_mB);
1501 if (err) {
1502 max_gain_mB = 0;
1503 min_gain_mB = -10000;
1504 step_gain_mB = VOL_STEP_mB;
1505 }
1506
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001507 err = hpi_meter_query_channels(h_control, &count);
1508 if (err)
1509 count = HPI_MAX_CHANNELS;
1510
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001511 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13001512 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001513 uinfo->value.integer.min = min_gain_mB / VOL_STEP_mB;
1514 uinfo->value.integer.max = max_gain_mB / VOL_STEP_mB;
1515 uinfo->value.integer.step = step_gain_mB / VOL_STEP_mB;
1516 return 0;
1517}
1518
1519static int snd_asihpi_volume_get(struct snd_kcontrol *kcontrol,
1520 struct snd_ctl_elem_value *ucontrol)
1521{
1522 u32 h_control = kcontrol->private_value;
1523 short an_gain_mB[HPI_MAX_CHANNELS];
1524
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001525 hpi_handle_error(hpi_volume_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001526 ucontrol->value.integer.value[0] = an_gain_mB[0] / VOL_STEP_mB;
1527 ucontrol->value.integer.value[1] = an_gain_mB[1] / VOL_STEP_mB;
1528
1529 return 0;
1530}
1531
1532static int snd_asihpi_volume_put(struct snd_kcontrol *kcontrol,
1533 struct snd_ctl_elem_value *ucontrol)
1534{
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001535 u32 h_control = kcontrol->private_value;
1536 short an_gain_mB[HPI_MAX_CHANNELS];
1537
1538 an_gain_mB[0] =
1539 (ucontrol->value.integer.value[0]) * VOL_STEP_mB;
1540 an_gain_mB[1] =
1541 (ucontrol->value.integer.value[1]) * VOL_STEP_mB;
1542 /* change = asihpi->mixer_volume[addr][0] != left ||
1543 asihpi->mixer_volume[addr][1] != right;
1544 */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001545 hpi_handle_error(hpi_volume_set_gain(h_control, an_gain_mB));
Hariprasad Kelambb26e0c2019-07-05 08:27:33 +05301546 return 1;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001547}
1548
1549static const DECLARE_TLV_DB_SCALE(db_scale_100, -10000, VOL_STEP_mB, 0);
1550
Takashi Iwai000477a2011-07-22 07:57:44 +02001551#define snd_asihpi_volume_mute_info snd_ctl_boolean_mono_info
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001552
1553static int snd_asihpi_volume_mute_get(struct snd_kcontrol *kcontrol,
1554 struct snd_ctl_elem_value *ucontrol)
1555{
1556 u32 h_control = kcontrol->private_value;
1557 u32 mute;
1558
1559 hpi_handle_error(hpi_volume_get_mute(h_control, &mute));
1560 ucontrol->value.integer.value[0] = mute ? 0 : 1;
1561
1562 return 0;
1563}
1564
1565static int snd_asihpi_volume_mute_put(struct snd_kcontrol *kcontrol,
1566 struct snd_ctl_elem_value *ucontrol)
1567{
1568 u32 h_control = kcontrol->private_value;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001569 /* HPI currently only supports all or none muting of multichannel volume
1570 ALSA Switch element has opposite sense to HPI mute: on==unmuted, off=muted
1571 */
1572 int mute = ucontrol->value.integer.value[0] ? 0 : HPI_BITMASK_ALL_CHANNELS;
1573 hpi_handle_error(hpi_volume_set_mute(h_control, mute));
Hariprasad Kelambb26e0c2019-07-05 08:27:33 +05301574 return 1;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001575}
1576
Bill Pembertone23e7a12012-12-06 12:35:10 -05001577static int snd_asihpi_volume_add(struct snd_card_asihpi *asihpi,
1578 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001579{
1580 struct snd_card *card = asihpi->card;
1581 struct snd_kcontrol_new snd_control;
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001582 int err;
1583 u32 mute;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001584
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001585 asihpi_ctl_init(&snd_control, hpi_ctl, "Volume");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001586 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1587 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1588 snd_control.info = snd_asihpi_volume_info;
1589 snd_control.get = snd_asihpi_volume_get;
1590 snd_control.put = snd_asihpi_volume_put;
1591 snd_control.tlv.p = db_scale_100;
1592
Eliot Blennerhassettfe0aa88e2011-07-22 15:53:03 +12001593 err = ctl_add(card, &snd_control, asihpi);
1594 if (err)
1595 return err;
1596
1597 if (hpi_volume_get_mute(hpi_ctl->h_control, &mute) == 0) {
1598 asihpi_ctl_init(&snd_control, hpi_ctl, "Switch");
1599 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1600 snd_control.info = snd_asihpi_volume_mute_info;
1601 snd_control.get = snd_asihpi_volume_mute_get;
1602 snd_control.put = snd_asihpi_volume_mute_put;
1603 err = ctl_add(card, &snd_control, asihpi);
1604 }
1605 return err;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001606}
1607
1608/*------------------------------------------------------------
1609 Level controls
1610 ------------------------------------------------------------*/
1611static int snd_asihpi_level_info(struct snd_kcontrol *kcontrol,
1612 struct snd_ctl_elem_info *uinfo)
1613{
1614 u32 h_control = kcontrol->private_value;
1615 u16 err;
1616 short min_gain_mB;
1617 short max_gain_mB;
1618 short step_gain_mB;
1619
1620 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001621 hpi_level_query_range(h_control, &min_gain_mB,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001622 &max_gain_mB, &step_gain_mB);
1623 if (err) {
1624 max_gain_mB = 2400;
1625 min_gain_mB = -1000;
1626 step_gain_mB = 100;
1627 }
1628
1629 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1630 uinfo->count = 2;
1631 uinfo->value.integer.min = min_gain_mB / HPI_UNITS_PER_dB;
1632 uinfo->value.integer.max = max_gain_mB / HPI_UNITS_PER_dB;
1633 uinfo->value.integer.step = step_gain_mB / HPI_UNITS_PER_dB;
1634 return 0;
1635}
1636
1637static int snd_asihpi_level_get(struct snd_kcontrol *kcontrol,
1638 struct snd_ctl_elem_value *ucontrol)
1639{
1640 u32 h_control = kcontrol->private_value;
1641 short an_gain_mB[HPI_MAX_CHANNELS];
1642
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001643 hpi_handle_error(hpi_level_get_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001644 ucontrol->value.integer.value[0] =
1645 an_gain_mB[0] / HPI_UNITS_PER_dB;
1646 ucontrol->value.integer.value[1] =
1647 an_gain_mB[1] / HPI_UNITS_PER_dB;
1648
1649 return 0;
1650}
1651
1652static int snd_asihpi_level_put(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol)
1654{
1655 int change;
1656 u32 h_control = kcontrol->private_value;
1657 short an_gain_mB[HPI_MAX_CHANNELS];
1658
1659 an_gain_mB[0] =
1660 (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
1661 an_gain_mB[1] =
1662 (ucontrol->value.integer.value[1]) * HPI_UNITS_PER_dB;
1663 /* change = asihpi->mixer_level[addr][0] != left ||
1664 asihpi->mixer_level[addr][1] != right;
1665 */
1666 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001667 hpi_handle_error(hpi_level_set_gain(h_control, an_gain_mB));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001668 return change;
1669}
1670
1671static const DECLARE_TLV_DB_SCALE(db_scale_level, -1000, 100, 0);
1672
Bill Pembertone23e7a12012-12-06 12:35:10 -05001673static int snd_asihpi_level_add(struct snd_card_asihpi *asihpi,
1674 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001675{
1676 struct snd_card *card = asihpi->card;
1677 struct snd_kcontrol_new snd_control;
1678
1679 /* can't use 'volume' cos some nodes have volume as well */
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001680 asihpi_ctl_init(&snd_control, hpi_ctl, "Level");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001681 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
1682 SNDRV_CTL_ELEM_ACCESS_TLV_READ;
1683 snd_control.info = snd_asihpi_level_info;
1684 snd_control.get = snd_asihpi_level_get;
1685 snd_control.put = snd_asihpi_level_put;
1686 snd_control.tlv.p = db_scale_level;
1687
1688 return ctl_add(card, &snd_control, asihpi);
1689}
1690
1691/*------------------------------------------------------------
1692 AESEBU controls
1693 ------------------------------------------------------------*/
1694
1695/* AESEBU format */
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001696static const char * const asihpi_aesebu_format_names[] = {
1697 "N/A", "S/PDIF", "AES/EBU" };
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001698
1699static int snd_asihpi_aesebu_format_info(struct snd_kcontrol *kcontrol,
1700 struct snd_ctl_elem_info *uinfo)
1701{
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001702 return snd_ctl_enum_info(uinfo, 1, 3, asihpi_aesebu_format_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001703}
1704
1705static int snd_asihpi_aesebu_format_get(struct snd_kcontrol *kcontrol,
1706 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001707 u16 (*func)(u32, u16 *))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001708{
1709 u32 h_control = kcontrol->private_value;
1710 u16 source, err;
1711
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001712 err = func(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001713
1714 /* default to N/A */
1715 ucontrol->value.enumerated.item[0] = 0;
1716 /* return success but set the control to N/A */
1717 if (err)
1718 return 0;
1719 if (source == HPI_AESEBU_FORMAT_SPDIF)
1720 ucontrol->value.enumerated.item[0] = 1;
1721 if (source == HPI_AESEBU_FORMAT_AESEBU)
1722 ucontrol->value.enumerated.item[0] = 2;
1723
1724 return 0;
1725}
1726
1727static int snd_asihpi_aesebu_format_put(struct snd_kcontrol *kcontrol,
1728 struct snd_ctl_elem_value *ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001729 u16 (*func)(u32, u16))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001730{
1731 u32 h_control = kcontrol->private_value;
1732
1733 /* default to S/PDIF */
1734 u16 source = HPI_AESEBU_FORMAT_SPDIF;
1735
1736 if (ucontrol->value.enumerated.item[0] == 1)
1737 source = HPI_AESEBU_FORMAT_SPDIF;
1738 if (ucontrol->value.enumerated.item[0] == 2)
1739 source = HPI_AESEBU_FORMAT_AESEBU;
1740
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001741 if (func(h_control, source) != 0)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001742 return -EINVAL;
1743
1744 return 1;
1745}
1746
1747static int snd_asihpi_aesebu_rx_format_get(struct snd_kcontrol *kcontrol,
1748 struct snd_ctl_elem_value *ucontrol) {
1749 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001750 hpi_aesebu_receiver_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001751}
1752
1753static int snd_asihpi_aesebu_rx_format_put(struct snd_kcontrol *kcontrol,
1754 struct snd_ctl_elem_value *ucontrol) {
1755 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001756 hpi_aesebu_receiver_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001757}
1758
1759static int snd_asihpi_aesebu_rxstatus_info(struct snd_kcontrol *kcontrol,
1760 struct snd_ctl_elem_info *uinfo)
1761{
1762 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1763 uinfo->count = 1;
1764
1765 uinfo->value.integer.min = 0;
1766 uinfo->value.integer.max = 0X1F;
1767 uinfo->value.integer.step = 1;
1768
1769 return 0;
1770}
1771
1772static int snd_asihpi_aesebu_rxstatus_get(struct snd_kcontrol *kcontrol,
1773 struct snd_ctl_elem_value *ucontrol) {
1774
1775 u32 h_control = kcontrol->private_value;
1776 u16 status;
1777
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001778 hpi_handle_error(hpi_aesebu_receiver_get_error_status(
1779 h_control, &status));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001780 ucontrol->value.integer.value[0] = status;
1781 return 0;
1782}
1783
Bill Pembertone23e7a12012-12-06 12:35:10 -05001784static int snd_asihpi_aesebu_rx_add(struct snd_card_asihpi *asihpi,
1785 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001786{
1787 struct snd_card *card = asihpi->card;
1788 struct snd_kcontrol_new snd_control;
1789
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001790 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001791 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1792 snd_control.info = snd_asihpi_aesebu_format_info;
1793 snd_control.get = snd_asihpi_aesebu_rx_format_get;
1794 snd_control.put = snd_asihpi_aesebu_rx_format_put;
1795
1796
1797 if (ctl_add(card, &snd_control, asihpi) < 0)
1798 return -EINVAL;
1799
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001800 asihpi_ctl_init(&snd_control, hpi_ctl, "Status");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001801 snd_control.access =
1802 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
1803 snd_control.info = snd_asihpi_aesebu_rxstatus_info;
1804 snd_control.get = snd_asihpi_aesebu_rxstatus_get;
1805
1806 return ctl_add(card, &snd_control, asihpi);
1807}
1808
1809static int snd_asihpi_aesebu_tx_format_get(struct snd_kcontrol *kcontrol,
1810 struct snd_ctl_elem_value *ucontrol) {
1811 return snd_asihpi_aesebu_format_get(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001812 hpi_aesebu_transmitter_get_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001813}
1814
1815static int snd_asihpi_aesebu_tx_format_put(struct snd_kcontrol *kcontrol,
1816 struct snd_ctl_elem_value *ucontrol) {
1817 return snd_asihpi_aesebu_format_put(kcontrol, ucontrol,
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001818 hpi_aesebu_transmitter_set_format);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001819}
1820
1821
Bill Pembertone23e7a12012-12-06 12:35:10 -05001822static int snd_asihpi_aesebu_tx_add(struct snd_card_asihpi *asihpi,
1823 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001824{
1825 struct snd_card *card = asihpi->card;
1826 struct snd_kcontrol_new snd_control;
1827
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13001828 asihpi_ctl_init(&snd_control, hpi_ctl, "Format");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001829 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
1830 snd_control.info = snd_asihpi_aesebu_format_info;
1831 snd_control.get = snd_asihpi_aesebu_tx_format_get;
1832 snd_control.put = snd_asihpi_aesebu_tx_format_put;
1833
1834 return ctl_add(card, &snd_control, asihpi);
1835}
1836
1837/*------------------------------------------------------------
1838 Tuner controls
1839 ------------------------------------------------------------*/
1840
1841/* Gain */
1842
1843static int snd_asihpi_tuner_gain_info(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_info *uinfo)
1845{
1846 u32 h_control = kcontrol->private_value;
1847 u16 err;
1848 short idx;
1849 u16 gain_range[3];
1850
1851 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001852 err = hpi_tuner_query_gain(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001853 idx, &gain_range[idx]);
1854 if (err != 0)
1855 return err;
1856 }
1857
1858 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1859 uinfo->count = 1;
1860 uinfo->value.integer.min = ((int)gain_range[0]) / HPI_UNITS_PER_dB;
1861 uinfo->value.integer.max = ((int)gain_range[1]) / HPI_UNITS_PER_dB;
1862 uinfo->value.integer.step = ((int) gain_range[2]) / HPI_UNITS_PER_dB;
1863 return 0;
1864}
1865
1866static int snd_asihpi_tuner_gain_get(struct snd_kcontrol *kcontrol,
1867 struct snd_ctl_elem_value *ucontrol)
1868{
1869 /*
1870 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1871 */
1872 u32 h_control = kcontrol->private_value;
1873 short gain;
1874
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001875 hpi_handle_error(hpi_tuner_get_gain(h_control, &gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001876 ucontrol->value.integer.value[0] = gain / HPI_UNITS_PER_dB;
1877
1878 return 0;
1879}
1880
1881static int snd_asihpi_tuner_gain_put(struct snd_kcontrol *kcontrol,
1882 struct snd_ctl_elem_value *ucontrol)
1883{
1884 /*
1885 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1886 */
1887 u32 h_control = kcontrol->private_value;
1888 short gain;
1889
1890 gain = (ucontrol->value.integer.value[0]) * HPI_UNITS_PER_dB;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001891 hpi_handle_error(hpi_tuner_set_gain(h_control, gain));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001892
1893 return 1;
1894}
1895
1896/* Band */
1897
1898static int asihpi_tuner_band_query(struct snd_kcontrol *kcontrol,
1899 u16 *band_list, u32 len) {
1900 u32 h_control = kcontrol->private_value;
1901 u16 err = 0;
1902 u32 i;
1903
1904 for (i = 0; i < len; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001905 err = hpi_tuner_query_band(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001906 h_control, i, &band_list[i]);
1907 if (err != 0)
1908 break;
1909 }
1910
1911 if (err && (err != HPI_ERROR_INVALID_OBJ_INDEX))
1912 return -EIO;
1913
1914 return i;
1915}
1916
1917static int snd_asihpi_tuner_band_info(struct snd_kcontrol *kcontrol,
1918 struct snd_ctl_elem_info *uinfo)
1919{
1920 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1921 int num_bands = 0;
1922
1923 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1924 HPI_TUNER_BAND_LAST);
1925
1926 if (num_bands < 0)
1927 return num_bands;
1928
Takashi Iwai30d0ae42014-10-20 18:15:50 +02001929 return snd_ctl_enum_info(uinfo, 1, num_bands, asihpi_tuner_band_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001930}
1931
1932static int snd_asihpi_tuner_band_get(struct snd_kcontrol *kcontrol,
1933 struct snd_ctl_elem_value *ucontrol)
1934{
1935 u32 h_control = kcontrol->private_value;
1936 /*
1937 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1938 */
1939 u16 band, idx;
1940 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1941 u32 num_bands = 0;
1942
1943 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1944 HPI_TUNER_BAND_LAST);
1945
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001946 hpi_handle_error(hpi_tuner_get_band(h_control, &band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001947
1948 ucontrol->value.enumerated.item[0] = -1;
1949 for (idx = 0; idx < HPI_TUNER_BAND_LAST; idx++)
1950 if (tuner_bands[idx] == band) {
1951 ucontrol->value.enumerated.item[0] = idx;
1952 break;
1953 }
1954
1955 return 0;
1956}
1957
1958static int snd_asihpi_tuner_band_put(struct snd_kcontrol *kcontrol,
1959 struct snd_ctl_elem_value *ucontrol)
1960{
1961 /*
1962 struct snd_card_asihpi *asihpi = snd_kcontrol_chip(kcontrol);
1963 */
1964 u32 h_control = kcontrol->private_value;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001965 unsigned int idx;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001966 u16 band;
1967 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1968 u32 num_bands = 0;
1969
1970 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1971 HPI_TUNER_BAND_LAST);
1972
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03001973 idx = ucontrol->value.enumerated.item[0];
1974 if (idx >= ARRAY_SIZE(tuner_bands))
1975 idx = ARRAY_SIZE(tuner_bands) - 1;
1976 band = tuner_bands[idx];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13001977 hpi_handle_error(hpi_tuner_set_band(h_control, band));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02001978
1979 return 1;
1980}
1981
1982/* Freq */
1983
1984static int snd_asihpi_tuner_freq_info(struct snd_kcontrol *kcontrol,
1985 struct snd_ctl_elem_info *uinfo)
1986{
1987 u32 h_control = kcontrol->private_value;
1988 u16 err;
1989 u16 tuner_bands[HPI_TUNER_BAND_LAST];
1990 u16 num_bands = 0, band_iter, idx;
1991 u32 freq_range[3], temp_freq_range[3];
1992
1993 num_bands = asihpi_tuner_band_query(kcontrol, tuner_bands,
1994 HPI_TUNER_BAND_LAST);
1995
1996 freq_range[0] = INT_MAX;
1997 freq_range[1] = 0;
1998 freq_range[2] = INT_MAX;
1999
2000 for (band_iter = 0; band_iter < num_bands; band_iter++) {
2001 for (idx = 0; idx < 3; idx++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002002 err = hpi_tuner_query_frequency(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002003 idx, tuner_bands[band_iter],
2004 &temp_freq_range[idx]);
2005 if (err != 0)
2006 return err;
2007 }
2008
2009 /* skip band with bogus stepping */
2010 if (temp_freq_range[2] <= 0)
2011 continue;
2012
2013 if (temp_freq_range[0] < freq_range[0])
2014 freq_range[0] = temp_freq_range[0];
2015 if (temp_freq_range[1] > freq_range[1])
2016 freq_range[1] = temp_freq_range[1];
2017 if (temp_freq_range[2] < freq_range[2])
2018 freq_range[2] = temp_freq_range[2];
2019 }
2020
2021 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2022 uinfo->count = 1;
2023 uinfo->value.integer.min = ((int)freq_range[0]);
2024 uinfo->value.integer.max = ((int)freq_range[1]);
2025 uinfo->value.integer.step = ((int)freq_range[2]);
2026 return 0;
2027}
2028
2029static int snd_asihpi_tuner_freq_get(struct snd_kcontrol *kcontrol,
2030 struct snd_ctl_elem_value *ucontrol)
2031{
2032 u32 h_control = kcontrol->private_value;
2033 u32 freq;
2034
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002035 hpi_handle_error(hpi_tuner_get_frequency(h_control, &freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002036 ucontrol->value.integer.value[0] = freq;
2037
2038 return 0;
2039}
2040
2041static int snd_asihpi_tuner_freq_put(struct snd_kcontrol *kcontrol,
2042 struct snd_ctl_elem_value *ucontrol)
2043{
2044 u32 h_control = kcontrol->private_value;
2045 u32 freq;
2046
2047 freq = ucontrol->value.integer.value[0];
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002048 hpi_handle_error(hpi_tuner_set_frequency(h_control, freq));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002049
2050 return 1;
2051}
2052
2053/* Tuner control group initializer */
Bill Pembertone23e7a12012-12-06 12:35:10 -05002054static int snd_asihpi_tuner_add(struct snd_card_asihpi *asihpi,
2055 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002056{
2057 struct snd_card *card = asihpi->card;
2058 struct snd_kcontrol_new snd_control;
2059
2060 snd_control.private_value = hpi_ctl->h_control;
2061 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2062
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002063 if (!hpi_tuner_get_gain(hpi_ctl->h_control, NULL)) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002064 asihpi_ctl_init(&snd_control, hpi_ctl, "Gain");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002065 snd_control.info = snd_asihpi_tuner_gain_info;
2066 snd_control.get = snd_asihpi_tuner_gain_get;
2067 snd_control.put = snd_asihpi_tuner_gain_put;
2068
2069 if (ctl_add(card, &snd_control, asihpi) < 0)
2070 return -EINVAL;
2071 }
2072
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002073 asihpi_ctl_init(&snd_control, hpi_ctl, "Band");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002074 snd_control.info = snd_asihpi_tuner_band_info;
2075 snd_control.get = snd_asihpi_tuner_band_get;
2076 snd_control.put = snd_asihpi_tuner_band_put;
2077
2078 if (ctl_add(card, &snd_control, asihpi) < 0)
2079 return -EINVAL;
2080
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002081 asihpi_ctl_init(&snd_control, hpi_ctl, "Freq");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002082 snd_control.info = snd_asihpi_tuner_freq_info;
2083 snd_control.get = snd_asihpi_tuner_freq_get;
2084 snd_control.put = snd_asihpi_tuner_freq_put;
2085
2086 return ctl_add(card, &snd_control, asihpi);
2087}
2088
2089/*------------------------------------------------------------
2090 Meter controls
2091 ------------------------------------------------------------*/
2092static int snd_asihpi_meter_info(struct snd_kcontrol *kcontrol,
2093 struct snd_ctl_elem_info *uinfo)
2094{
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002095 u32 h_control = kcontrol->private_value;
2096 u32 count;
2097 u16 err;
2098 err = hpi_meter_query_channels(h_control, &count);
2099 if (err)
2100 count = HPI_MAX_CHANNELS;
2101
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002102 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
Eliot Blennerhassettd4b06d22011-12-22 13:38:34 +13002103 uinfo->count = count;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002104 uinfo->value.integer.min = 0;
2105 uinfo->value.integer.max = 0x7FFFFFFF;
2106 return 0;
2107}
2108
2109/* linear values for 10dB steps */
2110static int log2lin[] = {
2111 0x7FFFFFFF, /* 0dB */
2112 679093956,
2113 214748365,
2114 67909396,
2115 21474837,
2116 6790940,
2117 2147484, /* -60dB */
2118 679094,
2119 214748, /* -80 */
2120 67909,
2121 21475, /* -100 */
2122 6791,
2123 2147,
2124 679,
2125 214,
2126 68,
2127 21,
2128 7,
2129 2
2130};
2131
2132static int snd_asihpi_meter_get(struct snd_kcontrol *kcontrol,
2133 struct snd_ctl_elem_value *ucontrol)
2134{
2135 u32 h_control = kcontrol->private_value;
2136 short an_gain_mB[HPI_MAX_CHANNELS], i;
2137 u16 err;
2138
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002139 err = hpi_meter_get_peak(h_control, an_gain_mB);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002140
2141 for (i = 0; i < HPI_MAX_CHANNELS; i++) {
2142 if (err) {
2143 ucontrol->value.integer.value[i] = 0;
2144 } else if (an_gain_mB[i] >= 0) {
2145 ucontrol->value.integer.value[i] =
2146 an_gain_mB[i] << 16;
2147 } else {
2148 /* -ve is log value in millibels < -60dB,
2149 * convert to (roughly!) linear,
2150 */
2151 ucontrol->value.integer.value[i] =
2152 log2lin[an_gain_mB[i] / -1000];
2153 }
2154 }
2155 return 0;
2156}
2157
Bill Pembertone23e7a12012-12-06 12:35:10 -05002158static int snd_asihpi_meter_add(struct snd_card_asihpi *asihpi,
2159 struct hpi_control *hpi_ctl, int subidx)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002160{
2161 struct snd_card *card = asihpi->card;
2162 struct snd_kcontrol_new snd_control;
2163
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002164 asihpi_ctl_init(&snd_control, hpi_ctl, "Meter");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002165 snd_control.access =
2166 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2167 snd_control.info = snd_asihpi_meter_info;
2168 snd_control.get = snd_asihpi_meter_get;
2169
2170 snd_control.index = subidx;
2171
2172 return ctl_add(card, &snd_control, asihpi);
2173}
2174
2175/*------------------------------------------------------------
2176 Multiplexer controls
2177 ------------------------------------------------------------*/
2178static int snd_card_asihpi_mux_count_sources(struct snd_kcontrol *snd_control)
2179{
2180 u32 h_control = snd_control->private_value;
2181 struct hpi_control hpi_ctl;
2182 int s, err;
2183 for (s = 0; s < 32; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002184 err = hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002185 &hpi_ctl.
2186 src_node_type,
2187 &hpi_ctl.
2188 src_node_index);
2189 if (err)
2190 break;
2191 }
2192 return s;
2193}
2194
2195static int snd_asihpi_mux_info(struct snd_kcontrol *kcontrol,
2196 struct snd_ctl_elem_info *uinfo)
2197{
2198 int err;
2199 u16 src_node_type, src_node_index;
2200 u32 h_control = kcontrol->private_value;
2201
2202 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2203 uinfo->count = 1;
2204 uinfo->value.enumerated.items =
2205 snd_card_asihpi_mux_count_sources(kcontrol);
2206
2207 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2208 uinfo->value.enumerated.item =
2209 uinfo->value.enumerated.items - 1;
2210
2211 err =
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002212 hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002213 uinfo->value.enumerated.item,
2214 &src_node_type, &src_node_index);
2215
2216 sprintf(uinfo->value.enumerated.name, "%s %d",
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002217 asihpi_src_names[src_node_type - HPI_SOURCENODE_NONE],
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002218 src_node_index);
2219 return 0;
2220}
2221
2222static int snd_asihpi_mux_get(struct snd_kcontrol *kcontrol,
2223 struct snd_ctl_elem_value *ucontrol)
2224{
2225 u32 h_control = kcontrol->private_value;
2226 u16 source_type, source_index;
2227 u16 src_node_type, src_node_index;
2228 int s;
2229
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002230 hpi_handle_error(hpi_multiplexer_get_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002231 &source_type, &source_index));
2232 /* Should cache this search result! */
2233 for (s = 0; s < 256; s++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002234 if (hpi_multiplexer_query_source(h_control, s,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002235 &src_node_type, &src_node_index))
2236 break;
2237
2238 if ((source_type == src_node_type)
2239 && (source_index == src_node_index)) {
2240 ucontrol->value.enumerated.item[0] = s;
2241 return 0;
2242 }
2243 }
2244 snd_printd(KERN_WARNING
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002245 "Control %x failed to match mux source %hu %hu\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002246 h_control, source_type, source_index);
2247 ucontrol->value.enumerated.item[0] = 0;
2248 return 0;
2249}
2250
2251static int snd_asihpi_mux_put(struct snd_kcontrol *kcontrol,
2252 struct snd_ctl_elem_value *ucontrol)
2253{
2254 int change;
2255 u32 h_control = kcontrol->private_value;
2256 u16 source_type, source_index;
2257 u16 e;
2258
2259 change = 1;
2260
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002261 e = hpi_multiplexer_query_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002262 ucontrol->value.enumerated.item[0],
2263 &source_type, &source_index);
2264 if (!e)
2265 hpi_handle_error(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002266 hpi_multiplexer_set_source(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002267 source_type, source_index));
2268 return change;
2269}
2270
2271
Bill Pembertone23e7a12012-12-06 12:35:10 -05002272static int snd_asihpi_mux_add(struct snd_card_asihpi *asihpi,
2273 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002274{
2275 struct snd_card *card = asihpi->card;
2276 struct snd_kcontrol_new snd_control;
2277
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002278 asihpi_ctl_init(&snd_control, hpi_ctl, "Route");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002279 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2280 snd_control.info = snd_asihpi_mux_info;
2281 snd_control.get = snd_asihpi_mux_get;
2282 snd_control.put = snd_asihpi_mux_put;
2283
2284 return ctl_add(card, &snd_control, asihpi);
2285
2286}
2287
2288/*------------------------------------------------------------
2289 Channel mode controls
2290 ------------------------------------------------------------*/
2291static int snd_asihpi_cmode_info(struct snd_kcontrol *kcontrol,
2292 struct snd_ctl_elem_info *uinfo)
2293{
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002294 static const char * const mode_names[HPI_CHANNEL_MODE_LAST + 1] = {
2295 "invalid",
2296 "Normal", "Swap",
2297 "From Left", "From Right",
2298 "To Left", "To Right"
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002299 };
2300
2301 u32 h_control = kcontrol->private_value;
2302 u16 mode;
2303 int i;
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002304 const char *mapped_names[6];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002305 int valid_modes = 0;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002306
2307 /* HPI channel mode values can be from 1 to 6
2308 Some adapters only support a contiguous subset
2309 */
2310 for (i = 0; i < HPI_CHANNEL_MODE_LAST; i++)
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002311 if (!hpi_channel_mode_query_mode(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002312 h_control, i, &mode)) {
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002313 mapped_names[valid_modes] = mode_names[mode];
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002314 valid_modes++;
2315 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002316
Takashi Iwai74eeb142012-01-09 18:26:05 +01002317 if (!valid_modes)
2318 return -EINVAL;
2319
Takashi Iwai30d0ae42014-10-20 18:15:50 +02002320 return snd_ctl_enum_info(uinfo, 1, valid_modes, mapped_names);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002321}
2322
2323static int snd_asihpi_cmode_get(struct snd_kcontrol *kcontrol,
2324 struct snd_ctl_elem_value *ucontrol)
2325{
2326 u32 h_control = kcontrol->private_value;
2327 u16 mode;
2328
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002329 if (hpi_channel_mode_get(h_control, &mode))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002330 mode = 1;
2331
2332 ucontrol->value.enumerated.item[0] = mode - 1;
2333
2334 return 0;
2335}
2336
2337static int snd_asihpi_cmode_put(struct snd_kcontrol *kcontrol,
2338 struct snd_ctl_elem_value *ucontrol)
2339{
2340 int change;
2341 u32 h_control = kcontrol->private_value;
2342
2343 change = 1;
2344
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002345 hpi_handle_error(hpi_channel_mode_set(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002346 ucontrol->value.enumerated.item[0] + 1));
2347 return change;
2348}
2349
2350
Bill Pembertone23e7a12012-12-06 12:35:10 -05002351static int snd_asihpi_cmode_add(struct snd_card_asihpi *asihpi,
2352 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002353{
2354 struct snd_card *card = asihpi->card;
2355 struct snd_kcontrol_new snd_control;
2356
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002357 asihpi_ctl_init(&snd_control, hpi_ctl, "Mode");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002358 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
2359 snd_control.info = snd_asihpi_cmode_info;
2360 snd_control.get = snd_asihpi_cmode_get;
2361 snd_control.put = snd_asihpi_cmode_put;
2362
2363 return ctl_add(card, &snd_control, asihpi);
2364}
2365
2366/*------------------------------------------------------------
2367 Sampleclock source controls
2368 ------------------------------------------------------------*/
Krzysztof Kozlowski46d212c2015-03-24 11:48:24 +01002369static const char * const sampleclock_sources[] = {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002370 "N/A", "Local PLL", "Digital Sync", "Word External", "Word Header",
2371 "SMPTE", "Digital1", "Auto", "Network", "Invalid",
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002372 "Prev Module", "BLU-Link",
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002373 "Digital2", "Digital3", "Digital4", "Digital5",
2374 "Digital6", "Digital7", "Digital8"};
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002375
Eliot Blennerhassett3872f192014-11-20 16:22:49 +13002376 /* Number of strings must match expected enumerated values */
2377 compile_time_assert(
2378 (ARRAY_SIZE(sampleclock_sources) == MAX_CLOCKSOURCES),
2379 assert_sampleclock_sources_size);
2380
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002381static int snd_asihpi_clksrc_info(struct snd_kcontrol *kcontrol,
2382 struct snd_ctl_elem_info *uinfo)
2383{
2384 struct snd_card_asihpi *asihpi =
2385 (struct snd_card_asihpi *)(kcontrol->private_data);
2386 struct clk_cache *clkcache = &asihpi->cc;
2387 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2388 uinfo->count = 1;
2389 uinfo->value.enumerated.items = clkcache->count;
2390
2391 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
2392 uinfo->value.enumerated.item =
2393 uinfo->value.enumerated.items - 1;
2394
2395 strcpy(uinfo->value.enumerated.name,
2396 clkcache->s[uinfo->value.enumerated.item].name);
2397 return 0;
2398}
2399
2400static int snd_asihpi_clksrc_get(struct snd_kcontrol *kcontrol,
2401 struct snd_ctl_elem_value *ucontrol)
2402{
2403 struct snd_card_asihpi *asihpi =
2404 (struct snd_card_asihpi *)(kcontrol->private_data);
2405 struct clk_cache *clkcache = &asihpi->cc;
2406 u32 h_control = kcontrol->private_value;
2407 u16 source, srcindex = 0;
2408 int i;
2409
2410 ucontrol->value.enumerated.item[0] = 0;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002411 if (hpi_sample_clock_get_source(h_control, &source))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002412 source = 0;
2413
2414 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002415 if (hpi_sample_clock_get_source_index(h_control, &srcindex))
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002416 srcindex = 0;
2417
2418 for (i = 0; i < clkcache->count; i++)
2419 if ((clkcache->s[i].source == source) &&
2420 (clkcache->s[i].index == srcindex))
2421 break;
2422
2423 ucontrol->value.enumerated.item[0] = i;
2424
2425 return 0;
2426}
2427
2428static int snd_asihpi_clksrc_put(struct snd_kcontrol *kcontrol,
2429 struct snd_ctl_elem_value *ucontrol)
2430{
2431 struct snd_card_asihpi *asihpi =
2432 (struct snd_card_asihpi *)(kcontrol->private_data);
2433 struct clk_cache *clkcache = &asihpi->cc;
Dan Carpenter0c21fcc2013-09-13 10:44:44 +03002434 unsigned int item;
2435 int change;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002436 u32 h_control = kcontrol->private_value;
2437
2438 change = 1;
2439 item = ucontrol->value.enumerated.item[0];
2440 if (item >= clkcache->count)
2441 item = clkcache->count-1;
2442
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002443 hpi_handle_error(hpi_sample_clock_set_source(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002444 h_control, clkcache->s[item].source));
2445
2446 if (clkcache->s[item].source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002447 hpi_handle_error(hpi_sample_clock_set_source_index(
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002448 h_control, clkcache->s[item].index));
2449 return change;
2450}
2451
2452/*------------------------------------------------------------
2453 Clkrate controls
2454 ------------------------------------------------------------*/
2455/* Need to change this to enumerated control with list of rates */
2456static int snd_asihpi_clklocal_info(struct snd_kcontrol *kcontrol,
2457 struct snd_ctl_elem_info *uinfo)
2458{
2459 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2460 uinfo->count = 1;
2461 uinfo->value.integer.min = 8000;
2462 uinfo->value.integer.max = 192000;
2463 uinfo->value.integer.step = 100;
2464
2465 return 0;
2466}
2467
2468static int snd_asihpi_clklocal_get(struct snd_kcontrol *kcontrol,
2469 struct snd_ctl_elem_value *ucontrol)
2470{
2471 u32 h_control = kcontrol->private_value;
2472 u32 rate;
2473 u16 e;
2474
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002475 e = hpi_sample_clock_get_local_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002476 if (!e)
2477 ucontrol->value.integer.value[0] = rate;
2478 else
2479 ucontrol->value.integer.value[0] = 0;
2480 return 0;
2481}
2482
2483static int snd_asihpi_clklocal_put(struct snd_kcontrol *kcontrol,
2484 struct snd_ctl_elem_value *ucontrol)
2485{
2486 int change;
2487 u32 h_control = kcontrol->private_value;
2488
2489 /* change = asihpi->mixer_clkrate[addr][0] != left ||
2490 asihpi->mixer_clkrate[addr][1] != right;
2491 */
2492 change = 1;
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002493 hpi_handle_error(hpi_sample_clock_set_local_rate(h_control,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002494 ucontrol->value.integer.value[0]));
2495 return change;
2496}
2497
2498static int snd_asihpi_clkrate_info(struct snd_kcontrol *kcontrol,
2499 struct snd_ctl_elem_info *uinfo)
2500{
2501 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
2502 uinfo->count = 1;
2503 uinfo->value.integer.min = 8000;
2504 uinfo->value.integer.max = 192000;
2505 uinfo->value.integer.step = 100;
2506
2507 return 0;
2508}
2509
2510static int snd_asihpi_clkrate_get(struct snd_kcontrol *kcontrol,
2511 struct snd_ctl_elem_value *ucontrol)
2512{
2513 u32 h_control = kcontrol->private_value;
2514 u32 rate;
2515 u16 e;
2516
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002517 e = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002518 if (!e)
2519 ucontrol->value.integer.value[0] = rate;
2520 else
2521 ucontrol->value.integer.value[0] = 0;
2522 return 0;
2523}
2524
Bill Pembertone23e7a12012-12-06 12:35:10 -05002525static int snd_asihpi_sampleclock_add(struct snd_card_asihpi *asihpi,
2526 struct hpi_control *hpi_ctl)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002527{
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002528 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002529 struct snd_kcontrol_new snd_control;
2530
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002531 struct clk_cache *clkcache;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002532 u32 hSC = hpi_ctl->h_control;
2533 int has_aes_in = 0;
2534 int i, j;
2535 u16 source;
2536
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002537 if (snd_BUG_ON(!asihpi))
2538 return -EINVAL;
2539 card = asihpi->card;
2540 clkcache = &asihpi->cc;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002541 snd_control.private_value = hpi_ctl->h_control;
2542
2543 clkcache->has_local = 0;
2544
2545 for (i = 0; i <= HPI_SAMPLECLOCK_SOURCE_LAST; i++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002546 if (hpi_sample_clock_query_source(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002547 i, &source))
2548 break;
2549 clkcache->s[i].source = source;
2550 clkcache->s[i].index = 0;
2551 clkcache->s[i].name = sampleclock_sources[source];
2552 if (source == HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT)
2553 has_aes_in = 1;
2554 if (source == HPI_SAMPLECLOCK_SOURCE_LOCAL)
2555 clkcache->has_local = 1;
2556 }
2557 if (has_aes_in)
2558 /* already will have picked up index 0 above */
2559 for (j = 1; j < 8; j++) {
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002560 if (hpi_sample_clock_query_source_index(hSC,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002561 j, HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT,
2562 &source))
2563 break;
2564 clkcache->s[i].source =
2565 HPI_SAMPLECLOCK_SOURCE_AESEBU_INPUT;
2566 clkcache->s[i].index = j;
2567 clkcache->s[i].name = sampleclock_sources[
2568 j+HPI_SAMPLECLOCK_SOURCE_LAST];
2569 i++;
2570 }
2571 clkcache->count = i;
2572
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002573 asihpi_ctl_init(&snd_control, hpi_ctl, "Source");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002574 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2575 snd_control.info = snd_asihpi_clksrc_info;
2576 snd_control.get = snd_asihpi_clksrc_get;
2577 snd_control.put = snd_asihpi_clksrc_put;
2578 if (ctl_add(card, &snd_control, asihpi) < 0)
2579 return -EINVAL;
2580
2581
2582 if (clkcache->has_local) {
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002583 asihpi_ctl_init(&snd_control, hpi_ctl, "Localrate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002584 snd_control.access = SNDRV_CTL_ELEM_ACCESS_READWRITE ;
2585 snd_control.info = snd_asihpi_clklocal_info;
2586 snd_control.get = snd_asihpi_clklocal_get;
2587 snd_control.put = snd_asihpi_clklocal_put;
2588
2589
2590 if (ctl_add(card, &snd_control, asihpi) < 0)
2591 return -EINVAL;
2592 }
2593
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002594 asihpi_ctl_init(&snd_control, hpi_ctl, "Rate");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002595 snd_control.access =
2596 SNDRV_CTL_ELEM_ACCESS_VOLATILE | SNDRV_CTL_ELEM_ACCESS_READ;
2597 snd_control.info = snd_asihpi_clkrate_info;
2598 snd_control.get = snd_asihpi_clkrate_get;
2599
2600 return ctl_add(card, &snd_control, asihpi);
2601}
2602/*------------------------------------------------------------
2603 Mixer
2604 ------------------------------------------------------------*/
2605
Bill Pembertone23e7a12012-12-06 12:35:10 -05002606static int snd_card_asihpi_mixer_new(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002607{
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002608 struct snd_card *card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002609 unsigned int idx = 0;
2610 unsigned int subindex = 0;
2611 int err;
2612 struct hpi_control hpi_ctl, prev_ctl;
2613
2614 if (snd_BUG_ON(!asihpi))
2615 return -EINVAL;
Wei Yongjun2e9b9a32013-03-12 00:16:40 +08002616 card = asihpi->card;
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002617 strcpy(card->mixername, "Asihpi Mixer");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002618
2619 err =
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002620 hpi_mixer_open(asihpi->hpi->adapter->index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002621 &asihpi->h_mixer);
2622 hpi_handle_error(err);
2623 if (err)
2624 return -err;
2625
Takashi Iwai21896bc2010-06-02 12:08:37 +02002626 memset(&prev_ctl, 0, sizeof(prev_ctl));
2627 prev_ctl.control_type = -1;
2628
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002629 for (idx = 0; idx < 2000; idx++) {
2630 err = hpi_mixer_get_control_by_index(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002631 asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002632 idx,
2633 &hpi_ctl.src_node_type,
2634 &hpi_ctl.src_node_index,
2635 &hpi_ctl.dst_node_type,
2636 &hpi_ctl.dst_node_index,
2637 &hpi_ctl.control_type,
2638 &hpi_ctl.h_control);
2639 if (err) {
2640 if (err == HPI_ERROR_CONTROL_DISABLED) {
2641 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002642 dev_info(&asihpi->pci->dev,
Eliot Blennerhassette64b1a22011-02-10 17:25:59 +13002643 "Disabled HPI Control(%d)\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002644 idx);
2645 continue;
2646 } else
2647 break;
2648
2649 }
2650
Eliot Blennerhassett168f1b02010-07-06 08:37:06 +12002651 hpi_ctl.src_node_type -= HPI_SOURCENODE_NONE;
2652 hpi_ctl.dst_node_type -= HPI_DESTNODE_NONE;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002653
2654 /* ASI50xx in SSX mode has multiple meters on the same node.
2655 Use subindex to create distinct ALSA controls
2656 for any duplicated controls.
2657 */
2658 if ((hpi_ctl.control_type == prev_ctl.control_type) &&
2659 (hpi_ctl.src_node_type == prev_ctl.src_node_type) &&
2660 (hpi_ctl.src_node_index == prev_ctl.src_node_index) &&
2661 (hpi_ctl.dst_node_type == prev_ctl.dst_node_type) &&
2662 (hpi_ctl.dst_node_index == prev_ctl.dst_node_index))
2663 subindex++;
2664 else
2665 subindex = 0;
2666
2667 prev_ctl = hpi_ctl;
2668
2669 switch (hpi_ctl.control_type) {
2670 case HPI_CONTROL_VOLUME:
2671 err = snd_asihpi_volume_add(asihpi, &hpi_ctl);
2672 break;
2673 case HPI_CONTROL_LEVEL:
2674 err = snd_asihpi_level_add(asihpi, &hpi_ctl);
2675 break;
2676 case HPI_CONTROL_MULTIPLEXER:
2677 err = snd_asihpi_mux_add(asihpi, &hpi_ctl);
2678 break;
2679 case HPI_CONTROL_CHANNEL_MODE:
2680 err = snd_asihpi_cmode_add(asihpi, &hpi_ctl);
2681 break;
2682 case HPI_CONTROL_METER:
2683 err = snd_asihpi_meter_add(asihpi, &hpi_ctl, subindex);
2684 break;
2685 case HPI_CONTROL_SAMPLECLOCK:
2686 err = snd_asihpi_sampleclock_add(
2687 asihpi, &hpi_ctl);
2688 break;
2689 case HPI_CONTROL_CONNECTION: /* ignore these */
2690 continue;
2691 case HPI_CONTROL_TUNER:
2692 err = snd_asihpi_tuner_add(asihpi, &hpi_ctl);
2693 break;
2694 case HPI_CONTROL_AESEBU_TRANSMITTER:
2695 err = snd_asihpi_aesebu_tx_add(asihpi, &hpi_ctl);
2696 break;
2697 case HPI_CONTROL_AESEBU_RECEIVER:
2698 err = snd_asihpi_aesebu_rx_add(asihpi, &hpi_ctl);
2699 break;
2700 case HPI_CONTROL_VOX:
2701 case HPI_CONTROL_BITSTREAM:
2702 case HPI_CONTROL_MICROPHONE:
2703 case HPI_CONTROL_PARAMETRIC_EQ:
2704 case HPI_CONTROL_COMPANDER:
2705 default:
2706 if (mixer_dump)
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002707 dev_info(&asihpi->pci->dev,
2708 "Untranslated HPI Control (%d) %d %d %d %d %d\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002709 idx,
2710 hpi_ctl.control_type,
2711 hpi_ctl.src_node_type,
2712 hpi_ctl.src_node_index,
2713 hpi_ctl.dst_node_type,
2714 hpi_ctl.dst_node_index);
2715 continue;
Peter Senna Tschudin395d9dd2012-09-28 11:24:57 +02002716 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002717 if (err < 0)
2718 return err;
2719 }
2720 if (HPI_ERROR_INVALID_OBJ_INDEX != err)
2721 hpi_handle_error(err);
2722
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002723 dev_info(&asihpi->pci->dev, "%d mixer controls found\n", idx);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002724
2725 return 0;
2726}
2727
2728/*------------------------------------------------------------
2729 /proc interface
2730 ------------------------------------------------------------*/
2731
2732static void
2733snd_asihpi_proc_read(struct snd_info_entry *entry,
2734 struct snd_info_buffer *buffer)
2735{
2736 struct snd_card_asihpi *asihpi = entry->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002737 u32 h_control;
2738 u32 rate = 0;
2739 u16 source = 0;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002740
2741 u16 num_outstreams;
2742 u16 num_instreams;
2743 u16 version;
2744 u32 serial_number;
2745 u16 type;
2746
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002747 int err;
2748
2749 snd_iprintf(buffer, "ASIHPI driver proc file\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002750
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002751 hpi_handle_error(hpi_adapter_get_info(asihpi->hpi->adapter->index,
2752 &num_outstreams, &num_instreams,
2753 &version, &serial_number, &type));
2754
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002755 snd_iprintf(buffer,
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002756 "Adapter type ASI%4X\nHardware Index %d\n"
2757 "%d outstreams\n%d instreams\n",
2758 type, asihpi->hpi->adapter->index,
2759 num_outstreams, num_instreams);
2760
2761 snd_iprintf(buffer,
2762 "Serial#%d\nHardware version %c%d\nDSP code version %03d\n",
2763 serial_number, ((version >> 3) & 0xf) + 'A', version & 0x7,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002764 ((version >> 13) * 100) + ((version >> 7) & 0x3f));
2765
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002766 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002767 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2768 HPI_CONTROL_SAMPLECLOCK, &h_control);
2769
2770 if (!err) {
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002771 err = hpi_sample_clock_get_sample_rate(h_control, &rate);
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002772 err += hpi_sample_clock_get_source(h_control, &source);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002773
2774 if (!err)
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002775 snd_iprintf(buffer, "Sample Clock %dHz, source %s\n",
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002776 rate, sampleclock_sources[source]);
2777 }
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002778}
2779
Bill Pembertone23e7a12012-12-06 12:35:10 -05002780static void snd_asihpi_proc_init(struct snd_card_asihpi *asihpi)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002781{
Takashi Iwai47f27692019-02-04 16:01:39 +01002782 snd_card_ro_proc_new(asihpi->card, "info", asihpi,
2783 snd_asihpi_proc_read);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002784}
2785
2786/*------------------------------------------------------------
2787 HWDEP
2788 ------------------------------------------------------------*/
2789
2790static int snd_asihpi_hpi_open(struct snd_hwdep *hw, struct file *file)
2791{
2792 if (enable_hpi_hwdep)
2793 return 0;
2794 else
2795 return -ENODEV;
2796
2797}
2798
2799static int snd_asihpi_hpi_release(struct snd_hwdep *hw, struct file *file)
2800{
2801 if (enable_hpi_hwdep)
2802 return asihpi_hpi_release(file);
2803 else
2804 return -ENODEV;
2805}
2806
2807static int snd_asihpi_hpi_ioctl(struct snd_hwdep *hw, struct file *file,
2808 unsigned int cmd, unsigned long arg)
2809{
2810 if (enable_hpi_hwdep)
2811 return asihpi_hpi_ioctl(file, cmd, arg);
2812 else
2813 return -ENODEV;
2814}
2815
2816
2817/* results in /dev/snd/hwC#D0 file for each card with index #
2818 also /proc/asound/hwdep will contain '#-00: asihpi (HPI) for each card'
2819*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002820static int snd_asihpi_hpi_new(struct snd_card_asihpi *asihpi, int device)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002821{
2822 struct snd_hwdep *hw;
2823 int err;
2824
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002825 err = snd_hwdep_new(asihpi->card, "HPI", device, &hw);
2826 if (err < 0)
2827 return err;
2828 strcpy(hw->name, "asihpi (HPI)");
2829 hw->iface = SNDRV_HWDEP_IFACE_LAST;
2830 hw->ops.open = snd_asihpi_hpi_open;
2831 hw->ops.ioctl = snd_asihpi_hpi_ioctl;
2832 hw->ops.release = snd_asihpi_hpi_release;
2833 hw->private_data = asihpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002834 return 0;
2835}
2836
2837/*------------------------------------------------------------
2838 CARD
2839 ------------------------------------------------------------*/
Bill Pembertone23e7a12012-12-06 12:35:10 -05002840static int snd_asihpi_probe(struct pci_dev *pci_dev,
2841 const struct pci_device_id *pci_id)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002842{
2843 int err;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002844 struct hpi_adapter *hpi;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002845 struct snd_card *card;
2846 struct snd_card_asihpi *asihpi;
2847
2848 u32 h_control;
2849 u32 h_stream;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002850 u32 adapter_index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002851
2852 static int dev;
2853 if (dev >= SNDRV_CARDS)
2854 return -ENODEV;
2855
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002856 /* Should this be enable[hpi->index] ? */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002857 if (!enable[dev]) {
2858 dev++;
2859 return -ENOENT;
2860 }
2861
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002862 /* Initialise low-level HPI driver */
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002863 err = asihpi_adapter_probe(pci_dev, pci_id);
2864 if (err < 0)
2865 return err;
2866
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002867 hpi = pci_get_drvdata(pci_dev);
2868 adapter_index = hpi->adapter->index;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002869 /* first try to give the card the same index as its hardware index */
Takashi Iwai60c57722014-01-29 14:20:19 +01002870 err = snd_card_new(&pci_dev->dev, adapter_index, id[adapter_index],
2871 THIS_MODULE, sizeof(struct snd_card_asihpi), &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002872 if (err < 0) {
2873 /* if that fails, try the default index==next available */
Takashi Iwai60c57722014-01-29 14:20:19 +01002874 err = snd_card_new(&pci_dev->dev, index[dev], id[dev],
2875 THIS_MODULE, sizeof(struct snd_card_asihpi),
2876 &card);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002877 if (err < 0)
2878 return err;
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002879 dev_warn(&pci_dev->dev, "Adapter index %d->ALSA index %d\n",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002880 adapter_index, card->number);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002881 }
2882
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002883 asihpi = card->private_data;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002884 asihpi->card = card;
Eliot Blennerhassett12253672011-02-10 17:26:10 +13002885 asihpi->pci = pci_dev;
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002886 asihpi->hpi = hpi;
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002887 hpi->snd_card = card;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002888
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002889 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002890 HPI_ADAPTER_PROPERTY_CAPS1,
2891 NULL, &asihpi->support_grouping);
2892 if (err)
2893 asihpi->support_grouping = 0;
2894
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002895 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002896 HPI_ADAPTER_PROPERTY_CAPS2,
2897 &asihpi->support_mrx, NULL);
2898 if (err)
2899 asihpi->support_mrx = 0;
2900
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002901 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002902 HPI_ADAPTER_PROPERTY_INTERVAL,
2903 NULL, &asihpi->update_interval_frames);
2904 if (err)
2905 asihpi->update_interval_frames = 512;
2906
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002907 if (hpi->interrupt_mode) {
2908 asihpi->pcm_start = snd_card_asihpi_pcm_int_start;
2909 asihpi->pcm_stop = snd_card_asihpi_pcm_int_stop;
2910 tasklet_init(&asihpi->t, snd_card_asihpi_int_task,
2911 (unsigned long)hpi);
2912 hpi->interrupt_callback = snd_card_asihpi_isr;
2913 } else {
2914 asihpi->pcm_start = snd_card_asihpi_pcm_timer_start;
2915 asihpi->pcm_stop = snd_card_asihpi_pcm_timer_stop;
2916 }
Eliot Blennerhassett26aebef2011-03-25 15:25:47 +13002917
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002918 hpi_handle_error(hpi_instream_open(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002919 0, &h_stream));
2920
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002921 err = hpi_instream_host_buffer_free(h_stream);
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002922 asihpi->can_dma = (!err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002923
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002924 hpi_handle_error(hpi_instream_close(h_stream));
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002925
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13002926 if (!asihpi->can_dma)
2927 asihpi->update_interval_frames *= 2;
2928
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002929 err = hpi_adapter_get_property(adapter_index,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002930 HPI_ADAPTER_PROPERTY_CURCHANNELS,
2931 &asihpi->in_max_chans, &asihpi->out_max_chans);
2932 if (err) {
2933 asihpi->in_max_chans = 2;
2934 asihpi->out_max_chans = 2;
2935 }
2936
Eliot Blennerhassettc382a5d2011-12-22 13:38:33 +13002937 if (asihpi->out_max_chans > 2) { /* assume LL mode */
2938 asihpi->out_min_chans = asihpi->out_max_chans;
2939 asihpi->in_min_chans = asihpi->in_max_chans;
2940 asihpi->support_grouping = 0;
2941 } else {
2942 asihpi->out_min_chans = 1;
2943 asihpi->in_min_chans = 1;
2944 }
2945
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002946 dev_info(&pci_dev->dev, "Has dma:%d, grouping:%d, mrx:%d, uif:%d\n",
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002947 asihpi->can_dma,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002948 asihpi->support_grouping,
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002949 asihpi->support_mrx,
2950 asihpi->update_interval_frames
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002951 );
2952
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002953 err = snd_card_asihpi_pcm_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002954 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002955 dev_err(&pci_dev->dev, "pcm_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002956 goto __nodev;
2957 }
2958 err = snd_card_asihpi_mixer_new(asihpi);
2959 if (err < 0) {
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002960 dev_err(&pci_dev->dev, "mixer_new failed\n");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002961 goto __nodev;
2962 }
2963
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002964 err = hpi_mixer_get_control(asihpi->h_mixer,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002965 HPI_SOURCENODE_CLOCK_SOURCE, 0, 0, 0,
2966 HPI_CONTROL_SAMPLECLOCK, &h_control);
2967
2968 if (!err)
2969 err = hpi_sample_clock_set_local_rate(
Eliot Blennerhassettba944552011-02-10 17:26:04 +13002970 h_control, adapter_fs);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002971
2972 snd_asihpi_proc_init(asihpi);
2973
2974 /* always create, can be enabled or disabled dynamically
2975 by enable_hwdep module param*/
Lars-Peter Clausend18132a2015-01-02 12:24:45 +01002976 snd_asihpi_hpi_new(asihpi, 0);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002977
Eliot Blennerhassettf3d145a2011-04-05 20:55:44 +12002978 strcpy(card->driver, "ASIHPI");
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002979
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002980 sprintf(card->shortname, "AudioScience ASI%4X",
2981 asihpi->hpi->adapter->type);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002982 sprintf(card->longname, "%s %i",
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002983 card->shortname, adapter_index);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002984 err = snd_card_register(card);
Eliot Blennerhassettb2e65c82011-03-25 15:25:48 +13002985
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002986 if (!err) {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002987 dev++;
2988 return 0;
2989 }
2990__nodev:
2991 snd_card_free(card);
Eliot Blennerhassett12eb0892014-11-20 16:22:55 +13002992 dev_err(&pci_dev->dev, "snd_asihpi_probe error %d\n", err);
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002993 return err;
2994
2995}
2996
Bill Pembertone23e7a12012-12-06 12:35:10 -05002997static void snd_asihpi_remove(struct pci_dev *pci_dev)
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02002998{
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13002999 struct hpi_adapter *hpi = pci_get_drvdata(pci_dev);
Eliot Blennerhassettf9a376c2014-11-20 16:22:53 +13003000 struct snd_card_asihpi *asihpi = hpi->snd_card->private_data;
3001
3002 /* Stop interrupts */
3003 if (hpi->interrupt_mode) {
3004 hpi->interrupt_callback = NULL;
3005 hpi_handle_error(hpi_adapter_set_property(hpi->adapter->index,
3006 HPI_ADAPTER_PROPERTY_IRQ_RATE, 0, 0));
3007 tasklet_kill(&asihpi->t);
3008 }
3009
Eliot Blennerhassett7036b922011-12-22 13:38:43 +13003010 snd_card_free(hpi->snd_card);
3011 hpi->snd_card = NULL;
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003012 asihpi_adapter_remove(pci_dev);
3013}
3014
Benoit Taine9baa3c32014-08-08 15:56:03 +02003015static const struct pci_device_id asihpi_pci_tbl[] = {
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003016 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_DSP6205,
3017 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3018 (kernel_ulong_t)HPI_6205},
3019 {HPI_PCI_VENDOR_ID_TI, HPI_PCI_DEV_ID_PCI2040,
3020 HPI_PCI_VENDOR_ID_AUDIOSCIENCE, PCI_ANY_ID, 0, 0,
3021 (kernel_ulong_t)HPI_6000},
3022 {0,}
3023};
3024MODULE_DEVICE_TABLE(pci, asihpi_pci_tbl);
3025
3026static struct pci_driver driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02003027 .name = KBUILD_MODNAME,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003028 .id_table = asihpi_pci_tbl,
3029 .probe = snd_asihpi_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05003030 .remove = snd_asihpi_remove,
Eliot Blennerhassett719f82d2010-04-21 18:17:39 +02003031};
3032
3033static int __init snd_asihpi_init(void)
3034{
3035 asihpi_init();
3036 return pci_register_driver(&driver);
3037}
3038
3039static void __exit snd_asihpi_exit(void)
3040{
3041
3042 pci_unregister_driver(&driver);
3043 asihpi_exit();
3044}
3045
3046module_init(snd_asihpi_init)
3047module_exit(snd_asihpi_exit)
3048