MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / sound / parisc / harmony.c
blob0c02a5f458ec346cf4bb51a002a970aa5d701420
1 /*
2 * Harmony chipset driver
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.
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 Vivace are supported by this driver.
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>
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.
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.
35 * Playback and recording use 4kb pages (dma or not, depending on the machine).
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.
46 * Harmony has no way to control 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.
51 * Bitrate, number of channels and data format are controlled with
52 * the CNTL register.
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.
59 * Buffers used by this driver are all DMA consistent.
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>
80 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>");
81 MODULE_DESCRIPTION("ALSA Harmony sound driver");
82 MODULE_LICENSE("GPL");
83 MODULE_SUPPORTED_DEVICE("{{ALSA,Harmony soundcard}}");
85 #undef DEBUG
86 #ifdef DEBUG
87 # define DPRINTK printk
88 #else
89 # define DPRINTK(x,...)
90 #endif
92 #define PFX "harmony: "
94 #define MAX_PCM_DEVICES 1
95 #define MAX_PCM_SUBSTREAMS 4
96 #define MAX_MIDI_DEVICES 0
98 #define HARMONY_BUF_SIZE 4096
99 #define MAX_BUFS 10
100 #define MAX_BUFFER_SIZE (MAX_BUFS * HARMONY_BUF_SIZE)
102 /* number of silence & graveyard buffers */
103 #define GRAVEYARD_BUFS 3
104 #define SILENCE_BUFS 3
106 #define HARMONY_CNTL_C 0x80000000
108 #define HARMONY_DSTATUS_PN 0x00000200
109 #define HARMONY_DSTATUS_RN 0x00000002
110 #define HARMONY_DSTATUS_IE 0x80000000
112 #define HARMONY_DF_16BIT_LINEAR 0x00000000
113 #define HARMONY_DF_8BIT_ULAW 0x00000001
114 #define HARMONY_DF_8BIT_ALAW 0x00000002
116 #define HARMONY_SS_MONO 0x00000000
117 #define HARMONY_SS_STEREO 0x00000001
120 * Channels Mask in mixer register
121 * try some "reasonable" default gain values
124 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
126 /* the following should be enough (mixer is
127 * very sensible on harmony)
129 #define HARMONY_GAIN_DEFAULT 0x0F2FF082
132 /* useless since only one card is supported ATM */
133 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
134 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
135 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
136 static int boot_devs;
138 module_param_array(index, int, boot_devs, 0444);
139 MODULE_PARM_DESC(index, "Index value for Sun CS4231 soundcard.");
140 module_param_array(id, charp, boot_devs, 0444);
141 MODULE_PARM_DESC(id, "ID string for Sun CS4231 soundcard.");
142 module_param_array(enable, bool, boot_devs, 0444);
143 MODULE_PARM_DESC(enable, "Enable Sun CS4231 soundcard.");
145 /* Register offset (from base hpa) */
146 #define REG_ID 0x00
147 #define REG_RESET 0x04
148 #define REG_CNTL 0x08
149 #define REG_GAINCTL 0x0C
150 #define REG_PNXTADD 0x10
151 #define REG_PCURADD 0x14
152 #define REG_RNXTADD 0x18
153 #define REG_RCURADD 0x1C
154 #define REG_DSTATUS 0x20
155 #define REG_OV 0x24
156 #define REG_PIO 0x28
157 #define REG_DIAG 0x3C
160 * main harmony structure
163 typedef struct snd_card_harmony {
165 /* spinlocks (To be done) */
166 spinlock_t mixer_lock;
167 spinlock_t control_lock;
169 /* parameters */
170 int irq;
171 unsigned long hpa;
172 int id;
173 int rev;
175 u32 current_gain;
176 int data_format; /* HARMONY_DF_xx_BIT_xxx */
177 int sample_rate; /* HARMONY_SR_xx_KHZ */
178 int stereo_select; /* HARMONY_SS_MONO or HARMONY_SS_STEREO */
179 int format_initialized;
181 unsigned long ply_buffer;
182 int ply_buf;
183 int ply_count;
184 int ply_size;
185 int ply_stopped;
186 int ply_total;
188 unsigned long cap_buffer;
189 int cap_buf;
190 int cap_count;
191 int cap_size;
192 int cap_stopped;
193 int cap_total;
195 struct parisc_device *pa_dev;
197 struct snd_dma_device dma_dev;
199 /* the graveyard buffer is used as recording buffer when playback,
200 * because harmony always want a buffer to put recorded data */
201 struct snd_dma_buffer graveyard_dma;
202 int graveyard_count;
204 /* same thing for silence buffer */
205 struct snd_dma_buffer silence_dma;
206 int silence_count;
208 /* alsa stuff */
209 snd_card_t *card;
210 snd_pcm_t *pcm;
211 snd_pcm_substream_t *playback_substream;
212 snd_pcm_substream_t *capture_substream;
213 snd_info_entry_t *proc_entry;
214 } snd_card_harmony_t;
216 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
218 /* wait to be out of control mode */
219 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
221 int timeout = 5000;
223 while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
225 /* Wait */ ;
227 if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
232 * sample rate routines
234 static unsigned int snd_card_harmony_rates[] = {
235 5125, 6615, 8000, 9600,
236 11025, 16000, 18900, 22050,
237 27428, 32000, 33075, 37800,
238 44100, 48000
241 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
242 .count = ARRAY_SIZE(snd_card_harmony_rates),
243 .list = snd_card_harmony_rates,
244 .mask = 0,
247 #define HARMONY_SR_8KHZ 0x08
248 #define HARMONY_SR_16KHZ 0x09
249 #define HARMONY_SR_27KHZ 0x0A
250 #define HARMONY_SR_32KHZ 0x0B
251 #define HARMONY_SR_48KHZ 0x0E
252 #define HARMONY_SR_9KHZ 0x0F
253 #define HARMONY_SR_5KHZ 0x10
254 #define HARMONY_SR_11KHZ 0x11
255 #define HARMONY_SR_18KHZ 0x12
256 #define HARMONY_SR_22KHZ 0x13
257 #define HARMONY_SR_37KHZ 0x14
258 #define HARMONY_SR_44KHZ 0x15
259 #define HARMONY_SR_33KHZ 0x16
260 #define HARMONY_SR_6KHZ 0x17
262 /* bits corresponding to the entries of snd_card_harmony_rates */
263 static unsigned int rate_bits[14] = {
264 HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
265 HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
266 HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
267 HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
268 HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
271 /* snd_card_harmony_rate_bits
272 * @rate: index of current data rate in list
273 * returns: harmony hex code for registers
275 static unsigned int snd_card_harmony_rate_bits(int rate)
277 unsigned int idx;
279 for (idx = 0; idx <= ARRAY_SIZE(snd_card_harmony_rates); idx++)
280 if (snd_card_harmony_rates[idx] == rate)
281 return rate_bits[idx];
282 return HARMONY_SR_44KHZ; /* fallback */
286 * update controls (data format, sample rate, number of channels)
287 * according to value supplied in data structure
289 void snd_harmony_update_control(snd_card_harmony_t *harmony)
291 u32 default_cntl;
293 /* Set CNTL */
294 default_cntl = (HARMONY_CNTL_C | /* The C bit */
295 (harmony->data_format << 6) | /* Set the data format */
296 (harmony->stereo_select << 5) | /* Stereo select */
297 (harmony->sample_rate)); /* Set sample rate */
299 /* initialize CNTL */
300 snd_harmony_wait_cntl(harmony);
302 gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
307 * interruption controls routines
310 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip)
312 snd_harmony_wait_cntl(chip);
313 gsc_writel(0, chip->hpa+REG_DSTATUS);
316 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip)
318 snd_harmony_wait_cntl(chip);
319 gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS);
323 * interruption routine:
324 * The interrupt routine must provide adresse of next physical pages
325 * used by harmony
327 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
329 snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
330 u32 dstatus = 0;
331 unsigned long hpa = harmony->hpa;
333 /* Turn off interrupts */
334 snd_harmony_disable_interrupts(harmony);
336 /* wait for control to free */
337 snd_harmony_wait_cntl(harmony);
339 /* Read dstatus and pcuradd (the current address) */
340 dstatus = gsc_readl(hpa+REG_DSTATUS);
342 /* Check if this is a request to get the next play buffer */
343 if (dstatus & HARMONY_DSTATUS_PN) {
344 if (harmony->playback_substream) {
345 harmony->ply_buf += harmony->ply_count;
346 harmony->ply_buf %= harmony->ply_size;
348 gsc_writel(harmony->ply_buffer + harmony->ply_buf,
349 hpa+REG_PNXTADD);
351 snd_pcm_period_elapsed(harmony->playback_substream);
352 harmony->ply_total++;
353 } else {
354 gsc_writel(harmony->silence_dma.addr +
355 (HARMONY_BUF_SIZE*harmony->silence_count),
356 hpa+REG_PNXTADD);
357 harmony->silence_count++;
358 harmony->silence_count %= SILENCE_BUFS;
362 /* Check if we're being asked to fill in a recording buffer */
363 if (dstatus & HARMONY_DSTATUS_RN) {
364 if (harmony->capture_substream) {
365 harmony->cap_buf += harmony->cap_count;
366 harmony->cap_buf %= harmony->cap_size;
368 gsc_writel(harmony->cap_buffer + harmony->cap_buf,
369 hpa+REG_RNXTADD);
371 snd_pcm_period_elapsed(harmony->capture_substream);
372 harmony->cap_total++;
373 } else {
374 /* graveyard buffer */
375 gsc_writel(harmony->graveyard_dma.addr +
376 (HARMONY_BUF_SIZE*harmony->graveyard_count),
377 hpa+REG_RNXTADD);
378 harmony->graveyard_count++;
379 harmony->graveyard_count %= GRAVEYARD_BUFS;
382 snd_harmony_enable_interrupts(harmony);
384 return IRQ_HANDLED;
388 * proc entry
389 * this proc file will give some debugging info
392 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
394 snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
396 snd_iprintf(buffer, "LASI Harmony driver\nLaurent Canet <canetl@esiee.fr>\n\n");
397 snd_iprintf(buffer, "IRQ %d, hpa %lx, id %d rev %d\n",
398 harmony->irq, harmony->hpa,
399 harmony->id, harmony->rev);
400 snd_iprintf(buffer, "Current gain %lx\n", (unsigned long) harmony->current_gain);
401 snd_iprintf(buffer, "\tsample rate=%d\n", harmony->sample_rate);
402 snd_iprintf(buffer, "\tstereo select=%d\n", harmony->stereo_select);
403 snd_iprintf(buffer, "\tbitperchan=%d\n\n", harmony->data_format);
405 snd_iprintf(buffer, "Play status:\n");
406 snd_iprintf(buffer, "\tstopped %d\n", harmony->ply_stopped);
407 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->ply_buffer, harmony->ply_count);
408 snd_iprintf(buffer, "\tbuf %d size %d\n\n", harmony->ply_buf, harmony->ply_size);
410 snd_iprintf(buffer, "Capture status:\n");
411 snd_iprintf(buffer, "\tstopped %d\n", harmony->cap_stopped);
412 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->cap_buffer, harmony->cap_count);
413 snd_iprintf(buffer, "\tbuf %d, size %d\n\n", harmony->cap_buf, harmony->cap_size);
415 snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
417 snd_iprintf(buffer, "Register:\n");
418 snd_iprintf(buffer, "\tgainctl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_GAINCTL));
419 snd_iprintf(buffer, "\tcntl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_CNTL));
420 snd_iprintf(buffer, "\tid: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_ID));
421 snd_iprintf(buffer, "\tpcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PCURADD));
422 snd_iprintf(buffer, "\trcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RCURADD));
423 snd_iprintf(buffer, "\tpnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PNXTADD));
424 snd_iprintf(buffer, "\trnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RNXTADD));
425 snd_iprintf(buffer, "\tdstatus: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_DSTATUS));
426 snd_iprintf(buffer, "\tov: %lx\n\n", (unsigned long) gsc_readl(harmony->hpa+REG_OV));
430 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
432 snd_info_entry_t *entry;
434 if (! snd_card_proc_new(harmony->card, "harmony", &entry))
435 snd_info_set_text_ops(entry, harmony, 2048, snd_harmony_proc_read);
439 * PCM Stuff
442 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
443 unsigned int cmd,
444 void *arg)
446 return snd_pcm_lib_ioctl(substream, cmd, arg);
449 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
450 unsigned int cmd,
451 void *arg)
453 return snd_pcm_lib_ioctl(substream, cmd, arg);
456 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
457 int cmd)
459 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
461 switch (cmd) {
462 case SNDRV_PCM_TRIGGER_STOP:
463 if (harmony->ply_stopped)
464 return -EBUSY;
465 harmony->ply_stopped = 1;
466 snd_harmony_disable_interrupts(harmony);
467 break;
468 case SNDRV_PCM_TRIGGER_START:
469 if (!harmony->ply_stopped)
470 return -EBUSY;
471 harmony->ply_stopped = 0;
472 /* write the location of the first buffer to play */
473 gsc_writel(harmony->ply_buffer, harmony->hpa+REG_PNXTADD);
474 snd_harmony_enable_interrupts(harmony);
475 break;
476 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
477 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
478 case SNDRV_PCM_TRIGGER_SUSPEND:
479 DPRINTK(KERN_INFO PFX "received unimplemented trigger: %d\n", cmd);
480 default:
481 return -EINVAL;
483 return 0;
486 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
487 int cmd)
489 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
491 switch (cmd) {
492 case SNDRV_PCM_TRIGGER_STOP:
493 if (harmony->cap_stopped)
494 return -EBUSY;
495 harmony->cap_stopped = 1;
496 snd_harmony_disable_interrupts(harmony);
497 break;
498 case SNDRV_PCM_TRIGGER_START:
499 if (!harmony->cap_stopped)
500 return -EBUSY;
501 harmony->cap_stopped = 0;
502 snd_harmony_enable_interrupts(harmony);
503 break;
504 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
505 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
506 case SNDRV_PCM_TRIGGER_SUSPEND:
507 DPRINTK(KERN_INFO PFX "Received unimplemented trigger: %d\n", cmd);
508 default:
509 return -EINVAL;
511 return 0;
514 /* set data format */
515 static int snd_harmony_set_data_format(snd_card_harmony_t *harmony, int pcm_format)
517 int old_format = harmony->data_format;
518 int new_format = old_format;
519 switch (pcm_format) {
520 case SNDRV_PCM_FORMAT_S16_BE:
521 new_format = HARMONY_DF_16BIT_LINEAR;
522 break;
523 case SNDRV_PCM_FORMAT_A_LAW:
524 new_format = HARMONY_DF_8BIT_ALAW;
525 break;
526 case SNDRV_PCM_FORMAT_MU_LAW:
527 new_format = HARMONY_DF_8BIT_ULAW;
528 break;
530 /* re-initialize silence buffer if needed */
531 if (old_format != new_format)
532 snd_pcm_format_set_silence(pcm_format, harmony->silence_dma.area,
533 (HARMONY_BUF_SIZE * SILENCE_BUFS * 8) / snd_pcm_format_width(pcm_format));
535 return new_format;
538 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
540 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
541 snd_pcm_runtime_t *runtime = substream->runtime;
543 harmony->ply_size = snd_pcm_lib_buffer_bytes(substream);
544 harmony->ply_count = snd_pcm_lib_period_bytes(substream);
545 harmony->ply_buf = 0;
546 harmony->ply_stopped = 1;
548 /* initialize given sample rate */
549 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
551 /* data format */
552 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
554 /* number of channels */
555 if (runtime->channels == 2)
556 harmony->stereo_select = HARMONY_SS_STEREO;
557 else
558 harmony->stereo_select = HARMONY_SS_MONO;
560 DPRINTK(KERN_INFO PFX "Playback_prepare, sr=%d(%x), df=%x, ss=%x hpa=%lx\n", runtime->rate,
561 harmony->sample_rate, harmony->data_format, harmony->stereo_select, harmony->hpa);
562 snd_harmony_update_control(harmony);
563 harmony->format_initialized = 1;
564 harmony->ply_buffer = runtime->dma_addr;
566 return 0;
569 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
571 snd_pcm_runtime_t *runtime = substream->runtime;
572 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
574 harmony->cap_size = snd_pcm_lib_buffer_bytes(substream);
575 harmony->cap_count = snd_pcm_lib_period_bytes(substream);
576 harmony->cap_count = 0;
577 harmony->cap_stopped = 1;
579 /* initialize given sample rate */
580 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
582 /* data format */
583 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
585 /* number of channels */
586 if (runtime->channels == 1)
587 harmony->stereo_select = HARMONY_SS_MONO;
588 else if (runtime->channels == 2)
589 harmony->stereo_select = HARMONY_SS_STEREO;
591 snd_harmony_update_control(harmony);
592 harmony->format_initialized = 1;
594 harmony->cap_buffer = runtime->dma_addr;
596 return 0;
599 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
601 snd_pcm_runtime_t *runtime = substream->runtime;
602 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
603 unsigned long rcuradd;
604 int recorded;
606 if (harmony->cap_stopped) return 0;
607 if (harmony->capture_substream == NULL) return 0;
609 rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
610 recorded = (rcuradd - harmony->cap_buffer);
611 recorded %= harmony->cap_size;
613 return bytes_to_frames(runtime, recorded);
619 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
621 snd_pcm_runtime_t *runtime = substream->runtime;
622 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
623 int played;
624 long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
626 if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
627 if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
629 played = (pcuradd - harmony->ply_buffer);
631 printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);
633 if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
635 return bytes_to_frames(runtime, played);
638 static snd_pcm_hardware_t snd_card_harmony_playback =
640 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
641 SNDRV_PCM_INFO_JOINT_DUPLEX |
642 SNDRV_PCM_INFO_MMAP_VALID |
643 SNDRV_PCM_INFO_BLOCK_TRANSFER),
644 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
645 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
646 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
647 .rate_min = 5500,
648 .rate_max = 48000,
649 .channels_min = 1,
650 .channels_max = 2,
651 .buffer_bytes_max = MAX_BUFFER_SIZE,
652 .period_bytes_min = HARMONY_BUF_SIZE,
653 .period_bytes_max = HARMONY_BUF_SIZE,
654 .periods_min = 1,
655 .periods_max = MAX_BUFS,
656 .fifo_size = 0,
659 static snd_pcm_hardware_t snd_card_harmony_capture =
661 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
662 SNDRV_PCM_INFO_JOINT_DUPLEX |
663 SNDRV_PCM_INFO_MMAP_VALID |
664 SNDRV_PCM_INFO_BLOCK_TRANSFER),
665 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
666 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
667 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
668 .rate_min = 5500,
669 .rate_max = 48000,
670 .channels_min = 1,
671 .channels_max = 2,
672 .buffer_bytes_max = MAX_BUFFER_SIZE,
673 .period_bytes_min = HARMONY_BUF_SIZE,
674 .period_bytes_max = HARMONY_BUF_SIZE,
675 .periods_min = 1,
676 .periods_max = MAX_BUFS,
677 .fifo_size = 0,
680 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
682 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
683 snd_pcm_runtime_t *runtime = substream->runtime;
684 int err;
686 harmony->playback_substream = substream;
687 runtime->hw = snd_card_harmony_playback;
688 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
690 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
691 return err;
693 return 0;
696 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
698 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
699 snd_pcm_runtime_t *runtime = substream->runtime;
700 int err;
702 harmony->capture_substream = substream;
703 runtime->hw = snd_card_harmony_capture;
704 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
705 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
706 return err;
707 return 0;
711 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
713 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
715 harmony->playback_substream = NULL;
716 harmony->ply_size = 0;
717 harmony->ply_buf = 0;
718 harmony->ply_buffer = 0;
719 harmony->ply_count = 0;
720 harmony->ply_stopped = 1;
721 harmony->format_initialized = 0;
723 return 0;
726 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
728 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
730 harmony->capture_substream = NULL;
731 harmony->cap_size = 0;
732 harmony->cap_buf = 0;
733 harmony->cap_buffer = 0;
734 harmony->cap_count = 0;
735 harmony->cap_stopped = 1;
736 harmony->format_initialized = 0;
738 return 0;
741 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream,
742 snd_pcm_hw_params_t * hw_params)
744 int err;
746 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
747 if (err > 0 && substream->dma_device.type == SNDRV_DMA_TYPE_CONTINUOUS)
748 substream->runtime->dma_addr = __pa(substream->runtime->dma_area);
749 DPRINTK(KERN_INFO PFX "HW Params returned %d, dma_addr %lx\n", err,
750 (unsigned long)substream->runtime->dma_addr);
751 return err;
754 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream)
756 snd_pcm_lib_free_pages(substream);
757 return 0;
760 static snd_pcm_ops_t snd_card_harmony_playback_ops = {
761 .open = snd_card_harmony_playback_open,
762 .close = snd_card_harmony_playback_close,
763 .ioctl = snd_card_harmony_playback_ioctl,
764 .hw_params = snd_card_harmony_hw_params,
765 .hw_free = snd_card_harmony_hw_free,
766 .prepare = snd_card_harmony_playback_prepare,
767 .trigger = snd_card_harmony_playback_trigger,
768 .pointer = snd_card_harmony_playback_pointer,
771 static snd_pcm_ops_t snd_card_harmony_capture_ops = {
772 .open = snd_card_harmony_capture_open,
773 .close = snd_card_harmony_capture_close,
774 .ioctl = snd_card_harmony_capture_ioctl,
775 .hw_params = snd_card_harmony_hw_params,
776 .hw_free = snd_card_harmony_hw_free,
777 .prepare = snd_card_harmony_capture_prepare,
778 .trigger = snd_card_harmony_capture_trigger,
779 .pointer = snd_card_harmony_capture_pointer,
782 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony)
784 snd_pcm_t *pcm;
785 int err;
787 /* Request that IRQ */
788 if (request_irq(harmony->irq, snd_card_harmony_interrupt, 0 ,"harmony", harmony)) {
789 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony->irq);
790 return -EFAULT;
793 snd_harmony_disable_interrupts(harmony);
795 if ((err = snd_pcm_new(harmony->card, "Harmony", 0, 1, 1, &pcm)) < 0)
796 return err;
798 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_harmony_playback_ops);
799 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_harmony_capture_ops);
801 pcm->private_data = harmony;
802 pcm->info_flags = 0;
803 strcpy(pcm->name, "Harmony");
804 harmony->pcm = pcm;
806 /* initialize graveyard buffer */
807 harmony->dma_dev.type = SNDRV_DMA_TYPE_DEV;
808 harmony->dma_dev.dev = &harmony->pa_dev->dev;
809 err = snd_dma_alloc_pages(harmony->dma_dev.type,
810 harmony->dma_dev.dev,
811 HARMONY_BUF_SIZE*GRAVEYARD_BUFS,
812 &harmony->graveyard_dma);
813 if (err == -ENOMEM) {
814 /* use continuous buffers */
815 harmony->dma_dev.type = SNDRV_DMA_TYPE_CONTINUOUS;
816 harmony->dma_dev.dev = snd_dma_continuous_data(GFP_KERNEL);
817 err = snd_dma_alloc_pages(harmony->dma_dev.type,
818 harmony->dma_dev.dev,
819 HARMONY_BUF_SIZE*GRAVEYARD_BUFS,
820 &harmony->graveyard_dma);
822 if (err < 0) {
823 printk(KERN_ERR PFX "can't allocate graveyard buffer\n");
824 return err;
826 harmony->graveyard_count = 0;
828 /* initialize silence buffers */
829 err = snd_dma_alloc_pages(harmony->dma_dev.type,
830 harmony->dma_dev.dev,
831 HARMONY_BUF_SIZE*SILENCE_BUFS,
832 &harmony->silence_dma);
833 if (err < 0) {
834 printk(KERN_ERR PFX "can't allocate silence buffer\n");
835 return err;
837 harmony->silence_count = 0;
839 if (harmony->dma_dev.type == SNDRV_DMA_TYPE_CONTINUOUS) {
840 harmony->graveyard_dma.addr = __pa(harmony->graveyard_dma.area);
841 harmony->silence_dma.addr = __pa(harmony->silence_dma.area);
844 harmony->ply_stopped = harmony->cap_stopped = 1;
846 harmony->playback_substream = NULL;
847 harmony->capture_substream = NULL;
848 harmony->graveyard_count = 0;
850 err = snd_pcm_lib_preallocate_pages_for_all(pcm, harmony->dma_dev.type,
851 harmony->dma_dev.dev,
852 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
853 if (err < 0) {
854 printk(KERN_ERR PFX "buffer allocation error %d\n", err);
855 // return err;
858 return 0;
862 * mixer routines
865 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
867 DPRINTK(KERN_INFO PFX "Setting new gain %x at %lx\n", harmony->current_gain, harmony->hpa+REG_GAINCTL);
868 /* Wait until we're out of control mode */
869 snd_harmony_wait_cntl(harmony);
871 gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
874 #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
875 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
876 .info = snd_harmony_mixercontrol_info, \
877 .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
878 .private_value = ((left_shift) | ((right_shift) << 8) | ((mask) << 16) | ((invert) << 24)) }
880 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
882 int mask = (kcontrol->private_value >> 16) & 0xff;
883 int left_shift = (kcontrol->private_value) & 0xff;
884 int right_shift = (kcontrol->private_value >> 8) & 0xff;
886 uinfo->type = (mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER);
887 uinfo->count = (left_shift == right_shift) ? 1 : 2;
888 uinfo->value.integer.min = 0;
889 uinfo->value.integer.max = mask;
890 return 0;
893 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
895 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
896 int shift_left = (kcontrol->private_value) & 0xff;
897 int shift_right = (kcontrol->private_value >> 8) & 0xff;
898 int mask = (kcontrol->private_value >> 16) & 0xff;
899 int invert = (kcontrol->private_value >> 24) & 0xff;
900 unsigned long flags;
901 int left, right;
903 spin_lock_irqsave(&harmony->mixer_lock, flags);
904 left = (harmony->current_gain >> shift_left) & mask;
905 right = (harmony->current_gain >> shift_right) & mask;
907 if (invert) {
908 left = mask - left;
909 right = mask - right;
911 ucontrol->value.integer.value[0] = left;
912 ucontrol->value.integer.value[1] = right;
913 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
915 return 0;
918 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
920 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
921 int shift_left = (kcontrol->private_value) & 0xff;
922 int shift_right = (kcontrol->private_value >> 8) & 0xff;
923 int mask = (kcontrol->private_value >> 16) & 0xff;
924 int invert = (kcontrol->private_value >> 24) & 0xff;
925 unsigned long flags;
926 int left, right;
927 int old_gain = harmony->current_gain;
929 left = ucontrol->value.integer.value[0] & mask;
930 right = ucontrol->value.integer.value[1] & mask;
931 if (invert) {
932 left = mask - left;
933 right = mask - right;
936 spin_lock_irqsave(&harmony->mixer_lock, flags);
937 harmony->current_gain = harmony->current_gain & ~( (mask << shift_right) | (mask << shift_left));
938 harmony->current_gain = harmony->current_gain | ((left << shift_left) | (right << shift_right) );
939 snd_harmony_set_new_gain(harmony);
940 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
942 return (old_gain - harmony->current_gain);
945 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
947 static snd_kcontrol_new_t snd_harmony_controls[] = {
948 HARMONY_VOLUME("PCM Capture Volume", 12, 16, 0x0f, 0),
949 HARMONY_VOLUME("Master Volume", 20, 20, 0x0f, 1),
950 HARMONY_VOLUME("PCM Playback Volume", 6, 0, 0x3f, 1),
953 static void __init snd_harmony_reset_codec(snd_card_harmony_t *harmony)
955 snd_harmony_wait_cntl(harmony);
956 gsc_writel(1, harmony->hpa+REG_RESET);
957 mdelay(50); /* wait 50 ms */
958 gsc_writel(0, harmony->hpa+REG_RESET);
962 * Mute all the output and reset Harmony.
965 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
967 harmony->current_gain = HARMONY_GAIN_TOTAL_SILENCE;
968 snd_harmony_set_new_gain(harmony);
969 snd_harmony_reset_codec(harmony);
970 harmony->current_gain = HARMONY_GAIN_DEFAULT;
971 snd_harmony_set_new_gain(harmony);
975 static int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
977 snd_card_t *card = harmony->card;
978 int idx, err;
980 snd_assert(harmony != NULL, return -EINVAL);
981 strcpy(card->mixername, "Harmony Gain control interface");
983 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
984 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
985 return err;
988 snd_harmony_mixer_reset(harmony);
990 return 0;
993 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
995 u32 cntl;
997 harmony->card = card;
999 harmony->pa_dev = pa_dev;
1001 /* Set the HPA of harmony */
1002 harmony->hpa = pa_dev->hpa;
1004 harmony->irq = pa_dev->irq;
1005 if (!harmony->irq) {
1006 printk(KERN_ERR PFX "no irq found\n");
1007 return -ENODEV;
1010 /* Grab the ID and revision from the device */
1011 harmony->id = (gsc_readl(harmony->hpa+REG_ID)&0x00ff0000) >> 16;
1012 if ((harmony->id | 1) != 0x15) {
1013 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", harmony->id);
1014 return -EBUSY;
1016 cntl = gsc_readl(harmony->hpa+REG_CNTL);
1017 harmony->rev = (cntl>>20) & 0xff;
1019 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);
1021 /* Make sure the control bit isn't set, although I don't think it
1022 ever is. */
1023 if (cntl & HARMONY_CNTL_C) {
1024 printk(KERN_WARNING PFX "CNTL busy\n");
1025 harmony->hpa = 0;
1026 return -EBUSY;
1029 return 0;
1032 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1034 static int dev;
1035 snd_card_harmony_t *chip;
1036 snd_card_t *card;
1037 int err;
1039 if (dev >= SNDRV_CARDS)
1040 return -ENODEV;
1041 if (!enable[dev]) {
1042 dev++;
1043 return -ENOENT;
1046 snd_harmony_cards[dev] = snd_card_new(index[dev], id[dev], THIS_MODULE,
1047 sizeof(snd_card_harmony_t));
1048 card = snd_harmony_cards[dev];
1050 if (card == NULL)
1051 return -ENOMEM;
1052 chip = (struct snd_card_harmony *)card->private_data;
1053 spin_lock_init(&chip->control_lock);
1054 spin_lock_init(&chip->mixer_lock);
1056 if ((err = snd_card_harmony_create(card, pa_dev, chip)) < 0) {
1057 printk(KERN_ERR PFX "Creation failed\n");
1058 snd_card_free(card);
1059 return err;
1061 if ((err = snd_card_harmony_pcm_init(chip)) < 0) {
1062 printk(KERN_ERR PFX "PCM Init failed\n");
1063 snd_card_free(card);
1064 return err;
1066 if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1067 printk(KERN_ERR PFX "Mixer init failed\n");
1068 snd_card_free(card);
1069 return err;
1072 snd_harmony_proc_init(chip);
1074 strcpy(card->driver, "Harmony");
1075 strcpy(card->shortname, "ALSA driver for LASI Harmony");
1076 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);
1078 if ((err = snd_card_register(card)) < 0) {
1079 snd_card_free(card);
1080 return err;
1083 printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
1084 dev++;
1085 return 0;
1088 static struct parisc_device_id snd_card_harmony_devicetbl[] = {
1089 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, /* Bushmaster/Flounder */
1090 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1091 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1092 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1093 { 0, }
1096 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
1099 * bloc device parisc. c'est une structure qui definit un device
1100 * que l'on trouve sur parisc.
1101 * On y trouve les differents numeros HVERSION correspondant au device
1102 * en question (ce qui permet a l'inventory de l'identifier) et la fonction
1103 * d'initialisation du chose
1106 static struct parisc_driver snd_card_harmony_driver = {
1107 .name = "Lasi ALSA Harmony",
1108 .id_table = snd_card_harmony_devicetbl,
1109 .probe = snd_card_harmony_probe,
1112 static int __init alsa_card_harmony_init(void)
1114 int err;
1116 if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1117 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1118 return err;
1121 return 0;
1124 static void __exit alsa_card_harmony_exit(void)
1126 int idx;
1127 snd_card_harmony_t *harmony;
1129 for (idx = 0; idx < SNDRV_CARDS; idx++)
1131 if (snd_harmony_cards[idx] != NULL)
1133 DPRINTK(KERN_INFO PFX "Freeing card %d\n", idx);
1134 harmony = snd_harmony_cards[idx]->private_data;
1135 free_irq(harmony->irq, snd_card_harmony_interrupt);
1136 printk(KERN_INFO PFX "Card unloaded %d, irq=%d\n", idx, harmony->irq);
1137 snd_card_free(snd_harmony_cards[idx]);
1142 module_init(alsa_card_harmony_init)
1143 module_exit(alsa_card_harmony_exit)