#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 snapper_softc
{
device_t sc_dev;
uint32_t sc_addr;
};
static int snapper_probe(device_t);
static int snapper_attach(device_t);
static int snapper_init(struct snd_mixer *m);
static int snapper_uninit(struct snd_mixer *m);
static int snapper_reinit(struct snd_mixer *m);
static int snapper_set(struct snd_mixer *m, unsigned dev, unsigned left,
unsigned right);
static uint32_t snapper_setrecsrc(struct snd_mixer *m, uint32_t src);
static device_method_t snapper_methods[] = {
DEVMETHOD(device_probe, snapper_probe),
DEVMETHOD(device_attach, snapper_attach),
DEVMETHOD_END
};
static driver_t snapper_driver = {
"snapper",
snapper_methods,
sizeof(struct snapper_softc)
};
DRIVER_MODULE(snapper, iicbus, snapper_driver, 0, 0);
MODULE_VERSION(snapper, 1);
MODULE_DEPEND(snapper, iicbus, 1, 1, 1);
static kobj_method_t snapper_mixer_methods[] = {
KOBJMETHOD(mixer_init, snapper_init),
KOBJMETHOD(mixer_uninit, snapper_uninit),
KOBJMETHOD(mixer_reinit, snapper_reinit),
KOBJMETHOD(mixer_set, snapper_set),
KOBJMETHOD(mixer_setrecsrc, snapper_setrecsrc),
KOBJMETHOD_END
};
MIXER_DECLARE(snapper_mixer);
#define SNAPPER_IICADDR 0x6a
#define SNAPPER_MCR1 0x01
#define SNAPPER_DRC 0x02
#define SNAPPER_VOLUME 0x04
#define SNAPPER_TREBLE 0x05
#define SNAPPER_BASS 0x06
#define SNAPPER_MIXER_L 0x07
#define SNAPPER_MIXER_R 0x08
#define SNAPPER_LB0 0x0a
#define SNAPPER_LB1 0x0b
#define SNAPPER_LB2 0x0c
#define SNAPPER_LB3 0x0d
#define SNAPPER_LB4 0x0e
#define SNAPPER_LB5 0x0f
#define SNAPPER_LB6 0x10
#define SNAPPER_RB0 0x13
#define SNAPPER_RB1 0x14
#define SNAPPER_RB2 0x15
#define SNAPPER_RB3 0x16
#define SNAPPER_RB4 0x17
#define SNAPPER_RB5 0x18
#define SNAPPER_RB6 0x19
#define SNAPPER_LLB 0x21
#define SNAPPER_RLB 0x22
#define SNAPPER_LLB_GAIN 0x23
#define SNAPPER_RLB_GAIN 0x24
#define SNAPPER_ACR 0x40
#define SNAPPER_MCR2 0x43
#define SNAPPER_MCR1_FL 0x80
#define SNAPPER_MCR1_SC 0x40
#define SNAPPER_MCR1_SC_32 0x00
#define SNAPPER_MCR1_SC_64 0x40
#define SNAPPER_MCR1_SM 0x30
#define SNAPPER_MCR1_SM_L 0x00
#define SNAPPER_MCR1_SM_R 0x10
#define SNAPPER_MCR1_SM_I2S 0x20
#define SNAPPER_MCR1_W 0x03
#define SNAPPER_MCR1_W_16 0x00
#define SNAPPER_MCR1_W_18 0x01
#define SNAPPER_MCR1_W_20 0x02
#define SNAPPER_MCR1_W_24 0x03
#define SNAPPER_MCR2_DL 0x80
#define SNAPPER_MCR2_AP 0x02
#define SNAPPER_ACR_ADM 0x80
#define SNAPPER_ACR_LRB 0x40
#define SNAPPER_ACR_DM 0x0c
#define SNAPPER_ACR_DM_OFF 0x00
#define SNAPPER_ACR_DM_48 0x04
#define SNAPPER_ACR_DM_44 0x08
#define SNAPPER_ACR_INP 0x02
#define SNAPPER_ACR_INP_A 0x00
#define SNAPPER_ACR_INP_B 0x02
#define SNAPPER_ACR_APD 0x01
struct snapper_reg {
u_char MCR1[1];
u_char DRC[6];
u_char VOLUME[6];
u_char TREBLE[1];
u_char BASS[1];
u_char MIXER_L[9];
u_char MIXER_R[9];
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 LB6[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];
u_char RB6[15];
u_char LLB[15];
u_char RLB[15];
u_char LLB_GAIN[3];
u_char RLB_GAIN[3];
u_char ACR[1];
u_char MCR2[1];
};
static const struct snapper_reg snapper_initdata = {
{ SNAPPER_MCR1_SC_64 | SNAPPER_MCR1_SM_I2S |
SNAPPER_MCR1_W_16 },
{ 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 },
{ 0x72 },
{ 0x72 },
{ 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 },
{ 0x10, 0x00, 0x00, 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 },
{ 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 },
{ 0, 0, 0 },
{ 0, 0, 0 },
{ SNAPPER_ACR_ADM | SNAPPER_ACR_LRB | SNAPPER_ACR_INP_B },
{ SNAPPER_MCR2_AP }
};
static const char snapper_regsize[] = {
0,
sizeof snapper_initdata.MCR1,
sizeof snapper_initdata.DRC,
0,
sizeof snapper_initdata.VOLUME,
sizeof snapper_initdata.TREBLE,
sizeof snapper_initdata.BASS,
sizeof snapper_initdata.MIXER_L,
sizeof snapper_initdata.MIXER_R,
0,
sizeof snapper_initdata.LB0,
sizeof snapper_initdata.LB1,
sizeof snapper_initdata.LB2,
sizeof snapper_initdata.LB3,
sizeof snapper_initdata.LB4,
sizeof snapper_initdata.LB5,
sizeof snapper_initdata.LB6,
0,
0,
sizeof snapper_initdata.RB0,
sizeof snapper_initdata.RB1,
sizeof snapper_initdata.RB2,
sizeof snapper_initdata.RB3,
sizeof snapper_initdata.RB4,
sizeof snapper_initdata.RB5,
sizeof snapper_initdata.RB6,
0,0,0,0, 0,0,
0,
sizeof snapper_initdata.LLB,
sizeof snapper_initdata.RLB,
sizeof snapper_initdata.LLB_GAIN,
sizeof snapper_initdata.RLB_GAIN,
0,0,0,0, 0,0,0,0, 0,0,0,
0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0,
sizeof snapper_initdata.ACR,
0,
0,
sizeof snapper_initdata.MCR2
};
static unsigned int snapper_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
snapper_write(struct snapper_softc *sc, uint8_t reg, const void *data)
{
unsigned int size;
uint8_t buf[16];
struct iic_msg msg[] = {
{ sc->sc_addr, IIC_M_WR, 0, buf }
};
KASSERT(reg < sizeof(snapper_regsize), ("bad reg"));
size = snapper_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
snapper_probe(device_t dev)
{
const char *name, *compat;
name = ofw_bus_get_name(dev);
if (name == NULL)
return (ENXIO);
if (strcmp(name, "deq") == 0) {
if (iicbus_get_addr(dev) != SNAPPER_IICADDR)
return (ENXIO);
} else if (strcmp(name, "codec") == 0) {
compat = ofw_bus_get_compat(dev);
if (compat == NULL || strcmp(compat, "tas3004") != 0)
return (ENXIO);
} else {
return (ENXIO);
}
device_set_desc(dev, "Texas Instruments TAS3004 Audio Codec");
return (0);
}
static int
snapper_attach(device_t dev)
{
struct snapper_softc *sc;
sc = device_get_softc(dev);
sc->sc_dev = dev;
sc->sc_addr = iicbus_get_addr(dev);
i2s_mixer_class = &snapper_mixer_class;
i2s_mixer = dev;
return (0);
}
static int
snapper_init(struct snd_mixer *m)
{
struct snapper_softc *sc;
unsigned int x = 0;
sc = device_get_softc(mix_getdevinfo(m));
snapper_write(sc, SNAPPER_LB0, snapper_initdata.LB0);
snapper_write(sc, SNAPPER_LB1, snapper_initdata.LB1);
snapper_write(sc, SNAPPER_LB2, snapper_initdata.LB2);
snapper_write(sc, SNAPPER_LB3, snapper_initdata.LB3);
snapper_write(sc, SNAPPER_LB4, snapper_initdata.LB4);
snapper_write(sc, SNAPPER_LB5, snapper_initdata.LB5);
snapper_write(sc, SNAPPER_LB6, snapper_initdata.LB6);
snapper_write(sc, SNAPPER_RB0, snapper_initdata.RB0);
snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
snapper_write(sc, SNAPPER_RB1, snapper_initdata.RB1);
snapper_write(sc, SNAPPER_RB2, snapper_initdata.RB2);
snapper_write(sc, SNAPPER_RB3, snapper_initdata.RB3);
snapper_write(sc, SNAPPER_RB4, snapper_initdata.RB4);
snapper_write(sc, SNAPPER_RB5, snapper_initdata.RB5);
snapper_write(sc, SNAPPER_RB6, snapper_initdata.RB6);
snapper_write(sc, SNAPPER_MCR1, snapper_initdata.MCR1);
snapper_write(sc, SNAPPER_MCR2, snapper_initdata.MCR2);
snapper_write(sc, SNAPPER_DRC, snapper_initdata.DRC);
snapper_write(sc, SNAPPER_VOLUME, snapper_initdata.VOLUME);
snapper_write(sc, SNAPPER_TREBLE, snapper_initdata.TREBLE);
snapper_write(sc, SNAPPER_BASS, snapper_initdata.BASS);
snapper_write(sc, SNAPPER_MIXER_L, snapper_initdata.MIXER_L);
snapper_write(sc, SNAPPER_MIXER_R, snapper_initdata.MIXER_R);
snapper_write(sc, SNAPPER_LLB, snapper_initdata.LLB);
snapper_write(sc, SNAPPER_RLB, snapper_initdata.RLB);
snapper_write(sc, SNAPPER_LLB_GAIN, snapper_initdata.LLB_GAIN);
snapper_write(sc, SNAPPER_RLB_GAIN, snapper_initdata.RLB_GAIN);
snapper_write(sc, SNAPPER_ACR, snapper_initdata.ACR);
x |= SOUND_MASK_VOLUME;
mix_setdevs(m, x);
return (0);
}
static int
snapper_uninit(struct snd_mixer *m)
{
return (0);
}
static int
snapper_reinit(struct snd_mixer *m)
{
return (0);
}
static int
snapper_set(struct snd_mixer *m, unsigned dev, unsigned left, unsigned right)
{
struct snapper_softc *sc;
unsigned int l, r;
u_char reg[6];
sc = device_get_softc(mix_getdevinfo(m));
if (left > 100 || right > 100)
return (0);
l = (left == 0) ? 0 : snapper_volume_table[left - 1];
r = (right == 0) ? 0 : snapper_volume_table[right - 1];
switch (dev) {
case SOUND_MIXER_VOLUME:
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;
snapper_write(sc, SNAPPER_VOLUME, reg);
return (left | (right << 8));
}
return (0);
}
static uint32_t
snapper_setrecsrc(struct snd_mixer *m, uint32_t src)
{
return (0);
}