Home | History | Annotate | Line # | Download | only in i2c
sgsmix.c revision 1.9.16.1
      1 /*	$NetBSD: sgsmix.c,v 1.9.16.1 2021/04/03 22:28:44 thorpej 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.9.16.1 2021/04/03 22:28:44 thorpej 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 <dev/i2c/i2cvar.h>
     44 
     45 #include <dev/i2c/sgsmixvar.h>
     46 #include "opt_sgsmix.h"
     47 
     48 #ifdef SGSMIX_DEBUG
     49 #define DPRINTF printf
     50 #else
     51 #define DPRINTF while (0) printf
     52 #endif
     53 
     54 struct sgsmix_softc {
     55 	device_t sc_dev;
     56 	device_t sc_parent;
     57 	i2c_tag_t sc_i2c;
     58 	int sc_node, sc_address;
     59 	uint8_t sc_regs[7];
     60 };
     61 
     62 #define SGSREG_INPUT_SELECT	0
     63 #define SGSREG_MASTER_GAIN	1
     64 #define SGSREG_BASS_TREBLE	2
     65 #define SGSREG_SPEAKER_L	3
     66 #define SGSREG_HEADPHONES_L	4
     67 #define SGSREG_SPEAKER_R	5
     68 #define SGSREG_HEADPHONES_R	6
     69 
     70 static void sgsmix_attach(device_t, device_t, void *);
     71 static int sgsmix_match(device_t, cfdata_t, void *);
     72 static void sgsmix_setup(struct sgsmix_softc *);
     73 static void sgsmix_writereg(struct sgsmix_softc *, int, uint8_t);
     74 
     75 CFATTACH_DECL_NEW(sgsmix, sizeof(struct sgsmix_softc),
     76     sgsmix_match, sgsmix_attach, NULL, NULL);
     77 
     78 static const struct device_compatible_entry compat_data[] = {
     79 	{ .compat = "st,tda7433" },
     80 	DEVICE_COMPAT_EOL
     81 };
     82 
     83 static int
     84 sgsmix_match(device_t parent, cfdata_t cf, void *aux)
     85 {
     86 	struct i2c_attach_args *args = aux;
     87 	int ret = -1;
     88 	uint8_t out[2] = {1, 0x20};
     89 	int match_result;
     90 
     91 	if (iic_use_direct_match(args, cf, compat_data, &match_result))
     92 		return match_result;
     93 
     94 	/* see if we can talk to something at address 0x8a */
     95 	if (args->ia_addr != 0x8a)
     96 		return 0;
     97 
     98 	iic_acquire_bus(args->ia_tag, 0);
     99 	ret = iic_exec(args->ia_tag, I2C_OP_WRITE, args->ia_addr,
    100 	    out, 2, NULL, 0, 0);
    101 	iic_release_bus(args->ia_tag, 0);
    102 
    103 	return (ret >= 0) ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
    104 }
    105 
    106 static void
    107 sgsmix_attach(device_t parent, device_t self, void *aux)
    108 {
    109 	struct sgsmix_softc *sc = device_private(self);
    110 	struct i2c_attach_args *args = aux;
    111 
    112 	sc->sc_dev = self;
    113 	sc->sc_parent = parent;
    114 	sc->sc_address = args->ia_addr;
    115 	aprint_normal(": SGS TDA7433 Basic Audio Processor\n");
    116 	sc->sc_i2c = args->ia_tag;
    117 	sgsmix_setup(sc);
    118 }
    119 
    120 static void
    121 sgsmix_setup(struct sgsmix_softc *sc)
    122 {
    123 	int i;
    124 	uint8_t out[2];
    125 
    126 	sc->sc_regs[0] = 9;	/* input 1 */
    127 	sc->sc_regs[1] = 0x20;	/* master gain 0dB */
    128 	sc->sc_regs[2] = 0x77;	/* flat bass / treble */
    129 	sc->sc_regs[3] = 0;	/* all speakers full volume */
    130 	sc->sc_regs[4] = 0;
    131 	sc->sc_regs[5] = 0;
    132 	sc->sc_regs[6] = 0;
    133 
    134 	iic_acquire_bus(sc->sc_i2c, 0);
    135 
    136 	for (i = 0; i < 7; i++) {
    137 		out[0] = i;
    138 		out[1] = sc->sc_regs[i];
    139 		iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2,
    140 		    NULL, 0, 0);
    141 	}
    142 
    143 	iic_release_bus(sc->sc_i2c, 0);
    144 }
    145 
    146 static void
    147 sgsmix_writereg(struct sgsmix_softc *sc, int reg, uint8_t val)
    148 {
    149 	uint8_t out[2] = {reg, val};
    150 
    151 	if ((reg < 0) || (reg >= 7))
    152 		return;
    153 
    154 	if (sc->sc_regs[reg] == val)
    155 		return;
    156 
    157 	if (iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_address, out, 2, NULL,
    158 	    0, 0) == 0) {
    159 	    	sc->sc_regs[reg] = val;
    160 	}
    161 }
    162 
    163 void
    164 sgsmix_set_speaker_vol(void *cookie, int left, int right)
    165 {
    166 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    167 
    168 	DPRINTF("%s: speaker %d %d\n", device_xname(sc->sc_dev), left, right);
    169 	if (left == 0) {
    170 		sgsmix_writereg(sc, SGSREG_SPEAKER_L, 0x20);
    171 	} else {
    172 		sgsmix_writereg(sc, SGSREG_SPEAKER_L,
    173 		    ((255 - left) >> 3) & 0x1f);
    174 	}
    175 
    176 	if (right == 0) {
    177 		sgsmix_writereg(sc, SGSREG_SPEAKER_R, 0x20);
    178 	} else {
    179 		sgsmix_writereg(sc, SGSREG_SPEAKER_R,
    180 		    ((255 - right) >> 3) & 0x1f);
    181 	}
    182 }
    183 
    184 void
    185 sgsmix_set_headphone_vol(void *cookie, int left, int right)
    186 {
    187 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    188 
    189 	DPRINTF("%s: headphones %d %d\n", device_xname(sc->sc_dev), left, right);
    190 	if (left == 0) {
    191 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L, 0x20);
    192 	} else {
    193 		sgsmix_writereg(sc, SGSREG_HEADPHONES_L,
    194 		    ((255 - left) >> 3) & 0x1f);
    195 	}
    196 
    197 	if (right == 0) {
    198 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R, 0x20);
    199 	} else {
    200 		sgsmix_writereg(sc, SGSREG_HEADPHONES_R,
    201 		    ((255 - right) >> 3) & 0x1f);
    202 	}
    203 }
    204 
    205 void
    206 sgsmix_set_bass_treble(void *cookie, int bass, int treble)
    207 {
    208 	struct sgsmix_softc *sc = device_private((device_t)cookie);
    209 	uint8_t b, t;
    210 
    211 	t = (treble >> 4) & 0xf;
    212 	if (t & 0x8)
    213 		t ^= 7;
    214 	b = bass & 0xf0;
    215 	if (b & 0x80)
    216 		b ^= 0x70;
    217 	DPRINTF("%s: bass/treble %02x %02x\n", device_xname(sc->sc_dev), b, t);
    218 	sgsmix_writereg(sc, SGSREG_BASS_TREBLE, b | t);
    219 }
    220