codec.txt 5.1 KB

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