s3c-i2s-v2.c 18 KB

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