Loading...
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4//
5// Author: Cezary Rojewski <cezary.rojewski@intel.com>
6//
7
8#include <linux/module.h>
9#include <linux/pm_runtime.h>
10#include <sound/soc.h>
11#include <sound/hdaudio_ext.h>
12#include <sound/hda_i915.h>
13#include <sound/hda_codec.h>
14#include "hda.h"
15
16static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
17 struct snd_soc_dai_driver **drivers)
18{
19 struct device *dev = &codec->core.dev;
20 struct snd_soc_dai_driver *drvs;
21 struct hda_pcm *pcm;
22 int i;
23
24 drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
25 if (!drvs)
26 return -ENOMEM;
27
28 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
29
30 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
31 struct snd_soc_pcm_stream *stream;
32 int dir;
33
34 dev_info(dev, "creating for %s %d\n", pcm->name, i);
35 drvs[i].id = i;
36 drvs[i].name = pcm->name;
37 drvs[i].ops = &snd_soc_hda_codec_dai_ops;
38
39 dir = SNDRV_PCM_STREAM_PLAYBACK;
40 stream = &drvs[i].playback;
41 if (!pcm->stream[dir].substreams) {
42 dev_info(dev, "skipping playback dai for %s\n", pcm->name);
43 goto capture_dais;
44 }
45
46 stream->stream_name =
47 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
48 snd_pcm_direction_name(dir));
49 if (!stream->stream_name)
50 return -ENOMEM;
51 stream->channels_min = pcm->stream[dir].channels_min;
52 stream->channels_max = pcm->stream[dir].channels_max;
53 stream->rates = pcm->stream[dir].rates;
54 stream->formats = pcm->stream[dir].formats;
55 stream->sig_bits = pcm->stream[dir].maxbps;
56
57capture_dais:
58 dir = SNDRV_PCM_STREAM_CAPTURE;
59 stream = &drvs[i].capture;
60 if (!pcm->stream[dir].substreams) {
61 dev_info(dev, "skipping capture dai for %s\n", pcm->name);
62 continue;
63 }
64
65 stream->stream_name =
66 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
67 snd_pcm_direction_name(dir));
68 if (!stream->stream_name)
69 return -ENOMEM;
70 stream->channels_min = pcm->stream[dir].channels_min;
71 stream->channels_max = pcm->stream[dir].channels_max;
72 stream->rates = pcm->stream[dir].rates;
73 stream->formats = pcm->stream[dir].formats;
74 stream->sig_bits = pcm->stream[dir].maxbps;
75 }
76
77 *drivers = drvs;
78 return 0;
79}
80
81static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
82{
83 struct snd_soc_dai_driver *drvs = NULL;
84 struct snd_soc_dapm_context *dapm;
85 struct hda_pcm *pcm;
86 int ret, pcm_count = 0;
87
88 if (list_empty(&codec->pcm_list_head))
89 return -EINVAL;
90 list_for_each_entry(pcm, &codec->pcm_list_head, list)
91 pcm_count++;
92
93 ret = hda_codec_create_dais(codec, pcm_count, &drvs);
94 if (ret < 0)
95 return ret;
96
97 dapm = snd_soc_component_get_dapm(component);
98
99 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
100 struct snd_soc_dai *dai;
101
102 dai = snd_soc_register_dai(component, drvs, false);
103 if (!dai) {
104 dev_err(component->dev, "register dai for %s failed\n", pcm->name);
105 return -EINVAL;
106 }
107
108 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
109 if (ret < 0) {
110 dev_err(component->dev, "create widgets failed: %d\n", ret);
111 snd_soc_unregister_dai(dai);
112 return ret;
113 }
114
115 snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
116 drvs++;
117 }
118
119 return 0;
120}
121
122static void hda_codec_unregister_dais(struct hda_codec *codec,
123 struct snd_soc_component *component)
124{
125 struct snd_soc_dai *dai, *save;
126 struct hda_pcm *pcm;
127
128 for_each_component_dais_safe(component, dai, save) {
129 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
130 if (strcmp(dai->driver->name, pcm->name))
131 continue;
132
133 snd_soc_dapm_free_widget(dai->playback_widget);
134 snd_soc_dapm_free_widget(dai->capture_widget);
135 snd_soc_unregister_dai(dai);
136 break;
137 }
138 }
139}
140
141int hda_codec_probe_complete(struct hda_codec *codec)
142{
143 struct hdac_device *hdev = &codec->core;
144 struct hdac_bus *bus = hdev->bus;
145 int ret;
146
147 ret = snd_hda_codec_build_controls(codec);
148 if (ret < 0) {
149 dev_err(&hdev->dev, "unable to create controls %d\n", ret);
150 goto out;
151 }
152
153 /* Bus suspended codecs as it does not manage their pm */
154 pm_runtime_set_active(&hdev->dev);
155 /* rpm was forbidden in snd_hda_codec_device_new() */
156 snd_hda_codec_set_power_save(codec, 2000);
157 snd_hda_codec_register(codec);
158out:
159 /* Complement pm_runtime_get_sync(bus) in probe */
160 pm_runtime_mark_last_busy(bus->dev);
161 pm_runtime_put_autosuspend(bus->dev);
162
163 return ret;
164}
165EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
166
167/* Expects codec with usage_count=1 and status=suspended */
168static int hda_codec_probe(struct snd_soc_component *component)
169{
170 struct hda_codec *codec = dev_to_hda_codec(component->dev);
171 struct hdac_device *hdev = &codec->core;
172 struct hdac_bus *bus = hdev->bus;
173 struct hdac_ext_link *hlink;
174 hda_codec_patch_t patch;
175 int ret;
176
177#ifdef CONFIG_PM
178 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
179 !pm_runtime_status_suspended(&hdev->dev));
180#endif
181
182 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
183 if (!hlink) {
184 dev_err(&hdev->dev, "hdac link not found\n");
185 return -EIO;
186 }
187
188 pm_runtime_get_sync(bus->dev);
189 if (hda_codec_is_display(codec))
190 snd_hdac_display_power(bus, hdev->addr, true);
191 snd_hdac_ext_bus_link_get(bus, hlink);
192
193 ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
194 false);
195 if (ret < 0) {
196 dev_err(&hdev->dev, "create hda codec failed: %d\n", ret);
197 goto device_new_err;
198 }
199
200 ret = snd_hda_codec_set_name(codec, codec->preset->name);
201 if (ret < 0) {
202 dev_err(&hdev->dev, "name failed %s\n", codec->preset->name);
203 goto err;
204 }
205
206 ret = snd_hdac_regmap_init(&codec->core);
207 if (ret < 0) {
208 dev_err(&hdev->dev, "regmap init failed\n");
209 goto err;
210 }
211
212 patch = (hda_codec_patch_t)codec->preset->driver_data;
213 if (!patch) {
214 dev_err(&hdev->dev, "no patch specified\n");
215 ret = -EINVAL;
216 goto err;
217 }
218
219 ret = patch(codec);
220 if (ret < 0) {
221 dev_err(&hdev->dev, "patch failed %d\n", ret);
222 goto err;
223 }
224
225 ret = snd_hda_codec_parse_pcms(codec);
226 if (ret < 0) {
227 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
228 goto parse_pcms_err;
229 }
230
231 ret = hda_codec_register_dais(codec, component);
232 if (ret < 0) {
233 dev_err(&hdev->dev, "update dais failed: %d\n", ret);
234 goto parse_pcms_err;
235 }
236
237 if (!hda_codec_is_display(codec)) {
238 ret = hda_codec_probe_complete(codec);
239 if (ret < 0)
240 goto complete_err;
241 }
242
243 codec->core.lazy_cache = true;
244
245 return 0;
246
247complete_err:
248 hda_codec_unregister_dais(codec, component);
249parse_pcms_err:
250 if (codec->patch_ops.free)
251 codec->patch_ops.free(codec);
252err:
253 snd_hda_codec_cleanup_for_unbind(codec);
254device_new_err:
255 if (hda_codec_is_display(codec))
256 snd_hdac_display_power(bus, hdev->addr, false);
257
258 snd_hdac_ext_bus_link_put(bus, hlink);
259
260 pm_runtime_mark_last_busy(bus->dev);
261 pm_runtime_put_autosuspend(bus->dev);
262 return ret;
263}
264
265/* Leaves codec with usage_count=1 and status=suspended */
266static void hda_codec_remove(struct snd_soc_component *component)
267{
268 struct hda_codec *codec = dev_to_hda_codec(component->dev);
269 struct hdac_device *hdev = &codec->core;
270 struct hdac_bus *bus = hdev->bus;
271 struct hdac_ext_link *hlink;
272 bool was_registered = codec->core.registered;
273
274 /* Don't allow any more runtime suspends */
275 pm_runtime_forbid(&hdev->dev);
276
277 hda_codec_unregister_dais(codec, component);
278
279 if (codec->patch_ops.free)
280 codec->patch_ops.free(codec);
281
282 snd_hda_codec_cleanup_for_unbind(codec);
283 pm_runtime_put_noidle(&hdev->dev);
284 /* snd_hdac_device_exit() is only called on bus remove */
285 pm_runtime_set_suspended(&hdev->dev);
286
287 if (hda_codec_is_display(codec))
288 snd_hdac_display_power(bus, hdev->addr, false);
289
290 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
291 if (hlink)
292 snd_hdac_ext_bus_link_put(bus, hlink);
293 /*
294 * HDMI card's hda_codec_probe_complete() (see late_probe()) may
295 * not be called due to early error, leaving bus uc unbalanced
296 */
297 if (!was_registered) {
298 pm_runtime_mark_last_busy(bus->dev);
299 pm_runtime_put_autosuspend(bus->dev);
300 }
301
302#ifdef CONFIG_PM
303 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
304 !pm_runtime_status_suspended(&hdev->dev));
305#endif
306}
307
308static const struct snd_soc_dapm_route hda_dapm_routes[] = {
309 {"AIF1TX", NULL, "Codec Input Pin1"},
310 {"AIF2TX", NULL, "Codec Input Pin2"},
311 {"AIF3TX", NULL, "Codec Input Pin3"},
312
313 {"Codec Output Pin1", NULL, "AIF1RX"},
314 {"Codec Output Pin2", NULL, "AIF2RX"},
315 {"Codec Output Pin3", NULL, "AIF3RX"},
316};
317
318static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
319 /* Audio Interface */
320 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
321 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
322 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
323 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
324 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
325 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
326
327 /* Input Pins */
328 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
329 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
330 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
331
332 /* Output Pins */
333 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
334 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
335 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
336};
337
338static struct snd_soc_dai_driver card_binder_dai = {
339 .id = -1,
340 .name = "codec-probing-DAI",
341};
342
343static int hda_hdev_attach(struct hdac_device *hdev)
344{
345 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
346 struct snd_soc_component_driver *comp_drv;
347
348 comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
349 if (!comp_drv)
350 return -ENOMEM;
351
352 /*
353 * It's save to rely on dev_name() rather than a copy as component
354 * driver's lifetime is directly tied to hda codec one
355 */
356 comp_drv->name = dev_name(&hdev->dev);
357 comp_drv->probe = hda_codec_probe;
358 comp_drv->remove = hda_codec_remove;
359 comp_drv->idle_bias_on = false;
360 if (!hda_codec_is_display(codec)) {
361 comp_drv->dapm_widgets = hda_dapm_widgets;
362 comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
363 comp_drv->dapm_routes = hda_dapm_routes;
364 comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
365 }
366
367 return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
368}
369
370static int hda_hdev_detach(struct hdac_device *hdev)
371{
372 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
373
374 if (codec->core.registered)
375 cancel_delayed_work_sync(&codec->jackpoll_work);
376
377 snd_soc_unregister_component(&hdev->dev);
378
379 return 0;
380}
381
382const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
383 .hdev_attach = hda_hdev_attach,
384 .hdev_detach = hda_hdev_detach,
385};
386EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
387
388MODULE_DESCRIPTION("HD-Audio codec driver");
389MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
390MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4//
5// Author: Cezary Rojewski <cezary.rojewski@intel.com>
6//
7
8#include <linux/module.h>
9#include <linux/pm_runtime.h>
10#include <sound/soc.h>
11#include <sound/hdaudio_ext.h>
12#include <sound/hda_i915.h>
13#include <sound/hda_codec.h>
14#include "hda.h"
15
16static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
17 struct snd_soc_dai_driver **drivers)
18{
19 struct device *dev = &codec->core.dev;
20 struct snd_soc_dai_driver *drvs;
21 struct hda_pcm *pcm;
22 int i;
23
24 drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
25 if (!drvs)
26 return -ENOMEM;
27
28 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
29
30 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
31 struct snd_soc_pcm_stream *stream;
32 int dir;
33
34 dev_info(dev, "creating for %s %d\n", pcm->name, i);
35 drvs[i].id = i;
36 drvs[i].name = pcm->name;
37 drvs[i].ops = &snd_soc_hda_codec_dai_ops;
38
39 dir = SNDRV_PCM_STREAM_PLAYBACK;
40 stream = &drvs[i].playback;
41 if (!pcm->stream[dir].substreams) {
42 dev_info(dev, "skipping playback dai for %s\n", pcm->name);
43 goto capture_dais;
44 }
45
46 stream->stream_name =
47 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
48 snd_pcm_direction_name(dir));
49 if (!stream->stream_name)
50 return -ENOMEM;
51 stream->channels_min = pcm->stream[dir].channels_min;
52 stream->channels_max = pcm->stream[dir].channels_max;
53 stream->rates = pcm->stream[dir].rates;
54 stream->formats = pcm->stream[dir].formats;
55 stream->subformats = pcm->stream[dir].subformats;
56 stream->sig_bits = pcm->stream[dir].maxbps;
57
58capture_dais:
59 dir = SNDRV_PCM_STREAM_CAPTURE;
60 stream = &drvs[i].capture;
61 if (!pcm->stream[dir].substreams) {
62 dev_info(dev, "skipping capture dai for %s\n", pcm->name);
63 continue;
64 }
65
66 stream->stream_name =
67 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
68 snd_pcm_direction_name(dir));
69 if (!stream->stream_name)
70 return -ENOMEM;
71 stream->channels_min = pcm->stream[dir].channels_min;
72 stream->channels_max = pcm->stream[dir].channels_max;
73 stream->rates = pcm->stream[dir].rates;
74 stream->formats = pcm->stream[dir].formats;
75 stream->subformats = pcm->stream[dir].subformats;
76 stream->sig_bits = pcm->stream[dir].maxbps;
77 }
78
79 *drivers = drvs;
80 return 0;
81}
82
83static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
84{
85 struct snd_soc_dai_driver *drvs = NULL;
86 struct snd_soc_dapm_context *dapm;
87 struct hda_pcm *pcm;
88 int ret, pcm_count = 0;
89
90 if (list_empty(&codec->pcm_list_head))
91 return -EINVAL;
92 list_for_each_entry(pcm, &codec->pcm_list_head, list)
93 pcm_count++;
94
95 ret = hda_codec_create_dais(codec, pcm_count, &drvs);
96 if (ret < 0)
97 return ret;
98
99 dapm = snd_soc_component_get_dapm(component);
100
101 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
102 struct snd_soc_dai *dai;
103
104 dai = snd_soc_register_dai(component, drvs, false);
105 if (!dai) {
106 dev_err(component->dev, "register dai for %s failed\n", pcm->name);
107 return -EINVAL;
108 }
109
110 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
111 if (ret < 0) {
112 dev_err(component->dev, "create widgets failed: %d\n", ret);
113 snd_soc_unregister_dai(dai);
114 return ret;
115 }
116
117 snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
118 drvs++;
119 }
120
121 return 0;
122}
123
124static void hda_codec_unregister_dais(struct hda_codec *codec,
125 struct snd_soc_component *component)
126{
127 struct snd_soc_dai *dai, *save;
128 struct hda_pcm *pcm;
129
130 for_each_component_dais_safe(component, dai, save) {
131 int stream;
132
133 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
134 if (strcmp(dai->driver->name, pcm->name))
135 continue;
136
137 for_each_pcm_streams(stream)
138 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
139
140 snd_soc_unregister_dai(dai);
141 break;
142 }
143 }
144}
145
146int hda_codec_probe_complete(struct hda_codec *codec)
147{
148 struct hdac_device *hdev = &codec->core;
149 struct hdac_bus *bus = hdev->bus;
150 int ret;
151
152 ret = snd_hda_codec_build_controls(codec);
153 if (ret < 0) {
154 dev_err(&hdev->dev, "unable to create controls %d\n", ret);
155 goto out;
156 }
157
158 /* Bus suspended codecs as it does not manage their pm */
159 pm_runtime_set_active(&hdev->dev);
160 /* rpm was forbidden in snd_hda_codec_device_new() */
161 snd_hda_codec_set_power_save(codec, 2000);
162 snd_hda_codec_register(codec);
163out:
164 /* Complement pm_runtime_get_sync(bus) in probe */
165 pm_runtime_mark_last_busy(bus->dev);
166 pm_runtime_put_autosuspend(bus->dev);
167
168 return ret;
169}
170EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
171
172/* Expects codec with usage_count=1 and status=suspended */
173static int hda_codec_probe(struct snd_soc_component *component)
174{
175 struct hda_codec *codec = dev_to_hda_codec(component->dev);
176 struct hdac_device *hdev = &codec->core;
177 struct hdac_bus *bus = hdev->bus;
178 struct hdac_ext_link *hlink;
179 hda_codec_patch_t patch;
180 int ret;
181
182#ifdef CONFIG_PM
183 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
184 !pm_runtime_status_suspended(&hdev->dev));
185#endif
186
187 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
188 if (!hlink) {
189 dev_err(&hdev->dev, "hdac link not found\n");
190 return -EIO;
191 }
192
193 pm_runtime_get_sync(bus->dev);
194 if (hda_codec_is_display(codec))
195 snd_hdac_display_power(bus, hdev->addr, true);
196 snd_hdac_ext_bus_link_get(bus, hlink);
197
198 ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
199 false);
200 if (ret < 0) {
201 dev_err(&hdev->dev, "codec create failed: %d\n", ret);
202 goto device_new_err;
203 }
204
205 ret = snd_hda_codec_set_name(codec, codec->preset->name);
206 if (ret < 0) {
207 dev_err(&hdev->dev, "set name: %s failed: %d\n", codec->preset->name, ret);
208 goto err;
209 }
210
211 ret = snd_hdac_regmap_init(&codec->core);
212 if (ret < 0) {
213 dev_err(&hdev->dev, "regmap init failed: %d\n", ret);
214 goto err;
215 }
216
217 patch = (hda_codec_patch_t)codec->preset->driver_data;
218 if (!patch) {
219 dev_err(&hdev->dev, "no patch specified\n");
220 ret = -EINVAL;
221 goto err;
222 }
223
224 ret = patch(codec);
225 if (ret < 0) {
226 dev_err(&hdev->dev, "codec init failed: %d\n", ret);
227 goto err;
228 }
229
230 ret = snd_hda_codec_parse_pcms(codec);
231 if (ret < 0) {
232 dev_err(&hdev->dev, "unable to map pcms to dai: %d\n", ret);
233 goto parse_pcms_err;
234 }
235
236 ret = hda_codec_register_dais(codec, component);
237 if (ret < 0) {
238 dev_err(&hdev->dev, "update dais failed: %d\n", ret);
239 goto parse_pcms_err;
240 }
241
242 if (!hda_codec_is_display(codec)) {
243 ret = hda_codec_probe_complete(codec);
244 if (ret < 0)
245 goto complete_err;
246 }
247
248 codec->core.lazy_cache = true;
249
250 return 0;
251
252complete_err:
253 hda_codec_unregister_dais(codec, component);
254parse_pcms_err:
255 if (codec->patch_ops.free)
256 codec->patch_ops.free(codec);
257err:
258 snd_hda_codec_cleanup_for_unbind(codec);
259device_new_err:
260 if (hda_codec_is_display(codec))
261 snd_hdac_display_power(bus, hdev->addr, false);
262
263 snd_hdac_ext_bus_link_put(bus, hlink);
264
265 pm_runtime_mark_last_busy(bus->dev);
266 pm_runtime_put_autosuspend(bus->dev);
267 return ret;
268}
269
270/* Leaves codec with usage_count=1 and status=suspended */
271static void hda_codec_remove(struct snd_soc_component *component)
272{
273 struct hda_codec *codec = dev_to_hda_codec(component->dev);
274 struct hdac_device *hdev = &codec->core;
275 struct hdac_bus *bus = hdev->bus;
276 struct hdac_ext_link *hlink;
277 bool was_registered = codec->core.registered;
278
279 /* Don't allow any more runtime suspends */
280 pm_runtime_forbid(&hdev->dev);
281
282 hda_codec_unregister_dais(codec, component);
283
284 if (codec->patch_ops.free)
285 codec->patch_ops.free(codec);
286
287 snd_hda_codec_cleanup_for_unbind(codec);
288 pm_runtime_put_noidle(&hdev->dev);
289 /* snd_hdac_device_exit() is only called on bus remove */
290 pm_runtime_set_suspended(&hdev->dev);
291
292 if (hda_codec_is_display(codec))
293 snd_hdac_display_power(bus, hdev->addr, false);
294
295 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
296 if (hlink)
297 snd_hdac_ext_bus_link_put(bus, hlink);
298 /*
299 * HDMI card's hda_codec_probe_complete() (see late_probe()) may
300 * not be called due to early error, leaving bus uc unbalanced
301 */
302 if (!was_registered) {
303 pm_runtime_mark_last_busy(bus->dev);
304 pm_runtime_put_autosuspend(bus->dev);
305 }
306
307#ifdef CONFIG_PM
308 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
309 !pm_runtime_status_suspended(&hdev->dev));
310#endif
311}
312
313static const struct snd_soc_dapm_route hda_dapm_routes[] = {
314 {"AIF1TX", NULL, "Codec Input Pin1"},
315 {"AIF2TX", NULL, "Codec Input Pin2"},
316 {"AIF3TX", NULL, "Codec Input Pin3"},
317
318 {"Codec Output Pin1", NULL, "AIF1RX"},
319 {"Codec Output Pin2", NULL, "AIF2RX"},
320 {"Codec Output Pin3", NULL, "AIF3RX"},
321};
322
323static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
324 /* Audio Interface */
325 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
326 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
327 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
328 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
329 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
330 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
331
332 /* Input Pins */
333 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
334 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
335 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
336
337 /* Output Pins */
338 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
339 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
340 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
341};
342
343static struct snd_soc_dai_driver card_binder_dai = {
344 .id = -1,
345 .name = "codec-probing-DAI",
346};
347
348static int hda_hdev_attach(struct hdac_device *hdev)
349{
350 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
351 struct snd_soc_component_driver *comp_drv;
352
353 if (hda_codec_is_display(codec) && !hdev->bus->audio_component) {
354 dev_dbg(&hdev->dev, "no i915, skip registration for 0x%08x\n", hdev->vendor_id);
355 return -ENODEV;
356 }
357
358 comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
359 if (!comp_drv)
360 return -ENOMEM;
361
362 /*
363 * It's save to rely on dev_name() rather than a copy as component
364 * driver's lifetime is directly tied to hda codec one
365 */
366 comp_drv->name = dev_name(&hdev->dev);
367 comp_drv->probe = hda_codec_probe;
368 comp_drv->remove = hda_codec_remove;
369 comp_drv->idle_bias_on = false;
370 if (!hda_codec_is_display(codec)) {
371 comp_drv->dapm_widgets = hda_dapm_widgets;
372 comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
373 comp_drv->dapm_routes = hda_dapm_routes;
374 comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
375 }
376
377 return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
378}
379
380static int hda_hdev_detach(struct hdac_device *hdev)
381{
382 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
383
384 if (codec->core.registered)
385 cancel_delayed_work_sync(&codec->jackpoll_work);
386
387 snd_soc_unregister_component(&hdev->dev);
388
389 return 0;
390}
391
392const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
393 .hdev_attach = hda_hdev_attach,
394 .hdev_detach = hda_hdev_detach,
395};
396EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
397
398MODULE_DESCRIPTION("HD-Audio codec driver");
399MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
400MODULE_LICENSE("GPL");