blob: c0ffb20da996fdcfdcc81005dd01f3603551693f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
3 * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
4 *
5 * Bugs:
6 * - sometimes record brokes playback with WSS portion of
7 * Yamaha OPL3-SA3 chip
8 * - CS4231 (GUS MAX) - still trouble with occasional noises
9 * - broken initialization?
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <sound/driver.h>
28#include <linux/delay.h>
29#include <linux/pm.h>
30#include <linux/init.h>
31#include <linux/interrupt.h>
32#include <linux/slab.h>
33#include <linux/ioport.h>
34#include <sound/core.h>
35#include <sound/cs4231.h>
36#include <sound/pcm_params.h>
37
38#include <asm/io.h>
39#include <asm/dma.h>
40#include <asm/irq.h>
41
42MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
43MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
44MODULE_LICENSE("GPL");
45
46#if 0
47#define SNDRV_DEBUG_MCE
48#endif
49
50/*
51 * Some variables
52 */
53
54static unsigned char freq_bits[14] = {
55 /* 5510 */ 0x00 | CS4231_XTAL2,
56 /* 6620 */ 0x0E | CS4231_XTAL2,
57 /* 8000 */ 0x00 | CS4231_XTAL1,
58 /* 9600 */ 0x0E | CS4231_XTAL1,
59 /* 11025 */ 0x02 | CS4231_XTAL2,
60 /* 16000 */ 0x02 | CS4231_XTAL1,
61 /* 18900 */ 0x04 | CS4231_XTAL2,
62 /* 22050 */ 0x06 | CS4231_XTAL2,
63 /* 27042 */ 0x04 | CS4231_XTAL1,
64 /* 32000 */ 0x06 | CS4231_XTAL1,
65 /* 33075 */ 0x0C | CS4231_XTAL2,
66 /* 37800 */ 0x08 | CS4231_XTAL2,
67 /* 44100 */ 0x0A | CS4231_XTAL2,
68 /* 48000 */ 0x0C | CS4231_XTAL1
69};
70
71static unsigned int rates[14] = {
72 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
73 27042, 32000, 33075, 37800, 44100, 48000
74};
75
Takashi Iwaiba2375a2005-11-17 14:30:42 +010076static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Helt6c041b52007-09-06 15:03:59 +020077 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .list = rates,
79 .mask = 0,
80};
81
Takashi Iwaiba2375a2005-11-17 14:30:42 +010082static int snd_cs4231_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
84 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates);
85}
86
87static unsigned char snd_cs4231_original_image[32] =
88{
89 0x00, /* 00/00 - lic */
90 0x00, /* 01/01 - ric */
91 0x9f, /* 02/02 - la1ic */
92 0x9f, /* 03/03 - ra1ic */
93 0x9f, /* 04/04 - la2ic */
94 0x9f, /* 05/05 - ra2ic */
95 0xbf, /* 06/06 - loc */
96 0xbf, /* 07/07 - roc */
97 0x20, /* 08/08 - pdfr */
98 CS4231_AUTOCALIB, /* 09/09 - ic */
99 0x00, /* 0a/10 - pc */
100 0x00, /* 0b/11 - ti */
101 CS4231_MODE2, /* 0c/12 - mi */
102 0xfc, /* 0d/13 - lbc */
103 0x00, /* 0e/14 - pbru */
104 0x00, /* 0f/15 - pbrl */
105 0x80, /* 10/16 - afei */
106 0x01, /* 11/17 - afeii */
107 0x9f, /* 12/18 - llic */
108 0x9f, /* 13/19 - rlic */
109 0x00, /* 14/20 - tlb */
110 0x00, /* 15/21 - thb */
111 0x00, /* 16/22 - la3mic/reserved */
112 0x00, /* 17/23 - ra3mic/reserved */
113 0x00, /* 18/24 - afs */
114 0x00, /* 19/25 - lamoc/version */
115 0xcf, /* 1a/26 - mioc */
116 0x00, /* 1b/27 - ramoc/reserved */
117 0x20, /* 1c/28 - cdfr */
118 0x00, /* 1d/29 - res4 */
119 0x00, /* 1e/30 - cbru */
120 0x00, /* 1f/31 - cbrl */
121};
122
123/*
124 * Basic I/O functions
125 */
126
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100127static inline void cs4231_outb(struct snd_cs4231 *chip, u8 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 outb(val, chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100132static inline u8 cs4231_inb(struct snd_cs4231 *chip, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return inb(chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200137static void snd_cs4231_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 for (timeout = 250;
142 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
143 timeout--)
144 udelay(100);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200145}
146
147static void snd_cs4231_outm(struct snd_cs4231 *chip, unsigned char reg,
148 unsigned char mask, unsigned char value)
149{
150 unsigned char tmp = (chip->image[reg] & mask) | value;
151
152 snd_cs4231_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153#ifdef CONFIG_SND_DEBUG
154 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
155 snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
156#endif
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200157 chip->image[reg] = tmp;
158 if (!chip->calibrate_mute) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200160 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 cs4231_outb(chip, CS4231P(REG), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 mb();
163 }
164}
165
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100166static void snd_cs4231_dout(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 int timeout;
169
170 for (timeout = 250;
171 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
172 timeout--)
173 udelay(10);
174 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
175 cs4231_outb(chip, CS4231P(REG), value);
176 mb();
177}
178
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100179void snd_cs4231_out(struct snd_cs4231 *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200181 snd_cs4231_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182#ifdef CONFIG_SND_DEBUG
183 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
184 snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
185#endif
186 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
187 cs4231_outb(chip, CS4231P(REG), value);
188 chip->image[reg] = value;
189 mb();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200190 snd_printdd("codec out - reg 0x%x = 0x%x\n",
191 chip->mce_bit | reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100194unsigned char snd_cs4231_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200196 snd_cs4231_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197#ifdef CONFIG_SND_DEBUG
198 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
199 snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
200#endif
201 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
202 mb();
203 return cs4231_inb(chip, CS4231P(REG));
204}
205
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100206void snd_cs4236_ext_out(struct snd_cs4231 *chip, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
209 cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
210 cs4231_outb(chip, CS4231P(REG), val);
211 chip->eimage[CS4236_REG(reg)] = val;
212#if 0
213 printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
214#endif
215}
216
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100217unsigned char snd_cs4236_ext_in(struct snd_cs4231 *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
220 cs4231_outb(chip, CS4231P(REG), reg | (chip->image[CS4236_EXT_REG] & 0x01));
221#if 1
222 return cs4231_inb(chip, CS4231P(REG));
223#else
224 {
225 unsigned char res;
226 res = cs4231_inb(chip, CS4231P(REG));
227 printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
228 return res;
229 }
230#endif
231}
232
233#if 0
234
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100235static void snd_cs4231_debug(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 printk("CS4231 REGS: INDEX = 0x%02x ", cs4231_inb(chip, CS4231P(REGSEL)));
238 printk(" STATUS = 0x%02x\n", cs4231_inb(chip, CS4231P(STATUS)));
239 printk(" 0x00: left input = 0x%02x ", snd_cs4231_in(chip, 0x00));
240 printk(" 0x10: alt 1 (CFIG 2) = 0x%02x\n", snd_cs4231_in(chip, 0x10));
241 printk(" 0x01: right input = 0x%02x ", snd_cs4231_in(chip, 0x01));
242 printk(" 0x11: alt 2 (CFIG 3) = 0x%02x\n", snd_cs4231_in(chip, 0x11));
243 printk(" 0x02: GF1 left input = 0x%02x ", snd_cs4231_in(chip, 0x02));
244 printk(" 0x12: left line in = 0x%02x\n", snd_cs4231_in(chip, 0x12));
245 printk(" 0x03: GF1 right input = 0x%02x ", snd_cs4231_in(chip, 0x03));
246 printk(" 0x13: right line in = 0x%02x\n", snd_cs4231_in(chip, 0x13));
247 printk(" 0x04: CD left input = 0x%02x ", snd_cs4231_in(chip, 0x04));
248 printk(" 0x14: timer low = 0x%02x\n", snd_cs4231_in(chip, 0x14));
249 printk(" 0x05: CD right input = 0x%02x ", snd_cs4231_in(chip, 0x05));
250 printk(" 0x15: timer high = 0x%02x\n", snd_cs4231_in(chip, 0x15));
251 printk(" 0x06: left output = 0x%02x ", snd_cs4231_in(chip, 0x06));
252 printk(" 0x16: left MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x16));
253 printk(" 0x07: right output = 0x%02x ", snd_cs4231_in(chip, 0x07));
254 printk(" 0x17: right MIC (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x17));
255 printk(" 0x08: playback format = 0x%02x ", snd_cs4231_in(chip, 0x08));
256 printk(" 0x18: IRQ status = 0x%02x\n", snd_cs4231_in(chip, 0x18));
257 printk(" 0x09: iface (CFIG 1) = 0x%02x ", snd_cs4231_in(chip, 0x09));
258 printk(" 0x19: left line out = 0x%02x\n", snd_cs4231_in(chip, 0x19));
259 printk(" 0x0a: pin control = 0x%02x ", snd_cs4231_in(chip, 0x0a));
260 printk(" 0x1a: mono control = 0x%02x\n", snd_cs4231_in(chip, 0x1a));
261 printk(" 0x0b: init & status = 0x%02x ", snd_cs4231_in(chip, 0x0b));
262 printk(" 0x1b: right line out = 0x%02x\n", snd_cs4231_in(chip, 0x1b));
263 printk(" 0x0c: revision & mode = 0x%02x ", snd_cs4231_in(chip, 0x0c));
264 printk(" 0x1c: record format = 0x%02x\n", snd_cs4231_in(chip, 0x1c));
265 printk(" 0x0d: loopback = 0x%02x ", snd_cs4231_in(chip, 0x0d));
266 printk(" 0x1d: var freq (PnP) = 0x%02x\n", snd_cs4231_in(chip, 0x1d));
267 printk(" 0x0e: ply upr count = 0x%02x ", snd_cs4231_in(chip, 0x0e));
268 printk(" 0x1e: ply lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1e));
269 printk(" 0x0f: rec upr count = 0x%02x ", snd_cs4231_in(chip, 0x0f));
270 printk(" 0x1f: rec lwr count = 0x%02x\n", snd_cs4231_in(chip, 0x1f));
271}
272
273#endif
274
275/*
276 * CS4231 detection / MCE routines
277 */
278
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100279static void snd_cs4231_busy_wait(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 int timeout;
282
283 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
284 for (timeout = 5; timeout > 0; timeout--)
285 cs4231_inb(chip, CS4231P(REGSEL));
286 /* end of cleanup sequence */
287 for (timeout = 250;
288 timeout > 0 && (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
289 timeout--)
290 udelay(10);
291}
292
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100293void snd_cs4231_mce_up(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294{
295 unsigned long flags;
296 int timeout;
297
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200298 snd_cs4231_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299#ifdef CONFIG_SND_DEBUG
300 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
301 snd_printk("mce_up - auto calibration time out (0)\n");
302#endif
303 spin_lock_irqsave(&chip->reg_lock, flags);
304 chip->mce_bit |= CS4231_MCE;
305 timeout = cs4231_inb(chip, CS4231P(REGSEL));
306 if (timeout == 0x80)
307 snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
308 if (!(timeout & CS4231_MCE))
309 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
310 spin_unlock_irqrestore(&chip->reg_lock, flags);
311}
312
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100313void snd_cs4231_mce_down(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 unsigned long flags;
316 int timeout;
317
318 snd_cs4231_busy_wait(chip);
Rene Hermand44df2d2007-09-10 23:22:55 +0200319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320#ifdef CONFIG_SND_DEBUG
321 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
322 snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
323#endif
324 spin_lock_irqsave(&chip->reg_lock, flags);
325 chip->mce_bit &= ~CS4231_MCE;
326 timeout = cs4231_inb(chip, CS4231P(REGSEL));
327 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
328 spin_unlock_irqrestore(&chip->reg_lock, flags);
329 if (timeout == 0x80)
330 snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
331 if ((timeout & CS4231_MCE) == 0 ||
332 !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
333 return;
334 }
335 snd_cs4231_busy_wait(chip);
336
Rene Herman90cf9b82007-09-10 23:19:55 +0200337 /*
338 * Wait for (possible -- during init auto-calibration may not be set)
339 * calibration process to start. Needs upto 5 sample periods on AD1848
340 * which at the slowest possible rate of 5.5125 kHz means 907 us.
341 */
342 msleep(1);
Rene Hermand44df2d2007-09-10 23:22:55 +0200343
344 snd_printdd("(1) jiffies = %lu\n", jiffies);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* in 10 ms increments, check condition, up to 250 ms */
347 timeout = 25;
348 while (snd_cs4231_in(chip, CS4231_TEST_INIT) & CS4231_CALIB_IN_PROGRESS) {
349 if (--timeout < 0) {
350 snd_printk("mce_down - auto calibration time out (2)\n");
351 return;
352 }
353 msleep(10);
354 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200355
356 snd_printdd("(2) jiffies = %lu\n", jiffies);
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* in 10 ms increments, check condition, up to 100 ms */
359 timeout = 10;
360 while (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
361 if (--timeout < 0) {
362 snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
363 return;
364 }
365 msleep(10);
366 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200367
368 snd_printdd("(3) jiffies = %lu\n", jiffies);
369 snd_printd("mce_down - exit = 0x%x\n", cs4231_inb(chip, CS4231P(REGSEL)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static unsigned int snd_cs4231_get_count(unsigned char format, unsigned int size)
373{
374 switch (format & 0xe0) {
375 case CS4231_LINEAR_16:
376 case CS4231_LINEAR_16_BIG:
377 size >>= 1;
378 break;
379 case CS4231_ADPCM_16:
380 return size >> 2;
381 }
382 if (format & CS4231_STEREO)
383 size >>= 1;
384 return size;
385}
386
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100387static int snd_cs4231_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 int cmd)
389{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100390 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int result = 0;
392 unsigned int what;
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100393 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int do_start;
395
396#if 0
397 printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, cs4231_inb(chip, CS4231P(STATUS)));
398#endif
399
400 switch (cmd) {
401 case SNDRV_PCM_TRIGGER_START:
402 case SNDRV_PCM_TRIGGER_RESUME:
403 do_start = 1; break;
404 case SNDRV_PCM_TRIGGER_STOP:
405 case SNDRV_PCM_TRIGGER_SUSPEND:
406 do_start = 0; break;
407 default:
408 return -EINVAL;
409 }
410
411 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100412 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (s == chip->playback_substream) {
414 what |= CS4231_PLAYBACK_ENABLE;
415 snd_pcm_trigger_done(s, substream);
416 } else if (s == chip->capture_substream) {
417 what |= CS4231_RECORD_ENABLE;
418 snd_pcm_trigger_done(s, substream);
419 }
420 }
421 spin_lock(&chip->reg_lock);
422 if (do_start) {
423 chip->image[CS4231_IFACE_CTRL] |= what;
424 if (chip->trigger)
425 chip->trigger(chip, what, 1);
426 } else {
427 chip->image[CS4231_IFACE_CTRL] &= ~what;
428 if (chip->trigger)
429 chip->trigger(chip, what, 0);
430 }
431 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
432 spin_unlock(&chip->reg_lock);
433#if 0
434 snd_cs4231_debug(chip);
435#endif
436 return result;
437}
438
439/*
440 * CODEC I/O
441 */
442
443static unsigned char snd_cs4231_get_rate(unsigned int rate)
444{
445 int i;
446
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200447 for (i = 0; i < ARRAY_SIZE(rates); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (rate == rates[i])
449 return freq_bits[i];
450 // snd_BUG();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200451 return freq_bits[ARRAY_SIZE(rates) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100454static unsigned char snd_cs4231_get_format(struct snd_cs4231 *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int format,
456 int channels)
457{
458 unsigned char rformat;
459
460 rformat = CS4231_LINEAR_8;
461 switch (format) {
462 case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
463 case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
464 case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
465 case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
466 case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
467 }
468 if (channels > 1)
469 rformat |= CS4231_STEREO;
470#if 0
471 snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
472#endif
473 return rformat;
474}
475
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100476static void snd_cs4231_calibrate_mute(struct snd_cs4231 *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned long flags;
479
480 mute = mute ? 1 : 0;
481 spin_lock_irqsave(&chip->reg_lock, flags);
482 if (chip->calibrate_mute == mute) {
483 spin_unlock_irqrestore(&chip->reg_lock, flags);
484 return;
485 }
486 if (!mute) {
487 snd_cs4231_dout(chip, CS4231_LEFT_INPUT, chip->image[CS4231_LEFT_INPUT]);
488 snd_cs4231_dout(chip, CS4231_RIGHT_INPUT, chip->image[CS4231_RIGHT_INPUT]);
489 snd_cs4231_dout(chip, CS4231_LOOPBACK, chip->image[CS4231_LOOPBACK]);
490 }
491 snd_cs4231_dout(chip, CS4231_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
492 snd_cs4231_dout(chip, CS4231_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
493 snd_cs4231_dout(chip, CS4231_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
494 snd_cs4231_dout(chip, CS4231_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
495 snd_cs4231_dout(chip, CS4231_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
496 snd_cs4231_dout(chip, CS4231_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
497 snd_cs4231_dout(chip, CS4231_LEFT_LINE_IN, mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
498 snd_cs4231_dout(chip, CS4231_RIGHT_LINE_IN, mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
499 snd_cs4231_dout(chip, CS4231_MONO_CTRL, mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
500 if (chip->hardware == CS4231_HW_INTERWAVE) {
501 snd_cs4231_dout(chip, CS4231_LEFT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
502 snd_cs4231_dout(chip, CS4231_RIGHT_MIC_INPUT, mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
503 snd_cs4231_dout(chip, CS4231_LINE_LEFT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
504 snd_cs4231_dout(chip, CS4231_LINE_RIGHT_OUTPUT, mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
505 }
506 chip->calibrate_mute = mute;
507 spin_unlock_irqrestore(&chip->reg_lock, flags);
508}
509
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100510static void snd_cs4231_playback_format(struct snd_cs4231 *chip,
511 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 unsigned char pdfr)
513{
514 unsigned long flags;
515 int full_calib = 1;
516
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100517 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 snd_cs4231_calibrate_mute(chip, 1);
519 if (chip->hardware == CS4231_HW_CS4231A ||
520 (chip->hardware & CS4231_HW_CS4232_MASK)) {
521 spin_lock_irqsave(&chip->reg_lock, flags);
522 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
523 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x10);
524 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
525 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
526 udelay(100); /* Fixes audible clicks at least on GUS MAX */
527 full_calib = 0;
528 }
529 spin_unlock_irqrestore(&chip->reg_lock, flags);
530 }
531 if (full_calib) {
532 snd_cs4231_mce_up(chip);
533 spin_lock_irqsave(&chip->reg_lock, flags);
534 if (chip->hardware != CS4231_HW_INTERWAVE && !chip->single_dma) {
535 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
536 (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE) ?
537 (pdfr & 0xf0) | (chip->image[CS4231_REC_FORMAT] & 0x0f) :
538 pdfr);
539 } else {
540 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT] = pdfr);
541 }
542 spin_unlock_irqrestore(&chip->reg_lock, flags);
Paul Vojtae2340462007-07-27 12:20:38 +0200543 if (chip->hardware == CS4231_HW_OPL3SA2)
544 udelay(100); /* this seems to help */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 snd_cs4231_mce_down(chip);
546 }
547 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100548 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100551static void snd_cs4231_capture_format(struct snd_cs4231 *chip,
552 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 unsigned char cdfr)
554{
555 unsigned long flags;
556 int full_calib = 1;
557
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100558 mutex_lock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 snd_cs4231_calibrate_mute(chip, 1);
560 if (chip->hardware == CS4231_HW_CS4231A ||
561 (chip->hardware & CS4231_HW_CS4232_MASK)) {
562 spin_lock_irqsave(&chip->reg_lock, flags);
563 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
564 (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
565 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | 0x20);
566 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT] = cdfr);
567 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
568 full_calib = 0;
569 }
570 spin_unlock_irqrestore(&chip->reg_lock, flags);
571 }
572 if (full_calib) {
573 snd_cs4231_mce_up(chip);
574 spin_lock_irqsave(&chip->reg_lock, flags);
575 if (chip->hardware != CS4231_HW_INTERWAVE) {
576 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
577 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT,
578 ((chip->single_dma ? cdfr : chip->image[CS4231_PLAYBK_FORMAT]) & 0xf0) |
579 (cdfr & 0x0f));
580 spin_unlock_irqrestore(&chip->reg_lock, flags);
581 snd_cs4231_mce_down(chip);
582 snd_cs4231_mce_up(chip);
583 spin_lock_irqsave(&chip->reg_lock, flags);
584 }
585 }
586 snd_cs4231_out(chip, CS4231_REC_FORMAT, cdfr);
587 spin_unlock_irqrestore(&chip->reg_lock, flags);
588 snd_cs4231_mce_down(chip);
589 }
590 snd_cs4231_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100591 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
594/*
595 * Timer interface
596 */
597
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100598static unsigned long snd_cs4231_timer_resolution(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100600 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (chip->hardware & CS4231_HW_CS4236B_MASK)
602 return 14467;
603 else
604 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
605}
606
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100607static int snd_cs4231_timer_start(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 unsigned long flags;
610 unsigned int ticks;
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100611 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_lock_irqsave(&chip->reg_lock, flags);
613 ticks = timer->sticks;
614 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
615 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
616 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
617 snd_cs4231_out(chip, CS4231_TIMER_HIGH, chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8));
618 snd_cs4231_out(chip, CS4231_TIMER_LOW, chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks);
619 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] | CS4231_TIMER_ENABLE);
620 }
621 spin_unlock_irqrestore(&chip->reg_lock, flags);
622 return 0;
623}
624
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100625static int snd_cs4231_timer_stop(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned long flags;
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100628 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 spin_lock_irqsave(&chip->reg_lock, flags);
630 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE);
631 spin_unlock_irqrestore(&chip->reg_lock, flags);
632 return 0;
633}
634
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100635static void snd_cs4231_init(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 unsigned long flags;
638
639 snd_cs4231_mce_down(chip);
640
641#ifdef SNDRV_DEBUG_MCE
642 snd_printk("init: (1)\n");
643#endif
644 snd_cs4231_mce_up(chip);
645 spin_lock_irqsave(&chip->reg_lock, flags);
646 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
647 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO |
648 CS4231_CALIB_MODE);
649 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
650 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
651 spin_unlock_irqrestore(&chip->reg_lock, flags);
652 snd_cs4231_mce_down(chip);
653
654#ifdef SNDRV_DEBUG_MCE
655 snd_printk("init: (2)\n");
656#endif
657
658 snd_cs4231_mce_up(chip);
659 spin_lock_irqsave(&chip->reg_lock, flags);
660 snd_cs4231_out(chip, CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
661 spin_unlock_irqrestore(&chip->reg_lock, flags);
662 snd_cs4231_mce_down(chip);
663
664#ifdef SNDRV_DEBUG_MCE
665 snd_printk("init: (3) - afei = 0x%x\n", chip->image[CS4231_ALT_FEATURE_1]);
666#endif
667
668 spin_lock_irqsave(&chip->reg_lock, flags);
669 snd_cs4231_out(chip, CS4231_ALT_FEATURE_2, chip->image[CS4231_ALT_FEATURE_2]);
670 spin_unlock_irqrestore(&chip->reg_lock, flags);
671
672 snd_cs4231_mce_up(chip);
673 spin_lock_irqsave(&chip->reg_lock, flags);
674 snd_cs4231_out(chip, CS4231_PLAYBK_FORMAT, chip->image[CS4231_PLAYBK_FORMAT]);
675 spin_unlock_irqrestore(&chip->reg_lock, flags);
676 snd_cs4231_mce_down(chip);
677
678#ifdef SNDRV_DEBUG_MCE
679 snd_printk("init: (4)\n");
680#endif
681
682 snd_cs4231_mce_up(chip);
683 spin_lock_irqsave(&chip->reg_lock, flags);
684 snd_cs4231_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
685 spin_unlock_irqrestore(&chip->reg_lock, flags);
686 snd_cs4231_mce_down(chip);
687
688#ifdef SNDRV_DEBUG_MCE
689 snd_printk("init: (5)\n");
690#endif
691}
692
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100693static int snd_cs4231_open(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
695 unsigned long flags;
696
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100697 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if ((chip->mode & mode) ||
699 ((chip->mode & CS4231_MODE_OPEN) && chip->single_dma)) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100700 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EAGAIN;
702 }
703 if (chip->mode & CS4231_MODE_OPEN) {
704 chip->mode |= mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100705 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return 0;
707 }
708 /* ok. now enable and ack CODEC IRQ */
709 spin_lock_irqsave(&chip->reg_lock, flags);
710 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
711 CS4231_RECORD_IRQ |
712 CS4231_TIMER_IRQ);
713 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
714 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
715 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
716 chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
717 snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
718 snd_cs4231_out(chip, CS4231_IRQ_STATUS, CS4231_PLAYBACK_IRQ |
719 CS4231_RECORD_IRQ |
720 CS4231_TIMER_IRQ);
721 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
722 spin_unlock_irqrestore(&chip->reg_lock, flags);
723
724 chip->mode = mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100725 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return 0;
727}
728
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100729static void snd_cs4231_close(struct snd_cs4231 *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 unsigned long flags;
732
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100733 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 chip->mode &= ~mode;
735 if (chip->mode & CS4231_MODE_OPEN) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100736 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return;
738 }
739 snd_cs4231_calibrate_mute(chip, 1);
740
741 /* disable IRQ */
742 spin_lock_irqsave(&chip->reg_lock, flags);
743 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
744 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
745 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
746 chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
747 snd_cs4231_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
748
749 /* now disable record & playback */
750
751 if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
752 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
753 spin_unlock_irqrestore(&chip->reg_lock, flags);
754 snd_cs4231_mce_up(chip);
755 spin_lock_irqsave(&chip->reg_lock, flags);
756 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
757 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
758 snd_cs4231_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
759 spin_unlock_irqrestore(&chip->reg_lock, flags);
760 snd_cs4231_mce_down(chip);
761 spin_lock_irqsave(&chip->reg_lock, flags);
762 }
763
764 /* clear IRQ again */
765 snd_cs4231_out(chip, CS4231_IRQ_STATUS, 0);
766 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
767 cs4231_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
768 spin_unlock_irqrestore(&chip->reg_lock, flags);
769
770 snd_cs4231_calibrate_mute(chip, 0);
771
772 chip->mode = 0;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100773 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
776/*
777 * timer open/close
778 */
779
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100780static int snd_cs4231_timer_open(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100782 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 snd_cs4231_open(chip, CS4231_MODE_TIMER);
784 return 0;
785}
786
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100787static int snd_cs4231_timer_close(struct snd_timer * timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100789 struct snd_cs4231 *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 snd_cs4231_close(chip, CS4231_MODE_TIMER);
791 return 0;
792}
793
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100794static struct snd_timer_hardware snd_cs4231_timer_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 .flags = SNDRV_TIMER_HW_AUTO,
797 .resolution = 9945,
798 .ticks = 65535,
799 .open = snd_cs4231_timer_open,
800 .close = snd_cs4231_timer_close,
801 .c_resolution = snd_cs4231_timer_resolution,
802 .start = snd_cs4231_timer_start,
803 .stop = snd_cs4231_timer_stop,
804};
805
806/*
807 * ok.. exported functions..
808 */
809
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100810static int snd_cs4231_playback_hw_params(struct snd_pcm_substream *substream,
811 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100813 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned char new_pdfr;
815 int err;
816
817 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
818 return err;
819 new_pdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
820 snd_cs4231_get_rate(params_rate(hw_params));
821 chip->set_playback_format(chip, hw_params, new_pdfr);
822 return 0;
823}
824
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100825static int snd_cs4231_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 return snd_pcm_lib_free_pages(substream);
828}
829
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100830static int snd_cs4231_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100832 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
833 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 unsigned long flags;
835 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
836 unsigned int count = snd_pcm_lib_period_bytes(substream);
837
838 spin_lock_irqsave(&chip->reg_lock, flags);
839 chip->p_dma_size = size;
840 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
841 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
842 count = snd_cs4231_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
843 snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
844 snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
845 spin_unlock_irqrestore(&chip->reg_lock, flags);
846#if 0
847 snd_cs4231_debug(chip);
848#endif
849 return 0;
850}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100852static int snd_cs4231_capture_hw_params(struct snd_pcm_substream *substream,
853 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100855 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned char new_cdfr;
857 int err;
858
859 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
860 return err;
861 new_cdfr = snd_cs4231_get_format(chip, params_format(hw_params), params_channels(hw_params)) |
862 snd_cs4231_get_rate(params_rate(hw_params));
863 chip->set_capture_format(chip, hw_params, new_cdfr);
864 return 0;
865}
866
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100867static int snd_cs4231_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 return snd_pcm_lib_free_pages(substream);
870}
871
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100872static int snd_cs4231_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100874 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
875 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 unsigned long flags;
877 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
878 unsigned int count = snd_pcm_lib_period_bytes(substream);
879
880 spin_lock_irqsave(&chip->reg_lock, flags);
881 chip->c_dma_size = size;
882 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
883 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
884 count = snd_cs4231_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
885 if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
886 snd_cs4231_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
887 snd_cs4231_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
888 } else {
889 snd_cs4231_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
890 snd_cs4231_out(chip, CS4231_REC_UPR_CNT, (unsigned char) (count >> 8));
891 }
892 spin_unlock_irqrestore(&chip->reg_lock, flags);
893 return 0;
894}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100896static void snd_cs4231_overrange(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 unsigned long flags;
899 unsigned char res;
900
901 spin_lock_irqsave(&chip->reg_lock, flags);
902 res = snd_cs4231_in(chip, CS4231_TEST_INIT);
903 spin_unlock_irqrestore(&chip->reg_lock, flags);
904 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
905 chip->capture_substream->runtime->overrange++;
906}
907
David Howells7d12e782006-10-05 14:55:46 +0100908irqreturn_t snd_cs4231_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100910 struct snd_cs4231 *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unsigned char status;
912
913 status = snd_cs4231_in(chip, CS4231_IRQ_STATUS);
914 if (status & CS4231_TIMER_IRQ) {
915 if (chip->timer)
916 snd_timer_interrupt(chip->timer, chip->timer->sticks);
917 }
918 if (chip->single_dma && chip->hardware != CS4231_HW_INTERWAVE) {
919 if (status & CS4231_PLAYBACK_IRQ) {
920 if (chip->mode & CS4231_MODE_PLAY) {
921 if (chip->playback_substream)
922 snd_pcm_period_elapsed(chip->playback_substream);
923 }
924 if (chip->mode & CS4231_MODE_RECORD) {
925 if (chip->capture_substream) {
926 snd_cs4231_overrange(chip);
927 snd_pcm_period_elapsed(chip->capture_substream);
928 }
929 }
930 }
931 } else {
932 if (status & CS4231_PLAYBACK_IRQ) {
933 if (chip->playback_substream)
934 snd_pcm_period_elapsed(chip->playback_substream);
935 }
936 if (status & CS4231_RECORD_IRQ) {
937 if (chip->capture_substream) {
938 snd_cs4231_overrange(chip);
939 snd_pcm_period_elapsed(chip->capture_substream);
940 }
941 }
942 }
943
944 spin_lock(&chip->reg_lock);
945 snd_cs4231_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
946 spin_unlock(&chip->reg_lock);
947 return IRQ_HANDLED;
948}
949
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100950static snd_pcm_uframes_t snd_cs4231_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100952 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 size_t ptr;
954
955 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
956 return 0;
957 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
958 return bytes_to_frames(substream->runtime, ptr);
959}
960
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100961static snd_pcm_uframes_t snd_cs4231_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100963 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 size_t ptr;
965
966 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
967 return 0;
968 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
969 return bytes_to_frames(substream->runtime, ptr);
970}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972/*
973
974 */
975
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100976static int snd_cs4231_probe(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 unsigned long flags;
979 int i, id, rev;
980 unsigned char *ptr;
981 unsigned int hw;
982
983#if 0
984 snd_cs4231_debug(chip);
985#endif
986 id = 0;
987 for (i = 0; i < 50; i++) {
988 mb();
989 if (cs4231_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
990 udelay(2000);
991 else {
992 spin_lock_irqsave(&chip->reg_lock, flags);
993 snd_cs4231_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
994 id = snd_cs4231_in(chip, CS4231_MISC_INFO) & 0x0f;
995 spin_unlock_irqrestore(&chip->reg_lock, flags);
996 if (id == 0x0a)
997 break; /* this is valid value */
998 }
999 }
1000 snd_printdd("cs4231: port = 0x%lx, id = 0x%x\n", chip->port, id);
1001 if (id != 0x0a)
1002 return -ENODEV; /* no valid device found */
1003
1004 if (((hw = chip->hardware) & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
1005 rev = snd_cs4231_in(chip, CS4231_VERSION) & 0xe7;
1006 snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
1007 if (rev == 0x80) {
1008 unsigned char tmp = snd_cs4231_in(chip, 23);
1009 snd_cs4231_out(chip, 23, ~tmp);
1010 if (snd_cs4231_in(chip, 23) != tmp)
1011 chip->hardware = CS4231_HW_AD1845;
1012 else
1013 chip->hardware = CS4231_HW_CS4231;
1014 } else if (rev == 0xa0) {
1015 chip->hardware = CS4231_HW_CS4231A;
1016 } else if (rev == 0xa2) {
1017 chip->hardware = CS4231_HW_CS4232;
1018 } else if (rev == 0xb2) {
1019 chip->hardware = CS4231_HW_CS4232A;
1020 } else if (rev == 0x83) {
1021 chip->hardware = CS4231_HW_CS4236;
1022 } else if (rev == 0x03) {
1023 chip->hardware = CS4231_HW_CS4236B;
1024 } else {
1025 snd_printk("unknown CS chip with version 0x%x\n", rev);
1026 return -ENODEV; /* unknown CS4231 chip? */
1027 }
1028 }
1029 spin_lock_irqsave(&chip->reg_lock, flags);
1030 cs4231_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
1031 cs4231_outb(chip, CS4231P(STATUS), 0);
1032 mb();
1033 spin_unlock_irqrestore(&chip->reg_lock, flags);
1034
1035 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1036 switch (chip->hardware) {
1037 case CS4231_HW_INTERWAVE:
1038 chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
1039 break;
1040 case CS4231_HW_CS4235:
1041 case CS4231_HW_CS4236B:
1042 case CS4231_HW_CS4237B:
1043 case CS4231_HW_CS4238B:
1044 case CS4231_HW_CS4239:
1045 if (hw == CS4231_HW_DETECT3)
1046 chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
1047 else
1048 chip->hardware = CS4231_HW_CS4236;
1049 break;
1050 }
1051
1052 chip->image[CS4231_IFACE_CTRL] =
1053 (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
1054 (chip->single_dma ? CS4231_SINGLE_DMA : 0);
1055 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1056 chip->image[CS4231_ALT_FEATURE_2] = chip->hardware == CS4231_HW_INTERWAVE ? 0xc2 : 0x01;
1057 ptr = (unsigned char *) &chip->image;
1058 snd_cs4231_mce_down(chip);
1059 spin_lock_irqsave(&chip->reg_lock, flags);
1060 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
1061 snd_cs4231_out(chip, i, *ptr++);
1062 spin_unlock_irqrestore(&chip->reg_lock, flags);
1063 snd_cs4231_mce_up(chip);
1064 snd_cs4231_mce_down(chip);
1065
1066 mdelay(2);
1067
1068 /* ok.. try check hardware version for CS4236+ chips */
1069 if ((hw & CS4231_HW_TYPE_MASK) == CS4231_HW_DETECT) {
1070 if (chip->hardware == CS4231_HW_CS4236B) {
1071 rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
1072 snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
1073 id = snd_cs4236_ext_in(chip, CS4236_VERSION);
1074 snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
1075 snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
1076 if ((id & 0x1f) == 0x1d) { /* CS4235 */
1077 chip->hardware = CS4231_HW_CS4235;
1078 switch (id >> 5) {
1079 case 4:
1080 case 5:
1081 case 6:
1082 break;
1083 default:
1084 snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
1085 }
1086 } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
1087 switch (id >> 5) {
1088 case 4:
1089 case 5:
1090 case 6:
1091 case 7:
1092 chip->hardware = CS4231_HW_CS4236B;
1093 break;
1094 default:
1095 snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
1096 }
1097 } else if ((id & 0x1f) == 0x08) { /* CS4237B */
1098 chip->hardware = CS4231_HW_CS4237B;
1099 switch (id >> 5) {
1100 case 4:
1101 case 5:
1102 case 6:
1103 case 7:
1104 break;
1105 default:
1106 snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
1107 }
1108 } else if ((id & 0x1f) == 0x09) { /* CS4238B */
1109 chip->hardware = CS4231_HW_CS4238B;
1110 switch (id >> 5) {
1111 case 5:
1112 case 6:
1113 case 7:
1114 break;
1115 default:
1116 snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
1117 }
1118 } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
1119 chip->hardware = CS4231_HW_CS4239;
1120 switch (id >> 5) {
1121 case 4:
1122 case 5:
1123 case 6:
1124 break;
1125 default:
1126 snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
1127 }
1128 } else {
1129 snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
1130 }
1131 }
1132 }
1133 return 0; /* all things are ok.. */
1134}
1135
1136/*
1137
1138 */
1139
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001140static struct snd_pcm_hardware snd_cs4231_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1143 SNDRV_PCM_INFO_MMAP_VALID |
1144 SNDRV_PCM_INFO_RESUME |
1145 SNDRV_PCM_INFO_SYNC_START),
1146 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1147 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1148 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1149 .rate_min = 5510,
1150 .rate_max = 48000,
1151 .channels_min = 1,
1152 .channels_max = 2,
1153 .buffer_bytes_max = (128*1024),
1154 .period_bytes_min = 64,
1155 .period_bytes_max = (128*1024),
1156 .periods_min = 1,
1157 .periods_max = 1024,
1158 .fifo_size = 0,
1159};
1160
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001161static struct snd_pcm_hardware snd_cs4231_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1164 SNDRV_PCM_INFO_MMAP_VALID |
1165 SNDRV_PCM_INFO_RESUME |
1166 SNDRV_PCM_INFO_SYNC_START),
1167 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1168 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1169 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1170 .rate_min = 5510,
1171 .rate_max = 48000,
1172 .channels_min = 1,
1173 .channels_max = 2,
1174 .buffer_bytes_max = (128*1024),
1175 .period_bytes_min = 64,
1176 .period_bytes_max = (128*1024),
1177 .periods_min = 1,
1178 .periods_max = 1024,
1179 .fifo_size = 0,
1180};
1181
1182/*
1183
1184 */
1185
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001186static int snd_cs4231_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001188 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1189 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 int err;
1191
1192 runtime->hw = snd_cs4231_playback;
1193
1194 /* hardware bug in InterWave chipset */
1195 if (chip->hardware == CS4231_HW_INTERWAVE && chip->dma1 > 3)
1196 runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
1197
1198 /* hardware limitation of cheap chips */
1199 if (chip->hardware == CS4231_HW_CS4235 ||
1200 chip->hardware == CS4231_HW_CS4239)
1201 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1204 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
1205
1206 if (chip->claim_dma) {
1207 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
1208 return err;
1209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 if ((err = snd_cs4231_open(chip, CS4231_MODE_PLAY)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 if (chip->release_dma)
1213 chip->release_dma(chip, chip->dma_private_data, chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1215 return err;
1216 }
1217 chip->playback_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 snd_pcm_set_sync(substream);
1219 chip->rate_constraint(runtime);
1220 return 0;
1221}
1222
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001223static int snd_cs4231_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001225 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
1226 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int err;
1228
1229 runtime->hw = snd_cs4231_capture;
1230
1231 /* hardware limitation of cheap chips */
1232 if (chip->hardware == CS4231_HW_CS4235 ||
1233 chip->hardware == CS4231_HW_CS4239)
1234 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1237 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
1238
1239 if (chip->claim_dma) {
1240 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
1241 return err;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if ((err = snd_cs4231_open(chip, CS4231_MODE_RECORD)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (chip->release_dma)
1246 chip->release_dma(chip, chip->dma_private_data, chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1248 return err;
1249 }
1250 chip->capture_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 snd_pcm_set_sync(substream);
1252 chip->rate_constraint(runtime);
1253 return 0;
1254}
1255
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001256static int snd_cs4231_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001258 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 chip->playback_substream = NULL;
1261 snd_cs4231_close(chip, CS4231_MODE_PLAY);
1262 return 0;
1263}
1264
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001265static int snd_cs4231_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001267 struct snd_cs4231 *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 chip->capture_substream = NULL;
1270 snd_cs4231_close(chip, CS4231_MODE_RECORD);
1271 return 0;
1272}
1273
1274#ifdef CONFIG_PM
1275
1276/* lowlevel suspend callback for CS4231 */
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001277static void snd_cs4231_suspend(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 int reg;
1280 unsigned long flags;
1281
Takashi Iwai7bb35e202005-11-17 17:00:17 +01001282 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 spin_lock_irqsave(&chip->reg_lock, flags);
1284 for (reg = 0; reg < 32; reg++)
1285 chip->image[reg] = snd_cs4231_in(chip, reg);
1286 spin_unlock_irqrestore(&chip->reg_lock, flags);
1287}
1288
1289/* lowlevel resume callback for CS4231 */
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001290static void snd_cs4231_resume(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
1292 int reg;
1293 unsigned long flags;
Takashi Iwaia2c855b2005-11-18 18:52:39 +01001294 /* int timeout; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 snd_cs4231_mce_up(chip);
1297 spin_lock_irqsave(&chip->reg_lock, flags);
1298 for (reg = 0; reg < 32; reg++) {
1299 switch (reg) {
1300 case CS4231_VERSION:
1301 break;
1302 default:
1303 snd_cs4231_out(chip, reg, chip->image[reg]);
1304 break;
1305 }
1306 }
1307 spin_unlock_irqrestore(&chip->reg_lock, flags);
Takashi Iwaifa55f832005-11-17 17:48:30 +01001308#if 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 snd_cs4231_mce_down(chip);
1310#else
1311 /* The following is a workaround to avoid freeze after resume on TP600E.
1312 This is the first half of copy of snd_cs4231_mce_down(), but doesn't
1313 include rescheduling. -- iwai
1314 */
1315 snd_cs4231_busy_wait(chip);
1316 spin_lock_irqsave(&chip->reg_lock, flags);
1317 chip->mce_bit &= ~CS4231_MCE;
1318 timeout = cs4231_inb(chip, CS4231P(REGSEL));
1319 cs4231_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
1320 spin_unlock_irqrestore(&chip->reg_lock, flags);
1321 if (timeout == 0x80)
1322 snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
1323 if ((timeout & CS4231_MCE) == 0 ||
1324 !(chip->hardware & (CS4231_HW_CS4231_MASK | CS4231_HW_CS4232_MASK))) {
1325 return;
1326 }
1327 snd_cs4231_busy_wait(chip);
1328#endif
1329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330#endif /* CONFIG_PM */
1331
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001332static int snd_cs4231_free(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001334 release_and_free_resource(chip->res_port);
1335 release_and_free_resource(chip->res_cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (chip->irq >= 0) {
1337 disable_irq(chip->irq);
1338 if (!(chip->hwshare & CS4231_HWSHARE_IRQ))
1339 free_irq(chip->irq, (void *) chip);
1340 }
1341 if (!(chip->hwshare & CS4231_HWSHARE_DMA1) && chip->dma1 >= 0) {
1342 snd_dma_disable(chip->dma1);
1343 free_dma(chip->dma1);
1344 }
1345 if (!(chip->hwshare & CS4231_HWSHARE_DMA2) && chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
1346 snd_dma_disable(chip->dma2);
1347 free_dma(chip->dma2);
1348 }
1349 if (chip->timer)
1350 snd_device_free(chip->card, chip->timer);
1351 kfree(chip);
1352 return 0;
1353}
1354
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001355static int snd_cs4231_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001357 struct snd_cs4231 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return snd_cs4231_free(chip);
1359}
1360
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001361const char *snd_cs4231_chip_id(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
1363 switch (chip->hardware) {
1364 case CS4231_HW_CS4231: return "CS4231";
1365 case CS4231_HW_CS4231A: return "CS4231A";
1366 case CS4231_HW_CS4232: return "CS4232";
1367 case CS4231_HW_CS4232A: return "CS4232A";
1368 case CS4231_HW_CS4235: return "CS4235";
1369 case CS4231_HW_CS4236: return "CS4236";
1370 case CS4231_HW_CS4236B: return "CS4236B";
1371 case CS4231_HW_CS4237B: return "CS4237B";
1372 case CS4231_HW_CS4238B: return "CS4238B";
1373 case CS4231_HW_CS4239: return "CS4239";
1374 case CS4231_HW_INTERWAVE: return "AMD InterWave";
1375 case CS4231_HW_OPL3SA2: return chip->card->shortname;
1376 case CS4231_HW_AD1845: return "AD1845";
1377 default: return "???";
1378 }
1379}
1380
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001381static int snd_cs4231_new(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned short hardware,
1383 unsigned short hwshare,
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001384 struct snd_cs4231 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001386 struct snd_cs4231 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001389 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (chip == NULL)
1391 return -ENOMEM;
1392 chip->hardware = hardware;
1393 chip->hwshare = hwshare;
1394
1395 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001396 mutex_init(&chip->mce_mutex);
1397 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 chip->card = card;
1399 chip->rate_constraint = snd_cs4231_xrate;
1400 chip->set_playback_format = snd_cs4231_playback_format;
1401 chip->set_capture_format = snd_cs4231_capture_format;
1402 memcpy(&chip->image, &snd_cs4231_original_image, sizeof(snd_cs4231_original_image));
1403
1404 *rchip = chip;
1405 return 0;
1406}
1407
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001408int snd_cs4231_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 unsigned long port,
1410 unsigned long cport,
1411 int irq, int dma1, int dma2,
1412 unsigned short hardware,
1413 unsigned short hwshare,
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001414 struct snd_cs4231 ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001416 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 .dev_free = snd_cs4231_dev_free,
1418 };
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001419 struct snd_cs4231 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 int err;
1421
1422 err = snd_cs4231_new(card, hardware, hwshare, &chip);
1423 if (err < 0)
1424 return err;
1425
1426 chip->irq = -1;
1427 chip->dma1 = -1;
1428 chip->dma2 = -1;
1429
1430 if ((chip->res_port = request_region(port, 4, "CS4231")) == NULL) {
1431 snd_printk(KERN_ERR "cs4231: can't grab port 0x%lx\n", port);
1432 snd_cs4231_free(chip);
1433 return -EBUSY;
1434 }
1435 chip->port = port;
1436 if ((long)cport >= 0 && (chip->res_cport = request_region(cport, 8, "CS4232 Control")) == NULL) {
1437 snd_printk(KERN_ERR "cs4231: can't grab control port 0x%lx\n", cport);
1438 snd_cs4231_free(chip);
1439 return -ENODEV;
1440 }
1441 chip->cport = cport;
Thomas Gleixner65ca68b2006-07-01 19:29:46 -07001442 if (!(hwshare & CS4231_HWSHARE_IRQ) && request_irq(irq, snd_cs4231_interrupt, IRQF_DISABLED, "CS4231", (void *) chip)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 snd_printk(KERN_ERR "cs4231: can't grab IRQ %d\n", irq);
1444 snd_cs4231_free(chip);
1445 return -EBUSY;
1446 }
1447 chip->irq = irq;
1448 if (!(hwshare & CS4231_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
1449 snd_printk(KERN_ERR "cs4231: can't grab DMA1 %d\n", dma1);
1450 snd_cs4231_free(chip);
1451 return -EBUSY;
1452 }
1453 chip->dma1 = dma1;
1454 if (!(hwshare & CS4231_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
1455 snd_printk(KERN_ERR "cs4231: can't grab DMA2 %d\n", dma2);
1456 snd_cs4231_free(chip);
1457 return -EBUSY;
1458 }
1459 if (dma1 == dma2 || dma2 < 0) {
1460 chip->single_dma = 1;
1461 chip->dma2 = chip->dma1;
1462 } else
1463 chip->dma2 = dma2;
1464
1465 /* global setup */
1466 if (snd_cs4231_probe(chip) < 0) {
1467 snd_cs4231_free(chip);
1468 return -ENODEV;
1469 }
1470 snd_cs4231_init(chip);
1471
Takashi Iwaia9824c82005-11-17 17:51:00 +01001472#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (chip->hardware & CS4231_HW_CS4232_MASK) {
1474 if (chip->res_cport == NULL)
1475 snd_printk("CS4232 control port features are not accessible\n");
1476 }
Takashi Iwaia9824c82005-11-17 17:51:00 +01001477#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /* Register device */
1480 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1481 snd_cs4231_free(chip);
1482 return err;
1483 }
1484
1485#ifdef CONFIG_PM
1486 /* Power Management */
1487 chip->suspend = snd_cs4231_suspend;
1488 chip->resume = snd_cs4231_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489#endif
1490
1491 *rchip = chip;
1492 return 0;
1493}
1494
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001495static struct snd_pcm_ops snd_cs4231_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .open = snd_cs4231_playback_open,
1497 .close = snd_cs4231_playback_close,
1498 .ioctl = snd_pcm_lib_ioctl,
1499 .hw_params = snd_cs4231_playback_hw_params,
1500 .hw_free = snd_cs4231_playback_hw_free,
1501 .prepare = snd_cs4231_playback_prepare,
1502 .trigger = snd_cs4231_trigger,
1503 .pointer = snd_cs4231_playback_pointer,
1504};
1505
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001506static struct snd_pcm_ops snd_cs4231_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 .open = snd_cs4231_capture_open,
1508 .close = snd_cs4231_capture_close,
1509 .ioctl = snd_pcm_lib_ioctl,
1510 .hw_params = snd_cs4231_capture_hw_params,
1511 .hw_free = snd_cs4231_capture_hw_free,
1512 .prepare = snd_cs4231_capture_prepare,
1513 .trigger = snd_cs4231_trigger,
1514 .pointer = snd_cs4231_capture_pointer,
1515};
1516
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001517int snd_cs4231_pcm(struct snd_cs4231 *chip, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001519 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int err;
1521
1522 if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
1523 return err;
1524
1525 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001526 mutex_init(&chip->mce_mutex);
1527 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs4231_playback_ops);
1530 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs4231_capture_ops);
1531
1532 /* global setup */
1533 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 pcm->info_flags = 0;
1535 if (chip->single_dma)
1536 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1537 if (chip->hardware != CS4231_HW_INTERWAVE)
1538 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1539 strcpy(pcm->name, snd_cs4231_chip_id(chip));
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1542 snd_dma_isa_data(),
1543 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 chip->pcm = pcm;
1546 if (rpcm)
1547 *rpcm = pcm;
1548 return 0;
1549}
1550
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001551static void snd_cs4231_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001553 struct snd_cs4231 *chip = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 chip->timer = NULL;
1555}
1556
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001557int snd_cs4231_timer(struct snd_cs4231 *chip, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001559 struct snd_timer *timer;
1560 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 int err;
1562
1563 /* Timer initialization */
1564 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1565 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1566 tid.card = chip->card->number;
1567 tid.device = device;
1568 tid.subdevice = 0;
1569 if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
1570 return err;
1571 strcpy(timer->name, snd_cs4231_chip_id(chip));
1572 timer->private_data = chip;
1573 timer->private_free = snd_cs4231_timer_free;
1574 timer->hw = snd_cs4231_timer_table;
1575 chip->timer = timer;
1576 if (rtimer)
1577 *rtimer = timer;
1578 return 0;
1579}
1580
1581/*
1582 * MIXER part
1583 */
1584
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001585static int snd_cs4231_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 static char *texts[4] = {
1588 "Line", "Aux", "Mic", "Mix"
1589 };
1590 static char *opl3sa_texts[4] = {
1591 "Line", "CD", "Mic", "Mix"
1592 };
1593 static char *gusmax_texts[4] = {
1594 "Line", "Synth", "Mic", "Mix"
1595 };
1596 char **ptexts = texts;
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001597 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 snd_assert(chip->card != NULL, return -EINVAL);
1600 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1601 uinfo->count = 2;
1602 uinfo->value.enumerated.items = 4;
1603 if (uinfo->value.enumerated.item > 3)
1604 uinfo->value.enumerated.item = 3;
1605 if (!strcmp(chip->card->driver, "GUS MAX"))
1606 ptexts = gusmax_texts;
1607 switch (chip->hardware) {
1608 case CS4231_HW_INTERWAVE: ptexts = gusmax_texts; break;
1609 case CS4231_HW_OPL3SA2: ptexts = opl3sa_texts; break;
1610 }
1611 strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
1612 return 0;
1613}
1614
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001615static int snd_cs4231_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001617 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 unsigned long flags;
1619
1620 spin_lock_irqsave(&chip->reg_lock, flags);
1621 ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1622 ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1623 spin_unlock_irqrestore(&chip->reg_lock, flags);
1624 return 0;
1625}
1626
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001627static int snd_cs4231_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001629 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 unsigned long flags;
1631 unsigned short left, right;
1632 int change;
1633
1634 if (ucontrol->value.enumerated.item[0] > 3 ||
1635 ucontrol->value.enumerated.item[1] > 3)
1636 return -EINVAL;
1637 left = ucontrol->value.enumerated.item[0] << 6;
1638 right = ucontrol->value.enumerated.item[1] << 6;
1639 spin_lock_irqsave(&chip->reg_lock, flags);
1640 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1641 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1642 change = left != chip->image[CS4231_LEFT_INPUT] ||
1643 right != chip->image[CS4231_RIGHT_INPUT];
1644 snd_cs4231_out(chip, CS4231_LEFT_INPUT, left);
1645 snd_cs4231_out(chip, CS4231_RIGHT_INPUT, right);
1646 spin_unlock_irqrestore(&chip->reg_lock, flags);
1647 return change;
1648}
1649
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001650int snd_cs4231_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
1652 int mask = (kcontrol->private_value >> 16) & 0xff;
1653
1654 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1655 uinfo->count = 1;
1656 uinfo->value.integer.min = 0;
1657 uinfo->value.integer.max = mask;
1658 return 0;
1659}
1660
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001661int snd_cs4231_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001663 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 unsigned long flags;
1665 int reg = kcontrol->private_value & 0xff;
1666 int shift = (kcontrol->private_value >> 8) & 0xff;
1667 int mask = (kcontrol->private_value >> 16) & 0xff;
1668 int invert = (kcontrol->private_value >> 24) & 0xff;
1669
1670 spin_lock_irqsave(&chip->reg_lock, flags);
1671 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1672 spin_unlock_irqrestore(&chip->reg_lock, flags);
1673 if (invert)
1674 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1675 return 0;
1676}
1677
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001678int snd_cs4231_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001680 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 unsigned long flags;
1682 int reg = kcontrol->private_value & 0xff;
1683 int shift = (kcontrol->private_value >> 8) & 0xff;
1684 int mask = (kcontrol->private_value >> 16) & 0xff;
1685 int invert = (kcontrol->private_value >> 24) & 0xff;
1686 int change;
1687 unsigned short val;
1688
1689 val = (ucontrol->value.integer.value[0] & mask);
1690 if (invert)
1691 val = mask - val;
1692 val <<= shift;
1693 spin_lock_irqsave(&chip->reg_lock, flags);
1694 val = (chip->image[reg] & ~(mask << shift)) | val;
1695 change = val != chip->image[reg];
1696 snd_cs4231_out(chip, reg, val);
1697 spin_unlock_irqrestore(&chip->reg_lock, flags);
1698 return change;
1699}
1700
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001701int snd_cs4231_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 int mask = (kcontrol->private_value >> 24) & 0xff;
1704
1705 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1706 uinfo->count = 2;
1707 uinfo->value.integer.min = 0;
1708 uinfo->value.integer.max = mask;
1709 return 0;
1710}
1711
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001712int snd_cs4231_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001714 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 unsigned long flags;
1716 int left_reg = kcontrol->private_value & 0xff;
1717 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1718 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1719 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1720 int mask = (kcontrol->private_value >> 24) & 0xff;
1721 int invert = (kcontrol->private_value >> 22) & 1;
1722
1723 spin_lock_irqsave(&chip->reg_lock, flags);
1724 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1725 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1726 spin_unlock_irqrestore(&chip->reg_lock, flags);
1727 if (invert) {
1728 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1729 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1730 }
1731 return 0;
1732}
1733
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001734int snd_cs4231_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001736 struct snd_cs4231 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 unsigned long flags;
1738 int left_reg = kcontrol->private_value & 0xff;
1739 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1740 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1741 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1742 int mask = (kcontrol->private_value >> 24) & 0xff;
1743 int invert = (kcontrol->private_value >> 22) & 1;
1744 int change;
1745 unsigned short val1, val2;
1746
1747 val1 = ucontrol->value.integer.value[0] & mask;
1748 val2 = ucontrol->value.integer.value[1] & mask;
1749 if (invert) {
1750 val1 = mask - val1;
1751 val2 = mask - val2;
1752 }
1753 val1 <<= shift_left;
1754 val2 <<= shift_right;
1755 spin_lock_irqsave(&chip->reg_lock, flags);
1756 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1757 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1758 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
1759 snd_cs4231_out(chip, left_reg, val1);
1760 snd_cs4231_out(chip, right_reg, val2);
1761 spin_unlock_irqrestore(&chip->reg_lock, flags);
1762 return change;
1763}
1764
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001765static struct snd_kcontrol_new snd_cs4231_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766CS4231_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1767CS4231_DOUBLE("PCM Playback Volume", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1768CS4231_DOUBLE("Line Playback Switch", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1769CS4231_DOUBLE("Line Playback Volume", 0, CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1770CS4231_DOUBLE("Aux Playback Switch", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1771CS4231_DOUBLE("Aux Playback Volume", 0, CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1772CS4231_DOUBLE("Aux Playback Switch", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1773CS4231_DOUBLE("Aux Playback Volume", 1, CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1774CS4231_SINGLE("Mono Playback Switch", 0, CS4231_MONO_CTRL, 7, 1, 1),
1775CS4231_SINGLE("Mono Playback Volume", 0, CS4231_MONO_CTRL, 0, 15, 1),
1776CS4231_SINGLE("Mono Output Playback Switch", 0, CS4231_MONO_CTRL, 6, 1, 1),
1777CS4231_SINGLE("Mono Output Playback Bypass", 0, CS4231_MONO_CTRL, 5, 1, 0),
1778CS4231_DOUBLE("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
1779{
1780 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1781 .name = "Capture Source",
1782 .info = snd_cs4231_info_mux,
1783 .get = snd_cs4231_get_mux,
1784 .put = snd_cs4231_put_mux,
1785},
1786CS4231_DOUBLE("Mic Boost", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
1787CS4231_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
1788CS4231_SINGLE("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1)
1789};
1790
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001791int snd_cs4231_mixer(struct snd_cs4231 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001793 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 unsigned int idx;
1795 int err;
1796
1797 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
1798
1799 card = chip->card;
1800
1801 strcpy(card->mixername, chip->pcm->name);
1802
1803 for (idx = 0; idx < ARRAY_SIZE(snd_cs4231_controls); idx++) {
1804 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cs4231_controls[idx], chip))) < 0)
1805 return err;
1806 }
1807 return 0;
1808}
1809
1810EXPORT_SYMBOL(snd_cs4231_out);
1811EXPORT_SYMBOL(snd_cs4231_in);
1812EXPORT_SYMBOL(snd_cs4236_ext_out);
1813EXPORT_SYMBOL(snd_cs4236_ext_in);
1814EXPORT_SYMBOL(snd_cs4231_mce_up);
1815EXPORT_SYMBOL(snd_cs4231_mce_down);
1816EXPORT_SYMBOL(snd_cs4231_interrupt);
1817EXPORT_SYMBOL(snd_cs4231_chip_id);
1818EXPORT_SYMBOL(snd_cs4231_create);
1819EXPORT_SYMBOL(snd_cs4231_pcm);
1820EXPORT_SYMBOL(snd_cs4231_mixer);
1821EXPORT_SYMBOL(snd_cs4231_timer);
1822EXPORT_SYMBOL(snd_cs4231_info_single);
1823EXPORT_SYMBOL(snd_cs4231_get_single);
1824EXPORT_SYMBOL(snd_cs4231_put_single);
1825EXPORT_SYMBOL(snd_cs4231_info_double);
1826EXPORT_SYMBOL(snd_cs4231_get_double);
1827EXPORT_SYMBOL(snd_cs4231_put_double);
1828
1829/*
1830 * INIT part
1831 */
1832
1833static int __init alsa_cs4231_init(void)
1834{
1835 return 0;
1836}
1837
1838static void __exit alsa_cs4231_exit(void)
1839{
1840}
1841
1842module_init(alsa_cs4231_init)
1843module_exit(alsa_cs4231_exit)