Home | History | Annotate | Line # | Download | only in display
      1  1.3  riastrad /*	$NetBSD: intel_audio.c,v 1.3 2021/12/19 11:38:03 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2014 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  1.1  riastrad  * DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_audio.c,v 1.3 2021/12/19 11:38:03 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/component.h>
     30  1.1  riastrad #include <linux/kernel.h>
     31  1.1  riastrad 
     32  1.1  riastrad #include <drm/drm_edid.h>
     33  1.1  riastrad #include <drm/i915_component.h>
     34  1.1  riastrad 
     35  1.1  riastrad #include "i915_drv.h"
     36  1.1  riastrad #include "intel_atomic.h"
     37  1.1  riastrad #include "intel_audio.h"
     38  1.1  riastrad #include "intel_display_types.h"
     39  1.1  riastrad #include "intel_lpe_audio.h"
     40  1.1  riastrad 
     41  1.1  riastrad /**
     42  1.1  riastrad  * DOC: High Definition Audio over HDMI and Display Port
     43  1.1  riastrad  *
     44  1.1  riastrad  * The graphics and audio drivers together support High Definition Audio over
     45  1.1  riastrad  * HDMI and Display Port. The audio programming sequences are divided into audio
     46  1.1  riastrad  * codec and controller enable and disable sequences. The graphics driver
     47  1.1  riastrad  * handles the audio codec sequences, while the audio driver handles the audio
     48  1.1  riastrad  * controller sequences.
     49  1.1  riastrad  *
     50  1.1  riastrad  * The disable sequences must be performed before disabling the transcoder or
     51  1.1  riastrad  * port. The enable sequences may only be performed after enabling the
     52  1.1  riastrad  * transcoder and port, and after completed link training. Therefore the audio
     53  1.1  riastrad  * enable/disable sequences are part of the modeset sequence.
     54  1.1  riastrad  *
     55  1.1  riastrad  * The codec and controller sequences could be done either parallel or serial,
     56  1.1  riastrad  * but generally the ELDV/PD change in the codec sequence indicates to the audio
     57  1.1  riastrad  * driver that the controller sequence should start. Indeed, most of the
     58  1.1  riastrad  * co-operation between the graphics and audio drivers is handled via audio
     59  1.1  riastrad  * related registers. (The notable exception is the power management, not
     60  1.1  riastrad  * covered here.)
     61  1.1  riastrad  *
     62  1.1  riastrad  * The struct &i915_audio_component is used to interact between the graphics
     63  1.1  riastrad  * and audio drivers. The struct &i915_audio_component_ops @ops in it is
     64  1.1  riastrad  * defined in graphics driver and called in audio driver. The
     65  1.1  riastrad  * struct &i915_audio_component_audio_ops @audio_ops is called from i915 driver.
     66  1.1  riastrad  */
     67  1.1  riastrad 
     68  1.1  riastrad /* DP N/M table */
     69  1.1  riastrad #define LC_810M	810000
     70  1.1  riastrad #define LC_540M	540000
     71  1.1  riastrad #define LC_270M	270000
     72  1.1  riastrad #define LC_162M	162000
     73  1.1  riastrad 
     74  1.1  riastrad struct dp_aud_n_m {
     75  1.1  riastrad 	int sample_rate;
     76  1.1  riastrad 	int clock;
     77  1.1  riastrad 	u16 m;
     78  1.1  riastrad 	u16 n;
     79  1.1  riastrad };
     80  1.1  riastrad 
     81  1.1  riastrad struct hdmi_aud_ncts {
     82  1.1  riastrad 	int sample_rate;
     83  1.1  riastrad 	int clock;
     84  1.1  riastrad 	int n;
     85  1.1  riastrad 	int cts;
     86  1.1  riastrad };
     87  1.1  riastrad 
     88  1.1  riastrad /* Values according to DP 1.4 Table 2-104 */
     89  1.1  riastrad static const struct dp_aud_n_m dp_aud_n_m[] = {
     90  1.1  riastrad 	{ 32000, LC_162M, 1024, 10125 },
     91  1.1  riastrad 	{ 44100, LC_162M, 784, 5625 },
     92  1.1  riastrad 	{ 48000, LC_162M, 512, 3375 },
     93  1.1  riastrad 	{ 64000, LC_162M, 2048, 10125 },
     94  1.1  riastrad 	{ 88200, LC_162M, 1568, 5625 },
     95  1.1  riastrad 	{ 96000, LC_162M, 1024, 3375 },
     96  1.1  riastrad 	{ 128000, LC_162M, 4096, 10125 },
     97  1.1  riastrad 	{ 176400, LC_162M, 3136, 5625 },
     98  1.1  riastrad 	{ 192000, LC_162M, 2048, 3375 },
     99  1.1  riastrad 	{ 32000, LC_270M, 1024, 16875 },
    100  1.1  riastrad 	{ 44100, LC_270M, 784, 9375 },
    101  1.1  riastrad 	{ 48000, LC_270M, 512, 5625 },
    102  1.1  riastrad 	{ 64000, LC_270M, 2048, 16875 },
    103  1.1  riastrad 	{ 88200, LC_270M, 1568, 9375 },
    104  1.1  riastrad 	{ 96000, LC_270M, 1024, 5625 },
    105  1.1  riastrad 	{ 128000, LC_270M, 4096, 16875 },
    106  1.1  riastrad 	{ 176400, LC_270M, 3136, 9375 },
    107  1.1  riastrad 	{ 192000, LC_270M, 2048, 5625 },
    108  1.1  riastrad 	{ 32000, LC_540M, 1024, 33750 },
    109  1.1  riastrad 	{ 44100, LC_540M, 784, 18750 },
    110  1.1  riastrad 	{ 48000, LC_540M, 512, 11250 },
    111  1.1  riastrad 	{ 64000, LC_540M, 2048, 33750 },
    112  1.1  riastrad 	{ 88200, LC_540M, 1568, 18750 },
    113  1.1  riastrad 	{ 96000, LC_540M, 1024, 11250 },
    114  1.1  riastrad 	{ 128000, LC_540M, 4096, 33750 },
    115  1.1  riastrad 	{ 176400, LC_540M, 3136, 18750 },
    116  1.1  riastrad 	{ 192000, LC_540M, 2048, 11250 },
    117  1.1  riastrad 	{ 32000, LC_810M, 1024, 50625 },
    118  1.1  riastrad 	{ 44100, LC_810M, 784, 28125 },
    119  1.1  riastrad 	{ 48000, LC_810M, 512, 16875 },
    120  1.1  riastrad 	{ 64000, LC_810M, 2048, 50625 },
    121  1.1  riastrad 	{ 88200, LC_810M, 1568, 28125 },
    122  1.1  riastrad 	{ 96000, LC_810M, 1024, 16875 },
    123  1.1  riastrad 	{ 128000, LC_810M, 4096, 50625 },
    124  1.1  riastrad 	{ 176400, LC_810M, 3136, 28125 },
    125  1.1  riastrad 	{ 192000, LC_810M, 2048, 16875 },
    126  1.1  riastrad };
    127  1.1  riastrad 
    128  1.1  riastrad static const struct dp_aud_n_m *
    129  1.1  riastrad audio_config_dp_get_n_m(const struct intel_crtc_state *crtc_state, int rate)
    130  1.1  riastrad {
    131  1.1  riastrad 	int i;
    132  1.1  riastrad 
    133  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(dp_aud_n_m); i++) {
    134  1.1  riastrad 		if (rate == dp_aud_n_m[i].sample_rate &&
    135  1.1  riastrad 		    crtc_state->port_clock == dp_aud_n_m[i].clock)
    136  1.1  riastrad 			return &dp_aud_n_m[i];
    137  1.1  riastrad 	}
    138  1.1  riastrad 
    139  1.1  riastrad 	return NULL;
    140  1.1  riastrad }
    141  1.1  riastrad 
    142  1.1  riastrad static const struct {
    143  1.1  riastrad 	int clock;
    144  1.1  riastrad 	u32 config;
    145  1.1  riastrad } hdmi_audio_clock[] = {
    146  1.1  riastrad 	{ 25175, AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
    147  1.1  riastrad 	{ 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
    148  1.1  riastrad 	{ 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
    149  1.1  riastrad 	{ 27027, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
    150  1.1  riastrad 	{ 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
    151  1.1  riastrad 	{ 54054, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
    152  1.1  riastrad 	{ 74176, AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
    153  1.1  riastrad 	{ 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
    154  1.1  riastrad 	{ 148352, AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
    155  1.1  riastrad 	{ 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
    156  1.1  riastrad };
    157  1.1  riastrad 
    158  1.1  riastrad /* HDMI N/CTS table */
    159  1.1  riastrad #define TMDS_297M 297000
    160  1.1  riastrad #define TMDS_296M 296703
    161  1.1  riastrad #define TMDS_594M 594000
    162  1.1  riastrad #define TMDS_593M 593407
    163  1.1  riastrad 
    164  1.1  riastrad static const struct hdmi_aud_ncts hdmi_aud_ncts_24bpp[] = {
    165  1.1  riastrad 	{ 32000, TMDS_296M, 5824, 421875 },
    166  1.1  riastrad 	{ 32000, TMDS_297M, 3072, 222750 },
    167  1.1  riastrad 	{ 32000, TMDS_593M, 5824, 843750 },
    168  1.1  riastrad 	{ 32000, TMDS_594M, 3072, 445500 },
    169  1.1  riastrad 	{ 44100, TMDS_296M, 4459, 234375 },
    170  1.1  riastrad 	{ 44100, TMDS_297M, 4704, 247500 },
    171  1.1  riastrad 	{ 44100, TMDS_593M, 8918, 937500 },
    172  1.1  riastrad 	{ 44100, TMDS_594M, 9408, 990000 },
    173  1.1  riastrad 	{ 88200, TMDS_296M, 8918, 234375 },
    174  1.1  riastrad 	{ 88200, TMDS_297M, 9408, 247500 },
    175  1.1  riastrad 	{ 88200, TMDS_593M, 17836, 937500 },
    176  1.1  riastrad 	{ 88200, TMDS_594M, 18816, 990000 },
    177  1.1  riastrad 	{ 176400, TMDS_296M, 17836, 234375 },
    178  1.1  riastrad 	{ 176400, TMDS_297M, 18816, 247500 },
    179  1.1  riastrad 	{ 176400, TMDS_593M, 35672, 937500 },
    180  1.1  riastrad 	{ 176400, TMDS_594M, 37632, 990000 },
    181  1.1  riastrad 	{ 48000, TMDS_296M, 5824, 281250 },
    182  1.1  riastrad 	{ 48000, TMDS_297M, 5120, 247500 },
    183  1.1  riastrad 	{ 48000, TMDS_593M, 5824, 562500 },
    184  1.1  riastrad 	{ 48000, TMDS_594M, 6144, 594000 },
    185  1.1  riastrad 	{ 96000, TMDS_296M, 11648, 281250 },
    186  1.1  riastrad 	{ 96000, TMDS_297M, 10240, 247500 },
    187  1.1  riastrad 	{ 96000, TMDS_593M, 11648, 562500 },
    188  1.1  riastrad 	{ 96000, TMDS_594M, 12288, 594000 },
    189  1.1  riastrad 	{ 192000, TMDS_296M, 23296, 281250 },
    190  1.1  riastrad 	{ 192000, TMDS_297M, 20480, 247500 },
    191  1.1  riastrad 	{ 192000, TMDS_593M, 23296, 562500 },
    192  1.1  riastrad 	{ 192000, TMDS_594M, 24576, 594000 },
    193  1.1  riastrad };
    194  1.1  riastrad 
    195  1.1  riastrad /* Appendix C - N & CTS values for deep color from HDMI 2.0 spec*/
    196  1.1  riastrad /* HDMI N/CTS table for 10 bit deep color(30 bpp)*/
    197  1.1  riastrad #define TMDS_371M 371250
    198  1.1  riastrad #define TMDS_370M 370878
    199  1.1  riastrad 
    200  1.1  riastrad static const struct hdmi_aud_ncts hdmi_aud_ncts_30bpp[] = {
    201  1.1  riastrad 	{ 32000, TMDS_370M, 5824, 527344 },
    202  1.1  riastrad 	{ 32000, TMDS_371M, 6144, 556875 },
    203  1.1  riastrad 	{ 44100, TMDS_370M, 8918, 585938 },
    204  1.1  riastrad 	{ 44100, TMDS_371M, 4704, 309375 },
    205  1.1  riastrad 	{ 88200, TMDS_370M, 17836, 585938 },
    206  1.1  riastrad 	{ 88200, TMDS_371M, 9408, 309375 },
    207  1.1  riastrad 	{ 176400, TMDS_370M, 35672, 585938 },
    208  1.1  riastrad 	{ 176400, TMDS_371M, 18816, 309375 },
    209  1.1  riastrad 	{ 48000, TMDS_370M, 11648, 703125 },
    210  1.1  riastrad 	{ 48000, TMDS_371M, 5120, 309375 },
    211  1.1  riastrad 	{ 96000, TMDS_370M, 23296, 703125 },
    212  1.1  riastrad 	{ 96000, TMDS_371M, 10240, 309375 },
    213  1.1  riastrad 	{ 192000, TMDS_370M, 46592, 703125 },
    214  1.1  riastrad 	{ 192000, TMDS_371M, 20480, 309375 },
    215  1.1  riastrad };
    216  1.1  riastrad 
    217  1.1  riastrad /* HDMI N/CTS table for 12 bit deep color(36 bpp)*/
    218  1.1  riastrad #define TMDS_445_5M 445500
    219  1.1  riastrad #define TMDS_445M 445054
    220  1.1  riastrad 
    221  1.1  riastrad static const struct hdmi_aud_ncts hdmi_aud_ncts_36bpp[] = {
    222  1.1  riastrad 	{ 32000, TMDS_445M, 5824, 632813 },
    223  1.1  riastrad 	{ 32000, TMDS_445_5M, 4096, 445500 },
    224  1.1  riastrad 	{ 44100, TMDS_445M, 8918, 703125 },
    225  1.1  riastrad 	{ 44100, TMDS_445_5M, 4704, 371250 },
    226  1.1  riastrad 	{ 88200, TMDS_445M, 17836, 703125 },
    227  1.1  riastrad 	{ 88200, TMDS_445_5M, 9408, 371250 },
    228  1.1  riastrad 	{ 176400, TMDS_445M, 35672, 703125 },
    229  1.1  riastrad 	{ 176400, TMDS_445_5M, 18816, 371250 },
    230  1.1  riastrad 	{ 48000, TMDS_445M, 5824, 421875 },
    231  1.1  riastrad 	{ 48000, TMDS_445_5M, 5120, 371250 },
    232  1.1  riastrad 	{ 96000, TMDS_445M, 11648, 421875 },
    233  1.1  riastrad 	{ 96000, TMDS_445_5M, 10240, 371250 },
    234  1.1  riastrad 	{ 192000, TMDS_445M, 23296, 421875 },
    235  1.1  riastrad 	{ 192000, TMDS_445_5M, 20480, 371250 },
    236  1.1  riastrad };
    237  1.1  riastrad 
    238  1.1  riastrad /* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
    239  1.1  riastrad static u32 audio_config_hdmi_pixel_clock(const struct intel_crtc_state *crtc_state)
    240  1.1  riastrad {
    241  1.1  riastrad 	const struct drm_display_mode *adjusted_mode =
    242  1.1  riastrad 		&crtc_state->hw.adjusted_mode;
    243  1.1  riastrad 	int i;
    244  1.1  riastrad 
    245  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
    246  1.1  riastrad 		if (adjusted_mode->crtc_clock == hdmi_audio_clock[i].clock)
    247  1.1  riastrad 			break;
    248  1.1  riastrad 	}
    249  1.1  riastrad 
    250  1.1  riastrad 	if (i == ARRAY_SIZE(hdmi_audio_clock)) {
    251  1.1  riastrad 		DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n",
    252  1.1  riastrad 			      adjusted_mode->crtc_clock);
    253  1.1  riastrad 		i = 1;
    254  1.1  riastrad 	}
    255  1.1  riastrad 
    256  1.1  riastrad 	DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
    257  1.1  riastrad 		      hdmi_audio_clock[i].clock,
    258  1.1  riastrad 		      hdmi_audio_clock[i].config);
    259  1.1  riastrad 
    260  1.1  riastrad 	return hdmi_audio_clock[i].config;
    261  1.1  riastrad }
    262  1.1  riastrad 
    263  1.1  riastrad static int audio_config_hdmi_get_n(const struct intel_crtc_state *crtc_state,
    264  1.1  riastrad 				   int rate)
    265  1.1  riastrad {
    266  1.1  riastrad 	const struct hdmi_aud_ncts *hdmi_ncts_table;
    267  1.1  riastrad 	int i, size;
    268  1.1  riastrad 
    269  1.1  riastrad 	if (crtc_state->pipe_bpp == 36) {
    270  1.1  riastrad 		hdmi_ncts_table = hdmi_aud_ncts_36bpp;
    271  1.1  riastrad 		size = ARRAY_SIZE(hdmi_aud_ncts_36bpp);
    272  1.1  riastrad 	} else if (crtc_state->pipe_bpp == 30) {
    273  1.1  riastrad 		hdmi_ncts_table = hdmi_aud_ncts_30bpp;
    274  1.1  riastrad 		size = ARRAY_SIZE(hdmi_aud_ncts_30bpp);
    275  1.1  riastrad 	} else {
    276  1.1  riastrad 		hdmi_ncts_table = hdmi_aud_ncts_24bpp;
    277  1.1  riastrad 		size = ARRAY_SIZE(hdmi_aud_ncts_24bpp);
    278  1.1  riastrad 	}
    279  1.1  riastrad 
    280  1.1  riastrad 	for (i = 0; i < size; i++) {
    281  1.1  riastrad 		if (rate == hdmi_ncts_table[i].sample_rate &&
    282  1.1  riastrad 		    crtc_state->port_clock == hdmi_ncts_table[i].clock) {
    283  1.1  riastrad 			return hdmi_ncts_table[i].n;
    284  1.1  riastrad 		}
    285  1.1  riastrad 	}
    286  1.1  riastrad 	return 0;
    287  1.1  riastrad }
    288  1.1  riastrad 
    289  1.1  riastrad static bool intel_eld_uptodate(struct drm_connector *connector,
    290  1.1  riastrad 			       i915_reg_t reg_eldv, u32 bits_eldv,
    291  1.1  riastrad 			       i915_reg_t reg_elda, u32 bits_elda,
    292  1.1  riastrad 			       i915_reg_t reg_edid)
    293  1.1  riastrad {
    294  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
    295  1.1  riastrad 	const u8 *eld = connector->eld;
    296  1.1  riastrad 	u32 tmp;
    297  1.1  riastrad 	int i;
    298  1.1  riastrad 
    299  1.1  riastrad 	tmp = I915_READ(reg_eldv);
    300  1.1  riastrad 	tmp &= bits_eldv;
    301  1.1  riastrad 
    302  1.1  riastrad 	if (!tmp)
    303  1.1  riastrad 		return false;
    304  1.1  riastrad 
    305  1.1  riastrad 	tmp = I915_READ(reg_elda);
    306  1.1  riastrad 	tmp &= ~bits_elda;
    307  1.1  riastrad 	I915_WRITE(reg_elda, tmp);
    308  1.1  riastrad 
    309  1.1  riastrad 	for (i = 0; i < drm_eld_size(eld) / 4; i++)
    310  1.1  riastrad 		if (I915_READ(reg_edid) != *((const u32 *)eld + i))
    311  1.1  riastrad 			return false;
    312  1.1  riastrad 
    313  1.1  riastrad 	return true;
    314  1.1  riastrad }
    315  1.1  riastrad 
    316  1.1  riastrad static void g4x_audio_codec_disable(struct intel_encoder *encoder,
    317  1.1  riastrad 				    const struct intel_crtc_state *old_crtc_state,
    318  1.1  riastrad 				    const struct drm_connector_state *old_conn_state)
    319  1.1  riastrad {
    320  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    321  1.1  riastrad 	u32 eldv, tmp;
    322  1.1  riastrad 
    323  1.1  riastrad 	DRM_DEBUG_KMS("Disable audio codec\n");
    324  1.1  riastrad 
    325  1.1  riastrad 	tmp = I915_READ(G4X_AUD_VID_DID);
    326  1.1  riastrad 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
    327  1.1  riastrad 		eldv = G4X_ELDV_DEVCL_DEVBLC;
    328  1.1  riastrad 	else
    329  1.1  riastrad 		eldv = G4X_ELDV_DEVCTG;
    330  1.1  riastrad 
    331  1.1  riastrad 	/* Invalidate ELD */
    332  1.1  riastrad 	tmp = I915_READ(G4X_AUD_CNTL_ST);
    333  1.1  riastrad 	tmp &= ~eldv;
    334  1.1  riastrad 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
    335  1.1  riastrad }
    336  1.1  riastrad 
    337  1.1  riastrad static void g4x_audio_codec_enable(struct intel_encoder *encoder,
    338  1.1  riastrad 				   const struct intel_crtc_state *crtc_state,
    339  1.1  riastrad 				   const struct drm_connector_state *conn_state)
    340  1.1  riastrad {
    341  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    342  1.1  riastrad 	struct drm_connector *connector = conn_state->connector;
    343  1.1  riastrad 	const u8 *eld = connector->eld;
    344  1.1  riastrad 	u32 eldv;
    345  1.1  riastrad 	u32 tmp;
    346  1.1  riastrad 	int len, i;
    347  1.1  riastrad 
    348  1.1  riastrad 	DRM_DEBUG_KMS("Enable audio codec, %u bytes ELD\n", drm_eld_size(eld));
    349  1.1  riastrad 
    350  1.1  riastrad 	tmp = I915_READ(G4X_AUD_VID_DID);
    351  1.1  riastrad 	if (tmp == INTEL_AUDIO_DEVBLC || tmp == INTEL_AUDIO_DEVCL)
    352  1.1  riastrad 		eldv = G4X_ELDV_DEVCL_DEVBLC;
    353  1.1  riastrad 	else
    354  1.1  riastrad 		eldv = G4X_ELDV_DEVCTG;
    355  1.1  riastrad 
    356  1.1  riastrad 	if (intel_eld_uptodate(connector,
    357  1.1  riastrad 			       G4X_AUD_CNTL_ST, eldv,
    358  1.1  riastrad 			       G4X_AUD_CNTL_ST, G4X_ELD_ADDR_MASK,
    359  1.1  riastrad 			       G4X_HDMIW_HDMIEDID))
    360  1.1  riastrad 		return;
    361  1.1  riastrad 
    362  1.1  riastrad 	tmp = I915_READ(G4X_AUD_CNTL_ST);
    363  1.1  riastrad 	tmp &= ~(eldv | G4X_ELD_ADDR_MASK);
    364  1.1  riastrad 	len = (tmp >> 9) & 0x1f;		/* ELD buffer size */
    365  1.1  riastrad 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
    366  1.1  riastrad 
    367  1.1  riastrad 	len = min(drm_eld_size(eld) / 4, len);
    368  1.1  riastrad 	DRM_DEBUG_DRIVER("ELD size %d\n", len);
    369  1.1  riastrad 	for (i = 0; i < len; i++)
    370  1.1  riastrad 		I915_WRITE(G4X_HDMIW_HDMIEDID, *((const u32 *)eld + i));
    371  1.1  riastrad 
    372  1.1  riastrad 	tmp = I915_READ(G4X_AUD_CNTL_ST);
    373  1.1  riastrad 	tmp |= eldv;
    374  1.1  riastrad 	I915_WRITE(G4X_AUD_CNTL_ST, tmp);
    375  1.1  riastrad }
    376  1.1  riastrad 
    377  1.1  riastrad static void
    378  1.1  riastrad hsw_dp_audio_config_update(struct intel_encoder *encoder,
    379  1.1  riastrad 			   const struct intel_crtc_state *crtc_state)
    380  1.1  riastrad {
    381  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    382  1.1  riastrad 	struct i915_audio_component *acomp = dev_priv->audio_component;
    383  1.1  riastrad 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
    384  1.1  riastrad 	enum port port = encoder->port;
    385  1.1  riastrad 	const struct dp_aud_n_m *nm;
    386  1.1  riastrad 	int rate;
    387  1.1  riastrad 	u32 tmp;
    388  1.1  riastrad 
    389  1.1  riastrad 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
    390  1.1  riastrad 	nm = audio_config_dp_get_n_m(crtc_state, rate);
    391  1.1  riastrad 	if (nm)
    392  1.1  riastrad 		DRM_DEBUG_KMS("using Maud %u, Naud %u\n", nm->m, nm->n);
    393  1.1  riastrad 	else
    394  1.1  riastrad 		DRM_DEBUG_KMS("using automatic Maud, Naud\n");
    395  1.1  riastrad 
    396  1.1  riastrad 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
    397  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
    398  1.1  riastrad 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
    399  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
    400  1.1  riastrad 	tmp |= AUD_CONFIG_N_VALUE_INDEX;
    401  1.1  riastrad 
    402  1.1  riastrad 	if (nm) {
    403  1.1  riastrad 		tmp &= ~AUD_CONFIG_N_MASK;
    404  1.1  riastrad 		tmp |= AUD_CONFIG_N(nm->n);
    405  1.1  riastrad 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
    406  1.1  riastrad 	}
    407  1.1  riastrad 
    408  1.1  riastrad 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
    409  1.1  riastrad 
    410  1.1  riastrad 	tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
    411  1.1  riastrad 	tmp &= ~AUD_CONFIG_M_MASK;
    412  1.1  riastrad 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
    413  1.1  riastrad 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
    414  1.1  riastrad 
    415  1.1  riastrad 	if (nm) {
    416  1.1  riastrad 		tmp |= nm->m;
    417  1.1  riastrad 		tmp |= AUD_M_CTS_M_VALUE_INDEX;
    418  1.1  riastrad 		tmp |= AUD_M_CTS_M_PROG_ENABLE;
    419  1.1  riastrad 	}
    420  1.1  riastrad 
    421  1.1  riastrad 	I915_WRITE(HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
    422  1.1  riastrad }
    423  1.1  riastrad 
    424  1.1  riastrad static void
    425  1.1  riastrad hsw_hdmi_audio_config_update(struct intel_encoder *encoder,
    426  1.1  riastrad 			     const struct intel_crtc_state *crtc_state)
    427  1.1  riastrad {
    428  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    429  1.1  riastrad 	struct i915_audio_component *acomp = dev_priv->audio_component;
    430  1.1  riastrad 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
    431  1.1  riastrad 	enum port port = encoder->port;
    432  1.1  riastrad 	int n, rate;
    433  1.1  riastrad 	u32 tmp;
    434  1.1  riastrad 
    435  1.1  riastrad 	rate = acomp ? acomp->aud_sample_rate[port] : 0;
    436  1.1  riastrad 
    437  1.1  riastrad 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
    438  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
    439  1.1  riastrad 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
    440  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
    441  1.1  riastrad 	tmp |= audio_config_hdmi_pixel_clock(crtc_state);
    442  1.1  riastrad 
    443  1.1  riastrad 	n = audio_config_hdmi_get_n(crtc_state, rate);
    444  1.1  riastrad 	if (n != 0) {
    445  1.1  riastrad 		DRM_DEBUG_KMS("using N %d\n", n);
    446  1.1  riastrad 
    447  1.1  riastrad 		tmp &= ~AUD_CONFIG_N_MASK;
    448  1.1  riastrad 		tmp |= AUD_CONFIG_N(n);
    449  1.1  riastrad 		tmp |= AUD_CONFIG_N_PROG_ENABLE;
    450  1.1  riastrad 	} else {
    451  1.1  riastrad 		DRM_DEBUG_KMS("using automatic N\n");
    452  1.1  riastrad 	}
    453  1.1  riastrad 
    454  1.1  riastrad 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
    455  1.1  riastrad 
    456  1.1  riastrad 	/*
    457  1.1  riastrad 	 * Let's disable "Enable CTS or M Prog bit"
    458  1.1  riastrad 	 * and let HW calculate the value
    459  1.1  riastrad 	 */
    460  1.1  riastrad 	tmp = I915_READ(HSW_AUD_M_CTS_ENABLE(cpu_transcoder));
    461  1.1  riastrad 	tmp &= ~AUD_M_CTS_M_PROG_ENABLE;
    462  1.1  riastrad 	tmp &= ~AUD_M_CTS_M_VALUE_INDEX;
    463  1.1  riastrad 	I915_WRITE(HSW_AUD_M_CTS_ENABLE(cpu_transcoder), tmp);
    464  1.1  riastrad }
    465  1.1  riastrad 
    466  1.1  riastrad static void
    467  1.1  riastrad hsw_audio_config_update(struct intel_encoder *encoder,
    468  1.1  riastrad 			const struct intel_crtc_state *crtc_state)
    469  1.1  riastrad {
    470  1.1  riastrad 	if (intel_crtc_has_dp_encoder(crtc_state))
    471  1.1  riastrad 		hsw_dp_audio_config_update(encoder, crtc_state);
    472  1.1  riastrad 	else
    473  1.1  riastrad 		hsw_hdmi_audio_config_update(encoder, crtc_state);
    474  1.1  riastrad }
    475  1.1  riastrad 
    476  1.1  riastrad static void hsw_audio_codec_disable(struct intel_encoder *encoder,
    477  1.1  riastrad 				    const struct intel_crtc_state *old_crtc_state,
    478  1.1  riastrad 				    const struct drm_connector_state *old_conn_state)
    479  1.1  riastrad {
    480  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    481  1.1  riastrad 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
    482  1.1  riastrad 	u32 tmp;
    483  1.1  riastrad 
    484  1.1  riastrad 	DRM_DEBUG_KMS("Disable audio codec on transcoder %s\n",
    485  1.1  riastrad 		      transcoder_name(cpu_transcoder));
    486  1.1  riastrad 
    487  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
    488  1.1  riastrad 
    489  1.1  riastrad 	/* Disable timestamps */
    490  1.1  riastrad 	tmp = I915_READ(HSW_AUD_CFG(cpu_transcoder));
    491  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
    492  1.1  riastrad 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
    493  1.1  riastrad 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
    494  1.1  riastrad 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
    495  1.1  riastrad 	if (intel_crtc_has_dp_encoder(old_crtc_state))
    496  1.1  riastrad 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
    497  1.1  riastrad 	I915_WRITE(HSW_AUD_CFG(cpu_transcoder), tmp);
    498  1.1  riastrad 
    499  1.1  riastrad 	/* Invalidate ELD */
    500  1.1  riastrad 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
    501  1.1  riastrad 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
    502  1.1  riastrad 	tmp &= ~AUDIO_OUTPUT_ENABLE(cpu_transcoder);
    503  1.1  riastrad 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
    504  1.1  riastrad 
    505  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
    506  1.1  riastrad }
    507  1.1  riastrad 
    508  1.1  riastrad static void hsw_audio_codec_enable(struct intel_encoder *encoder,
    509  1.1  riastrad 				   const struct intel_crtc_state *crtc_state,
    510  1.1  riastrad 				   const struct drm_connector_state *conn_state)
    511  1.1  riastrad {
    512  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    513  1.1  riastrad 	struct drm_connector *connector = conn_state->connector;
    514  1.1  riastrad 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
    515  1.1  riastrad 	const u8 *eld = connector->eld;
    516  1.1  riastrad 	u32 tmp;
    517  1.1  riastrad 	int len, i;
    518  1.1  riastrad 
    519  1.1  riastrad 	DRM_DEBUG_KMS("Enable audio codec on transcoder %s, %u bytes ELD\n",
    520  1.1  riastrad 		      transcoder_name(cpu_transcoder), drm_eld_size(eld));
    521  1.1  riastrad 
    522  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
    523  1.1  riastrad 
    524  1.1  riastrad 	/* Enable audio presence detect, invalidate ELD */
    525  1.1  riastrad 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
    526  1.1  riastrad 	tmp |= AUDIO_OUTPUT_ENABLE(cpu_transcoder);
    527  1.1  riastrad 	tmp &= ~AUDIO_ELD_VALID(cpu_transcoder);
    528  1.1  riastrad 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
    529  1.1  riastrad 
    530  1.1  riastrad 	/*
    531  1.1  riastrad 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
    532  1.1  riastrad 	 * disabled during the mode set. The proper fix would be to push the
    533  1.1  riastrad 	 * rest of the setup into a vblank work item, queued here, but the
    534  1.1  riastrad 	 * infrastructure is not there yet.
    535  1.1  riastrad 	 */
    536  1.1  riastrad 
    537  1.1  riastrad 	/* Reset ELD write address */
    538  1.1  riastrad 	tmp = I915_READ(HSW_AUD_DIP_ELD_CTRL(cpu_transcoder));
    539  1.1  riastrad 	tmp &= ~IBX_ELD_ADDRESS_MASK;
    540  1.1  riastrad 	I915_WRITE(HSW_AUD_DIP_ELD_CTRL(cpu_transcoder), tmp);
    541  1.1  riastrad 
    542  1.1  riastrad 	/* Up to 84 bytes of hw ELD buffer */
    543  1.1  riastrad 	len = min(drm_eld_size(eld), 84);
    544  1.1  riastrad 	for (i = 0; i < len / 4; i++)
    545  1.1  riastrad 		I915_WRITE(HSW_AUD_EDID_DATA(cpu_transcoder), *((const u32 *)eld + i));
    546  1.1  riastrad 
    547  1.1  riastrad 	/* ELD valid */
    548  1.1  riastrad 	tmp = I915_READ(HSW_AUD_PIN_ELD_CP_VLD);
    549  1.1  riastrad 	tmp |= AUDIO_ELD_VALID(cpu_transcoder);
    550  1.1  riastrad 	I915_WRITE(HSW_AUD_PIN_ELD_CP_VLD, tmp);
    551  1.1  riastrad 
    552  1.1  riastrad 	/* Enable timestamps */
    553  1.1  riastrad 	hsw_audio_config_update(encoder, crtc_state);
    554  1.1  riastrad 
    555  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
    556  1.1  riastrad }
    557  1.1  riastrad 
    558  1.1  riastrad static void ilk_audio_codec_disable(struct intel_encoder *encoder,
    559  1.1  riastrad 				    const struct intel_crtc_state *old_crtc_state,
    560  1.1  riastrad 				    const struct drm_connector_state *old_conn_state)
    561  1.1  riastrad {
    562  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    563  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
    564  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    565  1.1  riastrad 	enum port port = encoder->port;
    566  1.1  riastrad 	u32 tmp, eldv;
    567  1.1  riastrad 	i915_reg_t aud_config, aud_cntrl_st2;
    568  1.1  riastrad 
    569  1.1  riastrad 	DRM_DEBUG_KMS("Disable audio codec on [ENCODER:%d:%s], pipe %c\n",
    570  1.1  riastrad 		      encoder->base.base.id, encoder->base.name,
    571  1.1  riastrad 		      pipe_name(pipe));
    572  1.1  riastrad 
    573  1.1  riastrad 	if (WARN_ON(port == PORT_A))
    574  1.1  riastrad 		return;
    575  1.1  riastrad 
    576  1.1  riastrad 	if (HAS_PCH_IBX(dev_priv)) {
    577  1.1  riastrad 		aud_config = IBX_AUD_CFG(pipe);
    578  1.1  riastrad 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
    579  1.1  riastrad 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
    580  1.1  riastrad 		aud_config = VLV_AUD_CFG(pipe);
    581  1.1  riastrad 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
    582  1.1  riastrad 	} else {
    583  1.1  riastrad 		aud_config = CPT_AUD_CFG(pipe);
    584  1.1  riastrad 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
    585  1.1  riastrad 	}
    586  1.1  riastrad 
    587  1.1  riastrad 	/* Disable timestamps */
    588  1.1  riastrad 	tmp = I915_READ(aud_config);
    589  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
    590  1.1  riastrad 	tmp |= AUD_CONFIG_N_PROG_ENABLE;
    591  1.1  riastrad 	tmp &= ~AUD_CONFIG_UPPER_N_MASK;
    592  1.1  riastrad 	tmp &= ~AUD_CONFIG_LOWER_N_MASK;
    593  1.1  riastrad 	if (intel_crtc_has_dp_encoder(old_crtc_state))
    594  1.1  riastrad 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
    595  1.1  riastrad 	I915_WRITE(aud_config, tmp);
    596  1.1  riastrad 
    597  1.1  riastrad 	eldv = IBX_ELD_VALID(port);
    598  1.1  riastrad 
    599  1.1  riastrad 	/* Invalidate ELD */
    600  1.1  riastrad 	tmp = I915_READ(aud_cntrl_st2);
    601  1.1  riastrad 	tmp &= ~eldv;
    602  1.1  riastrad 	I915_WRITE(aud_cntrl_st2, tmp);
    603  1.1  riastrad }
    604  1.1  riastrad 
    605  1.1  riastrad static void ilk_audio_codec_enable(struct intel_encoder *encoder,
    606  1.1  riastrad 				   const struct intel_crtc_state *crtc_state,
    607  1.1  riastrad 				   const struct drm_connector_state *conn_state)
    608  1.1  riastrad {
    609  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    610  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    611  1.1  riastrad 	struct drm_connector *connector = conn_state->connector;
    612  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    613  1.1  riastrad 	enum port port = encoder->port;
    614  1.1  riastrad 	const u8 *eld = connector->eld;
    615  1.1  riastrad 	u32 tmp, eldv;
    616  1.1  riastrad 	int len, i;
    617  1.1  riastrad 	i915_reg_t hdmiw_hdmiedid, aud_config, aud_cntl_st, aud_cntrl_st2;
    618  1.1  riastrad 
    619  1.1  riastrad 	DRM_DEBUG_KMS("Enable audio codec on [ENCODER:%d:%s], pipe %c, %u bytes ELD\n",
    620  1.1  riastrad 		      encoder->base.base.id, encoder->base.name,
    621  1.1  riastrad 		      pipe_name(pipe), drm_eld_size(eld));
    622  1.1  riastrad 
    623  1.1  riastrad 	if (WARN_ON(port == PORT_A))
    624  1.1  riastrad 		return;
    625  1.1  riastrad 
    626  1.1  riastrad 	/*
    627  1.1  riastrad 	 * FIXME: We're supposed to wait for vblank here, but we have vblanks
    628  1.1  riastrad 	 * disabled during the mode set. The proper fix would be to push the
    629  1.1  riastrad 	 * rest of the setup into a vblank work item, queued here, but the
    630  1.1  riastrad 	 * infrastructure is not there yet.
    631  1.1  riastrad 	 */
    632  1.1  riastrad 
    633  1.1  riastrad 	if (HAS_PCH_IBX(dev_priv)) {
    634  1.1  riastrad 		hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
    635  1.1  riastrad 		aud_config = IBX_AUD_CFG(pipe);
    636  1.1  riastrad 		aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
    637  1.1  riastrad 		aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
    638  1.1  riastrad 	} else if (IS_VALLEYVIEW(dev_priv) ||
    639  1.1  riastrad 		   IS_CHERRYVIEW(dev_priv)) {
    640  1.1  riastrad 		hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
    641  1.1  riastrad 		aud_config = VLV_AUD_CFG(pipe);
    642  1.1  riastrad 		aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
    643  1.1  riastrad 		aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
    644  1.1  riastrad 	} else {
    645  1.1  riastrad 		hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
    646  1.1  riastrad 		aud_config = CPT_AUD_CFG(pipe);
    647  1.1  riastrad 		aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
    648  1.1  riastrad 		aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
    649  1.1  riastrad 	}
    650  1.1  riastrad 
    651  1.1  riastrad 	eldv = IBX_ELD_VALID(port);
    652  1.1  riastrad 
    653  1.1  riastrad 	/* Invalidate ELD */
    654  1.1  riastrad 	tmp = I915_READ(aud_cntrl_st2);
    655  1.1  riastrad 	tmp &= ~eldv;
    656  1.1  riastrad 	I915_WRITE(aud_cntrl_st2, tmp);
    657  1.1  riastrad 
    658  1.1  riastrad 	/* Reset ELD write address */
    659  1.1  riastrad 	tmp = I915_READ(aud_cntl_st);
    660  1.1  riastrad 	tmp &= ~IBX_ELD_ADDRESS_MASK;
    661  1.1  riastrad 	I915_WRITE(aud_cntl_st, tmp);
    662  1.1  riastrad 
    663  1.1  riastrad 	/* Up to 84 bytes of hw ELD buffer */
    664  1.1  riastrad 	len = min(drm_eld_size(eld), 84);
    665  1.1  riastrad 	for (i = 0; i < len / 4; i++)
    666  1.1  riastrad 		I915_WRITE(hdmiw_hdmiedid, *((const u32 *)eld + i));
    667  1.1  riastrad 
    668  1.1  riastrad 	/* ELD valid */
    669  1.1  riastrad 	tmp = I915_READ(aud_cntrl_st2);
    670  1.1  riastrad 	tmp |= eldv;
    671  1.1  riastrad 	I915_WRITE(aud_cntrl_st2, tmp);
    672  1.1  riastrad 
    673  1.1  riastrad 	/* Enable timestamps */
    674  1.1  riastrad 	tmp = I915_READ(aud_config);
    675  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_VALUE_INDEX;
    676  1.1  riastrad 	tmp &= ~AUD_CONFIG_N_PROG_ENABLE;
    677  1.1  riastrad 	tmp &= ~AUD_CONFIG_PIXEL_CLOCK_HDMI_MASK;
    678  1.1  riastrad 	if (intel_crtc_has_dp_encoder(crtc_state))
    679  1.1  riastrad 		tmp |= AUD_CONFIG_N_VALUE_INDEX;
    680  1.1  riastrad 	else
    681  1.1  riastrad 		tmp |= audio_config_hdmi_pixel_clock(crtc_state);
    682  1.1  riastrad 	I915_WRITE(aud_config, tmp);
    683  1.1  riastrad }
    684  1.1  riastrad 
    685  1.1  riastrad /**
    686  1.1  riastrad  * intel_audio_codec_enable - Enable the audio codec for HD audio
    687  1.1  riastrad  * @encoder: encoder on which to enable audio
    688  1.1  riastrad  * @crtc_state: pointer to the current crtc state.
    689  1.1  riastrad  * @conn_state: pointer to the current connector state.
    690  1.1  riastrad  *
    691  1.1  riastrad  * The enable sequences may only be performed after enabling the transcoder and
    692  1.1  riastrad  * port, and after completed link training.
    693  1.1  riastrad  */
    694  1.1  riastrad void intel_audio_codec_enable(struct intel_encoder *encoder,
    695  1.1  riastrad 			      const struct intel_crtc_state *crtc_state,
    696  1.1  riastrad 			      const struct drm_connector_state *conn_state)
    697  1.1  riastrad {
    698  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    699  1.1  riastrad 	struct i915_audio_component *acomp = dev_priv->audio_component;
    700  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    701  1.1  riastrad 	struct drm_connector *connector = conn_state->connector;
    702  1.1  riastrad 	const struct drm_display_mode *adjusted_mode =
    703  1.1  riastrad 		&crtc_state->hw.adjusted_mode;
    704  1.1  riastrad 	enum port port = encoder->port;
    705  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    706  1.1  riastrad 
    707  1.1  riastrad 	/* FIXME precompute the ELD in .compute_config() */
    708  1.1  riastrad 	if (!connector->eld[0])
    709  1.1  riastrad 		DRM_DEBUG_KMS("Bogus ELD on [CONNECTOR:%d:%s]\n",
    710  1.1  riastrad 			      connector->base.id, connector->name);
    711  1.1  riastrad 
    712  1.1  riastrad 	DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
    713  1.1  riastrad 			 connector->base.id,
    714  1.1  riastrad 			 connector->name,
    715  1.1  riastrad 			 encoder->base.base.id,
    716  1.1  riastrad 			 encoder->base.name);
    717  1.1  riastrad 
    718  1.1  riastrad 	connector->eld[6] = drm_av_sync_delay(connector, adjusted_mode) / 2;
    719  1.1  riastrad 
    720  1.1  riastrad 	if (dev_priv->display.audio_codec_enable)
    721  1.1  riastrad 		dev_priv->display.audio_codec_enable(encoder,
    722  1.1  riastrad 						     crtc_state,
    723  1.1  riastrad 						     conn_state);
    724  1.1  riastrad 
    725  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
    726  1.1  riastrad 	encoder->audio_connector = connector;
    727  1.1  riastrad 
    728  1.1  riastrad 	/* referred in audio callbacks */
    729  1.1  riastrad 	dev_priv->av_enc_map[pipe] = encoder;
    730  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
    731  1.1  riastrad 
    732  1.1  riastrad 	if (acomp && acomp->base.audio_ops &&
    733  1.1  riastrad 	    acomp->base.audio_ops->pin_eld_notify) {
    734  1.1  riastrad 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
    735  1.1  riastrad 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
    736  1.1  riastrad 			pipe = -1;
    737  1.1  riastrad 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
    738  1.1  riastrad 						 (int) port, (int) pipe);
    739  1.1  riastrad 	}
    740  1.1  riastrad 
    741  1.1  riastrad 	intel_lpe_audio_notify(dev_priv, pipe, port, connector->eld,
    742  1.1  riastrad 			       crtc_state->port_clock,
    743  1.1  riastrad 			       intel_crtc_has_dp_encoder(crtc_state));
    744  1.1  riastrad }
    745  1.1  riastrad 
    746  1.1  riastrad /**
    747  1.1  riastrad  * intel_audio_codec_disable - Disable the audio codec for HD audio
    748  1.1  riastrad  * @encoder: encoder on which to disable audio
    749  1.1  riastrad  * @old_crtc_state: pointer to the old crtc state.
    750  1.1  riastrad  * @old_conn_state: pointer to the old connector state.
    751  1.1  riastrad  *
    752  1.1  riastrad  * The disable sequences must be performed before disabling the transcoder or
    753  1.1  riastrad  * port.
    754  1.1  riastrad  */
    755  1.1  riastrad void intel_audio_codec_disable(struct intel_encoder *encoder,
    756  1.1  riastrad 			       const struct intel_crtc_state *old_crtc_state,
    757  1.1  riastrad 			       const struct drm_connector_state *old_conn_state)
    758  1.1  riastrad {
    759  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
    760  1.1  riastrad 	struct i915_audio_component *acomp = dev_priv->audio_component;
    761  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
    762  1.1  riastrad 	enum port port = encoder->port;
    763  1.1  riastrad 	enum pipe pipe = crtc->pipe;
    764  1.1  riastrad 
    765  1.1  riastrad 	if (dev_priv->display.audio_codec_disable)
    766  1.1  riastrad 		dev_priv->display.audio_codec_disable(encoder,
    767  1.1  riastrad 						      old_crtc_state,
    768  1.1  riastrad 						      old_conn_state);
    769  1.1  riastrad 
    770  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
    771  1.1  riastrad 	encoder->audio_connector = NULL;
    772  1.1  riastrad 	dev_priv->av_enc_map[pipe] = NULL;
    773  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
    774  1.1  riastrad 
    775  1.1  riastrad 	if (acomp && acomp->base.audio_ops &&
    776  1.1  riastrad 	    acomp->base.audio_ops->pin_eld_notify) {
    777  1.1  riastrad 		/* audio drivers expect pipe = -1 to indicate Non-MST cases */
    778  1.1  riastrad 		if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST))
    779  1.1  riastrad 			pipe = -1;
    780  1.1  riastrad 		acomp->base.audio_ops->pin_eld_notify(acomp->base.audio_ops->audio_ptr,
    781  1.1  riastrad 						 (int) port, (int) pipe);
    782  1.1  riastrad 	}
    783  1.1  riastrad 
    784  1.1  riastrad 	intel_lpe_audio_notify(dev_priv, pipe, port, NULL, 0, false);
    785  1.1  riastrad }
    786  1.1  riastrad 
    787  1.1  riastrad /**
    788  1.1  riastrad  * intel_init_audio_hooks - Set up chip specific audio hooks
    789  1.1  riastrad  * @dev_priv: device private
    790  1.1  riastrad  */
    791  1.1  riastrad void intel_init_audio_hooks(struct drm_i915_private *dev_priv)
    792  1.1  riastrad {
    793  1.1  riastrad 	if (IS_G4X(dev_priv)) {
    794  1.1  riastrad 		dev_priv->display.audio_codec_enable = g4x_audio_codec_enable;
    795  1.1  riastrad 		dev_priv->display.audio_codec_disable = g4x_audio_codec_disable;
    796  1.1  riastrad 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
    797  1.1  riastrad 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
    798  1.1  riastrad 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
    799  1.1  riastrad 	} else if (IS_HASWELL(dev_priv) || INTEL_GEN(dev_priv) >= 8) {
    800  1.1  riastrad 		dev_priv->display.audio_codec_enable = hsw_audio_codec_enable;
    801  1.1  riastrad 		dev_priv->display.audio_codec_disable = hsw_audio_codec_disable;
    802  1.1  riastrad 	} else if (HAS_PCH_SPLIT(dev_priv)) {
    803  1.1  riastrad 		dev_priv->display.audio_codec_enable = ilk_audio_codec_enable;
    804  1.1  riastrad 		dev_priv->display.audio_codec_disable = ilk_audio_codec_disable;
    805  1.1  riastrad 	}
    806  1.1  riastrad }
    807  1.1  riastrad 
    808  1.3  riastrad #ifndef __NetBSD__		/* XXX intel audio */
    809  1.3  riastrad 
    810  1.1  riastrad static void glk_force_audio_cdclk(struct drm_i915_private *dev_priv,
    811  1.1  riastrad 				  bool enable)
    812  1.1  riastrad {
    813  1.1  riastrad 	struct drm_modeset_acquire_ctx ctx;
    814  1.1  riastrad 	struct drm_atomic_state *state;
    815  1.1  riastrad 	int ret;
    816  1.1  riastrad 
    817  1.1  riastrad 	drm_modeset_acquire_init(&ctx, 0);
    818  1.1  riastrad 	state = drm_atomic_state_alloc(&dev_priv->drm);
    819  1.1  riastrad 	if (WARN_ON(!state))
    820  1.1  riastrad 		return;
    821  1.1  riastrad 
    822  1.1  riastrad 	state->acquire_ctx = &ctx;
    823  1.1  riastrad 
    824  1.1  riastrad retry:
    825  1.1  riastrad 	to_intel_atomic_state(state)->cdclk.force_min_cdclk_changed = true;
    826  1.1  riastrad 	to_intel_atomic_state(state)->cdclk.force_min_cdclk =
    827  1.1  riastrad 		enable ? 2 * 96000 : 0;
    828  1.1  riastrad 
    829  1.1  riastrad 	/* Protects dev_priv->cdclk.force_min_cdclk */
    830  1.1  riastrad 	ret = intel_atomic_lock_global_state(to_intel_atomic_state(state));
    831  1.1  riastrad 	if (!ret)
    832  1.1  riastrad 		ret = drm_atomic_commit(state);
    833  1.1  riastrad 
    834  1.1  riastrad 	if (ret == -EDEADLK) {
    835  1.1  riastrad 		drm_atomic_state_clear(state);
    836  1.1  riastrad 		drm_modeset_backoff(&ctx);
    837  1.1  riastrad 		goto retry;
    838  1.1  riastrad 	}
    839  1.1  riastrad 
    840  1.1  riastrad 	WARN_ON(ret);
    841  1.1  riastrad 
    842  1.1  riastrad 	drm_atomic_state_put(state);
    843  1.1  riastrad 
    844  1.1  riastrad 	drm_modeset_drop_locks(&ctx);
    845  1.1  riastrad 	drm_modeset_acquire_fini(&ctx);
    846  1.1  riastrad }
    847  1.1  riastrad 
    848  1.1  riastrad static unsigned long i915_audio_component_get_power(struct device *kdev)
    849  1.1  riastrad {
    850  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
    851  1.1  riastrad 	intel_wakeref_t ret;
    852  1.1  riastrad 
    853  1.1  riastrad 	/* Catch potential impedance mismatches before they occur! */
    854  1.1  riastrad 	BUILD_BUG_ON(sizeof(intel_wakeref_t) > sizeof(unsigned long));
    855  1.1  riastrad 
    856  1.1  riastrad 	ret = intel_display_power_get(dev_priv, POWER_DOMAIN_AUDIO);
    857  1.1  riastrad 
    858  1.1  riastrad 	if (dev_priv->audio_power_refcount++ == 0) {
    859  1.1  riastrad 		if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
    860  1.1  riastrad 			I915_WRITE(AUD_FREQ_CNTRL, dev_priv->audio_freq_cntrl);
    861  1.1  riastrad 			DRM_DEBUG_KMS("restored AUD_FREQ_CNTRL to 0x%x\n",
    862  1.1  riastrad 				      dev_priv->audio_freq_cntrl);
    863  1.1  riastrad 		}
    864  1.1  riastrad 
    865  1.1  riastrad 		/* Force CDCLK to 2*BCLK as long as we need audio powered. */
    866  1.1  riastrad 		if (IS_GEMINILAKE(dev_priv))
    867  1.1  riastrad 			glk_force_audio_cdclk(dev_priv, true);
    868  1.1  riastrad 
    869  1.1  riastrad 		if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
    870  1.1  riastrad 			I915_WRITE(AUD_PIN_BUF_CTL,
    871  1.1  riastrad 				   (I915_READ(AUD_PIN_BUF_CTL) |
    872  1.1  riastrad 				    AUD_PIN_BUF_ENABLE));
    873  1.1  riastrad 	}
    874  1.1  riastrad 
    875  1.1  riastrad 	return ret;
    876  1.1  riastrad }
    877  1.1  riastrad 
    878  1.1  riastrad static void i915_audio_component_put_power(struct device *kdev,
    879  1.1  riastrad 					   unsigned long cookie)
    880  1.1  riastrad {
    881  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
    882  1.1  riastrad 
    883  1.1  riastrad 	/* Stop forcing CDCLK to 2*BCLK if no need for audio to be powered. */
    884  1.1  riastrad 	if (--dev_priv->audio_power_refcount == 0)
    885  1.1  riastrad 		if (IS_GEMINILAKE(dev_priv))
    886  1.1  riastrad 			glk_force_audio_cdclk(dev_priv, false);
    887  1.1  riastrad 
    888  1.1  riastrad 	intel_display_power_put(dev_priv, POWER_DOMAIN_AUDIO, cookie);
    889  1.1  riastrad }
    890  1.1  riastrad 
    891  1.1  riastrad static void i915_audio_component_codec_wake_override(struct device *kdev,
    892  1.1  riastrad 						     bool enable)
    893  1.1  riastrad {
    894  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
    895  1.1  riastrad 	unsigned long cookie;
    896  1.1  riastrad 	u32 tmp;
    897  1.1  riastrad 
    898  1.1  riastrad 	if (!IS_GEN(dev_priv, 9))
    899  1.1  riastrad 		return;
    900  1.1  riastrad 
    901  1.1  riastrad 	cookie = i915_audio_component_get_power(kdev);
    902  1.1  riastrad 
    903  1.1  riastrad 	/*
    904  1.1  riastrad 	 * Enable/disable generating the codec wake signal, overriding the
    905  1.1  riastrad 	 * internal logic to generate the codec wake to controller.
    906  1.1  riastrad 	 */
    907  1.1  riastrad 	tmp = I915_READ(HSW_AUD_CHICKENBIT);
    908  1.1  riastrad 	tmp &= ~SKL_AUD_CODEC_WAKE_SIGNAL;
    909  1.1  riastrad 	I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
    910  1.1  riastrad 	usleep_range(1000, 1500);
    911  1.1  riastrad 
    912  1.1  riastrad 	if (enable) {
    913  1.1  riastrad 		tmp = I915_READ(HSW_AUD_CHICKENBIT);
    914  1.1  riastrad 		tmp |= SKL_AUD_CODEC_WAKE_SIGNAL;
    915  1.1  riastrad 		I915_WRITE(HSW_AUD_CHICKENBIT, tmp);
    916  1.1  riastrad 		usleep_range(1000, 1500);
    917  1.1  riastrad 	}
    918  1.1  riastrad 
    919  1.1  riastrad 	i915_audio_component_put_power(kdev, cookie);
    920  1.1  riastrad }
    921  1.1  riastrad 
    922  1.1  riastrad /* Get CDCLK in kHz  */
    923  1.1  riastrad static int i915_audio_component_get_cdclk_freq(struct device *kdev)
    924  1.1  riastrad {
    925  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
    926  1.1  riastrad 
    927  1.1  riastrad 	if (WARN_ON_ONCE(!HAS_DDI(dev_priv)))
    928  1.1  riastrad 		return -ENODEV;
    929  1.1  riastrad 
    930  1.1  riastrad 	return dev_priv->cdclk.hw.cdclk;
    931  1.1  riastrad }
    932  1.1  riastrad 
    933  1.1  riastrad /*
    934  1.1  riastrad  * get the intel_encoder according to the parameter port and pipe
    935  1.1  riastrad  * intel_encoder is saved by the index of pipe
    936  1.1  riastrad  * MST & (pipe >= 0): return the av_enc_map[pipe],
    937  1.1  riastrad  *   when port is matched
    938  1.1  riastrad  * MST & (pipe < 0): this is invalid
    939  1.1  riastrad  * Non-MST & (pipe >= 0): only pipe = 0 (the first device entry)
    940  1.1  riastrad  *   will get the right intel_encoder with port matched
    941  1.1  riastrad  * Non-MST & (pipe < 0): get the right intel_encoder with port matched
    942  1.1  riastrad  */
    943  1.1  riastrad static struct intel_encoder *get_saved_enc(struct drm_i915_private *dev_priv,
    944  1.1  riastrad 					       int port, int pipe)
    945  1.1  riastrad {
    946  1.1  riastrad 	struct intel_encoder *encoder;
    947  1.1  riastrad 
    948  1.1  riastrad 	/* MST */
    949  1.1  riastrad 	if (pipe >= 0) {
    950  1.1  riastrad 		if (WARN_ON(pipe >= ARRAY_SIZE(dev_priv->av_enc_map)))
    951  1.1  riastrad 			return NULL;
    952  1.1  riastrad 
    953  1.1  riastrad 		encoder = dev_priv->av_enc_map[pipe];
    954  1.1  riastrad 		/*
    955  1.1  riastrad 		 * when bootup, audio driver may not know it is
    956  1.1  riastrad 		 * MST or not. So it will poll all the port & pipe
    957  1.1  riastrad 		 * combinations
    958  1.1  riastrad 		 */
    959  1.1  riastrad 		if (encoder != NULL && encoder->port == port &&
    960  1.1  riastrad 		    encoder->type == INTEL_OUTPUT_DP_MST)
    961  1.1  riastrad 			return encoder;
    962  1.1  riastrad 	}
    963  1.1  riastrad 
    964  1.1  riastrad 	/* Non-MST */
    965  1.1  riastrad 	if (pipe > 0)
    966  1.1  riastrad 		return NULL;
    967  1.1  riastrad 
    968  1.1  riastrad 	for_each_pipe(dev_priv, pipe) {
    969  1.1  riastrad 		encoder = dev_priv->av_enc_map[pipe];
    970  1.1  riastrad 		if (encoder == NULL)
    971  1.1  riastrad 			continue;
    972  1.1  riastrad 
    973  1.1  riastrad 		if (encoder->type == INTEL_OUTPUT_DP_MST)
    974  1.1  riastrad 			continue;
    975  1.1  riastrad 
    976  1.1  riastrad 		if (port == encoder->port)
    977  1.1  riastrad 			return encoder;
    978  1.1  riastrad 	}
    979  1.1  riastrad 
    980  1.1  riastrad 	return NULL;
    981  1.1  riastrad }
    982  1.1  riastrad 
    983  1.1  riastrad static int i915_audio_component_sync_audio_rate(struct device *kdev, int port,
    984  1.1  riastrad 						int pipe, int rate)
    985  1.1  riastrad {
    986  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
    987  1.1  riastrad 	struct i915_audio_component *acomp = dev_priv->audio_component;
    988  1.1  riastrad 	struct intel_encoder *encoder;
    989  1.1  riastrad 	struct intel_crtc *crtc;
    990  1.1  riastrad 	unsigned long cookie;
    991  1.1  riastrad 	int err = 0;
    992  1.1  riastrad 
    993  1.1  riastrad 	if (!HAS_DDI(dev_priv))
    994  1.1  riastrad 		return 0;
    995  1.1  riastrad 
    996  1.1  riastrad 	cookie = i915_audio_component_get_power(kdev);
    997  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
    998  1.1  riastrad 
    999  1.1  riastrad 	/* 1. get the pipe */
   1000  1.1  riastrad 	encoder = get_saved_enc(dev_priv, port, pipe);
   1001  1.1  riastrad 	if (!encoder || !encoder->base.crtc) {
   1002  1.1  riastrad 		DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
   1003  1.1  riastrad 		err = -ENODEV;
   1004  1.1  riastrad 		goto unlock;
   1005  1.1  riastrad 	}
   1006  1.1  riastrad 
   1007  1.1  riastrad 	crtc = to_intel_crtc(encoder->base.crtc);
   1008  1.1  riastrad 
   1009  1.1  riastrad 	/* port must be valid now, otherwise the pipe will be invalid */
   1010  1.1  riastrad 	acomp->aud_sample_rate[port] = rate;
   1011  1.1  riastrad 
   1012  1.1  riastrad 	hsw_audio_config_update(encoder, crtc->config);
   1013  1.1  riastrad 
   1014  1.1  riastrad  unlock:
   1015  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
   1016  1.1  riastrad 	i915_audio_component_put_power(kdev, cookie);
   1017  1.1  riastrad 	return err;
   1018  1.1  riastrad }
   1019  1.1  riastrad 
   1020  1.1  riastrad static int i915_audio_component_get_eld(struct device *kdev, int port,
   1021  1.1  riastrad 					int pipe, bool *enabled,
   1022  1.1  riastrad 					unsigned char *buf, int max_bytes)
   1023  1.1  riastrad {
   1024  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(kdev);
   1025  1.1  riastrad 	struct intel_encoder *intel_encoder;
   1026  1.1  riastrad 	const u8 *eld;
   1027  1.1  riastrad 	int ret = -EINVAL;
   1028  1.1  riastrad 
   1029  1.1  riastrad 	mutex_lock(&dev_priv->av_mutex);
   1030  1.1  riastrad 
   1031  1.1  riastrad 	intel_encoder = get_saved_enc(dev_priv, port, pipe);
   1032  1.1  riastrad 	if (!intel_encoder) {
   1033  1.1  riastrad 		DRM_DEBUG_KMS("Not valid for port %c\n", port_name(port));
   1034  1.1  riastrad 		mutex_unlock(&dev_priv->av_mutex);
   1035  1.1  riastrad 		return ret;
   1036  1.1  riastrad 	}
   1037  1.1  riastrad 
   1038  1.1  riastrad 	ret = 0;
   1039  1.1  riastrad 	*enabled = intel_encoder->audio_connector != NULL;
   1040  1.1  riastrad 	if (*enabled) {
   1041  1.1  riastrad 		eld = intel_encoder->audio_connector->eld;
   1042  1.1  riastrad 		ret = drm_eld_size(eld);
   1043  1.1  riastrad 		memcpy(buf, eld, min(max_bytes, ret));
   1044  1.1  riastrad 	}
   1045  1.1  riastrad 
   1046  1.1  riastrad 	mutex_unlock(&dev_priv->av_mutex);
   1047  1.1  riastrad 	return ret;
   1048  1.1  riastrad }
   1049  1.1  riastrad 
   1050  1.1  riastrad static const struct drm_audio_component_ops i915_audio_component_ops = {
   1051  1.1  riastrad 	.owner		= THIS_MODULE,
   1052  1.1  riastrad 	.get_power	= i915_audio_component_get_power,
   1053  1.1  riastrad 	.put_power	= i915_audio_component_put_power,
   1054  1.1  riastrad 	.codec_wake_override = i915_audio_component_codec_wake_override,
   1055  1.1  riastrad 	.get_cdclk_freq	= i915_audio_component_get_cdclk_freq,
   1056  1.1  riastrad 	.sync_audio_rate = i915_audio_component_sync_audio_rate,
   1057  1.1  riastrad 	.get_eld	= i915_audio_component_get_eld,
   1058  1.1  riastrad };
   1059  1.1  riastrad 
   1060  1.1  riastrad static int i915_audio_component_bind(struct device *i915_kdev,
   1061  1.1  riastrad 				     struct device *hda_kdev, void *data)
   1062  1.1  riastrad {
   1063  1.1  riastrad 	struct i915_audio_component *acomp = data;
   1064  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
   1065  1.1  riastrad 	int i;
   1066  1.1  riastrad 
   1067  1.1  riastrad 	if (WARN_ON(acomp->base.ops || acomp->base.dev))
   1068  1.1  riastrad 		return -EEXIST;
   1069  1.1  riastrad 
   1070  1.1  riastrad 	if (WARN_ON(!device_link_add(hda_kdev, i915_kdev, DL_FLAG_STATELESS)))
   1071  1.1  riastrad 		return -ENOMEM;
   1072  1.1  riastrad 
   1073  1.1  riastrad 	drm_modeset_lock_all(&dev_priv->drm);
   1074  1.1  riastrad 	acomp->base.ops = &i915_audio_component_ops;
   1075  1.1  riastrad 	acomp->base.dev = i915_kdev;
   1076  1.1  riastrad 	BUILD_BUG_ON(MAX_PORTS != I915_MAX_PORTS);
   1077  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(acomp->aud_sample_rate); i++)
   1078  1.1  riastrad 		acomp->aud_sample_rate[i] = 0;
   1079  1.1  riastrad 	dev_priv->audio_component = acomp;
   1080  1.1  riastrad 	drm_modeset_unlock_all(&dev_priv->drm);
   1081  1.1  riastrad 
   1082  1.1  riastrad 	return 0;
   1083  1.1  riastrad }
   1084  1.1  riastrad 
   1085  1.1  riastrad static void i915_audio_component_unbind(struct device *i915_kdev,
   1086  1.1  riastrad 					struct device *hda_kdev, void *data)
   1087  1.1  riastrad {
   1088  1.1  riastrad 	struct i915_audio_component *acomp = data;
   1089  1.1  riastrad 	struct drm_i915_private *dev_priv = kdev_to_i915(i915_kdev);
   1090  1.1  riastrad 
   1091  1.1  riastrad 	drm_modeset_lock_all(&dev_priv->drm);
   1092  1.1  riastrad 	acomp->base.ops = NULL;
   1093  1.1  riastrad 	acomp->base.dev = NULL;
   1094  1.1  riastrad 	dev_priv->audio_component = NULL;
   1095  1.1  riastrad 	drm_modeset_unlock_all(&dev_priv->drm);
   1096  1.1  riastrad 
   1097  1.1  riastrad 	device_link_remove(hda_kdev, i915_kdev);
   1098  1.1  riastrad }
   1099  1.1  riastrad 
   1100  1.1  riastrad static const struct component_ops i915_audio_component_bind_ops = {
   1101  1.1  riastrad 	.bind	= i915_audio_component_bind,
   1102  1.1  riastrad 	.unbind	= i915_audio_component_unbind,
   1103  1.1  riastrad };
   1104  1.1  riastrad 
   1105  1.2  riastrad #endif	/* __NetBSD__ */
   1106  1.2  riastrad 
   1107  1.1  riastrad /**
   1108  1.1  riastrad  * i915_audio_component_init - initialize and register the audio component
   1109  1.1  riastrad  * @dev_priv: i915 device instance
   1110  1.1  riastrad  *
   1111  1.1  riastrad  * This will register with the component framework a child component which
   1112  1.1  riastrad  * will bind dynamically to the snd_hda_intel driver's corresponding master
   1113  1.1  riastrad  * component when the latter is registered. During binding the child
   1114  1.1  riastrad  * initializes an instance of struct i915_audio_component which it receives
   1115  1.1  riastrad  * from the master. The master can then start to use the interface defined by
   1116  1.1  riastrad  * this struct. Each side can break the binding at any point by deregistering
   1117  1.1  riastrad  * its own component after which each side's component unbind callback is
   1118  1.1  riastrad  * called.
   1119  1.1  riastrad  *
   1120  1.1  riastrad  * We ignore any error during registration and continue with reduced
   1121  1.1  riastrad  * functionality (i.e. without HDMI audio).
   1122  1.1  riastrad  */
   1123  1.1  riastrad static void i915_audio_component_init(struct drm_i915_private *dev_priv)
   1124  1.1  riastrad {
   1125  1.2  riastrad #ifndef __NetBSD__		/* XXX intel audio */
   1126  1.1  riastrad 	int ret;
   1127  1.1  riastrad 
   1128  1.1  riastrad 	ret = component_add_typed(dev_priv->drm.dev,
   1129  1.1  riastrad 				  &i915_audio_component_bind_ops,
   1130  1.1  riastrad 				  I915_COMPONENT_AUDIO);
   1131  1.1  riastrad 	if (ret < 0) {
   1132  1.1  riastrad 		DRM_ERROR("failed to add audio component (%d)\n", ret);
   1133  1.1  riastrad 		/* continue with reduced functionality */
   1134  1.1  riastrad 		return;
   1135  1.1  riastrad 	}
   1136  1.2  riastrad #endif
   1137  1.1  riastrad 
   1138  1.1  riastrad 	if (IS_TIGERLAKE(dev_priv) || IS_ICELAKE(dev_priv)) {
   1139  1.1  riastrad 		dev_priv->audio_freq_cntrl = I915_READ(AUD_FREQ_CNTRL);
   1140  1.1  riastrad 		DRM_DEBUG_KMS("init value of AUD_FREQ_CNTRL of 0x%x\n",
   1141  1.1  riastrad 			      dev_priv->audio_freq_cntrl);
   1142  1.1  riastrad 	}
   1143  1.1  riastrad 
   1144  1.1  riastrad 	dev_priv->audio_component_registered = true;
   1145  1.1  riastrad }
   1146  1.1  riastrad 
   1147  1.1  riastrad /**
   1148  1.1  riastrad  * i915_audio_component_cleanup - deregister the audio component
   1149  1.1  riastrad  * @dev_priv: i915 device instance
   1150  1.1  riastrad  *
   1151  1.1  riastrad  * Deregisters the audio component, breaking any existing binding to the
   1152  1.1  riastrad  * corresponding snd_hda_intel driver's master component.
   1153  1.1  riastrad  */
   1154  1.1  riastrad static void i915_audio_component_cleanup(struct drm_i915_private *dev_priv)
   1155  1.1  riastrad {
   1156  1.1  riastrad 	if (!dev_priv->audio_component_registered)
   1157  1.1  riastrad 		return;
   1158  1.1  riastrad 
   1159  1.2  riastrad #ifndef __NetBSD__		/* XXX intel audio */
   1160  1.1  riastrad 	component_del(dev_priv->drm.dev, &i915_audio_component_bind_ops);
   1161  1.2  riastrad #endif
   1162  1.1  riastrad 	dev_priv->audio_component_registered = false;
   1163  1.1  riastrad }
   1164  1.1  riastrad 
   1165  1.1  riastrad /**
   1166  1.1  riastrad  * intel_audio_init() - Initialize the audio driver either using
   1167  1.1  riastrad  * component framework or using lpe audio bridge
   1168  1.1  riastrad  * @dev_priv: the i915 drm device private data
   1169  1.1  riastrad  *
   1170  1.1  riastrad  */
   1171  1.1  riastrad void intel_audio_init(struct drm_i915_private *dev_priv)
   1172  1.1  riastrad {
   1173  1.1  riastrad 	if (intel_lpe_audio_init(dev_priv) < 0)
   1174  1.1  riastrad 		i915_audio_component_init(dev_priv);
   1175  1.1  riastrad }
   1176  1.1  riastrad 
   1177  1.1  riastrad /**
   1178  1.1  riastrad  * intel_audio_deinit() - deinitialize the audio driver
   1179  1.1  riastrad  * @dev_priv: the i915 drm device private data
   1180  1.1  riastrad  *
   1181  1.1  riastrad  */
   1182  1.1  riastrad void intel_audio_deinit(struct drm_i915_private *dev_priv)
   1183  1.1  riastrad {
   1184  1.1  riastrad 	if ((dev_priv)->lpe_audio.platdev != NULL)
   1185  1.1  riastrad 		intel_lpe_audio_teardown(dev_priv);
   1186  1.1  riastrad 	else
   1187  1.1  riastrad 		i915_audio_component_cleanup(dev_priv);
   1188  1.1  riastrad }
   1189