1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include <zircon/device/audio.h>
8#include <stdint.h>
9
10namespace audio {
11namespace intel_hda {
12
13/* Bitfield definitions for the PCM Size/Rate property.  See section 7.3.4.7 */
14static constexpr uint32_t IHDA_PCM_SIZE_32BITS    = (1u << 20); // 32-bit PCM samples supported
15static constexpr uint32_t IHDA_PCM_SIZE_24BITS    = (1u << 19); // 24-bit PCM samples supported
16static constexpr uint32_t IHDA_PCM_SIZE_20BITS    = (1u << 18); // 20-bit PCM samples supported
17static constexpr uint32_t IHDA_PCM_SIZE_16BITS    = (1u << 17); // 16-bit PCM samples supported
18static constexpr uint32_t IHDA_PCM_SIZE_8BITS     = (1u << 16); // 8-bit PCM samples supported
19
20static constexpr uint32_t IHDA_PCM_RATE_384000    = (1u << 11); // 384000 Hz
21static constexpr uint32_t IHDA_PCM_RATE_192000    = (1u << 10); // 192000 Hz
22static constexpr uint32_t IHDA_PCM_RATE_176400    = (1u <<  9); // 176400 Hz
23static constexpr uint32_t IHDA_PCM_RATE_96000     = (1u <<  8); // 96000 Hz
24static constexpr uint32_t IHDA_PCM_RATE_88200     = (1u <<  7); // 88200 Hz
25static constexpr uint32_t IHDA_PCM_RATE_48000     = (1u <<  6); // 48000 Hz
26static constexpr uint32_t IHDA_PCM_RATE_44100     = (1u <<  5); // 44100 Hz
27static constexpr uint32_t IHDA_PCM_RATE_32000     = (1u <<  4); // 32000 Hz
28static constexpr uint32_t IHDA_PCM_RATE_22050     = (1u <<  3); // 22050 Hz
29static constexpr uint32_t IHDA_PCM_RATE_16000     = (1u <<  2); // 16000 Hz
30static constexpr uint32_t IHDA_PCM_RATE_11025     = (1u <<  1); // 11025 Hz
31static constexpr uint32_t IHDA_PCM_RATE_8000      = (1u <<  0); // 8000 Hz
32
33/* Bitfield definitions for the PCM Formats property.  See section 7.3.4.8 */
34static constexpr uint32_t IHDA_PCM_FORMAT_AC3     = (1u <<  2); // Dolby Digital AC-3 / ATSC A.52
35static constexpr uint32_t IHDA_PCM_FORMAT_FLOAT32 = (1u <<  1); // 32-bit float
36static constexpr uint32_t IHDA_PCM_FORMAT_PCM     = (1u <<  0); // PCM
37
38/* Bitfield definitions for Supported Power States.  See section 7.3.4.12 */
39static constexpr uint32_t IHDA_PWR_STATE_EPSS     = (1u << 31);
40static constexpr uint32_t IHDA_PWR_STATE_CLKSTOP  = (1u << 30);
41static constexpr uint32_t IHDA_PWR_STATE_S3D3COLD = (1u << 29);
42static constexpr uint32_t IHDA_PWR_STATE_D3COLD   = (1u <<  4);
43static constexpr uint32_t IHDA_PWR_STATE_D3       = (1u <<  3);
44static constexpr uint32_t IHDA_PWR_STATE_D2       = (1u <<  2);
45static constexpr uint32_t IHDA_PWR_STATE_D1       = (1u <<  1);
46static constexpr uint32_t IHDA_PWR_STATE_D0       = (1u <<  0);
47
48/* Defined pin capability flags.  See section 7.3.4.9 and Fig. 90 */
49static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_IMPEDANCE_SENSE  = (1u << 0);
50static constexpr uint32_t AW_PIN_CAPS_FLAG_TRIGGER_REQUIRED     = (1u << 1);
51static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_PRESENCE_DETECT  = (1u << 2);
52static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_DRIVE_HEADPHONES = (1u << 3);
53static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_OUTPUT           = (1u << 4);
54static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_INPUT            = (1u << 5);
55static constexpr uint32_t AW_PIN_CAPS_FLAG_BALANCED_IO          = (1u << 6);
56static constexpr uint32_t AW_PIN_CAPS_FLAG_HDMI                 = (1u << 7);
57static constexpr uint32_t AW_PIN_CAPS_FLAG_VREF_HIZ             = (1u << 8);
58static constexpr uint32_t AW_PIN_CAPS_FLAG_VREF_50_PERCENT      = (1u << 9);
59static constexpr uint32_t AW_PIN_CAPS_FLAG_VREF_GROUND          = (1u << 10);
60static constexpr uint32_t AW_PIN_CAPS_FLAG_VREF_80_PERCENT      = (1u << 12);
61static constexpr uint32_t AW_PIN_CAPS_FLAG_VREF_100_PERCENT     = (1u << 13);
62static constexpr uint32_t AW_PIN_CAPS_FLAG_CAN_EAPD             = (1u << 16);
63static constexpr uint32_t AW_PIN_CAPS_FLAG_DISPLAY_PORT         = (1u << 24);
64static constexpr uint32_t AW_PIN_CAPS_FLAG_HIGH_BIT_RATE        = (1u << 27);
65
66// Section 7.3.4.5 : AFG Caps
67// Note: delays are expressed in audio frames.  If a path delay value is 0,
68// the delay should be computed by summing the delays of the widget chain
69// used to create either the input or output paths.
70struct AudioFunctionGroupCaps {
71    static constexpr uint32_t FLAG_HAS_BEEP_GEN = (1u << 16);
72
73    AudioFunctionGroupCaps() { }
74    explicit AudioFunctionGroupCaps(uint32_t raw_data) : raw_data_(raw_data) { }
75
76    bool     has_beep_gen()      const { return (raw_data_ & FLAG_HAS_BEEP_GEN) != 0; }
77    uint8_t  path_input_delay()  const { return static_cast<uint8_t>((raw_data_ >> 8) & 0xF); }
78    uint8_t  path_output_delay() const { return static_cast<uint8_t>(raw_data_ & 0xF); }
79
80    uint32_t raw_data_ = 0;
81};
82
83struct AudioWidgetCaps {
84    /* Defined audio widget types.  See Table 138 */
85    enum class Type : uint8_t {
86        OUTPUT      = 0x0,
87        INPUT       = 0x1,
88        MIXER       = 0x2,
89        SELECTOR    = 0x3,
90        PIN_COMPLEX = 0x4,
91        POWER       = 0x5,
92        VOLUME_KNOB = 0x6,
93        BEEP_GEN    = 0x7,
94        VENDOR      = 0xf,
95    };
96
97    static constexpr uint32_t FLAG_INPUT_AMP_PRESENT   = (1u << 1);
98    static constexpr uint32_t FLAG_OUTPUT_AMP_PRESENT  = (1u << 2);
99    static constexpr uint32_t FLAG_AMP_PARAM_OVERRIDE  = (1u << 3);
100    static constexpr uint32_t FLAG_FORMAT_OVERRIDE     = (1u << 4);
101    static constexpr uint32_t FLAG_STRIPE_SUPPORTED    = (1u << 5);
102    static constexpr uint32_t FLAG_PROC_WIDGET         = (1u << 6);
103    static constexpr uint32_t FLAG_CAN_SEND_UNSOL      = (1u << 7);
104    static constexpr uint32_t FLAG_HAS_CONN_LIST       = (1u << 8);
105    static constexpr uint32_t FLAG_DIGITAL             = (1u << 9);
106    static constexpr uint32_t FLAG_HAS_POWER_CTL       = (1u << 10);
107    static constexpr uint32_t FLAG_CAN_LR_SWAP         = (1u << 11);
108    static constexpr uint32_t FLAG_HAS_CONTENT_PROT    = (1u << 12);
109
110    AudioWidgetCaps() { }
111    explicit AudioWidgetCaps(uint32_t raw_data) : raw_data_(raw_data) { }
112
113    /* Raw data format documented in section 7.3.4.6 */
114    Type type()        const { return static_cast<Type>((raw_data_ >> 20) & 0xF); }
115    uint8_t delay()    const { return static_cast<uint8_t>((raw_data_ >> 16) & 0xF); }
116    uint8_t ch_count() const { return static_cast<uint8_t>((((raw_data_ >> 12) & 0xE) |
117                                                             (raw_data_ & 0x1)) + 1); }
118
119    bool input_amp_present()  const { return (raw_data_ & FLAG_INPUT_AMP_PRESENT)  != 0; }
120    bool output_amp_present() const { return (raw_data_ & FLAG_OUTPUT_AMP_PRESENT) != 0; }
121    bool amp_param_override() const { return (raw_data_ & FLAG_AMP_PARAM_OVERRIDE) != 0; }
122    bool format_override()    const { return (raw_data_ & FLAG_FORMAT_OVERRIDE)    != 0; }
123    bool stripe_supported()   const { return (raw_data_ & FLAG_STRIPE_SUPPORTED)   != 0; }
124    bool proc_widget()        const { return (raw_data_ & FLAG_PROC_WIDGET)        != 0; }
125    bool can_send_unsol()     const { return (raw_data_ & FLAG_CAN_SEND_UNSOL)     != 0; }
126    bool has_conn_list()      const { return (raw_data_ & FLAG_HAS_CONN_LIST)      != 0; }
127    bool digital()            const { return (raw_data_ & FLAG_DIGITAL)            != 0; }
128    bool has_power_ctl()      const { return (raw_data_ & FLAG_HAS_POWER_CTL)      != 0; }
129    bool can_lr_swap()        const { return (raw_data_ & FLAG_CAN_LR_SWAP)        != 0; }
130    bool has_content_prot()   const { return (raw_data_ & FLAG_HAS_CONTENT_PROT)   != 0; }
131
132    uint32_t raw_data_ = 0;
133};
134
135struct SampleCaps {
136    // Bit packing documented in Sections 7.3.4.7 (size/rate) and 7.3.4.8 (format)
137    SampleCaps() { }
138    SampleCaps(uint32_t size_rate, uint32_t formats)
139        : pcm_size_rate_(size_rate),
140          pcm_formats_(formats) { }
141
142    bool SupportsRate(uint32_t rate) const;
143    bool SupportsFormat(audio_sample_format_t sample_format) const;
144
145    uint32_t pcm_size_rate_ = 0;
146    uint32_t pcm_formats_ = 0;
147};
148
149struct AmpCaps {
150    // Bit packing documented in Section 7.3.4.10
151    AmpCaps() { }
152    explicit AmpCaps(uint32_t raw_data) : raw_data_(raw_data) { }
153    uint32_t raw_data_ = 0;
154
155    bool     can_mute()     const { return (raw_data_ & 0x80000000) != 0; }
156    uint32_t step_size()    const { return ((raw_data_ >> 16) & 0x7f) + 1; }
157    uint32_t num_steps()    const { return ((raw_data_ >>  8) & 0x7f) + 1; }
158    uint32_t offset()       const { return ((raw_data_ >>  0) & 0x7f); }
159
160    float    step_size_db() const { return 0.25f * static_cast<float>(step_size()); }
161    float    min_gain_db()  const { return -step_size_db() * static_cast<float>(offset()); }
162    float    max_gain_db()  const { return min_gain_db() + (step_size_db() *
163                                    static_cast<float>((num_steps() - 1))); }
164};
165
166struct PinCaps {
167    // Bit packing documented in Section 7.3.4.10
168    PinCaps() { }
169    explicit PinCaps(uint32_t raw_data) : raw_data_(raw_data) { }
170
171    bool can_imp_sense()        const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_IMPEDANCE_SENSE; }
172    bool trig_required()        const { return raw_data_ & AW_PIN_CAPS_FLAG_TRIGGER_REQUIRED; }
173    bool can_pres_detect()      const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_PRESENCE_DETECT; }
174    bool can_drive_headphones() const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_DRIVE_HEADPHONES; }
175    bool can_output()           const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_OUTPUT; }
176    bool can_input()            const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_INPUT; }
177    bool balanced_io()          const { return raw_data_ & AW_PIN_CAPS_FLAG_BALANCED_IO; }
178    bool is_hdmi()              const { return raw_data_ & AW_PIN_CAPS_FLAG_HDMI; }
179    bool vref_hi_z()            const { return raw_data_ & AW_PIN_CAPS_FLAG_VREF_HIZ; }
180    bool vref_50()              const { return raw_data_ & AW_PIN_CAPS_FLAG_VREF_50_PERCENT; }
181    bool vref_gnd()             const { return raw_data_ & AW_PIN_CAPS_FLAG_VREF_GROUND; }
182    bool vref_80()              const { return raw_data_ & AW_PIN_CAPS_FLAG_VREF_80_PERCENT; }
183    bool vref_100()             const { return raw_data_ & AW_PIN_CAPS_FLAG_VREF_100_PERCENT; }
184    bool has_eapd()             const { return raw_data_ & AW_PIN_CAPS_FLAG_CAN_EAPD; }
185    bool is_display_port()      const { return raw_data_ & AW_PIN_CAPS_FLAG_DISPLAY_PORT; }
186    bool hdmi_hbr()             const { return raw_data_ & AW_PIN_CAPS_FLAG_HIGH_BIT_RATE; }
187
188    uint32_t raw_data_ = 0;
189};
190
191
192struct ConfigDefaults {
193    // Bit packing documented in Section 7.3.3.31.  Present only in pin complexes
194    uint8_t port_connectivity() const { return static_cast<uint8_t>((raw_data_ >> 30) & 0x03); }
195    uint8_t location()          const { return static_cast<uint8_t>((raw_data_ >> 24) & 0x3F); }
196    uint8_t default_device()    const { return static_cast<uint8_t>((raw_data_ >> 20) & 0x0F); }
197    uint8_t connection_type()   const { return static_cast<uint8_t>((raw_data_ >> 16) & 0x0F); }
198    uint8_t color()             const { return static_cast<uint8_t>((raw_data_ >> 12) & 0x0F); }
199    uint8_t misc()              const { return static_cast<uint8_t>((raw_data_ >>  8) & 0x0F); }
200    uint8_t default_assoc()     const { return static_cast<uint8_t>((raw_data_ >>  4) & 0x0F); }
201    uint8_t sequence()          const { return static_cast<uint8_t>((raw_data_ >>  0) & 0x0F); }
202    bool jack_detect_override() const { return (misc() & 0x01) != 0; }
203
204    uint32_t raw_data_;
205};
206
207}  // namespace audio
208}  // namespace intel_hda
209