1
/* sound/soc/samsung/s3c24xx_simtec.c
3
* Copyright 2009 Simtec Electronics
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.
10
#include <linux/gpio.h>
11
#include <linux/clk.h>
13
#include <sound/soc.h>
15
#include <plat/audio-simtec.h>
17
#include "s3c24xx-i2s.h"
18
#include "s3c24xx_simtec.h"
20
static struct s3c24xx_audio_simtec_pdata *pdata;
21
static struct clk *xtal_clk;
24
static int spk_unmute;
27
* speaker_gain_get - read the speaker gain setting.
28
* @kcontrol: The control for the speaker gain.
29
* @ucontrol: The value that needs to be updated.
31
* Read the value for the AMP gain control.
33
static int speaker_gain_get(struct snd_kcontrol *kcontrol,
34
struct snd_ctl_elem_value *ucontrol)
36
ucontrol->value.integer.value[0] = spk_gain;
41
* speaker_gain_set - set the value of the speaker amp gain
42
* @value: The value to write.
44
static void speaker_gain_set(int value)
46
gpio_set_value_cansleep(pdata->amp_gain[0], value & 1);
47
gpio_set_value_cansleep(pdata->amp_gain[1], value >> 1);
51
* speaker_gain_put - set the speaker gain setting.
52
* @kcontrol: The control for the speaker gain.
53
* @ucontrol: The value that needs to be set.
55
* Set the value of the speaker gain from the specified
58
* Note, if the speaker amp is muted, then we do not set a gain value
59
* as at-least one of the ICs that is fitted will try and power up even
60
* if the main control is set to off.
62
static int speaker_gain_put(struct snd_kcontrol *kcontrol,
63
struct snd_ctl_elem_value *ucontrol)
65
int value = ucontrol->value.integer.value[0];
70
speaker_gain_set(value);
75
static const struct snd_kcontrol_new amp_gain_controls[] = {
76
SOC_SINGLE_EXT("Speaker Gain", 0, 0, 3, 0,
77
speaker_gain_get, speaker_gain_put),
81
* spk_unmute_state - set the unmute state of the speaker
82
* @to: zero to unmute, non-zero to ununmute.
84
static void spk_unmute_state(int to)
86
pr_debug("%s: to=%d\n", __func__, to);
89
gpio_set_value(pdata->amp_gpio, to);
91
/* if we're umuting, also re-set the gain */
92
if (to && pdata->amp_gain[0] > 0)
93
speaker_gain_set(spk_gain);
97
* speaker_unmute_get - read the speaker unmute setting.
98
* @kcontrol: The control for the speaker gain.
99
* @ucontrol: The value that needs to be updated.
101
* Read the value for the AMP gain control.
103
static int speaker_unmute_get(struct snd_kcontrol *kcontrol,
104
struct snd_ctl_elem_value *ucontrol)
106
ucontrol->value.integer.value[0] = spk_unmute;
111
* speaker_unmute_put - set the speaker unmute setting.
112
* @kcontrol: The control for the speaker gain.
113
* @ucontrol: The value that needs to be set.
115
* Set the value of the speaker gain from the specified
118
static int speaker_unmute_put(struct snd_kcontrol *kcontrol,
119
struct snd_ctl_elem_value *ucontrol)
121
spk_unmute_state(ucontrol->value.integer.value[0]);
125
/* This is added as a manual control as the speaker amps create clicks
126
* when their power state is changed, which are far more noticeable than
127
* anything produced by the CODEC itself.
129
static const struct snd_kcontrol_new amp_unmute_controls[] = {
130
SOC_SINGLE_EXT("Speaker Switch", 0, 0, 1, 0,
131
speaker_unmute_get, speaker_unmute_put),
134
void simtec_audio_init(struct snd_soc_pcm_runtime *rtd)
136
struct snd_soc_codec *codec = rtd->codec;
138
if (pdata->amp_gpio > 0) {
139
pr_debug("%s: adding amp routes\n", __func__);
141
snd_soc_add_controls(codec, amp_unmute_controls,
142
ARRAY_SIZE(amp_unmute_controls));
145
if (pdata->amp_gain[0] > 0) {
146
pr_debug("%s: adding amp controls\n", __func__);
147
snd_soc_add_controls(codec, amp_gain_controls,
148
ARRAY_SIZE(amp_gain_controls));
151
EXPORT_SYMBOL_GPL(simtec_audio_init);
153
#define CODEC_CLOCK 12000000
156
* simtec_hw_params - update hardware parameters
157
* @substream: The audio substream instance.
158
* @params: The parameters requested.
160
* Update the codec data routing and configuration settings
161
* from the supplied data.
163
static int simtec_hw_params(struct snd_pcm_substream *substream,
164
struct snd_pcm_hw_params *params)
166
struct snd_soc_pcm_runtime *rtd = substream->private_data;
167
struct snd_soc_dai *codec_dai = rtd->codec_dai;
168
struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
171
/* Set the CODEC as the bus clock master, I2S */
172
ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
173
SND_SOC_DAIFMT_NB_NF |
174
SND_SOC_DAIFMT_CBM_CFM);
176
pr_err("%s: failed set cpu dai format\n", __func__);
180
/* Set the CODEC as the bus clock master */
181
ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
182
SND_SOC_DAIFMT_NB_NF |
183
SND_SOC_DAIFMT_CBM_CFM);
185
pr_err("%s: failed set codec dai format\n", __func__);
189
ret = snd_soc_dai_set_sysclk(codec_dai, 0,
190
CODEC_CLOCK, SND_SOC_CLOCK_IN);
192
pr_err( "%s: failed setting codec sysclk\n", __func__);
196
if (pdata->use_mpllin) {
197
ret = snd_soc_dai_set_sysclk(cpu_dai, S3C24XX_CLKSRC_MPLL,
198
0, SND_SOC_CLOCK_OUT);
201
pr_err("%s: failed to set MPLLin as clksrc\n",
207
if (pdata->output_cdclk) {
210
cdclk_scale = clk_get_rate(xtal_clk) / CODEC_CLOCK;
213
ret = snd_soc_dai_set_clkdiv(cpu_dai, S3C24XX_DIV_PRESCALER,
220
static int simtec_call_startup(struct s3c24xx_audio_simtec_pdata *pd)
222
/* call any board supplied startup code, this currently only
223
* covers the bast/vr1000 which have a CPLD in the way of the
231
static struct snd_soc_ops simtec_snd_ops = {
232
.hw_params = simtec_hw_params,
236
* attach_gpio_amp - get and configure the necessary gpios
237
* @dev: The device we're probing.
238
* @pd: The platform data supplied by the board.
240
* If there is a GPIO based amplifier attached to the board, claim
241
* the necessary GPIO lines for it, and set default values.
243
static int attach_gpio_amp(struct device *dev,
244
struct s3c24xx_audio_simtec_pdata *pd)
248
/* attach gpio amp gain (if any) */
249
if (pdata->amp_gain[0] > 0) {
250
ret = gpio_request(pd->amp_gain[0], "gpio-amp-gain0");
252
dev_err(dev, "cannot get amp gpio gain0\n");
256
ret = gpio_request(pd->amp_gain[1], "gpio-amp-gain1");
258
dev_err(dev, "cannot get amp gpio gain1\n");
259
gpio_free(pdata->amp_gain[0]);
263
gpio_direction_output(pd->amp_gain[0], 0);
264
gpio_direction_output(pd->amp_gain[1], 0);
267
/* note, currently we assume GPA0 isn't valid amp */
268
if (pdata->amp_gpio > 0) {
269
ret = gpio_request(pd->amp_gpio, "gpio-amp");
271
dev_err(dev, "cannot get amp gpio %d (%d)\n",
276
/* set the amp off at startup */
283
if (pd->amp_gain[0] > 0) {
284
gpio_free(pd->amp_gain[0]);
285
gpio_free(pd->amp_gain[1]);
291
static void detach_gpio_amp(struct s3c24xx_audio_simtec_pdata *pd)
293
if (pd->amp_gain[0] > 0) {
294
gpio_free(pd->amp_gain[0]);
295
gpio_free(pd->amp_gain[1]);
298
if (pd->amp_gpio > 0)
299
gpio_free(pd->amp_gpio);
303
int simtec_audio_resume(struct device *dev)
305
simtec_call_startup(pdata);
309
const struct dev_pm_ops simtec_audio_pmops = {
310
.resume = simtec_audio_resume,
312
EXPORT_SYMBOL_GPL(simtec_audio_pmops);
315
int __devinit simtec_audio_core_probe(struct platform_device *pdev,
316
struct snd_soc_card *card)
318
struct platform_device *snd_dev;
321
card->dai_link->ops = &simtec_snd_ops;
323
pdata = pdev->dev.platform_data;
325
dev_err(&pdev->dev, "no platform data supplied\n");
329
simtec_call_startup(pdata);
331
xtal_clk = clk_get(&pdev->dev, "xtal");
332
if (IS_ERR(xtal_clk)) {
333
dev_err(&pdev->dev, "could not get clkout0\n");
337
dev_info(&pdev->dev, "xtal rate is %ld\n", clk_get_rate(xtal_clk));
339
ret = attach_gpio_amp(&pdev->dev, pdata);
343
snd_dev = platform_device_alloc("soc-audio", -1);
345
dev_err(&pdev->dev, "failed to alloc soc-audio devicec\n");
350
platform_set_drvdata(snd_dev, card);
352
ret = platform_device_add(snd_dev);
354
dev_err(&pdev->dev, "failed to add soc-audio dev\n");
358
platform_set_drvdata(pdev, snd_dev);
362
platform_device_put(snd_dev);
365
detach_gpio_amp(pdata);
371
EXPORT_SYMBOL_GPL(simtec_audio_core_probe);
373
int __devexit simtec_audio_remove(struct platform_device *pdev)
375
struct platform_device *snd_dev = platform_get_drvdata(pdev);
377
platform_device_unregister(snd_dev);
379
detach_gpio_amp(pdata);
383
EXPORT_SYMBOL_GPL(simtec_audio_remove);
385
MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
386
MODULE_DESCRIPTION("ALSA SoC Simtec Audio common support");
387
MODULE_LICENSE("GPL");