xref: /linux/sound/isa/es1688/es1688_lib.c (revision 8f8d5745bb520c76b81abef4a2cb3023d0313bfd)
1 /*
2  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
3  *  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 
22 #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 <linux/module.h>
28 #include <linux/io.h>
29 #include <sound/core.h>
30 #include <sound/es1688.h>
31 #include <sound/initval.h>
32 
33 #include <asm/dma.h>
34 
35 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
36 MODULE_DESCRIPTION("ESS ESx688 lowlevel module");
37 MODULE_LICENSE("GPL");
38 
39 static int snd_es1688_dsp_command(struct snd_es1688 *chip, unsigned char val)
40 {
41 	int i;
42 
43 	for (i = 10000; i; i--)
44 		if ((inb(ES1688P(chip, STATUS)) & 0x80) == 0) {
45 			outb(val, ES1688P(chip, COMMAND));
46 			return 1;
47 		}
48 #ifdef CONFIG_SND_DEBUG
49 	printk(KERN_DEBUG "snd_es1688_dsp_command: timeout (0x%x)\n", val);
50 #endif
51 	return 0;
52 }
53 
54 static int snd_es1688_dsp_get_byte(struct snd_es1688 *chip)
55 {
56 	int i;
57 
58 	for (i = 1000; i; i--)
59 		if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80)
60 			return inb(ES1688P(chip, READ));
61 	snd_printd("es1688 get byte failed: 0x%lx = 0x%x!!!\n", ES1688P(chip, DATA_AVAIL), inb(ES1688P(chip, DATA_AVAIL)));
62 	return -ENODEV;
63 }
64 
65 static int snd_es1688_write(struct snd_es1688 *chip,
66 			    unsigned char reg, unsigned char data)
67 {
68 	if (!snd_es1688_dsp_command(chip, reg))
69 		return 0;
70 	return snd_es1688_dsp_command(chip, data);
71 }
72 
73 static int snd_es1688_read(struct snd_es1688 *chip, unsigned char reg)
74 {
75 	/* Read a byte from an extended mode register of ES1688 */
76 	if (!snd_es1688_dsp_command(chip, 0xc0))
77 		return -1;
78 	if (!snd_es1688_dsp_command(chip, reg))
79 		return -1;
80 	return snd_es1688_dsp_get_byte(chip);
81 }
82 
83 void snd_es1688_mixer_write(struct snd_es1688 *chip,
84 			    unsigned char reg, unsigned char data)
85 {
86 	outb(reg, ES1688P(chip, MIXER_ADDR));
87 	udelay(10);
88 	outb(data, ES1688P(chip, MIXER_DATA));
89 	udelay(10);
90 }
91 
92 static unsigned char snd_es1688_mixer_read(struct snd_es1688 *chip, unsigned char reg)
93 {
94 	unsigned char result;
95 
96 	outb(reg, ES1688P(chip, MIXER_ADDR));
97 	udelay(10);
98 	result = inb(ES1688P(chip, MIXER_DATA));
99 	udelay(10);
100 	return result;
101 }
102 
103 int snd_es1688_reset(struct snd_es1688 *chip)
104 {
105 	int i;
106 
107 	outb(3, ES1688P(chip, RESET));		/* valid only for ESS chips, SB -> 1 */
108 	udelay(10);
109 	outb(0, ES1688P(chip, RESET));
110 	udelay(30);
111 	for (i = 0; i < 1000 && !(inb(ES1688P(chip, DATA_AVAIL)) & 0x80); i++);
112 	if (inb(ES1688P(chip, READ)) != 0xaa) {
113 		snd_printd("ess_reset at 0x%lx: failed!!!\n", chip->port);
114 		return -ENODEV;
115 	}
116 	snd_es1688_dsp_command(chip, 0xc6);	/* enable extended mode */
117 	return 0;
118 }
119 EXPORT_SYMBOL(snd_es1688_reset);
120 
121 static int snd_es1688_probe(struct snd_es1688 *chip)
122 {
123 	unsigned long flags;
124 	unsigned short major, minor;
125 	int i;
126 
127 	/*
128 	 *  initialization sequence
129 	 */
130 
131 	spin_lock_irqsave(&chip->reg_lock, flags);	/* Some ESS1688 cards need this */
132 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
133 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
134 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
135 	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */
136 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
137 	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */
138 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
139 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
140 	inb(ES1688P(chip, ENABLE2));	/* ENABLE2 */
141 	inb(ES1688P(chip, ENABLE1));	/* ENABLE1 */
142 	inb(ES1688P(chip, ENABLE0));	/* ENABLE0 */
143 
144 	if (snd_es1688_reset(chip) < 0) {
145 		snd_printdd("ESS: [0x%lx] reset failed... 0x%x\n", chip->port, inb(ES1688P(chip, READ)));
146 		spin_unlock_irqrestore(&chip->reg_lock, flags);
147 		return -ENODEV;
148 	}
149 	snd_es1688_dsp_command(chip, 0xe7);	/* return identification */
150 
151 	for (i = 1000, major = minor = 0; i; i--) {
152 		if (inb(ES1688P(chip, DATA_AVAIL)) & 0x80) {
153 			if (major == 0) {
154 				major = inb(ES1688P(chip, READ));
155 			} else {
156 				minor = inb(ES1688P(chip, READ));
157 			}
158 		}
159 	}
160 
161 	spin_unlock_irqrestore(&chip->reg_lock, flags);
162 
163 	snd_printdd("ESS: [0x%lx] found.. major = 0x%x, minor = 0x%x\n", chip->port, major, minor);
164 
165 	chip->version = (major << 8) | minor;
166 	if (!chip->version)
167 		return -ENODEV;	/* probably SB */
168 
169 	switch (chip->version & 0xfff0) {
170 	case 0x4880:
171 		snd_printk(KERN_ERR "[0x%lx] ESS: AudioDrive ES488 detected, "
172 			   "but driver is in another place\n", chip->port);
173 		return -ENODEV;
174 	case 0x6880:
175 		break;
176 	default:
177 		snd_printk(KERN_ERR "[0x%lx] ESS: unknown AudioDrive chip "
178 			   "with version 0x%x (Jazz16 soundcard?)\n",
179 			   chip->port, chip->version);
180 		return -ENODEV;
181 	}
182 
183 	spin_lock_irqsave(&chip->reg_lock, flags);
184 	snd_es1688_write(chip, 0xb1, 0x10);	/* disable IRQ */
185 	snd_es1688_write(chip, 0xb2, 0x00);	/* disable DMA */
186 	spin_unlock_irqrestore(&chip->reg_lock, flags);
187 
188 	/* enable joystick, but disable OPL3 */
189 	spin_lock_irqsave(&chip->mixer_lock, flags);
190 	snd_es1688_mixer_write(chip, 0x40, 0x01);
191 	spin_unlock_irqrestore(&chip->mixer_lock, flags);
192 
193 	return 0;
194 }
195 
196 static int snd_es1688_init(struct snd_es1688 * chip, int enable)
197 {
198 	static int irqs[16] = {-1, -1, 0, -1, -1, 1, -1, 2, -1, 0, 3, -1, -1, -1, -1, -1};
199 	unsigned long flags;
200 	int cfg, irq_bits, dma, dma_bits, tmp, tmp1;
201 
202 	/* ok.. setup MPU-401 port and joystick and OPL3 */
203 	cfg = 0x01;		/* enable joystick, but disable OPL3 */
204 	if (enable && chip->mpu_port >= 0x300 && chip->mpu_irq > 0 && chip->hardware != ES1688_HW_688) {
205 		tmp = (chip->mpu_port & 0x0f0) >> 4;
206 		if (tmp <= 3) {
207 			switch (chip->mpu_irq) {
208 			case 9:
209 				tmp1 = 4;
210 				break;
211 			case 5:
212 				tmp1 = 5;
213 				break;
214 			case 7:
215 				tmp1 = 6;
216 				break;
217 			case 10:
218 				tmp1 = 7;
219 				break;
220 			default:
221 				tmp1 = 0;
222 			}
223 			if (tmp1) {
224 				cfg |= (tmp << 3) | (tmp1 << 5);
225 			}
226 		}
227 	}
228 #if 0
229 	snd_printk(KERN_DEBUG "mpu cfg = 0x%x\n", cfg);
230 #endif
231 	spin_lock_irqsave(&chip->reg_lock, flags);
232 	snd_es1688_mixer_write(chip, 0x40, cfg);
233 	spin_unlock_irqrestore(&chip->reg_lock, flags);
234 	/* --- */
235 	spin_lock_irqsave(&chip->reg_lock, flags);
236 	snd_es1688_read(chip, 0xb1);
237 	snd_es1688_read(chip, 0xb2);
238 	spin_unlock_irqrestore(&chip->reg_lock, flags);
239 	if (enable) {
240 		cfg = 0xf0;	/* enable only DMA counter interrupt */
241 		irq_bits = irqs[chip->irq & 0x0f];
242 		if (irq_bits < 0) {
243 			snd_printk(KERN_ERR "[0x%lx] ESS: bad IRQ %d "
244 				   "for ES1688 chip!!\n",
245 				   chip->port, chip->irq);
246 #if 0
247 			irq_bits = 0;
248 			cfg = 0x10;
249 #endif
250 			return -EINVAL;
251 		}
252 		spin_lock_irqsave(&chip->reg_lock, flags);
253 		snd_es1688_write(chip, 0xb1, cfg | (irq_bits << 2));
254 		spin_unlock_irqrestore(&chip->reg_lock, flags);
255 		cfg = 0xf0;	/* extended mode DMA enable */
256 		dma = chip->dma8;
257 		if (dma > 3 || dma == 2) {
258 			snd_printk(KERN_ERR "[0x%lx] ESS: bad DMA channel %d "
259 				   "for ES1688 chip!!\n", chip->port, dma);
260 #if 0
261 			dma_bits = 0;
262 			cfg = 0x00;	/* disable all DMA */
263 #endif
264 			return -EINVAL;
265 		} else {
266 			dma_bits = dma;
267 			if (dma != 3)
268 				dma_bits++;
269 		}
270 		spin_lock_irqsave(&chip->reg_lock, flags);
271 		snd_es1688_write(chip, 0xb2, cfg | (dma_bits << 2));
272 		spin_unlock_irqrestore(&chip->reg_lock, flags);
273 	} else {
274 		spin_lock_irqsave(&chip->reg_lock, flags);
275 		snd_es1688_write(chip, 0xb1, 0x10);	/* disable IRQ */
276 		snd_es1688_write(chip, 0xb2, 0x00);	/* disable DMA */
277 		spin_unlock_irqrestore(&chip->reg_lock, flags);
278 	}
279 	spin_lock_irqsave(&chip->reg_lock, flags);
280 	snd_es1688_read(chip, 0xb1);
281 	snd_es1688_read(chip, 0xb2);
282 	snd_es1688_reset(chip);
283 	spin_unlock_irqrestore(&chip->reg_lock, flags);
284 	return 0;
285 }
286 
287 /*
288 
289  */
290 
291 static const struct snd_ratnum clocks[2] = {
292 	{
293 		.num = 795444,
294 		.den_min = 1,
295 		.den_max = 128,
296 		.den_step = 1,
297 	},
298 	{
299 		.num = 397722,
300 		.den_min = 1,
301 		.den_max = 128,
302 		.den_step = 1,
303 	}
304 };
305 
306 static const struct snd_pcm_hw_constraint_ratnums hw_constraints_clocks  = {
307 	.nrats = 2,
308 	.rats = clocks,
309 };
310 
311 static void snd_es1688_set_rate(struct snd_es1688 *chip, struct snd_pcm_substream *substream)
312 {
313 	struct snd_pcm_runtime *runtime = substream->runtime;
314 	unsigned int bits, divider;
315 
316 	if (runtime->rate_num == clocks[0].num)
317 		bits = 256 - runtime->rate_den;
318 	else
319 		bits = 128 - runtime->rate_den;
320 	/* set filter register */
321 	divider = 256 - 7160000*20/(8*82*runtime->rate);
322 	/* write result to hardware */
323 	snd_es1688_write(chip, 0xa1, bits);
324 	snd_es1688_write(chip, 0xa2, divider);
325 }
326 
327 static int snd_es1688_ioctl(struct snd_pcm_substream *substream,
328 			    unsigned int cmd, void *arg)
329 {
330 	return snd_pcm_lib_ioctl(substream, cmd, arg);
331 }
332 
333 static int snd_es1688_trigger(struct snd_es1688 *chip, int cmd, unsigned char value)
334 {
335 	int val;
336 
337 	if (cmd == SNDRV_PCM_TRIGGER_STOP) {
338 		value = 0x00;
339 	} else if (cmd != SNDRV_PCM_TRIGGER_START) {
340 		return -EINVAL;
341 	}
342 	spin_lock(&chip->reg_lock);
343 	chip->trigger_value = value;
344 	val = snd_es1688_read(chip, 0xb8);
345 	if ((val < 0) || (val & 0x0f) == value) {
346 		spin_unlock(&chip->reg_lock);
347 		return -EINVAL;	/* something is wrong */
348 	}
349 #if 0
350 	printk(KERN_DEBUG "trigger: val = 0x%x, value = 0x%x\n", val, value);
351 	printk(KERN_DEBUG "trigger: pointer = 0x%x\n",
352 	       snd_dma_pointer(chip->dma8, chip->dma_size));
353 #endif
354 	snd_es1688_write(chip, 0xb8, (val & 0xf0) | value);
355 	spin_unlock(&chip->reg_lock);
356 	return 0;
357 }
358 
359 static int snd_es1688_hw_params(struct snd_pcm_substream *substream,
360 				struct snd_pcm_hw_params *hw_params)
361 {
362 	return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
363 }
364 
365 static int snd_es1688_hw_free(struct snd_pcm_substream *substream)
366 {
367 	return snd_pcm_lib_free_pages(substream);
368 }
369 
370 static int snd_es1688_playback_prepare(struct snd_pcm_substream *substream)
371 {
372 	unsigned long flags;
373 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
374 	struct snd_pcm_runtime *runtime = substream->runtime;
375 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
376 	unsigned int count = snd_pcm_lib_period_bytes(substream);
377 
378 	chip->dma_size = size;
379 	spin_lock_irqsave(&chip->reg_lock, flags);
380 	snd_es1688_reset(chip);
381 	snd_es1688_set_rate(chip, substream);
382 	snd_es1688_write(chip, 0xb8, 4);	/* auto init DMA mode */
383 	snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
384 	snd_es1688_write(chip, 0xb9, 2);	/* demand mode (4 bytes/request) */
385 	if (runtime->channels == 1) {
386 		if (snd_pcm_format_width(runtime->format) == 8) {
387 			/* 8. bit mono */
388 			snd_es1688_write(chip, 0xb6, 0x80);
389 			snd_es1688_write(chip, 0xb7, 0x51);
390 			snd_es1688_write(chip, 0xb7, 0xd0);
391 		} else {
392 			/* 16. bit mono */
393 			snd_es1688_write(chip, 0xb6, 0x00);
394 			snd_es1688_write(chip, 0xb7, 0x71);
395 			snd_es1688_write(chip, 0xb7, 0xf4);
396 		}
397 	} else {
398 		if (snd_pcm_format_width(runtime->format) == 8) {
399 			/* 8. bit stereo */
400 			snd_es1688_write(chip, 0xb6, 0x80);
401 			snd_es1688_write(chip, 0xb7, 0x51);
402 			snd_es1688_write(chip, 0xb7, 0x98);
403 		} else {
404 			/* 16. bit stereo */
405 			snd_es1688_write(chip, 0xb6, 0x00);
406 			snd_es1688_write(chip, 0xb7, 0x71);
407 			snd_es1688_write(chip, 0xb7, 0xbc);
408 		}
409 	}
410 	snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
411 	snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
412 	snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKON);
413 	spin_unlock_irqrestore(&chip->reg_lock, flags);
414 	/* --- */
415 	count = -count;
416 	snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
417 	spin_lock_irqsave(&chip->reg_lock, flags);
418 	snd_es1688_write(chip, 0xa4, (unsigned char) count);
419 	snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
420 	spin_unlock_irqrestore(&chip->reg_lock, flags);
421 	return 0;
422 }
423 
424 static int snd_es1688_playback_trigger(struct snd_pcm_substream *substream,
425 				       int cmd)
426 {
427 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
428 	return snd_es1688_trigger(chip, cmd, 0x05);
429 }
430 
431 static int snd_es1688_capture_prepare(struct snd_pcm_substream *substream)
432 {
433 	unsigned long flags;
434 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
435 	struct snd_pcm_runtime *runtime = substream->runtime;
436 	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
437 	unsigned int count = snd_pcm_lib_period_bytes(substream);
438 
439 	chip->dma_size = size;
440 	spin_lock_irqsave(&chip->reg_lock, flags);
441 	snd_es1688_reset(chip);
442 	snd_es1688_set_rate(chip, substream);
443 	snd_es1688_dsp_command(chip, ES1688_DSP_CMD_SPKOFF);
444 	snd_es1688_write(chip, 0xb8, 0x0e);	/* auto init DMA mode */
445 	snd_es1688_write(chip, 0xa8, (snd_es1688_read(chip, 0xa8) & ~0x03) | (3 - runtime->channels));
446 	snd_es1688_write(chip, 0xb9, 2);	/* demand mode (4 bytes/request) */
447 	if (runtime->channels == 1) {
448 		if (snd_pcm_format_width(runtime->format) == 8) {
449 			/* 8. bit mono */
450 			snd_es1688_write(chip, 0xb7, 0x51);
451 			snd_es1688_write(chip, 0xb7, 0xd0);
452 		} else {
453 			/* 16. bit mono */
454 			snd_es1688_write(chip, 0xb7, 0x71);
455 			snd_es1688_write(chip, 0xb7, 0xf4);
456 		}
457 	} else {
458 		if (snd_pcm_format_width(runtime->format) == 8) {
459 			/* 8. bit stereo */
460 			snd_es1688_write(chip, 0xb7, 0x51);
461 			snd_es1688_write(chip, 0xb7, 0x98);
462 		} else {
463 			/* 16. bit stereo */
464 			snd_es1688_write(chip, 0xb7, 0x71);
465 			snd_es1688_write(chip, 0xb7, 0xbc);
466 		}
467 	}
468 	snd_es1688_write(chip, 0xb1, (snd_es1688_read(chip, 0xb1) & 0x0f) | 0x50);
469 	snd_es1688_write(chip, 0xb2, (snd_es1688_read(chip, 0xb2) & 0x0f) | 0x50);
470 	spin_unlock_irqrestore(&chip->reg_lock, flags);
471 	/* --- */
472 	count = -count;
473 	snd_dma_program(chip->dma8, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
474 	spin_lock_irqsave(&chip->reg_lock, flags);
475 	snd_es1688_write(chip, 0xa4, (unsigned char) count);
476 	snd_es1688_write(chip, 0xa5, (unsigned char) (count >> 8));
477 	spin_unlock_irqrestore(&chip->reg_lock, flags);
478 	return 0;
479 }
480 
481 static int snd_es1688_capture_trigger(struct snd_pcm_substream *substream,
482 				      int cmd)
483 {
484 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
485 	return snd_es1688_trigger(chip, cmd, 0x0f);
486 }
487 
488 static irqreturn_t snd_es1688_interrupt(int irq, void *dev_id)
489 {
490 	struct snd_es1688 *chip = dev_id;
491 
492 	if (chip->trigger_value == 0x05)	/* ok.. playback is active */
493 		snd_pcm_period_elapsed(chip->playback_substream);
494 	if (chip->trigger_value == 0x0f)	/* ok.. capture is active */
495 		snd_pcm_period_elapsed(chip->capture_substream);
496 
497 	inb(ES1688P(chip, DATA_AVAIL));	/* ack interrupt */
498 	return IRQ_HANDLED;
499 }
500 
501 static snd_pcm_uframes_t snd_es1688_playback_pointer(struct snd_pcm_substream *substream)
502 {
503 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
504 	size_t ptr;
505 
506 	if (chip->trigger_value != 0x05)
507 		return 0;
508 	ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
509 	return bytes_to_frames(substream->runtime, ptr);
510 }
511 
512 static snd_pcm_uframes_t snd_es1688_capture_pointer(struct snd_pcm_substream *substream)
513 {
514 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
515 	size_t ptr;
516 
517 	if (chip->trigger_value != 0x0f)
518 		return 0;
519 	ptr = snd_dma_pointer(chip->dma8, chip->dma_size);
520 	return bytes_to_frames(substream->runtime, ptr);
521 }
522 
523 /*
524 
525  */
526 
527 static const struct snd_pcm_hardware snd_es1688_playback =
528 {
529 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
530 				 SNDRV_PCM_INFO_MMAP_VALID),
531 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
532 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
533 	.rate_min =		4000,
534 	.rate_max =		48000,
535 	.channels_min =		1,
536 	.channels_max =		2,
537 	.buffer_bytes_max =	65536,
538 	.period_bytes_min =	64,
539 	.period_bytes_max =	65536,
540 	.periods_min =		1,
541 	.periods_max =		1024,
542 	.fifo_size =		0,
543 };
544 
545 static const struct snd_pcm_hardware snd_es1688_capture =
546 {
547 	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
548 				 SNDRV_PCM_INFO_MMAP_VALID),
549 	.formats =		SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
550 	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
551 	.rate_min =		4000,
552 	.rate_max =		48000,
553 	.channels_min =		1,
554 	.channels_max =		2,
555 	.buffer_bytes_max =	65536,
556 	.period_bytes_min =	64,
557 	.period_bytes_max =	65536,
558 	.periods_min =		1,
559 	.periods_max =		1024,
560 	.fifo_size =		0,
561 };
562 
563 /*
564 
565  */
566 
567 static int snd_es1688_playback_open(struct snd_pcm_substream *substream)
568 {
569 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
570 	struct snd_pcm_runtime *runtime = substream->runtime;
571 
572 	if (chip->capture_substream != NULL)
573 		return -EAGAIN;
574 	chip->playback_substream = substream;
575 	runtime->hw = snd_es1688_playback;
576 	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
577 				      &hw_constraints_clocks);
578 	return 0;
579 }
580 
581 static int snd_es1688_capture_open(struct snd_pcm_substream *substream)
582 {
583 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
584 	struct snd_pcm_runtime *runtime = substream->runtime;
585 
586 	if (chip->playback_substream != NULL)
587 		return -EAGAIN;
588 	chip->capture_substream = substream;
589 	runtime->hw = snd_es1688_capture;
590 	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
591 				      &hw_constraints_clocks);
592 	return 0;
593 }
594 
595 static int snd_es1688_playback_close(struct snd_pcm_substream *substream)
596 {
597 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
598 
599 	chip->playback_substream = NULL;
600 	return 0;
601 }
602 
603 static int snd_es1688_capture_close(struct snd_pcm_substream *substream)
604 {
605 	struct snd_es1688 *chip = snd_pcm_substream_chip(substream);
606 
607 	chip->capture_substream = NULL;
608 	return 0;
609 }
610 
611 static int snd_es1688_free(struct snd_es1688 *chip)
612 {
613 	if (chip->hardware != ES1688_HW_UNDEF)
614 		snd_es1688_init(chip, 0);
615 	release_and_free_resource(chip->res_port);
616 	if (chip->irq >= 0)
617 		free_irq(chip->irq, (void *) chip);
618 	if (chip->dma8 >= 0) {
619 		disable_dma(chip->dma8);
620 		free_dma(chip->dma8);
621 	}
622 	return 0;
623 }
624 
625 static int snd_es1688_dev_free(struct snd_device *device)
626 {
627 	struct snd_es1688 *chip = device->device_data;
628 	return snd_es1688_free(chip);
629 }
630 
631 static const char *snd_es1688_chip_id(struct snd_es1688 *chip)
632 {
633 	static char tmp[16];
634 	sprintf(tmp, "ES%s688 rev %i", chip->hardware == ES1688_HW_688 ? "" : "1", chip->version & 0x0f);
635 	return tmp;
636 }
637 
638 int snd_es1688_create(struct snd_card *card,
639 		      struct snd_es1688 *chip,
640 		      unsigned long port,
641 		      unsigned long mpu_port,
642 		      int irq,
643 		      int mpu_irq,
644 		      int dma8,
645 		      unsigned short hardware)
646 {
647 	static struct snd_device_ops ops = {
648 		.dev_free =	snd_es1688_dev_free,
649 	};
650 
651 	int err;
652 
653 	if (chip == NULL)
654 		return -ENOMEM;
655 	chip->irq = -1;
656 	chip->dma8 = -1;
657 	chip->hardware = ES1688_HW_UNDEF;
658 
659 	chip->res_port = request_region(port + 4, 12, "ES1688");
660 	if (chip->res_port == NULL) {
661 		snd_printk(KERN_ERR "es1688: can't grab port 0x%lx\n", port + 4);
662 		err = -EBUSY;
663 		goto exit;
664 	}
665 
666 	err = request_irq(irq, snd_es1688_interrupt, 0, "ES1688", (void *) chip);
667 	if (err < 0) {
668 		snd_printk(KERN_ERR "es1688: can't grab IRQ %d\n", irq);
669 		goto exit;
670 	}
671 
672 	chip->irq = irq;
673 	err = request_dma(dma8, "ES1688");
674 
675 	if (err < 0) {
676 		snd_printk(KERN_ERR "es1688: can't grab DMA8 %d\n", dma8);
677 		goto exit;
678 	}
679 	chip->dma8 = dma8;
680 
681 	spin_lock_init(&chip->reg_lock);
682 	spin_lock_init(&chip->mixer_lock);
683 	chip->port = port;
684 	mpu_port &= ~0x000f;
685 	if (mpu_port < 0x300 || mpu_port > 0x330)
686 		mpu_port = 0;
687 	chip->mpu_port = mpu_port;
688 	chip->mpu_irq = mpu_irq;
689 	chip->hardware = hardware;
690 
691 	err = snd_es1688_probe(chip);
692 	if (err < 0)
693 		goto exit;
694 
695 	err = snd_es1688_init(chip, 1);
696 	if (err < 0)
697 		goto exit;
698 
699 	/* Register device */
700 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
701 exit:
702 	if (err)
703 		snd_es1688_free(chip);
704 	return err;
705 }
706 
707 static const struct snd_pcm_ops snd_es1688_playback_ops = {
708 	.open =			snd_es1688_playback_open,
709 	.close =		snd_es1688_playback_close,
710 	.ioctl =		snd_es1688_ioctl,
711 	.hw_params =		snd_es1688_hw_params,
712 	.hw_free =		snd_es1688_hw_free,
713 	.prepare =		snd_es1688_playback_prepare,
714 	.trigger =		snd_es1688_playback_trigger,
715 	.pointer =		snd_es1688_playback_pointer,
716 };
717 
718 static const struct snd_pcm_ops snd_es1688_capture_ops = {
719 	.open =			snd_es1688_capture_open,
720 	.close =		snd_es1688_capture_close,
721 	.ioctl =		snd_es1688_ioctl,
722 	.hw_params =		snd_es1688_hw_params,
723 	.hw_free =		snd_es1688_hw_free,
724 	.prepare =		snd_es1688_capture_prepare,
725 	.trigger =		snd_es1688_capture_trigger,
726 	.pointer =		snd_es1688_capture_pointer,
727 };
728 
729 int snd_es1688_pcm(struct snd_card *card, struct snd_es1688 *chip, int device)
730 {
731 	struct snd_pcm *pcm;
732 	int err;
733 
734 	err = snd_pcm_new(card, "ESx688", device, 1, 1, &pcm);
735 	if (err < 0)
736 		return err;
737 
738 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1688_playback_ops);
739 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1688_capture_ops);
740 
741 	pcm->private_data = chip;
742 	pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX;
743 	strcpy(pcm->name, snd_es1688_chip_id(chip));
744 	chip->pcm = pcm;
745 
746 	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
747 					      card->dev,
748 					      64*1024, 64*1024);
749 	return 0;
750 }
751 
752 /*
753  *  MIXER part
754  */
755 
756 static int snd_es1688_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
757 {
758 	static const char * const texts[8] = {
759 		"Mic", "Mic Master", "CD", "AOUT",
760 		"Mic1", "Mix", "Line", "Master"
761 	};
762 
763 	return snd_ctl_enum_info(uinfo, 1, 8, texts);
764 }
765 
766 static int snd_es1688_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
767 {
768 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
769 	ucontrol->value.enumerated.item[0] = snd_es1688_mixer_read(chip, ES1688_REC_DEV) & 7;
770 	return 0;
771 }
772 
773 static int snd_es1688_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
774 {
775 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
776 	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 
798 static int snd_es1688_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
799 {
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 
809 static int snd_es1688_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
810 {
811 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
812 	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 
826 static int snd_es1688_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
827 {
828 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
829 	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 
857 static int snd_es1688_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
858 {
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 
868 static int snd_es1688_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
869 {
870 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
871 	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 
902 static int snd_es1688_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
903 {
904 	struct snd_es1688 *chip = snd_kcontrol_chip(kcontrol);
905 	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 
965 static struct snd_kcontrol_new snd_es1688_controls[] = {
966 ES1688_DOUBLE("Master Playback Volume", 0, ES1688_MASTER_DEV, ES1688_MASTER_DEV, 4, 0, 15, 0),
967 ES1688_DOUBLE("PCM Playback Volume", 0, ES1688_PCM_DEV, ES1688_PCM_DEV, 4, 0, 15, 0),
968 ES1688_DOUBLE("Line Playback Volume", 0, ES1688_LINE_DEV, ES1688_LINE_DEV, 4, 0, 15, 0),
969 ES1688_DOUBLE("CD Playback Volume", 0, ES1688_CD_DEV, ES1688_CD_DEV, 4, 0, 15, 0),
970 ES1688_DOUBLE("FM Playback Volume", 0, ES1688_FM_DEV, ES1688_FM_DEV, 4, 0, 15, 0),
971 ES1688_DOUBLE("Mic Playback Volume", 0, ES1688_MIC_DEV, ES1688_MIC_DEV, 4, 0, 15, 0),
972 ES1688_DOUBLE("Aux Playback Volume", 0, ES1688_AUX_DEV, ES1688_AUX_DEV, 4, 0, 15, 0),
973 ES1688_SINGLE("Beep Playback Volume", 0, ES1688_SPEAKER_DEV, 0, 7, 0),
974 ES1688_DOUBLE("Capture Volume", 0, ES1688_RECLEV_DEV, ES1688_RECLEV_DEV, 4, 0, 15, 0),
975 ES1688_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 
987 static 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 
1000 int snd_es1688_mixer(struct snd_card *card, struct snd_es1688 *chip)
1001 {
1002 	unsigned int idx;
1003 	int err;
1004 	unsigned char reg, val;
1005 
1006 	if (snd_BUG_ON(!chip || !card))
1007 		return -EINVAL;
1008 
1009 	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 
1026 EXPORT_SYMBOL(snd_es1688_mixer_write);
1027 EXPORT_SYMBOL(snd_es1688_create);
1028 EXPORT_SYMBOL(snd_es1688_pcm);
1029 EXPORT_SYMBOL(snd_es1688_mixer);
1030