blob: fba6d22f7f4bdfa3591a47c7ad364a293c038549 [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>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010025#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
Takashi Iwai0e7febf2006-08-22 13:16:01 +020027#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/ad1816a.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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,
Takashi Iwaid63f33d2018-07-25 23:19:39 +020088 snd_pcm_format_t format,
89 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 unsigned char retval = AD1816A_FMT_LINEAR_8;
92
93 switch (format) {
94 case SNDRV_PCM_FORMAT_MU_LAW:
95 retval = AD1816A_FMT_ULAW_8;
96 break;
97 case SNDRV_PCM_FORMAT_A_LAW:
98 retval = AD1816A_FMT_ALAW_8;
99 break;
100 case SNDRV_PCM_FORMAT_S16_LE:
101 retval = AD1816A_FMT_LINEAR_16_LIT;
102 break;
103 case SNDRV_PCM_FORMAT_S16_BE:
104 retval = AD1816A_FMT_LINEAR_16_BIG;
105 }
106 return (channels > 1) ? (retval | AD1816A_FMT_STEREO) : retval;
107}
108
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100109static int snd_ad1816a_open(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 unsigned long flags;
112
113 spin_lock_irqsave(&chip->lock, flags);
114
115 if (chip->mode & mode) {
116 spin_unlock_irqrestore(&chip->lock, flags);
117 return -EAGAIN;
118 }
119
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);
126 break;
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);
132 break;
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);
138 }
139 chip->mode |= mode;
140
141 spin_unlock_irqrestore(&chip->lock, flags);
142 return 0;
143}
144
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100145static void snd_ad1816a_close(struct snd_ad1816a *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 unsigned long flags;
148
149 spin_lock_irqsave(&chip->lock, flags);
150
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);
157 break;
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);
163 break;
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);
169 }
170 if (!((chip->mode &= ~mode) & AD1816A_MODE_OPEN))
171 chip->mode = 0;
172
173 spin_unlock_irqrestore(&chip->lock, flags);
174}
175
176
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100177static int snd_ad1816a_trigger(struct snd_ad1816a *chip, unsigned char what,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100178 int channel, int cmd, int iscapture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 int error = 0;
181
182 switch (cmd) {
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;
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100187 /* 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
190 */
191 if (! iscapture)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
193 AD1816A_PLAYBACK_ENABLE, cmd);
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100194 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
196 AD1816A_CAPTURE_ENABLE, cmd);
197 spin_unlock(&chip->lock);
198 break;
199 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100200 snd_printk(KERN_WARNING "invalid trigger mode 0x%x.\n", what);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 error = -EINVAL;
202 }
203
204 return error;
205}
206
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100207static int snd_ad1816a_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100209 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return snd_ad1816a_trigger(chip, AD1816A_PLAYBACK_ENABLE,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100211 SNDRV_PCM_STREAM_PLAYBACK, cmd, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100214static int snd_ad1816a_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100216 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return snd_ad1816a_trigger(chip, AD1816A_CAPTURE_ENABLE,
Ken Arromdeed08a23e2006-02-09 13:50:26 +0100218 SNDRV_PCM_STREAM_CAPTURE, cmd, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100221static int snd_ad1816a_hw_params(struct snd_pcm_substream *substream,
222 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
225}
226
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100227static int snd_ad1816a_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 return snd_pcm_lib_free_pages(substream);
230}
231
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100232static int snd_ad1816a_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100234 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100236 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200237 unsigned int size, rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 spin_lock_irqsave(&chip->lock, flags);
240
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);
244
245 snd_dma_program(chip->dma1, runtime->dma_addr, size,
246 DMA_MODE_WRITE | DMA_AUTOINIT);
247
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200248 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
253 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
254 snd_ad1816a_get_format(chip, runtime->format,
255 runtime->channels));
256
257 snd_ad1816a_write(chip, AD1816A_PLAYBACK_BASE_COUNT,
258 snd_pcm_lib_period_bytes(substream) / 4 - 1);
259
260 spin_unlock_irqrestore(&chip->lock, flags);
261 return 0;
262}
263
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100264static int snd_ad1816a_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100266 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100268 struct snd_pcm_runtime *runtime = substream->runtime;
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200269 unsigned int size, rate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 spin_lock_irqsave(&chip->lock, flags);
272
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);
276
277 snd_dma_program(chip->dma2, runtime->dma_addr, size,
278 DMA_MODE_READ | DMA_AUTOINIT);
279
Takashi Iwai5b8f7f72005-08-03 14:02:47 +0200280 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
285 AD1816A_FMT_ALL | AD1816A_FMT_STEREO,
286 snd_ad1816a_get_format(chip, runtime->format,
287 runtime->channels));
288
289 snd_ad1816a_write(chip, AD1816A_CAPTURE_BASE_COUNT,
290 snd_pcm_lib_period_bytes(substream) / 4 - 1);
291
292 spin_unlock_irqrestore(&chip->lock, flags);
293 return 0;
294}
295
296
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100297static snd_pcm_uframes_t snd_ad1816a_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100299 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 size_t ptr;
301 if (!(chip->mode & AD1816A_MODE_PLAYBACK))
302 return 0;
303 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
304 return bytes_to_frames(substream->runtime, ptr);
305}
306
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100307static snd_pcm_uframes_t snd_ad1816a_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100309 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 size_t ptr;
311 if (!(chip->mode & AD1816A_MODE_CAPTURE))
312 return 0;
313 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
314 return bytes_to_frames(substream->runtime, ptr);
315}
316
317
David Howells7d12e782006-10-05 14:55:46 +0100318static irqreturn_t snd_ad1816a_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100320 struct snd_ad1816a *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned char status;
322
323 spin_lock(&chip->lock);
324 status = snd_ad1816a_in(chip, AD1816A_INTERRUPT_STATUS);
325 spin_unlock(&chip->lock);
326
327 if ((status & AD1816A_PLAYBACK_IRQ_PENDING) && chip->playback_substream)
328 snd_pcm_period_elapsed(chip->playback_substream);
329
330 if ((status & AD1816A_CAPTURE_IRQ_PENDING) && chip->capture_substream)
331 snd_pcm_period_elapsed(chip->capture_substream);
332
333 if ((status & AD1816A_TIMER_IRQ_PENDING) && chip->timer)
334 snd_timer_interrupt(chip->timer, chip->timer->sticks);
335
336 spin_lock(&chip->lock);
337 snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
338 spin_unlock(&chip->lock);
339 return IRQ_HANDLED;
340}
341
342
Bhumika Goyalaec54652017-08-17 14:45:52 +0530343static const struct snd_pcm_hardware snd_ad1816a_playback = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 .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,
350 .rate_min = 4000,
351 .rate_max = 55200,
352 .channels_min = 1,
353 .channels_max = 2,
354 .buffer_bytes_max = (128*1024),
355 .period_bytes_min = 64,
356 .period_bytes_max = (128*1024),
357 .periods_min = 1,
358 .periods_max = 1024,
359 .fifo_size = 0,
360};
361
Bhumika Goyalaec54652017-08-17 14:45:52 +0530362static const struct snd_pcm_hardware snd_ad1816a_capture = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 .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,
369 .rate_min = 4000,
370 .rate_max = 55200,
371 .channels_min = 1,
372 .channels_max = 2,
373 .buffer_bytes_max = (128*1024),
374 .period_bytes_min = 64,
375 .period_bytes_max = (128*1024),
376 .periods_min = 1,
377 .periods_max = 1024,
378 .fifo_size = 0,
379};
380
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100381static int snd_ad1816a_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100383 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 snd_ad1816a_close(chip, AD1816A_MODE_TIMER);
385 return 0;
386}
387
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100388static int snd_ad1816a_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100390 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 snd_ad1816a_open(chip, AD1816A_MODE_TIMER);
392 return 0;
393}
394
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100395static unsigned long snd_ad1816a_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Takashi Iwai622207d2008-08-08 17:11:45 +0200397 if (snd_BUG_ON(!timer))
398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 return 10000;
401}
402
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100403static int snd_ad1816a_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 unsigned short bits;
406 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100407 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 spin_lock_irqsave(&chip->lock, flags);
409 bits = snd_ad1816a_read(chip, AD1816A_INTERRUPT_ENABLE);
410
411 if (!(bits & AD1816A_TIMER_ENABLE)) {
412 snd_ad1816a_write(chip, AD1816A_TIMER_BASE_COUNT,
413 timer->sticks & 0xffff);
414
415 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
416 AD1816A_TIMER_ENABLE, 0xffff);
417 }
418 spin_unlock_irqrestore(&chip->lock, flags);
419 return 0;
420}
421
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100422static int snd_ad1816a_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 unsigned long flags;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100425 struct snd_ad1816a *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 spin_lock_irqsave(&chip->lock, flags);
427
428 snd_ad1816a_write_mask(chip, AD1816A_INTERRUPT_ENABLE,
429 AD1816A_TIMER_ENABLE, 0x0000);
430
431 spin_unlock_irqrestore(&chip->lock, flags);
432 return 0;
433}
434
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100435static struct snd_timer_hardware snd_ad1816a_timer_table = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .flags = SNDRV_TIMER_HW_AUTO,
437 .resolution = 10000,
438 .ticks = 65535,
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,
444};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100446static int snd_ad1816a_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100448 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
449 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int error;
451
452 if ((error = snd_ad1816a_open(chip, AD1816A_MODE_PLAYBACK)) < 0)
453 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 runtime->hw = snd_ad1816a_playback;
455 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
456 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
457 chip->playback_substream = substream;
458 return 0;
459}
460
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100461static int snd_ad1816a_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100463 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
464 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 int error;
466
467 if ((error = snd_ad1816a_open(chip, AD1816A_MODE_CAPTURE)) < 0)
468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 runtime->hw = snd_ad1816a_capture;
470 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
471 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
472 chip->capture_substream = substream;
473 return 0;
474}
475
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100476static int snd_ad1816a_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100478 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 chip->playback_substream = NULL;
481 snd_ad1816a_close(chip, AD1816A_MODE_PLAYBACK);
482 return 0;
483}
484
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100485static int snd_ad1816a_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100487 struct snd_ad1816a *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 chip->capture_substream = NULL;
490 snd_ad1816a_close(chip, AD1816A_MODE_CAPTURE);
491 return 0;
492}
493
494
Ondrej Zary6f0fa662012-08-19 23:27:26 +0200495static void snd_ad1816a_init(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 unsigned long flags;
498
499 spin_lock_irqsave(&chip->lock, flags);
500
501 snd_ad1816a_out(chip, AD1816A_INTERRUPT_STATUS, 0x00);
502 snd_ad1816a_out_mask(chip, AD1816A_PLAYBACK_CONFIG,
503 AD1816A_PLAYBACK_ENABLE | AD1816A_PLAYBACK_PIO, 0x00);
504 snd_ad1816a_out_mask(chip, AD1816A_CAPTURE_CONFIG,
505 AD1816A_CAPTURE_ENABLE | AD1816A_CAPTURE_PIO, 0x00);
506 snd_ad1816a_write(chip, AD1816A_INTERRUPT_ENABLE, 0x0000);
507 snd_ad1816a_write_mask(chip, AD1816A_CHIP_CONFIG,
508 AD1816A_CAPTURE_NOT_EQUAL | AD1816A_WSS_ENABLE, 0xffff);
509 snd_ad1816a_write(chip, AD1816A_DSP_CONFIG, 0x0000);
510 snd_ad1816a_write(chip, AD1816A_POWERDOWN_CTRL, 0x0000);
511
512 spin_unlock_irqrestore(&chip->lock, flags);
513}
514
Ondrej Zary6f0fa662012-08-19 23:27:26 +0200515#ifdef CONFIG_PM
516void snd_ad1816a_suspend(struct snd_ad1816a *chip)
517{
518 int reg;
519 unsigned long flags;
520
521 snd_pcm_suspend_all(chip->pcm);
522 spin_lock_irqsave(&chip->lock, flags);
523 for (reg = 0; reg < 48; reg++)
524 chip->image[reg] = snd_ad1816a_read(chip, reg);
525 spin_unlock_irqrestore(&chip->lock, flags);
526}
527
528void snd_ad1816a_resume(struct snd_ad1816a *chip)
529{
530 int reg;
531 unsigned long flags;
532
533 snd_ad1816a_init(chip);
534 spin_lock_irqsave(&chip->lock, flags);
535 for (reg = 0; reg < 48; reg++)
536 snd_ad1816a_write(chip, reg, chip->image[reg]);
537 spin_unlock_irqrestore(&chip->lock, flags);
538}
539#endif
540
Bill Pemberton1bff2922012-12-06 12:35:21 -0500541static int snd_ad1816a_probe(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 unsigned long flags;
544
545 spin_lock_irqsave(&chip->lock, flags);
546
547 switch (chip->version = snd_ad1816a_read(chip, AD1816A_VERSION_ID)) {
548 case 0:
549 chip->hardware = AD1816A_HW_AD1815;
550 break;
551 case 1:
552 chip->hardware = AD1816A_HW_AD18MAX10;
553 break;
554 case 3:
555 chip->hardware = AD1816A_HW_AD1816A;
556 break;
557 default:
558 chip->hardware = AD1816A_HW_AUTO;
559 }
560
561 spin_unlock_irqrestore(&chip->lock, flags);
562 return 0;
563}
564
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100565static int snd_ad1816a_free(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Takashi Iwaib1d57762005-10-10 11:56:31 +0200567 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (chip->irq >= 0)
569 free_irq(chip->irq, (void *) chip);
570 if (chip->dma1 >= 0) {
571 snd_dma_disable(chip->dma1);
572 free_dma(chip->dma1);
573 }
574 if (chip->dma2 >= 0) {
575 snd_dma_disable(chip->dma2);
576 free_dma(chip->dma2);
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return 0;
579}
580
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100581static int snd_ad1816a_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100583 struct snd_ad1816a *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return snd_ad1816a_free(chip);
585}
586
Bill Pemberton1bff2922012-12-06 12:35:21 -0500587static const char *snd_ad1816a_chip_id(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 switch (chip->hardware) {
590 case AD1816A_HW_AD1816A: return "AD1816A";
591 case AD1816A_HW_AD1815: return "AD1815";
592 case AD1816A_HW_AD18MAX10: return "AD18max10";
593 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100594 snd_printk(KERN_WARNING "Unknown chip version %d:%d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 chip->version, chip->hardware);
596 return "AD1816A - unknown";
597 }
598}
599
Bill Pemberton1bff2922012-12-06 12:35:21 -0500600int snd_ad1816a_create(struct snd_card *card,
601 unsigned long port, int irq, int dma1, int dma2,
602 struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100604 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 .dev_free = snd_ad1816a_dev_free,
606 };
607 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 chip->irq = -1;
610 chip->dma1 = -1;
611 chip->dma2 = -1;
612
613 if ((chip->res_port = request_region(port, 16, "AD1816A")) == NULL) {
614 snd_printk(KERN_ERR "ad1816a: can't grab port 0x%lx\n", port);
615 snd_ad1816a_free(chip);
616 return -EBUSY;
617 }
Yong Zhang88e24c32011-09-22 16:59:20 +0800618 if (request_irq(irq, snd_ad1816a_interrupt, 0, "AD1816A", (void *) chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 snd_printk(KERN_ERR "ad1816a: can't grab IRQ %d\n", irq);
620 snd_ad1816a_free(chip);
621 return -EBUSY;
622 }
623 chip->irq = irq;
624 if (request_dma(dma1, "AD1816A - 1")) {
625 snd_printk(KERN_ERR "ad1816a: can't grab DMA1 %d\n", dma1);
626 snd_ad1816a_free(chip);
627 return -EBUSY;
628 }
629 chip->dma1 = dma1;
630 if (request_dma(dma2, "AD1816A - 2")) {
631 snd_printk(KERN_ERR "ad1816a: can't grab DMA2 %d\n", dma2);
632 snd_ad1816a_free(chip);
633 return -EBUSY;
634 }
635 chip->dma2 = dma2;
636
637 chip->card = card;
638 chip->port = port;
639 spin_lock_init(&chip->lock);
640
641 if ((error = snd_ad1816a_probe(chip))) {
642 snd_ad1816a_free(chip);
643 return error;
644 }
645
646 snd_ad1816a_init(chip);
647
648 /* Register device */
649 if ((error = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
650 snd_ad1816a_free(chip);
651 return error;
652 }
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return 0;
655}
656
Arvind Yadav58a24fc2017-08-11 17:27:59 +0530657static const struct snd_pcm_ops snd_ad1816a_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 .open = snd_ad1816a_playback_open,
659 .close = snd_ad1816a_playback_close,
660 .ioctl = snd_pcm_lib_ioctl,
661 .hw_params = snd_ad1816a_hw_params,
662 .hw_free = snd_ad1816a_hw_free,
663 .prepare = snd_ad1816a_playback_prepare,
664 .trigger = snd_ad1816a_playback_trigger,
665 .pointer = snd_ad1816a_playback_pointer,
666};
667
Arvind Yadav58a24fc2017-08-11 17:27:59 +0530668static const struct snd_pcm_ops snd_ad1816a_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 .open = snd_ad1816a_capture_open,
670 .close = snd_ad1816a_capture_close,
671 .ioctl = snd_pcm_lib_ioctl,
672 .hw_params = snd_ad1816a_hw_params,
673 .hw_free = snd_ad1816a_hw_free,
674 .prepare = snd_ad1816a_capture_prepare,
675 .trigger = snd_ad1816a_capture_trigger,
676 .pointer = snd_ad1816a_capture_pointer,
677};
678
Lars-Peter Clausen7f605412015-01-02 12:24:36 +0100679int snd_ad1816a_pcm(struct snd_ad1816a *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 int error;
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100682 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 if ((error = snd_pcm_new(chip->card, "AD1816A", device, 1, 1, &pcm)))
685 return error;
686
687 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1816a_playback_ops);
688 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1816a_capture_ops);
689
690 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 pcm->info_flags = (chip->dma1 == chip->dma2 ) ? SNDRV_PCM_INFO_JOINT_DUPLEX : 0;
692
693 strcpy(pcm->name, snd_ad1816a_chip_id(chip));
694 snd_ad1816a_init(chip);
695
696 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
697 snd_dma_isa_data(),
698 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
699
700 chip->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
Lars-Peter Clausen7f605412015-01-02 12:24:36 +0100704int snd_ad1816a_timer(struct snd_ad1816a *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100706 struct snd_timer *timer;
707 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int error;
709
710 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
711 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
712 tid.card = chip->card->number;
713 tid.device = device;
714 tid.subdevice = 0;
715 if ((error = snd_timer_new(chip->card, "AD1816A", &tid, &timer)) < 0)
716 return error;
717 strcpy(timer->name, snd_ad1816a_chip_id(chip));
718 timer->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 chip->timer = timer;
720 timer->hw = snd_ad1816a_timer_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724/*
725 *
726 */
727
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100728static int snd_ad1816a_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Takashi Iwai1da0c472014-10-20 18:13:39 +0200730 static const char * const texts[8] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 "Line", "Mix", "CD", "Synth", "Video",
732 "Mic", "Phone",
733 };
734
Takashi Iwai1da0c472014-10-20 18:13:39 +0200735 return snd_ctl_enum_info(uinfo, 2, 7, texts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100738static int snd_ad1816a_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100740 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 unsigned long flags;
742 unsigned short val;
743
744 spin_lock_irqsave(&chip->lock, flags);
745 val = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL);
746 spin_unlock_irqrestore(&chip->lock, flags);
747 ucontrol->value.enumerated.item[0] = (val >> 12) & 7;
748 ucontrol->value.enumerated.item[1] = (val >> 4) & 7;
749 return 0;
750}
751
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100752static int snd_ad1816a_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100754 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 unsigned long flags;
756 unsigned short val;
757 int change;
758
759 if (ucontrol->value.enumerated.item[0] > 6 ||
760 ucontrol->value.enumerated.item[1] > 6)
761 return -EINVAL;
762 val = (ucontrol->value.enumerated.item[0] << 12) |
763 (ucontrol->value.enumerated.item[1] << 4);
764 spin_lock_irqsave(&chip->lock, flags);
765 change = snd_ad1816a_read(chip, AD1816A_ADC_SOURCE_SEL) != val;
766 snd_ad1816a_write(chip, AD1816A_ADC_SOURCE_SEL, val);
767 spin_unlock_irqrestore(&chip->lock, flags);
768 return change;
769}
770
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200771#define AD1816A_SINGLE_TLV(xname, reg, shift, mask, invert, xtlv) \
772{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
773 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
774 .name = xname, .info = snd_ad1816a_info_single, \
775 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
776 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
777 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778#define AD1816A_SINGLE(xname, reg, shift, mask, invert) \
779{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_single, \
780 .get = snd_ad1816a_get_single, .put = snd_ad1816a_put_single, \
781 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
782
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100783static int snd_ad1816a_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int mask = (kcontrol->private_value >> 16) & 0xff;
786
787 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
788 uinfo->count = 1;
789 uinfo->value.integer.min = 0;
790 uinfo->value.integer.max = mask;
791 return 0;
792}
793
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100794static int snd_ad1816a_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100796 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 unsigned long flags;
798 int reg = kcontrol->private_value & 0xff;
799 int shift = (kcontrol->private_value >> 8) & 0xff;
800 int mask = (kcontrol->private_value >> 16) & 0xff;
801 int invert = (kcontrol->private_value >> 24) & 0xff;
802
803 spin_lock_irqsave(&chip->lock, flags);
804 ucontrol->value.integer.value[0] = (snd_ad1816a_read(chip, reg) >> shift) & mask;
805 spin_unlock_irqrestore(&chip->lock, flags);
806 if (invert)
807 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
808 return 0;
809}
810
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100811static int snd_ad1816a_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100813 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned long flags;
815 int reg = kcontrol->private_value & 0xff;
816 int shift = (kcontrol->private_value >> 8) & 0xff;
817 int mask = (kcontrol->private_value >> 16) & 0xff;
818 int invert = (kcontrol->private_value >> 24) & 0xff;
819 int change;
820 unsigned short old_val, val;
821
822 val = (ucontrol->value.integer.value[0] & mask);
823 if (invert)
824 val = mask - val;
825 val <<= shift;
826 spin_lock_irqsave(&chip->lock, flags);
827 old_val = snd_ad1816a_read(chip, reg);
828 val = (old_val & ~(mask << shift)) | val;
829 change = val != old_val;
830 snd_ad1816a_write(chip, reg, val);
831 spin_unlock_irqrestore(&chip->lock, flags);
832 return change;
833}
834
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200835#define AD1816A_DOUBLE_TLV(xname, reg, shift_left, shift_right, mask, invert, xtlv) \
836{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
837 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
838 .name = xname, .info = snd_ad1816a_info_double, \
839 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
840 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24), \
841 .tlv = { .p = (xtlv) } }
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843#define AD1816A_DOUBLE(xname, reg, shift_left, shift_right, mask, invert) \
844{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_ad1816a_info_double, \
845 .get = snd_ad1816a_get_double, .put = snd_ad1816a_put_double, \
846 .private_value = reg | (shift_left << 8) | (shift_right << 12) | (mask << 16) | (invert << 24) }
847
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100848static int snd_ad1816a_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 int mask = (kcontrol->private_value >> 16) & 0xff;
851
852 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
853 uinfo->count = 2;
854 uinfo->value.integer.min = 0;
855 uinfo->value.integer.max = mask;
856 return 0;
857}
858
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100859static int snd_ad1816a_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100861 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned long flags;
863 int reg = kcontrol->private_value & 0xff;
864 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
865 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
866 int mask = (kcontrol->private_value >> 16) & 0xff;
867 int invert = (kcontrol->private_value >> 24) & 0xff;
868 unsigned short val;
869
870 spin_lock_irqsave(&chip->lock, flags);
871 val = snd_ad1816a_read(chip, reg);
872 ucontrol->value.integer.value[0] = (val >> shift_left) & mask;
873 ucontrol->value.integer.value[1] = (val >> shift_right) & mask;
874 spin_unlock_irqrestore(&chip->lock, flags);
875 if (invert) {
876 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
877 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
878 }
879 return 0;
880}
881
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100882static int snd_ad1816a_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100884 struct snd_ad1816a *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 unsigned long flags;
886 int reg = kcontrol->private_value & 0xff;
887 int shift_left = (kcontrol->private_value >> 8) & 0x0f;
888 int shift_right = (kcontrol->private_value >> 12) & 0x0f;
889 int mask = (kcontrol->private_value >> 16) & 0xff;
890 int invert = (kcontrol->private_value >> 24) & 0xff;
891 int change;
892 unsigned short old_val, val1, val2;
893
894 val1 = ucontrol->value.integer.value[0] & mask;
895 val2 = ucontrol->value.integer.value[1] & mask;
896 if (invert) {
897 val1 = mask - val1;
898 val2 = mask - val2;
899 }
900 val1 <<= shift_left;
901 val2 <<= shift_right;
902 spin_lock_irqsave(&chip->lock, flags);
903 old_val = snd_ad1816a_read(chip, reg);
904 val1 = (old_val & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
905 change = val1 != old_val;
906 snd_ad1816a_write(chip, reg, val1);
907 spin_unlock_irqrestore(&chip->lock, flags);
908 return change;
909}
910
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100911static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
912static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
913static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
914static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
915static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200916
Bill Pemberton1bff2922012-12-06 12:35:21 -0500917static struct snd_kcontrol_new snd_ad1816a_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918AD1816A_DOUBLE("Master Playback Switch", AD1816A_MASTER_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200919AD1816A_DOUBLE_TLV("Master Playback Volume", AD1816A_MASTER_ATT, 8, 0, 31, 1,
920 db_scale_5bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921AD1816A_DOUBLE("PCM Playback Switch", AD1816A_VOICE_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200922AD1816A_DOUBLE_TLV("PCM Playback Volume", AD1816A_VOICE_ATT, 8, 0, 63, 1,
923 db_scale_6bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924AD1816A_DOUBLE("Line Playback Switch", AD1816A_LINE_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200925AD1816A_DOUBLE_TLV("Line Playback Volume", AD1816A_LINE_GAIN_ATT, 8, 0, 31, 1,
926 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927AD1816A_DOUBLE("CD Playback Switch", AD1816A_CD_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200928AD1816A_DOUBLE_TLV("CD Playback Volume", AD1816A_CD_GAIN_ATT, 8, 0, 31, 1,
929 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930AD1816A_DOUBLE("Synth Playback Switch", AD1816A_SYNTH_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200931AD1816A_DOUBLE_TLV("Synth Playback Volume", AD1816A_SYNTH_GAIN_ATT, 8, 0, 31, 1,
932 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933AD1816A_DOUBLE("FM Playback Switch", AD1816A_FM_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200934AD1816A_DOUBLE_TLV("FM Playback Volume", AD1816A_FM_ATT, 8, 0, 63, 1,
935 db_scale_6bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936AD1816A_SINGLE("Mic Playback Switch", AD1816A_MIC_GAIN_ATT, 15, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200937AD1816A_SINGLE_TLV("Mic Playback Volume", AD1816A_MIC_GAIN_ATT, 8, 31, 1,
938 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939AD1816A_SINGLE("Mic Boost", AD1816A_MIC_GAIN_ATT, 14, 1, 0),
940AD1816A_DOUBLE("Video Playback Switch", AD1816A_VID_GAIN_ATT, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200941AD1816A_DOUBLE_TLV("Video Playback Volume", AD1816A_VID_GAIN_ATT, 8, 0, 31, 1,
942 db_scale_5bit_12db_max),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943AD1816A_SINGLE("Phone Capture Switch", AD1816A_PHONE_IN_GAIN_ATT, 15, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200944AD1816A_SINGLE_TLV("Phone Capture Volume", AD1816A_PHONE_IN_GAIN_ATT, 0, 15, 1,
945 db_scale_4bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946AD1816A_SINGLE("Phone Playback Switch", AD1816A_PHONE_OUT_ATT, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200947AD1816A_SINGLE_TLV("Phone Playback Volume", AD1816A_PHONE_OUT_ATT, 0, 31, 1,
948 db_scale_5bit),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
951 .name = "Capture Source",
952 .info = snd_ad1816a_info_mux,
953 .get = snd_ad1816a_get_mux,
954 .put = snd_ad1816a_put_mux,
955},
956AD1816A_DOUBLE("Capture Switch", AD1816A_ADC_PGA, 15, 7, 1, 1),
Takashi Iwai0e7febf2006-08-22 13:16:01 +0200957AD1816A_DOUBLE_TLV("Capture Volume", AD1816A_ADC_PGA, 8, 0, 15, 0,
958 db_scale_rec_gain),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959AD1816A_SINGLE("3D Control - Switch", AD1816A_3D_PHAT_CTRL, 15, 1, 1),
960AD1816A_SINGLE("3D Control - Level", AD1816A_3D_PHAT_CTRL, 0, 15, 0),
961};
962
Bill Pemberton1bff2922012-12-06 12:35:21 -0500963int snd_ad1816a_mixer(struct snd_ad1816a *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Takashi Iwaicbdd0dd2005-11-17 14:28:35 +0100965 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned int idx;
967 int err;
968
Takashi Iwai622207d2008-08-08 17:11:45 +0200969 if (snd_BUG_ON(!chip || !chip->card))
970 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 card = chip->card;
973
974 strcpy(card->mixername, snd_ad1816a_chip_id(chip));
975
976 for (idx = 0; idx < ARRAY_SIZE(snd_ad1816a_controls); idx++) {
977 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ad1816a_controls[idx], chip))) < 0)
978 return err;
979 }
980 return 0;
981}