sound-i2s.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Samsung Electronics
  4. * R. Chandrasekar <rcsekar@samsung.com>
  5. */
  6. #include <malloc.h>
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <linux/libfdt.h>
  10. #include <fdtdec.h>
  11. #include <i2c.h>
  12. #include <i2s.h>
  13. #include <sound.h>
  14. #include <asm/arch/sound.h>
  15. #include "wm8994.h"
  16. #include "max98095.h"
  17. /* defines */
  18. #define SOUND_400_HZ 400
  19. #define SOUND_BITS_IN_BYTE 8
  20. static struct i2s_uc_priv g_i2stx_pri;
  21. /*
  22. * get_sound_i2s_values gets values for i2s parameters
  23. *
  24. * @param i2s_uc_priv i2s transmitter transfer param structure
  25. * @param blob FDT blob if enabled else NULL
  26. */
  27. static int get_sound_i2s_values(struct i2s_uc_priv *i2s, const void *blob)
  28. {
  29. int node;
  30. int error = 0;
  31. int base;
  32. node = fdt_path_offset(blob, "i2s");
  33. if (node <= 0) {
  34. debug("EXYNOS_SOUND: No node for sound in device tree\n");
  35. return -1;
  36. }
  37. /*
  38. * Get the pre-defined sound specific values from FDT.
  39. * All of these are expected to be correct otherwise
  40. * wrong register values in i2s setup parameters
  41. * may result in no sound play.
  42. */
  43. base = fdtdec_get_addr(blob, node, "reg");
  44. if (base == FDT_ADDR_T_NONE) {
  45. debug("%s: Missing i2s base\n", __func__);
  46. return -1;
  47. }
  48. i2s->base_address = base;
  49. i2s->audio_pll_clk = fdtdec_get_int(blob,
  50. node, "samsung,i2s-epll-clock-frequency", -1);
  51. error |= i2s->audio_pll_clk;
  52. debug("audio_pll_clk = %d\n", i2s->audio_pll_clk);
  53. i2s->samplingrate = fdtdec_get_int(blob,
  54. node, "samsung,i2s-sampling-rate", -1);
  55. error |= i2s->samplingrate;
  56. debug("samplingrate = %d\n", i2s->samplingrate);
  57. i2s->bitspersample = fdtdec_get_int(blob,
  58. node, "samsung,i2s-bits-per-sample", -1);
  59. error |= i2s->bitspersample;
  60. debug("bitspersample = %d\n", i2s->bitspersample);
  61. i2s->channels = fdtdec_get_int(blob,
  62. node, "samsung,i2s-channels", -1);
  63. error |= i2s->channels;
  64. debug("channels = %d\n", i2s->channels);
  65. i2s->rfs = fdtdec_get_int(blob,
  66. node, "samsung,i2s-lr-clk-framesize", -1);
  67. error |= i2s->rfs;
  68. debug("rfs = %d\n", i2s->rfs);
  69. i2s->bfs = fdtdec_get_int(blob,
  70. node, "samsung,i2s-bit-clk-framesize", -1);
  71. error |= i2s->bfs;
  72. debug("bfs = %d\n", i2s->bfs);
  73. i2s->id = fdtdec_get_int(blob, node, "samsung,i2s-id", -1);
  74. error |= i2s->id;
  75. debug("id = %d\n", i2s->id);
  76. if (error == -1) {
  77. debug("fail to get sound i2s node properties\n");
  78. return -1;
  79. }
  80. return 0;
  81. }
  82. /*
  83. * Init codec
  84. *
  85. * @param blob FDT blob
  86. * @param pi2s_tx i2s parameters required by codec
  87. * @return int value, 0 for success
  88. */
  89. static int codec_init(const void *blob, struct i2s_uc_priv *pi2s_tx)
  90. {
  91. int ret;
  92. const char *codectype;
  93. int node;
  94. /* Get the node from FDT for sound */
  95. node = fdt_path_offset(blob, "i2s");
  96. if (node <= 0) {
  97. debug("EXYNOS_SOUND: No node for sound in device tree\n");
  98. debug("node = %d\n", node);
  99. return -1;
  100. }
  101. /*
  102. * Get the pre-defined sound codec specific values from FDT.
  103. * All of these are expected to be correct otherwise sound
  104. * can not be played
  105. */
  106. codectype = fdt_getprop(blob, node, "samsung,codec-type", NULL);
  107. debug("device = %s\n", codectype);
  108. if (!strcmp(codectype, "wm8994")) {
  109. /* Check the codec type and initialise the same */
  110. ret = wm8994_init(blob, pi2s_tx->id, pi2s_tx->samplingrate,
  111. (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
  112. pi2s_tx->bitspersample, pi2s_tx->channels);
  113. } else if (!strcmp(codectype, "max98095")) {
  114. ret = max98095_init(blob, pi2s_tx->id, pi2s_tx->samplingrate,
  115. (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
  116. pi2s_tx->bitspersample);
  117. } else {
  118. debug("%s: Unknown codec type %s\n", __func__, codectype);
  119. return -1;
  120. }
  121. if (ret) {
  122. debug("%s: Codec init failed\n", __func__);
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. int sound_init(const void *blob)
  128. {
  129. int ret;
  130. struct i2s_uc_priv *pi2s_tx = &g_i2stx_pri;
  131. /* Get the I2S Values */
  132. if (get_sound_i2s_values(pi2s_tx, blob) < 0) {
  133. debug(" FDT I2S values failed\n");
  134. return -1;
  135. }
  136. if (codec_init(blob, pi2s_tx) < 0) {
  137. debug(" Codec init failed\n");
  138. return -1;
  139. }
  140. ret = i2s_tx_init(pi2s_tx);
  141. if (ret) {
  142. debug("%s: Failed to init i2c transmit: ret=%d\n", __func__,
  143. ret);
  144. return ret;
  145. }
  146. return ret;
  147. }
  148. int sound_play(uint32_t msec, uint32_t frequency)
  149. {
  150. unsigned int *data;
  151. unsigned long data_size;
  152. unsigned int ret = 0;
  153. /*Buffer length computation */
  154. data_size = g_i2stx_pri.samplingrate * g_i2stx_pri.channels;
  155. data_size *= (g_i2stx_pri.bitspersample / SOUND_BITS_IN_BYTE);
  156. data = malloc(data_size);
  157. if (data == NULL) {
  158. debug("%s: malloc failed\n", __func__);
  159. return -1;
  160. }
  161. sound_create_square_wave(g_i2stx_pri.samplingrate,
  162. (unsigned short *)data,
  163. data_size / sizeof(unsigned short),
  164. frequency);
  165. while (msec >= 1000) {
  166. ret = i2s_transfer_tx_data(&g_i2stx_pri, data,
  167. (data_size / sizeof(int)));
  168. msec -= 1000;
  169. }
  170. if (msec) {
  171. unsigned long size =
  172. (data_size * msec) / (sizeof(int) * 1000);
  173. ret = i2s_transfer_tx_data(&g_i2stx_pri, data, size);
  174. }
  175. free(data);
  176. return ret;
  177. }