xref: /linux/sound/soc/sof/intel/hda-codec.c (revision 52338415)
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
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 */
27 #ifdef MODULE
28 static void hda_codec_load_module(struct hda_codec *codec)
29 {
30 	char alias[MODULE_NAME_LEN];
31 	const char *module = alias;
32 
33 	snd_hdac_codec_modalias(&codec->core, alias, sizeof(alias));
34 	dev_dbg(&codec->core.dev, "loading codec module: %s\n", module);
35 	request_module(module);
36 }
37 #else
38 static void hda_codec_load_module(struct hda_codec *codec) {}
39 #endif
40 
41 /* enable controller wake up event for all codecs with jack connectors */
42 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev)
43 {
44 	struct hda_bus *hbus = sof_to_hbus(sdev);
45 	struct hdac_bus *bus = sof_to_bus(sdev);
46 	struct hda_codec *codec;
47 	unsigned int mask = 0;
48 
49 	list_for_each_codec(codec, hbus)
50 		if (codec->jacktbl.used)
51 			mask |= BIT(codec->core.addr);
52 
53 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, mask);
54 }
55 
56 /* check jack status after resuming from suspend mode */
57 void hda_codec_jack_check(struct snd_sof_dev *sdev)
58 {
59 	struct hda_bus *hbus = sof_to_hbus(sdev);
60 	struct hdac_bus *bus = sof_to_bus(sdev);
61 	struct hda_codec *codec;
62 
63 	/* disable controller Wake Up event*/
64 	snd_hdac_chip_updatew(bus, WAKEEN, STATESTS_INT_MASK, 0);
65 
66 	list_for_each_codec(codec, hbus)
67 		/*
68 		 * Wake up all jack-detecting codecs regardless whether an event
69 		 * has been recorded in STATESTS
70 		 */
71 		if (codec->jacktbl.used)
72 			schedule_delayed_work(&codec->jackpoll_work,
73 					      codec->jackpoll_interval);
74 }
75 #else
76 void hda_codec_jack_wake_enable(struct snd_sof_dev *sdev) {}
77 void hda_codec_jack_check(struct snd_sof_dev *sdev) {}
78 #endif /* CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC */
79 EXPORT_SYMBOL(hda_codec_jack_wake_enable);
80 EXPORT_SYMBOL(hda_codec_jack_check);
81 
82 /* probe individual codec */
83 static int hda_codec_probe(struct snd_sof_dev *sdev, int address)
84 {
85 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
86 	struct hdac_hda_priv *hda_priv;
87 #endif
88 	struct hda_bus *hbus = sof_to_hbus(sdev);
89 	struct hdac_device *hdev;
90 	u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
91 		(AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
92 	u32 resp = -1;
93 	int ret;
94 
95 	mutex_lock(&hbus->core.cmd_mutex);
96 	snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
97 	snd_hdac_bus_get_response(&hbus->core, address, &resp);
98 	mutex_unlock(&hbus->core.cmd_mutex);
99 	if (resp == -1)
100 		return -EIO;
101 	dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
102 		address, resp);
103 
104 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
105 	hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
106 	if (!hda_priv)
107 		return -ENOMEM;
108 
109 	hda_priv->codec.bus = hbus;
110 	hdev = &hda_priv->codec.core;
111 
112 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
113 	if (ret < 0)
114 		return ret;
115 
116 	/* use legacy bus only for HDA codecs, idisp uses ext bus */
117 	if ((resp & 0xFFFF0000) != IDISP_VID_INTEL) {
118 		hdev->type = HDA_DEV_LEGACY;
119 		hda_codec_load_module(&hda_priv->codec);
120 	}
121 
122 	return 0;
123 #else
124 	hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
125 	if (!hdev)
126 		return -ENOMEM;
127 
128 	ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
129 
130 	return ret;
131 #endif
132 }
133 
134 /* Codec initialization */
135 int hda_codec_probe_bus(struct snd_sof_dev *sdev)
136 {
137 	struct hdac_bus *bus = sof_to_bus(sdev);
138 	int i, ret;
139 
140 	/* probe codecs in avail slots */
141 	for (i = 0; i < HDA_MAX_CODECS; i++) {
142 
143 		if (!(bus->codec_mask & (1 << i)))
144 			continue;
145 
146 		ret = hda_codec_probe(sdev, i);
147 		if (ret < 0) {
148 			dev_err(bus->dev, "error: codec #%d probe error, ret: %d\n",
149 				i, ret);
150 			return ret;
151 		}
152 	}
153 
154 	return 0;
155 }
156 EXPORT_SYMBOL(hda_codec_probe_bus);
157 
158 #if IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
159 
160 void hda_codec_i915_get(struct snd_sof_dev *sdev)
161 {
162 	struct hdac_bus *bus = sof_to_bus(sdev);
163 
164 	dev_dbg(bus->dev, "Turning i915 HDAC power on\n");
165 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
166 }
167 EXPORT_SYMBOL(hda_codec_i915_get);
168 
169 void hda_codec_i915_put(struct snd_sof_dev *sdev)
170 {
171 	struct hdac_bus *bus = sof_to_bus(sdev);
172 
173 	dev_dbg(bus->dev, "Turning i915 HDAC power off\n");
174 	snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
175 }
176 EXPORT_SYMBOL(hda_codec_i915_put);
177 
178 int hda_codec_i915_init(struct snd_sof_dev *sdev)
179 {
180 	struct hdac_bus *bus = sof_to_bus(sdev);
181 	int ret;
182 
183 	/* i915 exposes a HDA codec for HDMI audio */
184 	ret = snd_hdac_i915_init(bus);
185 	if (ret < 0)
186 		return ret;
187 
188 	hda_codec_i915_get(sdev);
189 
190 	return 0;
191 }
192 EXPORT_SYMBOL(hda_codec_i915_init);
193 
194 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
195 {
196 	struct hdac_bus *bus = sof_to_bus(sdev);
197 	int ret;
198 
199 	hda_codec_i915_put(sdev);
200 
201 	ret = snd_hdac_i915_exit(bus);
202 
203 	return ret;
204 }
205 EXPORT_SYMBOL(hda_codec_i915_exit);
206 
207 #endif /* CONFIG_SND_SOC_HDAC_HDMI */
208 
209 MODULE_LICENSE("Dual BSD/GPL");
210