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