📄 ak4117.c
字号:
/* * Routines for control of the AK4117 via 4-wire serial interface * IEC958 (S/PDIF) receiver by Asahi Kasei * Copyright (c) by Jaroslav Kysela <perex@suse.cz> * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */#include <sound/driver.h>#include <linux/slab.h>#include <linux/delay.h>#include <sound/core.h>#include <sound/control.h>#include <sound/pcm.h>#include <sound/ak4117.h>#include <sound/asoundef.h>MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");MODULE_DESCRIPTION("AK4117 IEC958 (S/PDIF) receiver by Asahi Kasei");MODULE_LICENSE("GPL");#define AK4117_ADDR 0x00 /* fixed address */static void snd_ak4117_timer(unsigned long data);static void reg_write(ak4117_t *ak4117, unsigned char reg, unsigned char val){ ak4117->write(ak4117->private_data, reg, val); if (reg < sizeof(ak4117->regmap)) ak4117->regmap[reg] = val;}static inline unsigned char reg_read(ak4117_t *ak4117, unsigned char reg){ return ak4117->read(ak4117->private_data, reg);}#if 0static void reg_dump(ak4117_t *ak4117){ int i; printk(KERN_DEBUG "AK4117 REG DUMP:\n"); for (i = 0; i < 0x1b; i++) printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4117, i), i < sizeof(ak4117->regmap) ? ak4117->regmap[i] : 0);}#endifstatic void snd_ak4117_free(ak4117_t *chip){ del_timer(&chip->timer); kfree(chip);}static int snd_ak4117_dev_free(snd_device_t *device){ ak4117_t *chip = device->device_data; snd_ak4117_free(chip); return 0;}int snd_ak4117_create(snd_card_t *card, ak4117_read_t *read, ak4117_write_t *write, unsigned char pgm[5], void *private_data, ak4117_t **r_ak4117){ ak4117_t *chip; int err = 0; unsigned char reg; static snd_device_ops_t ops = { .dev_free = snd_ak4117_dev_free, }; chip = kzalloc(sizeof(*chip), GFP_KERNEL); if (chip == NULL) return -ENOMEM; spin_lock_init(&chip->lock); chip->card = card; chip->read = read; chip->write = write; chip->private_data = private_data; init_timer(&chip->timer); chip->timer.data = (unsigned long)chip; chip->timer.function = snd_ak4117_timer; for (reg = 0; reg < 5; reg++) chip->regmap[reg] = pgm[reg]; snd_ak4117_reinit(chip); chip->rcs0 = reg_read(chip, AK4117_REG_RCS0) & ~(AK4117_QINT | AK4117_CINT | AK4117_STC); chip->rcs1 = reg_read(chip, AK4117_REG_RCS1); chip->rcs2 = reg_read(chip, AK4117_REG_RCS2); if ((err = snd_device_new(card, SNDRV_DEV_CODEC, chip, &ops)) < 0) goto __fail; if (r_ak4117) *r_ak4117 = chip; return 0; __fail: snd_ak4117_free(chip); return err < 0 ? err : -EIO;}void snd_ak4117_reg_write(ak4117_t *chip, unsigned char reg, unsigned char mask, unsigned char val){ if (reg >= 5) return; reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);}void snd_ak4117_reinit(ak4117_t *chip){ unsigned char old = chip->regmap[AK4117_REG_PWRDN], reg; del_timer(&chip->timer); chip->init = 1; /* bring the chip to reset state and powerdown state */ reg_write(chip, AK4117_REG_PWRDN, 0); udelay(200); /* release reset, but leave powerdown */ reg_write(chip, AK4117_REG_PWRDN, (old | AK4117_RST) & ~AK4117_PWN); udelay(200); for (reg = 1; reg < 5; reg++) reg_write(chip, reg, chip->regmap[reg]); /* release powerdown, everything is initialized now */ reg_write(chip, AK4117_REG_PWRDN, old | AK4117_RST | AK4117_PWN); chip->init = 0; chip->timer.expires = 1 + jiffies; add_timer(&chip->timer);}static unsigned int external_rate(unsigned char rcs1){ switch (rcs1 & (AK4117_FS0|AK4117_FS1|AK4117_FS2|AK4117_FS3)) { case AK4117_FS_32000HZ: return 32000; case AK4117_FS_44100HZ: return 44100; case AK4117_FS_48000HZ: return 48000; case AK4117_FS_88200HZ: return 88200; case AK4117_FS_96000HZ: return 96000; case AK4117_FS_176400HZ: return 176400; case AK4117_FS_192000HZ: return 192000; default: return 0; }}static int snd_ak4117_in_error_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = LONG_MAX; return 0;}static int snd_ak4117_in_error_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); long *ptr; spin_lock_irq(&chip->lock); ptr = (long *)(((char *)chip) + kcontrol->private_value); ucontrol->value.integer.value[0] = *ptr; *ptr = 0; spin_unlock_irq(&chip->lock); return 0;}static int snd_ak4117_in_bit_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 1; return 0;}static int snd_ak4117_in_bit_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); unsigned char reg = kcontrol->private_value & 0xff; unsigned char bit = (kcontrol->private_value >> 8) & 0xff; unsigned char inv = (kcontrol->private_value >> 31) & 1; ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv; return 0;}static int snd_ak4117_rx_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 1; return 0;}static int snd_ak4117_rx_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = (chip->regmap[AK4117_REG_IO] & AK4117_IPS) ? 1 : 0; return 0;}static int snd_ak4117_rx_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); int change; u8 old_val; spin_lock_irq(&chip->lock); old_val = chip->regmap[AK4117_REG_IO]; change = !!ucontrol->value.integer.value[0] != ((old_val & AK4117_IPS) ? 1 : 0); if (change) reg_write(chip, AK4117_REG_IO, (old_val & ~AK4117_IPS) | (ucontrol->value.integer.value[0] ? AK4117_IPS : 0)); spin_unlock_irq(&chip->lock); return change;}static int snd_ak4117_rate_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; uinfo->count = 1; uinfo->value.integer.min = 0; uinfo->value.integer.max = 192000; return 0;}static int snd_ak4117_rate_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4117_REG_RCS1)); return 0;}static int snd_ak4117_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; uinfo->count = 1; return 0;}static int snd_ak4117_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol){ ak4117_t *chip = snd_kcontrol_chip(kcontrol); unsigned i; for (i = 0; i < AK4117_REG_RXCSB_SIZE; i++) ucontrol->value.iec958.status[i] = reg_read(chip, AK4117_REG_RXCSB0 + i); return 0;}static int snd_ak4117_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo){ uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -