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 Vicace 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>
17 * - use generic DMA interface and ioremap()/iounmap()
18 * - capture is still untested (and probaby non-working)
20 * - implement non-consistent DMA pages
21 * - implement gain meter
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 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.
51 * Bitrate, number of channels and data format are controlled with
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
59 * Buffers used by this driver are all DMA consistent. Since harmony is
60 * not "real" pci device, we use a fake struct pci_dev for
61 * pci_alloc_consistent().
62 * (note that some machines -712 for ex.- don't implement DMA consistent
63 * memory, so we will need to use kmalloc instead)
66 #include <linux/delay.h>
67 #include <sound/driver.h>
68 #include <linux/init.h>
69 #include <linux/interrupt.h>
70 #include <linux/slab.h>
71 #include <linux/time.h>
72 #include <linux/wait.h>
73 #include <sound/core.h>
74 #include <sound/control.h>
75 #include <sound/pcm.h>
76 #include <sound/rawmidi.h>
78 #include <sound/initval.h>
79 #include <sound/info.h>
80 #include <asm/hardware.h>
82 #include <asm/parisc-device.h>
84 MODULE_AUTHOR("Laurent Canet <canetl@esiee.fr>");
85 MODULE_DESCRIPTION("ALSA Harmony sound driver");
86 MODULE_LICENSE("GPL");
87 MODULE_CLASSES("{sound}");
88 MODULE_DEVICES("{{ALSA,Harmony soundcard}}");
92 # define DPRINTK printk
94 # define DPRINTK(x,...)
97 #define PFX "harmony: "
99 #define MAX_PCM_DEVICES 1
100 #define MAX_PCM_SUBSTREAMS 4
101 #define MAX_MIDI_DEVICES 0
103 #define BUFFER_SIZE 4096
106 /* number of silence & graveyard buffers */
107 #define GRAVEYARD_BUFS 3
108 #define SILENCE_BUFS 3
110 #define MAX_BUFFER_SIZE (MAX_BUFS * BUFFER_SIZE)
111 #define HARMONY_BUF_SIZE BUFFER_SIZE
113 #define HARMONY_CNTL_C 0x80000000
115 #define HARMONY_DSTATUS_PN 0x00000200
116 #define HARMONY_DSTATUS_RN 0x00000002
117 #define HARMONY_DSTATUS_IE 0x80000000
119 #define HARMONY_DF_16BIT_LINEAR 0x00000000
120 #define HARMONY_DF_8BIT_ULAW 0x00000001
121 #define HARMONY_DF_8BIT_ALAW 0x00000002
123 #define HARMONY_SS_MONO 0x00000000
124 #define HARMONY_SS_STEREO 0x00000001
127 * Channels Mask in mixer register
128 * try some "reasonable" default gain values
131 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
133 /* the following should be enough (mixer is
134 * very sensible on harmony)
136 #define HARMONY_GAIN_DEFAULT 0x0F2FF082
139 /* useless since only one card is supported ATM */
140 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
141 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
142 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
144 /* Register offset (from base hpa) */
146 #define REG_RESET 0x04
147 #define REG_CNTL 0x08
148 #define REG_GAINCTL 0x0C
149 #define REG_PNXTADD 0x10
150 #define REG_PCURADD 0x14
151 #define REG_RNXTADD 0x18
152 #define REG_RCURADD 0x1C
153 #define REG_DSTATUS 0x20
156 #define REG_DIAG 0x3C
159 * main harmony structure
162 typedef struct snd_card_harmony {
164 /* spinlocks (To be done) */
165 spinlock_t mixer_lock;
166 spinlock_t control_lock;
175 int data_format; /* HARMONY_DF_xx_BIT_xxx */
176 int sample_rate; /* HARMONY_SR_xx_KHZ */
177 int stereo_select; /* HARMONY_SS_MONO or HARMONY_SS_STEREO */
178 int format_initialized;
180 unsigned long ply_buffer;
187 unsigned long cap_buffer;
194 struct pci_dev *fake_pci_dev; /* The fake pci_dev needed for
195 pci_* functions under ccio. */
197 /* the graveyard buffer is used as recording buffer when playback,
198 * because harmony always want a buffer to put recorded data */
200 unsigned char *graveyard_addr;
201 dma_addr_t graveyard_dma;
204 /* same thing for silence buffer */
205 unsigned char *silence_addr;
206 dma_addr_t silence_dma;
208 struct snd_dma_device dma_dev;
213 snd_pcm_substream_t *playback_substream;
214 snd_pcm_substream_t *capture_substream;
215 snd_info_entry_t *proc_entry;
216 } snd_card_harmony_t;
217 #define chip_t snd_card_harmony_t
219 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
221 /* wait to be out of control mode */
222 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
226 while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
230 if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
235 * sample rate routines
237 static unsigned int snd_card_harmony_rates[] = {
238 5125, 6615, 8000, 9600,
239 11025, 16000, 18900, 22050,
240 27428, 32000, 33075, 37800,
244 #define RATES sizeof(snd_card_harmony_rates) / sizeof(snd_card_harmony_rates[0])
246 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
248 .list = snd_card_harmony_rates,
252 #define HARMONY_SR_8KHZ 0x08
253 #define HARMONY_SR_16KHZ 0x09
254 #define HARMONY_SR_27KHZ 0x0A
255 #define HARMONY_SR_32KHZ 0x0B
256 #define HARMONY_SR_48KHZ 0x0E
257 #define HARMONY_SR_9KHZ 0x0F
258 #define HARMONY_SR_5KHZ 0x10
259 #define HARMONY_SR_11KHZ 0x11
260 #define HARMONY_SR_18KHZ 0x12
261 #define HARMONY_SR_22KHZ 0x13
262 #define HARMONY_SR_37KHZ 0x14
263 #define HARMONY_SR_44KHZ 0x15
264 #define HARMONY_SR_33KHZ 0x16
265 #define HARMONY_SR_6KHZ 0x17
267 /* snd_card_harmony_rate_bits
268 * @rate: index of current data rate in list
269 * returns: harmony hex code for registers
271 static unsigned int snd_card_harmony_rate_bits(int rate)
275 for (idx = 0; idx <= RATES; idx++)
276 if (snd_card_harmony_rates[idx] == rate) break;
279 case 0: return HARMONY_SR_5KHZ;
280 case 1: return HARMONY_SR_6KHZ;
281 case 2: return HARMONY_SR_8KHZ;
282 case 3: return HARMONY_SR_9KHZ;
283 case 4: return HARMONY_SR_11KHZ;
284 case 5: return HARMONY_SR_16KHZ;
285 case 6: return HARMONY_SR_18KHZ;
286 case 7: return HARMONY_SR_22KHZ;
287 case 8: return HARMONY_SR_27KHZ;
288 case 9: return HARMONY_SR_32KHZ;
289 case 10: return HARMONY_SR_33KHZ;
290 case 11: return HARMONY_SR_37KHZ;
291 case 12: return HARMONY_SR_44KHZ;
292 case 13: return HARMONY_SR_48KHZ;
293 default: /* fallback */
294 return HARMONY_SR_44KHZ;
299 * update controls (data format, sample rate, number of channels)
300 * according to value supplied in data structure
302 void snd_harmony_update_control(snd_card_harmony_t *harmony)
307 default_cntl = (HARMONY_CNTL_C | /* The C bit */
308 (harmony->data_format << 6) | /* Set the data format */
309 (harmony->stereo_select << 5) | /* Stereo select */
310 (harmony->sample_rate)); /* Set sample rate */
312 /* initialize CNTL */
313 snd_harmony_wait_cntl(harmony);
315 gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
321 * XXX: alsa could probably do this by itself
322 * XXX: memset hpmc, commented.
325 void snd_harmony_silence(snd_card_harmony_t *harmony,
326 void *addr, int length)
330 switch(harmony->data_format) {
331 case HARMONY_DF_8BIT_ULAW: silence_char = 0x55; break;
332 case HARMONY_DF_8BIT_ALAW: silence_char = 0xff; break;
333 case HARMONY_DF_16BIT_LINEAR:
337 //memset(addr, silence_char, length);
341 * interruption controls routines
344 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip)
346 snd_harmony_wait_cntl(chip);
347 gsc_writel(0, chip->hpa+REG_DSTATUS);
350 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip)
352 snd_harmony_wait_cntl(chip);
353 gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS);
357 * interruption routine:
358 * The interrupt routine must provide adresse of next physical pages
361 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
363 snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
365 unsigned long hpa = harmony->hpa;
367 /* Turn off interrupts */
368 snd_harmony_disable_interrupts(harmony);
370 /* wait for control to free */
371 snd_harmony_wait_cntl(harmony);
373 /* Read dstatus and pcuradd (the current address) */
374 dstatus = gsc_readl(hpa+REG_DSTATUS);
376 /* Check if this is a request to get the next play buffer */
377 if (dstatus & HARMONY_DSTATUS_PN) {
378 if (harmony->playback_substream) {
379 harmony->ply_buf += harmony->ply_count;
380 harmony->ply_buf %= harmony->ply_size;
382 gsc_writel(harmony->ply_buffer + harmony->ply_buf,
385 snd_pcm_period_elapsed(harmony->playback_substream);
386 harmony->ply_total++;
388 gsc_writel(harmony->silence_dma +
389 (HARMONY_BUF_SIZE*harmony->silence_count),
391 harmony->silence_count++;
392 harmony->silence_count %= SILENCE_BUFS;
396 /* Check if we're being asked to fill in a recording buffer */
397 if (dstatus & HARMONY_DSTATUS_RN) {
398 if (harmony->capture_substream) {
399 harmony->cap_buf += harmony->cap_count;
400 harmony->cap_buf %= harmony->cap_size;
402 gsc_writel(harmony->cap_buffer + harmony->cap_buf,
405 snd_pcm_period_elapsed(harmony->capture_substream);
406 harmony->cap_total++;
408 /* graveyard buffer */
409 gsc_writel(harmony->graveyard_dma +
410 (HARMONY_BUF_SIZE*harmony->graveyard_count),
412 harmony->graveyard_count++;
413 harmony->graveyard_count %= GRAVEYARD_BUFS;
416 snd_harmony_enable_interrupts(harmony);
423 * this proc file will give some debugging info
426 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
428 snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
430 snd_iprintf(buffer, "LASI Harmony driver\nLaurent Canet <canetl@esiee.fr>\n\n");
431 snd_iprintf(buffer, "IRQ %d, hpa %lx, id %d rev %d\n",
432 harmony->irq, harmony->hpa,
433 harmony->id, harmony->rev);
434 snd_iprintf(buffer, "Current gain %lx\n", (unsigned long) harmony->current_gain);
435 snd_iprintf(buffer, "\tsample rate=%d\n", harmony->sample_rate);
436 snd_iprintf(buffer, "\tstereo select=%d\n", harmony->stereo_select);
437 snd_iprintf(buffer, "\tbitperchan=%d\n\n", harmony->data_format);
439 snd_iprintf(buffer, "Play status:\n");
440 snd_iprintf(buffer, "\tstopped %d\n", harmony->ply_stopped);
441 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->ply_buffer, harmony->ply_count);
442 snd_iprintf(buffer, "\tbuf %d size %d\n\n", harmony->ply_buf, harmony->ply_size);
444 snd_iprintf(buffer, "Capture status:\n");
445 snd_iprintf(buffer, "\tstopped %d\n", harmony->cap_stopped);
446 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->cap_buffer, harmony->cap_count);
447 snd_iprintf(buffer, "\tbuf %d, size %d\n\n", harmony->cap_buf, harmony->cap_size);
449 snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
451 snd_iprintf(buffer, "Register:\n");
452 snd_iprintf(buffer, "\tgainctl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_GAINCTL));
453 snd_iprintf(buffer, "\tcntl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_CNTL));
454 snd_iprintf(buffer, "\tid: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_ID));
455 snd_iprintf(buffer, "\tpcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PCURADD));
456 snd_iprintf(buffer, "\trcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RCURADD));
457 snd_iprintf(buffer, "\tpnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PNXTADD));
458 snd_iprintf(buffer, "\trnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RNXTADD));
459 snd_iprintf(buffer, "\tdstatus: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_DSTATUS));
460 snd_iprintf(buffer, "\tov: %lx\n\n", (unsigned long) gsc_readl(harmony->hpa+REG_OV));
464 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
466 snd_info_entry_t *entry;
468 if ((entry = snd_info_create_card_entry(harmony->card, "harmony", harmony->card->proc_root)) != NULL) {
469 entry->content = SNDRV_INFO_CONTENT_TEXT;
470 entry->private_data = harmony;
471 entry->mode = S_IFREG | S_IRUGO | S_IWUSR;
472 entry->c.text.read_size = 2048; /* should be enough */
473 entry->c.text.read = snd_harmony_proc_read;
474 if (snd_info_register(entry) < 0) {
475 snd_info_free_entry(entry);
479 harmony->proc_entry = entry;
482 static void snd_harmony_proc_done(snd_card_harmony_t *harmony)
484 if (harmony->proc_entry) {
485 snd_info_unregister(harmony->proc_entry);
486 harmony->proc_entry = NULL;
494 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
498 return snd_pcm_lib_ioctl(substream, cmd, arg);
501 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
505 return snd_pcm_lib_ioctl(substream, cmd, arg);
508 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
511 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
514 case SNDRV_PCM_TRIGGER_STOP:
515 if (harmony->ply_stopped)
517 harmony->ply_stopped = 1;
518 snd_harmony_disable_interrupts(harmony);
520 case SNDRV_PCM_TRIGGER_START:
521 if (!harmony->ply_stopped)
523 harmony->ply_stopped = 0;
524 /* write the location of the first buffer to play */
525 gsc_writel(harmony->ply_buffer, harmony->hpa+REG_PNXTADD);
526 snd_harmony_enable_interrupts(harmony);
528 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
529 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
530 case SNDRV_PCM_TRIGGER_SUSPEND:
531 DPRINTK(KERN_INFO PFX "received unimplemented trigger: %d\n", cmd);
538 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
541 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
544 case SNDRV_PCM_TRIGGER_STOP:
545 if (harmony->cap_stopped)
547 harmony->cap_stopped = 1;
548 snd_harmony_disable_interrupts(harmony);
550 case SNDRV_PCM_TRIGGER_START:
551 if (!harmony->cap_stopped)
553 harmony->cap_stopped = 0;
554 snd_harmony_enable_interrupts(harmony);
556 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
557 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
558 case SNDRV_PCM_TRIGGER_SUSPEND:
559 DPRINTK(KERN_INFO PFX "Received unimplemented trigger: %d\n", cmd);
566 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
568 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
569 snd_pcm_runtime_t *runtime = substream->runtime;
571 harmony->ply_size = snd_pcm_lib_buffer_bytes(substream);
572 harmony->ply_count = snd_pcm_lib_period_bytes(substream);
573 harmony->ply_buf = 0;
574 harmony->ply_stopped = 1;
576 /* initialize given sample rate */
577 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
580 if (snd_pcm_format_width(runtime->format) == 16) harmony->data_format = HARMONY_DF_16BIT_LINEAR;
581 else harmony->data_format = HARMONY_DF_8BIT_ULAW;
583 /* number of channels */
584 if (runtime->channels == 2) harmony->stereo_select = HARMONY_SS_STEREO;
585 else harmony->stereo_select = HARMONY_SS_MONO;
587 DPRINTK(KERN_INFO PFX "Playback_prepare, sr=%d(%x), df=%x, ss=%x hpa=%lx\n", runtime->rate,
588 harmony->sample_rate, harmony->data_format, harmony->stereo_select, harmony->hpa);
589 snd_harmony_update_control(harmony);
590 harmony->format_initialized = 1;
591 harmony->ply_buffer = runtime->dma_addr;
596 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
598 snd_pcm_runtime_t *runtime = substream->runtime;
599 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
601 harmony->cap_size = snd_pcm_lib_buffer_bytes(substream);
602 harmony->cap_count = snd_pcm_lib_period_bytes(substream);
603 harmony->cap_count = 0;
604 harmony->cap_stopped = 1;
606 /* initialize given sample rate */
607 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
610 if (snd_pcm_format_width(runtime->format) == 16) harmony->data_format = HARMONY_DF_16BIT_LINEAR;
611 else harmony->data_format = HARMONY_DF_8BIT_ULAW;
613 /* number of channels */
614 if (runtime->channels == 1) harmony->stereo_select = HARMONY_SS_MONO;
615 else if (runtime->channels == 2) harmony->stereo_select = HARMONY_SS_STEREO;
617 snd_harmony_update_control(harmony);
618 harmony->format_initialized = 1;
620 harmony->cap_buffer = runtime->dma_addr;
625 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
627 snd_pcm_runtime_t *runtime = substream->runtime;
628 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
629 unsigned long rcuradd;
632 if (harmony->cap_stopped) return 0;
633 if (harmony->capture_substream == NULL) return 0;
635 rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
636 recorded = (rcuradd - harmony->cap_buffer);
637 recorded %= harmony->cap_size;
639 return bytes_to_frames(runtime, recorded);
645 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
647 snd_pcm_runtime_t *runtime = substream->runtime;
648 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
650 long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
652 if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
653 if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
655 played = (pcuradd - harmony->ply_buffer);
657 printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);
659 if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
661 return bytes_to_frames(runtime, played);
664 static snd_pcm_hardware_t snd_card_harmony_playback =
666 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
667 SNDRV_PCM_INFO_JOINT_DUPLEX |
668 SNDRV_PCM_INFO_MMAP_VALID |
669 SNDRV_PCM_INFO_BLOCK_TRANSFER),
670 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
671 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
672 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
677 .buffer_bytes_max = MAX_BUFFER_SIZE,
678 .period_bytes_min = HARMONY_BUF_SIZE,
679 .period_bytes_max = HARMONY_BUF_SIZE,
681 .periods_max = MAX_BUFS,
685 static snd_pcm_hardware_t snd_card_harmony_capture =
687 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
688 SNDRV_PCM_INFO_JOINT_DUPLEX |
689 SNDRV_PCM_INFO_MMAP_VALID |
690 SNDRV_PCM_INFO_BLOCK_TRANSFER),
691 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
692 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
693 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
698 .buffer_bytes_max = MAX_BUFFER_SIZE,
699 .period_bytes_min = HARMONY_BUF_SIZE,
700 .period_bytes_max = HARMONY_BUF_SIZE,
702 .periods_max = MAX_BUFS,
706 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
708 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
709 snd_pcm_runtime_t *runtime = substream->runtime;
713 * harmony is not "real" pci, but we need a pci_dev
714 * to alloc PCI DMA pages
716 substream->runtime->dma_private = harmony->fake_pci_dev;
717 // substream->dma_type = SNDRV_PCM_DMA_TYPE_PCI;
719 harmony->playback_substream = substream;
720 runtime->hw = snd_card_harmony_playback;
721 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
723 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
729 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
731 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
732 snd_pcm_runtime_t *runtime = substream->runtime;
737 * harmony is not "real" pci, but we need a pci_dev
738 * to alloc PCI DMA pages
740 substream->runtime->dma_private = harmony->fake_pci_dev;
741 // substream->dma_type = SNDRV_PCM_DMA_TYPE_PCI;
743 harmony->capture_substream = substream;
744 runtime->hw = snd_card_harmony_capture;
745 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
746 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
752 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
754 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
755 snd_pcm_lib_free_pages(substream);
757 harmony->playback_substream = NULL;
758 harmony->ply_size = 0;
759 harmony->ply_buf = 0;
760 harmony->ply_buffer = 0;
761 harmony->ply_count = 0;
762 harmony->ply_stopped = 1;
763 harmony->format_initialized = 0;
768 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
770 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
772 snd_pcm_lib_free_pages(substream);
774 harmony->capture_substream = NULL;
775 harmony->cap_size = 0;
776 harmony->cap_buf = 0;
777 harmony->cap_buffer = 0;
778 harmony->cap_count = 0;
779 harmony->cap_stopped = 1;
780 harmony->format_initialized = 0;
785 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream,
786 snd_pcm_hw_params_t * hw_params)
788 snd_pcm_runtime_t *runtime = substream->runtime;
791 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
792 DPRINTK(KERN_INFO PFX "HW Params returned %d, dma_addr %lx\n", err,
793 (unsigned long)runtime->dma_addr);
797 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream)
799 snd_pcm_lib_free_pages(substream);
803 static snd_pcm_ops_t snd_card_harmony_playback_ops = {
804 .open = snd_card_harmony_playback_open,
805 .close = snd_card_harmony_playback_close,
806 .ioctl = snd_card_harmony_playback_ioctl,
807 .hw_params = snd_card_harmony_hw_params,
808 .hw_free = snd_card_harmony_hw_free,
809 .prepare = snd_card_harmony_playback_prepare,
810 .trigger = snd_card_harmony_playback_trigger,
811 .pointer = snd_card_harmony_playback_pointer,
814 static snd_pcm_ops_t snd_card_harmony_capture_ops = {
815 .open = snd_card_harmony_capture_open,
816 .close = snd_card_harmony_capture_close,
817 .ioctl = snd_card_harmony_capture_ioctl,
818 .hw_params = snd_card_harmony_hw_params,
819 .hw_free = snd_card_harmony_hw_free,
820 .prepare = snd_card_harmony_capture_prepare,
821 .trigger = snd_card_harmony_capture_trigger,
822 .pointer = snd_card_harmony_capture_pointer,
825 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony, int device)
830 /* Request that IRQ */
831 if (request_irq(harmony->irq, snd_card_harmony_interrupt, 0 ,"harmony", harmony)) {
832 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony->irq);
836 snd_harmony_disable_interrupts(harmony);
838 if ((err = snd_pcm_new(harmony->card, "Harmony", device, 1, 1, &pcm)) < 0)
841 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_harmony_playback_ops);
842 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_harmony_capture_ops);
844 pcm->private_data = harmony;
846 strcpy(pcm->name, "Harmony");
849 /* initialize graveyard buffer */
850 harmony->dma_dev.type = SNDRV_DMA_TYPE_PCI;
851 harmony->dma_dev.dev = snd_dma_pci_data(harmony->fake_pci_dev);
852 harmony->graveyard_addr = snd_dma_alloc_pages(&chip->dma_dev,
853 HARMONY_BUF_SIZE*GRAVEYARD_BUFS, &harmony->graveyard_dma);
854 harmony->graveyard_count = 0;
856 /* initialize silence buffers */
857 harmony->silence_addr = snd_dma_alloc_pages(&chip->dma_dev,
858 HARMONY_BUF_SIZE*SILENCE_BUFS, &harmony->silence_dma);
859 harmony->silence_count = 0;
861 harmony->ply_stopped = harmony->cap_stopped = 1;
863 harmony->playback_substream = NULL;
864 harmony->capture_substream = NULL;
865 harmony->graveyard_count = 0;
867 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
868 snd_dma_pci_data(harmony->fake_pci_dev),
869 64 * 1024, 128 * 1024);
878 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
880 DPRINTK(KERN_INFO PFX "Setting new gain %x at %lx\n", harmony->current_gain, harmony->hpa+REG_GAINCTL);
881 /* Wait until we're out of control mode */
882 snd_harmony_wait_cntl(harmony);
884 gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
887 #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
888 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
889 .info = snd_harmony_mixercontrol_info, \
890 .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
891 .private_value = ((left_shift) | ((right_shift) << 8) | ((mask) << 16) | ((invert) << 24)) }
893 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
895 int mask = (kcontrol->private_value >> 16) & 0xff;
896 int left_shift = (kcontrol->private_value) & 0xff;
897 int right_shift = (kcontrol->private_value >> 8) & 0xff;
899 uinfo->type = (mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER);
900 uinfo->count = (left_shift == right_shift) ? 1 : 2;
901 uinfo->value.integer.min = 0;
902 uinfo->value.integer.max = mask;
906 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
908 snd_card_harmony_t *harmony = _snd_kcontrol_chip(kcontrol);
909 int shift_left = (kcontrol->private_value) & 0xff;
910 int shift_right = (kcontrol->private_value >> 8) & 0xff;
911 int mask = (kcontrol->private_value >> 16) & 0xff;
912 int invert = (kcontrol->private_value >> 24) & 0xff;
916 spin_lock_irqsave(&harmony->mixer_lock, flags);
917 left = (harmony->current_gain >> shift_left) & mask;
918 right = (harmony->current_gain >> shift_right) & mask;
922 right = mask - right;
924 ucontrol->value.integer.value[0] = left;
925 ucontrol->value.integer.value[1] = right;
926 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
931 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
933 snd_card_harmony_t *harmony = _snd_kcontrol_chip(kcontrol);
934 int shift_left = (kcontrol->private_value) & 0xff;
935 int shift_right = (kcontrol->private_value >> 8) & 0xff;
936 int mask = (kcontrol->private_value >> 16) & 0xff;
937 int invert = (kcontrol->private_value >> 24) & 0xff;
940 int old_gain = harmony->current_gain;
942 left = ucontrol->value.integer.value[0] & mask;
943 right = ucontrol->value.integer.value[1] & mask;
946 right = mask - right;
949 spin_lock_irqsave(&harmony->mixer_lock, flags);
950 harmony->current_gain = harmony->current_gain & ~( (mask << shift_right) | (mask << shift_left));
951 harmony->current_gain = harmony->current_gain | ((left << shift_left) | (right << shift_right) );
952 snd_harmony_set_new_gain(harmony);
953 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
955 return (old_gain - harmony->current_gain);
958 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
960 static snd_kcontrol_new_t snd_harmony_controls[] = {
961 HARMONY_VOLUME("PCM Capture Volume", 12, 16, 0x0f, 0),
962 HARMONY_VOLUME("Master Volume", 20, 20, 0x0f, 1),
963 HARMONY_VOLUME("PCM Playback Volume", 6, 0, 0x3f, 1),
966 static void snd_harmony_reset_codec(snd_card_harmony_t *harmony)
968 snd_harmony_wait_cntl(harmony);
969 gsc_writel(1, harmony->hpa+REG_RESET);
970 mdelay(50); /* wait 50 ms */
971 gsc_writel(0, harmony->hpa+REG_RESET);
975 * Mute all the output and reset Harmony.
978 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
980 harmony->current_gain = HARMONY_GAIN_TOTAL_SILENCE;
981 snd_harmony_set_new_gain(harmony);
982 snd_harmony_reset_codec(harmony);
983 harmony->current_gain = HARMONY_GAIN_DEFAULT;
984 snd_harmony_set_new_gain(harmony);
988 int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
990 snd_card_t *card = harmony->card;
993 snd_assert(harmony != NULL, return -EINVAL);
994 strcpy(card->mixername, "Harmony Gain control interface");
996 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
997 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
1001 snd_harmony_mixer_reset(harmony);
1006 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
1010 harmony->card = card;
1012 /* Set the HPA of harmony */
1013 harmony->hpa = pa_dev->hpa;
1016 harmony->irq = pa_dev->irq;
1017 if (!harmony->irq) {
1018 printk(KERN_ERR PFX "no irq found\n");
1022 /* Grab the ID and revision from the device */
1023 harmony->id = (gsc_readl(harmony->hpa+REG_ID)&0x00ff0000) >> 16;
1024 if ((harmony->id | 1) != 0x15) {
1025 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", harmony->id);
1028 cntl = gsc_readl(harmony->hpa+REG_CNTL);
1029 harmony->rev = (cntl>>20) & 0xff;
1031 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);
1033 /* Make sure the control bit isn't set, although I don't think it
1035 if (cntl & HARMONY_CNTL_C) {
1036 printk(KERN_WARNING PFX "CNTL busy\n");
1041 /* a fake pci_dev is needed for pci_* functions under ccio */
1042 harmony->fake_pci_dev = ccio_get_fake(pa_dev);
1046 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1049 snd_card_harmony_t *chip;
1053 if (dev >= SNDRV_CARDS)
1060 snd_harmony_cards[dev] = snd_card_new(index[dev], id[dev], THIS_MODULE,
1061 sizeof(snd_card_harmony_t));
1062 card = snd_harmony_cards[dev];
1066 chip = (struct snd_card_harmony *)card->private_data;
1068 if ((err = snd_card_harmony_create(card, pa_dev, chip)) < 0) {
1069 printk(KERN_ERR PFX "Creation failed\n");
1070 snd_card_free(card);
1073 if ((err = snd_card_harmony_pcm_init(chip, dev)) < 0) {
1074 printk(KERN_ERR PFX "PCM Init failed\n");
1075 snd_card_free(card);
1078 if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1079 printk(KERN_ERR PFX "Mixer init failed\n");
1080 snd_card_free(card);
1084 snd_harmony_proc_init(chip);
1086 strcpy(card->driver, "Harmony");
1087 strcpy(card->shortname, "ALSA driver for LASI Harmony");
1088 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);
1090 if ((err = snd_card_register(card)) < 0) {
1091 snd_card_free(card);
1095 printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
1100 static struct parisc_device_id snd_card_harmony_devicetbl[] = {
1101 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, /* Bushmaster/Flounder */
1102 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1103 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1104 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1108 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
1111 * bloc device parisc. c'est une structure qui definit un device
1112 * que l'on trouve sur parisc.
1113 * On y trouve les differents numeros HVERSION correspondant au device
1114 * en question (ce qui permet a l'inventory de l'identifier) et la fonction
1115 * d'initialisation du chose
1118 static struct parisc_driver snd_card_harmony_driver = {
1119 .name = "Lasi ALSA Harmony",
1120 .id_table = snd_card_harmony_devicetbl,
1121 .probe = snd_card_harmony_probe,
1124 static int __init alsa_card_harmony_init(void)
1128 if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1129 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1136 static void __exit alsa_card_harmony_exit(void)
1139 snd_card_harmony_t *harmony;
1141 for (idx = 0; idx < SNDRV_CARDS; idx++)
1143 if (snd_harmony_cards[idx] != NULL)
1145 DPRINTK(KERN_INFO PFX "Freeing card %d\n", idx);
1146 harmony = snd_harmony_cards[idx]->private_data;
1147 snd_harmony_proc_done(harmony);
1148 free_irq(harmony->irq, snd_card_harmony_interrupt);
1149 printk(KERN_INFO PFX "Card unloaded %d, irq=%d\n", idx, harmony->irq);
1150 snd_card_free(snd_harmony_cards[idx]);
1155 module_init(alsa_card_harmony_init)
1156 module_exit(alsa_card_harmony_exit)