2 ad1816a.c - lowlevel code for Analog Devices AD1816A chip.
3 Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <sound/driver.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 #include <linux/ioport.h>
26 #include <sound/core.h>
27 #include <sound/tlv.h>
28 #include <sound/ad1816a.h>
33 static inline int snd_ad1816a_busy_wait(struct snd_ad1816a
*chip
)
37 for (timeout
= 1000; timeout
-- > 0; udelay(10))
38 if (inb(AD1816A_REG(AD1816A_CHIP_STATUS
)) & AD1816A_READY
)
41 snd_printk("chip busy.\n");
45 static inline unsigned char snd_ad1816a_in(struct snd_ad1816a
*chip
, unsigned char reg
)
47 snd_ad1816a_busy_wait(chip
);
48 return inb(AD1816A_REG(reg
));
51 static inline void snd_ad1816a_out(struct snd_ad1816a
*chip
, unsigned char reg
,
54 snd_ad1816a_busy_wait(chip
);
55 outb(value
, AD1816A_REG(reg
));
58 static inline void snd_ad1816a_out_mask(struct snd_ad1816a
*chip
, unsigned char reg
,
59 unsigned char mask
, unsigned char value
)
61 snd_ad1816a_out(chip
, reg
,
62 (value
& mask
) | (snd_ad1816a_in(chip
, reg
) & ~mask
));
65 static unsigned short snd_ad1816a_read(struct snd_ad1816a
*chip
, unsigned char reg
)
67 snd_ad1816a_out(chip
, AD1816A_INDIR_ADDR
, reg
& 0x3f);
68 return snd_ad1816a_in(chip
, AD1816A_INDIR_DATA_LOW
) |
69 (snd_ad1816a_in(chip
, AD1816A_INDIR_DATA_HIGH
) << 8);
72 static void snd_ad1816a_write(struct snd_ad1816a
*chip
, unsigned char reg
,
75 snd_ad1816a_out(chip
, AD1816A_INDIR_ADDR
, reg
& 0x3f);
76 snd_ad1816a_out(chip
, AD1816A_INDIR_DATA_LOW
, value
& 0xff);
77 snd_ad1816a_out(chip
, AD1816A_INDIR_DATA_HIGH
, (value
>> 8) & 0xff);
80 static void snd_ad1816a_write_mask(struct snd_ad1816a
*chip
, unsigned char reg
,
81 unsigned short mask
, unsigned short value
)
83 snd_ad1816a_write(chip
, reg
,
84 (value
& mask
) | (snd_ad1816a_read(chip
, reg
) & ~mask
));
88 static unsigned char snd_ad1816a_get_format(struct snd_ad1816a
*chip
,
89 unsigned int format
, int channels
)
91 unsigned char retval
= AD1816A_FMT_LINEAR_8
;
94 case SNDRV_PCM_FORMAT_MU_LAW
:
95 retval
= AD1816A_FMT_ULAW_8
;
97 case SNDRV_PCM_FORMAT_A_LAW
:
98 retval
= AD1816A_FMT_ALAW_8
;
100 case SNDRV_PCM_FORMAT_S16_LE
:
101 retval
= AD1816A_FMT_LINEAR_16_LIT
;
103 case SNDRV_PCM_FORMAT_S16_BE
:
104 retval
= AD1816A_FMT_LINEAR_16_BIG
;
106 return (channels
> 1) ? (retval
| AD1816A_FMT_STEREO
) : retval
;
109 static int snd_ad1816a_open(struct snd_ad1816a
*chip
, unsigned int mode
)
113 spin_lock_irqsave(&chip
->lock
, flags
);
115 if (chip
->mode
& mode
) {
116 spin_unlock_irqrestore(&chip
->lock
, flags
);
120 switch ((mode
&= AD1816A_MODE_OPEN
)) {
121 case AD1816A_MODE_PLAYBACK
:
122 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
123 AD1816A_PLAYBACK_IRQ_PENDING
, 0x00);
124 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
125 AD1816A_PLAYBACK_IRQ_ENABLE
, 0xffff);
127 case AD1816A_MODE_CAPTURE
:
128 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
129 AD1816A_CAPTURE_IRQ_PENDING
, 0x00);
130 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
131 AD1816A_CAPTURE_IRQ_ENABLE
, 0xffff);
133 case AD1816A_MODE_TIMER
:
134 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
135 AD1816A_TIMER_IRQ_PENDING
, 0x00);
136 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
137 AD1816A_TIMER_IRQ_ENABLE
, 0xffff);
141 spin_unlock_irqrestore(&chip
->lock
, flags
);
145 static void snd_ad1816a_close(struct snd_ad1816a
*chip
, unsigned int mode
)
149 spin_lock_irqsave(&chip
->lock
, flags
);
151 switch ((mode
&= AD1816A_MODE_OPEN
)) {
152 case AD1816A_MODE_PLAYBACK
:
153 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
154 AD1816A_PLAYBACK_IRQ_PENDING
, 0x00);
155 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
156 AD1816A_PLAYBACK_IRQ_ENABLE
, 0x0000);
158 case AD1816A_MODE_CAPTURE
:
159 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
160 AD1816A_CAPTURE_IRQ_PENDING
, 0x00);
161 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
162 AD1816A_CAPTURE_IRQ_ENABLE
, 0x0000);
164 case AD1816A_MODE_TIMER
:
165 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
166 AD1816A_TIMER_IRQ_PENDING
, 0x00);
167 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
168 AD1816A_TIMER_IRQ_ENABLE
, 0x0000);
170 if (!((chip
->mode
&= ~mode
) & AD1816A_MODE_OPEN
))
173 spin_unlock_irqrestore(&chip
->lock
, flags
);
177 static int snd_ad1816a_trigger(struct snd_ad1816a
*chip
, unsigned char what
,
178 int channel
, int cmd
, int iscapture
)
183 case SNDRV_PCM_TRIGGER_START
:
184 case SNDRV_PCM_TRIGGER_STOP
:
185 spin_lock(&chip
->lock
);
186 cmd
= (cmd
== SNDRV_PCM_TRIGGER_START
) ? 0xff: 0x00;
187 /* if (what & AD1816A_PLAYBACK_ENABLE) */
188 /* That is not valid, because playback and capture enable
189 * are the same bit pattern, just to different addresses
192 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
193 AD1816A_PLAYBACK_ENABLE
, cmd
);
195 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
196 AD1816A_CAPTURE_ENABLE
, cmd
);
197 spin_unlock(&chip
->lock
);
200 snd_printk("invalid trigger mode 0x%x.\n", what
);
207 static int snd_ad1816a_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
209 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
210 return snd_ad1816a_trigger(chip
, AD1816A_PLAYBACK_ENABLE
,
211 SNDRV_PCM_STREAM_PLAYBACK
, cmd
, 0);
214 static int snd_ad1816a_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
216 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
217 return snd_ad1816a_trigger(chip
, AD1816A_CAPTURE_ENABLE
,
218 SNDRV_PCM_STREAM_CAPTURE
, cmd
, 1);
221 static int snd_ad1816a_hw_params(struct snd_pcm_substream
*substream
,
222 struct snd_pcm_hw_params
*hw_params
)
224 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
227 static int snd_ad1816a_hw_free(struct snd_pcm_substream
*substream
)
229 return snd_pcm_lib_free_pages(substream
);
232 static int snd_ad1816a_playback_prepare(struct snd_pcm_substream
*substream
)
234 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
236 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
237 unsigned int size
, rate
;
239 spin_lock_irqsave(&chip
->lock
, flags
);
241 chip
->p_dma_size
= size
= snd_pcm_lib_buffer_bytes(substream
);
242 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
243 AD1816A_PLAYBACK_ENABLE
| AD1816A_PLAYBACK_PIO
, 0x00);
245 snd_dma_program(chip
->dma1
, runtime
->dma_addr
, size
,
246 DMA_MODE_WRITE
| DMA_AUTOINIT
);
248 rate
= runtime
->rate
;
249 if (chip
->clock_freq
)
250 rate
= (rate
* 33000) / chip
->clock_freq
;
251 snd_ad1816a_write(chip
, AD1816A_PLAYBACK_SAMPLE_RATE
, rate
);
252 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
253 AD1816A_FMT_ALL
| AD1816A_FMT_STEREO
,
254 snd_ad1816a_get_format(chip
, runtime
->format
,
257 snd_ad1816a_write(chip
, AD1816A_PLAYBACK_BASE_COUNT
,
258 snd_pcm_lib_period_bytes(substream
) / 4 - 1);
260 spin_unlock_irqrestore(&chip
->lock
, flags
);
264 static int snd_ad1816a_capture_prepare(struct snd_pcm_substream
*substream
)
266 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
268 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
269 unsigned int size
, rate
;
271 spin_lock_irqsave(&chip
->lock
, flags
);
273 chip
->c_dma_size
= size
= snd_pcm_lib_buffer_bytes(substream
);
274 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
275 AD1816A_CAPTURE_ENABLE
| AD1816A_CAPTURE_PIO
, 0x00);
277 snd_dma_program(chip
->dma2
, runtime
->dma_addr
, size
,
278 DMA_MODE_READ
| DMA_AUTOINIT
);
280 rate
= runtime
->rate
;
281 if (chip
->clock_freq
)
282 rate
= (rate
* 33000) / chip
->clock_freq
;
283 snd_ad1816a_write(chip
, AD1816A_CAPTURE_SAMPLE_RATE
, rate
);
284 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
285 AD1816A_FMT_ALL
| AD1816A_FMT_STEREO
,
286 snd_ad1816a_get_format(chip
, runtime
->format
,
289 snd_ad1816a_write(chip
, AD1816A_CAPTURE_BASE_COUNT
,
290 snd_pcm_lib_period_bytes(substream
) / 4 - 1);
292 spin_unlock_irqrestore(&chip
->lock
, flags
);
297 static snd_pcm_uframes_t
snd_ad1816a_playback_pointer(struct snd_pcm_substream
*substream
)
299 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
301 if (!(chip
->mode
& AD1816A_MODE_PLAYBACK
))
303 ptr
= snd_dma_pointer(chip
->dma1
, chip
->p_dma_size
);
304 return bytes_to_frames(substream
->runtime
, ptr
);
307 static snd_pcm_uframes_t
snd_ad1816a_capture_pointer(struct snd_pcm_substream
*substream
)
309 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
311 if (!(chip
->mode
& AD1816A_MODE_CAPTURE
))
313 ptr
= snd_dma_pointer(chip
->dma2
, chip
->c_dma_size
);
314 return bytes_to_frames(substream
->runtime
, ptr
);
318 static irqreturn_t
snd_ad1816a_interrupt(int irq
, void *dev_id
)
320 struct snd_ad1816a
*chip
= dev_id
;
321 unsigned char status
;
323 spin_lock(&chip
->lock
);
324 status
= snd_ad1816a_in(chip
, AD1816A_INTERRUPT_STATUS
);
325 spin_unlock(&chip
->lock
);
327 if ((status
& AD1816A_PLAYBACK_IRQ_PENDING
) && chip
->playback_substream
)
328 snd_pcm_period_elapsed(chip
->playback_substream
);
330 if ((status
& AD1816A_CAPTURE_IRQ_PENDING
) && chip
->capture_substream
)
331 snd_pcm_period_elapsed(chip
->capture_substream
);
333 if ((status
& AD1816A_TIMER_IRQ_PENDING
) && chip
->timer
)
334 snd_timer_interrupt(chip
->timer
, chip
->timer
->sticks
);
336 spin_lock(&chip
->lock
);
337 snd_ad1816a_out(chip
, AD1816A_INTERRUPT_STATUS
, 0x00);
338 spin_unlock(&chip
->lock
);
343 static struct snd_pcm_hardware snd_ad1816a_playback
= {
344 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
345 SNDRV_PCM_INFO_MMAP_VALID
),
346 .formats
= (SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
|
347 SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
348 SNDRV_PCM_FMTBIT_S16_BE
),
349 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
354 .buffer_bytes_max
= (128*1024),
355 .period_bytes_min
= 64,
356 .period_bytes_max
= (128*1024),
362 static struct snd_pcm_hardware snd_ad1816a_capture
= {
363 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
364 SNDRV_PCM_INFO_MMAP_VALID
),
365 .formats
= (SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
|
366 SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
367 SNDRV_PCM_FMTBIT_S16_BE
),
368 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
373 .buffer_bytes_max
= (128*1024),
374 .period_bytes_min
= 64,
375 .period_bytes_max
= (128*1024),
381 #if 0 /* not used now */
382 static int snd_ad1816a_timer_close(struct snd_timer
*timer
)
384 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
385 snd_ad1816a_close(chip
, AD1816A_MODE_TIMER
);
389 static int snd_ad1816a_timer_open(struct snd_timer
*timer
)
391 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
392 snd_ad1816a_open(chip
, AD1816A_MODE_TIMER
);
396 static unsigned long snd_ad1816a_timer_resolution(struct snd_timer
*timer
)
398 snd_assert(timer
!= NULL
, return 0);
403 static int snd_ad1816a_timer_start(struct snd_timer
*timer
)
407 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
408 spin_lock_irqsave(&chip
->lock
, flags
);
409 bits
= snd_ad1816a_read(chip
, AD1816A_INTERRUPT_ENABLE
);
411 if (!(bits
& AD1816A_TIMER_ENABLE
)) {
412 snd_ad1816a_write(chip
, AD1816A_TIMER_BASE_COUNT
,
413 timer
->sticks
& 0xffff);
415 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
416 AD1816A_TIMER_ENABLE
, 0xffff);
418 spin_unlock_irqrestore(&chip
->lock
, flags
);
422 static int snd_ad1816a_timer_stop(struct snd_timer
*timer
)
425 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
426 spin_lock_irqsave(&chip
->lock
, flags
);
428 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
429 AD1816A_TIMER_ENABLE
, 0x0000);
431 spin_unlock_irqrestore(&chip
->lock
, flags
);
435 static struct snd_timer_hardware snd_ad1816a_timer_table
= {
436 .flags
= SNDRV_TIMER_HW_AUTO
,
439 .open
= snd_ad1816a_timer_open
,
440 .close
= snd_ad1816a_timer_close
,
441 .c_resolution
= snd_ad1816a_timer_resolution
,
442 .start
= snd_ad1816a_timer_start
,
443 .stop
= snd_ad1816a_timer_stop
,
445 #endif /* not used now */
448 static int snd_ad1816a_playback_open(struct snd_pcm_substream
*substream
)
450 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
451 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
454 if ((error
= snd_ad1816a_open(chip
, AD1816A_MODE_PLAYBACK
)) < 0)
456 snd_pcm_set_sync(substream
);
457 runtime
->hw
= snd_ad1816a_playback
;
458 snd_pcm_limit_isa_dma_size(chip
->dma1
, &runtime
->hw
.buffer_bytes_max
);
459 snd_pcm_limit_isa_dma_size(chip
->dma1
, &runtime
->hw
.period_bytes_max
);
460 chip
->playback_substream
= substream
;
464 static int snd_ad1816a_capture_open(struct snd_pcm_substream
*substream
)
466 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
467 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
470 if ((error
= snd_ad1816a_open(chip
, AD1816A_MODE_CAPTURE
)) < 0)
472 snd_pcm_set_sync(substream
);
473 runtime
->hw
= snd_ad1816a_capture
;
474 snd_pcm_limit_isa_dma_size(chip
->dma2
, &runtime
->hw
.buffer_bytes_max
);
475 snd_pcm_limit_isa_dma_size(chip
->dma2
, &runtime
->hw
.period_bytes_max
);
476 chip
->capture_substream
= substream
;
480 static int snd_ad1816a_playback_close(struct snd_pcm_substream
*substream
)
482 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
484 chip
->playback_substream
= NULL
;
485 snd_ad1816a_close(chip
, AD1816A_MODE_PLAYBACK
);
489 static int snd_ad1816a_capture_close(struct snd_pcm_substream
*substream
)
491 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
493 chip
->capture_substream
= NULL
;
494 snd_ad1816a_close(chip
, AD1816A_MODE_CAPTURE
);
499 static void __devinit
snd_ad1816a_init(struct snd_ad1816a
*chip
)
503 spin_lock_irqsave(&chip
->lock
, flags
);
505 snd_ad1816a_out(chip
, AD1816A_INTERRUPT_STATUS
, 0x00);
506 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
507 AD1816A_PLAYBACK_ENABLE
| AD1816A_PLAYBACK_PIO
, 0x00);
508 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
509 AD1816A_CAPTURE_ENABLE
| AD1816A_CAPTURE_PIO
, 0x00);
510 snd_ad1816a_write(chip
, AD1816A_INTERRUPT_ENABLE
, 0x0000);
511 snd_ad1816a_write_mask(chip
, AD1816A_CHIP_CONFIG
,
512 AD1816A_CAPTURE_NOT_EQUAL
| AD1816A_WSS_ENABLE
, 0xffff);
513 snd_ad1816a_write(chip
, AD1816A_DSP_CONFIG
, 0x0000);
514 snd_ad1816a_write(chip
, AD1816A_POWERDOWN_CTRL
, 0x0000);
516 spin_unlock_irqrestore(&chip
->lock
, flags
);
519 static int __devinit
snd_ad1816a_probe(struct snd_ad1816a
*chip
)
523 spin_lock_irqsave(&chip
->lock
, flags
);
525 switch (chip
->version
= snd_ad1816a_read(chip
, AD1816A_VERSION_ID
)) {
527 chip
->hardware
= AD1816A_HW_AD1815
;
530 chip
->hardware
= AD1816A_HW_AD18MAX10
;
533 chip
->hardware
= AD1816A_HW_AD1816A
;
536 chip
->hardware
= AD1816A_HW_AUTO
;
539 spin_unlock_irqrestore(&chip
->lock
, flags
);
543 static int snd_ad1816a_free(struct snd_ad1816a
*chip
)
545 release_and_free_resource(chip
->res_port
);
547 free_irq(chip
->irq
, (void *) chip
);
548 if (chip
->dma1
>= 0) {
549 snd_dma_disable(chip
->dma1
);
550 free_dma(chip
->dma1
);
552 if (chip
->dma2
>= 0) {
553 snd_dma_disable(chip
->dma2
);
554 free_dma(chip
->dma2
);
560 static int snd_ad1816a_dev_free(struct snd_device
*device
)
562 struct snd_ad1816a
*chip
= device
->device_data
;
563 return snd_ad1816a_free(chip
);
566 static const char __devinit
*snd_ad1816a_chip_id(struct snd_ad1816a
*chip
)
568 switch (chip
->hardware
) {
569 case AD1816A_HW_AD1816A
: return "AD1816A";
570 case AD1816A_HW_AD1815
: return "AD1815";
571 case AD1816A_HW_AD18MAX10
: return "AD18max10";
573 snd_printk("Unknown chip version %d:%d.\n",
574 chip
->version
, chip
->hardware
);
575 return "AD1816A - unknown";
579 int __devinit
snd_ad1816a_create(struct snd_card
*card
,
580 unsigned long port
, int irq
, int dma1
, int dma2
,
581 struct snd_ad1816a
**rchip
)
583 static struct snd_device_ops ops
= {
584 .dev_free
= snd_ad1816a_dev_free
,
587 struct snd_ad1816a
*chip
;
591 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
598 if ((chip
->res_port
= request_region(port
, 16, "AD1816A")) == NULL
) {
599 snd_printk(KERN_ERR
"ad1816a: can't grab port 0x%lx\n", port
);
600 snd_ad1816a_free(chip
);
603 if (request_irq(irq
, snd_ad1816a_interrupt
, IRQF_DISABLED
, "AD1816A", (void *) chip
)) {
604 snd_printk(KERN_ERR
"ad1816a: can't grab IRQ %d\n", irq
);
605 snd_ad1816a_free(chip
);
609 if (request_dma(dma1
, "AD1816A - 1")) {
610 snd_printk(KERN_ERR
"ad1816a: can't grab DMA1 %d\n", dma1
);
611 snd_ad1816a_free(chip
);
615 if (request_dma(dma2
, "AD1816A - 2")) {
616 snd_printk(KERN_ERR
"ad1816a: can't grab DMA2 %d\n", dma2
);
617 snd_ad1816a_free(chip
);
624 spin_lock_init(&chip
->lock
);
626 if ((error
= snd_ad1816a_probe(chip
))) {
627 snd_ad1816a_free(chip
);
631 snd_ad1816a_init(chip
);
633 /* Register device */
634 if ((error
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
)) < 0) {
635 snd_ad1816a_free(chip
);
643 static struct snd_pcm_ops snd_ad1816a_playback_ops
= {
644 .open
= snd_ad1816a_playback_open
,
645 .close
= snd_ad1816a_playback_close
,
646 .ioctl
= snd_pcm_lib_ioctl
,
647 .hw_params
= snd_ad1816a_hw_params
,
648 .hw_free
= snd_ad1816a_hw_free
,
649 .prepare
= snd_ad1816a_playback_prepare
,
650 .trigger
= snd_ad1816a_playback_trigger
,
651 .pointer
= snd_ad1816a_playback_pointer
,
654 static struct snd_pcm_ops snd_ad1816a_capture_ops
= {
655 .open
= snd_ad1816a_capture_open
,
656 .close
= snd_ad1816a_capture_close
,
657 .ioctl
= snd_pcm_lib_ioctl
,
658 .hw_params
= snd_ad1816a_hw_params
,
659 .hw_free
= snd_ad1816a_hw_free
,
660 .prepare
= snd_ad1816a_capture_prepare
,
661 .trigger
= snd_ad1816a_capture_trigger
,
662 .pointer
= snd_ad1816a_capture_pointer
,
665 int __devinit
snd_ad1816a_pcm(struct snd_ad1816a
*chip
, int device
, struct snd_pcm
**rpcm
)
670 if ((error
= snd_pcm_new(chip
->card
, "AD1816A", device
, 1, 1, &pcm
)))
673 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ad1816a_playback_ops
);
674 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ad1816a_capture_ops
);
676 pcm
->private_data
= chip
;
677 pcm
->info_flags
= (chip
->dma1
== chip
->dma2
) ? SNDRV_PCM_INFO_JOINT_DUPLEX
: 0;
679 strcpy(pcm
->name
, snd_ad1816a_chip_id(chip
));
680 snd_ad1816a_init(chip
);
682 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
684 64*1024, chip
->dma1
> 3 || chip
->dma2
> 3 ? 128*1024 : 64*1024);
692 #if 0 /* not used now */
693 int __devinit
snd_ad1816a_timer(struct snd_ad1816a
*chip
, int device
, struct snd_timer
**rtimer
)
695 struct snd_timer
*timer
;
696 struct snd_timer_id tid
;
699 tid
.dev_class
= SNDRV_TIMER_CLASS_CARD
;
700 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
701 tid
.card
= chip
->card
->number
;
704 if ((error
= snd_timer_new(chip
->card
, "AD1816A", &tid
, &timer
)) < 0)
706 strcpy(timer
->name
, snd_ad1816a_chip_id(chip
));
707 timer
->private_data
= chip
;
709 timer
->hw
= snd_ad1816a_timer_table
;
714 #endif /* not used now */
720 static int snd_ad1816a_info_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
722 static char *texts
[8] = {
723 "Line", "Mix", "CD", "Synth", "Video",
727 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
729 uinfo
->value
.enumerated
.items
= 7;
730 if (uinfo
->value
.enumerated
.item
> 6)
731 uinfo
->value
.enumerated
.item
= 6;
732 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
736 static int snd_ad1816a_get_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
738 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
742 spin_lock_irqsave(&chip
->lock
, flags
);
743 val
= snd_ad1816a_read(chip
, AD1816A_ADC_SOURCE_SEL
);
744 spin_unlock_irqrestore(&chip
->lock
, flags
);
745 ucontrol
->value
.enumerated
.item
[0] = (val
>> 12) & 7;
746 ucontrol
->value
.enumerated
.item
[1] = (val
>> 4) & 7;
750 static int snd_ad1816a_put_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
752 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
757 if (ucontrol
->value
.enumerated
.item
[0] > 6 ||
758 ucontrol
->value
.enumerated
.item
[1] > 6)
760 val
= (ucontrol
->value
.enumerated
.item
[0] << 12) |
761 (ucontrol
->value
.enumerated
.item
[1] << 4);
762 spin_lock_irqsave(&chip
->lock
, flags
);
763 change
= snd_ad1816a_read(chip
, AD1816A_ADC_SOURCE_SEL
) != val
;
764 snd_ad1816a_write(chip
, AD1816A_ADC_SOURCE_SEL
, val
);
765 spin_unlock_irqrestore(&chip
->lock
, flags
);
769 #define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv) \
770 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
771 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
772 .name = xname, .info = snd_ad1816a_info_single, \
773 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
774 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
775 .tlv = { .p = (xtlv) } }
776 #define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
777 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
778 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
779 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
781 static int snd_ad1816a_info_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
783 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
785 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
787 uinfo
->value
.integer
.min
= 0;
788 uinfo
->value
.integer
.max
= mask
;
792 static int snd_ad1816a_get_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
794 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
796 int reg
= kcontrol
->private_value
& 0xff;
797 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
798 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
799 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
801 spin_lock_irqsave(&chip
->lock
, flags
);
802 ucontrol
->value
.integer
.value
[0] = (snd_ad1816a_read(chip
, reg
) >> shift
) & mask
;
803 spin_unlock_irqrestore(&chip
->lock
, flags
);
805 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
809 static int snd_ad1816a_put_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
811 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
813 int reg
= kcontrol
->private_value
& 0xff;
814 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
815 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
816 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
818 unsigned short old_val
, val
;
820 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
824 spin_lock_irqsave(&chip
->lock
, flags
);
825 old_val
= snd_ad1816a_read(chip
, reg
);
826 val
= (old_val
& ~(mask
<< shift
)) | val
;
827 change
= val
!= old_val
;
828 snd_ad1816a_write(chip
, reg
, val
);
829 spin_unlock_irqrestore(&chip
->lock
, flags
);
833 #define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
834 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
835 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
836 .name = xname, .info = snd_ad1816a_info_double, \
837 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
838 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
839 .tlv = { .p = (xtlv) } }
841 #define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
842 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
843 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
844 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
846 static int snd_ad1816a_info_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
848 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
850 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
852 uinfo
->value
.integer
.min
= 0;
853 uinfo
->value
.integer
.max
= mask
;
857 static int snd_ad1816a_get_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
859 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
861 int reg
= kcontrol
->private_value
& 0xff;
862 int shift_left
= (kcontrol
->private_value
>> 8) & 0x0f;
863 int shift_right
= (kcontrol
->private_value
>> 12) & 0x0f;
864 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
865 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
868 spin_lock_irqsave(&chip
->lock
, flags
);
869 val
= snd_ad1816a_read(chip
, reg
);
870 ucontrol
->value
.integer
.value
[0] = (val
>> shift_left
) & mask
;
871 ucontrol
->value
.integer
.value
[1] = (val
>> shift_right
) & mask
;
872 spin_unlock_irqrestore(&chip
->lock
, flags
);
874 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
875 ucontrol
->value
.integer
.value
[1] = mask
- ucontrol
->value
.integer
.value
[1];
880 static int snd_ad1816a_put_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
882 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
884 int reg
= kcontrol
->private_value
& 0xff;
885 int shift_left
= (kcontrol
->private_value
>> 8) & 0x0f;
886 int shift_right
= (kcontrol
->private_value
>> 12) & 0x0f;
887 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
888 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
890 unsigned short old_val
, val1
, val2
;
892 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
893 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
899 val2
<<= shift_right
;
900 spin_lock_irqsave(&chip
->lock
, flags
);
901 old_val
= snd_ad1816a_read(chip
, reg
);
902 val1
= (old_val
& ~((mask
<< shift_left
) | (mask
<< shift_right
))) | val1
| val2
;
903 change
= val1
!= old_val
;
904 snd_ad1816a_write(chip
, reg
, val1
);
905 spin_unlock_irqrestore(&chip
->lock
, flags
);
909 static DECLARE_TLV_DB_SCALE(db_scale_4bit
, -4500, 300, 0);
910 static DECLARE_TLV_DB_SCALE(db_scale_5bit
, -4650, 150, 0);
911 static DECLARE_TLV_DB_SCALE(db_scale_6bit
, -9450, 150, 0);
912 static DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max
, -3450, 150, 0);
913 static DECLARE_TLV_DB_SCALE(db_scale_rec_gain
, 0, 150, 0);
915 static struct snd_kcontrol_new snd_ad1816a_controls
[] __devinitdata
= {
916 AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT
, 15, 7, 1, 1),
917 AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT
, 8, 0, 31, 1,
919 AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT
, 15, 7, 1, 1),
920 AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT
, 8, 0, 63, 1,
922 AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT
, 15, 7, 1, 1),
923 AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT
, 8, 0, 31, 1,
924 db_scale_5bit_12db_max
),
925 AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT
, 15, 7, 1, 1),
926 AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT
, 8, 0, 31, 1,
927 db_scale_5bit_12db_max
),
928 AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT
, 15, 7, 1, 1),
929 AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT
, 8, 0, 31, 1,
930 db_scale_5bit_12db_max
),
931 AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT
, 15, 7, 1, 1),
932 AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT
, 8, 0, 63, 1,
934 AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT
, 15, 1, 1),
935 AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT
, 8, 31, 1,
936 db_scale_5bit_12db_max
),
937 AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT
, 14, 1, 0),
938 AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT
, 15, 7, 1, 1),
939 AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT
, 8, 0, 31, 1,
940 db_scale_5bit_12db_max
),
941 AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT
, 15, 1, 1),
942 AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT
, 0, 15, 1,
944 AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT
, 7, 1, 1),
945 AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT
, 0, 31, 1,
948 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
949 .name
= "Capture Source",
950 .info
= snd_ad1816a_info_mux
,
951 .get
= snd_ad1816a_get_mux
,
952 .put
= snd_ad1816a_put_mux
,
954 AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA
, 15, 7, 1, 1),
955 AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA
, 8, 0, 15, 0,
957 AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL
, 15, 1, 1),
958 AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL
, 0, 15, 0),
961 int __devinit
snd_ad1816a_mixer(struct snd_ad1816a
*chip
)
963 struct snd_card
*card
;
967 snd_assert(chip
!= NULL
&& chip
->card
!= NULL
, return -EINVAL
);
971 strcpy(card
->mixername
, snd_ad1816a_chip_id(chip
));
973 for (idx
= 0; idx
< ARRAY_SIZE(snd_ad1816a_controls
); idx
++) {
974 if ((err
= snd_ctl_add(card
, snd_ctl_new1(&snd_ad1816a_controls
[idx
], chip
))) < 0)