xref: /openbmc/linux/sound/soc/codecs/wl1273.c (revision df2634f43f5106947f3735a0b61a6527a4b278cd)
1 /*
2  * ALSA SoC WL1273 codec driver
3  *
4  * Author:      Matti Aaltonen, <matti.j.aaltonen@nokia.com>
5  *
6  * Copyright:   (C) 2010 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 <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30 
31 #include "wl1273.h"
32 
33 enum wl1273_mode { WL1273_MODE_BT, WL1273_MODE_FM_RX, WL1273_MODE_FM_TX };
34 
35 /* codec private data */
36 struct wl1273_priv {
37 	enum wl1273_mode mode;
38 	struct wl1273_core *core;
39 	unsigned int channels;
40 };
41 
42 static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core,
43 				      int rate, int width)
44 {
45 	struct device *dev = &core->client->dev;
46 	int r = 0;
47 	u16 mode;
48 
49 	dev_dbg(dev, "rate: %d\n", rate);
50 	dev_dbg(dev, "width: %d\n", width);
51 
52 	mutex_lock(&core->lock);
53 
54 	mode = core->i2s_mode & ~WL1273_IS2_WIDTH & ~WL1273_IS2_RATE;
55 
56 	switch (rate) {
57 	case 48000:
58 		mode |= WL1273_IS2_RATE_48K;
59 		break;
60 	case 44100:
61 		mode |= WL1273_IS2_RATE_44_1K;
62 		break;
63 	case 32000:
64 		mode |= WL1273_IS2_RATE_32K;
65 		break;
66 	case 22050:
67 		mode |= WL1273_IS2_RATE_22_05K;
68 		break;
69 	case 16000:
70 		mode |= WL1273_IS2_RATE_16K;
71 		break;
72 	case 12000:
73 		mode |= WL1273_IS2_RATE_12K;
74 		break;
75 	case 11025:
76 		mode |= WL1273_IS2_RATE_11_025;
77 		break;
78 	case 8000:
79 		mode |= WL1273_IS2_RATE_8K;
80 		break;
81 	default:
82 		dev_err(dev, "Sampling rate: %d not supported\n", rate);
83 		r = -EINVAL;
84 		goto out;
85 	}
86 
87 	switch (width) {
88 	case 16:
89 		mode |= WL1273_IS2_WIDTH_32;
90 		break;
91 	case 20:
92 		mode |= WL1273_IS2_WIDTH_40;
93 		break;
94 	case 24:
95 		mode |= WL1273_IS2_WIDTH_48;
96 		break;
97 	case 25:
98 		mode |= WL1273_IS2_WIDTH_50;
99 		break;
100 	case 30:
101 		mode |= WL1273_IS2_WIDTH_60;
102 		break;
103 	case 32:
104 		mode |= WL1273_IS2_WIDTH_64;
105 		break;
106 	case 40:
107 		mode |= WL1273_IS2_WIDTH_80;
108 		break;
109 	case 48:
110 		mode |= WL1273_IS2_WIDTH_96;
111 		break;
112 	case 64:
113 		mode |= WL1273_IS2_WIDTH_128;
114 		break;
115 	default:
116 		dev_err(dev, "Data width: %d not supported\n", width);
117 		r = -EINVAL;
118 		goto out;
119 	}
120 
121 	dev_dbg(dev, "WL1273_I2S_DEF_MODE: 0x%04x\n",  WL1273_I2S_DEF_MODE);
122 	dev_dbg(dev, "core->i2s_mode: 0x%04x\n", core->i2s_mode);
123 	dev_dbg(dev, "mode: 0x%04x\n", mode);
124 
125 	if (core->i2s_mode != mode) {
126 		r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode);
127 		if (r)
128 			goto out;
129 
130 		core->i2s_mode = mode;
131 		r = core->write(core, WL1273_AUDIO_ENABLE,
132 				WL1273_AUDIO_ENABLE_I2S);
133 		if (r)
134 			goto out;
135 	}
136 out:
137 	mutex_unlock(&core->lock);
138 
139 	return r;
140 }
141 
142 static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core,
143 					    int channel_number)
144 {
145 	struct device *dev = &core->client->dev;
146 	int r = 0;
147 
148 	dev_dbg(dev, "%s\n", __func__);
149 
150 	mutex_lock(&core->lock);
151 
152 	if (core->channel_number == channel_number)
153 		goto out;
154 
155 	if (channel_number == 1 && core->mode == WL1273_MODE_RX)
156 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO);
157 	else if (channel_number == 1 && core->mode == WL1273_MODE_TX)
158 		r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO);
159 	else if (channel_number == 2 && core->mode == WL1273_MODE_RX)
160 		r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO);
161 	else if (channel_number == 2 && core->mode == WL1273_MODE_TX)
162 		r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO);
163 	else
164 		r = -EINVAL;
165 out:
166 	mutex_unlock(&core->lock);
167 
168 	return r;
169 }
170 
171 static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol,
172 				      struct snd_ctl_elem_value *ucontrol)
173 {
174 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
175 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
176 
177 	ucontrol->value.integer.value[0] = wl1273->mode;
178 
179 	return 0;
180 }
181 
182 static const char *wl1273_audio_route[] = { "Bt", "FmRx", "FmTx" };
183 
184 static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol,
185 				      struct snd_ctl_elem_value *ucontrol)
186 {
187 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
188 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
189 
190 	if (wl1273->mode == ucontrol->value.integer.value[0])
191 		return 0;
192 
193 	/* Do not allow changes while stream is running */
194 	if (codec->active)
195 		return -EPERM;
196 
197 	if (ucontrol->value.integer.value[0] < 0 ||
198 	    ucontrol->value.integer.value[0] >=  ARRAY_SIZE(wl1273_audio_route))
199 		return -EINVAL;
200 
201 	wl1273->mode = ucontrol->value.integer.value[0];
202 
203 	return 1;
204 }
205 
206 static const struct soc_enum wl1273_enum =
207 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_route), wl1273_audio_route);
208 
209 static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol,
210 				   struct snd_ctl_elem_value *ucontrol)
211 {
212 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
213 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
214 
215 	dev_dbg(codec->dev, "%s: enter.\n", __func__);
216 
217 	ucontrol->value.integer.value[0] = wl1273->core->audio_mode;
218 
219 	return 0;
220 }
221 
222 static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol,
223 				   struct snd_ctl_elem_value *ucontrol)
224 {
225 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
226 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
227 	int val, r = 0;
228 
229 	dev_dbg(codec->dev, "%s: enter.\n", __func__);
230 
231 	val = ucontrol->value.integer.value[0];
232 	if (wl1273->core->audio_mode == val)
233 		return 0;
234 
235 	r = wl1273->core->set_audio(wl1273->core, val);
236 	if (r < 0)
237 		return r;
238 
239 	return 1;
240 }
241 
242 static const char *wl1273_audio_strings[] = { "Digital", "Analog" };
243 
244 static const struct soc_enum wl1273_audio_enum =
245 	SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(wl1273_audio_strings),
246 			    wl1273_audio_strings);
247 
248 static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol,
249 				    struct snd_ctl_elem_value *ucontrol)
250 {
251 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
252 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
253 
254 	dev_dbg(codec->dev, "%s: enter.\n", __func__);
255 
256 	ucontrol->value.integer.value[0] = wl1273->core->volume;
257 
258 	return 0;
259 }
260 
261 static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol,
262 				    struct snd_ctl_elem_value *ucontrol)
263 {
264 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
265 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
266 	int r;
267 
268 	dev_dbg(codec->dev, "%s: enter.\n", __func__);
269 
270 	r = wl1273->core->set_volume(wl1273->core,
271 				     ucontrol->value.integer.value[0]);
272 	if (r)
273 		return r;
274 
275 	return 1;
276 }
277 
278 static const struct snd_kcontrol_new wl1273_controls[] = {
279 	SOC_ENUM_EXT("Codec Mode", wl1273_enum,
280 		     snd_wl1273_get_audio_route, snd_wl1273_set_audio_route),
281 	SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum,
282 		     snd_wl1273_fm_audio_get,  snd_wl1273_fm_audio_put),
283 	SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0,
284 		       snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put),
285 };
286 
287 static int wl1273_startup(struct snd_pcm_substream *substream,
288 			  struct snd_soc_dai *dai)
289 {
290 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
291 	struct snd_soc_codec *codec = rtd->codec;
292 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
293 
294 	switch (wl1273->mode) {
295 	case WL1273_MODE_BT:
296 		snd_pcm_hw_constraint_minmax(substream->runtime,
297 					     SNDRV_PCM_HW_PARAM_RATE,
298 					     8000, 8000);
299 		snd_pcm_hw_constraint_minmax(substream->runtime,
300 					     SNDRV_PCM_HW_PARAM_CHANNELS, 1, 1);
301 		break;
302 	case WL1273_MODE_FM_RX:
303 		if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
304 			pr_err("Cannot play in RX mode.\n");
305 			return -EINVAL;
306 		}
307 		break;
308 	case WL1273_MODE_FM_TX:
309 		if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
310 			pr_err("Cannot capture in TX mode.\n");
311 			return -EINVAL;
312 		}
313 		break;
314 	default:
315 		return -EINVAL;
316 		break;
317 	}
318 
319 	return 0;
320 }
321 
322 static int wl1273_hw_params(struct snd_pcm_substream *substream,
323 			    struct snd_pcm_hw_params *params,
324 			    struct snd_soc_dai *dai)
325 {
326 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
327 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(rtd->codec);
328 	struct wl1273_core *core = wl1273->core;
329 	unsigned int rate, width, r;
330 
331 	if (params_format(params) != SNDRV_PCM_FORMAT_S16_LE) {
332 		pr_err("Only SNDRV_PCM_FORMAT_S16_LE supported.\n");
333 		return -EINVAL;
334 	}
335 
336 	rate = params_rate(params);
337 	width =  hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
338 
339 	if (wl1273->mode == WL1273_MODE_BT) {
340 		if (rate != 8000) {
341 			pr_err("Rate %d not supported.\n", params_rate(params));
342 			return -EINVAL;
343 		}
344 
345 		if (params_channels(params) != 1) {
346 			pr_err("Only mono supported.\n");
347 			return -EINVAL;
348 		}
349 
350 		return 0;
351 	}
352 
353 	if (wl1273->mode == WL1273_MODE_FM_TX &&
354 	    substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
355 		pr_err("Only playback supported with TX.\n");
356 		return -EINVAL;
357 	}
358 
359 	if (wl1273->mode == WL1273_MODE_FM_RX  &&
360 	    substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
361 		pr_err("Only capture supported with RX.\n");
362 		return -EINVAL;
363 	}
364 
365 	if (wl1273->mode != WL1273_MODE_FM_RX  &&
366 	    wl1273->mode != WL1273_MODE_FM_TX) {
367 		pr_err("Unexpected mode: %d.\n", wl1273->mode);
368 		return -EINVAL;
369 	}
370 
371 	r = snd_wl1273_fm_set_i2s_mode(core, rate, width);
372 	if (r)
373 		return r;
374 
375 	wl1273->channels = params_channels(params);
376 	r = snd_wl1273_fm_set_channel_number(core, wl1273->channels);
377 	if (r)
378 		return r;
379 
380 	return 0;
381 }
382 
383 static struct snd_soc_dai_ops wl1273_dai_ops = {
384 	.startup	= wl1273_startup,
385 	.hw_params	= wl1273_hw_params,
386 };
387 
388 static struct snd_soc_dai_driver wl1273_dai = {
389 	.name = "wl1273-fm",
390 	.playback = {
391 		.stream_name = "Playback",
392 		.channels_min = 1,
393 		.channels_max = 2,
394 		.rates = SNDRV_PCM_RATE_8000_48000,
395 		.formats = SNDRV_PCM_FMTBIT_S16_LE},
396 	.capture = {
397 		.stream_name = "Capture",
398 		.channels_min = 1,
399 		.channels_max = 2,
400 		.rates = SNDRV_PCM_RATE_8000_48000,
401 		.formats = SNDRV_PCM_FMTBIT_S16_LE},
402 	.ops = &wl1273_dai_ops,
403 };
404 
405 /* Audio interface format for the soc_card driver */
406 int wl1273_get_format(struct snd_soc_codec *codec, unsigned int *fmt)
407 {
408 	struct wl1273_priv *wl1273;
409 
410 	if (codec == NULL || fmt == NULL)
411 		return -EINVAL;
412 
413 	wl1273 = snd_soc_codec_get_drvdata(codec);
414 
415 	switch (wl1273->mode) {
416 	case WL1273_MODE_FM_RX:
417 	case WL1273_MODE_FM_TX:
418 		*fmt =	SND_SOC_DAIFMT_I2S |
419 			SND_SOC_DAIFMT_NB_NF |
420 			SND_SOC_DAIFMT_CBM_CFM;
421 
422 		break;
423 	case WL1273_MODE_BT:
424 		*fmt =	SND_SOC_DAIFMT_DSP_A |
425 			SND_SOC_DAIFMT_IB_NF |
426 			SND_SOC_DAIFMT_CBM_CFM;
427 
428 		break;
429 	default:
430 		return -EINVAL;
431 	}
432 
433 	return 0;
434 }
435 EXPORT_SYMBOL_GPL(wl1273_get_format);
436 
437 static int wl1273_probe(struct snd_soc_codec *codec)
438 {
439 	struct wl1273_core **core = codec->dev->platform_data;
440 	struct wl1273_priv *wl1273;
441 	int r;
442 
443 	dev_dbg(codec->dev, "%s.\n", __func__);
444 
445 	if (!core) {
446 		dev_err(codec->dev, "Platform data is missing.\n");
447 		return -EINVAL;
448 	}
449 
450 	wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL);
451 	if (wl1273 == NULL) {
452 		dev_err(codec->dev, "Cannot allocate memory.\n");
453 		return -ENOMEM;
454 	}
455 
456 	wl1273->mode = WL1273_MODE_BT;
457 	wl1273->core = *core;
458 
459 	snd_soc_codec_set_drvdata(codec, wl1273);
460 	mutex_init(&codec->mutex);
461 
462 	r = snd_soc_add_controls(codec, wl1273_controls,
463 				 ARRAY_SIZE(wl1273_controls));
464 	if (r)
465 		kfree(wl1273);
466 
467 	return r;
468 }
469 
470 static int wl1273_remove(struct snd_soc_codec *codec)
471 {
472 	struct wl1273_priv *wl1273 = snd_soc_codec_get_drvdata(codec);
473 
474 	dev_dbg(codec->dev, "%s\n", __func__);
475 	kfree(wl1273);
476 
477 	return 0;
478 }
479 
480 static struct snd_soc_codec_driver soc_codec_dev_wl1273 = {
481 	.probe = wl1273_probe,
482 	.remove = wl1273_remove,
483 };
484 
485 static int __devinit wl1273_platform_probe(struct platform_device *pdev)
486 {
487 	return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_wl1273,
488 				      &wl1273_dai, 1);
489 }
490 
491 static int __devexit wl1273_platform_remove(struct platform_device *pdev)
492 {
493 	snd_soc_unregister_codec(&pdev->dev);
494 	return 0;
495 }
496 
497 MODULE_ALIAS("platform:wl1273-codec");
498 
499 static struct platform_driver wl1273_platform_driver = {
500 	.driver		= {
501 		.name	= "wl1273-codec",
502 		.owner	= THIS_MODULE,
503 	},
504 	.probe		= wl1273_platform_probe,
505 	.remove		= __devexit_p(wl1273_platform_remove),
506 };
507 
508 static int __init wl1273_init(void)
509 {
510 	return platform_driver_register(&wl1273_platform_driver);
511 }
512 module_init(wl1273_init);
513 
514 static void __exit wl1273_exit(void)
515 {
516 	platform_driver_unregister(&wl1273_platform_driver);
517 }
518 module_exit(wl1273_exit);
519 
520 MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>");
521 MODULE_DESCRIPTION("ASoC WL1273 codec driver");
522 MODULE_LICENSE("GPL");
523