2 * ALSA SoC WL1273 codec driver
4 * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
6 * Copyright: (C) 2010 Nokia Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/mfd/wl1273-core.h>
25 #include <linux/slab.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc-dai.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
34 enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
36 /* codec private data */
38 enum wl1273_mode mode;
39 struct wl1273_core *core;
40 unsigned int channels;
43 static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
46 struct device *dev = &core->i2c_dev->dev;
50 dev_dbg(dev, "rate: %d\n", rate);
51 dev_dbg(dev, "width: %d\n", width);
53 mutex_lock(&core->lock);
55 mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
59 mode |= WL1273_IS2_RATE_48K;
62 mode |= WL1273_IS2_RATE_44_1K;
65 mode |= WL1273_IS2_RATE_32K;
68 mode |= WL1273_IS2_RATE_22_05K;
71 mode |= WL1273_IS2_RATE_16K;
74 mode |= WL1273_IS2_RATE_12K;
77 mode |= WL1273_IS2_RATE_11_025;
80 mode |= WL1273_IS2_RATE_8K;
83 dev_err(dev, "Sampling rate: %d not supported\n", rate);
90 mode |= WL1273_IS2_WIDTH_32;
93 mode |= WL1273_IS2_WIDTH_40;
96 mode |= WL1273_IS2_WIDTH_48;
99 mode |= WL1273_IS2_WIDTH_50;
102 mode |= WL1273_IS2_WIDTH_60;
105 mode |= WL1273_IS2_WIDTH_64;
108 mode |= WL1273_IS2_WIDTH_80;
111 mode |= WL1273_IS2_WIDTH_96;
114 mode |= WL1273_IS2_WIDTH_128;
117 dev_err(dev, "Data width: %d not supported\n", width);
122 dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n", WL1273_I2S_DEF_MODE);
123 dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
124 dev_dbg(dev, "mode: 0x%04x\n", mode);
126 if (core->i2s_mode != mode) {
127 r = wl1273_fm_write_cmd(core, WL1273_I2S_MODE_CONFIG_SET, mode);
131 core->i2s_mode = mode;
132 r = wl1273_fm_write_cmd(core, WL1273_AUDIO_ENABLE,
133 WL1273_AUDIO_ENABLE_I2S);
138 mutex_unlock(&core->lock);
143 static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
146 struct i2c_client *client = core->i2c_dev;
147 struct device *dev = &client->dev;
150 dev_dbg(dev, "%s\n", __func__);
152 mutex_lock(&core->lock);
154 if (core->channel_number == channel_number)
157 if (channel_number == 1 && core->mode == WL1273_MODE_RX)
158 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
160 else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
161 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET,
163 else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
164 r = wl1273_fm_write_cmd(core, WL1273_MOST_MODE_SET,
166 else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
167 r = wl1273_fm_write_cmd(core, WL1273_MONO_SET,
172 mutex_unlock(&core->lock);
177 static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_value *ucontrol)
180 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
181 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
183 ucontrol->value.integer.value[0] = wl1273->mode;
188 static const char *wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
190 static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_value *ucontrol)
193 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
194 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
196 /* Do not allow changes while stream is running */
200 if (ucontrol->value.integer.value[0] < 0 ||
201 ucontrol->value.integer.value[0] >= ARRAY_SIZE(wl1273_audio_route))
204 wl1273->mode = ucontrol->value.integer.value[0];
209 static const struct soc_enum wl1273_enum =
210 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route), wl1273_audio_route);
212 static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
215 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
216 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
218 dev_dbg(codec->dev, "%s: enter.\n", __func__);
220 ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
225 static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_value *ucontrol)
228 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
229 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
232 dev_dbg(codec->dev, "%s: enter.\n", __func__);
234 val = ucontrol->value.integer.value[0];
235 if (wl1273->core->audio_mode == val)
238 r = wl1273_fm_set_audio(wl1273->core, val);
245 static const char *wl1273_audio_strings[] = { "Digital", "Analog" };
247 static const struct soc_enum wl1273_audio_enum =
248 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings),
249 wl1273_audio_strings);
251 static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_value *ucontrol)
254 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
255 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
257 dev_dbg(codec->dev, "%s: enter.\n", __func__);
259 ucontrol->value.integer.value[0] = wl1273->core->volume;
264 static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
265 struct snd_ctl_elem_value *ucontrol)
267 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
268 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
271 dev_dbg(codec->dev, "%s: enter.\n", __func__);
273 r = wl1273_fm_set_volume(wl1273->core,
274 ucontrol->value.integer.value[0]);
281 static const struct snd_kcontrol_new wl1273_controls[] = {
282 SOC_ENUM_EXT("Codec Mode", wl1273_enum,
283 snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
284 SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
285 snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
286 SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
287 snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
290 static int wl1273_startup(struct snd_pcm_substream *substream,
291 struct snd_soc_dai *dai)
293 struct snd_soc_pcm_runtime *rtd = substream->private_data;
294 struct snd_soc_codec *codec = rtd->codec;
295 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
297 switch (wl1273->mode) {
299 snd_pcm_hw_constraint_minmax(substream->runtime,
300 SNDRV_PCM_HW_PARAM_RATE,
302 snd_pcm_hw_constraint_minmax(substream->runtime,
303 SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
305 case WL1273_MODE_FM_RX:
306 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
307 pr_err("Cannot play in RX mode.\n");
311 case WL1273_MODE_FM_TX:
312 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
313 pr_err("Cannot capture in TX mode.\n");
325 static int wl1273_hw_params(struct snd_pcm_substream *substream,
326 struct snd_pcm_hw_params *params,
327 struct snd_soc_dai *dai)
329 struct snd_soc_pcm_runtime *rtd = substream->private_data;
330 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(rtd->codec);
331 struct wl1273_core *core = wl1273->core;
332 unsigned int rate, width, r;
334 if (params_format(params) != SNDRV_PCM_FORMAT_S16_LE) {
335 pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
339 rate = params_rate(params);
340 width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
342 if (wl1273->mode == WL1273_MODE_BT) {
344 pr_err("Rate %d not supported.\n", params_rate(params));
348 if (params_channels(params) != 1) {
349 pr_err("Only mono supported.\n");
356 if (wl1273->mode == WL1273_MODE_FM_TX &&
357 substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
358 pr_err("Only playback supported with TX.\n");
362 if (wl1273->mode == WL1273_MODE_FM_RX &&
363 substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
364 pr_err("Only capture supported with RX.\n");
368 if (wl1273->mode != WL1273_MODE_FM_RX &&
369 wl1273->mode != WL1273_MODE_FM_TX) {
370 pr_err("Unexpected mode: %d.\n", wl1273->mode);
374 r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
378 wl1273->channels = params_channels(params);
379 r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
386 static struct snd_soc_dai_ops wl1273_dai_ops = {
387 .startup = wl1273_startup,
388 .hw_params = wl1273_hw_params,
391 static struct snd_soc_dai_driver wl1273_dai = {
394 .stream_name = "Playback",
397 .rates = SNDRV_PCM_RATE_8000_48000,
398 .formats = SNDRV_PCM_FMTBIT_S16_LE},
400 .stream_name = "Capture",
403 .rates = SNDRV_PCM_RATE_8000_48000,
404 .formats = SNDRV_PCM_FMTBIT_S16_LE},
405 .ops = &wl1273_dai_ops,
408 /* Audio interface format for the soc_card driver */
409 int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
411 struct wl1273_priv *wl1273;
413 if (codec == NULL || fmt == NULL)
416 wl1273 = snd_soc_codec_get_drvdata(codec);
418 switch (wl1273->mode) {
419 case WL1273_MODE_FM_RX:
420 case WL1273_MODE_FM_TX:
421 *fmt = SND_SOC_DAIFMT_I2S |
422 SND_SOC_DAIFMT_NB_NF |
423 SND_SOC_DAIFMT_CBM_CFM;
427 *fmt = SND_SOC_DAIFMT_DSP_A |
428 SND_SOC_DAIFMT_IB_NF |
429 SND_SOC_DAIFMT_CBM_CFM;
438 EXPORT_SYMBOL_GPL(wl1273_get_format);
440 static int wl1273_probe(struct snd_soc_codec *codec)
442 struct wl1273_core **core = codec->dev->platform_data;
443 struct wl1273_priv *wl1273;
446 dev_dbg(codec->dev, "%s.\n", __func__);
449 dev_err(codec->dev, "Platform data is missing.\n");
453 wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
454 if (wl1273 == NULL) {
455 dev_err(codec->dev, "Cannot allocate memory.\n");
459 wl1273->mode = WL1273_MODE_BT;
460 wl1273->core = *core;
462 snd_soc_codec_set_drvdata(codec, wl1273);
463 mutex_init(&codec->mutex);
465 r = snd_soc_add_controls(codec, wl1273_controls,
466 ARRAY_SIZE(wl1273_controls));
473 static int wl1273_remove(struct snd_soc_codec *codec)
475 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
477 dev_dbg(codec->dev, "%s\n", __func__);
483 static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
484 .probe = wl1273_probe,
485 .remove = wl1273_remove,
488 static int __devinit wl1273_platform_probe(struct platform_device *pdev)
490 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
494 static int __devexit wl1273_platform_remove(struct platform_device *pdev)
496 snd_soc_unregister_codec(&pdev->dev);
500 MODULE_ALIAS("platform:wl1273-codec");
502 static struct platform_driver wl1273_platform_driver = {
504 .name = "wl1273-codec",
505 .owner = THIS_MODULE,
507 .probe = wl1273_platform_probe,
508 .remove = __devexit_p(wl1273_platform_remove),
511 static int __init wl1273_init(void)
513 return platform_driver_register(&wl1273_platform_driver);
515 module_init(wl1273_init);
517 static void __exit wl1273_exit(void)
519 platform_driver_unregister(&wl1273_platform_driver);
521 module_exit(wl1273_exit);
523 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
524 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
525 MODULE_LICENSE("GPL");