hda_codec.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. Notes on Universal Interface for Intel High Definition Audio Codec
  2. ------------------------------------------------------------------
  3. Takashi Iwai <tiwai@suse.de>
  4. [Still a draft version]
  5. General
  6. =======
  7. The snd-hda-codec module supports the generic access function for the
  8. High Definition (HD) audio codecs. It's designed to be independent
  9. from the controller code like ac97 codec module. The real accessors
  10. from/to the controller must be implemented in the lowlevel driver.
  11. The structure of this module is similar with ac97_codec module.
  12. Each codec chip belongs to a bus class which communicates with the
  13. controller.
  14. Initialization of Bus Instance
  15. ==============================
  16. The card driver has to create struct hda_bus at first. The template
  17. struct should be filled and passed to the constructor:
  18. struct hda_bus_template {
  19. void *private_data;
  20. struct pci_dev *pci;
  21. const char *modelname;
  22. struct hda_bus_ops ops;
  23. };
  24. The card driver can set and use the private_data field to retrieve its
  25. own data in callback functions. The pci field is used when the patch
  26. needs to check the PCI subsystem IDs, so on. For non-PCI system, it
  27. doesn't have to be set, of course.
  28. The modelname field specifies the board's specific configuration. The
  29. string is passed to the codec parser, and it depends on the parser how
  30. the string is used.
  31. These fields, private_data, pci and modelname are all optional.
  32. The ops field contains the callback functions as the following:
  33. struct hda_bus_ops {
  34. int (*command)(struct hda_codec *codec, hda_nid_t nid, int direct,
  35. unsigned int verb, unsigned int parm);
  36. unsigned int (*get_response)(struct hda_codec *codec);
  37. void (*private_free)(struct hda_bus *);
  38. #ifdef CONFIG_SND_HDA_POWER_SAVE
  39. void (*pm_notify)(struct hda_codec *codec);
  40. #endif
  41. };
  42. The command callback is called when the codec module needs to send a
  43. VERB to the controller. It's always a single command.
  44. The get_response callback is called when the codec requires the answer
  45. for the last command. These two callbacks are mandatory and have to
  46. be given.
  47. The third, private_free callback, is optional. It's called in the
  48. destructor to release any necessary data in the lowlevel driver.
  49. The pm_notify callback is available only with
  50. CONFIG_SND_HDA_POWER_SAVE kconfig. It's called when the codec needs
  51. to power up or may power down. The controller should check the all
  52. belonging codecs on the bus whether they are actually powered off
  53. (check codec->power_on), and optionally the driver may power down the
  54. controller side, too.
  55. The bus instance is created via snd_hda_bus_new(). You need to pass
  56. the card instance, the template, and the pointer to store the
  57. resultant bus instance.
  58. int snd_hda_bus_new(struct snd_card *card, const struct hda_bus_template *temp,
  59. struct hda_bus **busp);
  60. It returns zero if successful. A negative return value means any
  61. error during creation.
  62. Creation of Codec Instance
  63. ==========================
  64. Each codec chip on the board is then created on the BUS instance.
  65. To create a codec instance, call snd_hda_codec_new().
  66. int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
  67. struct hda_codec **codecp);
  68. The first argument is the BUS instance, the second argument is the
  69. address of the codec, and the last one is the pointer to store the
  70. resultant codec instance (can be NULL if not needed).
  71. The codec is stored in a linked list of bus instance. You can follow
  72. the codec list like:
  73. struct hda_codec *codec;
  74. list_for_each_entry(codec, &bus->codec_list, list) {
  75. ...
  76. }
  77. The codec isn't initialized at this stage properly. The
  78. initialization sequence is called when the controls are built later.
  79. Codec Access
  80. ============
  81. To access codec, use snd_hda_codec_read() and snd_hda_codec_write().
  82. snd_hda_param_read() is for reading parameters.
  83. For writing a sequence of verbs, use snd_hda_sequence_write().
  84. There are variants of cached read/write, snd_hda_codec_write_cache(),
  85. snd_hda_sequence_write_cache(). These are used for recording the
  86. register states for the power-management resume. When no PM is needed,
  87. these are equivalent with non-cached version.
  88. To retrieve the number of sub nodes connected to the given node, use
  89. snd_hda_get_sub_nodes(). The connection list can be obtained via
  90. snd_hda_get_connections() call.
  91. When an unsolicited event happens, pass the event via
  92. snd_hda_queue_unsol_event() so that the codec routines will process it
  93. later.
  94. (Mixer) Controls
  95. ================
  96. To create mixer controls of all codecs, call
  97. snd_hda_build_controls(). It then builds the mixers and does
  98. initialization stuff on each codec.
  99. PCM Stuff
  100. =========
  101. snd_hda_build_pcms() gives the necessary information to create PCM
  102. streams. When it's called, each codec belonging to the bus stores
  103. codec->num_pcms and codec->pcm_info fields. The num_pcms indicates
  104. the number of elements in pcm_info array. The card driver is supposed
  105. to traverse the codec linked list, read the pcm information in
  106. pcm_info array, and build pcm instances according to them.
  107. The pcm_info array contains the following record:
  108. /* PCM information for each substream */
  109. struct hda_pcm_stream {
  110. unsigned int substreams; /* number of substreams, 0 = not exist */
  111. unsigned int channels_min; /* min. number of channels */
  112. unsigned int channels_max; /* max. number of channels */
  113. hda_nid_t nid; /* default NID to query rates/formats/bps, or set up */
  114. u32 rates; /* supported rates */
  115. u64 formats; /* supported formats (SNDRV_PCM_FMTBIT_) */
  116. unsigned int maxbps; /* supported max. bit per sample */
  117. struct hda_pcm_ops ops;
  118. };
  119. /* for PCM creation */
  120. struct hda_pcm {
  121. char *name;
  122. struct hda_pcm_stream stream[2];
  123. };
  124. The name can be passed to snd_pcm_new(). The stream field contains
  125. the information for playback (SNDRV_PCM_STREAM_PLAYBACK = 0) and
  126. capture (SNDRV_PCM_STREAM_CAPTURE = 1) directions. The card driver
  127. should pass substreams to snd_pcm_new() for the number of substreams
  128. to create.
  129. The channels_min, channels_max, rates and formats should be copied to
  130. runtime->hw record. They and maxbps fields are used also to compute
  131. the format value for the HDA codec and controller. Call
  132. snd_hda_calc_stream_format() to get the format value.
  133. The ops field contains the following callback functions:
  134. struct hda_pcm_ops {
  135. int (*open)(struct hda_pcm_stream *info, struct hda_codec *codec,
  136. struct snd_pcm_substream *substream);
  137. int (*close)(struct hda_pcm_stream *info, struct hda_codec *codec,
  138. struct snd_pcm_substream *substream);
  139. int (*prepare)(struct hda_pcm_stream *info, struct hda_codec *codec,
  140. unsigned int stream_tag, unsigned int format,
  141. struct snd_pcm_substream *substream);
  142. int (*cleanup)(struct hda_pcm_stream *info, struct hda_codec *codec,
  143. struct snd_pcm_substream *substream);
  144. };
  145. All are non-NULL, so you can call them safely without NULL check.
  146. The open callback should be called in PCM open after runtime->hw is
  147. set up. It may override some setting and constraints additionally.
  148. Similarly, the close callback should be called in the PCM close.
  149. The prepare callback should be called in PCM prepare. This will set
  150. up the codec chip properly for the operation. The cleanup should be
  151. called in hw_free to clean up the configuration.
  152. The caller should check the return value, at least for open and
  153. prepare callbacks. When a negative value is returned, some error
  154. occurred.
  155. Proc Files
  156. ==========
  157. Each codec dumps the widget node information in
  158. /proc/asound/card*/codec#* file. This information would be really
  159. helpful for debugging. Please provide its contents together with the
  160. bug report.
  161. Power Management
  162. ================
  163. It's simple:
  164. Call snd_hda_suspend() in the PM suspend callback.
  165. Call snd_hda_resume() in the PM resume callback.
  166. Codec Preset (Patch)
  167. ====================
  168. To set up and handle the codec functionality fully, each codec may
  169. have a codec preset (patch). It's defined in struct hda_codec_preset:
  170. struct hda_codec_preset {
  171. unsigned int id;
  172. unsigned int mask;
  173. unsigned int subs;
  174. unsigned int subs_mask;
  175. unsigned int rev;
  176. const char *name;
  177. int (*patch)(struct hda_codec *codec);
  178. };
  179. When the codec id and codec subsystem id match with the given id and
  180. subs fields bitwise (with bitmask mask and subs_mask), the callback
  181. patch is called. The patch callback should initialize the codec and
  182. set the codec->patch_ops field. This is defined as below:
  183. struct hda_codec_ops {
  184. int (*build_controls)(struct hda_codec *codec);
  185. int (*build_pcms)(struct hda_codec *codec);
  186. int (*init)(struct hda_codec *codec);
  187. void (*free)(struct hda_codec *codec);
  188. void (*unsol_event)(struct hda_codec *codec, unsigned int res);
  189. #ifdef CONFIG_PM
  190. int (*suspend)(struct hda_codec *codec, pm_message_t state);
  191. int (*resume)(struct hda_codec *codec);
  192. #endif
  193. #ifdef CONFIG_SND_HDA_POWER_SAVE
  194. int (*check_power_status)(struct hda_codec *codec,
  195. hda_nid_t nid);
  196. #endif
  197. };
  198. The build_controls callback is called from snd_hda_build_controls().
  199. Similarly, the build_pcms callback is called from
  200. snd_hda_build_pcms(). The init callback is called after
  201. build_controls to initialize the hardware.
  202. The free callback is called as a destructor.
  203. The unsol_event callback is called when an unsolicited event is
  204. received.
  205. The suspend and resume callbacks are for power management.
  206. They can be NULL if no special sequence is required. When the resume
  207. callback is NULL, the driver calls the init callback and resumes the
  208. registers from the cache. If other handling is needed, you'd need to
  209. write your own resume callback. There, the amp values can be resumed
  210. via
  211. void snd_hda_codec_resume_amp(struct hda_codec *codec);
  212. and the other codec registers via
  213. void snd_hda_codec_resume_cache(struct hda_codec *codec);
  214. The check_power_status callback is called when the amp value of the
  215. given widget NID is changed. The codec code can turn on/off the power
  216. appropriately from this information.
  217. Each entry can be NULL if not necessary to be called.
  218. Generic Parser
  219. ==============
  220. When the device doesn't match with any given presets, the widgets are
  221. parsed via th generic parser (hda_generic.c). Its support is
  222. limited: no multi-channel support, for example.
  223. Digital I/O
  224. ===========
  225. Call snd_hda_create_spdif_out_ctls() from the patch to create controls
  226. related with SPDIF out.
  227. Helper Functions
  228. ================
  229. snd_hda_get_codec_name() stores the codec name on the given string.
  230. snd_hda_check_board_config() can be used to obtain the configuration
  231. information matching with the device. Define the model string table
  232. and the table with struct snd_pci_quirk entries (zero-terminated),
  233. and pass it to the function. The function checks the modelname given
  234. as a module parameter, and PCI subsystem IDs. If the matching entry
  235. is found, it returns the config field value.
  236. snd_hda_add_new_ctls() can be used to create and add control entries.
  237. Pass the zero-terminated array of struct snd_kcontrol_new
  238. Macros HDA_CODEC_VOLUME(), HDA_CODEC_MUTE() and their variables can be
  239. used for the entry of struct snd_kcontrol_new.
  240. The input MUX helper callbacks for such a control are provided, too:
  241. snd_hda_input_mux_info() and snd_hda_input_mux_put(). See
  242. patch_realtek.c for example.