samsung-i2s.c 7.8 KB

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