Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * ALSA SoC WL1273 codec driver |
| 3 | * |
| 4 | * Author: Matti Aaltonen, <matti.j.aaltonen@nokia.com> |
| 5 | * |
Matti Aaltonen | 40285f8 | 2011-03-01 10:10:37 -0300 | [diff] [blame] | 6 | * Copyright: (C) 2010, 2011 Nokia Corporation |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 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> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 26 | #include <linux/module.h> |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 27 | #include <sound/pcm.h> |
| 28 | #include <sound/pcm_params.h> |
Jarkko Nikula | 0d911bae | 2010-11-21 19:48:46 +0200 | [diff] [blame] | 29 | #include <sound/soc.h> |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 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 | |
| 43 | static int snd_wl1273_fm_set_i2s_mode(struct wl1273_core *core, |
| 44 | int rate, int width) |
| 45 | { |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 46 | struct device *dev = &core->client->dev; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 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) { |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 127 | r = core->write(core, WL1273_I2S_MODE_CONFIG_SET, mode); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 128 | if (r) |
| 129 | goto out; |
| 130 | |
| 131 | core->i2s_mode = mode; |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 132 | r = core->write(core, WL1273_AUDIO_ENABLE, |
| 133 | WL1273_AUDIO_ENABLE_I2S); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 134 | if (r) |
| 135 | goto out; |
| 136 | } |
| 137 | out: |
| 138 | mutex_unlock(&core->lock); |
| 139 | |
| 140 | return r; |
| 141 | } |
| 142 | |
| 143 | static int snd_wl1273_fm_set_channel_number(struct wl1273_core *core, |
| 144 | int channel_number) |
| 145 | { |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 146 | struct device *dev = &core->client->dev; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 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) |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 157 | r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_MONO); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 158 | else if (channel_number == 1 && core->mode == WL1273_MODE_TX) |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 159 | r = core->write(core, WL1273_MONO_SET, WL1273_TX_MONO); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 160 | else if (channel_number == 2 && core->mode == WL1273_MODE_RX) |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 161 | r = core->write(core, WL1273_MOST_MODE_SET, WL1273_RX_STEREO); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 162 | else if (channel_number == 2 && core->mode == WL1273_MODE_TX) |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 163 | r = core->write(core, WL1273_MONO_SET, WL1273_TX_STEREO); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 164 | else |
| 165 | r = -EINVAL; |
| 166 | out: |
| 167 | mutex_unlock(&core->lock); |
| 168 | |
| 169 | return r; |
| 170 | } |
| 171 | |
| 172 | static int snd_wl1273_get_audio_route(struct snd_kcontrol *kcontrol, |
| 173 | struct snd_ctl_elem_value *ucontrol) |
| 174 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 175 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 176 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 177 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 178 | ucontrol->value.enumerated.item[0] = wl1273->mode; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
Matti Aaltonen | 40285f8 | 2011-03-01 10:10:37 -0300 | [diff] [blame] | 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" }; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 189 | |
| 190 | static int snd_wl1273_set_audio_route(struct snd_kcontrol *kcontrol, |
| 191 | struct snd_ctl_elem_value *ucontrol) |
| 192 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 193 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 194 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 195 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 196 | if (wl1273->mode == ucontrol->value.enumerated.item[0]) |
Matti J. Aaltonen | 2c4ee9b | 2010-09-10 10:41:30 +0300 | [diff] [blame] | 197 | return 0; |
| 198 | |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 199 | /* Do not allow changes while stream is running */ |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 200 | if (snd_soc_component_is_active(component)) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 201 | return -EPERM; |
| 202 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 203 | if (ucontrol->value.enumerated.item[0] >= ARRAY_SIZE(wl1273_audio_route)) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 206 | wl1273->mode = ucontrol->value.enumerated.item[0]; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 207 | |
| 208 | return 1; |
| 209 | } |
| 210 | |
Takashi Iwai | 4ec20a9 | 2014-02-18 10:33:06 +0100 | [diff] [blame] | 211 | static SOC_ENUM_SINGLE_EXT_DECL(wl1273_enum, wl1273_audio_route); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 212 | |
| 213 | static int snd_wl1273_fm_audio_get(struct snd_kcontrol *kcontrol, |
| 214 | struct snd_ctl_elem_value *ucontrol) |
| 215 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 216 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 217 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 218 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 219 | dev_dbg(component->dev, "%s: enter.\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 220 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 221 | ucontrol->value.enumerated.item[0] = wl1273->core->audio_mode; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static int snd_wl1273_fm_audio_put(struct snd_kcontrol *kcontrol, |
| 227 | struct snd_ctl_elem_value *ucontrol) |
| 228 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 229 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 230 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 231 | int val, r = 0; |
| 232 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 233 | dev_dbg(component->dev, "%s: enter.\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 234 | |
Takashi Iwai | 154a0fd | 2016-02-29 18:08:04 +0100 | [diff] [blame] | 235 | val = ucontrol->value.enumerated.item[0]; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 236 | if (wl1273->core->audio_mode == val) |
| 237 | return 0; |
| 238 | |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 239 | r = wl1273->core->set_audio(wl1273->core, val); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 240 | if (r < 0) |
| 241 | return r; |
| 242 | |
| 243 | return 1; |
| 244 | } |
| 245 | |
Matti Aaltonen | 40285f8 | 2011-03-01 10:10:37 -0300 | [diff] [blame] | 246 | static const char * const wl1273_audio_strings[] = { "Digital", "Analog" }; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 247 | |
Takashi Iwai | 4ec20a9 | 2014-02-18 10:33:06 +0100 | [diff] [blame] | 248 | static SOC_ENUM_SINGLE_EXT_DECL(wl1273_audio_enum, wl1273_audio_strings); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 249 | |
| 250 | static int snd_wl1273_fm_volume_get(struct snd_kcontrol *kcontrol, |
| 251 | struct snd_ctl_elem_value *ucontrol) |
| 252 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 253 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 254 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 255 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 256 | dev_dbg(component->dev, "%s: enter.\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 257 | |
| 258 | ucontrol->value.integer.value[0] = wl1273->core->volume; |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
| 263 | static int snd_wl1273_fm_volume_put(struct snd_kcontrol *kcontrol, |
| 264 | struct snd_ctl_elem_value *ucontrol) |
| 265 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 266 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 267 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 268 | int r; |
| 269 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 270 | dev_dbg(component->dev, "%s: enter.\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 271 | |
Matti J. Aaltonen | 228dd54 | 2011-01-13 15:22:45 +0200 | [diff] [blame] | 272 | r = wl1273->core->set_volume(wl1273->core, |
| 273 | ucontrol->value.integer.value[0]); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 274 | if (r) |
| 275 | return r; |
| 276 | |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | static const struct snd_kcontrol_new wl1273_controls[] = { |
| 281 | SOC_ENUM_EXT("Codec Mode", wl1273_enum, |
| 282 | snd_wl1273_get_audio_route, snd_wl1273_set_audio_route), |
| 283 | SOC_ENUM_EXT("Audio Switch", wl1273_audio_enum, |
| 284 | snd_wl1273_fm_audio_get, snd_wl1273_fm_audio_put), |
| 285 | SOC_SINGLE_EXT("Volume", 0, 0, WL1273_MAX_VOLUME, 0, |
| 286 | snd_wl1273_fm_volume_get, snd_wl1273_fm_volume_put), |
| 287 | }; |
| 288 | |
Mark Brown | e29deb4 | 2013-08-18 18:25:53 +0100 | [diff] [blame] | 289 | static const struct snd_soc_dapm_widget wl1273_dapm_widgets[] = { |
| 290 | SND_SOC_DAPM_INPUT("RX"), |
| 291 | |
| 292 | SND_SOC_DAPM_OUTPUT("TX"), |
| 293 | }; |
| 294 | |
| 295 | static const struct snd_soc_dapm_route wl1273_dapm_routes[] = { |
| 296 | { "Capture", NULL, "RX" }, |
| 297 | |
| 298 | { "TX", NULL, "Playback" }, |
| 299 | }; |
| 300 | |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 301 | static int wl1273_startup(struct snd_pcm_substream *substream, |
| 302 | struct snd_soc_dai *dai) |
| 303 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 304 | struct snd_soc_component *component = dai->component; |
| 305 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 306 | |
| 307 | switch (wl1273->mode) { |
| 308 | case WL1273_MODE_BT: |
Lars-Peter Clausen | 95c68b8 | 2015-10-18 15:39:25 +0200 | [diff] [blame] | 309 | snd_pcm_hw_constraint_single(substream->runtime, |
| 310 | SNDRV_PCM_HW_PARAM_RATE, 8000); |
| 311 | snd_pcm_hw_constraint_single(substream->runtime, |
| 312 | SNDRV_PCM_HW_PARAM_CHANNELS, 1); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 313 | break; |
| 314 | case WL1273_MODE_FM_RX: |
| 315 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 316 | pr_err("Cannot play in RX mode.\n"); |
| 317 | return -EINVAL; |
| 318 | } |
| 319 | break; |
| 320 | case WL1273_MODE_FM_TX: |
| 321 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 322 | pr_err("Cannot capture in TX mode.\n"); |
| 323 | return -EINVAL; |
| 324 | } |
| 325 | break; |
| 326 | default: |
| 327 | return -EINVAL; |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static int wl1273_hw_params(struct snd_pcm_substream *substream, |
| 335 | struct snd_pcm_hw_params *params, |
| 336 | struct snd_soc_dai *dai) |
| 337 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 338 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(dai->component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 339 | struct wl1273_core *core = wl1273->core; |
| 340 | unsigned int rate, width, r; |
| 341 | |
Mark Brown | 9630181 | 2014-07-31 12:49:36 +0100 | [diff] [blame] | 342 | if (params_width(params) != 16) { |
| 343 | dev_err(dai->dev, "%d bits/sample not supported\n", |
| 344 | params_width(params)); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 345 | return -EINVAL; |
| 346 | } |
| 347 | |
| 348 | rate = params_rate(params); |
| 349 | width = hw_param_interval(params, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min; |
| 350 | |
| 351 | if (wl1273->mode == WL1273_MODE_BT) { |
| 352 | if (rate != 8000) { |
| 353 | pr_err("Rate %d not supported.\n", params_rate(params)); |
| 354 | return -EINVAL; |
| 355 | } |
| 356 | |
| 357 | if (params_channels(params) != 1) { |
| 358 | pr_err("Only mono supported.\n"); |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | if (wl1273->mode == WL1273_MODE_FM_TX && |
| 366 | substream->stream == SNDRV_PCM_STREAM_CAPTURE) { |
| 367 | pr_err("Only playback supported with TX.\n"); |
| 368 | return -EINVAL; |
| 369 | } |
| 370 | |
| 371 | if (wl1273->mode == WL1273_MODE_FM_RX && |
| 372 | substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 373 | pr_err("Only capture supported with RX.\n"); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | if (wl1273->mode != WL1273_MODE_FM_RX && |
| 378 | wl1273->mode != WL1273_MODE_FM_TX) { |
| 379 | pr_err("Unexpected mode: %d.\n", wl1273->mode); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | r = snd_wl1273_fm_set_i2s_mode(core, rate, width); |
| 384 | if (r) |
| 385 | return r; |
| 386 | |
| 387 | wl1273->channels = params_channels(params); |
| 388 | r = snd_wl1273_fm_set_channel_number(core, wl1273->channels); |
| 389 | if (r) |
| 390 | return r; |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 395 | static const struct snd_soc_dai_ops wl1273_dai_ops = { |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 396 | .startup = wl1273_startup, |
| 397 | .hw_params = wl1273_hw_params, |
| 398 | }; |
| 399 | |
| 400 | static struct snd_soc_dai_driver wl1273_dai = { |
| 401 | .name = "wl1273-fm", |
| 402 | .playback = { |
| 403 | .stream_name = "Playback", |
| 404 | .channels_min = 1, |
| 405 | .channels_max = 2, |
| 406 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 407 | .formats = SNDRV_PCM_FMTBIT_S16_LE}, |
| 408 | .capture = { |
| 409 | .stream_name = "Capture", |
| 410 | .channels_min = 1, |
| 411 | .channels_max = 2, |
| 412 | .rates = SNDRV_PCM_RATE_8000_48000, |
| 413 | .formats = SNDRV_PCM_FMTBIT_S16_LE}, |
| 414 | .ops = &wl1273_dai_ops, |
| 415 | }; |
| 416 | |
| 417 | /* Audio interface format for the soc_card driver */ |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 418 | int wl1273_get_format(struct snd_soc_component *component, unsigned int *fmt) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 419 | { |
| 420 | struct wl1273_priv *wl1273; |
| 421 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 422 | if (component == NULL || fmt == NULL) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 423 | return -EINVAL; |
| 424 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 425 | wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 426 | |
| 427 | switch (wl1273->mode) { |
| 428 | case WL1273_MODE_FM_RX: |
| 429 | case WL1273_MODE_FM_TX: |
| 430 | *fmt = SND_SOC_DAIFMT_I2S | |
| 431 | SND_SOC_DAIFMT_NB_NF | |
| 432 | SND_SOC_DAIFMT_CBM_CFM; |
| 433 | |
| 434 | break; |
| 435 | case WL1273_MODE_BT: |
| 436 | *fmt = SND_SOC_DAIFMT_DSP_A | |
| 437 | SND_SOC_DAIFMT_IB_NF | |
| 438 | SND_SOC_DAIFMT_CBM_CFM; |
| 439 | |
| 440 | break; |
| 441 | default: |
| 442 | return -EINVAL; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | EXPORT_SYMBOL_GPL(wl1273_get_format); |
| 448 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 449 | static int wl1273_probe(struct snd_soc_component *component) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 450 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 451 | struct wl1273_core **core = component->dev->platform_data; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 452 | struct wl1273_priv *wl1273; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 453 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 454 | dev_dbg(component->dev, "%s.\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 455 | |
| 456 | if (!core) { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 457 | dev_err(component->dev, "Platform data is missing.\n"); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | wl1273 = kzalloc(sizeof(struct wl1273_priv), GFP_KERNEL); |
Sachin Kamat | ac872d3 | 2014-06-20 15:29:04 +0530 | [diff] [blame] | 462 | if (!wl1273) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 463 | return -ENOMEM; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 464 | |
| 465 | wl1273->mode = WL1273_MODE_BT; |
| 466 | wl1273->core = *core; |
| 467 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 468 | snd_soc_component_set_drvdata(component, wl1273); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 469 | |
Lars-Peter Clausen | c8b5d08 | 2014-11-05 10:46:33 +0100 | [diff] [blame] | 470 | return 0; |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 471 | } |
| 472 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 473 | static void wl1273_remove(struct snd_soc_component *component) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 474 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 475 | struct wl1273_priv *wl1273 = snd_soc_component_get_drvdata(component); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 476 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 477 | dev_dbg(component->dev, "%s\n", __func__); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 478 | kfree(wl1273); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 479 | } |
| 480 | |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 481 | static const struct snd_soc_component_driver soc_component_dev_wl1273 = { |
| 482 | .probe = wl1273_probe, |
| 483 | .remove = wl1273_remove, |
| 484 | .controls = wl1273_controls, |
| 485 | .num_controls = ARRAY_SIZE(wl1273_controls), |
| 486 | .dapm_widgets = wl1273_dapm_widgets, |
| 487 | .num_dapm_widgets = ARRAY_SIZE(wl1273_dapm_widgets), |
| 488 | .dapm_routes = wl1273_dapm_routes, |
| 489 | .num_dapm_routes = ARRAY_SIZE(wl1273_dapm_routes), |
| 490 | .idle_bias_on = 1, |
| 491 | .use_pmdown_time = 1, |
| 492 | .endianness = 1, |
| 493 | .non_legacy_dai_naming = 1, |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 494 | }; |
| 495 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 496 | static int wl1273_platform_probe(struct platform_device *pdev) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 497 | { |
Kuninori Morimoto | 559ab39 | 2018-01-29 04:36:21 +0000 | [diff] [blame] | 498 | return devm_snd_soc_register_component(&pdev->dev, |
| 499 | &soc_component_dev_wl1273, |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 500 | &wl1273_dai, 1); |
| 501 | } |
| 502 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 503 | static int wl1273_platform_remove(struct platform_device *pdev) |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 504 | { |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | MODULE_ALIAS("platform:wl1273-codec"); |
| 509 | |
| 510 | static struct platform_driver wl1273_platform_driver = { |
| 511 | .driver = { |
| 512 | .name = "wl1273-codec", |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 513 | }, |
| 514 | .probe = wl1273_platform_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 515 | .remove = wl1273_platform_remove, |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 516 | }; |
| 517 | |
Mark Brown | 5bbcc3c | 2011-11-23 22:52:08 +0000 | [diff] [blame] | 518 | module_platform_driver(wl1273_platform_driver); |
Matti J. Aaltonen | 3fabe08 | 2010-08-20 12:32:46 +0300 | [diff] [blame] | 519 | |
| 520 | MODULE_AUTHOR("Matti Aaltonen <matti.j.aaltonen@nokia.com>"); |
| 521 | MODULE_DESCRIPTION("ASoC WL1273 codec driver"); |
| 522 | MODULE_LICENSE("GPL"); |