s3c-i2s-v2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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/init.h>
  19. #include <linux/module.h>
  20. #include <linux/device.h>
  21. #include <linux/delay.h>
  22. #include <linux/clk.h>
  23. #include <linux/kernel.h>
  24. #include <linux/io.h>
  25. #include <sound/core.h>
  26. #include <sound/pcm.h>
  27. #include <sound/pcm_params.h>
  28. #include <sound/initval.h>
  29. #include <sound/soc.h>
  30. #include <plat/regs-s3c2412-iis.h>
  31. #include <plat/audio.h>
  32. #include <mach/dma.h>
  33. #include "s3c-i2s-v2.h"
  34. #include "s3c24xx-pcm.h"
  35. #undef S3C_IIS_V2_SUPPORTED
  36. #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
  37. #define S3C_IIS_V2_SUPPORTED
  38. #endif
  39. #ifdef CONFIG_PLAT_S3C64XX
  40. #define S3C_IIS_V2_SUPPORTED
  41. #endif
  42. #ifndef S3C_IIS_V2_SUPPORTED
  43. #error Unsupported CPU model
  44. #endif
  45. #define S3C2412_I2S_DEBUG_CON 0
  46. static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
  47. {
  48. return cpu_dai->private_data;
  49. }
  50. #define bit_set(v, b) (((v) & (b)) ? 1 : 0)
  51. #if S3C2412_I2S_DEBUG_CON
  52. static void dbg_showcon(const char *fn, u32 con)
  53. {
  54. printk(KERN_DEBUG "%s: LRI=%d, TXFEMPT=%d, RXFEMPT=%d, TXFFULL=%d, RXFFULL=%d\n", fn,
  55. bit_set(con, S3C2412_IISCON_LRINDEX),
  56. bit_set(con, S3C2412_IISCON_TXFIFO_EMPTY),
  57. bit_set(con, S3C2412_IISCON_RXFIFO_EMPTY),
  58. bit_set(con, S3C2412_IISCON_TXFIFO_FULL),
  59. bit_set(con, S3C2412_IISCON_RXFIFO_FULL));
  60. printk(KERN_DEBUG "%s: PAUSE: TXDMA=%d, RXDMA=%d, TXCH=%d, RXCH=%d\n",
  61. fn,
  62. bit_set(con, S3C2412_IISCON_TXDMA_PAUSE),
  63. bit_set(con, S3C2412_IISCON_RXDMA_PAUSE),
  64. bit_set(con, S3C2412_IISCON_TXCH_PAUSE),
  65. bit_set(con, S3C2412_IISCON_RXCH_PAUSE));
  66. printk(KERN_DEBUG "%s: ACTIVE: TXDMA=%d, RXDMA=%d, IIS=%d\n", fn,
  67. bit_set(con, S3C2412_IISCON_TXDMA_ACTIVE),
  68. bit_set(con, S3C2412_IISCON_RXDMA_ACTIVE),
  69. bit_set(con, S3C2412_IISCON_IIS_ACTIVE));
  70. }
  71. #else
  72. static inline void dbg_showcon(const char *fn, u32 con)
  73. {
  74. }
  75. #endif
  76. /* Turn on or off the transmission path. */
  77. static void s3c2412_snd_txctrl(struct s3c_i2sv2_info *i2s, int on)
  78. {
  79. void __iomem *regs = i2s->regs;
  80. u32 fic, con, mod;
  81. pr_debug("%s(%d)\n", __func__, on);
  82. fic = readl(regs + S3C2412_IISFIC);
  83. con = readl(regs + S3C2412_IISCON);
  84. mod = readl(regs + S3C2412_IISMOD);
  85. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  86. if (on) {
  87. con |= S3C2412_IISCON_TXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
  88. con &= ~S3C2412_IISCON_TXDMA_PAUSE;
  89. con &= ~S3C2412_IISCON_TXCH_PAUSE;
  90. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  91. case S3C2412_IISMOD_MODE_TXONLY:
  92. case S3C2412_IISMOD_MODE_TXRX:
  93. /* do nothing, we are in the right mode */
  94. break;
  95. case S3C2412_IISMOD_MODE_RXONLY:
  96. mod &= ~S3C2412_IISMOD_MODE_MASK;
  97. mod |= S3C2412_IISMOD_MODE_TXRX;
  98. break;
  99. default:
  100. dev_err(i2s->dev, "TXEN: Invalid MODE %x in IISMOD\n",
  101. mod & S3C2412_IISMOD_MODE_MASK);
  102. break;
  103. }
  104. writel(con, regs + S3C2412_IISCON);
  105. writel(mod, regs + S3C2412_IISMOD);
  106. } else {
  107. /* Note, we do not have any indication that the FIFO problems
  108. * tha the S3C2410/2440 had apply here, so we should be able
  109. * to disable the DMA and TX without resetting the FIFOS.
  110. */
  111. con |= S3C2412_IISCON_TXDMA_PAUSE;
  112. con |= S3C2412_IISCON_TXCH_PAUSE;
  113. con &= ~S3C2412_IISCON_TXDMA_ACTIVE;
  114. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  115. case S3C2412_IISMOD_MODE_TXRX:
  116. mod &= ~S3C2412_IISMOD_MODE_MASK;
  117. mod |= S3C2412_IISMOD_MODE_RXONLY;
  118. break;
  119. case S3C2412_IISMOD_MODE_TXONLY:
  120. mod &= ~S3C2412_IISMOD_MODE_MASK;
  121. con &= ~S3C2412_IISCON_IIS_ACTIVE;
  122. break;
  123. default:
  124. dev_err(i2s->dev, "TXDIS: Invalid MODE %x in IISMOD\n",
  125. mod & S3C2412_IISMOD_MODE_MASK);
  126. break;
  127. }
  128. writel(mod, regs + S3C2412_IISMOD);
  129. writel(con, regs + S3C2412_IISCON);
  130. }
  131. fic = readl(regs + S3C2412_IISFIC);
  132. dbg_showcon(__func__, con);
  133. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  134. }
  135. static void s3c2412_snd_rxctrl(struct s3c_i2sv2_info *i2s, int on)
  136. {
  137. void __iomem *regs = i2s->regs;
  138. u32 fic, con, mod;
  139. pr_debug("%s(%d)\n", __func__, on);
  140. fic = readl(regs + S3C2412_IISFIC);
  141. con = readl(regs + S3C2412_IISCON);
  142. mod = readl(regs + S3C2412_IISMOD);
  143. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  144. if (on) {
  145. con |= S3C2412_IISCON_RXDMA_ACTIVE | S3C2412_IISCON_IIS_ACTIVE;
  146. con &= ~S3C2412_IISCON_RXDMA_PAUSE;
  147. con &= ~S3C2412_IISCON_RXCH_PAUSE;
  148. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  149. case S3C2412_IISMOD_MODE_TXRX:
  150. case S3C2412_IISMOD_MODE_RXONLY:
  151. /* do nothing, we are in the right mode */
  152. break;
  153. case S3C2412_IISMOD_MODE_TXONLY:
  154. mod &= ~S3C2412_IISMOD_MODE_MASK;
  155. mod |= S3C2412_IISMOD_MODE_TXRX;
  156. break;
  157. default:
  158. dev_err(i2s->dev, "RXEN: Invalid MODE %x in IISMOD\n",
  159. mod & S3C2412_IISMOD_MODE_MASK);
  160. }
  161. writel(mod, regs + S3C2412_IISMOD);
  162. writel(con, regs + S3C2412_IISCON);
  163. } else {
  164. /* See txctrl notes on FIFOs. */
  165. con &= ~S3C2412_IISCON_RXDMA_ACTIVE;
  166. con |= S3C2412_IISCON_RXDMA_PAUSE;
  167. con |= S3C2412_IISCON_RXCH_PAUSE;
  168. switch (mod & S3C2412_IISMOD_MODE_MASK) {
  169. case S3C2412_IISMOD_MODE_RXONLY:
  170. con &= ~S3C2412_IISCON_IIS_ACTIVE;
  171. mod &= ~S3C2412_IISMOD_MODE_MASK;
  172. break;
  173. case S3C2412_IISMOD_MODE_TXRX:
  174. mod &= ~S3C2412_IISMOD_MODE_MASK;
  175. mod |= S3C2412_IISMOD_MODE_TXONLY;
  176. break;
  177. default:
  178. dev_err(i2s->dev, "RXDIS: Invalid MODE %x in IISMOD\n",
  179. mod & S3C2412_IISMOD_MODE_MASK);
  180. }
  181. writel(con, regs + S3C2412_IISCON);
  182. writel(mod, regs + S3C2412_IISMOD);
  183. }
  184. fic = readl(regs + S3C2412_IISFIC);
  185. pr_debug("%s: IIS: CON=%x MOD=%x FIC=%x\n", __func__, con, mod, fic);
  186. }
  187. #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
  188. /*
  189. * Wait for the LR signal to allow synchronisation to the L/R clock
  190. * from the codec. May only be needed for slave mode.
  191. */
  192. static int s3c2412_snd_lrsync(struct s3c_i2sv2_info *i2s)
  193. {
  194. u32 iiscon;
  195. unsigned long loops = msecs_to_loops(5);
  196. pr_debug("Entered %s\n", __func__);
  197. while (--loops) {
  198. iiscon = readl(i2s->regs + S3C2412_IISCON);
  199. if (iiscon & S3C2412_IISCON_LRINDEX)
  200. break;
  201. cpu_relax();
  202. }
  203. if (!loops) {
  204. printk(KERN_ERR "%s: timeout\n", __func__);
  205. return -ETIMEDOUT;
  206. }
  207. return 0;
  208. }
  209. /*
  210. * Set S3C2412 I2S DAI format
  211. */
  212. static int s3c2412_i2s_set_fmt(struct snd_soc_dai *cpu_dai,
  213. unsigned int fmt)
  214. {
  215. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  216. u32 iismod;
  217. pr_debug("Entered %s\n", __func__);
  218. iismod = readl(i2s->regs + S3C2412_IISMOD);
  219. pr_debug("hw_params r: IISMOD: %x \n", iismod);
  220. #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
  221. #define IISMOD_MASTER_MASK S3C2412_IISMOD_MASTER_MASK
  222. #define IISMOD_SLAVE S3C2412_IISMOD_SLAVE
  223. #define IISMOD_MASTER S3C2412_IISMOD_MASTER_INTERNAL
  224. #endif
  225. #if defined(CONFIG_PLAT_S3C64XX)
  226. /* From Rev1.1 datasheet, we have two master and two slave modes:
  227. * IMS[11:10]:
  228. * 00 = master mode, fed from PCLK
  229. * 01 = master mode, fed from CLKAUDIO
  230. * 10 = slave mode, using PCLK
  231. * 11 = slave mode, using I2SCLK
  232. */
  233. #define IISMOD_MASTER_MASK (1 << 11)
  234. #define IISMOD_SLAVE (1 << 11)
  235. #define IISMOD_MASTER (0 << 11)
  236. #endif
  237. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  238. case SND_SOC_DAIFMT_CBM_CFM:
  239. i2s->master = 0;
  240. iismod &= ~IISMOD_MASTER_MASK;
  241. iismod |= IISMOD_SLAVE;
  242. break;
  243. case SND_SOC_DAIFMT_CBS_CFS:
  244. i2s->master = 1;
  245. iismod &= ~IISMOD_MASTER_MASK;
  246. iismod |= IISMOD_MASTER;
  247. break;
  248. default:
  249. pr_err("unknwon master/slave format\n");
  250. return -EINVAL;
  251. }
  252. iismod &= ~S3C2412_IISMOD_SDF_MASK;
  253. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  254. case SND_SOC_DAIFMT_RIGHT_J:
  255. iismod |= S3C2412_IISMOD_SDF_MSB;
  256. break;
  257. case SND_SOC_DAIFMT_LEFT_J:
  258. iismod |= S3C2412_IISMOD_SDF_LSB;
  259. break;
  260. case SND_SOC_DAIFMT_I2S:
  261. iismod |= S3C2412_IISMOD_SDF_IIS;
  262. break;
  263. default:
  264. pr_err("Unknown data format\n");
  265. return -EINVAL;
  266. }
  267. writel(iismod, i2s->regs + S3C2412_IISMOD);
  268. pr_debug("hw_params w: IISMOD: %x \n", iismod);
  269. return 0;
  270. }
  271. static int s3c2412_i2s_hw_params(struct snd_pcm_substream *substream,
  272. struct snd_pcm_hw_params *params,
  273. struct snd_soc_dai *socdai)
  274. {
  275. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  276. struct snd_soc_dai_link *dai = rtd->dai;
  277. struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
  278. u32 iismod;
  279. pr_debug("Entered %s\n", __func__);
  280. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  281. dai->cpu_dai->dma_data = i2s->dma_playback;
  282. else
  283. dai->cpu_dai->dma_data = i2s->dma_capture;
  284. /* Working copies of register */
  285. iismod = readl(i2s->regs + S3C2412_IISMOD);
  286. pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
  287. #if defined(CONFIG_CPU_S3C2412) || defined(CONFIG_CPU_S3C2413)
  288. switch (params_format(params)) {
  289. case SNDRV_PCM_FORMAT_S8:
  290. iismod |= S3C2412_IISMOD_8BIT;
  291. break;
  292. case SNDRV_PCM_FORMAT_S16_LE:
  293. iismod &= ~S3C2412_IISMOD_8BIT;
  294. break;
  295. }
  296. #endif
  297. #ifdef CONFIG_PLAT_S3C64XX
  298. iismod &= ~(S3C64XX_IISMOD_BLC_MASK | S3C2412_IISMOD_BCLK_MASK);
  299. /* Sample size */
  300. switch (params_format(params)) {
  301. case SNDRV_PCM_FORMAT_S8:
  302. /* 8 bit sample, 16fs BCLK */
  303. iismod |= (S3C64XX_IISMOD_BLC_8BIT | S3C2412_IISMOD_BCLK_16FS);
  304. break;
  305. case SNDRV_PCM_FORMAT_S16_LE:
  306. /* 16 bit sample, 32fs BCLK */
  307. break;
  308. case SNDRV_PCM_FORMAT_S24_LE:
  309. /* 24 bit sample, 48fs BCLK */
  310. iismod |= (S3C64XX_IISMOD_BLC_24BIT | S3C2412_IISMOD_BCLK_48FS);
  311. break;
  312. }
  313. #endif
  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. int channel = ((struct s3c24xx_pcm_dma_params *)
  327. rtd->dai->cpu_dai->dma_data)->channel;
  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(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. reg = readl(i2s->regs + S3C2412_IISMOD);
  385. reg &= ~S3C2412_IISMOD_BCLK_MASK;
  386. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  387. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  388. break;
  389. case S3C_I2SV2_DIV_RCLK:
  390. if (div > 3) {
  391. /* convert value to bit field */
  392. switch (div) {
  393. case 256:
  394. div = S3C2412_IISMOD_RCLK_256FS;
  395. break;
  396. case 384:
  397. div = S3C2412_IISMOD_RCLK_384FS;
  398. break;
  399. case 512:
  400. div = S3C2412_IISMOD_RCLK_512FS;
  401. break;
  402. case 768:
  403. div = S3C2412_IISMOD_RCLK_768FS;
  404. break;
  405. default:
  406. return -EINVAL;
  407. }
  408. }
  409. reg = readl(i2s->regs + S3C2412_IISMOD);
  410. reg &= ~S3C2412_IISMOD_RCLK_MASK;
  411. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  412. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  413. break;
  414. case S3C_I2SV2_DIV_PRESCALER:
  415. if (div >= 0) {
  416. writel((div << 8) | S3C2412_IISPSR_PSREN,
  417. i2s->regs + S3C2412_IISPSR);
  418. } else {
  419. writel(0x0, i2s->regs + S3C2412_IISPSR);
  420. }
  421. pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
  422. break;
  423. default:
  424. return -EINVAL;
  425. }
  426. return 0;
  427. }
  428. /* default table of all avaialable root fs divisors */
  429. static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
  430. int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
  431. unsigned int *fstab,
  432. unsigned int rate, struct clk *clk)
  433. {
  434. unsigned long clkrate = clk_get_rate(clk);
  435. unsigned int div;
  436. unsigned int fsclk;
  437. unsigned int actual;
  438. unsigned int fs;
  439. unsigned int fsdiv;
  440. signed int deviation = 0;
  441. unsigned int best_fs = 0;
  442. unsigned int best_div = 0;
  443. unsigned int best_rate = 0;
  444. unsigned int best_deviation = INT_MAX;
  445. pr_debug("Input clock rate %ldHz\n", clkrate);
  446. if (fstab == NULL)
  447. fstab = iis_fs_tab;
  448. for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
  449. fsdiv = iis_fs_tab[fs];
  450. fsclk = clkrate / fsdiv;
  451. div = fsclk / rate;
  452. if ((fsclk % rate) > (rate / 2))
  453. div++;
  454. if (div <= 1)
  455. continue;
  456. actual = clkrate / (fsdiv * div);
  457. deviation = actual - rate;
  458. printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
  459. fsdiv, div, actual, deviation);
  460. deviation = abs(deviation);
  461. if (deviation < best_deviation) {
  462. best_fs = fsdiv;
  463. best_div = div;
  464. best_rate = actual;
  465. best_deviation = deviation;
  466. }
  467. if (deviation == 0)
  468. break;
  469. }
  470. printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
  471. best_fs, best_div, best_rate);
  472. info->fs_div = best_fs;
  473. info->clk_div = best_div;
  474. return 0;
  475. }
  476. EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
  477. int s3c_i2sv2_probe(struct platform_device *pdev,
  478. struct snd_soc_dai *dai,
  479. struct s3c_i2sv2_info *i2s,
  480. unsigned long base)
  481. {
  482. struct device *dev = &pdev->dev;
  483. unsigned int iismod;
  484. i2s->dev = dev;
  485. /* record our i2s structure for later use in the callbacks */
  486. dai->private_data = i2s;
  487. if (!base) {
  488. struct resource *res = platform_get_resource(pdev,
  489. IORESOURCE_MEM,
  490. 0);
  491. if (!res) {
  492. dev_err(dev, "Unable to get register resource\n");
  493. return -ENXIO;
  494. }
  495. if (!request_mem_region(res->start, resource_size(res),
  496. "s3c64xx-i2s-v4")) {
  497. dev_err(dev, "Unable to request register region\n");
  498. return -EBUSY;
  499. }
  500. base = res->start;
  501. }
  502. i2s->regs = ioremap(base, 0x100);
  503. if (i2s->regs == NULL) {
  504. dev_err(dev, "cannot ioremap registers\n");
  505. return -ENXIO;
  506. }
  507. i2s->iis_pclk = clk_get(dev, "iis");
  508. if (i2s->iis_pclk == NULL) {
  509. dev_err(dev, "failed to get iis_clock\n");
  510. iounmap(i2s->regs);
  511. return -ENOENT;
  512. }
  513. clk_enable(i2s->iis_pclk);
  514. /* Mark ourselves as in TXRX mode so we can run through our cleanup
  515. * process without warnings. */
  516. iismod = readl(i2s->regs + S3C2412_IISMOD);
  517. iismod |= S3C2412_IISMOD_MODE_TXRX;
  518. writel(iismod, i2s->regs + S3C2412_IISMOD);
  519. s3c2412_snd_txctrl(i2s, 0);
  520. s3c2412_snd_rxctrl(i2s, 0);
  521. return 0;
  522. }
  523. EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
  524. #ifdef CONFIG_PM
  525. static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
  526. {
  527. struct s3c_i2sv2_info *i2s = to_info(dai);
  528. u32 iismod;
  529. if (dai->active) {
  530. i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
  531. i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
  532. i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
  533. /* some basic suspend checks */
  534. iismod = readl(i2s->regs + S3C2412_IISMOD);
  535. if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
  536. pr_warning("%s: RXDMA active?\n", __func__);
  537. if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
  538. pr_warning("%s: TXDMA active?\n", __func__);
  539. if (iismod & S3C2412_IISCON_IIS_ACTIVE)
  540. pr_warning("%s: IIS active\n", __func__);
  541. }
  542. return 0;
  543. }
  544. static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
  545. {
  546. struct s3c_i2sv2_info *i2s = to_info(dai);
  547. pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
  548. dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
  549. if (dai->active) {
  550. writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
  551. writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
  552. writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
  553. writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
  554. i2s->regs + S3C2412_IISFIC);
  555. ndelay(250);
  556. writel(0x0, i2s->regs + S3C2412_IISFIC);
  557. }
  558. return 0;
  559. }
  560. #else
  561. #define s3c2412_i2s_suspend NULL
  562. #define s3c2412_i2s_resume NULL
  563. #endif
  564. int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
  565. {
  566. struct snd_soc_dai_ops *ops = dai->ops;
  567. ops->trigger = s3c2412_i2s_trigger;
  568. ops->hw_params = s3c2412_i2s_hw_params;
  569. ops->set_fmt = s3c2412_i2s_set_fmt;
  570. ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
  571. dai->suspend = s3c2412_i2s_suspend;
  572. dai->resume = s3c2412_i2s_resume;
  573. return snd_soc_register_dai(dai);
  574. }
  575. EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
  576. MODULE_LICENSE("GPL");