Merge branch 'origin' into devel-stable
[safe/jmp/linux-2.6] / sound / soc / s3c24xx / s3c64xx-i2s.c
1 /* sound/soc/s3c24xx/s3c64xx-i2s.c
2  *
3  * ALSA SoC Audio Layer - S3C64XX I2S driver
4  *
5  * Copyright 2008 Openmoko, Inc.
6  * Copyright 2008 Simtec Electronics
7  *      Ben Dooks <ben@simtec.co.uk>
8  *      http://armlinux.simtec.co.uk/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/device.h>
18 #include <linux/clk.h>
19 #include <linux/gpio.h>
20 #include <linux/io.h>
21
22 #include <sound/soc.h>
23
24 #include <plat/regs-s3c2412-iis.h>
25 #include <mach/gpio-bank-d.h>
26 #include <mach/gpio-bank-e.h>
27 #include <plat/gpio-cfg.h>
28
29 #include <mach/map.h>
30 #include <mach/dma.h>
31
32 #include "s3c-dma.h"
33 #include "s3c64xx-i2s.h"
34
35 /* The value should be set to maximum of the total number
36  * of I2Sv3 controllers that any supported SoC has.
37  */
38 #define MAX_I2SV3       2
39
40 static struct s3c2410_dma_client s3c64xx_dma_client_out = {
41         .name           = "I2S PCM Stereo out"
42 };
43
44 static struct s3c2410_dma_client s3c64xx_dma_client_in = {
45         .name           = "I2S PCM Stereo in"
46 };
47
48 static struct s3c_dma_params s3c64xx_i2s_pcm_stereo_out[MAX_I2SV3];
49 static struct s3c_dma_params s3c64xx_i2s_pcm_stereo_in[MAX_I2SV3];
50 static struct s3c_i2sv2_info s3c64xx_i2s[MAX_I2SV3];
51
52 struct snd_soc_dai s3c64xx_i2s_dai[MAX_I2SV3];
53 EXPORT_SYMBOL_GPL(s3c64xx_i2s_dai);
54
55 static inline struct s3c_i2sv2_info *to_info(struct snd_soc_dai *cpu_dai)
56 {
57         return cpu_dai->private_data;
58 }
59
60 static int s3c64xx_i2s_set_sysclk(struct snd_soc_dai *cpu_dai,
61                                   int clk_id, unsigned int freq, int dir)
62 {
63         struct s3c_i2sv2_info *i2s = to_info(cpu_dai);
64         u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
65
66         switch (clk_id) {
67         case S3C64XX_CLKSRC_PCLK:
68                 iismod &= ~S3C64XX_IISMOD_IMS_SYSMUX;
69                 break;
70
71         case S3C64XX_CLKSRC_MUX:
72                 iismod |= S3C64XX_IISMOD_IMS_SYSMUX;
73                 break;
74
75         case S3C64XX_CLKSRC_CDCLK:
76                 switch (dir) {
77                 case SND_SOC_CLOCK_IN:
78                         iismod |= S3C64XX_IISMOD_CDCLKCON;
79                         break;
80                 case SND_SOC_CLOCK_OUT:
81                         iismod &= ~S3C64XX_IISMOD_CDCLKCON;
82                         break;
83                 default:
84                         return -EINVAL;
85                 }
86                 break;
87
88         default:
89                 return -EINVAL;
90         }
91
92         writel(iismod, i2s->regs + S3C2412_IISMOD);
93
94         return 0;
95 }
96
97 struct clk *s3c64xx_i2s_get_clock(struct snd_soc_dai *dai)
98 {
99         struct s3c_i2sv2_info *i2s = to_info(dai);
100         u32 iismod = readl(i2s->regs + S3C2412_IISMOD);
101
102         if (iismod & S3C64XX_IISMOD_IMS_SYSMUX)
103                 return i2s->iis_cclk;
104         else
105                 return i2s->iis_pclk;
106 }
107 EXPORT_SYMBOL_GPL(s3c64xx_i2s_get_clock);
108
109 static int s3c64xx_i2s_probe(struct platform_device *pdev,
110                              struct snd_soc_dai *dai)
111 {
112         /* configure GPIO for i2s port */
113         switch (dai->id) {
114         case 0:
115                 s3c_gpio_cfgpin(S3C64XX_GPD(0), S3C64XX_GPD0_I2S0_CLK);
116                 s3c_gpio_cfgpin(S3C64XX_GPD(1), S3C64XX_GPD1_I2S0_CDCLK);
117                 s3c_gpio_cfgpin(S3C64XX_GPD(2), S3C64XX_GPD2_I2S0_LRCLK);
118                 s3c_gpio_cfgpin(S3C64XX_GPD(3), S3C64XX_GPD3_I2S0_DI);
119                 s3c_gpio_cfgpin(S3C64XX_GPD(4), S3C64XX_GPD4_I2S0_D0);
120                 break;
121         case 1:
122                 s3c_gpio_cfgpin(S3C64XX_GPE(0), S3C64XX_GPE0_I2S1_CLK);
123                 s3c_gpio_cfgpin(S3C64XX_GPE(1), S3C64XX_GPE1_I2S1_CDCLK);
124                 s3c_gpio_cfgpin(S3C64XX_GPE(2), S3C64XX_GPE2_I2S1_LRCLK);
125                 s3c_gpio_cfgpin(S3C64XX_GPE(3), S3C64XX_GPE3_I2S1_DI);
126                 s3c_gpio_cfgpin(S3C64XX_GPE(4), S3C64XX_GPE4_I2S1_D0);
127         }
128
129         return 0;
130 }
131
132
133 #define S3C64XX_I2S_RATES \
134         (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_16000 | \
135         SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
136         SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
137
138 #define S3C64XX_I2S_FMTS \
139         (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
140          SNDRV_PCM_FMTBIT_S24_LE)
141
142 static struct snd_soc_dai_ops s3c64xx_i2s_dai_ops = {
143         .set_sysclk     = s3c64xx_i2s_set_sysclk,       
144 };
145
146 static __devinit int s3c64xx_iis_dev_probe(struct platform_device *pdev)
147 {
148         struct s3c_i2sv2_info *i2s;
149         struct snd_soc_dai *dai;
150         int ret;
151
152         if (pdev->id >= MAX_I2SV3) {
153                 dev_err(&pdev->dev, "id %d out of range\n", pdev->id);
154                 return -EINVAL;
155         }
156
157         i2s = &s3c64xx_i2s[pdev->id];
158         dai = &s3c64xx_i2s_dai[pdev->id];
159         dai->dev = &pdev->dev;
160         dai->name = "s3c64xx-i2s";
161         dai->id = pdev->id;
162         dai->symmetric_rates = 1;
163         dai->playback.channels_min = 2;
164         dai->playback.channels_max = 2;
165         dai->playback.rates = S3C64XX_I2S_RATES;
166         dai->playback.formats = S3C64XX_I2S_FMTS;
167         dai->capture.channels_min = 2;
168         dai->capture.channels_max = 2;
169         dai->capture.rates = S3C64XX_I2S_RATES;
170         dai->capture.formats = S3C64XX_I2S_FMTS;
171         dai->probe = s3c64xx_i2s_probe;
172         dai->ops = &s3c64xx_i2s_dai_ops;
173
174         i2s->dma_capture = &s3c64xx_i2s_pcm_stereo_in[pdev->id];
175         i2s->dma_playback = &s3c64xx_i2s_pcm_stereo_out[pdev->id];
176
177         if (pdev->id == 0) {
178                 i2s->dma_capture->channel = DMACH_I2S0_IN;
179                 i2s->dma_capture->dma_addr = S3C64XX_PA_IIS0 + S3C2412_IISRXD;
180                 i2s->dma_playback->channel = DMACH_I2S0_OUT;
181                 i2s->dma_playback->dma_addr = S3C64XX_PA_IIS0 + S3C2412_IISTXD;
182         } else {
183                 i2s->dma_capture->channel = DMACH_I2S1_IN;
184                 i2s->dma_capture->dma_addr = S3C64XX_PA_IIS1 + S3C2412_IISRXD;
185                 i2s->dma_playback->channel = DMACH_I2S1_OUT;
186                 i2s->dma_playback->dma_addr = S3C64XX_PA_IIS1 + S3C2412_IISTXD;
187         }
188
189         i2s->dma_capture->client = &s3c64xx_dma_client_in;
190         i2s->dma_capture->dma_size = 4;
191         i2s->dma_playback->client = &s3c64xx_dma_client_out;
192         i2s->dma_playback->dma_size = 4;
193
194         i2s->iis_cclk = clk_get(&pdev->dev, "audio-bus");
195         if (IS_ERR(i2s->iis_cclk)) {
196                 dev_err(&pdev->dev, "failed to get audio-bus\n");
197                 ret = PTR_ERR(i2s->iis_cclk);
198                 goto err;
199         }
200
201         clk_enable(i2s->iis_cclk);
202
203         ret = s3c_i2sv2_probe(pdev, dai, i2s, 0);
204         if (ret)
205                 goto err_clk;
206
207         ret = s3c_i2sv2_register_dai(dai);
208         if (ret != 0)
209                 goto err_i2sv2;
210
211         return 0;
212
213 err_i2sv2:
214         /* Not implemented for I2Sv2 core yet */
215 err_clk:
216         clk_put(i2s->iis_cclk);
217 err:
218         return ret;
219 }
220
221 static __devexit int s3c64xx_iis_dev_remove(struct platform_device *pdev)
222 {
223         dev_err(&pdev->dev, "Device removal not yet supported\n");
224         return 0;
225 }
226
227 static struct platform_driver s3c64xx_iis_driver = {
228         .probe  = s3c64xx_iis_dev_probe,
229         .remove = s3c64xx_iis_dev_remove,
230         .driver = {
231                 .name = "s3c64xx-iis",
232                 .owner = THIS_MODULE,
233         },
234 };
235
236 static int __init s3c64xx_i2s_init(void)
237 {
238         return platform_driver_register(&s3c64xx_iis_driver);
239 }
240 module_init(s3c64xx_i2s_init);
241
242 static void __exit s3c64xx_i2s_exit(void)
243 {
244         platform_driver_unregister(&s3c64xx_iis_driver);
245 }
246 module_exit(s3c64xx_i2s_exit);
247
248 /* Module information */
249 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
250 MODULE_DESCRIPTION("S3C64XX I2S SoC Interface");
251 MODULE_LICENSE("GPL");