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