Path: blob/master/drivers/gpu/drm/bridge/ti-sn65dsi86.c
26494 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)108#define SN_AUX_CMD_STATUS_REG 0xF4109#define AUX_IRQ_STATUS_AUX_RPLY_TOUT BIT(3)110#define AUX_IRQ_STATUS_AUX_SHORT BIT(5)111#define AUX_IRQ_STATUS_NAT_I2C_FAIL BIT(6)112113#define MIN_DSI_CLK_FREQ_MHZ 40114115/* fudge factor required to account for 8b/10b encoding */116#define DP_CLK_FUDGE_NUM 10117#define DP_CLK_FUDGE_DEN 8118119/* Matches DP_AUX_MAX_PAYLOAD_BYTES (for now) */120#define SN_AUX_MAX_PAYLOAD_BYTES 16121122#define SN_REGULATOR_SUPPLY_NUM 4123124#define SN_MAX_DP_LANES 4125#define SN_NUM_GPIOS 4126#define SN_GPIO_PHYSICAL_OFFSET 1127128#define SN_LINK_TRAINING_TRIES 10129130#define SN_PWM_GPIO_IDX 3 /* 4th GPIO */131132/**133* struct ti_sn65dsi86 - Platform data for ti-sn65dsi86 driver.134* @bridge_aux: AUX-bus sub device for MIPI-to-eDP bridge functionality.135* @gpio_aux: AUX-bus sub device for GPIO controller functionality.136* @aux_aux: AUX-bus sub device for eDP AUX channel functionality.137* @pwm_aux: AUX-bus sub device for PWM controller functionality.138*139* @dev: Pointer to the top level (i2c) device.140* @regmap: Regmap for accessing i2c.141* @aux: Our aux channel.142* @bridge: Our bridge.143* @connector: Our connector.144* @host_node: Remote DSI node.145* @dsi: Our MIPI DSI source.146* @refclk: Our reference clock.147* @next_bridge: The bridge on the eDP side.148* @enable_gpio: The GPIO we toggle to enable the bridge.149* @supplies: Data for bulk enabling/disabling our regulators.150* @dp_lanes: Count of dp_lanes we're using.151* @ln_assign: Value to program to the LN_ASSIGN register.152* @ln_polrs: Value for the 4-bit LN_POLRS field of SN_ENH_FRAME_REG.153* @comms_enabled: If true then communication over the aux channel is enabled.154* @comms_mutex: Protects modification of comms_enabled.155*156* @gchip: If we expose our GPIOs, this is used.157* @gchip_output: A cache of whether we've set GPIOs to output. This158* serves double-duty of keeping track of the direction and159* also keeping track of whether we've incremented the160* pm_runtime reference count for this pin, which we do161* whenever a pin is configured as an output. This is a162* bitmap so we can do atomic ops on it without an extra163* lock so concurrent users of our 4 GPIOs don't stomp on164* each other's read-modify-write.165*166* @pchip: pwm_chip if the PWM is exposed.167* @pwm_enabled: Used to track if the PWM signal is currently enabled.168* @pwm_pin_busy: Track if GPIO4 is currently requested for GPIO or PWM.169* @pwm_refclk_freq: Cache for the reference clock input to the PWM.170*/171struct ti_sn65dsi86 {172struct auxiliary_device *bridge_aux;173struct auxiliary_device *gpio_aux;174struct auxiliary_device *aux_aux;175struct auxiliary_device *pwm_aux;176177struct device *dev;178struct regmap *regmap;179struct drm_dp_aux aux;180struct drm_bridge bridge;181struct drm_connector *connector;182struct device_node *host_node;183struct mipi_dsi_device *dsi;184struct clk *refclk;185struct drm_bridge *next_bridge;186struct gpio_desc *enable_gpio;187struct regulator_bulk_data supplies[SN_REGULATOR_SUPPLY_NUM];188int dp_lanes;189u8 ln_assign;190u8 ln_polrs;191bool comms_enabled;192struct mutex comms_mutex;193194#if defined(CONFIG_OF_GPIO)195struct gpio_chip gchip;196DECLARE_BITMAP(gchip_output, SN_NUM_GPIOS);197#endif198#if IS_REACHABLE(CONFIG_PWM)199struct pwm_chip *pchip;200bool pwm_enabled;201atomic_t pwm_pin_busy;202#endif203unsigned int pwm_refclk_freq;204};205206static const struct regmap_range ti_sn65dsi86_volatile_ranges[] = {207{ .range_min = 0, .range_max = 0xFF },208};209210static const struct regmap_access_table ti_sn_bridge_volatile_table = {211.yes_ranges = ti_sn65dsi86_volatile_ranges,212.n_yes_ranges = ARRAY_SIZE(ti_sn65dsi86_volatile_ranges),213};214215static const struct regmap_config ti_sn65dsi86_regmap_config = {216.reg_bits = 8,217.val_bits = 8,218.volatile_table = &ti_sn_bridge_volatile_table,219.cache_type = REGCACHE_NONE,220.max_register = 0xFF,221};222223static int __maybe_unused ti_sn65dsi86_read_u16(struct ti_sn65dsi86 *pdata,224unsigned int reg, u16 *val)225{226u8 buf[2];227int ret;228229ret = regmap_bulk_read(pdata->regmap, reg, buf, ARRAY_SIZE(buf));230if (ret)231return ret;232233*val = buf[0] | (buf[1] << 8);234235return 0;236}237238static void ti_sn65dsi86_write_u16(struct ti_sn65dsi86 *pdata,239unsigned int reg, u16 val)240{241u8 buf[2] = { val & 0xff, val >> 8 };242243regmap_bulk_write(pdata->regmap, reg, buf, ARRAY_SIZE(buf));244}245246static struct drm_display_mode *247get_new_adjusted_display_mode(struct drm_bridge *bridge,248struct drm_atomic_state *state)249{250struct drm_connector *connector =251drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);252struct drm_connector_state *conn_state =253drm_atomic_get_new_connector_state(state, connector);254struct drm_crtc_state *crtc_state =255drm_atomic_get_new_crtc_state(state, conn_state->crtc);256257return &crtc_state->adjusted_mode;258}259260static u32 ti_sn_bridge_get_dsi_freq(struct ti_sn65dsi86 *pdata,261struct drm_atomic_state *state)262{263u32 bit_rate_khz, clk_freq_khz;264struct drm_display_mode *mode =265get_new_adjusted_display_mode(&pdata->bridge, state);266267bit_rate_khz = mode->clock *268mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);269clk_freq_khz = bit_rate_khz / (pdata->dsi->lanes * 2);270271return clk_freq_khz;272}273274/* clk frequencies supported by bridge in Hz in case derived from REFCLK pin */275static const u32 ti_sn_bridge_refclk_lut[] = {27612000000,27719200000,27826000000,27927000000,28038400000,281};282283/* clk frequencies supported by bridge in Hz in case derived from DACP/N pin */284static const u32 ti_sn_bridge_dsiclk_lut[] = {285468000000,286384000000,287416000000,288486000000,289460800000,290};291292static void ti_sn_bridge_set_refclk_freq(struct ti_sn65dsi86 *pdata,293struct drm_atomic_state *state)294{295int i;296u32 refclk_rate;297const u32 *refclk_lut;298size_t refclk_lut_size;299300if (pdata->refclk) {301refclk_rate = clk_get_rate(pdata->refclk);302refclk_lut = ti_sn_bridge_refclk_lut;303refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_refclk_lut);304clk_prepare_enable(pdata->refclk);305} else {306refclk_rate = ti_sn_bridge_get_dsi_freq(pdata, state) * 1000;307refclk_lut = ti_sn_bridge_dsiclk_lut;308refclk_lut_size = ARRAY_SIZE(ti_sn_bridge_dsiclk_lut);309}310311/* for i equals to refclk_lut_size means default frequency */312for (i = 0; i < refclk_lut_size; i++)313if (refclk_lut[i] == refclk_rate)314break;315316/* avoid buffer overflow and "1" is the default rate in the datasheet. */317if (i >= refclk_lut_size)318i = 1;319320regmap_update_bits(pdata->regmap, SN_DPPLL_SRC_REG, REFCLK_FREQ_MASK,321REFCLK_FREQ(i));322323/*324* The PWM refclk is based on the value written to SN_DPPLL_SRC_REG,325* regardless of its actual sourcing.326*/327pdata->pwm_refclk_freq = ti_sn_bridge_refclk_lut[i];328}329330static void ti_sn65dsi86_enable_comms(struct ti_sn65dsi86 *pdata,331struct drm_atomic_state *state)332{333mutex_lock(&pdata->comms_mutex);334335/* configure bridge ref_clk */336ti_sn_bridge_set_refclk_freq(pdata, state);337338/*339* HPD on this bridge chip is a bit useless. This is an eDP bridge340* so the HPD is an internal signal that's only there to signal that341* the panel is done powering up. ...but the bridge chip debounces342* this signal by between 100 ms and 400 ms (depending on process,343* voltage, and temperate--I measured it at about 200 ms). One344* particular panel asserted HPD 84 ms after it was powered on meaning345* that we saw HPD 284 ms after power on. ...but the same panel said346* that instead of looking at HPD you could just hardcode a delay of347* 200 ms. We'll assume that the panel driver will have the hardcoded348* delay in its prepare and always disable HPD.349*350* For DisplayPort bridge type, we need HPD. So we use the bridge type351* to conditionally disable HPD.352* NOTE: The bridge type is set in ti_sn_bridge_probe() but enable_comms()353* can be called before. So for DisplayPort, HPD will be enabled once354* bridge type is set. We are using bridge type instead of "no-hpd"355* property because it is not used properly in devicetree description356* and hence is unreliable.357*/358359if (pdata->bridge.type != DRM_MODE_CONNECTOR_DisplayPort)360regmap_update_bits(pdata->regmap, SN_HPD_DISABLE_REG, HPD_DISABLE,361HPD_DISABLE);362363pdata->comms_enabled = true;364365mutex_unlock(&pdata->comms_mutex);366}367368static void ti_sn65dsi86_disable_comms(struct ti_sn65dsi86 *pdata)369{370mutex_lock(&pdata->comms_mutex);371372pdata->comms_enabled = false;373clk_disable_unprepare(pdata->refclk);374375mutex_unlock(&pdata->comms_mutex);376}377378static int __maybe_unused ti_sn65dsi86_resume(struct device *dev)379{380struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);381int ret;382383ret = regulator_bulk_enable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);384if (ret) {385DRM_ERROR("failed to enable supplies %d\n", ret);386return ret;387}388389/* td2: min 100 us after regulators before enabling the GPIO */390usleep_range(100, 110);391392gpiod_set_value_cansleep(pdata->enable_gpio, 1);393394/*395* After EN is deasserted and an external clock is detected, the bridge396* will sample GPIO3:1 to determine its frequency. The driver will397* overwrite this setting in ti_sn_bridge_set_refclk_freq(). But this is398* racy. Thus we have to wait a couple of us. According to the datasheet399* the GPIO lines has to be stable at least 5 us (td5) but it seems that400* is not enough and the refclk frequency value is still lost or401* overwritten by the bridge itself. Waiting for 20us seems to work.402*/403usleep_range(20, 30);404405/*406* If we have a reference clock we can enable communication w/ the407* panel (including the aux channel) w/out any need for an input clock408* so we can do it in resume which lets us read the EDID before409* pre_enable(). Without a reference clock we need the MIPI reference410* clock so reading early doesn't work.411*/412if (pdata->refclk)413ti_sn65dsi86_enable_comms(pdata, NULL);414415return ret;416}417418static int __maybe_unused ti_sn65dsi86_suspend(struct device *dev)419{420struct ti_sn65dsi86 *pdata = dev_get_drvdata(dev);421int ret;422423if (pdata->refclk)424ti_sn65dsi86_disable_comms(pdata);425426gpiod_set_value_cansleep(pdata->enable_gpio, 0);427428ret = regulator_bulk_disable(SN_REGULATOR_SUPPLY_NUM, pdata->supplies);429if (ret)430DRM_ERROR("failed to disable supplies %d\n", ret);431432return ret;433}434435static const struct dev_pm_ops ti_sn65dsi86_pm_ops = {436SET_RUNTIME_PM_OPS(ti_sn65dsi86_suspend, ti_sn65dsi86_resume, NULL)437SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,438pm_runtime_force_resume)439};440441static int status_show(struct seq_file *s, void *data)442{443struct ti_sn65dsi86 *pdata = s->private;444unsigned int reg, val;445446seq_puts(s, "STATUS REGISTERS:\n");447448pm_runtime_get_sync(pdata->dev);449450/* IRQ Status Registers, see Table 31 in datasheet */451for (reg = 0xf0; reg <= 0xf8; reg++) {452regmap_read(pdata->regmap, reg, &val);453seq_printf(s, "[0x%02x] = 0x%08x\n", reg, val);454}455456pm_runtime_put_autosuspend(pdata->dev);457458return 0;459}460DEFINE_SHOW_ATTRIBUTE(status);461462/* -----------------------------------------------------------------------------463* Auxiliary Devices (*not* AUX)464*/465466static int ti_sn65dsi86_add_aux_device(struct ti_sn65dsi86 *pdata,467struct auxiliary_device **aux_out,468const char *name)469{470struct device *dev = pdata->dev;471const struct i2c_client *client = to_i2c_client(dev);472struct auxiliary_device *aux;473int id;474475id = (client->adapter->nr << 10) | client->addr;476aux = __devm_auxiliary_device_create(dev, KBUILD_MODNAME, name,477NULL, id);478if (!aux)479return -ENODEV;480481*aux_out = aux;482return 0;483}484485/* -----------------------------------------------------------------------------486* AUX Adapter487*/488489static struct ti_sn65dsi86 *aux_to_ti_sn65dsi86(struct drm_dp_aux *aux)490{491return container_of(aux, struct ti_sn65dsi86, aux);492}493494static ssize_t ti_sn_aux_transfer(struct drm_dp_aux *aux,495struct drm_dp_aux_msg *msg)496{497struct ti_sn65dsi86 *pdata = aux_to_ti_sn65dsi86(aux);498u32 request = msg->request & ~(DP_AUX_I2C_MOT | DP_AUX_I2C_WRITE_STATUS_UPDATE);499u32 request_val = AUX_CMD_REQ(msg->request);500u8 *buf = msg->buffer;501unsigned int len = msg->size;502unsigned int short_len;503unsigned int val;504int ret;505u8 addr_len[SN_AUX_LENGTH_REG + 1 - SN_AUX_ADDR_19_16_REG];506507if (len > SN_AUX_MAX_PAYLOAD_BYTES)508return -EINVAL;509510pm_runtime_get_sync(pdata->dev);511mutex_lock(&pdata->comms_mutex);512513/*514* If someone tries to do a DDC over AUX transaction before pre_enable()515* on a device without a dedicated reference clock then we just can't516* do it. Fail right away. This prevents non-refclk users from reading517* the EDID before enabling the panel but such is life.518*/519if (!pdata->comms_enabled) {520ret = -EIO;521goto exit;522}523524switch (request) {525case DP_AUX_NATIVE_WRITE:526case DP_AUX_I2C_WRITE:527case DP_AUX_NATIVE_READ:528case DP_AUX_I2C_READ:529regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val);530/* Assume it's good */531msg->reply = 0;532break;533default:534ret = -EINVAL;535goto exit;536}537538BUILD_BUG_ON(sizeof(addr_len) != sizeof(__be32));539put_unaligned_be32((msg->address & SN_AUX_ADDR_MASK) << 8 | len,540addr_len);541regmap_bulk_write(pdata->regmap, SN_AUX_ADDR_19_16_REG, addr_len,542ARRAY_SIZE(addr_len));543544if (request == DP_AUX_NATIVE_WRITE || request == DP_AUX_I2C_WRITE)545regmap_bulk_write(pdata->regmap, SN_AUX_WDATA_REG(0), buf, len);546547/* Clear old status bits before start so we don't get confused */548regmap_write(pdata->regmap, SN_AUX_CMD_STATUS_REG,549AUX_IRQ_STATUS_NAT_I2C_FAIL |550AUX_IRQ_STATUS_AUX_RPLY_TOUT |551AUX_IRQ_STATUS_AUX_SHORT);552553regmap_write(pdata->regmap, SN_AUX_CMD_REG, request_val | AUX_CMD_SEND);554555/* Zero delay loop because i2c transactions are slow already */556ret = regmap_read_poll_timeout(pdata->regmap, SN_AUX_CMD_REG, val,557!(val & AUX_CMD_SEND), 0, 50 * 1000);558if (ret)559goto exit;560561ret = regmap_read(pdata->regmap, SN_AUX_CMD_STATUS_REG, &val);562if (ret)563goto exit;564565if (val & AUX_IRQ_STATUS_AUX_RPLY_TOUT) {566/*567* The hardware tried the message seven times per the DP spec568* but it hit a timeout. We ignore defers here because they're569* handled in hardware.570*/571ret = -ETIMEDOUT;572goto exit;573}574575if (val & AUX_IRQ_STATUS_AUX_SHORT) {576ret = regmap_read(pdata->regmap, SN_AUX_LENGTH_REG, &short_len);577len = min(len, short_len);578if (ret)579goto exit;580} else if (val & AUX_IRQ_STATUS_NAT_I2C_FAIL) {581switch (request) {582case DP_AUX_I2C_WRITE:583case DP_AUX_I2C_READ:584msg->reply |= DP_AUX_I2C_REPLY_NACK;585break;586case DP_AUX_NATIVE_READ:587case DP_AUX_NATIVE_WRITE:588msg->reply |= DP_AUX_NATIVE_REPLY_NACK;589break;590}591len = 0;592goto exit;593}594595if (request != DP_AUX_NATIVE_WRITE && request != DP_AUX_I2C_WRITE && len != 0)596ret = regmap_bulk_read(pdata->regmap, SN_AUX_RDATA_REG(0), buf, len);597598exit:599mutex_unlock(&pdata->comms_mutex);600pm_runtime_mark_last_busy(pdata->dev);601pm_runtime_put_autosuspend(pdata->dev);602603if (ret)604return ret;605return len;606}607608static int ti_sn_aux_wait_hpd_asserted(struct drm_dp_aux *aux, unsigned long wait_us)609{610/*611* The HPD in this chip is a bit useless (See comment in612* ti_sn65dsi86_enable_comms) so if our driver is expected to wait613* for HPD, we just assume it's asserted after the wait_us delay.614*615* In case we are asked to wait forever (wait_us=0) take conservative616* 500ms delay.617*/618if (wait_us == 0)619wait_us = 500000;620621usleep_range(wait_us, wait_us + 1000);622623return 0;624}625626static int ti_sn_aux_probe(struct auxiliary_device *adev,627const struct auxiliary_device_id *id)628{629struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);630int ret;631632pdata->aux.name = "ti-sn65dsi86-aux";633pdata->aux.dev = &adev->dev;634pdata->aux.transfer = ti_sn_aux_transfer;635pdata->aux.wait_hpd_asserted = ti_sn_aux_wait_hpd_asserted;636drm_dp_aux_init(&pdata->aux);637638ret = devm_of_dp_aux_populate_ep_devices(&pdata->aux);639if (ret)640return ret;641642/*643* The eDP to MIPI bridge parts don't work until the AUX channel is644* setup so we don't add it in the main driver probe, we add it now.645*/646return ti_sn65dsi86_add_aux_device(pdata, &pdata->bridge_aux, "bridge");647}648649static const struct auxiliary_device_id ti_sn_aux_id_table[] = {650{ .name = "ti_sn65dsi86.aux", },651{},652};653654static struct auxiliary_driver ti_sn_aux_driver = {655.name = "aux",656.probe = ti_sn_aux_probe,657.id_table = ti_sn_aux_id_table,658};659660/*------------------------------------------------------------------------------661* DRM Bridge662*/663664static struct ti_sn65dsi86 *bridge_to_ti_sn65dsi86(struct drm_bridge *bridge)665{666return container_of(bridge, struct ti_sn65dsi86, bridge);667}668669static int ti_sn_attach_host(struct auxiliary_device *adev, struct ti_sn65dsi86 *pdata)670{671int val;672struct mipi_dsi_host *host;673struct mipi_dsi_device *dsi;674struct device *dev = pdata->dev;675const struct mipi_dsi_device_info info = { .type = "ti_sn_bridge",676.channel = 0,677.node = NULL,678};679680host = of_find_mipi_dsi_host_by_node(pdata->host_node);681if (!host)682return -EPROBE_DEFER;683684dsi = devm_mipi_dsi_device_register_full(&adev->dev, host, &info);685if (IS_ERR(dsi))686return PTR_ERR(dsi);687688/* TODO: setting to 4 MIPI lanes always for now */689dsi->lanes = 4;690dsi->format = MIPI_DSI_FMT_RGB888;691dsi->mode_flags = MIPI_DSI_MODE_VIDEO;692693/* check if continuous dsi clock is required or not */694pm_runtime_get_sync(dev);695regmap_read(pdata->regmap, SN_DPPLL_SRC_REG, &val);696pm_runtime_put_autosuspend(dev);697if (!(val & DPPLL_CLK_SRC_DSICLK))698dsi->mode_flags |= MIPI_DSI_CLOCK_NON_CONTINUOUS;699700pdata->dsi = dsi;701702return devm_mipi_dsi_attach(&adev->dev, dsi);703}704705static int ti_sn_bridge_attach(struct drm_bridge *bridge,706struct drm_encoder *encoder,707enum drm_bridge_attach_flags flags)708{709struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);710int ret;711712pdata->aux.drm_dev = bridge->dev;713ret = drm_dp_aux_register(&pdata->aux);714if (ret < 0) {715drm_err(bridge->dev, "Failed to register DP AUX channel: %d\n", ret);716return ret;717}718719/*720* Attach the next bridge.721* We never want the next bridge to *also* create a connector.722*/723ret = drm_bridge_attach(encoder, pdata->next_bridge,724&pdata->bridge, flags | DRM_BRIDGE_ATTACH_NO_CONNECTOR);725if (ret < 0)726goto err_initted_aux;727728if (flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)729return 0;730731pdata->connector = drm_bridge_connector_init(pdata->bridge.dev,732pdata->bridge.encoder);733if (IS_ERR(pdata->connector)) {734ret = PTR_ERR(pdata->connector);735goto err_initted_aux;736}737738drm_connector_attach_encoder(pdata->connector, pdata->bridge.encoder);739740return 0;741742err_initted_aux:743drm_dp_aux_unregister(&pdata->aux);744return ret;745}746747static void ti_sn_bridge_detach(struct drm_bridge *bridge)748{749drm_dp_aux_unregister(&bridge_to_ti_sn65dsi86(bridge)->aux);750}751752static enum drm_mode_status753ti_sn_bridge_mode_valid(struct drm_bridge *bridge,754const struct drm_display_info *info,755const struct drm_display_mode *mode)756{757/* maximum supported resolution is 4K at 60 fps */758if (mode->clock > 594000)759return MODE_CLOCK_HIGH;760761/*762* The front and back porch registers are 8 bits, and pulse width763* registers are 15 bits, so reject any modes with larger periods.764*/765766if ((mode->hsync_start - mode->hdisplay) > 0xff)767return MODE_HBLANK_WIDE;768769if ((mode->vsync_start - mode->vdisplay) > 0xff)770return MODE_VBLANK_WIDE;771772if ((mode->hsync_end - mode->hsync_start) > 0x7fff)773return MODE_HSYNC_WIDE;774775if ((mode->vsync_end - mode->vsync_start) > 0x7fff)776return MODE_VSYNC_WIDE;777778if ((mode->htotal - mode->hsync_end) > 0xff)779return MODE_HBLANK_WIDE;780781if ((mode->vtotal - mode->vsync_end) > 0xff)782return MODE_VBLANK_WIDE;783784return MODE_OK;785}786787static void ti_sn_bridge_atomic_disable(struct drm_bridge *bridge,788struct drm_atomic_state *state)789{790struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);791792/* disable video stream */793regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE, 0);794}795796static void ti_sn_bridge_set_dsi_rate(struct ti_sn65dsi86 *pdata,797struct drm_atomic_state *state)798{799unsigned int bit_rate_mhz, clk_freq_mhz;800unsigned int val;801struct drm_display_mode *mode =802get_new_adjusted_display_mode(&pdata->bridge, state);803804/* set DSIA clk frequency */805bit_rate_mhz = (mode->clock / 1000) *806mipi_dsi_pixel_format_to_bpp(pdata->dsi->format);807clk_freq_mhz = bit_rate_mhz / (pdata->dsi->lanes * 2);808809/* for each increment in val, frequency increases by 5MHz */810val = (MIN_DSI_CLK_FREQ_MHZ / 5) +811(((clk_freq_mhz - MIN_DSI_CLK_FREQ_MHZ) / 5) & 0xFF);812regmap_write(pdata->regmap, SN_DSIA_CLK_FREQ_REG, val);813}814815static unsigned int ti_sn_bridge_get_bpp(struct drm_connector *connector)816{817if (connector->display_info.bpc <= 6)818return 18;819else820return 24;821}822823/*824* LUT index corresponds to register value and825* LUT values corresponds to dp data rate supported826* by the bridge in Mbps unit.827*/828static const unsigned int ti_sn_bridge_dp_rate_lut[] = {8290, 1620, 2160, 2430, 2700, 3240, 4320, 5400830};831832static int ti_sn_bridge_calc_min_dp_rate_idx(struct ti_sn65dsi86 *pdata,833struct drm_atomic_state *state,834unsigned int bpp)835{836unsigned int bit_rate_khz, dp_rate_mhz;837unsigned int i;838struct drm_display_mode *mode =839get_new_adjusted_display_mode(&pdata->bridge, state);840841/* Calculate minimum bit rate based on our pixel clock. */842bit_rate_khz = mode->clock * bpp;843844/* Calculate minimum DP data rate, taking 80% as per DP spec */845dp_rate_mhz = DIV_ROUND_UP(bit_rate_khz * DP_CLK_FUDGE_NUM,8461000 * pdata->dp_lanes * DP_CLK_FUDGE_DEN);847848for (i = 1; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut) - 1; i++)849if (ti_sn_bridge_dp_rate_lut[i] >= dp_rate_mhz)850break;851852return i;853}854855static unsigned int ti_sn_bridge_read_valid_rates(struct ti_sn65dsi86 *pdata)856{857unsigned int valid_rates = 0;858unsigned int rate_per_200khz;859unsigned int rate_mhz;860u8 dpcd_val;861int ret;862int i, j;863864ret = drm_dp_dpcd_readb(&pdata->aux, DP_EDP_DPCD_REV, &dpcd_val);865if (ret != 1) {866DRM_DEV_ERROR(pdata->dev,867"Can't read eDP rev (%d), assuming 1.1\n", ret);868dpcd_val = DP_EDP_11;869}870871if (dpcd_val >= DP_EDP_14) {872/* eDP 1.4 devices must provide a custom table */873__le16 sink_rates[DP_MAX_SUPPORTED_RATES];874875ret = drm_dp_dpcd_read(&pdata->aux, DP_SUPPORTED_LINK_RATES,876sink_rates, sizeof(sink_rates));877878if (ret != sizeof(sink_rates)) {879DRM_DEV_ERROR(pdata->dev,880"Can't read supported rate table (%d)\n", ret);881882/* By zeroing we'll fall back to DP_MAX_LINK_RATE. */883memset(sink_rates, 0, sizeof(sink_rates));884}885886for (i = 0; i < ARRAY_SIZE(sink_rates); i++) {887rate_per_200khz = le16_to_cpu(sink_rates[i]);888889if (!rate_per_200khz)890break;891892rate_mhz = rate_per_200khz * 200 / 1000;893for (j = 0;894j < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);895j++) {896if (ti_sn_bridge_dp_rate_lut[j] == rate_mhz)897valid_rates |= BIT(j);898}899}900901for (i = 0; i < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut); i++) {902if (valid_rates & BIT(i))903return valid_rates;904}905DRM_DEV_ERROR(pdata->dev,906"No matching eDP rates in table; falling back\n");907}908909/* On older versions best we can do is use DP_MAX_LINK_RATE */910ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LINK_RATE, &dpcd_val);911if (ret != 1) {912DRM_DEV_ERROR(pdata->dev,913"Can't read max rate (%d); assuming 5.4 GHz\n",914ret);915dpcd_val = DP_LINK_BW_5_4;916}917918switch (dpcd_val) {919default:920DRM_DEV_ERROR(pdata->dev,921"Unexpected max rate (%#x); assuming 5.4 GHz\n",922(int)dpcd_val);923fallthrough;924case DP_LINK_BW_5_4:925valid_rates |= BIT(7);926fallthrough;927case DP_LINK_BW_2_7:928valid_rates |= BIT(4);929fallthrough;930case DP_LINK_BW_1_62:931valid_rates |= BIT(1);932break;933}934935return valid_rates;936}937938static void ti_sn_bridge_set_video_timings(struct ti_sn65dsi86 *pdata,939struct drm_atomic_state *state)940{941struct drm_display_mode *mode =942get_new_adjusted_display_mode(&pdata->bridge, state);943u8 hsync_polarity = 0, vsync_polarity = 0;944945if (mode->flags & DRM_MODE_FLAG_NHSYNC)946hsync_polarity = CHA_HSYNC_POLARITY;947if (mode->flags & DRM_MODE_FLAG_NVSYNC)948vsync_polarity = CHA_VSYNC_POLARITY;949950ti_sn65dsi86_write_u16(pdata, SN_CHA_ACTIVE_LINE_LENGTH_LOW_REG,951mode->hdisplay);952ti_sn65dsi86_write_u16(pdata, SN_CHA_VERTICAL_DISPLAY_SIZE_LOW_REG,953mode->vdisplay);954regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_LOW_REG,955(mode->hsync_end - mode->hsync_start) & 0xFF);956regmap_write(pdata->regmap, SN_CHA_HSYNC_PULSE_WIDTH_HIGH_REG,957(((mode->hsync_end - mode->hsync_start) >> 8) & 0x7F) |958hsync_polarity);959regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_LOW_REG,960(mode->vsync_end - mode->vsync_start) & 0xFF);961regmap_write(pdata->regmap, SN_CHA_VSYNC_PULSE_WIDTH_HIGH_REG,962(((mode->vsync_end - mode->vsync_start) >> 8) & 0x7F) |963vsync_polarity);964965regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_BACK_PORCH_REG,966(mode->htotal - mode->hsync_end) & 0xFF);967regmap_write(pdata->regmap, SN_CHA_VERTICAL_BACK_PORCH_REG,968(mode->vtotal - mode->vsync_end) & 0xFF);969970regmap_write(pdata->regmap, SN_CHA_HORIZONTAL_FRONT_PORCH_REG,971(mode->hsync_start - mode->hdisplay) & 0xFF);972regmap_write(pdata->regmap, SN_CHA_VERTICAL_FRONT_PORCH_REG,973(mode->vsync_start - mode->vdisplay) & 0xFF);974975usleep_range(10000, 10500); /* 10ms delay recommended by spec */976}977978static unsigned int ti_sn_get_max_lanes(struct ti_sn65dsi86 *pdata)979{980u8 data;981int ret;982983ret = drm_dp_dpcd_readb(&pdata->aux, DP_MAX_LANE_COUNT, &data);984if (ret != 1) {985DRM_DEV_ERROR(pdata->dev,986"Can't read lane count (%d); assuming 4\n", ret);987return 4;988}989990return data & DP_LANE_COUNT_MASK;991}992993static int ti_sn_link_training(struct ti_sn65dsi86 *pdata, int dp_rate_idx,994const char **last_err_str)995{996unsigned int val;997int ret;998int i;9991000/* set dp clk frequency value */1001regmap_update_bits(pdata->regmap, SN_DATARATE_CONFIG_REG,1002DP_DATARATE_MASK, DP_DATARATE(dp_rate_idx));10031004/* enable DP PLL */1005regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 1);10061007ret = regmap_read_poll_timeout(pdata->regmap, SN_DPPLL_SRC_REG, val,1008val & DPPLL_SRC_DP_PLL_LOCK, 1000,100950 * 1000);1010if (ret) {1011*last_err_str = "DP_PLL_LOCK polling failed";1012goto exit;1013}10141015/*1016* We'll try to link train several times. As part of link training1017* the bridge chip will write DP_SET_POWER_D0 to DP_SET_POWER. If1018* the panel isn't ready quite it might respond NAK here which means1019* we need to try again.1020*/1021for (i = 0; i < SN_LINK_TRAINING_TRIES; i++) {1022/* Semi auto link training mode */1023regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0x0A);1024ret = regmap_read_poll_timeout(pdata->regmap, SN_ML_TX_MODE_REG, val,1025val == ML_TX_MAIN_LINK_OFF ||1026val == ML_TX_NORMAL_MODE, 1000,1027500 * 1000);1028if (ret) {1029*last_err_str = "Training complete polling failed";1030} else if (val == ML_TX_MAIN_LINK_OFF) {1031*last_err_str = "Link training failed, link is off";1032ret = -EIO;1033continue;1034}10351036break;1037}10381039/* If we saw quite a few retries, add a note about it */1040if (!ret && i > SN_LINK_TRAINING_TRIES / 2)1041DRM_DEV_INFO(pdata->dev, "Link training needed %d retries\n", i);10421043exit:1044/* Disable the PLL if we failed */1045if (ret)1046regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);10471048return ret;1049}10501051static void ti_sn_bridge_atomic_enable(struct drm_bridge *bridge,1052struct drm_atomic_state *state)1053{1054struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1055struct drm_connector *connector;1056const char *last_err_str = "No supported DP rate";1057unsigned int valid_rates;1058int dp_rate_idx;1059unsigned int val;1060int ret = -EINVAL;1061int max_dp_lanes;1062unsigned int bpp;10631064connector = drm_atomic_get_new_connector_for_encoder(state,1065bridge->encoder);1066if (!connector) {1067dev_err_ratelimited(pdata->dev, "Could not get the connector\n");1068return;1069}10701071max_dp_lanes = ti_sn_get_max_lanes(pdata);1072pdata->dp_lanes = min(pdata->dp_lanes, max_dp_lanes);10731074/* DSI_A lane config */1075val = CHA_DSI_LANES(SN_MAX_DP_LANES - pdata->dsi->lanes);1076regmap_update_bits(pdata->regmap, SN_DSI_LANES_REG,1077CHA_DSI_LANES_MASK, val);10781079regmap_write(pdata->regmap, SN_LN_ASSIGN_REG, pdata->ln_assign);1080regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, LN_POLRS_MASK,1081pdata->ln_polrs << LN_POLRS_OFFSET);10821083/* set dsi clk frequency value */1084ti_sn_bridge_set_dsi_rate(pdata, state);10851086/*1087* The SN65DSI86 only supports ASSR Display Authentication method and1088* this method is enabled for eDP panels. An eDP panel must support this1089* authentication method. We need to enable this method in the eDP panel1090* at DisplayPort address 0x0010A prior to link training.1091*1092* As only ASSR is supported by SN65DSI86, for full DisplayPort displays1093* we need to disable the scrambler.1094*/1095if (pdata->bridge.type == DRM_MODE_CONNECTOR_eDP) {1096drm_dp_dpcd_writeb(&pdata->aux, DP_EDP_CONFIGURATION_SET,1097DP_ALTERNATE_SCRAMBLER_RESET_ENABLE);10981099regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,1100SCRAMBLE_DISABLE, 0);1101} else {1102regmap_update_bits(pdata->regmap, SN_TRAINING_SETTING_REG,1103SCRAMBLE_DISABLE, SCRAMBLE_DISABLE);1104}11051106bpp = ti_sn_bridge_get_bpp(connector);1107/* Set the DP output format (18 bpp or 24 bpp) */1108val = bpp == 18 ? BPP_18_RGB : 0;1109regmap_update_bits(pdata->regmap, SN_DATA_FORMAT_REG, BPP_18_RGB, val);11101111/* DP lane config */1112val = DP_NUM_LANES(min(pdata->dp_lanes, 3));1113regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK,1114val);11151116valid_rates = ti_sn_bridge_read_valid_rates(pdata);11171118/* Train until we run out of rates */1119for (dp_rate_idx = ti_sn_bridge_calc_min_dp_rate_idx(pdata, state, bpp);1120dp_rate_idx < ARRAY_SIZE(ti_sn_bridge_dp_rate_lut);1121dp_rate_idx++) {1122if (!(valid_rates & BIT(dp_rate_idx)))1123continue;11241125ret = ti_sn_link_training(pdata, dp_rate_idx, &last_err_str);1126if (!ret)1127break;1128}1129if (ret) {1130DRM_DEV_ERROR(pdata->dev, "%s (%d)\n", last_err_str, ret);1131return;1132}11331134/* config video parameters */1135ti_sn_bridge_set_video_timings(pdata, state);11361137/* enable video stream */1138regmap_update_bits(pdata->regmap, SN_ENH_FRAME_REG, VSTREAM_ENABLE,1139VSTREAM_ENABLE);1140}11411142static void ti_sn_bridge_atomic_pre_enable(struct drm_bridge *bridge,1143struct drm_atomic_state *state)1144{1145struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);11461147pm_runtime_get_sync(pdata->dev);11481149if (!pdata->refclk)1150ti_sn65dsi86_enable_comms(pdata, state);11511152/* td7: min 100 us after enable before DSI data */1153usleep_range(100, 110);1154}11551156static void ti_sn_bridge_atomic_post_disable(struct drm_bridge *bridge,1157struct drm_atomic_state *state)1158{1159struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);11601161/* semi auto link training mode OFF */1162regmap_write(pdata->regmap, SN_ML_TX_MODE_REG, 0);1163/* Num lanes to 0 as per power sequencing in data sheet */1164regmap_update_bits(pdata->regmap, SN_SSC_CONFIG_REG, DP_NUM_LANES_MASK, 0);1165/* disable DP PLL */1166regmap_write(pdata->regmap, SN_PLL_ENABLE_REG, 0);11671168if (!pdata->refclk)1169ti_sn65dsi86_disable_comms(pdata);11701171pm_runtime_put_sync(pdata->dev);1172}11731174static enum drm_connector_status1175ti_sn_bridge_detect(struct drm_bridge *bridge, struct drm_connector *connector)1176{1177struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1178int val = 0;11791180/*1181* Runtime reference is grabbed in ti_sn_bridge_hpd_enable()1182* as the chip won't report HPD just after being powered on.1183* HPD_DEBOUNCED_STATE reflects correct state only after the1184* debounce time (~100-400 ms).1185*/11861187regmap_read(pdata->regmap, SN_HPD_DISABLE_REG, &val);11881189return val & HPD_DEBOUNCED_STATE ? connector_status_connected1190: connector_status_disconnected;1191}11921193static const struct drm_edid *ti_sn_bridge_edid_read(struct drm_bridge *bridge,1194struct drm_connector *connector)1195{1196struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);11971198return drm_edid_read_ddc(connector, &pdata->aux.ddc);1199}12001201static void ti_sn65dsi86_debugfs_init(struct drm_bridge *bridge, struct dentry *root)1202{1203struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);1204struct dentry *debugfs;12051206debugfs = debugfs_create_dir(dev_name(pdata->dev), root);1207debugfs_create_file("status", 0600, debugfs, pdata, &status_fops);1208}12091210static void ti_sn_bridge_hpd_enable(struct drm_bridge *bridge)1211{1212struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);12131214/*1215* Device needs to be powered on before reading the HPD state1216* for reliable hpd detection in ti_sn_bridge_detect() due to1217* the high debounce time.1218*/12191220pm_runtime_get_sync(pdata->dev);1221}12221223static void ti_sn_bridge_hpd_disable(struct drm_bridge *bridge)1224{1225struct ti_sn65dsi86 *pdata = bridge_to_ti_sn65dsi86(bridge);12261227pm_runtime_put_autosuspend(pdata->dev);1228}12291230static const struct drm_bridge_funcs ti_sn_bridge_funcs = {1231.attach = ti_sn_bridge_attach,1232.detach = ti_sn_bridge_detach,1233.mode_valid = ti_sn_bridge_mode_valid,1234.edid_read = ti_sn_bridge_edid_read,1235.detect = ti_sn_bridge_detect,1236.atomic_pre_enable = ti_sn_bridge_atomic_pre_enable,1237.atomic_enable = ti_sn_bridge_atomic_enable,1238.atomic_disable = ti_sn_bridge_atomic_disable,1239.atomic_post_disable = ti_sn_bridge_atomic_post_disable,1240.atomic_reset = drm_atomic_helper_bridge_reset,1241.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,1242.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,1243.debugfs_init = ti_sn65dsi86_debugfs_init,1244.hpd_enable = ti_sn_bridge_hpd_enable,1245.hpd_disable = ti_sn_bridge_hpd_disable,1246};12471248static void ti_sn_bridge_parse_lanes(struct ti_sn65dsi86 *pdata,1249struct device_node *np)1250{1251u32 lane_assignments[SN_MAX_DP_LANES] = { 0, 1, 2, 3 };1252u32 lane_polarities[SN_MAX_DP_LANES] = { };1253struct device_node *endpoint;1254u8 ln_assign = 0;1255u8 ln_polrs = 0;1256int dp_lanes;1257int i;12581259/*1260* Read config from the device tree about lane remapping and lane1261* polarities. These are optional and we assume identity map and1262* normal polarity if nothing is specified. It's OK to specify just1263* data-lanes but not lane-polarities but not vice versa.1264*1265* Error checking is light (we just make sure we don't crash or1266* buffer overrun) and we assume dts is well formed and specifying1267* mappings that the hardware supports.1268*/1269endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);1270dp_lanes = drm_of_get_data_lanes_count(endpoint, 1, SN_MAX_DP_LANES);1271if (dp_lanes > 0) {1272of_property_read_u32_array(endpoint, "data-lanes",1273lane_assignments, dp_lanes);1274of_property_read_u32_array(endpoint, "lane-polarities",1275lane_polarities, dp_lanes);1276} else {1277dp_lanes = SN_MAX_DP_LANES;1278}1279of_node_put(endpoint);12801281/*1282* Convert into register format. Loop over all lanes even if1283* data-lanes had fewer elements so that we nicely initialize1284* the LN_ASSIGN register.1285*/1286for (i = SN_MAX_DP_LANES - 1; i >= 0; i--) {1287ln_assign = ln_assign << LN_ASSIGN_WIDTH | lane_assignments[i];1288ln_polrs = ln_polrs << 1 | lane_polarities[i];1289}12901291/* Stash in our struct for when we power on */1292pdata->dp_lanes = dp_lanes;1293pdata->ln_assign = ln_assign;1294pdata->ln_polrs = ln_polrs;1295}12961297static int ti_sn_bridge_parse_dsi_host(struct ti_sn65dsi86 *pdata)1298{1299struct device_node *np = pdata->dev->of_node;13001301pdata->host_node = of_graph_get_remote_node(np, 0, 0);13021303if (!pdata->host_node) {1304DRM_ERROR("remote dsi host node not found\n");1305return -ENODEV;1306}13071308return 0;1309}13101311static int ti_sn_bridge_probe(struct auxiliary_device *adev,1312const struct auxiliary_device_id *id)1313{1314struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);1315struct device_node *np = pdata->dev->of_node;1316int ret;13171318pdata->next_bridge = devm_drm_of_get_bridge(&adev->dev, np, 1, 0);1319if (IS_ERR(pdata->next_bridge))1320return dev_err_probe(&adev->dev, PTR_ERR(pdata->next_bridge),1321"failed to create panel bridge\n");13221323ti_sn_bridge_parse_lanes(pdata, np);13241325ret = ti_sn_bridge_parse_dsi_host(pdata);1326if (ret)1327return ret;13281329pdata->bridge.of_node = np;1330pdata->bridge.type = pdata->next_bridge->type == DRM_MODE_CONNECTOR_DisplayPort1331? DRM_MODE_CONNECTOR_DisplayPort : DRM_MODE_CONNECTOR_eDP;13321333if (pdata->bridge.type == DRM_MODE_CONNECTOR_DisplayPort) {1334pdata->bridge.ops = DRM_BRIDGE_OP_EDID | DRM_BRIDGE_OP_DETECT |1335DRM_BRIDGE_OP_HPD;1336/*1337* If comms were already enabled they would have been enabled1338* with the wrong value of HPD_DISABLE. Update it now. Comms1339* could be enabled if anyone is holding a pm_runtime reference1340* (like if a GPIO is in use). Note that in most cases nobody1341* is doing AUX channel xfers before the bridge is added so1342* HPD doesn't _really_ matter then. The only exception is in1343* the eDP case where the panel wants to read the EDID before1344* the bridge is added. We always consistently have HPD disabled1345* for eDP.1346*/1347mutex_lock(&pdata->comms_mutex);1348if (pdata->comms_enabled)1349regmap_update_bits(pdata->regmap, SN_HPD_DISABLE_REG,1350HPD_DISABLE, 0);1351mutex_unlock(&pdata->comms_mutex);1352}13531354drm_bridge_add(&pdata->bridge);13551356ret = ti_sn_attach_host(adev, pdata);1357if (ret) {1358dev_err_probe(&adev->dev, ret, "failed to attach dsi host\n");1359goto err_remove_bridge;1360}13611362return 0;13631364err_remove_bridge:1365drm_bridge_remove(&pdata->bridge);1366return ret;1367}13681369static void ti_sn_bridge_remove(struct auxiliary_device *adev)1370{1371struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);13721373if (!pdata)1374return;13751376drm_bridge_remove(&pdata->bridge);13771378of_node_put(pdata->host_node);1379}13801381static const struct auxiliary_device_id ti_sn_bridge_id_table[] = {1382{ .name = "ti_sn65dsi86.bridge", },1383{},1384};13851386static struct auxiliary_driver ti_sn_bridge_driver = {1387.name = "bridge",1388.probe = ti_sn_bridge_probe,1389.remove = ti_sn_bridge_remove,1390.id_table = ti_sn_bridge_id_table,1391};13921393/* -----------------------------------------------------------------------------1394* PWM Controller1395*/1396#if IS_REACHABLE(CONFIG_PWM)1397static int ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata)1398{1399return atomic_xchg(&pdata->pwm_pin_busy, 1) ? -EBUSY : 0;1400}14011402static void ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata)1403{1404atomic_set(&pdata->pwm_pin_busy, 0);1405}14061407static struct ti_sn65dsi86 *pwm_chip_to_ti_sn_bridge(struct pwm_chip *chip)1408{1409return pwmchip_get_drvdata(chip);1410}14111412static int ti_sn_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)1413{1414struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);14151416return ti_sn_pwm_pin_request(pdata);1417}14181419static void ti_sn_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)1420{1421struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);14221423ti_sn_pwm_pin_release(pdata);1424}14251426/*1427* Limitations:1428* - The PWM signal is not driven when the chip is powered down, or in its1429* reset state and the driver does not implement the "suspend state"1430* described in the documentation. In order to save power, state->enabled is1431* interpreted as denoting if the signal is expected to be valid, and is used1432* to determine if the chip needs to be kept powered.1433* - Changing both period and duty_cycle is not done atomically, neither is the1434* multi-byte register updates, so the output might briefly be undefined1435* during update.1436*/1437static int ti_sn_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,1438const struct pwm_state *state)1439{1440struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);1441unsigned int pwm_en_inv;1442unsigned int backlight;1443unsigned int pre_div;1444unsigned int scale;1445u64 period_max;1446u64 period;1447int ret;14481449if (!pdata->pwm_enabled) {1450ret = pm_runtime_resume_and_get(pwmchip_parent(chip));1451if (ret < 0)1452return ret;1453}14541455if (state->enabled) {1456if (!pdata->pwm_enabled) {1457/*1458* The chip might have been powered down while we1459* didn't hold a PM runtime reference, so mux in the1460* PWM function on the GPIO pin again.1461*/1462ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1463SN_GPIO_MUX_MASK << (2 * SN_PWM_GPIO_IDX),1464SN_GPIO_MUX_SPECIAL << (2 * SN_PWM_GPIO_IDX));1465if (ret) {1466dev_err(pwmchip_parent(chip), "failed to mux in PWM function\n");1467goto out;1468}1469}14701471/*1472* Per the datasheet the PWM frequency is given by:1473*1474* REFCLK_FREQ1475* PWM_FREQ = -----------------------------------1476* PWM_PRE_DIV * BACKLIGHT_SCALE + 11477*1478* However, after careful review the author is convinced that1479* the documentation has lost some parenthesis around1480* "BACKLIGHT_SCALE + 1".1481*1482* With the period T_pwm = 1/PWM_FREQ this can be written:1483*1484* T_pwm * REFCLK_FREQ = PWM_PRE_DIV * (BACKLIGHT_SCALE + 1)1485*1486* In order to keep BACKLIGHT_SCALE within its 16 bits,1487* PWM_PRE_DIV must be:1488*1489* T_pwm * REFCLK_FREQ1490* PWM_PRE_DIV >= -------------------------1491* BACKLIGHT_SCALE_MAX + 11492*1493* To simplify the search and to favour higher resolution of1494* the duty cycle over accuracy of the period, the lowest1495* possible PWM_PRE_DIV is used. Finally the scale is1496* calculated as:1497*1498* T_pwm * REFCLK_FREQ1499* BACKLIGHT_SCALE = ---------------------- - 11500* PWM_PRE_DIV1501*1502* Here T_pwm is represented in seconds, so appropriate scaling1503* to nanoseconds is necessary.1504*/15051506/* Minimum T_pwm is 1 / REFCLK_FREQ */1507if (state->period <= NSEC_PER_SEC / pdata->pwm_refclk_freq) {1508ret = -EINVAL;1509goto out;1510}15111512/*1513* Maximum T_pwm is 255 * (65535 + 1) / REFCLK_FREQ1514* Limit period to this to avoid overflows1515*/1516period_max = div_u64((u64)NSEC_PER_SEC * 255 * (65535 + 1),1517pdata->pwm_refclk_freq);1518period = min(state->period, period_max);15191520pre_div = DIV64_U64_ROUND_UP(period * pdata->pwm_refclk_freq,1521(u64)NSEC_PER_SEC * (BACKLIGHT_SCALE_MAX + 1));1522scale = div64_u64(period * pdata->pwm_refclk_freq, (u64)NSEC_PER_SEC * pre_div) - 1;15231524/*1525* The documentation has the duty ratio given as:1526*1527* duty BACKLIGHT1528* ------- = ---------------------1529* period BACKLIGHT_SCALE + 11530*1531* Solve for BACKLIGHT, substituting BACKLIGHT_SCALE according1532* to definition above and adjusting for nanosecond1533* representation of duty cycle gives us:1534*/1535backlight = div64_u64(state->duty_cycle * pdata->pwm_refclk_freq,1536(u64)NSEC_PER_SEC * pre_div);1537if (backlight > scale)1538backlight = scale;15391540ret = regmap_write(pdata->regmap, SN_PWM_PRE_DIV_REG, pre_div);1541if (ret) {1542dev_err(pwmchip_parent(chip), "failed to update PWM_PRE_DIV\n");1543goto out;1544}15451546ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_SCALE_REG, scale);1547ti_sn65dsi86_write_u16(pdata, SN_BACKLIGHT_REG, backlight);1548}15491550pwm_en_inv = FIELD_PREP(SN_PWM_EN_MASK, state->enabled) |1551FIELD_PREP(SN_PWM_INV_MASK, state->polarity == PWM_POLARITY_INVERSED);1552ret = regmap_write(pdata->regmap, SN_PWM_EN_INV_REG, pwm_en_inv);1553if (ret) {1554dev_err(pwmchip_parent(chip), "failed to update PWM_EN/PWM_INV\n");1555goto out;1556}15571558pdata->pwm_enabled = state->enabled;1559out:15601561if (!pdata->pwm_enabled)1562pm_runtime_put_sync(pwmchip_parent(chip));15631564return ret;1565}15661567static int ti_sn_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,1568struct pwm_state *state)1569{1570struct ti_sn65dsi86 *pdata = pwm_chip_to_ti_sn_bridge(chip);1571unsigned int pwm_en_inv;1572unsigned int pre_div;1573u16 backlight;1574u16 scale;1575int ret;15761577ret = regmap_read(pdata->regmap, SN_PWM_EN_INV_REG, &pwm_en_inv);1578if (ret)1579return ret;15801581ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_SCALE_REG, &scale);1582if (ret)1583return ret;15841585ret = ti_sn65dsi86_read_u16(pdata, SN_BACKLIGHT_REG, &backlight);1586if (ret)1587return ret;15881589ret = regmap_read(pdata->regmap, SN_PWM_PRE_DIV_REG, &pre_div);1590if (ret)1591return ret;15921593state->enabled = FIELD_GET(SN_PWM_EN_MASK, pwm_en_inv);1594if (FIELD_GET(SN_PWM_INV_MASK, pwm_en_inv))1595state->polarity = PWM_POLARITY_INVERSED;1596else1597state->polarity = PWM_POLARITY_NORMAL;15981599state->period = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * (scale + 1),1600pdata->pwm_refclk_freq);1601state->duty_cycle = DIV_ROUND_UP_ULL((u64)NSEC_PER_SEC * pre_div * backlight,1602pdata->pwm_refclk_freq);16031604if (state->duty_cycle > state->period)1605state->duty_cycle = state->period;16061607return 0;1608}16091610static const struct pwm_ops ti_sn_pwm_ops = {1611.request = ti_sn_pwm_request,1612.free = ti_sn_pwm_free,1613.apply = ti_sn_pwm_apply,1614.get_state = ti_sn_pwm_get_state,1615};16161617static int ti_sn_pwm_probe(struct auxiliary_device *adev,1618const struct auxiliary_device_id *id)1619{1620struct pwm_chip *chip;1621struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);16221623pdata->pchip = chip = devm_pwmchip_alloc(&adev->dev, 1, 0);1624if (IS_ERR(chip))1625return PTR_ERR(chip);16261627pwmchip_set_drvdata(chip, pdata);16281629chip->ops = &ti_sn_pwm_ops;1630chip->of_xlate = of_pwm_single_xlate;16311632devm_pm_runtime_enable(&adev->dev);16331634return pwmchip_add(chip);1635}16361637static void ti_sn_pwm_remove(struct auxiliary_device *adev)1638{1639struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);16401641pwmchip_remove(pdata->pchip);16421643if (pdata->pwm_enabled)1644pm_runtime_put_sync(&adev->dev);1645}16461647static const struct auxiliary_device_id ti_sn_pwm_id_table[] = {1648{ .name = "ti_sn65dsi86.pwm", },1649{},1650};16511652static struct auxiliary_driver ti_sn_pwm_driver = {1653.name = "pwm",1654.probe = ti_sn_pwm_probe,1655.remove = ti_sn_pwm_remove,1656.id_table = ti_sn_pwm_id_table,1657};16581659static int __init ti_sn_pwm_register(void)1660{1661return auxiliary_driver_register(&ti_sn_pwm_driver);1662}16631664static void ti_sn_pwm_unregister(void)1665{1666auxiliary_driver_unregister(&ti_sn_pwm_driver);1667}16681669#else1670static inline int __maybe_unused ti_sn_pwm_pin_request(struct ti_sn65dsi86 *pdata) { return 0; }1671static inline void __maybe_unused ti_sn_pwm_pin_release(struct ti_sn65dsi86 *pdata) {}16721673static inline int ti_sn_pwm_register(void) { return 0; }1674static inline void ti_sn_pwm_unregister(void) {}1675#endif16761677/* -----------------------------------------------------------------------------1678* GPIO Controller1679*/1680#if defined(CONFIG_OF_GPIO)16811682static int tn_sn_bridge_of_xlate(struct gpio_chip *chip,1683const struct of_phandle_args *gpiospec,1684u32 *flags)1685{1686if (WARN_ON(gpiospec->args_count < chip->of_gpio_n_cells))1687return -EINVAL;16881689if (gpiospec->args[0] > chip->ngpio || gpiospec->args[0] < 1)1690return -EINVAL;16911692if (flags)1693*flags = gpiospec->args[1];16941695return gpiospec->args[0] - SN_GPIO_PHYSICAL_OFFSET;1696}16971698static int ti_sn_bridge_gpio_get_direction(struct gpio_chip *chip,1699unsigned int offset)1700{1701struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);17021703/*1704* We already have to keep track of the direction because we use1705* that to figure out whether we've powered the device. We can1706* just return that rather than (maybe) powering up the device1707* to ask its direction.1708*/1709return test_bit(offset, pdata->gchip_output) ?1710GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN;1711}17121713static int ti_sn_bridge_gpio_get(struct gpio_chip *chip, unsigned int offset)1714{1715struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1716unsigned int val;1717int ret;17181719/*1720* When the pin is an input we don't forcibly keep the bridge1721* powered--we just power it on to read the pin. NOTE: part of1722* the reason this works is that the bridge defaults (when1723* powered back on) to all 4 GPIOs being configured as GPIO input.1724* Also note that if something else is keeping the chip powered the1725* pm_runtime functions are lightweight increments of a refcount.1726*/1727pm_runtime_get_sync(pdata->dev);1728ret = regmap_read(pdata->regmap, SN_GPIO_IO_REG, &val);1729pm_runtime_put_autosuspend(pdata->dev);17301731if (ret)1732return ret;17331734return !!(val & BIT(SN_GPIO_INPUT_SHIFT + offset));1735}17361737static int ti_sn_bridge_gpio_set(struct gpio_chip *chip, unsigned int offset,1738int val)1739{1740struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);17411742val &= 1;1743return regmap_update_bits(pdata->regmap, SN_GPIO_IO_REG,1744BIT(SN_GPIO_OUTPUT_SHIFT + offset),1745val << (SN_GPIO_OUTPUT_SHIFT + offset));1746}17471748static int ti_sn_bridge_gpio_direction_input(struct gpio_chip *chip,1749unsigned int offset)1750{1751struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1752int shift = offset * 2;1753int ret;17541755if (!test_and_clear_bit(offset, pdata->gchip_output))1756return 0;17571758ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1759SN_GPIO_MUX_MASK << shift,1760SN_GPIO_MUX_INPUT << shift);1761if (ret) {1762set_bit(offset, pdata->gchip_output);1763return ret;1764}17651766/*1767* NOTE: if nobody else is powering the device this may fully power1768* it off and when it comes back it will have lost all state, but1769* that's OK because the default is input and we're now an input.1770*/1771pm_runtime_put_autosuspend(pdata->dev);17721773return 0;1774}17751776static int ti_sn_bridge_gpio_direction_output(struct gpio_chip *chip,1777unsigned int offset, int val)1778{1779struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);1780int shift = offset * 2;1781int ret;17821783if (test_and_set_bit(offset, pdata->gchip_output))1784return 0;17851786pm_runtime_get_sync(pdata->dev);17871788/* Set value first to avoid glitching */1789ti_sn_bridge_gpio_set(chip, offset, val);17901791/* Set direction */1792ret = regmap_update_bits(pdata->regmap, SN_GPIO_CTRL_REG,1793SN_GPIO_MUX_MASK << shift,1794SN_GPIO_MUX_OUTPUT << shift);1795if (ret) {1796clear_bit(offset, pdata->gchip_output);1797pm_runtime_put_autosuspend(pdata->dev);1798}17991800return ret;1801}18021803static int ti_sn_bridge_gpio_request(struct gpio_chip *chip, unsigned int offset)1804{1805struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);18061807if (offset == SN_PWM_GPIO_IDX)1808return ti_sn_pwm_pin_request(pdata);18091810return 0;1811}18121813static void ti_sn_bridge_gpio_free(struct gpio_chip *chip, unsigned int offset)1814{1815struct ti_sn65dsi86 *pdata = gpiochip_get_data(chip);18161817/* We won't keep pm_runtime if we're input, so switch there on free */1818ti_sn_bridge_gpio_direction_input(chip, offset);18191820if (offset == SN_PWM_GPIO_IDX)1821ti_sn_pwm_pin_release(pdata);1822}18231824static const char * const ti_sn_bridge_gpio_names[SN_NUM_GPIOS] = {1825"GPIO1", "GPIO2", "GPIO3", "GPIO4"1826};18271828static int ti_sn_gpio_probe(struct auxiliary_device *adev,1829const struct auxiliary_device_id *id)1830{1831struct ti_sn65dsi86 *pdata = dev_get_drvdata(adev->dev.parent);1832int ret;18331834/* Only init if someone is going to use us as a GPIO controller */1835if (!of_property_read_bool(pdata->dev->of_node, "gpio-controller"))1836return 0;18371838pdata->gchip.label = dev_name(pdata->dev);1839pdata->gchip.parent = pdata->dev;1840pdata->gchip.owner = THIS_MODULE;1841pdata->gchip.of_xlate = tn_sn_bridge_of_xlate;1842pdata->gchip.of_gpio_n_cells = 2;1843pdata->gchip.request = ti_sn_bridge_gpio_request;1844pdata->gchip.free = ti_sn_bridge_gpio_free;1845pdata->gchip.get_direction = ti_sn_bridge_gpio_get_direction;1846pdata->gchip.direction_input = ti_sn_bridge_gpio_direction_input;1847pdata->gchip.direction_output = ti_sn_bridge_gpio_direction_output;1848pdata->gchip.get = ti_sn_bridge_gpio_get;1849pdata->gchip.set = ti_sn_bridge_gpio_set;1850pdata->gchip.can_sleep = true;1851pdata->gchip.names = ti_sn_bridge_gpio_names;1852pdata->gchip.ngpio = SN_NUM_GPIOS;1853pdata->gchip.base = -1;1854ret = devm_gpiochip_add_data(&adev->dev, &pdata->gchip, pdata);1855if (ret)1856dev_err(pdata->dev, "can't add gpio chip\n");18571858return ret;1859}18601861static const struct auxiliary_device_id ti_sn_gpio_id_table[] = {1862{ .name = "ti_sn65dsi86.gpio", },1863{},1864};18651866MODULE_DEVICE_TABLE(auxiliary, ti_sn_gpio_id_table);18671868static struct auxiliary_driver ti_sn_gpio_driver = {1869.name = "gpio",1870.probe = ti_sn_gpio_probe,1871.id_table = ti_sn_gpio_id_table,1872};18731874static int __init ti_sn_gpio_register(void)1875{1876return auxiliary_driver_register(&ti_sn_gpio_driver);1877}18781879static void ti_sn_gpio_unregister(void)1880{1881auxiliary_driver_unregister(&ti_sn_gpio_driver);1882}18831884#else18851886static inline int ti_sn_gpio_register(void) { return 0; }1887static inline void ti_sn_gpio_unregister(void) {}18881889#endif18901891/* -----------------------------------------------------------------------------1892* Probe & Remove1893*/18941895static void ti_sn65dsi86_runtime_disable(void *data)1896{1897pm_runtime_dont_use_autosuspend(data);1898pm_runtime_disable(data);1899}19001901static int ti_sn65dsi86_parse_regulators(struct ti_sn65dsi86 *pdata)1902{1903unsigned int i;1904const char * const ti_sn_bridge_supply_names[] = {1905"vcca", "vcc", "vccio", "vpll",1906};19071908for (i = 0; i < SN_REGULATOR_SUPPLY_NUM; i++)1909pdata->supplies[i].supply = ti_sn_bridge_supply_names[i];19101911return devm_regulator_bulk_get(pdata->dev, SN_REGULATOR_SUPPLY_NUM,1912pdata->supplies);1913}19141915static int ti_sn65dsi86_probe(struct i2c_client *client)1916{1917struct device *dev = &client->dev;1918struct ti_sn65dsi86 *pdata;1919u8 id_buf[8];1920int ret;19211922if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {1923DRM_ERROR("device doesn't support I2C\n");1924return -ENODEV;1925}19261927pdata = devm_drm_bridge_alloc(dev, struct ti_sn65dsi86, bridge, &ti_sn_bridge_funcs);1928if (IS_ERR(pdata))1929return PTR_ERR(pdata);1930dev_set_drvdata(dev, pdata);1931pdata->dev = dev;19321933mutex_init(&pdata->comms_mutex);19341935pdata->regmap = devm_regmap_init_i2c(client,1936&ti_sn65dsi86_regmap_config);1937if (IS_ERR(pdata->regmap))1938return dev_err_probe(dev, PTR_ERR(pdata->regmap),1939"regmap i2c init failed\n");19401941pdata->enable_gpio = devm_gpiod_get_optional(dev, "enable",1942GPIOD_OUT_LOW);1943if (IS_ERR(pdata->enable_gpio))1944return dev_err_probe(dev, PTR_ERR(pdata->enable_gpio),1945"failed to get enable gpio from DT\n");19461947ret = ti_sn65dsi86_parse_regulators(pdata);1948if (ret)1949return dev_err_probe(dev, ret, "failed to parse regulators\n");19501951pdata->refclk = devm_clk_get_optional(dev, "refclk");1952if (IS_ERR(pdata->refclk))1953return dev_err_probe(dev, PTR_ERR(pdata->refclk),1954"failed to get reference clock\n");19551956pm_runtime_enable(dev);1957pm_runtime_set_autosuspend_delay(pdata->dev, 500);1958pm_runtime_use_autosuspend(pdata->dev);1959ret = devm_add_action_or_reset(dev, ti_sn65dsi86_runtime_disable, dev);1960if (ret)1961return ret;19621963pm_runtime_get_sync(dev);1964ret = regmap_bulk_read(pdata->regmap, SN_DEVICE_ID_REGS, id_buf, ARRAY_SIZE(id_buf));1965pm_runtime_put_autosuspend(dev);1966if (ret)1967return dev_err_probe(dev, ret, "failed to read device id\n");19681969/* The ID string is stored backwards */1970if (strncmp(id_buf, "68ISD ", ARRAY_SIZE(id_buf)))1971return dev_err_probe(dev, -EOPNOTSUPP, "unsupported device id\n");19721973/*1974* Break ourselves up into a collection of aux devices. The only real1975* motiviation here is to solve the chicken-and-egg problem of probe1976* ordering. The bridge wants the panel to be there when it probes.1977* The panel wants its HPD GPIO (provided by sn65dsi86 on some boards)1978* when it probes. The panel and maybe backlight might want the DDC1979* bus or the pwm_chip. Having sub-devices allows the some sub devices1980* to finish probing even if others return -EPROBE_DEFER and gets us1981* around the problems.1982*/19831984if (IS_ENABLED(CONFIG_OF_GPIO)) {1985ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->gpio_aux, "gpio");1986if (ret)1987return ret;1988}19891990if (IS_REACHABLE(CONFIG_PWM)) {1991ret = ti_sn65dsi86_add_aux_device(pdata, &pdata->pwm_aux, "pwm");1992if (ret)1993return ret;1994}19951996/*1997* NOTE: At the end of the AUX channel probe we'll add the aux device1998* for the bridge. This is because the bridge can't be used until the1999* AUX channel is there and this is a very simple solution to the2000* dependency problem.2001*/2002return ti_sn65dsi86_add_aux_device(pdata, &pdata->aux_aux, "aux");2003}20042005static const struct i2c_device_id ti_sn65dsi86_id[] = {2006{ "ti,sn65dsi86" },2007{}2008};2009MODULE_DEVICE_TABLE(i2c, ti_sn65dsi86_id);20102011static const struct of_device_id ti_sn65dsi86_match_table[] = {2012{.compatible = "ti,sn65dsi86"},2013{},2014};2015MODULE_DEVICE_TABLE(of, ti_sn65dsi86_match_table);20162017static struct i2c_driver ti_sn65dsi86_driver = {2018.driver = {2019.name = "ti_sn65dsi86",2020.of_match_table = ti_sn65dsi86_match_table,2021.pm = &ti_sn65dsi86_pm_ops,2022},2023.probe = ti_sn65dsi86_probe,2024.id_table = ti_sn65dsi86_id,2025};20262027static int __init ti_sn65dsi86_init(void)2028{2029int ret;20302031ret = i2c_add_driver(&ti_sn65dsi86_driver);2032if (ret)2033return ret;20342035ret = ti_sn_gpio_register();2036if (ret)2037goto err_main_was_registered;20382039ret = ti_sn_pwm_register();2040if (ret)2041goto err_gpio_was_registered;20422043ret = auxiliary_driver_register(&ti_sn_aux_driver);2044if (ret)2045goto err_pwm_was_registered;20462047ret = auxiliary_driver_register(&ti_sn_bridge_driver);2048if (ret)2049goto err_aux_was_registered;20502051return 0;20522053err_aux_was_registered:2054auxiliary_driver_unregister(&ti_sn_aux_driver);2055err_pwm_was_registered:2056ti_sn_pwm_unregister();2057err_gpio_was_registered:2058ti_sn_gpio_unregister();2059err_main_was_registered:2060i2c_del_driver(&ti_sn65dsi86_driver);20612062return ret;2063}2064module_init(ti_sn65dsi86_init);20652066static void __exit ti_sn65dsi86_exit(void)2067{2068auxiliary_driver_unregister(&ti_sn_bridge_driver);2069auxiliary_driver_unregister(&ti_sn_aux_driver);2070ti_sn_pwm_unregister();2071ti_sn_gpio_unregister();2072i2c_del_driver(&ti_sn65dsi86_driver);2073}2074module_exit(ti_sn65dsi86_exit);20752076MODULE_AUTHOR("Sandeep Panda <[email protected]>");2077MODULE_DESCRIPTION("sn65dsi86 DSI to eDP bridge driver");2078MODULE_LICENSE("GPL v2");207920802081