11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Driver for Sound Core PDAudioCF soundcard
41da177e4SLinus Torvalds *
5c1017a4cSJaroslav Kysela * Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/delay.h>
95a0e3ad6STejun Heo #include <linux/slab.h>
101da177e4SLinus Torvalds #include <sound/core.h>
111da177e4SLinus Torvalds #include <sound/info.h>
121da177e4SLinus Torvalds #include "pdaudiocf.h"
131da177e4SLinus Torvalds #include <sound/initval.h>
141da177e4SLinus Torvalds
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds */
pdacf_ak4117_read(void * private_data,unsigned char reg)181da177e4SLinus Torvalds static unsigned char pdacf_ak4117_read(void *private_data, unsigned char reg)
191da177e4SLinus Torvalds {
20db131548STakashi Iwai struct snd_pdacf *chip = private_data;
211da177e4SLinus Torvalds unsigned long timeout;
221da177e4SLinus Torvalds unsigned long flags;
231da177e4SLinus Torvalds unsigned char res;
241da177e4SLinus Torvalds
251da177e4SLinus Torvalds spin_lock_irqsave(&chip->ak4117_lock, flags);
261da177e4SLinus Torvalds timeout = 1000;
271da177e4SLinus Torvalds while (pdacf_reg_read(chip, PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
281da177e4SLinus Torvalds udelay(5);
291da177e4SLinus Torvalds if (--timeout == 0) {
301da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->ak4117_lock, flags);
311da177e4SLinus Torvalds snd_printk(KERN_ERR "AK4117 ready timeout (read)\n");
321da177e4SLinus Torvalds return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds }
351da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_AK_IFR, (u16)reg << 8);
361da177e4SLinus Torvalds timeout = 1000;
371da177e4SLinus Torvalds while (pdacf_reg_read(chip, PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
381da177e4SLinus Torvalds udelay(5);
391da177e4SLinus Torvalds if (--timeout == 0) {
401da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->ak4117_lock, flags);
411da177e4SLinus Torvalds snd_printk(KERN_ERR "AK4117 read timeout (read2)\n");
421da177e4SLinus Torvalds return 0;
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds res = (unsigned char)pdacf_reg_read(chip, PDAUDIOCF_REG_AK_IFR);
461da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->ak4117_lock, flags);
471da177e4SLinus Torvalds return res;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds
pdacf_ak4117_write(void * private_data,unsigned char reg,unsigned char val)501da177e4SLinus Torvalds static void pdacf_ak4117_write(void *private_data, unsigned char reg, unsigned char val)
511da177e4SLinus Torvalds {
52db131548STakashi Iwai struct snd_pdacf *chip = private_data;
531da177e4SLinus Torvalds unsigned long timeout;
541da177e4SLinus Torvalds unsigned long flags;
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds spin_lock_irqsave(&chip->ak4117_lock, flags);
571da177e4SLinus Torvalds timeout = 1000;
581da177e4SLinus Torvalds while (inw(chip->port + PDAUDIOCF_REG_SCR) & PDAUDIOCF_AK_SBP) {
591da177e4SLinus Torvalds udelay(5);
601da177e4SLinus Torvalds if (--timeout == 0) {
611da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->ak4117_lock, flags);
621da177e4SLinus Torvalds snd_printk(KERN_ERR "AK4117 ready timeout (write)\n");
631da177e4SLinus Torvalds return;
641da177e4SLinus Torvalds }
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds outw((u16)reg << 8 | val | (1<<13), chip->port + PDAUDIOCF_REG_AK_IFR);
671da177e4SLinus Torvalds spin_unlock_irqrestore(&chip->ak4117_lock, flags);
681da177e4SLinus Torvalds }
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds #if 0
71db131548STakashi Iwai void pdacf_dump(struct snd_pdacf *chip)
721da177e4SLinus Torvalds {
732ebfb8eeSTakashi Iwai printk(KERN_DEBUG "PDAUDIOCF DUMP (0x%lx):\n", chip->port);
742ebfb8eeSTakashi Iwai printk(KERN_DEBUG "WPD : 0x%x\n",
752ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_WDP));
762ebfb8eeSTakashi Iwai printk(KERN_DEBUG "RDP : 0x%x\n",
772ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_RDP));
782ebfb8eeSTakashi Iwai printk(KERN_DEBUG "TCR : 0x%x\n",
792ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_TCR));
802ebfb8eeSTakashi Iwai printk(KERN_DEBUG "SCR : 0x%x\n",
812ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_SCR));
822ebfb8eeSTakashi Iwai printk(KERN_DEBUG "ISR : 0x%x\n",
832ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_ISR));
842ebfb8eeSTakashi Iwai printk(KERN_DEBUG "IER : 0x%x\n",
852ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_IER));
862ebfb8eeSTakashi Iwai printk(KERN_DEBUG "AK_IFR : 0x%x\n",
872ebfb8eeSTakashi Iwai inw(chip->port + PDAUDIOCF_REG_AK_IFR));
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds #endif
901da177e4SLinus Torvalds
pdacf_reset(struct snd_pdacf * chip,int powerdown)91db131548STakashi Iwai static int pdacf_reset(struct snd_pdacf *chip, int powerdown)
921da177e4SLinus Torvalds {
931da177e4SLinus Torvalds u16 val;
941da177e4SLinus Torvalds
951da177e4SLinus Torvalds val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
961da177e4SLinus Torvalds val |= PDAUDIOCF_PDN;
971da177e4SLinus Torvalds val &= ~PDAUDIOCF_RECORD; /* for sure */
981da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
991da177e4SLinus Torvalds udelay(5);
1001da177e4SLinus Torvalds val |= PDAUDIOCF_RST;
1011da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
1021da177e4SLinus Torvalds udelay(200);
1031da177e4SLinus Torvalds val &= ~PDAUDIOCF_RST;
1041da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
1051da177e4SLinus Torvalds udelay(5);
1061da177e4SLinus Torvalds if (!powerdown) {
1071da177e4SLinus Torvalds val &= ~PDAUDIOCF_PDN;
1081da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
1091da177e4SLinus Torvalds udelay(200);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds return 0;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds
pdacf_reinit(struct snd_pdacf * chip,int resume)114db131548STakashi Iwai void pdacf_reinit(struct snd_pdacf *chip, int resume)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds pdacf_reset(chip, 0);
1171da177e4SLinus Torvalds if (resume)
1181da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, chip->suspend_reg_scr);
1191da177e4SLinus Torvalds snd_ak4117_reinit(chip->ak4117);
1201da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_TCR, chip->regmap[PDAUDIOCF_REG_TCR>>1]);
1211da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_IER, chip->regmap[PDAUDIOCF_REG_IER>>1]);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds
pdacf_proc_read(struct snd_info_entry * entry,struct snd_info_buffer * buffer)124db131548STakashi Iwai static void pdacf_proc_read(struct snd_info_entry * entry,
125db131548STakashi Iwai struct snd_info_buffer *buffer)
1261da177e4SLinus Torvalds {
127db131548STakashi Iwai struct snd_pdacf *chip = entry->private_data;
1281da177e4SLinus Torvalds u16 tmp;
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds snd_iprintf(buffer, "PDAudioCF\n\n");
1311da177e4SLinus Torvalds tmp = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
1321da177e4SLinus Torvalds snd_iprintf(buffer, "FPGA revision : 0x%x\n", PDAUDIOCF_FPGAREV(tmp));
1331da177e4SLinus Torvalds
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds
pdacf_proc_init(struct snd_pdacf * chip)136db131548STakashi Iwai static void pdacf_proc_init(struct snd_pdacf *chip)
1371da177e4SLinus Torvalds {
13850a7a8e9STakashi Iwai snd_card_ro_proc_new(chip->card, "pdaudiocf", chip, pdacf_proc_read);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds
snd_pdacf_create(struct snd_card * card)141db131548STakashi Iwai struct snd_pdacf *snd_pdacf_create(struct snd_card *card)
1421da177e4SLinus Torvalds {
143db131548STakashi Iwai struct snd_pdacf *chip;
1441da177e4SLinus Torvalds
145561b220aSTakashi Iwai chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1461da177e4SLinus Torvalds if (chip == NULL)
1471da177e4SLinus Torvalds return NULL;
1481da177e4SLinus Torvalds chip->card = card;
1493b73cfe5STakashi Iwai mutex_init(&chip->reg_lock);
1501da177e4SLinus Torvalds spin_lock_init(&chip->ak4117_lock);
1511da177e4SLinus Torvalds card->private_data = chip;
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds pdacf_proc_init(chip);
1541da177e4SLinus Torvalds return chip;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
snd_pdacf_ak4117_change(struct ak4117 * ak4117,unsigned char c0,unsigned char c1)157db131548STakashi Iwai static void snd_pdacf_ak4117_change(struct ak4117 *ak4117, unsigned char c0, unsigned char c1)
1581da177e4SLinus Torvalds {
159db131548STakashi Iwai struct snd_pdacf *chip = ak4117->change_callback_private;
1601da177e4SLinus Torvalds u16 val;
1611da177e4SLinus Torvalds
1621da177e4SLinus Torvalds if (!(c0 & AK4117_UNLCK))
1631da177e4SLinus Torvalds return;
1643b73cfe5STakashi Iwai mutex_lock(&chip->reg_lock);
1651da177e4SLinus Torvalds val = chip->regmap[PDAUDIOCF_REG_SCR>>1];
1661da177e4SLinus Torvalds if (ak4117->rcs0 & AK4117_UNLCK)
1671da177e4SLinus Torvalds val |= PDAUDIOCF_BLUE_LED_OFF;
1681da177e4SLinus Torvalds else
1691da177e4SLinus Torvalds val &= ~PDAUDIOCF_BLUE_LED_OFF;
1701da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
1713b73cfe5STakashi Iwai mutex_unlock(&chip->reg_lock);
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds
snd_pdacf_ak4117_create(struct snd_pdacf * chip)174db131548STakashi Iwai int snd_pdacf_ak4117_create(struct snd_pdacf *chip)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds int err;
1771da177e4SLinus Torvalds u16 val;
1781da177e4SLinus Torvalds /* design note: if we unmask PLL unlock, parity, valid, audio or auto bit interrupts */
1791da177e4SLinus Torvalds /* from AK4117 then INT1 pin from AK4117 will be high all time, because PCMCIA interrupts are */
1801da177e4SLinus Torvalds /* egde based and FPGA does logical OR for all interrupt sources, we cannot use these */
1811da177e4SLinus Torvalds /* high-rate sources */
182*6ab9eabeSTakashi Iwai static const unsigned char pgm[5] = {
1831da177e4SLinus Torvalds AK4117_XTL_24_576M | AK4117_EXCT, /* AK4117_REG_PWRDN */
1841da177e4SLinus Torvalds AK4117_CM_PLL_XTAL | AK4117_PKCS_128fs | AK4117_XCKS_128fs, /* AK4117_REQ_CLOCK */
1851da177e4SLinus Torvalds AK4117_EFH_1024LRCLK | AK4117_DIF_24R | AK4117_IPS, /* AK4117_REG_IO */
1861da177e4SLinus Torvalds 0xff, /* AK4117_REG_INT0_MASK */
1871da177e4SLinus Torvalds AK4117_MAUTO | AK4117_MAUD | AK4117_MULK | AK4117_MPAR | AK4117_MV, /* AK4117_REG_INT1_MASK */
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds err = pdacf_reset(chip, 0);
1911da177e4SLinus Torvalds if (err < 0)
1921da177e4SLinus Torvalds return err;
1931da177e4SLinus Torvalds err = snd_ak4117_create(chip->card, pdacf_ak4117_read, pdacf_ak4117_write, pgm, chip, &chip->ak4117);
1941da177e4SLinus Torvalds if (err < 0)
1951da177e4SLinus Torvalds return err;
1961da177e4SLinus Torvalds
1971da177e4SLinus Torvalds val = pdacf_reg_read(chip, PDAUDIOCF_REG_TCR);
1981da177e4SLinus Torvalds #if 1 /* normal operation */
1991da177e4SLinus Torvalds val &= ~(PDAUDIOCF_ELIMAKMBIT|PDAUDIOCF_TESTDATASEL);
2001da177e4SLinus Torvalds #else /* debug */
2011da177e4SLinus Torvalds val |= PDAUDIOCF_ELIMAKMBIT;
2021da177e4SLinus Torvalds val &= ~PDAUDIOCF_TESTDATASEL;
2031da177e4SLinus Torvalds #endif
2041da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_TCR, val);
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds /* setup the FPGA to match AK4117 setup */
2071da177e4SLinus Torvalds val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
2081da177e4SLinus Torvalds val &= ~(PDAUDIOCF_CLKDIV0 | PDAUDIOCF_CLKDIV1); /* use 24.576Mhz clock */
2091da177e4SLinus Torvalds val &= ~(PDAUDIOCF_RED_LED_OFF|PDAUDIOCF_BLUE_LED_OFF);
2101da177e4SLinus Torvalds val |= PDAUDIOCF_DATAFMT0 | PDAUDIOCF_DATAFMT1; /* 24-bit data */
2111da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
2121da177e4SLinus Torvalds
2131da177e4SLinus Torvalds /* setup LEDs and IRQ */
2141da177e4SLinus Torvalds val = pdacf_reg_read(chip, PDAUDIOCF_REG_IER);
2151da177e4SLinus Torvalds val &= ~(PDAUDIOCF_IRQLVLEN0 | PDAUDIOCF_IRQLVLEN1);
2161da177e4SLinus Torvalds val &= ~(PDAUDIOCF_BLUEDUTY0 | PDAUDIOCF_REDDUTY0 | PDAUDIOCF_REDDUTY1);
2171da177e4SLinus Torvalds val |= PDAUDIOCF_BLUEDUTY1 | PDAUDIOCF_HALFRATE;
2181da177e4SLinus Torvalds val |= PDAUDIOCF_IRQOVREN | PDAUDIOCF_IRQAKMEN;
2191da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_IER, val);
2201da177e4SLinus Torvalds
2211da177e4SLinus Torvalds chip->ak4117->change_callback_private = chip;
2221da177e4SLinus Torvalds chip->ak4117->change_callback = snd_pdacf_ak4117_change;
2231da177e4SLinus Torvalds
2241da177e4SLinus Torvalds /* update LED status */
2251da177e4SLinus Torvalds snd_pdacf_ak4117_change(chip->ak4117, AK4117_UNLCK, 0);
2261da177e4SLinus Torvalds
2271da177e4SLinus Torvalds return 0;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds
snd_pdacf_powerdown(struct snd_pdacf * chip)230db131548STakashi Iwai void snd_pdacf_powerdown(struct snd_pdacf *chip)
2311da177e4SLinus Torvalds {
2321da177e4SLinus Torvalds u16 val;
2331da177e4SLinus Torvalds
2341da177e4SLinus Torvalds val = pdacf_reg_read(chip, PDAUDIOCF_REG_SCR);
2351da177e4SLinus Torvalds chip->suspend_reg_scr = val;
2361da177e4SLinus Torvalds val |= PDAUDIOCF_RED_LED_OFF | PDAUDIOCF_BLUE_LED_OFF;
2371da177e4SLinus Torvalds pdacf_reg_write(chip, PDAUDIOCF_REG_SCR, val);
2381da177e4SLinus Torvalds /* disable interrupts, but use direct write to preserve old register value in chip->regmap */
2391da177e4SLinus Torvalds val = inw(chip->port + PDAUDIOCF_REG_IER);
2401da177e4SLinus Torvalds val &= ~(PDAUDIOCF_IRQOVREN|PDAUDIOCF_IRQAKMEN|PDAUDIOCF_IRQLVLEN0|PDAUDIOCF_IRQLVLEN1);
2411da177e4SLinus Torvalds outw(val, chip->port + PDAUDIOCF_REG_IER);
2421da177e4SLinus Torvalds pdacf_reset(chip, 1);
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds
2451da177e4SLinus Torvalds #ifdef CONFIG_PM
2461da177e4SLinus Torvalds
snd_pdacf_suspend(struct snd_pdacf * chip)2472cb1331dSTakashi Iwai int snd_pdacf_suspend(struct snd_pdacf *chip)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds u16 val;
2501da177e4SLinus Torvalds
251e4f163d9STakashi Iwai snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
2521da177e4SLinus Torvalds /* disable interrupts, but use direct write to preserve old register value in chip->regmap */
2531da177e4SLinus Torvalds val = inw(chip->port + PDAUDIOCF_REG_IER);
2541da177e4SLinus Torvalds val &= ~(PDAUDIOCF_IRQOVREN|PDAUDIOCF_IRQAKMEN|PDAUDIOCF_IRQLVLEN0|PDAUDIOCF_IRQLVLEN1);
2551da177e4SLinus Torvalds outw(val, chip->port + PDAUDIOCF_REG_IER);
2561da177e4SLinus Torvalds chip->chip_status |= PDAUDIOCF_STAT_IS_SUSPENDED; /* ignore interrupts from now */
2571da177e4SLinus Torvalds snd_pdacf_powerdown(chip);
2581da177e4SLinus Torvalds return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds
check_signal(struct snd_pdacf * chip)261db131548STakashi Iwai static inline int check_signal(struct snd_pdacf *chip)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds return (chip->ak4117->rcs0 & AK4117_UNLCK) == 0;
2641da177e4SLinus Torvalds }
2651da177e4SLinus Torvalds
snd_pdacf_resume(struct snd_pdacf * chip)266e4f163d9STakashi Iwai int snd_pdacf_resume(struct snd_pdacf *chip)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds int timeout = 40;
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvalds pdacf_reinit(chip, 1);
2711da177e4SLinus Torvalds /* wait for AK4117's PLL */
2721da177e4SLinus Torvalds while (timeout-- > 0 &&
2731da177e4SLinus Torvalds (snd_ak4117_external_rate(chip->ak4117) <= 0 || !check_signal(chip)))
2741da177e4SLinus Torvalds mdelay(1);
2751da177e4SLinus Torvalds chip->chip_status &= ~PDAUDIOCF_STAT_IS_SUSPENDED;
276e4f163d9STakashi Iwai snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
2771da177e4SLinus Torvalds return 0;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds #endif
280