1bc1aee7fSJitao Shi // SPDX-License-Identifier: GPL-2.0-only 2bc1aee7fSJitao Shi /* 3bc1aee7fSJitao Shi * Copyright (c) 2016 MediaTek Inc. 4bc1aee7fSJitao Shi */ 5bc1aee7fSJitao Shi 6bc1aee7fSJitao Shi #include <linux/delay.h> 7bc1aee7fSJitao Shi #include <linux/err.h> 8bc1aee7fSJitao Shi #include <linux/gpio/consumer.h> 9bc1aee7fSJitao Shi #include <linux/i2c.h> 10bc1aee7fSJitao Shi #include <linux/module.h> 11bc1aee7fSJitao Shi #include <linux/of_graph.h> 12*826cff3fSPhilip Chen #include <linux/pm_runtime.h> 13692d8db0SPhilip Chen #include <linux/regmap.h> 14bc1aee7fSJitao Shi #include <linux/regulator/consumer.h> 15bc1aee7fSJitao Shi 16bc1aee7fSJitao Shi #include <drm/drm_bridge.h> 1713afcdd7SPhilip Chen #include <drm/drm_dp_helper.h> 18bc1aee7fSJitao Shi #include <drm/drm_mipi_dsi.h> 19bc1aee7fSJitao Shi #include <drm/drm_of.h> 20bc1aee7fSJitao Shi #include <drm/drm_panel.h> 21bc1aee7fSJitao Shi #include <drm/drm_print.h> 22bc1aee7fSJitao Shi 2313afcdd7SPhilip Chen #define PAGE0_AUXCH_CFG3 0x76 2413afcdd7SPhilip Chen #define AUXCH_CFG3_RESET 0xff 2513afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_7_0 0x7d 2613afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_15_8 0x7e 2713afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_23_16 0x7f 2813afcdd7SPhilip Chen #define SWAUX_ADDR_MASK GENMASK(19, 0) 2913afcdd7SPhilip Chen #define PAGE0_SWAUX_LENGTH 0x80 3013afcdd7SPhilip Chen #define SWAUX_LENGTH_MASK GENMASK(3, 0) 3113afcdd7SPhilip Chen #define SWAUX_NO_PAYLOAD BIT(7) 3213afcdd7SPhilip Chen #define PAGE0_SWAUX_WDATA 0x81 3313afcdd7SPhilip Chen #define PAGE0_SWAUX_RDATA 0x82 3413afcdd7SPhilip Chen #define PAGE0_SWAUX_CTRL 0x83 3513afcdd7SPhilip Chen #define SWAUX_SEND BIT(0) 3613afcdd7SPhilip Chen #define PAGE0_SWAUX_STATUS 0x84 3713afcdd7SPhilip Chen #define SWAUX_M_MASK GENMASK(4, 0) 3813afcdd7SPhilip Chen #define SWAUX_STATUS_MASK GENMASK(7, 5) 3913afcdd7SPhilip Chen #define SWAUX_STATUS_NACK (0x1 << 5) 4013afcdd7SPhilip Chen #define SWAUX_STATUS_DEFER (0x2 << 5) 4113afcdd7SPhilip Chen #define SWAUX_STATUS_ACKM (0x3 << 5) 4213afcdd7SPhilip Chen #define SWAUX_STATUS_INVALID (0x4 << 5) 4313afcdd7SPhilip Chen #define SWAUX_STATUS_I2C_NACK (0x5 << 5) 4413afcdd7SPhilip Chen #define SWAUX_STATUS_I2C_DEFER (0x6 << 5) 4513afcdd7SPhilip Chen #define SWAUX_STATUS_TIMEOUT (0x7 << 5) 4613afcdd7SPhilip Chen 47bc1aee7fSJitao Shi #define PAGE2_GPIO_H 0xa7 48bc1aee7fSJitao Shi #define PS_GPIO9 BIT(1) 49bc1aee7fSJitao Shi #define PAGE2_I2C_BYPASS 0xea 50bc1aee7fSJitao Shi #define I2C_BYPASS_EN 0xd0 51bc1aee7fSJitao Shi #define PAGE2_MCS_EN 0xf3 52bc1aee7fSJitao Shi #define MCS_EN BIT(0) 5328210a3fSPhilip Chen 54bc1aee7fSJitao Shi #define PAGE3_SET_ADD 0xfe 55bc1aee7fSJitao Shi #define VDO_CTL_ADD 0x13 56bc1aee7fSJitao Shi #define VDO_DIS 0x18 57bc1aee7fSJitao Shi #define VDO_EN 0x1c 5828210a3fSPhilip Chen 5928210a3fSPhilip Chen #define NUM_MIPI_LANES 4 60bc1aee7fSJitao Shi 61692d8db0SPhilip Chen #define COMMON_PS8640_REGMAP_CONFIG \ 62692d8db0SPhilip Chen .reg_bits = 8, \ 63692d8db0SPhilip Chen .val_bits = 8, \ 64692d8db0SPhilip Chen .cache_type = REGCACHE_NONE 65692d8db0SPhilip Chen 66bc1aee7fSJitao Shi /* 67bc1aee7fSJitao Shi * PS8640 uses multiple addresses: 68bc1aee7fSJitao Shi * page[0]: for DP control 69bc1aee7fSJitao Shi * page[1]: for VIDEO Bridge 70bc1aee7fSJitao Shi * page[2]: for control top 71bc1aee7fSJitao Shi * page[3]: for DSI Link Control1 72bc1aee7fSJitao Shi * page[4]: for MIPI Phy 73bc1aee7fSJitao Shi * page[5]: for VPLL 74bc1aee7fSJitao Shi * page[6]: for DSI Link Control2 75bc1aee7fSJitao Shi * page[7]: for SPI ROM mapping 76bc1aee7fSJitao Shi */ 77bc1aee7fSJitao Shi enum page_addr_offset { 78bc1aee7fSJitao Shi PAGE0_DP_CNTL = 0, 79bc1aee7fSJitao Shi PAGE1_VDO_BDG, 80bc1aee7fSJitao Shi PAGE2_TOP_CNTL, 81bc1aee7fSJitao Shi PAGE3_DSI_CNTL1, 82bc1aee7fSJitao Shi PAGE4_MIPI_PHY, 83bc1aee7fSJitao Shi PAGE5_VPLL, 84bc1aee7fSJitao Shi PAGE6_DSI_CNTL2, 85bc1aee7fSJitao Shi PAGE7_SPI_CNTL, 86bc1aee7fSJitao Shi MAX_DEVS 87bc1aee7fSJitao Shi }; 88bc1aee7fSJitao Shi 89bc1aee7fSJitao Shi enum ps8640_vdo_control { 90bc1aee7fSJitao Shi DISABLE = VDO_DIS, 91bc1aee7fSJitao Shi ENABLE = VDO_EN, 92bc1aee7fSJitao Shi }; 93bc1aee7fSJitao Shi 94bc1aee7fSJitao Shi struct ps8640 { 95bc1aee7fSJitao Shi struct drm_bridge bridge; 96bc1aee7fSJitao Shi struct drm_bridge *panel_bridge; 9713afcdd7SPhilip Chen struct drm_dp_aux aux; 98bc1aee7fSJitao Shi struct mipi_dsi_device *dsi; 99bc1aee7fSJitao Shi struct i2c_client *page[MAX_DEVS]; 100692d8db0SPhilip Chen struct regmap *regmap[MAX_DEVS]; 101bc1aee7fSJitao Shi struct regulator_bulk_data supplies[2]; 102bc1aee7fSJitao Shi struct gpio_desc *gpio_reset; 103bc1aee7fSJitao Shi struct gpio_desc *gpio_powerdown; 104*826cff3fSPhilip Chen bool pre_enabled; 105bc1aee7fSJitao Shi }; 106bc1aee7fSJitao Shi 107692d8db0SPhilip Chen static const struct regmap_config ps8640_regmap_config[] = { 108692d8db0SPhilip Chen [PAGE0_DP_CNTL] = { 109692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 110692d8db0SPhilip Chen .max_register = 0xbf, 111692d8db0SPhilip Chen }, 112692d8db0SPhilip Chen [PAGE1_VDO_BDG] = { 113692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 114692d8db0SPhilip Chen .max_register = 0xff, 115692d8db0SPhilip Chen }, 116692d8db0SPhilip Chen [PAGE2_TOP_CNTL] = { 117692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 118692d8db0SPhilip Chen .max_register = 0xff, 119692d8db0SPhilip Chen }, 120692d8db0SPhilip Chen [PAGE3_DSI_CNTL1] = { 121692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 122692d8db0SPhilip Chen .max_register = 0xff, 123692d8db0SPhilip Chen }, 124692d8db0SPhilip Chen [PAGE4_MIPI_PHY] = { 125692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 126692d8db0SPhilip Chen .max_register = 0xff, 127692d8db0SPhilip Chen }, 128692d8db0SPhilip Chen [PAGE5_VPLL] = { 129692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 130692d8db0SPhilip Chen .max_register = 0x7f, 131692d8db0SPhilip Chen }, 132692d8db0SPhilip Chen [PAGE6_DSI_CNTL2] = { 133692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 134692d8db0SPhilip Chen .max_register = 0xff, 135692d8db0SPhilip Chen }, 136692d8db0SPhilip Chen [PAGE7_SPI_CNTL] = { 137692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 138692d8db0SPhilip Chen .max_register = 0xff, 139692d8db0SPhilip Chen }, 140692d8db0SPhilip Chen }; 141692d8db0SPhilip Chen 142bc1aee7fSJitao Shi static inline struct ps8640 *bridge_to_ps8640(struct drm_bridge *e) 143bc1aee7fSJitao Shi { 144bc1aee7fSJitao Shi return container_of(e, struct ps8640, bridge); 145bc1aee7fSJitao Shi } 146bc1aee7fSJitao Shi 14713afcdd7SPhilip Chen static inline struct ps8640 *aux_to_ps8640(struct drm_dp_aux *aux) 14813afcdd7SPhilip Chen { 14913afcdd7SPhilip Chen return container_of(aux, struct ps8640, aux); 15013afcdd7SPhilip Chen } 15113afcdd7SPhilip Chen 152*826cff3fSPhilip Chen static int ps8640_ensure_hpd(struct ps8640 *ps_bridge) 153*826cff3fSPhilip Chen { 154*826cff3fSPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE2_TOP_CNTL]; 155*826cff3fSPhilip Chen struct device *dev = &ps_bridge->page[PAGE2_TOP_CNTL]->dev; 156*826cff3fSPhilip Chen int status; 157*826cff3fSPhilip Chen int ret; 158*826cff3fSPhilip Chen 159*826cff3fSPhilip Chen /* 160*826cff3fSPhilip Chen * Apparently something about the firmware in the chip signals that 161*826cff3fSPhilip Chen * HPD goes high by reporting GPIO9 as high (even though HPD isn't 162*826cff3fSPhilip Chen * actually connected to GPIO9). 163*826cff3fSPhilip Chen */ 164*826cff3fSPhilip Chen ret = regmap_read_poll_timeout(map, PAGE2_GPIO_H, status, 165*826cff3fSPhilip Chen status & PS_GPIO9, 20 * 1000, 200 * 1000); 166*826cff3fSPhilip Chen 167*826cff3fSPhilip Chen if (ret < 0) 168*826cff3fSPhilip Chen dev_warn(dev, "HPD didn't go high: %d\n", ret); 169*826cff3fSPhilip Chen 170*826cff3fSPhilip Chen return ret; 171*826cff3fSPhilip Chen } 172*826cff3fSPhilip Chen 173*826cff3fSPhilip Chen static ssize_t ps8640_aux_transfer_msg(struct drm_dp_aux *aux, 17413afcdd7SPhilip Chen struct drm_dp_aux_msg *msg) 17513afcdd7SPhilip Chen { 17613afcdd7SPhilip Chen struct ps8640 *ps_bridge = aux_to_ps8640(aux); 17713afcdd7SPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE0_DP_CNTL]; 17813afcdd7SPhilip Chen struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev; 17913afcdd7SPhilip Chen unsigned int len = msg->size; 18013afcdd7SPhilip Chen unsigned int data; 18113afcdd7SPhilip Chen unsigned int base; 18213afcdd7SPhilip Chen int ret; 18313afcdd7SPhilip Chen u8 request = msg->request & 18413afcdd7SPhilip Chen ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE); 18513afcdd7SPhilip Chen u8 *buf = msg->buffer; 18613afcdd7SPhilip Chen u8 addr_len[PAGE0_SWAUX_LENGTH + 1 - PAGE0_SWAUX_ADDR_7_0]; 18713afcdd7SPhilip Chen u8 i; 18813afcdd7SPhilip Chen bool is_native_aux = false; 18913afcdd7SPhilip Chen 19013afcdd7SPhilip Chen if (len > DP_AUX_MAX_PAYLOAD_BYTES) 19113afcdd7SPhilip Chen return -EINVAL; 19213afcdd7SPhilip Chen 19313afcdd7SPhilip Chen if (msg->address & ~SWAUX_ADDR_MASK) 19413afcdd7SPhilip Chen return -EINVAL; 19513afcdd7SPhilip Chen 19613afcdd7SPhilip Chen switch (request) { 19713afcdd7SPhilip Chen case DP_AUX_NATIVE_WRITE: 19813afcdd7SPhilip Chen case DP_AUX_NATIVE_READ: 19913afcdd7SPhilip Chen is_native_aux = true; 20013afcdd7SPhilip Chen fallthrough; 20113afcdd7SPhilip Chen case DP_AUX_I2C_WRITE: 20213afcdd7SPhilip Chen case DP_AUX_I2C_READ: 20313afcdd7SPhilip Chen break; 20413afcdd7SPhilip Chen default: 20513afcdd7SPhilip Chen return -EINVAL; 20613afcdd7SPhilip Chen } 20713afcdd7SPhilip Chen 20813afcdd7SPhilip Chen ret = regmap_write(map, PAGE0_AUXCH_CFG3, AUXCH_CFG3_RESET); 20913afcdd7SPhilip Chen if (ret) { 21013afcdd7SPhilip Chen DRM_DEV_ERROR(dev, "failed to write PAGE0_AUXCH_CFG3: %d\n", 21113afcdd7SPhilip Chen ret); 21213afcdd7SPhilip Chen return ret; 21313afcdd7SPhilip Chen } 21413afcdd7SPhilip Chen 21513afcdd7SPhilip Chen /* Assume it's good */ 21613afcdd7SPhilip Chen msg->reply = 0; 21713afcdd7SPhilip Chen 21813afcdd7SPhilip Chen base = PAGE0_SWAUX_ADDR_7_0; 21913afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_7_0 - base] = msg->address; 22013afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_15_8 - base] = msg->address >> 8; 22113afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_23_16 - base] = (msg->address >> 16) | 22213afcdd7SPhilip Chen (msg->request << 4); 22313afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_LENGTH - base] = (len == 0) ? SWAUX_NO_PAYLOAD : 22413afcdd7SPhilip Chen ((len - 1) & SWAUX_LENGTH_MASK); 22513afcdd7SPhilip Chen 22613afcdd7SPhilip Chen regmap_bulk_write(map, PAGE0_SWAUX_ADDR_7_0, addr_len, 22713afcdd7SPhilip Chen ARRAY_SIZE(addr_len)); 22813afcdd7SPhilip Chen 22913afcdd7SPhilip Chen if (len && (request == DP_AUX_NATIVE_WRITE || 23013afcdd7SPhilip Chen request == DP_AUX_I2C_WRITE)) { 23113afcdd7SPhilip Chen /* Write to the internal FIFO buffer */ 23213afcdd7SPhilip Chen for (i = 0; i < len; i++) { 23313afcdd7SPhilip Chen ret = regmap_write(map, PAGE0_SWAUX_WDATA, buf[i]); 23413afcdd7SPhilip Chen if (ret) { 23513afcdd7SPhilip Chen DRM_DEV_ERROR(dev, 23613afcdd7SPhilip Chen "failed to write WDATA: %d\n", 23713afcdd7SPhilip Chen ret); 23813afcdd7SPhilip Chen return ret; 23913afcdd7SPhilip Chen } 24013afcdd7SPhilip Chen } 24113afcdd7SPhilip Chen } 24213afcdd7SPhilip Chen 24313afcdd7SPhilip Chen regmap_write(map, PAGE0_SWAUX_CTRL, SWAUX_SEND); 24413afcdd7SPhilip Chen 24513afcdd7SPhilip Chen /* Zero delay loop because i2c transactions are slow already */ 24613afcdd7SPhilip Chen regmap_read_poll_timeout(map, PAGE0_SWAUX_CTRL, data, 24713afcdd7SPhilip Chen !(data & SWAUX_SEND), 0, 50 * 1000); 24813afcdd7SPhilip Chen 24913afcdd7SPhilip Chen regmap_read(map, PAGE0_SWAUX_STATUS, &data); 25013afcdd7SPhilip Chen if (ret) { 25113afcdd7SPhilip Chen DRM_DEV_ERROR(dev, "failed to read PAGE0_SWAUX_STATUS: %d\n", 25213afcdd7SPhilip Chen ret); 25313afcdd7SPhilip Chen return ret; 25413afcdd7SPhilip Chen } 25513afcdd7SPhilip Chen 25613afcdd7SPhilip Chen switch (data & SWAUX_STATUS_MASK) { 25713afcdd7SPhilip Chen /* Ignore the DEFER cases as they are already handled in hardware */ 25813afcdd7SPhilip Chen case SWAUX_STATUS_NACK: 25913afcdd7SPhilip Chen case SWAUX_STATUS_I2C_NACK: 26013afcdd7SPhilip Chen /* 26113afcdd7SPhilip Chen * The programming guide is not clear about whether a I2C NACK 26213afcdd7SPhilip Chen * would trigger SWAUX_STATUS_NACK or SWAUX_STATUS_I2C_NACK. So 26313afcdd7SPhilip Chen * we handle both cases together. 26413afcdd7SPhilip Chen */ 26513afcdd7SPhilip Chen if (is_native_aux) 26613afcdd7SPhilip Chen msg->reply |= DP_AUX_NATIVE_REPLY_NACK; 26713afcdd7SPhilip Chen else 26813afcdd7SPhilip Chen msg->reply |= DP_AUX_I2C_REPLY_NACK; 26913afcdd7SPhilip Chen 27013afcdd7SPhilip Chen fallthrough; 27113afcdd7SPhilip Chen case SWAUX_STATUS_ACKM: 27213afcdd7SPhilip Chen len = data & SWAUX_M_MASK; 27313afcdd7SPhilip Chen break; 27413afcdd7SPhilip Chen case SWAUX_STATUS_INVALID: 27513afcdd7SPhilip Chen return -EOPNOTSUPP; 27613afcdd7SPhilip Chen case SWAUX_STATUS_TIMEOUT: 27713afcdd7SPhilip Chen return -ETIMEDOUT; 27813afcdd7SPhilip Chen } 27913afcdd7SPhilip Chen 28013afcdd7SPhilip Chen if (len && (request == DP_AUX_NATIVE_READ || 28113afcdd7SPhilip Chen request == DP_AUX_I2C_READ)) { 28213afcdd7SPhilip Chen /* Read from the internal FIFO buffer */ 28313afcdd7SPhilip Chen for (i = 0; i < len; i++) { 28413afcdd7SPhilip Chen ret = regmap_read(map, PAGE0_SWAUX_RDATA, &data); 28513afcdd7SPhilip Chen if (ret) { 28613afcdd7SPhilip Chen DRM_DEV_ERROR(dev, 28713afcdd7SPhilip Chen "failed to read RDATA: %d\n", 28813afcdd7SPhilip Chen ret); 28913afcdd7SPhilip Chen return ret; 29013afcdd7SPhilip Chen } 29113afcdd7SPhilip Chen 29213afcdd7SPhilip Chen buf[i] = data; 29313afcdd7SPhilip Chen } 29413afcdd7SPhilip Chen } 29513afcdd7SPhilip Chen 29613afcdd7SPhilip Chen return len; 29713afcdd7SPhilip Chen } 29813afcdd7SPhilip Chen 299*826cff3fSPhilip Chen static ssize_t ps8640_aux_transfer(struct drm_dp_aux *aux, 300*826cff3fSPhilip Chen struct drm_dp_aux_msg *msg) 301*826cff3fSPhilip Chen { 302*826cff3fSPhilip Chen struct ps8640 *ps_bridge = aux_to_ps8640(aux); 303*826cff3fSPhilip Chen struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev; 304*826cff3fSPhilip Chen int ret; 305*826cff3fSPhilip Chen 306*826cff3fSPhilip Chen pm_runtime_get_sync(dev); 307*826cff3fSPhilip Chen ret = ps8640_ensure_hpd(ps_bridge); 308*826cff3fSPhilip Chen if (!ret) 309*826cff3fSPhilip Chen ret = ps8640_aux_transfer_msg(aux, msg); 310*826cff3fSPhilip Chen pm_runtime_mark_last_busy(dev); 311*826cff3fSPhilip Chen pm_runtime_put_autosuspend(dev); 312*826cff3fSPhilip Chen 313*826cff3fSPhilip Chen return ret; 314*826cff3fSPhilip Chen } 315*826cff3fSPhilip Chen 316*826cff3fSPhilip Chen static void ps8640_bridge_vdo_control(struct ps8640 *ps_bridge, 317bc1aee7fSJitao Shi const enum ps8640_vdo_control ctrl) 318bc1aee7fSJitao Shi { 319692d8db0SPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE3_DSI_CNTL1]; 320*826cff3fSPhilip Chen struct device *dev = &ps_bridge->page[PAGE3_DSI_CNTL1]->dev; 321bc1aee7fSJitao Shi u8 vdo_ctrl_buf[] = { VDO_CTL_ADD, ctrl }; 322bc1aee7fSJitao Shi int ret; 323bc1aee7fSJitao Shi 324692d8db0SPhilip Chen ret = regmap_bulk_write(map, PAGE3_SET_ADD, 325692d8db0SPhilip Chen vdo_ctrl_buf, sizeof(vdo_ctrl_buf)); 326692d8db0SPhilip Chen 327*826cff3fSPhilip Chen if (ret < 0) 328*826cff3fSPhilip Chen dev_err(dev, "failed to %sable VDO: %d\n", 32994d4c132SEnric Balletbo i Serra ctrl == ENABLE ? "en" : "dis", ret); 33094d4c132SEnric Balletbo i Serra } 331bc1aee7fSJitao Shi 332*826cff3fSPhilip Chen static int __maybe_unused ps8640_resume(struct device *dev) 333bc1aee7fSJitao Shi { 334*826cff3fSPhilip Chen struct ps8640 *ps_bridge = dev_get_drvdata(dev); 335*826cff3fSPhilip Chen int ret; 33646f20630SEnric Balletbo i Serra 337bc1aee7fSJitao Shi ret = regulator_bulk_enable(ARRAY_SIZE(ps_bridge->supplies), 338bc1aee7fSJitao Shi ps_bridge->supplies); 339bc1aee7fSJitao Shi if (ret < 0) { 340*826cff3fSPhilip Chen dev_err(dev, "cannot enable regulators %d\n", ret); 341*826cff3fSPhilip Chen return ret; 342bc1aee7fSJitao Shi } 343bc1aee7fSJitao Shi 344bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_powerdown, 0); 345bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_reset, 1); 346bc1aee7fSJitao Shi usleep_range(2000, 2500); 347bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_reset, 0); 348bc1aee7fSJitao Shi 349bc1aee7fSJitao Shi /* 350*826cff3fSPhilip Chen * Mystery 200 ms delay for the "MCU to be ready". It's unclear if 351*826cff3fSPhilip Chen * this is truly necessary since the MCU will already signal that 352*826cff3fSPhilip Chen * things are "good to go" by signaling HPD on "gpio 9". See 353*826cff3fSPhilip Chen * ps8640_ensure_hpd(). For now we'll keep this mystery delay just in 354*826cff3fSPhilip Chen * case. 355bc1aee7fSJitao Shi */ 356bc1aee7fSJitao Shi msleep(200); 357bc1aee7fSJitao Shi 358*826cff3fSPhilip Chen return 0; 359bc1aee7fSJitao Shi } 360bc1aee7fSJitao Shi 361*826cff3fSPhilip Chen static int __maybe_unused ps8640_suspend(struct device *dev) 362*826cff3fSPhilip Chen { 363*826cff3fSPhilip Chen struct ps8640 *ps_bridge = dev_get_drvdata(dev); 364*826cff3fSPhilip Chen int ret; 365*826cff3fSPhilip Chen 366*826cff3fSPhilip Chen gpiod_set_value(ps_bridge->gpio_reset, 1); 367*826cff3fSPhilip Chen gpiod_set_value(ps_bridge->gpio_powerdown, 1); 368*826cff3fSPhilip Chen ret = regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies), 369*826cff3fSPhilip Chen ps_bridge->supplies); 370*826cff3fSPhilip Chen if (ret < 0) 371*826cff3fSPhilip Chen dev_err(dev, "cannot disable regulators %d\n", ret); 372*826cff3fSPhilip Chen 373*826cff3fSPhilip Chen return ret; 374*826cff3fSPhilip Chen } 375*826cff3fSPhilip Chen 376*826cff3fSPhilip Chen static const struct dev_pm_ops ps8640_pm_ops = { 377*826cff3fSPhilip Chen SET_RUNTIME_PM_OPS(ps8640_suspend, ps8640_resume, NULL) 378*826cff3fSPhilip Chen SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 379*826cff3fSPhilip Chen pm_runtime_force_resume) 380*826cff3fSPhilip Chen }; 381*826cff3fSPhilip Chen 382*826cff3fSPhilip Chen static void ps8640_pre_enable(struct drm_bridge *bridge) 383*826cff3fSPhilip Chen { 384*826cff3fSPhilip Chen struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 385*826cff3fSPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE2_TOP_CNTL]; 386*826cff3fSPhilip Chen struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev; 387*826cff3fSPhilip Chen int ret; 388*826cff3fSPhilip Chen 389*826cff3fSPhilip Chen pm_runtime_get_sync(dev); 390*826cff3fSPhilip Chen ps8640_ensure_hpd(ps_bridge); 391bc1aee7fSJitao Shi 392bc1aee7fSJitao Shi /* 393bc1aee7fSJitao Shi * The Manufacturer Command Set (MCS) is a device dependent interface 394bc1aee7fSJitao Shi * intended for factory programming of the display module default 395bc1aee7fSJitao Shi * parameters. Once the display module is configured, the MCS shall be 396bc1aee7fSJitao Shi * disabled by the manufacturer. Once disabled, all MCS commands are 397bc1aee7fSJitao Shi * ignored by the display interface. 398bc1aee7fSJitao Shi */ 399bc1aee7fSJitao Shi 400692d8db0SPhilip Chen ret = regmap_update_bits(map, PAGE2_MCS_EN, MCS_EN, 0); 401*826cff3fSPhilip Chen if (ret < 0) 402*826cff3fSPhilip Chen dev_warn(dev, "failed write PAGE2_MCS_EN: %d\n", ret); 403bc1aee7fSJitao Shi 404bc1aee7fSJitao Shi /* Switch access edp panel's edid through i2c */ 405692d8db0SPhilip Chen ret = regmap_write(map, PAGE2_I2C_BYPASS, I2C_BYPASS_EN); 406bc1aee7fSJitao Shi if (ret < 0) 407*826cff3fSPhilip Chen dev_warn(dev, "failed write PAGE2_MCS_EN: %d\n", ret); 40846f20630SEnric Balletbo i Serra 409*826cff3fSPhilip Chen ps8640_bridge_vdo_control(ps_bridge, ENABLE); 41046f20630SEnric Balletbo i Serra 411*826cff3fSPhilip Chen ps_bridge->pre_enabled = true; 41246f20630SEnric Balletbo i Serra } 41346f20630SEnric Balletbo i Serra 41446f20630SEnric Balletbo i Serra static void ps8640_post_disable(struct drm_bridge *bridge) 41546f20630SEnric Balletbo i Serra { 41646f20630SEnric Balletbo i Serra struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 41746f20630SEnric Balletbo i Serra 418*826cff3fSPhilip Chen ps_bridge->pre_enabled = false; 419*826cff3fSPhilip Chen 42046f20630SEnric Balletbo i Serra ps8640_bridge_vdo_control(ps_bridge, DISABLE); 421*826cff3fSPhilip Chen pm_runtime_put_sync_suspend(&ps_bridge->page[PAGE0_DP_CNTL]->dev); 422bc1aee7fSJitao Shi } 423bc1aee7fSJitao Shi 424a25b988fSLaurent Pinchart static int ps8640_bridge_attach(struct drm_bridge *bridge, 425a25b988fSLaurent Pinchart enum drm_bridge_attach_flags flags) 426bc1aee7fSJitao Shi { 427bc1aee7fSJitao Shi struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 428bc1aee7fSJitao Shi struct device *dev = &ps_bridge->page[0]->dev; 429bc1aee7fSJitao Shi int ret; 430812a65baSEnric Balletbo i Serra 431812a65baSEnric Balletbo i Serra if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) 432812a65baSEnric Balletbo i Serra return -EINVAL; 433812a65baSEnric Balletbo i Serra 43413afcdd7SPhilip Chen ret = drm_dp_aux_register(&ps_bridge->aux); 43513afcdd7SPhilip Chen if (ret) { 43613afcdd7SPhilip Chen dev_err(dev, "failed to register DP AUX channel: %d\n", ret); 437fe93ae80SMaxime Ripard return ret; 43813afcdd7SPhilip Chen } 439bc1aee7fSJitao Shi 440bc1aee7fSJitao Shi /* Attach the panel-bridge to the dsi bridge */ 441bc1aee7fSJitao Shi return drm_bridge_attach(bridge->encoder, ps_bridge->panel_bridge, 442a25b988fSLaurent Pinchart &ps_bridge->bridge, flags); 443bc1aee7fSJitao Shi } 444bc1aee7fSJitao Shi 44513afcdd7SPhilip Chen static void ps8640_bridge_detach(struct drm_bridge *bridge) 44613afcdd7SPhilip Chen { 44713afcdd7SPhilip Chen drm_dp_aux_unregister(&bridge_to_ps8640(bridge)->aux); 44813afcdd7SPhilip Chen } 44913afcdd7SPhilip Chen 450d82c12abSEnric Balletbo i Serra static struct edid *ps8640_bridge_get_edid(struct drm_bridge *bridge, 451d82c12abSEnric Balletbo i Serra struct drm_connector *connector) 452d82c12abSEnric Balletbo i Serra { 453d82c12abSEnric Balletbo i Serra struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 454*826cff3fSPhilip Chen bool poweroff = !ps_bridge->pre_enabled; 45546f20630SEnric Balletbo i Serra struct edid *edid; 456d82c12abSEnric Balletbo i Serra 45746f20630SEnric Balletbo i Serra /* 45846f20630SEnric Balletbo i Serra * When we end calling get_edid() triggered by an ioctl, i.e 45946f20630SEnric Balletbo i Serra * 46046f20630SEnric Balletbo i Serra * drm_mode_getconnector (ioctl) 46146f20630SEnric Balletbo i Serra * -> drm_helper_probe_single_connector_modes 46246f20630SEnric Balletbo i Serra * -> drm_bridge_connector_get_modes 46346f20630SEnric Balletbo i Serra * -> ps8640_bridge_get_edid 46446f20630SEnric Balletbo i Serra * 46546f20630SEnric Balletbo i Serra * We need to make sure that what we need is enabled before reading 46646f20630SEnric Balletbo i Serra * EDID, for this chip, we need to do a full poweron, otherwise it will 46746f20630SEnric Balletbo i Serra * fail. 46846f20630SEnric Balletbo i Serra */ 46946f20630SEnric Balletbo i Serra drm_bridge_chain_pre_enable(bridge); 47046f20630SEnric Balletbo i Serra 47146f20630SEnric Balletbo i Serra edid = drm_get_edid(connector, 472d82c12abSEnric Balletbo i Serra ps_bridge->page[PAGE0_DP_CNTL]->adapter); 47346f20630SEnric Balletbo i Serra 47446f20630SEnric Balletbo i Serra /* 47546f20630SEnric Balletbo i Serra * If we call the get_edid() function without having enabled the chip 47646f20630SEnric Balletbo i Serra * before, return the chip to its original power state. 47746f20630SEnric Balletbo i Serra */ 47846f20630SEnric Balletbo i Serra if (poweroff) 47946f20630SEnric Balletbo i Serra drm_bridge_chain_post_disable(bridge); 48046f20630SEnric Balletbo i Serra 48146f20630SEnric Balletbo i Serra return edid; 482d82c12abSEnric Balletbo i Serra } 483d82c12abSEnric Balletbo i Serra 484*826cff3fSPhilip Chen static void ps8640_runtime_disable(void *data) 485*826cff3fSPhilip Chen { 486*826cff3fSPhilip Chen pm_runtime_dont_use_autosuspend(data); 487*826cff3fSPhilip Chen pm_runtime_disable(data); 488*826cff3fSPhilip Chen } 489*826cff3fSPhilip Chen 490bc1aee7fSJitao Shi static const struct drm_bridge_funcs ps8640_bridge_funcs = { 491bc1aee7fSJitao Shi .attach = ps8640_bridge_attach, 49213afcdd7SPhilip Chen .detach = ps8640_bridge_detach, 493d82c12abSEnric Balletbo i Serra .get_edid = ps8640_bridge_get_edid, 494bc1aee7fSJitao Shi .post_disable = ps8640_post_disable, 495bc1aee7fSJitao Shi .pre_enable = ps8640_pre_enable, 496bc1aee7fSJitao Shi }; 497bc1aee7fSJitao Shi 4987abbc26fSMaxime Ripard static int ps8640_bridge_host_attach(struct device *dev, struct ps8640 *ps_bridge) 4997abbc26fSMaxime Ripard { 5007abbc26fSMaxime Ripard struct device_node *in_ep, *dsi_node; 5017abbc26fSMaxime Ripard struct mipi_dsi_device *dsi; 5027abbc26fSMaxime Ripard struct mipi_dsi_host *host; 5037abbc26fSMaxime Ripard int ret; 5047abbc26fSMaxime Ripard const struct mipi_dsi_device_info info = { .type = "ps8640", 5057abbc26fSMaxime Ripard .channel = 0, 5067abbc26fSMaxime Ripard .node = NULL, 5077abbc26fSMaxime Ripard }; 5087abbc26fSMaxime Ripard 5097abbc26fSMaxime Ripard /* port@0 is ps8640 dsi input port */ 5107abbc26fSMaxime Ripard in_ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1); 5117abbc26fSMaxime Ripard if (!in_ep) 5127abbc26fSMaxime Ripard return -ENODEV; 5137abbc26fSMaxime Ripard 5147abbc26fSMaxime Ripard dsi_node = of_graph_get_remote_port_parent(in_ep); 5157abbc26fSMaxime Ripard of_node_put(in_ep); 5167abbc26fSMaxime Ripard if (!dsi_node) 5177abbc26fSMaxime Ripard return -ENODEV; 5187abbc26fSMaxime Ripard 5197abbc26fSMaxime Ripard host = of_find_mipi_dsi_host_by_node(dsi_node); 5207abbc26fSMaxime Ripard of_node_put(dsi_node); 5217abbc26fSMaxime Ripard if (!host) 5227abbc26fSMaxime Ripard return -EPROBE_DEFER; 5237abbc26fSMaxime Ripard 5247abbc26fSMaxime Ripard dsi = devm_mipi_dsi_device_register_full(dev, host, &info); 5257abbc26fSMaxime Ripard if (IS_ERR(dsi)) { 5267abbc26fSMaxime Ripard dev_err(dev, "failed to create dsi device\n"); 5277abbc26fSMaxime Ripard return PTR_ERR(dsi); 5287abbc26fSMaxime Ripard } 5297abbc26fSMaxime Ripard 5307abbc26fSMaxime Ripard ps_bridge->dsi = dsi; 5317abbc26fSMaxime Ripard 5327abbc26fSMaxime Ripard dsi->host = host; 5337abbc26fSMaxime Ripard dsi->mode_flags = MIPI_DSI_MODE_VIDEO | 5347abbc26fSMaxime Ripard MIPI_DSI_MODE_VIDEO_SYNC_PULSE; 5357abbc26fSMaxime Ripard dsi->format = MIPI_DSI_FMT_RGB888; 5367abbc26fSMaxime Ripard dsi->lanes = NUM_MIPI_LANES; 5377abbc26fSMaxime Ripard 5387abbc26fSMaxime Ripard ret = devm_mipi_dsi_attach(dev, dsi); 5397abbc26fSMaxime Ripard if (ret) 5407abbc26fSMaxime Ripard return ret; 5417abbc26fSMaxime Ripard 5427abbc26fSMaxime Ripard return 0; 5437abbc26fSMaxime Ripard } 5447abbc26fSMaxime Ripard 545bc1aee7fSJitao Shi static int ps8640_probe(struct i2c_client *client) 546bc1aee7fSJitao Shi { 547bc1aee7fSJitao Shi struct device *dev = &client->dev; 548bc1aee7fSJitao Shi struct device_node *np = dev->of_node; 549bc1aee7fSJitao Shi struct ps8640 *ps_bridge; 550bc1aee7fSJitao Shi struct drm_panel *panel; 551bc1aee7fSJitao Shi int ret; 552bc1aee7fSJitao Shi u32 i; 553bc1aee7fSJitao Shi 554bc1aee7fSJitao Shi ps_bridge = devm_kzalloc(dev, sizeof(*ps_bridge), GFP_KERNEL); 555bc1aee7fSJitao Shi if (!ps_bridge) 556bc1aee7fSJitao Shi return -ENOMEM; 557bc1aee7fSJitao Shi 558bc1aee7fSJitao Shi /* port@1 is ps8640 output port */ 559bc1aee7fSJitao Shi ret = drm_of_find_panel_or_bridge(np, 1, 0, &panel, NULL); 560bc1aee7fSJitao Shi if (ret < 0) 561bc1aee7fSJitao Shi return ret; 562bc1aee7fSJitao Shi if (!panel) 563bc1aee7fSJitao Shi return -ENODEV; 564bc1aee7fSJitao Shi 565bc1aee7fSJitao Shi ps_bridge->panel_bridge = devm_drm_panel_bridge_add(dev, panel); 566bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->panel_bridge)) 567bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->panel_bridge); 568bc1aee7fSJitao Shi 569bc1aee7fSJitao Shi ps_bridge->supplies[0].supply = "vdd33"; 570bc1aee7fSJitao Shi ps_bridge->supplies[1].supply = "vdd12"; 571bc1aee7fSJitao Shi ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ps_bridge->supplies), 572bc1aee7fSJitao Shi ps_bridge->supplies); 573bc1aee7fSJitao Shi if (ret) 574bc1aee7fSJitao Shi return ret; 575bc1aee7fSJitao Shi 576bc1aee7fSJitao Shi ps_bridge->gpio_powerdown = devm_gpiod_get(&client->dev, "powerdown", 577bc1aee7fSJitao Shi GPIOD_OUT_HIGH); 578bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->gpio_powerdown)) 579bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->gpio_powerdown); 580bc1aee7fSJitao Shi 581bc1aee7fSJitao Shi /* 582bc1aee7fSJitao Shi * Assert the reset to avoid the bridge being initialized prematurely 583bc1aee7fSJitao Shi */ 584bc1aee7fSJitao Shi ps_bridge->gpio_reset = devm_gpiod_get(&client->dev, "reset", 585bc1aee7fSJitao Shi GPIOD_OUT_HIGH); 586bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->gpio_reset)) 587bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->gpio_reset); 588bc1aee7fSJitao Shi 589bc1aee7fSJitao Shi ps_bridge->bridge.funcs = &ps8640_bridge_funcs; 590bc1aee7fSJitao Shi ps_bridge->bridge.of_node = dev->of_node; 591d82c12abSEnric Balletbo i Serra ps_bridge->bridge.ops = DRM_BRIDGE_OP_EDID; 592d82c12abSEnric Balletbo i Serra ps_bridge->bridge.type = DRM_MODE_CONNECTOR_eDP; 593bc1aee7fSJitao Shi 594bc1aee7fSJitao Shi ps_bridge->page[PAGE0_DP_CNTL] = client; 595bc1aee7fSJitao Shi 596692d8db0SPhilip Chen ps_bridge->regmap[PAGE0_DP_CNTL] = devm_regmap_init_i2c(client, ps8640_regmap_config); 597692d8db0SPhilip Chen if (IS_ERR(ps_bridge->regmap[PAGE0_DP_CNTL])) 598692d8db0SPhilip Chen return PTR_ERR(ps_bridge->regmap[PAGE0_DP_CNTL]); 599692d8db0SPhilip Chen 600bc1aee7fSJitao Shi for (i = 1; i < ARRAY_SIZE(ps_bridge->page); i++) { 601bc1aee7fSJitao Shi ps_bridge->page[i] = devm_i2c_new_dummy_device(&client->dev, 602bc1aee7fSJitao Shi client->adapter, 603bc1aee7fSJitao Shi client->addr + i); 604692d8db0SPhilip Chen if (IS_ERR(ps_bridge->page[i])) 605bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->page[i]); 606692d8db0SPhilip Chen 607692d8db0SPhilip Chen ps_bridge->regmap[i] = devm_regmap_init_i2c(ps_bridge->page[i], 608692d8db0SPhilip Chen ps8640_regmap_config + i); 609692d8db0SPhilip Chen if (IS_ERR(ps_bridge->regmap[i])) 610692d8db0SPhilip Chen return PTR_ERR(ps_bridge->regmap[i]); 611bc1aee7fSJitao Shi } 612bc1aee7fSJitao Shi 613bc1aee7fSJitao Shi i2c_set_clientdata(client, ps_bridge); 614bc1aee7fSJitao Shi 61513afcdd7SPhilip Chen ps_bridge->aux.name = "parade-ps8640-aux"; 61613afcdd7SPhilip Chen ps_bridge->aux.dev = dev; 61713afcdd7SPhilip Chen ps_bridge->aux.transfer = ps8640_aux_transfer; 61813afcdd7SPhilip Chen drm_dp_aux_init(&ps_bridge->aux); 61913afcdd7SPhilip Chen 620*826cff3fSPhilip Chen pm_runtime_enable(dev); 621*826cff3fSPhilip Chen /* 622*826cff3fSPhilip Chen * Powering on ps8640 takes ~300ms. To avoid wasting time on power 623*826cff3fSPhilip Chen * cycling ps8640 too often, set autosuspend_delay to 500ms to ensure 624*826cff3fSPhilip Chen * the bridge wouldn't suspend in between each _aux_transfer_msg() call 625*826cff3fSPhilip Chen * during EDID read (~20ms in my experiment) and in between the last 626*826cff3fSPhilip Chen * _aux_transfer_msg() call during EDID read and the _pre_enable() call 627*826cff3fSPhilip Chen * (~100ms in my experiment). 628*826cff3fSPhilip Chen */ 629*826cff3fSPhilip Chen pm_runtime_set_autosuspend_delay(dev, 500); 630*826cff3fSPhilip Chen pm_runtime_use_autosuspend(dev); 631*826cff3fSPhilip Chen pm_suspend_ignore_children(dev, true); 632*826cff3fSPhilip Chen ret = devm_add_action_or_reset(dev, ps8640_runtime_disable, dev); 633*826cff3fSPhilip Chen if (ret) 634*826cff3fSPhilip Chen return ret; 635*826cff3fSPhilip Chen 636bc1aee7fSJitao Shi drm_bridge_add(&ps_bridge->bridge); 637bc1aee7fSJitao Shi 6387abbc26fSMaxime Ripard ret = ps8640_bridge_host_attach(dev, ps_bridge); 6397abbc26fSMaxime Ripard if (ret) 6407abbc26fSMaxime Ripard goto err_bridge_remove; 6417abbc26fSMaxime Ripard 642bc1aee7fSJitao Shi return 0; 6437abbc26fSMaxime Ripard 6447abbc26fSMaxime Ripard err_bridge_remove: 6457abbc26fSMaxime Ripard drm_bridge_remove(&ps_bridge->bridge); 6467abbc26fSMaxime Ripard return ret; 647bc1aee7fSJitao Shi } 648bc1aee7fSJitao Shi 649bc1aee7fSJitao Shi static int ps8640_remove(struct i2c_client *client) 650bc1aee7fSJitao Shi { 651bc1aee7fSJitao Shi struct ps8640 *ps_bridge = i2c_get_clientdata(client); 652bc1aee7fSJitao Shi 653bc1aee7fSJitao Shi drm_bridge_remove(&ps_bridge->bridge); 654bc1aee7fSJitao Shi 655bc1aee7fSJitao Shi return 0; 656bc1aee7fSJitao Shi } 657bc1aee7fSJitao Shi 658bc1aee7fSJitao Shi static const struct of_device_id ps8640_match[] = { 659bc1aee7fSJitao Shi { .compatible = "parade,ps8640" }, 660bc1aee7fSJitao Shi { } 661bc1aee7fSJitao Shi }; 662bc1aee7fSJitao Shi MODULE_DEVICE_TABLE(of, ps8640_match); 663bc1aee7fSJitao Shi 664bc1aee7fSJitao Shi static struct i2c_driver ps8640_driver = { 665bc1aee7fSJitao Shi .probe_new = ps8640_probe, 666bc1aee7fSJitao Shi .remove = ps8640_remove, 667bc1aee7fSJitao Shi .driver = { 668bc1aee7fSJitao Shi .name = "ps8640", 669bc1aee7fSJitao Shi .of_match_table = ps8640_match, 670*826cff3fSPhilip Chen .pm = &ps8640_pm_ops, 671bc1aee7fSJitao Shi }, 672bc1aee7fSJitao Shi }; 673bc1aee7fSJitao Shi module_i2c_driver(ps8640_driver); 674bc1aee7fSJitao Shi 675bc1aee7fSJitao Shi MODULE_AUTHOR("Jitao Shi <jitao.shi@mediatek.com>"); 676bc1aee7fSJitao Shi MODULE_AUTHOR("CK Hu <ck.hu@mediatek.com>"); 677bc1aee7fSJitao Shi MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>"); 678bc1aee7fSJitao Shi MODULE_DESCRIPTION("PARADE ps8640 DSI-eDP converter driver"); 679bc1aee7fSJitao Shi MODULE_LICENSE("GPL v2"); 680