2 * Harmony chipset driver
4 * This is a sound driver for ASP's and Lasi's Harmony sound chip
5 * and is unlikely to be used for anything other than on a HP PA-RISC.
7 * Harmony is found in HP 712s, 715/new and many other GSC based machines.
8 * On older 715 machines you'll find the technically identical chip
9 * called 'Vivace'. Both Harmony and Vivace are supported by this driver.
11 * this ALSA driver is based on OSS driver by:
12 * Copyright 2000 (c) Linuxcare Canada, Alex deVries <alex@linuxcare.com>
13 * Copyright 2000-2002 (c) Helge Deller <deller@gmx.de>
14 * Copyright 2001 (c) Matthieu Delahaye <delahaym@esiee.fr>
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 control full duplex or half duplex mode. It means
47 * that we always need to provide adresses of playback and capture data, even
48 * when this is not needed. That's why we statically alloc one graveyard
49 * buffer (to put recorded data in play-only mode) and a silence buffer.
51 * Bitrate, number of channels and data format are controlled with
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_SUPPORTED_DEVICE("{{ALSA,Harmony soundcard}}");
87 # define DPRINTK printk
89 # define DPRINTK(x,...)
92 #define PFX "harmony: "
94 #define MAX_PCM_DEVICES 1
95 #define MAX_PCM_SUBSTREAMS 4
96 #define MAX_MIDI_DEVICES 0
98 #define HARMONY_BUF_SIZE 4096
100 #define MAX_BUFFER_SIZE (MAX_BUFS * HARMONY_BUF_SIZE)
102 /* number of silence & graveyard buffers */
103 #define GRAVEYARD_BUFS 3
104 #define SILENCE_BUFS 3
106 #define HARMONY_CNTL_C 0x80000000
108 #define HARMONY_DSTATUS_PN 0x00000200
109 #define HARMONY_DSTATUS_RN 0x00000002
110 #define HARMONY_DSTATUS_IE 0x80000000
112 #define HARMONY_DF_16BIT_LINEAR 0x00000000
113 #define HARMONY_DF_8BIT_ULAW 0x00000001
114 #define HARMONY_DF_8BIT_ALAW 0x00000002
116 #define HARMONY_SS_MONO 0x00000000
117 #define HARMONY_SS_STEREO 0x00000001
120 * Channels Mask in mixer register
121 * try some "reasonable" default gain values
124 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
126 /* the following should be enough (mixer is
127 * very sensible on harmony)
129 #define HARMONY_GAIN_DEFAULT 0x0F2FF082
132 /* useless since only one card is supported ATM */
133 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
134 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
135 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
137 module_param_array(index, int, NULL, 0444);
138 MODULE_PARM_DESC(index, "Index value for Harmony device.");
139 module_param_array(id, charp, NULL, 0444);
140 MODULE_PARM_DESC(id, "ID string for Harmony device.");
141 module_param_array(enable, bool, NULL, 0444);
142 MODULE_PARM_DESC(enable, "Enable Harmony device.");
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 parisc_device *pa_dev;
196 struct snd_dma_device dma_dev;
198 /* the graveyard buffer is used as recording buffer when playback,
199 * because harmony always want a buffer to put recorded data */
200 struct snd_dma_buffer graveyard_dma;
203 /* same thing for silence buffer */
204 struct snd_dma_buffer silence_dma;
210 snd_pcm_substream_t *playback_substream;
211 snd_pcm_substream_t *capture_substream;
212 snd_info_entry_t *proc_entry;
213 } snd_card_harmony_t;
215 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
217 /* wait to be out of control mode */
218 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
222 while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
226 if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
231 * sample rate routines
233 static unsigned int snd_card_harmony_rates[] = {
234 5125, 6615, 8000, 9600,
235 11025, 16000, 18900, 22050,
236 27428, 32000, 33075, 37800,
240 static snd_pcm_hw_constraint_list_t hw_constraint_rates = {
241 .count = ARRAY_SIZE(snd_card_harmony_rates),
242 .list = snd_card_harmony_rates,
246 #define HARMONY_SR_8KHZ 0x08
247 #define HARMONY_SR_16KHZ 0x09
248 #define HARMONY_SR_27KHZ 0x0A
249 #define HARMONY_SR_32KHZ 0x0B
250 #define HARMONY_SR_48KHZ 0x0E
251 #define HARMONY_SR_9KHZ 0x0F
252 #define HARMONY_SR_5KHZ 0x10
253 #define HARMONY_SR_11KHZ 0x11
254 #define HARMONY_SR_18KHZ 0x12
255 #define HARMONY_SR_22KHZ 0x13
256 #define HARMONY_SR_37KHZ 0x14
257 #define HARMONY_SR_44KHZ 0x15
258 #define HARMONY_SR_33KHZ 0x16
259 #define HARMONY_SR_6KHZ 0x17
261 /* bits corresponding to the entries of snd_card_harmony_rates */
262 static unsigned int rate_bits[14] = {
263 HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
264 HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
265 HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
266 HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
267 HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
270 /* snd_card_harmony_rate_bits
271 * @rate: index of current data rate in list
272 * returns: harmony hex code for registers
274 static unsigned int snd_card_harmony_rate_bits(int rate)
278 for (idx = 0; idx < ARRAY_SIZE(snd_card_harmony_rates); idx++)
279 if (snd_card_harmony_rates[idx] == rate)
280 return rate_bits[idx];
281 return HARMONY_SR_44KHZ; /* fallback */
285 * update controls (data format, sample rate, number of channels)
286 * according to value supplied in data structure
288 void snd_harmony_update_control(snd_card_harmony_t *harmony)
293 default_cntl = (HARMONY_CNTL_C | /* The C bit */
294 (harmony->data_format << 6) | /* Set the data format */
295 (harmony->stereo_select << 5) | /* Stereo select */
296 (harmony->sample_rate)); /* Set sample rate */
298 /* initialize CNTL */
299 snd_harmony_wait_cntl(harmony);
301 gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
306 * interruption controls routines
309 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip)
311 snd_harmony_wait_cntl(chip);
312 gsc_writel(0, chip->hpa+REG_DSTATUS);
315 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip)
317 snd_harmony_wait_cntl(chip);
318 gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS);
322 * interruption routine:
323 * The interrupt routine must provide adresse of next physical pages
326 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
328 snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
330 unsigned long hpa = harmony->hpa;
332 /* Turn off interrupts */
333 snd_harmony_disable_interrupts(harmony);
335 /* wait for control to free */
336 snd_harmony_wait_cntl(harmony);
338 /* Read dstatus and pcuradd (the current address) */
339 dstatus = gsc_readl(hpa+REG_DSTATUS);
341 /* Check if this is a request to get the next play buffer */
342 if (dstatus & HARMONY_DSTATUS_PN) {
343 if (harmony->playback_substream) {
344 harmony->ply_buf += harmony->ply_count;
345 harmony->ply_buf %= harmony->ply_size;
347 gsc_writel(harmony->ply_buffer + harmony->ply_buf,
350 snd_pcm_period_elapsed(harmony->playback_substream);
351 harmony->ply_total++;
353 gsc_writel(harmony->silence_dma.addr +
354 (HARMONY_BUF_SIZE*harmony->silence_count),
356 harmony->silence_count++;
357 harmony->silence_count %= SILENCE_BUFS;
361 /* Check if we're being asked to fill in a recording buffer */
362 if (dstatus & HARMONY_DSTATUS_RN) {
363 if (harmony->capture_substream) {
364 harmony->cap_buf += harmony->cap_count;
365 harmony->cap_buf %= harmony->cap_size;
367 gsc_writel(harmony->cap_buffer + harmony->cap_buf,
370 snd_pcm_period_elapsed(harmony->capture_substream);
371 harmony->cap_total++;
373 /* graveyard buffer */
374 gsc_writel(harmony->graveyard_dma.addr +
375 (HARMONY_BUF_SIZE*harmony->graveyard_count),
377 harmony->graveyard_count++;
378 harmony->graveyard_count %= GRAVEYARD_BUFS;
381 snd_harmony_enable_interrupts(harmony);
388 * this proc file will give some debugging info
391 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
393 snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
395 snd_iprintf(buffer, "LASI Harmony driver\nLaurent Canet <canetl@esiee.fr>\n\n");
396 snd_iprintf(buffer, "IRQ %d, hpa %lx, id %d rev %d\n",
397 harmony->irq, harmony->hpa,
398 harmony->id, harmony->rev);
399 snd_iprintf(buffer, "Current gain %lx\n", (unsigned long) harmony->current_gain);
400 snd_iprintf(buffer, "\tsample rate=%d\n", harmony->sample_rate);
401 snd_iprintf(buffer, "\tstereo select=%d\n", harmony->stereo_select);
402 snd_iprintf(buffer, "\tbitperchan=%d\n\n", harmony->data_format);
404 snd_iprintf(buffer, "Play status:\n");
405 snd_iprintf(buffer, "\tstopped %d\n", harmony->ply_stopped);
406 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->ply_buffer, harmony->ply_count);
407 snd_iprintf(buffer, "\tbuf %d size %d\n\n", harmony->ply_buf, harmony->ply_size);
409 snd_iprintf(buffer, "Capture status:\n");
410 snd_iprintf(buffer, "\tstopped %d\n", harmony->cap_stopped);
411 snd_iprintf(buffer, "\tbuffer %lx, count %d\n", harmony->cap_buffer, harmony->cap_count);
412 snd_iprintf(buffer, "\tbuf %d, size %d\n\n", harmony->cap_buf, harmony->cap_size);
414 snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
416 snd_iprintf(buffer, "Register:\n");
417 snd_iprintf(buffer, "\tgainctl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_GAINCTL));
418 snd_iprintf(buffer, "\tcntl: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_CNTL));
419 snd_iprintf(buffer, "\tid: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_ID));
420 snd_iprintf(buffer, "\tpcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PCURADD));
421 snd_iprintf(buffer, "\trcuradd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RCURADD));
422 snd_iprintf(buffer, "\tpnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_PNXTADD));
423 snd_iprintf(buffer, "\trnxtadd: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_RNXTADD));
424 snd_iprintf(buffer, "\tdstatus: %lx\n", (unsigned long) gsc_readl(harmony->hpa+REG_DSTATUS));
425 snd_iprintf(buffer, "\tov: %lx\n\n", (unsigned long) gsc_readl(harmony->hpa+REG_OV));
429 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
431 snd_info_entry_t *entry;
433 if (! snd_card_proc_new(harmony->card, "harmony", &entry))
434 snd_info_set_text_ops(entry, harmony, 2048, snd_harmony_proc_read);
441 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
445 return snd_pcm_lib_ioctl(substream, cmd, arg);
448 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
452 return snd_pcm_lib_ioctl(substream, cmd, arg);
455 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
458 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
461 case SNDRV_PCM_TRIGGER_STOP:
462 if (harmony->ply_stopped)
464 harmony->ply_stopped = 1;
465 snd_harmony_disable_interrupts(harmony);
467 case SNDRV_PCM_TRIGGER_START:
468 if (!harmony->ply_stopped)
470 harmony->ply_stopped = 0;
471 /* write the location of the first buffer to play */
472 gsc_writel(harmony->ply_buffer, harmony->hpa+REG_PNXTADD);
473 snd_harmony_enable_interrupts(harmony);
475 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
476 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
477 case SNDRV_PCM_TRIGGER_SUSPEND:
478 DPRINTK(KERN_INFO PFX "received unimplemented trigger: %d\n", cmd);
485 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
488 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
491 case SNDRV_PCM_TRIGGER_STOP:
492 if (harmony->cap_stopped)
494 harmony->cap_stopped = 1;
495 snd_harmony_disable_interrupts(harmony);
497 case SNDRV_PCM_TRIGGER_START:
498 if (!harmony->cap_stopped)
500 harmony->cap_stopped = 0;
501 snd_harmony_enable_interrupts(harmony);
503 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
504 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
505 case SNDRV_PCM_TRIGGER_SUSPEND:
506 DPRINTK(KERN_INFO PFX "Received unimplemented trigger: %d\n", cmd);
513 /* set data format */
514 static int snd_harmony_set_data_format(snd_card_harmony_t *harmony, int pcm_format)
516 int old_format = harmony->data_format;
517 int new_format = old_format;
518 switch (pcm_format) {
519 case SNDRV_PCM_FORMAT_S16_BE:
520 new_format = HARMONY_DF_16BIT_LINEAR;
522 case SNDRV_PCM_FORMAT_A_LAW:
523 new_format = HARMONY_DF_8BIT_ALAW;
525 case SNDRV_PCM_FORMAT_MU_LAW:
526 new_format = HARMONY_DF_8BIT_ULAW;
529 /* re-initialize silence buffer if needed */
530 if (old_format != new_format)
531 snd_pcm_format_set_silence(pcm_format, harmony->silence_dma.area,
532 (HARMONY_BUF_SIZE * SILENCE_BUFS * 8) / snd_pcm_format_width(pcm_format));
537 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
539 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
540 snd_pcm_runtime_t *runtime = substream->runtime;
542 harmony->ply_size = snd_pcm_lib_buffer_bytes(substream);
543 harmony->ply_count = snd_pcm_lib_period_bytes(substream);
544 harmony->ply_buf = 0;
545 harmony->ply_stopped = 1;
547 /* initialize given sample rate */
548 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
551 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
553 /* number of channels */
554 if (runtime->channels == 2)
555 harmony->stereo_select = HARMONY_SS_STEREO;
557 harmony->stereo_select = HARMONY_SS_MONO;
559 DPRINTK(KERN_INFO PFX "Playback_prepare, sr=%d(%x), df=%x, ss=%x hpa=%lx\n", runtime->rate,
560 harmony->sample_rate, harmony->data_format, harmony->stereo_select, harmony->hpa);
561 snd_harmony_update_control(harmony);
562 harmony->format_initialized = 1;
563 harmony->ply_buffer = runtime->dma_addr;
568 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
570 snd_pcm_runtime_t *runtime = substream->runtime;
571 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
573 harmony->cap_size = snd_pcm_lib_buffer_bytes(substream);
574 harmony->cap_count = snd_pcm_lib_period_bytes(substream);
575 harmony->cap_count = 0;
576 harmony->cap_stopped = 1;
578 /* initialize given sample rate */
579 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
582 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
584 /* number of channels */
585 if (runtime->channels == 1)
586 harmony->stereo_select = HARMONY_SS_MONO;
587 else if (runtime->channels == 2)
588 harmony->stereo_select = HARMONY_SS_STEREO;
590 snd_harmony_update_control(harmony);
591 harmony->format_initialized = 1;
593 harmony->cap_buffer = runtime->dma_addr;
598 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
600 snd_pcm_runtime_t *runtime = substream->runtime;
601 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
602 unsigned long rcuradd;
605 if (harmony->cap_stopped) return 0;
606 if (harmony->capture_substream == NULL) return 0;
608 rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
609 recorded = (rcuradd - harmony->cap_buffer);
610 recorded %= harmony->cap_size;
612 return bytes_to_frames(runtime, recorded);
618 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
620 snd_pcm_runtime_t *runtime = substream->runtime;
621 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
623 long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
625 if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
626 if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
628 played = (pcuradd - harmony->ply_buffer);
630 printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);
632 if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
634 return bytes_to_frames(runtime, played);
637 static snd_pcm_hardware_t snd_card_harmony_playback =
639 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
640 SNDRV_PCM_INFO_JOINT_DUPLEX |
641 SNDRV_PCM_INFO_MMAP_VALID |
642 SNDRV_PCM_INFO_BLOCK_TRANSFER),
643 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
644 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
645 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
650 .buffer_bytes_max = MAX_BUFFER_SIZE,
651 .period_bytes_min = HARMONY_BUF_SIZE,
652 .period_bytes_max = HARMONY_BUF_SIZE,
654 .periods_max = MAX_BUFS,
658 static snd_pcm_hardware_t snd_card_harmony_capture =
660 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
661 SNDRV_PCM_INFO_JOINT_DUPLEX |
662 SNDRV_PCM_INFO_MMAP_VALID |
663 SNDRV_PCM_INFO_BLOCK_TRANSFER),
664 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_BE |
665 SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_MU_LAW),
666 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
671 .buffer_bytes_max = MAX_BUFFER_SIZE,
672 .period_bytes_min = HARMONY_BUF_SIZE,
673 .period_bytes_max = HARMONY_BUF_SIZE,
675 .periods_max = MAX_BUFS,
679 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
681 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
682 snd_pcm_runtime_t *runtime = substream->runtime;
685 harmony->playback_substream = substream;
686 runtime->hw = snd_card_harmony_playback;
687 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
689 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
695 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
697 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
698 snd_pcm_runtime_t *runtime = substream->runtime;
701 harmony->capture_substream = substream;
702 runtime->hw = snd_card_harmony_capture;
703 snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraint_rates);
704 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
710 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
712 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
714 harmony->playback_substream = NULL;
715 harmony->ply_size = 0;
716 harmony->ply_buf = 0;
717 harmony->ply_buffer = 0;
718 harmony->ply_count = 0;
719 harmony->ply_stopped = 1;
720 harmony->format_initialized = 0;
725 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
727 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
729 harmony->capture_substream = NULL;
730 harmony->cap_size = 0;
731 harmony->cap_buf = 0;
732 harmony->cap_buffer = 0;
733 harmony->cap_count = 0;
734 harmony->cap_stopped = 1;
735 harmony->format_initialized = 0;
740 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream,
741 snd_pcm_hw_params_t * hw_params)
744 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
746 err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
747 if (err > 0 && harmony->dma_dev.type == SNDRV_DMA_TYPE_CONTINUOUS)
748 substream->runtime->dma_addr = __pa(substream->runtime->dma_area);
749 DPRINTK(KERN_INFO PFX "HW Params returned %d, dma_addr %lx\n", err,
750 (unsigned long)substream->runtime->dma_addr);
754 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream)
756 snd_pcm_lib_free_pages(substream);
760 static snd_pcm_ops_t snd_card_harmony_playback_ops = {
761 .open = snd_card_harmony_playback_open,
762 .close = snd_card_harmony_playback_close,
763 .ioctl = snd_card_harmony_playback_ioctl,
764 .hw_params = snd_card_harmony_hw_params,
765 .hw_free = snd_card_harmony_hw_free,
766 .prepare = snd_card_harmony_playback_prepare,
767 .trigger = snd_card_harmony_playback_trigger,
768 .pointer = snd_card_harmony_playback_pointer,
771 static snd_pcm_ops_t snd_card_harmony_capture_ops = {
772 .open = snd_card_harmony_capture_open,
773 .close = snd_card_harmony_capture_close,
774 .ioctl = snd_card_harmony_capture_ioctl,
775 .hw_params = snd_card_harmony_hw_params,
776 .hw_free = snd_card_harmony_hw_free,
777 .prepare = snd_card_harmony_capture_prepare,
778 .trigger = snd_card_harmony_capture_trigger,
779 .pointer = snd_card_harmony_capture_pointer,
782 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony)
787 /* Request that IRQ */
788 if (request_irq(harmony->irq, snd_card_harmony_interrupt, 0 ,"harmony", harmony)) {
789 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony->irq);
793 snd_harmony_disable_interrupts(harmony);
795 if ((err = snd_pcm_new(harmony->card, "Harmony", 0, 1, 1, &pcm)) < 0)
798 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_card_harmony_playback_ops);
799 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_harmony_capture_ops);
801 pcm->private_data = harmony;
803 strcpy(pcm->name, "Harmony");
806 /* initialize graveyard buffer */
807 harmony->dma_dev.type = SNDRV_DMA_TYPE_DEV;
808 harmony->dma_dev.dev = &harmony->pa_dev->dev;
809 err = snd_dma_alloc_pages(harmony->dma_dev.type,
810 harmony->dma_dev.dev,
811 HARMONY_BUF_SIZE*GRAVEYARD_BUFS,
812 &harmony->graveyard_dma);
813 if (err == -ENOMEM) {
814 /* use continuous buffers */
815 harmony->dma_dev.type = SNDRV_DMA_TYPE_CONTINUOUS;
816 harmony->dma_dev.dev = snd_dma_continuous_data(GFP_KERNEL);
817 err = snd_dma_alloc_pages(harmony->dma_dev.type,
818 harmony->dma_dev.dev,
819 HARMONY_BUF_SIZE*GRAVEYARD_BUFS,
820 &harmony->graveyard_dma);
823 printk(KERN_ERR PFX "can't allocate graveyard buffer\n");
826 harmony->graveyard_count = 0;
828 /* initialize silence buffers */
829 err = snd_dma_alloc_pages(harmony->dma_dev.type,
830 harmony->dma_dev.dev,
831 HARMONY_BUF_SIZE*SILENCE_BUFS,
832 &harmony->silence_dma);
834 printk(KERN_ERR PFX "can't allocate silence buffer\n");
837 harmony->silence_count = 0;
839 if (harmony->dma_dev.type == SNDRV_DMA_TYPE_CONTINUOUS) {
840 harmony->graveyard_dma.addr = __pa(harmony->graveyard_dma.area);
841 harmony->silence_dma.addr = __pa(harmony->silence_dma.area);
844 harmony->ply_stopped = harmony->cap_stopped = 1;
846 harmony->playback_substream = NULL;
847 harmony->capture_substream = NULL;
848 harmony->graveyard_count = 0;
850 err = snd_pcm_lib_preallocate_pages_for_all(pcm, harmony->dma_dev.type,
851 harmony->dma_dev.dev,
852 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
854 printk(KERN_ERR PFX "buffer allocation error %d\n", err);
865 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
867 DPRINTK(KERN_INFO PFX "Setting new gain %x at %lx\n", harmony->current_gain, harmony->hpa+REG_GAINCTL);
868 /* Wait until we're out of control mode */
869 snd_harmony_wait_cntl(harmony);
871 gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
874 #define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
875 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
876 .info = snd_harmony_mixercontrol_info, \
877 .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
878 .private_value = ((left_shift) | ((right_shift) << 8) | ((mask) << 16) | ((invert) << 24)) }
880 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
882 int mask = (kcontrol->private_value >> 16) & 0xff;
883 int left_shift = (kcontrol->private_value) & 0xff;
884 int right_shift = (kcontrol->private_value >> 8) & 0xff;
886 uinfo->type = (mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER);
887 uinfo->count = (left_shift == right_shift) ? 1 : 2;
888 uinfo->value.integer.min = 0;
889 uinfo->value.integer.max = mask;
893 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
895 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
896 int shift_left = (kcontrol->private_value) & 0xff;
897 int shift_right = (kcontrol->private_value >> 8) & 0xff;
898 int mask = (kcontrol->private_value >> 16) & 0xff;
899 int invert = (kcontrol->private_value >> 24) & 0xff;
903 spin_lock_irqsave(&harmony->mixer_lock, flags);
904 left = (harmony->current_gain >> shift_left) & mask;
905 right = (harmony->current_gain >> shift_right) & mask;
909 right = mask - right;
911 ucontrol->value.integer.value[0] = left;
912 ucontrol->value.integer.value[1] = right;
913 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
918 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
920 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
921 int shift_left = (kcontrol->private_value) & 0xff;
922 int shift_right = (kcontrol->private_value >> 8) & 0xff;
923 int mask = (kcontrol->private_value >> 16) & 0xff;
924 int invert = (kcontrol->private_value >> 24) & 0xff;
927 int old_gain = harmony->current_gain;
929 left = ucontrol->value.integer.value[0] & mask;
930 right = ucontrol->value.integer.value[1] & mask;
933 right = mask - right;
936 spin_lock_irqsave(&harmony->mixer_lock, flags);
937 harmony->current_gain = harmony->current_gain & ~( (mask << shift_right) | (mask << shift_left));
938 harmony->current_gain = harmony->current_gain | ((left << shift_left) | (right << shift_right) );
939 snd_harmony_set_new_gain(harmony);
940 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
942 return (old_gain - harmony->current_gain);
945 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
947 static snd_kcontrol_new_t snd_harmony_controls[] = {
948 HARMONY_VOLUME("PCM Capture Volume", 12, 16, 0x0f, 0),
949 HARMONY_VOLUME("Master Volume", 20, 20, 0x0f, 1),
950 HARMONY_VOLUME("PCM Playback Volume", 6, 0, 0x3f, 1),
953 static void __init snd_harmony_reset_codec(snd_card_harmony_t *harmony)
955 snd_harmony_wait_cntl(harmony);
956 gsc_writel(1, harmony->hpa+REG_RESET);
957 mdelay(50); /* wait 50 ms */
958 gsc_writel(0, harmony->hpa+REG_RESET);
962 * Mute all the output and reset Harmony.
965 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
967 harmony->current_gain = HARMONY_GAIN_TOTAL_SILENCE;
968 snd_harmony_set_new_gain(harmony);
969 snd_harmony_reset_codec(harmony);
970 harmony->current_gain = HARMONY_GAIN_DEFAULT;
971 snd_harmony_set_new_gain(harmony);
975 static int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
977 snd_card_t *card = harmony->card;
980 snd_assert(harmony != NULL, return -EINVAL);
981 strcpy(card->mixername, "Harmony Gain control interface");
983 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
984 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
988 snd_harmony_mixer_reset(harmony);
993 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
997 harmony->card = card;
999 harmony->pa_dev = pa_dev;
1001 /* Set the HPA of harmony */
1002 harmony->hpa = pa_dev->hpa;
1004 harmony->irq = pa_dev->irq;
1005 if (!harmony->irq) {
1006 printk(KERN_ERR PFX "no irq found\n");
1010 /* Grab the ID and revision from the device */
1011 harmony->id = (gsc_readl(harmony->hpa+REG_ID)&0x00ff0000) >> 16;
1012 if ((harmony->id | 1) != 0x15) {
1013 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", harmony->id);
1016 cntl = gsc_readl(harmony->hpa+REG_CNTL);
1017 harmony->rev = (cntl>>20) & 0xff;
1019 printk(KERN_INFO "Lasi Harmony Audio driver h/w id %i, rev. %i at 0x%lx, IRQ %i\n", harmony->id, harmony->rev, pa_dev->hpa, harmony->irq);
1021 /* Make sure the control bit isn't set, although I don't think it
1023 if (cntl & HARMONY_CNTL_C) {
1024 printk(KERN_WARNING PFX "CNTL busy\n");
1032 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1035 snd_card_harmony_t *chip;
1039 if (dev >= SNDRV_CARDS)
1046 snd_harmony_cards[dev] = snd_card_new(index[dev], id[dev], THIS_MODULE,
1047 sizeof(snd_card_harmony_t));
1048 card = snd_harmony_cards[dev];
1052 chip = (struct snd_card_harmony *)card->private_data;
1053 spin_lock_init(&chip->control_lock);
1054 spin_lock_init(&chip->mixer_lock);
1056 if ((err = snd_card_harmony_create(card, pa_dev, chip)) < 0) {
1057 printk(KERN_ERR PFX "Creation failed\n");
1058 snd_card_free(card);
1061 if ((err = snd_card_harmony_pcm_init(chip)) < 0) {
1062 printk(KERN_ERR PFX "PCM Init failed\n");
1063 snd_card_free(card);
1066 if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1067 printk(KERN_ERR PFX "Mixer init failed\n");
1068 snd_card_free(card);
1072 snd_harmony_proc_init(chip);
1074 strcpy(card->driver, "Harmony");
1075 strcpy(card->shortname, "ALSA driver for LASI Harmony");
1076 sprintf(card->longname, "%s at h/w, id %i, rev. %i hpa 0x%lx, IRQ %i\n",card->shortname, chip->id, chip->rev, pa_dev->hpa, chip->irq);
1078 if ((err = snd_card_register(card)) < 0) {
1079 snd_card_free(card);
1083 printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
1088 static struct parisc_device_id snd_card_harmony_devicetbl[] = {
1089 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, /* Bushmaster/Flounder */
1090 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1091 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1092 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1096 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
1099 * bloc device parisc. c'est une structure qui definit un device
1100 * que l'on trouve sur parisc.
1101 * On y trouve les differents numeros HVERSION correspondant au device
1102 * en question (ce qui permet a l'inventory de l'identifier) et la fonction
1103 * d'initialisation du chose
1106 static struct parisc_driver snd_card_harmony_driver = {
1107 .name = "Lasi ALSA Harmony",
1108 .id_table = snd_card_harmony_devicetbl,
1109 .probe = snd_card_harmony_probe,
1112 static int __init alsa_card_harmony_init(void)
1116 if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1117 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1124 static void __exit alsa_card_harmony_exit(void)
1127 snd_card_harmony_t *harmony;
1129 for (idx = 0; idx < SNDRV_CARDS; idx++)
1131 if (snd_harmony_cards[idx] != NULL)
1133 DPRINTK(KERN_INFO PFX "Freeing card %d\n", idx);
1134 harmony = snd_harmony_cards[idx]->private_data;
1135 free_irq(harmony->irq, harmony);
1136 printk(KERN_INFO PFX "Card unloaded %d, irq=%d\n", idx, harmony->irq);
1137 snd_card_free(snd_harmony_cards[idx]);
1140 if (unregister_parisc_driver(&snd_card_harmony_driver) < 0)
1141 printk(KERN_ERR PFX "Failed to unregister Harmony driver\n");
1144 module_init(alsa_card_harmony_init)
1145 module_exit(alsa_card_harmony_exit)