Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_audio.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2014 Advanced Micro Devices, Inc.
      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 shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Slava Grigorev <slava.grigorev (at) amd.com>
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.1  riastrad #include <sys/cdefs.h>
     28  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_audio.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     29  1.1  riastrad 
     30  1.1  riastrad #include <linux/gcd.h>
     31  1.3  riastrad 
     32  1.1  riastrad #include <drm/drm_crtc.h>
     33  1.1  riastrad #include "radeon.h"
     34  1.1  riastrad #include "atom.h"
     35  1.1  riastrad #include "radeon_audio.h"
     36  1.1  riastrad 
     37  1.1  riastrad void r600_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
     38  1.1  riastrad 		u8 enable_mask);
     39  1.1  riastrad void dce4_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
     40  1.1  riastrad 		u8 enable_mask);
     41  1.1  riastrad void dce6_audio_enable(struct radeon_device *rdev, struct r600_audio_pin *pin,
     42  1.1  riastrad 		u8 enable_mask);
     43  1.1  riastrad u32 dce6_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg);
     44  1.1  riastrad void dce6_endpoint_wreg(struct radeon_device *rdev,
     45  1.1  riastrad 		u32 offset, u32 reg, u32 v);
     46  1.1  riastrad void dce3_2_afmt_write_sad_regs(struct drm_encoder *encoder,
     47  1.1  riastrad 		struct cea_sad *sads, int sad_count);
     48  1.1  riastrad void evergreen_hdmi_write_sad_regs(struct drm_encoder *encoder,
     49  1.1  riastrad 		struct cea_sad *sads, int sad_count);
     50  1.1  riastrad void dce6_afmt_write_sad_regs(struct drm_encoder *encoder,
     51  1.1  riastrad 		struct cea_sad *sads, int sad_count);
     52  1.1  riastrad void dce3_2_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
     53  1.1  riastrad 		u8 *sadb, int sad_count);
     54  1.1  riastrad void dce3_2_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
     55  1.1  riastrad 		u8 *sadb, int sad_count);
     56  1.1  riastrad void dce4_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
     57  1.1  riastrad 		u8 *sadb, int sad_count);
     58  1.1  riastrad void dce4_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
     59  1.1  riastrad 		u8 *sadb, int sad_count);
     60  1.1  riastrad void dce6_afmt_hdmi_write_speaker_allocation(struct drm_encoder *encoder,
     61  1.1  riastrad 		u8 *sadb, int sad_count);
     62  1.1  riastrad void dce6_afmt_dp_write_speaker_allocation(struct drm_encoder *encoder,
     63  1.1  riastrad 		u8 *sadb, int sad_count);
     64  1.1  riastrad void dce4_afmt_write_latency_fields(struct drm_encoder *encoder,
     65  1.1  riastrad 		struct drm_connector *connector, struct drm_display_mode *mode);
     66  1.1  riastrad void dce6_afmt_write_latency_fields(struct drm_encoder *encoder,
     67  1.1  riastrad 		struct drm_connector *connector, struct drm_display_mode *mode);
     68  1.1  riastrad struct r600_audio_pin* r600_audio_get_pin(struct radeon_device *rdev);
     69  1.1  riastrad struct r600_audio_pin* dce6_audio_get_pin(struct radeon_device *rdev);
     70  1.1  riastrad void dce6_afmt_select_pin(struct drm_encoder *encoder);
     71  1.1  riastrad void r600_hdmi_audio_set_dto(struct radeon_device *rdev,
     72  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     73  1.1  riastrad void dce3_2_audio_set_dto(struct radeon_device *rdev,
     74  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     75  1.1  riastrad void dce4_hdmi_audio_set_dto(struct radeon_device *rdev,
     76  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     77  1.1  riastrad void dce4_dp_audio_set_dto(struct radeon_device *rdev,
     78  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     79  1.1  riastrad void dce6_hdmi_audio_set_dto(struct radeon_device *rdev,
     80  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     81  1.1  riastrad void dce6_dp_audio_set_dto(struct radeon_device *rdev,
     82  1.1  riastrad 	struct radeon_crtc *crtc, unsigned int clock);
     83  1.1  riastrad void r600_set_avi_packet(struct radeon_device *rdev, u32 offset,
     84  1.1  riastrad 	unsigned char *buffer, size_t size);
     85  1.1  riastrad void evergreen_set_avi_packet(struct radeon_device *rdev, u32 offset,
     86  1.1  riastrad 	unsigned char *buffer, size_t size);
     87  1.1  riastrad void r600_hdmi_update_acr(struct drm_encoder *encoder, long offset,
     88  1.1  riastrad 	const struct radeon_hdmi_acr *acr);
     89  1.1  riastrad void dce3_2_hdmi_update_acr(struct drm_encoder *encoder, long offset,
     90  1.1  riastrad 	const struct radeon_hdmi_acr *acr);
     91  1.1  riastrad void evergreen_hdmi_update_acr(struct drm_encoder *encoder, long offset,
     92  1.1  riastrad 	const struct radeon_hdmi_acr *acr);
     93  1.1  riastrad void r600_set_vbi_packet(struct drm_encoder *encoder, u32 offset);
     94  1.1  riastrad void dce4_set_vbi_packet(struct drm_encoder *encoder, u32 offset);
     95  1.1  riastrad void dce4_hdmi_set_color_depth(struct drm_encoder *encoder,
     96  1.1  riastrad 	u32 offset, int bpc);
     97  1.1  riastrad void r600_set_audio_packet(struct drm_encoder *encoder, u32 offset);
     98  1.1  riastrad void dce3_2_set_audio_packet(struct drm_encoder *encoder, u32 offset);
     99  1.1  riastrad void dce4_set_audio_packet(struct drm_encoder *encoder, u32 offset);
    100  1.1  riastrad void r600_set_mute(struct drm_encoder *encoder, u32 offset, bool mute);
    101  1.1  riastrad void dce3_2_set_mute(struct drm_encoder *encoder, u32 offset, bool mute);
    102  1.1  riastrad void dce4_set_mute(struct drm_encoder *encoder, u32 offset, bool mute);
    103  1.1  riastrad static void radeon_audio_hdmi_mode_set(struct drm_encoder *encoder,
    104  1.1  riastrad 	struct drm_display_mode *mode);
    105  1.1  riastrad static void radeon_audio_dp_mode_set(struct drm_encoder *encoder,
    106  1.1  riastrad 	struct drm_display_mode *mode);
    107  1.1  riastrad void r600_hdmi_enable(struct drm_encoder *encoder, bool enable);
    108  1.1  riastrad void evergreen_hdmi_enable(struct drm_encoder *encoder, bool enable);
    109  1.1  riastrad void evergreen_dp_enable(struct drm_encoder *encoder, bool enable);
    110  1.1  riastrad 
    111  1.1  riastrad static const u32 pin_offsets[7] =
    112  1.1  riastrad {
    113  1.1  riastrad 	(0x5e00 - 0x5e00),
    114  1.1  riastrad 	(0x5e18 - 0x5e00),
    115  1.1  riastrad 	(0x5e30 - 0x5e00),
    116  1.1  riastrad 	(0x5e48 - 0x5e00),
    117  1.1  riastrad 	(0x5e60 - 0x5e00),
    118  1.1  riastrad 	(0x5e78 - 0x5e00),
    119  1.1  riastrad 	(0x5e90 - 0x5e00),
    120  1.1  riastrad };
    121  1.1  riastrad 
    122  1.1  riastrad static u32 radeon_audio_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
    123  1.1  riastrad {
    124  1.1  riastrad 	return RREG32(reg);
    125  1.1  riastrad }
    126  1.1  riastrad 
    127  1.1  riastrad static void radeon_audio_wreg(struct radeon_device *rdev, u32 offset,
    128  1.1  riastrad 		u32 reg, u32 v)
    129  1.1  riastrad {
    130  1.1  riastrad 	WREG32(reg, v);
    131  1.1  riastrad }
    132  1.1  riastrad 
    133  1.1  riastrad static struct radeon_audio_basic_funcs r600_funcs = {
    134  1.1  riastrad 	.endpoint_rreg = radeon_audio_rreg,
    135  1.1  riastrad 	.endpoint_wreg = radeon_audio_wreg,
    136  1.1  riastrad 	.enable = r600_audio_enable,
    137  1.1  riastrad };
    138  1.1  riastrad 
    139  1.1  riastrad static struct radeon_audio_basic_funcs dce32_funcs = {
    140  1.1  riastrad 	.endpoint_rreg = radeon_audio_rreg,
    141  1.1  riastrad 	.endpoint_wreg = radeon_audio_wreg,
    142  1.1  riastrad 	.enable = r600_audio_enable,
    143  1.1  riastrad };
    144  1.1  riastrad 
    145  1.1  riastrad static struct radeon_audio_basic_funcs dce4_funcs = {
    146  1.1  riastrad 	.endpoint_rreg = radeon_audio_rreg,
    147  1.1  riastrad 	.endpoint_wreg = radeon_audio_wreg,
    148  1.1  riastrad 	.enable = dce4_audio_enable,
    149  1.1  riastrad };
    150  1.1  riastrad 
    151  1.1  riastrad static struct radeon_audio_basic_funcs dce6_funcs = {
    152  1.1  riastrad 	.endpoint_rreg = dce6_endpoint_rreg,
    153  1.1  riastrad 	.endpoint_wreg = dce6_endpoint_wreg,
    154  1.1  riastrad 	.enable = dce6_audio_enable,
    155  1.1  riastrad };
    156  1.1  riastrad 
    157  1.1  riastrad static struct radeon_audio_funcs r600_hdmi_funcs = {
    158  1.1  riastrad 	.get_pin = r600_audio_get_pin,
    159  1.1  riastrad 	.set_dto = r600_hdmi_audio_set_dto,
    160  1.1  riastrad 	.update_acr = r600_hdmi_update_acr,
    161  1.1  riastrad 	.set_vbi_packet = r600_set_vbi_packet,
    162  1.1  riastrad 	.set_avi_packet = r600_set_avi_packet,
    163  1.1  riastrad 	.set_audio_packet = r600_set_audio_packet,
    164  1.1  riastrad 	.set_mute = r600_set_mute,
    165  1.1  riastrad 	.mode_set = radeon_audio_hdmi_mode_set,
    166  1.1  riastrad 	.dpms = r600_hdmi_enable,
    167  1.1  riastrad };
    168  1.1  riastrad 
    169  1.1  riastrad static struct radeon_audio_funcs dce32_hdmi_funcs = {
    170  1.1  riastrad 	.get_pin = r600_audio_get_pin,
    171  1.1  riastrad 	.write_sad_regs = dce3_2_afmt_write_sad_regs,
    172  1.1  riastrad 	.write_speaker_allocation = dce3_2_afmt_hdmi_write_speaker_allocation,
    173  1.1  riastrad 	.set_dto = dce3_2_audio_set_dto,
    174  1.1  riastrad 	.update_acr = dce3_2_hdmi_update_acr,
    175  1.1  riastrad 	.set_vbi_packet = r600_set_vbi_packet,
    176  1.1  riastrad 	.set_avi_packet = r600_set_avi_packet,
    177  1.1  riastrad 	.set_audio_packet = dce3_2_set_audio_packet,
    178  1.1  riastrad 	.set_mute = dce3_2_set_mute,
    179  1.1  riastrad 	.mode_set = radeon_audio_hdmi_mode_set,
    180  1.1  riastrad 	.dpms = r600_hdmi_enable,
    181  1.1  riastrad };
    182  1.1  riastrad 
    183  1.1  riastrad static struct radeon_audio_funcs dce32_dp_funcs = {
    184  1.1  riastrad 	.get_pin = r600_audio_get_pin,
    185  1.1  riastrad 	.write_sad_regs = dce3_2_afmt_write_sad_regs,
    186  1.1  riastrad 	.write_speaker_allocation = dce3_2_afmt_dp_write_speaker_allocation,
    187  1.1  riastrad 	.set_dto = dce3_2_audio_set_dto,
    188  1.1  riastrad 	.set_avi_packet = r600_set_avi_packet,
    189  1.1  riastrad 	.set_audio_packet = dce3_2_set_audio_packet,
    190  1.1  riastrad };
    191  1.1  riastrad 
    192  1.1  riastrad static struct radeon_audio_funcs dce4_hdmi_funcs = {
    193  1.1  riastrad 	.get_pin = r600_audio_get_pin,
    194  1.1  riastrad 	.write_sad_regs = evergreen_hdmi_write_sad_regs,
    195  1.1  riastrad 	.write_speaker_allocation = dce4_afmt_hdmi_write_speaker_allocation,
    196  1.1  riastrad 	.write_latency_fields = dce4_afmt_write_latency_fields,
    197  1.1  riastrad 	.set_dto = dce4_hdmi_audio_set_dto,
    198  1.1  riastrad 	.update_acr = evergreen_hdmi_update_acr,
    199  1.1  riastrad 	.set_vbi_packet = dce4_set_vbi_packet,
    200  1.1  riastrad 	.set_color_depth = dce4_hdmi_set_color_depth,
    201  1.1  riastrad 	.set_avi_packet = evergreen_set_avi_packet,
    202  1.1  riastrad 	.set_audio_packet = dce4_set_audio_packet,
    203  1.1  riastrad 	.set_mute = dce4_set_mute,
    204  1.1  riastrad 	.mode_set = radeon_audio_hdmi_mode_set,
    205  1.1  riastrad 	.dpms = evergreen_hdmi_enable,
    206  1.1  riastrad };
    207  1.1  riastrad 
    208  1.1  riastrad static struct radeon_audio_funcs dce4_dp_funcs = {
    209  1.1  riastrad 	.get_pin = r600_audio_get_pin,
    210  1.1  riastrad 	.write_sad_regs = evergreen_hdmi_write_sad_regs,
    211  1.1  riastrad 	.write_speaker_allocation = dce4_afmt_dp_write_speaker_allocation,
    212  1.1  riastrad 	.write_latency_fields = dce4_afmt_write_latency_fields,
    213  1.1  riastrad 	.set_dto = dce4_dp_audio_set_dto,
    214  1.1  riastrad 	.set_avi_packet = evergreen_set_avi_packet,
    215  1.1  riastrad 	.set_audio_packet = dce4_set_audio_packet,
    216  1.1  riastrad 	.mode_set = radeon_audio_dp_mode_set,
    217  1.1  riastrad 	.dpms = evergreen_dp_enable,
    218  1.1  riastrad };
    219  1.1  riastrad 
    220  1.1  riastrad static struct radeon_audio_funcs dce6_hdmi_funcs = {
    221  1.1  riastrad 	.select_pin = dce6_afmt_select_pin,
    222  1.1  riastrad 	.get_pin = dce6_audio_get_pin,
    223  1.1  riastrad 	.write_sad_regs = dce6_afmt_write_sad_regs,
    224  1.1  riastrad 	.write_speaker_allocation = dce6_afmt_hdmi_write_speaker_allocation,
    225  1.1  riastrad 	.write_latency_fields = dce6_afmt_write_latency_fields,
    226  1.1  riastrad 	.set_dto = dce6_hdmi_audio_set_dto,
    227  1.1  riastrad 	.update_acr = evergreen_hdmi_update_acr,
    228  1.1  riastrad 	.set_vbi_packet = dce4_set_vbi_packet,
    229  1.1  riastrad 	.set_color_depth = dce4_hdmi_set_color_depth,
    230  1.1  riastrad 	.set_avi_packet = evergreen_set_avi_packet,
    231  1.1  riastrad 	.set_audio_packet = dce4_set_audio_packet,
    232  1.1  riastrad 	.set_mute = dce4_set_mute,
    233  1.1  riastrad 	.mode_set = radeon_audio_hdmi_mode_set,
    234  1.1  riastrad 	.dpms = evergreen_hdmi_enable,
    235  1.1  riastrad };
    236  1.1  riastrad 
    237  1.1  riastrad static struct radeon_audio_funcs dce6_dp_funcs = {
    238  1.1  riastrad 	.select_pin = dce6_afmt_select_pin,
    239  1.1  riastrad 	.get_pin = dce6_audio_get_pin,
    240  1.1  riastrad 	.write_sad_regs = dce6_afmt_write_sad_regs,
    241  1.1  riastrad 	.write_speaker_allocation = dce6_afmt_dp_write_speaker_allocation,
    242  1.1  riastrad 	.write_latency_fields = dce6_afmt_write_latency_fields,
    243  1.1  riastrad 	.set_dto = dce6_dp_audio_set_dto,
    244  1.1  riastrad 	.set_avi_packet = evergreen_set_avi_packet,
    245  1.1  riastrad 	.set_audio_packet = dce4_set_audio_packet,
    246  1.1  riastrad 	.mode_set = radeon_audio_dp_mode_set,
    247  1.1  riastrad 	.dpms = evergreen_dp_enable,
    248  1.1  riastrad };
    249  1.1  riastrad 
    250  1.1  riastrad static void radeon_audio_enable(struct radeon_device *rdev,
    251  1.1  riastrad 				struct r600_audio_pin *pin, u8 enable_mask)
    252  1.1  riastrad {
    253  1.1  riastrad 	struct drm_encoder *encoder;
    254  1.1  riastrad 	struct radeon_encoder *radeon_encoder;
    255  1.1  riastrad 	struct radeon_encoder_atom_dig *dig;
    256  1.1  riastrad 	int pin_count = 0;
    257  1.1  riastrad 
    258  1.1  riastrad 	if (!pin)
    259  1.1  riastrad 		return;
    260  1.1  riastrad 
    261  1.1  riastrad 	if (rdev->mode_info.mode_config_initialized) {
    262  1.1  riastrad 		list_for_each_entry(encoder, &rdev->ddev->mode_config.encoder_list, head) {
    263  1.1  riastrad 			if (radeon_encoder_is_digital(encoder)) {
    264  1.1  riastrad 				radeon_encoder = to_radeon_encoder(encoder);
    265  1.1  riastrad 				dig = radeon_encoder->enc_priv;
    266  1.1  riastrad 				if (dig->pin == pin)
    267  1.1  riastrad 					pin_count++;
    268  1.1  riastrad 			}
    269  1.1  riastrad 		}
    270  1.1  riastrad 
    271  1.1  riastrad 		if ((pin_count > 1) && (enable_mask == 0))
    272  1.1  riastrad 			return;
    273  1.1  riastrad 	}
    274  1.1  riastrad 
    275  1.1  riastrad 	if (rdev->audio.funcs->enable)
    276  1.1  riastrad 		rdev->audio.funcs->enable(rdev, pin, enable_mask);
    277  1.1  riastrad }
    278  1.1  riastrad 
    279  1.1  riastrad static void radeon_audio_interface_init(struct radeon_device *rdev)
    280  1.1  riastrad {
    281  1.1  riastrad 	if (ASIC_IS_DCE6(rdev)) {
    282  1.1  riastrad 		rdev->audio.funcs = &dce6_funcs;
    283  1.1  riastrad 		rdev->audio.hdmi_funcs = &dce6_hdmi_funcs;
    284  1.1  riastrad 		rdev->audio.dp_funcs = &dce6_dp_funcs;
    285  1.1  riastrad 	} else if (ASIC_IS_DCE4(rdev)) {
    286  1.1  riastrad 		rdev->audio.funcs = &dce4_funcs;
    287  1.1  riastrad 		rdev->audio.hdmi_funcs = &dce4_hdmi_funcs;
    288  1.1  riastrad 		rdev->audio.dp_funcs = &dce4_dp_funcs;
    289  1.1  riastrad 	} else if (ASIC_IS_DCE32(rdev)) {
    290  1.1  riastrad 		rdev->audio.funcs = &dce32_funcs;
    291  1.1  riastrad 		rdev->audio.hdmi_funcs = &dce32_hdmi_funcs;
    292  1.1  riastrad 		rdev->audio.dp_funcs = &dce32_dp_funcs;
    293  1.1  riastrad 	} else {
    294  1.1  riastrad 		rdev->audio.funcs = &r600_funcs;
    295  1.1  riastrad 		rdev->audio.hdmi_funcs = &r600_hdmi_funcs;
    296  1.3  riastrad 		rdev->audio.dp_funcs = NULL;
    297  1.1  riastrad 	}
    298  1.1  riastrad }
    299  1.1  riastrad 
    300  1.1  riastrad static int radeon_audio_chipset_supported(struct radeon_device *rdev)
    301  1.1  riastrad {
    302  1.1  riastrad 	return ASIC_IS_DCE2(rdev) && !ASIC_IS_NODCE(rdev);
    303  1.1  riastrad }
    304  1.1  riastrad 
    305  1.1  riastrad int radeon_audio_init(struct radeon_device *rdev)
    306  1.1  riastrad {
    307  1.1  riastrad 	int i;
    308  1.1  riastrad 
    309  1.1  riastrad 	if (!radeon_audio || !radeon_audio_chipset_supported(rdev))
    310  1.1  riastrad 		return 0;
    311  1.1  riastrad 
    312  1.1  riastrad 	rdev->audio.enabled = true;
    313  1.1  riastrad 
    314  1.1  riastrad 	if (ASIC_IS_DCE83(rdev))		/* KB: 2 streams, 3 endpoints */
    315  1.1  riastrad 		rdev->audio.num_pins = 3;
    316  1.1  riastrad 	else if (ASIC_IS_DCE81(rdev))	/* KV: 4 streams, 7 endpoints */
    317  1.1  riastrad 		rdev->audio.num_pins = 7;
    318  1.1  riastrad 	else if (ASIC_IS_DCE8(rdev))	/* BN/HW: 6 streams, 7 endpoints */
    319  1.1  riastrad 		rdev->audio.num_pins = 7;
    320  1.1  riastrad 	else if (ASIC_IS_DCE64(rdev))	/* OL: 2 streams, 2 endpoints */
    321  1.1  riastrad 		rdev->audio.num_pins = 2;
    322  1.1  riastrad 	else if (ASIC_IS_DCE61(rdev))	/* TN: 4 streams, 6 endpoints */
    323  1.1  riastrad 		rdev->audio.num_pins = 6;
    324  1.1  riastrad 	else if (ASIC_IS_DCE6(rdev))	/* SI: 6 streams, 6 endpoints */
    325  1.1  riastrad 		rdev->audio.num_pins = 6;
    326  1.1  riastrad 	else
    327  1.1  riastrad 		rdev->audio.num_pins = 1;
    328  1.1  riastrad 
    329  1.1  riastrad 	for (i = 0; i < rdev->audio.num_pins; i++) {
    330  1.1  riastrad 		rdev->audio.pin[i].channels = -1;
    331  1.1  riastrad 		rdev->audio.pin[i].rate = -1;
    332  1.1  riastrad 		rdev->audio.pin[i].bits_per_sample = -1;
    333  1.1  riastrad 		rdev->audio.pin[i].status_bits = 0;
    334  1.1  riastrad 		rdev->audio.pin[i].category_code = 0;
    335  1.1  riastrad 		rdev->audio.pin[i].connected = false;
    336  1.1  riastrad 		rdev->audio.pin[i].offset = pin_offsets[i];
    337  1.1  riastrad 		rdev->audio.pin[i].id = i;
    338  1.1  riastrad 	}
    339  1.1  riastrad 
    340  1.1  riastrad 	radeon_audio_interface_init(rdev);
    341  1.1  riastrad 
    342  1.1  riastrad 	/* disable audio.  it will be set up later */
    343  1.1  riastrad 	for (i = 0; i < rdev->audio.num_pins; i++)
    344  1.1  riastrad 		radeon_audio_enable(rdev, &rdev->audio.pin[i], 0);
    345  1.1  riastrad 
    346  1.1  riastrad 	return 0;
    347  1.1  riastrad }
    348  1.1  riastrad 
    349  1.1  riastrad u32 radeon_audio_endpoint_rreg(struct radeon_device *rdev, u32 offset, u32 reg)
    350  1.1  riastrad {
    351  1.1  riastrad 	if (rdev->audio.funcs->endpoint_rreg)
    352  1.1  riastrad 		return rdev->audio.funcs->endpoint_rreg(rdev, offset, reg);
    353  1.1  riastrad 
    354  1.1  riastrad 	return 0;
    355  1.1  riastrad }
    356  1.1  riastrad 
    357  1.1  riastrad void radeon_audio_endpoint_wreg(struct radeon_device *rdev, u32 offset,
    358  1.1  riastrad 	u32 reg, u32 v)
    359  1.1  riastrad {
    360  1.1  riastrad 	if (rdev->audio.funcs->endpoint_wreg)
    361  1.1  riastrad 		rdev->audio.funcs->endpoint_wreg(rdev, offset, reg, v);
    362  1.1  riastrad }
    363  1.1  riastrad 
    364  1.1  riastrad static void radeon_audio_write_sad_regs(struct drm_encoder *encoder)
    365  1.1  riastrad {
    366  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    367  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    368  1.1  riastrad 	struct cea_sad *sads;
    369  1.1  riastrad 	int sad_count;
    370  1.1  riastrad 
    371  1.1  riastrad 	if (!connector)
    372  1.1  riastrad 		return;
    373  1.1  riastrad 
    374  1.1  riastrad 	sad_count = drm_edid_to_sad(radeon_connector_edid(connector), &sads);
    375  1.3  riastrad 	if (sad_count < 0)
    376  1.1  riastrad 		DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
    377  1.3  riastrad 	if (sad_count <= 0)
    378  1.1  riastrad 		return;
    379  1.1  riastrad 	BUG_ON(!sads);
    380  1.1  riastrad 
    381  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->write_sad_regs)
    382  1.1  riastrad 		radeon_encoder->audio->write_sad_regs(encoder, sads, sad_count);
    383  1.1  riastrad 
    384  1.1  riastrad 	kfree(sads);
    385  1.1  riastrad }
    386  1.1  riastrad 
    387  1.1  riastrad static void radeon_audio_write_speaker_allocation(struct drm_encoder *encoder)
    388  1.1  riastrad {
    389  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    390  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    391  1.1  riastrad 	u8 *sadb = NULL;
    392  1.1  riastrad 	int sad_count;
    393  1.1  riastrad 
    394  1.1  riastrad 	if (!connector)
    395  1.1  riastrad 		return;
    396  1.1  riastrad 
    397  1.1  riastrad 	sad_count = drm_edid_to_speaker_allocation(radeon_connector_edid(connector),
    398  1.1  riastrad 						   &sadb);
    399  1.1  riastrad 	if (sad_count < 0) {
    400  1.1  riastrad 		DRM_DEBUG("Couldn't read Speaker Allocation Data Block: %d\n",
    401  1.1  riastrad 			  sad_count);
    402  1.1  riastrad 		sad_count = 0;
    403  1.1  riastrad 	}
    404  1.1  riastrad 
    405  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->write_speaker_allocation)
    406  1.1  riastrad 		radeon_encoder->audio->write_speaker_allocation(encoder, sadb, sad_count);
    407  1.1  riastrad 
    408  1.1  riastrad 	kfree(sadb);
    409  1.1  riastrad }
    410  1.1  riastrad 
    411  1.1  riastrad static void radeon_audio_write_latency_fields(struct drm_encoder *encoder,
    412  1.1  riastrad 					      struct drm_display_mode *mode)
    413  1.1  riastrad {
    414  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    415  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    416  1.1  riastrad 
    417  1.1  riastrad 	if (!connector)
    418  1.1  riastrad 		return;
    419  1.1  riastrad 
    420  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->write_latency_fields)
    421  1.1  riastrad 		radeon_encoder->audio->write_latency_fields(encoder, connector, mode);
    422  1.1  riastrad }
    423  1.1  riastrad 
    424  1.1  riastrad struct r600_audio_pin* radeon_audio_get_pin(struct drm_encoder *encoder)
    425  1.1  riastrad {
    426  1.1  riastrad 	struct radeon_device *rdev = encoder->dev->dev_private;
    427  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    428  1.1  riastrad 
    429  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->get_pin)
    430  1.1  riastrad 		return radeon_encoder->audio->get_pin(rdev);
    431  1.1  riastrad 
    432  1.1  riastrad 	return NULL;
    433  1.1  riastrad }
    434  1.1  riastrad 
    435  1.1  riastrad static void radeon_audio_select_pin(struct drm_encoder *encoder)
    436  1.1  riastrad {
    437  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    438  1.1  riastrad 
    439  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->select_pin)
    440  1.1  riastrad 		radeon_encoder->audio->select_pin(encoder);
    441  1.1  riastrad }
    442  1.1  riastrad 
    443  1.1  riastrad void radeon_audio_detect(struct drm_connector *connector,
    444  1.1  riastrad 			 struct drm_encoder *encoder,
    445  1.1  riastrad 			 enum drm_connector_status status)
    446  1.1  riastrad {
    447  1.1  riastrad 	struct drm_device *dev = connector->dev;
    448  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    449  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    450  1.1  riastrad 	struct radeon_encoder_atom_dig *dig;
    451  1.1  riastrad 
    452  1.1  riastrad 	if (!radeon_audio_chipset_supported(rdev))
    453  1.1  riastrad 		return;
    454  1.1  riastrad 
    455  1.1  riastrad 	if (!radeon_encoder_is_digital(encoder))
    456  1.1  riastrad 		return;
    457  1.1  riastrad 
    458  1.1  riastrad 	dig = radeon_encoder->enc_priv;
    459  1.1  riastrad 
    460  1.1  riastrad 	if (status == connector_status_connected) {
    461  1.1  riastrad 		if (connector->connector_type == DRM_MODE_CONNECTOR_DisplayPort) {
    462  1.1  riastrad 			struct radeon_connector *radeon_connector = to_radeon_connector(connector);
    463  1.1  riastrad 
    464  1.1  riastrad 			if (radeon_dp_getsinktype(radeon_connector) ==
    465  1.1  riastrad 			    CONNECTOR_OBJECT_ID_DISPLAYPORT)
    466  1.1  riastrad 				radeon_encoder->audio = rdev->audio.dp_funcs;
    467  1.1  riastrad 			else
    468  1.1  riastrad 				radeon_encoder->audio = rdev->audio.hdmi_funcs;
    469  1.1  riastrad 		} else {
    470  1.1  riastrad 			radeon_encoder->audio = rdev->audio.hdmi_funcs;
    471  1.1  riastrad 		}
    472  1.1  riastrad 
    473  1.1  riastrad 		if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
    474  1.1  riastrad 			if (!dig->pin)
    475  1.1  riastrad 				dig->pin = radeon_audio_get_pin(encoder);
    476  1.1  riastrad 			radeon_audio_enable(rdev, dig->pin, 0xf);
    477  1.1  riastrad 		} else {
    478  1.1  riastrad 			radeon_audio_enable(rdev, dig->pin, 0);
    479  1.1  riastrad 			dig->pin = NULL;
    480  1.1  riastrad 		}
    481  1.1  riastrad 	} else {
    482  1.1  riastrad 		radeon_audio_enable(rdev, dig->pin, 0);
    483  1.1  riastrad 		dig->pin = NULL;
    484  1.1  riastrad 	}
    485  1.1  riastrad }
    486  1.1  riastrad 
    487  1.1  riastrad void radeon_audio_fini(struct radeon_device *rdev)
    488  1.1  riastrad {
    489  1.1  riastrad 	int i;
    490  1.1  riastrad 
    491  1.1  riastrad 	if (!rdev->audio.enabled)
    492  1.1  riastrad 		return;
    493  1.1  riastrad 
    494  1.1  riastrad 	for (i = 0; i < rdev->audio.num_pins; i++)
    495  1.1  riastrad 		radeon_audio_enable(rdev, &rdev->audio.pin[i], 0);
    496  1.1  riastrad 
    497  1.1  riastrad 	rdev->audio.enabled = false;
    498  1.1  riastrad }
    499  1.1  riastrad 
    500  1.1  riastrad static void radeon_audio_set_dto(struct drm_encoder *encoder, unsigned int clock)
    501  1.1  riastrad {
    502  1.1  riastrad 	struct radeon_device *rdev = encoder->dev->dev_private;
    503  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    504  1.1  riastrad 	struct radeon_crtc *crtc = to_radeon_crtc(encoder->crtc);
    505  1.1  riastrad 
    506  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->set_dto)
    507  1.1  riastrad 		radeon_encoder->audio->set_dto(rdev, crtc, clock);
    508  1.1  riastrad }
    509  1.1  riastrad 
    510  1.1  riastrad static int radeon_audio_set_avi_packet(struct drm_encoder *encoder,
    511  1.1  riastrad 				       struct drm_display_mode *mode)
    512  1.1  riastrad {
    513  1.1  riastrad 	struct radeon_device *rdev = encoder->dev->dev_private;
    514  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    515  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    516  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    517  1.1  riastrad 	u8 buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AVI_INFOFRAME_SIZE];
    518  1.1  riastrad 	struct hdmi_avi_infoframe frame;
    519  1.1  riastrad 	int err;
    520  1.1  riastrad 
    521  1.1  riastrad 	if (!connector)
    522  1.1  riastrad 		return -EINVAL;
    523  1.1  riastrad 
    524  1.3  riastrad 	err = drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);
    525  1.1  riastrad 	if (err < 0) {
    526  1.1  riastrad 		DRM_ERROR("failed to setup AVI infoframe: %d\n", err);
    527  1.1  riastrad 		return err;
    528  1.1  riastrad 	}
    529  1.1  riastrad 
    530  1.1  riastrad 	if (radeon_encoder->output_csc != RADEON_OUTPUT_CSC_BYPASS) {
    531  1.3  riastrad 		drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,
    532  1.3  riastrad 						   radeon_encoder->output_csc == RADEON_OUTPUT_CSC_TVRGB ?
    533  1.3  riastrad 						   HDMI_QUANTIZATION_RANGE_LIMITED :
    534  1.3  riastrad 						   HDMI_QUANTIZATION_RANGE_FULL);
    535  1.1  riastrad 	}
    536  1.1  riastrad 
    537  1.1  riastrad 	err = hdmi_avi_infoframe_pack(&frame, buffer, sizeof(buffer));
    538  1.1  riastrad 	if (err < 0) {
    539  1.1  riastrad 		DRM_ERROR("failed to pack AVI infoframe: %d\n", err);
    540  1.1  riastrad 		return err;
    541  1.1  riastrad 	}
    542  1.1  riastrad 
    543  1.1  riastrad 	if (dig && dig->afmt && radeon_encoder->audio &&
    544  1.1  riastrad 	    radeon_encoder->audio->set_avi_packet)
    545  1.1  riastrad 		radeon_encoder->audio->set_avi_packet(rdev, dig->afmt->offset,
    546  1.1  riastrad 			buffer, sizeof(buffer));
    547  1.1  riastrad 
    548  1.1  riastrad 	return 0;
    549  1.1  riastrad }
    550  1.1  riastrad 
    551  1.1  riastrad /*
    552  1.1  riastrad  * calculate CTS and N values if they are not found in the table
    553  1.1  riastrad  */
    554  1.1  riastrad static void radeon_audio_calc_cts(unsigned int clock, int *CTS, int *N, int freq)
    555  1.1  riastrad {
    556  1.1  riastrad 	int n, cts;
    557  1.1  riastrad 	unsigned long div, mul;
    558  1.1  riastrad 
    559  1.1  riastrad 	/* Safe, but overly large values */
    560  1.1  riastrad 	n = 128 * freq;
    561  1.1  riastrad 	cts = clock * 1000;
    562  1.1  riastrad 
    563  1.1  riastrad 	/* Smallest valid fraction */
    564  1.1  riastrad 	div = gcd(n, cts);
    565  1.1  riastrad 
    566  1.1  riastrad 	n /= div;
    567  1.1  riastrad 	cts /= div;
    568  1.1  riastrad 
    569  1.1  riastrad 	/*
    570  1.1  riastrad 	 * The optimal N is 128*freq/1000. Calculate the closest larger
    571  1.1  riastrad 	 * value that doesn't truncate any bits.
    572  1.1  riastrad 	 */
    573  1.1  riastrad 	mul = ((128*freq/1000) + (n-1))/n;
    574  1.1  riastrad 
    575  1.1  riastrad 	n *= mul;
    576  1.1  riastrad 	cts *= mul;
    577  1.1  riastrad 
    578  1.1  riastrad 	/* Check that we are in spec (not always possible) */
    579  1.1  riastrad 	if (n < (128*freq/1500))
    580  1.3  riastrad 		pr_warn("Calculated ACR N value is too small. You may experience audio problems.\n");
    581  1.1  riastrad 	if (n > (128*freq/300))
    582  1.3  riastrad 		pr_warn("Calculated ACR N value is too large. You may experience audio problems.\n");
    583  1.1  riastrad 
    584  1.1  riastrad 	*N = n;
    585  1.1  riastrad 	*CTS = cts;
    586  1.1  riastrad 
    587  1.1  riastrad 	DRM_DEBUG("Calculated ACR timing N=%d CTS=%d for frequency %d\n",
    588  1.1  riastrad 		*N, *CTS, freq);
    589  1.1  riastrad }
    590  1.1  riastrad 
    591  1.1  riastrad static const struct radeon_hdmi_acr* radeon_audio_acr(unsigned int clock)
    592  1.1  riastrad {
    593  1.1  riastrad 	static struct radeon_hdmi_acr res;
    594  1.1  riastrad 	u8 i;
    595  1.1  riastrad 
    596  1.1  riastrad 	static const struct radeon_hdmi_acr hdmi_predefined_acr[] = {
    597  1.1  riastrad 		/*       32kHz    44.1kHz   48kHz    */
    598  1.1  riastrad 		/* Clock      N     CTS      N     CTS      N     CTS */
    599  1.1  riastrad 		{  25175,  4096,  25175, 28224, 125875,  6144,  25175 }, /*  25,20/1.001 MHz */
    600  1.1  riastrad 		{  25200,  4096,  25200,  6272,  28000,  6144,  25200 }, /*  25.20       MHz */
    601  1.1  riastrad 		{  27000,  4096,  27000,  6272,  30000,  6144,  27000 }, /*  27.00       MHz */
    602  1.1  riastrad 		{  27027,  4096,  27027,  6272,  30030,  6144,  27027 }, /*  27.00*1.001 MHz */
    603  1.1  riastrad 		{  54000,  4096,  54000,  6272,  60000,  6144,  54000 }, /*  54.00       MHz */
    604  1.1  riastrad 		{  54054,  4096,  54054,  6272,  60060,  6144,  54054 }, /*  54.00*1.001 MHz */
    605  1.1  riastrad 		{  74176,  4096,  74176,  5733,  75335,  6144,  74176 }, /*  74.25/1.001 MHz */
    606  1.1  riastrad 		{  74250,  4096,  74250,  6272,  82500,  6144,  74250 }, /*  74.25       MHz */
    607  1.1  riastrad 		{ 148352,  4096, 148352,  5733, 150670,  6144, 148352 }, /* 148.50/1.001 MHz */
    608  1.1  riastrad 		{ 148500,  4096, 148500,  6272, 165000,  6144, 148500 }, /* 148.50       MHz */
    609  1.1  riastrad 	};
    610  1.1  riastrad 
    611  1.1  riastrad 	/* Precalculated values for common clocks */
    612  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(hdmi_predefined_acr); i++)
    613  1.1  riastrad 		if (hdmi_predefined_acr[i].clock == clock)
    614  1.1  riastrad 			return &hdmi_predefined_acr[i];
    615  1.1  riastrad 
    616  1.1  riastrad 	/* And odd clocks get manually calculated */
    617  1.1  riastrad 	radeon_audio_calc_cts(clock, &res.cts_32khz, &res.n_32khz, 32000);
    618  1.1  riastrad 	radeon_audio_calc_cts(clock, &res.cts_44_1khz, &res.n_44_1khz, 44100);
    619  1.1  riastrad 	radeon_audio_calc_cts(clock, &res.cts_48khz, &res.n_48khz, 48000);
    620  1.1  riastrad 
    621  1.1  riastrad 	return &res;
    622  1.1  riastrad }
    623  1.1  riastrad 
    624  1.1  riastrad /*
    625  1.1  riastrad  * update the N and CTS parameters for a given pixel clock rate
    626  1.1  riastrad  */
    627  1.1  riastrad static void radeon_audio_update_acr(struct drm_encoder *encoder, unsigned int clock)
    628  1.1  riastrad {
    629  1.1  riastrad 	const struct radeon_hdmi_acr *acr = radeon_audio_acr(clock);
    630  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    631  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    632  1.1  riastrad 
    633  1.1  riastrad 	if (!dig || !dig->afmt)
    634  1.1  riastrad 		return;
    635  1.1  riastrad 
    636  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->update_acr)
    637  1.1  riastrad 		radeon_encoder->audio->update_acr(encoder, dig->afmt->offset, acr);
    638  1.1  riastrad }
    639  1.1  riastrad 
    640  1.1  riastrad static void radeon_audio_set_vbi_packet(struct drm_encoder *encoder)
    641  1.1  riastrad {
    642  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    643  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    644  1.1  riastrad 
    645  1.1  riastrad 	if (!dig || !dig->afmt)
    646  1.1  riastrad 		return;
    647  1.1  riastrad 
    648  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->set_vbi_packet)
    649  1.1  riastrad 		radeon_encoder->audio->set_vbi_packet(encoder, dig->afmt->offset);
    650  1.1  riastrad }
    651  1.1  riastrad 
    652  1.1  riastrad static void radeon_hdmi_set_color_depth(struct drm_encoder *encoder)
    653  1.1  riastrad {
    654  1.1  riastrad 	int bpc = 8;
    655  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    656  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    657  1.1  riastrad 
    658  1.1  riastrad 	if (!dig || !dig->afmt)
    659  1.1  riastrad 		return;
    660  1.1  riastrad 
    661  1.1  riastrad 	if (encoder->crtc) {
    662  1.1  riastrad 		struct radeon_crtc *radeon_crtc = to_radeon_crtc(encoder->crtc);
    663  1.1  riastrad 		bpc = radeon_crtc->bpc;
    664  1.1  riastrad 	}
    665  1.1  riastrad 
    666  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->set_color_depth)
    667  1.1  riastrad 		radeon_encoder->audio->set_color_depth(encoder, dig->afmt->offset, bpc);
    668  1.1  riastrad }
    669  1.1  riastrad 
    670  1.1  riastrad static void radeon_audio_set_audio_packet(struct drm_encoder *encoder)
    671  1.1  riastrad {
    672  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    673  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    674  1.1  riastrad 
    675  1.1  riastrad 	if (!dig || !dig->afmt)
    676  1.1  riastrad 		return;
    677  1.1  riastrad 
    678  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->set_audio_packet)
    679  1.1  riastrad 		radeon_encoder->audio->set_audio_packet(encoder, dig->afmt->offset);
    680  1.1  riastrad }
    681  1.1  riastrad 
    682  1.1  riastrad static void radeon_audio_set_mute(struct drm_encoder *encoder, bool mute)
    683  1.1  riastrad {
    684  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    685  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    686  1.1  riastrad 
    687  1.1  riastrad 	if (!dig || !dig->afmt)
    688  1.1  riastrad 		return;
    689  1.1  riastrad 
    690  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->set_mute)
    691  1.1  riastrad 		radeon_encoder->audio->set_mute(encoder, dig->afmt->offset, mute);
    692  1.1  riastrad }
    693  1.1  riastrad 
    694  1.1  riastrad /*
    695  1.1  riastrad  * update the info frames with the data from the current display mode
    696  1.1  riastrad  */
    697  1.1  riastrad static void radeon_audio_hdmi_mode_set(struct drm_encoder *encoder,
    698  1.1  riastrad 				       struct drm_display_mode *mode)
    699  1.1  riastrad {
    700  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    701  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    702  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    703  1.1  riastrad 
    704  1.1  riastrad 	if (!dig || !dig->afmt)
    705  1.1  riastrad 		return;
    706  1.1  riastrad 
    707  1.1  riastrad 	if (!connector)
    708  1.1  riastrad 		return;
    709  1.1  riastrad 
    710  1.1  riastrad 	if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
    711  1.1  riastrad 		radeon_audio_set_mute(encoder, true);
    712  1.1  riastrad 
    713  1.1  riastrad 		radeon_audio_write_speaker_allocation(encoder);
    714  1.1  riastrad 		radeon_audio_write_sad_regs(encoder);
    715  1.1  riastrad 		radeon_audio_write_latency_fields(encoder, mode);
    716  1.1  riastrad 		radeon_audio_set_dto(encoder, mode->clock);
    717  1.1  riastrad 		radeon_audio_set_vbi_packet(encoder);
    718  1.1  riastrad 		radeon_hdmi_set_color_depth(encoder);
    719  1.1  riastrad 		radeon_audio_update_acr(encoder, mode->clock);
    720  1.1  riastrad 		radeon_audio_set_audio_packet(encoder);
    721  1.1  riastrad 		radeon_audio_select_pin(encoder);
    722  1.1  riastrad 
    723  1.1  riastrad 		if (radeon_audio_set_avi_packet(encoder, mode) < 0)
    724  1.1  riastrad 			return;
    725  1.1  riastrad 
    726  1.1  riastrad 		radeon_audio_set_mute(encoder, false);
    727  1.1  riastrad 	} else {
    728  1.1  riastrad 		radeon_hdmi_set_color_depth(encoder);
    729  1.1  riastrad 
    730  1.1  riastrad 		if (radeon_audio_set_avi_packet(encoder, mode) < 0)
    731  1.1  riastrad 			return;
    732  1.1  riastrad 	}
    733  1.1  riastrad }
    734  1.1  riastrad 
    735  1.1  riastrad static void radeon_audio_dp_mode_set(struct drm_encoder *encoder,
    736  1.1  riastrad 				     struct drm_display_mode *mode)
    737  1.1  riastrad {
    738  1.1  riastrad 	struct drm_device *dev = encoder->dev;
    739  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    740  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    741  1.1  riastrad 	struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
    742  1.1  riastrad 	struct drm_connector *connector = radeon_get_connector_for_encoder(encoder);
    743  1.1  riastrad 
    744  1.1  riastrad 	if (!dig || !dig->afmt)
    745  1.1  riastrad 		return;
    746  1.1  riastrad 
    747  1.1  riastrad 	if (!connector)
    748  1.1  riastrad 		return;
    749  1.1  riastrad 
    750  1.1  riastrad 	if (drm_detect_monitor_audio(radeon_connector_edid(connector))) {
    751  1.1  riastrad 		radeon_audio_write_speaker_allocation(encoder);
    752  1.1  riastrad 		radeon_audio_write_sad_regs(encoder);
    753  1.1  riastrad 		radeon_audio_write_latency_fields(encoder, mode);
    754  1.1  riastrad 		radeon_audio_set_dto(encoder, rdev->clock.vco_freq * 10);
    755  1.1  riastrad 		radeon_audio_set_audio_packet(encoder);
    756  1.1  riastrad 		radeon_audio_select_pin(encoder);
    757  1.1  riastrad 
    758  1.1  riastrad 		if (radeon_audio_set_avi_packet(encoder, mode) < 0)
    759  1.1  riastrad 			return;
    760  1.1  riastrad 	}
    761  1.1  riastrad }
    762  1.1  riastrad 
    763  1.1  riastrad void radeon_audio_mode_set(struct drm_encoder *encoder,
    764  1.1  riastrad 			   struct drm_display_mode *mode)
    765  1.1  riastrad {
    766  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    767  1.1  riastrad 
    768  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->mode_set)
    769  1.1  riastrad 		radeon_encoder->audio->mode_set(encoder, mode);
    770  1.1  riastrad }
    771  1.1  riastrad 
    772  1.1  riastrad void radeon_audio_dpms(struct drm_encoder *encoder, int mode)
    773  1.1  riastrad {
    774  1.1  riastrad 	struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
    775  1.1  riastrad 
    776  1.1  riastrad 	if (radeon_encoder->audio && radeon_encoder->audio->dpms)
    777  1.1  riastrad 		radeon_encoder->audio->dpms(encoder, mode == DRM_MODE_DPMS_ON);
    778  1.1  riastrad }
    779  1.1  riastrad 
    780  1.1  riastrad unsigned int radeon_audio_decode_dfs_div(unsigned int div)
    781  1.1  riastrad {
    782  1.1  riastrad 	if (div >= 8 && div < 64)
    783  1.1  riastrad 		return (div - 8) * 25 + 200;
    784  1.1  riastrad 	else if (div >= 64 && div < 96)
    785  1.1  riastrad 		return (div - 64) * 50 + 1600;
    786  1.1  riastrad 	else if (div >= 96 && div < 128)
    787  1.1  riastrad 		return (div - 96) * 100 + 3200;
    788  1.1  riastrad 	else
    789  1.1  riastrad 		return 0;
    790  1.1  riastrad }
    791