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