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