1 /* $OpenBSD: snapper.c,v 1.34 2008/11/06 10:01:50 todd Exp $ */ 2 /* $NetBSD: snapper.c,v 1.1 2003/12/27 02:19:34 grant Exp $ */ 3 4 /*- 5 * Copyright (c) 2002 Tsubai Masanari. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /* 31 * Datasheet is available from 32 * http://focus.ti.com/docs/prod/folders/print/tas3004.html 33 */ 34 35 #include <sys/param.h> 36 #include <sys/audioio.h> 37 #include <sys/device.h> 38 #include <sys/systm.h> 39 40 #include <dev/audio_if.h> 41 #include <dev/ofw/openfirm.h> 42 #include <macppc/dev/dbdma.h> 43 44 #include <machine/autoconf.h> 45 46 #include <macppc/dev/i2svar.h> 47 48 #ifdef SNAPPER_DEBUG 49 # define DPRINTF printf 50 #else 51 # define DPRINTF while (0) printf 52 #endif 53 54 /* XXX */ 55 #define snapper_softc i2s_softc 56 57 /* XXX */ 58 int kiic_write(struct device *, int, int, const void *, int); 59 int kiic_writereg(struct device *, int, u_int); 60 61 void snapper_init(struct snapper_softc *); 62 int snapper_getdev(void *, struct audio_device *); 63 int snapper_match(struct device *, void *, void *); 64 void snapper_attach(struct device *, struct device *, void *); 65 void snapper_defer(struct device *); 66 void snapper_set_volume(struct snapper_softc *, int, int); 67 void snapper_set_bass(struct snapper_softc *, int); 68 void snapper_set_treble(struct snapper_softc *, int); 69 void snapper_set_input(struct snapper_softc *, int); 70 void snapper_get_default_params(void *, int, struct audio_params *); 71 72 int tas3004_write(struct snapper_softc *, u_int, const void *); 73 int tas3004_init(struct snapper_softc *); 74 75 struct cfattach snapper_ca = { 76 sizeof(struct snapper_softc), snapper_match, snapper_attach 77 }; 78 struct cfdriver snapper_cd = { 79 NULL, "snapper", DV_DULL 80 }; 81 82 struct audio_hw_if snapper_hw_if = { 83 i2s_open, 84 i2s_close, 85 NULL, 86 i2s_query_encoding, 87 i2s_set_params, 88 i2s_round_blocksize, 89 NULL, 90 NULL, 91 NULL, 92 NULL, 93 NULL, 94 i2s_halt_output, 95 i2s_halt_input, 96 NULL, 97 snapper_getdev, 98 NULL, 99 i2s_set_port, 100 i2s_get_port, 101 i2s_query_devinfo, 102 i2s_allocm, /* allocm */ 103 NULL, 104 i2s_round_buffersize, 105 i2s_mappage, 106 i2s_get_props, 107 i2s_trigger_output, 108 i2s_trigger_input, 109 snapper_get_default_params 110 }; 111 112 struct audio_device snapper_device = { 113 "SNAPPER", 114 "", 115 "snapper" 116 }; 117 118 const uint8_t snapper_trebletab[] = { 119 0x96, /* -18dB */ 120 0x94, /* -17dB */ 121 0x92, /* -16dB */ 122 0x90, /* -15dB */ 123 0x8e, /* -14dB */ 124 0x8c, /* -13dB */ 125 0x8a, /* -12dB */ 126 0x88, /* -11dB */ 127 0x86, /* -10dB */ 128 0x84, /* -9dB */ 129 0x82, /* -8dB */ 130 0x80, /* -7dB */ 131 0x7e, /* -6dB */ 132 0x7c, /* -5dB */ 133 0x7a, /* -4dB */ 134 0x78, /* -3dB */ 135 0x76, /* -2dB */ 136 0x74, /* -1dB */ 137 0x72, /* 0dB */ 138 0x70, /* 1dB */ 139 0x6d, /* 2dB */ 140 0x6b, /* 3dB */ 141 0x68, /* 4dB */ 142 0x65, /* 5dB */ 143 0x62, /* 6dB */ 144 0x5d, /* 7dB */ 145 0x59, /* 8dB */ 146 0x53, /* 9dB */ 147 0x4d, /* 10dB */ 148 0x47, /* 11dB */ 149 0x3f, /* 12dB */ 150 0x36, /* 13dB */ 151 0x2c, /* 14dB */ 152 0x20, /* 15dB */ 153 0x13, /* 16dB */ 154 0x04, /* 17dB */ 155 0x01, /* 18dB */ 156 }; 157 158 const uint8_t snapper_basstab[] = { 159 0x96, /* -18dB */ 160 0x94, /* -17dB */ 161 0x92, /* -16dB */ 162 0x90, /* -15dB */ 163 0x8e, /* -14dB */ 164 0x8c, /* -13dB */ 165 0x8a, /* -12dB */ 166 0x88, /* -11dB */ 167 0x86, /* -10dB */ 168 0x84, /* -9dB */ 169 0x82, /* -8dB */ 170 0x80, /* -7dB */ 171 0x7e, /* -6dB */ 172 0x7c, /* -5dB */ 173 0x7a, /* -4dB */ 174 0x78, /* -3dB */ 175 0x76, /* -2dB */ 176 0x74, /* -1dB */ 177 0x72, /* 0dB */ 178 0x6f, /* 1dB */ 179 0x6d, /* 2dB */ 180 0x6a, /* 3dB */ 181 0x67, /* 4dB */ 182 0x65, /* 5dB */ 183 0x62, /* 6dB */ 184 0x5f, /* 7dB */ 185 0x5b, /* 8dB */ 186 0x55, /* 9dB */ 187 0x4f, /* 10dB */ 188 0x49, /* 11dB */ 189 0x43, /* 12dB */ 190 0x3b, /* 13dB */ 191 0x33, /* 14dB */ 192 0x29, /* 15dB */ 193 0x1e, /* 16dB */ 194 0x11, /* 17dB */ 195 0x01, /* 18dB */ 196 }; 197 198 struct { 199 int high, mid, low; 200 } snapper_volumetab[] = { 201 { 0x07, 0xF1, 0x7B }, /* 18.0 */ 202 { 0x07, 0x7F, 0xBB }, /* 17.5 */ 203 { 0x07, 0x14, 0x57 }, /* 17.0 */ 204 { 0x06, 0xAE, 0xF6 }, /* 16.5 */ 205 { 0x06, 0x4F, 0x40 }, /* 16.0 */ 206 { 0x05, 0xF4, 0xE5 }, /* 15.5 */ 207 { 0x05, 0x9F, 0x98 }, /* 15.0 */ 208 { 0x05, 0x4F, 0x10 }, /* 14.5 */ 209 { 0x05, 0x03, 0x0A }, /* 14.0 */ 210 { 0x04, 0xBB, 0x44 }, /* 13.5 */ 211 { 0x04, 0x77, 0x83 }, /* 13.0 */ 212 { 0x04, 0x37, 0x8B }, /* 12.5 */ 213 { 0x03, 0xFB, 0x28 }, /* 12.0 */ 214 { 0x03, 0xC2, 0x25 }, /* 11.5 */ 215 { 0x03, 0x8C, 0x53 }, /* 11.0 */ 216 { 0x03, 0x59, 0x83 }, /* 10.5 */ 217 { 0x03, 0x29, 0x8B }, /* 10.0 */ 218 { 0x02, 0xFC, 0x42 }, /* 9.5 */ 219 { 0x02, 0xD1, 0x82 }, /* 9.0 */ 220 { 0x02, 0xA9, 0x25 }, /* 8.5 */ 221 { 0x02, 0x83, 0x0B }, /* 8.0 */ 222 { 0x02, 0x5F, 0x12 }, /* 7.5 */ 223 { 0x02, 0x3D, 0x1D }, /* 7.0 */ 224 { 0x02, 0x1D, 0x0E }, /* 6.5 */ 225 { 0x01, 0xFE, 0xCA }, /* 6.0 */ 226 { 0x01, 0xE2, 0x37 }, /* 5.5 */ 227 { 0x01, 0xC7, 0x3D }, /* 5.0 */ 228 { 0x01, 0xAD, 0xC6 }, /* 4.5 */ 229 { 0x01, 0x95, 0xBC }, /* 4.0 */ 230 { 0x01, 0x7F, 0x09 }, /* 3.5 */ 231 { 0x01, 0x69, 0x9C }, /* 3.0 */ 232 { 0x01, 0x55, 0x62 }, /* 2.5 */ 233 { 0x01, 0x42, 0x49 }, /* 2.0 */ 234 { 0x01, 0x30, 0x42 }, /* 1.5 */ 235 { 0x01, 0x1F, 0x3D }, /* 1.0 */ 236 { 0x01, 0x0F, 0x2B }, /* 0.5 */ 237 { 0x01, 0x00, 0x00 }, /* 0.0 */ 238 { 0x00, 0xF1, 0xAE }, /* -0.5 */ 239 { 0x00, 0xE4, 0x29 }, /* -1.0 */ 240 { 0x00, 0xD7, 0x66 }, /* -1.5 */ 241 { 0x00, 0xCB, 0x59 }, /* -2.0 */ 242 { 0x00, 0xBF, 0xF9 }, /* -2.5 */ 243 { 0x00, 0xB5, 0x3C }, /* -3.0 */ 244 { 0x00, 0xAB, 0x19 }, /* -3.5 */ 245 { 0x00, 0xA1, 0x86 }, /* -4.0 */ 246 { 0x00, 0x98, 0x7D }, /* -4.5 */ 247 { 0x00, 0x8F, 0xF6 }, /* -5.0 */ 248 { 0x00, 0x87, 0xE8 }, /* -5.5 */ 249 { 0x00, 0x80, 0x4E }, /* -6.0 */ 250 { 0x00, 0x79, 0x20 }, /* -6.5 */ 251 { 0x00, 0x72, 0x5A }, /* -7.0 */ 252 { 0x00, 0x6B, 0xF4 }, /* -7.5 */ 253 { 0x00, 0x65, 0xEA }, /* -8.0 */ 254 { 0x00, 0x60, 0x37 }, /* -8.5 */ 255 { 0x00, 0x5A, 0xD5 }, /* -9.0 */ 256 { 0x00, 0x55, 0xC0 }, /* -9.5 */ 257 { 0x00, 0x50, 0xF4 }, /* -10.0 */ 258 { 0x00, 0x4C, 0x6D }, /* -10.5 */ 259 { 0x00, 0x48, 0x27 }, /* -11.0 */ 260 { 0x00, 0x44, 0x1D }, /* -11.5 */ 261 { 0x00, 0x40, 0x4E }, /* -12.0 */ 262 { 0x00, 0x3C, 0xB5 }, /* -12.5 */ 263 { 0x00, 0x39, 0x50 }, /* -13.0 */ 264 { 0x00, 0x36, 0x1B }, /* -13.5 */ 265 { 0x00, 0x33, 0x14 }, /* -14.0 */ 266 { 0x00, 0x30, 0x39 }, /* -14.5 */ 267 { 0x00, 0x2D, 0x86 }, /* -15.0 */ 268 { 0x00, 0x2A, 0xFA }, /* -15.5 */ 269 { 0x00, 0x28, 0x93 }, /* -16.0 */ 270 { 0x00, 0x26, 0x4E }, /* -16.5 */ 271 { 0x00, 0x24, 0x29 }, /* -17.0 */ 272 { 0x00, 0x22, 0x23 }, /* -17.5 */ 273 { 0x00, 0x20, 0x3A }, /* -18.0 */ 274 { 0x00, 0x1E, 0x6D }, /* -18.5 */ 275 { 0x00, 0x1C, 0xB9 }, /* -19.0 */ 276 { 0x00, 0x1B, 0x1E }, /* -19.5 */ 277 { 0x00, 0x19, 0x9A }, /* -20.0 */ 278 { 0x00, 0x18, 0x2B }, /* -20.5 */ 279 { 0x00, 0x16, 0xD1 }, /* -21.0 */ 280 { 0x00, 0x15, 0x8A }, /* -21.5 */ 281 { 0x00, 0x14, 0x56 }, /* -22.0 */ 282 { 0x00, 0x13, 0x33 }, /* -22.5 */ 283 { 0x00, 0x12, 0x20 }, /* -23.0 */ 284 { 0x00, 0x11, 0x1C }, /* -23.5 */ 285 { 0x00, 0x10, 0x27 }, /* -24.0 */ 286 { 0x00, 0x0F, 0x40 }, /* -24.5 */ 287 { 0x00, 0x0E, 0x65 }, /* -25.0 */ 288 { 0x00, 0x0D, 0x97 }, /* -25.5 */ 289 { 0x00, 0x0C, 0xD5 }, /* -26.0 */ 290 { 0x00, 0x0C, 0x1D }, /* -26.5 */ 291 { 0x00, 0x0B, 0x6F }, /* -27.0 */ 292 { 0x00, 0x0A, 0xCC }, /* -27.5 */ 293 { 0x00, 0x0A, 0x31 }, /* -28.0 */ 294 { 0x00, 0x09, 0x9F }, /* -28.5 */ 295 { 0x00, 0x09, 0x15 }, /* -29.0 */ 296 { 0x00, 0x08, 0x93 }, /* -29.5 */ 297 { 0x00, 0x08, 0x18 }, /* -30.0 */ 298 { 0x00, 0x07, 0xA5 }, /* -30.5 */ 299 { 0x00, 0x07, 0x37 }, /* -31.0 */ 300 { 0x00, 0x06, 0xD0 }, /* -31.5 */ 301 { 0x00, 0x06, 0x6E }, /* -32.0 */ 302 { 0x00, 0x06, 0x12 }, /* -32.5 */ 303 { 0x00, 0x05, 0xBB }, /* -33.0 */ 304 { 0x00, 0x05, 0x69 }, /* -33.5 */ 305 { 0x00, 0x05, 0x1C }, /* -34.0 */ 306 { 0x00, 0x04, 0xD2 }, /* -34.5 */ 307 { 0x00, 0x04, 0x8D }, /* -35.0 */ 308 { 0x00, 0x04, 0x4C }, /* -35.5 */ 309 { 0x00, 0x04, 0x0F }, /* -36.0 */ 310 { 0x00, 0x03, 0xD5 }, /* -36.5 */ 311 { 0x00, 0x03, 0x9E }, /* -37.0 */ 312 { 0x00, 0x03, 0x6A }, /* -37.5 */ 313 { 0x00, 0x03, 0x39 }, /* -38.0 */ 314 { 0x00, 0x03, 0x0B }, /* -38.5 */ 315 { 0x00, 0x02, 0xDF }, /* -39.0 */ 316 { 0x00, 0x02, 0xB6 }, /* -39.5 */ 317 { 0x00, 0x02, 0x8F }, /* -40.0 */ 318 { 0x00, 0x02, 0x6B }, /* -40.5 */ 319 { 0x00, 0x02, 0x48 }, /* -41.0 */ 320 { 0x00, 0x02, 0x27 }, /* -41.5 */ 321 { 0x00, 0x02, 0x09 }, /* -42.0 */ 322 { 0x00, 0x01, 0xEB }, /* -42.5 */ 323 { 0x00, 0x01, 0xD0 }, /* -43.0 */ 324 { 0x00, 0x01, 0xB6 }, /* -43.5 */ 325 { 0x00, 0x01, 0x9E }, /* -44.0 */ 326 { 0x00, 0x01, 0x86 }, /* -44.5 */ 327 { 0x00, 0x01, 0x71 }, /* -45.0 */ 328 { 0x00, 0x01, 0x5C }, /* -45.5 */ 329 { 0x00, 0x01, 0x48 }, /* -46.0 */ 330 { 0x00, 0x01, 0x36 }, /* -46.5 */ 331 { 0x00, 0x01, 0x25 }, /* -47.0 */ 332 { 0x00, 0x01, 0x14 }, /* -47.5 */ 333 { 0x00, 0x01, 0x05 }, /* -48.0 */ 334 { 0x00, 0x00, 0xF6 }, /* -48.5 */ 335 { 0x00, 0x00, 0xE9 }, /* -49.0 */ 336 { 0x00, 0x00, 0xDC }, /* -49.5 */ 337 { 0x00, 0x00, 0xCF }, /* -50.0 */ 338 { 0x00, 0x00, 0xC4 }, /* -50.5 */ 339 { 0x00, 0x00, 0xB9 }, /* -51.0 */ 340 { 0x00, 0x00, 0xAE }, /* -51.5 */ 341 { 0x00, 0x00, 0xA5 }, /* -52.0 */ 342 { 0x00, 0x00, 0x9B }, /* -52.5 */ 343 { 0x00, 0x00, 0x93 }, /* -53.0 */ 344 { 0x00, 0x00, 0x8B }, /* -53.5 */ 345 { 0x00, 0x00, 0x83 }, /* -54.0 */ 346 { 0x00, 0x00, 0x7B }, /* -54.5 */ 347 { 0x00, 0x00, 0x75 }, /* -55.0 */ 348 { 0x00, 0x00, 0x6E }, /* -55.5 */ 349 { 0x00, 0x00, 0x68 }, /* -56.0 */ 350 { 0x00, 0x00, 0x62 }, /* -56.5 */ 351 { 0x00, 0x00, 0x0 } /* Mute? */ 352 353 }; 354 355 /* TAS3004 registers */ 356 #define DEQ_MCR1 0x01 /* Main control register 1 (1byte) */ 357 #define DEQ_DRC 0x02 /* Dynamic range compression (6bytes?) */ 358 #define DEQ_VOLUME 0x04 /* Volume (6bytes) */ 359 #define DEQ_TREBLE 0x05 /* Treble control (1byte) */ 360 #define DEQ_BASS 0x06 /* Bass control (1byte) */ 361 #define DEQ_MIXER_L 0x07 /* Mixer left gain (9bytes) */ 362 #define DEQ_MIXER_R 0x08 /* Mixer right gain (9bytes) */ 363 #define DEQ_LB0 0x0a /* Left biquad 0 (15bytes) */ 364 #define DEQ_LB1 0x0b /* Left biquad 1 (15bytes) */ 365 #define DEQ_LB2 0x0c /* Left biquad 2 (15bytes) */ 366 #define DEQ_LB3 0x0d /* Left biquad 3 (15bytes) */ 367 #define DEQ_LB4 0x0e /* Left biquad 4 (15bytes) */ 368 #define DEQ_LB5 0x0f /* Left biquad 5 (15bytes) */ 369 #define DEQ_LB6 0x10 /* Left biquad 6 (15bytes) */ 370 #define DEQ_RB0 0x13 /* Right biquad 0 (15bytes) */ 371 #define DEQ_RB1 0x14 /* Right biquad 1 (15bytes) */ 372 #define DEQ_RB2 0x15 /* Right biquad 2 (15bytes) */ 373 #define DEQ_RB3 0x16 /* Right biquad 3 (15bytes) */ 374 #define DEQ_RB4 0x17 /* Right biquad 4 (15bytes) */ 375 #define DEQ_RB5 0x18 /* Right biquad 5 (15bytes) */ 376 #define DEQ_RB6 0x19 /* Right biquad 6 (15bytes) */ 377 #define DEQ_LLB 0x21 /* Left loudness biquad (15bytes) */ 378 #define DEQ_RLB 0x22 /* Right loudness biquad (15bytes) */ 379 #define DEQ_LLB_GAIN 0x23 /* Left loudness biquad gain (3bytes) */ 380 #define DEQ_RLB_GAIN 0x24 /* Right loudness biquad gain (3bytes) */ 381 #define DEQ_ACR 0x40 /* Analog control register (1byte) */ 382 #define DEQ_MCR2 0x43 /* Main control register 2 (1byte) */ 383 384 #define DEQ_MCR1_FL 0x80 /* Fast load */ 385 #define DEQ_MCR1_SC 0x40 /* SCLK frequency */ 386 #define DEQ_MCR1_SC_32 0x00 /* 32fs */ 387 #define DEQ_MCR1_SC_64 0x40 /* 64fs */ 388 #define DEQ_MCR1_SM 0x30 /* Output serial port mode */ 389 #define DEQ_MCR1_SM_L 0x00 /* Left justified */ 390 #define DEQ_MCR1_SM_R 0x10 /* Right justified */ 391 #define DEQ_MCR1_SM_I2S 0x20 /* I2S */ 392 #define DEQ_MCR1_W 0x03 /* Serial port word length */ 393 #define DEQ_MCR1_W_16 0x00 /* 16 bit */ 394 #define DEQ_MCR1_W_18 0x01 /* 18 bit */ 395 #define DEQ_MCR1_W_20 0x02 /* 20 bit */ 396 397 #define DEQ_MCR2_DL 0x80 /* Download */ 398 #define DEQ_MCR2_AP 0x02 /* All pass mode */ 399 400 #define DEQ_ACR_ADM 0x80 /* ADC output mode */ 401 #define DEQ_ACR_LRB 0x40 /* Select B input */ 402 #define DEQ_ACR_DM 0x0c /* De-emphasis control */ 403 #define DEQ_ACR_DM_OFF 0x00 /* off */ 404 #define DEQ_ACR_DM_48 0x04 /* fs = 48kHz */ 405 #define DEQ_ACR_DM_44 0x08 /* fs = 44.1kHz */ 406 #define DEQ_ACR_INP 0x02 /* Analog input select */ 407 #define DEQ_ACR_INP_A 0x00 /* A */ 408 #define DEQ_ACR_INP_B 0x02 /* B */ 409 #define DEQ_ACR_APD 0x01 /* Analog power down */ 410 411 struct tas3004_reg { 412 u_char MCR1[1]; 413 u_char DRC[6]; 414 u_char VOLUME[6]; 415 u_char TREBLE[1]; 416 u_char BASS[1]; 417 u_char MIXER_L[9]; 418 u_char MIXER_R[9]; 419 u_char LB0[15]; 420 u_char LB1[15]; 421 u_char LB2[15]; 422 u_char LB3[15]; 423 u_char LB4[15]; 424 u_char LB5[15]; 425 u_char LB6[15]; 426 u_char RB0[15]; 427 u_char RB1[15]; 428 u_char RB2[15]; 429 u_char RB3[15]; 430 u_char RB4[15]; 431 u_char RB5[15]; 432 u_char RB6[15]; 433 u_char LLB[15]; 434 u_char RLB[15]; 435 u_char LLB_GAIN[3]; 436 u_char RLB_GAIN[3]; 437 u_char ACR[1]; 438 u_char MCR2[1]; 439 }; 440 441 int 442 snapper_match(struct device *parent, void *match, void *aux) 443 { 444 struct confargs *ca = aux; 445 int soundbus, soundchip, soundcodec; 446 char compat[32]; 447 448 if (strcmp(ca->ca_name, "i2s") != 0) 449 return (0); 450 451 if ((soundbus = OF_child(ca->ca_node)) == 0 || 452 (soundchip = OF_child(soundbus)) == 0) 453 return (0); 454 455 bzero(compat, sizeof compat); 456 OF_getprop(soundchip, "compatible", compat, sizeof compat); 457 458 if (strcmp(compat, "snapper") == 0) 459 return (1); 460 461 if (OF_getprop(soundchip, "platform-tas-codec-ref", 462 &soundcodec, sizeof soundcodec) == sizeof soundcodec) 463 return (1); 464 465 return (0); 466 } 467 468 void 469 snapper_attach(struct device *parent, struct device *self, void *aux) 470 { 471 struct snapper_softc *sc = (struct snapper_softc *)self; 472 473 sc->sc_setvolume = snapper_set_volume; 474 sc->sc_setbass = snapper_set_bass; 475 sc->sc_settreble = snapper_set_treble; 476 sc->sc_setinput = snapper_set_input; 477 478 i2s_attach(parent, sc, aux); 479 config_defer(self, snapper_defer); 480 } 481 482 void 483 snapper_defer(struct device *dev) 484 { 485 struct snapper_softc *sc = (struct snapper_softc *)dev; 486 struct device *dv; 487 488 TAILQ_FOREACH(dv, &alldevs, dv_list) 489 if (strncmp(dv->dv_xname, "kiic", 4) == 0 && 490 strncmp(dv->dv_parent->dv_xname, "macobio", 7) == 0) 491 sc->sc_i2c = dv; 492 if (sc->sc_i2c == NULL) { 493 printf("%s: unable to find i2c\n", sc->sc_dev.dv_xname); 494 return; 495 } 496 497 /* XXX If i2c has failed to attach, what should we do? */ 498 499 audio_attach_mi(&snapper_hw_if, sc, &sc->sc_dev); 500 501 /* kiic_setmode(sc->sc_i2c, I2C_STDSUBMODE); */ 502 snapper_init(sc); 503 } 504 505 void 506 snapper_set_volume(struct snapper_softc *sc, int left, int right) 507 { 508 u_char vol[6]; 509 int nentries = sizeof(snapper_volumetab) / sizeof(snapper_volumetab[0]); 510 int l, r; 511 512 sc->sc_vol_l = left; 513 sc->sc_vol_r = right; 514 515 l = nentries - (left * nentries / 256); 516 r = nentries - (right * nentries / 256); 517 518 DPRINTF(" left %d vol %d %d, right %d vol %d %d\n", 519 left, l, nentries, 520 right, r, nentries); 521 if (l >= nentries) 522 l = nentries-1; 523 if (r >= nentries) 524 r = nentries-1; 525 526 vol[0] = snapper_volumetab[l].high; 527 vol[1] = snapper_volumetab[l].mid; 528 vol[2] = snapper_volumetab[l].low; 529 vol[3] = snapper_volumetab[r].high; 530 vol[4] = snapper_volumetab[r].mid; 531 vol[5] = snapper_volumetab[r].low; 532 533 tas3004_write(sc, DEQ_VOLUME, vol); 534 } 535 536 void 537 snapper_set_treble(struct snapper_softc *sc, int value) 538 { 539 uint8_t reg; 540 541 if ((value >= 0) && (value <= 255) && (value != sc->sc_treble)) { 542 reg = snapper_trebletab[(value >> 3) + 2]; 543 if (tas3004_write(sc, DEQ_TREBLE, ®) < 0) 544 return; 545 sc->sc_treble = value; 546 } 547 } 548 549 void 550 snapper_set_bass(struct snapper_softc *sc, int value) 551 { 552 uint8_t reg; 553 554 if ((value >= 0) && (value <= 255) && (value != sc->sc_bass)) { 555 reg = snapper_basstab[(value >> 3) + 2]; 556 if (tas3004_write(sc, DEQ_BASS, ®) < 0) 557 return; 558 sc->sc_bass = value; 559 } 560 } 561 562 void 563 snapper_set_input(struct snapper_softc *sc, int mask) 564 { 565 uint8_t val = 0; 566 567 switch (mask) { 568 case 1 << 0: /* microphone */ 569 val = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B; 570 break; 571 case 1 << 1: /* line in */ 572 val = 0; 573 break; 574 } 575 tas3004_write(sc, DEQ_ACR, &val); 576 } 577 578 const struct tas3004_reg tas3004_initdata = { 579 { DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_20 }, /* MCR1 */ 580 { 1, 0, 0, 0, 0, 0 }, /* DRC */ 581 { 0x00, 0xd7, 0x66, 0x00, 0xd7, 0x66 }, /* VOLUME */ 582 { 0x72 }, /* TREBLE */ 583 { 0x72 }, /* BASS */ 584 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_L */ 585 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_R */ 586 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 587 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 588 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 589 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 590 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 591 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 592 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 593 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 594 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 595 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 596 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 597 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 598 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 599 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 600 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 601 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 602 { 0, 0, 0 }, /* LLB_GAIN */ 603 { 0, 0, 0 }, /* RLB_GAIN */ 604 { DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B }, /* ACR */ 605 { 0 } /* MCR2 */ 606 }; 607 608 const char tas3004_regsize[] = { 609 0, /* 0x00 */ 610 sizeof tas3004_initdata.MCR1, /* 0x01 */ 611 sizeof tas3004_initdata.DRC, /* 0x02 */ 612 0, /* 0x03 */ 613 sizeof tas3004_initdata.VOLUME, /* 0x04 */ 614 sizeof tas3004_initdata.TREBLE, /* 0x05 */ 615 sizeof tas3004_initdata.BASS, /* 0x06 */ 616 sizeof tas3004_initdata.MIXER_L, /* 0x07 */ 617 sizeof tas3004_initdata.MIXER_R, /* 0x08 */ 618 0, /* 0x09 */ 619 sizeof tas3004_initdata.LB0, /* 0x0a */ 620 sizeof tas3004_initdata.LB1, /* 0x0b */ 621 sizeof tas3004_initdata.LB2, /* 0x0c */ 622 sizeof tas3004_initdata.LB3, /* 0x0d */ 623 sizeof tas3004_initdata.LB4, /* 0x0e */ 624 sizeof tas3004_initdata.LB5, /* 0x0f */ 625 sizeof tas3004_initdata.LB6, /* 0x10 */ 626 0, /* 0x11 */ 627 0, /* 0x12 */ 628 sizeof tas3004_initdata.RB0, /* 0x13 */ 629 sizeof tas3004_initdata.RB1, /* 0x14 */ 630 sizeof tas3004_initdata.RB2, /* 0x15 */ 631 sizeof tas3004_initdata.RB3, /* 0x16 */ 632 sizeof tas3004_initdata.RB4, /* 0x17 */ 633 sizeof tas3004_initdata.RB5, /* 0x18 */ 634 sizeof tas3004_initdata.RB6, /* 0x19 */ 635 0,0,0,0, 0,0, 636 0, /* 0x20 */ 637 sizeof tas3004_initdata.LLB, /* 0x21 */ 638 sizeof tas3004_initdata.RLB, /* 0x22 */ 639 sizeof tas3004_initdata.LLB_GAIN, /* 0x23 */ 640 sizeof tas3004_initdata.RLB_GAIN, /* 0x24 */ 641 0,0,0,0, 0,0,0,0, 0,0,0, 642 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 643 sizeof tas3004_initdata.ACR, /* 0x40 */ 644 0, /* 0x41 */ 645 0, /* 0x42 */ 646 sizeof tas3004_initdata.MCR2 /* 0x43 */ 647 }; 648 649 #define DEQaddr 0x6a 650 651 int 652 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data) 653 { 654 int size; 655 656 KASSERT(reg < sizeof tas3004_regsize); 657 size = tas3004_regsize[reg]; 658 KASSERT(size > 0); 659 660 if (kiic_write(sc->sc_i2c, DEQaddr, reg, data, size)) 661 return (-1); 662 663 return (0); 664 } 665 666 #define DEQ_WRITE(sc, reg, addr) \ 667 if (tas3004_write(sc, reg, addr)) goto err 668 669 int 670 tas3004_init(struct snapper_softc *sc) 671 { 672 deq_reset(sc); 673 674 DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0); 675 DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1); 676 DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2); 677 DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3); 678 DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4); 679 DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5); 680 DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6); 681 DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0); 682 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1); 683 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1); 684 DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2); 685 DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3); 686 DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4); 687 DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5); 688 DEQ_WRITE(sc, DEQ_RB6, tas3004_initdata.RB6); 689 DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1); 690 DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2); 691 DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC); 692 DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME); 693 DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE); 694 DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS); 695 DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L); 696 DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R); 697 DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB); 698 DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB); 699 DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN); 700 DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN); 701 DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR); 702 703 return (0); 704 err: 705 printf("%s: tas3004_init failed\n", sc->sc_dev.dv_xname); 706 return (-1); 707 } 708 709 void 710 snapper_init(struct snapper_softc *sc) 711 { 712 713 /* "sample-rates" (44100, 48000) */ 714 i2s_set_rate(sc, 44100); 715 716 #if 1 717 /* Enable I2C interrupts. */ 718 #define IER 4 719 #define I2C_INT_DATA 0x01 720 #define I2C_INT_ADDR 0x02 721 #define I2C_INT_STOP 0x04 722 kiic_writereg(sc->sc_i2c, IER,I2C_INT_DATA|I2C_INT_ADDR|I2C_INT_STOP); 723 #endif 724 725 if (tas3004_init(sc)) 726 return; 727 728 snapper_set_volume(sc, 190, 190); 729 snapper_set_treble(sc, 128); /* 0 dB */ 730 snapper_set_bass(sc, 128); /* 0 dB */ 731 732 /* Mic in, reflects tas3004_initdata.ACR */ 733 sc->sc_record_source = 1 << 1; 734 snapper_set_input(sc, sc->sc_record_source); 735 } 736 737 int 738 snapper_getdev(void *h, struct audio_device *retp) 739 { 740 *retp = snapper_device; 741 return (0); 742 } 743 744 void 745 snapper_get_default_params(void *addr, int mode, struct audio_params *params) 746 { 747 i2s_get_default_params(params); 748 } 749