···6868 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },6969};70707171+/* HDMI N/CTS table */7272+#define TMDS_297M 2970007373+#define TMDS_296M DIV_ROUND_UP(297000 * 1000, 1001)7474+static const struct {7575+ int sample_rate;7676+ int clock;7777+ int n;7878+ int cts;7979+} aud_ncts[] = {8080+ { 44100, TMDS_296M, 4459, 234375 },8181+ { 44100, TMDS_297M, 4704, 247500 },8282+ { 48000, TMDS_296M, 5824, 281250 },8383+ { 48000, TMDS_297M, 5120, 247500 },8484+ { 32000, TMDS_296M, 5824, 421875 },8585+ { 32000, TMDS_297M, 3072, 222750 },8686+ { 88200, TMDS_296M, 8918, 234375 },8787+ { 88200, TMDS_297M, 9408, 247500 },8888+ { 96000, TMDS_296M, 11648, 281250 },8989+ { 96000, TMDS_297M, 10240, 247500 },9090+ { 176400, TMDS_296M, 17836, 234375 },9191+ { 176400, TMDS_297M, 18816, 247500 },9292+ { 192000, TMDS_296M, 23296, 281250 },9393+ { 192000, TMDS_297M, 20480, 247500 },9494+};9595+7196/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */7297static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)7398{···11388 hdmi_audio_clock[i].config);1148911590 return hdmi_audio_clock[i].config;9191+}9292+9393+static int audio_config_get_n(const struct drm_display_mode *mode, int rate)9494+{9595+ int i;9696+9797+ for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) {9898+ if ((rate == aud_ncts[i].sample_rate) &&9999+ (mode->clock == aud_ncts[i].clock)) {100100+ return aud_ncts[i].n;101101+ }102102+ }103103+ return 0;104104+}105105+106106+static uint32_t audio_config_setup_n_reg(int n, uint32_t val)107107+{108108+ int n_low, n_up;109109+ uint32_t tmp = val;110110+111111+ n_low = n & 0xfff;112112+ n_up = (n >> 12) & 0xff;113113+ tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK);114114+ tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) |115115+ (n_low << AUD_CONFIG_LOWER_N_SHIFT) |116116+ AUD_CONFIG_N_PROG_ENABLE);117117+ return tmp;118118+}119119+120120+/* check whether N/CTS/M need be set manually */121121+static bool audio_rate_need_prog(struct intel_crtc *crtc,122122+ struct drm_display_mode *mode)123123+{124124+ if (((mode->clock == TMDS_297M) ||125125+ (mode->clock == TMDS_296M)) &&126126+ intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))127127+ return true;128128+ else129129+ return false;116130}117131118132static bool intel_eld_uptodate(struct drm_connector *connector,···248184249185 DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe));250186187187+ mutex_lock(&dev_priv->av_mutex);188188+251189 /* Disable timestamps */252190 tmp = I915_READ(HSW_AUD_CFG(pipe));253191 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;···265199 tmp &= ~AUDIO_ELD_VALID(pipe);266200 tmp &= ~AUDIO_OUTPUT_ENABLE(pipe);267201 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);202202+203203+ mutex_unlock(&dev_priv->av_mutex);268204}269205270206static void hsw_audio_codec_enable(struct drm_connector *connector,···276208 struct drm_i915_private *dev_priv = connector->dev->dev_private;277209 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc);278210 enum pipe pipe = intel_crtc->pipe;211211+ struct i915_audio_component *acomp = dev_priv->audio_component;279212 const uint8_t *eld = connector->eld;213213+ struct intel_digital_port *intel_dig_port =214214+ enc_to_dig_port(&encoder->base);215215+ enum port port = intel_dig_port->port;280216 uint32_t tmp;281217 int len, i;218218+ int n, rate;282219283220 DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n",284221 pipe_name(pipe), drm_eld_size(eld));222222+223223+ mutex_lock(&dev_priv->av_mutex);285224286225 /* Enable audio presence detect, invalidate ELD */287226 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);···321246 /* Enable timestamps */322247 tmp = I915_READ(HSW_AUD_CFG(pipe));323248 tmp &= ~AUD_CONFIG_N_VALUE_INDEX;324324- tmp &= ~AUD_CONFIG_N_PROG_ENABLE;325249 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;326250 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT))327251 tmp |= AUD_CONFIG_N_VALUE_INDEX;328252 else329253 tmp |= audio_config_hdmi_pixel_clock(mode);254254+255255+ tmp &= ~AUD_CONFIG_N_PROG_ENABLE;256256+ if (audio_rate_need_prog(intel_crtc, mode)) {257257+ if (!acomp)258258+ rate = 0;259259+ else if (port >= PORT_A && port <= PORT_E)260260+ rate = acomp->aud_sample_rate[port];261261+ else {262262+ DRM_ERROR("invalid port: %d\n", port);263263+ rate = 0;264264+ }265265+ n = audio_config_get_n(mode, rate);266266+ if (n != 0)267267+ tmp = audio_config_setup_n_reg(n, tmp);268268+ else269269+ DRM_DEBUG_KMS("no suitable N value is found\n");270270+ }271271+330272 I915_WRITE(HSW_AUD_CFG(pipe), tmp);273273+274274+ mutex_unlock(&dev_priv->av_mutex);331275}332276333277static void ilk_audio_codec_disable(struct intel_encoder *encoder)···621527 return ret;622528}623529530530+static int i915_audio_component_sync_audio_rate(struct device *dev,531531+ int port, int rate)532532+{533533+ struct drm_i915_private *dev_priv = dev_to_i915(dev);534534+ struct drm_device *drm_dev = dev_priv->dev;535535+ struct intel_encoder *intel_encoder;536536+ struct intel_digital_port *intel_dig_port;537537+ struct intel_crtc *crtc;538538+ struct drm_display_mode *mode;539539+ struct i915_audio_component *acomp = dev_priv->audio_component;540540+ enum pipe pipe = -1;541541+ u32 tmp;542542+ int n;543543+544544+ /* HSW, BDW SKL need this fix */545545+ if (!IS_SKYLAKE(dev_priv) &&546546+ !IS_BROADWELL(dev_priv) &&547547+ !IS_HASWELL(dev_priv))548548+ return 0;549549+550550+ mutex_lock(&dev_priv->av_mutex);551551+ /* 1. get the pipe */552552+ for_each_intel_encoder(drm_dev, intel_encoder) {553553+ if (intel_encoder->type != INTEL_OUTPUT_HDMI)554554+ continue;555555+ intel_dig_port = enc_to_dig_port(&intel_encoder->base);556556+ if (port == intel_dig_port->port) {557557+ crtc = to_intel_crtc(intel_encoder->base.crtc);558558+ if (!crtc) {559559+ DRM_DEBUG_KMS("%s: crtc is NULL\n", __func__);560560+ continue;561561+ }562562+ pipe = crtc->pipe;563563+ break;564564+ }565565+ }566566+567567+ if (pipe == INVALID_PIPE) {568568+ DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port));569569+ mutex_unlock(&dev_priv->av_mutex);570570+ return -ENODEV;571571+ }572572+ DRM_DEBUG_KMS("pipe %c connects port %c\n",573573+ pipe_name(pipe), port_name(port));574574+ mode = &crtc->config->base.adjusted_mode;575575+576576+ /* port must be valid now, otherwise the pipe will be invalid */577577+ acomp->aud_sample_rate[port] = rate;578578+579579+ /* 2. check whether to set the N/CTS/M manually or not */580580+ if (!audio_rate_need_prog(crtc, mode)) {581581+ tmp = I915_READ(HSW_AUD_CFG(pipe));582582+ tmp &= ~AUD_CONFIG_N_PROG_ENABLE;583583+ I915_WRITE(HSW_AUD_CFG(pipe), tmp);584584+ mutex_unlock(&dev_priv->av_mutex);585585+ return 0;586586+ }587587+588588+ n = audio_config_get_n(mode, rate);589589+ if (n == 0) {590590+ DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n",591591+ port_name(port));592592+ tmp = I915_READ(HSW_AUD_CFG(pipe));593593+ tmp &= ~AUD_CONFIG_N_PROG_ENABLE;594594+ I915_WRITE(HSW_AUD_CFG(pipe), tmp);595595+ mutex_unlock(&dev_priv->av_mutex);596596+ return 0;597597+ }598598+599599+ /* 3. set the N/CTS/M */600600+ tmp = I915_READ(HSW_AUD_CFG(pipe));601601+ tmp = audio_config_setup_n_reg(n, tmp);602602+ I915_WRITE(HSW_AUD_CFG(pipe), tmp);603603+604604+ mutex_unlock(&dev_priv->av_mutex);605605+ return 0;606606+}607607+624608static const struct i915_audio_component_ops i915_audio_component_ops = {625609 .owner = THIS_MODULE,626610 .get_power = i915_audio_component_get_power,627611 .put_power = i915_audio_component_put_power,628612 .codec_wake_override = i915_audio_component_codec_wake_override,629613 .get_cdclk_freq = i915_audio_component_get_cdclk_freq,614614+ .sync_audio_rate = i915_audio_component_sync_audio_rate,630615};631616632617static int i915_audio_component_bind(struct device *i915_dev,···713540{714541 struct i915_audio_component *acomp = data;715542 struct drm_i915_private *dev_priv = dev_to_i915(i915_dev);543543+ int i;716544717545 if (WARN_ON(acomp->ops || acomp->dev))718546 return -EEXIST;···721547 drm_modeset_lock_all(dev_priv->dev);722548 acomp->ops = &i915_audio_component_ops;723549 acomp->dev = i915_dev;550550+ BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);551551+ for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)552552+ acomp->aud_sample_rate[i] = 0;724553 dev_priv->audio_component = acomp;725554 drm_modeset_unlock_all(dev_priv->dev);726555
+17
include/drm/i915_component.h
···2424#ifndef _I915_COMPONENT_H_2525#define _I915_COMPONENT_H_26262727+/* MAX_PORT is the number of port2828+ * It must be sync with I915_MAX_PORTS defined i915_drv.h2929+ * 5 should be enough as only HSW, BDW, SKL need such fix.3030+ */3131+#define MAX_PORTS 53232+2733struct i915_audio_component {2834 struct device *dev;3535+ /**3636+ * @aud_sample_rate: the array of audio sample rate per port3737+ */3838+ int aud_sample_rate[MAX_PORTS];29393040 const struct i915_audio_component_ops {3141 struct module *owner;···4333 void (*put_power)(struct device *);4434 void (*codec_wake_override)(struct device *, bool enable);4535 int (*get_cdclk_freq)(struct device *);3636+ /**3737+ * @sync_audio_rate: set n/cts based on the sample rate3838+ *3939+ * Called from audio driver. After audio driver sets the4040+ * sample rate, it will call this function to set n/cts4141+ */4242+ int (*sync_audio_rate)(struct device *, int port, int rate);4643 } *ops;47444845 const struct i915_audio_component_audio_ops {
+19
sound/pci/hda/patch_hdmi.c
···17751775 return non_pcm;17761776}1777177717781778+/* There is a fixed mapping between audio pin node and display port17791779+ * on current Intel platforms:17801780+ * Pin Widget 5 - PORT B (port = 1 in i915 driver)17811781+ * Pin Widget 6 - PORT C (port = 2 in i915 driver)17821782+ * Pin Widget 7 - PORT D (port = 3 in i915 driver)17831783+ */17841784+static int intel_pin2port(hda_nid_t pin_nid)17851785+{17861786+ return pin_nid - 4;17871787+}1778178817791789/*17801790 * HDMI callbacks···18011791 int pin_idx = hinfo_to_pin_index(codec, hinfo);18021792 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);18031793 hda_nid_t pin_nid = per_pin->pin_nid;17941794+ struct snd_pcm_runtime *runtime = substream->runtime;17951795+ struct i915_audio_component *acomp = codec->bus->core.audio_component;18041796 bool non_pcm;18051797 int pinctl;18061798···18181806 intel_verify_pin_cvt_connect(codec, per_pin);18191807 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);18201808 }18091809+18101810+ /* Call sync_audio_rate to set the N/CTS/M manually if necessary */18111811+ /* Todo: add DP1.2 MST audio support later */18121812+ if (acomp && acomp->ops && acomp->ops->sync_audio_rate)18131813+ acomp->ops->sync_audio_rate(acomp->dev,18141814+ intel_pin2port(pin_nid),18151815+ runtime->rate);1821181618221817 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);18231818 mutex_lock(&per_pin->lock);