linux/drivers/gpu/drm/zte/zx_hdmi.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright 2016 Linaro Ltd.
   4 * Copyright 2016 ZTE Corporation.
   5 */
   6
   7#include <linux/clk.h>
   8#include <linux/component.h>
   9#include <linux/delay.h>
  10#include <linux/err.h>
  11#include <linux/hdmi.h>
  12#include <linux/irq.h>
  13#include <linux/mfd/syscon.h>
  14#include <linux/module.h>
  15#include <linux/mutex.h>
  16#include <linux/of_device.h>
  17
  18#include <drm/drm_atomic_helper.h>
  19#include <drm/drm_edid.h>
  20#include <drm/drm_of.h>
  21#include <drm/drm_probe_helper.h>
  22#include <drm/drm_print.h>
  23#include <drm/drm_simple_kms_helper.h>
  24
  25#include <sound/hdmi-codec.h>
  26
  27#include "zx_hdmi_regs.h"
  28#include "zx_vou.h"
  29
  30#define ZX_HDMI_INFOFRAME_SIZE          31
  31#define DDC_SEGMENT_ADDR                0x30
  32
  33struct zx_hdmi_i2c {
  34        struct i2c_adapter adap;
  35        struct mutex lock;
  36};
  37
  38struct zx_hdmi {
  39        struct drm_connector connector;
  40        struct drm_encoder encoder;
  41        struct zx_hdmi_i2c *ddc;
  42        struct device *dev;
  43        struct drm_device *drm;
  44        void __iomem *mmio;
  45        struct clk *cec_clk;
  46        struct clk *osc_clk;
  47        struct clk *xclk;
  48        bool sink_is_hdmi;
  49        bool sink_has_audio;
  50        struct platform_device *audio_pdev;
  51};
  52
  53#define to_zx_hdmi(x) container_of(x, struct zx_hdmi, x)
  54
  55static inline u8 hdmi_readb(struct zx_hdmi *hdmi, u16 offset)
  56{
  57        return readl_relaxed(hdmi->mmio + offset * 4);
  58}
  59
  60static inline void hdmi_writeb(struct zx_hdmi *hdmi, u16 offset, u8 val)
  61{
  62        writel_relaxed(val, hdmi->mmio + offset * 4);
  63}
  64
  65static inline void hdmi_writeb_mask(struct zx_hdmi *hdmi, u16 offset,
  66                                    u8 mask, u8 val)
  67{
  68        u8 tmp;
  69
  70        tmp = hdmi_readb(hdmi, offset);
  71        tmp = (tmp & ~mask) | (val & mask);
  72        hdmi_writeb(hdmi, offset, tmp);
  73}
  74
  75static int zx_hdmi_infoframe_trans(struct zx_hdmi *hdmi,
  76                                   union hdmi_infoframe *frame, u8 fsel)
  77{
  78        u8 buffer[ZX_HDMI_INFOFRAME_SIZE];
  79        int num;
  80        int i;
  81
  82        hdmi_writeb(hdmi, TPI_INFO_FSEL, fsel);
  83
  84        num = hdmi_infoframe_pack(frame, buffer, ZX_HDMI_INFOFRAME_SIZE);
  85        if (num < 0) {
  86                DRM_DEV_ERROR(hdmi->dev, "failed to pack infoframe: %d\n", num);
  87                return num;
  88        }
  89
  90        for (i = 0; i < num; i++)
  91                hdmi_writeb(hdmi, TPI_INFO_B0 + i, buffer[i]);
  92
  93        hdmi_writeb_mask(hdmi, TPI_INFO_EN, TPI_INFO_TRANS_RPT,
  94                         TPI_INFO_TRANS_RPT);
  95        hdmi_writeb_mask(hdmi, TPI_INFO_EN, TPI_INFO_TRANS_EN,
  96                         TPI_INFO_TRANS_EN);
  97
  98        return num;
  99}
 100
 101static int zx_hdmi_config_video_vsi(struct zx_hdmi *hdmi,
 102                                    struct drm_display_mode *mode)
 103{
 104        union hdmi_infoframe frame;
 105        int ret;
 106
 107        ret = drm_hdmi_vendor_infoframe_from_display_mode(&frame.vendor.hdmi,
 108                                                          &hdmi->connector,
 109                                                          mode);
 110        if (ret) {
 111                DRM_DEV_ERROR(hdmi->dev, "failed to get vendor infoframe: %d\n",
 112                              ret);
 113                return ret;
 114        }
 115
 116        return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_VSIF);
 117}
 118
 119static int zx_hdmi_config_video_avi(struct zx_hdmi *hdmi,
 120                                    struct drm_display_mode *mode)
 121{
 122        union hdmi_infoframe frame;
 123        int ret;
 124
 125        ret = drm_hdmi_avi_infoframe_from_display_mode(&frame.avi,
 126                                                       &hdmi->connector,
 127                                                       mode);
 128        if (ret) {
 129                DRM_DEV_ERROR(hdmi->dev, "failed to get avi infoframe: %d\n",
 130                              ret);
 131                return ret;
 132        }
 133
 134        /* We always use YUV444 for HDMI output. */
 135        frame.avi.colorspace = HDMI_COLORSPACE_YUV444;
 136
 137        return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_AVI);
 138}
 139
 140static void zx_hdmi_encoder_mode_set(struct drm_encoder *encoder,
 141                                     struct drm_display_mode *mode,
 142                                     struct drm_display_mode *adj_mode)
 143{
 144        struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
 145
 146        if (hdmi->sink_is_hdmi) {
 147                zx_hdmi_config_video_avi(hdmi, mode);
 148                zx_hdmi_config_video_vsi(hdmi, mode);
 149        }
 150}
 151
 152static void zx_hdmi_phy_start(struct zx_hdmi *hdmi)
 153{
 154        /* Copy from ZTE BSP code */
 155        hdmi_writeb(hdmi, 0x222, 0x0);
 156        hdmi_writeb(hdmi, 0x224, 0x4);
 157        hdmi_writeb(hdmi, 0x909, 0x0);
 158        hdmi_writeb(hdmi, 0x7b0, 0x90);
 159        hdmi_writeb(hdmi, 0x7b1, 0x00);
 160        hdmi_writeb(hdmi, 0x7b2, 0xa7);
 161        hdmi_writeb(hdmi, 0x7b8, 0xaa);
 162        hdmi_writeb(hdmi, 0x7b2, 0xa7);
 163        hdmi_writeb(hdmi, 0x7b3, 0x0f);
 164        hdmi_writeb(hdmi, 0x7b4, 0x0f);
 165        hdmi_writeb(hdmi, 0x7b5, 0x55);
 166        hdmi_writeb(hdmi, 0x7b7, 0x03);
 167        hdmi_writeb(hdmi, 0x7b9, 0x12);
 168        hdmi_writeb(hdmi, 0x7ba, 0x32);
 169        hdmi_writeb(hdmi, 0x7bc, 0x68);
 170        hdmi_writeb(hdmi, 0x7be, 0x40);
 171        hdmi_writeb(hdmi, 0x7bf, 0x84);
 172        hdmi_writeb(hdmi, 0x7c1, 0x0f);
 173        hdmi_writeb(hdmi, 0x7c8, 0x02);
 174        hdmi_writeb(hdmi, 0x7c9, 0x03);
 175        hdmi_writeb(hdmi, 0x7ca, 0x40);
 176        hdmi_writeb(hdmi, 0x7dc, 0x31);
 177        hdmi_writeb(hdmi, 0x7e2, 0x04);
 178        hdmi_writeb(hdmi, 0x7e0, 0x06);
 179        hdmi_writeb(hdmi, 0x7cb, 0x68);
 180        hdmi_writeb(hdmi, 0x7f9, 0x02);
 181        hdmi_writeb(hdmi, 0x7b6, 0x02);
 182        hdmi_writeb(hdmi, 0x7f3, 0x0);
 183}
 184
 185static void zx_hdmi_hw_enable(struct zx_hdmi *hdmi)
 186{
 187        /* Enable pclk */
 188        hdmi_writeb_mask(hdmi, CLKPWD, CLKPWD_PDIDCK, CLKPWD_PDIDCK);
 189
 190        /* Enable HDMI for TX */
 191        hdmi_writeb_mask(hdmi, FUNC_SEL, FUNC_HDMI_EN, FUNC_HDMI_EN);
 192
 193        /* Enable deep color packet */
 194        hdmi_writeb_mask(hdmi, P2T_CTRL, P2T_DC_PKT_EN, P2T_DC_PKT_EN);
 195
 196        /* Enable HDMI/MHL mode for output */
 197        hdmi_writeb_mask(hdmi, TEST_TXCTRL, TEST_TXCTRL_HDMI_MODE,
 198                         TEST_TXCTRL_HDMI_MODE);
 199
 200        /* Configure reg_qc_sel */
 201        hdmi_writeb(hdmi, HDMICTL4, 0x3);
 202
 203        /* Enable interrupt */
 204        hdmi_writeb_mask(hdmi, INTR1_MASK, INTR1_MONITOR_DETECT,
 205                         INTR1_MONITOR_DETECT);
 206
 207        /* Start up phy */
 208        zx_hdmi_phy_start(hdmi);
 209}
 210
 211static void zx_hdmi_hw_disable(struct zx_hdmi *hdmi)
 212{
 213        /* Disable interrupt */
 214        hdmi_writeb_mask(hdmi, INTR1_MASK, INTR1_MONITOR_DETECT, 0);
 215
 216        /* Disable deep color packet */
 217        hdmi_writeb_mask(hdmi, P2T_CTRL, P2T_DC_PKT_EN, P2T_DC_PKT_EN);
 218
 219        /* Disable HDMI for TX */
 220        hdmi_writeb_mask(hdmi, FUNC_SEL, FUNC_HDMI_EN, 0);
 221
 222        /* Disable pclk */
 223        hdmi_writeb_mask(hdmi, CLKPWD, CLKPWD_PDIDCK, 0);
 224}
 225
 226static void zx_hdmi_encoder_enable(struct drm_encoder *encoder)
 227{
 228        struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
 229
 230        clk_prepare_enable(hdmi->cec_clk);
 231        clk_prepare_enable(hdmi->osc_clk);
 232        clk_prepare_enable(hdmi->xclk);
 233
 234        zx_hdmi_hw_enable(hdmi);
 235
 236        vou_inf_enable(VOU_HDMI, encoder->crtc);
 237}
 238
 239static void zx_hdmi_encoder_disable(struct drm_encoder *encoder)
 240{
 241        struct zx_hdmi *hdmi = to_zx_hdmi(encoder);
 242
 243        vou_inf_disable(VOU_HDMI, encoder->crtc);
 244
 245        zx_hdmi_hw_disable(hdmi);
 246
 247        clk_disable_unprepare(hdmi->xclk);
 248        clk_disable_unprepare(hdmi->osc_clk);
 249        clk_disable_unprepare(hdmi->cec_clk);
 250}
 251
 252static const struct drm_encoder_helper_funcs zx_hdmi_encoder_helper_funcs = {
 253        .enable = zx_hdmi_encoder_enable,
 254        .disable = zx_hdmi_encoder_disable,
 255        .mode_set = zx_hdmi_encoder_mode_set,
 256};
 257
 258static int zx_hdmi_connector_get_modes(struct drm_connector *connector)
 259{
 260        struct zx_hdmi *hdmi = to_zx_hdmi(connector);
 261        struct edid *edid;
 262        int ret;
 263
 264        edid = drm_get_edid(connector, &hdmi->ddc->adap);
 265        if (!edid)
 266                return 0;
 267
 268        hdmi->sink_is_hdmi = drm_detect_hdmi_monitor(edid);
 269        hdmi->sink_has_audio = drm_detect_monitor_audio(edid);
 270        drm_connector_update_edid_property(connector, edid);
 271        ret = drm_add_edid_modes(connector, edid);
 272        kfree(edid);
 273
 274        return ret;
 275}
 276
 277static enum drm_mode_status
 278zx_hdmi_connector_mode_valid(struct drm_connector *connector,
 279                             struct drm_display_mode *mode)
 280{
 281        return MODE_OK;
 282}
 283
 284static struct drm_connector_helper_funcs zx_hdmi_connector_helper_funcs = {
 285        .get_modes = zx_hdmi_connector_get_modes,
 286        .mode_valid = zx_hdmi_connector_mode_valid,
 287};
 288
 289static enum drm_connector_status
 290zx_hdmi_connector_detect(struct drm_connector *connector, bool force)
 291{
 292        struct zx_hdmi *hdmi = to_zx_hdmi(connector);
 293
 294        return (hdmi_readb(hdmi, TPI_HPD_RSEN) & TPI_HPD_CONNECTION) ?
 295                connector_status_connected : connector_status_disconnected;
 296}
 297
 298static const struct drm_connector_funcs zx_hdmi_connector_funcs = {
 299        .fill_modes = drm_helper_probe_single_connector_modes,
 300        .detect = zx_hdmi_connector_detect,
 301        .destroy = drm_connector_cleanup,
 302        .reset = drm_atomic_helper_connector_reset,
 303        .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 304        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 305};
 306
 307static int zx_hdmi_register(struct drm_device *drm, struct zx_hdmi *hdmi)
 308{
 309        struct drm_encoder *encoder = &hdmi->encoder;
 310
 311        encoder->possible_crtcs = VOU_CRTC_MASK;
 312
 313        drm_simple_encoder_init(drm, encoder, DRM_MODE_ENCODER_TMDS);
 314        drm_encoder_helper_add(encoder, &zx_hdmi_encoder_helper_funcs);
 315
 316        hdmi->connector.polled = DRM_CONNECTOR_POLL_HPD;
 317
 318        drm_connector_init_with_ddc(drm, &hdmi->connector,
 319                                    &zx_hdmi_connector_funcs,
 320                                    DRM_MODE_CONNECTOR_HDMIA,
 321                                    &hdmi->ddc->adap);
 322        drm_connector_helper_add(&hdmi->connector,
 323                                 &zx_hdmi_connector_helper_funcs);
 324
 325        drm_connector_attach_encoder(&hdmi->connector, encoder);
 326
 327        return 0;
 328}
 329
 330static irqreturn_t zx_hdmi_irq_thread(int irq, void *dev_id)
 331{
 332        struct zx_hdmi *hdmi = dev_id;
 333
 334        drm_helper_hpd_irq_event(hdmi->connector.dev);
 335
 336        return IRQ_HANDLED;
 337}
 338
 339static irqreturn_t zx_hdmi_irq_handler(int irq, void *dev_id)
 340{
 341        struct zx_hdmi *hdmi = dev_id;
 342        u8 lstat;
 343
 344        lstat = hdmi_readb(hdmi, L1_INTR_STAT);
 345
 346        /* Monitor detect/HPD interrupt */
 347        if (lstat & L1_INTR_STAT_INTR1) {
 348                u8 stat;
 349
 350                stat = hdmi_readb(hdmi, INTR1_STAT);
 351                hdmi_writeb(hdmi, INTR1_STAT, stat);
 352
 353                if (stat & INTR1_MONITOR_DETECT)
 354                        return IRQ_WAKE_THREAD;
 355        }
 356
 357        return IRQ_NONE;
 358}
 359
 360static int zx_hdmi_audio_startup(struct device *dev, void *data)
 361{
 362        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 363        struct drm_encoder *encoder = &hdmi->encoder;
 364
 365        vou_inf_hdmi_audio_sel(encoder->crtc, VOU_HDMI_AUD_SPDIF);
 366
 367        return 0;
 368}
 369
 370static void zx_hdmi_audio_shutdown(struct device *dev, void *data)
 371{
 372        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 373
 374        /* Disable audio input */
 375        hdmi_writeb_mask(hdmi, AUD_EN, AUD_IN_EN, 0);
 376}
 377
 378static inline int zx_hdmi_audio_get_n(unsigned int fs)
 379{
 380        unsigned int n;
 381
 382        if (fs && (fs % 44100) == 0)
 383                n = 6272 * (fs / 44100);
 384        else
 385                n = fs * 128 / 1000;
 386
 387        return n;
 388}
 389
 390static int zx_hdmi_audio_hw_params(struct device *dev,
 391                                   void *data,
 392                                   struct hdmi_codec_daifmt *daifmt,
 393                                   struct hdmi_codec_params *params)
 394{
 395        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 396        struct hdmi_audio_infoframe *cea = &params->cea;
 397        union hdmi_infoframe frame;
 398        int n;
 399
 400        /* We only support spdif for now */
 401        if (daifmt->fmt != HDMI_SPDIF) {
 402                DRM_DEV_ERROR(hdmi->dev, "invalid daifmt %d\n", daifmt->fmt);
 403                return -EINVAL;
 404        }
 405
 406        switch (params->sample_width) {
 407        case 16:
 408                hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
 409                                 SPDIF_SAMPLE_SIZE_16BIT);
 410                break;
 411        case 20:
 412                hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
 413                                 SPDIF_SAMPLE_SIZE_20BIT);
 414                break;
 415        case 24:
 416                hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, SPDIF_SAMPLE_SIZE_MASK,
 417                                 SPDIF_SAMPLE_SIZE_24BIT);
 418                break;
 419        default:
 420                DRM_DEV_ERROR(hdmi->dev, "invalid sample width %d\n",
 421                              params->sample_width);
 422                return -EINVAL;
 423        }
 424
 425        /* CTS is calculated by hardware, and we only need to take care of N */
 426        n = zx_hdmi_audio_get_n(params->sample_rate);
 427        hdmi_writeb(hdmi, N_SVAL1, n & 0xff);
 428        hdmi_writeb(hdmi, N_SVAL2, (n >> 8) & 0xff);
 429        hdmi_writeb(hdmi, N_SVAL3, (n >> 16) & 0xf);
 430
 431        /* Enable spdif mode */
 432        hdmi_writeb_mask(hdmi, AUD_MODE, SPDIF_EN, SPDIF_EN);
 433
 434        /* Enable audio input */
 435        hdmi_writeb_mask(hdmi, AUD_EN, AUD_IN_EN, AUD_IN_EN);
 436
 437        memcpy(&frame.audio, cea, sizeof(*cea));
 438
 439        return zx_hdmi_infoframe_trans(hdmi, &frame, FSEL_AUDIO);
 440}
 441
 442static int zx_hdmi_audio_digital_mute(struct device *dev, void *data,
 443                                      bool enable)
 444{
 445        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 446
 447        if (enable)
 448                hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, TPI_AUD_MUTE,
 449                                 TPI_AUD_MUTE);
 450        else
 451                hdmi_writeb_mask(hdmi, TPI_AUD_CONFIG, TPI_AUD_MUTE, 0);
 452
 453        return 0;
 454}
 455
 456static int zx_hdmi_audio_get_eld(struct device *dev, void *data,
 457                                 uint8_t *buf, size_t len)
 458{
 459        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 460        struct drm_connector *connector = &hdmi->connector;
 461
 462        memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
 463
 464        return 0;
 465}
 466
 467static const struct hdmi_codec_ops zx_hdmi_codec_ops = {
 468        .audio_startup = zx_hdmi_audio_startup,
 469        .hw_params = zx_hdmi_audio_hw_params,
 470        .audio_shutdown = zx_hdmi_audio_shutdown,
 471        .digital_mute = zx_hdmi_audio_digital_mute,
 472        .get_eld = zx_hdmi_audio_get_eld,
 473};
 474
 475static struct hdmi_codec_pdata zx_hdmi_codec_pdata = {
 476        .ops = &zx_hdmi_codec_ops,
 477        .spdif = 1,
 478};
 479
 480static int zx_hdmi_audio_register(struct zx_hdmi *hdmi)
 481{
 482        struct platform_device *pdev;
 483
 484        pdev = platform_device_register_data(hdmi->dev, HDMI_CODEC_DRV_NAME,
 485                                             PLATFORM_DEVID_AUTO,
 486                                             &zx_hdmi_codec_pdata,
 487                                             sizeof(zx_hdmi_codec_pdata));
 488        if (IS_ERR(pdev))
 489                return PTR_ERR(pdev);
 490
 491        hdmi->audio_pdev = pdev;
 492
 493        return 0;
 494}
 495
 496static int zx_hdmi_i2c_read(struct zx_hdmi *hdmi, struct i2c_msg *msg)
 497{
 498        int len = msg->len;
 499        u8 *buf = msg->buf;
 500        int retry = 0;
 501        int ret = 0;
 502
 503        /* Bits [9:8] of bytes */
 504        hdmi_writeb(hdmi, ZX_DDC_DIN_CNT2, (len >> 8) & 0xff);
 505        /* Bits [7:0] of bytes */
 506        hdmi_writeb(hdmi, ZX_DDC_DIN_CNT1, len & 0xff);
 507
 508        /* Clear FIFO */
 509        hdmi_writeb_mask(hdmi, ZX_DDC_CMD, DDC_CMD_MASK, DDC_CMD_CLEAR_FIFO);
 510
 511        /* Kick off the read */
 512        hdmi_writeb_mask(hdmi, ZX_DDC_CMD, DDC_CMD_MASK,
 513                         DDC_CMD_SEQUENTIAL_READ);
 514
 515        while (len > 0) {
 516                int cnt, i;
 517
 518                /* FIFO needs some time to get ready */
 519                usleep_range(500, 1000);
 520
 521                cnt = hdmi_readb(hdmi, ZX_DDC_DOUT_CNT) & DDC_DOUT_CNT_MASK;
 522                if (cnt == 0) {
 523                        if (++retry > 5) {
 524                                DRM_DEV_ERROR(hdmi->dev,
 525                                              "DDC FIFO read timed out!");
 526                                return -ETIMEDOUT;
 527                        }
 528                        continue;
 529                }
 530
 531                for (i = 0; i < cnt; i++)
 532                        *buf++ = hdmi_readb(hdmi, ZX_DDC_DATA);
 533                len -= cnt;
 534        }
 535
 536        return ret;
 537}
 538
 539static int zx_hdmi_i2c_write(struct zx_hdmi *hdmi, struct i2c_msg *msg)
 540{
 541        /*
 542         * The DDC I2C adapter is only for reading EDID data, so we assume
 543         * that the write to this adapter must be the EDID data offset.
 544         */
 545        if ((msg->len != 1) ||
 546            ((msg->addr != DDC_ADDR) && (msg->addr != DDC_SEGMENT_ADDR)))
 547                return -EINVAL;
 548
 549        if (msg->addr == DDC_SEGMENT_ADDR)
 550                hdmi_writeb(hdmi, ZX_DDC_SEGM, msg->addr << 1);
 551        else if (msg->addr == DDC_ADDR)
 552                hdmi_writeb(hdmi, ZX_DDC_ADDR, msg->addr << 1);
 553
 554        hdmi_writeb(hdmi, ZX_DDC_OFFSET, msg->buf[0]);
 555
 556        return 0;
 557}
 558
 559static int zx_hdmi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 560                            int num)
 561{
 562        struct zx_hdmi *hdmi = i2c_get_adapdata(adap);
 563        struct zx_hdmi_i2c *ddc = hdmi->ddc;
 564        int i, ret = 0;
 565
 566        mutex_lock(&ddc->lock);
 567
 568        /* Enable DDC master access */
 569        hdmi_writeb_mask(hdmi, TPI_DDC_MASTER_EN, HW_DDC_MASTER, HW_DDC_MASTER);
 570
 571        for (i = 0; i < num; i++) {
 572                DRM_DEV_DEBUG(hdmi->dev,
 573                              "xfer: num: %d/%d, len: %d, flags: %#x\n",
 574                              i + 1, num, msgs[i].len, msgs[i].flags);
 575
 576                if (msgs[i].flags & I2C_M_RD)
 577                        ret = zx_hdmi_i2c_read(hdmi, &msgs[i]);
 578                else
 579                        ret = zx_hdmi_i2c_write(hdmi, &msgs[i]);
 580
 581                if (ret < 0)
 582                        break;
 583        }
 584
 585        if (!ret)
 586                ret = num;
 587
 588        /* Disable DDC master access */
 589        hdmi_writeb_mask(hdmi, TPI_DDC_MASTER_EN, HW_DDC_MASTER, 0);
 590
 591        mutex_unlock(&ddc->lock);
 592
 593        return ret;
 594}
 595
 596static u32 zx_hdmi_i2c_func(struct i2c_adapter *adapter)
 597{
 598        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 599}
 600
 601static const struct i2c_algorithm zx_hdmi_algorithm = {
 602        .master_xfer    = zx_hdmi_i2c_xfer,
 603        .functionality  = zx_hdmi_i2c_func,
 604};
 605
 606static int zx_hdmi_ddc_register(struct zx_hdmi *hdmi)
 607{
 608        struct i2c_adapter *adap;
 609        struct zx_hdmi_i2c *ddc;
 610        int ret;
 611
 612        ddc = devm_kzalloc(hdmi->dev, sizeof(*ddc), GFP_KERNEL);
 613        if (!ddc)
 614                return -ENOMEM;
 615
 616        hdmi->ddc = ddc;
 617        mutex_init(&ddc->lock);
 618
 619        adap = &ddc->adap;
 620        adap->owner = THIS_MODULE;
 621        adap->class = I2C_CLASS_DDC;
 622        adap->dev.parent = hdmi->dev;
 623        adap->algo = &zx_hdmi_algorithm;
 624        snprintf(adap->name, sizeof(adap->name), "zx hdmi i2c");
 625
 626        ret = i2c_add_adapter(adap);
 627        if (ret) {
 628                DRM_DEV_ERROR(hdmi->dev, "failed to add I2C adapter: %d\n",
 629                              ret);
 630                return ret;
 631        }
 632
 633        i2c_set_adapdata(adap, hdmi);
 634
 635        return 0;
 636}
 637
 638static int zx_hdmi_bind(struct device *dev, struct device *master, void *data)
 639{
 640        struct platform_device *pdev = to_platform_device(dev);
 641        struct drm_device *drm = data;
 642        struct resource *res;
 643        struct zx_hdmi *hdmi;
 644        int irq;
 645        int ret;
 646
 647        hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL);
 648        if (!hdmi)
 649                return -ENOMEM;
 650
 651        hdmi->dev = dev;
 652        hdmi->drm = drm;
 653
 654        dev_set_drvdata(dev, hdmi);
 655
 656        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 657        hdmi->mmio = devm_ioremap_resource(dev, res);
 658        if (IS_ERR(hdmi->mmio)) {
 659                ret = PTR_ERR(hdmi->mmio);
 660                DRM_DEV_ERROR(dev, "failed to remap hdmi region: %d\n", ret);
 661                return ret;
 662        }
 663
 664        irq = platform_get_irq(pdev, 0);
 665        if (irq < 0)
 666                return irq;
 667
 668        hdmi->cec_clk = devm_clk_get(hdmi->dev, "osc_cec");
 669        if (IS_ERR(hdmi->cec_clk)) {
 670                ret = PTR_ERR(hdmi->cec_clk);
 671                DRM_DEV_ERROR(dev, "failed to get cec_clk: %d\n", ret);
 672                return ret;
 673        }
 674
 675        hdmi->osc_clk = devm_clk_get(hdmi->dev, "osc_clk");
 676        if (IS_ERR(hdmi->osc_clk)) {
 677                ret = PTR_ERR(hdmi->osc_clk);
 678                DRM_DEV_ERROR(dev, "failed to get osc_clk: %d\n", ret);
 679                return ret;
 680        }
 681
 682        hdmi->xclk = devm_clk_get(hdmi->dev, "xclk");
 683        if (IS_ERR(hdmi->xclk)) {
 684                ret = PTR_ERR(hdmi->xclk);
 685                DRM_DEV_ERROR(dev, "failed to get xclk: %d\n", ret);
 686                return ret;
 687        }
 688
 689        ret = zx_hdmi_ddc_register(hdmi);
 690        if (ret) {
 691                DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
 692                return ret;
 693        }
 694
 695        ret = zx_hdmi_audio_register(hdmi);
 696        if (ret) {
 697                DRM_DEV_ERROR(dev, "failed to register audio: %d\n", ret);
 698                return ret;
 699        }
 700
 701        ret = zx_hdmi_register(drm, hdmi);
 702        if (ret) {
 703                DRM_DEV_ERROR(dev, "failed to register hdmi: %d\n", ret);
 704                return ret;
 705        }
 706
 707        ret = devm_request_threaded_irq(dev, irq, zx_hdmi_irq_handler,
 708                                        zx_hdmi_irq_thread, IRQF_SHARED,
 709                                        dev_name(dev), hdmi);
 710        if (ret) {
 711                DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
 712                return ret;
 713        }
 714
 715        return 0;
 716}
 717
 718static void zx_hdmi_unbind(struct device *dev, struct device *master,
 719                           void *data)
 720{
 721        struct zx_hdmi *hdmi = dev_get_drvdata(dev);
 722
 723        hdmi->connector.funcs->destroy(&hdmi->connector);
 724        hdmi->encoder.funcs->destroy(&hdmi->encoder);
 725
 726        if (hdmi->audio_pdev)
 727                platform_device_unregister(hdmi->audio_pdev);
 728}
 729
 730static const struct component_ops zx_hdmi_component_ops = {
 731        .bind = zx_hdmi_bind,
 732        .unbind = zx_hdmi_unbind,
 733};
 734
 735static int zx_hdmi_probe(struct platform_device *pdev)
 736{
 737        return component_add(&pdev->dev, &zx_hdmi_component_ops);
 738}
 739
 740static int zx_hdmi_remove(struct platform_device *pdev)
 741{
 742        component_del(&pdev->dev, &zx_hdmi_component_ops);
 743        return 0;
 744}
 745
 746static const struct of_device_id zx_hdmi_of_match[] = {
 747        { .compatible = "zte,zx296718-hdmi", },
 748        { /* end */ },
 749};
 750MODULE_DEVICE_TABLE(of, zx_hdmi_of_match);
 751
 752struct platform_driver zx_hdmi_driver = {
 753        .probe = zx_hdmi_probe,
 754        .remove = zx_hdmi_remove,
 755        .driver = {
 756                .name = "zx-hdmi",
 757                .of_match_table = zx_hdmi_of_match,
 758        },
 759};
 760