codec.txt 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ASoC Codec Driver
  2. =================
  3. The codec driver is generic and hardware independent code that configures the
  4. codec to provide audio capture and playback. It should contain no code that is
  5. specific to the target platform or machine. All platform and machine specific
  6. code should be added to the platform and machine drivers respectively.
  7. Each codec driver must provide the following features:-
  8. 1) Digital audio interface (DAI) description
  9. 2) Digital audio interface configuration
  10. 3) PCM's description
  11. 4) Codec control IO - using I2C, 3 Wire(SPI) or both API's
  12. 5) Mixers and audio controls
  13. 6) Sysclk configuration
  14. 7) Codec audio operations
  15. Optionally, codec drivers can also provide:-
  16. 8) DAPM description.
  17. 9) DAPM event handler.
  18. 10) DAC Digital mute control.
  19. It's probably best to use this guide in conjuction with the existing codec
  20. driver code in sound/soc/codecs/
  21. ASoC Codec driver breakdown
  22. ===========================
  23. 1 - Digital Audio Interface (DAI) description
  24. ---------------------------------------------
  25. The DAI is a digital audio data transfer link between the codec and host SoC
  26. CPU. It typically has data transfer capabilities in both directions
  27. (playback and capture) and can run at a variety of different speeds.
  28. Supported interfaces currently include AC97, I2S and generic PCM style links.
  29. Please read DAI.txt for implementation information.
  30. 2 - Digital Audio Interface (DAI) configuration
  31. -----------------------------------------------
  32. DAI configuration is handled by the codec_pcm_prepare function and is
  33. responsible for configuring and starting the DAI on the codec. This can be
  34. called multiple times and is atomic. It can access the runtime parameters.
  35. This usually consists of a large function with numerous switch statements to
  36. set up each configuration option. These options are set by the core at runtime.
  37. 3 - Codec PCM's
  38. ---------------
  39. Each codec must have it's PCM's defined. This defines the number of channels,
  40. stream names, callbacks and codec name. It is also used to register the DAI
  41. with the ASoC core. The PCM structure also associates the DAI capabilities with
  42. the ALSA PCM.
  43. e.g.
  44. static struct snd_soc_pcm_codec wm8731_pcm_client = {
  45. .name = "WM8731",
  46. .playback = {
  47. .stream_name = "Playback",
  48. .channels_min = 1,
  49. .channels_max = 2,
  50. },
  51. .capture = {
  52. .stream_name = "Capture",
  53. .channels_min = 1,
  54. .channels_max = 2,
  55. },
  56. .config_sysclk = wm8731_config_sysclk,
  57. .ops = {
  58. .prepare = wm8731_pcm_prepare,
  59. },
  60. .caps = {
  61. .num_modes = ARRAY_SIZE(wm8731_hwfmt),
  62. .modes = &wm8731_hwfmt[0],
  63. },
  64. };
  65. 4 - Codec control IO
  66. --------------------
  67. The codec can ususally be controlled via an I2C or SPI style interface (AC97
  68. combines control with data in the DAI). The codec drivers will have to provide
  69. functions to read and write the codec registers along with supplying a register
  70. cache:-
  71. /* IO control data and register cache */
  72. void *control_data; /* codec control (i2c/3wire) data */
  73. void *reg_cache;
  74. Codec read/write should do any data formatting and call the hardware read write
  75. below to perform the IO. These functions are called by the core and alsa when
  76. performing DAPM or changing the mixer:-
  77. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  78. int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
  79. Codec hardware IO functions - usually points to either the I2C, SPI or AC97
  80. read/write:-
  81. hw_write_t hw_write;
  82. hw_read_t hw_read;
  83. 5 - Mixers and audio controls
  84. -----------------------------
  85. All the codec mixers and audio controls can be defined using the convenience
  86. macros defined in soc.h.
  87. #define SOC_SINGLE(xname, reg, shift, mask, invert)
  88. Defines a single control as follows:-
  89. xname = Control name e.g. "Playback Volume"
  90. reg = codec register
  91. shift = control bit(s) offset in register
  92. mask = control bit size(s) e.g. mask of 7 = 3 bits
  93. invert = the control is inverted
  94. Other macros include:-
  95. #define SOC_DOUBLE(xname, reg, shift_left, shift_right, mask, invert)
  96. A stereo control
  97. #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, mask, invert)
  98. A stereo control spanning 2 registers
  99. #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts)
  100. Defines an single enumerated control as follows:-
  101. xreg = register
  102. xshift = control bit(s) offset in register
  103. xmask = control bit(s) size
  104. xtexts = pointer to array of strings that describe each setting
  105. #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts)
  106. Defines a stereo enumerated control
  107. 6 - System clock configuration.
  108. -------------------------------
  109. The system clock that drives the audio subsystem can change depending on sample
  110. rate and the system power state. i.e.
  111. o Higher sample rates sometimes need a higher system clock.
  112. o Low system power states can sometimes limit the available clocks.
  113. This function is a callback that the machine driver can call to set and
  114. determine if the clock and sample rate combination is supported by the codec at
  115. the present time (and system state).
  116. NOTE: If the codec has a PLL then it has a lot more flexability wrt clock and
  117. sample rate combinations.
  118. Your config_sysclock function should return the MCLK if it's a valid
  119. combination for your codec else 0;
  120. Please read clocking.txt now.
  121. 7 - Codec Audio Operations
  122. --------------------------
  123. The codec driver also supports the following alsa operations:-
  124. /* SoC audio ops */
  125. struct snd_soc_ops {
  126. int (*startup)(snd_pcm_substream_t *);
  127. void (*shutdown)(snd_pcm_substream_t *);
  128. int (*hw_params)(snd_pcm_substream_t *, snd_pcm_hw_params_t *);
  129. int (*hw_free)(snd_pcm_substream_t *);
  130. int (*prepare)(snd_pcm_substream_t *);
  131. };
  132. Please refer to the alsa driver PCM documentation for details.
  133. http://www.alsa-project.org/~iwai/writing-an-alsa-driver/c436.htm
  134. 8 - DAPM description.
  135. ---------------------
  136. The Dynamic Audio Power Management description describes the codec's power
  137. components, their relationships and registers to the ASoC core. Please read
  138. dapm.txt for details of building the description.
  139. Please also see the examples in other codec drivers.
  140. 9 - DAPM event handler
  141. ----------------------
  142. This function is a callback that handles codec domain PM calls and system
  143. domain PM calls (e.g. suspend and resume). It's used to put the codec to sleep
  144. when not in use.
  145. Power states:-
  146. SNDRV_CTL_POWER_D0: /* full On */
  147. /* vref/mid, clk and osc on, active */
  148. SNDRV_CTL_POWER_D1: /* partial On */
  149. SNDRV_CTL_POWER_D2: /* partial On */
  150. SNDRV_CTL_POWER_D3hot: /* Off, with power */
  151. /* everything off except vref/vmid, inactive */
  152. SNDRV_CTL_POWER_D3cold: /* Everything Off, without power */
  153. 10 - Codec DAC digital mute control.
  154. ------------------------------------
  155. Most codecs have a digital mute before the DAC's that can be used to minimise
  156. any system noise. The mute stops any digital data from entering the DAC.
  157. A callback can be created that is called by the core for each codec DAI when the
  158. mute is applied or freed.
  159. i.e.
  160. static int wm8974_mute(struct snd_soc_codec *codec,
  161. struct snd_soc_codec_dai *dai, int mute)
  162. {
  163. u16 mute_reg = wm8974_read_reg_cache(codec, WM8974_DAC) & 0xffbf;
  164. if(mute)
  165. wm8974_write(codec, WM8974_DAC, mute_reg | 0x40);
  166. else
  167. wm8974_write(codec, WM8974_DAC, mute_reg);
  168. return 0;
  169. }