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> 12692d8db0SPhilip Chen #include <linux/regmap.h> 13bc1aee7fSJitao Shi #include <linux/regulator/consumer.h> 14bc1aee7fSJitao Shi 15bc1aee7fSJitao Shi #include <drm/drm_bridge.h> 1613afcdd7SPhilip Chen #include <drm/drm_dp_helper.h> 17bc1aee7fSJitao Shi #include <drm/drm_mipi_dsi.h> 18bc1aee7fSJitao Shi #include <drm/drm_of.h> 19bc1aee7fSJitao Shi #include <drm/drm_panel.h> 20bc1aee7fSJitao Shi #include <drm/drm_print.h> 21bc1aee7fSJitao Shi 2213afcdd7SPhilip Chen #define PAGE0_AUXCH_CFG3 0x76 2313afcdd7SPhilip Chen #define AUXCH_CFG3_RESET 0xff 2413afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_7_0 0x7d 2513afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_15_8 0x7e 2613afcdd7SPhilip Chen #define PAGE0_SWAUX_ADDR_23_16 0x7f 2713afcdd7SPhilip Chen #define SWAUX_ADDR_MASK GENMASK(19, 0) 2813afcdd7SPhilip Chen #define PAGE0_SWAUX_LENGTH 0x80 2913afcdd7SPhilip Chen #define SWAUX_LENGTH_MASK GENMASK(3, 0) 3013afcdd7SPhilip Chen #define SWAUX_NO_PAYLOAD BIT(7) 3113afcdd7SPhilip Chen #define PAGE0_SWAUX_WDATA 0x81 3213afcdd7SPhilip Chen #define PAGE0_SWAUX_RDATA 0x82 3313afcdd7SPhilip Chen #define PAGE0_SWAUX_CTRL 0x83 3413afcdd7SPhilip Chen #define SWAUX_SEND BIT(0) 3513afcdd7SPhilip Chen #define PAGE0_SWAUX_STATUS 0x84 3613afcdd7SPhilip Chen #define SWAUX_M_MASK GENMASK(4, 0) 3713afcdd7SPhilip Chen #define SWAUX_STATUS_MASK GENMASK(7, 5) 3813afcdd7SPhilip Chen #define SWAUX_STATUS_NACK (0x1 << 5) 3913afcdd7SPhilip Chen #define SWAUX_STATUS_DEFER (0x2 << 5) 4013afcdd7SPhilip Chen #define SWAUX_STATUS_ACKM (0x3 << 5) 4113afcdd7SPhilip Chen #define SWAUX_STATUS_INVALID (0x4 << 5) 4213afcdd7SPhilip Chen #define SWAUX_STATUS_I2C_NACK (0x5 << 5) 4313afcdd7SPhilip Chen #define SWAUX_STATUS_I2C_DEFER (0x6 << 5) 4413afcdd7SPhilip Chen #define SWAUX_STATUS_TIMEOUT (0x7 << 5) 4513afcdd7SPhilip Chen 46bc1aee7fSJitao Shi #define PAGE2_GPIO_H 0xa7 47bc1aee7fSJitao Shi #define PS_GPIO9 BIT(1) 48bc1aee7fSJitao Shi #define PAGE2_I2C_BYPASS 0xea 49bc1aee7fSJitao Shi #define I2C_BYPASS_EN 0xd0 50bc1aee7fSJitao Shi #define PAGE2_MCS_EN 0xf3 51bc1aee7fSJitao Shi #define MCS_EN BIT(0) 5228210a3fSPhilip Chen 53bc1aee7fSJitao Shi #define PAGE3_SET_ADD 0xfe 54bc1aee7fSJitao Shi #define VDO_CTL_ADD 0x13 55bc1aee7fSJitao Shi #define VDO_DIS 0x18 56bc1aee7fSJitao Shi #define VDO_EN 0x1c 5728210a3fSPhilip Chen 5828210a3fSPhilip Chen #define NUM_MIPI_LANES 4 59bc1aee7fSJitao Shi 60692d8db0SPhilip Chen #define COMMON_PS8640_REGMAP_CONFIG \ 61692d8db0SPhilip Chen .reg_bits = 8, \ 62692d8db0SPhilip Chen .val_bits = 8, \ 63692d8db0SPhilip Chen .cache_type = REGCACHE_NONE 64692d8db0SPhilip Chen 65bc1aee7fSJitao Shi /* 66bc1aee7fSJitao Shi * PS8640 uses multiple addresses: 67bc1aee7fSJitao Shi * page[0]: for DP control 68bc1aee7fSJitao Shi * page[1]: for VIDEO Bridge 69bc1aee7fSJitao Shi * page[2]: for control top 70bc1aee7fSJitao Shi * page[3]: for DSI Link Control1 71bc1aee7fSJitao Shi * page[4]: for MIPI Phy 72bc1aee7fSJitao Shi * page[5]: for VPLL 73bc1aee7fSJitao Shi * page[6]: for DSI Link Control2 74bc1aee7fSJitao Shi * page[7]: for SPI ROM mapping 75bc1aee7fSJitao Shi */ 76bc1aee7fSJitao Shi enum page_addr_offset { 77bc1aee7fSJitao Shi PAGE0_DP_CNTL = 0, 78bc1aee7fSJitao Shi PAGE1_VDO_BDG, 79bc1aee7fSJitao Shi PAGE2_TOP_CNTL, 80bc1aee7fSJitao Shi PAGE3_DSI_CNTL1, 81bc1aee7fSJitao Shi PAGE4_MIPI_PHY, 82bc1aee7fSJitao Shi PAGE5_VPLL, 83bc1aee7fSJitao Shi PAGE6_DSI_CNTL2, 84bc1aee7fSJitao Shi PAGE7_SPI_CNTL, 85bc1aee7fSJitao Shi MAX_DEVS 86bc1aee7fSJitao Shi }; 87bc1aee7fSJitao Shi 88bc1aee7fSJitao Shi enum ps8640_vdo_control { 89bc1aee7fSJitao Shi DISABLE = VDO_DIS, 90bc1aee7fSJitao Shi ENABLE = VDO_EN, 91bc1aee7fSJitao Shi }; 92bc1aee7fSJitao Shi 93bc1aee7fSJitao Shi struct ps8640 { 94bc1aee7fSJitao Shi struct drm_bridge bridge; 95bc1aee7fSJitao Shi struct drm_bridge *panel_bridge; 9613afcdd7SPhilip Chen struct drm_dp_aux aux; 97bc1aee7fSJitao Shi struct mipi_dsi_device *dsi; 98bc1aee7fSJitao Shi struct i2c_client *page[MAX_DEVS]; 99692d8db0SPhilip Chen struct regmap *regmap[MAX_DEVS]; 100bc1aee7fSJitao Shi struct regulator_bulk_data supplies[2]; 101bc1aee7fSJitao Shi struct gpio_desc *gpio_reset; 102bc1aee7fSJitao Shi struct gpio_desc *gpio_powerdown; 10346f20630SEnric Balletbo i Serra bool powered; 104bc1aee7fSJitao Shi }; 105bc1aee7fSJitao Shi 106692d8db0SPhilip Chen static const struct regmap_config ps8640_regmap_config[] = { 107692d8db0SPhilip Chen [PAGE0_DP_CNTL] = { 108692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 109692d8db0SPhilip Chen .max_register = 0xbf, 110692d8db0SPhilip Chen }, 111692d8db0SPhilip Chen [PAGE1_VDO_BDG] = { 112692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 113692d8db0SPhilip Chen .max_register = 0xff, 114692d8db0SPhilip Chen }, 115692d8db0SPhilip Chen [PAGE2_TOP_CNTL] = { 116692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 117692d8db0SPhilip Chen .max_register = 0xff, 118692d8db0SPhilip Chen }, 119692d8db0SPhilip Chen [PAGE3_DSI_CNTL1] = { 120692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 121692d8db0SPhilip Chen .max_register = 0xff, 122692d8db0SPhilip Chen }, 123692d8db0SPhilip Chen [PAGE4_MIPI_PHY] = { 124692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 125692d8db0SPhilip Chen .max_register = 0xff, 126692d8db0SPhilip Chen }, 127692d8db0SPhilip Chen [PAGE5_VPLL] = { 128692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 129692d8db0SPhilip Chen .max_register = 0x7f, 130692d8db0SPhilip Chen }, 131692d8db0SPhilip Chen [PAGE6_DSI_CNTL2] = { 132692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 133692d8db0SPhilip Chen .max_register = 0xff, 134692d8db0SPhilip Chen }, 135692d8db0SPhilip Chen [PAGE7_SPI_CNTL] = { 136692d8db0SPhilip Chen COMMON_PS8640_REGMAP_CONFIG, 137692d8db0SPhilip Chen .max_register = 0xff, 138692d8db0SPhilip Chen }, 139692d8db0SPhilip Chen }; 140692d8db0SPhilip Chen 141bc1aee7fSJitao Shi static inline struct ps8640 *bridge_to_ps8640(struct drm_bridge *e) 142bc1aee7fSJitao Shi { 143bc1aee7fSJitao Shi return container_of(e, struct ps8640, bridge); 144bc1aee7fSJitao Shi } 145bc1aee7fSJitao Shi 14613afcdd7SPhilip Chen static inline struct ps8640 *aux_to_ps8640(struct drm_dp_aux *aux) 14713afcdd7SPhilip Chen { 14813afcdd7SPhilip Chen return container_of(aux, struct ps8640, aux); 14913afcdd7SPhilip Chen } 15013afcdd7SPhilip Chen 15113afcdd7SPhilip Chen static ssize_t ps8640_aux_transfer(struct drm_dp_aux *aux, 15213afcdd7SPhilip Chen struct drm_dp_aux_msg *msg) 15313afcdd7SPhilip Chen { 15413afcdd7SPhilip Chen struct ps8640 *ps_bridge = aux_to_ps8640(aux); 15513afcdd7SPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE0_DP_CNTL]; 15613afcdd7SPhilip Chen struct device *dev = &ps_bridge->page[PAGE0_DP_CNTL]->dev; 15713afcdd7SPhilip Chen unsigned int len = msg->size; 15813afcdd7SPhilip Chen unsigned int data; 15913afcdd7SPhilip Chen unsigned int base; 16013afcdd7SPhilip Chen int ret; 16113afcdd7SPhilip Chen u8 request = msg->request & 16213afcdd7SPhilip Chen ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE); 16313afcdd7SPhilip Chen u8 *buf = msg->buffer; 16413afcdd7SPhilip Chen u8 addr_len[PAGE0_SWAUX_LENGTH + 1 - PAGE0_SWAUX_ADDR_7_0]; 16513afcdd7SPhilip Chen u8 i; 16613afcdd7SPhilip Chen bool is_native_aux = false; 16713afcdd7SPhilip Chen 16813afcdd7SPhilip Chen if (len > DP_AUX_MAX_PAYLOAD_BYTES) 16913afcdd7SPhilip Chen return -EINVAL; 17013afcdd7SPhilip Chen 17113afcdd7SPhilip Chen if (msg->address & ~SWAUX_ADDR_MASK) 17213afcdd7SPhilip Chen return -EINVAL; 17313afcdd7SPhilip Chen 17413afcdd7SPhilip Chen switch (request) { 17513afcdd7SPhilip Chen case DP_AUX_NATIVE_WRITE: 17613afcdd7SPhilip Chen case DP_AUX_NATIVE_READ: 17713afcdd7SPhilip Chen is_native_aux = true; 17813afcdd7SPhilip Chen fallthrough; 17913afcdd7SPhilip Chen case DP_AUX_I2C_WRITE: 18013afcdd7SPhilip Chen case DP_AUX_I2C_READ: 18113afcdd7SPhilip Chen break; 18213afcdd7SPhilip Chen default: 18313afcdd7SPhilip Chen return -EINVAL; 18413afcdd7SPhilip Chen } 18513afcdd7SPhilip Chen 18613afcdd7SPhilip Chen ret = regmap_write(map, PAGE0_AUXCH_CFG3, AUXCH_CFG3_RESET); 18713afcdd7SPhilip Chen if (ret) { 18813afcdd7SPhilip Chen DRM_DEV_ERROR(dev, "failed to write PAGE0_AUXCH_CFG3: %d\n", 18913afcdd7SPhilip Chen ret); 19013afcdd7SPhilip Chen return ret; 19113afcdd7SPhilip Chen } 19213afcdd7SPhilip Chen 19313afcdd7SPhilip Chen /* Assume it's good */ 19413afcdd7SPhilip Chen msg->reply = 0; 19513afcdd7SPhilip Chen 19613afcdd7SPhilip Chen base = PAGE0_SWAUX_ADDR_7_0; 19713afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_7_0 - base] = msg->address; 19813afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_15_8 - base] = msg->address >> 8; 19913afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_ADDR_23_16 - base] = (msg->address >> 16) | 20013afcdd7SPhilip Chen (msg->request << 4); 20113afcdd7SPhilip Chen addr_len[PAGE0_SWAUX_LENGTH - base] = (len == 0) ? SWAUX_NO_PAYLOAD : 20213afcdd7SPhilip Chen ((len - 1) & SWAUX_LENGTH_MASK); 20313afcdd7SPhilip Chen 20413afcdd7SPhilip Chen regmap_bulk_write(map, PAGE0_SWAUX_ADDR_7_0, addr_len, 20513afcdd7SPhilip Chen ARRAY_SIZE(addr_len)); 20613afcdd7SPhilip Chen 20713afcdd7SPhilip Chen if (len && (request == DP_AUX_NATIVE_WRITE || 20813afcdd7SPhilip Chen request == DP_AUX_I2C_WRITE)) { 20913afcdd7SPhilip Chen /* Write to the internal FIFO buffer */ 21013afcdd7SPhilip Chen for (i = 0; i < len; i++) { 21113afcdd7SPhilip Chen ret = regmap_write(map, PAGE0_SWAUX_WDATA, buf[i]); 21213afcdd7SPhilip Chen if (ret) { 21313afcdd7SPhilip Chen DRM_DEV_ERROR(dev, 21413afcdd7SPhilip Chen "failed to write WDATA: %d\n", 21513afcdd7SPhilip Chen ret); 21613afcdd7SPhilip Chen return ret; 21713afcdd7SPhilip Chen } 21813afcdd7SPhilip Chen } 21913afcdd7SPhilip Chen } 22013afcdd7SPhilip Chen 22113afcdd7SPhilip Chen regmap_write(map, PAGE0_SWAUX_CTRL, SWAUX_SEND); 22213afcdd7SPhilip Chen 22313afcdd7SPhilip Chen /* Zero delay loop because i2c transactions are slow already */ 22413afcdd7SPhilip Chen regmap_read_poll_timeout(map, PAGE0_SWAUX_CTRL, data, 22513afcdd7SPhilip Chen !(data & SWAUX_SEND), 0, 50 * 1000); 22613afcdd7SPhilip Chen 22713afcdd7SPhilip Chen regmap_read(map, PAGE0_SWAUX_STATUS, &data); 22813afcdd7SPhilip Chen if (ret) { 22913afcdd7SPhilip Chen DRM_DEV_ERROR(dev, "failed to read PAGE0_SWAUX_STATUS: %d\n", 23013afcdd7SPhilip Chen ret); 23113afcdd7SPhilip Chen return ret; 23213afcdd7SPhilip Chen } 23313afcdd7SPhilip Chen 23413afcdd7SPhilip Chen switch (data & SWAUX_STATUS_MASK) { 23513afcdd7SPhilip Chen /* Ignore the DEFER cases as they are already handled in hardware */ 23613afcdd7SPhilip Chen case SWAUX_STATUS_NACK: 23713afcdd7SPhilip Chen case SWAUX_STATUS_I2C_NACK: 23813afcdd7SPhilip Chen /* 23913afcdd7SPhilip Chen * The programming guide is not clear about whether a I2C NACK 24013afcdd7SPhilip Chen * would trigger SWAUX_STATUS_NACK or SWAUX_STATUS_I2C_NACK. So 24113afcdd7SPhilip Chen * we handle both cases together. 24213afcdd7SPhilip Chen */ 24313afcdd7SPhilip Chen if (is_native_aux) 24413afcdd7SPhilip Chen msg->reply |= DP_AUX_NATIVE_REPLY_NACK; 24513afcdd7SPhilip Chen else 24613afcdd7SPhilip Chen msg->reply |= DP_AUX_I2C_REPLY_NACK; 24713afcdd7SPhilip Chen 24813afcdd7SPhilip Chen fallthrough; 24913afcdd7SPhilip Chen case SWAUX_STATUS_ACKM: 25013afcdd7SPhilip Chen len = data & SWAUX_M_MASK; 25113afcdd7SPhilip Chen break; 25213afcdd7SPhilip Chen case SWAUX_STATUS_INVALID: 25313afcdd7SPhilip Chen return -EOPNOTSUPP; 25413afcdd7SPhilip Chen case SWAUX_STATUS_TIMEOUT: 25513afcdd7SPhilip Chen return -ETIMEDOUT; 25613afcdd7SPhilip Chen } 25713afcdd7SPhilip Chen 25813afcdd7SPhilip Chen if (len && (request == DP_AUX_NATIVE_READ || 25913afcdd7SPhilip Chen request == DP_AUX_I2C_READ)) { 26013afcdd7SPhilip Chen /* Read from the internal FIFO buffer */ 26113afcdd7SPhilip Chen for (i = 0; i < len; i++) { 26213afcdd7SPhilip Chen ret = regmap_read(map, PAGE0_SWAUX_RDATA, &data); 26313afcdd7SPhilip Chen if (ret) { 26413afcdd7SPhilip Chen DRM_DEV_ERROR(dev, 26513afcdd7SPhilip Chen "failed to read RDATA: %d\n", 26613afcdd7SPhilip Chen ret); 26713afcdd7SPhilip Chen return ret; 26813afcdd7SPhilip Chen } 26913afcdd7SPhilip Chen 27013afcdd7SPhilip Chen buf[i] = data; 27113afcdd7SPhilip Chen } 27213afcdd7SPhilip Chen } 27313afcdd7SPhilip Chen 27413afcdd7SPhilip Chen return len; 27513afcdd7SPhilip Chen } 27613afcdd7SPhilip Chen 277bc1aee7fSJitao Shi static int ps8640_bridge_vdo_control(struct ps8640 *ps_bridge, 278bc1aee7fSJitao Shi const enum ps8640_vdo_control ctrl) 279bc1aee7fSJitao Shi { 280692d8db0SPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE3_DSI_CNTL1]; 281bc1aee7fSJitao Shi u8 vdo_ctrl_buf[] = { VDO_CTL_ADD, ctrl }; 282bc1aee7fSJitao Shi int ret; 283bc1aee7fSJitao Shi 284692d8db0SPhilip Chen ret = regmap_bulk_write(map, PAGE3_SET_ADD, 285692d8db0SPhilip Chen vdo_ctrl_buf, sizeof(vdo_ctrl_buf)); 286692d8db0SPhilip Chen 28794d4c132SEnric Balletbo i Serra if (ret < 0) { 28894d4c132SEnric Balletbo i Serra DRM_ERROR("failed to %sable VDO: %d\n", 28994d4c132SEnric Balletbo i Serra ctrl == ENABLE ? "en" : "dis", ret); 290bc1aee7fSJitao Shi return ret; 29194d4c132SEnric Balletbo i Serra } 292bc1aee7fSJitao Shi 293bc1aee7fSJitao Shi return 0; 294bc1aee7fSJitao Shi } 295bc1aee7fSJitao Shi 29646f20630SEnric Balletbo i Serra static void ps8640_bridge_poweron(struct ps8640 *ps_bridge) 297bc1aee7fSJitao Shi { 298692d8db0SPhilip Chen struct regmap *map = ps_bridge->regmap[PAGE2_TOP_CNTL]; 299bc1aee7fSJitao Shi int ret, status; 300bc1aee7fSJitao Shi 30146f20630SEnric Balletbo i Serra if (ps_bridge->powered) 30246f20630SEnric Balletbo i Serra return; 30346f20630SEnric Balletbo i Serra 304bc1aee7fSJitao Shi ret = regulator_bulk_enable(ARRAY_SIZE(ps_bridge->supplies), 305bc1aee7fSJitao Shi ps_bridge->supplies); 306bc1aee7fSJitao Shi if (ret < 0) { 307bc1aee7fSJitao Shi DRM_ERROR("cannot enable regulators %d\n", ret); 308bc1aee7fSJitao Shi return; 309bc1aee7fSJitao Shi } 310bc1aee7fSJitao Shi 311bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_powerdown, 0); 312bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_reset, 1); 313bc1aee7fSJitao Shi usleep_range(2000, 2500); 314bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_reset, 0); 315bc1aee7fSJitao Shi 316bc1aee7fSJitao Shi /* 317bc1aee7fSJitao Shi * Wait for the ps8640 embedded MCU to be ready 318bc1aee7fSJitao Shi * First wait 200ms and then check the MCU ready flag every 20ms 319bc1aee7fSJitao Shi */ 320bc1aee7fSJitao Shi msleep(200); 321bc1aee7fSJitao Shi 322692d8db0SPhilip Chen ret = regmap_read_poll_timeout(map, PAGE2_GPIO_H, status, 323692d8db0SPhilip Chen status & PS_GPIO9, 20 * 1000, 200 * 1000); 324bc1aee7fSJitao Shi 325692d8db0SPhilip Chen if (ret < 0) { 326692d8db0SPhilip Chen DRM_ERROR("failed read PAGE2_GPIO_H: %d\n", ret); 327bc1aee7fSJitao Shi goto err_regulators_disable; 328bc1aee7fSJitao Shi } 329bc1aee7fSJitao Shi 330bc1aee7fSJitao Shi msleep(50); 331bc1aee7fSJitao Shi 332bc1aee7fSJitao Shi /* 333bc1aee7fSJitao Shi * The Manufacturer Command Set (MCS) is a device dependent interface 334bc1aee7fSJitao Shi * intended for factory programming of the display module default 335bc1aee7fSJitao Shi * parameters. Once the display module is configured, the MCS shall be 336bc1aee7fSJitao Shi * disabled by the manufacturer. Once disabled, all MCS commands are 337bc1aee7fSJitao Shi * ignored by the display interface. 338bc1aee7fSJitao Shi */ 339bc1aee7fSJitao Shi 340692d8db0SPhilip Chen ret = regmap_update_bits(map, PAGE2_MCS_EN, MCS_EN, 0); 341bc1aee7fSJitao Shi if (ret < 0) { 342bc1aee7fSJitao Shi DRM_ERROR("failed write PAGE2_MCS_EN: %d\n", ret); 343bc1aee7fSJitao Shi goto err_regulators_disable; 344bc1aee7fSJitao Shi } 345bc1aee7fSJitao Shi 346bc1aee7fSJitao Shi /* Switch access edp panel's edid through i2c */ 347692d8db0SPhilip Chen ret = regmap_write(map, PAGE2_I2C_BYPASS, I2C_BYPASS_EN); 348bc1aee7fSJitao Shi if (ret < 0) { 349bc1aee7fSJitao Shi DRM_ERROR("failed write PAGE2_I2C_BYPASS: %d\n", ret); 350bc1aee7fSJitao Shi goto err_regulators_disable; 351bc1aee7fSJitao Shi } 352bc1aee7fSJitao Shi 35346f20630SEnric Balletbo i Serra ps_bridge->powered = true; 35446f20630SEnric Balletbo i Serra 355bc1aee7fSJitao Shi return; 356bc1aee7fSJitao Shi 357bc1aee7fSJitao Shi err_regulators_disable: 358bc1aee7fSJitao Shi regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies), 359bc1aee7fSJitao Shi ps_bridge->supplies); 360bc1aee7fSJitao Shi } 361bc1aee7fSJitao Shi 36246f20630SEnric Balletbo i Serra static void ps8640_bridge_poweroff(struct ps8640 *ps_bridge) 363bc1aee7fSJitao Shi { 364bc1aee7fSJitao Shi int ret; 365bc1aee7fSJitao Shi 36646f20630SEnric Balletbo i Serra if (!ps_bridge->powered) 36746f20630SEnric Balletbo i Serra return; 368bc1aee7fSJitao Shi 369bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_reset, 1); 370bc1aee7fSJitao Shi gpiod_set_value(ps_bridge->gpio_powerdown, 1); 371bc1aee7fSJitao Shi ret = regulator_bulk_disable(ARRAY_SIZE(ps_bridge->supplies), 372bc1aee7fSJitao Shi ps_bridge->supplies); 373bc1aee7fSJitao Shi if (ret < 0) 374bc1aee7fSJitao Shi DRM_ERROR("cannot disable regulators %d\n", ret); 37546f20630SEnric Balletbo i Serra 37646f20630SEnric Balletbo i Serra ps_bridge->powered = false; 37746f20630SEnric Balletbo i Serra } 37846f20630SEnric Balletbo i Serra 37946f20630SEnric Balletbo i Serra static void ps8640_pre_enable(struct drm_bridge *bridge) 38046f20630SEnric Balletbo i Serra { 38146f20630SEnric Balletbo i Serra struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 38246f20630SEnric Balletbo i Serra int ret; 38346f20630SEnric Balletbo i Serra 38446f20630SEnric Balletbo i Serra ps8640_bridge_poweron(ps_bridge); 38546f20630SEnric Balletbo i Serra 38646f20630SEnric Balletbo i Serra ret = ps8640_bridge_vdo_control(ps_bridge, ENABLE); 38746f20630SEnric Balletbo i Serra if (ret < 0) 38846f20630SEnric Balletbo i Serra ps8640_bridge_poweroff(ps_bridge); 38946f20630SEnric Balletbo i Serra } 39046f20630SEnric Balletbo i Serra 39146f20630SEnric Balletbo i Serra static void ps8640_post_disable(struct drm_bridge *bridge) 39246f20630SEnric Balletbo i Serra { 39346f20630SEnric Balletbo i Serra struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 39446f20630SEnric Balletbo i Serra 39546f20630SEnric Balletbo i Serra ps8640_bridge_vdo_control(ps_bridge, DISABLE); 39646f20630SEnric Balletbo i Serra ps8640_bridge_poweroff(ps_bridge); 397bc1aee7fSJitao Shi } 398bc1aee7fSJitao Shi 399a25b988fSLaurent Pinchart static int ps8640_bridge_attach(struct drm_bridge *bridge, 400a25b988fSLaurent Pinchart enum drm_bridge_attach_flags flags) 401bc1aee7fSJitao Shi { 402bc1aee7fSJitao Shi struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 403bc1aee7fSJitao Shi struct device *dev = &ps_bridge->page[0]->dev; 404bc1aee7fSJitao Shi int ret; 405812a65baSEnric Balletbo i Serra 406812a65baSEnric Balletbo i Serra if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) 407812a65baSEnric Balletbo i Serra return -EINVAL; 408812a65baSEnric Balletbo i Serra 40913afcdd7SPhilip Chen ret = drm_dp_aux_register(&ps_bridge->aux); 41013afcdd7SPhilip Chen if (ret) { 41113afcdd7SPhilip Chen dev_err(dev, "failed to register DP AUX channel: %d\n", ret); 412fe93ae80SMaxime Ripard return ret; 41313afcdd7SPhilip Chen } 414bc1aee7fSJitao Shi 415bc1aee7fSJitao Shi /* Attach the panel-bridge to the dsi bridge */ 416bc1aee7fSJitao Shi return drm_bridge_attach(bridge->encoder, ps_bridge->panel_bridge, 417a25b988fSLaurent Pinchart &ps_bridge->bridge, flags); 418bc1aee7fSJitao Shi } 419bc1aee7fSJitao Shi 42013afcdd7SPhilip Chen static void ps8640_bridge_detach(struct drm_bridge *bridge) 42113afcdd7SPhilip Chen { 42213afcdd7SPhilip Chen drm_dp_aux_unregister(&bridge_to_ps8640(bridge)->aux); 42313afcdd7SPhilip Chen } 42413afcdd7SPhilip Chen 425d82c12abSEnric Balletbo i Serra static struct edid *ps8640_bridge_get_edid(struct drm_bridge *bridge, 426d82c12abSEnric Balletbo i Serra struct drm_connector *connector) 427d82c12abSEnric Balletbo i Serra { 428d82c12abSEnric Balletbo i Serra struct ps8640 *ps_bridge = bridge_to_ps8640(bridge); 42946f20630SEnric Balletbo i Serra bool poweroff = !ps_bridge->powered; 43046f20630SEnric Balletbo i Serra struct edid *edid; 431d82c12abSEnric Balletbo i Serra 43246f20630SEnric Balletbo i Serra /* 43346f20630SEnric Balletbo i Serra * When we end calling get_edid() triggered by an ioctl, i.e 43446f20630SEnric Balletbo i Serra * 43546f20630SEnric Balletbo i Serra * drm_mode_getconnector (ioctl) 43646f20630SEnric Balletbo i Serra * -> drm_helper_probe_single_connector_modes 43746f20630SEnric Balletbo i Serra * -> drm_bridge_connector_get_modes 43846f20630SEnric Balletbo i Serra * -> ps8640_bridge_get_edid 43946f20630SEnric Balletbo i Serra * 44046f20630SEnric Balletbo i Serra * We need to make sure that what we need is enabled before reading 44146f20630SEnric Balletbo i Serra * EDID, for this chip, we need to do a full poweron, otherwise it will 44246f20630SEnric Balletbo i Serra * fail. 44346f20630SEnric Balletbo i Serra */ 44446f20630SEnric Balletbo i Serra drm_bridge_chain_pre_enable(bridge); 44546f20630SEnric Balletbo i Serra 44646f20630SEnric Balletbo i Serra edid = drm_get_edid(connector, 447d82c12abSEnric Balletbo i Serra ps_bridge->page[PAGE0_DP_CNTL]->adapter); 44846f20630SEnric Balletbo i Serra 44946f20630SEnric Balletbo i Serra /* 45046f20630SEnric Balletbo i Serra * If we call the get_edid() function without having enabled the chip 45146f20630SEnric Balletbo i Serra * before, return the chip to its original power state. 45246f20630SEnric Balletbo i Serra */ 45346f20630SEnric Balletbo i Serra if (poweroff) 45446f20630SEnric Balletbo i Serra drm_bridge_chain_post_disable(bridge); 45546f20630SEnric Balletbo i Serra 45646f20630SEnric Balletbo i Serra return edid; 457d82c12abSEnric Balletbo i Serra } 458d82c12abSEnric Balletbo i Serra 459bc1aee7fSJitao Shi static const struct drm_bridge_funcs ps8640_bridge_funcs = { 460bc1aee7fSJitao Shi .attach = ps8640_bridge_attach, 46113afcdd7SPhilip Chen .detach = ps8640_bridge_detach, 462d82c12abSEnric Balletbo i Serra .get_edid = ps8640_bridge_get_edid, 463bc1aee7fSJitao Shi .post_disable = ps8640_post_disable, 464bc1aee7fSJitao Shi .pre_enable = ps8640_pre_enable, 465bc1aee7fSJitao Shi }; 466bc1aee7fSJitao Shi 467*7abbc26fSMaxime Ripard static int ps8640_bridge_host_attach(struct device *dev, struct ps8640 *ps_bridge) 468*7abbc26fSMaxime Ripard { 469*7abbc26fSMaxime Ripard struct device_node *in_ep, *dsi_node; 470*7abbc26fSMaxime Ripard struct mipi_dsi_device *dsi; 471*7abbc26fSMaxime Ripard struct mipi_dsi_host *host; 472*7abbc26fSMaxime Ripard int ret; 473*7abbc26fSMaxime Ripard const struct mipi_dsi_device_info info = { .type = "ps8640", 474*7abbc26fSMaxime Ripard .channel = 0, 475*7abbc26fSMaxime Ripard .node = NULL, 476*7abbc26fSMaxime Ripard }; 477*7abbc26fSMaxime Ripard 478*7abbc26fSMaxime Ripard /* port@0 is ps8640 dsi input port */ 479*7abbc26fSMaxime Ripard in_ep = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1); 480*7abbc26fSMaxime Ripard if (!in_ep) 481*7abbc26fSMaxime Ripard return -ENODEV; 482*7abbc26fSMaxime Ripard 483*7abbc26fSMaxime Ripard dsi_node = of_graph_get_remote_port_parent(in_ep); 484*7abbc26fSMaxime Ripard of_node_put(in_ep); 485*7abbc26fSMaxime Ripard if (!dsi_node) 486*7abbc26fSMaxime Ripard return -ENODEV; 487*7abbc26fSMaxime Ripard 488*7abbc26fSMaxime Ripard host = of_find_mipi_dsi_host_by_node(dsi_node); 489*7abbc26fSMaxime Ripard of_node_put(dsi_node); 490*7abbc26fSMaxime Ripard if (!host) 491*7abbc26fSMaxime Ripard return -EPROBE_DEFER; 492*7abbc26fSMaxime Ripard 493*7abbc26fSMaxime Ripard dsi = devm_mipi_dsi_device_register_full(dev, host, &info); 494*7abbc26fSMaxime Ripard if (IS_ERR(dsi)) { 495*7abbc26fSMaxime Ripard dev_err(dev, "failed to create dsi device\n"); 496*7abbc26fSMaxime Ripard return PTR_ERR(dsi); 497*7abbc26fSMaxime Ripard } 498*7abbc26fSMaxime Ripard 499*7abbc26fSMaxime Ripard ps_bridge->dsi = dsi; 500*7abbc26fSMaxime Ripard 501*7abbc26fSMaxime Ripard dsi->host = host; 502*7abbc26fSMaxime Ripard dsi->mode_flags = MIPI_DSI_MODE_VIDEO | 503*7abbc26fSMaxime Ripard MIPI_DSI_MODE_VIDEO_SYNC_PULSE; 504*7abbc26fSMaxime Ripard dsi->format = MIPI_DSI_FMT_RGB888; 505*7abbc26fSMaxime Ripard dsi->lanes = NUM_MIPI_LANES; 506*7abbc26fSMaxime Ripard 507*7abbc26fSMaxime Ripard ret = devm_mipi_dsi_attach(dev, dsi); 508*7abbc26fSMaxime Ripard if (ret) 509*7abbc26fSMaxime Ripard return ret; 510*7abbc26fSMaxime Ripard 511*7abbc26fSMaxime Ripard return 0; 512*7abbc26fSMaxime Ripard } 513*7abbc26fSMaxime Ripard 514bc1aee7fSJitao Shi static int ps8640_probe(struct i2c_client *client) 515bc1aee7fSJitao Shi { 516bc1aee7fSJitao Shi struct device *dev = &client->dev; 517bc1aee7fSJitao Shi struct device_node *np = dev->of_node; 518bc1aee7fSJitao Shi struct ps8640 *ps_bridge; 519bc1aee7fSJitao Shi struct drm_panel *panel; 520bc1aee7fSJitao Shi int ret; 521bc1aee7fSJitao Shi u32 i; 522bc1aee7fSJitao Shi 523bc1aee7fSJitao Shi ps_bridge = devm_kzalloc(dev, sizeof(*ps_bridge), GFP_KERNEL); 524bc1aee7fSJitao Shi if (!ps_bridge) 525bc1aee7fSJitao Shi return -ENOMEM; 526bc1aee7fSJitao Shi 527bc1aee7fSJitao Shi /* port@1 is ps8640 output port */ 528bc1aee7fSJitao Shi ret = drm_of_find_panel_or_bridge(np, 1, 0, &panel, NULL); 529bc1aee7fSJitao Shi if (ret < 0) 530bc1aee7fSJitao Shi return ret; 531bc1aee7fSJitao Shi if (!panel) 532bc1aee7fSJitao Shi return -ENODEV; 533bc1aee7fSJitao Shi 534bc1aee7fSJitao Shi ps_bridge->panel_bridge = devm_drm_panel_bridge_add(dev, panel); 535bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->panel_bridge)) 536bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->panel_bridge); 537bc1aee7fSJitao Shi 538bc1aee7fSJitao Shi ps_bridge->supplies[0].supply = "vdd33"; 539bc1aee7fSJitao Shi ps_bridge->supplies[1].supply = "vdd12"; 540bc1aee7fSJitao Shi ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ps_bridge->supplies), 541bc1aee7fSJitao Shi ps_bridge->supplies); 542bc1aee7fSJitao Shi if (ret) 543bc1aee7fSJitao Shi return ret; 544bc1aee7fSJitao Shi 545bc1aee7fSJitao Shi ps_bridge->gpio_powerdown = devm_gpiod_get(&client->dev, "powerdown", 546bc1aee7fSJitao Shi GPIOD_OUT_HIGH); 547bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->gpio_powerdown)) 548bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->gpio_powerdown); 549bc1aee7fSJitao Shi 550bc1aee7fSJitao Shi /* 551bc1aee7fSJitao Shi * Assert the reset to avoid the bridge being initialized prematurely 552bc1aee7fSJitao Shi */ 553bc1aee7fSJitao Shi ps_bridge->gpio_reset = devm_gpiod_get(&client->dev, "reset", 554bc1aee7fSJitao Shi GPIOD_OUT_HIGH); 555bc1aee7fSJitao Shi if (IS_ERR(ps_bridge->gpio_reset)) 556bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->gpio_reset); 557bc1aee7fSJitao Shi 558bc1aee7fSJitao Shi ps_bridge->bridge.funcs = &ps8640_bridge_funcs; 559bc1aee7fSJitao Shi ps_bridge->bridge.of_node = dev->of_node; 560d82c12abSEnric Balletbo i Serra ps_bridge->bridge.ops = DRM_BRIDGE_OP_EDID; 561d82c12abSEnric Balletbo i Serra ps_bridge->bridge.type = DRM_MODE_CONNECTOR_eDP; 562bc1aee7fSJitao Shi 563bc1aee7fSJitao Shi ps_bridge->page[PAGE0_DP_CNTL] = client; 564bc1aee7fSJitao Shi 565692d8db0SPhilip Chen ps_bridge->regmap[PAGE0_DP_CNTL] = devm_regmap_init_i2c(client, ps8640_regmap_config); 566692d8db0SPhilip Chen if (IS_ERR(ps_bridge->regmap[PAGE0_DP_CNTL])) 567692d8db0SPhilip Chen return PTR_ERR(ps_bridge->regmap[PAGE0_DP_CNTL]); 568692d8db0SPhilip Chen 569bc1aee7fSJitao Shi for (i = 1; i < ARRAY_SIZE(ps_bridge->page); i++) { 570bc1aee7fSJitao Shi ps_bridge->page[i] = devm_i2c_new_dummy_device(&client->dev, 571bc1aee7fSJitao Shi client->adapter, 572bc1aee7fSJitao Shi client->addr + i); 573692d8db0SPhilip Chen if (IS_ERR(ps_bridge->page[i])) 574bc1aee7fSJitao Shi return PTR_ERR(ps_bridge->page[i]); 575692d8db0SPhilip Chen 576692d8db0SPhilip Chen ps_bridge->regmap[i] = devm_regmap_init_i2c(ps_bridge->page[i], 577692d8db0SPhilip Chen ps8640_regmap_config + i); 578692d8db0SPhilip Chen if (IS_ERR(ps_bridge->regmap[i])) 579692d8db0SPhilip Chen return PTR_ERR(ps_bridge->regmap[i]); 580bc1aee7fSJitao Shi } 581bc1aee7fSJitao Shi 582bc1aee7fSJitao Shi i2c_set_clientdata(client, ps_bridge); 583bc1aee7fSJitao Shi 58413afcdd7SPhilip Chen ps_bridge->aux.name = "parade-ps8640-aux"; 58513afcdd7SPhilip Chen ps_bridge->aux.dev = dev; 58613afcdd7SPhilip Chen ps_bridge->aux.transfer = ps8640_aux_transfer; 58713afcdd7SPhilip Chen drm_dp_aux_init(&ps_bridge->aux); 58813afcdd7SPhilip Chen 589bc1aee7fSJitao Shi drm_bridge_add(&ps_bridge->bridge); 590bc1aee7fSJitao Shi 591*7abbc26fSMaxime Ripard ret = ps8640_bridge_host_attach(dev, ps_bridge); 592*7abbc26fSMaxime Ripard if (ret) 593*7abbc26fSMaxime Ripard goto err_bridge_remove; 594*7abbc26fSMaxime Ripard 595bc1aee7fSJitao Shi return 0; 596*7abbc26fSMaxime Ripard 597*7abbc26fSMaxime Ripard err_bridge_remove: 598*7abbc26fSMaxime Ripard drm_bridge_remove(&ps_bridge->bridge); 599*7abbc26fSMaxime Ripard return ret; 600bc1aee7fSJitao Shi } 601bc1aee7fSJitao Shi 602bc1aee7fSJitao Shi static int ps8640_remove(struct i2c_client *client) 603bc1aee7fSJitao Shi { 604bc1aee7fSJitao Shi struct ps8640 *ps_bridge = i2c_get_clientdata(client); 605bc1aee7fSJitao Shi 606bc1aee7fSJitao Shi drm_bridge_remove(&ps_bridge->bridge); 607bc1aee7fSJitao Shi 608bc1aee7fSJitao Shi return 0; 609bc1aee7fSJitao Shi } 610bc1aee7fSJitao Shi 611bc1aee7fSJitao Shi static const struct of_device_id ps8640_match[] = { 612bc1aee7fSJitao Shi { .compatible = "parade,ps8640" }, 613bc1aee7fSJitao Shi { } 614bc1aee7fSJitao Shi }; 615bc1aee7fSJitao Shi MODULE_DEVICE_TABLE(of, ps8640_match); 616bc1aee7fSJitao Shi 617bc1aee7fSJitao Shi static struct i2c_driver ps8640_driver = { 618bc1aee7fSJitao Shi .probe_new = ps8640_probe, 619bc1aee7fSJitao Shi .remove = ps8640_remove, 620bc1aee7fSJitao Shi .driver = { 621bc1aee7fSJitao Shi .name = "ps8640", 622bc1aee7fSJitao Shi .of_match_table = ps8640_match, 623bc1aee7fSJitao Shi }, 624bc1aee7fSJitao Shi }; 625bc1aee7fSJitao Shi module_i2c_driver(ps8640_driver); 626bc1aee7fSJitao Shi 627bc1aee7fSJitao Shi MODULE_AUTHOR("Jitao Shi <jitao.shi@mediatek.com>"); 628bc1aee7fSJitao Shi MODULE_AUTHOR("CK Hu <ck.hu@mediatek.com>"); 629bc1aee7fSJitao Shi MODULE_AUTHOR("Enric Balletbo i Serra <enric.balletbo@collabora.com>"); 630bc1aee7fSJitao Shi MODULE_DESCRIPTION("PARADE ps8640 DSI-eDP converter driver"); 631bc1aee7fSJitao Shi MODULE_LICENSE("GPL v2"); 632