sandbox.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. */
  5. #define LOG_CATEGORY UCLASS_SOUND
  6. #include <common.h>
  7. #include <audio_codec.h>
  8. #include <dm.h>
  9. #include <i2s.h>
  10. #include <sound.h>
  11. #include <asm/sdl.h>
  12. struct sandbox_codec_priv {
  13. int interface;
  14. int rate;
  15. int mclk_freq;
  16. int bits_per_sample;
  17. uint channels;
  18. };
  19. struct sandbox_i2s_priv {
  20. int sum; /* Use to sum the provided audio data */
  21. bool silent; /* Sound is silent, don't use SDL */
  22. };
  23. struct sandbox_sound_priv {
  24. int setup_called; /* Incremented when setup() method is called */
  25. bool active; /* TX data is being sent */
  26. int sum; /* Use to sum the provided audio data */
  27. bool allow_beep; /* true to allow the start_beep() interface */
  28. int frequency_hz; /* Beep frequency if active, else 0 */
  29. };
  30. void sandbox_get_codec_params(struct udevice *dev, int *interfacep, int *ratep,
  31. int *mclk_freqp, int *bits_per_samplep,
  32. uint *channelsp)
  33. {
  34. struct sandbox_codec_priv *priv = dev_get_priv(dev);
  35. *interfacep = priv->interface;
  36. *ratep = priv->rate;
  37. *mclk_freqp = priv->mclk_freq;
  38. *bits_per_samplep = priv->bits_per_sample;
  39. *channelsp = priv->channels;
  40. }
  41. int sandbox_get_i2s_sum(struct udevice *dev)
  42. {
  43. struct sandbox_i2s_priv *priv = dev_get_priv(dev);
  44. return priv->sum;
  45. }
  46. int sandbox_get_setup_called(struct udevice *dev)
  47. {
  48. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  49. return priv->setup_called;
  50. }
  51. int sandbox_get_sound_active(struct udevice *dev)
  52. {
  53. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  54. return priv->active;
  55. }
  56. int sandbox_get_sound_sum(struct udevice *dev)
  57. {
  58. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  59. return priv->sum;
  60. }
  61. void sandbox_set_allow_beep(struct udevice *dev, bool allow)
  62. {
  63. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  64. priv->allow_beep = allow;
  65. }
  66. int sandbox_get_beep_frequency(struct udevice *dev)
  67. {
  68. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  69. return priv->frequency_hz;
  70. }
  71. static int sandbox_codec_set_params(struct udevice *dev, int interface,
  72. int rate, int mclk_freq,
  73. int bits_per_sample, uint channels)
  74. {
  75. struct sandbox_codec_priv *priv = dev_get_priv(dev);
  76. priv->interface = interface;
  77. priv->rate = rate;
  78. priv->mclk_freq = mclk_freq;
  79. priv->bits_per_sample = bits_per_sample;
  80. priv->channels = channels;
  81. return 0;
  82. }
  83. static int sandbox_i2s_tx_data(struct udevice *dev, void *data,
  84. uint data_size)
  85. {
  86. struct sandbox_i2s_priv *priv = dev_get_priv(dev);
  87. int i;
  88. for (i = 0; i < data_size; i++)
  89. priv->sum += ((uint8_t *)data)[i];
  90. if (!priv->silent) {
  91. int ret;
  92. ret = sandbox_sdl_sound_play(data, data_size);
  93. if (ret)
  94. return ret;
  95. }
  96. return 0;
  97. }
  98. static int sandbox_i2s_probe(struct udevice *dev)
  99. {
  100. struct i2s_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  101. struct sandbox_i2s_priv *priv = dev_get_priv(dev);
  102. /* Use hard-coded values here */
  103. uc_priv->rfs = 256;
  104. uc_priv->bfs = 32;
  105. uc_priv->audio_pll_clk = 192000000;
  106. uc_priv->samplingrate = 48000;
  107. uc_priv->bitspersample = 16;
  108. uc_priv->channels = 2;
  109. uc_priv->id = 1;
  110. priv->silent = dev_read_bool(dev, "sandbox,silent");
  111. if (priv->silent) {
  112. log_warning("Sound is silenced\n");
  113. } else if (sandbox_sdl_sound_init(uc_priv->samplingrate,
  114. uc_priv->channels)) {
  115. /* Ignore any error here - we'll just have no sound */
  116. priv->silent = true;
  117. }
  118. return 0;
  119. }
  120. static int sandbox_sound_setup(struct udevice *dev)
  121. {
  122. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  123. priv->setup_called++;
  124. return 0;
  125. }
  126. static int sandbox_sound_play(struct udevice *dev, void *data, uint data_size)
  127. {
  128. struct sound_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  129. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  130. int i;
  131. for (i = 0; i < data_size; i++)
  132. priv->sum += ((uint8_t *)data)[i];
  133. return i2s_tx_data(uc_priv->i2s, data, data_size);
  134. }
  135. static int sandbox_sound_stop_play(struct udevice *dev)
  136. {
  137. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  138. sandbox_sdl_sound_stop();
  139. priv->active = false;
  140. return 0;
  141. }
  142. int sandbox_sound_start_beep(struct udevice *dev, int frequency_hz)
  143. {
  144. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  145. if (!priv->allow_beep)
  146. return -ENOSYS;
  147. priv->frequency_hz = frequency_hz;
  148. return 0;
  149. }
  150. int sandbox_sound_stop_beep(struct udevice *dev)
  151. {
  152. struct sandbox_sound_priv *priv = dev_get_priv(dev);
  153. if (!priv->allow_beep)
  154. return -ENOSYS;
  155. priv->frequency_hz = 0;
  156. return 0;
  157. }
  158. static int sandbox_sound_probe(struct udevice *dev)
  159. {
  160. return sound_find_codec_i2s(dev);
  161. }
  162. static const struct audio_codec_ops sandbox_codec_ops = {
  163. .set_params = sandbox_codec_set_params,
  164. };
  165. static const struct udevice_id sandbox_codec_ids[] = {
  166. { .compatible = "sandbox,audio-codec" },
  167. { }
  168. };
  169. U_BOOT_DRIVER(sandbox_codec) = {
  170. .name = "sandbox_codec",
  171. .id = UCLASS_AUDIO_CODEC,
  172. .of_match = sandbox_codec_ids,
  173. .ops = &sandbox_codec_ops,
  174. .priv_auto_alloc_size = sizeof(struct sandbox_codec_priv),
  175. };
  176. static const struct i2s_ops sandbox_i2s_ops = {
  177. .tx_data = sandbox_i2s_tx_data,
  178. };
  179. static const struct udevice_id sandbox_i2s_ids[] = {
  180. { .compatible = "sandbox,i2s" },
  181. { }
  182. };
  183. U_BOOT_DRIVER(sandbox_i2s) = {
  184. .name = "sandbox_i2s",
  185. .id = UCLASS_I2S,
  186. .of_match = sandbox_i2s_ids,
  187. .ops = &sandbox_i2s_ops,
  188. .probe = sandbox_i2s_probe,
  189. .priv_auto_alloc_size = sizeof(struct sandbox_i2s_priv),
  190. };
  191. static const struct sound_ops sandbox_sound_ops = {
  192. .setup = sandbox_sound_setup,
  193. .play = sandbox_sound_play,
  194. .stop_play = sandbox_sound_stop_play,
  195. .start_beep = sandbox_sound_start_beep,
  196. .stop_beep = sandbox_sound_stop_beep,
  197. };
  198. static const struct udevice_id sandbox_sound_ids[] = {
  199. { .compatible = "sandbox,sound" },
  200. { }
  201. };
  202. U_BOOT_DRIVER(sandbox_sound) = {
  203. .name = "sandbox_sound",
  204. .id = UCLASS_SOUND,
  205. .of_match = sandbox_sound_ids,
  206. .ops = &sandbox_sound_ops,
  207. .priv_auto_alloc_size = sizeof(struct sandbox_sound_priv),
  208. .probe = sandbox_sound_probe,
  209. };