#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <machine/dbdma.h>
#include <machine/intr_machdep.h>
#include <machine/resource.h>
#include <machine/bus.h>
#include <machine/pio.h>
#include <sys/rman.h>
#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>
#include <dev/ofw/ofw_bus.h>
#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif
#include <dev/sound/pcm/sound.h>
#include "mixer_if.h"
extern kobj_class_t i2s_mixer_class;
extern device_t i2s_mixer;
struct tumbler_softc
{
device_t sc_dev;
uint32_t sc_addr;
};
static int tumbler_probe(device_t);
static int tumbler_attach(device_t);
static int tumbler_init(struct snd_mixer *m);
static int tumbler_uninit(struct snd_mixer *m);
static int tumbler_reinit(struct snd_mixer *m);
static int tumbler_set(struct snd_mixer *m, unsigned dev, unsigned left,
unsigned right);
static u_int32_t tumbler_setrecsrc(struct snd_mixer *m, u_int32_t src);
static device_method_t tumbler_methods[] = {
DEVMETHOD(device_probe, tumbler_probe),
DEVMETHOD(device_attach, tumbler_attach),
DEVMETHOD_END
};
static driver_t tumbler_driver = {
"tumbler",
tumbler_methods,
sizeof(struct tumbler_softc)
};
DRIVER_MODULE(tumbler, iicbus, tumbler_driver, 0, 0);
MODULE_VERSION(tumbler, 1);
MODULE_DEPEND(tumbler, iicbus, 1, 1, 1);
static kobj_method_t tumbler_mixer_methods[] = {
KOBJMETHOD(mixer_init, tumbler_init),
KOBJMETHOD(mixer_uninit, tumbler_uninit),
KOBJMETHOD(mixer_reinit, tumbler_reinit),
KOBJMETHOD(mixer_set, tumbler_set),
KOBJMETHOD(mixer_setrecsrc, tumbler_setrecsrc),
KOBJMETHOD_END
};
MIXER_DECLARE(tumbler_mixer);
#define TUMBLER_IICADDR 0x68
#define TUMBLER_MCR 0x01
#define TUMBLER_DRC 0x02
#define TUMBLER_VOLUME 0x04
#define TUMBLER_TREBLE 0x05
#define TUMBLER_BASS 0x06
#define TUMBLER_MIXER1 0x07
#define TUMBLER_MIXER2 0x08
#define TUMBLER_LB0 0x0a
#define TUMBLER_LB1 0x0b
#define TUMBLER_LB2 0x0c
#define TUMBLER_LB3 0x0d
#define TUMBLER_LB4 0x0e
#define TUMBLER_LB5 0x0f
#define TUMBLER_RB0 0x13
#define TUMBLER_RB1 0x14
#define TUMBLER_RB2 0x15
#define TUMBLER_RB3 0x16
#define TUMBLER_RB4 0x17
#define TUMBLER_RB5 0x18
#define TUMBLER_MCR_FL 0x80
#define TUMBLER_MCR_SC 0x40
#define TUMBLER_MCR_SC_32 0x00
#define TUMBLER_MCR_SC_64 0x40
#define TUMBLER_MCR_SM 0x30
#define TUMBLER_MCR_SM_L 0x00
#define TUMBLER_MCR_SM_R 0x10
#define TUMBLER_MCR_SM_I2S 0x20
#define TUMBLER_MCR_ISM 0x0C
#define TUMBLER_MCR_ISM_L 0x00
#define TUMBLER_MCR_ISM_R 0x04
#define TUMBLER_MCR_ISM_I2S 0x08
#define TUMBLER_MCR_W 0x03
#define TUMBLER_MCR_W_16 0x00
#define TUMBLER_MCR_W_18 0x01
#define TUMBLER_MCR_W_20 0x02
#define TUMBLER_DRC_COMP_31 0xc0
#define TUMBLER_DRC_ENABLE 0x01
#define TUMBLER_DRC_DEFL_TH 0xa0
struct tumbler_reg {
u_char MCR[1];
u_char DRC[2];
u_char VOLUME[6];
u_char TREBLE[1];
u_char BASS[1];
u_char MIXER1[3];
u_char MIXER2[3];
u_char LB0[15];
u_char LB1[15];
u_char LB2[15];
u_char LB3[15];
u_char LB4[15];
u_char LB5[15];
u_char RB0[15];
u_char RB1[15];
u_char RB2[15];
u_char RB3[15];
u_char RB4[15];
u_char RB5[15];
};
const struct tumbler_reg tumbler_initdata = {
{ TUMBLER_MCR_SC_64 | TUMBLER_MCR_SM_I2S |
TUMBLER_MCR_ISM_I2S | TUMBLER_MCR_W_16 },
{ TUMBLER_DRC_COMP_31, TUMBLER_DRC_DEFL_TH },
{ 0, 0, 0, 0, 0, 0 },
{ 0x72 },
{ 0x3e },
{ 0x10, 0x00, 0x00 },
{ 0x00, 0x00, 0x00 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
const char tumbler_regsize[] = {
0,
sizeof tumbler_initdata.MCR,
sizeof tumbler_initdata.DRC,
0,
sizeof tumbler_initdata.VOLUME,
sizeof tumbler_initdata.TREBLE,
sizeof tumbler_initdata.BASS,
sizeof tumbler_initdata.MIXER1,
sizeof tumbler_initdata.MIXER2,
0,
sizeof tumbler_initdata.LB0,
sizeof tumbler_initdata.LB1,
sizeof tumbler_initdata.LB2,
sizeof tumbler_initdata.LB3,
sizeof tumbler_initdata.LB4,
sizeof tumbler_initdata.LB5,
0,
0,
0,
sizeof tumbler_initdata.RB0,
sizeof tumbler_initdata.RB1,
sizeof tumbler_initdata.RB2,
sizeof tumbler_initdata.RB3,
sizeof tumbler_initdata.RB4,
sizeof tumbler_initdata.RB5
};
static u_int tumbler_volume_table[100] = {
0x00000148, 0x0000015C, 0x00000171, 0x00000186,
0x0000019E, 0x000001B6, 0x000001D0, 0x000001EB,
0x00000209, 0x00000227, 0x00000248, 0x0000026B,
0x0000028F, 0x000002B6, 0x000002DF, 0x0000030B,
0x00000339, 0x0000036A, 0x0000039E, 0x000003D5,
0x0000040F, 0x0000044C, 0x0000048D, 0x000004D2,
0x0000051C, 0x00000569, 0x000005BB, 0x00000612,
0x0000066E, 0x000006D0, 0x00000737, 0x000007A5,
0x00000818, 0x00000893, 0x00000915, 0x0000099F,
0x00000A31, 0x00000ACC, 0x00000B6F, 0x00000C1D,
0x00000CD5, 0x00000D97, 0x00000E65, 0x00000F40,
0x00001027, 0x0000111C, 0x00001220, 0x00001333,
0x00001456, 0x0000158A, 0x000016D1, 0x0000182B,
0x0000199A, 0x00001B1E, 0x00001CB9, 0x00001E6D,
0x0000203A, 0x00002223, 0x00002429, 0x0000264E,
0x00002893, 0x00002AFA, 0x00002D86, 0x00003039,
0x00003314, 0x0000361B, 0x00003950, 0x00003CB5,
0x0000404E, 0x0000441D, 0x00004827, 0x00004C6D,
0x000050F4, 0x000055C0, 0x00005AD5, 0x00006037,
0x000065EA, 0x00006BF4, 0x0000725A, 0x00007920,
0x0000804E, 0x000087EF, 0x00008FF6, 0x0000987D,
0x0000A186, 0x0000AB19, 0x0000B53C, 0x0000BFF9,
0x0000CB59, 0x0000D766, 0x0000E429, 0x0000F1AE,
0x00010000, 0x00010F2B, 0x00011F3D, 0x00013042,
0x00014249, 0x00015562, 0x0001699C, 0x00017F09
};
static int
tumbler_write(struct tumbler_softc *sc, uint8_t reg, const void *data)
{
u_int size;
uint8_t buf[16];
struct iic_msg msg[] = {
{ sc->sc_addr, IIC_M_WR, 0, buf }
};
KASSERT(reg < sizeof(tumbler_regsize), ("bad reg"));
size = tumbler_regsize[reg];
msg[0].len = size + 1;
buf[0] = reg;
memcpy(&buf[1], data, size);
iicbus_transfer(sc->sc_dev, msg, 1);
return (0);
}
static int
tumbler_probe(device_t dev)
{
const char *name;
name = ofw_bus_get_name(dev);
if (name == NULL)
return (ENXIO);
if (strcmp(name, "deq") == 0 && iicbus_get_addr(dev) ==
TUMBLER_IICADDR) {
device_set_desc(dev, "Texas Instruments TAS3001 Audio Codec");
return (0);
}
return (ENXIO);
}
static int
tumbler_attach(device_t dev)
{
struct tumbler_softc *sc;
sc = device_get_softc(dev);
sc->sc_dev = dev;
sc->sc_addr = iicbus_get_addr(dev);
i2s_mixer_class = &tumbler_mixer_class;
i2s_mixer = dev;
return (0);
}
static int
tumbler_init(struct snd_mixer *m)
{
struct tumbler_softc *sc;
u_int x = 0;
sc = device_get_softc(mix_getdevinfo(m));
tumbler_write(sc, TUMBLER_LB0, tumbler_initdata.LB0);
tumbler_write(sc, TUMBLER_LB1, tumbler_initdata.LB1);
tumbler_write(sc, TUMBLER_LB2, tumbler_initdata.LB2);
tumbler_write(sc, TUMBLER_LB3, tumbler_initdata.LB3);
tumbler_write(sc, TUMBLER_LB4, tumbler_initdata.LB4);
tumbler_write(sc, TUMBLER_LB5, tumbler_initdata.LB5);
tumbler_write(sc, TUMBLER_RB0, tumbler_initdata.RB0);
tumbler_write(sc, TUMBLER_RB1, tumbler_initdata.RB1);
tumbler_write(sc, TUMBLER_RB1, tumbler_initdata.RB1);
tumbler_write(sc, TUMBLER_RB2, tumbler_initdata.RB2);
tumbler_write(sc, TUMBLER_RB3, tumbler_initdata.RB3);
tumbler_write(sc, TUMBLER_RB4, tumbler_initdata.RB4);
tumbler_write(sc, TUMBLER_RB5, tumbler_initdata.RB5);
tumbler_write(sc, TUMBLER_MCR, tumbler_initdata.MCR);
tumbler_write(sc, TUMBLER_DRC, tumbler_initdata.DRC);
tumbler_write(sc, TUMBLER_VOLUME, tumbler_initdata.VOLUME);
tumbler_write(sc, TUMBLER_TREBLE, tumbler_initdata.TREBLE);
tumbler_write(sc, TUMBLER_BASS, tumbler_initdata.BASS);
tumbler_write(sc, TUMBLER_MIXER1, tumbler_initdata.MIXER1);
tumbler_write(sc, TUMBLER_MIXER2, tumbler_initdata.MIXER2);
x |= SOUND_MASK_VOLUME;
mix_setdevs(m, x);
return (0);
}
static int
tumbler_uninit(struct snd_mixer *m)
{
return (0);
}
static int
tumbler_reinit(struct snd_mixer *m)
{
return (0);
}
static int
tumbler_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
{
struct tumbler_softc *sc;
struct mtx *mixer_lock;
int locked;
u_int l, r;
u_char reg[6];
sc = device_get_softc(mix_getdevinfo(m));
mixer_lock = mixer_get_lock(m);
locked = mtx_owned(mixer_lock);
switch (dev) {
case SOUND_MIXER_VOLUME:
if (left > 100 || right > 100)
return (0);
l = (left == 0 ? 0 : tumbler_volume_table[left - 1]);
r = (right == 0 ? 0 : tumbler_volume_table[right - 1]);
reg[0] = (l & 0xff0000) >> 16;
reg[1] = (l & 0x00ff00) >> 8;
reg[2] = l & 0x0000ff;
reg[3] = (r & 0xff0000) >> 16;
reg[4] = (r & 0x00ff00) >> 8;
reg[5] = r & 0x0000ff;
if (locked)
mtx_unlock(mixer_lock);
tumbler_write(sc, TUMBLER_VOLUME, reg);
if (locked)
mtx_lock(mixer_lock);
return (left | (right << 8));
}
return (0);
}
static u_int32_t
tumbler_setrecsrc(struct snd_mixer *m, u_int32_t src)
{
return (0);
}