linux/drivers/gpu/drm/bridge/megachips-stdpxxxx-ge-b850v3-fw.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Driver for MegaChips STDP4028 with GE B850v3 firmware (LVDS-DP)
   4 * Driver for MegaChips STDP2690 with GE B850v3 firmware (DP-DP++)
   5
   6 * Copyright (c) 2017, Collabora Ltd.
   7 * Copyright (c) 2017, General Electric Company
   8
   9
  10 * This driver creates a drm_bridge and a drm_connector for the LVDS to DP++
  11 * display bridge of the GE B850v3. There are two physical bridges on the video
  12 * signal pipeline: a STDP4028(LVDS to DP) and a STDP2690(DP to DP++). The
  13 * physical bridges are automatically configured by the input video signal, and
  14 * the driver has no access to the video processing pipeline. The driver is
  15 * only needed to read EDID from the STDP2690 and to handle HPD events from the
  16 * STDP4028. The driver communicates with both bridges over i2c. The video
  17 * signal pipeline is as follows:
  18 *
  19 *   Host -> LVDS|--(STDP4028)--|DP -> DP|--(STDP2690)--|DP++ -> Video output
  20 */
  21
  22#include <linux/i2c.h>
  23#include <linux/module.h>
  24#include <linux/of.h>
  25
  26#include <drm/drm_atomic.h>
  27#include <drm/drm_atomic_helper.h>
  28#include <drm/drm_bridge.h>
  29#include <drm/drm_edid.h>
  30#include <drm/drm_print.h>
  31#include <drm/drm_probe_helper.h>
  32
  33#define EDID_EXT_BLOCK_CNT 0x7E
  34
  35#define STDP4028_IRQ_OUT_CONF_REG 0x02
  36#define STDP4028_DPTX_IRQ_EN_REG 0x3C
  37#define STDP4028_DPTX_IRQ_STS_REG 0x3D
  38#define STDP4028_DPTX_STS_REG 0x3E
  39
  40#define STDP4028_DPTX_DP_IRQ_EN 0x1000
  41
  42#define STDP4028_DPTX_HOTPLUG_IRQ_EN 0x0400
  43#define STDP4028_DPTX_LINK_CH_IRQ_EN 0x2000
  44#define STDP4028_DPTX_IRQ_CONFIG \
  45                (STDP4028_DPTX_LINK_CH_IRQ_EN | STDP4028_DPTX_HOTPLUG_IRQ_EN)
  46
  47#define STDP4028_DPTX_HOTPLUG_STS 0x0200
  48#define STDP4028_DPTX_LINK_STS 0x1000
  49#define STDP4028_CON_STATE_CONNECTED \
  50                (STDP4028_DPTX_HOTPLUG_STS | STDP4028_DPTX_LINK_STS)
  51
  52#define STDP4028_DPTX_HOTPLUG_CH_STS 0x0400
  53#define STDP4028_DPTX_LINK_CH_STS 0x2000
  54#define STDP4028_DPTX_IRQ_CLEAR \
  55                (STDP4028_DPTX_LINK_CH_STS | STDP4028_DPTX_HOTPLUG_CH_STS)
  56
  57static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex);
  58
  59struct ge_b850v3_lvds {
  60        struct drm_connector connector;
  61        struct drm_bridge bridge;
  62        struct i2c_client *stdp4028_i2c;
  63        struct i2c_client *stdp2690_i2c;
  64        struct edid *edid;
  65};
  66
  67static struct ge_b850v3_lvds *ge_b850v3_lvds_ptr;
  68
  69static u8 *stdp2690_get_edid(struct i2c_client *client)
  70{
  71        struct i2c_adapter *adapter = client->adapter;
  72        unsigned char start = 0x00;
  73        unsigned int total_size;
  74        u8 *block = kmalloc(EDID_LENGTH, GFP_KERNEL);
  75
  76        struct i2c_msg msgs[] = {
  77                {
  78                        .addr   = client->addr,
  79                        .flags  = 0,
  80                        .len    = 1,
  81                        .buf    = &start,
  82                }, {
  83                        .addr   = client->addr,
  84                        .flags  = I2C_M_RD,
  85                        .len    = EDID_LENGTH,
  86                        .buf    = block,
  87                }
  88        };
  89
  90        if (!block)
  91                return NULL;
  92
  93        if (i2c_transfer(adapter, msgs, 2) != 2) {
  94                DRM_ERROR("Unable to read EDID.\n");
  95                goto err;
  96        }
  97
  98        if (!drm_edid_block_valid(block, 0, false, NULL)) {
  99                DRM_ERROR("Invalid EDID data\n");
 100                goto err;
 101        }
 102
 103        total_size = (block[EDID_EXT_BLOCK_CNT] + 1) * EDID_LENGTH;
 104        if (total_size > EDID_LENGTH) {
 105                kfree(block);
 106                block = kmalloc(total_size, GFP_KERNEL);
 107                if (!block)
 108                        return NULL;
 109
 110                /* Yes, read the entire buffer, and do not skip the first
 111                 * EDID_LENGTH bytes.
 112                 */
 113                start = 0x00;
 114                msgs[1].len = total_size;
 115                msgs[1].buf = block;
 116
 117                if (i2c_transfer(adapter, msgs, 2) != 2) {
 118                        DRM_ERROR("Unable to read EDID extension blocks.\n");
 119                        goto err;
 120                }
 121                if (!drm_edid_block_valid(block, 1, false, NULL)) {
 122                        DRM_ERROR("Invalid EDID data\n");
 123                        goto err;
 124                }
 125        }
 126
 127        return block;
 128
 129err:
 130        kfree(block);
 131        return NULL;
 132}
 133
 134static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
 135{
 136        struct i2c_client *client;
 137        int num_modes = 0;
 138
 139        client = ge_b850v3_lvds_ptr->stdp2690_i2c;
 140
 141        kfree(ge_b850v3_lvds_ptr->edid);
 142        ge_b850v3_lvds_ptr->edid = (struct edid *)stdp2690_get_edid(client);
 143
 144        if (ge_b850v3_lvds_ptr->edid) {
 145                drm_connector_update_edid_property(connector,
 146                                                      ge_b850v3_lvds_ptr->edid);
 147                num_modes = drm_add_edid_modes(connector,
 148                                               ge_b850v3_lvds_ptr->edid);
 149        }
 150
 151        return num_modes;
 152}
 153
 154static enum drm_mode_status ge_b850v3_lvds_mode_valid(
 155                struct drm_connector *connector, struct drm_display_mode *mode)
 156{
 157        return MODE_OK;
 158}
 159
 160static const struct
 161drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
 162        .get_modes = ge_b850v3_lvds_get_modes,
 163        .mode_valid = ge_b850v3_lvds_mode_valid,
 164};
 165
 166static enum drm_connector_status ge_b850v3_lvds_detect(
 167                struct drm_connector *connector, bool force)
 168{
 169        struct i2c_client *stdp4028_i2c =
 170                        ge_b850v3_lvds_ptr->stdp4028_i2c;
 171        s32 link_state;
 172
 173        link_state = i2c_smbus_read_word_data(stdp4028_i2c,
 174                                              STDP4028_DPTX_STS_REG);
 175
 176        if (link_state == STDP4028_CON_STATE_CONNECTED)
 177                return connector_status_connected;
 178
 179        if (link_state == 0)
 180                return connector_status_disconnected;
 181
 182        return connector_status_unknown;
 183}
 184
 185static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
 186        .fill_modes = drm_helper_probe_single_connector_modes,
 187        .detect = ge_b850v3_lvds_detect,
 188        .destroy = drm_connector_cleanup,
 189        .reset = drm_atomic_helper_connector_reset,
 190        .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
 191        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
 192};
 193
 194static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
 195{
 196        struct i2c_client *stdp4028_i2c
 197                        = ge_b850v3_lvds_ptr->stdp4028_i2c;
 198
 199        i2c_smbus_write_word_data(stdp4028_i2c,
 200                                  STDP4028_DPTX_IRQ_STS_REG,
 201                                  STDP4028_DPTX_IRQ_CLEAR);
 202
 203        if (ge_b850v3_lvds_ptr->connector.dev)
 204                drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->connector.dev);
 205
 206        return IRQ_HANDLED;
 207}
 208
 209static int ge_b850v3_lvds_attach(struct drm_bridge *bridge)
 210{
 211        struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
 212        struct i2c_client *stdp4028_i2c
 213                        = ge_b850v3_lvds_ptr->stdp4028_i2c;
 214        int ret;
 215
 216        if (!bridge->encoder) {
 217                DRM_ERROR("Parent encoder object not found");
 218                return -ENODEV;
 219        }
 220
 221        connector->polled = DRM_CONNECTOR_POLL_HPD;
 222
 223        drm_connector_helper_add(connector,
 224                                 &ge_b850v3_lvds_connector_helper_funcs);
 225
 226        ret = drm_connector_init(bridge->dev, connector,
 227                                 &ge_b850v3_lvds_connector_funcs,
 228                                 DRM_MODE_CONNECTOR_DisplayPort);
 229        if (ret) {
 230                DRM_ERROR("Failed to initialize connector with drm\n");
 231                return ret;
 232        }
 233
 234        ret = drm_connector_attach_encoder(connector, bridge->encoder);
 235        if (ret)
 236                return ret;
 237
 238        /* Configures the bridge to re-enable interrupts after each ack. */
 239        i2c_smbus_write_word_data(stdp4028_i2c,
 240                                  STDP4028_IRQ_OUT_CONF_REG,
 241                                  STDP4028_DPTX_DP_IRQ_EN);
 242
 243        /* Enable interrupts */
 244        i2c_smbus_write_word_data(stdp4028_i2c,
 245                                  STDP4028_DPTX_IRQ_EN_REG,
 246                                  STDP4028_DPTX_IRQ_CONFIG);
 247
 248        return 0;
 249}
 250
 251static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
 252        .attach = ge_b850v3_lvds_attach,
 253};
 254
 255static int ge_b850v3_lvds_init(struct device *dev)
 256{
 257        mutex_lock(&ge_b850v3_lvds_dev_mutex);
 258
 259        if (ge_b850v3_lvds_ptr)
 260                goto success;
 261
 262        ge_b850v3_lvds_ptr = devm_kzalloc(dev,
 263                                          sizeof(*ge_b850v3_lvds_ptr),
 264                                          GFP_KERNEL);
 265
 266        if (!ge_b850v3_lvds_ptr) {
 267                mutex_unlock(&ge_b850v3_lvds_dev_mutex);
 268                return -ENOMEM;
 269        }
 270
 271success:
 272        mutex_unlock(&ge_b850v3_lvds_dev_mutex);
 273        return 0;
 274}
 275
 276static void ge_b850v3_lvds_remove(void)
 277{
 278        mutex_lock(&ge_b850v3_lvds_dev_mutex);
 279        /*
 280         * This check is to avoid both the drivers
 281         * removing the bridge in their remove() function
 282         */
 283        if (!ge_b850v3_lvds_ptr)
 284                goto out;
 285
 286        drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
 287
 288        kfree(ge_b850v3_lvds_ptr->edid);
 289
 290        ge_b850v3_lvds_ptr = NULL;
 291out:
 292        mutex_unlock(&ge_b850v3_lvds_dev_mutex);
 293}
 294
 295static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c,
 296                                       const struct i2c_device_id *id)
 297{
 298        struct device *dev = &stdp4028_i2c->dev;
 299
 300        ge_b850v3_lvds_init(dev);
 301
 302        ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
 303        i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
 304
 305        /* drm bridge initialization */
 306        ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
 307        ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
 308        drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
 309
 310        /* Clear pending interrupts since power up. */
 311        i2c_smbus_write_word_data(stdp4028_i2c,
 312                                  STDP4028_DPTX_IRQ_STS_REG,
 313                                  STDP4028_DPTX_IRQ_CLEAR);
 314
 315        if (!stdp4028_i2c->irq)
 316                return 0;
 317
 318        return devm_request_threaded_irq(&stdp4028_i2c->dev,
 319                        stdp4028_i2c->irq, NULL,
 320                        ge_b850v3_lvds_irq_handler,
 321                        IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
 322                        "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
 323}
 324
 325static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
 326{
 327        ge_b850v3_lvds_remove();
 328
 329        return 0;
 330}
 331
 332static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
 333        {"stdp4028_ge_fw", 0},
 334        {},
 335};
 336MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
 337
 338static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
 339        { .compatible = "megachips,stdp4028-ge-b850v3-fw" },
 340        {},
 341};
 342MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
 343
 344static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
 345        .id_table       = stdp4028_ge_b850v3_fw_i2c_table,
 346        .probe          = stdp4028_ge_b850v3_fw_probe,
 347        .remove         = stdp4028_ge_b850v3_fw_remove,
 348        .driver         = {
 349                .name           = "stdp4028-ge-b850v3-fw",
 350                .of_match_table = stdp4028_ge_b850v3_fw_match,
 351        },
 352};
 353
 354static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c,
 355                                       const struct i2c_device_id *id)
 356{
 357        struct device *dev = &stdp2690_i2c->dev;
 358
 359        ge_b850v3_lvds_init(dev);
 360
 361        ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
 362        i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
 363
 364        return 0;
 365}
 366
 367static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
 368{
 369        ge_b850v3_lvds_remove();
 370
 371        return 0;
 372}
 373
 374static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
 375        {"stdp2690_ge_fw", 0},
 376        {},
 377};
 378MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
 379
 380static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
 381        { .compatible = "megachips,stdp2690-ge-b850v3-fw" },
 382        {},
 383};
 384MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
 385
 386static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
 387        .id_table       = stdp2690_ge_b850v3_fw_i2c_table,
 388        .probe          = stdp2690_ge_b850v3_fw_probe,
 389        .remove         = stdp2690_ge_b850v3_fw_remove,
 390        .driver         = {
 391                .name           = "stdp2690-ge-b850v3-fw",
 392                .of_match_table = stdp2690_ge_b850v3_fw_match,
 393        },
 394};
 395
 396static int __init stdpxxxx_ge_b850v3_init(void)
 397{
 398        int ret;
 399
 400        ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
 401        if (ret)
 402                return ret;
 403
 404        return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
 405}
 406module_init(stdpxxxx_ge_b850v3_init);
 407
 408static void __exit stdpxxxx_ge_b850v3_exit(void)
 409{
 410        i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
 411        i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
 412}
 413module_exit(stdpxxxx_ge_b850v3_exit);
 414
 415MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
 416MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
 417MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
 418MODULE_LICENSE("GPL v2");
 419