Home | History | Annotate | Line # | Download | only in i2c
sgsmix.c revision 1.2.20.2
      1 /*	$NetBSD: sgsmix.c,v 1.2.20.2 2008/09/28 10:40:21 mjf Exp $	*/
      2 
      3 /*-
      4  * Copyright (C) 2005 Michael Lorenz.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 /*
     28  * a driver for the SGS TDA7433 mixer chip found in Beige PowerMac G3 and
     29  * probably others
     30  */
     31 
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: sgsmix.c,v 1.2.20.2 2008/09/28 10:40:21 mjf Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/sysctl.h>
     42 
     43 #include <uvm/uvm_extern.h>
     44 
     45 #include <dev/i2c/i2cvar.h>
     46 
     47 #include <dev/i2c/sgsmixvar.h>
     48 #include "opt_sgsmix.h"
     49 
     50 #ifdef SGSMIX_DEBUG
     51 #define DPRINTF printf
     52 #else
     53 #define DPRINTF while (0) printf
     54 #endif
     55 
     56 struct sgsmix_softc {
     57 	device_t sc_dev;
     58 	device_t sc_parent;
     59 	i2c_tag_t sc_i2c;
     60 	int sc_node, sc_address;
     61 	uint8_t sc_regs[7];
     62 };
     63 
     64 #define SGSREG_INPUT_SELECT	0
     65 #define SGSREG_MASTER_GAIN	1
     66 #define SGSREG_BASS_TREBLE	2
     67 #define SGSREG_SPEAKER_L	3
     68 #define SGSREG_HEADPHONES_L	4
     69 #define SGSREG_SPEAKER_R	5
     70 #define SGSREG_HEADPHONES_R	6
     71 
     72 static void sgsmix_attach(device_t, device_t, void *);
     73 static int sgsmix_match(device_t, cfdata_t, void *);
     74 static void sgsmix_setup(struct sgsmix_softc *);
     75 static void sgsmix_writereg(struct sgsmix_softc *, int, uint8_t);
     76 
     77 CFATTACH_DECL_NEW(sgsmix, sizeof(struct sgsmix_softc),
     78     sgsmix_match, sgsmix_attach, NULL, NULL);
     79 
     80 static int
     81 sgsmix_match(device_t parent, cfdata_t cf, void *aux)
     82 {
     83 	struct i2c_attach_args *args = aux;
     84 	int ret = -1;
     85 	uint8_t out[2] = {1, 0x20};
     86 
     87 	/* see if we can talk to something at address 0x8a */
     88 	if (args->ia_addr == 0x8a) {
     89 		iic_acquire_bus(args->ia_tag, 0);
     90 		ret = iic_exec(args->ia_tag, I2C_OP_WRITE, args->ia_addr,
     91 		    out, 2, NULL, 0, 0);
     92 		iic_release_bus(args->ia_tag, 0);
     93 	}
     94 	return (ret >= 0);
     95 }
     96 
     97 static void
     98 sgsmix_attach(device_t parent, device_t self, void *aux)
     99 {
    100 	struct sgsmix_softc *sc = device_private(self);
    101 	struct i2c_attach_args *args = aux;
    102 
    103 	sc->sc_dev = self;
    104 	sc->sc_parent = parent;
    105 	sc->sc_address = args->ia_addr;
    106 	aprint_normal(": SGS TDA7433 Basic Audio Processor\n");
    107 	sc->sc_i2c = args->ia_tag;
    108 	sgsmix_setup(sc);
    109 }
    110 
    111 static void
    112 sgsmix_setup(struct sgsmix_softc *sc)
    113 {
    114 	int i;
    115 	uint8_t out[2];
    116 
    117 	sc->sc_regs[0] = 9;	/* input 1 */
    118 	sc->sc_regs[1] = 0x20;	/* master gain 0dB */
    119 	sc->sc_regs[2] = 0x77;	/* flat bass / treble */
    120 	sc->sc_regs[3] = 0;	/* all speakers full volume */
    121 	sc->sc_regs[4] = 0;
    122 	sc->sc_regs[5] = 0;
    123 	sc->sc_regs[6] = 0;
    124 
    125 	iic_acquire_bus(sc->sc_i2c, 0);
    126 
    127 	for (i = 0; i < 7; i++) {
    128 		out[0] = i;
    129 		out[1] = sc->sc_regs[i];
    130 		iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2,
    131 		    NULL, 0, 0);
    132 	}
    133 
    134 	iic_release_bus(sc->sc_i2c, 0);
    135 }
    136 
    137 static void
    138 sgsmix_writereg(struct sgsmix_softc *sc, int reg, uint8_t val)
    139 {
    140 	uint8_t out[2] = {reg, val};
    141 
    142 	if ((reg < 0) || (reg >= 7))
    143 		return;
    144 
    145 	if (sc->sc_regs[reg] == val)
    146 		return;
    147 
    148 	if (iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2, NULL,
    149 	    0, 0) == 0) {
    150 	    	sc->sc_regs[reg] = val;
    151 	}
    152 }
    153 
    154 void
    155 sgsmix_set_speaker_vol(void *cookie, int left, int right)
    156 {
    157 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    158 
    159 	DPRINTF("%s: speaker %d %d\n", device_xname(sc->sc_dev), left, right);
    160 	if (left == 0) {
    161 		sgsmix_writereg(sc, SGSREG_SPEAKER_L, 0x20);
    162 	} else {
    163 		sgsmix_writereg(sc, SGSREG_SPEAKER_L,
    164 		    ((255 - left) >> 3) & 0x1f);
    165 	}
    166 
    167 	if (right == 0) {
    168 		sgsmix_writereg(sc, SGSREG_SPEAKER_R, 0x20);
    169 	} else {
    170 		sgsmix_writereg(sc, SGSREG_SPEAKER_R,
    171 		    ((255 - right) >> 3) & 0x1f);
    172 	}
    173 }
    174 
    175 void
    176 sgsmix_set_headphone_vol(void *cookie, int left, int right)
    177 {
    178 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    179 
    180 	DPRINTF("%s: headphones %d %d\n", device_xname(sc->sc_dev), left, right);
    181 	if (left == 0) {
    182 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L, 0x20);
    183 	} else {
    184 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L,
    185 		    ((255 - left) >> 3) & 0x1f);
    186 	}
    187 
    188 	if (right == 0) {
    189 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R, 0x20);
    190 	} else {
    191 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R,
    192 		    ((255 - right) >> 3) & 0x1f);
    193 	}
    194 }
    195 
    196 void
    197 sgsmix_set_bass_treble(void *cookie, int bass, int treble)
    198 {
    199 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    200 	uint8_t b, t;
    201 
    202 	t = (treble >> 4) & 0xf;
    203 	if (t & 0x8)
    204 		t ^= 7;
    205 	b = bass & 0xf0;
    206 	if (b & 0x80)
    207 		b ^= 0x70;
    208 	DPRINTF("%s: bass/treble %02x %02x\n", device_xname(sc->sc_dev), b, t);
    209 	sgsmix_writereg(sc, SGSREG_BASS_TREBLE, b | t);
    210 }
    211