]> git.karo-electronics.de Git - mv-sheeva.git/blob - sound/soc/s3c24xx/s3c24xx_simtec.c
c4c1114420109944bdbf73cdeda17083a054e5e7
[mv-sheeva.git] / sound / soc / s3c24xx / s3c24xx_simtec.c
1 /* sound/soc/s3c24xx/s3c24xx_simtec.c
2  *
3  * Copyright 2009 Simtec Electronics
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8 */
9
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/platform_device.h>
13 #include <linux/gpio.h>
14 #include <linux/clk.h>
15 #include <linux/i2c.h>
16
17 #include <sound/core.h>
18 #include <sound/pcm.h>
19 #include <sound/soc.h>
20 #include <sound/soc-dapm.h>
21
22 #include <plat/audio-simtec.h>
23
24 #include "s3c-dma.h"
25 #include "s3c24xx-i2s.h"
26 #include "s3c24xx_simtec.h"
27
28 static struct s3c24xx_audio_simtec_pdata *pdata;
29 static struct clk *xtal_clk;
30
31 static int spk_gain;
32 static int spk_unmute;
33
34 /**
35  * speaker_gain_get - read the speaker gain setting.
36  * @kcontrol: The control for the speaker gain.
37  * @ucontrol: The value that needs to be updated.
38  *
39  * Read the value for the AMP gain control.
40  */
41 static int speaker_gain_get(struct snd_kcontrol *kcontrol,
42                             struct snd_ctl_elem_value *ucontrol)
43 {
44         ucontrol->value.integer.value[0] = spk_gain;
45         return 0;
46 }
47
48 /**
49  * speaker_gain_set - set the value of the speaker amp gain
50  * @value: The value to write.
51  */
52 static void speaker_gain_set(int value)
53 {
54         gpio_set_value_cansleep(pdata->amp_gain[0], value & 1);
55         gpio_set_value_cansleep(pdata->amp_gain[1], value >> 1);
56 }
57
58 /**
59  * speaker_gain_put - set the speaker gain setting.
60  * @kcontrol: The control for the speaker gain.
61  * @ucontrol: The value that needs to be set.
62  *
63  * Set the value of the speaker gain from the specified
64  * @ucontrol setting.
65  *
66  * Note, if the speaker amp is muted, then we do not set a gain value
67  * as at-least one of the ICs that is fitted will try and power up even
68  * if the main control is set to off.
69  */
70 static int speaker_gain_put(struct snd_kcontrol *kcontrol,
71                             struct snd_ctl_elem_value *ucontrol)
72 {
73         int value = ucontrol->value.integer.value[0];
74
75         spk_gain = value;
76
77         if (!spk_unmute)
78                 speaker_gain_set(value);
79
80         return 0;
81 }
82
83 static const struct snd_kcontrol_new amp_gain_controls[] = {
84         SOC_SINGLE_EXT("Speaker Gain", 0, 0, 3, 0,
85                        speaker_gain_get, speaker_gain_put),
86 };
87
88 /**
89  * spk_unmute_state - set the unmute state of the speaker
90  * @to: zero to unmute, non-zero to ununmute.
91  */
92 static void spk_unmute_state(int to)
93 {
94         pr_debug("%s: to=%d\n", __func__, to);
95
96         spk_unmute = to;
97         gpio_set_value(pdata->amp_gpio, to);
98
99         /* if we're umuting, also re-set the gain */
100         if (to && pdata->amp_gain[0] > 0)
101                 speaker_gain_set(spk_gain);
102 }
103
104 /**
105  * speaker_unmute_get - read the speaker unmute setting.
106  * @kcontrol: The control for the speaker gain.
107  * @ucontrol: The value that needs to be updated.
108  *
109  * Read the value for the AMP gain control.
110  */
111 static int speaker_unmute_get(struct snd_kcontrol *kcontrol,
112                             struct snd_ctl_elem_value *ucontrol)
113 {
114         ucontrol->value.integer.value[0] = spk_unmute;
115         return 0;
116 }
117
118 /**
119  * speaker_unmute_put - set the speaker unmute setting.
120  * @kcontrol: The control for the speaker gain.
121  * @ucontrol: The value that needs to be set.
122  *
123  * Set the value of the speaker gain from the specified
124  * @ucontrol setting.
125  */
126 static int speaker_unmute_put(struct snd_kcontrol *kcontrol,
127                             struct snd_ctl_elem_value *ucontrol)
128 {
129         spk_unmute_state(ucontrol->value.integer.value[0]);
130         return 0;
131 }
132
133 /* This is added as a manual control as the speaker amps create clicks
134  * when their power state is changed, which are far more noticeable than
135  * anything produced by the CODEC itself.
136  */
137 static const struct snd_kcontrol_new amp_unmute_controls[] = {
138         SOC_SINGLE_EXT("Speaker Switch", 0, 0, 1, 0,
139                        speaker_unmute_get, speaker_unmute_put),
140 };
141
142 void simtec_audio_init(struct snd_soc_pcm_runtime *rtd)
143 {
144         struct snd_soc_codec *codec = rtd->codec;
145
146         if (pdata->amp_gpio > 0) {
147                 pr_debug("%s: adding amp routes\n", __func__);
148
149                 snd_soc_add_controls(codec, amp_unmute_controls,
150                                      ARRAY_SIZE(amp_unmute_controls));
151         }
152
153         if (pdata->amp_gain[0] > 0) {
154                 pr_debug("%s: adding amp controls\n", __func__);
155                 snd_soc_add_controls(codec, amp_gain_controls,
156                                      ARRAY_SIZE(amp_gain_controls));
157         }
158 }
159 EXPORT_SYMBOL_GPL(simtec_audio_init);
160
161 #define CODEC_CLOCK 12000000
162
163 /**
164  * simtec_hw_params - update hardware parameters
165  * @substream: The audio substream instance.
166  * @params: The parameters requested.
167  *
168  * Update the codec data routing and configuration  settings
169  * from the supplied data.
170  */
171 static int simtec_hw_params(struct snd_pcm_substream *substream,
172                             struct snd_pcm_hw_params *params)
173 {
174         struct snd_soc_pcm_runtime *rtd = substream->private_data;
175         struct snd_soc_dai *codec_dai = rtd->codec_dai;
176         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
177         int ret;
178
179         /* Set the CODEC as the bus clock master, I2S */
180         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
181                                   SND_SOC_DAIFMT_NB_NF |
182                                   SND_SOC_DAIFMT_CBM_CFM);
183         if (ret) {
184                 pr_err("%s: failed set cpu dai format\n", __func__);
185                 return ret;
186         }
187
188         /* Set the CODEC as the bus clock master */
189         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
190                                   SND_SOC_DAIFMT_NB_NF |
191                                   SND_SOC_DAIFMT_CBM_CFM);
192         if (ret) {
193                 pr_err("%s: failed set codec dai format\n", __func__);
194                 return ret;
195         }
196
197         ret = snd_soc_dai_set_sysclk(codec_dai, 0,
198                                      CODEC_CLOCK, SND_SOC_CLOCK_IN);
199         if (ret) {
200                 pr_err( "%s: failed setting codec sysclk\n", __func__);
201                 return ret;
202         }
203
204         if (pdata->use_mpllin) {
205                 ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_MPLL,
206                                              0, SND_SOC_CLOCK_OUT);
207
208                 if (ret) {
209                         pr_err("%s: failed to set MPLLin as clksrc\n",
210                                __func__);
211                         return ret;
212                 }
213         }
214
215         if (pdata->output_cdclk) {
216                 int cdclk_scale;
217
218                 cdclk_scale = clk_get_rate(xtal_clk) / CODEC_CLOCK;
219                 cdclk_scale--;
220
221                 ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
222                                              cdclk_scale);
223         }
224
225         return 0;
226 }
227
228 static int simtec_call_startup(struct s3c24xx_audio_simtec_pdata *pd)
229 {
230         /* call any board supplied startup code, this currently only
231          * covers the bast/vr1000 which have a CPLD in the way of the
232          * LRCLK */
233         if (pd->startup)
234                 pd->startup();
235
236         return 0;
237 }
238
239 static struct snd_soc_ops simtec_snd_ops = {
240         .hw_params      = simtec_hw_params,
241 };
242
243 /**
244  * attach_gpio_amp - get and configure the necessary gpios
245  * @dev: The device we're probing.
246  * @pd: The platform data supplied by the board.
247  *
248  * If there is a GPIO based amplifier attached to the board, claim
249  * the necessary GPIO lines for it, and set default values.
250  */
251 static int attach_gpio_amp(struct device *dev,
252                            struct s3c24xx_audio_simtec_pdata *pd)
253 {
254         int ret;
255
256         /* attach gpio amp gain (if any) */
257         if (pdata->amp_gain[0] > 0) {
258                 ret = gpio_request(pd->amp_gain[0], "gpio-amp-gain0");
259                 if (ret) {
260                         dev_err(dev, "cannot get amp gpio gain0\n");
261                         return ret;
262                 }
263
264                 ret = gpio_request(pd->amp_gain[1], "gpio-amp-gain1");
265                 if (ret) {
266                         dev_err(dev, "cannot get amp gpio gain1\n");
267                         gpio_free(pdata->amp_gain[0]);
268                         return ret;
269                 }
270
271                 gpio_direction_output(pd->amp_gain[0], 0);
272                 gpio_direction_output(pd->amp_gain[1], 0);
273         }
274
275         /* note, currently we assume GPA0 isn't valid amp */
276         if (pdata->amp_gpio > 0) {
277                 ret = gpio_request(pd->amp_gpio, "gpio-amp");
278                 if (ret) {
279                         dev_err(dev, "cannot get amp gpio %d (%d)\n",
280                                 pd->amp_gpio, ret);
281                         goto err_amp;
282                 }
283
284                 /* set the amp off at startup */
285                 spk_unmute_state(0);
286         }
287
288         return 0;
289
290 err_amp:
291         if (pd->amp_gain[0] > 0) {
292                 gpio_free(pd->amp_gain[0]);
293                 gpio_free(pd->amp_gain[1]);
294         }
295
296         return ret;
297 }
298
299 static void detach_gpio_amp(struct s3c24xx_audio_simtec_pdata *pd)
300 {
301         if (pd->amp_gain[0] > 0) {
302                 gpio_free(pd->amp_gain[0]);
303                 gpio_free(pd->amp_gain[1]);
304         }
305
306         if (pd->amp_gpio > 0)
307                 gpio_free(pd->amp_gpio);
308 }
309
310 #ifdef CONFIG_PM
311 int simtec_audio_resume(struct device *dev)
312 {
313         simtec_call_startup(pdata);
314         return 0;
315 }
316
317 const struct dev_pm_ops simtec_audio_pmops = {
318         .resume = simtec_audio_resume,
319 };
320 EXPORT_SYMBOL_GPL(simtec_audio_pmops);
321 #endif
322
323 int __devinit simtec_audio_core_probe(struct platform_device *pdev,
324                                       struct snd_soc_card *card)
325 {
326         struct platform_device *snd_dev;
327         int ret;
328
329         card->dai_link->ops = &simtec_snd_ops;
330
331         pdata = pdev->dev.platform_data;
332         if (!pdata) {
333                 dev_err(&pdev->dev, "no platform data supplied\n");
334                 return -EINVAL;
335         }
336
337         simtec_call_startup(pdata);
338
339         xtal_clk = clk_get(&pdev->dev, "xtal");
340         if (IS_ERR(xtal_clk)) {
341                 dev_err(&pdev->dev, "could not get clkout0\n");
342                 return -EINVAL;
343         }
344
345         dev_info(&pdev->dev, "xtal rate is %ld\n", clk_get_rate(xtal_clk));
346
347         ret = attach_gpio_amp(&pdev->dev, pdata);
348         if (ret)
349                 goto err_clk;
350
351         snd_dev = platform_device_alloc("soc-audio", -1);
352         if (!snd_dev) {
353                 dev_err(&pdev->dev, "failed to alloc soc-audio devicec\n");
354                 ret = -ENOMEM;
355                 goto err_gpio;
356         }
357
358         platform_set_drvdata(snd_dev, card);
359
360         ret = platform_device_add(snd_dev);
361         if (ret) {
362                 dev_err(&pdev->dev, "failed to add soc-audio dev\n");
363                 goto err_pdev;
364         }
365
366         platform_set_drvdata(pdev, snd_dev);
367         return 0;
368
369 err_pdev:
370         platform_device_put(snd_dev);
371
372 err_gpio:
373         detach_gpio_amp(pdata);
374
375 err_clk:
376         clk_put(xtal_clk);
377         return ret;
378 }
379 EXPORT_SYMBOL_GPL(simtec_audio_core_probe);
380
381 int __devexit simtec_audio_remove(struct platform_device *pdev)
382 {
383         struct platform_device *snd_dev = platform_get_drvdata(pdev);
384
385         platform_device_unregister(snd_dev);
386
387         detach_gpio_amp(pdata);
388         clk_put(xtal_clk);
389         return 0;
390 }
391 EXPORT_SYMBOL_GPL(simtec_audio_remove);
392
393 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
394 MODULE_DESCRIPTION("ALSA SoC Simtec Audio common support");
395 MODULE_LICENSE("GPL");