Linux Audio

Check our new training course

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