1 /*
2 * Harmony chipset driver
3 *
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.
6 *
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.
10 *
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>
15 *
16 * TODO:
17 * - use generic DMA interface and ioremap()/iounmap()
18 * - capture is still untested (and probaby non-working)
19 * - spin locks
20 * - implement non-consistent DMA pages
21 * - implement gain meter
22 * - module parameters
23 * - correct cleaning sequence
24 * - better error checking
25 * - try to have a better quality.
26 *
27 */
28
29 /*
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.
34 *
35 * Playback and recording use 4kb pages (dma or not, depending on the machine).
36 *
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.
45 *
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.
50 *
51 * Bitrate, number of channels and data format are controlled with
52 * the CNTL register.
53 *
54 * Mixer work is done through one register (GAINCTL). Only input gain,
55 * output attenuation and general attenuation control is provided. There is
56 * also controls for enabling/disabling internal speaker and line
57 * input.
58 *
59 * Buffers used by this driver are all DMA consistent.
60 */
61
62 #include <linux/delay.h>
63 #include <sound/driver.h>
64 #include <linux/init.h>
65 #include <linux/interrupt.h>
66 #include <linux/slab.h>
67 #include <linux/time.h>
68 #include <linux/wait.h>
69 #include <linux/moduleparam.h>
70 #include <sound/core.h>
71 #include <sound/control.h>
72 #include <sound/pcm.h>
73 #include <sound/rawmidi.h>
74 #include <sound/initval.h>
75 #include <sound/info.h>
76 #include <asm/hardware.h>
77 #include <asm/io.h>
78 #include <asm/parisc-device.h>
79
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}}");
84
85 #undef DEBUG
86 #ifdef DEBUG
87 # define DPRINTK printk
88 #else
89 # define DPRINTK(x,...)
90 #endif
91
92 #define PFX "harmony: "
93
94 #define MAX_PCM_DEVICES 1
95 #define MAX_PCM_SUBSTREAMS 4
96 #define MAX_MIDI_DEVICES 0
97
98 #define HARMONY_BUF_SIZE 4096
99 #define MAX_BUFS 10
100 #define MAX_BUFFER_SIZE (MAX_BUFS * HARMONY_BUF_SIZE)
101
102 /* number of silence & graveyard buffers */
103 #define GRAVEYARD_BUFS 3
104 #define SILENCE_BUFS 3
105
106 #define HARMONY_CNTL_C 0x80000000
107
108 #define HARMONY_DSTATUS_PN 0x00000200
109 #define HARMONY_DSTATUS_RN 0x00000002
110 #define HARMONY_DSTATUS_IE 0x80000000
111
112 #define HARMONY_DF_16BIT_LINEAR 0x00000000
113 #define HARMONY_DF_8BIT_ULAW 0x00000001
114 #define HARMONY_DF_8BIT_ALAW 0x00000002
115
116 #define HARMONY_SS_MONO 0x00000000
117 #define HARMONY_SS_STEREO 0x00000001
118
119 /*
120 * Channels Mask in mixer register
121 * try some "reasonable" default gain values
122 */
123
124 #define HARMONY_GAIN_TOTAL_SILENCE 0x00F00FFF
125
126 /* the following should be enough (mixer is
127 * very sensible on harmony)
128 */
129 #define HARMONY_GAIN_DEFAULT 0x0F2FF082
130
131
132 /* useless since only one card is supported ATM */
133 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
134 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
135 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
136
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.");
143
144 /* Register offset (from base hpa) */
145 #define REG_ID 0x00
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
154 #define REG_OV 0x24
155 #define REG_PIO 0x28
156 #define REG_DIAG 0x3C
157
158 /*
159 * main harmony structure
160 */
161
162 typedef struct snd_card_harmony {
163
164 /* spinlocks (To be done) */
165 spinlock_t mixer_lock;
166 spinlock_t control_lock;
167
168 /* parameters */
169 int irq;
170 unsigned long hpa;
171 int id;
172 int rev;
173
174 u32 current_gain;
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;
179
180 unsigned long ply_buffer;
181 int ply_buf;
182 int ply_count;
183 int ply_size;
184 int ply_stopped;
185 int ply_total;
186
187 unsigned long cap_buffer;
188 int cap_buf;
189 int cap_count;
190 int cap_size;
191 int cap_stopped;
192 int cap_total;
193
194 struct parisc_device *pa_dev;
195
196 struct snd_dma_device dma_dev;
197
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;
201 int graveyard_count;
202
203 /* same thing for silence buffer */
204 struct snd_dma_buffer silence_dma;
205 int silence_count;
206
207 /* alsa stuff */
208 snd_card_t *card;
209 snd_pcm_t *pcm;
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;
214
215 static snd_card_t *snd_harmony_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
216
217 /* wait to be out of control mode */
218 static inline void snd_harmony_wait_cntl(snd_card_harmony_t *harmony)
219 {
220 int timeout = 5000;
221
222 while ( (gsc_readl(harmony->hpa+REG_CNTL) & HARMONY_CNTL_C) && --timeout)
223 {
224 /* Wait */ ;
225 }
226 if (timeout == 0) DPRINTK(KERN_DEBUG PFX "Error: wait cntl timeouted\n");
227 }
228
229
230 /*
231 * sample rate routines
232 */
233 static unsigned int snd_card_harmony_rates[] = {
234 5125, 6615, 8000, 9600,
235 11025, 16000, 18900, 22050,
236 27428, 32000, 33075, 37800,
237 44100, 48000
238 };
239
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,
243 .mask = 0,
244 };
245
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
260
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
268 };
269
270 /* snd_card_harmony_rate_bits
271 * @rate: index of current data rate in list
272 * returns: harmony hex code for registers
273 */
274 static unsigned int snd_card_harmony_rate_bits(int rate)
275 {
276 unsigned int idx;
277
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 */
282 }
283
284 /*
285 * update controls (data format, sample rate, number of channels)
286 * according to value supplied in data structure
287 */
288 void snd_harmony_update_control(snd_card_harmony_t *harmony)
289 {
290 u32 default_cntl;
291
292 /* Set CNTL */
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 */
297
298 /* initialize CNTL */
299 snd_harmony_wait_cntl(harmony);
300
301 gsc_writel(default_cntl, harmony->hpa+REG_CNTL);
302
303 }
304
305 /*
306 * interruption controls routines
307 */
308
309 static void snd_harmony_disable_interrupts(snd_card_harmony_t *chip)
310 {
311 snd_harmony_wait_cntl(chip);
312 gsc_writel(0, chip->hpa+REG_DSTATUS);
313 }
314
315 static void snd_harmony_enable_interrupts(snd_card_harmony_t *chip)
316 {
317 snd_harmony_wait_cntl(chip);
318 gsc_writel(HARMONY_DSTATUS_IE, chip->hpa+REG_DSTATUS);
319 }
320
321 /*
322 * interruption routine:
323 * The interrupt routine must provide adresse of next physical pages
324 * used by harmony
325 */
326 static int snd_card_harmony_interrupt(int irq, void *dev, struct pt_regs *regs)
327 {
328 snd_card_harmony_t *harmony = (snd_card_harmony_t *)dev;
329 u32 dstatus = 0;
330 unsigned long hpa = harmony->hpa;
331
332 /* Turn off interrupts */
333 snd_harmony_disable_interrupts(harmony);
334
335 /* wait for control to free */
336 snd_harmony_wait_cntl(harmony);
337
338 /* Read dstatus and pcuradd (the current address) */
339 dstatus = gsc_readl(hpa+REG_DSTATUS);
340
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;
346
347 gsc_writel(harmony->ply_buffer + harmony->ply_buf,
348 hpa+REG_PNXTADD);
349
350 snd_pcm_period_elapsed(harmony->playback_substream);
351 harmony->ply_total++;
352 } else {
353 gsc_writel(harmony->silence_dma.addr +
354 (HARMONY_BUF_SIZE*harmony->silence_count),
355 hpa+REG_PNXTADD);
356 harmony->silence_count++;
357 harmony->silence_count %= SILENCE_BUFS;
358 }
359 }
360
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;
366
367 gsc_writel(harmony->cap_buffer + harmony->cap_buf,
368 hpa+REG_RNXTADD);
369
370 snd_pcm_period_elapsed(harmony->capture_substream);
371 harmony->cap_total++;
372 } else {
373 /* graveyard buffer */
374 gsc_writel(harmony->graveyard_dma.addr +
375 (HARMONY_BUF_SIZE*harmony->graveyard_count),
376 hpa+REG_RNXTADD);
377 harmony->graveyard_count++;
378 harmony->graveyard_count %= GRAVEYARD_BUFS;
379 }
380 }
381 snd_harmony_enable_interrupts(harmony);
382
383 return IRQ_HANDLED;
384 }
385
386 /*
387 * proc entry
388 * this proc file will give some debugging info
389 */
390
391 static void snd_harmony_proc_read(snd_info_entry_t *entry, snd_info_buffer_t *buffer)
392 {
393 snd_card_harmony_t *harmony = (snd_card_harmony_t *)entry->private_data;
394
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);
403
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);
408
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);
413
414 snd_iprintf(buffer, "Funny stats: total played=%d, recorded=%d\n\n", harmony->ply_total, harmony->cap_total);
415
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));
426
427 }
428
429 static void __devinit snd_harmony_proc_init(snd_card_harmony_t *harmony)
430 {
431 snd_info_entry_t *entry;
432
433 if (! snd_card_proc_new(harmony->card, "harmony", &entry))
434 snd_info_set_text_ops(entry, harmony, 2048, snd_harmony_proc_read);
435 }
436
437 /*
438 * PCM Stuff
439 */
440
441 static int snd_card_harmony_playback_ioctl(snd_pcm_substream_t * substream,
442 unsigned int cmd,
443 void *arg)
444 {
445 return snd_pcm_lib_ioctl(substream, cmd, arg);
446 }
447
448 static int snd_card_harmony_capture_ioctl(snd_pcm_substream_t * substream,
449 unsigned int cmd,
450 void *arg)
451 {
452 return snd_pcm_lib_ioctl(substream, cmd, arg);
453 }
454
455 static int snd_card_harmony_playback_trigger(snd_pcm_substream_t * substream,
456 int cmd)
457 {
458 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
459
460 switch (cmd) {
461 case SNDRV_PCM_TRIGGER_STOP:
462 if (harmony->ply_stopped)
463 return -EBUSY;
464 harmony->ply_stopped = 1;
465 snd_harmony_disable_interrupts(harmony);
466 break;
467 case SNDRV_PCM_TRIGGER_START:
468 if (!harmony->ply_stopped)
469 return -EBUSY;
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);
474 break;
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);
479 default:
480 return -EINVAL;
481 }
482 return 0;
483 }
484
485 static int snd_card_harmony_capture_trigger(snd_pcm_substream_t * substream,
486 int cmd)
487 {
488 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
489
490 switch (cmd) {
491 case SNDRV_PCM_TRIGGER_STOP:
492 if (harmony->cap_stopped)
493 return -EBUSY;
494 harmony->cap_stopped = 1;
495 snd_harmony_disable_interrupts(harmony);
496 break;
497 case SNDRV_PCM_TRIGGER_START:
498 if (!harmony->cap_stopped)
499 return -EBUSY;
500 harmony->cap_stopped = 0;
501 snd_harmony_enable_interrupts(harmony);
502 break;
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);
507 default:
508 return -EINVAL;
509 }
510 return 0;
511 }
512
513 /* set data format */
514 static int snd_harmony_set_data_format(snd_card_harmony_t *harmony, int pcm_format)
515 {
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;
521 break;
522 case SNDRV_PCM_FORMAT_A_LAW:
523 new_format = HARMONY_DF_8BIT_ALAW;
524 break;
525 case SNDRV_PCM_FORMAT_MU_LAW:
526 new_format = HARMONY_DF_8BIT_ULAW;
527 break;
528 }
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));
533
534 return new_format;
535 }
536
537 static int snd_card_harmony_playback_prepare(snd_pcm_substream_t * substream)
538 {
539 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
540 snd_pcm_runtime_t *runtime = substream->runtime;
541
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;
546
547 /* initialize given sample rate */
548 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
549
550 /* data format */
551 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
552
553 /* number of channels */
554 if (runtime->channels == 2)
555 harmony->stereo_select = HARMONY_SS_STEREO;
556 else
557 harmony->stereo_select = HARMONY_SS_MONO;
558
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;
564
565 return 0;
566 }
567
568 static int snd_card_harmony_capture_prepare(snd_pcm_substream_t * substream)
569 {
570 snd_pcm_runtime_t *runtime = substream->runtime;
571 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
572
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;
577
578 /* initialize given sample rate */
579 harmony->sample_rate = snd_card_harmony_rate_bits(runtime->rate);
580
581 /* data format */
582 harmony->data_format = snd_harmony_set_data_format(harmony, runtime->format);
583
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;
589
590 snd_harmony_update_control(harmony);
591 harmony->format_initialized = 1;
592
593 harmony->cap_buffer = runtime->dma_addr;
594
595 return 0;
596 }
597
598 static snd_pcm_uframes_t snd_card_harmony_capture_pointer(snd_pcm_substream_t * substream)
599 {
600 snd_pcm_runtime_t *runtime = substream->runtime;
601 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
602 unsigned long rcuradd;
603 int recorded;
604
605 if (harmony->cap_stopped) return 0;
606 if (harmony->capture_substream == NULL) return 0;
607
608 rcuradd = gsc_readl(harmony->hpa+REG_RCURADD);
609 recorded = (rcuradd - harmony->cap_buffer);
610 recorded %= harmony->cap_size;
611
612 return bytes_to_frames(runtime, recorded);
613 }
614
615 /*
616 */
617
618 static snd_pcm_uframes_t snd_card_harmony_playback_pointer(snd_pcm_substream_t * substream)
619 {
620 snd_pcm_runtime_t *runtime = substream->runtime;
621 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
622 int played;
623 long int pcuradd = gsc_readl(harmony->hpa+REG_PCURADD);
624
625 if ((harmony->ply_stopped) || (harmony->playback_substream == NULL)) return 0;
626 if ((harmony->ply_buffer == 0) || (harmony->ply_size == 0)) return 0;
627
628 played = (pcuradd - harmony->ply_buffer);
629
630 printk(KERN_DEBUG PFX "Pointer is %lx-%lx = %d\n", pcuradd, harmony->ply_buffer, played);
631
632 if (pcuradd > harmony->ply_buffer + harmony->ply_size) return 0;
633
634 return bytes_to_frames(runtime, played);
635 }
636
637 static snd_pcm_hardware_t snd_card_harmony_playback =
638 {
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,
646 .rate_min = 5500,
647 .rate_max = 48000,
648 .channels_min = 1,
649 .channels_max = 2,
650 .buffer_bytes_max = MAX_BUFFER_SIZE,
651 .period_bytes_min = HARMONY_BUF_SIZE,
652 .period_bytes_max = HARMONY_BUF_SIZE,
653 .periods_min = 1,
654 .periods_max = MAX_BUFS,
655 .fifo_size = 0,
656 };
657
658 static snd_pcm_hardware_t snd_card_harmony_capture =
659 {
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,
667 .rate_min = 5500,
668 .rate_max = 48000,
669 .channels_min = 1,
670 .channels_max = 2,
671 .buffer_bytes_max = MAX_BUFFER_SIZE,
672 .period_bytes_min = HARMONY_BUF_SIZE,
673 .period_bytes_max = HARMONY_BUF_SIZE,
674 .periods_min = 1,
675 .periods_max = MAX_BUFS,
676 .fifo_size = 0,
677 };
678
679 static int snd_card_harmony_playback_open(snd_pcm_substream_t * substream)
680 {
681 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
682 snd_pcm_runtime_t *runtime = substream->runtime;
683 int err;
684
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);
688
689 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
690 return err;
691
692 return 0;
693 }
694
695 static int snd_card_harmony_capture_open(snd_pcm_substream_t * substream)
696 {
697 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
698 snd_pcm_runtime_t *runtime = substream->runtime;
699 int err;
700
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)
705 return err;
706 return 0;
707
708 }
709
710 static int snd_card_harmony_playback_close(snd_pcm_substream_t * substream)
711 {
712 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
713
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;
721
722 return 0;
723 }
724
725 static int snd_card_harmony_capture_close(snd_pcm_substream_t * substream)
726 {
727 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
728
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;
736
737 return 0;
738 }
739
740 static int snd_card_harmony_hw_params(snd_pcm_substream_t *substream,
741 snd_pcm_hw_params_t * hw_params)
742 {
743 int err;
744 snd_card_harmony_t *harmony = snd_pcm_substream_chip(substream);
745
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);
751 return err;
752 }
753
754 static int snd_card_harmony_hw_free(snd_pcm_substream_t *substream)
755 {
756 snd_pcm_lib_free_pages(substream);
757 return 0;
758 }
759
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,
769 };
770
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,
780 };
781
782 static int snd_card_harmony_pcm_init(snd_card_harmony_t *harmony)
783 {
784 snd_pcm_t *pcm;
785 int err;
786
787 /* Request that IRQ */
788 if (request_irq(harmony->irq, snd_card_harmony_interrupt, 0 ,"harmony", harmony)) {
789 printk(KERN_ERR PFX "Error requesting irq %d.\n", harmony->irq);
790 return -EFAULT;
791 }
792
793 snd_harmony_disable_interrupts(harmony);
794
795 if ((err = snd_pcm_new(harmony->card, "Harmony", 0, 1, 1, &pcm)) < 0)
796 return err;
797
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);
800
801 pcm->private_data = harmony;
802 pcm->info_flags = 0;
803 strcpy(pcm->name, "Harmony");
804 harmony->pcm = pcm;
805
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);
821 }
822 if (err < 0) {
823 printk(KERN_ERR PFX "can't allocate graveyard buffer\n");
824 return err;
825 }
826 harmony->graveyard_count = 0;
827
828 /* initialize silence buffers */
829 err = snd_dma_alloc_pages(harmony->dma_dev.type,
830 harmony->dma_dev.dev,
831 HARMONY_BUF_SIZE*SILENCE_BUFS,
832 &harmony->silence_dma);
833 if (err < 0) {
834 printk(KERN_ERR PFX "can't allocate silence buffer\n");
835 return err;
836 }
837 harmony->silence_count = 0;
838
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);
842 }
843
844 harmony->ply_stopped = harmony->cap_stopped = 1;
845
846 harmony->playback_substream = NULL;
847 harmony->capture_substream = NULL;
848 harmony->graveyard_count = 0;
849
850 err = snd_pcm_lib_preallocate_pages_for_all(pcm, harmony->dma_dev.type,
851 harmony->dma_dev.dev,
852 MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);
853 if (err < 0) {
854 printk(KERN_ERR PFX "buffer allocation error %d\n", err);
855 // return err;
856 }
857
858 return 0;
859 }
860
861 /*
862 * mixer routines
863 */
864
865 static void snd_harmony_set_new_gain(snd_card_harmony_t *harmony)
866 {
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);
870
871 gsc_writel(harmony->current_gain, harmony->hpa+REG_GAINCTL);
872 }
873
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)) }
879
880 static int snd_harmony_mixercontrol_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
881 {
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;
885
886 uinfo->type = (mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER);
887 uinfo->count = (left_shift == right_shift) ? 1 : 2;
888 uinfo->value.integer.min = 0;
889 uinfo->value.integer.max = mask;
890 return 0;
891 }
892
893 static int snd_harmony_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
894 {
895 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
896 int shift_left = (kcontrol->private_value) & 0xff;
897 int shift_right = (kcontrol->private_value >> 8) & 0xff;
898 int mask = (kcontrol->private_value >> 16) & 0xff;
899 int invert = (kcontrol->private_value >> 24) & 0xff;
900 unsigned long flags;
901 int left, right;
902
903 spin_lock_irqsave(&harmony->mixer_lock, flags);
904 left = (harmony->current_gain >> shift_left) & mask;
905 right = (harmony->current_gain >> shift_right) & mask;
906
907 if (invert) {
908 left = mask - left;
909 right = mask - right;
910 }
911 ucontrol->value.integer.value[0] = left;
912 ucontrol->value.integer.value[1] = right;
913 spin_unlock_irqrestore(&harmony->mixer_lock, flags);
914
915 return 0;
916 }
917
918 static int snd_harmony_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
919 {
920 snd_card_harmony_t *harmony = snd_kcontrol_chip(kcontrol);
921 int shift_left = (kcontrol->private_value) & 0xff;
922 int shift_right = (kcontrol->private_value >> 8) & 0xff;
923 int mask = (kcontrol->private_value >> 16) & 0xff;
924 int invert = (kcontrol->private_value >> 24) & 0xff;
925 unsigned long flags;
926 int left, right;
927 int old_gain = harmony->current_gain;
928
929 left = ucontrol->value.integer.value[0] & mask;
930 right = ucontrol->value.integer.value[1] & mask;
931 if (invert) {
932 left = mask - left;
933 right = mask - right;
934 }
935
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);
941
942 return (old_gain - harmony->current_gain);
943 }
944
945 #define HARMONY_CONTROLS (sizeof(snd_harmony_controls)/sizeof(snd_kcontrol_new_t))
946
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),
951 };
952
953 static void __init snd_harmony_reset_codec(snd_card_harmony_t *harmony)
954 {
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);
959 }
960
961 /*
962 * Mute all the output and reset Harmony.
963 */
964
965 static void __init snd_harmony_mixer_reset(snd_card_harmony_t *harmony)
966 {
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);
972 }
973
974
975 static int __init snd_card_harmony_mixer_init(snd_card_harmony_t *harmony)
976 {
977 snd_card_t *card = harmony->card;
978 int idx, err;
979
980 snd_assert(harmony != NULL, return -EINVAL);
981 strcpy(card->mixername, "Harmony Gain control interface");
982
983 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
984 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_harmony_controls[idx], harmony))) < 0)
985 return err;
986 }
987
988 snd_harmony_mixer_reset(harmony);
989
990 return 0;
991 }
992
993 static int snd_card_harmony_create(snd_card_t *card, struct parisc_device *pa_dev, snd_card_harmony_t *harmony)
994 {
995 u32 cntl;
996
997 harmony->card = card;
998
999 harmony->pa_dev = pa_dev;
1000
1001 /* Set the HPA of harmony */
1002 harmony->hpa = pa_dev->hpa;
1003
1004 harmony->irq = pa_dev->irq;
1005 if (!harmony->irq) {
1006 printk(KERN_ERR PFX "no irq found\n");
1007 return -ENODEV;
1008 }
1009
1010 /* Grab the ID and revision from the device */
1011 harmony->id = (gsc_readl(harmony->hpa+REG_ID)&0x00ff0000) >> 16;
1012 if ((harmony->id | 1) != 0x15) {
1013 printk(KERN_WARNING PFX "wrong harmony id 0x%02x\n", harmony->id);
1014 return -EBUSY;
1015 }
1016 cntl = gsc_readl(harmony->hpa+REG_CNTL);
1017 harmony->rev = (cntl>>20) & 0xff;
1018
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);
1020
1021 /* Make sure the control bit isn't set, although I don't think it
1022 ever is. */
1023 if (cntl & HARMONY_CNTL_C) {
1024 printk(KERN_WARNING PFX "CNTL busy\n");
1025 harmony->hpa = 0;
1026 return -EBUSY;
1027 }
1028
1029 return 0;
1030 }
1031
1032 static int __init snd_card_harmony_probe(struct parisc_device *pa_dev)
1033 {
1034 static int dev;
1035 snd_card_harmony_t *chip;
1036 snd_card_t *card;
1037 int err;
1038
1039 if (dev >= SNDRV_CARDS)
1040 return -ENODEV;
1041 if (!enable[dev]) {
1042 dev++;
1043 return -ENOENT;
1044 }
1045
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];
1049
1050 if (card == NULL)
1051 return -ENOMEM;
1052 chip = (struct snd_card_harmony *)card->private_data;
1053 spin_lock_init(&chip->control_lock);
1054 spin_lock_init(&chip->mixer_lock);
1055
1056 if ((err = snd_card_harmony_create(card, pa_dev, chip)) < 0) {
1057 printk(KERN_ERR PFX "Creation failed\n");
1058 snd_card_free(card);
1059 return err;
1060 }
1061 if ((err = snd_card_harmony_pcm_init(chip)) < 0) {
1062 printk(KERN_ERR PFX "PCM Init failed\n");
1063 snd_card_free(card);
1064 return err;
1065 }
1066 if ((err = snd_card_harmony_mixer_init(chip)) < 0) {
1067 printk(KERN_ERR PFX "Mixer init failed\n");
1068 snd_card_free(card);
1069 return err;
1070 }
1071
1072 snd_harmony_proc_init(chip);
1073
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);
1077
1078 if ((err = snd_card_register(card)) < 0) {
1079 snd_card_free(card);
1080 return err;
1081 }
1082
1083 printk(KERN_DEBUG PFX "Successfully registered harmony pcm backend & mixer %d\n", dev);
1084 dev++;
1085 return 0;
1086 }
1087
1088 static struct parisc_device_id snd_card_harmony_devicetbl[] = {
1089 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A }, /* Bushmaster/Flounder */
1090 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B }, /* 712/715 Audio */
1091 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E }, /* Pace Audio */
1092 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F }, /* Outfield / Coral II */
1093 { 0, }
1094 };
1095
1096 MODULE_DEVICE_TABLE(parisc, snd_card_harmony_devicetbl);
1097
1098 /*
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
1104 */
1105
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,
1110 };
1111
1112 static int __init alsa_card_harmony_init(void)
1113 {
1114 int err;
1115
1116 if ((err = register_parisc_driver(&snd_card_harmony_driver)) < 0) {
1117 printk(KERN_ERR "Harmony soundcard not found or device busy\n");
1118 return err;
1119 }
1120
1121 return 0;
1122 }
1123
1124 static void __exit alsa_card_harmony_exit(void)
1125 {
1126 int idx;
1127 snd_card_harmony_t *harmony;
1128
1129 for (idx = 0; idx < SNDRV_CARDS; idx++)
1130 {
1131 if (snd_harmony_cards[idx] != NULL)
1132 {
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]);
1138 }
1139 }
1140 if (unregister_parisc_driver(&snd_card_harmony_driver) < 0)
1141 printk(KERN_ERR PFX "Failed to unregister Harmony driver\n");
1142 }
1143
1144 module_init(alsa_card_harmony_init)
1145 module_exit(alsa_card_harmony_exit)
1146
|
This page was automatically generated by the
LXR engine.
|