hdac_i915.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 *  hdac_i915.c - routines for sync between HD-A core and i915 display driver
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 *  for more details.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/component.h>
#include <drm/i915_component.h>
#include <sound/core.h>
#include <sound/hdaudio.h>
#include <sound/hda_i915.h>

static struct i915_audio_component *hdac_acomp;

26 27 28 29 30 31 32 33 34 35 36 37 38
/**
 * snd_hdac_set_codec_wakeup - Enable / disable HDMI/DP codec wakeup
 * @bus: HDA core bus
 * @enable: enable or disable the wakeup
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function should be called during the chip reset, also called at
 * resume for updating STATESTS register read.
 *
 * Returns zero for success or a negative error code.
 */
39 40 41 42
int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
{
	struct i915_audio_component *acomp = bus->audio_component;

43
	if (!acomp || !acomp->ops)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
		return -ENODEV;

	if (!acomp->ops->codec_wake_override) {
		dev_warn(bus->dev,
			"Invalid codec wake callback\n");
		return 0;
	}

	dev_dbg(bus->dev, "%s codec wakeup\n",
		enable ? "enable" : "disable");

	acomp->ops->codec_wake_override(acomp->dev, enable);

	return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);

61 62 63 64 65 66 67 68 69 70 71 72 73
/**
 * snd_hdac_display_power - Power up / down the power refcount
 * @bus: HDA core bus
 * @enable: power up or down
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function manages a refcount and calls the i915 get_power() and
 * put_power() ops accordingly, toggling the codec wakeup, too.
 *
 * Returns zero for success or a negative error code.
 */
74 75 76 77
int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
{
	struct i915_audio_component *acomp = bus->audio_component;

78
	if (!acomp || !acomp->ops)
79 80 81 82 83 84
		return -ENODEV;

	dev_dbg(bus->dev, "display power %s\n",
		enable ? "enable" : "disable");

	if (enable) {
85
		if (!bus->i915_power_refcount++) {
86
			acomp->ops->get_power(acomp->dev);
87 88 89
			snd_hdac_set_codec_wakeup(bus, true);
			snd_hdac_set_codec_wakeup(bus, false);
		}
90 91 92 93 94 95 96 97 98 99
	} else {
		WARN_ON(!bus->i915_power_refcount);
		if (!--bus->i915_power_refcount)
			acomp->ops->put_power(acomp->dev);
	}

	return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_display_power);

100 101 102 103 104 105 106 107 108 109
/**
 * snd_hdac_get_display_clk - Get CDCLK in kHz
 * @bus: HDA core bus
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function queries CDCLK value in kHz from the graphics driver and
 * returns the value.  A negative code is returned in error.
 */
110 111 112 113
int snd_hdac_get_display_clk(struct hdac_bus *bus)
{
	struct i915_audio_component *acomp = bus->audio_component;

114
	if (!acomp || !acomp->ops)
115 116 117 118 119 120
		return -ENODEV;

	return acomp->ops->get_cdclk_freq(acomp->dev);
}
EXPORT_SYMBOL_GPL(snd_hdac_get_display_clk);

121 122
/* There is a fixed mapping between audio pin node and display port.
 * on SNB, IVY, HSW, BSW, SKL, BXT, KBL:
123 124 125
 * Pin Widget 5 - PORT B (port = 1 in i915 driver)
 * Pin Widget 6 - PORT C (port = 2 in i915 driver)
 * Pin Widget 7 - PORT D (port = 3 in i915 driver)
126 127 128 129 130
 *
 * on VLV, ILK:
 * Pin Widget 4 - PORT B (port = 1 in i915 driver)
 * Pin Widget 5 - PORT C (port = 2 in i915 driver)
 * Pin Widget 6 - PORT D (port = 3 in i915 driver)
131
 */
132
static int pin2port(struct hdac_device *codec, hda_nid_t pin_nid)
133
{
134 135 136 137 138 139 140 141 142 143 144 145 146 147
	int base_nid;

	switch (codec->vendor_id) {
	case 0x80860054: /* ILK */
	case 0x80862804: /* ILK */
	case 0x80862882: /* VLV */
		base_nid = 3;
		break;
	default:
		base_nid = 4;
		break;
	}

	if (WARN_ON(pin_nid <= base_nid || pin_nid > base_nid + 3))
148
		return -1;
149
	return pin_nid - base_nid;
150 151 152 153
}

/**
 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
154
 * @codec: HDA codec
155 156 157 158 159 160 161 162 163
 * @nid: the pin widget NID
 * @rate: the sample rate to set
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function sets N/CTS value based on the given sample rate.
 * Returns zero for success, or a negative error code.
 */
164
int snd_hdac_sync_audio_rate(struct hdac_device *codec, hda_nid_t nid, int rate)
165
{
166
	struct hdac_bus *bus = codec->bus;
167
	struct i915_audio_component *acomp = bus->audio_component;
168
	int port;
169 170 171

	if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
		return -ENODEV;
172
	port = pin2port(codec, nid);
173 174 175
	if (port < 0)
		return -EINVAL;
	return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
176 177 178 179 180
}
EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);

/**
 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
181
 * @codec: HDA codec
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
 * @nid: the pin widget NID
 * @audio_enabled: the pointer to store the current audio state
 * @buffer: the buffer pointer to store ELD bytes
 * @max_bytes: the max bytes to be stored on @buffer
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function queries the current state of the audio on the given
 * digital port and fetches the ELD bytes onto the given buffer.
 * It returns the number of bytes for the total ELD data, zero for
 * invalid ELD, or a negative error code.
 *
 * The return size is the total bytes required for the whole ELD bytes,
 * thus it may be over @max_bytes.  If it's over @max_bytes, it implies
 * that only a part of ELD bytes have been fetched.
 */
199
int snd_hdac_acomp_get_eld(struct hdac_device *codec, hda_nid_t nid,
200 201
			   bool *audio_enabled, char *buffer, int max_bytes)
{
202
	struct hdac_bus *bus = codec->bus;
203
	struct i915_audio_component *acomp = bus->audio_component;
204
	int port;
205 206 207 208

	if (!acomp || !acomp->ops || !acomp->ops->get_eld)
		return -ENODEV;

209
	port = pin2port(codec, nid);
210 211 212
	if (port < 0)
		return -EINVAL;
	return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
213 214 215 216
				   buffer, max_bytes);
}
EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
static int hdac_component_master_bind(struct device *dev)
{
	struct i915_audio_component *acomp = hdac_acomp;
	int ret;

	ret = component_bind_all(dev, acomp);
	if (ret < 0)
		return ret;

	if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
		      acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
		ret = -EINVAL;
		goto out_unbind;
	}

	/*
	 * Atm, we don't support dynamic unbinding initiated by the child
	 * component, so pin its containing module until we unbind.
	 */
	if (!try_module_get(acomp->ops->owner)) {
		ret = -ENODEV;
		goto out_unbind;
	}

	return 0;

out_unbind:
	component_unbind_all(dev, acomp);

	return ret;
}

static void hdac_component_master_unbind(struct device *dev)
{
	struct i915_audio_component *acomp = hdac_acomp;

	module_put(acomp->ops->owner);
	component_unbind_all(dev, acomp);
	WARN_ON(acomp->ops || acomp->dev);
}

static const struct component_master_ops hdac_component_master_ops = {
	.bind = hdac_component_master_bind,
	.unbind = hdac_component_master_unbind,
};

static int hdac_component_master_match(struct device *dev, void *data)
{
	/* i915 is the only supported component */
	return !strcmp(dev->driver->name, "i915");
}

269 270 271 272 273 274 275 276 277 278 279
/**
 * snd_hdac_i915_register_notifier - Register i915 audio component ops
 * @aops: i915 audio component ops
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function sets the given ops to be called by the i915 graphics driver.
 *
 * Returns zero for success or a negative error code.
 */
280 281 282 283 284 285 286 287 288 289
int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
{
	if (WARN_ON(!hdac_acomp))
		return -ENODEV;

	hdac_acomp->audio_ops = aops;
	return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);

290 291 292 293 294 295 296 297 298 299 300 301
/**
 * snd_hdac_i915_init - Initialize i915 audio component
 * @bus: HDA core bus
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function initializes and sets up the audio component to communicate
 * with i915 graphics driver.
 *
 * Returns zero for success or a negative error code.
 */
302 303 304 305 306 307 308
int snd_hdac_i915_init(struct hdac_bus *bus)
{
	struct component_match *match = NULL;
	struct device *dev = bus->dev;
	struct i915_audio_component *acomp;
	int ret;

309 310 311
	if (WARN_ON(hdac_acomp))
		return -EBUSY;

312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
	if (!acomp)
		return -ENOMEM;
	bus->audio_component = acomp;
	hdac_acomp = acomp;

	component_match_add(dev, &match, hdac_component_master_match, bus);
	ret = component_master_add_with_match(dev, &hdac_component_master_ops,
					      match);
	if (ret < 0)
		goto out_err;

	/*
	 * Atm, we don't support deferring the component binding, so make sure
	 * i915 is loaded and that the binding successfully completes.
	 */
	request_module("i915");

	if (!acomp->ops) {
		ret = -ENODEV;
		goto out_master_del;
	}
	dev_dbg(dev, "bound to i915 component master\n");

	return 0;
out_master_del:
	component_master_del(dev, &hdac_component_master_ops);
out_err:
	kfree(acomp);
	bus->audio_component = NULL;
342
	dev_info(dev, "failed to add i915 component master (%d)\n", ret);
343 344 345 346 347

	return ret;
}
EXPORT_SYMBOL_GPL(snd_hdac_i915_init);

348 349 350 351 352 353 354 355 356 357 358
/**
 * snd_hdac_i915_exit - Finalize i915 audio component
 * @bus: HDA core bus
 *
 * This function is supposed to be used only by a HD-audio controller
 * driver that needs the interaction with i915 graphics.
 *
 * This function releases the i915 audio component that has been used.
 *
 * Returns zero for success or a negative error code.
 */
359 360 361 362 363
int snd_hdac_i915_exit(struct hdac_bus *bus)
{
	struct device *dev = bus->dev;
	struct i915_audio_component *acomp = bus->audio_component;

364 365 366
	if (!acomp)
		return 0;

367
	WARN_ON(bus->i915_power_refcount);
368
	if (bus->i915_power_refcount > 0 && acomp->ops)
369 370 371 372 373 374 375 376 377 378
		acomp->ops->put_power(acomp->dev);

	component_master_del(dev, &hdac_component_master_ops);

	kfree(acomp);
	bus->audio_component = NULL;

	return 0;
}
EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);