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