blob: f3d4e88d0b7b3b1fc3f3224c7e50f97edb580ade [file] [log] [blame]
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +03001/*
2 * ALSA SoC WL1273 codec driver
3 *
4 * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
5 *
Matti Aaltonen40285f82011-03-01 10:10:37 -03006 * Copyright: (C) 2010, 2011 Nokia Corporation
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +03007 *
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.
11 *
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.
16 *
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
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/mfd/wl1273-core.h>
25#include <linux/slab.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040026#include <linux/module.h>
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +030027#include <sound/pcm.h>
28#include <sound/pcm_params.h>
Jarkko Nikula0d911bae2010-11-21 19:48:46 +020029#include <sound/soc.h>
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +030030#include <sound/initval.h>
31
32#include "wl1273.h"
33
34enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
35
36/* codec private data */
37struct wl1273_priv {
38 enum wl1273_mode mode;
39 struct wl1273_core *core;
40 unsigned int channels;
41};
42
43static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
44 int rate, int width)
45{
Matti J. Aaltonen228dd542011-01-13 15:22:45 +020046 struct device *dev = &core->client->dev;
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +030047 int r = 0;
48 u16 mode;
49
50 dev_dbg(dev, "rate: %d\n", rate);
51 dev_dbg(dev, "width: %d\n", width);
52
53 mutex_lock(&core->lock);
54
55 mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
56
57 switch (rate) {
58 case 48000:
59 mode |= WL1273_IS2_RATE_48K;
60 break;
61 case 44100:
62 mode |= WL1273_IS2_RATE_44_1K;
63 break;
64 case 32000:
65 mode |= WL1273_IS2_RATE_32K;
66 break;
67 case 22050:
68 mode |= WL1273_IS2_RATE_22_05K;
69 break;
70 case 16000:
71 mode |= WL1273_IS2_RATE_16K;
72 break;
73 case 12000:
74 mode |= WL1273_IS2_RATE_12K;
75 break;
76 case 11025:
77 mode |= WL1273_IS2_RATE_11_025;
78 break;
79 case 8000:
80 mode |= WL1273_IS2_RATE_8K;
81 break;
82 default:
83 dev_err(dev, "Sampling rate: %d not supported\n", rate);
84 r = -EINVAL;
85 goto out;
86 }
87
88 switch (width) {
89 case 16:
90 mode |= WL1273_IS2_WIDTH_32;
91 break;
92 case 20:
93 mode |= WL1273_IS2_WIDTH_40;
94 break;
95 case 24:
96 mode |= WL1273_IS2_WIDTH_48;
97 break;
98 case 25:
99 mode |= WL1273_IS2_WIDTH_50;
100 break;
101 case 30:
102 mode |= WL1273_IS2_WIDTH_60;
103 break;
104 case 32:
105 mode |= WL1273_IS2_WIDTH_64;
106 break;
107 case 40:
108 mode |= WL1273_IS2_WIDTH_80;
109 break;
110 case 48:
111 mode |= WL1273_IS2_WIDTH_96;
112 break;
113 case 64:
114 mode |= WL1273_IS2_WIDTH_128;
115 break;
116 default:
117 dev_err(dev, "Data width: %d not supported\n", width);
118 r = -EINVAL;
119 goto out;
120 }
121
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);
125
126 if (core->i2s_mode != mode) {
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200127 r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300128 if (r)
129 goto out;
130
131 core->i2s_mode = mode;
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200132 r = core->write(core, WL1273_AUDIO_ENABLE,
133 WL1273_AUDIO_ENABLE_I2S);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300134 if (r)
135 goto out;
136 }
137out:
138 mutex_unlock(&core->lock);
139
140 return r;
141}
142
143static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
144 int channel_number)
145{
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200146 struct device *dev = &core->client->dev;
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300147 int r = 0;
148
149 dev_dbg(dev, "%s\n", __func__);
150
151 mutex_lock(&core->lock);
152
153 if (core->channel_number == channel_number)
154 goto out;
155
156 if (channel_number == 1 && core->mode == WL1273_MODE_RX)
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200157 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300158 else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200159 r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300160 else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200161 r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300162 else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200163 r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300164 else
165 r = -EINVAL;
166out:
167 mutex_unlock(&core->lock);
168
169 return r;
170}
171
172static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
174{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100175 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300176 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
177
178 ucontrol->value.integer.value[0] = wl1273->mode;
179
180 return 0;
181}
182
Matti Aaltonen40285f82011-03-01 10:10:37 -0300183/*
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
186 * space).
187 */
188static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300189
190static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_value *ucontrol)
192{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100193 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300194 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
195
Matti J. Aaltonen2c4ee9b2010-09-10 10:41:30 +0300196 if (wl1273->mode == ucontrol->value.integer.value[0])
197 return 0;
198
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300199 /* Do not allow changes while stream is running */
Lars-Peter Clausen5c898e72014-03-05 13:17:45 +0100200 if (snd_soc_codec_is_active(codec))
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300201 return -EPERM;
202
203 if (ucontrol->value.integer.value[0] < 0 ||
204 ucontrol->value.integer.value[0] >= ARRAY_SIZE(wl1273_audio_route))
205 return -EINVAL;
206
207 wl1273->mode = ucontrol->value.integer.value[0];
208
209 return 1;
210}
211
Takashi Iwai4ec20a92014-02-18 10:33:06 +0100212static SOC_ENUM_SINGLE_EXT_DECL(wl1273_enum, wl1273_audio_route);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300213
214static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
215 struct snd_ctl_elem_value *ucontrol)
216{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100217 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300218 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
219
220 dev_dbg(codec->dev, "%s: enter.\n", __func__);
221
222 ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
223
224 return 0;
225}
226
227static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
228 struct snd_ctl_elem_value *ucontrol)
229{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100230 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300231 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
232 int val, r = 0;
233
234 dev_dbg(codec->dev, "%s: enter.\n", __func__);
235
236 val = ucontrol->value.integer.value[0];
237 if (wl1273->core->audio_mode == val)
238 return 0;
239
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200240 r = wl1273->core->set_audio(wl1273->core, val);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300241 if (r < 0)
242 return r;
243
244 return 1;
245}
246
Matti Aaltonen40285f82011-03-01 10:10:37 -0300247static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300248
Takashi Iwai4ec20a92014-02-18 10:33:06 +0100249static SOC_ENUM_SINGLE_EXT_DECL(wl1273_audio_enum, wl1273_audio_strings);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300250
251static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_value *ucontrol)
253{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100254 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300255 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
256
257 dev_dbg(codec->dev, "%s: enter.\n", __func__);
258
259 ucontrol->value.integer.value[0] = wl1273->core->volume;
260
261 return 0;
262}
263
264static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
265 struct snd_ctl_elem_value *ucontrol)
266{
Lars-Peter Clausenea53bf72014-03-18 09:02:04 +0100267 struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300268 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
269 int r;
270
271 dev_dbg(codec->dev, "%s: enter.\n", __func__);
272
Matti J. Aaltonen228dd542011-01-13 15:22:45 +0200273 r = wl1273->core->set_volume(wl1273->core,
274 ucontrol->value.integer.value[0]);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300275 if (r)
276 return r;
277
278 return 1;
279}
280
281static 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),
288};
289
Mark Browne29deb42013-08-18 18:25:53 +0100290static const struct snd_soc_dapm_widget wl1273_dapm_widgets[] = {
291 SND_SOC_DAPM_INPUT("RX"),
292
293 SND_SOC_DAPM_OUTPUT("TX"),
294};
295
296static const struct snd_soc_dapm_route wl1273_dapm_routes[] = {
297 { "Capture", NULL, "RX" },
298
299 { "TX", NULL, "Playback" },
300};
301
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300302static int wl1273_startup(struct snd_pcm_substream *substream,
303 struct snd_soc_dai *dai)
304{
Mark Browne6968a12012-04-04 15:58:16 +0100305 struct snd_soc_codec *codec = dai->codec;
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300306 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
307
308 switch (wl1273->mode) {
309 case WL1273_MODE_BT:
310 snd_pcm_hw_constraint_minmax(substream->runtime,
311 SNDRV_PCM_HW_PARAM_RATE,
312 8000, 8000);
313 snd_pcm_hw_constraint_minmax(substream->runtime,
314 SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
315 break;
316 case WL1273_MODE_FM_RX:
317 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
318 pr_err("Cannot play in RX mode.\n");
319 return -EINVAL;
320 }
321 break;
322 case WL1273_MODE_FM_TX:
323 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
324 pr_err("Cannot capture in TX mode.\n");
325 return -EINVAL;
326 }
327 break;
328 default:
329 return -EINVAL;
330 break;
331 }
332
333 return 0;
334}
335
336static int wl1273_hw_params(struct snd_pcm_substream *substream,
337 struct snd_pcm_hw_params *params,
338 struct snd_soc_dai *dai)
339{
Mark Browne6968a12012-04-04 15:58:16 +0100340 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(dai->codec);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300341 struct wl1273_core *core = wl1273->core;
342 unsigned int rate, width, r;
343
Mark Brown96301812014-07-31 12:49:36 +0100344 if (params_width(params) != 16) {
345 dev_err(dai->dev, "%d bits/sample not supported\n",
346 params_width(params));
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300347 return -EINVAL;
348 }
349
350 rate = params_rate(params);
351 width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
352
353 if (wl1273->mode == WL1273_MODE_BT) {
354 if (rate != 8000) {
355 pr_err("Rate %d not supported.\n", params_rate(params));
356 return -EINVAL;
357 }
358
359 if (params_channels(params) != 1) {
360 pr_err("Only mono supported.\n");
361 return -EINVAL;
362 }
363
364 return 0;
365 }
366
367 if (wl1273->mode == WL1273_MODE_FM_TX &&
368 substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
369 pr_err("Only playback supported with TX.\n");
370 return -EINVAL;
371 }
372
373 if (wl1273->mode == WL1273_MODE_FM_RX &&
374 substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
375 pr_err("Only capture supported with RX.\n");
376 return -EINVAL;
377 }
378
379 if (wl1273->mode != WL1273_MODE_FM_RX &&
380 wl1273->mode != WL1273_MODE_FM_TX) {
381 pr_err("Unexpected mode: %d.\n", wl1273->mode);
382 return -EINVAL;
383 }
384
385 r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
386 if (r)
387 return r;
388
389 wl1273->channels = params_channels(params);
390 r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
391 if (r)
392 return r;
393
394 return 0;
395}
396
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100397static const struct snd_soc_dai_ops wl1273_dai_ops = {
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300398 .startup = wl1273_startup,
399 .hw_params = wl1273_hw_params,
400};
401
402static struct snd_soc_dai_driver wl1273_dai = {
403 .name = "wl1273-fm",
404 .playback = {
405 .stream_name = "Playback",
406 .channels_min = 1,
407 .channels_max = 2,
408 .rates = SNDRV_PCM_RATE_8000_48000,
409 .formats = SNDRV_PCM_FMTBIT_S16_LE},
410 .capture = {
411 .stream_name = "Capture",
412 .channels_min = 1,
413 .channels_max = 2,
414 .rates = SNDRV_PCM_RATE_8000_48000,
415 .formats = SNDRV_PCM_FMTBIT_S16_LE},
416 .ops = &wl1273_dai_ops,
417};
418
419/* Audio interface format for the soc_card driver */
420int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
421{
422 struct wl1273_priv *wl1273;
423
424 if (codec == NULL || fmt == NULL)
425 return -EINVAL;
426
427 wl1273 = snd_soc_codec_get_drvdata(codec);
428
429 switch (wl1273->mode) {
430 case WL1273_MODE_FM_RX:
431 case WL1273_MODE_FM_TX:
432 *fmt = SND_SOC_DAIFMT_I2S |
433 SND_SOC_DAIFMT_NB_NF |
434 SND_SOC_DAIFMT_CBM_CFM;
435
436 break;
437 case WL1273_MODE_BT:
438 *fmt = SND_SOC_DAIFMT_DSP_A |
439 SND_SOC_DAIFMT_IB_NF |
440 SND_SOC_DAIFMT_CBM_CFM;
441
442 break;
443 default:
444 return -EINVAL;
445 }
446
447 return 0;
448}
449EXPORT_SYMBOL_GPL(wl1273_get_format);
450
451static int wl1273_probe(struct snd_soc_codec *codec)
452{
Samuel Ortiz9e554692011-04-06 11:56:04 +0200453 struct wl1273_core **core = codec->dev->platform_data;
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300454 struct wl1273_priv *wl1273;
455 int r;
456
457 dev_dbg(codec->dev, "%s.\n", __func__);
458
459 if (!core) {
460 dev_err(codec->dev, "Platform data is missing.\n");
461 return -EINVAL;
462 }
463
464 wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
Sachin Kamatac872d32014-06-20 15:29:04 +0530465 if (!wl1273)
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300466 return -ENOMEM;
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300467
468 wl1273->mode = WL1273_MODE_BT;
469 wl1273->core = *core;
470
471 snd_soc_codec_set_drvdata(codec, wl1273);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300472
Liam Girdwood022658b2012-02-03 17:43:09 +0000473 r = snd_soc_add_codec_controls(codec, wl1273_controls,
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300474 ARRAY_SIZE(wl1273_controls));
475 if (r)
476 kfree(wl1273);
477
478 return r;
479}
480
481static int wl1273_remove(struct snd_soc_codec *codec)
482{
483 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
484
485 dev_dbg(codec->dev, "%s\n", __func__);
486 kfree(wl1273);
487
488 return 0;
489}
490
491static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
492 .probe = wl1273_probe,
493 .remove = wl1273_remove,
Mark Browne29deb42013-08-18 18:25:53 +0100494
495 .dapm_widgets = wl1273_dapm_widgets,
496 .num_dapm_widgets = ARRAY_SIZE(wl1273_dapm_widgets),
497 .dapm_routes = wl1273_dapm_routes,
498 .num_dapm_routes = ARRAY_SIZE(wl1273_dapm_routes),
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300499};
500
Bill Pemberton7a79e942012-12-07 09:26:37 -0500501static int wl1273_platform_probe(struct platform_device *pdev)
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300502{
503 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
504 &wl1273_dai, 1);
505}
506
Bill Pemberton7a79e942012-12-07 09:26:37 -0500507static int wl1273_platform_remove(struct platform_device *pdev)
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300508{
509 snd_soc_unregister_codec(&pdev->dev);
510 return 0;
511}
512
513MODULE_ALIAS("platform:wl1273-codec");
514
515static struct platform_driver wl1273_platform_driver = {
516 .driver = {
517 .name = "wl1273-codec",
518 .owner = THIS_MODULE,
519 },
520 .probe = wl1273_platform_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500521 .remove = wl1273_platform_remove,
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300522};
523
Mark Brown5bbcc3c2011-11-23 22:52:08 +0000524module_platform_driver(wl1273_platform_driver);
Matti J. Aaltonen3fabe082010-08-20 12:32:46 +0300525
526MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
527MODULE_DESCRIPTION("ASoC WL1273 codec driver");
528MODULE_LICENSE("GPL");