Path: blob/master/drivers/gpu/drm/bridge/ti-sn65dsi83.c
26494 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;408409/* Reset the pipe */410ret = sn65dsi83_reset_pipe(ctx);411if (ret) {412dev_err(ctx->dev, "reset pipe failed %pe\n", ERR_PTR(ret));413return;414}415if (ctx->irq)416enable_irq(ctx->irq);417}418419static void sn65dsi83_handle_errors(struct sn65dsi83 *ctx)420{421unsigned int irq_stat;422int ret;423424/*425* Schedule a reset in case of:426* - the bridge doesn't answer427* - the bridge signals an error428*/429430ret = regmap_read(ctx->regmap, REG_IRQ_STAT, &irq_stat);431if (ret || irq_stat) {432/*433* IRQ acknowledged is not always possible (the bridge can be in434* a state where it doesn't answer anymore). To prevent an435* interrupt storm, disable interrupt. The interrupt will be436* after the reset.437*/438if (ctx->irq)439disable_irq_nosync(ctx->irq);440441schedule_work(&ctx->reset_work);442}443}444445static void sn65dsi83_monitor_work(struct work_struct *work)446{447struct sn65dsi83 *ctx = container_of(to_delayed_work(work),448struct sn65dsi83, monitor_work);449450sn65dsi83_handle_errors(ctx);451452schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));453}454455static void sn65dsi83_monitor_start(struct sn65dsi83 *ctx)456{457schedule_delayed_work(&ctx->monitor_work, msecs_to_jiffies(1000));458}459460static void sn65dsi83_monitor_stop(struct sn65dsi83 *ctx)461{462cancel_delayed_work_sync(&ctx->monitor_work);463}464465static void sn65dsi83_atomic_pre_enable(struct drm_bridge *bridge,466struct drm_atomic_state *state)467{468struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);469const struct drm_bridge_state *bridge_state;470const struct drm_crtc_state *crtc_state;471const struct drm_display_mode *mode;472struct drm_connector *connector;473struct drm_crtc *crtc;474bool lvds_format_24bpp;475bool lvds_format_jeida;476unsigned int pval;477__le16 le16val;478u16 val;479int ret;480481ret = regulator_enable(ctx->vcc);482if (ret) {483dev_err(ctx->dev, "Failed to enable vcc: %d\n", ret);484return;485}486487/* Deassert reset */488gpiod_set_value_cansleep(ctx->enable_gpio, 1);489usleep_range(10000, 11000);490491/* Get the LVDS format from the bridge state. */492bridge_state = drm_atomic_get_new_bridge_state(state, bridge);493494switch (bridge_state->output_bus_cfg.format) {495case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:496lvds_format_24bpp = false;497lvds_format_jeida = true;498break;499case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:500lvds_format_24bpp = true;501lvds_format_jeida = true;502break;503case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:504lvds_format_24bpp = true;505lvds_format_jeida = false;506break;507default:508/*509* Some bridges still don't set the correct510* LVDS bus pixel format, use SPWG24 default511* format until those are fixed.512*/513lvds_format_24bpp = true;514lvds_format_jeida = false;515dev_warn(ctx->dev,516"Unsupported LVDS bus format 0x%04x, please check output bridge driver. Falling back to SPWG24.\n",517bridge_state->output_bus_cfg.format);518break;519}520521/*522* Retrieve the CRTC adjusted mode. This requires a little dance to go523* from the bridge to the encoder, to the connector and to the CRTC.524*/525connector = drm_atomic_get_new_connector_for_encoder(state,526bridge->encoder);527crtc = drm_atomic_get_new_connector_state(state, connector)->crtc;528crtc_state = drm_atomic_get_new_crtc_state(state, crtc);529mode = &crtc_state->adjusted_mode;530531/* Clear reset, disable PLL */532regmap_write(ctx->regmap, REG_RC_RESET, 0x00);533regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);534535/* Reference clock derived from DSI link clock. */536regmap_write(ctx->regmap, REG_RC_LVDS_PLL,537REG_RC_LVDS_PLL_LVDS_CLK_RANGE(sn65dsi83_get_lvds_range(ctx, mode)) |538REG_RC_LVDS_PLL_HS_CLK_SRC_DPHY);539regmap_write(ctx->regmap, REG_DSI_CLK,540REG_DSI_CLK_CHA_DSI_CLK_RANGE(sn65dsi83_get_dsi_range(ctx, mode)));541regmap_write(ctx->regmap, REG_RC_DSI_CLK,542REG_RC_DSI_CLK_DSI_CLK_DIVIDER(sn65dsi83_get_dsi_div(ctx)));543544/* Set number of DSI lanes and LVDS link config. */545regmap_write(ctx->regmap, REG_DSI_LANE,546REG_DSI_LANE_DSI_CHANNEL_MODE_SINGLE |547REG_DSI_LANE_CHA_DSI_LANES(~(ctx->dsi->lanes - 1)) |548/* CHB is DSI85-only, set to default on DSI83/DSI84 */549REG_DSI_LANE_CHB_DSI_LANES(3));550/* No equalization. */551regmap_write(ctx->regmap, REG_DSI_EQ, 0x00);552553/* Set up sync signal polarity. */554val = (mode->flags & DRM_MODE_FLAG_NHSYNC ?555REG_LVDS_FMT_HS_NEG_POLARITY : 0) |556(mode->flags & DRM_MODE_FLAG_NVSYNC ?557REG_LVDS_FMT_VS_NEG_POLARITY : 0);558val |= bridge_state->output_bus_cfg.flags & DRM_BUS_FLAG_DE_LOW ?559REG_LVDS_FMT_DE_NEG_POLARITY : 0;560561/* Set up bits-per-pixel, 18bpp or 24bpp. */562if (lvds_format_24bpp) {563val |= REG_LVDS_FMT_CHA_24BPP_MODE;564if (ctx->lvds_dual_link)565val |= REG_LVDS_FMT_CHB_24BPP_MODE;566}567568/* Set up LVDS format, JEIDA/Format 1 or SPWG/Format 2 */569if (lvds_format_jeida) {570val |= REG_LVDS_FMT_CHA_24BPP_FORMAT1;571if (ctx->lvds_dual_link)572val |= REG_LVDS_FMT_CHB_24BPP_FORMAT1;573}574575/* Set up LVDS output config (DSI84,DSI85) */576if (!ctx->lvds_dual_link)577val |= REG_LVDS_FMT_LVDS_LINK_CFG;578579regmap_write(ctx->regmap, REG_LVDS_FMT, val);580regmap_write(ctx->regmap, REG_LVDS_VCOM,581REG_LVDS_VCOM_CHA_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_A]) |582REG_LVDS_VCOM_CHB_LVDS_VOD_SWING(ctx->lvds_vod_swing_conf[CHANNEL_B]));583regmap_write(ctx->regmap, REG_LVDS_LANE,584(ctx->lvds_dual_link_even_odd_swap ?585REG_LVDS_LANE_EVEN_ODD_SWAP : 0) |586(ctx->lvds_term_conf[CHANNEL_A] ?587REG_LVDS_LANE_CHA_LVDS_TERM : 0) |588(ctx->lvds_term_conf[CHANNEL_B] ?589REG_LVDS_LANE_CHB_LVDS_TERM : 0));590regmap_write(ctx->regmap, REG_LVDS_CM, 0x00);591592le16val = cpu_to_le16(mode->hdisplay);593regmap_bulk_write(ctx->regmap, REG_VID_CHA_ACTIVE_LINE_LENGTH_LOW,594&le16val, 2);595le16val = cpu_to_le16(mode->vdisplay);596regmap_bulk_write(ctx->regmap, REG_VID_CHA_VERTICAL_DISPLAY_SIZE_LOW,597&le16val, 2);598/* 32 + 1 pixel clock to ensure proper operation */599le16val = cpu_to_le16(32 + 1);600regmap_bulk_write(ctx->regmap, REG_VID_CHA_SYNC_DELAY_LOW, &le16val, 2);601le16val = cpu_to_le16(mode->hsync_end - mode->hsync_start);602regmap_bulk_write(ctx->regmap, REG_VID_CHA_HSYNC_PULSE_WIDTH_LOW,603&le16val, 2);604le16val = cpu_to_le16(mode->vsync_end - mode->vsync_start);605regmap_bulk_write(ctx->regmap, REG_VID_CHA_VSYNC_PULSE_WIDTH_LOW,606&le16val, 2);607regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_BACK_PORCH,608mode->htotal - mode->hsync_end);609regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_BACK_PORCH,610mode->vtotal - mode->vsync_end);611regmap_write(ctx->regmap, REG_VID_CHA_HORIZONTAL_FRONT_PORCH,612mode->hsync_start - mode->hdisplay);613regmap_write(ctx->regmap, REG_VID_CHA_VERTICAL_FRONT_PORCH,614mode->vsync_start - mode->vdisplay);615regmap_write(ctx->regmap, REG_VID_CHA_TEST_PATTERN, 0x00);616617/* Enable PLL */618regmap_write(ctx->regmap, REG_RC_PLL_EN, REG_RC_PLL_EN_PLL_EN);619usleep_range(3000, 4000);620ret = regmap_read_poll_timeout(ctx->regmap, REG_RC_LVDS_PLL, pval,621pval & REG_RC_LVDS_PLL_PLL_EN_STAT,6221000, 100000);623if (ret) {624dev_err(ctx->dev, "failed to lock PLL, ret=%i\n", ret);625/* On failure, disable PLL again and exit. */626regmap_write(ctx->regmap, REG_RC_PLL_EN, 0x00);627return;628}629630/* Trigger reset after CSR register update. */631regmap_write(ctx->regmap, REG_RC_RESET, REG_RC_RESET_SOFT_RESET);632633/* Wait for 10ms after soft reset as specified in datasheet */634usleep_range(10000, 12000);635}636637static void sn65dsi83_atomic_enable(struct drm_bridge *bridge,638struct drm_atomic_state *state)639{640struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);641unsigned int pval;642643/* Clear all errors that got asserted during initialization. */644regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);645regmap_write(ctx->regmap, REG_IRQ_STAT, pval);646647/* Wait for 1ms and check for errors in status register */648usleep_range(1000, 1100);649regmap_read(ctx->regmap, REG_IRQ_STAT, &pval);650if (pval)651dev_err(ctx->dev, "Unexpected link status 0x%02x\n", pval);652653if (ctx->irq) {654/* Enable irq to detect errors */655regmap_write(ctx->regmap, REG_IRQ_GLOBAL, REG_IRQ_GLOBAL_IRQ_EN);656regmap_write(ctx->regmap, REG_IRQ_EN, 0xff);657} else {658/* Use the polling task */659sn65dsi83_monitor_start(ctx);660}661}662663static void sn65dsi83_atomic_disable(struct drm_bridge *bridge,664struct drm_atomic_state *state)665{666struct sn65dsi83 *ctx = bridge_to_sn65dsi83(bridge);667int ret;668669if (ctx->irq) {670/* Disable irq */671regmap_write(ctx->regmap, REG_IRQ_EN, 0x0);672regmap_write(ctx->regmap, REG_IRQ_GLOBAL, 0x0);673} else {674/* Stop the polling task */675sn65dsi83_monitor_stop(ctx);676}677678/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */679gpiod_set_value_cansleep(ctx->enable_gpio, 0);680usleep_range(10000, 11000);681682ret = regulator_disable(ctx->vcc);683if (ret)684dev_err(ctx->dev, "Failed to disable vcc: %d\n", ret);685686regcache_mark_dirty(ctx->regmap);687}688689static enum drm_mode_status690sn65dsi83_mode_valid(struct drm_bridge *bridge,691const struct drm_display_info *info,692const struct drm_display_mode *mode)693{694/* LVDS output clock range 25..154 MHz */695if (mode->clock < 25000)696return MODE_CLOCK_LOW;697if (mode->clock > 154000)698return MODE_CLOCK_HIGH;699700return MODE_OK;701}702703#define MAX_INPUT_SEL_FORMATS 1704705static u32 *706sn65dsi83_atomic_get_input_bus_fmts(struct drm_bridge *bridge,707struct drm_bridge_state *bridge_state,708struct drm_crtc_state *crtc_state,709struct drm_connector_state *conn_state,710u32 output_fmt,711unsigned int *num_input_fmts)712{713u32 *input_fmts;714715*num_input_fmts = 0;716717input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),718GFP_KERNEL);719if (!input_fmts)720return NULL;721722/* This is the DSI-end bus format */723input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;724*num_input_fmts = 1;725726return input_fmts;727}728729static const struct drm_bridge_funcs sn65dsi83_funcs = {730.attach = sn65dsi83_attach,731.detach = sn65dsi83_detach,732.atomic_enable = sn65dsi83_atomic_enable,733.atomic_pre_enable = sn65dsi83_atomic_pre_enable,734.atomic_disable = sn65dsi83_atomic_disable,735.mode_valid = sn65dsi83_mode_valid,736737.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,738.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,739.atomic_reset = drm_atomic_helper_bridge_reset,740.atomic_get_input_bus_fmts = sn65dsi83_atomic_get_input_bus_fmts,741};742743static int sn65dsi83_select_lvds_vod_swing(struct device *dev,744u32 lvds_vod_swing_data[2], u32 lvds_vod_swing_clk[2], u8 lvds_term)745{746int i;747748for (i = 0; i <= 3; i++) {749if (lvds_vod_swing_data_table[lvds_term][i][0] >= lvds_vod_swing_data[0] &&750lvds_vod_swing_data_table[lvds_term][i][1] <= lvds_vod_swing_data[1] &&751lvds_vod_swing_clock_table[lvds_term][i][0] >= lvds_vod_swing_clk[0] &&752lvds_vod_swing_clock_table[lvds_term][i][1] <= lvds_vod_swing_clk[1])753return i;754}755756dev_err(dev, "failed to find appropriate LVDS_VOD_SWING configuration\n");757return -EINVAL;758}759760static int sn65dsi83_parse_lvds_endpoint(struct sn65dsi83 *ctx, int channel)761{762struct device *dev = ctx->dev;763struct device_node *endpoint;764int endpoint_reg;765/* Set so the property can be freely selected if not defined */766u32 lvds_vod_swing_data[2] = { 0, 1000000 };767u32 lvds_vod_swing_clk[2] = { 0, 1000000 };768/* Set default near end terminataion to 200 Ohm */769u32 lvds_term = 200;770int lvds_vod_swing_conf;771int ret = 0;772int ret_data;773int ret_clock;774775if (channel == CHANNEL_A)776endpoint_reg = 2;777else778endpoint_reg = 3;779780endpoint = of_graph_get_endpoint_by_regs(dev->of_node, endpoint_reg, -1);781782of_property_read_u32(endpoint, "ti,lvds-termination-ohms", &lvds_term);783if (lvds_term == 100)784ctx->lvds_term_conf[channel] = OHM_100;785else if (lvds_term == 200)786ctx->lvds_term_conf[channel] = OHM_200;787else {788ret = -EINVAL;789goto exit;790}791792ret_data = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-data-microvolt",793lvds_vod_swing_data, ARRAY_SIZE(lvds_vod_swing_data));794if (ret_data != 0 && ret_data != -EINVAL) {795ret = ret_data;796goto exit;797}798799ret_clock = of_property_read_u32_array(endpoint, "ti,lvds-vod-swing-clock-microvolt",800lvds_vod_swing_clk, ARRAY_SIZE(lvds_vod_swing_clk));801if (ret_clock != 0 && ret_clock != -EINVAL) {802ret = ret_clock;803goto exit;804}805806/* Use default value if both properties are NOT defined. */807if (ret_data == -EINVAL && ret_clock == -EINVAL)808lvds_vod_swing_conf = 0x1;809810/* Use lookup table if any of the two properties is defined. */811if (!ret_data || !ret_clock) {812lvds_vod_swing_conf = sn65dsi83_select_lvds_vod_swing(dev, lvds_vod_swing_data,813lvds_vod_swing_clk, ctx->lvds_term_conf[channel]);814if (lvds_vod_swing_conf < 0) {815ret = lvds_vod_swing_conf;816goto exit;817}818}819820ctx->lvds_vod_swing_conf[channel] = lvds_vod_swing_conf;821ret = 0;822exit:823of_node_put(endpoint);824return ret;825}826827static int sn65dsi83_parse_dt(struct sn65dsi83 *ctx, enum sn65dsi83_model model)828{829struct drm_bridge *panel_bridge;830struct device *dev = ctx->dev;831int ret;832833ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_A);834if (ret < 0)835return ret;836837ret = sn65dsi83_parse_lvds_endpoint(ctx, CHANNEL_B);838if (ret < 0)839return ret;840841ctx->lvds_dual_link = false;842ctx->lvds_dual_link_even_odd_swap = false;843if (model != MODEL_SN65DSI83) {844struct device_node *port2, *port3;845int dual_link;846847port2 = of_graph_get_port_by_id(dev->of_node, 2);848port3 = of_graph_get_port_by_id(dev->of_node, 3);849dual_link = drm_of_lvds_get_dual_link_pixel_order(port2, port3);850of_node_put(port2);851of_node_put(port3);852853if (dual_link == DRM_LVDS_DUAL_LINK_ODD_EVEN_PIXELS) {854ctx->lvds_dual_link = true;855/* Odd pixels to LVDS Channel A, even pixels to B */856ctx->lvds_dual_link_even_odd_swap = false;857} else if (dual_link == DRM_LVDS_DUAL_LINK_EVEN_ODD_PIXELS) {858ctx->lvds_dual_link = true;859/* Even pixels to LVDS Channel A, odd pixels to B */860ctx->lvds_dual_link_even_odd_swap = true;861}862}863864panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 2, 0);865if (IS_ERR(panel_bridge))866return dev_err_probe(dev, PTR_ERR(panel_bridge), "Failed to get panel bridge\n");867868ctx->panel_bridge = panel_bridge;869870ctx->vcc = devm_regulator_get(dev, "vcc");871if (IS_ERR(ctx->vcc))872return dev_err_probe(dev, PTR_ERR(ctx->vcc),873"Failed to get supply 'vcc'\n");874875return 0;876}877878static int sn65dsi83_host_attach(struct sn65dsi83 *ctx)879{880struct device *dev = ctx->dev;881struct device_node *host_node;882struct device_node *endpoint;883struct mipi_dsi_device *dsi;884struct mipi_dsi_host *host;885const struct mipi_dsi_device_info info = {886.type = "sn65dsi83",887.channel = 0,888.node = NULL,889};890int dsi_lanes, ret;891892endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);893dsi_lanes = drm_of_get_data_lanes_count(endpoint, 1, 4);894host_node = of_graph_get_remote_port_parent(endpoint);895host = of_find_mipi_dsi_host_by_node(host_node);896of_node_put(host_node);897of_node_put(endpoint);898899if (!host)900return -EPROBE_DEFER;901902if (dsi_lanes < 0)903return dsi_lanes;904905dsi = devm_mipi_dsi_device_register_full(dev, host, &info);906if (IS_ERR(dsi))907return dev_err_probe(dev, PTR_ERR(dsi),908"failed to create dsi device\n");909910ctx->dsi = dsi;911912dsi->lanes = dsi_lanes;913dsi->format = MIPI_DSI_FMT_RGB888;914dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |915MIPI_DSI_MODE_VIDEO_NO_HFP | MIPI_DSI_MODE_VIDEO_NO_HBP |916MIPI_DSI_MODE_VIDEO_NO_HSA | MIPI_DSI_MODE_NO_EOT_PACKET;917918ret = devm_mipi_dsi_attach(dev, dsi);919if (ret < 0) {920dev_err(dev, "failed to attach dsi to host: %d\n", ret);921return ret;922}923924return 0;925}926927static irqreturn_t sn65dsi83_irq(int irq, void *data)928{929struct sn65dsi83 *ctx = data;930931sn65dsi83_handle_errors(ctx);932return IRQ_HANDLED;933}934935static int sn65dsi83_probe(struct i2c_client *client)936{937const struct i2c_device_id *id = i2c_client_get_device_id(client);938struct device *dev = &client->dev;939enum sn65dsi83_model model;940struct sn65dsi83 *ctx;941int ret;942943ctx = devm_drm_bridge_alloc(dev, struct sn65dsi83, bridge, &sn65dsi83_funcs);944if (IS_ERR(ctx))945return PTR_ERR(ctx);946947ctx->dev = dev;948INIT_WORK(&ctx->reset_work, sn65dsi83_reset_work);949INIT_DELAYED_WORK(&ctx->monitor_work, sn65dsi83_monitor_work);950951if (dev->of_node) {952model = (enum sn65dsi83_model)(uintptr_t)953of_device_get_match_data(dev);954} else {955model = id->driver_data;956}957958/* Put the chip in reset, pull EN line low, and assure 10ms reset low timing. */959ctx->enable_gpio = devm_gpiod_get_optional(ctx->dev, "enable",960GPIOD_OUT_LOW);961if (IS_ERR(ctx->enable_gpio))962return dev_err_probe(dev, PTR_ERR(ctx->enable_gpio), "failed to get enable GPIO\n");963964usleep_range(10000, 11000);965966ret = sn65dsi83_parse_dt(ctx, model);967if (ret)968return ret;969970ctx->regmap = devm_regmap_init_i2c(client, &sn65dsi83_regmap_config);971if (IS_ERR(ctx->regmap))972return dev_err_probe(dev, PTR_ERR(ctx->regmap), "failed to get regmap\n");973974if (client->irq) {975ctx->irq = client->irq;976ret = devm_request_threaded_irq(ctx->dev, ctx->irq, NULL, sn65dsi83_irq,977IRQF_ONESHOT, dev_name(ctx->dev), ctx);978if (ret)979return dev_err_probe(dev, ret, "failed to request irq\n");980}981982dev_set_drvdata(dev, ctx);983i2c_set_clientdata(client, ctx);984985ctx->bridge.of_node = dev->of_node;986ctx->bridge.pre_enable_prev_first = true;987ctx->bridge.type = DRM_MODE_CONNECTOR_LVDS;988drm_bridge_add(&ctx->bridge);989990ret = sn65dsi83_host_attach(ctx);991if (ret) {992dev_err_probe(dev, ret, "failed to attach DSI host\n");993goto err_remove_bridge;994}995996return 0;997998err_remove_bridge:999drm_bridge_remove(&ctx->bridge);1000return ret;1001}10021003static void sn65dsi83_remove(struct i2c_client *client)1004{1005struct sn65dsi83 *ctx = i2c_get_clientdata(client);10061007drm_bridge_remove(&ctx->bridge);1008}10091010static const struct i2c_device_id sn65dsi83_id[] = {1011{ "ti,sn65dsi83", MODEL_SN65DSI83 },1012{ "ti,sn65dsi84", MODEL_SN65DSI84 },1013{},1014};1015MODULE_DEVICE_TABLE(i2c, sn65dsi83_id);10161017static const struct of_device_id sn65dsi83_match_table[] = {1018{ .compatible = "ti,sn65dsi83", .data = (void *)MODEL_SN65DSI83 },1019{ .compatible = "ti,sn65dsi84", .data = (void *)MODEL_SN65DSI84 },1020{},1021};1022MODULE_DEVICE_TABLE(of, sn65dsi83_match_table);10231024static struct i2c_driver sn65dsi83_driver = {1025.probe = sn65dsi83_probe,1026.remove = sn65dsi83_remove,1027.id_table = sn65dsi83_id,1028.driver = {1029.name = "sn65dsi83",1030.of_match_table = sn65dsi83_match_table,1031},1032};1033module_i2c_driver(sn65dsi83_driver);10341035MODULE_AUTHOR("Marek Vasut <[email protected]>");1036MODULE_DESCRIPTION("TI SN65DSI83 DSI to LVDS bridge driver");1037MODULE_LICENSE("GPL v2");103810391040