samsung-i2s.c 7.9 KB

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