s3c-i2s-v2.c 18 KB

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