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