blob: f47f9e226b0823c35970fcbed96096e18042d15f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Hewlett-Packard Harmony audio driver
2 *
3 * This is a driver for the Harmony audio chipset found
4 * on the LASI ASIC of various early HP PA-RISC workstations.
5 *
6 * Copyright (C) 2004, Kyle McMartin <kyle@{debian.org,parisc-linux.org}>
7 *
8 * Based on the previous Harmony incarnations by,
9 * Copyright 2000 (c) Linuxcare Canada, Alex deVries
10 * Copyright 2000-2003 (c) Helge Deller
11 * Copyright 2001 (c) Matthieu Delahaye
12 * Copyright 2001 (c) Jean-Christophe Vaugeois
13 * Copyright 2003 (c) Laurent Canet
14 * Copyright 2004 (c) Stuart Brady
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 * Notes:
30 * - graveyard and silence buffers last for lifetime of
31 * the driver. playback and capture buffers are allocated
32 * per _open()/_close().
33 *
34 * TODO:
35 *
36 */
37
38#include <linux/init.h>
39#include <linux/slab.h>
40#include <linux/time.h>
41#include <linux/wait.h>
42#include <linux/delay.h>
43#include <linux/module.h>
44#include <linux/interrupt.h>
45#include <linux/spinlock.h>
46#include <linux/dma-mapping.h>
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <sound/core.h>
49#include <sound/pcm.h>
50#include <sound/control.h>
51#include <sound/rawmidi.h>
52#include <sound/initval.h>
53#include <sound/info.h>
54
55#include <asm/io.h>
56#include <asm/hardware.h>
57#include <asm/parisc-device.h>
58
59#include "harmony.h"
60
Takashi Iwai03f9ae22005-11-17 10:34:40 +010061static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
62static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
63module_param(index, int, 0444);
64MODULE_PARM_DESC(index, "Index value for Harmony driver.");
65module_param(id, charp, 0444);
66MODULE_PARM_DESC(id, "ID string for Harmony driver.");
67
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static struct parisc_device_id snd_harmony_devtable[] = {
70 /* bushmaster / flounder */
71 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007A },
72 /* 712 / 715 */
73 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007B },
74 /* pace */
75 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007E },
76 /* outfield / coral II */
77 { HPHW_FIO, HVERSION_REV_ANY_ID, HVERSION_ANY_ID, 0x0007F },
78 { 0, }
79};
80
81MODULE_DEVICE_TABLE(parisc, snd_harmony_devtable);
82
83#define NAME "harmony"
84#define PFX NAME ": "
85
86static unsigned int snd_harmony_rates[] = {
87 5512, 6615, 8000, 9600,
88 11025, 16000, 18900, 22050,
89 27428, 32000, 33075, 37800,
90 44100, 48000
91};
92
93static unsigned int rate_bits[14] = {
94 HARMONY_SR_5KHZ, HARMONY_SR_6KHZ, HARMONY_SR_8KHZ,
95 HARMONY_SR_9KHZ, HARMONY_SR_11KHZ, HARMONY_SR_16KHZ,
96 HARMONY_SR_18KHZ, HARMONY_SR_22KHZ, HARMONY_SR_27KHZ,
97 HARMONY_SR_32KHZ, HARMONY_SR_33KHZ, HARMONY_SR_37KHZ,
98 HARMONY_SR_44KHZ, HARMONY_SR_48KHZ
99};
100
Takashi Iwai67b10202005-11-17 15:12:11 +0100101static struct snd_pcm_hw_constraint_list hw_constraint_rates = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .count = ARRAY_SIZE(snd_harmony_rates),
103 .list = snd_harmony_rates,
104 .mask = 0,
105};
106
Takashi Iwai67b10202005-11-17 15:12:11 +0100107static inline unsigned long
108harmony_read(struct snd_harmony *h, unsigned r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 return __raw_readl(h->iobase + r);
111}
112
Takashi Iwai67b10202005-11-17 15:12:11 +0100113static inline void
114harmony_write(struct snd_harmony *h, unsigned r, unsigned long v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 __raw_writel(v, h->iobase + r);
117}
118
Takashi Iwai67b10202005-11-17 15:12:11 +0100119static inline void
120harmony_wait_for_control(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 while (harmony_read(h, HARMONY_CNTL) & HARMONY_CNTL_C) ;
123}
124
Takashi Iwai67b10202005-11-17 15:12:11 +0100125static inline void
126harmony_reset(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 harmony_write(h, HARMONY_RESET, 1);
129 mdelay(50);
130 harmony_write(h, HARMONY_RESET, 0);
131}
132
133static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100134harmony_disable_interrupts(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 u32 dstatus;
137 harmony_wait_for_control(h);
138 dstatus = harmony_read(h, HARMONY_DSTATUS);
139 dstatus &= ~HARMONY_DSTATUS_IE;
140 harmony_write(h, HARMONY_DSTATUS, dstatus);
141}
142
143static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100144harmony_enable_interrupts(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 u32 dstatus;
147 harmony_wait_for_control(h);
148 dstatus = harmony_read(h, HARMONY_DSTATUS);
149 dstatus |= HARMONY_DSTATUS_IE;
150 harmony_write(h, HARMONY_DSTATUS, dstatus);
151}
152
153static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100154harmony_mute(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 unsigned long flags;
157
158 spin_lock_irqsave(&h->mixer_lock, flags);
159 harmony_wait_for_control(h);
160 harmony_write(h, HARMONY_GAINCTL, HARMONY_GAIN_SILENCE);
161 spin_unlock_irqrestore(&h->mixer_lock, flags);
162}
163
164static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100165harmony_unmute(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 unsigned long flags;
168
169 spin_lock_irqsave(&h->mixer_lock, flags);
170 harmony_wait_for_control(h);
171 harmony_write(h, HARMONY_GAINCTL, h->st.gain);
172 spin_unlock_irqrestore(&h->mixer_lock, flags);
173}
174
175static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100176harmony_set_control(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 u32 ctrl;
179 unsigned long flags;
180
181 spin_lock_irqsave(&h->lock, flags);
182
183 ctrl = (HARMONY_CNTL_C |
184 (h->st.format << 6) |
185 (h->st.stereo << 5) |
186 (h->st.rate));
187
188 harmony_wait_for_control(h);
189 harmony_write(h, HARMONY_CNTL, ctrl);
190
191 spin_unlock_irqrestore(&h->lock, flags);
192}
193
194static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100195snd_harmony_interrupt(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 u32 dstatus;
Takashi Iwai67b10202005-11-17 15:12:11 +0100198 struct snd_harmony *h = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 spin_lock(&h->lock);
201 harmony_disable_interrupts(h);
202 harmony_wait_for_control(h);
203 dstatus = harmony_read(h, HARMONY_DSTATUS);
204 spin_unlock(&h->lock);
205
206 if (dstatus & HARMONY_DSTATUS_PN) {
Stuart Brady3a165682005-10-21 22:42:38 -0400207 if (h->psubs && h->st.playing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_lock(&h->lock);
209 h->pbuf.buf += h->pbuf.count; /* PAGE_SIZE */
210 h->pbuf.buf %= h->pbuf.size; /* MAX_BUFS*PAGE_SIZE */
211
212 harmony_write(h, HARMONY_PNXTADD,
213 h->pbuf.addr + h->pbuf.buf);
214 h->stats.play_intr++;
215 spin_unlock(&h->lock);
216 snd_pcm_period_elapsed(h->psubs);
217 } else {
218 spin_lock(&h->lock);
219 harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
220 h->stats.silence_intr++;
221 spin_unlock(&h->lock);
222 }
223 }
224
225 if (dstatus & HARMONY_DSTATUS_RN) {
Stuart Brady3a165682005-10-21 22:42:38 -0400226 if (h->csubs && h->st.capturing) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_lock(&h->lock);
228 h->cbuf.buf += h->cbuf.count;
229 h->cbuf.buf %= h->cbuf.size;
230
231 harmony_write(h, HARMONY_RNXTADD,
232 h->cbuf.addr + h->cbuf.buf);
233 h->stats.rec_intr++;
234 spin_unlock(&h->lock);
235 snd_pcm_period_elapsed(h->csubs);
236 } else {
237 spin_lock(&h->lock);
238 harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
239 h->stats.graveyard_intr++;
240 spin_unlock(&h->lock);
241 }
242 }
243
244 spin_lock(&h->lock);
245 harmony_enable_interrupts(h);
246 spin_unlock(&h->lock);
247
248 return IRQ_HANDLED;
249}
250
251static unsigned int
252snd_harmony_rate_bits(int rate)
253{
254 unsigned int i;
255
256 for (i = 0; i < ARRAY_SIZE(snd_harmony_rates); i++)
257 if (snd_harmony_rates[i] == rate)
258 return rate_bits[i];
259
260 return HARMONY_SR_44KHZ;
261}
262
Takashi Iwai67b10202005-11-17 15:12:11 +0100263static struct snd_pcm_hardware snd_harmony_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
266 SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
267 SNDRV_PCM_INFO_BLOCK_TRANSFER),
268 .formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
269 SNDRV_PCM_FMTBIT_A_LAW),
270 .rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
271 SNDRV_PCM_RATE_KNOT),
272 .rate_min = 5512,
273 .rate_max = 48000,
274 .channels_min = 1,
275 .channels_max = 2,
276 .buffer_bytes_max = MAX_BUF_SIZE,
277 .period_bytes_min = BUF_SIZE,
278 .period_bytes_max = BUF_SIZE,
279 .periods_min = 1,
280 .periods_max = MAX_BUFS,
281 .fifo_size = 0,
282};
283
Takashi Iwai67b10202005-11-17 15:12:11 +0100284static struct snd_pcm_hardware snd_harmony_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
287 SNDRV_PCM_INFO_JOINT_DUPLEX | SNDRV_PCM_INFO_MMAP_VALID |
288 SNDRV_PCM_INFO_BLOCK_TRANSFER),
289 .formats = (SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_MU_LAW |
290 SNDRV_PCM_FMTBIT_A_LAW),
291 .rates = (SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000 |
292 SNDRV_PCM_RATE_KNOT),
293 .rate_min = 5512,
294 .rate_max = 48000,
295 .channels_min = 1,
296 .channels_max = 2,
297 .buffer_bytes_max = MAX_BUF_SIZE,
298 .period_bytes_min = BUF_SIZE,
299 .period_bytes_max = BUF_SIZE,
300 .periods_min = 1,
301 .periods_max = MAX_BUFS,
302 .fifo_size = 0,
303};
304
305static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100306snd_harmony_playback_trigger(struct snd_pcm_substream *ss, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Takashi Iwai67b10202005-11-17 15:12:11 +0100308 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 if (h->st.capturing)
311 return -EBUSY;
312
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100313 spin_lock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 switch (cmd) {
315 case SNDRV_PCM_TRIGGER_START:
316 h->st.playing = 1;
317 harmony_write(h, HARMONY_PNXTADD, h->pbuf.addr);
318 harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
319 harmony_unmute(h);
320 harmony_enable_interrupts(h);
321 break;
322 case SNDRV_PCM_TRIGGER_STOP:
323 h->st.playing = 0;
324 harmony_mute(h);
Stuart Brady3a165682005-10-21 22:42:38 -0400325 harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 harmony_disable_interrupts(h);
327 break;
328 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
329 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
330 case SNDRV_PCM_TRIGGER_SUSPEND:
331 default:
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100332 spin_unlock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 snd_BUG();
334 return -EINVAL;
335 }
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100336 spin_unlock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 return 0;
339}
340
341static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100342snd_harmony_capture_trigger(struct snd_pcm_substream *ss, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Takashi Iwai67b10202005-11-17 15:12:11 +0100344 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (h->st.playing)
347 return -EBUSY;
348
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100349 spin_lock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 switch (cmd) {
351 case SNDRV_PCM_TRIGGER_START:
352 h->st.capturing = 1;
353 harmony_write(h, HARMONY_PNXTADD, h->sdma.addr);
354 harmony_write(h, HARMONY_RNXTADD, h->cbuf.addr);
355 harmony_unmute(h);
356 harmony_enable_interrupts(h);
357 break;
358 case SNDRV_PCM_TRIGGER_STOP:
359 h->st.capturing = 0;
Stuart Brady3a165682005-10-21 22:42:38 -0400360 harmony_mute(h);
361 harmony_write(h, HARMONY_RNXTADD, h->gdma.addr);
362 harmony_disable_interrupts(h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
365 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
366 case SNDRV_PCM_TRIGGER_SUSPEND:
367 default:
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100368 spin_unlock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 snd_BUG();
370 return -EINVAL;
371 }
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100372 spin_unlock(&h->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 return 0;
375}
376
377static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100378snd_harmony_set_data_format(struct snd_harmony *h, int fmt, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 int o = h->st.format;
381 int n;
382
383 switch(fmt) {
384 case SNDRV_PCM_FORMAT_S16_BE:
385 n = HARMONY_DF_16BIT_LINEAR;
386 break;
387 case SNDRV_PCM_FORMAT_A_LAW:
388 n = HARMONY_DF_8BIT_ALAW;
389 break;
390 case SNDRV_PCM_FORMAT_MU_LAW:
391 n = HARMONY_DF_8BIT_ULAW;
392 break;
393 default:
394 n = HARMONY_DF_16BIT_LINEAR;
395 break;
396 }
397
398 if (force || o != n) {
399 snd_pcm_format_set_silence(fmt, h->sdma.area, SILENCE_BUFSZ /
400 (snd_pcm_format_physical_width(fmt)
401 / 8));
402 }
403
404 return n;
405}
406
407static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100408snd_harmony_playback_prepare(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Takashi Iwai67b10202005-11-17 15:12:11 +0100410 struct snd_harmony *h = snd_pcm_substream_chip(ss);
411 struct snd_pcm_runtime *rt = ss->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 if (h->st.capturing)
414 return -EBUSY;
415
416 h->pbuf.size = snd_pcm_lib_buffer_bytes(ss);
417 h->pbuf.count = snd_pcm_lib_period_bytes(ss);
Stuart Brady3a165682005-10-21 22:42:38 -0400418 if (h->pbuf.buf >= h->pbuf.size)
419 h->pbuf.buf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 h->st.playing = 0;
421
422 h->st.rate = snd_harmony_rate_bits(rt->rate);
423 h->st.format = snd_harmony_set_data_format(h, rt->format, 0);
424
425 if (rt->channels == 2)
426 h->st.stereo = HARMONY_SS_STEREO;
427 else
428 h->st.stereo = HARMONY_SS_MONO;
429
430 harmony_set_control(h);
431
432 h->pbuf.addr = rt->dma_addr;
433
434 return 0;
435}
436
437static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100438snd_harmony_capture_prepare(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Takashi Iwai67b10202005-11-17 15:12:11 +0100440 struct snd_harmony *h = snd_pcm_substream_chip(ss);
441 struct snd_pcm_runtime *rt = ss->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (h->st.playing)
444 return -EBUSY;
445
446 h->cbuf.size = snd_pcm_lib_buffer_bytes(ss);
447 h->cbuf.count = snd_pcm_lib_period_bytes(ss);
Stuart Brady3a165682005-10-21 22:42:38 -0400448 if (h->cbuf.buf >= h->cbuf.size)
449 h->cbuf.buf = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 h->st.capturing = 0;
451
452 h->st.rate = snd_harmony_rate_bits(rt->rate);
453 h->st.format = snd_harmony_set_data_format(h, rt->format, 0);
454
455 if (rt->channels == 2)
456 h->st.stereo = HARMONY_SS_STEREO;
457 else
458 h->st.stereo = HARMONY_SS_MONO;
459
460 harmony_set_control(h);
461
462 h->cbuf.addr = rt->dma_addr;
463
464 return 0;
465}
466
467static snd_pcm_uframes_t
Takashi Iwai67b10202005-11-17 15:12:11 +0100468snd_harmony_playback_pointer(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Takashi Iwai67b10202005-11-17 15:12:11 +0100470 struct snd_pcm_runtime *rt = ss->runtime;
471 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 unsigned long pcuradd;
473 unsigned long played;
474
475 if (!(h->st.playing) || (h->psubs == NULL))
476 return 0;
477
478 if ((h->pbuf.addr == 0) || (h->pbuf.size == 0))
479 return 0;
480
481 pcuradd = harmony_read(h, HARMONY_PCURADD);
482 played = pcuradd - h->pbuf.addr;
483
484#ifdef HARMONY_DEBUG
485 printk(KERN_DEBUG PFX "playback_pointer is 0x%lx-0x%lx = %d bytes\n",
486 pcuradd, h->pbuf.addr, played);
487#endif
488
489 if (pcuradd > h->pbuf.addr + h->pbuf.size) {
490 return 0;
491 }
492
493 return bytes_to_frames(rt, played);
494}
495
496static snd_pcm_uframes_t
Takashi Iwai67b10202005-11-17 15:12:11 +0100497snd_harmony_capture_pointer(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Takashi Iwai67b10202005-11-17 15:12:11 +0100499 struct snd_pcm_runtime *rt = ss->runtime;
500 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long rcuradd;
502 unsigned long caught;
503
504 if (!(h->st.capturing) || (h->csubs == NULL))
505 return 0;
506
507 if ((h->cbuf.addr == 0) || (h->cbuf.size == 0))
508 return 0;
509
510 rcuradd = harmony_read(h, HARMONY_RCURADD);
511 caught = rcuradd - h->cbuf.addr;
512
513#ifdef HARMONY_DEBUG
514 printk(KERN_DEBUG PFX "capture_pointer is 0x%lx-0x%lx = %d bytes\n",
515 rcuradd, h->cbuf.addr, caught);
516#endif
517
518 if (rcuradd > h->cbuf.addr + h->cbuf.size) {
519 return 0;
520 }
521
522 return bytes_to_frames(rt, caught);
523}
524
525static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100526snd_harmony_playback_open(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Takashi Iwai67b10202005-11-17 15:12:11 +0100528 struct snd_harmony *h = snd_pcm_substream_chip(ss);
529 struct snd_pcm_runtime *rt = ss->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int err;
531
532 h->psubs = ss;
533 rt->hw = snd_harmony_playback;
534 snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
535 &hw_constraint_rates);
536
537 err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
538 if (err < 0)
539 return err;
540
541 return 0;
542}
543
544static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100545snd_harmony_capture_open(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Takashi Iwai67b10202005-11-17 15:12:11 +0100547 struct snd_harmony *h = snd_pcm_substream_chip(ss);
548 struct snd_pcm_runtime *rt = ss->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 int err;
550
551 h->csubs = ss;
552 rt->hw = snd_harmony_capture;
553 snd_pcm_hw_constraint_list(rt, 0, SNDRV_PCM_HW_PARAM_RATE,
554 &hw_constraint_rates);
555
556 err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
557 if (err < 0)
558 return err;
559
560 return 0;
561}
562
563static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100564snd_harmony_playback_close(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Takashi Iwai67b10202005-11-17 15:12:11 +0100566 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 h->psubs = NULL;
568 return 0;
569}
570
571static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100572snd_harmony_capture_close(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Takashi Iwai67b10202005-11-17 15:12:11 +0100574 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 h->csubs = NULL;
576 return 0;
577}
578
579static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100580snd_harmony_hw_params(struct snd_pcm_substream *ss,
581 struct snd_pcm_hw_params *hw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 int err;
Takashi Iwai67b10202005-11-17 15:12:11 +0100584 struct snd_harmony *h = snd_pcm_substream_chip(ss);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 err = snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw));
587 if (err > 0 && h->dma.type == SNDRV_DMA_TYPE_CONTINUOUS)
588 ss->runtime->dma_addr = __pa(ss->runtime->dma_area);
589
590 return err;
591}
592
593static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100594snd_harmony_hw_free(struct snd_pcm_substream *ss)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
596 return snd_pcm_lib_free_pages(ss);
597}
598
Takashi Iwai67b10202005-11-17 15:12:11 +0100599static struct snd_pcm_ops snd_harmony_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 .open = snd_harmony_playback_open,
601 .close = snd_harmony_playback_close,
602 .ioctl = snd_pcm_lib_ioctl,
603 .hw_params = snd_harmony_hw_params,
604 .hw_free = snd_harmony_hw_free,
605 .prepare = snd_harmony_playback_prepare,
606 .trigger = snd_harmony_playback_trigger,
607 .pointer = snd_harmony_playback_pointer,
608};
609
Takashi Iwai67b10202005-11-17 15:12:11 +0100610static struct snd_pcm_ops snd_harmony_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 .open = snd_harmony_capture_open,
612 .close = snd_harmony_capture_close,
613 .ioctl = snd_pcm_lib_ioctl,
614 .hw_params = snd_harmony_hw_params,
615 .hw_free = snd_harmony_hw_free,
616 .prepare = snd_harmony_capture_prepare,
617 .trigger = snd_harmony_capture_trigger,
618 .pointer = snd_harmony_capture_pointer,
619};
620
621static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100622snd_harmony_pcm_init(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Takashi Iwai67b10202005-11-17 15:12:11 +0100624 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 int err;
626
Julia Lawalle8e09292009-10-17 08:33:47 +0200627 if (snd_BUG_ON(!h))
628 return -EINVAL;
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 harmony_disable_interrupts(h);
631
632 err = snd_pcm_new(h->card, "harmony", 0, 1, 1, &pcm);
633 if (err < 0)
634 return err;
635
636 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
637 &snd_harmony_playback_ops);
638 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
639 &snd_harmony_capture_ops);
640
641 pcm->private_data = h;
642 pcm->info_flags = 0;
643 strcpy(pcm->name, "harmony");
644 h->pcm = pcm;
645
646 h->psubs = NULL;
647 h->csubs = NULL;
648
649 /* initialize graveyard buffer */
650 h->dma.type = SNDRV_DMA_TYPE_DEV;
651 h->dma.dev = &h->dev->dev;
652 err = snd_dma_alloc_pages(h->dma.type,
653 h->dma.dev,
654 BUF_SIZE*GRAVEYARD_BUFS,
655 &h->gdma);
656 if (err < 0) {
657 printk(KERN_ERR PFX "cannot allocate graveyard buffer!\n");
658 return err;
659 }
660
661 /* initialize silence buffers */
662 err = snd_dma_alloc_pages(h->dma.type,
663 h->dma.dev,
664 BUF_SIZE*SILENCE_BUFS,
665 &h->sdma);
666 if (err < 0) {
667 printk(KERN_ERR PFX "cannot allocate silence buffer!\n");
668 return err;
669 }
670
671 /* pre-allocate space for DMA */
672 err = snd_pcm_lib_preallocate_pages_for_all(pcm, h->dma.type,
673 h->dma.dev,
674 MAX_BUF_SIZE,
675 MAX_BUF_SIZE);
676 if (err < 0) {
677 printk(KERN_ERR PFX "buffer allocation error: %d\n", err);
678 return err;
679 }
680
681 h->st.format = snd_harmony_set_data_format(h,
682 SNDRV_PCM_FORMAT_S16_BE, 1);
683
684 return 0;
685}
686
687static void
Takashi Iwai67b10202005-11-17 15:12:11 +0100688snd_harmony_set_new_gain(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 harmony_wait_for_control(h);
691 harmony_write(h, HARMONY_GAINCTL, h->st.gain);
692}
693
694static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100695snd_harmony_mixercontrol_info(struct snd_kcontrol *kc,
696 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 int mask = (kc->private_value >> 16) & 0xff;
699 int left_shift = (kc->private_value) & 0xff;
700 int right_shift = (kc->private_value >> 8) & 0xff;
701
702 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN :
703 SNDRV_CTL_ELEM_TYPE_INTEGER;
704 uinfo->count = left_shift == right_shift ? 1 : 2;
705 uinfo->value.integer.min = 0;
706 uinfo->value.integer.max = mask;
707
708 return 0;
709}
710
711static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100712snd_harmony_volume_get(struct snd_kcontrol *kc,
713 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Takashi Iwai67b10202005-11-17 15:12:11 +0100715 struct snd_harmony *h = snd_kcontrol_chip(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 int shift_left = (kc->private_value) & 0xff;
717 int shift_right = (kc->private_value >> 8) & 0xff;
718 int mask = (kc->private_value >> 16) & 0xff;
719 int invert = (kc->private_value >> 24) & 0xff;
720 int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100722 spin_lock_irq(&h->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 left = (h->st.gain >> shift_left) & mask;
725 right = (h->st.gain >> shift_right) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (invert) {
727 left = mask - left;
728 right = mask - right;
729 }
Stuart Brady3a165682005-10-21 22:42:38 -0400730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ucontrol->value.integer.value[0] = left;
Stuart Brady3a165682005-10-21 22:42:38 -0400732 if (shift_left != shift_right)
733 ucontrol->value.integer.value[1] = right;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100735 spin_unlock_irq(&h->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 return 0;
738}
739
740static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100741snd_harmony_volume_put(struct snd_kcontrol *kc,
742 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Takashi Iwai67b10202005-11-17 15:12:11 +0100744 struct snd_harmony *h = snd_kcontrol_chip(kc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int shift_left = (kc->private_value) & 0xff;
746 int shift_right = (kc->private_value >> 8) & 0xff;
747 int mask = (kc->private_value >> 16) & 0xff;
748 int invert = (kc->private_value >> 24) & 0xff;
749 int left, right;
750 int old_gain = h->st.gain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100752 spin_lock_irq(&h->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Stuart Brady3a165682005-10-21 22:42:38 -0400754 left = ucontrol->value.integer.value[0] & mask;
755 if (invert)
756 left = mask - left;
757 h->st.gain &= ~( (mask << shift_left ) );
758 h->st.gain |= (left << shift_left);
759
760 if (shift_left != shift_right) {
761 right = ucontrol->value.integer.value[1] & mask;
762 if (invert)
763 right = mask - right;
764 h->st.gain &= ~( (mask << shift_right) );
765 h->st.gain |= (right << shift_right);
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 snd_harmony_set_new_gain(h);
769
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100770 spin_unlock_irq(&h->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Stuart Brady3a165682005-10-21 22:42:38 -0400772 return h->st.gain != old_gain;
773}
774
775static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100776snd_harmony_captureroute_info(struct snd_kcontrol *kc,
777 struct snd_ctl_elem_info *uinfo)
Stuart Brady3a165682005-10-21 22:42:38 -0400778{
779 static char *texts[2] = { "Line", "Mic" };
780 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
781 uinfo->count = 1;
782 uinfo->value.enumerated.items = 2;
783 if (uinfo->value.enumerated.item > 1)
784 uinfo->value.enumerated.item = 1;
785 strcpy(uinfo->value.enumerated.name,
786 texts[uinfo->value.enumerated.item]);
787 return 0;
788}
789
790static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100791snd_harmony_captureroute_get(struct snd_kcontrol *kc,
792 struct snd_ctl_elem_value *ucontrol)
Stuart Brady3a165682005-10-21 22:42:38 -0400793{
Takashi Iwai67b10202005-11-17 15:12:11 +0100794 struct snd_harmony *h = snd_kcontrol_chip(kc);
Stuart Brady3a165682005-10-21 22:42:38 -0400795 int value;
Stuart Brady3a165682005-10-21 22:42:38 -0400796
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100797 spin_lock_irq(&h->mixer_lock);
Stuart Brady3a165682005-10-21 22:42:38 -0400798
799 value = (h->st.gain >> HARMONY_GAIN_IS_SHIFT) & 1;
800 ucontrol->value.enumerated.item[0] = value;
801
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100802 spin_unlock_irq(&h->mixer_lock);
Stuart Brady3a165682005-10-21 22:42:38 -0400803
804 return 0;
805}
806
807static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100808snd_harmony_captureroute_put(struct snd_kcontrol *kc,
809 struct snd_ctl_elem_value *ucontrol)
Stuart Brady3a165682005-10-21 22:42:38 -0400810{
Takashi Iwai67b10202005-11-17 15:12:11 +0100811 struct snd_harmony *h = snd_kcontrol_chip(kc);
Stuart Brady3a165682005-10-21 22:42:38 -0400812 int value;
813 int old_gain = h->st.gain;
Stuart Brady3a165682005-10-21 22:42:38 -0400814
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100815 spin_lock_irq(&h->mixer_lock);
Stuart Brady3a165682005-10-21 22:42:38 -0400816
817 value = ucontrol->value.enumerated.item[0] & 1;
818 h->st.gain &= ~HARMONY_GAIN_IS_MASK;
819 h->st.gain |= value << HARMONY_GAIN_IS_SHIFT;
820
821 snd_harmony_set_new_gain(h);
822
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100823 spin_unlock_irq(&h->mixer_lock);
Stuart Brady3a165682005-10-21 22:42:38 -0400824
825 return h->st.gain != old_gain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Takashi Iwai67b10202005-11-17 15:12:11 +0100828#define HARMONY_CONTROLS ARRAY_SIZE(snd_harmony_controls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830#define HARMONY_VOLUME(xname, left_shift, right_shift, mask, invert) \
831{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
832 .info = snd_harmony_mixercontrol_info, \
833 .get = snd_harmony_volume_get, .put = snd_harmony_volume_put, \
834 .private_value = ((left_shift) | ((right_shift) << 8) | \
835 ((mask) << 16) | ((invert) << 24)) }
836
Takashi Iwai67b10202005-11-17 15:12:11 +0100837static struct snd_kcontrol_new snd_harmony_controls[] = {
Stuart Brady3a165682005-10-21 22:42:38 -0400838 HARMONY_VOLUME("Master Playback Volume", HARMONY_GAIN_LO_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 HARMONY_GAIN_RO_SHIFT, HARMONY_GAIN_OUT, 1),
840 HARMONY_VOLUME("Capture Volume", HARMONY_GAIN_LI_SHIFT,
841 HARMONY_GAIN_RI_SHIFT, HARMONY_GAIN_IN, 0),
Stuart Brady3a165682005-10-21 22:42:38 -0400842 HARMONY_VOLUME("Monitor Volume", HARMONY_GAIN_MA_SHIFT,
843 HARMONY_GAIN_MA_SHIFT, HARMONY_GAIN_MA, 1),
844 {
845 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
846 .name = "Input Route",
847 .info = snd_harmony_captureroute_info,
848 .get = snd_harmony_captureroute_get,
849 .put = snd_harmony_captureroute_put
850 },
851 HARMONY_VOLUME("Internal Speaker Switch", HARMONY_GAIN_SE_SHIFT,
852 HARMONY_GAIN_SE_SHIFT, 1, 0),
853 HARMONY_VOLUME("Line-Out Switch", HARMONY_GAIN_LE_SHIFT,
854 HARMONY_GAIN_LE_SHIFT, 1, 0),
855 HARMONY_VOLUME("Headphones Switch", HARMONY_GAIN_HE_SHIFT,
856 HARMONY_GAIN_HE_SHIFT, 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857};
858
Helge Deller992378a2007-01-28 17:06:12 +0100859static void __devinit
Takashi Iwai67b10202005-11-17 15:12:11 +0100860snd_harmony_mixer_reset(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 harmony_mute(h);
863 harmony_reset(h);
864 h->st.gain = HARMONY_GAIN_DEFAULT;
865 harmony_unmute(h);
866}
867
Helge Deller992378a2007-01-28 17:06:12 +0100868static int __devinit
Takashi Iwai67b10202005-11-17 15:12:11 +0100869snd_harmony_mixer_init(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Julia Lawalle8e09292009-10-17 08:33:47 +0200871 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 int idx, err;
873
Takashi Iwai5e246b82008-08-08 17:12:47 +0200874 if (snd_BUG_ON(!h))
Takashi Iwaic86a4562008-09-01 19:45:58 +0200875 return -EINVAL;
Julia Lawalle8e09292009-10-17 08:33:47 +0200876 card = h->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 strcpy(card->mixername, "Harmony Gain control interface");
878
879 for (idx = 0; idx < HARMONY_CONTROLS; idx++) {
880 err = snd_ctl_add(card,
881 snd_ctl_new1(&snd_harmony_controls[idx], h));
882 if (err < 0)
883 return err;
884 }
885
886 snd_harmony_mixer_reset(h);
887
888 return 0;
889}
890
891static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100892snd_harmony_free(struct snd_harmony *h)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 if (h->gdma.addr)
895 snd_dma_free_pages(&h->gdma);
896 if (h->sdma.addr)
897 snd_dma_free_pages(&h->sdma);
898
899 if (h->irq >= 0)
900 free_irq(h->irq, h);
901
902 if (h->iobase)
903 iounmap(h->iobase);
904
905 parisc_set_drvdata(h->dev, NULL);
906
907 kfree(h);
908 return 0;
909}
910
911static int
Takashi Iwai67b10202005-11-17 15:12:11 +0100912snd_harmony_dev_free(struct snd_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Takashi Iwai67b10202005-11-17 15:12:11 +0100914 struct snd_harmony *h = dev->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return snd_harmony_free(h);
916}
917
918static int __devinit
Takashi Iwai67b10202005-11-17 15:12:11 +0100919snd_harmony_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 struct parisc_device *padev,
Takashi Iwai67b10202005-11-17 15:12:11 +0100921 struct snd_harmony **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int err;
Takashi Iwai67b10202005-11-17 15:12:11 +0100924 struct snd_harmony *h;
925 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 .dev_free = snd_harmony_dev_free,
927 };
928
929 *rchip = NULL;
930
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100931 h = kzalloc(sizeof(*h), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (h == NULL)
933 return -ENOMEM;
934
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400935 h->hpa = padev->hpa.start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 h->card = card;
937 h->dev = padev;
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100938 h->irq = -1;
Matthew Wilcox53f01bb2005-10-21 22:36:40 -0400939 h->iobase = ioremap_nocache(padev->hpa.start, HARMONY_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (h->iobase == NULL) {
941 printk(KERN_ERR PFX "unable to remap hpa 0x%lx\n",
Alexander Beregalovb04b4f72009-05-15 18:00:38 +0400942 (unsigned long)padev->hpa.start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 err = -EBUSY;
944 goto free_and_ret;
945 }
946
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100947 err = request_irq(padev->irq, snd_harmony_interrupt, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 "harmony", h);
949 if (err) {
950 printk(KERN_ERR PFX "could not obtain interrupt %d",
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100951 padev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 goto free_and_ret;
953 }
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100954 h->irq = padev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 spin_lock_init(&h->mixer_lock);
957 spin_lock_init(&h->lock);
958
959 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL,
960 h, &ops)) < 0) {
961 goto free_and_ret;
962 }
963
Takashi Iwaia76af192005-09-05 16:56:40 +0200964 snd_card_set_dev(card, &padev->dev);
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *rchip = h;
967
968 return 0;
969
970free_and_ret:
971 snd_harmony_free(h);
972 return err;
973}
974
975static int __devinit
976snd_harmony_probe(struct parisc_device *padev)
977{
978 int err;
Takashi Iwai67b10202005-11-17 15:12:11 +0100979 struct snd_card *card;
980 struct snd_harmony *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Takashi Iwaibd7dd772008-12-28 16:45:02 +0100982 err = snd_card_create(index, id, THIS_MODULE, 0, &card);
983 if (err < 0)
984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 err = snd_harmony_create(card, padev, &h);
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100987 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 goto free_and_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 err = snd_harmony_pcm_init(h);
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100991 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto free_and_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 err = snd_harmony_mixer_init(h);
Takashi Iwai03f9ae22005-11-17 10:34:40 +0100995 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 goto free_and_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 strcpy(card->driver, "harmony");
999 strcpy(card->shortname, "Harmony");
1000 sprintf(card->longname, "%s at 0x%lx, irq %i",
1001 card->shortname, h->hpa, h->irq);
1002
1003 err = snd_card_register(card);
Takashi Iwai03f9ae22005-11-17 10:34:40 +01001004 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 goto free_and_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Takashi Iwai03f9ae22005-11-17 10:34:40 +01001007 parisc_set_drvdata(padev, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return 0;
1009
1010free_and_ret:
1011 snd_card_free(card);
1012 return err;
1013}
1014
1015static int __devexit
1016snd_harmony_remove(struct parisc_device *padev)
1017{
Takashi Iwai03f9ae22005-11-17 10:34:40 +01001018 snd_card_free(parisc_get_drvdata(padev));
1019 parisc_set_drvdata(padev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return 0;
1021}
1022
1023static struct parisc_driver snd_harmony_driver = {
1024 .name = "harmony",
1025 .id_table = snd_harmony_devtable,
1026 .probe = snd_harmony_probe,
Jean Delvare82ced6f2009-06-04 10:46:43 +02001027 .remove = __devexit_p(snd_harmony_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028};
1029
1030static int __init
1031alsa_harmony_init(void)
1032{
Takashi Iwai03f9ae22005-11-17 10:34:40 +01001033 return register_parisc_driver(&snd_harmony_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036static void __exit
1037alsa_harmony_fini(void)
1038{
Takashi Iwai67b10202005-11-17 15:12:11 +01001039 unregister_parisc_driver(&snd_harmony_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042MODULE_LICENSE("GPL");
1043MODULE_AUTHOR("Kyle McMartin <kyle@parisc-linux.org>");
1044MODULE_DESCRIPTION("Harmony sound driver");
1045
1046module_init(alsa_harmony_init);
1047module_exit(alsa_harmony_fini);