Rakesh Ughreja | 6bae5ea | 2018-08-22 15:25:03 -0500 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | // Copyright(c) 2015-18 Intel Corporation. |
| 3 | |
| 4 | /* |
| 5 | * hdac_hda.c - ASoC extensions to reuse the legacy HDA codec drivers |
| 6 | * with ASoC platform drivers. These APIs are called by the legacy HDA |
| 7 | * codec drivers using hdac_ext_bus_ops ops. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/pm_runtime.h> |
| 14 | #include <sound/pcm_params.h> |
| 15 | #include <sound/soc.h> |
| 16 | #include <sound/hdaudio_ext.h> |
| 17 | #include <sound/hda_codec.h> |
| 18 | #include <sound/hda_register.h> |
| 19 | #include "hdac_hda.h" |
| 20 | |
| 21 | #define HDAC_ANALOG_DAI_ID 0 |
| 22 | #define HDAC_DIGITAL_DAI_ID 1 |
| 23 | #define HDAC_ALT_ANALOG_DAI_ID 2 |
| 24 | |
| 25 | #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 26 | SNDRV_PCM_FMTBIT_U8 | \ |
| 27 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 28 | SNDRV_PCM_FMTBIT_U16_LE | \ |
| 29 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 30 | SNDRV_PCM_FMTBIT_U24_LE | \ |
| 31 | SNDRV_PCM_FMTBIT_S32_LE | \ |
| 32 | SNDRV_PCM_FMTBIT_U32_LE | \ |
| 33 | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE) |
| 34 | |
| 35 | static int hdac_hda_dai_open(struct snd_pcm_substream *substream, |
| 36 | struct snd_soc_dai *dai); |
| 37 | static void hdac_hda_dai_close(struct snd_pcm_substream *substream, |
| 38 | struct snd_soc_dai *dai); |
| 39 | static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream, |
| 40 | struct snd_soc_dai *dai); |
| 41 | static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream, |
| 42 | struct snd_soc_dai *dai); |
| 43 | static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai, |
| 44 | unsigned int tx_mask, unsigned int rx_mask, |
| 45 | int slots, int slot_width); |
| 46 | static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt, |
| 47 | struct snd_soc_dai *dai); |
| 48 | |
| 49 | static struct snd_soc_dai_ops hdac_hda_dai_ops = { |
| 50 | .startup = hdac_hda_dai_open, |
| 51 | .shutdown = hdac_hda_dai_close, |
| 52 | .prepare = hdac_hda_dai_prepare, |
| 53 | .hw_free = hdac_hda_dai_hw_free, |
| 54 | .set_tdm_slot = hdac_hda_dai_set_tdm_slot, |
| 55 | }; |
| 56 | |
| 57 | static struct snd_soc_dai_driver hdac_hda_dais[] = { |
| 58 | { |
| 59 | .id = HDAC_ANALOG_DAI_ID, |
| 60 | .name = "Analog Codec DAI", |
| 61 | .ops = &hdac_hda_dai_ops, |
| 62 | .playback = { |
| 63 | .stream_name = "Analog Codec Playback", |
| 64 | .channels_min = 1, |
| 65 | .channels_max = 16, |
| 66 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 67 | .formats = STUB_FORMATS, |
| 68 | .sig_bits = 24, |
| 69 | }, |
| 70 | .capture = { |
| 71 | .stream_name = "Analog Codec Capture", |
| 72 | .channels_min = 1, |
| 73 | .channels_max = 16, |
| 74 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 75 | .formats = STUB_FORMATS, |
| 76 | .sig_bits = 24, |
| 77 | }, |
| 78 | }, |
| 79 | { |
| 80 | .id = HDAC_DIGITAL_DAI_ID, |
| 81 | .name = "Digital Codec DAI", |
| 82 | .ops = &hdac_hda_dai_ops, |
| 83 | .playback = { |
| 84 | .stream_name = "Digital Codec Playback", |
| 85 | .channels_min = 1, |
| 86 | .channels_max = 16, |
| 87 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 88 | .formats = STUB_FORMATS, |
| 89 | .sig_bits = 24, |
| 90 | }, |
| 91 | .capture = { |
| 92 | .stream_name = "Digital Codec Capture", |
| 93 | .channels_min = 1, |
| 94 | .channels_max = 16, |
| 95 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 96 | .formats = STUB_FORMATS, |
| 97 | .sig_bits = 24, |
| 98 | }, |
| 99 | }, |
| 100 | { |
| 101 | .id = HDAC_ALT_ANALOG_DAI_ID, |
| 102 | .name = "Alt Analog Codec DAI", |
| 103 | .ops = &hdac_hda_dai_ops, |
| 104 | .playback = { |
| 105 | .stream_name = "Alt Analog Codec Playback", |
| 106 | .channels_min = 1, |
| 107 | .channels_max = 16, |
| 108 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 109 | .formats = STUB_FORMATS, |
| 110 | .sig_bits = 24, |
| 111 | }, |
| 112 | .capture = { |
| 113 | .stream_name = "Alt Analog Codec Capture", |
| 114 | .channels_min = 1, |
| 115 | .channels_max = 16, |
| 116 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 117 | .formats = STUB_FORMATS, |
| 118 | .sig_bits = 24, |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | }; |
| 123 | |
| 124 | static int hdac_hda_dai_set_tdm_slot(struct snd_soc_dai *dai, |
| 125 | unsigned int tx_mask, unsigned int rx_mask, |
| 126 | int slots, int slot_width) |
| 127 | { |
| 128 | struct snd_soc_component *component = dai->component; |
| 129 | struct hdac_hda_priv *hda_pvt; |
| 130 | struct hdac_hda_pcm *pcm; |
| 131 | |
| 132 | hda_pvt = snd_soc_component_get_drvdata(component); |
| 133 | pcm = &hda_pvt->pcm[dai->id]; |
| 134 | if (tx_mask) |
| 135 | pcm[dai->id].stream_tag[SNDRV_PCM_STREAM_PLAYBACK] = tx_mask; |
| 136 | else |
| 137 | pcm[dai->id].stream_tag[SNDRV_PCM_STREAM_CAPTURE] = rx_mask; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int hdac_hda_dai_hw_free(struct snd_pcm_substream *substream, |
| 143 | struct snd_soc_dai *dai) |
| 144 | { |
| 145 | struct snd_soc_component *component = dai->component; |
| 146 | struct hdac_hda_priv *hda_pvt; |
| 147 | struct hda_pcm_stream *hda_stream; |
| 148 | struct hda_pcm *pcm; |
| 149 | |
| 150 | hda_pvt = snd_soc_component_get_drvdata(component); |
| 151 | pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); |
| 152 | if (!pcm) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | hda_stream = &pcm->stream[substream->stream]; |
| 156 | snd_hda_codec_cleanup(&hda_pvt->codec, hda_stream, substream); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static int hdac_hda_dai_prepare(struct snd_pcm_substream *substream, |
| 162 | struct snd_soc_dai *dai) |
| 163 | { |
| 164 | struct snd_soc_component *component = dai->component; |
| 165 | struct hdac_hda_priv *hda_pvt; |
| 166 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 167 | struct hdac_device *hdev; |
| 168 | struct hda_pcm_stream *hda_stream; |
| 169 | unsigned int format_val; |
| 170 | struct hda_pcm *pcm; |
| 171 | unsigned int stream; |
| 172 | int ret = 0; |
| 173 | |
| 174 | hda_pvt = snd_soc_component_get_drvdata(component); |
| 175 | hdev = &hda_pvt->codec.core; |
| 176 | pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); |
| 177 | if (!pcm) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | hda_stream = &pcm->stream[substream->stream]; |
| 181 | |
| 182 | format_val = snd_hdac_calc_stream_format(runtime->rate, |
| 183 | runtime->channels, |
| 184 | runtime->format, |
| 185 | hda_stream->maxbps, |
| 186 | 0); |
| 187 | if (!format_val) { |
| 188 | dev_err(&hdev->dev, |
| 189 | "invalid format_val, rate=%d, ch=%d, format=%d\n", |
| 190 | runtime->rate, runtime->channels, runtime->format); |
| 191 | return -EINVAL; |
| 192 | } |
| 193 | |
| 194 | stream = hda_pvt->pcm[dai->id].stream_tag[substream->stream]; |
| 195 | |
| 196 | ret = snd_hda_codec_prepare(&hda_pvt->codec, hda_stream, |
| 197 | stream, format_val, substream); |
| 198 | if (ret < 0) |
| 199 | dev_err(&hdev->dev, "codec prepare failed %d\n", ret); |
| 200 | |
| 201 | return ret; |
| 202 | } |
| 203 | |
| 204 | static int hdac_hda_dai_open(struct snd_pcm_substream *substream, |
| 205 | struct snd_soc_dai *dai) |
| 206 | { |
| 207 | struct snd_soc_component *component = dai->component; |
| 208 | struct hdac_hda_priv *hda_pvt; |
| 209 | struct hda_pcm_stream *hda_stream; |
| 210 | struct hda_pcm *pcm; |
| 211 | int ret; |
| 212 | |
| 213 | hda_pvt = snd_soc_component_get_drvdata(component); |
| 214 | pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); |
| 215 | if (!pcm) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | snd_hda_codec_pcm_get(pcm); |
| 219 | |
| 220 | hda_stream = &pcm->stream[substream->stream]; |
| 221 | |
| 222 | ret = hda_stream->ops.open(hda_stream, &hda_pvt->codec, substream); |
| 223 | if (ret < 0) |
| 224 | snd_hda_codec_pcm_put(pcm); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static void hdac_hda_dai_close(struct snd_pcm_substream *substream, |
| 230 | struct snd_soc_dai *dai) |
| 231 | { |
| 232 | struct snd_soc_component *component = dai->component; |
| 233 | struct hdac_hda_priv *hda_pvt; |
| 234 | struct hda_pcm_stream *hda_stream; |
| 235 | struct hda_pcm *pcm; |
| 236 | |
| 237 | hda_pvt = snd_soc_component_get_drvdata(component); |
| 238 | pcm = snd_soc_find_pcm_from_dai(hda_pvt, dai); |
| 239 | if (!pcm) |
| 240 | return; |
| 241 | |
| 242 | hda_stream = &pcm->stream[substream->stream]; |
| 243 | |
| 244 | hda_stream->ops.close(hda_stream, &hda_pvt->codec, substream); |
| 245 | |
| 246 | snd_hda_codec_pcm_put(pcm); |
| 247 | } |
| 248 | |
| 249 | static struct hda_pcm *snd_soc_find_pcm_from_dai(struct hdac_hda_priv *hda_pvt, |
| 250 | struct snd_soc_dai *dai) |
| 251 | { |
| 252 | struct hda_codec *hcodec = &hda_pvt->codec; |
| 253 | struct hda_pcm *cpcm; |
| 254 | const char *pcm_name; |
| 255 | |
| 256 | switch (dai->id) { |
| 257 | case HDAC_ANALOG_DAI_ID: |
| 258 | pcm_name = "Analog"; |
| 259 | break; |
| 260 | case HDAC_DIGITAL_DAI_ID: |
| 261 | pcm_name = "Digital"; |
| 262 | break; |
| 263 | case HDAC_ALT_ANALOG_DAI_ID: |
| 264 | pcm_name = "Alt Analog"; |
| 265 | break; |
| 266 | default: |
| 267 | dev_err(&hcodec->core.dev, "invalid dai id %d\n", dai->id); |
| 268 | return NULL; |
| 269 | } |
| 270 | |
| 271 | list_for_each_entry(cpcm, &hcodec->pcm_list_head, list) { |
| 272 | if (strpbrk(cpcm->name, pcm_name)) |
| 273 | return cpcm; |
| 274 | } |
| 275 | |
| 276 | dev_err(&hcodec->core.dev, "didn't find PCM for DAI %s\n", dai->name); |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | static int hdac_hda_codec_probe(struct snd_soc_component *component) |
| 281 | { |
| 282 | struct hdac_hda_priv *hda_pvt = |
| 283 | snd_soc_component_get_drvdata(component); |
| 284 | struct snd_soc_dapm_context *dapm = |
| 285 | snd_soc_component_get_dapm(component); |
| 286 | struct hdac_device *hdev = &hda_pvt->codec.core; |
| 287 | struct hda_codec *hcodec = &hda_pvt->codec; |
| 288 | struct hdac_ext_link *hlink; |
| 289 | hda_codec_patch_t patch; |
| 290 | int ret; |
| 291 | |
| 292 | hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); |
| 293 | if (!hlink) { |
| 294 | dev_err(&hdev->dev, "hdac link not found\n"); |
| 295 | return -EIO; |
| 296 | } |
| 297 | |
| 298 | snd_hdac_ext_bus_link_get(hdev->bus, hlink); |
| 299 | |
| 300 | ret = snd_hda_codec_device_new(hcodec->bus, component->card->snd_card, |
| 301 | hdev->addr, hcodec); |
| 302 | if (ret < 0) { |
| 303 | dev_err(&hdev->dev, "failed to create hda codec %d\n", ret); |
| 304 | goto error_no_pm; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * snd_hda_codec_device_new decrements the usage count so call get pm |
| 309 | * else the device will be powered off |
| 310 | */ |
| 311 | pm_runtime_get_noresume(&hdev->dev); |
| 312 | |
| 313 | hcodec->bus->card = dapm->card->snd_card; |
| 314 | |
| 315 | ret = snd_hda_codec_set_name(hcodec, hcodec->preset->name); |
| 316 | if (ret < 0) { |
| 317 | dev_err(&hdev->dev, "name failed %s\n", hcodec->preset->name); |
| 318 | goto error; |
| 319 | } |
| 320 | |
| 321 | ret = snd_hdac_regmap_init(&hcodec->core); |
| 322 | if (ret < 0) { |
| 323 | dev_err(&hdev->dev, "regmap init failed\n"); |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | patch = (hda_codec_patch_t)hcodec->preset->driver_data; |
| 328 | if (patch) { |
| 329 | ret = patch(hcodec); |
| 330 | if (ret < 0) { |
| 331 | dev_err(&hdev->dev, "patch failed %d\n", ret); |
| 332 | goto error; |
| 333 | } |
| 334 | } else { |
| 335 | dev_dbg(&hdev->dev, "no patch file found\n"); |
| 336 | } |
| 337 | |
| 338 | ret = snd_hda_codec_parse_pcms(hcodec); |
| 339 | if (ret < 0) { |
| 340 | dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret); |
| 341 | goto error; |
| 342 | } |
| 343 | |
| 344 | ret = snd_hda_codec_build_controls(hcodec); |
| 345 | if (ret < 0) { |
| 346 | dev_err(&hdev->dev, "unable to create controls %d\n", ret); |
| 347 | goto error; |
| 348 | } |
| 349 | |
| 350 | hcodec->core.lazy_cache = true; |
| 351 | |
| 352 | /* |
| 353 | * hdac_device core already sets the state to active and calls |
| 354 | * get_noresume. So enable runtime and set the device to suspend. |
| 355 | * pm_runtime_enable is also called during codec registeration |
| 356 | */ |
| 357 | pm_runtime_put(&hdev->dev); |
| 358 | pm_runtime_suspend(&hdev->dev); |
| 359 | |
| 360 | return 0; |
| 361 | |
| 362 | error: |
| 363 | pm_runtime_put(&hdev->dev); |
| 364 | error_no_pm: |
| 365 | snd_hdac_ext_bus_link_put(hdev->bus, hlink); |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | static void hdac_hda_codec_remove(struct snd_soc_component *component) |
| 370 | { |
| 371 | struct hdac_hda_priv *hda_pvt = |
| 372 | snd_soc_component_get_drvdata(component); |
| 373 | struct hdac_device *hdev = &hda_pvt->codec.core; |
| 374 | struct hdac_ext_link *hlink = NULL; |
| 375 | |
| 376 | hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); |
| 377 | if (!hlink) { |
| 378 | dev_err(&hdev->dev, "hdac link not found\n"); |
| 379 | return; |
| 380 | } |
| 381 | |
| 382 | snd_hdac_ext_bus_link_put(hdev->bus, hlink); |
| 383 | pm_runtime_disable(&hdev->dev); |
| 384 | } |
| 385 | |
| 386 | static const struct snd_soc_dapm_route hdac_hda_dapm_routes[] = { |
| 387 | {"AIF1TX", NULL, "Codec Input Pin1"}, |
| 388 | {"AIF2TX", NULL, "Codec Input Pin2"}, |
| 389 | {"AIF3TX", NULL, "Codec Input Pin3"}, |
| 390 | |
| 391 | {"Codec Output Pin1", NULL, "AIF1RX"}, |
| 392 | {"Codec Output Pin2", NULL, "AIF2RX"}, |
| 393 | {"Codec Output Pin3", NULL, "AIF3RX"}, |
| 394 | }; |
| 395 | |
| 396 | static const struct snd_soc_dapm_widget hdac_hda_dapm_widgets[] = { |
| 397 | /* Audio Interface */ |
| 398 | SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, |
| 399 | SND_SOC_NOPM, 0, 0), |
| 400 | SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, |
| 401 | SND_SOC_NOPM, 0, 0), |
| 402 | SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, |
| 403 | SND_SOC_NOPM, 0, 0), |
| 404 | SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, |
| 405 | SND_SOC_NOPM, 0, 0), |
| 406 | SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, |
| 407 | SND_SOC_NOPM, 0, 0), |
| 408 | SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, |
| 409 | SND_SOC_NOPM, 0, 0), |
| 410 | |
| 411 | /* Input Pins */ |
| 412 | SND_SOC_DAPM_INPUT("Codec Input Pin1"), |
| 413 | SND_SOC_DAPM_INPUT("Codec Input Pin2"), |
| 414 | SND_SOC_DAPM_INPUT("Codec Input Pin3"), |
| 415 | |
| 416 | /* Output Pins */ |
| 417 | SND_SOC_DAPM_OUTPUT("Codec Output Pin1"), |
| 418 | SND_SOC_DAPM_OUTPUT("Codec Output Pin2"), |
| 419 | SND_SOC_DAPM_OUTPUT("Codec Output Pin3"), |
| 420 | }; |
| 421 | |
| 422 | static const struct snd_soc_component_driver hdac_hda_codec = { |
| 423 | .probe = hdac_hda_codec_probe, |
| 424 | .remove = hdac_hda_codec_remove, |
| 425 | .idle_bias_on = false, |
| 426 | .dapm_widgets = hdac_hda_dapm_widgets, |
| 427 | .num_dapm_widgets = ARRAY_SIZE(hdac_hda_dapm_widgets), |
| 428 | .dapm_routes = hdac_hda_dapm_routes, |
| 429 | .num_dapm_routes = ARRAY_SIZE(hdac_hda_dapm_routes), |
| 430 | }; |
| 431 | |
| 432 | static int hdac_hda_dev_probe(struct hdac_device *hdev) |
| 433 | { |
| 434 | struct hdac_ext_link *hlink; |
| 435 | struct hdac_hda_priv *hda_pvt; |
| 436 | int ret; |
| 437 | |
| 438 | /* hold the ref while we probe */ |
| 439 | hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev)); |
| 440 | if (!hlink) { |
| 441 | dev_err(&hdev->dev, "hdac link not found\n"); |
| 442 | return -EIO; |
| 443 | } |
| 444 | snd_hdac_ext_bus_link_get(hdev->bus, hlink); |
| 445 | |
| 446 | hda_pvt = hdac_to_hda_priv(hdev); |
| 447 | if (!hda_pvt) |
| 448 | return -ENOMEM; |
| 449 | |
| 450 | /* ASoC specific initialization */ |
| 451 | ret = snd_soc_register_component(&hdev->dev, |
| 452 | &hdac_hda_codec, hdac_hda_dais, |
| 453 | ARRAY_SIZE(hdac_hda_dais)); |
| 454 | if (ret < 0) { |
| 455 | dev_err(&hdev->dev, "failed to register HDA codec %d\n", ret); |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | dev_set_drvdata(&hdev->dev, hda_pvt); |
| 460 | snd_hdac_ext_bus_link_put(hdev->bus, hlink); |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | static int hdac_hda_dev_remove(struct hdac_device *hdev) |
| 466 | { |
| 467 | snd_soc_unregister_component(&hdev->dev); |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static struct hdac_ext_bus_ops hdac_ops = { |
| 472 | .hdev_attach = hdac_hda_dev_probe, |
| 473 | .hdev_detach = hdac_hda_dev_remove, |
| 474 | }; |
| 475 | |
| 476 | struct hdac_ext_bus_ops *snd_soc_hdac_hda_get_ops(void) |
| 477 | { |
| 478 | return &hdac_ops; |
| 479 | } |
| 480 | EXPORT_SYMBOL_GPL(snd_soc_hdac_hda_get_ops); |
| 481 | |
| 482 | MODULE_LICENSE("GPL v2"); |
| 483 | MODULE_DESCRIPTION("ASoC Extensions for legacy HDA Drivers"); |
| 484 | MODULE_AUTHOR("Rakesh Ughreja<rakesh.a.ughreja@intel.com>"); |