Path: blob/master/drivers/gpu/drm/bridge/ti-sn65dsi83.c
51289 views
// SPDX-License-Identifier: GPL-2.01/*2* TI SN65DSI83,84,85 driver3*4* Currently supported:5* - SN65DSI836* = 1x Single-link DSI ~ 1x Single-link LVDS7* - Supported8* - Single-link LVDS mode tested9* - SN65DSI8410* = 1x Single-link DSI ~ 2x Single-link or 1x Dual-link LVDS11* - Supported12* - Dual-link LVDS mode tested13* - 2x Single-link LVDS mode unsupported14* (should be easy to add by someone who has the HW)15* - SN65DSI8516* = 2x Single-link or 1x Dual-link DSI ~ 2x Single-link or 1x Dual-link LVDS17* - Unsupported18* (should be easy to add by someone who has the HW)19*20* Copyright (C) 2021 Marek Vasut <[email protected]>21*22* Based on previous work of:23* Valentin Raevsky <[email protected]>24* Philippe Schenker <[email protected]>25*/2627#include <linux/bits.h>28#include <linux/clk.h>29#include <linux/gpio/consumer.h>30#include <linux/i2c.h>31#include <linux/media-bus-format.h>32#include <linux/module.h>33#include <linux/of.h>34#include <linux/of_graph.h>35#include <linux/regmap.h>36#include <linux/regulator/consumer.h>37#include <linux/timer.h>38#include <linux/workqueue.h>3940#include <drm/drm_atomic_helper.h>41#include <drm/drm_bridge.h>42#include <drm/drm_bridge_helper.h>43#include <drm/drm_mipi_dsi.h>44#include <drm/drm_of.h>45#include <drm/drm_print.h>46#include <drm/drm_probe_helper.h>4748/* ID registers */49#define REG_ID(n) (0x00 + (n))50/* Reset and clock registers */51#define REG_RC_RESET 0x0952#define REG_RC_RESET_SOFT_RESET BIT(0)53#define REG_RC_LVDS_PLL 0x0a54#define REG_RC_LVDS_PLL_PLL_EN_STAT BIT(7)55#define REG_RC_LVDS_PLL_LVDS_CLK_RANGE(n) (((n) & 0x7) << 1)56#define REG_RC_LVDS_PLL_HS_CLK_SRC_DPHY BIT(0)57#define REG_RC_DSI_CLK 0x0b58#define REG_RC_DSI_CLK_DSI_CLK_DIVIDER(n) (((n) & 0x1f) << 3)59#define REG_RC_DSI_CLK_REFCLK_MULTIPLIER(n) ((n) & 0x3)60#define REG_RC_PLL_EN 0x0d61#define REG_RC_PLL_EN_PLL_EN BIT(0)62/* DSI registers */63#define REG_DSI_LANE 0x1064#define REG_DSI_LANE_LEFT_RIGHT_PIXELS BIT(7) /* DSI85-only */65#define REG_DSI_LANE_DSI_CHANNEL_MODE_DUAL 0 /* DSI85-only */66#define REG_DSI_LANE_DSI_CHANNEL_MODE_2SINGLE BIT(6) /* DSI85-only */67#define REG_DSI_LANE_DSI_CHANNEL_MODE_SINGLE BIT(5)68#define REG_DSI_LANE_CHA_DSI_LANES(n) (((n) & 0x3) << 3)69#define REG_DSI_LANE_CHB_DSI_LANES(n) (((n) & 0x3) << 1)70#define REG_DSI_LANE_SOT_ERR_TOL_DIS BIT(0)71#define REG_DSI_EQ 0x1172#define REG_DSI_EQ_CHA_DSI_DATA_EQ(n) (((n) & 0x3) << 6)73#define REG_DSI_EQ_CHA_DSI_CLK_EQ(n) (((n) & 0x3) << 2)74#define REG_DSI_CLK 0x1275#define REG_DSI_CLK_CHA_DSI_CLK_RANGE(n) ((n) & 0xff)76/* LVDS registers */77#define REG_LVDS_FMT 0x1878#define REG_LVDS_FMT_DE_NEG_POLARITY BIT(7)79#define REG_LVDS_FMT_HS_NEG_POLARITY BIT(6)80#define REG_LVDS_FMT_VS_NEG_POLARITY BIT(5)81#define REG_LVDS_FMT_LVDS_LINK_CFG BIT(4) /* 0:AB 1:A-only */82#define REG_LVDS_FMT_CHA_24BPP_MODE BIT(3)83#define REG_LVDS_FMT_CHB_24BPP_MODE BIT(2)84#define REG_LVDS_FMT_CHA_24BPP_FORMAT1 BIT(1)85#define REG_LVDS_FMT_CHB_24BPP_FORMAT1 BIT(0)86#define REG_LVDS_VCOM 0x1987#define REG_LVDS_VCOM_CHA_LVDS_VOCM BIT(6)88#define REG_LVDS_VCOM_CHB_LVDS_VOCM BIT(4)89#define REG_LVDS_VCOM_CHA_LVDS_VOD_SWING(n) (((n) & 0x3) << 2)90#define REG_LVDS_VCOM_CHB_LVDS_VOD_SWING(n) ((n) & 0x3)91#define REG_LVDS_LANE 0x1a92#define REG_LVDS_LANE_EVEN_ODD_SWAP BIT(6)93#define REG_LVDS_LANE_CHA_REVERSE_LVDS BIT(5)94#define REG_LVDS_LANE_CHB_REVERSE_LVDS BIT(4)95#define REG_LVDS_LANE_CHA_LVDS_TERM BIT(1)96#define REG_LVDS_LANE_CHB_LVDS_TERM BIT(0)97#define REG_LVDS_CM 0x1b98#define REG_LVDS_CM_CHA_LVDS_CM_ADJUST(n) (((n) & 0x3) << 4)99#define REG_LVDS_CM_CHB_LVDS_CM_ADJUST(n) ((n) & 0x3)100/* Video registers */101#define REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW 0x20102#define REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH 0x21103#define REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW 0x24104#define REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH 0x25105#define REG_VID_CHA_SYNC_DELAY_LOW 0x28106#define REG_VID_CHA_SYNC_DELAY_HIGH 0x29107#define REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW 0x2c108#define REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH 0x2d109#define REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW 0x30110#define REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH 0x31111#define REG_VID_CHA_HORIZONTAL_BACK_PORCH 0x34112#define REG_VID_CHA_VERTICAL_BACK_PORCH 0x36113#define REG_VID_CHA_HORIZONTAL_FRONT_PORCH 0x38114#define REG_VID_CHA_VERTICAL_FRONT_PORCH 0x3a115#define REG_VID_CHA_TEST_PATTERN 0x3c116/* IRQ registers */117#define REG_IRQ_GLOBAL 0xe0118#define REG_IRQ_GLOBAL_IRQ_EN BIT(0)119#define REG_IRQ_EN 0xe1120#define REG_IRQ_EN_CHA_SYNCH_ERR_EN BIT(7)121#define REG_IRQ_EN_CHA_CRC_ERR_EN BIT(6)122#define REG_IRQ_EN_CHA_UNC_ECC_ERR_EN BIT(5)123#define REG_IRQ_EN_CHA_COR_ECC_ERR_EN BIT(4)124#define REG_IRQ_EN_CHA_LLP_ERR_EN BIT(3)125#define REG_IRQ_EN_CHA_SOT_BIT_ERR_EN BIT(2)126#define REG_IRQ_EN_CHA_PLL_UNLOCK_EN BIT(0)127#define REG_IRQ_STAT 0xe5128#define REG_IRQ_STAT_CHA_SYNCH_ERR BIT(7)129#define REG_IRQ_STAT_CHA_CRC_ERR BIT(6)130#define REG_IRQ_STAT_CHA_UNC_ECC_ERR BIT(5)131#define REG_IRQ_STAT_CHA_COR_ECC_ERR BIT(4)132#define REG_IRQ_STAT_CHA_LLP_ERR BIT(3)133#define REG_IRQ_STAT_CHA_SOT_BIT_ERR BIT(2)134#define REG_IRQ_STAT_CHA_PLL_UNLOCK BIT(0)135136enum sn65dsi83_channel {137CHANNEL_A,138CHANNEL_B139};140141enum sn65dsi83_lvds_term {142OHM_100,143OHM_200144};145146enum sn65dsi83_model {147MODEL_SN65DSI83,148MODEL_SN65DSI84,149};150151struct sn65dsi83 {152struct drm_bridge bridge;153struct device *dev;154struct regmap *regmap;155struct mipi_dsi_device *dsi;156struct drm_bridge *panel_bridge;157struct gpio_desc *enable_gpio;158struct regulator *vcc;159bool lvds_dual_link;160bool lvds_dual_link_even_odd_swap;161int lvds_vod_swing_conf[2];162int lvds_term_conf[2];163int irq;164struct delayed_work monitor_work;165struct work_struct reset_work;166};167168static const struct regmap_range sn65dsi83_readable_ranges[] = {169regmap_reg_range(REG_ID(0), REG_ID(8)),170regmap_reg_range(REG_RC_LVDS_PLL, REG_RC_DSI_CLK),171regmap_reg_range(REG_RC_PLL_EN, REG_RC_PLL_EN),172regmap_reg_range(REG_DSI_LANE, REG_DSI_CLK),173regmap_reg_range(REG_LVDS_FMT, REG_LVDS_CM),174regmap_reg_range(REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,175REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH),176regmap_reg_range(REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,177REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH),178regmap_reg_range(REG_VID_CHA_SYNC_DELAY_LOW,179REG_VID_CHA_SYNC_DELAY_HIGH),180regmap_reg_range(REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,181REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH),182regmap_reg_range(REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,183REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH),184regmap_reg_range(REG_VID_CHA_HORIZONTAL_BACK_PORCH,185REG_VID_CHA_HORIZONTAL_BACK_PORCH),186regmap_reg_range(REG_VID_CHA_VERTICAL_BACK_PORCH,187REG_VID_CHA_VERTICAL_BACK_PORCH),188regmap_reg_range(REG_VID_CHA_HORIZONTAL_FRONT_PORCH,189REG_VID_CHA_HORIZONTAL_FRONT_PORCH),190regmap_reg_range(REG_VID_CHA_VERTICAL_FRONT_PORCH,191REG_VID_CHA_VERTICAL_FRONT_PORCH),192regmap_reg_range(REG_VID_CHA_TEST_PATTERN, REG_VID_CHA_TEST_PATTERN),193regmap_reg_range(REG_IRQ_GLOBAL, REG_IRQ_EN),194regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),195};196197static const struct regmap_access_table sn65dsi83_readable_table = {198.yes_ranges = sn65dsi83_readable_ranges,199.n_yes_ranges = ARRAY_SIZE(sn65dsi83_readable_ranges),200};201202static const struct regmap_range sn65dsi83_writeable_ranges[] = {203regmap_reg_range(REG_RC_RESET, REG_RC_DSI_CLK),204regmap_reg_range(REG_RC_PLL_EN, REG_RC_PLL_EN),205regmap_reg_range(REG_DSI_LANE, REG_DSI_CLK),206regmap_reg_range(REG_LVDS_FMT, REG_LVDS_CM),207regmap_reg_range(REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,208REG_VID_CHA_ACTIVE_LINE_LENGTH_HIGH),209regmap_reg_range(REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,210REG_VID_CHA_VERTICAL_DISPLAY_SIZE_HIGH),211regmap_reg_range(REG_VID_CHA_SYNC_DELAY_LOW,212REG_VID_CHA_SYNC_DELAY_HIGH),213regmap_reg_range(REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,214REG_VID_CHA_HSYNC_PULSE_WIDTH_HIGH),215regmap_reg_range(REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,216REG_VID_CHA_VSYNC_PULSE_WIDTH_HIGH),217regmap_reg_range(REG_VID_CHA_HORIZONTAL_BACK_PORCH,218REG_VID_CHA_HORIZONTAL_BACK_PORCH),219regmap_reg_range(REG_VID_CHA_VERTICAL_BACK_PORCH,220REG_VID_CHA_VERTICAL_BACK_PORCH),221regmap_reg_range(REG_VID_CHA_HORIZONTAL_FRONT_PORCH,222REG_VID_CHA_HORIZONTAL_FRONT_PORCH),223regmap_reg_range(REG_VID_CHA_VERTICAL_FRONT_PORCH,224REG_VID_CHA_VERTICAL_FRONT_PORCH),225regmap_reg_range(REG_VID_CHA_TEST_PATTERN, REG_VID_CHA_TEST_PATTERN),226regmap_reg_range(REG_IRQ_GLOBAL, REG_IRQ_EN),227regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),228};229230static const struct regmap_access_table sn65dsi83_writeable_table = {231.yes_ranges = sn65dsi83_writeable_ranges,232.n_yes_ranges = ARRAY_SIZE(sn65dsi83_writeable_ranges),233};234235static const struct regmap_range sn65dsi83_volatile_ranges[] = {236regmap_reg_range(REG_RC_RESET, REG_RC_RESET),237regmap_reg_range(REG_RC_LVDS_PLL, REG_RC_LVDS_PLL),238regmap_reg_range(REG_IRQ_STAT, REG_IRQ_STAT),239};240241static const struct regmap_access_table sn65dsi83_volatile_table = {242.yes_ranges = sn65dsi83_volatile_ranges,243.n_yes_ranges = ARRAY_SIZE(sn65dsi83_volatile_ranges),244};245246static const struct regmap_config sn65dsi83_regmap_config = {247.reg_bits = 8,248.val_bits = 8,249.rd_table = &sn65dsi83_readable_table,250.wr_table = &sn65dsi83_writeable_table,251.volatile_table = &sn65dsi83_volatile_table,252.cache_type = REGCACHE_MAPLE,253.max_register = REG_IRQ_STAT,254};255256static const int lvds_vod_swing_data_table[2][4][2] = {257{ /* 100 Ohm */258{ 180000, 313000 },259{ 215000, 372000 },260{ 250000, 430000 },261{ 290000, 488000 },262},263{ /* 200 Ohm */264{ 150000, 261000 },265{ 200000, 346000 },266{ 250000, 428000 },267{ 300000, 511000 },268},269};270271static const int lvds_vod_swing_clock_table[2][4][2] = {272{ /* 100 Ohm */273{ 140000, 244000 },274{ 168000, 290000 },275{ 195000, 335000 },276{ 226000, 381000 },277},278{ /* 200 Ohm */279{ 117000, 204000 },280{ 156000, 270000 },281{ 195000, 334000 },282{ 234000, 399000 },283},284};285286static struct sn65dsi83 *bridge_to_sn65dsi83(struct drm_bridge *bridge)287{288return container_of(bridge, struct sn65dsi83, bridge);289}290291static int sn65dsi83_attach(struct drm_bridge *bridge,292struct drm_encoder *encoder,293enum drm_bridge_attach_flags flags)294{295struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);296297return drm_bridge_attach(encoder, ctx->panel_bridge,298&ctx->bridge, flags);299}300301static void sn65dsi83_detach(struct drm_bridge *bridge)302{303struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);304305if (!ctx->dsi)306return;307308ctx->dsi = NULL;309}310311static u8 sn65dsi83_get_lvds_range(struct sn65dsi83 *ctx,312const struct drm_display_mode *mode)313{314/*315* The encoding of the LVDS_CLK_RANGE is as follows:316* 000 - 25 MHz <= LVDS_CLK < 37.5 MHz317* 001 - 37.5 MHz <= LVDS_CLK < 62.5 MHz318* 010 - 62.5 MHz <= LVDS_CLK < 87.5 MHz319* 011 - 87.5 MHz <= LVDS_CLK < 112.5 MHz320* 100 - 112.5 MHz <= LVDS_CLK < 137.5 MHz321* 101 - 137.5 MHz <= LVDS_CLK <= 154 MHz322* which is a range of 12.5MHz..162.5MHz in 50MHz steps, except that323* the ends of the ranges are clamped to the supported range. Since324* sn65dsi83_mode_valid() already filters the valid modes and limits325* the clock to 25..154 MHz, the range calculation can be simplified326* as follows:327*/328int mode_clock = mode->clock;329330if (ctx->lvds_dual_link)331mode_clock /= 2;332333return (mode_clock - 12500) / 25000;334}335336static u8 sn65dsi83_get_dsi_range(struct sn65dsi83 *ctx,337const struct drm_display_mode *mode)338{339/*340* The encoding of the CHA_DSI_CLK_RANGE is as follows:341* 0x00 through 0x07 - Reserved342* 0x08 - 40 <= DSI_CLK < 45 MHz343* 0x09 - 45 <= DSI_CLK < 50 MHz344* ...345* 0x63 - 495 <= DSI_CLK < 500 MHz346* 0x64 - 500 MHz347* 0x65 through 0xFF - Reserved348* which is DSI clock in 5 MHz steps, clamped to 40..500 MHz.349* The DSI clock are calculated as:350* DSI_CLK = mode clock * bpp / dsi_data_lanes / 2351* the 2 is there because the bus is DDR.352*/353return DIV_ROUND_UP(clamp((unsigned int)mode->clock *354mipi_dsi_pixel_format_to_bpp(ctx->dsi->format) /355ctx->dsi->lanes / 2, 40000U, 500000U), 5000U);356}357358static u8 sn65dsi83_get_dsi_div(struct sn65dsi83 *ctx)359{360/* The divider is (DSI_CLK / LVDS_CLK) - 1, which really is: */361unsigned int dsi_div = mipi_dsi_pixel_format_to_bpp(ctx->dsi->format);362363dsi_div /= ctx->dsi->lanes;364365if (!ctx->lvds_dual_link)366dsi_div /= 2;367368return dsi_div - 1;369}370371static int sn65dsi83_reset_pipe(struct sn65dsi83 *sn65dsi83)372{373struct drm_modeset_acquire_ctx ctx;374int err;375376/*377* Reset active outputs of the related CRTC.378*379* This way, drm core will reconfigure each components in the CRTC380* outputs path. In our case, this will force the previous component to381* go back in LP11 mode and so allow the reconfiguration of SN65DSI83382* bridge.383*384* Keep the lock during the whole operation to be atomic.385*/386387drm_modeset_acquire_init(&ctx, 0);388389dev_warn(sn65dsi83->dev, "reset the pipe\n");390391retry:392err = drm_bridge_helper_reset_crtc(&sn65dsi83->bridge, &ctx);393if (err == -EDEADLK) {394drm_modeset_backoff(&ctx);395goto retry;396}397398drm_modeset_drop_locks(&ctx);399drm_modeset_acquire_fini(&ctx);400401return 0;402}403404static void sn65dsi83_reset_work(struct work_struct *ws)405{406struct sn65dsi83 *ctx = container_of(ws, struct sn65dsi83, reset_work);407int ret;408int idx;409410if (!drm_bridge_enter(&ctx->bridge, &idx))411return;412413/* Reset the pipe */414ret = sn65dsi83_reset_pipe(ctx);415if (ret) {416dev_err(ctx->dev, "reset pipe failed %pe\n", ERR_PTR(ret));417return;418}419if (ctx->irq)420enable_irq(ctx->irq);421422drm_bridge_exit(idx);423}424425static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx)426{427unsigned int irq_stat;428int ret;429int idx;430431if (!drm_bridge_enter(&ctx->bridge, &idx))432return;433434/*435* Schedule a reset in case of:436* - the bridge doesn't answer437* - the bridge signals an error438*/439440ret = regmap_read(ctx->regmap, REG_IRQ_STAT, &irq_stat);441442/*443* Some hardware (Toradex Verdin AM62) is known to report the444* PLL_UNLOCK error interrupt while working without visible445* problems. In lack of a reliable way to discriminate such cases446* from user-visible PLL_UNLOCK cases, ignore that bit entirely.447*/448if (ret || irq_stat & ~REG_IRQ_STAT_CHA_PLL_UNLOCK) {449/*450* IRQ acknowledged is not always possible (the bridge can be in451* a state where it doesn't answer anymore). To prevent an452* interrupt storm, disable interrupt. The interrupt will be453* after the reset.454*/455if (ctx->irq)456disable_irq_nosync(ctx->irq);457458schedule_work(&ctx->reset_work);459}460461drm_bridge_exit(idx);462}463464static void sn65dsi83_monitor_work(struct work_struct *work)465{466struct sn65dsi83 *ctx = container_of(to_delayed_work(work),467struct sn65dsi83, monitor_work);468469sn65dsi83_handle_errors(ctx);470471schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));472}473474static void sn65dsi83_monitor_start(struct sn65dsi83 *ctx)475{476schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));477}478479static void sn65dsi83_monitor_stop(struct sn65dsi83 *ctx)480{481cancel_delayed_work_sync(&ctx->monitor_work);482}483484/*485* Release resources taken by sn65dsi83_atomic_pre_enable().486*487* Invoked by sn65dsi83_atomic_disable() normally, or by devres after488* sn65dsi83_remove() in case this happens befora atomic_disable.489*/490static void sn65dsi83_release_resources(void *data)491{492struct sn65dsi83 *ctx = (struct sn65dsi83 *)data;493int ret;494495if (ctx->irq) {496/* Disable irq */497regmap_write(ctx->regmap, REG_IRQ_EN, 0x0);498regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0);499} else {500/* Stop the polling task */501sn65dsi83_monitor_stop(ctx);502}503504/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */505gpiod_set_value_cansleep(ctx->enable_gpio, 0);506usleep_range(10000, 11000);507508ret = regulator_disable(ctx->vcc);509if (ret)510dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret);511512regcache_mark_dirty(ctx->regmap);513}514515static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,516struct drm_atomic_state *state)517{518struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);519const struct drm_bridge_state *bridge_state;520const struct drm_crtc_state *crtc_state;521const struct drm_display_mode *mode;522struct drm_connector *connector;523struct drm_crtc *crtc;524bool lvds_format_24bpp;525bool lvds_format_jeida;526unsigned int pval;527__le16 le16val;528u16 val;529int ret;530int idx;531532if (!drm_bridge_enter(bridge, &idx))533return;534535ret = regulator_enable(ctx->vcc);536if (ret) {537dev_err(ctx->dev, "Failed to enable vcc: %d\n", ret);538goto err_exit;539}540541/* Deassert reset */542gpiod_set_value_cansleep(ctx->enable_gpio, 1);543usleep_range(10000, 11000);544545/* Get the LVDS format from the bridge state. */546bridge_state = drm_atomic_get_new_bridge_state(state, bridge);547548switch (bridge_state->output_bus_cfg.format) {549case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:550lvds_format_24bpp = false;551lvds_format_jeida = true;552break;553case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:554lvds_format_24bpp = true;555lvds_format_jeida = true;556break;557case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:558lvds_format_24bpp = true;559lvds_format_jeida = false;560break;561default:562/*563* Some bridges still don't set the correct564* LVDS bus pixel format, use SPWG24 default565* format until those are fixed.566*/567lvds_format_24bpp = true;568lvds_format_jeida = false;569dev_warn(ctx->dev,570"Unsupported LVDS bus format 0x%04x, please check output bridge driver. Falling back to SPWG24.\n",571bridge_state->output_bus_cfg.format);572break;573}574575/*576* Retrieve the CRTC adjusted mode. This requires a little dance to go577* from the bridge to the encoder, to the connector and to the CRTC.578*/579connector = drm_atomic_get_new_connector_for_encoder(state,580bridge->encoder);581crtc = drm_atomic_get_new_connector_state(state, connector)->crtc;582crtc_state = drm_atomic_get_new_crtc_state(state, crtc);583mode = &crtc_state->adjusted_mode;584585/* Clear reset, disable PLL */586regmap_write(ctx->regmap, REG_RC_RESET, 0x00);587regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);588589/* Reference clock derived from DSI link clock. */590regmap_write(ctx->regmap, REG_RC_LVDS_PLL,591REG_RC_LVDS_PLL_LVDS_CLK_RANGE(sn65dsi83_get_lvds_range(ctx, mode)) |592REG_RC_LVDS_PLL_HS_CLK_SRC_DPHY);593regmap_write(ctx->regmap, REG_DSI_CLK,594REG_DSI_CLK_CHA_DSI_CLK_RANGE(sn65dsi83_get_dsi_range(ctx, mode)));595regmap_write(ctx->regmap, REG_RC_DSI_CLK,596REG_RC_DSI_CLK_DSI_CLK_DIVIDER(sn65dsi83_get_dsi_div(ctx)));597598/* Set number of DSI lanes and LVDS link config. */599regmap_write(ctx->regmap, REG_DSI_LANE,600REG_DSI_LANE_DSI_CHANNEL_MODE_SINGLE |601REG_DSI_LANE_CHA_DSI_LANES(~(ctx->dsi->lanes - 1)) |602/* CHB is DSI85-only, set to default on DSI83/DSI84 */603REG_DSI_LANE_CHB_DSI_LANES(3));604/* No equalization. */605regmap_write(ctx->regmap, REG_DSI_EQ, 0x00);606607/* Set up sync signal polarity. */608val = (mode->flags & DRM_MODE_FLAG_NHSYNC ?609REG_LVDS_FMT_HS_NEG_POLARITY : 0) |610(mode->flags & DRM_MODE_FLAG_NVSYNC ?611REG_LVDS_FMT_VS_NEG_POLARITY : 0);612val |= bridge_state->output_bus_cfg.flags & DRM_BUS_FLAG_DE_LOW ?613REG_LVDS_FMT_DE_NEG_POLARITY : 0;614615/* Set up bits-per-pixel, 18bpp or 24bpp. */616if (lvds_format_24bpp) {617val |= REG_LVDS_FMT_CHA_24BPP_MODE;618if (ctx->lvds_dual_link)619val |= REG_LVDS_FMT_CHB_24BPP_MODE;620}621622/* Set up LVDS format, JEIDA/Format 1 or SPWG/Format 2 */623if (lvds_format_jeida) {624val |= REG_LVDS_FMT_CHA_24BPP_FORMAT1;625if (ctx->lvds_dual_link)626val |= REG_LVDS_FMT_CHB_24BPP_FORMAT1;627}628629/* Set up LVDS output config (DSI84,DSI85) */630if (!ctx->lvds_dual_link)631val |= REG_LVDS_FMT_LVDS_LINK_CFG;632633regmap_write(ctx->regmap, REG_LVDS_FMT, val);634regmap_write(ctx->regmap, REG_LVDS_VCOM,635REG_LVDS_VCOM_CHA_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_A]) |636REG_LVDS_VCOM_CHB_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_B]));637regmap_write(ctx->regmap, REG_LVDS_LANE,638(ctx->lvds_dual_link_even_odd_swap ?639REG_LVDS_LANE_EVEN_ODD_SWAP : 0) |640(ctx->lvds_term_conf[CHANNEL_A] ?641REG_LVDS_LANE_CHA_LVDS_TERM : 0) |642(ctx->lvds_term_conf[CHANNEL_B] ?643REG_LVDS_LANE_CHB_LVDS_TERM : 0));644regmap_write(ctx->regmap, REG_LVDS_CM, 0x00);645646le16val = cpu_to_le16(mode->hdisplay);647regmap_bulk_write(ctx->regmap, REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,648&le16val, 2);649le16val = cpu_to_le16(mode->vdisplay);650regmap_bulk_write(ctx->regmap, REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,651&le16val, 2);652/* 32 + 1 pixel clock to ensure proper operation */653le16val = cpu_to_le16(32 + 1);654regmap_bulk_write(ctx->regmap, REG_VID_CHA_SYNC_DELAY_LOW, &le16val, 2);655le16val = cpu_to_le16(mode->hsync_end - mode->hsync_start);656regmap_bulk_write(ctx->regmap, REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,657&le16val, 2);658le16val = cpu_to_le16(mode->vsync_end - mode->vsync_start);659regmap_bulk_write(ctx->regmap, REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,660&le16val, 2);661regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_BACK_PORCH,662mode->htotal - mode->hsync_end);663regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_BACK_PORCH,664mode->vtotal - mode->vsync_end);665regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_FRONT_PORCH,666mode->hsync_start - mode->hdisplay);667regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_FRONT_PORCH,668mode->vsync_start - mode->vdisplay);669regmap_write(ctx->regmap, REG_VID_CHA_TEST_PATTERN, 0x00);670671/* Enable PLL */672regmap_write(ctx->regmap, REG_RC_PLL_EN, REG_RC_PLL_EN_PLL_EN);673usleep_range(3000, 4000);674ret = regmap_read_poll_timeout(ctx->regmap, REG_RC_LVDS_PLL, pval,675pval & REG_RC_LVDS_PLL_PLL_EN_STAT,6761000, 100000);677if (ret) {678dev_err(ctx->dev, "failed to lock PLL, ret=%i\n", ret);679/* On failure, disable PLL again and exit. */680regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);681goto err_add_action;682}683684/* Trigger reset after CSR register update. */685regmap_write(ctx->regmap, REG_RC_RESET, REG_RC_RESET_SOFT_RESET);686687/* Wait for 10ms after soft reset as specified in datasheet */688usleep_range(10000, 12000);689690err_add_action:691devm_add_action(ctx->dev, sn65dsi83_release_resources, ctx);692err_exit:693drm_bridge_exit(idx);694}695696static void sn65dsi83_atomic_enable(struct drm_bridge *bridge,697struct drm_atomic_state *state)698{699struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);700unsigned int pval;701int idx;702703if (!drm_bridge_enter(bridge, &idx))704return;705706/* Clear all errors that got asserted during initialization. */707regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);708regmap_write(ctx->regmap, REG_IRQ_STAT, pval);709710/* Wait for 1ms and check for errors in status register */711usleep_range(1000, 1100);712regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);713if (pval)714dev_err(ctx->dev, "Unexpected link status 0x%02x\n", pval);715716if (ctx->irq) {717/* Enable irq to detect errors */718regmap_write(ctx->regmap, REG_IRQ_GLOBAL, REG_IRQ_GLOBAL_IRQ_EN);719regmap_write(ctx->regmap, REG_IRQ_EN, 0xff & ~REG_IRQ_EN_CHA_PLL_UNLOCK_EN);720} else {721/* Use the polling task */722sn65dsi83_monitor_start(ctx);723}724725drm_bridge_exit(idx);726}727728static void sn65dsi83_atomic_disable(struct drm_bridge *bridge,729struct drm_atomic_state *state)730{731struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);732int idx;733734if (!drm_bridge_enter(bridge, &idx))735return;736737devm_release_action(ctx->dev, sn65dsi83_release_resources, ctx);738739drm_bridge_exit(idx);740}741742static enum drm_mode_status743sn65dsi83_mode_valid(struct drm_bridge *bridge,744const struct drm_display_info *info,745const struct drm_display_mode *mode)746{747/* LVDS output clock range 25..154 MHz */748if (mode->clock < 25000)749return MODE_CLOCK_LOW;750if (mode->clock > 154000)751return MODE_CLOCK_HIGH;752753return MODE_OK;754}755756#define MAX_INPUT_SEL_FORMATS 1757758static u32 *759sn65dsi83_atomic_get_input_bus_fmts(struct drm_bridge *bridge,760struct drm_bridge_state *bridge_state,761struct drm_crtc_state *crtc_state,762struct drm_connector_state *conn_state,763u32 output_fmt,764unsigned int *num_input_fmts)765{766u32 *input_fmts;767768*num_input_fmts = 0;769770input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),771GFP_KERNEL);772if (!input_fmts)773return NULL;774775/* This is the DSI-end bus format */776input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;777*num_input_fmts = 1;778779return input_fmts;780}781782static const struct drm_bridge_funcs sn65dsi83_funcs = {783.attach = sn65dsi83_attach,784.detach = sn65dsi83_detach,785.atomic_enable = sn65dsi83_atomic_enable,786.atomic_pre_enable = sn65dsi83_atomic_pre_enable,787.atomic_disable = sn65dsi83_atomic_disable,788.mode_valid = sn65dsi83_mode_valid,789790.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,791.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,792.atomic_reset = drm_atomic_helper_bridge_reset,793.atomic_get_input_bus_fmts = sn65dsi83_atomic_get_input_bus_fmts,794};795796static int sn65dsi83_select_lvds_vod_swing(struct device *dev,797u32 lvds_vod_swing_data[2], u32 lvds_vod_swing_clk[2], u8 lvds_term)798{799int i;800801for (i = 0; i <= 3; i++) {802if (lvds_vod_swing_data_table[lvds_term][i][0] >= lvds_vod_swing_data[0] &&803lvds_vod_swing_data_table[lvds_term][i][1] <= lvds_vod_swing_data[1] &&804lvds_vod_swing_clock_table[lvds_term][i][0] >= lvds_vod_swing_clk[0] &&805lvds_vod_swing_clock_table[lvds_term][i][1] <= lvds_vod_swing_clk[1])806return i;807}808809dev_err(dev, "failed to find appropriate LVDS_VOD_SWING configuration\n");810return -EINVAL;811}812813static int sn65dsi83_parse_lvds_endpoint(struct sn65dsi83 *ctx, int channel)814{815struct device *dev = ctx->dev;816struct device_node *endpoint;817int endpoint_reg;818/* Set so the property can be freely selected if not defined */819u32 lvds_vod_swing_data[2] = { 0, 1000000 };820u32 lvds_vod_swing_clk[2] = { 0, 1000000 };821/* Set default near end terminataion to 200 Ohm */822u32 lvds_term = 200;823int lvds_vod_swing_conf;824int ret = 0;825int ret_data;826int ret_clock;827828if (channel == CHANNEL_A)829endpoint_reg = 2;830else831endpoint_reg = 3;832833endpoint = of_graph_get_endpoint_by_regs(dev->of_node, endpoint_reg, -1);834835of_property_read_u32(endpoint, "ti,lvds-termination-ohms", &lvds_term);836if (lvds_term == 100)837ctx->lvds_term_conf[channel] = OHM_100;838else if (lvds_term == 200)839ctx->lvds_term_conf[channel] = OHM_200;840else {841ret = -EINVAL;842goto exit;843}844845ret_data = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-data-microvolt",846lvds_vod_swing_data, ARRAY_SIZE(lvds_vod_swing_data));847if (ret_data != 0 && ret_data != -EINVAL) {848ret = ret_data;849goto exit;850}851852ret_clock = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-clock-microvolt",853lvds_vod_swing_clk, ARRAY_SIZE(lvds_vod_swing_clk));854if (ret_clock != 0 && ret_clock != -EINVAL) {855ret = ret_clock;856goto exit;857}858859/* Use default value if both properties are NOT defined. */860if (ret_data == -EINVAL && ret_clock == -EINVAL)861lvds_vod_swing_conf = 0x1;862863/* Use lookup table if any of the two properties is defined. */864if (!ret_data || !ret_clock) {865lvds_vod_swing_conf = sn65dsi83_select_lvds_vod_swing(dev, lvds_vod_swing_data,866lvds_vod_swing_clk, ctx->lvds_term_conf[channel]);867if (lvds_vod_swing_conf < 0) {868ret = lvds_vod_swing_conf;869goto exit;870}871}872873ctx->lvds_vod_swing_conf[channel] = lvds_vod_swing_conf;874ret = 0;875exit:876of_node_put(endpoint);877return ret;878}879880static int sn65dsi83_parse_dt(struct sn65dsi83 *ctx, enum sn65dsi83_model model)881{882struct drm_bridge *panel_bridge;883struct device *dev = ctx->dev;884int ret;885886ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_A);887if (ret < 0)888return ret;889890ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_B);891if (ret < 0)892return ret;893894ctx->lvds_dual_link = false;895ctx->lvds_dual_link_even_odd_swap = false;896if (model != MODEL_SN65DSI83) {897struct device_node *port2, *port3;898int dual_link;899900port2 = of_graph_get_port_by_id(dev->of_node, 2);901port3 = of_graph_get_port_by_id(dev->of_node, 3);902dual_link = drm_of_lvds_get_dual_link_pixel_order(port2, port3);903of_node_put(port2);904of_node_put(port3);905906if (dual_link == DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS) {907ctx->lvds_dual_link = true;908/* Odd pixels to LVDS Channel A, even pixels to B */909ctx->lvds_dual_link_even_odd_swap = false;910} else if (dual_link == DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS) {911ctx->lvds_dual_link = true;912/* Even pixels to LVDS Channel A, odd pixels to B */913ctx->lvds_dual_link_even_odd_swap = true;914}915}916917panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 2, 0);918if (IS_ERR(panel_bridge))919return dev_err_probe(dev, PTR_ERR(panel_bridge), "Failed to get panel bridge\n");920921ctx->panel_bridge = panel_bridge;922923ctx->vcc = devm_regulator_get(dev, "vcc");924if (IS_ERR(ctx->vcc))925return dev_err_probe(dev, PTR_ERR(ctx->vcc),926"Failed to get supply 'vcc'\n");927928return 0;929}930931static int sn65dsi83_host_attach(struct sn65dsi83 *ctx)932{933struct device *dev = ctx->dev;934struct device_node *host_node;935struct device_node *endpoint;936struct mipi_dsi_device *dsi;937struct mipi_dsi_host *host;938const struct mipi_dsi_device_info info = {939.type = "sn65dsi83",940.channel = 0,941.node = NULL,942};943int dsi_lanes, ret;944945endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);946dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4);947host_node = of_graph_get_remote_port_parent(endpoint);948host = of_find_mipi_dsi_host_by_node(host_node);949of_node_put(host_node);950of_node_put(endpoint);951952if (!host)953return -EPROBE_DEFER;954955if (dsi_lanes < 0)956return dsi_lanes;957958dsi = devm_mipi_dsi_device_register_full(dev, host, &info);959if (IS_ERR(dsi))960return dev_err_probe(dev, PTR_ERR(dsi),961"failed to create dsi device\n");962963ctx->dsi = dsi;964965dsi->lanes = dsi_lanes;966dsi->format = MIPI_DSI_FMT_RGB888;967dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |968MIPI_DSI_MODE_VIDEO_NO_HFP | MIPI_DSI_MODE_VIDEO_NO_HBP |969MIPI_DSI_MODE_VIDEO_NO_HSA | MIPI_DSI_MODE_NO_EOT_PACKET;970971ret = devm_mipi_dsi_attach(dev, dsi);972if (ret < 0) {973dev_err(dev, "failed to attach dsi to host: %d\n", ret);974return ret;975}976977return 0;978}979980static irqreturn_t sn65dsi83_irq(int irq, void *data)981{982struct sn65dsi83 *ctx = data;983984sn65dsi83_handle_errors(ctx);985return IRQ_HANDLED;986}987988static int sn65dsi83_probe(struct i2c_client *client)989{990const struct i2c_device_id *id = i2c_client_get_device_id(client);991struct device *dev = &client->dev;992enum sn65dsi83_model model;993struct sn65dsi83 *ctx;994int ret;995996ctx = devm_drm_bridge_alloc(dev, struct sn65dsi83, bridge, &sn65dsi83_funcs);997if (IS_ERR(ctx))998return PTR_ERR(ctx);9991000ctx->dev = dev;1001INIT_WORK(&ctx->reset_work, sn65dsi83_reset_work);1002INIT_DELAYED_WORK(&ctx->monitor_work, sn65dsi83_monitor_work);10031004if (dev->of_node) {1005model = (enum sn65dsi83_model)(uintptr_t)1006of_device_get_match_data(dev);1007} else {1008model = id->driver_data;1009}10101011/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */1012ctx->enable_gpio = devm_gpiod_get_optional(ctx->dev, "enable",1013GPIOD_OUT_LOW);1014if (IS_ERR(ctx->enable_gpio))1015return dev_err_probe(dev, PTR_ERR(ctx->enable_gpio), "failed to get enable GPIO\n");10161017usleep_range(10000, 11000);10181019ret = sn65dsi83_parse_dt(ctx, model);1020if (ret)1021return ret;10221023ctx->regmap = devm_regmap_init_i2c(client, &sn65dsi83_regmap_config);1024if (IS_ERR(ctx->regmap))1025return dev_err_probe(dev, PTR_ERR(ctx->regmap), "failed to get regmap\n");10261027if (client->irq) {1028ctx->irq = client->irq;1029ret = devm_request_threaded_irq(ctx->dev, ctx->irq, NULL, sn65dsi83_irq,1030IRQF_ONESHOT, dev_name(ctx->dev), ctx);1031if (ret)1032return dev_err_probe(dev, ret, "failed to request irq\n");1033}10341035dev_set_drvdata(dev, ctx);1036i2c_set_clientdata(client, ctx);10371038ctx->bridge.of_node = dev->of_node;1039ctx->bridge.pre_enable_prev_first = true;1040ctx->bridge.type = DRM_MODE_CONNECTOR_LVDS;1041drm_bridge_add(&ctx->bridge);10421043ret = sn65dsi83_host_attach(ctx);1044if (ret) {1045dev_err_probe(dev, ret, "failed to attach DSI host\n");1046goto err_remove_bridge;1047}10481049return 0;10501051err_remove_bridge:1052drm_bridge_remove(&ctx->bridge);1053return ret;1054}10551056static void sn65dsi83_remove(struct i2c_client *client)1057{1058struct sn65dsi83 *ctx = i2c_get_clientdata(client);10591060drm_bridge_unplug(&ctx->bridge);1061}10621063static const struct i2c_device_id sn65dsi83_id[] = {1064{ "ti,sn65dsi83", MODEL_SN65DSI83 },1065{ "ti,sn65dsi84", MODEL_SN65DSI84 },1066{},1067};1068MODULE_DEVICE_TABLE(i2c, sn65dsi83_id);10691070static const struct of_device_id sn65dsi83_match_table[] = {1071{ .compatible = "ti,sn65dsi83", .data = (void *)MODEL_SN65DSI83 },1072{ .compatible = "ti,sn65dsi84", .data = (void *)MODEL_SN65DSI84 },1073{},1074};1075MODULE_DEVICE_TABLE(of, sn65dsi83_match_table);10761077static struct i2c_driver sn65dsi83_driver = {1078.probe = sn65dsi83_probe,1079.remove = sn65dsi83_remove,1080.id_table = sn65dsi83_id,1081.driver = {1082.name = "sn65dsi83",1083.of_match_table = sn65dsi83_match_table,1084},1085};1086module_i2c_driver(sn65dsi83_driver);10871088MODULE_AUTHOR("Marek Vasut <[email protected]>");1089MODULE_DESCRIPTION("TI SN65DSI83 DSI to LVDS bridge driver");1090MODULE_LICENSE("GPL v2");109110921093