0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/mfd/wl1273-core.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <sound/pcm.h>
0014 #include <sound/pcm_params.h>
0015 #include <sound/soc.h>
0016 #include <sound/initval.h>
0017
0018 #include "wl1273.h"
0019
0020 enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
0021
0022
0023 struct wl1273_priv {
0024 enum wl1273_mode mode;
0025 struct wl1273_core *core;
0026 unsigned int channels;
0027 };
0028
0029 static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
0030 int rate, int width)
0031 {
0032 struct device *dev = &core->client->dev;
0033 int r = 0;
0034 u16 mode;
0035
0036 dev_dbg(dev, "rate: %d\n", rate);
0037 dev_dbg(dev, "width: %d\n", width);
0038
0039 mutex_lock(&core->lock);
0040
0041 mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
0042
0043 switch (rate) {
0044 case 48000:
0045 mode |= WL1273_IS2_RATE_48K;
0046 break;
0047 case 44100:
0048 mode |= WL1273_IS2_RATE_44_1K;
0049 break;
0050 case 32000:
0051 mode |= WL1273_IS2_RATE_32K;
0052 break;
0053 case 22050:
0054 mode |= WL1273_IS2_RATE_22_05K;
0055 break;
0056 case 16000:
0057 mode |= WL1273_IS2_RATE_16K;
0058 break;
0059 case 12000:
0060 mode |= WL1273_IS2_RATE_12K;
0061 break;
0062 case 11025:
0063 mode |= WL1273_IS2_RATE_11_025;
0064 break;
0065 case 8000:
0066 mode |= WL1273_IS2_RATE_8K;
0067 break;
0068 default:
0069 dev_err(dev, "Sampling rate: %d not supported\n", rate);
0070 r = -EINVAL;
0071 goto out;
0072 }
0073
0074 switch (width) {
0075 case 16:
0076 mode |= WL1273_IS2_WIDTH_32;
0077 break;
0078 case 20:
0079 mode |= WL1273_IS2_WIDTH_40;
0080 break;
0081 case 24:
0082 mode |= WL1273_IS2_WIDTH_48;
0083 break;
0084 case 25:
0085 mode |= WL1273_IS2_WIDTH_50;
0086 break;
0087 case 30:
0088 mode |= WL1273_IS2_WIDTH_60;
0089 break;
0090 case 32:
0091 mode |= WL1273_IS2_WIDTH_64;
0092 break;
0093 case 40:
0094 mode |= WL1273_IS2_WIDTH_80;
0095 break;
0096 case 48:
0097 mode |= WL1273_IS2_WIDTH_96;
0098 break;
0099 case 64:
0100 mode |= WL1273_IS2_WIDTH_128;
0101 break;
0102 default:
0103 dev_err(dev, "Data width: %d not supported\n", width);
0104 r = -EINVAL;
0105 goto out;
0106 }
0107
0108 dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n", WL1273_I2S_DEF_MODE);
0109 dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
0110 dev_dbg(dev, "mode: 0x%04x\n", mode);
0111
0112 if (core->i2s_mode != mode) {
0113 r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
0114 if (r)
0115 goto out;
0116
0117 core->i2s_mode = mode;
0118 r = core->write(core, WL1273_AUDIO_ENABLE,
0119 WL1273_AUDIO_ENABLE_I2S);
0120 if (r)
0121 goto out;
0122 }
0123 out:
0124 mutex_unlock(&core->lock);
0125
0126 return r;
0127 }
0128
0129 static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
0130 int channel_number)
0131 {
0132 struct device *dev = &core->client->dev;
0133 int r = 0;
0134
0135 dev_dbg(dev, "%s\n", __func__);
0136
0137 mutex_lock(&core->lock);
0138
0139 if (core->channel_number == channel_number)
0140 goto out;
0141
0142 if (channel_number == 1 && core->mode == WL1273_MODE_RX)
0143 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
0144 else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
0145 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
0146 else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
0147 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
0148 else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
0149 r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
0150 else
0151 r = -EINVAL;
0152 out:
0153 mutex_unlock(&core->lock);
0154
0155 return r;
0156 }
0157
0158 static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
0159 struct snd_ctl_elem_value *ucontrol)
0160 {
0161 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0162 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0163
0164 ucontrol->value.enumerated.item[0] = wl1273->mode;
0165
0166 return 0;
0167 }
0168
0169
0170
0171
0172
0173
0174 static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
0175
0176 static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
0177 struct snd_ctl_elem_value *ucontrol)
0178 {
0179 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0180 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0181
0182 if (wl1273->mode == ucontrol->value.enumerated.item[0])
0183 return 0;
0184
0185
0186 if (snd_soc_component_active(component))
0187 return -EPERM;
0188
0189 if (ucontrol->value.enumerated.item[0] >= ARRAY_SIZE(wl1273_audio_route))
0190 return -EINVAL;
0191
0192 wl1273->mode = ucontrol->value.enumerated.item[0];
0193
0194 return 1;
0195 }
0196
0197 static SOC_ENUM_SINGLE_EXT_DECL(wl1273_enum, wl1273_audio_route);
0198
0199 static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
0200 struct snd_ctl_elem_value *ucontrol)
0201 {
0202 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0203 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0204
0205 dev_dbg(component->dev, "%s: enter.\n", __func__);
0206
0207 ucontrol->value.enumerated.item[0] = wl1273->core->audio_mode;
0208
0209 return 0;
0210 }
0211
0212 static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
0213 struct snd_ctl_elem_value *ucontrol)
0214 {
0215 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0216 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0217 int val, r = 0;
0218
0219 dev_dbg(component->dev, "%s: enter.\n", __func__);
0220
0221 val = ucontrol->value.enumerated.item[0];
0222 if (wl1273->core->audio_mode == val)
0223 return 0;
0224
0225 r = wl1273->core->set_audio(wl1273->core, val);
0226 if (r < 0)
0227 return r;
0228
0229 return 1;
0230 }
0231
0232 static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
0233
0234 static SOC_ENUM_SINGLE_EXT_DECL(wl1273_audio_enum, wl1273_audio_strings);
0235
0236 static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
0237 struct snd_ctl_elem_value *ucontrol)
0238 {
0239 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0240 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0241
0242 dev_dbg(component->dev, "%s: enter.\n", __func__);
0243
0244 ucontrol->value.integer.value[0] = wl1273->core->volume;
0245
0246 return 0;
0247 }
0248
0249 static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
0250 struct snd_ctl_elem_value *ucontrol)
0251 {
0252 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0253 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0254 int r;
0255
0256 dev_dbg(component->dev, "%s: enter.\n", __func__);
0257
0258 r = wl1273->core->set_volume(wl1273->core,
0259 ucontrol->value.integer.value[0]);
0260 if (r)
0261 return r;
0262
0263 return 1;
0264 }
0265
0266 static const struct snd_kcontrol_new wl1273_controls[] = {
0267 SOC_ENUM_EXT("Codec Mode", wl1273_enum,
0268 snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
0269 SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
0270 snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put),
0271 SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
0272 snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
0273 };
0274
0275 static const struct snd_soc_dapm_widget wl1273_dapm_widgets[] = {
0276 SND_SOC_DAPM_INPUT("RX"),
0277
0278 SND_SOC_DAPM_OUTPUT("TX"),
0279 };
0280
0281 static const struct snd_soc_dapm_route wl1273_dapm_routes[] = {
0282 { "Capture", NULL, "RX" },
0283
0284 { "TX", NULL, "Playback" },
0285 };
0286
0287 static int wl1273_startup(struct snd_pcm_substream *substream,
0288 struct snd_soc_dai *dai)
0289 {
0290 struct snd_soc_component *component = dai->component;
0291 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0292
0293 switch (wl1273->mode) {
0294 case WL1273_MODE_BT:
0295 snd_pcm_hw_constraint_single(substream->runtime,
0296 SNDRV_PCM_HW_PARAM_RATE, 8000);
0297 snd_pcm_hw_constraint_single(substream->runtime,
0298 SNDRV_PCM_HW_PARAM_CHANNELS, 1);
0299 break;
0300 case WL1273_MODE_FM_RX:
0301 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
0302 pr_err("Cannot play in RX mode.\n");
0303 return -EINVAL;
0304 }
0305 break;
0306 case WL1273_MODE_FM_TX:
0307 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
0308 pr_err("Cannot capture in TX mode.\n");
0309 return -EINVAL;
0310 }
0311 break;
0312 default:
0313 return -EINVAL;
0314 }
0315
0316 return 0;
0317 }
0318
0319 static int wl1273_hw_params(struct snd_pcm_substream *substream,
0320 struct snd_pcm_hw_params *params,
0321 struct snd_soc_dai *dai)
0322 {
0323 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(dai->component);
0324 struct wl1273_core *core = wl1273->core;
0325 unsigned int rate, width, r;
0326
0327 if (params_width(params) != 16) {
0328 dev_err(dai->dev, "%d bits/sample not supported\n",
0329 params_width(params));
0330 return -EINVAL;
0331 }
0332
0333 rate = params_rate(params);
0334 width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
0335
0336 if (wl1273->mode == WL1273_MODE_BT) {
0337 if (rate != 8000) {
0338 pr_err("Rate %d not supported.\n", params_rate(params));
0339 return -EINVAL;
0340 }
0341
0342 if (params_channels(params) != 1) {
0343 pr_err("Only mono supported.\n");
0344 return -EINVAL;
0345 }
0346
0347 return 0;
0348 }
0349
0350 if (wl1273->mode == WL1273_MODE_FM_TX &&
0351 substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
0352 pr_err("Only playback supported with TX.\n");
0353 return -EINVAL;
0354 }
0355
0356 if (wl1273->mode == WL1273_MODE_FM_RX &&
0357 substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
0358 pr_err("Only capture supported with RX.\n");
0359 return -EINVAL;
0360 }
0361
0362 if (wl1273->mode != WL1273_MODE_FM_RX &&
0363 wl1273->mode != WL1273_MODE_FM_TX) {
0364 pr_err("Unexpected mode: %d.\n", wl1273->mode);
0365 return -EINVAL;
0366 }
0367
0368 r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
0369 if (r)
0370 return r;
0371
0372 wl1273->channels = params_channels(params);
0373 r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
0374 if (r)
0375 return r;
0376
0377 return 0;
0378 }
0379
0380 static const struct snd_soc_dai_ops wl1273_dai_ops = {
0381 .startup = wl1273_startup,
0382 .hw_params = wl1273_hw_params,
0383 };
0384
0385 static struct snd_soc_dai_driver wl1273_dai = {
0386 .name = "wl1273-fm",
0387 .playback = {
0388 .stream_name = "Playback",
0389 .channels_min = 1,
0390 .channels_max = 2,
0391 .rates = SNDRV_PCM_RATE_8000_48000,
0392 .formats = SNDRV_PCM_FMTBIT_S16_LE},
0393 .capture = {
0394 .stream_name = "Capture",
0395 .channels_min = 1,
0396 .channels_max = 2,
0397 .rates = SNDRV_PCM_RATE_8000_48000,
0398 .formats = SNDRV_PCM_FMTBIT_S16_LE},
0399 .ops = &wl1273_dai_ops,
0400 };
0401
0402
0403 int wl1273_get_format(struct snd_soc_component *component, unsigned int *fmt)
0404 {
0405 struct wl1273_priv *wl1273;
0406
0407 if (component == NULL || fmt == NULL)
0408 return -EINVAL;
0409
0410 wl1273 = snd_soc_component_get_drvdata(component);
0411
0412 switch (wl1273->mode) {
0413 case WL1273_MODE_FM_RX:
0414 case WL1273_MODE_FM_TX:
0415 *fmt = SND_SOC_DAIFMT_I2S |
0416 SND_SOC_DAIFMT_NB_NF |
0417 SND_SOC_DAIFMT_CBP_CFP;
0418
0419 break;
0420 case WL1273_MODE_BT:
0421 *fmt = SND_SOC_DAIFMT_DSP_A |
0422 SND_SOC_DAIFMT_IB_NF |
0423 SND_SOC_DAIFMT_CBP_CFP;
0424
0425 break;
0426 default:
0427 return -EINVAL;
0428 }
0429
0430 return 0;
0431 }
0432 EXPORT_SYMBOL_GPL(wl1273_get_format);
0433
0434 static int wl1273_probe(struct snd_soc_component *component)
0435 {
0436 struct wl1273_core **core = component->dev->platform_data;
0437 struct wl1273_priv *wl1273;
0438
0439 dev_dbg(component->dev, "%s.\n", __func__);
0440
0441 if (!core) {
0442 dev_err(component->dev, "Platform data is missing.\n");
0443 return -EINVAL;
0444 }
0445
0446 wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
0447 if (!wl1273)
0448 return -ENOMEM;
0449
0450 wl1273->mode = WL1273_MODE_BT;
0451 wl1273->core = *core;
0452
0453 snd_soc_component_set_drvdata(component, wl1273);
0454
0455 return 0;
0456 }
0457
0458 static void wl1273_remove(struct snd_soc_component *component)
0459 {
0460 struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component);
0461
0462 dev_dbg(component->dev, "%s\n", __func__);
0463 kfree(wl1273);
0464 }
0465
0466 static const struct snd_soc_component_driver soc_component_dev_wl1273 = {
0467 .probe = wl1273_probe,
0468 .remove = wl1273_remove,
0469 .controls = wl1273_controls,
0470 .num_controls = ARRAY_SIZE(wl1273_controls),
0471 .dapm_widgets = wl1273_dapm_widgets,
0472 .num_dapm_widgets = ARRAY_SIZE(wl1273_dapm_widgets),
0473 .dapm_routes = wl1273_dapm_routes,
0474 .num_dapm_routes = ARRAY_SIZE(wl1273_dapm_routes),
0475 .idle_bias_on = 1,
0476 .use_pmdown_time = 1,
0477 .endianness = 1,
0478 };
0479
0480 static int wl1273_platform_probe(struct platform_device *pdev)
0481 {
0482 return devm_snd_soc_register_component(&pdev->dev,
0483 &soc_component_dev_wl1273,
0484 &wl1273_dai, 1);
0485 }
0486
0487 static int wl1273_platform_remove(struct platform_device *pdev)
0488 {
0489 return 0;
0490 }
0491
0492 MODULE_ALIAS("platform:wl1273-codec");
0493
0494 static struct platform_driver wl1273_platform_driver = {
0495 .driver = {
0496 .name = "wl1273-codec",
0497 },
0498 .probe = wl1273_platform_probe,
0499 .remove = wl1273_platform_remove,
0500 };
0501
0502 module_platform_driver(wl1273_platform_driver);
0503
0504 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
0505 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
0506 MODULE_LICENSE("GPL");