patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / sound / parisc / harmony.c
1 /*
2  *  Harmony chipset driver
3  *
4  *      This is a sound driver for ASP's and Lasi's Harmony sound chip
5  *      and is unlikely to be used for anything other than on a HP PA-RISC.
6  *
7  *      Harmony is found in HP 712s, 715/new and many other GSC based machines.
8  *      On older 715 machines you'll find the technically identical chip 
9  *      called 'Vivace'. Both Harmony and Vicace are supported by this driver.
10  *
11  *  this ALSA driver is based on OSS driver by:
12  *      Copyright 2000 (c) Linuxcare Canada, Alex deVries <alex@linuxcare.com>
13  *      Copyright 2000-2002 (c) Helge Deller <deller@gmx.de>
14  *      Copyright 2001 (c) Matthieu Delahaye <delahaym@esiee.fr>
15  *
16  * TODO:
17  * - use generic DMA interface and ioremap()/iounmap()
18  * - capture is still untested (and probaby non-working)
19  * - spin locks
20  * - implement non-consistent DMA pages
21  * - implement gain meter
22  * - module parameters
23  * - correct cleaning sequence
24  * - better error checking
25  * - try to have a better quality.
26  *   
27  */
28
29 /*
30  * Harmony chipset 'modus operandi'.
31  * - This chipset is found in some HP 32bit workstations, like 712, or B132 class.
32  * most of controls are done through registers. Register are found at a fixed offset
33  * from the hard physical adress, given in struct dev by register_parisc_driver.
34  *
35  * Playback and recording use 4kb pages (dma or not, depending on the machine).
36  *
37  * Most of PCM playback & capture is done through interrupt. When harmony needs
38  * a new buffer to put recorded data or read played PCM, it sends an interrupt.
39  * Bits 2 and 10 of DSTATUS register are '1' when harmony needs respectively
40  * a new page for recording and playing. 
41  * Interrupt are disabled/enabled by writing to bit 32 of DSTATUS. 
42  * Adresses of next page to be played is put in PNXTADD register, next page
43  * to be recorded is put in RNXTADD. There is 2 read-only registers, PCURADD and 
44  * RCURADD that provides adress of current page.
45  * 
46  * Harmony has no way to controll full duplex or half duplex mode. It means
47  * that we always need to provide adresses of playback and capture data, even
48  * when this is not needed. That's why we statically alloc one graveyard
49  * buffer (to put recorded data in play-only mode) and a silence buffer.
50  * 
51  * Bitrate, number of channels and data format are controlled with
52  * the CNTL register.
53  *
54  * Mixer work is done through one register (GAINCTL). Only input gain,
55  * output attenuation and general attenuation control is provided. There is
56  * also controls for enabling/disabling internal speaker and line
57  * input.
58  *
59  * Buffers used by this driver are all DMA consistent.
60  */
61
62 #include <linux/delay.h>
63 #include <sound/driver.h>
64 #include <linux/init.h>
65 #include <linux/interrupt.h>
66 #include <linux/slab.h>
67 #include <linux/time.h>
68 #include <linux/wait.h>
69 #include <linux/moduleparam.h>
70 #include <sound/core.h>
71 #include <sound/control.h>
72 #include <sound/pcm.h>
73 #include <sound/rawmidi.h>
74 #include <sound/initval.h>
75 #include <sound/info.h>
76 #include <asm/hardware.h>
77 #include <asm/io.h>
78 #include <asm/parisc-device.h>
79
80 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>");
81 MODULE_DESCRIPTION("ALSA Harmony sound driver");
82 MODULE_LICENSE("GPL");
83 MODULE_CLASSES("{sound}");
84 MODULE_DEVICES("{{ALSA,Harmony soundcard}}");
85
86 #undef DEBUG
87 #ifdef DEBUG
88 # define DPRINTK printk 
89 #else
90 # define DPRINTK(x,...)
91 #endif
92
93 #define PFX     "harmony: "
94
95 #define MAX_PCM_DEVICES         1
96 #define MAX_PCM_SUBSTREAMS      4
97 #define MAX_MIDI_DEVICES        0
98
99 #define HARMONY_BUF_SIZE        4096
100 #define MAX_BUFS                10
101 #define MAX_BUFFER_SIZE         (MAX_BUFS * HARMONY_BUF_SIZE)
102
103 /* number of silence & graveyard buffers */
104 #define GRAVEYARD_BUFS          3
105 #define SILENCE_BUFS            3
106
107 #define HARMONY_CNTL_C          0x80000000
108
109 #define HARMONY_DSTATUS_PN      0x00000200
110 #define HARMONY_DSTATUS_RN      0x00000002
111 #define HARMONY_DSTATUS_IE      0x80000000
112
113 #define HARMONY_DF_16BIT_LINEAR 0x00000000
114 #define HARMONY_DF_8BIT_ULAW    0x00000001
115 #define HARMONY_DF_8BIT_ALAW    0x00000002
116
117 #define HARMONY_SS_MONO         0x00000000
118 #define HARMONY_SS_STEREO       0x00000001
119
120 /*
121  * Channels Mask in mixer register
122  * try some "reasonable" default gain values
123  */
124
125 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
126
127 /* the following should be enough (mixer is 
128  * very sensible on harmony)
129  */
130 #define HARMONY_GAIN_DEFAULT       0x0F2FF082
131
132
133 /* useless since only one card is supported ATM */
134 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
135 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
136 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
137 static int boot_devs;
138
139 module_param_array(index, int, boot_devs, 0444);
140 MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
141 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
142 module_param_array(id, charp, boot_devs, 0444);
143 MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
144 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
145 module_param_array(enable, bool, boot_devs, 0444);
146 MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
147 MODULE_PARM_SYNTAX(enable, SNDRV_ENABLE_DESC);
148
149 /* Register offset (from base hpa) */
150 #define REG_ID          0x00
151 #define REG_RESET       0x04
152 #define REG_CNTL        0x08
153 #define REG_GAINCTL     0x0C
154 #define REG_PNXTADD     0x10
155 #define REG_PCURADD     0x14
156 #define REG_RNXTADD     0x18
157 #define REG_RCURADD     0x1C
158 #define REG_DSTATUS     0x20
159 #define REG_OV          0x24
160 #define REG_PIO         0x28
161 #define REG_DIAG        0x3C
162
163 /*
164  * main harmony structure
165  */
166
167 typedef struct snd_card_harmony {
168
169         /* spinlocks (To be done) */
170         spinlock_t mixer_lock;
171         spinlock_t control_lock;
172
173         /* parameters */        
174         int irq;
175         unsigned long hpa;
176         int id;
177         int rev;
178         
179         u32 current_gain;
180         int data_format;                /* HARMONY_DF_xx_BIT_xxx */
181         int sample_rate;                /* HARMONY_SR_xx_KHZ */
182         int stereo_select;      /* HARMONY_SS_MONO or HARMONY_SS_STEREO */
183         int format_initialized;
184         
185         unsigned long ply_buffer;
186         int ply_buf;
187         int ply_count;
188         int ply_size;
189         int ply_stopped;
190         int ply_total;
191         
192         unsigned long cap_buffer;
193         int cap_buf;
194         int cap_count;
195         int cap_size;
196         int cap_stopped;
197         int cap_total;
198
199         struct parisc_device *pa_dev;
200
201         struct snd_dma_device dma_dev;
202
203         /* the graveyard buffer is used as recording buffer when playback, 
204          * because harmony always want a buffer to put recorded data */
205         struct snd_dma_buffer graveyard_dma;
206         int graveyard_count;
207         
208         /* same thing for silence buffer */
209         struct snd_dma_buffer silence_dma;
210         int silence_count;
211
212         /* alsa stuff */
213         snd_card_t *card;
214         snd_pcm_t *pcm;
215         snd_pcm_substream_t *playback_substream;
216         snd_pcm_substream_t *capture_substream;
217         snd_info_entry_t *proc_entry;
218 } snd_card_harmony_t;
219 #define chip_t snd_card_harmony_t
220
221 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
222
223 /* wait to be out of control mode */
224 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
225 {
226         int timeout = 5000;
227
228         while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
229         {
230                 /* Wait */ ;    
231         }
232         if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
233 }
234
235
236 /*
237  * sample rate routines 
238  */
239 static unsigned int snd_card_harmony_rates[] = {
240         5125, 6615, 8000, 9600,
241         11025, 16000, 18900, 22050,
242         27428, 32000, 33075, 37800,
243         44100, 48000
244 };
245
246 #define RATES sizeof(snd_card_harmony_rates) / sizeof(snd_card_harmony_rates[0])
247
248 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
249         .count = RATES,
250         .list = snd_card_harmony_rates,
251         .mask = 0,
252 };
253
254 #define HARMONY_SR_8KHZ         0x08
255 #define HARMONY_SR_16KHZ        0x09
256 #define HARMONY_SR_27KHZ        0x0A
257 #define HARMONY_SR_32KHZ        0x0B
258 #define HARMONY_SR_48KHZ        0x0E
259 #define HARMONY_SR_9KHZ         0x0F
260 #define HARMONY_SR_5KHZ         0x10
261 #define HARMONY_SR_11KHZ        0x11
262 #define HARMONY_SR_18KHZ        0x12
263 #define HARMONY_SR_22KHZ        0x13
264 #define HARMONY_SR_37KHZ        0x14
265 #define HARMONY_SR_44KHZ        0x15
266 #define HARMONY_SR_33KHZ        0x16
267 #define HARMONY_SR_6KHZ         0x17
268
269 /* bits corresponding to the entries of snd_card_harmony_rates */
270 static unsigned int rate_bits[14] = {
271         HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
272         HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
273         HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
274         HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
275         HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
276 };
277
278 /* snd_card_harmony_rate_bits
279  * @rate:       index of current data rate in list
280  * returns: harmony hex code for registers
281  */
282 static unsigned int snd_card_harmony_rate_bits(int rate)
283 {
284         unsigned int idx;
285         
286         for (idx = 0; idx <= RATES; idx++)
287                 if (snd_card_harmony_rates[idx] == rate)
288                         return rate_bits[idx];
289         return HARMONY_SR_44KHZ; /* fallback */
290 }
291
292 /*
293  * update controls (data format, sample rate, number of channels)
294  * according to value supplied in data structure
295  */
296 void snd_harmony_update_control(snd_card_harmony_t *harmony) 
297 {
298         u32 default_cntl;
299         
300         /* Set CNTL */
301         default_cntl = (HARMONY_CNTL_C |        /* The C bit */
302                 (harmony->data_format << 6) |   /* Set the data format */
303                 (harmony->stereo_select << 5) | /* Stereo select */
304                 (harmony->sample_rate));                /* Set sample rate */
305         
306         /* initialize CNTL */
307         snd_harmony_wait_cntl(harmony);
308         
309         gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
310         
311 }
312
313 /*
314  * interruption controls routines
315  */
316
317 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip) 
318 {
319         snd_harmony_wait_cntl(chip);
320         gsc_writel(0, chip->hpa+REG_DSTATUS); 
321 }
322
323 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip) 
324 {
325         snd_harmony_wait_cntl(chip);
326         gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS); 
327 }
328
329 /*
330  * interruption routine:
331  * The interrupt routine must provide adresse of next physical pages 
332  * used by harmony
333  */
334 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
335 {
336         snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
337         u32 dstatus = 0;
338         unsigned long hpa = harmony->hpa;
339         
340         /* Turn off interrupts */
341         snd_harmony_disable_interrupts(harmony);
342         
343         /* wait for control to free */
344         snd_harmony_wait_cntl(harmony);
345         
346         /* Read dstatus and pcuradd (the current address) */
347         dstatus = gsc_readl(hpa+REG_DSTATUS);
348         
349         /* Check if this is a request to get the next play buffer */
350         if (dstatus & HARMONY_DSTATUS_PN) {
351                 if (harmony->playback_substream) {
352                         harmony->ply_buf += harmony->ply_count;
353                         harmony->ply_buf %= harmony->ply_size;
354                 
355                         gsc_writel(harmony->ply_buffer + harmony->ply_buf,
356                                         hpa+REG_PNXTADD);
357                 
358                         snd_pcm_period_elapsed(harmony->playback_substream);
359                         harmony->ply_total++;
360                 } else {
361                         gsc_writel(harmony->silence_dma.addr + 
362                                    (HARMONY_BUF_SIZE*harmony->silence_count),
363                                    hpa+REG_PNXTADD);
364                         harmony->silence_count++;
365                         harmony->silence_count %= SILENCE_BUFS;
366                 }
367         }
368         
369         /* Check if we're being asked to fill in a recording buffer */
370         if (dstatus & HARMONY_DSTATUS_RN) {
371                 if (harmony->capture_substream) {
372                         harmony->cap_buf += harmony->cap_count;
373                         harmony->cap_buf %= harmony->cap_size;
374                 
375                         gsc_writel(harmony->cap_buffer + harmony->cap_buf,
376                                         hpa+REG_RNXTADD);
377                 
378                         snd_pcm_period_elapsed(harmony->capture_substream);
379                         harmony->cap_total++;
380                 } else {
381                         /* graveyard buffer */
382                         gsc_writel(harmony->graveyard_dma.addr +
383                                    (HARMONY_BUF_SIZE*harmony->graveyard_count),
384                                    hpa+REG_RNXTADD);
385                         harmony->graveyard_count++;
386                         harmony->graveyard_count %= GRAVEYARD_BUFS;
387                 }
388         }
389         snd_harmony_enable_interrupts(harmony);
390
391         return IRQ_HANDLED;
392 }
393
394 /* 
395  * proc entry
396  * this proc file will give some debugging info
397  */
398
399 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
400 {
401         snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
402
403         snd_iprintf(buffer, "LASI Harmony driver\nLaurent Canet <canetl@esiee.fr>\n\n");
404         snd_iprintf(buffer, "IRQ %d, hpa %lx, id %d rev %d\n",
405                         harmony->irq, harmony->hpa,
406                         harmony->id, harmony->rev);
407         snd_iprintf(buffer, "Current gain %lx\n", (unsigned long) harmony->current_gain);
408         snd_iprintf(buffer, "\tsample rate=%d\n", harmony->sample_rate);
409         snd_iprintf(buffer, "\tstereo select=%d\n", harmony->stereo_select);
410         snd_iprintf(buffer, "\tbitperchan=%d\n\n", harmony->data_format);
411         
412         snd_iprintf(buffer, "Play status:\n");
413         snd_iprintf(buffer, "\tstopped %d\n", harmony->ply_stopped);
414         snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->ply_buffer, harmony->ply_count);
415         snd_iprintf(buffer, "\tbuf %d size %d\n\n", harmony->ply_buf, harmony->ply_size);
416         
417         snd_iprintf(buffer, "Capture status:\n");
418         snd_iprintf(buffer, "\tstopped %d\n", harmony->cap_stopped);
419         snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->cap_buffer, harmony->cap_count);
420         snd_iprintf(buffer, "\tbuf %d, size %d\n\n", harmony->cap_buf, harmony->cap_size);
421
422         snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
423                 
424         snd_iprintf(buffer, "Register:\n");
425         snd_iprintf(buffer, "\tgainctl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_GAINCTL));
426         snd_iprintf(buffer, "\tcntl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_CNTL));
427         snd_iprintf(buffer, "\tid: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_ID));
428         snd_iprintf(buffer, "\tpcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PCURADD));
429         snd_iprintf(buffer, "\trcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RCURADD));
430         snd_iprintf(buffer, "\tpnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PNXTADD));
431         snd_iprintf(buffer, "\trnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RNXTADD));
432         snd_iprintf(buffer, "\tdstatus: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_DSTATUS));
433         snd_iprintf(buffer, "\tov: %lx\n\n", (unsigned long) gsc_readl(harmony->hpa+REG_OV));
434         
435 }
436
437 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
438 {
439         snd_info_entry_t *entry;
440         
441         if (! snd_card_proc_new(harmony->card, "harmony", &entry))
442                 snd_info_set_text_ops(entry, harmony, 2048, snd_harmony_proc_read);
443 }
444
445 /* 
446  * PCM Stuff
447  */
448
449 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
450                                          unsigned int cmd,
451                                          void *arg)
452 {
453         return snd_pcm_lib_ioctl(substream, cmd, arg);
454 }
455
456 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
457                                         unsigned int cmd,
458                                         void *arg)
459 {
460         return snd_pcm_lib_ioctl(substream, cmd, arg);
461 }
462
463 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
464                                            int cmd)
465 {
466         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
467         
468         switch (cmd) {
469                 case SNDRV_PCM_TRIGGER_STOP:
470                         if (harmony->ply_stopped) 
471                                 return -EBUSY;
472                         harmony->ply_stopped = 1;
473                         snd_harmony_disable_interrupts(harmony);
474                         break;
475                 case SNDRV_PCM_TRIGGER_START:
476                         if (!harmony->ply_stopped)
477                                 return -EBUSY;
478                         harmony->ply_stopped = 0;
479                         /* write the location of the first buffer to play */
480                         gsc_writel(harmony->ply_buffer, harmony->hpa+REG_PNXTADD);
481                         snd_harmony_enable_interrupts(harmony);
482                         break;
483                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
484                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
485                 case SNDRV_PCM_TRIGGER_SUSPEND:
486                         DPRINTK(KERN_INFO PFX "received unimplemented trigger: %d\n", cmd);
487                 default:
488                         return -EINVAL;
489         }
490         return 0;
491 }
492
493 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
494                                           int cmd)
495 {
496         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
497         
498         switch (cmd) {
499                 case SNDRV_PCM_TRIGGER_STOP:
500                         if (harmony->cap_stopped) 
501                                 return -EBUSY;
502                         harmony->cap_stopped = 1;
503                         snd_harmony_disable_interrupts(harmony);
504                         break;
505                 case SNDRV_PCM_TRIGGER_START:
506                         if (!harmony->cap_stopped)
507                                 return -EBUSY;
508                         harmony->cap_stopped = 0;
509                         snd_harmony_enable_interrupts(harmony);
510                         break;
511                 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
512                 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
513                 case SNDRV_PCM_TRIGGER_SUSPEND:
514                         DPRINTK(KERN_INFO PFX "Received unimplemented trigger: %d\n", cmd);
515                 default:
516                         return -EINVAL;
517         }
518         return 0;
519 }
520
521 /* set data format */
522 static int snd_harmony_set_data_format(snd_card_harmony_t *harmony, int pcm_format)
523 {
524         int old_format = harmony->data_format;
525         int new_format = old_format;
526         switch (pcm_format) {
527         case SNDRV_PCM_FORMAT_S16_BE:
528                 new_format = HARMONY_DF_16BIT_LINEAR;
529                 break;
530         case SNDRV_PCM_FORMAT_A_LAW:
531                 new_format = HARMONY_DF_8BIT_ALAW;
532                 break;
533         case SNDRV_PCM_FORMAT_MU_LAW:
534                 new_format = HARMONY_DF_8BIT_ULAW;
535                 break;
536         }
537         /* re-initialize silence buffer if needed */
538         if (old_format != new_format)
539                 snd_pcm_format_set_silence(pcm_format, harmony->silence_dma.area,
540                                            (HARMONY_BUF_SIZE * SILENCE_BUFS * 8) / snd_pcm_format_width(pcm_format));
541
542         return new_format;
543 }
544
545 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
546 {
547         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
548         snd_pcm_runtime_t *runtime = substream->runtime;
549         
550         harmony->ply_size                       = snd_pcm_lib_buffer_bytes(substream);
551         harmony->ply_count                      = snd_pcm_lib_period_bytes(substream);
552         harmony->ply_buf                        = 0;
553         harmony->ply_stopped            = 1;
554         
555         /* initialize given sample rate */
556         harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
557
558         /* data format */
559         harmony->data_format = snd_harmony_set_data_format(haromny, runtime->format);
560
561         /* number of channels */
562         if (runtime->channels == 2)
563                 harmony->stereo_select = HARMONY_SS_STEREO;
564         else
565                 harmony->stereo_select = HARMONY_SS_MONO;
566         
567         DPRINTK(KERN_INFO PFX "Playback_prepare, sr=%d(%x), df=%x, ss=%x hpa=%lx\n", runtime->rate,
568                                 harmony->sample_rate, harmony->data_format, harmony->stereo_select, harmony->hpa);
569         snd_harmony_update_control(harmony);
570         harmony->format_initialized = 1;
571         harmony->ply_buffer = runtime->dma_addr;
572         
573         return 0;
574 }
575
576 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
577 {
578         snd_pcm_runtime_t *runtime = substream->runtime;
579         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
580         
581         harmony->cap_size                       = snd_pcm_lib_buffer_bytes(substream);
582         harmony->cap_count                      = snd_pcm_lib_period_bytes(substream);
583         harmony->cap_count                      = 0;
584         harmony->cap_stopped            = 1;
585
586         /* initialize given sample rate */
587         harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
588         
589         /* data format */
590         harmony->data_format = snd_harmony_set_data_format(haromny, runtime->format);
591         
592         /* number of channels */
593         if (runtime->channels == 1)
594                 harmony->stereo_select = HARMONY_SS_MONO;
595         else if (runtime->channels == 2)
596                 harmony->stereo_select = HARMONY_SS_STEREO;
597                 
598         snd_harmony_update_control(harmony);
599         harmony->format_initialized = 1;
600         
601         harmony->cap_buffer = runtime->dma_addr;
602
603         return 0;
604 }
605
606 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
607 {
608         snd_pcm_runtime_t *runtime = substream->runtime;
609         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
610         unsigned long rcuradd;
611         int recorded;
612         
613         if (harmony->cap_stopped) return 0;
614         if (harmony->capture_substream == NULL) return 0;
615
616         rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
617         recorded = (rcuradd - harmony->cap_buffer);
618         recorded %= harmony->cap_size;
619                 
620         return bytes_to_frames(runtime, recorded);
621 }
622
623 /*
624  */
625
626 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
627 {
628         snd_pcm_runtime_t *runtime = substream->runtime;
629         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
630         int played;
631         long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
632         
633         if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
634         if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
635         
636         played = (pcuradd - harmony->ply_buffer);
637         
638         printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);       
639
640         if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
641         
642         return bytes_to_frames(runtime, played);
643 }
644
645 static snd_pcm_hardware_t snd_card_harmony_playback =
646 {
647         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 
648                                         SNDRV_PCM_INFO_JOINT_DUPLEX | 
649                                         SNDRV_PCM_INFO_MMAP_VALID |
650                                         SNDRV_PCM_INFO_BLOCK_TRANSFER),
651         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE | 
652                                         SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
653         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
654         .rate_min =             5500,
655         .rate_max =             48000,
656         .channels_min =         1,
657         .channels_max =         2,
658         .buffer_bytes_max =     MAX_BUFFER_SIZE,
659         .period_bytes_min =     HARMONY_BUF_SIZE,
660         .period_bytes_max =     HARMONY_BUF_SIZE,
661         .periods_min =          1,
662         .periods_max =          MAX_BUFS,
663         .fifo_size =            0,
664 };
665
666 static snd_pcm_hardware_t snd_card_harmony_capture =
667 {
668         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | 
669                                         SNDRV_PCM_INFO_JOINT_DUPLEX | 
670                                         SNDRV_PCM_INFO_MMAP_VALID |
671                                         SNDRV_PCM_INFO_BLOCK_TRANSFER),
672         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE | 
673                                         SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
674         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
675         .rate_min =             5500,
676         .rate_max =             48000,
677         .channels_min =         1,
678         .channels_max =         2,
679         .buffer_bytes_max =     MAX_BUFFER_SIZE,
680         .period_bytes_min =     HARMONY_BUF_SIZE,
681         .period_bytes_max =     HARMONY_BUF_SIZE,
682         .periods_min =          1,
683         .periods_max =          MAX_BUFS,
684         .fifo_size =            0,
685 };
686
687 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
688 {
689         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
690         snd_pcm_runtime_t *runtime = substream->runtime;
691         int err;
692         
693         harmony->playback_substream = substream;
694         runtime->hw = snd_card_harmony_playback;
695         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
696         
697         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
698                 return err;
699         
700         return 0;
701 }
702
703 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
704 {
705         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
706         snd_pcm_runtime_t *runtime = substream->runtime;
707         int err;
708         
709         harmony->capture_substream = substream;
710         runtime->hw = snd_card_harmony_capture;
711         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
712         if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
713                 return err;
714         return 0;
715
716 }
717
718 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
719 {
720         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
721         
722         harmony->playback_substream = NULL;
723         harmony->ply_size                       = 0;
724         harmony->ply_buf                        = 0;
725         harmony->ply_buffer                     = 0;
726         harmony->ply_count                      = 0;
727         harmony->ply_stopped            = 1;
728         harmony->format_initialized = 0;
729         
730         return 0;
731 }
732
733 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
734 {
735         snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
736         
737         harmony->capture_substream = NULL;
738         harmony->cap_size                       = 0;
739         harmony->cap_buf                        = 0;
740         harmony->cap_buffer                     = 0;
741         harmony->cap_count                      = 0;
742         harmony->cap_stopped            = 1;
743         harmony->format_initialized = 0;
744         
745         return 0;
746 }
747
748 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream, 
749                            snd_pcm_hw_params_t * hw_params)
750 {
751         int err;
752         
753         err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
754         DPRINTK(KERN_INFO PFX "HW Params returned %d, dma_addr %lx\n", err,
755                         (unsigned long)substream->runtime->dma_addr);
756         return err;
757 }
758
759 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream) 
760 {
761         snd_pcm_lib_free_pages(substream);              
762         return 0;
763 }
764
765 static snd_pcm_ops_t snd_card_harmony_playback_ops = {
766         .open =                 snd_card_harmony_playback_open,
767         .close =                snd_card_harmony_playback_close,
768         .ioctl =                snd_card_harmony_playback_ioctl,
769         .hw_params =    snd_card_harmony_hw_params,
770         .hw_free =              snd_card_harmony_hw_free,
771         .prepare =              snd_card_harmony_playback_prepare,
772         .trigger =              snd_card_harmony_playback_trigger,
773         .pointer =              snd_card_harmony_playback_pointer,
774 };
775
776 static snd_pcm_ops_t snd_card_harmony_capture_ops = {
777         .open =                 snd_card_harmony_capture_open,
778         .close =                snd_card_harmony_capture_close,
779         .ioctl =                snd_card_harmony_capture_ioctl,
780         .hw_params =    snd_card_harmony_hw_params,
781         .hw_free =              snd_card_harmony_hw_free,
782         .prepare =              snd_card_harmony_capture_prepare,
783         .trigger =              snd_card_harmony_capture_trigger,
784         .pointer =              snd_card_harmony_capture_pointer,
785 };
786
787 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony, int device)
788 {
789         snd_pcm_t *pcm;
790         int err;
791
792         /* Request that IRQ */
793         if (request_irq(harmony->irq, snd_card_harmony_interrupt, 0 ,"harmony", harmony)) {
794                 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony->irq);
795                 return -EFAULT;
796         }
797         
798         snd_harmony_disable_interrupts(harmony);
799         
800         if ((err = snd_pcm_new(harmony->card, "Harmony", device, 1, 1, &pcm)) < 0)
801                 return err;
802         
803         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_harmony_playback_ops);
804         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_harmony_capture_ops); 
805         
806         pcm->private_data = harmony;
807         pcm->info_flags = 0;
808         strcpy(pcm->name, "Harmony");
809         harmony->pcm = pcm;
810         
811         /* initialize graveyard buffer */
812         harmony->dma_dev.type = SNDRV_DMA_TYPE_DEV;
813         harmony->dma_dev.dev = &harmony->pa_dev->dev;
814         err = snd_dma_alloc_pages(&harmony->dma_dev, HARMONY_BUF_SIZE*GRAVEYARD_BUFS,
815                                   &harmony->graveyard_dma);
816         if (err < 0)
817                 return err;
818         harmony->graveyard_count = 0;
819         
820         /* initialize silence buffers */
821         err = snd_dma_alloc_pages(&harmony->dma_dev, HARMONY_BUF_SIZE*SILENCE_BUFS,
822                                   &harmony->silence_dma);
823         if (err < 0)
824                 return err;
825         harmony->silence_count = 0;
826
827         harmony->ply_stopped = harmony->cap_stopped = 1;
828         
829         harmony->playback_substream = NULL;
830         harmony->capture_substream = NULL;
831         harmony->graveyard_count = 0;
832         
833         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
834                                               &harmony->pa_dev->dev,
835                                               MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
836
837         return 0;
838 }
839
840 /*
841  * mixer routines
842  */
843
844 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
845 {
846         DPRINTK(KERN_INFO PFX "Setting new gain %x at %lx\n", harmony->current_gain, harmony->hpa+REG_GAINCTL);
847         /* Wait until we're out of control mode */
848         snd_harmony_wait_cntl(harmony);
849         
850         gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
851 }
852
853 #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
854 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
855   .info = snd_harmony_mixercontrol_info, \
856   .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
857   .private_value = ((left_shift) | ((right_shift) << 8) | ((mask) << 16) | ((invert) << 24)) }
858
859 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
860 {
861         int mask = (kcontrol->private_value >> 16) & 0xff;
862         int left_shift = (kcontrol->private_value) & 0xff;
863         int right_shift = (kcontrol->private_value >> 8) & 0xff;
864         
865         uinfo->type = (mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER);
866         uinfo->count = (left_shift == right_shift) ? 1 : 2;
867         uinfo->value.integer.min = 0;
868         uinfo->value.integer.max = mask;
869         return 0;
870 }
871  
872 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
873 {
874         snd_card_harmony_t *harmony = _snd_kcontrol_chip(kcontrol);
875         int shift_left = (kcontrol->private_value) & 0xff;
876         int shift_right = (kcontrol->private_value >> 8) & 0xff;
877         int mask = (kcontrol->private_value >> 16) & 0xff;
878         int invert = (kcontrol->private_value >> 24) & 0xff;
879         unsigned long flags;
880         int left, right;
881         
882         spin_lock_irqsave(&harmony->mixer_lock, flags);
883         left = (harmony->current_gain >> shift_left) & mask;
884         right = (harmony->current_gain >> shift_right) & mask;
885
886         if (invert) {
887                 left = mask - left;
888                 right = mask - right;
889         }
890         ucontrol->value.integer.value[0] = left;
891         ucontrol->value.integer.value[1] = right;
892         spin_unlock_irqrestore(&harmony->mixer_lock, flags);
893
894         return 0;
895 }  
896
897 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
898 {
899         snd_card_harmony_t *harmony = _snd_kcontrol_chip(kcontrol);
900         int shift_left = (kcontrol->private_value) & 0xff;
901         int shift_right = (kcontrol->private_value >> 8) & 0xff;
902         int mask = (kcontrol->private_value >> 16) & 0xff;
903         int invert = (kcontrol->private_value >> 24) & 0xff;
904         unsigned long flags;
905         int left, right;
906         int old_gain = harmony->current_gain;
907         
908         left = ucontrol->value.integer.value[0] & mask;
909         right = ucontrol->value.integer.value[1] & mask;
910         if (invert) {
911                 left = mask - left;
912                 right = mask - right;
913         }
914         
915         spin_lock_irqsave(&harmony->mixer_lock, flags);
916         harmony->current_gain = harmony->current_gain & ~( (mask << shift_right) | (mask << shift_left));
917         harmony->current_gain = harmony->current_gain | ((left << shift_left) | (right << shift_right) );
918         snd_harmony_set_new_gain(harmony);
919         spin_unlock_irqrestore(&harmony->mixer_lock, flags);
920         
921         return (old_gain - harmony->current_gain);
922 }
923
924 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
925
926 static snd_kcontrol_new_t snd_harmony_controls[] = {
927 HARMONY_VOLUME("PCM Capture Volume", 12, 16, 0x0f, 0),
928 HARMONY_VOLUME("Master Volume", 20, 20, 0x0f, 1),
929 HARMONY_VOLUME("PCM Playback Volume", 6, 0, 0x3f, 1),
930 };
931
932 static void __init snd_harmony_reset_codec(snd_card_harmony_t *harmony)
933 {
934         snd_harmony_wait_cntl(harmony);
935         gsc_writel(1, harmony->hpa+REG_RESET);
936         mdelay(50);             /* wait 50 ms */
937         gsc_writel(0, harmony->hpa+REG_RESET);
938 }
939
940 /*
941  * Mute all the output and reset Harmony.
942  */
943
944 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
945 {
946         harmony->current_gain = HARMONY_GAIN_TOTAL_SILENCE;
947         snd_harmony_set_new_gain(harmony);
948         snd_harmony_reset_codec(harmony);
949         harmony->current_gain = HARMONY_GAIN_DEFAULT;
950         snd_harmony_set_new_gain(harmony);
951 }
952
953
954 static int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
955 {
956         snd_card_t *card = harmony->card;
957         int idx, err;
958
959         snd_assert(harmony != NULL, return -EINVAL);
960         strcpy(card->mixername, "Harmony Gain control interface");
961
962         for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
963                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
964                         return err;
965         }
966         
967         snd_harmony_mixer_reset(harmony);
968
969         return 0;
970 }
971
972 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
973 {
974         u32     cntl;
975         
976         harmony->card = card;
977         
978         harmony->pa_dev = pa_dev;
979
980         /* Set the HPA of harmony */
981         harmony->hpa = pa_dev->hpa;
982         
983         harmony->irq = pa_dev->irq;
984         if (!harmony->irq) {
985                 printk(KERN_ERR PFX "no irq found\n");
986                 return -ENODEV;
987         }
988
989         /* Grab the ID and revision from the device */
990         harmony->id = (gsc_readl(harmony->hpa+REG_ID)&0x00ff0000) >> 16;
991         if ((harmony->id | 1) != 0x15) {
992                 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", harmony->id);
993                 return -EBUSY;
994         }
995         cntl = gsc_readl(harmony->hpa+REG_CNTL);
996         harmony->rev = (cntl>>20) & 0xff;
997
998         printk(KERN_INFO "Lasi Harmony Audio driver h/w id %i, rev. %i at 0x%lx, IRQ %i\n",     harmony->id, harmony->rev, pa_dev->hpa, harmony->irq);
999         
1000         /* Make sure the control bit isn't set, although I don't think it 
1001            ever is. */
1002         if (cntl & HARMONY_CNTL_C) {
1003                 printk(KERN_WARNING PFX "CNTL busy\n");
1004                 harmony->hpa = 0;
1005                 return -EBUSY;
1006         }
1007         
1008         return 0;
1009 }
1010         
1011 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1012 {
1013         static int dev;
1014         snd_card_harmony_t *chip;
1015         snd_card_t *card;
1016         int err;
1017         
1018         if (dev >= SNDRV_CARDS)
1019                 return -ENODEV;
1020         if (!enable[dev]) {
1021                 dev++;
1022                 return -ENOENT;
1023         }
1024         
1025         snd_harmony_cards[dev] = snd_card_new(index[dev], id[dev], THIS_MODULE,
1026                             sizeof(snd_card_harmony_t));
1027         card = snd_harmony_cards[dev];
1028                                 
1029         if (card == NULL)
1030                 return -ENOMEM;
1031         chip = (struct snd_card_harmony *)card->private_data;
1032         spin_lock_init(&chip->control_lock);
1033         spin_lock_init(&chip->mixer_lock);
1034         
1035         if ((err = snd_card_harmony_create(card, pa_dev, chip)) < 0) {
1036                 printk(KERN_ERR PFX "Creation failed\n");
1037                 snd_card_free(card);
1038                 return err;
1039         }
1040         if ((err = snd_card_harmony_pcm_init(chip, dev)) < 0) {
1041                 printk(KERN_ERR PFX "PCM Init failed\n");
1042                 snd_card_free(card);
1043                 return err;
1044         }
1045         if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1046                 printk(KERN_ERR PFX "Mixer init failed\n");
1047                 snd_card_free(card);
1048                 return err;
1049         }
1050         
1051         snd_harmony_proc_init(chip);
1052         
1053         strcpy(card->driver, "Harmony");
1054         strcpy(card->shortname, "ALSA driver for LASI Harmony");
1055         sprintf(card->longname, "%s at h/w, id %i, rev. %i hpa 0x%lx, IRQ %i\n",card->shortname, chip->id, chip->rev, pa_dev->hpa, chip->irq);
1056
1057         if ((err = snd_card_register(card)) < 0) {
1058                 snd_card_free(card);
1059                 return err;
1060         }
1061
1062         printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
1063         dev++;
1064         return 0;
1065 }
1066
1067 static struct parisc_device_id snd_card_harmony_devicetbl[] = {
1068  { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, /* Bushmaster/Flounder */
1069  { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1070  { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1071  { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1072  { 0, }
1073 };
1074
1075 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
1076
1077 /*
1078  * bloc device parisc. c'est une structure qui definit un device
1079  * que l'on trouve sur parisc. 
1080  * On y trouve les differents numeros HVERSION correspondant au device
1081  * en question (ce qui permet a l'inventory de l'identifier) et la fonction
1082  * d'initialisation du chose 
1083  */
1084
1085 static struct parisc_driver snd_card_harmony_driver = {
1086         .name           = "Lasi ALSA Harmony",
1087         .id_table       = snd_card_harmony_devicetbl,
1088         .probe          = snd_card_harmony_probe,
1089 };
1090
1091 static int __init alsa_card_harmony_init(void)
1092 {
1093         int err;
1094         
1095         if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1096                 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1097                 return err;
1098         }
1099
1100         return 0;
1101 }
1102
1103 static void __exit alsa_card_harmony_exit(void)
1104 {
1105         int idx;
1106         snd_card_harmony_t *harmony;
1107         
1108         for (idx = 0; idx < SNDRV_CARDS; idx++)
1109         {
1110                 if (snd_harmony_cards[idx] != NULL)
1111                 {       
1112                         DPRINTK(KERN_INFO PFX "Freeing card %d\n", idx);
1113                         harmony = snd_harmony_cards[idx]->private_data;
1114                         free_irq(harmony->irq, snd_card_harmony_interrupt);
1115                         printk(KERN_INFO PFX "Card unloaded %d, irq=%d\n", idx, harmony->irq);
1116                         snd_card_free(snd_harmony_cards[idx]);
1117                 }
1118         }       
1119 }
1120
1121 module_init(alsa_card_harmony_init)
1122 module_exit(alsa_card_harmony_exit)