hda_regmap.h 6.6 KB

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