hda_regmap.h 6.8 KB

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