codec.txt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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) Codec DAI and PCM configuration
  9. 2) Codec control IO - using I2C, 3 Wire(SPI) or both APIs
  10. 3) Mixers and audio controls
  11. 4) Codec audio operations
  12. Optionally, codec drivers can also provide:-
  13. 5) DAPM description.
  14. 6) DAPM event handler.
  15. 7) DAC Digital mute control.
  16. Its probably best to use this guide in conjunction with the existing codec
  17. driver code in sound/soc/codecs/
  18. ASoC Codec driver breakdown
  19. ===========================
  20. 1 - Codec DAI and PCM configuration
  21. -----------------------------------
  22. Each codec driver must have a struct snd_soc_codec_dai to define its DAI and
  23. PCM capabilities and operations. This struct is exported so that it can be
  24. registered with the core by your machine driver.
  25. e.g.
  26. struct snd_soc_codec_dai wm8731_dai = {
  27. .name = "WM8731",
  28. /* playback capabilities */
  29. .playback = {
  30. .stream_name = "Playback",
  31. .channels_min = 1,
  32. .channels_max = 2,
  33. .rates = WM8731_RATES,
  34. .formats = WM8731_FORMATS,},
  35. /* capture capabilities */
  36. .capture = {
  37. .stream_name = "Capture",
  38. .channels_min = 1,
  39. .channels_max = 2,
  40. .rates = WM8731_RATES,
  41. .formats = WM8731_FORMATS,},
  42. /* pcm operations - see section 4 below */
  43. .ops = {
  44. .prepare = wm8731_pcm_prepare,
  45. .hw_params = wm8731_hw_params,
  46. .shutdown = wm8731_shutdown,
  47. },
  48. /* DAI operations - see DAI.txt */
  49. .dai_ops = {
  50. .digital_mute = wm8731_mute,
  51. .set_sysclk = wm8731_set_dai_sysclk,
  52. .set_fmt = wm8731_set_dai_fmt,
  53. }
  54. };
  55. EXPORT_SYMBOL_GPL(wm8731_dai);
  56. 2 - Codec control IO
  57. --------------------
  58. The codec can usually be controlled via an I2C or SPI style interface
  59. (AC97 combines control with data in the DAI). The codec drivers provide
  60. functions to read and write the codec registers along with supplying a
  61. register cache:-
  62. /* IO control data and register cache */
  63. void *control_data; /* codec control (i2c/3wire) data */
  64. void *reg_cache;
  65. Codec read/write should do any data formatting and call the hardware
  66. read write below to perform the IO. These functions are called by the
  67. core and ALSA when performing DAPM or changing the mixer:-
  68. unsigned int (*read)(struct snd_soc_codec *, unsigned int);
  69. int (*write)(struct snd_soc_codec *, unsigned int, unsigned int);
  70. Codec hardware IO functions - usually points to either the I2C, SPI or AC97
  71. read/write:-
  72. hw_write_t hw_write;
  73. hw_read_t hw_read;
  74. 3 - Mixers and audio controls
  75. -----------------------------
  76. All the codec mixers and audio controls can be defined using the convenience
  77. macros defined in soc.h.
  78. #define SOC_SINGLE(xname, reg, shift, mask, invert)
  79. Defines a single control as follows:-
  80. xname = Control name e.g. "Playback Volume"
  81. reg = codec register
  82. shift = control bit(s) offset in register
  83. mask = control bit size(s) e.g. mask of 7 = 3 bits
  84. invert = the control is inverted
  85. Other macros include:-
  86. #define SOC_DOUBLE(xname, reg, shift_left, shift_right, mask, invert)
  87. A stereo control
  88. #define SOC_DOUBLE_R(xname, reg_left, reg_right, shift, mask, invert)
  89. A stereo control spanning 2 registers
  90. #define SOC_ENUM_SINGLE(xreg, xshift, xmask, xtexts)
  91. Defines an single enumerated control as follows:-
  92. xreg = register
  93. xshift = control bit(s) offset in register
  94. xmask = control bit(s) size
  95. xtexts = pointer to array of strings that describe each setting
  96. #define SOC_ENUM_DOUBLE(xreg, xshift_l, xshift_r, xmask, xtexts)
  97. Defines a stereo enumerated control
  98. 4 - Codec Audio Operations
  99. --------------------------
  100. The codec driver also supports the following ALSA operations:-
  101. /* SoC audio ops */
  102. struct snd_soc_ops {
  103. int (*startup)(struct snd_pcm_substream *);
  104. void (*shutdown)(struct snd_pcm_substream *);
  105. int (*hw_params)(struct snd_pcm_substream *, struct snd_pcm_hw_params *);
  106. int (*hw_free)(struct snd_pcm_substream *);
  107. int (*prepare)(struct snd_pcm_substream *);
  108. };
  109. Please refer to the ALSA driver PCM documentation for details.
  110. http://www.alsa-project.org/~iwai/writing-an-alsa-driver/c436.htm
  111. 5 - DAPM description.
  112. ---------------------
  113. The Dynamic Audio Power Management description describes the codec power
  114. components and their relationships and registers to the ASoC core.
  115. Please read dapm.txt for details of building the description.
  116. Please also see the examples in other codec drivers.
  117. 6 - DAPM event handler
  118. ----------------------
  119. This function is a callback that handles codec domain PM calls and system
  120. domain PM calls (e.g. suspend and resume). It is used to put the codec
  121. to sleep when not in use.
  122. Power states:-
  123. SNDRV_CTL_POWER_D0: /* full On */
  124. /* vref/mid, clk and osc on, active */
  125. SNDRV_CTL_POWER_D1: /* partial On */
  126. SNDRV_CTL_POWER_D2: /* partial On */
  127. SNDRV_CTL_POWER_D3hot: /* Off, with power */
  128. /* everything off except vref/vmid, inactive */
  129. SNDRV_CTL_POWER_D3cold: /* Everything Off, without power */
  130. 7 - Codec DAC digital mute control
  131. ----------------------------------
  132. Most codecs have a digital mute before the DACs that can be used to
  133. minimise any system noise. The mute stops any digital data from
  134. entering the DAC.
  135. A callback can be created that is called by the core for each codec DAI
  136. when the mute is applied or freed.
  137. i.e.
  138. static int wm8974_mute(struct snd_soc_codec *codec,
  139. struct snd_soc_codec_dai *dai, int mute)
  140. {
  141. u16 mute_reg = wm8974_read_reg_cache(codec, WM8974_DAC) & 0xffbf;
  142. if(mute)
  143. wm8974_write(codec, WM8974_DAC, mute_reg | 0x40);
  144. else
  145. wm8974_write(codec, WM8974_DAC, mute_reg);
  146. return 0;
  147. }