Path: blob/master/drivers/gpu/drm/display/drm_dp_helper.c
50337 views
/*1* Copyright © 2009 Keith Packard2*3* Permission to use, copy, modify, distribute, and sell this software and its4* documentation for any purpose is hereby granted without fee, provided that5* the above copyright notice appear in all copies and that both that copyright6* notice and this permission notice appear in supporting documentation, and7* that the name of the copyright holders not be used in advertising or8* publicity pertaining to distribution of the software without specific,9* written prior permission. The copyright holders make no representations10* about the suitability of this software for any purpose. It is provided "as11* is" without express or implied warranty.12*13* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,14* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO15* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR16* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,17* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER18* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE19* OF THIS SOFTWARE.20*/2122#include <linux/backlight.h>23#include <linux/delay.h>24#include <linux/dynamic_debug.h>25#include <linux/errno.h>26#include <linux/export.h>27#include <linux/i2c.h>28#include <linux/init.h>29#include <linux/iopoll.h>30#include <linux/kernel.h>31#include <linux/minmax.h>32#include <linux/module.h>33#include <linux/sched.h>34#include <linux/seq_file.h>35#include <linux/string_helpers.h>3637#include <drm/display/drm_dp_helper.h>38#include <drm/display/drm_dp_mst_helper.h>39#include <drm/drm_edid.h>40#include <drm/drm_fixed.h>41#include <drm/drm_print.h>42#include <drm/drm_vblank.h>43#include <drm/drm_panel.h>4445#include "drm_dp_helper_internal.h"4647DECLARE_DYNDBG_CLASSMAP(drm_debug_classes, DD_CLASS_TYPE_DISJOINT_BITS, 0,48"DRM_UT_CORE",49"DRM_UT_DRIVER",50"DRM_UT_KMS",51"DRM_UT_PRIME",52"DRM_UT_ATOMIC",53"DRM_UT_VBL",54"DRM_UT_STATE",55"DRM_UT_LEASE",56"DRM_UT_DP",57"DRM_UT_DRMRES");5859struct dp_aux_backlight {60struct backlight_device *base;61struct drm_dp_aux *aux;62struct drm_edp_backlight_info info;63bool enabled;64};6566/**67* DOC: dp helpers68*69* These functions contain some common logic and helpers at various abstraction70* levels to deal with Display Port sink devices and related things like DP aux71* channel transfers, EDID reading over DP aux channels, decoding certain DPCD72* blocks, ...73*/7475/* Helpers for DP link training */76static u8 dp_link_status(const u8 link_status[DP_LINK_STATUS_SIZE], int r)77{78return link_status[r - DP_LANE0_1_STATUS];79}8081static u8 dp_get_lane_status(const u8 link_status[DP_LINK_STATUS_SIZE],82int lane)83{84int i = DP_LANE0_1_STATUS + (lane >> 1);85int s = (lane & 1) * 4;86u8 l = dp_link_status(link_status, i);8788return (l >> s) & 0xf;89}9091bool drm_dp_channel_eq_ok(const u8 link_status[DP_LINK_STATUS_SIZE],92int lane_count)93{94u8 lane_align;95u8 lane_status;96int lane;9798lane_align = dp_link_status(link_status,99DP_LANE_ALIGN_STATUS_UPDATED);100if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)101return false;102for (lane = 0; lane < lane_count; lane++) {103lane_status = dp_get_lane_status(link_status, lane);104if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)105return false;106}107return true;108}109EXPORT_SYMBOL(drm_dp_channel_eq_ok);110111bool drm_dp_clock_recovery_ok(const u8 link_status[DP_LINK_STATUS_SIZE],112int lane_count)113{114int lane;115u8 lane_status;116117for (lane = 0; lane < lane_count; lane++) {118lane_status = dp_get_lane_status(link_status, lane);119if ((lane_status & DP_LANE_CR_DONE) == 0)120return false;121}122return true;123}124EXPORT_SYMBOL(drm_dp_clock_recovery_ok);125126bool drm_dp_post_lt_adj_req_in_progress(const u8 link_status[DP_LINK_STATUS_SIZE])127{128u8 lane_align = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);129130return lane_align & DP_POST_LT_ADJ_REQ_IN_PROGRESS;131}132EXPORT_SYMBOL(drm_dp_post_lt_adj_req_in_progress);133134u8 drm_dp_get_adjust_request_voltage(const u8 link_status[DP_LINK_STATUS_SIZE],135int lane)136{137int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);138int s = ((lane & 1) ?139DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :140DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);141u8 l = dp_link_status(link_status, i);142143return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;144}145EXPORT_SYMBOL(drm_dp_get_adjust_request_voltage);146147u8 drm_dp_get_adjust_request_pre_emphasis(const u8 link_status[DP_LINK_STATUS_SIZE],148int lane)149{150int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);151int s = ((lane & 1) ?152DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :153DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);154u8 l = dp_link_status(link_status, i);155156return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;157}158EXPORT_SYMBOL(drm_dp_get_adjust_request_pre_emphasis);159160/* DP 2.0 128b/132b */161u8 drm_dp_get_adjust_tx_ffe_preset(const u8 link_status[DP_LINK_STATUS_SIZE],162int lane)163{164int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);165int s = ((lane & 1) ?166DP_ADJUST_TX_FFE_PRESET_LANE1_SHIFT :167DP_ADJUST_TX_FFE_PRESET_LANE0_SHIFT);168u8 l = dp_link_status(link_status, i);169170return (l >> s) & 0xf;171}172EXPORT_SYMBOL(drm_dp_get_adjust_tx_ffe_preset);173174/* DP 2.0 errata for 128b/132b */175bool drm_dp_128b132b_lane_channel_eq_done(const u8 link_status[DP_LINK_STATUS_SIZE],176int lane_count)177{178u8 lane_align, lane_status;179int lane;180181lane_align = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);182if (!(lane_align & DP_INTERLANE_ALIGN_DONE))183return false;184185for (lane = 0; lane < lane_count; lane++) {186lane_status = dp_get_lane_status(link_status, lane);187if (!(lane_status & DP_LANE_CHANNEL_EQ_DONE))188return false;189}190return true;191}192EXPORT_SYMBOL(drm_dp_128b132b_lane_channel_eq_done);193194/* DP 2.0 errata for 128b/132b */195bool drm_dp_128b132b_lane_symbol_locked(const u8 link_status[DP_LINK_STATUS_SIZE],196int lane_count)197{198u8 lane_status;199int lane;200201for (lane = 0; lane < lane_count; lane++) {202lane_status = dp_get_lane_status(link_status, lane);203if (!(lane_status & DP_LANE_SYMBOL_LOCKED))204return false;205}206return true;207}208EXPORT_SYMBOL(drm_dp_128b132b_lane_symbol_locked);209210/* DP 2.0 errata for 128b/132b */211bool drm_dp_128b132b_eq_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])212{213u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);214215return status & DP_128B132B_DPRX_EQ_INTERLANE_ALIGN_DONE;216}217EXPORT_SYMBOL(drm_dp_128b132b_eq_interlane_align_done);218219/* DP 2.0 errata for 128b/132b */220bool drm_dp_128b132b_cds_interlane_align_done(const u8 link_status[DP_LINK_STATUS_SIZE])221{222u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);223224return status & DP_128B132B_DPRX_CDS_INTERLANE_ALIGN_DONE;225}226EXPORT_SYMBOL(drm_dp_128b132b_cds_interlane_align_done);227228/* DP 2.0 errata for 128b/132b */229bool drm_dp_128b132b_link_training_failed(const u8 link_status[DP_LINK_STATUS_SIZE])230{231u8 status = dp_link_status(link_status, DP_LANE_ALIGN_STATUS_UPDATED);232233return status & DP_128B132B_LT_FAILED;234}235EXPORT_SYMBOL(drm_dp_128b132b_link_training_failed);236237static int __8b10b_clock_recovery_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)238{239if (rd_interval > 4)240drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",241aux->name, rd_interval);242243if (rd_interval == 0)244return 100;245246return rd_interval * 4 * USEC_PER_MSEC;247}248249static int __8b10b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)250{251if (rd_interval > 4)252drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x (max 4)\n",253aux->name, rd_interval);254255if (rd_interval == 0)256return 400;257258return rd_interval * 4 * USEC_PER_MSEC;259}260261static int __128b132b_channel_eq_delay_us(const struct drm_dp_aux *aux, u8 rd_interval)262{263switch (rd_interval) {264default:265drm_dbg_kms(aux->drm_dev, "%s: invalid AUX interval 0x%02x\n",266aux->name, rd_interval);267fallthrough;268case DP_128B132B_TRAINING_AUX_RD_INTERVAL_400_US:269return 400;270case DP_128B132B_TRAINING_AUX_RD_INTERVAL_4_MS:271return 4000;272case DP_128B132B_TRAINING_AUX_RD_INTERVAL_8_MS:273return 8000;274case DP_128B132B_TRAINING_AUX_RD_INTERVAL_12_MS:275return 12000;276case DP_128B132B_TRAINING_AUX_RD_INTERVAL_16_MS:277return 16000;278case DP_128B132B_TRAINING_AUX_RD_INTERVAL_32_MS:279return 32000;280case DP_128B132B_TRAINING_AUX_RD_INTERVAL_64_MS:281return 64000;282}283}284285/*286* The link training delays are different for:287*288* - Clock recovery vs. channel equalization289* - DPRX vs. LTTPR290* - 128b/132b vs. 8b/10b291* - DPCD rev 1.3 vs. later292*293* Get the correct delay in us, reading DPCD if necessary.294*/295static int __read_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],296enum drm_dp_phy dp_phy, bool uhbr, bool cr)297{298int (*parse)(const struct drm_dp_aux *aux, u8 rd_interval);299unsigned int offset;300u8 rd_interval, mask;301302if (dp_phy == DP_PHY_DPRX) {303if (uhbr) {304if (cr)305return 100;306307offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL;308mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;309parse = __128b132b_channel_eq_delay_us;310} else {311if (cr && dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)312return 100;313314offset = DP_TRAINING_AUX_RD_INTERVAL;315mask = DP_TRAINING_AUX_RD_MASK;316if (cr)317parse = __8b10b_clock_recovery_delay_us;318else319parse = __8b10b_channel_eq_delay_us;320}321} else {322if (uhbr) {323offset = DP_128B132B_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);324mask = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;325parse = __128b132b_channel_eq_delay_us;326} else {327if (cr)328return 100;329330offset = DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy);331mask = DP_TRAINING_AUX_RD_MASK;332parse = __8b10b_channel_eq_delay_us;333}334}335336if (offset < DP_RECEIVER_CAP_SIZE) {337rd_interval = dpcd[offset];338} else {339if (drm_dp_dpcd_read_byte(aux, offset, &rd_interval) < 0) {340drm_dbg_kms(aux->drm_dev, "%s: failed rd interval read\n",341aux->name);342/* arbitrary default delay */343return 400;344}345}346347return parse(aux, rd_interval & mask);348}349350int drm_dp_read_clock_recovery_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],351enum drm_dp_phy dp_phy, bool uhbr)352{353return __read_delay(aux, dpcd, dp_phy, uhbr, true);354}355EXPORT_SYMBOL(drm_dp_read_clock_recovery_delay);356357int drm_dp_read_channel_eq_delay(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE],358enum drm_dp_phy dp_phy, bool uhbr)359{360return __read_delay(aux, dpcd, dp_phy, uhbr, false);361}362EXPORT_SYMBOL(drm_dp_read_channel_eq_delay);363364/* Per DP 2.0 Errata */365int drm_dp_128b132b_read_aux_rd_interval(struct drm_dp_aux *aux)366{367int unit;368u8 val;369370if (drm_dp_dpcd_read_byte(aux, DP_128B132B_TRAINING_AUX_RD_INTERVAL, &val) < 0) {371drm_err(aux->drm_dev, "%s: failed rd interval read\n",372aux->name);373/* default to max */374val = DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;375}376377unit = (val & DP_128B132B_TRAINING_AUX_RD_INTERVAL_1MS_UNIT) ? 1 : 2;378val &= DP_128B132B_TRAINING_AUX_RD_INTERVAL_MASK;379380return (val + 1) * unit * 1000;381}382EXPORT_SYMBOL(drm_dp_128b132b_read_aux_rd_interval);383384void drm_dp_link_train_clock_recovery_delay(const struct drm_dp_aux *aux,385const u8 dpcd[DP_RECEIVER_CAP_SIZE])386{387u8 rd_interval = dpcd[DP_TRAINING_AUX_RD_INTERVAL] &388DP_TRAINING_AUX_RD_MASK;389int delay_us;390391if (dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)392delay_us = 100;393else394delay_us = __8b10b_clock_recovery_delay_us(aux, rd_interval);395396usleep_range(delay_us, delay_us * 2);397}398EXPORT_SYMBOL(drm_dp_link_train_clock_recovery_delay);399400static void __drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,401u8 rd_interval)402{403int delay_us = __8b10b_channel_eq_delay_us(aux, rd_interval);404405usleep_range(delay_us, delay_us * 2);406}407408void drm_dp_link_train_channel_eq_delay(const struct drm_dp_aux *aux,409const u8 dpcd[DP_RECEIVER_CAP_SIZE])410{411__drm_dp_link_train_channel_eq_delay(aux,412dpcd[DP_TRAINING_AUX_RD_INTERVAL] &413DP_TRAINING_AUX_RD_MASK);414}415EXPORT_SYMBOL(drm_dp_link_train_channel_eq_delay);416417/**418* drm_dp_phy_name() - Get the name of the given DP PHY419* @dp_phy: The DP PHY identifier420*421* Given the @dp_phy, get a user friendly name of the DP PHY, either "DPRX" or422* "LTTPR <N>", or "<INVALID DP PHY>" on errors. The returned string is always423* non-NULL and valid.424*425* Returns: Name of the DP PHY.426*/427const char *drm_dp_phy_name(enum drm_dp_phy dp_phy)428{429static const char * const phy_names[] = {430[DP_PHY_DPRX] = "DPRX",431[DP_PHY_LTTPR1] = "LTTPR 1",432[DP_PHY_LTTPR2] = "LTTPR 2",433[DP_PHY_LTTPR3] = "LTTPR 3",434[DP_PHY_LTTPR4] = "LTTPR 4",435[DP_PHY_LTTPR5] = "LTTPR 5",436[DP_PHY_LTTPR6] = "LTTPR 6",437[DP_PHY_LTTPR7] = "LTTPR 7",438[DP_PHY_LTTPR8] = "LTTPR 8",439};440441if (dp_phy < 0 || dp_phy >= ARRAY_SIZE(phy_names) ||442WARN_ON(!phy_names[dp_phy]))443return "<INVALID DP PHY>";444445return phy_names[dp_phy];446}447EXPORT_SYMBOL(drm_dp_phy_name);448449void drm_dp_lttpr_link_train_clock_recovery_delay(void)450{451usleep_range(100, 200);452}453EXPORT_SYMBOL(drm_dp_lttpr_link_train_clock_recovery_delay);454455static u8 dp_lttpr_phy_cap(const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE], int r)456{457return phy_cap[r - DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1];458}459460void drm_dp_lttpr_link_train_channel_eq_delay(const struct drm_dp_aux *aux,461const u8 phy_cap[DP_LTTPR_PHY_CAP_SIZE])462{463u8 interval = dp_lttpr_phy_cap(phy_cap,464DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER1) &465DP_TRAINING_AUX_RD_MASK;466467__drm_dp_link_train_channel_eq_delay(aux, interval);468}469EXPORT_SYMBOL(drm_dp_lttpr_link_train_channel_eq_delay);470471/**472* drm_dp_lttpr_wake_timeout_setup() - Grant extended time for sink to wake up473* @aux: The DP AUX channel to use474* @transparent_mode: This is true if lttpr is in transparent mode475*476* This function checks if the sink needs any extended wake time, if it does477* it grants this request. Post this setup the source device can keep trying478* the Aux transaction till the granted wake timeout.479* If this function is not called all Aux transactions are expected to take480* a default of 1ms before they throw an error.481*/482void drm_dp_lttpr_wake_timeout_setup(struct drm_dp_aux *aux, bool transparent_mode)483{484u8 val = 1;485int ret;486487if (transparent_mode) {488static const u8 timeout_mapping[] = {489[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_1_MS] = 1,490[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_20_MS] = 20,491[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_40_MS] = 40,492[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_60_MS] = 60,493[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_80_MS] = 80,494[DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_100_MS] = 100,495};496497ret = drm_dp_dpcd_readb(aux, DP_EXTENDED_DPRX_SLEEP_WAKE_TIMEOUT_REQUEST, &val);498if (ret != 1) {499drm_dbg_kms(aux->drm_dev,500"Failed to read Extended sleep wake timeout request\n");501return;502}503504val = (val < sizeof(timeout_mapping) && timeout_mapping[val]) ?505timeout_mapping[val] : 1;506507if (val > 1)508drm_dp_dpcd_writeb(aux,509DP_EXTENDED_DPRX_SLEEP_WAKE_TIMEOUT_GRANT,510DP_DPRX_SLEEP_WAKE_TIMEOUT_PERIOD_GRANTED);511} else {512ret = drm_dp_dpcd_readb(aux, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT, &val);513if (ret != 1) {514drm_dbg_kms(aux->drm_dev,515"Failed to read Extended sleep wake timeout request\n");516return;517}518519val = (val & DP_EXTENDED_WAKE_TIMEOUT_REQUEST_MASK) ?520(val & DP_EXTENDED_WAKE_TIMEOUT_REQUEST_MASK) * 10 : 1;521522if (val > 1)523drm_dp_dpcd_writeb(aux, DP_PHY_REPEATER_EXTENDED_WAIT_TIMEOUT,524DP_EXTENDED_WAKE_TIMEOUT_GRANT);525}526}527EXPORT_SYMBOL(drm_dp_lttpr_wake_timeout_setup);528529u8 drm_dp_link_rate_to_bw_code(int link_rate)530{531switch (link_rate) {532case 1000000:533return DP_LINK_BW_10;534case 1350000:535return DP_LINK_BW_13_5;536case 2000000:537return DP_LINK_BW_20;538default:539/* Spec says link_bw = link_rate / 0.27Gbps */540return link_rate / 27000;541}542}543EXPORT_SYMBOL(drm_dp_link_rate_to_bw_code);544545int drm_dp_bw_code_to_link_rate(u8 link_bw)546{547switch (link_bw) {548case DP_LINK_BW_10:549return 1000000;550case DP_LINK_BW_13_5:551return 1350000;552case DP_LINK_BW_20:553return 2000000;554default:555/* Spec says link_rate = link_bw * 0.27Gbps */556return link_bw * 27000;557}558}559EXPORT_SYMBOL(drm_dp_bw_code_to_link_rate);560561#define AUX_RETRY_INTERVAL 500 /* us */562563static inline void564drm_dp_dump_access(const struct drm_dp_aux *aux,565u8 request, uint offset, void *buffer, int ret)566{567const char *arrow = request == DP_AUX_NATIVE_READ ? "->" : "<-";568569if (ret > 0)570drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d) %*ph\n",571aux->name, offset, arrow, ret, min(ret, 20), buffer);572else573drm_dbg_dp(aux->drm_dev, "%s: 0x%05x AUX %s (ret=%3d)\n",574aux->name, offset, arrow, ret);575}576577/**578* DOC: dp helpers579*580* The DisplayPort AUX channel is an abstraction to allow generic, driver-581* independent access to AUX functionality. Drivers can take advantage of582* this by filling in the fields of the drm_dp_aux structure.583*584* Transactions are described using a hardware-independent drm_dp_aux_msg585* structure, which is passed into a driver's .transfer() implementation.586* Both native and I2C-over-AUX transactions are supported.587*/588589static int drm_dp_dpcd_access(struct drm_dp_aux *aux, u8 request,590unsigned int offset, void *buffer, size_t size)591{592struct drm_dp_aux_msg msg;593unsigned int retry, native_reply;594int err = 0, ret = 0;595596memset(&msg, 0, sizeof(msg));597msg.address = offset;598msg.request = request;599msg.buffer = buffer;600msg.size = size;601602mutex_lock(&aux->hw_mutex);603604/*605* If the device attached to the aux bus is powered down then there's606* no reason to attempt a transfer. Error out immediately.607*/608if (aux->powered_down) {609ret = -EBUSY;610goto unlock;611}612613/*614* The specification doesn't give any recommendation on how often to615* retry native transactions. We used to retry 7 times like for616* aux i2c transactions but real world devices this wasn't617* sufficient, bump to 32 which makes Dell 4k monitors happier.618*/619for (retry = 0; retry < 32; retry++) {620if (ret != 0 && ret != -ETIMEDOUT) {621usleep_range(AUX_RETRY_INTERVAL,622AUX_RETRY_INTERVAL + 100);623}624625ret = aux->transfer(aux, &msg);626if (ret >= 0) {627native_reply = msg.reply & DP_AUX_NATIVE_REPLY_MASK;628if (native_reply == DP_AUX_NATIVE_REPLY_ACK) {629if (ret == size)630goto unlock;631632ret = -EPROTO;633} else634ret = -EIO;635}636637/*638* We want the error we return to be the error we received on639* the first transaction, since we may get a different error the640* next time we retry641*/642if (!err)643err = ret;644}645646drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up. First error: %d\n",647aux->name, err);648ret = err;649650unlock:651mutex_unlock(&aux->hw_mutex);652return ret;653}654655/**656* drm_dp_dpcd_probe() - probe a given DPCD address with a 1-byte read access657* @aux: DisplayPort AUX channel (SST)658* @offset: address of the register to probe659*660* Probe the provided DPCD address by reading 1 byte from it. The function can661* be used to trigger some side-effect the read access has, like waking up the662* sink, without the need for the read-out value.663*664* Returns 0 if the read access suceeded, or a negative error code on failure.665*/666int drm_dp_dpcd_probe(struct drm_dp_aux *aux, unsigned int offset)667{668u8 buffer;669int ret;670671ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, 1);672WARN_ON(ret == 0);673674drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, &buffer, ret);675676return ret < 0 ? ret : 0;677}678EXPORT_SYMBOL(drm_dp_dpcd_probe);679680/**681* drm_dp_dpcd_set_powered() - Set whether the DP device is powered682* @aux: DisplayPort AUX channel; for convenience it's OK to pass NULL here683* and the function will be a no-op.684* @powered: true if powered; false if not685*686* If the endpoint device on the DP AUX bus is known to be powered down687* then this function can be called to make future transfers fail immediately688* instead of needing to time out.689*690* If this function is never called then a device defaults to being powered.691*/692void drm_dp_dpcd_set_powered(struct drm_dp_aux *aux, bool powered)693{694if (!aux)695return;696697mutex_lock(&aux->hw_mutex);698aux->powered_down = !powered;699mutex_unlock(&aux->hw_mutex);700}701EXPORT_SYMBOL(drm_dp_dpcd_set_powered);702703/**704* drm_dp_dpcd_set_probe() - Set whether a probing before DPCD access is done705* @aux: DisplayPort AUX channel706* @enable: Enable the probing if required707*/708void drm_dp_dpcd_set_probe(struct drm_dp_aux *aux, bool enable)709{710WRITE_ONCE(aux->dpcd_probe_disabled, !enable);711}712EXPORT_SYMBOL(drm_dp_dpcd_set_probe);713714static bool dpcd_access_needs_probe(struct drm_dp_aux *aux)715{716/*717* HP ZR24w corrupts the first DPCD access after entering power save718* mode. Eg. on a read, the entire buffer will be filled with the same719* byte. Do a throw away read to avoid corrupting anything we care720* about. Afterwards things will work correctly until the monitor721* gets woken up and subsequently re-enters power save mode.722*723* The user pressing any button on the monitor is enough to wake it724* up, so there is no particularly good place to do the workaround.725* We just have to do it before any DPCD access and hope that the726* monitor doesn't power down exactly after the throw away read.727*/728return !aux->is_remote && !READ_ONCE(aux->dpcd_probe_disabled);729}730731/**732* drm_dp_dpcd_read() - read a series of bytes from the DPCD733* @aux: DisplayPort AUX channel (SST or MST)734* @offset: address of the (first) register to read735* @buffer: buffer to store the register values736* @size: number of bytes in @buffer737*738* Returns the number of bytes transferred on success, or a negative error739* code on failure. -EIO is returned if the request was NAKed by the sink or740* if the retry count was exceeded. If not all bytes were transferred, this741* function returns -EPROTO. Errors from the underlying AUX channel transfer742* function, with the exception of -EBUSY (which causes the transaction to743* be retried), are propagated to the caller.744*745* In most of the cases you want to use drm_dp_dpcd_read_data() instead.746*/747ssize_t drm_dp_dpcd_read(struct drm_dp_aux *aux, unsigned int offset,748void *buffer, size_t size)749{750int ret;751752if (dpcd_access_needs_probe(aux)) {753ret = drm_dp_dpcd_probe(aux, DP_TRAINING_PATTERN_SET);754if (ret < 0)755return ret;756}757758if (aux->is_remote)759ret = drm_dp_mst_dpcd_read(aux, offset, buffer, size);760else761ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_READ, offset,762buffer, size);763764drm_dp_dump_access(aux, DP_AUX_NATIVE_READ, offset, buffer, ret);765return ret;766}767EXPORT_SYMBOL(drm_dp_dpcd_read);768769/**770* drm_dp_dpcd_write() - write a series of bytes to the DPCD771* @aux: DisplayPort AUX channel (SST or MST)772* @offset: address of the (first) register to write773* @buffer: buffer containing the values to write774* @size: number of bytes in @buffer775*776* Returns the number of bytes transferred on success, or a negative error777* code on failure. -EIO is returned if the request was NAKed by the sink or778* if the retry count was exceeded. If not all bytes were transferred, this779* function returns -EPROTO. Errors from the underlying AUX channel transfer780* function, with the exception of -EBUSY (which causes the transaction to781* be retried), are propagated to the caller.782*783* In most of the cases you want to use drm_dp_dpcd_write_data() instead.784*/785ssize_t drm_dp_dpcd_write(struct drm_dp_aux *aux, unsigned int offset,786void *buffer, size_t size)787{788int ret;789790if (aux->is_remote)791ret = drm_dp_mst_dpcd_write(aux, offset, buffer, size);792else793ret = drm_dp_dpcd_access(aux, DP_AUX_NATIVE_WRITE, offset,794buffer, size);795796drm_dp_dump_access(aux, DP_AUX_NATIVE_WRITE, offset, buffer, ret);797return ret;798}799EXPORT_SYMBOL(drm_dp_dpcd_write);800801/**802* drm_dp_dpcd_read_link_status() - read DPCD link status (bytes 0x202-0x207)803* @aux: DisplayPort AUX channel804* @status: buffer to store the link status in (must be at least 6 bytes)805*806* Returns a negative error code on failure or 0 on success.807*/808int drm_dp_dpcd_read_link_status(struct drm_dp_aux *aux,809u8 status[DP_LINK_STATUS_SIZE])810{811return drm_dp_dpcd_read_data(aux, DP_LANE0_1_STATUS, status,812DP_LINK_STATUS_SIZE);813}814EXPORT_SYMBOL(drm_dp_dpcd_read_link_status);815816/**817* drm_dp_dpcd_read_phy_link_status - get the link status information for a DP PHY818* @aux: DisplayPort AUX channel819* @dp_phy: the DP PHY to get the link status for820* @link_status: buffer to return the status in821*822* Fetch the AUX DPCD registers for the DPRX or an LTTPR PHY link status. The823* layout of the returned @link_status matches the DPCD register layout of the824* DPRX PHY link status.825*826* Returns 0 if the information was read successfully or a negative error code827* on failure.828*/829int drm_dp_dpcd_read_phy_link_status(struct drm_dp_aux *aux,830enum drm_dp_phy dp_phy,831u8 link_status[DP_LINK_STATUS_SIZE])832{833int ret;834835if (dp_phy == DP_PHY_DPRX)836return drm_dp_dpcd_read_data(aux,837DP_LANE0_1_STATUS,838link_status,839DP_LINK_STATUS_SIZE);840841ret = drm_dp_dpcd_read_data(aux,842DP_LANE0_1_STATUS_PHY_REPEATER(dp_phy),843link_status,844DP_LINK_STATUS_SIZE - 1);845846if (ret < 0)847return ret;848849/* Convert the LTTPR to the sink PHY link status layout */850memmove(&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS + 1],851&link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS],852DP_LINK_STATUS_SIZE - (DP_SINK_STATUS - DP_LANE0_1_STATUS) - 1);853link_status[DP_SINK_STATUS - DP_LANE0_1_STATUS] = 0;854855return 0;856}857EXPORT_SYMBOL(drm_dp_dpcd_read_phy_link_status);858859/**860* drm_dp_link_power_up() - power up a DisplayPort link861* @aux: DisplayPort AUX channel862* @revision: DPCD revision supported on the link863*864* Returns 0 on success or a negative error code on failure.865*/866int drm_dp_link_power_up(struct drm_dp_aux *aux, unsigned char revision)867{868u8 value;869int err;870871/* DP_SET_POWER register is only available on DPCD v1.1 and later */872if (revision < DP_DPCD_REV_11)873return 0;874875err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);876if (err < 0)877return err;878879value &= ~DP_SET_POWER_MASK;880value |= DP_SET_POWER_D0;881882err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);883if (err < 0)884return err;885886/*887* According to the DP 1.1 specification, a "Sink Device must exit the888* power saving state within 1 ms" (Section 2.5.3.1, Table 5-52, "Sink889* Control Field" (register 0x600).890*/891usleep_range(1000, 2000);892893return 0;894}895EXPORT_SYMBOL(drm_dp_link_power_up);896897/**898* drm_dp_link_power_down() - power down a DisplayPort link899* @aux: DisplayPort AUX channel900* @revision: DPCD revision supported on the link901*902* Returns 0 on success or a negative error code on failure.903*/904int drm_dp_link_power_down(struct drm_dp_aux *aux, unsigned char revision)905{906u8 value;907int err;908909/* DP_SET_POWER register is only available on DPCD v1.1 and later */910if (revision < DP_DPCD_REV_11)911return 0;912913err = drm_dp_dpcd_readb(aux, DP_SET_POWER, &value);914if (err < 0)915return err;916917value &= ~DP_SET_POWER_MASK;918value |= DP_SET_POWER_D3;919920err = drm_dp_dpcd_writeb(aux, DP_SET_POWER, value);921if (err < 0)922return err;923924return 0;925}926EXPORT_SYMBOL(drm_dp_link_power_down);927928static int read_payload_update_status(struct drm_dp_aux *aux)929{930int ret;931u8 status;932933ret = drm_dp_dpcd_read_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);934if (ret < 0)935return ret;936937return status;938}939940/**941* drm_dp_dpcd_write_payload() - Write Virtual Channel information to payload table942* @aux: DisplayPort AUX channel943* @vcpid: Virtual Channel Payload ID944* @start_time_slot: Starting time slot945* @time_slot_count: Time slot count946*947* Write the Virtual Channel payload allocation table, checking the payload948* update status and retrying as necessary.949*950* Returns:951* 0 on success, negative error otherwise952*/953int drm_dp_dpcd_write_payload(struct drm_dp_aux *aux,954int vcpid, u8 start_time_slot, u8 time_slot_count)955{956u8 payload_alloc[3], status;957int ret;958int retries = 0;959960drm_dp_dpcd_write_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS,961DP_PAYLOAD_TABLE_UPDATED);962963payload_alloc[0] = vcpid;964payload_alloc[1] = start_time_slot;965payload_alloc[2] = time_slot_count;966967ret = drm_dp_dpcd_write_data(aux, DP_PAYLOAD_ALLOCATE_SET, payload_alloc, 3);968if (ret < 0) {969drm_dbg_kms(aux->drm_dev, "failed to write payload allocation %d\n", ret);970goto fail;971}972973retry:974ret = drm_dp_dpcd_read_byte(aux, DP_PAYLOAD_TABLE_UPDATE_STATUS, &status);975if (ret < 0) {976drm_dbg_kms(aux->drm_dev, "failed to read payload table status %d\n", ret);977goto fail;978}979980if (!(status & DP_PAYLOAD_TABLE_UPDATED)) {981retries++;982if (retries < 20) {983usleep_range(10000, 20000);984goto retry;985}986drm_dbg_kms(aux->drm_dev, "status not set after read payload table status %d\n",987status);988ret = -EINVAL;989goto fail;990}991ret = 0;992fail:993return ret;994}995EXPORT_SYMBOL(drm_dp_dpcd_write_payload);996997/**998* drm_dp_dpcd_clear_payload() - Clear the entire VC Payload ID table999* @aux: DisplayPort AUX channel1000*1001* Clear the entire VC Payload ID table.1002*1003* Returns: 0 on success, negative error code on errors.1004*/1005int drm_dp_dpcd_clear_payload(struct drm_dp_aux *aux)1006{1007return drm_dp_dpcd_write_payload(aux, 0, 0, 0x3f);1008}1009EXPORT_SYMBOL(drm_dp_dpcd_clear_payload);10101011/**1012* drm_dp_dpcd_poll_act_handled() - Poll for ACT handled status1013* @aux: DisplayPort AUX channel1014* @timeout_ms: Timeout in ms1015*1016* Try waiting for the sink to finish updating its payload table by polling for1017* the ACT handled bit of DP_PAYLOAD_TABLE_UPDATE_STATUS for up to @timeout_ms1018* milliseconds, defaulting to 3000 ms if 0.1019*1020* Returns:1021* 0 if the ACT was handled in time, negative error code on failure.1022*/1023int drm_dp_dpcd_poll_act_handled(struct drm_dp_aux *aux, int timeout_ms)1024{1025int ret, status;10261027/* default to 3 seconds, this is arbitrary */1028timeout_ms = timeout_ms ?: 3000;10291030ret = readx_poll_timeout(read_payload_update_status, aux, status,1031status & DP_PAYLOAD_ACT_HANDLED || status < 0,1032200, timeout_ms * USEC_PER_MSEC);1033if (ret < 0 && status >= 0) {1034drm_err(aux->drm_dev, "Failed to get ACT after %d ms, last status: %02x\n",1035timeout_ms, status);1036return -EINVAL;1037} else if (status < 0) {1038/*1039* Failure here isn't unexpected - the hub may have1040* just been unplugged1041*/1042drm_dbg_kms(aux->drm_dev, "Failed to read payload table status: %d\n", status);1043return status;1044}10451046return 0;1047}1048EXPORT_SYMBOL(drm_dp_dpcd_poll_act_handled);10491050static bool is_edid_digital_input_dp(const struct drm_edid *drm_edid)1051{1052/* FIXME: get rid of drm_edid_raw() */1053const struct edid *edid = drm_edid_raw(drm_edid);10541055return edid && edid->revision >= 4 &&1056edid->input & DRM_EDID_INPUT_DIGITAL &&1057(edid->input & DRM_EDID_DIGITAL_TYPE_MASK) == DRM_EDID_DIGITAL_TYPE_DP;1058}10591060/**1061* drm_dp_downstream_is_type() - is the downstream facing port of certain type?1062* @dpcd: DisplayPort configuration data1063* @port_cap: port capabilities1064* @type: port type to be checked. Can be:1065* %DP_DS_PORT_TYPE_DP, %DP_DS_PORT_TYPE_VGA, %DP_DS_PORT_TYPE_DVI,1066* %DP_DS_PORT_TYPE_HDMI, %DP_DS_PORT_TYPE_NON_EDID,1067* %DP_DS_PORT_TYPE_DP_DUALMODE or %DP_DS_PORT_TYPE_WIRELESS.1068*1069* Caveat: Only works with DPCD 1.1+ port caps.1070*1071* Returns: whether the downstream facing port matches the type.1072*/1073bool drm_dp_downstream_is_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1074const u8 port_cap[4], u8 type)1075{1076return drm_dp_is_branch(dpcd) &&1077dpcd[DP_DPCD_REV] >= 0x11 &&1078(port_cap[0] & DP_DS_PORT_TYPE_MASK) == type;1079}1080EXPORT_SYMBOL(drm_dp_downstream_is_type);10811082/**1083* drm_dp_downstream_is_tmds() - is the downstream facing port TMDS?1084* @dpcd: DisplayPort configuration data1085* @port_cap: port capabilities1086* @drm_edid: EDID1087*1088* Returns: whether the downstream facing port is TMDS (HDMI/DVI).1089*/1090bool drm_dp_downstream_is_tmds(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1091const u8 port_cap[4],1092const struct drm_edid *drm_edid)1093{1094if (dpcd[DP_DPCD_REV] < 0x11) {1095switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {1096case DP_DWN_STRM_PORT_TYPE_TMDS:1097return true;1098default:1099return false;1100}1101}11021103switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1104case DP_DS_PORT_TYPE_DP_DUALMODE:1105if (is_edid_digital_input_dp(drm_edid))1106return false;1107fallthrough;1108case DP_DS_PORT_TYPE_DVI:1109case DP_DS_PORT_TYPE_HDMI:1110return true;1111default:1112return false;1113}1114}1115EXPORT_SYMBOL(drm_dp_downstream_is_tmds);11161117/**1118* drm_dp_send_real_edid_checksum() - send back real edid checksum value1119* @aux: DisplayPort AUX channel1120* @real_edid_checksum: real edid checksum for the last block1121*1122* Returns:1123* True on success1124*/1125bool drm_dp_send_real_edid_checksum(struct drm_dp_aux *aux,1126u8 real_edid_checksum)1127{1128u8 link_edid_read = 0, auto_test_req = 0, test_resp = 0;11291130if (drm_dp_dpcd_read_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,1131&auto_test_req) < 0) {1132drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",1133aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);1134return false;1135}1136auto_test_req &= DP_AUTOMATED_TEST_REQUEST;11371138if (drm_dp_dpcd_read_byte(aux, DP_TEST_REQUEST, &link_edid_read) < 0) {1139drm_err(aux->drm_dev, "%s: DPCD failed read at register 0x%x\n",1140aux->name, DP_TEST_REQUEST);1141return false;1142}1143link_edid_read &= DP_TEST_LINK_EDID_READ;11441145if (!auto_test_req || !link_edid_read) {1146drm_dbg_kms(aux->drm_dev, "%s: Source DUT does not support TEST_EDID_READ\n",1147aux->name);1148return false;1149}11501151if (drm_dp_dpcd_write_byte(aux, DP_DEVICE_SERVICE_IRQ_VECTOR,1152auto_test_req) < 0) {1153drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",1154aux->name, DP_DEVICE_SERVICE_IRQ_VECTOR);1155return false;1156}11571158/* send back checksum for the last edid extension block data */1159if (drm_dp_dpcd_write_byte(aux, DP_TEST_EDID_CHECKSUM,1160real_edid_checksum) < 0) {1161drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",1162aux->name, DP_TEST_EDID_CHECKSUM);1163return false;1164}11651166test_resp |= DP_TEST_EDID_CHECKSUM_WRITE;1167if (drm_dp_dpcd_write_byte(aux, DP_TEST_RESPONSE, test_resp) < 0) {1168drm_err(aux->drm_dev, "%s: DPCD failed write at register 0x%x\n",1169aux->name, DP_TEST_RESPONSE);1170return false;1171}11721173return true;1174}1175EXPORT_SYMBOL(drm_dp_send_real_edid_checksum);11761177static u8 drm_dp_downstream_port_count(const u8 dpcd[DP_RECEIVER_CAP_SIZE])1178{1179u8 port_count = dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_PORT_COUNT_MASK;11801181if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE && port_count > 4)1182port_count = 4;11831184return port_count;1185}11861187static int drm_dp_read_extended_dpcd_caps(struct drm_dp_aux *aux,1188u8 dpcd[DP_RECEIVER_CAP_SIZE])1189{1190u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];1191int ret;11921193/*1194* Prior to DP1.3 the bit represented by1195* DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.1196* If it is set DP_DPCD_REV at 0000h could be at a value less than1197* the true capability of the panel. The only way to check is to1198* then compare 0000h and 2200h.1199*/1200if (!(dpcd[DP_TRAINING_AUX_RD_INTERVAL] &1201DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))1202return 0;12031204ret = drm_dp_dpcd_read_data(aux, DP_DP13_DPCD_REV, &dpcd_ext,1205sizeof(dpcd_ext));1206if (ret < 0)1207return ret;12081209if (dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {1210drm_dbg_kms(aux->drm_dev,1211"%s: Extended DPCD rev less than base DPCD rev (%d > %d)\n",1212aux->name, dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);1213return 0;1214}12151216if (!memcmp(dpcd, dpcd_ext, sizeof(dpcd_ext)))1217return 0;12181219drm_dbg_kms(aux->drm_dev, "%s: Base DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);12201221memcpy(dpcd, dpcd_ext, sizeof(dpcd_ext));12221223return 0;1224}12251226/**1227* drm_dp_read_dpcd_caps() - read DPCD caps and extended DPCD caps if1228* available1229* @aux: DisplayPort AUX channel1230* @dpcd: Buffer to store the resulting DPCD in1231*1232* Attempts to read the base DPCD caps for @aux. Additionally, this function1233* checks for and reads the extended DPRX caps (%DP_DP13_DPCD_REV) if1234* present.1235*1236* Returns: %0 if the DPCD was read successfully, negative error code1237* otherwise.1238*/1239int drm_dp_read_dpcd_caps(struct drm_dp_aux *aux,1240u8 dpcd[DP_RECEIVER_CAP_SIZE])1241{1242int ret;12431244ret = drm_dp_dpcd_read_data(aux, DP_DPCD_REV, dpcd, DP_RECEIVER_CAP_SIZE);1245if (ret < 0)1246return ret;1247if (dpcd[DP_DPCD_REV] == 0)1248return -EIO;12491250ret = drm_dp_read_extended_dpcd_caps(aux, dpcd);1251if (ret < 0)1252return ret;12531254drm_dbg_kms(aux->drm_dev, "%s: DPCD: %*ph\n", aux->name, DP_RECEIVER_CAP_SIZE, dpcd);12551256return ret;1257}1258EXPORT_SYMBOL(drm_dp_read_dpcd_caps);12591260/**1261* drm_dp_read_downstream_info() - read DPCD downstream port info if available1262* @aux: DisplayPort AUX channel1263* @dpcd: A cached copy of the port's DPCD1264* @downstream_ports: buffer to store the downstream port info in1265*1266* See also:1267* drm_dp_downstream_max_clock()1268* drm_dp_downstream_max_bpc()1269*1270* Returns: 0 if either the downstream port info was read successfully or1271* there was no downstream info to read, or a negative error code otherwise.1272*/1273int drm_dp_read_downstream_info(struct drm_dp_aux *aux,1274const u8 dpcd[DP_RECEIVER_CAP_SIZE],1275u8 downstream_ports[DP_MAX_DOWNSTREAM_PORTS])1276{1277int ret;1278u8 len;12791280memset(downstream_ports, 0, DP_MAX_DOWNSTREAM_PORTS);12811282/* No downstream info to read */1283if (!drm_dp_is_branch(dpcd) || dpcd[DP_DPCD_REV] == DP_DPCD_REV_10)1284return 0;12851286/* Some branches advertise having 0 downstream ports, despite also advertising they have a1287* downstream port present. The DP spec isn't clear on if this is allowed or not, but since1288* some branches do it we need to handle it regardless.1289*/1290len = drm_dp_downstream_port_count(dpcd);1291if (!len)1292return 0;12931294if (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE)1295len *= 4;12961297ret = drm_dp_dpcd_read_data(aux, DP_DOWNSTREAM_PORT_0, downstream_ports, len);1298if (ret < 0)1299return ret;13001301drm_dbg_kms(aux->drm_dev, "%s: DPCD DFP: %*ph\n", aux->name, len, downstream_ports);13021303return 0;1304}1305EXPORT_SYMBOL(drm_dp_read_downstream_info);13061307/**1308* drm_dp_downstream_max_dotclock() - extract downstream facing port max dot clock1309* @dpcd: DisplayPort configuration data1310* @port_cap: port capabilities1311*1312* Returns: Downstream facing port max dot clock in kHz on success,1313* or 0 if max clock not defined1314*/1315int drm_dp_downstream_max_dotclock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1316const u8 port_cap[4])1317{1318if (!drm_dp_is_branch(dpcd))1319return 0;13201321if (dpcd[DP_DPCD_REV] < 0x11)1322return 0;13231324switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1325case DP_DS_PORT_TYPE_VGA:1326if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1327return 0;1328return port_cap[1] * 8000;1329default:1330return 0;1331}1332}1333EXPORT_SYMBOL(drm_dp_downstream_max_dotclock);13341335/**1336* drm_dp_downstream_max_tmds_clock() - extract downstream facing port max TMDS clock1337* @dpcd: DisplayPort configuration data1338* @port_cap: port capabilities1339* @drm_edid: EDID1340*1341* Returns: HDMI/DVI downstream facing port max TMDS clock in kHz on success,1342* or 0 if max TMDS clock not defined1343*/1344int drm_dp_downstream_max_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1345const u8 port_cap[4],1346const struct drm_edid *drm_edid)1347{1348if (!drm_dp_is_branch(dpcd))1349return 0;13501351if (dpcd[DP_DPCD_REV] < 0x11) {1352switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {1353case DP_DWN_STRM_PORT_TYPE_TMDS:1354return 165000;1355default:1356return 0;1357}1358}13591360switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1361case DP_DS_PORT_TYPE_DP_DUALMODE:1362if (is_edid_digital_input_dp(drm_edid))1363return 0;1364/*1365* It's left up to the driver to check the1366* DP dual mode adapter's max TMDS clock.1367*1368* Unfortunately it looks like branch devices1369* may not fordward that the DP dual mode i2c1370* access so we just usually get i2c nak :(1371*/1372fallthrough;1373case DP_DS_PORT_TYPE_HDMI:1374/*1375* We should perhaps assume 165 MHz when detailed cap1376* info is not available. But looks like many typical1377* branch devices fall into that category and so we'd1378* probably end up with users complaining that they can't1379* get high resolution modes with their favorite dongle.1380*1381* So let's limit to 300 MHz instead since DPCD 1.41382* HDMI 2.0 DFPs are required to have the detailed cap1383* info. So it's more likely we're dealing with a HDMI 1.41384* compatible* device here.1385*/1386if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1387return 300000;1388return port_cap[1] * 2500;1389case DP_DS_PORT_TYPE_DVI:1390if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1391return 165000;1392/* FIXME what to do about DVI dual link? */1393return port_cap[1] * 2500;1394default:1395return 0;1396}1397}1398EXPORT_SYMBOL(drm_dp_downstream_max_tmds_clock);13991400/**1401* drm_dp_downstream_min_tmds_clock() - extract downstream facing port min TMDS clock1402* @dpcd: DisplayPort configuration data1403* @port_cap: port capabilities1404* @drm_edid: EDID1405*1406* Returns: HDMI/DVI downstream facing port min TMDS clock in kHz on success,1407* or 0 if max TMDS clock not defined1408*/1409int drm_dp_downstream_min_tmds_clock(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1410const u8 port_cap[4],1411const struct drm_edid *drm_edid)1412{1413if (!drm_dp_is_branch(dpcd))1414return 0;14151416if (dpcd[DP_DPCD_REV] < 0x11) {1417switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {1418case DP_DWN_STRM_PORT_TYPE_TMDS:1419return 25000;1420default:1421return 0;1422}1423}14241425switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1426case DP_DS_PORT_TYPE_DP_DUALMODE:1427if (is_edid_digital_input_dp(drm_edid))1428return 0;1429fallthrough;1430case DP_DS_PORT_TYPE_DVI:1431case DP_DS_PORT_TYPE_HDMI:1432/*1433* Unclear whether the protocol converter could1434* utilize pixel replication. Assume it won't.1435*/1436return 25000;1437default:1438return 0;1439}1440}1441EXPORT_SYMBOL(drm_dp_downstream_min_tmds_clock);14421443/**1444* drm_dp_downstream_max_bpc() - extract downstream facing port max1445* bits per component1446* @dpcd: DisplayPort configuration data1447* @port_cap: downstream facing port capabilities1448* @drm_edid: EDID1449*1450* Returns: Max bpc on success or 0 if max bpc not defined1451*/1452int drm_dp_downstream_max_bpc(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1453const u8 port_cap[4],1454const struct drm_edid *drm_edid)1455{1456if (!drm_dp_is_branch(dpcd))1457return 0;14581459if (dpcd[DP_DPCD_REV] < 0x11) {1460switch (dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_TYPE_MASK) {1461case DP_DWN_STRM_PORT_TYPE_DP:1462return 0;1463default:1464return 8;1465}1466}14671468switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1469case DP_DS_PORT_TYPE_DP:1470return 0;1471case DP_DS_PORT_TYPE_DP_DUALMODE:1472if (is_edid_digital_input_dp(drm_edid))1473return 0;1474fallthrough;1475case DP_DS_PORT_TYPE_HDMI:1476case DP_DS_PORT_TYPE_DVI:1477case DP_DS_PORT_TYPE_VGA:1478if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1479return 8;14801481switch (port_cap[2] & DP_DS_MAX_BPC_MASK) {1482case DP_DS_8BPC:1483return 8;1484case DP_DS_10BPC:1485return 10;1486case DP_DS_12BPC:1487return 12;1488case DP_DS_16BPC:1489return 16;1490default:1491return 8;1492}1493break;1494default:1495return 8;1496}1497}1498EXPORT_SYMBOL(drm_dp_downstream_max_bpc);14991500/**1501* drm_dp_downstream_420_passthrough() - determine downstream facing port1502* YCbCr 4:2:0 pass-through capability1503* @dpcd: DisplayPort configuration data1504* @port_cap: downstream facing port capabilities1505*1506* Returns: whether the downstream facing port can pass through YCbCr 4:2:01507*/1508bool drm_dp_downstream_420_passthrough(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1509const u8 port_cap[4])1510{1511if (!drm_dp_is_branch(dpcd))1512return false;15131514if (dpcd[DP_DPCD_REV] < 0x13)1515return false;15161517switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1518case DP_DS_PORT_TYPE_DP:1519return true;1520case DP_DS_PORT_TYPE_HDMI:1521if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1522return false;15231524return port_cap[3] & DP_DS_HDMI_YCBCR420_PASS_THROUGH;1525default:1526return false;1527}1528}1529EXPORT_SYMBOL(drm_dp_downstream_420_passthrough);15301531/**1532* drm_dp_downstream_444_to_420_conversion() - determine downstream facing port1533* YCbCr 4:4:4->4:2:0 conversion capability1534* @dpcd: DisplayPort configuration data1535* @port_cap: downstream facing port capabilities1536*1537* Returns: whether the downstream facing port can convert YCbCr 4:4:4 to 4:2:01538*/1539bool drm_dp_downstream_444_to_420_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1540const u8 port_cap[4])1541{1542if (!drm_dp_is_branch(dpcd))1543return false;15441545if (dpcd[DP_DPCD_REV] < 0x13)1546return false;15471548switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1549case DP_DS_PORT_TYPE_HDMI:1550if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1551return false;15521553return port_cap[3] & DP_DS_HDMI_YCBCR444_TO_420_CONV;1554default:1555return false;1556}1557}1558EXPORT_SYMBOL(drm_dp_downstream_444_to_420_conversion);15591560/**1561* drm_dp_downstream_rgb_to_ycbcr_conversion() - determine downstream facing port1562* RGB->YCbCr conversion capability1563* @dpcd: DisplayPort configuration data1564* @port_cap: downstream facing port capabilities1565* @color_spc: Colorspace for which conversion cap is sought1566*1567* Returns: whether the downstream facing port can convert RGB->YCbCr for a given1568* colorspace.1569*/1570bool drm_dp_downstream_rgb_to_ycbcr_conversion(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1571const u8 port_cap[4],1572u8 color_spc)1573{1574if (!drm_dp_is_branch(dpcd))1575return false;15761577if (dpcd[DP_DPCD_REV] < 0x13)1578return false;15791580switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1581case DP_DS_PORT_TYPE_HDMI:1582if ((dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DETAILED_CAP_INFO_AVAILABLE) == 0)1583return false;15841585return port_cap[3] & color_spc;1586default:1587return false;1588}1589}1590EXPORT_SYMBOL(drm_dp_downstream_rgb_to_ycbcr_conversion);15911592/**1593* drm_dp_downstream_mode() - return a mode for downstream facing port1594* @dev: DRM device1595* @dpcd: DisplayPort configuration data1596* @port_cap: port capabilities1597*1598* Provides a suitable mode for downstream facing ports without EDID.1599*1600* Returns: A new drm_display_mode on success or NULL on failure1601*/1602struct drm_display_mode *1603drm_dp_downstream_mode(struct drm_device *dev,1604const u8 dpcd[DP_RECEIVER_CAP_SIZE],1605const u8 port_cap[4])16061607{1608u8 vic;16091610if (!drm_dp_is_branch(dpcd))1611return NULL;16121613if (dpcd[DP_DPCD_REV] < 0x11)1614return NULL;16151616switch (port_cap[0] & DP_DS_PORT_TYPE_MASK) {1617case DP_DS_PORT_TYPE_NON_EDID:1618switch (port_cap[0] & DP_DS_NON_EDID_MASK) {1619case DP_DS_NON_EDID_720x480i_60:1620vic = 6;1621break;1622case DP_DS_NON_EDID_720x480i_50:1623vic = 21;1624break;1625case DP_DS_NON_EDID_1920x1080i_60:1626vic = 5;1627break;1628case DP_DS_NON_EDID_1920x1080i_50:1629vic = 20;1630break;1631case DP_DS_NON_EDID_1280x720_60:1632vic = 4;1633break;1634case DP_DS_NON_EDID_1280x720_50:1635vic = 19;1636break;1637default:1638return NULL;1639}1640return drm_display_mode_from_cea_vic(dev, vic);1641default:1642return NULL;1643}1644}1645EXPORT_SYMBOL(drm_dp_downstream_mode);16461647/**1648* drm_dp_downstream_id() - identify branch device1649* @aux: DisplayPort AUX channel1650* @id: DisplayPort branch device id1651*1652* Returns branch device id on success or NULL on failure1653*/1654int drm_dp_downstream_id(struct drm_dp_aux *aux, char id[6])1655{1656return drm_dp_dpcd_read_data(aux, DP_BRANCH_ID, id, 6);1657}1658EXPORT_SYMBOL(drm_dp_downstream_id);16591660/**1661* drm_dp_downstream_debug() - debug DP branch devices1662* @m: pointer for debugfs file1663* @dpcd: DisplayPort configuration data1664* @port_cap: port capabilities1665* @drm_edid: EDID1666* @aux: DisplayPort AUX channel1667*1668*/1669void drm_dp_downstream_debug(struct seq_file *m,1670const u8 dpcd[DP_RECEIVER_CAP_SIZE],1671const u8 port_cap[4],1672const struct drm_edid *drm_edid,1673struct drm_dp_aux *aux)1674{1675bool detailed_cap_info = dpcd[DP_DOWNSTREAMPORT_PRESENT] &1676DP_DETAILED_CAP_INFO_AVAILABLE;1677int clk;1678int bpc;1679char id[7];1680int len;1681uint8_t rev[2];1682int type = port_cap[0] & DP_DS_PORT_TYPE_MASK;1683bool branch_device = drm_dp_is_branch(dpcd);16841685seq_printf(m, "\tDP branch device present: %s\n",1686str_yes_no(branch_device));16871688if (!branch_device)1689return;16901691switch (type) {1692case DP_DS_PORT_TYPE_DP:1693seq_puts(m, "\t\tType: DisplayPort\n");1694break;1695case DP_DS_PORT_TYPE_VGA:1696seq_puts(m, "\t\tType: VGA\n");1697break;1698case DP_DS_PORT_TYPE_DVI:1699seq_puts(m, "\t\tType: DVI\n");1700break;1701case DP_DS_PORT_TYPE_HDMI:1702seq_puts(m, "\t\tType: HDMI\n");1703break;1704case DP_DS_PORT_TYPE_NON_EDID:1705seq_puts(m, "\t\tType: others without EDID support\n");1706break;1707case DP_DS_PORT_TYPE_DP_DUALMODE:1708seq_puts(m, "\t\tType: DP++\n");1709break;1710case DP_DS_PORT_TYPE_WIRELESS:1711seq_puts(m, "\t\tType: Wireless\n");1712break;1713default:1714seq_puts(m, "\t\tType: N/A\n");1715}17161717memset(id, 0, sizeof(id));1718drm_dp_downstream_id(aux, id);1719seq_printf(m, "\t\tID: %s\n", id);17201721len = drm_dp_dpcd_read_data(aux, DP_BRANCH_HW_REV, &rev[0], 1);1722if (!len)1723seq_printf(m, "\t\tHW: %d.%d\n",1724(rev[0] & 0xf0) >> 4, rev[0] & 0xf);17251726len = drm_dp_dpcd_read_data(aux, DP_BRANCH_SW_REV, rev, 2);1727if (!len)1728seq_printf(m, "\t\tSW: %d.%d\n", rev[0], rev[1]);17291730if (detailed_cap_info) {1731clk = drm_dp_downstream_max_dotclock(dpcd, port_cap);1732if (clk > 0)1733seq_printf(m, "\t\tMax dot clock: %d kHz\n", clk);17341735clk = drm_dp_downstream_max_tmds_clock(dpcd, port_cap, drm_edid);1736if (clk > 0)1737seq_printf(m, "\t\tMax TMDS clock: %d kHz\n", clk);17381739clk = drm_dp_downstream_min_tmds_clock(dpcd, port_cap, drm_edid);1740if (clk > 0)1741seq_printf(m, "\t\tMin TMDS clock: %d kHz\n", clk);17421743bpc = drm_dp_downstream_max_bpc(dpcd, port_cap, drm_edid);17441745if (bpc > 0)1746seq_printf(m, "\t\tMax bpc: %d\n", bpc);1747}1748}1749EXPORT_SYMBOL(drm_dp_downstream_debug);17501751/**1752* drm_dp_subconnector_type() - get DP branch device type1753* @dpcd: DisplayPort configuration data1754* @port_cap: port capabilities1755*/1756enum drm_mode_subconnector1757drm_dp_subconnector_type(const u8 dpcd[DP_RECEIVER_CAP_SIZE],1758const u8 port_cap[4])1759{1760int type;1761if (!drm_dp_is_branch(dpcd))1762return DRM_MODE_SUBCONNECTOR_Native;1763/* DP 1.0 approach */1764if (dpcd[DP_DPCD_REV] == DP_DPCD_REV_10) {1765type = dpcd[DP_DOWNSTREAMPORT_PRESENT] &1766DP_DWN_STRM_PORT_TYPE_MASK;17671768switch (type) {1769case DP_DWN_STRM_PORT_TYPE_TMDS:1770/* Can be HDMI or DVI-D, DVI-D is a safer option */1771return DRM_MODE_SUBCONNECTOR_DVID;1772case DP_DWN_STRM_PORT_TYPE_ANALOG:1773/* Can be VGA or DVI-A, VGA is more popular */1774return DRM_MODE_SUBCONNECTOR_VGA;1775case DP_DWN_STRM_PORT_TYPE_DP:1776return DRM_MODE_SUBCONNECTOR_DisplayPort;1777case DP_DWN_STRM_PORT_TYPE_OTHER:1778default:1779return DRM_MODE_SUBCONNECTOR_Unknown;1780}1781}1782type = port_cap[0] & DP_DS_PORT_TYPE_MASK;17831784switch (type) {1785case DP_DS_PORT_TYPE_DP:1786case DP_DS_PORT_TYPE_DP_DUALMODE:1787return DRM_MODE_SUBCONNECTOR_DisplayPort;1788case DP_DS_PORT_TYPE_VGA:1789return DRM_MODE_SUBCONNECTOR_VGA;1790case DP_DS_PORT_TYPE_DVI:1791return DRM_MODE_SUBCONNECTOR_DVID;1792case DP_DS_PORT_TYPE_HDMI:1793return DRM_MODE_SUBCONNECTOR_HDMIA;1794case DP_DS_PORT_TYPE_WIRELESS:1795return DRM_MODE_SUBCONNECTOR_Wireless;1796case DP_DS_PORT_TYPE_NON_EDID:1797default:1798return DRM_MODE_SUBCONNECTOR_Unknown;1799}1800}1801EXPORT_SYMBOL(drm_dp_subconnector_type);18021803/**1804* drm_dp_set_subconnector_property - set subconnector for DP connector1805* @connector: connector to set property on1806* @status: connector status1807* @dpcd: DisplayPort configuration data1808* @port_cap: port capabilities1809*1810* Called by a driver on every detect event.1811*/1812void drm_dp_set_subconnector_property(struct drm_connector *connector,1813enum drm_connector_status status,1814const u8 *dpcd,1815const u8 port_cap[4])1816{1817enum drm_mode_subconnector subconnector = DRM_MODE_SUBCONNECTOR_Unknown;18181819if (status == connector_status_connected)1820subconnector = drm_dp_subconnector_type(dpcd, port_cap);1821drm_object_property_set_value(&connector->base,1822connector->dev->mode_config.dp_subconnector_property,1823subconnector);1824}1825EXPORT_SYMBOL(drm_dp_set_subconnector_property);18261827/**1828* drm_dp_read_sink_count_cap() - Check whether a given connector has a valid sink1829* count1830* @connector: The DRM connector to check1831* @dpcd: A cached copy of the connector's DPCD RX capabilities1832* @desc: A cached copy of the connector's DP descriptor1833*1834* See also: drm_dp_read_sink_count()1835*1836* Returns: %True if the (e)DP connector has a valid sink count that should1837* be probed, %false otherwise.1838*/1839bool drm_dp_read_sink_count_cap(struct drm_connector *connector,1840const u8 dpcd[DP_RECEIVER_CAP_SIZE],1841const struct drm_dp_desc *desc)1842{1843/* Some eDP panels don't set a valid value for the sink count */1844return connector->connector_type != DRM_MODE_CONNECTOR_eDP &&1845dpcd[DP_DPCD_REV] >= DP_DPCD_REV_11 &&1846dpcd[DP_DOWNSTREAMPORT_PRESENT] & DP_DWN_STRM_PORT_PRESENT &&1847!drm_dp_has_quirk(desc, DP_DPCD_QUIRK_NO_SINK_COUNT);1848}1849EXPORT_SYMBOL(drm_dp_read_sink_count_cap);18501851/**1852* drm_dp_read_sink_count() - Retrieve the sink count for a given sink1853* @aux: The DP AUX channel to use1854*1855* See also: drm_dp_read_sink_count_cap()1856*1857* Returns: The current sink count reported by @aux, or a negative error code1858* otherwise.1859*/1860int drm_dp_read_sink_count(struct drm_dp_aux *aux)1861{1862u8 count;1863int ret;18641865ret = drm_dp_dpcd_read_byte(aux, DP_SINK_COUNT, &count);1866if (ret < 0)1867return ret;18681869return DP_GET_SINK_COUNT(count);1870}1871EXPORT_SYMBOL(drm_dp_read_sink_count);18721873/*1874* I2C-over-AUX implementation1875*/18761877static u32 drm_dp_i2c_functionality(struct i2c_adapter *adapter)1878{1879return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |1880I2C_FUNC_SMBUS_READ_BLOCK_DATA |1881I2C_FUNC_SMBUS_BLOCK_PROC_CALL |1882I2C_FUNC_10BIT_ADDR;1883}18841885static void drm_dp_i2c_msg_write_status_update(struct drm_dp_aux_msg *msg)1886{1887/*1888* In case of i2c defer or short i2c ack reply to a write,1889* we need to switch to WRITE_STATUS_UPDATE to drain the1890* rest of the message1891*/1892if ((msg->request & ~DP_AUX_I2C_MOT) == DP_AUX_I2C_WRITE) {1893msg->request &= DP_AUX_I2C_MOT;1894msg->request |= DP_AUX_I2C_WRITE_STATUS_UPDATE;1895}1896}18971898#define AUX_PRECHARGE_LEN 10 /* 10 to 16 */1899#define AUX_SYNC_LEN (16 + 4) /* preamble + AUX_SYNC_END */1900#define AUX_STOP_LEN 41901#define AUX_CMD_LEN 41902#define AUX_ADDRESS_LEN 201903#define AUX_REPLY_PAD_LEN 41904#define AUX_LENGTH_LEN 819051906/*1907* Calculate the duration of the AUX request/reply in usec. Gives the1908* "best" case estimate, ie. successful while as short as possible.1909*/1910static int drm_dp_aux_req_duration(const struct drm_dp_aux_msg *msg)1911{1912int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +1913AUX_CMD_LEN + AUX_ADDRESS_LEN + AUX_LENGTH_LEN;19141915if ((msg->request & DP_AUX_I2C_READ) == 0)1916len += msg->size * 8;19171918return len;1919}19201921static int drm_dp_aux_reply_duration(const struct drm_dp_aux_msg *msg)1922{1923int len = AUX_PRECHARGE_LEN + AUX_SYNC_LEN + AUX_STOP_LEN +1924AUX_CMD_LEN + AUX_REPLY_PAD_LEN;19251926/*1927* For read we expect what was asked. For writes there will1928* be 0 or 1 data bytes. Assume 0 for the "best" case.1929*/1930if (msg->request & DP_AUX_I2C_READ)1931len += msg->size * 8;19321933return len;1934}19351936#define I2C_START_LEN 11937#define I2C_STOP_LEN 11938#define I2C_ADDR_LEN 9 /* ADDRESS + R/W + ACK/NACK */1939#define I2C_DATA_LEN 9 /* DATA + ACK/NACK */19401941/*1942* Calculate the length of the i2c transfer in usec, assuming1943* the i2c bus speed is as specified. Gives the "worst"1944* case estimate, ie. successful while as long as possible.1945* Doesn't account the "MOT" bit, and instead assumes each1946* message includes a START, ADDRESS and STOP. Neither does it1947* account for additional random variables such as clock stretching.1948*/1949static int drm_dp_i2c_msg_duration(const struct drm_dp_aux_msg *msg,1950int i2c_speed_khz)1951{1952/* AUX bitrate is 1MHz, i2c bitrate as specified */1953return DIV_ROUND_UP((I2C_START_LEN + I2C_ADDR_LEN +1954msg->size * I2C_DATA_LEN +1955I2C_STOP_LEN) * 1000, i2c_speed_khz);1956}19571958/*1959* Determine how many retries should be attempted to successfully transfer1960* the specified message, based on the estimated durations of the1961* i2c and AUX transfers.1962*/1963static int drm_dp_i2c_retry_count(const struct drm_dp_aux_msg *msg,1964int i2c_speed_khz)1965{1966int aux_time_us = drm_dp_aux_req_duration(msg) +1967drm_dp_aux_reply_duration(msg);1968int i2c_time_us = drm_dp_i2c_msg_duration(msg, i2c_speed_khz);19691970return DIV_ROUND_UP(i2c_time_us, aux_time_us + AUX_RETRY_INTERVAL);1971}19721973/*1974* FIXME currently assumes 10 kHz as some real world devices seem1975* to require it. We should query/set the speed via DPCD if supported.1976*/1977static int dp_aux_i2c_speed_khz __read_mostly = 10;1978module_param_unsafe(dp_aux_i2c_speed_khz, int, 0644);1979MODULE_PARM_DESC(dp_aux_i2c_speed_khz,1980"Assumed speed of the i2c bus in kHz, (1-400, default 10)");19811982/*1983* Transfer a single I2C-over-AUX message and handle various error conditions,1984* retrying the transaction as appropriate. It is assumed that the1985* &drm_dp_aux.transfer function does not modify anything in the msg other than the1986* reply field.1987*1988* Returns bytes transferred on success, or a negative error code on failure.1989*/1990static int drm_dp_i2c_do_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)1991{1992unsigned int retry, defer_i2c;1993int ret;1994/*1995* DP1.2 sections 2.7.7.1.5.6.1 and 2.7.7.1.6.6.1: A DP Source device1996* is required to retry at least seven times upon receiving AUX_DEFER1997* before giving up the AUX transaction.1998*1999* We also try to account for the i2c bus speed.2000*/2001int max_retries = max(7, drm_dp_i2c_retry_count(msg, dp_aux_i2c_speed_khz));20022003for (retry = 0, defer_i2c = 0; retry < (max_retries + defer_i2c); retry++) {2004ret = aux->transfer(aux, msg);2005if (ret < 0) {2006if (ret == -EBUSY)2007continue;20082009/*2010* While timeouts can be errors, they're usually normal2011* behavior (for instance, when a driver tries to2012* communicate with a non-existent DisplayPort device).2013* Avoid spamming the kernel log with timeout errors.2014*/2015if (ret == -ETIMEDOUT)2016drm_dbg_kms_ratelimited(aux->drm_dev, "%s: transaction timed out\n",2017aux->name);2018else2019drm_dbg_kms(aux->drm_dev, "%s: transaction failed: %d\n",2020aux->name, ret);2021return ret;2022}202320242025switch (msg->reply & DP_AUX_NATIVE_REPLY_MASK) {2026case DP_AUX_NATIVE_REPLY_ACK:2027/*2028* For I2C-over-AUX transactions this isn't enough, we2029* need to check for the I2C ACK reply.2030*/2031break;20322033case DP_AUX_NATIVE_REPLY_NACK:2034drm_dbg_kms(aux->drm_dev, "%s: native nack (result=%d, size=%zu)\n",2035aux->name, ret, msg->size);2036return -EREMOTEIO;20372038case DP_AUX_NATIVE_REPLY_DEFER:2039drm_dbg_kms(aux->drm_dev, "%s: native defer\n", aux->name);2040/*2041* We could check for I2C bit rate capabilities and if2042* available adjust this interval. We could also be2043* more careful with DP-to-legacy adapters where a2044* long legacy cable may force very low I2C bit rates.2045*2046* For now just defer for long enough to hopefully be2047* safe for all use-cases.2048*/2049usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);2050continue;20512052default:2053drm_err(aux->drm_dev, "%s: invalid native reply %#04x\n",2054aux->name, msg->reply);2055return -EREMOTEIO;2056}20572058switch (msg->reply & DP_AUX_I2C_REPLY_MASK) {2059case DP_AUX_I2C_REPLY_ACK:2060/*2061* Both native ACK and I2C ACK replies received. We2062* can assume the transfer was successful.2063*/2064if (ret != msg->size)2065drm_dp_i2c_msg_write_status_update(msg);2066return ret;20672068case DP_AUX_I2C_REPLY_NACK:2069drm_dbg_kms(aux->drm_dev, "%s: I2C nack (result=%d, size=%zu)\n",2070aux->name, ret, msg->size);2071aux->i2c_nack_count++;2072return -EREMOTEIO;20732074case DP_AUX_I2C_REPLY_DEFER:2075drm_dbg_kms(aux->drm_dev, "%s: I2C defer\n", aux->name);2076/* DP Compliance Test 4.2.2.5 Requirement:2077* Must have at least 7 retries for I2C defers on the2078* transaction to pass this test2079*/2080aux->i2c_defer_count++;2081if (defer_i2c < 7)2082defer_i2c++;2083usleep_range(AUX_RETRY_INTERVAL, AUX_RETRY_INTERVAL + 100);2084drm_dp_i2c_msg_write_status_update(msg);20852086continue;20872088default:2089drm_err(aux->drm_dev, "%s: invalid I2C reply %#04x\n",2090aux->name, msg->reply);2091return -EREMOTEIO;2092}2093}20942095drm_dbg_kms(aux->drm_dev, "%s: Too many retries, giving up\n", aux->name);2096return -EREMOTEIO;2097}20982099static void drm_dp_i2c_msg_set_request(struct drm_dp_aux_msg *msg,2100const struct i2c_msg *i2c_msg)2101{2102msg->request = (i2c_msg->flags & I2C_M_RD) ?2103DP_AUX_I2C_READ : DP_AUX_I2C_WRITE;2104if (!(i2c_msg->flags & I2C_M_STOP))2105msg->request |= DP_AUX_I2C_MOT;2106}21072108/*2109* Keep retrying drm_dp_i2c_do_msg until all data has been transferred.2110*2111* Returns an error code on failure, or a recommended transfer size on success.2112*/2113static int drm_dp_i2c_drain_msg(struct drm_dp_aux *aux, struct drm_dp_aux_msg *orig_msg)2114{2115int err, ret = orig_msg->size;2116struct drm_dp_aux_msg msg = *orig_msg;21172118while (msg.size > 0) {2119err = drm_dp_i2c_do_msg(aux, &msg);2120if (err <= 0)2121return err == 0 ? -EPROTO : err;21222123if (err < msg.size && err < ret) {2124drm_dbg_kms(aux->drm_dev,2125"%s: Partial I2C reply: requested %zu bytes got %d bytes\n",2126aux->name, msg.size, err);2127ret = err;2128}21292130msg.size -= err;2131msg.buffer += err;2132}21332134return ret;2135}21362137/*2138* Bizlink designed DP->DVI-D Dual Link adapters require the I2C over AUX2139* packets to be as large as possible. If not, the I2C transactions never2140* succeed. Hence the default is maximum.2141*/2142static int dp_aux_i2c_transfer_size __read_mostly = DP_AUX_MAX_PAYLOAD_BYTES;2143module_param_unsafe(dp_aux_i2c_transfer_size, int, 0644);2144MODULE_PARM_DESC(dp_aux_i2c_transfer_size,2145"Number of bytes to transfer in a single I2C over DP AUX CH message, (1-16, default 16)");21462147static int drm_dp_i2c_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs,2148int num)2149{2150struct drm_dp_aux *aux = adapter->algo_data;2151unsigned int i, j;2152unsigned transfer_size;2153struct drm_dp_aux_msg msg;2154int err = 0;21552156if (aux->powered_down)2157return -EBUSY;21582159dp_aux_i2c_transfer_size = clamp(dp_aux_i2c_transfer_size, 1, DP_AUX_MAX_PAYLOAD_BYTES);21602161memset(&msg, 0, sizeof(msg));21622163for (i = 0; i < num; i++) {2164msg.address = msgs[i].addr;21652166if (!aux->no_zero_sized) {2167drm_dp_i2c_msg_set_request(&msg, &msgs[i]);2168/* Send a bare address packet to start the transaction.2169* Zero sized messages specify an address only (bare2170* address) transaction.2171*/2172msg.buffer = NULL;2173msg.size = 0;2174err = drm_dp_i2c_do_msg(aux, &msg);2175}21762177/*2178* Reset msg.request in case in case it got2179* changed into a WRITE_STATUS_UPDATE.2180*/2181drm_dp_i2c_msg_set_request(&msg, &msgs[i]);21822183if (err < 0)2184break;2185/* We want each transaction to be as large as possible, but2186* we'll go to smaller sizes if the hardware gives us a2187* short reply.2188*/2189transfer_size = dp_aux_i2c_transfer_size;2190for (j = 0; j < msgs[i].len; j += msg.size) {2191msg.buffer = msgs[i].buf + j;2192msg.size = min(transfer_size, msgs[i].len - j);21932194if (j + msg.size == msgs[i].len && aux->no_zero_sized)2195msg.request &= ~DP_AUX_I2C_MOT;2196err = drm_dp_i2c_drain_msg(aux, &msg);21972198/*2199* Reset msg.request in case in case it got2200* changed into a WRITE_STATUS_UPDATE.2201*/2202drm_dp_i2c_msg_set_request(&msg, &msgs[i]);22032204if (err < 0)2205break;2206transfer_size = err;2207}2208if (err < 0)2209break;2210}2211if (err >= 0)2212err = num;22132214if (!aux->no_zero_sized) {2215/* Send a bare address packet to close out the transaction.2216* Zero sized messages specify an address only (bare2217* address) transaction.2218*/2219msg.request &= ~DP_AUX_I2C_MOT;2220msg.buffer = NULL;2221msg.size = 0;2222(void)drm_dp_i2c_do_msg(aux, &msg);2223}2224return err;2225}22262227static const struct i2c_algorithm drm_dp_i2c_algo = {2228.functionality = drm_dp_i2c_functionality,2229.master_xfer = drm_dp_i2c_xfer,2230};22312232static struct drm_dp_aux *i2c_to_aux(struct i2c_adapter *i2c)2233{2234return container_of(i2c, struct drm_dp_aux, ddc);2235}22362237static void lock_bus(struct i2c_adapter *i2c, unsigned int flags)2238{2239mutex_lock(&i2c_to_aux(i2c)->hw_mutex);2240}22412242static int trylock_bus(struct i2c_adapter *i2c, unsigned int flags)2243{2244return mutex_trylock(&i2c_to_aux(i2c)->hw_mutex);2245}22462247static void unlock_bus(struct i2c_adapter *i2c, unsigned int flags)2248{2249mutex_unlock(&i2c_to_aux(i2c)->hw_mutex);2250}22512252static const struct i2c_lock_operations drm_dp_i2c_lock_ops = {2253.lock_bus = lock_bus,2254.trylock_bus = trylock_bus,2255.unlock_bus = unlock_bus,2256};22572258static int drm_dp_aux_get_crc(struct drm_dp_aux *aux, u8 *crc)2259{2260u8 buf, count;2261int ret;22622263ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);2264if (ret < 0)2265return ret;22662267WARN_ON(!(buf & DP_TEST_SINK_START));22682269ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK_MISC, &buf);2270if (ret < 0)2271return ret;22722273count = buf & DP_TEST_COUNT_MASK;2274if (count == aux->crc_count)2275return -EAGAIN; /* No CRC yet */22762277aux->crc_count = count;22782279/*2280* At DP_TEST_CRC_R_CR, there's 6 bytes containing CRC data, 2 bytes2281* per component (RGB or CrYCb).2282*/2283return drm_dp_dpcd_read_data(aux, DP_TEST_CRC_R_CR, crc, 6);2284}22852286static void drm_dp_aux_crc_work(struct work_struct *work)2287{2288struct drm_dp_aux *aux = container_of(work, struct drm_dp_aux,2289crc_work);2290struct drm_crtc *crtc;2291u8 crc_bytes[6];2292uint32_t crcs[3];2293int ret;22942295if (WARN_ON(!aux->crtc))2296return;22972298crtc = aux->crtc;2299while (crtc->crc.opened) {2300drm_crtc_wait_one_vblank(crtc);2301if (!crtc->crc.opened)2302break;23032304ret = drm_dp_aux_get_crc(aux, crc_bytes);2305if (ret == -EAGAIN) {2306usleep_range(1000, 2000);2307ret = drm_dp_aux_get_crc(aux, crc_bytes);2308}23092310if (ret == -EAGAIN) {2311drm_dbg_kms(aux->drm_dev, "%s: Get CRC failed after retrying: %d\n",2312aux->name, ret);2313continue;2314} else if (ret) {2315drm_dbg_kms(aux->drm_dev, "%s: Failed to get a CRC: %d\n", aux->name, ret);2316continue;2317}23182319crcs[0] = crc_bytes[0] | crc_bytes[1] << 8;2320crcs[1] = crc_bytes[2] | crc_bytes[3] << 8;2321crcs[2] = crc_bytes[4] | crc_bytes[5] << 8;2322drm_crtc_add_crc_entry(crtc, false, 0, crcs);2323}2324}23252326/**2327* drm_dp_remote_aux_init() - minimally initialise a remote aux channel2328* @aux: DisplayPort AUX channel2329*2330* Used for remote aux channel in general. Merely initialize the crc work2331* struct.2332*/2333void drm_dp_remote_aux_init(struct drm_dp_aux *aux)2334{2335INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);2336}2337EXPORT_SYMBOL(drm_dp_remote_aux_init);23382339/**2340* drm_dp_aux_init() - minimally initialise an aux channel2341* @aux: DisplayPort AUX channel2342*2343* If you need to use the drm_dp_aux's i2c adapter prior to registering it with2344* the outside world, call drm_dp_aux_init() first. For drivers which are2345* grandparents to their AUX adapters (e.g. the AUX adapter is parented by a2346* &drm_connector), you must still call drm_dp_aux_register() once the connector2347* has been registered to allow userspace access to the auxiliary DP channel.2348* Likewise, for such drivers you should also assign &drm_dp_aux.drm_dev as2349* early as possible so that the &drm_device that corresponds to the AUX adapter2350* may be mentioned in debugging output from the DRM DP helpers.2351*2352* For devices which use a separate platform device for their AUX adapters, this2353* may be called as early as required by the driver.2354*2355*/2356void drm_dp_aux_init(struct drm_dp_aux *aux)2357{2358mutex_init(&aux->hw_mutex);2359mutex_init(&aux->cec.lock);2360INIT_WORK(&aux->crc_work, drm_dp_aux_crc_work);23612362aux->ddc.algo = &drm_dp_i2c_algo;2363aux->ddc.algo_data = aux;2364aux->ddc.retries = 3;23652366aux->ddc.lock_ops = &drm_dp_i2c_lock_ops;2367}2368EXPORT_SYMBOL(drm_dp_aux_init);23692370/**2371* drm_dp_aux_register() - initialise and register aux channel2372* @aux: DisplayPort AUX channel2373*2374* Automatically calls drm_dp_aux_init() if this hasn't been done yet. This2375* should only be called once the parent of @aux, &drm_dp_aux.dev, is2376* initialized. For devices which are grandparents of their AUX channels,2377* &drm_dp_aux.dev will typically be the &drm_connector &device which2378* corresponds to @aux. For these devices, it's advised to call2379* drm_dp_aux_register() in &drm_connector_funcs.late_register, and likewise to2380* call drm_dp_aux_unregister() in &drm_connector_funcs.early_unregister.2381* Functions which don't follow this will likely Oops when2382* %CONFIG_DRM_DISPLAY_DP_AUX_CHARDEV is enabled.2383*2384* For devices where the AUX channel is a device that exists independently of2385* the &drm_device that uses it, such as SoCs and bridge devices, it is2386* recommended to call drm_dp_aux_register() after a &drm_device has been2387* assigned to &drm_dp_aux.drm_dev, and likewise to call2388* drm_dp_aux_unregister() once the &drm_device should no longer be associated2389* with the AUX channel (e.g. on bridge detach).2390*2391* Drivers which need to use the aux channel before either of the two points2392* mentioned above need to call drm_dp_aux_init() in order to use the AUX2393* channel before registration.2394*2395* Returns 0 on success or a negative error code on failure.2396*/2397int drm_dp_aux_register(struct drm_dp_aux *aux)2398{2399int ret;24002401WARN_ON_ONCE(!aux->drm_dev);24022403if (!aux->ddc.algo)2404drm_dp_aux_init(aux);24052406aux->ddc.owner = THIS_MODULE;2407aux->ddc.dev.parent = aux->dev;24082409strscpy(aux->ddc.name, aux->name ? aux->name : dev_name(aux->dev),2410sizeof(aux->ddc.name));24112412ret = drm_dp_aux_register_devnode(aux);2413if (ret)2414return ret;24152416ret = i2c_add_adapter(&aux->ddc);2417if (ret) {2418drm_dp_aux_unregister_devnode(aux);2419return ret;2420}24212422return 0;2423}2424EXPORT_SYMBOL(drm_dp_aux_register);24252426/**2427* drm_dp_aux_unregister() - unregister an AUX adapter2428* @aux: DisplayPort AUX channel2429*/2430void drm_dp_aux_unregister(struct drm_dp_aux *aux)2431{2432drm_dp_aux_unregister_devnode(aux);2433i2c_del_adapter(&aux->ddc);2434}2435EXPORT_SYMBOL(drm_dp_aux_unregister);24362437#define PSR_SETUP_TIME(x) [DP_PSR_SETUP_TIME_ ## x >> DP_PSR_SETUP_TIME_SHIFT] = (x)24382439/**2440* drm_dp_psr_setup_time() - PSR setup in time usec2441* @psr_cap: PSR capabilities from DPCD2442*2443* Returns:2444* PSR setup time for the panel in microseconds, negative2445* error code on failure.2446*/2447int drm_dp_psr_setup_time(const u8 psr_cap[EDP_PSR_RECEIVER_CAP_SIZE])2448{2449static const u16 psr_setup_time_us[] = {2450PSR_SETUP_TIME(330),2451PSR_SETUP_TIME(275),2452PSR_SETUP_TIME(220),2453PSR_SETUP_TIME(165),2454PSR_SETUP_TIME(110),2455PSR_SETUP_TIME(55),2456PSR_SETUP_TIME(0),2457};2458int i;24592460i = (psr_cap[1] & DP_PSR_SETUP_TIME_MASK) >> DP_PSR_SETUP_TIME_SHIFT;2461if (i >= ARRAY_SIZE(psr_setup_time_us))2462return -EINVAL;24632464return psr_setup_time_us[i];2465}2466EXPORT_SYMBOL(drm_dp_psr_setup_time);24672468#undef PSR_SETUP_TIME24692470/**2471* drm_dp_start_crc() - start capture of frame CRCs2472* @aux: DisplayPort AUX channel2473* @crtc: CRTC displaying the frames whose CRCs are to be captured2474*2475* Returns 0 on success or a negative error code on failure.2476*/2477int drm_dp_start_crc(struct drm_dp_aux *aux, struct drm_crtc *crtc)2478{2479u8 buf;2480int ret;24812482ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);2483if (ret < 0)2484return ret;24852486ret = drm_dp_dpcd_write_byte(aux, DP_TEST_SINK, buf | DP_TEST_SINK_START);2487if (ret < 0)2488return ret;24892490aux->crc_count = 0;2491aux->crtc = crtc;2492schedule_work(&aux->crc_work);24932494return 0;2495}2496EXPORT_SYMBOL(drm_dp_start_crc);24972498/**2499* drm_dp_stop_crc() - stop capture of frame CRCs2500* @aux: DisplayPort AUX channel2501*2502* Returns 0 on success or a negative error code on failure.2503*/2504int drm_dp_stop_crc(struct drm_dp_aux *aux)2505{2506u8 buf;2507int ret;25082509ret = drm_dp_dpcd_read_byte(aux, DP_TEST_SINK, &buf);2510if (ret < 0)2511return ret;25122513ret = drm_dp_dpcd_write_byte(aux, DP_TEST_SINK, buf & ~DP_TEST_SINK_START);2514if (ret < 0)2515return ret;25162517flush_work(&aux->crc_work);2518aux->crtc = NULL;25192520return 0;2521}2522EXPORT_SYMBOL(drm_dp_stop_crc);25232524struct dpcd_quirk {2525u8 oui[3];2526u8 device_id[6];2527bool is_branch;2528u32 quirks;2529};25302531#define OUI(first, second, third) { (first), (second), (third) }2532#define DEVICE_ID(first, second, third, fourth, fifth, sixth) \2533{ (first), (second), (third), (fourth), (fifth), (sixth) }25342535#define DEVICE_ID_ANY DEVICE_ID(0, 0, 0, 0, 0, 0)25362537static const struct dpcd_quirk dpcd_quirk_list[] = {2538/* Analogix 7737 needs reduced M and N at HBR2 link rates */2539{ OUI(0x00, 0x22, 0xb9), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_CONSTANT_N) },2540/* LG LP140WF6-SPM1 eDP panel */2541{ OUI(0x00, 0x22, 0xb9), DEVICE_ID('s', 'i', 'v', 'a', 'r', 'T'), false, BIT(DP_DPCD_QUIRK_CONSTANT_N) },2542/* Apple panels need some additional handling to support PSR */2543{ OUI(0x00, 0x10, 0xfa), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_NO_PSR) },2544/* CH7511 seems to leave SINK_COUNT zeroed */2545{ OUI(0x00, 0x00, 0x00), DEVICE_ID('C', 'H', '7', '5', '1', '1'), false, BIT(DP_DPCD_QUIRK_NO_SINK_COUNT) },2546/* Synaptics DP1.4 MST hubs can support DSC without virtual DPCD */2547{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_DSC_WITHOUT_VIRTUAL_DPCD) },2548/* Synaptics DP1.4 MST hubs require DSC for some modes on which it applies HBLANK expansion. */2549{ OUI(0x90, 0xCC, 0x24), DEVICE_ID_ANY, true, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },2550/* MediaTek panels (at least in U3224KBA) require DSC for modes with a short HBLANK on UHBR links. */2551{ OUI(0x00, 0x0C, 0xE7), DEVICE_ID_ANY, false, BIT(DP_DPCD_QUIRK_HBLANK_EXPANSION_REQUIRES_DSC) },2552/* Apple MacBookPro 2017 15 inch eDP Retina panel reports too low DP_MAX_LINK_RATE */2553{ OUI(0x00, 0x10, 0xfa), DEVICE_ID(101, 68, 21, 101, 98, 97), false, BIT(DP_DPCD_QUIRK_CAN_DO_MAX_LINK_RATE_3_24_GBPS) },2554/* Synaptics Panamera supports only a compressed bpp of 12 above 50% of its max DSC pixel throughput */2555{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x22), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },2556{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x31), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },2557{ OUI(0x90, 0xCC, 0x24), DEVICE_ID('S', 'Y', 'N', 'A', 0x53, 0x33), true, BIT(DP_DPCD_QUIRK_DSC_THROUGHPUT_BPP_LIMIT) },2558};25592560#undef OUI25612562/*2563* Get a bit mask of DPCD quirks for the sink/branch device identified by2564* ident. The quirk data is shared but it's up to the drivers to act on the2565* data.2566*2567* For now, only the OUI (first three bytes) is used, but this may be extended2568* to device identification string and hardware/firmware revisions later.2569*/2570static u322571drm_dp_get_quirks(const struct drm_dp_dpcd_ident *ident, bool is_branch)2572{2573const struct dpcd_quirk *quirk;2574u32 quirks = 0;2575int i;2576u8 any_device[] = DEVICE_ID_ANY;25772578for (i = 0; i < ARRAY_SIZE(dpcd_quirk_list); i++) {2579quirk = &dpcd_quirk_list[i];25802581if (quirk->is_branch != is_branch)2582continue;25832584if (memcmp(quirk->oui, ident->oui, sizeof(ident->oui)) != 0)2585continue;25862587if (memcmp(quirk->device_id, any_device, sizeof(any_device)) != 0 &&2588memcmp(quirk->device_id, ident->device_id, sizeof(ident->device_id)) != 0)2589continue;25902591quirks |= quirk->quirks;2592}25932594return quirks;2595}25962597#undef DEVICE_ID_ANY2598#undef DEVICE_ID25992600static int drm_dp_read_ident(struct drm_dp_aux *aux, unsigned int offset,2601struct drm_dp_dpcd_ident *ident)2602{2603return drm_dp_dpcd_read_data(aux, offset, ident, sizeof(*ident));2604}26052606static void drm_dp_dump_desc(struct drm_dp_aux *aux,2607const char *device_name, const struct drm_dp_desc *desc)2608{2609const struct drm_dp_dpcd_ident *ident = &desc->ident;26102611drm_dbg_kms(aux->drm_dev,2612"%s: %s: OUI %*phD dev-ID %*pE HW-rev %d.%d SW-rev %d.%d quirks 0x%04x\n",2613aux->name, device_name,2614(int)sizeof(ident->oui), ident->oui,2615(int)strnlen(ident->device_id, sizeof(ident->device_id)), ident->device_id,2616ident->hw_rev >> 4, ident->hw_rev & 0xf,2617ident->sw_major_rev, ident->sw_minor_rev,2618desc->quirks);2619}26202621/**2622* drm_dp_read_desc - read sink/branch descriptor from DPCD2623* @aux: DisplayPort AUX channel2624* @desc: Device descriptor to fill from DPCD2625* @is_branch: true for branch devices, false for sink devices2626*2627* Read DPCD 0x400 (sink) or 0x500 (branch) into @desc. Also debug log the2628* identification.2629*2630* Returns 0 on success or a negative error code on failure.2631*/2632int drm_dp_read_desc(struct drm_dp_aux *aux, struct drm_dp_desc *desc,2633bool is_branch)2634{2635struct drm_dp_dpcd_ident *ident = &desc->ident;2636unsigned int offset = is_branch ? DP_BRANCH_OUI : DP_SINK_OUI;2637int ret;26382639ret = drm_dp_read_ident(aux, offset, ident);2640if (ret < 0)2641return ret;26422643desc->quirks = drm_dp_get_quirks(ident, is_branch);26442645drm_dp_dump_desc(aux, is_branch ? "DP branch" : "DP sink", desc);26462647return 0;2648}2649EXPORT_SYMBOL(drm_dp_read_desc);26502651/**2652* drm_dp_dump_lttpr_desc - read and dump the DPCD descriptor for an LTTPR PHY2653* @aux: DisplayPort AUX channel2654* @dp_phy: LTTPR PHY instance2655*2656* Read the DPCD LTTPR PHY descriptor for @dp_phy and print a debug message2657* with its details to dmesg.2658*2659* Returns 0 on success or a negative error code on failure.2660*/2661int drm_dp_dump_lttpr_desc(struct drm_dp_aux *aux, enum drm_dp_phy dp_phy)2662{2663struct drm_dp_desc desc = {};2664int ret;26652666if (drm_WARN_ON(aux->drm_dev, dp_phy < DP_PHY_LTTPR1 || dp_phy > DP_MAX_LTTPR_COUNT))2667return -EINVAL;26682669ret = drm_dp_read_ident(aux, DP_OUI_PHY_REPEATER(dp_phy), &desc.ident);2670if (ret < 0)2671return ret;26722673drm_dp_dump_desc(aux, drm_dp_phy_name(dp_phy), &desc);26742675return 0;2676}2677EXPORT_SYMBOL(drm_dp_dump_lttpr_desc);26782679/**2680* drm_dp_dsc_sink_bpp_incr() - Get bits per pixel increment2681* @dsc_dpcd: DSC capabilities from DPCD2682*2683* Returns the bpp precision supported by the DP sink.2684*/2685u8 drm_dp_dsc_sink_bpp_incr(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])2686{2687u8 bpp_increment_dpcd = dsc_dpcd[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT];26882689switch (bpp_increment_dpcd & DP_DSC_BITS_PER_PIXEL_MASK) {2690case DP_DSC_BITS_PER_PIXEL_1_16:2691return 16;2692case DP_DSC_BITS_PER_PIXEL_1_8:2693return 8;2694case DP_DSC_BITS_PER_PIXEL_1_4:2695return 4;2696case DP_DSC_BITS_PER_PIXEL_1_2:2697return 2;2698case DP_DSC_BITS_PER_PIXEL_1_1:2699return 1;2700}27012702return 0;2703}2704EXPORT_SYMBOL(drm_dp_dsc_sink_bpp_incr);27052706/**2707* drm_dp_dsc_sink_max_slice_count() - Get the max slice count2708* supported by the DSC sink.2709* @dsc_dpcd: DSC capabilities from DPCD2710* @is_edp: true if its eDP, false for DP2711*2712* Read the slice capabilities DPCD register from DSC sink to get2713* the maximum slice count supported. This is used to populate2714* the DSC parameters in the &struct drm_dsc_config by the driver.2715* Driver creates an infoframe using these parameters to populate2716* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2717* infoframe using the helper function drm_dsc_pps_infoframe_pack()2718*2719* Returns:2720* Maximum slice count supported by DSC sink or 0 its invalid2721*/2722u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2723bool is_edp)2724{2725u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];27262727if (is_edp) {2728/* For eDP, register DSC_SLICE_CAPABILITIES_1 gives slice count */2729if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)2730return 4;2731if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)2732return 2;2733if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)2734return 1;2735} else {2736/* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */2737u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];27382739if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)2740return 24;2741if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)2742return 20;2743if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)2744return 16;2745if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)2746return 12;2747if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)2748return 10;2749if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)2750return 8;2751if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)2752return 6;2753if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)2754return 4;2755if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)2756return 2;2757if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)2758return 1;2759}27602761return 0;2762}2763EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);27642765/**2766* drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits2767* @dsc_dpcd: DSC capabilities from DPCD2768*2769* Read the DSC DPCD register to parse the line buffer depth in bits which is2770* number of bits of precision within the decoder line buffer supported by2771* the DSC sink. This is used to populate the DSC parameters in the2772* &struct drm_dsc_config by the driver.2773* Driver creates an infoframe using these parameters to populate2774* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2775* infoframe using the helper function drm_dsc_pps_infoframe_pack()2776*2777* Returns:2778* Line buffer depth supported by DSC panel or 0 its invalid2779*/2780u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])2781{2782u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];27832784switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {2785case DP_DSC_LINE_BUF_BIT_DEPTH_9:2786return 9;2787case DP_DSC_LINE_BUF_BIT_DEPTH_10:2788return 10;2789case DP_DSC_LINE_BUF_BIT_DEPTH_11:2790return 11;2791case DP_DSC_LINE_BUF_BIT_DEPTH_12:2792return 12;2793case DP_DSC_LINE_BUF_BIT_DEPTH_13:2794return 13;2795case DP_DSC_LINE_BUF_BIT_DEPTH_14:2796return 14;2797case DP_DSC_LINE_BUF_BIT_DEPTH_15:2798return 15;2799case DP_DSC_LINE_BUF_BIT_DEPTH_16:2800return 16;2801case DP_DSC_LINE_BUF_BIT_DEPTH_8:2802return 8;2803}28042805return 0;2806}2807EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);28082809/**2810* drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component2811* values supported by the DSC sink.2812* @dsc_dpcd: DSC capabilities from DPCD2813* @dsc_bpc: An array to be filled by this helper with supported2814* input bpcs.2815*2816* Read the DSC DPCD from the sink device to parse the supported bits per2817* component values. This is used to populate the DSC parameters2818* in the &struct drm_dsc_config by the driver.2819* Driver creates an infoframe using these parameters to populate2820* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2821* infoframe using the helper function drm_dsc_pps_infoframe_pack()2822*2823* Returns:2824* Number of input BPC values parsed from the DPCD2825*/2826int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2827u8 dsc_bpc[3])2828{2829int num_bpc = 0;2830u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];28312832if (!drm_dp_sink_supports_dsc(dsc_dpcd))2833return 0;28342835if (color_depth & DP_DSC_12_BPC)2836dsc_bpc[num_bpc++] = 12;2837if (color_depth & DP_DSC_10_BPC)2838dsc_bpc[num_bpc++] = 10;28392840/* A DP DSC Sink device shall support 8 bpc. */2841dsc_bpc[num_bpc++] = 8;28422843return num_bpc;2844}2845EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);28462847/*2848* See DP Standard v2.1a 2.8.4 Minimum Slices/Display, Table 2-159 and2849* Appendix L.1 Derivation of Slice Count Requirements.2850*/2851static int dsc_sink_min_slice_throughput(int peak_pixel_rate)2852{2853if (peak_pixel_rate >= 4800000)2854return 600000;2855else if (peak_pixel_rate >= 2700000)2856return 400000;2857else2858return 340000;2859}28602861/**2862* drm_dp_dsc_sink_max_slice_throughput() - Get a DSC sink's maximum pixel throughput per slice2863* @dsc_dpcd: DSC sink's capabilities from DPCD2864* @peak_pixel_rate: Cumulative peak pixel rate in kHz2865* @is_rgb_yuv444: The mode is either RGB or YUV4442866*2867* Return the DSC sink device's maximum pixel throughput per slice, based on2868* the device's @dsc_dpcd capabilities, the @peak_pixel_rate of the transferred2869* stream(s) and whether the output format @is_rgb_yuv444 or yuv422/yuv420.2870*2871* Note that @peak_pixel_rate is the total pixel rate transferred to the same2872* DSC/display sink. For instance to calculate a tile's slice count of an MST2873* multi-tiled display sink (not considering here the required2874* rounding/alignment of slice count)::2875*2876* @peak_pixel_rate = tile_pixel_rate * tile_count2877* total_slice_count = @peak_pixel_rate / drm_dp_dsc_sink_max_slice_throughput(@peak_pixel_rate)2878* tile_slice_count = total_slice_count / tile_count2879*2880* Returns:2881* The maximum pixel throughput per slice supported by the DSC sink device2882* in kPixels/sec.2883*/2884int drm_dp_dsc_sink_max_slice_throughput(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2885int peak_pixel_rate, bool is_rgb_yuv444)2886{2887int throughput;2888int delta = 0;2889int base;28902891throughput = dsc_dpcd[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];28922893if (is_rgb_yuv444) {2894throughput = (throughput & DP_DSC_THROUGHPUT_MODE_0_MASK) >>2895DP_DSC_THROUGHPUT_MODE_0_SHIFT;28962897delta = ((dsc_dpcd[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT]) &2898DP_DSC_THROUGHPUT_MODE_0_DELTA_MASK) >>2899DP_DSC_THROUGHPUT_MODE_0_DELTA_SHIFT; /* in units of 2 MPixels/sec */2900delta *= 2000;2901} else {2902throughput = (throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >>2903DP_DSC_THROUGHPUT_MODE_1_SHIFT;2904}29052906switch (throughput) {2907case 0:2908return dsc_sink_min_slice_throughput(peak_pixel_rate);2909case 1:2910base = 340000;2911break;2912case 2 ... 14:2913base = 400000 + 50000 * (throughput - 2);2914break;2915case 15:2916base = 170000;2917break;2918}29192920return base + delta;2921}2922EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_throughput);29232924static u8 dsc_branch_dpcd_cap(const u8 dpcd[DP_DSC_BRANCH_CAP_SIZE], int reg)2925{2926return dpcd[reg - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];2927}29282929/**2930* drm_dp_dsc_branch_max_overall_throughput() - Branch device's max overall DSC pixel throughput2931* @dsc_branch_dpcd: DSC branch capabilities from DPCD2932* @is_rgb_yuv444: The mode is either RGB or YUV4442933*2934* Return the branch device's maximum overall DSC pixel throughput, based on2935* the device's DPCD DSC branch capabilities, and whether the output2936* format @is_rgb_yuv444 or yuv422/yuv420.2937*2938* Returns:2939* - 0: The maximum overall throughput capability is not indicated by2940* the device separately and it must be determined from the per-slice2941* max throughput (see @drm_dp_dsc_branch_slice_max_throughput())2942* and the maximum slice count supported by the device.2943* - > 0: The maximum overall DSC pixel throughput supported by the branch2944* device in kPixels/sec.2945*/2946int drm_dp_dsc_branch_max_overall_throughput(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE],2947bool is_rgb_yuv444)2948{2949int throughput;29502951if (is_rgb_yuv444)2952throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,2953DP_DSC_BRANCH_OVERALL_THROUGHPUT_0);2954else2955throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,2956DP_DSC_BRANCH_OVERALL_THROUGHPUT_1);29572958switch (throughput) {2959case 0:2960return 0;2961case 1:2962return 680000;2963default:2964return 600000 + 50000 * throughput;2965}2966}2967EXPORT_SYMBOL(drm_dp_dsc_branch_max_overall_throughput);29682969/**2970* drm_dp_dsc_branch_max_line_width() - Branch device's max DSC line width2971* @dsc_branch_dpcd: DSC branch capabilities from DPCD2972*2973* Return the branch device's maximum overall DSC line width, based on2974* the device's @dsc_branch_dpcd capabilities.2975*2976* Returns:2977* - 0: The maximum line width is not indicated by the device2978* separately and it must be determined from the maximum2979* slice count and slice-width supported by the device.2980* - %-EINVAL: The device indicates an invalid maximum line width2981* (< 5120 pixels).2982* - >= 5120: The maximum line width in pixels.2983*/2984int drm_dp_dsc_branch_max_line_width(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE])2985{2986int line_width = dsc_branch_dpcd_cap(dsc_branch_dpcd, DP_DSC_BRANCH_MAX_LINE_WIDTH);29872988switch (line_width) {2989case 0:2990return 0;2991case 1 ... 15:2992return -EINVAL;2993default:2994return line_width * 320;2995}2996}2997EXPORT_SYMBOL(drm_dp_dsc_branch_max_line_width);29982999static int drm_dp_read_lttpr_regs(struct drm_dp_aux *aux,3000const u8 dpcd[DP_RECEIVER_CAP_SIZE], int address,3001u8 *buf, int buf_size)3002{3003/*3004* At least the DELL P2715Q monitor with a DPCD_REV < 0x14 returns3005* corrupted values when reading from the 0xF0000- range with a block3006* size bigger than 1.3007*/3008int block_size = dpcd[DP_DPCD_REV] < 0x14 ? 1 : buf_size;3009int offset;3010int ret;30113012for (offset = 0; offset < buf_size; offset += block_size) {3013ret = drm_dp_dpcd_read_data(aux,3014address + offset,3015&buf[offset], block_size);3016if (ret < 0)3017return ret;3018}30193020return 0;3021}30223023/**3024* drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities3025* @aux: DisplayPort AUX channel3026* @dpcd: DisplayPort configuration data3027* @caps: buffer to return the capability info in3028*3029* Read capabilities common to all LTTPRs.3030*3031* Returns 0 on success or a negative error code on failure.3032*/3033int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,3034const u8 dpcd[DP_RECEIVER_CAP_SIZE],3035u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3036{3037return drm_dp_read_lttpr_regs(aux, dpcd,3038DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,3039caps, DP_LTTPR_COMMON_CAP_SIZE);3040}3041EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);30423043/**3044* drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY3045* @aux: DisplayPort AUX channel3046* @dpcd: DisplayPort configuration data3047* @dp_phy: LTTPR PHY to read the capabilities for3048* @caps: buffer to return the capability info in3049*3050* Read the capabilities for the given LTTPR PHY.3051*3052* Returns 0 on success or a negative error code on failure.3053*/3054int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,3055const u8 dpcd[DP_RECEIVER_CAP_SIZE],3056enum drm_dp_phy dp_phy,3057u8 caps[DP_LTTPR_PHY_CAP_SIZE])3058{3059return drm_dp_read_lttpr_regs(aux, dpcd,3060DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),3061caps, DP_LTTPR_PHY_CAP_SIZE);3062}3063EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);30643065static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)3066{3067return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];3068}30693070/**3071* drm_dp_lttpr_count - get the number of detected LTTPRs3072* @caps: LTTPR common capabilities3073*3074* Get the number of detected LTTPRs from the LTTPR common capabilities info.3075*3076* Returns:3077* -ERANGE if more than supported number (8) of LTTPRs are detected3078* -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value3079* otherwise the number of detected LTTPRs3080*/3081int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3082{3083u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);30843085switch (hweight8(count)) {3086case 0:3087return 0;3088case 1:3089return 8 - ilog2(count);3090case 8:3091return -ERANGE;3092default:3093return -EINVAL;3094}3095}3096EXPORT_SYMBOL(drm_dp_lttpr_count);30973098/**3099* drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs3100* @caps: LTTPR common capabilities3101*3102* Returns the maximum link rate supported by all detected LTTPRs.3103*/3104int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3105{3106u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);31073108return drm_dp_bw_code_to_link_rate(rate);3109}3110EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);31113112/**3113* drm_dp_lttpr_set_transparent_mode() - set the LTTPR in transparent mode3114* @aux: DisplayPort AUX channel3115* @enable: Enable or disable transparent mode3116*3117* Returns: 0 on success or a negative error code on failure.3118*/3119int drm_dp_lttpr_set_transparent_mode(struct drm_dp_aux *aux, bool enable)3120{3121u8 val = enable ? DP_PHY_REPEATER_MODE_TRANSPARENT :3122DP_PHY_REPEATER_MODE_NON_TRANSPARENT;3123int ret = drm_dp_dpcd_writeb(aux, DP_PHY_REPEATER_MODE, val);31243125if (ret < 0)3126return ret;31273128return (ret == 1) ? 0 : -EIO;3129}3130EXPORT_SYMBOL(drm_dp_lttpr_set_transparent_mode);31313132/**3133* drm_dp_lttpr_init() - init LTTPR transparency mode according to DP standard3134* @aux: DisplayPort AUX channel3135* @lttpr_count: Number of LTTPRs. Between 0 and 8, according to DP standard.3136* Negative error code for any non-valid number.3137* See drm_dp_lttpr_count().3138*3139* Returns: 0 on success or a negative error code on failure.3140*/3141int drm_dp_lttpr_init(struct drm_dp_aux *aux, int lttpr_count)3142{3143int ret;31443145if (!lttpr_count)3146return 0;31473148/*3149* See DP Standard v2.0 3.6.6.1 about the explicit disabling of3150* non-transparent mode and the disable->enable non-transparent mode3151* sequence.3152*/3153ret = drm_dp_lttpr_set_transparent_mode(aux, true);3154if (ret)3155return ret;31563157if (lttpr_count < 0)3158return -ENODEV;31593160if (drm_dp_lttpr_set_transparent_mode(aux, false)) {3161/*3162* Roll-back to transparent mode if setting non-transparent3163* mode has failed3164*/3165drm_dp_lttpr_set_transparent_mode(aux, true);3166return -EINVAL;3167}31683169return 0;3170}3171EXPORT_SYMBOL(drm_dp_lttpr_init);31723173/**3174* drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs3175* @caps: LTTPR common capabilities3176*3177* Returns the maximum lane count supported by all detected LTTPRs.3178*/3179int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3180{3181u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);31823183return max_lanes & DP_MAX_LANE_COUNT_MASK;3184}3185EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);31863187/**3188* drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support3189* @caps: LTTPR PHY capabilities3190*3191* Returns true if the @caps for an LTTPR TX PHY indicate support for3192* voltage swing level 3.3193*/3194bool3195drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])3196{3197u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);31983199return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;3200}3201EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);32023203/**3204* drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support3205* @caps: LTTPR PHY capabilities3206*3207* Returns true if the @caps for an LTTPR TX PHY indicate support for3208* pre-emphasis level 3.3209*/3210bool3211drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])3212{3213u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);32143215return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;3216}3217EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);32183219/**3220* drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.3221* @aux: DisplayPort AUX channel3222* @data: DP phy compliance test parameters.3223*3224* Returns 0 on success or a negative error code on failure.3225*/3226int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,3227struct drm_dp_phy_test_params *data)3228{3229int err;3230u8 rate, lanes;32313232err = drm_dp_dpcd_read_byte(aux, DP_TEST_LINK_RATE, &rate);3233if (err < 0)3234return err;3235data->link_rate = drm_dp_bw_code_to_link_rate(rate);32363237err = drm_dp_dpcd_read_byte(aux, DP_TEST_LANE_COUNT, &lanes);3238if (err < 0)3239return err;3240data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;32413242if (lanes & DP_ENHANCED_FRAME_CAP)3243data->enhanced_frame_cap = true;32443245err = drm_dp_dpcd_read_byte(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);3246if (err < 0)3247return err;32483249switch (data->phy_pattern) {3250case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:3251err = drm_dp_dpcd_read_data(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,3252&data->custom80, sizeof(data->custom80));3253if (err < 0)3254return err;32553256break;3257case DP_PHY_TEST_PATTERN_CP2520:3258err = drm_dp_dpcd_read_data(aux, DP_TEST_HBR2_SCRAMBLER_RESET,3259&data->hbr2_reset,3260sizeof(data->hbr2_reset));3261if (err < 0)3262return err;3263}32643265return 0;3266}3267EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);32683269/**3270* drm_dp_set_phy_test_pattern() - set the pattern to the sink.3271* @aux: DisplayPort AUX channel3272* @data: DP phy compliance test parameters.3273* @dp_rev: DP revision to use for compliance testing3274*3275* Returns 0 on success or a negative error code on failure.3276*/3277int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,3278struct drm_dp_phy_test_params *data, u8 dp_rev)3279{3280int err, i;3281u8 test_pattern;32823283test_pattern = data->phy_pattern;3284if (dp_rev < 0x12) {3285test_pattern = (test_pattern << 2) &3286DP_LINK_QUAL_PATTERN_11_MASK;3287err = drm_dp_dpcd_write_byte(aux, DP_TRAINING_PATTERN_SET,3288test_pattern);3289if (err < 0)3290return err;3291} else {3292for (i = 0; i < data->num_lanes; i++) {3293err = drm_dp_dpcd_write_byte(aux,3294DP_LINK_QUAL_LANE0_SET + i,3295test_pattern);3296if (err < 0)3297return err;3298}3299}33003301return 0;3302}3303EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);33043305static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)3306{3307if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)3308return "Invalid";33093310switch (pixelformat) {3311case DP_PIXELFORMAT_RGB:3312return "RGB";3313case DP_PIXELFORMAT_YUV444:3314return "YUV444";3315case DP_PIXELFORMAT_YUV422:3316return "YUV422";3317case DP_PIXELFORMAT_YUV420:3318return "YUV420";3319case DP_PIXELFORMAT_Y_ONLY:3320return "Y_ONLY";3321case DP_PIXELFORMAT_RAW:3322return "RAW";3323default:3324return "Reserved";3325}3326}33273328static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,3329enum dp_colorimetry colorimetry)3330{3331if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)3332return "Invalid";33333334switch (colorimetry) {3335case DP_COLORIMETRY_DEFAULT:3336switch (pixelformat) {3337case DP_PIXELFORMAT_RGB:3338return "sRGB";3339case DP_PIXELFORMAT_YUV444:3340case DP_PIXELFORMAT_YUV422:3341case DP_PIXELFORMAT_YUV420:3342return "BT.601";3343case DP_PIXELFORMAT_Y_ONLY:3344return "DICOM PS3.14";3345case DP_PIXELFORMAT_RAW:3346return "Custom Color Profile";3347default:3348return "Reserved";3349}3350case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */3351switch (pixelformat) {3352case DP_PIXELFORMAT_RGB:3353return "Wide Fixed";3354case DP_PIXELFORMAT_YUV444:3355case DP_PIXELFORMAT_YUV422:3356case DP_PIXELFORMAT_YUV420:3357return "BT.709";3358default:3359return "Reserved";3360}3361case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */3362switch (pixelformat) {3363case DP_PIXELFORMAT_RGB:3364return "Wide Float";3365case DP_PIXELFORMAT_YUV444:3366case DP_PIXELFORMAT_YUV422:3367case DP_PIXELFORMAT_YUV420:3368return "xvYCC 601";3369default:3370return "Reserved";3371}3372case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */3373switch (pixelformat) {3374case DP_PIXELFORMAT_RGB:3375return "OpRGB";3376case DP_PIXELFORMAT_YUV444:3377case DP_PIXELFORMAT_YUV422:3378case DP_PIXELFORMAT_YUV420:3379return "xvYCC 709";3380default:3381return "Reserved";3382}3383case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */3384switch (pixelformat) {3385case DP_PIXELFORMAT_RGB:3386return "DCI-P3";3387case DP_PIXELFORMAT_YUV444:3388case DP_PIXELFORMAT_YUV422:3389case DP_PIXELFORMAT_YUV420:3390return "sYCC 601";3391default:3392return "Reserved";3393}3394case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */3395switch (pixelformat) {3396case DP_PIXELFORMAT_RGB:3397return "Custom Profile";3398case DP_PIXELFORMAT_YUV444:3399case DP_PIXELFORMAT_YUV422:3400case DP_PIXELFORMAT_YUV420:3401return "OpYCC 601";3402default:3403return "Reserved";3404}3405case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */3406switch (pixelformat) {3407case DP_PIXELFORMAT_RGB:3408return "BT.2020 RGB";3409case DP_PIXELFORMAT_YUV444:3410case DP_PIXELFORMAT_YUV422:3411case DP_PIXELFORMAT_YUV420:3412return "BT.2020 CYCC";3413default:3414return "Reserved";3415}3416case DP_COLORIMETRY_BT2020_YCC:3417switch (pixelformat) {3418case DP_PIXELFORMAT_YUV444:3419case DP_PIXELFORMAT_YUV422:3420case DP_PIXELFORMAT_YUV420:3421return "BT.2020 YCC";3422default:3423return "Reserved";3424}3425default:3426return "Invalid";3427}3428}34293430static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)3431{3432switch (dynamic_range) {3433case DP_DYNAMIC_RANGE_VESA:3434return "VESA range";3435case DP_DYNAMIC_RANGE_CTA:3436return "CTA range";3437default:3438return "Invalid";3439}3440}34413442static const char *dp_content_type_get_name(enum dp_content_type content_type)3443{3444switch (content_type) {3445case DP_CONTENT_TYPE_NOT_DEFINED:3446return "Not defined";3447case DP_CONTENT_TYPE_GRAPHICS:3448return "Graphics";3449case DP_CONTENT_TYPE_PHOTO:3450return "Photo";3451case DP_CONTENT_TYPE_VIDEO:3452return "Video";3453case DP_CONTENT_TYPE_GAME:3454return "Game";3455default:3456return "Reserved";3457}3458}34593460void drm_dp_vsc_sdp_log(struct drm_printer *p, const struct drm_dp_vsc_sdp *vsc)3461{3462drm_printf(p, "DP SDP: VSC, revision %u, length %u\n",3463vsc->revision, vsc->length);3464drm_printf(p, " pixelformat: %s\n",3465dp_pixelformat_get_name(vsc->pixelformat));3466drm_printf(p, " colorimetry: %s\n",3467dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));3468drm_printf(p, " bpc: %u\n", vsc->bpc);3469drm_printf(p, " dynamic range: %s\n",3470dp_dynamic_range_get_name(vsc->dynamic_range));3471drm_printf(p, " content type: %s\n",3472dp_content_type_get_name(vsc->content_type));3473}3474EXPORT_SYMBOL(drm_dp_vsc_sdp_log);34753476void drm_dp_as_sdp_log(struct drm_printer *p, const struct drm_dp_as_sdp *as_sdp)3477{3478drm_printf(p, "DP SDP: AS_SDP, revision %u, length %u\n",3479as_sdp->revision, as_sdp->length);3480drm_printf(p, " vtotal: %d\n", as_sdp->vtotal);3481drm_printf(p, " target_rr: %d\n", as_sdp->target_rr);3482drm_printf(p, " duration_incr_ms: %d\n", as_sdp->duration_incr_ms);3483drm_printf(p, " duration_decr_ms: %d\n", as_sdp->duration_decr_ms);3484drm_printf(p, " operation_mode: %d\n", as_sdp->mode);3485}3486EXPORT_SYMBOL(drm_dp_as_sdp_log);34873488/**3489* drm_dp_as_sdp_supported() - check if adaptive sync sdp is supported3490* @aux: DisplayPort AUX channel3491* @dpcd: DisplayPort configuration data3492*3493* Returns true if adaptive sync sdp is supported, else returns false3494*/3495bool drm_dp_as_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])3496{3497u8 rx_feature;34983499if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)3500return false;35013502if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,3503&rx_feature) < 0) {3504drm_dbg_dp(aux->drm_dev,3505"Failed to read DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1\n");3506return false;3507}35083509return (rx_feature & DP_ADAPTIVE_SYNC_SDP_SUPPORTED);3510}3511EXPORT_SYMBOL(drm_dp_as_sdp_supported);35123513/**3514* drm_dp_vsc_sdp_supported() - check if vsc sdp is supported3515* @aux: DisplayPort AUX channel3516* @dpcd: DisplayPort configuration data3517*3518* Returns true if vsc sdp is supported, else returns false3519*/3520bool drm_dp_vsc_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])3521{3522u8 rx_feature;35233524if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)3525return false;35263527if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST, &rx_feature) < 0) {3528drm_dbg_dp(aux->drm_dev, "failed to read DP_DPRX_FEATURE_ENUMERATION_LIST\n");3529return false;3530}35313532return (rx_feature & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED);3533}3534EXPORT_SYMBOL(drm_dp_vsc_sdp_supported);35353536/**3537* drm_dp_vsc_sdp_pack() - pack a given vsc sdp into generic dp_sdp3538* @vsc: vsc sdp initialized according to its purpose as defined in3539* table 2-118 - table 2-120 in DP 1.4a specification3540* @sdp: valid handle to the generic dp_sdp which will be packed3541*3542* Returns length of sdp on success and error code on failure3543*/3544ssize_t drm_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,3545struct dp_sdp *sdp)3546{3547size_t length = sizeof(struct dp_sdp);35483549memset(sdp, 0, sizeof(struct dp_sdp));35503551/*3552* Prepare VSC Header for SU as per DP 1.4a spec, Table 2-1193553* VSC SDP Header Bytes3554*/3555sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */3556sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */3557sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */3558sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */35593560if (vsc->revision == 0x6) {3561sdp->db[0] = 1;3562sdp->db[3] = 1;3563}35643565/*3566* Revision 0x5 and revision 0x7 supports Pixel Encoding/Colorimetry3567* Format as per DP 1.4a spec and DP 2.0 respectively.3568*/3569if (!(vsc->revision == 0x5 || vsc->revision == 0x7))3570goto out;35713572/* VSC SDP Payload for DB16 through DB18 */3573/* Pixel Encoding and Colorimetry Formats */3574sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */3575sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */35763577switch (vsc->bpc) {3578case 6:3579/* 6bpc: 0x0 */3580break;3581case 8:3582sdp->db[17] = 0x1; /* DB17[3:0] */3583break;3584case 10:3585sdp->db[17] = 0x2;3586break;3587case 12:3588sdp->db[17] = 0x3;3589break;3590case 16:3591sdp->db[17] = 0x4;3592break;3593default:3594WARN(1, "Missing case %d\n", vsc->bpc);3595return -EINVAL;3596}35973598/* Dynamic Range and Component Bit Depth */3599if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)3600sdp->db[17] |= 0x80; /* DB17[7] */36013602/* Content Type */3603sdp->db[18] = vsc->content_type & 0x7;36043605out:3606return length;3607}3608EXPORT_SYMBOL(drm_dp_vsc_sdp_pack);36093610/**3611* drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON3612* @dpcd: DisplayPort configuration data3613* @port_cap: port capabilities3614*3615* Returns maximum frl bandwidth supported by PCON in GBPS,3616* returns 0 if not supported.3617*/3618int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],3619const u8 port_cap[4])3620{3621int bw;3622u8 buf;36233624buf = port_cap[2];3625bw = buf & DP_PCON_MAX_FRL_BW;36263627switch (bw) {3628case DP_PCON_MAX_9GBPS:3629return 9;3630case DP_PCON_MAX_18GBPS:3631return 18;3632case DP_PCON_MAX_24GBPS:3633return 24;3634case DP_PCON_MAX_32GBPS:3635return 32;3636case DP_PCON_MAX_40GBPS:3637return 40;3638case DP_PCON_MAX_48GBPS:3639return 48;3640case DP_PCON_MAX_0GBPS:3641default:3642return 0;3643}36443645return 0;3646}3647EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);36483649/**3650* drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.3651* @aux: DisplayPort AUX channel3652* @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.3653*3654* Returns 0 if success, else returns negative error code.3655*/3656int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)3657{3658u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |3659DP_PCON_ENABLE_LINK_FRL_MODE;36603661if (enable_frl_ready_hpd)3662buf |= DP_PCON_ENABLE_HPD_READY;36633664return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3665}3666EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);36673668/**3669* drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL3670* @aux: DisplayPort AUX channel3671*3672* Returns true if success, else returns false.3673*/3674bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)3675{3676int ret;3677u8 buf;36783679ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);3680if (ret < 0)3681return false;36823683if (buf & DP_PCON_FRL_READY)3684return true;36853686return false;3687}3688EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);36893690/**3691* drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step13692* @aux: DisplayPort AUX channel3693* @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink3694* @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.3695* In Concurrent Mode, the FRL link bring up can be done along with3696* DP Link training. In Sequential mode, the FRL link bring up is done prior to3697* the DP Link training.3698*3699* Returns 0 if success, else returns negative error code.3700*/37013702int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,3703u8 frl_mode)3704{3705int ret;3706u8 buf;37073708ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);3709if (ret < 0)3710return ret;37113712if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)3713buf |= DP_PCON_ENABLE_CONCURRENT_LINK;3714else3715buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;37163717switch (max_frl_gbps) {3718case 9:3719buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;3720break;3721case 18:3722buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;3723break;3724case 24:3725buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;3726break;3727case 32:3728buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;3729break;3730case 40:3731buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;3732break;3733case 48:3734buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;3735break;3736case 0:3737buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;3738break;3739default:3740return -EINVAL;3741}37423743return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3744}3745EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);37463747/**3748* drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-23749* @aux: DisplayPort AUX channel3750* @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink3751* @frl_type : FRL training type, can be Extended, or Normal.3752* In Normal FRL training, the PCON tries each frl bw from the max_frl_mask3753* starting from min, and stops when link training is successful. In Extended3754* FRL training, all frl bw selected in the mask are trained by the PCON.3755*3756* Returns 0 if success, else returns negative error code.3757*/3758int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,3759u8 frl_type)3760{3761int ret;3762u8 buf = max_frl_mask;37633764if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)3765buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;3766else3767buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;37683769return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);3770if (ret < 0)3771return ret;37723773return 0;3774}3775EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);37763777/**3778* drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.3779* @aux: DisplayPort AUX channel3780*3781* Returns 0 if success, else returns negative error code.3782*/3783int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)3784{3785return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);3786}3787EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);37883789/**3790* drm_dp_pcon_frl_enable() - Enable HDMI link through FRL3791* @aux: DisplayPort AUX channel3792*3793* Returns 0 if success, else returns negative error code.3794*/3795int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)3796{3797int ret;3798u8 buf = 0;37993800ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);3801if (ret < 0)3802return ret;3803if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {3804drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",3805aux->name);3806return -EINVAL;3807}3808buf |= DP_PCON_ENABLE_HDMI_LINK;3809return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3810}3811EXPORT_SYMBOL(drm_dp_pcon_frl_enable);38123813/**3814* drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.3815* @aux: DisplayPort AUX channel3816*3817* Returns true if link is active else returns false.3818*/3819bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)3820{3821u8 buf;3822int ret;38233824ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);3825if (ret < 0)3826return false;38273828return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;3829}3830EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);38313832/**3833* drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE3834* @aux: DisplayPort AUX channel3835* @frl_trained_mask: pointer to store bitmask of the trained bw configuration.3836* Valid only if the MODE returned is FRL. For Normal Link training mode3837* only 1 of the bits will be set, but in case of Extended mode, more than3838* one bits can be set.3839*3840* Returns the link mode : TMDS or FRL on success, else returns negative error3841* code.3842*/3843int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)3844{3845u8 buf;3846int mode;3847int ret;38483849ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);3850if (ret < 0)3851return ret;38523853mode = buf & DP_PCON_HDMI_LINK_MODE;38543855if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)3856*frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;38573858return mode;3859}3860EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);38613862/**3863* drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane3864* during link failure between PCON and HDMI sink3865* @aux: DisplayPort AUX channel3866* @connector: DRM connector3867* code.3868**/38693870void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,3871struct drm_connector *connector)3872{3873u8 buf, error_count;3874int i, num_error;3875struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;38763877for (i = 0; i < hdmi->max_lanes; i++) {3878if (drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)3879return;38803881error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;3882switch (error_count) {3883case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:3884num_error = 100;3885break;3886case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:3887num_error = 10;3888break;3889case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:3890num_error = 3;3891break;3892default:3893num_error = 0;3894}38953896drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",3897aux->name, num_error, i);3898}3899}3900EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);39013902/*3903* drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.23904* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3905*3906* Returns true is PCON encoder is DSC 1.2 else returns false.3907*/3908bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3909{3910u8 buf;3911u8 major_v, minor_v;39123913buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];3914major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;3915minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;39163917if (major_v == 1 && minor_v == 2)3918return true;39193920return false;3921}3922EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);39233924/*3925* drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder3926* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3927*3928* Returns maximum no. of slices supported by the PCON DSC Encoder.3929*/3930int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3931{3932u8 slice_cap1, slice_cap2;39333934slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];3935slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];39363937if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)3938return 24;3939if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)3940return 20;3941if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)3942return 16;3943if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)3944return 12;3945if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)3946return 10;3947if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)3948return 8;3949if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)3950return 6;3951if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)3952return 4;3953if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)3954return 2;3955if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)3956return 1;39573958return 0;3959}3960EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);39613962/*3963* drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder3964* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3965*3966* Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.3967*/3968int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3969{3970u8 buf;39713972buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];39733974return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;3975}3976EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);39773978/*3979* drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder3980* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3981*3982* Returns the bpp precision supported by the PCON encoder.3983*/3984int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3985{3986u8 buf;39873988buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];39893990switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {3991case DP_PCON_DSC_ONE_16TH_BPP:3992return 16;3993case DP_PCON_DSC_ONE_8TH_BPP:3994return 8;3995case DP_PCON_DSC_ONE_4TH_BPP:3996return 4;3997case DP_PCON_DSC_ONE_HALF_BPP:3998return 2;3999case DP_PCON_DSC_ONE_BPP:4000return 1;4001}40024003return 0;4004}4005EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);40064007static4008int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)4009{4010u8 buf;4011int ret;40124013ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);4014if (ret < 0)4015return ret;40164017buf |= DP_PCON_ENABLE_DSC_ENCODER;40184019if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {4020buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;4021buf |= pps_buf_config << 2;4022}40234024return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);4025}40264027/**4028* drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters4029* for DSC1.2 between PCON & HDMI2.1 sink4030* @aux: DisplayPort AUX channel4031*4032* Returns 0 on success, else returns negative error code.4033*/4034int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)4035{4036return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);4037}4038EXPORT_SYMBOL(drm_dp_pcon_pps_default);40394040/**4041* drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for4042* HDMI sink4043* @aux: DisplayPort AUX channel4044* @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.4045*4046* Returns 0 on success, else returns negative error code.4047*/4048int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])4049{4050int ret;40514052ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);4053if (ret < 0)4054return ret;40554056return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);4057}4058EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);40594060/*4061* drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder4062* override registers4063* @aux: DisplayPort AUX channel4064* @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,4065* bits_per_pixel.4066*4067* Returns 0 on success, else returns negative error code.4068*/4069int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])4070{4071int ret;40724073ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);4074if (ret < 0)4075return ret;4076ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);4077if (ret < 0)4078return ret;4079ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);4080if (ret < 0)4081return ret;40824083return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);4084}4085EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);40864087/*4088* drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr4089* @aux: displayPort AUX channel4090* @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.4091*4092* Returns 0 on success, else returns negative error code.4093*/4094int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)4095{4096int ret;4097u8 buf;40984099ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);4100if (ret < 0)4101return ret;41024103if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)4104buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);4105else4106buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;41074108return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);4109}4110EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);41114112/**4113* drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX4114* @aux: The DP AUX channel to use4115* @bl: Backlight capability info from drm_edp_backlight_init()4116* @level: The brightness level to set4117*4118* Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must4119* already have been enabled by the driver by calling drm_edp_backlight_enable().4120*4121* Returns: %0 on success, negative error code on failure4122*/4123int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4124u32 level)4125{4126int ret;4127unsigned int offset = DP_EDP_BACKLIGHT_BRIGHTNESS_MSB;4128u8 buf[3] = { 0 };4129size_t len = 2;41304131/* The panel uses the PWM for controlling brightness levels */4132if (!(bl->aux_set || bl->luminance_set))4133return 0;41344135if (bl->luminance_set) {4136level = level * 1000;4137level &= 0xffffff;4138buf[0] = (level & 0x0000ff);4139buf[1] = (level & 0x00ff00) >> 8;4140buf[2] = (level & 0xff0000) >> 16;4141offset = DP_EDP_PANEL_TARGET_LUMINANCE_VALUE;4142len = 3;4143} else if (bl->lsb_reg_used) {4144buf[0] = (level & 0xff00) >> 8;4145buf[1] = (level & 0x00ff);4146} else {4147buf[0] = level;4148}41494150ret = drm_dp_dpcd_write_data(aux, offset, buf, len);4151if (ret < 0) {4152drm_err(aux->drm_dev,4153"%s: Failed to write aux backlight level: %d\n",4154aux->name, ret);4155return ret;4156}41574158return 0;4159}4160EXPORT_SYMBOL(drm_edp_backlight_set_level);41614162static int4163drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4164bool enable)4165{4166int ret;4167u8 buf;41684169/* This panel uses the EDP_BL_PWR GPIO for enablement */4170if (!bl->aux_enable)4171return 0;41724173ret = drm_dp_dpcd_read_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);4174if (ret < 0) {4175drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",4176aux->name, ret);4177return ret;4178}4179if (enable)4180buf |= DP_EDP_BACKLIGHT_ENABLE;4181else4182buf &= ~DP_EDP_BACKLIGHT_ENABLE;41834184ret = drm_dp_dpcd_write_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);4185if (ret < 0) {4186drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",4187aux->name, ret);4188return ret;4189}41904191return 0;4192}41934194/**4195* drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD4196* @aux: The DP AUX channel to use4197* @bl: Backlight capability info from drm_edp_backlight_init()4198* @level: The initial backlight level to set via AUX, if there is one4199*4200* This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally4201* restoring any important backlight state such as the given backlight level, the brightness byte4202* count, backlight frequency, etc.4203*4204* Note that certain panels do not support being enabled or disabled via DPCD, but instead require4205* that the driver handle enabling/disabling the panel through implementation-specific means using4206* the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,4207* this function becomes a no-op, and the driver is expected to handle powering the panel on using4208* the EDP_BL_PWR GPIO.4209*4210* Returns: %0 on success, negative error code on failure.4211*/4212int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4213const u32 level)4214{4215int ret;4216u8 dpcd_buf;42174218if (bl->aux_set)4219dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;4220else4221dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_PWM;42224223if (bl->luminance_set)4224dpcd_buf |= DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE;42254226if (bl->pwmgen_bit_count) {4227ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);4228if (ret < 0)4229drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",4230aux->name, ret);4231}42324233if (bl->pwm_freq_pre_divider) {4234ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_FREQ_SET,4235bl->pwm_freq_pre_divider);4236if (ret < 0)4237drm_dbg_kms(aux->drm_dev,4238"%s: Failed to write aux backlight frequency: %d\n",4239aux->name, ret);4240else4241dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;4242}42434244ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);4245if (ret < 0) {4246drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",4247aux->name, ret);4248return ret < 0 ? ret : -EIO;4249}42504251ret = drm_edp_backlight_set_level(aux, bl, level);4252if (ret < 0)4253return ret;4254ret = drm_edp_backlight_set_enable(aux, bl, true);4255if (ret < 0)4256return ret;42574258return 0;4259}4260EXPORT_SYMBOL(drm_edp_backlight_enable);42614262/**4263* drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported4264* @aux: The DP AUX channel to use4265* @bl: Backlight capability info from drm_edp_backlight_init()4266*4267* This function handles disabling DPCD backlight controls on a panel over AUX.4268*4269* Note that certain panels do not support being enabled or disabled via DPCD, but instead require4270* that the driver handle enabling/disabling the panel through implementation-specific means using4271* the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,4272* this function becomes a no-op, and the driver is expected to handle powering the panel off using4273* the EDP_BL_PWR GPIO.4274*4275* Returns: %0 on success or no-op, negative error code on failure.4276*/4277int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)4278{4279int ret;42804281ret = drm_edp_backlight_set_enable(aux, bl, false);4282if (ret < 0)4283return ret;42844285return 0;4286}4287EXPORT_SYMBOL(drm_edp_backlight_disable);42884289static inline int4290drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4291u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])4292{4293int fxp, fxp_min, fxp_max, fxp_actual, f = 1;4294int ret;4295u8 pn, pn_min, pn_max, bit_count;42964297if (!bl->aux_set)4298return 0;42994300ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, &bit_count);4301if (ret < 0) {4302drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",4303aux->name, ret);4304return -ENODEV;4305}43064307bit_count &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43084309ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);4310if (ret < 0) {4311drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",4312aux->name, ret);4313return -ENODEV;4314}4315pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43164317ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);4318if (ret < 0) {4319drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",4320aux->name, ret);4321return -ENODEV;4322}4323pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43244325if (unlikely(pn_min > pn_max)) {4326drm_dbg_kms(aux->drm_dev, "%s: Invalid pwmgen bit count cap min/max returned: %d %d\n",4327aux->name, pn_min, pn_max);4328return -EINVAL;4329}43304331/*4332* Per VESA eDP Spec v1.4b, section 3.3.10.2:4333* If DP_EDP_PWMGEN_BIT_COUNT is less than DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN,4334* the sink must use the MIN value as the effective PWM bit count.4335* Clamp the reported value to the [MIN, MAX] capability range to ensure4336* correct brightness scaling on compliant eDP panels.4337* Only enable this logic if the [MIN, MAX] range is valid in regard to Spec.4338*/4339pn = bit_count;4340if (bit_count < pn_min)4341pn = clamp(bit_count, pn_min, pn_max);43424343bl->max = (1 << pn) - 1;4344if (!driver_pwm_freq_hz) {4345if (pn != bit_count)4346goto bit_count_write_back;43474348return 0;4349}43504351/*4352* Set PWM Frequency divider to match desired frequency provided by the driver.4353* The PWM Frequency is calculated as 27Mhz / (F x P).4354* - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the4355* EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)4356* - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the4357* EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)4358*/43594360/* Find desired value of (F x P)4361* Note that, if F x P is out of supported range, the maximum value or minimum value will4362* applied automatically. So no need to check that.4363*/4364fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);43654366/* Use highest possible value of Pn for more granularity of brightness adjustment while4367* satisfying the conditions below.4368* - Pn is in the range of Pn_min and Pn_max4369* - F is in the range of 1 and 2554370* - FxP is within 25% of desired value.4371* Note: 25% is arbitrary value and may need some tweak.4372*/4373/* Ensure frequency is within 25% of desired value */4374fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);4375fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);4376if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {4377drm_dbg_kms(aux->drm_dev,4378"%s: Driver defined backlight frequency (%d) out of range\n",4379aux->name, driver_pwm_freq_hz);4380return 0;4381}43824383for (pn = pn_max; pn >= pn_min; pn--) {4384f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);4385fxp_actual = f << pn;4386if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)4387break;4388}43894390bit_count_write_back:4391ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);4392if (ret < 0) {4393drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",4394aux->name, ret);4395return 0;4396}43974398if (!driver_pwm_freq_hz)4399return 0;44004401bl->pwmgen_bit_count = pn;4402bl->max = (1 << pn) - 1;44034404if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {4405bl->pwm_freq_pre_divider = f;4406drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",4407aux->name, driver_pwm_freq_hz);4408}44094410return 0;4411}44124413static inline int4414drm_edp_backlight_probe_state(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4415u8 *current_mode)4416{4417int ret;4418u8 buf[3];4419u8 mode_reg;44204421ret = drm_dp_dpcd_read_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);4422if (ret < 0) {4423drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",4424aux->name, ret);4425return ret < 0 ? ret : -EIO;4426}44274428*current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);4429if (!bl->aux_set)4430return 0;44314432if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {4433int size = 1 + bl->lsb_reg_used;44344435if (bl->luminance_set) {4436ret = drm_dp_dpcd_read_data(aux, DP_EDP_PANEL_TARGET_LUMINANCE_VALUE,4437buf, sizeof(buf));4438if (ret < 0) {4439drm_dbg_kms(aux->drm_dev,4440"%s: Failed to read backlight level: %d\n",4441aux->name, ret);4442return ret;4443}44444445/*4446* Incase luminance is set we want to send the value back in nits but4447* since DP_EDP_PANEL_TARGET_LUMINANCE stores values in millinits we4448* need to divide by 1000.4449*/4450return (buf[0] | buf[1] << 8 | buf[2] << 16) / 1000;4451} else {4452ret = drm_dp_dpcd_read_data(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,4453buf, size);4454if (ret < 0) {4455drm_dbg_kms(aux->drm_dev,4456"%s: Failed to read backlight level: %d\n",4457aux->name, ret);4458return ret;4459}44604461if (bl->lsb_reg_used)4462return (buf[0] << 8) | buf[1];4463else4464return buf[0];4465}4466}44674468/*4469* If we're not in DPCD control mode yet, the programmed brightness value is meaningless and4470* the driver should assume max brightness4471*/4472return bl->max;4473}44744475/**4476* drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight4477* interface.4478* @aux: The DP aux device to use for probing4479* @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight4480* @max_luminance: max luminance when need luminance is set as true4481* @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz4482* @edp_dpcd: A cached copy of the eDP DPCD4483* @current_level: Where to store the probed brightness level, if any4484* @current_mode: Where to store the currently set backlight control mode4485* @need_luminance: Tells us if a we want to manipulate backlight using luminance values4486*4487* Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,4488* along with also probing the current and maximum supported brightness levels.4489*4490* If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the4491* default frequency from the panel is used.4492*4493* Returns: %0 on success, negative error code on failure.4494*/4495int4496drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4497u32 max_luminance,4498u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],4499u32 *current_level, u8 *current_mode, bool need_luminance)4500{4501int ret;45024503if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)4504bl->aux_enable = true;4505if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)4506bl->aux_set = true;4507if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)4508bl->lsb_reg_used = true;4509if ((edp_dpcd[0] & DP_EDP_15) && edp_dpcd[3] &4510(DP_EDP_PANEL_LUMINANCE_CONTROL_CAPABLE) && need_luminance)4511bl->luminance_set = true;45124513/* Sanity check caps */4514if (!bl->aux_set && !(edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP) &&4515!bl->luminance_set) {4516drm_dbg_kms(aux->drm_dev,4517"%s: Panel does not support AUX, PWM or luminance-based brightness control. Aborting\n",4518aux->name);4519return -EINVAL;4520}45214522if (bl->luminance_set) {4523bl->max = max_luminance;4524} else {4525ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);4526if (ret < 0)4527return ret;4528}45294530ret = drm_edp_backlight_probe_state(aux, bl, current_mode);4531if (ret < 0)4532return ret;4533*current_level = ret;45344535drm_dbg_kms(aux->drm_dev,4536"%s: Found backlight: aux_set=%d aux_enable=%d mode=%d\n",4537aux->name, bl->aux_set, bl->aux_enable, *current_mode);4538if (bl->aux_set) {4539drm_dbg_kms(aux->drm_dev,4540"%s: Backlight caps: level=%d/%d pwm_freq_pre_divider=%d lsb_reg_used=%d\n",4541aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider,4542bl->lsb_reg_used);4543}45444545return 0;4546}4547EXPORT_SYMBOL(drm_edp_backlight_init);45484549#if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \4550(IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))45514552static int dp_aux_backlight_update_status(struct backlight_device *bd)4553{4554struct dp_aux_backlight *bl = bl_get_data(bd);4555u16 brightness = backlight_get_brightness(bd);4556int ret = 0;45574558if (!backlight_is_blank(bd)) {4559if (!bl->enabled) {4560drm_edp_backlight_enable(bl->aux, &bl->info, brightness);4561bl->enabled = true;4562return 0;4563}4564ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);4565} else {4566if (bl->enabled) {4567drm_edp_backlight_disable(bl->aux, &bl->info);4568bl->enabled = false;4569}4570}45714572return ret;4573}45744575static const struct backlight_ops dp_aux_bl_ops = {4576.update_status = dp_aux_backlight_update_status,4577};45784579/**4580* drm_panel_dp_aux_backlight - create and use DP AUX backlight4581* @panel: DRM panel4582* @aux: The DP AUX channel to use4583*4584* Use this function to create and handle backlight if your panel4585* supports backlight control over DP AUX channel using DPCD4586* registers as per VESA's standard backlight control interface.4587*4588* When the panel is enabled backlight will be enabled after a4589* successful call to &drm_panel_funcs.enable()4590*4591* When the panel is disabled backlight will be disabled before the4592* call to &drm_panel_funcs.disable().4593*4594* A typical implementation for a panel driver supporting backlight4595* control over DP AUX will call this function at probe time.4596* Backlight will then be handled transparently without requiring4597* any intervention from the driver.4598*4599* drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().4600*4601* Return: 0 on success or a negative error code on failure.4602*/4603int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)4604{4605struct dp_aux_backlight *bl;4606struct backlight_properties props = { 0 };4607u32 current_level;4608u8 current_mode;4609u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];4610int ret;46114612if (!panel || !panel->dev || !aux)4613return -EINVAL;46144615ret = drm_dp_dpcd_read_data(aux, DP_EDP_DPCD_REV, edp_dpcd,4616EDP_DISPLAY_CTL_CAP_SIZE);4617if (ret < 0)4618return ret;46194620if (!drm_edp_backlight_supported(edp_dpcd)) {4621DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");4622return 0;4623}46244625bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);4626if (!bl)4627return -ENOMEM;46284629bl->aux = aux;46304631ret = drm_edp_backlight_init(aux, &bl->info, 0, 0, edp_dpcd,4632¤t_level, ¤t_mode, false);4633if (ret < 0)4634return ret;46354636props.type = BACKLIGHT_RAW;4637props.brightness = current_level;4638props.max_brightness = bl->info.max;46394640bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",4641panel->dev, bl,4642&dp_aux_bl_ops, &props);4643if (IS_ERR(bl->base))4644return PTR_ERR(bl->base);46454646backlight_disable(bl->base);46474648panel->backlight = bl->base;46494650return 0;4651}4652EXPORT_SYMBOL(drm_panel_dp_aux_backlight);46534654#endif46554656/* See DP Standard v2.1 2.6.4.4.1.1, 2.8.4.4, 2.8.7 */4657static int drm_dp_link_data_symbol_cycles(int lane_count, int pixels,4658int bpp_x16, int symbol_size,4659bool is_mst)4660{4661int cycles = DIV_ROUND_UP(pixels * bpp_x16, 16 * symbol_size * lane_count);4662int align = is_mst ? 4 / lane_count : 1;46634664return ALIGN(cycles, align);4665}46664667/**4668* drm_dp_link_symbol_cycles - calculate the link symbol count with/without dsc4669* @lane_count: DP link lane count4670* @pixels: number of pixels in a scanline4671* @dsc_slice_count: number of slices for DSC or '0' for non-DSC4672* @bpp_x16: bits per pixel in .4 binary fixed format4673* @symbol_size: DP symbol size4674* @is_mst: %true for MST and %false for SST4675*4676* Calculate the link symbol cycles for both DSC (@dsc_slice_count !=0) and4677* non-DSC case (@dsc_slice_count == 0) and return the count.4678*/4679int drm_dp_link_symbol_cycles(int lane_count, int pixels, int dsc_slice_count,4680int bpp_x16, int symbol_size, bool is_mst)4681{4682int slice_count = dsc_slice_count ? : 1;4683int slice_pixels = DIV_ROUND_UP(pixels, slice_count);4684int slice_data_cycles = drm_dp_link_data_symbol_cycles(lane_count,4685slice_pixels,4686bpp_x16,4687symbol_size,4688is_mst);4689int slice_eoc_cycles = 0;46904691if (dsc_slice_count)4692slice_eoc_cycles = is_mst ? 4 / lane_count : 1;46934694return slice_count * (slice_data_cycles + slice_eoc_cycles);4695}4696EXPORT_SYMBOL(drm_dp_link_symbol_cycles);46974698/**4699* drm_dp_bw_overhead - Calculate the BW overhead of a DP link stream4700* @lane_count: DP link lane count4701* @hactive: pixel count of the active period in one scanline of the stream4702* @dsc_slice_count: number of slices for DSC or '0' for non-DSC4703* @bpp_x16: bits per pixel in .4 binary fixed point4704* @flags: DRM_DP_OVERHEAD_x flags4705*4706* Calculate the BW allocation overhead of a DP link stream, depending4707* on the link's4708* - @lane_count4709* - SST/MST mode (@flags / %DRM_DP_OVERHEAD_MST)4710* - symbol size (@flags / %DRM_DP_OVERHEAD_UHBR)4711* - FEC mode (@flags / %DRM_DP_OVERHEAD_FEC)4712* - SSC/REF_CLK mode (@flags / %DRM_DP_OVERHEAD_SSC_REF_CLK)4713* as well as the stream's4714* - @hactive timing4715* - @bpp_x16 color depth4716* - compression mode (@dsc_slice_count != 0)4717* Note that this overhead doesn't account for the 8b/10b, 128b/132b4718* channel coding efficiency, for that see4719* @drm_dp_link_bw_channel_coding_efficiency().4720*4721* Returns the overhead as 100% + overhead% in 1ppm units.4722*/4723int drm_dp_bw_overhead(int lane_count, int hactive,4724int dsc_slice_count,4725int bpp_x16, unsigned long flags)4726{4727int symbol_size = flags & DRM_DP_BW_OVERHEAD_UHBR ? 32 : 8;4728bool is_mst = flags & DRM_DP_BW_OVERHEAD_MST;4729u32 overhead = 1000000;4730int symbol_cycles;47314732if (lane_count == 0 || hactive == 0 || bpp_x16 == 0) {4733DRM_DEBUG_KMS("Invalid BW overhead params: lane_count %d, hactive %d, bpp_x16 " FXP_Q4_FMT "\n",4734lane_count, hactive,4735FXP_Q4_ARGS(bpp_x16));4736return 0;4737}47384739/*4740* DP Standard v2.1 2.6.4.14741* SSC downspread and ref clock variation margin:4742* 5300ppm + 300ppm ~ 0.6%4743*/4744if (flags & DRM_DP_BW_OVERHEAD_SSC_REF_CLK)4745overhead += 6000;47464747/*4748* DP Standard v2.1 2.6.4.1.1, 3.5.1.5.4:4749* FEC symbol insertions for 8b/10b channel coding:4750* After each 250 data symbols on 2-4 lanes:4751* 250 LL + 5 FEC_PARITY_PH + 1 CD_ADJ (256 byte FEC block)4752* After each 2 x 250 data symbols on 1 lane:4753* 2 * 250 LL + 11 FEC_PARITY_PH + 1 CD_ADJ (512 byte FEC block)4754* After 256 (2-4 lanes) or 128 (1 lane) FEC blocks:4755* 256 * 256 bytes + 1 FEC_PM4756* or4757* 128 * 512 bytes + 1 FEC_PM4758* (256 * 6 + 1) / (256 * 250) = 2.4015625 %4759*/4760if (flags & DRM_DP_BW_OVERHEAD_FEC)4761overhead += 24016;47624763/*4764* DP Standard v2.1 2.7.9, 5.9.74765* The FEC overhead for UHBR is accounted for in its 96.71% channel4766* coding efficiency.4767*/4768WARN_ON((flags & DRM_DP_BW_OVERHEAD_UHBR) &&4769(flags & DRM_DP_BW_OVERHEAD_FEC));47704771symbol_cycles = drm_dp_link_symbol_cycles(lane_count, hactive,4772dsc_slice_count,4773bpp_x16, symbol_size,4774is_mst);47754776return DIV_ROUND_UP_ULL(mul_u32_u32(symbol_cycles * symbol_size * lane_count,4777overhead * 16),4778hactive * bpp_x16);4779}4780EXPORT_SYMBOL(drm_dp_bw_overhead);47814782/**4783* drm_dp_bw_channel_coding_efficiency - Get a DP link's channel coding efficiency4784* @is_uhbr: Whether the link has a 128b/132b channel coding4785*4786* Return the channel coding efficiency of the given DP link type, which is4787* either 8b/10b or 128b/132b (aka UHBR). The corresponding overhead includes4788* the 8b -> 10b, 128b -> 132b pixel data to link symbol conversion overhead4789* and for 128b/132b any link or PHY level control symbol insertion overhead4790* (LLCP, FEC, PHY sync, see DP Standard v2.1 3.5.2.18). For 8b/10b the4791* corresponding FEC overhead is BW allocation specific, included in the value4792* returned by drm_dp_bw_overhead().4793*4794* Returns the efficiency in the 100%/coding-overhead% ratio in4795* 1ppm units.4796*/4797int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)4798{4799if (is_uhbr)4800return 967100;4801else4802/*4803* Note that on 8b/10b MST the efficiency is only4804* 78.75% due to the 1 out of 64 MTPH packet overhead,4805* not accounted for here.4806*/4807return 800000;4808}4809EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);48104811/**4812* drm_dp_max_dprx_data_rate - Get the max data bandwidth of a DPRX sink4813* @max_link_rate: max DPRX link rate in 10kbps units4814* @max_lanes: max DPRX lane count4815*4816* Given a link rate and lanes, get the data bandwidth.4817*4818* Data bandwidth is the actual payload rate, which depends on the data4819* bandwidth efficiency and the link rate.4820*4821* Note that protocol layers above the DPRX link level considered here can4822* further limit the maximum data rate. Such layers are the MST topology (with4823* limits on the link between the source and first branch device as well as on4824* the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -4825* which in turn can encapsulate an MST link with its own limit - with each4826* SST or MST encapsulated tunnel sharing the BW of a tunnel group.4827*4828* Returns the maximum data rate in kBps units.4829*/4830int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)4831{4832int ch_coding_efficiency =4833drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));48344835return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate * 10 * max_lanes,4836ch_coding_efficiency),48371000000 * 8);4838}4839EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);484048414842