Merge tag 'hwlock-v5.5' of git://git.kernel.org/pub/scm/linux/kernel/git/andersson...
[platform/kernel/linux-rpi.git] / sound / soc / sof / intel / hda-codec.c
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         struct snd_soc_acpi_mach_params *mach_params = NULL;
88         struct snd_sof_pdata *pdata = sdev->pdata;
89 #endif
90         struct hda_bus *hbus = sof_to_hbus(sdev);
91         struct hdac_device *hdev;
92         u32 hda_cmd = (address << 28) | (AC_NODE_ROOT << 20) |
93                 (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
94         u32 resp = -1;
95         int ret;
96
97         mutex_lock(&hbus->core.cmd_mutex);
98         snd_hdac_bus_send_cmd(&hbus->core, hda_cmd);
99         snd_hdac_bus_get_response(&hbus->core, address, &resp);
100         mutex_unlock(&hbus->core.cmd_mutex);
101         if (resp == -1)
102                 return -EIO;
103         dev_dbg(sdev->dev, "HDA codec #%d probed OK: response: %x\n",
104                 address, resp);
105
106 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
107         hda_priv = devm_kzalloc(sdev->dev, sizeof(*hda_priv), GFP_KERNEL);
108         if (!hda_priv)
109                 return -ENOMEM;
110
111         hda_priv->codec.bus = hbus;
112         hdev = &hda_priv->codec.core;
113
114         ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
115         if (ret < 0)
116                 return ret;
117
118         if (pdata->machine)
119                 mach_params = (struct snd_soc_acpi_mach_params *)
120                         &pdata->machine->mach_params;
121
122         if ((resp & 0xFFFF0000) == IDISP_VID_INTEL)
123                 hda_priv->need_display_power = true;
124
125         /*
126          * if common HDMI codec driver is not used, codec load
127          * is skipped here and hdac_hdmi is used instead
128          */
129         if ((mach_params && mach_params->common_hdmi_codec_drv) ||
130             (resp & 0xFFFF0000) != IDISP_VID_INTEL) {
131                 hdev->type = HDA_DEV_LEGACY;
132                 hda_codec_load_module(&hda_priv->codec);
133         }
134
135         return 0;
136 #else
137         hdev = devm_kzalloc(sdev->dev, sizeof(*hdev), GFP_KERNEL);
138         if (!hdev)
139                 return -ENOMEM;
140
141         ret = snd_hdac_ext_bus_device_init(&hbus->core, address, hdev);
142
143         return ret;
144 #endif
145 }
146
147 /* Codec initialization */
148 int hda_codec_probe_bus(struct snd_sof_dev *sdev)
149 {
150         struct hdac_bus *bus = sof_to_bus(sdev);
151         int i, ret;
152
153         /* probe codecs in avail slots */
154         for (i = 0; i < HDA_MAX_CODECS; i++) {
155
156                 if (!(bus->codec_mask & (1 << i)))
157                         continue;
158
159                 ret = hda_codec_probe(sdev, i);
160                 if (ret < 0) {
161                         dev_err(bus->dev, "error: codec #%d probe error, ret: %d\n",
162                                 i, ret);
163                         return ret;
164                 }
165         }
166
167         return 0;
168 }
169 EXPORT_SYMBOL(hda_codec_probe_bus);
170
171 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) || \
172         IS_ENABLED(CONFIG_SND_SOC_HDAC_HDMI)
173
174 void hda_codec_i915_get(struct snd_sof_dev *sdev)
175 {
176         struct hdac_bus *bus = sof_to_bus(sdev);
177
178         dev_dbg(bus->dev, "Turning i915 HDAC power on\n");
179         snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
180 }
181 EXPORT_SYMBOL(hda_codec_i915_get);
182
183 void hda_codec_i915_put(struct snd_sof_dev *sdev)
184 {
185         struct hdac_bus *bus = sof_to_bus(sdev);
186
187         dev_dbg(bus->dev, "Turning i915 HDAC power off\n");
188         snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
189 }
190 EXPORT_SYMBOL(hda_codec_i915_put);
191
192 int hda_codec_i915_init(struct snd_sof_dev *sdev)
193 {
194         struct hdac_bus *bus = sof_to_bus(sdev);
195         int ret;
196
197         /* i915 exposes a HDA codec for HDMI audio */
198         ret = snd_hdac_i915_init(bus);
199         if (ret < 0)
200                 return ret;
201
202         hda_codec_i915_get(sdev);
203
204         return 0;
205 }
206 EXPORT_SYMBOL(hda_codec_i915_init);
207
208 int hda_codec_i915_exit(struct snd_sof_dev *sdev)
209 {
210         struct hdac_bus *bus = sof_to_bus(sdev);
211         int ret;
212
213         hda_codec_i915_put(sdev);
214
215         ret = snd_hdac_i915_exit(bus);
216
217         return ret;
218 }
219 EXPORT_SYMBOL(hda_codec_i915_exit);
220
221 #endif
222
223 MODULE_LICENSE("Dual BSD/GPL");