blob: 8d65004c917a1941d9e65d27bf5fdb3e29aadb89 [file] [log] [blame]
Olivier Deprez157378f2022-04-04 15:47:50 +02001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
David Brazdil0f672f62019-12-10 10:32:29 +00002//
3// This file is provided under a dual BSD/GPLv2 license. When using or
4// redistributing this file, you may do so under either license.
5//
6// Copyright(c) 2018 Intel Corporation. All rights reserved.
7//
8// Authors: Keyon Jie <yang.jie@linux.intel.com>
9//
10
11#include <linux/module.h>
12#include <sound/hdaudio_ext.h>
13#include <sound/hda_register.h>
14#include <sound/hda_codec.h>
15#include <sound/hda_i915.h>
16#include <sound/sof.h>
17#include "../ops.h"
18#include "hda.h"
19#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
20#include "../../codecs/hdac_hda.h"
21#endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
22
23#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
24#define IDISP_VID_INTEL 0x80860000
25
26/* load the legacy HDA codec driver */
Olivier Deprez157378f2022-04-04 15:47:50 +020027static int request_codec_module(struct hda_codec *codec)
David Brazdil0f672f62019-12-10 10:32:29 +000028{
Olivier Deprez0e641232021-09-23 10:07:05 +020029#ifdef MODULE
David Brazdil0f672f62019-12-10 10:32:29 +000030 char alias[MODULE_NAME_LEN];
Olivier Deprez157378f2022-04-04 15:47:50 +020031 const char *mod = NULL;
David Brazdil0f672f62019-12-10 10:32:29 +000032
Olivier Deprez157378f2022-04-04 15:47:50 +020033 switch (codec->probe_id) {
34 case HDA_CODEC_ID_GENERIC:
35#if IS_MODULE(CONFIG_SND_HDA_GENERIC)
36 mod = "snd-hda-codec-generic";
David Brazdil0f672f62019-12-10 10:32:29 +000037#endif
Olivier Deprez157378f2022-04-04 15:47:50 +020038 break;
39 default:
40 snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
41 mod = alias;
42 break;
43 }
44
45 if (mod) {
46 dev_dbg(&codec->core.dev, "loading codec module: %s\n", mod);
47 request_module(mod);
48 }
49#endif /* MODULE */
Olivier Deprez0e641232021-09-23 10:07:05 +020050 return device_attach(hda_codec_dev(codec));
51}
David Brazdil0f672f62019-12-10 10:32:29 +000052
Olivier Deprez157378f2022-04-04 15:47:50 +020053static int hda_codec_load_module(struct hda_codec *codec)
54{
55 int ret = request_codec_module(codec);
56
57 if (ret <= 0) {
58 codec->probe_id = HDA_CODEC_ID_GENERIC;
59 ret = request_codec_module(codec);
60 }
61
62 return ret;
63}
64
David Brazdil0f672f62019-12-10 10:32:29 +000065/* enable controller wake up event for all codecs with jack connectors */
66void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev)
67{
68 struct hda_bus *hbus = sof_to_hbus(sdev);
69 struct hdac_bus *bus = sof_to_bus(sdev);
70 struct hda_codec *codec;
71 unsigned int mask = 0;
72
73 list_for_each_codec(codec, hbus)
74 if (codec->jacktbl.used)
75 mask |= BIT(codec->core.addr);
76
77 snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
78}
79
80/* check jack status after resuming from suspend mode */
81void hda_codec_jack_check(struct snd_sof_dev *sdev)
82{
83 struct hda_bus *hbus = sof_to_hbus(sdev);
84 struct hdac_bus *bus = sof_to_bus(sdev);
85 struct hda_codec *codec;
86
87 /* disable controller Wake Up event*/
88 snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
89
90 list_for_each_codec(codec, hbus)
91 /*
92 * Wake up all jack-detecting codecs regardless whether an event
93 * has been recorded in STATESTS
94 */
95 if (codec->jacktbl.used)
Olivier Deprez0e641232021-09-23 10:07:05 +020096 pm_request_resume(&codec->core.dev);
David Brazdil0f672f62019-12-10 10:32:29 +000097}
98#else
99void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev) {}
100void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
101#endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
Olivier Deprez157378f2022-04-04 15:47:50 +0200102EXPORT_SYMBOL_NS(hda_codec_jack_wake_enable, SND_SOC_SOF_HDA_AUDIO_CODEC);
103EXPORT_SYMBOL_NS(hda_codec_jack_check, SND_SOC_SOF_HDA_AUDIO_CODEC);
104
105#if IS_ENABLED(CONFIG_SND_HDA_GENERIC)
106#define is_generic_config(bus) \
107 ((bus)->modelname && !strcmp((bus)->modelname, "generic"))
108#else
109#define is_generic_config(x) 0
110#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000111
112/* probe individual codec */
Olivier Deprez157378f2022-04-04 15:47:50 +0200113static int hda_codec_probe(struct snd_sof_dev *sdev, int address,
114 bool hda_codec_use_common_hdmi)
David Brazdil0f672f62019-12-10 10:32:29 +0000115{
116#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
117 struct hdac_hda_priv *hda_priv;
Olivier Deprez157378f2022-04-04 15:47:50 +0200118 struct hda_codec *codec;
119 int type = HDA_DEV_LEGACY;
David Brazdil0f672f62019-12-10 10:32:29 +0000120#endif
121 struct hda_bus *hbus = sof_to_hbus(sdev);
122 struct hdac_device *hdev;
123 u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
124 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
125 u32 resp = -1;
126 int ret;
127
128 mutex_lock(&hbus->core.cmd_mutex);
129 snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
130 snd_hdac_bus_get_response(&hbus->core, address, &resp);
131 mutex_unlock(&hbus->core.cmd_mutex);
132 if (resp == -1)
133 return -EIO;
134 dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
135 address, resp);
136
137#if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
138 hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
139 if (!hda_priv)
140 return -ENOMEM;
141
142 hda_priv->codec.bus = hbus;
143 hdev = &hda_priv->codec.core;
Olivier Deprez157378f2022-04-04 15:47:50 +0200144 codec = &hda_priv->codec;
David Brazdil0f672f62019-12-10 10:32:29 +0000145
Olivier Deprez157378f2022-04-04 15:47:50 +0200146 /* only probe ASoC codec drivers for HDAC-HDMI */
147 if (!hda_codec_use_common_hdmi && (resp & 0xFFFF0000) == IDISP_VID_INTEL)
148 type = HDA_DEV_ASOC;
149
150 ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, type);
David Brazdil0f672f62019-12-10 10:32:29 +0000151 if (ret < 0)
152 return ret;
153
Olivier Deprez157378f2022-04-04 15:47:50 +0200154 if ((resp & 0xFFFF0000) == IDISP_VID_INTEL) {
155 if (!hdev->bus->audio_component) {
156 dev_dbg(sdev->dev,
157 "iDisp hw present but no driver\n");
158 ret = -ENOENT;
159 goto out;
160 }
161 hda_priv->need_display_power = true;
162 }
163
164 if (is_generic_config(hbus))
165 codec->probe_id = HDA_CODEC_ID_GENERIC;
166 else
167 codec->probe_id = 0;
168
169 if (type == HDA_DEV_LEGACY) {
170 ret = hda_codec_load_module(codec);
Olivier Deprez0e641232021-09-23 10:07:05 +0200171 /*
172 * handle ret==0 (no driver bound) as an error, but pass
173 * other return codes without modification
174 */
175 if (ret == 0)
176 ret = -ENOENT;
David Brazdil0f672f62019-12-10 10:32:29 +0000177 }
178
Olivier Deprez157378f2022-04-04 15:47:50 +0200179out:
180 if (ret < 0) {
181 snd_hdac_device_unregister(hdev);
182 put_device(&hdev->dev);
183 }
David Brazdil0f672f62019-12-10 10:32:29 +0000184#else
185 hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
186 if (!hdev)
187 return -ENOMEM;
188
Olivier Deprez157378f2022-04-04 15:47:50 +0200189 ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev, HDA_DEV_ASOC);
190#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000191
192 return ret;
David Brazdil0f672f62019-12-10 10:32:29 +0000193}
194
195/* Codec initialization */
Olivier Deprez157378f2022-04-04 15:47:50 +0200196void hda_codec_probe_bus(struct snd_sof_dev *sdev,
197 bool hda_codec_use_common_hdmi)
David Brazdil0f672f62019-12-10 10:32:29 +0000198{
199 struct hdac_bus *bus = sof_to_bus(sdev);
200 int i, ret;
201
202 /* probe codecs in avail slots */
203 for (i = 0; i < HDA_MAX_CODECS; i++) {
204
205 if (!(bus->codec_mask & (1 << i)))
206 continue;
207
Olivier Deprez157378f2022-04-04 15:47:50 +0200208 ret = hda_codec_probe(sdev, i, hda_codec_use_common_hdmi);
David Brazdil0f672f62019-12-10 10:32:29 +0000209 if (ret < 0) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200210 dev_warn(bus->dev, "codec #%d probe error, ret: %d\n",
211 i, ret);
212 bus->codec_mask &= ~BIT(i);
David Brazdil0f672f62019-12-10 10:32:29 +0000213 }
214 }
David Brazdil0f672f62019-12-10 10:32:29 +0000215}
Olivier Deprez157378f2022-04-04 15:47:50 +0200216EXPORT_SYMBOL_NS(hda_codec_probe_bus, SND_SOC_SOF_HDA_AUDIO_CODEC);
David Brazdil0f672f62019-12-10 10:32:29 +0000217
Olivier Deprez157378f2022-04-04 15:47:50 +0200218#if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
219 IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
David Brazdil0f672f62019-12-10 10:32:29 +0000220
Olivier Deprez157378f2022-04-04 15:47:50 +0200221void hda_codec_i915_display_power(struct snd_sof_dev *sdev, bool enable)
David Brazdil0f672f62019-12-10 10:32:29 +0000222{
223 struct hdac_bus *bus = sof_to_bus(sdev);
224
Olivier Deprez157378f2022-04-04 15:47:50 +0200225 if (HDA_IDISP_CODEC(bus->codec_mask)) {
226 dev_dbg(bus->dev, "Turning i915 HDAC power %d\n", enable);
227 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, enable);
228 }
David Brazdil0f672f62019-12-10 10:32:29 +0000229}
Olivier Deprez157378f2022-04-04 15:47:50 +0200230EXPORT_SYMBOL_NS(hda_codec_i915_display_power, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
David Brazdil0f672f62019-12-10 10:32:29 +0000231
232int hda_codec_i915_init(struct snd_sof_dev *sdev)
233{
234 struct hdac_bus *bus = sof_to_bus(sdev);
235 int ret;
236
237 /* i915 exposes a HDA codec for HDMI audio */
238 ret = snd_hdac_i915_init(bus);
239 if (ret < 0)
240 return ret;
241
Olivier Deprez157378f2022-04-04 15:47:50 +0200242 /* codec_mask not yet known, power up for probe */
243 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
David Brazdil0f672f62019-12-10 10:32:29 +0000244
245 return 0;
246}
Olivier Deprez157378f2022-04-04 15:47:50 +0200247EXPORT_SYMBOL_NS(hda_codec_i915_init, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
David Brazdil0f672f62019-12-10 10:32:29 +0000248
249int hda_codec_i915_exit(struct snd_sof_dev *sdev)
250{
251 struct hdac_bus *bus = sof_to_bus(sdev);
David Brazdil0f672f62019-12-10 10:32:29 +0000252
Olivier Deprez157378f2022-04-04 15:47:50 +0200253 if (!bus->audio_component)
254 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000255
Olivier Deprez157378f2022-04-04 15:47:50 +0200256 /* power down unconditionally */
257 snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
David Brazdil0f672f62019-12-10 10:32:29 +0000258
Olivier Deprez157378f2022-04-04 15:47:50 +0200259 return snd_hdac_i915_exit(bus);
David Brazdil0f672f62019-12-10 10:32:29 +0000260}
Olivier Deprez157378f2022-04-04 15:47:50 +0200261EXPORT_SYMBOL_NS(hda_codec_i915_exit, SND_SOC_SOF_HDA_AUDIO_CODEC_I915);
David Brazdil0f672f62019-12-10 10:32:29 +0000262
Olivier Deprez157378f2022-04-04 15:47:50 +0200263#endif
David Brazdil0f672f62019-12-10 10:32:29 +0000264
265MODULE_LICENSE("Dual BSD/GPL");