s3c-i2s-v2.c 17 KB

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