blob: 091db5dce07e97bc991b9075abae4f6e15a4058d [file] [log] [blame]
Jassi Brar5033f432010-11-22 15:37:25 +09001/* sound/soc/samsung/pcm.c
Jassi Brar357a1db2009-11-17 16:54:03 +09002 *
3 * ALSA SoC Audio Layer - S3C PCM-Controller driver
4 *
5 * Copyright (c) 2009 Samsung Electronics Co. Ltd
6 * Author: Jaswinder Singh <jassi.brar@samsung.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
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/delay.h>
18#include <linux/clk.h>
19#include <linux/kernel.h>
20#include <linux/gpio.h>
21#include <linux/io.h>
22
23#include <sound/core.h>
24#include <sound/pcm.h>
25#include <sound/pcm_params.h>
26#include <sound/initval.h>
27#include <sound/soc.h>
28
29#include <plat/audio.h>
30#include <plat/dma.h>
31
Jassi Brar4b640cf2010-11-22 15:35:57 +090032#include "dma.h"
Jassi Brar158a78282010-11-22 15:36:44 +090033#include "pcm.h"
Jassi Brar357a1db2009-11-17 16:54:03 +090034
Seungwhan Youn9c6df192011-01-07 13:57:23 +090035/*Register Offsets */
36#define S3C_PCM_CTL 0x00
37#define S3C_PCM_CLKCTL 0x04
38#define S3C_PCM_TXFIFO 0x08
39#define S3C_PCM_RXFIFO 0x0C
40#define S3C_PCM_IRQCTL 0x10
41#define S3C_PCM_IRQSTAT 0x14
42#define S3C_PCM_FIFOSTAT 0x18
43#define S3C_PCM_CLRINT 0x20
44
45/* PCM_CTL Bit-Fields */
46#define S3C_PCM_CTL_TXDIPSTICK_MASK 0x3f
47#define S3C_PCM_CTL_TXDIPSTICK_SHIFT 13
48#define S3C_PCM_CTL_RXDIPSTICK_MASK 0x3f
49#define S3C_PCM_CTL_RXDIPSTICK_SHIFT 7
50#define S3C_PCM_CTL_TXDMA_EN (0x1 << 6)
51#define S3C_PCM_CTL_RXDMA_EN (0x1 << 5)
52#define S3C_PCM_CTL_TXMSB_AFTER_FSYNC (0x1 << 4)
53#define S3C_PCM_CTL_RXMSB_AFTER_FSYNC (0x1 << 3)
54#define S3C_PCM_CTL_TXFIFO_EN (0x1 << 2)
55#define S3C_PCM_CTL_RXFIFO_EN (0x1 << 1)
56#define S3C_PCM_CTL_ENABLE (0x1 << 0)
57
58/* PCM_CLKCTL Bit-Fields */
59#define S3C_PCM_CLKCTL_SERCLK_EN (0x1 << 19)
60#define S3C_PCM_CLKCTL_SERCLKSEL_PCLK (0x1 << 18)
61#define S3C_PCM_CLKCTL_SCLKDIV_MASK 0x1ff
62#define S3C_PCM_CLKCTL_SYNCDIV_MASK 0x1ff
63#define S3C_PCM_CLKCTL_SCLKDIV_SHIFT 9
64#define S3C_PCM_CLKCTL_SYNCDIV_SHIFT 0
65
66/* PCM_TXFIFO Bit-Fields */
67#define S3C_PCM_TXFIFO_DVALID (0x1 << 16)
68#define S3C_PCM_TXFIFO_DATA_MSK (0xffff << 0)
69
70/* PCM_RXFIFO Bit-Fields */
71#define S3C_PCM_RXFIFO_DVALID (0x1 << 16)
72#define S3C_PCM_RXFIFO_DATA_MSK (0xffff << 0)
73
74/* PCM_IRQCTL Bit-Fields */
75#define S3C_PCM_IRQCTL_IRQEN (0x1 << 14)
76#define S3C_PCM_IRQCTL_WRDEN (0x1 << 12)
77#define S3C_PCM_IRQCTL_TXEMPTYEN (0x1 << 11)
78#define S3C_PCM_IRQCTL_TXALMSTEMPTYEN (0x1 << 10)
79#define S3C_PCM_IRQCTL_TXFULLEN (0x1 << 9)
80#define S3C_PCM_IRQCTL_TXALMSTFULLEN (0x1 << 8)
81#define S3C_PCM_IRQCTL_TXSTARVEN (0x1 << 7)
82#define S3C_PCM_IRQCTL_TXERROVRFLEN (0x1 << 6)
83#define S3C_PCM_IRQCTL_RXEMPTEN (0x1 << 5)
84#define S3C_PCM_IRQCTL_RXALMSTEMPTEN (0x1 << 4)
85#define S3C_PCM_IRQCTL_RXFULLEN (0x1 << 3)
86#define S3C_PCM_IRQCTL_RXALMSTFULLEN (0x1 << 2)
87#define S3C_PCM_IRQCTL_RXSTARVEN (0x1 << 1)
88#define S3C_PCM_IRQCTL_RXERROVRFLEN (0x1 << 0)
89
90/* PCM_IRQSTAT Bit-Fields */
91#define S3C_PCM_IRQSTAT_IRQPND (0x1 << 13)
92#define S3C_PCM_IRQSTAT_WRD_XFER (0x1 << 12)
93#define S3C_PCM_IRQSTAT_TXEMPTY (0x1 << 11)
94#define S3C_PCM_IRQSTAT_TXALMSTEMPTY (0x1 << 10)
95#define S3C_PCM_IRQSTAT_TXFULL (0x1 << 9)
96#define S3C_PCM_IRQSTAT_TXALMSTFULL (0x1 << 8)
97#define S3C_PCM_IRQSTAT_TXSTARV (0x1 << 7)
98#define S3C_PCM_IRQSTAT_TXERROVRFL (0x1 << 6)
99#define S3C_PCM_IRQSTAT_RXEMPT (0x1 << 5)
100#define S3C_PCM_IRQSTAT_RXALMSTEMPT (0x1 << 4)
101#define S3C_PCM_IRQSTAT_RXFULL (0x1 << 3)
102#define S3C_PCM_IRQSTAT_RXALMSTFULL (0x1 << 2)
103#define S3C_PCM_IRQSTAT_RXSTARV (0x1 << 1)
104#define S3C_PCM_IRQSTAT_RXERROVRFL (0x1 << 0)
105
106/* PCM_FIFOSTAT Bit-Fields */
107#define S3C_PCM_FIFOSTAT_TXCNT_MSK (0x3f << 14)
108#define S3C_PCM_FIFOSTAT_TXFIFOEMPTY (0x1 << 13)
109#define S3C_PCM_FIFOSTAT_TXFIFOALMSTEMPTY (0x1 << 12)
110#define S3C_PCM_FIFOSTAT_TXFIFOFULL (0x1 << 11)
111#define S3C_PCM_FIFOSTAT_TXFIFOALMSTFULL (0x1 << 10)
112#define S3C_PCM_FIFOSTAT_RXCNT_MSK (0x3f << 4)
113#define S3C_PCM_FIFOSTAT_RXFIFOEMPTY (0x1 << 3)
114#define S3C_PCM_FIFOSTAT_RXFIFOALMSTEMPTY (0x1 << 2)
115#define S3C_PCM_FIFOSTAT_RXFIFOFULL (0x1 << 1)
116#define S3C_PCM_FIFOSTAT_RXFIFOALMSTFULL (0x1 << 0)
117
118/**
119 * struct s3c_pcm_info - S3C PCM Controller information
120 * @dev: The parent device passed to use from the probe.
121 * @regs: The pointer to the device register block.
122 * @dma_playback: DMA information for playback channel.
123 * @dma_capture: DMA information for capture channel.
124 */
125struct s3c_pcm_info {
126 spinlock_t lock;
127 struct device *dev;
128 void __iomem *regs;
129
130 unsigned int sclk_per_fs;
131
132 /* Whether to keep PCMSCLK enabled even when idle(no active xfer) */
133 unsigned int idleclk;
134
135 struct clk *pclk;
136 struct clk *cclk;
137
138 struct s3c_dma_params *dma_playback;
139 struct s3c_dma_params *dma_capture;
140};
141
Jassi Brar357a1db2009-11-17 16:54:03 +0900142static struct s3c2410_dma_client s3c_pcm_dma_client_out = {
143 .name = "PCM Stereo out"
144};
145
146static struct s3c2410_dma_client s3c_pcm_dma_client_in = {
147 .name = "PCM Stereo in"
148};
149
150static struct s3c_dma_params s3c_pcm_stereo_out[] = {
151 [0] = {
152 .client = &s3c_pcm_dma_client_out,
153 .dma_size = 4,
154 },
155 [1] = {
156 .client = &s3c_pcm_dma_client_out,
157 .dma_size = 4,
158 },
159};
160
161static struct s3c_dma_params s3c_pcm_stereo_in[] = {
162 [0] = {
163 .client = &s3c_pcm_dma_client_in,
164 .dma_size = 4,
165 },
166 [1] = {
167 .client = &s3c_pcm_dma_client_in,
168 .dma_size = 4,
169 },
170};
171
172static struct s3c_pcm_info s3c_pcm[2];
173
Jassi Brar357a1db2009-11-17 16:54:03 +0900174static void s3c_pcm_snd_txctrl(struct s3c_pcm_info *pcm, int on)
175{
176 void __iomem *regs = pcm->regs;
177 u32 ctl, clkctl;
178
179 clkctl = readl(regs + S3C_PCM_CLKCTL);
180 ctl = readl(regs + S3C_PCM_CTL);
181 ctl &= ~(S3C_PCM_CTL_TXDIPSTICK_MASK
182 << S3C_PCM_CTL_TXDIPSTICK_SHIFT);
183
184 if (on) {
185 ctl |= S3C_PCM_CTL_TXDMA_EN;
186 ctl |= S3C_PCM_CTL_TXFIFO_EN;
187 ctl |= S3C_PCM_CTL_ENABLE;
Seungwhan Youn29f9e392010-09-10 17:20:45 +0900188 ctl |= (0x4<<S3C_PCM_CTL_TXDIPSTICK_SHIFT);
Jassi Brar357a1db2009-11-17 16:54:03 +0900189 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
190 } else {
191 ctl &= ~S3C_PCM_CTL_TXDMA_EN;
192 ctl &= ~S3C_PCM_CTL_TXFIFO_EN;
193
194 if (!(ctl & S3C_PCM_CTL_RXFIFO_EN)) {
195 ctl &= ~S3C_PCM_CTL_ENABLE;
196 if (!pcm->idleclk)
197 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
198 }
199 }
200
201 writel(clkctl, regs + S3C_PCM_CLKCTL);
202 writel(ctl, regs + S3C_PCM_CTL);
203}
204
205static void s3c_pcm_snd_rxctrl(struct s3c_pcm_info *pcm, int on)
206{
207 void __iomem *regs = pcm->regs;
208 u32 ctl, clkctl;
209
210 ctl = readl(regs + S3C_PCM_CTL);
211 clkctl = readl(regs + S3C_PCM_CLKCTL);
Seungwhan Youn0cca9012010-09-10 17:20:00 +0900212 ctl &= ~(S3C_PCM_CTL_RXDIPSTICK_MASK
213 << S3C_PCM_CTL_RXDIPSTICK_SHIFT);
Jassi Brar357a1db2009-11-17 16:54:03 +0900214
215 if (on) {
216 ctl |= S3C_PCM_CTL_RXDMA_EN;
217 ctl |= S3C_PCM_CTL_RXFIFO_EN;
218 ctl |= S3C_PCM_CTL_ENABLE;
Seungwhan Youn0cca9012010-09-10 17:20:00 +0900219 ctl |= (0x20<<S3C_PCM_CTL_RXDIPSTICK_SHIFT);
Jassi Brar357a1db2009-11-17 16:54:03 +0900220 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
221 } else {
222 ctl &= ~S3C_PCM_CTL_RXDMA_EN;
223 ctl &= ~S3C_PCM_CTL_RXFIFO_EN;
224
225 if (!(ctl & S3C_PCM_CTL_TXFIFO_EN)) {
226 ctl &= ~S3C_PCM_CTL_ENABLE;
227 if (!pcm->idleclk)
228 clkctl |= S3C_PCM_CLKCTL_SERCLK_EN;
229 }
230 }
231
232 writel(clkctl, regs + S3C_PCM_CLKCTL);
233 writel(ctl, regs + S3C_PCM_CTL);
234}
235
236static int s3c_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
237 struct snd_soc_dai *dai)
238{
239 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000240 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Jassi Brar357a1db2009-11-17 16:54:03 +0900241 unsigned long flags;
242
243 dev_dbg(pcm->dev, "Entered %s\n", __func__);
244
245 switch (cmd) {
246 case SNDRV_PCM_TRIGGER_START:
247 case SNDRV_PCM_TRIGGER_RESUME:
248 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
249 spin_lock_irqsave(&pcm->lock, flags);
250
251 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
252 s3c_pcm_snd_rxctrl(pcm, 1);
253 else
254 s3c_pcm_snd_txctrl(pcm, 1);
255
256 spin_unlock_irqrestore(&pcm->lock, flags);
257 break;
258
259 case SNDRV_PCM_TRIGGER_STOP:
260 case SNDRV_PCM_TRIGGER_SUSPEND:
261 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
262 spin_lock_irqsave(&pcm->lock, flags);
263
264 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
265 s3c_pcm_snd_rxctrl(pcm, 0);
266 else
267 s3c_pcm_snd_txctrl(pcm, 0);
268
269 spin_unlock_irqrestore(&pcm->lock, flags);
270 break;
271
272 default:
273 return -EINVAL;
274 }
275
276 return 0;
277}
278
279static int s3c_pcm_hw_params(struct snd_pcm_substream *substream,
280 struct snd_pcm_hw_params *params,
281 struct snd_soc_dai *socdai)
282{
283 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000284 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
Daniel Mack5f712b22010-03-22 10:11:15 +0100285 struct s3c_dma_params *dma_data;
Jassi Brar357a1db2009-11-17 16:54:03 +0900286 void __iomem *regs = pcm->regs;
287 struct clk *clk;
288 int sclk_div, sync_div;
289 unsigned long flags;
290 u32 clkctl;
291
292 dev_dbg(pcm->dev, "Entered %s\n", __func__);
293
294 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Daniel Mack5f712b22010-03-22 10:11:15 +0100295 dma_data = pcm->dma_playback;
Jassi Brar357a1db2009-11-17 16:54:03 +0900296 else
Daniel Mack5f712b22010-03-22 10:11:15 +0100297 dma_data = pcm->dma_capture;
298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299 snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data);
Jassi Brar357a1db2009-11-17 16:54:03 +0900300
301 /* Strictly check for sample size */
302 switch (params_format(params)) {
303 case SNDRV_PCM_FORMAT_S16_LE:
304 break;
305 default:
306 return -EINVAL;
307 }
308
309 spin_lock_irqsave(&pcm->lock, flags);
310
311 /* Get hold of the PCMSOURCE_CLK */
312 clkctl = readl(regs + S3C_PCM_CLKCTL);
313 if (clkctl & S3C_PCM_CLKCTL_SERCLKSEL_PCLK)
314 clk = pcm->pclk;
315 else
316 clk = pcm->cclk;
317
318 /* Set the SCLK divider */
319 sclk_div = clk_get_rate(clk) / pcm->sclk_per_fs /
320 params_rate(params) / 2 - 1;
321
322 clkctl &= ~(S3C_PCM_CLKCTL_SCLKDIV_MASK
323 << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
324 clkctl |= ((sclk_div & S3C_PCM_CLKCTL_SCLKDIV_MASK)
325 << S3C_PCM_CLKCTL_SCLKDIV_SHIFT);
326
327 /* Set the SYNC divider */
328 sync_div = pcm->sclk_per_fs - 1;
329
330 clkctl &= ~(S3C_PCM_CLKCTL_SYNCDIV_MASK
331 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
332 clkctl |= ((sync_div & S3C_PCM_CLKCTL_SYNCDIV_MASK)
333 << S3C_PCM_CLKCTL_SYNCDIV_SHIFT);
334
335 writel(clkctl, regs + S3C_PCM_CLKCTL);
336
337 spin_unlock_irqrestore(&pcm->lock, flags);
338
Joe Perches59cdd9b2010-02-01 23:22:16 -0800339 dev_dbg(pcm->dev, "PCMSOURCE_CLK-%lu SCLK=%ufs SCLK_DIV=%d SYNC_DIV=%d\n",
Jassi Brar357a1db2009-11-17 16:54:03 +0900340 clk_get_rate(clk), pcm->sclk_per_fs,
341 sclk_div, sync_div);
342
343 return 0;
344}
345
346static int s3c_pcm_set_fmt(struct snd_soc_dai *cpu_dai,
347 unsigned int fmt)
348{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000349 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
Jassi Brar357a1db2009-11-17 16:54:03 +0900350 void __iomem *regs = pcm->regs;
351 unsigned long flags;
352 int ret = 0;
353 u32 ctl;
354
355 dev_dbg(pcm->dev, "Entered %s\n", __func__);
356
357 spin_lock_irqsave(&pcm->lock, flags);
358
359 ctl = readl(regs + S3C_PCM_CTL);
360
361 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
362 case SND_SOC_DAIFMT_NB_NF:
363 /* Nothing to do, NB_NF by default */
364 break;
365 default:
366 dev_err(pcm->dev, "Unsupported clock inversion!\n");
367 ret = -EINVAL;
368 goto exit;
369 }
370
371 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
372 case SND_SOC_DAIFMT_CBS_CFS:
373 /* Nothing to do, Master by default */
374 break;
375 default:
376 dev_err(pcm->dev, "Unsupported master/slave format!\n");
377 ret = -EINVAL;
378 goto exit;
379 }
380
381 switch (fmt & SND_SOC_DAIFMT_CLOCK_MASK) {
382 case SND_SOC_DAIFMT_CONT:
383 pcm->idleclk = 1;
384 break;
385 case SND_SOC_DAIFMT_GATED:
386 pcm->idleclk = 0;
387 break;
388 default:
389 dev_err(pcm->dev, "Invalid Clock gating request!\n");
390 ret = -EINVAL;
391 goto exit;
392 }
393
394 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
395 case SND_SOC_DAIFMT_DSP_A:
396 ctl |= S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
397 ctl |= S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
398 break;
399 case SND_SOC_DAIFMT_DSP_B:
400 ctl &= ~S3C_PCM_CTL_TXMSB_AFTER_FSYNC;
401 ctl &= ~S3C_PCM_CTL_RXMSB_AFTER_FSYNC;
402 break;
403 default:
404 dev_err(pcm->dev, "Unsupported data format!\n");
405 ret = -EINVAL;
406 goto exit;
407 }
408
409 writel(ctl, regs + S3C_PCM_CTL);
410
411exit:
412 spin_unlock_irqrestore(&pcm->lock, flags);
413
414 return ret;
415}
416
417static int s3c_pcm_set_clkdiv(struct snd_soc_dai *cpu_dai,
418 int div_id, int div)
419{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000420 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
Jassi Brar357a1db2009-11-17 16:54:03 +0900421
422 switch (div_id) {
423 case S3C_PCM_SCLK_PER_FS:
424 pcm->sclk_per_fs = div;
425 break;
426
427 default:
428 return -EINVAL;
429 }
430
431 return 0;
432}
433
434static int s3c_pcm_set_sysclk(struct snd_soc_dai *cpu_dai,
435 int clk_id, unsigned int freq, int dir)
436{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000437 struct s3c_pcm_info *pcm = snd_soc_dai_get_drvdata(cpu_dai);
Jassi Brar357a1db2009-11-17 16:54:03 +0900438 void __iomem *regs = pcm->regs;
439 u32 clkctl = readl(regs + S3C_PCM_CLKCTL);
440
441 switch (clk_id) {
442 case S3C_PCM_CLKSRC_PCLK:
443 clkctl |= S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
444 break;
445
446 case S3C_PCM_CLKSRC_MUX:
447 clkctl &= ~S3C_PCM_CLKCTL_SERCLKSEL_PCLK;
448
449 if (clk_get_rate(pcm->cclk) != freq)
450 clk_set_rate(pcm->cclk, freq);
451
452 break;
453
454 default:
455 return -EINVAL;
456 }
457
458 writel(clkctl, regs + S3C_PCM_CLKCTL);
459
460 return 0;
461}
462
463static struct snd_soc_dai_ops s3c_pcm_dai_ops = {
464 .set_sysclk = s3c_pcm_set_sysclk,
465 .set_clkdiv = s3c_pcm_set_clkdiv,
466 .trigger = s3c_pcm_trigger,
467 .hw_params = s3c_pcm_hw_params,
468 .set_fmt = s3c_pcm_set_fmt,
469};
470
471#define S3C_PCM_RATES SNDRV_PCM_RATE_8000_96000
472
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000473#define S3C_PCM_DAI_DECLARE \
Jassi Brar357a1db2009-11-17 16:54:03 +0900474 .symmetric_rates = 1, \
475 .ops = &s3c_pcm_dai_ops, \
476 .playback = { \
477 .channels_min = 2, \
478 .channels_max = 2, \
479 .rates = S3C_PCM_RATES, \
480 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
481 }, \
482 .capture = { \
483 .channels_min = 2, \
484 .channels_max = 2, \
485 .rates = S3C_PCM_RATES, \
486 .formats = SNDRV_PCM_FMTBIT_S16_LE, \
Jassi Brar12280fa2010-09-10 16:41:05 +0900487 }
Jassi Brar357a1db2009-11-17 16:54:03 +0900488
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000489struct snd_soc_dai_driver s3c_pcm_dai[] = {
Jassi Brar12280fa2010-09-10 16:41:05 +0900490 [0] = {
491 .name = "samsung-pcm.0",
492 S3C_PCM_DAI_DECLARE,
493 },
494 [1] = {
495 .name = "samsung-pcm.1",
496 S3C_PCM_DAI_DECLARE,
497 },
Jassi Brar357a1db2009-11-17 16:54:03 +0900498};
499EXPORT_SYMBOL_GPL(s3c_pcm_dai);
500
501static __devinit int s3c_pcm_dev_probe(struct platform_device *pdev)
502{
503 struct s3c_pcm_info *pcm;
Jassi Brar357a1db2009-11-17 16:54:03 +0900504 struct resource *mem_res, *dmatx_res, *dmarx_res;
505 struct s3c_audio_pdata *pcm_pdata;
506 int ret;
507
508 /* Check for valid device index */
509 if ((pdev->id < 0) || pdev->id >= ARRAY_SIZE(s3c_pcm)) {
510 dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
511 return -EINVAL;
512 }
513
514 pcm_pdata = pdev->dev.platform_data;
515
516 /* Check for availability of necessary resource */
517 dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
518 if (!dmatx_res) {
519 dev_err(&pdev->dev, "Unable to get PCM-TX dma resource\n");
520 return -ENXIO;
521 }
522
523 dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
524 if (!dmarx_res) {
525 dev_err(&pdev->dev, "Unable to get PCM-RX dma resource\n");
526 return -ENXIO;
527 }
528
529 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
530 if (!mem_res) {
531 dev_err(&pdev->dev, "Unable to get register resource\n");
532 return -ENXIO;
533 }
534
535 if (pcm_pdata && pcm_pdata->cfg_gpio && pcm_pdata->cfg_gpio(pdev)) {
536 dev_err(&pdev->dev, "Unable to configure gpio\n");
537 return -EINVAL;
538 }
539
540 pcm = &s3c_pcm[pdev->id];
541 pcm->dev = &pdev->dev;
542
543 spin_lock_init(&pcm->lock);
544
Jassi Brar357a1db2009-11-17 16:54:03 +0900545 /* Default is 128fs */
546 pcm->sclk_per_fs = 128;
547
548 pcm->cclk = clk_get(&pdev->dev, "audio-bus");
549 if (IS_ERR(pcm->cclk)) {
550 dev_err(&pdev->dev, "failed to get audio-bus\n");
551 ret = PTR_ERR(pcm->cclk);
552 goto err1;
553 }
554 clk_enable(pcm->cclk);
555
556 /* record our pcm structure for later use in the callbacks */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000557 dev_set_drvdata(&pdev->dev, pcm);
Jassi Brar357a1db2009-11-17 16:54:03 +0900558
559 if (!request_mem_region(mem_res->start,
560 resource_size(mem_res), "samsung-pcm")) {
561 dev_err(&pdev->dev, "Unable to request register region\n");
562 ret = -EBUSY;
563 goto err2;
564 }
565
566 pcm->regs = ioremap(mem_res->start, 0x100);
567 if (pcm->regs == NULL) {
568 dev_err(&pdev->dev, "cannot ioremap registers\n");
569 ret = -ENXIO;
570 goto err3;
571 }
572
573 pcm->pclk = clk_get(&pdev->dev, "pcm");
574 if (IS_ERR(pcm->pclk)) {
575 dev_err(&pdev->dev, "failed to get pcm_clock\n");
576 ret = -ENOENT;
577 goto err4;
578 }
579 clk_enable(pcm->pclk);
580
Jassi Brar12280fa2010-09-10 16:41:05 +0900581 ret = snd_soc_register_dai(&pdev->dev, &s3c_pcm_dai[pdev->id]);
Jassi Brar357a1db2009-11-17 16:54:03 +0900582 if (ret != 0) {
583 dev_err(&pdev->dev, "failed to get pcm_clock\n");
584 goto err5;
585 }
586
587 s3c_pcm_stereo_in[pdev->id].dma_addr = mem_res->start
588 + S3C_PCM_RXFIFO;
589 s3c_pcm_stereo_out[pdev->id].dma_addr = mem_res->start
590 + S3C_PCM_TXFIFO;
591
592 s3c_pcm_stereo_in[pdev->id].channel = dmarx_res->start;
593 s3c_pcm_stereo_out[pdev->id].channel = dmatx_res->start;
594
595 pcm->dma_capture = &s3c_pcm_stereo_in[pdev->id];
596 pcm->dma_playback = &s3c_pcm_stereo_out[pdev->id];
597
598 return 0;
599
600err5:
601 clk_disable(pcm->pclk);
602 clk_put(pcm->pclk);
603err4:
604 iounmap(pcm->regs);
605err3:
606 release_mem_region(mem_res->start, resource_size(mem_res));
607err2:
608 clk_disable(pcm->cclk);
609 clk_put(pcm->cclk);
610err1:
611 return ret;
612}
613
614static __devexit int s3c_pcm_dev_remove(struct platform_device *pdev)
615{
616 struct s3c_pcm_info *pcm = &s3c_pcm[pdev->id];
617 struct resource *mem_res;
618
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000619 snd_soc_unregister_dai(&pdev->dev);
620
Jassi Brar357a1db2009-11-17 16:54:03 +0900621 iounmap(pcm->regs);
622
623 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
624 release_mem_region(mem_res->start, resource_size(mem_res));
625
626 clk_disable(pcm->cclk);
627 clk_disable(pcm->pclk);
628 clk_put(pcm->pclk);
629 clk_put(pcm->cclk);
630
631 return 0;
632}
633
634static struct platform_driver s3c_pcm_driver = {
635 .probe = s3c_pcm_dev_probe,
636 .remove = s3c_pcm_dev_remove,
637 .driver = {
Jassi Brar5fbdedf2010-09-10 16:41:17 +0900638 .name = "samsung-pcm",
Jassi Brar357a1db2009-11-17 16:54:03 +0900639 .owner = THIS_MODULE,
640 },
641};
642
643static int __init s3c_pcm_init(void)
644{
645 return platform_driver_register(&s3c_pcm_driver);
646}
647module_init(s3c_pcm_init);
648
649static void __exit s3c_pcm_exit(void)
650{
651 platform_driver_unregister(&s3c_pcm_driver);
652}
653module_exit(s3c_pcm_exit);
654
655/* Module information */
656MODULE_AUTHOR("Jaswinder Singh, <jassi.brar@samsung.com>");
657MODULE_DESCRIPTION("S3C PCM Controller Driver");
658MODULE_LICENSE("GPL");
Jassi Brar5fbdedf2010-09-10 16:41:17 +0900659MODULE_ALIAS("platform:samsung-pcm");