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