preprocessor cleanup: __sparc
[unleashed/tickless.git] / usr / src / uts / common / io / audio / ac97 / ac97.c
blob5c5fb0a50b04c9bbcf80f683a4f903ebcdce894c
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright (C) 4Front Technologies 1996-2008.
24 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #include <sys/types.h>
29 #include <sys/list.h>
30 #include <sys/sysmacros.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/audio/audio_driver.h>
34 #include <sys/audio/ac97.h>
35 #include <sys/note.h>
36 #include "ac97_impl.h"
39 * This is the initial value for many controls. This is
40 * a 75% level.
42 #define INIT_VAL_MAIN 75
43 #define INIT_VAL_ST ((75 << 8) | 75)
44 #define INIT_VAL_MN 75
45 #define INIT_IGAIN_ST ((50 << 8) | 50)
46 #define INIT_IGAIN_MN 50
49 * In AC'97 v2.3, the registers are carved up as follows:
51 * Audio Base Registers: 0x00 - 0x26
52 * Audio Extended Registers: 0x28 - 0x3A
53 * Modem Extended Registers: 0x3C - 0x58
54 * Vendor Reserved Registers: 0x5A - 0x5F
55 * Page Registers: 0x60 - 0x6F
56 * Vendor Reserved Registers: 0x70 - 0x7A
57 * Vendor ID Registers: 0x7C - 0x7F
59 * We only need to shadow the normal audio registers by default.
60 * TBD: Handling of codec-specific registers in vendor reserved space.
61 * We cannot necessarily meaningfully shadow them.
63 #define LAST_SHADOW_REG 0x3A
64 #define NUM_SHADOW ((LAST_SHADOW_REG / sizeof (uint16_t)) + 1)
65 #define SHADOW(ac, reg) ((ac)->shadow[((reg) / sizeof (uint16_t))])
68 * Record source selection.
70 #define INPUT_MIC 0
71 #define INPUT_CD 1
72 #define INPUT_VIDEO 2
73 #define INPUT_AUXIN 3
74 #define INPUT_LINEIN 4
75 #define INPUT_STEREOMIX 5
76 #define INPUT_MONOMIX 6
77 #define INPUT_PHONE 7
79 static const char *ac_insrcs[] = {
80 AUDIO_PORT_MIC,
81 AUDIO_PORT_CD,
82 AUDIO_PORT_VIDEO,
83 AUDIO_PORT_AUX1IN,
84 AUDIO_PORT_LINEIN,
85 AUDIO_PORT_STEREOMIX,
86 AUDIO_PORT_MONOMIX,
87 AUDIO_PORT_PHONE,
88 NULL,
92 * Per audio device state structure
94 struct ac97 {
95 dev_info_t *dip; /* DDI device instance */
96 audio_dev_t *d;
97 void *private; /* drivers devc */
98 ac97_rd_t rd; /* drivers port read routine */
99 ac97_wr_t wr; /* drivers port write routine */
100 char name[128]; /* driver instance name */
101 uint8_t nchan;
103 uint16_t shadow[NUM_SHADOW];
105 uint32_t flags;
106 #define AC97_FLAG_AMPLIFIER (1 << 0) /* ext. amp on by default */
107 #define AC97_FLAG_MICBOOST (1 << 1) /* micboost on by default */
108 #define AC97_FLAG_SPEAKER (1 << 2) /* mono out on by default */
110 #define AC97_FLAG_AUX_HP (1 << 4) /* possible uses for AUX_OUT */
111 #define AC97_FLAG_AUX_4CH (1 << 5)
112 #define AC97_FLAG_AUX_LVL (1 << 6)
113 #define AC97_FLAG_SPEAKER_OK (1 << 7) /* expose mono out */
114 #define AC97_FLAG_NO_HEADPHONE (1 << 8) /* do not expose headphone */
115 #define AC97_FLAG_NO_CDROM (1 << 9) /* do not expose CDROM */
116 #define AC97_FLAG_NO_PHONE (1 << 10) /* do not expose phone in */
117 #define AC97_FLAG_NO_VIDEO (1 << 11) /* do not expose video in */
118 #define AC97_FLAG_NO_AUXIN (1 << 12) /* do not expose aux in */
119 #define AC97_FLAG_NO_AUXOUT (1 << 13) /* do not expose aux out */
120 #define AC97_FLAG_NO_LINEIN (1 << 14) /* do not expose linein */
121 #define AC97_FLAG_NO_MIC (1 << 15) /* do not expose mic */
123 uint32_t vid; /* Vendor ID for CODEC */
124 uint16_t caps;
126 void (*codec_init)(ac97_t *);
127 void (*codec_reset)(ac97_t *);
129 list_t ctrls;
131 uint64_t inputs;
135 struct modlmisc ac97_modlmisc = {
136 &mod_miscops,
137 "Audio Codec '97 Support"
140 struct modlinkage ac97_modlinkage = {
141 MODREV_1,
142 { &ac97_modlmisc, NULL }
146 _init(void)
148 return (mod_install(&ac97_modlinkage));
152 _fini(void)
154 return (mod_install(&ac97_modlinkage));
158 _info(struct modinfo *modinfop)
160 return (mod_info(&ac97_modlinkage, modinfop));
164 #if 0
166 * The following table, and the code to scale it, works in percentages.
167 * This may be convenient for humans, but it would be faster if the table
168 * entries were rescaled to 256. (Division by 100 is painful. Divison by
169 * 256 is trivial.)
171 static const char ac97_val_cvt[101] = {
172 0, 0, 3, 7, 10, 13, 16, 19,
173 21, 23, 26, 28, 30, 32, 34, 35,
174 37, 39, 40, 42, 43, 45, 46, 47,
175 49, 50, 51, 52, 53, 55, 56, 57,
176 58, 59, 60, 61, 62, 63, 64, 65,
177 65, 66, 67, 68, 69, 70, 70, 71,
178 72, 73, 73, 74, 75, 75, 76, 77,
179 77, 78, 79, 79, 80, 81, 81, 82,
180 82, 83, 84, 84, 85, 85, 86, 86,
181 87, 87, 88, 88, 89, 89, 90, 90,
182 91, 91, 92, 92, 93, 93, 94, 94,
183 95, 95, 96, 96, 96, 97, 97, 98,
184 98, 98, 99, 99, 100
186 #endif
189 * This code has three main functions. All related to converting
190 * a standard controls value to hardware specific values. All
191 * Standard passed in values are 0-100 as in percent.
193 * First it takes a value passed in as volume or gain and
194 * converts to attenuation or gain correspondingly. Since this is
195 * what the hardware needs.
197 * Second it adjusts the value passed in to compensate for the none
198 * linear nature of human hearing, sound loudness, sensitivity. It
199 * converts the linear value to a logarithmic value. This gives users
200 * the perception that the controls are linear.
202 * Third it converts the value to the number of bits that a hardware
203 * register needs to be.
205 * On input the following are supplied:
206 * left - The gain or volume in percent for left channel.
207 * right - The gain or volume in percent for right channel.
208 * bits - The number of bits the hardware needs. If this value
209 * is negetive then right and left are gain else they
210 * are volume.
212 * On return the following is returned:
214 * bit: 15 8 7 0
215 * ----------------------------------
216 * | left channel | right channel |
217 * ----------------------------------
218 * ( each channel is "bits" wide )
220 uint16_t
221 ac_val_scale(int left, int right, int bits)
223 ASSERT(left <= 100);
224 ASSERT(right <= 100);
226 if (bits < 0) { /* This is gain not ATTN */
227 left = 100 - left;
228 right = 100 - right;
229 bits = -bits;
232 #if 0
234 * 4Front's code used a table to smooth the transitions
235 * somewhat. Without this change, the volume levels adjusted
236 * near the top of the table seem to have less effect. Its
237 * hard to notice a volume change from 100 to 95, without the
238 * val_cvt table, for example. However, the scaling has an
239 * ugly side effect, which is at the default volumes (75%), we
240 * wind up having the level set too high for some
241 * codec/amplifier combinations.
243 * Legacy Sun code didn't have this table, and some
244 * qualitative testing shows that it isn't really necessary.
246 left = 100 - ac97_val_cvt[left];
247 right = 100 - ac97_val_cvt[right];
248 #else
249 left = 100 - left;
250 right = 100 - right;
251 #endif
252 return (((left * ((1 << bits) - 1) / 100) << 8) |
253 (right * ((1 << bits) - 1) / 100));
256 uint16_t
257 ac_mono_scale(int val, int bits)
259 ASSERT(val <= 100);
261 if (bits < 0) { /* This is gain not ATTN */
262 bits = -bits;
263 } else {
264 val = 100 - val; /* convert to attenuation */
266 return (val * ((1 << bits) - 1) / 100);
269 audio_dev_t *
270 ac_get_dev(ac97_t *ac)
272 return (ac->d);
276 ac_get_prop(ac97_t *ac, char *prop, int defval)
278 int rv;
280 rv = ddi_prop_get_int(DDI_DEV_T_ANY, ac->dip, DDI_PROP_DONTPASS,
281 prop, defval);
282 return (rv);
286 * This calls the Hardware drivers access write routine
287 * to write to a device register.
289 #define WR(r, v) (ac)->wr((ac)->private, (r), (v))
290 #define RD(r) (ac)->rd((ac)->private, (r))
293 * Probe routines for optional controls
295 * These routines each probe one aspect of hardware
296 * for controls presents.
297 * If the control is present these routines should
298 * return none zero.
302 * Is the named register implemented? This routine saves and
303 * restores the original value, and relies on the fact that the
304 * registers (if implemented) will have at least one bit that acts
305 * as a mute (0x8000, 0x8080), so we can probe "silently".
307 * The probe logic is suggested by the AC'97 2.3 spec. (Unimplemented
308 * registers are required to return zero to facilitate this sort of
309 * detection.)
311 static int
312 ac_probe_reg(ac97_t *ac, uint8_t reg)
314 uint16_t val;
315 int rv = 0;
317 /* get the original value */
318 val = RD(reg);
319 WR(reg, 0xffff);
320 if (RD(reg) != 0) {
321 rv = 1;
323 /* restore the original value */
324 WR(reg, val);
325 return (rv);
329 * Does this device have bass/treble controls?
331 static int
332 ac_probe_tone(ac97_t *ac)
334 /* Bass/Treble contols present */
335 if (ac->caps & RR_BASS_TREBLE)
336 return (1);
337 else
338 return (0);
342 * If there is a loudness switch?
344 static int
345 ac_probe_loud(ac97_t *ac)
347 /* loudness contol present */
348 if (ac->caps & RR_LOUDNESS_SUPPORT)
349 return (1);
350 else
351 return (0);
355 * Does this device have a mono-mic input volume control?
357 static int
358 ac_probe_mmic(ac97_t *ac)
360 /* mono mic present */
361 if (ac->caps & RR_DEDICATED_MIC)
362 return (1);
363 else
364 return (0);
368 * Does this device have a simulated stereo switch?
370 static int
371 ac_probe_stsim(ac97_t *ac)
373 /* simulated stereocontol present */
374 if (ac->caps & RR_PSEUDO_STEREO)
375 return (1);
376 else
377 return (0);
381 * Does this device have a PC beeper input volume control?
383 static int
384 ac_probe_pcbeep(ac97_t *ac)
386 return (ac_probe_reg(ac, AC97_PC_BEEP_REGISTER));
390 * Does this device have AUX output port volume control?
392 static int
393 ac_probe_rear(ac97_t *ac)
395 if (ac->flags & AC97_FLAG_AUX_4CH)
396 return (1);
397 else
398 return (0);
403 * Does this device have a mic?
405 static int
406 ac_probe_mic(ac97_t *ac)
408 if ((!(ac->flags & AC97_FLAG_NO_MIC)) &&
409 (ac_probe_reg(ac, AC97_MIC_VOLUME_REGISTER))) {
410 ac->inputs |= (1U << INPUT_MIC);
411 return (1);
413 return (0);
417 * If this device has an AUX output port is it used for headphones?
419 static int
420 ac_probe_headphone(ac97_t *ac)
422 /* headphone control present */
423 if ((ac->flags & AC97_FLAG_AUX_HP) &&
424 !(ac->flags & AC97_FLAG_NO_HEADPHONE)) {
425 return (1);
427 return (0);
431 * Does this device have AUX output port volume control?
433 static int
434 ac_probe_auxout(ac97_t *ac)
436 /* ALT PCM control present */
437 if ((ac->flags & AC97_FLAG_AUX_LVL) &&
438 !(ac->flags & AC97_FLAG_NO_AUXOUT)) {
439 return (1);
441 return (0);
445 * Does this device have an AUX input port volume control?
447 static int
448 ac_probe_auxin(ac97_t *ac)
450 if ((!(ac->flags & AC97_FLAG_NO_AUXIN)) &&
451 (ac_probe_reg(ac, AC97_AUX_VOLUME_REGISTER))) {
452 ac->inputs |= (1U << INPUT_AUXIN);
453 return (1);
455 return (0);
459 * Does this device have a phone input port with a volume control?
461 static int
462 ac_probe_phone(ac97_t *ac)
464 if ((!(ac->flags & AC97_FLAG_NO_PHONE)) &&
465 (ac_probe_reg(ac, AC97_PHONE_VOLUME_REGISTER))) {
466 ac->inputs |= (1U << INPUT_PHONE);
467 return (1);
469 return (0);
473 * Does this device have a mono output port with volume control?
475 static int
476 ac_probe_mono(ac97_t *ac)
478 if (!(ac->flags & AC97_FLAG_SPEAKER_OK)) {
479 return (0);
481 if (ac_probe_reg(ac, AC97_MONO_MASTER_VOLUME_REGISTER)) {
482 return (1);
484 return (0);
488 * Does this device have a line input port with volume control?
490 static int
491 ac_probe_linein(ac97_t *ac)
493 if ((!(ac->flags & AC97_FLAG_NO_LINEIN)) &&
494 (ac_probe_reg(ac, AC97_LINE_IN_VOLUME_REGISTER))) {
495 ac->inputs |= (1U << INPUT_LINEIN);
496 return (1);
498 return (0);
502 * Does this device have a cdrom input port with volume control?
504 static int
505 ac_probe_cdrom(ac97_t *ac)
507 if ((!(ac->flags & AC97_FLAG_NO_CDROM)) &&
508 (ac_probe_reg(ac, AC97_CD_VOLUME_REGISTER))) {
509 ac->inputs |= (1U << INPUT_CD);
510 return (1);
512 return (0);
516 * Does this device have a video input port with volume control?
518 static int
519 ac_probe_video(ac97_t *ac)
521 if ((!(ac->flags & AC97_FLAG_NO_VIDEO)) &&
522 (ac_probe_reg(ac, AC97_VIDEO_VOLUME_REGISTER))) {
523 ac->inputs |= (1U << INPUT_VIDEO);
524 return (1);
526 return (0);
530 * Does this device have a 3D sound enhancement?
532 static int
533 ac_probe_3d(ac97_t *ac)
535 /* 3D control present */
536 if (ac->caps & RR_3D_STEREO_ENHANCE_MASK)
537 return (1);
538 else
539 return (0);
542 static int
543 ac_probe_3d_impl(ac97_t *ac, uint16_t mask)
545 int rv = 0;
546 uint16_t val;
548 if ((ac->caps & RR_3D_STEREO_ENHANCE_MASK) == 0)
549 return (0);
551 /* get the original value */
552 val = RD(AC97_THREE_D_CONTROL_REGISTER);
553 WR(AC97_THREE_D_CONTROL_REGISTER, mask);
554 if ((RD(AC97_THREE_D_CONTROL_REGISTER) & mask) != 0) {
555 rv = 1;
557 /* restore the original value */
558 WR(AC97_THREE_D_CONTROL_REGISTER, val);
559 return (rv);
562 static int
563 ac_probe_3d_depth(ac97_t *ac)
565 return (ac_probe_3d_impl(ac, TDCR_DEPTH_MASK));
568 static int
569 ac_probe_3d_center(ac97_t *ac)
571 return (ac_probe_3d_impl(ac, TDCR_CENTER_MASK));
575 * Does this device have a center output port with volume control?
577 static int
578 ac_probe_center(ac97_t *ac)
580 uint16_t val;
582 val = RD(AC97_EXTENDED_AUDIO_REGISTER);
584 /* center volume present */
585 if (val & EAR_CDAC)
586 return (1);
587 else
588 return (0);
592 * Does this device have a LFE (Sub-woofer) output port with
593 * a volume control?
595 static int
596 ac_probe_lfe(ac97_t *ac)
598 uint16_t val;
600 val = RD(AC97_EXTENDED_AUDIO_REGISTER);
602 /* We have LFE control */
603 if (val & EAR_LDAC)
604 return (1);
605 else
606 return (0);
611 * Are we a multichannel codec?
613 static int
614 ac_probe_front(ac97_t *ac)
616 uint16_t val;
618 val = RD(AC97_EXTENDED_AUDIO_REGISTER);
620 /* Are any of the Surround, Center, or LFE dacs present? */
621 if (val & (EAR_SDAC | EAR_CDAC | EAR_LDAC))
622 return (1);
623 else
624 return (0);
627 static int
628 ac_probe_lineout(ac97_t *ac)
630 /* if not multichannel, then use "lineout" instead of "front" label */
631 return (!ac_probe_front(ac));
634 static const char *ac_mics[] = {
635 AUDIO_PORT_MIC1,
636 AUDIO_PORT_MIC2,
637 NULL,
640 static const char *ac_monos[] = {
641 AUDIO_PORT_MONOMIX,
642 AUDIO_PORT_MIC,
643 NULL
647 * This calls the Hardware drivers access write routine
648 * to write to a device register.
650 void
651 ac_wr(ac97_t *ac, uint8_t reg, uint16_t val)
653 if ((reg < LAST_SHADOW_REG) && (reg > 0)) {
654 SHADOW(ac, reg) = val;
657 ac->wr(ac->private, reg, val);
661 * This obtains the shadowed value of a register. If the register is
662 * out of range, zero is returned.
664 * To read a hardware register, use the RD() macro above.
666 uint16_t
667 ac_rd(ac97_t *ac, uint8_t reg)
669 if ((reg < LAST_SHADOW_REG) && (reg > 0)) {
670 return (SHADOW(ac, reg));
672 return (ac->rd(ac->private, reg));
676 * This calls the hardware driver's access read/write routine
677 * to set bits in a device register.
679 void
680 ac_set(ac97_t *ac, uint8_t reg, uint16_t val)
682 ac_wr(ac, reg, ac->rd(ac->private, reg) | val);
686 * This calls the hardware driver's access read/write routine
687 * to clear bits in a device register.
689 void
690 ac_clr(ac97_t *ac, uint8_t reg, uint16_t val)
692 ac_wr(ac, reg, ac->rd(ac->private, reg) & ~val);
696 * Look for a control attached to this device based
697 * on its control number.
699 * If this control number is found the per controls state
700 * structure is returned.
702 ac97_ctrl_t *
703 ac97_control_find(ac97_t *ac, const char *name)
705 ac97_ctrl_t *ctrl;
706 list_t *l = &ac->ctrls;
708 /* Validate that ctrlnum is real and usable */
709 for (ctrl = list_head(l); ctrl; ctrl = list_next(l, ctrl)) {
710 if (strcmp(ctrl->actrl_name, name) == 0) {
711 return (ctrl);
714 return (NULL);
718 * This will update all the codec registers from the shadow table.
720 static void
721 ac_restore(ac97_t *ac)
724 * If we are restoring previous settings, just reload from the
725 * shadowed settings.
727 for (int i = 2; i < LAST_SHADOW_REG; i += sizeof (uint16_t)) {
728 ac->wr(ac->private, i, SHADOW(ac, i));
733 * This will update all the hardware controls to the initial values at
734 * start of day.
736 static void
737 ac_init_values(ac97_t *ac)
739 ac97_ctrl_t *ctrl;
741 for (ctrl = list_head(&ac->ctrls); ctrl;
742 ctrl = list_next(&ac->ctrls, ctrl)) {
743 ctrl->actrl_value = ctrl->actrl_initval;
744 ctrl->actrl_write_fn(ctrl, ctrl->actrl_initval);
749 * Select the input source for recording. This is the set routine
750 * for the control AUDIO_CONTROL_INPUTS.
752 static void
753 ac_insrc_set(ac97_ctrl_t *ctrl, uint64_t value)
755 ac97_t *ac = ctrl->actrl_ac97;
756 uint16_t set_val;
758 set_val = ddi_ffs(value & 0xffff);
759 if ((set_val > 0) && (set_val <= 8)) {
760 set_val--;
761 ac_wr(ac, AC97_RECORD_SELECT_CTRL_REGISTER,
762 set_val | (set_val << 8));
766 static void
767 ac_gpr_toggle(ac97_ctrl_t *ctrl, int bit, uint64_t onoff)
769 ac97_t *ac = ctrl->actrl_ac97;
770 uint16_t v;
772 v = SHADOW(ac, AC97_GENERAL_PURPOSE_REGISTER);
773 if (onoff) {
774 v |= bit;
775 } else {
776 v &= ~bit;
778 ac_wr(ac, AC97_GENERAL_PURPOSE_REGISTER, v);
781 static void
782 ac_3donoff_set(ac97_ctrl_t *ctrl, uint64_t value)
784 ac_gpr_toggle(ctrl, GPR_3D_STEREO_ENHANCE, value);
787 static void
788 ac_loudness_set(ac97_ctrl_t *ctrl, uint64_t value)
790 ac_gpr_toggle(ctrl, GPR_BASS_BOOST, value);
793 static void
794 ac_loopback_set(ac97_ctrl_t *ctrl, uint64_t value)
796 ac_gpr_toggle(ctrl, GPR_LPBK, value);
800 * This will set simulated stereo control to on or off.
802 static void
803 ac_stsim_set(ac97_ctrl_t *ctrl, uint64_t value)
805 ac_gpr_toggle(ctrl, GPR_ST, value);
809 * This will set mic select control to mic1=0 or mic2=1.
811 static void
812 ac_selmic_set(ac97_ctrl_t *ctrl, uint64_t value)
814 ac_gpr_toggle(ctrl, GPR_MS_MIC2, value & 2);
818 * This will set mono source select control to mix=0 or mic=1.
820 static void
821 ac_monosrc_set(ac97_ctrl_t *ctrl, uint64_t value)
823 ac_gpr_toggle(ctrl, GPR_MONO_MIC_IN, value & 2);
826 static void
827 ac_stereo_set(ac97_ctrl_t *ctrl, uint64_t value, uint8_t reg)
829 ac97_t *ac = ctrl->actrl_ac97;
830 uint8_t left, right;
831 uint16_t mute;
833 left = (value >> 8) & 0xff;
834 right = value & 0xff;
835 mute = value ? 0 : ctrl->actrl_muteable;
837 ac_wr(ac, reg, ac_val_scale(left, right, ctrl->actrl_bits) | mute);
840 static void
841 ac_mono_set(ac97_ctrl_t *ctrl, uint64_t value, uint8_t reg, int shift)
843 ac97_t *ac = ctrl->actrl_ac97;
844 uint8_t val;
845 uint16_t mute, v;
846 uint16_t mask;
848 val = value & 0xff;
849 mute = val ? 0 : ctrl->actrl_muteable;
851 mask = ctrl->actrl_muteable |
852 (((1 << ABS(ctrl->actrl_bits)) - 1) << shift);
854 v = SHADOW(ac, reg);
855 v &= ~mask; /* clear all of our bits, preserve others */
857 /* now set the mute bit, and volume bits */
858 v |= mute;
859 v |= (ac_mono_scale(val, ctrl->actrl_bits) << shift);
861 ac_wr(ac, reg, v);
864 static void
865 ac97_master_set(ac97_ctrl_t *ctrl, uint64_t value)
867 value = value | (value << 8);
868 ac_stereo_set(ctrl, value, AC97_PCM_OUT_VOLUME_REGISTER);
871 static void
872 ac97_lineout_set(ac97_ctrl_t *ctrl, uint64_t value)
874 ac_stereo_set(ctrl, value, AC97_MASTER_VOLUME_REGISTER);
877 static void
878 ac97_surround_set(ac97_ctrl_t *ctrl, uint64_t value)
880 ac_stereo_set(ctrl, value, AC97_EXTENDED_LRS_VOLUME_REGISTER);
883 static void
884 ac97_aux1out_set(ac97_ctrl_t *ctrl, uint64_t value)
886 ac_stereo_set(ctrl, value, AC97_HEADPHONE_VOLUME_REGISTER);
889 static void
890 ac97_headphone_set(ac97_ctrl_t *ctrl, uint64_t value)
892 ac_stereo_set(ctrl, value, AC97_HEADPHONE_VOLUME_REGISTER);
895 static void
896 ac_cd_set(ac97_ctrl_t *ctrl, uint64_t value)
898 ac_stereo_set(ctrl, value, AC97_CD_VOLUME_REGISTER);
901 static void
902 ac_video_set(ac97_ctrl_t *ctrl, uint64_t value)
904 ac_stereo_set(ctrl, value, AC97_VIDEO_VOLUME_REGISTER);
907 static void
908 ac_auxin_set(ac97_ctrl_t *ctrl, uint64_t value)
910 ac_stereo_set(ctrl, value, AC97_AUX_VOLUME_REGISTER);
913 static void
914 ac_linein_set(ac97_ctrl_t *ctrl, uint64_t value)
916 ac_stereo_set(ctrl, value, AC97_LINE_IN_VOLUME_REGISTER);
920 * This will set mono mic gain control.
922 static void
923 ac_monomic_set(ac97_ctrl_t *ctrl, uint64_t value)
925 ac_mono_set(ctrl, value, AC97_RECORD_GAIN_MIC_REGISTER, 0);
928 static void
929 ac_phone_set(ac97_ctrl_t *ctrl, uint64_t value)
931 ac_mono_set(ctrl, value, AC97_PHONE_VOLUME_REGISTER, 0);
934 static void
935 ac_mic_set(ac97_ctrl_t *ctrl, uint64_t value)
937 ac_mono_set(ctrl, value, AC97_MIC_VOLUME_REGISTER, 0);
940 static void
941 ac_speaker_set(ac97_ctrl_t *ctrl, uint64_t value)
943 ac_mono_set(ctrl, value, AC97_MONO_MASTER_VOLUME_REGISTER, 0);
946 static void
947 ac_pcbeep_set(ac97_ctrl_t *ctrl, uint64_t value)
949 ac_mono_set(ctrl, value, AC97_PC_BEEP_REGISTER, 1);
952 static void
953 ac_recgain_set(ac97_ctrl_t *ctrl, uint64_t value)
955 ac_stereo_set(ctrl, value, AC97_RECORD_GAIN_REGISTER);
958 static void
959 ac_center_set(ac97_ctrl_t *ctrl, uint64_t value)
961 ac_mono_set(ctrl, value, AC97_EXTENDED_C_LFE_VOLUME_REGISTER, 0);
964 static void
965 ac_lfe_set(ac97_ctrl_t *ctrl, uint64_t value)
967 ac_mono_set(ctrl, value, AC97_EXTENDED_C_LFE_VOLUME_REGISTER, 8);
970 static void
971 ac_bass_set(ac97_ctrl_t *ctrl, uint64_t value)
973 ac_mono_set(ctrl, value, AC97_MASTER_TONE_CONTROL_REGISTER, 8);
976 static void
977 ac_treble_set(ac97_ctrl_t *ctrl, uint64_t value)
979 ac_mono_set(ctrl, value, AC97_MASTER_TONE_CONTROL_REGISTER, 0);
982 static void
983 ac_3ddepth_set(ac97_ctrl_t *ctrl, uint64_t value)
986 * XXX: This is all wrong... 3D depth/center cannot necessarily
987 * be scaled, because the technology in use may vary. We
988 * need more information about each of the options available
989 * to do the right thing.
991 ac_mono_set(ctrl, value, AC97_THREE_D_CONTROL_REGISTER, 0);
994 static void
995 ac_3dcent_set(ac97_ctrl_t *ctrl, uint64_t value)
998 * XXX: This is all wrong... 3D depth/center cannot necessarily
999 * be scaled, because the technology in use may vary. We
1000 * need more information about each of the options available
1001 * to do the right thing.
1003 ac_mono_set(ctrl, value, AC97_THREE_D_CONTROL_REGISTER, 8);
1006 static void
1007 ac97_micboost_set(ac97_ctrl_t *ctrl, uint64_t value)
1009 ac97_t *ac = ctrl->actrl_ac97;
1010 uint16_t v;
1012 v = SHADOW(ac, AC97_MIC_VOLUME_REGISTER);
1013 if (value) {
1014 v |= MICVR_20dB_BOOST;
1015 } else {
1016 v &= ~MICVR_20dB_BOOST;
1018 ac_wr(ac, AC97_MIC_VOLUME_REGISTER, v);
1022 * This will return the stored value for any control that has been set.
1023 * Note this does not return the actual hardware value from a port. But
1024 * instead returns the cached value from the last write to the hardware
1025 * port.
1027 * arg - This control structure for this control.
1028 * value - This is a pointer to the location to put the
1029 * controls value.
1031 * On success zero is returned.
1034 ac97_control_get(ac97_ctrl_t *ctrl, uint64_t *value)
1036 *value = ctrl->actrl_value;
1038 return (0);
1042 ac97_control_set(ac97_ctrl_t *ctrl, uint64_t value)
1044 uint8_t v1, v2;
1046 /* a bit of quick checking */
1047 switch (ctrl->actrl_type) {
1048 case AUDIO_CTRL_TYPE_STEREO:
1049 v1 = (value >> 8) & 0xff;
1050 v2 = value & 0xff;
1051 if ((v1 < ctrl->actrl_minval) || (v1 > ctrl->actrl_maxval) ||
1052 (v2 < ctrl->actrl_minval) || (v2 > ctrl->actrl_maxval) ||
1053 (value > 0xffff)) {
1054 return (EINVAL);
1056 break;
1058 case AUDIO_CTRL_TYPE_ENUM:
1059 if ((value & ~ctrl->actrl_minval) !=
1060 (ctrl->actrl_maxval & ~ctrl->actrl_minval)) {
1061 return (EINVAL);
1063 break;
1065 case AUDIO_CTRL_TYPE_MONO:
1066 case AUDIO_CTRL_TYPE_BOOLEAN:
1067 if ((value < ctrl->actrl_minval) ||
1068 (value > ctrl->actrl_maxval)) {
1069 return (EINVAL);
1071 break;
1074 ctrl->actrl_value = value;
1075 ctrl->actrl_write_fn(ctrl, value);
1077 return (0);
1080 static int
1081 ac_get_value(void *arg, uint64_t *value)
1083 return (ac97_control_get(arg, value));
1086 static int
1087 ac_set_value(void *arg, uint64_t value)
1089 return (ac97_control_set(arg, value));
1093 * Reset the analog codec hardware
1095 * Reset all analog AC97 hardware, input ADC's, output DAC's and MIXER.
1096 * Wait a resonable amount of time for hardware to become ready.
1098 static void
1099 ac_analog_reset(ac97_t *ac)
1101 uint16_t tmp;
1102 int wait = 1000; /* delay for up to 1s */
1104 /* Clear stale data and resync register accesses */
1105 tmp = RD(AC97_POWERDOWN_CTRL_STAT_REGISTER);
1107 /* reset the codec */
1108 WR(AC97_RESET_REGISTER, 0);
1109 tmp = RD(AC97_RESET_REGISTER);
1111 /* power up */
1112 WR(AC97_POWERDOWN_CTRL_STAT_REGISTER, 0);
1114 /* Wait for ADC/DAC/MIXER to become ready */
1115 while (wait--) {
1116 /* 1 msec delay */
1117 drv_usecwait(1000);
1119 /* If all ready - end delay */
1120 tmp = RD(AC97_POWERDOWN_CTRL_STAT_REGISTER);
1121 SHADOW(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER) = tmp;
1122 if ((tmp & PCSR_POWERD_UP) == PCSR_POWERD_UP) {
1123 return;
1127 audio_dev_warn(ac->d, "AC'97 analog power up timed out");
1131 * This is the internal hardware reset routine.
1132 * It has no locking and we must be locked before it is
1133 * called!
1135 * This will reset and re-initialize the device.
1136 * It has two modes of operation that affect how it handles
1137 * all controls.
1139 * It re-initializes the device and reloads values with
1140 * last updated versions.
1142 static void
1143 ac_hw_reset(ac97_t *ac)
1146 * Fully Power up the device
1148 if (ac->flags & AC97_FLAG_AMPLIFIER) {
1149 /* power up - external amp powerd up */
1150 ac_wr(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER, 0);
1151 } else {
1152 /* power up - external amp powered down */
1153 ac_wr(ac, AC97_POWERDOWN_CTRL_STAT_REGISTER, PCSR_EAPD);
1156 ac_wr(ac, AC97_GENERAL_PURPOSE_REGISTER, 0);
1158 switch (ac->vid) {
1159 case AC97_CODEC_STAC9708:
1160 #if 0
1161 /* non-inverted phase */
1162 /* ac_rd(ac, AC97_VENDOR_REGISTER_11) & ~0x8); */
1163 #endif
1164 WR(AC97_VENDOR_REGISTER_11, 8);
1165 break;
1167 case AC97_CODEC_AD1886:
1168 /* jack sense */
1169 WR(AC97_VENDOR_REGISTER_13,
1170 (RD(AC97_VENDOR_REGISTER_13) & ~0xEF) | 0x10);
1171 break;
1173 case AC97_CODEC_AD1888:
1174 WR(AC97_VENDOR_REGISTER_15, 0xC420);
1175 #if 0
1176 /* GED: This looks fishy to me, so I'm nuking it for now */
1177 /* headphone/aux volume (?) */
1178 ac_wr(ac, AC97_HEADPHONE_VOLUME_REGISTER, 0x0808);
1179 #endif
1180 break;
1182 case AC97_CODEC_AD1980:
1183 #if 0
1184 /* set jacksense to mute line if headphone is plugged */
1185 WR(AC97_VENDOR_REGISTER_13,
1186 (RD(AC97_VENDOR_REGISTER_13) & ~0xe00) | 0x400);
1187 #endif
1188 WR(AC97_VENDOR_REGISTER_15, 0xC420);
1189 break;
1191 case AC97_CODEC_AD1985:
1192 WR(AC97_VENDOR_REGISTER_15, 0xC420);
1193 break;
1195 case AC97_CODEC_WM9704:
1196 /* enable I2S */
1197 WR(AC97_VENDOR_REGISTER_01, RD(AC97_VENDOR_REGISTER_01) | 0x80);
1198 break;
1200 case AC97_CODEC_VT1612A:
1201 case AC97_CODEC_VT1617A:
1202 case AC97_CODEC_VT1616:
1203 /* Turn on Center, Surround, and LFE DACs */
1204 ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER,
1205 EASCR_PRI | EASCR_PRJ | EASCR_PRK);
1206 WR(AC97_VENDOR_REGISTER_01, 0x0230);
1207 break;
1209 case AC97_CODEC_YMF753:
1210 /* set TX8 + 3AWE */
1211 WR(AC97_VENDOR_REGISTER_07, RD(AC97_VENDOR_REGISTER_07) | 0x9);
1212 break;
1214 default:
1215 break;
1218 /* call codec specific reset hook */
1219 if (ac->codec_reset != NULL) {
1220 ac->codec_reset(ac);
1223 /* Turn off variable sampling rate support */
1224 ac_clr(ac, AC97_EXTENDED_AUDIO_STAT_CTRL_REGISTER, EASCR_VRA);
1228 * This will reset and re-initialize the device. It is still incumbent
1229 * on the caller (or the audio framework) to replay control settings!
1231 void
1232 ac97_reset(ac97_t *ac)
1234 ac_analog_reset(ac);
1235 ac_hw_reset(ac);
1236 ac_restore(ac);
1240 * Return the number of channels supported by this codec.
1243 ac97_num_channels(ac97_t *ac)
1245 return (ac->nchan);
1249 * Register a control -- if it fails, it will generate a message to
1250 * syslog, but the driver muddles on. (Failure to register a control
1251 * should never occur, but is generally benign if it happens.)
1253 void
1254 ac97_control_register(ac97_ctrl_t *ctrl)
1256 ac97_t *ac = ctrl->actrl_ac97;
1257 ASSERT(ac->d != NULL);
1259 ctrl->actrl_suppress = B_FALSE;
1261 /* Register control with framework */
1262 ctrl->actrl_ctrl = audio_dev_add_control(ac->d, &ctrl->actrl_desc,
1263 ac_get_value, ac_set_value, ctrl);
1264 if (ctrl->actrl_ctrl == NULL) {
1265 audio_dev_warn(ac->d, "AC97 %s alloc failed",
1266 ctrl->actrl_name);
1270 void
1271 ac97_control_unregister(ac97_ctrl_t *ctrl)
1273 ctrl->actrl_suppress = B_TRUE;
1275 if (ctrl->actrl_ctrl != NULL) {
1276 audio_dev_del_control(ctrl->actrl_ctrl);
1277 ctrl->actrl_ctrl = NULL;
1281 const char *
1282 ac97_control_name(ac97_ctrl_t *ctrl)
1284 return (ctrl->actrl_name);
1287 const audio_ctrl_desc_t *
1288 ac97_control_desc(ac97_ctrl_t *ctrl)
1290 return (&ctrl->actrl_desc);
1293 void
1294 ac97_register_controls(ac97_t *ac)
1296 ac97_ctrl_t *ctrl;
1298 for (ctrl = list_head(&ac->ctrls); ctrl;
1299 ctrl = list_next(&ac->ctrls, ctrl)) {
1300 if (ctrl->actrl_suppress)
1301 continue;
1302 ac97_control_register(ctrl);
1306 void
1307 ac97_walk_controls(ac97_t *ac, ac97_ctrl_walk_t walker, void *arg)
1309 ac97_ctrl_t *ctrl;
1311 for (ctrl = list_head(&ac->ctrls); ctrl;
1312 ctrl = list_next(&ac->ctrls, ctrl)) {
1313 if (!(*walker)(ctrl, arg)) {
1314 break;
1319 void
1320 ac_add_control(ac97_t *ac, ac97_ctrl_probe_t *cpt)
1322 ac97_ctrl_t *ctrl;
1323 boolean_t is_new;
1325 ASSERT(ac);
1326 ASSERT(ac->d);
1328 ctrl = ac97_control_find(ac, cpt->cp_name);
1329 if (ctrl != NULL) {
1330 is_new = B_FALSE;
1331 } else {
1332 ctrl = kmem_zalloc(sizeof (ac97_ctrl_t), KM_SLEEP);
1333 is_new = B_TRUE;
1335 ctrl->actrl_ac97 = ac;
1336 ctrl->actrl_minval = cpt->cp_minval;
1337 ctrl->actrl_maxval = cpt->cp_maxval;
1338 ctrl->actrl_type = cpt->cp_type;
1339 ctrl->actrl_name = cpt->cp_name;
1340 ctrl->actrl_flags = cpt->cp_flags;
1341 if (cpt->cp_enum) {
1342 for (int e = 0; e < 64; e++) {
1343 if (cpt->cp_enum[e] == NULL)
1344 break;
1345 ctrl->actrl_enum[e] = cpt->cp_enum[e];
1350 * Warning for extended controls this field gets changed
1351 * by audio_dev_add_control() to be a unique value.
1353 ctrl->actrl_initval = cpt->cp_initval;
1354 ctrl->actrl_muteable = cpt->cp_muteable;
1355 ctrl->actrl_write_fn = cpt->cp_write_fn;
1356 ctrl->actrl_bits = cpt->cp_bits;
1359 * Not that it can not be referenced until it is in the
1360 * list. So again by adding to the list last we avoid the need
1361 * for locks.
1363 if (is_new)
1364 list_insert_tail(&ac->ctrls, ctrl);
1368 * De-Register and free up a control
1370 void
1371 ac97_control_remove(ac97_ctrl_t *ctrl)
1373 ac97_t *ac = ctrl->actrl_ac97;
1375 list_remove(&ac->ctrls, ctrl);
1377 if (ctrl->actrl_ctrl != NULL)
1378 audio_dev_del_control(ctrl->actrl_ctrl);
1379 kmem_free(ctrl, sizeof (ac97_ctrl_t));
1383 * This is the master list of all controls known and handled by
1384 * the AC97 framework. This is the list used to probe, allocate
1385 * and configure controls. If a control is not in this list it
1386 * will not be handled. If a control is in this list but does not
1387 * have a probe routine then it will always be included. If a
1388 * control in list has a probe routine then it must return true
1389 * for that control to be included.
1392 #define MONCTL (AC97_FLAGS | AUDIO_CTRL_FLAG_MONITOR)
1393 #define PLAYCTL (AC97_FLAGS | AUDIO_CTRL_FLAG_PLAY)
1394 #define RECCTL (AC97_FLAGS | AUDIO_CTRL_FLAG_REC)
1395 #define T3DCTL (AC97_FLAGS | AUDIO_CTRL_FLAG_3D)
1396 #define TONECTL (AC97_FLAGS | AUDIO_CTRL_FLAG_TONE)
1397 #define MAINVOL (PLAYCTL | AUDIO_CTRL_FLAG_MAINVOL)
1398 #define PCMVOL (PLAYCTL | AUDIO_CTRL_FLAG_PCMVOL)
1399 #define RECVOL (RECCTL | AUDIO_CTRL_FLAG_RECVOL)
1400 #define MONVOL (MONCTL | AUDIO_CTRL_FLAG_MONVOL)
1402 ac97_ctrl_probe_t ctrl_probe_tbl[] = {
1404 /* Master PCM Volume */
1405 {AUDIO_CTRL_ID_VOLUME, INIT_VAL_MAIN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1406 PCMVOL, PCMOVR_MUTE, ac97_master_set, NULL, 5},
1408 /* LINE out volume */
1409 {AUDIO_CTRL_ID_LINEOUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1410 MAINVOL, 0x8080, ac97_lineout_set, ac_probe_lineout, 6},
1412 /* Front volume */
1413 {AUDIO_CTRL_ID_FRONT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1414 MAINVOL, 0x8080, ac97_lineout_set, ac_probe_front, 6},
1416 /* 4CH out volume (has one of three possible uses, first use) */
1417 {AUDIO_CTRL_ID_SURROUND, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1418 MAINVOL, 0x8080, ac97_surround_set, ac_probe_rear, 6},
1420 /* ALT out volume (has one of three possible uses, second use) */
1421 {AUDIO_CTRL_ID_HEADPHONE, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1422 MAINVOL, 0x8080, ac97_headphone_set, ac_probe_headphone, 6},
1424 /* ALT out volume (has one of three possible uses, third use) */
1425 {AUDIO_CTRL_ID_AUX1OUT, INIT_VAL_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1426 MAINVOL, 0x8080, ac97_aux1out_set, ac_probe_auxout, 6},
1428 /* center out volume */
1429 {AUDIO_CTRL_ID_CENTER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1430 MAINVOL, EXLFEVR_CENTER_MUTE, ac_center_set, ac_probe_center, 6},
1432 /* LFE out volume (sub-woofer) */
1433 {AUDIO_CTRL_ID_LFE, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1434 MAINVOL, EXLFEVR_LFE_MUTE, ac_lfe_set, ac_probe_lfe, 6},
1436 /* MONO out volume */
1437 {AUDIO_CTRL_ID_SPEAKER, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1438 MAINVOL, MMVR_MUTE, ac_speaker_set, ac_probe_mono, 6},
1440 /* Record in GAIN */
1441 {AUDIO_CTRL_ID_RECGAIN, INIT_IGAIN_ST, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1442 RECVOL, RGR_MUTE, ac_recgain_set, NULL, -4},
1444 /* MIC in volume */
1445 {AUDIO_CTRL_ID_MIC, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1446 MONVOL, MICVR_MUTE, ac_mic_set, ac_probe_mic, 5},
1448 /* LINE in volume */
1449 {AUDIO_CTRL_ID_LINEIN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1450 MONVOL, LIVR_MUTE, ac_linein_set, ac_probe_linein, 5},
1452 /* CD in volume */
1453 {AUDIO_CTRL_ID_CD, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1454 MONVOL, CDVR_MUTE, ac_cd_set, ac_probe_cdrom, 5},
1456 /* VIDEO in volume */
1457 {AUDIO_CTRL_ID_VIDEO, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1458 MONVOL, VIDVR_MUTE, ac_video_set, ac_probe_video, 5},
1460 /* AUX in volume */
1461 {AUDIO_CTRL_ID_AUX1IN, 0, 0, 100, AUDIO_CTRL_TYPE_STEREO,
1462 MONVOL, AUXVR_MUTE, ac_auxin_set, ac_probe_auxin, 5},
1464 /* PHONE in volume */
1465 {AUDIO_CTRL_ID_PHONE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1466 MONVOL, PVR_MUTE, ac_phone_set, ac_probe_phone, 5},
1468 /* PC BEEPER in volume (motherboard speaker pins) */
1469 {AUDIO_CTRL_ID_BEEP, INIT_VAL_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1470 AC97_RW, PCBR_MUTE, ac_pcbeep_set, ac_probe_pcbeep, 4},
1472 /* BASS out level (note, zero is hardware bypass) */
1473 {AUDIO_CTRL_ID_BASS, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1474 TONECTL, 0, ac_bass_set, ac_probe_tone, 4},
1476 /* TREBLE out level (note, zero is hardware bypass) */
1477 {AUDIO_CTRL_ID_TREBLE, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1478 TONECTL, 0, ac_treble_set, ac_probe_tone, 4},
1480 /* Loudness on/off switch */
1481 {AUDIO_CTRL_ID_LOUDNESS, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1482 TONECTL, 0, ac_loudness_set, ac_probe_loud, 0},
1484 /* 3D depth out level */
1485 {AUDIO_CTRL_ID_3DDEPTH, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1486 T3DCTL, 0, ac_3ddepth_set, ac_probe_3d_depth, 4},
1488 /* 3D center out level */
1489 {AUDIO_CTRL_ID_3DCENT, 0, 0, 100, AUDIO_CTRL_TYPE_MONO,
1490 T3DCTL, 0, ac_3dcent_set, ac_probe_3d_center, 4},
1492 /* 3D enhance on/off switch */
1493 {AUDIO_CTRL_ID_3DENHANCE, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1494 T3DCTL, 0, ac_3donoff_set, ac_probe_3d, 0},
1496 /* MIC BOOST switch */
1497 {AUDIO_CTRL_ID_MICBOOST, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1498 RECCTL, 0, ac97_micboost_set, ac_probe_mic, 0},
1500 /* Loopback on/off switch */
1501 {AUDIO_CTRL_ID_LOOPBACK, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1502 AC97_RW, 0, ac_loopback_set, NULL, 0},
1505 * The following selectors *must* come after the others, as they rely
1506 * on the probe results of other controls.
1508 /* record src select (only one port at a time) */
1509 {AUDIO_CTRL_ID_RECSRC, (1U << INPUT_MIC), 0, 0, AUDIO_CTRL_TYPE_ENUM,
1510 RECCTL, 0, ac_insrc_set, NULL, 0, ac_insrcs},
1512 /* Start of non-standard private controls */
1514 /* Simulated stereo on/off switch */
1515 {AUDIO_CTRL_ID_STEREOSIM, 0, 0, 1, AUDIO_CTRL_TYPE_BOOLEAN,
1516 AC97_RW, 0, ac_stsim_set, ac_probe_stsim, 0},
1518 /* mono MIC GAIN */
1519 {AUDIO_CTRL_ID_MICGAIN, INIT_IGAIN_MN, 0, 100, AUDIO_CTRL_TYPE_MONO,
1520 RECCTL, RGMR_MUTE, ac_monomic_set, ac_probe_mmic, -4},
1522 /* MIC select switch 0=mic1 1=mic2 */
1523 {AUDIO_CTRL_ID_MICSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1524 RECCTL, 0, ac_selmic_set, ac_probe_mic, 0, ac_mics},
1526 /* MONO out src select 0=mix 1=mic */
1527 {AUDIO_CTRL_ID_SPKSRC, 1, 3, 3, AUDIO_CTRL_TYPE_ENUM,
1528 AC97_RW, 0, ac_monosrc_set, ac_probe_mono, 0, ac_monos},
1530 {NULL}
1534 * Probe all possible controls and register existing
1535 * ones and set initial values
1537 * Returns zero on success
1539 static void
1540 ac_probeinit_ctrls(ac97_t *ac, int vol_bits, int enh_bits)
1542 ac97_ctrl_probe_t *cpt;
1543 ac97_ctrl_probe_t my_cpt;
1545 ASSERT(ac);
1548 * Set some ports which are always present.
1550 ac->inputs = (1U << INPUT_STEREOMIX) | (1U << INPUT_MONOMIX);
1551 for (cpt = &ctrl_probe_tbl[0]; cpt->cp_name != NULL; cpt++) {
1552 bcopy(cpt, &my_cpt, sizeof (my_cpt));
1554 if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_RECSRC) == 0) {
1555 my_cpt.cp_minval |= ac->inputs;
1556 my_cpt.cp_maxval |= ac->inputs;
1559 if (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_MICBOOST) == 0) {
1560 if (ac->flags & AC97_FLAG_MICBOOST)
1561 my_cpt.cp_initval = 1;
1564 if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_FRONT) == 0) ||
1565 (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_HEADPHONE) == 0) ||
1566 (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SURROUND) == 0) ||
1567 (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_SPEAKER) == 0)) {
1568 my_cpt.cp_bits = vol_bits;
1571 if ((strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DDEPTH) == 0) ||
1572 (strcmp(my_cpt.cp_name, AUDIO_CTRL_ID_3DCENT) == 0)) {
1573 my_cpt.cp_bits = enh_bits;
1576 if (!my_cpt.cp_probe || my_cpt.cp_probe(ac)) {
1577 ac_add_control(ac, &my_cpt);
1581 if (ac->codec_init != NULL) {
1582 ac->codec_init(ac);
1587 * Allocate an AC97 instance for use by a hardware driver.
1589 * returns an allocated and initialize ac97 structure.
1591 ac97_t *
1592 ac97_alloc(dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr, void *priv)
1594 ac97_t *ac;
1596 ac = kmem_zalloc(sizeof (ac97_t), KM_SLEEP);
1597 ac->dip = dip;
1598 ac->rd = rd;
1599 ac->wr = wr;
1600 ac->private = priv;
1602 list_create(&ac->ctrls, sizeof (struct ac97_ctrl),
1603 offsetof(struct ac97_ctrl, actrl_linkage));
1605 #define PROP_FLAG(prop, flag, def) \
1606 if (ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, \
1607 (prop), (def))) { \
1608 ac->flags |= (flag); \
1609 } else { \
1610 ac->flags &= ~(flag); \
1614 * Engage the external amplifier by default, suppress with
1615 * a property of the form "ac97-amplifier=0".
1617 PROP_FLAG(AC97_PROP_AMPLIFIER, AC97_FLAG_AMPLIFIER, 1);
1620 * We cannot necessarily know if the headphone jack is present
1621 * or not. There's a technique to probe the codec for
1622 * headphone support, but many vendors seem to simply hang the
1623 * headphone jack on the line out circuit, and have some kind
1624 * of jack sense detection to enable or disable it by default.
1625 * None of this is visible in the AC'97 registers.
1627 * We cannot do much about it, but what we can do is offer users
1628 * a way to suppress the option for a headphone port. Users and
1629 * administrators can then set a flag in the driver.conf to suppress
1630 * the option from display.
1632 * It turns out that this problem exists for other signals as
1633 * well.
1635 PROP_FLAG(AC97_PROP_NO_HEADPHONE, AC97_FLAG_NO_HEADPHONE, 0);
1636 PROP_FLAG(AC97_PROP_NO_AUXOUT, AC97_FLAG_NO_AUXOUT, 0);
1637 PROP_FLAG(AC97_PROP_NO_CDROM, AC97_FLAG_NO_CDROM, 0);
1638 PROP_FLAG(AC97_PROP_NO_AUXIN, AC97_FLAG_NO_AUXIN, 0);
1639 PROP_FLAG(AC97_PROP_NO_VIDEO, AC97_FLAG_NO_VIDEO, 0);
1640 PROP_FLAG(AC97_PROP_NO_LINEIN, AC97_FLAG_NO_LINEIN, 0);
1641 PROP_FLAG(AC97_PROP_NO_MIC, AC97_FLAG_NO_MIC, 0);
1644 * Most SPARC systems use the AC97 monoaural output for the
1645 * built-in speaker. On these systems, we want to expose and
1646 * enable the built-in speaker by default.
1648 * On most x86 systems, the mono output is not connected to
1649 * anything -- the AC'97 spec makes it pretty clear that the
1650 * output was actually intended for use with speaker phones.
1651 * So on those systems, we really don't want to activate the
1652 * speaker -- we don't even want to expose it's presence
1653 * normally.
1655 * However, there could be an exception to the rule here. To
1656 * facilitate this, we allow for the presence of the property
1657 * to indicate that the speaker should be exposed. Whether it
1658 * is enabled by default or not depends on the value of the
1659 * property. (Generally on SPARC, we enable by default. On
1660 * other systems we do not.)
1662 if (ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
1663 AC97_PROP_SPEAKER)) {
1664 ac->flags |= AC97_FLAG_SPEAKER_OK;
1665 PROP_FLAG(AC97_PROP_SPEAKER, AC97_FLAG_SPEAKER, 0);
1669 * Enable microphone boost (20dB normally) by default?
1671 PROP_FLAG(AC97_PROP_MICBOOST, AC97_FLAG_MICBOOST, 0);
1673 return (ac);
1676 * Allocate an AC97 instance for use by a hardware driver.
1678 * returns an allocated and initialize ac97 structure.
1680 ac97_t *
1681 ac97_allocate(audio_dev_t *adev, dev_info_t *dip, ac97_rd_t rd, ac97_wr_t wr,
1682 void *priv)
1684 ac97_t *ac;
1686 ac = ac97_alloc(dip, rd, wr, priv);
1687 if (ac != NULL) {
1688 ac->d = adev;
1690 return (ac);
1694 * Free an AC97 instance.
1696 void
1697 ac97_free(ac97_t *ac)
1699 ac97_ctrl_t *ctrl;
1701 /* Clear out any controls that are still attached */
1702 while ((ctrl = list_head(&ac->ctrls)) != NULL) {
1703 ac97_control_remove(ctrl);
1706 list_destroy(&ac->ctrls);
1707 kmem_free(ac, sizeof (ac97_t));
1710 static struct vendor {
1711 unsigned id;
1712 const char *name;
1713 } vendors[] = {
1714 { AC97_VENDOR_ADS, "Analog Devices" },
1715 { AC97_VENDOR_AKM, "Asahi Kasei" },
1716 { AC97_VENDOR_ALC, "Realtek" },
1717 { AC97_VENDOR_ALG, "Avance Logic" },
1718 { AC97_VENDOR_CMI, "C-Media" },
1719 { AC97_VENDOR_CRY, "Cirrus Logic" },
1720 { AC97_VENDOR_CXT, "Conexant" },
1721 { AC97_VENDOR_EMC, "eMicro" },
1722 { AC97_VENDOR_ESS, "ESS Technology" },
1723 { AC97_VENDOR_EV, "Ectiva" },
1724 { AC97_VENDOR_HRS, "Intersil" },
1725 { AC97_VENDOR_ICE, "ICEnsemble" },
1726 { AC97_VENDOR_ITE, "ITE, Inc." },
1727 { AC97_VENDOR_NSC, "National Semiconductor" },
1728 { AC97_VENDOR_PSC, "Philips Semiconductor" },
1729 { AC97_VENDOR_SIL, "Silicon Laboratories" },
1730 { AC97_VENDOR_ST, "SigmaTel" },
1731 { AC97_VENDOR_TRA, "TriTech", },
1732 { AC97_VENDOR_TXN, "Texas Instruments", },
1733 { AC97_VENDOR_VIA, "VIA Technologies" },
1734 { AC97_VENDOR_WML, "Wolfson" },
1735 { AC97_VENDOR_YMH, "Yamaha" },
1736 { 0, NULL },
1739 static struct codec {
1740 unsigned id;
1741 const char *name;
1742 int enh_bits;
1743 void (*init)(ac97_t *ac);
1744 void (*reset)(ac97_t *ac);
1745 } codecs[] = {
1746 { AC97_CODEC_AK4540, "AK4540" },
1747 { AC97_CODEC_STAC9700, "STAC9700" },
1748 { AC97_CODEC_STAC9701, "STAC9701" },
1749 { AC97_CODEC_STAC9701_2, "STAC9701" },
1750 { AC97_CODEC_STAC9704, "STAC9704" },
1751 { AC97_CODEC_STAC9705, "STAC9705" },
1752 { AC97_CODEC_STAC9721, "STAC9721" },
1753 { AC97_CODEC_STAC9708, "STAC9708", 2 },
1754 { AC97_CODEC_STAC9744, "STAC9744" },
1755 { AC97_CODEC_STAC9750, "STAC9750", 3 },
1756 { AC97_CODEC_STAC9752, "STAC9752", 3 },
1757 { AC97_CODEC_STAC9756, "STAC9756", 3 },
1758 { AC97_CODEC_STAC9758, "STAC9758", 3 },
1759 { AC97_CODEC_STAC9766, "STAC9766", 3 },
1760 { AC97_CODEC_TR28028, "TR28028" },
1761 { AC97_CODEC_TR28028_2, "TR28028" },
1762 { AC97_CODEC_TR28023, "TR28023" },
1763 { AC97_CODEC_TR28023_2, "TR28023" },
1764 { AC97_CODEC_EM28028, "EM28028" },
1765 { AC97_CODEC_CX20468, "CX20468" },
1766 { AC97_CODEC_CX20468_2, "CX20468" },
1767 { AC97_CODEC_CX20468_21, "CX20468-21" },
1768 { AC97_CODEC_CS4297, "CS4297" },
1769 { AC97_CODEC_CS4297A, "CS4297A" },
1770 { AC97_CODEC_CS4294, "CS4294" },
1771 { AC97_CODEC_CS4299, "CS4299" },
1772 { AC97_CODEC_CS4202, "CS4202" },
1773 { AC97_CODEC_CS4205, "CS4205" },
1774 { AC97_CODEC_AD1819B, "AD1819B" },
1775 { AC97_CODEC_AD1881, "AD1881" },
1776 { AC97_CODEC_AD1881A, "AD1881A" },
1777 { AC97_CODEC_AD1885, "AD1885" },
1778 { AC97_CODEC_AD1886, "AD1886" },
1779 { AC97_CODEC_AD1887, "AD1887" },
1780 { AC97_CODEC_AD1888, "AD1888" },
1781 { AC97_CODEC_AD1980, "AD1980" },
1782 { AC97_CODEC_AD1981, "AD1981" }, /* no data sheet */
1783 { AC97_CODEC_AD1981A, "AD1981A", 0, ad1981a_init },
1784 { AC97_CODEC_AD1981B, "AD1981B", 0, ad1981b_init },
1785 { AC97_CODEC_AD1985, "AD1985" },
1786 { AC97_CODEC_WM9701A, "WM9701A" },
1787 { AC97_CODEC_WM9703, "WM9703" },
1788 { AC97_CODEC_WM9704, "WM9704" },
1789 { AC97_CODEC_ES1921, "ES1921" },
1790 { AC97_CODEC_ICE1232, "ICE1232/VT1611A" },
1791 { AC97_CODEC_LM4550, "LM4550" },
1792 { AC97_CODEC_VT1612A, "VT1612A" },
1793 { AC97_CODEC_VT1616, "VT1616" },
1794 { AC97_CODEC_VT1616A, "VT1616A" },
1795 { AC97_CODEC_VT1617A, "VT1617A" },
1796 { AC97_CODEC_VT1618, "VT1618" },
1797 { AC97_CODEC_ALC100, "ALC100", 2 },
1798 { AC97_CODEC_ALC200P, "ALC200P", 2 },
1799 { AC97_CODEC_ALC202, "ALC202", 2 },
1800 { AC97_CODEC_ALC203, "ALC203", 2 },
1801 { AC97_CODEC_ALC250, "ALC250", 2 },
1802 { AC97_CODEC_ALC250_2, "ALC250", 2 },
1803 { AC97_CODEC_ALC650, "ALC650", 2, alc650_init },
1804 { AC97_CODEC_ALC655, "ALC655", 2, alc650_init },
1805 { AC97_CODEC_ALC658, "ALC658", 2, alc650_init },
1806 { AC97_CODEC_ALC850, "ALC850", 2, alc850_init },
1807 { AC97_CODEC_EV1938, "EV1938" },
1808 { AC97_CODEC_CMI9738, "CMI9738", 0, cmi9738_init },
1809 { AC97_CODEC_CMI9739, "CMI9739", 0, cmi9739_init },
1810 { AC97_CODEC_CMI9780, "CMI9780" },
1811 { AC97_CODEC_CMI9761, "CMI9761A", 0, cmi9761_init },
1812 { AC97_CODEC_CMI9761_2, "CMI9761B", 0, cmi9761_init },
1813 { AC97_CODEC_CMI9761_3, "CMI9761A+", 0, cmi9761_init },
1814 { AC97_CODEC_YMF743, "YMF743" },
1815 { AC97_CODEC_YMF753, "YMF753" },
1816 { 0, NULL }
1819 void
1820 ac97_probe_controls(ac97_t *ac)
1822 uint32_t vid1, vid2;
1823 uint16_t ear;
1824 const char *name = NULL;
1825 const char *vendor = NULL;
1826 int enh_bits;
1827 int vol_bits;
1828 uint32_t flags;
1829 char nmbuf[128];
1830 char buf[128];
1832 /* This is only valid when used with new style ac97_allocate(). */
1833 ASSERT(ac->d);
1835 ac_analog_reset(ac);
1837 vid1 = RD(AC97_VENDOR_ID1_REGISTER);
1838 vid2 = RD(AC97_VENDOR_ID2_REGISTER);
1840 if (vid1 == 0xffff) {
1841 audio_dev_warn(ac->d, "AC'97 codec unresponsive");
1842 return;
1845 ac->vid = (vid1 << 16) | vid2;
1848 * Find out kind of codec we have and set any special case
1849 * settings needed.
1851 for (int i = 0; codecs[i].id; i++) {
1852 if (ac->vid == codecs[i].id) {
1853 name = codecs[i].name;
1854 enh_bits = codecs[i].enh_bits;
1855 ac->codec_init = codecs[i].init;
1856 break;
1859 for (int i = 0; vendors[i].id; i++) {
1860 if ((ac->vid & 0xffffff00) == vendors[i].id) {
1861 vendor = vendors[i].name;
1862 break;
1865 if (name == NULL) {
1866 (void) snprintf(nmbuf, sizeof (nmbuf), "0x%04x%04x",
1867 vid1, vid2);
1868 name = nmbuf;
1870 if (vendor == NULL) {
1871 vendor = "Unknown";
1875 * Populate the initial shadow table.
1877 for (int i = 0; i < LAST_SHADOW_REG; i += sizeof (uint16_t)) {
1878 SHADOW(ac, i) = RD(i);
1881 ac->caps = RD(AC97_RESET_REGISTER);
1883 enh_bits = 4;
1884 vol_bits = 6;
1885 flags = 0;
1887 /* detect the bit width of the master volume controls */
1888 WR(AC97_MASTER_VOLUME_REGISTER, 0x20);
1889 if ((RD(AC97_MASTER_VOLUME_REGISTER) & 0x1f) == 0x1f) {
1890 vol_bits = 5;
1894 * AC'97 2.3 spec indicates three possible uses for AUX_OUT
1895 * (aka LNLVL_OUT aka HP_OUT). We have to figure out which one
1896 * is in use.
1898 if (ac->caps & RR_HEADPHONE_SUPPORT) {
1899 /* it looks like it is probably headphones */
1900 if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
1901 /* it is implemented */
1902 ac->flags |= AC97_FLAG_AUX_HP;
1906 /* Read EAR just once. */
1907 ear = RD(AC97_EXTENDED_AUDIO_REGISTER);
1910 * If not a headphone, is it 4CH_OUT (surround?)
1912 if ((!(ac->flags & AC97_FLAG_AUX_HP)) && (ear & EAR_SDAC)) {
1913 if (ac_probe_reg(ac, AC97_EXTENDED_LRS_VOLUME_REGISTER)) {
1914 ac->flags |= AC97_FLAG_AUX_4CH;
1919 * If neither, then maybe its an auxiliary line level output?
1921 if (!(ac->flags & (AC97_FLAG_AUX_HP | AC97_FLAG_AUX_4CH))) {
1922 if (ac_probe_reg(ac, AC97_HEADPHONE_VOLUME_REGISTER)) {
1923 ac->flags |= AC97_FLAG_AUX_LVL;
1928 * How many channels?
1930 ac->nchan = 2;
1931 if (ear & EAR_SDAC) {
1932 ac->nchan += 2;
1934 if (ear & EAR_CDAC) {
1935 ac->nchan++;
1937 if (ear & EAR_LDAC) {
1938 ac->nchan++;
1941 ac->flags |= flags;
1942 (void) snprintf(ac->name, sizeof (ac->name), "%s %s", vendor, name);
1944 (void) snprintf(buf, sizeof (buf), "AC'97 codec: %s", ac->name);
1945 audio_dev_add_info(ac->d, buf);
1947 cmn_err(CE_CONT,
1948 "?%s#%d: AC'97 codec id %s (%x, %d channels, caps %x)\n",
1949 ddi_driver_name(ac->dip), ddi_get_instance(ac->dip),
1950 ac->name, ac->vid, ac->nchan, ac->caps);
1953 * Probe and register all known controls with framework
1955 ac_probeinit_ctrls(ac, vol_bits, enh_bits);
1957 ac_hw_reset(ac);
1958 ac_init_values(ac);
1962 * Init the actual hardware related to a previously allocated instance
1963 * of an AC97 device. This is a legacy function and should not be
1964 * used in new code.
1966 * Return zero on success.
1969 ac97_init(ac97_t *ac, struct audio_dev *d)
1971 /* Make sure we aren't using this with new style ac97_allocate(). */
1972 ASSERT(ac->d == NULL);
1974 /* Save audio framework instance structure */
1975 ac->d = d;
1977 ac97_probe_controls(ac);
1978 ac97_register_controls(ac);
1980 return (0);