sandbox.c 5.0 KB

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