blob: 01a07986f4a3dcffb89aa8b7d00338616f73c864 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 ad1816a.c - lowlevel code for Analog Devices AD1816A chip.
3 Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
4
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.
9
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.
14
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
18*/
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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>
Takashi Iwai0e7febf2006-08-22 13:16:01 +020026#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <sound/ad1816a.h>
28
29#include <asm/io.h>
30#include <asm/dma.h>
31
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010032static inline int snd_ad1816a_busy_wait(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
34 int timeout;
35
36 for (timeout = 1000; timeout-- > 0; udelay(10))
37 if (inb(AD1816A_REG(AD1816A_CHIP_STATUS)) & AD1816A_READY)
38 return 0;
39
Takashi Iwai4c9f1d32009-02-05 15:47:51 +010040 snd_printk(KERN_WARNING "chip busy.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return -EBUSY;
42}
43
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010044static inline unsigned char snd_ad1816a_in(struct snd_ad1816a *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 snd_ad1816a_busy_wait(chip);
47 return inb(AD1816A_REG(reg));
48}
49
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010050static inline void snd_ad1816a_out(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 unsigned char value)
52{
53 snd_ad1816a_busy_wait(chip);
54 outb(value, AD1816A_REG(reg));
55}
56
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010057static inline void snd_ad1816a_out_mask(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 unsigned char mask, unsigned char value)
59{
60 snd_ad1816a_out(chip, reg,
61 (value & mask) | (snd_ad1816a_in(chip, reg) & ~mask));
62}
63
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010064static unsigned short snd_ad1816a_read(struct snd_ad1816a *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
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);
69}
70
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010071static void snd_ad1816a_write(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned short value)
73{
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);
77}
78
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010079static void snd_ad1816a_write_mask(struct snd_ad1816a *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 unsigned short mask, unsigned short value)
81{
82 snd_ad1816a_write(chip, reg,
83 (value & mask) | (snd_ad1816a_read(chip, reg) & ~mask));
84}
85
86
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +010087static unsigned char snd_ad1816a_get_format(struct snd_ad1816a *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned int format, int channels)
89{
90 unsigned char retval = AD1816A_FMT_LINEAR_8;
91
92 switch (format) {
93 case SNDRV_PCM_FORMAT_MU_LAW:
94 retval = AD1816A_FMT_ULAW_8;
95 break;
96 case SNDRV_PCM_FORMAT_A_LAW:
97 retval = AD1816A_FMT_ALAW_8;
98 break;
99 case SNDRV_PCM_FORMAT_S16_LE:
100 retval = AD1816A_FMT_LINEAR_16_LIT;
101 break;
102 case SNDRV_PCM_FORMAT_S16_BE:
103 retval = AD1816A_FMT_LINEAR_16_BIG;
104 }
105 return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval;
106}
107
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100108static int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned long flags;
111
112 spin_lock_irqsave(&chip->lock, flags);
113
114 if (chip->mode & mode) {
115 spin_unlock_irqrestore(&chip->lock, flags);
116 return -EAGAIN;
117 }
118
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);
125 break;
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);
131 break;
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);
137 }
138 chip->mode |= mode;
139
140 spin_unlock_irqrestore(&chip->lock, flags);
141 return 0;
142}
143
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100144static void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 unsigned long flags;
147
148 spin_lock_irqsave(&chip->lock, flags);
149
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);
156 break;
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);
162 break;
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);
168 }
169 if (!((chip->mode &= ~mode) & AD1816A_MODE_OPEN))
170 chip->mode = 0;
171
172 spin_unlock_irqrestore(&chip->lock, flags);
173}
174
175
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100176static int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100177 int channel, int cmd, int iscapture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 int error = 0;
180
181 switch (cmd) {
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;
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100186 /* 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
189 */
190 if (! iscapture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
192 AD1816A_PLAYBACK_ENABLE, cmd);
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100193 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
195 AD1816A_CAPTURE_ENABLE, cmd);
196 spin_unlock(&chip->lock);
197 break;
198 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100199 snd_printk(KERN_WARNING "invalid trigger mode 0x%x.\n", what);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 error = -EINVAL;
201 }
202
203 return error;
204}
205
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100206static int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100208 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100210 SNDRV_PCM_STREAM_PLAYBACK, cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100213static int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100215 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100217 SNDRV_PCM_STREAM_CAPTURE, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100220static int snd_ad1816a_hw_params(struct snd_pcm_substream *substream,
221 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
224}
225
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100226static int snd_ad1816a_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 return snd_pcm_lib_free_pages(substream);
229}
230
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100231static int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100233 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100235 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200236 unsigned int size, rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 spin_lock_irqsave(&chip->lock, flags);
239
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);
243
244 snd_dma_program(chip->dma1, runtime->dma_addr, size,
245 DMA_MODE_WRITE | DMA_AUTOINIT);
246
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200247 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
252 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
253 snd_ad1816a_get_format(chip, runtime->format,
254 runtime->channels));
255
256 snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT,
257 snd_pcm_lib_period_bytes(substream) / 4 - 1);
258
259 spin_unlock_irqrestore(&chip->lock, flags);
260 return 0;
261}
262
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100263static int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100265 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100267 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200268 unsigned int size, rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 spin_lock_irqsave(&chip->lock, flags);
271
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);
275
276 snd_dma_program(chip->dma2, runtime->dma_addr, size,
277 DMA_MODE_READ | DMA_AUTOINIT);
278
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200279 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
284 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
285 snd_ad1816a_get_format(chip, runtime->format,
286 runtime->channels));
287
288 snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT,
289 snd_pcm_lib_period_bytes(substream) / 4 - 1);
290
291 spin_unlock_irqrestore(&chip->lock, flags);
292 return 0;
293}
294
295
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100296static snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100298 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 size_t ptr;
300 if (!(chip->mode & AD1816A_MODE_PLAYBACK))
301 return 0;
302 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
303 return bytes_to_frames(substream->runtime, ptr);
304}
305
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100306static snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100308 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 size_t ptr;
310 if (!(chip->mode & AD1816A_MODE_CAPTURE))
311 return 0;
312 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
313 return bytes_to_frames(substream->runtime, ptr);
314}
315
316
David Howells7d12e782006-10-05 14:55:46 +0100317static irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100319 struct snd_ad1816a *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 unsigned char status;
321
322 spin_lock(&chip->lock);
323 status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS);
324 spin_unlock(&chip->lock);
325
326 if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream)
327 snd_pcm_period_elapsed(chip->playback_substream);
328
329 if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream)
330 snd_pcm_period_elapsed(chip->capture_substream);
331
332 if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer)
333 snd_timer_interrupt(chip->timer, chip->timer->sticks);
334
335 spin_lock(&chip->lock);
336 snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
337 spin_unlock(&chip->lock);
338 return IRQ_HANDLED;
339}
340
341
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100342static struct snd_pcm_hardware snd_ad1816a_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 .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,
349 .rate_min = 4000,
350 .rate_max = 55200,
351 .channels_min = 1,
352 .channels_max = 2,
353 .buffer_bytes_max = (128*1024),
354 .period_bytes_min = 64,
355 .period_bytes_max = (128*1024),
356 .periods_min = 1,
357 .periods_max = 1024,
358 .fifo_size = 0,
359};
360
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100361static struct snd_pcm_hardware snd_ad1816a_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .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,
368 .rate_min = 4000,
369 .rate_max = 55200,
370 .channels_min = 1,
371 .channels_max = 2,
372 .buffer_bytes_max = (128*1024),
373 .period_bytes_min = 64,
374 .period_bytes_max = (128*1024),
375 .periods_min = 1,
376 .periods_max = 1024,
377 .fifo_size = 0,
378};
379
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100380static int snd_ad1816a_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100382 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 snd_ad1816a_close(chip, AD1816A_MODE_TIMER);
384 return 0;
385}
386
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100387static int snd_ad1816a_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100389 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 snd_ad1816a_open(chip, AD1816A_MODE_TIMER);
391 return 0;
392}
393
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100394static unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Takashi Iwai622207d2008-08-08 17:11:45 +0200396 if (snd_BUG_ON(!timer))
397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 10000;
400}
401
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100402static int snd_ad1816a_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 unsigned short bits;
405 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100406 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_lock_irqsave(&chip->lock, flags);
408 bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE);
409
410 if (!(bits & AD1816A_TIMER_ENABLE)) {
411 snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT,
412 timer->sticks & 0xffff);
413
414 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
415 AD1816A_TIMER_ENABLE, 0xffff);
416 }
417 spin_unlock_irqrestore(&chip->lock, flags);
418 return 0;
419}
420
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100421static int snd_ad1816a_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100424 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock_irqsave(&chip->lock, flags);
426
427 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
428 AD1816A_TIMER_ENABLE, 0x0000);
429
430 spin_unlock_irqrestore(&chip->lock, flags);
431 return 0;
432}
433
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100434static struct snd_timer_hardware snd_ad1816a_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .flags = SNDRV_TIMER_HW_AUTO,
436 .resolution = 10000,
437 .ticks = 65535,
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,
443};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100445static int snd_ad1816a_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100447 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
448 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int error;
450
451 if ((error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK)) < 0)
452 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 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;
457 return 0;
458}
459
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100460static int snd_ad1816a_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100462 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
463 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 int error;
465
466 if ((error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE)) < 0)
467 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 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;
472 return 0;
473}
474
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100475static int snd_ad1816a_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100477 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 chip->playback_substream = NULL;
480 snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK);
481 return 0;
482}
483
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100484static int snd_ad1816a_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100486 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 chip->capture_substream = NULL;
489 snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE);
490 return 0;
491}
492
493
Ondrej Zary6f0fa662012-08-19 23:27:26 +0200494static void snd_ad1816a_init(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 unsigned long flags;
497
498 spin_lock_irqsave(&chip->lock, flags);
499
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);
510
511 spin_unlock_irqrestore(&chip->lock, flags);
512}
513
Ondrej Zary6f0fa662012-08-19 23:27:26 +0200514#ifdef CONFIG_PM
515void snd_ad1816a_suspend(struct snd_ad1816a *chip)
516{
517 int reg;
518 unsigned long flags;
519
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);
525}
526
527void snd_ad1816a_resume(struct snd_ad1816a *chip)
528{
529 int reg;
530 unsigned long flags;
531
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);
537}
538#endif
539
Bill Pemberton1bff2922012-12-06 12:35:21 -0500540static int snd_ad1816a_probe(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 unsigned long flags;
543
544 spin_lock_irqsave(&chip->lock, flags);
545
546 switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) {
547 case 0:
548 chip->hardware = AD1816A_HW_AD1815;
549 break;
550 case 1:
551 chip->hardware = AD1816A_HW_AD18MAX10;
552 break;
553 case 3:
554 chip->hardware = AD1816A_HW_AD1816A;
555 break;
556 default:
557 chip->hardware = AD1816A_HW_AUTO;
558 }
559
560 spin_unlock_irqrestore(&chip->lock, flags);
561 return 0;
562}
563
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100564static int snd_ad1816a_free(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Takashi Iwaib1d57762005-10-10 11:56:31 +0200566 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (chip->irq >= 0)
568 free_irq(chip->irq, (void *) chip);
569 if (chip->dma1 >= 0) {
570 snd_dma_disable(chip->dma1);
571 free_dma(chip->dma1);
572 }
573 if (chip->dma2 >= 0) {
574 snd_dma_disable(chip->dma2);
575 free_dma(chip->dma2);
576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return 0;
578}
579
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100580static int snd_ad1816a_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100582 struct snd_ad1816a *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return snd_ad1816a_free(chip);
584}
585
Bill Pemberton1bff2922012-12-06 12:35:21 -0500586static const char *snd_ad1816a_chip_id(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
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";
592 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100593 snd_printk(KERN_WARNING "Unknown chip version %d:%d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 chip->version, chip->hardware);
595 return "AD1816A - unknown";
596 }
597}
598
Bill Pemberton1bff2922012-12-06 12:35:21 -0500599int snd_ad1816a_create(struct snd_card *card,
600 unsigned long port, int irq, int dma1, int dma2,
601 struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100603 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 .dev_free = snd_ad1816a_dev_free,
605 };
606 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 chip->irq = -1;
609 chip->dma1 = -1;
610 chip->dma2 = -1;
611
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);
615 return -EBUSY;
616 }
Yong Zhang88e24c32011-09-22 16:59:20 +0800617 if (request_irq(irq, snd_ad1816a_interrupt, 0, "AD1816A", (void *) chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq);
619 snd_ad1816a_free(chip);
620 return -EBUSY;
621 }
622 chip->irq = irq;
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);
626 return -EBUSY;
627 }
628 chip->dma1 = dma1;
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);
632 return -EBUSY;
633 }
634 chip->dma2 = dma2;
635
636 chip->card = card;
637 chip->port = port;
638 spin_lock_init(&chip->lock);
639
640 if ((error = snd_ad1816a_probe(chip))) {
641 snd_ad1816a_free(chip);
642 return error;
643 }
644
645 snd_ad1816a_init(chip);
646
647 /* Register device */
648 if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
649 snd_ad1816a_free(chip);
650 return error;
651 }
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return 0;
654}
655
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100656static struct snd_pcm_ops snd_ad1816a_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 .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,
665};
666
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100667static struct snd_pcm_ops snd_ad1816a_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 .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,
676};
677
Bill Pemberton1bff2922012-12-06 12:35:21 -0500678int snd_ad1816a_pcm(struct snd_ad1816a *chip, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int error;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100681 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 if ((error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm)))
684 return error;
685
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);
688
689 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0;
691
692 strcpy(pcm->name, snd_ad1816a_chip_id(chip));
693 snd_ad1816a_init(chip);
694
695 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
696 snd_dma_isa_data(),
697 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
698
699 chip->pcm = pcm;
700 if (rpcm)
701 *rpcm = pcm;
702 return 0;
703}
704
Bill Pemberton1bff2922012-12-06 12:35:21 -0500705int snd_ad1816a_timer(struct snd_ad1816a *chip, int device,
706 struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100708 struct snd_timer *timer;
709 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 int error;
711
712 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
713 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
714 tid.card = chip->card->number;
715 tid.device = device;
716 tid.subdevice = 0;
717 if ((error = snd_timer_new(chip->card, "AD1816A", &tid, &timer)) < 0)
718 return error;
719 strcpy(timer->name, snd_ad1816a_chip_id(chip));
720 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 chip->timer = timer;
722 timer->hw = snd_ad1816a_timer_table;
723 if (rtimer)
724 *rtimer = timer;
725 return 0;
726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728/*
729 *
730 */
731
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100732static int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Takashi Iwai1da0c472014-10-20 18:13:39 +0200734 static const char * const texts[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 "Line", "Mix", "CD", "Synth", "Video",
736 "Mic", "Phone",
737 };
738
Takashi Iwai1da0c472014-10-20 18:13:39 +0200739 return snd_ctl_enum_info(uinfo, 2, 7, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100742static int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100744 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned long flags;
746 unsigned short val;
747
748 spin_lock_irqsave(&chip->lock, flags);
749 val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL);
750 spin_unlock_irqrestore(&chip->lock, flags);
751 ucontrol->value.enumerated.item[0] = (val >> 12) & 7;
752 ucontrol->value.enumerated.item[1] = (val >> 4) & 7;
753 return 0;
754}
755
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100756static int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100758 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 unsigned long flags;
760 unsigned short val;
761 int change;
762
763 if (ucontrol->value.enumerated.item[0] > 6 ||
764 ucontrol->value.enumerated.item[1] > 6)
765 return -EINVAL;
766 val = (ucontrol->value.enumerated.item[0] << 12) |
767 (ucontrol->value.enumerated.item[1] << 4);
768 spin_lock_irqsave(&chip->lock, flags);
769 change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val;
770 snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val);
771 spin_unlock_irqrestore(&chip->lock, flags);
772 return change;
773}
774
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200775#define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv) \
776{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
777 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
778 .name = xname, .info = snd_ad1816a_info_single, \
779 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
780 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
781 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
783{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
784 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
785 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
786
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100787static int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 int mask = (kcontrol->private_value >> 16) & 0xff;
790
791 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
792 uinfo->count = 1;
793 uinfo->value.integer.min = 0;
794 uinfo->value.integer.max = mask;
795 return 0;
796}
797
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100798static int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100800 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unsigned long flags;
802 int reg = kcontrol->private_value & 0xff;
803 int shift = (kcontrol->private_value >> 8) & 0xff;
804 int mask = (kcontrol->private_value >> 16) & 0xff;
805 int invert = (kcontrol->private_value >> 24) & 0xff;
806
807 spin_lock_irqsave(&chip->lock, flags);
808 ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask;
809 spin_unlock_irqrestore(&chip->lock, flags);
810 if (invert)
811 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
812 return 0;
813}
814
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100815static int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100817 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 unsigned long flags;
819 int reg = kcontrol->private_value & 0xff;
820 int shift = (kcontrol->private_value >> 8) & 0xff;
821 int mask = (kcontrol->private_value >> 16) & 0xff;
822 int invert = (kcontrol->private_value >> 24) & 0xff;
823 int change;
824 unsigned short old_val, val;
825
826 val = (ucontrol->value.integer.value[0] & mask);
827 if (invert)
828 val = mask - val;
829 val <<= shift;
830 spin_lock_irqsave(&chip->lock, flags);
831 old_val = snd_ad1816a_read(chip, reg);
832 val = (old_val & ~(mask << shift)) | val;
833 change = val != old_val;
834 snd_ad1816a_write(chip, reg, val);
835 spin_unlock_irqrestore(&chip->lock, flags);
836 return change;
837}
838
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200839#define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
840{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
841 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
842 .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), \
845 .tlv = { .p = (xtlv) } }
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
848{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .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
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100852static int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 int mask = (kcontrol->private_value >> 16) & 0xff;
855
856 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
857 uinfo->count = 2;
858 uinfo->value.integer.min = 0;
859 uinfo->value.integer.max = mask;
860 return 0;
861}
862
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100863static int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100865 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 unsigned long flags;
867 int reg = kcontrol->private_value & 0xff;
868 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
869 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
870 int mask = (kcontrol->private_value >> 16) & 0xff;
871 int invert = (kcontrol->private_value >> 24) & 0xff;
872 unsigned short val;
873
874 spin_lock_irqsave(&chip->lock, flags);
875 val = snd_ad1816a_read(chip, reg);
876 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
877 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
878 spin_unlock_irqrestore(&chip->lock, flags);
879 if (invert) {
880 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
881 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
882 }
883 return 0;
884}
885
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100886static int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100888 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned long flags;
890 int reg = kcontrol->private_value & 0xff;
891 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
892 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
893 int mask = (kcontrol->private_value >> 16) & 0xff;
894 int invert = (kcontrol->private_value >> 24) & 0xff;
895 int change;
896 unsigned short old_val, val1, val2;
897
898 val1 = ucontrol->value.integer.value[0] & mask;
899 val2 = ucontrol->value.integer.value[1] & mask;
900 if (invert) {
901 val1 = mask - val1;
902 val2 = mask - val2;
903 }
904 val1 <<= shift_left;
905 val2 <<= shift_right;
906 spin_lock_irqsave(&chip->lock, flags);
907 old_val = snd_ad1816a_read(chip, reg);
908 val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
909 change = val1 != old_val;
910 snd_ad1816a_write(chip, reg, val1);
911 spin_unlock_irqrestore(&chip->lock, flags);
912 return change;
913}
914
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100915static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
916static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
917static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
918static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
919static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200920
Bill Pemberton1bff2922012-12-06 12:35:21 -0500921static struct snd_kcontrol_new snd_ad1816a_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200923AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1,
924 db_scale_5bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200926AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1,
927 db_scale_6bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200929AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1,
930 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200932AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1,
933 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200935AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1,
936 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200938AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1,
939 db_scale_6bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200941AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1,
942 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0),
944AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200945AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1,
946 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200948AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1,
949 db_scale_4bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200951AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1,
952 db_scale_5bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
955 .name = "Capture Source",
956 .info = snd_ad1816a_info_mux,
957 .get = snd_ad1816a_get_mux,
958 .put = snd_ad1816a_put_mux,
959},
960AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200961AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0,
962 db_scale_rec_gain),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1),
964AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0),
965};
966
Bill Pemberton1bff2922012-12-06 12:35:21 -0500967int snd_ad1816a_mixer(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100969 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 unsigned int idx;
971 int err;
972
Takashi Iwai622207d2008-08-08 17:11:45 +0200973 if (snd_BUG_ON(!chip || !chip->card))
974 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 card = chip->card;
977
978 strcpy(card->mixername, snd_ad1816a_chip_id(chip));
979
980 for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) {
981 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip))) < 0)
982 return err;
983 }
984 return 0;
985}