cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

lontium-lt9611.c (30475B)


      1// SPDX-License-Identifier: GPL-2.0
      2/*
      3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
      4 * Copyright (c) 2019-2020. Linaro Limited.
      5 */
      6
      7#include <linux/gpio/consumer.h>
      8#include <linux/interrupt.h>
      9#include <linux/module.h>
     10#include <linux/of_graph.h>
     11#include <linux/platform_device.h>
     12#include <linux/regmap.h>
     13#include <linux/regulator/consumer.h>
     14
     15#include <sound/hdmi-codec.h>
     16
     17#include <drm/drm_atomic_helper.h>
     18#include <drm/drm_bridge.h>
     19#include <drm/drm_mipi_dsi.h>
     20#include <drm/drm_print.h>
     21#include <drm/drm_probe_helper.h>
     22
     23#define EDID_SEG_SIZE	256
     24#define EDID_LEN	32
     25#define EDID_LOOP	8
     26#define KEY_DDC_ACCS_DONE 0x02
     27#define DDC_NO_ACK	0x50
     28
     29#define LT9611_4LANES	0
     30
     31struct lt9611 {
     32	struct device *dev;
     33	struct drm_bridge bridge;
     34	struct drm_connector connector;
     35
     36	struct regmap *regmap;
     37
     38	struct device_node *dsi0_node;
     39	struct device_node *dsi1_node;
     40	struct mipi_dsi_device *dsi0;
     41	struct mipi_dsi_device *dsi1;
     42	struct platform_device *audio_pdev;
     43
     44	bool ac_mode;
     45
     46	struct gpio_desc *reset_gpio;
     47	struct gpio_desc *enable_gpio;
     48
     49	bool power_on;
     50	bool sleep;
     51
     52	struct regulator_bulk_data supplies[2];
     53
     54	struct i2c_client *client;
     55
     56	enum drm_connector_status status;
     57
     58	u8 edid_buf[EDID_SEG_SIZE];
     59	u32 vic;
     60};
     61
     62#define LT9611_PAGE_CONTROL	0xff
     63
     64static const struct regmap_range_cfg lt9611_ranges[] = {
     65	{
     66		.name = "register_range",
     67		.range_min =  0,
     68		.range_max = 0x85ff,
     69		.selector_reg = LT9611_PAGE_CONTROL,
     70		.selector_mask = 0xff,
     71		.selector_shift = 0,
     72		.window_start = 0,
     73		.window_len = 0x100,
     74	},
     75};
     76
     77static const struct regmap_config lt9611_regmap_config = {
     78	.reg_bits = 8,
     79	.val_bits = 8,
     80	.max_register = 0xffff,
     81	.ranges = lt9611_ranges,
     82	.num_ranges = ARRAY_SIZE(lt9611_ranges),
     83};
     84
     85struct lt9611_mode {
     86	u16 hdisplay;
     87	u16 vdisplay;
     88	u8 vrefresh;
     89	u8 lanes;
     90	u8 intfs;
     91};
     92
     93static struct lt9611_mode lt9611_modes[] = {
     94	{ 3840, 2160, 30, 4, 2 }, /* 3840x2160 24bit 30Hz 4Lane 2ports */
     95	{ 1920, 1080, 60, 4, 1 }, /* 1080P 24bit 60Hz 4lane 1port */
     96	{ 1920, 1080, 30, 3, 1 }, /* 1080P 24bit 30Hz 3lane 1port */
     97	{ 1920, 1080, 24, 3, 1 },
     98	{ 720, 480, 60, 4, 1 },
     99	{ 720, 576, 50, 2, 1 },
    100	{ 640, 480, 60, 2, 1 },
    101};
    102
    103static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
    104{
    105	return container_of(bridge, struct lt9611, bridge);
    106}
    107
    108static struct lt9611 *connector_to_lt9611(struct drm_connector *connector)
    109{
    110	return container_of(connector, struct lt9611, connector);
    111}
    112
    113static int lt9611_mipi_input_analog(struct lt9611 *lt9611)
    114{
    115	const struct reg_sequence reg_cfg[] = {
    116		{ 0x8106, 0x40 }, /* port A rx current */
    117		{ 0x810a, 0xfe }, /* port A ldo voltage set */
    118		{ 0x810b, 0xbf }, /* enable port A lprx */
    119		{ 0x8111, 0x40 }, /* port B rx current */
    120		{ 0x8115, 0xfe }, /* port B ldo voltage set */
    121		{ 0x8116, 0xbf }, /* enable port B lprx */
    122
    123		{ 0x811c, 0x03 }, /* PortA clk lane no-LP mode */
    124		{ 0x8120, 0x03 }, /* PortB clk lane with-LP mode */
    125	};
    126
    127	return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
    128}
    129
    130static int lt9611_mipi_input_digital(struct lt9611 *lt9611,
    131				     const struct drm_display_mode *mode)
    132{
    133	struct reg_sequence reg_cfg[] = {
    134		{ 0x8300, LT9611_4LANES },
    135		{ 0x830a, 0x00 },
    136		{ 0x824f, 0x80 },
    137		{ 0x8250, 0x10 },
    138		{ 0x8302, 0x0a },
    139		{ 0x8306, 0x0a },
    140	};
    141
    142	if (mode->hdisplay == 3840)
    143		reg_cfg[1].def = 0x03;
    144
    145	return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
    146}
    147
    148static void lt9611_mipi_video_setup(struct lt9611 *lt9611,
    149				    const struct drm_display_mode *mode)
    150{
    151	u32 h_total, hactive, hsync_len, hfront_porch, hsync_porch;
    152	u32 v_total, vactive, vsync_len, vfront_porch, vsync_porch;
    153
    154	h_total = mode->htotal;
    155	v_total = mode->vtotal;
    156
    157	hactive = mode->hdisplay;
    158	hsync_len = mode->hsync_end - mode->hsync_start;
    159	hfront_porch = mode->hsync_start - mode->hdisplay;
    160	hsync_porch = hsync_len + mode->htotal - mode->hsync_end;
    161
    162	vactive = mode->vdisplay;
    163	vsync_len = mode->vsync_end - mode->vsync_start;
    164	vfront_porch = mode->vsync_start - mode->vdisplay;
    165	vsync_porch = vsync_len + mode->vtotal - mode->vsync_end;
    166
    167	regmap_write(lt9611->regmap, 0x830d, (u8)(v_total / 256));
    168	regmap_write(lt9611->regmap, 0x830e, (u8)(v_total % 256));
    169
    170	regmap_write(lt9611->regmap, 0x830f, (u8)(vactive / 256));
    171	regmap_write(lt9611->regmap, 0x8310, (u8)(vactive % 256));
    172
    173	regmap_write(lt9611->regmap, 0x8311, (u8)(h_total / 256));
    174	regmap_write(lt9611->regmap, 0x8312, (u8)(h_total % 256));
    175
    176	regmap_write(lt9611->regmap, 0x8313, (u8)(hactive / 256));
    177	regmap_write(lt9611->regmap, 0x8314, (u8)(hactive % 256));
    178
    179	regmap_write(lt9611->regmap, 0x8315, (u8)(vsync_len % 256));
    180	regmap_write(lt9611->regmap, 0x8316, (u8)(hsync_len % 256));
    181
    182	regmap_write(lt9611->regmap, 0x8317, (u8)(vfront_porch % 256));
    183
    184	regmap_write(lt9611->regmap, 0x8318, (u8)(vsync_porch % 256));
    185
    186	regmap_write(lt9611->regmap, 0x8319, (u8)(hfront_porch % 256));
    187
    188	regmap_write(lt9611->regmap, 0x831a, (u8)(hsync_porch / 256));
    189	regmap_write(lt9611->regmap, 0x831b, (u8)(hsync_porch % 256));
    190}
    191
    192static void lt9611_pcr_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode)
    193{
    194	const struct reg_sequence reg_cfg[] = {
    195		{ 0x830b, 0x01 },
    196		{ 0x830c, 0x10 },
    197		{ 0x8348, 0x00 },
    198		{ 0x8349, 0x81 },
    199
    200		/* stage 1 */
    201		{ 0x8321, 0x4a },
    202		{ 0x8324, 0x71 },
    203		{ 0x8325, 0x30 },
    204		{ 0x832a, 0x01 },
    205
    206		/* stage 2 */
    207		{ 0x834a, 0x40 },
    208		{ 0x831d, 0x10 },
    209
    210		/* MK limit */
    211		{ 0x832d, 0x38 },
    212		{ 0x8331, 0x08 },
    213	};
    214	const struct reg_sequence reg_cfg2[] = {
    215		{ 0x830b, 0x03 },
    216		{ 0x830c, 0xd0 },
    217		{ 0x8348, 0x03 },
    218		{ 0x8349, 0xe0 },
    219		{ 0x8324, 0x72 },
    220		{ 0x8325, 0x00 },
    221		{ 0x832a, 0x01 },
    222		{ 0x834a, 0x10 },
    223		{ 0x831d, 0x10 },
    224		{ 0x8326, 0x37 },
    225	};
    226
    227	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
    228
    229	switch (mode->hdisplay) {
    230	case 640:
    231		regmap_write(lt9611->regmap, 0x8326, 0x14);
    232		break;
    233	case 1920:
    234		regmap_write(lt9611->regmap, 0x8326, 0x37);
    235		break;
    236	case 3840:
    237		regmap_multi_reg_write(lt9611->regmap, reg_cfg2, ARRAY_SIZE(reg_cfg2));
    238		break;
    239	}
    240
    241	/* pcr rst */
    242	regmap_write(lt9611->regmap, 0x8011, 0x5a);
    243	regmap_write(lt9611->regmap, 0x8011, 0xfa);
    244}
    245
    246static int lt9611_pll_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode)
    247{
    248	unsigned int pclk = mode->clock;
    249	const struct reg_sequence reg_cfg[] = {
    250		/* txpll init */
    251		{ 0x8123, 0x40 },
    252		{ 0x8124, 0x64 },
    253		{ 0x8125, 0x80 },
    254		{ 0x8126, 0x55 },
    255		{ 0x812c, 0x37 },
    256		{ 0x812f, 0x01 },
    257		{ 0x8126, 0x55 },
    258		{ 0x8127, 0x66 },
    259		{ 0x8128, 0x88 },
    260	};
    261
    262	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
    263
    264	if (pclk > 150000)
    265		regmap_write(lt9611->regmap, 0x812d, 0x88);
    266	else if (pclk > 70000)
    267		regmap_write(lt9611->regmap, 0x812d, 0x99);
    268	else
    269		regmap_write(lt9611->regmap, 0x812d, 0xaa);
    270
    271	/*
    272	 * first divide pclk by 2 first
    273	 *  - write divide by 64k to 19:16 bits which means shift by 17
    274	 *  - write divide by 256 to 15:8 bits which means shift by 9
    275	 *  - write remainder to 7:0 bits, which means shift by 1
    276	 */
    277	regmap_write(lt9611->regmap, 0x82e3, pclk >> 17); /* pclk[19:16] */
    278	regmap_write(lt9611->regmap, 0x82e4, pclk >> 9);  /* pclk[15:8]  */
    279	regmap_write(lt9611->regmap, 0x82e5, pclk >> 1);  /* pclk[7:0]   */
    280
    281	regmap_write(lt9611->regmap, 0x82de, 0x20);
    282	regmap_write(lt9611->regmap, 0x82de, 0xe0);
    283
    284	regmap_write(lt9611->regmap, 0x8016, 0xf1);
    285	regmap_write(lt9611->regmap, 0x8016, 0xf3);
    286
    287	return 0;
    288}
    289
    290static int lt9611_read_video_check(struct lt9611 *lt9611, unsigned int reg)
    291{
    292	unsigned int temp, temp2;
    293	int ret;
    294
    295	ret = regmap_read(lt9611->regmap, reg, &temp);
    296	if (ret)
    297		return ret;
    298	temp <<= 8;
    299	ret = regmap_read(lt9611->regmap, reg + 1, &temp2);
    300	if (ret)
    301		return ret;
    302
    303	return (temp + temp2);
    304}
    305
    306static int lt9611_video_check(struct lt9611 *lt9611)
    307{
    308	u32 v_total, vactive, hactive_a, hactive_b, h_total_sysclk;
    309	int temp;
    310
    311	/* top module video check */
    312
    313	/* vactive */
    314	temp = lt9611_read_video_check(lt9611, 0x8282);
    315	if (temp < 0)
    316		goto end;
    317	vactive = temp;
    318
    319	/* v_total */
    320	temp = lt9611_read_video_check(lt9611, 0x826c);
    321	if (temp < 0)
    322		goto end;
    323	v_total = temp;
    324
    325	/* h_total_sysclk */
    326	temp = lt9611_read_video_check(lt9611, 0x8286);
    327	if (temp < 0)
    328		goto end;
    329	h_total_sysclk = temp;
    330
    331	/* hactive_a */
    332	temp = lt9611_read_video_check(lt9611, 0x8382);
    333	if (temp < 0)
    334		goto end;
    335	hactive_a = temp / 3;
    336
    337	/* hactive_b */
    338	temp = lt9611_read_video_check(lt9611, 0x8386);
    339	if (temp < 0)
    340		goto end;
    341	hactive_b = temp / 3;
    342
    343	dev_info(lt9611->dev,
    344		 "video check: hactive_a=%d, hactive_b=%d, vactive=%d, v_total=%d, h_total_sysclk=%d\n",
    345		 hactive_a, hactive_b, vactive, v_total, h_total_sysclk);
    346
    347	return 0;
    348
    349end:
    350	dev_err(lt9611->dev, "read video check error\n");
    351	return temp;
    352}
    353
    354static void lt9611_hdmi_tx_digital(struct lt9611 *lt9611)
    355{
    356	regmap_write(lt9611->regmap, 0x8443, 0x46 - lt9611->vic);
    357	regmap_write(lt9611->regmap, 0x8447, lt9611->vic);
    358	regmap_write(lt9611->regmap, 0x843d, 0x0a); /* UD1 infoframe */
    359
    360	regmap_write(lt9611->regmap, 0x82d6, 0x8c);
    361	regmap_write(lt9611->regmap, 0x82d7, 0x04);
    362}
    363
    364static void lt9611_hdmi_tx_phy(struct lt9611 *lt9611)
    365{
    366	struct reg_sequence reg_cfg[] = {
    367		{ 0x8130, 0x6a },
    368		{ 0x8131, 0x44 }, /* HDMI DC mode */
    369		{ 0x8132, 0x4a },
    370		{ 0x8133, 0x0b },
    371		{ 0x8134, 0x00 },
    372		{ 0x8135, 0x00 },
    373		{ 0x8136, 0x00 },
    374		{ 0x8137, 0x44 },
    375		{ 0x813f, 0x0f },
    376		{ 0x8140, 0xa0 },
    377		{ 0x8141, 0xa0 },
    378		{ 0x8142, 0xa0 },
    379		{ 0x8143, 0xa0 },
    380		{ 0x8144, 0x0a },
    381	};
    382
    383	/* HDMI AC mode */
    384	if (lt9611->ac_mode)
    385		reg_cfg[2].def = 0x73;
    386
    387	regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
    388}
    389
    390static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
    391{
    392	struct lt9611 *lt9611 = dev_id;
    393	unsigned int irq_flag0 = 0;
    394	unsigned int irq_flag3 = 0;
    395
    396	regmap_read(lt9611->regmap, 0x820f, &irq_flag3);
    397	regmap_read(lt9611->regmap, 0x820c, &irq_flag0);
    398
    399	/* hpd changed low */
    400	if (irq_flag3 & 0x80) {
    401		dev_info(lt9611->dev, "hdmi cable disconnected\n");
    402
    403		regmap_write(lt9611->regmap, 0x8207, 0xbf);
    404		regmap_write(lt9611->regmap, 0x8207, 0x3f);
    405	}
    406
    407	/* hpd changed high */
    408	if (irq_flag3 & 0x40) {
    409		dev_info(lt9611->dev, "hdmi cable connected\n");
    410
    411		regmap_write(lt9611->regmap, 0x8207, 0x7f);
    412		regmap_write(lt9611->regmap, 0x8207, 0x3f);
    413	}
    414
    415	if (irq_flag3 & 0xc0 && lt9611->bridge.dev)
    416		drm_kms_helper_hotplug_event(lt9611->bridge.dev);
    417
    418	/* video input changed */
    419	if (irq_flag0 & 0x01) {
    420		dev_info(lt9611->dev, "video input changed\n");
    421		regmap_write(lt9611->regmap, 0x829e, 0xff);
    422		regmap_write(lt9611->regmap, 0x829e, 0xf7);
    423		regmap_write(lt9611->regmap, 0x8204, 0xff);
    424		regmap_write(lt9611->regmap, 0x8204, 0xfe);
    425	}
    426
    427	return IRQ_HANDLED;
    428}
    429
    430static void lt9611_enable_hpd_interrupts(struct lt9611 *lt9611)
    431{
    432	unsigned int val;
    433
    434	regmap_read(lt9611->regmap, 0x8203, &val);
    435
    436	val &= ~0xc0;
    437	regmap_write(lt9611->regmap, 0x8203, val);
    438	regmap_write(lt9611->regmap, 0x8207, 0xff); /* clear */
    439	regmap_write(lt9611->regmap, 0x8207, 0x3f);
    440}
    441
    442static void lt9611_sleep_setup(struct lt9611 *lt9611)
    443{
    444	const struct reg_sequence sleep_setup[] = {
    445		{ 0x8024, 0x76 },
    446		{ 0x8023, 0x01 },
    447		{ 0x8157, 0x03 }, /* set addr pin as output */
    448		{ 0x8149, 0x0b },
    449		{ 0x8151, 0x30 }, /* disable IRQ */
    450		{ 0x8102, 0x48 }, /* MIPI Rx power down */
    451		{ 0x8123, 0x80 },
    452		{ 0x8130, 0x00 },
    453		{ 0x8100, 0x01 }, /* bandgap power down */
    454		{ 0x8101, 0x00 }, /* system clk power down */
    455	};
    456
    457	regmap_multi_reg_write(lt9611->regmap,
    458			       sleep_setup, ARRAY_SIZE(sleep_setup));
    459	lt9611->sleep = true;
    460}
    461
    462static int lt9611_power_on(struct lt9611 *lt9611)
    463{
    464	int ret;
    465	const struct reg_sequence seq[] = {
    466		/* LT9611_System_Init */
    467		{ 0x8101, 0x18 }, /* sel xtal clock */
    468
    469		/* timer for frequency meter */
    470		{ 0x821b, 0x69 }, /* timer 2 */
    471		{ 0x821c, 0x78 },
    472		{ 0x82cb, 0x69 }, /* timer 1 */
    473		{ 0x82cc, 0x78 },
    474
    475		/* irq init */
    476		{ 0x8251, 0x01 },
    477		{ 0x8258, 0x0a }, /* hpd irq */
    478		{ 0x8259, 0x80 }, /* hpd debounce width */
    479		{ 0x829e, 0xf7 }, /* video check irq */
    480
    481		/* power consumption for work */
    482		{ 0x8004, 0xf0 },
    483		{ 0x8006, 0xf0 },
    484		{ 0x800a, 0x80 },
    485		{ 0x800b, 0x40 },
    486		{ 0x800d, 0xef },
    487		{ 0x8011, 0xfa },
    488	};
    489
    490	if (lt9611->power_on)
    491		return 0;
    492
    493	ret = regmap_multi_reg_write(lt9611->regmap, seq, ARRAY_SIZE(seq));
    494	if (!ret)
    495		lt9611->power_on = true;
    496
    497	return ret;
    498}
    499
    500static int lt9611_power_off(struct lt9611 *lt9611)
    501{
    502	int ret;
    503
    504	ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
    505	if (!ret)
    506		lt9611->power_on = false;
    507
    508	return ret;
    509}
    510
    511static void lt9611_reset(struct lt9611 *lt9611)
    512{
    513	gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
    514	msleep(20);
    515
    516	gpiod_set_value_cansleep(lt9611->reset_gpio, 0);
    517	msleep(20);
    518
    519	gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
    520	msleep(100);
    521}
    522
    523static void lt9611_assert_5v(struct lt9611 *lt9611)
    524{
    525	if (!lt9611->enable_gpio)
    526		return;
    527
    528	gpiod_set_value_cansleep(lt9611->enable_gpio, 1);
    529	msleep(20);
    530}
    531
    532static int lt9611_regulator_init(struct lt9611 *lt9611)
    533{
    534	int ret;
    535
    536	lt9611->supplies[0].supply = "vdd";
    537	lt9611->supplies[1].supply = "vcc";
    538
    539	ret = devm_regulator_bulk_get(lt9611->dev, 2, lt9611->supplies);
    540	if (ret < 0)
    541		return ret;
    542
    543	return regulator_set_load(lt9611->supplies[0].consumer, 300000);
    544}
    545
    546static int lt9611_regulator_enable(struct lt9611 *lt9611)
    547{
    548	int ret;
    549
    550	ret = regulator_enable(lt9611->supplies[0].consumer);
    551	if (ret < 0)
    552		return ret;
    553
    554	usleep_range(1000, 10000);
    555
    556	ret = regulator_enable(lt9611->supplies[1].consumer);
    557	if (ret < 0) {
    558		regulator_disable(lt9611->supplies[0].consumer);
    559		return ret;
    560	}
    561
    562	return 0;
    563}
    564
    565static struct lt9611_mode *lt9611_find_mode(const struct drm_display_mode *mode)
    566{
    567	int i;
    568
    569	for (i = 0; i < ARRAY_SIZE(lt9611_modes); i++) {
    570		if (lt9611_modes[i].hdisplay == mode->hdisplay &&
    571		    lt9611_modes[i].vdisplay == mode->vdisplay &&
    572		    lt9611_modes[i].vrefresh == drm_mode_vrefresh(mode)) {
    573			return &lt9611_modes[i];
    574		}
    575	}
    576
    577	return NULL;
    578}
    579
    580/* connector funcs */
    581static enum drm_connector_status
    582lt9611_connector_detect(struct drm_connector *connector, bool force)
    583{
    584	struct lt9611 *lt9611 = connector_to_lt9611(connector);
    585	unsigned int reg_val = 0;
    586	int connected = 0;
    587
    588	regmap_read(lt9611->regmap, 0x825e, &reg_val);
    589	connected  = (reg_val & BIT(0));
    590
    591	lt9611->status = connected ?  connector_status_connected :
    592				connector_status_disconnected;
    593
    594	return lt9611->status;
    595}
    596
    597static int lt9611_read_edid(struct lt9611 *lt9611)
    598{
    599	unsigned int temp;
    600	int ret = 0;
    601	int i, j;
    602
    603	/* memset to clear old buffer, if any */
    604	memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf));
    605
    606	regmap_write(lt9611->regmap, 0x8503, 0xc9);
    607
    608	/* 0xA0 is EDID device address */
    609	regmap_write(lt9611->regmap, 0x8504, 0xa0);
    610	/* 0x00 is EDID offset address */
    611	regmap_write(lt9611->regmap, 0x8505, 0x00);
    612
    613	/* length for read */
    614	regmap_write(lt9611->regmap, 0x8506, EDID_LEN);
    615	regmap_write(lt9611->regmap, 0x8514, 0x7f);
    616
    617	for (i = 0; i < EDID_LOOP; i++) {
    618		/* offset address */
    619		regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN);
    620		regmap_write(lt9611->regmap, 0x8507, 0x36);
    621		regmap_write(lt9611->regmap, 0x8507, 0x31);
    622		regmap_write(lt9611->regmap, 0x8507, 0x37);
    623		usleep_range(5000, 10000);
    624
    625		regmap_read(lt9611->regmap, 0x8540, &temp);
    626
    627		if (temp & KEY_DDC_ACCS_DONE) {
    628			for (j = 0; j < EDID_LEN; j++) {
    629				regmap_read(lt9611->regmap, 0x8583, &temp);
    630				lt9611->edid_buf[i * EDID_LEN + j] = temp;
    631			}
    632
    633		} else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */
    634			dev_err(lt9611->dev, "read edid failed: no ack\n");
    635			ret = -EIO;
    636			goto end;
    637
    638		} else {
    639			dev_err(lt9611->dev, "read edid failed: access not done\n");
    640			ret = -EIO;
    641			goto end;
    642		}
    643	}
    644
    645end:
    646	regmap_write(lt9611->regmap, 0x8507, 0x1f);
    647	return ret;
    648}
    649
    650static int
    651lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
    652{
    653	struct lt9611 *lt9611 = data;
    654	int ret;
    655
    656	if (len > 128)
    657		return -EINVAL;
    658
    659	/* supports up to 1 extension block */
    660	/* TODO: add support for more extension blocks */
    661	if (block > 1)
    662		return -EINVAL;
    663
    664	if (block == 0) {
    665		ret = lt9611_read_edid(lt9611);
    666		if (ret) {
    667			dev_err(lt9611->dev, "edid read failed\n");
    668			return ret;
    669		}
    670	}
    671
    672	block %= 2;
    673	memcpy(buf, lt9611->edid_buf + (block * 128), len);
    674
    675	return 0;
    676}
    677
    678static int lt9611_connector_get_modes(struct drm_connector *connector)
    679{
    680	struct lt9611 *lt9611 = connector_to_lt9611(connector);
    681	unsigned int count;
    682	struct edid *edid;
    683
    684	lt9611_power_on(lt9611);
    685	edid = drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
    686	drm_connector_update_edid_property(connector, edid);
    687	count = drm_add_edid_modes(connector, edid);
    688	kfree(edid);
    689
    690	return count;
    691}
    692
    693static enum drm_mode_status
    694lt9611_connector_mode_valid(struct drm_connector *connector,
    695			    struct drm_display_mode *mode)
    696{
    697	struct lt9611_mode *lt9611_mode = lt9611_find_mode(mode);
    698
    699	return lt9611_mode ? MODE_OK : MODE_BAD;
    700}
    701
    702/* bridge funcs */
    703static void
    704lt9611_bridge_atomic_enable(struct drm_bridge *bridge,
    705			    struct drm_bridge_state *old_bridge_state)
    706{
    707	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    708
    709	if (lt9611_power_on(lt9611)) {
    710		dev_err(lt9611->dev, "power on failed\n");
    711		return;
    712	}
    713
    714	lt9611_mipi_input_analog(lt9611);
    715	lt9611_hdmi_tx_digital(lt9611);
    716	lt9611_hdmi_tx_phy(lt9611);
    717
    718	msleep(500);
    719
    720	lt9611_video_check(lt9611);
    721
    722	/* Enable HDMI output */
    723	regmap_write(lt9611->regmap, 0x8130, 0xea);
    724}
    725
    726static void
    727lt9611_bridge_atomic_disable(struct drm_bridge *bridge,
    728			     struct drm_bridge_state *old_bridge_state)
    729{
    730	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    731	int ret;
    732
    733	/* Disable HDMI output */
    734	ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
    735	if (ret) {
    736		dev_err(lt9611->dev, "video on failed\n");
    737		return;
    738	}
    739
    740	if (lt9611_power_off(lt9611)) {
    741		dev_err(lt9611->dev, "power on failed\n");
    742		return;
    743	}
    744}
    745
    746static struct
    747drm_connector_helper_funcs lt9611_bridge_connector_helper_funcs = {
    748	.get_modes = lt9611_connector_get_modes,
    749	.mode_valid = lt9611_connector_mode_valid,
    750};
    751
    752static const struct drm_connector_funcs lt9611_bridge_connector_funcs = {
    753	.fill_modes = drm_helper_probe_single_connector_modes,
    754	.detect = lt9611_connector_detect,
    755	.destroy = drm_connector_cleanup,
    756	.reset = drm_atomic_helper_connector_reset,
    757	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
    758	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
    759};
    760
    761static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611,
    762						 struct device_node *dsi_node)
    763{
    764	const struct mipi_dsi_device_info info = { "lt9611", 0, NULL };
    765	struct mipi_dsi_device *dsi;
    766	struct mipi_dsi_host *host;
    767	struct device *dev = lt9611->dev;
    768	int ret;
    769
    770	host = of_find_mipi_dsi_host_by_node(dsi_node);
    771	if (!host) {
    772		dev_err(lt9611->dev, "failed to find dsi host\n");
    773		return ERR_PTR(-EPROBE_DEFER);
    774	}
    775
    776	dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
    777	if (IS_ERR(dsi)) {
    778		dev_err(lt9611->dev, "failed to create dsi device\n");
    779		return dsi;
    780	}
    781
    782	dsi->lanes = 4;
    783	dsi->format = MIPI_DSI_FMT_RGB888;
    784	dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
    785			  MIPI_DSI_MODE_VIDEO_HSE;
    786
    787	ret = devm_mipi_dsi_attach(dev, dsi);
    788	if (ret < 0) {
    789		dev_err(dev, "failed to attach dsi to host\n");
    790		return ERR_PTR(ret);
    791	}
    792
    793	return dsi;
    794}
    795
    796static int lt9611_connector_init(struct drm_bridge *bridge, struct lt9611 *lt9611)
    797{
    798	int ret;
    799
    800	ret = drm_connector_init(bridge->dev, &lt9611->connector,
    801				 &lt9611_bridge_connector_funcs,
    802				 DRM_MODE_CONNECTOR_HDMIA);
    803	if (ret) {
    804		DRM_ERROR("Failed to initialize connector with drm\n");
    805		return ret;
    806	}
    807
    808	drm_connector_helper_add(&lt9611->connector,
    809				 &lt9611_bridge_connector_helper_funcs);
    810	drm_connector_attach_encoder(&lt9611->connector, bridge->encoder);
    811
    812	if (!bridge->encoder) {
    813		DRM_ERROR("Parent encoder object not found");
    814		return -ENODEV;
    815	}
    816
    817	return 0;
    818}
    819
    820static int lt9611_bridge_attach(struct drm_bridge *bridge,
    821				enum drm_bridge_attach_flags flags)
    822{
    823	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    824	int ret;
    825
    826	if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
    827		ret = lt9611_connector_init(bridge, lt9611);
    828		if (ret < 0)
    829			return ret;
    830	}
    831
    832	return 0;
    833}
    834
    835static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
    836						     const struct drm_display_info *info,
    837						     const struct drm_display_mode *mode)
    838{
    839	struct lt9611_mode *lt9611_mode = lt9611_find_mode(mode);
    840	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    841
    842	if (!lt9611_mode)
    843		return MODE_BAD;
    844	else if (lt9611_mode->intfs > 1 && !lt9611->dsi1)
    845		return MODE_PANEL;
    846	else
    847		return MODE_OK;
    848}
    849
    850static void lt9611_bridge_pre_enable(struct drm_bridge *bridge)
    851{
    852	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    853
    854	if (!lt9611->sleep)
    855		return;
    856
    857	lt9611_reset(lt9611);
    858	regmap_write(lt9611->regmap, 0x80ee, 0x01);
    859
    860	lt9611->sleep = false;
    861}
    862
    863static void
    864lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
    865				  struct drm_bridge_state *old_bridge_state)
    866{
    867	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    868
    869	lt9611_sleep_setup(lt9611);
    870}
    871
    872static void lt9611_bridge_mode_set(struct drm_bridge *bridge,
    873				   const struct drm_display_mode *mode,
    874				   const struct drm_display_mode *adj_mode)
    875{
    876	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    877	struct hdmi_avi_infoframe avi_frame;
    878	int ret;
    879
    880	lt9611_bridge_pre_enable(bridge);
    881
    882	lt9611_mipi_input_digital(lt9611, mode);
    883	lt9611_pll_setup(lt9611, mode);
    884	lt9611_mipi_video_setup(lt9611, mode);
    885	lt9611_pcr_setup(lt9611, mode);
    886
    887	ret = drm_hdmi_avi_infoframe_from_display_mode(&avi_frame,
    888						       &lt9611->connector,
    889						       mode);
    890	if (!ret)
    891		lt9611->vic = avi_frame.video_code;
    892}
    893
    894static enum drm_connector_status lt9611_bridge_detect(struct drm_bridge *bridge)
    895{
    896	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    897	unsigned int reg_val = 0;
    898	int connected;
    899
    900	regmap_read(lt9611->regmap, 0x825e, &reg_val);
    901	connected  = reg_val & BIT(0);
    902
    903	lt9611->status = connected ?  connector_status_connected :
    904				connector_status_disconnected;
    905
    906	return lt9611->status;
    907}
    908
    909static struct edid *lt9611_bridge_get_edid(struct drm_bridge *bridge,
    910					   struct drm_connector *connector)
    911{
    912	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    913
    914	lt9611_power_on(lt9611);
    915	return drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
    916}
    917
    918static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
    919{
    920	struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
    921
    922	lt9611_enable_hpd_interrupts(lt9611);
    923}
    924
    925#define MAX_INPUT_SEL_FORMATS	1
    926
    927static u32 *
    928lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
    929				 struct drm_bridge_state *bridge_state,
    930				 struct drm_crtc_state *crtc_state,
    931				 struct drm_connector_state *conn_state,
    932				 u32 output_fmt,
    933				 unsigned int *num_input_fmts)
    934{
    935	u32 *input_fmts;
    936
    937	*num_input_fmts = 0;
    938
    939	input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
    940			     GFP_KERNEL);
    941	if (!input_fmts)
    942		return NULL;
    943
    944	/* This is the DSI-end bus format */
    945	input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
    946	*num_input_fmts = 1;
    947
    948	return input_fmts;
    949}
    950
    951static const struct drm_bridge_funcs lt9611_bridge_funcs = {
    952	.attach = lt9611_bridge_attach,
    953	.mode_valid = lt9611_bridge_mode_valid,
    954	.mode_set = lt9611_bridge_mode_set,
    955	.detect = lt9611_bridge_detect,
    956	.get_edid = lt9611_bridge_get_edid,
    957	.hpd_enable = lt9611_bridge_hpd_enable,
    958
    959	.atomic_enable = lt9611_bridge_atomic_enable,
    960	.atomic_disable = lt9611_bridge_atomic_disable,
    961	.atomic_post_disable = lt9611_bridge_atomic_post_disable,
    962	.atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
    963	.atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
    964	.atomic_reset = drm_atomic_helper_bridge_reset,
    965	.atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
    966};
    967
    968static int lt9611_parse_dt(struct device *dev,
    969			   struct lt9611 *lt9611)
    970{
    971	lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
    972	if (!lt9611->dsi0_node) {
    973		dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
    974		return -ENODEV;
    975	}
    976
    977	lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
    978
    979	lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
    980
    981	return 0;
    982}
    983
    984static int lt9611_gpio_init(struct lt9611 *lt9611)
    985{
    986	struct device *dev = lt9611->dev;
    987
    988	lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
    989	if (IS_ERR(lt9611->reset_gpio)) {
    990		dev_err(dev, "failed to acquire reset gpio\n");
    991		return PTR_ERR(lt9611->reset_gpio);
    992	}
    993
    994	lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
    995						      GPIOD_OUT_LOW);
    996	if (IS_ERR(lt9611->enable_gpio)) {
    997		dev_err(dev, "failed to acquire enable gpio\n");
    998		return PTR_ERR(lt9611->enable_gpio);
    999	}
   1000
   1001	return 0;
   1002}
   1003
   1004static int lt9611_read_device_rev(struct lt9611 *lt9611)
   1005{
   1006	unsigned int rev;
   1007	int ret;
   1008
   1009	regmap_write(lt9611->regmap, 0x80ee, 0x01);
   1010	ret = regmap_read(lt9611->regmap, 0x8002, &rev);
   1011	if (ret)
   1012		dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
   1013	else
   1014		dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
   1015
   1016	return ret;
   1017}
   1018
   1019static int lt9611_hdmi_hw_params(struct device *dev, void *data,
   1020				 struct hdmi_codec_daifmt *fmt,
   1021				 struct hdmi_codec_params *hparms)
   1022{
   1023	struct lt9611 *lt9611 = data;
   1024
   1025	if (hparms->sample_rate == 48000)
   1026		regmap_write(lt9611->regmap, 0x840f, 0x2b);
   1027	else if (hparms->sample_rate == 96000)
   1028		regmap_write(lt9611->regmap, 0x840f, 0xab);
   1029	else
   1030		return -EINVAL;
   1031
   1032	regmap_write(lt9611->regmap, 0x8435, 0x00);
   1033	regmap_write(lt9611->regmap, 0x8436, 0x18);
   1034	regmap_write(lt9611->regmap, 0x8437, 0x00);
   1035
   1036	return 0;
   1037}
   1038
   1039static int lt9611_audio_startup(struct device *dev, void *data)
   1040{
   1041	struct lt9611 *lt9611 = data;
   1042
   1043	regmap_write(lt9611->regmap, 0x82d6, 0x8c);
   1044	regmap_write(lt9611->regmap, 0x82d7, 0x04);
   1045
   1046	regmap_write(lt9611->regmap, 0x8406, 0x08);
   1047	regmap_write(lt9611->regmap, 0x8407, 0x10);
   1048
   1049	regmap_write(lt9611->regmap, 0x8434, 0xd5);
   1050
   1051	return 0;
   1052}
   1053
   1054static void lt9611_audio_shutdown(struct device *dev, void *data)
   1055{
   1056	struct lt9611 *lt9611 = data;
   1057
   1058	regmap_write(lt9611->regmap, 0x8406, 0x00);
   1059	regmap_write(lt9611->regmap, 0x8407, 0x00);
   1060}
   1061
   1062static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
   1063				      struct device_node *endpoint)
   1064{
   1065	struct of_endpoint of_ep;
   1066	int ret;
   1067
   1068	ret = of_graph_parse_endpoint(endpoint, &of_ep);
   1069	if (ret < 0)
   1070		return ret;
   1071
   1072	/*
   1073	 * HDMI sound should be located as reg = <2>
   1074	 * Then, it is sound port 0
   1075	 */
   1076	if (of_ep.port == 2)
   1077		return 0;
   1078
   1079	return -EINVAL;
   1080}
   1081
   1082static const struct hdmi_codec_ops lt9611_codec_ops = {
   1083	.hw_params	= lt9611_hdmi_hw_params,
   1084	.audio_shutdown = lt9611_audio_shutdown,
   1085	.audio_startup	= lt9611_audio_startup,
   1086	.get_dai_id	= lt9611_hdmi_i2s_get_dai_id,
   1087};
   1088
   1089static struct hdmi_codec_pdata codec_data = {
   1090	.ops = &lt9611_codec_ops,
   1091	.max_i2s_channels = 8,
   1092	.i2s = 1,
   1093};
   1094
   1095static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
   1096{
   1097	codec_data.data = lt9611;
   1098	lt9611->audio_pdev =
   1099		platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
   1100					      PLATFORM_DEVID_AUTO,
   1101					      &codec_data, sizeof(codec_data));
   1102
   1103	return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
   1104}
   1105
   1106static void lt9611_audio_exit(struct lt9611 *lt9611)
   1107{
   1108	if (lt9611->audio_pdev) {
   1109		platform_device_unregister(lt9611->audio_pdev);
   1110		lt9611->audio_pdev = NULL;
   1111	}
   1112}
   1113
   1114static int lt9611_probe(struct i2c_client *client,
   1115			const struct i2c_device_id *id)
   1116{
   1117	struct lt9611 *lt9611;
   1118	struct device *dev = &client->dev;
   1119	int ret;
   1120
   1121	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
   1122		dev_err(dev, "device doesn't support I2C\n");
   1123		return -ENODEV;
   1124	}
   1125
   1126	lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
   1127	if (!lt9611)
   1128		return -ENOMEM;
   1129
   1130	lt9611->dev = dev;
   1131	lt9611->client = client;
   1132	lt9611->sleep = false;
   1133
   1134	lt9611->regmap = devm_regmap_init_i2c(client, &lt9611_regmap_config);
   1135	if (IS_ERR(lt9611->regmap)) {
   1136		dev_err(lt9611->dev, "regmap i2c init failed\n");
   1137		return PTR_ERR(lt9611->regmap);
   1138	}
   1139
   1140	ret = lt9611_parse_dt(dev, lt9611);
   1141	if (ret) {
   1142		dev_err(dev, "failed to parse device tree\n");
   1143		return ret;
   1144	}
   1145
   1146	ret = lt9611_gpio_init(lt9611);
   1147	if (ret < 0)
   1148		goto err_of_put;
   1149
   1150	ret = lt9611_regulator_init(lt9611);
   1151	if (ret < 0)
   1152		goto err_of_put;
   1153
   1154	lt9611_assert_5v(lt9611);
   1155
   1156	ret = lt9611_regulator_enable(lt9611);
   1157	if (ret)
   1158		goto err_of_put;
   1159
   1160	lt9611_reset(lt9611);
   1161
   1162	ret = lt9611_read_device_rev(lt9611);
   1163	if (ret) {
   1164		dev_err(dev, "failed to read chip rev\n");
   1165		goto err_disable_regulators;
   1166	}
   1167
   1168	ret = devm_request_threaded_irq(dev, client->irq, NULL,
   1169					lt9611_irq_thread_handler,
   1170					IRQF_ONESHOT, "lt9611", lt9611);
   1171	if (ret) {
   1172		dev_err(dev, "failed to request irq\n");
   1173		goto err_disable_regulators;
   1174	}
   1175
   1176	i2c_set_clientdata(client, lt9611);
   1177
   1178	lt9611->bridge.funcs = &lt9611_bridge_funcs;
   1179	lt9611->bridge.of_node = client->dev.of_node;
   1180	lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
   1181			     DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
   1182	lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
   1183
   1184	drm_bridge_add(&lt9611->bridge);
   1185
   1186	/* Attach primary DSI */
   1187	lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
   1188	if (IS_ERR(lt9611->dsi0)) {
   1189		ret = PTR_ERR(lt9611->dsi0);
   1190		goto err_remove_bridge;
   1191	}
   1192
   1193	/* Attach secondary DSI, if specified */
   1194	if (lt9611->dsi1_node) {
   1195		lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
   1196		if (IS_ERR(lt9611->dsi1)) {
   1197			ret = PTR_ERR(lt9611->dsi1);
   1198			goto err_remove_bridge;
   1199		}
   1200	}
   1201
   1202	lt9611_enable_hpd_interrupts(lt9611);
   1203
   1204	ret = lt9611_audio_init(dev, lt9611);
   1205	if (ret)
   1206		goto err_remove_bridge;
   1207
   1208	return 0;
   1209
   1210err_remove_bridge:
   1211	drm_bridge_remove(&lt9611->bridge);
   1212
   1213err_disable_regulators:
   1214	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
   1215
   1216err_of_put:
   1217	of_node_put(lt9611->dsi1_node);
   1218	of_node_put(lt9611->dsi0_node);
   1219
   1220	return ret;
   1221}
   1222
   1223static int lt9611_remove(struct i2c_client *client)
   1224{
   1225	struct lt9611 *lt9611 = i2c_get_clientdata(client);
   1226
   1227	disable_irq(client->irq);
   1228	lt9611_audio_exit(lt9611);
   1229	drm_bridge_remove(&lt9611->bridge);
   1230
   1231	regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
   1232
   1233	of_node_put(lt9611->dsi1_node);
   1234	of_node_put(lt9611->dsi0_node);
   1235
   1236	return 0;
   1237}
   1238
   1239static struct i2c_device_id lt9611_id[] = {
   1240	{ "lontium,lt9611", 0 },
   1241	{}
   1242};
   1243MODULE_DEVICE_TABLE(i2c, lt9611_id);
   1244
   1245static const struct of_device_id lt9611_match_table[] = {
   1246	{ .compatible = "lontium,lt9611" },
   1247	{ }
   1248};
   1249MODULE_DEVICE_TABLE(of, lt9611_match_table);
   1250
   1251static struct i2c_driver lt9611_driver = {
   1252	.driver = {
   1253		.name = "lt9611",
   1254		.of_match_table = lt9611_match_table,
   1255	},
   1256	.probe = lt9611_probe,
   1257	.remove = lt9611_remove,
   1258	.id_table = lt9611_id,
   1259};
   1260module_i2c_driver(lt9611_driver);
   1261
   1262MODULE_LICENSE("GPL v2");