1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4 * Copyright (c) 2019-2020. Linaro Limited.
7 #include <linux/gpio/consumer.h>
9 #include <linux/interrupt.h>
10 #include <linux/media-bus-format.h>
11 #include <linux/module.h>
12 #include <linux/of_graph.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/regulator/consumer.h>
17 #include <sound/hdmi-codec.h>
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_bridge.h>
21 #include <drm/drm_mipi_dsi.h>
22 #include <drm/drm_of.h>
23 #include <drm/drm_print.h>
24 #include <drm/drm_probe_helper.h>
26 #define EDID_SEG_SIZE 256
29 #define KEY_DDC_ACCS_DONE 0x02
30 #define DDC_NO_ACK 0x50
32 #define LT9611_4LANES 0
36 struct drm_bridge bridge;
37 struct drm_bridge *next_bridge;
39 struct regmap *regmap;
41 struct device_node *dsi0_node;
42 struct device_node *dsi1_node;
43 struct mipi_dsi_device *dsi0;
44 struct mipi_dsi_device *dsi1;
45 struct platform_device *audio_pdev;
49 struct gpio_desc *reset_gpio;
50 struct gpio_desc *enable_gpio;
55 struct regulator_bulk_data supplies[2];
57 struct i2c_client *client;
59 enum drm_connector_status status;
61 u8 edid_buf[EDID_SEG_SIZE];
64 #define LT9611_PAGE_CONTROL 0xff
66 static const struct regmap_range_cfg lt9611_ranges[] = {
68 .name = "register_range",
71 .selector_reg = LT9611_PAGE_CONTROL,
72 .selector_mask = 0xff,
79 static const struct regmap_config lt9611_regmap_config = {
82 .max_register = 0xffff,
83 .ranges = lt9611_ranges,
84 .num_ranges = ARRAY_SIZE(lt9611_ranges),
87 static struct lt9611 *bridge_to_lt9611(struct drm_bridge *bridge)
89 return container_of(bridge, struct lt9611, bridge);
92 static int lt9611_mipi_input_analog(struct lt9611 *lt9611)
94 const struct reg_sequence reg_cfg[] = {
95 { 0x8106, 0x40 }, /* port A rx current */
96 { 0x810a, 0xfe }, /* port A ldo voltage set */
97 { 0x810b, 0xbf }, /* enable port A lprx */
98 { 0x8111, 0x40 }, /* port B rx current */
99 { 0x8115, 0xfe }, /* port B ldo voltage set */
100 { 0x8116, 0xbf }, /* enable port B lprx */
102 { 0x811c, 0x03 }, /* PortA clk lane no-LP mode */
103 { 0x8120, 0x03 }, /* PortB clk lane with-LP mode */
106 return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
109 static int lt9611_mipi_input_digital(struct lt9611 *lt9611,
110 const struct drm_display_mode *mode)
112 struct reg_sequence reg_cfg[] = {
113 { 0x8300, LT9611_4LANES },
121 if (lt9611->dsi1_node)
122 reg_cfg[1].def = 0x03;
124 return regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
127 static void lt9611_mipi_video_setup(struct lt9611 *lt9611,
128 const struct drm_display_mode *mode)
130 u32 h_total, hactive, hsync_len, hfront_porch, hsync_porch;
131 u32 v_total, vactive, vsync_len, vfront_porch, vsync_porch;
133 h_total = mode->htotal;
134 v_total = mode->vtotal;
136 hactive = mode->hdisplay;
137 hsync_len = mode->hsync_end - mode->hsync_start;
138 hfront_porch = mode->hsync_start - mode->hdisplay;
139 hsync_porch = mode->htotal - mode->hsync_start;
141 vactive = mode->vdisplay;
142 vsync_len = mode->vsync_end - mode->vsync_start;
143 vfront_porch = mode->vsync_start - mode->vdisplay;
144 vsync_porch = mode->vtotal - mode->vsync_start;
146 regmap_write(lt9611->regmap, 0x830d, (u8)(v_total / 256));
147 regmap_write(lt9611->regmap, 0x830e, (u8)(v_total % 256));
149 regmap_write(lt9611->regmap, 0x830f, (u8)(vactive / 256));
150 regmap_write(lt9611->regmap, 0x8310, (u8)(vactive % 256));
152 regmap_write(lt9611->regmap, 0x8311, (u8)(h_total / 256));
153 regmap_write(lt9611->regmap, 0x8312, (u8)(h_total % 256));
155 regmap_write(lt9611->regmap, 0x8313, (u8)(hactive / 256));
156 regmap_write(lt9611->regmap, 0x8314, (u8)(hactive % 256));
158 regmap_write(lt9611->regmap, 0x8315, (u8)(vsync_len % 256));
159 regmap_write(lt9611->regmap, 0x8316, (u8)(hsync_len % 256));
161 regmap_write(lt9611->regmap, 0x8317, (u8)(vfront_porch % 256));
163 regmap_write(lt9611->regmap, 0x8318, (u8)(vsync_porch % 256));
165 regmap_write(lt9611->regmap, 0x8319, (u8)(hfront_porch % 256));
167 regmap_write(lt9611->regmap, 0x831a, (u8)(hsync_porch / 256) |
168 ((hfront_porch / 256) << 4));
169 regmap_write(lt9611->regmap, 0x831b, (u8)(hsync_porch % 256));
172 static void lt9611_pcr_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int postdiv)
174 unsigned int pcr_m = mode->clock * 5 * postdiv / 27000;
175 const struct reg_sequence reg_cfg[] = {
196 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
198 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
200 regmap_write(lt9611->regmap, 0x831d, pol);
202 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
203 if (lt9611->dsi1_node) {
204 unsigned int hact = mode->hdisplay;
208 hact = min(hact, 0x3e0U);
209 regmap_write(lt9611->regmap, 0x830b, hact / 256);
210 regmap_write(lt9611->regmap, 0x830c, hact % 256);
211 regmap_write(lt9611->regmap, 0x8348, hact / 256);
212 regmap_write(lt9611->regmap, 0x8349, hact % 256);
215 regmap_write(lt9611->regmap, 0x8326, pcr_m);
218 regmap_write(lt9611->regmap, 0x8011, 0x5a);
219 regmap_write(lt9611->regmap, 0x8011, 0xfa);
222 static int lt9611_pll_setup(struct lt9611 *lt9611, const struct drm_display_mode *mode, unsigned int *postdiv)
224 unsigned int pclk = mode->clock;
225 const struct reg_sequence reg_cfg[] = {
239 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
242 regmap_write(lt9611->regmap, 0x812d, 0x88);
244 } else if (pclk > 70000) {
245 regmap_write(lt9611->regmap, 0x812d, 0x99);
248 regmap_write(lt9611->regmap, 0x812d, 0xaa);
253 * first divide pclk by 2 first
254 * - write divide by 64k to 19:16 bits which means shift by 17
255 * - write divide by 256 to 15:8 bits which means shift by 9
256 * - write remainder to 7:0 bits, which means shift by 1
258 regmap_write(lt9611->regmap, 0x82e3, pclk >> 17); /* pclk[19:16] */
259 regmap_write(lt9611->regmap, 0x82e4, pclk >> 9); /* pclk[15:8] */
260 regmap_write(lt9611->regmap, 0x82e5, pclk >> 1); /* pclk[7:0] */
262 regmap_write(lt9611->regmap, 0x82de, 0x20);
263 regmap_write(lt9611->regmap, 0x82de, 0xe0);
265 regmap_write(lt9611->regmap, 0x8016, 0xf1);
266 regmap_write(lt9611->regmap, 0x8016, 0xf3);
271 static int lt9611_read_video_check(struct lt9611 *lt9611, unsigned int reg)
273 unsigned int temp, temp2;
276 ret = regmap_read(lt9611->regmap, reg, &temp);
280 ret = regmap_read(lt9611->regmap, reg + 1, &temp2);
284 return (temp + temp2);
287 static int lt9611_video_check(struct lt9611 *lt9611)
289 u32 v_total, vactive, hactive_a, hactive_b, h_total_sysclk;
292 /* top module video check */
295 temp = lt9611_read_video_check(lt9611, 0x8282);
301 temp = lt9611_read_video_check(lt9611, 0x826c);
307 temp = lt9611_read_video_check(lt9611, 0x8286);
310 h_total_sysclk = temp;
313 temp = lt9611_read_video_check(lt9611, 0x8382);
316 hactive_a = temp / 3;
319 temp = lt9611_read_video_check(lt9611, 0x8386);
322 hactive_b = temp / 3;
324 dev_info(lt9611->dev,
325 "video check: hactive_a=%d, hactive_b=%d, vactive=%d, v_total=%d, h_total_sysclk=%d\n",
326 hactive_a, hactive_b, vactive, v_total, h_total_sysclk);
331 dev_err(lt9611->dev, "read video check error\n");
335 static void lt9611_hdmi_set_infoframes(struct lt9611 *lt9611,
336 struct drm_connector *connector,
337 struct drm_display_mode *mode)
339 union hdmi_infoframe infoframe;
341 u8 iframes = 0x0a; /* UD1 infoframe */
346 ret = drm_hdmi_avi_infoframe_from_display_mode(&infoframe.avi,
352 len = hdmi_infoframe_pack(&infoframe, buf, sizeof(buf));
356 for (i = 0; i < len; i++)
357 regmap_write(lt9611->regmap, 0x8440 + i, buf[i]);
359 ret = drm_hdmi_vendor_infoframe_from_display_mode(&infoframe.vendor.hdmi,
365 len = hdmi_infoframe_pack(&infoframe, buf, sizeof(buf));
369 for (i = 0; i < len; i++)
370 regmap_write(lt9611->regmap, 0x8474 + i, buf[i]);
375 regmap_write(lt9611->regmap, 0x843d, iframes); /* UD1 infoframe */
378 static void lt9611_hdmi_tx_digital(struct lt9611 *lt9611, bool is_hdmi)
381 regmap_write(lt9611->regmap, 0x82d6, 0x8c);
383 regmap_write(lt9611->regmap, 0x82d6, 0x0c);
384 regmap_write(lt9611->regmap, 0x82d7, 0x04);
387 static void lt9611_hdmi_tx_phy(struct lt9611 *lt9611)
389 struct reg_sequence reg_cfg[] = {
391 { 0x8131, 0x44 }, /* HDMI DC mode */
408 reg_cfg[2].def = 0x73;
410 regmap_multi_reg_write(lt9611->regmap, reg_cfg, ARRAY_SIZE(reg_cfg));
413 static irqreturn_t lt9611_irq_thread_handler(int irq, void *dev_id)
415 struct lt9611 *lt9611 = dev_id;
416 unsigned int irq_flag0 = 0;
417 unsigned int irq_flag3 = 0;
419 regmap_read(lt9611->regmap, 0x820f, &irq_flag3);
420 regmap_read(lt9611->regmap, 0x820c, &irq_flag0);
422 /* hpd changed low */
423 if (irq_flag3 & 0x80) {
424 dev_info(lt9611->dev, "hdmi cable disconnected\n");
426 regmap_write(lt9611->regmap, 0x8207, 0xbf);
427 regmap_write(lt9611->regmap, 0x8207, 0x3f);
430 /* hpd changed high */
431 if (irq_flag3 & 0x40) {
432 dev_info(lt9611->dev, "hdmi cable connected\n");
434 regmap_write(lt9611->regmap, 0x8207, 0x7f);
435 regmap_write(lt9611->regmap, 0x8207, 0x3f);
438 if (irq_flag3 & 0xc0 && lt9611->bridge.dev)
439 drm_kms_helper_hotplug_event(lt9611->bridge.dev);
441 /* video input changed */
442 if (irq_flag0 & 0x01) {
443 dev_info(lt9611->dev, "video input changed\n");
444 regmap_write(lt9611->regmap, 0x829e, 0xff);
445 regmap_write(lt9611->regmap, 0x829e, 0xf7);
446 regmap_write(lt9611->regmap, 0x8204, 0xff);
447 regmap_write(lt9611->regmap, 0x8204, 0xfe);
453 static void lt9611_enable_hpd_interrupts(struct lt9611 *lt9611)
457 regmap_read(lt9611->regmap, 0x8203, &val);
460 regmap_write(lt9611->regmap, 0x8203, val);
461 regmap_write(lt9611->regmap, 0x8207, 0xff); /* clear */
462 regmap_write(lt9611->regmap, 0x8207, 0x3f);
465 static void lt9611_sleep_setup(struct lt9611 *lt9611)
467 const struct reg_sequence sleep_setup[] = {
470 { 0x8157, 0x03 }, /* set addr pin as output */
473 { 0x8102, 0x48 }, /* MIPI Rx power down */
479 regmap_multi_reg_write(lt9611->regmap,
480 sleep_setup, ARRAY_SIZE(sleep_setup));
481 lt9611->sleep = true;
484 static int lt9611_power_on(struct lt9611 *lt9611)
487 const struct reg_sequence seq[] = {
488 /* LT9611_System_Init */
489 { 0x8101, 0x18 }, /* sel xtal clock */
491 /* timer for frequency meter */
492 { 0x821b, 0x69 }, /* timer 2 */
494 { 0x82cb, 0x69 }, /* timer 1 */
499 { 0x8258, 0x0a }, /* hpd irq */
500 { 0x8259, 0x80 }, /* hpd debounce width */
501 { 0x829e, 0xf7 }, /* video check irq */
503 /* power consumption for work */
512 if (lt9611->power_on)
515 ret = regmap_multi_reg_write(lt9611->regmap, seq, ARRAY_SIZE(seq));
517 lt9611->power_on = true;
522 static int lt9611_power_off(struct lt9611 *lt9611)
526 ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
528 lt9611->power_on = false;
533 static void lt9611_reset(struct lt9611 *lt9611)
535 gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
538 gpiod_set_value_cansleep(lt9611->reset_gpio, 0);
541 gpiod_set_value_cansleep(lt9611->reset_gpio, 1);
545 static void lt9611_assert_5v(struct lt9611 *lt9611)
547 if (!lt9611->enable_gpio)
550 gpiod_set_value_cansleep(lt9611->enable_gpio, 1);
554 static int lt9611_regulator_init(struct lt9611 *lt9611)
558 lt9611->supplies[0].supply = "vdd";
559 lt9611->supplies[1].supply = "vcc";
561 ret = devm_regulator_bulk_get(lt9611->dev, 2, lt9611->supplies);
565 return regulator_set_load(lt9611->supplies[0].consumer, 300000);
568 static int lt9611_regulator_enable(struct lt9611 *lt9611)
572 ret = regulator_enable(lt9611->supplies[0].consumer);
576 usleep_range(1000, 10000);
578 ret = regulator_enable(lt9611->supplies[1].consumer);
580 regulator_disable(lt9611->supplies[0].consumer);
587 static enum drm_connector_status lt9611_bridge_detect(struct drm_bridge *bridge)
589 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
590 unsigned int reg_val = 0;
593 regmap_read(lt9611->regmap, 0x825e, ®_val);
594 connected = (reg_val & (BIT(2) | BIT(0)));
596 lt9611->status = connected ? connector_status_connected :
597 connector_status_disconnected;
599 return lt9611->status;
602 static int lt9611_read_edid(struct lt9611 *lt9611)
608 /* memset to clear old buffer, if any */
609 memset(lt9611->edid_buf, 0, sizeof(lt9611->edid_buf));
611 regmap_write(lt9611->regmap, 0x8503, 0xc9);
613 /* 0xA0 is EDID device address */
614 regmap_write(lt9611->regmap, 0x8504, 0xa0);
615 /* 0x00 is EDID offset address */
616 regmap_write(lt9611->regmap, 0x8505, 0x00);
618 /* length for read */
619 regmap_write(lt9611->regmap, 0x8506, EDID_LEN);
620 regmap_write(lt9611->regmap, 0x8514, 0x7f);
622 for (i = 0; i < EDID_LOOP; i++) {
624 regmap_write(lt9611->regmap, 0x8505, i * EDID_LEN);
625 regmap_write(lt9611->regmap, 0x8507, 0x36);
626 regmap_write(lt9611->regmap, 0x8507, 0x31);
627 regmap_write(lt9611->regmap, 0x8507, 0x37);
628 usleep_range(5000, 10000);
630 regmap_read(lt9611->regmap, 0x8540, &temp);
632 if (temp & KEY_DDC_ACCS_DONE) {
633 for (j = 0; j < EDID_LEN; j++) {
634 regmap_read(lt9611->regmap, 0x8583, &temp);
635 lt9611->edid_buf[i * EDID_LEN + j] = temp;
638 } else if (temp & DDC_NO_ACK) { /* DDC No Ack or Abitration lost */
639 dev_err(lt9611->dev, "read edid failed: no ack\n");
644 dev_err(lt9611->dev, "read edid failed: access not done\n");
651 regmap_write(lt9611->regmap, 0x8507, 0x1f);
656 lt9611_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
658 struct lt9611 *lt9611 = data;
664 /* supports up to 1 extension block */
665 /* TODO: add support for more extension blocks */
670 ret = lt9611_read_edid(lt9611);
672 dev_err(lt9611->dev, "edid read failed\n");
678 memcpy(buf, lt9611->edid_buf + (block * 128), len);
685 lt9611_bridge_atomic_enable(struct drm_bridge *bridge,
686 struct drm_bridge_state *old_bridge_state)
688 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
689 struct drm_atomic_state *state = old_bridge_state->base.state;
690 struct drm_connector *connector;
691 struct drm_connector_state *conn_state;
692 struct drm_crtc_state *crtc_state;
693 struct drm_display_mode *mode;
694 unsigned int postdiv;
696 connector = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder);
697 if (WARN_ON(!connector))
700 conn_state = drm_atomic_get_new_connector_state(state, connector);
701 if (WARN_ON(!conn_state))
704 crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
705 if (WARN_ON(!crtc_state))
708 mode = &crtc_state->adjusted_mode;
710 lt9611_mipi_input_digital(lt9611, mode);
711 lt9611_pll_setup(lt9611, mode, &postdiv);
712 lt9611_mipi_video_setup(lt9611, mode);
713 lt9611_pcr_setup(lt9611, mode, postdiv);
715 if (lt9611_power_on(lt9611)) {
716 dev_err(lt9611->dev, "power on failed\n");
720 lt9611_mipi_input_analog(lt9611);
721 lt9611_hdmi_set_infoframes(lt9611, connector, mode);
722 lt9611_hdmi_tx_digital(lt9611, connector->display_info.is_hdmi);
723 lt9611_hdmi_tx_phy(lt9611);
727 lt9611_video_check(lt9611);
729 /* Enable HDMI output */
730 regmap_write(lt9611->regmap, 0x8130, 0xea);
734 lt9611_bridge_atomic_disable(struct drm_bridge *bridge,
735 struct drm_bridge_state *old_bridge_state)
737 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
740 /* Disable HDMI output */
741 ret = regmap_write(lt9611->regmap, 0x8130, 0x6a);
743 dev_err(lt9611->dev, "video on failed\n");
747 if (lt9611_power_off(lt9611)) {
748 dev_err(lt9611->dev, "power on failed\n");
753 static struct mipi_dsi_device *lt9611_attach_dsi(struct lt9611 *lt9611,
754 struct device_node *dsi_node)
756 const struct mipi_dsi_device_info info = { "lt9611", 0, lt9611->dev->of_node};
757 struct mipi_dsi_device *dsi;
758 struct mipi_dsi_host *host;
759 struct device *dev = lt9611->dev;
762 host = of_find_mipi_dsi_host_by_node(dsi_node);
764 dev_err(lt9611->dev, "failed to find dsi host\n");
765 return ERR_PTR(-EPROBE_DEFER);
768 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
770 dev_err(lt9611->dev, "failed to create dsi device\n");
775 dsi->format = MIPI_DSI_FMT_RGB888;
776 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE |
777 MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_VIDEO_NO_HSA |
778 MIPI_DSI_MODE_VIDEO_NO_HFP | MIPI_DSI_MODE_VIDEO_NO_HBP |
779 MIPI_DSI_MODE_NO_EOT_PACKET;
781 ret = devm_mipi_dsi_attach(dev, dsi);
783 dev_err(dev, "failed to attach dsi to host\n");
790 static int lt9611_bridge_attach(struct drm_bridge *bridge,
791 enum drm_bridge_attach_flags flags)
793 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
795 return drm_bridge_attach(bridge->encoder, lt9611->next_bridge,
799 static enum drm_mode_status lt9611_bridge_mode_valid(struct drm_bridge *bridge,
800 const struct drm_display_info *info,
801 const struct drm_display_mode *mode)
803 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
805 if (mode->hdisplay > 3840)
806 return MODE_BAD_HVALUE;
808 if (mode->vdisplay > 2160)
809 return MODE_BAD_VVALUE;
811 if (mode->hdisplay == 3840 &&
812 mode->vdisplay == 2160 &&
813 drm_mode_vrefresh(mode) > 30)
814 return MODE_CLOCK_HIGH;
816 if (mode->hdisplay > 2000 && !lt9611->dsi1_node)
822 static void lt9611_bridge_atomic_pre_enable(struct drm_bridge *bridge,
823 struct drm_bridge_state *old_bridge_state)
825 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
826 static const struct reg_sequence reg_cfg[] = {
836 regmap_multi_reg_write(lt9611->regmap,
837 reg_cfg, ARRAY_SIZE(reg_cfg));
839 lt9611->sleep = false;
843 lt9611_bridge_atomic_post_disable(struct drm_bridge *bridge,
844 struct drm_bridge_state *old_bridge_state)
846 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
848 lt9611_sleep_setup(lt9611);
851 static struct edid *lt9611_bridge_get_edid(struct drm_bridge *bridge,
852 struct drm_connector *connector)
854 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
856 lt9611_power_on(lt9611);
857 return drm_do_get_edid(connector, lt9611_get_edid_block, lt9611);
860 static void lt9611_bridge_hpd_enable(struct drm_bridge *bridge)
862 struct lt9611 *lt9611 = bridge_to_lt9611(bridge);
864 lt9611_enable_hpd_interrupts(lt9611);
867 #define MAX_INPUT_SEL_FORMATS 1
870 lt9611_atomic_get_input_bus_fmts(struct drm_bridge *bridge,
871 struct drm_bridge_state *bridge_state,
872 struct drm_crtc_state *crtc_state,
873 struct drm_connector_state *conn_state,
875 unsigned int *num_input_fmts)
881 input_fmts = kcalloc(MAX_INPUT_SEL_FORMATS, sizeof(*input_fmts),
886 /* This is the DSI-end bus format */
887 input_fmts[0] = MEDIA_BUS_FMT_RGB888_1X24;
893 static const struct drm_bridge_funcs lt9611_bridge_funcs = {
894 .attach = lt9611_bridge_attach,
895 .mode_valid = lt9611_bridge_mode_valid,
896 .detect = lt9611_bridge_detect,
897 .get_edid = lt9611_bridge_get_edid,
898 .hpd_enable = lt9611_bridge_hpd_enable,
900 .atomic_pre_enable = lt9611_bridge_atomic_pre_enable,
901 .atomic_enable = lt9611_bridge_atomic_enable,
902 .atomic_disable = lt9611_bridge_atomic_disable,
903 .atomic_post_disable = lt9611_bridge_atomic_post_disable,
904 .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
905 .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
906 .atomic_reset = drm_atomic_helper_bridge_reset,
907 .atomic_get_input_bus_fmts = lt9611_atomic_get_input_bus_fmts,
910 static int lt9611_parse_dt(struct device *dev,
911 struct lt9611 *lt9611)
913 lt9611->dsi0_node = of_graph_get_remote_node(dev->of_node, 0, -1);
914 if (!lt9611->dsi0_node) {
915 dev_err(lt9611->dev, "failed to get remote node for primary dsi\n");
919 lt9611->dsi1_node = of_graph_get_remote_node(dev->of_node, 1, -1);
921 lt9611->ac_mode = of_property_read_bool(dev->of_node, "lt,ac-mode");
923 return drm_of_find_panel_or_bridge(dev->of_node, 2, -1, NULL, <9611->next_bridge);
926 static int lt9611_gpio_init(struct lt9611 *lt9611)
928 struct device *dev = lt9611->dev;
930 lt9611->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
931 if (IS_ERR(lt9611->reset_gpio)) {
932 dev_err(dev, "failed to acquire reset gpio\n");
933 return PTR_ERR(lt9611->reset_gpio);
936 lt9611->enable_gpio = devm_gpiod_get_optional(dev, "enable",
938 if (IS_ERR(lt9611->enable_gpio)) {
939 dev_err(dev, "failed to acquire enable gpio\n");
940 return PTR_ERR(lt9611->enable_gpio);
946 static int lt9611_read_device_rev(struct lt9611 *lt9611)
951 regmap_write(lt9611->regmap, 0x80ee, 0x01);
952 ret = regmap_read(lt9611->regmap, 0x8002, &rev);
954 dev_err(lt9611->dev, "failed to read revision: %d\n", ret);
956 dev_info(lt9611->dev, "LT9611 revision: 0x%x\n", rev);
961 static int lt9611_hdmi_hw_params(struct device *dev, void *data,
962 struct hdmi_codec_daifmt *fmt,
963 struct hdmi_codec_params *hparms)
965 struct lt9611 *lt9611 = data;
967 if (hparms->sample_rate == 48000)
968 regmap_write(lt9611->regmap, 0x840f, 0x2b);
969 else if (hparms->sample_rate == 96000)
970 regmap_write(lt9611->regmap, 0x840f, 0xab);
974 regmap_write(lt9611->regmap, 0x8435, 0x00);
975 regmap_write(lt9611->regmap, 0x8436, 0x18);
976 regmap_write(lt9611->regmap, 0x8437, 0x00);
981 static int lt9611_audio_startup(struct device *dev, void *data)
983 struct lt9611 *lt9611 = data;
985 regmap_write(lt9611->regmap, 0x82d6, 0x8c);
986 regmap_write(lt9611->regmap, 0x82d7, 0x04);
988 regmap_write(lt9611->regmap, 0x8406, 0x08);
989 regmap_write(lt9611->regmap, 0x8407, 0x10);
991 regmap_write(lt9611->regmap, 0x8434, 0xd5);
996 static void lt9611_audio_shutdown(struct device *dev, void *data)
998 struct lt9611 *lt9611 = data;
1000 regmap_write(lt9611->regmap, 0x8406, 0x00);
1001 regmap_write(lt9611->regmap, 0x8407, 0x00);
1004 static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component *component,
1005 struct device_node *endpoint)
1007 struct of_endpoint of_ep;
1010 ret = of_graph_parse_endpoint(endpoint, &of_ep);
1015 * HDMI sound should be located as reg = <2>
1016 * Then, it is sound port 0
1018 if (of_ep.port == 2)
1024 static const struct hdmi_codec_ops lt9611_codec_ops = {
1025 .hw_params = lt9611_hdmi_hw_params,
1026 .audio_shutdown = lt9611_audio_shutdown,
1027 .audio_startup = lt9611_audio_startup,
1028 .get_dai_id = lt9611_hdmi_i2s_get_dai_id,
1031 static struct hdmi_codec_pdata codec_data = {
1032 .ops = <9611_codec_ops,
1033 .max_i2s_channels = 8,
1037 static int lt9611_audio_init(struct device *dev, struct lt9611 *lt9611)
1039 codec_data.data = lt9611;
1040 lt9611->audio_pdev =
1041 platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
1042 PLATFORM_DEVID_AUTO,
1043 &codec_data, sizeof(codec_data));
1045 return PTR_ERR_OR_ZERO(lt9611->audio_pdev);
1048 static void lt9611_audio_exit(struct lt9611 *lt9611)
1050 if (lt9611->audio_pdev) {
1051 platform_device_unregister(lt9611->audio_pdev);
1052 lt9611->audio_pdev = NULL;
1056 static int lt9611_probe(struct i2c_client *client)
1058 struct lt9611 *lt9611;
1059 struct device *dev = &client->dev;
1062 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1063 dev_err(dev, "device doesn't support I2C\n");
1067 lt9611 = devm_kzalloc(dev, sizeof(*lt9611), GFP_KERNEL);
1072 lt9611->client = client;
1073 lt9611->sleep = false;
1075 lt9611->regmap = devm_regmap_init_i2c(client, <9611_regmap_config);
1076 if (IS_ERR(lt9611->regmap)) {
1077 dev_err(lt9611->dev, "regmap i2c init failed\n");
1078 return PTR_ERR(lt9611->regmap);
1081 ret = lt9611_parse_dt(dev, lt9611);
1083 dev_err(dev, "failed to parse device tree\n");
1087 ret = lt9611_gpio_init(lt9611);
1091 ret = lt9611_regulator_init(lt9611);
1095 lt9611_assert_5v(lt9611);
1097 ret = lt9611_regulator_enable(lt9611);
1101 lt9611_reset(lt9611);
1103 ret = lt9611_read_device_rev(lt9611);
1105 dev_err(dev, "failed to read chip rev\n");
1106 goto err_disable_regulators;
1109 ret = devm_request_threaded_irq(dev, client->irq, NULL,
1110 lt9611_irq_thread_handler,
1111 IRQF_ONESHOT, "lt9611", lt9611);
1113 dev_err(dev, "failed to request irq\n");
1114 goto err_disable_regulators;
1117 i2c_set_clientdata(client, lt9611);
1119 lt9611->bridge.funcs = <9611_bridge_funcs;
1120 lt9611->bridge.of_node = client->dev.of_node;
1121 lt9611->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID |
1122 DRM_BRIDGE_OP_HPD | DRM_BRIDGE_OP_MODES;
1123 lt9611->bridge.type = DRM_MODE_CONNECTOR_HDMIA;
1125 drm_bridge_add(<9611->bridge);
1127 /* Attach primary DSI */
1128 lt9611->dsi0 = lt9611_attach_dsi(lt9611, lt9611->dsi0_node);
1129 if (IS_ERR(lt9611->dsi0)) {
1130 ret = PTR_ERR(lt9611->dsi0);
1131 goto err_remove_bridge;
1134 /* Attach secondary DSI, if specified */
1135 if (lt9611->dsi1_node) {
1136 lt9611->dsi1 = lt9611_attach_dsi(lt9611, lt9611->dsi1_node);
1137 if (IS_ERR(lt9611->dsi1)) {
1138 ret = PTR_ERR(lt9611->dsi1);
1139 goto err_remove_bridge;
1143 lt9611_enable_hpd_interrupts(lt9611);
1145 ret = lt9611_audio_init(dev, lt9611);
1147 goto err_remove_bridge;
1152 drm_bridge_remove(<9611->bridge);
1154 err_disable_regulators:
1155 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1158 of_node_put(lt9611->dsi1_node);
1159 of_node_put(lt9611->dsi0_node);
1164 static void lt9611_remove(struct i2c_client *client)
1166 struct lt9611 *lt9611 = i2c_get_clientdata(client);
1168 disable_irq(client->irq);
1169 lt9611_audio_exit(lt9611);
1170 drm_bridge_remove(<9611->bridge);
1172 regulator_bulk_disable(ARRAY_SIZE(lt9611->supplies), lt9611->supplies);
1174 of_node_put(lt9611->dsi1_node);
1175 of_node_put(lt9611->dsi0_node);
1178 static struct i2c_device_id lt9611_id[] = {
1179 { "lontium,lt9611", 0 },
1182 MODULE_DEVICE_TABLE(i2c, lt9611_id);
1184 static const struct of_device_id lt9611_match_table[] = {
1185 { .compatible = "lontium,lt9611" },
1188 MODULE_DEVICE_TABLE(of, lt9611_match_table);
1190 static struct i2c_driver lt9611_driver = {
1193 .of_match_table = lt9611_match_table,
1195 .probe = lt9611_probe,
1196 .remove = lt9611_remove,
1197 .id_table = lt9611_id,
1199 module_i2c_driver(lt9611_driver);
1201 MODULE_LICENSE("GPL v2");