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