Path: blob/master/drivers/gpu/drm/bridge/ti-sn65dsi86.c
50904 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (c) 2018, The Linux Foundation. All rights reserved.3* datasheet: https://www.ti.com/lit/ds/symlink/sn65dsi86.pdf4*/56#include <linux/atomic.h>7#include <linux/auxiliary_bus.h>8#include <linux/bitfield.h>9#include <linux/bits.h>10#include <linux/clk.h>11#include <linux/debugfs.h>12#include <linux/gpio/consumer.h>13#include <linux/gpio/driver.h>14#include <linux/i2c.h>15#include <linux/iopoll.h>16#include <linux/module.h>17#include <linux/of_graph.h>18#include <linux/pm_runtime.h>19#include <linux/pwm.h>20#include <linux/regmap.h>21#include <linux/regulator/consumer.h>2223#include <linux/unaligned.h>2425#include <drm/display/drm_dp_aux_bus.h>26#include <drm/display/drm_dp_helper.h>27#include <drm/drm_atomic.h>28#include <drm/drm_atomic_helper.h>29#include <drm/drm_bridge.h>30#include <drm/drm_bridge_connector.h>31#include <drm/drm_edid.h>32#include <drm/drm_mipi_dsi.h>33#include <drm/drm_of.h>34#include <drm/drm_print.h>35#include <drm/drm_probe_helper.h>3637#define SN_DEVICE_ID_REGS 0x00 /* up to 0x07 */38#define SN_DEVICE_REV_REG 0x0839#define SN_DPPLL_SRC_REG 0x0A40#define DPPLL_CLK_SRC_DSICLK BIT(0)41#define REFCLK_FREQ_MASK GENMASK(3, 1)42#define REFCLK_FREQ(x) ((x) << 1)43#define DPPLL_SRC_DP_PLL_LOCK BIT(7)44#define SN_PLL_ENABLE_REG 0x0D45#define SN_DSI_LANES_REG 0x1046#define CHA_DSI_LANES_MASK GENMASK(4, 3)47#define CHA_DSI_LANES(x) ((x) << 3)48#define SN_DSIA_CLK_FREQ_REG 0x1249#define SN_CHA_ACTIVE_LINE_LENGTH_LOW_REG 0x2050#define SN_CHA_VERTICAL_DISPLAY_SIZE_LOW_REG 0x2451#define SN_CHA_HSYNC_PULSE_WIDTH_LOW_REG 0x2C52#define SN_CHA_HSYNC_PULSE_WIDTH_HIGH_REG 0x2D53#define CHA_HSYNC_POLARITY BIT(7)54#define SN_CHA_VSYNC_PULSE_WIDTH_LOW_REG 0x3055#define SN_CHA_VSYNC_PULSE_WIDTH_HIGH_REG 0x3156#define CHA_VSYNC_POLARITY BIT(7)57#define SN_CHA_HORIZONTAL_BACK_PORCH_REG 0x3458#define SN_CHA_VERTICAL_BACK_PORCH_REG 0x3659#define SN_CHA_HORIZONTAL_FRONT_PORCH_REG 0x3860#define SN_CHA_VERTICAL_FRONT_PORCH_REG 0x3A61#define SN_LN_ASSIGN_REG 0x5962#define LN_ASSIGN_WIDTH 263#define SN_ENH_FRAME_REG 0x5A64#define VSTREAM_ENABLE BIT(3)65#define LN_POLRS_OFFSET 466#define LN_POLRS_MASK 0xf067#define SN_DATA_FORMAT_REG 0x5B68#define BPP_18_RGB BIT(0)69#define SN_HPD_DISABLE_REG 0x5C70#define HPD_DISABLE BIT(0)71#define HPD_DEBOUNCED_STATE BIT(4)72#define SN_GPIO_IO_REG 0x5E73#define SN_GPIO_INPUT_SHIFT 474#define SN_GPIO_OUTPUT_SHIFT 075#define SN_GPIO_CTRL_REG 0x5F76#define SN_GPIO_MUX_INPUT 077#define SN_GPIO_MUX_OUTPUT 178#define SN_GPIO_MUX_SPECIAL 279#define SN_GPIO_MUX_MASK 0x380#define SN_AUX_WDATA_REG(x) (0x64 + (x))81#define SN_AUX_ADDR_19_16_REG 0x7482#define SN_AUX_ADDR_15_8_REG 0x7583#define SN_AUX_ADDR_7_0_REG 0x7684#define SN_AUX_ADDR_MASK GENMASK(19, 0)85#define SN_AUX_LENGTH_REG 0x7786#define SN_AUX_CMD_REG 0x7887#define AUX_CMD_SEND BIT(0)88#define AUX_CMD_REQ(x) ((x) << 4)89#define SN_AUX_RDATA_REG(x) (0x79 + (x))90#define SN_SSC_CONFIG_REG 0x9391#define DP_NUM_LANES_MASK GENMASK(5, 4)92#define DP_NUM_LANES(x) ((x) << 4)93#define SN_DATARATE_CONFIG_REG 0x9494#define DP_DATARATE_MASK GENMASK(7, 5)95#define DP_DATARATE(x) ((x) << 5)96#define SN_TRAINING_SETTING_REG 0x9597#define SCRAMBLE_DISABLE BIT(4)98#define SN_ML_TX_MODE_REG 0x9699#define ML_TX_MAIN_LINK_OFF 0100#define ML_TX_NORMAL_MODE BIT(0)101#define SN_PWM_PRE_DIV_REG 0xA0102#define SN_BACKLIGHT_SCALE_REG 0xA1103#define BACKLIGHT_SCALE_MAX 0xFFFF104#define SN_BACKLIGHT_REG 0xA3105#define SN_PWM_EN_INV_REG 0xA5106#define SN_PWM_INV_MASK BIT(0)107#define SN_PWM_EN_MASK BIT(1)108109#define SN_IRQ_EN_REG 0xE0110#define IRQ_EN BIT(0)111112#define SN_IRQ_EVENTS_EN_REG 0xE6113#define HPD_INSERTION_EN BIT(1)114#define HPD_REMOVAL_EN BIT(2)115116#define SN_AUX_CMD_STATUS_REG 0xF4117#define AUX_IRQ_STATUS_AUX_RPLY_TOUT BIT(3)118#define AUX_IRQ_STATUS_AUX_SHORT BIT(5)119#define AUX_IRQ_STATUS_NAT_I2C_FAIL BIT(6)120#define SN_IRQ_STATUS_REG 0xF5121#define HPD_REMOVAL_STATUS BIT(2)122#define HPD_INSERTION_STATUS BIT(1)123124#define MIN_DSI_CLK_FREQ_MHZ 40125126/* fudge factor required to account for 8b/10b encoding */127#define DP_CLK_FUDGE_NUM 10128#define DP_CLK_FUDGE_DEN 8129130/* Matches DP_AUX_MAX_PAYLOAD_BYTES (for now) */131#define SN_AUX_MAX_PAYLOAD_BYTES 16132133#define SN_REGULATOR_SUPPLY_NUM 4134135#define SN_MAX_DP_LANES 4136#define SN_NUM_GPIOS 4137#define SN_GPIO_PHYSICAL_OFFSET 1138139#define SN_LINK_TRAINING_TRIES 10140141#define SN_PWM_GPIO_IDX 3 /* 4th GPIO */142143/**144* struct ti_sn65dsi86 - Platform data for ti-sn65dsi86 driver.145* @bridge_aux: AUX-bus sub device for MIPI-to-eDP bridge functionality.146* @gpio_aux: AUX-bus sub device for GPIO controller functionality.147* @aux_aux: AUX-bus sub device for eDP AUX channel functionality.148* @pwm_aux: AUX-bus sub device for PWM controller functionality.149*150* @dev: Pointer to the top level (i2c) device.151* @regmap: Regmap for accessing i2c.152* @aux: Our aux channel.153* @bridge: Our bridge.154* @connector: Our connector.155* @host_node: Remote DSI node.156* @dsi: Our MIPI DSI source.157* @refclk: Our reference clock.158* @next_bridge: The bridge on the eDP side.159* @enable_gpio: The GPIO we toggle to enable the bridge.160* @supplies: Data for bulk enabling/disabling our regulators.161* @dp_lanes: Count of dp_lanes we're using.162* @ln_assign: Value to program to the LN_ASSIGN register.163* @ln_polrs: Value for the 4-bit LN_POLRS field of SN_ENH_FRAME_REG.164* @comms_enabled: If true then communication over the aux channel is enabled.165* @hpd_enabled: If true then HPD events are enabled.166* @comms_mutex: Protects modification of comms_enabled.167* @hpd_mutex: Protects modification of hpd_enabled.168*169* @gchip: If we expose our GPIOs, this is used.170* @gchip_output: A cache of whether we've set GPIOs to output. This171* serves double-duty of keeping track of the direction and172* also keeping track of whether we've incremented the173* pm_runtime reference count for this pin, which we do174* whenever a pin is configured as an output. This is a175* bitmap so we can do atomic ops on it without an extra176* lock so concurrent users of our 4 GPIOs don't stomp on177* each other's read-modify-write.178*179* @pchip: pwm_chip if the PWM is exposed.180* @pwm_enabled: Used to track if the PWM signal is currently enabled.181* @pwm_pin_busy: Track if GPIO4 is currently requested for GPIO or PWM.182* @pwm_refclk_freq: Cache for the reference clock input to the PWM.183*/184struct ti_sn65dsi86 {185struct auxiliary_device *bridge_aux;186struct auxiliary_device *gpio_aux;187struct auxiliary_device *aux_aux;188struct auxiliary_device *pwm_aux;189190struct device *dev;191struct regmap *regmap;192struct drm_dp_aux aux;193struct drm_bridge bridge;194struct drm_connector *connector;195struct device_node *host_node;196struct mipi_dsi_device *dsi;197struct clk *refclk;198struct drm_bridge *next_bridge;199struct gpio_desc *enable_gpio;200struct regulator_bulk_data supplies[SN_REGULATOR_SUPPLY_NUM];201int dp_lanes;202u8 ln_assign;203u8 ln_polrs;204bool comms_enabled;205bool hpd_enabled;206struct mutex comms_mutex;207struct mutex hpd_mutex;208209#if defined(CONFIG_OF_GPIO)210struct gpio_chip gchip;211DECLARE_BITMAP(gchip_output, SN_NUM_GPIOS);212#endif213#if IS_REACHABLE(CONFIG_PWM)214struct pwm_chip *pchip;215bool pwm_enabled;216atomic_t pwm_pin_busy;217#endif218unsigned int pwm_refclk_freq;219};220221static const struct regmap_range ti_sn65dsi86_volatile_ranges[] = {222{ .range_min = 0, .range_max = 0xFF },223};224225static const struct regmap_access_table ti_sn_bridge_volatile_table = {226.yes_ranges = ti_sn65dsi86_volatile_ranges,227.n_yes_ranges = ARRAY_SIZE(ti_sn65dsi86_volatile_ranges),228};229230static const struct regmap_config ti_sn65dsi86_regmap_config = {231.reg_bits = 8,232.val_bits = 8,233.volatile_table = &ti_sn_bridge_volatile_table,234.cache_type = REGCACHE_NONE,235.max_register = 0xFF,236};237238static int ti_sn65dsi86_read_u8(struct ti_sn65dsi86 *pdata, unsigned int reg,239u8 *val)240{241int ret;242unsigned int reg_val;243244ret = regmap_read(pdata->regmap, reg, ®_val);245if (ret) {246dev_err(pdata->dev, "fail to read raw reg %#x: %d\n",247reg, ret);248return ret;249}250*val = (u8)reg_val;251252return 0;253}254255static int __maybe_unused ti_sn65dsi86_read_u16(struct ti_sn65dsi86 *pdata,256unsigned int reg, u16 *val)257{258u8 buf[2];259int ret;260261ret = regmap_bulk_read(pdata->regmap, reg, buf, ARRAY_SIZE(buf));262if (ret)263return ret;264265*val = buf[0] | (buf[1] << 8);266267return 0;268}269270static void ti_sn65dsi86_write_u16(struct ti_sn65dsi86 *pdata,271unsigned int reg, u16 val)272{273u8 buf[2] = { val & 0xff, val >> 8 };274275regmap_bulk_write(pdata->regmap, reg, buf, ARRAY_SIZE(buf));276}277278static struct drm_display_mode *279get_new_adjusted_display_mode(struct drm_bridge *bridge,280struct drm_atomic_state *state)281{282struct drm_connector *connector =283drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);284struct drm_connector_state *conn_state =285drm_atomic_get_new_connector_state(state, connector);286struct drm_crtc_state *crtc_state =287drm_atomic_get_new_crtc_state(state, conn_state->crtc);288289return &crtc_state->adjusted_mode;290}291292static u32 ti_sn_bridge_get_dsi_freq(struct ti_sn65dsi86 *pdata,293struct drm_atomic_state *state)294{295u32 bit_rate_khz, clk_freq_khz;296struct drm_display_mode *mode =297get_new_adjusted_display_mode(&pdata->bridge, state);298299bit_rate_khz = mode->clock *300mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);301clk_freq_khz = bit_rate_khz / (pdata->dsi->lanes * 2);302303return clk_freq_khz;304}305306/* clk frequencies supported by bridge in Hz in case derived from REFCLK pin */307static const u32 ti_sn_bridge_refclk_lut[] = {30812000000,30919200000,31026000000,31127000000,31238400000,313};314315/* clk frequencies supported by bridge in Hz in case derived from DACP/N pin */316static const u32 ti_sn_bridge_dsiclk_lut[] = {317468000000,318384000000,319416000000,320486000000,321460800000,322};323324static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata,325struct drm_atomic_state *state)326{327int i;328u32 refclk_rate;329const u32 *refclk_lut;330size_t refclk_lut_size;331332if (pdata->refclk) {333refclk_rate = clk_get_rate(pdata->refclk);334refclk_lut = ti_sn_bridge_refclk_lut;335refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_refclk_lut);336clk_prepare_enable(pdata->refclk);337} else {338refclk_rate = ti_sn_bridge_get_dsi_freq(pdata, state) * 1000;339refclk_lut = ti_sn_bridge_dsiclk_lut;340refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_dsiclk_lut);341}342343/* for i equals to refclk_lut_size means default frequency */344for (i = 0; i < refclk_lut_size; i++)345if (refclk_lut[i] == refclk_rate)346break;347348/* avoid buffer overflow and "1" is the default rate in the datasheet. */349if (i >= refclk_lut_size)350i = 1;351352regmap_update_bits(pdata->regmap, SN_DPPLL_SRC_REG, REFCLK_FREQ_MASK,353REFCLK_FREQ(i));354355/*356* The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,357* regardless of its actual sourcing.358*/359pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];360}361362static void ti_sn65dsi86_enable_comms(struct ti_sn65dsi86 *pdata,363struct drm_atomic_state *state)364{365mutex_lock(&pdata->comms_mutex);366367/* configure bridge ref_clk */368ti_sn_bridge_set_refclk_freq(pdata, state);369370/*371* HPD on this bridge chip is a bit useless. This is an eDP bridge372* so the HPD is an internal signal that's only there to signal that373* the panel is done powering up. ...but the bridge chip debounces374* this signal by between 100 ms and 400 ms (depending on process,375* voltage, and temperate--I measured it at about 200 ms). One376* particular panel asserted HPD 84 ms after it was powered on meaning377* that we saw HPD 284 ms after power on. ...but the same panel said378* that instead of looking at HPD you could just hardcode a delay of379* 200 ms. We'll assume that the panel driver will have the hardcoded380* delay in its prepare and always disable HPD.381*382* For DisplayPort bridge type, we need HPD. So we use the bridge type383* to conditionally disable HPD.384* NOTE: The bridge type is set in ti_sn_bridge_probe() but enable_comms()385* can be called before. So for DisplayPort, HPD will be enabled once386* bridge type is set. We are using bridge type instead of "no-hpd"387* property because it is not used properly in devicetree description388* and hence is unreliable.389*/390391if (pdata->bridge.type != DRM_MODE_CONNECTOR_DisplayPort)392regmap_update_bits(pdata->regmap, SN_HPD_DISABLE_REG, HPD_DISABLE,393HPD_DISABLE);394395pdata->comms_enabled = true;396397mutex_unlock(&pdata->comms_mutex);398}399400static void ti_sn65dsi86_disable_comms(struct ti_sn65dsi86 *pdata)401{402mutex_lock(&pdata->comms_mutex);403404pdata->comms_enabled = false;405clk_disable_unprepare(pdata->refclk);406407mutex_unlock(&pdata->comms_mutex);408}409410static int __maybe_unused ti_sn65dsi86_resume(struct device *dev)411{412struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);413const struct i2c_client *client = to_i2c_client(pdata->dev);414int ret;415416ret = regulator_bulk_enable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);417if (ret) {418DRM_ERROR("failed to enable supplies %d\n", ret);419return ret;420}421422/* td2: min 100 us after regulators before enabling the GPIO */423usleep_range(100, 110);424425gpiod_set_value_cansleep(pdata->enable_gpio, 1);426427/*428* After EN is deasserted and an external clock is detected, the bridge429* will sample GPIO3:1 to determine its frequency. The driver will430* overwrite this setting in ti_sn_bridge_set_refclk_freq(). But this is431* racy. Thus we have to wait a couple of us. According to the datasheet432* the GPIO lines has to be stable at least 5 us (td5) but it seems that433* is not enough and the refclk frequency value is still lost or434* overwritten by the bridge itself. Waiting for 20us seems to work.435*/436usleep_range(20, 30);437438/*439* If we have a reference clock we can enable communication w/ the440* panel (including the aux channel) w/out any need for an input clock441* so we can do it in resume which lets us read the EDID before442* pre_enable(). Without a reference clock we need the MIPI reference443* clock so reading early doesn't work.444*/445if (pdata->refclk)446ti_sn65dsi86_enable_comms(pdata, NULL);447448if (client->irq) {449ret = regmap_update_bits(pdata->regmap, SN_IRQ_EN_REG, IRQ_EN,450IRQ_EN);451if (ret)452dev_err(pdata->dev, "Failed to enable IRQ events: %d\n", ret);453}454455return ret;456}457458static int __maybe_unused ti_sn65dsi86_suspend(struct device *dev)459{460struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);461int ret;462463if (pdata->refclk)464ti_sn65dsi86_disable_comms(pdata);465466gpiod_set_value_cansleep(pdata->enable_gpio, 0);467468ret = regulator_bulk_disable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);469if (ret)470DRM_ERROR("failed to disable supplies %d\n", ret);471472return ret;473}474475static const struct dev_pm_ops ti_sn65dsi86_pm_ops = {476SET_RUNTIME_PM_OPS(ti_sn65dsi86_suspend, ti_sn65dsi86_resume, NULL)477SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,478pm_runtime_force_resume)479};480481static int status_show(struct seq_file *s, void *data)482{483struct ti_sn65dsi86 *pdata = s->private;484unsigned int reg, val;485486seq_puts(s, "STATUS REGISTERS:\n");487488pm_runtime_get_sync(pdata->dev);489490/* IRQ Status Registers, see Table 31 in datasheet */491for (reg = 0xf0; reg <= 0xf8; reg++) {492regmap_read(pdata->regmap, reg, &val);493seq_printf(s, "[0x%02x] = 0x%08x\n", reg, val);494}495496pm_runtime_put_autosuspend(pdata->dev);497498return 0;499}500DEFINE_SHOW_ATTRIBUTE(status);501502/* -----------------------------------------------------------------------------503* Auxiliary Devices (*not* AUX)504*/505506static int ti_sn65dsi86_add_aux_device(struct ti_sn65dsi86 *pdata,507struct auxiliary_device **aux_out,508const char *name)509{510struct device *dev = pdata->dev;511const struct i2c_client *client = to_i2c_client(dev);512struct auxiliary_device *aux;513int id;514515id = (client->adapter->nr << 10) | client->addr;516aux = __devm_auxiliary_device_create(dev, KBUILD_MODNAME, name,517NULL, id);518if (!aux)519return -ENODEV;520521*aux_out = aux;522return 0;523}524525/* -----------------------------------------------------------------------------526* AUX Adapter527*/528529static struct ti_sn65dsi86 *aux_to_ti_sn65dsi86(struct drm_dp_aux *aux)530{531return container_of(aux, struct ti_sn65dsi86, aux);532}533534static ssize_t ti_sn_aux_transfer(struct drm_dp_aux *aux,535struct drm_dp_aux_msg *msg)536{537struct ti_sn65dsi86 *pdata = aux_to_ti_sn65dsi86(aux);538u32 request = msg->request & ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE);539u32 request_val = AUX_CMD_REQ(msg->request);540u8 *buf = msg->buffer;541unsigned int len = msg->size;542unsigned int short_len;543unsigned int val;544int ret;545u8 addr_len[SN_AUX_LENGTH_REG + 1 - SN_AUX_ADDR_19_16_REG];546547if (len > SN_AUX_MAX_PAYLOAD_BYTES)548return -EINVAL;549550pm_runtime_get_sync(pdata->dev);551mutex_lock(&pdata->comms_mutex);552553/*554* If someone tries to do a DDC over AUX transaction before pre_enable()555* on a device without a dedicated reference clock then we just can't556* do it. Fail right away. This prevents non-refclk users from reading557* the EDID before enabling the panel but such is life.558*/559if (!pdata->comms_enabled) {560ret = -EIO;561goto exit;562}563564switch (request) {565case DP_AUX_NATIVE_WRITE:566case DP_AUX_I2C_WRITE:567case DP_AUX_NATIVE_READ:568case DP_AUX_I2C_READ:569regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val);570/* Assume it's good */571msg->reply = 0;572break;573default:574ret = -EINVAL;575goto exit;576}577578BUILD_BUG_ON(sizeof(addr_len) != sizeof(__be32));579put_unaligned_be32((msg->address & SN_AUX_ADDR_MASK) << 8 | len,580addr_len);581regmap_bulk_write(pdata->regmap, SN_AUX_ADDR_19_16_REG, addr_len,582ARRAY_SIZE(addr_len));583584if (request == DP_AUX_NATIVE_WRITE || request == DP_AUX_I2C_WRITE)585regmap_bulk_write(pdata->regmap, SN_AUX_WDATA_REG(0), buf, len);586587/* Clear old status bits before start so we don't get confused */588regmap_write(pdata->regmap, SN_AUX_CMD_STATUS_REG,589AUX_IRQ_STATUS_NAT_I2C_FAIL |590AUX_IRQ_STATUS_AUX_RPLY_TOUT |591AUX_IRQ_STATUS_AUX_SHORT);592593regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val | AUX_CMD_SEND);594595/* Zero delay loop because i2c transactions are slow already */596ret = regmap_read_poll_timeout(pdata->regmap, SN_AUX_CMD_REG, val,597!(val & AUX_CMD_SEND), 0, 50 * 1000);598if (ret)599goto exit;600601ret = regmap_read(pdata->regmap, SN_AUX_CMD_STATUS_REG, &val);602if (ret)603goto exit;604605if (val & AUX_IRQ_STATUS_AUX_RPLY_TOUT) {606/*607* The hardware tried the message seven times per the DP spec608* but it hit a timeout. We ignore defers here because they're609* handled in hardware.610*/611ret = -ETIMEDOUT;612goto exit;613}614615if (val & AUX_IRQ_STATUS_AUX_SHORT) {616ret = regmap_read(pdata->regmap, SN_AUX_LENGTH_REG, &short_len);617len = min(len, short_len);618if (ret)619goto exit;620} else if (val & AUX_IRQ_STATUS_NAT_I2C_FAIL) {621switch (request) {622case DP_AUX_I2C_WRITE:623case DP_AUX_I2C_READ:624msg->reply |= DP_AUX_I2C_REPLY_NACK;625break;626case DP_AUX_NATIVE_READ:627case DP_AUX_NATIVE_WRITE:628msg->reply |= DP_AUX_NATIVE_REPLY_NACK;629break;630}631len = 0;632goto exit;633}634635if (request != DP_AUX_NATIVE_WRITE && request != DP_AUX_I2C_WRITE && len != 0)636ret = regmap_bulk_read(pdata->regmap, SN_AUX_RDATA_REG(0), buf, len);637638exit:639mutex_unlock(&pdata->comms_mutex);640pm_runtime_mark_last_busy(pdata->dev);641pm_runtime_put_autosuspend(pdata->dev);642643if (ret)644return ret;645return len;646}647648static int ti_sn_aux_wait_hpd_asserted(struct drm_dp_aux *aux, unsigned long wait_us)649{650/*651* The HPD in this chip is a bit useless (See comment in652* ti_sn65dsi86_enable_comms) so if our driver is expected to wait653* for HPD, we just assume it's asserted after the wait_us delay.654*655* In case we are asked to wait forever (wait_us=0) take conservative656* 500ms delay.657*/658if (wait_us == 0)659wait_us = 500000;660661usleep_range(wait_us, wait_us + 1000);662663return 0;664}665666static int ti_sn_aux_probe(struct auxiliary_device *adev,667const struct auxiliary_device_id *id)668{669struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);670int ret;671672pdata->aux.name = "ti-sn65dsi86-aux";673pdata->aux.dev = &adev->dev;674pdata->aux.transfer = ti_sn_aux_transfer;675pdata->aux.wait_hpd_asserted = ti_sn_aux_wait_hpd_asserted;676drm_dp_aux_init(&pdata->aux);677678ret = devm_of_dp_aux_populate_ep_devices(&pdata->aux);679if (ret)680return ret;681682/*683* The eDP to MIPI bridge parts don't work until the AUX channel is684* setup so we don't add it in the main driver probe, we add it now.685*/686return ti_sn65dsi86_add_aux_device(pdata, &pdata->bridge_aux, "bridge");687}688689static const struct auxiliary_device_id ti_sn_aux_id_table[] = {690{ .name = "ti_sn65dsi86.aux", },691{},692};693694static struct auxiliary_driver ti_sn_aux_driver = {695.name = "aux",696.probe = ti_sn_aux_probe,697.id_table = ti_sn_aux_id_table,698};699700/*------------------------------------------------------------------------------701* DRM Bridge702*/703704static struct ti_sn65dsi86 *bridge_to_ti_sn65dsi86(struct drm_bridge *bridge)705{706return container_of(bridge, struct ti_sn65dsi86, bridge);707}708709static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86 *pdata)710{711int val;712struct mipi_dsi_host *host;713struct mipi_dsi_device *dsi;714struct device *dev = pdata->dev;715const struct mipi_dsi_device_info info = { .type = "ti_sn_bridge",716.channel = 0,717.node = NULL,718};719720host = of_find_mipi_dsi_host_by_node(pdata->host_node);721if (!host)722return -EPROBE_DEFER;723724dsi = devm_mipi_dsi_device_register_full(&adev->dev, host, &info);725if (IS_ERR(dsi))726return PTR_ERR(dsi);727728/* TODO: setting to 4 MIPI lanes always for now */729dsi->lanes = 4;730dsi->format = MIPI_DSI_FMT_RGB888;731dsi->mode_flags = MIPI_DSI_MODE_VIDEO;732733/* check if continuous dsi clock is required or not */734pm_runtime_get_sync(dev);735regmap_read(pdata->regmap, SN_DPPLL_SRC_REG, &val);736pm_runtime_put_autosuspend(dev);737if (!(val & DPPLL_CLK_SRC_DSICLK))738dsi->mode_flags |= MIPI_DSI_CLOCK_NON_CONTINUOUS;739740pdata->dsi = dsi;741742return devm_mipi_dsi_attach(&adev->dev, dsi);743}744745static int ti_sn_bridge_attach(struct drm_bridge *bridge,746struct drm_encoder *encoder,747enum drm_bridge_attach_flags flags)748{749struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);750int ret;751752pdata->aux.drm_dev = bridge->dev;753ret = drm_dp_aux_register(&pdata->aux);754if (ret < 0) {755drm_err(bridge->dev, "Failed to register DP AUX channel: %d\n", ret);756return ret;757}758759/*760* Attach the next bridge.761* We never want the next bridge to *also* create a connector.762*/763ret = drm_bridge_attach(encoder, pdata->next_bridge,764&pdata->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);765if (ret < 0)766goto err_initted_aux;767768if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)769return 0;770771pdata->connector = drm_bridge_connector_init(pdata->bridge.dev,772pdata->bridge.encoder);773if (IS_ERR(pdata->connector)) {774ret = PTR_ERR(pdata->connector);775goto err_initted_aux;776}777778drm_connector_attach_encoder(pdata->connector, pdata->bridge.encoder);779780return 0;781782err_initted_aux:783drm_dp_aux_unregister(&pdata->aux);784return ret;785}786787static void ti_sn_bridge_detach(struct drm_bridge *bridge)788{789drm_dp_aux_unregister(&bridge_to_ti_sn65dsi86(bridge)->aux);790}791792static enum drm_mode_status793ti_sn_bridge_mode_valid(struct drm_bridge *bridge,794const struct drm_display_info *info,795const struct drm_display_mode *mode)796{797/* maximum supported resolution is 4K at 60 fps */798if (mode->clock > 594000)799return MODE_CLOCK_HIGH;800801/*802* The front and back porch registers are 8 bits, and pulse width803* registers are 15 bits, so reject any modes with larger periods.804*/805806if ((mode->hsync_start - mode->hdisplay) > 0xff)807return MODE_HBLANK_WIDE;808809if ((mode->vsync_start - mode->vdisplay) > 0xff)810return MODE_VBLANK_WIDE;811812if ((mode->hsync_end - mode->hsync_start) > 0x7fff)813return MODE_HSYNC_WIDE;814815if ((mode->vsync_end - mode->vsync_start) > 0x7fff)816return MODE_VSYNC_WIDE;817818if ((mode->htotal - mode->hsync_end) > 0xff)819return MODE_HBLANK_WIDE;820821if ((mode->vtotal - mode->vsync_end) > 0xff)822return MODE_VBLANK_WIDE;823824return MODE_OK;825}826827static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge,828struct drm_atomic_state *state)829{830struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);831832/* disable video stream */833regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);834}835836static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata,837struct drm_atomic_state *state)838{839unsigned int bit_rate_mhz, clk_freq_mhz;840unsigned int val;841struct drm_display_mode *mode =842get_new_adjusted_display_mode(&pdata->bridge, state);843844/* set DSIA clk frequency */845bit_rate_mhz = (mode->clock / 1000) *846mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);847clk_freq_mhz = bit_rate_mhz / (pdata->dsi->lanes * 2);848849/* for each increment in val, frequency increases by 5MHz */850val = (MIN_DSI_CLK_FREQ_MHZ / 5) +851(((clk_freq_mhz - MIN_DSI_CLK_FREQ_MHZ) / 5) & 0xFF);852regmap_write(pdata->regmap, SN_DSIA_CLK_FREQ_REG, val);853}854855static unsigned int ti_sn_bridge_get_bpp(struct drm_connector *connector)856{857if (connector->display_info.bpc <= 6)858return 18;859else860return 24;861}862863/*864* LUT index corresponds to register value and865* LUT values corresponds to dp data rate supported866* by the bridge in Mbps unit.867*/868static const unsigned int ti_sn_bridge_dp_rate_lut[] = {8690, 1620, 2160, 2430, 2700, 3240, 4320, 5400870};871872static int ti_sn_bridge_calc_min_dp_rate_idx(struct ti_sn65dsi86 *pdata,873struct drm_atomic_state *state,874unsigned int bpp)875{876unsigned int bit_rate_khz, dp_rate_mhz;877unsigned int i;878struct drm_display_mode *mode =879get_new_adjusted_display_mode(&pdata->bridge, state);880881/* Calculate minimum bit rate based on our pixel clock. */882bit_rate_khz = mode->clock * bpp;883884/* Calculate minimum DP data rate, taking 80% as per DP spec */885dp_rate_mhz = DIV_ROUND_UP(bit_rate_khz * DP_CLK_FUDGE_NUM,8861000 * pdata->dp_lanes * DP_CLK_FUDGE_DEN);887888for (i = 1; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut) - 1; i++)889if (ti_sn_bridge_dp_rate_lut[i] >= dp_rate_mhz)890break;891892return i;893}894895static unsigned int ti_sn_bridge_read_valid_rates(struct ti_sn65dsi86 *pdata)896{897unsigned int valid_rates = 0;898unsigned int rate_per_200khz;899unsigned int rate_mhz;900u8 dpcd_val;901int ret;902int i, j;903904ret = drm_dp_dpcd_readb(&pdata->aux, DP_EDP_DPCD_REV, &dpcd_val);905if (ret != 1) {906DRM_DEV_ERROR(pdata->dev,907"Can't read eDP rev (%d), assuming 1.1\n", ret);908dpcd_val = DP_EDP_11;909}910911if (dpcd_val >= DP_EDP_14) {912/* eDP 1.4 devices must provide a custom table */913__le16 sink_rates[DP_MAX_SUPPORTED_RATES];914915ret = drm_dp_dpcd_read(&pdata->aux, DP_SUPPORTED_LINK_RATES,916sink_rates, sizeof(sink_rates));917918if (ret != sizeof(sink_rates)) {919DRM_DEV_ERROR(pdata->dev,920"Can't read supported rate table (%d)\n", ret);921922/* By zeroing we'll fall back to DP_MAX_LINK_RATE. */923memset(sink_rates, 0, sizeof(sink_rates));924}925926for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {927rate_per_200khz = le16_to_cpu(sink_rates[i]);928929if (!rate_per_200khz)930break;931932rate_mhz = rate_per_200khz * 200 / 1000;933for (j = 0;934j < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);935j++) {936if (ti_sn_bridge_dp_rate_lut[j] == rate_mhz)937valid_rates |= BIT(j);938}939}940941for (i = 0; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut); i++) {942if (valid_rates & BIT(i))943return valid_rates;944}945DRM_DEV_ERROR(pdata->dev,946"No matching eDP rates in table; falling back\n");947}948949/* On older versions best we can do is use DP_MAX_LINK_RATE */950ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LINK_RATE, &dpcd_val);951if (ret != 1) {952DRM_DEV_ERROR(pdata->dev,953"Can't read max rate (%d); assuming 5.4 GHz\n",954ret);955dpcd_val = DP_LINK_BW_5_4;956}957958switch (dpcd_val) {959default:960DRM_DEV_ERROR(pdata->dev,961"Unexpected max rate (%#x); assuming 5.4 GHz\n",962(int)dpcd_val);963fallthrough;964case DP_LINK_BW_5_4:965valid_rates |= BIT(7);966fallthrough;967case DP_LINK_BW_2_7:968valid_rates |= BIT(4);969fallthrough;970case DP_LINK_BW_1_62:971valid_rates |= BIT(1);972break;973}974975return valid_rates;976}977978static void ti_sn_bridge_set_video_timings(struct ti_sn65dsi86 *pdata,979struct drm_atomic_state *state)980{981struct drm_display_mode *mode =982get_new_adjusted_display_mode(&pdata->bridge, state);983u8 hsync_polarity = 0, vsync_polarity = 0;984985if (mode->flags & DRM_MODE_FLAG_NHSYNC)986hsync_polarity = CHA_HSYNC_POLARITY;987if (mode->flags & DRM_MODE_FLAG_NVSYNC)988vsync_polarity = CHA_VSYNC_POLARITY;989990ti_sn65dsi86_write_u16(pdata, SN_CHA_ACTIVE_LINE_LENGTH_LOW_REG,991mode->hdisplay);992ti_sn65dsi86_write_u16(pdata, SN_CHA_VERTICAL_DISPLAY_SIZE_LOW_REG,993mode->vdisplay);994regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_LOW_REG,995(mode->hsync_end - mode->hsync_start) & 0xFF);996regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_HIGH_REG,997(((mode->hsync_end - mode->hsync_start) >> 8) & 0x7F) |998hsync_polarity);999regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_LOW_REG,1000(mode->vsync_end - mode->vsync_start) & 0xFF);1001regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_HIGH_REG,1002(((mode->vsync_end - mode->vsync_start) >> 8) & 0x7F) |1003vsync_polarity);10041005regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_BACK_PORCH_REG,1006(mode->htotal - mode->hsync_end) & 0xFF);1007regmap_write(pdata->regmap, SN_CHA_VERTICAL_BACK_PORCH_REG,1008(mode->vtotal - mode->vsync_end) & 0xFF);10091010regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_FRONT_PORCH_REG,1011(mode->hsync_start - mode->hdisplay) & 0xFF);1012regmap_write(pdata->regmap, SN_CHA_VERTICAL_FRONT_PORCH_REG,1013(mode->vsync_start - mode->vdisplay) & 0xFF);10141015usleep_range(10000, 10500); /* 10ms delay recommended by spec */1016}10171018static unsigned int ti_sn_get_max_lanes(struct ti_sn65dsi86 *pdata)1019{1020u8 data;1021int ret;10221023ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LANE_COUNT, &data);1024if (ret != 1) {1025DRM_DEV_ERROR(pdata->dev,1026"Can't read lane count (%d); assuming 4\n", ret);1027return 4;1028}10291030return data & DP_LANE_COUNT_MASK;1031}10321033static int ti_sn_link_training(struct ti_sn65dsi86 *pdata, int dp_rate_idx,1034const char **last_err_str)1035{1036unsigned int val;1037int ret;1038int i;10391040/* set dp clk frequency value */1041regmap_update_bits(pdata->regmap, SN_DATARATE_CONFIG_REG,1042DP_DATARATE_MASK, DP_DATARATE(dp_rate_idx));10431044/* enable DP PLL */1045regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 1);10461047ret = regmap_read_poll_timeout(pdata->regmap, SN_DPPLL_SRC_REG, val,1048val & DPPLL_SRC_DP_PLL_LOCK, 1000,104950 * 1000);1050if (ret) {1051*last_err_str = "DP_PLL_LOCK polling failed";1052goto exit;1053}10541055/*1056* We'll try to link train several times. As part of link training1057* the bridge chip will write DP_SET_POWER_D0 to DP_SET_POWER. If1058* the panel isn't ready quite it might respond NAK here which means1059* we need to try again.1060*/1061for (i = 0; i < SN_LINK_TRAINING_TRIES; i++) {1062/* Semi auto link training mode */1063regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0x0A);1064ret = regmap_read_poll_timeout(pdata->regmap, SN_ML_TX_MODE_REG, val,1065val == ML_TX_MAIN_LINK_OFF ||1066val == ML_TX_NORMAL_MODE, 1000,1067500 * 1000);1068if (ret) {1069*last_err_str = "Training complete polling failed";1070} else if (val == ML_TX_MAIN_LINK_OFF) {1071*last_err_str = "Link training failed, link is off";1072ret = -EIO;1073continue;1074}10751076break;1077}10781079/* If we saw quite a few retries, add a note about it */1080if (!ret && i > SN_LINK_TRAINING_TRIES / 2)1081DRM_DEV_INFO(pdata->dev, "Link training needed %d retries\n", i);10821083exit:1084/* Disable the PLL if we failed */1085if (ret)1086regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);10871088return ret;1089}10901091static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,1092struct drm_atomic_state *state)1093{1094struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1095struct drm_connector *connector;1096const char *last_err_str = "No supported DP rate";1097unsigned int valid_rates;1098int dp_rate_idx;1099unsigned int val;1100int ret = -EINVAL;1101int max_dp_lanes;1102unsigned int bpp;11031104connector = drm_atomic_get_new_connector_for_encoder(state,1105bridge->encoder);1106if (!connector) {1107dev_err_ratelimited(pdata->dev, "Could not get the connector\n");1108return;1109}11101111max_dp_lanes = ti_sn_get_max_lanes(pdata);1112pdata->dp_lanes = min(pdata->dp_lanes, max_dp_lanes);11131114/* DSI_A lane config */1115val = CHA_DSI_LANES(SN_MAX_DP_LANES - pdata->dsi->lanes);1116regmap_update_bits(pdata->regmap, SN_DSI_LANES_REG,1117CHA_DSI_LANES_MASK, val);11181119regmap_write(pdata->regmap, SN_LN_ASSIGN_REG, pdata->ln_assign);1120regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, LN_POLRS_MASK,1121pdata->ln_polrs << LN_POLRS_OFFSET);11221123/* set dsi clk frequency value */1124ti_sn_bridge_set_dsi_rate(pdata, state);11251126/*1127* The SN65DSI86 only supports ASSR Display Authentication method and1128* this method is enabled for eDP panels. An eDP panel must support this1129* authentication method. We need to enable this method in the eDP panel1130* at DisplayPort address 0x0010A prior to link training.1131*1132* As only ASSR is supported by SN65DSI86, for full DisplayPort displays1133* we need to disable the scrambler.1134*/1135if (pdata->bridge.type == DRM_MODE_CONNECTOR_eDP) {1136drm_dp_dpcd_writeb(&pdata->aux, DP_EDP_CONFIGURATION_SET,1137DP_ALTERNATE_SCRAMBLER_RESET_ENABLE);11381139regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,1140SCRAMBLE_DISABLE, 0);1141} else {1142regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,1143SCRAMBLE_DISABLE, SCRAMBLE_DISABLE);1144}11451146bpp = ti_sn_bridge_get_bpp(connector);1147/* Set the DP output format (18 bpp or 24 bpp) */1148val = bpp == 18 ? BPP_18_RGB : 0;1149regmap_update_bits(pdata->regmap, SN_DATA_FORMAT_REG, BPP_18_RGB, val);11501151/* DP lane config */1152val = DP_NUM_LANES(min(pdata->dp_lanes, 3));1153regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK,1154val);11551156valid_rates = ti_sn_bridge_read_valid_rates(pdata);11571158/* Train until we run out of rates */1159for (dp_rate_idx = ti_sn_bridge_calc_min_dp_rate_idx(pdata, state, bpp);1160dp_rate_idx < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);1161dp_rate_idx++) {1162if (!(valid_rates & BIT(dp_rate_idx)))1163continue;11641165ret = ti_sn_link_training(pdata, dp_rate_idx, &last_err_str);1166if (!ret)1167break;1168}1169if (ret) {1170DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);1171return;1172}11731174/* config video parameters */1175ti_sn_bridge_set_video_timings(pdata, state);11761177/* enable video stream */1178regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,1179VSTREAM_ENABLE);1180}11811182static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,1183struct drm_atomic_state *state)1184{1185struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);11861187pm_runtime_get_sync(pdata->dev);11881189if (!pdata->refclk)1190ti_sn65dsi86_enable_comms(pdata, state);11911192/* td7: min 100 us after enable before DSI data */1193usleep_range(100, 110);1194}11951196static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,1197struct drm_atomic_state *state)1198{1199struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);12001201/* semi auto link training mode OFF */1202regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);1203/* Num lanes to 0 as per power sequencing in data sheet */1204regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK, 0);1205/* disable DP PLL */1206regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);12071208if (!pdata->refclk)1209ti_sn65dsi86_disable_comms(pdata);12101211pm_runtime_put_sync(pdata->dev);1212}12131214static enum drm_connector_status1215ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)1216{1217struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1218int val = 0;12191220/*1221* Runtime reference is grabbed in ti_sn_bridge_hpd_enable()1222* as the chip won't report HPD just after being powered on.1223* HPD_DEBOUNCED_STATE reflects correct state only after the1224* debounce time (~100-400 ms).1225*/12261227regmap_read(pdata->regmap, SN_HPD_DISABLE_REG, &val);12281229return val & HPD_DEBOUNCED_STATE ? connector_status_connected1230: connector_status_disconnected;1231}12321233static const struct drm_edid *ti_sn_bridge_edid_read(struct drm_bridge *bridge,1234struct drm_connector *connector)1235{1236struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);12371238return drm_edid_read_ddc(connector, &pdata->aux.ddc);1239}12401241static void ti_sn65dsi86_debugfs_init(struct drm_bridge *bridge, struct dentry *root)1242{1243struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1244struct dentry *debugfs;12451246debugfs = debugfs_create_dir(dev_name(pdata->dev), root);1247debugfs_create_file("status", 0600, debugfs, pdata, &status_fops);1248}12491250static void ti_sn_bridge_hpd_enable(struct drm_bridge *bridge)1251{1252struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1253const struct i2c_client *client = to_i2c_client(pdata->dev);1254int ret;12551256/*1257* Device needs to be powered on before reading the HPD state1258* for reliable hpd detection in ti_sn_bridge_detect() due to1259* the high debounce time.1260*/12611262pm_runtime_get_sync(pdata->dev);12631264mutex_lock(&pdata->hpd_mutex);1265pdata->hpd_enabled = true;1266mutex_unlock(&pdata->hpd_mutex);12671268if (client->irq) {1269ret = regmap_set_bits(pdata->regmap, SN_IRQ_EVENTS_EN_REG,1270HPD_REMOVAL_EN | HPD_INSERTION_EN);1271if (ret)1272dev_err(pdata->dev, "Failed to enable HPD events: %d\n", ret);1273}1274}12751276static void ti_sn_bridge_hpd_disable(struct drm_bridge *bridge)1277{1278struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1279const struct i2c_client *client = to_i2c_client(pdata->dev);1280int ret;12811282if (client->irq) {1283ret = regmap_clear_bits(pdata->regmap, SN_IRQ_EVENTS_EN_REG,1284HPD_REMOVAL_EN | HPD_INSERTION_EN);1285if (ret)1286dev_err(pdata->dev, "Failed to disable HPD events: %d\n", ret);1287}12881289mutex_lock(&pdata->hpd_mutex);1290pdata->hpd_enabled = false;1291mutex_unlock(&pdata->hpd_mutex);12921293pm_runtime_put_autosuspend(pdata->dev);1294}12951296static const struct drm_bridge_funcs ti_sn_bridge_funcs = {1297.attach = ti_sn_bridge_attach,1298.detach = ti_sn_bridge_detach,1299.mode_valid = ti_sn_bridge_mode_valid,1300.edid_read = ti_sn_bridge_edid_read,1301.detect = ti_sn_bridge_detect,1302.atomic_pre_enable = ti_sn_bridge_atomic_pre_enable,1303.atomic_enable = ti_sn_bridge_atomic_enable,1304.atomic_disable = ti_sn_bridge_atomic_disable,1305.atomic_post_disable = ti_sn_bridge_atomic_post_disable,1306.atomic_reset = drm_atomic_helper_bridge_reset,1307.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,1308.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,1309.debugfs_init = ti_sn65dsi86_debugfs_init,1310.hpd_enable = ti_sn_bridge_hpd_enable,1311.hpd_disable = ti_sn_bridge_hpd_disable,1312};13131314static void ti_sn_bridge_parse_lanes(struct ti_sn65dsi86 *pdata,1315struct device_node *np)1316{1317u32 lane_assignments[SN_MAX_DP_LANES] = { 0, 1, 2, 3 };1318u32 lane_polarities[SN_MAX_DP_LANES] = { };1319struct device_node *endpoint;1320u8 ln_assign = 0;1321u8 ln_polrs = 0;1322int dp_lanes;1323int i;13241325/*1326* Read config from the device tree about lane remapping and lane1327* polarities. These are optional and we assume identity map and1328* normal polarity if nothing is specified. It's OK to specify just1329* data-lanes but not lane-polarities but not vice versa.1330*1331* Error checking is light (we just make sure we don't crash or1332* buffer overrun) and we assume dts is well formed and specifying1333* mappings that the hardware supports.1334*/1335endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);1336dp_lanes = drm_of_get_data_lanes_count(endpoint, 1, SN_MAX_DP_LANES);1337if (dp_lanes > 0) {1338of_property_read_u32_array(endpoint, "data-lanes",1339lane_assignments, dp_lanes);1340of_property_read_u32_array(endpoint, "lane-polarities",1341lane_polarities, dp_lanes);1342} else {1343dp_lanes = SN_MAX_DP_LANES;1344}1345of_node_put(endpoint);13461347/*1348* Convert into register format. Loop over all lanes even if1349* data-lanes had fewer elements so that we nicely initialize1350* the LN_ASSIGN register.1351*/1352for (i = SN_MAX_DP_LANES - 1; i >= 0; i--) {1353ln_assign = ln_assign << LN_ASSIGN_WIDTH | lane_assignments[i];1354ln_polrs = ln_polrs << 1 | lane_polarities[i];1355}13561357/* Stash in our struct for when we power on */1358pdata->dp_lanes = dp_lanes;1359pdata->ln_assign = ln_assign;1360pdata->ln_polrs = ln_polrs;1361}13621363static int ti_sn_bridge_parse_dsi_host(struct ti_sn65dsi86 *pdata)1364{1365struct device_node *np = pdata->dev->of_node;13661367pdata->host_node = of_graph_get_remote_node(np, 0, 0);13681369if (!pdata->host_node) {1370DRM_ERROR("remote dsi host node not found\n");1371return -ENODEV;1372}13731374return 0;1375}13761377static irqreturn_t ti_sn_bridge_interrupt(int irq, void *private)1378{1379struct ti_sn65dsi86 *pdata = private;1380struct drm_device *dev = pdata->bridge.dev;1381u8 status;1382int ret;1383bool hpd_event;13841385ret = ti_sn65dsi86_read_u8(pdata, SN_IRQ_STATUS_REG, &status);1386if (ret) {1387dev_err(pdata->dev, "Failed to read IRQ status: %d\n", ret);1388return IRQ_NONE;1389}13901391hpd_event = status & (HPD_REMOVAL_STATUS | HPD_INSERTION_STATUS);13921393dev_dbg(pdata->dev, "(SN_IRQ_STATUS_REG = %#x)\n", status);1394if (!status)1395return IRQ_NONE;13961397ret = regmap_write(pdata->regmap, SN_IRQ_STATUS_REG, status);1398if (ret) {1399dev_err(pdata->dev, "Failed to clear IRQ status: %d\n", ret);1400return IRQ_NONE;1401}14021403/* Only send the HPD event if we are bound with a device. */1404mutex_lock(&pdata->hpd_mutex);1405if (pdata->hpd_enabled && hpd_event)1406drm_kms_helper_hotplug_event(dev);1407mutex_unlock(&pdata->hpd_mutex);14081409return IRQ_HANDLED;1410}14111412static int ti_sn_bridge_probe(struct auxiliary_device *adev,1413const struct auxiliary_device_id *id)1414{1415struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);1416struct device_node *np = pdata->dev->of_node;1417int ret;14181419pdata->next_bridge = devm_drm_of_get_bridge(&adev->dev, np, 1, 0);1420if (IS_ERR(pdata->next_bridge))1421return dev_err_probe(&adev->dev, PTR_ERR(pdata->next_bridge),1422"failed to create panel bridge\n");14231424ti_sn_bridge_parse_lanes(pdata, np);14251426ret = ti_sn_bridge_parse_dsi_host(pdata);1427if (ret)1428return ret;14291430pdata->bridge.of_node = np;1431pdata->bridge.type = pdata->next_bridge->type == DRM_MODE_CONNECTOR_DisplayPort1432? DRM_MODE_CONNECTOR_DisplayPort : DRM_MODE_CONNECTOR_eDP;14331434if (pdata->bridge.type == DRM_MODE_CONNECTOR_DisplayPort) {1435pdata->bridge.ops = DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_DETECT |1436DRM_BRIDGE_OP_HPD;1437/*1438* If comms were already enabled they would have been enabled1439* with the wrong value of HPD_DISABLE. Update it now. Comms1440* could be enabled if anyone is holding a pm_runtime reference1441* (like if a GPIO is in use). Note that in most cases nobody1442* is doing AUX channel xfers before the bridge is added so1443* HPD doesn't _really_ matter then. The only exception is in1444* the eDP case where the panel wants to read the EDID before1445* the bridge is added. We always consistently have HPD disabled1446* for eDP.1447*/1448mutex_lock(&pdata->comms_mutex);1449if (pdata->comms_enabled)1450regmap_update_bits(pdata->regmap, SN_HPD_DISABLE_REG,1451HPD_DISABLE, 0);1452mutex_unlock(&pdata->comms_mutex);1453}14541455drm_bridge_add(&pdata->bridge);14561457ret = ti_sn_attach_host(adev, pdata);1458if (ret) {1459dev_err_probe(&adev->dev, ret, "failed to attach dsi host\n");1460goto err_remove_bridge;1461}14621463return 0;14641465err_remove_bridge:1466drm_bridge_remove(&pdata->bridge);1467return ret;1468}14691470static void ti_sn_bridge_remove(struct auxiliary_device *adev)1471{1472struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);14731474if (!pdata)1475return;14761477drm_bridge_remove(&pdata->bridge);14781479of_node_put(pdata->host_node);1480}14811482static const struct auxiliary_device_id ti_sn_bridge_id_table[] = {1483{ .name = "ti_sn65dsi86.bridge", },1484{},1485};14861487static struct auxiliary_driver ti_sn_bridge_driver = {1488.name = "bridge",1489.probe = ti_sn_bridge_probe,1490.remove = ti_sn_bridge_remove,1491.id_table = ti_sn_bridge_id_table,1492};14931494/* -----------------------------------------------------------------------------1495* PWM Controller1496*/1497#if IS_REACHABLE(CONFIG_PWM)1498static int ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata)1499{1500return atomic_xchg(&pdata->pwm_pin_busy, 1) ? -EBUSY : 0;1501}15021503static void ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata)1504{1505atomic_set(&pdata->pwm_pin_busy, 0);1506}15071508static struct ti_sn65dsi86 *pwm_chip_to_ti_sn_bridge(struct pwm_chip *chip)1509{1510return pwmchip_get_drvdata(chip);1511}15121513static int ti_sn_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)1514{1515struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);15161517return ti_sn_pwm_pin_request(pdata);1518}15191520static void ti_sn_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)1521{1522struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);15231524ti_sn_pwm_pin_release(pdata);1525}15261527/*1528* Limitations:1529* - The PWM signal is not driven when the chip is powered down, or in its1530* reset state and the driver does not implement the "suspend state"1531* described in the documentation. In order to save power, state->enabled is1532* interpreted as denoting if the signal is expected to be valid, and is used1533* to determine if the chip needs to be kept powered.1534* - Changing both period and duty_cycle is not done atomically, neither is the1535* multi-byte register updates, so the output might briefly be undefined1536* during update.1537*/1538static int ti_sn_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,1539const struct pwm_state *state)1540{1541struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);1542unsigned int pwm_en_inv;1543unsigned int backlight;1544unsigned int pre_div;1545unsigned int scale;1546u64 period_max;1547u64 period;1548int ret;15491550if (!pdata->pwm_enabled) {1551ret = pm_runtime_resume_and_get(pwmchip_parent(chip));1552if (ret < 0)1553return ret;1554}15551556if (state->enabled) {1557if (!pdata->pwm_enabled) {1558/*1559* The chip might have been powered down while we1560* didn't hold a PM runtime reference, so mux in the1561* PWM function on the GPIO pin again.1562*/1563ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1564SN_GPIO_MUX_MASK << (2 * SN_PWM_GPIO_IDX),1565SN_GPIO_MUX_SPECIAL << (2 * SN_PWM_GPIO_IDX));1566if (ret) {1567dev_err(pwmchip_parent(chip), "failed to mux in PWM function\n");1568goto out;1569}1570}15711572/*1573* Per the datasheet the PWM frequency is given by:1574*1575* REFCLK_FREQ1576* PWM_FREQ = -----------------------------------1577* PWM_PRE_DIV * BACKLIGHT_SCALE + 11578*1579* However, after careful review the author is convinced that1580* the documentation has lost some parenthesis around1581* "BACKLIGHT_SCALE + 1".1582*1583* With the period T_pwm = 1/PWM_FREQ this can be written:1584*1585* T_pwm * REFCLK_FREQ = PWM_PRE_DIV * (BACKLIGHT_SCALE + 1)1586*1587* In order to keep BACKLIGHT_SCALE within its 16 bits,1588* PWM_PRE_DIV must be:1589*1590* T_pwm * REFCLK_FREQ1591* PWM_PRE_DIV >= -------------------------1592* BACKLIGHT_SCALE_MAX + 11593*1594* To simplify the search and to favour higher resolution of1595* the duty cycle over accuracy of the period, the lowest1596* possible PWM_PRE_DIV is used. Finally the scale is1597* calculated as:1598*1599* T_pwm * REFCLK_FREQ1600* BACKLIGHT_SCALE = ---------------------- - 11601* PWM_PRE_DIV1602*1603* Here T_pwm is represented in seconds, so appropriate scaling1604* to nanoseconds is necessary.1605*/16061607/* Minimum T_pwm is 1 / REFCLK_FREQ */1608if (state->period <= NSEC_PER_SEC / pdata->pwm_refclk_freq) {1609ret = -EINVAL;1610goto out;1611}16121613/*1614* Maximum T_pwm is 255 * (65535 + 1) / REFCLK_FREQ1615* Limit period to this to avoid overflows1616*/1617period_max = div_u64((u64)NSEC_PER_SEC * 255 * (65535 + 1),1618pdata->pwm_refclk_freq);1619period = min(state->period, period_max);16201621pre_div = DIV64_U64_ROUND_UP(period * pdata->pwm_refclk_freq,1622(u64)NSEC_PER_SEC * (BACKLIGHT_SCALE_MAX + 1));1623scale = div64_u64(period * pdata->pwm_refclk_freq, (u64)NSEC_PER_SEC * pre_div) - 1;16241625/*1626* The documentation has the duty ratio given as:1627*1628* duty BACKLIGHT1629* ------- = ---------------------1630* period BACKLIGHT_SCALE + 11631*1632* Solve for BACKLIGHT, substituting BACKLIGHT_SCALE according1633* to definition above and adjusting for nanosecond1634* representation of duty cycle gives us:1635*/1636backlight = div64_u64(state->duty_cycle * pdata->pwm_refclk_freq,1637(u64)NSEC_PER_SEC * pre_div);1638if (backlight > scale)1639backlight = scale;16401641ret = regmap_write(pdata->regmap, SN_PWM_PRE_DIV_REG, pre_div);1642if (ret) {1643dev_err(pwmchip_parent(chip), "failed to update PWM_PRE_DIV\n");1644goto out;1645}16461647ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_SCALE_REG, scale);1648ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_REG, backlight);1649}16501651pwm_en_inv = FIELD_PREP(SN_PWM_EN_MASK, state->enabled) |1652FIELD_PREP(SN_PWM_INV_MASK, state->polarity == PWM_POLARITY_INVERSED);1653ret = regmap_write(pdata->regmap, SN_PWM_EN_INV_REG, pwm_en_inv);1654if (ret) {1655dev_err(pwmchip_parent(chip), "failed to update PWM_EN/PWM_INV\n");1656goto out;1657}16581659pdata->pwm_enabled = state->enabled;1660out:16611662if (!pdata->pwm_enabled)1663pm_runtime_put_sync(pwmchip_parent(chip));16641665return ret;1666}16671668static int ti_sn_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,1669struct pwm_state *state)1670{1671struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);1672unsigned int pwm_en_inv;1673unsigned int pre_div;1674u16 backlight;1675u16 scale;1676int ret;16771678ret = regmap_read(pdata->regmap, SN_PWM_EN_INV_REG, &pwm_en_inv);1679if (ret)1680return ret;16811682ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_SCALE_REG, &scale);1683if (ret)1684return ret;16851686ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_REG, &backlight);1687if (ret)1688return ret;16891690ret = regmap_read(pdata->regmap, SN_PWM_PRE_DIV_REG, &pre_div);1691if (ret)1692return ret;16931694state->enabled = FIELD_GET(SN_PWM_EN_MASK, pwm_en_inv);1695if (FIELD_GET(SN_PWM_INV_MASK, pwm_en_inv))1696state->polarity = PWM_POLARITY_INVERSED;1697else1698state->polarity = PWM_POLARITY_NORMAL;16991700state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * (scale + 1),1701pdata->pwm_refclk_freq);1702state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * backlight,1703pdata->pwm_refclk_freq);17041705if (state->duty_cycle > state->period)1706state->duty_cycle = state->period;17071708return 0;1709}17101711static const struct pwm_ops ti_sn_pwm_ops = {1712.request = ti_sn_pwm_request,1713.free = ti_sn_pwm_free,1714.apply = ti_sn_pwm_apply,1715.get_state = ti_sn_pwm_get_state,1716};17171718static int ti_sn_pwm_probe(struct auxiliary_device *adev,1719const struct auxiliary_device_id *id)1720{1721struct pwm_chip *chip;1722struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);17231724pdata->pchip = chip = devm_pwmchip_alloc(&adev->dev, 1, 0);1725if (IS_ERR(chip))1726return PTR_ERR(chip);17271728pwmchip_set_drvdata(chip, pdata);17291730chip->ops = &ti_sn_pwm_ops;1731chip->of_xlate = of_pwm_single_xlate;17321733devm_pm_runtime_enable(&adev->dev);17341735return pwmchip_add(chip);1736}17371738static void ti_sn_pwm_remove(struct auxiliary_device *adev)1739{1740struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);17411742pwmchip_remove(pdata->pchip);17431744if (pdata->pwm_enabled)1745pm_runtime_put_sync(&adev->dev);1746}17471748static const struct auxiliary_device_id ti_sn_pwm_id_table[] = {1749{ .name = "ti_sn65dsi86.pwm", },1750{},1751};17521753static struct auxiliary_driver ti_sn_pwm_driver = {1754.name = "pwm",1755.probe = ti_sn_pwm_probe,1756.remove = ti_sn_pwm_remove,1757.id_table = ti_sn_pwm_id_table,1758};17591760static int __init ti_sn_pwm_register(void)1761{1762return auxiliary_driver_register(&ti_sn_pwm_driver);1763}17641765static void ti_sn_pwm_unregister(void)1766{1767auxiliary_driver_unregister(&ti_sn_pwm_driver);1768}17691770#else1771static inline int __maybe_unused ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata) { return 0; }1772static inline void __maybe_unused ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata) {}17731774static inline int ti_sn_pwm_register(void) { return 0; }1775static inline void ti_sn_pwm_unregister(void) {}1776#endif17771778/* -----------------------------------------------------------------------------1779* GPIO Controller1780*/1781#if defined(CONFIG_OF_GPIO)17821783static int tn_sn_bridge_of_xlate(struct gpio_chip *chip,1784const struct of_phandle_args *gpiospec,1785u32 *flags)1786{1787if (WARN_ON(gpiospec->args_count < chip->of_gpio_n_cells))1788return -EINVAL;17891790if (gpiospec->args[0] > chip->ngpio || gpiospec->args[0] < 1)1791return -EINVAL;17921793if (flags)1794*flags = gpiospec->args[1];17951796return gpiospec->args[0] - SN_GPIO_PHYSICAL_OFFSET;1797}17981799static int ti_sn_bridge_gpio_get_direction(struct gpio_chip *chip,1800unsigned int offset)1801{1802struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);18031804/*1805* We already have to keep track of the direction because we use1806* that to figure out whether we've powered the device. We can1807* just return that rather than (maybe) powering up the device1808* to ask its direction.1809*/1810return test_bit(offset, pdata->gchip_output) ?1811GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;1812}18131814static int ti_sn_bridge_gpio_get(struct gpio_chip *chip, unsigned int offset)1815{1816struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1817unsigned int val;1818int ret;18191820/*1821* When the pin is an input we don't forcibly keep the bridge1822* powered--we just power it on to read the pin. NOTE: part of1823* the reason this works is that the bridge defaults (when1824* powered back on) to all 4 GPIOs being configured as GPIO input.1825* Also note that if something else is keeping the chip powered the1826* pm_runtime functions are lightweight increments of a refcount.1827*/1828pm_runtime_get_sync(pdata->dev);1829ret = regmap_read(pdata->regmap, SN_GPIO_IO_REG, &val);1830pm_runtime_put_autosuspend(pdata->dev);18311832if (ret)1833return ret;18341835return !!(val & BIT(SN_GPIO_INPUT_SHIFT + offset));1836}18371838static int ti_sn_bridge_gpio_set(struct gpio_chip *chip, unsigned int offset,1839int val)1840{1841struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);18421843val &= 1;1844return regmap_update_bits(pdata->regmap, SN_GPIO_IO_REG,1845BIT(SN_GPIO_OUTPUT_SHIFT + offset),1846val << (SN_GPIO_OUTPUT_SHIFT + offset));1847}18481849static int ti_sn_bridge_gpio_direction_input(struct gpio_chip *chip,1850unsigned int offset)1851{1852struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1853int shift = offset * 2;1854int ret;18551856if (!test_and_clear_bit(offset, pdata->gchip_output))1857return 0;18581859ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1860SN_GPIO_MUX_MASK << shift,1861SN_GPIO_MUX_INPUT << shift);1862if (ret) {1863set_bit(offset, pdata->gchip_output);1864return ret;1865}18661867/*1868* NOTE: if nobody else is powering the device this may fully power1869* it off and when it comes back it will have lost all state, but1870* that's OK because the default is input and we're now an input.1871*/1872pm_runtime_put_autosuspend(pdata->dev);18731874return 0;1875}18761877static int ti_sn_bridge_gpio_direction_output(struct gpio_chip *chip,1878unsigned int offset, int val)1879{1880struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1881int shift = offset * 2;1882int ret;18831884if (test_and_set_bit(offset, pdata->gchip_output))1885return 0;18861887pm_runtime_get_sync(pdata->dev);18881889/* Set value first to avoid glitching */1890ti_sn_bridge_gpio_set(chip, offset, val);18911892/* Set direction */1893ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1894SN_GPIO_MUX_MASK << shift,1895SN_GPIO_MUX_OUTPUT << shift);1896if (ret) {1897clear_bit(offset, pdata->gchip_output);1898pm_runtime_put_autosuspend(pdata->dev);1899}19001901return ret;1902}19031904static int ti_sn_bridge_gpio_request(struct gpio_chip *chip, unsigned int offset)1905{1906struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);19071908if (offset == SN_PWM_GPIO_IDX)1909return ti_sn_pwm_pin_request(pdata);19101911return 0;1912}19131914static void ti_sn_bridge_gpio_free(struct gpio_chip *chip, unsigned int offset)1915{1916struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);19171918/* We won't keep pm_runtime if we're input, so switch there on free */1919ti_sn_bridge_gpio_direction_input(chip, offset);19201921if (offset == SN_PWM_GPIO_IDX)1922ti_sn_pwm_pin_release(pdata);1923}19241925static const char * const ti_sn_bridge_gpio_names[SN_NUM_GPIOS] = {1926"GPIO1", "GPIO2", "GPIO3", "GPIO4"1927};19281929static int ti_sn_gpio_probe(struct auxiliary_device *adev,1930const struct auxiliary_device_id *id)1931{1932struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);1933int ret;19341935/* Only init if someone is going to use us as a GPIO controller */1936if (!of_property_read_bool(pdata->dev->of_node, "gpio-controller"))1937return 0;19381939pdata->gchip.label = dev_name(pdata->dev);1940pdata->gchip.parent = pdata->dev;1941pdata->gchip.owner = THIS_MODULE;1942pdata->gchip.of_xlate = tn_sn_bridge_of_xlate;1943pdata->gchip.of_gpio_n_cells = 2;1944pdata->gchip.request = ti_sn_bridge_gpio_request;1945pdata->gchip.free = ti_sn_bridge_gpio_free;1946pdata->gchip.get_direction = ti_sn_bridge_gpio_get_direction;1947pdata->gchip.direction_input = ti_sn_bridge_gpio_direction_input;1948pdata->gchip.direction_output = ti_sn_bridge_gpio_direction_output;1949pdata->gchip.get = ti_sn_bridge_gpio_get;1950pdata->gchip.set = ti_sn_bridge_gpio_set;1951pdata->gchip.can_sleep = true;1952pdata->gchip.names = ti_sn_bridge_gpio_names;1953pdata->gchip.ngpio = SN_NUM_GPIOS;1954pdata->gchip.base = -1;1955ret = devm_gpiochip_add_data(&adev->dev, &pdata->gchip, pdata);1956if (ret)1957dev_err(pdata->dev, "can't add gpio chip\n");19581959return ret;1960}19611962static const struct auxiliary_device_id ti_sn_gpio_id_table[] = {1963{ .name = "ti_sn65dsi86.gpio", },1964{},1965};19661967MODULE_DEVICE_TABLE(auxiliary, ti_sn_gpio_id_table);19681969static struct auxiliary_driver ti_sn_gpio_driver = {1970.name = "gpio",1971.probe = ti_sn_gpio_probe,1972.id_table = ti_sn_gpio_id_table,1973};19741975static int __init ti_sn_gpio_register(void)1976{1977return auxiliary_driver_register(&ti_sn_gpio_driver);1978}19791980static void ti_sn_gpio_unregister(void)1981{1982auxiliary_driver_unregister(&ti_sn_gpio_driver);1983}19841985#else19861987static inline int ti_sn_gpio_register(void) { return 0; }1988static inline void ti_sn_gpio_unregister(void) {}19891990#endif19911992/* -----------------------------------------------------------------------------1993* Probe & Remove1994*/19951996static void ti_sn65dsi86_runtime_disable(void *data)1997{1998pm_runtime_dont_use_autosuspend(data);1999pm_runtime_disable(data);2000}20012002static int ti_sn65dsi86_parse_regulators(struct ti_sn65dsi86 *pdata)2003{2004unsigned int i;2005const char * const ti_sn_bridge_supply_names[] = {2006"vcca", "vcc", "vccio", "vpll",2007};20082009for (i = 0; i < SN_REGULATOR_SUPPLY_NUM; i++)2010pdata->supplies[i].supply = ti_sn_bridge_supply_names[i];20112012return devm_regulator_bulk_get(pdata->dev, SN_REGULATOR_SUPPLY_NUM,2013pdata->supplies);2014}20152016static int ti_sn65dsi86_probe(struct i2c_client *client)2017{2018struct device *dev = &client->dev;2019struct ti_sn65dsi86 *pdata;2020u8 id_buf[8];2021int ret;20222023if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {2024DRM_ERROR("device doesn't support I2C\n");2025return -ENODEV;2026}20272028pdata = devm_drm_bridge_alloc(dev, struct ti_sn65dsi86, bridge, &ti_sn_bridge_funcs);2029if (IS_ERR(pdata))2030return PTR_ERR(pdata);2031dev_set_drvdata(dev, pdata);2032pdata->dev = dev;20332034mutex_init(&pdata->hpd_mutex);2035mutex_init(&pdata->comms_mutex);20362037pdata->regmap = devm_regmap_init_i2c(client,2038&ti_sn65dsi86_regmap_config);2039if (IS_ERR(pdata->regmap))2040return dev_err_probe(dev, PTR_ERR(pdata->regmap),2041"regmap i2c init failed\n");20422043pdata->enable_gpio = devm_gpiod_get_optional(dev, "enable",2044GPIOD_OUT_LOW);2045if (IS_ERR(pdata->enable_gpio))2046return dev_err_probe(dev, PTR_ERR(pdata->enable_gpio),2047"failed to get enable gpio from DT\n");20482049ret = ti_sn65dsi86_parse_regulators(pdata);2050if (ret)2051return dev_err_probe(dev, ret, "failed to parse regulators\n");20522053pdata->refclk = devm_clk_get_optional(dev, "refclk");2054if (IS_ERR(pdata->refclk))2055return dev_err_probe(dev, PTR_ERR(pdata->refclk),2056"failed to get reference clock\n");20572058pm_runtime_enable(dev);2059pm_runtime_set_autosuspend_delay(pdata->dev, 500);2060pm_runtime_use_autosuspend(pdata->dev);2061ret = devm_add_action_or_reset(dev, ti_sn65dsi86_runtime_disable, dev);2062if (ret)2063return ret;20642065pm_runtime_get_sync(dev);2066ret = regmap_bulk_read(pdata->regmap, SN_DEVICE_ID_REGS, id_buf, ARRAY_SIZE(id_buf));2067pm_runtime_put_autosuspend(dev);2068if (ret)2069return dev_err_probe(dev, ret, "failed to read device id\n");20702071/* The ID string is stored backwards */2072if (strncmp(id_buf, "68ISD ", ARRAY_SIZE(id_buf)))2073return dev_err_probe(dev, -EOPNOTSUPP, "unsupported device id\n");20742075if (client->irq) {2076ret = devm_request_threaded_irq(pdata->dev, client->irq, NULL,2077ti_sn_bridge_interrupt,2078IRQF_ONESHOT,2079dev_name(pdata->dev), pdata);20802081if (ret)2082return dev_err_probe(dev, ret, "failed to request interrupt\n");2083}20842085/*2086* Break ourselves up into a collection of aux devices. The only real2087* motiviation here is to solve the chicken-and-egg problem of probe2088* ordering. The bridge wants the panel to be there when it probes.2089* The panel wants its HPD GPIO (provided by sn65dsi86 on some boards)2090* when it probes. The panel and maybe backlight might want the DDC2091* bus or the pwm_chip. Having sub-devices allows the some sub devices2092* to finish probing even if others return -EPROBE_DEFER and gets us2093* around the problems.2094*/20952096if (IS_ENABLED(CONFIG_OF_GPIO)) {2097ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->gpio_aux, "gpio");2098if (ret)2099return ret;2100}21012102if (IS_REACHABLE(CONFIG_PWM)) {2103ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->pwm_aux, "pwm");2104if (ret)2105return ret;2106}21072108/*2109* NOTE: At the end of the AUX channel probe we'll add the aux device2110* for the bridge. This is because the bridge can't be used until the2111* AUX channel is there and this is a very simple solution to the2112* dependency problem.2113*/2114return ti_sn65dsi86_add_aux_device(pdata, &pdata->aux_aux, "aux");2115}21162117static const struct i2c_device_id ti_sn65dsi86_id[] = {2118{ "ti,sn65dsi86" },2119{}2120};2121MODULE_DEVICE_TABLE(i2c, ti_sn65dsi86_id);21222123static const struct of_device_id ti_sn65dsi86_match_table[] = {2124{.compatible = "ti,sn65dsi86"},2125{},2126};2127MODULE_DEVICE_TABLE(of, ti_sn65dsi86_match_table);21282129static struct i2c_driver ti_sn65dsi86_driver = {2130.driver = {2131.name = "ti_sn65dsi86",2132.of_match_table = ti_sn65dsi86_match_table,2133.pm = &ti_sn65dsi86_pm_ops,2134},2135.probe = ti_sn65dsi86_probe,2136.id_table = ti_sn65dsi86_id,2137};21382139static int __init ti_sn65dsi86_init(void)2140{2141int ret;21422143ret = i2c_add_driver(&ti_sn65dsi86_driver);2144if (ret)2145return ret;21462147ret = ti_sn_gpio_register();2148if (ret)2149goto err_main_was_registered;21502151ret = ti_sn_pwm_register();2152if (ret)2153goto err_gpio_was_registered;21542155ret = auxiliary_driver_register(&ti_sn_aux_driver);2156if (ret)2157goto err_pwm_was_registered;21582159ret = auxiliary_driver_register(&ti_sn_bridge_driver);2160if (ret)2161goto err_aux_was_registered;21622163return 0;21642165err_aux_was_registered:2166auxiliary_driver_unregister(&ti_sn_aux_driver);2167err_pwm_was_registered:2168ti_sn_pwm_unregister();2169err_gpio_was_registered:2170ti_sn_gpio_unregister();2171err_main_was_registered:2172i2c_del_driver(&ti_sn65dsi86_driver);21732174return ret;2175}2176module_init(ti_sn65dsi86_init);21772178static void __exit ti_sn65dsi86_exit(void)2179{2180auxiliary_driver_unregister(&ti_sn_bridge_driver);2181auxiliary_driver_unregister(&ti_sn_aux_driver);2182ti_sn_pwm_unregister();2183ti_sn_gpio_unregister();2184i2c_del_driver(&ti_sn65dsi86_driver);2185}2186module_exit(ti_sn65dsi86_exit);21872188MODULE_AUTHOR("Sandeep Panda <[email protected]>");2189MODULE_DESCRIPTION("sn65dsi86 DSI to eDP bridge driver");2190MODULE_LICENSE("GPL v2");219121922193