sound-i2s.c 4.9 KB

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