Path: blob/master/drivers/gpu/drm/bridge/synopsys/dw-hdmi.c
50920 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;134135unsigned int version;136137struct platform_device *audio;138struct platform_device *cec;139struct device *dev;140struct dw_hdmi_i2c *i2c;141142struct hdmi_data_info hdmi_data;143const struct dw_hdmi_plat_data *plat_data;144145int vic;146147u8 edid[HDMI_EDID_LEN];148149struct {150const struct dw_hdmi_phy_ops *ops;151const char *name;152void *data;153bool enabled;154} phy;155156struct drm_display_mode previous_mode;157158struct i2c_adapter *ddc;159void __iomem *regs;160bool sink_is_hdmi;161bool sink_has_audio;162163struct pinctrl *pinctrl;164struct pinctrl_state *default_state;165struct pinctrl_state *unwedge_state;166167struct mutex mutex; /* for state below and previous_mode */168enum drm_connector_force force; /* mutex-protected force state */169struct drm_connector *curr_conn;/* current connector (only valid when !disabled) */170bool disabled; /* DRM has disabled our bridge */171bool bridge_is_on; /* indicates the bridge is on */172bool rxsense; /* rxsense state */173u8 phy_mask; /* desired phy int mask settings */174u8 mc_clkdis; /* clock disable register */175176spinlock_t audio_lock;177struct mutex audio_mutex;178unsigned int sample_iec958;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};199200const struct dw_hdmi_plat_data *dw_hdmi_to_plat_data(struct dw_hdmi *hdmi)201{202return hdmi->plat_data;203}204EXPORT_SYMBOL_GPL(dw_hdmi_to_plat_data);205206#define HDMI_IH_PHY_STAT0_RX_SENSE \207(HDMI_IH_PHY_STAT0_RX_SENSE0 | HDMI_IH_PHY_STAT0_RX_SENSE1 | \208HDMI_IH_PHY_STAT0_RX_SENSE2 | HDMI_IH_PHY_STAT0_RX_SENSE3)209210#define HDMI_PHY_RX_SENSE \211(HDMI_PHY_RX_SENSE0 | HDMI_PHY_RX_SENSE1 | \212HDMI_PHY_RX_SENSE2 | HDMI_PHY_RX_SENSE3)213214static inline void hdmi_writeb(struct dw_hdmi *hdmi, u8 val, int offset)215{216regmap_write(hdmi->regm, offset << hdmi->reg_shift, val);217}218219static inline u8 hdmi_readb(struct dw_hdmi *hdmi, int offset)220{221unsigned int val = 0;222223regmap_read(hdmi->regm, offset << hdmi->reg_shift, &val);224225return val;226}227228static void handle_plugged_change(struct dw_hdmi *hdmi, bool plugged)229{230if (hdmi->plugged_cb && hdmi->codec_dev)231hdmi->plugged_cb(hdmi->codec_dev, plugged);232}233234int dw_hdmi_set_plugged_cb(struct dw_hdmi *hdmi, hdmi_codec_plugged_cb fn,235struct device *codec_dev)236{237bool plugged;238239mutex_lock(&hdmi->mutex);240hdmi->plugged_cb = fn;241hdmi->codec_dev = codec_dev;242plugged = hdmi->last_connector_result == connector_status_connected;243handle_plugged_change(hdmi, plugged);244mutex_unlock(&hdmi->mutex);245246return 0;247}248EXPORT_SYMBOL_GPL(dw_hdmi_set_plugged_cb);249250static void hdmi_modb(struct dw_hdmi *hdmi, u8 data, u8 mask, unsigned reg)251{252regmap_update_bits(hdmi->regm, reg << hdmi->reg_shift, mask, data);253}254255static void hdmi_mask_writeb(struct dw_hdmi *hdmi, u8 data, unsigned int reg,256u8 shift, u8 mask)257{258hdmi_modb(hdmi, data << shift, mask, reg);259}260261static void dw_hdmi_i2c_init(struct dw_hdmi *hdmi)262{263hdmi_writeb(hdmi, HDMI_PHY_I2CM_INT_ADDR_DONE_POL,264HDMI_PHY_I2CM_INT_ADDR);265266hdmi_writeb(hdmi, HDMI_PHY_I2CM_CTLINT_ADDR_NAC_POL |267HDMI_PHY_I2CM_CTLINT_ADDR_ARBITRATION_POL,268HDMI_PHY_I2CM_CTLINT_ADDR);269270/* Software reset */271hdmi_writeb(hdmi, 0x00, HDMI_I2CM_SOFTRSTZ);272273/* Set Standard Mode speed (determined to be 100KHz on iMX6) */274hdmi_writeb(hdmi, 0x00, HDMI_I2CM_DIV);275276/* Set done, not acknowledged and arbitration interrupt polarities */277hdmi_writeb(hdmi, HDMI_I2CM_INT_DONE_POL, HDMI_I2CM_INT);278hdmi_writeb(hdmi, HDMI_I2CM_CTLINT_NAC_POL | HDMI_I2CM_CTLINT_ARB_POL,279HDMI_I2CM_CTLINT);280281/* Clear DONE and ERROR interrupts */282hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,283HDMI_IH_I2CM_STAT0);284285/* Mute DONE and ERROR interrupts */286hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,287HDMI_IH_MUTE_I2CM_STAT0);288}289290static bool dw_hdmi_i2c_unwedge(struct dw_hdmi *hdmi)291{292/* If no unwedge state then give up */293if (!hdmi->unwedge_state)294return false;295296dev_info(hdmi->dev, "Attempting to unwedge stuck i2c bus\n");297298/*299* This is a huge hack to workaround a problem where the dw_hdmi i2c300* bus could sometimes get wedged. Once wedged there doesn't appear301* to be any way to unwedge it (including the HDMI_I2CM_SOFTRSTZ)302* other than pulsing the SDA line.303*304* We appear to be able to pulse the SDA line (in the eyes of dw_hdmi)305* by:306* 1. Remux the pin as a GPIO output, driven low.307* 2. Wait a little while. 1 ms seems to work, but we'll do 10.308* 3. Immediately jump to remux the pin as dw_hdmi i2c again.309*310* At the moment of remuxing, the line will still be low due to its311* recent stint as an output, but then it will be pulled high by the312* (presumed) external pullup. dw_hdmi seems to see this as a rising313* edge and that seems to get it out of its jam.314*315* This wedging was only ever seen on one TV, and only on one of316* its HDMI ports. It happened when the TV was powered on while the317* device was plugged in. A scope trace shows the TV bringing both SDA318* and SCL low, then bringing them both back up at roughly the same319* time. Presumably this confuses dw_hdmi because it saw activity but320* no real STOP (maybe it thinks there's another master on the bus?).321* Giving it a clean rising edge of SDA while SCL is already high322* presumably makes dw_hdmi see a STOP which seems to bring dw_hdmi out323* of its stupor.324*325* Note that after coming back alive, transfers seem to immediately326* resume, so if we unwedge due to a timeout we should wait a little327* longer for our transfer to finish, since it might have just started328* now.329*/330pinctrl_select_state(hdmi->pinctrl, hdmi->unwedge_state);331msleep(10);332pinctrl_select_state(hdmi->pinctrl, hdmi->default_state);333334return true;335}336337static int dw_hdmi_i2c_wait(struct dw_hdmi *hdmi)338{339struct dw_hdmi_i2c *i2c = hdmi->i2c;340int stat;341342stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);343if (!stat) {344/* If we can't unwedge, return timeout */345if (!dw_hdmi_i2c_unwedge(hdmi))346return -EAGAIN;347348/* We tried to unwedge; give it another chance */349stat = wait_for_completion_timeout(&i2c->cmp, HZ / 10);350if (!stat)351return -EAGAIN;352}353354/* Check for error condition on the bus */355if (i2c->stat & HDMI_IH_I2CM_STAT0_ERROR)356return -EIO;357358return 0;359}360361static int dw_hdmi_i2c_read(struct dw_hdmi *hdmi,362unsigned char *buf, unsigned int length)363{364struct dw_hdmi_i2c *i2c = hdmi->i2c;365int ret;366367if (!i2c->is_regaddr) {368dev_dbg(hdmi->dev, "set read register address to 0\n");369i2c->slave_reg = 0x00;370i2c->is_regaddr = true;371}372373while (length--) {374reinit_completion(&i2c->cmp);375376hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);377if (i2c->is_segment)378hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ_EXT,379HDMI_I2CM_OPERATION);380else381hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_READ,382HDMI_I2CM_OPERATION);383384ret = dw_hdmi_i2c_wait(hdmi);385if (ret)386return ret;387388*buf++ = hdmi_readb(hdmi, HDMI_I2CM_DATAI);389}390i2c->is_segment = false;391392return 0;393}394395static int dw_hdmi_i2c_write(struct dw_hdmi *hdmi,396unsigned char *buf, unsigned int length)397{398struct dw_hdmi_i2c *i2c = hdmi->i2c;399int ret;400401if (!i2c->is_regaddr) {402/* Use the first write byte as register address */403i2c->slave_reg = buf[0];404length--;405buf++;406i2c->is_regaddr = true;407}408409while (length--) {410reinit_completion(&i2c->cmp);411412hdmi_writeb(hdmi, *buf++, HDMI_I2CM_DATAO);413hdmi_writeb(hdmi, i2c->slave_reg++, HDMI_I2CM_ADDRESS);414hdmi_writeb(hdmi, HDMI_I2CM_OPERATION_WRITE,415HDMI_I2CM_OPERATION);416417ret = dw_hdmi_i2c_wait(hdmi);418if (ret)419return ret;420}421422return 0;423}424425static int dw_hdmi_i2c_xfer(struct i2c_adapter *adap,426struct i2c_msg *msgs, int num)427{428struct dw_hdmi *hdmi = i2c_get_adapdata(adap);429struct dw_hdmi_i2c *i2c = hdmi->i2c;430u8 addr = msgs[0].addr;431int i, ret = 0;432433if (addr == DDC_CI_ADDR)434/*435* The internal I2C controller does not support the multi-byte436* read and write operations needed for DDC/CI.437* TOFIX: Blacklist the DDC/CI address until we filter out438* unsupported I2C operations.439*/440return -EOPNOTSUPP;441442dev_dbg(hdmi->dev, "xfer: num: %d, addr: %#x\n", num, addr);443444for (i = 0; i < num; i++) {445if (msgs[i].len == 0) {446dev_dbg(hdmi->dev,447"unsupported transfer %d/%d, no data\n",448i + 1, num);449return -EOPNOTSUPP;450}451}452453mutex_lock(&i2c->lock);454455/* Unmute DONE and ERROR interrupts */456hdmi_writeb(hdmi, 0x00, HDMI_IH_MUTE_I2CM_STAT0);457458/* Set slave device address taken from the first I2C message */459hdmi_writeb(hdmi, addr, HDMI_I2CM_SLAVE);460461/* Set slave device register address on transfer */462i2c->is_regaddr = false;463464/* Set segment pointer for I2C extended read mode operation */465i2c->is_segment = false;466467for (i = 0; i < num; i++) {468dev_dbg(hdmi->dev, "xfer: num: %d/%d, len: %d, flags: %#x\n",469i + 1, num, msgs[i].len, msgs[i].flags);470if (msgs[i].addr == DDC_SEGMENT_ADDR && msgs[i].len == 1) {471i2c->is_segment = true;472hdmi_writeb(hdmi, DDC_SEGMENT_ADDR, HDMI_I2CM_SEGADDR);473hdmi_writeb(hdmi, *msgs[i].buf, HDMI_I2CM_SEGPTR);474} else {475if (msgs[i].flags & I2C_M_RD)476ret = dw_hdmi_i2c_read(hdmi, msgs[i].buf,477msgs[i].len);478else479ret = dw_hdmi_i2c_write(hdmi, msgs[i].buf,480msgs[i].len);481}482if (ret < 0)483break;484}485486if (!ret)487ret = num;488489/* Mute DONE and ERROR interrupts */490hdmi_writeb(hdmi, HDMI_IH_I2CM_STAT0_ERROR | HDMI_IH_I2CM_STAT0_DONE,491HDMI_IH_MUTE_I2CM_STAT0);492493mutex_unlock(&i2c->lock);494495return ret;496}497498static u32 dw_hdmi_i2c_func(struct i2c_adapter *adapter)499{500return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;501}502503static const struct i2c_algorithm dw_hdmi_algorithm = {504.master_xfer = dw_hdmi_i2c_xfer,505.functionality = dw_hdmi_i2c_func,506};507508static struct i2c_adapter *dw_hdmi_i2c_adapter(struct dw_hdmi *hdmi)509{510struct i2c_adapter *adap;511struct dw_hdmi_i2c *i2c;512int ret;513514i2c = devm_kzalloc(hdmi->dev, sizeof(*i2c), GFP_KERNEL);515if (!i2c)516return ERR_PTR(-ENOMEM);517518mutex_init(&i2c->lock);519init_completion(&i2c->cmp);520521adap = &i2c->adap;522adap->owner = THIS_MODULE;523adap->dev.parent = hdmi->dev;524adap->algo = &dw_hdmi_algorithm;525strscpy(adap->name, "DesignWare HDMI", sizeof(adap->name));526i2c_set_adapdata(adap, hdmi);527528ret = i2c_add_adapter(adap);529if (ret) {530dev_warn(hdmi->dev, "cannot add %s I2C adapter\n", adap->name);531devm_kfree(hdmi->dev, i2c);532return ERR_PTR(ret);533}534535hdmi->i2c = i2c;536537dev_info(hdmi->dev, "registered %s I2C bus driver\n", adap->name);538539return adap;540}541542static void hdmi_set_cts_n(struct dw_hdmi *hdmi, unsigned int cts,543unsigned int n)544{545/* Must be set/cleared first */546hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_CTS_MANUAL, HDMI_AUD_CTS3);547548/* nshift factor = 0 */549hdmi_modb(hdmi, 0, HDMI_AUD_CTS3_N_SHIFT_MASK, HDMI_AUD_CTS3);550551/* Use automatic CTS generation mode when CTS is not set */552if (cts)553hdmi_writeb(hdmi, ((cts >> 16) &554HDMI_AUD_CTS3_AUDCTS19_16_MASK) |555HDMI_AUD_CTS3_CTS_MANUAL,556HDMI_AUD_CTS3);557else558hdmi_writeb(hdmi, 0, HDMI_AUD_CTS3);559hdmi_writeb(hdmi, (cts >> 8) & 0xff, HDMI_AUD_CTS2);560hdmi_writeb(hdmi, cts & 0xff, HDMI_AUD_CTS1);561562hdmi_writeb(hdmi, (n >> 16) & 0x0f, HDMI_AUD_N3);563hdmi_writeb(hdmi, (n >> 8) & 0xff, HDMI_AUD_N2);564hdmi_writeb(hdmi, n & 0xff, HDMI_AUD_N1);565}566567static unsigned int hdmi_compute_n(unsigned int freq, unsigned long pixel_clk)568{569unsigned int n = (128 * freq) / 1000;570unsigned int mult = 1;571572while (freq > 48000) {573mult *= 2;574freq /= 2;575}576577switch (freq) {578case 32000:579if (pixel_clk == 25175000)580n = 4576;581else if (pixel_clk == 27027000)582n = 4096;583else if (pixel_clk == 74176000 || pixel_clk == 148352000)584n = 11648;585else if (pixel_clk == 297000000)586n = 3072;587else588n = 4096;589n *= mult;590break;591592case 44100:593if (pixel_clk == 25175000)594n = 7007;595else if (pixel_clk == 74176000)596n = 17836;597else if (pixel_clk == 148352000)598n = 8918;599else if (pixel_clk == 297000000)600n = 4704;601else602n = 6272;603n *= mult;604break;605606case 48000:607if (pixel_clk == 25175000)608n = 6864;609else if (pixel_clk == 27027000)610n = 6144;611else if (pixel_clk == 74176000)612n = 11648;613else if (pixel_clk == 148352000)614n = 5824;615else if (pixel_clk == 297000000)616n = 5120;617else618n = 6144;619n *= mult;620break;621622default:623break;624}625626return n;627}628629/*630* When transmitting IEC60958 linear PCM audio, these registers allow to631* configure the channel status information of all the channel status632* bits in the IEC60958 frame. For the moment this configuration is only633* used when the I2S audio interface, General Purpose Audio (GPA),634* or AHB audio DMA (AHBAUDDMA) interface is active635* (for S/PDIF interface this information comes from the stream).636*/637void dw_hdmi_set_channel_status(struct dw_hdmi *hdmi,638u8 *channel_status)639{640/*641* Set channel status register for frequency and word length.642* Use default values for other registers.643*/644hdmi_writeb(hdmi, channel_status[3], HDMI_FC_AUDSCHNLS7);645hdmi_writeb(hdmi, channel_status[4], HDMI_FC_AUDSCHNLS8);646}647EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_status);648649static void hdmi_set_clk_regenerator(struct dw_hdmi *hdmi,650unsigned long pixel_clk, unsigned int sample_rate)651{652unsigned long ftdms = pixel_clk;653unsigned int n, cts;654u8 config3;655u64 tmp;656657n = hdmi_compute_n(sample_rate, pixel_clk);658659config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);660661/* Compute CTS when using internal AHB audio or General Parallel audio*/662if ((config3 & HDMI_CONFIG3_AHBAUDDMA) || (config3 & HDMI_CONFIG3_GPAUD)) {663/*664* Compute the CTS value from the N value. Note that CTS and N665* can be up to 20 bits in total, so we need 64-bit math. Also666* note that our TDMS clock is not fully accurate; it is667* accurate to kHz. This can introduce an unnecessary remainder668* in the calculation below, so we don't try to warn about that.669*/670tmp = (u64)ftdms * n;671do_div(tmp, 128 * sample_rate);672cts = tmp;673674dev_dbg(hdmi->dev, "%s: fs=%uHz ftdms=%lu.%03luMHz N=%d cts=%d\n",675__func__, sample_rate,676ftdms / 1000000, (ftdms / 1000) % 1000,677n, cts);678} else {679cts = 0;680}681682spin_lock_irq(&hdmi->audio_lock);683hdmi->audio_n = n;684hdmi->audio_cts = cts;685hdmi_set_cts_n(hdmi, cts, hdmi->audio_enable ? n : 0);686spin_unlock_irq(&hdmi->audio_lock);687}688689static void hdmi_init_clk_regenerator(struct dw_hdmi *hdmi)690{691mutex_lock(&hdmi->audio_mutex);692hdmi_set_clk_regenerator(hdmi, 74250000, hdmi->sample_rate);693mutex_unlock(&hdmi->audio_mutex);694}695696static void hdmi_clk_regenerator_update_pixel_clock(struct dw_hdmi *hdmi)697{698mutex_lock(&hdmi->audio_mutex);699hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,700hdmi->sample_rate);701mutex_unlock(&hdmi->audio_mutex);702}703704void dw_hdmi_set_sample_width(struct dw_hdmi *hdmi, unsigned int width)705{706mutex_lock(&hdmi->audio_mutex);707hdmi->sample_width = width;708mutex_unlock(&hdmi->audio_mutex);709}710EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_width);711712void dw_hdmi_set_sample_non_pcm(struct dw_hdmi *hdmi, unsigned int non_pcm)713{714mutex_lock(&hdmi->audio_mutex);715hdmi->sample_non_pcm = non_pcm;716mutex_unlock(&hdmi->audio_mutex);717}718EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_non_pcm);719720void dw_hdmi_set_sample_iec958(struct dw_hdmi *hdmi, unsigned int iec958)721{722mutex_lock(&hdmi->audio_mutex);723hdmi->sample_iec958 = iec958;724mutex_unlock(&hdmi->audio_mutex);725}726EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_iec958);727728void dw_hdmi_set_sample_rate(struct dw_hdmi *hdmi, unsigned int rate)729{730mutex_lock(&hdmi->audio_mutex);731hdmi->sample_rate = rate;732hdmi_set_clk_regenerator(hdmi, hdmi->hdmi_data.video_mode.mtmdsclock,733hdmi->sample_rate);734mutex_unlock(&hdmi->audio_mutex);735}736EXPORT_SYMBOL_GPL(dw_hdmi_set_sample_rate);737738void dw_hdmi_set_channel_count(struct dw_hdmi *hdmi, unsigned int cnt)739{740u8 layout;741742mutex_lock(&hdmi->audio_mutex);743hdmi->channels = cnt;744745/*746* For >2 channel PCM audio, we need to select layout 1747* and set an appropriate channel map.748*/749if (cnt > 2)750layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT1;751else752layout = HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_LAYOUT0;753754hdmi_modb(hdmi, layout, HDMI_FC_AUDSCONF_AUD_PACKET_LAYOUT_MASK,755HDMI_FC_AUDSCONF);756757/* Set the audio infoframes channel count */758hdmi_modb(hdmi, (cnt - 1) << HDMI_FC_AUDICONF0_CC_OFFSET,759HDMI_FC_AUDICONF0_CC_MASK, HDMI_FC_AUDICONF0);760761mutex_unlock(&hdmi->audio_mutex);762}763EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_count);764765void dw_hdmi_set_channel_allocation(struct dw_hdmi *hdmi, unsigned int ca)766{767mutex_lock(&hdmi->audio_mutex);768769hdmi_writeb(hdmi, ca, HDMI_FC_AUDICONF2);770771mutex_unlock(&hdmi->audio_mutex);772}773EXPORT_SYMBOL_GPL(dw_hdmi_set_channel_allocation);774775static void hdmi_enable_audio_clk(struct dw_hdmi *hdmi, bool enable)776{777if (enable)778hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_AUDCLK_DISABLE;779else780hdmi->mc_clkdis |= HDMI_MC_CLKDIS_AUDCLK_DISABLE;781hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);782}783784static u8 *hdmi_audio_get_eld(struct dw_hdmi *hdmi)785{786if (!hdmi->curr_conn)787return NULL;788789return hdmi->curr_conn->eld;790}791792static void dw_hdmi_gp_audio_enable(struct dw_hdmi *hdmi)793{794const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;795int sample_freq = 0x2, org_sample_freq = 0xD;796int ch_mask = BIT(hdmi->channels) - 1;797798switch (hdmi->sample_rate) {799case 32000:800sample_freq = 0x03;801org_sample_freq = 0x0C;802break;803case 44100:804sample_freq = 0x00;805org_sample_freq = 0x0F;806break;807case 48000:808sample_freq = 0x02;809org_sample_freq = 0x0D;810break;811case 88200:812sample_freq = 0x08;813org_sample_freq = 0x07;814break;815case 96000:816sample_freq = 0x0A;817org_sample_freq = 0x05;818break;819case 176400:820sample_freq = 0x0C;821org_sample_freq = 0x03;822break;823case 192000:824sample_freq = 0x0E;825org_sample_freq = 0x01;826break;827default:828break;829}830831hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);832hdmi_enable_audio_clk(hdmi, true);833834hdmi_writeb(hdmi, 0x1, HDMI_FC_AUDSCHNLS0);835hdmi_writeb(hdmi, hdmi->channels, HDMI_FC_AUDSCHNLS2);836hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS3);837hdmi_writeb(hdmi, 0x22, HDMI_FC_AUDSCHNLS4);838hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS5);839hdmi_writeb(hdmi, 0x11, HDMI_FC_AUDSCHNLS6);840hdmi_writeb(hdmi, (0x3 << 4) | sample_freq, HDMI_FC_AUDSCHNLS7);841hdmi_writeb(hdmi, (org_sample_freq << 4) | 0xb, HDMI_FC_AUDSCHNLS8);842843hdmi_writeb(hdmi, ch_mask, HDMI_GP_CONF1);844hdmi_writeb(hdmi, 0x02, HDMI_GP_CONF2);845hdmi_writeb(hdmi, 0x01, HDMI_GP_CONF0);846847hdmi_modb(hdmi, 0x3, 0x3, HDMI_FC_DATAUTO3);848849/* hbr */850if (hdmi->sample_rate == 192000 && hdmi->channels == 8 &&851hdmi->sample_width == 32 && hdmi->sample_non_pcm)852hdmi_modb(hdmi, 0x01, 0x01, HDMI_GP_CONF2);853854if (pdata->enable_audio)855pdata->enable_audio(hdmi,856hdmi->channels,857hdmi->sample_width,858hdmi->sample_rate,859hdmi->sample_non_pcm,860hdmi->sample_iec958);861}862863static void dw_hdmi_gp_audio_disable(struct dw_hdmi *hdmi)864{865const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;866867hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);868869hdmi_modb(hdmi, 0, 0x3, HDMI_FC_DATAUTO3);870if (pdata->disable_audio)871pdata->disable_audio(hdmi);872873hdmi_enable_audio_clk(hdmi, false);874}875876static void dw_hdmi_ahb_audio_enable(struct dw_hdmi *hdmi)877{878hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);879}880881static void dw_hdmi_ahb_audio_disable(struct dw_hdmi *hdmi)882{883hdmi_set_cts_n(hdmi, hdmi->audio_cts, 0);884}885886static void dw_hdmi_i2s_audio_enable(struct dw_hdmi *hdmi)887{888hdmi_set_cts_n(hdmi, hdmi->audio_cts, hdmi->audio_n);889hdmi_enable_audio_clk(hdmi, true);890}891892static void dw_hdmi_i2s_audio_disable(struct dw_hdmi *hdmi)893{894hdmi_enable_audio_clk(hdmi, false);895}896897void dw_hdmi_audio_enable(struct dw_hdmi *hdmi)898{899unsigned long flags;900901spin_lock_irqsave(&hdmi->audio_lock, flags);902hdmi->audio_enable = true;903if (hdmi->enable_audio)904hdmi->enable_audio(hdmi);905spin_unlock_irqrestore(&hdmi->audio_lock, flags);906}907EXPORT_SYMBOL_GPL(dw_hdmi_audio_enable);908909void dw_hdmi_audio_disable(struct dw_hdmi *hdmi)910{911unsigned long flags;912913spin_lock_irqsave(&hdmi->audio_lock, flags);914hdmi->audio_enable = false;915if (hdmi->disable_audio)916hdmi->disable_audio(hdmi);917spin_unlock_irqrestore(&hdmi->audio_lock, flags);918}919EXPORT_SYMBOL_GPL(dw_hdmi_audio_disable);920921static bool hdmi_bus_fmt_is_rgb(unsigned int bus_format)922{923switch (bus_format) {924case MEDIA_BUS_FMT_RGB888_1X24:925case MEDIA_BUS_FMT_RGB101010_1X30:926case MEDIA_BUS_FMT_RGB121212_1X36:927case MEDIA_BUS_FMT_RGB161616_1X48:928return true;929930default:931return false;932}933}934935static bool hdmi_bus_fmt_is_yuv444(unsigned int bus_format)936{937switch (bus_format) {938case MEDIA_BUS_FMT_YUV8_1X24:939case MEDIA_BUS_FMT_YUV10_1X30:940case MEDIA_BUS_FMT_YUV12_1X36:941case MEDIA_BUS_FMT_YUV16_1X48:942return true;943944default:945return false;946}947}948949static bool hdmi_bus_fmt_is_yuv422(unsigned int bus_format)950{951switch (bus_format) {952case MEDIA_BUS_FMT_UYVY8_1X16:953case MEDIA_BUS_FMT_UYVY10_1X20:954case MEDIA_BUS_FMT_UYVY12_1X24:955return true;956957default:958return false;959}960}961962static bool hdmi_bus_fmt_is_yuv420(unsigned int bus_format)963{964switch (bus_format) {965case MEDIA_BUS_FMT_UYYVYY8_0_5X24:966case MEDIA_BUS_FMT_UYYVYY10_0_5X30:967case MEDIA_BUS_FMT_UYYVYY12_0_5X36:968case MEDIA_BUS_FMT_UYYVYY16_0_5X48:969return true;970971default:972return false;973}974}975976static int hdmi_bus_fmt_color_depth(unsigned int bus_format)977{978switch (bus_format) {979case MEDIA_BUS_FMT_RGB888_1X24:980case MEDIA_BUS_FMT_YUV8_1X24:981case MEDIA_BUS_FMT_UYVY8_1X16:982case MEDIA_BUS_FMT_UYYVYY8_0_5X24:983return 8;984985case MEDIA_BUS_FMT_RGB101010_1X30:986case MEDIA_BUS_FMT_YUV10_1X30:987case MEDIA_BUS_FMT_UYVY10_1X20:988case MEDIA_BUS_FMT_UYYVYY10_0_5X30:989return 10;990991case MEDIA_BUS_FMT_RGB121212_1X36:992case MEDIA_BUS_FMT_YUV12_1X36:993case MEDIA_BUS_FMT_UYVY12_1X24:994case MEDIA_BUS_FMT_UYYVYY12_0_5X36:995return 12;996997case MEDIA_BUS_FMT_RGB161616_1X48:998case MEDIA_BUS_FMT_YUV16_1X48:999case MEDIA_BUS_FMT_UYYVYY16_0_5X48:1000return 16;10011002default:1003return 0;1004}1005}10061007/*1008* this submodule is responsible for the video data synchronization.1009* for example, for RGB 4:4:4 input, the data map is defined as1010* pin{47~40} <==> R[7:0]1011* pin{31~24} <==> G[7:0]1012* pin{15~8} <==> B[7:0]1013*/1014static void hdmi_video_sample(struct dw_hdmi *hdmi)1015{1016int color_format = 0;1017u8 val;10181019switch (hdmi->hdmi_data.enc_in_bus_format) {1020case MEDIA_BUS_FMT_RGB888_1X24:1021color_format = 0x01;1022break;1023case MEDIA_BUS_FMT_RGB101010_1X30:1024color_format = 0x03;1025break;1026case MEDIA_BUS_FMT_RGB121212_1X36:1027color_format = 0x05;1028break;1029case MEDIA_BUS_FMT_RGB161616_1X48:1030color_format = 0x07;1031break;10321033case MEDIA_BUS_FMT_YUV8_1X24:1034case MEDIA_BUS_FMT_UYYVYY8_0_5X24:1035color_format = 0x09;1036break;1037case MEDIA_BUS_FMT_YUV10_1X30:1038case MEDIA_BUS_FMT_UYYVYY10_0_5X30:1039color_format = 0x0B;1040break;1041case MEDIA_BUS_FMT_YUV12_1X36:1042case MEDIA_BUS_FMT_UYYVYY12_0_5X36:1043color_format = 0x0D;1044break;1045case MEDIA_BUS_FMT_YUV16_1X48:1046case MEDIA_BUS_FMT_UYYVYY16_0_5X48:1047color_format = 0x0F;1048break;10491050case MEDIA_BUS_FMT_UYVY8_1X16:1051color_format = 0x16;1052break;1053case MEDIA_BUS_FMT_UYVY10_1X20:1054color_format = 0x14;1055break;1056case MEDIA_BUS_FMT_UYVY12_1X24:1057color_format = 0x12;1058break;10591060default:1061return;1062}10631064val = HDMI_TX_INVID0_INTERNAL_DE_GENERATOR_DISABLE |1065((color_format << HDMI_TX_INVID0_VIDEO_MAPPING_OFFSET) &1066HDMI_TX_INVID0_VIDEO_MAPPING_MASK);1067hdmi_writeb(hdmi, val, HDMI_TX_INVID0);10681069/* Enable TX stuffing: When DE is inactive, fix the output data to 0 */1070val = HDMI_TX_INSTUFFING_BDBDATA_STUFFING_ENABLE |1071HDMI_TX_INSTUFFING_RCRDATA_STUFFING_ENABLE |1072HDMI_TX_INSTUFFING_GYDATA_STUFFING_ENABLE;1073hdmi_writeb(hdmi, val, HDMI_TX_INSTUFFING);1074hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA0);1075hdmi_writeb(hdmi, 0x0, HDMI_TX_GYDATA1);1076hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA0);1077hdmi_writeb(hdmi, 0x0, HDMI_TX_RCRDATA1);1078hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA0);1079hdmi_writeb(hdmi, 0x0, HDMI_TX_BCBDATA1);1080}10811082static int is_color_space_conversion(struct dw_hdmi *hdmi)1083{1084struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;1085bool is_input_rgb, is_output_rgb;10861087is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_in_bus_format);1088is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi_data->enc_out_bus_format);10891090return (is_input_rgb != is_output_rgb) ||1091(is_input_rgb && is_output_rgb && hdmi_data->rgb_limited_range);1092}10931094static int is_color_space_decimation(struct dw_hdmi *hdmi)1095{1096if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))1097return 0;10981099if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format) ||1100hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_in_bus_format))1101return 1;11021103return 0;1104}11051106static int is_color_space_interpolation(struct dw_hdmi *hdmi)1107{1108if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_in_bus_format))1109return 0;11101111if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||1112hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))1113return 1;11141115return 0;1116}11171118static bool is_csc_needed(struct dw_hdmi *hdmi)1119{1120return is_color_space_conversion(hdmi) ||1121is_color_space_decimation(hdmi) ||1122is_color_space_interpolation(hdmi);1123}11241125static void dw_hdmi_update_csc_coeffs(struct dw_hdmi *hdmi)1126{1127const u16 (*csc_coeff)[3][4] = &csc_coeff_default;1128bool is_input_rgb, is_output_rgb;1129unsigned i;1130u32 csc_scale = 1;11311132is_input_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_in_bus_format);1133is_output_rgb = hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format);11341135if (!is_input_rgb && is_output_rgb) {1136if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)1137csc_coeff = &csc_coeff_rgb_out_eitu601;1138else1139csc_coeff = &csc_coeff_rgb_out_eitu709;1140} else if (is_input_rgb && !is_output_rgb) {1141if (hdmi->hdmi_data.enc_out_encoding == V4L2_YCBCR_ENC_601)1142csc_coeff = &csc_coeff_rgb_in_eitu601;1143else1144csc_coeff = &csc_coeff_rgb_in_eitu709;1145csc_scale = 0;1146} else if (is_input_rgb && is_output_rgb &&1147hdmi->hdmi_data.rgb_limited_range) {1148csc_coeff = &csc_coeff_rgb_full_to_rgb_limited;1149}11501151/* The CSC registers are sequential, alternating MSB then LSB */1152for (i = 0; i < ARRAY_SIZE(csc_coeff_default[0]); i++) {1153u16 coeff_a = (*csc_coeff)[0][i];1154u16 coeff_b = (*csc_coeff)[1][i];1155u16 coeff_c = (*csc_coeff)[2][i];11561157hdmi_writeb(hdmi, coeff_a & 0xff, HDMI_CSC_COEF_A1_LSB + i * 2);1158hdmi_writeb(hdmi, coeff_a >> 8, HDMI_CSC_COEF_A1_MSB + i * 2);1159hdmi_writeb(hdmi, coeff_b & 0xff, HDMI_CSC_COEF_B1_LSB + i * 2);1160hdmi_writeb(hdmi, coeff_b >> 8, HDMI_CSC_COEF_B1_MSB + i * 2);1161hdmi_writeb(hdmi, coeff_c & 0xff, HDMI_CSC_COEF_C1_LSB + i * 2);1162hdmi_writeb(hdmi, coeff_c >> 8, HDMI_CSC_COEF_C1_MSB + i * 2);1163}11641165hdmi_modb(hdmi, csc_scale, HDMI_CSC_SCALE_CSCSCALE_MASK,1166HDMI_CSC_SCALE);1167}11681169static void hdmi_video_csc(struct dw_hdmi *hdmi)1170{1171int color_depth = 0;1172int interpolation = HDMI_CSC_CFG_INTMODE_DISABLE;1173int decimation = 0;11741175/* YCC422 interpolation to 444 mode */1176if (is_color_space_interpolation(hdmi))1177interpolation = HDMI_CSC_CFG_INTMODE_CHROMA_INT_FORMULA1;1178else if (is_color_space_decimation(hdmi))1179decimation = HDMI_CSC_CFG_DECMODE_CHROMA_INT_FORMULA3;11801181switch (hdmi_bus_fmt_color_depth(hdmi->hdmi_data.enc_out_bus_format)) {1182case 8:1183color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_24BPP;1184break;1185case 10:1186color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_30BPP;1187break;1188case 12:1189color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_36BPP;1190break;1191case 16:1192color_depth = HDMI_CSC_SCALE_CSC_COLORDE_PTH_48BPP;1193break;11941195default:1196return;1197}11981199/* Configure the CSC registers */1200hdmi_writeb(hdmi, interpolation | decimation, HDMI_CSC_CFG);1201hdmi_modb(hdmi, color_depth, HDMI_CSC_SCALE_CSC_COLORDE_PTH_MASK,1202HDMI_CSC_SCALE);12031204dw_hdmi_update_csc_coeffs(hdmi);1205}12061207/*1208* HDMI video packetizer is used to packetize the data.1209* for example, if input is YCC422 mode or repeater is used,1210* data should be repacked this module can be bypassed.1211*/1212static void hdmi_video_packetize(struct dw_hdmi *hdmi)1213{1214unsigned int color_depth = 0;1215unsigned int remap_size = HDMI_VP_REMAP_YCC422_16bit;1216unsigned int output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_PP;1217struct hdmi_data_info *hdmi_data = &hdmi->hdmi_data;1218u8 val, vp_conf;1219u8 clear_gcp_auto = 0;122012211222if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format) ||1223hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format) ||1224hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {1225switch (hdmi_bus_fmt_color_depth(1226hdmi->hdmi_data.enc_out_bus_format)) {1227case 8:1228color_depth = 4;1229output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;1230clear_gcp_auto = 1;1231break;1232case 10:1233color_depth = 5;1234break;1235case 12:1236color_depth = 6;1237break;1238case 16:1239color_depth = 7;1240break;1241default:1242output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS;1243}1244} else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {1245switch (hdmi_bus_fmt_color_depth(1246hdmi->hdmi_data.enc_out_bus_format)) {1247case 0:1248case 8:1249remap_size = HDMI_VP_REMAP_YCC422_16bit;1250clear_gcp_auto = 1;1251break;1252case 10:1253remap_size = HDMI_VP_REMAP_YCC422_20bit;1254break;1255case 12:1256remap_size = HDMI_VP_REMAP_YCC422_24bit;1257break;12581259default:1260return;1261}1262output_select = HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422;1263} else {1264return;1265}12661267/* set the packetizer registers */1268val = ((color_depth << HDMI_VP_PR_CD_COLOR_DEPTH_OFFSET) &1269HDMI_VP_PR_CD_COLOR_DEPTH_MASK) |1270((hdmi_data->pix_repet_factor <<1271HDMI_VP_PR_CD_DESIRED_PR_FACTOR_OFFSET) &1272HDMI_VP_PR_CD_DESIRED_PR_FACTOR_MASK);1273hdmi_writeb(hdmi, val, HDMI_VP_PR_CD);12741275/* HDMI1.4b specification section 6.5.3:1276* Source shall only send GCPs with non-zero CD to sinks1277* that indicate support for Deep Color.1278* GCP only transmit CD and do not handle AVMUTE, PP norDefault_Phase (yet).1279* Disable Auto GCP when 24-bit color for sinks that not support Deep Color.1280*/1281val = hdmi_readb(hdmi, HDMI_FC_DATAUTO3);1282if (clear_gcp_auto == 1)1283val &= ~HDMI_FC_DATAUTO3_GCP_AUTO;1284else1285val |= HDMI_FC_DATAUTO3_GCP_AUTO;1286hdmi_writeb(hdmi, val, HDMI_FC_DATAUTO3);12871288hdmi_modb(hdmi, HDMI_VP_STUFF_PR_STUFFING_STUFFING_MODE,1289HDMI_VP_STUFF_PR_STUFFING_MASK, HDMI_VP_STUFF);12901291/* Data from pixel repeater block */1292if (hdmi_data->pix_repet_factor > 1) {1293vp_conf = HDMI_VP_CONF_PR_EN_ENABLE |1294HDMI_VP_CONF_BYPASS_SELECT_PIX_REPEATER;1295} else { /* data from packetizer block */1296vp_conf = HDMI_VP_CONF_PR_EN_DISABLE |1297HDMI_VP_CONF_BYPASS_SELECT_VID_PACKETIZER;1298}12991300hdmi_modb(hdmi, vp_conf,1301HDMI_VP_CONF_PR_EN_MASK |1302HDMI_VP_CONF_BYPASS_SELECT_MASK, HDMI_VP_CONF);13031304hdmi_modb(hdmi, 1 << HDMI_VP_STUFF_IDEFAULT_PHASE_OFFSET,1305HDMI_VP_STUFF_IDEFAULT_PHASE_MASK, HDMI_VP_STUFF);13061307hdmi_writeb(hdmi, remap_size, HDMI_VP_REMAP);13081309if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_PP) {1310vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |1311HDMI_VP_CONF_PP_EN_ENABLE |1312HDMI_VP_CONF_YCC422_EN_DISABLE;1313} else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_YCC422) {1314vp_conf = HDMI_VP_CONF_BYPASS_EN_DISABLE |1315HDMI_VP_CONF_PP_EN_DISABLE |1316HDMI_VP_CONF_YCC422_EN_ENABLE;1317} else if (output_select == HDMI_VP_CONF_OUTPUT_SELECTOR_BYPASS) {1318vp_conf = HDMI_VP_CONF_BYPASS_EN_ENABLE |1319HDMI_VP_CONF_PP_EN_DISABLE |1320HDMI_VP_CONF_YCC422_EN_DISABLE;1321} else {1322return;1323}13241325hdmi_modb(hdmi, vp_conf,1326HDMI_VP_CONF_BYPASS_EN_MASK | HDMI_VP_CONF_PP_EN_ENMASK |1327HDMI_VP_CONF_YCC422_EN_MASK, HDMI_VP_CONF);13281329hdmi_modb(hdmi, HDMI_VP_STUFF_PP_STUFFING_STUFFING_MODE |1330HDMI_VP_STUFF_YCC422_STUFFING_STUFFING_MODE,1331HDMI_VP_STUFF_PP_STUFFING_MASK |1332HDMI_VP_STUFF_YCC422_STUFFING_MASK, HDMI_VP_STUFF);13331334hdmi_modb(hdmi, output_select, HDMI_VP_CONF_OUTPUT_SELECTOR_MASK,1335HDMI_VP_CONF);1336}13371338/* -----------------------------------------------------------------------------1339* Synopsys PHY Handling1340*/13411342static inline void hdmi_phy_test_clear(struct dw_hdmi *hdmi,1343unsigned char bit)1344{1345hdmi_modb(hdmi, bit << HDMI_PHY_TST0_TSTCLR_OFFSET,1346HDMI_PHY_TST0_TSTCLR_MASK, HDMI_PHY_TST0);1347}13481349static bool hdmi_phy_wait_i2c_done(struct dw_hdmi *hdmi, int msec)1350{1351u32 val;13521353while ((val = hdmi_readb(hdmi, HDMI_IH_I2CMPHY_STAT0) & 0x3) == 0) {1354if (msec-- == 0)1355return false;1356udelay(1000);1357}1358hdmi_writeb(hdmi, val, HDMI_IH_I2CMPHY_STAT0);13591360return true;1361}13621363void dw_hdmi_phy_i2c_write(struct dw_hdmi *hdmi, unsigned short data,1364unsigned char addr)1365{1366hdmi_writeb(hdmi, 0xFF, HDMI_IH_I2CMPHY_STAT0);1367hdmi_writeb(hdmi, addr, HDMI_PHY_I2CM_ADDRESS_ADDR);1368hdmi_writeb(hdmi, (unsigned char)(data >> 8),1369HDMI_PHY_I2CM_DATAO_1_ADDR);1370hdmi_writeb(hdmi, (unsigned char)(data >> 0),1371HDMI_PHY_I2CM_DATAO_0_ADDR);1372hdmi_writeb(hdmi, HDMI_PHY_I2CM_OPERATION_ADDR_WRITE,1373HDMI_PHY_I2CM_OPERATION_ADDR);1374hdmi_phy_wait_i2c_done(hdmi, 1000);1375}1376EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_write);13771378/* Filter out invalid setups to avoid configuring SCDC and scrambling */1379static bool dw_hdmi_support_scdc(struct dw_hdmi *hdmi,1380const struct drm_display_info *display)1381{1382/* Completely disable SCDC support for older controllers */1383if (hdmi->version < 0x200a)1384return false;13851386/* Disable if no DDC bus */1387if (!hdmi->ddc)1388return false;13891390/* Disable if SCDC is not supported, or if an HF-VSDB block is absent */1391if (!display->hdmi.scdc.supported ||1392!display->hdmi.scdc.scrambling.supported)1393return false;13941395/*1396* Disable if display only support low TMDS rates and scrambling1397* for low rates is not supported either1398*/1399if (!display->hdmi.scdc.scrambling.low_rates &&1400display->max_tmds_clock <= 340000)1401return false;14021403return true;1404}14051406/*1407* HDMI2.0 Specifies the following procedure for High TMDS Bit Rates:1408* - The Source shall suspend transmission of the TMDS clock and data1409* - The Source shall write to the TMDS_Bit_Clock_Ratio bit to change it1410* from a 0 to a 1 or from a 1 to a 01411* - The Source shall allow a minimum of 1 ms and a maximum of 100 ms from1412* the time the TMDS_Bit_Clock_Ratio bit is written until resuming1413* transmission of TMDS clock and data1414*1415* To respect the 100ms maximum delay, the dw_hdmi_set_high_tmds_clock_ratio()1416* helper should called right before enabling the TMDS Clock and Data in1417* the PHY configuration callback.1418*/1419void dw_hdmi_set_high_tmds_clock_ratio(struct dw_hdmi *hdmi,1420const struct drm_display_info *display)1421{1422unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;14231424/* Control for TMDS Bit Period/TMDS Clock-Period Ratio */1425if (dw_hdmi_support_scdc(hdmi, display)) {1426if (mtmdsclock > HDMI14_MAX_TMDSCLK)1427drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, 1);1428else1429drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, 0);1430}1431}1432EXPORT_SYMBOL_GPL(dw_hdmi_set_high_tmds_clock_ratio);14331434static void dw_hdmi_phy_enable_powerdown(struct dw_hdmi *hdmi, bool enable)1435{1436hdmi_mask_writeb(hdmi, !enable, HDMI_PHY_CONF0,1437HDMI_PHY_CONF0_PDZ_OFFSET,1438HDMI_PHY_CONF0_PDZ_MASK);1439}14401441static void dw_hdmi_phy_enable_tmds(struct dw_hdmi *hdmi, u8 enable)1442{1443hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1444HDMI_PHY_CONF0_ENTMDS_OFFSET,1445HDMI_PHY_CONF0_ENTMDS_MASK);1446}14471448static void dw_hdmi_phy_enable_svsret(struct dw_hdmi *hdmi, u8 enable)1449{1450hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1451HDMI_PHY_CONF0_SVSRET_OFFSET,1452HDMI_PHY_CONF0_SVSRET_MASK);1453}14541455void dw_hdmi_phy_gen2_pddq(struct dw_hdmi *hdmi, u8 enable)1456{1457hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1458HDMI_PHY_CONF0_GEN2_PDDQ_OFFSET,1459HDMI_PHY_CONF0_GEN2_PDDQ_MASK);1460}1461EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_pddq);14621463void dw_hdmi_phy_gen2_txpwron(struct dw_hdmi *hdmi, u8 enable)1464{1465hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1466HDMI_PHY_CONF0_GEN2_TXPWRON_OFFSET,1467HDMI_PHY_CONF0_GEN2_TXPWRON_MASK);1468}1469EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_txpwron);14701471static void dw_hdmi_phy_sel_data_en_pol(struct dw_hdmi *hdmi, u8 enable)1472{1473hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1474HDMI_PHY_CONF0_SELDATAENPOL_OFFSET,1475HDMI_PHY_CONF0_SELDATAENPOL_MASK);1476}14771478static void dw_hdmi_phy_sel_interface_control(struct dw_hdmi *hdmi, u8 enable)1479{1480hdmi_mask_writeb(hdmi, enable, HDMI_PHY_CONF0,1481HDMI_PHY_CONF0_SELDIPIF_OFFSET,1482HDMI_PHY_CONF0_SELDIPIF_MASK);1483}14841485void dw_hdmi_phy_gen1_reset(struct dw_hdmi *hdmi)1486{1487/* PHY reset. The reset signal is active low on Gen1 PHYs. */1488hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);1489hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);1490}1491EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen1_reset);14921493void dw_hdmi_phy_gen2_reset(struct dw_hdmi *hdmi)1494{1495/* PHY reset. The reset signal is active high on Gen2 PHYs. */1496hdmi_writeb(hdmi, HDMI_MC_PHYRSTZ_PHYRSTZ, HDMI_MC_PHYRSTZ);1497hdmi_writeb(hdmi, 0, HDMI_MC_PHYRSTZ);1498}1499EXPORT_SYMBOL_GPL(dw_hdmi_phy_gen2_reset);15001501void dw_hdmi_phy_i2c_set_addr(struct dw_hdmi *hdmi, u8 address)1502{1503hdmi_phy_test_clear(hdmi, 1);1504hdmi_writeb(hdmi, address, HDMI_PHY_I2CM_SLAVE_ADDR);1505hdmi_phy_test_clear(hdmi, 0);1506}1507EXPORT_SYMBOL_GPL(dw_hdmi_phy_i2c_set_addr);15081509static void dw_hdmi_phy_power_off(struct dw_hdmi *hdmi)1510{1511const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1512unsigned int i;1513u16 val;15141515if (phy->gen == 1) {1516dw_hdmi_phy_enable_tmds(hdmi, 0);1517dw_hdmi_phy_enable_powerdown(hdmi, true);1518return;1519}15201521dw_hdmi_phy_gen2_txpwron(hdmi, 0);15221523/*1524* Wait for TX_PHY_LOCK to be deasserted to indicate that the PHY went1525* to low power mode.1526*/1527for (i = 0; i < 5; ++i) {1528val = hdmi_readb(hdmi, HDMI_PHY_STAT0);1529if (!(val & HDMI_PHY_TX_PHY_LOCK))1530break;15311532usleep_range(1000, 2000);1533}15341535if (val & HDMI_PHY_TX_PHY_LOCK)1536dev_warn(hdmi->dev, "PHY failed to power down\n");1537else1538dev_dbg(hdmi->dev, "PHY powered down in %u iterations\n", i);15391540dw_hdmi_phy_gen2_pddq(hdmi, 1);1541}15421543static int dw_hdmi_phy_power_on(struct dw_hdmi *hdmi)1544{1545const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1546unsigned int i;1547u8 val;15481549if (phy->gen == 1) {1550dw_hdmi_phy_enable_powerdown(hdmi, false);15511552/* Toggle TMDS enable. */1553dw_hdmi_phy_enable_tmds(hdmi, 0);1554dw_hdmi_phy_enable_tmds(hdmi, 1);1555return 0;1556}15571558dw_hdmi_phy_gen2_txpwron(hdmi, 1);1559dw_hdmi_phy_gen2_pddq(hdmi, 0);15601561/* Wait for PHY PLL lock */1562for (i = 0; i < 5; ++i) {1563val = hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_TX_PHY_LOCK;1564if (val)1565break;15661567usleep_range(1000, 2000);1568}15691570if (!val) {1571dev_err(hdmi->dev, "PHY PLL failed to lock\n");1572return -ETIMEDOUT;1573}15741575dev_dbg(hdmi->dev, "PHY PLL locked %u iterations\n", i);1576return 0;1577}15781579/*1580* PHY configuration function for the DWC HDMI 3D TX PHY. Based on the available1581* information the DWC MHL PHY has the same register layout and is thus also1582* supported by this function.1583*/1584static int hdmi_phy_configure_dwc_hdmi_3d_tx(struct dw_hdmi *hdmi,1585const struct dw_hdmi_plat_data *pdata,1586unsigned long mpixelclock)1587{1588const struct dw_hdmi_mpll_config *mpll_config = pdata->mpll_cfg;1589const struct dw_hdmi_curr_ctrl *curr_ctrl = pdata->cur_ctr;1590const struct dw_hdmi_phy_config *phy_config = pdata->phy_config;15911592/* TOFIX Will need 420 specific PHY configuration tables */15931594/* PLL/MPLL Cfg - always match on final entry */1595for (; mpll_config->mpixelclock != ~0UL; mpll_config++)1596if (mpixelclock <= mpll_config->mpixelclock)1597break;15981599for (; curr_ctrl->mpixelclock != ~0UL; curr_ctrl++)1600if (mpixelclock <= curr_ctrl->mpixelclock)1601break;16021603for (; phy_config->mpixelclock != ~0UL; phy_config++)1604if (mpixelclock <= phy_config->mpixelclock)1605break;16061607if (mpll_config->mpixelclock == ~0UL ||1608curr_ctrl->mpixelclock == ~0UL ||1609phy_config->mpixelclock == ~0UL)1610return -EINVAL;16111612dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].cpce,1613HDMI_3D_TX_PHY_CPCE_CTRL);1614dw_hdmi_phy_i2c_write(hdmi, mpll_config->res[0].gmp,1615HDMI_3D_TX_PHY_GMPCTRL);1616dw_hdmi_phy_i2c_write(hdmi, curr_ctrl->curr[0],1617HDMI_3D_TX_PHY_CURRCTRL);16181619dw_hdmi_phy_i2c_write(hdmi, 0, HDMI_3D_TX_PHY_PLLPHBYCTRL);1620dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_MSM_CTRL_CKO_SEL_FB_CLK,1621HDMI_3D_TX_PHY_MSM_CTRL);16221623dw_hdmi_phy_i2c_write(hdmi, phy_config->term, HDMI_3D_TX_PHY_TXTERM);1624dw_hdmi_phy_i2c_write(hdmi, phy_config->sym_ctr,1625HDMI_3D_TX_PHY_CKSYMTXCTRL);1626dw_hdmi_phy_i2c_write(hdmi, phy_config->vlev_ctr,1627HDMI_3D_TX_PHY_VLEVCTRL);16281629/* Override and disable clock termination. */1630dw_hdmi_phy_i2c_write(hdmi, HDMI_3D_TX_PHY_CKCALCTRL_OVERRIDE,1631HDMI_3D_TX_PHY_CKCALCTRL);16321633return 0;1634}16351636static int hdmi_phy_configure(struct dw_hdmi *hdmi,1637const struct drm_display_info *display)1638{1639const struct dw_hdmi_phy_data *phy = hdmi->phy.data;1640const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;1641unsigned long mpixelclock = hdmi->hdmi_data.video_mode.mpixelclock;1642unsigned long mtmdsclock = hdmi->hdmi_data.video_mode.mtmdsclock;1643int ret;16441645dw_hdmi_phy_power_off(hdmi);16461647dw_hdmi_set_high_tmds_clock_ratio(hdmi, display);16481649/* Leave low power consumption mode by asserting SVSRET. */1650if (phy->has_svsret)1651dw_hdmi_phy_enable_svsret(hdmi, 1);16521653dw_hdmi_phy_gen2_reset(hdmi);16541655hdmi_writeb(hdmi, HDMI_MC_HEACPHY_RST_ASSERT, HDMI_MC_HEACPHY_RST);16561657dw_hdmi_phy_i2c_set_addr(hdmi, HDMI_PHY_I2CM_SLAVE_ADDR_PHY_GEN2);16581659/* Write to the PHY as configured by the platform */1660if (pdata->configure_phy)1661ret = pdata->configure_phy(hdmi, pdata->priv_data, mpixelclock);1662else1663ret = phy->configure(hdmi, pdata, mpixelclock);1664if (ret) {1665dev_err(hdmi->dev, "PHY configuration failed (clock %lu)\n",1666mpixelclock);1667return ret;1668}16691670/* Wait for resuming transmission of TMDS clock and data */1671if (mtmdsclock > HDMI14_MAX_TMDSCLK)1672msleep(100);16731674return dw_hdmi_phy_power_on(hdmi);1675}16761677static int dw_hdmi_phy_init(struct dw_hdmi *hdmi, void *data,1678const struct drm_display_info *display,1679const struct drm_display_mode *mode)1680{1681int i, ret;16821683/* HDMI Phy spec says to do the phy initialization sequence twice */1684for (i = 0; i < 2; i++) {1685dw_hdmi_phy_sel_data_en_pol(hdmi, 1);1686dw_hdmi_phy_sel_interface_control(hdmi, 0);16871688ret = hdmi_phy_configure(hdmi, display);1689if (ret)1690return ret;1691}16921693return 0;1694}16951696static void dw_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)1697{1698dw_hdmi_phy_power_off(hdmi);1699}17001701enum drm_connector_status dw_hdmi_phy_read_hpd(struct dw_hdmi *hdmi,1702void *data)1703{1704return hdmi_readb(hdmi, HDMI_PHY_STAT0) & HDMI_PHY_HPD ?1705connector_status_connected : connector_status_disconnected;1706}1707EXPORT_SYMBOL_GPL(dw_hdmi_phy_read_hpd);17081709void dw_hdmi_phy_update_hpd(struct dw_hdmi *hdmi, void *data,1710bool force, bool disabled, bool rxsense)1711{1712u8 old_mask = hdmi->phy_mask;17131714if (force || disabled || !rxsense)1715hdmi->phy_mask |= HDMI_PHY_RX_SENSE;1716else1717hdmi->phy_mask &= ~HDMI_PHY_RX_SENSE;17181719if (old_mask != hdmi->phy_mask)1720hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);1721}1722EXPORT_SYMBOL_GPL(dw_hdmi_phy_update_hpd);17231724void dw_hdmi_phy_setup_hpd(struct dw_hdmi *hdmi, void *data)1725{1726/*1727* Configure the PHY RX SENSE and HPD interrupts polarities and clear1728* any pending interrupt.1729*/1730hdmi_writeb(hdmi, HDMI_PHY_HPD | HDMI_PHY_RX_SENSE, HDMI_PHY_POL0);1731hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,1732HDMI_IH_PHY_STAT0);17331734/* Enable cable hot plug irq. */1735hdmi_writeb(hdmi, hdmi->phy_mask, HDMI_PHY_MASK0);17361737/* Clear and unmute interrupts. */1738hdmi_writeb(hdmi, HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE,1739HDMI_IH_PHY_STAT0);1740hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),1741HDMI_IH_MUTE_PHY_STAT0);1742}1743EXPORT_SYMBOL_GPL(dw_hdmi_phy_setup_hpd);17441745static const struct dw_hdmi_phy_ops dw_hdmi_synopsys_phy_ops = {1746.init = dw_hdmi_phy_init,1747.disable = dw_hdmi_phy_disable,1748.read_hpd = dw_hdmi_phy_read_hpd,1749.update_hpd = dw_hdmi_phy_update_hpd,1750.setup_hpd = dw_hdmi_phy_setup_hpd,1751};17521753/* -----------------------------------------------------------------------------1754* HDMI TX Setup1755*/17561757static void hdmi_tx_hdcp_config(struct dw_hdmi *hdmi)1758{1759u8 de;17601761if (hdmi->hdmi_data.video_mode.mdataenablepolarity)1762de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_HIGH;1763else1764de = HDMI_A_VIDPOLCFG_DATAENPOL_ACTIVE_LOW;17651766/* disable rx detect */1767hdmi_modb(hdmi, HDMI_A_HDCPCFG0_RXDETECT_DISABLE,1768HDMI_A_HDCPCFG0_RXDETECT_MASK, HDMI_A_HDCPCFG0);17691770hdmi_modb(hdmi, de, HDMI_A_VIDPOLCFG_DATAENPOL_MASK, HDMI_A_VIDPOLCFG);17711772hdmi_modb(hdmi, HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_DISABLE,1773HDMI_A_HDCPCFG1_ENCRYPTIONDISABLE_MASK, HDMI_A_HDCPCFG1);1774}17751776static void hdmi_config_AVI(struct dw_hdmi *hdmi,1777const struct drm_connector *connector,1778const struct drm_display_mode *mode)1779{1780struct hdmi_avi_infoframe frame;1781u8 val;17821783/* Initialise info frame from DRM mode */1784drm_hdmi_avi_infoframe_from_display_mode(&frame, connector, mode);17851786if (hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {1787drm_hdmi_avi_infoframe_quant_range(&frame, connector, mode,1788hdmi->hdmi_data.rgb_limited_range ?1789HDMI_QUANTIZATION_RANGE_LIMITED :1790HDMI_QUANTIZATION_RANGE_FULL);1791} else {1792frame.quantization_range = HDMI_QUANTIZATION_RANGE_DEFAULT;1793frame.ycc_quantization_range =1794HDMI_YCC_QUANTIZATION_RANGE_LIMITED;1795}17961797if (hdmi_bus_fmt_is_yuv444(hdmi->hdmi_data.enc_out_bus_format))1798frame.colorspace = HDMI_COLORSPACE_YUV444;1799else if (hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format))1800frame.colorspace = HDMI_COLORSPACE_YUV422;1801else if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))1802frame.colorspace = HDMI_COLORSPACE_YUV420;1803else1804frame.colorspace = HDMI_COLORSPACE_RGB;18051806/* Set up colorimetry */1807if (!hdmi_bus_fmt_is_rgb(hdmi->hdmi_data.enc_out_bus_format)) {1808switch (hdmi->hdmi_data.enc_out_encoding) {1809case V4L2_YCBCR_ENC_601:1810if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV601)1811frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;1812else1813frame.colorimetry = HDMI_COLORIMETRY_ITU_601;1814frame.extended_colorimetry =1815HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1816break;1817case V4L2_YCBCR_ENC_709:1818if (hdmi->hdmi_data.enc_in_encoding == V4L2_YCBCR_ENC_XV709)1819frame.colorimetry = HDMI_COLORIMETRY_EXTENDED;1820else1821frame.colorimetry = HDMI_COLORIMETRY_ITU_709;1822frame.extended_colorimetry =1823HDMI_EXTENDED_COLORIMETRY_XV_YCC_709;1824break;1825default: /* Carries no data */1826frame.colorimetry = HDMI_COLORIMETRY_ITU_601;1827frame.extended_colorimetry =1828HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1829break;1830}1831} else {1832frame.colorimetry = HDMI_COLORIMETRY_NONE;1833frame.extended_colorimetry =1834HDMI_EXTENDED_COLORIMETRY_XV_YCC_601;1835}18361837/*1838* The Designware IP uses a different byte format from standard1839* AVI info frames, though generally the bits are in the correct1840* bytes.1841*/18421843/*1844* AVI data byte 1 differences: Colorspace in bits 0,1 rather than 5,6,1845* scan info in bits 4,5 rather than 0,1 and active aspect present in1846* bit 6 rather than 4.1847*/1848val = (frame.scan_mode & 3) << 4 | (frame.colorspace & 3);1849if (frame.active_aspect & 15)1850val |= HDMI_FC_AVICONF0_ACTIVE_FMT_INFO_PRESENT;1851if (frame.top_bar || frame.bottom_bar)1852val |= HDMI_FC_AVICONF0_BAR_DATA_HORIZ_BAR;1853if (frame.left_bar || frame.right_bar)1854val |= HDMI_FC_AVICONF0_BAR_DATA_VERT_BAR;1855hdmi_writeb(hdmi, val, HDMI_FC_AVICONF0);18561857/* AVI data byte 2 differences: none */1858val = ((frame.colorimetry & 0x3) << 6) |1859((frame.picture_aspect & 0x3) << 4) |1860(frame.active_aspect & 0xf);1861hdmi_writeb(hdmi, val, HDMI_FC_AVICONF1);18621863/* AVI data byte 3 differences: none */1864val = ((frame.extended_colorimetry & 0x7) << 4) |1865((frame.quantization_range & 0x3) << 2) |1866(frame.nups & 0x3);1867if (frame.itc)1868val |= HDMI_FC_AVICONF2_IT_CONTENT_VALID;1869hdmi_writeb(hdmi, val, HDMI_FC_AVICONF2);18701871/* AVI data byte 4 differences: none */1872val = frame.video_code & 0x7f;1873hdmi_writeb(hdmi, val, HDMI_FC_AVIVID);18741875/* AVI Data Byte 5- set up input and output pixel repetition */1876val = (((hdmi->hdmi_data.video_mode.mpixelrepetitioninput + 1) <<1877HDMI_FC_PRCONF_INCOMING_PR_FACTOR_OFFSET) &1878HDMI_FC_PRCONF_INCOMING_PR_FACTOR_MASK) |1879((hdmi->hdmi_data.video_mode.mpixelrepetitionoutput <<1880HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_OFFSET) &1881HDMI_FC_PRCONF_OUTPUT_PR_FACTOR_MASK);1882hdmi_writeb(hdmi, val, HDMI_FC_PRCONF);18831884/*1885* AVI data byte 5 differences: content type in 0,1 rather than 4,5,1886* ycc range in bits 2,3 rather than 6,71887*/1888val = ((frame.ycc_quantization_range & 0x3) << 2) |1889(frame.content_type & 0x3);1890hdmi_writeb(hdmi, val, HDMI_FC_AVICONF3);18911892/* AVI Data Bytes 6-13 */1893hdmi_writeb(hdmi, frame.top_bar & 0xff, HDMI_FC_AVIETB0);1894hdmi_writeb(hdmi, (frame.top_bar >> 8) & 0xff, HDMI_FC_AVIETB1);1895hdmi_writeb(hdmi, frame.bottom_bar & 0xff, HDMI_FC_AVISBB0);1896hdmi_writeb(hdmi, (frame.bottom_bar >> 8) & 0xff, HDMI_FC_AVISBB1);1897hdmi_writeb(hdmi, frame.left_bar & 0xff, HDMI_FC_AVIELB0);1898hdmi_writeb(hdmi, (frame.left_bar >> 8) & 0xff, HDMI_FC_AVIELB1);1899hdmi_writeb(hdmi, frame.right_bar & 0xff, HDMI_FC_AVISRB0);1900hdmi_writeb(hdmi, (frame.right_bar >> 8) & 0xff, HDMI_FC_AVISRB1);1901}19021903static void hdmi_config_vendor_specific_infoframe(struct dw_hdmi *hdmi,1904const struct drm_connector *connector,1905const struct drm_display_mode *mode)1906{1907struct hdmi_vendor_infoframe frame;1908u8 buffer[10];1909ssize_t err;19101911err = drm_hdmi_vendor_infoframe_from_display_mode(&frame, connector,1912mode);1913if (err < 0)1914/*1915* Going into that statement does not means vendor infoframe1916* fails. It just informed us that vendor infoframe is not1917* needed for the selected mode. Only 4k or stereoscopic 3D1918* mode requires vendor infoframe. So just simply return.1919*/1920return;19211922err = hdmi_vendor_infoframe_pack(&frame, buffer, sizeof(buffer));1923if (err < 0) {1924dev_err(hdmi->dev, "Failed to pack vendor infoframe: %zd\n",1925err);1926return;1927}1928hdmi_mask_writeb(hdmi, 0, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,1929HDMI_FC_DATAUTO0_VSD_MASK);19301931/* Set the length of HDMI vendor specific InfoFrame payload */1932hdmi_writeb(hdmi, buffer[2], HDMI_FC_VSDSIZE);19331934/* Set 24bit IEEE Registration Identifier */1935hdmi_writeb(hdmi, buffer[4], HDMI_FC_VSDIEEEID0);1936hdmi_writeb(hdmi, buffer[5], HDMI_FC_VSDIEEEID1);1937hdmi_writeb(hdmi, buffer[6], HDMI_FC_VSDIEEEID2);19381939/* Set HDMI_Video_Format and HDMI_VIC/3D_Structure */1940hdmi_writeb(hdmi, buffer[7], HDMI_FC_VSDPAYLOAD0);1941hdmi_writeb(hdmi, buffer[8], HDMI_FC_VSDPAYLOAD1);19421943if (frame.s3d_struct >= HDMI_3D_STRUCTURE_SIDE_BY_SIDE_HALF)1944hdmi_writeb(hdmi, buffer[9], HDMI_FC_VSDPAYLOAD2);19451946/* Packet frame interpolation */1947hdmi_writeb(hdmi, 1, HDMI_FC_DATAUTO1);19481949/* Auto packets per frame and line spacing */1950hdmi_writeb(hdmi, 0x11, HDMI_FC_DATAUTO2);19511952/* Configures the Frame Composer On RDRB mode */1953hdmi_mask_writeb(hdmi, 1, HDMI_FC_DATAUTO0, HDMI_FC_DATAUTO0_VSD_OFFSET,1954HDMI_FC_DATAUTO0_VSD_MASK);1955}19561957static void hdmi_config_drm_infoframe(struct dw_hdmi *hdmi,1958const struct drm_connector *connector)1959{1960const struct drm_connector_state *conn_state = connector->state;1961struct hdmi_drm_infoframe frame;1962u8 buffer[30];1963ssize_t err;1964int i;19651966if (!hdmi->plat_data->use_drm_infoframe)1967return;19681969hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_DISABLE,1970HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);19711972err = drm_hdmi_infoframe_set_hdr_metadata(&frame, conn_state);1973if (err < 0)1974return;19751976err = hdmi_drm_infoframe_pack(&frame, buffer, sizeof(buffer));1977if (err < 0) {1978dev_err(hdmi->dev, "Failed to pack drm infoframe: %zd\n", err);1979return;1980}19811982hdmi_writeb(hdmi, frame.version, HDMI_FC_DRM_HB0);1983hdmi_writeb(hdmi, frame.length, HDMI_FC_DRM_HB1);19841985for (i = 0; i < frame.length; i++)1986hdmi_writeb(hdmi, buffer[4 + i], HDMI_FC_DRM_PB0 + i);19871988hdmi_writeb(hdmi, 1, HDMI_FC_DRM_UP);1989hdmi_modb(hdmi, HDMI_FC_PACKET_TX_EN_DRM_ENABLE,1990HDMI_FC_PACKET_TX_EN_DRM_MASK, HDMI_FC_PACKET_TX_EN);1991}19921993static void hdmi_av_composer(struct dw_hdmi *hdmi,1994const struct drm_display_info *display,1995const struct drm_display_mode *mode)1996{1997u8 inv_val, bytes;1998const struct drm_hdmi_info *hdmi_info = &display->hdmi;1999struct hdmi_vmode *vmode = &hdmi->hdmi_data.video_mode;2000int hblank, vblank, h_de_hs, v_de_vs, hsync_len, vsync_len;2001unsigned int vdisplay, hdisplay;20022003vmode->mpixelclock = mode->clock * 1000;20042005dev_dbg(hdmi->dev, "final pixclk = %d\n", vmode->mpixelclock);20062007vmode->mtmdsclock = vmode->mpixelclock;20082009if (!hdmi_bus_fmt_is_yuv422(hdmi->hdmi_data.enc_out_bus_format)) {2010switch (hdmi_bus_fmt_color_depth(2011hdmi->hdmi_data.enc_out_bus_format)) {2012case 16:2013vmode->mtmdsclock = vmode->mpixelclock * 2;2014break;2015case 12:2016vmode->mtmdsclock = vmode->mpixelclock * 3 / 2;2017break;2018case 10:2019vmode->mtmdsclock = vmode->mpixelclock * 5 / 4;2020break;2021}2022}20232024if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format))2025vmode->mtmdsclock /= 2;20262027dev_dbg(hdmi->dev, "final tmdsclock = %d\n", vmode->mtmdsclock);20282029/* Set up HDMI_FC_INVIDCONF */2030inv_val = (hdmi->hdmi_data.hdcp_enable ||2031(dw_hdmi_support_scdc(hdmi, display) &&2032(vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||2033hdmi_info->scdc.scrambling.low_rates)) ?2034HDMI_FC_INVIDCONF_HDCP_KEEPOUT_ACTIVE :2035HDMI_FC_INVIDCONF_HDCP_KEEPOUT_INACTIVE);20362037inv_val |= mode->flags & DRM_MODE_FLAG_PVSYNC ?2038HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_HIGH :2039HDMI_FC_INVIDCONF_VSYNC_IN_POLARITY_ACTIVE_LOW;20402041inv_val |= mode->flags & DRM_MODE_FLAG_PHSYNC ?2042HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_HIGH :2043HDMI_FC_INVIDCONF_HSYNC_IN_POLARITY_ACTIVE_LOW;20442045inv_val |= (vmode->mdataenablepolarity ?2046HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_HIGH :2047HDMI_FC_INVIDCONF_DE_IN_POLARITY_ACTIVE_LOW);20482049if (hdmi->vic == 39)2050inv_val |= HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH;2051else2052inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?2053HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_HIGH :2054HDMI_FC_INVIDCONF_R_V_BLANK_IN_OSC_ACTIVE_LOW;20552056inv_val |= mode->flags & DRM_MODE_FLAG_INTERLACE ?2057HDMI_FC_INVIDCONF_IN_I_P_INTERLACED :2058HDMI_FC_INVIDCONF_IN_I_P_PROGRESSIVE;20592060inv_val |= hdmi->sink_is_hdmi ?2061HDMI_FC_INVIDCONF_DVI_MODEZ_HDMI_MODE :2062HDMI_FC_INVIDCONF_DVI_MODEZ_DVI_MODE;20632064hdmi_writeb(hdmi, inv_val, HDMI_FC_INVIDCONF);20652066hdisplay = mode->hdisplay;2067hblank = mode->htotal - mode->hdisplay;2068h_de_hs = mode->hsync_start - mode->hdisplay;2069hsync_len = mode->hsync_end - mode->hsync_start;20702071/*2072* When we're setting a YCbCr420 mode, we need2073* to adjust the horizontal timing to suit.2074*/2075if (hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format)) {2076hdisplay /= 2;2077hblank /= 2;2078h_de_hs /= 2;2079hsync_len /= 2;2080}20812082vdisplay = mode->vdisplay;2083vblank = mode->vtotal - mode->vdisplay;2084v_de_vs = mode->vsync_start - mode->vdisplay;2085vsync_len = mode->vsync_end - mode->vsync_start;20862087/*2088* When we're setting an interlaced mode, we need2089* to adjust the vertical timing to suit.2090*/2091if (mode->flags & DRM_MODE_FLAG_INTERLACE) {2092vdisplay /= 2;2093vblank /= 2;2094v_de_vs /= 2;2095vsync_len /= 2;2096}20972098/* Scrambling Control */2099if (dw_hdmi_support_scdc(hdmi, display)) {2100if (vmode->mtmdsclock > HDMI14_MAX_TMDSCLK ||2101hdmi_info->scdc.scrambling.low_rates) {2102/*2103* HDMI2.0 Specifies the following procedure:2104* After the Source Device has determined that2105* SCDC_Present is set (=1), the Source Device should2106* write the accurate Version of the Source Device2107* to the Source Version field in the SCDCS.2108* Source Devices compliant shall set the2109* Source Version = 1.2110*/2111drm_scdc_readb(hdmi->ddc, SCDC_SINK_VERSION,2112&bytes);2113drm_scdc_writeb(hdmi->ddc, SCDC_SOURCE_VERSION,2114min_t(u8, bytes, SCDC_MIN_SOURCE_VERSION));21152116/* Enabled Scrambling in the Sink */2117drm_scdc_set_scrambling(hdmi->curr_conn, 1);21182119/*2120* To activate the scrambler feature, you must ensure2121* that the quasi-static configuration bit2122* fc_invidconf.HDCP_keepout is set at configuration2123* time, before the required mc_swrstzreq.tmdsswrst_req2124* reset request is issued.2125*/2126hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,2127HDMI_MC_SWRSTZ);2128hdmi_writeb(hdmi, 1, HDMI_FC_SCRAMBLER_CTRL);2129} else {2130hdmi_writeb(hdmi, 0, HDMI_FC_SCRAMBLER_CTRL);2131hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ,2132HDMI_MC_SWRSTZ);2133drm_scdc_set_scrambling(hdmi->curr_conn, 0);2134}2135}21362137/* Set up horizontal active pixel width */2138hdmi_writeb(hdmi, hdisplay >> 8, HDMI_FC_INHACTV1);2139hdmi_writeb(hdmi, hdisplay, HDMI_FC_INHACTV0);21402141/* Set up vertical active lines */2142hdmi_writeb(hdmi, vdisplay >> 8, HDMI_FC_INVACTV1);2143hdmi_writeb(hdmi, vdisplay, HDMI_FC_INVACTV0);21442145/* Set up horizontal blanking pixel region width */2146hdmi_writeb(hdmi, hblank >> 8, HDMI_FC_INHBLANK1);2147hdmi_writeb(hdmi, hblank, HDMI_FC_INHBLANK0);21482149/* Set up vertical blanking pixel region width */2150hdmi_writeb(hdmi, vblank, HDMI_FC_INVBLANK);21512152/* Set up HSYNC active edge delay width (in pixel clks) */2153hdmi_writeb(hdmi, h_de_hs >> 8, HDMI_FC_HSYNCINDELAY1);2154hdmi_writeb(hdmi, h_de_hs, HDMI_FC_HSYNCINDELAY0);21552156/* Set up VSYNC active edge delay (in lines) */2157hdmi_writeb(hdmi, v_de_vs, HDMI_FC_VSYNCINDELAY);21582159/* Set up HSYNC active pulse width (in pixel clks) */2160hdmi_writeb(hdmi, hsync_len >> 8, HDMI_FC_HSYNCINWIDTH1);2161hdmi_writeb(hdmi, hsync_len, HDMI_FC_HSYNCINWIDTH0);21622163/* Set up VSYNC active edge delay (in lines) */2164hdmi_writeb(hdmi, vsync_len, HDMI_FC_VSYNCINWIDTH);2165}21662167/* HDMI Initialization Step B.4 */2168static void dw_hdmi_enable_video_path(struct dw_hdmi *hdmi)2169{2170/* control period minimum duration */2171hdmi_writeb(hdmi, 12, HDMI_FC_CTRLDUR);2172hdmi_writeb(hdmi, 32, HDMI_FC_EXCTRLDUR);2173hdmi_writeb(hdmi, 1, HDMI_FC_EXCTRLSPAC);21742175/* Set to fill TMDS data channels */2176hdmi_writeb(hdmi, 0x0B, HDMI_FC_CH0PREAM);2177hdmi_writeb(hdmi, 0x16, HDMI_FC_CH1PREAM);2178hdmi_writeb(hdmi, 0x21, HDMI_FC_CH2PREAM);21792180/* Enable pixel clock and tmds data path */2181hdmi->mc_clkdis |= HDMI_MC_CLKDIS_HDCPCLK_DISABLE |2182HDMI_MC_CLKDIS_CSCCLK_DISABLE |2183HDMI_MC_CLKDIS_AUDCLK_DISABLE |2184HDMI_MC_CLKDIS_PREPCLK_DISABLE |2185HDMI_MC_CLKDIS_TMDSCLK_DISABLE;2186hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_PIXELCLK_DISABLE;2187hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21882189hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_TMDSCLK_DISABLE;2190hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21912192/* Enable csc path */2193if (is_csc_needed(hdmi)) {2194hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CSCCLK_DISABLE;2195hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);21962197hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_IN_PATH,2198HDMI_MC_FLOWCTRL);2199} else {2200hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CSCCLK_DISABLE;2201hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);22022203hdmi_writeb(hdmi, HDMI_MC_FLOWCTRL_FEED_THROUGH_OFF_CSC_BYPASS,2204HDMI_MC_FLOWCTRL);2205}2206}22072208/* Workaround to clear the overflow condition */2209static void dw_hdmi_clear_overflow(struct dw_hdmi *hdmi)2210{2211unsigned int count;2212unsigned int i;2213u8 val;22142215/*2216* Under some circumstances the Frame Composer arithmetic unit can miss2217* an FC register write due to being busy processing the previous one.2218* The issue can be worked around by issuing a TMDS software reset and2219* then write one of the FC registers several times.2220*2221* The number of iterations matters and depends on the HDMI TX revision2222* (and possibly on the platform).2223* 4 iterations for i.MX6Q(v1.30a) and 1 iteration for others.2224* i.MX6DL (v1.31a), Allwinner SoCs (v1.32a), Rockchip RK3288 SoC (v2.00a),2225* Amlogic Meson GX SoCs (v2.01a), RK3328/RK3399 SoCs (v2.11a)2226* and i.MX8MPlus (v2.13a) have been identified as needing the workaround2227* with a single iteration.2228*/22292230switch (hdmi->version) {2231case 0x130a:2232count = 4;2233break;2234default:2235count = 1;2236break;2237}22382239/* TMDS software reset */2240hdmi_writeb(hdmi, (u8)~HDMI_MC_SWRSTZ_TMDSSWRST_REQ, HDMI_MC_SWRSTZ);22412242val = hdmi_readb(hdmi, HDMI_FC_INVIDCONF);2243for (i = 0; i < count; i++)2244hdmi_writeb(hdmi, val, HDMI_FC_INVIDCONF);2245}22462247static void hdmi_disable_overflow_interrupts(struct dw_hdmi *hdmi)2248{2249hdmi_writeb(hdmi, HDMI_IH_MUTE_FC_STAT2_OVERFLOW_MASK,2250HDMI_IH_MUTE_FC_STAT2);2251}22522253static int dw_hdmi_setup(struct dw_hdmi *hdmi,2254const struct drm_connector *connector,2255const struct drm_display_mode *mode)2256{2257int ret;22582259hdmi_disable_overflow_interrupts(hdmi);22602261hdmi->vic = drm_match_cea_mode(mode);22622263if (!hdmi->vic) {2264dev_dbg(hdmi->dev, "Non-CEA mode used in HDMI\n");2265} else {2266dev_dbg(hdmi->dev, "CEA mode used vic=%d\n", hdmi->vic);2267}22682269if ((hdmi->vic == 6) || (hdmi->vic == 7) ||2270(hdmi->vic == 21) || (hdmi->vic == 22) ||2271(hdmi->vic == 2) || (hdmi->vic == 3) ||2272(hdmi->vic == 17) || (hdmi->vic == 18))2273hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_601;2274else2275hdmi->hdmi_data.enc_out_encoding = V4L2_YCBCR_ENC_709;22762277hdmi->hdmi_data.video_mode.mpixelrepetitionoutput = 0;2278hdmi->hdmi_data.video_mode.mpixelrepetitioninput = 0;22792280if (hdmi->hdmi_data.enc_in_bus_format == MEDIA_BUS_FMT_FIXED)2281hdmi->hdmi_data.enc_in_bus_format = MEDIA_BUS_FMT_RGB888_1X24;22822283/* TOFIX: Get input encoding from plat data or fallback to none */2284if (hdmi->plat_data->input_bus_encoding)2285hdmi->hdmi_data.enc_in_encoding =2286hdmi->plat_data->input_bus_encoding;2287else2288hdmi->hdmi_data.enc_in_encoding = V4L2_YCBCR_ENC_DEFAULT;22892290if (hdmi->hdmi_data.enc_out_bus_format == MEDIA_BUS_FMT_FIXED)2291hdmi->hdmi_data.enc_out_bus_format = MEDIA_BUS_FMT_RGB888_1X24;22922293hdmi->hdmi_data.rgb_limited_range = hdmi->sink_is_hdmi &&2294drm_default_rgb_quant_range(mode) ==2295HDMI_QUANTIZATION_RANGE_LIMITED;22962297hdmi->hdmi_data.pix_repet_factor = 0;2298hdmi->hdmi_data.hdcp_enable = 0;2299hdmi->hdmi_data.video_mode.mdataenablepolarity = true;23002301/* HDMI Initialization Step B.1 */2302hdmi_av_composer(hdmi, &connector->display_info, mode);23032304/* HDMI Initializateion Step B.2 */2305ret = hdmi->phy.ops->init(hdmi, hdmi->phy.data,2306&connector->display_info,2307&hdmi->previous_mode);2308if (ret)2309return ret;2310hdmi->phy.enabled = true;23112312/* HDMI Initialization Step B.3 */2313dw_hdmi_enable_video_path(hdmi);23142315if (hdmi->sink_has_audio) {2316dev_dbg(hdmi->dev, "sink has audio support\n");23172318/* HDMI Initialization Step E - Configure audio */2319hdmi_clk_regenerator_update_pixel_clock(hdmi);2320hdmi_enable_audio_clk(hdmi, hdmi->audio_enable);2321}23222323/* not for DVI mode */2324if (hdmi->sink_is_hdmi) {2325dev_dbg(hdmi->dev, "%s HDMI mode\n", __func__);23262327/* HDMI Initialization Step F - Configure AVI InfoFrame */2328hdmi_config_AVI(hdmi, connector, mode);2329hdmi_config_vendor_specific_infoframe(hdmi, connector, mode);2330hdmi_config_drm_infoframe(hdmi, connector);2331} else {2332dev_dbg(hdmi->dev, "%s DVI mode\n", __func__);2333}23342335hdmi_video_packetize(hdmi);2336hdmi_video_csc(hdmi);2337hdmi_video_sample(hdmi);2338hdmi_tx_hdcp_config(hdmi);23392340dw_hdmi_clear_overflow(hdmi);23412342return 0;2343}23442345static void initialize_hdmi_ih_mutes(struct dw_hdmi *hdmi)2346{2347u8 ih_mute;23482349/*2350* Boot up defaults are:2351* HDMI_IH_MUTE = 0x03 (disabled)2352* HDMI_IH_MUTE_* = 0x00 (enabled)2353*2354* Disable top level interrupt bits in HDMI block2355*/2356ih_mute = hdmi_readb(hdmi, HDMI_IH_MUTE) |2357HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |2358HDMI_IH_MUTE_MUTE_ALL_INTERRUPT;23592360hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);23612362/* by default mask all interrupts */2363hdmi_writeb(hdmi, 0xff, HDMI_VP_MASK);2364hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK0);2365hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK1);2366hdmi_writeb(hdmi, 0xff, HDMI_FC_MASK2);2367hdmi_writeb(hdmi, 0xff, HDMI_PHY_MASK0);2368hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_INT_ADDR);2369hdmi_writeb(hdmi, 0xff, HDMI_PHY_I2CM_CTLINT_ADDR);2370hdmi_writeb(hdmi, 0xff, HDMI_AUD_INT);2371hdmi_writeb(hdmi, 0xff, HDMI_AUD_SPDIFINT);2372hdmi_writeb(hdmi, 0xff, HDMI_AUD_HBR_MASK);2373hdmi_writeb(hdmi, 0xff, HDMI_GP_MASK);2374hdmi_writeb(hdmi, 0xff, HDMI_A_APIINTMSK);2375hdmi_writeb(hdmi, 0xff, HDMI_I2CM_INT);2376hdmi_writeb(hdmi, 0xff, HDMI_I2CM_CTLINT);23772378/* Disable interrupts in the IH_MUTE_* registers */2379hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT0);2380hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT1);2381hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_FC_STAT2);2382hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AS_STAT0);2383hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_PHY_STAT0);2384hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CM_STAT0);2385hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_CEC_STAT0);2386hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_VP_STAT0);2387hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_I2CMPHY_STAT0);2388hdmi_writeb(hdmi, 0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);23892390/* Enable top level interrupt bits in HDMI block */2391ih_mute &= ~(HDMI_IH_MUTE_MUTE_WAKEUP_INTERRUPT |2392HDMI_IH_MUTE_MUTE_ALL_INTERRUPT);2393hdmi_writeb(hdmi, ih_mute, HDMI_IH_MUTE);2394}23952396static void dw_hdmi_poweron(struct dw_hdmi *hdmi)2397{2398hdmi->bridge_is_on = true;23992400/*2401* The curr_conn field is guaranteed to be valid here, as this function2402* is only be called when !hdmi->disabled.2403*/2404dw_hdmi_setup(hdmi, hdmi->curr_conn, &hdmi->previous_mode);2405}24062407static void dw_hdmi_poweroff(struct dw_hdmi *hdmi)2408{2409if (hdmi->phy.enabled) {2410hdmi->phy.ops->disable(hdmi, hdmi->phy.data);2411hdmi->phy.enabled = false;2412}24132414hdmi->bridge_is_on = false;2415}24162417static void dw_hdmi_update_power(struct dw_hdmi *hdmi)2418{2419int force = hdmi->force;24202421if (hdmi->disabled) {2422force = DRM_FORCE_OFF;2423} else if (force == DRM_FORCE_UNSPECIFIED) {2424if (hdmi->rxsense)2425force = DRM_FORCE_ON;2426else2427force = DRM_FORCE_OFF;2428}24292430if (force == DRM_FORCE_OFF) {2431if (hdmi->bridge_is_on)2432dw_hdmi_poweroff(hdmi);2433} else {2434if (!hdmi->bridge_is_on)2435dw_hdmi_poweron(hdmi);2436}2437}24382439/*2440* Adjust the detection of RXSENSE according to whether we have a forced2441* connection mode enabled, or whether we have been disabled. There is2442* no point processing RXSENSE interrupts if we have a forced connection2443* state, or DRM has us disabled.2444*2445* We also disable rxsense interrupts when we think we're disconnected2446* to avoid floating TDMS signals giving false rxsense interrupts.2447*2448* Note: we still need to listen for HPD interrupts even when DRM has us2449* disabled so that we can detect a connect event.2450*/2451static void dw_hdmi_update_phy_mask(struct dw_hdmi *hdmi)2452{2453if (hdmi->phy.ops->update_hpd)2454hdmi->phy.ops->update_hpd(hdmi, hdmi->phy.data,2455hdmi->force, hdmi->disabled,2456hdmi->rxsense);2457}24582459static enum drm_connector_status dw_hdmi_detect(struct dw_hdmi *hdmi)2460{2461enum drm_connector_status result;24622463result = hdmi->phy.ops->read_hpd(hdmi, hdmi->phy.data);2464hdmi->last_connector_result = result;24652466return result;2467}24682469static const struct drm_edid *dw_hdmi_edid_read(struct dw_hdmi *hdmi,2470struct drm_connector *connector)2471{2472const struct drm_edid *drm_edid;2473const struct edid *edid;24742475if (!hdmi->ddc)2476return NULL;24772478drm_edid = drm_edid_read_ddc(connector, hdmi->ddc);2479if (!drm_edid) {2480dev_dbg(hdmi->dev, "failed to get edid\n");2481return NULL;2482}24832484/*2485* FIXME: This should use connector->display_info.is_hdmi and2486* connector->display_info.has_audio from a path that has read the EDID2487* and called drm_edid_connector_update().2488*/2489edid = drm_edid_raw(drm_edid);24902491dev_dbg(hdmi->dev, "got edid: width[%d] x height[%d]\n",2492edid->width_cm, edid->height_cm);24932494hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);2495hdmi->sink_has_audio = drm_detect_monitor_audio(edid);24962497return drm_edid;2498}24992500/* -----------------------------------------------------------------------------2501* DRM Connector Operations2502*/25032504static enum drm_connector_status2505dw_hdmi_connector_detect(struct drm_connector *connector, bool force)2506{2507struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2508connector);2509return dw_hdmi_detect(hdmi);2510}25112512static int dw_hdmi_connector_get_modes(struct drm_connector *connector)2513{2514struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2515connector);2516const struct drm_edid *drm_edid;2517int ret;25182519drm_edid = dw_hdmi_edid_read(hdmi, connector);25202521drm_edid_connector_update(connector, drm_edid);2522cec_notifier_set_phys_addr(hdmi->cec_notifier,2523connector->display_info.source_physical_address);2524ret = drm_edid_connector_add_modes(connector);2525drm_edid_free(drm_edid);25262527return ret;2528}25292530static int dw_hdmi_connector_atomic_check(struct drm_connector *connector,2531struct drm_atomic_state *state)2532{2533struct drm_connector_state *old_state =2534drm_atomic_get_old_connector_state(state, connector);2535struct drm_connector_state *new_state =2536drm_atomic_get_new_connector_state(state, connector);2537struct drm_crtc *crtc = new_state->crtc;2538struct drm_crtc_state *crtc_state;25392540if (!crtc)2541return 0;25422543if (!drm_connector_atomic_hdr_metadata_equal(old_state, new_state)) {2544crtc_state = drm_atomic_get_crtc_state(state, crtc);2545if (IS_ERR(crtc_state))2546return PTR_ERR(crtc_state);25472548crtc_state->mode_changed = true;2549}25502551return 0;2552}25532554static void dw_hdmi_connector_force(struct drm_connector *connector)2555{2556struct dw_hdmi *hdmi = container_of(connector, struct dw_hdmi,2557connector);25582559mutex_lock(&hdmi->mutex);2560hdmi->force = connector->force;2561dw_hdmi_update_power(hdmi);2562dw_hdmi_update_phy_mask(hdmi);2563mutex_unlock(&hdmi->mutex);2564}25652566static const struct drm_connector_funcs dw_hdmi_connector_funcs = {2567.fill_modes = drm_helper_probe_single_connector_modes,2568.detect = dw_hdmi_connector_detect,2569.destroy = drm_connector_cleanup,2570.force = dw_hdmi_connector_force,2571.reset = drm_atomic_helper_connector_reset,2572.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,2573.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,2574};25752576static const struct drm_connector_helper_funcs dw_hdmi_connector_helper_funcs = {2577.get_modes = dw_hdmi_connector_get_modes,2578.atomic_check = dw_hdmi_connector_atomic_check,2579};25802581static int dw_hdmi_connector_create(struct dw_hdmi *hdmi)2582{2583struct drm_connector *connector = &hdmi->connector;2584struct cec_connector_info conn_info;2585struct cec_notifier *notifier;25862587if (hdmi->version >= 0x200a)2588connector->ycbcr_420_allowed =2589hdmi->plat_data->ycbcr_420_allowed;2590else2591connector->ycbcr_420_allowed = false;25922593connector->interlace_allowed = 1;2594connector->polled = DRM_CONNECTOR_POLL_HPD;25952596drm_connector_helper_add(connector, &dw_hdmi_connector_helper_funcs);25972598drm_connector_init_with_ddc(hdmi->bridge.dev, connector,2599&dw_hdmi_connector_funcs,2600DRM_MODE_CONNECTOR_HDMIA,2601hdmi->ddc);26022603/*2604* drm_connector_attach_max_bpc_property() requires the2605* connector to have a state.2606*/2607drm_atomic_helper_connector_reset(connector);26082609drm_connector_attach_max_bpc_property(connector, 8, 16);26102611if (hdmi->version >= 0x200a && hdmi->plat_data->use_drm_infoframe)2612drm_connector_attach_hdr_output_metadata_property(connector);26132614drm_connector_attach_encoder(connector, hdmi->bridge.encoder);26152616cec_fill_conn_info_from_drm(&conn_info, connector);26172618notifier = cec_notifier_conn_register(hdmi->dev, NULL, &conn_info);2619if (!notifier)2620return -ENOMEM;26212622mutex_lock(&hdmi->cec_notifier_mutex);2623hdmi->cec_notifier = notifier;2624mutex_unlock(&hdmi->cec_notifier_mutex);26252626return 0;2627}26282629/* -----------------------------------------------------------------------------2630* DRM Bridge Operations2631*/26322633/*2634* Possible output formats :2635* - MEDIA_BUS_FMT_UYYVYY16_0_5X48,2636* - MEDIA_BUS_FMT_UYYVYY12_0_5X36,2637* - MEDIA_BUS_FMT_UYYVYY10_0_5X30,2638* - MEDIA_BUS_FMT_UYYVYY8_0_5X24,2639* - MEDIA_BUS_FMT_RGB888_1X24,2640* - MEDIA_BUS_FMT_YUV16_1X48,2641* - MEDIA_BUS_FMT_RGB161616_1X48,2642* - MEDIA_BUS_FMT_UYVY12_1X24,2643* - MEDIA_BUS_FMT_YUV12_1X36,2644* - MEDIA_BUS_FMT_RGB121212_1X36,2645* - MEDIA_BUS_FMT_UYVY10_1X20,2646* - MEDIA_BUS_FMT_YUV10_1X30,2647* - MEDIA_BUS_FMT_RGB101010_1X30,2648* - MEDIA_BUS_FMT_UYVY8_1X16,2649* - MEDIA_BUS_FMT_YUV8_1X24,2650*/26512652/* Can return a maximum of 11 possible output formats for a mode/connector */2653#define MAX_OUTPUT_SEL_FORMATS 1126542655static u32 *dw_hdmi_bridge_atomic_get_output_bus_fmts(struct drm_bridge *bridge,2656struct drm_bridge_state *bridge_state,2657struct drm_crtc_state *crtc_state,2658struct drm_connector_state *conn_state,2659unsigned int *num_output_fmts)2660{2661struct drm_connector *conn = conn_state->connector;2662struct drm_display_info *info = &conn->display_info;2663struct drm_display_mode *mode = &crtc_state->mode;2664u8 max_bpc = conn_state->max_requested_bpc;2665bool is_hdmi2_sink = info->hdmi.scdc.supported ||2666(info->color_formats & DRM_COLOR_FORMAT_YCBCR420);2667u32 *output_fmts;2668unsigned int i = 0;26692670*num_output_fmts = 0;26712672output_fmts = kcalloc(MAX_OUTPUT_SEL_FORMATS, sizeof(*output_fmts),2673GFP_KERNEL);2674if (!output_fmts)2675return NULL;26762677/* If dw-hdmi is the first or only bridge, avoid negociating with ourselves */2678if (list_is_singular(&bridge->encoder->bridge_chain) ||2679list_is_first(&bridge->chain_node, &bridge->encoder->bridge_chain)) {2680*num_output_fmts = 1;2681output_fmts[0] = MEDIA_BUS_FMT_FIXED;26822683return output_fmts;2684}26852686/*2687* If the current mode enforces 4:2:0, force the output bus format2688* to 4:2:0 and do not add the YUV422/444/RGB formats2689*/2690if (conn->ycbcr_420_allowed &&2691(drm_mode_is_420_only(info, mode) ||2692(is_hdmi2_sink && drm_mode_is_420_also(info, mode)))) {26932694/* Order bus formats from 16bit to 8bit if supported */2695if (max_bpc >= 16 && info->bpc == 16 &&2696(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_48))2697output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY16_0_5X48;26982699if (max_bpc >= 12 && info->bpc >= 12 &&2700(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_36))2701output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY12_0_5X36;27022703if (max_bpc >= 10 && info->bpc >= 10 &&2704(info->hdmi.y420_dc_modes & DRM_EDID_YCBCR420_DC_30))2705output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY10_0_5X30;27062707/* Default 8bit fallback */2708output_fmts[i++] = MEDIA_BUS_FMT_UYYVYY8_0_5X24;27092710if (drm_mode_is_420_only(info, mode)) {2711*num_output_fmts = i;2712return output_fmts;2713}2714}27152716/*2717* Order bus formats from 16bit to 8bit and from YUV422 to RGB2718* if supported. In any case the default RGB888 format is added2719*/27202721/* Default 8bit RGB fallback */2722output_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;27232724if (max_bpc >= 16 && info->bpc == 16) {2725if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2726output_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;27272728output_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2729}27302731if (max_bpc >= 12 && info->bpc >= 12) {2732if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2733output_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;27342735if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2736output_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;27372738output_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2739}27402741if (max_bpc >= 10 && info->bpc >= 10) {2742if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2743output_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;27442745if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2746output_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;27472748output_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2749}27502751if (info->color_formats & DRM_COLOR_FORMAT_YCBCR422)2752output_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;27532754if (info->color_formats & DRM_COLOR_FORMAT_YCBCR444)2755output_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;27562757*num_output_fmts = i;27582759return output_fmts;2760}27612762/*2763* Possible input formats :2764* - MEDIA_BUS_FMT_RGB888_1X242765* - MEDIA_BUS_FMT_YUV8_1X242766* - MEDIA_BUS_FMT_UYVY8_1X162767* - MEDIA_BUS_FMT_UYYVYY8_0_5X242768* - MEDIA_BUS_FMT_RGB101010_1X302769* - MEDIA_BUS_FMT_YUV10_1X302770* - MEDIA_BUS_FMT_UYVY10_1X202771* - MEDIA_BUS_FMT_UYYVYY10_0_5X302772* - MEDIA_BUS_FMT_RGB121212_1X362773* - MEDIA_BUS_FMT_YUV12_1X362774* - MEDIA_BUS_FMT_UYVY12_1X242775* - MEDIA_BUS_FMT_UYYVYY12_0_5X362776* - MEDIA_BUS_FMT_RGB161616_1X482777* - MEDIA_BUS_FMT_YUV16_1X482778* - MEDIA_BUS_FMT_UYYVYY16_0_5X482779*/27802781/* Can return a maximum of 3 possible input formats for an output format */2782#define MAX_INPUT_SEL_FORMATS 327832784static u32 *dw_hdmi_bridge_atomic_get_input_bus_fmts(struct drm_bridge *bridge,2785struct drm_bridge_state *bridge_state,2786struct drm_crtc_state *crtc_state,2787struct drm_connector_state *conn_state,2788u32 output_fmt,2789unsigned int *num_input_fmts)2790{2791u32 *input_fmts;2792unsigned int i = 0;27932794*num_input_fmts = 0;27952796input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),2797GFP_KERNEL);2798if (!input_fmts)2799return NULL;28002801switch (output_fmt) {2802/* If MEDIA_BUS_FMT_FIXED is tested, return default bus format */2803case MEDIA_BUS_FMT_FIXED:2804input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2805break;2806/* 8bit */2807case MEDIA_BUS_FMT_RGB888_1X24:2808input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2809input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2810input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2811break;2812case MEDIA_BUS_FMT_YUV8_1X24:2813input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2814input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2815input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2816break;2817case MEDIA_BUS_FMT_UYVY8_1X16:2818input_fmts[i++] = MEDIA_BUS_FMT_UYVY8_1X16;2819input_fmts[i++] = MEDIA_BUS_FMT_YUV8_1X24;2820input_fmts[i++] = MEDIA_BUS_FMT_RGB888_1X24;2821break;28222823/* 10bit */2824case MEDIA_BUS_FMT_RGB101010_1X30:2825input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2826input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2827input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2828break;2829case MEDIA_BUS_FMT_YUV10_1X30:2830input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2831input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2832input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2833break;2834case MEDIA_BUS_FMT_UYVY10_1X20:2835input_fmts[i++] = MEDIA_BUS_FMT_UYVY10_1X20;2836input_fmts[i++] = MEDIA_BUS_FMT_YUV10_1X30;2837input_fmts[i++] = MEDIA_BUS_FMT_RGB101010_1X30;2838break;28392840/* 12bit */2841case MEDIA_BUS_FMT_RGB121212_1X36:2842input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2843input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2844input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2845break;2846case MEDIA_BUS_FMT_YUV12_1X36:2847input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2848input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2849input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2850break;2851case MEDIA_BUS_FMT_UYVY12_1X24:2852input_fmts[i++] = MEDIA_BUS_FMT_UYVY12_1X24;2853input_fmts[i++] = MEDIA_BUS_FMT_YUV12_1X36;2854input_fmts[i++] = MEDIA_BUS_FMT_RGB121212_1X36;2855break;28562857/* 16bit */2858case MEDIA_BUS_FMT_RGB161616_1X48:2859input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2860input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;2861break;2862case MEDIA_BUS_FMT_YUV16_1X48:2863input_fmts[i++] = MEDIA_BUS_FMT_YUV16_1X48;2864input_fmts[i++] = MEDIA_BUS_FMT_RGB161616_1X48;2865break;28662867/*YUV 4:2:0 */2868case MEDIA_BUS_FMT_UYYVYY8_0_5X24:2869case MEDIA_BUS_FMT_UYYVYY10_0_5X30:2870case MEDIA_BUS_FMT_UYYVYY12_0_5X36:2871case MEDIA_BUS_FMT_UYYVYY16_0_5X48:2872input_fmts[i++] = output_fmt;2873break;2874}28752876*num_input_fmts = i;28772878if (*num_input_fmts == 0) {2879kfree(input_fmts);2880input_fmts = NULL;2881}28822883return input_fmts;2884}28852886static int dw_hdmi_bridge_atomic_check(struct drm_bridge *bridge,2887struct drm_bridge_state *bridge_state,2888struct drm_crtc_state *crtc_state,2889struct drm_connector_state *conn_state)2890{2891struct dw_hdmi *hdmi = bridge->driver_private;28922893hdmi->hdmi_data.enc_out_bus_format =2894bridge_state->output_bus_cfg.format;28952896hdmi->hdmi_data.enc_in_bus_format =2897bridge_state->input_bus_cfg.format;28982899dev_dbg(hdmi->dev, "input format 0x%04x, output format 0x%04x\n",2900bridge_state->input_bus_cfg.format,2901bridge_state->output_bus_cfg.format);29022903return 0;2904}29052906static int dw_hdmi_bridge_attach(struct drm_bridge *bridge,2907struct drm_encoder *encoder,2908enum drm_bridge_attach_flags flags)2909{2910struct dw_hdmi *hdmi = bridge->driver_private;29112912if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)2913return drm_bridge_attach(encoder, hdmi->bridge.next_bridge,2914bridge, flags);29152916return dw_hdmi_connector_create(hdmi);2917}29182919static void dw_hdmi_bridge_detach(struct drm_bridge *bridge)2920{2921struct dw_hdmi *hdmi = bridge->driver_private;29222923mutex_lock(&hdmi->cec_notifier_mutex);2924cec_notifier_conn_unregister(hdmi->cec_notifier);2925hdmi->cec_notifier = NULL;2926mutex_unlock(&hdmi->cec_notifier_mutex);2927}29282929static enum drm_mode_status2930dw_hdmi_bridge_mode_valid(struct drm_bridge *bridge,2931const struct drm_display_info *info,2932const struct drm_display_mode *mode)2933{2934struct dw_hdmi *hdmi = bridge->driver_private;2935const struct dw_hdmi_plat_data *pdata = hdmi->plat_data;2936enum drm_mode_status mode_status = MODE_OK;29372938/* We don't support double-clocked modes */2939if (mode->flags & DRM_MODE_FLAG_DBLCLK)2940return MODE_BAD;29412942if (pdata->mode_valid)2943mode_status = pdata->mode_valid(hdmi, pdata->priv_data, info,2944mode);29452946return mode_status;2947}29482949static void dw_hdmi_bridge_mode_set(struct drm_bridge *bridge,2950const struct drm_display_mode *orig_mode,2951const struct drm_display_mode *mode)2952{2953struct dw_hdmi *hdmi = bridge->driver_private;29542955mutex_lock(&hdmi->mutex);29562957/* Store the display mode for plugin/DKMS poweron events */2958drm_mode_copy(&hdmi->previous_mode, mode);29592960mutex_unlock(&hdmi->mutex);2961}29622963static void dw_hdmi_bridge_atomic_disable(struct drm_bridge *bridge,2964struct drm_atomic_state *state)2965{2966struct dw_hdmi *hdmi = bridge->driver_private;29672968mutex_lock(&hdmi->mutex);2969hdmi->disabled = true;2970hdmi->curr_conn = NULL;2971dw_hdmi_update_power(hdmi);2972dw_hdmi_update_phy_mask(hdmi);2973handle_plugged_change(hdmi, false);2974mutex_unlock(&hdmi->mutex);2975}29762977static void dw_hdmi_bridge_atomic_enable(struct drm_bridge *bridge,2978struct drm_atomic_state *state)2979{2980struct dw_hdmi *hdmi = bridge->driver_private;2981struct drm_connector *connector;29822983connector = drm_atomic_get_new_connector_for_encoder(state,2984bridge->encoder);29852986mutex_lock(&hdmi->mutex);2987hdmi->disabled = false;2988hdmi->curr_conn = connector;2989dw_hdmi_update_power(hdmi);2990dw_hdmi_update_phy_mask(hdmi);2991handle_plugged_change(hdmi, true);2992mutex_unlock(&hdmi->mutex);2993}29942995static enum drm_connector_status2996dw_hdmi_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)2997{2998struct dw_hdmi *hdmi = bridge->driver_private;29993000return dw_hdmi_detect(hdmi);3001}30023003static const struct drm_edid *dw_hdmi_bridge_edid_read(struct drm_bridge *bridge,3004struct drm_connector *connector)3005{3006struct dw_hdmi *hdmi = bridge->driver_private;30073008return dw_hdmi_edid_read(hdmi, connector);3009}30103011static const struct drm_bridge_funcs dw_hdmi_bridge_funcs = {3012.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,3013.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,3014.atomic_reset = drm_atomic_helper_bridge_reset,3015.attach = dw_hdmi_bridge_attach,3016.detach = dw_hdmi_bridge_detach,3017.atomic_check = dw_hdmi_bridge_atomic_check,3018.atomic_get_output_bus_fmts = dw_hdmi_bridge_atomic_get_output_bus_fmts,3019.atomic_get_input_bus_fmts = dw_hdmi_bridge_atomic_get_input_bus_fmts,3020.atomic_enable = dw_hdmi_bridge_atomic_enable,3021.atomic_disable = dw_hdmi_bridge_atomic_disable,3022.mode_set = dw_hdmi_bridge_mode_set,3023.mode_valid = dw_hdmi_bridge_mode_valid,3024.detect = dw_hdmi_bridge_detect,3025.edid_read = dw_hdmi_bridge_edid_read,3026};30273028/* -----------------------------------------------------------------------------3029* IRQ Handling3030*/30313032static irqreturn_t dw_hdmi_i2c_irq(struct dw_hdmi *hdmi)3033{3034struct dw_hdmi_i2c *i2c = hdmi->i2c;3035unsigned int stat;30363037stat = hdmi_readb(hdmi, HDMI_IH_I2CM_STAT0);3038if (!stat)3039return IRQ_NONE;30403041hdmi_writeb(hdmi, stat, HDMI_IH_I2CM_STAT0);30423043i2c->stat = stat;30443045complete(&i2c->cmp);30463047return IRQ_HANDLED;3048}30493050static irqreturn_t dw_hdmi_hardirq(int irq, void *dev_id)3051{3052struct dw_hdmi *hdmi = dev_id;3053u8 intr_stat;3054irqreturn_t ret = IRQ_NONE;30553056if (hdmi->i2c)3057ret = dw_hdmi_i2c_irq(hdmi);30583059intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);3060if (intr_stat) {3061hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);3062return IRQ_WAKE_THREAD;3063}30643065return ret;3066}30673068void dw_hdmi_setup_rx_sense(struct dw_hdmi *hdmi, bool hpd, bool rx_sense)3069{3070mutex_lock(&hdmi->mutex);30713072if (!hdmi->force) {3073/*3074* If the RX sense status indicates we're disconnected,3075* clear the software rxsense status.3076*/3077if (!rx_sense)3078hdmi->rxsense = false;30793080/*3081* Only set the software rxsense status when both3082* rxsense and hpd indicates we're connected.3083* This avoids what seems to be bad behaviour in3084* at least iMX6S versions of the phy.3085*/3086if (hpd)3087hdmi->rxsense = true;30883089dw_hdmi_update_power(hdmi);3090dw_hdmi_update_phy_mask(hdmi);3091}3092mutex_unlock(&hdmi->mutex);3093}3094EXPORT_SYMBOL_GPL(dw_hdmi_setup_rx_sense);30953096static irqreturn_t dw_hdmi_irq(int irq, void *dev_id)3097{3098struct dw_hdmi *hdmi = dev_id;3099u8 intr_stat, phy_int_pol, phy_pol_mask, phy_stat;3100enum drm_connector_status status = connector_status_unknown;31013102intr_stat = hdmi_readb(hdmi, HDMI_IH_PHY_STAT0);3103phy_int_pol = hdmi_readb(hdmi, HDMI_PHY_POL0);3104phy_stat = hdmi_readb(hdmi, HDMI_PHY_STAT0);31053106phy_pol_mask = 0;3107if (intr_stat & HDMI_IH_PHY_STAT0_HPD)3108phy_pol_mask |= HDMI_PHY_HPD;3109if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE0)3110phy_pol_mask |= HDMI_PHY_RX_SENSE0;3111if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE1)3112phy_pol_mask |= HDMI_PHY_RX_SENSE1;3113if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE2)3114phy_pol_mask |= HDMI_PHY_RX_SENSE2;3115if (intr_stat & HDMI_IH_PHY_STAT0_RX_SENSE3)3116phy_pol_mask |= HDMI_PHY_RX_SENSE3;31173118if (phy_pol_mask)3119hdmi_modb(hdmi, ~phy_int_pol, phy_pol_mask, HDMI_PHY_POL0);31203121/*3122* RX sense tells us whether the TDMS transmitters are detecting3123* load - in other words, there's something listening on the3124* other end of the link. Use this to decide whether we should3125* power on the phy as HPD may be toggled by the sink to merely3126* ask the source to re-read the EDID.3127*/3128if (intr_stat &3129(HDMI_IH_PHY_STAT0_RX_SENSE | HDMI_IH_PHY_STAT0_HPD)) {3130dw_hdmi_setup_rx_sense(hdmi,3131phy_stat & HDMI_PHY_HPD,3132phy_stat & HDMI_PHY_RX_SENSE);31333134if ((phy_stat & (HDMI_PHY_RX_SENSE | HDMI_PHY_HPD)) == 0) {3135mutex_lock(&hdmi->cec_notifier_mutex);3136cec_notifier_phys_addr_invalidate(hdmi->cec_notifier);3137mutex_unlock(&hdmi->cec_notifier_mutex);3138}31393140if (phy_stat & HDMI_PHY_HPD)3141status = connector_status_connected;31423143if (!(phy_stat & (HDMI_PHY_HPD | HDMI_PHY_RX_SENSE)))3144status = connector_status_disconnected;3145}31463147if (status != connector_status_unknown) {3148dev_dbg(hdmi->dev, "EVENT=%s\n",3149status == connector_status_connected ?3150"plugin" : "plugout");31513152if (hdmi->bridge.dev) {3153drm_helper_hpd_irq_event(hdmi->bridge.dev);3154drm_bridge_hpd_notify(&hdmi->bridge, status);3155}3156}31573158hdmi_writeb(hdmi, intr_stat, HDMI_IH_PHY_STAT0);3159hdmi_writeb(hdmi, ~(HDMI_IH_PHY_STAT0_HPD | HDMI_IH_PHY_STAT0_RX_SENSE),3160HDMI_IH_MUTE_PHY_STAT0);31613162return IRQ_HANDLED;3163}31643165static const struct dw_hdmi_phy_data dw_hdmi_phys[] = {3166{3167.type = DW_HDMI_PHY_DWC_HDMI_TX_PHY,3168.name = "DWC HDMI TX PHY",3169.gen = 1,3170}, {3171.type = DW_HDMI_PHY_DWC_MHL_PHY_HEAC,3172.name = "DWC MHL PHY + HEAC PHY",3173.gen = 2,3174.has_svsret = true,3175.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3176}, {3177.type = DW_HDMI_PHY_DWC_MHL_PHY,3178.name = "DWC MHL PHY",3179.gen = 2,3180.has_svsret = true,3181.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3182}, {3183.type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY_HEAC,3184.name = "DWC HDMI 3D TX PHY + HEAC PHY",3185.gen = 2,3186.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3187}, {3188.type = DW_HDMI_PHY_DWC_HDMI_3D_TX_PHY,3189.name = "DWC HDMI 3D TX PHY",3190.gen = 2,3191.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3192}, {3193.type = DW_HDMI_PHY_DWC_HDMI20_TX_PHY,3194.name = "DWC HDMI 2.0 TX PHY",3195.gen = 2,3196.has_svsret = true,3197.configure = hdmi_phy_configure_dwc_hdmi_3d_tx,3198}, {3199.type = DW_HDMI_PHY_VENDOR_PHY,3200.name = "Vendor PHY",3201}3202};32033204static int dw_hdmi_detect_phy(struct dw_hdmi *hdmi)3205{3206unsigned int i;3207u8 phy_type;32083209phy_type = hdmi->plat_data->phy_force_vendor ?3210DW_HDMI_PHY_VENDOR_PHY :3211hdmi_readb(hdmi, HDMI_CONFIG2_ID);32123213if (phy_type == DW_HDMI_PHY_VENDOR_PHY) {3214/* Vendor PHYs require support from the glue layer. */3215if (!hdmi->plat_data->phy_ops || !hdmi->plat_data->phy_name) {3216dev_err(hdmi->dev,3217"Vendor HDMI PHY not supported by glue layer\n");3218return -ENODEV;3219}32203221hdmi->phy.ops = hdmi->plat_data->phy_ops;3222hdmi->phy.data = hdmi->plat_data->phy_data;3223hdmi->phy.name = hdmi->plat_data->phy_name;3224return 0;3225}32263227/* Synopsys PHYs are handled internally. */3228for (i = 0; i < ARRAY_SIZE(dw_hdmi_phys); ++i) {3229if (dw_hdmi_phys[i].type == phy_type) {3230hdmi->phy.ops = &dw_hdmi_synopsys_phy_ops;3231hdmi->phy.name = dw_hdmi_phys[i].name;3232hdmi->phy.data = (void *)&dw_hdmi_phys[i];32333234if (!dw_hdmi_phys[i].configure &&3235!hdmi->plat_data->configure_phy) {3236dev_err(hdmi->dev, "%s requires platform support\n",3237hdmi->phy.name);3238return -ENODEV;3239}32403241return 0;3242}3243}32443245dev_err(hdmi->dev, "Unsupported HDMI PHY type (%02x)\n", phy_type);3246return -ENODEV;3247}32483249static void dw_hdmi_cec_enable(struct dw_hdmi *hdmi)3250{3251mutex_lock(&hdmi->mutex);3252hdmi->mc_clkdis &= ~HDMI_MC_CLKDIS_CECCLK_DISABLE;3253hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);3254mutex_unlock(&hdmi->mutex);3255}32563257static void dw_hdmi_cec_disable(struct dw_hdmi *hdmi)3258{3259mutex_lock(&hdmi->mutex);3260hdmi->mc_clkdis |= HDMI_MC_CLKDIS_CECCLK_DISABLE;3261hdmi_writeb(hdmi, hdmi->mc_clkdis, HDMI_MC_CLKDIS);3262mutex_unlock(&hdmi->mutex);3263}32643265static const struct dw_hdmi_cec_ops dw_hdmi_cec_ops = {3266.write = hdmi_writeb,3267.read = hdmi_readb,3268.enable = dw_hdmi_cec_enable,3269.disable = dw_hdmi_cec_disable,3270};32713272static const struct regmap_config hdmi_regmap_8bit_config = {3273.reg_bits = 32,3274.val_bits = 8,3275.reg_stride = 1,3276.max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR,3277};32783279static const struct regmap_config hdmi_regmap_32bit_config = {3280.reg_bits = 32,3281.val_bits = 32,3282.reg_stride = 4,3283.max_register = HDMI_I2CM_FS_SCL_LCNT_0_ADDR << 2,3284};32853286static void dw_hdmi_init_hw(struct dw_hdmi *hdmi)3287{3288initialize_hdmi_ih_mutes(hdmi);32893290/*3291* Reset HDMI DDC I2C master controller and mute I2CM interrupts.3292* Even if we are using a separate i2c adapter doing this doesn't3293* hurt.3294*/3295dw_hdmi_i2c_init(hdmi);32963297if (hdmi->phy.ops->setup_hpd)3298hdmi->phy.ops->setup_hpd(hdmi, hdmi->phy.data);3299}33003301/* -----------------------------------------------------------------------------3302* Probe/remove API, used from platforms based on the DRM bridge API.3303*/33043305static int dw_hdmi_parse_dt(struct dw_hdmi *hdmi)3306{3307struct device_node *remote;33083309if (!hdmi->plat_data->output_port)3310return 0;331133123313remote = of_graph_get_remote_node(hdmi->dev->of_node,3314hdmi->plat_data->output_port,3315-1);3316if (!remote)3317return -ENODEV;33183319hdmi->bridge.next_bridge = of_drm_find_and_get_bridge(remote);3320of_node_put(remote);3321if (!hdmi->bridge.next_bridge)3322return -EPROBE_DEFER;33233324return 0;3325}33263327bool dw_hdmi_bus_fmt_is_420(struct dw_hdmi *hdmi)3328{3329return hdmi_bus_fmt_is_yuv420(hdmi->hdmi_data.enc_out_bus_format);3330}3331EXPORT_SYMBOL_GPL(dw_hdmi_bus_fmt_is_420);33323333struct dw_hdmi *dw_hdmi_probe(struct platform_device *pdev,3334const struct dw_hdmi_plat_data *plat_data)3335{3336struct device *dev = &pdev->dev;3337struct device_node *np = dev->of_node;3338struct platform_device_info pdevinfo;3339struct device_node *ddc_node;3340struct dw_hdmi_cec_data cec;3341struct dw_hdmi *hdmi;3342struct clk *clk;3343struct resource *iores = NULL;3344int irq;3345int ret;3346u32 val = 1;3347u8 prod_id0;3348u8 prod_id1;3349u8 config0;3350u8 config3;33513352hdmi = devm_drm_bridge_alloc(dev, struct dw_hdmi, bridge, &dw_hdmi_bridge_funcs);3353if (IS_ERR(hdmi))3354return hdmi;33553356hdmi->plat_data = plat_data;3357hdmi->dev = dev;3358hdmi->sample_rate = 48000;3359hdmi->channels = 2;3360hdmi->disabled = true;3361hdmi->rxsense = true;3362hdmi->phy_mask = (u8)~(HDMI_PHY_HPD | HDMI_PHY_RX_SENSE);3363hdmi->mc_clkdis = 0x7f;3364hdmi->last_connector_result = connector_status_disconnected;33653366mutex_init(&hdmi->mutex);3367mutex_init(&hdmi->audio_mutex);3368mutex_init(&hdmi->cec_notifier_mutex);3369spin_lock_init(&hdmi->audio_lock);33703371ret = dw_hdmi_parse_dt(hdmi);3372if (ret < 0)3373return ERR_PTR(ret);33743375ddc_node = of_parse_phandle(np, "ddc-i2c-bus", 0);3376if (ddc_node) {3377hdmi->ddc = of_get_i2c_adapter_by_node(ddc_node);3378of_node_put(ddc_node);3379if (!hdmi->ddc) {3380dev_dbg(hdmi->dev, "failed to read ddc node\n");3381return ERR_PTR(-EPROBE_DEFER);3382}33833384} else {3385dev_dbg(hdmi->dev, "no ddc property found\n");3386}33873388if (!plat_data->regm) {3389const struct regmap_config *reg_config;33903391of_property_read_u32(np, "reg-io-width", &val);3392switch (val) {3393case 4:3394reg_config = &hdmi_regmap_32bit_config;3395hdmi->reg_shift = 2;3396break;3397case 1:3398reg_config = &hdmi_regmap_8bit_config;3399break;3400default:3401dev_err(dev, "reg-io-width must be 1 or 4\n");3402return ERR_PTR(-EINVAL);3403}34043405iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);3406hdmi->regs = devm_ioremap_resource(dev, iores);3407if (IS_ERR(hdmi->regs)) {3408ret = PTR_ERR(hdmi->regs);3409goto err_res;3410}34113412hdmi->regm = devm_regmap_init_mmio(dev, hdmi->regs, reg_config);3413if (IS_ERR(hdmi->regm)) {3414dev_err(dev, "Failed to configure regmap\n");3415ret = PTR_ERR(hdmi->regm);3416goto err_res;3417}3418} else {3419hdmi->regm = plat_data->regm;3420}34213422clk = devm_clk_get_enabled(hdmi->dev, "isfr");3423if (IS_ERR(clk)) {3424ret = PTR_ERR(clk);3425dev_err(hdmi->dev, "Unable to get HDMI isfr clk: %d\n", ret);3426goto err_res;3427}34283429clk = devm_clk_get_enabled(hdmi->dev, "iahb");3430if (IS_ERR(clk)) {3431ret = PTR_ERR(clk);3432dev_err(hdmi->dev, "Unable to get HDMI iahb clk: %d\n", ret);3433goto err_res;3434}34353436clk = devm_clk_get_optional_enabled(hdmi->dev, "cec");3437if (IS_ERR(clk)) {3438ret = PTR_ERR(clk);3439if (ret != -EPROBE_DEFER)3440dev_err(hdmi->dev, "Cannot get HDMI cec clock: %d\n",3441ret);3442goto err_res;3443}34443445/* Product and revision IDs */3446hdmi->version = (hdmi_readb(hdmi, HDMI_DESIGN_ID) << 8)3447| (hdmi_readb(hdmi, HDMI_REVISION_ID) << 0);3448prod_id0 = hdmi_readb(hdmi, HDMI_PRODUCT_ID0);3449prod_id1 = hdmi_readb(hdmi, HDMI_PRODUCT_ID1);34503451if (prod_id0 != HDMI_PRODUCT_ID0_HDMI_TX ||3452(prod_id1 & ~HDMI_PRODUCT_ID1_HDCP) != HDMI_PRODUCT_ID1_HDMI_TX) {3453dev_err(dev, "Unsupported HDMI controller (%04x:%02x:%02x)\n",3454hdmi->version, prod_id0, prod_id1);3455ret = -ENODEV;3456goto err_res;3457}34583459ret = dw_hdmi_detect_phy(hdmi);3460if (ret < 0)3461goto err_res;34623463dev_info(dev, "Detected HDMI TX controller v%x.%03x %s HDCP (%s)\n",3464hdmi->version >> 12, hdmi->version & 0xfff,3465prod_id1 & HDMI_PRODUCT_ID1_HDCP ? "with" : "without",3466hdmi->phy.name);34673468dw_hdmi_init_hw(hdmi);34693470irq = platform_get_irq(pdev, 0);3471if (irq < 0) {3472ret = irq;3473goto err_res;3474}34753476ret = devm_request_threaded_irq(dev, irq, dw_hdmi_hardirq,3477dw_hdmi_irq, IRQF_SHARED,3478dev_name(dev), hdmi);3479if (ret)3480goto err_res;34813482/*3483* To prevent overflows in HDMI_IH_FC_STAT2, set the clk regenerator3484* N and cts values before enabling phy3485*/3486hdmi_init_clk_regenerator(hdmi);34873488/* If DDC bus is not specified, try to register HDMI I2C bus */3489if (!hdmi->ddc) {3490/* Look for (optional) stuff related to unwedging */3491hdmi->pinctrl = devm_pinctrl_get(dev);3492if (!IS_ERR(hdmi->pinctrl)) {3493hdmi->unwedge_state =3494pinctrl_lookup_state(hdmi->pinctrl, "unwedge");3495hdmi->default_state =3496pinctrl_lookup_state(hdmi->pinctrl, "default");34973498if (IS_ERR(hdmi->default_state) ||3499IS_ERR(hdmi->unwedge_state)) {3500if (!IS_ERR(hdmi->unwedge_state))3501dev_warn(dev,3502"Unwedge requires default pinctrl\n");3503hdmi->default_state = NULL;3504hdmi->unwedge_state = NULL;3505}3506}35073508hdmi->ddc = dw_hdmi_i2c_adapter(hdmi);3509if (IS_ERR(hdmi->ddc))3510hdmi->ddc = NULL;3511}35123513hdmi->bridge.driver_private = hdmi;3514hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID3515| DRM_BRIDGE_OP_HPD;3516hdmi->bridge.interlace_allowed = true;3517hdmi->bridge.ddc = hdmi->ddc;3518hdmi->bridge.of_node = pdev->dev.of_node;3519hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA;35203521if (hdmi->version >= 0x200a)3522hdmi->bridge.ycbcr_420_allowed = plat_data->ycbcr_420_allowed;35233524memset(&pdevinfo, 0, sizeof(pdevinfo));3525pdevinfo.parent = dev;3526pdevinfo.id = PLATFORM_DEVID_AUTO;35273528config0 = hdmi_readb(hdmi, HDMI_CONFIG0_ID);3529config3 = hdmi_readb(hdmi, HDMI_CONFIG3_ID);35303531if (iores && config3 & HDMI_CONFIG3_AHBAUDDMA) {3532struct dw_hdmi_audio_data audio;35333534audio.phys = iores->start;3535audio.base = hdmi->regs;3536audio.irq = irq;3537audio.hdmi = hdmi;3538audio.get_eld = hdmi_audio_get_eld;3539hdmi->enable_audio = dw_hdmi_ahb_audio_enable;3540hdmi->disable_audio = dw_hdmi_ahb_audio_disable;35413542pdevinfo.name = "dw-hdmi-ahb-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 (config0 & HDMI_CONFIG0_I2S) {3548struct dw_hdmi_i2s_audio_data audio;35493550audio.hdmi = hdmi;3551audio.get_eld = hdmi_audio_get_eld;3552audio.write = hdmi_writeb;3553audio.read = hdmi_readb;3554hdmi->enable_audio = dw_hdmi_i2s_audio_enable;3555hdmi->disable_audio = dw_hdmi_i2s_audio_disable;35563557pdevinfo.name = "dw-hdmi-i2s-audio";3558pdevinfo.data = &audio;3559pdevinfo.size_data = sizeof(audio);3560pdevinfo.dma_mask = DMA_BIT_MASK(32);3561hdmi->audio = platform_device_register_full(&pdevinfo);3562} else if (iores && config3 & HDMI_CONFIG3_GPAUD) {3563struct dw_hdmi_audio_data audio;35643565audio.phys = iores->start;3566audio.base = hdmi->regs;3567audio.irq = irq;3568audio.hdmi = hdmi;3569audio.get_eld = hdmi_audio_get_eld;35703571hdmi->enable_audio = dw_hdmi_gp_audio_enable;3572hdmi->disable_audio = dw_hdmi_gp_audio_disable;35733574pdevinfo.name = "dw-hdmi-gp-audio";3575pdevinfo.id = PLATFORM_DEVID_NONE;3576pdevinfo.data = &audio;3577pdevinfo.size_data = sizeof(audio);3578pdevinfo.dma_mask = DMA_BIT_MASK(32);3579hdmi->audio = platform_device_register_full(&pdevinfo);3580}35813582if (!plat_data->disable_cec && (config0 & HDMI_CONFIG0_CEC)) {3583cec.hdmi = hdmi;3584cec.ops = &dw_hdmi_cec_ops;3585cec.irq = irq;35863587pdevinfo.name = "dw-hdmi-cec";3588pdevinfo.data = &cec;3589pdevinfo.size_data = sizeof(cec);3590pdevinfo.dma_mask = 0;35913592hdmi->cec = platform_device_register_full(&pdevinfo);3593}35943595drm_bridge_add(&hdmi->bridge);35963597return hdmi;35983599err_res:3600i2c_put_adapter(hdmi->ddc);36013602return ERR_PTR(ret);3603}3604EXPORT_SYMBOL_GPL(dw_hdmi_probe);36053606void dw_hdmi_remove(struct dw_hdmi *hdmi)3607{3608drm_bridge_remove(&hdmi->bridge);36093610if (hdmi->audio && !IS_ERR(hdmi->audio))3611platform_device_unregister(hdmi->audio);3612if (!IS_ERR(hdmi->cec))3613platform_device_unregister(hdmi->cec);36143615/* Disable all interrupts */3616hdmi_writeb(hdmi, ~0, HDMI_IH_MUTE_PHY_STAT0);36173618if (hdmi->i2c)3619i2c_del_adapter(&hdmi->i2c->adap);3620else3621i2c_put_adapter(hdmi->ddc);3622}3623EXPORT_SYMBOL_GPL(dw_hdmi_remove);36243625/* -----------------------------------------------------------------------------3626* Bind/unbind API, used from platforms based on the component framework.3627*/3628struct dw_hdmi *dw_hdmi_bind(struct platform_device *pdev,3629struct drm_encoder *encoder,3630const struct dw_hdmi_plat_data *plat_data)3631{3632struct dw_hdmi *hdmi;3633int ret;36343635hdmi = dw_hdmi_probe(pdev, plat_data);3636if (IS_ERR(hdmi))3637return hdmi;36383639ret = drm_bridge_attach(encoder, &hdmi->bridge, NULL, 0);3640if (ret) {3641dw_hdmi_remove(hdmi);3642return ERR_PTR(ret);3643}36443645return hdmi;3646}3647EXPORT_SYMBOL_GPL(dw_hdmi_bind);36483649void dw_hdmi_unbind(struct dw_hdmi *hdmi)3650{3651dw_hdmi_remove(hdmi);3652}3653EXPORT_SYMBOL_GPL(dw_hdmi_unbind);36543655void dw_hdmi_resume(struct dw_hdmi *hdmi)3656{3657dw_hdmi_init_hw(hdmi);3658}3659EXPORT_SYMBOL_GPL(dw_hdmi_resume);36603661MODULE_AUTHOR("Sascha Hauer <[email protected]>");3662MODULE_AUTHOR("Andy Yan <[email protected]>");3663MODULE_AUTHOR("Yakir Yang <[email protected]>");3664MODULE_AUTHOR("Vladimir Zapolskiy <[email protected]>");3665MODULE_DESCRIPTION("DW HDMI transmitter driver");3666MODULE_LICENSE("GPL");3667MODULE_ALIAS("platform:dw-hdmi");366836693670