Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'topic/drm-sync-audio-rate' into for-next

+220 -1
+1
drivers/gpu/drm/i915/i915_dma.c
··· 832 832 mutex_init(&dev_priv->sb_lock); 833 833 mutex_init(&dev_priv->modeset_restore_lock); 834 834 mutex_init(&dev_priv->csr_lock); 835 + mutex_init(&dev_priv->av_mutex); 835 836 836 837 intel_pm_setup(dev); 837 838
+5
drivers/gpu/drm/i915/i915_drv.h
··· 1885 1885 /* hda/i915 audio component */ 1886 1886 struct i915_audio_component *audio_component; 1887 1887 bool audio_component_registered; 1888 + /** 1889 + * av_mutex - mutex for audio/video sync 1890 + * 1891 + */ 1892 + struct mutex av_mutex; 1888 1893 1889 1894 uint32_t hw_context_size; 1890 1895 struct list_head context_list;
+178 -1
drivers/gpu/drm/i915/intel_audio.c
··· 68 68 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 }, 69 69 }; 70 70 71 + /* HDMI N/CTS table */ 72 + #define TMDS_297M 297000 73 + #define TMDS_296M DIV_ROUND_UP(297000 * 1000, 1001) 74 + static const struct { 75 + int sample_rate; 76 + int clock; 77 + int n; 78 + int cts; 79 + } aud_ncts[] = { 80 + { 44100, TMDS_296M, 4459, 234375 }, 81 + { 44100, TMDS_297M, 4704, 247500 }, 82 + { 48000, TMDS_296M, 5824, 281250 }, 83 + { 48000, TMDS_297M, 5120, 247500 }, 84 + { 32000, TMDS_296M, 5824, 421875 }, 85 + { 32000, TMDS_297M, 3072, 222750 }, 86 + { 88200, TMDS_296M, 8918, 234375 }, 87 + { 88200, TMDS_297M, 9408, 247500 }, 88 + { 96000, TMDS_296M, 11648, 281250 }, 89 + { 96000, TMDS_297M, 10240, 247500 }, 90 + { 176400, TMDS_296M, 17836, 234375 }, 91 + { 176400, TMDS_297M, 18816, 247500 }, 92 + { 192000, TMDS_296M, 23296, 281250 }, 93 + { 192000, TMDS_297M, 20480, 247500 }, 94 + }; 95 + 71 96 /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */ 72 97 static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode) 73 98 { ··· 113 88 hdmi_audio_clock[i].config); 114 89 115 90 return hdmi_audio_clock[i].config; 91 + } 92 + 93 + static int audio_config_get_n(const struct drm_display_mode *mode, int rate) 94 + { 95 + int i; 96 + 97 + for (i = 0; i < ARRAY_SIZE(aud_ncts); i++) { 98 + if ((rate == aud_ncts[i].sample_rate) && 99 + (mode->clock == aud_ncts[i].clock)) { 100 + return aud_ncts[i].n; 101 + } 102 + } 103 + return 0; 104 + } 105 + 106 + static uint32_t audio_config_setup_n_reg(int n, uint32_t val) 107 + { 108 + int n_low, n_up; 109 + uint32_t tmp = val; 110 + 111 + n_low = n & 0xfff; 112 + n_up = (n >> 12) & 0xff; 113 + tmp &= ~(AUD_CONFIG_UPPER_N_MASK | AUD_CONFIG_LOWER_N_MASK); 114 + tmp |= ((n_up << AUD_CONFIG_UPPER_N_SHIFT) | 115 + (n_low << AUD_CONFIG_LOWER_N_SHIFT) | 116 + AUD_CONFIG_N_PROG_ENABLE); 117 + return tmp; 118 + } 119 + 120 + /* check whether N/CTS/M need be set manually */ 121 + static bool audio_rate_need_prog(struct intel_crtc *crtc, 122 + struct drm_display_mode *mode) 123 + { 124 + if (((mode->clock == TMDS_297M) || 125 + (mode->clock == TMDS_296M)) && 126 + intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) 127 + return true; 128 + else 129 + return false; 116 130 } 117 131 118 132 static bool intel_eld_uptodate(struct drm_connector *connector, ··· 248 184 249 185 DRM_DEBUG_KMS("Disable audio codec on pipe %c\n", pipe_name(pipe)); 250 186 187 + mutex_lock(&dev_priv->av_mutex); 188 + 251 189 /* Disable timestamps */ 252 190 tmp = I915_READ(HSW_AUD_CFG(pipe)); 253 191 tmp &= ~AUD_CONFIG_N_VALUE_INDEX; ··· 265 199 tmp &= ~AUDIO_ELD_VALID(pipe); 266 200 tmp &= ~AUDIO_OUTPUT_ENABLE(pipe); 267 201 I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp); 202 + 203 + mutex_unlock(&dev_priv->av_mutex); 268 204 } 269 205 270 206 static void hsw_audio_codec_enable(struct drm_connector *connector, ··· 276 208 struct drm_i915_private *dev_priv = connector->dev->dev_private; 277 209 struct intel_crtc *intel_crtc = to_intel_crtc(encoder->base.crtc); 278 210 enum pipe pipe = intel_crtc->pipe; 211 + struct i915_audio_component *acomp = dev_priv->audio_component; 279 212 const uint8_t *eld = connector->eld; 213 + struct intel_digital_port *intel_dig_port = 214 + enc_to_dig_port(&encoder->base); 215 + enum port port = intel_dig_port->port; 280 216 uint32_t tmp; 281 217 int len, i; 218 + int n, rate; 282 219 283 220 DRM_DEBUG_KMS("Enable audio codec on pipe %c, %u bytes ELD\n", 284 221 pipe_name(pipe), drm_eld_size(eld)); 222 + 223 + mutex_lock(&dev_priv->av_mutex); 285 224 286 225 /* Enable audio presence detect, invalidate ELD */ 287 226 tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD); ··· 321 246 /* Enable timestamps */ 322 247 tmp = I915_READ(HSW_AUD_CFG(pipe)); 323 248 tmp &= ~AUD_CONFIG_N_VALUE_INDEX; 324 - tmp &= ~AUD_CONFIG_N_PROG_ENABLE; 325 249 tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK; 326 250 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT)) 327 251 tmp |= AUD_CONFIG_N_VALUE_INDEX; 328 252 else 329 253 tmp |= audio_config_hdmi_pixel_clock(mode); 254 + 255 + tmp &= ~AUD_CONFIG_N_PROG_ENABLE; 256 + if (audio_rate_need_prog(intel_crtc, mode)) { 257 + if (!acomp) 258 + rate = 0; 259 + else if (port >= PORT_A && port <= PORT_E) 260 + rate = acomp->aud_sample_rate[port]; 261 + else { 262 + DRM_ERROR("invalid port: %d\n", port); 263 + rate = 0; 264 + } 265 + n = audio_config_get_n(mode, rate); 266 + if (n != 0) 267 + tmp = audio_config_setup_n_reg(n, tmp); 268 + else 269 + DRM_DEBUG_KMS("no suitable N value is found\n"); 270 + } 271 + 330 272 I915_WRITE(HSW_AUD_CFG(pipe), tmp); 273 + 274 + mutex_unlock(&dev_priv->av_mutex); 331 275 } 332 276 333 277 static void ilk_audio_codec_disable(struct intel_encoder *encoder) ··· 621 527 return ret; 622 528 } 623 529 530 + static int i915_audio_component_sync_audio_rate(struct device *dev, 531 + int port, int rate) 532 + { 533 + struct drm_i915_private *dev_priv = dev_to_i915(dev); 534 + struct drm_device *drm_dev = dev_priv->dev; 535 + struct intel_encoder *intel_encoder; 536 + struct intel_digital_port *intel_dig_port; 537 + struct intel_crtc *crtc; 538 + struct drm_display_mode *mode; 539 + struct i915_audio_component *acomp = dev_priv->audio_component; 540 + enum pipe pipe = -1; 541 + u32 tmp; 542 + int n; 543 + 544 + /* HSW, BDW SKL need this fix */ 545 + if (!IS_SKYLAKE(dev_priv) && 546 + !IS_BROADWELL(dev_priv) && 547 + !IS_HASWELL(dev_priv)) 548 + return 0; 549 + 550 + mutex_lock(&dev_priv->av_mutex); 551 + /* 1. get the pipe */ 552 + for_each_intel_encoder(drm_dev, intel_encoder) { 553 + if (intel_encoder->type != INTEL_OUTPUT_HDMI) 554 + continue; 555 + intel_dig_port = enc_to_dig_port(&intel_encoder->base); 556 + if (port == intel_dig_port->port) { 557 + crtc = to_intel_crtc(intel_encoder->base.crtc); 558 + if (!crtc) { 559 + DRM_DEBUG_KMS("%s: crtc is NULL\n", __func__); 560 + continue; 561 + } 562 + pipe = crtc->pipe; 563 + break; 564 + } 565 + } 566 + 567 + if (pipe == INVALID_PIPE) { 568 + DRM_DEBUG_KMS("no pipe for the port %c\n", port_name(port)); 569 + mutex_unlock(&dev_priv->av_mutex); 570 + return -ENODEV; 571 + } 572 + DRM_DEBUG_KMS("pipe %c connects port %c\n", 573 + pipe_name(pipe), port_name(port)); 574 + mode = &crtc->config->base.adjusted_mode; 575 + 576 + /* port must be valid now, otherwise the pipe will be invalid */ 577 + acomp->aud_sample_rate[port] = rate; 578 + 579 + /* 2. check whether to set the N/CTS/M manually or not */ 580 + if (!audio_rate_need_prog(crtc, mode)) { 581 + tmp = I915_READ(HSW_AUD_CFG(pipe)); 582 + tmp &= ~AUD_CONFIG_N_PROG_ENABLE; 583 + I915_WRITE(HSW_AUD_CFG(pipe), tmp); 584 + mutex_unlock(&dev_priv->av_mutex); 585 + return 0; 586 + } 587 + 588 + n = audio_config_get_n(mode, rate); 589 + if (n == 0) { 590 + DRM_DEBUG_KMS("Using automatic mode for N value on port %c\n", 591 + port_name(port)); 592 + tmp = I915_READ(HSW_AUD_CFG(pipe)); 593 + tmp &= ~AUD_CONFIG_N_PROG_ENABLE; 594 + I915_WRITE(HSW_AUD_CFG(pipe), tmp); 595 + mutex_unlock(&dev_priv->av_mutex); 596 + return 0; 597 + } 598 + 599 + /* 3. set the N/CTS/M */ 600 + tmp = I915_READ(HSW_AUD_CFG(pipe)); 601 + tmp = audio_config_setup_n_reg(n, tmp); 602 + I915_WRITE(HSW_AUD_CFG(pipe), tmp); 603 + 604 + mutex_unlock(&dev_priv->av_mutex); 605 + return 0; 606 + } 607 + 624 608 static const struct i915_audio_component_ops i915_audio_component_ops = { 625 609 .owner = THIS_MODULE, 626 610 .get_power = i915_audio_component_get_power, 627 611 .put_power = i915_audio_component_put_power, 628 612 .codec_wake_override = i915_audio_component_codec_wake_override, 629 613 .get_cdclk_freq = i915_audio_component_get_cdclk_freq, 614 + .sync_audio_rate = i915_audio_component_sync_audio_rate, 630 615 }; 631 616 632 617 static int i915_audio_component_bind(struct device *i915_dev, ··· 713 540 { 714 541 struct i915_audio_component *acomp = data; 715 542 struct drm_i915_private *dev_priv = dev_to_i915(i915_dev); 543 + int i; 716 544 717 545 if (WARN_ON(acomp->ops || acomp->dev)) 718 546 return -EEXIST; ··· 721 547 drm_modeset_lock_all(dev_priv->dev); 722 548 acomp->ops = &i915_audio_component_ops; 723 549 acomp->dev = i915_dev; 550 + BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS); 551 + for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++) 552 + acomp->aud_sample_rate[i] = 0; 724 553 dev_priv->audio_component = acomp; 725 554 drm_modeset_unlock_all(dev_priv->dev); 726 555
+17
include/drm/i915_component.h
··· 24 24 #ifndef _I915_COMPONENT_H_ 25 25 #define _I915_COMPONENT_H_ 26 26 27 + /* MAX_PORT is the number of port 28 + * It must be sync with I915_MAX_PORTS defined i915_drv.h 29 + * 5 should be enough as only HSW, BDW, SKL need such fix. 30 + */ 31 + #define MAX_PORTS 5 32 + 27 33 struct i915_audio_component { 28 34 struct device *dev; 35 + /** 36 + * @aud_sample_rate: the array of audio sample rate per port 37 + */ 38 + int aud_sample_rate[MAX_PORTS]; 29 39 30 40 const struct i915_audio_component_ops { 31 41 struct module *owner; ··· 43 33 void (*put_power)(struct device *); 44 34 void (*codec_wake_override)(struct device *, bool enable); 45 35 int (*get_cdclk_freq)(struct device *); 36 + /** 37 + * @sync_audio_rate: set n/cts based on the sample rate 38 + * 39 + * Called from audio driver. After audio driver sets the 40 + * sample rate, it will call this function to set n/cts 41 + */ 42 + int (*sync_audio_rate)(struct device *, int port, int rate); 46 43 } *ops; 47 44 48 45 const struct i915_audio_component_audio_ops {
+19
sound/pci/hda/patch_hdmi.c
··· 1775 1775 return non_pcm; 1776 1776 } 1777 1777 1778 + /* There is a fixed mapping between audio pin node and display port 1779 + * on current Intel platforms: 1780 + * Pin Widget 5 - PORT B (port = 1 in i915 driver) 1781 + * Pin Widget 6 - PORT C (port = 2 in i915 driver) 1782 + * Pin Widget 7 - PORT D (port = 3 in i915 driver) 1783 + */ 1784 + static int intel_pin2port(hda_nid_t pin_nid) 1785 + { 1786 + return pin_nid - 4; 1787 + } 1778 1788 1779 1789 /* 1780 1790 * HDMI callbacks ··· 1801 1791 int pin_idx = hinfo_to_pin_index(codec, hinfo); 1802 1792 struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx); 1803 1793 hda_nid_t pin_nid = per_pin->pin_nid; 1794 + struct snd_pcm_runtime *runtime = substream->runtime; 1795 + struct i915_audio_component *acomp = codec->bus->core.audio_component; 1804 1796 bool non_pcm; 1805 1797 int pinctl; 1806 1798 ··· 1818 1806 intel_verify_pin_cvt_connect(codec, per_pin); 1819 1807 intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx); 1820 1808 } 1809 + 1810 + /* Call sync_audio_rate to set the N/CTS/M manually if necessary */ 1811 + /* Todo: add DP1.2 MST audio support later */ 1812 + if (acomp && acomp->ops && acomp->ops->sync_audio_rate) 1813 + acomp->ops->sync_audio_rate(acomp->dev, 1814 + intel_pin2port(pin_nid), 1815 + runtime->rate); 1821 1816 1822 1817 non_pcm = check_non_pcm_per_cvt(codec, cvt_nid); 1823 1818 mutex_lock(&per_pin->lock);