2 * ALSA SoC WL1273 codec driver
4 * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
6 * Copyright: (C) 2010, 2011 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 <linux/module.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.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
->client
->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
= core
->write(core
, WL1273_I2S_MODE_CONFIG_SET
, mode
);
131 core
->i2s_mode
= mode
;
132 r
= core
->write(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 device
*dev
= &core
->client
->dev
;
149 dev_dbg(dev
, "%s\n", __func__
);
151 mutex_lock(&core
->lock
);
153 if (core
->channel_number
== channel_number
)
156 if (channel_number
== 1 && core
->mode
== WL1273_MODE_RX
)
157 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_MONO
);
158 else if (channel_number
== 1 && core
->mode
== WL1273_MODE_TX
)
159 r
= core
->write(core
, WL1273_MONO_SET
, WL1273_TX_MONO
);
160 else if (channel_number
== 2 && core
->mode
== WL1273_MODE_RX
)
161 r
= core
->write(core
, WL1273_MOST_MODE_SET
, WL1273_RX_STEREO
);
162 else if (channel_number
== 2 && core
->mode
== WL1273_MODE_TX
)
163 r
= core
->write(core
, WL1273_MONO_SET
, WL1273_TX_STEREO
);
167 mutex_unlock(&core
->lock
);
172 static int snd_wl1273_get_audio_route(struct snd_kcontrol
*kcontrol
,
173 struct snd_ctl_elem_value
*ucontrol
)
175 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
176 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
178 ucontrol
->value
.integer
.value
[0] = wl1273
->mode
;
184 * TODO: Implement the audio routing in the driver. Now this control
185 * only indicates the setting that has been done elsewhere (in the user
188 static const char * const 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 if (wl1273
->mode
== ucontrol
->value
.integer
.value
[0])
199 /* Do not allow changes while stream is running */
203 if (ucontrol
->value
.integer
.value
[0] < 0 ||
204 ucontrol
->value
.integer
.value
[0] >= ARRAY_SIZE(wl1273_audio_route
))
207 wl1273
->mode
= ucontrol
->value
.integer
.value
[0];
212 static const struct soc_enum wl1273_enum
=
213 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route
), wl1273_audio_route
);
215 static int snd_wl1273_fm_audio_get(struct snd_kcontrol
*kcontrol
,
216 struct snd_ctl_elem_value
*ucontrol
)
218 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
219 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
221 dev_dbg(codec
->dev
, "%s: enter.\n", __func__
);
223 ucontrol
->value
.integer
.value
[0] = wl1273
->core
->audio_mode
;
228 static int snd_wl1273_fm_audio_put(struct snd_kcontrol
*kcontrol
,
229 struct snd_ctl_elem_value
*ucontrol
)
231 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
232 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
235 dev_dbg(codec
->dev
, "%s: enter.\n", __func__
);
237 val
= ucontrol
->value
.integer
.value
[0];
238 if (wl1273
->core
->audio_mode
== val
)
241 r
= wl1273
->core
->set_audio(wl1273
->core
, val
);
248 static const char * const wl1273_audio_strings
[] = { "Digital", "Analog" };
250 static const struct soc_enum wl1273_audio_enum
=
251 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings
),
252 wl1273_audio_strings
);
254 static int snd_wl1273_fm_volume_get(struct snd_kcontrol
*kcontrol
,
255 struct snd_ctl_elem_value
*ucontrol
)
257 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
258 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
260 dev_dbg(codec
->dev
, "%s: enter.\n", __func__
);
262 ucontrol
->value
.integer
.value
[0] = wl1273
->core
->volume
;
267 static int snd_wl1273_fm_volume_put(struct snd_kcontrol
*kcontrol
,
268 struct snd_ctl_elem_value
*ucontrol
)
270 struct snd_soc_codec
*codec
= snd_kcontrol_chip(kcontrol
);
271 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
274 dev_dbg(codec
->dev
, "%s: enter.\n", __func__
);
276 r
= wl1273
->core
->set_volume(wl1273
->core
,
277 ucontrol
->value
.integer
.value
[0]);
284 static const struct snd_kcontrol_new wl1273_controls
[] = {
285 SOC_ENUM_EXT("Codec Mode", wl1273_enum
,
286 snd_wl1273_get_audio_route
, snd_wl1273_set_audio_route
),
287 SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum
,
288 snd_wl1273_fm_audio_get
, snd_wl1273_fm_audio_put
),
289 SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME
, 0,
290 snd_wl1273_fm_volume_get
, snd_wl1273_fm_volume_put
),
293 static const struct snd_soc_dapm_widget wl1273_dapm_widgets
[] = {
294 SND_SOC_DAPM_INPUT("RX"),
296 SND_SOC_DAPM_OUTPUT("TX"),
299 static const struct snd_soc_dapm_route wl1273_dapm_routes
[] = {
300 { "Capture", NULL
, "RX" },
302 { "TX", NULL
, "Playback" },
305 static int wl1273_startup(struct snd_pcm_substream
*substream
,
306 struct snd_soc_dai
*dai
)
308 struct snd_soc_codec
*codec
= dai
->codec
;
309 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
311 switch (wl1273
->mode
) {
313 snd_pcm_hw_constraint_minmax(substream
->runtime
,
314 SNDRV_PCM_HW_PARAM_RATE
,
316 snd_pcm_hw_constraint_minmax(substream
->runtime
,
317 SNDRV_PCM_HW_PARAM_CHANNELS
, 1, 1);
319 case WL1273_MODE_FM_RX
:
320 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
321 pr_err("Cannot play in RX mode.\n");
325 case WL1273_MODE_FM_TX
:
326 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
327 pr_err("Cannot capture in TX mode.\n");
339 static int wl1273_hw_params(struct snd_pcm_substream
*substream
,
340 struct snd_pcm_hw_params
*params
,
341 struct snd_soc_dai
*dai
)
343 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(dai
->codec
);
344 struct wl1273_core
*core
= wl1273
->core
;
345 unsigned int rate
, width
, r
;
347 if (params_format(params
) != SNDRV_PCM_FORMAT_S16_LE
) {
348 pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
352 rate
= params_rate(params
);
353 width
= hw_param_interval(params
, SNDRV_PCM_HW_PARAM_SAMPLE_BITS
)->min
;
355 if (wl1273
->mode
== WL1273_MODE_BT
) {
357 pr_err("Rate %d not supported.\n", params_rate(params
));
361 if (params_channels(params
) != 1) {
362 pr_err("Only mono supported.\n");
369 if (wl1273
->mode
== WL1273_MODE_FM_TX
&&
370 substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
) {
371 pr_err("Only playback supported with TX.\n");
375 if (wl1273
->mode
== WL1273_MODE_FM_RX
&&
376 substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
377 pr_err("Only capture supported with RX.\n");
381 if (wl1273
->mode
!= WL1273_MODE_FM_RX
&&
382 wl1273
->mode
!= WL1273_MODE_FM_TX
) {
383 pr_err("Unexpected mode: %d.\n", wl1273
->mode
);
387 r
= snd_wl1273_fm_set_i2s_mode(core
, rate
, width
);
391 wl1273
->channels
= params_channels(params
);
392 r
= snd_wl1273_fm_set_channel_number(core
, wl1273
->channels
);
399 static const struct snd_soc_dai_ops wl1273_dai_ops
= {
400 .startup
= wl1273_startup
,
401 .hw_params
= wl1273_hw_params
,
404 static struct snd_soc_dai_driver wl1273_dai
= {
407 .stream_name
= "Playback",
410 .rates
= SNDRV_PCM_RATE_8000_48000
,
411 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
413 .stream_name
= "Capture",
416 .rates
= SNDRV_PCM_RATE_8000_48000
,
417 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
418 .ops
= &wl1273_dai_ops
,
421 /* Audio interface format for the soc_card driver */
422 int wl1273_get_format(struct snd_soc_codec
*codec
, unsigned int *fmt
)
424 struct wl1273_priv
*wl1273
;
426 if (codec
== NULL
|| fmt
== NULL
)
429 wl1273
= snd_soc_codec_get_drvdata(codec
);
431 switch (wl1273
->mode
) {
432 case WL1273_MODE_FM_RX
:
433 case WL1273_MODE_FM_TX
:
434 *fmt
= SND_SOC_DAIFMT_I2S
|
435 SND_SOC_DAIFMT_NB_NF
|
436 SND_SOC_DAIFMT_CBM_CFM
;
440 *fmt
= SND_SOC_DAIFMT_DSP_A
|
441 SND_SOC_DAIFMT_IB_NF
|
442 SND_SOC_DAIFMT_CBM_CFM
;
451 EXPORT_SYMBOL_GPL(wl1273_get_format
);
453 static int wl1273_probe(struct snd_soc_codec
*codec
)
455 struct wl1273_core
**core
= codec
->dev
->platform_data
;
456 struct wl1273_priv
*wl1273
;
459 dev_dbg(codec
->dev
, "%s.\n", __func__
);
462 dev_err(codec
->dev
, "Platform data is missing.\n");
466 wl1273
= kzalloc(sizeof(struct wl1273_priv
), GFP_KERNEL
);
467 if (wl1273
== NULL
) {
468 dev_err(codec
->dev
, "Cannot allocate memory.\n");
472 wl1273
->mode
= WL1273_MODE_BT
;
473 wl1273
->core
= *core
;
475 snd_soc_codec_set_drvdata(codec
, wl1273
);
477 r
= snd_soc_add_codec_controls(codec
, wl1273_controls
,
478 ARRAY_SIZE(wl1273_controls
));
485 static int wl1273_remove(struct snd_soc_codec
*codec
)
487 struct wl1273_priv
*wl1273
= snd_soc_codec_get_drvdata(codec
);
489 dev_dbg(codec
->dev
, "%s\n", __func__
);
495 static struct snd_soc_codec_driver soc_codec_dev_wl1273
= {
496 .probe
= wl1273_probe
,
497 .remove
= wl1273_remove
,
499 .dapm_widgets
= wl1273_dapm_widgets
,
500 .num_dapm_widgets
= ARRAY_SIZE(wl1273_dapm_widgets
),
501 .dapm_routes
= wl1273_dapm_routes
,
502 .num_dapm_routes
= ARRAY_SIZE(wl1273_dapm_routes
),
505 static int wl1273_platform_probe(struct platform_device
*pdev
)
507 return snd_soc_register_codec(&pdev
->dev
, &soc_codec_dev_wl1273
,
511 static int wl1273_platform_remove(struct platform_device
*pdev
)
513 snd_soc_unregister_codec(&pdev
->dev
);
517 MODULE_ALIAS("platform:wl1273-codec");
519 static struct platform_driver wl1273_platform_driver
= {
521 .name
= "wl1273-codec",
522 .owner
= THIS_MODULE
,
524 .probe
= wl1273_platform_probe
,
525 .remove
= wl1273_platform_remove
,
528 module_platform_driver(wl1273_platform_driver
);
530 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
531 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
532 MODULE_LICENSE("GPL");