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.
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>
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_CLASSES("{sound}");
84 MODULE_DEVICES("{{ALSA,Harmony soundcard}}");
88 # define DPRINTK printk
90 # define DPRINTK(x,...)
93 #define PFX "harmony: "
95 #define MAX_PCM_DEVICES 1
96 #define MAX_PCM_SUBSTREAMS 4
97 #define MAX_MIDI_DEVICES 0
99 #define HARMONY_BUF_SIZE 4096
101 #define MAX_BUFFER_SIZE (MAX_BUFS * HARMONY_BUF_SIZE)
103 /* number of silence & graveyard buffers */
104 #define GRAVEYARD_BUFS 3
105 #define SILENCE_BUFS 3
107 #define HARMONY_CNTL_C 0x80000000
109 #define HARMONY_DSTATUS_PN 0x00000200
110 #define HARMONY_DSTATUS_RN 0x00000002
111 #define HARMONY_DSTATUS_IE 0x80000000
113 #define HARMONY_DF_16BIT_LINEAR 0x00000000
114 #define HARMONY_DF_8BIT_ULAW 0x00000001
115 #define HARMONY_DF_8BIT_ALAW 0x00000002
117 #define HARMONY_SS_MONO 0x00000000
118 #define HARMONY_SS_STEREO 0x00000001
121 * Channels Mask in mixer register
122 * try some "reasonable" default gain values
125 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
127 /* the following should be enough (mixer is
128 * very sensible on harmony)
130 #define HARMONY_GAIN_DEFAULT 0x0F2FF082
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;
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);
149 /* Register offset (from base hpa) */
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
161 #define REG_DIAG 0x3C
164 * main harmony structure
167 typedef struct snd_card_harmony {
169 /* spinlocks (To be done) */
170 spinlock_t mixer_lock;
171 spinlock_t control_lock;
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;
185 unsigned long ply_buffer;
192 unsigned long cap_buffer;
199 struct parisc_device *pa_dev;
201 struct snd_dma_device dma_dev;
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;
208 /* same thing for silence buffer */
209 struct snd_dma_buffer silence_dma;
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
221 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
223 /* wait to be out of control mode */
224 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
228 while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
232 if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
237 * sample rate routines
239 static unsigned int snd_card_harmony_rates[] = {
240 5125, 6615, 8000, 9600,
241 11025, 16000, 18900, 22050,
242 27428, 32000, 33075, 37800,
246 #define RATES sizeof(snd_card_harmony_rates) / sizeof(snd_card_harmony_rates[0])
248 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
250 .list = snd_card_harmony_rates,
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
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
278 /* snd_card_harmony_rate_bits
279 * @rate: index of current data rate in list
280 * returns: harmony hex code for registers
282 static unsigned int snd_card_harmony_rate_bits(int rate)
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 */
293 * update controls (data format, sample rate, number of channels)
294 * according to value supplied in data structure
296 void snd_harmony_update_control(snd_card_harmony_t *harmony)
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 */
306 /* initialize CNTL */
307 snd_harmony_wait_cntl(harmony);
309 gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
314 * interruption controls routines
317 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip)
319 snd_harmony_wait_cntl(chip);
320 gsc_writel(0, chip->hpa+REG_DSTATUS);
323 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip)
325 snd_harmony_wait_cntl(chip);
326 gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS);
330 * interruption routine:
331 * The interrupt routine must provide adresse of next physical pages
334 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
336 snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
338 unsigned long hpa = harmony->hpa;
340 /* Turn off interrupts */
341 snd_harmony_disable_interrupts(harmony);
343 /* wait for control to free */
344 snd_harmony_wait_cntl(harmony);
346 /* Read dstatus and pcuradd (the current address) */
347 dstatus = gsc_readl(hpa+REG_DSTATUS);
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;
355 gsc_writel(harmony->ply_buffer + harmony->ply_buf,
358 snd_pcm_period_elapsed(harmony->playback_substream);
359 harmony->ply_total++;
361 gsc_writel(harmony->silence_dma.addr +
362 (HARMONY_BUF_SIZE*harmony->silence_count),
364 harmony->silence_count++;
365 harmony->silence_count %= SILENCE_BUFS;
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;
375 gsc_writel(harmony->cap_buffer + harmony->cap_buf,
378 snd_pcm_period_elapsed(harmony->capture_substream);
379 harmony->cap_total++;
381 /* graveyard buffer */
382 gsc_writel(harmony->graveyard_dma.addr +
383 (HARMONY_BUF_SIZE*harmony->graveyard_count),
385 harmony->graveyard_count++;
386 harmony->graveyard_count %= GRAVEYARD_BUFS;
389 snd_harmony_enable_interrupts(harmony);
396 * this proc file will give some debugging info
399 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
401 snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
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);
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);
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);
422 snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
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));
437 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
439 snd_info_entry_t *entry;
441 if (! snd_card_proc_new(harmony->card, "harmony", &entry))
442 snd_info_set_text_ops(entry, harmony, 2048, snd_harmony_proc_read);
449 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
453 return snd_pcm_lib_ioctl(substream, cmd, arg);
456 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
460 return snd_pcm_lib_ioctl(substream, cmd, arg);
463 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
466 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
469 case SNDRV_PCM_TRIGGER_STOP:
470 if (harmony->ply_stopped)
472 harmony->ply_stopped = 1;
473 snd_harmony_disable_interrupts(harmony);
475 case SNDRV_PCM_TRIGGER_START:
476 if (!harmony->ply_stopped)
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);
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);
493 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
496 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
499 case SNDRV_PCM_TRIGGER_STOP:
500 if (harmony->cap_stopped)
502 harmony->cap_stopped = 1;
503 snd_harmony_disable_interrupts(harmony);
505 case SNDRV_PCM_TRIGGER_START:
506 if (!harmony->cap_stopped)
508 harmony->cap_stopped = 0;
509 snd_harmony_enable_interrupts(harmony);
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);
521 /* set data format */
522 static int snd_harmony_set_data_format(snd_card_harmony_t *harmony, int pcm_format)
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;
530 case SNDRV_PCM_FORMAT_A_LAW:
531 new_format = HARMONY_DF_8BIT_ALAW;
533 case SNDRV_PCM_FORMAT_MU_LAW:
534 new_format = HARMONY_DF_8BIT_ULAW;
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));
545 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
547 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
548 snd_pcm_runtime_t *runtime = substream->runtime;
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;
555 /* initialize given sample rate */
556 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
559 harmony->data_format = snd_harmony_set_data_format(haromny, runtime->format);
561 /* number of channels */
562 if (runtime->channels == 2)
563 harmony->stereo_select = HARMONY_SS_STEREO;
565 harmony->stereo_select = HARMONY_SS_MONO;
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;
576 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
578 snd_pcm_runtime_t *runtime = substream->runtime;
579 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
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;
586 /* initialize given sample rate */
587 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
590 harmony->data_format = snd_harmony_set_data_format(haromny, runtime->format);
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;
598 snd_harmony_update_control(harmony);
599 harmony->format_initialized = 1;
601 harmony->cap_buffer = runtime->dma_addr;
606 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
608 snd_pcm_runtime_t *runtime = substream->runtime;
609 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
610 unsigned long rcuradd;
613 if (harmony->cap_stopped) return 0;
614 if (harmony->capture_substream == NULL) return 0;
616 rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
617 recorded = (rcuradd - harmony->cap_buffer);
618 recorded %= harmony->cap_size;
620 return bytes_to_frames(runtime, recorded);
626 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
628 snd_pcm_runtime_t *runtime = substream->runtime;
629 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
631 long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
633 if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
634 if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
636 played = (pcuradd - harmony->ply_buffer);
638 printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);
640 if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
642 return bytes_to_frames(runtime, played);
645 static snd_pcm_hardware_t snd_card_harmony_playback =
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,
658 .buffer_bytes_max = MAX_BUFFER_SIZE,
659 .period_bytes_min = HARMONY_BUF_SIZE,
660 .period_bytes_max = HARMONY_BUF_SIZE,
662 .periods_max = MAX_BUFS,
666 static snd_pcm_hardware_t snd_card_harmony_capture =
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,
679 .buffer_bytes_max = MAX_BUFFER_SIZE,
680 .period_bytes_min = HARMONY_BUF_SIZE,
681 .period_bytes_max = HARMONY_BUF_SIZE,
683 .periods_max = MAX_BUFS,
687 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
689 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
690 snd_pcm_runtime_t *runtime = substream->runtime;
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);
697 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
703 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
705 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
706 snd_pcm_runtime_t *runtime = substream->runtime;
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)
718 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
720 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
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;
733 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
735 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
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;
748 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream,
749 snd_pcm_hw_params_t * hw_params)
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);
759 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream)
761 snd_pcm_lib_free_pages(substream);
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,
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,
787 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony, int device)
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);
798 snd_harmony_disable_interrupts(harmony);
800 if ((err = snd_pcm_new(harmony->card, "Harmony", device, 1, 1, &pcm)) < 0)
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);
806 pcm->private_data = harmony;
808 strcpy(pcm->name, "Harmony");
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);
818 harmony->graveyard_count = 0;
820 /* initialize silence buffers */
821 err = snd_dma_alloc_pages(&harmony->dma_dev, HARMONY_BUF_SIZE*SILENCE_BUFS,
822 &harmony->silence_dma);
825 harmony->silence_count = 0;
827 harmony->ply_stopped = harmony->cap_stopped = 1;
829 harmony->playback_substream = NULL;
830 harmony->capture_substream = NULL;
831 harmony->graveyard_count = 0;
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);
844 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
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);
850 gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
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)) }
859 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
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;
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;
872 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
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;
882 spin_lock_irqsave(&harmony->mixer_lock, flags);
883 left = (harmony->current_gain >> shift_left) & mask;
884 right = (harmony->current_gain >> shift_right) & mask;
888 right = mask - right;
890 ucontrol->value.integer.value[0] = left;
891 ucontrol->value.integer.value[1] = right;
892 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
897 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
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;
906 int old_gain = harmony->current_gain;
908 left = ucontrol->value.integer.value[0] & mask;
909 right = ucontrol->value.integer.value[1] & mask;
912 right = mask - right;
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);
921 return (old_gain - harmony->current_gain);
924 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
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),
932 static void __init snd_harmony_reset_codec(snd_card_harmony_t *harmony)
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);
941 * Mute all the output and reset Harmony.
944 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
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);
954 static int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
956 snd_card_t *card = harmony->card;
959 snd_assert(harmony != NULL, return -EINVAL);
960 strcpy(card->mixername, "Harmony Gain control interface");
962 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
963 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
967 snd_harmony_mixer_reset(harmony);
972 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
976 harmony->card = card;
978 harmony->pa_dev = pa_dev;
980 /* Set the HPA of harmony */
981 harmony->hpa = pa_dev->hpa;
983 harmony->irq = pa_dev->irq;
985 printk(KERN_ERR PFX "no irq found\n");
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);
995 cntl = gsc_readl(harmony->hpa+REG_CNTL);
996 harmony->rev = (cntl>>20) & 0xff;
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);
1000 /* Make sure the control bit isn't set, although I don't think it
1002 if (cntl & HARMONY_CNTL_C) {
1003 printk(KERN_WARNING PFX "CNTL busy\n");
1011 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1014 snd_card_harmony_t *chip;
1018 if (dev >= SNDRV_CARDS)
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];
1031 chip = (struct snd_card_harmony *)card->private_data;
1032 spin_lock_init(&chip->control_lock);
1033 spin_lock_init(&chip->mixer_lock);
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);
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);
1045 if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1046 printk(KERN_ERR PFX "Mixer init failed\n");
1047 snd_card_free(card);
1051 snd_harmony_proc_init(chip);
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);
1057 if ((err = snd_card_register(card)) < 0) {
1058 snd_card_free(card);
1062 printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
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 */
1075 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
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
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,
1091 static int __init alsa_card_harmony_init(void)
1095 if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1096 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1103 static void __exit alsa_card_harmony_exit(void)
1106 snd_card_harmony_t *harmony;
1108 for (idx = 0; idx < SNDRV_CARDS; idx++)
1110 if (snd_harmony_cards[idx] != NULL)
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]);
1121 module_init(alsa_card_harmony_init)
1122 module_exit(alsa_card_harmony_exit)