linux/drivers/gpu/drm/zte/zx_vga.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
   3 * Copyright 2017 Linaro Ltd.
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License version 2 as
   7 * published by the Free Software Foundation.
   8 */
   9
  10#include <linux/clk.h>
  11#include <linux/component.h>
  12#include <linux/mfd/syscon.h>
  13#include <linux/regmap.h>
  14
  15#include <drm/drm_atomic_helper.h>
  16#include <drm/drm_crtc_helper.h>
  17#include <drm/drmP.h>
  18
  19#include "zx_drm_drv.h"
  20#include "zx_vga_regs.h"
  21#include "zx_vou.h"
  22
  23struct zx_vga_pwrctrl {
  24        struct regmap *regmap;
  25        u32 reg;
  26        u32 mask;
  27};
  28
  29struct zx_vga_i2c {
  30        struct i2c_adapter adap;
  31        struct mutex lock;
  32};
  33
  34struct zx_vga {
  35        struct drm_connector connector;
  36        struct drm_encoder encoder;
  37        struct zx_vga_i2c *ddc;
  38        struct device *dev;
  39        void __iomem *mmio;
  40        struct clk *i2c_wclk;
  41        struct zx_vga_pwrctrl pwrctrl;
  42        struct completion complete;
  43        bool connected;
  44};
  45
  46#define to_zx_vga(x) container_of(x, struct zx_vga, x)
  47
  48static void zx_vga_encoder_enable(struct drm_encoder *encoder)
  49{
  50        struct zx_vga *vga = to_zx_vga(encoder);
  51        struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
  52
  53        /* Set bit to power up VGA DACs */
  54        regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask,
  55                           pwrctrl->mask);
  56
  57        vou_inf_enable(VOU_VGA, encoder->crtc);
  58}
  59
  60static void zx_vga_encoder_disable(struct drm_encoder *encoder)
  61{
  62        struct zx_vga *vga = to_zx_vga(encoder);
  63        struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
  64
  65        vou_inf_disable(VOU_VGA, encoder->crtc);
  66
  67        /* Clear bit to power down VGA DACs */
  68        regmap_update_bits(pwrctrl->regmap, pwrctrl->reg, pwrctrl->mask, 0);
  69}
  70
  71static const struct drm_encoder_helper_funcs zx_vga_encoder_helper_funcs = {
  72        .enable = zx_vga_encoder_enable,
  73        .disable = zx_vga_encoder_disable,
  74};
  75
  76static const struct drm_encoder_funcs zx_vga_encoder_funcs = {
  77        .destroy = drm_encoder_cleanup,
  78};
  79
  80static int zx_vga_connector_get_modes(struct drm_connector *connector)
  81{
  82        struct zx_vga *vga = to_zx_vga(connector);
  83        struct edid *edid;
  84        int ret;
  85
  86        /*
  87         * Clear both detection bits to switch I2C bus from device
  88         * detecting to EDID reading.
  89         */
  90        zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, 0);
  91
  92        edid = drm_get_edid(connector, &vga->ddc->adap);
  93        if (!edid) {
  94                /*
  95                 * If EDID reading fails, we set the device state into
  96                 * disconnected.  Locking is not required here, since the
  97                 * VGA_AUTO_DETECT_SEL register write in irq handler cannot
  98                 * be triggered when both detection bits are cleared as above.
  99                 */
 100                zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
 101                          VGA_DETECT_SEL_NO_DEVICE);
 102                vga->connected = false;
 103                return 0;
 104        }
 105
 106        /*
 107         * As edid reading succeeds, device must be connected, so we set
 108         * up detection bit for unplug interrupt here.
 109         */
 110        zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_HAS_DEVICE);
 111
 112        drm_mode_connector_update_edid_property(connector, edid);
 113        ret = drm_add_edid_modes(connector, edid);
 114        kfree(edid);
 115
 116        return ret;
 117}
 118
 119static enum drm_mode_status
 120zx_vga_connector_mode_valid(struct drm_connector *connector,
 121                            struct drm_display_mode *mode)
 122{
 123        return MODE_OK;
 124}
 125
 126static struct drm_connector_helper_funcs zx_vga_connector_helper_funcs = {
 127        .get_modes = zx_vga_connector_get_modes,
 128        .mode_valid = zx_vga_connector_mode_valid,
 129};
 130
 131static enum drm_connector_status
 132zx_vga_connector_detect(struct drm_connector *connector, bool force)
 133{
 134        struct zx_vga *vga = to_zx_vga(connector);
 135
 136        return vga->connected ? connector_status_connected :
 137                                connector_status_disconnected;
 138}
 139
 140static const struct drm_connector_funcs zx_vga_connector_funcs = {
 141        .fill_modes = drm_helper_probe_single_connector_modes,
 142        .detect = zx_vga_connector_detect,
 143        .destroy = drm_connector_cleanup,
 144        .reset = drm_atomic_helper_connector_reset,
 145        .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 146        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 147};
 148
 149static int zx_vga_register(struct drm_device *drm, struct zx_vga *vga)
 150{
 151        struct drm_encoder *encoder = &vga->encoder;
 152        struct drm_connector *connector = &vga->connector;
 153        struct device *dev = vga->dev;
 154        int ret;
 155
 156        encoder->possible_crtcs = VOU_CRTC_MASK;
 157
 158        ret = drm_encoder_init(drm, encoder, &zx_vga_encoder_funcs,
 159                               DRM_MODE_ENCODER_DAC, NULL);
 160        if (ret) {
 161                DRM_DEV_ERROR(dev, "failed to init encoder: %d\n", ret);
 162                return ret;
 163        };
 164
 165        drm_encoder_helper_add(encoder, &zx_vga_encoder_helper_funcs);
 166
 167        vga->connector.polled = DRM_CONNECTOR_POLL_HPD;
 168
 169        ret = drm_connector_init(drm, connector, &zx_vga_connector_funcs,
 170                                 DRM_MODE_CONNECTOR_VGA);
 171        if (ret) {
 172                DRM_DEV_ERROR(dev, "failed to init connector: %d\n", ret);
 173                goto clean_encoder;
 174        };
 175
 176        drm_connector_helper_add(connector, &zx_vga_connector_helper_funcs);
 177
 178        ret = drm_mode_connector_attach_encoder(connector, encoder);
 179        if (ret) {
 180                DRM_DEV_ERROR(dev, "failed to attach encoder: %d\n", ret);
 181                goto clean_connector;
 182        };
 183
 184        return 0;
 185
 186clean_connector:
 187        drm_connector_cleanup(connector);
 188clean_encoder:
 189        drm_encoder_cleanup(encoder);
 190        return ret;
 191}
 192
 193static int zx_vga_pwrctrl_init(struct zx_vga *vga)
 194{
 195        struct zx_vga_pwrctrl *pwrctrl = &vga->pwrctrl;
 196        struct device *dev = vga->dev;
 197        struct of_phandle_args out_args;
 198        struct regmap *regmap;
 199        int ret;
 200
 201        ret = of_parse_phandle_with_fixed_args(dev->of_node,
 202                                "zte,vga-power-control", 2, 0, &out_args);
 203        if (ret)
 204                return ret;
 205
 206        regmap = syscon_node_to_regmap(out_args.np);
 207        if (IS_ERR(regmap)) {
 208                ret = PTR_ERR(regmap);
 209                goto out;
 210        }
 211
 212        pwrctrl->regmap = regmap;
 213        pwrctrl->reg = out_args.args[0];
 214        pwrctrl->mask = out_args.args[1];
 215
 216out:
 217        of_node_put(out_args.np);
 218        return ret;
 219}
 220
 221static int zx_vga_i2c_read(struct zx_vga *vga, struct i2c_msg *msg)
 222{
 223        int len = msg->len;
 224        u8 *buf = msg->buf;
 225        u32 offset = 0;
 226        int i;
 227
 228        reinit_completion(&vga->complete);
 229
 230        /* Select combo write */
 231        zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_COMBO, VGA_CMD_COMBO);
 232        zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_RW, 0);
 233
 234        while (len > 0) {
 235                u32 cnt;
 236
 237                /* Clear RX FIFO */
 238                zx_writel_mask(vga->mmio + VGA_RXF_CTRL, VGA_RX_FIFO_CLEAR,
 239                               VGA_RX_FIFO_CLEAR);
 240
 241                /* Data offset to read from */
 242                zx_writel(vga->mmio + VGA_SUB_ADDR, offset);
 243
 244                /* Kick off the transfer */
 245                zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS,
 246                               VGA_CMD_TRANS);
 247
 248                if (!wait_for_completion_timeout(&vga->complete,
 249                                                 msecs_to_jiffies(1000))) {
 250                        DRM_DEV_ERROR(vga->dev, "transfer timeout\n");
 251                        return -ETIMEDOUT;
 252                }
 253
 254                cnt = zx_readl(vga->mmio + VGA_RXF_STATUS);
 255                cnt = (cnt & VGA_RXF_COUNT_MASK) >> VGA_RXF_COUNT_SHIFT;
 256                /* FIFO status may report more data than we need to read */
 257                cnt = min_t(u32, len, cnt);
 258
 259                for (i = 0; i < cnt; i++)
 260                        *buf++ = zx_readl(vga->mmio + VGA_DATA);
 261
 262                len -= cnt;
 263                offset += cnt;
 264        }
 265
 266        return 0;
 267}
 268
 269static int zx_vga_i2c_write(struct zx_vga *vga, struct i2c_msg *msg)
 270{
 271        /*
 272         * The DDC I2C adapter is only for reading EDID data, so we assume
 273         * that the write to this adapter must be the EDID data offset.
 274         */
 275        if ((msg->len != 1) || ((msg->addr != DDC_ADDR)))
 276                return -EINVAL;
 277
 278        /* Hardware will take care of the slave address shifting */
 279        zx_writel(vga->mmio + VGA_DEVICE_ADDR, msg->addr);
 280
 281        return 0;
 282}
 283
 284static int zx_vga_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
 285                           int num)
 286{
 287        struct zx_vga *vga = i2c_get_adapdata(adap);
 288        struct zx_vga_i2c *ddc = vga->ddc;
 289        int ret = 0;
 290        int i;
 291
 292        mutex_lock(&ddc->lock);
 293
 294        for (i = 0; i < num; i++) {
 295                if (msgs[i].flags & I2C_M_RD)
 296                        ret = zx_vga_i2c_read(vga, &msgs[i]);
 297                else
 298                        ret = zx_vga_i2c_write(vga, &msgs[i]);
 299
 300                if (ret < 0)
 301                        break;
 302        }
 303
 304        if (!ret)
 305                ret = num;
 306
 307        mutex_unlock(&ddc->lock);
 308
 309        return ret;
 310}
 311
 312static u32 zx_vga_i2c_func(struct i2c_adapter *adapter)
 313{
 314        return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 315}
 316
 317static const struct i2c_algorithm zx_vga_algorithm = {
 318        .master_xfer    = zx_vga_i2c_xfer,
 319        .functionality  = zx_vga_i2c_func,
 320};
 321
 322static int zx_vga_ddc_register(struct zx_vga *vga)
 323{
 324        struct device *dev = vga->dev;
 325        struct i2c_adapter *adap;
 326        struct zx_vga_i2c *ddc;
 327        int ret;
 328
 329        ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL);
 330        if (!ddc)
 331                return -ENOMEM;
 332
 333        vga->ddc = ddc;
 334        mutex_init(&ddc->lock);
 335
 336        adap = &ddc->adap;
 337        adap->owner = THIS_MODULE;
 338        adap->class = I2C_CLASS_DDC;
 339        adap->dev.parent = dev;
 340        adap->algo = &zx_vga_algorithm;
 341        snprintf(adap->name, sizeof(adap->name), "zx vga i2c");
 342
 343        ret = i2c_add_adapter(adap);
 344        if (ret) {
 345                DRM_DEV_ERROR(dev, "failed to add I2C adapter: %d\n", ret);
 346                return ret;
 347        }
 348
 349        i2c_set_adapdata(adap, vga);
 350
 351        return 0;
 352}
 353
 354static irqreturn_t zx_vga_irq_thread(int irq, void *dev_id)
 355{
 356        struct zx_vga *vga = dev_id;
 357
 358        drm_helper_hpd_irq_event(vga->connector.dev);
 359
 360        return IRQ_HANDLED;
 361}
 362
 363static irqreturn_t zx_vga_irq_handler(int irq, void *dev_id)
 364{
 365        struct zx_vga *vga = dev_id;
 366        u32 status;
 367
 368        status = zx_readl(vga->mmio + VGA_I2C_STATUS);
 369
 370        /* Clear interrupt status */
 371        zx_writel_mask(vga->mmio + VGA_I2C_STATUS, VGA_CLEAR_IRQ,
 372                       VGA_CLEAR_IRQ);
 373
 374        if (status & VGA_DEVICE_CONNECTED) {
 375                /*
 376                 * Since VGA_DETECT_SEL bits need to be reset for switching DDC
 377                 * bus from device detection to EDID read, rather than setting
 378                 * up HAS_DEVICE bit here, we need to do that in .get_modes
 379                 * hook for unplug detecting after EDID read succeeds.
 380                 */
 381                vga->connected = true;
 382                return IRQ_WAKE_THREAD;
 383        }
 384
 385        if (status & VGA_DEVICE_DISCONNECTED) {
 386                zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL,
 387                          VGA_DETECT_SEL_NO_DEVICE);
 388                vga->connected = false;
 389                return IRQ_WAKE_THREAD;
 390        }
 391
 392        if (status & VGA_TRANS_DONE) {
 393                complete(&vga->complete);
 394                return IRQ_HANDLED;
 395        }
 396
 397        return IRQ_NONE;
 398}
 399
 400static void zx_vga_hw_init(struct zx_vga *vga)
 401{
 402        unsigned long ref = clk_get_rate(vga->i2c_wclk);
 403        int div;
 404
 405        /*
 406         * Set up I2C fast speed divider per formula below to get 400kHz.
 407         *   scl = ref / ((div + 1) * 4)
 408         */
 409        div = DIV_ROUND_UP(ref / 1000, 400 * 4) - 1;
 410        zx_writel(vga->mmio + VGA_CLK_DIV_FS, div);
 411
 412        /* Set up device detection */
 413        zx_writel(vga->mmio + VGA_AUTO_DETECT_PARA, 0x80);
 414        zx_writel(vga->mmio + VGA_AUTO_DETECT_SEL, VGA_DETECT_SEL_NO_DEVICE);
 415
 416        /*
 417         * We need to poke monitor via DDC bus to get connection irq
 418         * start working.
 419         */
 420        zx_writel(vga->mmio + VGA_DEVICE_ADDR, DDC_ADDR);
 421        zx_writel_mask(vga->mmio + VGA_CMD_CFG, VGA_CMD_TRANS, VGA_CMD_TRANS);
 422}
 423
 424static int zx_vga_bind(struct device *dev, struct device *master, void *data)
 425{
 426        struct platform_device *pdev = to_platform_device(dev);
 427        struct drm_device *drm = data;
 428        struct resource *res;
 429        struct zx_vga *vga;
 430        int irq;
 431        int ret;
 432
 433        vga = devm_kzalloc(dev, sizeof(*vga), GFP_KERNEL);
 434        if (!vga)
 435                return -ENOMEM;
 436
 437        vga->dev = dev;
 438        dev_set_drvdata(dev, vga);
 439
 440        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 441        vga->mmio = devm_ioremap_resource(dev, res);
 442        if (IS_ERR(vga->mmio))
 443                return PTR_ERR(vga->mmio);
 444
 445        irq = platform_get_irq(pdev, 0);
 446        if (irq < 0)
 447                return irq;
 448
 449        vga->i2c_wclk = devm_clk_get(dev, "i2c_wclk");
 450        if (IS_ERR(vga->i2c_wclk)) {
 451                ret = PTR_ERR(vga->i2c_wclk);
 452                DRM_DEV_ERROR(dev, "failed to get i2c_wclk: %d\n", ret);
 453                return ret;
 454        }
 455
 456        ret = zx_vga_pwrctrl_init(vga);
 457        if (ret) {
 458                DRM_DEV_ERROR(dev, "failed to init power control: %d\n", ret);
 459                return ret;
 460        }
 461
 462        ret = zx_vga_ddc_register(vga);
 463        if (ret) {
 464                DRM_DEV_ERROR(dev, "failed to register ddc: %d\n", ret);
 465                return ret;
 466        }
 467
 468        ret = zx_vga_register(drm, vga);
 469        if (ret) {
 470                DRM_DEV_ERROR(dev, "failed to register vga: %d\n", ret);
 471                return ret;
 472        }
 473
 474        init_completion(&vga->complete);
 475
 476        ret = devm_request_threaded_irq(dev, irq, zx_vga_irq_handler,
 477                                        zx_vga_irq_thread, IRQF_SHARED,
 478                                        dev_name(dev), vga);
 479        if (ret) {
 480                DRM_DEV_ERROR(dev, "failed to request threaded irq: %d\n", ret);
 481                return ret;
 482        }
 483
 484        ret = clk_prepare_enable(vga->i2c_wclk);
 485        if (ret)
 486                return ret;
 487
 488        zx_vga_hw_init(vga);
 489
 490        return 0;
 491}
 492
 493static void zx_vga_unbind(struct device *dev, struct device *master,
 494                          void *data)
 495{
 496        struct zx_vga *vga = dev_get_drvdata(dev);
 497
 498        clk_disable_unprepare(vga->i2c_wclk);
 499}
 500
 501static const struct component_ops zx_vga_component_ops = {
 502        .bind = zx_vga_bind,
 503        .unbind = zx_vga_unbind,
 504};
 505
 506static int zx_vga_probe(struct platform_device *pdev)
 507{
 508        return component_add(&pdev->dev, &zx_vga_component_ops);
 509}
 510
 511static int zx_vga_remove(struct platform_device *pdev)
 512{
 513        component_del(&pdev->dev, &zx_vga_component_ops);
 514        return 0;
 515}
 516
 517static const struct of_device_id zx_vga_of_match[] = {
 518        { .compatible = "zte,zx296718-vga", },
 519        { /* end */ },
 520};
 521MODULE_DEVICE_TABLE(of, zx_vga_of_match);
 522
 523struct platform_driver zx_vga_driver = {
 524        .probe = zx_vga_probe,
 525        .remove = zx_vga_remove,
 526        .driver = {
 527                .name = "zx-vga",
 528                .of_match_table = zx_vga_of_match,
 529        },
 530};
 531