blob: f2df77bd1308ca96d6bcf8d9f2769737e6d18908 [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>
Krzysztof Helt5664daa2008-07-31 21:08:32 +020036#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/io.h>
39#include <asm/dma.h>
40#include <asm/irq.h>
41
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020042MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
44MODULE_LICENSE("GPL");
45
46#if 0
47#define SNDRV_DEBUG_MCE
48#endif
49
50/*
51 * Some variables
52 */
53
54static unsigned char freq_bits[14] = {
55 /* 5510 */ 0x00 | CS4231_XTAL2,
56 /* 6620 */ 0x0E | CS4231_XTAL2,
57 /* 8000 */ 0x00 | CS4231_XTAL1,
58 /* 9600 */ 0x0E | CS4231_XTAL1,
59 /* 11025 */ 0x02 | CS4231_XTAL2,
60 /* 16000 */ 0x02 | CS4231_XTAL1,
61 /* 18900 */ 0x04 | CS4231_XTAL2,
62 /* 22050 */ 0x06 | CS4231_XTAL2,
63 /* 27042 */ 0x04 | CS4231_XTAL1,
64 /* 32000 */ 0x06 | CS4231_XTAL1,
65 /* 33075 */ 0x0C | CS4231_XTAL2,
66 /* 37800 */ 0x08 | CS4231_XTAL2,
67 /* 44100 */ 0x0A | CS4231_XTAL2,
68 /* 48000 */ 0x0C | CS4231_XTAL1
69};
70
71static unsigned int rates[14] = {
72 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
73 27042, 32000, 33075, 37800, 44100, 48000
74};
75
Takashi Iwaiba2375a2005-11-17 14:30:42 +010076static struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Helt6c041b52007-09-06 15:03:59 +020077 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .list = rates,
79 .mask = 0,
80};
81
Krzysztof Helt7779f752008-07-31 21:03:41 +020082static int snd_wss_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Krzysztof Helt7779f752008-07-31 21:03:41 +020084 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
85 &hw_constraints_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Krzysztof Helt7779f752008-07-31 21:03:41 +020088static unsigned char snd_wss_original_image[32] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 0x00, /* 00/00 - lic */
91 0x00, /* 01/01 - ric */
92 0x9f, /* 02/02 - la1ic */
93 0x9f, /* 03/03 - ra1ic */
94 0x9f, /* 04/04 - la2ic */
95 0x9f, /* 05/05 - ra2ic */
96 0xbf, /* 06/06 - loc */
97 0xbf, /* 07/07 - roc */
98 0x20, /* 08/08 - pdfr */
99 CS4231_AUTOCALIB, /* 09/09 - ic */
100 0x00, /* 0a/10 - pc */
101 0x00, /* 0b/11 - ti */
102 CS4231_MODE2, /* 0c/12 - mi */
103 0xfc, /* 0d/13 - lbc */
104 0x00, /* 0e/14 - pbru */
105 0x00, /* 0f/15 - pbrl */
106 0x80, /* 10/16 - afei */
107 0x01, /* 11/17 - afeii */
108 0x9f, /* 12/18 - llic */
109 0x9f, /* 13/19 - rlic */
110 0x00, /* 14/20 - tlb */
111 0x00, /* 15/21 - thb */
112 0x00, /* 16/22 - la3mic/reserved */
113 0x00, /* 17/23 - ra3mic/reserved */
114 0x00, /* 18/24 - afs */
115 0x00, /* 19/25 - lamoc/version */
116 0xcf, /* 1a/26 - mioc */
117 0x00, /* 1b/27 - ramoc/reserved */
118 0x20, /* 1c/28 - cdfr */
119 0x00, /* 1d/29 - res4 */
120 0x00, /* 1e/30 - cbru */
121 0x00, /* 1f/31 - cbrl */
122};
123
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +0200124static unsigned char snd_opti93x_original_image[32] =
125{
126 0x00, /* 00/00 - l_mixout_outctrl */
127 0x00, /* 01/01 - r_mixout_outctrl */
128 0x88, /* 02/02 - l_cd_inctrl */
129 0x88, /* 03/03 - r_cd_inctrl */
130 0x88, /* 04/04 - l_a1/fm_inctrl */
131 0x88, /* 05/05 - r_a1/fm_inctrl */
132 0x80, /* 06/06 - l_dac_inctrl */
133 0x80, /* 07/07 - r_dac_inctrl */
134 0x00, /* 08/08 - ply_dataform_reg */
135 0x00, /* 09/09 - if_conf */
136 0x00, /* 0a/10 - pin_ctrl */
137 0x00, /* 0b/11 - err_init_reg */
138 0x0a, /* 0c/12 - id_reg */
139 0x00, /* 0d/13 - reserved */
140 0x00, /* 0e/14 - ply_upcount_reg */
141 0x00, /* 0f/15 - ply_lowcount_reg */
142 0x88, /* 10/16 - reserved/l_a1_inctrl */
143 0x88, /* 11/17 - reserved/r_a1_inctrl */
144 0x88, /* 12/18 - l_line_inctrl */
145 0x88, /* 13/19 - r_line_inctrl */
146 0x88, /* 14/20 - l_mic_inctrl */
147 0x88, /* 15/21 - r_mic_inctrl */
148 0x80, /* 16/22 - l_out_outctrl */
149 0x80, /* 17/23 - r_out_outctrl */
150 0x00, /* 18/24 - reserved */
151 0x00, /* 19/25 - reserved */
152 0x00, /* 1a/26 - reserved */
153 0x00, /* 1b/27 - reserved */
154 0x00, /* 1c/28 - cap_dataform_reg */
155 0x00, /* 1d/29 - reserved */
156 0x00, /* 1e/30 - cap_upcount_reg */
157 0x00 /* 1f/31 - cap_lowcount_reg */
158};
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/*
161 * Basic I/O functions
162 */
163
Krzysztof Helt7779f752008-07-31 21:03:41 +0200164static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 outb(val, chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Krzysztof Helt7779f752008-07-31 21:03:41 +0200169static inline u8 wss_inb(struct snd_wss *chip, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return inb(chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Krzysztof Helt7779f752008-07-31 21:03:41 +0200174static void snd_wss_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200179 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200181 udelay(100);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200182}
183
Krzysztof Helt7779f752008-07-31 21:03:41 +0200184static void snd_wss_outm(struct snd_wss *chip, unsigned char reg,
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200185 unsigned char mask, unsigned char value)
186{
187 unsigned char tmp = (chip->image[reg] & mask) | value;
188
Krzysztof Helt7779f752008-07-31 21:03:41 +0200189 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200191 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 snd_printk("outm: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
193#endif
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200194 chip->image[reg] = tmp;
195 if (!chip->calibrate_mute) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200196 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200197 wmb();
Krzysztof Helt7779f752008-07-31 21:03:41 +0200198 wss_outb(chip, CS4231P(REG), tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 mb();
200 }
201}
202
Krzysztof Helt7779f752008-07-31 21:03:41 +0200203static void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
204 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int timeout;
207
208 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200209 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200211 udelay(10);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200212 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
213 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 mb();
215}
216
Krzysztof Helt7779f752008-07-31 21:03:41 +0200217void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200219 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200221 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 snd_printk("out: auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value);
223#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200224 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
225 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 chip->image[reg] = value;
227 mb();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200228 snd_printdd("codec out - reg 0x%x = 0x%x\n",
229 chip->mce_bit | reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200231EXPORT_SYMBOL(snd_wss_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Krzysztof Helt7779f752008-07-31 21:03:41 +0200233unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200235 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200237 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 snd_printk("in: auto calibration time out - reg = 0x%x\n", reg);
239#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200240 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 mb();
Krzysztof Helt7779f752008-07-31 21:03:41 +0200242 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200244EXPORT_SYMBOL(snd_wss_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Krzysztof Helt7779f752008-07-31 21:03:41 +0200246void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
247 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200249 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
250 wss_outb(chip, CS4231P(REG),
251 reg | (chip->image[CS4236_EXT_REG] & 0x01));
252 wss_outb(chip, CS4231P(REG), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 chip->eimage[CS4236_REG(reg)] = val;
254#if 0
255 printk("ext out : reg = 0x%x, val = 0x%x\n", reg, val);
256#endif
257}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200258EXPORT_SYMBOL(snd_cs4236_ext_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Krzysztof Helt7779f752008-07-31 21:03:41 +0200260unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200262 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
263 wss_outb(chip, CS4231P(REG),
264 reg | (chip->image[CS4236_EXT_REG] & 0x01));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +0200266 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#else
268 {
269 unsigned char res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200270 res = wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 printk("ext in : reg = 0x%x, val = 0x%x\n", reg, res);
272 return res;
273 }
274#endif
275}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200276EXPORT_SYMBOL(snd_cs4236_ext_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278#if 0
279
Krzysztof Helt7779f752008-07-31 21:03:41 +0200280static void snd_wss_debug(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200282 printk(KERN_DEBUG
283 "CS4231 REGS: INDEX = 0x%02x "
284 " STATUS = 0x%02x\n",
285 wss_inb(chip, CS4231P(REGSEL),
286 wss_inb(chip, CS4231P(STATUS)));
287 printk(KERN_DEBUG
288 " 0x00: left input = 0x%02x "
289 " 0x10: alt 1 (CFIG 2) = 0x%02x\n",
290 snd_wss_in(chip, 0x00),
291 snd_wss_in(chip, 0x10));
292 printk(KERN_DEBUG
293 " 0x01: right input = 0x%02x "
294 " 0x11: alt 2 (CFIG 3) = 0x%02x\n",
295 snd_wss_in(chip, 0x01),
296 snd_wss_in(chip, 0x11));
297 printk(KERN_DEBUG
298 " 0x02: GF1 left input = 0x%02x "
299 " 0x12: left line in = 0x%02x\n",
300 snd_wss_in(chip, 0x02),
301 snd_wss_in(chip, 0x12));
302 printk(KERN_DEBUG
303 " 0x03: GF1 right input = 0x%02x "
304 " 0x13: right line in = 0x%02x\n",
305 snd_wss_in(chip, 0x03),
306 snd_wss_in(chip, 0x13));
307 printk(KERN_DEBUG
308 " 0x04: CD left input = 0x%02x "
309 " 0x14: timer low = 0x%02x\n",
310 snd_wss_in(chip, 0x04),
311 snd_wss_in(chip, 0x14));
312 printk(KERN_DEBUG
313 " 0x05: CD right input = 0x%02x "
314 " 0x15: timer high = 0x%02x\n",
315 snd_wss_in(chip, 0x05),
316 snd_wss_in(chip, 0x15));
317 printk(KERN_DEBUG
318 " 0x06: left output = 0x%02x "
319 " 0x16: left MIC (PnP) = 0x%02x\n",
320 snd_wss_in(chip, 0x06),
321 snd_wss_in(chip, 0x16));
322 printk(KERN_DEBUG
323 " 0x07: right output = 0x%02x "
324 " 0x17: right MIC (PnP) = 0x%02x\n",
325 snd_wss_in(chip, 0x07),
326 snd_wss_in(chip, 0x17));
327 printk(KERN_DEBUG
328 " 0x08: playback format = 0x%02x "
329 " 0x18: IRQ status = 0x%02x\n",
330 snd_wss_in(chip, 0x08),
331 snd_wss_in(chip, 0x18));
332 printk(KERN_DEBUG
333 " 0x09: iface (CFIG 1) = 0x%02x "
334 " 0x19: left line out = 0x%02x\n",
335 snd_wss_in(chip, 0x09),
336 snd_wss_in(chip, 0x19));
337 printk(KERN_DEBUG
338 " 0x0a: pin control = 0x%02x "
339 " 0x1a: mono control = 0x%02x\n",
340 snd_wss_in(chip, 0x0a),
341 snd_wss_in(chip, 0x1a));
342 printk(KERN_DEBUG
343 " 0x0b: init & status = 0x%02x "
344 " 0x1b: right line out = 0x%02x\n",
345 snd_wss_in(chip, 0x0b),
346 snd_wss_in(chip, 0x1b));
347 printk(KERN_DEBUG
348 " 0x0c: revision & mode = 0x%02x "
349 " 0x1c: record format = 0x%02x\n",
350 snd_wss_in(chip, 0x0c),
351 snd_wss_in(chip, 0x1c));
352 printk(KERN_DEBUG
353 " 0x0d: loopback = 0x%02x "
354 " 0x1d: var freq (PnP) = 0x%02x\n",
355 snd_wss_in(chip, 0x0d),
356 snd_wss_in(chip, 0x1d));
357 printk(KERN_DEBUG
358 " 0x0e: ply upr count = 0x%02x "
359 " 0x1e: ply lwr count = 0x%02x\n",
360 snd_wss_in(chip, 0x0e),
361 snd_wss_in(chip, 0x1e));
362 printk(KERN_DEBUG
363 " 0x0f: rec upr count = 0x%02x "
364 " 0x1f: rec lwr count = 0x%02x\n",
365 snd_wss_in(chip, 0x0f),
366 snd_wss_in(chip, 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
369#endif
370
371/*
372 * CS4231 detection / MCE routines
373 */
374
Krzysztof Helt7779f752008-07-31 21:03:41 +0200375static void snd_wss_busy_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 int timeout;
378
379 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
380 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7779f752008-07-31 21:03:41 +0200381 wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /* end of cleanup sequence */
Krzysztof Heltead893c2008-07-31 21:09:32 +0200383 for (timeout = 25000;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200384 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200386 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Krzysztof Helt7779f752008-07-31 21:03:41 +0200389void snd_wss_mce_up(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned long flags;
392 int timeout;
393
Krzysztof Helt7779f752008-07-31 21:03:41 +0200394 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200396 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 snd_printk("mce_up - auto calibration time out (0)\n");
398#endif
399 spin_lock_irqsave(&chip->reg_lock, flags);
400 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200401 timeout = wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (timeout == 0x80)
403 snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port);
404 if (!(timeout & CS4231_MCE))
Krzysztof Helt7779f752008-07-31 21:03:41 +0200405 wss_outb(chip, CS4231P(REGSEL),
406 chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_unlock_irqrestore(&chip->reg_lock, flags);
408}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200409EXPORT_SYMBOL(snd_wss_mce_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Krzysztof Helt7779f752008-07-31 21:03:41 +0200411void snd_wss_mce_down(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned long flags;
Takashi Iwaib875d652007-09-11 10:12:14 +0200414 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int timeout;
Krzysztof Heltead893c2008-07-31 21:09:32 +0200416 int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Krzysztof Helt7779f752008-07-31 21:03:41 +0200418 snd_wss_busy_wait(chip);
Rene Hermand44df2d2007-09-10 23:22:55 +0200419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200421 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", (long)CS4231P(REGSEL));
423#endif
424 spin_lock_irqsave(&chip->reg_lock, flags);
425 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200426 timeout = wss_inb(chip, CS4231P(REGSEL));
427 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 spin_unlock_irqrestore(&chip->reg_lock, flags);
429 if (timeout == 0x80)
430 snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200431 if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return;
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]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200603 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
604 snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
605 mute ? 0x80 : chip->image[CS4231_LEFT_LINE_IN]);
606 snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
607 mute ? 0x80 : chip->image[CS4231_RIGHT_LINE_IN]);
608 snd_wss_dout(chip, CS4231_MONO_CTRL,
609 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
610 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200611 if (chip->hardware == WSS_HW_INTERWAVE) {
612 snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
613 mute ? 0x80 : chip->image[CS4231_LEFT_MIC_INPUT]);
614 snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
615 mute ? 0x80 : chip->image[CS4231_RIGHT_MIC_INPUT]);
616 snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
617 mute ? 0x80 : chip->image[CS4231_LINE_LEFT_OUTPUT]);
618 snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
619 mute ? 0x80 : chip->image[CS4231_LINE_RIGHT_OUTPUT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621 chip->calibrate_mute = mute;
622 spin_unlock_irqrestore(&chip->reg_lock, flags);
623}
624
Krzysztof Helt7779f752008-07-31 21:03:41 +0200625static void snd_wss_playback_format(struct snd_wss *chip,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100626 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 unsigned char pdfr)
628{
629 unsigned long flags;
630 int full_calib = 1;
631
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100632 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200633 snd_wss_calibrate_mute(chip, 1);
634 if (chip->hardware == WSS_HW_CS4231A ||
635 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_lock_irqsave(&chip->reg_lock, flags);
637 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200638 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
639 chip->image[CS4231_ALT_FEATURE_1] | 0x10);
640 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
641 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
642 chip->image[CS4231_PLAYBK_FORMAT]);
643 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
644 chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 udelay(100); /* Fixes audible clicks at least on GUS MAX */
646 full_calib = 0;
647 }
648 spin_unlock_irqrestore(&chip->reg_lock, flags);
649 }
650 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200651 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200653 if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
654 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
655 pdfr = (pdfr & 0xf0) |
656 (chip->image[CS4231_REC_FORMAT] & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200658 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200660 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200662 if (chip->hardware == WSS_HW_OPL3SA2)
Paul Vojtae2340462007-07-27 12:20:38 +0200663 udelay(100); /* this seems to help */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200664 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200666 snd_wss_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100667 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Krzysztof Helt7779f752008-07-31 21:03:41 +0200670static void snd_wss_capture_format(struct snd_wss *chip,
671 struct snd_pcm_hw_params *params,
672 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 unsigned long flags;
675 int full_calib = 1;
676
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100677 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200678 snd_wss_calibrate_mute(chip, 1);
679 if (chip->hardware == WSS_HW_CS4231A ||
680 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_lock_irqsave(&chip->reg_lock, flags);
682 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
683 (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200684 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
685 chip->image[CS4231_ALT_FEATURE_1] | 0x20);
686 snd_wss_out(chip, CS4231_REC_FORMAT,
687 chip->image[CS4231_REC_FORMAT] = cdfr);
688 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
689 chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 full_calib = 0;
691 }
692 spin_unlock_irqrestore(&chip->reg_lock, flags);
693 }
694 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200695 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200697 if (chip->hardware != WSS_HW_INTERWAVE &&
698 !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
699 if (chip->single_dma)
700 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
701 else
702 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
703 (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
704 (cdfr & 0x0f));
705 spin_unlock_irqrestore(&chip->reg_lock, flags);
706 snd_wss_mce_down(chip);
707 snd_wss_mce_up(chip);
708 spin_lock_irqsave(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Krzysztof Heltead893c2008-07-31 21:09:32 +0200710 if (chip->hardware & WSS_HW_AD1848_MASK)
711 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
712 else
713 snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200715 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200717 snd_wss_calibrate_mute(chip, 0);
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100718 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
722 * Timer interface
723 */
724
Krzysztof Helt7779f752008-07-31 21:03:41 +0200725static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200727 struct snd_wss *chip = snd_timer_chip(timer);
728 if (chip->hardware & WSS_HW_CS4236B_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return 14467;
730 else
731 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
732}
733
Krzysztof Helt7779f752008-07-31 21:03:41 +0200734static int snd_wss_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 unsigned long flags;
737 unsigned int ticks;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200738 struct snd_wss *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_lock_irqsave(&chip->reg_lock, flags);
740 ticks = timer->sticks;
741 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
742 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
743 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200744 chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
745 snd_wss_out(chip, CS4231_TIMER_HIGH,
746 chip->image[CS4231_TIMER_HIGH]);
747 chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
748 snd_wss_out(chip, CS4231_TIMER_LOW,
749 chip->image[CS4231_TIMER_LOW]);
750 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
751 chip->image[CS4231_ALT_FEATURE_1] |
752 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754 spin_unlock_irqrestore(&chip->reg_lock, flags);
755 return 0;
756}
757
Krzysztof Helt7779f752008-07-31 21:03:41 +0200758static int snd_wss_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 unsigned long flags;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200761 struct snd_wss *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200763 chip->image[CS4231_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
764 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
765 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 spin_unlock_irqrestore(&chip->reg_lock, flags);
767 return 0;
768}
769
Krzysztof Helt7779f752008-07-31 21:03:41 +0200770static void snd_wss_init(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 unsigned long flags;
773
Krzysztof Helt7779f752008-07-31 21:03:41 +0200774 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776#ifdef SNDRV_DEBUG_MCE
777 snd_printk("init: (1)\n");
778#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200779 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200781 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
782 CS4231_PLAYBACK_PIO |
783 CS4231_RECORD_ENABLE |
784 CS4231_RECORD_PIO |
785 CS4231_CALIB_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200787 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200789 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791#ifdef SNDRV_DEBUG_MCE
792 snd_printk("init: (2)\n");
793#endif
794
Krzysztof Helt7779f752008-07-31 21:03:41 +0200795 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200797 snd_wss_out(chip,
798 CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200800 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802#ifdef SNDRV_DEBUG_MCE
Krzysztof Helt7779f752008-07-31 21:03:41 +0200803 snd_printk("init: (3) - afei = 0x%x\n",
804 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805#endif
806
807 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200808 snd_wss_out(chip, CS4231_ALT_FEATURE_2,
809 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 spin_unlock_irqrestore(&chip->reg_lock, flags);
811
Krzysztof Helt7779f752008-07-31 21:03:41 +0200812 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200814 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
815 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200817 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819#ifdef SNDRV_DEBUG_MCE
820 snd_printk("init: (4)\n");
821#endif
822
Krzysztof Helt7779f752008-07-31 21:03:41 +0200823 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200825 if (!(chip->hardware & WSS_HW_AD1848_MASK))
826 snd_wss_out(chip, CS4231_REC_FORMAT,
827 chip->image[CS4231_REC_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200829 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831#ifdef SNDRV_DEBUG_MCE
832 snd_printk("init: (5)\n");
833#endif
834}
835
Krzysztof Helt7779f752008-07-31 21:03:41 +0200836static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 unsigned long flags;
839
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100840 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if ((chip->mode & mode) ||
Krzysztof Helt7779f752008-07-31 21:03:41 +0200842 ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100843 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EAGAIN;
845 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200846 if (chip->mode & WSS_MODE_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 chip->mode |= mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100848 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
850 }
851 /* ok. now enable and ack CODEC IRQ */
852 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200853 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
854 snd_wss_out(chip, CS4231_IRQ_STATUS,
855 CS4231_PLAYBACK_IRQ |
856 CS4231_RECORD_IRQ |
857 CS4231_TIMER_IRQ);
858 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
859 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200860 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
861 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200863 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200864 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
865 snd_wss_out(chip, CS4231_IRQ_STATUS,
866 CS4231_PLAYBACK_IRQ |
867 CS4231_RECORD_IRQ |
868 CS4231_TIMER_IRQ);
869 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 spin_unlock_irqrestore(&chip->reg_lock, flags);
872
873 chip->mode = mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100874 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876}
877
Krzysztof Helt7779f752008-07-31 21:03:41 +0200878static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 unsigned long flags;
881
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100882 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 chip->mode &= ~mode;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200884 if (chip->mode & WSS_MODE_OPEN) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100885 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return;
887 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200888 snd_wss_calibrate_mute(chip, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* disable IRQ */
891 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200892 if (!(chip->hardware & WSS_HW_AD1848_MASK))
893 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200894 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
895 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200897 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* now disable record & playback */
900
901 if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
902 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
903 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200904 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 spin_lock_irqsave(&chip->reg_lock, flags);
906 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
907 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200908 snd_wss_out(chip, CS4231_IFACE_CTRL,
909 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200911 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_lock_irqsave(&chip->reg_lock, flags);
913 }
914
915 /* clear IRQ again */
Krzysztof Heltead893c2008-07-31 21:09:32 +0200916 if (!(chip->hardware & WSS_HW_AD1848_MASK))
917 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200918 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
919 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 spin_unlock_irqrestore(&chip->reg_lock, flags);
921
Krzysztof Helt7779f752008-07-31 21:03:41 +0200922 snd_wss_calibrate_mute(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 chip->mode = 0;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100925 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
927
928/*
929 * timer open/close
930 */
931
Krzysztof Helt7779f752008-07-31 21:03:41 +0200932static int snd_wss_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200934 struct snd_wss *chip = snd_timer_chip(timer);
935 snd_wss_open(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return 0;
937}
938
Krzysztof Helt7779f752008-07-31 21:03:41 +0200939static int snd_wss_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200941 struct snd_wss *chip = snd_timer_chip(timer);
942 snd_wss_close(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
944}
945
Krzysztof Helt7779f752008-07-31 21:03:41 +0200946static struct snd_timer_hardware snd_wss_timer_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
948 .flags = SNDRV_TIMER_HW_AUTO,
949 .resolution = 9945,
950 .ticks = 65535,
Krzysztof Helt7779f752008-07-31 21:03:41 +0200951 .open = snd_wss_timer_open,
952 .close = snd_wss_timer_close,
953 .c_resolution = snd_wss_timer_resolution,
954 .start = snd_wss_timer_start,
955 .stop = snd_wss_timer_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956};
957
958/*
959 * ok.. exported functions..
960 */
961
Krzysztof Helt7779f752008-07-31 21:03:41 +0200962static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100963 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200965 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned char new_pdfr;
967 int err;
968
969 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
970 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200971 new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
972 params_channels(hw_params)) |
973 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 chip->set_playback_format(chip, hw_params, new_pdfr);
975 return 0;
976}
977
Krzysztof Helt7779f752008-07-31 21:03:41 +0200978static int snd_wss_playback_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
980 return snd_pcm_lib_free_pages(substream);
981}
982
Krzysztof Helt7779f752008-07-31 21:03:41 +0200983static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200985 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100986 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned long flags;
988 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
989 unsigned int count = snd_pcm_lib_period_bytes(substream);
990
991 spin_lock_irqsave(&chip->reg_lock, flags);
992 chip->p_dma_size = size;
993 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
994 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200995 count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
996 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
997 snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 spin_unlock_irqrestore(&chip->reg_lock, flags);
999#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001000 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#endif
1002 return 0;
1003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Krzysztof Helt7779f752008-07-31 21:03:41 +02001005static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001006 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001008 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned char new_cdfr;
1010 int err;
1011
1012 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
1013 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001014 new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
1015 params_channels(hw_params)) |
1016 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 chip->set_capture_format(chip, hw_params, new_cdfr);
1018 return 0;
1019}
1020
Krzysztof Helt7779f752008-07-31 21:03:41 +02001021static int snd_wss_capture_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
1023 return snd_pcm_lib_free_pages(substream);
1024}
1025
Krzysztof Helt7779f752008-07-31 21:03:41 +02001026static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001028 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001029 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unsigned long flags;
1031 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1032 unsigned int count = snd_pcm_lib_period_bytes(substream);
1033
1034 spin_lock_irqsave(&chip->reg_lock, flags);
1035 chip->c_dma_size = size;
1036 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
1037 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
Krzysztof Heltead893c2008-07-31 21:09:32 +02001038 if (chip->hardware & WSS_HW_AD1848_MASK)
1039 count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT],
1040 count);
1041 else
1042 count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT],
1043 count);
1044 count--;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001045 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
1046 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
1047 snd_wss_out(chip, CS4231_PLY_UPR_CNT,
1048 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001050 snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
1051 snd_wss_out(chip, CS4231_REC_UPR_CNT,
1052 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 spin_unlock_irqrestore(&chip->reg_lock, flags);
1055 return 0;
1056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Krzysztof Helt7779f752008-07-31 21:03:41 +02001058void snd_wss_overrange(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 unsigned long flags;
1061 unsigned char res;
1062
1063 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001064 res = snd_wss_in(chip, CS4231_TEST_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_unlock_irqrestore(&chip->reg_lock, flags);
1066 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
1067 chip->capture_substream->runtime->overrange++;
1068}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001069EXPORT_SYMBOL(snd_wss_overrange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Krzysztof Helt7779f752008-07-31 21:03:41 +02001071irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001073 struct snd_wss *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 unsigned char status;
1075
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001076 if (chip->hardware & WSS_HW_AD1848_MASK)
1077 /* pretend it was the only possible irq for AD1848 */
1078 status = CS4231_PLAYBACK_IRQ;
1079 else
1080 status = snd_wss_in(chip, CS4231_IRQ_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (status & CS4231_TIMER_IRQ) {
1082 if (chip->timer)
1083 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001084 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001085 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (status & CS4231_PLAYBACK_IRQ) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001087 if (chip->mode & WSS_MODE_PLAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (chip->playback_substream)
1089 snd_pcm_period_elapsed(chip->playback_substream);
1090 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001091 if (chip->mode & WSS_MODE_RECORD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001093 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 snd_pcm_period_elapsed(chip->capture_substream);
1095 }
1096 }
1097 }
1098 } else {
1099 if (status & CS4231_PLAYBACK_IRQ) {
1100 if (chip->playback_substream)
1101 snd_pcm_period_elapsed(chip->playback_substream);
1102 }
1103 if (status & CS4231_RECORD_IRQ) {
1104 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001105 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 snd_pcm_period_elapsed(chip->capture_substream);
1107 }
1108 }
1109 }
1110
1111 spin_lock(&chip->reg_lock);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001112 status = ~CS4231_ALL_IRQS | ~status;
1113 if (chip->hardware & WSS_HW_AD1848_MASK)
1114 wss_outb(chip, CS4231P(STATUS), 0);
1115 else
1116 snd_wss_outm(chip, CS4231_IRQ_STATUS, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 spin_unlock(&chip->reg_lock);
1118 return IRQ_HANDLED;
1119}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001120EXPORT_SYMBOL(snd_wss_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Krzysztof Helt7779f752008-07-31 21:03:41 +02001122static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001124 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 size_t ptr;
1126
1127 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1128 return 0;
1129 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1130 return bytes_to_frames(substream->runtime, ptr);
1131}
1132
Krzysztof Helt7779f752008-07-31 21:03:41 +02001133static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001135 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 size_t ptr;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1139 return 0;
1140 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1141 return bytes_to_frames(substream->runtime, ptr);
1142}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144/*
1145
1146 */
1147
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001148static int snd_ad1848_probe(struct snd_wss *chip)
1149{
1150 unsigned long flags;
1151 int i, id, rev, ad1847;
1152
1153 id = 0;
1154 ad1847 = 0;
1155 for (i = 0; i < 1000; i++) {
1156 mb();
1157 if (inb(chip->port + CS4231P(REGSEL)) & CS4231_INIT)
1158 msleep(1);
1159 else {
1160 spin_lock_irqsave(&chip->reg_lock, flags);
1161 snd_wss_out(chip, CS4231_MISC_INFO, 0x00);
1162 snd_wss_out(chip, CS4231_LEFT_INPUT, 0xaa);
1163 snd_wss_out(chip, CS4231_RIGHT_INPUT, 0x45);
1164 rev = snd_wss_in(chip, CS4231_RIGHT_INPUT);
1165 if (rev == 0x65) {
1166 spin_unlock_irqrestore(&chip->reg_lock, flags);
1167 id = 1;
1168 ad1847 = 1;
1169 break;
1170 }
1171 if (snd_wss_in(chip, CS4231_LEFT_INPUT) == 0xaa &&
1172 rev == 0x45) {
1173 spin_unlock_irqrestore(&chip->reg_lock, flags);
1174 id = 1;
1175 break;
1176 }
1177 spin_unlock_irqrestore(&chip->reg_lock, flags);
1178 }
1179 }
1180 if (id != 1)
1181 return -ENODEV; /* no valid device found */
1182 id = 0;
1183 if (chip->hardware == WSS_HW_DETECT)
1184 id = ad1847 ? WSS_HW_AD1847 : WSS_HW_AD1848;
1185
1186 spin_lock_irqsave(&chip->reg_lock, flags);
1187 inb(chip->port + CS4231P(STATUS)); /* clear any pendings IRQ */
1188 outb(0, chip->port + CS4231P(STATUS));
1189 mb();
1190 if (id == WSS_HW_AD1848) {
1191 /* check if there are more than 16 registers */
1192 rev = snd_wss_in(chip, CS4231_MISC_INFO);
1193 snd_wss_out(chip, CS4231_MISC_INFO, 0x40);
1194 for (i = 0; i < 16; ++i) {
1195 if (snd_wss_in(chip, i) != snd_wss_in(chip, i + 16)) {
1196 id = WSS_HW_CMI8330;
1197 break;
1198 }
1199 }
1200 snd_wss_out(chip, CS4231_MISC_INFO, 0x00);
1201 if (id != WSS_HW_CMI8330 && (rev & 0x80))
1202 id = WSS_HW_CS4248;
1203 if (id == WSS_HW_CMI8330 && (rev & 0x0f) != 0x0a)
1204 id = 0;
1205 }
1206 if (id == WSS_HW_CMI8330) {
1207 /* verify it is not CS4231 by changing the version register */
1208 /* on CMI8330 it is volume control register and can be set 0 */
1209 snd_wss_out(chip, CS4231_MISC_INFO, CS4231_MODE2);
1210 snd_wss_dout(chip, CS4231_VERSION, 0x00);
1211 rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
1212 if (rev)
1213 id = 0;
1214 snd_wss_out(chip, CS4231_MISC_INFO, 0);
1215 }
1216 if (id)
1217 chip->hardware = id;
1218
1219 spin_unlock_irqrestore(&chip->reg_lock, flags);
1220 return 0; /* all things are ok.. */
1221}
1222
Krzysztof Helt7779f752008-07-31 21:03:41 +02001223static int snd_wss_probe(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 unsigned long flags;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001226 int i, id, rev, regnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 unsigned char *ptr;
1228 unsigned int hw;
1229
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001230 id = snd_ad1848_probe(chip);
1231 if (id < 0)
1232 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Krzysztof Helt7779f752008-07-31 21:03:41 +02001234 hw = chip->hardware;
1235 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001236 for (i = 0; i < 50; i++) {
1237 mb();
1238 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
1239 msleep(2);
1240 else {
1241 spin_lock_irqsave(&chip->reg_lock, flags);
1242 snd_wss_out(chip, CS4231_MISC_INFO,
1243 CS4231_MODE2);
1244 id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
1245 spin_unlock_irqrestore(&chip->reg_lock, flags);
1246 if (id == 0x0a)
1247 break; /* this is valid value */
1248 }
1249 }
1250 snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
1251 if (id != 0x0a)
1252 return -ENODEV; /* no valid device found */
1253
Krzysztof Helt7779f752008-07-31 21:03:41 +02001254 rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
1256 if (rev == 0x80) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001257 unsigned char tmp = snd_wss_in(chip, 23);
1258 snd_wss_out(chip, 23, ~tmp);
1259 if (snd_wss_in(chip, 23) != tmp)
1260 chip->hardware = WSS_HW_AD1845;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001262 chip->hardware = WSS_HW_CS4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 } else if (rev == 0xa0) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001264 chip->hardware = WSS_HW_CS4231A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 } else if (rev == 0xa2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001266 chip->hardware = WSS_HW_CS4232;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 } else if (rev == 0xb2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001268 chip->hardware = WSS_HW_CS4232A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 } else if (rev == 0x83) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001270 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else if (rev == 0x03) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001272 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 } else {
1274 snd_printk("unknown CS chip with version 0x%x\n", rev);
1275 return -ENODEV; /* unknown CS4231 chip? */
1276 }
1277 }
1278 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001279 wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
1280 wss_outb(chip, CS4231P(STATUS), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 mb();
1282 spin_unlock_irqrestore(&chip->reg_lock, flags);
1283
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001284 if (!(chip->hardware & WSS_HW_AD1848_MASK))
1285 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001287 case WSS_HW_INTERWAVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
1289 break;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001290 case WSS_HW_CS4235:
1291 case WSS_HW_CS4236B:
1292 case WSS_HW_CS4237B:
1293 case WSS_HW_CS4238B:
1294 case WSS_HW_CS4239:
1295 if (hw == WSS_HW_DETECT3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
1297 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001298 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 break;
1300 }
1301
1302 chip->image[CS4231_IFACE_CTRL] =
1303 (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
1304 (chip->single_dma ? CS4231_SINGLE_DMA : 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001305 if (chip->hardware != WSS_HW_OPTI93X) {
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001306 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1307 chip->image[CS4231_ALT_FEATURE_2] =
Krzysztof Helt7779f752008-07-31 21:03:41 +02001308 chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ptr = (unsigned char *) &chip->image;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001311 regnum = (chip->hardware & WSS_HW_AD1848_MASK) ? 16 : 32;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001312 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001314 for (i = 0; i < regnum; i++) /* ok.. fill all registers */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001315 snd_wss_out(chip, i, *ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001317 snd_wss_mce_up(chip);
1318 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 mdelay(2);
1321
1322 /* ok.. try check hardware version for CS4236+ chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001323 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
1324 if (chip->hardware == WSS_HW_CS4236B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
1326 snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
1327 id = snd_cs4236_ext_in(chip, CS4236_VERSION);
1328 snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
1329 snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
1330 if ((id & 0x1f) == 0x1d) { /* CS4235 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001331 chip->hardware = WSS_HW_CS4235;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 switch (id >> 5) {
1333 case 4:
1334 case 5:
1335 case 6:
1336 break;
1337 default:
1338 snd_printk("unknown CS4235 chip (enhanced version = 0x%x)\n", id);
1339 }
1340 } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
1341 switch (id >> 5) {
1342 case 4:
1343 case 5:
1344 case 6:
1345 case 7:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001346 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
1348 default:
1349 snd_printk("unknown CS4236 chip (enhanced version = 0x%x)\n", id);
1350 }
1351 } else if ((id & 0x1f) == 0x08) { /* CS4237B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001352 chip->hardware = WSS_HW_CS4237B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 switch (id >> 5) {
1354 case 4:
1355 case 5:
1356 case 6:
1357 case 7:
1358 break;
1359 default:
1360 snd_printk("unknown CS4237B chip (enhanced version = 0x%x)\n", id);
1361 }
1362 } else if ((id & 0x1f) == 0x09) { /* CS4238B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001363 chip->hardware = WSS_HW_CS4238B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 switch (id >> 5) {
1365 case 5:
1366 case 6:
1367 case 7:
1368 break;
1369 default:
1370 snd_printk("unknown CS4238B chip (enhanced version = 0x%x)\n", id);
1371 }
1372 } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001373 chip->hardware = WSS_HW_CS4239;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 switch (id >> 5) {
1375 case 4:
1376 case 5:
1377 case 6:
1378 break;
1379 default:
1380 snd_printk("unknown CS4239 chip (enhanced version = 0x%x)\n", id);
1381 }
1382 } else {
1383 snd_printk("unknown CS4236/CS423xB chip (enhanced version = 0x%x)\n", id);
1384 }
1385 }
1386 }
1387 return 0; /* all things are ok.. */
1388}
1389
1390/*
1391
1392 */
1393
Krzysztof Helt7779f752008-07-31 21:03:41 +02001394static struct snd_pcm_hardware snd_wss_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1397 SNDRV_PCM_INFO_MMAP_VALID |
1398 SNDRV_PCM_INFO_RESUME |
1399 SNDRV_PCM_INFO_SYNC_START),
1400 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1401 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1402 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1403 .rate_min = 5510,
1404 .rate_max = 48000,
1405 .channels_min = 1,
1406 .channels_max = 2,
1407 .buffer_bytes_max = (128*1024),
1408 .period_bytes_min = 64,
1409 .period_bytes_max = (128*1024),
1410 .periods_min = 1,
1411 .periods_max = 1024,
1412 .fifo_size = 0,
1413};
1414
Krzysztof Helt7779f752008-07-31 21:03:41 +02001415static struct snd_pcm_hardware snd_wss_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
1417 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1418 SNDRV_PCM_INFO_MMAP_VALID |
1419 SNDRV_PCM_INFO_RESUME |
1420 SNDRV_PCM_INFO_SYNC_START),
1421 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1422 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1423 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1424 .rate_min = 5510,
1425 .rate_max = 48000,
1426 .channels_min = 1,
1427 .channels_max = 2,
1428 .buffer_bytes_max = (128*1024),
1429 .period_bytes_min = 64,
1430 .period_bytes_max = (128*1024),
1431 .periods_min = 1,
1432 .periods_max = 1024,
1433 .fifo_size = 0,
1434};
1435
1436/*
1437
1438 */
1439
Krzysztof Helt7779f752008-07-31 21:03:41 +02001440static int snd_wss_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001442 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001443 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 int err;
1445
Krzysztof Helt7779f752008-07-31 21:03:41 +02001446 runtime->hw = snd_wss_playback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Krzysztof Heltead893c2008-07-31 21:09:32 +02001448 /* hardware limitation of older chipsets */
1449 if (chip->hardware & WSS_HW_AD1848_MASK)
1450 runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
1451 SNDRV_PCM_FMTBIT_S16_BE);
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 /* hardware bug in InterWave chipset */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001454 if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001455 runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
1456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001458 if (chip->hardware == WSS_HW_CS4235 ||
1459 chip->hardware == WSS_HW_CS4239)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1463 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
1464
1465 if (chip->claim_dma) {
1466 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1)) < 0)
1467 return err;
1468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Krzysztof Helt7779f752008-07-31 21:03:41 +02001470 err = snd_wss_open(chip, WSS_MODE_PLAY);
1471 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (chip->release_dma)
1473 chip->release_dma(chip, chip->dma_private_data, chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1475 return err;
1476 }
1477 chip->playback_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 snd_pcm_set_sync(substream);
1479 chip->rate_constraint(runtime);
1480 return 0;
1481}
1482
Krzysztof Helt7779f752008-07-31 21:03:41 +02001483static int snd_wss_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001485 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001486 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 int err;
1488
Krzysztof Helt7779f752008-07-31 21:03:41 +02001489 runtime->hw = snd_wss_capture;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Krzysztof Heltead893c2008-07-31 21:09:32 +02001491 /* hardware limitation of older chipsets */
1492 if (chip->hardware & WSS_HW_AD1848_MASK)
1493 runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
1494 SNDRV_PCM_FMTBIT_S16_BE);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001497 if (chip->hardware == WSS_HW_CS4235 ||
Krzysztof Helt31eca302008-07-31 21:11:46 +02001498 chip->hardware == WSS_HW_CS4239 ||
1499 chip->hardware == WSS_HW_OPTI93X)
1500 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 |
1501 SNDRV_PCM_FMTBIT_S16_LE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1504 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
1505
1506 if (chip->claim_dma) {
1507 if ((err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2)) < 0)
1508 return err;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Krzysztof Helt7779f752008-07-31 21:03:41 +02001511 err = snd_wss_open(chip, WSS_MODE_RECORD);
1512 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 if (chip->release_dma)
1514 chip->release_dma(chip, chip->dma_private_data, chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 snd_free_pages(runtime->dma_area, runtime->dma_bytes);
1516 return err;
1517 }
1518 chip->capture_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 snd_pcm_set_sync(substream);
1520 chip->rate_constraint(runtime);
1521 return 0;
1522}
1523
Krzysztof Helt7779f752008-07-31 21:03:41 +02001524static int snd_wss_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001526 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528 chip->playback_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001529 snd_wss_close(chip, WSS_MODE_PLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return 0;
1531}
1532
Krzysztof Helt7779f752008-07-31 21:03:41 +02001533static int snd_wss_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001535 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 chip->capture_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001538 snd_wss_close(chip, WSS_MODE_RECORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 0;
1540}
1541
Krzysztof Heltead893c2008-07-31 21:09:32 +02001542static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
1543{
1544 int tmp;
1545
1546 if (!chip->thinkpad_flag)
1547 return;
1548
1549 outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
1550 tmp = inb(AD1848_THINKPAD_CTL_PORT2);
1551
1552 if (on)
1553 /* turn it on */
1554 tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
1555 else
1556 /* turn it off */
1557 tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
1558
1559 outb(tmp, AD1848_THINKPAD_CTL_PORT2);
1560}
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#ifdef CONFIG_PM
1563
1564/* lowlevel suspend callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001565static void snd_wss_suspend(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 int reg;
1568 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001569
Takashi Iwai7bb35e202005-11-17 17:00:17 +01001570 snd_pcm_suspend_all(chip->pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 spin_lock_irqsave(&chip->reg_lock, flags);
1572 for (reg = 0; reg < 32; reg++)
Krzysztof Helt7779f752008-07-31 21:03:41 +02001573 chip->image[reg] = snd_wss_in(chip, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +02001575 if (chip->thinkpad_flag)
1576 snd_wss_thinkpad_twiddle(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
1579/* lowlevel resume callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001580static void snd_wss_resume(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 int reg;
1583 unsigned long flags;
Takashi Iwaia2c855b2005-11-18 18:52:39 +01001584 /* int timeout; */
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001585
Krzysztof Heltead893c2008-07-31 21:09:32 +02001586 if (chip->thinkpad_flag)
1587 snd_wss_thinkpad_twiddle(chip, 1);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001588 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 spin_lock_irqsave(&chip->reg_lock, flags);
1590 for (reg = 0; reg < 32; reg++) {
1591 switch (reg) {
1592 case CS4231_VERSION:
1593 break;
1594 default:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001595 snd_wss_out(chip, reg, chip->image[reg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 }
1598 }
1599 spin_unlock_irqrestore(&chip->reg_lock, flags);
Takashi Iwaifa55f832005-11-17 17:48:30 +01001600#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +02001601 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602#else
1603 /* The following is a workaround to avoid freeze after resume on TP600E.
Krzysztof Helt7779f752008-07-31 21:03:41 +02001604 This is the first half of copy of snd_wss_mce_down(), but doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 include rescheduling. -- iwai
1606 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001607 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 spin_lock_irqsave(&chip->reg_lock, flags);
1609 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001610 timeout = wss_inb(chip, CS4231P(REGSEL));
1611 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 spin_unlock_irqrestore(&chip->reg_lock, flags);
1613 if (timeout == 0x80)
1614 snd_printk("down [0x%lx]: serious init problem - codec still busy\n", chip->port);
1615 if ((timeout & CS4231_MCE) == 0 ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001616 !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return;
1618 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001619 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620#endif
1621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622#endif /* CONFIG_PM */
1623
Krzysztof Helt7779f752008-07-31 21:03:41 +02001624static int snd_wss_free(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001626 release_and_free_resource(chip->res_port);
1627 release_and_free_resource(chip->res_cport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (chip->irq >= 0) {
1629 disable_irq(chip->irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001630 if (!(chip->hwshare & WSS_HWSHARE_IRQ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 free_irq(chip->irq, (void *) chip);
1632 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001633 if (!(chip->hwshare & WSS_HWSHARE_DMA1) && chip->dma1 >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 snd_dma_disable(chip->dma1);
1635 free_dma(chip->dma1);
1636 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001637 if (!(chip->hwshare & WSS_HWSHARE_DMA2) &&
1638 chip->dma2 >= 0 && chip->dma2 != chip->dma1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 snd_dma_disable(chip->dma2);
1640 free_dma(chip->dma2);
1641 }
1642 if (chip->timer)
1643 snd_device_free(chip->card, chip->timer);
1644 kfree(chip);
1645 return 0;
1646}
1647
Krzysztof Helt7779f752008-07-31 21:03:41 +02001648static int snd_wss_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001650 struct snd_wss *chip = device->device_data;
1651 return snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Krzysztof Helt7779f752008-07-31 21:03:41 +02001654const char *snd_wss_chip_id(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001657 case WSS_HW_CS4231:
1658 return "CS4231";
1659 case WSS_HW_CS4231A:
1660 return "CS4231A";
1661 case WSS_HW_CS4232:
1662 return "CS4232";
1663 case WSS_HW_CS4232A:
1664 return "CS4232A";
1665 case WSS_HW_CS4235:
1666 return "CS4235";
1667 case WSS_HW_CS4236:
1668 return "CS4236";
1669 case WSS_HW_CS4236B:
1670 return "CS4236B";
1671 case WSS_HW_CS4237B:
1672 return "CS4237B";
1673 case WSS_HW_CS4238B:
1674 return "CS4238B";
1675 case WSS_HW_CS4239:
1676 return "CS4239";
1677 case WSS_HW_INTERWAVE:
1678 return "AMD InterWave";
1679 case WSS_HW_OPL3SA2:
1680 return chip->card->shortname;
1681 case WSS_HW_AD1845:
1682 return "AD1845";
1683 case WSS_HW_OPTI93X:
1684 return "OPTi 93x";
Krzysztof Heltead893c2008-07-31 21:09:32 +02001685 case WSS_HW_AD1847:
1686 return "AD1847";
1687 case WSS_HW_AD1848:
1688 return "AD1848";
1689 case WSS_HW_CS4248:
1690 return "CS4248";
1691 case WSS_HW_CMI8330:
1692 return "CMI8330/C3D";
Krzysztof Helt7779f752008-07-31 21:03:41 +02001693 default:
1694 return "???";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
1696}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001697EXPORT_SYMBOL(snd_wss_chip_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Krzysztof Helt7779f752008-07-31 21:03:41 +02001699static int snd_wss_new(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 unsigned short hardware,
1701 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001702 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001704 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001707 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (chip == NULL)
1709 return -ENOMEM;
1710 chip->hardware = hardware;
1711 chip->hwshare = hwshare;
1712
1713 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001714 mutex_init(&chip->mce_mutex);
1715 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 chip->card = card;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001717 chip->rate_constraint = snd_wss_xrate;
1718 chip->set_playback_format = snd_wss_playback_format;
1719 chip->set_capture_format = snd_wss_capture_format;
1720 if (chip->hardware == WSS_HW_OPTI93X)
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001721 memcpy(&chip->image, &snd_opti93x_original_image,
1722 sizeof(snd_opti93x_original_image));
1723 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001724 memcpy(&chip->image, &snd_wss_original_image,
1725 sizeof(snd_wss_original_image));
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001726 if (chip->hardware & WSS_HW_AD1848_MASK) {
1727 chip->image[CS4231_PIN_CTRL] = 0;
1728 chip->image[CS4231_TEST_INIT] = 0;
1729 }
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001730
Krzysztof Helt7779f752008-07-31 21:03:41 +02001731 *rchip = chip;
1732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Krzysztof Helt7779f752008-07-31 21:03:41 +02001735int snd_wss_create(struct snd_card *card,
1736 unsigned long port,
1737 unsigned long cport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 int irq, int dma1, int dma2,
1739 unsigned short hardware,
1740 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001741 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001743 static struct snd_device_ops ops = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001744 .dev_free = snd_wss_dev_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 };
Krzysztof Helt7779f752008-07-31 21:03:41 +02001746 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int err;
1748
Krzysztof Helt7779f752008-07-31 21:03:41 +02001749 err = snd_wss_new(card, hardware, hwshare, &chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (err < 0)
1751 return err;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 chip->irq = -1;
1754 chip->dma1 = -1;
1755 chip->dma2 = -1;
1756
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001757 chip->res_port = request_region(port, 4, "WSS");
Krzysztof Helt7779f752008-07-31 21:03:41 +02001758 if (!chip->res_port) {
1759 snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
1760 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 return -EBUSY;
1762 }
1763 chip->port = port;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001764 if ((long)cport >= 0) {
1765 chip->res_cport = request_region(cport, 8, "CS4232 Control");
1766 if (!chip->res_cport) {
1767 snd_printk(KERN_ERR
1768 "wss: can't grab control port 0x%lx\n", cport);
1769 snd_wss_free(chip);
1770 return -ENODEV;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 chip->cport = cport;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001774 if (!(hwshare & WSS_HWSHARE_IRQ))
1775 if (request_irq(irq, snd_wss_interrupt, IRQF_DISABLED,
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001776 "WSS", (void *) chip)) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001777 snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
1778 snd_wss_free(chip);
1779 return -EBUSY;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 chip->irq = irq;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001782 if (!(hwshare & WSS_HWSHARE_DMA1) && request_dma(dma1, "WSS - 1")) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001783 snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
1784 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -EBUSY;
1786 }
1787 chip->dma1 = dma1;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001788 if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 &&
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001789 dma2 >= 0 && request_dma(dma2, "WSS - 2")) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001790 snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
1791 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return -EBUSY;
1793 }
1794 if (dma1 == dma2 || dma2 < 0) {
1795 chip->single_dma = 1;
1796 chip->dma2 = chip->dma1;
1797 } else
1798 chip->dma2 = dma2;
1799
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001800 if (hardware == WSS_HW_THINKPAD) {
1801 chip->thinkpad_flag = 1;
1802 chip->hardware = WSS_HW_DETECT; /* reset */
1803 snd_wss_thinkpad_twiddle(chip, 1);
1804 }
1805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 /* global setup */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001807 if (snd_wss_probe(chip) < 0) {
1808 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -ENODEV;
1810 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001811 snd_wss_init(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Takashi Iwaia9824c82005-11-17 17:51:00 +01001813#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001814 if (chip->hardware & WSS_HW_CS4232_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (chip->res_cport == NULL)
1816 snd_printk("CS4232 control port features are not accessible\n");
1817 }
Takashi Iwaia9824c82005-11-17 17:51:00 +01001818#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 /* Register device */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001821 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
1822 if (err < 0) {
1823 snd_wss_free(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return err;
1825 }
1826
1827#ifdef CONFIG_PM
1828 /* Power Management */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001829 chip->suspend = snd_wss_suspend;
1830 chip->resume = snd_wss_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831#endif
1832
1833 *rchip = chip;
1834 return 0;
1835}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001836EXPORT_SYMBOL(snd_wss_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Krzysztof Helt7779f752008-07-31 21:03:41 +02001838static struct snd_pcm_ops snd_wss_playback_ops = {
1839 .open = snd_wss_playback_open,
1840 .close = snd_wss_playback_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 .ioctl = snd_pcm_lib_ioctl,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001842 .hw_params = snd_wss_playback_hw_params,
1843 .hw_free = snd_wss_playback_hw_free,
1844 .prepare = snd_wss_playback_prepare,
1845 .trigger = snd_wss_trigger,
1846 .pointer = snd_wss_playback_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847};
1848
Krzysztof Helt7779f752008-07-31 21:03:41 +02001849static struct snd_pcm_ops snd_wss_capture_ops = {
1850 .open = snd_wss_capture_open,
1851 .close = snd_wss_capture_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 .ioctl = snd_pcm_lib_ioctl,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001853 .hw_params = snd_wss_capture_hw_params,
1854 .hw_free = snd_wss_capture_hw_free,
1855 .prepare = snd_wss_capture_prepare,
1856 .trigger = snd_wss_trigger,
1857 .pointer = snd_wss_capture_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858};
1859
Krzysztof Helt7779f752008-07-31 21:03:41 +02001860int snd_wss_pcm(struct snd_wss *chip, int device, struct snd_pcm **rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001862 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 int err;
1864
Krzysztof Heltead893c2008-07-31 21:09:32 +02001865 err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
1866 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return err;
1868
1869 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001870 mutex_init(&chip->mce_mutex);
1871 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Krzysztof Helt7779f752008-07-31 21:03:41 +02001873 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
1874 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /* global setup */
1877 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 pcm->info_flags = 0;
1879 if (chip->single_dma)
1880 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001881 if (chip->hardware != WSS_HW_INTERWAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001883 strcpy(pcm->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1886 snd_dma_isa_data(),
1887 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 chip->pcm = pcm;
1890 if (rpcm)
1891 *rpcm = pcm;
1892 return 0;
1893}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001894EXPORT_SYMBOL(snd_wss_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Krzysztof Helt7779f752008-07-31 21:03:41 +02001896static void snd_wss_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001898 struct snd_wss *chip = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 chip->timer = NULL;
1900}
1901
Krzysztof Helt7779f752008-07-31 21:03:41 +02001902int snd_wss_timer(struct snd_wss *chip, int device, struct snd_timer **rtimer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001904 struct snd_timer *timer;
1905 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int err;
1907
1908 /* Timer initialization */
1909 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1910 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1911 tid.card = chip->card->number;
1912 tid.device = device;
1913 tid.subdevice = 0;
1914 if ((err = snd_timer_new(chip->card, "CS4231", &tid, &timer)) < 0)
1915 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001916 strcpy(timer->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 timer->private_data = chip;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001918 timer->private_free = snd_wss_timer_free;
1919 timer->hw = snd_wss_timer_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 chip->timer = timer;
1921 if (rtimer)
1922 *rtimer = timer;
1923 return 0;
1924}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001925EXPORT_SYMBOL(snd_wss_timer);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927/*
1928 * MIXER part
1929 */
1930
Krzysztof Helt7779f752008-07-31 21:03:41 +02001931static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
1932 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
1934 static char *texts[4] = {
1935 "Line", "Aux", "Mic", "Mix"
1936 };
1937 static char *opl3sa_texts[4] = {
1938 "Line", "CD", "Mic", "Mix"
1939 };
1940 static char *gusmax_texts[4] = {
1941 "Line", "Synth", "Mic", "Mix"
1942 };
1943 char **ptexts = texts;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001944 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 snd_assert(chip->card != NULL, return -EINVAL);
1947 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1948 uinfo->count = 2;
1949 uinfo->value.enumerated.items = 4;
1950 if (uinfo->value.enumerated.item > 3)
1951 uinfo->value.enumerated.item = 3;
1952 if (!strcmp(chip->card->driver, "GUS MAX"))
1953 ptexts = gusmax_texts;
1954 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001955 case WSS_HW_INTERWAVE:
1956 ptexts = gusmax_texts;
1957 break;
1958 case WSS_HW_OPL3SA2:
1959 ptexts = opl3sa_texts;
1960 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962 strcpy(uinfo->value.enumerated.name, ptexts[uinfo->value.enumerated.item]);
1963 return 0;
1964}
1965
Krzysztof Helt7779f752008-07-31 21:03:41 +02001966static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
1967 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001969 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 spin_lock_irqsave(&chip->reg_lock, flags);
1973 ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1974 ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1975 spin_unlock_irqrestore(&chip->reg_lock, flags);
1976 return 0;
1977}
1978
Krzysztof Helt7779f752008-07-31 21:03:41 +02001979static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
1980 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001982 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 unsigned long flags;
1984 unsigned short left, right;
1985 int change;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (ucontrol->value.enumerated.item[0] > 3 ||
1988 ucontrol->value.enumerated.item[1] > 3)
1989 return -EINVAL;
1990 left = ucontrol->value.enumerated.item[0] << 6;
1991 right = ucontrol->value.enumerated.item[1] << 6;
1992 spin_lock_irqsave(&chip->reg_lock, flags);
1993 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1994 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1995 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001996 right != chip->image[CS4231_RIGHT_INPUT];
1997 snd_wss_out(chip, CS4231_LEFT_INPUT, left);
1998 snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 spin_unlock_irqrestore(&chip->reg_lock, flags);
2000 return change;
2001}
2002
Krzysztof Helt7779f752008-07-31 21:03:41 +02002003int snd_wss_info_single(struct snd_kcontrol *kcontrol,
2004 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 int mask = (kcontrol->private_value >> 16) & 0xff;
2007
2008 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2009 uinfo->count = 1;
2010 uinfo->value.integer.min = 0;
2011 uinfo->value.integer.max = mask;
2012 return 0;
2013}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002014EXPORT_SYMBOL(snd_wss_info_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Krzysztof Helt7779f752008-07-31 21:03:41 +02002016int snd_wss_get_single(struct snd_kcontrol *kcontrol,
2017 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002019 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 unsigned long flags;
2021 int reg = kcontrol->private_value & 0xff;
2022 int shift = (kcontrol->private_value >> 8) & 0xff;
2023 int mask = (kcontrol->private_value >> 16) & 0xff;
2024 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 spin_lock_irqsave(&chip->reg_lock, flags);
2027 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
2028 spin_unlock_irqrestore(&chip->reg_lock, flags);
2029 if (invert)
2030 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
2031 return 0;
2032}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002033EXPORT_SYMBOL(snd_wss_get_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Krzysztof Helt7779f752008-07-31 21:03:41 +02002035int snd_wss_put_single(struct snd_kcontrol *kcontrol,
2036 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002038 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 unsigned long flags;
2040 int reg = kcontrol->private_value & 0xff;
2041 int shift = (kcontrol->private_value >> 8) & 0xff;
2042 int mask = (kcontrol->private_value >> 16) & 0xff;
2043 int invert = (kcontrol->private_value >> 24) & 0xff;
2044 int change;
2045 unsigned short val;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 val = (ucontrol->value.integer.value[0] & mask);
2048 if (invert)
2049 val = mask - val;
2050 val <<= shift;
2051 spin_lock_irqsave(&chip->reg_lock, flags);
2052 val = (chip->image[reg] & ~(mask << shift)) | val;
2053 change = val != chip->image[reg];
Krzysztof Helt7779f752008-07-31 21:03:41 +02002054 snd_wss_out(chip, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 spin_unlock_irqrestore(&chip->reg_lock, flags);
2056 return change;
2057}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002058EXPORT_SYMBOL(snd_wss_put_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Krzysztof Helt7779f752008-07-31 21:03:41 +02002060int snd_wss_info_double(struct snd_kcontrol *kcontrol,
2061 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 int mask = (kcontrol->private_value >> 24) & 0xff;
2064
2065 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2066 uinfo->count = 2;
2067 uinfo->value.integer.min = 0;
2068 uinfo->value.integer.max = mask;
2069 return 0;
2070}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002071EXPORT_SYMBOL(snd_wss_info_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Krzysztof Helt7779f752008-07-31 21:03:41 +02002073int snd_wss_get_double(struct snd_kcontrol *kcontrol,
2074 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002076 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 unsigned long flags;
2078 int left_reg = kcontrol->private_value & 0xff;
2079 int right_reg = (kcontrol->private_value >> 8) & 0xff;
2080 int shift_left = (kcontrol->private_value >> 16) & 0x07;
2081 int shift_right = (kcontrol->private_value >> 19) & 0x07;
2082 int mask = (kcontrol->private_value >> 24) & 0xff;
2083 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 spin_lock_irqsave(&chip->reg_lock, flags);
2086 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
2087 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
2088 spin_unlock_irqrestore(&chip->reg_lock, flags);
2089 if (invert) {
2090 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
2091 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
2092 }
2093 return 0;
2094}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002095EXPORT_SYMBOL(snd_wss_get_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Krzysztof Helt7779f752008-07-31 21:03:41 +02002097int snd_wss_put_double(struct snd_kcontrol *kcontrol,
2098 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002100 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 unsigned long flags;
2102 int left_reg = kcontrol->private_value & 0xff;
2103 int right_reg = (kcontrol->private_value >> 8) & 0xff;
2104 int shift_left = (kcontrol->private_value >> 16) & 0x07;
2105 int shift_right = (kcontrol->private_value >> 19) & 0x07;
2106 int mask = (kcontrol->private_value >> 24) & 0xff;
2107 int invert = (kcontrol->private_value >> 22) & 1;
2108 int change;
2109 unsigned short val1, val2;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 val1 = ucontrol->value.integer.value[0] & mask;
2112 val2 = ucontrol->value.integer.value[1] & mask;
2113 if (invert) {
2114 val1 = mask - val1;
2115 val2 = mask - val2;
2116 }
2117 val1 <<= shift_left;
2118 val2 <<= shift_right;
2119 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002120 if (left_reg != right_reg) {
2121 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
2122 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
2123 change = val1 != chip->image[left_reg] ||
2124 val2 != chip->image[right_reg];
2125 snd_wss_out(chip, left_reg, val1);
2126 snd_wss_out(chip, right_reg, val2);
2127 } else {
2128 mask = (mask << shift_left) | (mask << shift_right);
2129 val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
2130 change = val1 != chip->image[left_reg];
2131 snd_wss_out(chip, left_reg, val1);
2132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 spin_unlock_irqrestore(&chip->reg_lock, flags);
2134 return change;
2135}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002136EXPORT_SYMBOL(snd_wss_put_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002138static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
2139static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
2140static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
2141
2142static struct snd_kcontrol_new snd_ad1848_controls[] = {
2143WSS_DOUBLE("PCM Playback Switch", 0, CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT,
2144 7, 7, 1, 1),
2145WSS_DOUBLE_TLV("PCM Playback Volume", 0,
2146 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
2147 db_scale_6bit),
2148WSS_DOUBLE("Aux Playback Switch", 0,
2149 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
2150WSS_DOUBLE_TLV("Aux Playback Volume", 0,
2151 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
2152 db_scale_5bit_12db_max),
2153WSS_DOUBLE("Aux Playback Switch", 1,
2154 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
2155WSS_DOUBLE_TLV("Aux Playback Volume", 1,
2156 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
2157 db_scale_5bit_12db_max),
2158WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
2159 0, 0, 15, 0, db_scale_rec_gain),
2160{
2161 .name = "Capture Source",
2162 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2163 .info = snd_wss_info_mux,
2164 .get = snd_wss_get_mux,
2165 .put = snd_wss_put_mux,
2166},
2167WSS_SINGLE("Loopback Capture Switch", 0, CS4231_LOOPBACK, 0, 1, 0),
2168WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 1, 63, 0,
2169 db_scale_6bit),
2170};
2171
Krzysztof Helt7779f752008-07-31 21:03:41 +02002172static struct snd_kcontrol_new snd_wss_controls[] = {
2173WSS_DOUBLE("PCM Playback Switch", 0,
2174 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
2175WSS_DOUBLE("PCM Playback Volume", 0,
2176 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1),
2177WSS_DOUBLE("Line Playback Switch", 0,
2178 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
2179WSS_DOUBLE("Line Playback Volume", 0,
2180 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1),
2181WSS_DOUBLE("Aux Playback Switch", 0,
2182 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
2183WSS_DOUBLE("Aux Playback Volume", 0,
2184 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1),
2185WSS_DOUBLE("Aux Playback Switch", 1,
2186 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
2187WSS_DOUBLE("Aux Playback Volume", 1,
2188 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1),
2189WSS_SINGLE("Mono Playback Switch", 0,
2190 CS4231_MONO_CTRL, 7, 1, 1),
2191WSS_SINGLE("Mono Playback Volume", 0,
2192 CS4231_MONO_CTRL, 0, 15, 1),
2193WSS_SINGLE("Mono Output Playback Switch", 0,
2194 CS4231_MONO_CTRL, 6, 1, 1),
2195WSS_SINGLE("Mono Output Playback Bypass", 0,
2196 CS4231_MONO_CTRL, 5, 1, 0),
2197WSS_DOUBLE("Capture Volume", 0,
2198 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2201 .name = "Capture Source",
Krzysztof Helt7779f752008-07-31 21:03:41 +02002202 .info = snd_wss_info_mux,
2203 .get = snd_wss_get_mux,
2204 .put = snd_wss_put_mux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205},
Krzysztof Helt7779f752008-07-31 21:03:41 +02002206WSS_DOUBLE("Mic Boost", 0,
2207 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
2208WSS_SINGLE("Loopback Capture Switch", 0,
2209 CS4231_LOOPBACK, 0, 1, 0),
2210WSS_SINGLE("Loopback Capture Volume", 0,
2211 CS4231_LOOPBACK, 2, 63, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212};
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002213
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002214static struct snd_kcontrol_new snd_opti93x_controls[] = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02002215WSS_DOUBLE("Master Playback Switch", 0,
2216 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
2217WSS_DOUBLE("Master Playback Volume", 0,
2218 OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1),
2219WSS_DOUBLE("PCM Playback Switch", 0,
2220 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
2221WSS_DOUBLE("PCM Playback Volume", 0,
2222 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
2223WSS_DOUBLE("FM Playback Switch", 0,
2224 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
2225WSS_DOUBLE("FM Playback Volume", 0,
2226 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
2227WSS_DOUBLE("Line Playback Switch", 0,
2228 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
2229WSS_DOUBLE("Line Playback Volume", 0,
2230 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
2231WSS_DOUBLE("Mic Playback Switch", 0,
2232 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
2233WSS_DOUBLE("Mic Playback Volume", 0,
2234 OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
2235WSS_DOUBLE("Mic Boost", 0,
2236 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
2237WSS_DOUBLE("CD Playback Switch", 0,
2238 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
2239WSS_DOUBLE("CD Playback Volume", 0,
2240 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
2241WSS_DOUBLE("Aux Playback Switch", 0,
2242 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
2243WSS_DOUBLE("Aux Playback Volume", 0,
2244 OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
2245WSS_DOUBLE("Capture Volume", 0,
2246 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 0, 0, 15, 0),
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002247{
2248 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2249 .name = "Capture Source",
Krzysztof Helt7779f752008-07-31 21:03:41 +02002250 .info = snd_wss_info_mux,
2251 .get = snd_wss_get_mux,
2252 .put = snd_wss_put_mux,
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002253}
2254};
2255
Krzysztof Helt7779f752008-07-31 21:03:41 +02002256int snd_wss_mixer(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01002258 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 unsigned int idx;
2260 int err;
2261
2262 snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL);
2263
2264 card = chip->card;
2265
2266 strcpy(card->mixername, chip->pcm->name);
2267
Krzysztof Helt7779f752008-07-31 21:03:41 +02002268 if (chip->hardware == WSS_HW_OPTI93X)
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002269 for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
2270 err = snd_ctl_add(card,
2271 snd_ctl_new1(&snd_opti93x_controls[idx],
2272 chip));
2273 if (err < 0)
2274 return err;
2275 }
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002276 else if (chip->hardware & WSS_HW_AD1848_MASK)
2277 for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++) {
2278 err = snd_ctl_add(card,
2279 snd_ctl_new1(&snd_ad1848_controls[idx],
2280 chip));
2281 if (err < 0)
2282 return err;
2283 }
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002284 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02002285 for (idx = 0; idx < ARRAY_SIZE(snd_wss_controls); idx++) {
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002286 err = snd_ctl_add(card,
Krzysztof Helt7779f752008-07-31 21:03:41 +02002287 snd_ctl_new1(&snd_wss_controls[idx],
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02002288 chip));
2289 if (err < 0)
2290 return err;
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002294EXPORT_SYMBOL(snd_wss_mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Krzysztof Heltead893c2008-07-31 21:09:32 +02002296const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
2297{
2298 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
2299 &snd_wss_playback_ops : &snd_wss_capture_ops;
2300}
2301EXPORT_SYMBOL(snd_wss_get_pcm_ops);
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303/*
2304 * INIT part
2305 */
2306
Krzysztof Helt7779f752008-07-31 21:03:41 +02002307static int __init alsa_wss_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 return 0;
2310}
2311
Krzysztof Helt7779f752008-07-31 21:03:41 +02002312static void __exit alsa_wss_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
2314}
2315
Krzysztof Helt7779f752008-07-31 21:03:41 +02002316module_init(alsa_wss_init);
2317module_exit(alsa_wss_exit);