Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_r600_hdmi.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2008 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  * Copyright 2009 Christian Knig.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  * Authors: Christian Knig
     27  1.1  riastrad  */
     28  1.1  riastrad #include <sys/cdefs.h>
     29  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_r600_hdmi.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $");
     30  1.1  riastrad 
     31  1.1  riastrad #include <linux/hdmi.h>
     32  1.1  riastrad #include <linux/gcd.h>
     33  1.2  riastrad 
     34  1.1  riastrad #include <drm/radeon_drm.h>
     35  1.1  riastrad #include "radeon.h"
     36  1.1  riastrad #include "radeon_asic.h"
     37  1.1  riastrad #include "radeon_audio.h"
     38  1.1  riastrad #include "r600d.h"
     39  1.1  riastrad #include "atom.h"
     40  1.1  riastrad 
     41  1.1  riastrad /*
     42  1.1  riastrad  * HDMI color format
     43  1.1  riastrad  */
     44  1.1  riastrad enum r600_hdmi_color_format {
     45  1.1  riastrad 	RGB = 0,
     46  1.1  riastrad 	YCC_422 = 1,
     47  1.1  riastrad 	YCC_444 = 2
     48  1.1  riastrad };
     49  1.1  riastrad 
     50  1.1  riastrad /*
     51  1.1  riastrad  * IEC60958 status bits
     52  1.1  riastrad  */
     53  1.1  riastrad enum r600_hdmi_iec_status_bits {
     54  1.1  riastrad 	AUDIO_STATUS_DIG_ENABLE   = 0x01,
     55  1.1  riastrad 	AUDIO_STATUS_V            = 0x02,
     56  1.1  riastrad 	AUDIO_STATUS_VCFG         = 0x04,
     57  1.1  riastrad 	AUDIO_STATUS_EMPHASIS     = 0x08,
     58  1.1  riastrad 	AUDIO_STATUS_COPYRIGHT    = 0x10,
     59  1.1  riastrad 	AUDIO_STATUS_NONAUDIO     = 0x20,
     60  1.1  riastrad 	AUDIO_STATUS_PROFESSIONAL = 0x40,
     61  1.1  riastrad 	AUDIO_STATUS_LEVEL        = 0x80
     62  1.1  riastrad };
     63  1.1  riastrad 
     64  1.1  riastrad static struct r600_audio_pin r600_audio_status(struct radeon_device *rdev)
     65  1.1  riastrad {
     66  1.2  riastrad 	struct r600_audio_pin status = {};
     67  1.1  riastrad 	uint32_t value;
     68  1.1  riastrad 
     69  1.1  riastrad 	value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);
     70  1.1  riastrad 
     71  1.1  riastrad 	/* number of channels */
     72  1.1  riastrad 	status.channels = (value & 0x7) + 1;
     73  1.1  riastrad 
     74  1.1  riastrad 	/* bits per sample */
     75  1.1  riastrad 	switch ((value & 0xF0) >> 4) {
     76  1.1  riastrad 	case 0x0:
     77  1.1  riastrad 		status.bits_per_sample = 8;
     78  1.1  riastrad 		break;
     79  1.1  riastrad 	case 0x1:
     80  1.1  riastrad 		status.bits_per_sample = 16;
     81  1.1  riastrad 		break;
     82  1.1  riastrad 	case 0x2:
     83  1.1  riastrad 		status.bits_per_sample = 20;
     84  1.1  riastrad 		break;
     85  1.1  riastrad 	case 0x3:
     86  1.1  riastrad 		status.bits_per_sample = 24;
     87  1.1  riastrad 		break;
     88  1.1  riastrad 	case 0x4:
     89  1.1  riastrad 		status.bits_per_sample = 32;
     90  1.1  riastrad 		break;
     91  1.1  riastrad 	default:
     92  1.1  riastrad 		dev_err(rdev->dev, "Unknown bits per sample 0x%x, using 16\n",
     93  1.1  riastrad 			(int)value);
     94  1.1  riastrad 		status.bits_per_sample = 16;
     95  1.1  riastrad 	}
     96  1.1  riastrad 
     97  1.1  riastrad 	/* current sampling rate in HZ */
     98  1.1  riastrad 	if (value & 0x4000)
     99  1.1  riastrad 		status.rate = 44100;
    100  1.1  riastrad 	else
    101  1.1  riastrad 		status.rate = 48000;
    102  1.1  riastrad 	status.rate *= ((value >> 11) & 0x7) + 1;
    103  1.1  riastrad 	status.rate /= ((value >> 8) & 0x7) + 1;
    104  1.1  riastrad 
    105  1.1  riastrad 	value = RREG32(R600_AUDIO_STATUS_BITS);
    106  1.1  riastrad 
    107  1.1  riastrad 	/* iec 60958 status bits */
    108  1.1  riastrad 	status.status_bits = value & 0xff;
    109  1.1  riastrad 
    110  1.1  riastrad 	/* iec 60958 category code */
    111  1.1  riastrad 	status.category_code = (value >> 8) & 0xff;
    112  1.1  riastrad 
    113  1.1  riastrad 	return status;
    114  1.1  riastrad }
    115  1.1  riastrad 
    116  1.1  riastrad /*
    117  1.1  riastrad  * update all hdmi interfaces with current audio parameters
    118  1.1  riastrad  */
    119  1.1  riastrad void r600_audio_update_hdmi(struct work_struct *work)
    120  1.1  riastrad {
    121  1.1  riastrad 	struct radeon_device *rdev = container_of(work, struct radeon_device,
    122  1.1  riastrad 						  audio_work);
    123  1.1  riastrad 	struct drm_device *dev = rdev->ddev;
    124  1.1  riastrad 	struct r600_audio_pin audio_status = r600_audio_status(rdev);
    125  1.1  riastrad 	struct drm_encoder *encoder;
    126  1.1  riastrad 	bool changed = false;
    127  1.1  riastrad 
    128  1.1  riastrad 	if (rdev->audio.pin[0].channels != audio_status.channels ||
    129  1.1  riastrad 	    rdev->audio.pin[0].rate != audio_status.rate ||
    130  1.1  riastrad 	    rdev->audio.pin[0].bits_per_sample != audio_status.bits_per_sample ||
    131  1.1  riastrad 	    rdev->audio.pin[0].status_bits != audio_status.status_bits ||
    132  1.1  riastrad 	    rdev->audio.pin[0].category_code != audio_status.category_code) {
    133  1.1  riastrad 		rdev->audio.pin[0] = audio_status;
    134  1.1  riastrad 		changed = true;
    135  1.1  riastrad 	}
    136  1.1  riastrad 
    137  1.1  riastrad 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
    138  1.1  riastrad 		if (!radeon_encoder_is_digital(encoder))
    139  1.1  riastrad 			continue;
    140  1.1  riastrad 		if (changed || r600_hdmi_buffer_status_changed(encoder))
    141  1.1  riastrad 			r600_hdmi_update_audio_settings(encoder);
    142  1.1  riastrad 	}
    143  1.1  riastrad }
    144  1.1  riastrad 
    145  1.1  riastrad /* enable the audio stream */
    146  1.1  riastrad void r600_audio_enable(struct radeon_device *rdev,
    147  1.1  riastrad 		       struct r600_audio_pin *pin,
    148  1.1  riastrad 		       u8 enable_mask)
    149  1.1  riastrad {
    150  1.1  riastrad 	u32 tmp = RREG32(AZ_HOT_PLUG_CONTROL);
    151  1.1  riastrad 
    152  1.1  riastrad 	if (!pin)
    153  1.1  riastrad 		return;
    154  1.1  riastrad 
    155  1.1  riastrad 	if (enable_mask) {
    156  1.1  riastrad 		tmp |= AUDIO_ENABLED;
    157  1.1  riastrad 		if (enable_mask & 1)
    158  1.1  riastrad 			tmp |= PIN0_AUDIO_ENABLED;
    159  1.1  riastrad 		if (enable_mask & 2)
    160  1.1  riastrad 			tmp |= PIN1_AUDIO_ENABLED;
    161  1.1  riastrad 		if (enable_mask & 4)
    162  1.1  riastrad 			tmp |= PIN2_AUDIO_ENABLED;
    163  1.1  riastrad 		if (enable_mask & 8)
    164  1.1  riastrad 			tmp |= PIN3_AUDIO_ENABLED;
    165  1.1  riastrad 	} else {
    166  1.1  riastrad 		tmp &= ~(AUDIO_ENABLED |
    167  1.1  riastrad 			 PIN0_AUDIO_ENABLED |
    168  1.1  riastrad 			 PIN1_AUDIO_ENABLED |
    169  1.1  riastrad 			 PIN2_AUDIO_ENABLED |
    170  1.1  riastrad 			 PIN3_AUDIO_ENABLED);
    171  1.1  riastrad 	}
    172  1.1  riastrad 
    173  1.1  riastrad 	WREG32(AZ_HOT_PLUG_CONTROL, tmp);
    174  1.1  riastrad }
    175  1.1  riastrad 
    176  1.1  riastrad struct r600_audio_pin *r600_audio_get_pin(struct radeon_device *rdev)
    177  1.1  riastrad {
    178  1.1  riastrad 	/* only one pin on 6xx-NI */
    179  1.1  riastrad 	return &rdev->audio.pin[0];
    180  1.1  riastrad }
    181  1.1  riastrad 
    182  1.1  riastrad void r600_hdmi_update_acr(struct drm_encoder *encoder, long offset,
    183  1.1  riastrad 	const struct radeon_hdmi_acr *acr)
    184  1.1  riastrad {
    185  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    186  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    187  1.1  riastrad 
    188  1.1  riastrad 	/* DCE 3.0 uses register that's normally for CRC_CONTROL */
    189  1.1  riastrad 	uint32_t acr_ctl = ASIC_IS_DCE3(rdev) ? DCE3_HDMI0_ACR_PACKET_CONTROL :
    190  1.1  riastrad 				       HDMI0_ACR_PACKET_CONTROL;
    191  1.1  riastrad 	WREG32_P(acr_ctl + offset,
    192  1.1  riastrad 		HDMI0_ACR_SOURCE |		/* select SW CTS value */
    193  1.1  riastrad 		HDMI0_ACR_AUTO_SEND,	/* allow hw to sent ACR packets when required */
    194  1.1  riastrad 		~(HDMI0_ACR_SOURCE |
    195  1.1  riastrad 		HDMI0_ACR_AUTO_SEND));
    196  1.1  riastrad 
    197  1.1  riastrad 	WREG32_P(HDMI0_ACR_32_0 + offset,
    198  1.1  riastrad 		HDMI0_ACR_CTS_32(acr->cts_32khz),
    199  1.1  riastrad 		~HDMI0_ACR_CTS_32_MASK);
    200  1.1  riastrad 	WREG32_P(HDMI0_ACR_32_1 + offset,
    201  1.1  riastrad 		HDMI0_ACR_N_32(acr->n_32khz),
    202  1.1  riastrad 		~HDMI0_ACR_N_32_MASK);
    203  1.1  riastrad 
    204  1.1  riastrad 	WREG32_P(HDMI0_ACR_44_0 + offset,
    205  1.1  riastrad 		HDMI0_ACR_CTS_44(acr->cts_44_1khz),
    206  1.1  riastrad 		~HDMI0_ACR_CTS_44_MASK);
    207  1.1  riastrad 	WREG32_P(HDMI0_ACR_44_1 + offset,
    208  1.1  riastrad 		HDMI0_ACR_N_44(acr->n_44_1khz),
    209  1.1  riastrad 		~HDMI0_ACR_N_44_MASK);
    210  1.1  riastrad 
    211  1.1  riastrad 	WREG32_P(HDMI0_ACR_48_0 + offset,
    212  1.1  riastrad 		HDMI0_ACR_CTS_48(acr->cts_48khz),
    213  1.1  riastrad 		~HDMI0_ACR_CTS_48_MASK);
    214  1.1  riastrad 	WREG32_P(HDMI0_ACR_48_1 + offset,
    215  1.1  riastrad 		HDMI0_ACR_N_48(acr->n_48khz),
    216  1.1  riastrad 		~HDMI0_ACR_N_48_MASK);
    217  1.1  riastrad }
    218  1.1  riastrad 
    219  1.1  riastrad /*
    220  1.1  riastrad  * build a HDMI Video Info Frame
    221  1.1  riastrad  */
    222  1.1  riastrad void r600_set_avi_packet(struct radeon_device *rdev, u32 offset,
    223  1.2  riastrad 			 unsigned char *buffer, size_t size)
    224  1.1  riastrad {
    225  1.1  riastrad 	uint8_t *frame = buffer + 3;
    226  1.1  riastrad 
    227  1.1  riastrad 	WREG32(HDMI0_AVI_INFO0 + offset,
    228  1.1  riastrad 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
    229  1.1  riastrad 	WREG32(HDMI0_AVI_INFO1 + offset,
    230  1.1  riastrad 		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x7] << 24));
    231  1.1  riastrad 	WREG32(HDMI0_AVI_INFO2 + offset,
    232  1.1  riastrad 		frame[0x8] | (frame[0x9] << 8) | (frame[0xA] << 16) | (frame[0xB] << 24));
    233  1.1  riastrad 	WREG32(HDMI0_AVI_INFO3 + offset,
    234  1.1  riastrad 		frame[0xC] | (frame[0xD] << 8) | (buffer[1] << 24));
    235  1.1  riastrad 
    236  1.1  riastrad 	WREG32_OR(HDMI0_INFOFRAME_CONTROL1 + offset,
    237  1.1  riastrad 		  HDMI0_AVI_INFO_LINE(2));	/* anything other than 0 */
    238  1.1  riastrad 
    239  1.1  riastrad 	WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
    240  1.1  riastrad 		  HDMI0_AVI_INFO_SEND |	/* enable AVI info frames */
    241  1.1  riastrad 		  HDMI0_AVI_INFO_CONT);	/* send AVI info frames every frame/field */
    242  1.1  riastrad 
    243  1.1  riastrad }
    244  1.1  riastrad 
    245  1.1  riastrad /*
    246  1.1  riastrad  * build a Audio Info Frame
    247  1.1  riastrad  */
    248  1.1  riastrad static void r600_hdmi_update_audio_infoframe(struct drm_encoder *encoder,
    249  1.1  riastrad 					     const void *buffer, size_t size)
    250  1.1  riastrad {
    251  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    252  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    253  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    254  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    255  1.1  riastrad 	uint32_t offset = dig->afmt->offset;
    256  1.1  riastrad 	const u8 *frame = (const u8 *)buffer + 3;
    257  1.1  riastrad 
    258  1.1  riastrad 	WREG32(HDMI0_AUDIO_INFO0 + offset,
    259  1.1  riastrad 		frame[0x0] | (frame[0x1] << 8) | (frame[0x2] << 16) | (frame[0x3] << 24));
    260  1.1  riastrad 	WREG32(HDMI0_AUDIO_INFO1 + offset,
    261  1.1  riastrad 		frame[0x4] | (frame[0x5] << 8) | (frame[0x6] << 16) | (frame[0x8] << 24));
    262  1.1  riastrad }
    263  1.1  riastrad 
    264  1.1  riastrad /*
    265  1.1  riastrad  * test if audio buffer is filled enough to start playing
    266  1.1  riastrad  */
    267  1.1  riastrad static bool r600_hdmi_is_audio_buffer_filled(struct drm_encoder *encoder)
    268  1.1  riastrad {
    269  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    270  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    271  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    272  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    273  1.1  riastrad 	uint32_t offset = dig->afmt->offset;
    274  1.1  riastrad 
    275  1.1  riastrad 	return (RREG32(HDMI0_STATUS + offset) & 0x10) != 0;
    276  1.1  riastrad }
    277  1.1  riastrad 
    278  1.1  riastrad /*
    279  1.1  riastrad  * have buffer status changed since last call?
    280  1.1  riastrad  */
    281  1.1  riastrad int r600_hdmi_buffer_status_changed(struct drm_encoder *encoder)
    282  1.1  riastrad {
    283  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    284  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    285  1.1  riastrad 	int status, result;
    286  1.1  riastrad 
    287  1.1  riastrad 	if (!dig->afmt || !dig->afmt->enabled)
    288  1.1  riastrad 		return 0;
    289  1.1  riastrad 
    290  1.1  riastrad 	status = r600_hdmi_is_audio_buffer_filled(encoder);
    291  1.1  riastrad 	result = dig->afmt->last_buffer_filled_status != status;
    292  1.1  riastrad 	dig->afmt->last_buffer_filled_status = status;
    293  1.1  riastrad 
    294  1.1  riastrad 	return result;
    295  1.1  riastrad }
    296  1.1  riastrad 
    297  1.1  riastrad /*
    298  1.1  riastrad  * write the audio workaround status to the hardware
    299  1.1  riastrad  */
    300  1.1  riastrad void r600_hdmi_audio_workaround(struct drm_encoder *encoder)
    301  1.1  riastrad {
    302  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    303  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    304  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    305  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    306  1.1  riastrad 	uint32_t offset = dig->afmt->offset;
    307  1.1  riastrad 	bool hdmi_audio_workaround = false; /* FIXME */
    308  1.1  riastrad 	u32 value;
    309  1.1  riastrad 
    310  1.1  riastrad 	if (!hdmi_audio_workaround ||
    311  1.1  riastrad 	    r600_hdmi_is_audio_buffer_filled(encoder))
    312  1.1  riastrad 		value = 0; /* disable workaround */
    313  1.1  riastrad 	else
    314  1.1  riastrad 		value = HDMI0_AUDIO_TEST_EN; /* enable workaround */
    315  1.1  riastrad 	WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
    316  1.1  riastrad 		 value, ~HDMI0_AUDIO_TEST_EN);
    317  1.1  riastrad }
    318  1.1  riastrad 
    319  1.1  riastrad void r600_hdmi_audio_set_dto(struct radeon_device *rdev,
    320  1.2  riastrad 			     struct radeon_crtc *crtc, unsigned int clock)
    321  1.1  riastrad {
    322  1.1  riastrad 	struct radeon_encoder *radeon_encoder;
    323  1.1  riastrad 	struct radeon_encoder_atom_dig *dig;
    324  1.1  riastrad 
    325  1.1  riastrad 	if (!crtc)
    326  1.1  riastrad 		return;
    327  1.1  riastrad 
    328  1.1  riastrad 	radeon_encoder = to_radeon_encoder(crtc->encoder);
    329  1.1  riastrad 	dig = radeon_encoder->enc_priv;
    330  1.1  riastrad 
    331  1.1  riastrad 	if (!dig)
    332  1.1  riastrad 		return;
    333  1.1  riastrad 
    334  1.1  riastrad 	if (dig->dig_encoder == 0) {
    335  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO0_PHASE, 24000 * 100);
    336  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO0_MODULE, clock * 100);
    337  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO_SELECT, 0); /* select DTO0 */
    338  1.1  riastrad 	} else {
    339  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO1_PHASE, 24000 * 100);
    340  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO1_MODULE, clock * 100);
    341  1.1  riastrad 		WREG32(DCCG_AUDIO_DTO_SELECT, 1); /* select DTO1 */
    342  1.1  riastrad 	}
    343  1.1  riastrad }
    344  1.1  riastrad 
    345  1.1  riastrad void r600_set_vbi_packet(struct drm_encoder *encoder, u32 offset)
    346  1.1  riastrad {
    347  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    348  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    349  1.1  riastrad 
    350  1.1  riastrad 	WREG32_OR(HDMI0_VBI_PACKET_CONTROL + offset,
    351  1.1  riastrad 		HDMI0_NULL_SEND |	/* send null packets when required */
    352  1.1  riastrad 		HDMI0_GC_SEND |		/* send general control packets */
    353  1.1  riastrad 		HDMI0_GC_CONT);		/* send general control packets every frame */
    354  1.1  riastrad }
    355  1.1  riastrad 
    356  1.1  riastrad void r600_set_audio_packet(struct drm_encoder *encoder, u32 offset)
    357  1.1  riastrad {
    358  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    359  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    360  1.1  riastrad 
    361  1.1  riastrad 	WREG32_P(HDMI0_AUDIO_PACKET_CONTROL + offset,
    362  1.1  riastrad 		HDMI0_AUDIO_SAMPLE_SEND |			/* send audio packets */
    363  1.1  riastrad 		HDMI0_AUDIO_DELAY_EN(1) |			/* default audio delay */
    364  1.1  riastrad 		HDMI0_AUDIO_PACKETS_PER_LINE(3) |	/* should be suffient for all audio modes and small enough for all hblanks */
    365  1.1  riastrad 		HDMI0_60958_CS_UPDATE,				/* allow 60958 channel status fields to be updated */
    366  1.1  riastrad 		~(HDMI0_AUDIO_SAMPLE_SEND |
    367  1.1  riastrad 		HDMI0_AUDIO_DELAY_EN_MASK |
    368  1.1  riastrad 		HDMI0_AUDIO_PACKETS_PER_LINE_MASK |
    369  1.1  riastrad 		HDMI0_60958_CS_UPDATE));
    370  1.1  riastrad 
    371  1.1  riastrad 	WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
    372  1.1  riastrad 		HDMI0_AUDIO_INFO_SEND |		/* enable audio info frames (frames won't be set until audio is enabled) */
    373  1.1  riastrad 		HDMI0_AUDIO_INFO_UPDATE);	/* required for audio info values to be updated */
    374  1.1  riastrad 
    375  1.1  riastrad 	WREG32_P(HDMI0_INFOFRAME_CONTROL1 + offset,
    376  1.1  riastrad 		HDMI0_AUDIO_INFO_LINE(2),	/* anything other than 0 */
    377  1.1  riastrad 		~HDMI0_AUDIO_INFO_LINE_MASK);
    378  1.1  riastrad 
    379  1.1  riastrad 	WREG32_AND(HDMI0_GENERIC_PACKET_CONTROL + offset,
    380  1.1  riastrad 		~(HDMI0_GENERIC0_SEND |
    381  1.1  riastrad 		HDMI0_GENERIC0_CONT |
    382  1.1  riastrad 		HDMI0_GENERIC0_UPDATE |
    383  1.1  riastrad 		HDMI0_GENERIC1_SEND |
    384  1.1  riastrad 		HDMI0_GENERIC1_CONT |
    385  1.1  riastrad 		HDMI0_GENERIC0_LINE_MASK |
    386  1.1  riastrad 		HDMI0_GENERIC1_LINE_MASK));
    387  1.1  riastrad 
    388  1.1  riastrad 	WREG32_P(HDMI0_60958_0 + offset,
    389  1.1  riastrad 		HDMI0_60958_CS_CHANNEL_NUMBER_L(1),
    390  1.1  riastrad 		~(HDMI0_60958_CS_CHANNEL_NUMBER_L_MASK |
    391  1.1  riastrad 		HDMI0_60958_CS_CLOCK_ACCURACY_MASK));
    392  1.1  riastrad 
    393  1.1  riastrad 	WREG32_P(HDMI0_60958_1 + offset,
    394  1.1  riastrad 		HDMI0_60958_CS_CHANNEL_NUMBER_R(2),
    395  1.1  riastrad 		~HDMI0_60958_CS_CHANNEL_NUMBER_R_MASK);
    396  1.1  riastrad }
    397  1.1  riastrad 
    398  1.1  riastrad void r600_set_mute(struct drm_encoder *encoder, u32 offset, bool mute)
    399  1.1  riastrad {
    400  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    401  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    402  1.1  riastrad 
    403  1.1  riastrad 	if (mute)
    404  1.1  riastrad 		WREG32_OR(HDMI0_GC + offset, HDMI0_GC_AVMUTE);
    405  1.1  riastrad 	else
    406  1.1  riastrad 		WREG32_AND(HDMI0_GC + offset, ~HDMI0_GC_AVMUTE);
    407  1.1  riastrad }
    408  1.1  riastrad 
    409  1.1  riastrad /**
    410  1.1  riastrad  * r600_hdmi_update_audio_settings - Update audio infoframe
    411  1.1  riastrad  *
    412  1.1  riastrad  * @encoder: drm encoder
    413  1.1  riastrad  *
    414  1.1  riastrad  * Gets info about current audio stream and updates audio infoframe.
    415  1.1  riastrad  */
    416  1.1  riastrad void r600_hdmi_update_audio_settings(struct drm_encoder *encoder)
    417  1.1  riastrad {
    418  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    419  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    420  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    421  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    422  1.1  riastrad 	struct r600_audio_pin audio = r600_audio_status(rdev);
    423  1.1  riastrad 	uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
    424  1.1  riastrad 	struct hdmi_audio_infoframe frame;
    425  1.1  riastrad 	uint32_t offset;
    426  1.1  riastrad 	uint32_t value;
    427  1.1  riastrad 	ssize_t err;
    428  1.1  riastrad 
    429  1.1  riastrad 	if (!dig->afmt || !dig->afmt->enabled)
    430  1.1  riastrad 		return;
    431  1.1  riastrad 	offset = dig->afmt->offset;
    432  1.1  riastrad 
    433  1.1  riastrad 	DRM_DEBUG("%s with %d channels, %d Hz sampling rate, %d bits per sample,\n",
    434  1.1  riastrad 		 r600_hdmi_is_audio_buffer_filled(encoder) ? "playing" : "stopped",
    435  1.1  riastrad 		  audio.channels, audio.rate, audio.bits_per_sample);
    436  1.1  riastrad 	DRM_DEBUG("0x%02X IEC60958 status bits and 0x%02X category code\n",
    437  1.1  riastrad 		  (int)audio.status_bits, (int)audio.category_code);
    438  1.1  riastrad 
    439  1.1  riastrad 	err = hdmi_audio_infoframe_init(&frame);
    440  1.1  riastrad 	if (err < 0) {
    441  1.1  riastrad 		DRM_ERROR("failed to setup audio infoframe\n");
    442  1.1  riastrad 		return;
    443  1.1  riastrad 	}
    444  1.1  riastrad 
    445  1.1  riastrad 	frame.channels = audio.channels;
    446  1.1  riastrad 
    447  1.1  riastrad 	err = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
    448  1.1  riastrad 	if (err < 0) {
    449  1.1  riastrad 		DRM_ERROR("failed to pack audio infoframe\n");
    450  1.1  riastrad 		return;
    451  1.1  riastrad 	}
    452  1.1  riastrad 
    453  1.1  riastrad 	value = RREG32(HDMI0_AUDIO_PACKET_CONTROL + offset);
    454  1.1  riastrad 	if (value & HDMI0_AUDIO_TEST_EN)
    455  1.1  riastrad 		WREG32(HDMI0_AUDIO_PACKET_CONTROL + offset,
    456  1.1  riastrad 		       value & ~HDMI0_AUDIO_TEST_EN);
    457  1.1  riastrad 
    458  1.1  riastrad 	WREG32_OR(HDMI0_CONTROL + offset,
    459  1.1  riastrad 		  HDMI0_ERROR_ACK);
    460  1.1  riastrad 
    461  1.1  riastrad 	WREG32_AND(HDMI0_INFOFRAME_CONTROL0 + offset,
    462  1.1  riastrad 		   ~HDMI0_AUDIO_INFO_SOURCE);
    463  1.1  riastrad 
    464  1.1  riastrad 	r600_hdmi_update_audio_infoframe(encoder, buffer, sizeof(buffer));
    465  1.1  riastrad 
    466  1.1  riastrad 	WREG32_OR(HDMI0_INFOFRAME_CONTROL0 + offset,
    467  1.1  riastrad 		  HDMI0_AUDIO_INFO_CONT |
    468  1.1  riastrad 		  HDMI0_AUDIO_INFO_UPDATE);
    469  1.1  riastrad }
    470  1.1  riastrad 
    471  1.1  riastrad /*
    472  1.1  riastrad  * enable the HDMI engine
    473  1.1  riastrad  */
    474  1.1  riastrad void r600_hdmi_enable(struct drm_encoder *encoder, bool enable)
    475  1.1  riastrad {
    476  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    477  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    478  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    479  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    480  1.1  riastrad 	u32 hdmi = HDMI0_ERROR_ACK;
    481  1.1  riastrad 
    482  1.1  riastrad 	if (!dig || !dig->afmt)
    483  1.1  riastrad 		return;
    484  1.1  riastrad 
    485  1.1  riastrad 	/* Older chipsets require setting HDMI and routing manually */
    486  1.1  riastrad 	if (!ASIC_IS_DCE3(rdev)) {
    487  1.1  riastrad 		if (enable)
    488  1.1  riastrad 			hdmi |= HDMI0_ENABLE;
    489  1.1  riastrad 		switch (radeon_encoder->encoder_id) {
    490  1.1  riastrad 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:
    491  1.1  riastrad 			if (enable) {
    492  1.1  riastrad 				WREG32_OR(AVIVO_TMDSA_CNTL, AVIVO_TMDSA_CNTL_HDMI_EN);
    493  1.1  riastrad 				hdmi |= HDMI0_STREAM(HDMI0_STREAM_TMDSA);
    494  1.1  riastrad 			} else {
    495  1.1  riastrad 				WREG32_AND(AVIVO_TMDSA_CNTL, ~AVIVO_TMDSA_CNTL_HDMI_EN);
    496  1.1  riastrad 			}
    497  1.1  riastrad 			break;
    498  1.1  riastrad 		case ENCODER_OBJECT_ID_INTERNAL_LVTM1:
    499  1.1  riastrad 			if (enable) {
    500  1.1  riastrad 				WREG32_OR(AVIVO_LVTMA_CNTL, AVIVO_LVTMA_CNTL_HDMI_EN);
    501  1.1  riastrad 				hdmi |= HDMI0_STREAM(HDMI0_STREAM_LVTMA);
    502  1.1  riastrad 			} else {
    503  1.1  riastrad 				WREG32_AND(AVIVO_LVTMA_CNTL, ~AVIVO_LVTMA_CNTL_HDMI_EN);
    504  1.1  riastrad 			}
    505  1.1  riastrad 			break;
    506  1.1  riastrad 		case ENCODER_OBJECT_ID_INTERNAL_DDI:
    507  1.1  riastrad 			if (enable) {
    508  1.1  riastrad 				WREG32_OR(DDIA_CNTL, DDIA_HDMI_EN);
    509  1.1  riastrad 				hdmi |= HDMI0_STREAM(HDMI0_STREAM_DDIA);
    510  1.1  riastrad 			} else {
    511  1.1  riastrad 				WREG32_AND(DDIA_CNTL, ~DDIA_HDMI_EN);
    512  1.1  riastrad 			}
    513  1.1  riastrad 			break;
    514  1.1  riastrad 		case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_DVO1:
    515  1.1  riastrad 			if (enable)
    516  1.1  riastrad 				hdmi |= HDMI0_STREAM(HDMI0_STREAM_DVOA);
    517  1.1  riastrad 			break;
    518  1.1  riastrad 		default:
    519  1.1  riastrad 			dev_err(rdev->dev, "Invalid encoder for HDMI: 0x%X\n",
    520  1.1  riastrad 				radeon_encoder->encoder_id);
    521  1.1  riastrad 			break;
    522  1.1  riastrad 		}
    523  1.1  riastrad 		WREG32(HDMI0_CONTROL + dig->afmt->offset, hdmi);
    524  1.1  riastrad 	}
    525  1.1  riastrad 
    526  1.1  riastrad 	if (rdev->irq.installed) {
    527  1.1  riastrad 		/* if irq is available use it */
    528  1.1  riastrad 		/* XXX: shouldn't need this on any asics.  Double check DCE2/3 */
    529  1.1  riastrad 		if (enable)
    530  1.1  riastrad 			radeon_irq_kms_enable_afmt(rdev, dig->afmt->id);
    531  1.1  riastrad 		else
    532  1.1  riastrad 			radeon_irq_kms_disable_afmt(rdev, dig->afmt->id);
    533  1.1  riastrad 	}
    534  1.1  riastrad 
    535  1.1  riastrad 	dig->afmt->enabled = enable;
    536  1.1  riastrad 
    537  1.1  riastrad 	DRM_DEBUG("%sabling HDMI interface @ 0x%04X for encoder 0x%x\n",
    538  1.1  riastrad 		  enable ? "En" : "Dis", dig->afmt->offset, radeon_encoder->encoder_id);
    539  1.1  riastrad }
    540  1.1  riastrad 
    541