Path: blob/master/drivers/gpu/drm/radeon/r600_audio.c
15113 views
/*1* Copyright 2008 Advanced Micro Devices, Inc.2* Copyright 2008 Red Hat Inc.3* Copyright 2009 Christian König.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the "Software"),7* to deal in the Software without restriction, including without limitation8* the rights to use, copy, modify, merge, publish, distribute, sublicense,9* and/or sell copies of the Software, and to permit persons to whom the10* Software is furnished to do so, subject to the following conditions:11*12* The above copyright notice and this permission notice shall be included in13* all copies or substantial portions of the Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR19* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,20* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR21* OTHER DEALINGS IN THE SOFTWARE.22*23* Authors: Christian König24*/25#include "drmP.h"26#include "radeon.h"27#include "radeon_reg.h"28#include "radeon_asic.h"29#include "atom.h"3031#define AUDIO_TIMER_INTERVALL 100 /* 1/10 sekund should be enough */3233/*34* check if the chipset is supported35*/36static int r600_audio_chipset_supported(struct radeon_device *rdev)37{38return (rdev->family >= CHIP_R600 && rdev->family < CHIP_CEDAR)39|| rdev->family == CHIP_RS60040|| rdev->family == CHIP_RS69041|| rdev->family == CHIP_RS740;42}4344/*45* current number of channels46*/47int r600_audio_channels(struct radeon_device *rdev)48{49return (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0x7) + 1;50}5152/*53* current bits per sample54*/55int r600_audio_bits_per_sample(struct radeon_device *rdev)56{57uint32_t value = (RREG32(R600_AUDIO_RATE_BPS_CHANNEL) & 0xF0) >> 4;58switch (value) {59case 0x0: return 8;60case 0x1: return 16;61case 0x2: return 20;62case 0x3: return 24;63case 0x4: return 32;64}6566dev_err(rdev->dev, "Unknown bits per sample 0x%x using 16 instead\n",67(int)value);6869return 16;70}7172/*73* current sampling rate in HZ74*/75int r600_audio_rate(struct radeon_device *rdev)76{77uint32_t value = RREG32(R600_AUDIO_RATE_BPS_CHANNEL);78uint32_t result;7980if (value & 0x4000)81result = 44100;82else83result = 48000;8485result *= ((value >> 11) & 0x7) + 1;86result /= ((value >> 8) & 0x7) + 1;8788return result;89}9091/*92* iec 60958 status bits93*/94uint8_t r600_audio_status_bits(struct radeon_device *rdev)95{96return RREG32(R600_AUDIO_STATUS_BITS) & 0xff;97}9899/*100* iec 60958 category code101*/102uint8_t r600_audio_category_code(struct radeon_device *rdev)103{104return (RREG32(R600_AUDIO_STATUS_BITS) >> 8) & 0xff;105}106107/*108* schedule next audio update event109*/110void r600_audio_schedule_polling(struct radeon_device *rdev)111{112mod_timer(&rdev->audio_timer,113jiffies + msecs_to_jiffies(AUDIO_TIMER_INTERVALL));114}115116/*117* update all hdmi interfaces with current audio parameters118*/119static void r600_audio_update_hdmi(unsigned long param)120{121struct radeon_device *rdev = (struct radeon_device *)param;122struct drm_device *dev = rdev->ddev;123124int channels = r600_audio_channels(rdev);125int rate = r600_audio_rate(rdev);126int bps = r600_audio_bits_per_sample(rdev);127uint8_t status_bits = r600_audio_status_bits(rdev);128uint8_t category_code = r600_audio_category_code(rdev);129130struct drm_encoder *encoder;131int changes = 0, still_going = 0;132133changes |= channels != rdev->audio_channels;134changes |= rate != rdev->audio_rate;135changes |= bps != rdev->audio_bits_per_sample;136changes |= status_bits != rdev->audio_status_bits;137changes |= category_code != rdev->audio_category_code;138139if (changes) {140rdev->audio_channels = channels;141rdev->audio_rate = rate;142rdev->audio_bits_per_sample = bps;143rdev->audio_status_bits = status_bits;144rdev->audio_category_code = category_code;145}146147list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {148struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);149still_going |= radeon_encoder->audio_polling_active;150if (changes || r600_hdmi_buffer_status_changed(encoder))151r600_hdmi_update_audio_settings(encoder);152}153154if (still_going)155r600_audio_schedule_polling(rdev);156}157158/*159* turn on/off audio engine160*/161static void r600_audio_engine_enable(struct radeon_device *rdev, bool enable)162{163DRM_INFO("%s audio support\n", enable ? "Enabling" : "Disabling");164WREG32_P(R600_AUDIO_ENABLE, enable ? 0x81000000 : 0x0, ~0x81000000);165rdev->audio_enabled = enable;166}167168/*169* initialize the audio vars and register the update timer170*/171int r600_audio_init(struct radeon_device *rdev)172{173if (!radeon_audio || !r600_audio_chipset_supported(rdev))174return 0;175176r600_audio_engine_enable(rdev, true);177178rdev->audio_channels = -1;179rdev->audio_rate = -1;180rdev->audio_bits_per_sample = -1;181rdev->audio_status_bits = 0;182rdev->audio_category_code = 0;183184setup_timer(185&rdev->audio_timer,186r600_audio_update_hdmi,187(unsigned long)rdev);188189return 0;190}191192/*193* enable the polling timer, to check for status changes194*/195void r600_audio_enable_polling(struct drm_encoder *encoder)196{197struct drm_device *dev = encoder->dev;198struct radeon_device *rdev = dev->dev_private;199struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);200201DRM_DEBUG("r600_audio_enable_polling: %d\n",202radeon_encoder->audio_polling_active);203if (radeon_encoder->audio_polling_active)204return;205206radeon_encoder->audio_polling_active = 1;207if (rdev->audio_enabled)208mod_timer(&rdev->audio_timer, jiffies + 1);209}210211/*212* disable the polling timer, so we get no more status updates213*/214void r600_audio_disable_polling(struct drm_encoder *encoder)215{216struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);217DRM_DEBUG("r600_audio_disable_polling: %d\n",218radeon_encoder->audio_polling_active);219radeon_encoder->audio_polling_active = 0;220}221222/*223* atach the audio codec to the clock source of the encoder224*/225void r600_audio_set_clock(struct drm_encoder *encoder, int clock)226{227struct drm_device *dev = encoder->dev;228struct radeon_device *rdev = dev->dev_private;229struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);230struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;231int base_rate = 48000;232233switch (radeon_encoder->encoder_id) {234case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_TMDS1:235case ENCODER_OBJECT_ID_INTERNAL_LVTM1:236WREG32_P(R600_AUDIO_TIMING, 0, ~0x301);237break;238case ENCODER_OBJECT_ID_INTERNAL_UNIPHY:239case ENCODER_OBJECT_ID_INTERNAL_UNIPHY1:240case ENCODER_OBJECT_ID_INTERNAL_UNIPHY2:241case ENCODER_OBJECT_ID_INTERNAL_KLDSCP_LVTMA:242WREG32_P(R600_AUDIO_TIMING, 0x100, ~0x301);243break;244default:245dev_err(rdev->dev, "Unsupported encoder type 0x%02X\n",246radeon_encoder->encoder_id);247return;248}249250switch (dig->dig_encoder) {251case 0:252WREG32(R600_AUDIO_PLL1_MUL, base_rate * 50);253WREG32(R600_AUDIO_PLL1_DIV, clock * 100);254WREG32(R600_AUDIO_CLK_SRCSEL, 0);255break;256257case 1:258WREG32(R600_AUDIO_PLL2_MUL, base_rate * 50);259WREG32(R600_AUDIO_PLL2_DIV, clock * 100);260WREG32(R600_AUDIO_CLK_SRCSEL, 1);261break;262default:263dev_err(rdev->dev, "Unsupported DIG on encoder 0x%02X\n",264radeon_encoder->encoder_id);265return;266}267}268269/*270* release the audio timer271* TODO: How to do this correctly on SMP systems?272*/273void r600_audio_fini(struct radeon_device *rdev)274{275if (!rdev->audio_enabled)276return;277278del_timer(&rdev->audio_timer);279280r600_audio_engine_enable(rdev, false);281}282283284