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 <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/slab.h>
24 #include <linux/ioport.h>
25 #include <sound/core.h>
26 #include <sound/tlv.h>
27 #include <sound/ad1816a.h>
32 static inline int snd_ad1816a_busy_wait(struct snd_ad1816a
*chip
)
36 for (timeout
= 1000; timeout
-- > 0; udelay(10))
37 if (inb(AD1816A_REG(AD1816A_CHIP_STATUS
)) & AD1816A_READY
)
40 snd_printk(KERN_WARNING
"chip busy.\n");
44 static inline unsigned char snd_ad1816a_in(struct snd_ad1816a
*chip
, unsigned char reg
)
46 snd_ad1816a_busy_wait(chip
);
47 return inb(AD1816A_REG(reg
));
50 static inline void snd_ad1816a_out(struct snd_ad1816a
*chip
, unsigned char reg
,
53 snd_ad1816a_busy_wait(chip
);
54 outb(value
, AD1816A_REG(reg
));
57 static inline void snd_ad1816a_out_mask(struct snd_ad1816a
*chip
, unsigned char reg
,
58 unsigned char mask
, unsigned char value
)
60 snd_ad1816a_out(chip
, reg
,
61 (value
& mask
) | (snd_ad1816a_in(chip
, reg
) & ~mask
));
64 static unsigned short snd_ad1816a_read(struct snd_ad1816a
*chip
, unsigned char reg
)
66 snd_ad1816a_out(chip
, AD1816A_INDIR_ADDR
, reg
& 0x3f);
67 return snd_ad1816a_in(chip
, AD1816A_INDIR_DATA_LOW
) |
68 (snd_ad1816a_in(chip
, AD1816A_INDIR_DATA_HIGH
) << 8);
71 static void snd_ad1816a_write(struct snd_ad1816a
*chip
, unsigned char reg
,
74 snd_ad1816a_out(chip
, AD1816A_INDIR_ADDR
, reg
& 0x3f);
75 snd_ad1816a_out(chip
, AD1816A_INDIR_DATA_LOW
, value
& 0xff);
76 snd_ad1816a_out(chip
, AD1816A_INDIR_DATA_HIGH
, (value
>> 8) & 0xff);
79 static void snd_ad1816a_write_mask(struct snd_ad1816a
*chip
, unsigned char reg
,
80 unsigned short mask
, unsigned short value
)
82 snd_ad1816a_write(chip
, reg
,
83 (value
& mask
) | (snd_ad1816a_read(chip
, reg
) & ~mask
));
87 static unsigned char snd_ad1816a_get_format(struct snd_ad1816a
*chip
,
88 unsigned int format
, int channels
)
90 unsigned char retval
= AD1816A_FMT_LINEAR_8
;
93 case SNDRV_PCM_FORMAT_MU_LAW
:
94 retval
= AD1816A_FMT_ULAW_8
;
96 case SNDRV_PCM_FORMAT_A_LAW
:
97 retval
= AD1816A_FMT_ALAW_8
;
99 case SNDRV_PCM_FORMAT_S16_LE
:
100 retval
= AD1816A_FMT_LINEAR_16_LIT
;
102 case SNDRV_PCM_FORMAT_S16_BE
:
103 retval
= AD1816A_FMT_LINEAR_16_BIG
;
105 return (channels
> 1) ? (retval
| AD1816A_FMT_STEREO
) : retval
;
108 static int snd_ad1816a_open(struct snd_ad1816a
*chip
, unsigned int mode
)
112 spin_lock_irqsave(&chip
->lock
, flags
);
114 if (chip
->mode
& mode
) {
115 spin_unlock_irqrestore(&chip
->lock
, flags
);
119 switch ((mode
&= AD1816A_MODE_OPEN
)) {
120 case AD1816A_MODE_PLAYBACK
:
121 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
122 AD1816A_PLAYBACK_IRQ_PENDING
, 0x00);
123 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
124 AD1816A_PLAYBACK_IRQ_ENABLE
, 0xffff);
126 case AD1816A_MODE_CAPTURE
:
127 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
128 AD1816A_CAPTURE_IRQ_PENDING
, 0x00);
129 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
130 AD1816A_CAPTURE_IRQ_ENABLE
, 0xffff);
132 case AD1816A_MODE_TIMER
:
133 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
134 AD1816A_TIMER_IRQ_PENDING
, 0x00);
135 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
136 AD1816A_TIMER_IRQ_ENABLE
, 0xffff);
140 spin_unlock_irqrestore(&chip
->lock
, flags
);
144 static void snd_ad1816a_close(struct snd_ad1816a
*chip
, unsigned int mode
)
148 spin_lock_irqsave(&chip
->lock
, flags
);
150 switch ((mode
&= AD1816A_MODE_OPEN
)) {
151 case AD1816A_MODE_PLAYBACK
:
152 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
153 AD1816A_PLAYBACK_IRQ_PENDING
, 0x00);
154 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
155 AD1816A_PLAYBACK_IRQ_ENABLE
, 0x0000);
157 case AD1816A_MODE_CAPTURE
:
158 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
159 AD1816A_CAPTURE_IRQ_PENDING
, 0x00);
160 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
161 AD1816A_CAPTURE_IRQ_ENABLE
, 0x0000);
163 case AD1816A_MODE_TIMER
:
164 snd_ad1816a_out_mask(chip
, AD1816A_INTERRUPT_STATUS
,
165 AD1816A_TIMER_IRQ_PENDING
, 0x00);
166 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
167 AD1816A_TIMER_IRQ_ENABLE
, 0x0000);
169 if (!((chip
->mode
&= ~mode
) & AD1816A_MODE_OPEN
))
172 spin_unlock_irqrestore(&chip
->lock
, flags
);
176 static int snd_ad1816a_trigger(struct snd_ad1816a
*chip
, unsigned char what
,
177 int channel
, int cmd
, int iscapture
)
182 case SNDRV_PCM_TRIGGER_START
:
183 case SNDRV_PCM_TRIGGER_STOP
:
184 spin_lock(&chip
->lock
);
185 cmd
= (cmd
== SNDRV_PCM_TRIGGER_START
) ? 0xff: 0x00;
186 /* if (what & AD1816A_PLAYBACK_ENABLE) */
187 /* That is not valid, because playback and capture enable
188 * are the same bit pattern, just to different addresses
191 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
192 AD1816A_PLAYBACK_ENABLE
, cmd
);
194 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
195 AD1816A_CAPTURE_ENABLE
, cmd
);
196 spin_unlock(&chip
->lock
);
199 snd_printk(KERN_WARNING
"invalid trigger mode 0x%x.\n", what
);
206 static int snd_ad1816a_playback_trigger(struct snd_pcm_substream
*substream
, int cmd
)
208 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
209 return snd_ad1816a_trigger(chip
, AD1816A_PLAYBACK_ENABLE
,
210 SNDRV_PCM_STREAM_PLAYBACK
, cmd
, 0);
213 static int snd_ad1816a_capture_trigger(struct snd_pcm_substream
*substream
, int cmd
)
215 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
216 return snd_ad1816a_trigger(chip
, AD1816A_CAPTURE_ENABLE
,
217 SNDRV_PCM_STREAM_CAPTURE
, cmd
, 1);
220 static int snd_ad1816a_hw_params(struct snd_pcm_substream
*substream
,
221 struct snd_pcm_hw_params
*hw_params
)
223 return snd_pcm_lib_malloc_pages(substream
, params_buffer_bytes(hw_params
));
226 static int snd_ad1816a_hw_free(struct snd_pcm_substream
*substream
)
228 return snd_pcm_lib_free_pages(substream
);
231 static int snd_ad1816a_playback_prepare(struct snd_pcm_substream
*substream
)
233 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
235 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
236 unsigned int size
, rate
;
238 spin_lock_irqsave(&chip
->lock
, flags
);
240 chip
->p_dma_size
= size
= snd_pcm_lib_buffer_bytes(substream
);
241 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
242 AD1816A_PLAYBACK_ENABLE
| AD1816A_PLAYBACK_PIO
, 0x00);
244 snd_dma_program(chip
->dma1
, runtime
->dma_addr
, size
,
245 DMA_MODE_WRITE
| DMA_AUTOINIT
);
247 rate
= runtime
->rate
;
248 if (chip
->clock_freq
)
249 rate
= (rate
* 33000) / chip
->clock_freq
;
250 snd_ad1816a_write(chip
, AD1816A_PLAYBACK_SAMPLE_RATE
, rate
);
251 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
252 AD1816A_FMT_ALL
| AD1816A_FMT_STEREO
,
253 snd_ad1816a_get_format(chip
, runtime
->format
,
256 snd_ad1816a_write(chip
, AD1816A_PLAYBACK_BASE_COUNT
,
257 snd_pcm_lib_period_bytes(substream
) / 4 - 1);
259 spin_unlock_irqrestore(&chip
->lock
, flags
);
263 static int snd_ad1816a_capture_prepare(struct snd_pcm_substream
*substream
)
265 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
267 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
268 unsigned int size
, rate
;
270 spin_lock_irqsave(&chip
->lock
, flags
);
272 chip
->c_dma_size
= size
= snd_pcm_lib_buffer_bytes(substream
);
273 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
274 AD1816A_CAPTURE_ENABLE
| AD1816A_CAPTURE_PIO
, 0x00);
276 snd_dma_program(chip
->dma2
, runtime
->dma_addr
, size
,
277 DMA_MODE_READ
| DMA_AUTOINIT
);
279 rate
= runtime
->rate
;
280 if (chip
->clock_freq
)
281 rate
= (rate
* 33000) / chip
->clock_freq
;
282 snd_ad1816a_write(chip
, AD1816A_CAPTURE_SAMPLE_RATE
, rate
);
283 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
284 AD1816A_FMT_ALL
| AD1816A_FMT_STEREO
,
285 snd_ad1816a_get_format(chip
, runtime
->format
,
288 snd_ad1816a_write(chip
, AD1816A_CAPTURE_BASE_COUNT
,
289 snd_pcm_lib_period_bytes(substream
) / 4 - 1);
291 spin_unlock_irqrestore(&chip
->lock
, flags
);
296 static snd_pcm_uframes_t
snd_ad1816a_playback_pointer(struct snd_pcm_substream
*substream
)
298 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
300 if (!(chip
->mode
& AD1816A_MODE_PLAYBACK
))
302 ptr
= snd_dma_pointer(chip
->dma1
, chip
->p_dma_size
);
303 return bytes_to_frames(substream
->runtime
, ptr
);
306 static snd_pcm_uframes_t
snd_ad1816a_capture_pointer(struct snd_pcm_substream
*substream
)
308 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
310 if (!(chip
->mode
& AD1816A_MODE_CAPTURE
))
312 ptr
= snd_dma_pointer(chip
->dma2
, chip
->c_dma_size
);
313 return bytes_to_frames(substream
->runtime
, ptr
);
317 static irqreturn_t
snd_ad1816a_interrupt(int irq
, void *dev_id
)
319 struct snd_ad1816a
*chip
= dev_id
;
320 unsigned char status
;
322 spin_lock(&chip
->lock
);
323 status
= snd_ad1816a_in(chip
, AD1816A_INTERRUPT_STATUS
);
324 spin_unlock(&chip
->lock
);
326 if ((status
& AD1816A_PLAYBACK_IRQ_PENDING
) && chip
->playback_substream
)
327 snd_pcm_period_elapsed(chip
->playback_substream
);
329 if ((status
& AD1816A_CAPTURE_IRQ_PENDING
) && chip
->capture_substream
)
330 snd_pcm_period_elapsed(chip
->capture_substream
);
332 if ((status
& AD1816A_TIMER_IRQ_PENDING
) && chip
->timer
)
333 snd_timer_interrupt(chip
->timer
, chip
->timer
->sticks
);
335 spin_lock(&chip
->lock
);
336 snd_ad1816a_out(chip
, AD1816A_INTERRUPT_STATUS
, 0x00);
337 spin_unlock(&chip
->lock
);
342 static struct snd_pcm_hardware snd_ad1816a_playback
= {
343 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
344 SNDRV_PCM_INFO_MMAP_VALID
),
345 .formats
= (SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
|
346 SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
347 SNDRV_PCM_FMTBIT_S16_BE
),
348 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
353 .buffer_bytes_max
= (128*1024),
354 .period_bytes_min
= 64,
355 .period_bytes_max
= (128*1024),
361 static struct snd_pcm_hardware snd_ad1816a_capture
= {
362 .info
= (SNDRV_PCM_INFO_MMAP
| SNDRV_PCM_INFO_INTERLEAVED
|
363 SNDRV_PCM_INFO_MMAP_VALID
),
364 .formats
= (SNDRV_PCM_FMTBIT_MU_LAW
| SNDRV_PCM_FMTBIT_A_LAW
|
365 SNDRV_PCM_FMTBIT_U8
| SNDRV_PCM_FMTBIT_S16_LE
|
366 SNDRV_PCM_FMTBIT_S16_BE
),
367 .rates
= SNDRV_PCM_RATE_CONTINUOUS
| SNDRV_PCM_RATE_8000_48000
,
372 .buffer_bytes_max
= (128*1024),
373 .period_bytes_min
= 64,
374 .period_bytes_max
= (128*1024),
380 static int snd_ad1816a_timer_close(struct snd_timer
*timer
)
382 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
383 snd_ad1816a_close(chip
, AD1816A_MODE_TIMER
);
387 static int snd_ad1816a_timer_open(struct snd_timer
*timer
)
389 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
390 snd_ad1816a_open(chip
, AD1816A_MODE_TIMER
);
394 static unsigned long snd_ad1816a_timer_resolution(struct snd_timer
*timer
)
396 if (snd_BUG_ON(!timer
))
402 static int snd_ad1816a_timer_start(struct snd_timer
*timer
)
406 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
407 spin_lock_irqsave(&chip
->lock
, flags
);
408 bits
= snd_ad1816a_read(chip
, AD1816A_INTERRUPT_ENABLE
);
410 if (!(bits
& AD1816A_TIMER_ENABLE
)) {
411 snd_ad1816a_write(chip
, AD1816A_TIMER_BASE_COUNT
,
412 timer
->sticks
& 0xffff);
414 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
415 AD1816A_TIMER_ENABLE
, 0xffff);
417 spin_unlock_irqrestore(&chip
->lock
, flags
);
421 static int snd_ad1816a_timer_stop(struct snd_timer
*timer
)
424 struct snd_ad1816a
*chip
= snd_timer_chip(timer
);
425 spin_lock_irqsave(&chip
->lock
, flags
);
427 snd_ad1816a_write_mask(chip
, AD1816A_INTERRUPT_ENABLE
,
428 AD1816A_TIMER_ENABLE
, 0x0000);
430 spin_unlock_irqrestore(&chip
->lock
, flags
);
434 static struct snd_timer_hardware snd_ad1816a_timer_table
= {
435 .flags
= SNDRV_TIMER_HW_AUTO
,
438 .open
= snd_ad1816a_timer_open
,
439 .close
= snd_ad1816a_timer_close
,
440 .c_resolution
= snd_ad1816a_timer_resolution
,
441 .start
= snd_ad1816a_timer_start
,
442 .stop
= snd_ad1816a_timer_stop
,
445 static int snd_ad1816a_playback_open(struct snd_pcm_substream
*substream
)
447 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
448 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
451 if ((error
= snd_ad1816a_open(chip
, AD1816A_MODE_PLAYBACK
)) < 0)
453 runtime
->hw
= snd_ad1816a_playback
;
454 snd_pcm_limit_isa_dma_size(chip
->dma1
, &runtime
->hw
.buffer_bytes_max
);
455 snd_pcm_limit_isa_dma_size(chip
->dma1
, &runtime
->hw
.period_bytes_max
);
456 chip
->playback_substream
= substream
;
460 static int snd_ad1816a_capture_open(struct snd_pcm_substream
*substream
)
462 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
463 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
466 if ((error
= snd_ad1816a_open(chip
, AD1816A_MODE_CAPTURE
)) < 0)
468 runtime
->hw
= snd_ad1816a_capture
;
469 snd_pcm_limit_isa_dma_size(chip
->dma2
, &runtime
->hw
.buffer_bytes_max
);
470 snd_pcm_limit_isa_dma_size(chip
->dma2
, &runtime
->hw
.period_bytes_max
);
471 chip
->capture_substream
= substream
;
475 static int snd_ad1816a_playback_close(struct snd_pcm_substream
*substream
)
477 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
479 chip
->playback_substream
= NULL
;
480 snd_ad1816a_close(chip
, AD1816A_MODE_PLAYBACK
);
484 static int snd_ad1816a_capture_close(struct snd_pcm_substream
*substream
)
486 struct snd_ad1816a
*chip
= snd_pcm_substream_chip(substream
);
488 chip
->capture_substream
= NULL
;
489 snd_ad1816a_close(chip
, AD1816A_MODE_CAPTURE
);
494 static void snd_ad1816a_init(struct snd_ad1816a
*chip
)
498 spin_lock_irqsave(&chip
->lock
, flags
);
500 snd_ad1816a_out(chip
, AD1816A_INTERRUPT_STATUS
, 0x00);
501 snd_ad1816a_out_mask(chip
, AD1816A_PLAYBACK_CONFIG
,
502 AD1816A_PLAYBACK_ENABLE
| AD1816A_PLAYBACK_PIO
, 0x00);
503 snd_ad1816a_out_mask(chip
, AD1816A_CAPTURE_CONFIG
,
504 AD1816A_CAPTURE_ENABLE
| AD1816A_CAPTURE_PIO
, 0x00);
505 snd_ad1816a_write(chip
, AD1816A_INTERRUPT_ENABLE
, 0x0000);
506 snd_ad1816a_write_mask(chip
, AD1816A_CHIP_CONFIG
,
507 AD1816A_CAPTURE_NOT_EQUAL
| AD1816A_WSS_ENABLE
, 0xffff);
508 snd_ad1816a_write(chip
, AD1816A_DSP_CONFIG
, 0x0000);
509 snd_ad1816a_write(chip
, AD1816A_POWERDOWN_CTRL
, 0x0000);
511 spin_unlock_irqrestore(&chip
->lock
, flags
);
515 void snd_ad1816a_suspend(struct snd_ad1816a
*chip
)
520 snd_pcm_suspend_all(chip
->pcm
);
521 spin_lock_irqsave(&chip
->lock
, flags
);
522 for (reg
= 0; reg
< 48; reg
++)
523 chip
->image
[reg
] = snd_ad1816a_read(chip
, reg
);
524 spin_unlock_irqrestore(&chip
->lock
, flags
);
527 void snd_ad1816a_resume(struct snd_ad1816a
*chip
)
532 snd_ad1816a_init(chip
);
533 spin_lock_irqsave(&chip
->lock
, flags
);
534 for (reg
= 0; reg
< 48; reg
++)
535 snd_ad1816a_write(chip
, reg
, chip
->image
[reg
]);
536 spin_unlock_irqrestore(&chip
->lock
, flags
);
540 static int snd_ad1816a_probe(struct snd_ad1816a
*chip
)
544 spin_lock_irqsave(&chip
->lock
, flags
);
546 switch (chip
->version
= snd_ad1816a_read(chip
, AD1816A_VERSION_ID
)) {
548 chip
->hardware
= AD1816A_HW_AD1815
;
551 chip
->hardware
= AD1816A_HW_AD18MAX10
;
554 chip
->hardware
= AD1816A_HW_AD1816A
;
557 chip
->hardware
= AD1816A_HW_AUTO
;
560 spin_unlock_irqrestore(&chip
->lock
, flags
);
564 static int snd_ad1816a_free(struct snd_ad1816a
*chip
)
566 release_and_free_resource(chip
->res_port
);
568 free_irq(chip
->irq
, (void *) chip
);
569 if (chip
->dma1
>= 0) {
570 snd_dma_disable(chip
->dma1
);
571 free_dma(chip
->dma1
);
573 if (chip
->dma2
>= 0) {
574 snd_dma_disable(chip
->dma2
);
575 free_dma(chip
->dma2
);
580 static int snd_ad1816a_dev_free(struct snd_device
*device
)
582 struct snd_ad1816a
*chip
= device
->device_data
;
583 return snd_ad1816a_free(chip
);
586 static const char *snd_ad1816a_chip_id(struct snd_ad1816a
*chip
)
588 switch (chip
->hardware
) {
589 case AD1816A_HW_AD1816A
: return "AD1816A";
590 case AD1816A_HW_AD1815
: return "AD1815";
591 case AD1816A_HW_AD18MAX10
: return "AD18max10";
593 snd_printk(KERN_WARNING
"Unknown chip version %d:%d.\n",
594 chip
->version
, chip
->hardware
);
595 return "AD1816A - unknown";
599 int snd_ad1816a_create(struct snd_card
*card
,
600 unsigned long port
, int irq
, int dma1
, int dma2
,
601 struct snd_ad1816a
*chip
)
603 static struct snd_device_ops ops
= {
604 .dev_free
= snd_ad1816a_dev_free
,
612 if ((chip
->res_port
= request_region(port
, 16, "AD1816A")) == NULL
) {
613 snd_printk(KERN_ERR
"ad1816a: can't grab port 0x%lx\n", port
);
614 snd_ad1816a_free(chip
);
617 if (request_irq(irq
, snd_ad1816a_interrupt
, 0, "AD1816A", (void *) chip
)) {
618 snd_printk(KERN_ERR
"ad1816a: can't grab IRQ %d\n", irq
);
619 snd_ad1816a_free(chip
);
623 if (request_dma(dma1
, "AD1816A - 1")) {
624 snd_printk(KERN_ERR
"ad1816a: can't grab DMA1 %d\n", dma1
);
625 snd_ad1816a_free(chip
);
629 if (request_dma(dma2
, "AD1816A - 2")) {
630 snd_printk(KERN_ERR
"ad1816a: can't grab DMA2 %d\n", dma2
);
631 snd_ad1816a_free(chip
);
638 spin_lock_init(&chip
->lock
);
640 if ((error
= snd_ad1816a_probe(chip
))) {
641 snd_ad1816a_free(chip
);
645 snd_ad1816a_init(chip
);
647 /* Register device */
648 if ((error
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, chip
, &ops
)) < 0) {
649 snd_ad1816a_free(chip
);
656 static struct snd_pcm_ops snd_ad1816a_playback_ops
= {
657 .open
= snd_ad1816a_playback_open
,
658 .close
= snd_ad1816a_playback_close
,
659 .ioctl
= snd_pcm_lib_ioctl
,
660 .hw_params
= snd_ad1816a_hw_params
,
661 .hw_free
= snd_ad1816a_hw_free
,
662 .prepare
= snd_ad1816a_playback_prepare
,
663 .trigger
= snd_ad1816a_playback_trigger
,
664 .pointer
= snd_ad1816a_playback_pointer
,
667 static struct snd_pcm_ops snd_ad1816a_capture_ops
= {
668 .open
= snd_ad1816a_capture_open
,
669 .close
= snd_ad1816a_capture_close
,
670 .ioctl
= snd_pcm_lib_ioctl
,
671 .hw_params
= snd_ad1816a_hw_params
,
672 .hw_free
= snd_ad1816a_hw_free
,
673 .prepare
= snd_ad1816a_capture_prepare
,
674 .trigger
= snd_ad1816a_capture_trigger
,
675 .pointer
= snd_ad1816a_capture_pointer
,
678 int snd_ad1816a_pcm(struct snd_ad1816a
*chip
, int device
, struct snd_pcm
**rpcm
)
683 if ((error
= snd_pcm_new(chip
->card
, "AD1816A", device
, 1, 1, &pcm
)))
686 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
, &snd_ad1816a_playback_ops
);
687 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_CAPTURE
, &snd_ad1816a_capture_ops
);
689 pcm
->private_data
= chip
;
690 pcm
->info_flags
= (chip
->dma1
== chip
->dma2
) ? SNDRV_PCM_INFO_JOINT_DUPLEX
: 0;
692 strcpy(pcm
->name
, snd_ad1816a_chip_id(chip
));
693 snd_ad1816a_init(chip
);
695 snd_pcm_lib_preallocate_pages_for_all(pcm
, SNDRV_DMA_TYPE_DEV
,
697 64*1024, chip
->dma1
> 3 || chip
->dma2
> 3 ? 128*1024 : 64*1024);
705 int snd_ad1816a_timer(struct snd_ad1816a
*chip
, int device
,
706 struct snd_timer
**rtimer
)
708 struct snd_timer
*timer
;
709 struct snd_timer_id tid
;
712 tid
.dev_class
= SNDRV_TIMER_CLASS_CARD
;
713 tid
.dev_sclass
= SNDRV_TIMER_SCLASS_NONE
;
714 tid
.card
= chip
->card
->number
;
717 if ((error
= snd_timer_new(chip
->card
, "AD1816A", &tid
, &timer
)) < 0)
719 strcpy(timer
->name
, snd_ad1816a_chip_id(chip
));
720 timer
->private_data
= chip
;
722 timer
->hw
= snd_ad1816a_timer_table
;
732 static int snd_ad1816a_info_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
734 static char *texts
[8] = {
735 "Line", "Mix", "CD", "Synth", "Video",
739 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
741 uinfo
->value
.enumerated
.items
= 7;
742 if (uinfo
->value
.enumerated
.item
> 6)
743 uinfo
->value
.enumerated
.item
= 6;
744 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
748 static int snd_ad1816a_get_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
750 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
754 spin_lock_irqsave(&chip
->lock
, flags
);
755 val
= snd_ad1816a_read(chip
, AD1816A_ADC_SOURCE_SEL
);
756 spin_unlock_irqrestore(&chip
->lock
, flags
);
757 ucontrol
->value
.enumerated
.item
[0] = (val
>> 12) & 7;
758 ucontrol
->value
.enumerated
.item
[1] = (val
>> 4) & 7;
762 static int snd_ad1816a_put_mux(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
764 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
769 if (ucontrol
->value
.enumerated
.item
[0] > 6 ||
770 ucontrol
->value
.enumerated
.item
[1] > 6)
772 val
= (ucontrol
->value
.enumerated
.item
[0] << 12) |
773 (ucontrol
->value
.enumerated
.item
[1] << 4);
774 spin_lock_irqsave(&chip
->lock
, flags
);
775 change
= snd_ad1816a_read(chip
, AD1816A_ADC_SOURCE_SEL
) != val
;
776 snd_ad1816a_write(chip
, AD1816A_ADC_SOURCE_SEL
, val
);
777 spin_unlock_irqrestore(&chip
->lock
, flags
);
781 #define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv) \
782 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
783 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
784 .name = xname, .info = snd_ad1816a_info_single, \
785 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
786 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
787 .tlv = { .p = (xtlv) } }
788 #define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
789 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
790 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
791 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
793 static int snd_ad1816a_info_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
795 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
797 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
799 uinfo
->value
.integer
.min
= 0;
800 uinfo
->value
.integer
.max
= mask
;
804 static int snd_ad1816a_get_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
806 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
808 int reg
= kcontrol
->private_value
& 0xff;
809 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
810 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
811 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
813 spin_lock_irqsave(&chip
->lock
, flags
);
814 ucontrol
->value
.integer
.value
[0] = (snd_ad1816a_read(chip
, reg
) >> shift
) & mask
;
815 spin_unlock_irqrestore(&chip
->lock
, flags
);
817 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
821 static int snd_ad1816a_put_single(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
823 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
825 int reg
= kcontrol
->private_value
& 0xff;
826 int shift
= (kcontrol
->private_value
>> 8) & 0xff;
827 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
828 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
830 unsigned short old_val
, val
;
832 val
= (ucontrol
->value
.integer
.value
[0] & mask
);
836 spin_lock_irqsave(&chip
->lock
, flags
);
837 old_val
= snd_ad1816a_read(chip
, reg
);
838 val
= (old_val
& ~(mask
<< shift
)) | val
;
839 change
= val
!= old_val
;
840 snd_ad1816a_write(chip
, reg
, val
);
841 spin_unlock_irqrestore(&chip
->lock
, flags
);
845 #define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
846 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
847 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
848 .name = xname, .info = snd_ad1816a_info_double, \
849 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
850 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
851 .tlv = { .p = (xtlv) } }
853 #define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
854 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
855 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
856 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
858 static int snd_ad1816a_info_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
860 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
862 uinfo
->type
= mask
== 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN
: SNDRV_CTL_ELEM_TYPE_INTEGER
;
864 uinfo
->value
.integer
.min
= 0;
865 uinfo
->value
.integer
.max
= mask
;
869 static int snd_ad1816a_get_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
871 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
873 int reg
= kcontrol
->private_value
& 0xff;
874 int shift_left
= (kcontrol
->private_value
>> 8) & 0x0f;
875 int shift_right
= (kcontrol
->private_value
>> 12) & 0x0f;
876 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
877 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
880 spin_lock_irqsave(&chip
->lock
, flags
);
881 val
= snd_ad1816a_read(chip
, reg
);
882 ucontrol
->value
.integer
.value
[0] = (val
>> shift_left
) & mask
;
883 ucontrol
->value
.integer
.value
[1] = (val
>> shift_right
) & mask
;
884 spin_unlock_irqrestore(&chip
->lock
, flags
);
886 ucontrol
->value
.integer
.value
[0] = mask
- ucontrol
->value
.integer
.value
[0];
887 ucontrol
->value
.integer
.value
[1] = mask
- ucontrol
->value
.integer
.value
[1];
892 static int snd_ad1816a_put_double(struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_value
*ucontrol
)
894 struct snd_ad1816a
*chip
= snd_kcontrol_chip(kcontrol
);
896 int reg
= kcontrol
->private_value
& 0xff;
897 int shift_left
= (kcontrol
->private_value
>> 8) & 0x0f;
898 int shift_right
= (kcontrol
->private_value
>> 12) & 0x0f;
899 int mask
= (kcontrol
->private_value
>> 16) & 0xff;
900 int invert
= (kcontrol
->private_value
>> 24) & 0xff;
902 unsigned short old_val
, val1
, val2
;
904 val1
= ucontrol
->value
.integer
.value
[0] & mask
;
905 val2
= ucontrol
->value
.integer
.value
[1] & mask
;
911 val2
<<= shift_right
;
912 spin_lock_irqsave(&chip
->lock
, flags
);
913 old_val
= snd_ad1816a_read(chip
, reg
);
914 val1
= (old_val
& ~((mask
<< shift_left
) | (mask
<< shift_right
))) | val1
| val2
;
915 change
= val1
!= old_val
;
916 snd_ad1816a_write(chip
, reg
, val1
);
917 spin_unlock_irqrestore(&chip
->lock
, flags
);
921 static const DECLARE_TLV_DB_SCALE(db_scale_4bit
, -4500, 300, 0);
922 static const DECLARE_TLV_DB_SCALE(db_scale_5bit
, -4650, 150, 0);
923 static const DECLARE_TLV_DB_SCALE(db_scale_6bit
, -9450, 150, 0);
924 static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max
, -3450, 150, 0);
925 static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain
, 0, 150, 0);
927 static struct snd_kcontrol_new snd_ad1816a_controls
[] = {
928 AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT
, 15, 7, 1, 1),
929 AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT
, 8, 0, 31, 1,
931 AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT
, 15, 7, 1, 1),
932 AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT
, 8, 0, 63, 1,
934 AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT
, 15, 7, 1, 1),
935 AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT
, 8, 0, 31, 1,
936 db_scale_5bit_12db_max
),
937 AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT
, 15, 7, 1, 1),
938 AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT
, 8, 0, 31, 1,
939 db_scale_5bit_12db_max
),
940 AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT
, 15, 7, 1, 1),
941 AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT
, 8, 0, 31, 1,
942 db_scale_5bit_12db_max
),
943 AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT
, 15, 7, 1, 1),
944 AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT
, 8, 0, 63, 1,
946 AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT
, 15, 1, 1),
947 AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT
, 8, 31, 1,
948 db_scale_5bit_12db_max
),
949 AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT
, 14, 1, 0),
950 AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT
, 15, 7, 1, 1),
951 AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT
, 8, 0, 31, 1,
952 db_scale_5bit_12db_max
),
953 AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT
, 15, 1, 1),
954 AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT
, 0, 15, 1,
956 AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT
, 7, 1, 1),
957 AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT
, 0, 31, 1,
960 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
961 .name
= "Capture Source",
962 .info
= snd_ad1816a_info_mux
,
963 .get
= snd_ad1816a_get_mux
,
964 .put
= snd_ad1816a_put_mux
,
966 AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA
, 15, 7, 1, 1),
967 AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA
, 8, 0, 15, 0,
969 AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL
, 15, 1, 1),
970 AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL
, 0, 15, 0),
973 int snd_ad1816a_mixer(struct snd_ad1816a
*chip
)
975 struct snd_card
*card
;
979 if (snd_BUG_ON(!chip
|| !chip
->card
))
984 strcpy(card
->mixername
, snd_ad1816a_chip_id(chip
));
986 for (idx
= 0; idx
< ARRAY_SIZE(snd_ad1816a_controls
); idx
++) {
987 if ((err
= snd_ctl_add(card
, snd_ctl_new1(&snd_ad1816a_controls
[idx
], chip
))) < 0)