blob: 026061b55ee9451edd8417d4a47e6fdddd56be74 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02003 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Routines for control of CS4231(A)/CS4232/InterWave & compatible chips
5 *
6 * Bugs:
Krzysztof Helt9295aea2008-07-31 21:00:17 +02007 * - sometimes record brokes playback with WSS portion of
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Yamaha OPL3-SA3 chip
9 * - CS4231 (GUS MAX) - still trouble with occasional noises
Krzysztof Helt7779f752008-07-31 21:03:41 +020010 * - broken initialization?
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/delay.h>
14#include <linux/pm.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
17#include <linux/slab.h>
18#include <linux/ioport.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040019#include <linux/module.h>
Takashi Iwai6cbbfe12015-01-28 16:49:33 +010020#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <sound/core.h>
Krzysztof Helt61ef19d2008-07-31 21:02:42 +020022#include <sound/wss.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <sound/pcm_params.h>
Krzysztof Helt5664daa2008-07-31 21:08:32 +020024#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/dma.h>
27#include <asm/irq.h>
28
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020029MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030MODULE_DESCRIPTION("Routines for control of CS4231(A)/CS4232/InterWave & compatible chips");
31MODULE_LICENSE("GPL");
32
33#if 0
34#define SNDRV_DEBUG_MCE
35#endif
36
37/*
38 * Some variables
39 */
40
Takashi Iwai429bca42020-01-05 15:48:06 +010041static const unsigned char freq_bits[14] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 /* 5510 */ 0x00 | CS4231_XTAL2,
43 /* 6620 */ 0x0E | CS4231_XTAL2,
44 /* 8000 */ 0x00 | CS4231_XTAL1,
45 /* 9600 */ 0x0E | CS4231_XTAL1,
46 /* 11025 */ 0x02 | CS4231_XTAL2,
47 /* 16000 */ 0x02 | CS4231_XTAL1,
48 /* 18900 */ 0x04 | CS4231_XTAL2,
49 /* 22050 */ 0x06 | CS4231_XTAL2,
50 /* 27042 */ 0x04 | CS4231_XTAL1,
51 /* 32000 */ 0x06 | CS4231_XTAL1,
52 /* 33075 */ 0x0C | CS4231_XTAL2,
53 /* 37800 */ 0x08 | CS4231_XTAL2,
54 /* 44100 */ 0x0A | CS4231_XTAL2,
55 /* 48000 */ 0x0C | CS4231_XTAL1
56};
57
Takashi Iwai7b3f4772017-06-07 14:16:43 +020058static const unsigned int rates[14] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050,
60 27042, 32000, 33075, 37800, 44100, 48000
61};
62
Takashi Iwai7b3f4772017-06-07 14:16:43 +020063static const struct snd_pcm_hw_constraint_list hw_constraints_rates = {
Krzysztof Helt6c041b52007-09-06 15:03:59 +020064 .count = ARRAY_SIZE(rates),
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 .list = rates,
66 .mask = 0,
67};
68
Krzysztof Helt7779f752008-07-31 21:03:41 +020069static int snd_wss_xrate(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Krzysztof Helt7779f752008-07-31 21:03:41 +020071 return snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
72 &hw_constraints_rates);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Takashi Iwai429bca42020-01-05 15:48:06 +010075static const unsigned char snd_wss_original_image[32] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
77 0x00, /* 00/00 - lic */
78 0x00, /* 01/01 - ric */
79 0x9f, /* 02/02 - la1ic */
80 0x9f, /* 03/03 - ra1ic */
81 0x9f, /* 04/04 - la2ic */
82 0x9f, /* 05/05 - ra2ic */
83 0xbf, /* 06/06 - loc */
84 0xbf, /* 07/07 - roc */
85 0x20, /* 08/08 - pdfr */
86 CS4231_AUTOCALIB, /* 09/09 - ic */
87 0x00, /* 0a/10 - pc */
88 0x00, /* 0b/11 - ti */
89 CS4231_MODE2, /* 0c/12 - mi */
90 0xfc, /* 0d/13 - lbc */
91 0x00, /* 0e/14 - pbru */
92 0x00, /* 0f/15 - pbrl */
93 0x80, /* 10/16 - afei */
94 0x01, /* 11/17 - afeii */
95 0x9f, /* 12/18 - llic */
96 0x9f, /* 13/19 - rlic */
97 0x00, /* 14/20 - tlb */
98 0x00, /* 15/21 - thb */
99 0x00, /* 16/22 - la3mic/reserved */
100 0x00, /* 17/23 - ra3mic/reserved */
101 0x00, /* 18/24 - afs */
102 0x00, /* 19/25 - lamoc/version */
103 0xcf, /* 1a/26 - mioc */
104 0x00, /* 1b/27 - ramoc/reserved */
105 0x20, /* 1c/28 - cdfr */
106 0x00, /* 1d/29 - res4 */
107 0x00, /* 1e/30 - cbru */
108 0x00, /* 1f/31 - cbrl */
109};
110
Takashi Iwai429bca42020-01-05 15:48:06 +0100111static const unsigned char snd_opti93x_original_image[32] =
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +0200112{
113 0x00, /* 00/00 - l_mixout_outctrl */
114 0x00, /* 01/01 - r_mixout_outctrl */
115 0x88, /* 02/02 - l_cd_inctrl */
116 0x88, /* 03/03 - r_cd_inctrl */
117 0x88, /* 04/04 - l_a1/fm_inctrl */
118 0x88, /* 05/05 - r_a1/fm_inctrl */
119 0x80, /* 06/06 - l_dac_inctrl */
120 0x80, /* 07/07 - r_dac_inctrl */
121 0x00, /* 08/08 - ply_dataform_reg */
122 0x00, /* 09/09 - if_conf */
123 0x00, /* 0a/10 - pin_ctrl */
124 0x00, /* 0b/11 - err_init_reg */
125 0x0a, /* 0c/12 - id_reg */
126 0x00, /* 0d/13 - reserved */
127 0x00, /* 0e/14 - ply_upcount_reg */
128 0x00, /* 0f/15 - ply_lowcount_reg */
129 0x88, /* 10/16 - reserved/l_a1_inctrl */
130 0x88, /* 11/17 - reserved/r_a1_inctrl */
131 0x88, /* 12/18 - l_line_inctrl */
132 0x88, /* 13/19 - r_line_inctrl */
133 0x88, /* 14/20 - l_mic_inctrl */
134 0x88, /* 15/21 - r_mic_inctrl */
135 0x80, /* 16/22 - l_out_outctrl */
136 0x80, /* 17/23 - r_out_outctrl */
137 0x00, /* 18/24 - reserved */
138 0x00, /* 19/25 - reserved */
139 0x00, /* 1a/26 - reserved */
140 0x00, /* 1b/27 - reserved */
141 0x00, /* 1c/28 - cap_dataform_reg */
142 0x00, /* 1d/29 - reserved */
143 0x00, /* 1e/30 - cap_upcount_reg */
144 0x00 /* 1f/31 - cap_lowcount_reg */
145};
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 * Basic I/O functions
149 */
150
Krzysztof Helt7779f752008-07-31 21:03:41 +0200151static inline void wss_outb(struct snd_wss *chip, u8 offset, u8 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 outb(val, chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Krzysztof Helt7779f752008-07-31 21:03:41 +0200156static inline u8 wss_inb(struct snd_wss *chip, u8 offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return inb(chip->port + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Krzysztof Helt7779f752008-07-31 21:03:41 +0200161static void snd_wss_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 int timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200166 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200168 udelay(100);
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200169}
170
Krzysztof Helt7779f752008-07-31 21:03:41 +0200171static void snd_wss_dout(struct snd_wss *chip, unsigned char reg,
172 unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 int timeout;
175
176 for (timeout = 250;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200177 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200179 udelay(10);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200180 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
181 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 mb();
183}
184
Krzysztof Helt7779f752008-07-31 21:03:41 +0200185void snd_wss_out(struct snd_wss *chip, unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200187 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200189 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100190 snd_printk(KERN_DEBUG "out: auto calibration time out "
191 "- reg = 0x%x, value = 0x%x\n", reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200193 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
194 wss_outb(chip, CS4231P(REG), value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 chip->image[reg] = value;
196 mb();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200197 snd_printdd("codec out - reg 0x%x = 0x%x\n",
198 chip->mce_bit | reg, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200200EXPORT_SYMBOL(snd_wss_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Krzysztof Helt7779f752008-07-31 21:03:41 +0200202unsigned char snd_wss_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200204 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200206 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100207 snd_printk(KERN_DEBUG "in: auto calibration time out "
208 "- reg = 0x%x\n", reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200210 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 mb();
Krzysztof Helt7779f752008-07-31 21:03:41 +0200212 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200214EXPORT_SYMBOL(snd_wss_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Krzysztof Helt7779f752008-07-31 21:03:41 +0200216void snd_cs4236_ext_out(struct snd_wss *chip, unsigned char reg,
217 unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200219 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
220 wss_outb(chip, CS4231P(REG),
221 reg | (chip->image[CS4236_EXT_REG] & 0x01));
222 wss_outb(chip, CS4231P(REG), val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 chip->eimage[CS4236_REG(reg)] = val;
224#if 0
Takashi Iwai76d498e2009-02-05 15:45:05 +0100225 printk(KERN_DEBUG "ext out : reg = 0x%x, val = 0x%x\n", reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
227}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200228EXPORT_SYMBOL(snd_cs4236_ext_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Krzysztof Helt7779f752008-07-31 21:03:41 +0200230unsigned char snd_cs4236_ext_in(struct snd_wss *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200232 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | 0x17);
233 wss_outb(chip, CS4231P(REG),
234 reg | (chip->image[CS4236_EXT_REG] & 0x01));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +0200236 return wss_inb(chip, CS4231P(REG));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237#else
238 {
239 unsigned char res;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200240 res = wss_inb(chip, CS4231P(REG));
Takashi Iwai76d498e2009-02-05 15:45:05 +0100241 printk(KERN_DEBUG "ext in : reg = 0x%x, val = 0x%x\n",
242 reg, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return res;
244 }
245#endif
246}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200247EXPORT_SYMBOL(snd_cs4236_ext_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249#if 0
250
Krzysztof Helt7779f752008-07-31 21:03:41 +0200251static void snd_wss_debug(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200253 printk(KERN_DEBUG
254 "CS4231 REGS: INDEX = 0x%02x "
255 " STATUS = 0x%02x\n",
Rene Herman3caf8c02008-08-08 16:39:21 +0200256 wss_inb(chip, CS4231P(REGSEL)),
Krzysztof Helt7779f752008-07-31 21:03:41 +0200257 wss_inb(chip, CS4231P(STATUS)));
258 printk(KERN_DEBUG
259 " 0x00: left input = 0x%02x "
260 " 0x10: alt 1 (CFIG 2) = 0x%02x\n",
261 snd_wss_in(chip, 0x00),
262 snd_wss_in(chip, 0x10));
263 printk(KERN_DEBUG
264 " 0x01: right input = 0x%02x "
265 " 0x11: alt 2 (CFIG 3) = 0x%02x\n",
266 snd_wss_in(chip, 0x01),
267 snd_wss_in(chip, 0x11));
268 printk(KERN_DEBUG
269 " 0x02: GF1 left input = 0x%02x "
270 " 0x12: left line in = 0x%02x\n",
271 snd_wss_in(chip, 0x02),
272 snd_wss_in(chip, 0x12));
273 printk(KERN_DEBUG
274 " 0x03: GF1 right input = 0x%02x "
275 " 0x13: right line in = 0x%02x\n",
276 snd_wss_in(chip, 0x03),
277 snd_wss_in(chip, 0x13));
278 printk(KERN_DEBUG
279 " 0x04: CD left input = 0x%02x "
280 " 0x14: timer low = 0x%02x\n",
281 snd_wss_in(chip, 0x04),
282 snd_wss_in(chip, 0x14));
283 printk(KERN_DEBUG
284 " 0x05: CD right input = 0x%02x "
285 " 0x15: timer high = 0x%02x\n",
286 snd_wss_in(chip, 0x05),
287 snd_wss_in(chip, 0x15));
288 printk(KERN_DEBUG
289 " 0x06: left output = 0x%02x "
290 " 0x16: left MIC (PnP) = 0x%02x\n",
291 snd_wss_in(chip, 0x06),
292 snd_wss_in(chip, 0x16));
293 printk(KERN_DEBUG
294 " 0x07: right output = 0x%02x "
295 " 0x17: right MIC (PnP) = 0x%02x\n",
296 snd_wss_in(chip, 0x07),
297 snd_wss_in(chip, 0x17));
298 printk(KERN_DEBUG
299 " 0x08: playback format = 0x%02x "
300 " 0x18: IRQ status = 0x%02x\n",
301 snd_wss_in(chip, 0x08),
302 snd_wss_in(chip, 0x18));
303 printk(KERN_DEBUG
304 " 0x09: iface (CFIG 1) = 0x%02x "
305 " 0x19: left line out = 0x%02x\n",
306 snd_wss_in(chip, 0x09),
307 snd_wss_in(chip, 0x19));
308 printk(KERN_DEBUG
309 " 0x0a: pin control = 0x%02x "
310 " 0x1a: mono control = 0x%02x\n",
311 snd_wss_in(chip, 0x0a),
312 snd_wss_in(chip, 0x1a));
313 printk(KERN_DEBUG
314 " 0x0b: init & status = 0x%02x "
315 " 0x1b: right line out = 0x%02x\n",
316 snd_wss_in(chip, 0x0b),
317 snd_wss_in(chip, 0x1b));
318 printk(KERN_DEBUG
319 " 0x0c: revision & mode = 0x%02x "
320 " 0x1c: record format = 0x%02x\n",
321 snd_wss_in(chip, 0x0c),
322 snd_wss_in(chip, 0x1c));
323 printk(KERN_DEBUG
324 " 0x0d: loopback = 0x%02x "
325 " 0x1d: var freq (PnP) = 0x%02x\n",
326 snd_wss_in(chip, 0x0d),
327 snd_wss_in(chip, 0x1d));
328 printk(KERN_DEBUG
329 " 0x0e: ply upr count = 0x%02x "
330 " 0x1e: ply lwr count = 0x%02x\n",
331 snd_wss_in(chip, 0x0e),
332 snd_wss_in(chip, 0x1e));
333 printk(KERN_DEBUG
334 " 0x0f: rec upr count = 0x%02x "
335 " 0x1f: rec lwr count = 0x%02x\n",
336 snd_wss_in(chip, 0x0f),
337 snd_wss_in(chip, 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340#endif
341
342/*
343 * CS4231 detection / MCE routines
344 */
345
Krzysztof Helt7779f752008-07-31 21:03:41 +0200346static void snd_wss_busy_wait(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int timeout;
349
350 /* huh.. looks like this sequence is proper for CS4231A chip (GUS MAX) */
351 for (timeout = 5; timeout > 0; timeout--)
Krzysztof Helt7779f752008-07-31 21:03:41 +0200352 wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* end of cleanup sequence */
Krzysztof Heltead893c2008-07-31 21:09:32 +0200354 for (timeout = 25000;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200355 timeout > 0 && (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 timeout--)
Krzysztof Helt9295aea2008-07-31 21:00:17 +0200357 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Krzysztof Helt7779f752008-07-31 21:03:41 +0200360void snd_wss_mce_up(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 unsigned long flags;
363 int timeout;
364
Krzysztof Helt7779f752008-07-31 21:03:41 +0200365 snd_wss_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200367 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100368 snd_printk(KERN_DEBUG
369 "mce_up - auto calibration time out (0)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#endif
371 spin_lock_irqsave(&chip->reg_lock, flags);
372 chip->mce_bit |= CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200373 timeout = wss_inb(chip, CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (timeout == 0x80)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100375 snd_printk(KERN_DEBUG "mce_up [0x%lx]: "
376 "serious init problem - codec still busy\n",
377 chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!(timeout & CS4231_MCE))
Krzysztof Helt7779f752008-07-31 21:03:41 +0200379 wss_outb(chip, CS4231P(REGSEL),
380 chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_unlock_irqrestore(&chip->reg_lock, flags);
382}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200383EXPORT_SYMBOL(snd_wss_mce_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Krzysztof Helt7779f752008-07-31 21:03:41 +0200385void snd_wss_mce_down(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 unsigned long flags;
Takashi Iwaib875d652007-09-11 10:12:14 +0200388 unsigned long end_time;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 int timeout;
Krzysztof Heltead893c2008-07-31 21:09:32 +0200390 int hw_mask = WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK | WSS_HW_AD1848;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Krzysztof Helt7779f752008-07-31 21:03:41 +0200392 snd_wss_busy_wait(chip);
Rene Hermand44df2d2007-09-10 23:22:55 +0200393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394#ifdef CONFIG_SND_DEBUG
Krzysztof Helt7779f752008-07-31 21:03:41 +0200395 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100396 snd_printk(KERN_DEBUG "mce_down [0x%lx] - "
397 "auto calibration time out (0)\n",
398 (long)CS4231P(REGSEL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399#endif
400 spin_lock_irqsave(&chip->reg_lock, flags);
401 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200402 timeout = wss_inb(chip, CS4231P(REGSEL));
403 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_unlock_irqrestore(&chip->reg_lock, flags);
405 if (timeout == 0x80)
Takashi Iwai76d498e2009-02-05 15:45:05 +0100406 snd_printk(KERN_DEBUG "mce_down [0x%lx]: "
407 "serious init problem - codec still busy\n",
408 chip->port);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200409 if ((timeout & CS4231_MCE) == 0 || !(chip->hardware & hw_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Rene Herman90cf9b82007-09-10 23:19:55 +0200412 /*
413 * Wait for (possible -- during init auto-calibration may not be set)
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300414 * calibration process to start. Needs up to 5 sample periods on AD1848
Rene Herman90cf9b82007-09-10 23:19:55 +0200415 * which at the slowest possible rate of 5.5125 kHz means 907 us.
416 */
417 msleep(1);
Rene Hermand44df2d2007-09-10 23:22:55 +0200418
419 snd_printdd("(1) jiffies = %lu\n", jiffies);
420
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200421 /* check condition up to 250 ms */
Takashi Iwaib875d652007-09-11 10:12:14 +0200422 end_time = jiffies + msecs_to_jiffies(250);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200423 while (snd_wss_in(chip, CS4231_TEST_INIT) &
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200424 CS4231_CALIB_IN_PROGRESS) {
425
Takashi Iwaib875d652007-09-11 10:12:14 +0200426 if (time_after(jiffies, end_time)) {
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200427 snd_printk(KERN_ERR "mce_down - "
428 "auto calibration time out (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return;
430 }
Takashi Iwaib875d652007-09-11 10:12:14 +0200431 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200433
434 snd_printdd("(2) jiffies = %lu\n", jiffies);
435
Krzysztof Helt23d4635ed2007-09-11 00:40:42 +0200436 /* check condition up to 100 ms */
Takashi Iwaib875d652007-09-11 10:12:14 +0200437 end_time = jiffies + msecs_to_jiffies(100);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200438 while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
Takashi Iwaib875d652007-09-11 10:12:14 +0200439 if (time_after(jiffies, end_time)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 snd_printk(KERN_ERR "mce_down - auto calibration time out (3)\n");
441 return;
442 }
Takashi Iwaib875d652007-09-11 10:12:14 +0200443 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
Rene Hermand44df2d2007-09-10 23:22:55 +0200445
446 snd_printdd("(3) jiffies = %lu\n", jiffies);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200447 snd_printd("mce_down - exit = 0x%x\n", wss_inb(chip, CS4231P(REGSEL)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
Krzysztof Helt7779f752008-07-31 21:03:41 +0200449EXPORT_SYMBOL(snd_wss_mce_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Krzysztof Helt7779f752008-07-31 21:03:41 +0200451static unsigned int snd_wss_get_count(unsigned char format, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 switch (format & 0xe0) {
454 case CS4231_LINEAR_16:
455 case CS4231_LINEAR_16_BIG:
456 size >>= 1;
457 break;
458 case CS4231_ADPCM_16:
459 return size >> 2;
460 }
461 if (format & CS4231_STEREO)
462 size >>= 1;
463 return size;
464}
465
Krzysztof Helt7779f752008-07-31 21:03:41 +0200466static int snd_wss_trigger(struct snd_pcm_substream *substream,
467 int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200469 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 int result = 0;
471 unsigned int what;
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100472 struct snd_pcm_substream *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 int do_start;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 switch (cmd) {
476 case SNDRV_PCM_TRIGGER_START:
477 case SNDRV_PCM_TRIGGER_RESUME:
478 do_start = 1; break;
479 case SNDRV_PCM_TRIGGER_STOP:
480 case SNDRV_PCM_TRIGGER_SUSPEND:
481 do_start = 0; break;
482 default:
483 return -EINVAL;
484 }
485
486 what = 0;
Takashi Iwaief991b92007-02-22 12:52:53 +0100487 snd_pcm_group_for_each_entry(s, substream) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (s == chip->playback_substream) {
489 what |= CS4231_PLAYBACK_ENABLE;
490 snd_pcm_trigger_done(s, substream);
491 } else if (s == chip->capture_substream) {
492 what |= CS4231_RECORD_ENABLE;
493 snd_pcm_trigger_done(s, substream);
494 }
495 }
496 spin_lock(&chip->reg_lock);
497 if (do_start) {
498 chip->image[CS4231_IFACE_CTRL] |= what;
499 if (chip->trigger)
500 chip->trigger(chip, what, 1);
501 } else {
502 chip->image[CS4231_IFACE_CTRL] &= ~what;
503 if (chip->trigger)
504 chip->trigger(chip, what, 0);
505 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200506 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 spin_unlock(&chip->reg_lock);
508#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +0200509 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510#endif
511 return result;
512}
513
514/*
515 * CODEC I/O
516 */
517
Krzysztof Helt7779f752008-07-31 21:03:41 +0200518static unsigned char snd_wss_get_rate(unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int i;
521
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200522 for (i = 0; i < ARRAY_SIZE(rates); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (rate == rates[i])
524 return freq_bits[i];
525 // snd_BUG();
Krzysztof Helt6c041b52007-09-06 15:03:59 +0200526 return freq_bits[ARRAY_SIZE(rates) - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Krzysztof Helt7779f752008-07-31 21:03:41 +0200529static unsigned char snd_wss_get_format(struct snd_wss *chip,
Takashi Iwai6be9a602018-07-25 23:19:42 +0200530 snd_pcm_format_t format,
Krzysztof Helt7779f752008-07-31 21:03:41 +0200531 int channels)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned char rformat;
534
535 rformat = CS4231_LINEAR_8;
536 switch (format) {
537 case SNDRV_PCM_FORMAT_MU_LAW: rformat = CS4231_ULAW_8; break;
538 case SNDRV_PCM_FORMAT_A_LAW: rformat = CS4231_ALAW_8; break;
539 case SNDRV_PCM_FORMAT_S16_LE: rformat = CS4231_LINEAR_16; break;
540 case SNDRV_PCM_FORMAT_S16_BE: rformat = CS4231_LINEAR_16_BIG; break;
541 case SNDRV_PCM_FORMAT_IMA_ADPCM: rformat = CS4231_ADPCM_16; break;
542 }
543 if (channels > 1)
544 rformat |= CS4231_STEREO;
545#if 0
Takashi Iwai76d498e2009-02-05 15:45:05 +0100546 snd_printk(KERN_DEBUG "get_format: 0x%x (mode=0x%x)\n", format, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#endif
548 return rformat;
549}
550
Krzysztof Helt7779f752008-07-31 21:03:41 +0200551static void snd_wss_calibrate_mute(struct snd_wss *chip, int mute)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
553 unsigned long flags;
554
Krzysztof Heltace457c2008-08-17 22:38:27 +0200555 mute = mute ? 0x80 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_lock_irqsave(&chip->reg_lock, flags);
557 if (chip->calibrate_mute == mute) {
558 spin_unlock_irqrestore(&chip->reg_lock, flags);
559 return;
560 }
561 if (!mute) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200562 snd_wss_dout(chip, CS4231_LEFT_INPUT,
563 chip->image[CS4231_LEFT_INPUT]);
564 snd_wss_dout(chip, CS4231_RIGHT_INPUT,
565 chip->image[CS4231_RIGHT_INPUT]);
566 snd_wss_dout(chip, CS4231_LOOPBACK,
567 chip->image[CS4231_LOOPBACK]);
Krzysztof Helt9ef344f2009-01-16 22:47:30 +0100568 } else {
569 snd_wss_dout(chip, CS4231_LEFT_INPUT,
570 0);
571 snd_wss_dout(chip, CS4231_RIGHT_INPUT,
572 0);
573 snd_wss_dout(chip, CS4231_LOOPBACK,
574 0xfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Krzysztof Helt9ef344f2009-01-16 22:47:30 +0100576
Krzysztof Helt7779f752008-07-31 21:03:41 +0200577 snd_wss_dout(chip, CS4231_AUX1_LEFT_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200578 mute | chip->image[CS4231_AUX1_LEFT_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200579 snd_wss_dout(chip, CS4231_AUX1_RIGHT_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200580 mute | chip->image[CS4231_AUX1_RIGHT_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200581 snd_wss_dout(chip, CS4231_AUX2_LEFT_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200582 mute | chip->image[CS4231_AUX2_LEFT_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200583 snd_wss_dout(chip, CS4231_AUX2_RIGHT_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200584 mute | chip->image[CS4231_AUX2_RIGHT_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200585 snd_wss_dout(chip, CS4231_LEFT_OUTPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200586 mute | chip->image[CS4231_LEFT_OUTPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200587 snd_wss_dout(chip, CS4231_RIGHT_OUTPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200588 mute | chip->image[CS4231_RIGHT_OUTPUT]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200589 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
590 snd_wss_dout(chip, CS4231_LEFT_LINE_IN,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200591 mute | chip->image[CS4231_LEFT_LINE_IN]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200592 snd_wss_dout(chip, CS4231_RIGHT_LINE_IN,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200593 mute | chip->image[CS4231_RIGHT_LINE_IN]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200594 snd_wss_dout(chip, CS4231_MONO_CTRL,
595 mute ? 0xc0 : chip->image[CS4231_MONO_CTRL]);
596 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200597 if (chip->hardware == WSS_HW_INTERWAVE) {
598 snd_wss_dout(chip, CS4231_LEFT_MIC_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200599 mute | chip->image[CS4231_LEFT_MIC_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200600 snd_wss_dout(chip, CS4231_RIGHT_MIC_INPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200601 mute | chip->image[CS4231_RIGHT_MIC_INPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200602 snd_wss_dout(chip, CS4231_LINE_LEFT_OUTPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200603 mute | chip->image[CS4231_LINE_LEFT_OUTPUT]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200604 snd_wss_dout(chip, CS4231_LINE_RIGHT_OUTPUT,
Krzysztof Heltace457c2008-08-17 22:38:27 +0200605 mute | chip->image[CS4231_LINE_RIGHT_OUTPUT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 chip->calibrate_mute = mute;
608 spin_unlock_irqrestore(&chip->reg_lock, flags);
609}
610
Krzysztof Helt7779f752008-07-31 21:03:41 +0200611static void snd_wss_playback_format(struct snd_wss *chip,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100612 struct snd_pcm_hw_params *params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 unsigned char pdfr)
614{
615 unsigned long flags;
616 int full_calib = 1;
617
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100618 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200619 if (chip->hardware == WSS_HW_CS4231A ||
620 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 spin_lock_irqsave(&chip->reg_lock, flags);
622 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (pdfr & 0x0f)) { /* rate is same? */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200623 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
624 chip->image[CS4231_ALT_FEATURE_1] | 0x10);
625 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
626 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
627 chip->image[CS4231_PLAYBK_FORMAT]);
628 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
629 chip->image[CS4231_ALT_FEATURE_1] &= ~0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 udelay(100); /* Fixes audible clicks at least on GUS MAX */
631 full_calib = 0;
632 }
633 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt199f7972009-01-09 23:10:52 +0100634 } else if (chip->hardware == WSS_HW_AD1845) {
635 unsigned rate = params_rate(params);
636
637 /*
638 * Program the AD1845 correctly for the playback stream.
639 * Note that we do NOT need to toggle the MCE bit because
640 * the PLAYBACK_ENABLE bit of the Interface Configuration
641 * register is set.
642 *
643 * NOTE: We seem to need to write to the MSB before the LSB
644 * to get the correct sample frequency.
645 */
646 spin_lock_irqsave(&chip->reg_lock, flags);
647 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, (pdfr & 0xf0));
648 snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff);
649 snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff);
650 full_calib = 0;
651 spin_unlock_irqrestore(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200654 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200656 if (chip->hardware != WSS_HW_INTERWAVE && !chip->single_dma) {
657 if (chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE)
658 pdfr = (pdfr & 0xf0) |
659 (chip->image[CS4231_REC_FORMAT] & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200661 chip->image[CS4231_PLAYBK_FORMAT] = pdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200663 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, pdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200665 if (chip->hardware == WSS_HW_OPL3SA2)
Paul Vojtae2340462007-07-27 12:20:38 +0200666 udelay(100); /* this seems to help */
Krzysztof Helt7779f752008-07-31 21:03:41 +0200667 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100669 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Krzysztof Helt7779f752008-07-31 21:03:41 +0200672static void snd_wss_capture_format(struct snd_wss *chip,
673 struct snd_pcm_hw_params *params,
674 unsigned char cdfr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 unsigned long flags;
677 int full_calib = 1;
678
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100679 mutex_lock(&chip->mce_mutex);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200680 if (chip->hardware == WSS_HW_CS4231A ||
681 (chip->hardware & WSS_HW_CS4232_MASK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_lock_irqsave(&chip->reg_lock, flags);
683 if ((chip->image[CS4231_PLAYBK_FORMAT] & 0x0f) == (cdfr & 0x0f) || /* rate is same? */
684 (chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200685 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
686 chip->image[CS4231_ALT_FEATURE_1] | 0x20);
687 snd_wss_out(chip, CS4231_REC_FORMAT,
688 chip->image[CS4231_REC_FORMAT] = cdfr);
689 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
690 chip->image[CS4231_ALT_FEATURE_1] &= ~0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 full_calib = 0;
692 }
693 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt199f7972009-01-09 23:10:52 +0100694 } else if (chip->hardware == WSS_HW_AD1845) {
695 unsigned rate = params_rate(params);
696
697 /*
698 * Program the AD1845 correctly for the capture stream.
699 * Note that we do NOT need to toggle the MCE bit because
700 * the PLAYBACK_ENABLE bit of the Interface Configuration
701 * register is set.
702 *
703 * NOTE: We seem to need to write to the MSB before the LSB
704 * to get the correct sample frequency.
705 */
706 spin_lock_irqsave(&chip->reg_lock, flags);
707 snd_wss_out(chip, CS4231_REC_FORMAT, (cdfr & 0xf0));
708 snd_wss_out(chip, AD1845_UPR_FREQ_SEL, (rate >> 8) & 0xff);
709 snd_wss_out(chip, AD1845_LWR_FREQ_SEL, rate & 0xff);
710 full_calib = 0;
711 spin_unlock_irqrestore(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 if (full_calib) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200714 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200716 if (chip->hardware != WSS_HW_INTERWAVE &&
717 !(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE)) {
718 if (chip->single_dma)
719 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
720 else
721 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
722 (chip->image[CS4231_PLAYBK_FORMAT] & 0xf0) |
723 (cdfr & 0x0f));
724 spin_unlock_irqrestore(&chip->reg_lock, flags);
725 snd_wss_mce_down(chip);
726 snd_wss_mce_up(chip);
727 spin_lock_irqsave(&chip->reg_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Krzysztof Heltead893c2008-07-31 21:09:32 +0200729 if (chip->hardware & WSS_HW_AD1848_MASK)
730 snd_wss_out(chip, CS4231_PLAYBK_FORMAT, cdfr);
731 else
732 snd_wss_out(chip, CS4231_REC_FORMAT, cdfr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200734 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100736 mutex_unlock(&chip->mce_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
739/*
740 * Timer interface
741 */
742
Krzysztof Helt7779f752008-07-31 21:03:41 +0200743static unsigned long snd_wss_timer_resolution(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200745 struct snd_wss *chip = snd_timer_chip(timer);
746 if (chip->hardware & WSS_HW_CS4236B_MASK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 14467;
748 else
749 return chip->image[CS4231_PLAYBK_FORMAT] & 1 ? 9969 : 9920;
750}
751
Krzysztof Helt7779f752008-07-31 21:03:41 +0200752static int snd_wss_timer_start(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 unsigned long flags;
755 unsigned int ticks;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200756 struct snd_wss *chip = snd_timer_chip(timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 spin_lock_irqsave(&chip->reg_lock, flags);
758 ticks = timer->sticks;
759 if ((chip->image[CS4231_ALT_FEATURE_1] & CS4231_TIMER_ENABLE) == 0 ||
760 (unsigned char)(ticks >> 8) != chip->image[CS4231_TIMER_HIGH] ||
761 (unsigned char)ticks != chip->image[CS4231_TIMER_LOW]) {
Krzysztof Helt7779f752008-07-31 21:03:41 +0200762 chip->image[CS4231_TIMER_HIGH] = (unsigned char) (ticks >> 8);
763 snd_wss_out(chip, CS4231_TIMER_HIGH,
764 chip->image[CS4231_TIMER_HIGH]);
765 chip->image[CS4231_TIMER_LOW] = (unsigned char) ticks;
766 snd_wss_out(chip, CS4231_TIMER_LOW,
767 chip->image[CS4231_TIMER_LOW]);
768 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
769 chip->image[CS4231_ALT_FEATURE_1] |
770 CS4231_TIMER_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772 spin_unlock_irqrestore(&chip->reg_lock, flags);
773 return 0;
774}
775
Krzysztof Helt7779f752008-07-31 21:03:41 +0200776static int snd_wss_timer_stop(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 unsigned long flags;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200779 struct snd_wss *chip = snd_timer_chip(timer);
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_ALT_FEATURE_1] &= ~CS4231_TIMER_ENABLE;
782 snd_wss_out(chip, CS4231_ALT_FEATURE_1,
783 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 spin_unlock_irqrestore(&chip->reg_lock, flags);
785 return 0;
786}
787
Krzysztof Helt7779f752008-07-31 21:03:41 +0200788static void snd_wss_init(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 unsigned long flags;
791
Krzysztof Helt9ef344f2009-01-16 22:47:30 +0100792 snd_wss_calibrate_mute(chip, 1);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200793 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795#ifdef SNDRV_DEBUG_MCE
Takashi Iwai76d498e2009-02-05 15:45:05 +0100796 snd_printk(KERN_DEBUG "init: (1)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797#endif
Krzysztof Helt7779f752008-07-31 21:03:41 +0200798 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200800 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE |
801 CS4231_PLAYBACK_PIO |
802 CS4231_RECORD_ENABLE |
803 CS4231_RECORD_PIO |
804 CS4231_CALIB_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 chip->image[CS4231_IFACE_CTRL] |= CS4231_AUTOCALIB;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200806 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200808 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810#ifdef SNDRV_DEBUG_MCE
Takashi Iwai76d498e2009-02-05 15:45:05 +0100811 snd_printk(KERN_DEBUG "init: (2)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#endif
813
Krzysztof Helt7779f752008-07-31 21:03:41 +0200814 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt9ef344f2009-01-16 22:47:30 +0100816 chip->image[CS4231_IFACE_CTRL] &= ~CS4231_AUTOCALIB;
817 snd_wss_out(chip, CS4231_IFACE_CTRL, chip->image[CS4231_IFACE_CTRL]);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200818 snd_wss_out(chip,
819 CS4231_ALT_FEATURE_1, chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200821 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823#ifdef SNDRV_DEBUG_MCE
Takashi Iwai76d498e2009-02-05 15:45:05 +0100824 snd_printk(KERN_DEBUG "init: (3) - afei = 0x%x\n",
Krzysztof Helt7779f752008-07-31 21:03:41 +0200825 chip->image[CS4231_ALT_FEATURE_1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826#endif
827
828 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200829 snd_wss_out(chip, CS4231_ALT_FEATURE_2,
830 chip->image[CS4231_ALT_FEATURE_2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 spin_unlock_irqrestore(&chip->reg_lock, flags);
832
Krzysztof Helt7779f752008-07-31 21:03:41 +0200833 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200835 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
836 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200838 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840#ifdef SNDRV_DEBUG_MCE
Takashi Iwai76d498e2009-02-05 15:45:05 +0100841 snd_printk(KERN_DEBUG "init: (4)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842#endif
843
Krzysztof Helt7779f752008-07-31 21:03:41 +0200844 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200846 if (!(chip->hardware & WSS_HW_AD1848_MASK))
847 snd_wss_out(chip, CS4231_REC_FORMAT,
848 chip->image[CS4231_REC_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200850 snd_wss_mce_down(chip);
Krzysztof Helt9ef344f2009-01-16 22:47:30 +0100851 snd_wss_calibrate_mute(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853#ifdef SNDRV_DEBUG_MCE
Takashi Iwai76d498e2009-02-05 15:45:05 +0100854 snd_printk(KERN_DEBUG "init: (5)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855#endif
856}
857
Krzysztof Helt7779f752008-07-31 21:03:41 +0200858static int snd_wss_open(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 unsigned long flags;
861
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100862 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if ((chip->mode & mode) ||
Krzysztof Helt7779f752008-07-31 21:03:41 +0200864 ((chip->mode & WSS_MODE_OPEN) && chip->single_dma)) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100865 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EAGAIN;
867 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200868 if (chip->mode & WSS_MODE_OPEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 chip->mode |= mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100870 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872 }
873 /* ok. now enable and ack CODEC IRQ */
874 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200875 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
876 snd_wss_out(chip, CS4231_IRQ_STATUS,
877 CS4231_PLAYBACK_IRQ |
878 CS4231_RECORD_IRQ |
879 CS4231_TIMER_IRQ);
880 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
881 }
Krzysztof Helt7779f752008-07-31 21:03:41 +0200882 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
883 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 chip->image[CS4231_PIN_CTRL] |= CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200885 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200886 if (!(chip->hardware & WSS_HW_AD1848_MASK)) {
887 snd_wss_out(chip, CS4231_IRQ_STATUS,
888 CS4231_PLAYBACK_IRQ |
889 CS4231_RECORD_IRQ |
890 CS4231_TIMER_IRQ);
891 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 spin_unlock_irqrestore(&chip->reg_lock, flags);
894
895 chip->mode = mode;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100896 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898}
899
Krzysztof Helt7779f752008-07-31 21:03:41 +0200900static void snd_wss_close(struct snd_wss *chip, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 unsigned long flags;
903
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100904 mutex_lock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 chip->mode &= ~mode;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200906 if (chip->mode & WSS_MODE_OPEN) {
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100907 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* disable IRQ */
911 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +0200912 if (!(chip->hardware & WSS_HW_AD1848_MASK))
913 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200914 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
915 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 chip->image[CS4231_PIN_CTRL] &= ~CS4231_IRQ_ENABLE;
Krzysztof Helt7779f752008-07-31 21:03:41 +0200917 snd_wss_out(chip, CS4231_PIN_CTRL, chip->image[CS4231_PIN_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /* now disable record & playback */
920
921 if (chip->image[CS4231_IFACE_CTRL] & (CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
922 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO)) {
923 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200924 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 spin_lock_irqsave(&chip->reg_lock, flags);
926 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO |
927 CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200928 snd_wss_out(chip, CS4231_IFACE_CTRL,
929 chip->image[CS4231_IFACE_CTRL]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200931 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 spin_lock_irqsave(&chip->reg_lock, flags);
933 }
934
935 /* clear IRQ again */
Krzysztof Heltead893c2008-07-31 21:09:32 +0200936 if (!(chip->hardware & WSS_HW_AD1848_MASK))
937 snd_wss_out(chip, CS4231_IRQ_STATUS, 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +0200938 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
939 wss_outb(chip, CS4231P(STATUS), 0); /* clear IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_unlock_irqrestore(&chip->reg_lock, flags);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 chip->mode = 0;
Ingo Molnar8b7547f2006-01-16 16:33:08 +0100943 mutex_unlock(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/*
947 * timer open/close
948 */
949
Krzysztof Helt7779f752008-07-31 21:03:41 +0200950static int snd_wss_timer_open(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200952 struct snd_wss *chip = snd_timer_chip(timer);
953 snd_wss_open(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
Krzysztof Helt7779f752008-07-31 21:03:41 +0200957static int snd_wss_timer_close(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200959 struct snd_wss *chip = snd_timer_chip(timer);
960 snd_wss_close(chip, WSS_MODE_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
962}
963
Takashi Iwai5ff16a32020-01-03 09:16:37 +0100964static const struct snd_timer_hardware snd_wss_timer_table =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 .flags = SNDRV_TIMER_HW_AUTO,
967 .resolution = 9945,
968 .ticks = 65535,
Krzysztof Helt7779f752008-07-31 21:03:41 +0200969 .open = snd_wss_timer_open,
970 .close = snd_wss_timer_close,
971 .c_resolution = snd_wss_timer_resolution,
972 .start = snd_wss_timer_start,
973 .stop = snd_wss_timer_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974};
975
976/*
977 * ok.. exported functions..
978 */
979
Krzysztof Helt7779f752008-07-31 21:03:41 +0200980static int snd_wss_playback_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100981 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200983 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 unsigned char new_pdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Krzysztof Helt7779f752008-07-31 21:03:41 +0200986 new_pdfr = snd_wss_get_format(chip, params_format(hw_params),
987 params_channels(hw_params)) |
988 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 chip->set_playback_format(chip, hw_params, new_pdfr);
990 return 0;
991}
992
Krzysztof Helt7779f752008-07-31 21:03:41 +0200993static int snd_wss_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Krzysztof Helt7779f752008-07-31 21:03:41 +0200995 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +0100996 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned long flags;
998 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
999 unsigned int count = snd_pcm_lib_period_bytes(substream);
1000
1001 spin_lock_irqsave(&chip->reg_lock, flags);
1002 chip->p_dma_size = size;
1003 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_PLAYBACK_ENABLE | CS4231_PLAYBACK_PIO);
1004 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001005 count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT], count) - 1;
1006 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
1007 snd_wss_out(chip, CS4231_PLY_UPR_CNT, (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_unlock_irqrestore(&chip->reg_lock, flags);
1009#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001010 snd_wss_debug(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#endif
1012 return 0;
1013}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Krzysztof Helt7779f752008-07-31 21:03:41 +02001015static int snd_wss_capture_hw_params(struct snd_pcm_substream *substream,
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001016 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001018 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 unsigned char new_cdfr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Krzysztof Helt7779f752008-07-31 21:03:41 +02001021 new_cdfr = snd_wss_get_format(chip, params_format(hw_params),
1022 params_channels(hw_params)) |
1023 snd_wss_get_rate(params_rate(hw_params));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 chip->set_capture_format(chip, hw_params, new_cdfr);
1025 return 0;
1026}
1027
Krzysztof Helt7779f752008-07-31 21:03:41 +02001028static int snd_wss_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001030 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001031 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned long flags;
1033 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1034 unsigned int count = snd_pcm_lib_period_bytes(substream);
1035
1036 spin_lock_irqsave(&chip->reg_lock, flags);
1037 chip->c_dma_size = size;
1038 chip->image[CS4231_IFACE_CTRL] &= ~(CS4231_RECORD_ENABLE | CS4231_RECORD_PIO);
1039 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
Krzysztof Heltead893c2008-07-31 21:09:32 +02001040 if (chip->hardware & WSS_HW_AD1848_MASK)
1041 count = snd_wss_get_count(chip->image[CS4231_PLAYBK_FORMAT],
1042 count);
1043 else
1044 count = snd_wss_get_count(chip->image[CS4231_REC_FORMAT],
1045 count);
1046 count--;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001047 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
1048 snd_wss_out(chip, CS4231_PLY_LWR_CNT, (unsigned char) count);
1049 snd_wss_out(chip, CS4231_PLY_UPR_CNT,
1050 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 } else {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001052 snd_wss_out(chip, CS4231_REC_LWR_CNT, (unsigned char) count);
1053 snd_wss_out(chip, CS4231_REC_UPR_CNT,
1054 (unsigned char) (count >> 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056 spin_unlock_irqrestore(&chip->reg_lock, flags);
1057 return 0;
1058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Krzysztof Helt7779f752008-07-31 21:03:41 +02001060void snd_wss_overrange(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
1062 unsigned long flags;
1063 unsigned char res;
1064
1065 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001066 res = snd_wss_in(chip, CS4231_TEST_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 spin_unlock_irqrestore(&chip->reg_lock, flags);
1068 if (res & (0x08 | 0x02)) /* detect overrange only above 0dB; may be user selectable? */
1069 chip->capture_substream->runtime->overrange++;
1070}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001071EXPORT_SYMBOL(snd_wss_overrange);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Krzysztof Helt7779f752008-07-31 21:03:41 +02001073irqreturn_t snd_wss_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001075 struct snd_wss *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 unsigned char status;
1077
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001078 if (chip->hardware & WSS_HW_AD1848_MASK)
1079 /* pretend it was the only possible irq for AD1848 */
1080 status = CS4231_PLAYBACK_IRQ;
1081 else
1082 status = snd_wss_in(chip, CS4231_IRQ_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (status & CS4231_TIMER_IRQ) {
1084 if (chip->timer)
1085 snd_timer_interrupt(chip->timer, chip->timer->sticks);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001086 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001087 if (chip->single_dma && chip->hardware != WSS_HW_INTERWAVE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (status & CS4231_PLAYBACK_IRQ) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001089 if (chip->mode & WSS_MODE_PLAY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (chip->playback_substream)
1091 snd_pcm_period_elapsed(chip->playback_substream);
1092 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001093 if (chip->mode & WSS_MODE_RECORD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001095 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 snd_pcm_period_elapsed(chip->capture_substream);
1097 }
1098 }
1099 }
1100 } else {
1101 if (status & CS4231_PLAYBACK_IRQ) {
1102 if (chip->playback_substream)
1103 snd_pcm_period_elapsed(chip->playback_substream);
1104 }
1105 if (status & CS4231_RECORD_IRQ) {
1106 if (chip->capture_substream) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001107 snd_wss_overrange(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 snd_pcm_period_elapsed(chip->capture_substream);
1109 }
1110 }
1111 }
1112
1113 spin_lock(&chip->reg_lock);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001114 status = ~CS4231_ALL_IRQS | ~status;
1115 if (chip->hardware & WSS_HW_AD1848_MASK)
1116 wss_outb(chip, CS4231P(STATUS), 0);
1117 else
Krzysztof Helt9ef344f2009-01-16 22:47:30 +01001118 snd_wss_out(chip, CS4231_IRQ_STATUS, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_unlock(&chip->reg_lock);
1120 return IRQ_HANDLED;
1121}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001122EXPORT_SYMBOL(snd_wss_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Krzysztof Helt7779f752008-07-31 21:03:41 +02001124static snd_pcm_uframes_t snd_wss_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001126 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 size_t ptr;
1128
1129 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_PLAYBACK_ENABLE))
1130 return 0;
1131 ptr = snd_dma_pointer(chip->dma1, chip->p_dma_size);
1132 return bytes_to_frames(substream->runtime, ptr);
1133}
1134
Krzysztof Helt7779f752008-07-31 21:03:41 +02001135static snd_pcm_uframes_t snd_wss_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001137 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 size_t ptr;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!(chip->image[CS4231_IFACE_CTRL] & CS4231_RECORD_ENABLE))
1141 return 0;
1142 ptr = snd_dma_pointer(chip->dma2, chip->c_dma_size);
1143 return bytes_to_frames(substream->runtime, ptr);
1144}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146/*
1147
1148 */
1149
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001150static int snd_ad1848_probe(struct snd_wss *chip)
1151{
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001152 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001153 unsigned long flags;
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001154 unsigned char r;
1155 unsigned short hardware = 0;
1156 int err = 0;
1157 int i;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001158
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001159 while (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT) {
1160 if (time_after(jiffies, timeout))
1161 return -ENODEV;
1162 cond_resched();
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001163 }
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001164 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001165
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001166 /* set CS423x MODE 1 */
Krzysztof Heltf83a59c2008-08-24 18:08:03 +02001167 snd_wss_dout(chip, CS4231_MISC_INFO, 0);
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001168
Krzysztof Heltf83a59c2008-08-24 18:08:03 +02001169 snd_wss_dout(chip, CS4231_RIGHT_INPUT, 0x45); /* 0x55 & ~0x10 */
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001170 r = snd_wss_in(chip, CS4231_RIGHT_INPUT);
1171 if (r != 0x45) {
1172 /* RMGE always high on AD1847 */
1173 if ((r & ~CS4231_ENABLE_MIC_GAIN) != 0x45) {
1174 err = -ENODEV;
1175 goto out;
1176 }
1177 hardware = WSS_HW_AD1847;
1178 } else {
Krzysztof Heltf83a59c2008-08-24 18:08:03 +02001179 snd_wss_dout(chip, CS4231_LEFT_INPUT, 0xaa);
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001180 r = snd_wss_in(chip, CS4231_LEFT_INPUT);
1181 /* L/RMGE always low on AT2320 */
1182 if ((r | CS4231_ENABLE_MIC_GAIN) != 0xaa) {
1183 err = -ENODEV;
1184 goto out;
1185 }
1186 }
1187
1188 /* clear pending IRQ */
1189 wss_inb(chip, CS4231P(STATUS));
1190 wss_outb(chip, CS4231P(STATUS), 0);
1191 mb();
1192
1193 if ((chip->hardware & WSS_HW_TYPE_MASK) != WSS_HW_DETECT)
1194 goto out;
1195
1196 if (hardware) {
1197 chip->hardware = hardware;
1198 goto out;
1199 }
1200
1201 r = snd_wss_in(chip, CS4231_MISC_INFO);
1202
1203 /* set CS423x MODE 2 */
Krzysztof Heltf83a59c2008-08-24 18:08:03 +02001204 snd_wss_dout(chip, CS4231_MISC_INFO, CS4231_MODE2);
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001205 for (i = 0; i < 16; i++) {
1206 if (snd_wss_in(chip, i) != snd_wss_in(chip, 16 + i)) {
1207 /* we have more than 16 registers: check ID */
1208 if ((r & 0xf) != 0xa)
1209 goto out_mode;
1210 /*
1211 * on CMI8330, CS4231_VERSION is volume control and
1212 * can be set to 0
1213 */
1214 snd_wss_dout(chip, CS4231_VERSION, 0);
1215 r = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
1216 if (!r)
1217 chip->hardware = WSS_HW_CMI8330;
1218 goto out_mode;
1219 }
1220 }
1221 if (r & 0x80)
1222 chip->hardware = WSS_HW_CS4248;
1223 else
1224 chip->hardware = WSS_HW_AD1848;
1225out_mode:
Krzysztof Heltf83a59c2008-08-24 18:08:03 +02001226 snd_wss_dout(chip, CS4231_MISC_INFO, 0);
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001227out:
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001228 spin_unlock_irqrestore(&chip->reg_lock, flags);
Rene Hermanc9a7dc22008-08-06 08:09:34 +02001229 return err;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001230}
1231
Krzysztof Helt7779f752008-07-31 21:03:41 +02001232static int snd_wss_probe(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 unsigned long flags;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001235 int i, id, rev, regnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 unsigned char *ptr;
1237 unsigned int hw;
1238
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001239 id = snd_ad1848_probe(chip);
1240 if (id < 0)
1241 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Krzysztof Helt7779f752008-07-31 21:03:41 +02001243 hw = chip->hardware;
1244 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001245 for (i = 0; i < 50; i++) {
1246 mb();
1247 if (wss_inb(chip, CS4231P(REGSEL)) & CS4231_INIT)
1248 msleep(2);
1249 else {
1250 spin_lock_irqsave(&chip->reg_lock, flags);
1251 snd_wss_out(chip, CS4231_MISC_INFO,
1252 CS4231_MODE2);
1253 id = snd_wss_in(chip, CS4231_MISC_INFO) & 0x0f;
1254 spin_unlock_irqrestore(&chip->reg_lock, flags);
1255 if (id == 0x0a)
1256 break; /* this is valid value */
1257 }
1258 }
1259 snd_printdd("wss: port = 0x%lx, id = 0x%x\n", chip->port, id);
1260 if (id != 0x0a)
1261 return -ENODEV; /* no valid device found */
1262
Krzysztof Helt7779f752008-07-31 21:03:41 +02001263 rev = snd_wss_in(chip, CS4231_VERSION) & 0xe7;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 snd_printdd("CS4231: VERSION (I25) = 0x%x\n", rev);
1265 if (rev == 0x80) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001266 unsigned char tmp = snd_wss_in(chip, 23);
1267 snd_wss_out(chip, 23, ~tmp);
1268 if (snd_wss_in(chip, 23) != tmp)
1269 chip->hardware = WSS_HW_AD1845;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001271 chip->hardware = WSS_HW_CS4231;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 } else if (rev == 0xa0) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001273 chip->hardware = WSS_HW_CS4231A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else if (rev == 0xa2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001275 chip->hardware = WSS_HW_CS4232;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 } else if (rev == 0xb2) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001277 chip->hardware = WSS_HW_CS4232A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else if (rev == 0x83) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001279 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else if (rev == 0x03) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001281 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 } else {
Takashi Iwai76d498e2009-02-05 15:45:05 +01001283 snd_printk(KERN_ERR
1284 "unknown CS chip with version 0x%x\n", rev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 return -ENODEV; /* unknown CS4231 chip? */
1286 }
1287 }
1288 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001289 wss_inb(chip, CS4231P(STATUS)); /* clear any pendings IRQ */
1290 wss_outb(chip, CS4231P(STATUS), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 mb();
1292 spin_unlock_irqrestore(&chip->reg_lock, flags);
1293
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001294 if (!(chip->hardware & WSS_HW_AD1848_MASK))
1295 chip->image[CS4231_MISC_INFO] = CS4231_MODE2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001297 case WSS_HW_INTERWAVE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 chip->image[CS4231_MISC_INFO] = CS4231_IW_MODE3;
1299 break;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001300 case WSS_HW_CS4235:
1301 case WSS_HW_CS4236B:
1302 case WSS_HW_CS4237B:
1303 case WSS_HW_CS4238B:
1304 case WSS_HW_CS4239:
1305 if (hw == WSS_HW_DETECT3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 chip->image[CS4231_MISC_INFO] = CS4231_4236_MODE3;
1307 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001308 chip->hardware = WSS_HW_CS4236;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 break;
1310 }
1311
1312 chip->image[CS4231_IFACE_CTRL] =
1313 (chip->image[CS4231_IFACE_CTRL] & ~CS4231_SINGLE_DMA) |
1314 (chip->single_dma ? CS4231_SINGLE_DMA : 0);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001315 if (chip->hardware != WSS_HW_OPTI93X) {
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001316 chip->image[CS4231_ALT_FEATURE_1] = 0x80;
1317 chip->image[CS4231_ALT_FEATURE_2] =
Krzysztof Helt7779f752008-07-31 21:03:41 +02001318 chip->hardware == WSS_HW_INTERWAVE ? 0xc2 : 0x01;
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001319 }
Krzysztof Helt199f7972009-01-09 23:10:52 +01001320 /* enable fine grained frequency selection */
1321 if (chip->hardware == WSS_HW_AD1845)
1322 chip->image[AD1845_PWR_DOWN] = 8;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 ptr = (unsigned char *) &chip->image;
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001325 regnum = (chip->hardware & WSS_HW_AD1848_MASK) ? 16 : 32;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001326 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001328 for (i = 0; i < regnum; i++) /* ok.. fill all registers */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001329 snd_wss_out(chip, i, *ptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001331 snd_wss_mce_up(chip);
1332 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 mdelay(2);
1335
1336 /* ok.. try check hardware version for CS4236+ chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001337 if ((hw & WSS_HW_TYPE_MASK) == WSS_HW_DETECT) {
1338 if (chip->hardware == WSS_HW_CS4236B) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 rev = snd_cs4236_ext_in(chip, CS4236_VERSION);
1340 snd_cs4236_ext_out(chip, CS4236_VERSION, 0xff);
1341 id = snd_cs4236_ext_in(chip, CS4236_VERSION);
1342 snd_cs4236_ext_out(chip, CS4236_VERSION, rev);
1343 snd_printdd("CS4231: ext version; rev = 0x%x, id = 0x%x\n", rev, id);
1344 if ((id & 0x1f) == 0x1d) { /* CS4235 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001345 chip->hardware = WSS_HW_CS4235;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 switch (id >> 5) {
1347 case 4:
1348 case 5:
1349 case 6:
1350 break;
1351 default:
Takashi Iwai76d498e2009-02-05 15:45:05 +01001352 snd_printk(KERN_WARNING
1353 "unknown CS4235 chip "
1354 "(enhanced version = 0x%x)\n",
1355 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
1357 } else if ((id & 0x1f) == 0x0b) { /* CS4236/B */
1358 switch (id >> 5) {
1359 case 4:
1360 case 5:
1361 case 6:
1362 case 7:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001363 chip->hardware = WSS_HW_CS4236B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 break;
1365 default:
Takashi Iwai76d498e2009-02-05 15:45:05 +01001366 snd_printk(KERN_WARNING
1367 "unknown CS4236 chip "
1368 "(enhanced version = 0x%x)\n",
1369 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 } else if ((id & 0x1f) == 0x08) { /* CS4237B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001372 chip->hardware = WSS_HW_CS4237B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 switch (id >> 5) {
1374 case 4:
1375 case 5:
1376 case 6:
1377 case 7:
1378 break;
1379 default:
Takashi Iwai76d498e2009-02-05 15:45:05 +01001380 snd_printk(KERN_WARNING
1381 "unknown CS4237B chip "
1382 "(enhanced version = 0x%x)\n",
1383 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 }
1385 } else if ((id & 0x1f) == 0x09) { /* CS4238B */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001386 chip->hardware = WSS_HW_CS4238B;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 switch (id >> 5) {
1388 case 5:
1389 case 6:
1390 case 7:
1391 break;
1392 default:
Takashi Iwai76d498e2009-02-05 15:45:05 +01001393 snd_printk(KERN_WARNING
1394 "unknown CS4238B chip "
1395 "(enhanced version = 0x%x)\n",
1396 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398 } else if ((id & 0x1f) == 0x1e) { /* CS4239 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001399 chip->hardware = WSS_HW_CS4239;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 switch (id >> 5) {
1401 case 4:
1402 case 5:
1403 case 6:
1404 break;
1405 default:
Takashi Iwai76d498e2009-02-05 15:45:05 +01001406 snd_printk(KERN_WARNING
1407 "unknown CS4239 chip "
1408 "(enhanced version = 0x%x)\n",
1409 id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 } else {
Takashi Iwai76d498e2009-02-05 15:45:05 +01001412 snd_printk(KERN_WARNING
1413 "unknown CS4236/CS423xB chip "
1414 "(enhanced version = 0x%x)\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416 }
1417 }
1418 return 0; /* all things are ok.. */
1419}
1420
1421/*
1422
1423 */
1424
Bhumika Goyalaec54652017-08-17 14:45:52 +05301425static const struct snd_pcm_hardware snd_wss_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1428 SNDRV_PCM_INFO_MMAP_VALID |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 SNDRV_PCM_INFO_SYNC_START),
1430 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1431 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1432 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1433 .rate_min = 5510,
1434 .rate_max = 48000,
1435 .channels_min = 1,
1436 .channels_max = 2,
1437 .buffer_bytes_max = (128*1024),
1438 .period_bytes_min = 64,
1439 .period_bytes_max = (128*1024),
1440 .periods_min = 1,
1441 .periods_max = 1024,
1442 .fifo_size = 0,
1443};
1444
Bhumika Goyalaec54652017-08-17 14:45:52 +05301445static const struct snd_pcm_hardware snd_wss_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
1447 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
1448 SNDRV_PCM_INFO_MMAP_VALID |
1449 SNDRV_PCM_INFO_RESUME |
1450 SNDRV_PCM_INFO_SYNC_START),
1451 .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | SNDRV_PCM_FMTBIT_IMA_ADPCM |
1452 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE),
1453 .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000,
1454 .rate_min = 5510,
1455 .rate_max = 48000,
1456 .channels_min = 1,
1457 .channels_max = 2,
1458 .buffer_bytes_max = (128*1024),
1459 .period_bytes_min = 64,
1460 .period_bytes_max = (128*1024),
1461 .periods_min = 1,
1462 .periods_max = 1024,
1463 .fifo_size = 0,
1464};
1465
1466/*
1467
1468 */
1469
Krzysztof Helt7779f752008-07-31 21:03:41 +02001470static int snd_wss_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001472 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001473 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 int err;
1475
Krzysztof Helt7779f752008-07-31 21:03:41 +02001476 runtime->hw = snd_wss_playback;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Krzysztof Heltead893c2008-07-31 21:09:32 +02001478 /* hardware limitation of older chipsets */
1479 if (chip->hardware & WSS_HW_AD1848_MASK)
1480 runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
1481 SNDRV_PCM_FMTBIT_S16_BE);
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* hardware bug in InterWave chipset */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001484 if (chip->hardware == WSS_HW_INTERWAVE && chip->dma1 > 3)
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001485 runtime->hw.formats &= ~SNDRV_PCM_FMTBIT_MU_LAW;
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001488 if (chip->hardware == WSS_HW_CS4235 ||
1489 chip->hardware == WSS_HW_CS4239)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE;
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.buffer_bytes_max);
1493 snd_pcm_limit_isa_dma_size(chip->dma1, &runtime->hw.period_bytes_max);
1494
1495 if (chip->claim_dma) {
Takashi Iwaieb767942021-06-08 16:04:48 +02001496 err = chip->claim_dma(chip, chip->dma_private_data, chip->dma1);
1497 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return err;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Krzysztof Helt7779f752008-07-31 21:03:41 +02001501 err = snd_wss_open(chip, WSS_MODE_PLAY);
1502 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (chip->release_dma)
1504 chip->release_dma(chip, chip->dma_private_data, chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return err;
1506 }
1507 chip->playback_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 snd_pcm_set_sync(substream);
1509 chip->rate_constraint(runtime);
1510 return 0;
1511}
1512
Krzysztof Helt7779f752008-07-31 21:03:41 +02001513static int snd_wss_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001515 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001516 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int err;
1518
Krzysztof Helt7779f752008-07-31 21:03:41 +02001519 runtime->hw = snd_wss_capture;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Krzysztof Heltead893c2008-07-31 21:09:32 +02001521 /* hardware limitation of older chipsets */
1522 if (chip->hardware & WSS_HW_AD1848_MASK)
1523 runtime->hw.formats &= ~(SNDRV_PCM_FMTBIT_IMA_ADPCM |
1524 SNDRV_PCM_FMTBIT_S16_BE);
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 /* hardware limitation of cheap chips */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001527 if (chip->hardware == WSS_HW_CS4235 ||
Krzysztof Helt31eca302008-07-31 21:11:46 +02001528 chip->hardware == WSS_HW_CS4239 ||
1529 chip->hardware == WSS_HW_OPTI93X)
1530 runtime->hw.formats = SNDRV_PCM_FMTBIT_U8 |
1531 SNDRV_PCM_FMTBIT_S16_LE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.buffer_bytes_max);
1534 snd_pcm_limit_isa_dma_size(chip->dma2, &runtime->hw.period_bytes_max);
1535
1536 if (chip->claim_dma) {
Takashi Iwaieb767942021-06-08 16:04:48 +02001537 err = chip->claim_dma(chip, chip->dma_private_data, chip->dma2);
1538 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return err;
1540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Krzysztof Helt7779f752008-07-31 21:03:41 +02001542 err = snd_wss_open(chip, WSS_MODE_RECORD);
1543 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (chip->release_dma)
1545 chip->release_dma(chip, chip->dma_private_data, chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return err;
1547 }
1548 chip->capture_substream = substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 snd_pcm_set_sync(substream);
1550 chip->rate_constraint(runtime);
1551 return 0;
1552}
1553
Krzysztof Helt7779f752008-07-31 21:03:41 +02001554static int snd_wss_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001556 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 chip->playback_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001559 snd_wss_close(chip, WSS_MODE_PLAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
1561}
1562
Krzysztof Helt7779f752008-07-31 21:03:41 +02001563static int snd_wss_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001565 struct snd_wss *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 chip->capture_substream = NULL;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001568 snd_wss_close(chip, WSS_MODE_RECORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 return 0;
1570}
1571
Krzysztof Heltead893c2008-07-31 21:09:32 +02001572static void snd_wss_thinkpad_twiddle(struct snd_wss *chip, int on)
1573{
1574 int tmp;
1575
1576 if (!chip->thinkpad_flag)
1577 return;
1578
1579 outb(0x1c, AD1848_THINKPAD_CTL_PORT1);
1580 tmp = inb(AD1848_THINKPAD_CTL_PORT2);
1581
1582 if (on)
1583 /* turn it on */
1584 tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT;
1585 else
1586 /* turn it off */
1587 tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT;
1588
1589 outb(tmp, AD1848_THINKPAD_CTL_PORT2);
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592#ifdef CONFIG_PM
1593
1594/* lowlevel suspend callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001595static void snd_wss_suspend(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 int reg;
1598 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 spin_lock_irqsave(&chip->reg_lock, flags);
1601 for (reg = 0; reg < 32; reg++)
Krzysztof Helt7779f752008-07-31 21:03:41 +02001602 chip->image[reg] = snd_wss_in(chip, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 spin_unlock_irqrestore(&chip->reg_lock, flags);
Krzysztof Heltead893c2008-07-31 21:09:32 +02001604 if (chip->thinkpad_flag)
1605 snd_wss_thinkpad_twiddle(chip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608/* lowlevel resume callback for CS4231 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001609static void snd_wss_resume(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 int reg;
1612 unsigned long flags;
Takashi Iwaia2c855b2005-11-18 18:52:39 +01001613 /* int timeout; */
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001614
Krzysztof Heltead893c2008-07-31 21:09:32 +02001615 if (chip->thinkpad_flag)
1616 snd_wss_thinkpad_twiddle(chip, 1);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001617 snd_wss_mce_up(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 spin_lock_irqsave(&chip->reg_lock, flags);
1619 for (reg = 0; reg < 32; reg++) {
1620 switch (reg) {
1621 case CS4231_VERSION:
1622 break;
1623 default:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001624 snd_wss_out(chip, reg, chip->image[reg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 break;
1626 }
1627 }
Ondrej Zaryb4e2a162012-07-16 08:30:18 +02001628 /* Yamaha needs this to resume properly */
1629 if (chip->hardware == WSS_HW_OPL3SA2)
1630 snd_wss_out(chip, CS4231_PLAYBK_FORMAT,
1631 chip->image[CS4231_PLAYBK_FORMAT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 spin_unlock_irqrestore(&chip->reg_lock, flags);
Takashi Iwaifa55f832005-11-17 17:48:30 +01001633#if 1
Krzysztof Helt7779f752008-07-31 21:03:41 +02001634 snd_wss_mce_down(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635#else
1636 /* The following is a workaround to avoid freeze after resume on TP600E.
Krzysztof Helt7779f752008-07-31 21:03:41 +02001637 This is the first half of copy of snd_wss_mce_down(), but doesn't
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 include rescheduling. -- iwai
1639 */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001640 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 spin_lock_irqsave(&chip->reg_lock, flags);
1642 chip->mce_bit &= ~CS4231_MCE;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001643 timeout = wss_inb(chip, CS4231P(REGSEL));
1644 wss_outb(chip, CS4231P(REGSEL), chip->mce_bit | (timeout & 0x1f));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 spin_unlock_irqrestore(&chip->reg_lock, flags);
1646 if (timeout == 0x80)
Takashi Iwai76d498e2009-02-05 15:45:05 +01001647 snd_printk(KERN_ERR "down [0x%lx]: serious init problem "
1648 "- codec still busy\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if ((timeout & CS4231_MCE) == 0 ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001650 !(chip->hardware & (WSS_HW_CS4231_MASK | WSS_HW_CS4232_MASK))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return;
1652 }
Krzysztof Helt7779f752008-07-31 21:03:41 +02001653 snd_wss_busy_wait(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654#endif
1655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656#endif /* CONFIG_PM */
1657
Krzysztof Helt7779f752008-07-31 21:03:41 +02001658const char *snd_wss_chip_id(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
1660 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001661 case WSS_HW_CS4231:
1662 return "CS4231";
1663 case WSS_HW_CS4231A:
1664 return "CS4231A";
1665 case WSS_HW_CS4232:
1666 return "CS4232";
1667 case WSS_HW_CS4232A:
1668 return "CS4232A";
1669 case WSS_HW_CS4235:
1670 return "CS4235";
1671 case WSS_HW_CS4236:
1672 return "CS4236";
1673 case WSS_HW_CS4236B:
1674 return "CS4236B";
1675 case WSS_HW_CS4237B:
1676 return "CS4237B";
1677 case WSS_HW_CS4238B:
1678 return "CS4238B";
1679 case WSS_HW_CS4239:
1680 return "CS4239";
1681 case WSS_HW_INTERWAVE:
1682 return "AMD InterWave";
1683 case WSS_HW_OPL3SA2:
1684 return chip->card->shortname;
1685 case WSS_HW_AD1845:
1686 return "AD1845";
1687 case WSS_HW_OPTI93X:
1688 return "OPTi 93x";
Krzysztof Heltead893c2008-07-31 21:09:32 +02001689 case WSS_HW_AD1847:
1690 return "AD1847";
1691 case WSS_HW_AD1848:
1692 return "AD1848";
1693 case WSS_HW_CS4248:
1694 return "CS4248";
1695 case WSS_HW_CMI8330:
1696 return "CMI8330/C3D";
Krzysztof Helt7779f752008-07-31 21:03:41 +02001697 default:
1698 return "???";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001701EXPORT_SYMBOL(snd_wss_chip_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Krzysztof Helt7779f752008-07-31 21:03:41 +02001703static int snd_wss_new(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 unsigned short hardware,
1705 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001706 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001708 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 *rchip = NULL;
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001711 chip = devm_kzalloc(card->dev, sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (chip == NULL)
1713 return -ENOMEM;
1714 chip->hardware = hardware;
1715 chip->hwshare = hwshare;
1716
1717 spin_lock_init(&chip->reg_lock);
Ingo Molnar8b7547f2006-01-16 16:33:08 +01001718 mutex_init(&chip->mce_mutex);
1719 mutex_init(&chip->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 chip->card = card;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001721 chip->rate_constraint = snd_wss_xrate;
1722 chip->set_playback_format = snd_wss_playback_format;
1723 chip->set_capture_format = snd_wss_capture_format;
1724 if (chip->hardware == WSS_HW_OPTI93X)
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001725 memcpy(&chip->image, &snd_opti93x_original_image,
1726 sizeof(snd_opti93x_original_image));
1727 else
Krzysztof Helt7779f752008-07-31 21:03:41 +02001728 memcpy(&chip->image, &snd_wss_original_image,
1729 sizeof(snd_wss_original_image));
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001730 if (chip->hardware & WSS_HW_AD1848_MASK) {
1731 chip->image[CS4231_PIN_CTRL] = 0;
1732 chip->image[CS4231_TEST_INIT] = 0;
1733 }
Krzysztof Heltabf1f5a2008-06-09 23:07:28 +02001734
Krzysztof Helt7779f752008-07-31 21:03:41 +02001735 *rchip = chip;
1736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
Krzysztof Helt7779f752008-07-31 21:03:41 +02001739int snd_wss_create(struct snd_card *card,
1740 unsigned long port,
1741 unsigned long cport,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int irq, int dma1, int dma2,
1743 unsigned short hardware,
1744 unsigned short hwshare,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001745 struct snd_wss **rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001747 struct snd_wss *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int err;
1749
Krzysztof Helt7779f752008-07-31 21:03:41 +02001750 err = snd_wss_new(card, hardware, hwshare, &chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (err < 0)
1752 return err;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 chip->irq = -1;
1755 chip->dma1 = -1;
1756 chip->dma2 = -1;
1757
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001758 chip->res_port = devm_request_region(card->dev, port, 4, "WSS");
Krzysztof Helt7779f752008-07-31 21:03:41 +02001759 if (!chip->res_port) {
1760 snd_printk(KERN_ERR "wss: can't grab port 0x%lx\n", port);
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) {
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001765 chip->res_cport = devm_request_region(card->dev, cport, 8,
1766 "CS4232 Control");
Krzysztof Helt7779f752008-07-31 21:03:41 +02001767 if (!chip->res_cport) {
1768 snd_printk(KERN_ERR
1769 "wss: can't grab control port 0x%lx\n", cport);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001770 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))
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001775 if (devm_request_irq(card->dev, irq, snd_wss_interrupt, 0,
1776 "WSS", (void *) chip)) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001777 snd_printk(KERN_ERR "wss: can't grab IRQ %d\n", irq);
Krzysztof Helt7779f752008-07-31 21:03:41 +02001778 return -EBUSY;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 chip->irq = irq;
Takashi Iwai959d4c82019-12-10 07:34:47 +01001781 card->sync_irq = chip->irq;
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001782 if (!(hwshare & WSS_HWSHARE_DMA1) &&
1783 snd_devm_request_dma(card->dev, dma1, "WSS - 1")) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001784 snd_printk(KERN_ERR "wss: can't grab DMA1 %d\n", dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return -EBUSY;
1786 }
1787 chip->dma1 = dma1;
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001788 if (!(hwshare & WSS_HWSHARE_DMA2) && dma1 != dma2 && dma2 >= 0 &&
1789 snd_devm_request_dma(card->dev, dma2, "WSS - 2")) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001790 snd_printk(KERN_ERR "wss: can't grab DMA2 %d\n", dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -EBUSY;
1792 }
1793 if (dma1 == dma2 || dma2 < 0) {
1794 chip->single_dma = 1;
1795 chip->dma2 = chip->dma1;
1796 } else
1797 chip->dma2 = dma2;
1798
Krzysztof Helt760fc6b82008-07-31 21:10:47 +02001799 if (hardware == WSS_HW_THINKPAD) {
1800 chip->thinkpad_flag = 1;
1801 chip->hardware = WSS_HW_DETECT; /* reset */
1802 snd_wss_thinkpad_twiddle(chip, 1);
1803 }
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 /* global setup */
Takashi Iwaiea2bfa22021-07-15 09:59:15 +02001806 if (snd_wss_probe(chip) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return -ENODEV;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001808 snd_wss_init(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Takashi Iwaia9824c82005-11-17 17:51:00 +01001810#if 0
Krzysztof Helt7779f752008-07-31 21:03:41 +02001811 if (chip->hardware & WSS_HW_CS4232_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (chip->res_cport == NULL)
Takashi Iwai76d498e2009-02-05 15:45:05 +01001813 snd_printk(KERN_ERR "CS4232 control port features are "
1814 "not accessible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
Takashi Iwaia9824c82005-11-17 17:51:00 +01001816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818#ifdef CONFIG_PM
1819 /* Power Management */
Krzysztof Helt7779f752008-07-31 21:03:41 +02001820 chip->suspend = snd_wss_suspend;
1821 chip->resume = snd_wss_resume;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822#endif
1823
1824 *rchip = chip;
1825 return 0;
1826}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001827EXPORT_SYMBOL(snd_wss_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Arvind Yadavaa5ebc72017-08-11 17:28:07 +05301829static const struct snd_pcm_ops snd_wss_playback_ops = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001830 .open = snd_wss_playback_open,
1831 .close = snd_wss_playback_close,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001832 .hw_params = snd_wss_playback_hw_params,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001833 .prepare = snd_wss_playback_prepare,
1834 .trigger = snd_wss_trigger,
1835 .pointer = snd_wss_playback_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
1837
Arvind Yadavaa5ebc72017-08-11 17:28:07 +05301838static const struct snd_pcm_ops snd_wss_capture_ops = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001839 .open = snd_wss_capture_open,
1840 .close = snd_wss_capture_close,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001841 .hw_params = snd_wss_capture_hw_params,
Krzysztof Helt7779f752008-07-31 21:03:41 +02001842 .prepare = snd_wss_capture_prepare,
1843 .trigger = snd_wss_trigger,
1844 .pointer = snd_wss_capture_pointer,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845};
1846
Lars-Peter Clausenfa60c062015-01-02 12:24:43 +01001847int snd_wss_pcm(struct snd_wss *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001849 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int err;
1851
Krzysztof Heltead893c2008-07-31 21:09:32 +02001852 err = snd_pcm_new(chip->card, "WSS", device, 1, 1, &pcm);
1853 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return err;
1855
Krzysztof Helt7779f752008-07-31 21:03:41 +02001856 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_wss_playback_ops);
1857 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_wss_capture_ops);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* global setup */
1860 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 pcm->info_flags = 0;
1862 if (chip->single_dma)
1863 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001864 if (chip->hardware != WSS_HW_INTERWAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001866 strcpy(pcm->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Takashi Iwai15fbacfe2019-12-09 10:48:48 +01001868 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, chip->card->dev,
1869 64*1024, chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 chip->pcm = pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 return 0;
1873}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001874EXPORT_SYMBOL(snd_wss_pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Krzysztof Helt7779f752008-07-31 21:03:41 +02001876static void snd_wss_timer_free(struct snd_timer *timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001878 struct snd_wss *chip = timer->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 chip->timer = NULL;
1880}
1881
Lars-Peter Clausenfa60c062015-01-02 12:24:43 +01001882int snd_wss_timer(struct snd_wss *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01001884 struct snd_timer *timer;
1885 struct snd_timer_id tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 int err;
1887
1888 /* Timer initialization */
1889 tid.dev_class = SNDRV_TIMER_CLASS_CARD;
1890 tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
1891 tid.card = chip->card->number;
1892 tid.device = device;
1893 tid.subdevice = 0;
Takashi Iwaieb767942021-06-08 16:04:48 +02001894 err = snd_timer_new(chip->card, "CS4231", &tid, &timer);
1895 if (err < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return err;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001897 strcpy(timer->name, snd_wss_chip_id(chip));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 timer->private_data = chip;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001899 timer->private_free = snd_wss_timer_free;
1900 timer->hw = snd_wss_timer_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 chip->timer = timer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 return 0;
1903}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001904EXPORT_SYMBOL(snd_wss_timer);
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906/*
1907 * MIXER part
1908 */
1909
Krzysztof Helt7779f752008-07-31 21:03:41 +02001910static int snd_wss_info_mux(struct snd_kcontrol *kcontrol,
1911 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Takashi Iwai0773efa2014-10-20 18:15:06 +02001913 static const char * const texts[4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 "Line", "Aux", "Mic", "Mix"
1915 };
Takashi Iwai0773efa2014-10-20 18:15:06 +02001916 static const char * const opl3sa_texts[4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 "Line", "CD", "Mic", "Mix"
1918 };
Takashi Iwai0773efa2014-10-20 18:15:06 +02001919 static const char * const gusmax_texts[4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 "Line", "Synth", "Mic", "Mix"
1921 };
Takashi Iwai0773efa2014-10-20 18:15:06 +02001922 const char * const *ptexts = texts;
Krzysztof Helt7779f752008-07-31 21:03:41 +02001923 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Takashi Iwai622207d2008-08-08 17:11:45 +02001925 if (snd_BUG_ON(!chip->card))
1926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (!strcmp(chip->card->driver, "GUS MAX"))
1928 ptexts = gusmax_texts;
1929 switch (chip->hardware) {
Krzysztof Helt7779f752008-07-31 21:03:41 +02001930 case WSS_HW_INTERWAVE:
1931 ptexts = gusmax_texts;
1932 break;
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +01001933 case WSS_HW_OPTI93X:
Krzysztof Helt7779f752008-07-31 21:03:41 +02001934 case WSS_HW_OPL3SA2:
1935 ptexts = opl3sa_texts;
1936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
Takashi Iwai0773efa2014-10-20 18:15:06 +02001938 return snd_ctl_enum_info(uinfo, 2, 4, ptexts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Krzysztof Helt7779f752008-07-31 21:03:41 +02001941static int snd_wss_get_mux(struct snd_kcontrol *kcontrol,
1942 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001944 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 unsigned long flags;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 spin_lock_irqsave(&chip->reg_lock, flags);
1948 ucontrol->value.enumerated.item[0] = (chip->image[CS4231_LEFT_INPUT] & CS4231_MIXS_ALL) >> 6;
1949 ucontrol->value.enumerated.item[1] = (chip->image[CS4231_RIGHT_INPUT] & CS4231_MIXS_ALL) >> 6;
1950 spin_unlock_irqrestore(&chip->reg_lock, flags);
1951 return 0;
1952}
1953
Krzysztof Helt7779f752008-07-31 21:03:41 +02001954static int snd_wss_put_mux(struct snd_kcontrol *kcontrol,
1955 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001957 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 unsigned long flags;
1959 unsigned short left, right;
1960 int change;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02001961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (ucontrol->value.enumerated.item[0] > 3 ||
1963 ucontrol->value.enumerated.item[1] > 3)
1964 return -EINVAL;
1965 left = ucontrol->value.enumerated.item[0] << 6;
1966 right = ucontrol->value.enumerated.item[1] << 6;
1967 spin_lock_irqsave(&chip->reg_lock, flags);
1968 left = (chip->image[CS4231_LEFT_INPUT] & ~CS4231_MIXS_ALL) | left;
1969 right = (chip->image[CS4231_RIGHT_INPUT] & ~CS4231_MIXS_ALL) | right;
1970 change = left != chip->image[CS4231_LEFT_INPUT] ||
Krzysztof Helt7779f752008-07-31 21:03:41 +02001971 right != chip->image[CS4231_RIGHT_INPUT];
1972 snd_wss_out(chip, CS4231_LEFT_INPUT, left);
1973 snd_wss_out(chip, CS4231_RIGHT_INPUT, right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 spin_unlock_irqrestore(&chip->reg_lock, flags);
1975 return change;
1976}
1977
Krzysztof Helt7779f752008-07-31 21:03:41 +02001978int snd_wss_info_single(struct snd_kcontrol *kcontrol,
1979 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
1981 int mask = (kcontrol->private_value >> 16) & 0xff;
1982
1983 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1984 uinfo->count = 1;
1985 uinfo->value.integer.min = 0;
1986 uinfo->value.integer.max = mask;
1987 return 0;
1988}
Krzysztof Helt7779f752008-07-31 21:03:41 +02001989EXPORT_SYMBOL(snd_wss_info_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Krzysztof Helt7779f752008-07-31 21:03:41 +02001991int snd_wss_get_single(struct snd_kcontrol *kcontrol,
1992 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993{
Krzysztof Helt7779f752008-07-31 21:03:41 +02001994 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 unsigned long flags;
1996 int reg = kcontrol->private_value & 0xff;
1997 int shift = (kcontrol->private_value >> 8) & 0xff;
1998 int mask = (kcontrol->private_value >> 16) & 0xff;
1999 int invert = (kcontrol->private_value >> 24) & 0xff;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 spin_lock_irqsave(&chip->reg_lock, flags);
2002 ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask;
2003 spin_unlock_irqrestore(&chip->reg_lock, flags);
2004 if (invert)
2005 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
2006 return 0;
2007}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002008EXPORT_SYMBOL(snd_wss_get_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Krzysztof Helt7779f752008-07-31 21:03:41 +02002010int snd_wss_put_single(struct snd_kcontrol *kcontrol,
2011 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002013 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 unsigned long flags;
2015 int reg = kcontrol->private_value & 0xff;
2016 int shift = (kcontrol->private_value >> 8) & 0xff;
2017 int mask = (kcontrol->private_value >> 16) & 0xff;
2018 int invert = (kcontrol->private_value >> 24) & 0xff;
2019 int change;
2020 unsigned short val;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 val = (ucontrol->value.integer.value[0] & mask);
2023 if (invert)
2024 val = mask - val;
2025 val <<= shift;
2026 spin_lock_irqsave(&chip->reg_lock, flags);
2027 val = (chip->image[reg] & ~(mask << shift)) | val;
2028 change = val != chip->image[reg];
Krzysztof Helt7779f752008-07-31 21:03:41 +02002029 snd_wss_out(chip, reg, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 spin_unlock_irqrestore(&chip->reg_lock, flags);
2031 return change;
2032}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002033EXPORT_SYMBOL(snd_wss_put_single);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Krzysztof Helt7779f752008-07-31 21:03:41 +02002035int snd_wss_info_double(struct snd_kcontrol *kcontrol,
2036 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 int mask = (kcontrol->private_value >> 24) & 0xff;
2039
2040 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
2041 uinfo->count = 2;
2042 uinfo->value.integer.min = 0;
2043 uinfo->value.integer.max = mask;
2044 return 0;
2045}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002046EXPORT_SYMBOL(snd_wss_info_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Krzysztof Helt7779f752008-07-31 21:03:41 +02002048int snd_wss_get_double(struct snd_kcontrol *kcontrol,
2049 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002051 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 unsigned long flags;
2053 int left_reg = kcontrol->private_value & 0xff;
2054 int right_reg = (kcontrol->private_value >> 8) & 0xff;
2055 int shift_left = (kcontrol->private_value >> 16) & 0x07;
2056 int shift_right = (kcontrol->private_value >> 19) & 0x07;
2057 int mask = (kcontrol->private_value >> 24) & 0xff;
2058 int invert = (kcontrol->private_value >> 22) & 1;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 spin_lock_irqsave(&chip->reg_lock, flags);
2061 ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask;
2062 ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask;
2063 spin_unlock_irqrestore(&chip->reg_lock, flags);
2064 if (invert) {
2065 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
2066 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
2067 }
2068 return 0;
2069}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002070EXPORT_SYMBOL(snd_wss_get_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Krzysztof Helt7779f752008-07-31 21:03:41 +02002072int snd_wss_put_double(struct snd_kcontrol *kcontrol,
2073 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Krzysztof Helt7779f752008-07-31 21:03:41 +02002075 struct snd_wss *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 unsigned long flags;
2077 int left_reg = kcontrol->private_value & 0xff;
2078 int right_reg = (kcontrol->private_value >> 8) & 0xff;
2079 int shift_left = (kcontrol->private_value >> 16) & 0x07;
2080 int shift_right = (kcontrol->private_value >> 19) & 0x07;
2081 int mask = (kcontrol->private_value >> 24) & 0xff;
2082 int invert = (kcontrol->private_value >> 22) & 1;
2083 int change;
2084 unsigned short val1, val2;
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 val1 = ucontrol->value.integer.value[0] & mask;
2087 val2 = ucontrol->value.integer.value[1] & mask;
2088 if (invert) {
2089 val1 = mask - val1;
2090 val2 = mask - val2;
2091 }
2092 val1 <<= shift_left;
2093 val2 <<= shift_right;
2094 spin_lock_irqsave(&chip->reg_lock, flags);
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002095 if (left_reg != right_reg) {
2096 val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1;
2097 val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2;
2098 change = val1 != chip->image[left_reg] ||
2099 val2 != chip->image[right_reg];
2100 snd_wss_out(chip, left_reg, val1);
2101 snd_wss_out(chip, right_reg, val2);
2102 } else {
2103 mask = (mask << shift_left) | (mask << shift_right);
2104 val1 = (chip->image[left_reg] & ~mask) | val1 | val2;
2105 change = val1 != chip->image[left_reg];
2106 snd_wss_out(chip, left_reg, val1);
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 spin_unlock_irqrestore(&chip->reg_lock, flags);
2109 return change;
2110}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002111EXPORT_SYMBOL(snd_wss_put_double);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002113static const DECLARE_TLV_DB_SCALE(db_scale_6bit, -9450, 150, 0);
2114static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
2115static const DECLARE_TLV_DB_SCALE(db_scale_rec_gain, 0, 150, 0);
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002116static const DECLARE_TLV_DB_SCALE(db_scale_4bit, -4500, 300, 0);
Krzysztof Helt5664daa2008-07-31 21:08:32 +02002117
Takashi Iwaifdd1f6f2020-01-03 09:16:51 +01002118static const struct snd_kcontrol_new snd_wss_controls[] = {
Krzysztof Helt7779f752008-07-31 21:03:41 +02002119WSS_DOUBLE("PCM Playback Switch", 0,
2120 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 7, 7, 1, 1),
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002121WSS_DOUBLE_TLV("PCM Playback Volume", 0,
2122 CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 63, 1,
2123 db_scale_6bit),
Krzysztof Helt7779f752008-07-31 21:03:41 +02002124WSS_DOUBLE("Aux Playback Switch", 0,
2125 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002126WSS_DOUBLE_TLV("Aux Playback Volume", 0,
2127 CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 0, 0, 31, 1,
2128 db_scale_5bit_12db_max),
Krzysztof Helt7779f752008-07-31 21:03:41 +02002129WSS_DOUBLE("Aux Playback Switch", 1,
2130 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 7, 7, 1, 1),
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002131WSS_DOUBLE_TLV("Aux Playback Volume", 1,
2132 CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 0, 0, 31, 1,
2133 db_scale_5bit_12db_max),
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002134WSS_DOUBLE_TLV("Capture Volume", 0, CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT,
2135 0, 0, 15, 0, db_scale_rec_gain),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2138 .name = "Capture Source",
Krzysztof Helt7779f752008-07-31 21:03:41 +02002139 .info = snd_wss_info_mux,
2140 .get = snd_wss_get_mux,
2141 .put = snd_wss_put_mux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142},
Krzysztof Heltb753e032009-11-17 18:34:54 +01002143WSS_DOUBLE("Mic Boost (+20dB)", 0,
Krzysztof Helt7779f752008-07-31 21:03:41 +02002144 CS4231_LEFT_INPUT, CS4231_RIGHT_INPUT, 5, 5, 1, 0),
2145WSS_SINGLE("Loopback Capture Switch", 0,
2146 CS4231_LOOPBACK, 0, 1, 0),
Krzysztof Heltabd134d2009-10-10 10:25:39 +02002147WSS_SINGLE_TLV("Loopback Capture Volume", 0, CS4231_LOOPBACK, 2, 63, 1,
2148 db_scale_6bit),
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002149WSS_DOUBLE("Line Playback Switch", 0,
2150 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
2151WSS_DOUBLE_TLV("Line Playback Volume", 0,
2152 CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 31, 1,
2153 db_scale_5bit_12db_max),
Krzysztof Heltb753e032009-11-17 18:34:54 +01002154WSS_SINGLE("Beep Playback Switch", 0,
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002155 CS4231_MONO_CTRL, 7, 1, 1),
Krzysztof Heltb753e032009-11-17 18:34:54 +01002156WSS_SINGLE_TLV("Beep Playback Volume", 0,
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002157 CS4231_MONO_CTRL, 0, 15, 1,
2158 db_scale_4bit),
2159WSS_SINGLE("Mono Output Playback Switch", 0,
2160 CS4231_MONO_CTRL, 6, 1, 1),
Krzysztof Heltb753e032009-11-17 18:34:54 +01002161WSS_SINGLE("Beep Bypass Playback Switch", 0,
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002162 CS4231_MONO_CTRL, 5, 1, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163};
Krzysztof Helt9295aea2008-07-31 21:00:17 +02002164
Krzysztof Helt7779f752008-07-31 21:03:41 +02002165int snd_wss_mixer(struct snd_wss *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Takashi Iwaiba2375a2005-11-17 14:30:42 +01002167 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 unsigned int idx;
2169 int err;
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +01002170 int count = ARRAY_SIZE(snd_wss_controls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Takashi Iwai622207d2008-08-08 17:11:45 +02002172 if (snd_BUG_ON(!chip || !chip->pcm))
2173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 card = chip->card;
2176
2177 strcpy(card->mixername, chip->pcm->name);
2178
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +01002179 /* Use only the first 11 entries on AD1848 */
2180 if (chip->hardware & WSS_HW_AD1848_MASK)
2181 count = 11;
2182 /* There is no loopback on OPTI93X */
2183 else if (chip->hardware == WSS_HW_OPTI93X)
2184 count = 9;
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002185
Krzysztof Heltb2e8d7d2009-12-10 20:40:18 +01002186 for (idx = 0; idx < count; idx++) {
2187 err = snd_ctl_add(card,
2188 snd_ctl_new1(&snd_wss_controls[idx],
2189 chip));
2190 if (err < 0)
2191 return err;
Krzysztof Helt633c7e92009-10-11 12:38:49 +02002192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194}
Krzysztof Helt7779f752008-07-31 21:03:41 +02002195EXPORT_SYMBOL(snd_wss_mixer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Krzysztof Heltead893c2008-07-31 21:09:32 +02002197const struct snd_pcm_ops *snd_wss_get_pcm_ops(int direction)
2198{
2199 return direction == SNDRV_PCM_STREAM_PLAYBACK ?
2200 &snd_wss_playback_ops : &snd_wss_capture_ops;
2201}
2202EXPORT_SYMBOL(snd_wss_get_pcm_ops);