blob: a982997805c4f74fffa2b00178c1d8f5669b978b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
4 *
5 * Bugs:
Krzysztof Helt9295aea2008-07-31 21:00:17 +02006 * - sometimes record brokes playback with WSS portion of
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Yamaha OPL3-SA3 chip
8 * - CS4231 (GUS MAX) - still trouble with occasional noises
Krzysztof Helt7779f752008-07-31 21:03:41 +02009 * - broken initialization?
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
28#include <linux/pm.h>
29#include <linux/init.h>
30#include <linux/interrupt.h>
31#include <linux/slab.h>
32#include <linux/ioport.h>
33#include <sound/core.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020034#include <sound/wss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <sound/pcm_params.h>
36
37#include <asm/io.h>
38#include <asm/dma.h>
39#include <asm/irq.h>
40
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020041MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070042MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
43MODULE_LICENSE("GPL");
44
45#if 0
46#define SNDRV_DEBUG_MCE
47#endif
48
49/*
50 * Some variables
51 */
52
53static unsigned char freq_bits[14] = {
54 /* 5510 */ 0x00 | CS4231_XTAL2,
55 /* 6620 */ 0x0E | CS4231_XTAL2,
56 /* 8000 */ 0x00 | CS4231_XTAL1,
57 /* 9600 */ 0x0E | CS4231_XTAL1,
58 /* 11025 */ 0x02 | CS4231_XTAL2,
59 /* 16000 */ 0x02 | CS4231_XTAL1,
60 /* 18900 */ 0x04 | CS4231_XTAL2,
61 /* 22050 */ 0x06 | CS4231_XTAL2,
62 /* 27042 */ 0x04 | CS4231_XTAL1,
63 /* 32000 */ 0x06 | CS4231_XTAL1,
64 /* 33075 */ 0x0C | CS4231_XTAL2,
65 /* 37800 */ 0x08 | CS4231_XTAL2,
66 /* 44100 */ 0x0A | CS4231_XTAL2,
67 /* 48000 */ 0x0C | CS4231_XTAL1
68};
69
70static unsigned int rates[14] = {
71 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
72 27042, 32000, 33075, 37800, 44100, 48000
73};
74
Takashi Iwaiba2375a2005-11-17 14:30:42 +010075static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Helt6c041b52007-09-06 15:03:59 +020076 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .list = rates,
78 .mask = 0,
79};
80
Krzysztof Helt7779f752008-07-31 21:03:41 +020081static int snd_wss_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Krzysztof Helt7779f752008-07-31 21:03:41 +020083 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
84 &hw_constraints_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Krzysztof Helt7779f752008-07-31 21:03:41 +020087static unsigned char snd_wss_original_image[32] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
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
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +0200123static unsigned char snd_opti93x_original_image[32] =
124{
125 0x00, /* 00/00 - l_mixout_outctrl */
126 0x00, /* 01/01 - r_mixout_outctrl */
127 0x88, /* 02/02 - l_cd_inctrl */
128 0x88, /* 03/03 - r_cd_inctrl */
129 0x88, /* 04/04 - l_a1/fm_inctrl */
130 0x88, /* 05/05 - r_a1/fm_inctrl */
131 0x80, /* 06/06 - l_dac_inctrl */
132 0x80, /* 07/07 - r_dac_inctrl */
133 0x00, /* 08/08 - ply_dataform_reg */
134 0x00, /* 09/09 - if_conf */
135 0x00, /* 0a/10 - pin_ctrl */
136 0x00, /* 0b/11 - err_init_reg */
137 0x0a, /* 0c/12 - id_reg */
138 0x00, /* 0d/13 - reserved */
139 0x00, /* 0e/14 - ply_upcount_reg */
140 0x00, /* 0f/15 - ply_lowcount_reg */
141 0x88, /* 10/16 - reserved/l_a1_inctrl */
142 0x88, /* 11/17 - reserved/r_a1_inctrl */
143 0x88, /* 12/18 - l_line_inctrl */
144 0x88, /* 13/19 - r_line_inctrl */
145 0x88, /* 14/20 - l_mic_inctrl */
146 0x88, /* 15/21 - r_mic_inctrl */
147 0x80, /* 16/22 - l_out_outctrl */
148 0x80, /* 17/23 - r_out_outctrl */
149 0x00, /* 18/24 - reserved */
150 0x00, /* 19/25 - reserved */
151 0x00, /* 1a/26 - reserved */
152 0x00, /* 1b/27 - reserved */
153 0x00, /* 1c/28 - cap_dataform_reg */
154 0x00, /* 1d/29 - reserved */
155 0x00, /* 1e/30 - cap_upcount_reg */
156 0x00 /* 1f/31 - cap_lowcount_reg */
157};
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159/*
160 * Basic I/O functions
161 */
162
Krzysztof Helt7779f752008-07-31 21:03:41 +0200163static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 outb(val, chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Krzysztof Helt7779f752008-07-31 21:03:41 +0200168static inline u8 wss_inb(struct snd_wss *chip, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return inb(chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Krzysztof Helt7779f752008-07-31 21:03:41 +0200173static void snd_wss_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200178 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200180 udelay(100);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200181}
182
Krzysztof Helt7779f752008-07-31 21:03:41 +0200183static void snd_wss_outm(struct snd_wss *chip, unsigned char reg,
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200184 unsigned char mask, unsigned char value)
185{
186 unsigned char tmp = (chip->image[reg] & mask) | value;
187
Krzysztof Helt7779f752008-07-31 21:03:41 +0200188 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200190 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
192#endif
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200193 chip->image[reg] = tmp;
194 if (!chip->calibrate_mute) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200195 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200196 wmb();
Krzysztof Helt7779f752008-07-31 21:03:41 +0200197 wss_outb(chip, CS4231P(REG), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mb();
199 }
200}
201
Krzysztof Helt7779f752008-07-31 21:03:41 +0200202static void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
203 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 int timeout;
206
207 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200208 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200210 udelay(10);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200211 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
212 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 mb();
214}
215
Krzysztof Helt7779f752008-07-31 21:03:41 +0200216void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200218 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200220 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
222#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200223 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
224 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 chip->image[reg] = value;
226 mb();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200227 snd_printdd("codec out - reg 0x%x = 0x%x\n",
228 chip->mce_bit | reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200230EXPORT_SYMBOL(snd_wss_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Krzysztof Helt7779f752008-07-31 21:03:41 +0200232unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200234 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200236 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
238#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200239 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 mb();
Krzysztof Helt7779f752008-07-31 21:03:41 +0200241 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200243EXPORT_SYMBOL(snd_wss_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Krzysztof Helt7779f752008-07-31 21:03:41 +0200245void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
246 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200248 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
249 wss_outb(chip, CS4231P(REG),
250 reg | (chip->image[CS4236_EXT_REG] & 0x01));
251 wss_outb(chip, CS4231P(REG), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 chip->eimage[CS4236_REG(reg)] = val;
253#if 0
254 printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
255#endif
256}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200257EXPORT_SYMBOL(snd_cs4236_ext_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Krzysztof Helt7779f752008-07-31 21:03:41 +0200259unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200261 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
262 wss_outb(chip, CS4231P(REG),
263 reg | (chip->image[CS4236_EXT_REG] & 0x01));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +0200265 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#else
267 {
268 unsigned char res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200269 res = wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
271 return res;
272 }
273#endif
274}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200275EXPORT_SYMBOL(snd_cs4236_ext_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277#if 0
278
Krzysztof Helt7779f752008-07-31 21:03:41 +0200279static void snd_wss_debug(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200281 printk(KERN_DEBUG
282 "CS4231 REGS: INDEX = 0x%02x "
283 " STATUS = 0x%02x\n",
284 wss_inb(chip, CS4231P(REGSEL),
285 wss_inb(chip, CS4231P(STATUS)));
286 printk(KERN_DEBUG
287 " 0x00: left input = 0x%02x "
288 " 0x10: alt 1 (CFIG 2) = 0x%02x\n",
289 snd_wss_in(chip, 0x00),
290 snd_wss_in(chip, 0x10));
291 printk(KERN_DEBUG
292 " 0x01: right input = 0x%02x "
293 " 0x11: alt 2 (CFIG 3) = 0x%02x\n",
294 snd_wss_in(chip, 0x01),
295 snd_wss_in(chip, 0x11));
296 printk(KERN_DEBUG
297 " 0x02: GF1 left input = 0x%02x "
298 " 0x12: left line in = 0x%02x\n",
299 snd_wss_in(chip, 0x02),
300 snd_wss_in(chip, 0x12));
301 printk(KERN_DEBUG
302 " 0x03: GF1 right input = 0x%02x "
303 " 0x13: right line in = 0x%02x\n",
304 snd_wss_in(chip, 0x03),
305 snd_wss_in(chip, 0x13));
306 printk(KERN_DEBUG
307 " 0x04: CD left input = 0x%02x "
308 " 0x14: timer low = 0x%02x\n",
309 snd_wss_in(chip, 0x04),
310 snd_wss_in(chip, 0x14));
311 printk(KERN_DEBUG
312 " 0x05: CD right input = 0x%02x "
313 " 0x15: timer high = 0x%02x\n",
314 snd_wss_in(chip, 0x05),
315 snd_wss_in(chip, 0x15));
316 printk(KERN_DEBUG
317 " 0x06: left output = 0x%02x "
318 " 0x16: left MIC (PnP) = 0x%02x\n",
319 snd_wss_in(chip, 0x06),
320 snd_wss_in(chip, 0x16));
321 printk(KERN_DEBUG
322 " 0x07: right output = 0x%02x "
323 " 0x17: right MIC (PnP) = 0x%02x\n",
324 snd_wss_in(chip, 0x07),
325 snd_wss_in(chip, 0x17));
326 printk(KERN_DEBUG
327 " 0x08: playback format = 0x%02x "
328 " 0x18: IRQ status = 0x%02x\n",
329 snd_wss_in(chip, 0x08),
330 snd_wss_in(chip, 0x18));
331 printk(KERN_DEBUG
332 " 0x09: iface (CFIG 1) = 0x%02x "
333 " 0x19: left line out = 0x%02x\n",
334 snd_wss_in(chip, 0x09),
335 snd_wss_in(chip, 0x19));
336 printk(KERN_DEBUG
337 " 0x0a: pin control = 0x%02x "
338 " 0x1a: mono control = 0x%02x\n",
339 snd_wss_in(chip, 0x0a),
340 snd_wss_in(chip, 0x1a));
341 printk(KERN_DEBUG
342 " 0x0b: init & status = 0x%02x "
343 " 0x1b: right line out = 0x%02x\n",
344 snd_wss_in(chip, 0x0b),
345 snd_wss_in(chip, 0x1b));
346 printk(KERN_DEBUG
347 " 0x0c: revision & mode = 0x%02x "
348 " 0x1c: record format = 0x%02x\n",
349 snd_wss_in(chip, 0x0c),
350 snd_wss_in(chip, 0x1c));
351 printk(KERN_DEBUG
352 " 0x0d: loopback = 0x%02x "
353 " 0x1d: var freq (PnP) = 0x%02x\n",
354 snd_wss_in(chip, 0x0d),
355 snd_wss_in(chip, 0x1d));
356 printk(KERN_DEBUG
357 " 0x0e: ply upr count = 0x%02x "
358 " 0x1e: ply lwr count = 0x%02x\n",
359 snd_wss_in(chip, 0x0e),
360 snd_wss_in(chip, 0x1e));
361 printk(KERN_DEBUG
362 " 0x0f: rec upr count = 0x%02x "
363 " 0x1f: rec lwr count = 0x%02x\n",
364 snd_wss_in(chip, 0x0f),
365 snd_wss_in(chip, 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368#endif
369
370/*
371 * CS4231 detection / MCE routines
372 */
373
Krzysztof Helt7779f752008-07-31 21:03:41 +0200374static void snd_wss_busy_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 int timeout;
377
378 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
379 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7779f752008-07-31 21:03:41 +0200380 wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /* end of cleanup sequence */
382 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200383 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200385 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Krzysztof Helt7779f752008-07-31 21:03:41 +0200388void snd_wss_mce_up(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 unsigned long flags;
391 int timeout;
392
Krzysztof Helt7779f752008-07-31 21:03:41 +0200393 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200395 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 snd_printk("mce_up - auto calibration time out (0)\n");
397#endif
398 spin_lock_irqsave(&chip->reg_lock, flags);
399 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200400 timeout = wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (timeout == 0x80)
402 snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
403 if (!(timeout & CS4231_MCE))
Krzysztof Helt7779f752008-07-31 21:03:41 +0200404 wss_outb(chip, CS4231P(REGSEL),
405 chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 spin_unlock_irqrestore(&chip->reg_lock, flags);
407}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200408EXPORT_SYMBOL(snd_wss_mce_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Krzysztof Helt7779f752008-07-31 21:03:41 +0200410void snd_wss_mce_down(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 unsigned long flags;
Takashi Iwaib875d652007-09-11 10:12:14 +0200413 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 int timeout;
415
Krzysztof Helt7779f752008-07-31 21:03:41 +0200416 snd_wss_busy_wait(chip);
Rene Hermand44df2d2007-09-10 23:22:55 +0200417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200419 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
421#endif
422 spin_lock_irqsave(&chip->reg_lock, flags);
423 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200424 timeout = wss_inb(chip, CS4231P(REGSEL));
425 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 spin_unlock_irqrestore(&chip->reg_lock, flags);
427 if (timeout == 0x80)
428 snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
429 if ((timeout & CS4231_MCE) == 0 ||
Krzysztof Helt7779f752008-07-31 21:03:41 +0200430 !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Rene Herman90cf9b82007-09-10 23:19:55 +0200434 /*
435 * Wait for (possible -- during init auto-calibration may not be set)
436 * calibration process to start. Needs upto 5 sample periods on AD1848
437 * which at the slowest possible rate of 5.5125 kHz means 907 us.
438 */
439 msleep(1);
Rene Hermand44df2d2007-09-10 23:22:55 +0200440
441 snd_printdd("(1) jiffies = %lu\n", jiffies);
442
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200443 /* check condition up to 250 ms */
Takashi Iwaib875d652007-09-11 10:12:14 +0200444 end_time = jiffies + msecs_to_jiffies(250);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200445 while (snd_wss_in(chip, CS4231_TEST_INIT) &
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200446 CS4231_CALIB_IN_PROGRESS) {
447
Takashi Iwaib875d652007-09-11 10:12:14 +0200448 if (time_after(jiffies, end_time)) {
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200449 snd_printk(KERN_ERR "mce_down - "
450 "auto calibration time out (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return;
452 }
Takashi Iwaib875d652007-09-11 10:12:14 +0200453 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200455
456 snd_printdd("(2) jiffies = %lu\n", jiffies);
457
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200458 /* check condition up to 100 ms */
Takashi Iwaib875d652007-09-11 10:12:14 +0200459 end_time = jiffies + msecs_to_jiffies(100);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200460 while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
Takashi Iwaib875d652007-09-11 10:12:14 +0200461 if (time_after(jiffies, end_time)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
463 return;
464 }
Takashi Iwaib875d652007-09-11 10:12:14 +0200465 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200467
468 snd_printdd("(3) jiffies = %lu\n", jiffies);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200469 snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200471EXPORT_SYMBOL(snd_wss_mce_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Krzysztof Helt7779f752008-07-31 21:03:41 +0200473static unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 switch (format & 0xe0) {
476 case CS4231_LINEAR_16:
477 case CS4231_LINEAR_16_BIG:
478 size >>= 1;
479 break;
480 case CS4231_ADPCM_16:
481 return size >> 2;
482 }
483 if (format & CS4231_STEREO)
484 size >>= 1;
485 return size;
486}
487
Krzysztof Helt7779f752008-07-31 21:03:41 +0200488static int snd_wss_trigger(struct snd_pcm_substream *substream,
489 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200491 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 int result = 0;
493 unsigned int what;
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100494 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int do_start;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 switch (cmd) {
498 case SNDRV_PCM_TRIGGER_START:
499 case SNDRV_PCM_TRIGGER_RESUME:
500 do_start = 1; break;
501 case SNDRV_PCM_TRIGGER_STOP:
502 case SNDRV_PCM_TRIGGER_SUSPEND:
503 do_start = 0; break;
504 default:
505 return -EINVAL;
506 }
507
508 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100509 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (s == chip->playback_substream) {
511 what |= CS4231_PLAYBACK_ENABLE;
512 snd_pcm_trigger_done(s, substream);
513 } else if (s == chip->capture_substream) {
514 what |= CS4231_RECORD_ENABLE;
515 snd_pcm_trigger_done(s, substream);
516 }
517 }
518 spin_lock(&chip->reg_lock);
519 if (do_start) {
520 chip->image[CS4231_IFACE_CTRL] |= what;
521 if (chip->trigger)
522 chip->trigger(chip, what, 1);
523 } else {
524 chip->image[CS4231_IFACE_CTRL] &= ~what;
525 if (chip->trigger)
526 chip->trigger(chip, what, 0);
527 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200528 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 spin_unlock(&chip->reg_lock);
530#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +0200531 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
533 return result;
534}
535
536/*
537 * CODEC I/O
538 */
539
Krzysztof Helt7779f752008-07-31 21:03:41 +0200540static unsigned char snd_wss_get_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 int i;
543
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200544 for (i = 0; i < ARRAY_SIZE(rates); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (rate == rates[i])
546 return freq_bits[i];
547 // snd_BUG();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200548 return freq_bits[ARRAY_SIZE(rates) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Krzysztof Helt7779f752008-07-31 21:03:41 +0200551static unsigned char snd_wss_get_format(struct snd_wss *chip,
552 int format,
553 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 unsigned char rformat;
556
557 rformat = CS4231_LINEAR_8;
558 switch (format) {
559 case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
560 case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
561 case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
562 case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
563 case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
564 }
565 if (channels > 1)
566 rformat |= CS4231_STEREO;
567#if 0
568 snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode);
569#endif
570 return rformat;
571}
572
Krzysztof Helt7779f752008-07-31 21:03:41 +0200573static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 unsigned long flags;
576
577 mute = mute ? 1 : 0;
578 spin_lock_irqsave(&chip->reg_lock, flags);
579 if (chip->calibrate_mute == mute) {
580 spin_unlock_irqrestore(&chip->reg_lock, flags);
581 return;
582 }
583 if (!mute) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200584 snd_wss_dout(chip, CS4231_LEFT_INPUT,
585 chip->image[CS4231_LEFT_INPUT]);
586 snd_wss_dout(chip, CS4231_RIGHT_INPUT,
587 chip->image[CS4231_RIGHT_INPUT]);
588 snd_wss_dout(chip, CS4231_LOOPBACK,
589 chip->image[CS4231_LOOPBACK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200591 snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
592 mute ? 0x80 : chip->image[CS4231_AUX1_LEFT_INPUT]);
593 snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
594 mute ? 0x80 : chip->image[CS4231_AUX1_RIGHT_INPUT]);
595 snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
596 mute ? 0x80 : chip->image[CS4231_AUX2_LEFT_INPUT]);
597 snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
598 mute ? 0x80 : chip->image[CS4231_AUX2_RIGHT_INPUT]);
599 snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
600 mute ? 0x80 : chip->image[CS4231_LEFT_OUTPUT]);
601 snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
602 mute ? 0x80 : chip->image[CS4231_RIGHT_OUTPUT]);
603 snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
604 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
605 snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
606 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
607 snd_wss_dout(chip, CS4231_MONO_CTRL,
608 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
609 if (chip->hardware == WSS_HW_INTERWAVE) {
610 snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
611 mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
612 snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
613 mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
614 snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
615 mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
616 snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
617 mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 chip->calibrate_mute = mute;
620 spin_unlock_irqrestore(&chip->reg_lock, flags);
621}
622
Krzysztof Helt7779f752008-07-31 21:03:41 +0200623static void snd_wss_playback_format(struct snd_wss *chip,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100624 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 unsigned char pdfr)
626{
627 unsigned long flags;
628 int full_calib = 1;
629
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100630 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200631 snd_wss_calibrate_mute(chip, 1);
632 if (chip->hardware == WSS_HW_CS4231A ||
633 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 spin_lock_irqsave(&chip->reg_lock, flags);
635 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200636 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
637 chip->image[CS4231_ALT_FEATURE_1] | 0x10);
638 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
639 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
640 chip->image[CS4231_PLAYBK_FORMAT]);
641 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
642 chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 udelay(100); /* Fixes audible clicks at least on GUS MAX */
644 full_calib = 0;
645 }
646 spin_unlock_irqrestore(&chip->reg_lock, flags);
647 }
648 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200649 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200651 if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
652 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
653 pdfr = (pdfr & 0xf0) |
654 (chip->image[CS4231_REC_FORMAT] & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200656 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200658 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200660 if (chip->hardware == WSS_HW_OPL3SA2)
Paul Vojtae2340462007-07-27 12:20:38 +0200661 udelay(100); /* this seems to help */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200662 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200664 snd_wss_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100665 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Krzysztof Helt7779f752008-07-31 21:03:41 +0200668static void snd_wss_capture_format(struct snd_wss *chip,
669 struct snd_pcm_hw_params *params,
670 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 unsigned long flags;
673 int full_calib = 1;
674
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100675 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200676 snd_wss_calibrate_mute(chip, 1);
677 if (chip->hardware == WSS_HW_CS4231A ||
678 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 spin_lock_irqsave(&chip->reg_lock, flags);
680 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
681 (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200682 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
683 chip->image[CS4231_ALT_FEATURE_1] | 0x20);
684 snd_wss_out(chip, CS4231_REC_FORMAT,
685 chip->image[CS4231_REC_FORMAT] = cdfr);
686 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
687 chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 full_calib = 0;
689 }
690 spin_unlock_irqrestore(&chip->reg_lock, flags);
691 }
692 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200693 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200695 if (chip->hardware != WSS_HW_INTERWAVE &&
696 !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
697 if (chip->single_dma)
698 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
699 else
700 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
701 (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
702 (cdfr & 0x0f));
703 spin_unlock_irqrestore(&chip->reg_lock, flags);
704 snd_wss_mce_down(chip);
705 snd_wss_mce_up(chip);
706 spin_lock_irqsave(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200708 snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200710 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200712 snd_wss_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100713 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/*
717 * Timer interface
718 */
719
Krzysztof Helt7779f752008-07-31 21:03:41 +0200720static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200722 struct snd_wss *chip = snd_timer_chip(timer);
723 if (chip->hardware & WSS_HW_CS4236B_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 14467;
725 else
726 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
727}
728
Krzysztof Helt7779f752008-07-31 21:03:41 +0200729static int snd_wss_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 unsigned long flags;
732 unsigned int ticks;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200733 struct snd_wss *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 spin_lock_irqsave(&chip->reg_lock, flags);
735 ticks = timer->sticks;
736 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
737 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
738 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200739 chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
740 snd_wss_out(chip, CS4231_TIMER_HIGH,
741 chip->image[CS4231_TIMER_HIGH]);
742 chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
743 snd_wss_out(chip, CS4231_TIMER_LOW,
744 chip->image[CS4231_TIMER_LOW]);
745 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
746 chip->image[CS4231_ALT_FEATURE_1] |
747 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749 spin_unlock_irqrestore(&chip->reg_lock, flags);
750 return 0;
751}
752
Krzysztof Helt7779f752008-07-31 21:03:41 +0200753static int snd_wss_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 unsigned long flags;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200756 struct snd_wss *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200758 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
759 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
760 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_unlock_irqrestore(&chip->reg_lock, flags);
762 return 0;
763}
764
Krzysztof Helt7779f752008-07-31 21:03:41 +0200765static void snd_wss_init(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 unsigned long flags;
768
Krzysztof Helt7779f752008-07-31 21:03:41 +0200769 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771#ifdef SNDRV_DEBUG_MCE
772 snd_printk("init: (1)\n");
773#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200774 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200776 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
777 CS4231_PLAYBACK_PIO |
778 CS4231_RECORD_ENABLE |
779 CS4231_RECORD_PIO |
780 CS4231_CALIB_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200782 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200784 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786#ifdef SNDRV_DEBUG_MCE
787 snd_printk("init: (2)\n");
788#endif
789
Krzysztof Helt7779f752008-07-31 21:03:41 +0200790 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200792 snd_wss_out(chip,
793 CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200795 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt7779f752008-07-31 21:03:41 +0200798 snd_printk("init: (3) - afei = 0x%x\n",
799 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800#endif
801
802 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200803 snd_wss_out(chip, CS4231_ALT_FEATURE_2,
804 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 spin_unlock_irqrestore(&chip->reg_lock, flags);
806
Krzysztof Helt7779f752008-07-31 21:03:41 +0200807 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200809 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
810 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200812 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814#ifdef SNDRV_DEBUG_MCE
815 snd_printk("init: (4)\n");
816#endif
817
Krzysztof Helt7779f752008-07-31 21:03:41 +0200818 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200820 snd_wss_out(chip, CS4231_REC_FORMAT, chip->image[CS4231_REC_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200822 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824#ifdef SNDRV_DEBUG_MCE
825 snd_printk("init: (5)\n");
826#endif
827}
828
Krzysztof Helt7779f752008-07-31 21:03:41 +0200829static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 unsigned long flags;
832
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100833 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if ((chip->mode & mode) ||
Krzysztof Helt7779f752008-07-31 21:03:41 +0200835 ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100836 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EAGAIN;
838 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200839 if (chip->mode & WSS_MODE_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 chip->mode |= mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100841 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return 0;
843 }
844 /* ok. now enable and ack CODEC IRQ */
845 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200846 snd_wss_out(chip, CS4231_IRQ_STATUS,
847 CS4231_PLAYBACK_IRQ |
848 CS4231_RECORD_IRQ |
849 CS4231_TIMER_IRQ);
850 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
851 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
852 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200854 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
855 snd_wss_out(chip, CS4231_IRQ_STATUS,
856 CS4231_PLAYBACK_IRQ |
857 CS4231_RECORD_IRQ |
858 CS4231_TIMER_IRQ);
859 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_unlock_irqrestore(&chip->reg_lock, flags);
861
862 chip->mode = mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100863 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
Krzysztof Helt7779f752008-07-31 21:03:41 +0200867static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 unsigned long flags;
870
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100871 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 chip->mode &= ~mode;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200873 if (chip->mode & WSS_MODE_OPEN) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100874 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return;
876 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200877 snd_wss_calibrate_mute(chip, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* disable IRQ */
880 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200881 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
882 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
883 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200885 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* now disable record & playback */
888
889 if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
890 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
891 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200892 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 spin_lock_irqsave(&chip->reg_lock, flags);
894 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
895 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200896 snd_wss_out(chip, CS4231_IFACE_CTRL,
897 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200899 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 spin_lock_irqsave(&chip->reg_lock, flags);
901 }
902
903 /* clear IRQ again */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200904 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
905 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
906 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 spin_unlock_irqrestore(&chip->reg_lock, flags);
908
Krzysztof Helt7779f752008-07-31 21:03:41 +0200909 snd_wss_calibrate_mute(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 chip->mode = 0;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100912 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
916 * timer open/close
917 */
918
Krzysztof Helt7779f752008-07-31 21:03:41 +0200919static int snd_wss_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200921 struct snd_wss *chip = snd_timer_chip(timer);
922 snd_wss_open(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return 0;
924}
925
Krzysztof Helt7779f752008-07-31 21:03:41 +0200926static int snd_wss_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200928 struct snd_wss *chip = snd_timer_chip(timer);
929 snd_wss_close(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return 0;
931}
932
Krzysztof Helt7779f752008-07-31 21:03:41 +0200933static struct snd_timer_hardware snd_wss_timer_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
935 .flags = SNDRV_TIMER_HW_AUTO,
936 .resolution = 9945,
937 .ticks = 65535,
Krzysztof Helt7779f752008-07-31 21:03:41 +0200938 .open = snd_wss_timer_open,
939 .close = snd_wss_timer_close,
940 .c_resolution = snd_wss_timer_resolution,
941 .start = snd_wss_timer_start,
942 .stop = snd_wss_timer_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943};
944
945/*
946 * ok.. exported functions..
947 */
948
Krzysztof Helt7779f752008-07-31 21:03:41 +0200949static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100950 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200952 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 unsigned char new_pdfr;
954 int err;
955
956 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
957 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200958 new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
959 params_channels(hw_params)) |
960 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 chip->set_playback_format(chip, hw_params, new_pdfr);
962 return 0;
963}
964
Krzysztof Helt7779f752008-07-31 21:03:41 +0200965static int snd_wss_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 return snd_pcm_lib_free_pages(substream);
968}
969
Krzysztof Helt7779f752008-07-31 21:03:41 +0200970static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200972 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100973 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 unsigned long flags;
975 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
976 unsigned int count = snd_pcm_lib_period_bytes(substream);
977
978 spin_lock_irqsave(&chip->reg_lock, flags);
979 chip->p_dma_size = size;
980 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
981 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200982 count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
983 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
984 snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock_irqrestore(&chip->reg_lock, flags);
986#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +0200987 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988#endif
989 return 0;
990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Krzysztof Helt7779f752008-07-31 21:03:41 +0200992static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100993 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200995 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 unsigned char new_cdfr;
997 int err;
998
999 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1000 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001001 new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
1002 params_channels(hw_params)) |
1003 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 chip->set_capture_format(chip, hw_params, new_cdfr);
1005 return 0;
1006}
1007
Krzysztof Helt7779f752008-07-31 21:03:41 +02001008static int snd_wss_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 return snd_pcm_lib_free_pages(substream);
1011}
1012
Krzysztof Helt7779f752008-07-31 21:03:41 +02001013static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001015 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001016 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned long flags;
1018 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1019 unsigned int count = snd_pcm_lib_period_bytes(substream);
1020
1021 spin_lock_irqsave(&chip->reg_lock, flags);
1022 chip->c_dma_size = size;
1023 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
1024 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001025 count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT], count) - 1;
1026 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
1027 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
1028 snd_wss_out(chip, CS4231_PLY_UPR_CNT,
1029 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001031 snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
1032 snd_wss_out(chip, CS4231_REC_UPR_CNT,
1033 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 spin_unlock_irqrestore(&chip->reg_lock, flags);
1036 return 0;
1037}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Krzysztof Helt7779f752008-07-31 21:03:41 +02001039void snd_wss_overrange(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 unsigned long flags;
1042 unsigned char res;
1043
1044 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001045 res = snd_wss_in(chip, CS4231_TEST_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 spin_unlock_irqrestore(&chip->reg_lock, flags);
1047 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
1048 chip->capture_substream->runtime->overrange++;
1049}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001050EXPORT_SYMBOL(snd_wss_overrange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Krzysztof Helt7779f752008-07-31 21:03:41 +02001052irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001054 struct snd_wss *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 unsigned char status;
1056
Krzysztof Helt7779f752008-07-31 21:03:41 +02001057 status = snd_wss_in(chip, CS4231_IRQ_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (status & CS4231_TIMER_IRQ) {
1059 if (chip->timer)
1060 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001061 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001062 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (status & CS4231_PLAYBACK_IRQ) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001064 if (chip->mode & WSS_MODE_PLAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (chip->playback_substream)
1066 snd_pcm_period_elapsed(chip->playback_substream);
1067 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001068 if (chip->mode & WSS_MODE_RECORD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001070 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 snd_pcm_period_elapsed(chip->capture_substream);
1072 }
1073 }
1074 }
1075 } else {
1076 if (status & CS4231_PLAYBACK_IRQ) {
1077 if (chip->playback_substream)
1078 snd_pcm_period_elapsed(chip->playback_substream);
1079 }
1080 if (status & CS4231_RECORD_IRQ) {
1081 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001082 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 snd_pcm_period_elapsed(chip->capture_substream);
1084 }
1085 }
1086 }
1087
1088 spin_lock(&chip->reg_lock);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001089 snd_wss_outm(chip, CS4231_IRQ_STATUS, ~CS4231_ALL_IRQS | ~status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 spin_unlock(&chip->reg_lock);
1091 return IRQ_HANDLED;
1092}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001093EXPORT_SYMBOL(snd_wss_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Krzysztof Helt7779f752008-07-31 21:03:41 +02001095static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001097 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 size_t ptr;
1099
1100 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1101 return 0;
1102 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1103 return bytes_to_frames(substream->runtime, ptr);
1104}
1105
Krzysztof Helt7779f752008-07-31 21:03:41 +02001106static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001108 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 size_t ptr;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1112 return 0;
1113 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1114 return bytes_to_frames(substream->runtime, ptr);
1115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117/*
1118
1119 */
1120
Krzysztof Helt7779f752008-07-31 21:03:41 +02001121static int snd_wss_probe(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 unsigned long flags;
1124 int i, id, rev;
1125 unsigned char *ptr;
1126 unsigned int hw;
1127
1128#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001129 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130#endif
1131 id = 0;
1132 for (i = 0; i < 50; i++) {
1133 mb();
Krzysztof Helt7779f752008-07-31 21:03:41 +02001134 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 udelay(2000);
1136 else {
1137 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001138 snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1139 id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 spin_unlock_irqrestore(&chip->reg_lock, flags);
1141 if (id == 0x0a)
1142 break; /* this is valid value */
1143 }
1144 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001145 snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (id != 0x0a)
1147 return -ENODEV; /* no valid device found */
1148
Krzysztof Helt7779f752008-07-31 21:03:41 +02001149 hw = chip->hardware;
1150 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
1151 rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
1153 if (rev == 0x80) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001154 unsigned char tmp = snd_wss_in(chip, 23);
1155 snd_wss_out(chip, 23, ~tmp);
1156 if (snd_wss_in(chip, 23) != tmp)
1157 chip->hardware = WSS_HW_AD1845;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001159 chip->hardware = WSS_HW_CS4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 } else if (rev == 0xa0) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001161 chip->hardware = WSS_HW_CS4231A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 } else if (rev == 0xa2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001163 chip->hardware = WSS_HW_CS4232;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 } else if (rev == 0xb2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001165 chip->hardware = WSS_HW_CS4232A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 } else if (rev == 0x83) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001167 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 } else if (rev == 0x03) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001169 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 } else {
1171 snd_printk("unknown CS chip with version 0x%x\n", rev);
1172 return -ENODEV; /* unknown CS4231 chip? */
1173 }
1174 }
1175 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001176 wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
1177 wss_outb(chip, CS4231P(STATUS), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 mb();
1179 spin_unlock_irqrestore(&chip->reg_lock, flags);
1180
1181 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
1182 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001183 case WSS_HW_INTERWAVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
1185 break;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001186 case WSS_HW_CS4235:
1187 case WSS_HW_CS4236B:
1188 case WSS_HW_CS4237B:
1189 case WSS_HW_CS4238B:
1190 case WSS_HW_CS4239:
1191 if (hw == WSS_HW_DETECT3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
1193 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001194 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196 }
1197
1198 chip->image[CS4231_IFACE_CTRL] =
1199 (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
1200 (chip->single_dma ? CS4231_SINGLE_DMA : 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001201 if (chip->hardware != WSS_HW_OPTI93X) {
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001202 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1203 chip->image[CS4231_ALT_FEATURE_2] =
Krzysztof Helt7779f752008-07-31 21:03:41 +02001204 chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 ptr = (unsigned char *) &chip->image;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001207 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 spin_lock_irqsave(&chip->reg_lock, flags);
1209 for (i = 0; i < 32; i++) /* ok.. fill all CS4231 registers */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001210 snd_wss_out(chip, i, *ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001212 snd_wss_mce_up(chip);
1213 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 mdelay(2);
1216
1217 /* ok.. try check hardware version for CS4236+ chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001218 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
1219 if (chip->hardware == WSS_HW_CS4236B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
1221 snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
1222 id = snd_cs4236_ext_in(chip, CS4236_VERSION);
1223 snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
1224 snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
1225 if ((id & 0x1f) == 0x1d) { /* CS4235 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001226 chip->hardware = WSS_HW_CS4235;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 switch (id >> 5) {
1228 case 4:
1229 case 5:
1230 case 6:
1231 break;
1232 default:
1233 snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
1234 }
1235 } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
1236 switch (id >> 5) {
1237 case 4:
1238 case 5:
1239 case 6:
1240 case 7:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001241 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 break;
1243 default:
1244 snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
1245 }
1246 } else if ((id & 0x1f) == 0x08) { /* CS4237B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001247 chip->hardware = WSS_HW_CS4237B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 switch (id >> 5) {
1249 case 4:
1250 case 5:
1251 case 6:
1252 case 7:
1253 break;
1254 default:
1255 snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
1256 }
1257 } else if ((id & 0x1f) == 0x09) { /* CS4238B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001258 chip->hardware = WSS_HW_CS4238B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 switch (id >> 5) {
1260 case 5:
1261 case 6:
1262 case 7:
1263 break;
1264 default:
1265 snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
1266 }
1267 } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001268 chip->hardware = WSS_HW_CS4239;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 switch (id >> 5) {
1270 case 4:
1271 case 5:
1272 case 6:
1273 break;
1274 default:
1275 snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
1276 }
1277 } else {
1278 snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
1279 }
1280 }
1281 }
1282 return 0; /* all things are ok.. */
1283}
1284
1285/*
1286
1287 */
1288
Krzysztof Helt7779f752008-07-31 21:03:41 +02001289static struct snd_pcm_hardware snd_wss_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1292 SNDRV_PCM_INFO_MMAP_VALID |
1293 SNDRV_PCM_INFO_RESUME |
1294 SNDRV_PCM_INFO_SYNC_START),
1295 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1296 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1297 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1298 .rate_min = 5510,
1299 .rate_max = 48000,
1300 .channels_min = 1,
1301 .channels_max = 2,
1302 .buffer_bytes_max = (128*1024),
1303 .period_bytes_min = 64,
1304 .period_bytes_max = (128*1024),
1305 .periods_min = 1,
1306 .periods_max = 1024,
1307 .fifo_size = 0,
1308};
1309
Krzysztof Helt7779f752008-07-31 21:03:41 +02001310static struct snd_pcm_hardware snd_wss_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1313 SNDRV_PCM_INFO_MMAP_VALID |
1314 SNDRV_PCM_INFO_RESUME |
1315 SNDRV_PCM_INFO_SYNC_START),
1316 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1317 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1318 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1319 .rate_min = 5510,
1320 .rate_max = 48000,
1321 .channels_min = 1,
1322 .channels_max = 2,
1323 .buffer_bytes_max = (128*1024),
1324 .period_bytes_min = 64,
1325 .period_bytes_max = (128*1024),
1326 .periods_min = 1,
1327 .periods_max = 1024,
1328 .fifo_size = 0,
1329};
1330
1331/*
1332
1333 */
1334
Krzysztof Helt7779f752008-07-31 21:03:41 +02001335static int snd_wss_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001337 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001338 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int err;
1340
Krzysztof Helt7779f752008-07-31 21:03:41 +02001341 runtime->hw = snd_wss_playback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 /* hardware bug in InterWave chipset */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001344 if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001345 runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001348 if (chip->hardware == WSS_HW_CS4235 ||
1349 chip->hardware == WSS_HW_CS4239)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1353 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
1354
1355 if (chip->claim_dma) {
1356 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
1357 return err;
1358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Krzysztof Helt7779f752008-07-31 21:03:41 +02001360 err = snd_wss_open(chip, WSS_MODE_PLAY);
1361 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (chip->release_dma)
1363 chip->release_dma(chip, chip->dma_private_data, chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1365 return err;
1366 }
1367 chip->playback_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 snd_pcm_set_sync(substream);
1369 chip->rate_constraint(runtime);
1370 return 0;
1371}
1372
Krzysztof Helt7779f752008-07-31 21:03:41 +02001373static int snd_wss_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001375 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001376 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 int err;
1378
Krzysztof Helt7779f752008-07-31 21:03:41 +02001379 runtime->hw = snd_wss_capture;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001382 if (chip->hardware == WSS_HW_CS4235 ||
1383 chip->hardware == WSS_HW_CS4239)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1387 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
1388
1389 if (chip->claim_dma) {
1390 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
1391 return err;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Krzysztof Helt7779f752008-07-31 21:03:41 +02001394 err = snd_wss_open(chip, WSS_MODE_RECORD);
1395 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 if (chip->release_dma)
1397 chip->release_dma(chip, chip->dma_private_data, chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1399 return err;
1400 }
1401 chip->capture_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 snd_pcm_set_sync(substream);
1403 chip->rate_constraint(runtime);
1404 return 0;
1405}
1406
Krzysztof Helt7779f752008-07-31 21:03:41 +02001407static int snd_wss_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001409 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 chip->playback_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001412 snd_wss_close(chip, WSS_MODE_PLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return 0;
1414}
1415
Krzysztof Helt7779f752008-07-31 21:03:41 +02001416static int snd_wss_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001418 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 chip->capture_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001421 snd_wss_close(chip, WSS_MODE_RECORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return 0;
1423}
1424
1425#ifdef CONFIG_PM
1426
1427/* lowlevel suspend callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001428static void snd_wss_suspend(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 int reg;
1431 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001432
Takashi Iwai7bb35e202005-11-17 17:00:17 +01001433 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 spin_lock_irqsave(&chip->reg_lock, flags);
1435 for (reg = 0; reg < 32; reg++)
Krzysztof Helt7779f752008-07-31 21:03:41 +02001436 chip->image[reg] = snd_wss_in(chip, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_unlock_irqrestore(&chip->reg_lock, flags);
1438}
1439
1440/* lowlevel resume callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001441static void snd_wss_resume(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 int reg;
1444 unsigned long flags;
Takashi Iwaia2c855b2005-11-18 18:52:39 +01001445 /* int timeout; */
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001446
Krzysztof Helt7779f752008-07-31 21:03:41 +02001447 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 spin_lock_irqsave(&chip->reg_lock, flags);
1449 for (reg = 0; reg < 32; reg++) {
1450 switch (reg) {
1451 case CS4231_VERSION:
1452 break;
1453 default:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001454 snd_wss_out(chip, reg, chip->image[reg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 }
1457 }
1458 spin_unlock_irqrestore(&chip->reg_lock, flags);
Takashi Iwaifa55f832005-11-17 17:48:30 +01001459#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +02001460 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461#else
1462 /* The following is a workaround to avoid freeze after resume on TP600E.
Krzysztof Helt7779f752008-07-31 21:03:41 +02001463 This is the first half of copy of snd_wss_mce_down(), but doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 include rescheduling. -- iwai
1465 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001466 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 spin_lock_irqsave(&chip->reg_lock, flags);
1468 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001469 timeout = wss_inb(chip, CS4231P(REGSEL));
1470 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 spin_unlock_irqrestore(&chip->reg_lock, flags);
1472 if (timeout == 0x80)
1473 snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
1474 if ((timeout & CS4231_MCE) == 0 ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001475 !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return;
1477 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001478 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479#endif
1480}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481#endif /* CONFIG_PM */
1482
Krzysztof Helt7779f752008-07-31 21:03:41 +02001483static int snd_wss_free(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001485 release_and_free_resource(chip->res_port);
1486 release_and_free_resource(chip->res_cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (chip->irq >= 0) {
1488 disable_irq(chip->irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001489 if (!(chip->hwshare & WSS_HWSHARE_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 free_irq(chip->irq, (void *) chip);
1491 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001492 if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 snd_dma_disable(chip->dma1);
1494 free_dma(chip->dma1);
1495 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001496 if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
1497 chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 snd_dma_disable(chip->dma2);
1499 free_dma(chip->dma2);
1500 }
1501 if (chip->timer)
1502 snd_device_free(chip->card, chip->timer);
1503 kfree(chip);
1504 return 0;
1505}
1506
Krzysztof Helt7779f752008-07-31 21:03:41 +02001507static int snd_wss_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001509 struct snd_wss *chip = device->device_data;
1510 return snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
Krzysztof Helt7779f752008-07-31 21:03:41 +02001513const char *snd_wss_chip_id(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001516 case WSS_HW_CS4231:
1517 return "CS4231";
1518 case WSS_HW_CS4231A:
1519 return "CS4231A";
1520 case WSS_HW_CS4232:
1521 return "CS4232";
1522 case WSS_HW_CS4232A:
1523 return "CS4232A";
1524 case WSS_HW_CS4235:
1525 return "CS4235";
1526 case WSS_HW_CS4236:
1527 return "CS4236";
1528 case WSS_HW_CS4236B:
1529 return "CS4236B";
1530 case WSS_HW_CS4237B:
1531 return "CS4237B";
1532 case WSS_HW_CS4238B:
1533 return "CS4238B";
1534 case WSS_HW_CS4239:
1535 return "CS4239";
1536 case WSS_HW_INTERWAVE:
1537 return "AMD InterWave";
1538 case WSS_HW_OPL3SA2:
1539 return chip->card->shortname;
1540 case WSS_HW_AD1845:
1541 return "AD1845";
1542 case WSS_HW_OPTI93X:
1543 return "OPTi 93x";
1544 default:
1545 return "???";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
1547}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001548EXPORT_SYMBOL(snd_wss_chip_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Krzysztof Helt7779f752008-07-31 21:03:41 +02001550static int snd_wss_new(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 unsigned short hardware,
1552 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001553 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001555 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001558 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (chip == NULL)
1560 return -ENOMEM;
1561 chip->hardware = hardware;
1562 chip->hwshare = hwshare;
1563
1564 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001565 mutex_init(&chip->mce_mutex);
1566 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 chip->card = card;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001568 chip->rate_constraint = snd_wss_xrate;
1569 chip->set_playback_format = snd_wss_playback_format;
1570 chip->set_capture_format = snd_wss_capture_format;
1571 if (chip->hardware == WSS_HW_OPTI93X)
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001572 memcpy(&chip->image, &snd_opti93x_original_image,
1573 sizeof(snd_opti93x_original_image));
1574 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001575 memcpy(&chip->image, &snd_wss_original_image,
1576 sizeof(snd_wss_original_image));
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001577
Krzysztof Helt7779f752008-07-31 21:03:41 +02001578 *rchip = chip;
1579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Krzysztof Helt7779f752008-07-31 21:03:41 +02001582int snd_wss_create(struct snd_card *card,
1583 unsigned long port,
1584 unsigned long cport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 int irq, int dma1, int dma2,
1586 unsigned short hardware,
1587 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001588 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001590 static struct snd_device_ops ops = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001591 .dev_free = snd_wss_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 };
Krzysztof Helt7779f752008-07-31 21:03:41 +02001593 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int err;
1595
Krzysztof Helt7779f752008-07-31 21:03:41 +02001596 err = snd_wss_new(card, hardware, hwshare, &chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (err < 0)
1598 return err;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 chip->irq = -1;
1601 chip->dma1 = -1;
1602 chip->dma2 = -1;
1603
Krzysztof Helt7779f752008-07-31 21:03:41 +02001604 chip->res_port = request_region(port, 4, "CS4231");
1605 if (!chip->res_port) {
1606 snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
1607 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 return -EBUSY;
1609 }
1610 chip->port = port;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001611 if ((long)cport >= 0) {
1612 chip->res_cport = request_region(cport, 8, "CS4232 Control");
1613 if (!chip->res_cport) {
1614 snd_printk(KERN_ERR
1615 "wss: can't grab control port 0x%lx\n", cport);
1616 snd_wss_free(chip);
1617 return -ENODEV;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620 chip->cport = cport;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001621 if (!(hwshare & WSS_HWSHARE_IRQ))
1622 if (request_irq(irq, snd_wss_interrupt, IRQF_DISABLED,
1623 "CS4231", (void *) chip)) {
1624 snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
1625 snd_wss_free(chip);
1626 return -EBUSY;
1627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 chip->irq = irq;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001629 if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "CS4231 - 1")) {
1630 snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
1631 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -EBUSY;
1633 }
1634 chip->dma1 = dma1;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001635 if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
1636 dma2 >= 0 && request_dma(dma2, "CS4231 - 2")) {
1637 snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
1638 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 return -EBUSY;
1640 }
1641 if (dma1 == dma2 || dma2 < 0) {
1642 chip->single_dma = 1;
1643 chip->dma2 = chip->dma1;
1644 } else
1645 chip->dma2 = dma2;
1646
1647 /* global setup */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001648 if (snd_wss_probe(chip) < 0) {
1649 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -ENODEV;
1651 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001652 snd_wss_init(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Takashi Iwaia9824c82005-11-17 17:51:00 +01001654#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001655 if (chip->hardware & WSS_HW_CS4232_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (chip->res_cport == NULL)
1657 snd_printk("CS4232 control port features are not accessible\n");
1658 }
Takashi Iwaia9824c82005-11-17 17:51:00 +01001659#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Register device */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001662 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1663 if (err < 0) {
1664 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return err;
1666 }
1667
1668#ifdef CONFIG_PM
1669 /* Power Management */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001670 chip->suspend = snd_wss_suspend;
1671 chip->resume = snd_wss_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672#endif
1673
1674 *rchip = chip;
1675 return 0;
1676}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001677EXPORT_SYMBOL(snd_wss_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Krzysztof Helt7779f752008-07-31 21:03:41 +02001679static struct snd_pcm_ops snd_wss_playback_ops = {
1680 .open = snd_wss_playback_open,
1681 .close = snd_wss_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .ioctl = snd_pcm_lib_ioctl,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001683 .hw_params = snd_wss_playback_hw_params,
1684 .hw_free = snd_wss_playback_hw_free,
1685 .prepare = snd_wss_playback_prepare,
1686 .trigger = snd_wss_trigger,
1687 .pointer = snd_wss_playback_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688};
1689
Krzysztof Helt7779f752008-07-31 21:03:41 +02001690static struct snd_pcm_ops snd_wss_capture_ops = {
1691 .open = snd_wss_capture_open,
1692 .close = snd_wss_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 .ioctl = snd_pcm_lib_ioctl,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001694 .hw_params = snd_wss_capture_hw_params,
1695 .hw_free = snd_wss_capture_hw_free,
1696 .prepare = snd_wss_capture_prepare,
1697 .trigger = snd_wss_trigger,
1698 .pointer = snd_wss_capture_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699};
1700
Krzysztof Helt7779f752008-07-31 21:03:41 +02001701int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001703 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 int err;
1705
1706 if ((err = snd_pcm_new(chip->card, "CS4231", device, 1, 1, &pcm)) < 0)
1707 return err;
1708
1709 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001710 mutex_init(&chip->mce_mutex);
1711 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Krzysztof Helt7779f752008-07-31 21:03:41 +02001713 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
1714 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /* global setup */
1717 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 pcm->info_flags = 0;
1719 if (chip->single_dma)
1720 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001721 if (chip->hardware != WSS_HW_INTERWAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001723 strcpy(pcm->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1726 snd_dma_isa_data(),
1727 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 chip->pcm = pcm;
1730 if (rpcm)
1731 *rpcm = pcm;
1732 return 0;
1733}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001734EXPORT_SYMBOL(snd_wss_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Krzysztof Helt7779f752008-07-31 21:03:41 +02001736static void snd_wss_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001738 struct snd_wss *chip = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 chip->timer = NULL;
1740}
1741
Krzysztof Helt7779f752008-07-31 21:03:41 +02001742int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001744 struct snd_timer *timer;
1745 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 int err;
1747
1748 /* Timer initialization */
1749 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1750 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1751 tid.card = chip->card->number;
1752 tid.device = device;
1753 tid.subdevice = 0;
1754 if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
1755 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001756 strcpy(timer->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 timer->private_data = chip;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001758 timer->private_free = snd_wss_timer_free;
1759 timer->hw = snd_wss_timer_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 chip->timer = timer;
1761 if (rtimer)
1762 *rtimer = timer;
1763 return 0;
1764}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001765EXPORT_SYMBOL(snd_wss_timer);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * MIXER part
1769 */
1770
Krzysztof Helt7779f752008-07-31 21:03:41 +02001771static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
1772 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 static char *texts[4] = {
1775 "Line", "Aux", "Mic", "Mix"
1776 };
1777 static char *opl3sa_texts[4] = {
1778 "Line", "CD", "Mic", "Mix"
1779 };
1780 static char *gusmax_texts[4] = {
1781 "Line", "Synth", "Mic", "Mix"
1782 };
1783 char **ptexts = texts;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001784 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 snd_assert(chip->card != NULL, return -EINVAL);
1787 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1788 uinfo->count = 2;
1789 uinfo->value.enumerated.items = 4;
1790 if (uinfo->value.enumerated.item > 3)
1791 uinfo->value.enumerated.item = 3;
1792 if (!strcmp(chip->card->driver, "GUS MAX"))
1793 ptexts = gusmax_texts;
1794 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001795 case WSS_HW_INTERWAVE:
1796 ptexts = gusmax_texts;
1797 break;
1798 case WSS_HW_OPL3SA2:
1799 ptexts = opl3sa_texts;
1800 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
1803 return 0;
1804}
1805
Krzysztof Helt7779f752008-07-31 21:03:41 +02001806static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
1807 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001809 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 spin_lock_irqsave(&chip->reg_lock, flags);
1813 ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1814 ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1815 spin_unlock_irqrestore(&chip->reg_lock, flags);
1816 return 0;
1817}
1818
Krzysztof Helt7779f752008-07-31 21:03:41 +02001819static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001822 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned long flags;
1824 unsigned short left, right;
1825 int change;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (ucontrol->value.enumerated.item[0] > 3 ||
1828 ucontrol->value.enumerated.item[1] > 3)
1829 return -EINVAL;
1830 left = ucontrol->value.enumerated.item[0] << 6;
1831 right = ucontrol->value.enumerated.item[1] << 6;
1832 spin_lock_irqsave(&chip->reg_lock, flags);
1833 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1834 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1835 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001836 right != chip->image[CS4231_RIGHT_INPUT];
1837 snd_wss_out(chip, CS4231_LEFT_INPUT, left);
1838 snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 spin_unlock_irqrestore(&chip->reg_lock, flags);
1840 return change;
1841}
1842
Krzysztof Helt7779f752008-07-31 21:03:41 +02001843int snd_wss_info_single(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
1846 int mask = (kcontrol->private_value >> 16) & 0xff;
1847
1848 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1849 uinfo->count = 1;
1850 uinfo->value.integer.min = 0;
1851 uinfo->value.integer.max = mask;
1852 return 0;
1853}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001854EXPORT_SYMBOL(snd_wss_info_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Krzysztof Helt7779f752008-07-31 21:03:41 +02001856int snd_wss_get_single(struct snd_kcontrol *kcontrol,
1857 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001859 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 unsigned long flags;
1861 int reg = kcontrol->private_value & 0xff;
1862 int shift = (kcontrol->private_value >> 8) & 0xff;
1863 int mask = (kcontrol->private_value >> 16) & 0xff;
1864 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 spin_lock_irqsave(&chip->reg_lock, flags);
1867 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
1868 spin_unlock_irqrestore(&chip->reg_lock, flags);
1869 if (invert)
1870 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1871 return 0;
1872}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001873EXPORT_SYMBOL(snd_wss_get_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Krzysztof Helt7779f752008-07-31 21:03:41 +02001875int snd_wss_put_single(struct snd_kcontrol *kcontrol,
1876 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001878 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 unsigned long flags;
1880 int reg = kcontrol->private_value & 0xff;
1881 int shift = (kcontrol->private_value >> 8) & 0xff;
1882 int mask = (kcontrol->private_value >> 16) & 0xff;
1883 int invert = (kcontrol->private_value >> 24) & 0xff;
1884 int change;
1885 unsigned short val;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 val = (ucontrol->value.integer.value[0] & mask);
1888 if (invert)
1889 val = mask - val;
1890 val <<= shift;
1891 spin_lock_irqsave(&chip->reg_lock, flags);
1892 val = (chip->image[reg] & ~(mask << shift)) | val;
1893 change = val != chip->image[reg];
Krzysztof Helt7779f752008-07-31 21:03:41 +02001894 snd_wss_out(chip, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 spin_unlock_irqrestore(&chip->reg_lock, flags);
1896 return change;
1897}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001898EXPORT_SYMBOL(snd_wss_put_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Krzysztof Helt7779f752008-07-31 21:03:41 +02001900int snd_wss_info_double(struct snd_kcontrol *kcontrol,
1901 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 int mask = (kcontrol->private_value >> 24) & 0xff;
1904
1905 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1906 uinfo->count = 2;
1907 uinfo->value.integer.min = 0;
1908 uinfo->value.integer.max = mask;
1909 return 0;
1910}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001911EXPORT_SYMBOL(snd_wss_info_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Krzysztof Helt7779f752008-07-31 21:03:41 +02001913int snd_wss_get_double(struct snd_kcontrol *kcontrol,
1914 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001916 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 unsigned long flags;
1918 int left_reg = kcontrol->private_value & 0xff;
1919 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1920 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1921 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1922 int mask = (kcontrol->private_value >> 24) & 0xff;
1923 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 spin_lock_irqsave(&chip->reg_lock, flags);
1926 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
1927 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
1928 spin_unlock_irqrestore(&chip->reg_lock, flags);
1929 if (invert) {
1930 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1931 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1932 }
1933 return 0;
1934}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001935EXPORT_SYMBOL(snd_wss_get_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Krzysztof Helt7779f752008-07-31 21:03:41 +02001937int snd_wss_put_double(struct snd_kcontrol *kcontrol,
1938 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001940 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 unsigned long flags;
1942 int left_reg = kcontrol->private_value & 0xff;
1943 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1944 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1945 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1946 int mask = (kcontrol->private_value >> 24) & 0xff;
1947 int invert = (kcontrol->private_value >> 22) & 1;
1948 int change;
1949 unsigned short val1, val2;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 val1 = ucontrol->value.integer.value[0] & mask;
1952 val2 = ucontrol->value.integer.value[1] & mask;
1953 if (invert) {
1954 val1 = mask - val1;
1955 val2 = mask - val2;
1956 }
1957 val1 <<= shift_left;
1958 val2 <<= shift_right;
1959 spin_lock_irqsave(&chip->reg_lock, flags);
1960 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
1961 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
1962 change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg];
Krzysztof Helt7779f752008-07-31 21:03:41 +02001963 snd_wss_out(chip, left_reg, val1);
1964 snd_wss_out(chip, right_reg, val2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 spin_unlock_irqrestore(&chip->reg_lock, flags);
1966 return change;
1967}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001968EXPORT_SYMBOL(snd_wss_put_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Krzysztof Helt7779f752008-07-31 21:03:41 +02001970static struct snd_kcontrol_new snd_wss_controls[] = {
1971WSS_DOUBLE("PCM Playback Switch", 0,
1972 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
1973WSS_DOUBLE("PCM Playback Volume", 0,
1974 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
1975WSS_DOUBLE("Line Playback Switch", 0,
1976 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
1977WSS_DOUBLE("Line Playback Volume", 0,
1978 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
1979WSS_DOUBLE("Aux Playback Switch", 0,
1980 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
1981WSS_DOUBLE("Aux Playback Volume", 0,
1982 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
1983WSS_DOUBLE("Aux Playback Switch", 1,
1984 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
1985WSS_DOUBLE("Aux Playback Volume", 1,
1986 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
1987WSS_SINGLE("Mono Playback Switch", 0,
1988 CS4231_MONO_CTRL, 7, 1, 1),
1989WSS_SINGLE("Mono Playback Volume", 0,
1990 CS4231_MONO_CTRL, 0, 15, 1),
1991WSS_SINGLE("Mono Output Playback Switch", 0,
1992 CS4231_MONO_CTRL, 6, 1, 1),
1993WSS_SINGLE("Mono Output Playback Bypass", 0,
1994 CS4231_MONO_CTRL, 5, 1, 0),
1995WSS_DOUBLE("Capture Volume", 0,
1996 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1999 .name = "Capture Source",
Krzysztof Helt7779f752008-07-31 21:03:41 +02002000 .info = snd_wss_info_mux,
2001 .get = snd_wss_get_mux,
2002 .put = snd_wss_put_mux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003},
Krzysztof Helt7779f752008-07-31 21:03:41 +02002004WSS_DOUBLE("Mic Boost", 0,
2005 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
2006WSS_SINGLE("Loopback Capture Switch", 0,
2007 CS4231_LOOPBACK, 0, 1, 0),
2008WSS_SINGLE("Loopback Capture Volume", 0,
2009 CS4231_LOOPBACK, 2, 63, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010};
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002011
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002012static struct snd_kcontrol_new snd_opti93x_controls[] = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02002013WSS_DOUBLE("Master Playback Switch", 0,
2014 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
2015WSS_DOUBLE("Master Playback Volume", 0,
2016 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
2017WSS_DOUBLE("PCM Playback Switch", 0,
2018 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
2019WSS_DOUBLE("PCM Playback Volume", 0,
2020 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
2021WSS_DOUBLE("FM Playback Switch", 0,
2022 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
2023WSS_DOUBLE("FM Playback Volume", 0,
2024 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
2025WSS_DOUBLE("Line Playback Switch", 0,
2026 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
2027WSS_DOUBLE("Line Playback Volume", 0,
2028 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
2029WSS_DOUBLE("Mic Playback Switch", 0,
2030 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
2031WSS_DOUBLE("Mic Playback Volume", 0,
2032 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
2033WSS_DOUBLE("Mic Boost", 0,
2034 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
2035WSS_DOUBLE("CD Playback Switch", 0,
2036 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
2037WSS_DOUBLE("CD Playback Volume", 0,
2038 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
2039WSS_DOUBLE("Aux Playback Switch", 0,
2040 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
2041WSS_DOUBLE("Aux Playback Volume", 0,
2042 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
2043WSS_DOUBLE("Capture Volume", 0,
2044 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002045{
2046 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2047 .name = "Capture Source",
Krzysztof Helt7779f752008-07-31 21:03:41 +02002048 .info = snd_wss_info_mux,
2049 .get = snd_wss_get_mux,
2050 .put = snd_wss_put_mux,
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002051}
2052};
2053
Krzysztof Helt7779f752008-07-31 21:03:41 +02002054int snd_wss_mixer(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01002056 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 unsigned int idx;
2058 int err;
2059
2060 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
2061
2062 card = chip->card;
2063
2064 strcpy(card->mixername, chip->pcm->name);
2065
Krzysztof Helt7779f752008-07-31 21:03:41 +02002066 if (chip->hardware == WSS_HW_OPTI93X)
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002067 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
2068 err = snd_ctl_add(card,
2069 snd_ctl_new1(&snd_opti93x_controls[idx],
2070 chip));
2071 if (err < 0)
2072 return err;
2073 }
2074 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02002075 for (idx = 0; idx < ARRAY_SIZE(snd_wss_controls); idx++) {
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002076 err = snd_ctl_add(card,
Krzysztof Helt7779f752008-07-31 21:03:41 +02002077 snd_ctl_new1(&snd_wss_controls[idx],
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002078 chip));
2079 if (err < 0)
2080 return err;
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 return 0;
2083}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002084EXPORT_SYMBOL(snd_wss_mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086/*
2087 * INIT part
2088 */
2089
Krzysztof Helt7779f752008-07-31 21:03:41 +02002090static int __init alsa_wss_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091{
2092 return 0;
2093}
2094
Krzysztof Helt7779f752008-07-31 21:03:41 +02002095static void __exit alsa_wss_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097}
2098
Krzysztof Helt7779f752008-07-31 21:03:41 +02002099module_init(alsa_wss_init);
2100module_exit(alsa_wss_exit);