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