codec.rst 5.2 KB

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