s3c-i2s-v2.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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/delay.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <mach/dma.h>
  25. #include "regs-i2s-v2.h"
  26. #include "s3c-i2s-v2.h"
  27. #include "s3c-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 cpu_dai->private_data;
  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 *socdai)
  252. {
  253. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  254. struct snd_soc_dai_link *dai = rtd->dai;
  255. struct s3c_i2sv2_info *i2s = to_info(dai->cpu_dai);
  256. struct s3c_dma_params *dma_data;
  257. u32 iismod;
  258. pr_debug("Entered %s\n", __func__);
  259. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  260. dma_data = i2s->dma_playback;
  261. else
  262. dma_data = i2s->dma_capture;
  263. snd_soc_dai_set_dma_data(dai->cpu_dai, substream, dma_data);
  264. /* Working copies of register */
  265. iismod = readl(i2s->regs + S3C2412_IISMOD);
  266. pr_debug("%s: r: IISMOD: %x\n", __func__, iismod);
  267. iismod &= ~S3C64XX_IISMOD_BLC_MASK;
  268. /* Sample size */
  269. switch (params_format(params)) {
  270. case SNDRV_PCM_FORMAT_S8:
  271. iismod |= S3C64XX_IISMOD_BLC_8BIT;
  272. break;
  273. case SNDRV_PCM_FORMAT_S16_LE:
  274. break;
  275. case SNDRV_PCM_FORMAT_S24_LE:
  276. iismod |= S3C64XX_IISMOD_BLC_24BIT;
  277. break;
  278. }
  279. writel(iismod, i2s->regs + S3C2412_IISMOD);
  280. pr_debug("%s: w: IISMOD: %x\n", __func__, iismod);
  281. return 0;
  282. }
  283. static int s3c_i2sv2_set_sysclk(struct snd_soc_dai *cpu_dai,
  284. int clk_id, unsigned int freq, int dir)
  285. {
  286. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  287. u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
  288. pr_debug("Entered %s\n", __func__);
  289. pr_debug("%s r: IISMOD: %x\n", __func__, iismod);
  290. switch (clk_id) {
  291. case S3C_I2SV2_CLKSRC_PCLK:
  292. iismod &= ~S3C2412_IISMOD_IMS_SYSMUX;
  293. break;
  294. case S3C_I2SV2_CLKSRC_AUDIOBUS:
  295. iismod |= S3C2412_IISMOD_IMS_SYSMUX;
  296. break;
  297. case S3C_I2SV2_CLKSRC_CDCLK:
  298. /* Error if controller doesn't have the CDCLKCON bit */
  299. if (!(i2s->feature & S3C_FEATURE_CDCLKCON))
  300. return -EINVAL;
  301. switch (dir) {
  302. case SND_SOC_CLOCK_IN:
  303. iismod |= S3C64XX_IISMOD_CDCLKCON;
  304. break;
  305. case SND_SOC_CLOCK_OUT:
  306. iismod &= ~S3C64XX_IISMOD_CDCLKCON;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. writel(iismod, i2s->regs + S3C2412_IISMOD);
  316. pr_debug("%s w: IISMOD: %x\n", __func__, iismod);
  317. return 0;
  318. }
  319. static int s3c2412_i2s_trigger(struct snd_pcm_substream *substream, int cmd,
  320. struct snd_soc_dai *dai)
  321. {
  322. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  323. struct s3c_i2sv2_info *i2s = to_info(rtd->dai->cpu_dai);
  324. int capture = (substream->stream == SNDRV_PCM_STREAM_CAPTURE);
  325. unsigned long irqs;
  326. int ret = 0;
  327. struct s3c_dma_params *dma_data =
  328. snd_soc_dai_get_dma_data(rtd->dai->cpu_dai, substream);
  329. pr_debug("Entered %s\n", __func__);
  330. switch (cmd) {
  331. case SNDRV_PCM_TRIGGER_START:
  332. /* On start, ensure that the FIFOs are cleared and reset. */
  333. writel(capture ? S3C2412_IISFIC_RXFLUSH : S3C2412_IISFIC_TXFLUSH,
  334. i2s->regs + S3C2412_IISFIC);
  335. /* clear again, just in case */
  336. writel(0x0, i2s->regs + S3C2412_IISFIC);
  337. case SNDRV_PCM_TRIGGER_RESUME:
  338. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  339. if (!i2s->master) {
  340. ret = s3c2412_snd_lrsync(i2s);
  341. if (ret)
  342. goto exit_err;
  343. }
  344. local_irq_save(irqs);
  345. if (capture)
  346. s3c2412_snd_rxctrl(i2s, 1);
  347. else
  348. s3c2412_snd_txctrl(i2s, 1);
  349. local_irq_restore(irqs);
  350. /*
  351. * Load the next buffer to DMA to meet the reqirement
  352. * of the auto reload mechanism of S3C24XX.
  353. * This call won't bother S3C64XX.
  354. */
  355. s3c2410_dma_ctrl(dma_data->channel, S3C2410_DMAOP_STARTED);
  356. break;
  357. case SNDRV_PCM_TRIGGER_STOP:
  358. case SNDRV_PCM_TRIGGER_SUSPEND:
  359. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  360. local_irq_save(irqs);
  361. if (capture)
  362. s3c2412_snd_rxctrl(i2s, 0);
  363. else
  364. s3c2412_snd_txctrl(i2s, 0);
  365. local_irq_restore(irqs);
  366. break;
  367. default:
  368. ret = -EINVAL;
  369. break;
  370. }
  371. exit_err:
  372. return ret;
  373. }
  374. /*
  375. * Set S3C2412 Clock dividers
  376. */
  377. static int s3c2412_i2s_set_clkdiv(struct snd_soc_dai *cpu_dai,
  378. int div_id, int div)
  379. {
  380. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  381. u32 reg;
  382. pr_debug("%s(%p, %d, %d)\n", __func__, cpu_dai, div_id, div);
  383. switch (div_id) {
  384. case S3C_I2SV2_DIV_BCLK:
  385. switch (div) {
  386. case 16:
  387. div = S3C2412_IISMOD_BCLK_16FS;
  388. break;
  389. case 32:
  390. div = S3C2412_IISMOD_BCLK_32FS;
  391. break;
  392. case 24:
  393. div = S3C2412_IISMOD_BCLK_24FS;
  394. break;
  395. case 48:
  396. div = S3C2412_IISMOD_BCLK_48FS;
  397. break;
  398. default:
  399. return -EINVAL;
  400. }
  401. reg = readl(i2s->regs + S3C2412_IISMOD);
  402. reg &= ~S3C2412_IISMOD_BCLK_MASK;
  403. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  404. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  405. break;
  406. case S3C_I2SV2_DIV_RCLK:
  407. switch (div) {
  408. case 256:
  409. div = S3C2412_IISMOD_RCLK_256FS;
  410. break;
  411. case 384:
  412. div = S3C2412_IISMOD_RCLK_384FS;
  413. break;
  414. case 512:
  415. div = S3C2412_IISMOD_RCLK_512FS;
  416. break;
  417. case 768:
  418. div = S3C2412_IISMOD_RCLK_768FS;
  419. break;
  420. default:
  421. return -EINVAL;
  422. }
  423. reg = readl(i2s->regs + S3C2412_IISMOD);
  424. reg &= ~S3C2412_IISMOD_RCLK_MASK;
  425. writel(reg | div, i2s->regs + S3C2412_IISMOD);
  426. pr_debug("%s: MOD=%08x\n", __func__, readl(i2s->regs + S3C2412_IISMOD));
  427. break;
  428. case S3C_I2SV2_DIV_PRESCALER:
  429. if (div >= 0) {
  430. writel((div << 8) | S3C2412_IISPSR_PSREN,
  431. i2s->regs + S3C2412_IISPSR);
  432. } else {
  433. writel(0x0, i2s->regs + S3C2412_IISPSR);
  434. }
  435. pr_debug("%s: PSR=%08x\n", __func__, readl(i2s->regs + S3C2412_IISPSR));
  436. break;
  437. default:
  438. return -EINVAL;
  439. }
  440. return 0;
  441. }
  442. static snd_pcm_sframes_t s3c2412_i2s_delay(struct snd_pcm_substream *substream,
  443. struct snd_soc_dai *dai)
  444. {
  445. struct s3c_i2sv2_info *i2s = to_info(dai);
  446. u32 reg = readl(i2s->regs + S3C2412_IISFIC);
  447. snd_pcm_sframes_t delay;
  448. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  449. delay = S3C2412_IISFIC_TXCOUNT(reg);
  450. else
  451. delay = S3C2412_IISFIC_RXCOUNT(reg);
  452. return delay;
  453. }
  454. struct clk *s3c_i2sv2_get_clock(struct snd_soc_dai *cpu_dai)
  455. {
  456. struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
  457. u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
  458. if (iismod & S3C2412_IISMOD_IMS_SYSMUX)
  459. return i2s->iis_cclk;
  460. else
  461. return i2s->iis_pclk;
  462. }
  463. EXPORT_SYMBOL_GPL(s3c_i2sv2_get_clock);
  464. /* default table of all avaialable root fs divisors */
  465. static unsigned int iis_fs_tab[] = { 256, 512, 384, 768 };
  466. int s3c_i2sv2_iis_calc_rate(struct s3c_i2sv2_rate_calc *info,
  467. unsigned int *fstab,
  468. unsigned int rate, struct clk *clk)
  469. {
  470. unsigned long clkrate = clk_get_rate(clk);
  471. unsigned int div;
  472. unsigned int fsclk;
  473. unsigned int actual;
  474. unsigned int fs;
  475. unsigned int fsdiv;
  476. signed int deviation = 0;
  477. unsigned int best_fs = 0;
  478. unsigned int best_div = 0;
  479. unsigned int best_rate = 0;
  480. unsigned int best_deviation = INT_MAX;
  481. pr_debug("Input clock rate %ldHz\n", clkrate);
  482. if (fstab == NULL)
  483. fstab = iis_fs_tab;
  484. for (fs = 0; fs < ARRAY_SIZE(iis_fs_tab); fs++) {
  485. fsdiv = iis_fs_tab[fs];
  486. fsclk = clkrate / fsdiv;
  487. div = fsclk / rate;
  488. if ((fsclk % rate) > (rate / 2))
  489. div++;
  490. if (div <= 1)
  491. continue;
  492. actual = clkrate / (fsdiv * div);
  493. deviation = actual - rate;
  494. printk(KERN_DEBUG "%ufs: div %u => result %u, deviation %d\n",
  495. fsdiv, div, actual, deviation);
  496. deviation = abs(deviation);
  497. if (deviation < best_deviation) {
  498. best_fs = fsdiv;
  499. best_div = div;
  500. best_rate = actual;
  501. best_deviation = deviation;
  502. }
  503. if (deviation == 0)
  504. break;
  505. }
  506. printk(KERN_DEBUG "best: fs=%u, div=%u, rate=%u\n",
  507. best_fs, best_div, best_rate);
  508. info->fs_div = best_fs;
  509. info->clk_div = best_div;
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(s3c_i2sv2_iis_calc_rate);
  513. int s3c_i2sv2_probe(struct platform_device *pdev,
  514. struct snd_soc_dai *dai,
  515. struct s3c_i2sv2_info *i2s,
  516. unsigned long base)
  517. {
  518. struct device *dev = &pdev->dev;
  519. unsigned int iismod;
  520. i2s->dev = dev;
  521. /* record our i2s structure for later use in the callbacks */
  522. dai->private_data = i2s;
  523. if (!base) {
  524. struct resource *res = platform_get_resource(pdev,
  525. IORESOURCE_MEM,
  526. 0);
  527. if (!res) {
  528. dev_err(dev, "Unable to get register resource\n");
  529. return -ENXIO;
  530. }
  531. if (!request_mem_region(res->start, resource_size(res),
  532. "s3c64xx-i2s-v4")) {
  533. dev_err(dev, "Unable to request register region\n");
  534. return -EBUSY;
  535. }
  536. base = res->start;
  537. }
  538. i2s->regs = ioremap(base, 0x100);
  539. if (i2s->regs == NULL) {
  540. dev_err(dev, "cannot ioremap registers\n");
  541. return -ENXIO;
  542. }
  543. i2s->iis_pclk = clk_get(dev, "iis");
  544. if (IS_ERR(i2s->iis_pclk)) {
  545. dev_err(dev, "failed to get iis_clock\n");
  546. iounmap(i2s->regs);
  547. return -ENOENT;
  548. }
  549. clk_enable(i2s->iis_pclk);
  550. /* Mark ourselves as in TXRX mode so we can run through our cleanup
  551. * process without warnings. */
  552. iismod = readl(i2s->regs + S3C2412_IISMOD);
  553. iismod |= S3C2412_IISMOD_MODE_TXRX;
  554. writel(iismod, i2s->regs + S3C2412_IISMOD);
  555. s3c2412_snd_txctrl(i2s, 0);
  556. s3c2412_snd_rxctrl(i2s, 0);
  557. return 0;
  558. }
  559. EXPORT_SYMBOL_GPL(s3c_i2sv2_probe);
  560. #ifdef CONFIG_PM
  561. static int s3c2412_i2s_suspend(struct snd_soc_dai *dai)
  562. {
  563. struct s3c_i2sv2_info *i2s = to_info(dai);
  564. u32 iismod;
  565. if (dai->active) {
  566. i2s->suspend_iismod = readl(i2s->regs + S3C2412_IISMOD);
  567. i2s->suspend_iiscon = readl(i2s->regs + S3C2412_IISCON);
  568. i2s->suspend_iispsr = readl(i2s->regs + S3C2412_IISPSR);
  569. /* some basic suspend checks */
  570. iismod = readl(i2s->regs + S3C2412_IISMOD);
  571. if (iismod & S3C2412_IISCON_RXDMA_ACTIVE)
  572. pr_warning("%s: RXDMA active?\n", __func__);
  573. if (iismod & S3C2412_IISCON_TXDMA_ACTIVE)
  574. pr_warning("%s: TXDMA active?\n", __func__);
  575. if (iismod & S3C2412_IISCON_IIS_ACTIVE)
  576. pr_warning("%s: IIS active\n", __func__);
  577. }
  578. return 0;
  579. }
  580. static int s3c2412_i2s_resume(struct snd_soc_dai *dai)
  581. {
  582. struct s3c_i2sv2_info *i2s = to_info(dai);
  583. pr_info("dai_active %d, IISMOD %08x, IISCON %08x\n",
  584. dai->active, i2s->suspend_iismod, i2s->suspend_iiscon);
  585. if (dai->active) {
  586. writel(i2s->suspend_iiscon, i2s->regs + S3C2412_IISCON);
  587. writel(i2s->suspend_iismod, i2s->regs + S3C2412_IISMOD);
  588. writel(i2s->suspend_iispsr, i2s->regs + S3C2412_IISPSR);
  589. writel(S3C2412_IISFIC_RXFLUSH | S3C2412_IISFIC_TXFLUSH,
  590. i2s->regs + S3C2412_IISFIC);
  591. ndelay(250);
  592. writel(0x0, i2s->regs + S3C2412_IISFIC);
  593. }
  594. return 0;
  595. }
  596. #else
  597. #define s3c2412_i2s_suspend NULL
  598. #define s3c2412_i2s_resume NULL
  599. #endif
  600. int s3c_i2sv2_register_dai(struct snd_soc_dai *dai)
  601. {
  602. struct snd_soc_dai_ops *ops = dai->ops;
  603. ops->trigger = s3c2412_i2s_trigger;
  604. if (!ops->hw_params)
  605. ops->hw_params = s3c_i2sv2_hw_params;
  606. ops->set_fmt = s3c2412_i2s_set_fmt;
  607. ops->set_clkdiv = s3c2412_i2s_set_clkdiv;
  608. ops->set_sysclk = s3c_i2sv2_set_sysclk;
  609. /* Allow overriding by (for example) IISv4 */
  610. if (!ops->delay)
  611. ops->delay = s3c2412_i2s_delay;
  612. dai->suspend = s3c2412_i2s_suspend;
  613. dai->resume = s3c2412_i2s_resume;
  614. return snd_soc_register_dai(dai);
  615. }
  616. EXPORT_SYMBOL_GPL(s3c_i2sv2_register_dai);
  617. MODULE_LICENSE("GPL");