linux/sound/hda/hdac_i915.c
<<
>>
Prefs
   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>
  23#include <sound/hda_register.h>
  24
  25static struct i915_audio_component *hdac_acomp;
  26
  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 */
  40int snd_hdac_set_codec_wakeup(struct hdac_bus *bus, bool enable)
  41{
  42        struct i915_audio_component *acomp = bus->audio_component;
  43
  44        if (!acomp || !acomp->ops)
  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}
  60EXPORT_SYMBOL_GPL(snd_hdac_set_codec_wakeup);
  61
  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 */
  75int snd_hdac_display_power(struct hdac_bus *bus, bool enable)
  76{
  77        struct i915_audio_component *acomp = bus->audio_component;
  78
  79        if (!acomp || !acomp->ops)
  80                return -ENODEV;
  81
  82        dev_dbg(bus->dev, "display power %s\n",
  83                enable ? "enable" : "disable");
  84
  85        if (enable) {
  86                if (!bus->i915_power_refcount++) {
  87                        acomp->ops->get_power(acomp->dev);
  88                        snd_hdac_set_codec_wakeup(bus, true);
  89                        snd_hdac_set_codec_wakeup(bus, false);
  90                }
  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}
  99EXPORT_SYMBOL_GPL(snd_hdac_display_power);
 100
 101#define CONTROLLER_IN_GPU(pci) (((pci)->device == 0x0a0c) || \
 102                                ((pci)->device == 0x0c0c) || \
 103                                ((pci)->device == 0x0d0c) || \
 104                                ((pci)->device == 0x160c))
 105
 106/**
 107 * snd_hdac_i915_set_bclk - Reprogram BCLK for HSW/BDW
 108 * @bus: HDA core bus
 109 *
 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.
 116 *
 117 * Call this function at initializing and changing power well, as well as
 118 * at ELD notifier for the hotplug.
 119 */
 120void snd_hdac_i915_set_bclk(struct hdac_bus *bus)
 121{
 122        struct i915_audio_component *acomp = bus->audio_component;
 123        struct pci_dev *pci = to_pci_dev(bus->dev);
 124        int cdclk_freq;
 125        unsigned int bclk_m, bclk_n;
 126
 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 */
 131
 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);
 158}
 159EXPORT_SYMBOL_GPL(snd_hdac_i915_set_bclk);
 160
 161/* There is a fixed mapping between audio pin node and display port
 162 * on current Intel platforms:
 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)
 166 */
 167static int pin2port(hda_nid_t pin_nid)
 168{
 169        if (WARN_ON(pin_nid < 5 || pin_nid > 7))
 170                return -1;
 171        return pin_nid - 4;
 172}
 173
 174/**
 175 * snd_hdac_sync_audio_rate - Set N/CTS based on the sample rate
 176 * @bus: HDA core bus
 177 * @nid: the pin widget NID
 178 * @rate: the sample rate to set
 179 *
 180 * This function is supposed to be used only by a HD-audio controller
 181 * driver that needs the interaction with i915 graphics.
 182 *
 183 * This function sets N/CTS value based on the given sample rate.
 184 * Returns zero for success, or a negative error code.
 185 */
 186int snd_hdac_sync_audio_rate(struct hdac_bus *bus, hda_nid_t nid, int rate)
 187{
 188        struct i915_audio_component *acomp = bus->audio_component;
 189        int port;
 190
 191        if (!acomp || !acomp->ops || !acomp->ops->sync_audio_rate)
 192                return -ENODEV;
 193        port = pin2port(nid);
 194        if (port < 0)
 195                return -EINVAL;
 196        return acomp->ops->sync_audio_rate(acomp->dev, port, rate);
 197}
 198EXPORT_SYMBOL_GPL(snd_hdac_sync_audio_rate);
 199
 200/**
 201 * snd_hdac_acomp_get_eld - Get the audio state and ELD via component
 202 * @bus: HDA core bus
 203 * @nid: the pin widget NID
 204 * @audio_enabled: the pointer to store the current audio state
 205 * @buffer: the buffer pointer to store ELD bytes
 206 * @max_bytes: the max bytes to be stored on @buffer
 207 *
 208 * This function is supposed to be used only by a HD-audio controller
 209 * driver that needs the interaction with i915 graphics.
 210 *
 211 * This function queries the current state of the audio on the given
 212 * digital port and fetches the ELD bytes onto the given buffer.
 213 * It returns the number of bytes for the total ELD data, zero for
 214 * invalid ELD, or a negative error code.
 215 *
 216 * The return size is the total bytes required for the whole ELD bytes,
 217 * thus it may be over @max_bytes.  If it's over @max_bytes, it implies
 218 * that only a part of ELD bytes have been fetched.
 219 */
 220int snd_hdac_acomp_get_eld(struct hdac_bus *bus, hda_nid_t nid,
 221                           bool *audio_enabled, char *buffer, int max_bytes)
 222{
 223        struct i915_audio_component *acomp = bus->audio_component;
 224        int port;
 225
 226        if (!acomp || !acomp->ops || !acomp->ops->get_eld)
 227                return -ENODEV;
 228
 229        port = pin2port(nid);
 230        if (port < 0)
 231                return -EINVAL;
 232        return acomp->ops->get_eld(acomp->dev, port, audio_enabled,
 233                                   buffer, max_bytes);
 234}
 235EXPORT_SYMBOL_GPL(snd_hdac_acomp_get_eld);
 236
 237static int hdac_component_master_bind(struct device *dev)
 238{
 239        struct i915_audio_component *acomp = hdac_acomp;
 240        int ret;
 241
 242        ret = component_bind_all(dev, acomp);
 243        if (ret < 0)
 244                return ret;
 245
 246        if (WARN_ON(!(acomp->dev && acomp->ops && acomp->ops->get_power &&
 247                      acomp->ops->put_power && acomp->ops->get_cdclk_freq))) {
 248                ret = -EINVAL;
 249                goto out_unbind;
 250        }
 251
 252        /*
 253         * Atm, we don't support dynamic unbinding initiated by the child
 254         * component, so pin its containing module until we unbind.
 255         */
 256        if (!try_module_get(acomp->ops->owner)) {
 257                ret = -ENODEV;
 258                goto out_unbind;
 259        }
 260
 261        return 0;
 262
 263out_unbind:
 264        component_unbind_all(dev, acomp);
 265
 266        return ret;
 267}
 268
 269static void hdac_component_master_unbind(struct device *dev)
 270{
 271        struct i915_audio_component *acomp = hdac_acomp;
 272
 273        module_put(acomp->ops->owner);
 274        component_unbind_all(dev, acomp);
 275        WARN_ON(acomp->ops || acomp->dev);
 276}
 277
 278static const struct component_master_ops hdac_component_master_ops = {
 279        .bind = hdac_component_master_bind,
 280        .unbind = hdac_component_master_unbind,
 281};
 282
 283static int hdac_component_master_match(struct device *dev, void *data)
 284{
 285        /* i915 is the only supported component */
 286        return !strcmp(dev->driver->name, "i915");
 287}
 288
 289/**
 290 * snd_hdac_i915_register_notifier - Register i915 audio component ops
 291 * @aops: i915 audio component ops
 292 *
 293 * This function is supposed to be used only by a HD-audio controller
 294 * driver that needs the interaction with i915 graphics.
 295 *
 296 * This function sets the given ops to be called by the i915 graphics driver.
 297 *
 298 * Returns zero for success or a negative error code.
 299 */
 300int snd_hdac_i915_register_notifier(const struct i915_audio_component_audio_ops *aops)
 301{
 302        if (WARN_ON(!hdac_acomp))
 303                return -ENODEV;
 304
 305        hdac_acomp->audio_ops = aops;
 306        return 0;
 307}
 308EXPORT_SYMBOL_GPL(snd_hdac_i915_register_notifier);
 309
 310/* check whether intel graphics is present */
 311static bool i915_gfx_present(void)
 312{
 313        static struct pci_device_id ids[] = {
 314                { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_ANY_ID),
 315                  .class = PCI_BASE_CLASS_DISPLAY << 16,
 316                  .class_mask = 0xff << 16 },
 317                {}
 318        };
 319        return pci_dev_present(ids);
 320}
 321
 322/**
 323 * snd_hdac_i915_init - Initialize i915 audio component
 324 * @bus: HDA core bus
 325 *
 326 * This function is supposed to be used only by a HD-audio controller
 327 * driver that needs the interaction with i915 graphics.
 328 *
 329 * This function initializes and sets up the audio component to communicate
 330 * with i915 graphics driver.
 331 *
 332 * Returns zero for success or a negative error code.
 333 */
 334int snd_hdac_i915_init(struct hdac_bus *bus)
 335{
 336        struct component_match *match = NULL;
 337        struct device *dev = bus->dev;
 338        struct i915_audio_component *acomp;
 339        int ret;
 340
 341        if (!i915_gfx_present())
 342                return -ENODEV;
 343
 344        acomp = kzalloc(sizeof(*acomp), GFP_KERNEL);
 345        if (!acomp)
 346                return -ENOMEM;
 347        bus->audio_component = acomp;
 348        hdac_acomp = acomp;
 349
 350        component_match_add(dev, &match, hdac_component_master_match, bus);
 351        ret = component_master_add_with_match(dev, &hdac_component_master_ops,
 352                                              match);
 353        if (ret < 0)
 354                goto out_err;
 355
 356        /*
 357         * Atm, we don't support deferring the component binding, so make sure
 358         * i915 is loaded and that the binding successfully completes.
 359         */
 360        request_module("i915");
 361
 362        if (!acomp->ops) {
 363                ret = -ENODEV;
 364                goto out_master_del;
 365        }
 366        dev_dbg(dev, "bound to i915 component master\n");
 367
 368        return 0;
 369out_master_del:
 370        component_master_del(dev, &hdac_component_master_ops);
 371out_err:
 372        kfree(acomp);
 373        bus->audio_component = NULL;
 374        dev_info(dev, "failed to add i915 component master (%d)\n", ret);
 375
 376        return ret;
 377}
 378EXPORT_SYMBOL_GPL(snd_hdac_i915_init);
 379
 380/**
 381 * snd_hdac_i915_exit - Finalize i915 audio component
 382 * @bus: HDA core bus
 383 *
 384 * This function is supposed to be used only by a HD-audio controller
 385 * driver that needs the interaction with i915 graphics.
 386 *
 387 * This function releases the i915 audio component that has been used.
 388 *
 389 * Returns zero for success or a negative error code.
 390 */
 391int snd_hdac_i915_exit(struct hdac_bus *bus)
 392{
 393        struct device *dev = bus->dev;
 394        struct i915_audio_component *acomp = bus->audio_component;
 395
 396        if (!acomp)
 397                return 0;
 398
 399        WARN_ON(bus->i915_power_refcount);
 400        if (bus->i915_power_refcount > 0 && acomp->ops)
 401                acomp->ops->put_power(acomp->dev);
 402
 403        component_master_del(dev, &hdac_component_master_ops);
 404
 405        kfree(acomp);
 406        bus->audio_component = NULL;
 407
 408        return 0;
 409}
 410EXPORT_SYMBOL_GPL(snd_hdac_i915_exit);
 411