1 /* SPDX-License-Identifier: GPL-2.0 */
3 * HD-audio regmap helpers
6 #ifndef __SOUND_HDA_REGMAP_H
7 #define __SOUND_HDA_REGMAP_H
9 #include <linux/regmap.h>
10 #include <sound/core.h>
11 #include <sound/hdaudio.h>
13 #define AC_AMP_FAKE_MUTE 0x10 /* fake mute bit set to amp verbs */
15 int snd_hdac_regmap_init(struct hdac_device
*codec
);
16 void snd_hdac_regmap_exit(struct hdac_device
*codec
);
17 int snd_hdac_regmap_add_vendor_verb(struct hdac_device
*codec
,
19 int snd_hdac_regmap_read_raw(struct hdac_device
*codec
, unsigned int reg
,
21 int snd_hdac_regmap_read_raw_uncached(struct hdac_device
*codec
,
22 unsigned int reg
, unsigned int *val
);
23 int snd_hdac_regmap_write_raw(struct hdac_device
*codec
, unsigned int reg
,
25 int snd_hdac_regmap_update_raw(struct hdac_device
*codec
, unsigned int reg
,
26 unsigned int mask
, unsigned int val
);
27 int snd_hdac_regmap_update_raw_once(struct hdac_device
*codec
, unsigned int reg
,
28 unsigned int mask
, unsigned int val
);
29 void snd_hdac_regmap_sync(struct hdac_device
*codec
);
32 * snd_hdac_regmap_encode_verb - encode the verb to a pseudo register
36 * Returns an encoded pseudo register.
38 #define snd_hdac_regmap_encode_verb(nid, verb) \
39 (((verb) << 8) | 0x80000 | ((unsigned int)(nid) << 20))
42 * snd_hdac_regmap_encode_amp - encode the AMP verb to a pseudo register
44 * @ch: channel (left = 0, right = 1)
45 * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
46 * @idx: input index value
48 * Returns an encoded pseudo register.
50 #define snd_hdac_regmap_encode_amp(nid, ch, dir, idx) \
51 (snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) | \
52 ((ch) ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT) | \
53 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
57 * snd_hdac_regmap_encode_amp_stereo - encode a pseudo register for stereo AMPs
59 * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
60 * @idx: input index value
62 * Returns an encoded pseudo register.
64 #define snd_hdac_regmap_encode_amp_stereo(nid, dir, idx) \
65 (snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) | \
66 AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT | /* both bits set! */ \
67 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
71 * snd_hdac_regmap_write - Write a verb with caching
74 * @val: value to write
76 * For writing an amp value, use snd_hdac_regmap_update_amp().
79 snd_hdac_regmap_write(struct hdac_device
*codec
, hda_nid_t nid
,
80 unsigned int verb
, unsigned int val
)
82 unsigned int cmd
= snd_hdac_regmap_encode_verb(nid
, verb
);
84 return snd_hdac_regmap_write_raw(codec
, cmd
, val
);
88 * snd_hda_regmap_update - Update a verb value with caching
90 * @verb: verb to update
91 * @mask: bit mask to update
92 * @val: value to update
94 * For updating an amp value, use snd_hdac_regmap_update_amp().
97 snd_hdac_regmap_update(struct hdac_device
*codec
, hda_nid_t nid
,
98 unsigned int verb
, unsigned int mask
,
101 unsigned int cmd
= snd_hdac_regmap_encode_verb(nid
, verb
);
103 return snd_hdac_regmap_update_raw(codec
, cmd
, mask
, val
);
107 * snd_hda_regmap_read - Read a verb with caching
109 * @verb: verb to read
110 * @val: pointer to store the value
112 * For reading an amp value, use snd_hda_regmap_get_amp().
115 snd_hdac_regmap_read(struct hdac_device
*codec
, hda_nid_t nid
,
116 unsigned int verb
, unsigned int *val
)
118 unsigned int cmd
= snd_hdac_regmap_encode_verb(nid
, verb
);
120 return snd_hdac_regmap_read_raw(codec
, cmd
, val
);
124 * snd_hdac_regmap_get_amp - Read AMP value
125 * @codec: HD-audio codec
126 * @nid: NID to read the AMP value
127 * @ch: channel (left=0 or right=1)
128 * @direction: #HDA_INPUT or #HDA_OUTPUT
129 * @index: the index value (only for input direction)
130 * @val: the pointer to store the value
132 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
133 * Returns the value or a negative error.
136 snd_hdac_regmap_get_amp(struct hdac_device
*codec
, hda_nid_t nid
,
137 int ch
, int dir
, int idx
)
139 unsigned int cmd
= snd_hdac_regmap_encode_amp(nid
, ch
, dir
, idx
);
142 err
= snd_hdac_regmap_read_raw(codec
, cmd
, &val
);
143 return err
< 0 ? err
: val
;
147 * snd_hdac_regmap_update_amp - update the AMP value
148 * @codec: HD-audio codec
149 * @nid: NID to read the AMP value
150 * @ch: channel (left=0 or right=1)
151 * @direction: #HDA_INPUT or #HDA_OUTPUT
152 * @idx: the index value (only for input direction)
153 * @mask: bit mask to set
154 * @val: the bits value to set
156 * Update the AMP value with a bit mask.
157 * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
160 snd_hdac_regmap_update_amp(struct hdac_device
*codec
, hda_nid_t nid
,
161 int ch
, int dir
, int idx
, int mask
, int val
)
163 unsigned int cmd
= snd_hdac_regmap_encode_amp(nid
, ch
, dir
, idx
);
165 return snd_hdac_regmap_update_raw(codec
, cmd
, mask
, val
);
169 * snd_hdac_regmap_get_amp_stereo - Read stereo AMP values
170 * @codec: HD-audio codec
171 * @nid: NID to read the AMP value
172 * @ch: channel (left=0 or right=1)
173 * @direction: #HDA_INPUT or #HDA_OUTPUT
174 * @index: the index value (only for input direction)
175 * @val: the pointer to store the value
177 * Read stereo AMP values. The lower byte is left, the upper byte is right.
178 * Returns the value or a negative error.
181 snd_hdac_regmap_get_amp_stereo(struct hdac_device
*codec
, hda_nid_t nid
,
184 unsigned int cmd
= snd_hdac_regmap_encode_amp_stereo(nid
, dir
, idx
);
187 err
= snd_hdac_regmap_read_raw(codec
, cmd
, &val
);
188 return err
< 0 ? err
: val
;
192 * snd_hdac_regmap_update_amp_stereo - update the stereo AMP value
193 * @codec: HD-audio codec
194 * @nid: NID to read the AMP value
195 * @direction: #HDA_INPUT or #HDA_OUTPUT
196 * @idx: the index value (only for input direction)
197 * @mask: bit mask to set
198 * @val: the bits value to set
200 * Update the stereo AMP value with a bit mask.
201 * The lower byte is left, the upper byte is right.
202 * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
205 snd_hdac_regmap_update_amp_stereo(struct hdac_device
*codec
, hda_nid_t nid
,
206 int dir
, int idx
, int mask
, int val
)
208 unsigned int cmd
= snd_hdac_regmap_encode_amp_stereo(nid
, dir
, idx
);
210 return snd_hdac_regmap_update_raw(codec
, cmd
, mask
, val
);
214 * snd_hdac_regmap_sync_node - sync the widget node attributes
215 * @codec: HD-audio codec
219 snd_hdac_regmap_sync_node(struct hdac_device
*codec
, hda_nid_t nid
)
221 regcache_mark_dirty(codec
->regmap
);
222 regcache_sync_region(codec
->regmap
, nid
<< 20, ((nid
+ 1) << 20) - 1);
225 #endif /* __SOUND_HDA_REGMAP_H */