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