2 drivers/sound/harmony.c
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 Copyright 2000 (c) Linuxcare Canada, Alex deVries <alex@linuxcare.com>
12 Copyright 2000-2003 (c) Helge Deller <deller@gmx.de>
13 Copyright 2001 (c) Matthieu Delahaye <delahaym@esiee.fr>
14 Copyright 2001 (c) Jean-Christophe Vaugeois <vaugeoij@esiee.fr>
18 - fix SNDCTL_DSP_GETOSPACE and SNDCTL_DSP_GETISPACE ioctls to
19 return the real values
20 - add private ioctl for selecting line- or microphone input
21 (only one of them is available at the same time)
22 - add module parameters
23 - implement mmap functionality
24 - implement gain meter ?
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/interrupt.h>
32 #include <linux/ioport.h>
33 #include <linux/types.h>
35 #include <linux/pci.h>
37 #include <asm/parisc-device.h>
40 #include "sound_config.h"
43 #define PFX "harmony: "
44 #define HARMONY_VERSION "V0.9a"
48 # define DPRINTK printk
50 # define DPRINTK(x,...)
54 #define MAX_BUFS 10 /* maximum number of rotating buffers */
55 #define HARMONY_BUF_SIZE 4096 /* needs to be a multiple of PAGE_SIZE (4096)! */
57 #define CNTL_C 0x80000000
58 #define CNTL_ST 0x00000020
59 #define CNTL_44100 0x00000015 /* HARMONY_SR_44KHZ */
60 #define CNTL_8000 0x00000008 /* HARMONY_SR_8KHZ */
62 #define GAINCTL_HE 0x08000000
63 #define GAINCTL_LE 0x04000000
64 #define GAINCTL_SE 0x02000000
66 #define DSTATUS_PN 0x00000200
67 #define DSTATUS_RN 0x00000002
69 #define DSTATUS_IE 0x80000000
71 #define HARMONY_DF_16BIT_LINEAR 0
72 #define HARMONY_DF_8BIT_ULAW 1
73 #define HARMONY_DF_8BIT_ALAW 2
75 #define HARMONY_SS_MONO 0
76 #define HARMONY_SS_STEREO 1
78 #define HARMONY_SR_8KHZ 0x08
79 #define HARMONY_SR_16KHZ 0x09
80 #define HARMONY_SR_27KHZ 0x0A
81 #define HARMONY_SR_32KHZ 0x0B
82 #define HARMONY_SR_48KHZ 0x0E
83 #define HARMONY_SR_9KHZ 0x0F
84 #define HARMONY_SR_5KHZ 0x10
85 #define HARMONY_SR_11KHZ 0x11
86 #define HARMONY_SR_18KHZ 0x12
87 #define HARMONY_SR_22KHZ 0x13
88 #define HARMONY_SR_37KHZ 0x14
89 #define HARMONY_SR_44KHZ 0x15
90 #define HARMONY_SR_33KHZ 0x16
91 #define HARMONY_SR_6KHZ 0x17
94 * Some magics numbers used to auto-detect file formats
97 #define HARMONY_MAGIC_8B_ULAW 1
98 #define HARMONY_MAGIC_8B_ALAW 27
99 #define HARMONY_MAGIC_16B_LINEAR 3
100 #define HARMONY_MAGIC_MONO 1
101 #define HARMONY_MAGIC_STEREO 2
104 * Channels Positions in mixer register
107 #define GAIN_HE_SHIFT 27
108 #define GAIN_HE_MASK ( 1 << GAIN_HE_SHIFT)
109 #define GAIN_LE_SHIFT 26
110 #define GAIN_LE_MASK ( 1 << GAIN_LE_SHIFT)
111 #define GAIN_SE_SHIFT 25
112 #define GAIN_SE_MASK ( 1 << GAIN_SE_SHIFT)
113 #define GAIN_IS_SHIFT 24
114 #define GAIN_IS_MASK ( 1 << GAIN_IS_SHIFT)
115 #define GAIN_MA_SHIFT 20
116 #define GAIN_MA_MASK ( 0x0f << GAIN_MA_SHIFT)
117 #define GAIN_LI_SHIFT 16
118 #define GAIN_LI_MASK ( 0x0f << GAIN_LI_SHIFT)
119 #define GAIN_RI_SHIFT 12
120 #define GAIN_RI_MASK ( 0x0f << GAIN_RI_SHIFT)
121 #define GAIN_LO_SHIFT 6
122 #define GAIN_LO_MASK ( 0x3f << GAIN_LO_SHIFT)
123 #define GAIN_RO_SHIFT 0
124 #define GAIN_RO_MASK ( 0x3f << GAIN_RO_SHIFT)
127 #define MAX_OUTPUT_LEVEL (GAIN_RO_MASK >> GAIN_RO_SHIFT)
128 #define MAX_INPUT_LEVEL (GAIN_RI_MASK >> GAIN_RI_SHIFT)
129 #define MAX_VOLUME_LEVEL (GAIN_MA_MASK >> GAIN_MA_SHIFT)
132 * Channels Mask in mixer register
135 #define GAIN_TOTAL_SILENCE 0x00F00FFF
136 #define GAIN_DEFAULT 0x0FF00000
160 struct harmony_hpa *hpa;
161 struct parisc_device *dev;
163 u32 dac_rate; /* 8000 ... 48000 (Hz) */
164 u8 data_format; /* HARMONY_DF_xx_BIT_xxx */
165 u8 sample_rate; /* HARMONY_SR_xx_KHZ */
166 u8 stereo_select; /* HARMONY_SS_MONO or HARMONY_SS_STEREO */
167 int format_initialized :1;
168 int suspended_playing :1;
169 int suspended_recording :1;
171 int blocked_playing :1;
172 int blocked_recording :1;
176 wait_queue_head_t wq_play, wq_record;
177 int first_filled_play; /* first buffer containing data (next to play) */
180 int first_filled_record;
181 int nb_filled_record;
183 int dsp_unit, mixer_unit;
187 static struct harmony_dev harmony;
191 * Dynamic sound buffer allocation and DMA memory
194 struct harmony_buffer {
196 dma_addr_t dma_handle;
197 int dma_coherent; /* Zero if dma_alloc_coherent() fails */
202 * Harmony memory buffers
205 static struct harmony_buffer played_buf, recorded_buf, silent, graveyard;
208 #define CHECK_WBACK_INV_OFFSET(b,offset,len) \
209 do { if (!b.dma_coherent) \
210 dma_cache_wback_inv((unsigned long)b.addr+offset,len); \
214 static int __init harmony_alloc_buffer(struct harmony_buffer *b,
215 unsigned int buffer_count)
217 b->len = buffer_count * HARMONY_BUF_SIZE;
218 b->addr = dma_alloc_coherent(&harmony.dev->dev,
219 b->len, &b->dma_handle, GFP_KERNEL|GFP_DMA);
220 if (b->addr && b->dma_handle) {
222 DPRINTK(KERN_INFO PFX "coherent memory: 0x%lx, played_buf: 0x%lx\n",
223 (unsigned long)b->dma_handle, (unsigned long)b->addr);
226 /* kmalloc()ed memory will HPMC on ccio machines ! */
227 b->addr = kmalloc(b->len, GFP_KERNEL);
229 printk(KERN_ERR PFX "couldn't allocate memory\n");
232 b->dma_handle = __pa(b->addr);
237 static void __exit harmony_free_buffer(struct harmony_buffer *b)
243 dma_free_coherent(&harmony.dev->dev,
244 b->len, b->addr, b->dma_handle);
248 memset(b, 0, sizeof(*b));
254 * Low-Level sound-chip programming
257 static void __inline__ harmony_wait_CNTL(void)
259 /* Wait until we're out of control mode */
260 while (gsc_readl(&harmony.hpa->cntl) & CNTL_C)
265 static void harmony_update_control(void)
270 default_cntl = (CNTL_C | /* The C bit */
271 (harmony.data_format << 6) | /* Set the data format */
272 (harmony.stereo_select << 5) | /* Stereo select */
273 (harmony.sample_rate)); /* Set sample rate */
274 harmony.format_initialized = 1;
276 /* initialize CNTL */
277 gsc_writel(default_cntl, &harmony.hpa->cntl);
280 static void harmony_set_control(u8 data_format, u8 sample_rate, u8 stereo_select)
282 harmony.sample_rate = sample_rate;
283 harmony.data_format = data_format;
284 harmony.stereo_select = stereo_select;
285 harmony_update_control();
288 static void harmony_set_rate(u8 data_rate)
290 harmony.sample_rate = data_rate;
291 harmony_update_control();
294 static int harmony_detect_rate(int *freq)
298 case 8000: newrate = HARMONY_SR_8KHZ; break;
299 case 16000: newrate = HARMONY_SR_16KHZ; break;
300 case 27428: newrate = HARMONY_SR_27KHZ; break;
301 case 32000: newrate = HARMONY_SR_32KHZ; break;
302 case 48000: newrate = HARMONY_SR_48KHZ; break;
303 case 9600: newrate = HARMONY_SR_9KHZ; break;
304 case 5125: newrate = HARMONY_SR_5KHZ; break;
305 case 11025: newrate = HARMONY_SR_11KHZ; break;
306 case 18900: newrate = HARMONY_SR_18KHZ; break;
307 case 22050: newrate = HARMONY_SR_22KHZ; break;
308 case 37800: newrate = HARMONY_SR_37KHZ; break;
309 case 44100: newrate = HARMONY_SR_44KHZ; break;
310 case 33075: newrate = HARMONY_SR_33KHZ; break;
311 case 6615: newrate = HARMONY_SR_6KHZ; break;
312 default: newrate = HARMONY_SR_8KHZ;
318 static void harmony_set_format(u8 data_format)
320 harmony.data_format = data_format;
321 harmony_update_control();
324 static void harmony_set_stereo(u8 stereo_select)
326 harmony.stereo_select = stereo_select;
327 harmony_update_control();
330 static void harmony_disable_interrupts(void)
333 gsc_writel(0, &harmony.hpa->dstatus);
336 static void harmony_enable_interrupts(void)
339 gsc_writel(DSTATUS_IE, &harmony.hpa->dstatus);
345 * This subroutine fills in a buffer starting at location start and
346 * silences for length bytes. This references the current
347 * configuration of the audio format.
351 static void harmony_silence(struct harmony_buffer *buffer, int start, int length)
355 /* Despite what you hear, silence is different in
356 different audio formats. */
357 switch (harmony.data_format) {
358 case HARMONY_DF_8BIT_ULAW: silence_char = 0x55; break;
359 case HARMONY_DF_8BIT_ALAW: silence_char = 0xff; break;
360 case HARMONY_DF_16BIT_LINEAR: /* fall through */
361 default: silence_char = 0;
364 memset(buffer->addr+start, silence_char, length);
368 static int harmony_audio_open(struct inode *inode, struct file *file)
370 if (harmony.audio_open)
373 harmony.audio_open = 1;
374 harmony.suspended_playing = harmony.suspended_recording = 1;
375 harmony.blocked_playing = harmony.blocked_recording = 0;
376 harmony.first_filled_play = harmony.first_filled_record = 0;
377 harmony.nb_filled_play = harmony.nb_filled_record = 0;
378 harmony.play_offset = 0;
379 init_waitqueue_head(&harmony.wq_play);
380 init_waitqueue_head(&harmony.wq_record);
382 /* Start off in a balanced mode. */
383 harmony_set_control(HARMONY_DF_8BIT_ULAW, HARMONY_SR_8KHZ, HARMONY_SS_MONO);
384 harmony_update_control();
385 harmony.format_initialized = 0;
387 /* Clear out all the buffers and flush to cache */
388 harmony_silence(&played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
389 CHECK_WBACK_INV_OFFSET(played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
395 * Release (close) the audio device.
398 static int harmony_audio_release(struct inode *inode, struct file *file)
400 if (!harmony.audio_open)
403 harmony.audio_open = 0;
409 * Read recorded data off the audio device.
412 static ssize_t harmony_audio_read(struct file *file,
417 int total_count = (int) size_count;
421 while (count<total_count) {
422 /* Wait until we're out of control mode */
425 /* Figure out which buffer to fill in */
426 if (harmony.nb_filled_record <= 2) {
427 harmony.blocked_recording = 1;
428 if (harmony.suspended_recording) {
429 harmony.suspended_recording = 0;
430 harmony_enable_interrupts();
433 interruptible_sleep_on(&harmony.wq_record);
434 harmony.blocked_recording = 0;
437 if (harmony.nb_filled_record < 2)
440 buf_to_read = harmony.first_filled_record;
442 /* Copy the page to an aligned buffer */
443 if (copy_to_user(buffer+count, recorded_buf.addr +
444 (HARMONY_BUF_SIZE*buf_to_read),
450 harmony.nb_filled_record--;
451 harmony.first_filled_record++;
452 harmony.first_filled_record %= MAX_BUFS;
454 count += HARMONY_BUF_SIZE;
463 * Here is the place where we try to recognize file format.
464 * Sun/NeXT .au files begin with the string .snd
465 * At offset 12 is specified the encoding.
466 * At offset 16 is specified speed rate
467 * At Offset 20 is specified the numbers of voices
470 #define four_bytes_to_u32(start) (file_header[start] << 24)|\
471 (file_header[start+1] << 16)|\
472 (file_header[start+2] << 8)|\
473 (file_header[start+3]);
475 #define test_rate(tested,real_value,harmony_value) if ((tested)<=(real_value))\
478 static int harmony_format_auto_detect(const char *buffer, int block_size)
485 if (copy_from_user(file_header, buffer, sizeof(file_header)))
488 start_string = four_bytes_to_u32(0);
490 if ((file_header[4]==0) && (start_string==0x2E736E64)) {
495 format = four_bytes_to_u32(12);
496 nb_voices = four_bytes_to_u32(20);
497 speed = four_bytes_to_u32(16);
500 case HARMONY_MAGIC_8B_ULAW:
501 harmony.data_format = HARMONY_DF_8BIT_ULAW;
503 case HARMONY_MAGIC_8B_ALAW:
504 harmony.data_format = HARMONY_DF_8BIT_ALAW;
506 case HARMONY_MAGIC_16B_LINEAR:
507 harmony.data_format = HARMONY_DF_16BIT_LINEAR;
510 harmony_set_control(HARMONY_DF_16BIT_LINEAR,
511 HARMONY_SR_44KHZ, HARMONY_SS_STEREO);
515 case HARMONY_MAGIC_MONO:
516 harmony.stereo_select = HARMONY_SS_MONO;
518 case HARMONY_MAGIC_STEREO:
519 harmony.stereo_select = HARMONY_SS_STEREO;
522 harmony.stereo_select = HARMONY_SS_MONO;
525 harmony_set_rate(harmony_detect_rate(&speed));
526 harmony.dac_rate = speed;
530 harmony_set_control(HARMONY_DF_8BIT_ULAW, HARMONY_SR_8KHZ, HARMONY_SS_MONO);
534 #undef four_bytes_to_u32
537 static ssize_t harmony_audio_write(struct file *file,
542 int total_count = (int) size_count;
547 if (!harmony.format_initialized) {
548 if (harmony_format_auto_detect(buffer, total_count))
552 while (count<total_count) {
553 /* Wait until we're out of control mode */
556 /* Figure out which buffer to fill in */
557 if (harmony.nb_filled_play+2 >= MAX_BUFS && !harmony.play_offset) {
558 harmony.blocked_playing = 1;
559 interruptible_sleep_on(&harmony.wq_play);
560 harmony.blocked_playing = 0;
562 if (harmony.nb_filled_play+2 >= MAX_BUFS && !harmony.play_offset)
566 buf_to_fill = (harmony.first_filled_play+harmony.nb_filled_play);
567 if (harmony.play_offset)
569 buf_to_fill %= MAX_BUFS;
571 /* Figure out the size of the frame */
572 if ((total_count-count) > HARMONY_BUF_SIZE - harmony.play_offset) {
573 frame_size = HARMONY_BUF_SIZE - harmony.play_offset;
575 frame_size = total_count - count;
576 /* Clear out the buffer, since there we'll only be
577 overlaying part of the old buffer with the new one */
578 harmony_silence(&played_buf,
579 HARMONY_BUF_SIZE*buf_to_fill+frame_size+harmony.play_offset,
580 HARMONY_BUF_SIZE-frame_size-harmony.play_offset);
583 /* Copy the page to an aligned buffer */
584 if (copy_from_user(played_buf.addr +(HARMONY_BUF_SIZE*buf_to_fill) + harmony.play_offset,
585 buffer+count, frame_size))
587 CHECK_WBACK_INV_OFFSET(played_buf, (HARMONY_BUF_SIZE*buf_to_fill + harmony.play_offset),
590 if (!harmony.play_offset)
591 harmony.nb_filled_play++;
594 harmony.play_offset += frame_size;
595 harmony.play_offset %= HARMONY_BUF_SIZE;
596 if (harmony.suspended_playing && (harmony.nb_filled_play>=4))
597 harmony_enable_interrupts();
603 static unsigned int harmony_audio_poll(struct file *file,
604 struct poll_table_struct *wait)
606 unsigned int mask = 0;
608 if (file->f_mode & FMODE_READ) {
609 if (!harmony.suspended_recording)
610 poll_wait(file, &harmony.wq_record, wait);
611 if (harmony.nb_filled_record)
612 mask |= POLLIN | POLLRDNORM;
615 if (file->f_mode & FMODE_WRITE) {
616 if (!harmony.suspended_playing)
617 poll_wait(file, &harmony.wq_play, wait);
618 if (harmony.nb_filled_play)
619 mask |= POLLOUT | POLLWRNORM;
625 static int harmony_audio_ioctl(struct inode *inode,
630 int ival, new_format;
631 int frag_size, frag_buf;
632 struct audio_buf_info info;
636 return put_user(SOUND_VERSION, (int *) arg);
638 case SNDCTL_DSP_GETCAPS:
639 ival = DSP_CAP_DUPLEX;
640 return put_user(ival, (int *) arg);
642 case SNDCTL_DSP_GETFMTS:
643 ival = (AFMT_S16_BE | AFMT_MU_LAW | AFMT_A_LAW );
644 return put_user(ival, (int *) arg);
646 case SNDCTL_DSP_SETFMT:
647 if (get_user(ival, (int *) arg))
649 if (ival != AFMT_QUERY) {
651 case AFMT_MU_LAW: new_format = HARMONY_DF_8BIT_ULAW; break;
652 case AFMT_A_LAW: new_format = HARMONY_DF_8BIT_ALAW; break;
653 case AFMT_S16_LE: /* fall through, but not really supported */
654 case AFMT_S16_BE: new_format = HARMONY_DF_16BIT_LINEAR;
658 DPRINTK(KERN_WARNING PFX
659 "unsupported sound format 0x%04x requested.\n",
664 harmony_set_format(new_format);
666 switch (harmony.data_format) {
667 case HARMONY_DF_8BIT_ULAW: ival = AFMT_MU_LAW; break;
668 case HARMONY_DF_8BIT_ALAW: ival = AFMT_A_LAW; break;
669 case HARMONY_DF_16BIT_LINEAR: ival = AFMT_U16_BE; break;
673 return put_user(ival, (int *) arg);
675 case SOUND_PCM_READ_RATE:
676 ival = harmony.dac_rate;
677 return put_user(ival, (int *) arg);
679 case SNDCTL_DSP_SPEED:
680 if (get_user(ival, (int *) arg))
682 harmony_set_rate(harmony_detect_rate(&ival));
683 harmony.dac_rate = ival;
684 return put_user(ival, (int*) arg);
686 case SNDCTL_DSP_STEREO:
687 if (get_user(ival, (int *) arg))
689 if (ival != 0 && ival != 1)
691 harmony_set_stereo(ival);
692 return put_user(ival, (int *) arg);
694 case SNDCTL_DSP_GETBLKSIZE:
695 ival = HARMONY_BUF_SIZE;
696 return put_user(ival, (int *) arg);
698 case SNDCTL_DSP_NONBLOCK:
699 file->f_flags |= O_NONBLOCK;
702 case SNDCTL_DSP_RESET:
703 if (!harmony.suspended_recording) {
704 /* TODO: stop_recording() */
708 case SNDCTL_DSP_SETFRAGMENT:
709 if (get_user(ival, (int *)arg))
711 frag_size = ival & 0xffff;
712 frag_buf = (ival>>16) & 0xffff;
713 /* TODO: We use hardcoded fragment sizes and numbers for now */
714 frag_size = 12; /* 4096 == 2^12 */
716 ival = (frag_buf << 16) + frag_size;
717 return put_user(ival, (int *) arg);
719 case SNDCTL_DSP_GETOSPACE:
720 if (!(file->f_mode & FMODE_WRITE))
722 info.fragstotal = MAX_BUFS;
723 info.fragments = MAX_BUFS - harmony.nb_filled_play;
724 info.fragsize = HARMONY_BUF_SIZE;
725 info.bytes = info.fragments * info.fragsize;
726 return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
728 case SNDCTL_DSP_GETISPACE:
729 if (!(file->f_mode & FMODE_READ))
731 info.fragstotal = MAX_BUFS;
732 info.fragments = /*MAX_BUFS-*/ harmony.nb_filled_record;
733 info.fragsize = HARMONY_BUF_SIZE;
734 info.bytes = info.fragments * info.fragsize;
735 return copy_to_user((void *)arg, &info, sizeof(info)) ? -EFAULT : 0;
737 case SNDCTL_DSP_SYNC:
746 * harmony_interrupt()
748 * harmony interruption service routine
752 static irqreturn_t harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
755 struct harmony_hpa *hpa;
758 hpa = ((struct harmony_dev *)dev)->hpa;
761 /* Read dstatus and pcuradd (the current address) */
762 dstatus = gsc_readl(&hpa->dstatus);
764 /* Turn off interrupts */
765 harmony_disable_interrupts();
767 /* Check if this is a request to get the next play buffer */
768 if (dstatus & DSTATUS_PN) {
769 if (!harmony.nb_filled_play) {
770 harmony.suspended_playing = 1;
771 gsc_writel((unsigned long)silent.dma_handle, &hpa->pnxtadd);
773 if (!harmony.suspended_recording)
774 harmony_enable_interrupts();
776 harmony.suspended_playing = 0;
777 gsc_writel((unsigned long)played_buf.dma_handle +
778 (HARMONY_BUF_SIZE*harmony.first_filled_play),
780 harmony.first_filled_play++;
781 harmony.first_filled_play %= MAX_BUFS;
782 harmony.nb_filled_play--;
784 harmony_enable_interrupts();
787 if (harmony.blocked_playing)
788 wake_up_interruptible(&harmony.wq_play);
791 /* Check if we're being asked to fill in a recording buffer */
792 if (dstatus & DSTATUS_RN) {
793 if((harmony.nb_filled_record+2>=MAX_BUFS) || harmony.suspended_recording)
795 harmony.nb_filled_record = 0;
796 harmony.first_filled_record = 0;
797 harmony.suspended_recording = 1;
798 gsc_writel((unsigned long)graveyard.dma_handle, &hpa->rnxtadd);
799 if (!harmony.suspended_playing)
800 harmony_enable_interrupts();
803 buf_to_fill = (harmony.first_filled_record+harmony.nb_filled_record) % MAX_BUFS;
804 CHECK_WBACK_INV_OFFSET(recorded_buf, HARMONY_BUF_SIZE*buf_to_fill, HARMONY_BUF_SIZE);
805 gsc_writel((unsigned long)recorded_buf.dma_handle +
806 HARMONY_BUF_SIZE*buf_to_fill,
808 harmony.nb_filled_record++;
809 harmony_enable_interrupts();
812 if (harmony.blocked_recording && harmony.nb_filled_record>3)
813 wake_up_interruptible(&harmony.wq_record);
819 * Sound playing functions
822 static struct file_operations harmony_audio_fops = {
823 .owner = THIS_MODULE,
825 .read = harmony_audio_read,
826 .write = harmony_audio_write,
827 .poll = harmony_audio_poll,
828 .ioctl = harmony_audio_ioctl,
829 .open = harmony_audio_open,
830 .release = harmony_audio_release,
833 static int harmony_audio_init(void)
835 /* Request that IRQ */
836 if (request_irq(harmony.dev->irq, harmony_interrupt, 0 ,"harmony", &harmony)) {
837 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony.dev->irq);
841 harmony.dsp_unit = register_sound_dsp(&harmony_audio_fops, -1);
842 if (harmony.dsp_unit < 0) {
843 printk(KERN_ERR PFX "Error registering dsp\n");
844 free_irq(harmony.dev->irq, &harmony);
848 /* Clear the buffers so you don't end up with crap in the buffers. */
849 harmony_silence(&played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
851 /* Make sure this makes it to cache */
852 CHECK_WBACK_INV_OFFSET(played_buf, 0, HARMONY_BUF_SIZE*MAX_BUFS);
854 /* Clear out the silent buffer and flush to cache */
855 harmony_silence(&silent, 0, HARMONY_BUF_SIZE);
856 CHECK_WBACK_INV_OFFSET(silent, 0, HARMONY_BUF_SIZE);
858 harmony.audio_open = 0;
868 static void harmony_mixer_set_gain(void)
871 gsc_writel(harmony.current_gain, &harmony.hpa->gainctl);
875 * Read gain of selected channel.
876 * The OSS rate is from 0 (silent) to 100 -> need some conversions
878 * The harmony gain are attenuation for output and monitor gain.
879 * is amplifaction for input gain
881 #define to_harmony_level(level,max) ((level)*max/100)
882 #define to_oss_level(level,max) ((level)*100/max)
884 static int harmony_mixer_get_level(int channel)
890 case SOUND_MIXER_OGAIN:
891 left_level = (harmony.current_gain & GAIN_LO_MASK) >> GAIN_LO_SHIFT;
892 right_level = (harmony.current_gain & GAIN_RO_MASK) >> GAIN_RO_SHIFT;
893 left_level = to_oss_level(MAX_OUTPUT_LEVEL - left_level, MAX_OUTPUT_LEVEL);
894 right_level = to_oss_level(MAX_OUTPUT_LEVEL - right_level, MAX_OUTPUT_LEVEL);
895 return (right_level << 8)+left_level;
897 case SOUND_MIXER_IGAIN:
898 left_level = (harmony.current_gain & GAIN_LI_MASK) >> GAIN_LI_SHIFT;
899 right_level= (harmony.current_gain & GAIN_RI_MASK) >> GAIN_RI_SHIFT;
900 left_level = to_oss_level(left_level, MAX_INPUT_LEVEL);
901 right_level= to_oss_level(right_level, MAX_INPUT_LEVEL);
902 return (right_level << 8)+left_level;
904 case SOUND_MIXER_VOLUME:
905 left_level = (harmony.current_gain & GAIN_MA_MASK) >> GAIN_MA_SHIFT;
906 left_level = to_oss_level(MAX_VOLUME_LEVEL-left_level, MAX_VOLUME_LEVEL);
915 * Some conversions for the same reasons.
916 * We give back the new real value(s) due to
920 static int harmony_mixer_set_level(int channel, int value)
927 right_level = (value & 0x0000ff00) >> 8;
928 left_level = value & 0x000000ff;
931 case SOUND_MIXER_OGAIN:
932 right_level = to_harmony_level(100-right_level, MAX_OUTPUT_LEVEL);
933 left_level = to_harmony_level(100-left_level, MAX_OUTPUT_LEVEL);
934 new_right_level = to_oss_level(MAX_OUTPUT_LEVEL - right_level, MAX_OUTPUT_LEVEL);
935 new_left_level = to_oss_level(MAX_OUTPUT_LEVEL - left_level, MAX_OUTPUT_LEVEL);
936 harmony.current_gain = (harmony.current_gain & ~(GAIN_LO_MASK | GAIN_RO_MASK))
937 | (left_level << GAIN_LO_SHIFT) | (right_level << GAIN_RO_SHIFT);
938 harmony_mixer_set_gain();
939 return (new_right_level << 8) + new_left_level;
941 case SOUND_MIXER_IGAIN:
942 right_level = to_harmony_level(right_level, MAX_INPUT_LEVEL);
943 left_level = to_harmony_level(left_level, MAX_INPUT_LEVEL);
944 new_right_level = to_oss_level(right_level, MAX_INPUT_LEVEL);
945 new_left_level = to_oss_level(left_level, MAX_INPUT_LEVEL);
946 harmony.current_gain = (harmony.current_gain & ~(GAIN_LI_MASK | GAIN_RI_MASK))
947 | (left_level << GAIN_LI_SHIFT) | (right_level << GAIN_RI_SHIFT);
948 harmony_mixer_set_gain();
949 return (new_right_level << 8) + new_left_level;
951 case SOUND_MIXER_VOLUME:
952 left_level = to_harmony_level(100-left_level, MAX_VOLUME_LEVEL);
953 new_left_level = to_oss_level(MAX_VOLUME_LEVEL-left_level, MAX_VOLUME_LEVEL);
954 harmony.current_gain = (harmony.current_gain & ~GAIN_MA_MASK)| (left_level << GAIN_MA_SHIFT);
955 harmony_mixer_set_gain();
956 return new_left_level;
962 #undef to_harmony_level
966 * Return the selected input device (mic or line)
969 static int harmony_mixer_get_recmask(void)
971 int current_input_line;
973 current_input_line = (harmony.current_gain & GAIN_IS_MASK)
975 if (current_input_line)
976 return SOUND_MASK_MIC;
978 return SOUND_MASK_LINE;
982 * Set the input (only one at time, arbitrary priority to line in)
985 static int harmony_mixer_set_recmask(int recmask)
990 if ((recmask & SOUND_MASK_LINE)) {
992 new_input_mask = SOUND_MASK_LINE;
995 new_input_mask = SOUND_MASK_MIC;
997 harmony.current_gain = ((harmony.current_gain & ~GAIN_IS_MASK) |
998 (new_input_line << GAIN_IS_SHIFT ));
999 harmony_mixer_set_gain();
1000 return new_input_mask;
1005 * give the active outlines
1008 static int harmony_mixer_get_outmask(void)
1012 if (harmony.current_gain & GAIN_HE_MASK) outmask |=SOUND_MASK_PHONEOUT;
1013 if (harmony.current_gain & GAIN_LE_MASK) outmask |=SOUND_MASK_LINE;
1014 if (harmony.current_gain & GAIN_SE_MASK) outmask |=SOUND_MASK_SPEAKER;
1020 static int harmony_mixer_set_outmask(int outmask)
1022 if (outmask & SOUND_MASK_PHONEOUT)
1023 harmony.current_gain |= GAIN_HE_MASK;
1025 harmony.current_gain &= ~GAIN_HE_MASK;
1027 if (outmask & SOUND_MASK_LINE)
1028 harmony.current_gain |= GAIN_LE_MASK;
1030 harmony.current_gain &= ~GAIN_LE_MASK;
1032 if (outmask & SOUND_MASK_SPEAKER)
1033 harmony.current_gain |= GAIN_SE_MASK;
1035 harmony.current_gain &= ~GAIN_SE_MASK;
1037 harmony_mixer_set_gain();
1039 return (outmask & (SOUND_MASK_PHONEOUT | SOUND_MASK_LINE | SOUND_MASK_SPEAKER));
1043 * This code is inspired from sb_mixer.c
1046 static int harmony_mixer_ioctl(struct inode * inode, struct file * file,
1047 unsigned int cmd, unsigned long arg)
1052 if (cmd == SOUND_MIXER_INFO) {
1054 memset(&info, 0, sizeof(info));
1055 strncpy(info.id, "harmony", sizeof(info.id)-1);
1056 strncpy(info.name, "Harmony audio", sizeof(info.name)-1);
1057 info.modify_counter = 1; /* ? */
1058 if (copy_to_user((void *)arg, &info, sizeof(info)))
1063 if (cmd == OSS_GETVERSION)
1064 return put_user(SOUND_VERSION, (int *)arg);
1068 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
1069 if (get_user(val, (int *)arg))
1073 case MIXER_READ(SOUND_MIXER_CAPS):
1074 ret = SOUND_CAP_EXCL_INPUT;
1076 case MIXER_READ(SOUND_MIXER_STEREODEVS):
1077 ret = SOUND_MASK_IGAIN | SOUND_MASK_OGAIN;
1080 case MIXER_READ(SOUND_MIXER_RECMASK):
1081 ret = SOUND_MASK_MIC | SOUND_MASK_LINE;
1083 case MIXER_READ(SOUND_MIXER_DEVMASK):
1084 ret = SOUND_MASK_OGAIN | SOUND_MASK_IGAIN |
1087 case MIXER_READ(SOUND_MIXER_OUTMASK):
1088 ret = SOUND_MASK_SPEAKER | SOUND_MASK_LINE |
1089 SOUND_MASK_PHONEOUT;
1092 case MIXER_WRITE(SOUND_MIXER_RECSRC):
1093 ret = harmony_mixer_set_recmask(val);
1095 case MIXER_READ(SOUND_MIXER_RECSRC):
1096 ret = harmony_mixer_get_recmask();
1099 case MIXER_WRITE(SOUND_MIXER_OUTSRC):
1100 ret = harmony_mixer_set_outmask(val);
1102 case MIXER_READ(SOUND_MIXER_OUTSRC):
1103 ret = harmony_mixer_get_outmask();
1106 case MIXER_WRITE(SOUND_MIXER_OGAIN):
1107 case MIXER_WRITE(SOUND_MIXER_IGAIN):
1108 case MIXER_WRITE(SOUND_MIXER_VOLUME):
1109 ret = harmony_mixer_set_level(cmd & 0xff, val);
1112 case MIXER_READ(SOUND_MIXER_OGAIN):
1113 case MIXER_READ(SOUND_MIXER_IGAIN):
1114 case MIXER_READ(SOUND_MIXER_VOLUME):
1115 ret = harmony_mixer_get_level(cmd & 0xff);
1122 if (put_user(ret, (int *)arg))
1128 static int harmony_mixer_open(struct inode *inode, struct file *file)
1130 if (harmony.mixer_open)
1132 harmony.mixer_open = 1;
1136 static int harmony_mixer_release(struct inode *inode, struct file *file)
1138 if (!harmony.mixer_open)
1140 harmony.mixer_open = 0;
1144 static struct file_operations harmony_mixer_fops = {
1145 .owner = THIS_MODULE,
1146 .llseek = no_llseek,
1147 .open = harmony_mixer_open,
1148 .release = harmony_mixer_release,
1149 .ioctl = harmony_mixer_ioctl,
1154 * Mute all the output and reset Harmony.
1157 static void __init harmony_mixer_reset(void)
1159 harmony.current_gain = GAIN_TOTAL_SILENCE;
1160 harmony_mixer_set_gain();
1161 harmony_wait_CNTL();
1162 gsc_writel(1, &harmony.hpa->reset);
1163 mdelay(50); /* wait 50 ms */
1164 gsc_writel(0, &harmony.hpa->reset);
1165 harmony.current_gain = GAIN_DEFAULT;
1166 harmony_mixer_set_gain();
1169 static int __init harmony_mixer_init(void)
1171 /* Register the device file operations */
1172 harmony.mixer_unit = register_sound_mixer(&harmony_mixer_fops, -1);
1173 if (harmony.mixer_unit < 0) {
1174 printk(KERN_WARNING PFX "Error Registering Mixer Driver\n");
1178 harmony_mixer_reset();
1179 harmony.mixer_open = 0;
1187 * This is the callback that's called by the inventory hardware code
1188 * if it finds a match to the registered driver.
1190 static int __devinit
1191 harmony_driver_probe(struct parisc_device *dev)
1199 /* We only support one Harmony at this time */
1200 printk(KERN_ERR PFX "driver already registered\n");
1206 /* Set the HPA of harmony */
1207 harmony.hpa = (struct harmony_hpa *)dev->hpa;
1209 if (!harmony.dev->irq) {
1210 printk(KERN_ERR PFX "no irq found\n");
1214 /* Grab the ID and revision from the device */
1215 id = gsc_readb(&harmony.hpa->id);
1216 if ((id | 1) != 0x15) {
1217 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", id);
1220 cntl = gsc_readl(&harmony.hpa->cntl);
1221 rev = (cntl>>20) & 0xff;
1223 printk(KERN_INFO "Lasi Harmony Audio driver " HARMONY_VERSION ", "
1224 "h/w id %i, rev. %i at 0x%lx, IRQ %i\n",
1225 id, rev, dev->hpa, harmony.dev->irq);
1227 /* Make sure the control bit isn't set, although I don't think it
1229 if (cntl & CNTL_C) {
1230 printk(KERN_WARNING PFX "CNTL busy\n");
1235 /* Initialize the memory buffers */
1236 if (harmony_alloc_buffer(&played_buf, MAX_BUFS) ||
1237 harmony_alloc_buffer(&recorded_buf, MAX_BUFS) ||
1238 harmony_alloc_buffer(&graveyard, 1) ||
1239 harmony_alloc_buffer(&silent, 1)) {
1244 /* Initialize /dev/mixer and /dev/audio */
1245 if ((ret=harmony_mixer_init()))
1247 if ((ret=harmony_audio_init()))
1254 harmony_free_buffer(&played_buf);
1255 harmony_free_buffer(&recorded_buf);
1256 harmony_free_buffer(&graveyard);
1257 harmony_free_buffer(&silent);
1262 static struct parisc_device_id harmony_tbl[] = {
1263 /* { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, Bushmaster/Flounder */
1264 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1265 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1266 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1270 MODULE_DEVICE_TABLE(parisc, harmony_tbl);
1272 static struct parisc_driver harmony_driver = {
1273 .name = "Lasi Harmony",
1274 .id_table = harmony_tbl,
1275 .probe = harmony_driver_probe,
1278 static int __init init_harmony(void)
1280 return register_parisc_driver(&harmony_driver);
1283 static void __exit cleanup_harmony(void)
1285 free_irq(harmony.dev->irq, &harmony);
1286 unregister_sound_mixer(harmony.mixer_unit);
1287 unregister_sound_dsp(harmony.dsp_unit);
1288 harmony_free_buffer(&played_buf);
1289 harmony_free_buffer(&recorded_buf);
1290 harmony_free_buffer(&graveyard);
1291 harmony_free_buffer(&silent);
1292 unregister_parisc_driver(&harmony_driver);
1296 MODULE_AUTHOR("Alex DeVries <alex@linuxcare.com>");
1297 MODULE_DESCRIPTION("Harmony sound driver");
1298 MODULE_LICENSE("GPL");
1300 module_init(init_harmony);
1301 module_exit(cleanup_harmony);