1 /* $NetBSD: wm8731_zaudio.c,v 1.1 2014/09/23 14:49:46 nonaka Exp $ */ 2 3 /*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by TOYOKURA Atsushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * TODO: 34 * - powerhooks (currently only works until first suspend) 35 */ 36 37 #include "opt_cputypes.h" 38 #include "opt_zaudio.h" 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: wm8731_zaudio.c,v 1.1 2014/09/23 14:49:46 nonaka Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/callout.h> 46 #include <sys/device.h> 47 #include <sys/kmem.h> 48 #include <sys/kernel.h> 49 #include <sys/audioio.h> 50 #include <sys/mutex.h> 51 #include <sys/intr.h> 52 #include <sys/bus.h> 53 54 #include <dev/audio_if.h> 55 #include <dev/mulaw.h> 56 #include <dev/auconv.h> 57 58 #include <dev/i2c/i2cvar.h> 59 60 #include <arm/xscale/pxa2x0reg.h> 61 #include <arm/xscale/pxa2x0var.h> 62 #include <arm/xscale/pxa2x0_i2c.h> 63 #include <arm/xscale/pxa2x0_i2s.h> 64 #include <arm/xscale/pxa2x0_dmac.h> 65 #include <arm/xscale/pxa2x0_gpio.h> 66 67 #include <zaurus/zaurus/zaurus_var.h> 68 #include <zaurus/dev/zaudiovar.h> 69 #include <zaurus/dev/wm8731reg.h> 70 #include <zaurus/dev/wm8731var.h> 71 #include <zaurus/dev/scoopvar.h> 72 73 #define WM8731_ADDRESS 0x1B 74 75 /* GPIO pins */ 76 #define GPIO_HP_IN_C860 4 77 78 #define WM8731_OP_SPKR 0 79 #define WM8731_OP_MIC 1 80 #define WM8731_OP_NUM 2 81 82 static int wm8731_finalize(device_t); 83 static bool wm8731_suspend(device_t, const pmf_qual_t *); 84 static bool wm8731_resume(device_t, const pmf_qual_t *); 85 static void wm8731_volume_up(device_t); 86 static void wm8731_volume_down(device_t); 87 static void wm8731_volume_toggle(device_t); 88 89 static struct audio_device wm8731_device = { 90 "WM8731", 91 "1.0", 92 "wm" 93 }; 94 95 static const struct audio_format wm8731_formats[] = { 96 { 97 .driver_data = NULL, 98 .mode = AUMODE_PLAY | AUMODE_RECORD, 99 .encoding = AUDIO_ENCODING_SLINEAR_LE, 100 .validbits = 16, 101 .precision = 16, 102 .channels = 2, 103 .channel_mask = AUFMT_STEREO, 104 .frequency_type = 0, 105 .frequency = { 4000, 48000 } 106 } 107 }; 108 static const int wm8731_nformats = (int)__arraycount(wm8731_formats); 109 110 static void wm8731_init(struct zaudio_softc *); 111 static int wm8731_jack_intr(void *); 112 static void wm8731_jack(void *); 113 static void wm8731_standby(struct zaudio_softc *); 114 static void wm8731_update_volume(struct zaudio_softc *, int); 115 static void wm8731_update_mutes(struct zaudio_softc *, int); 116 static void wm8731_play_setup(struct zaudio_softc *); 117 /*static*/ void wm8731_record_setup(struct zaudio_softc *); 118 static int wm8731_query_encoding(void *, struct audio_encoding *); 119 static int wm8731_set_params(void *, int, int, audio_params_t *, 120 audio_params_t *, stream_filter_list_t *, stream_filter_list_t *); 121 static int wm8731_start_output(void *, void *, int, void (*)(void *), void *); 122 static int wm8731_start_input(void *, void *, int, void (*)(void *), void *); 123 static int wm8731_halt_output(void *); 124 static int wm8731_halt_input(void *); 125 static int wm8731_getdev(void *, struct audio_device *); 126 static int wm8731_set_port(void *, struct mixer_ctrl *); 127 static int wm8731_get_port(void *, struct mixer_ctrl *); 128 static int wm8731_query_devinfo(void *, struct mixer_devinfo *); 129 130 static struct audio_hw_if wm8731_hw_if = { 131 .open = zaudio_open, 132 .close = zaudio_close, 133 .drain = NULL, 134 .query_encoding = wm8731_query_encoding, 135 .set_params = wm8731_set_params, 136 .round_blocksize = zaudio_round_blocksize, 137 .commit_settings = NULL, 138 .init_output = NULL, 139 .init_input = NULL, 140 .start_output = wm8731_start_output, 141 .start_input = wm8731_start_input, 142 .halt_output = wm8731_halt_output, 143 .halt_input = wm8731_halt_input, 144 .speaker_ctl = NULL, 145 .getdev = wm8731_getdev, 146 .setfd = NULL, 147 .set_port = wm8731_set_port, 148 .get_port = wm8731_get_port, 149 .query_devinfo = wm8731_query_devinfo, 150 .allocm = zaudio_allocm, 151 .freem = zaudio_freem, 152 .round_buffersize = zaudio_round_buffersize, 153 .mappage = zaudio_mappage, 154 .get_props = zaudio_get_props, 155 .trigger_output = NULL, 156 .trigger_input = NULL, 157 .dev_ioctl = NULL, 158 .get_locks = zaudio_get_locks, 159 }; 160 161 static const uint16_t playback_regs[][2] = { 162 /* Power Down Control */ 163 { WM8731_PD_REG, WM8731_CLKOUTPD | WM8731_OSCPD | WM8731_OUTPD 164 | WM8731_ADCPD | WM8731_MICPD | WM8731_LINEINPD }, 165 166 /* Digital Audio Path Control */ 167 { WM8731_DAP_REG, 0 }, 168 169 /* Analogue Audio Path Control */ 170 { WM8731_AAP_REG, WM8731_DACSEL | WM8731_MUTEMIC }, 171 172 /* Activating DSP and DAI */ 173 { WM8731_AC_REG, WM8731_ACTIVE }, 174 175 /* End of list */ 176 { 0xffff, 0xffff } 177 }; 178 179 static const uint16_t record_regs[][2] = { 180 /* Power Down Control */ 181 { WM8731_PD_REG, WM8731_CLKOUTPD | WM8731_OSCPD | WM8731_DACPD 182 | WM8731_LINEINPD }, 183 184 /* Digital Audio Path Control */ 185 { WM8731_DAP_REG, 0 }, 186 187 /* Analogue Audio Path Control */ 188 { WM8731_AAP_REG, WM8731_INSEL | WM8731_MICBOOST }, 189 190 /* Activating DSP and DAI */ 191 { WM8731_AC_REG, WM8731_ACTIVE }, 192 193 /* End of list */ 194 { 0xffff, 0xffff } 195 }; 196 197 static __inline int 198 wm8731_write(struct zaudio_softc *sc, int reg, int val) 199 { 200 uint16_t tmp; 201 uint8_t cmd; 202 uint8_t data; 203 204 tmp = (reg << 9) | (val & 0x1ff); 205 cmd = tmp >> 8; 206 data = tmp; 207 return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, WM8731_ADDRESS, 208 &cmd, 1, &data, 1, 0); 209 } 210 211 int 212 wm8731_match(device_t parent, cfdata_t cf, struct i2c_attach_args *ia) 213 { 214 215 if (ZAURUS_ISC1000 || ZAURUS_ISC3000) 216 return 0; 217 218 if (ia->ia_name) { 219 /* direct config - check name */ 220 if (strcmp(ia->ia_name, "zaudio") == 0) 221 return 1; 222 } else { 223 /* indirect config - check typical address */ 224 if (ia->ia_addr == WM8731_ADDRESS) 225 return 1; 226 } 227 return 0; 228 } 229 230 void 231 wm8731_attach(device_t parent, device_t self, struct i2c_attach_args *ia) 232 { 233 struct zaudio_softc *sc = device_private(self); 234 int error; 235 236 aprint_normal(": I2S, WM8731 Audio\n"); 237 aprint_naive("\n"); 238 239 /* Check for an I2C response from the wm8731 */ 240 iic_acquire_bus(sc->sc_i2c, 0); 241 error = wm8731_write(sc, WM8731_RESET_REG, 0); 242 iic_release_bus(sc->sc_i2c, 0); 243 if (error) { 244 aprint_error_dev(self, "codec failed to respond\n"); 245 goto fail_i2c; 246 } 247 delay(100); 248 249 /* Allocate memory for volume & mute operations */ 250 sc->sc_volume = kmem_zalloc(sizeof(*sc->sc_volume) * WM8731_OP_NUM, 251 KM_SLEEP); 252 sc->sc_unmute = kmem_zalloc(sizeof(*sc->sc_unmute) * WM8731_OP_NUM, 253 KM_SLEEP); 254 sc->sc_unmute_toggle = kmem_zalloc( 255 sizeof(*sc->sc_unmute_toggle) * WM8731_OP_NUM, KM_SLEEP); 256 257 /* Speaker On by default. */ 258 sc->sc_volume[WM8731_OP_SPKR].left = 180; 259 sc->sc_volume[WM8731_OP_SPKR].right = 180; 260 sc->sc_jack = FALSE; 261 UNMUTE(sc, WM8731_OP_SPKR, 1); 262 sc->sc_volume[WM8731_OP_MIC].left = 180; 263 UNMUTE(sc, WM8731_OP_MIC, 0); 264 265 /* Configure headphone jack state change handling. */ 266 callout_setfunc(&sc->sc_to, wm8731_jack, sc); 267 pxa2x0_gpio_set_function(GPIO_HP_IN_C860, GPIO_IN); 268 (void) pxa2x0_gpio_intr_establish(GPIO_HP_IN_C860, IST_EDGE_BOTH, 269 IPL_BIO, wm8731_jack_intr, sc); 270 271 /* wm8731_init() implicitly depends on ioexp or scoop */ 272 config_finalize_register(self, wm8731_finalize); 273 274 audio_attach_mi(&wm8731_hw_if, sc, self); 275 276 if (!pmf_device_register(self, wm8731_suspend, wm8731_resume)) 277 aprint_error_dev(self, "couldn't establish power handler\n"); 278 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP, 279 wm8731_volume_up, true)) 280 aprint_error_dev(self, "couldn't register event handler\n"); 281 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN, 282 wm8731_volume_down, true)) 283 aprint_error_dev(self, "couldn't register event handler\n"); 284 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE, 285 wm8731_volume_toggle, true)) 286 aprint_error_dev(self, "couldn't register event handler\n"); 287 288 return; 289 290 fail_i2c: 291 pxa2x0_i2s_detach_sub(&sc->sc_i2s); 292 } 293 294 static int 295 wm8731_finalize(device_t dv) 296 { 297 struct zaudio_softc *sc = device_private(dv); 298 299 wm8731_init(sc); 300 return 0; 301 } 302 303 static bool 304 wm8731_suspend(device_t dv, const pmf_qual_t *qual) 305 { 306 struct zaudio_softc *sc = device_private(dv); 307 308 callout_stop(&sc->sc_to); 309 wm8731_standby(sc); 310 311 return true; 312 } 313 314 static bool 315 wm8731_resume(device_t dv, const pmf_qual_t *qual) 316 { 317 struct zaudio_softc *sc = device_private(dv); 318 319 pxa2x0_i2s_init(&sc->sc_i2s); 320 wm8731_init(sc); 321 322 return true; 323 } 324 325 static __inline uint8_t 326 vol_sadd(int vol, int stride) 327 { 328 329 vol += stride; 330 if (vol > 255) 331 return 255; 332 return (uint8_t)vol; 333 } 334 335 #ifndef ZAUDIO_VOLUME_STRIDE 336 #define ZAUDIO_VOLUME_STRIDE 8 337 #endif 338 339 static void 340 wm8731_volume_up(device_t dv) 341 { 342 struct zaudio_softc *sc = device_private(dv); 343 int s; 344 345 s = splbio(); 346 iic_acquire_bus(sc->sc_i2c, 0); 347 348 sc->sc_volume[WM8731_OP_SPKR].left = 349 vol_sadd(sc->sc_volume[WM8731_OP_SPKR].left, ZAUDIO_VOLUME_STRIDE); 350 sc->sc_volume[WM8731_OP_SPKR].right = 351 vol_sadd(sc->sc_volume[WM8731_OP_SPKR].right, ZAUDIO_VOLUME_STRIDE); 352 353 wm8731_update_volume(sc, WM8731_OP_SPKR); 354 355 iic_release_bus(sc->sc_i2c, 0); 356 splx(s); 357 } 358 359 static __inline uint8_t 360 vol_ssub(int vol, int stride) 361 { 362 363 vol -= stride; 364 if (vol < 0) 365 return 0; 366 return (uint8_t)vol; 367 } 368 369 static void 370 wm8731_volume_down(device_t dv) 371 { 372 struct zaudio_softc *sc = device_private(dv); 373 int s; 374 375 s = splbio(); 376 iic_acquire_bus(sc->sc_i2c, 0); 377 378 sc->sc_volume[WM8731_OP_SPKR].left = 379 vol_ssub(sc->sc_volume[WM8731_OP_SPKR].left, ZAUDIO_VOLUME_STRIDE); 380 sc->sc_volume[WM8731_OP_SPKR].right = 381 vol_ssub(sc->sc_volume[WM8731_OP_SPKR].right, ZAUDIO_VOLUME_STRIDE); 382 383 wm8731_update_volume(sc, WM8731_OP_SPKR); 384 385 iic_release_bus(sc->sc_i2c, 0); 386 splx(s); 387 } 388 389 static void 390 wm8731_volume_toggle(device_t dv) 391 { 392 struct zaudio_softc *sc = device_private(dv); 393 int s; 394 395 s = splbio(); 396 iic_acquire_bus(sc->sc_i2c, 0); 397 398 if (!sc->sc_unmute[WM8731_OP_SPKR]) { 399 sc->sc_unmute[WM8731_OP_SPKR] = 400 sc->sc_unmute_toggle[WM8731_OP_SPKR]; 401 } else { 402 sc->sc_unmute[WM8731_OP_SPKR] = 0; 403 } 404 wm8731_update_mutes(sc, 1); 405 406 iic_release_bus(sc->sc_i2c, 0); 407 splx(s); 408 } 409 410 static void 411 wm8731_init(struct zaudio_softc *sc) 412 { 413 414 iic_acquire_bus(sc->sc_i2c, 0); 415 416 /* Reset the codec */ 417 wm8731_write(sc, WM8731_RESET_REG, 0); 418 delay(100); 419 420 /* Switch to standby power only */ 421 wm8731_write(sc, WM8731_PD_REG, WM8731_CLKOUTPD | WM8731_OSCPD | 422 WM8731_OUTPD | WM8731_DACPD | WM8731_ADCPD | WM8731_MICPD | 423 WM8731_LINEINPD); 424 425 /* Configure digital interface for I2S */ 426 wm8731_write(sc, WM8731_DAI_REG, WM8731_SET_IWL(2) | WM8731_SET_FORMAT(2)); 427 428 /* Initialise volume levels */ 429 wm8731_update_volume(sc, WM8731_OP_SPKR); 430 wm8731_update_volume(sc, WM8731_OP_MIC); 431 432 scoop_set_headphone(0); 433 scoop_set_speaker(0); 434 scoop_set_mic_bias(0); 435 436 iic_release_bus(sc->sc_i2c, 0); 437 438 /* Assume that the jack state has changed. */ 439 wm8731_jack(sc); 440 } 441 442 static int 443 wm8731_jack_intr(void *v) 444 { 445 struct zaudio_softc *sc = v; 446 447 if (!callout_active(&sc->sc_to)) 448 wm8731_jack(sc); 449 450 return 1; 451 } 452 453 static void 454 wm8731_jack(void *v) 455 { 456 struct zaudio_softc *sc = v; 457 458 switch (sc->sc_state) { 459 case ZAUDIO_JACK_STATE_OUT: 460 if (pxa2x0_gpio_get_bit(GPIO_HP_IN_C860)) { 461 sc->sc_state = ZAUDIO_JACK_STATE_INS; 462 sc->sc_icount = 0; 463 } 464 break; 465 466 case ZAUDIO_JACK_STATE_INS: 467 if (sc->sc_icount++ > 2) { 468 if (pxa2x0_gpio_get_bit(GPIO_HP_IN_C860)) { 469 sc->sc_state = ZAUDIO_JACK_STATE_IN; 470 sc->sc_jack = TRUE; 471 UNMUTE(sc, WM8731_OP_MIC, 1); 472 goto update_mutes; 473 } else 474 sc->sc_state = ZAUDIO_JACK_STATE_OUT; 475 } 476 break; 477 478 case ZAUDIO_JACK_STATE_IN: 479 if (!pxa2x0_gpio_get_bit(GPIO_HP_IN_C860)) { 480 sc->sc_state = ZAUDIO_JACK_STATE_REM; 481 sc->sc_icount = 0; 482 } 483 break; 484 485 case ZAUDIO_JACK_STATE_REM: 486 if (sc->sc_icount++ > 2) { 487 if (!pxa2x0_gpio_get_bit(GPIO_HP_IN_C860)) { 488 sc->sc_state = ZAUDIO_JACK_STATE_OUT; 489 sc->sc_jack = FALSE; 490 UNMUTE(sc, WM8731_OP_MIC, 0); 491 goto update_mutes; 492 } else 493 sc->sc_state = ZAUDIO_JACK_STATE_IN; 494 } 495 break; 496 } 497 498 callout_schedule(&sc->sc_to, hz/4); 499 500 return; 501 502 update_mutes: 503 callout_stop(&sc->sc_to); 504 505 if (sc->sc_playing || sc->sc_recording) { 506 iic_acquire_bus(sc->sc_i2c, 0); 507 if (sc->sc_playing) 508 wm8731_update_mutes(sc, 1); 509 if (sc->sc_recording) 510 wm8731_update_mutes(sc, 2); 511 iic_release_bus(sc->sc_i2c, 0); 512 } 513 } 514 515 static void 516 wm8731_standby(struct zaudio_softc *sc) 517 { 518 519 iic_acquire_bus(sc->sc_i2c, 0); 520 521 /* Switch to standby power only */ 522 wm8731_write(sc, WM8731_PD_REG, WM8731_CLKOUTPD | WM8731_OSCPD | 523 WM8731_OUTPD | WM8731_DACPD | WM8731_ADCPD | WM8731_MICPD | 524 WM8731_LINEINPD); 525 526 scoop_set_headphone(0); 527 scoop_set_speaker(0); 528 scoop_set_mic_bias(0); 529 530 /* Activating DSP and DAI */ 531 wm8731_write(sc, WM8731_AC_REG, 0); 532 533 iic_release_bus(sc->sc_i2c, 0); 534 } 535 536 static void 537 wm8731_update_volume(struct zaudio_softc *sc, int output) 538 { 539 struct zaudio_volume *volume; 540 541 switch (output) { 542 case WM8731_OP_SPKR: 543 volume = &sc->sc_volume[WM8731_OP_SPKR]; 544 wm8731_write(sc, WM8731_LHP_REG, 545 WM8731_SET_LHPVOL(volume->left >> 1)); 546 wm8731_write(sc, WM8731_RHP_REG, 547 WM8731_SET_RHPVOL(volume->right >> 1)); 548 break; 549 550 case WM8731_OP_MIC: 551 volume = &sc->sc_volume[WM8731_OP_MIC]; 552 wm8731_write(sc, WM8731_LIN_REG, WM8731_LRINBOTH | 553 WM8731_SET_LINVOL(volume->left >> 3)); 554 break; 555 } 556 } 557 558 static void 559 wm8731_update_mutes(struct zaudio_softc *sc, int mask) 560 { 561 uint16_t val = WM8731_CLKOUTPD | WM8731_OSCPD | WM8731_LINEINPD; 562 563 /* playback */ 564 if (mask & 1) { 565 val |= WM8731_ADCPD | WM8731_MICPD; 566 if (!sc->sc_unmute[WM8731_OP_SPKR]) { 567 val |= WM8731_OUTPD | WM8731_DACPD; 568 } 569 wm8731_write(sc, WM8731_PD_REG, val); 570 scoop_set_headphone(sc->sc_unmute[WM8731_OP_SPKR] & sc->sc_jack); 571 scoop_set_speaker(sc->sc_unmute[WM8731_OP_SPKR] & !sc->sc_jack); 572 } 573 574 /* record */ 575 if (mask & 2) { 576 val = WM8731_OUTPD | WM8731_DACPD; 577 if (!sc->sc_unmute[WM8731_OP_MIC]) { 578 val |= WM8731_ADCPD | WM8731_MICPD; 579 } 580 wm8731_write(sc, WM8731_PD_REG, val); 581 scoop_set_mic_bias(sc->sc_unmute[WM8731_OP_MIC]); 582 } 583 } 584 585 static void 586 wm8731_play_setup(struct zaudio_softc *sc) 587 { 588 int i; 589 590 iic_acquire_bus(sc->sc_i2c, 0); 591 592 /* Program the codec with playback settings */ 593 for (i = 0; playback_regs[i][0] != 0xffff; i++) { 594 wm8731_write(sc, playback_regs[i][0], playback_regs[i][1]); 595 } 596 wm8731_update_mutes(sc, 1); 597 598 iic_release_bus(sc->sc_i2c, 0); 599 } 600 601 /*static*/ void 602 wm8731_record_setup(struct zaudio_softc *sc) 603 { 604 int i; 605 606 iic_acquire_bus(sc->sc_i2c, 0); 607 608 /* Program the codec with playback settings */ 609 for (i = 0; record_regs[i][0] != 0xffff; i++) { 610 wm8731_write(sc, record_regs[i][0], record_regs[i][1]); 611 } 612 wm8731_update_mutes(sc, 2); 613 614 iic_release_bus(sc->sc_i2c, 0); 615 } 616 617 static int 618 wm8731_query_encoding(void *hdl, struct audio_encoding *aep) 619 { 620 621 switch (aep->index) { 622 case 0: 623 strlcpy(aep->name, AudioEulinear, sizeof(aep->name)); 624 aep->encoding = AUDIO_ENCODING_ULINEAR; 625 aep->precision = 8; 626 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 627 break; 628 629 case 1: 630 strlcpy(aep->name, AudioEmulaw, sizeof(aep->name)); 631 aep->encoding = AUDIO_ENCODING_ULAW; 632 aep->precision = 8; 633 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 634 break; 635 636 case 2: 637 strlcpy(aep->name, AudioEalaw, sizeof(aep->name)); 638 aep->encoding = AUDIO_ENCODING_ALAW; 639 aep->precision = 8; 640 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 641 break; 642 643 case 3: 644 strlcpy(aep->name, AudioEslinear, sizeof(aep->name)); 645 aep->encoding = AUDIO_ENCODING_SLINEAR; 646 aep->precision = 8; 647 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 648 break; 649 650 case 4: 651 strlcpy(aep->name, AudioEslinear_le, sizeof(aep->name)); 652 aep->encoding = AUDIO_ENCODING_SLINEAR_LE; 653 aep->precision = 16; 654 aep->flags = 0; 655 break; 656 657 case 5: 658 strlcpy(aep->name, AudioEulinear_le, sizeof(aep->name)); 659 aep->encoding = AUDIO_ENCODING_ULINEAR_LE; 660 aep->precision = 16; 661 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 662 break; 663 664 case 6: 665 strlcpy(aep->name, AudioEslinear_be, sizeof(aep->name)); 666 aep->encoding = AUDIO_ENCODING_SLINEAR_BE; 667 aep->precision = 16; 668 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 669 break; 670 671 case 7: 672 strlcpy(aep->name, AudioEulinear_be, sizeof(aep->name)); 673 aep->encoding = AUDIO_ENCODING_ULINEAR_BE; 674 aep->precision = 16; 675 aep->flags = AUDIO_ENCODINGFLAG_EMULATED; 676 break; 677 678 default: 679 return EINVAL; 680 } 681 682 return 0; 683 } 684 685 static int 686 wm8731_set_params(void *hdl, int setmode, int usemode, audio_params_t *play, 687 audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil) 688 { 689 struct zaudio_softc *sc = hdl; 690 struct audio_params *p; 691 stream_filter_list_t *fil; 692 int mode, i; 693 694 if (play->sample_rate != rec->sample_rate && 695 usemode == (AUMODE_PLAY | AUMODE_RECORD)) { 696 if (setmode == AUMODE_PLAY) { 697 rec->sample_rate = play->sample_rate; 698 setmode |= AUMODE_RECORD; 699 } else if (setmode == AUMODE_RECORD) { 700 play->sample_rate = rec->sample_rate; 701 setmode |= AUMODE_PLAY; 702 } else 703 return EINVAL; 704 } 705 706 for (mode = AUMODE_RECORD; mode != -1; 707 mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) { 708 if ((setmode & mode) == 0) 709 continue; 710 711 p = (mode == AUMODE_PLAY) ? play : rec; 712 713 if (p->sample_rate < 4000 || p->sample_rate > 48000 || 714 (p->precision != 8 && p->precision != 16) || 715 (p->channels != 1 && p->channels != 2)) 716 return EINVAL; 717 718 fil = (mode == AUMODE_PLAY) ? pfil : rfil; 719 i = auconv_set_converter(wm8731_formats, wm8731_nformats, 720 mode, p, true, fil); 721 if (i < 0) 722 return EINVAL; 723 } 724 725 if (setmode == AUMODE_RECORD) 726 pxa2x0_i2s_setspeed(&sc->sc_i2s, &rec->sample_rate); 727 else 728 pxa2x0_i2s_setspeed(&sc->sc_i2s, &play->sample_rate); 729 730 return 0; 731 } 732 733 static int 734 wm8731_halt_output(void *hdl) 735 { 736 struct zaudio_softc *sc = hdl; 737 int rv; 738 739 rv = pxa2x0_i2s_halt_output(&sc->sc_i2s); 740 if (!sc->sc_recording) 741 wm8731_standby(sc); 742 sc->sc_playing = 0; 743 744 return rv; 745 } 746 747 static int 748 wm8731_halt_input(void *hdl) 749 { 750 struct zaudio_softc *sc = hdl; 751 int rv; 752 753 rv = pxa2x0_i2s_halt_input(&sc->sc_i2s); 754 if (!sc->sc_playing) 755 wm8731_standby(sc); 756 sc->sc_recording = 0; 757 758 return rv; 759 } 760 761 static int 762 wm8731_getdev(void *hdl, struct audio_device *ret) 763 { 764 765 *ret = wm8731_device; 766 return 0; 767 } 768 769 #define WM8731_SPKR_LVL 0 770 #define WM8731_SPKR_MUTE 1 771 #define WM8731_MIC_LVL 2 772 #define WM8731_MIC_MUTE 3 773 #define WM8731_RECORD_SOURCE 4 774 #define WM8731_OUTPUT_CLASS 5 775 #define WM8731_INPUT_CLASS 6 776 #define WM8731_RECORD_CLASS 7 777 778 static int 779 wm8731_set_port(void *hdl, struct mixer_ctrl *mc) 780 { 781 struct zaudio_softc *sc = hdl; 782 int error = EINVAL; 783 int s; 784 785 s = splbio(); 786 iic_acquire_bus(sc->sc_i2c, 0); 787 788 switch (mc->dev) { 789 case WM8731_SPKR_LVL: 790 if (mc->type != AUDIO_MIXER_VALUE) 791 break; 792 if (mc->un.value.num_channels == 1) { 793 sc->sc_volume[WM8731_OP_SPKR].left = 794 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 795 sc->sc_volume[WM8731_OP_SPKR].right = 796 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 797 } else if (mc->un.value.num_channels == 2) { 798 sc->sc_volume[WM8731_OP_SPKR].left = 799 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT]; 800 sc->sc_volume[WM8731_OP_SPKR].right = 801 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT]; 802 } 803 else 804 break; 805 wm8731_update_volume(sc, WM8731_OP_SPKR); 806 error = 0; 807 break; 808 809 case WM8731_SPKR_MUTE: 810 if (mc->type != AUDIO_MIXER_ENUM) 811 break; 812 UNMUTE(sc, WM8731_OP_SPKR, mc->un.ord ? 1 : 0); 813 wm8731_update_mutes(sc, 1); 814 error = 0; 815 break; 816 817 case WM8731_MIC_LVL: 818 if (mc->type != AUDIO_MIXER_VALUE) 819 break; 820 if (mc->un.value.num_channels == 1) 821 sc->sc_volume[WM8731_OP_MIC].left = 822 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 823 else 824 break; 825 wm8731_update_volume(sc, WM8731_OP_MIC); 826 error = 0; 827 break; 828 829 case WM8731_MIC_MUTE: 830 if (mc->type != AUDIO_MIXER_ENUM) 831 break; 832 UNMUTE(sc, WM8731_OP_MIC, mc->un.ord ? 1 : 0); 833 wm8731_update_mutes(sc, 2); 834 error = 0; 835 break; 836 837 case WM8731_RECORD_SOURCE: 838 if (mc->type != AUDIO_MIXER_ENUM) 839 break; 840 if (mc->un.ord != 0) 841 break; 842 /* MIC only */ 843 error = 0; 844 break; 845 } 846 847 iic_release_bus(sc->sc_i2c, 0); 848 splx(s); 849 850 return error; 851 } 852 853 static int 854 wm8731_get_port(void *hdl, struct mixer_ctrl *mc) 855 { 856 struct zaudio_softc *sc = hdl; 857 int error = EINVAL; 858 859 switch (mc->dev) { 860 case WM8731_SPKR_LVL: 861 if (mc->type != AUDIO_MIXER_VALUE) 862 break; 863 if (mc->un.value.num_channels == 1) 864 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 865 sc->sc_volume[WM8731_OP_SPKR].left; 866 else if (mc->un.value.num_channels == 2) { 867 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = 868 sc->sc_volume[WM8731_OP_SPKR].left; 869 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 870 sc->sc_volume[WM8731_OP_SPKR].right; 871 } 872 else 873 break; 874 error = 0; 875 break; 876 877 case WM8731_SPKR_MUTE: 878 if (mc->type != AUDIO_MIXER_ENUM) 879 break; 880 mc->un.ord = sc->sc_unmute[WM8731_OP_SPKR] ? 1 : 0; 881 error = 0; 882 break; 883 884 case WM8731_MIC_LVL: 885 if (mc->type != AUDIO_MIXER_VALUE) 886 break; 887 if (mc->un.value.num_channels == 1) 888 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = 889 sc->sc_volume[WM8731_OP_MIC].left; 890 else 891 break; 892 error = 0; 893 break; 894 895 case WM8731_MIC_MUTE: 896 if (mc->type != AUDIO_MIXER_ENUM) 897 break; 898 mc->un.ord = sc->sc_unmute[WM8731_OP_MIC] ? 1 : 0; 899 error = 0; 900 break; 901 902 case WM8731_RECORD_SOURCE: 903 if (mc->type != AUDIO_MIXER_ENUM) 904 break; 905 mc->un.ord = 0; /* MIC only */ 906 error = 0; 907 break; 908 } 909 910 return error; 911 } 912 913 /*ARGSUSED*/ 914 static int 915 wm8731_query_devinfo(void *hdl, struct mixer_devinfo *di) 916 { 917 918 switch (di->index) { 919 case WM8731_SPKR_LVL: 920 di->type = AUDIO_MIXER_VALUE; 921 di->mixer_class = WM8731_OUTPUT_CLASS; 922 di->prev = AUDIO_MIXER_LAST; 923 di->next = WM8731_SPKR_MUTE; 924 strlcpy(di->label.name, AudioNspeaker, 925 sizeof(di->label.name)); 926 di->un.v.num_channels = 1; 927 strlcpy(di->un.v.units.name, AudioNvolume, 928 sizeof(di->un.v.units.name)); 929 break; 930 931 case WM8731_SPKR_MUTE: 932 di->type = AUDIO_MIXER_ENUM; 933 di->mixer_class = WM8731_OUTPUT_CLASS; 934 di->prev = WM8731_SPKR_LVL; 935 di->next = AUDIO_MIXER_LAST; 936 mute: 937 strlcpy(di->label.name, AudioNmute, sizeof(di->label.name)); 938 di->un.e.num_mem = 2; 939 strlcpy(di->un.e.member[0].label.name, AudioNon, 940 sizeof(di->un.e.member[0].label.name)); 941 di->un.e.member[0].ord = 0; 942 strlcpy(di->un.e.member[1].label.name, AudioNoff, 943 sizeof(di->un.e.member[1].label.name)); 944 di->un.e.member[1].ord = 1; 945 break; 946 947 case WM8731_MIC_LVL: 948 di->type = AUDIO_MIXER_VALUE; 949 di->mixer_class = WM8731_INPUT_CLASS; 950 di->prev = AUDIO_MIXER_LAST; 951 di->next = WM8731_MIC_MUTE; 952 strlcpy(di->label.name, AudioNmicrophone, 953 sizeof(di->label.name)); 954 strlcpy(di->un.v.units.name, AudioNvolume, 955 sizeof(di->un.v.units.name)); 956 di->un.v.num_channels = 1; 957 break; 958 959 case WM8731_MIC_MUTE: 960 di->type = AUDIO_MIXER_ENUM; 961 di->mixer_class = WM8731_INPUT_CLASS; 962 di->prev = WM8731_MIC_LVL; 963 di->next = AUDIO_MIXER_LAST; 964 goto mute; 965 966 case WM8731_RECORD_SOURCE: 967 di->type = AUDIO_MIXER_ENUM; 968 di->mixer_class = WM8731_RECORD_CLASS; 969 di->prev = AUDIO_MIXER_LAST; 970 di->next = AUDIO_MIXER_LAST; 971 strlcpy(di->label.name, AudioNsource, sizeof(di->label.name)); 972 di->un.e.num_mem = 1; 973 strlcpy(di->un.e.member[0].label.name, AudioNmicrophone, 974 sizeof(di->un.e.member[0].label.name)); 975 di->un.e.member[0].ord = 0; 976 break; 977 978 case WM8731_OUTPUT_CLASS: 979 di->type = AUDIO_MIXER_CLASS; 980 di->mixer_class = WM8731_OUTPUT_CLASS; 981 di->prev = AUDIO_MIXER_LAST; 982 di->next = AUDIO_MIXER_LAST; 983 strlcpy(di->label.name, AudioCoutputs, sizeof(di->label.name)); 984 break; 985 986 case WM8731_INPUT_CLASS: 987 di->type = AUDIO_MIXER_CLASS; 988 di->mixer_class = WM8731_INPUT_CLASS; 989 di->prev = AUDIO_MIXER_LAST; 990 di->next = AUDIO_MIXER_LAST; 991 strlcpy(di->label.name, AudioCinputs, sizeof(di->label.name)); 992 break; 993 994 case WM8731_RECORD_CLASS: 995 di->type = AUDIO_MIXER_CLASS; 996 di->mixer_class = WM8731_RECORD_CLASS; 997 di->prev = AUDIO_MIXER_LAST; 998 di->next = AUDIO_MIXER_LAST; 999 strlcpy(di->label.name, AudioCinputs, sizeof(di->label.name)); 1000 break; 1001 1002 default: 1003 return ENXIO; 1004 } 1005 1006 return 0; 1007 } 1008 1009 static int 1010 wm8731_start_output(void *hdl, void *block, int bsize, void (*intr)(void *), 1011 void *intrarg) 1012 { 1013 struct zaudio_softc *sc = hdl; 1014 int rv; 1015 1016 /* Power up codec if we are not already playing. */ 1017 if (!sc->sc_playing) { 1018 sc->sc_playing = 1; 1019 wm8731_play_setup(sc); 1020 } 1021 1022 /* Start DMA via I2S */ 1023 rv = pxa2x0_i2s_start_output(&sc->sc_i2s, block, bsize, intr, intrarg); 1024 if (rv) { 1025 if (!sc->sc_recording) 1026 wm8731_standby(sc); 1027 sc->sc_playing = 0; 1028 } 1029 1030 return rv; 1031 } 1032 1033 static int 1034 wm8731_start_input(void *hdl, void *block, int bsize, void (*intr)(void *), 1035 void *intrarg) 1036 { 1037 struct zaudio_softc *sc = hdl; 1038 int rv; 1039 1040 /* Power up codec if we are not already recording. */ 1041 if (!sc->sc_recording) { 1042 sc->sc_recording = 1; 1043 wm8731_record_setup(sc); 1044 } 1045 1046 /* Start DMA via I2S */ 1047 rv = pxa2x0_i2s_start_input(&sc->sc_i2s, block, bsize, intr, intrarg); 1048 if (rv) { 1049 if (!sc->sc_playing) 1050 wm8731_standby(sc); 1051 sc->sc_recording = 0; 1052 } 1053 return rv; 1054 } 1055