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