s3c-i2s-v2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /* sound/soc/s3c24xx/s3c-i2c-v2.c
  2. *
  3. * ALSA Soc Audio Layer - I2S core for newer Samsung SoCs.
  4. *
  5. * Copyright (c) 2006 Wolfson Microelectronics PLC.
  6. * Graeme Gregory graeme.gregory@wolfsonmicro.com
  7. * linux@wolfsonmicro.com
  8. *
  9. * Copyright (c) 2008, 2007, 2004-2005 Simtec Electronics
  10. * http://armlinux.simtec.co.uk/
  11. * Ben Dooks <ben@simtec.co.uk>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/delay.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <mach/dma.h>
  25. #include "regs-i2s-v2.h"
  26. #include "s3c-i2s-v2.h"
  27. #include "s3c-dma.h"
  28. #undef S3C_IIS_V2_SUPPORTED
  29. #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
  30. #define S3C_IIS_V2_SUPPORTED
  31. #endif
  32. #ifdef CONFIG_PLAT_S3C64XX
  33. #define S3C_IIS_V2_SUPPORTED
  34. #endif
  35. #ifndef S3C_IIS_V2_SUPPORTED
  36. #error Unsupported CPU model
  37. #endif
  38. #define S3C2412_I2S_DEBUG_CON 0
  39. static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
  40. {
  41. return cpu_dai->private_data;
  42. }
  43. #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
  44. #if S3C2412_I2S_DEBUG_CON
  45. static void dbg_showcon(const char *fn, u32 con)
  46. {
  47. printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
  48. bit_set(con, S3C2412_IISCON_LRINDEX),
  49. bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
  50. bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
  51. bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
  52. bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
  53. printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
  54. fn,
  55. bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
  56. bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
  57. bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
  58. bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
  59. printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
  60. bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
  61. bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
  62. bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
  63. }
  64. #else
  65. static inline void dbg_showcon(const char *fn, u32 con)
  66. {
  67. }
  68. #endif
  69. /* Turn on or off the transmission path. */
  70. static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
  71. {
  72. void __iomem *regs = i2s->regs;
  73. u32 fic, con, mod;
  74. pr_debug("%s(%d)\n", __func__, on);
  75. fic = readl(regs + S3C2412_IISFIC);
  76. con = readl(regs + S3C2412_IISCON);
  77. mod = readl(regs + S3C2412_IISMOD);
  78. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  79. if (on) {
  80. con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
  81. con &= ~S3C2412_IISCON_TXDMA_PAUSE;
  82. con &= ~S3C2412_IISCON_TXCH_PAUSE;
  83. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  84. case S3C2412_IISMOD_MODE_TXONLY:
  85. case S3C2412_IISMOD_MODE_TXRX:
  86. /* do nothing, we are in the right mode */
  87. break;
  88. case S3C2412_IISMOD_MODE_RXONLY:
  89. mod &= ~S3C2412_IISMOD_MODE_MASK;
  90. mod |= S3C2412_IISMOD_MODE_TXRX;
  91. break;
  92. default:
  93. dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
  94. mod & S3C2412_IISMOD_MODE_MASK);
  95. break;
  96. }
  97. writel(con, regs + S3C2412_IISCON);
  98. writel(mod, regs + S3C2412_IISMOD);
  99. } else {
  100. /* Note, we do not have any indication that the FIFO problems
  101. * tha the S3C2410/2440 had apply here, so we should be able
  102. * to disable the DMA and TX without resetting the FIFOS.
  103. */
  104. con |= S3C2412_IISCON_TXDMA_PAUSE;
  105. con |= S3C2412_IISCON_TXCH_PAUSE;
  106. con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
  107. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  108. case S3C2412_IISMOD_MODE_TXRX:
  109. mod &= ~S3C2412_IISMOD_MODE_MASK;
  110. mod |= S3C2412_IISMOD_MODE_RXONLY;
  111. break;
  112. case S3C2412_IISMOD_MODE_TXONLY:
  113. mod &= ~S3C2412_IISMOD_MODE_MASK;
  114. con &= ~S3C2412_IISCON_IIS_ACTIVE;
  115. break;
  116. default:
  117. dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
  118. mod & S3C2412_IISMOD_MODE_MASK);
  119. break;
  120. }
  121. writel(mod, regs + S3C2412_IISMOD);
  122. writel(con, regs + S3C2412_IISCON);
  123. }
  124. fic = readl(regs + S3C2412_IISFIC);
  125. dbg_showcon(__func__, con);
  126. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  127. }
  128. static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
  129. {
  130. void __iomem *regs = i2s->regs;
  131. u32 fic, con, mod;
  132. pr_debug("%s(%d)\n", __func__, on);
  133. fic = readl(regs + S3C2412_IISFIC);
  134. con = readl(regs + S3C2412_IISCON);
  135. mod = readl(regs + S3C2412_IISMOD);
  136. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  137. if (on) {
  138. con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
  139. con &= ~S3C2412_IISCON_RXDMA_PAUSE;
  140. con &= ~S3C2412_IISCON_RXCH_PAUSE;
  141. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  142. case S3C2412_IISMOD_MODE_TXRX:
  143. case S3C2412_IISMOD_MODE_RXONLY:
  144. /* do nothing, we are in the right mode */
  145. break;
  146. case S3C2412_IISMOD_MODE_TXONLY:
  147. mod &= ~S3C2412_IISMOD_MODE_MASK;
  148. mod |= S3C2412_IISMOD_MODE_TXRX;
  149. break;
  150. default:
  151. dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
  152. mod & S3C2412_IISMOD_MODE_MASK);
  153. }
  154. writel(mod, regs + S3C2412_IISMOD);
  155. writel(con, regs + S3C2412_IISCON);
  156. } else {
  157. /* See txctrl notes on FIFOs. */
  158. con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
  159. con |= S3C2412_IISCON_RXDMA_PAUSE;
  160. con |= S3C2412_IISCON_RXCH_PAUSE;
  161. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  162. case S3C2412_IISMOD_MODE_RXONLY:
  163. con &= ~S3C2412_IISCON_IIS_ACTIVE;
  164. mod &= ~S3C2412_IISMOD_MODE_MASK;
  165. break;
  166. case S3C2412_IISMOD_MODE_TXRX:
  167. mod &= ~S3C2412_IISMOD_MODE_MASK;
  168. mod |= S3C2412_IISMOD_MODE_TXONLY;
  169. break;
  170. default:
  171. dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
  172. mod & S3C2412_IISMOD_MODE_MASK);
  173. }
  174. writel(con, regs + S3C2412_IISCON);
  175. writel(mod, regs + S3C2412_IISMOD);
  176. }
  177. fic = readl(regs + S3C2412_IISFIC);
  178. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  179. }
  180. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  181. /*
  182. * Wait for the LR signal to allow synchronisation to the L/R clock
  183. * from the codec. May only be needed for slave mode.
  184. */
  185. static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
  186. {
  187. u32 iiscon;
  188. unsigned long loops = msecs_to_loops(5);
  189. pr_debug("Entered %s\n", __func__);
  190. while (--loops) {
  191. iiscon = readl(i2s->regs + S3C2412_IISCON);
  192. if (iiscon & S3C2412_IISCON_LRINDEX)
  193. break;
  194. cpu_relax();
  195. }
  196. if (!loops) {
  197. printk(KERN_ERR "%s: timeout\n", __func__);
  198. return -ETIMEDOUT;
  199. }
  200. return 0;
  201. }
  202. /*
  203. * Set S3C2412 I2S DAI format
  204. */
  205. static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  206. unsigned int fmt)
  207. {
  208. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  209. u32 iismod;
  210. pr_debug("Entered %s\n", __func__);
  211. iismod = readl(i2s->regs + S3C2412_IISMOD);
  212. pr_debug("hw_params r: IISMOD: %x \n", iismod);
  213. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  214. case SND_SOC_DAIFMT_CBM_CFM:
  215. i2s->master = 0;
  216. iismod |= S3C2412_IISMOD_SLAVE;
  217. break;
  218. case SND_SOC_DAIFMT_CBS_CFS:
  219. i2s->master = 1;
  220. iismod &= ~S3C2412_IISMOD_SLAVE;
  221. break;
  222. default:
  223. pr_err("unknwon master/slave format\n");
  224. return -EINVAL;
  225. }
  226. iismod &= ~S3C2412_IISMOD_SDF_MASK;
  227. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  228. case SND_SOC_DAIFMT_RIGHT_J:
  229. iismod |= S3C2412_IISMOD_LR_RLOW;
  230. iismod |= S3C2412_IISMOD_SDF_MSB;
  231. break;
  232. case SND_SOC_DAIFMT_LEFT_J:
  233. iismod |= S3C2412_IISMOD_LR_RLOW;
  234. iismod |= S3C2412_IISMOD_SDF_LSB;
  235. break;
  236. case SND_SOC_DAIFMT_I2S:
  237. iismod &= ~S3C2412_IISMOD_LR_RLOW;
  238. iismod |= S3C2412_IISMOD_SDF_IIS;
  239. break;
  240. default:
  241. pr_err("Unknown data format\n");
  242. return -EINVAL;
  243. }
  244. writel(iismod, i2s->regs + S3C2412_IISMOD);
  245. pr_debug("hw_params w: IISMOD: %x \n", iismod);
  246. return 0;
  247. }
  248. static int s3c_i2sv2_hw_params(struct snd_pcm_substream *substream,
  249. struct snd_pcm_hw_params *params,
  250. struct snd_soc_dai *socdai)
  251. {
  252. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  253. struct snd_soc_dai_link *dai = rtd->dai;
  254. struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
  255. struct s3c_dma_params *dma_data;
  256. u32 iismod;
  257. pr_debug("Entered %s\n", __func__);
  258. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  259. dma_data = i2s->dma_playback;
  260. else
  261. dma_data = i2s->dma_capture;
  262. snd_soc_dai_set_dma_data(dai->cpu_dai, substream, dma_data);
  263. /* Working copies of register */
  264. iismod = readl(i2s->regs + S3C2412_IISMOD);
  265. pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
  266. iismod &= ~S3C64XX_IISMOD_BLC_MASK;
  267. /* Sample size */
  268. switch (params_format(params)) {
  269. case SNDRV_PCM_FORMAT_S8:
  270. iismod |= S3C64XX_IISMOD_BLC_8BIT;
  271. break;
  272. case SNDRV_PCM_FORMAT_S16_LE:
  273. break;
  274. case SNDRV_PCM_FORMAT_S24_LE:
  275. iismod |= S3C64XX_IISMOD_BLC_24BIT;
  276. break;
  277. }
  278. writel(iismod, i2s->regs + S3C2412_IISMOD);
  279. pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
  280. return 0;
  281. }
  282. static int s3c_i2sv2_set_sysclk(struct snd_soc_dai *cpu_dai,
  283. int clk_id, unsigned int freq, int dir)
  284. {
  285. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  286. u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
  287. pr_debug("Entered %s\n", __func__);
  288. pr_debug("%s r: IISMOD: %x\n", __func__, iismod);
  289. switch (clk_id) {
  290. case S3C_I2SV2_CLKSRC_PCLK:
  291. iismod &= ~S3C2412_IISMOD_IMS_SYSMUX;
  292. break;
  293. case S3C_I2SV2_CLKSRC_AUDIOBUS:
  294. iismod |= S3C2412_IISMOD_IMS_SYSMUX;
  295. break;
  296. case S3C_I2SV2_CLKSRC_CDCLK:
  297. /* Error if controller doesn't have the CDCLKCON bit */
  298. if (!(i2s->feature & S3C_FEATURE_CDCLKCON))
  299. return -EINVAL;
  300. switch (dir) {
  301. case SND_SOC_CLOCK_IN:
  302. iismod |= S3C64XX_IISMOD_CDCLKCON;
  303. break;
  304. case SND_SOC_CLOCK_OUT:
  305. iismod &= ~S3C64XX_IISMOD_CDCLKCON;
  306. break;
  307. default:
  308. return -EINVAL;
  309. }
  310. break;
  311. default:
  312. return -EINVAL;
  313. }
  314. writel(iismod, i2s->regs + S3C2412_IISMOD);
  315. pr_debug("%s w: IISMOD: %x\n", __func__, iismod);
  316. return 0;
  317. }
  318. static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  319. struct snd_soc_dai *dai)
  320. {
  321. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  322. struct s3c_i2sv2_info *i2s = to_info(rtd->dai->cpu_dai);
  323. int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  324. unsigned long irqs;
  325. int ret = 0;
  326. struct s3c_dma_params *dma_data =
  327. snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
  328. pr_debug("Entered %s\n", __func__);
  329. switch (cmd) {
  330. case SNDRV_PCM_TRIGGER_START:
  331. /* On start, ensure that the FIFOs are cleared and reset. */
  332. writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
  333. i2s->regs + S3C2412_IISFIC);
  334. /* clear again, just in case */
  335. writel(0x0, i2s->regs + S3C2412_IISFIC);
  336. case SNDRV_PCM_TRIGGER_RESUME:
  337. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  338. if (!i2s->master) {
  339. ret = s3c2412_snd_lrsync(i2s);
  340. if (ret)
  341. goto exit_err;
  342. }
  343. local_irq_save(irqs);
  344. if (capture)
  345. s3c2412_snd_rxctrl(i2s, 1);
  346. else
  347. s3c2412_snd_txctrl(i2s, 1);
  348. local_irq_restore(irqs);
  349. /*
  350. * Load the next buffer to DMA to meet the reqirement
  351. * of the auto reload mechanism of S3C24XX.
  352. * This call won't bother S3C64XX.
  353. */
  354. s3c2410_dma_ctrl(dma_data->channel, S3C2410_DMAOP_STARTED);
  355. break;
  356. case SNDRV_PCM_TRIGGER_STOP:
  357. case SNDRV_PCM_TRIGGER_SUSPEND:
  358. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  359. local_irq_save(irqs);
  360. if (capture)
  361. s3c2412_snd_rxctrl(i2s, 0);
  362. else
  363. s3c2412_snd_txctrl(i2s, 0);
  364. local_irq_restore(irqs);
  365. break;
  366. default:
  367. ret = -EINVAL;
  368. break;
  369. }
  370. exit_err:
  371. return ret;
  372. }
  373. /*
  374. * Set S3C2412 Clock dividers
  375. */
  376. static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
  377. int div_id, int div)
  378. {
  379. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  380. u32 reg;
  381. pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
  382. switch (div_id) {
  383. case S3C_I2SV2_DIV_BCLK:
  384. switch (div) {
  385. case 16:
  386. div = S3C2412_IISMOD_BCLK_16FS;
  387. break;
  388. case 32:
  389. div = S3C2412_IISMOD_BCLK_32FS;
  390. break;
  391. case 24:
  392. div = S3C2412_IISMOD_BCLK_24FS;
  393. break;
  394. case 48:
  395. div = S3C2412_IISMOD_BCLK_48FS;
  396. break;
  397. default:
  398. return -EINVAL;
  399. }
  400. reg = readl(i2s->regs + S3C2412_IISMOD);
  401. reg &= ~S3C2412_IISMOD_BCLK_MASK;
  402. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  403. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  404. break;
  405. case S3C_I2SV2_DIV_RCLK:
  406. switch (div) {
  407. case 256:
  408. div = S3C2412_IISMOD_RCLK_256FS;
  409. break;
  410. case 384:
  411. div = S3C2412_IISMOD_RCLK_384FS;
  412. break;
  413. case 512:
  414. div = S3C2412_IISMOD_RCLK_512FS;
  415. break;
  416. case 768:
  417. div = S3C2412_IISMOD_RCLK_768FS;
  418. break;
  419. default:
  420. return -EINVAL;
  421. }
  422. reg = readl(i2s->regs + S3C2412_IISMOD);
  423. reg &= ~S3C2412_IISMOD_RCLK_MASK;
  424. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  425. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  426. break;
  427. case S3C_I2SV2_DIV_PRESCALER:
  428. if (div >= 0) {
  429. writel((div << 8) | S3C2412_IISPSR_PSREN,
  430. i2s->regs + S3C2412_IISPSR);
  431. } else {
  432. writel(0x0, i2s->regs + S3C2412_IISPSR);
  433. }
  434. pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
  435. break;
  436. default:
  437. return -EINVAL;
  438. }
  439. return 0;
  440. }
  441. static snd_pcm_sframes_t s3c2412_i2s_delay(struct snd_pcm_substream *substream,
  442. struct snd_soc_dai *dai)
  443. {
  444. struct s3c_i2sv2_info *i2s = to_info(dai);
  445. u32 reg = readl(i2s->regs + S3C2412_IISFIC);
  446. snd_pcm_sframes_t delay;
  447. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  448. delay = S3C2412_IISFIC_TXCOUNT(reg);
  449. else
  450. delay = S3C2412_IISFIC_RXCOUNT(reg);
  451. return delay;
  452. }
  453. struct clk *s3c_i2sv2_get_clock(struct snd_soc_dai *cpu_dai)
  454. {
  455. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  456. u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
  457. if (iismod & S3C2412_IISMOD_IMS_SYSMUX)
  458. return i2s->iis_cclk;
  459. else
  460. return i2s->iis_pclk;
  461. }
  462. EXPORT_SYMBOL_GPL(s3c_i2sv2_get_clock);
  463. /* default table of all avaialable root fs divisors */
  464. static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
  465. int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
  466. unsigned int *fstab,
  467. unsigned int rate, struct clk *clk)
  468. {
  469. unsigned long clkrate = clk_get_rate(clk);
  470. unsigned int div;
  471. unsigned int fsclk;
  472. unsigned int actual;
  473. unsigned int fs;
  474. unsigned int fsdiv;
  475. signed int deviation = 0;
  476. unsigned int best_fs = 0;
  477. unsigned int best_div = 0;
  478. unsigned int best_rate = 0;
  479. unsigned int best_deviation = INT_MAX;
  480. pr_debug("Input clock rate %ldHz\n", clkrate);
  481. if (fstab == NULL)
  482. fstab = iis_fs_tab;
  483. for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
  484. fsdiv = iis_fs_tab[fs];
  485. fsclk = clkrate / fsdiv;
  486. div = fsclk / rate;
  487. if ((fsclk % rate) > (rate / 2))
  488. div++;
  489. if (div <= 1)
  490. continue;
  491. actual = clkrate / (fsdiv * div);
  492. deviation = actual - rate;
  493. printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
  494. fsdiv, div, actual, deviation);
  495. deviation = abs(deviation);
  496. if (deviation < best_deviation) {
  497. best_fs = fsdiv;
  498. best_div = div;
  499. best_rate = actual;
  500. best_deviation = deviation;
  501. }
  502. if (deviation == 0)
  503. break;
  504. }
  505. printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
  506. best_fs, best_div, best_rate);
  507. info->fs_div = best_fs;
  508. info->clk_div = best_div;
  509. return 0;
  510. }
  511. EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
  512. int s3c_i2sv2_probe(struct platform_device *pdev,
  513. struct snd_soc_dai *dai,
  514. struct s3c_i2sv2_info *i2s,
  515. unsigned long base)
  516. {
  517. struct device *dev = &pdev->dev;
  518. unsigned int iismod;
  519. i2s->dev = dev;
  520. /* record our i2s structure for later use in the callbacks */
  521. dai->private_data = i2s;
  522. if (!base) {
  523. struct resource *res = platform_get_resource(pdev,
  524. IORESOURCE_MEM,
  525. 0);
  526. if (!res) {
  527. dev_err(dev, "Unable to get register resource\n");
  528. return -ENXIO;
  529. }
  530. if (!request_mem_region(res->start, resource_size(res),
  531. "s3c64xx-i2s-v4")) {
  532. dev_err(dev, "Unable to request register region\n");
  533. return -EBUSY;
  534. }
  535. base = res->start;
  536. }
  537. i2s->regs = ioremap(base, 0x100);
  538. if (i2s->regs == NULL) {
  539. dev_err(dev, "cannot ioremap registers\n");
  540. return -ENXIO;
  541. }
  542. i2s->iis_pclk = clk_get(dev, "iis");
  543. if (IS_ERR(i2s->iis_pclk)) {
  544. dev_err(dev, "failed to get iis_clock\n");
  545. iounmap(i2s->regs);
  546. return -ENOENT;
  547. }
  548. clk_enable(i2s->iis_pclk);
  549. /* Mark ourselves as in TXRX mode so we can run through our cleanup
  550. * process without warnings. */
  551. iismod = readl(i2s->regs + S3C2412_IISMOD);
  552. iismod |= S3C2412_IISMOD_MODE_TXRX;
  553. writel(iismod, i2s->regs + S3C2412_IISMOD);
  554. s3c2412_snd_txctrl(i2s, 0);
  555. s3c2412_snd_rxctrl(i2s, 0);
  556. return 0;
  557. }
  558. EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
  559. #ifdef CONFIG_PM
  560. static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
  561. {
  562. struct s3c_i2sv2_info *i2s = to_info(dai);
  563. u32 iismod;
  564. if (dai->active) {
  565. i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
  566. i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
  567. i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
  568. /* some basic suspend checks */
  569. iismod = readl(i2s->regs + S3C2412_IISMOD);
  570. if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
  571. pr_warning("%s: RXDMA active?\n", __func__);
  572. if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
  573. pr_warning("%s: TXDMA active?\n", __func__);
  574. if (iismod & S3C2412_IISCON_IIS_ACTIVE)
  575. pr_warning("%s: IIS active\n", __func__);
  576. }
  577. return 0;
  578. }
  579. static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
  580. {
  581. struct s3c_i2sv2_info *i2s = to_info(dai);
  582. pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
  583. dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
  584. if (dai->active) {
  585. writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
  586. writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
  587. writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
  588. writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
  589. i2s->regs + S3C2412_IISFIC);
  590. ndelay(250);
  591. writel(0x0, i2s->regs + S3C2412_IISFIC);
  592. }
  593. return 0;
  594. }
  595. #else
  596. #define s3c2412_i2s_suspend NULL
  597. #define s3c2412_i2s_resume NULL
  598. #endif
  599. int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
  600. {
  601. struct snd_soc_dai_ops *ops = dai->ops;
  602. ops->trigger = s3c2412_i2s_trigger;
  603. if (!ops->hw_params)
  604. ops->hw_params = s3c_i2sv2_hw_params;
  605. ops->set_fmt = s3c2412_i2s_set_fmt;
  606. ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
  607. ops->set_sysclk = s3c_i2sv2_set_sysclk;
  608. /* Allow overriding by (for example) IISv4 */
  609. if (!ops->delay)
  610. ops->delay = s3c2412_i2s_delay;
  611. dai->suspend = s3c2412_i2s_suspend;
  612. dai->resume = s3c2412_i2s_resume;
  613. return snd_soc_register_dai(dai);
  614. }
  615. EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
  616. MODULE_LICENSE("GPL");