sandbox.c 5.8 KB

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