Path: blob/master/drivers/gpu/drm/display/drm_dp_helper.c
51907 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_slice_count_to_mask() - Convert a slice count to a slice count mask2708* @slice_count: slice count2709*2710* Convert @slice_count to a slice count mask.2711*2712* Returns the slice count mask.2713*/2714u32 drm_dp_dsc_slice_count_to_mask(int slice_count)2715{2716return BIT(slice_count - 1);2717}2718EXPORT_SYMBOL(drm_dp_dsc_slice_count_to_mask);27192720/**2721* drm_dp_dsc_sink_slice_count_mask() - Get the mask of valid DSC sink slice counts2722* @dsc_dpcd: the sink's DSC DPCD capabilities2723* @is_edp: %true for an eDP sink2724*2725* Get the mask of supported slice counts from the sink's DSC DPCD register.2726*2727* Returns:2728* Mask of slice counts supported by the DSC sink:2729* - > 0: bit#0,1,3,5..,23 set if the sink supports 1,2,4,6..,24 slices2730* - 0: if the sink doesn't support any slices2731*/2732u32 drm_dp_dsc_sink_slice_count_mask(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2733bool is_edp)2734{2735u8 slice_cap1 = dsc_dpcd[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];2736u32 mask = 0;27372738if (!is_edp) {2739/* For DP, use values from DSC_SLICE_CAP_1 and DSC_SLICE_CAP2 */2740u8 slice_cap2 = dsc_dpcd[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];27412742if (slice_cap2 & DP_DSC_24_PER_DP_DSC_SINK)2743mask |= drm_dp_dsc_slice_count_to_mask(24);2744if (slice_cap2 & DP_DSC_20_PER_DP_DSC_SINK)2745mask |= drm_dp_dsc_slice_count_to_mask(20);2746if (slice_cap2 & DP_DSC_16_PER_DP_DSC_SINK)2747mask |= drm_dp_dsc_slice_count_to_mask(16);2748}27492750/* DP, eDP v1.5+ */2751if (slice_cap1 & DP_DSC_12_PER_DP_DSC_SINK)2752mask |= drm_dp_dsc_slice_count_to_mask(12);2753if (slice_cap1 & DP_DSC_10_PER_DP_DSC_SINK)2754mask |= drm_dp_dsc_slice_count_to_mask(10);2755if (slice_cap1 & DP_DSC_8_PER_DP_DSC_SINK)2756mask |= drm_dp_dsc_slice_count_to_mask(8);2757if (slice_cap1 & DP_DSC_6_PER_DP_DSC_SINK)2758mask |= drm_dp_dsc_slice_count_to_mask(6);2759/* DP, eDP v1.4+ */2760if (slice_cap1 & DP_DSC_4_PER_DP_DSC_SINK)2761mask |= drm_dp_dsc_slice_count_to_mask(4);2762if (slice_cap1 & DP_DSC_2_PER_DP_DSC_SINK)2763mask |= drm_dp_dsc_slice_count_to_mask(2);2764if (slice_cap1 & DP_DSC_1_PER_DP_DSC_SINK)2765mask |= drm_dp_dsc_slice_count_to_mask(1);27662767return mask;2768}2769EXPORT_SYMBOL(drm_dp_dsc_sink_slice_count_mask);27702771/**2772* drm_dp_dsc_sink_max_slice_count() - Get the max slice count2773* supported by the DSC sink.2774* @dsc_dpcd: DSC capabilities from DPCD2775* @is_edp: true if its eDP, false for DP2776*2777* Read the slice capabilities DPCD register from DSC sink to get2778* the maximum slice count supported. This is used to populate2779* the DSC parameters in the &struct drm_dsc_config by the driver.2780* Driver creates an infoframe using these parameters to populate2781* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2782* infoframe using the helper function drm_dsc_pps_infoframe_pack()2783*2784* Returns:2785* Maximum slice count supported by DSC sink or 0 its invalid2786*/2787u8 drm_dp_dsc_sink_max_slice_count(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2788bool is_edp)2789{2790return fls(drm_dp_dsc_sink_slice_count_mask(dsc_dpcd, is_edp));2791}2792EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_count);27932794/**2795* drm_dp_dsc_sink_line_buf_depth() - Get the line buffer depth in bits2796* @dsc_dpcd: DSC capabilities from DPCD2797*2798* Read the DSC DPCD register to parse the line buffer depth in bits which is2799* number of bits of precision within the decoder line buffer supported by2800* the DSC sink. This is used to populate the DSC parameters in the2801* &struct drm_dsc_config by the driver.2802* Driver creates an infoframe using these parameters to populate2803* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2804* infoframe using the helper function drm_dsc_pps_infoframe_pack()2805*2806* Returns:2807* Line buffer depth supported by DSC panel or 0 its invalid2808*/2809u8 drm_dp_dsc_sink_line_buf_depth(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE])2810{2811u8 line_buf_depth = dsc_dpcd[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT];28122813switch (line_buf_depth & DP_DSC_LINE_BUF_BIT_DEPTH_MASK) {2814case DP_DSC_LINE_BUF_BIT_DEPTH_9:2815return 9;2816case DP_DSC_LINE_BUF_BIT_DEPTH_10:2817return 10;2818case DP_DSC_LINE_BUF_BIT_DEPTH_11:2819return 11;2820case DP_DSC_LINE_BUF_BIT_DEPTH_12:2821return 12;2822case DP_DSC_LINE_BUF_BIT_DEPTH_13:2823return 13;2824case DP_DSC_LINE_BUF_BIT_DEPTH_14:2825return 14;2826case DP_DSC_LINE_BUF_BIT_DEPTH_15:2827return 15;2828case DP_DSC_LINE_BUF_BIT_DEPTH_16:2829return 16;2830case DP_DSC_LINE_BUF_BIT_DEPTH_8:2831return 8;2832}28332834return 0;2835}2836EXPORT_SYMBOL(drm_dp_dsc_sink_line_buf_depth);28372838/**2839* drm_dp_dsc_sink_supported_input_bpcs() - Get all the input bits per component2840* values supported by the DSC sink.2841* @dsc_dpcd: DSC capabilities from DPCD2842* @dsc_bpc: An array to be filled by this helper with supported2843* input bpcs.2844*2845* Read the DSC DPCD from the sink device to parse the supported bits per2846* component values. This is used to populate the DSC parameters2847* in the &struct drm_dsc_config by the driver.2848* Driver creates an infoframe using these parameters to populate2849* &struct drm_dsc_pps_infoframe. These are sent to the sink using DSC2850* infoframe using the helper function drm_dsc_pps_infoframe_pack()2851*2852* Returns:2853* Number of input BPC values parsed from the DPCD2854*/2855int drm_dp_dsc_sink_supported_input_bpcs(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2856u8 dsc_bpc[3])2857{2858int num_bpc = 0;2859u8 color_depth = dsc_dpcd[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];28602861if (!drm_dp_sink_supports_dsc(dsc_dpcd))2862return 0;28632864if (color_depth & DP_DSC_12_BPC)2865dsc_bpc[num_bpc++] = 12;2866if (color_depth & DP_DSC_10_BPC)2867dsc_bpc[num_bpc++] = 10;28682869/* A DP DSC Sink device shall support 8 bpc. */2870dsc_bpc[num_bpc++] = 8;28712872return num_bpc;2873}2874EXPORT_SYMBOL(drm_dp_dsc_sink_supported_input_bpcs);28752876/*2877* See DP Standard v2.1a 2.8.4 Minimum Slices/Display, Table 2-159 and2878* Appendix L.1 Derivation of Slice Count Requirements.2879*/2880static int dsc_sink_min_slice_throughput(int peak_pixel_rate)2881{2882if (peak_pixel_rate >= 4800000)2883return 600000;2884else if (peak_pixel_rate >= 2700000)2885return 400000;2886else2887return 340000;2888}28892890/**2891* drm_dp_dsc_sink_max_slice_throughput() - Get a DSC sink's maximum pixel throughput per slice2892* @dsc_dpcd: DSC sink's capabilities from DPCD2893* @peak_pixel_rate: Cumulative peak pixel rate in kHz2894* @is_rgb_yuv444: The mode is either RGB or YUV4442895*2896* Return the DSC sink device's maximum pixel throughput per slice, based on2897* the device's @dsc_dpcd capabilities, the @peak_pixel_rate of the transferred2898* stream(s) and whether the output format @is_rgb_yuv444 or yuv422/yuv420.2899*2900* Note that @peak_pixel_rate is the total pixel rate transferred to the same2901* DSC/display sink. For instance to calculate a tile's slice count of an MST2902* multi-tiled display sink (not considering here the required2903* rounding/alignment of slice count)::2904*2905* @peak_pixel_rate = tile_pixel_rate * tile_count2906* total_slice_count = @peak_pixel_rate / drm_dp_dsc_sink_max_slice_throughput(@peak_pixel_rate)2907* tile_slice_count = total_slice_count / tile_count2908*2909* Returns:2910* The maximum pixel throughput per slice supported by the DSC sink device2911* in kPixels/sec.2912*/2913int drm_dp_dsc_sink_max_slice_throughput(const u8 dsc_dpcd[DP_DSC_RECEIVER_CAP_SIZE],2914int peak_pixel_rate, bool is_rgb_yuv444)2915{2916int throughput;2917int delta = 0;2918int base;29192920throughput = dsc_dpcd[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];29212922if (is_rgb_yuv444) {2923throughput = (throughput & DP_DSC_THROUGHPUT_MODE_0_MASK) >>2924DP_DSC_THROUGHPUT_MODE_0_SHIFT;29252926delta = ((dsc_dpcd[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT]) &2927DP_DSC_THROUGHPUT_MODE_0_DELTA_MASK) >>2928DP_DSC_THROUGHPUT_MODE_0_DELTA_SHIFT; /* in units of 2 MPixels/sec */2929delta *= 2000;2930} else {2931throughput = (throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >>2932DP_DSC_THROUGHPUT_MODE_1_SHIFT;2933}29342935switch (throughput) {2936case 0:2937return dsc_sink_min_slice_throughput(peak_pixel_rate);2938case 1:2939base = 340000;2940break;2941case 2 ... 14:2942base = 400000 + 50000 * (throughput - 2);2943break;2944case 15:2945base = 170000;2946break;2947}29482949return base + delta;2950}2951EXPORT_SYMBOL(drm_dp_dsc_sink_max_slice_throughput);29522953static u8 dsc_branch_dpcd_cap(const u8 dpcd[DP_DSC_BRANCH_CAP_SIZE], int reg)2954{2955return dpcd[reg - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];2956}29572958/**2959* drm_dp_dsc_branch_max_overall_throughput() - Branch device's max overall DSC pixel throughput2960* @dsc_branch_dpcd: DSC branch capabilities from DPCD2961* @is_rgb_yuv444: The mode is either RGB or YUV4442962*2963* Return the branch device's maximum overall DSC pixel throughput, based on2964* the device's DPCD DSC branch capabilities, and whether the output2965* format @is_rgb_yuv444 or yuv422/yuv420.2966*2967* Returns:2968* - 0: The maximum overall throughput capability is not indicated by2969* the device separately and it must be determined from the per-slice2970* max throughput (see @drm_dp_dsc_branch_slice_max_throughput())2971* and the maximum slice count supported by the device.2972* - > 0: The maximum overall DSC pixel throughput supported by the branch2973* device in kPixels/sec.2974*/2975int drm_dp_dsc_branch_max_overall_throughput(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE],2976bool is_rgb_yuv444)2977{2978int throughput;29792980if (is_rgb_yuv444)2981throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,2982DP_DSC_BRANCH_OVERALL_THROUGHPUT_0);2983else2984throughput = dsc_branch_dpcd_cap(dsc_branch_dpcd,2985DP_DSC_BRANCH_OVERALL_THROUGHPUT_1);29862987switch (throughput) {2988case 0:2989return 0;2990case 1:2991return 680000;2992default:2993return 600000 + 50000 * throughput;2994}2995}2996EXPORT_SYMBOL(drm_dp_dsc_branch_max_overall_throughput);29972998/**2999* drm_dp_dsc_branch_max_line_width() - Branch device's max DSC line width3000* @dsc_branch_dpcd: DSC branch capabilities from DPCD3001*3002* Return the branch device's maximum overall DSC line width, based on3003* the device's @dsc_branch_dpcd capabilities.3004*3005* Returns:3006* - 0: The maximum line width is not indicated by the device3007* separately and it must be determined from the maximum3008* slice count and slice-width supported by the device.3009* - %-EINVAL: The device indicates an invalid maximum line width3010* (< 5120 pixels).3011* - >= 5120: The maximum line width in pixels.3012*/3013int drm_dp_dsc_branch_max_line_width(const u8 dsc_branch_dpcd[DP_DSC_BRANCH_CAP_SIZE])3014{3015int line_width = dsc_branch_dpcd_cap(dsc_branch_dpcd, DP_DSC_BRANCH_MAX_LINE_WIDTH);30163017switch (line_width) {3018case 0:3019return 0;3020case 1 ... 15:3021return -EINVAL;3022default:3023return line_width * 320;3024}3025}3026EXPORT_SYMBOL(drm_dp_dsc_branch_max_line_width);30273028static int drm_dp_read_lttpr_regs(struct drm_dp_aux *aux,3029const u8 dpcd[DP_RECEIVER_CAP_SIZE], int address,3030u8 *buf, int buf_size)3031{3032/*3033* At least the DELL P2715Q monitor with a DPCD_REV < 0x14 returns3034* corrupted values when reading from the 0xF0000- range with a block3035* size bigger than 1.3036*/3037int block_size = dpcd[DP_DPCD_REV] < 0x14 ? 1 : buf_size;3038int offset;3039int ret;30403041for (offset = 0; offset < buf_size; offset += block_size) {3042ret = drm_dp_dpcd_read_data(aux,3043address + offset,3044&buf[offset], block_size);3045if (ret < 0)3046return ret;3047}30483049return 0;3050}30513052/**3053* drm_dp_read_lttpr_common_caps - read the LTTPR common capabilities3054* @aux: DisplayPort AUX channel3055* @dpcd: DisplayPort configuration data3056* @caps: buffer to return the capability info in3057*3058* Read capabilities common to all LTTPRs.3059*3060* Returns 0 on success or a negative error code on failure.3061*/3062int drm_dp_read_lttpr_common_caps(struct drm_dp_aux *aux,3063const u8 dpcd[DP_RECEIVER_CAP_SIZE],3064u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3065{3066return drm_dp_read_lttpr_regs(aux, dpcd,3067DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV,3068caps, DP_LTTPR_COMMON_CAP_SIZE);3069}3070EXPORT_SYMBOL(drm_dp_read_lttpr_common_caps);30713072/**3073* drm_dp_read_lttpr_phy_caps - read the capabilities for a given LTTPR PHY3074* @aux: DisplayPort AUX channel3075* @dpcd: DisplayPort configuration data3076* @dp_phy: LTTPR PHY to read the capabilities for3077* @caps: buffer to return the capability info in3078*3079* Read the capabilities for the given LTTPR PHY.3080*3081* Returns 0 on success or a negative error code on failure.3082*/3083int drm_dp_read_lttpr_phy_caps(struct drm_dp_aux *aux,3084const u8 dpcd[DP_RECEIVER_CAP_SIZE],3085enum drm_dp_phy dp_phy,3086u8 caps[DP_LTTPR_PHY_CAP_SIZE])3087{3088return drm_dp_read_lttpr_regs(aux, dpcd,3089DP_TRAINING_AUX_RD_INTERVAL_PHY_REPEATER(dp_phy),3090caps, DP_LTTPR_PHY_CAP_SIZE);3091}3092EXPORT_SYMBOL(drm_dp_read_lttpr_phy_caps);30933094static u8 dp_lttpr_common_cap(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE], int r)3095{3096return caps[r - DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV];3097}30983099/**3100* drm_dp_lttpr_count - get the number of detected LTTPRs3101* @caps: LTTPR common capabilities3102*3103* Get the number of detected LTTPRs from the LTTPR common capabilities info.3104*3105* Returns:3106* -ERANGE if more than supported number (8) of LTTPRs are detected3107* -EINVAL if the DP_PHY_REPEATER_CNT register contains an invalid value3108* otherwise the number of detected LTTPRs3109*/3110int drm_dp_lttpr_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3111{3112u8 count = dp_lttpr_common_cap(caps, DP_PHY_REPEATER_CNT);31133114switch (hweight8(count)) {3115case 0:3116return 0;3117case 1:3118return 8 - ilog2(count);3119case 8:3120return -ERANGE;3121default:3122return -EINVAL;3123}3124}3125EXPORT_SYMBOL(drm_dp_lttpr_count);31263127/**3128* drm_dp_lttpr_max_link_rate - get the maximum link rate supported by all LTTPRs3129* @caps: LTTPR common capabilities3130*3131* Returns the maximum link rate supported by all detected LTTPRs.3132*/3133int drm_dp_lttpr_max_link_rate(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3134{3135u8 rate = dp_lttpr_common_cap(caps, DP_MAX_LINK_RATE_PHY_REPEATER);31363137return drm_dp_bw_code_to_link_rate(rate);3138}3139EXPORT_SYMBOL(drm_dp_lttpr_max_link_rate);31403141/**3142* drm_dp_lttpr_set_transparent_mode() - set the LTTPR in transparent mode3143* @aux: DisplayPort AUX channel3144* @enable: Enable or disable transparent mode3145*3146* Returns: 0 on success or a negative error code on failure.3147*/3148int drm_dp_lttpr_set_transparent_mode(struct drm_dp_aux *aux, bool enable)3149{3150u8 val = enable ? DP_PHY_REPEATER_MODE_TRANSPARENT :3151DP_PHY_REPEATER_MODE_NON_TRANSPARENT;3152int ret = drm_dp_dpcd_writeb(aux, DP_PHY_REPEATER_MODE, val);31533154if (ret < 0)3155return ret;31563157return (ret == 1) ? 0 : -EIO;3158}3159EXPORT_SYMBOL(drm_dp_lttpr_set_transparent_mode);31603161/**3162* drm_dp_lttpr_init() - init LTTPR transparency mode according to DP standard3163* @aux: DisplayPort AUX channel3164* @lttpr_count: Number of LTTPRs. Between 0 and 8, according to DP standard.3165* Negative error code for any non-valid number.3166* See drm_dp_lttpr_count().3167*3168* Returns: 0 on success or a negative error code on failure.3169*/3170int drm_dp_lttpr_init(struct drm_dp_aux *aux, int lttpr_count)3171{3172int ret;31733174if (!lttpr_count)3175return 0;31763177/*3178* See DP Standard v2.0 3.6.6.1 about the explicit disabling of3179* non-transparent mode and the disable->enable non-transparent mode3180* sequence.3181*/3182ret = drm_dp_lttpr_set_transparent_mode(aux, true);3183if (ret)3184return ret;31853186if (lttpr_count < 0)3187return -ENODEV;31883189if (drm_dp_lttpr_set_transparent_mode(aux, false)) {3190/*3191* Roll-back to transparent mode if setting non-transparent3192* mode has failed3193*/3194drm_dp_lttpr_set_transparent_mode(aux, true);3195return -EINVAL;3196}31973198return 0;3199}3200EXPORT_SYMBOL(drm_dp_lttpr_init);32013202/**3203* drm_dp_lttpr_max_lane_count - get the maximum lane count supported by all LTTPRs3204* @caps: LTTPR common capabilities3205*3206* Returns the maximum lane count supported by all detected LTTPRs.3207*/3208int drm_dp_lttpr_max_lane_count(const u8 caps[DP_LTTPR_COMMON_CAP_SIZE])3209{3210u8 max_lanes = dp_lttpr_common_cap(caps, DP_MAX_LANE_COUNT_PHY_REPEATER);32113212return max_lanes & DP_MAX_LANE_COUNT_MASK;3213}3214EXPORT_SYMBOL(drm_dp_lttpr_max_lane_count);32153216/**3217* drm_dp_lttpr_voltage_swing_level_3_supported - check for LTTPR vswing3 support3218* @caps: LTTPR PHY capabilities3219*3220* Returns true if the @caps for an LTTPR TX PHY indicate support for3221* voltage swing level 3.3222*/3223bool3224drm_dp_lttpr_voltage_swing_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])3225{3226u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);32273228return txcap & DP_VOLTAGE_SWING_LEVEL_3_SUPPORTED;3229}3230EXPORT_SYMBOL(drm_dp_lttpr_voltage_swing_level_3_supported);32313232/**3233* drm_dp_lttpr_pre_emphasis_level_3_supported - check for LTTPR preemph3 support3234* @caps: LTTPR PHY capabilities3235*3236* Returns true if the @caps for an LTTPR TX PHY indicate support for3237* pre-emphasis level 3.3238*/3239bool3240drm_dp_lttpr_pre_emphasis_level_3_supported(const u8 caps[DP_LTTPR_PHY_CAP_SIZE])3241{3242u8 txcap = dp_lttpr_phy_cap(caps, DP_TRANSMITTER_CAPABILITY_PHY_REPEATER1);32433244return txcap & DP_PRE_EMPHASIS_LEVEL_3_SUPPORTED;3245}3246EXPORT_SYMBOL(drm_dp_lttpr_pre_emphasis_level_3_supported);32473248/**3249* drm_dp_get_phy_test_pattern() - get the requested pattern from the sink.3250* @aux: DisplayPort AUX channel3251* @data: DP phy compliance test parameters.3252*3253* Returns 0 on success or a negative error code on failure.3254*/3255int drm_dp_get_phy_test_pattern(struct drm_dp_aux *aux,3256struct drm_dp_phy_test_params *data)3257{3258int err;3259u8 rate, lanes;32603261err = drm_dp_dpcd_read_byte(aux, DP_TEST_LINK_RATE, &rate);3262if (err < 0)3263return err;3264data->link_rate = drm_dp_bw_code_to_link_rate(rate);32653266err = drm_dp_dpcd_read_byte(aux, DP_TEST_LANE_COUNT, &lanes);3267if (err < 0)3268return err;3269data->num_lanes = lanes & DP_MAX_LANE_COUNT_MASK;32703271if (lanes & DP_ENHANCED_FRAME_CAP)3272data->enhanced_frame_cap = true;32733274err = drm_dp_dpcd_read_byte(aux, DP_PHY_TEST_PATTERN, &data->phy_pattern);3275if (err < 0)3276return err;32773278switch (data->phy_pattern) {3279case DP_PHY_TEST_PATTERN_80BIT_CUSTOM:3280err = drm_dp_dpcd_read_data(aux, DP_TEST_80BIT_CUSTOM_PATTERN_7_0,3281&data->custom80, sizeof(data->custom80));3282if (err < 0)3283return err;32843285break;3286case DP_PHY_TEST_PATTERN_CP2520:3287err = drm_dp_dpcd_read_data(aux, DP_TEST_HBR2_SCRAMBLER_RESET,3288&data->hbr2_reset,3289sizeof(data->hbr2_reset));3290if (err < 0)3291return err;3292}32933294return 0;3295}3296EXPORT_SYMBOL(drm_dp_get_phy_test_pattern);32973298/**3299* drm_dp_set_phy_test_pattern() - set the pattern to the sink.3300* @aux: DisplayPort AUX channel3301* @data: DP phy compliance test parameters.3302* @dp_rev: DP revision to use for compliance testing3303*3304* Returns 0 on success or a negative error code on failure.3305*/3306int drm_dp_set_phy_test_pattern(struct drm_dp_aux *aux,3307struct drm_dp_phy_test_params *data, u8 dp_rev)3308{3309int err, i;3310u8 test_pattern;33113312test_pattern = data->phy_pattern;3313if (dp_rev < 0x12) {3314test_pattern = (test_pattern << 2) &3315DP_LINK_QUAL_PATTERN_11_MASK;3316err = drm_dp_dpcd_write_byte(aux, DP_TRAINING_PATTERN_SET,3317test_pattern);3318if (err < 0)3319return err;3320} else {3321for (i = 0; i < data->num_lanes; i++) {3322err = drm_dp_dpcd_write_byte(aux,3323DP_LINK_QUAL_LANE0_SET + i,3324test_pattern);3325if (err < 0)3326return err;3327}3328}33293330return 0;3331}3332EXPORT_SYMBOL(drm_dp_set_phy_test_pattern);33333334static const char *dp_pixelformat_get_name(enum dp_pixelformat pixelformat)3335{3336if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)3337return "Invalid";33383339switch (pixelformat) {3340case DP_PIXELFORMAT_RGB:3341return "RGB";3342case DP_PIXELFORMAT_YUV444:3343return "YUV444";3344case DP_PIXELFORMAT_YUV422:3345return "YUV422";3346case DP_PIXELFORMAT_YUV420:3347return "YUV420";3348case DP_PIXELFORMAT_Y_ONLY:3349return "Y_ONLY";3350case DP_PIXELFORMAT_RAW:3351return "RAW";3352default:3353return "Reserved";3354}3355}33563357static const char *dp_colorimetry_get_name(enum dp_pixelformat pixelformat,3358enum dp_colorimetry colorimetry)3359{3360if (pixelformat < 0 || pixelformat > DP_PIXELFORMAT_RESERVED)3361return "Invalid";33623363switch (colorimetry) {3364case DP_COLORIMETRY_DEFAULT:3365switch (pixelformat) {3366case DP_PIXELFORMAT_RGB:3367return "sRGB";3368case DP_PIXELFORMAT_YUV444:3369case DP_PIXELFORMAT_YUV422:3370case DP_PIXELFORMAT_YUV420:3371return "BT.601";3372case DP_PIXELFORMAT_Y_ONLY:3373return "DICOM PS3.14";3374case DP_PIXELFORMAT_RAW:3375return "Custom Color Profile";3376default:3377return "Reserved";3378}3379case DP_COLORIMETRY_RGB_WIDE_FIXED: /* and DP_COLORIMETRY_BT709_YCC */3380switch (pixelformat) {3381case DP_PIXELFORMAT_RGB:3382return "Wide Fixed";3383case DP_PIXELFORMAT_YUV444:3384case DP_PIXELFORMAT_YUV422:3385case DP_PIXELFORMAT_YUV420:3386return "BT.709";3387default:3388return "Reserved";3389}3390case DP_COLORIMETRY_RGB_WIDE_FLOAT: /* and DP_COLORIMETRY_XVYCC_601 */3391switch (pixelformat) {3392case DP_PIXELFORMAT_RGB:3393return "Wide Float";3394case DP_PIXELFORMAT_YUV444:3395case DP_PIXELFORMAT_YUV422:3396case DP_PIXELFORMAT_YUV420:3397return "xvYCC 601";3398default:3399return "Reserved";3400}3401case DP_COLORIMETRY_OPRGB: /* and DP_COLORIMETRY_XVYCC_709 */3402switch (pixelformat) {3403case DP_PIXELFORMAT_RGB:3404return "OpRGB";3405case DP_PIXELFORMAT_YUV444:3406case DP_PIXELFORMAT_YUV422:3407case DP_PIXELFORMAT_YUV420:3408return "xvYCC 709";3409default:3410return "Reserved";3411}3412case DP_COLORIMETRY_DCI_P3_RGB: /* and DP_COLORIMETRY_SYCC_601 */3413switch (pixelformat) {3414case DP_PIXELFORMAT_RGB:3415return "DCI-P3";3416case DP_PIXELFORMAT_YUV444:3417case DP_PIXELFORMAT_YUV422:3418case DP_PIXELFORMAT_YUV420:3419return "sYCC 601";3420default:3421return "Reserved";3422}3423case DP_COLORIMETRY_RGB_CUSTOM: /* and DP_COLORIMETRY_OPYCC_601 */3424switch (pixelformat) {3425case DP_PIXELFORMAT_RGB:3426return "Custom Profile";3427case DP_PIXELFORMAT_YUV444:3428case DP_PIXELFORMAT_YUV422:3429case DP_PIXELFORMAT_YUV420:3430return "OpYCC 601";3431default:3432return "Reserved";3433}3434case DP_COLORIMETRY_BT2020_RGB: /* and DP_COLORIMETRY_BT2020_CYCC */3435switch (pixelformat) {3436case DP_PIXELFORMAT_RGB:3437return "BT.2020 RGB";3438case DP_PIXELFORMAT_YUV444:3439case DP_PIXELFORMAT_YUV422:3440case DP_PIXELFORMAT_YUV420:3441return "BT.2020 CYCC";3442default:3443return "Reserved";3444}3445case DP_COLORIMETRY_BT2020_YCC:3446switch (pixelformat) {3447case DP_PIXELFORMAT_YUV444:3448case DP_PIXELFORMAT_YUV422:3449case DP_PIXELFORMAT_YUV420:3450return "BT.2020 YCC";3451default:3452return "Reserved";3453}3454default:3455return "Invalid";3456}3457}34583459static const char *dp_dynamic_range_get_name(enum dp_dynamic_range dynamic_range)3460{3461switch (dynamic_range) {3462case DP_DYNAMIC_RANGE_VESA:3463return "VESA range";3464case DP_DYNAMIC_RANGE_CTA:3465return "CTA range";3466default:3467return "Invalid";3468}3469}34703471static const char *dp_content_type_get_name(enum dp_content_type content_type)3472{3473switch (content_type) {3474case DP_CONTENT_TYPE_NOT_DEFINED:3475return "Not defined";3476case DP_CONTENT_TYPE_GRAPHICS:3477return "Graphics";3478case DP_CONTENT_TYPE_PHOTO:3479return "Photo";3480case DP_CONTENT_TYPE_VIDEO:3481return "Video";3482case DP_CONTENT_TYPE_GAME:3483return "Game";3484default:3485return "Reserved";3486}3487}34883489void drm_dp_vsc_sdp_log(struct drm_printer *p, const struct drm_dp_vsc_sdp *vsc)3490{3491drm_printf(p, "DP SDP: VSC, revision %u, length %u\n",3492vsc->revision, vsc->length);3493drm_printf(p, " pixelformat: %s\n",3494dp_pixelformat_get_name(vsc->pixelformat));3495drm_printf(p, " colorimetry: %s\n",3496dp_colorimetry_get_name(vsc->pixelformat, vsc->colorimetry));3497drm_printf(p, " bpc: %u\n", vsc->bpc);3498drm_printf(p, " dynamic range: %s\n",3499dp_dynamic_range_get_name(vsc->dynamic_range));3500drm_printf(p, " content type: %s\n",3501dp_content_type_get_name(vsc->content_type));3502}3503EXPORT_SYMBOL(drm_dp_vsc_sdp_log);35043505void drm_dp_as_sdp_log(struct drm_printer *p, const struct drm_dp_as_sdp *as_sdp)3506{3507drm_printf(p, "DP SDP: AS_SDP, revision %u, length %u\n",3508as_sdp->revision, as_sdp->length);3509drm_printf(p, " vtotal: %d\n", as_sdp->vtotal);3510drm_printf(p, " target_rr: %d\n", as_sdp->target_rr);3511drm_printf(p, " duration_incr_ms: %d\n", as_sdp->duration_incr_ms);3512drm_printf(p, " duration_decr_ms: %d\n", as_sdp->duration_decr_ms);3513drm_printf(p, " operation_mode: %d\n", as_sdp->mode);3514}3515EXPORT_SYMBOL(drm_dp_as_sdp_log);35163517/**3518* drm_dp_as_sdp_supported() - check if adaptive sync sdp is supported3519* @aux: DisplayPort AUX channel3520* @dpcd: DisplayPort configuration data3521*3522* Returns true if adaptive sync sdp is supported, else returns false3523*/3524bool drm_dp_as_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])3525{3526u8 rx_feature;35273528if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)3529return false;35303531if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1,3532&rx_feature) < 0) {3533drm_dbg_dp(aux->drm_dev,3534"Failed to read DP_DPRX_FEATURE_ENUMERATION_LIST_CONT_1\n");3535return false;3536}35373538return (rx_feature & DP_ADAPTIVE_SYNC_SDP_SUPPORTED);3539}3540EXPORT_SYMBOL(drm_dp_as_sdp_supported);35413542/**3543* drm_dp_vsc_sdp_supported() - check if vsc sdp is supported3544* @aux: DisplayPort AUX channel3545* @dpcd: DisplayPort configuration data3546*3547* Returns true if vsc sdp is supported, else returns false3548*/3549bool drm_dp_vsc_sdp_supported(struct drm_dp_aux *aux, const u8 dpcd[DP_RECEIVER_CAP_SIZE])3550{3551u8 rx_feature;35523553if (dpcd[DP_DPCD_REV] < DP_DPCD_REV_13)3554return false;35553556if (drm_dp_dpcd_read_byte(aux, DP_DPRX_FEATURE_ENUMERATION_LIST, &rx_feature) < 0) {3557drm_dbg_dp(aux->drm_dev, "failed to read DP_DPRX_FEATURE_ENUMERATION_LIST\n");3558return false;3559}35603561return (rx_feature & DP_VSC_SDP_EXT_FOR_COLORIMETRY_SUPPORTED);3562}3563EXPORT_SYMBOL(drm_dp_vsc_sdp_supported);35643565/**3566* drm_dp_vsc_sdp_pack() - pack a given vsc sdp into generic dp_sdp3567* @vsc: vsc sdp initialized according to its purpose as defined in3568* table 2-118 - table 2-120 in DP 1.4a specification3569* @sdp: valid handle to the generic dp_sdp which will be packed3570*3571* Returns length of sdp on success and error code on failure3572*/3573ssize_t drm_dp_vsc_sdp_pack(const struct drm_dp_vsc_sdp *vsc,3574struct dp_sdp *sdp)3575{3576size_t length = sizeof(struct dp_sdp);35773578memset(sdp, 0, sizeof(struct dp_sdp));35793580/*3581* Prepare VSC Header for SU as per DP 1.4a spec, Table 2-1193582* VSC SDP Header Bytes3583*/3584sdp->sdp_header.HB0 = 0; /* Secondary-Data Packet ID = 0 */3585sdp->sdp_header.HB1 = vsc->sdp_type; /* Secondary-data Packet Type */3586sdp->sdp_header.HB2 = vsc->revision; /* Revision Number */3587sdp->sdp_header.HB3 = vsc->length; /* Number of Valid Data Bytes */35883589if (vsc->revision == 0x6) {3590sdp->db[0] = 1;3591sdp->db[3] = 1;3592}35933594/*3595* Revision 0x5 and revision 0x7 supports Pixel Encoding/Colorimetry3596* Format as per DP 1.4a spec and DP 2.0 respectively.3597*/3598if (!(vsc->revision == 0x5 || vsc->revision == 0x7))3599goto out;36003601/* VSC SDP Payload for DB16 through DB18 */3602/* Pixel Encoding and Colorimetry Formats */3603sdp->db[16] = (vsc->pixelformat & 0xf) << 4; /* DB16[7:4] */3604sdp->db[16] |= vsc->colorimetry & 0xf; /* DB16[3:0] */36053606switch (vsc->bpc) {3607case 6:3608/* 6bpc: 0x0 */3609break;3610case 8:3611sdp->db[17] = 0x1; /* DB17[3:0] */3612break;3613case 10:3614sdp->db[17] = 0x2;3615break;3616case 12:3617sdp->db[17] = 0x3;3618break;3619case 16:3620sdp->db[17] = 0x4;3621break;3622default:3623WARN(1, "Missing case %d\n", vsc->bpc);3624return -EINVAL;3625}36263627/* Dynamic Range and Component Bit Depth */3628if (vsc->dynamic_range == DP_DYNAMIC_RANGE_CTA)3629sdp->db[17] |= 0x80; /* DB17[7] */36303631/* Content Type */3632sdp->db[18] = vsc->content_type & 0x7;36333634out:3635return length;3636}3637EXPORT_SYMBOL(drm_dp_vsc_sdp_pack);36383639/**3640* drm_dp_get_pcon_max_frl_bw() - maximum frl supported by PCON3641* @dpcd: DisplayPort configuration data3642* @port_cap: port capabilities3643*3644* Returns maximum frl bandwidth supported by PCON in GBPS,3645* returns 0 if not supported.3646*/3647int drm_dp_get_pcon_max_frl_bw(const u8 dpcd[DP_RECEIVER_CAP_SIZE],3648const u8 port_cap[4])3649{3650int bw;3651u8 buf;36523653buf = port_cap[2];3654bw = buf & DP_PCON_MAX_FRL_BW;36553656switch (bw) {3657case DP_PCON_MAX_9GBPS:3658return 9;3659case DP_PCON_MAX_18GBPS:3660return 18;3661case DP_PCON_MAX_24GBPS:3662return 24;3663case DP_PCON_MAX_32GBPS:3664return 32;3665case DP_PCON_MAX_40GBPS:3666return 40;3667case DP_PCON_MAX_48GBPS:3668return 48;3669case DP_PCON_MAX_0GBPS:3670default:3671return 0;3672}36733674return 0;3675}3676EXPORT_SYMBOL(drm_dp_get_pcon_max_frl_bw);36773678/**3679* drm_dp_pcon_frl_prepare() - Prepare PCON for FRL.3680* @aux: DisplayPort AUX channel3681* @enable_frl_ready_hpd: Configure DP_PCON_ENABLE_HPD_READY.3682*3683* Returns 0 if success, else returns negative error code.3684*/3685int drm_dp_pcon_frl_prepare(struct drm_dp_aux *aux, bool enable_frl_ready_hpd)3686{3687u8 buf = DP_PCON_ENABLE_SOURCE_CTL_MODE |3688DP_PCON_ENABLE_LINK_FRL_MODE;36893690if (enable_frl_ready_hpd)3691buf |= DP_PCON_ENABLE_HPD_READY;36923693return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3694}3695EXPORT_SYMBOL(drm_dp_pcon_frl_prepare);36963697/**3698* drm_dp_pcon_is_frl_ready() - Is PCON ready for FRL3699* @aux: DisplayPort AUX channel3700*3701* Returns true if success, else returns false.3702*/3703bool drm_dp_pcon_is_frl_ready(struct drm_dp_aux *aux)3704{3705int ret;3706u8 buf;37073708ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);3709if (ret < 0)3710return false;37113712if (buf & DP_PCON_FRL_READY)3713return true;37143715return false;3716}3717EXPORT_SYMBOL(drm_dp_pcon_is_frl_ready);37183719/**3720* drm_dp_pcon_frl_configure_1() - Set HDMI LINK Configuration-Step13721* @aux: DisplayPort AUX channel3722* @max_frl_gbps: maximum frl bw to be configured between PCON and HDMI sink3723* @frl_mode: FRL Training mode, it can be either Concurrent or Sequential.3724* In Concurrent Mode, the FRL link bring up can be done along with3725* DP Link training. In Sequential mode, the FRL link bring up is done prior to3726* the DP Link training.3727*3728* Returns 0 if success, else returns negative error code.3729*/37303731int drm_dp_pcon_frl_configure_1(struct drm_dp_aux *aux, int max_frl_gbps,3732u8 frl_mode)3733{3734int ret;3735u8 buf;37363737ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);3738if (ret < 0)3739return ret;37403741if (frl_mode == DP_PCON_ENABLE_CONCURRENT_LINK)3742buf |= DP_PCON_ENABLE_CONCURRENT_LINK;3743else3744buf &= ~DP_PCON_ENABLE_CONCURRENT_LINK;37453746switch (max_frl_gbps) {3747case 9:3748buf |= DP_PCON_ENABLE_MAX_BW_9GBPS;3749break;3750case 18:3751buf |= DP_PCON_ENABLE_MAX_BW_18GBPS;3752break;3753case 24:3754buf |= DP_PCON_ENABLE_MAX_BW_24GBPS;3755break;3756case 32:3757buf |= DP_PCON_ENABLE_MAX_BW_32GBPS;3758break;3759case 40:3760buf |= DP_PCON_ENABLE_MAX_BW_40GBPS;3761break;3762case 48:3763buf |= DP_PCON_ENABLE_MAX_BW_48GBPS;3764break;3765case 0:3766buf |= DP_PCON_ENABLE_MAX_BW_0GBPS;3767break;3768default:3769return -EINVAL;3770}37713772return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3773}3774EXPORT_SYMBOL(drm_dp_pcon_frl_configure_1);37753776/**3777* drm_dp_pcon_frl_configure_2() - Set HDMI Link configuration Step-23778* @aux: DisplayPort AUX channel3779* @max_frl_mask : Max FRL BW to be tried by the PCON with HDMI Sink3780* @frl_type : FRL training type, can be Extended, or Normal.3781* In Normal FRL training, the PCON tries each frl bw from the max_frl_mask3782* starting from min, and stops when link training is successful. In Extended3783* FRL training, all frl bw selected in the mask are trained by the PCON.3784*3785* Returns 0 if success, else returns negative error code.3786*/3787int drm_dp_pcon_frl_configure_2(struct drm_dp_aux *aux, int max_frl_mask,3788u8 frl_type)3789{3790int ret;3791u8 buf = max_frl_mask;37923793if (frl_type == DP_PCON_FRL_LINK_TRAIN_EXTENDED)3794buf |= DP_PCON_FRL_LINK_TRAIN_EXTENDED;3795else3796buf &= ~DP_PCON_FRL_LINK_TRAIN_EXTENDED;37973798return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_2, buf);3799if (ret < 0)3800return ret;38013802return 0;3803}3804EXPORT_SYMBOL(drm_dp_pcon_frl_configure_2);38053806/**3807* drm_dp_pcon_reset_frl_config() - Re-Set HDMI Link configuration.3808* @aux: DisplayPort AUX channel3809*3810* Returns 0 if success, else returns negative error code.3811*/3812int drm_dp_pcon_reset_frl_config(struct drm_dp_aux *aux)3813{3814return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, 0x0);3815}3816EXPORT_SYMBOL(drm_dp_pcon_reset_frl_config);38173818/**3819* drm_dp_pcon_frl_enable() - Enable HDMI link through FRL3820* @aux: DisplayPort AUX channel3821*3822* Returns 0 if success, else returns negative error code.3823*/3824int drm_dp_pcon_frl_enable(struct drm_dp_aux *aux)3825{3826int ret;3827u8 buf = 0;38283829ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, &buf);3830if (ret < 0)3831return ret;3832if (!(buf & DP_PCON_ENABLE_SOURCE_CTL_MODE)) {3833drm_dbg_kms(aux->drm_dev, "%s: PCON in Autonomous mode, can't enable FRL\n",3834aux->name);3835return -EINVAL;3836}3837buf |= DP_PCON_ENABLE_HDMI_LINK;3838return drm_dp_dpcd_write_byte(aux, DP_PCON_HDMI_LINK_CONFIG_1, buf);3839}3840EXPORT_SYMBOL(drm_dp_pcon_frl_enable);38413842/**3843* drm_dp_pcon_hdmi_link_active() - check if the PCON HDMI LINK status is active.3844* @aux: DisplayPort AUX channel3845*3846* Returns true if link is active else returns false.3847*/3848bool drm_dp_pcon_hdmi_link_active(struct drm_dp_aux *aux)3849{3850u8 buf;3851int ret;38523853ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_TX_LINK_STATUS, &buf);3854if (ret < 0)3855return false;38563857return buf & DP_PCON_HDMI_TX_LINK_ACTIVE;3858}3859EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_active);38603861/**3862* drm_dp_pcon_hdmi_link_mode() - get the PCON HDMI LINK MODE3863* @aux: DisplayPort AUX channel3864* @frl_trained_mask: pointer to store bitmask of the trained bw configuration.3865* Valid only if the MODE returned is FRL. For Normal Link training mode3866* only 1 of the bits will be set, but in case of Extended mode, more than3867* one bits can be set.3868*3869* Returns the link mode : TMDS or FRL on success, else returns negative error3870* code.3871*/3872int drm_dp_pcon_hdmi_link_mode(struct drm_dp_aux *aux, u8 *frl_trained_mask)3873{3874u8 buf;3875int mode;3876int ret;38773878ret = drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_POST_FRL_STATUS, &buf);3879if (ret < 0)3880return ret;38813882mode = buf & DP_PCON_HDMI_LINK_MODE;38833884if (frl_trained_mask && DP_PCON_HDMI_MODE_FRL == mode)3885*frl_trained_mask = (buf & DP_PCON_HDMI_FRL_TRAINED_BW) >> 1;38863887return mode;3888}3889EXPORT_SYMBOL(drm_dp_pcon_hdmi_link_mode);38903891/**3892* drm_dp_pcon_hdmi_frl_link_error_count() - print the error count per lane3893* during link failure between PCON and HDMI sink3894* @aux: DisplayPort AUX channel3895* @connector: DRM connector3896* code.3897**/38983899void drm_dp_pcon_hdmi_frl_link_error_count(struct drm_dp_aux *aux,3900struct drm_connector *connector)3901{3902u8 buf, error_count;3903int i, num_error;3904struct drm_hdmi_info *hdmi = &connector->display_info.hdmi;39053906for (i = 0; i < hdmi->max_lanes; i++) {3907if (drm_dp_dpcd_read_byte(aux, DP_PCON_HDMI_ERROR_STATUS_LN0 + i, &buf) < 0)3908return;39093910error_count = buf & DP_PCON_HDMI_ERROR_COUNT_MASK;3911switch (error_count) {3912case DP_PCON_HDMI_ERROR_COUNT_HUNDRED_PLUS:3913num_error = 100;3914break;3915case DP_PCON_HDMI_ERROR_COUNT_TEN_PLUS:3916num_error = 10;3917break;3918case DP_PCON_HDMI_ERROR_COUNT_THREE_PLUS:3919num_error = 3;3920break;3921default:3922num_error = 0;3923}39243925drm_err(aux->drm_dev, "%s: More than %d errors since the last read for lane %d",3926aux->name, num_error, i);3927}3928}3929EXPORT_SYMBOL(drm_dp_pcon_hdmi_frl_link_error_count);39303931/*3932* drm_dp_pcon_enc_is_dsc_1_2 - Does PCON Encoder supports DSC 1.23933* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3934*3935* Returns true is PCON encoder is DSC 1.2 else returns false.3936*/3937bool drm_dp_pcon_enc_is_dsc_1_2(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3938{3939u8 buf;3940u8 major_v, minor_v;39413942buf = pcon_dsc_dpcd[DP_PCON_DSC_VERSION - DP_PCON_DSC_ENCODER];3943major_v = (buf & DP_PCON_DSC_MAJOR_MASK) >> DP_PCON_DSC_MAJOR_SHIFT;3944minor_v = (buf & DP_PCON_DSC_MINOR_MASK) >> DP_PCON_DSC_MINOR_SHIFT;39453946if (major_v == 1 && minor_v == 2)3947return true;39483949return false;3950}3951EXPORT_SYMBOL(drm_dp_pcon_enc_is_dsc_1_2);39523953/*3954* drm_dp_pcon_dsc_max_slices - Get max slices supported by PCON DSC Encoder3955* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3956*3957* Returns maximum no. of slices supported by the PCON DSC Encoder.3958*/3959int drm_dp_pcon_dsc_max_slices(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3960{3961u8 slice_cap1, slice_cap2;39623963slice_cap1 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_1 - DP_PCON_DSC_ENCODER];3964slice_cap2 = pcon_dsc_dpcd[DP_PCON_DSC_SLICE_CAP_2 - DP_PCON_DSC_ENCODER];39653966if (slice_cap2 & DP_PCON_DSC_24_PER_DSC_ENC)3967return 24;3968if (slice_cap2 & DP_PCON_DSC_20_PER_DSC_ENC)3969return 20;3970if (slice_cap2 & DP_PCON_DSC_16_PER_DSC_ENC)3971return 16;3972if (slice_cap1 & DP_PCON_DSC_12_PER_DSC_ENC)3973return 12;3974if (slice_cap1 & DP_PCON_DSC_10_PER_DSC_ENC)3975return 10;3976if (slice_cap1 & DP_PCON_DSC_8_PER_DSC_ENC)3977return 8;3978if (slice_cap1 & DP_PCON_DSC_6_PER_DSC_ENC)3979return 6;3980if (slice_cap1 & DP_PCON_DSC_4_PER_DSC_ENC)3981return 4;3982if (slice_cap1 & DP_PCON_DSC_2_PER_DSC_ENC)3983return 2;3984if (slice_cap1 & DP_PCON_DSC_1_PER_DSC_ENC)3985return 1;39863987return 0;3988}3989EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slices);39903991/*3992* drm_dp_pcon_dsc_max_slice_width() - Get max slice width for Pcon DSC encoder3993* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder3994*3995* Returns maximum width of the slices in pixel width i.e. no. of pixels x 320.3996*/3997int drm_dp_pcon_dsc_max_slice_width(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])3998{3999u8 buf;40004001buf = pcon_dsc_dpcd[DP_PCON_DSC_MAX_SLICE_WIDTH - DP_PCON_DSC_ENCODER];40024003return buf * DP_DSC_SLICE_WIDTH_MULTIPLIER;4004}4005EXPORT_SYMBOL(drm_dp_pcon_dsc_max_slice_width);40064007/*4008* drm_dp_pcon_dsc_bpp_incr() - Get bits per pixel increment for PCON DSC encoder4009* @pcon_dsc_dpcd: DSC capabilities of the PCON DSC Encoder4010*4011* Returns the bpp precision supported by the PCON encoder.4012*/4013int drm_dp_pcon_dsc_bpp_incr(const u8 pcon_dsc_dpcd[DP_PCON_DSC_ENCODER_CAP_SIZE])4014{4015u8 buf;40164017buf = pcon_dsc_dpcd[DP_PCON_DSC_BPP_INCR - DP_PCON_DSC_ENCODER];40184019switch (buf & DP_PCON_DSC_BPP_INCR_MASK) {4020case DP_PCON_DSC_ONE_16TH_BPP:4021return 16;4022case DP_PCON_DSC_ONE_8TH_BPP:4023return 8;4024case DP_PCON_DSC_ONE_4TH_BPP:4025return 4;4026case DP_PCON_DSC_ONE_HALF_BPP:4027return 2;4028case DP_PCON_DSC_ONE_BPP:4029return 1;4030}40314032return 0;4033}4034EXPORT_SYMBOL(drm_dp_pcon_dsc_bpp_incr);40354036static4037int drm_dp_pcon_configure_dsc_enc(struct drm_dp_aux *aux, u8 pps_buf_config)4038{4039u8 buf;4040int ret;40414042ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);4043if (ret < 0)4044return ret;40454046buf |= DP_PCON_ENABLE_DSC_ENCODER;40474048if (pps_buf_config <= DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER) {4049buf &= ~DP_PCON_ENCODER_PPS_OVERRIDE_MASK;4050buf |= pps_buf_config << 2;4051}40524053return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);4054}40554056/**4057* drm_dp_pcon_pps_default() - Let PCON fill the default pps parameters4058* for DSC1.2 between PCON & HDMI2.1 sink4059* @aux: DisplayPort AUX channel4060*4061* Returns 0 on success, else returns negative error code.4062*/4063int drm_dp_pcon_pps_default(struct drm_dp_aux *aux)4064{4065return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_DISABLED);4066}4067EXPORT_SYMBOL(drm_dp_pcon_pps_default);40684069/**4070* drm_dp_pcon_pps_override_buf() - Configure PPS encoder override buffer for4071* HDMI sink4072* @aux: DisplayPort AUX channel4073* @pps_buf: 128 bytes to be written into PPS buffer for HDMI sink by PCON.4074*4075* Returns 0 on success, else returns negative error code.4076*/4077int drm_dp_pcon_pps_override_buf(struct drm_dp_aux *aux, u8 pps_buf[128])4078{4079int ret;40804081ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVERRIDE_BASE, &pps_buf, 128);4082if (ret < 0)4083return ret;40844085return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);4086}4087EXPORT_SYMBOL(drm_dp_pcon_pps_override_buf);40884089/*4090* drm_dp_pcon_pps_override_param() - Write PPS parameters to DSC encoder4091* override registers4092* @aux: DisplayPort AUX channel4093* @pps_param: 3 Parameters (2 Bytes each) : Slice Width, Slice Height,4094* bits_per_pixel.4095*4096* Returns 0 on success, else returns negative error code.4097*/4098int drm_dp_pcon_pps_override_param(struct drm_dp_aux *aux, u8 pps_param[6])4099{4100int ret;41014102ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_HEIGHT, &pps_param[0], 2);4103if (ret < 0)4104return ret;4105ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_SLICE_WIDTH, &pps_param[2], 2);4106if (ret < 0)4107return ret;4108ret = drm_dp_dpcd_write_data(aux, DP_PCON_HDMI_PPS_OVRD_BPP, &pps_param[4], 2);4109if (ret < 0)4110return ret;41114112return drm_dp_pcon_configure_dsc_enc(aux, DP_PCON_ENC_PPS_OVERRIDE_EN_BUFFER);4113}4114EXPORT_SYMBOL(drm_dp_pcon_pps_override_param);41154116/*4117* drm_dp_pcon_convert_rgb_to_ycbcr() - Configure the PCon to convert RGB to Ycbcr4118* @aux: displayPort AUX channel4119* @color_spc: Color-space/s for which conversion is to be enabled, 0 for disable.4120*4121* Returns 0 on success, else returns negative error code.4122*/4123int drm_dp_pcon_convert_rgb_to_ycbcr(struct drm_dp_aux *aux, u8 color_spc)4124{4125int ret;4126u8 buf;41274128ret = drm_dp_dpcd_read_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, &buf);4129if (ret < 0)4130return ret;41314132if (color_spc & DP_CONVERSION_RGB_YCBCR_MASK)4133buf |= (color_spc & DP_CONVERSION_RGB_YCBCR_MASK);4134else4135buf &= ~DP_CONVERSION_RGB_YCBCR_MASK;41364137return drm_dp_dpcd_write_byte(aux, DP_PROTOCOL_CONVERTER_CONTROL_2, buf);4138}4139EXPORT_SYMBOL(drm_dp_pcon_convert_rgb_to_ycbcr);41404141/**4142* drm_edp_backlight_set_level() - Set the backlight level of an eDP panel via AUX4143* @aux: The DP AUX channel to use4144* @bl: Backlight capability info from drm_edp_backlight_init()4145* @level: The brightness level to set4146*4147* Sets the brightness level of an eDP panel's backlight. Note that the panel's backlight must4148* already have been enabled by the driver by calling drm_edp_backlight_enable().4149*4150* Returns: %0 on success, negative error code on failure4151*/4152int drm_edp_backlight_set_level(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4153u32 level)4154{4155int ret;4156unsigned int offset = DP_EDP_BACKLIGHT_BRIGHTNESS_MSB;4157u8 buf[3] = { 0 };4158size_t len = 2;41594160/* The panel uses the PWM for controlling brightness levels */4161if (!(bl->aux_set || bl->luminance_set))4162return 0;41634164if (bl->luminance_set) {4165level = level * 1000;4166level &= 0xffffff;4167buf[0] = (level & 0x0000ff);4168buf[1] = (level & 0x00ff00) >> 8;4169buf[2] = (level & 0xff0000) >> 16;4170offset = DP_EDP_PANEL_TARGET_LUMINANCE_VALUE;4171len = 3;4172} else if (bl->lsb_reg_used) {4173buf[0] = (level & 0xff00) >> 8;4174buf[1] = (level & 0x00ff);4175} else {4176buf[0] = level;4177}41784179ret = drm_dp_dpcd_write_data(aux, offset, buf, len);4180if (ret < 0) {4181drm_err(aux->drm_dev,4182"%s: Failed to write aux backlight level: %d\n",4183aux->name, ret);4184return ret;4185}41864187return 0;4188}4189EXPORT_SYMBOL(drm_edp_backlight_set_level);41904191static int4192drm_edp_backlight_set_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4193bool enable)4194{4195int ret;4196u8 buf;41974198/* This panel uses the EDP_BL_PWR GPIO for enablement */4199if (!bl->aux_enable)4200return 0;42014202ret = drm_dp_dpcd_read_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, &buf);4203if (ret < 0) {4204drm_err(aux->drm_dev, "%s: Failed to read eDP display control register: %d\n",4205aux->name, ret);4206return ret;4207}4208if (enable)4209buf |= DP_EDP_BACKLIGHT_ENABLE;4210else4211buf &= ~DP_EDP_BACKLIGHT_ENABLE;42124213ret = drm_dp_dpcd_write_byte(aux, DP_EDP_DISPLAY_CONTROL_REGISTER, buf);4214if (ret < 0) {4215drm_err(aux->drm_dev, "%s: Failed to write eDP display control register: %d\n",4216aux->name, ret);4217return ret;4218}42194220return 0;4221}42224223/**4224* drm_edp_backlight_enable() - Enable an eDP panel's backlight using DPCD4225* @aux: The DP AUX channel to use4226* @bl: Backlight capability info from drm_edp_backlight_init()4227* @level: The initial backlight level to set via AUX, if there is one4228*4229* This function handles enabling DPCD backlight controls on a panel over DPCD, while additionally4230* restoring any important backlight state such as the given backlight level, the brightness byte4231* count, backlight frequency, etc.4232*4233* Note that certain panels do not support being enabled or disabled via DPCD, but instead require4234* that the driver handle enabling/disabling the panel through implementation-specific means using4235* the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,4236* this function becomes a no-op, and the driver is expected to handle powering the panel on using4237* the EDP_BL_PWR GPIO.4238*4239* Returns: %0 on success, negative error code on failure.4240*/4241int drm_edp_backlight_enable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl,4242const u32 level)4243{4244int ret;4245u8 dpcd_buf;42464247if (bl->aux_set)4248dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD;4249else4250dpcd_buf = DP_EDP_BACKLIGHT_CONTROL_MODE_PWM;42514252if (bl->luminance_set)4253dpcd_buf |= DP_EDP_PANEL_LUMINANCE_CONTROL_ENABLE;42544255if (bl->pwmgen_bit_count) {4256ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, bl->pwmgen_bit_count);4257if (ret < 0)4258drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",4259aux->name, ret);4260}42614262if (bl->pwm_freq_pre_divider) {4263ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_FREQ_SET,4264bl->pwm_freq_pre_divider);4265if (ret < 0)4266drm_dbg_kms(aux->drm_dev,4267"%s: Failed to write aux backlight frequency: %d\n",4268aux->name, ret);4269else4270dpcd_buf |= DP_EDP_BACKLIGHT_FREQ_AUX_SET_ENABLE;4271}42724273ret = drm_dp_dpcd_write_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, dpcd_buf);4274if (ret < 0) {4275drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux backlight mode: %d\n",4276aux->name, ret);4277return ret < 0 ? ret : -EIO;4278}42794280ret = drm_edp_backlight_set_level(aux, bl, level);4281if (ret < 0)4282return ret;4283ret = drm_edp_backlight_set_enable(aux, bl, true);4284if (ret < 0)4285return ret;42864287return 0;4288}4289EXPORT_SYMBOL(drm_edp_backlight_enable);42904291/**4292* drm_edp_backlight_disable() - Disable an eDP backlight using DPCD, if supported4293* @aux: The DP AUX channel to use4294* @bl: Backlight capability info from drm_edp_backlight_init()4295*4296* This function handles disabling DPCD backlight controls on a panel over AUX.4297*4298* Note that certain panels do not support being enabled or disabled via DPCD, but instead require4299* that the driver handle enabling/disabling the panel through implementation-specific means using4300* the EDP_BL_PWR GPIO. For such panels, &drm_edp_backlight_info.aux_enable will be set to %false,4301* this function becomes a no-op, and the driver is expected to handle powering the panel off using4302* the EDP_BL_PWR GPIO.4303*4304* Returns: %0 on success or no-op, negative error code on failure.4305*/4306int drm_edp_backlight_disable(struct drm_dp_aux *aux, const struct drm_edp_backlight_info *bl)4307{4308int ret;43094310ret = drm_edp_backlight_set_enable(aux, bl, false);4311if (ret < 0)4312return ret;43134314return 0;4315}4316EXPORT_SYMBOL(drm_edp_backlight_disable);43174318static inline int4319drm_edp_backlight_probe_max(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4320u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE])4321{4322int fxp, fxp_min, fxp_max, fxp_actual, f = 1;4323int ret;4324u8 pn, pn_min, pn_max, bit_count;43254326if (!bl->aux_set)4327return 0;43284329ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, &bit_count);4330if (ret < 0) {4331drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap: %d\n",4332aux->name, ret);4333return -ENODEV;4334}43354336bit_count &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43374338ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN, &pn_min);4339if (ret < 0) {4340drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap min: %d\n",4341aux->name, ret);4342return -ENODEV;4343}4344pn_min &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43454346ret = drm_dp_dpcd_read_byte(aux, DP_EDP_PWMGEN_BIT_COUNT_CAP_MAX, &pn_max);4347if (ret < 0) {4348drm_dbg_kms(aux->drm_dev, "%s: Failed to read pwmgen bit count cap max: %d\n",4349aux->name, ret);4350return -ENODEV;4351}4352pn_max &= DP_EDP_PWMGEN_BIT_COUNT_MASK;43534354if (unlikely(pn_min > pn_max)) {4355drm_dbg_kms(aux->drm_dev, "%s: Invalid pwmgen bit count cap min/max returned: %d %d\n",4356aux->name, pn_min, pn_max);4357return -EINVAL;4358}43594360/*4361* Per VESA eDP Spec v1.4b, section 3.3.10.2:4362* If DP_EDP_PWMGEN_BIT_COUNT is less than DP_EDP_PWMGEN_BIT_COUNT_CAP_MIN,4363* the sink must use the MIN value as the effective PWM bit count.4364* Clamp the reported value to the [MIN, MAX] capability range to ensure4365* correct brightness scaling on compliant eDP panels.4366* Only enable this logic if the [MIN, MAX] range is valid in regard to Spec.4367*/4368pn = bit_count;4369if (bit_count < pn_min)4370pn = clamp(bit_count, pn_min, pn_max);43714372bl->max = (1 << pn) - 1;4373if (!driver_pwm_freq_hz) {4374if (pn != bit_count)4375goto bit_count_write_back;43764377return 0;4378}43794380/*4381* Set PWM Frequency divider to match desired frequency provided by the driver.4382* The PWM Frequency is calculated as 27Mhz / (F x P).4383* - Where F = PWM Frequency Pre-Divider value programmed by field 7:0 of the4384* EDP_BACKLIGHT_FREQ_SET register (DPCD Address 00728h)4385* - Where P = 2^Pn, where Pn is the value programmed by field 4:0 of the4386* EDP_PWMGEN_BIT_COUNT register (DPCD Address 00724h)4387*/43884389/* Find desired value of (F x P)4390* Note that, if F x P is out of supported range, the maximum value or minimum value will4391* applied automatically. So no need to check that.4392*/4393fxp = DIV_ROUND_CLOSEST(1000 * DP_EDP_BACKLIGHT_FREQ_BASE_KHZ, driver_pwm_freq_hz);43944395/* Use highest possible value of Pn for more granularity of brightness adjustment while4396* satisfying the conditions below.4397* - Pn is in the range of Pn_min and Pn_max4398* - F is in the range of 1 and 2554399* - FxP is within 25% of desired value.4400* Note: 25% is arbitrary value and may need some tweak.4401*/4402/* Ensure frequency is within 25% of desired value */4403fxp_min = DIV_ROUND_CLOSEST(fxp * 3, 4);4404fxp_max = DIV_ROUND_CLOSEST(fxp * 5, 4);4405if (fxp_min < (1 << pn_min) || (255 << pn_max) < fxp_max) {4406drm_dbg_kms(aux->drm_dev,4407"%s: Driver defined backlight frequency (%d) out of range\n",4408aux->name, driver_pwm_freq_hz);4409return 0;4410}44114412for (pn = pn_max; pn >= pn_min; pn--) {4413f = clamp(DIV_ROUND_CLOSEST(fxp, 1 << pn), 1, 255);4414fxp_actual = f << pn;4415if (fxp_min <= fxp_actual && fxp_actual <= fxp_max)4416break;4417}44184419bit_count_write_back:4420ret = drm_dp_dpcd_write_byte(aux, DP_EDP_PWMGEN_BIT_COUNT, pn);4421if (ret < 0) {4422drm_dbg_kms(aux->drm_dev, "%s: Failed to write aux pwmgen bit count: %d\n",4423aux->name, ret);4424return 0;4425}44264427if (!driver_pwm_freq_hz)4428return 0;44294430bl->pwmgen_bit_count = pn;4431bl->max = (1 << pn) - 1;44324433if (edp_dpcd[2] & DP_EDP_BACKLIGHT_FREQ_AUX_SET_CAP) {4434bl->pwm_freq_pre_divider = f;4435drm_dbg_kms(aux->drm_dev, "%s: Using backlight frequency from driver (%dHz)\n",4436aux->name, driver_pwm_freq_hz);4437}44384439return 0;4440}44414442static inline int4443drm_edp_backlight_probe_state(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4444u8 *current_mode)4445{4446int ret;4447u8 buf[3];4448u8 mode_reg;44494450ret = drm_dp_dpcd_read_byte(aux, DP_EDP_BACKLIGHT_MODE_SET_REGISTER, &mode_reg);4451if (ret < 0) {4452drm_dbg_kms(aux->drm_dev, "%s: Failed to read backlight mode: %d\n",4453aux->name, ret);4454return ret < 0 ? ret : -EIO;4455}44564457*current_mode = (mode_reg & DP_EDP_BACKLIGHT_CONTROL_MODE_MASK);4458if (!bl->aux_set)4459return 0;44604461if (*current_mode == DP_EDP_BACKLIGHT_CONTROL_MODE_DPCD) {4462int size = 1 + bl->lsb_reg_used;44634464if (bl->luminance_set) {4465ret = drm_dp_dpcd_read_data(aux, DP_EDP_PANEL_TARGET_LUMINANCE_VALUE,4466buf, sizeof(buf));4467if (ret < 0) {4468drm_dbg_kms(aux->drm_dev,4469"%s: Failed to read backlight level: %d\n",4470aux->name, ret);4471return ret;4472}44734474/*4475* Incase luminance is set we want to send the value back in nits but4476* since DP_EDP_PANEL_TARGET_LUMINANCE stores values in millinits we4477* need to divide by 1000.4478*/4479return (buf[0] | buf[1] << 8 | buf[2] << 16) / 1000;4480} else {4481ret = drm_dp_dpcd_read_data(aux, DP_EDP_BACKLIGHT_BRIGHTNESS_MSB,4482buf, size);4483if (ret < 0) {4484drm_dbg_kms(aux->drm_dev,4485"%s: Failed to read backlight level: %d\n",4486aux->name, ret);4487return ret;4488}44894490if (bl->lsb_reg_used)4491return (buf[0] << 8) | buf[1];4492else4493return buf[0];4494}4495}44964497/*4498* If we're not in DPCD control mode yet, the programmed brightness value is meaningless and4499* the driver should assume max brightness4500*/4501return bl->max;4502}45034504/**4505* drm_edp_backlight_init() - Probe a display panel's TCON using the standard VESA eDP backlight4506* interface.4507* @aux: The DP aux device to use for probing4508* @bl: The &drm_edp_backlight_info struct to fill out with information on the backlight4509* @max_luminance: max luminance when need luminance is set as true4510* @driver_pwm_freq_hz: Optional PWM frequency from the driver in hz4511* @edp_dpcd: A cached copy of the eDP DPCD4512* @current_level: Where to store the probed brightness level, if any4513* @current_mode: Where to store the currently set backlight control mode4514* @need_luminance: Tells us if a we want to manipulate backlight using luminance values4515*4516* Initializes a &drm_edp_backlight_info struct by probing @aux for it's backlight capabilities,4517* along with also probing the current and maximum supported brightness levels.4518*4519* If @driver_pwm_freq_hz is non-zero, this will be used as the backlight frequency. Otherwise, the4520* default frequency from the panel is used.4521*4522* Returns: %0 on success, negative error code on failure.4523*/4524int4525drm_edp_backlight_init(struct drm_dp_aux *aux, struct drm_edp_backlight_info *bl,4526u32 max_luminance,4527u16 driver_pwm_freq_hz, const u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE],4528u32 *current_level, u8 *current_mode, bool need_luminance)4529{4530int ret;45314532if (edp_dpcd[1] & DP_EDP_BACKLIGHT_AUX_ENABLE_CAP)4533bl->aux_enable = true;4534if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_AUX_SET_CAP)4535bl->aux_set = true;4536if (edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_BYTE_COUNT)4537bl->lsb_reg_used = true;4538if ((edp_dpcd[0] & DP_EDP_15) && edp_dpcd[3] &4539(DP_EDP_PANEL_LUMINANCE_CONTROL_CAPABLE) && need_luminance)4540bl->luminance_set = true;45414542/* Sanity check caps */4543if (!bl->aux_set && !(edp_dpcd[2] & DP_EDP_BACKLIGHT_BRIGHTNESS_PWM_PIN_CAP) &&4544!bl->luminance_set) {4545drm_dbg_kms(aux->drm_dev,4546"%s: Panel does not support AUX, PWM or luminance-based brightness control. Aborting\n",4547aux->name);4548return -EINVAL;4549}45504551if (bl->luminance_set) {4552bl->max = max_luminance;4553} else {4554ret = drm_edp_backlight_probe_max(aux, bl, driver_pwm_freq_hz, edp_dpcd);4555if (ret < 0)4556return ret;4557}45584559ret = drm_edp_backlight_probe_state(aux, bl, current_mode);4560if (ret < 0)4561return ret;4562*current_level = ret;45634564drm_dbg_kms(aux->drm_dev,4565"%s: Found backlight: aux_set=%d aux_enable=%d mode=%d\n",4566aux->name, bl->aux_set, bl->aux_enable, *current_mode);4567if (bl->aux_set) {4568drm_dbg_kms(aux->drm_dev,4569"%s: Backlight caps: level=%d/%d pwm_freq_pre_divider=%d lsb_reg_used=%d\n",4570aux->name, *current_level, bl->max, bl->pwm_freq_pre_divider,4571bl->lsb_reg_used);4572}45734574return 0;4575}4576EXPORT_SYMBOL(drm_edp_backlight_init);45774578#if IS_BUILTIN(CONFIG_BACKLIGHT_CLASS_DEVICE) || \4579(IS_MODULE(CONFIG_DRM_KMS_HELPER) && IS_MODULE(CONFIG_BACKLIGHT_CLASS_DEVICE))45804581static int dp_aux_backlight_update_status(struct backlight_device *bd)4582{4583struct dp_aux_backlight *bl = bl_get_data(bd);4584u16 brightness = backlight_get_brightness(bd);4585int ret = 0;45864587if (!backlight_is_blank(bd)) {4588if (!bl->enabled) {4589drm_edp_backlight_enable(bl->aux, &bl->info, brightness);4590bl->enabled = true;4591return 0;4592}4593ret = drm_edp_backlight_set_level(bl->aux, &bl->info, brightness);4594} else {4595if (bl->enabled) {4596drm_edp_backlight_disable(bl->aux, &bl->info);4597bl->enabled = false;4598}4599}46004601return ret;4602}46034604static const struct backlight_ops dp_aux_bl_ops = {4605.update_status = dp_aux_backlight_update_status,4606};46074608/**4609* drm_panel_dp_aux_backlight - create and use DP AUX backlight4610* @panel: DRM panel4611* @aux: The DP AUX channel to use4612*4613* Use this function to create and handle backlight if your panel4614* supports backlight control over DP AUX channel using DPCD4615* registers as per VESA's standard backlight control interface.4616*4617* When the panel is enabled backlight will be enabled after a4618* successful call to &drm_panel_funcs.enable()4619*4620* When the panel is disabled backlight will be disabled before the4621* call to &drm_panel_funcs.disable().4622*4623* A typical implementation for a panel driver supporting backlight4624* control over DP AUX will call this function at probe time.4625* Backlight will then be handled transparently without requiring4626* any intervention from the driver.4627*4628* drm_panel_dp_aux_backlight() must be called after the call to drm_panel_init().4629*4630* Return: 0 on success or a negative error code on failure.4631*/4632int drm_panel_dp_aux_backlight(struct drm_panel *panel, struct drm_dp_aux *aux)4633{4634struct dp_aux_backlight *bl;4635struct backlight_properties props = { 0 };4636u32 current_level;4637u8 current_mode;4638u8 edp_dpcd[EDP_DISPLAY_CTL_CAP_SIZE];4639int ret;46404641if (!panel || !panel->dev || !aux)4642return -EINVAL;46434644ret = drm_dp_dpcd_read_data(aux, DP_EDP_DPCD_REV, edp_dpcd,4645EDP_DISPLAY_CTL_CAP_SIZE);4646if (ret < 0)4647return ret;46484649if (!drm_edp_backlight_supported(edp_dpcd)) {4650DRM_DEV_INFO(panel->dev, "DP AUX backlight is not supported\n");4651return 0;4652}46534654bl = devm_kzalloc(panel->dev, sizeof(*bl), GFP_KERNEL);4655if (!bl)4656return -ENOMEM;46574658bl->aux = aux;46594660ret = drm_edp_backlight_init(aux, &bl->info, 0, 0, edp_dpcd,4661¤t_level, ¤t_mode, false);4662if (ret < 0)4663return ret;46644665props.type = BACKLIGHT_RAW;4666props.brightness = current_level;4667props.max_brightness = bl->info.max;46684669bl->base = devm_backlight_device_register(panel->dev, "dp_aux_backlight",4670panel->dev, bl,4671&dp_aux_bl_ops, &props);4672if (IS_ERR(bl->base))4673return PTR_ERR(bl->base);46744675backlight_disable(bl->base);46764677panel->backlight = bl->base;46784679return 0;4680}4681EXPORT_SYMBOL(drm_panel_dp_aux_backlight);46824683#endif46844685/* See DP Standard v2.1 2.6.4.4.1.1, 2.8.4.4, 2.8.7 */4686static int drm_dp_link_data_symbol_cycles(int lane_count, int pixels,4687int bpp_x16, int symbol_size,4688bool is_mst)4689{4690int cycles = DIV_ROUND_UP(pixels * bpp_x16, 16 * symbol_size * lane_count);4691int align = is_mst ? 4 / lane_count : 1;46924693return ALIGN(cycles, align);4694}46954696/**4697* drm_dp_link_symbol_cycles - calculate the link symbol count with/without dsc4698* @lane_count: DP link lane count4699* @pixels: number of pixels in a scanline4700* @dsc_slice_count: number of slices for DSC or '0' for non-DSC4701* @bpp_x16: bits per pixel in .4 binary fixed format4702* @symbol_size: DP symbol size4703* @is_mst: %true for MST and %false for SST4704*4705* Calculate the link symbol cycles for both DSC (@dsc_slice_count !=0) and4706* non-DSC case (@dsc_slice_count == 0) and return the count.4707*/4708int drm_dp_link_symbol_cycles(int lane_count, int pixels, int dsc_slice_count,4709int bpp_x16, int symbol_size, bool is_mst)4710{4711int slice_count = dsc_slice_count ? : 1;4712int slice_pixels = DIV_ROUND_UP(pixels, slice_count);4713int slice_data_cycles = drm_dp_link_data_symbol_cycles(lane_count,4714slice_pixels,4715bpp_x16,4716symbol_size,4717is_mst);4718int slice_eoc_cycles = 0;47194720if (dsc_slice_count)4721slice_eoc_cycles = is_mst ? 4 / lane_count : 1;47224723return slice_count * (slice_data_cycles + slice_eoc_cycles);4724}4725EXPORT_SYMBOL(drm_dp_link_symbol_cycles);47264727/**4728* drm_dp_bw_overhead - Calculate the BW overhead of a DP link stream4729* @lane_count: DP link lane count4730* @hactive: pixel count of the active period in one scanline of the stream4731* @dsc_slice_count: number of slices for DSC or '0' for non-DSC4732* @bpp_x16: bits per pixel in .4 binary fixed point4733* @flags: DRM_DP_OVERHEAD_x flags4734*4735* Calculate the BW allocation overhead of a DP link stream, depending4736* on the link's4737* - @lane_count4738* - SST/MST mode (@flags / %DRM_DP_OVERHEAD_MST)4739* - symbol size (@flags / %DRM_DP_OVERHEAD_UHBR)4740* - FEC mode (@flags / %DRM_DP_OVERHEAD_FEC)4741* - SSC/REF_CLK mode (@flags / %DRM_DP_OVERHEAD_SSC_REF_CLK)4742* as well as the stream's4743* - @hactive timing4744* - @bpp_x16 color depth4745* - compression mode (@dsc_slice_count != 0)4746* Note that this overhead doesn't account for the 8b/10b, 128b/132b4747* channel coding efficiency, for that see4748* @drm_dp_link_bw_channel_coding_efficiency().4749*4750* Returns the overhead as 100% + overhead% in 1ppm units.4751*/4752int drm_dp_bw_overhead(int lane_count, int hactive,4753int dsc_slice_count,4754int bpp_x16, unsigned long flags)4755{4756int symbol_size = flags & DRM_DP_BW_OVERHEAD_UHBR ? 32 : 8;4757bool is_mst = flags & DRM_DP_BW_OVERHEAD_MST;4758u32 overhead = 1000000;4759int symbol_cycles;47604761if (lane_count == 0 || hactive == 0 || bpp_x16 == 0) {4762DRM_DEBUG_KMS("Invalid BW overhead params: lane_count %d, hactive %d, bpp_x16 " FXP_Q4_FMT "\n",4763lane_count, hactive,4764FXP_Q4_ARGS(bpp_x16));4765return 0;4766}47674768/*4769* DP Standard v2.1 2.6.4.14770* SSC downspread and ref clock variation margin:4771* 5300ppm + 300ppm ~ 0.6%4772*/4773if (flags & DRM_DP_BW_OVERHEAD_SSC_REF_CLK)4774overhead += 6000;47754776/*4777* DP Standard v2.1 2.6.4.1.1, 3.5.1.5.4:4778* FEC symbol insertions for 8b/10b channel coding:4779* After each 250 data symbols on 2-4 lanes:4780* 250 LL + 5 FEC_PARITY_PH + 1 CD_ADJ (256 byte FEC block)4781* After each 2 x 250 data symbols on 1 lane:4782* 2 * 250 LL + 11 FEC_PARITY_PH + 1 CD_ADJ (512 byte FEC block)4783* After 256 (2-4 lanes) or 128 (1 lane) FEC blocks:4784* 256 * 256 bytes + 1 FEC_PM4785* or4786* 128 * 512 bytes + 1 FEC_PM4787* (256 * 6 + 1) / (256 * 250) = 2.4015625 %4788*/4789if (flags & DRM_DP_BW_OVERHEAD_FEC)4790overhead += 24016;47914792/*4793* DP Standard v2.1 2.7.9, 5.9.74794* The FEC overhead for UHBR is accounted for in its 96.71% channel4795* coding efficiency.4796*/4797WARN_ON((flags & DRM_DP_BW_OVERHEAD_UHBR) &&4798(flags & DRM_DP_BW_OVERHEAD_FEC));47994800symbol_cycles = drm_dp_link_symbol_cycles(lane_count, hactive,4801dsc_slice_count,4802bpp_x16, symbol_size,4803is_mst);48044805return DIV_ROUND_UP_ULL(mul_u32_u32(symbol_cycles * symbol_size * lane_count,4806overhead * 16),4807hactive * bpp_x16);4808}4809EXPORT_SYMBOL(drm_dp_bw_overhead);48104811/**4812* drm_dp_bw_channel_coding_efficiency - Get a DP link's channel coding efficiency4813* @is_uhbr: Whether the link has a 128b/132b channel coding4814*4815* Return the channel coding efficiency of the given DP link type, which is4816* either 8b/10b or 128b/132b (aka UHBR). The corresponding overhead includes4817* the 8b -> 10b, 128b -> 132b pixel data to link symbol conversion overhead4818* and for 128b/132b any link or PHY level control symbol insertion overhead4819* (LLCP, FEC, PHY sync, see DP Standard v2.1 3.5.2.18). For 8b/10b the4820* corresponding FEC overhead is BW allocation specific, included in the value4821* returned by drm_dp_bw_overhead().4822*4823* Returns the efficiency in the 100%/coding-overhead% ratio in4824* 1ppm units.4825*/4826int drm_dp_bw_channel_coding_efficiency(bool is_uhbr)4827{4828if (is_uhbr)4829return 967100;4830else4831/*4832* Note that on 8b/10b MST the efficiency is only4833* 78.75% due to the 1 out of 64 MTPH packet overhead,4834* not accounted for here.4835*/4836return 800000;4837}4838EXPORT_SYMBOL(drm_dp_bw_channel_coding_efficiency);48394840/**4841* drm_dp_max_dprx_data_rate - Get the max data bandwidth of a DPRX sink4842* @max_link_rate: max DPRX link rate in 10kbps units4843* @max_lanes: max DPRX lane count4844*4845* Given a link rate and lanes, get the data bandwidth.4846*4847* Data bandwidth is the actual payload rate, which depends on the data4848* bandwidth efficiency and the link rate.4849*4850* Note that protocol layers above the DPRX link level considered here can4851* further limit the maximum data rate. Such layers are the MST topology (with4852* limits on the link between the source and first branch device as well as on4853* the whole MST path until the DPRX link) and (Thunderbolt) DP tunnels -4854* which in turn can encapsulate an MST link with its own limit - with each4855* SST or MST encapsulated tunnel sharing the BW of a tunnel group.4856*4857* Returns the maximum data rate in kBps units.4858*/4859int drm_dp_max_dprx_data_rate(int max_link_rate, int max_lanes)4860{4861int ch_coding_efficiency =4862drm_dp_bw_channel_coding_efficiency(drm_dp_is_uhbr_rate(max_link_rate));48634864return DIV_ROUND_DOWN_ULL(mul_u32_u32(max_link_rate * 10 * max_lanes,4865ch_coding_efficiency),48661000000 * 8);4867}4868EXPORT_SYMBOL(drm_dp_max_dprx_data_rate);486948704871