codec.txt 5.6 KB

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