1 /* $OpenBSD: onyx.c,v 1.18 2023/07/31 12:00:07 tobhe Exp $ */ 2 3 /*- 4 * Copyright (c) 2005 Tsubai Masanari. All rights reserved. 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * http://focus.ti.com/docs/prod/folders/print/pcm3052.html 31 * 32 * Datasheet is available from 33 * http://focus.ti.com/docs/prod/folders/print/pcm3052a.html 34 */ 35 36 #include <sys/param.h> 37 #include <sys/audioio.h> 38 #include <sys/device.h> 39 #include <sys/systm.h> 40 41 #include <dev/audio_if.h> 42 #include <dev/ofw/openfirm.h> 43 #include <macppc/dev/dbdma.h> 44 45 #include <machine/autoconf.h> 46 47 #include <macppc/dev/i2svar.h> 48 #include <macppc/dev/kiicvar.h> 49 50 #ifdef ONYX_DEBUG 51 # define DPRINTF printf 52 #else 53 # define DPRINTF while (0) printf 54 #endif 55 56 /* XXX */ 57 #define PCM3052_I2C_ADDR 0x8c 58 59 /* PCM3052 registers */ 60 #define PCM3052_REG_LEFT_VOLUME 0x41 61 #define PCM3052_REG_RIGHT_VOLUME 0x42 62 #define PCM3052_REG_ADC_CONTROL 0x48 63 64 #define PCM3052_ADC_INPUT_MIC 0x20 65 66 /* XXX */ 67 #define onyx_softc i2s_softc 68 69 /* XXX */ 70 void kiic_setmode(struct kiic_softc *, u_int, u_int); 71 int kiic_write(struct device *, int, int, const void *, int); 72 73 int onyx_match(struct device *, void *, void *); 74 void onyx_attach(struct device *, struct device *, void *); 75 void onyx_defer(struct device *); 76 void onyx_set_volume(struct onyx_softc *, int, int); 77 void onyx_set_input(struct onyx_softc *, int); 78 79 const struct cfattach onyx_ca = { 80 sizeof(struct onyx_softc), onyx_match, onyx_attach 81 }; 82 83 struct cfdriver onyx_cd = { 84 NULL, "onyx", DV_DULL 85 }; 86 87 const struct audio_hw_if onyx_hw_if = { 88 .open = i2s_open, 89 .close = i2s_close, 90 .set_params = i2s_set_params, 91 .round_blocksize = i2s_round_blocksize, 92 .halt_output =i2s_halt_output, 93 .halt_input = i2s_halt_input, 94 .set_port = i2s_set_port, 95 .get_port = i2s_get_port, 96 .query_devinfo = i2s_query_devinfo, 97 .allocm = i2s_allocm, 98 .round_buffersize = i2s_round_buffersize, 99 .trigger_output = i2s_trigger_output, 100 .trigger_input = i2s_trigger_input, 101 }; 102 103 int 104 onyx_match(struct device *parent, void *match, void *aux) 105 { 106 struct confargs *ca = aux; 107 int soundbus, soundchip, soundcodec; 108 int32_t layout = 0; 109 110 if (strcmp(ca->ca_name, "i2s") != 0) 111 return (0); 112 113 if ((soundbus = OF_child(ca->ca_node)) == 0 || 114 (soundchip = OF_child(soundbus)) == 0) 115 return (0); 116 117 if (OF_getprop(soundchip, "platform-onyx-codec-ref", 118 &soundcodec, sizeof soundcodec) == sizeof soundcodec) 119 return (1); 120 121 /* 122 * Apple really messed up. First and second generation iMac 123 * G5 (PowerMac8,1 and PowerMac8,2) have a "deq" i2c device 124 * listed in the OF device tree, which is a telltale sign of 125 * snapper(4). But in reality that chip isn't there. So we 126 * match on "layout-id" instead. 127 */ 128 if (OF_getprop(soundchip, "layout-id", &layout, sizeof layout) && 129 (layout == 0x2d || layout == 0x56)) 130 return (1); 131 132 return (0); 133 } 134 135 void 136 onyx_attach(struct device *parent, struct device *self, void *aux) 137 { 138 struct onyx_softc *sc = (struct onyx_softc *)self; 139 140 sc->sc_setvolume = onyx_set_volume; 141 sc->sc_setinput = onyx_set_input; 142 143 i2s_attach(parent, sc, aux); 144 config_defer(self, onyx_defer); 145 } 146 147 void 148 onyx_defer(struct device *dev) 149 { 150 struct onyx_softc *sc = (struct onyx_softc *)dev; 151 struct device *dv; 152 153 TAILQ_FOREACH(dv, &alldevs, dv_list) 154 if (strcmp(dv->dv_cfdata->cf_driver->cd_name, "kiic") == 0 && 155 strcmp(dv->dv_parent->dv_cfdata->cf_driver->cd_name, "macobio") == 0) 156 sc->sc_i2c = dv; 157 if (sc->sc_i2c == NULL) { 158 printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname); 159 return; 160 } 161 162 /* XXX If i2c has failed to attach, what should we do? */ 163 164 audio_attach_mi(&onyx_hw_if, sc, NULL, &sc->sc_dev); 165 166 deq_reset(sc); 167 onyx_set_volume(sc, 192, 192); 168 onyx_set_input(sc, 1); 169 } 170 171 void 172 onyx_set_volume(struct onyx_softc *sc, int left, int right) 173 { 174 u_int8_t data; 175 176 sc->sc_vol_l = left; 177 sc->sc_vol_r = right; 178 179 kiic_setmode(sc->sc_i2c, I2C_STDSUBMODE, 0); 180 data = 128 + (left >> 1); 181 kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR, 182 PCM3052_REG_LEFT_VOLUME, &data, 1); 183 data = 128 + (right >> 1); 184 kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR, 185 PCM3052_REG_RIGHT_VOLUME, &data, 1); 186 } 187 188 void 189 onyx_set_input(struct onyx_softc *sc, int mask) 190 { 191 uint8_t data = 0; 192 193 sc->sc_record_source = mask; 194 195 switch (mask) { 196 case 1 << 0: /* microphone */ 197 data = PCM3052_ADC_INPUT_MIC; 198 break; 199 case 1 << 1: /* line in */ 200 data = 0; 201 break; 202 } 203 data |= 12; /* +4dB */ 204 205 kiic_write(sc->sc_i2c, PCM3052_I2C_ADDR, 206 PCM3052_REG_ADC_CONTROL, &data, 1); 207 } 208