11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Driver for Sound Core PDAudioCF soundcard 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 71da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 81da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 91da177e4SLinus Torvalds * (at your option) any later version. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 121da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 131da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 141da177e4SLinus Torvalds * GNU General Public License for more details. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 171da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 181da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <sound/driver.h> 221da177e4SLinus Torvalds #include <sound/core.h> 231da177e4SLinus Torvalds #include "pdaudiocf.h" 241da177e4SLinus Torvalds #include <sound/initval.h> 25*97432886SAl Viro #include <asm/irq_regs.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds /* 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds */ 307d12e780SDavid Howells irqreturn_t pdacf_interrupt(int irq, void *dev) 311da177e4SLinus Torvalds { 32db131548STakashi Iwai struct snd_pdacf *chip = dev; 331da177e4SLinus Torvalds unsigned short stat; 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds if ((chip->chip_status & (PDAUDIOCF_STAT_IS_STALE| 361da177e4SLinus Torvalds PDAUDIOCF_STAT_IS_CONFIGURED| 371da177e4SLinus Torvalds PDAUDIOCF_STAT_IS_SUSPENDED)) != PDAUDIOCF_STAT_IS_CONFIGURED) 381da177e4SLinus Torvalds return IRQ_HANDLED; /* IRQ_NONE here? */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds stat = inw(chip->port + PDAUDIOCF_REG_ISR); 411da177e4SLinus Torvalds if (stat & (PDAUDIOCF_IRQLVL|PDAUDIOCF_IRQOVR)) { 421da177e4SLinus Torvalds if (stat & PDAUDIOCF_IRQOVR) /* should never happen */ 431da177e4SLinus Torvalds snd_printk(KERN_ERR "PDAUDIOCF SRAM buffer overrun detected!\n"); 441da177e4SLinus Torvalds if (chip->pcm_substream) 451da177e4SLinus Torvalds tasklet_hi_schedule(&chip->tq); 461da177e4SLinus Torvalds if (!(stat & PDAUDIOCF_IRQAKM)) 471da177e4SLinus Torvalds stat |= PDAUDIOCF_IRQAKM; /* check rate */ 481da177e4SLinus Torvalds } 497d12e780SDavid Howells if (get_irq_regs() != NULL) 501da177e4SLinus Torvalds snd_ak4117_check_rate_and_errors(chip->ak4117, 0); 511da177e4SLinus Torvalds return IRQ_HANDLED; 521da177e4SLinus Torvalds } 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds static inline void pdacf_transfer_mono16(u16 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds while (size-- > 0) { 571da177e4SLinus Torvalds *dst++ = inw(rdp_port) ^ xor; 581da177e4SLinus Torvalds inw(rdp_port); 591da177e4SLinus Torvalds } 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds static inline void pdacf_transfer_mono32(u32 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 631da177e4SLinus Torvalds { 641da177e4SLinus Torvalds register u16 val1, val2; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds while (size-- > 0) { 671da177e4SLinus Torvalds val1 = inw(rdp_port); 681da177e4SLinus Torvalds val2 = inw(rdp_port); 691da177e4SLinus Torvalds inw(rdp_port); 701da177e4SLinus Torvalds *dst++ = ((((u32)val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor; 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds } 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds static inline void pdacf_transfer_stereo16(u16 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 751da177e4SLinus Torvalds { 761da177e4SLinus Torvalds while (size-- > 0) { 771da177e4SLinus Torvalds *dst++ = inw(rdp_port) ^ xor; 781da177e4SLinus Torvalds *dst++ = inw(rdp_port) ^ xor; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static inline void pdacf_transfer_stereo32(u32 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds register u16 val1, val2, val3; 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds while (size-- > 0) { 871da177e4SLinus Torvalds val1 = inw(rdp_port); 881da177e4SLinus Torvalds val2 = inw(rdp_port); 891da177e4SLinus Torvalds val3 = inw(rdp_port); 901da177e4SLinus Torvalds *dst++ = ((((u32)val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor; 911da177e4SLinus Torvalds *dst++ = (((u32)val3 << 16) | (val2 & 0xff00)) ^ xor; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds } 941da177e4SLinus Torvalds 951da177e4SLinus Torvalds static inline void pdacf_transfer_mono16sw(u16 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 961da177e4SLinus Torvalds { 971da177e4SLinus Torvalds while (size-- > 0) { 981da177e4SLinus Torvalds *dst++ = swab16(inw(rdp_port) ^ xor); 991da177e4SLinus Torvalds inw(rdp_port); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds static inline void pdacf_transfer_mono32sw(u32 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds register u16 val1, val2; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds while (size-- > 0) { 1081da177e4SLinus Torvalds val1 = inw(rdp_port); 1091da177e4SLinus Torvalds val2 = inw(rdp_port); 1101da177e4SLinus Torvalds inw(rdp_port); 1111da177e4SLinus Torvalds *dst++ = swab32((((val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor); 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds static inline void pdacf_transfer_stereo16sw(u16 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 1161da177e4SLinus Torvalds { 1171da177e4SLinus Torvalds while (size-- > 0) { 1181da177e4SLinus Torvalds *dst++ = swab16(inw(rdp_port) ^ xor); 1191da177e4SLinus Torvalds *dst++ = swab16(inw(rdp_port) ^ xor); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds static inline void pdacf_transfer_stereo32sw(u32 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 1241da177e4SLinus Torvalds { 1251da177e4SLinus Torvalds register u16 val1, val2, val3; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds while (size-- > 0) { 1281da177e4SLinus Torvalds val1 = inw(rdp_port); 1291da177e4SLinus Torvalds val2 = inw(rdp_port); 1301da177e4SLinus Torvalds val3 = inw(rdp_port); 1311da177e4SLinus Torvalds *dst++ = swab32((((val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor); 1321da177e4SLinus Torvalds *dst++ = swab32((((u32)val3 << 16) | (val2 & 0xff00)) ^ xor); 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds static inline void pdacf_transfer_mono24le(u8 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 1371da177e4SLinus Torvalds { 1381da177e4SLinus Torvalds register u16 val1, val2; 1391da177e4SLinus Torvalds register u32 xval1; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds while (size-- > 0) { 1421da177e4SLinus Torvalds val1 = inw(rdp_port); 1431da177e4SLinus Torvalds val2 = inw(rdp_port); 1441da177e4SLinus Torvalds inw(rdp_port); 1451da177e4SLinus Torvalds xval1 = (((val2 & 0xff) << 8) | (val1 << 16)) ^ xor; 1461da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 8); 1471da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 16); 1481da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 24); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static inline void pdacf_transfer_mono24be(u8 *dst, u16 xor, unsigned int size, unsigned long rdp_port) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds register u16 val1, val2; 1551da177e4SLinus Torvalds register u32 xval1; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds while (size-- > 0) { 1581da177e4SLinus Torvalds val1 = inw(rdp_port); 1591da177e4SLinus Torvalds val2 = inw(rdp_port); 1601da177e4SLinus Torvalds inw(rdp_port); 1611da177e4SLinus Torvalds xval1 = (((val2 & 0xff) << 8) | (val1 << 16)) ^ xor; 1621da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 24); 1631da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 16); 1641da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 8); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static inline void pdacf_transfer_stereo24le(u8 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds register u16 val1, val2, val3; 1711da177e4SLinus Torvalds register u32 xval1, xval2; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds while (size-- > 0) { 1741da177e4SLinus Torvalds val1 = inw(rdp_port); 1751da177e4SLinus Torvalds val2 = inw(rdp_port); 1761da177e4SLinus Torvalds val3 = inw(rdp_port); 1771da177e4SLinus Torvalds xval1 = ((((u32)val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor; 1781da177e4SLinus Torvalds xval2 = (((u32)val3 << 16) | (val2 & 0xff00)) ^ xor; 1791da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 8); 1801da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 16); 1811da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 24); 1821da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 8); 1831da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 16); 1841da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 24); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds static inline void pdacf_transfer_stereo24be(u8 *dst, u32 xor, unsigned int size, unsigned long rdp_port) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds register u16 val1, val2, val3; 1911da177e4SLinus Torvalds register u32 xval1, xval2; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds while (size-- > 0) { 1941da177e4SLinus Torvalds val1 = inw(rdp_port); 1951da177e4SLinus Torvalds val2 = inw(rdp_port); 1961da177e4SLinus Torvalds val3 = inw(rdp_port); 1971da177e4SLinus Torvalds xval1 = ((((u32)val2 & 0xff) << 24) | ((u32)val1 << 8)) ^ xor; 1981da177e4SLinus Torvalds xval2 = (((u32)val3 << 16) | (val2 & 0xff00)) ^ xor; 1991da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 24); 2001da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 16); 2011da177e4SLinus Torvalds *dst++ = (u8)(xval1 >> 8); 2021da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 24); 2031da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 16); 2041da177e4SLinus Torvalds *dst++ = (u8)(xval2 >> 8); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 208db131548STakashi Iwai static void pdacf_transfer(struct snd_pdacf *chip, unsigned int size, unsigned int off) 2091da177e4SLinus Torvalds { 2101da177e4SLinus Torvalds unsigned long rdp_port = chip->port + PDAUDIOCF_REG_MD; 2111da177e4SLinus Torvalds unsigned int xor = chip->pcm_xor; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds if (chip->pcm_sample == 3) { 2141da177e4SLinus Torvalds if (chip->pcm_little) { 2151da177e4SLinus Torvalds if (chip->pcm_channels == 1) { 2161da177e4SLinus Torvalds pdacf_transfer_mono24le((char *)chip->pcm_area + (off * 3), xor, size, rdp_port); 2171da177e4SLinus Torvalds } else { 2181da177e4SLinus Torvalds pdacf_transfer_stereo24le((char *)chip->pcm_area + (off * 6), xor, size, rdp_port); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds } else { 2211da177e4SLinus Torvalds if (chip->pcm_channels == 1) { 2221da177e4SLinus Torvalds pdacf_transfer_mono24be((char *)chip->pcm_area + (off * 3), xor, size, rdp_port); 2231da177e4SLinus Torvalds } else { 2241da177e4SLinus Torvalds pdacf_transfer_stereo24be((char *)chip->pcm_area + (off * 6), xor, size, rdp_port); 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds return; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds if (chip->pcm_swab == 0) { 2301da177e4SLinus Torvalds if (chip->pcm_channels == 1) { 2311da177e4SLinus Torvalds if (chip->pcm_frame == 2) { 2321da177e4SLinus Torvalds pdacf_transfer_mono16((u16 *)chip->pcm_area + off, xor, size, rdp_port); 2331da177e4SLinus Torvalds } else { 2341da177e4SLinus Torvalds pdacf_transfer_mono32((u32 *)chip->pcm_area + off, xor, size, rdp_port); 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } else { 2371da177e4SLinus Torvalds if (chip->pcm_frame == 2) { 2381da177e4SLinus Torvalds pdacf_transfer_stereo16((u16 *)chip->pcm_area + (off * 2), xor, size, rdp_port); 2391da177e4SLinus Torvalds } else { 2401da177e4SLinus Torvalds pdacf_transfer_stereo32((u32 *)chip->pcm_area + (off * 2), xor, size, rdp_port); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds } else { 2441da177e4SLinus Torvalds if (chip->pcm_channels == 1) { 2451da177e4SLinus Torvalds if (chip->pcm_frame == 2) { 2461da177e4SLinus Torvalds pdacf_transfer_mono16sw((u16 *)chip->pcm_area + off, xor, size, rdp_port); 2471da177e4SLinus Torvalds } else { 2481da177e4SLinus Torvalds pdacf_transfer_mono32sw((u32 *)chip->pcm_area + off, xor, size, rdp_port); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds } else { 2511da177e4SLinus Torvalds if (chip->pcm_frame == 2) { 2521da177e4SLinus Torvalds pdacf_transfer_stereo16sw((u16 *)chip->pcm_area + (off * 2), xor, size, rdp_port); 2531da177e4SLinus Torvalds } else { 2541da177e4SLinus Torvalds pdacf_transfer_stereo32sw((u32 *)chip->pcm_area + (off * 2), xor, size, rdp_port); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds void pdacf_tasklet(unsigned long private_data) 2611da177e4SLinus Torvalds { 262db131548STakashi Iwai struct snd_pdacf *chip = (struct snd_pdacf *) private_data; 2631da177e4SLinus Torvalds int size, off, cont, rdp, wdp; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds if ((chip->chip_status & (PDAUDIOCF_STAT_IS_STALE|PDAUDIOCF_STAT_IS_CONFIGURED)) != PDAUDIOCF_STAT_IS_CONFIGURED) 2661da177e4SLinus Torvalds return; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (chip->pcm_substream == NULL || chip->pcm_substream->runtime == NULL || !snd_pcm_running(chip->pcm_substream)) 2691da177e4SLinus Torvalds return; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds rdp = inw(chip->port + PDAUDIOCF_REG_RDP); 2721da177e4SLinus Torvalds wdp = inw(chip->port + PDAUDIOCF_REG_WDP); 2731da177e4SLinus Torvalds // printk("TASKLET: rdp = %x, wdp = %x\n", rdp, wdp); 2741da177e4SLinus Torvalds size = wdp - rdp; 2751da177e4SLinus Torvalds if (size < 0) 2761da177e4SLinus Torvalds size += 0x10000; 2771da177e4SLinus Torvalds if (size == 0) 2781da177e4SLinus Torvalds size = 0x10000; 2791da177e4SLinus Torvalds size /= chip->pcm_frame; 2801da177e4SLinus Torvalds if (size > 64) 2811da177e4SLinus Torvalds size -= 32; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds #if 0 2841da177e4SLinus Torvalds chip->pcm_hwptr += size; 2851da177e4SLinus Torvalds chip->pcm_hwptr %= chip->pcm_size; 2861da177e4SLinus Torvalds chip->pcm_tdone += size; 2871da177e4SLinus Torvalds if (chip->pcm_frame == 2) { 2881da177e4SLinus Torvalds unsigned long rdp_port = chip->port + PDAUDIOCF_REG_MD; 2891da177e4SLinus Torvalds while (size-- > 0) { 2901da177e4SLinus Torvalds inw(rdp_port); 2911da177e4SLinus Torvalds inw(rdp_port); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds } else { 2941da177e4SLinus Torvalds unsigned long rdp_port = chip->port + PDAUDIOCF_REG_MD; 2951da177e4SLinus Torvalds while (size-- > 0) { 2961da177e4SLinus Torvalds inw(rdp_port); 2971da177e4SLinus Torvalds inw(rdp_port); 2981da177e4SLinus Torvalds inw(rdp_port); 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds #else 3021da177e4SLinus Torvalds off = chip->pcm_hwptr + chip->pcm_tdone; 3031da177e4SLinus Torvalds off %= chip->pcm_size; 3041da177e4SLinus Torvalds chip->pcm_tdone += size; 3051da177e4SLinus Torvalds while (size > 0) { 3061da177e4SLinus Torvalds cont = chip->pcm_size - off; 3071da177e4SLinus Torvalds if (cont > size) 3081da177e4SLinus Torvalds cont = size; 3091da177e4SLinus Torvalds pdacf_transfer(chip, cont, off); 3101da177e4SLinus Torvalds off += cont; 3111da177e4SLinus Torvalds off %= chip->pcm_size; 3121da177e4SLinus Torvalds size -= cont; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds #endif 3151da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3161da177e4SLinus Torvalds while (chip->pcm_tdone >= chip->pcm_period) { 3171da177e4SLinus Torvalds chip->pcm_hwptr += chip->pcm_period; 3181da177e4SLinus Torvalds chip->pcm_hwptr %= chip->pcm_size; 3191da177e4SLinus Torvalds chip->pcm_tdone -= chip->pcm_period; 3201da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3211da177e4SLinus Torvalds snd_pcm_period_elapsed(chip->pcm_substream); 3221da177e4SLinus Torvalds spin_lock(&chip->reg_lock); 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds spin_unlock(&chip->reg_lock); 3251da177e4SLinus Torvalds // printk("TASKLET: end\n"); 3261da177e4SLinus Torvalds } 327