hda_regmap.h 6.6 KB

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