freebsd-dev/sys/dev/sound/pci/spicds.c

366 lines
10 KiB
C
Raw Normal View History

2010-03-29 20:27:17 +00:00
/*-
* Copyright (c) 2006 Konstantin Dimitrov <kosio.dimitrov@gmail.com>
* Copyright (c) 2001 Katsurajima Naoto <raven@katsurajima.seya.yokohama.jp>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
Sound Mega-commit. Expect further cleanup until code freeze. For a slightly thorough explaination, please refer to [1] http://people.freebsd.org/~ariff/SOUND_4.TXT.html . Summary of changes includes: 1 Volume Per-Channel (vpc). Provides private / standalone volume control unique per-stream pcm channel without touching master volume / pcm. Applications can directly use SNDCTL_DSP_[GET|SET][PLAY|REC]VOL, or for backwards compatibility, SOUND_MIXER_PCM through the opened dsp device instead of /dev/mixer. Special "bypass" mode is enabled through /dev/mixer which will automatically detect if the adjustment is made through /dev/mixer and forward its request to this private volume controller. Changes to this volume object will not interfere with other channels. Requirements: - SNDCTL_DSP_[GET|SET][PLAY|REC]_VOL are newer ioctls (OSSv4) which require specific application modifications (preferred). - No modifications required for using bypass mode, so applications like mplayer or xmms should work out of the box. Kernel hints: - hint.pcm.%d.vpc (0 = disable vpc). Kernel sysctls: - hw.snd.vpc_mixer_bypass (default: 1). Enable or disable /dev/mixer bypass mode. - hw.snd.vpc_autoreset (default: 1). By default, closing/opening /dev/dsp will reset the volume back to 0 db gain/attenuation. Setting this to 0 will preserve its settings across device closing/opening. - hw.snd.vpc_reset (default: 0). Panic/reset button to reset all volume settings back to 0 db. - hw.snd.vpc_0db (default: 45). 0 db relative to linear mixer value. 2 High quality fixed-point Bandlimited SINC sampling rate converter, based on Julius O'Smith's Digital Audio Resampling - http://ccrma.stanford.edu/~jos/resample/. It includes a filter design script written in awk (the clumsiest joke I've ever written) - 100% 32bit fixed-point, 64bit accumulator. - Possibly among the fastest (if not fastest) of its kind. - Resampling quality is tunable, either runtime or during kernel compilation (FEEDER_RATE_PRESETS). - Quality can be further customized during kernel compilation by defining FEEDER_RATE_PRESETS in /etc/make.conf. Kernel sysctls: - hw.snd.feeder_rate_quality. 0 - Zero-order Hold (ZOH). Fastest, bad quality. 1 - Linear Interpolation (LINEAR). Slightly slower than ZOH, better quality but still does not eliminate aliasing. 2 - (and above) - Sinc Interpolation(SINC). Best quality. SINC quality always start from 2 and above. Rough quality comparisons: - http://people.freebsd.org/~ariff/z_comparison/ 3 Bit-perfect mode. Bypasses all feeder/dsp effects. Pure sound will be directly fed into the hardware. 4 Parametric (compile time) Software Equalizer (Bass/Treble mixer). Can be customized by defining FEEDER_EQ_PRESETS in /etc/make.conf. 5 Transparent/Adaptive Virtual Channel. Now you don't have to disable vchans in order to make digital format pass through. It also makes vchans more dynamic by choosing a better format/rate among all the concurrent streams, which means that dev.pcm.X.play.vchanformat/rate becomes sort of optional. 6 Exclusive Stream, with special open() mode O_EXCL. This will "mute" other concurrent vchan streams and only allow a single channel with O_EXCL set to keep producing sound. Other Changes: * most feeder_* stuffs are compilable in userland. Let's not speculate whether we should go all out for it (save that for FreeBSD 16.0-RELEASE). * kobj signature fixups, thanks to Andriy Gapon <avg@freebsd.org> * pull out channel mixing logic out of vchan.c and create its own feeder_mixer for world justice. * various refactoring here and there, for good or bad. * activation of few more OSSv4 ioctls() (see [1] above). * opt_snd.h for possible compile time configuration: (mostly for debugging purposes, don't try these at home) SND_DEBUG SND_DIAGNOSTIC SND_FEEDER_MULTIFORMAT SND_FEEDER_FULL_MULTIFORMAT SND_FEEDER_RATE_HP SND_PCM_64 SND_OLDSTEREO Manual page updates are on the way. Tested by: joel, Olivier SMEDTS <olivier at gid0 d org>, too many unsung / unnamed heroes.
2009-06-07 19:12:08 +00:00
#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif
#include <dev/sound/pcm/sound.h>
#include <dev/sound/pci/spicds.h>
MALLOC_DEFINE(M_SPICDS, "spicds", "SPI codec");
#define SPICDS_NAMELEN 16
struct spicds_info {
device_t dev;
spicds_ctrl ctrl;
void *devinfo;
int num; /* number of this device */
unsigned int type; /* codec type */
unsigned int cif; /* Controll data Interface Format (0/1) */
unsigned int format; /* data format and master clock frequency */
unsigned int dvc; /* De-emphasis and Volume Control */
unsigned int left, right;
char name[SPICDS_NAMELEN];
struct mtx *lock;
};
static void
spicds_wrbit(struct spicds_info *codec, int bit)
{
unsigned int cs, cdti;
if (codec->cif)
cs = 1;
else
cs = 0;
if (bit)
cdti = 1;
else
cdti = 0;
codec->ctrl(codec->devinfo, cs, 0, cdti);
DELAY(1);
codec->ctrl(codec->devinfo, cs, 1, cdti);
DELAY(1);
return;
}
static void
spicds_wrcd(struct spicds_info *codec, int reg, u_int16_t val)
{
int mask;
#if(0)
device_printf(codec->dev, "spicds_wrcd(codec, 0x%02x, 0x%02x)\n", reg, val);
#endif
/* start */
if (codec->cif)
codec->ctrl(codec->devinfo, 1, 1, 0);
else
codec->ctrl(codec->devinfo, 0, 1, 0);
DELAY(1);
if (codec->type != SPICDS_TYPE_WM8770) {
if (codec->type == SPICDS_TYPE_AK4381) {
/* AK4381 chip address */
spicds_wrbit(codec, 0);
spicds_wrbit(codec, 1);
}
else if (codec->type == SPICDS_TYPE_AK4396)
{
/* AK4396 chip address */
spicds_wrbit(codec, 0);
spicds_wrbit(codec, 0);
}
else {
/* chip address */
spicds_wrbit(codec, 1);
spicds_wrbit(codec, 0);
}
/* write */
spicds_wrbit(codec, 1);
/* register address */
for (mask = 0x10; mask != 0; mask >>= 1)
spicds_wrbit(codec, reg & mask);
/* data */
for (mask = 0x80; mask != 0; mask >>= 1)
spicds_wrbit(codec, val & mask);
/* stop */
DELAY(1);
}
else {
/* register address */
for (mask = 0x40; mask != 0; mask >>= 1)
spicds_wrbit(codec, reg & mask);
/* data */
for (mask = 0x100; mask != 0; mask >>= 1)
spicds_wrbit(codec, val & mask);
/* stop */
DELAY(1);
}
if (codec->cif) {
codec->ctrl(codec->devinfo, 0, 1, 0);
DELAY(1);
codec->ctrl(codec->devinfo, 1, 1, 0);
}
else {
codec->ctrl(codec->devinfo, 1, 1, 0);
}
return;
}
struct spicds_info *
spicds_create(device_t dev, void *devinfo, int num, spicds_ctrl ctrl)
{
struct spicds_info *codec;
#if(0)
device_printf(dev, "spicds_create(dev, devinfo, %d, ctrl)\n", num);
#endif
codec = (struct spicds_info *)malloc(sizeof *codec, M_SPICDS, M_NOWAIT);
if (codec == NULL)
return NULL;
snprintf(codec->name, SPICDS_NAMELEN, "%s:spicds%d", device_get_nameunit(dev), num);
- fix compatibility with newer versions of FreeBSD - fix all warnings during compilation - fix obvious bugs - add support for more cards Now supported: - M-Audio Delta Dio 2496 - M-Audio Audiophile 2496 - Terratec DMX 6fire Known bugs (detected by Nokolas and Stefan): - $ kldunload snd_ak452x.ko Warning: memory type ak452x leaked memory on destroy (1 allocations, 64 bytes leaked). - No sound in KDE: Everything works fine at the console but when I load KDE (3.5.3) the sound stutters and plays at less then 1/2 speed. - 'mixer: WRITE_MIXER: Device not configured' The message repeats x times at system startup, x = whatever hw.snd.maxautovchans is set to. (this is because only vol, pcm and line are supported, but the driver shows more than those mixer devices and setting those additional mixers results in this error message) - vchans don't work - 24 bit playback not supported (only 16/32 bit) - after kld(un)loading some times, the card fails to be probed until reboot Datasheets are available from: http://www.nbritton.org/uploads/envy24/ http://www.asahi-kasei.co.jp/akm/en/product/ak4528/ak4528_f01e.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4528/ekd4528-01.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4524/ak4524_f03e.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4524/ekd4524.pdf http://www.wolfson.co.uk/uploads/documents/en/WM8728.pdf http://www.richtech.co.kr/down/richtek/RT9131.pdf http://xkodi.svobodno.com/xkodi/space71.html http://people.freebsd.org/~lofi/envy24.pdf http://people.freebsd.org/~lofi/4524.pdf Submitted by: Konstantin Dimitrov <kosio.dimitrov@gmail.com> Tested by: Nikolas Britton <nikolas.britton@gmail.com> Stefan Ehmann <shoesoft@gmx.net>
2006-06-17 15:11:36 +00:00
codec->lock = snd_mtxcreate(codec->name, codec->name);
codec->dev = dev;
codec->ctrl = ctrl;
codec->devinfo = devinfo;
codec->num = num;
codec->type = SPICDS_TYPE_AK4524;
codec->cif = 0;
codec->format = AK452X_FORMAT_I2S | AK452X_FORMAT_256FSN | AK452X_FORMAT_1X;
codec->dvc = AK452X_DVC_DEMOFF | AK452X_DVC_ZTM1024 | AK452X_DVC_ZCE;
return codec;
}
void
spicds_destroy(struct spicds_info *codec)
{
snd_mtxfree(codec->lock);
free(codec, M_SPICDS);
}
void
spicds_settype(struct spicds_info *codec, unsigned int type)
{
snd_mtxlock(codec->lock);
codec->type = type;
snd_mtxunlock(codec->lock);
}
void
spicds_setcif(struct spicds_info *codec, unsigned int cif)
{
snd_mtxlock(codec->lock);
codec->cif = cif;
snd_mtxunlock(codec->lock);
}
void
spicds_setformat(struct spicds_info *codec, unsigned int format)
{
snd_mtxlock(codec->lock);
codec->format = format;
snd_mtxunlock(codec->lock);
}
void
spicds_setdvc(struct spicds_info *codec, unsigned int dvc)
{
snd_mtxlock(codec->lock);
- fix compatibility with newer versions of FreeBSD - fix all warnings during compilation - fix obvious bugs - add support for more cards Now supported: - M-Audio Delta Dio 2496 - M-Audio Audiophile 2496 - Terratec DMX 6fire Known bugs (detected by Nokolas and Stefan): - $ kldunload snd_ak452x.ko Warning: memory type ak452x leaked memory on destroy (1 allocations, 64 bytes leaked). - No sound in KDE: Everything works fine at the console but when I load KDE (3.5.3) the sound stutters and plays at less then 1/2 speed. - 'mixer: WRITE_MIXER: Device not configured' The message repeats x times at system startup, x = whatever hw.snd.maxautovchans is set to. (this is because only vol, pcm and line are supported, but the driver shows more than those mixer devices and setting those additional mixers results in this error message) - vchans don't work - 24 bit playback not supported (only 16/32 bit) - after kld(un)loading some times, the card fails to be probed until reboot Datasheets are available from: http://www.nbritton.org/uploads/envy24/ http://www.asahi-kasei.co.jp/akm/en/product/ak4528/ak4528_f01e.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4528/ekd4528-01.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4524/ak4524_f03e.pdf http://www.asahi-kasei.co.jp/akm/en/product/ak4524/ekd4524.pdf http://www.wolfson.co.uk/uploads/documents/en/WM8728.pdf http://www.richtech.co.kr/down/richtek/RT9131.pdf http://xkodi.svobodno.com/xkodi/space71.html http://people.freebsd.org/~lofi/envy24.pdf http://people.freebsd.org/~lofi/4524.pdf Submitted by: Konstantin Dimitrov <kosio.dimitrov@gmail.com> Tested by: Nikolas Britton <nikolas.britton@gmail.com> Stefan Ehmann <shoesoft@gmx.net>
2006-06-17 15:11:36 +00:00
codec->dvc = dvc;
snd_mtxunlock(codec->lock);
}
void
spicds_init(struct spicds_info *codec)
{
#if(0)
device_printf(codec->dev, "spicds_init(codec)\n");
#endif
snd_mtxlock(codec->lock);
if (codec->type == SPICDS_TYPE_AK4524 ||\
codec->type == SPICDS_TYPE_AK4528) {
/* power off */
spicds_wrcd(codec, AK4524_POWER, 0);
/* set parameter */
spicds_wrcd(codec, AK4524_FORMAT, codec->format);
spicds_wrcd(codec, AK4524_DVC, codec->dvc);
/* power on */
spicds_wrcd(codec, AK4524_POWER, AK452X_POWER_PWDA | AK452X_POWER_PWAD | AK452X_POWER_PWVR);
/* free reset register */
spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
}
if (codec->type == SPICDS_TYPE_WM8770) {
/* WM8770 init values are taken from ALSA */
/* These come first to reduce init pop noise */
spicds_wrcd(codec, 0x1b, 0x044); /* ADC Mux (AC'97 source) */
spicds_wrcd(codec, 0x1c, 0x00B); /* Out Mux1 (VOUT1 = DAC+AUX, VOUT2 = DAC) */
spicds_wrcd(codec, 0x1d, 0x009); /* Out Mux2 (VOUT2 = DAC, VOUT3 = DAC) */
spicds_wrcd(codec, 0x18, 0x000); /* All power-up */
spicds_wrcd(codec, 0x16, 0x122); /* I2S, normal polarity, 24bit */
spicds_wrcd(codec, 0x17, 0x022); /* 256fs, slave mode */
spicds_wrcd(codec, 0x19, 0x000); /* -12dB ADC/L */
spicds_wrcd(codec, 0x1a, 0x000); /* -12dB ADC/R */
}
if (codec->type == SPICDS_TYPE_AK4358)
spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
if (codec->type == SPICDS_TYPE_AK4381)
spicds_wrcd(codec, 0x00, 0x8f); /* I2S, 24bit, power-up */
if (codec->type == SPICDS_TYPE_AK4396)
spicds_wrcd(codec, 0x00, 0x07); /* I2S, 24bit, power-up */
snd_mtxunlock(codec->lock);
}
void
spicds_reinit(struct spicds_info *codec)
{
snd_mtxlock(codec->lock);
if (codec->type != SPICDS_TYPE_WM8770) {
/* reset */
spicds_wrcd(codec, AK4524_RESET, 0);
/* set parameter */
spicds_wrcd(codec, AK4524_FORMAT, codec->format);
spicds_wrcd(codec, AK4524_DVC, codec->dvc);
/* free reset register */
spicds_wrcd(codec, AK4524_RESET, AK452X_RESET_RSDA | AK452X_RESET_RSAD);
}
else {
/* WM8770 reinit */
/* AK4358 reinit */
/* AK4381 reinit */
}
snd_mtxunlock(codec->lock);
}
void
spicds_set(struct spicds_info *codec, int dir, unsigned int left, unsigned int right)
{
#if(0)
device_printf(codec->dev, "spicds_set(codec, %d, %d, %d)\n", dir, left, right);
#endif
snd_mtxlock(codec->lock);
if (left >= 100)
if ((codec->type == SPICDS_TYPE_AK4381) || \
(codec->type == SPICDS_TYPE_AK4396))
left = 255;
else
left = 127;
else
switch (codec->type) {
case SPICDS_TYPE_WM8770:
left = left + 27;
break;
case SPICDS_TYPE_AK4381 || SPICDS_TYPE_AK4396:
left = left * 255 / 100;
break;
default:
left = left * 127 / 100;
}
if (right >= 100)
if ((codec->type == SPICDS_TYPE_AK4381) || \
(codec->type == SPICDS_TYPE_AK4396))
right = 255;
else
right = 127;
else
switch (codec->type) {
case SPICDS_TYPE_WM8770:
right = right + 27;
break;
case SPICDS_TYPE_AK4381:
case SPICDS_TYPE_AK4396:
right = right * 255 / 100;
break;
default:
right = right * 127 / 100;
}
if (dir == PCMDIR_REC && codec->type == SPICDS_TYPE_AK4524) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4524(REC) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4524_LIPGA, left);
spicds_wrcd(codec, AK4524_RIPGA, right);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4524) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4524(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4524_LOATT, left);
spicds_wrcd(codec, AK4524_ROATT, right);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4528) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4528(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4528_LOATT, left);
spicds_wrcd(codec, AK4528_ROATT, right);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_WM8770) {
#if(0)
device_printf(codec->dev, "spicds_set(): WM8770(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, WM8770_AOATT_L1, left | WM8770_AOATT_UPDATE);
spicds_wrcd(codec, WM8770_AOATT_R1, right | WM8770_AOATT_UPDATE);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4358) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4358(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4358_LO1ATT, left | AK4358_OATT_ENABLE);
spicds_wrcd(codec, AK4358_RO1ATT, right | AK4358_OATT_ENABLE);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4381) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4381(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4381_LOATT, left);
spicds_wrcd(codec, AK4381_ROATT, right);
}
if (dir == PCMDIR_PLAY && codec->type == SPICDS_TYPE_AK4396) {
#if(0)
device_printf(codec->dev, "spicds_set(): AK4396(PLAY) %d/%d\n", left, right);
#endif
spicds_wrcd(codec, AK4396_LOATT, left);
spicds_wrcd(codec, AK4396_ROATT, right);
}
snd_mtxunlock(codec->lock);
}
MODULE_DEPEND(snd_spicds, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
MODULE_VERSION(snd_spicds, 1);