1 /* $NetBSD: snapper.c,v 1.36 2010/11/12 12:26:29 phx Exp $ */ 2 /* Id: snapper.c,v 1.11 2002/10/31 17:42:13 tsubai Exp */ 3 /* Id: i2s.c,v 1.12 2005/01/15 14:32:35 tsubai Exp */ 4 5 /*- 6 * Copyright (c) 2002, 2003 Tsubai Masanari. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Datasheet is available from 33 * http://www.ti.com/sc/docs/products/analog/tas3004.html 34 * http://www.ti.com/sc/docs/products/analog/tas3001.html 35 */ 36 37 #include <sys/cdefs.h> 38 __KERNEL_RCSID(0, "$NetBSD: snapper.c,v 1.36 2010/11/12 12:26:29 phx Exp $"); 39 40 #include <sys/param.h> 41 #include <sys/audioio.h> 42 #include <sys/device.h> 43 #include <sys/systm.h> 44 #include <sys/malloc.h> 45 46 #include <dev/auconv.h> 47 #include <dev/audio_if.h> 48 #include <dev/mulaw.h> 49 #include <dev/ofw/openfirm.h> 50 #include <macppc/dev/dbdma.h> 51 52 #include <uvm/uvm_extern.h> 53 #include <dev/i2c/i2cvar.h> 54 55 #include <machine/autoconf.h> 56 #include <machine/pio.h> 57 58 #include <macppc/dev/deqvar.h> 59 #include <macppc/dev/obiovar.h> 60 61 #ifdef SNAPPER_DEBUG 62 # define DPRINTF printf 63 #else 64 # define DPRINTF while (0) printf 65 #endif 66 67 #define SNAPPER_MAXPAGES 16 68 69 struct snapper_softc { 70 device_t sc_dev; 71 int sc_mode; // 0 for TAS3004 72 #define SNAPPER_IS_TAS3001 1 // codec is TAS3001 73 #define SNAPPER_SWVOL 2 // software codec 74 75 int sc_node; 76 77 struct audio_encoding_set *sc_encodings; 78 79 void (*sc_ointr)(void *); /* dma completion intr handler */ 80 void *sc_oarg; /* arg for sc_ointr() */ 81 int sc_opages; /* # of output pages */ 82 83 void (*sc_iintr)(void *); /* dma completion intr handler */ 84 void *sc_iarg; /* arg for sc_iintr() */ 85 int sc_ipages; /* # of input pages */ 86 87 u_int sc_record_source; /* recording source mask */ 88 u_int sc_output_mask; /* output source mask */ 89 90 bus_space_tag_t sc_tag; 91 bus_space_handle_t sc_bsh; 92 i2c_addr_t sc_deqaddr; 93 i2c_tag_t sc_i2c; 94 uint32_t sc_baseaddr; 95 96 int sc_rate; /* current sampling rate */ 97 int sc_bitspersample; 98 99 int sc_swvol; 100 101 u_int sc_vol_l; 102 u_int sc_vol_r; 103 u_int sc_treble; 104 u_int sc_bass; 105 u_int mixer[6]; /* s1_l, s2_l, an_l, s1_r, s2_r, an_r */ 106 107 bus_space_handle_t sc_odmah; 108 bus_space_handle_t sc_idmah; 109 dbdma_regmap_t *sc_odma; 110 dbdma_regmap_t *sc_idma; 111 unsigned char dbdma_cmdspace[sizeof(struct dbdma_command) * 40 + 15]; 112 struct dbdma_command *sc_odmacmd; 113 struct dbdma_command *sc_idmacmd; 114 }; 115 116 static int snapper_match(device_t, struct cfdata *, void *); 117 static void snapper_attach(device_t, device_t, void *); 118 static void snapper_defer(device_t); 119 static int snapper_intr(void *); 120 static int snapper_query_encoding(void *, struct audio_encoding *); 121 static int snapper_set_params(void *, int, int, audio_params_t *, 122 audio_params_t *, stream_filter_list_t *, stream_filter_list_t *); 123 static int snapper_round_blocksize(void *, int, int, const audio_params_t *); 124 static int snapper_halt_output(void *); 125 static int snapper_halt_input(void *); 126 static int snapper_getdev(void *, struct audio_device *); 127 static int snapper_set_port(void *, mixer_ctrl_t *); 128 static int snapper_get_port(void *, mixer_ctrl_t *); 129 static int snapper_query_devinfo(void *, mixer_devinfo_t *); 130 static size_t snapper_round_buffersize(void *, int, size_t); 131 static paddr_t snapper_mappage(void *, void *, off_t, int); 132 static int snapper_get_props(void *); 133 static int snapper_trigger_output(void *, void *, void *, int, void (*)(void *), 134 void *, const audio_params_t *); 135 static int snapper_trigger_input(void *, void *, void *, int, void (*)(void *), 136 void *, const audio_params_t *); 137 static void snapper_set_volume(struct snapper_softc *, u_int, u_int); 138 static int snapper_set_rate(struct snapper_softc *); 139 static void snapper_set_treble(struct snapper_softc *, u_int); 140 static void snapper_set_bass(struct snapper_softc *, u_int); 141 static void snapper_write_mixers(struct snapper_softc *); 142 143 static int tas3004_write(struct snapper_softc *, u_int, const void *); 144 static int gpio_read(char *); 145 static void gpio_write(char *, int); 146 static void snapper_mute_speaker(struct snapper_softc *, int); 147 static void snapper_mute_headphone(struct snapper_softc *, int); 148 static int snapper_cint(void *); 149 static int tas3004_init(struct snapper_softc *); 150 static void snapper_init(struct snapper_softc *, int); 151 152 struct snapper_codecvar { 153 stream_filter_t base; 154 155 #ifdef DIAGNOSTIC 156 # define SNAPPER_CODECVAR_MAGIC 0xC0DEC 157 uint32_t magic; 158 #endif // DIAGNOSTIC 159 160 int16_t rval; // for snapper_fixphase 161 }; 162 163 static stream_filter_t *snapper_filter_factory 164 (int (*)(stream_fetcher_t *, audio_stream_t *, int)); 165 static void snapper_filter_dtor(stream_filter_t *); 166 167 /* XXX We can't access the hw device softc from our audio 168 * filter -- lame... 169 */ 170 static u_int snapper_vol_l = 128, snapper_vol_r = 128; 171 172 /* XXX why doesn't auconv define this? */ 173 #define DEFINE_FILTER(name) \ 174 static int \ 175 name##_fetch_to(stream_fetcher_t *, audio_stream_t *, int); \ 176 stream_filter_t * name(struct audio_softc *, \ 177 const audio_params_t *, const audio_params_t *); \ 178 stream_filter_t * \ 179 name(struct audio_softc *sc, const audio_params_t *from, \ 180 const audio_params_t *to) \ 181 { \ 182 return snapper_filter_factory(name##_fetch_to); \ 183 } \ 184 static int \ 185 name##_fetch_to(stream_fetcher_t *self, audio_stream_t *dst, int max_used) 186 187 DEFINE_FILTER(snapper_volume) 188 { 189 stream_filter_t *this; 190 int16_t j; 191 int16_t *wp; 192 int m, err; 193 194 this = (stream_filter_t *)self; 195 max_used = (max_used + 1) & ~1; 196 if ((err = this->prev->fetch_to(this->prev, this->src, max_used))) 197 return err; 198 m = (dst->end - dst->start) & ~1; 199 m = min(m, max_used); 200 FILTER_LOOP_PROLOGUE(this->src, 2, dst, 2, m) { 201 j = (s[0] << 8 | s[1]); 202 wp = (int16_t *)d; 203 *wp = ((j * snapper_vol_l) / 255); 204 } FILTER_LOOP_EPILOGUE(this->src, dst); 205 206 return 0; 207 } 208 209 /* 210 * A hardware bug in the TAS3004 I2S transport 211 * produces phase differences between channels 212 * (left channel appears delayed by one sample). 213 * Fix the phase difference by delaying the right channel 214 * by one sample. 215 */ 216 DEFINE_FILTER(snapper_fixphase) 217 { 218 struct snapper_codecvar *cv = (struct snapper_codecvar *) self; 219 stream_filter_t *this = &cv->base; 220 int err, m; 221 const int16_t *rp; 222 int16_t *wp, rval = cv->rval; 223 224 #ifdef DIAGNOSTIC 225 if (cv->magic != SNAPPER_CODECVAR_MAGIC) 226 panic("snapper_fixphase"); 227 #endif 228 max_used = (max_used + 3) & ~2; 229 if ((err = this->prev->fetch_to(this->prev, this->src, max_used))) 230 return err; 231 232 /* work in stereo frames (4 bytes) */ 233 m = (dst->end - dst->start) & ~2; 234 m = min(m, max_used); 235 FILTER_LOOP_PROLOGUE(this->src, 4, dst, 4, m) { 236 rp = (const int16_t *) s; 237 wp = (int16_t *) d; 238 wp[0] = rp[0]; 239 wp[1] = rval; 240 rval = rp[1]; 241 } FILTER_LOOP_EPILOGUE(this->src, dst); 242 cv->rval = rval; 243 244 return 0; 245 } 246 247 static stream_filter_t * 248 snapper_filter_factory(int (*fetch_to)(stream_fetcher_t *, audio_stream_t *, int)) 249 { 250 struct snapper_codecvar *this; 251 252 this = malloc(sizeof(*this), M_DEVBUF, M_WAITOK | M_ZERO); 253 this->base.base.fetch_to = fetch_to; 254 this->base.dtor = snapper_filter_dtor; 255 this->base.set_fetcher = stream_filter_set_fetcher; 256 this->base.set_inputbuffer = stream_filter_set_inputbuffer; 257 258 #ifdef DIAGNOSTIC 259 this->magic = SNAPPER_CODECVAR_MAGIC; 260 #endif 261 return (stream_filter_t *) this; 262 } 263 264 static void 265 snapper_filter_dtor(stream_filter_t *this) 266 { 267 if (this != NULL) 268 free(this, M_DEVBUF); 269 } 270 271 CFATTACH_DECL_NEW(snapper, sizeof(struct snapper_softc), snapper_match, 272 snapper_attach, NULL, NULL); 273 274 const struct audio_hw_if snapper_hw_if = { 275 NULL, 276 NULL, 277 NULL, 278 snapper_query_encoding, 279 snapper_set_params, 280 snapper_round_blocksize, 281 NULL, 282 NULL, 283 NULL, 284 NULL, 285 NULL, 286 snapper_halt_output, 287 snapper_halt_input, 288 NULL, 289 snapper_getdev, 290 NULL, 291 snapper_set_port, 292 snapper_get_port, 293 snapper_query_devinfo, 294 NULL, 295 NULL, 296 snapper_round_buffersize, 297 snapper_mappage, 298 snapper_get_props, 299 snapper_trigger_output, 300 snapper_trigger_input, 301 NULL, 302 NULL 303 }; 304 305 struct audio_device snapper_device = { 306 "SNAPPER", 307 "", 308 "snapper" 309 }; 310 311 #define SNAPPER_BASSTAB_0DB 18 312 const uint8_t snapper_basstab[] = { 313 0x96, /* -18dB */ 314 0x94, /* -17dB */ 315 0x92, /* -16dB */ 316 0x90, /* -15dB */ 317 0x8e, /* -14dB */ 318 0x8c, /* -13dB */ 319 0x8a, /* -12dB */ 320 0x88, /* -11dB */ 321 0x86, /* -10dB */ 322 0x84, /* -9dB */ 323 0x82, /* -8dB */ 324 0x80, /* -7dB */ 325 0x7e, /* -6dB */ 326 0x7c, /* -5dB */ 327 0x7a, /* -4dB */ 328 0x78, /* -3dB */ 329 0x76, /* -2dB */ 330 0x74, /* -1dB */ 331 0x72, /* 0dB */ 332 0x6f, /* 1dB */ 333 0x6d, /* 2dB */ 334 0x6a, /* 3dB */ 335 0x67, /* 4dB */ 336 0x65, /* 5dB */ 337 0x62, /* 6dB */ 338 0x5f, /* 7dB */ 339 0x5b, /* 8dB */ 340 0x55, /* 9dB */ 341 0x4f, /* 10dB */ 342 0x49, /* 11dB */ 343 0x43, /* 12dB */ 344 0x3b, /* 13dB */ 345 0x33, /* 14dB */ 346 0x29, /* 15dB */ 347 0x1e, /* 16dB */ 348 0x11, /* 17dB */ 349 0x01, /* 18dB */ 350 }; 351 352 #define SNAPPER_MIXER_GAIN_0DB 36 353 const uint8_t snapper_mixer_gain[178][3] = { 354 { 0x7f, 0x17, 0xaf }, /* 18.0 dB */ 355 { 0x77, 0xfb, 0xaa }, /* 17.5 dB */ 356 { 0x71, 0x45, 0x75 }, /* 17.0 dB */ 357 { 0x6a, 0xef, 0x5d }, /* 16.5 dB */ 358 { 0x64, 0xf4, 0x03 }, /* 16.0 dB */ 359 { 0x5f, 0x4e, 0x52 }, /* 15.5 dB */ 360 { 0x59, 0xf9, 0x80 }, /* 15.0 dB */ 361 { 0x54, 0xf1, 0x06 }, /* 14.5 dB */ 362 { 0x50, 0x30, 0xa1 }, /* 14.0 dB */ 363 { 0x4b, 0xb4, 0x46 }, /* 13.5 dB */ 364 { 0x47, 0x78, 0x28 }, /* 13.0 dB */ 365 { 0x43, 0x78, 0xb0 }, /* 12.5 dB */ 366 { 0x3f, 0xb2, 0x78 }, /* 12.0 dB */ 367 { 0x3c, 0x22, 0x4c }, /* 11.5 dB */ 368 { 0x38, 0xc5, 0x28 }, /* 11.0 dB */ 369 { 0x35, 0x98, 0x2f }, /* 10.5 dB */ 370 { 0x32, 0x98, 0xb0 }, /* 10.0 dB */ 371 { 0x2f, 0xc4, 0x20 }, /* 9.5 dB */ 372 { 0x2d, 0x18, 0x18 }, /* 9.0 dB */ 373 { 0x2a, 0x92, 0x54 }, /* 8.5 dB */ 374 { 0x28, 0x30, 0xaf }, /* 8.0 dB */ 375 { 0x25, 0xf1, 0x25 }, /* 7.5 dB */ 376 { 0x23, 0xd1, 0xcd }, /* 7.0 dB */ 377 { 0x21, 0xd0, 0xd9 }, /* 6.5 dB */ 378 { 0x1f, 0xec, 0x98 }, /* 6.0 dB */ 379 { 0x1e, 0x23, 0x6d }, /* 5.5 dB */ 380 { 0x1c, 0x73, 0xd5 }, /* 5.0 dB */ 381 { 0x1a, 0xdc, 0x61 }, /* 4.5 dB */ 382 { 0x19, 0x5b, 0xb8 }, /* 4.0 dB */ 383 { 0x17, 0xf0, 0x94 }, /* 3.5 dB */ 384 { 0x16, 0x99, 0xc0 }, /* 3.0 dB */ 385 { 0x15, 0x56, 0x1a }, /* 2.5 dB */ 386 { 0x14, 0x24, 0x8e }, /* 2.0 dB */ 387 { 0x13, 0x04, 0x1a }, /* 1.5 dB */ 388 { 0x11, 0xf3, 0xc9 }, /* 1.0 dB */ 389 { 0x10, 0xf2, 0xb4 }, /* 0.5 dB */ 390 { 0x10, 0x00, 0x00 }, /* 0.0 dB */ 391 { 0x0f, 0x1a, 0xdf }, /* -0.5 dB */ 392 { 0x0e, 0x42, 0x90 }, /* -1.0 dB */ 393 { 0x0d, 0x76, 0x5a }, /* -1.5 dB */ 394 { 0x0c, 0xb5, 0x91 }, /* -2.0 dB */ 395 { 0x0b, 0xff, 0x91 }, /* -2.5 dB */ 396 { 0x0b, 0x53, 0xbe }, /* -3.0 dB */ 397 { 0x0a, 0xb1, 0x89 }, /* -3.5 dB */ 398 { 0x0a, 0x18, 0x66 }, /* -4.0 dB */ 399 { 0x09, 0x87, 0xd5 }, /* -4.5 dB */ 400 { 0x08, 0xff, 0x59 }, /* -5.0 dB */ 401 { 0x08, 0x7e, 0x80 }, /* -5.5 dB */ 402 { 0x08, 0x04, 0xdc }, /* -6.0 dB */ 403 { 0x07, 0x92, 0x07 }, /* -6.5 dB */ 404 { 0x07, 0x25, 0x9d }, /* -7.0 dB */ 405 { 0x06, 0xbf, 0x44 }, /* -7.5 dB */ 406 { 0x06, 0x5e, 0xa5 }, /* -8.0 dB */ 407 { 0x06, 0x03, 0x6e }, /* -8.5 dB */ 408 { 0x05, 0xad, 0x50 }, /* -9.0 dB */ 409 { 0x05, 0x5c, 0x04 }, /* -9.5 dB */ 410 { 0x05, 0x0f, 0x44 }, /* -10.0 dB */ 411 { 0x04, 0xc6, 0xd0 }, /* -10.5 dB */ 412 { 0x04, 0x82, 0x68 }, /* -11.0 dB */ 413 { 0x04, 0x41, 0xd5 }, /* -11.5 dB */ 414 { 0x04, 0x04, 0xde }, /* -12.0 dB */ 415 { 0x03, 0xcb, 0x50 }, /* -12.5 dB */ 416 { 0x03, 0x94, 0xfa }, /* -13.0 dB */ 417 { 0x03, 0x61, 0xaf }, /* -13.5 dB */ 418 { 0x03, 0x31, 0x42 }, /* -14.0 dB */ 419 { 0x03, 0x03, 0x8a }, /* -14.5 dB */ 420 { 0x02, 0xd8, 0x62 }, /* -15.0 dB */ 421 { 0x02, 0xaf, 0xa3 }, /* -15.5 dB */ 422 { 0x02, 0x89, 0x2c }, /* -16.0 dB */ 423 { 0x02, 0x64, 0xdb }, /* -16.5 dB */ 424 { 0x02, 0x42, 0x93 }, /* -17.0 dB */ 425 { 0x02, 0x22, 0x35 }, /* -17.5 dB */ 426 { 0x02, 0x03, 0xa7 }, /* -18.0 dB */ 427 { 0x01, 0xe6, 0xcf }, /* -18.5 dB */ 428 { 0x01, 0xcb, 0x94 }, /* -19.0 dB */ 429 { 0x01, 0xb1, 0xde }, /* -19.5 dB */ 430 { 0x01, 0x99, 0x99 }, /* -20.0 dB */ 431 { 0x01, 0x82, 0xaf }, /* -20.5 dB */ 432 { 0x01, 0x6d, 0x0e }, /* -21.0 dB */ 433 { 0x01, 0x58, 0xa2 }, /* -21.5 dB */ 434 { 0x01, 0x45, 0x5b }, /* -22.0 dB */ 435 { 0x01, 0x33, 0x28 }, /* -22.5 dB */ 436 { 0x01, 0x21, 0xf9 }, /* -23.0 dB */ 437 { 0x01, 0x11, 0xc0 }, /* -23.5 dB */ 438 { 0x01, 0x02, 0x70 }, /* -24.0 dB */ 439 { 0x00, 0xf3, 0xfb }, /* -24.5 dB */ 440 { 0x00, 0xe6, 0x55 }, /* -25.0 dB */ 441 { 0x00, 0xd9, 0x73 }, /* -25.5 dB */ 442 { 0x00, 0xcd, 0x49 }, /* -26.0 dB */ 443 { 0x00, 0xc1, 0xcd }, /* -26.5 dB */ 444 { 0x00, 0xb6, 0xf6 }, /* -27.0 dB */ 445 { 0x00, 0xac, 0xba }, /* -27.5 dB */ 446 { 0x00, 0xa3, 0x10 }, /* -28.0 dB */ 447 { 0x00, 0x99, 0xf1 }, /* -28.5 dB */ 448 { 0x00, 0x91, 0x54 }, /* -29.0 dB */ 449 { 0x00, 0x89, 0x33 }, /* -29.5 dB */ 450 { 0x00, 0x81, 0x86 }, /* -30.0 dB */ 451 { 0x00, 0x7a, 0x48 }, /* -30.5 dB */ 452 { 0x00, 0x73, 0x70 }, /* -31.0 dB */ 453 { 0x00, 0x6c, 0xfb }, /* -31.5 dB */ 454 { 0x00, 0x66, 0xe3 }, /* -32.0 dB */ 455 { 0x00, 0x61, 0x21 }, /* -32.5 dB */ 456 { 0x00, 0x5b, 0xb2 }, /* -33.0 dB */ 457 { 0x00, 0x56, 0x91 }, /* -33.5 dB */ 458 { 0x00, 0x51, 0xb9 }, /* -34.0 dB */ 459 { 0x00, 0x4d, 0x27 }, /* -34.5 dB */ 460 { 0x00, 0x48, 0xd6 }, /* -35.0 dB */ 461 { 0x00, 0x44, 0xc3 }, /* -35.5 dB */ 462 { 0x00, 0x40, 0xea }, /* -36.0 dB */ 463 { 0x00, 0x3d, 0x49 }, /* -36.5 dB */ 464 { 0x00, 0x39, 0xdb }, /* -37.0 dB */ 465 { 0x00, 0x36, 0x9e }, /* -37.5 dB */ 466 { 0x00, 0x33, 0x90 }, /* -38.0 dB */ 467 { 0x00, 0x30, 0xae }, /* -38.5 dB */ 468 { 0x00, 0x2d, 0xf5 }, /* -39.0 dB */ 469 { 0x00, 0x2b, 0x63 }, /* -39.5 dB */ 470 { 0x00, 0x28, 0xf5 }, /* -40.0 dB */ 471 { 0x00, 0x26, 0xab }, /* -40.5 dB */ 472 { 0x00, 0x24, 0x81 }, /* -41.0 dB */ 473 { 0x00, 0x22, 0x76 }, /* -41.5 dB */ 474 { 0x00, 0x20, 0x89 }, /* -42.0 dB */ 475 { 0x00, 0x1e, 0xb7 }, /* -42.5 dB */ 476 { 0x00, 0x1c, 0xff }, /* -43.0 dB */ 477 { 0x00, 0x1b, 0x60 }, /* -43.5 dB */ 478 { 0x00, 0x19, 0xd8 }, /* -44.0 dB */ 479 { 0x00, 0x18, 0x65 }, /* -44.5 dB */ 480 { 0x00, 0x17, 0x08 }, /* -45.0 dB */ 481 { 0x00, 0x15, 0xbe }, /* -45.5 dB */ 482 { 0x00, 0x14, 0x87 }, /* -46.0 dB */ 483 { 0x00, 0x13, 0x61 }, /* -46.5 dB */ 484 { 0x00, 0x12, 0x4b }, /* -47.0 dB */ 485 { 0x00, 0x11, 0x45 }, /* -47.5 dB */ 486 { 0x00, 0x10, 0x4e }, /* -48.0 dB */ 487 { 0x00, 0x0f, 0x64 }, /* -48.5 dB */ 488 { 0x00, 0x0e, 0x88 }, /* -49.0 dB */ 489 { 0x00, 0x0d, 0xb8 }, /* -49.5 dB */ 490 { 0x00, 0x0c, 0xf3 }, /* -50.0 dB */ 491 { 0x00, 0x0c, 0x3a }, /* -50.5 dB */ 492 { 0x00, 0x0b, 0x8b }, /* -51.0 dB */ 493 { 0x00, 0x0a, 0xe5 }, /* -51.5 dB */ 494 { 0x00, 0x0a, 0x49 }, /* -52.0 dB */ 495 { 0x00, 0x09, 0xb6 }, /* -52.5 dB */ 496 { 0x00, 0x09, 0x2b }, /* -53.0 dB */ 497 { 0x00, 0x08, 0xa8 }, /* -53.5 dB */ 498 { 0x00, 0x08, 0x2c }, /* -54.0 dB */ 499 { 0x00, 0x07, 0xb7 }, /* -54.5 dB */ 500 { 0x00, 0x07, 0x48 }, /* -55.0 dB */ 501 { 0x00, 0x06, 0xe0 }, /* -55.5 dB */ 502 { 0x00, 0x06, 0x7d }, /* -56.0 dB */ 503 { 0x00, 0x06, 0x20 }, /* -56.5 dB */ 504 { 0x00, 0x05, 0xc9 }, /* -57.0 dB */ 505 { 0x00, 0x05, 0x76 }, /* -57.5 dB */ 506 { 0x00, 0x05, 0x28 }, /* -58.0 dB */ 507 { 0x00, 0x04, 0xde }, /* -58.5 dB */ 508 { 0x00, 0x04, 0x98 }, /* -59.0 dB */ 509 { 0x00, 0x04, 0x56 }, /* -59.5 dB */ 510 { 0x00, 0x04, 0x18 }, /* -60.0 dB */ 511 { 0x00, 0x03, 0xdd }, /* -60.5 dB */ 512 { 0x00, 0x03, 0xa6 }, /* -61.0 dB */ 513 { 0x00, 0x03, 0x72 }, /* -61.5 dB */ 514 { 0x00, 0x03, 0x40 }, /* -62.0 dB */ 515 { 0x00, 0x03, 0x12 }, /* -62.5 dB */ 516 { 0x00, 0x02, 0xe6 }, /* -63.0 dB */ 517 { 0x00, 0x02, 0xbc }, /* -63.5 dB */ 518 { 0x00, 0x02, 0x95 }, /* -64.0 dB */ 519 { 0x00, 0x02, 0x70 }, /* -64.5 dB */ 520 { 0x00, 0x02, 0x4d }, /* -65.0 dB */ 521 { 0x00, 0x02, 0x2c }, /* -65.5 dB */ 522 { 0x00, 0x02, 0x0d }, /* -66.0 dB */ 523 { 0x00, 0x01, 0xf0 }, /* -66.5 dB */ 524 { 0x00, 0x01, 0xd4 }, /* -67.0 dB */ 525 { 0x00, 0x01, 0xba }, /* -67.5 dB */ 526 { 0x00, 0x01, 0xa1 }, /* -68.0 dB */ 527 { 0x00, 0x01, 0x8a }, /* -68.5 dB */ 528 { 0x00, 0x01, 0x74 }, /* -69.0 dB */ 529 { 0x00, 0x01, 0x5f }, /* -69.5 dB */ 530 { 0x00, 0x01, 0x4b }, /* -70.0 dB */ 531 { 0x00, 0x00, 0x00 } /* Mute */ 532 }; 533 534 #define SNAPPER_NFORMATS 2 535 static const struct audio_format snapper_formats[SNAPPER_NFORMATS] = { 536 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16, 537 2, AUFMT_STEREO, 3, {32000, 44100, 48000}}, 538 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 24, 24, 539 2, AUFMT_STEREO, 3, {32000, 44100, 48000}}, 540 }; 541 542 #define TUMBLER_NFORMATS 1 543 static const struct audio_format tumbler_formats[TUMBLER_NFORMATS] = { 544 {NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_BE, 16, 16, 545 2, AUFMT_STEREO, 4, {32000, 44100, 48000, 96000}}, 546 }; 547 548 static u_char *amp_mute; 549 static u_char *headphone_mute; 550 static u_char *audio_hw_reset; 551 static u_char *headphone_detect; 552 static int headphone_detect_active; 553 554 555 /* I2S registers */ 556 #define I2S_INT 0x00 557 #define I2S_FORMAT 0x10 558 #define I2S_FRAMECOUNT 0x40 559 #define I2S_FRAMEMATCH 0x50 560 #define I2S_WORDSIZE 0x60 561 562 /* I2S_INT register definitions */ 563 #define I2S_INT_CLKSTOPPEND 0x01000000 /* clock-stop interrupt pending */ 564 565 /* FCR(0x3c) bits */ 566 #define KEYLARGO_FCR1 0x3c 567 #define I2S0CLKEN 0x1000 568 #define I2S0EN 0x2000 569 #define I2S1CLKEN 0x080000 570 #define I2S1EN 0x100000 571 #define FCR3C_BITMASK "\020\25I2S1EN\24I2S1CLKEN\16I2S0EN\15I2S0CLKEN" 572 573 /* TAS3004/TAS3001 registers */ 574 #define DEQ_MCR1 0x01 /* Main control register 1 (1byte) */ 575 #define DEQ_DRC 0x02 /* Dynamic range compression (6bytes?) 576 2 bytes (reserved) on the TAS 3001 */ 577 #define DEQ_VOLUME 0x04 /* Volume (6bytes) */ 578 #define DEQ_TREBLE 0x05 /* Treble control (1byte) */ 579 #define DEQ_BASS 0x06 /* Bass control (1byte) */ 580 #define DEQ_MIXER_L 0x07 /* Mixer left gain (9bytes; 3 on TAS3001) */ 581 #define DEQ_MIXER_R 0x08 /* Mixer right gain (9bytes; 3 on TAS3001) */ 582 #define DEQ_LB0 0x0a /* Left biquad 0 (15bytes) */ 583 #define DEQ_LB1 0x0b /* Left biquad 1 (15bytes) */ 584 #define DEQ_LB2 0x0c /* Left biquad 2 (15bytes) */ 585 #define DEQ_LB3 0x0d /* Left biquad 3 (15bytes) */ 586 #define DEQ_LB4 0x0e /* Left biquad 4 (15bytes) */ 587 #define DEQ_LB5 0x0f /* Left biquad 5 (15bytes) */ 588 #define DEQ_LB6 0x10 /* Left biquad 6 (15bytes) */ 589 #define DEQ_RB0 0x13 /* Right biquad 0 (15bytes) */ 590 #define DEQ_RB1 0x14 /* Right biquad 1 (15bytes) */ 591 #define DEQ_RB2 0x15 /* Right biquad 2 (15bytes) */ 592 #define DEQ_RB3 0x16 /* Right biquad 3 (15bytes) */ 593 #define DEQ_RB4 0x17 /* Right biquad 4 (15bytes) */ 594 #define DEQ_RB5 0x18 /* Right biquad 5 (15bytes) */ 595 #define DEQ_RB6 0x19 /* Right biquad 6 (15bytes) */ 596 #define DEQ_LLB 0x21 /* Left loudness biquad (15bytes) */ 597 #define DEQ_RLB 0x22 /* Right loudness biquad (15bytes) */ 598 #define DEQ_LLB_GAIN 0x23 /* Left loudness biquad gain (3bytes) */ 599 #define DEQ_RLB_GAIN 0x24 /* Right loudness biquad gain (3bytes) */ 600 #define DEQ_ACR 0x40 /* [TAS3004] Analog control register (1byte) */ 601 #define DEQ_MCR2 0x43 /* [TAS3004] Main control register 2 (1byte) */ 602 #define DEQ_MCR1_FL 0x80 /* Fast load */ 603 #define DEQ_MCR1_SC 0x40 /* SCLK frequency */ 604 #define DEQ_MCR1_SC_32 0x00 /* 32fs */ 605 #define DEQ_MCR1_SC_64 0x40 /* 64fs */ 606 #define DEQ_MCR1_SM 0x30 /* Output serial port mode */ 607 #define DEQ_MCR1_SM_L 0x00 /* Left justified */ 608 #define DEQ_MCR1_SM_R 0x10 /* Right justified */ 609 #define DEQ_MCR1_SM_I2S 0x20 /* I2S */ 610 #define DEQ_MCR1_ISM 0x0c /* [TAS3001] Input serial port mode */ 611 #define DEQ_MCR1_ISM_L 0x00 /* Left justified */ 612 #define DEQ_MCR1_ISM_R 0x04 /* Right justified */ 613 #define DEQ_MCR1_ISM_I2S 0x08 /* I2S */ 614 #define DEQ_MCR1_W 0x03 /* Serial port word length */ 615 #define DEQ_MCR1_W_16 0x00 /* 16 bit */ 616 #define DEQ_MCR1_W_18 0x01 /* 18 bit */ 617 #define DEQ_MCR1_W_20 0x02 /* 20 bit */ 618 #define DEQ_MCR1_W_24 0x03 /* 24 bit */ 619 620 #define DEQ_MCR2_DL 0x80 /* Download */ 621 #define DEQ_MCR2_AP 0x02 /* All pass mode */ 622 623 #define DEQ_ACR_ADM 0x80 /* ADC output mode */ 624 #define DEQ_ACR_LRB 0x40 /* Select B input */ 625 #define DEQ_ACR_DM 0x0c /* De-emphasis control */ 626 #define DEQ_ACR_DM_OFF 0x00 /* off */ 627 #define DEQ_ACR_DM_48 0x04 /* fs = 48kHz */ 628 #define DEQ_ACR_DM_44 0x08 /* fs = 44.1kHz */ 629 #define DEQ_ACR_INP 0x02 /* Analog input select */ 630 #define DEQ_ACR_INP_A 0x00 /* A */ 631 #define DEQ_ACR_INP_B 0x02 /* B */ 632 #define DEQ_ACR_APD 0x01 /* Analog power down */ 633 634 struct tas3004_reg { 635 u_char MCR1[1]; 636 u_char DRC[6]; 637 u_char VOLUME[6]; 638 u_char TREBLE[1]; 639 u_char BASS[1]; 640 u_char MIXER_L[9]; 641 u_char MIXER_R[9]; 642 u_char LB0[15]; 643 u_char LB1[15]; 644 u_char LB2[15]; 645 u_char LB3[15]; 646 u_char LB4[15]; 647 u_char LB5[15]; 648 u_char LB6[15]; 649 u_char RB0[15]; 650 u_char RB1[15]; 651 u_char RB2[15]; 652 u_char RB3[15]; 653 u_char RB4[15]; 654 u_char RB5[15]; 655 u_char RB6[15]; 656 u_char LLB[15]; 657 u_char RLB[15]; 658 u_char LLB_GAIN[3]; 659 u_char RLB_GAIN[3]; 660 u_char ACR[1]; 661 u_char MCR2[1]; 662 }; 663 664 #define GPIO_OUTSEL 0xf0 /* Output select */ 665 /* 0x00 GPIO bit0 is output 666 0x10 media-bay power 667 0x20 reserved 668 0x30 MPIC */ 669 670 #define GPIO_ALTOE 0x08 /* Alternate output enable */ 671 /* 0x00 Use DDR 672 0x08 Use output select */ 673 674 #define GPIO_DDR 0x04 /* Data direction */ 675 #define GPIO_DDR_OUTPUT 0x04 /* Output */ 676 #define GPIO_DDR_INPUT 0x00 /* Input */ 677 678 #define GPIO_LEVEL 0x02 /* Pin level (RO) */ 679 680 #define GPIO_DATA 0x01 /* Data */ 681 682 static int 683 snapper_match(device_t parent, struct cfdata *match, void *aux) 684 { 685 struct confargs *ca; 686 int soundbus, soundchip, soundcodec; 687 char compat[32]; 688 689 ca = aux; 690 if (strcmp(ca->ca_name, "i2s") != 0) 691 return 0; 692 693 if ((soundbus = OF_child(ca->ca_node)) == 0 || 694 (soundchip = OF_child(soundbus)) == 0) 695 return 0; 696 697 memset(compat, 0, sizeof compat); 698 OF_getprop(soundchip, "compatible", compat, sizeof compat); 699 700 if (strcmp(compat, "snapper") == 0) 701 return 1; 702 703 if (strcmp(compat, "tumbler") == 0) 704 return 1; 705 706 if (strcmp(compat, "AOAKeylargo") == 0) 707 return 1; 708 709 if (strcmp(compat, "AOAK2") == 0) 710 return 1; 711 712 if (OF_getprop(soundchip, "platform-tas-codec-ref", 713 &soundcodec, sizeof soundcodec) == sizeof soundcodec) 714 return 1; 715 716 return 0; 717 } 718 719 static void 720 snapper_attach(device_t parent, device_t self, void *aux) 721 { 722 struct snapper_softc *sc; 723 struct confargs *ca; 724 int cirq, oirq, iirq, cirq_type, oirq_type, iirq_type, soundbus; 725 uint32_t intr[6], reg[6]; 726 char compat[32]; 727 728 sc = device_private(self); 729 sc->sc_dev = self; 730 731 ca = aux; 732 733 soundbus = OF_child(ca->ca_node); 734 memset(compat, 0, sizeof compat); 735 OF_getprop(OF_child(soundbus), "compatible", compat, sizeof compat); 736 737 if (strcmp(compat, "tumbler") == 0) 738 sc->sc_mode = SNAPPER_IS_TAS3001; 739 740 if (sc->sc_mode == SNAPPER_IS_TAS3001) { 741 if (auconv_create_encodings(tumbler_formats, TUMBLER_NFORMATS, 742 &sc->sc_encodings) != 0) { 743 aprint_normal("can't create encodings\n"); 744 return; 745 } 746 } else { 747 if (auconv_create_encodings(snapper_formats, SNAPPER_NFORMATS, 748 &sc->sc_encodings) != 0) { 749 aprint_normal("can't create encodings\n"); 750 return; 751 } 752 } 753 754 sc->sc_odmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) * 755 sizeof(struct dbdma_command)); 756 sc->sc_idmacmd = dbdma_alloc((SNAPPER_MAXPAGES + 4) * 757 sizeof(struct dbdma_command)); 758 759 sc->sc_baseaddr = ca->ca_baseaddr; 760 OF_getprop(soundbus, "reg", reg, sizeof reg); 761 reg[0] += ca->ca_baseaddr; 762 reg[2] += ca->ca_baseaddr; 763 reg[4] += ca->ca_baseaddr; 764 765 sc->sc_node = ca->ca_node; 766 sc->sc_tag = ca->ca_tag; 767 768 bus_space_map(sc->sc_tag, reg[0], reg[1], 0, &sc->sc_bsh); 769 bus_space_map(sc->sc_tag, reg[2], reg[3], 770 BUS_SPACE_MAP_LINEAR, &sc->sc_odmah); 771 bus_space_map(sc->sc_tag, reg[4], reg[5], 772 BUS_SPACE_MAP_LINEAR, &sc->sc_idmah); 773 774 sc->sc_odma = bus_space_vaddr(sc->sc_tag, sc->sc_odmah); 775 sc->sc_idma = bus_space_vaddr(sc->sc_tag, sc->sc_idmah); 776 777 OF_getprop(soundbus, "interrupts", intr, sizeof intr); 778 cirq = intr[0]; 779 oirq = intr[2]; 780 iirq = intr[4]; 781 cirq_type = intr[1] ? IST_LEVEL : IST_EDGE; 782 oirq_type = intr[3] ? IST_LEVEL : IST_EDGE; 783 iirq_type = intr[5] ? IST_LEVEL : IST_EDGE; 784 785 /* intr_establish(cirq, cirq_type, IPL_AUDIO, snapper_intr, sc); */ 786 intr_establish(oirq, oirq_type, IPL_AUDIO, snapper_intr, sc); 787 intr_establish(iirq, iirq_type, IPL_AUDIO, snapper_intr, sc); 788 789 aprint_normal(": irq %d,%d,%d\n", cirq, oirq, iirq); 790 791 /* PMF event handler */ 792 pmf_device_register(sc->sc_dev, NULL, NULL); 793 794 config_defer(self, snapper_defer); 795 } 796 797 static void 798 snapper_defer(device_t dev) 799 { 800 struct snapper_softc *sc; 801 device_t dv; 802 deviter_t di; 803 struct deq_softc *deq; 804 805 sc = device_private(dev); 806 for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); 807 dv != NULL; 808 dv = deviter_next(&di)) { 809 if (device_is_a(dv, "deq")) { 810 deq = device_private(dv); 811 sc->sc_i2c = deq->sc_i2c; 812 sc->sc_deqaddr = deq->sc_address; 813 } 814 } 815 deviter_release(&di); 816 817 /* If we don't find a codec, it's not the end of the world; 818 * we can control the volume in software in this case. 819 */ 820 if (sc->sc_i2c == NULL) 821 sc->sc_mode = SNAPPER_SWVOL; 822 823 switch (sc->sc_mode) { 824 case SNAPPER_SWVOL: 825 aprint_verbose("%s: software codec\n", device_xname(dev)); 826 break; 827 case SNAPPER_IS_TAS3001: 828 aprint_verbose("%s: codec: TAS3001\n", device_xname(dev)); 829 break; 830 case 0: 831 aprint_verbose("%s: codec: TAS3004\n", device_xname(dev)); 832 break; 833 } 834 835 audio_attach_mi(&snapper_hw_if, sc, sc->sc_dev); 836 837 /* ki2c_setmode(sc->sc_i2c, I2C_STDSUBMODE); */ 838 snapper_init(sc, sc->sc_node); 839 } 840 841 static int 842 snapper_intr(void *v) 843 { 844 struct snapper_softc *sc; 845 struct dbdma_command *cmd; 846 int count; 847 int status; 848 849 sc = v; 850 cmd = sc->sc_odmacmd; 851 count = sc->sc_opages; 852 /* Fill used buffer(s). */ 853 while (count-- > 0) { 854 if ((in16rb(&cmd->d_command) & 0x30) == 0x30) { 855 status = in16rb(&cmd->d_status); 856 cmd->d_status = 0; 857 if (status) /* status == 0x8400 */ 858 if (sc->sc_ointr) 859 (*sc->sc_ointr)(sc->sc_oarg); 860 } 861 cmd++; 862 } 863 864 cmd = sc->sc_idmacmd; 865 count = sc->sc_ipages; 866 while (count-- > 0) { 867 if ((in16rb(&cmd->d_command) & 0x30) == 0x30) { 868 status = in16rb(&cmd->d_status); 869 cmd->d_status = 0; 870 if (status) /* status == 0x8400 */ 871 if (sc->sc_iintr) 872 (*sc->sc_iintr)(sc->sc_iarg); 873 } 874 cmd++; 875 } 876 877 878 return 1; 879 } 880 881 882 static int 883 snapper_query_encoding(void *h, struct audio_encoding *ae) 884 { 885 886 struct snapper_softc *sc = h; 887 888 return auconv_query_encoding(sc->sc_encodings, ae); 889 } 890 891 static int 892 snapper_set_params(void *h, int setmode, int usemode, 893 audio_params_t *play, audio_params_t *rec, 894 stream_filter_list_t *pfil, stream_filter_list_t *rfil) 895 { 896 struct snapper_softc *sc; 897 audio_params_t *p; 898 stream_filter_list_t *fil = NULL; /* XXX gcc */ 899 int mode; 900 901 sc = h; 902 p = NULL; 903 904 /* 905 * This device only has one clock, so make the sample rates match. 906 */ 907 if (play->sample_rate != rec->sample_rate && 908 usemode == (AUMODE_PLAY | AUMODE_RECORD)) { 909 if (setmode == AUMODE_PLAY) { 910 rec->sample_rate = play->sample_rate; 911 setmode |= AUMODE_RECORD; 912 } else if (setmode == AUMODE_RECORD) { 913 play->sample_rate = rec->sample_rate; 914 setmode |= AUMODE_PLAY; 915 } else 916 return EINVAL; 917 } 918 919 for (mode = AUMODE_RECORD; mode != -1; 920 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { 921 if ((setmode & mode) == 0) 922 continue; 923 924 p = mode == AUMODE_PLAY ? play : rec; 925 fil = mode == AUMODE_PLAY ? pfil : rfil; 926 if (sc->sc_mode == SNAPPER_IS_TAS3001) { 927 if (auconv_set_converter(tumbler_formats, 928 TUMBLER_NFORMATS, mode, p, true, fil) < 0) { 929 DPRINTF("snapper_set_params: " 930 "auconv_set_converter failed\n"); 931 return EINVAL; 932 } 933 } else { /* TAS 3004 */ 934 if (auconv_set_converter(snapper_formats, 935 SNAPPER_NFORMATS, mode, p, true, fil) < 0) { 936 DPRINTF("snapper_set_params: " 937 "auconv_set_converter failed\n"); 938 return EINVAL; 939 } 940 } 941 942 if (fil->req_size > 0) 943 p = &fil->filters[0].param; 944 if (p->precision == 16) { 945 if (sc->sc_mode == SNAPPER_SWVOL) 946 fil->prepend(fil, snapper_volume, p); 947 else if (sc->sc_mode == 0 && p->channels == 2) { 948 /* 949 * Fix phase problems on TAS3004. 950 * This filter must go last on the chain, 951 * so prepend it, not append it. 952 */ 953 fil->prepend(fil, snapper_fixphase, p); 954 } 955 } 956 } 957 958 /* Set the speed. p points HW encoding. */ 959 if (p) { 960 sc->sc_rate = p->sample_rate; 961 sc->sc_bitspersample = p->precision; 962 } 963 return 0; 964 } 965 966 static int 967 snapper_round_blocksize(void *h, int size, int mode, 968 const audio_params_t *param) 969 { 970 971 if (size < NBPG) 972 size = NBPG; 973 return size & ~PGOFSET; 974 } 975 976 static int 977 snapper_halt_output(void *h) 978 { 979 struct snapper_softc *sc; 980 981 sc = h; 982 dbdma_stop(sc->sc_odma); 983 dbdma_reset(sc->sc_odma); 984 sc->sc_ointr = NULL; 985 return 0; 986 } 987 988 static int 989 snapper_halt_input(void *h) 990 { 991 struct snapper_softc *sc; 992 993 sc = h; 994 dbdma_stop(sc->sc_idma); 995 dbdma_reset(sc->sc_idma); 996 sc->sc_iintr = NULL; 997 return 0; 998 } 999 1000 static int 1001 snapper_getdev(void *h, struct audio_device *retp) 1002 { 1003 1004 *retp = snapper_device; 1005 return 0; 1006 } 1007 1008 enum { 1009 SNAPPER_MONITOR_CLASS, 1010 SNAPPER_OUTPUT_CLASS, 1011 SNAPPER_RECORD_CLASS, 1012 SNAPPER_OUTPUT_SELECT, 1013 SNAPPER_VOL_OUTPUT, 1014 SNAPPER_DIGI1, 1015 SNAPPER_DIGI2, 1016 SNAPPER_VOL_INPUT, 1017 SNAPPER_TREBLE, 1018 SNAPPER_BASS, 1019 /* From this point, unsupported by the TAS 3001 */ 1020 SNAPPER_ANALOG, 1021 SNAPPER_INPUT_SELECT, 1022 SNAPPER_ENUM_LAST 1023 }; 1024 1025 static int 1026 snapper_set_port(void *h, mixer_ctrl_t *mc) 1027 { 1028 struct snapper_softc *sc; 1029 int l, r; 1030 u_char data; 1031 1032 DPRINTF("snapper_set_port dev = %d, type = %d\n", mc->dev, mc->type); 1033 sc = h; 1034 l = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 1035 r = mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 1036 1037 switch (mc->dev) { 1038 case SNAPPER_OUTPUT_SELECT: 1039 /* No change necessary? */ 1040 if (mc->un.mask == sc->sc_output_mask) 1041 return 0; 1042 1043 snapper_mute_speaker(sc, 1); 1044 snapper_mute_headphone(sc, 1); 1045 if (mc->un.mask & 1 << 0) 1046 snapper_mute_speaker(sc, 0); 1047 if (mc->un.mask & 1 << 1) 1048 snapper_mute_headphone(sc, 0); 1049 1050 sc->sc_output_mask = mc->un.mask; 1051 return 0; 1052 1053 case SNAPPER_VOL_OUTPUT: 1054 snapper_set_volume(sc, l, r); 1055 return 0; 1056 1057 case SNAPPER_INPUT_SELECT: 1058 if (sc->sc_mode != 0) 1059 return ENXIO; 1060 1061 /* no change necessary? */ 1062 if (mc->un.mask == sc->sc_record_source) 1063 return 0; 1064 switch (mc->un.mask) { 1065 case 1 << 0: /* microphone */ 1066 /* Select right channel of B input */ 1067 data = DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B; 1068 tas3004_write(sc, DEQ_ACR, &data); 1069 break; 1070 case 1 << 1: /* line in */ 1071 /* Select both channels of A input */ 1072 data = 0; 1073 tas3004_write(sc, DEQ_ACR, &data); 1074 break; 1075 default: /* invalid argument */ 1076 return EINVAL; 1077 } 1078 sc->sc_record_source = mc->un.mask; 1079 return 0; 1080 1081 case SNAPPER_VOL_INPUT: 1082 /* XXX TO BE DONE */ 1083 return 0; 1084 1085 case SNAPPER_BASS: 1086 if (sc->sc_mode == SNAPPER_SWVOL) 1087 return ENXIO; 1088 snapper_set_bass(sc, l); 1089 return 0; 1090 case SNAPPER_TREBLE: 1091 if (sc->sc_mode == SNAPPER_SWVOL) 1092 return ENXIO; 1093 snapper_set_treble(sc, l); 1094 return 0; 1095 case SNAPPER_DIGI1: 1096 if (sc->sc_mode == SNAPPER_SWVOL) 1097 return ENXIO; 1098 1099 sc->mixer[0] = l; 1100 sc->mixer[3] = r; 1101 snapper_write_mixers(sc); 1102 return 0; 1103 case SNAPPER_DIGI2: 1104 if (sc->sc_mode == SNAPPER_SWVOL) 1105 return ENXIO; 1106 1107 if (sc->sc_mode == SNAPPER_IS_TAS3001) 1108 sc->mixer[3] = l; 1109 else { 1110 sc->mixer[1] = l; 1111 sc->mixer[4] = r; 1112 } 1113 snapper_write_mixers(sc); 1114 return 0; 1115 case SNAPPER_ANALOG: 1116 if (sc->sc_mode != 0) 1117 return ENXIO; 1118 1119 sc->mixer[2] = l; 1120 sc->mixer[5] = r; 1121 snapper_write_mixers(sc); 1122 return 0; 1123 } 1124 return ENXIO; 1125 } 1126 1127 static int 1128 snapper_get_port(void *h, mixer_ctrl_t *mc) 1129 { 1130 struct snapper_softc *sc; 1131 1132 DPRINTF("snapper_get_port dev = %d, type = %d\n", mc->dev, mc->type); 1133 sc = h; 1134 switch (mc->dev) { 1135 case SNAPPER_OUTPUT_SELECT: 1136 mc->un.mask = sc->sc_output_mask; 1137 return 0; 1138 1139 case SNAPPER_VOL_OUTPUT: 1140 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->sc_vol_l; 1141 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->sc_vol_r; 1142 return 0; 1143 1144 case SNAPPER_INPUT_SELECT: 1145 if (sc->sc_mode != 0) 1146 return ENXIO; 1147 1148 mc->un.mask = sc->sc_record_source; 1149 return 0; 1150 1151 case SNAPPER_VOL_INPUT: 1152 /* XXX TO BE DONE */ 1153 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 0; 1154 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 0; 1155 return 0; 1156 1157 case SNAPPER_TREBLE: 1158 if (sc->sc_mode == SNAPPER_SWVOL) 1159 return ENXIO; 1160 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_treble; 1161 return 0; 1162 case SNAPPER_BASS: 1163 if (sc->sc_mode == SNAPPER_SWVOL) 1164 return ENXIO; 1165 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_bass; 1166 return 0; 1167 1168 case SNAPPER_DIGI1: 1169 if (sc->sc_mode == SNAPPER_SWVOL) 1170 return ENXIO; 1171 1172 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[0]; 1173 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[3]; 1174 return 0; 1175 case SNAPPER_DIGI2: 1176 if (sc->sc_mode == SNAPPER_SWVOL) 1177 return ENXIO; 1178 1179 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[1]; 1180 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[4]; 1181 return 0; 1182 case SNAPPER_ANALOG: 1183 if (sc->sc_mode != 0) 1184 return ENXIO; 1185 1186 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = sc->mixer[2]; 1187 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = sc->mixer[5]; 1188 return 0; 1189 default: 1190 return ENXIO; 1191 } 1192 1193 return 0; 1194 } 1195 1196 static int 1197 snapper_query_devinfo(void *h, mixer_devinfo_t *dip) 1198 { 1199 struct snapper_softc *sc = h; 1200 1201 switch (dip->index) { 1202 1203 case SNAPPER_OUTPUT_SELECT: 1204 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1205 strcpy(dip->label.name, AudioNoutput); 1206 dip->type = AUDIO_MIXER_SET; 1207 dip->prev = dip->next = AUDIO_MIXER_LAST; 1208 dip->un.s.num_mem = 2; 1209 strcpy(dip->un.s.member[0].label.name, AudioNspeaker); 1210 dip->un.s.member[0].mask = 1 << 0; 1211 strcpy(dip->un.s.member[1].label.name, AudioNheadphone); 1212 dip->un.s.member[1].mask = 1 << 1; 1213 return 0; 1214 1215 case SNAPPER_VOL_OUTPUT: 1216 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1217 strcpy(dip->label.name, AudioNmaster); 1218 dip->type = AUDIO_MIXER_VALUE; 1219 dip->prev = dip->next = AUDIO_MIXER_LAST; 1220 dip->un.v.num_channels = 2; 1221 dip->un.v.delta = 16; 1222 strcpy(dip->un.v.units.name, AudioNvolume); 1223 return 0; 1224 1225 case SNAPPER_INPUT_SELECT: 1226 if (sc->sc_mode != 0) 1227 return ENXIO; 1228 1229 dip->mixer_class = SNAPPER_RECORD_CLASS; 1230 strcpy(dip->label.name, AudioNsource); 1231 dip->type = AUDIO_MIXER_SET; 1232 dip->prev = dip->next = AUDIO_MIXER_LAST; 1233 dip->un.s.num_mem = 2; 1234 strcpy(dip->un.s.member[0].label.name, AudioNmicrophone); 1235 dip->un.s.member[0].mask = 1 << 0; 1236 strcpy(dip->un.s.member[1].label.name, AudioNline); 1237 dip->un.s.member[1].mask = 1 << 1; 1238 return 0; 1239 1240 case SNAPPER_VOL_INPUT: 1241 dip->mixer_class = SNAPPER_RECORD_CLASS; 1242 strcpy(dip->label.name, AudioNrecord); 1243 dip->type = AUDIO_MIXER_VALUE; 1244 dip->prev = dip->next = AUDIO_MIXER_LAST; 1245 dip->un.v.num_channels = 2; 1246 strcpy(dip->un.v.units.name, AudioNvolume); 1247 return 0; 1248 1249 case SNAPPER_MONITOR_CLASS: 1250 dip->mixer_class = SNAPPER_MONITOR_CLASS; 1251 strcpy(dip->label.name, AudioCmonitor); 1252 dip->type = AUDIO_MIXER_CLASS; 1253 dip->next = dip->prev = AUDIO_MIXER_LAST; 1254 return 0; 1255 1256 case SNAPPER_OUTPUT_CLASS: 1257 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1258 strcpy(dip->label.name, AudioCoutputs); 1259 dip->type = AUDIO_MIXER_CLASS; 1260 dip->next = dip->prev = AUDIO_MIXER_LAST; 1261 return 0; 1262 1263 case SNAPPER_RECORD_CLASS: 1264 dip->mixer_class = SNAPPER_RECORD_CLASS; 1265 strcpy(dip->label.name, AudioCrecord); 1266 dip->type = AUDIO_MIXER_CLASS; 1267 dip->next = dip->prev = AUDIO_MIXER_LAST; 1268 return 0; 1269 1270 case SNAPPER_TREBLE: 1271 if (sc->sc_mode == SNAPPER_SWVOL) 1272 return ENXIO; 1273 1274 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1275 strcpy(dip->label.name, AudioNtreble); 1276 dip->type = AUDIO_MIXER_VALUE; 1277 dip->prev = dip->next = AUDIO_MIXER_LAST; 1278 dip->un.v.num_channels = 1; 1279 return 0; 1280 1281 case SNAPPER_BASS: 1282 if (sc->sc_mode == SNAPPER_SWVOL) 1283 return ENXIO; 1284 1285 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1286 strcpy(dip->label.name, AudioNbass); 1287 dip->type = AUDIO_MIXER_VALUE; 1288 dip->prev = dip->next = AUDIO_MIXER_LAST; 1289 dip->un.v.num_channels = 1; 1290 return 0; 1291 1292 case SNAPPER_DIGI1: 1293 if (sc->sc_mode == SNAPPER_SWVOL) 1294 return ENXIO; 1295 1296 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1297 strcpy(dip->label.name, AudioNdac); 1298 dip->type = AUDIO_MIXER_VALUE; 1299 dip->prev = dip->next = AUDIO_MIXER_LAST; 1300 dip->un.v.num_channels = 1301 sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2; 1302 return 0; 1303 case SNAPPER_DIGI2: 1304 if (sc->sc_mode == SNAPPER_SWVOL) 1305 return ENXIO; 1306 1307 dip->mixer_class = SNAPPER_OUTPUT_CLASS; 1308 strcpy(dip->label.name, AudioNline); 1309 dip->type = AUDIO_MIXER_VALUE; 1310 dip->prev = dip->next = AUDIO_MIXER_LAST; 1311 dip->un.v.num_channels = 1312 sc->sc_mode == SNAPPER_IS_TAS3001? 1 : 2; 1313 return 0; 1314 case SNAPPER_ANALOG: 1315 if (sc->sc_mode != 0) 1316 return ENXIO; 1317 1318 dip->mixer_class = SNAPPER_MONITOR_CLASS; 1319 strcpy(dip->label.name, AudioNmicrophone); 1320 dip->type = AUDIO_MIXER_VALUE; 1321 dip->prev = dip->next = AUDIO_MIXER_LAST; 1322 dip->un.v.num_channels = 2; 1323 return 0; 1324 } 1325 1326 return ENXIO; 1327 } 1328 1329 static size_t 1330 snapper_round_buffersize(void *h, int dir, size_t size) 1331 { 1332 1333 if (size > 65536) 1334 size = 65536; 1335 return size; 1336 } 1337 1338 static paddr_t 1339 snapper_mappage(void *h, void *mem, off_t off, int prot) 1340 { 1341 1342 if (off < 0) 1343 return -1; 1344 return -1; /* XXX */ 1345 } 1346 1347 static int 1348 snapper_get_props(void *h) 1349 { 1350 return AUDIO_PROP_FULLDUPLEX /* | AUDIO_PROP_MMAP */; 1351 } 1352 1353 static int 1354 snapper_trigger_output(void *h, void *start, void *end, int bsize, 1355 void (*intr)(void *), void *arg, 1356 const audio_params_t *param) 1357 { 1358 struct snapper_softc *sc; 1359 struct dbdma_command *cmd; 1360 vaddr_t va; 1361 int i, len, intmode; 1362 int res; 1363 1364 DPRINTF("trigger_output %p %p 0x%x\n", start, end, bsize); 1365 sc = h; 1366 1367 if ((res = snapper_set_rate(sc)) != 0) 1368 return res; 1369 1370 cmd = sc->sc_odmacmd; 1371 sc->sc_ointr = intr; 1372 sc->sc_oarg = arg; 1373 sc->sc_opages = ((char *)end - (char *)start) / NBPG; 1374 1375 #ifdef DIAGNOSTIC 1376 if (sc->sc_opages > SNAPPER_MAXPAGES) 1377 panic("snapper_trigger_output"); 1378 #endif 1379 1380 va = (vaddr_t)start; 1381 len = 0; 1382 for (i = sc->sc_opages; i > 0; i--) { 1383 len += NBPG; 1384 if (len < bsize) 1385 intmode = 0; 1386 else { 1387 len = 0; 1388 intmode = DBDMA_INT_ALWAYS; 1389 } 1390 1391 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_MORE, 0, NBPG, vtophys(va), 1392 intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER); 1393 cmd++; 1394 va += NBPG; 1395 } 1396 1397 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 1398 0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER, 1399 DBDMA_BRANCH_ALWAYS); 1400 1401 out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_odmacmd)); 1402 1403 dbdma_start(sc->sc_odma, sc->sc_odmacmd); 1404 1405 return 0; 1406 } 1407 1408 static int 1409 snapper_trigger_input(void *h, void *start, void *end, int bsize, 1410 void (*intr)(void *), void *arg, 1411 const audio_params_t *param) 1412 { 1413 struct snapper_softc *sc; 1414 struct dbdma_command *cmd; 1415 vaddr_t va; 1416 int i, len, intmode; 1417 int res; 1418 1419 DPRINTF("trigger_input %p %p 0x%x\n", start, end, bsize); 1420 sc = h; 1421 1422 if ((res = snapper_set_rate(sc)) != 0) 1423 return res; 1424 1425 cmd = sc->sc_idmacmd; 1426 sc->sc_iintr = intr; 1427 sc->sc_iarg = arg; 1428 sc->sc_ipages = ((char *)end - (char *)start) / NBPG; 1429 1430 #ifdef DIAGNOSTIC 1431 if (sc->sc_ipages > SNAPPER_MAXPAGES) 1432 panic("snapper_trigger_input"); 1433 #endif 1434 1435 va = (vaddr_t)start; 1436 len = 0; 1437 for (i = sc->sc_ipages; i > 0; i--) { 1438 len += NBPG; 1439 if (len < bsize) 1440 intmode = 0; 1441 else { 1442 len = 0; 1443 intmode = DBDMA_INT_ALWAYS; 1444 } 1445 1446 DBDMA_BUILD(cmd, DBDMA_CMD_IN_MORE, 0, NBPG, vtophys(va), 1447 intmode, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER); 1448 cmd++; 1449 va += NBPG; 1450 } 1451 1452 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 1453 0/*vtophys((vaddr_t)sc->sc_odmacmd)*/, 0, DBDMA_WAIT_NEVER, 1454 DBDMA_BRANCH_ALWAYS); 1455 1456 out32rb(&cmd->d_cmddep, vtophys((vaddr_t)sc->sc_idmacmd)); 1457 1458 dbdma_start(sc->sc_idma, sc->sc_idmacmd); 1459 1460 return 0; 1461 } 1462 1463 static void 1464 snapper_set_volume(struct snapper_softc *sc, u_int left, u_int right) 1465 { 1466 u_char regs[6]; 1467 int l, r; 1468 1469 left = min(255, left); 1470 right = min(255, right); 1471 1472 if (sc->sc_mode == SNAPPER_SWVOL) { 1473 snapper_vol_l = left; 1474 snapper_vol_r = right; 1475 } else { 1476 /* 1477 * for some insane reason the gain table for master volume and the 1478 * mixer channels is almost identical - just shifted by 4 bits 1479 * so we use the mixer_gain table and bit-twiddle it... 1480 */ 1481 l = 177 - (left * 178 / 256); 1482 regs[0] = (snapper_mixer_gain[l][0] >> 4); 1483 regs[1] = ((snapper_mixer_gain[l][0] & 0x0f) << 4) | 1484 (snapper_mixer_gain[l][1] >> 4); 1485 regs[2] = ((snapper_mixer_gain[l][1] & 0x0f) << 4) | 1486 (snapper_mixer_gain[l][2] >> 4); 1487 1488 r = 177 - (right * 178 / 256); 1489 regs[3] = (snapper_mixer_gain[r][0] >> 4); 1490 regs[4] = ((snapper_mixer_gain[r][0] & 0x0f) << 4) | 1491 (snapper_mixer_gain[r][1] >> 4); 1492 regs[5] = ((snapper_mixer_gain[r][1] & 0x0f) << 4) | 1493 (snapper_mixer_gain[r][2] >> 4); 1494 1495 tas3004_write(sc, DEQ_VOLUME, regs); 1496 1497 DPRINTF("%d %02x %02x %02x : %d %02x %02x %02x\n", l, regs[0], 1498 regs[1], regs[2], r, regs[3], regs[4], regs[5]); 1499 } 1500 1501 sc->sc_vol_l = left; 1502 sc->sc_vol_r = right; 1503 } 1504 1505 static void 1506 snapper_set_basstreble(struct snapper_softc *sc, u_int val, u_int mode) 1507 { 1508 int i = val & 0xFF; 1509 uint8_t reg; 1510 1511 /* 1512 * Make 128 match the 0 dB point 1513 */ 1514 i = (i - (128 - (SNAPPER_BASSTAB_0DB << 2))) >> 2; 1515 if (i < 0) 1516 i = 0; 1517 else if (i >= sizeof(snapper_basstab)) 1518 i = sizeof(snapper_basstab) - 1; 1519 reg = snapper_basstab[i]; 1520 1521 if (sc->sc_mode == SNAPPER_IS_TAS3001 && 1522 mode == DEQ_BASS) { 1523 /* 1524 * XXX -- The TAS3001 bass table is different 1525 * than the other tables. 1526 */ 1527 reg = (reg >> 1) + 5; // map 0x72 -> 0x3E (0 dB) 1528 } 1529 1530 tas3004_write(sc, mode, ®); 1531 } 1532 1533 static void 1534 snapper_set_treble(struct snapper_softc *sc, u_int val) 1535 { 1536 if (sc->sc_treble != (u_char)val) { 1537 sc->sc_treble = val; 1538 snapper_set_basstreble(sc, val, DEQ_TREBLE); 1539 } 1540 } 1541 1542 static void 1543 snapper_set_bass(struct snapper_softc *sc, u_int val) 1544 { 1545 if (sc->sc_bass != (u_char)val) { 1546 sc->sc_bass = val; 1547 snapper_set_basstreble(sc, val, DEQ_BASS); 1548 } 1549 } 1550 1551 1552 /* 1553 * In the mixer gain setting, make 128 correspond to 1554 * the 0dB value from the table. 1555 * Note that the table values are complemented. 1556 */ 1557 #define SNAPPER_MIXER_GAIN_SIZE (sizeof(snapper_mixer_gain) / \ 1558 sizeof(snapper_mixer_gain[0])) 1559 #define NORMALIZE(i) ((~(i) & 0xff) - ((~128 & 0xff) - SNAPPER_MIXER_GAIN_0DB)) 1560 #define ADJUST(v, i) do { \ 1561 (v) = NORMALIZE(i);\ 1562 if ((v) < 0) \ 1563 (v) = 0; \ 1564 else if ((v) >= SNAPPER_MIXER_GAIN_SIZE) \ 1565 (v) = SNAPPER_MIXER_GAIN_SIZE - 1; \ 1566 \ 1567 } while (0) 1568 static void 1569 snapper_write_mixers(struct snapper_softc *sc) 1570 { 1571 uint8_t regs[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; 1572 int i; 1573 1574 /* Left channel of SDIN1 */ 1575 ADJUST(i, sc->mixer[0]); 1576 regs[0] = snapper_mixer_gain[i][0]; 1577 regs[1] = snapper_mixer_gain[i][1]; 1578 regs[2] = snapper_mixer_gain[i][2]; 1579 1580 /* Left channel of SDIN2 */ 1581 ADJUST(i, sc->mixer[1]); 1582 regs[3] = snapper_mixer_gain[i][0]; 1583 regs[4] = snapper_mixer_gain[i][1]; 1584 regs[5] = snapper_mixer_gain[i][2]; 1585 1586 /* Left channel of analog input */ 1587 ADJUST(i, sc->mixer[2]); 1588 regs[6] = snapper_mixer_gain[i][0]; 1589 regs[7] = snapper_mixer_gain[i][1]; 1590 regs[8] = snapper_mixer_gain[i][2]; 1591 1592 tas3004_write(sc, DEQ_MIXER_L, regs); 1593 1594 /* Right channel of SDIN1 */ 1595 ADJUST(i, sc->mixer[3]); 1596 regs[0] = snapper_mixer_gain[i][0]; 1597 regs[1] = snapper_mixer_gain[i][1]; 1598 regs[2] = snapper_mixer_gain[i][2]; 1599 1600 /* Right channel of SDIN2 */ 1601 ADJUST(i, sc->mixer[4]); 1602 regs[3] = snapper_mixer_gain[i][0]; 1603 regs[4] = snapper_mixer_gain[i][1]; 1604 regs[5] = snapper_mixer_gain[i][2]; 1605 1606 /* Right channel of analog input */ 1607 ADJUST(i, sc->mixer[5]); 1608 regs[6] = snapper_mixer_gain[i][0]; 1609 regs[7] = snapper_mixer_gain[i][1]; 1610 regs[8] = snapper_mixer_gain[i][2]; 1611 1612 tas3004_write(sc, DEQ_MIXER_R, regs); 1613 } 1614 1615 #define CLKSRC_49MHz 0x80000000 /* Use 49152000Hz Osc. */ 1616 #define CLKSRC_45MHz 0x40000000 /* Use 45158400Hz Osc. */ 1617 #define CLKSRC_18MHz 0x00000000 /* Use 18432000Hz Osc. */ 1618 #define MCLK_DIV 0x1f000000 /* MCLK = SRC / DIV */ 1619 #define MCLK_DIV1 0x14000000 /* MCLK = SRC */ 1620 #define MCLK_DIV3 0x13000000 /* MCLK = SRC / 3 */ 1621 #define MCLK_DIV5 0x12000000 /* MCLK = SRC / 5 */ 1622 #define SCLK_DIV 0x00f00000 /* SCLK = MCLK / DIV */ 1623 #define SCLK_DIV1 0x00800000 1624 #define SCLK_DIV3 0x00900000 1625 #define SCLK_MASTER 0x00080000 /* Master mode */ 1626 #define SCLK_SLAVE 0x00000000 /* Slave mode */ 1627 #define SERIAL_FORMAT 0x00070000 1628 #define SERIAL_SONY 0x00000000 1629 #define SERIAL_64x 0x00010000 1630 #define SERIAL_32x 0x00020000 1631 #define SERIAL_DAV 0x00040000 1632 #define SERIAL_SILICON 0x00050000 1633 1634 /* 1635 * rate = fs = LRCLK 1636 * SCLK = 64*LRCLK (I2S) 1637 * MCLK = 256fs (typ. -- changeable) 1638 * 1639 * MCLK = clksrc / mdiv 1640 * SCLK = MCLK / sdiv 1641 * rate = SCLK / 64 ( = LRCLK = fs) 1642 */ 1643 1644 int 1645 snapper_set_rate(struct snapper_softc *sc) 1646 { 1647 u_int reg = 0, x; 1648 u_int rate = sc->sc_rate; 1649 uint32_t wordsize, ows; 1650 int MCLK; 1651 int clksrc, mdiv, sdiv; 1652 int mclk_fs; 1653 int timo; 1654 uint8_t mcr1; 1655 1656 switch (rate) { 1657 case 44100: 1658 clksrc = 45158400; /* 45MHz */ 1659 reg = CLKSRC_45MHz; 1660 mclk_fs = 256; 1661 break; 1662 1663 case 32000: 1664 case 48000: 1665 case 96000: 1666 clksrc = 49152000; /* 49MHz */ 1667 reg = CLKSRC_49MHz; 1668 mclk_fs = 256; 1669 break; 1670 1671 default: 1672 DPRINTF("snapper_set_rate: invalid rate %u\n", rate); 1673 return EINVAL; 1674 } 1675 1676 MCLK = rate * mclk_fs; 1677 mdiv = clksrc / MCLK; /* 4 */ 1678 sdiv = mclk_fs / 64; /* 4 */ 1679 1680 switch (mdiv) { 1681 case 1: 1682 reg |= MCLK_DIV1; 1683 break; 1684 case 3: 1685 reg |= MCLK_DIV3; 1686 break; 1687 case 5: 1688 reg |= MCLK_DIV5; 1689 break; 1690 default: 1691 reg |= ((mdiv / 2 - 1) << 24) & 0x1f000000; 1692 break; 1693 } 1694 1695 switch (sdiv) { 1696 case 1: 1697 reg |= SCLK_DIV1; 1698 break; 1699 case 3: 1700 reg |= SCLK_DIV3; 1701 break; 1702 default: 1703 reg |= ((sdiv / 2 - 1) << 20) & 0x00f00000; 1704 break; 1705 } 1706 1707 reg |= SCLK_MASTER; /* XXX master mode */ 1708 1709 reg |= SERIAL_64x; 1710 1711 /* stereo input and output */ 1712 1713 DPRINTF("precision: %d\n", sc->sc_bitspersample); 1714 switch(sc->sc_bitspersample) { 1715 case 16: 1716 wordsize = 0x02000200; 1717 mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16; 1718 break; 1719 case 24: 1720 wordsize = 0x03000300; 1721 mcr1 = DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_24; 1722 break; 1723 default: 1724 printf("%s: unsupported sample size %d\n", 1725 device_xname(sc->sc_dev), sc->sc_bitspersample); 1726 return EINVAL; 1727 } 1728 1729 if (sc->sc_mode == SNAPPER_IS_TAS3001) 1730 mcr1 |= DEQ_MCR1_ISM_I2S; 1731 1732 ows = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE); 1733 1734 DPRINTF("I2SSetDataWordSizeReg 0x%08x -> 0x%08x\n", 1735 ows, wordsize); 1736 if (ows != wordsize) { 1737 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_WORDSIZE, 1738 wordsize); 1739 if (sc->sc_mode != SNAPPER_SWVOL) 1740 tas3004_write(sc, DEQ_MCR1, &mcr1); 1741 } 1742 1743 x = bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT); 1744 if (x == reg) 1745 return 0; /* No change; do nothing. */ 1746 1747 DPRINTF("I2SSetSerialFormatReg 0x%x -> 0x%x\n", 1748 bus_space_read_4(sc->sc_tag, sc->sc_bsh, + I2S_FORMAT), reg); 1749 1750 /* Clear CLKSTOPPEND. */ 1751 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_INT, I2S_INT_CLKSTOPPEND); 1752 1753 x = obio_read_4(KEYLARGO_FCR1); /* FCR */ 1754 x &= ~I2S0CLKEN; /* XXX I2S0 */ 1755 obio_write_4(KEYLARGO_FCR1, x); 1756 1757 /* Wait until clock is stopped. */ 1758 for (timo = 1000; timo > 0; timo--) { 1759 if (bus_space_read_4(sc->sc_tag, sc->sc_bsh, I2S_INT) & 1760 I2S_INT_CLKSTOPPEND) 1761 goto done; 1762 delay(1); 1763 } 1764 DPRINTF("snapper_set_rate: timeout\n"); 1765 done: 1766 bus_space_write_4(sc->sc_tag, sc->sc_bsh, I2S_FORMAT, reg); 1767 1768 x = obio_read_4(KEYLARGO_FCR1); 1769 x |= I2S0CLKEN; 1770 obio_write_4(KEYLARGO_FCR1, x); 1771 1772 return 0; 1773 } 1774 1775 const struct tas3004_reg tas3004_initdata = { 1776 { DEQ_MCR1_SC_64 | DEQ_MCR1_SM_I2S | DEQ_MCR1_W_16 }, /* MCR1 */ 1777 { 1, 0, 0, 0, 0, 0 }, /* DRC */ 1778 { 0, 0, 0, 0, 0, 0 }, /* VOLUME */ 1779 { 0x72 }, /* TREBLE */ 1780 { 0x72 }, /* BASS */ 1781 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_L */ 1782 { 0x10, 0x00, 0x00, 0, 0, 0, 0, 0, 0 }, /* MIXER_R */ 1783 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1784 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1785 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1786 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1787 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1788 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1789 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1790 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1791 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1792 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1793 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1794 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1795 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1796 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1797 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1798 { 0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* BIQUAD */ 1799 { 0, 0, 0 }, /* LLB_GAIN */ 1800 { 0, 0, 0 }, /* RLB_GAIN */ 1801 { DEQ_ACR_ADM | DEQ_ACR_LRB | DEQ_ACR_INP_B }, /* ACR - right channel of input B is the microphone */ 1802 { 2 } /* MCR2 - AllPass mode since we don't use the equalizer anyway */ 1803 }; 1804 1805 const char tas3004_regsize[] = { 1806 0, /* 0x00 */ 1807 sizeof tas3004_initdata.MCR1, /* 0x01 */ 1808 sizeof tas3004_initdata.DRC, /* 0x02 */ 1809 0, /* 0x03 */ 1810 sizeof tas3004_initdata.VOLUME, /* 0x04 */ 1811 sizeof tas3004_initdata.TREBLE, /* 0x05 */ 1812 sizeof tas3004_initdata.BASS, /* 0x06 */ 1813 sizeof tas3004_initdata.MIXER_L, /* 0x07 */ 1814 sizeof tas3004_initdata.MIXER_R, /* 0x08 */ 1815 0, /* 0x09 */ 1816 sizeof tas3004_initdata.LB0, /* 0x0a */ 1817 sizeof tas3004_initdata.LB1, /* 0x0b */ 1818 sizeof tas3004_initdata.LB2, /* 0x0c */ 1819 sizeof tas3004_initdata.LB3, /* 0x0d */ 1820 sizeof tas3004_initdata.LB4, /* 0x0e */ 1821 sizeof tas3004_initdata.LB5, /* 0x0f */ 1822 sizeof tas3004_initdata.LB6, /* 0x10 */ 1823 0, /* 0x11 */ 1824 0, /* 0x12 */ 1825 sizeof tas3004_initdata.RB0, /* 0x13 */ 1826 sizeof tas3004_initdata.RB1, /* 0x14 */ 1827 sizeof tas3004_initdata.RB2, /* 0x15 */ 1828 sizeof tas3004_initdata.RB3, /* 0x16 */ 1829 sizeof tas3004_initdata.RB4, /* 0x17 */ 1830 sizeof tas3004_initdata.RB5, /* 0x18 */ 1831 sizeof tas3004_initdata.RB6, /* 0x19 */ 1832 0,0,0,0, 0,0, 1833 0, /* 0x20 */ 1834 sizeof tas3004_initdata.LLB, /* 0x21 */ 1835 sizeof tas3004_initdata.RLB, /* 0x22 */ 1836 sizeof tas3004_initdata.LLB_GAIN, /* 0x23 */ 1837 sizeof tas3004_initdata.RLB_GAIN, /* 0x24 */ 1838 0,0,0,0, 0,0,0,0, 0,0,0, 1839 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1840 sizeof tas3004_initdata.ACR, /* 0x40 */ 1841 0, /* 0x41 */ 1842 0, /* 0x42 */ 1843 sizeof tas3004_initdata.MCR2 /* 0x43 */ 1844 }; 1845 1846 static int 1847 tas3004_write(struct snapper_softc *sc, u_int reg, const void *data) 1848 { 1849 int size; 1850 static char regblock[sizeof(struct tas3004_reg)+1]; 1851 1852 if (sc->sc_i2c == NULL) 1853 return 0; 1854 1855 KASSERT(reg < sizeof tas3004_regsize); 1856 size = tas3004_regsize[reg]; 1857 KASSERT(size > 0); 1858 1859 DPRINTF("reg: %x, %d %d\n", reg, size, ((const char*)data)[0]); 1860 1861 regblock[0] = reg; 1862 memcpy(®block[1], data, size); 1863 if (sc->sc_mode == SNAPPER_IS_TAS3001) { 1864 if (reg == DEQ_MIXER_L || reg == DEQ_MIXER_R) 1865 size = 3; 1866 else if (reg == DEQ_DRC || reg == DEQ_ACR || 1867 reg == DEQ_MCR2) { 1868 /* these registers are not available on TAS3001 */ 1869 return 0; 1870 } 1871 } 1872 iic_acquire_bus(sc->sc_i2c, 0); 1873 iic_exec(sc->sc_i2c, I2C_OP_WRITE, sc->sc_deqaddr, regblock, size + 1, 1874 NULL, 0, 0); 1875 iic_release_bus(sc->sc_i2c, 0); 1876 1877 return 0; 1878 } 1879 1880 static int 1881 gpio_read(char *addr) 1882 { 1883 1884 if (*addr & GPIO_DATA) 1885 return 1; 1886 return 0; 1887 } 1888 1889 static void 1890 gpio_write(char *addr, int val) 1891 { 1892 u_int data; 1893 1894 data = GPIO_DDR_OUTPUT; 1895 if (val) 1896 data |= GPIO_DATA; 1897 *addr = data; 1898 __asm volatile ("eieio"); 1899 } 1900 1901 #define headphone_active 0 /* XXX OF */ 1902 #define amp_active 0 /* XXX OF */ 1903 1904 static void 1905 snapper_mute_speaker(struct snapper_softc *sc, int mute) 1906 { 1907 u_int x; 1908 1909 if (amp_mute) { 1910 DPRINTF("ampmute %d --> ", gpio_read(amp_mute)); 1911 1912 if (mute) 1913 x = amp_active; /* mute */ 1914 else 1915 x = !amp_active; /* unmute */ 1916 if (x != gpio_read(amp_mute)) 1917 gpio_write(amp_mute, x); 1918 1919 DPRINTF("%d\n", gpio_read(amp_mute)); 1920 } 1921 } 1922 1923 static void 1924 snapper_mute_headphone(struct snapper_softc *sc, int mute) 1925 { 1926 u_int x; 1927 1928 if (headphone_mute != NULL) { 1929 DPRINTF("headphonemute %d --> ", gpio_read(headphone_mute)); 1930 1931 if (mute) 1932 x = headphone_active; /* mute */ 1933 else 1934 x = !headphone_active; /* unmute */ 1935 if (x != gpio_read(headphone_mute)) 1936 gpio_write(headphone_mute, x); 1937 1938 DPRINTF("%d\n", gpio_read(headphone_mute)); 1939 } 1940 } 1941 1942 static int 1943 snapper_cint(void *v) 1944 { 1945 struct snapper_softc *sc; 1946 u_int sense; 1947 1948 if (headphone_detect != NULL) { 1949 sc = v; 1950 sense = *headphone_detect; 1951 DPRINTF("headphone detect = 0x%x\n", sense); 1952 1953 if (((sense & 0x02) >> 1) == headphone_detect_active) { 1954 DPRINTF("headphone is inserted\n"); 1955 snapper_mute_speaker(sc, 1); 1956 snapper_mute_headphone(sc, 0); 1957 sc->sc_output_mask = 1 << 1; 1958 } else { 1959 DPRINTF("headphone is NOT inserted\n"); 1960 snapper_mute_speaker(sc, 0); 1961 snapper_mute_headphone(sc, 1); 1962 sc->sc_output_mask = 1 << 0; 1963 } 1964 } 1965 1966 return 1; 1967 } 1968 1969 #define reset_active 0 /* XXX OF */ 1970 1971 #define DEQ_WRITE(sc, reg, addr) \ 1972 if (tas3004_write(sc, reg, addr)) goto err 1973 1974 static int 1975 tas3004_init(struct snapper_softc *sc) 1976 { 1977 1978 /* No reset port. Nothing to do. */ 1979 if (audio_hw_reset == NULL) 1980 goto noreset; 1981 1982 /* Reset TAS3004. */ 1983 gpio_write(audio_hw_reset, !reset_active); /* Negate RESET */ 1984 delay(100000); /* XXX Really needed? */ 1985 1986 gpio_write(audio_hw_reset, reset_active); /* Assert RESET */ 1987 delay(1); 1988 1989 gpio_write(audio_hw_reset, !reset_active); /* Negate RESET */ 1990 delay(10000); 1991 1992 noreset: 1993 DEQ_WRITE(sc, DEQ_LB0, tas3004_initdata.LB0); 1994 DEQ_WRITE(sc, DEQ_LB1, tas3004_initdata.LB1); 1995 DEQ_WRITE(sc, DEQ_LB2, tas3004_initdata.LB2); 1996 DEQ_WRITE(sc, DEQ_LB3, tas3004_initdata.LB3); 1997 DEQ_WRITE(sc, DEQ_LB4, tas3004_initdata.LB4); 1998 DEQ_WRITE(sc, DEQ_LB5, tas3004_initdata.LB5); 1999 DEQ_WRITE(sc, DEQ_LB6, tas3004_initdata.LB6); 2000 DEQ_WRITE(sc, DEQ_RB0, tas3004_initdata.RB0); 2001 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1); 2002 DEQ_WRITE(sc, DEQ_RB1, tas3004_initdata.RB1); 2003 DEQ_WRITE(sc, DEQ_RB2, tas3004_initdata.RB2); 2004 DEQ_WRITE(sc, DEQ_RB3, tas3004_initdata.RB3); 2005 DEQ_WRITE(sc, DEQ_RB4, tas3004_initdata.RB4); 2006 DEQ_WRITE(sc, DEQ_RB5, tas3004_initdata.RB5); 2007 DEQ_WRITE(sc, DEQ_MCR1, tas3004_initdata.MCR1); 2008 DEQ_WRITE(sc, DEQ_MCR2, tas3004_initdata.MCR2); 2009 DEQ_WRITE(sc, DEQ_DRC, tas3004_initdata.DRC); 2010 DEQ_WRITE(sc, DEQ_VOLUME, tas3004_initdata.VOLUME); 2011 DEQ_WRITE(sc, DEQ_TREBLE, tas3004_initdata.TREBLE); 2012 DEQ_WRITE(sc, DEQ_BASS, tas3004_initdata.BASS); 2013 DEQ_WRITE(sc, DEQ_MIXER_L, tas3004_initdata.MIXER_L); 2014 DEQ_WRITE(sc, DEQ_MIXER_R, tas3004_initdata.MIXER_R); 2015 DEQ_WRITE(sc, DEQ_LLB, tas3004_initdata.LLB); 2016 DEQ_WRITE(sc, DEQ_RLB, tas3004_initdata.RLB); 2017 DEQ_WRITE(sc, DEQ_LLB_GAIN, tas3004_initdata.LLB_GAIN); 2018 DEQ_WRITE(sc, DEQ_RLB_GAIN, tas3004_initdata.RLB_GAIN); 2019 DEQ_WRITE(sc, DEQ_ACR, tas3004_initdata.ACR); 2020 2021 return 0; 2022 err: 2023 printf("tas3004_init: error\n"); 2024 return -1; 2025 } 2026 2027 static void 2028 snapper_init(struct snapper_softc *sc, int node) 2029 { 2030 int gpio; 2031 int headphone_detect_intr, headphone_detect_intrtype; 2032 uint32_t gpio_base, reg[1]; 2033 #ifdef SNAPPER_DEBUG 2034 char fcr[32]; 2035 2036 snprintb(fcr, sizeof(fcr), FCR3C_BITMASK, obio_read_4(KEYLARGO_FCR1)); 2037 printf("FCR(0x3c) 0x%s\n", fcr); 2038 #endif 2039 headphone_detect_intr = -1; 2040 2041 gpio = of_getnode_byname(OF_parent(node), "gpio"); 2042 if (OF_getprop(gpio, "reg", reg, sizeof(reg)) == sizeof(reg)) 2043 gpio_base = reg[0]; 2044 else 2045 gpio_base = 0; 2046 DPRINTF(" /gpio 0x%x@0x%x\n", (unsigned)gpio, gpio_base); 2047 2048 gpio = OF_child(gpio); 2049 while (gpio) { 2050 char name[64], audio_gpio[64]; 2051 int intr[2]; 2052 char *addr; 2053 2054 memset(name, 0, sizeof name); 2055 memset(audio_gpio, 0, sizeof audio_gpio); 2056 addr = 0; 2057 OF_getprop(gpio, "name", name, sizeof name); 2058 OF_getprop(gpio, "audio-gpio", audio_gpio, sizeof audio_gpio); 2059 if (OF_getprop(gpio, "AAPL,address", &addr, sizeof addr) == -1) 2060 if (OF_getprop(gpio, "reg", reg, sizeof reg) 2061 == sizeof reg) 2062 addr = (char *)sc->sc_baseaddr + 2063 gpio_base + reg[0]; 2064 DPRINTF(" 0x%x %s %s\n", gpio, name, audio_gpio); 2065 2066 /* gpio5 */ 2067 if (strcmp(audio_gpio, "headphone-mute") == 0 || 2068 strcmp(name, "headphone-mute") == 0) 2069 headphone_mute = addr; 2070 /* gpio6 */ 2071 if (strcmp(audio_gpio, "amp-mute") == 0 || 2072 strcmp(name, "amp-mute") == 0) 2073 amp_mute = addr; 2074 /* extint-gpio15 */ 2075 if (strcmp(audio_gpio, "headphone-detect") == 0 || 2076 strcmp(name, "headphone-detect") == 0) { 2077 headphone_detect = addr; 2078 OF_getprop(gpio, "audio-gpio-active-state", 2079 &headphone_detect_active, 4); 2080 if (OF_getprop(gpio, "interrupts", intr, 8) == 8) { 2081 headphone_detect_intr = intr[0]; 2082 headphone_detect_intrtype = intr[1]; 2083 } 2084 } 2085 /* gpio11 (keywest-11) */ 2086 if (strcmp(audio_gpio, "audio-hw-reset") == 0 || 2087 strcmp(name, "hw-reset") == 0) 2088 audio_hw_reset = addr; 2089 2090 gpio = OF_peer(gpio); 2091 } 2092 2093 DPRINTF(" headphone-mute %p\n", headphone_mute); 2094 DPRINTF(" amp-mute %p\n", amp_mute); 2095 DPRINTF(" headphone-detect %p\n", headphone_detect); 2096 DPRINTF(" headphone-detect active %x\n", headphone_detect_active); 2097 DPRINTF(" headphone-detect intr %x\n", headphone_detect_intr); 2098 DPRINTF(" audio-hw-reset %p\n", audio_hw_reset); 2099 2100 if (headphone_detect_intr != -1) 2101 intr_establish(headphone_detect_intr, IST_EDGE, IPL_AUDIO, 2102 snapper_cint, sc); 2103 2104 sc->sc_rate = 44100; /* default rate */ 2105 sc->sc_bitspersample = 16; 2106 2107 /* Enable headphone interrupt? */ 2108 if (headphone_detect != NULL) { 2109 *headphone_detect |= 0x80; 2110 __asm volatile ("eieio"); 2111 } 2112 2113 /* i2c_set_port(port); */ 2114 2115 if (tas3004_init(sc)) 2116 return; 2117 2118 /* Update headphone status. */ 2119 snapper_cint(sc); 2120 2121 snapper_set_volume(sc, 128, 128); 2122 snapper_set_bass(sc, 128); 2123 snapper_set_treble(sc, 128); 2124 2125 /* Record source defaults to microphone. This reflects the 2126 * default value for the ACR (see tas3004_initdata). 2127 */ 2128 sc->sc_record_source = 1 << 0; 2129 2130 /* We mute the analog input for now */ 2131 sc->mixer[0] = 128; 2132 sc->mixer[1] = 128; 2133 sc->mixer[2] = 0; 2134 sc->mixer[3] = 128; 2135 sc->mixer[4] = 128; 2136 sc->mixer[5] = 0; 2137 snapper_write_mixers(sc); 2138 } 2139