pcm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /* sound/soc/samsung/pcm.c
  2. *
  3. * ALSA SoC Audio Layer - S3C PCM-Controller driver
  4. *
  5. * Copyright (c) 2009 Samsung Electronics Co. Ltd
  6. * Author: Jaswinder Singh <jassisinghbrar@gmail.com>
  7. * based upon I2S drivers by Ben Dooks.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/pm_runtime.h>
  17. #include <sound/soc.h>
  18. #include <sound/pcm_params.h>
  19. #include <plat/audio.h>
  20. #include <plat/dma.h>
  21. #include "dma.h"
  22. #include "pcm.h"
  23. /*Register Offsets */
  24. #define S3C_PCM_CTL 0x00
  25. #define S3C_PCM_CLKCTL 0x04
  26. #define S3C_PCM_TXFIFO 0x08
  27. #define S3C_PCM_RXFIFO 0x0C
  28. #define S3C_PCM_IRQCTL 0x10
  29. #define S3C_PCM_IRQSTAT 0x14
  30. #define S3C_PCM_FIFOSTAT 0x18
  31. #define S3C_PCM_CLRINT 0x20
  32. /* PCM_CTL Bit-Fields */
  33. #define S3C_PCM_CTL_TXDIPSTICK_MASK 0x3f
  34. #define S3C_PCM_CTL_TXDIPSTICK_SHIFT 13
  35. #define S3C_PCM_CTL_RXDIPSTICK_MASK 0x3f
  36. #define S3C_PCM_CTL_RXDIPSTICK_SHIFT 7
  37. #define S3C_PCM_CTL_TXDMA_EN (0x1 << 6)
  38. #define S3C_PCM_CTL_RXDMA_EN (0x1 << 5)
  39. #define S3C_PCM_CTL_TXMSB_AFTER_FSYNC (0x1 << 4)
  40. #define S3C_PCM_CTL_RXMSB_AFTER_FSYNC (0x1 << 3)
  41. #define S3C_PCM_CTL_TXFIFO_EN (0x1 << 2)
  42. #define S3C_PCM_CTL_RXFIFO_EN (0x1 << 1)
  43. #define S3C_PCM_CTL_ENABLE (0x1 << 0)
  44. /* PCM_CLKCTL Bit-Fields */
  45. #define S3C_PCM_CLKCTL_SERCLK_EN (0x1 << 19)
  46. #define S3C_PCM_CLKCTL_SERCLKSEL_PCLK (0x1 << 18)
  47. #define S3C_PCM_CLKCTL_SCLKDIV_MASK 0x1ff
  48. #define S3C_PCM_CLKCTL_SYNCDIV_MASK 0x1ff
  49. #define S3C_PCM_CLKCTL_SCLKDIV_SHIFT 9
  50. #define S3C_PCM_CLKCTL_SYNCDIV_SHIFT 0
  51. /* PCM_TXFIFO Bit-Fields */
  52. #define S3C_PCM_TXFIFO_DVALID (0x1 << 16)
  53. #define S3C_PCM_TXFIFO_DATA_MSK (0xffff << 0)
  54. /* PCM_RXFIFO Bit-Fields */
  55. #define S3C_PCM_RXFIFO_DVALID (0x1 << 16)
  56. #define S3C_PCM_RXFIFO_DATA_MSK (0xffff << 0)
  57. /* PCM_IRQCTL Bit-Fields */
  58. #define S3C_PCM_IRQCTL_IRQEN (0x1 << 14)
  59. #define S3C_PCM_IRQCTL_WRDEN (0x1 << 12)
  60. #define S3C_PCM_IRQCTL_TXEMPTYEN (0x1 << 11)
  61. #define S3C_PCM_IRQCTL_TXALMSTEMPTYEN (0x1 << 10)
  62. #define S3C_PCM_IRQCTL_TXFULLEN (0x1 << 9)
  63. #define S3C_PCM_IRQCTL_TXALMSTFULLEN (0x1 << 8)
  64. #define S3C_PCM_IRQCTL_TXSTARVEN (0x1 << 7)
  65. #define S3C_PCM_IRQCTL_TXERROVRFLEN (0x1 << 6)
  66. #define S3C_PCM_IRQCTL_RXEMPTEN (0x1 << 5)
  67. #define S3C_PCM_IRQCTL_RXALMSTEMPTEN (0x1 << 4)
  68. #define S3C_PCM_IRQCTL_RXFULLEN (0x1 << 3)
  69. #define S3C_PCM_IRQCTL_RXALMSTFULLEN (0x1 << 2)
  70. #define S3C_PCM_IRQCTL_RXSTARVEN (0x1 << 1)
  71. #define S3C_PCM_IRQCTL_RXERROVRFLEN (0x1 << 0)
  72. /* PCM_IRQSTAT Bit-Fields */
  73. #define S3C_PCM_IRQSTAT_IRQPND (0x1 << 13)
  74. #define S3C_PCM_IRQSTAT_WRD_XFER (0x1 << 12)
  75. #define S3C_PCM_IRQSTAT_TXEMPTY (0x1 << 11)
  76. #define S3C_PCM_IRQSTAT_TXALMSTEMPTY (0x1 << 10)
  77. #define S3C_PCM_IRQSTAT_TXFULL (0x1 << 9)
  78. #define S3C_PCM_IRQSTAT_TXALMSTFULL (0x1 << 8)
  79. #define S3C_PCM_IRQSTAT_TXSTARV (0x1 << 7)
  80. #define S3C_PCM_IRQSTAT_TXERROVRFL (0x1 << 6)
  81. #define S3C_PCM_IRQSTAT_RXEMPT (0x1 << 5)
  82. #define S3C_PCM_IRQSTAT_RXALMSTEMPT (0x1 << 4)
  83. #define S3C_PCM_IRQSTAT_RXFULL (0x1 << 3)
  84. #define S3C_PCM_IRQSTAT_RXALMSTFULL (0x1 << 2)
  85. #define S3C_PCM_IRQSTAT_RXSTARV (0x1 << 1)
  86. #define S3C_PCM_IRQSTAT_RXERROVRFL (0x1 << 0)
  87. /* PCM_FIFOSTAT Bit-Fields */
  88. #define S3C_PCM_FIFOSTAT_TXCNT_MSK (0x3f << 14)
  89. #define S3C_PCM_FIFOSTAT_TXFIFOEMPTY (0x1 << 13)
  90. #define S3C_PCM_FIFOSTAT_TXFIFOALMSTEMPTY (0x1 << 12)
  91. #define S3C_PCM_FIFOSTAT_TXFIFOFULL (0x1 << 11)
  92. #define S3C_PCM_FIFOSTAT_TXFIFOALMSTFULL (0x1 << 10)
  93. #define S3C_PCM_FIFOSTAT_RXCNT_MSK (0x3f << 4)
  94. #define S3C_PCM_FIFOSTAT_RXFIFOEMPTY (0x1 << 3)
  95. #define S3C_PCM_FIFOSTAT_RXFIFOALMSTEMPTY (0x1 << 2)
  96. #define S3C_PCM_FIFOSTAT_RXFIFOFULL (0x1 << 1)
  97. #define S3C_PCM_FIFOSTAT_RXFIFOALMSTFULL (0x1 << 0)
  98. /**
  99. * struct s3c_pcm_info - S3C PCM Controller information
  100. * @dev: The parent device passed to use from the probe.
  101. * @regs: The pointer to the device register block.
  102. * @dma_playback: DMA information for playback channel.
  103. * @dma_capture: DMA information for capture channel.
  104. */
  105. struct s3c_pcm_info {
  106. spinlock_t lock;
  107. struct device *dev;
  108. void __iomem *regs;
  109. unsigned int sclk_per_fs;
  110. /* Whether to keep PCMSCLK enabled even when idle(no active xfer) */
  111. unsigned int idleclk;
  112. struct clk *pclk;
  113. struct clk *cclk;
  114. struct s3c_dma_params *dma_playback;
  115. struct s3c_dma_params *dma_capture;
  116. };
  117. static struct s3c2410_dma_client s3c_pcm_dma_client_out = {
  118. .name = "PCM Stereo out"
  119. };
  120. static struct s3c2410_dma_client s3c_pcm_dma_client_in = {
  121. .name = "PCM Stereo in"
  122. };
  123. static struct s3c_dma_params s3c_pcm_stereo_out[] = {
  124. [0] = {
  125. .client = &s3c_pcm_dma_client_out,
  126. .dma_size = 4,
  127. },
  128. [1] = {
  129. .client = &s3c_pcm_dma_client_out,
  130. .dma_size = 4,
  131. },
  132. };
  133. static struct s3c_dma_params s3c_pcm_stereo_in[] = {
  134. [0] = {
  135. .client = &s3c_pcm_dma_client_in,
  136. .dma_size = 4,
  137. },
  138. [1] = {
  139. .client = &s3c_pcm_dma_client_in,
  140. .dma_size = 4,
  141. },
  142. };
  143. static struct s3c_pcm_info s3c_pcm[2];
  144. static void s3c_pcm_snd_txctrl(struct s3c_pcm_info *pcm, int on)
  145. {
  146. void __iomem *regs = pcm->regs;
  147. u32 ctl, clkctl;
  148. clkctl = readl(regs + S3C_PCM_CLKCTL);
  149. ctl = readl(regs + S3C_PCM_CTL);
  150. ctl &= ~(S3C_PCM_CTL_TXDIPSTICK_MASK
  151. << S3C_PCM_CTL_TXDIPSTICK_SHIFT);
  152. if (on) {
  153. ctl |= S3C_PCM_CTL_TXDMA_EN;
  154. ctl |= S3C_PCM_CTL_TXFIFO_EN;
  155. ctl |= S3C_PCM_CTL_ENABLE;
  156. ctl |= (0x4<<S3C_PCM_CTL_TXDIPSTICK_SHIFT);
  157. clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
  158. } else {
  159. ctl &= ~S3C_PCM_CTL_TXDMA_EN;
  160. ctl &= ~S3C_PCM_CTL_TXFIFO_EN;
  161. if (!(ctl & S3C_PCM_CTL_RXFIFO_EN)) {
  162. ctl &= ~S3C_PCM_CTL_ENABLE;
  163. if (!pcm->idleclk)
  164. clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
  165. }
  166. }
  167. writel(clkctl, regs + S3C_PCM_CLKCTL);
  168. writel(ctl, regs + S3C_PCM_CTL);
  169. }
  170. static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info *pcm, int on)
  171. {
  172. void __iomem *regs = pcm->regs;
  173. u32 ctl, clkctl;
  174. ctl = readl(regs + S3C_PCM_CTL);
  175. clkctl = readl(regs + S3C_PCM_CLKCTL);
  176. ctl &= ~(S3C_PCM_CTL_RXDIPSTICK_MASK
  177. << S3C_PCM_CTL_RXDIPSTICK_SHIFT);
  178. if (on) {
  179. ctl |= S3C_PCM_CTL_RXDMA_EN;
  180. ctl |= S3C_PCM_CTL_RXFIFO_EN;
  181. ctl |= S3C_PCM_CTL_ENABLE;
  182. ctl |= (0x20<<S3C_PCM_CTL_RXDIPSTICK_SHIFT);
  183. clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
  184. } else {
  185. ctl &= ~S3C_PCM_CTL_RXDMA_EN;
  186. ctl &= ~S3C_PCM_CTL_RXFIFO_EN;
  187. if (!(ctl & S3C_PCM_CTL_TXFIFO_EN)) {
  188. ctl &= ~S3C_PCM_CTL_ENABLE;
  189. if (!pcm->idleclk)
  190. clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
  191. }
  192. }
  193. writel(clkctl, regs + S3C_PCM_CLKCTL);
  194. writel(ctl, regs + S3C_PCM_CTL);
  195. }
  196. static int s3c_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  200. struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  201. unsigned long flags;
  202. dev_dbg(pcm->dev, "Entered %s\n", __func__);
  203. switch (cmd) {
  204. case SNDRV_PCM_TRIGGER_START:
  205. case SNDRV_PCM_TRIGGER_RESUME:
  206. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  207. spin_lock_irqsave(&pcm->lock, flags);
  208. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  209. s3c_pcm_snd_rxctrl(pcm, 1);
  210. else
  211. s3c_pcm_snd_txctrl(pcm, 1);
  212. spin_unlock_irqrestore(&pcm->lock, flags);
  213. break;
  214. case SNDRV_PCM_TRIGGER_STOP:
  215. case SNDRV_PCM_TRIGGER_SUSPEND:
  216. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  217. spin_lock_irqsave(&pcm->lock, flags);
  218. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  219. s3c_pcm_snd_rxctrl(pcm, 0);
  220. else
  221. s3c_pcm_snd_txctrl(pcm, 0);
  222. spin_unlock_irqrestore(&pcm->lock, flags);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. return 0;
  228. }
  229. static int s3c_pcm_hw_params(struct snd_pcm_substream *substream,
  230. struct snd_pcm_hw_params *params,
  231. struct snd_soc_dai *socdai)
  232. {
  233. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  234. struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
  235. struct s3c_dma_params *dma_data;
  236. void __iomem *regs = pcm->regs;
  237. struct clk *clk;
  238. int sclk_div, sync_div;
  239. unsigned long flags;
  240. u32 clkctl;
  241. dev_dbg(pcm->dev, "Entered %s\n", __func__);
  242. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  243. dma_data = pcm->dma_playback;
  244. else
  245. dma_data = pcm->dma_capture;
  246. snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
  247. /* Strictly check for sample size */
  248. switch (params_format(params)) {
  249. case SNDRV_PCM_FORMAT_S16_LE:
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. spin_lock_irqsave(&pcm->lock, flags);
  255. /* Get hold of the PCMSOURCE_CLK */
  256. clkctl = readl(regs + S3C_PCM_CLKCTL);
  257. if (clkctl & S3C_PCM_CLKCTL_SERCLKSEL_PCLK)
  258. clk = pcm->pclk;
  259. else
  260. clk = pcm->cclk;
  261. /* Set the SCLK divider */
  262. sclk_div = clk_get_rate(clk) / pcm->sclk_per_fs /
  263. params_rate(params) / 2 - 1;
  264. clkctl &= ~(S3C_PCM_CLKCTL_SCLKDIV_MASK
  265. << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
  266. clkctl |= ((sclk_div & S3C_PCM_CLKCTL_SCLKDIV_MASK)
  267. << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
  268. /* Set the SYNC divider */
  269. sync_div = pcm->sclk_per_fs - 1;
  270. clkctl &= ~(S3C_PCM_CLKCTL_SYNCDIV_MASK
  271. << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
  272. clkctl |= ((sync_div & S3C_PCM_CLKCTL_SYNCDIV_MASK)
  273. << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
  274. writel(clkctl, regs + S3C_PCM_CLKCTL);
  275. spin_unlock_irqrestore(&pcm->lock, flags);
  276. dev_dbg(pcm->dev, "PCMSOURCE_CLK-%lu SCLK=%ufs SCLK_DIV=%d SYNC_DIV=%d\n",
  277. clk_get_rate(clk), pcm->sclk_per_fs,
  278. sclk_div, sync_div);
  279. return 0;
  280. }
  281. static int s3c_pcm_set_fmt(struct snd_soc_dai *cpu_dai,
  282. unsigned int fmt)
  283. {
  284. struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
  285. void __iomem *regs = pcm->regs;
  286. unsigned long flags;
  287. int ret = 0;
  288. u32 ctl;
  289. dev_dbg(pcm->dev, "Entered %s\n", __func__);
  290. spin_lock_irqsave(&pcm->lock, flags);
  291. ctl = readl(regs + S3C_PCM_CTL);
  292. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  293. case SND_SOC_DAIFMT_IB_NF:
  294. /* Nothing to do, IB_NF by default */
  295. break;
  296. default:
  297. dev_err(pcm->dev, "Unsupported clock inversion!\n");
  298. ret = -EINVAL;
  299. goto exit;
  300. }
  301. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  302. case SND_SOC_DAIFMT_CBS_CFS:
  303. /* Nothing to do, Master by default */
  304. break;
  305. default:
  306. dev_err(pcm->dev, "Unsupported master/slave format!\n");
  307. ret = -EINVAL;
  308. goto exit;
  309. }
  310. switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
  311. case SND_SOC_DAIFMT_CONT:
  312. pcm->idleclk = 1;
  313. break;
  314. case SND_SOC_DAIFMT_GATED:
  315. pcm->idleclk = 0;
  316. break;
  317. default:
  318. dev_err(pcm->dev, "Invalid Clock gating request!\n");
  319. ret = -EINVAL;
  320. goto exit;
  321. }
  322. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  323. case SND_SOC_DAIFMT_DSP_A:
  324. ctl |= S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
  325. ctl |= S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
  326. break;
  327. case SND_SOC_DAIFMT_DSP_B:
  328. ctl &= ~S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
  329. ctl &= ~S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
  330. break;
  331. default:
  332. dev_err(pcm->dev, "Unsupported data format!\n");
  333. ret = -EINVAL;
  334. goto exit;
  335. }
  336. writel(ctl, regs + S3C_PCM_CTL);
  337. exit:
  338. spin_unlock_irqrestore(&pcm->lock, flags);
  339. return ret;
  340. }
  341. static int s3c_pcm_set_clkdiv(struct snd_soc_dai *cpu_dai,
  342. int div_id, int div)
  343. {
  344. struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
  345. switch (div_id) {
  346. case S3C_PCM_SCLK_PER_FS:
  347. pcm->sclk_per_fs = div;
  348. break;
  349. default:
  350. return -EINVAL;
  351. }
  352. return 0;
  353. }
  354. static int s3c_pcm_set_sysclk(struct snd_soc_dai *cpu_dai,
  355. int clk_id, unsigned int freq, int dir)
  356. {
  357. struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
  358. void __iomem *regs = pcm->regs;
  359. u32 clkctl = readl(regs + S3C_PCM_CLKCTL);
  360. switch (clk_id) {
  361. case S3C_PCM_CLKSRC_PCLK:
  362. clkctl |= S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
  363. break;
  364. case S3C_PCM_CLKSRC_MUX:
  365. clkctl &= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
  366. if (clk_get_rate(pcm->cclk) != freq)
  367. clk_set_rate(pcm->cclk, freq);
  368. break;
  369. default:
  370. return -EINVAL;
  371. }
  372. writel(clkctl, regs + S3C_PCM_CLKCTL);
  373. return 0;
  374. }
  375. static const struct snd_soc_dai_ops s3c_pcm_dai_ops = {
  376. .set_sysclk = s3c_pcm_set_sysclk,
  377. .set_clkdiv = s3c_pcm_set_clkdiv,
  378. .trigger = s3c_pcm_trigger,
  379. .hw_params = s3c_pcm_hw_params,
  380. .set_fmt = s3c_pcm_set_fmt,
  381. };
  382. #define S3C_PCM_RATES SNDRV_PCM_RATE_8000_96000
  383. #define S3C_PCM_DAI_DECLARE \
  384. .symmetric_rates = 1, \
  385. .ops = &s3c_pcm_dai_ops, \
  386. .playback = { \
  387. .channels_min = 2, \
  388. .channels_max = 2, \
  389. .rates = S3C_PCM_RATES, \
  390. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  391. }, \
  392. .capture = { \
  393. .channels_min = 2, \
  394. .channels_max = 2, \
  395. .rates = S3C_PCM_RATES, \
  396. .formats = SNDRV_PCM_FMTBIT_S16_LE, \
  397. }
  398. static struct snd_soc_dai_driver s3c_pcm_dai[] = {
  399. [0] = {
  400. .name = "samsung-pcm.0",
  401. S3C_PCM_DAI_DECLARE,
  402. },
  403. [1] = {
  404. .name = "samsung-pcm.1",
  405. S3C_PCM_DAI_DECLARE,
  406. },
  407. };
  408. static __devinit int s3c_pcm_dev_probe(struct platform_device *pdev)
  409. {
  410. struct s3c_pcm_info *pcm;
  411. struct resource *mem_res, *dmatx_res, *dmarx_res;
  412. struct s3c_audio_pdata *pcm_pdata;
  413. int ret;
  414. /* Check for valid device index */
  415. if ((pdev->id < 0) || pdev->id >= ARRAY_SIZE(s3c_pcm)) {
  416. dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
  417. return -EINVAL;
  418. }
  419. pcm_pdata = pdev->dev.platform_data;
  420. /* Check for availability of necessary resource */
  421. dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  422. if (!dmatx_res) {
  423. dev_err(&pdev->dev, "Unable to get PCM-TX dma resource\n");
  424. return -ENXIO;
  425. }
  426. dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  427. if (!dmarx_res) {
  428. dev_err(&pdev->dev, "Unable to get PCM-RX dma resource\n");
  429. return -ENXIO;
  430. }
  431. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  432. if (!mem_res) {
  433. dev_err(&pdev->dev, "Unable to get register resource\n");
  434. return -ENXIO;
  435. }
  436. if (pcm_pdata && pcm_pdata->cfg_gpio && pcm_pdata->cfg_gpio(pdev)) {
  437. dev_err(&pdev->dev, "Unable to configure gpio\n");
  438. return -EINVAL;
  439. }
  440. pcm = &s3c_pcm[pdev->id];
  441. pcm->dev = &pdev->dev;
  442. spin_lock_init(&pcm->lock);
  443. /* Default is 128fs */
  444. pcm->sclk_per_fs = 128;
  445. pcm->cclk = clk_get(&pdev->dev, "audio-bus");
  446. if (IS_ERR(pcm->cclk)) {
  447. dev_err(&pdev->dev, "failed to get audio-bus\n");
  448. ret = PTR_ERR(pcm->cclk);
  449. goto err1;
  450. }
  451. clk_enable(pcm->cclk);
  452. /* record our pcm structure for later use in the callbacks */
  453. dev_set_drvdata(&pdev->dev, pcm);
  454. if (!request_mem_region(mem_res->start,
  455. resource_size(mem_res), "samsung-pcm")) {
  456. dev_err(&pdev->dev, "Unable to request register region\n");
  457. ret = -EBUSY;
  458. goto err2;
  459. }
  460. pcm->regs = ioremap(mem_res->start, 0x100);
  461. if (pcm->regs == NULL) {
  462. dev_err(&pdev->dev, "cannot ioremap registers\n");
  463. ret = -ENXIO;
  464. goto err3;
  465. }
  466. pcm->pclk = clk_get(&pdev->dev, "pcm");
  467. if (IS_ERR(pcm->pclk)) {
  468. dev_err(&pdev->dev, "failed to get pcm_clock\n");
  469. ret = -ENOENT;
  470. goto err4;
  471. }
  472. clk_enable(pcm->pclk);
  473. s3c_pcm_stereo_in[pdev->id].dma_addr = mem_res->start
  474. + S3C_PCM_RXFIFO;
  475. s3c_pcm_stereo_out[pdev->id].dma_addr = mem_res->start
  476. + S3C_PCM_TXFIFO;
  477. s3c_pcm_stereo_in[pdev->id].channel = dmarx_res->start;
  478. s3c_pcm_stereo_out[pdev->id].channel = dmatx_res->start;
  479. pcm->dma_capture = &s3c_pcm_stereo_in[pdev->id];
  480. pcm->dma_playback = &s3c_pcm_stereo_out[pdev->id];
  481. pm_runtime_enable(&pdev->dev);
  482. ret = snd_soc_register_dai(&pdev->dev, &s3c_pcm_dai[pdev->id]);
  483. if (ret != 0) {
  484. dev_err(&pdev->dev, "failed to get register DAI: %d\n", ret);
  485. goto err5;
  486. }
  487. return 0;
  488. err5:
  489. clk_disable(pcm->pclk);
  490. clk_put(pcm->pclk);
  491. err4:
  492. iounmap(pcm->regs);
  493. err3:
  494. release_mem_region(mem_res->start, resource_size(mem_res));
  495. err2:
  496. clk_disable(pcm->cclk);
  497. clk_put(pcm->cclk);
  498. err1:
  499. return ret;
  500. }
  501. static __devexit int s3c_pcm_dev_remove(struct platform_device *pdev)
  502. {
  503. struct s3c_pcm_info *pcm = &s3c_pcm[pdev->id];
  504. struct resource *mem_res;
  505. snd_soc_unregister_dai(&pdev->dev);
  506. pm_runtime_disable(&pdev->dev);
  507. iounmap(pcm->regs);
  508. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  509. release_mem_region(mem_res->start, resource_size(mem_res));
  510. clk_disable(pcm->cclk);
  511. clk_disable(pcm->pclk);
  512. clk_put(pcm->pclk);
  513. clk_put(pcm->cclk);
  514. return 0;
  515. }
  516. static struct platform_driver s3c_pcm_driver = {
  517. .probe = s3c_pcm_dev_probe,
  518. .remove = __devexit_p(s3c_pcm_dev_remove),
  519. .driver = {
  520. .name = "samsung-pcm",
  521. .owner = THIS_MODULE,
  522. },
  523. };
  524. module_platform_driver(s3c_pcm_driver);
  525. /* Module information */
  526. MODULE_AUTHOR("Jaswinder Singh, <jassisinghbrar@gmail.com>");
  527. MODULE_DESCRIPTION("S3C PCM Controller Driver");
  528. MODULE_LICENSE("GPL");
  529. MODULE_ALIAS("platform:samsung-pcm");