1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- linux-c -*- *
3 *
4 * ALSA driver for the digigram lx6464es interface
5 *
6 * Copyright (c) 2008, 2009 Tim Blechmann <tim@klingt.org>
7 */
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/pci.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14
15 #include <sound/initval.h>
16 #include <sound/control.h>
17 #include <sound/info.h>
18
19 #include "lx6464es.h"
20
21 MODULE_AUTHOR("Tim Blechmann");
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("digigram lx6464es");
24
25 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
26 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
27 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
28
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for Digigram LX6464ES interface.");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for Digigram LX6464ES interface.");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable/disable specific Digigram LX6464ES soundcards.");
35
36 static const char card_name[] = "LX6464ES";
37
38
39 #define PCI_DEVICE_ID_PLX_LX6464ES PCI_DEVICE_ID_PLX_9056
40
41 static const struct pci_device_id snd_lx6464es_ids[] = {
42 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
43 PCI_VENDOR_ID_DIGIGRAM,
44 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_SERIAL_SUBSYSTEM),
45 }, /* LX6464ES */
46 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
47 PCI_VENDOR_ID_DIGIGRAM,
48 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ES_CAE_SERIAL_SUBSYSTEM),
49 }, /* LX6464ES-CAE */
50 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
51 PCI_VENDOR_ID_DIGIGRAM,
52 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_SERIAL_SUBSYSTEM),
53 }, /* LX6464ESe */
54 { PCI_DEVICE_SUB(PCI_VENDOR_ID_PLX, PCI_DEVICE_ID_PLX_LX6464ES,
55 PCI_VENDOR_ID_DIGIGRAM,
56 PCI_SUBDEVICE_ID_DIGIGRAM_LX6464ESE_CAE_SERIAL_SUBSYSTEM),
57 }, /* LX6464ESe-CAE */
58 { 0, },
59 };
60
61 MODULE_DEVICE_TABLE(pci, snd_lx6464es_ids);
62
63
64
65 /* PGO pour USERo dans le registre pci_0x06/loc_0xEC */
66 #define CHIPSC_RESET_XILINX (1L<<16)
67
68
69 /* alsa callbacks */
70 static const struct snd_pcm_hardware lx_caps = {
71 .info = (SNDRV_PCM_INFO_MMAP |
72 SNDRV_PCM_INFO_INTERLEAVED |
73 SNDRV_PCM_INFO_MMAP_VALID |
74 SNDRV_PCM_INFO_SYNC_START),
75 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
76 SNDRV_PCM_FMTBIT_S16_BE |
77 SNDRV_PCM_FMTBIT_S24_3LE |
78 SNDRV_PCM_FMTBIT_S24_3BE),
79 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
80 SNDRV_PCM_RATE_8000_192000),
81 .rate_min = 8000,
82 .rate_max = 192000,
83 .channels_min = 2,
84 .channels_max = 64,
85 .buffer_bytes_max = 64*2*3*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER,
86 .period_bytes_min = (2*2*MICROBLAZE_IBL_MIN*2),
87 .period_bytes_max = (4*64*MICROBLAZE_IBL_MAX*MAX_STREAM_BUFFER),
88 .periods_min = 2,
89 .periods_max = MAX_STREAM_BUFFER,
90 };
91
92 static int lx_set_granularity(struct lx6464es *chip, u32 gran);
93
94
lx_hardware_open(struct lx6464es * chip,struct snd_pcm_substream * substream)95 static int lx_hardware_open(struct lx6464es *chip,
96 struct snd_pcm_substream *substream)
97 {
98 int err = 0;
99 struct snd_pcm_runtime *runtime = substream->runtime;
100 int channels = runtime->channels;
101 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
102
103 snd_pcm_uframes_t period_size = runtime->period_size;
104
105 dev_dbg(chip->card->dev, "allocating pipe for %d channels\n", channels);
106 err = lx_pipe_allocate(chip, 0, is_capture, channels);
107 if (err < 0) {
108 dev_err(chip->card->dev, LXP "allocating pipe failed\n");
109 return err;
110 }
111
112 err = lx_set_granularity(chip, period_size);
113 if (err < 0) {
114 dev_err(chip->card->dev, "setting granularity to %ld failed\n",
115 period_size);
116 return err;
117 }
118
119 return 0;
120 }
121
lx_hardware_start(struct lx6464es * chip,struct snd_pcm_substream * substream)122 static int lx_hardware_start(struct lx6464es *chip,
123 struct snd_pcm_substream *substream)
124 {
125 int err = 0;
126 struct snd_pcm_runtime *runtime = substream->runtime;
127 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
128
129 dev_dbg(chip->card->dev, "setting stream format\n");
130 err = lx_stream_set_format(chip, runtime, 0, is_capture);
131 if (err < 0) {
132 dev_err(chip->card->dev, "setting stream format failed\n");
133 return err;
134 }
135
136 dev_dbg(chip->card->dev, "starting pipe\n");
137 err = lx_pipe_start(chip, 0, is_capture);
138 if (err < 0) {
139 dev_err(chip->card->dev, "starting pipe failed\n");
140 return err;
141 }
142
143 dev_dbg(chip->card->dev, "waiting for pipe to start\n");
144 err = lx_pipe_wait_for_start(chip, 0, is_capture);
145 if (err < 0) {
146 dev_err(chip->card->dev, "waiting for pipe failed\n");
147 return err;
148 }
149
150 return err;
151 }
152
153
lx_hardware_stop(struct lx6464es * chip,struct snd_pcm_substream * substream)154 static int lx_hardware_stop(struct lx6464es *chip,
155 struct snd_pcm_substream *substream)
156 {
157 int err = 0;
158 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
159
160 dev_dbg(chip->card->dev, "pausing pipe\n");
161 err = lx_pipe_pause(chip, 0, is_capture);
162 if (err < 0) {
163 dev_err(chip->card->dev, "pausing pipe failed\n");
164 return err;
165 }
166
167 dev_dbg(chip->card->dev, "waiting for pipe to become idle\n");
168 err = lx_pipe_wait_for_idle(chip, 0, is_capture);
169 if (err < 0) {
170 dev_err(chip->card->dev, "waiting for pipe failed\n");
171 return err;
172 }
173
174 dev_dbg(chip->card->dev, "stopping pipe\n");
175 err = lx_pipe_stop(chip, 0, is_capture);
176 if (err < 0) {
177 dev_err(chip->card->dev, "stopping pipe failed\n");
178 return err;
179 }
180
181 return err;
182 }
183
184
lx_hardware_close(struct lx6464es * chip,struct snd_pcm_substream * substream)185 static int lx_hardware_close(struct lx6464es *chip,
186 struct snd_pcm_substream *substream)
187 {
188 int err = 0;
189 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
190
191 dev_dbg(chip->card->dev, "releasing pipe\n");
192 err = lx_pipe_release(chip, 0, is_capture);
193 if (err < 0) {
194 dev_err(chip->card->dev, "releasing pipe failed\n");
195 return err;
196 }
197
198 return err;
199 }
200
201
lx_pcm_open(struct snd_pcm_substream * substream)202 static int lx_pcm_open(struct snd_pcm_substream *substream)
203 {
204 struct lx6464es *chip = snd_pcm_substream_chip(substream);
205 struct snd_pcm_runtime *runtime = substream->runtime;
206 int err = 0;
207 int board_rate;
208
209 dev_dbg(chip->card->dev, "->lx_pcm_open\n");
210 guard(mutex)(&chip->setup_mutex);
211
212 /* copy the struct snd_pcm_hardware struct */
213 runtime->hw = lx_caps;
214
215 #if 0
216 /* buffer-size should better be multiple of period-size */
217 err = snd_pcm_hw_constraint_integer(runtime,
218 SNDRV_PCM_HW_PARAM_PERIODS);
219 if (err < 0) {
220 dev_warn(chip->card->dev, "could not constrain periods\n");
221 return err;
222 }
223 #endif
224
225 /* the clock rate cannot be changed */
226 board_rate = chip->board_sample_rate;
227 err = snd_pcm_hw_constraint_single(runtime, SNDRV_PCM_HW_PARAM_RATE,
228 board_rate);
229
230 if (err < 0) {
231 dev_warn(chip->card->dev, "could not constrain periods\n");
232 return err;
233 }
234
235 /* constrain period size */
236 err = snd_pcm_hw_constraint_minmax(runtime,
237 SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
238 MICROBLAZE_IBL_MIN,
239 MICROBLAZE_IBL_MAX);
240 if (err < 0) {
241 dev_warn(chip->card->dev,
242 "could not constrain period size\n");
243 return err;
244 }
245
246 snd_pcm_hw_constraint_step(runtime, 0,
247 SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 32);
248
249 snd_pcm_set_sync(substream);
250 err = 0;
251
252 runtime->private_data = chip;
253
254 dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
255 return err;
256 }
257
lx_pcm_close(struct snd_pcm_substream * substream)258 static int lx_pcm_close(struct snd_pcm_substream *substream)
259 {
260 dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
261 return 0;
262 }
263
lx_pcm_stream_pointer(struct snd_pcm_substream * substream)264 static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
265 *substream)
266 {
267 struct lx6464es *chip = snd_pcm_substream_chip(substream);
268 snd_pcm_uframes_t pos;
269 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
270
271 struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
272 &chip->playback_stream;
273
274 dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
275
276 guard(mutex)(&chip->lock);
277 pos = lx_stream->frame_pos * substream->runtime->period_size;
278
279 dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
280 return pos;
281 }
282
lx_pcm_prepare(struct snd_pcm_substream * substream)283 static int lx_pcm_prepare(struct snd_pcm_substream *substream)
284 {
285 struct lx6464es *chip = snd_pcm_substream_chip(substream);
286 int err = 0;
287 const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
288
289 dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
290
291 guard(mutex)(&chip->setup_mutex);
292
293 if (chip->hardware_running[is_capture]) {
294 err = lx_hardware_stop(chip, substream);
295 if (err < 0) {
296 dev_err(chip->card->dev, "failed to stop hardware. "
297 "Error code %d\n", err);
298 return err;
299 }
300
301 err = lx_hardware_close(chip, substream);
302 if (err < 0) {
303 dev_err(chip->card->dev, "failed to close hardware. "
304 "Error code %d\n", err);
305 return err;
306 }
307 }
308
309 dev_dbg(chip->card->dev, "opening hardware\n");
310 err = lx_hardware_open(chip, substream);
311 if (err < 0) {
312 dev_err(chip->card->dev, "failed to open hardware. "
313 "Error code %d\n", err);
314 return err;
315 }
316
317 err = lx_hardware_start(chip, substream);
318 if (err < 0) {
319 dev_err(chip->card->dev, "failed to start hardware. "
320 "Error code %d\n", err);
321 return err;
322 }
323
324 chip->hardware_running[is_capture] = 1;
325
326 if (chip->board_sample_rate != substream->runtime->rate) {
327 if (!err)
328 chip->board_sample_rate = substream->runtime->rate;
329 }
330
331 return err;
332 }
333
lx_pcm_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params,int is_capture)334 static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
335 struct snd_pcm_hw_params *hw_params, int is_capture)
336 {
337 struct lx6464es *chip = snd_pcm_substream_chip(substream);
338
339 dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
340
341 guard(mutex)(&chip->setup_mutex);
342
343 if (is_capture)
344 chip->capture_stream.stream = substream;
345 else
346 chip->playback_stream.stream = substream;
347
348 return 0;
349 }
350
lx_pcm_hw_params_playback(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)351 static int lx_pcm_hw_params_playback(struct snd_pcm_substream *substream,
352 struct snd_pcm_hw_params *hw_params)
353 {
354 return lx_pcm_hw_params(substream, hw_params, 0);
355 }
356
lx_pcm_hw_params_capture(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)357 static int lx_pcm_hw_params_capture(struct snd_pcm_substream *substream,
358 struct snd_pcm_hw_params *hw_params)
359 {
360 return lx_pcm_hw_params(substream, hw_params, 1);
361 }
362
lx_pcm_hw_free(struct snd_pcm_substream * substream)363 static int lx_pcm_hw_free(struct snd_pcm_substream *substream)
364 {
365 struct lx6464es *chip = snd_pcm_substream_chip(substream);
366 int err = 0;
367 int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
368
369 dev_dbg(chip->card->dev, "->lx_pcm_hw_free\n");
370 guard(mutex)(&chip->setup_mutex);
371
372 if (chip->hardware_running[is_capture]) {
373 err = lx_hardware_stop(chip, substream);
374 if (err < 0) {
375 dev_err(chip->card->dev, "failed to stop hardware. "
376 "Error code %d\n", err);
377 return err;
378 }
379
380 err = lx_hardware_close(chip, substream);
381 if (err < 0) {
382 dev_err(chip->card->dev, "failed to close hardware. "
383 "Error code %d\n", err);
384 return err;
385 }
386
387 chip->hardware_running[is_capture] = 0;
388 }
389
390 if (is_capture)
391 chip->capture_stream.stream = NULL;
392 else
393 chip->playback_stream.stream = NULL;
394
395 return 0;
396 }
397
lx_trigger_start(struct lx6464es * chip,struct lx_stream * lx_stream)398 static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
399 {
400 struct snd_pcm_substream *substream = lx_stream->stream;
401 const unsigned int is_capture = lx_stream->is_capture;
402
403 int err;
404
405 const u32 periods = substream->runtime->periods;
406 const u32 period_bytes = snd_pcm_lib_period_bytes(substream);
407
408 dma_addr_t buf = substream->dma_buffer.addr;
409 int i;
410
411 u32 needed, freed;
412 u32 size_array[5];
413
414 for (i = 0; i != periods; ++i) {
415 u32 buffer_index = 0;
416
417 err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
418 size_array);
419 dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
420 needed, freed);
421
422 err = lx_buffer_give(chip, 0, is_capture, period_bytes,
423 lower_32_bits(buf), upper_32_bits(buf),
424 &buffer_index);
425
426 dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
427 buffer_index, (unsigned long)buf, period_bytes);
428 buf += period_bytes;
429 }
430
431 err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
432 dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
433
434 dev_dbg(chip->card->dev, "starting: starting stream\n");
435 err = lx_stream_start(chip, 0, is_capture);
436 if (err < 0)
437 dev_err(chip->card->dev, "couldn't start stream\n");
438 else
439 lx_stream->status = LX_STREAM_STATUS_RUNNING;
440
441 lx_stream->frame_pos = 0;
442 }
443
lx_trigger_stop(struct lx6464es * chip,struct lx_stream * lx_stream)444 static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
445 {
446 const unsigned int is_capture = lx_stream->is_capture;
447 int err;
448
449 dev_dbg(chip->card->dev, "stopping: stopping stream\n");
450 err = lx_stream_stop(chip, 0, is_capture);
451 if (err < 0)
452 dev_err(chip->card->dev, "couldn't stop stream\n");
453 else
454 lx_stream->status = LX_STREAM_STATUS_FREE;
455
456 }
457
lx_trigger_dispatch_stream(struct lx6464es * chip,struct lx_stream * lx_stream)458 static void lx_trigger_dispatch_stream(struct lx6464es *chip,
459 struct lx_stream *lx_stream)
460 {
461 switch (lx_stream->status) {
462 case LX_STREAM_STATUS_SCHEDULE_RUN:
463 lx_trigger_start(chip, lx_stream);
464 break;
465
466 case LX_STREAM_STATUS_SCHEDULE_STOP:
467 lx_trigger_stop(chip, lx_stream);
468 break;
469
470 default:
471 break;
472 }
473 }
474
lx_pcm_trigger_dispatch(struct lx6464es * chip,struct lx_stream * lx_stream,int cmd)475 static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
476 struct lx_stream *lx_stream, int cmd)
477 {
478 guard(mutex)(&chip->lock);
479 switch (cmd) {
480 case SNDRV_PCM_TRIGGER_START:
481 lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
482 break;
483
484 case SNDRV_PCM_TRIGGER_STOP:
485 lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
486 break;
487
488 default:
489 return -EINVAL;
490 }
491
492 lx_trigger_dispatch_stream(chip, &chip->capture_stream);
493 lx_trigger_dispatch_stream(chip, &chip->playback_stream);
494
495 return 0;
496 }
497
498
lx_pcm_trigger(struct snd_pcm_substream * substream,int cmd)499 static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
500 {
501 struct lx6464es *chip = snd_pcm_substream_chip(substream);
502 const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
503 struct lx_stream *stream = is_capture ? &chip->capture_stream :
504 &chip->playback_stream;
505
506 dev_dbg(chip->card->dev, "->lx_pcm_trigger\n");
507
508 return lx_pcm_trigger_dispatch(chip, stream, cmd);
509 }
510
snd_lx6464es_free(struct snd_card * card)511 static void snd_lx6464es_free(struct snd_card *card)
512 {
513 struct lx6464es *chip = card->private_data;
514
515 lx_irq_disable(chip);
516 }
517
518 /* reset the dsp during initialization */
lx_init_xilinx_reset(struct lx6464es * chip)519 static int lx_init_xilinx_reset(struct lx6464es *chip)
520 {
521 int i;
522 u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
523
524 dev_dbg(chip->card->dev, "->lx_init_xilinx_reset\n");
525
526 /* activate reset of xilinx */
527 plx_reg &= ~CHIPSC_RESET_XILINX;
528
529 lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
530 msleep(1);
531
532 lx_plx_reg_write(chip, ePLX_MBOX3, 0);
533 msleep(1);
534
535 plx_reg |= CHIPSC_RESET_XILINX;
536 lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
537
538 /* deactivate reset of xilinx */
539 for (i = 0; i != 100; ++i) {
540 u32 reg_mbox3;
541 msleep(10);
542 reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
543 if (reg_mbox3) {
544 dev_dbg(chip->card->dev, "xilinx reset done\n");
545 dev_dbg(chip->card->dev, "xilinx took %d loops\n", i);
546 break;
547 }
548 }
549
550 /* todo: add some error handling? */
551
552 /* clear mr */
553 lx_dsp_reg_write(chip, eReg_CSM, 0);
554
555 /* le xilinx ES peut ne pas etre encore pret, on attend. */
556 msleep(600);
557
558 return 0;
559 }
560
lx_init_xilinx_test(struct lx6464es * chip)561 static int lx_init_xilinx_test(struct lx6464es *chip)
562 {
563 u32 reg;
564
565 dev_dbg(chip->card->dev, "->lx_init_xilinx_test\n");
566
567 /* TEST if we have access to Xilinx/MicroBlaze */
568 lx_dsp_reg_write(chip, eReg_CSM, 0);
569
570 reg = lx_dsp_reg_read(chip, eReg_CSM);
571
572 if (reg) {
573 dev_err(chip->card->dev, "Problem: Reg_CSM %x.\n", reg);
574
575 /* PCI9056_SPACE0_REMAP */
576 lx_plx_reg_write(chip, ePLX_PCICR, 1);
577
578 reg = lx_dsp_reg_read(chip, eReg_CSM);
579 if (reg) {
580 dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
581 return -EAGAIN; /* seems to be appropriate */
582 }
583 }
584
585 dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
586
587 return 0;
588 }
589
590 /* initialize ethersound */
lx_init_ethersound_config(struct lx6464es * chip)591 static int lx_init_ethersound_config(struct lx6464es *chip)
592 {
593 int i;
594 u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
595
596 /* configure 64 io channels */
597 u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
598 (64 << IOCR_INPUTS_OFFSET) |
599 (64 << IOCR_OUTPUTS_OFFSET) |
600 (FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
601
602 dev_dbg(chip->card->dev, "->lx_init_ethersound\n");
603
604 chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
605
606 /*
607 * write it to the card !
608 * this actually kicks the ES xilinx, the first time since poweron.
609 * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
610 * is not ready before this is done, and the bit 2 in Reg_CSES is set.
611 * */
612 lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
613
614 for (i = 0; i != 1000; ++i) {
615 if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
616 dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
617 i);
618 goto ethersound_initialized;
619 }
620 msleep(1);
621 }
622 dev_warn(chip->card->dev,
623 "ethersound could not be initialized after %dms\n", i);
624 return -ETIMEDOUT;
625
626 ethersound_initialized:
627 dev_dbg(chip->card->dev, "ethersound initialized\n");
628 return 0;
629 }
630
lx_init_get_version_features(struct lx6464es * chip)631 static int lx_init_get_version_features(struct lx6464es *chip)
632 {
633 u32 dsp_version;
634
635 int err;
636
637 dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
638
639 err = lx_dsp_get_version(chip, &dsp_version);
640
641 if (err == 0) {
642 u32 freq;
643
644 dev_info(chip->card->dev, "DSP version: V%02d.%02d #%d\n",
645 (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
646 dsp_version & 0xff);
647
648 /* later: what firmware version do we expect? */
649
650 /* retrieve Play/Rec features */
651 /* done here because we may have to handle alternate
652 * DSP files. */
653 /* later */
654
655 /* init the EtherSound sample rate */
656 err = lx_dsp_get_clock_frequency(chip, &freq);
657 if (err == 0)
658 chip->board_sample_rate = freq;
659 dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
660 } else {
661 dev_err(chip->card->dev, "DSP corrupted \n");
662 err = -EAGAIN;
663 }
664
665 return err;
666 }
667
lx_set_granularity(struct lx6464es * chip,u32 gran)668 static int lx_set_granularity(struct lx6464es *chip, u32 gran)
669 {
670 int err = 0;
671 u32 snapped_gran = MICROBLAZE_IBL_MIN;
672
673 dev_dbg(chip->card->dev, "->lx_set_granularity\n");
674
675 /* blocksize is a power of 2 */
676 while ((snapped_gran < gran) &&
677 (snapped_gran < MICROBLAZE_IBL_MAX)) {
678 snapped_gran *= 2;
679 }
680
681 if (snapped_gran == chip->pcm_granularity)
682 return 0;
683
684 err = lx_dsp_set_granularity(chip, snapped_gran);
685 if (err < 0) {
686 dev_warn(chip->card->dev, "could not set granularity\n");
687 err = -EAGAIN;
688 }
689
690 if (snapped_gran != gran)
691 dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
692
693 dev_dbg(chip->card->dev, "set blocksize on board %d\n", snapped_gran);
694 chip->pcm_granularity = snapped_gran;
695
696 return err;
697 }
698
699 /* initialize and test the xilinx dsp chip */
lx_init_dsp(struct lx6464es * chip)700 static int lx_init_dsp(struct lx6464es *chip)
701 {
702 int err;
703 int i;
704
705 dev_dbg(chip->card->dev, "->lx_init_dsp\n");
706
707 dev_dbg(chip->card->dev, "initialize board\n");
708 err = lx_init_xilinx_reset(chip);
709 if (err)
710 return err;
711
712 dev_dbg(chip->card->dev, "testing board\n");
713 err = lx_init_xilinx_test(chip);
714 if (err)
715 return err;
716
717 dev_dbg(chip->card->dev, "initialize ethersound configuration\n");
718 err = lx_init_ethersound_config(chip);
719 if (err)
720 return err;
721
722 lx_irq_enable(chip);
723
724 /** \todo the mac address should be ready by not, but it isn't,
725 * so we wait for it */
726 for (i = 0; i != 1000; ++i) {
727 err = lx_dsp_get_mac(chip);
728 if (err)
729 return err;
730 if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
731 chip->mac_address[3] || chip->mac_address[4] || chip->mac_address[5])
732 goto mac_ready;
733 msleep(1);
734 }
735 return -ETIMEDOUT;
736
737 mac_ready:
738 dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
739 dev_info(chip->card->dev,
740 "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
741 chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
742 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
743
744 err = lx_init_get_version_features(chip);
745 if (err)
746 return err;
747
748 lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
749
750 chip->playback_mute = 0;
751
752 return err;
753 }
754
755 static const struct snd_pcm_ops lx_ops_playback = {
756 .open = lx_pcm_open,
757 .close = lx_pcm_close,
758 .prepare = lx_pcm_prepare,
759 .hw_params = lx_pcm_hw_params_playback,
760 .hw_free = lx_pcm_hw_free,
761 .trigger = lx_pcm_trigger,
762 .pointer = lx_pcm_stream_pointer,
763 };
764
765 static const struct snd_pcm_ops lx_ops_capture = {
766 .open = lx_pcm_open,
767 .close = lx_pcm_close,
768 .prepare = lx_pcm_prepare,
769 .hw_params = lx_pcm_hw_params_capture,
770 .hw_free = lx_pcm_hw_free,
771 .trigger = lx_pcm_trigger,
772 .pointer = lx_pcm_stream_pointer,
773 };
774
lx_pcm_create(struct lx6464es * chip)775 static int lx_pcm_create(struct lx6464es *chip)
776 {
777 int err;
778 struct snd_pcm *pcm;
779
780 u32 size = 64 * /* channels */
781 3 * /* 24 bit samples */
782 MAX_STREAM_BUFFER * /* periods */
783 MICROBLAZE_IBL_MAX * /* frames per period */
784 2; /* duplex */
785
786 size = PAGE_ALIGN(size);
787
788 /* hardcoded device name & channel count */
789 err = snd_pcm_new(chip->card, (char *)card_name, 0,
790 1, 1, &pcm);
791 if (err < 0)
792 return err;
793
794 pcm->private_data = chip;
795
796 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
797 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
798
799 pcm->info_flags = 0;
800 pcm->nonatomic = true;
801 strscpy(pcm->name, card_name);
802
803 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
804 &chip->pci->dev, size, size);
805
806 chip->pcm = pcm;
807 chip->capture_stream.is_capture = 1;
808
809 return 0;
810 }
811
lx_control_playback_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)812 static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
813 struct snd_ctl_elem_info *uinfo)
814 {
815 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
816 uinfo->count = 1;
817 uinfo->value.integer.min = 0;
818 uinfo->value.integer.max = 1;
819 return 0;
820 }
821
lx_control_playback_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)822 static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
823 struct snd_ctl_elem_value *ucontrol)
824 {
825 struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
826 ucontrol->value.integer.value[0] = chip->playback_mute;
827 return 0;
828 }
829
lx_control_playback_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)830 static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
831 struct snd_ctl_elem_value *ucontrol)
832 {
833 struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
834 int changed = 0;
835 int current_value = chip->playback_mute;
836
837 if (current_value != ucontrol->value.integer.value[0]) {
838 lx_level_unmute(chip, 0, !current_value);
839 chip->playback_mute = !current_value;
840 changed = 1;
841 }
842 return changed;
843 }
844
845 static const struct snd_kcontrol_new lx_control_playback_switch = {
846 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
847 .name = "PCM Playback Switch",
848 .index = 0,
849 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
850 .private_value = 0,
851 .info = lx_control_playback_info,
852 .get = lx_control_playback_get,
853 .put = lx_control_playback_put
854 };
855
856
857
lx_proc_levels_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)858 static void lx_proc_levels_read(struct snd_info_entry *entry,
859 struct snd_info_buffer *buffer)
860 {
861 u32 levels[64];
862 int err;
863 int i, j;
864 struct lx6464es *chip = entry->private_data;
865
866 snd_iprintf(buffer, "capture levels:\n");
867 err = lx_level_peaks(chip, 1, 64, levels);
868 if (err < 0)
869 return;
870
871 for (i = 0; i != 8; ++i) {
872 for (j = 0; j != 8; ++j)
873 snd_iprintf(buffer, "%08x ", levels[i*8+j]);
874 snd_iprintf(buffer, "\n");
875 }
876
877 snd_iprintf(buffer, "\nplayback levels:\n");
878
879 err = lx_level_peaks(chip, 0, 64, levels);
880 if (err < 0)
881 return;
882
883 for (i = 0; i != 8; ++i) {
884 for (j = 0; j != 8; ++j)
885 snd_iprintf(buffer, "%08x ", levels[i*8+j]);
886 snd_iprintf(buffer, "\n");
887 }
888
889 snd_iprintf(buffer, "\n");
890 }
891
lx_proc_create(struct snd_card * card,struct lx6464es * chip)892 static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
893 {
894 return snd_card_ro_proc_new(card, "levels", chip, lx_proc_levels_read);
895 }
896
897
snd_lx6464es_create(struct snd_card * card,struct pci_dev * pci)898 static int snd_lx6464es_create(struct snd_card *card,
899 struct pci_dev *pci)
900 {
901 struct lx6464es *chip = card->private_data;
902 int err;
903
904 dev_dbg(card->dev, "->snd_lx6464es_create\n");
905
906 /* enable PCI device */
907 err = pcim_enable_device(pci);
908 if (err < 0)
909 return err;
910
911 pci_set_master(pci);
912
913 /* check if we can restrict PCI DMA transfers to 32 bits */
914 err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
915 if (err < 0) {
916 dev_err(card->dev,
917 "architecture does not support 32bit PCI busmaster DMA\n");
918 return -ENXIO;
919 }
920
921 chip->card = card;
922 chip->pci = pci;
923 chip->irq = -1;
924
925 /* initialize synchronization structs */
926 mutex_init(&chip->lock);
927 mutex_init(&chip->msg_lock);
928 mutex_init(&chip->setup_mutex);
929
930 /* request resources */
931 err = pcim_request_all_regions(pci, card_name);
932 if (err < 0)
933 return err;
934
935 /* plx port */
936 chip->port_plx = pci_resource_start(pci, 1);
937 chip->port_plx_remapped = devm_ioport_map(&pci->dev, chip->port_plx,
938 pci_resource_len(pci, 1));
939 if (!chip->port_plx_remapped)
940 return -ENOMEM;
941
942 /* dsp port */
943 chip->port_dsp_bar = pcim_iomap(pci, 2, 0);
944 if (!chip->port_dsp_bar)
945 return -ENOMEM;
946
947 err = devm_request_threaded_irq(&pci->dev, pci->irq, lx_interrupt,
948 lx_threaded_irq, IRQF_SHARED,
949 KBUILD_MODNAME, chip);
950 if (err) {
951 dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
952 return err;
953 }
954 chip->irq = pci->irq;
955 card->sync_irq = chip->irq;
956 card->private_free = snd_lx6464es_free;
957
958 err = lx_init_dsp(chip);
959 if (err < 0) {
960 dev_err(card->dev, "error during DSP initialization\n");
961 return err;
962 }
963
964 err = lx_pcm_create(chip);
965 if (err < 0)
966 return err;
967
968 err = lx_proc_create(card, chip);
969 if (err < 0)
970 return err;
971
972 err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
973 chip));
974 if (err < 0)
975 return err;
976
977 return 0;
978 }
979
snd_lx6464es_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)980 static int snd_lx6464es_probe(struct pci_dev *pci,
981 const struct pci_device_id *pci_id)
982 {
983 static int dev;
984 struct snd_card *card;
985 struct lx6464es *chip;
986 int err;
987
988 dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
989
990 if (dev >= SNDRV_CARDS)
991 return -ENODEV;
992 if (!enable[dev]) {
993 dev++;
994 return -ENOENT;
995 }
996
997 err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
998 sizeof(*chip), &card);
999 if (err < 0)
1000 return err;
1001 chip = card->private_data;
1002
1003 err = snd_lx6464es_create(card, pci);
1004 if (err < 0) {
1005 dev_err(card->dev, "error during snd_lx6464es_create\n");
1006 goto error;
1007 }
1008
1009 strscpy(card->driver, "LX6464ES");
1010 sprintf(card->id, "LX6464ES_%02X%02X%02X",
1011 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1012
1013 sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
1014 chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
1015 chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1016
1017 sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
1018 card->shortname, chip->port_plx,
1019 chip->port_dsp_bar, chip->irq);
1020
1021 err = snd_card_register(card);
1022 if (err < 0)
1023 goto error;
1024
1025 dev_dbg(chip->card->dev, "initialization successful\n");
1026 pci_set_drvdata(pci, card);
1027 dev++;
1028 return 0;
1029
1030 error:
1031 snd_card_free(card);
1032 return err;
1033 }
1034
1035 static struct pci_driver lx6464es_driver = {
1036 .name = KBUILD_MODNAME,
1037 .id_table = snd_lx6464es_ids,
1038 .probe = snd_lx6464es_probe,
1039 };
1040
1041 module_pci_driver(lx6464es_driver);
1042