s3c-i2s-v2.c 17 KB

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