Path: blob/master/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
26516 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* DesignWare High-Definition Multimedia Interface (HDMI) driver3*4* Copyright (C) 2013-2015 Mentor Graphics Inc.5* Copyright (C) 2011-2013 Freescale Semiconductor, Inc.6* Copyright (C) 2010, Guennadi Liakhovetski <[email protected]>7*/8#include <linux/clk.h>9#include <linux/delay.h>10#include <linux/err.h>11#include <linux/export.h>12#include <linux/hdmi.h>13#include <linux/i2c.h>14#include <linux/irq.h>15#include <linux/module.h>16#include <linux/mutex.h>17#include <linux/of.h>18#include <linux/pinctrl/consumer.h>19#include <linux/regmap.h>20#include <linux/dma-mapping.h>21#include <linux/spinlock.h>2223#include <media/cec-notifier.h>2425#include <linux/media-bus-format.h>26#include <linux/videodev2.h>2728#include <drm/bridge/dw_hdmi.h>29#include <drm/display/drm_hdmi_helper.h>30#include <drm/display/drm_scdc_helper.h>31#include <drm/drm_atomic.h>32#include <drm/drm_atomic_helper.h>33#include <drm/drm_bridge.h>34#include <drm/drm_edid.h>35#include <drm/drm_of.h>36#include <drm/drm_print.h>37#include <drm/drm_probe_helper.h>3839#include "dw-hdmi-audio.h"40#include "dw-hdmi-cec.h"41#include "dw-hdmi.h"4243#define DDC_CI_ADDR 0x3744#define DDC_SEGMENT_ADDR 0x304546#define HDMI_EDID_LEN 5124748/* DW-HDMI Controller >= 0x200a are at least compliant with SCDC version 1 */49#define SCDC_MIN_SOURCE_VERSION 0x15051#define HDMI14_MAX_TMDSCLK 3400000005253static const u16 csc_coeff_default[3][4] = {54{ 0x2000, 0x0000, 0x0000, 0x0000 },55{ 0x0000, 0x2000, 0x0000, 0x0000 },56{ 0x0000, 0x0000, 0x2000, 0x0000 }57};5859static const u16 csc_coeff_rgb_out_eitu601[3][4] = {60{ 0x2000, 0x6926, 0x74fd, 0x010e },61{ 0x2000, 0x2cdd, 0x0000, 0x7e9a },62{ 0x2000, 0x0000, 0x38b4, 0x7e3b }63};6465static const u16 csc_coeff_rgb_out_eitu709[3][4] = {66{ 0x2000, 0x7106, 0x7a02, 0x00a7 },67{ 0x2000, 0x3264, 0x0000, 0x7e6d },68{ 0x2000, 0x0000, 0x3b61, 0x7e25 }69};7071static const u16 csc_coeff_rgb_in_eitu601[3][4] = {72{ 0x2591, 0x1322, 0x074b, 0x0000 },73{ 0x6535, 0x2000, 0x7acc, 0x0200 },74{ 0x6acd, 0x7534, 0x2000, 0x0200 }75};7677static const u16 csc_coeff_rgb_in_eitu709[3][4] = {78{ 0x2dc5, 0x0d9b, 0x049e, 0x0000 },79{ 0x62f0, 0x2000, 0x7d11, 0x0200 },80{ 0x6756, 0x78ab, 0x2000, 0x0200 }81};8283static const u16 csc_coeff_rgb_full_to_rgb_limited[3][4] = {84{ 0x1b7c, 0x0000, 0x0000, 0x0020 },85{ 0x0000, 0x1b7c, 0x0000, 0x0020 },86{ 0x0000, 0x0000, 0x1b7c, 0x0020 }87};8889struct hdmi_vmode {90bool mdataenablepolarity;9192unsigned int mpixelclock;93unsigned int mpixelrepetitioninput;94unsigned int mpixelrepetitionoutput;95unsigned int mtmdsclock;96};9798struct hdmi_data_info {99unsigned int enc_in_bus_format;100unsigned int enc_out_bus_format;101unsigned int enc_in_encoding;102unsigned int enc_out_encoding;103unsigned int pix_repet_factor;104unsigned int hdcp_enable;105struct hdmi_vmode video_mode;106bool rgb_limited_range;107};108109struct dw_hdmi_i2c {110struct i2c_adapter adap;111112struct mutex lock; /* used to serialize data transfers */113struct completion cmp;114u8 stat;115116u8 slave_reg;117bool is_regaddr;118bool is_segment;119};120121struct dw_hdmi_phy_data {122enum dw_hdmi_phy_type type;123const char *name;124unsigned int gen;125bool has_svsret;126int (*configure)(struct dw_hdmi *hdmi,127const struct dw_hdmi_plat_data *pdata,128unsigned long mpixelclock);129};130131struct dw_hdmi {132struct drm_connector connector;133struct drm_bridge bridge;134struct drm_bridge *next_bridge;135136unsigned int version;137138struct platform_device *audio;139struct platform_device *cec;140struct device *dev;141struct dw_hdmi_i2c *i2c;142143struct hdmi_data_info hdmi_data;144const struct dw_hdmi_plat_data *plat_data;145146int vic;147148u8 edid[HDMI_EDID_LEN];149150struct {151const struct dw_hdmi_phy_ops *ops;152const char *name;153void *data;154bool enabled;155} phy;156157struct drm_display_mode previous_mode;158159struct i2c_adapter *ddc;160void __iomem *regs;161bool sink_is_hdmi;162bool sink_has_audio;163164struct pinctrl *pinctrl;165struct pinctrl_state *default_state;166struct pinctrl_state *unwedge_state;167168struct mutex mutex; /* for state below and previous_mode */169enum drm_connector_force force; /* mutex-protected force state */170struct drm_connector *curr_conn;/* current connector (only valid when !disabled) */171bool disabled; /* DRM has disabled our bridge */172bool bridge_is_on; /* indicates the bridge is on */173bool rxsense; /* rxsense state */174u8 phy_mask; /* desired phy int mask settings */175u8 mc_clkdis; /* clock disable register */176177spinlock_t audio_lock;178struct mutex audio_mutex;179unsigned int sample_non_pcm;180unsigned int sample_width;181unsigned int sample_rate;182unsigned int channels;183unsigned int audio_cts;184unsigned int audio_n;185bool audio_enable;186187unsigned int reg_shift;188struct regmap *regm;189void (*enable_audio)(struct dw_hdmi *hdmi);190void (*disable_audio)(struct dw_hdmi *hdmi);191192struct mutex cec_notifier_mutex;193struct cec_notifier *cec_notifier;194195hdmi_codec_plugged_cb plugged_cb;196struct device *codec_dev;197enum drm_connector_status last_connector_result;198};199200#define HDMI_IH_PHY_STAT0_RX_SENSE \201(HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \202HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)203204#define HDMI_PHY_RX_SENSE \205(HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \206HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)207208static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)209{210regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);211}212213static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)214{215unsigned int val = 0;216217regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);218219return val;220}221222static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)223{224if (hdmi->plugged_cb && hdmi->codec_dev)225hdmi->plugged_cb(hdmi->codec_dev, plugged);226}227228int dw_hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn,229struct device *codec_dev)230{231bool plugged;232233mutex_lock(&hdmi->mutex);234hdmi->plugged_cb = fn;235hdmi->codec_dev = codec_dev;236plugged = hdmi->last_connector_result == connector_status_connected;237handle_plugged_change(hdmi, plugged);238mutex_unlock(&hdmi->mutex);239240return 0;241}242EXPORT_SYMBOL_GPL(dw_hdmi_set_plugged_cb);243244static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)245{246regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);247}248249static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,250u8 shift, u8 mask)251{252hdmi_modb(hdmi, data << shift, mask, reg);253}254255static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)256{257hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,258HDMI_PHY_I2CM_INT_ADDR);259260hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |261HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,262HDMI_PHY_I2CM_CTLINT_ADDR);263264/* Software reset */265hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);266267/* Set Standard Mode speed (determined to be 100KHz on iMX6) */268hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);269270/* Set done, not acknowledged and arbitration interrupt polarities */271hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);272hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,273HDMI_I2CM_CTLINT);274275/* Clear DONE and ERROR interrupts */276hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,277HDMI_IH_I2CM_STAT0);278279/* Mute DONE and ERROR interrupts */280hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,281HDMI_IH_MUTE_I2CM_STAT0);282}283284static bool dw_hdmi_i2c_unwedge(struct dw_hdmi *hdmi)285{286/* If no unwedge state then give up */287if (!hdmi->unwedge_state)288return false;289290dev_info(hdmi->dev, "Attempting to unwedge stuck i2c bus\n");291292/*293* This is a huge hack to workaround a problem where the dw_hdmi i2c294* bus could sometimes get wedged. Once wedged there doesn't appear295* to be any way to unwedge it (including the HDMI_I2CM_SOFTRSTZ)296* other than pulsing the SDA line.297*298* We appear to be able to pulse the SDA line (in the eyes of dw_hdmi)299* by:300* 1. Remux the pin as a GPIO output, driven low.301* 2. Wait a little while. 1 ms seems to work, but we'll do 10.302* 3. Immediately jump to remux the pin as dw_hdmi i2c again.303*304* At the moment of remuxing, the line will still be low due to its305* recent stint as an output, but then it will be pulled high by the306* (presumed) external pullup. dw_hdmi seems to see this as a rising307* edge and that seems to get it out of its jam.308*309* This wedging was only ever seen on one TV, and only on one of310* its HDMI ports. It happened when the TV was powered on while the311* device was plugged in. A scope trace shows the TV bringing both SDA312* and SCL low, then bringing them both back up at roughly the same313* time. Presumably this confuses dw_hdmi because it saw activity but314* no real STOP (maybe it thinks there's another master on the bus?).315* Giving it a clean rising edge of SDA while SCL is already high316* presumably makes dw_hdmi see a STOP which seems to bring dw_hdmi out317* of its stupor.318*319* Note that after coming back alive, transfers seem to immediately320* resume, so if we unwedge due to a timeout we should wait a little321* longer for our transfer to finish, since it might have just started322* now.323*/324pinctrl_select_state(hdmi->pinctrl, hdmi->unwedge_state);325msleep(10);326pinctrl_select_state(hdmi->pinctrl, hdmi->default_state);327328return true;329}330331static int dw_hdmi_i2c_wait(struct dw_hdmi *hdmi)332{333struct dw_hdmi_i2c *i2c = hdmi->i2c;334int stat;335336stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);337if (!stat) {338/* If we can't unwedge, return timeout */339if (!dw_hdmi_i2c_unwedge(hdmi))340return -EAGAIN;341342/* We tried to unwedge; give it another chance */343stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);344if (!stat)345return -EAGAIN;346}347348/* Check for error condition on the bus */349if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)350return -EIO;351352return 0;353}354355static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,356unsigned char *buf, unsigned int length)357{358struct dw_hdmi_i2c *i2c = hdmi->i2c;359int ret;360361if (!i2c->is_regaddr) {362dev_dbg(hdmi->dev, "set read register address to 0\n");363i2c->slave_reg = 0x00;364i2c->is_regaddr = true;365}366367while (length--) {368reinit_completion(&i2c->cmp);369370hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);371if (i2c->is_segment)372hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,373HDMI_I2CM_OPERATION);374else375hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,376HDMI_I2CM_OPERATION);377378ret = dw_hdmi_i2c_wait(hdmi);379if (ret)380return ret;381382*buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);383}384i2c->is_segment = false;385386return 0;387}388389static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,390unsigned char *buf, unsigned int length)391{392struct dw_hdmi_i2c *i2c = hdmi->i2c;393int ret;394395if (!i2c->is_regaddr) {396/* Use the first write byte as register address */397i2c->slave_reg = buf[0];398length--;399buf++;400i2c->is_regaddr = true;401}402403while (length--) {404reinit_completion(&i2c->cmp);405406hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);407hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);408hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,409HDMI_I2CM_OPERATION);410411ret = dw_hdmi_i2c_wait(hdmi);412if (ret)413return ret;414}415416return 0;417}418419static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,420struct i2c_msg *msgs, int num)421{422struct dw_hdmi *hdmi = i2c_get_adapdata(adap);423struct dw_hdmi_i2c *i2c = hdmi->i2c;424u8 addr = msgs[0].addr;425int i, ret = 0;426427if (addr == DDC_CI_ADDR)428/*429* The internal I2C controller does not support the multi-byte430* read and write operations needed for DDC/CI.431* TOFIX: Blacklist the DDC/CI address until we filter out432* unsupported I2C operations.433*/434return -EOPNOTSUPP;435436dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);437438for (i = 0; i < num; i++) {439if (msgs[i].len == 0) {440dev_dbg(hdmi->dev,441"unsupported transfer %d/%d, no data\n",442i + 1, num);443return -EOPNOTSUPP;444}445}446447mutex_lock(&i2c->lock);448449/* Unmute DONE and ERROR interrupts */450hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);451452/* Set slave device address taken from the first I2C message */453hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);454455/* Set slave device register address on transfer */456i2c->is_regaddr = false;457458/* Set segment pointer for I2C extended read mode operation */459i2c->is_segment = false;460461for (i = 0; i < num; i++) {462dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",463i + 1, num, msgs[i].len, msgs[i].flags);464if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {465i2c->is_segment = true;466hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);467hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);468} else {469if (msgs[i].flags & I2C_M_RD)470ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,471msgs[i].len);472else473ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,474msgs[i].len);475}476if (ret < 0)477break;478}479480if (!ret)481ret = num;482483/* Mute DONE and ERROR interrupts */484hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,485HDMI_IH_MUTE_I2CM_STAT0);486487mutex_unlock(&i2c->lock);488489return ret;490}491492static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)493{494return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;495}496497static const struct i2c_algorithm dw_hdmi_algorithm = {498.master_xfer = dw_hdmi_i2c_xfer,499.functionality = dw_hdmi_i2c_func,500};501502static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)503{504struct i2c_adapter *adap;505struct dw_hdmi_i2c *i2c;506int ret;507508i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);509if (!i2c)510return ERR_PTR(-ENOMEM);511512mutex_init(&i2c->lock);513init_completion(&i2c->cmp);514515adap = &i2c->adap;516adap->owner = THIS_MODULE;517adap->dev.parent = hdmi->dev;518adap->algo = &dw_hdmi_algorithm;519strscpy(adap->name, "DesignWare HDMI", sizeof(adap->name));520i2c_set_adapdata(adap, hdmi);521522ret = i2c_add_adapter(adap);523if (ret) {524dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);525devm_kfree(hdmi->dev, i2c);526return ERR_PTR(ret);527}528529hdmi->i2c = i2c;530531dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);532533return adap;534}535536static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,537unsigned int n)538{539/* Must be set/cleared first */540hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);541542/* nshift factor = 0 */543hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);544545/* Use automatic CTS generation mode when CTS is not set */546if (cts)547hdmi_writeb(hdmi, ((cts >> 16) &548HDMI_AUD_CTS3_AUDCTS19_16_MASK) |549HDMI_AUD_CTS3_CTS_MANUAL,550HDMI_AUD_CTS3);551else552hdmi_writeb(hdmi, 0, HDMI_AUD_CTS3);553hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);554hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);555556hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);557hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);558hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);559}560561static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)562{563unsigned int n = (128 * freq) / 1000;564unsigned int mult = 1;565566while (freq > 48000) {567mult *= 2;568freq /= 2;569}570571switch (freq) {572case 32000:573if (pixel_clk == 25175000)574n = 4576;575else if (pixel_clk == 27027000)576n = 4096;577else if (pixel_clk == 74176000 || pixel_clk == 148352000)578n = 11648;579else if (pixel_clk == 297000000)580n = 3072;581else582n = 4096;583n *= mult;584break;585586case 44100:587if (pixel_clk == 25175000)588n = 7007;589else if (pixel_clk == 74176000)590n = 17836;591else if (pixel_clk == 148352000)592n = 8918;593else if (pixel_clk == 297000000)594n = 4704;595else596n = 6272;597n *= mult;598break;599600case 48000:601if (pixel_clk == 25175000)602n = 6864;603else if (pixel_clk == 27027000)604n = 6144;605else if (pixel_clk == 74176000)606n = 11648;607else if (pixel_clk == 148352000)608n = 5824;609else if (pixel_clk == 297000000)610n = 5120;611else612n = 6144;613n *= mult;614break;615616default:617break;618}619620return n;621}622623/*624* When transmitting IEC60958 linear PCM audio, these registers allow to625* configure the channel status information of all the channel status626* bits in the IEC60958 frame. For the moment this configuration is only627* used when the I2S audio interface, General Purpose Audio (GPA),628* or AHB audio DMA (AHBAUDDMA) interface is active629* (for S/PDIF interface this information comes from the stream).630*/631void dw_hdmi_set_channel_status(struct dw_hdmi *hdmi,632u8 *channel_status)633{634/*635* Set channel status register for frequency and word length.636* Use default values for other registers.637*/638hdmi_writeb(hdmi, channel_status[3], HDMI_FC_AUDSCHNLS7);639hdmi_writeb(hdmi, channel_status[4], HDMI_FC_AUDSCHNLS8);640}641EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_status);642643static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,644unsigned long pixel_clk, unsigned int sample_rate)645{646unsigned long ftdms = pixel_clk;647unsigned int n, cts;648u8 config3;649u64 tmp;650651n = hdmi_compute_n(sample_rate, pixel_clk);652653config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);654655/* Compute CTS when using internal AHB audio or General Parallel audio*/656if ((config3 & HDMI_CONFIG3_AHBAUDDMA) || (config3 & HDMI_CONFIG3_GPAUD)) {657/*658* Compute the CTS value from the N value. Note that CTS and N659* can be up to 20 bits in total, so we need 64-bit math. Also660* note that our TDMS clock is not fully accurate; it is661* accurate to kHz. This can introduce an unnecessary remainder662* in the calculation below, so we don't try to warn about that.663*/664tmp = (u64)ftdms * n;665do_div(tmp, 128 * sample_rate);666cts = tmp;667668dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",669__func__, sample_rate,670ftdms / 1000000, (ftdms / 1000) % 1000,671n, cts);672} else {673cts = 0;674}675676spin_lock_irq(&hdmi->audio_lock);677hdmi->audio_n = n;678hdmi->audio_cts = cts;679hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);680spin_unlock_irq(&hdmi->audio_lock);681}682683static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)684{685mutex_lock(&hdmi->audio_mutex);686hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);687mutex_unlock(&hdmi->audio_mutex);688}689690static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)691{692mutex_lock(&hdmi->audio_mutex);693hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,694hdmi->sample_rate);695mutex_unlock(&hdmi->audio_mutex);696}697698void dw_hdmi_set_sample_width(struct dw_hdmi *hdmi, unsigned int width)699{700mutex_lock(&hdmi->audio_mutex);701hdmi->sample_width = width;702mutex_unlock(&hdmi->audio_mutex);703}704EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_width);705706void dw_hdmi_set_sample_non_pcm(struct dw_hdmi *hdmi, unsigned int non_pcm)707{708mutex_lock(&hdmi->audio_mutex);709hdmi->sample_non_pcm = non_pcm;710mutex_unlock(&hdmi->audio_mutex);711}712EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_non_pcm);713714void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)715{716mutex_lock(&hdmi->audio_mutex);717hdmi->sample_rate = rate;718hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,719hdmi->sample_rate);720mutex_unlock(&hdmi->audio_mutex);721}722EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);723724void dw_hdmi_set_channel_count(struct dw_hdmi *hdmi, unsigned int cnt)725{726u8 layout;727728mutex_lock(&hdmi->audio_mutex);729hdmi->channels = cnt;730731/*732* For >2 channel PCM audio, we need to select layout 1733* and set an appropriate channel map.734*/735if (cnt > 2)736layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT1;737else738layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT0;739740hdmi_modb(hdmi, layout, HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_MASK,741HDMI_FC_AUDSCONF);742743/* Set the audio infoframes channel count */744hdmi_modb(hdmi, (cnt - 1) << HDMI_FC_AUDICONF0_CC_OFFSET,745HDMI_FC_AUDICONF0_CC_MASK, HDMI_FC_AUDICONF0);746747mutex_unlock(&hdmi->audio_mutex);748}749EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_count);750751void dw_hdmi_set_channel_allocation(struct dw_hdmi *hdmi, unsigned int ca)752{753mutex_lock(&hdmi->audio_mutex);754755hdmi_writeb(hdmi, ca, HDMI_FC_AUDICONF2);756757mutex_unlock(&hdmi->audio_mutex);758}759EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_allocation);760761static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)762{763if (enable)764hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;765else766hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;767hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);768}769770static u8 *hdmi_audio_get_eld(struct dw_hdmi *hdmi)771{772if (!hdmi->curr_conn)773return NULL;774775return hdmi->curr_conn->eld;776}777778static void dw_hdmi_gp_audio_enable(struct dw_hdmi *hdmi)779{780const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;781int sample_freq = 0x2, org_sample_freq = 0xD;782int ch_mask = BIT(hdmi->channels) - 1;783784switch (hdmi->sample_rate) {785case 32000:786sample_freq = 0x03;787org_sample_freq = 0x0C;788break;789case 44100:790sample_freq = 0x00;791org_sample_freq = 0x0F;792break;793case 48000:794sample_freq = 0x02;795org_sample_freq = 0x0D;796break;797case 88200:798sample_freq = 0x08;799org_sample_freq = 0x07;800break;801case 96000:802sample_freq = 0x0A;803org_sample_freq = 0x05;804break;805case 176400:806sample_freq = 0x0C;807org_sample_freq = 0x03;808break;809case 192000:810sample_freq = 0x0E;811org_sample_freq = 0x01;812break;813default:814break;815}816817hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);818hdmi_enable_audio_clk(hdmi, true);819820hdmi_writeb(hdmi, 0x1, HDMI_FC_AUDSCHNLS0);821hdmi_writeb(hdmi, hdmi->channels, HDMI_FC_AUDSCHNLS2);822hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS3);823hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS4);824hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS5);825hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS6);826hdmi_writeb(hdmi, (0x3 << 4) | sample_freq, HDMI_FC_AUDSCHNLS7);827hdmi_writeb(hdmi, (org_sample_freq << 4) | 0xb, HDMI_FC_AUDSCHNLS8);828829hdmi_writeb(hdmi, ch_mask, HDMI_GP_CONF1);830hdmi_writeb(hdmi, 0x02, HDMI_GP_CONF2);831hdmi_writeb(hdmi, 0x01, HDMI_GP_CONF0);832833hdmi_modb(hdmi, 0x3, 0x3, HDMI_FC_DATAUTO3);834835/* hbr */836if (hdmi->sample_rate == 192000 && hdmi->channels == 8 &&837hdmi->sample_width == 32 && hdmi->sample_non_pcm)838hdmi_modb(hdmi, 0x01, 0x01, HDMI_GP_CONF2);839840if (pdata->enable_audio)841pdata->enable_audio(hdmi,842hdmi->channels,843hdmi->sample_width,844hdmi->sample_rate,845hdmi->sample_non_pcm);846}847848static void dw_hdmi_gp_audio_disable(struct dw_hdmi *hdmi)849{850const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;851852hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);853854hdmi_modb(hdmi, 0, 0x3, HDMI_FC_DATAUTO3);855if (pdata->disable_audio)856pdata->disable_audio(hdmi);857858hdmi_enable_audio_clk(hdmi, false);859}860861static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)862{863hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);864}865866static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)867{868hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);869}870871static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)872{873hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);874hdmi_enable_audio_clk(hdmi, true);875}876877static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)878{879hdmi_enable_audio_clk(hdmi, false);880}881882void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)883{884unsigned long flags;885886spin_lock_irqsave(&hdmi->audio_lock, flags);887hdmi->audio_enable = true;888if (hdmi->enable_audio)889hdmi->enable_audio(hdmi);890spin_unlock_irqrestore(&hdmi->audio_lock, flags);891}892EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);893894void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)895{896unsigned long flags;897898spin_lock_irqsave(&hdmi->audio_lock, flags);899hdmi->audio_enable = false;900if (hdmi->disable_audio)901hdmi->disable_audio(hdmi);902spin_unlock_irqrestore(&hdmi->audio_lock, flags);903}904EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);905906static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)907{908switch (bus_format) {909case MEDIA_BUS_FMT_RGB888_1X24:910case MEDIA_BUS_FMT_RGB101010_1X30:911case MEDIA_BUS_FMT_RGB121212_1X36:912case MEDIA_BUS_FMT_RGB161616_1X48:913return true;914915default:916return false;917}918}919920static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)921{922switch (bus_format) {923case MEDIA_BUS_FMT_YUV8_1X24:924case MEDIA_BUS_FMT_YUV10_1X30:925case MEDIA_BUS_FMT_YUV12_1X36:926case MEDIA_BUS_FMT_YUV16_1X48:927return true;928929default:930return false;931}932}933934static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)935{936switch (bus_format) {937case MEDIA_BUS_FMT_UYVY8_1X16:938case MEDIA_BUS_FMT_UYVY10_1X20:939case MEDIA_BUS_FMT_UYVY12_1X24:940return true;941942default:943return false;944}945}946947static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)948{949switch (bus_format) {950case MEDIA_BUS_FMT_UYYVYY8_0_5X24:951case MEDIA_BUS_FMT_UYYVYY10_0_5X30:952case MEDIA_BUS_FMT_UYYVYY12_0_5X36:953case MEDIA_BUS_FMT_UYYVYY16_0_5X48:954return true;955956default:957return false;958}959}960961static int hdmi_bus_fmt_color_depth(unsigned int bus_format)962{963switch (bus_format) {964case MEDIA_BUS_FMT_RGB888_1X24:965case MEDIA_BUS_FMT_YUV8_1X24:966case MEDIA_BUS_FMT_UYVY8_1X16:967case MEDIA_BUS_FMT_UYYVYY8_0_5X24:968return 8;969970case MEDIA_BUS_FMT_RGB101010_1X30:971case MEDIA_BUS_FMT_YUV10_1X30:972case MEDIA_BUS_FMT_UYVY10_1X20:973case MEDIA_BUS_FMT_UYYVYY10_0_5X30:974return 10;975976case MEDIA_BUS_FMT_RGB121212_1X36:977case MEDIA_BUS_FMT_YUV12_1X36:978case MEDIA_BUS_FMT_UYVY12_1X24:979case MEDIA_BUS_FMT_UYYVYY12_0_5X36:980return 12;981982case MEDIA_BUS_FMT_RGB161616_1X48:983case MEDIA_BUS_FMT_YUV16_1X48:984case MEDIA_BUS_FMT_UYYVYY16_0_5X48:985return 16;986987default:988return 0;989}990}991992/*993* this submodule is responsible for the video data synchronization.994* for example, for RGB 4:4:4 input, the data map is defined as995* pin{47~40} <==> R[7:0]996* pin{31~24} <==> G[7:0]997* pin{15~8} <==> B[7:0]998*/999static void hdmi_video_sample(struct dw_hdmi *hdmi)1000{1001int color_format = 0;1002u8 val;10031004switch (hdmi->hdmi_data.enc_in_bus_format) {1005case MEDIA_BUS_FMT_RGB888_1X24:1006color_format = 0x01;1007break;1008case MEDIA_BUS_FMT_RGB101010_1X30:1009color_format = 0x03;1010break;1011case MEDIA_BUS_FMT_RGB121212_1X36:1012color_format = 0x05;1013break;1014case MEDIA_BUS_FMT_RGB161616_1X48:1015color_format = 0x07;1016break;10171018case MEDIA_BUS_FMT_YUV8_1X24:1019case MEDIA_BUS_FMT_UYYVYY8_0_5X24:1020color_format = 0x09;1021break;1022case MEDIA_BUS_FMT_YUV10_1X30:1023case MEDIA_BUS_FMT_UYYVYY10_0_5X30:1024color_format = 0x0B;1025break;1026case MEDIA_BUS_FMT_YUV12_1X36:1027case MEDIA_BUS_FMT_UYYVYY12_0_5X36:1028color_format = 0x0D;1029break;1030case MEDIA_BUS_FMT_YUV16_1X48:1031case MEDIA_BUS_FMT_UYYVYY16_0_5X48:1032color_format = 0x0F;1033break;10341035case MEDIA_BUS_FMT_UYVY8_1X16:1036color_format = 0x16;1037break;1038case MEDIA_BUS_FMT_UYVY10_1X20:1039color_format = 0x14;1040break;1041case MEDIA_BUS_FMT_UYVY12_1X24:1042color_format = 0x12;1043break;10441045default:1046return;1047}10481049val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |1050((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &1051HDMI_TX_INVID0_VIDEO_MAPPING_MASK);1052hdmi_writeb(hdmi, val, HDMI_TX_INVID0);10531054/* Enable TX stuffing: When DE is inactive, fix the output data to 0 */1055val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |1056HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |1057HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;1058hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);1059hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);1060hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);1061hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);1062hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);1063hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);1064hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);1065}10661067static int is_color_space_conversion(struct dw_hdmi *hdmi)1068{1069struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;1070bool is_input_rgb, is_output_rgb;10711072is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_in_bus_format);1073is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_out_bus_format);10741075return (is_input_rgb != is_output_rgb) ||1076(is_input_rgb && is_output_rgb && hdmi_data->rgb_limited_range);1077}10781079static int is_color_space_decimation(struct dw_hdmi *hdmi)1080{1081if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))1082return 0;10831084if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||1085hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))1086return 1;10871088return 0;1089}10901091static int is_color_space_interpolation(struct dw_hdmi *hdmi)1092{1093if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))1094return 0;10951096if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||1097hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))1098return 1;10991100return 0;1101}11021103static bool is_csc_needed(struct dw_hdmi *hdmi)1104{1105return is_color_space_conversion(hdmi) ||1106is_color_space_decimation(hdmi) ||1107is_color_space_interpolation(hdmi);1108}11091110static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)1111{1112const u16 (*csc_coeff)[3][4] = &csc_coeff_default;1113bool is_input_rgb, is_output_rgb;1114unsigned i;1115u32 csc_scale = 1;11161117is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format);1118is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format);11191120if (!is_input_rgb && is_output_rgb) {1121if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)1122csc_coeff = &csc_coeff_rgb_out_eitu601;1123else1124csc_coeff = &csc_coeff_rgb_out_eitu709;1125} else if (is_input_rgb && !is_output_rgb) {1126if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)1127csc_coeff = &csc_coeff_rgb_in_eitu601;1128else1129csc_coeff = &csc_coeff_rgb_in_eitu709;1130csc_scale = 0;1131} else if (is_input_rgb && is_output_rgb &&1132hdmi->hdmi_data.rgb_limited_range) {1133csc_coeff = &csc_coeff_rgb_full_to_rgb_limited;1134}11351136/* The CSC registers are sequential, alternating MSB then LSB */1137for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {1138u16 coeff_a = (*csc_coeff)[0][i];1139u16 coeff_b = (*csc_coeff)[1][i];1140u16 coeff_c = (*csc_coeff)[2][i];11411142hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);1143hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);1144hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);1145hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);1146hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);1147hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);1148}11491150hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,1151HDMI_CSC_SCALE);1152}11531154static void hdmi_video_csc(struct dw_hdmi *hdmi)1155{1156int color_depth = 0;1157int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;1158int decimation = 0;11591160/* YCC422 interpolation to 444 mode */1161if (is_color_space_interpolation(hdmi))1162interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;1163else if (is_color_space_decimation(hdmi))1164decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;11651166switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {1167case 8:1168color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;1169break;1170case 10:1171color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;1172break;1173case 12:1174color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;1175break;1176case 16:1177color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;1178break;11791180default:1181return;1182}11831184/* Configure the CSC registers */1185hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);1186hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,1187HDMI_CSC_SCALE);11881189dw_hdmi_update_csc_coeffs(hdmi);1190}11911192/*1193* HDMI video packetizer is used to packetize the data.1194* for example, if input is YCC422 mode or repeater is used,1195* data should be repacked this module can be bypassed.1196*/1197static void hdmi_video_packetize(struct dw_hdmi *hdmi)1198{1199unsigned int color_depth = 0;1200unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;1201unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;1202struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;1203u8 val, vp_conf;1204u8 clear_gcp_auto = 0;120512061207if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||1208hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format) ||1209hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {1210switch (hdmi_bus_fmt_color_depth(1211hdmi->hdmi_data.enc_out_bus_format)) {1212case 8:1213color_depth = 4;1214output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;1215clear_gcp_auto = 1;1216break;1217case 10:1218color_depth = 5;1219break;1220case 12:1221color_depth = 6;1222break;1223case 16:1224color_depth = 7;1225break;1226default:1227output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;1228}1229} else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {1230switch (hdmi_bus_fmt_color_depth(1231hdmi->hdmi_data.enc_out_bus_format)) {1232case 0:1233case 8:1234remap_size = HDMI_VP_REMAP_YCC422_16bit;1235clear_gcp_auto = 1;1236break;1237case 10:1238remap_size = HDMI_VP_REMAP_YCC422_20bit;1239break;1240case 12:1241remap_size = HDMI_VP_REMAP_YCC422_24bit;1242break;12431244default:1245return;1246}1247output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;1248} else {1249return;1250}12511252/* set the packetizer registers */1253val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &1254HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |1255((hdmi_data->pix_repet_factor <<1256HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &1257HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);1258hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);12591260/* HDMI1.4b specification section 6.5.3:1261* Source shall only send GCPs with non-zero CD to sinks1262* that indicate support for Deep Color.1263* GCP only transmit CD and do not handle AVMUTE, PP norDefault_Phase (yet).1264* Disable Auto GCP when 24-bit color for sinks that not support Deep Color.1265*/1266val = hdmi_readb(hdmi, HDMI_FC_DATAUTO3);1267if (clear_gcp_auto == 1)1268val &= ~HDMI_FC_DATAUTO3_GCP_AUTO;1269else1270val |= HDMI_FC_DATAUTO3_GCP_AUTO;1271hdmi_writeb(hdmi, val, HDMI_FC_DATAUTO3);12721273hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,1274HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);12751276/* Data from pixel repeater block */1277if (hdmi_data->pix_repet_factor > 1) {1278vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |1279HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;1280} else { /* data from packetizer block */1281vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |1282HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;1283}12841285hdmi_modb(hdmi, vp_conf,1286HDMI_VP_CONF_PR_EN_MASK |1287HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);12881289hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,1290HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);12911292hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);12931294if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {1295vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |1296HDMI_VP_CONF_PP_EN_ENABLE |1297HDMI_VP_CONF_YCC422_EN_DISABLE;1298} else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {1299vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |1300HDMI_VP_CONF_PP_EN_DISABLE |1301HDMI_VP_CONF_YCC422_EN_ENABLE;1302} else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {1303vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |1304HDMI_VP_CONF_PP_EN_DISABLE |1305HDMI_VP_CONF_YCC422_EN_DISABLE;1306} else {1307return;1308}13091310hdmi_modb(hdmi, vp_conf,1311HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |1312HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);13131314hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |1315HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,1316HDMI_VP_STUFF_PP_STUFFING_MASK |1317HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);13181319hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,1320HDMI_VP_CONF);1321}13221323/* -----------------------------------------------------------------------------1324* Synopsys PHY Handling1325*/13261327static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,1328unsigned char bit)1329{1330hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,1331HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);1332}13331334static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)1335{1336u32 val;13371338while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {1339if (msec-- == 0)1340return false;1341udelay(1000);1342}1343hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);13441345return true;1346}13471348void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,1349unsigned char addr)1350{1351hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);1352hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);1353hdmi_writeb(hdmi, (unsigned char)(data >> 8),1354HDMI_PHY_I2CM_DATAO_1_ADDR);1355hdmi_writeb(hdmi, (unsigned char)(data >> 0),1356HDMI_PHY_I2CM_DATAO_0_ADDR);1357hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,1358HDMI_PHY_I2CM_OPERATION_ADDR);1359hdmi_phy_wait_i2c_done(hdmi, 1000);1360}1361EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);13621363/* Filter out invalid setups to avoid configuring SCDC and scrambling */1364static bool dw_hdmi_support_scdc(struct dw_hdmi *hdmi,1365const struct drm_display_info *display)1366{1367/* Completely disable SCDC support for older controllers */1368if (hdmi->version < 0x200a)1369return false;13701371/* Disable if no DDC bus */1372if (!hdmi->ddc)1373return false;13741375/* Disable if SCDC is not supported, or if an HF-VSDB block is absent */1376if (!display->hdmi.scdc.supported ||1377!display->hdmi.scdc.scrambling.supported)1378return false;13791380/*1381* Disable if display only support low TMDS rates and scrambling1382* for low rates is not supported either1383*/1384if (!display->hdmi.scdc.scrambling.low_rates &&1385display->max_tmds_clock <= 340000)1386return false;13871388return true;1389}13901391/*1392* HDMI2.0 Specifies the following procedure for High TMDS Bit Rates:1393* - The Source shall suspend transmission of the TMDS clock and data1394* - The Source shall write to the TMDS_Bit_Clock_Ratio bit to change it1395* from a 0 to a 1 or from a 1 to a 01396* - The Source shall allow a minimum of 1 ms and a maximum of 100 ms from1397* the time the TMDS_Bit_Clock_Ratio bit is written until resuming1398* transmission of TMDS clock and data1399*1400* To respect the 100ms maximum delay, the dw_hdmi_set_high_tmds_clock_ratio()1401* helper should called right before enabling the TMDS Clock and Data in1402* the PHY configuration callback.1403*/1404void dw_hdmi_set_high_tmds_clock_ratio(struct dw_hdmi *hdmi,1405const struct drm_display_info *display)1406{1407unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;14081409/* Control for TMDS Bit Period/TMDS Clock-Period Ratio */1410if (dw_hdmi_support_scdc(hdmi, display)) {1411if (mtmdsclock > HDMI14_MAX_TMDSCLK)1412drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, 1);1413else1414drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, 0);1415}1416}1417EXPORT_SYMBOL_GPL(dw_hdmi_set_high_tmds_clock_ratio);14181419static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)1420{1421hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,1422HDMI_PHY_CONF0_PDZ_OFFSET,1423HDMI_PHY_CONF0_PDZ_MASK);1424}14251426static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)1427{1428hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1429HDMI_PHY_CONF0_ENTMDS_OFFSET,1430HDMI_PHY_CONF0_ENTMDS_MASK);1431}14321433static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)1434{1435hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1436HDMI_PHY_CONF0_SVSRET_OFFSET,1437HDMI_PHY_CONF0_SVSRET_MASK);1438}14391440void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)1441{1442hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1443HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,1444HDMI_PHY_CONF0_GEN2_PDDQ_MASK);1445}1446EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);14471448void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)1449{1450hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1451HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,1452HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);1453}1454EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);14551456static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)1457{1458hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1459HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,1460HDMI_PHY_CONF0_SELDATAENPOL_MASK);1461}14621463static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)1464{1465hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1466HDMI_PHY_CONF0_SELDIPIF_OFFSET,1467HDMI_PHY_CONF0_SELDIPIF_MASK);1468}14691470void dw_hdmi_phy_gen1_reset(struct dw_hdmi *hdmi)1471{1472/* PHY reset. The reset signal is active low on Gen1 PHYs. */1473hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);1474hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);1475}1476EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen1_reset);14771478void dw_hdmi_phy_gen2_reset(struct dw_hdmi *hdmi)1479{1480/* PHY reset. The reset signal is active high on Gen2 PHYs. */1481hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);1482hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);1483}1484EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_reset);14851486void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)1487{1488hdmi_phy_test_clear(hdmi, 1);1489hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);1490hdmi_phy_test_clear(hdmi, 0);1491}1492EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);14931494static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)1495{1496const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1497unsigned int i;1498u16 val;14991500if (phy->gen == 1) {1501dw_hdmi_phy_enable_tmds(hdmi, 0);1502dw_hdmi_phy_enable_powerdown(hdmi, true);1503return;1504}15051506dw_hdmi_phy_gen2_txpwron(hdmi, 0);15071508/*1509* Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went1510* to low power mode.1511*/1512for (i = 0; i < 5; ++i) {1513val = hdmi_readb(hdmi, HDMI_PHY_STAT0);1514if (!(val & HDMI_PHY_TX_PHY_LOCK))1515break;15161517usleep_range(1000, 2000);1518}15191520if (val & HDMI_PHY_TX_PHY_LOCK)1521dev_warn(hdmi->dev, "PHY failed to power down\n");1522else1523dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);15241525dw_hdmi_phy_gen2_pddq(hdmi, 1);1526}15271528static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)1529{1530const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1531unsigned int i;1532u8 val;15331534if (phy->gen == 1) {1535dw_hdmi_phy_enable_powerdown(hdmi, false);15361537/* Toggle TMDS enable. */1538dw_hdmi_phy_enable_tmds(hdmi, 0);1539dw_hdmi_phy_enable_tmds(hdmi, 1);1540return 0;1541}15421543dw_hdmi_phy_gen2_txpwron(hdmi, 1);1544dw_hdmi_phy_gen2_pddq(hdmi, 0);15451546/* Wait for PHY PLL lock */1547for (i = 0; i < 5; ++i) {1548val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;1549if (val)1550break;15511552usleep_range(1000, 2000);1553}15541555if (!val) {1556dev_err(hdmi->dev, "PHY PLL failed to lock\n");1557return -ETIMEDOUT;1558}15591560dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);1561return 0;1562}15631564/*1565* PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available1566* information the DWC MHL PHY has the same register layout and is thus also1567* supported by this function.1568*/1569static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,1570const struct dw_hdmi_plat_data *pdata,1571unsigned long mpixelclock)1572{1573const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;1574const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;1575const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;15761577/* TOFIX Will need 420 specific PHY configuration tables */15781579/* PLL/MPLL Cfg - always match on final entry */1580for (; mpll_config->mpixelclock != ~0UL; mpll_config++)1581if (mpixelclock <= mpll_config->mpixelclock)1582break;15831584for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)1585if (mpixelclock <= curr_ctrl->mpixelclock)1586break;15871588for (; phy_config->mpixelclock != ~0UL; phy_config++)1589if (mpixelclock <= phy_config->mpixelclock)1590break;15911592if (mpll_config->mpixelclock == ~0UL ||1593curr_ctrl->mpixelclock == ~0UL ||1594phy_config->mpixelclock == ~0UL)1595return -EINVAL;15961597dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,1598HDMI_3D_TX_PHY_CPCE_CTRL);1599dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,1600HDMI_3D_TX_PHY_GMPCTRL);1601dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],1602HDMI_3D_TX_PHY_CURRCTRL);16031604dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);1605dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,1606HDMI_3D_TX_PHY_MSM_CTRL);16071608dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);1609dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,1610HDMI_3D_TX_PHY_CKSYMTXCTRL);1611dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,1612HDMI_3D_TX_PHY_VLEVCTRL);16131614/* Override and disable clock termination. */1615dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,1616HDMI_3D_TX_PHY_CKCALCTRL);16171618return 0;1619}16201621static int hdmi_phy_configure(struct dw_hdmi *hdmi,1622const struct drm_display_info *display)1623{1624const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1625const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;1626unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;1627unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;1628int ret;16291630dw_hdmi_phy_power_off(hdmi);16311632dw_hdmi_set_high_tmds_clock_ratio(hdmi, display);16331634/* Leave low power consumption mode by asserting SVSRET. */1635if (phy->has_svsret)1636dw_hdmi_phy_enable_svsret(hdmi, 1);16371638dw_hdmi_phy_gen2_reset(hdmi);16391640hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);16411642dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);16431644/* Write to the PHY as configured by the platform */1645if (pdata->configure_phy)1646ret = pdata->configure_phy(hdmi, pdata->priv_data, mpixelclock);1647else1648ret = phy->configure(hdmi, pdata, mpixelclock);1649if (ret) {1650dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",1651mpixelclock);1652return ret;1653}16541655/* Wait for resuming transmission of TMDS clock and data */1656if (mtmdsclock > HDMI14_MAX_TMDSCLK)1657msleep(100);16581659return dw_hdmi_phy_power_on(hdmi);1660}16611662static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,1663const struct drm_display_info *display,1664const struct drm_display_mode *mode)1665{1666int i, ret;16671668/* HDMI Phy spec says to do the phy initialization sequence twice */1669for (i = 0; i < 2; i++) {1670dw_hdmi_phy_sel_data_en_pol(hdmi, 1);1671dw_hdmi_phy_sel_interface_control(hdmi, 0);16721673ret = hdmi_phy_configure(hdmi, display);1674if (ret)1675return ret;1676}16771678return 0;1679}16801681static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)1682{1683dw_hdmi_phy_power_off(hdmi);1684}16851686enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,1687void *data)1688{1689return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?1690connector_status_connected : connector_status_disconnected;1691}1692EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);16931694void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,1695bool force, bool disabled, bool rxsense)1696{1697u8 old_mask = hdmi->phy_mask;16981699if (force || disabled || !rxsense)1700hdmi->phy_mask |= HDMI_PHY_RX_SENSE;1701else1702hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;17031704if (old_mask != hdmi->phy_mask)1705hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);1706}1707EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);17081709void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)1710{1711/*1712* Configure the PHY RX SENSE and HPD interrupts polarities and clear1713* any pending interrupt.1714*/1715hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);1716hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,1717HDMI_IH_PHY_STAT0);17181719/* Enable cable hot plug irq. */1720hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);17211722/* Clear and unmute interrupts. */1723hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,1724HDMI_IH_PHY_STAT0);1725hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),1726HDMI_IH_MUTE_PHY_STAT0);1727}1728EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);17291730static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {1731.init = dw_hdmi_phy_init,1732.disable = dw_hdmi_phy_disable,1733.read_hpd = dw_hdmi_phy_read_hpd,1734.update_hpd = dw_hdmi_phy_update_hpd,1735.setup_hpd = dw_hdmi_phy_setup_hpd,1736};17371738/* -----------------------------------------------------------------------------1739* HDMI TX Setup1740*/17411742static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)1743{1744u8 de;17451746if (hdmi->hdmi_data.video_mode.mdataenablepolarity)1747de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;1748else1749de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;17501751/* disable rx detect */1752hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,1753HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);17541755hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);17561757hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,1758HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);1759}17601761static void hdmi_config_AVI(struct dw_hdmi *hdmi,1762const struct drm_connector *connector,1763const struct drm_display_mode *mode)1764{1765struct hdmi_avi_infoframe frame;1766u8 val;17671768/* Initialise info frame from DRM mode */1769drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);17701771if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {1772drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,1773hdmi->hdmi_data.rgb_limited_range ?1774HDMI_QUANTIZATION_RANGE_LIMITED :1775HDMI_QUANTIZATION_RANGE_FULL);1776} else {1777frame.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;1778frame.ycc_quantization_range =1779HDMI_YCC_QUANTIZATION_RANGE_LIMITED;1780}17811782if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))1783frame.colorspace = HDMI_COLORSPACE_YUV444;1784else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))1785frame.colorspace = HDMI_COLORSPACE_YUV422;1786else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))1787frame.colorspace = HDMI_COLORSPACE_YUV420;1788else1789frame.colorspace = HDMI_COLORSPACE_RGB;17901791/* Set up colorimetry */1792if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {1793switch (hdmi->hdmi_data.enc_out_encoding) {1794case V4L2_YCBCR_ENC_601:1795if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)1796frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;1797else1798frame.colorimetry = HDMI_COLORIMETRY_ITU_601;1799frame.extended_colorimetry =1800HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1801break;1802case V4L2_YCBCR_ENC_709:1803if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)1804frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;1805else1806frame.colorimetry = HDMI_COLORIMETRY_ITU_709;1807frame.extended_colorimetry =1808HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;1809break;1810default: /* Carries no data */1811frame.colorimetry = HDMI_COLORIMETRY_ITU_601;1812frame.extended_colorimetry =1813HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1814break;1815}1816} else {1817frame.colorimetry = HDMI_COLORIMETRY_NONE;1818frame.extended_colorimetry =1819HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1820}18211822/*1823* The Designware IP uses a different byte format from standard1824* AVI info frames, though generally the bits are in the correct1825* bytes.1826*/18271828/*1829* AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,1830* scan info in bits 4,5 rather than 0,1 and active aspect present in1831* bit 6 rather than 4.1832*/1833val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);1834if (frame.active_aspect & 15)1835val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;1836if (frame.top_bar || frame.bottom_bar)1837val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;1838if (frame.left_bar || frame.right_bar)1839val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;1840hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);18411842/* AVI data byte 2 differences: none */1843val = ((frame.colorimetry & 0x3) << 6) |1844((frame.picture_aspect & 0x3) << 4) |1845(frame.active_aspect & 0xf);1846hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);18471848/* AVI data byte 3 differences: none */1849val = ((frame.extended_colorimetry & 0x7) << 4) |1850((frame.quantization_range & 0x3) << 2) |1851(frame.nups & 0x3);1852if (frame.itc)1853val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;1854hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);18551856/* AVI data byte 4 differences: none */1857val = frame.video_code & 0x7f;1858hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);18591860/* AVI Data Byte 5- set up input and output pixel repetition */1861val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<1862HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &1863HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |1864((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<1865HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &1866HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);1867hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);18681869/*1870* AVI data byte 5 differences: content type in 0,1 rather than 4,5,1871* ycc range in bits 2,3 rather than 6,71872*/1873val = ((frame.ycc_quantization_range & 0x3) << 2) |1874(frame.content_type & 0x3);1875hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);18761877/* AVI Data Bytes 6-13 */1878hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);1879hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);1880hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);1881hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);1882hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);1883hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);1884hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);1885hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);1886}18871888static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,1889const struct drm_connector *connector,1890const struct drm_display_mode *mode)1891{1892struct hdmi_vendor_infoframe frame;1893u8 buffer[10];1894ssize_t err;18951896err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, connector,1897mode);1898if (err < 0)1899/*1900* Going into that statement does not means vendor infoframe1901* fails. It just informed us that vendor infoframe is not1902* needed for the selected mode. Only 4k or stereoscopic 3D1903* mode requires vendor infoframe. So just simply return.1904*/1905return;19061907err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));1908if (err < 0) {1909dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",1910err);1911return;1912}1913hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,1914HDMI_FC_DATAUTO0_VSD_MASK);19151916/* Set the length of HDMI vendor specific InfoFrame payload */1917hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);19181919/* Set 24bit IEEE Registration Identifier */1920hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);1921hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);1922hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);19231924/* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */1925hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);1926hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);19271928if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)1929hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);19301931/* Packet frame interpolation */1932hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);19331934/* Auto packets per frame and line spacing */1935hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);19361937/* Configures the Frame Composer On RDRB mode */1938hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,1939HDMI_FC_DATAUTO0_VSD_MASK);1940}19411942static void hdmi_config_drm_infoframe(struct dw_hdmi *hdmi,1943const struct drm_connector *connector)1944{1945const struct drm_connector_state *conn_state = connector->state;1946struct hdmi_drm_infoframe frame;1947u8 buffer[30];1948ssize_t err;1949int i;19501951if (!hdmi->plat_data->use_drm_infoframe)1952return;19531954hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_DISABLE,1955HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);19561957err = drm_hdmi_infoframe_set_hdr_metadata(&frame, conn_state);1958if (err < 0)1959return;19601961err = hdmi_drm_infoframe_pack(&frame, buffer, sizeof(buffer));1962if (err < 0) {1963dev_err(hdmi->dev, "Failed to pack drm infoframe: %zd\n", err);1964return;1965}19661967hdmi_writeb(hdmi, frame.version, HDMI_FC_DRM_HB0);1968hdmi_writeb(hdmi, frame.length, HDMI_FC_DRM_HB1);19691970for (i = 0; i < frame.length; i++)1971hdmi_writeb(hdmi, buffer[4 + i], HDMI_FC_DRM_PB0 + i);19721973hdmi_writeb(hdmi, 1, HDMI_FC_DRM_UP);1974hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_ENABLE,1975HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);1976}19771978static void hdmi_av_composer(struct dw_hdmi *hdmi,1979const struct drm_display_info *display,1980const struct drm_display_mode *mode)1981{1982u8 inv_val, bytes;1983const struct drm_hdmi_info *hdmi_info = &display->hdmi;1984struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;1985int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;1986unsigned int vdisplay, hdisplay;19871988vmode->mpixelclock = mode->clock * 1000;19891990dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);19911992vmode->mtmdsclock = vmode->mpixelclock;19931994if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {1995switch (hdmi_bus_fmt_color_depth(1996hdmi->hdmi_data.enc_out_bus_format)) {1997case 16:1998vmode->mtmdsclock = vmode->mpixelclock * 2;1999break;2000case 12:2001vmode->mtmdsclock = vmode->mpixelclock * 3 / 2;2002break;2003case 10:2004vmode->mtmdsclock = vmode->mpixelclock * 5 / 4;2005break;2006}2007}20082009if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))2010vmode->mtmdsclock /= 2;20112012dev_dbg(hdmi->dev, "final tmdsclock = %d\n", vmode->mtmdsclock);20132014/* Set up HDMI_FC_INVIDCONF */2015inv_val = (hdmi->hdmi_data.hdcp_enable ||2016(dw_hdmi_support_scdc(hdmi, display) &&2017(vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||2018hdmi_info->scdc.scrambling.low_rates)) ?2019HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :2020HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);20212022inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?2023HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :2024HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;20252026inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?2027HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :2028HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;20292030inv_val |= (vmode->mdataenablepolarity ?2031HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :2032HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);20332034if (hdmi->vic == 39)2035inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;2036else2037inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?2038HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :2039HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;20402041inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?2042HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :2043HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;20442045inv_val |= hdmi->sink_is_hdmi ?2046HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :2047HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;20482049hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);20502051hdisplay = mode->hdisplay;2052hblank = mode->htotal - mode->hdisplay;2053h_de_hs = mode->hsync_start - mode->hdisplay;2054hsync_len = mode->hsync_end - mode->hsync_start;20552056/*2057* When we're setting a YCbCr420 mode, we need2058* to adjust the horizontal timing to suit.2059*/2060if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {2061hdisplay /= 2;2062hblank /= 2;2063h_de_hs /= 2;2064hsync_len /= 2;2065}20662067vdisplay = mode->vdisplay;2068vblank = mode->vtotal - mode->vdisplay;2069v_de_vs = mode->vsync_start - mode->vdisplay;2070vsync_len = mode->vsync_end - mode->vsync_start;20712072/*2073* When we're setting an interlaced mode, we need2074* to adjust the vertical timing to suit.2075*/2076if (mode->flags & DRM_MODE_FLAG_INTERLACE) {2077vdisplay /= 2;2078vblank /= 2;2079v_de_vs /= 2;2080vsync_len /= 2;2081}20822083/* Scrambling Control */2084if (dw_hdmi_support_scdc(hdmi, display)) {2085if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||2086hdmi_info->scdc.scrambling.low_rates) {2087/*2088* HDMI2.0 Specifies the following procedure:2089* After the Source Device has determined that2090* SCDC_Present is set (=1), the Source Device should2091* write the accurate Version of the Source Device2092* to the Source Version field in the SCDCS.2093* Source Devices compliant shall set the2094* Source Version = 1.2095*/2096drm_scdc_readb(hdmi->ddc, SCDC_SINK_VERSION,2097&bytes);2098drm_scdc_writeb(hdmi->ddc, SCDC_SOURCE_VERSION,2099min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION));21002101/* Enabled Scrambling in the Sink */2102drm_scdc_set_scrambling(hdmi->curr_conn, 1);21032104/*2105* To activate the scrambler feature, you must ensure2106* that the quasi-static configuration bit2107* fc_invidconf.HDCP_keepout is set at configuration2108* time, before the required mc_swrstzreq.tmdsswrst_req2109* reset request is issued.2110*/2111hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,2112HDMI_MC_SWRSTZ);2113hdmi_writeb(hdmi, 1, HDMI_FC_SCRAMBLER_CTRL);2114} else {2115hdmi_writeb(hdmi, 0, HDMI_FC_SCRAMBLER_CTRL);2116hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,2117HDMI_MC_SWRSTZ);2118drm_scdc_set_scrambling(hdmi->curr_conn, 0);2119}2120}21212122/* Set up horizontal active pixel width */2123hdmi_writeb(hdmi, hdisplay >> 8, HDMI_FC_INHACTV1);2124hdmi_writeb(hdmi, hdisplay, HDMI_FC_INHACTV0);21252126/* Set up vertical active lines */2127hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);2128hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);21292130/* Set up horizontal blanking pixel region width */2131hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);2132hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);21332134/* Set up vertical blanking pixel region width */2135hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);21362137/* Set up HSYNC active edge delay width (in pixel clks) */2138hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);2139hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);21402141/* Set up VSYNC active edge delay (in lines) */2142hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);21432144/* Set up HSYNC active pulse width (in pixel clks) */2145hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);2146hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);21472148/* Set up VSYNC active edge delay (in lines) */2149hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);2150}21512152/* HDMI Initialization Step B.4 */2153static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)2154{2155/* control period minimum duration */2156hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);2157hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);2158hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);21592160/* Set to fill TMDS data channels */2161hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);2162hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);2163hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);21642165/* Enable pixel clock and tmds data path */2166hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |2167HDMI_MC_CLKDIS_CSCCLK_DISABLE |2168HDMI_MC_CLKDIS_AUDCLK_DISABLE |2169HDMI_MC_CLKDIS_PREPCLK_DISABLE |2170HDMI_MC_CLKDIS_TMDSCLK_DISABLE;2171hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;2172hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21732174hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;2175hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21762177/* Enable csc path */2178if (is_csc_needed(hdmi)) {2179hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;2180hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21812182hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,2183HDMI_MC_FLOWCTRL);2184} else {2185hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CSCCLK_DISABLE;2186hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21872188hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,2189HDMI_MC_FLOWCTRL);2190}2191}21922193/* Workaround to clear the overflow condition */2194static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)2195{2196unsigned int count;2197unsigned int i;2198u8 val;21992200/*2201* Under some circumstances the Frame Composer arithmetic unit can miss2202* an FC register write due to being busy processing the previous one.2203* The issue can be worked around by issuing a TMDS software reset and2204* then write one of the FC registers several times.2205*2206* The number of iterations matters and depends on the HDMI TX revision2207* (and possibly on the platform).2208* 4 iterations for i.MX6Q(v1.30a) and 1 iteration for others.2209* i.MX6DL (v1.31a), Allwinner SoCs (v1.32a), Rockchip RK3288 SoC (v2.00a),2210* Amlogic Meson GX SoCs (v2.01a), RK3328/RK3399 SoCs (v2.11a)2211* and i.MX8MPlus (v2.13a) have been identified as needing the workaround2212* with a single iteration.2213*/22142215switch (hdmi->version) {2216case 0x130a:2217count = 4;2218break;2219default:2220count = 1;2221break;2222}22232224/* TMDS software reset */2225hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);22262227val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);2228for (i = 0; i < count; i++)2229hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);2230}22312232static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)2233{2234hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,2235HDMI_IH_MUTE_FC_STAT2);2236}22372238static int dw_hdmi_setup(struct dw_hdmi *hdmi,2239const struct drm_connector *connector,2240const struct drm_display_mode *mode)2241{2242int ret;22432244hdmi_disable_overflow_interrupts(hdmi);22452246hdmi->vic = drm_match_cea_mode(mode);22472248if (!hdmi->vic) {2249dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");2250} else {2251dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);2252}22532254if ((hdmi->vic == 6) || (hdmi->vic == 7) ||2255(hdmi->vic == 21) || (hdmi->vic == 22) ||2256(hdmi->vic == 2) || (hdmi->vic == 3) ||2257(hdmi->vic == 17) || (hdmi->vic == 18))2258hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;2259else2260hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;22612262hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;2263hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;22642265if (hdmi->hdmi_data.enc_in_bus_format == MEDIA_BUS_FMT_FIXED)2266hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;22672268/* TOFIX: Get input encoding from plat data or fallback to none */2269if (hdmi->plat_data->input_bus_encoding)2270hdmi->hdmi_data.enc_in_encoding =2271hdmi->plat_data->input_bus_encoding;2272else2273hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;22742275if (hdmi->hdmi_data.enc_out_bus_format == MEDIA_BUS_FMT_FIXED)2276hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;22772278hdmi->hdmi_data.rgb_limited_range = hdmi->sink_is_hdmi &&2279drm_default_rgb_quant_range(mode) ==2280HDMI_QUANTIZATION_RANGE_LIMITED;22812282hdmi->hdmi_data.pix_repet_factor = 0;2283hdmi->hdmi_data.hdcp_enable = 0;2284hdmi->hdmi_data.video_mode.mdataenablepolarity = true;22852286/* HDMI Initialization Step B.1 */2287hdmi_av_composer(hdmi, &connector->display_info, mode);22882289/* HDMI Initializateion Step B.2 */2290ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data,2291&connector->display_info,2292&hdmi->previous_mode);2293if (ret)2294return ret;2295hdmi->phy.enabled = true;22962297/* HDMI Initialization Step B.3 */2298dw_hdmi_enable_video_path(hdmi);22992300if (hdmi->sink_has_audio) {2301dev_dbg(hdmi->dev, "sink has audio support\n");23022303/* HDMI Initialization Step E - Configure audio */2304hdmi_clk_regenerator_update_pixel_clock(hdmi);2305hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);2306}23072308/* not for DVI mode */2309if (hdmi->sink_is_hdmi) {2310dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);23112312/* HDMI Initialization Step F - Configure AVI InfoFrame */2313hdmi_config_AVI(hdmi, connector, mode);2314hdmi_config_vendor_specific_infoframe(hdmi, connector, mode);2315hdmi_config_drm_infoframe(hdmi, connector);2316} else {2317dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);2318}23192320hdmi_video_packetize(hdmi);2321hdmi_video_csc(hdmi);2322hdmi_video_sample(hdmi);2323hdmi_tx_hdcp_config(hdmi);23242325dw_hdmi_clear_overflow(hdmi);23262327return 0;2328}23292330static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)2331{2332u8 ih_mute;23332334/*2335* Boot up defaults are:2336* HDMI_IH_MUTE = 0x03 (disabled)2337* HDMI_IH_MUTE_* = 0x00 (enabled)2338*2339* Disable top level interrupt bits in HDMI block2340*/2341ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |2342HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |2343HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;23442345hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);23462347/* by default mask all interrupts */2348hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);2349hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);2350hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);2351hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);2352hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);2353hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);2354hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);2355hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);2356hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);2357hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);2358hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);2359hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);2360hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);2361hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);23622363/* Disable interrupts in the IH_MUTE_* registers */2364hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);2365hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);2366hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);2367hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);2368hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);2369hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);2370hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);2371hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);2372hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);2373hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);23742375/* Enable top level interrupt bits in HDMI block */2376ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |2377HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);2378hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);2379}23802381static void dw_hdmi_poweron(struct dw_hdmi *hdmi)2382{2383hdmi->bridge_is_on = true;23842385/*2386* The curr_conn field is guaranteed to be valid here, as this function2387* is only be called when !hdmi->disabled.2388*/2389dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);2390}23912392static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)2393{2394if (hdmi->phy.enabled) {2395hdmi->phy.ops->disable(hdmi, hdmi->phy.data);2396hdmi->phy.enabled = false;2397}23982399hdmi->bridge_is_on = false;2400}24012402static void dw_hdmi_update_power(struct dw_hdmi *hdmi)2403{2404int force = hdmi->force;24052406if (hdmi->disabled) {2407force = DRM_FORCE_OFF;2408} else if (force == DRM_FORCE_UNSPECIFIED) {2409if (hdmi->rxsense)2410force = DRM_FORCE_ON;2411else2412force = DRM_FORCE_OFF;2413}24142415if (force == DRM_FORCE_OFF) {2416if (hdmi->bridge_is_on)2417dw_hdmi_poweroff(hdmi);2418} else {2419if (!hdmi->bridge_is_on)2420dw_hdmi_poweron(hdmi);2421}2422}24232424/*2425* Adjust the detection of RXSENSE according to whether we have a forced2426* connection mode enabled, or whether we have been disabled. There is2427* no point processing RXSENSE interrupts if we have a forced connection2428* state, or DRM has us disabled.2429*2430* We also disable rxsense interrupts when we think we're disconnected2431* to avoid floating TDMS signals giving false rxsense interrupts.2432*2433* Note: we still need to listen for HPD interrupts even when DRM has us2434* disabled so that we can detect a connect event.2435*/2436static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)2437{2438if (hdmi->phy.ops->update_hpd)2439hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,2440hdmi->force, hdmi->disabled,2441hdmi->rxsense);2442}24432444static enum drm_connector_status dw_hdmi_detect(struct dw_hdmi *hdmi)2445{2446enum drm_connector_status result;24472448result = hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);2449hdmi->last_connector_result = result;24502451return result;2452}24532454static const struct drm_edid *dw_hdmi_edid_read(struct dw_hdmi *hdmi,2455struct drm_connector *connector)2456{2457const struct drm_edid *drm_edid;2458const struct edid *edid;24592460if (!hdmi->ddc)2461return NULL;24622463drm_edid = drm_edid_read_ddc(connector, hdmi->ddc);2464if (!drm_edid) {2465dev_dbg(hdmi->dev, "failed to get edid\n");2466return NULL;2467}24682469/*2470* FIXME: This should use connector->display_info.is_hdmi and2471* connector->display_info.has_audio from a path that has read the EDID2472* and called drm_edid_connector_update().2473*/2474edid = drm_edid_raw(drm_edid);24752476dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",2477edid->width_cm, edid->height_cm);24782479hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);2480hdmi->sink_has_audio = drm_detect_monitor_audio(edid);24812482return drm_edid;2483}24842485/* -----------------------------------------------------------------------------2486* DRM Connector Operations2487*/24882489static enum drm_connector_status2490dw_hdmi_connector_detect(struct drm_connector *connector, bool force)2491{2492struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2493connector);2494return dw_hdmi_detect(hdmi);2495}24962497static int dw_hdmi_connector_get_modes(struct drm_connector *connector)2498{2499struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2500connector);2501const struct drm_edid *drm_edid;2502int ret;25032504drm_edid = dw_hdmi_edid_read(hdmi, connector);25052506drm_edid_connector_update(connector, drm_edid);2507cec_notifier_set_phys_addr(hdmi->cec_notifier,2508connector->display_info.source_physical_address);2509ret = drm_edid_connector_add_modes(connector);2510drm_edid_free(drm_edid);25112512return ret;2513}25142515static int dw_hdmi_connector_atomic_check(struct drm_connector *connector,2516struct drm_atomic_state *state)2517{2518struct drm_connector_state *old_state =2519drm_atomic_get_old_connector_state(state, connector);2520struct drm_connector_state *new_state =2521drm_atomic_get_new_connector_state(state, connector);2522struct drm_crtc *crtc = new_state->crtc;2523struct drm_crtc_state *crtc_state;25242525if (!crtc)2526return 0;25272528if (!drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {2529crtc_state = drm_atomic_get_crtc_state(state, crtc);2530if (IS_ERR(crtc_state))2531return PTR_ERR(crtc_state);25322533crtc_state->mode_changed = true;2534}25352536return 0;2537}25382539static void dw_hdmi_connector_force(struct drm_connector *connector)2540{2541struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2542connector);25432544mutex_lock(&hdmi->mutex);2545hdmi->force = connector->force;2546dw_hdmi_update_power(hdmi);2547dw_hdmi_update_phy_mask(hdmi);2548mutex_unlock(&hdmi->mutex);2549}25502551static const struct drm_connector_funcs dw_hdmi_connector_funcs = {2552.fill_modes = drm_helper_probe_single_connector_modes,2553.detect = dw_hdmi_connector_detect,2554.destroy = drm_connector_cleanup,2555.force = dw_hdmi_connector_force,2556.reset = drm_atomic_helper_connector_reset,2557.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,2558.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,2559};25602561static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {2562.get_modes = dw_hdmi_connector_get_modes,2563.atomic_check = dw_hdmi_connector_atomic_check,2564};25652566static int dw_hdmi_connector_create(struct dw_hdmi *hdmi)2567{2568struct drm_connector *connector = &hdmi->connector;2569struct cec_connector_info conn_info;2570struct cec_notifier *notifier;25712572if (hdmi->version >= 0x200a)2573connector->ycbcr_420_allowed =2574hdmi->plat_data->ycbcr_420_allowed;2575else2576connector->ycbcr_420_allowed = false;25772578connector->interlace_allowed = 1;2579connector->polled = DRM_CONNECTOR_POLL_HPD;25802581drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);25822583drm_connector_init_with_ddc(hdmi->bridge.dev, connector,2584&dw_hdmi_connector_funcs,2585DRM_MODE_CONNECTOR_HDMIA,2586hdmi->ddc);25872588/*2589* drm_connector_attach_max_bpc_property() requires the2590* connector to have a state.2591*/2592drm_atomic_helper_connector_reset(connector);25932594drm_connector_attach_max_bpc_property(connector, 8, 16);25952596if (hdmi->version >= 0x200a && hdmi->plat_data->use_drm_infoframe)2597drm_connector_attach_hdr_output_metadata_property(connector);25982599drm_connector_attach_encoder(connector, hdmi->bridge.encoder);26002601cec_fill_conn_info_from_drm(&conn_info, connector);26022603notifier = cec_notifier_conn_register(hdmi->dev, NULL, &conn_info);2604if (!notifier)2605return -ENOMEM;26062607mutex_lock(&hdmi->cec_notifier_mutex);2608hdmi->cec_notifier = notifier;2609mutex_unlock(&hdmi->cec_notifier_mutex);26102611return 0;2612}26132614/* -----------------------------------------------------------------------------2615* DRM Bridge Operations2616*/26172618/*2619* Possible output formats :2620* - MEDIA_BUS_FMT_UYYVYY16_0_5X48,2621* - MEDIA_BUS_FMT_UYYVYY12_0_5X36,2622* - MEDIA_BUS_FMT_UYYVYY10_0_5X30,2623* - MEDIA_BUS_FMT_UYYVYY8_0_5X24,2624* - MEDIA_BUS_FMT_RGB888_1X24,2625* - MEDIA_BUS_FMT_YUV16_1X48,2626* - MEDIA_BUS_FMT_RGB161616_1X48,2627* - MEDIA_BUS_FMT_UYVY12_1X24,2628* - MEDIA_BUS_FMT_YUV12_1X36,2629* - MEDIA_BUS_FMT_RGB121212_1X36,2630* - MEDIA_BUS_FMT_UYVY10_1X20,2631* - MEDIA_BUS_FMT_YUV10_1X30,2632* - MEDIA_BUS_FMT_RGB101010_1X30,2633* - MEDIA_BUS_FMT_UYVY8_1X16,2634* - MEDIA_BUS_FMT_YUV8_1X24,2635*/26362637/* Can return a maximum of 11 possible output formats for a mode/connector */2638#define MAX_OUTPUT_SEL_FORMATS 1126392640static u32 *dw_hdmi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,2641struct drm_bridge_state *bridge_state,2642struct drm_crtc_state *crtc_state,2643struct drm_connector_state *conn_state,2644unsigned int *num_output_fmts)2645{2646struct drm_connector *conn = conn_state->connector;2647struct drm_display_info *info = &conn->display_info;2648struct drm_display_mode *mode = &crtc_state->mode;2649u8 max_bpc = conn_state->max_requested_bpc;2650bool is_hdmi2_sink = info->hdmi.scdc.supported ||2651(info->color_formats & DRM_COLOR_FORMAT_YCBCR420);2652u32 *output_fmts;2653unsigned int i = 0;26542655*num_output_fmts = 0;26562657output_fmts = kcalloc(MAX_OUTPUT_SEL_FORMATS, sizeof(*output_fmts),2658GFP_KERNEL);2659if (!output_fmts)2660return NULL;26612662/* If dw-hdmi is the first or only bridge, avoid negociating with ourselves */2663if (list_is_singular(&bridge->encoder->bridge_chain) ||2664list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain)) {2665*num_output_fmts = 1;2666output_fmts[0] = MEDIA_BUS_FMT_FIXED;26672668return output_fmts;2669}26702671/*2672* If the current mode enforces 4:2:0, force the output bus format2673* to 4:2:0 and do not add the YUV422/444/RGB formats2674*/2675if (conn->ycbcr_420_allowed &&2676(drm_mode_is_420_only(info, mode) ||2677(is_hdmi2_sink && drm_mode_is_420_also(info, mode)))) {26782679/* Order bus formats from 16bit to 8bit if supported */2680if (max_bpc >= 16 && info->bpc == 16 &&2681(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48))2682output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY16_0_5X48;26832684if (max_bpc >= 12 && info->bpc >= 12 &&2685(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36))2686output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY12_0_5X36;26872688if (max_bpc >= 10 && info->bpc >= 10 &&2689(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30))2690output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY10_0_5X30;26912692/* Default 8bit fallback */2693output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY8_0_5X24;26942695if (drm_mode_is_420_only(info, mode)) {2696*num_output_fmts = i;2697return output_fmts;2698}2699}27002701/*2702* Order bus formats from 16bit to 8bit and from YUV422 to RGB2703* if supported. In any case the default RGB888 format is added2704*/27052706/* Default 8bit RGB fallback */2707output_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;27082709if (max_bpc >= 16 && info->bpc == 16) {2710if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2711output_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;27122713output_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2714}27152716if (max_bpc >= 12 && info->bpc >= 12) {2717if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2718output_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;27192720if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2721output_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;27222723output_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2724}27252726if (max_bpc >= 10 && info->bpc >= 10) {2727if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2728output_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;27292730if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2731output_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;27322733output_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2734}27352736if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2737output_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;27382739if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2740output_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;27412742*num_output_fmts = i;27432744return output_fmts;2745}27462747/*2748* Possible input formats :2749* - MEDIA_BUS_FMT_RGB888_1X242750* - MEDIA_BUS_FMT_YUV8_1X242751* - MEDIA_BUS_FMT_UYVY8_1X162752* - MEDIA_BUS_FMT_UYYVYY8_0_5X242753* - MEDIA_BUS_FMT_RGB101010_1X302754* - MEDIA_BUS_FMT_YUV10_1X302755* - MEDIA_BUS_FMT_UYVY10_1X202756* - MEDIA_BUS_FMT_UYYVYY10_0_5X302757* - MEDIA_BUS_FMT_RGB121212_1X362758* - MEDIA_BUS_FMT_YUV12_1X362759* - MEDIA_BUS_FMT_UYVY12_1X242760* - MEDIA_BUS_FMT_UYYVYY12_0_5X362761* - MEDIA_BUS_FMT_RGB161616_1X482762* - MEDIA_BUS_FMT_YUV16_1X482763* - MEDIA_BUS_FMT_UYYVYY16_0_5X482764*/27652766/* Can return a maximum of 3 possible input formats for an output format */2767#define MAX_INPUT_SEL_FORMATS 327682769static u32 *dw_hdmi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,2770struct drm_bridge_state *bridge_state,2771struct drm_crtc_state *crtc_state,2772struct drm_connector_state *conn_state,2773u32 output_fmt,2774unsigned int *num_input_fmts)2775{2776u32 *input_fmts;2777unsigned int i = 0;27782779*num_input_fmts = 0;27802781input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),2782GFP_KERNEL);2783if (!input_fmts)2784return NULL;27852786switch (output_fmt) {2787/* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */2788case MEDIA_BUS_FMT_FIXED:2789input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2790break;2791/* 8bit */2792case MEDIA_BUS_FMT_RGB888_1X24:2793input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2794input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2795input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2796break;2797case MEDIA_BUS_FMT_YUV8_1X24:2798input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2799input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2800input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2801break;2802case MEDIA_BUS_FMT_UYVY8_1X16:2803input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2804input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2805input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2806break;28072808/* 10bit */2809case MEDIA_BUS_FMT_RGB101010_1X30:2810input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2811input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2812input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2813break;2814case MEDIA_BUS_FMT_YUV10_1X30:2815input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2816input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2817input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2818break;2819case MEDIA_BUS_FMT_UYVY10_1X20:2820input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2821input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2822input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2823break;28242825/* 12bit */2826case MEDIA_BUS_FMT_RGB121212_1X36:2827input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2828input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2829input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2830break;2831case MEDIA_BUS_FMT_YUV12_1X36:2832input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2833input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2834input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2835break;2836case MEDIA_BUS_FMT_UYVY12_1X24:2837input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2838input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2839input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2840break;28412842/* 16bit */2843case MEDIA_BUS_FMT_RGB161616_1X48:2844input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2845input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;2846break;2847case MEDIA_BUS_FMT_YUV16_1X48:2848input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;2849input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2850break;28512852/*YUV 4:2:0 */2853case MEDIA_BUS_FMT_UYYVYY8_0_5X24:2854case MEDIA_BUS_FMT_UYYVYY10_0_5X30:2855case MEDIA_BUS_FMT_UYYVYY12_0_5X36:2856case MEDIA_BUS_FMT_UYYVYY16_0_5X48:2857input_fmts[i++] = output_fmt;2858break;2859}28602861*num_input_fmts = i;28622863if (*num_input_fmts == 0) {2864kfree(input_fmts);2865input_fmts = NULL;2866}28672868return input_fmts;2869}28702871static int dw_hdmi_bridge_atomic_check(struct drm_bridge *bridge,2872struct drm_bridge_state *bridge_state,2873struct drm_crtc_state *crtc_state,2874struct drm_connector_state *conn_state)2875{2876struct dw_hdmi *hdmi = bridge->driver_private;28772878hdmi->hdmi_data.enc_out_bus_format =2879bridge_state->output_bus_cfg.format;28802881hdmi->hdmi_data.enc_in_bus_format =2882bridge_state->input_bus_cfg.format;28832884dev_dbg(hdmi->dev, "input format 0x%04x, output format 0x%04x\n",2885bridge_state->input_bus_cfg.format,2886bridge_state->output_bus_cfg.format);28872888return 0;2889}28902891static int dw_hdmi_bridge_attach(struct drm_bridge *bridge,2892struct drm_encoder *encoder,2893enum drm_bridge_attach_flags flags)2894{2895struct dw_hdmi *hdmi = bridge->driver_private;28962897if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)2898return drm_bridge_attach(encoder, hdmi->next_bridge,2899bridge, flags);29002901return dw_hdmi_connector_create(hdmi);2902}29032904static void dw_hdmi_bridge_detach(struct drm_bridge *bridge)2905{2906struct dw_hdmi *hdmi = bridge->driver_private;29072908mutex_lock(&hdmi->cec_notifier_mutex);2909cec_notifier_conn_unregister(hdmi->cec_notifier);2910hdmi->cec_notifier = NULL;2911mutex_unlock(&hdmi->cec_notifier_mutex);2912}29132914static enum drm_mode_status2915dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,2916const struct drm_display_info *info,2917const struct drm_display_mode *mode)2918{2919struct dw_hdmi *hdmi = bridge->driver_private;2920const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;2921enum drm_mode_status mode_status = MODE_OK;29222923/* We don't support double-clocked modes */2924if (mode->flags & DRM_MODE_FLAG_DBLCLK)2925return MODE_BAD;29262927if (pdata->mode_valid)2928mode_status = pdata->mode_valid(hdmi, pdata->priv_data, info,2929mode);29302931return mode_status;2932}29332934static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,2935const struct drm_display_mode *orig_mode,2936const struct drm_display_mode *mode)2937{2938struct dw_hdmi *hdmi = bridge->driver_private;29392940mutex_lock(&hdmi->mutex);29412942/* Store the display mode for plugin/DKMS poweron events */2943drm_mode_copy(&hdmi->previous_mode, mode);29442945mutex_unlock(&hdmi->mutex);2946}29472948static void dw_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,2949struct drm_atomic_state *state)2950{2951struct dw_hdmi *hdmi = bridge->driver_private;29522953mutex_lock(&hdmi->mutex);2954hdmi->disabled = true;2955hdmi->curr_conn = NULL;2956dw_hdmi_update_power(hdmi);2957dw_hdmi_update_phy_mask(hdmi);2958handle_plugged_change(hdmi, false);2959mutex_unlock(&hdmi->mutex);2960}29612962static void dw_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,2963struct drm_atomic_state *state)2964{2965struct dw_hdmi *hdmi = bridge->driver_private;2966struct drm_connector *connector;29672968connector = drm_atomic_get_new_connector_for_encoder(state,2969bridge->encoder);29702971mutex_lock(&hdmi->mutex);2972hdmi->disabled = false;2973hdmi->curr_conn = connector;2974dw_hdmi_update_power(hdmi);2975dw_hdmi_update_phy_mask(hdmi);2976handle_plugged_change(hdmi, true);2977mutex_unlock(&hdmi->mutex);2978}29792980static enum drm_connector_status2981dw_hdmi_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)2982{2983struct dw_hdmi *hdmi = bridge->driver_private;29842985return dw_hdmi_detect(hdmi);2986}29872988static const struct drm_edid *dw_hdmi_bridge_edid_read(struct drm_bridge *bridge,2989struct drm_connector *connector)2990{2991struct dw_hdmi *hdmi = bridge->driver_private;29922993return dw_hdmi_edid_read(hdmi, connector);2994}29952996static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {2997.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,2998.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,2999.atomic_reset = drm_atomic_helper_bridge_reset,3000.attach = dw_hdmi_bridge_attach,3001.detach = dw_hdmi_bridge_detach,3002.atomic_check = dw_hdmi_bridge_atomic_check,3003.atomic_get_output_bus_fmts = dw_hdmi_bridge_atomic_get_output_bus_fmts,3004.atomic_get_input_bus_fmts = dw_hdmi_bridge_atomic_get_input_bus_fmts,3005.atomic_enable = dw_hdmi_bridge_atomic_enable,3006.atomic_disable = dw_hdmi_bridge_atomic_disable,3007.mode_set = dw_hdmi_bridge_mode_set,3008.mode_valid = dw_hdmi_bridge_mode_valid,3009.detect = dw_hdmi_bridge_detect,3010.edid_read = dw_hdmi_bridge_edid_read,3011};30123013/* -----------------------------------------------------------------------------3014* IRQ Handling3015*/30163017static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)3018{3019struct dw_hdmi_i2c *i2c = hdmi->i2c;3020unsigned int stat;30213022stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);3023if (!stat)3024return IRQ_NONE;30253026hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);30273028i2c->stat = stat;30293030complete(&i2c->cmp);30313032return IRQ_HANDLED;3033}30343035static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)3036{3037struct dw_hdmi *hdmi = dev_id;3038u8 intr_stat;3039irqreturn_t ret = IRQ_NONE;30403041if (hdmi->i2c)3042ret = dw_hdmi_i2c_irq(hdmi);30433044intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);3045if (intr_stat) {3046hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);3047return IRQ_WAKE_THREAD;3048}30493050return ret;3051}30523053void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)3054{3055mutex_lock(&hdmi->mutex);30563057if (!hdmi->force) {3058/*3059* If the RX sense status indicates we're disconnected,3060* clear the software rxsense status.3061*/3062if (!rx_sense)3063hdmi->rxsense = false;30643065/*3066* Only set the software rxsense status when both3067* rxsense and hpd indicates we're connected.3068* This avoids what seems to be bad behaviour in3069* at least iMX6S versions of the phy.3070*/3071if (hpd)3072hdmi->rxsense = true;30733074dw_hdmi_update_power(hdmi);3075dw_hdmi_update_phy_mask(hdmi);3076}3077mutex_unlock(&hdmi->mutex);3078}3079EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);30803081static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)3082{3083struct dw_hdmi *hdmi = dev_id;3084u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;3085enum drm_connector_status status = connector_status_unknown;30863087intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);3088phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);3089phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);30903091phy_pol_mask = 0;3092if (intr_stat & HDMI_IH_PHY_STAT0_HPD)3093phy_pol_mask |= HDMI_PHY_HPD;3094if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)3095phy_pol_mask |= HDMI_PHY_RX_SENSE0;3096if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)3097phy_pol_mask |= HDMI_PHY_RX_SENSE1;3098if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)3099phy_pol_mask |= HDMI_PHY_RX_SENSE2;3100if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)3101phy_pol_mask |= HDMI_PHY_RX_SENSE3;31023103if (phy_pol_mask)3104hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);31053106/*3107* RX sense tells us whether the TDMS transmitters are detecting3108* load - in other words, there's something listening on the3109* other end of the link. Use this to decide whether we should3110* power on the phy as HPD may be toggled by the sink to merely3111* ask the source to re-read the EDID.3112*/3113if (intr_stat &3114(HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {3115dw_hdmi_setup_rx_sense(hdmi,3116phy_stat & HDMI_PHY_HPD,3117phy_stat & HDMI_PHY_RX_SENSE);31183119if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0) {3120mutex_lock(&hdmi->cec_notifier_mutex);3121cec_notifier_phys_addr_invalidate(hdmi->cec_notifier);3122mutex_unlock(&hdmi->cec_notifier_mutex);3123}31243125if (phy_stat & HDMI_PHY_HPD)3126status = connector_status_connected;31273128if (!(phy_stat & (HDMI_PHY_HPD | HDMI_PHY_RX_SENSE)))3129status = connector_status_disconnected;3130}31313132if (status != connector_status_unknown) {3133dev_dbg(hdmi->dev, "EVENT=%s\n",3134status == connector_status_connected ?3135"plugin" : "plugout");31363137if (hdmi->bridge.dev) {3138drm_helper_hpd_irq_event(hdmi->bridge.dev);3139drm_bridge_hpd_notify(&hdmi->bridge, status);3140}3141}31423143hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);3144hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),3145HDMI_IH_MUTE_PHY_STAT0);31463147return IRQ_HANDLED;3148}31493150static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {3151{3152.type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,3153.name = "DWC HDMI TX PHY",3154.gen = 1,3155}, {3156.type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,3157.name = "DWC MHL PHY + HEAC PHY",3158.gen = 2,3159.has_svsret = true,3160.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3161}, {3162.type = DW_HDMI_PHY_DWC_MHL_PHY,3163.name = "DWC MHL PHY",3164.gen = 2,3165.has_svsret = true,3166.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3167}, {3168.type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,3169.name = "DWC HDMI 3D TX PHY + HEAC PHY",3170.gen = 2,3171.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3172}, {3173.type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,3174.name = "DWC HDMI 3D TX PHY",3175.gen = 2,3176.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3177}, {3178.type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,3179.name = "DWC HDMI 2.0 TX PHY",3180.gen = 2,3181.has_svsret = true,3182.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3183}, {3184.type = DW_HDMI_PHY_VENDOR_PHY,3185.name = "Vendor PHY",3186}3187};31883189static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)3190{3191unsigned int i;3192u8 phy_type;31933194phy_type = hdmi->plat_data->phy_force_vendor ?3195DW_HDMI_PHY_VENDOR_PHY :3196hdmi_readb(hdmi, HDMI_CONFIG2_ID);31973198if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {3199/* Vendor PHYs require support from the glue layer. */3200if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {3201dev_err(hdmi->dev,3202"Vendor HDMI PHY not supported by glue layer\n");3203return -ENODEV;3204}32053206hdmi->phy.ops = hdmi->plat_data->phy_ops;3207hdmi->phy.data = hdmi->plat_data->phy_data;3208hdmi->phy.name = hdmi->plat_data->phy_name;3209return 0;3210}32113212/* Synopsys PHYs are handled internally. */3213for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {3214if (dw_hdmi_phys[i].type == phy_type) {3215hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;3216hdmi->phy.name = dw_hdmi_phys[i].name;3217hdmi->phy.data = (void *)&dw_hdmi_phys[i];32183219if (!dw_hdmi_phys[i].configure &&3220!hdmi->plat_data->configure_phy) {3221dev_err(hdmi->dev, "%s requires platform support\n",3222hdmi->phy.name);3223return -ENODEV;3224}32253226return 0;3227}3228}32293230dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);3231return -ENODEV;3232}32333234static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)3235{3236mutex_lock(&hdmi->mutex);3237hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;3238hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);3239mutex_unlock(&hdmi->mutex);3240}32413242static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)3243{3244mutex_lock(&hdmi->mutex);3245hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;3246hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);3247mutex_unlock(&hdmi->mutex);3248}32493250static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {3251.write = hdmi_writeb,3252.read = hdmi_readb,3253.enable = dw_hdmi_cec_enable,3254.disable = dw_hdmi_cec_disable,3255};32563257static const struct regmap_config hdmi_regmap_8bit_config = {3258.reg_bits = 32,3259.val_bits = 8,3260.reg_stride = 1,3261.max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,3262};32633264static const struct regmap_config hdmi_regmap_32bit_config = {3265.reg_bits = 32,3266.val_bits = 32,3267.reg_stride = 4,3268.max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,3269};32703271static void dw_hdmi_init_hw(struct dw_hdmi *hdmi)3272{3273initialize_hdmi_ih_mutes(hdmi);32743275/*3276* Reset HDMI DDC I2C master controller and mute I2CM interrupts.3277* Even if we are using a separate i2c adapter doing this doesn't3278* hurt.3279*/3280dw_hdmi_i2c_init(hdmi);32813282if (hdmi->phy.ops->setup_hpd)3283hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);3284}32853286/* -----------------------------------------------------------------------------3287* Probe/remove API, used from platforms based on the DRM bridge API.3288*/32893290static int dw_hdmi_parse_dt(struct dw_hdmi *hdmi)3291{3292struct device_node *remote;32933294if (!hdmi->plat_data->output_port)3295return 0;329632973298remote = of_graph_get_remote_node(hdmi->dev->of_node,3299hdmi->plat_data->output_port,3300-1);3301if (!remote)3302return -ENODEV;33033304hdmi->next_bridge = of_drm_find_bridge(remote);3305of_node_put(remote);3306if (!hdmi->next_bridge)3307return -EPROBE_DEFER;33083309return 0;3310}33113312bool dw_hdmi_bus_fmt_is_420(struct dw_hdmi *hdmi)3313{3314return hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format);3315}3316EXPORT_SYMBOL_GPL(dw_hdmi_bus_fmt_is_420);33173318struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,3319const struct dw_hdmi_plat_data *plat_data)3320{3321struct device *dev = &pdev->dev;3322struct device_node *np = dev->of_node;3323struct platform_device_info pdevinfo;3324struct device_node *ddc_node;3325struct dw_hdmi_cec_data cec;3326struct dw_hdmi *hdmi;3327struct clk *clk;3328struct resource *iores = NULL;3329int irq;3330int ret;3331u32 val = 1;3332u8 prod_id0;3333u8 prod_id1;3334u8 config0;3335u8 config3;33363337hdmi = devm_drm_bridge_alloc(dev, struct dw_hdmi, bridge, &dw_hdmi_bridge_funcs);3338if (IS_ERR(hdmi))3339return hdmi;33403341hdmi->plat_data = plat_data;3342hdmi->dev = dev;3343hdmi->sample_rate = 48000;3344hdmi->channels = 2;3345hdmi->disabled = true;3346hdmi->rxsense = true;3347hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);3348hdmi->mc_clkdis = 0x7f;3349hdmi->last_connector_result = connector_status_disconnected;33503351mutex_init(&hdmi->mutex);3352mutex_init(&hdmi->audio_mutex);3353mutex_init(&hdmi->cec_notifier_mutex);3354spin_lock_init(&hdmi->audio_lock);33553356ret = dw_hdmi_parse_dt(hdmi);3357if (ret < 0)3358return ERR_PTR(ret);33593360ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);3361if (ddc_node) {3362hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);3363of_node_put(ddc_node);3364if (!hdmi->ddc) {3365dev_dbg(hdmi->dev, "failed to read ddc node\n");3366return ERR_PTR(-EPROBE_DEFER);3367}33683369} else {3370dev_dbg(hdmi->dev, "no ddc property found\n");3371}33723373if (!plat_data->regm) {3374const struct regmap_config *reg_config;33753376of_property_read_u32(np, "reg-io-width", &val);3377switch (val) {3378case 4:3379reg_config = &hdmi_regmap_32bit_config;3380hdmi->reg_shift = 2;3381break;3382case 1:3383reg_config = &hdmi_regmap_8bit_config;3384break;3385default:3386dev_err(dev, "reg-io-width must be 1 or 4\n");3387return ERR_PTR(-EINVAL);3388}33893390iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);3391hdmi->regs = devm_ioremap_resource(dev, iores);3392if (IS_ERR(hdmi->regs)) {3393ret = PTR_ERR(hdmi->regs);3394goto err_res;3395}33963397hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);3398if (IS_ERR(hdmi->regm)) {3399dev_err(dev, "Failed to configure regmap\n");3400ret = PTR_ERR(hdmi->regm);3401goto err_res;3402}3403} else {3404hdmi->regm = plat_data->regm;3405}34063407clk = devm_clk_get_enabled(hdmi->dev, "isfr");3408if (IS_ERR(clk)) {3409ret = PTR_ERR(clk);3410dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);3411goto err_res;3412}34133414clk = devm_clk_get_enabled(hdmi->dev, "iahb");3415if (IS_ERR(clk)) {3416ret = PTR_ERR(clk);3417dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);3418goto err_res;3419}34203421clk = devm_clk_get_optional_enabled(hdmi->dev, "cec");3422if (IS_ERR(clk)) {3423ret = PTR_ERR(clk);3424if (ret != -EPROBE_DEFER)3425dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",3426ret);3427goto err_res;3428}34293430/* Product and revision IDs */3431hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)3432| (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);3433prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);3434prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);34353436if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||3437(prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {3438dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",3439hdmi->version, prod_id0, prod_id1);3440ret = -ENODEV;3441goto err_res;3442}34433444ret = dw_hdmi_detect_phy(hdmi);3445if (ret < 0)3446goto err_res;34473448dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",3449hdmi->version >> 12, hdmi->version & 0xfff,3450prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",3451hdmi->phy.name);34523453dw_hdmi_init_hw(hdmi);34543455irq = platform_get_irq(pdev, 0);3456if (irq < 0) {3457ret = irq;3458goto err_res;3459}34603461ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,3462dw_hdmi_irq, IRQF_SHARED,3463dev_name(dev), hdmi);3464if (ret)3465goto err_res;34663467/*3468* To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator3469* N and cts values before enabling phy3470*/3471hdmi_init_clk_regenerator(hdmi);34723473/* If DDC bus is not specified, try to register HDMI I2C bus */3474if (!hdmi->ddc) {3475/* Look for (optional) stuff related to unwedging */3476hdmi->pinctrl = devm_pinctrl_get(dev);3477if (!IS_ERR(hdmi->pinctrl)) {3478hdmi->unwedge_state =3479pinctrl_lookup_state(hdmi->pinctrl, "unwedge");3480hdmi->default_state =3481pinctrl_lookup_state(hdmi->pinctrl, "default");34823483if (IS_ERR(hdmi->default_state) ||3484IS_ERR(hdmi->unwedge_state)) {3485if (!IS_ERR(hdmi->unwedge_state))3486dev_warn(dev,3487"Unwedge requires default pinctrl\n");3488hdmi->default_state = NULL;3489hdmi->unwedge_state = NULL;3490}3491}34923493hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);3494if (IS_ERR(hdmi->ddc))3495hdmi->ddc = NULL;3496}34973498hdmi->bridge.driver_private = hdmi;3499hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID3500| DRM_BRIDGE_OP_HPD;3501hdmi->bridge.interlace_allowed = true;3502hdmi->bridge.ddc = hdmi->ddc;3503hdmi->bridge.of_node = pdev->dev.of_node;3504hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;35053506if (hdmi->version >= 0x200a)3507hdmi->bridge.ycbcr_420_allowed = plat_data->ycbcr_420_allowed;35083509memset(&pdevinfo, 0, sizeof(pdevinfo));3510pdevinfo.parent = dev;3511pdevinfo.id = PLATFORM_DEVID_AUTO;35123513config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);3514config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);35153516if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {3517struct dw_hdmi_audio_data audio;35183519audio.phys = iores->start;3520audio.base = hdmi->regs;3521audio.irq = irq;3522audio.hdmi = hdmi;3523audio.get_eld = hdmi_audio_get_eld;3524hdmi->enable_audio = dw_hdmi_ahb_audio_enable;3525hdmi->disable_audio = dw_hdmi_ahb_audio_disable;35263527pdevinfo.name = "dw-hdmi-ahb-audio";3528pdevinfo.data = &audio;3529pdevinfo.size_data = sizeof(audio);3530pdevinfo.dma_mask = DMA_BIT_MASK(32);3531hdmi->audio = platform_device_register_full(&pdevinfo);3532} else if (config0 & HDMI_CONFIG0_I2S) {3533struct dw_hdmi_i2s_audio_data audio;35343535audio.hdmi = hdmi;3536audio.get_eld = hdmi_audio_get_eld;3537audio.write = hdmi_writeb;3538audio.read = hdmi_readb;3539hdmi->enable_audio = dw_hdmi_i2s_audio_enable;3540hdmi->disable_audio = dw_hdmi_i2s_audio_disable;35413542pdevinfo.name = "dw-hdmi-i2s-audio";3543pdevinfo.data = &audio;3544pdevinfo.size_data = sizeof(audio);3545pdevinfo.dma_mask = DMA_BIT_MASK(32);3546hdmi->audio = platform_device_register_full(&pdevinfo);3547} else if (iores && config3 & HDMI_CONFIG3_GPAUD) {3548struct dw_hdmi_audio_data audio;35493550audio.phys = iores->start;3551audio.base = hdmi->regs;3552audio.irq = irq;3553audio.hdmi = hdmi;3554audio.get_eld = hdmi_audio_get_eld;35553556hdmi->enable_audio = dw_hdmi_gp_audio_enable;3557hdmi->disable_audio = dw_hdmi_gp_audio_disable;35583559pdevinfo.name = "dw-hdmi-gp-audio";3560pdevinfo.id = PLATFORM_DEVID_NONE;3561pdevinfo.data = &audio;3562pdevinfo.size_data = sizeof(audio);3563pdevinfo.dma_mask = DMA_BIT_MASK(32);3564hdmi->audio = platform_device_register_full(&pdevinfo);3565}35663567if (!plat_data->disable_cec && (config0 & HDMI_CONFIG0_CEC)) {3568cec.hdmi = hdmi;3569cec.ops = &dw_hdmi_cec_ops;3570cec.irq = irq;35713572pdevinfo.name = "dw-hdmi-cec";3573pdevinfo.data = &cec;3574pdevinfo.size_data = sizeof(cec);3575pdevinfo.dma_mask = 0;35763577hdmi->cec = platform_device_register_full(&pdevinfo);3578}35793580drm_bridge_add(&hdmi->bridge);35813582return hdmi;35833584err_res:3585i2c_put_adapter(hdmi->ddc);35863587return ERR_PTR(ret);3588}3589EXPORT_SYMBOL_GPL(dw_hdmi_probe);35903591void dw_hdmi_remove(struct dw_hdmi *hdmi)3592{3593drm_bridge_remove(&hdmi->bridge);35943595if (hdmi->audio && !IS_ERR(hdmi->audio))3596platform_device_unregister(hdmi->audio);3597if (!IS_ERR(hdmi->cec))3598platform_device_unregister(hdmi->cec);35993600/* Disable all interrupts */3601hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);36023603if (hdmi->i2c)3604i2c_del_adapter(&hdmi->i2c->adap);3605else3606i2c_put_adapter(hdmi->ddc);3607}3608EXPORT_SYMBOL_GPL(dw_hdmi_remove);36093610/* -----------------------------------------------------------------------------3611* Bind/unbind API, used from platforms based on the component framework.3612*/3613struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,3614struct drm_encoder *encoder,3615const struct dw_hdmi_plat_data *plat_data)3616{3617struct dw_hdmi *hdmi;3618int ret;36193620hdmi = dw_hdmi_probe(pdev, plat_data);3621if (IS_ERR(hdmi))3622return hdmi;36233624ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL, 0);3625if (ret) {3626dw_hdmi_remove(hdmi);3627return ERR_PTR(ret);3628}36293630return hdmi;3631}3632EXPORT_SYMBOL_GPL(dw_hdmi_bind);36333634void dw_hdmi_unbind(struct dw_hdmi *hdmi)3635{3636dw_hdmi_remove(hdmi);3637}3638EXPORT_SYMBOL_GPL(dw_hdmi_unbind);36393640void dw_hdmi_resume(struct dw_hdmi *hdmi)3641{3642dw_hdmi_init_hw(hdmi);3643}3644EXPORT_SYMBOL_GPL(dw_hdmi_resume);36453646MODULE_AUTHOR("Sascha Hauer <[email protected]>");3647MODULE_AUTHOR("Andy Yan <[email protected]>");3648MODULE_AUTHOR("Yakir Yang <[email protected]>");3649MODULE_AUTHOR("Vladimir Zapolskiy <[email protected]>");3650MODULE_DESCRIPTION("DW HDMI transmitter driver");3651MODULE_LICENSE("GPL");3652MODULE_ALIAS("platform:dw-hdmi");365336543655