blob: f9c3e86d55d5515b2b834fc6e0b96410b65c8ecf [file] [log] [blame]
Tim Blechmann02bec492009-03-24 12:24:35 +01001/* -*- linux-c -*- *
2 *
3 * ALSA driver for the digigram lx6464es interface
4 *
5 * Copyright (c) 2008, 2009 Tim Blechmann <tim@klingt.org>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/pci.h>
28#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Tim Blechmann02bec492009-03-24 12:24:35 +010030
31#include <sound/initval.h>
32#include <sound/control.h>
33#include <sound/info.h>
34
35#include "lx6464es.h"
36
37MODULE_AUTHOR("Tim Blechmann");
38MODULE_LICENSE("GPL");
39MODULE_DESCRIPTION("digigram lx6464es");
40MODULE_SUPPORTED_DEVICE("{digigram lx6464es{}}");
41
42
43static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
44static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
Rusty Russella67ff6a2011-12-15 13:49:36 +103045static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Tim Blechmann02bec492009-03-24 12:24:35 +010046
Tim Blechmannde0525c2009-06-11 16:03:34 +020047module_param_array(index, int, NULL, 0444);
48MODULE_PARM_DESC(index, "Index value for Digigram LX6464ES interface.");
49module_param_array(id, charp, NULL, 0444);
50MODULE_PARM_DESC(id, "ID string for Digigram LX6464ES interface.");
51module_param_array(enable, bool, NULL, 0444);
52MODULE_PARM_DESC(enable, "Enable/disable specific Digigram LX6464ES soundcards.");
53
Tim Blechmann02bec492009-03-24 12:24:35 +010054static const char card_name[] = "LX6464ES";
55
56
57#define PCI_DEVICE_ID_PLX_LX6464ES PCI_DEVICE_ID_PLX_9056
58
Benoit Taine9baa3c32014-08-08 15:56:03 +020059static const struct pci_device_id snd_lx6464es_ids[] = {
Takashi Iwai19831262015-05-26 12:52:54 +020060 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
61 PCI_VENDOR_ID_DIGIGRAM,
62 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM),
Tim Blechmann02bec492009-03-24 12:24:35 +010063 }, /* LX6464ES */
Takashi Iwai19831262015-05-26 12:52:54 +020064 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
65 PCI_VENDOR_ID_DIGIGRAM,
66 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM),
Tim Blechmann02bec492009-03-24 12:24:35 +010067 }, /* LX6464ES-CAE */
68 { 0, },
69};
70
71MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
72
73
74
75/* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
76#define CHIPSC_RESET_XILINX (1L<<16)
77
78
79/* alsa callbacks */
80static struct snd_pcm_hardware lx_caps = {
81 .info = (SNDRV_PCM_INFO_MMAP |
82 SNDRV_PCM_INFO_INTERLEAVED |
83 SNDRV_PCM_INFO_MMAP_VALID |
84 SNDRV_PCM_INFO_SYNC_START),
85 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
86 SNDRV_PCM_FMTBIT_S16_BE |
87 SNDRV_PCM_FMTBIT_S24_3LE |
88 SNDRV_PCM_FMTBIT_S24_3BE),
89 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
90 SNDRV_PCM_RATE_8000_192000),
91 .rate_min = 8000,
92 .rate_max = 192000,
93 .channels_min = 2,
94 .channels_max = 64,
95 .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
96 .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
97 .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
98 .periods_min = 2,
99 .periods_max = MAX_STREAM_BUFFER,
100};
101
102static int lx_set_granularity(struct lx6464es *chip, u32 gran);
103
104
105static int lx_hardware_open(struct lx6464es *chip,
106 struct snd_pcm_substream *substream)
107{
108 int err = 0;
109 struct snd_pcm_runtime *runtime = substream->runtime;
110 int channels = runtime->channels;
111 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
112
113 snd_pcm_uframes_t period_size = runtime->period_size;
114
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100115 dev_dbg(chip->card->dev, "allocating pipe for %d channels\n", channels);
Tim Blechmann02bec492009-03-24 12:24:35 +0100116 err = lx_pipe_allocate(chip, 0, is_capture, channels);
117 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100118 dev_err(chip->card->dev, LXP "allocating pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100119 return err;
120 }
121
122 err = lx_set_granularity(chip, period_size);
123 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100124 dev_err(chip->card->dev, "setting granularity to %ld failed\n",
Tim Blechmann02bec492009-03-24 12:24:35 +0100125 period_size);
126 return err;
127 }
128
129 return 0;
130}
131
132static int lx_hardware_start(struct lx6464es *chip,
133 struct snd_pcm_substream *substream)
134{
135 int err = 0;
136 struct snd_pcm_runtime *runtime = substream->runtime;
137 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
138
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100139 dev_dbg(chip->card->dev, "setting stream format\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100140 err = lx_stream_set_format(chip, runtime, 0, is_capture);
141 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100142 dev_err(chip->card->dev, "setting stream format failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100143 return err;
144 }
145
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100146 dev_dbg(chip->card->dev, "starting pipe\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100147 err = lx_pipe_start(chip, 0, is_capture);
148 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100149 dev_err(chip->card->dev, "starting pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100150 return err;
151 }
152
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100153 dev_dbg(chip->card->dev, "waiting for pipe to start\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100154 err = lx_pipe_wait_for_start(chip, 0, is_capture);
155 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100156 dev_err(chip->card->dev, "waiting for pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100157 return err;
158 }
159
160 return err;
161}
162
163
164static int lx_hardware_stop(struct lx6464es *chip,
165 struct snd_pcm_substream *substream)
166{
167 int err = 0;
168 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
169
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100170 dev_dbg(chip->card->dev, "pausing pipe\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100171 err = lx_pipe_pause(chip, 0, is_capture);
172 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100173 dev_err(chip->card->dev, "pausing pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100174 return err;
175 }
176
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100177 dev_dbg(chip->card->dev, "waiting for pipe to become idle\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100178 err = lx_pipe_wait_for_idle(chip, 0, is_capture);
179 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100180 dev_err(chip->card->dev, "waiting for pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100181 return err;
182 }
183
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100184 dev_dbg(chip->card->dev, "stopping pipe\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100185 err = lx_pipe_stop(chip, 0, is_capture);
186 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100187 dev_err(chip->card->dev, "stopping pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100188 return err;
189 }
190
191 return err;
192}
193
194
195static int lx_hardware_close(struct lx6464es *chip,
196 struct snd_pcm_substream *substream)
197{
198 int err = 0;
199 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
200
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100201 dev_dbg(chip->card->dev, "releasing pipe\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100202 err = lx_pipe_release(chip, 0, is_capture);
203 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100204 dev_err(chip->card->dev, "releasing pipe failed\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100205 return err;
206 }
207
208 return err;
209}
210
211
212static int lx_pcm_open(struct snd_pcm_substream *substream)
213{
214 struct lx6464es *chip = snd_pcm_substream_chip(substream);
215 struct snd_pcm_runtime *runtime = substream->runtime;
216 int err = 0;
217 int board_rate;
218
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100219 dev_dbg(chip->card->dev, "->lx_pcm_open\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100220 mutex_lock(&chip->setup_mutex);
221
222 /* copy the struct snd_pcm_hardware struct */
223 runtime->hw = lx_caps;
224
225#if 0
226 /* buffer-size should better be multiple of period-size */
227 err = snd_pcm_hw_constraint_integer(runtime,
228 SNDRV_PCM_HW_PARAM_PERIODS);
229 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100230 dev_warn(chip->card->dev, "could not constrain periods\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100231 goto exit;
232 }
233#endif
234
235 /* the clock rate cannot be changed */
236 board_rate = chip->board_sample_rate;
Lars-Peter Clausendfcdb022015-10-18 15:39:18 +0200237 err = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_RATE,
238 board_rate);
Tim Blechmann02bec492009-03-24 12:24:35 +0100239
240 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100241 dev_warn(chip->card->dev, "could not constrain periods\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100242 goto exit;
243 }
244
245 /* constrain period size */
246 err = snd_pcm_hw_constraint_minmax(runtime,
247 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
248 MICROBLAZE_IBL_MIN,
249 MICROBLAZE_IBL_MAX);
250 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100251 dev_warn(chip->card->dev,
Tim Blechmann02bec492009-03-24 12:24:35 +0100252 "could not constrain period size\n");
253 goto exit;
254 }
255
256 snd_pcm_hw_constraint_step(runtime, 0,
257 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
258
259 snd_pcm_set_sync(substream);
260 err = 0;
261
262exit:
263 runtime->private_data = chip;
264
265 mutex_unlock(&chip->setup_mutex);
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100266 dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
Tim Blechmann02bec492009-03-24 12:24:35 +0100267 return err;
268}
269
270static int lx_pcm_close(struct snd_pcm_substream *substream)
271{
272 int err = 0;
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100273 dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100274 return err;
275}
276
277static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
278 *substream)
279{
280 struct lx6464es *chip = snd_pcm_substream_chip(substream);
281 snd_pcm_uframes_t pos;
Tim Blechmann02bec492009-03-24 12:24:35 +0100282 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
283
284 struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
285 &chip->playback_stream;
286
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100287 dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100288
Takashi Iwai6336c202014-09-10 14:01:05 +0200289 mutex_lock(&chip->lock);
Tim Blechmann02bec492009-03-24 12:24:35 +0100290 pos = lx_stream->frame_pos * substream->runtime->period_size;
Takashi Iwai6336c202014-09-10 14:01:05 +0200291 mutex_unlock(&chip->lock);
Tim Blechmann02bec492009-03-24 12:24:35 +0100292
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100293 dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
Tim Blechmann02bec492009-03-24 12:24:35 +0100294 return pos;
295}
296
297static int lx_pcm_prepare(struct snd_pcm_substream *substream)
298{
299 struct lx6464es *chip = snd_pcm_substream_chip(substream);
300 int err = 0;
301 const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
302
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100303 dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100304
305 mutex_lock(&chip->setup_mutex);
306
307 if (chip->hardware_running[is_capture]) {
308 err = lx_hardware_stop(chip, substream);
309 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100310 dev_err(chip->card->dev, "failed to stop hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100311 "Error code %d\n", err);
312 goto exit;
313 }
314
315 err = lx_hardware_close(chip, substream);
316 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100317 dev_err(chip->card->dev, "failed to close hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100318 "Error code %d\n", err);
319 goto exit;
320 }
321 }
322
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100323 dev_dbg(chip->card->dev, "opening hardware\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100324 err = lx_hardware_open(chip, substream);
325 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100326 dev_err(chip->card->dev, "failed to open hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100327 "Error code %d\n", err);
328 goto exit;
329 }
330
331 err = lx_hardware_start(chip, substream);
332 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100333 dev_err(chip->card->dev, "failed to start hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100334 "Error code %d\n", err);
335 goto exit;
336 }
337
338 chip->hardware_running[is_capture] = 1;
339
340 if (chip->board_sample_rate != substream->runtime->rate) {
341 if (!err)
342 chip->board_sample_rate = substream->runtime->rate;
343 }
344
345exit:
346 mutex_unlock(&chip->setup_mutex);
347 return err;
348}
349
350static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
351 struct snd_pcm_hw_params *hw_params, int is_capture)
352{
353 struct lx6464es *chip = snd_pcm_substream_chip(substream);
354 int err = 0;
355
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100356 dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100357
358 mutex_lock(&chip->setup_mutex);
359
360 /* set dma buffer */
361 err = snd_pcm_lib_malloc_pages(substream,
362 params_buffer_bytes(hw_params));
363
364 if (is_capture)
365 chip->capture_stream.stream = substream;
366 else
367 chip->playback_stream.stream = substream;
368
369 mutex_unlock(&chip->setup_mutex);
370 return err;
371}
372
373static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
374 struct snd_pcm_hw_params *hw_params)
375{
376 return lx_pcm_hw_params(substream, hw_params, 0);
377}
378
379static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
380 struct snd_pcm_hw_params *hw_params)
381{
382 return lx_pcm_hw_params(substream, hw_params, 1);
383}
384
385static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
386{
387 struct lx6464es *chip = snd_pcm_substream_chip(substream);
388 int err = 0;
389 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
390
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100391 dev_dbg(chip->card->dev, "->lx_pcm_hw_free\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100392 mutex_lock(&chip->setup_mutex);
393
394 if (chip->hardware_running[is_capture]) {
395 err = lx_hardware_stop(chip, substream);
396 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100397 dev_err(chip->card->dev, "failed to stop hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100398 "Error code %d\n", err);
399 goto exit;
400 }
401
402 err = lx_hardware_close(chip, substream);
403 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100404 dev_err(chip->card->dev, "failed to close hardware. "
Tim Blechmann02bec492009-03-24 12:24:35 +0100405 "Error code %d\n", err);
406 goto exit;
407 }
408
409 chip->hardware_running[is_capture] = 0;
410 }
411
412 err = snd_pcm_lib_free_pages(substream);
413
414 if (is_capture)
Takashi Iwaie42e7482015-05-26 12:56:21 +0200415 chip->capture_stream.stream = NULL;
Tim Blechmann02bec492009-03-24 12:24:35 +0100416 else
Takashi Iwaie42e7482015-05-26 12:56:21 +0200417 chip->playback_stream.stream = NULL;
Tim Blechmann02bec492009-03-24 12:24:35 +0100418
419exit:
420 mutex_unlock(&chip->setup_mutex);
421 return err;
422}
423
424static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
425{
426 struct snd_pcm_substream *substream = lx_stream->stream;
Tim Blechmannf7467452010-10-31 19:46:19 +0100427 const unsigned int is_capture = lx_stream->is_capture;
Tim Blechmann02bec492009-03-24 12:24:35 +0100428
429 int err;
430
431 const u32 channels = substream->runtime->channels;
432 const u32 bytes_per_frame = channels * 3;
433 const u32 period_size = substream->runtime->period_size;
434 const u32 periods = substream->runtime->periods;
435 const u32 period_bytes = period_size * bytes_per_frame;
436
437 dma_addr_t buf = substream->dma_buffer.addr;
438 int i;
439
440 u32 needed, freed;
441 u32 size_array[5];
442
443 for (i = 0; i != periods; ++i) {
444 u32 buffer_index = 0;
445
446 err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
447 size_array);
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100448 dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
Tim Blechmann02bec492009-03-24 12:24:35 +0100449 needed, freed);
450
451 err = lx_buffer_give(chip, 0, is_capture, period_bytes,
452 lower_32_bits(buf), upper_32_bits(buf),
453 &buffer_index);
454
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100455 dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
Takashi Iwai293db842013-11-06 17:54:55 +0100456 buffer_index, (unsigned long)buf, period_bytes);
Tim Blechmann02bec492009-03-24 12:24:35 +0100457 buf += period_bytes;
458 }
459
460 err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100461 dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
Tim Blechmann02bec492009-03-24 12:24:35 +0100462
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100463 dev_dbg(chip->card->dev, "starting: starting stream\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100464 err = lx_stream_start(chip, 0, is_capture);
465 if (err < 0)
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100466 dev_err(chip->card->dev, "couldn't start stream\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100467 else
468 lx_stream->status = LX_STREAM_STATUS_RUNNING;
469
470 lx_stream->frame_pos = 0;
471}
472
473static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
474{
Tim Blechmannf7467452010-10-31 19:46:19 +0100475 const unsigned int is_capture = lx_stream->is_capture;
Tim Blechmann02bec492009-03-24 12:24:35 +0100476 int err;
477
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100478 dev_dbg(chip->card->dev, "stopping: stopping stream\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100479 err = lx_stream_stop(chip, 0, is_capture);
480 if (err < 0)
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100481 dev_err(chip->card->dev, "couldn't stop stream\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100482 else
483 lx_stream->status = LX_STREAM_STATUS_FREE;
484
485}
486
Takashi Iwai6336c202014-09-10 14:01:05 +0200487static void lx_trigger_dispatch_stream(struct lx6464es *chip,
488 struct lx_stream *lx_stream)
Tim Blechmann02bec492009-03-24 12:24:35 +0100489{
490 switch (lx_stream->status) {
491 case LX_STREAM_STATUS_SCHEDULE_RUN:
492 lx_trigger_start(chip, lx_stream);
493 break;
494
495 case LX_STREAM_STATUS_SCHEDULE_STOP:
496 lx_trigger_stop(chip, lx_stream);
497 break;
498
499 default:
500 break;
501 }
502}
503
Tim Blechmann02bec492009-03-24 12:24:35 +0100504static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
505 struct lx_stream *lx_stream, int cmd)
506{
507 int err = 0;
508
Takashi Iwai6336c202014-09-10 14:01:05 +0200509 mutex_lock(&chip->lock);
Tim Blechmann02bec492009-03-24 12:24:35 +0100510 switch (cmd) {
511 case SNDRV_PCM_TRIGGER_START:
512 lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
513 break;
514
515 case SNDRV_PCM_TRIGGER_STOP:
516 lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
517 break;
518
519 default:
520 err = -EINVAL;
521 goto exit;
522 }
Takashi Iwai6336c202014-09-10 14:01:05 +0200523
524 lx_trigger_dispatch_stream(chip, &chip->capture_stream);
525 lx_trigger_dispatch_stream(chip, &chip->playback_stream);
Tim Blechmann02bec492009-03-24 12:24:35 +0100526
527exit:
Takashi Iwai6336c202014-09-10 14:01:05 +0200528 mutex_unlock(&chip->lock);
Tim Blechmann02bec492009-03-24 12:24:35 +0100529 return err;
530}
531
532
533static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
534{
535 struct lx6464es *chip = snd_pcm_substream_chip(substream);
536 const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
537 struct lx_stream *stream = is_capture ? &chip->capture_stream :
538 &chip->playback_stream;
539
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100540 dev_dbg(chip->card->dev, "->lx_pcm_trigger\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100541
542 return lx_pcm_trigger_dispatch(chip, stream, cmd);
543}
544
545static int snd_lx6464es_free(struct lx6464es *chip)
546{
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100547 dev_dbg(chip->card->dev, "->snd_lx6464es_free\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100548
549 lx_irq_disable(chip);
550
551 if (chip->irq >= 0)
552 free_irq(chip->irq, chip);
553
554 iounmap(chip->port_dsp_bar);
555 ioport_unmap(chip->port_plx_remapped);
556
557 pci_release_regions(chip->pci);
558 pci_disable_device(chip->pci);
559
560 kfree(chip);
561
562 return 0;
563}
564
565static int snd_lx6464es_dev_free(struct snd_device *device)
566{
567 return snd_lx6464es_free(device->device_data);
568}
569
570/* reset the dsp during initialization */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500571static int lx_init_xilinx_reset(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100572{
573 int i;
574 u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
575
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100576 dev_dbg(chip->card->dev, "->lx_init_xilinx_reset\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100577
578 /* activate reset of xilinx */
579 plx_reg &= ~CHIPSC_RESET_XILINX;
580
581 lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
582 msleep(1);
583
584 lx_plx_reg_write(chip, ePLX_MBOX3, 0);
585 msleep(1);
586
587 plx_reg |= CHIPSC_RESET_XILINX;
588 lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
589
590 /* deactivate reset of xilinx */
591 for (i = 0; i != 100; ++i) {
592 u32 reg_mbox3;
593 msleep(10);
594 reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
595 if (reg_mbox3) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100596 dev_dbg(chip->card->dev, "xilinx reset done\n");
597 dev_dbg(chip->card->dev, "xilinx took %d loops\n", i);
Tim Blechmann02bec492009-03-24 12:24:35 +0100598 break;
599 }
600 }
601
602 /* todo: add some error handling? */
603
604 /* clear mr */
605 lx_dsp_reg_write(chip, eReg_CSM, 0);
606
607 /* le xilinx ES peut ne pas etre encore pret, on attend. */
608 msleep(600);
609
610 return 0;
611}
612
Bill Pembertone23e7a12012-12-06 12:35:10 -0500613static int lx_init_xilinx_test(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100614{
615 u32 reg;
616
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100617 dev_dbg(chip->card->dev, "->lx_init_xilinx_test\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100618
619 /* TEST if we have access to Xilinx/MicroBlaze */
620 lx_dsp_reg_write(chip, eReg_CSM, 0);
621
622 reg = lx_dsp_reg_read(chip, eReg_CSM);
623
624 if (reg) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100625 dev_err(chip->card->dev, "Problem: Reg_CSM %x.\n", reg);
Tim Blechmann02bec492009-03-24 12:24:35 +0100626
627 /* PCI9056_SPACE0_REMAP */
628 lx_plx_reg_write(chip, ePLX_PCICR, 1);
629
630 reg = lx_dsp_reg_read(chip, eReg_CSM);
631 if (reg) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100632 dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
Tim Blechmann02bec492009-03-24 12:24:35 +0100633 return -EAGAIN; /* seems to be appropriate */
634 }
635 }
636
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100637 dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100638
639 return 0;
640}
641
642/* initialize ethersound */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500643static int lx_init_ethersound_config(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100644{
645 int i;
646 u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
647
Tim Blechmann7e895cf2009-06-25 09:41:46 +0200648 /* configure 64 io channels */
649 u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
Tim Blechmann02bec492009-03-24 12:24:35 +0100650 (64 << IOCR_INPUTS_OFFSET) |
Tim Blechmann7e895cf2009-06-25 09:41:46 +0200651 (64 << IOCR_OUTPUTS_OFFSET) |
Tim Blechmann02bec492009-03-24 12:24:35 +0100652 (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
653
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100654 dev_dbg(chip->card->dev, "->lx_init_ethersound\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100655
656 chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
657
658 /*
659 * write it to the card !
660 * this actually kicks the ES xilinx, the first time since poweron.
661 * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
662 * is not ready before this is done, and the bit 2 in Reg_CSES is set.
663 * */
664 lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
665
666 for (i = 0; i != 1000; ++i) {
667 if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100668 dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
Tim Blechmann02bec492009-03-24 12:24:35 +0100669 i);
670 goto ethersound_initialized;
671 }
672 msleep(1);
673 }
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100674 dev_warn(chip->card->dev,
Tim Blechmann02bec492009-03-24 12:24:35 +0100675 "ethersound could not be initialized after %dms\n", i);
676 return -ETIMEDOUT;
677
678 ethersound_initialized:
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100679 dev_dbg(chip->card->dev, "ethersound initialized\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100680 return 0;
681}
682
Bill Pembertone23e7a12012-12-06 12:35:10 -0500683static int lx_init_get_version_features(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100684{
685 u32 dsp_version;
686
687 int err;
688
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100689 dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100690
691 err = lx_dsp_get_version(chip, &dsp_version);
692
693 if (err == 0) {
694 u32 freq;
695
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100696 dev_info(chip->card->dev, "DSP version: V%02d.%02d #%d\n",
Tim Blechmann02bec492009-03-24 12:24:35 +0100697 (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
698 dsp_version & 0xff);
699
700 /* later: what firmware version do we expect? */
701
702 /* retrieve Play/Rec features */
703 /* done here because we may have to handle alternate
704 * DSP files. */
705 /* later */
706
707 /* init the EtherSound sample rate */
708 err = lx_dsp_get_clock_frequency(chip, &freq);
709 if (err == 0)
710 chip->board_sample_rate = freq;
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100711 dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
Tim Blechmann02bec492009-03-24 12:24:35 +0100712 } else {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100713 dev_err(chip->card->dev, "DSP corrupted \n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100714 err = -EAGAIN;
715 }
716
717 return err;
718}
719
720static int lx_set_granularity(struct lx6464es *chip, u32 gran)
721{
722 int err = 0;
723 u32 snapped_gran = MICROBLAZE_IBL_MIN;
724
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100725 dev_dbg(chip->card->dev, "->lx_set_granularity\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100726
727 /* blocksize is a power of 2 */
728 while ((snapped_gran < gran) &&
729 (snapped_gran < MICROBLAZE_IBL_MAX)) {
730 snapped_gran *= 2;
731 }
732
733 if (snapped_gran == chip->pcm_granularity)
734 return 0;
735
736 err = lx_dsp_set_granularity(chip, snapped_gran);
737 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100738 dev_warn(chip->card->dev, "could not set granularity\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100739 err = -EAGAIN;
740 }
741
742 if (snapped_gran != gran)
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100743 dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
Tim Blechmann02bec492009-03-24 12:24:35 +0100744
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100745 dev_dbg(chip->card->dev, "set blocksize on board %d\n", snapped_gran);
Tim Blechmann02bec492009-03-24 12:24:35 +0100746 chip->pcm_granularity = snapped_gran;
747
748 return err;
749}
750
751/* initialize and test the xilinx dsp chip */
Bill Pembertone23e7a12012-12-06 12:35:10 -0500752static int lx_init_dsp(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100753{
754 int err;
Tim Blechmann02bec492009-03-24 12:24:35 +0100755 int i;
756
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100757 dev_dbg(chip->card->dev, "->lx_init_dsp\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100758
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100759 dev_dbg(chip->card->dev, "initialize board\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100760 err = lx_init_xilinx_reset(chip);
761 if (err)
762 return err;
763
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100764 dev_dbg(chip->card->dev, "testing board\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100765 err = lx_init_xilinx_test(chip);
766 if (err)
767 return err;
768
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100769 dev_dbg(chip->card->dev, "initialize ethersound configuration\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100770 err = lx_init_ethersound_config(chip);
771 if (err)
772 return err;
773
774 lx_irq_enable(chip);
775
776 /** \todo the mac address should be ready by not, but it isn't,
777 * so we wait for it */
778 for (i = 0; i != 1000; ++i) {
Tim Blechmann80b52492011-06-24 17:36:20 +0200779 err = lx_dsp_get_mac(chip);
Tim Blechmann02bec492009-03-24 12:24:35 +0100780 if (err)
781 return err;
Tim Blechmann80b52492011-06-24 17:36:20 +0200782 if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
783 chip->mac_address[3] || chip->mac_address[4] || chip->mac_address[5])
Tim Blechmann02bec492009-03-24 12:24:35 +0100784 goto mac_ready;
785 msleep(1);
786 }
787 return -ETIMEDOUT;
788
789mac_ready:
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100790 dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
791 dev_info(chip->card->dev,
792 "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
Tim Blechmann80b52492011-06-24 17:36:20 +0200793 chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
794 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
Tim Blechmann02bec492009-03-24 12:24:35 +0100795
796 err = lx_init_get_version_features(chip);
797 if (err)
798 return err;
799
800 lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
801
802 chip->playback_mute = 0;
803
804 return err;
805}
806
Julia Lawall6769e9882016-09-02 00:13:10 +0200807static const struct snd_pcm_ops lx_ops_playback = {
Tim Blechmann02bec492009-03-24 12:24:35 +0100808 .open = lx_pcm_open,
809 .close = lx_pcm_close,
810 .ioctl = snd_pcm_lib_ioctl,
811 .prepare = lx_pcm_prepare,
812 .hw_params = lx_pcm_hw_params_playback,
813 .hw_free = lx_pcm_hw_free,
814 .trigger = lx_pcm_trigger,
815 .pointer = lx_pcm_stream_pointer,
816};
817
Julia Lawall6769e9882016-09-02 00:13:10 +0200818static const struct snd_pcm_ops lx_ops_capture = {
Tim Blechmann02bec492009-03-24 12:24:35 +0100819 .open = lx_pcm_open,
820 .close = lx_pcm_close,
821 .ioctl = snd_pcm_lib_ioctl,
822 .prepare = lx_pcm_prepare,
823 .hw_params = lx_pcm_hw_params_capture,
824 .hw_free = lx_pcm_hw_free,
825 .trigger = lx_pcm_trigger,
826 .pointer = lx_pcm_stream_pointer,
827};
828
Bill Pembertone23e7a12012-12-06 12:35:10 -0500829static int lx_pcm_create(struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100830{
831 int err;
832 struct snd_pcm *pcm;
833
834 u32 size = 64 * /* channels */
835 3 * /* 24 bit samples */
836 MAX_STREAM_BUFFER * /* periods */
837 MICROBLAZE_IBL_MAX * /* frames per period */
838 2; /* duplex */
839
840 size = PAGE_ALIGN(size);
841
842 /* hardcoded device name & channel count */
843 err = snd_pcm_new(chip->card, (char *)card_name, 0,
844 1, 1, &pcm);
Takashi Iwai3bdcff72012-08-14 17:42:11 +0200845 if (err < 0)
846 return err;
Tim Blechmann02bec492009-03-24 12:24:35 +0100847
848 pcm->private_data = chip;
849
850 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
851 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
852
853 pcm->info_flags = 0;
Takashi Iwai6336c202014-09-10 14:01:05 +0200854 pcm->nonatomic = true;
Tim Blechmann02bec492009-03-24 12:24:35 +0100855 strcpy(pcm->name, card_name);
856
857 err = snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
858 snd_dma_pci_data(chip->pci),
859 size, size);
860 if (err < 0)
861 return err;
862
863 chip->pcm = pcm;
864 chip->capture_stream.is_capture = 1;
865
866 return 0;
867}
868
869static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
870 struct snd_ctl_elem_info *uinfo)
871{
872 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
873 uinfo->count = 1;
874 uinfo->value.integer.min = 0;
875 uinfo->value.integer.max = 1;
876 return 0;
877}
878
879static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
880 struct snd_ctl_elem_value *ucontrol)
881{
882 struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
883 ucontrol->value.integer.value[0] = chip->playback_mute;
884 return 0;
885}
886
887static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
888 struct snd_ctl_elem_value *ucontrol)
889{
890 struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
891 int changed = 0;
892 int current_value = chip->playback_mute;
893
894 if (current_value != ucontrol->value.integer.value[0]) {
895 lx_level_unmute(chip, 0, !current_value);
896 chip->playback_mute = !current_value;
897 changed = 1;
898 }
899 return changed;
900}
901
Bhumika Goyalf3b827e2017-02-20 00:18:09 +0530902static const struct snd_kcontrol_new lx_control_playback_switch = {
Tim Blechmann02bec492009-03-24 12:24:35 +0100903 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
904 .name = "PCM Playback Switch",
905 .index = 0,
906 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
907 .private_value = 0,
908 .info = lx_control_playback_info,
909 .get = lx_control_playback_get,
910 .put = lx_control_playback_put
911};
912
913
914
915static void lx_proc_levels_read(struct snd_info_entry *entry,
916 struct snd_info_buffer *buffer)
917{
918 u32 levels[64];
919 int err;
920 int i, j;
921 struct lx6464es *chip = entry->private_data;
922
923 snd_iprintf(buffer, "capture levels:\n");
924 err = lx_level_peaks(chip, 1, 64, levels);
925 if (err < 0)
926 return;
927
928 for (i = 0; i != 8; ++i) {
929 for (j = 0; j != 8; ++j)
930 snd_iprintf(buffer, "%08x ", levels[i*8+j]);
931 snd_iprintf(buffer, "\n");
932 }
933
934 snd_iprintf(buffer, "\nplayback levels:\n");
935
936 err = lx_level_peaks(chip, 0, 64, levels);
937 if (err < 0)
938 return;
939
940 for (i = 0; i != 8; ++i) {
941 for (j = 0; j != 8; ++j)
942 snd_iprintf(buffer, "%08x ", levels[i*8+j]);
943 snd_iprintf(buffer, "\n");
944 }
945
946 snd_iprintf(buffer, "\n");
947}
948
Bill Pembertone23e7a12012-12-06 12:35:10 -0500949static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100950{
951 struct snd_info_entry *entry;
952 int err = snd_card_proc_new(card, "levels", &entry);
953 if (err < 0)
954 return err;
955
956 snd_info_set_text_ops(entry, chip, lx_proc_levels_read);
957 return 0;
958}
959
960
Bill Pembertone23e7a12012-12-06 12:35:10 -0500961static int snd_lx6464es_create(struct snd_card *card,
962 struct pci_dev *pci,
963 struct lx6464es **rchip)
Tim Blechmann02bec492009-03-24 12:24:35 +0100964{
965 struct lx6464es *chip;
966 int err;
967
968 static struct snd_device_ops ops = {
969 .dev_free = snd_lx6464es_dev_free,
970 };
971
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100972 dev_dbg(card->dev, "->snd_lx6464es_create\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100973
974 *rchip = NULL;
975
976 /* enable PCI device */
977 err = pci_enable_device(pci);
978 if (err < 0)
979 return err;
980
981 pci_set_master(pci);
982
983 /* check if we can restrict PCI DMA transfers to 32 bits */
Quentin Lambert412b9792015-04-15 16:10:17 +0200984 err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
Tim Blechmann02bec492009-03-24 12:24:35 +0100985 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +0100986 dev_err(card->dev,
987 "architecture does not support 32bit PCI busmaster DMA\n");
Tim Blechmann02bec492009-03-24 12:24:35 +0100988 pci_disable_device(pci);
989 return -ENXIO;
990 }
991
992 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
993 if (chip == NULL) {
994 err = -ENOMEM;
995 goto alloc_failed;
996 }
997
998 chip->card = card;
999 chip->pci = pci;
1000 chip->irq = -1;
1001
1002 /* initialize synchronization structs */
Takashi Iwai6336c202014-09-10 14:01:05 +02001003 mutex_init(&chip->lock);
1004 mutex_init(&chip->msg_lock);
Tim Blechmann02bec492009-03-24 12:24:35 +01001005 mutex_init(&chip->setup_mutex);
Tim Blechmann02bec492009-03-24 12:24:35 +01001006
1007 /* request resources */
1008 err = pci_request_regions(pci, card_name);
1009 if (err < 0)
1010 goto request_regions_failed;
1011
1012 /* plx port */
1013 chip->port_plx = pci_resource_start(pci, 1);
1014 chip->port_plx_remapped = ioport_map(chip->port_plx,
1015 pci_resource_len(pci, 1));
1016
1017 /* dsp port */
1018 chip->port_dsp_bar = pci_ioremap_bar(pci, 2);
1019
Takashi Iwai6336c202014-09-10 14:01:05 +02001020 err = request_threaded_irq(pci->irq, lx_interrupt, lx_threaded_irq,
1021 IRQF_SHARED, KBUILD_MODNAME, chip);
Tim Blechmann02bec492009-03-24 12:24:35 +01001022 if (err) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +01001023 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
Tim Blechmann02bec492009-03-24 12:24:35 +01001024 goto request_irq_failed;
1025 }
1026 chip->irq = pci->irq;
1027
1028 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1029 if (err < 0)
1030 goto device_new_failed;
1031
1032 err = lx_init_dsp(chip);
1033 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +01001034 dev_err(card->dev, "error during DSP initialization\n");
Tim Blechmann02bec492009-03-24 12:24:35 +01001035 return err;
1036 }
1037
1038 err = lx_pcm_create(chip);
1039 if (err < 0)
1040 return err;
1041
1042 err = lx_proc_create(card, chip);
1043 if (err < 0)
1044 return err;
1045
1046 err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
1047 chip));
1048 if (err < 0)
1049 return err;
1050
Tim Blechmann02bec492009-03-24 12:24:35 +01001051 *rchip = chip;
1052 return 0;
1053
1054device_new_failed:
1055 free_irq(pci->irq, chip);
1056
1057request_irq_failed:
1058 pci_release_regions(pci);
1059
1060request_regions_failed:
1061 kfree(chip);
1062
1063alloc_failed:
1064 pci_disable_device(pci);
1065
1066 return err;
1067}
1068
Bill Pembertone23e7a12012-12-06 12:35:10 -05001069static int snd_lx6464es_probe(struct pci_dev *pci,
1070 const struct pci_device_id *pci_id)
Tim Blechmann02bec492009-03-24 12:24:35 +01001071{
1072 static int dev;
1073 struct snd_card *card;
1074 struct lx6464es *chip;
1075 int err;
1076
Takashi Iwaibe4e6d32014-02-25 17:32:49 +01001077 dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
Tim Blechmann02bec492009-03-24 12:24:35 +01001078
1079 if (dev >= SNDRV_CARDS)
1080 return -ENODEV;
1081 if (!enable[dev]) {
1082 dev++;
1083 return -ENOENT;
1084 }
1085
Takashi Iwai60c57722014-01-29 14:20:19 +01001086 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1087 0, &card);
Takashi Iwai7852fd02009-04-14 12:25:52 +02001088 if (err < 0)
1089 return err;
Tim Blechmann02bec492009-03-24 12:24:35 +01001090
1091 err = snd_lx6464es_create(card, pci, &chip);
1092 if (err < 0) {
Takashi Iwaibe4e6d32014-02-25 17:32:49 +01001093 dev_err(card->dev, "error during snd_lx6464es_create\n");
Tim Blechmann02bec492009-03-24 12:24:35 +01001094 goto out_free;
1095 }
1096
Tim Blechmann80b52492011-06-24 17:36:20 +02001097 strcpy(card->driver, "LX6464ES");
1098 sprintf(card->id, "LX6464ES_%02X%02X%02X",
1099 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1100
1101 sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
1102 chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
1103 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1104
Tim Blechmann02bec492009-03-24 12:24:35 +01001105 sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
1106 card->shortname, chip->port_plx,
1107 chip->port_dsp_bar, chip->irq);
1108
1109 err = snd_card_register(card);
1110 if (err < 0)
1111 goto out_free;
1112
Takashi Iwaibe4e6d32014-02-25 17:32:49 +01001113 dev_dbg(chip->card->dev, "initialization successful\n");
Tim Blechmann02bec492009-03-24 12:24:35 +01001114 pci_set_drvdata(pci, card);
1115 dev++;
1116 return 0;
1117
1118out_free:
1119 snd_card_free(card);
1120 return err;
1121
1122}
1123
Bill Pembertone23e7a12012-12-06 12:35:10 -05001124static void snd_lx6464es_remove(struct pci_dev *pci)
Tim Blechmann02bec492009-03-24 12:24:35 +01001125{
1126 snd_card_free(pci_get_drvdata(pci));
Tim Blechmann02bec492009-03-24 12:24:35 +01001127}
1128
1129
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001130static struct pci_driver lx6464es_driver = {
Takashi Iwai3733e422011-06-10 16:20:20 +02001131 .name = KBUILD_MODNAME,
Tim Blechmann02bec492009-03-24 12:24:35 +01001132 .id_table = snd_lx6464es_ids,
1133 .probe = snd_lx6464es_probe,
Bill Pembertone23e7a12012-12-06 12:35:10 -05001134 .remove = snd_lx6464es_remove,
Tim Blechmann02bec492009-03-24 12:24:35 +01001135};
1136
Takashi Iwaie9f66d92012-04-24 12:25:00 +02001137module_pci_driver(lx6464es_driver);