]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> | |
3 | * Routines for control of AD1848/AD1847/CS4248 | |
4 | * | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #define SNDRV_MAIN_OBJECT_FILE | |
23 | #include <sound/driver.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/pm.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/ioport.h> | |
30 | #include <sound/core.h> | |
31 | #include <sound/ad1848.h> | |
32 | #include <sound/control.h> | |
33 | #include <sound/pcm_params.h> | |
34 | ||
35 | #include <asm/io.h> | |
36 | #include <asm/dma.h> | |
37 | ||
38 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); | |
39 | MODULE_DESCRIPTION("Routines for control of AD1848/AD1847/CS4248"); | |
40 | MODULE_LICENSE("GPL"); | |
41 | ||
42 | #if 0 | |
43 | #define SNDRV_DEBUG_MCE | |
44 | #endif | |
45 | ||
46 | /* | |
47 | * Some variables | |
48 | */ | |
49 | ||
50 | static unsigned char freq_bits[14] = { | |
51 | /* 5510 */ 0x00 | AD1848_XTAL2, | |
52 | /* 6620 */ 0x0E | AD1848_XTAL2, | |
53 | /* 8000 */ 0x00 | AD1848_XTAL1, | |
54 | /* 9600 */ 0x0E | AD1848_XTAL1, | |
55 | /* 11025 */ 0x02 | AD1848_XTAL2, | |
56 | /* 16000 */ 0x02 | AD1848_XTAL1, | |
57 | /* 18900 */ 0x04 | AD1848_XTAL2, | |
58 | /* 22050 */ 0x06 | AD1848_XTAL2, | |
59 | /* 27042 */ 0x04 | AD1848_XTAL1, | |
60 | /* 32000 */ 0x06 | AD1848_XTAL1, | |
61 | /* 33075 */ 0x0C | AD1848_XTAL2, | |
62 | /* 37800 */ 0x08 | AD1848_XTAL2, | |
63 | /* 44100 */ 0x0A | AD1848_XTAL2, | |
64 | /* 48000 */ 0x0C | AD1848_XTAL1 | |
65 | }; | |
66 | ||
67 | static unsigned int rates[14] = { | |
68 | 5510, 6620, 8000, 9600, 11025, 16000, 18900, 22050, | |
69 | 27042, 32000, 33075, 37800, 44100, 48000 | |
70 | }; | |
71 | ||
72 | static snd_pcm_hw_constraint_list_t hw_constraints_rates = { | |
73 | .count = 14, | |
74 | .list = rates, | |
75 | .mask = 0, | |
76 | }; | |
77 | ||
78 | static unsigned char snd_ad1848_original_image[16] = | |
79 | { | |
80 | 0x00, /* 00 - lic */ | |
81 | 0x00, /* 01 - ric */ | |
82 | 0x9f, /* 02 - la1ic */ | |
83 | 0x9f, /* 03 - ra1ic */ | |
84 | 0x9f, /* 04 - la2ic */ | |
85 | 0x9f, /* 05 - ra2ic */ | |
86 | 0xbf, /* 06 - loc */ | |
87 | 0xbf, /* 07 - roc */ | |
88 | 0x20, /* 08 - dfr */ | |
89 | AD1848_AUTOCALIB, /* 09 - ic */ | |
90 | 0x00, /* 0a - pc */ | |
91 | 0x00, /* 0b - ti */ | |
92 | 0x00, /* 0c - mi */ | |
93 | 0x00, /* 0d - lbc */ | |
94 | 0x00, /* 0e - dru */ | |
95 | 0x00, /* 0f - drl */ | |
96 | }; | |
97 | ||
98 | /* | |
99 | * Basic I/O functions | |
100 | */ | |
101 | ||
102 | void snd_ad1848_out(ad1848_t *chip, | |
103 | unsigned char reg, | |
104 | unsigned char value) | |
105 | { | |
106 | int timeout; | |
107 | ||
108 | for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--) | |
109 | udelay(100); | |
110 | #ifdef CONFIG_SND_DEBUG | |
111 | if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) | |
112 | snd_printk("auto calibration time out - reg = 0x%x, value = 0x%x\n", reg, value); | |
113 | #endif | |
114 | outb(chip->mce_bit | reg, AD1848P(chip, REGSEL)); | |
115 | outb(chip->image[reg] = value, AD1848P(chip, REG)); | |
116 | mb(); | |
117 | #if 0 | |
118 | printk("codec out - reg 0x%x = 0x%x\n", chip->mce_bit | reg, value); | |
119 | #endif | |
120 | } | |
121 | ||
122 | static void snd_ad1848_dout(ad1848_t *chip, | |
123 | unsigned char reg, unsigned char value) | |
124 | { | |
125 | int timeout; | |
126 | ||
127 | for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--) | |
128 | udelay(100); | |
129 | outb(chip->mce_bit | reg, AD1848P(chip, REGSEL)); | |
130 | outb(value, AD1848P(chip, REG)); | |
131 | mb(); | |
132 | } | |
133 | ||
134 | static unsigned char snd_ad1848_in(ad1848_t *chip, unsigned char reg) | |
135 | { | |
136 | int timeout; | |
137 | ||
138 | for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--) | |
139 | udelay(100); | |
140 | #ifdef CONFIG_SND_DEBUG | |
141 | if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) | |
142 | snd_printk("auto calibration time out - reg = 0x%x\n", reg); | |
143 | #endif | |
144 | outb(chip->mce_bit | reg, AD1848P(chip, REGSEL)); | |
145 | mb(); | |
146 | return inb(AD1848P(chip, REG)); | |
147 | } | |
148 | ||
149 | #if 0 | |
150 | ||
151 | static void snd_ad1848_debug(ad1848_t *chip) | |
152 | { | |
153 | printk("AD1848 REGS: INDEX = 0x%02x ", inb(AD1848P(chip, REGSEL))); | |
154 | printk(" STATUS = 0x%02x\n", inb(AD1848P(chip, STATUS))); | |
155 | printk(" 0x00: left input = 0x%02x ", snd_ad1848_in(chip, 0x00)); | |
156 | printk(" 0x08: playback format = 0x%02x\n", snd_ad1848_in(chip, 0x08)); | |
157 | printk(" 0x01: right input = 0x%02x ", snd_ad1848_in(chip, 0x01)); | |
158 | printk(" 0x09: iface (CFIG 1) = 0x%02x\n", snd_ad1848_in(chip, 0x09)); | |
159 | printk(" 0x02: AUXA left = 0x%02x ", snd_ad1848_in(chip, 0x02)); | |
160 | printk(" 0x0a: pin control = 0x%02x\n", snd_ad1848_in(chip, 0x0a)); | |
161 | printk(" 0x03: AUXA right = 0x%02x ", snd_ad1848_in(chip, 0x03)); | |
162 | printk(" 0x0b: init & status = 0x%02x\n", snd_ad1848_in(chip, 0x0b)); | |
163 | printk(" 0x04: AUXB left = 0x%02x ", snd_ad1848_in(chip, 0x04)); | |
164 | printk(" 0x0c: revision & mode = 0x%02x\n", snd_ad1848_in(chip, 0x0c)); | |
165 | printk(" 0x05: AUXB right = 0x%02x ", snd_ad1848_in(chip, 0x05)); | |
166 | printk(" 0x0d: loopback = 0x%02x\n", snd_ad1848_in(chip, 0x0d)); | |
167 | printk(" 0x06: left output = 0x%02x ", snd_ad1848_in(chip, 0x06)); | |
168 | printk(" 0x0e: data upr count = 0x%02x\n", snd_ad1848_in(chip, 0x0e)); | |
169 | printk(" 0x07: right output = 0x%02x ", snd_ad1848_in(chip, 0x07)); | |
170 | printk(" 0x0f: data lwr count = 0x%02x\n", snd_ad1848_in(chip, 0x0f)); | |
171 | } | |
172 | ||
173 | #endif | |
174 | ||
175 | /* | |
176 | * AD1848 detection / MCE routines | |
177 | */ | |
178 | ||
179 | static void snd_ad1848_mce_up(ad1848_t *chip) | |
180 | { | |
181 | unsigned long flags; | |
182 | int timeout; | |
183 | ||
184 | for (timeout = 250; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--) | |
185 | udelay(100); | |
186 | #ifdef CONFIG_SND_DEBUG | |
187 | if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) | |
188 | snd_printk("mce_up - auto calibration time out (0)\n"); | |
189 | #endif | |
190 | spin_lock_irqsave(&chip->reg_lock, flags); | |
191 | chip->mce_bit |= AD1848_MCE; | |
192 | timeout = inb(AD1848P(chip, REGSEL)); | |
193 | if (timeout == 0x80) | |
194 | snd_printk("mce_up [0x%lx]: serious init problem - codec still busy\n", chip->port); | |
195 | if (!(timeout & AD1848_MCE)) | |
196 | outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL)); | |
197 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
198 | } | |
199 | ||
200 | static void snd_ad1848_mce_down(ad1848_t *chip) | |
201 | { | |
202 | unsigned long flags; | |
203 | int timeout; | |
204 | signed long time; | |
205 | ||
206 | spin_lock_irqsave(&chip->reg_lock, flags); | |
207 | for (timeout = 5; timeout > 0; timeout--) | |
208 | inb(AD1848P(chip, REGSEL)); | |
209 | /* end of cleanup sequence */ | |
210 | for (timeout = 12000; timeout > 0 && (inb(AD1848P(chip, REGSEL)) & AD1848_INIT); timeout--) | |
211 | udelay(100); | |
212 | #if 0 | |
213 | printk("(1) timeout = %i\n", timeout); | |
214 | #endif | |
215 | #ifdef CONFIG_SND_DEBUG | |
216 | if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) | |
217 | snd_printk("mce_down [0x%lx] - auto calibration time out (0)\n", AD1848P(chip, REGSEL)); | |
218 | #endif | |
219 | chip->mce_bit &= ~AD1848_MCE; | |
220 | timeout = inb(AD1848P(chip, REGSEL)); | |
221 | outb(chip->mce_bit | (timeout & 0x1f), AD1848P(chip, REGSEL)); | |
222 | if (timeout == 0x80) | |
223 | snd_printk("mce_down [0x%lx]: serious init problem - codec still busy\n", chip->port); | |
224 | if ((timeout & AD1848_MCE) == 0) { | |
225 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
226 | return; | |
227 | } | |
228 | /* calibration process */ | |
229 | ||
230 | for (timeout = 500; timeout > 0 && (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0; timeout--); | |
231 | if ((snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) == 0) { | |
232 | snd_printd("mce_down - auto calibration time out (1)\n"); | |
233 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
234 | return; | |
235 | } | |
236 | #if 0 | |
237 | printk("(2) timeout = %i, jiffies = %li\n", timeout, jiffies); | |
238 | #endif | |
239 | time = HZ / 4; | |
240 | while (snd_ad1848_in(chip, AD1848_TEST_INIT) & AD1848_CALIB_IN_PROGRESS) { | |
241 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
242 | if (time <= 0) { | |
243 | snd_printk("mce_down - auto calibration time out (2)\n"); | |
244 | return; | |
245 | } | |
246 | set_current_state(TASK_INTERRUPTIBLE); | |
247 | time = schedule_timeout(time); | |
248 | spin_lock_irqsave(&chip->reg_lock, flags); | |
249 | } | |
250 | #if 0 | |
251 | printk("(3) jiffies = %li\n", jiffies); | |
252 | #endif | |
253 | time = HZ / 10; | |
254 | while (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) { | |
255 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
256 | if (time <= 0) { | |
257 | snd_printk("mce_down - auto calibration time out (3)\n"); | |
258 | return; | |
259 | } | |
260 | set_current_state(TASK_INTERRUPTIBLE); | |
261 | time = schedule_timeout(time); | |
262 | spin_lock_irqsave(&chip->reg_lock, flags); | |
263 | } | |
264 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
265 | #if 0 | |
266 | printk("(4) jiffies = %li\n", jiffies); | |
267 | snd_printk("mce_down - exit = 0x%x\n", inb(AD1848P(chip, REGSEL))); | |
268 | #endif | |
269 | } | |
270 | ||
271 | static unsigned int snd_ad1848_get_count(unsigned char format, | |
272 | unsigned int size) | |
273 | { | |
274 | switch (format & 0xe0) { | |
275 | case AD1848_LINEAR_16: | |
276 | size >>= 1; | |
277 | break; | |
278 | } | |
279 | if (format & AD1848_STEREO) | |
280 | size >>= 1; | |
281 | return size; | |
282 | } | |
283 | ||
284 | static int snd_ad1848_trigger(ad1848_t *chip, unsigned char what, | |
285 | int channel, int cmd) | |
286 | { | |
287 | int result = 0; | |
288 | ||
289 | #if 0 | |
290 | printk("codec trigger!!! - what = %i, enable = %i, status = 0x%x\n", what, enable, inb(AD1848P(card, STATUS))); | |
291 | #endif | |
292 | spin_lock(&chip->reg_lock); | |
293 | if (cmd == SNDRV_PCM_TRIGGER_START) { | |
294 | if (chip->image[AD1848_IFACE_CTRL] & what) { | |
295 | spin_unlock(&chip->reg_lock); | |
296 | return 0; | |
297 | } | |
298 | snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] |= what); | |
299 | chip->mode |= AD1848_MODE_RUNNING; | |
300 | } else if (cmd == SNDRV_PCM_TRIGGER_STOP) { | |
301 | if (!(chip->image[AD1848_IFACE_CTRL] & what)) { | |
302 | spin_unlock(&chip->reg_lock); | |
303 | return 0; | |
304 | } | |
305 | snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL] &= ~what); | |
306 | chip->mode &= ~AD1848_MODE_RUNNING; | |
307 | } else { | |
308 | result = -EINVAL; | |
309 | } | |
310 | spin_unlock(&chip->reg_lock); | |
311 | return result; | |
312 | } | |
313 | ||
314 | /* | |
315 | * CODEC I/O | |
316 | */ | |
317 | ||
318 | static unsigned char snd_ad1848_get_rate(unsigned int rate) | |
319 | { | |
320 | int i; | |
321 | ||
322 | for (i = 0; i < 14; i++) | |
323 | if (rate == rates[i]) | |
324 | return freq_bits[i]; | |
325 | snd_BUG(); | |
326 | return freq_bits[13]; | |
327 | } | |
328 | ||
329 | static int snd_ad1848_ioctl(snd_pcm_substream_t * substream, | |
330 | unsigned int cmd, void *arg) | |
331 | { | |
332 | return snd_pcm_lib_ioctl(substream, cmd, arg); | |
333 | } | |
334 | ||
335 | static unsigned char snd_ad1848_get_format(int format, int channels) | |
336 | { | |
337 | unsigned char rformat; | |
338 | ||
339 | rformat = AD1848_LINEAR_8; | |
340 | switch (format) { | |
341 | case SNDRV_PCM_FORMAT_A_LAW: rformat = AD1848_ALAW_8; break; | |
342 | case SNDRV_PCM_FORMAT_MU_LAW: rformat = AD1848_ULAW_8; break; | |
343 | case SNDRV_PCM_FORMAT_S16_LE: rformat = AD1848_LINEAR_16; break; | |
344 | } | |
345 | if (channels > 1) | |
346 | rformat |= AD1848_STEREO; | |
347 | #if 0 | |
348 | snd_printk("get_format: 0x%x (mode=0x%x)\n", format, mode); | |
349 | #endif | |
350 | return rformat; | |
351 | } | |
352 | ||
353 | static void snd_ad1848_calibrate_mute(ad1848_t *chip, int mute) | |
354 | { | |
355 | unsigned long flags; | |
356 | ||
357 | mute = mute ? 1 : 0; | |
358 | spin_lock_irqsave(&chip->reg_lock, flags); | |
359 | if (chip->calibrate_mute == mute) { | |
360 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
361 | return; | |
362 | } | |
363 | if (!mute) { | |
364 | snd_ad1848_dout(chip, AD1848_LEFT_INPUT, chip->image[AD1848_LEFT_INPUT]); | |
365 | snd_ad1848_dout(chip, AD1848_RIGHT_INPUT, chip->image[AD1848_RIGHT_INPUT]); | |
366 | } | |
367 | snd_ad1848_dout(chip, AD1848_AUX1_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_LEFT_INPUT]); | |
368 | snd_ad1848_dout(chip, AD1848_AUX1_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX1_RIGHT_INPUT]); | |
369 | snd_ad1848_dout(chip, AD1848_AUX2_LEFT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_LEFT_INPUT]); | |
370 | snd_ad1848_dout(chip, AD1848_AUX2_RIGHT_INPUT, mute ? 0x80 : chip->image[AD1848_AUX2_RIGHT_INPUT]); | |
371 | snd_ad1848_dout(chip, AD1848_LEFT_OUTPUT, mute ? 0x80 : chip->image[AD1848_LEFT_OUTPUT]); | |
372 | snd_ad1848_dout(chip, AD1848_RIGHT_OUTPUT, mute ? 0x80 : chip->image[AD1848_RIGHT_OUTPUT]); | |
373 | chip->calibrate_mute = mute; | |
374 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
375 | } | |
376 | ||
377 | static void snd_ad1848_set_data_format(ad1848_t *chip, snd_pcm_hw_params_t *hw_params) | |
378 | { | |
379 | if (hw_params == NULL) { | |
380 | chip->image[AD1848_DATA_FORMAT] = 0x20; | |
381 | } else { | |
382 | chip->image[AD1848_DATA_FORMAT] = | |
383 | snd_ad1848_get_format(params_format(hw_params), params_channels(hw_params)) | | |
384 | snd_ad1848_get_rate(params_rate(hw_params)); | |
385 | } | |
386 | // snd_printk(">>> pmode = 0x%x, dfr = 0x%x\n", pstr->mode, chip->image[AD1848_DATA_FORMAT]); | |
387 | } | |
388 | ||
389 | static int snd_ad1848_open(ad1848_t *chip, unsigned int mode) | |
390 | { | |
391 | unsigned long flags; | |
392 | ||
393 | down(&chip->open_mutex); | |
394 | if (chip->mode & AD1848_MODE_OPEN) { | |
395 | up(&chip->open_mutex); | |
396 | return -EAGAIN; | |
397 | } | |
398 | snd_ad1848_mce_down(chip); | |
399 | ||
400 | #ifdef SNDRV_DEBUG_MCE | |
401 | snd_printk("open: (1)\n"); | |
402 | #endif | |
403 | snd_ad1848_mce_up(chip); | |
404 | spin_lock_irqsave(&chip->reg_lock, flags); | |
405 | chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO | | |
406 | AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO | | |
407 | AD1848_CALIB_MODE); | |
408 | chip->image[AD1848_IFACE_CTRL] |= AD1848_AUTOCALIB; | |
409 | snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]); | |
410 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
411 | snd_ad1848_mce_down(chip); | |
412 | ||
413 | #ifdef SNDRV_DEBUG_MCE | |
414 | snd_printk("open: (2)\n"); | |
415 | #endif | |
416 | ||
417 | snd_ad1848_set_data_format(chip, NULL); | |
418 | ||
419 | snd_ad1848_mce_up(chip); | |
420 | spin_lock_irqsave(&chip->reg_lock, flags); | |
421 | snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]); | |
422 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
423 | snd_ad1848_mce_down(chip); | |
424 | ||
425 | #ifdef SNDRV_DEBUG_MCE | |
426 | snd_printk("open: (3)\n"); | |
427 | #endif | |
428 | ||
429 | /* ok. now enable and ack CODEC IRQ */ | |
430 | spin_lock_irqsave(&chip->reg_lock, flags); | |
431 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
432 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
433 | chip->image[AD1848_PIN_CTRL] |= AD1848_IRQ_ENABLE; | |
434 | snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]); | |
435 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
436 | ||
437 | chip->mode = mode; | |
438 | up(&chip->open_mutex); | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | static void snd_ad1848_close(ad1848_t *chip) | |
444 | { | |
445 | unsigned long flags; | |
446 | ||
447 | down(&chip->open_mutex); | |
448 | if (!chip->mode) { | |
449 | up(&chip->open_mutex); | |
450 | return; | |
451 | } | |
452 | /* disable IRQ */ | |
453 | spin_lock_irqsave(&chip->reg_lock, flags); | |
454 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
455 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
456 | chip->image[AD1848_PIN_CTRL] &= ~AD1848_IRQ_ENABLE; | |
457 | snd_ad1848_out(chip, AD1848_PIN_CTRL, chip->image[AD1848_PIN_CTRL]); | |
458 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
459 | ||
460 | /* now disable capture & playback */ | |
461 | ||
462 | snd_ad1848_mce_up(chip); | |
463 | spin_lock_irqsave(&chip->reg_lock, flags); | |
464 | chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO | | |
465 | AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO); | |
466 | snd_ad1848_out(chip, AD1848_IFACE_CTRL, chip->image[AD1848_IFACE_CTRL]); | |
467 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
468 | snd_ad1848_mce_down(chip); | |
469 | ||
470 | /* clear IRQ again */ | |
471 | spin_lock_irqsave(&chip->reg_lock, flags); | |
472 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
473 | outb(0, AD1848P(chip, STATUS)); /* clear IRQ */ | |
474 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
475 | ||
476 | chip->mode = 0; | |
477 | up(&chip->open_mutex); | |
478 | } | |
479 | ||
480 | /* | |
481 | * ok.. exported functions.. | |
482 | */ | |
483 | ||
484 | static int snd_ad1848_playback_trigger(snd_pcm_substream_t * substream, | |
485 | int cmd) | |
486 | { | |
487 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
488 | return snd_ad1848_trigger(chip, AD1848_PLAYBACK_ENABLE, SNDRV_PCM_STREAM_PLAYBACK, cmd); | |
489 | } | |
490 | ||
491 | static int snd_ad1848_capture_trigger(snd_pcm_substream_t * substream, | |
492 | int cmd) | |
493 | { | |
494 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
495 | return snd_ad1848_trigger(chip, AD1848_CAPTURE_ENABLE, SNDRV_PCM_STREAM_CAPTURE, cmd); | |
496 | } | |
497 | ||
498 | static int snd_ad1848_playback_hw_params(snd_pcm_substream_t * substream, | |
499 | snd_pcm_hw_params_t * hw_params) | |
500 | { | |
501 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
502 | unsigned long flags; | |
503 | int err; | |
504 | ||
505 | if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) | |
506 | return err; | |
507 | snd_ad1848_calibrate_mute(chip, 1); | |
508 | snd_ad1848_set_data_format(chip, hw_params); | |
509 | snd_ad1848_mce_up(chip); | |
510 | spin_lock_irqsave(&chip->reg_lock, flags); | |
511 | snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]); | |
512 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
513 | snd_ad1848_mce_down(chip); | |
514 | snd_ad1848_calibrate_mute(chip, 0); | |
515 | return 0; | |
516 | } | |
517 | ||
518 | static int snd_ad1848_playback_hw_free(snd_pcm_substream_t * substream) | |
519 | { | |
520 | return snd_pcm_lib_free_pages(substream); | |
521 | } | |
522 | ||
523 | static int snd_ad1848_playback_prepare(snd_pcm_substream_t * substream) | |
524 | { | |
525 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
526 | snd_pcm_runtime_t *runtime = substream->runtime; | |
527 | unsigned long flags; | |
528 | unsigned int size = snd_pcm_lib_buffer_bytes(substream); | |
529 | unsigned int count = snd_pcm_lib_period_bytes(substream); | |
530 | ||
531 | chip->dma_size = size; | |
532 | chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_PLAYBACK_ENABLE | AD1848_PLAYBACK_PIO); | |
533 | snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT); | |
534 | count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1; | |
535 | spin_lock_irqsave(&chip->reg_lock, flags); | |
536 | snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count); | |
537 | snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8)); | |
538 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | static int snd_ad1848_capture_hw_params(snd_pcm_substream_t * substream, | |
543 | snd_pcm_hw_params_t * hw_params) | |
544 | { | |
545 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
546 | unsigned long flags; | |
547 | int err; | |
548 | ||
549 | if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) | |
550 | return err; | |
551 | snd_ad1848_calibrate_mute(chip, 1); | |
552 | snd_ad1848_set_data_format(chip, hw_params); | |
553 | snd_ad1848_mce_up(chip); | |
554 | spin_lock_irqsave(&chip->reg_lock, flags); | |
555 | snd_ad1848_out(chip, AD1848_DATA_FORMAT, chip->image[AD1848_DATA_FORMAT]); | |
556 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
557 | snd_ad1848_mce_down(chip); | |
558 | snd_ad1848_calibrate_mute(chip, 0); | |
559 | return 0; | |
560 | } | |
561 | ||
562 | static int snd_ad1848_capture_hw_free(snd_pcm_substream_t * substream) | |
563 | { | |
564 | return snd_pcm_lib_free_pages(substream); | |
565 | } | |
566 | ||
567 | static int snd_ad1848_capture_prepare(snd_pcm_substream_t * substream) | |
568 | { | |
569 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
570 | snd_pcm_runtime_t *runtime = substream->runtime; | |
571 | unsigned long flags; | |
572 | unsigned int size = snd_pcm_lib_buffer_bytes(substream); | |
573 | unsigned int count = snd_pcm_lib_period_bytes(substream); | |
574 | ||
575 | chip->dma_size = size; | |
576 | chip->image[AD1848_IFACE_CTRL] &= ~(AD1848_CAPTURE_ENABLE | AD1848_CAPTURE_PIO); | |
577 | snd_dma_program(chip->dma, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT); | |
578 | count = snd_ad1848_get_count(chip->image[AD1848_DATA_FORMAT], count) - 1; | |
579 | spin_lock_irqsave(&chip->reg_lock, flags); | |
580 | snd_ad1848_out(chip, AD1848_DATA_LWR_CNT, (unsigned char) count); | |
581 | snd_ad1848_out(chip, AD1848_DATA_UPR_CNT, (unsigned char) (count >> 8)); | |
582 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
583 | return 0; | |
584 | } | |
585 | ||
586 | static irqreturn_t snd_ad1848_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
587 | { | |
588 | ad1848_t *chip = dev_id; | |
589 | ||
590 | if ((chip->mode & AD1848_MODE_PLAY) && chip->playback_substream && | |
591 | (chip->mode & AD1848_MODE_RUNNING)) | |
592 | snd_pcm_period_elapsed(chip->playback_substream); | |
593 | if ((chip->mode & AD1848_MODE_CAPTURE) && chip->capture_substream && | |
594 | (chip->mode & AD1848_MODE_RUNNING)) | |
595 | snd_pcm_period_elapsed(chip->capture_substream); | |
596 | outb(0, AD1848P(chip, STATUS)); /* clear global interrupt bit */ | |
597 | return IRQ_HANDLED; | |
598 | } | |
599 | ||
600 | static snd_pcm_uframes_t snd_ad1848_playback_pointer(snd_pcm_substream_t * substream) | |
601 | { | |
602 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
603 | size_t ptr; | |
604 | ||
605 | if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_PLAYBACK_ENABLE)) | |
606 | return 0; | |
607 | ptr = snd_dma_pointer(chip->dma, chip->dma_size); | |
608 | return bytes_to_frames(substream->runtime, ptr); | |
609 | } | |
610 | ||
611 | static snd_pcm_uframes_t snd_ad1848_capture_pointer(snd_pcm_substream_t * substream) | |
612 | { | |
613 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
614 | size_t ptr; | |
615 | ||
616 | if (!(chip->image[AD1848_IFACE_CTRL] & AD1848_CAPTURE_ENABLE)) | |
617 | return 0; | |
618 | ptr = snd_dma_pointer(chip->dma, chip->dma_size); | |
619 | return bytes_to_frames(substream->runtime, ptr); | |
620 | } | |
621 | ||
622 | /* | |
623 | ||
624 | */ | |
625 | ||
626 | static void snd_ad1848_thinkpad_twiddle(ad1848_t *chip, int on) { | |
627 | ||
628 | int tmp; | |
629 | ||
630 | if (!chip->thinkpad_flag) return; | |
631 | ||
632 | outb(0x1c, AD1848_THINKPAD_CTL_PORT1); | |
633 | tmp = inb(AD1848_THINKPAD_CTL_PORT2); | |
634 | ||
635 | if (on) | |
636 | /* turn it on */ | |
637 | tmp |= AD1848_THINKPAD_CS4248_ENABLE_BIT; | |
638 | else | |
639 | /* turn it off */ | |
640 | tmp &= ~AD1848_THINKPAD_CS4248_ENABLE_BIT; | |
641 | ||
642 | outb(tmp, AD1848_THINKPAD_CTL_PORT2); | |
643 | ||
644 | } | |
645 | ||
646 | #ifdef CONFIG_PM | |
647 | static int snd_ad1848_suspend(snd_card_t *card, pm_message_t state) | |
648 | { | |
649 | ad1848_t *chip = card->pm_private_data; | |
650 | ||
651 | snd_pcm_suspend_all(chip->pcm); | |
652 | /* FIXME: save registers? */ | |
653 | ||
654 | if (chip->thinkpad_flag) | |
655 | snd_ad1848_thinkpad_twiddle(chip, 0); | |
656 | ||
657 | return 0; | |
658 | } | |
659 | ||
660 | static int snd_ad1848_resume(snd_card_t *card) | |
661 | { | |
662 | ad1848_t *chip = card->pm_private_data; | |
663 | ||
664 | if (chip->thinkpad_flag) | |
665 | snd_ad1848_thinkpad_twiddle(chip, 1); | |
666 | ||
667 | /* FIXME: restore registers? */ | |
668 | ||
669 | return 0; | |
670 | } | |
671 | #endif /* CONFIG_PM */ | |
672 | ||
673 | static int snd_ad1848_probe(ad1848_t * chip) | |
674 | { | |
675 | unsigned long flags; | |
676 | int i, id, rev, ad1847; | |
677 | unsigned char *ptr; | |
678 | ||
679 | #if 0 | |
680 | snd_ad1848_debug(chip); | |
681 | #endif | |
682 | id = ad1847 = 0; | |
683 | for (i = 0; i < 1000; i++) { | |
684 | mb(); | |
685 | if (inb(AD1848P(chip, REGSEL)) & AD1848_INIT) | |
686 | udelay(500); | |
687 | else { | |
688 | spin_lock_irqsave(&chip->reg_lock, flags); | |
689 | snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00); | |
690 | snd_ad1848_out(chip, AD1848_LEFT_INPUT, 0xaa); | |
691 | snd_ad1848_out(chip, AD1848_RIGHT_INPUT, 0x45); | |
692 | rev = snd_ad1848_in(chip, AD1848_RIGHT_INPUT); | |
693 | if (rev == 0x65) { | |
694 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
695 | id = 1; | |
696 | ad1847 = 1; | |
697 | break; | |
698 | } | |
699 | if (snd_ad1848_in(chip, AD1848_LEFT_INPUT) == 0xaa && rev == 0x45) { | |
700 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
701 | id = 1; | |
702 | break; | |
703 | } | |
704 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
705 | } | |
706 | } | |
707 | if (id != 1) | |
708 | return -ENODEV; /* no valid device found */ | |
709 | if (chip->hardware == AD1848_HW_DETECT) { | |
710 | if (ad1847) { | |
711 | chip->hardware = AD1848_HW_AD1847; | |
712 | } else { | |
713 | chip->hardware = AD1848_HW_AD1848; | |
714 | rev = snd_ad1848_in(chip, AD1848_MISC_INFO); | |
715 | if (rev & 0x80) { | |
716 | chip->hardware = AD1848_HW_CS4248; | |
717 | } else if ((rev & 0x0f) == 0x0a) { | |
718 | snd_ad1848_out(chip, AD1848_MISC_INFO, 0x40); | |
719 | for (i = 0; i < 16; ++i) { | |
720 | if (snd_ad1848_in(chip, i) != snd_ad1848_in(chip, i + 16)) { | |
721 | chip->hardware = AD1848_HW_CMI8330; | |
722 | break; | |
723 | } | |
724 | } | |
725 | snd_ad1848_out(chip, AD1848_MISC_INFO, 0x00); | |
726 | } | |
727 | } | |
728 | } | |
729 | spin_lock_irqsave(&chip->reg_lock, flags); | |
730 | inb(AD1848P(chip, STATUS)); /* clear any pendings IRQ */ | |
731 | outb(0, AD1848P(chip, STATUS)); | |
732 | mb(); | |
733 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
734 | ||
735 | chip->image[AD1848_MISC_INFO] = 0x00; | |
736 | chip->image[AD1848_IFACE_CTRL] = | |
737 | (chip->image[AD1848_IFACE_CTRL] & ~AD1848_SINGLE_DMA) | AD1848_SINGLE_DMA; | |
738 | ptr = (unsigned char *) &chip->image; | |
739 | snd_ad1848_mce_down(chip); | |
740 | spin_lock_irqsave(&chip->reg_lock, flags); | |
741 | for (i = 0; i < 16; i++) /* ok.. fill all AD1848 registers */ | |
742 | snd_ad1848_out(chip, i, *ptr++); | |
743 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
744 | snd_ad1848_mce_up(chip); | |
745 | snd_ad1848_mce_down(chip); | |
746 | return 0; /* all things are ok.. */ | |
747 | } | |
748 | ||
749 | /* | |
750 | ||
751 | */ | |
752 | ||
753 | static snd_pcm_hardware_t snd_ad1848_playback = | |
754 | { | |
755 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
756 | SNDRV_PCM_INFO_MMAP_VALID), | |
757 | .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | | |
758 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE), | |
759 | .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000, | |
760 | .rate_min = 5510, | |
761 | .rate_max = 48000, | |
762 | .channels_min = 1, | |
763 | .channels_max = 2, | |
764 | .buffer_bytes_max = (128*1024), | |
765 | .period_bytes_min = 64, | |
766 | .period_bytes_max = (128*1024), | |
767 | .periods_min = 1, | |
768 | .periods_max = 1024, | |
769 | .fifo_size = 0, | |
770 | }; | |
771 | ||
772 | static snd_pcm_hardware_t snd_ad1848_capture = | |
773 | { | |
774 | .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED | | |
775 | SNDRV_PCM_INFO_MMAP_VALID), | |
776 | .formats = (SNDRV_PCM_FMTBIT_MU_LAW | SNDRV_PCM_FMTBIT_A_LAW | | |
777 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE), | |
778 | .rates = SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_8000_48000, | |
779 | .rate_min = 5510, | |
780 | .rate_max = 48000, | |
781 | .channels_min = 1, | |
782 | .channels_max = 2, | |
783 | .buffer_bytes_max = (128*1024), | |
784 | .period_bytes_min = 64, | |
785 | .period_bytes_max = (128*1024), | |
786 | .periods_min = 1, | |
787 | .periods_max = 1024, | |
788 | .fifo_size = 0, | |
789 | }; | |
790 | ||
791 | /* | |
792 | ||
793 | */ | |
794 | ||
795 | static int snd_ad1848_playback_open(snd_pcm_substream_t * substream) | |
796 | { | |
797 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
798 | snd_pcm_runtime_t *runtime = substream->runtime; | |
799 | int err; | |
800 | ||
801 | if ((err = snd_ad1848_open(chip, AD1848_MODE_PLAY)) < 0) | |
802 | return err; | |
803 | chip->playback_substream = substream; | |
804 | runtime->hw = snd_ad1848_playback; | |
805 | snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max); | |
806 | snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max); | |
807 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates); | |
808 | return 0; | |
809 | } | |
810 | ||
811 | static int snd_ad1848_capture_open(snd_pcm_substream_t * substream) | |
812 | { | |
813 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
814 | snd_pcm_runtime_t *runtime = substream->runtime; | |
815 | int err; | |
816 | ||
817 | if ((err = snd_ad1848_open(chip, AD1848_MODE_CAPTURE)) < 0) | |
818 | return err; | |
819 | chip->capture_substream = substream; | |
820 | runtime->hw = snd_ad1848_capture; | |
821 | snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.buffer_bytes_max); | |
822 | snd_pcm_limit_isa_dma_size(chip->dma, &runtime->hw.period_bytes_max); | |
823 | snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, &hw_constraints_rates); | |
824 | return 0; | |
825 | } | |
826 | ||
827 | static int snd_ad1848_playback_close(snd_pcm_substream_t * substream) | |
828 | { | |
829 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
830 | ||
831 | chip->mode &= ~AD1848_MODE_PLAY; | |
832 | chip->playback_substream = NULL; | |
833 | snd_ad1848_close(chip); | |
834 | return 0; | |
835 | } | |
836 | ||
837 | static int snd_ad1848_capture_close(snd_pcm_substream_t * substream) | |
838 | { | |
839 | ad1848_t *chip = snd_pcm_substream_chip(substream); | |
840 | ||
841 | chip->mode &= ~AD1848_MODE_CAPTURE; | |
842 | chip->capture_substream = NULL; | |
843 | snd_ad1848_close(chip); | |
844 | return 0; | |
845 | } | |
846 | ||
847 | static int snd_ad1848_free(ad1848_t *chip) | |
848 | { | |
b1d5776d | 849 | release_and_free_resource(chip->res_port); |
1da177e4 LT |
850 | if (chip->irq >= 0) |
851 | free_irq(chip->irq, (void *) chip); | |
852 | if (chip->dma >= 0) { | |
853 | snd_dma_disable(chip->dma); | |
854 | free_dma(chip->dma); | |
855 | } | |
856 | kfree(chip); | |
857 | return 0; | |
858 | } | |
859 | ||
860 | static int snd_ad1848_dev_free(snd_device_t *device) | |
861 | { | |
862 | ad1848_t *chip = device->device_data; | |
863 | return snd_ad1848_free(chip); | |
864 | } | |
865 | ||
866 | static const char *snd_ad1848_chip_id(ad1848_t *chip) | |
867 | { | |
868 | switch (chip->hardware) { | |
869 | case AD1848_HW_AD1847: return "AD1847"; | |
870 | case AD1848_HW_AD1848: return "AD1848"; | |
871 | case AD1848_HW_CS4248: return "CS4248"; | |
872 | case AD1848_HW_CMI8330: return "CMI8330/C3D"; | |
873 | default: return "???"; | |
874 | } | |
875 | } | |
876 | ||
877 | int snd_ad1848_create(snd_card_t * card, | |
878 | unsigned long port, | |
879 | int irq, int dma, | |
880 | unsigned short hardware, | |
881 | ad1848_t ** rchip) | |
882 | { | |
883 | static snd_device_ops_t ops = { | |
884 | .dev_free = snd_ad1848_dev_free, | |
885 | }; | |
886 | ad1848_t *chip; | |
887 | int err; | |
888 | ||
889 | *rchip = NULL; | |
9e76a76e | 890 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
1da177e4 LT |
891 | if (chip == NULL) |
892 | return -ENOMEM; | |
893 | spin_lock_init(&chip->reg_lock); | |
894 | init_MUTEX(&chip->open_mutex); | |
895 | chip->card = card; | |
896 | chip->port = port; | |
897 | chip->irq = -1; | |
898 | chip->dma = -1; | |
899 | chip->hardware = hardware; | |
900 | memcpy(&chip->image, &snd_ad1848_original_image, sizeof(snd_ad1848_original_image)); | |
901 | ||
902 | if ((chip->res_port = request_region(port, 4, "AD1848")) == NULL) { | |
903 | snd_printk(KERN_ERR "ad1848: can't grab port 0x%lx\n", port); | |
904 | snd_ad1848_free(chip); | |
905 | return -EBUSY; | |
906 | } | |
907 | if (request_irq(irq, snd_ad1848_interrupt, SA_INTERRUPT, "AD1848", (void *) chip)) { | |
908 | snd_printk(KERN_ERR "ad1848: can't grab IRQ %d\n", irq); | |
909 | snd_ad1848_free(chip); | |
910 | return -EBUSY; | |
911 | } | |
912 | chip->irq = irq; | |
913 | if (request_dma(dma, "AD1848")) { | |
914 | snd_printk(KERN_ERR "ad1848: can't grab DMA %d\n", dma); | |
915 | snd_ad1848_free(chip); | |
916 | return -EBUSY; | |
917 | } | |
918 | chip->dma = dma; | |
919 | ||
920 | if (hardware == AD1848_HW_THINKPAD) { | |
921 | chip->thinkpad_flag = 1; | |
922 | chip->hardware = AD1848_HW_DETECT; /* reset */ | |
923 | snd_ad1848_thinkpad_twiddle(chip, 1); | |
924 | snd_card_set_isa_pm_callback(card, snd_ad1848_suspend, snd_ad1848_resume, chip); | |
925 | } | |
926 | ||
927 | if (snd_ad1848_probe(chip) < 0) { | |
928 | snd_ad1848_free(chip); | |
929 | return -ENODEV; | |
930 | } | |
931 | ||
932 | /* Register device */ | |
933 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { | |
934 | snd_ad1848_free(chip); | |
935 | return err; | |
936 | } | |
937 | ||
938 | *rchip = chip; | |
939 | return 0; | |
940 | } | |
941 | ||
942 | static snd_pcm_ops_t snd_ad1848_playback_ops = { | |
943 | .open = snd_ad1848_playback_open, | |
944 | .close = snd_ad1848_playback_close, | |
945 | .ioctl = snd_ad1848_ioctl, | |
946 | .hw_params = snd_ad1848_playback_hw_params, | |
947 | .hw_free = snd_ad1848_playback_hw_free, | |
948 | .prepare = snd_ad1848_playback_prepare, | |
949 | .trigger = snd_ad1848_playback_trigger, | |
950 | .pointer = snd_ad1848_playback_pointer, | |
951 | }; | |
952 | ||
953 | static snd_pcm_ops_t snd_ad1848_capture_ops = { | |
954 | .open = snd_ad1848_capture_open, | |
955 | .close = snd_ad1848_capture_close, | |
956 | .ioctl = snd_ad1848_ioctl, | |
957 | .hw_params = snd_ad1848_capture_hw_params, | |
958 | .hw_free = snd_ad1848_capture_hw_free, | |
959 | .prepare = snd_ad1848_capture_prepare, | |
960 | .trigger = snd_ad1848_capture_trigger, | |
961 | .pointer = snd_ad1848_capture_pointer, | |
962 | }; | |
963 | ||
964 | static void snd_ad1848_pcm_free(snd_pcm_t *pcm) | |
965 | { | |
966 | ad1848_t *chip = pcm->private_data; | |
967 | chip->pcm = NULL; | |
968 | snd_pcm_lib_preallocate_free_for_all(pcm); | |
969 | } | |
970 | ||
971 | int snd_ad1848_pcm(ad1848_t *chip, int device, snd_pcm_t **rpcm) | |
972 | { | |
973 | snd_pcm_t *pcm; | |
974 | int err; | |
975 | ||
976 | if ((err = snd_pcm_new(chip->card, "AD1848", device, 1, 1, &pcm)) < 0) | |
977 | return err; | |
978 | ||
979 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ad1848_playback_ops); | |
980 | snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ad1848_capture_ops); | |
981 | ||
982 | pcm->private_free = snd_ad1848_pcm_free; | |
983 | pcm->private_data = chip; | |
984 | pcm->info_flags = SNDRV_PCM_INFO_HALF_DUPLEX; | |
985 | strcpy(pcm->name, snd_ad1848_chip_id(chip)); | |
986 | ||
987 | snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, | |
988 | snd_dma_isa_data(), | |
989 | 64*1024, chip->dma > 3 ? 128*1024 : 64*1024); | |
990 | ||
991 | chip->pcm = pcm; | |
992 | if (rpcm) | |
993 | *rpcm = pcm; | |
994 | return 0; | |
995 | } | |
996 | ||
997 | const snd_pcm_ops_t *snd_ad1848_get_pcm_ops(int direction) | |
998 | { | |
999 | return direction == SNDRV_PCM_STREAM_PLAYBACK ? | |
1000 | &snd_ad1848_playback_ops : &snd_ad1848_capture_ops; | |
1001 | } | |
1002 | ||
1003 | /* | |
1004 | * MIXER part | |
1005 | */ | |
1006 | ||
1007 | static int snd_ad1848_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo) | |
1008 | { | |
1009 | static char *texts[4] = { | |
1010 | "Line", "Aux", "Mic", "Mix" | |
1011 | }; | |
1012 | ||
1013 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
1014 | uinfo->count = 2; | |
1015 | uinfo->value.enumerated.items = 4; | |
1016 | if (uinfo->value.enumerated.item > 3) | |
1017 | uinfo->value.enumerated.item = 3; | |
1018 | strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); | |
1019 | return 0; | |
1020 | } | |
1021 | ||
1022 | static int snd_ad1848_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1023 | { | |
1024 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1025 | unsigned long flags; | |
1026 | ||
1027 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1028 | ucontrol->value.enumerated.item[0] = (chip->image[AD1848_LEFT_INPUT] & AD1848_MIXS_ALL) >> 6; | |
1029 | ucontrol->value.enumerated.item[1] = (chip->image[AD1848_RIGHT_INPUT] & AD1848_MIXS_ALL) >> 6; | |
1030 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1031 | return 0; | |
1032 | } | |
1033 | ||
1034 | static int snd_ad1848_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1035 | { | |
1036 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1037 | unsigned long flags; | |
1038 | unsigned short left, right; | |
1039 | int change; | |
1040 | ||
1041 | if (ucontrol->value.enumerated.item[0] > 3 || | |
1042 | ucontrol->value.enumerated.item[1] > 3) | |
1043 | return -EINVAL; | |
1044 | left = ucontrol->value.enumerated.item[0] << 6; | |
1045 | right = ucontrol->value.enumerated.item[1] << 6; | |
1046 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1047 | left = (chip->image[AD1848_LEFT_INPUT] & ~AD1848_MIXS_ALL) | left; | |
1048 | right = (chip->image[AD1848_RIGHT_INPUT] & ~AD1848_MIXS_ALL) | right; | |
1049 | change = left != chip->image[AD1848_LEFT_INPUT] || | |
1050 | right != chip->image[AD1848_RIGHT_INPUT]; | |
1051 | snd_ad1848_out(chip, AD1848_LEFT_INPUT, left); | |
1052 | snd_ad1848_out(chip, AD1848_RIGHT_INPUT, right); | |
1053 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1054 | return change; | |
1055 | } | |
1056 | ||
1057 | static int snd_ad1848_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo) | |
1058 | { | |
1059 | int mask = (kcontrol->private_value >> 16) & 0xff; | |
1060 | ||
1061 | uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1062 | uinfo->count = 1; | |
1063 | uinfo->value.integer.min = 0; | |
1064 | uinfo->value.integer.max = mask; | |
1065 | return 0; | |
1066 | } | |
1067 | ||
1068 | static int snd_ad1848_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1069 | { | |
1070 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1071 | unsigned long flags; | |
1072 | int reg = kcontrol->private_value & 0xff; | |
1073 | int shift = (kcontrol->private_value >> 8) & 0xff; | |
1074 | int mask = (kcontrol->private_value >> 16) & 0xff; | |
1075 | int invert = (kcontrol->private_value >> 24) & 0xff; | |
1076 | ||
1077 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1078 | ucontrol->value.integer.value[0] = (chip->image[reg] >> shift) & mask; | |
1079 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1080 | if (invert) | |
1081 | ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; | |
1082 | return 0; | |
1083 | } | |
1084 | ||
1085 | static int snd_ad1848_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1086 | { | |
1087 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1088 | unsigned long flags; | |
1089 | int reg = kcontrol->private_value & 0xff; | |
1090 | int shift = (kcontrol->private_value >> 8) & 0xff; | |
1091 | int mask = (kcontrol->private_value >> 16) & 0xff; | |
1092 | int invert = (kcontrol->private_value >> 24) & 0xff; | |
1093 | int change; | |
1094 | unsigned short val; | |
1095 | ||
1096 | val = (ucontrol->value.integer.value[0] & mask); | |
1097 | if (invert) | |
1098 | val = mask - val; | |
1099 | val <<= shift; | |
1100 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1101 | val = (chip->image[reg] & ~(mask << shift)) | val; | |
1102 | change = val != chip->image[reg]; | |
1103 | snd_ad1848_out(chip, reg, val); | |
1104 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1105 | return change; | |
1106 | } | |
1107 | ||
1108 | static int snd_ad1848_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo) | |
1109 | { | |
1110 | int mask = (kcontrol->private_value >> 24) & 0xff; | |
1111 | ||
1112 | uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; | |
1113 | uinfo->count = 2; | |
1114 | uinfo->value.integer.min = 0; | |
1115 | uinfo->value.integer.max = mask; | |
1116 | return 0; | |
1117 | } | |
1118 | ||
1119 | static int snd_ad1848_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1120 | { | |
1121 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1122 | unsigned long flags; | |
1123 | int left_reg = kcontrol->private_value & 0xff; | |
1124 | int right_reg = (kcontrol->private_value >> 8) & 0xff; | |
1125 | int shift_left = (kcontrol->private_value >> 16) & 0x07; | |
1126 | int shift_right = (kcontrol->private_value >> 19) & 0x07; | |
1127 | int mask = (kcontrol->private_value >> 24) & 0xff; | |
1128 | int invert = (kcontrol->private_value >> 22) & 1; | |
1129 | ||
1130 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1131 | ucontrol->value.integer.value[0] = (chip->image[left_reg] >> shift_left) & mask; | |
1132 | ucontrol->value.integer.value[1] = (chip->image[right_reg] >> shift_right) & mask; | |
1133 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1134 | if (invert) { | |
1135 | ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0]; | |
1136 | ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1]; | |
1137 | } | |
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | static int snd_ad1848_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol) | |
1142 | { | |
1143 | ad1848_t *chip = snd_kcontrol_chip(kcontrol); | |
1144 | unsigned long flags; | |
1145 | int left_reg = kcontrol->private_value & 0xff; | |
1146 | int right_reg = (kcontrol->private_value >> 8) & 0xff; | |
1147 | int shift_left = (kcontrol->private_value >> 16) & 0x07; | |
1148 | int shift_right = (kcontrol->private_value >> 19) & 0x07; | |
1149 | int mask = (kcontrol->private_value >> 24) & 0xff; | |
1150 | int invert = (kcontrol->private_value >> 22) & 1; | |
1151 | int change; | |
1152 | unsigned short val1, val2; | |
1153 | ||
1154 | val1 = ucontrol->value.integer.value[0] & mask; | |
1155 | val2 = ucontrol->value.integer.value[1] & mask; | |
1156 | if (invert) { | |
1157 | val1 = mask - val1; | |
1158 | val2 = mask - val2; | |
1159 | } | |
1160 | val1 <<= shift_left; | |
1161 | val2 <<= shift_right; | |
1162 | spin_lock_irqsave(&chip->reg_lock, flags); | |
1163 | if (left_reg != right_reg) { | |
1164 | val1 = (chip->image[left_reg] & ~(mask << shift_left)) | val1; | |
1165 | val2 = (chip->image[right_reg] & ~(mask << shift_right)) | val2; | |
1166 | change = val1 != chip->image[left_reg] || val2 != chip->image[right_reg]; | |
1167 | snd_ad1848_out(chip, left_reg, val1); | |
1168 | snd_ad1848_out(chip, right_reg, val2); | |
1169 | } else { | |
1170 | val1 = (chip->image[left_reg] & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2; | |
1171 | change = val1 != chip->image[left_reg]; | |
1172 | snd_ad1848_out(chip, left_reg, val1); | |
1173 | } | |
1174 | spin_unlock_irqrestore(&chip->reg_lock, flags); | |
1175 | return change; | |
1176 | } | |
1177 | ||
1178 | /* | |
1179 | */ | |
1180 | int snd_ad1848_add_ctl(ad1848_t *chip, const char *name, int index, int type, unsigned long value) | |
1181 | { | |
1182 | static snd_kcontrol_new_t newctls[] = { | |
1183 | [AD1848_MIX_SINGLE] = { | |
1184 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1185 | .info = snd_ad1848_info_single, | |
1186 | .get = snd_ad1848_get_single, | |
1187 | .put = snd_ad1848_put_single, | |
1188 | }, | |
1189 | [AD1848_MIX_DOUBLE] = { | |
1190 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
1191 | .info = snd_ad1848_info_double, | |
1192 | .get = snd_ad1848_get_double, | |
1193 | .put = snd_ad1848_put_double, | |
1194 | }, | |
1195 | [AD1848_MIX_CAPTURE] = { | |
67ed4161 | 1196 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
1da177e4 LT |
1197 | .info = snd_ad1848_info_mux, |
1198 | .get = snd_ad1848_get_mux, | |
1199 | .put = snd_ad1848_put_mux, | |
1200 | }, | |
1201 | }; | |
1202 | snd_kcontrol_t *ctl; | |
1203 | int err; | |
1204 | ||
1205 | ctl = snd_ctl_new1(&newctls[type], chip); | |
1206 | if (! ctl) | |
1207 | return -ENOMEM; | |
1208 | strlcpy(ctl->id.name, name, sizeof(ctl->id.name)); | |
1209 | ctl->id.index = index; | |
1210 | ctl->private_value = value; | |
1211 | if ((err = snd_ctl_add(chip->card, ctl)) < 0) { | |
1212 | snd_ctl_free_one(ctl); | |
1213 | return err; | |
1214 | } | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | ||
1219 | static struct ad1848_mix_elem snd_ad1848_controls[] = { | |
1220 | AD1848_DOUBLE("PCM Playback Switch", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 7, 7, 1, 1), | |
1221 | AD1848_DOUBLE("PCM Playback Volume", 0, AD1848_LEFT_OUTPUT, AD1848_RIGHT_OUTPUT, 0, 0, 63, 1), | |
1222 | AD1848_DOUBLE("Aux Playback Switch", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 7, 7, 1, 1), | |
1223 | AD1848_DOUBLE("Aux Playback Volume", 0, AD1848_AUX1_LEFT_INPUT, AD1848_AUX1_RIGHT_INPUT, 0, 0, 31, 1), | |
1224 | AD1848_DOUBLE("Aux Playback Switch", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 7, 7, 1, 1), | |
1225 | AD1848_DOUBLE("Aux Playback Volume", 1, AD1848_AUX2_LEFT_INPUT, AD1848_AUX2_RIGHT_INPUT, 0, 0, 31, 1), | |
1226 | AD1848_DOUBLE("Capture Volume", 0, AD1848_LEFT_INPUT, AD1848_RIGHT_INPUT, 0, 0, 15, 0), | |
1227 | { | |
1228 | .name = "Capture Source", | |
1229 | .type = AD1848_MIX_CAPTURE, | |
1230 | }, | |
1231 | AD1848_SINGLE("Loopback Capture Switch", 0, AD1848_LOOPBACK, 0, 1, 0), | |
1232 | AD1848_SINGLE("Loopback Capture Volume", 0, AD1848_LOOPBACK, 1, 63, 0) | |
1233 | }; | |
1234 | ||
1235 | int snd_ad1848_mixer(ad1848_t *chip) | |
1236 | { | |
1237 | snd_card_t *card; | |
1238 | snd_pcm_t *pcm; | |
1239 | unsigned int idx; | |
1240 | int err; | |
1241 | ||
1242 | snd_assert(chip != NULL && chip->pcm != NULL, return -EINVAL); | |
1243 | ||
1244 | pcm = chip->pcm; | |
1245 | card = chip->card; | |
1246 | ||
1247 | strcpy(card->mixername, pcm->name); | |
1248 | ||
1249 | for (idx = 0; idx < ARRAY_SIZE(snd_ad1848_controls); idx++) | |
1250 | if ((err = snd_ad1848_add_ctl_elem(chip, &snd_ad1848_controls[idx])) < 0) | |
1251 | return err; | |
1252 | ||
1253 | return 0; | |
1254 | } | |
1255 | ||
1256 | EXPORT_SYMBOL(snd_ad1848_out); | |
1257 | EXPORT_SYMBOL(snd_ad1848_create); | |
1258 | EXPORT_SYMBOL(snd_ad1848_pcm); | |
1259 | EXPORT_SYMBOL(snd_ad1848_get_pcm_ops); | |
1260 | EXPORT_SYMBOL(snd_ad1848_mixer); | |
1261 | EXPORT_SYMBOL(snd_ad1848_add_ctl); | |
1262 | ||
1263 | /* | |
1264 | * INIT part | |
1265 | */ | |
1266 | ||
1267 | static int __init alsa_ad1848_init(void) | |
1268 | { | |
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | static void __exit alsa_ad1848_exit(void) | |
1273 | { | |
1274 | } | |
1275 | ||
1276 | module_init(alsa_ad1848_init) | |
1277 | module_exit(alsa_ad1848_exit) |