xref: /openbmc/linux/sound/pci/lx6464es/lx6464es.c (revision fd88d2e5)
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 
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 
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 
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 
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 
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 	mutex_lock(&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 		goto exit;
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 		goto exit;
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 		goto exit;
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 exit:
253 	runtime->private_data = chip;
254 
255 	mutex_unlock(&chip->setup_mutex);
256 	dev_dbg(chip->card->dev, "<-lx_pcm_open, %d\n", err);
257 	return err;
258 }
259 
260 static int lx_pcm_close(struct snd_pcm_substream *substream)
261 {
262 	dev_dbg(substream->pcm->card->dev, "->lx_pcm_close\n");
263 	return 0;
264 }
265 
266 static snd_pcm_uframes_t lx_pcm_stream_pointer(struct snd_pcm_substream
267 					       *substream)
268 {
269 	struct lx6464es *chip = snd_pcm_substream_chip(substream);
270 	snd_pcm_uframes_t pos;
271 	int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
272 
273 	struct lx_stream *lx_stream = is_capture ? &chip->capture_stream :
274 		&chip->playback_stream;
275 
276 	dev_dbg(chip->card->dev, "->lx_pcm_stream_pointer\n");
277 
278 	mutex_lock(&chip->lock);
279 	pos = lx_stream->frame_pos * substream->runtime->period_size;
280 	mutex_unlock(&chip->lock);
281 
282 	dev_dbg(chip->card->dev, "stream_pointer at %ld\n", pos);
283 	return pos;
284 }
285 
286 static int lx_pcm_prepare(struct snd_pcm_substream *substream)
287 {
288 	struct lx6464es *chip = snd_pcm_substream_chip(substream);
289 	int err = 0;
290 	const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
291 
292 	dev_dbg(chip->card->dev, "->lx_pcm_prepare\n");
293 
294 	mutex_lock(&chip->setup_mutex);
295 
296 	if (chip->hardware_running[is_capture]) {
297 		err = lx_hardware_stop(chip, substream);
298 		if (err < 0) {
299 			dev_err(chip->card->dev, "failed to stop hardware. "
300 				   "Error code %d\n", err);
301 			goto exit;
302 		}
303 
304 		err = lx_hardware_close(chip, substream);
305 		if (err < 0) {
306 			dev_err(chip->card->dev, "failed to close hardware. "
307 				   "Error code %d\n", err);
308 			goto exit;
309 		}
310 	}
311 
312 	dev_dbg(chip->card->dev, "opening hardware\n");
313 	err = lx_hardware_open(chip, substream);
314 	if (err < 0) {
315 		dev_err(chip->card->dev, "failed to open hardware. "
316 			   "Error code %d\n", err);
317 		goto exit;
318 	}
319 
320 	err = lx_hardware_start(chip, substream);
321 	if (err < 0) {
322 		dev_err(chip->card->dev, "failed to start hardware. "
323 			   "Error code %d\n", err);
324 		goto exit;
325 	}
326 
327 	chip->hardware_running[is_capture] = 1;
328 
329 	if (chip->board_sample_rate != substream->runtime->rate) {
330 		if (!err)
331 			chip->board_sample_rate = substream->runtime->rate;
332 	}
333 
334 exit:
335 	mutex_unlock(&chip->setup_mutex);
336 	return err;
337 }
338 
339 static int lx_pcm_hw_params(struct snd_pcm_substream *substream,
340 			    struct snd_pcm_hw_params *hw_params, int is_capture)
341 {
342 	struct lx6464es *chip = snd_pcm_substream_chip(substream);
343 
344 	dev_dbg(chip->card->dev, "->lx_pcm_hw_params\n");
345 
346 	mutex_lock(&chip->setup_mutex);
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 0;
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 	if (is_capture)
397 		chip->capture_stream.stream = NULL;
398 	else
399 		chip->playback_stream.stream = NULL;
400 
401 exit:
402 	mutex_unlock(&chip->setup_mutex);
403 	return err;
404 }
405 
406 static void lx_trigger_start(struct lx6464es *chip, struct lx_stream *lx_stream)
407 {
408 	struct snd_pcm_substream *substream = lx_stream->stream;
409 	const unsigned int is_capture = lx_stream->is_capture;
410 
411 	int err;
412 
413 	const u32 channels = substream->runtime->channels;
414 	const u32 bytes_per_frame = channels * 3;
415 	const u32 period_size = substream->runtime->period_size;
416 	const u32 periods = substream->runtime->periods;
417 	const u32 period_bytes = period_size * bytes_per_frame;
418 
419 	dma_addr_t buf = substream->dma_buffer.addr;
420 	int i;
421 
422 	u32 needed, freed;
423 	u32 size_array[5];
424 
425 	for (i = 0; i != periods; ++i) {
426 		u32 buffer_index = 0;
427 
428 		err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed,
429 				    size_array);
430 		dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n",
431 			    needed, freed);
432 
433 		err = lx_buffer_give(chip, 0, is_capture, period_bytes,
434 				     lower_32_bits(buf), upper_32_bits(buf),
435 				     &buffer_index);
436 
437 		dev_dbg(chip->card->dev, "starting: buffer index %x on 0x%lx (%d bytes)\n",
438 			    buffer_index, (unsigned long)buf, period_bytes);
439 		buf += period_bytes;
440 	}
441 
442 	err = lx_buffer_ask(chip, 0, is_capture, &needed, &freed, size_array);
443 	dev_dbg(chip->card->dev, "starting: needed %d, freed %d\n", needed, freed);
444 
445 	dev_dbg(chip->card->dev, "starting: starting stream\n");
446 	err = lx_stream_start(chip, 0, is_capture);
447 	if (err < 0)
448 		dev_err(chip->card->dev, "couldn't start stream\n");
449 	else
450 		lx_stream->status = LX_STREAM_STATUS_RUNNING;
451 
452 	lx_stream->frame_pos = 0;
453 }
454 
455 static void lx_trigger_stop(struct lx6464es *chip, struct lx_stream *lx_stream)
456 {
457 	const unsigned int is_capture = lx_stream->is_capture;
458 	int err;
459 
460 	dev_dbg(chip->card->dev, "stopping: stopping stream\n");
461 	err = lx_stream_stop(chip, 0, is_capture);
462 	if (err < 0)
463 		dev_err(chip->card->dev, "couldn't stop stream\n");
464 	else
465 		lx_stream->status = LX_STREAM_STATUS_FREE;
466 
467 }
468 
469 static void lx_trigger_dispatch_stream(struct lx6464es *chip,
470 				       struct lx_stream *lx_stream)
471 {
472 	switch (lx_stream->status) {
473 	case LX_STREAM_STATUS_SCHEDULE_RUN:
474 		lx_trigger_start(chip, lx_stream);
475 		break;
476 
477 	case LX_STREAM_STATUS_SCHEDULE_STOP:
478 		lx_trigger_stop(chip, lx_stream);
479 		break;
480 
481 	default:
482 		break;
483 	}
484 }
485 
486 static int lx_pcm_trigger_dispatch(struct lx6464es *chip,
487 				   struct lx_stream *lx_stream, int cmd)
488 {
489 	int err = 0;
490 
491 	mutex_lock(&chip->lock);
492 	switch (cmd) {
493 	case SNDRV_PCM_TRIGGER_START:
494 		lx_stream->status = LX_STREAM_STATUS_SCHEDULE_RUN;
495 		break;
496 
497 	case SNDRV_PCM_TRIGGER_STOP:
498 		lx_stream->status = LX_STREAM_STATUS_SCHEDULE_STOP;
499 		break;
500 
501 	default:
502 		err = -EINVAL;
503 		goto exit;
504 	}
505 
506 	lx_trigger_dispatch_stream(chip, &chip->capture_stream);
507 	lx_trigger_dispatch_stream(chip, &chip->playback_stream);
508 
509 exit:
510 	mutex_unlock(&chip->lock);
511 	return err;
512 }
513 
514 
515 static int lx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
516 {
517 	struct lx6464es *chip = snd_pcm_substream_chip(substream);
518 	const int is_capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
519 	struct lx_stream *stream = is_capture ? &chip->capture_stream :
520 		&chip->playback_stream;
521 
522 	dev_dbg(chip->card->dev, "->lx_pcm_trigger\n");
523 
524 	return lx_pcm_trigger_dispatch(chip, stream, cmd);
525 }
526 
527 static int snd_lx6464es_free(struct lx6464es *chip)
528 {
529 	dev_dbg(chip->card->dev, "->snd_lx6464es_free\n");
530 
531 	lx_irq_disable(chip);
532 
533 	if (chip->irq >= 0)
534 		free_irq(chip->irq, chip);
535 
536 	iounmap(chip->port_dsp_bar);
537 	ioport_unmap(chip->port_plx_remapped);
538 
539 	pci_release_regions(chip->pci);
540 	pci_disable_device(chip->pci);
541 
542 	kfree(chip);
543 
544 	return 0;
545 }
546 
547 static int snd_lx6464es_dev_free(struct snd_device *device)
548 {
549 	return snd_lx6464es_free(device->device_data);
550 }
551 
552 /* reset the dsp during initialization */
553 static int lx_init_xilinx_reset(struct lx6464es *chip)
554 {
555 	int i;
556 	u32 plx_reg = lx_plx_reg_read(chip, ePLX_CHIPSC);
557 
558 	dev_dbg(chip->card->dev, "->lx_init_xilinx_reset\n");
559 
560 	/* activate reset of xilinx */
561 	plx_reg &= ~CHIPSC_RESET_XILINX;
562 
563 	lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
564 	msleep(1);
565 
566 	lx_plx_reg_write(chip, ePLX_MBOX3, 0);
567 	msleep(1);
568 
569 	plx_reg |= CHIPSC_RESET_XILINX;
570 	lx_plx_reg_write(chip, ePLX_CHIPSC, plx_reg);
571 
572 	/* deactivate reset of xilinx */
573 	for (i = 0; i != 100; ++i) {
574 		u32 reg_mbox3;
575 		msleep(10);
576 		reg_mbox3 = lx_plx_reg_read(chip, ePLX_MBOX3);
577 		if (reg_mbox3) {
578 			dev_dbg(chip->card->dev, "xilinx reset done\n");
579 			dev_dbg(chip->card->dev, "xilinx took %d loops\n", i);
580 			break;
581 		}
582 	}
583 
584 	/* todo: add some error handling? */
585 
586 	/* clear mr */
587 	lx_dsp_reg_write(chip, eReg_CSM, 0);
588 
589 	/* le xilinx ES peut ne pas etre encore pret, on attend. */
590 	msleep(600);
591 
592 	return 0;
593 }
594 
595 static int lx_init_xilinx_test(struct lx6464es *chip)
596 {
597 	u32 reg;
598 
599 	dev_dbg(chip->card->dev, "->lx_init_xilinx_test\n");
600 
601 	/* TEST if we have access to Xilinx/MicroBlaze */
602 	lx_dsp_reg_write(chip, eReg_CSM, 0);
603 
604 	reg = lx_dsp_reg_read(chip, eReg_CSM);
605 
606 	if (reg) {
607 		dev_err(chip->card->dev, "Problem: Reg_CSM %x.\n", reg);
608 
609 		/* PCI9056_SPACE0_REMAP */
610 		lx_plx_reg_write(chip, ePLX_PCICR, 1);
611 
612 		reg = lx_dsp_reg_read(chip, eReg_CSM);
613 		if (reg) {
614 			dev_err(chip->card->dev, "Error: Reg_CSM %x.\n", reg);
615 			return -EAGAIN; /* seems to be appropriate */
616 		}
617 	}
618 
619 	dev_dbg(chip->card->dev, "Xilinx/MicroBlaze access test successful\n");
620 
621 	return 0;
622 }
623 
624 /* initialize ethersound */
625 static int lx_init_ethersound_config(struct lx6464es *chip)
626 {
627 	int i;
628 	u32 orig_conf_es = lx_dsp_reg_read(chip, eReg_CONFES);
629 
630 	/* configure 64 io channels */
631 	u32 conf_es = (orig_conf_es & CONFES_READ_PART_MASK) |
632 		(64 << IOCR_INPUTS_OFFSET) |
633 		(64 << IOCR_OUTPUTS_OFFSET) |
634 		(FREQ_RATIO_SINGLE_MODE << FREQ_RATIO_OFFSET);
635 
636 	dev_dbg(chip->card->dev, "->lx_init_ethersound\n");
637 
638 	chip->freq_ratio = FREQ_RATIO_SINGLE_MODE;
639 
640 	/*
641 	 * write it to the card !
642 	 * this actually kicks the ES xilinx, the first time since poweron.
643 	 * the MAC address in the Reg_ADMACESMSB Reg_ADMACESLSB registers
644 	 * is not ready before this is done, and the bit 2 in Reg_CSES is set.
645 	 * */
646 	lx_dsp_reg_write(chip, eReg_CONFES, conf_es);
647 
648 	for (i = 0; i != 1000; ++i) {
649 		if (lx_dsp_reg_read(chip, eReg_CSES) & 4) {
650 			dev_dbg(chip->card->dev, "ethersound initialized after %dms\n",
651 				   i);
652 			goto ethersound_initialized;
653 		}
654 		msleep(1);
655 	}
656 	dev_warn(chip->card->dev,
657 		   "ethersound could not be initialized after %dms\n", i);
658 	return -ETIMEDOUT;
659 
660  ethersound_initialized:
661 	dev_dbg(chip->card->dev, "ethersound initialized\n");
662 	return 0;
663 }
664 
665 static int lx_init_get_version_features(struct lx6464es *chip)
666 {
667 	u32 dsp_version;
668 
669 	int err;
670 
671 	dev_dbg(chip->card->dev, "->lx_init_get_version_features\n");
672 
673 	err = lx_dsp_get_version(chip, &dsp_version);
674 
675 	if (err == 0) {
676 		u32 freq;
677 
678 		dev_info(chip->card->dev, "DSP version: V%02d.%02d #%d\n",
679 			   (dsp_version>>16) & 0xff, (dsp_version>>8) & 0xff,
680 			   dsp_version & 0xff);
681 
682 		/* later: what firmware version do we expect? */
683 
684 		/* retrieve Play/Rec features */
685 		/* done here because we may have to handle alternate
686 		 * DSP files. */
687 		/* later */
688 
689 		/* init the EtherSound sample rate */
690 		err = lx_dsp_get_clock_frequency(chip, &freq);
691 		if (err == 0)
692 			chip->board_sample_rate = freq;
693 		dev_dbg(chip->card->dev, "actual clock frequency %d\n", freq);
694 	} else {
695 		dev_err(chip->card->dev, "DSP corrupted \n");
696 		err = -EAGAIN;
697 	}
698 
699 	return err;
700 }
701 
702 static int lx_set_granularity(struct lx6464es *chip, u32 gran)
703 {
704 	int err = 0;
705 	u32 snapped_gran = MICROBLAZE_IBL_MIN;
706 
707 	dev_dbg(chip->card->dev, "->lx_set_granularity\n");
708 
709 	/* blocksize is a power of 2 */
710 	while ((snapped_gran < gran) &&
711 	       (snapped_gran < MICROBLAZE_IBL_MAX)) {
712 		snapped_gran *= 2;
713 	}
714 
715 	if (snapped_gran == chip->pcm_granularity)
716 		return 0;
717 
718 	err = lx_dsp_set_granularity(chip, snapped_gran);
719 	if (err < 0) {
720 		dev_warn(chip->card->dev, "could not set granularity\n");
721 		err = -EAGAIN;
722 	}
723 
724 	if (snapped_gran != gran)
725 		dev_err(chip->card->dev, "snapped blocksize to %d\n", snapped_gran);
726 
727 	dev_dbg(chip->card->dev, "set blocksize on board %d\n", snapped_gran);
728 	chip->pcm_granularity = snapped_gran;
729 
730 	return err;
731 }
732 
733 /* initialize and test the xilinx dsp chip */
734 static int lx_init_dsp(struct lx6464es *chip)
735 {
736 	int err;
737 	int i;
738 
739 	dev_dbg(chip->card->dev, "->lx_init_dsp\n");
740 
741 	dev_dbg(chip->card->dev, "initialize board\n");
742 	err = lx_init_xilinx_reset(chip);
743 	if (err)
744 		return err;
745 
746 	dev_dbg(chip->card->dev, "testing board\n");
747 	err = lx_init_xilinx_test(chip);
748 	if (err)
749 		return err;
750 
751 	dev_dbg(chip->card->dev, "initialize ethersound configuration\n");
752 	err = lx_init_ethersound_config(chip);
753 	if (err)
754 		return err;
755 
756 	lx_irq_enable(chip);
757 
758 	/** \todo the mac address should be ready by not, but it isn't,
759 	 *  so we wait for it */
760 	for (i = 0; i != 1000; ++i) {
761 		err = lx_dsp_get_mac(chip);
762 		if (err)
763 			return err;
764 		if (chip->mac_address[0] || chip->mac_address[1] || chip->mac_address[2] ||
765 		    chip->mac_address[3] || chip->mac_address[4] || chip->mac_address[5])
766 			goto mac_ready;
767 		msleep(1);
768 	}
769 	return -ETIMEDOUT;
770 
771 mac_ready:
772 	dev_dbg(chip->card->dev, "mac address ready read after: %dms\n", i);
773 	dev_info(chip->card->dev,
774 		 "mac address: %02X.%02X.%02X.%02X.%02X.%02X\n",
775 		   chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
776 		   chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
777 
778 	err = lx_init_get_version_features(chip);
779 	if (err)
780 		return err;
781 
782 	lx_set_granularity(chip, MICROBLAZE_IBL_DEFAULT);
783 
784 	chip->playback_mute = 0;
785 
786 	return err;
787 }
788 
789 static const struct snd_pcm_ops lx_ops_playback = {
790 	.open      = lx_pcm_open,
791 	.close     = lx_pcm_close,
792 	.prepare   = lx_pcm_prepare,
793 	.hw_params = lx_pcm_hw_params_playback,
794 	.hw_free   = lx_pcm_hw_free,
795 	.trigger   = lx_pcm_trigger,
796 	.pointer   = lx_pcm_stream_pointer,
797 };
798 
799 static const struct snd_pcm_ops lx_ops_capture = {
800 	.open      = lx_pcm_open,
801 	.close     = lx_pcm_close,
802 	.prepare   = lx_pcm_prepare,
803 	.hw_params = lx_pcm_hw_params_capture,
804 	.hw_free   = lx_pcm_hw_free,
805 	.trigger   = lx_pcm_trigger,
806 	.pointer   = lx_pcm_stream_pointer,
807 };
808 
809 static int lx_pcm_create(struct lx6464es *chip)
810 {
811 	int err;
812 	struct snd_pcm *pcm;
813 
814 	u32 size = 64 *		     /* channels */
815 		3 *		     /* 24 bit samples */
816 		MAX_STREAM_BUFFER *  /* periods */
817 		MICROBLAZE_IBL_MAX * /* frames per period */
818 		2;		     /* duplex */
819 
820 	size = PAGE_ALIGN(size);
821 
822 	/* hardcoded device name & channel count */
823 	err = snd_pcm_new(chip->card, (char *)card_name, 0,
824 			  1, 1, &pcm);
825 	if (err < 0)
826 		return err;
827 
828 	pcm->private_data = chip;
829 
830 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &lx_ops_playback);
831 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &lx_ops_capture);
832 
833 	pcm->info_flags = 0;
834 	pcm->nonatomic = true;
835 	strcpy(pcm->name, card_name);
836 
837 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
838 				       &chip->pci->dev, size, size);
839 
840 	chip->pcm = pcm;
841 	chip->capture_stream.is_capture = 1;
842 
843 	return 0;
844 }
845 
846 static int lx_control_playback_info(struct snd_kcontrol *kcontrol,
847 				    struct snd_ctl_elem_info *uinfo)
848 {
849 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
850 	uinfo->count = 1;
851 	uinfo->value.integer.min = 0;
852 	uinfo->value.integer.max = 1;
853 	return 0;
854 }
855 
856 static int lx_control_playback_get(struct snd_kcontrol *kcontrol,
857 				   struct snd_ctl_elem_value *ucontrol)
858 {
859 	struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
860 	ucontrol->value.integer.value[0] = chip->playback_mute;
861 	return 0;
862 }
863 
864 static int lx_control_playback_put(struct snd_kcontrol *kcontrol,
865 				   struct snd_ctl_elem_value *ucontrol)
866 {
867 	struct lx6464es *chip = snd_kcontrol_chip(kcontrol);
868 	int changed = 0;
869 	int current_value = chip->playback_mute;
870 
871 	if (current_value != ucontrol->value.integer.value[0]) {
872 		lx_level_unmute(chip, 0, !current_value);
873 		chip->playback_mute = !current_value;
874 		changed = 1;
875 	}
876 	return changed;
877 }
878 
879 static const struct snd_kcontrol_new lx_control_playback_switch = {
880 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
881 	.name = "PCM Playback Switch",
882 	.index = 0,
883 	.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
884 	.private_value = 0,
885 	.info = lx_control_playback_info,
886 	.get = lx_control_playback_get,
887 	.put = lx_control_playback_put
888 };
889 
890 
891 
892 static void lx_proc_levels_read(struct snd_info_entry *entry,
893 				struct snd_info_buffer *buffer)
894 {
895 	u32 levels[64];
896 	int err;
897 	int i, j;
898 	struct lx6464es *chip = entry->private_data;
899 
900 	snd_iprintf(buffer, "capture levels:\n");
901 	err = lx_level_peaks(chip, 1, 64, levels);
902 	if (err < 0)
903 		return;
904 
905 	for (i = 0; i != 8; ++i) {
906 		for (j = 0; j != 8; ++j)
907 			snd_iprintf(buffer, "%08x ", levels[i*8+j]);
908 		snd_iprintf(buffer, "\n");
909 	}
910 
911 	snd_iprintf(buffer, "\nplayback levels:\n");
912 
913 	err = lx_level_peaks(chip, 0, 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, "\n");
924 }
925 
926 static int lx_proc_create(struct snd_card *card, struct lx6464es *chip)
927 {
928 	return snd_card_ro_proc_new(card, "levels", chip, lx_proc_levels_read);
929 }
930 
931 
932 static int snd_lx6464es_create(struct snd_card *card,
933 			       struct pci_dev *pci,
934 			       struct lx6464es **rchip)
935 {
936 	struct lx6464es *chip;
937 	int err;
938 
939 	static const struct snd_device_ops ops = {
940 		.dev_free = snd_lx6464es_dev_free,
941 	};
942 
943 	dev_dbg(card->dev, "->snd_lx6464es_create\n");
944 
945 	*rchip = NULL;
946 
947 	/* enable PCI device */
948 	err = pci_enable_device(pci);
949 	if (err < 0)
950 		return err;
951 
952 	pci_set_master(pci);
953 
954 	/* check if we can restrict PCI DMA transfers to 32 bits */
955 	err = dma_set_mask(&pci->dev, DMA_BIT_MASK(32));
956 	if (err < 0) {
957 		dev_err(card->dev,
958 			"architecture does not support 32bit PCI busmaster DMA\n");
959 		pci_disable_device(pci);
960 		return -ENXIO;
961 	}
962 
963 	chip = kzalloc(sizeof(*chip), GFP_KERNEL);
964 	if (chip == NULL) {
965 		err = -ENOMEM;
966 		goto alloc_failed;
967 	}
968 
969 	chip->card = card;
970 	chip->pci = pci;
971 	chip->irq = -1;
972 
973 	/* initialize synchronization structs */
974 	mutex_init(&chip->lock);
975 	mutex_init(&chip->msg_lock);
976 	mutex_init(&chip->setup_mutex);
977 
978 	/* request resources */
979 	err = pci_request_regions(pci, card_name);
980 	if (err < 0)
981 		goto request_regions_failed;
982 
983 	/* plx port */
984 	chip->port_plx = pci_resource_start(pci, 1);
985 	chip->port_plx_remapped = ioport_map(chip->port_plx,
986 					     pci_resource_len(pci, 1));
987 
988 	/* dsp port */
989 	chip->port_dsp_bar = pci_ioremap_bar(pci, 2);
990 	if (!chip->port_dsp_bar) {
991 		dev_err(card->dev, "cannot remap PCI memory region\n");
992 		err = -ENOMEM;
993 		goto remap_pci_failed;
994 	}
995 
996 	err = request_threaded_irq(pci->irq, lx_interrupt, lx_threaded_irq,
997 				   IRQF_SHARED, KBUILD_MODNAME, chip);
998 	if (err) {
999 		dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
1000 		goto request_irq_failed;
1001 	}
1002 	chip->irq = pci->irq;
1003 	card->sync_irq = chip->irq;
1004 
1005 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1006 	if (err < 0)
1007 		goto device_new_failed;
1008 
1009 	err = lx_init_dsp(chip);
1010 	if (err < 0) {
1011 		dev_err(card->dev, "error during DSP initialization\n");
1012 		return err;
1013 	}
1014 
1015 	err = lx_pcm_create(chip);
1016 	if (err < 0)
1017 		return err;
1018 
1019 	err = lx_proc_create(card, chip);
1020 	if (err < 0)
1021 		return err;
1022 
1023 	err = snd_ctl_add(card, snd_ctl_new1(&lx_control_playback_switch,
1024 					     chip));
1025 	if (err < 0)
1026 		return err;
1027 
1028 	*rchip = chip;
1029 	return 0;
1030 
1031 device_new_failed:
1032 	free_irq(pci->irq, chip);
1033 
1034 request_irq_failed:
1035 	iounmap(chip->port_dsp_bar);
1036 
1037 remap_pci_failed:
1038 	pci_release_regions(pci);
1039 
1040 request_regions_failed:
1041 	kfree(chip);
1042 
1043 alloc_failed:
1044 	pci_disable_device(pci);
1045 
1046 	return err;
1047 }
1048 
1049 static int snd_lx6464es_probe(struct pci_dev *pci,
1050 			      const struct pci_device_id *pci_id)
1051 {
1052 	static int dev;
1053 	struct snd_card *card;
1054 	struct lx6464es *chip;
1055 	int err;
1056 
1057 	dev_dbg(&pci->dev, "->snd_lx6464es_probe\n");
1058 
1059 	if (dev >= SNDRV_CARDS)
1060 		return -ENODEV;
1061 	if (!enable[dev]) {
1062 		dev++;
1063 		return -ENOENT;
1064 	}
1065 
1066 	err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
1067 			   0, &card);
1068 	if (err < 0)
1069 		return err;
1070 
1071 	err = snd_lx6464es_create(card, pci, &chip);
1072 	if (err < 0) {
1073 		dev_err(card->dev, "error during snd_lx6464es_create\n");
1074 		goto out_free;
1075 	}
1076 
1077 	strcpy(card->driver, "LX6464ES");
1078 	sprintf(card->id, "LX6464ES_%02X%02X%02X",
1079 		chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1080 
1081 	sprintf(card->shortname, "LX6464ES %02X.%02X.%02X.%02X.%02X.%02X",
1082 		chip->mac_address[0], chip->mac_address[1], chip->mac_address[2],
1083 		chip->mac_address[3], chip->mac_address[4], chip->mac_address[5]);
1084 
1085 	sprintf(card->longname, "%s at 0x%lx, 0x%p, irq %i",
1086 		card->shortname, chip->port_plx,
1087 		chip->port_dsp_bar, chip->irq);
1088 
1089 	err = snd_card_register(card);
1090 	if (err < 0)
1091 		goto out_free;
1092 
1093 	dev_dbg(chip->card->dev, "initialization successful\n");
1094 	pci_set_drvdata(pci, card);
1095 	dev++;
1096 	return 0;
1097 
1098 out_free:
1099 	snd_card_free(card);
1100 	return err;
1101 
1102 }
1103 
1104 static void snd_lx6464es_remove(struct pci_dev *pci)
1105 {
1106 	snd_card_free(pci_get_drvdata(pci));
1107 }
1108 
1109 
1110 static struct pci_driver lx6464es_driver = {
1111 	.name =     KBUILD_MODNAME,
1112 	.id_table = snd_lx6464es_ids,
1113 	.probe =    snd_lx6464es_probe,
1114 	.remove = snd_lx6464es_remove,
1115 };
1116 
1117 module_pci_driver(lx6464es_driver);
1118