blob: 07676200496ae607313354d886378b4f549af4e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Routines for control of ESS ES1688/688/488 chip
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/init.h>
23#include <linux/interrupt.h>
24#include <linux/delay.h>
25#include <linux/slab.h>
26#include <linux/ioport.h>
27#include <sound/core.h>
28#include <sound/es1688.h>
29#include <sound/initval.h>
30
31#include <asm/io.h>
32#include <asm/dma.h>
33
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020034MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
36MODULE_LICENSE("GPL");
37
Takashi Iwaid3a7e472005-11-17 14:31:42 +010038static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 int i;
41
42 for (i = 10000; i; i--)
43 if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
44 outb(val, ES1688P(chip, COMMAND));
45 return 1;
46 }
47#ifdef CONFIG_SND_DEBUG
Takashi Iwai4c9f1d32009-02-05 15:47:51 +010048 printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
50 return 0;
51}
52
Takashi Iwaid3a7e472005-11-17 14:31:42 +010053static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int i;
56
57 for (i = 1000; i; i--)
58 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
59 return inb(ES1688P(chip, READ));
60 snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
61 return -ENODEV;
62}
63
Takashi Iwaid3a7e472005-11-17 14:31:42 +010064static int snd_es1688_write(struct snd_es1688 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 unsigned char reg, unsigned char data)
66{
67 if (!snd_es1688_dsp_command(chip, reg))
68 return 0;
69 return snd_es1688_dsp_command(chip, data);
70}
71
Takashi Iwaid3a7e472005-11-17 14:31:42 +010072static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 /* Read a byte from an extended mode register of ES1688 */
75 if (!snd_es1688_dsp_command(chip, 0xc0))
76 return -1;
77 if (!snd_es1688_dsp_command(chip, reg))
78 return -1;
79 return snd_es1688_dsp_get_byte(chip);
80}
81
Takashi Iwaid3a7e472005-11-17 14:31:42 +010082void snd_es1688_mixer_write(struct snd_es1688 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned char reg, unsigned char data)
84{
85 outb(reg, ES1688P(chip, MIXER_ADDR));
86 udelay(10);
87 outb(data, ES1688P(chip, MIXER_DATA));
88 udelay(10);
89}
90
Takashi Iwaid3a7e472005-11-17 14:31:42 +010091static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 unsigned char result;
94
95 outb(reg, ES1688P(chip, MIXER_ADDR));
96 udelay(10);
97 result = inb(ES1688P(chip, MIXER_DATA));
98 udelay(10);
99 return result;
100}
101
Krzysztof Helta20971b2010-05-10 09:47:32 +0200102int snd_es1688_reset(struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
104 int i;
105
106 outb(3, ES1688P(chip, RESET)); /* valid only for ESS chips, SB -> 1 */
107 udelay(10);
108 outb(0, ES1688P(chip, RESET));
109 udelay(30);
110 for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
111 if (inb(ES1688P(chip, READ)) != 0xaa) {
112 snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
113 return -ENODEV;
114 }
115 snd_es1688_dsp_command(chip, 0xc6); /* enable extended mode */
116 return 0;
117}
Krzysztof Helta20971b2010-05-10 09:47:32 +0200118EXPORT_SYMBOL(snd_es1688_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100120static int snd_es1688_probe(struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 unsigned long flags;
123 unsigned short major, minor, hw;
124 int i;
125
126 /*
127 * initialization sequence
128 */
129
130 spin_lock_irqsave(&chip->reg_lock, flags); /* Some ESS1688 cards need this */
131 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
132 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
133 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
134 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
135 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
136 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
137 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
138 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
139 inb(ES1688P(chip, ENABLE2)); /* ENABLE2 */
140 inb(ES1688P(chip, ENABLE1)); /* ENABLE1 */
141 inb(ES1688P(chip, ENABLE0)); /* ENABLE0 */
142
143 if (snd_es1688_reset(chip) < 0) {
144 snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
145 spin_unlock_irqrestore(&chip->reg_lock, flags);
146 return -ENODEV;
147 }
148 snd_es1688_dsp_command(chip, 0xe7); /* return identification */
149
150 for (i = 1000, major = minor = 0; i; i--) {
151 if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
152 if (major == 0) {
153 major = inb(ES1688P(chip, READ));
154 } else {
155 minor = inb(ES1688P(chip, READ));
156 }
157 }
158 }
159
160 spin_unlock_irqrestore(&chip->reg_lock, flags);
161
162 snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
163
164 chip->version = (major << 8) | minor;
165 if (!chip->version)
166 return -ENODEV; /* probably SB */
167
168 hw = ES1688_HW_AUTO;
169 switch (chip->version & 0xfff0) {
170 case 0x4880:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100171 snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
172 "but driver is in another place\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return -ENODEV;
174 case 0x6880:
175 hw = (chip->version & 0x0f) >= 8 ? ES1688_HW_1688 : ES1688_HW_688;
176 break;
177 default:
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100178 snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
179 "with version 0x%x (Jazz16 soundcard?)\n",
180 chip->port, chip->version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return -ENODEV;
182 }
183
184 spin_lock_irqsave(&chip->reg_lock, flags);
185 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
186 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
187 spin_unlock_irqrestore(&chip->reg_lock, flags);
188
189 /* enable joystick, but disable OPL3 */
190 spin_lock_irqsave(&chip->mixer_lock, flags);
191 snd_es1688_mixer_write(chip, 0x40, 0x01);
192 spin_unlock_irqrestore(&chip->mixer_lock, flags);
193
194 return 0;
195}
196
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100197static int snd_es1688_init(struct snd_es1688 * chip, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
200 unsigned long flags;
201 int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
202
203 /* ok.. setup MPU-401 port and joystick and OPL3 */
204 cfg = 0x01; /* enable joystick, but disable OPL3 */
205 if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
206 tmp = (chip->mpu_port & 0x0f0) >> 4;
207 if (tmp <= 3) {
208 switch (chip->mpu_irq) {
209 case 9:
210 tmp1 = 4;
211 break;
212 case 5:
213 tmp1 = 5;
214 break;
215 case 7:
216 tmp1 = 6;
217 break;
218 case 10:
219 tmp1 = 7;
220 break;
221 default:
222 tmp1 = 0;
223 }
224 if (tmp1) {
225 cfg |= (tmp << 3) | (tmp1 << 5);
226 }
227 }
228 }
229#if 0
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100230 snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#endif
232 spin_lock_irqsave(&chip->reg_lock, flags);
233 snd_es1688_mixer_write(chip, 0x40, cfg);
234 spin_unlock_irqrestore(&chip->reg_lock, flags);
235 /* --- */
236 spin_lock_irqsave(&chip->reg_lock, flags);
237 snd_es1688_read(chip, 0xb1);
238 snd_es1688_read(chip, 0xb2);
239 spin_unlock_irqrestore(&chip->reg_lock, flags);
240 if (enable) {
241 cfg = 0xf0; /* enable only DMA counter interrupt */
242 irq_bits = irqs[chip->irq & 0x0f];
243 if (irq_bits < 0) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100244 snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
245 "for ES1688 chip!!\n",
246 chip->port, chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247#if 0
248 irq_bits = 0;
249 cfg = 0x10;
250#endif
251 return -EINVAL;
252 }
253 spin_lock_irqsave(&chip->reg_lock, flags);
254 snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
255 spin_unlock_irqrestore(&chip->reg_lock, flags);
256 cfg = 0xf0; /* extended mode DMA enable */
257 dma = chip->dma8;
258 if (dma > 3 || dma == 2) {
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100259 snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
260 "for ES1688 chip!!\n", chip->port, dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261#if 0
262 dma_bits = 0;
263 cfg = 0x00; /* disable all DMA */
264#endif
265 return -EINVAL;
266 } else {
267 dma_bits = dma;
268 if (dma != 3)
269 dma_bits++;
270 }
271 spin_lock_irqsave(&chip->reg_lock, flags);
272 snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
273 spin_unlock_irqrestore(&chip->reg_lock, flags);
274 } else {
275 spin_lock_irqsave(&chip->reg_lock, flags);
276 snd_es1688_write(chip, 0xb1, 0x10); /* disable IRQ */
277 snd_es1688_write(chip, 0xb2, 0x00); /* disable DMA */
278 spin_unlock_irqrestore(&chip->reg_lock, flags);
279 }
280 spin_lock_irqsave(&chip->reg_lock, flags);
281 snd_es1688_read(chip, 0xb1);
282 snd_es1688_read(chip, 0xb2);
283 snd_es1688_reset(chip);
284 spin_unlock_irqrestore(&chip->reg_lock, flags);
285 return 0;
286}
287
288/*
289
290 */
291
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100292static struct snd_ratnum clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 {
294 .num = 795444,
295 .den_min = 1,
296 .den_max = 128,
297 .den_step = 1,
298 },
299 {
300 .num = 397722,
301 .den_min = 1,
302 .den_max = 128,
303 .den_step = 1,
304 }
305};
306
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100307static struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 .nrats = 2,
309 .rats = clocks,
310};
311
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100312static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100314 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 unsigned int bits, divider;
316
317 if (runtime->rate_num == clocks[0].num)
318 bits = 256 - runtime->rate_den;
319 else
320 bits = 128 - runtime->rate_den;
321 /* set filter register */
322 divider = 256 - 7160000*20/(8*82*runtime->rate);
323 /* write result to hardware */
324 snd_es1688_write(chip, 0xa1, bits);
325 snd_es1688_write(chip, 0xa2, divider);
326}
327
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100328static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned int cmd, void *arg)
330{
331 return snd_pcm_lib_ioctl(substream, cmd, arg);
332}
333
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100334static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 int val;
337
338 if (cmd == SNDRV_PCM_TRIGGER_STOP) {
339 value = 0x00;
340 } else if (cmd != SNDRV_PCM_TRIGGER_START) {
341 return -EINVAL;
342 }
343 spin_lock(&chip->reg_lock);
344 chip->trigger_value = value;
345 val = snd_es1688_read(chip, 0xb8);
346 if ((val < 0) || (val & 0x0f) == value) {
347 spin_unlock(&chip->reg_lock);
348 return -EINVAL; /* something is wrong */
349 }
350#if 0
Takashi Iwai4c9f1d32009-02-05 15:47:51 +0100351 printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
352 printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
353 snd_dma_pointer(chip->dma8, chip->dma_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
355 snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
356 spin_unlock(&chip->reg_lock);
357 return 0;
358}
359
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100360static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
361 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
364}
365
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100366static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 return snd_pcm_lib_free_pages(substream);
369}
370
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100371static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 unsigned long flags;
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100374 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
375 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
377 unsigned int count = snd_pcm_lib_period_bytes(substream);
378
379 chip->dma_size = size;
380 spin_lock_irqsave(&chip->reg_lock, flags);
381 snd_es1688_reset(chip);
382 snd_es1688_set_rate(chip, substream);
383 snd_es1688_write(chip, 0xb8, 4); /* auto init DMA mode */
384 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
385 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
386 if (runtime->channels == 1) {
387 if (snd_pcm_format_width(runtime->format) == 8) {
388 /* 8. bit mono */
389 snd_es1688_write(chip, 0xb6, 0x80);
390 snd_es1688_write(chip, 0xb7, 0x51);
391 snd_es1688_write(chip, 0xb7, 0xd0);
392 } else {
393 /* 16. bit mono */
394 snd_es1688_write(chip, 0xb6, 0x00);
395 snd_es1688_write(chip, 0xb7, 0x71);
396 snd_es1688_write(chip, 0xb7, 0xf4);
397 }
398 } else {
399 if (snd_pcm_format_width(runtime->format) == 8) {
400 /* 8. bit stereo */
401 snd_es1688_write(chip, 0xb6, 0x80);
402 snd_es1688_write(chip, 0xb7, 0x51);
403 snd_es1688_write(chip, 0xb7, 0x98);
404 } else {
405 /* 16. bit stereo */
406 snd_es1688_write(chip, 0xb6, 0x00);
407 snd_es1688_write(chip, 0xb7, 0x71);
408 snd_es1688_write(chip, 0xb7, 0xbc);
409 }
410 }
411 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
412 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
413 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
414 spin_unlock_irqrestore(&chip->reg_lock, flags);
415 /* --- */
416 count = -count;
417 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
418 spin_lock_irqsave(&chip->reg_lock, flags);
419 snd_es1688_write(chip, 0xa4, (unsigned char) count);
420 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
421 spin_unlock_irqrestore(&chip->reg_lock, flags);
422 return 0;
423}
424
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100425static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int cmd)
427{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100428 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return snd_es1688_trigger(chip, cmd, 0x05);
430}
431
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100432static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 unsigned long flags;
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100435 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
436 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
438 unsigned int count = snd_pcm_lib_period_bytes(substream);
439
440 chip->dma_size = size;
441 spin_lock_irqsave(&chip->reg_lock, flags);
442 snd_es1688_reset(chip);
443 snd_es1688_set_rate(chip, substream);
444 snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
445 snd_es1688_write(chip, 0xb8, 0x0e); /* auto init DMA mode */
446 snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
447 snd_es1688_write(chip, 0xb9, 2); /* demand mode (4 bytes/request) */
448 if (runtime->channels == 1) {
449 if (snd_pcm_format_width(runtime->format) == 8) {
450 /* 8. bit mono */
451 snd_es1688_write(chip, 0xb7, 0x51);
452 snd_es1688_write(chip, 0xb7, 0xd0);
453 } else {
454 /* 16. bit mono */
455 snd_es1688_write(chip, 0xb7, 0x71);
456 snd_es1688_write(chip, 0xb7, 0xf4);
457 }
458 } else {
459 if (snd_pcm_format_width(runtime->format) == 8) {
460 /* 8. bit stereo */
461 snd_es1688_write(chip, 0xb7, 0x51);
462 snd_es1688_write(chip, 0xb7, 0x98);
463 } else {
464 /* 16. bit stereo */
465 snd_es1688_write(chip, 0xb7, 0x71);
466 snd_es1688_write(chip, 0xb7, 0xbc);
467 }
468 }
469 snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
470 snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
471 spin_unlock_irqrestore(&chip->reg_lock, flags);
472 /* --- */
473 count = -count;
474 snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
475 spin_lock_irqsave(&chip->reg_lock, flags);
476 snd_es1688_write(chip, 0xa4, (unsigned char) count);
477 snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
478 spin_unlock_irqrestore(&chip->reg_lock, flags);
479 return 0;
480}
481
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100482static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 int cmd)
484{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100485 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return snd_es1688_trigger(chip, cmd, 0x0f);
487}
488
David Howells7d12e782006-10-05 14:55:46 +0100489static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100491 struct snd_es1688 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (chip->trigger_value == 0x05) /* ok.. playback is active */
494 snd_pcm_period_elapsed(chip->playback_substream);
495 if (chip->trigger_value == 0x0f) /* ok.. capture is active */
496 snd_pcm_period_elapsed(chip->capture_substream);
497
498 inb(ES1688P(chip, DATA_AVAIL)); /* ack interrupt */
499 return IRQ_HANDLED;
500}
501
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100502static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100504 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 size_t ptr;
506
507 if (chip->trigger_value != 0x05)
508 return 0;
509 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
510 return bytes_to_frames(substream->runtime, ptr);
511}
512
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100513static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100515 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 size_t ptr;
517
518 if (chip->trigger_value != 0x0f)
519 return 0;
520 ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
521 return bytes_to_frames(substream->runtime, ptr);
522}
523
524/*
525
526 */
527
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100528static struct snd_pcm_hardware snd_es1688_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
531 SNDRV_PCM_INFO_MMAP_VALID),
532 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
533 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
534 .rate_min = 4000,
535 .rate_max = 48000,
536 .channels_min = 1,
537 .channels_max = 2,
538 .buffer_bytes_max = 65536,
539 .period_bytes_min = 64,
540 .period_bytes_max = 65536,
541 .periods_min = 1,
542 .periods_max = 1024,
543 .fifo_size = 0,
544};
545
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100546static struct snd_pcm_hardware snd_es1688_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
549 SNDRV_PCM_INFO_MMAP_VALID),
550 .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
551 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
552 .rate_min = 4000,
553 .rate_max = 48000,
554 .channels_min = 1,
555 .channels_max = 2,
556 .buffer_bytes_max = 65536,
557 .period_bytes_min = 64,
558 .period_bytes_max = 65536,
559 .periods_min = 1,
560 .periods_max = 1024,
561 .fifo_size = 0,
562};
563
564/*
565
566 */
567
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100568static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100570 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
571 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 if (chip->capture_substream != NULL)
574 return -EAGAIN;
575 chip->playback_substream = substream;
576 runtime->hw = snd_es1688_playback;
577 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
578 &hw_constraints_clocks);
579 return 0;
580}
581
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100582static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100584 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
585 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (chip->playback_substream != NULL)
588 return -EAGAIN;
589 chip->capture_substream = substream;
590 runtime->hw = snd_es1688_capture;
591 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
592 &hw_constraints_clocks);
593 return 0;
594}
595
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100596static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100598 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 chip->playback_substream = NULL;
601 return 0;
602}
603
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100604static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100606 struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 chip->capture_substream = NULL;
609 return 0;
610}
611
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100612static int snd_es1688_free(struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 if (chip->res_port) {
615 snd_es1688_init(chip, 0);
Takashi Iwaib1d57762005-10-10 11:56:31 +0200616 release_and_free_resource(chip->res_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 if (chip->irq >= 0)
619 free_irq(chip->irq, (void *) chip);
620 if (chip->dma8 >= 0) {
621 disable_dma(chip->dma8);
622 free_dma(chip->dma8);
623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return 0;
625}
626
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100627static int snd_es1688_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100629 struct snd_es1688 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return snd_es1688_free(chip);
631}
632
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100633static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 static char tmp[16];
636 sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
637 return tmp;
638}
639
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100640int snd_es1688_create(struct snd_card *card,
Krzysztof Helt396fa822010-05-09 20:35:44 +0200641 struct snd_es1688 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned long port,
643 unsigned long mpu_port,
644 int irq,
645 int mpu_irq,
646 int dma8,
Krzysztof Helt396fa822010-05-09 20:35:44 +0200647 unsigned short hardware)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100649 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 .dev_free = snd_es1688_dev_free,
651 };
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int err;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (chip == NULL)
656 return -ENOMEM;
657 chip->irq = -1;
658 chip->dma8 = -1;
659
660 if ((chip->res_port = request_region(port + 4, 12, "ES1688")) == NULL) {
661 snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return -EBUSY;
663 }
Thomas Gleixner65ca68b2006-07-01 19:29:46 -0700664 if (request_irq(irq, snd_es1688_interrupt, IRQF_DISABLED, "ES1688", (void *) chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -EBUSY;
667 }
668 chip->irq = irq;
669 if (request_dma(dma8, "ES1688")) {
670 snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EBUSY;
672 }
673 chip->dma8 = dma8;
674
675 spin_lock_init(&chip->reg_lock);
676 spin_lock_init(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 chip->port = port;
678 mpu_port &= ~0x000f;
679 if (mpu_port < 0x300 || mpu_port > 0x330)
680 mpu_port = 0;
681 chip->mpu_port = mpu_port;
682 chip->mpu_irq = mpu_irq;
683 chip->hardware = hardware;
684
Krzysztof Helt396fa822010-05-09 20:35:44 +0200685 err = snd_es1688_probe(chip);
686 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return err;
Krzysztof Helt396fa822010-05-09 20:35:44 +0200688
689 err = snd_es1688_init(chip, 1);
690 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* Register device */
Krzysztof Helt396fa822010-05-09 20:35:44 +0200694 return snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100697static struct snd_pcm_ops snd_es1688_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 .open = snd_es1688_playback_open,
699 .close = snd_es1688_playback_close,
700 .ioctl = snd_es1688_ioctl,
701 .hw_params = snd_es1688_hw_params,
702 .hw_free = snd_es1688_hw_free,
703 .prepare = snd_es1688_playback_prepare,
704 .trigger = snd_es1688_playback_trigger,
705 .pointer = snd_es1688_playback_pointer,
706};
707
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100708static struct snd_pcm_ops snd_es1688_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 .open = snd_es1688_capture_open,
710 .close = snd_es1688_capture_close,
711 .ioctl = snd_es1688_ioctl,
712 .hw_params = snd_es1688_hw_params,
713 .hw_free = snd_es1688_hw_free,
714 .prepare = snd_es1688_capture_prepare,
715 .trigger = snd_es1688_capture_trigger,
716 .pointer = snd_es1688_capture_pointer,
717};
718
Krzysztof Helt396fa822010-05-09 20:35:44 +0200719int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip,
720 int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100722 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int err;
724
Krzysztof Helt396fa822010-05-09 20:35:44 +0200725 err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
726 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return err;
728
729 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
730 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
731
732 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
734 sprintf(pcm->name, snd_es1688_chip_id(chip));
735 chip->pcm = pcm;
736
737 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
738 snd_dma_isa_data(),
739 64*1024, 64*1024);
740
741 if (rpcm)
742 *rpcm = pcm;
743 return 0;
744}
745
746/*
747 * MIXER part
748 */
749
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100750static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 static char *texts[9] = {
753 "Mic", "Mic Master", "CD", "AOUT",
754 "Mic1", "Mix", "Line", "Master"
755 };
756
757 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
758 uinfo->count = 1;
759 uinfo->value.enumerated.items = 8;
760 if (uinfo->value.enumerated.item > 7)
761 uinfo->value.enumerated.item = 7;
762 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
763 return 0;
764}
765
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100766static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100768 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
770 return 0;
771}
772
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100773static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100775 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 unsigned long flags;
777 unsigned char oval, nval;
778 int change;
779
780 if (ucontrol->value.enumerated.item[0] > 8)
781 return -EINVAL;
782 spin_lock_irqsave(&chip->reg_lock, flags);
783 oval = snd_es1688_mixer_read(chip, ES1688_REC_DEV);
784 nval = (ucontrol->value.enumerated.item[0] & 7) | (oval & ~15);
785 change = nval != oval;
786 if (change)
787 snd_es1688_mixer_write(chip, ES1688_REC_DEV, nval);
788 spin_unlock_irqrestore(&chip->reg_lock, flags);
789 return change;
790}
791
792#define ES1688_SINGLE(xname, xindex, reg, shift, mask, invert) \
793{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
794 .info = snd_es1688_info_single, \
795 .get = snd_es1688_get_single, .put = snd_es1688_put_single, \
796 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
797
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100798static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 int mask = (kcontrol->private_value >> 16) & 0xff;
801
802 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
803 uinfo->count = 1;
804 uinfo->value.integer.min = 0;
805 uinfo->value.integer.max = mask;
806 return 0;
807}
808
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100809static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100811 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long flags;
813 int reg = kcontrol->private_value & 0xff;
814 int shift = (kcontrol->private_value >> 8) & 0xff;
815 int mask = (kcontrol->private_value >> 16) & 0xff;
816 int invert = (kcontrol->private_value >> 24) & 0xff;
817
818 spin_lock_irqsave(&chip->reg_lock, flags);
819 ucontrol->value.integer.value[0] = (snd_es1688_mixer_read(chip, reg) >> shift) & mask;
820 spin_unlock_irqrestore(&chip->reg_lock, flags);
821 if (invert)
822 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
823 return 0;
824}
825
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100826static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100828 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 unsigned long flags;
830 int reg = kcontrol->private_value & 0xff;
831 int shift = (kcontrol->private_value >> 8) & 0xff;
832 int mask = (kcontrol->private_value >> 16) & 0xff;
833 int invert = (kcontrol->private_value >> 24) & 0xff;
834 int change;
835 unsigned char oval, nval;
836
837 nval = (ucontrol->value.integer.value[0] & mask);
838 if (invert)
839 nval = mask - nval;
840 nval <<= shift;
841 spin_lock_irqsave(&chip->reg_lock, flags);
842 oval = snd_es1688_mixer_read(chip, reg);
843 nval = (oval & ~(mask << shift)) | nval;
844 change = nval != oval;
845 if (change)
846 snd_es1688_mixer_write(chip, reg, nval);
847 spin_unlock_irqrestore(&chip->reg_lock, flags);
848 return change;
849}
850
851#define ES1688_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
852{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
853 .info = snd_es1688_info_double, \
854 .get = snd_es1688_get_double, .put = snd_es1688_put_double, \
855 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
856
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100857static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 int mask = (kcontrol->private_value >> 24) & 0xff;
860
861 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
862 uinfo->count = 2;
863 uinfo->value.integer.min = 0;
864 uinfo->value.integer.max = mask;
865 return 0;
866}
867
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100868static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100870 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 unsigned long flags;
872 int left_reg = kcontrol->private_value & 0xff;
873 int right_reg = (kcontrol->private_value >> 8) & 0xff;
874 int shift_left = (kcontrol->private_value >> 16) & 0x07;
875 int shift_right = (kcontrol->private_value >> 19) & 0x07;
876 int mask = (kcontrol->private_value >> 24) & 0xff;
877 int invert = (kcontrol->private_value >> 22) & 1;
878 unsigned char left, right;
879
880 spin_lock_irqsave(&chip->reg_lock, flags);
881 if (left_reg < 0xa0)
882 left = snd_es1688_mixer_read(chip, left_reg);
883 else
884 left = snd_es1688_read(chip, left_reg);
885 if (left_reg != right_reg) {
886 if (right_reg < 0xa0)
887 right = snd_es1688_mixer_read(chip, right_reg);
888 else
889 right = snd_es1688_read(chip, right_reg);
890 } else
891 right = left;
892 spin_unlock_irqrestore(&chip->reg_lock, flags);
893 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
894 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
895 if (invert) {
896 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
897 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
898 }
899 return 0;
900}
901
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100902static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100904 struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 unsigned long flags;
906 int left_reg = kcontrol->private_value & 0xff;
907 int right_reg = (kcontrol->private_value >> 8) & 0xff;
908 int shift_left = (kcontrol->private_value >> 16) & 0x07;
909 int shift_right = (kcontrol->private_value >> 19) & 0x07;
910 int mask = (kcontrol->private_value >> 24) & 0xff;
911 int invert = (kcontrol->private_value >> 22) & 1;
912 int change;
913 unsigned char val1, val2, oval1, oval2;
914
915 val1 = ucontrol->value.integer.value[0] & mask;
916 val2 = ucontrol->value.integer.value[1] & mask;
917 if (invert) {
918 val1 = mask - val1;
919 val2 = mask - val2;
920 }
921 val1 <<= shift_left;
922 val2 <<= shift_right;
923 spin_lock_irqsave(&chip->reg_lock, flags);
924 if (left_reg != right_reg) {
925 if (left_reg < 0xa0)
926 oval1 = snd_es1688_mixer_read(chip, left_reg);
927 else
928 oval1 = snd_es1688_read(chip, left_reg);
929 if (right_reg < 0xa0)
930 oval2 = snd_es1688_mixer_read(chip, right_reg);
931 else
932 oval2 = snd_es1688_read(chip, right_reg);
933 val1 = (oval1 & ~(mask << shift_left)) | val1;
934 val2 = (oval2 & ~(mask << shift_right)) | val2;
935 change = val1 != oval1 || val2 != oval2;
936 if (change) {
937 if (left_reg < 0xa0)
938 snd_es1688_mixer_write(chip, left_reg, val1);
939 else
940 snd_es1688_write(chip, left_reg, val1);
941 if (right_reg < 0xa0)
942 snd_es1688_mixer_write(chip, right_reg, val1);
943 else
944 snd_es1688_write(chip, right_reg, val1);
945 }
946 } else {
947 if (left_reg < 0xa0)
948 oval1 = snd_es1688_mixer_read(chip, left_reg);
949 else
950 oval1 = snd_es1688_read(chip, left_reg);
951 val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
952 change = val1 != oval1;
953 if (change) {
954 if (left_reg < 0xa0)
955 snd_es1688_mixer_write(chip, left_reg, val1);
956 else
957 snd_es1688_write(chip, left_reg, val1);
958 }
959
960 }
961 spin_unlock_irqrestore(&chip->reg_lock, flags);
962 return change;
963}
964
Takashi Iwaid3a7e472005-11-17 14:31:42 +0100965static struct snd_kcontrol_new snd_es1688_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
967ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
968ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
969ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
970ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
971ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
972ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
Jaroslav Kyselad355c82a2009-11-03 15:47:25 +0100973ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
975ES1688_SINGLE("Capture Switch", 0, ES1688_REC_DEV, 4, 1, 1),
976{
977 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
978 .name = "Capture Source",
979 .info = snd_es1688_info_mux,
980 .get = snd_es1688_get_mux,
981 .put = snd_es1688_put_mux,
982},
983};
984
985#define ES1688_INIT_TABLE_SIZE (sizeof(snd_es1688_init_table)/2)
986
987static unsigned char snd_es1688_init_table[][2] = {
988 { ES1688_MASTER_DEV, 0 },
989 { ES1688_PCM_DEV, 0 },
990 { ES1688_LINE_DEV, 0 },
991 { ES1688_CD_DEV, 0 },
992 { ES1688_FM_DEV, 0 },
993 { ES1688_MIC_DEV, 0 },
994 { ES1688_AUX_DEV, 0 },
995 { ES1688_SPEAKER_DEV, 0 },
996 { ES1688_RECLEV_DEV, 0 },
997 { ES1688_REC_DEV, 0x17 }
998};
999
Krzysztof Helt396fa822010-05-09 20:35:44 +02001000int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned int idx;
1003 int err;
1004 unsigned char reg, val;
1005
Krzysztof Helt396fa822010-05-09 20:35:44 +02001006 if (snd_BUG_ON(!chip || !card))
Takashi Iwai622207d2008-08-08 17:11:45 +02001007 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 strcpy(card->mixername, snd_es1688_chip_id(chip));
1010
1011 for (idx = 0; idx < ARRAY_SIZE(snd_es1688_controls); idx++) {
1012 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es1688_controls[idx], chip))) < 0)
1013 return err;
1014 }
1015 for (idx = 0; idx < ES1688_INIT_TABLE_SIZE; idx++) {
1016 reg = snd_es1688_init_table[idx][0];
1017 val = snd_es1688_init_table[idx][1];
1018 if (reg < 0xa0)
1019 snd_es1688_mixer_write(chip, reg, val);
1020 else
1021 snd_es1688_write(chip, reg, val);
1022 }
1023 return 0;
1024}
1025
1026EXPORT_SYMBOL(snd_es1688_mixer_write);
1027EXPORT_SYMBOL(snd_es1688_create);
1028EXPORT_SYMBOL(snd_es1688_pcm);
1029EXPORT_SYMBOL(snd_es1688_mixer);
1030
1031/*
1032 * INIT part
1033 */
1034
1035static int __init alsa_es1688_init(void)
1036{
1037 return 0;
1038}
1039
1040static void __exit alsa_es1688_exit(void)
1041{
1042}
1043
1044module_init(alsa_es1688_init)
1045module_exit(alsa_es1688_exit)