xref: /openbmc/linux/include/sound/hda_regmap.h (revision fa0a497b)
1 /*
2  * HD-audio regmap helpers
3  */
4 
5 #ifndef __SOUND_HDA_REGMAP_H
6 #define __SOUND_HDA_REGMAP_H
7 
8 #include <linux/regmap.h>
9 #include <sound/core.h>
10 #include <sound/hdaudio.h>
11 
12 #define AC_AMP_FAKE_MUTE	0x10	/* fake mute bit set to amp verbs */
13 
14 int snd_hdac_regmap_init(struct hdac_device *codec);
15 void snd_hdac_regmap_exit(struct hdac_device *codec);
16 int snd_hdac_regmap_add_vendor_verb(struct hdac_device *codec,
17 				    unsigned int verb);
18 int snd_hdac_regmap_read_raw(struct hdac_device *codec, unsigned int reg,
19 			     unsigned int *val);
20 int snd_hdac_regmap_read_raw_uncached(struct hdac_device *codec,
21 				      unsigned int reg, unsigned int *val);
22 int snd_hdac_regmap_write_raw(struct hdac_device *codec, unsigned int reg,
23 			      unsigned int val);
24 int snd_hdac_regmap_update_raw(struct hdac_device *codec, unsigned int reg,
25 			       unsigned int mask, unsigned int val);
26 
27 /**
28  * snd_hdac_regmap_encode_verb - encode the verb to a pseudo register
29  * @nid: widget NID
30  * @verb: codec verb
31  *
32  * Returns an encoded pseudo register.
33  */
34 #define snd_hdac_regmap_encode_verb(nid, verb)		\
35 	(((verb) << 8) | 0x80000 | ((unsigned int)(nid) << 20))
36 
37 /**
38  * snd_hdac_regmap_encode_amp - encode the AMP verb to a pseudo register
39  * @nid: widget NID
40  * @ch: channel (left = 0, right = 1)
41  * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
42  * @idx: input index value
43  *
44  * Returns an encoded pseudo register.
45  */
46 #define snd_hdac_regmap_encode_amp(nid, ch, dir, idx)			\
47 	(snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) |	\
48 	 ((ch) ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT) |			\
49 	 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
50 	 (idx))
51 
52 /**
53  * snd_hdac_regmap_encode_amp_stereo - encode a pseudo register for stereo AMPs
54  * @nid: widget NID
55  * @dir: direction (#HDA_INPUT, #HDA_OUTPUT)
56  * @idx: input index value
57  *
58  * Returns an encoded pseudo register.
59  */
60 #define snd_hdac_regmap_encode_amp_stereo(nid, dir, idx)		\
61 	(snd_hdac_regmap_encode_verb(nid, AC_VERB_GET_AMP_GAIN_MUTE) |	\
62 	 AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT | /* both bits set! */	\
63 	 ((dir) == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT) | \
64 	 (idx))
65 
66 /**
67  * snd_hdac_regmap_write - Write a verb with caching
68  * @nid: codec NID
69  * @reg: verb to write
70  * @val: value to write
71  *
72  * For writing an amp value, use snd_hdac_regmap_update_amp().
73  */
74 static inline int
75 snd_hdac_regmap_write(struct hdac_device *codec, hda_nid_t nid,
76 		      unsigned int verb, unsigned int val)
77 {
78 	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
79 
80 	return snd_hdac_regmap_write_raw(codec, cmd, val);
81 }
82 
83 /**
84  * snd_hda_regmap_update - Update a verb value with caching
85  * @nid: codec NID
86  * @verb: verb to update
87  * @mask: bit mask to update
88  * @val: value to update
89  *
90  * For updating an amp value, use snd_hdac_regmap_update_amp().
91  */
92 static inline int
93 snd_hdac_regmap_update(struct hdac_device *codec, hda_nid_t nid,
94 		       unsigned int verb, unsigned int mask,
95 		       unsigned int val)
96 {
97 	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
98 
99 	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
100 }
101 
102 /**
103  * snd_hda_regmap_read - Read a verb with caching
104  * @nid: codec NID
105  * @verb: verb to read
106  * @val: pointer to store the value
107  *
108  * For reading an amp value, use snd_hda_regmap_get_amp().
109  */
110 static inline int
111 snd_hdac_regmap_read(struct hdac_device *codec, hda_nid_t nid,
112 		     unsigned int verb, unsigned int *val)
113 {
114 	unsigned int cmd = snd_hdac_regmap_encode_verb(nid, verb);
115 
116 	return snd_hdac_regmap_read_raw(codec, cmd, val);
117 }
118 
119 /**
120  * snd_hdac_regmap_get_amp - Read AMP value
121  * @codec: HD-audio codec
122  * @nid: NID to read the AMP value
123  * @ch: channel (left=0 or right=1)
124  * @direction: #HDA_INPUT or #HDA_OUTPUT
125  * @index: the index value (only for input direction)
126  * @val: the pointer to store the value
127  *
128  * Read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
129  * Returns the value or a negative error.
130  */
131 static inline int
132 snd_hdac_regmap_get_amp(struct hdac_device *codec, hda_nid_t nid,
133 			int ch, int dir, int idx)
134 {
135 	unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
136 	int err, val;
137 
138 	err = snd_hdac_regmap_read_raw(codec, cmd, &val);
139 	return err < 0 ? err : val;
140 }
141 
142 /**
143  * snd_hdac_regmap_update_amp - update the AMP value
144  * @codec: HD-audio codec
145  * @nid: NID to read the AMP value
146  * @ch: channel (left=0 or right=1)
147  * @direction: #HDA_INPUT or #HDA_OUTPUT
148  * @idx: the index value (only for input direction)
149  * @mask: bit mask to set
150  * @val: the bits value to set
151  *
152  * Update the AMP value with a bit mask.
153  * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
154  */
155 static inline int
156 snd_hdac_regmap_update_amp(struct hdac_device *codec, hda_nid_t nid,
157 			   int ch, int dir, int idx, int mask, int val)
158 {
159 	unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
160 
161 	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
162 }
163 
164 /**
165  * snd_hdac_regmap_get_amp_stereo - Read stereo AMP values
166  * @codec: HD-audio codec
167  * @nid: NID to read the AMP value
168  * @ch: channel (left=0 or right=1)
169  * @direction: #HDA_INPUT or #HDA_OUTPUT
170  * @index: the index value (only for input direction)
171  * @val: the pointer to store the value
172  *
173  * Read stereo AMP values.  The lower byte is left, the upper byte is right.
174  * Returns the value or a negative error.
175  */
176 static inline int
177 snd_hdac_regmap_get_amp_stereo(struct hdac_device *codec, hda_nid_t nid,
178 			       int dir, int idx)
179 {
180 	unsigned int cmd = snd_hdac_regmap_encode_amp_stereo(nid, dir, idx);
181 	int err, val;
182 
183 	err = snd_hdac_regmap_read_raw(codec, cmd, &val);
184 	return err < 0 ? err : val;
185 }
186 
187 /**
188  * snd_hdac_regmap_update_amp_stereo - update the stereo AMP value
189  * @codec: HD-audio codec
190  * @nid: NID to read the AMP value
191  * @direction: #HDA_INPUT or #HDA_OUTPUT
192  * @idx: the index value (only for input direction)
193  * @mask: bit mask to set
194  * @val: the bits value to set
195  *
196  * Update the stereo AMP value with a bit mask.
197  * The lower byte is left, the upper byte is right.
198  * Returns 0 if the value is unchanged, 1 if changed, or a negative error.
199  */
200 static inline int
201 snd_hdac_regmap_update_amp_stereo(struct hdac_device *codec, hda_nid_t nid,
202 				  int dir, int idx, int mask, int val)
203 {
204 	unsigned int cmd = snd_hdac_regmap_encode_amp_stereo(nid, dir, idx);
205 
206 	return snd_hdac_regmap_update_raw(codec, cmd, mask, val);
207 }
208 
209 /**
210  * snd_hdac_regmap_sync_node - sync the widget node attributes
211  * @codec: HD-audio codec
212  * @nid: NID to sync
213  */
214 static inline void
215 snd_hdac_regmap_sync_node(struct hdac_device *codec, hda_nid_t nid)
216 {
217 	regcache_mark_dirty(codec->regmap);
218 	regcache_sync_region(codec->regmap, nid << 20, ((nid + 1) << 20) - 1);
219 }
220 
221 #endif /* __SOUND_HDA_REGMAP_H */
222