Path: blob/master/drivers/gpu/drm/bridge/cros-ec-anx7688.c
26494 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* CrOS EC ANX7688 HDMI->DP bridge driver3*4* Copyright 2020 Google LLC5*/67#include <drm/drm_bridge.h>8#include <drm/drm_print.h>9#include <linux/i2c.h>10#include <linux/module.h>11#include <linux/regmap.h>12#include <linux/types.h>1314/* Register addresses */15#define ANX7688_VENDOR_ID_REG 0x0016#define ANX7688_DEVICE_ID_REG 0x021718#define ANX7688_FW_VERSION_REG 0x801920#define ANX7688_DP_BANDWIDTH_REG 0x8521#define ANX7688_DP_LANE_COUNT_REG 0x862223#define ANX7688_VENDOR_ID 0x1f2924#define ANX7688_DEVICE_ID 0x76882526/* First supported firmware version (0.85) */27#define ANX7688_MINIMUM_FW_VERSION 0x00852829static const struct regmap_config cros_ec_anx7688_regmap_config = {30.reg_bits = 8,31.val_bits = 8,32};3334struct cros_ec_anx7688 {35struct i2c_client *client;36struct regmap *regmap;37struct drm_bridge bridge;38bool filter;39};4041static inline struct cros_ec_anx7688 *42bridge_to_cros_ec_anx7688(struct drm_bridge *bridge)43{44return container_of(bridge, struct cros_ec_anx7688, bridge);45}4647static bool cros_ec_anx7688_bridge_mode_fixup(struct drm_bridge *bridge,48const struct drm_display_mode *mode,49struct drm_display_mode *adjusted_mode)50{51struct cros_ec_anx7688 *anx = bridge_to_cros_ec_anx7688(bridge);52int totalbw, requiredbw;53u8 dpbw, lanecount;54u8 regs[2];55int ret;5657if (!anx->filter)58return true;5960/* Read both regs 0x85 (bandwidth) and 0x86 (lane count). */61ret = regmap_bulk_read(anx->regmap, ANX7688_DP_BANDWIDTH_REG, regs, 2);62if (ret < 0) {63DRM_ERROR("Failed to read bandwidth/lane count\n");64return false;65}66dpbw = regs[0];67lanecount = regs[1];6869/* Maximum 0x19 bandwidth (6.75 Gbps Turbo mode), 2 lanes */70if (dpbw > 0x19 || lanecount > 2) {71DRM_ERROR("Invalid bandwidth/lane count (%02x/%d)\n", dpbw,72lanecount);73return false;74}7576/* Compute available bandwidth (kHz) */77totalbw = dpbw * lanecount * 270000 * 8 / 10;7879/* Required bandwidth (8 bpc, kHz) */80requiredbw = mode->clock * 8 * 3;8182DRM_DEBUG_KMS("DP bandwidth: %d kHz (%02x/%d); mode requires %d Khz\n",83totalbw, dpbw, lanecount, requiredbw);8485if (totalbw == 0) {86DRM_ERROR("Bandwidth/lane count are 0, not rejecting modes\n");87return true;88}8990return totalbw >= requiredbw;91}9293static const struct drm_bridge_funcs cros_ec_anx7688_bridge_funcs = {94.mode_fixup = cros_ec_anx7688_bridge_mode_fixup,95};9697static int cros_ec_anx7688_bridge_probe(struct i2c_client *client)98{99struct device *dev = &client->dev;100struct cros_ec_anx7688 *anx7688;101u16 vendor, device, fw_version;102u8 buffer[4];103int ret;104105anx7688 = devm_drm_bridge_alloc(dev, struct cros_ec_anx7688, bridge,106&cros_ec_anx7688_bridge_funcs);107if (IS_ERR(anx7688))108return PTR_ERR(anx7688);109110anx7688->client = client;111i2c_set_clientdata(client, anx7688);112113anx7688->regmap = devm_regmap_init_i2c(client, &cros_ec_anx7688_regmap_config);114if (IS_ERR(anx7688->regmap)) {115ret = PTR_ERR(anx7688->regmap);116dev_err(dev, "regmap i2c init failed: %d\n", ret);117return ret;118}119120/* Read both vendor and device id (4 bytes). */121ret = regmap_bulk_read(anx7688->regmap, ANX7688_VENDOR_ID_REG,122buffer, 4);123if (ret) {124dev_err(dev, "Failed to read chip vendor/device id\n");125return ret;126}127128vendor = (u16)buffer[1] << 8 | buffer[0];129device = (u16)buffer[3] << 8 | buffer[2];130if (vendor != ANX7688_VENDOR_ID || device != ANX7688_DEVICE_ID) {131dev_err(dev, "Invalid vendor/device id %04x/%04x\n",132vendor, device);133return -ENODEV;134}135136ret = regmap_bulk_read(anx7688->regmap, ANX7688_FW_VERSION_REG,137buffer, 2);138if (ret) {139dev_err(dev, "Failed to read firmware version\n");140return ret;141}142143fw_version = (u16)buffer[0] << 8 | buffer[1];144dev_info(dev, "ANX7688 firmware version 0x%04x\n", fw_version);145146anx7688->bridge.of_node = dev->of_node;147148/* FW version >= 0.85 supports bandwidth/lane count registers */149if (fw_version >= ANX7688_MINIMUM_FW_VERSION)150anx7688->filter = true;151else152/* Warn, but not fail, for backwards compatibility */153DRM_WARN("Old ANX7688 FW version (0x%04x), not filtering\n",154fw_version);155156drm_bridge_add(&anx7688->bridge);157158return 0;159}160161static void cros_ec_anx7688_bridge_remove(struct i2c_client *client)162{163struct cros_ec_anx7688 *anx7688 = i2c_get_clientdata(client);164165drm_bridge_remove(&anx7688->bridge);166}167168static const struct of_device_id cros_ec_anx7688_bridge_match_table[] = {169{ .compatible = "google,cros-ec-anx7688" },170{ }171};172MODULE_DEVICE_TABLE(of, cros_ec_anx7688_bridge_match_table);173174static struct i2c_driver cros_ec_anx7688_bridge_driver = {175.probe = cros_ec_anx7688_bridge_probe,176.remove = cros_ec_anx7688_bridge_remove,177.driver = {178.name = "cros-ec-anx7688-bridge",179.of_match_table = cros_ec_anx7688_bridge_match_table,180},181};182183module_i2c_driver(cros_ec_anx7688_bridge_driver);184185MODULE_DESCRIPTION("ChromeOS EC ANX7688 HDMI->DP bridge driver");186MODULE_AUTHOR("Nicolas Boichat <[email protected]>");187MODULE_AUTHOR("Enric Balletbo i Serra <[email protected]>");188MODULE_LICENSE("GPL");189190191