Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 1 | /* |
| 2 | * hdac_i915.c - routines for sync between HD-A core and i915 display driver |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License as published by the Free |
| 6 | * Software Foundation; either version 2 of the License, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 11 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/component.h> |
| 19 | #include <drm/i915_component.h> |
| 20 | #include <sound/core.h> |
| 21 | #include <sound/hdaudio.h> |
| 22 | #include <sound/hda_i915.h> |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 23 | #include <sound/hda_register.h> |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 24 | |
| 25 | static struct i915_audio_component *hdac_acomp; |
| 26 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 27 | /** |
| 28 | * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup |
| 29 | * @bus: HDA core bus |
| 30 | * @enable: enable or disable the wakeup |
| 31 | * |
| 32 | * This function is supposed to be used only by a HD-audio controller |
| 33 | * driver that needs the interaction with i915 graphics. |
| 34 | * |
| 35 | * This function should be called during the chip reset, also called at |
| 36 | * resume for updating STATESTS register read. |
| 37 | * |
| 38 | * Returns zero for success or a negative error code. |
| 39 | */ |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 40 | int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable) |
| 41 | { |
| 42 | struct i915_audio_component *acomp = bus->audio_component; |
| 43 | |
Takashi Iwai | 692af90 | 2015-06-10 12:15:27 +0200 | [diff] [blame] | 44 | if (!acomp || !acomp->ops) |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 45 | return -ENODEV; |
| 46 | |
| 47 | if (!acomp->ops->codec_wake_override) { |
| 48 | dev_warn(bus->dev, |
| 49 | "Invalid codec wake callback\n"); |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | dev_dbg(bus->dev, "%s codec wakeup\n", |
| 54 | enable ? "enable" : "disable"); |
| 55 | |
| 56 | acomp->ops->codec_wake_override(acomp->dev, enable); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup); |
| 61 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 62 | /** |
| 63 | * snd_hdac_display_power - Power up / down the power refcount |
| 64 | * @bus: HDA core bus |
| 65 | * @enable: power up or down |
| 66 | * |
| 67 | * This function is supposed to be used only by a HD-audio controller |
| 68 | * driver that needs the interaction with i915 graphics. |
| 69 | * |
| 70 | * This function manages a refcount and calls the i915 get_power() and |
| 71 | * put_power() ops accordingly, toggling the codec wakeup, too. |
| 72 | * |
| 73 | * Returns zero for success or a negative error code. |
| 74 | */ |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 75 | int snd_hdac_display_power(struct hdac_bus *bus, bool enable) |
| 76 | { |
| 77 | struct i915_audio_component *acomp = bus->audio_component; |
| 78 | |
Takashi Iwai | 692af90 | 2015-06-10 12:15:27 +0200 | [diff] [blame] | 79 | if (!acomp || !acomp->ops) |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 80 | return -ENODEV; |
| 81 | |
| 82 | dev_dbg(bus->dev, "display power %s\n", |
| 83 | enable ? "enable" : "disable"); |
| 84 | |
| 85 | if (enable) { |
David Henningsson | 033ea34 | 2015-07-16 10:39:24 +0200 | [diff] [blame] | 86 | if (!bus->i915_power_refcount++) { |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 87 | acomp->ops->get_power(acomp->dev); |
David Henningsson | 033ea34 | 2015-07-16 10:39:24 +0200 | [diff] [blame] | 88 | snd_hdac_set_codec_wakeup(bus, true); |
| 89 | snd_hdac_set_codec_wakeup(bus, false); |
| 90 | } |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 91 | } else { |
| 92 | WARN_ON(!bus->i915_power_refcount); |
| 93 | if (!--bus->i915_power_refcount) |
| 94 | acomp->ops->put_power(acomp->dev); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | EXPORT_SYMBOL_GPL(snd_hdac_display_power); |
| 100 | |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 101 | #define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \ |
| 102 | ((pci)->device == 0x0c0c) || \ |
| 103 | ((pci)->device == 0x0d0c) || \ |
| 104 | ((pci)->device == 0x160c)) |
| 105 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 106 | /** |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 107 | * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 108 | * @bus: HDA core bus |
| 109 | * |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 110 | * Intel HSW/BDW display HDA controller is in GPU. Both its power and link BCLK |
| 111 | * depends on GPU. Two Extended Mode registers EM4 (M value) and EM5 (N Value) |
| 112 | * are used to convert CDClk (Core Display Clock) to 24MHz BCLK: |
| 113 | * BCLK = CDCLK * M / N |
| 114 | * The values will be lost when the display power well is disabled and need to |
| 115 | * be restored to avoid abnormal playback speed. |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 116 | * |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 117 | * Call this function at initializing and changing power well, as well as |
| 118 | * at ELD notifier for the hotplug. |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 119 | */ |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 120 | void snd_hdac_i915_set_bclk(struct hdac_bus *bus) |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 121 | { |
| 122 | struct i915_audio_component *acomp = bus->audio_component; |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 123 | struct pci_dev *pci = to_pci_dev(bus->dev); |
| 124 | int cdclk_freq; |
| 125 | unsigned int bclk_m, bclk_n; |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 126 | |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 127 | if (!acomp || !acomp->ops || !acomp->ops->get_cdclk_freq) |
| 128 | return; /* only for i915 binding */ |
| 129 | if (!CONTROLLER_IN_GPU(pci)) |
| 130 | return; /* only HSW/BDW */ |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 131 | |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 132 | cdclk_freq = acomp->ops->get_cdclk_freq(acomp->dev); |
| 133 | switch (cdclk_freq) { |
| 134 | case 337500: |
| 135 | bclk_m = 16; |
| 136 | bclk_n = 225; |
| 137 | break; |
| 138 | |
| 139 | case 450000: |
| 140 | default: /* default CDCLK 450MHz */ |
| 141 | bclk_m = 4; |
| 142 | bclk_n = 75; |
| 143 | break; |
| 144 | |
| 145 | case 540000: |
| 146 | bclk_m = 4; |
| 147 | bclk_n = 90; |
| 148 | break; |
| 149 | |
| 150 | case 675000: |
| 151 | bclk_m = 8; |
| 152 | bclk_n = 225; |
| 153 | break; |
| 154 | } |
| 155 | |
| 156 | snd_hdac_chip_writew(bus, HSW_EM4, bclk_m); |
| 157 | snd_hdac_chip_writew(bus, HSW_EM5, bclk_n); |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 158 | } |
Takashi Iwai | bb03ed2 | 2016-04-21 16:39:17 +0200 | [diff] [blame] | 159 | EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk); |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 160 | |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 161 | /* There is a fixed mapping between audio pin node and display port. |
| 162 | * on SNB, IVY, HSW, BSW, SKL, BXT, KBL: |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 163 | * Pin Widget 5 - PORT B (port = 1 in i915 driver) |
| 164 | * Pin Widget 6 - PORT C (port = 2 in i915 driver) |
| 165 | * Pin Widget 7 - PORT D (port = 3 in i915 driver) |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 166 | * |
| 167 | * on VLV, ILK: |
| 168 | * Pin Widget 4 - PORT B (port = 1 in i915 driver) |
| 169 | * Pin Widget 5 - PORT C (port = 2 in i915 driver) |
| 170 | * Pin Widget 6 - PORT D (port = 3 in i915 driver) |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 171 | */ |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 172 | static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid) |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 173 | { |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 174 | int base_nid; |
| 175 | |
| 176 | switch (codec->vendor_id) { |
| 177 | case 0x80860054: /* ILK */ |
| 178 | case 0x80862804: /* ILK */ |
| 179 | case 0x80862882: /* VLV */ |
| 180 | base_nid = 3; |
| 181 | break; |
| 182 | default: |
| 183 | base_nid = 4; |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3)) |
Takashi Iwai | 1a414f4 | 2016-03-10 15:49:35 +0100 | [diff] [blame] | 188 | return -1; |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 189 | return pin_nid - base_nid; |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 194 | * @codec: HDA codec |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 195 | * @nid: the pin widget NID |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 196 | * @dev_id: device identifier |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 197 | * @rate: the sample rate to set |
| 198 | * |
| 199 | * This function is supposed to be used only by a HD-audio controller |
| 200 | * driver that needs the interaction with i915 graphics. |
| 201 | * |
| 202 | * This function sets N/CTS value based on the given sample rate. |
| 203 | * Returns zero for success, or a negative error code. |
| 204 | */ |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 205 | int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid, |
| 206 | int dev_id, int rate) |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 207 | { |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 208 | struct hdac_bus *bus = codec->bus; |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 209 | struct i915_audio_component *acomp = bus->audio_component; |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 210 | int port, pipe; |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 211 | |
| 212 | if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate) |
| 213 | return -ENODEV; |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 214 | port = pin2port(codec, nid); |
Takashi Iwai | 1a414f4 | 2016-03-10 15:49:35 +0100 | [diff] [blame] | 215 | if (port < 0) |
| 216 | return -EINVAL; |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 217 | pipe = dev_id; |
| 218 | return acomp->ops->sync_audio_rate(acomp->dev, port, pipe, rate); |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 219 | } |
| 220 | EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate); |
| 221 | |
| 222 | /** |
| 223 | * snd_hdac_acomp_get_eld - Get the audio state and ELD via component |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 224 | * @codec: HDA codec |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 225 | * @nid: the pin widget NID |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 226 | * @dev_id: device identifier |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 227 | * @audio_enabled: the pointer to store the current audio state |
| 228 | * @buffer: the buffer pointer to store ELD bytes |
| 229 | * @max_bytes: the max bytes to be stored on @buffer |
| 230 | * |
| 231 | * This function is supposed to be used only by a HD-audio controller |
| 232 | * driver that needs the interaction with i915 graphics. |
| 233 | * |
| 234 | * This function queries the current state of the audio on the given |
| 235 | * digital port and fetches the ELD bytes onto the given buffer. |
| 236 | * It returns the number of bytes for the total ELD data, zero for |
| 237 | * invalid ELD, or a negative error code. |
| 238 | * |
| 239 | * The return size is the total bytes required for the whole ELD bytes, |
| 240 | * thus it may be over @max_bytes. If it's over @max_bytes, it implies |
| 241 | * that only a part of ELD bytes have been fetched. |
| 242 | */ |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 243 | int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid, int dev_id, |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 244 | bool *audio_enabled, char *buffer, int max_bytes) |
| 245 | { |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 246 | struct hdac_bus *bus = codec->bus; |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 247 | struct i915_audio_component *acomp = bus->audio_component; |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 248 | int port, pipe; |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 249 | |
| 250 | if (!acomp || !acomp->ops || !acomp->ops->get_eld) |
| 251 | return -ENODEV; |
| 252 | |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 253 | port = pin2port(codec, nid); |
Takashi Iwai | 1a414f4 | 2016-03-10 15:49:35 +0100 | [diff] [blame] | 254 | if (port < 0) |
| 255 | return -EINVAL; |
Pandiyan, Dhinakaran | f931894 | 2016-09-21 13:02:48 -0700 | [diff] [blame] | 256 | |
| 257 | pipe = dev_id; |
| 258 | return acomp->ops->get_eld(acomp->dev, port, pipe, audio_enabled, |
Takashi Iwai | e2dc7d7 | 2015-12-01 12:39:38 +0100 | [diff] [blame] | 259 | buffer, max_bytes); |
| 260 | } |
| 261 | EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld); |
| 262 | |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 263 | static int hdac_component_master_bind(struct device *dev) |
| 264 | { |
| 265 | struct i915_audio_component *acomp = hdac_acomp; |
| 266 | int ret; |
| 267 | |
| 268 | ret = component_bind_all(dev, acomp); |
| 269 | if (ret < 0) |
| 270 | return ret; |
| 271 | |
| 272 | if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power && |
| 273 | acomp->ops->put_power && acomp->ops->get_cdclk_freq))) { |
| 274 | ret = -EINVAL; |
| 275 | goto out_unbind; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Atm, we don't support dynamic unbinding initiated by the child |
| 280 | * component, so pin its containing module until we unbind. |
| 281 | */ |
| 282 | if (!try_module_get(acomp->ops->owner)) { |
| 283 | ret = -ENODEV; |
| 284 | goto out_unbind; |
| 285 | } |
| 286 | |
| 287 | return 0; |
| 288 | |
| 289 | out_unbind: |
| 290 | component_unbind_all(dev, acomp); |
| 291 | |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | static void hdac_component_master_unbind(struct device *dev) |
| 296 | { |
| 297 | struct i915_audio_component *acomp = hdac_acomp; |
| 298 | |
| 299 | module_put(acomp->ops->owner); |
| 300 | component_unbind_all(dev, acomp); |
| 301 | WARN_ON(acomp->ops || acomp->dev); |
| 302 | } |
| 303 | |
| 304 | static const struct component_master_ops hdac_component_master_ops = { |
| 305 | .bind = hdac_component_master_bind, |
| 306 | .unbind = hdac_component_master_unbind, |
| 307 | }; |
| 308 | |
| 309 | static int hdac_component_master_match(struct device *dev, void *data) |
| 310 | { |
| 311 | /* i915 is the only supported component */ |
| 312 | return !strcmp(dev->driver->name, "i915"); |
| 313 | } |
| 314 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 315 | /** |
| 316 | * snd_hdac_i915_register_notifier - Register i915 audio component ops |
| 317 | * @aops: i915 audio component ops |
| 318 | * |
| 319 | * This function is supposed to be used only by a HD-audio controller |
| 320 | * driver that needs the interaction with i915 graphics. |
| 321 | * |
| 322 | * This function sets the given ops to be called by the i915 graphics driver. |
| 323 | * |
| 324 | * Returns zero for success or a negative error code. |
| 325 | */ |
David Henningsson | 45c053d | 2015-08-19 10:48:57 +0200 | [diff] [blame] | 326 | int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops) |
| 327 | { |
| 328 | if (WARN_ON(!hdac_acomp)) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | hdac_acomp->audio_ops = aops; |
| 332 | return 0; |
| 333 | } |
| 334 | EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier); |
| 335 | |
Takashi Iwai | bfa5fb1 | 2016-03-29 15:03:06 +0200 | [diff] [blame] | 336 | /* check whether intel graphics is present */ |
| 337 | static bool i915_gfx_present(void) |
| 338 | { |
Arvind Yadav | 6c5a266 | 2017-07-18 22:35:06 +0530 | [diff] [blame] | 339 | static const struct pci_device_id ids[] = { |
Takashi Iwai | bfa5fb1 | 2016-03-29 15:03:06 +0200 | [diff] [blame] | 340 | { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID), |
| 341 | .class = PCI_BASE_CLASS_DISPLAY << 16, |
| 342 | .class_mask = 0xff << 16 }, |
| 343 | {} |
| 344 | }; |
| 345 | return pci_dev_present(ids); |
| 346 | } |
| 347 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 348 | /** |
| 349 | * snd_hdac_i915_init - Initialize i915 audio component |
| 350 | * @bus: HDA core bus |
| 351 | * |
| 352 | * This function is supposed to be used only by a HD-audio controller |
| 353 | * driver that needs the interaction with i915 graphics. |
| 354 | * |
| 355 | * This function initializes and sets up the audio component to communicate |
| 356 | * with i915 graphics driver. |
| 357 | * |
| 358 | * Returns zero for success or a negative error code. |
| 359 | */ |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 360 | int snd_hdac_i915_init(struct hdac_bus *bus) |
| 361 | { |
| 362 | struct component_match *match = NULL; |
| 363 | struct device *dev = bus->dev; |
| 364 | struct i915_audio_component *acomp; |
| 365 | int ret; |
| 366 | |
Takashi Iwai | d745f5e | 2016-03-21 14:41:58 +0100 | [diff] [blame] | 367 | if (WARN_ON(hdac_acomp)) |
| 368 | return -EBUSY; |
| 369 | |
Takashi Iwai | bfa5fb1 | 2016-03-29 15:03:06 +0200 | [diff] [blame] | 370 | if (!i915_gfx_present()) |
| 371 | return -ENODEV; |
| 372 | |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 373 | acomp = kzalloc(sizeof(*acomp), GFP_KERNEL); |
| 374 | if (!acomp) |
| 375 | return -ENOMEM; |
| 376 | bus->audio_component = acomp; |
| 377 | hdac_acomp = acomp; |
| 378 | |
| 379 | component_match_add(dev, &match, hdac_component_master_match, bus); |
| 380 | ret = component_master_add_with_match(dev, &hdac_component_master_ops, |
| 381 | match); |
| 382 | if (ret < 0) |
| 383 | goto out_err; |
| 384 | |
| 385 | /* |
| 386 | * Atm, we don't support deferring the component binding, so make sure |
| 387 | * i915 is loaded and that the binding successfully completes. |
| 388 | */ |
| 389 | request_module("i915"); |
| 390 | |
| 391 | if (!acomp->ops) { |
| 392 | ret = -ENODEV; |
| 393 | goto out_master_del; |
| 394 | } |
| 395 | dev_dbg(dev, "bound to i915 component master\n"); |
| 396 | |
| 397 | return 0; |
| 398 | out_master_del: |
| 399 | component_master_del(dev, &hdac_component_master_ops); |
| 400 | out_err: |
| 401 | kfree(acomp); |
| 402 | bus->audio_component = NULL; |
Takashi Iwai | 97cc2ed | 2016-03-29 18:48:07 +0200 | [diff] [blame] | 403 | hdac_acomp = NULL; |
Takashi Iwai | bed2e98 | 2016-01-20 15:00:26 +0100 | [diff] [blame] | 404 | dev_info(dev, "failed to add i915 component master (%d)\n", ret); |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | EXPORT_SYMBOL_GPL(snd_hdac_i915_init); |
| 409 | |
Takashi Iwai | 78dd5e2 | 2015-10-28 12:26:48 +0100 | [diff] [blame] | 410 | /** |
| 411 | * snd_hdac_i915_exit - Finalize i915 audio component |
| 412 | * @bus: HDA core bus |
| 413 | * |
| 414 | * This function is supposed to be used only by a HD-audio controller |
| 415 | * driver that needs the interaction with i915 graphics. |
| 416 | * |
| 417 | * This function releases the i915 audio component that has been used. |
| 418 | * |
| 419 | * Returns zero for success or a negative error code. |
| 420 | */ |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 421 | int snd_hdac_i915_exit(struct hdac_bus *bus) |
| 422 | { |
| 423 | struct device *dev = bus->dev; |
| 424 | struct i915_audio_component *acomp = bus->audio_component; |
| 425 | |
Takashi Iwai | 692af90 | 2015-06-10 12:15:27 +0200 | [diff] [blame] | 426 | if (!acomp) |
| 427 | return 0; |
| 428 | |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 429 | WARN_ON(bus->i915_power_refcount); |
Takashi Iwai | 692af90 | 2015-06-10 12:15:27 +0200 | [diff] [blame] | 430 | if (bus->i915_power_refcount > 0 && acomp->ops) |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 431 | acomp->ops->put_power(acomp->dev); |
| 432 | |
| 433 | component_master_del(dev, &hdac_component_master_ops); |
| 434 | |
| 435 | kfree(acomp); |
| 436 | bus->audio_component = NULL; |
Takashi Iwai | faafd03 | 2016-03-29 12:29:24 +0200 | [diff] [blame] | 437 | hdac_acomp = NULL; |
Mengdong Lin | 98d8fc6 | 2015-05-19 22:29:30 +0800 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | EXPORT_SYMBOL_GPL(snd_hdac_i915_exit); |