samsung-i2s.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2012 Samsung Electronics
  4. * R. Chandrasekar <rcsekar@samsung.com>
  5. */
  6. #include <asm/arch/clk.h>
  7. #include <asm/arch/pinmux.h>
  8. #include <asm/arch/i2s-regs.h>
  9. #include <asm/io.h>
  10. #include <common.h>
  11. #include <sound.h>
  12. #include <i2s.h>
  13. #define FIC_TX2COUNT(x) (((x) >> 24) & 0xf)
  14. #define FIC_TX1COUNT(x) (((x) >> 16) & 0xf)
  15. #define FIC_TXCOUNT(x) (((x) >> 8) & 0xf)
  16. #define FIC_RXCOUNT(x) (((x) >> 0) & 0xf)
  17. #define FICS_TXCOUNT(x) (((x) >> 8) & 0x7f)
  18. #define TIMEOUT_I2S_TX 100 /* i2s transfer timeout */
  19. /*
  20. * Sets the frame size for I2S LR clock
  21. *
  22. * @param i2s_reg i2s regiter address
  23. * @param rfs Frame Size
  24. */
  25. static void i2s_set_lr_framesize(struct i2s_reg *i2s_reg, unsigned int rfs)
  26. {
  27. unsigned int mod = readl(&i2s_reg->mod);
  28. mod &= ~MOD_RCLK_MASK;
  29. switch (rfs) {
  30. case 768:
  31. mod |= MOD_RCLK_768FS;
  32. break;
  33. case 512:
  34. mod |= MOD_RCLK_512FS;
  35. break;
  36. case 384:
  37. mod |= MOD_RCLK_384FS;
  38. break;
  39. default:
  40. mod |= MOD_RCLK_256FS;
  41. break;
  42. }
  43. writel(mod, &i2s_reg->mod);
  44. }
  45. /*
  46. * Sets the i2s transfer control
  47. *
  48. * @param i2s_reg i2s regiter address
  49. * @param on 1 enable tx , 0 disable tx transfer
  50. */
  51. static void i2s_txctrl(struct i2s_reg *i2s_reg, int on)
  52. {
  53. unsigned int con = readl(&i2s_reg->con);
  54. unsigned int mod = readl(&i2s_reg->mod) & ~MOD_MASK;
  55. if (on) {
  56. con |= CON_ACTIVE;
  57. con &= ~CON_TXCH_PAUSE;
  58. } else {
  59. con |= CON_TXCH_PAUSE;
  60. con &= ~CON_ACTIVE;
  61. }
  62. writel(mod, &i2s_reg->mod);
  63. writel(con, &i2s_reg->con);
  64. }
  65. /*
  66. * set the bit clock frame size (in multiples of LRCLK)
  67. *
  68. * @param i2s_reg i2s regiter address
  69. * @param bfs bit Frame Size
  70. */
  71. static void i2s_set_bitclk_framesize(struct i2s_reg *i2s_reg, unsigned bfs)
  72. {
  73. unsigned int mod = readl(&i2s_reg->mod);
  74. mod &= ~MOD_BCLK_MASK;
  75. switch (bfs) {
  76. case 48:
  77. mod |= MOD_BCLK_48FS;
  78. break;
  79. case 32:
  80. mod |= MOD_BCLK_32FS;
  81. break;
  82. case 24:
  83. mod |= MOD_BCLK_24FS;
  84. break;
  85. case 16:
  86. mod |= MOD_BCLK_16FS;
  87. break;
  88. default:
  89. return;
  90. }
  91. writel(mod, &i2s_reg->mod);
  92. }
  93. /*
  94. * flushes the i2stx fifo
  95. *
  96. * @param i2s_reg i2s regiter address
  97. * @param flush Tx fifo flush command (0x00 - do not flush
  98. * 0x80 - flush tx fifo)
  99. */
  100. void i2s_fifo(struct i2s_reg *i2s_reg, unsigned int flush)
  101. {
  102. /* Flush the FIFO */
  103. setbits_le32(&i2s_reg->fic, flush);
  104. clrbits_le32(&i2s_reg->fic, flush);
  105. }
  106. /*
  107. * Set System Clock direction
  108. *
  109. * @param i2s_reg i2s regiter address
  110. * @param dir Clock direction
  111. *
  112. * @return int value 0 for success, -1 in case of error
  113. */
  114. int i2s_set_sysclk_dir(struct i2s_reg *i2s_reg, int dir)
  115. {
  116. unsigned int mod = readl(&i2s_reg->mod);
  117. if (dir == SND_SOC_CLOCK_IN)
  118. mod |= MOD_CDCLKCON;
  119. else
  120. mod &= ~MOD_CDCLKCON;
  121. writel(mod, &i2s_reg->mod);
  122. return 0;
  123. }
  124. /*
  125. * Sets I2S Clcok format
  126. *
  127. * @param fmt i2s clock properties
  128. * @param i2s_reg i2s regiter address
  129. *
  130. * @return int value 0 for success, -1 in case of error
  131. */
  132. int i2s_set_fmt(struct i2s_reg *i2s_reg, unsigned int fmt)
  133. {
  134. unsigned int mod = readl(&i2s_reg->mod);
  135. unsigned int tmp = 0;
  136. unsigned int ret = 0;
  137. /* Format is priority */
  138. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  139. case SND_SOC_DAIFMT_RIGHT_J:
  140. tmp |= MOD_LR_RLOW;
  141. tmp |= MOD_SDF_MSB;
  142. break;
  143. case SND_SOC_DAIFMT_LEFT_J:
  144. tmp |= MOD_LR_RLOW;
  145. tmp |= MOD_SDF_LSB;
  146. break;
  147. case SND_SOC_DAIFMT_I2S:
  148. tmp |= MOD_SDF_IIS;
  149. break;
  150. default:
  151. debug("%s: Invalid format priority [0x%x]\n", __func__,
  152. (fmt & SND_SOC_DAIFMT_FORMAT_MASK));
  153. return -1;
  154. }
  155. /*
  156. * INV flag is relative to the FORMAT flag - if set it simply
  157. * flips the polarity specified by the Standard
  158. */
  159. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  160. case SND_SOC_DAIFMT_NB_NF:
  161. break;
  162. case SND_SOC_DAIFMT_NB_IF:
  163. if (tmp & MOD_LR_RLOW)
  164. tmp &= ~MOD_LR_RLOW;
  165. else
  166. tmp |= MOD_LR_RLOW;
  167. break;
  168. default:
  169. debug("%s: Invalid clock ploarity input [0x%x]\n", __func__,
  170. (fmt & SND_SOC_DAIFMT_INV_MASK));
  171. return -1;
  172. }
  173. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  174. case SND_SOC_DAIFMT_CBS_CFS:
  175. tmp |= MOD_SLAVE;
  176. break;
  177. case SND_SOC_DAIFMT_CBM_CFM:
  178. /* Set default source clock in Master mode */
  179. ret = i2s_set_sysclk_dir(i2s_reg, SND_SOC_CLOCK_OUT);
  180. if (ret != 0) {
  181. debug("%s:set i2s clock direction failed\n", __func__);
  182. return -1;
  183. }
  184. break;
  185. default:
  186. debug("%s: Invalid master selection [0x%x]\n", __func__,
  187. (fmt & SND_SOC_DAIFMT_MASTER_MASK));
  188. return -1;
  189. }
  190. mod &= ~(MOD_SDF_MASK | MOD_LR_RLOW | MOD_SLAVE);
  191. mod |= tmp;
  192. writel(mod, &i2s_reg->mod);
  193. return 0;
  194. }
  195. /*
  196. * Sets the sample width in bits
  197. *
  198. * @param blc samplewidth (size of sample in bits)
  199. * @param i2s_reg i2s regiter address
  200. *
  201. * @return int value 0 for success, -1 in case of error
  202. */
  203. int i2s_set_samplesize(struct i2s_reg *i2s_reg, unsigned int blc)
  204. {
  205. unsigned int mod = readl(&i2s_reg->mod);
  206. mod &= ~MOD_BLCP_MASK;
  207. mod &= ~MOD_BLC_MASK;
  208. switch (blc) {
  209. case 8:
  210. mod |= MOD_BLCP_8BIT;
  211. mod |= MOD_BLC_8BIT;
  212. break;
  213. case 16:
  214. mod |= MOD_BLCP_16BIT;
  215. mod |= MOD_BLC_16BIT;
  216. break;
  217. case 24:
  218. mod |= MOD_BLCP_24BIT;
  219. mod |= MOD_BLC_24BIT;
  220. break;
  221. default:
  222. debug("%s: Invalid sample size input [0x%x]\n",
  223. __func__, blc);
  224. return -1;
  225. }
  226. writel(mod, &i2s_reg->mod);
  227. return 0;
  228. }
  229. int i2s_transfer_tx_data(struct i2stx_info *pi2s_tx, unsigned int *data,
  230. unsigned long data_size)
  231. {
  232. int i;
  233. int start;
  234. struct i2s_reg *i2s_reg =
  235. (struct i2s_reg *)pi2s_tx->base_address;
  236. if (data_size < FIFO_LENGTH) {
  237. debug("%s : Invalid data size\n", __func__);
  238. return -1; /* invalid pcm data size */
  239. }
  240. /* fill the tx buffer before stating the tx transmit */
  241. for (i = 0; i < FIFO_LENGTH; i++)
  242. writel(*data++, &i2s_reg->txd);
  243. data_size -= FIFO_LENGTH;
  244. i2s_txctrl(i2s_reg, I2S_TX_ON);
  245. while (data_size > 0) {
  246. start = get_timer(0);
  247. if (!(CON_TXFIFO_FULL & (readl(&i2s_reg->con)))) {
  248. writel(*data++, &i2s_reg->txd);
  249. data_size--;
  250. } else {
  251. if (get_timer(start) > TIMEOUT_I2S_TX) {
  252. i2s_txctrl(i2s_reg, I2S_TX_OFF);
  253. debug("%s: I2S Transfer Timeout\n", __func__);
  254. return -1;
  255. }
  256. }
  257. }
  258. i2s_txctrl(i2s_reg, I2S_TX_OFF);
  259. return 0;
  260. }
  261. int i2s_tx_init(struct i2stx_info *pi2s_tx)
  262. {
  263. int ret;
  264. struct i2s_reg *i2s_reg =
  265. (struct i2s_reg *)pi2s_tx->base_address;
  266. if (pi2s_tx->id == 0) {
  267. /* Initialize GPIO for I2S-0 */
  268. exynos_pinmux_config(PERIPH_ID_I2S0, 0);
  269. /* Set EPLL Clock */
  270. ret = set_epll_clk(pi2s_tx->samplingrate * pi2s_tx->rfs * 4);
  271. } else if (pi2s_tx->id == 1) {
  272. /* Initialize GPIO for I2S-1 */
  273. exynos_pinmux_config(PERIPH_ID_I2S1, 0);
  274. /* Set EPLL Clock */
  275. ret = set_epll_clk(pi2s_tx->audio_pll_clk);
  276. } else {
  277. debug("%s: unsupported i2s-%d bus\n", __func__, pi2s_tx->id);
  278. return -1;
  279. }
  280. if (ret != 0) {
  281. debug("%s: epll clock set rate failed\n", __func__);
  282. return -1;
  283. }
  284. /* Select Clk Source for Audio 0 or 1 */
  285. ret = set_i2s_clk_source(pi2s_tx->id);
  286. if (ret == -1) {
  287. debug("%s: unsupported clock for i2s-%d\n", __func__,
  288. pi2s_tx->id);
  289. return -1;
  290. }
  291. if (pi2s_tx->id == 0) {
  292. /*Reset the i2s module */
  293. writel(CON_RESET, &i2s_reg->con);
  294. writel(MOD_OP_CLK | MOD_RCLKSRC, &i2s_reg->mod);
  295. /* set i2s prescaler */
  296. writel(PSREN | PSVAL, &i2s_reg->psr);
  297. } else {
  298. /* Set Prescaler to get MCLK */
  299. ret = set_i2s_clk_prescaler(pi2s_tx->audio_pll_clk,
  300. (pi2s_tx->samplingrate * (pi2s_tx->rfs)),
  301. pi2s_tx->id);
  302. }
  303. if (ret == -1) {
  304. debug("%s: unsupported prescalar for i2s-%d\n", __func__,
  305. pi2s_tx->id);
  306. return -1;
  307. }
  308. /* Configure I2s format */
  309. ret = i2s_set_fmt(i2s_reg, (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  310. SND_SOC_DAIFMT_CBM_CFM));
  311. if (ret == 0) {
  312. i2s_set_lr_framesize(i2s_reg, pi2s_tx->rfs);
  313. ret = i2s_set_samplesize(i2s_reg, pi2s_tx->bitspersample);
  314. if (ret != 0) {
  315. debug("%s:set sample rate failed\n", __func__);
  316. return -1;
  317. }
  318. i2s_set_bitclk_framesize(i2s_reg, pi2s_tx->bfs);
  319. /* disable i2s transfer flag and flush the fifo */
  320. i2s_txctrl(i2s_reg, I2S_TX_OFF);
  321. i2s_fifo(i2s_reg, FIC_TXFLUSH);
  322. } else {
  323. debug("%s: failed\n", __func__);
  324. }
  325. return ret;
  326. }