s3c-i2s-v2.c 18 KB

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