1 // SPDX-License-Identifier: GPL-2.0-only
3 * ALSA SoC WL1273 codec driver
5 * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
7 * Copyright: (C) 2010, 2011 Nokia Corporation
10 #include <linux/mfd/wl1273-core.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/initval.h>
20 enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
22 /* codec private data */
24 enum wl1273_mode mode;
25 struct wl1273_core *core;
26 unsigned int channels;
29 static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
32 struct device *dev = &core->client->dev;
36 dev_dbg(dev, "rate: %d\n", rate);
37 dev_dbg(dev, "width: %d\n", width);
39 mutex_lock(&core->lock);
41 mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
45 mode |= WL1273_IS2_RATE_48K;
48 mode |= WL1273_IS2_RATE_44_1K;
51 mode |= WL1273_IS2_RATE_32K;
54 mode |= WL1273_IS2_RATE_22_05K;
57 mode |= WL1273_IS2_RATE_16K;
60 mode |= WL1273_IS2_RATE_12K;
63 mode |= WL1273_IS2_RATE_11_025;
66 mode |= WL1273_IS2_RATE_8K;
69 dev_err(dev, "Sampling rate: %d not supported\n", rate);
76 mode |= WL1273_IS2_WIDTH_32;
79 mode |= WL1273_IS2_WIDTH_40;
82 mode |= WL1273_IS2_WIDTH_48;
85 mode |= WL1273_IS2_WIDTH_50;
88 mode |= WL1273_IS2_WIDTH_60;
91 mode |= WL1273_IS2_WIDTH_64;
94 mode |= WL1273_IS2_WIDTH_80;
97 mode |= WL1273_IS2_WIDTH_96;
100 mode |= WL1273_IS2_WIDTH_128;
103 dev_err(dev, "Data width: %d not supported\n", width);
108 dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n", WL1273_I2S_DEF_MODE);
109 dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
110 dev_dbg(dev, "mode: 0x%04x\n", mode);
112 if (core->i2s_mode != mode) {
113 r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
117 core->i2s_mode = mode;
118 r = core->write(core, WL1273_AUDIO_ENABLE,
119 WL1273_AUDIO_ENABLE_I2S);
124 mutex_unlock(&core->lock);
129 static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
132 struct device *dev = &core->client->dev;
135 dev_dbg(dev, "%s\n", __func__);
137 mutex_lock(&core->lock);
139 if (core->channel_number == channel_number)
142 if (channel_number == 1 && core->mode == WL1273_MODE_RX)
143 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
144 else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
145 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
146 else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
147 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
148 else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
149 r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
153 mutex_unlock(&core->lock);
158 static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
159 struct snd_ctl_elem_value *ucontrol)
161 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
162 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
164 ucontrol->value.enumerated.item[0] = wl1273->mode;
170 * TODO: Implement the audio routing in the driver. Now this control
171 * only indicates the setting that has been done elsewhere (in the user
174 static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
176 static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
177 struct snd_ctl_elem_value *ucontrol)
179 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
180 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
182 if (wl1273->mode == ucontrol->value.enumerated.item[0])
185 /* Do not allow changes while stream is running */
186 if (snd_soc_component_is_active(component))
189 if (ucontrol->value.enumerated.item[0] >= ARRAY_SIZE(wl1273_audio_route))
192 wl1273->mode = ucontrol->value.enumerated.item[0];
197 static SOC_ENUM_SINGLE_EXT_DECL(wl1273_enum, wl1273_audio_route);
199 static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
200 struct snd_ctl_elem_value *ucontrol)
202 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
203 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
205 dev_dbg(component->dev, "%s: enter.\n", __func__);
207 ucontrol->value.enumerated.item[0] = wl1273->core->audio_mode;
212 static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
213 struct snd_ctl_elem_value *ucontrol)
215 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
216 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
219 dev_dbg(component->dev, "%s: enter.\n", __func__);
221 val = ucontrol->value.enumerated.item[0];
222 if (wl1273->core->audio_mode == val)
225 r = wl1273->core->set_audio(wl1273->core, val);
232 static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
234 static SOC_ENUM_SINGLE_EXT_DECL(wl1273_audio_enum, wl1273_audio_strings);
236 static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
237 struct snd_ctl_elem_value *ucontrol)
239 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
240 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
242 dev_dbg(component->dev, "%s: enter.\n", __func__);
244 ucontrol->value.integer.value[0] = wl1273->core->volume;
249 static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
250 struct snd_ctl_elem_value *ucontrol)
252 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
253 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
256 dev_dbg(component->dev, "%s: enter.\n", __func__);
258 r = wl1273->core->set_volume(wl1273->core,
259 ucontrol->value.integer.value[0]);
266 static const struct snd_kcontrol_new wl1273_controls[] = {
267 SOC_ENUM_EXT("Codec Mode", wl1273_enum,
268 snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
269 SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
270 snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
271 SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
272 snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
275 static const struct snd_soc_dapm_widget wl1273_dapm_widgets[] = {
276 SND_SOC_DAPM_INPUT("RX"),
278 SND_SOC_DAPM_OUTPUT("TX"),
281 static const struct snd_soc_dapm_route wl1273_dapm_routes[] = {
282 { "Capture", NULL, "RX" },
284 { "TX", NULL, "Playback" },
287 static int wl1273_startup(struct snd_pcm_substream *substream,
288 struct snd_soc_dai *dai)
290 struct snd_soc_component *component = dai->component;
291 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
293 switch (wl1273->mode) {
295 snd_pcm_hw_constraint_single(substream->runtime,
296 SNDRV_PCM_HW_PARAM_RATE, 8000);
297 snd_pcm_hw_constraint_single(substream->runtime,
298 SNDRV_PCM_HW_PARAM_CHANNELS, 1);
300 case WL1273_MODE_FM_RX:
301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
302 pr_err("Cannot play in RX mode.\n");
306 case WL1273_MODE_FM_TX:
307 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
308 pr_err("Cannot capture in TX mode.\n");
320 static int wl1273_hw_params(struct snd_pcm_substream *substream,
321 struct snd_pcm_hw_params *params,
322 struct snd_soc_dai *dai)
324 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(dai->component);
325 struct wl1273_core *core = wl1273->core;
326 unsigned int rate, width, r;
328 if (params_width(params) != 16) {
329 dev_err(dai->dev, "%d bits/sample not supported\n",
330 params_width(params));
334 rate = params_rate(params);
335 width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
337 if (wl1273->mode == WL1273_MODE_BT) {
339 pr_err("Rate %d not supported.\n", params_rate(params));
343 if (params_channels(params) != 1) {
344 pr_err("Only mono supported.\n");
351 if (wl1273->mode == WL1273_MODE_FM_TX &&
352 substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
353 pr_err("Only playback supported with TX.\n");
357 if (wl1273->mode == WL1273_MODE_FM_RX &&
358 substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
359 pr_err("Only capture supported with RX.\n");
363 if (wl1273->mode != WL1273_MODE_FM_RX &&
364 wl1273->mode != WL1273_MODE_FM_TX) {
365 pr_err("Unexpected mode: %d.\n", wl1273->mode);
369 r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
373 wl1273->channels = params_channels(params);
374 r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
381 static const struct snd_soc_dai_ops wl1273_dai_ops = {
382 .startup = wl1273_startup,
383 .hw_params = wl1273_hw_params,
386 static struct snd_soc_dai_driver wl1273_dai = {
389 .stream_name = "Playback",
392 .rates = SNDRV_PCM_RATE_8000_48000,
393 .formats = SNDRV_PCM_FMTBIT_S16_LE},
395 .stream_name = "Capture",
398 .rates = SNDRV_PCM_RATE_8000_48000,
399 .formats = SNDRV_PCM_FMTBIT_S16_LE},
400 .ops = &wl1273_dai_ops,
403 /* Audio interface format for the soc_card driver */
404 int wl1273_get_format(struct snd_soc_component *component, unsigned int *fmt)
406 struct wl1273_priv *wl1273;
408 if (component == NULL || fmt == NULL)
411 wl1273 = snd_soc_component_get_drvdata(component);
413 switch (wl1273->mode) {
414 case WL1273_MODE_FM_RX:
415 case WL1273_MODE_FM_TX:
416 *fmt = SND_SOC_DAIFMT_I2S |
417 SND_SOC_DAIFMT_NB_NF |
418 SND_SOC_DAIFMT_CBM_CFM;
422 *fmt = SND_SOC_DAIFMT_DSP_A |
423 SND_SOC_DAIFMT_IB_NF |
424 SND_SOC_DAIFMT_CBM_CFM;
433 EXPORT_SYMBOL_GPL(wl1273_get_format);
435 static int wl1273_probe(struct snd_soc_component *component)
437 struct wl1273_core **core = component->dev->platform_data;
438 struct wl1273_priv *wl1273;
440 dev_dbg(component->dev, "%s.\n", __func__);
443 dev_err(component->dev, "Platform data is missing.\n");
447 wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
451 wl1273->mode = WL1273_MODE_BT;
452 wl1273->core = *core;
454 snd_soc_component_set_drvdata(component, wl1273);
459 static void wl1273_remove(struct snd_soc_component *component)
461 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
463 dev_dbg(component->dev, "%s\n", __func__);
467 static const struct snd_soc_component_driver soc_component_dev_wl1273 = {
468 .probe = wl1273_probe,
469 .remove = wl1273_remove,
470 .controls = wl1273_controls,
471 .num_controls = ARRAY_SIZE(wl1273_controls),
472 .dapm_widgets = wl1273_dapm_widgets,
473 .num_dapm_widgets = ARRAY_SIZE(wl1273_dapm_widgets),
474 .dapm_routes = wl1273_dapm_routes,
475 .num_dapm_routes = ARRAY_SIZE(wl1273_dapm_routes),
477 .use_pmdown_time = 1,
479 .non_legacy_dai_naming = 1,
482 static int wl1273_platform_probe(struct platform_device *pdev)
484 return devm_snd_soc_register_component(&pdev->dev,
485 &soc_component_dev_wl1273,
489 static int wl1273_platform_remove(struct platform_device *pdev)
494 MODULE_ALIAS("platform:wl1273-codec");
496 static struct platform_driver wl1273_platform_driver = {
498 .name = "wl1273-codec",
500 .probe = wl1273_platform_probe,
501 .remove = wl1273_platform_remove,
504 module_platform_driver(wl1273_platform_driver);
506 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
507 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
508 MODULE_LICENSE("GPL");