1 /*
2 * ALSA SoC WL1273 codec driver
3 *
4 * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com>
5 *
6 * Copyright: (C) 2010, 2011 Nokia Corporation
7 *
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>
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>
31
32 #include "wl1273.h"
33
34 enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
35
36 /* codec private data */
37 struct wl1273_priv {
38 enum wl1273_mode mode;
39 struct wl1273_core *core;
40 unsigned int channels;
41 };
42
snd_wl1273_fm_set_i2s_mode(struct wl1273_core * core,int rate,int width)43 static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
44 int rate, int width)
45 {
46 struct device *dev = &core->client->dev;
47 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) {
127 r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
128 if (r)
129 goto out;
130
131 core->i2s_mode = mode;
132 r = core->write(core, WL1273_AUDIO_ENABLE,
133 WL1273_AUDIO_ENABLE_I2S);
134 if (r)
135 goto out;
136 }
137 out:
138 mutex_unlock(&core->lock);
139
140 return r;
141 }
142
snd_wl1273_fm_set_channel_number(struct wl1273_core * core,int channel_number)143 static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
144 int channel_number)
145 {
146 struct device *dev = &core->client->dev;
147 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)
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);
164 else
165 r = -EINVAL;
166 out:
167 mutex_unlock(&core->lock);
168
169 return r;
170 }
171
snd_wl1273_get_audio_route(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)172 static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
173 struct snd_ctl_elem_value *ucontrol)
174 {
175 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
176 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
183 /*
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 */
188 static const char * const wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
189
snd_wl1273_set_audio_route(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)190 static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_value *ucontrol)
192 {
193 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
194 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
195
196 if (wl1273->mode == ucontrol->value.integer.value[0])
197 return 0;
198
199 /* Do not allow changes while stream is running */
200 if (codec->active)
201 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
212 static const struct soc_enum wl1273_enum =
213 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route), wl1273_audio_route);
214
snd_wl1273_fm_audio_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)215 static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_value *ucontrol)
217 {
218 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
219 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
220
221 dev_dbg(codec->dev, "%s: enter.\n", __func__);
222
223 ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
224
225 return 0;
226 }
227
snd_wl1273_fm_audio_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)228 static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
230 {
231 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
232 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
233 int val, r = 0;
234
235 dev_dbg(codec->dev, "%s: enter.\n", __func__);
236
237 val = ucontrol->value.integer.value[0];
238 if (wl1273->core->audio_mode == val)
239 return 0;
240
241 r = wl1273->core->set_audio(wl1273->core, val);
242 if (r < 0)
243 return r;
244
245 return 1;
246 }
247
248 static const char * const wl1273_audio_strings[] = { "Digital", "Analog" };
249
250 static const struct soc_enum wl1273_audio_enum =
251 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings),
252 wl1273_audio_strings);
253
snd_wl1273_fm_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)254 static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
255 struct snd_ctl_elem_value *ucontrol)
256 {
257 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
258 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
259
260 dev_dbg(codec->dev, "%s: enter.\n", __func__);
261
262 ucontrol->value.integer.value[0] = wl1273->core->volume;
263
264 return 0;
265 }
266
snd_wl1273_fm_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)267 static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
268 struct snd_ctl_elem_value *ucontrol)
269 {
270 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
271 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
272 int r;
273
274 dev_dbg(codec->dev, "%s: enter.\n", __func__);
275
276 r = wl1273->core->set_volume(wl1273->core,
277 ucontrol->value.integer.value[0]);
278 if (r)
279 return r;
280
281 return 1;
282 }
283
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),
291 };
292
wl1273_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)293 static int wl1273_startup(struct snd_pcm_substream *substream,
294 struct snd_soc_dai *dai)
295 {
296 struct snd_soc_pcm_runtime *rtd = substream->private_data;
297 struct snd_soc_codec *codec = rtd->codec;
298 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
299
300 switch (wl1273->mode) {
301 case WL1273_MODE_BT:
302 snd_pcm_hw_constraint_minmax(substream->runtime,
303 SNDRV_PCM_HW_PARAM_RATE,
304 8000, 8000);
305 snd_pcm_hw_constraint_minmax(substream->runtime,
306 SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
307 break;
308 case WL1273_MODE_FM_RX:
309 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
310 pr_err("Cannot play in RX mode.\n");
311 return -EINVAL;
312 }
313 break;
314 case WL1273_MODE_FM_TX:
315 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
316 pr_err("Cannot capture in TX mode.\n");
317 return -EINVAL;
318 }
319 break;
320 default:
321 return -EINVAL;
322 break;
323 }
324
325 return 0;
326 }
327
wl1273_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)328 static int wl1273_hw_params(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *params,
330 struct snd_soc_dai *dai)
331 {
332 struct snd_soc_pcm_runtime *rtd = substream->private_data;
333 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(rtd->codec);
334 struct wl1273_core *core = wl1273->core;
335 unsigned int rate, width, r;
336
337 if (params_format(params) != SNDRV_PCM_FORMAT_S16_LE) {
338 pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
339 return -EINVAL;
340 }
341
342 rate = params_rate(params);
343 width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
344
345 if (wl1273->mode == WL1273_MODE_BT) {
346 if (rate != 8000) {
347 pr_err("Rate %d not supported.\n", params_rate(params));
348 return -EINVAL;
349 }
350
351 if (params_channels(params) != 1) {
352 pr_err("Only mono supported.\n");
353 return -EINVAL;
354 }
355
356 return 0;
357 }
358
359 if (wl1273->mode == WL1273_MODE_FM_TX &&
360 substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
361 pr_err("Only playback supported with TX.\n");
362 return -EINVAL;
363 }
364
365 if (wl1273->mode == WL1273_MODE_FM_RX &&
366 substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
367 pr_err("Only capture supported with RX.\n");
368 return -EINVAL;
369 }
370
371 if (wl1273->mode != WL1273_MODE_FM_RX &&
372 wl1273->mode != WL1273_MODE_FM_TX) {
373 pr_err("Unexpected mode: %d.\n", wl1273->mode);
374 return -EINVAL;
375 }
376
377 r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
378 if (r)
379 return r;
380
381 wl1273->channels = params_channels(params);
382 r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
383 if (r)
384 return r;
385
386 return 0;
387 }
388
389 static const struct snd_soc_dai_ops wl1273_dai_ops = {
390 .startup = wl1273_startup,
391 .hw_params = wl1273_hw_params,
392 };
393
394 static struct snd_soc_dai_driver wl1273_dai = {
395 .name = "wl1273-fm",
396 .playback = {
397 .stream_name = "Playback",
398 .channels_min = 1,
399 .channels_max = 2,
400 .rates = SNDRV_PCM_RATE_8000_48000,
401 .formats = SNDRV_PCM_FMTBIT_S16_LE},
402 .capture = {
403 .stream_name = "Capture",
404 .channels_min = 1,
405 .channels_max = 2,
406 .rates = SNDRV_PCM_RATE_8000_48000,
407 .formats = SNDRV_PCM_FMTBIT_S16_LE},
408 .ops = &wl1273_dai_ops,
409 };
410
411 /* Audio interface format for the soc_card driver */
wl1273_get_format(struct snd_soc_codec * codec,unsigned int * fmt)412 int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
413 {
414 struct wl1273_priv *wl1273;
415
416 if (codec == NULL || fmt == NULL)
417 return -EINVAL;
418
419 wl1273 = snd_soc_codec_get_drvdata(codec);
420
421 switch (wl1273->mode) {
422 case WL1273_MODE_FM_RX:
423 case WL1273_MODE_FM_TX:
424 *fmt = SND_SOC_DAIFMT_I2S |
425 SND_SOC_DAIFMT_NB_NF |
426 SND_SOC_DAIFMT_CBM_CFM;
427
428 break;
429 case WL1273_MODE_BT:
430 *fmt = SND_SOC_DAIFMT_DSP_A |
431 SND_SOC_DAIFMT_IB_NF |
432 SND_SOC_DAIFMT_CBM_CFM;
433
434 break;
435 default:
436 return -EINVAL;
437 }
438
439 return 0;
440 }
441 EXPORT_SYMBOL_GPL(wl1273_get_format);
442
wl1273_probe(struct snd_soc_codec * codec)443 static int wl1273_probe(struct snd_soc_codec *codec)
444 {
445 struct wl1273_core **core = codec->dev->platform_data;
446 struct wl1273_priv *wl1273;
447 int r;
448
449 dev_dbg(codec->dev, "%s.\n", __func__);
450
451 if (!core) {
452 dev_err(codec->dev, "Platform data is missing.\n");
453 return -EINVAL;
454 }
455
456 wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
457 if (wl1273 == NULL) {
458 dev_err(codec->dev, "Cannot allocate memory.\n");
459 return -ENOMEM;
460 }
461
462 wl1273->mode = WL1273_MODE_BT;
463 wl1273->core = *core;
464
465 snd_soc_codec_set_drvdata(codec, wl1273);
466
467 r = snd_soc_add_codec_controls(codec, wl1273_controls,
468 ARRAY_SIZE(wl1273_controls));
469 if (r)
470 kfree(wl1273);
471
472 return r;
473 }
474
wl1273_remove(struct snd_soc_codec * codec)475 static int wl1273_remove(struct snd_soc_codec *codec)
476 {
477 struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
478
479 dev_dbg(codec->dev, "%s\n", __func__);
480 kfree(wl1273);
481
482 return 0;
483 }
484
485 static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
486 .probe = wl1273_probe,
487 .remove = wl1273_remove,
488 };
489
wl1273_platform_probe(struct platform_device * pdev)490 static int __devinit wl1273_platform_probe(struct platform_device *pdev)
491 {
492 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
493 &wl1273_dai, 1);
494 }
495
wl1273_platform_remove(struct platform_device * pdev)496 static int __devexit wl1273_platform_remove(struct platform_device *pdev)
497 {
498 snd_soc_unregister_codec(&pdev->dev);
499 return 0;
500 }
501
502 MODULE_ALIAS("platform:wl1273-codec");
503
504 static struct platform_driver wl1273_platform_driver = {
505 .driver = {
506 .name = "wl1273-codec",
507 .owner = THIS_MODULE,
508 },
509 .probe = wl1273_platform_probe,
510 .remove = __devexit_p(wl1273_platform_remove),
511 };
512
513 module_platform_driver(wl1273_platform_driver);
514
515 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
516 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
517 MODULE_LICENSE("GPL");
518