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