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>
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>
15 #include <sound/hdmi-codec.h>
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>
23 #define EDID_SEG_SIZE 256
26 #define KEY_DDC_ACCS_DONE 0x02
27 #define DDC_NO_ACK 0x50
29 #define LT9611_4LANES 0
33 struct drm_bridge bridge
;
34 struct drm_connector connector
;
36 struct regmap
*regmap
;
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
;
46 struct gpio_desc
*reset_gpio
;
47 struct gpio_desc
*enable_gpio
;
52 struct regulator_bulk_data supplies
[2];
54 struct i2c_client
*client
;
56 enum drm_connector_status status
;
58 u8 edid_buf
[EDID_SEG_SIZE
];
62 #define LT9611_PAGE_CONTROL 0xff
64 static const struct regmap_range_cfg lt9611_ranges
[] = {
66 .name
= "register_range",
69 .selector_reg
= LT9611_PAGE_CONTROL
,
70 .selector_mask
= 0xff,
77 static const struct regmap_config lt9611_regmap_config
= {
80 .max_register
= 0xffff,
81 .ranges
= lt9611_ranges
,
82 .num_ranges
= ARRAY_SIZE(lt9611_ranges
),
93 static 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 },
103 static struct lt9611
*bridge_to_lt9611(struct drm_bridge
*bridge
)
105 return container_of(bridge
, struct lt9611
, bridge
);
108 static struct lt9611
*connector_to_lt9611(struct drm_connector
*connector
)
110 return container_of(connector
, struct lt9611
, connector
);
113 static int lt9611_mipi_input_analog(struct lt9611
*lt9611
)
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 */
123 { 0x811c, 0x03 }, /* PortA clk lane no-LP mode */
124 { 0x8120, 0x03 }, /* PortB clk lane with-LP mode */
127 return regmap_multi_reg_write(lt9611
->regmap
, reg_cfg
, ARRAY_SIZE(reg_cfg
));
130 static int lt9611_mipi_input_digital(struct lt9611
*lt9611
,
131 const struct drm_display_mode
*mode
)
133 struct reg_sequence reg_cfg
[] = {
134 { 0x8300, LT9611_4LANES
},
142 if (mode
->hdisplay
== 3840)
143 reg_cfg
[1].def
= 0x03;
145 return regmap_multi_reg_write(lt9611
->regmap
, reg_cfg
, ARRAY_SIZE(reg_cfg
));
148 static void lt9611_mipi_video_setup(struct lt9611
*lt9611
,
149 const struct drm_display_mode
*mode
)
151 u32 h_total
, hactive
, hsync_len
, hfront_porch
, hsync_porch
;
152 u32 v_total
, vactive
, vsync_len
, vfront_porch
, vsync_porch
;
154 h_total
= mode
->htotal
;
155 v_total
= mode
->vtotal
;
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
;
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
;
167 regmap_write(lt9611
->regmap
, 0x830d, (u8
)(v_total
/ 256));
168 regmap_write(lt9611
->regmap
, 0x830e, (u8
)(v_total
% 256));
170 regmap_write(lt9611
->regmap
, 0x830f, (u8
)(vactive
/ 256));
171 regmap_write(lt9611
->regmap
, 0x8310, (u8
)(vactive
% 256));
173 regmap_write(lt9611
->regmap
, 0x8311, (u8
)(h_total
/ 256));
174 regmap_write(lt9611
->regmap
, 0x8312, (u8
)(h_total
% 256));
176 regmap_write(lt9611
->regmap
, 0x8313, (u8
)(hactive
/ 256));
177 regmap_write(lt9611
->regmap
, 0x8314, (u8
)(hactive
% 256));
179 regmap_write(lt9611
->regmap
, 0x8315, (u8
)(vsync_len
% 256));
180 regmap_write(lt9611
->regmap
, 0x8316, (u8
)(hsync_len
% 256));
182 regmap_write(lt9611
->regmap
, 0x8317, (u8
)(vfront_porch
% 256));
184 regmap_write(lt9611
->regmap
, 0x8318, (u8
)(vsync_porch
% 256));
186 regmap_write(lt9611
->regmap
, 0x8319, (u8
)(hfront_porch
% 256));
188 regmap_write(lt9611
->regmap
, 0x831a, (u8
)(hsync_porch
/ 256));
189 regmap_write(lt9611
->regmap
, 0x831b, (u8
)(hsync_porch
% 256));
192 static void lt9611_pcr_setup(struct lt9611
*lt9611
, const struct drm_display_mode
*mode
)
194 const struct reg_sequence reg_cfg
[] = {
214 const struct reg_sequence reg_cfg2
[] = {
227 regmap_multi_reg_write(lt9611
->regmap
, reg_cfg
, ARRAY_SIZE(reg_cfg
));
229 switch (mode
->hdisplay
) {
231 regmap_write(lt9611
->regmap
, 0x8326, 0x14);
234 regmap_write(lt9611
->regmap
, 0x8326, 0x37);
237 regmap_multi_reg_write(lt9611
->regmap
, reg_cfg2
, ARRAY_SIZE(reg_cfg2
));
242 regmap_write(lt9611
->regmap
, 0x8011, 0x5a);
243 regmap_write(lt9611
->regmap
, 0x8011, 0xfa);
246 static int lt9611_pll_setup(struct lt9611
*lt9611
, const struct drm_display_mode
*mode
)
248 unsigned int pclk
= mode
->clock
;
249 const struct reg_sequence reg_cfg
[] = {
262 regmap_multi_reg_write(lt9611
->regmap
, reg_cfg
, ARRAY_SIZE(reg_cfg
));
265 regmap_write(lt9611
->regmap
, 0x812d, 0x88);
266 else if (pclk
> 70000)
267 regmap_write(lt9611
->regmap
, 0x812d, 0x99);
269 regmap_write(lt9611
->regmap
, 0x812d, 0xaa);
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
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] */
281 regmap_write(lt9611
->regmap
, 0x82de, 0x20);
282 regmap_write(lt9611
->regmap
, 0x82de, 0xe0);
284 regmap_write(lt9611
->regmap
, 0x8016, 0xf1);
285 regmap_write(lt9611
->regmap
, 0x8016, 0xf3);
290 static int lt9611_read_video_check(struct lt9611
*lt9611
, unsigned int reg
)
292 unsigned int temp
, temp2
;
295 ret
= regmap_read(lt9611
->regmap
, reg
, &temp
);
299 ret
= regmap_read(lt9611
->regmap
, reg
+ 1, &temp2
);
303 return (temp
+ temp2
);
306 static int lt9611_video_check(struct lt9611
*lt9611
)
308 u32 v_total
, vactive
, hactive_a
, hactive_b
, h_total_sysclk
;
311 /* top module video check */
314 temp
= lt9611_read_video_check(lt9611
, 0x8282);
320 temp
= lt9611_read_video_check(lt9611
, 0x826c);
326 temp
= lt9611_read_video_check(lt9611
, 0x8286);
329 h_total_sysclk
= temp
;
332 temp
= lt9611_read_video_check(lt9611
, 0x8382);
335 hactive_a
= temp
/ 3;
338 temp
= lt9611_read_video_check(lt9611
, 0x8386);
341 hactive_b
= temp
/ 3;
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
);
350 dev_err(lt9611
->dev
, "read video check error\n");
354 static void lt9611_hdmi_tx_digital(struct lt9611
*lt9611
)
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 */
360 regmap_write(lt9611
->regmap
, 0x82d6, 0x8c);
361 regmap_write(lt9611
->regmap
, 0x82d7, 0x04);
364 static void lt9611_hdmi_tx_phy(struct lt9611
*lt9611
)
366 struct reg_sequence reg_cfg
[] = {
368 { 0x8131, 0x44 }, /* HDMI DC mode */
385 reg_cfg
[2].def
= 0x73;
387 regmap_multi_reg_write(lt9611
->regmap
, reg_cfg
, ARRAY_SIZE(reg_cfg
));
390 static irqreturn_t
lt9611_irq_thread_handler(int irq
, void *dev_id
)
392 struct lt9611
*lt9611
= dev_id
;
393 unsigned int irq_flag0
= 0;
394 unsigned int irq_flag3
= 0;
396 regmap_read(lt9611
->regmap
, 0x820f, &irq_flag3
);
397 regmap_read(lt9611
->regmap
, 0x820c, &irq_flag0
);
399 /* hpd changed low */
400 if (irq_flag3
& 0x80) {
401 dev_info(lt9611
->dev
, "hdmi cable disconnected\n");
403 regmap_write(lt9611
->regmap
, 0x8207, 0xbf);
404 regmap_write(lt9611
->regmap
, 0x8207, 0x3f);
407 /* hpd changed high */
408 if (irq_flag3
& 0x40) {
409 dev_info(lt9611
->dev
, "hdmi cable connected\n");
411 regmap_write(lt9611
->regmap
, 0x8207, 0x7f);
412 regmap_write(lt9611
->regmap
, 0x8207, 0x3f);
415 if (irq_flag3
& 0xc0 && lt9611
->bridge
.dev
)
416 drm_kms_helper_hotplug_event(lt9611
->bridge
.dev
);
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);
430 static void lt9611_enable_hpd_interrupts(struct lt9611
*lt9611
)
434 regmap_read(lt9611
->regmap
, 0x8203, &val
);
437 regmap_write(lt9611
->regmap
, 0x8203, val
);
438 regmap_write(lt9611
->regmap
, 0x8207, 0xff); /* clear */
439 regmap_write(lt9611
->regmap
, 0x8207, 0x3f);
442 static void lt9611_sleep_setup(struct lt9611
*lt9611
)
444 const struct reg_sequence sleep_setup
[] = {
447 { 0x8157, 0x03 }, /* set addr pin as output */
449 { 0x8151, 0x30 }, /* disable IRQ */
450 { 0x8102, 0x48 }, /* MIPI Rx power down */
453 { 0x8100, 0x01 }, /* bandgap power down */
454 { 0x8101, 0x00 }, /* system clk power down */
457 regmap_multi_reg_write(lt9611
->regmap
,
458 sleep_setup
, ARRAY_SIZE(sleep_setup
));
459 lt9611
->sleep
= true;
462 static int lt9611_power_on(struct lt9611
*lt9611
)
465 const struct reg_sequence seq
[] = {
466 /* LT9611_System_Init */
467 { 0x8101, 0x18 }, /* sel xtal clock */
469 /* timer for frequency meter */
470 { 0x821b, 0x69 }, /* timer 2 */
472 { 0x82cb, 0x69 }, /* timer 1 */
477 { 0x8258, 0x0a }, /* hpd irq */
478 { 0x8259, 0x80 }, /* hpd debounce width */
479 { 0x829e, 0xf7 }, /* video check irq */
481 /* power consumption for work */
490 if (lt9611
->power_on
)
493 ret
= regmap_multi_reg_write(lt9611
->regmap
, seq
, ARRAY_SIZE(seq
));
495 lt9611
->power_on
= true;
500 static int lt9611_power_off(struct lt9611
*lt9611
)
504 ret
= regmap_write(lt9611
->regmap
, 0x8130, 0x6a);
506 lt9611
->power_on
= false;
511 static void lt9611_reset(struct lt9611
*lt9611
)
513 gpiod_set_value_cansleep(lt9611
->reset_gpio
, 1);
516 gpiod_set_value_cansleep(lt9611
->reset_gpio
, 0);
519 gpiod_set_value_cansleep(lt9611
->reset_gpio
, 1);
523 static void lt9611_assert_5v(struct lt9611
*lt9611
)
525 if (!lt9611
->enable_gpio
)
528 gpiod_set_value_cansleep(lt9611
->enable_gpio
, 1);
532 static int lt9611_regulator_init(struct lt9611
*lt9611
)
536 lt9611
->supplies
[0].supply
= "vdd";
537 lt9611
->supplies
[1].supply
= "vcc";
539 ret
= devm_regulator_bulk_get(lt9611
->dev
, 2, lt9611
->supplies
);
543 return regulator_set_load(lt9611
->supplies
[0].consumer
, 300000);
546 static int lt9611_regulator_enable(struct lt9611
*lt9611
)
550 ret
= regulator_enable(lt9611
->supplies
[0].consumer
);
554 usleep_range(1000, 10000);
556 ret
= regulator_enable(lt9611
->supplies
[1].consumer
);
558 regulator_disable(lt9611
->supplies
[0].consumer
);
565 static struct lt9611_mode
*lt9611_find_mode(const struct drm_display_mode
*mode
)
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 <9611_modes
[i
];
580 /* connector funcs */
581 static enum drm_connector_status
582 lt9611_connector_detect(struct drm_connector
*connector
, bool force
)
584 struct lt9611
*lt9611
= connector_to_lt9611(connector
);
585 unsigned int reg_val
= 0;
588 regmap_read(lt9611
->regmap
, 0x825e, ®_val
);
589 connected
= (reg_val
& BIT(2));
591 lt9611
->status
= connected
? connector_status_connected
:
592 connector_status_disconnected
;
594 return lt9611
->status
;
597 static int lt9611_read_edid(struct lt9611
*lt9611
)
603 /* memset to clear old buffer, if any */
604 memset(lt9611
->edid_buf
, 0, sizeof(lt9611
->edid_buf
));
606 regmap_write(lt9611
->regmap
, 0x8503, 0xc9);
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);
613 /* length for read */
614 regmap_write(lt9611
->regmap
, 0x8506, EDID_LEN
);
615 regmap_write(lt9611
->regmap
, 0x8514, 0x7f);
617 for (i
= 0; i
< EDID_LOOP
; i
++) {
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);
625 regmap_read(lt9611
->regmap
, 0x8540, &temp
);
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
;
633 } else if (temp
& DDC_NO_ACK
) { /* DDC No Ack or Abitration lost */
634 dev_err(lt9611
->dev
, "read edid failed: no ack\n");
639 dev_err(lt9611
->dev
, "read edid failed: access not done\n");
646 regmap_write(lt9611
->regmap
, 0x8507, 0x1f);
651 lt9611_get_edid_block(void *data
, u8
*buf
, unsigned int block
, size_t len
)
653 struct lt9611
*lt9611
= data
;
659 /* supports up to 1 extension block */
660 /* TODO: add support for more extension blocks */
665 ret
= lt9611_read_edid(lt9611
);
667 dev_err(lt9611
->dev
, "edid read failed\n");
673 memcpy(buf
, lt9611
->edid_buf
+ (block
* 128), len
);
678 static int lt9611_connector_get_modes(struct drm_connector
*connector
)
680 struct lt9611
*lt9611
= connector_to_lt9611(connector
);
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
);
693 static enum drm_mode_status
694 lt9611_connector_mode_valid(struct drm_connector
*connector
,
695 struct drm_display_mode
*mode
)
697 struct lt9611_mode
*lt9611_mode
= lt9611_find_mode(mode
);
699 return lt9611_mode
? MODE_OK
: MODE_BAD
;
703 static void lt9611_bridge_enable(struct drm_bridge
*bridge
)
705 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
707 if (lt9611_power_on(lt9611
)) {
708 dev_err(lt9611
->dev
, "power on failed\n");
712 lt9611_mipi_input_analog(lt9611
);
713 lt9611_hdmi_tx_digital(lt9611
);
714 lt9611_hdmi_tx_phy(lt9611
);
718 lt9611_video_check(lt9611
);
720 /* Enable HDMI output */
721 regmap_write(lt9611
->regmap
, 0x8130, 0xea);
724 static void lt9611_bridge_disable(struct drm_bridge
*bridge
)
726 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
729 /* Disable HDMI output */
730 ret
= regmap_write(lt9611
->regmap
, 0x8130, 0x6a);
732 dev_err(lt9611
->dev
, "video on failed\n");
736 if (lt9611_power_off(lt9611
)) {
737 dev_err(lt9611
->dev
, "power on failed\n");
743 drm_connector_helper_funcs lt9611_bridge_connector_helper_funcs
= {
744 .get_modes
= lt9611_connector_get_modes
,
745 .mode_valid
= lt9611_connector_mode_valid
,
748 static const struct drm_connector_funcs lt9611_bridge_connector_funcs
= {
749 .fill_modes
= drm_helper_probe_single_connector_modes
,
750 .detect
= lt9611_connector_detect
,
751 .destroy
= drm_connector_cleanup
,
752 .reset
= drm_atomic_helper_connector_reset
,
753 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
754 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
757 static struct mipi_dsi_device
*lt9611_attach_dsi(struct lt9611
*lt9611
,
758 struct device_node
*dsi_node
)
760 const struct mipi_dsi_device_info info
= { "lt9611", 0, NULL
};
761 struct mipi_dsi_device
*dsi
;
762 struct mipi_dsi_host
*host
;
765 host
= of_find_mipi_dsi_host_by_node(dsi_node
);
767 dev_err(lt9611
->dev
, "failed to find dsi host\n");
768 return ERR_PTR(-EPROBE_DEFER
);
771 dsi
= mipi_dsi_device_register_full(host
, &info
);
773 dev_err(lt9611
->dev
, "failed to create dsi device\n");
778 dsi
->format
= MIPI_DSI_FMT_RGB888
;
779 dsi
->mode_flags
= MIPI_DSI_MODE_VIDEO
| MIPI_DSI_MODE_VIDEO_SYNC_PULSE
|
780 MIPI_DSI_MODE_VIDEO_HSE
;
782 ret
= mipi_dsi_attach(dsi
);
784 dev_err(lt9611
->dev
, "failed to attach dsi to host\n");
785 mipi_dsi_device_unregister(dsi
);
792 static void lt9611_bridge_detach(struct drm_bridge
*bridge
)
794 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
797 mipi_dsi_detach(lt9611
->dsi1
);
798 mipi_dsi_device_unregister(lt9611
->dsi1
);
801 mipi_dsi_detach(lt9611
->dsi0
);
802 mipi_dsi_device_unregister(lt9611
->dsi0
);
805 static int lt9611_connector_init(struct drm_bridge
*bridge
, struct lt9611
*lt9611
)
809 ret
= drm_connector_init(bridge
->dev
, <9611
->connector
,
810 <9611_bridge_connector_funcs
,
811 DRM_MODE_CONNECTOR_HDMIA
);
813 DRM_ERROR("Failed to initialize connector with drm\n");
817 drm_connector_helper_add(<9611
->connector
,
818 <9611_bridge_connector_helper_funcs
);
819 drm_connector_attach_encoder(<9611
->connector
, bridge
->encoder
);
821 if (!bridge
->encoder
) {
822 DRM_ERROR("Parent encoder object not found");
829 static int lt9611_bridge_attach(struct drm_bridge
*bridge
,
830 enum drm_bridge_attach_flags flags
)
832 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
835 if (!(flags
& DRM_BRIDGE_ATTACH_NO_CONNECTOR
)) {
836 ret
= lt9611_connector_init(bridge
, lt9611
);
841 /* Attach primary DSI */
842 lt9611
->dsi0
= lt9611_attach_dsi(lt9611
, lt9611
->dsi0_node
);
843 if (IS_ERR(lt9611
->dsi0
))
844 return PTR_ERR(lt9611
->dsi0
);
846 /* Attach secondary DSI, if specified */
847 if (lt9611
->dsi1_node
) {
848 lt9611
->dsi1
= lt9611_attach_dsi(lt9611
, lt9611
->dsi1_node
);
849 if (IS_ERR(lt9611
->dsi1
)) {
850 ret
= PTR_ERR(lt9611
->dsi1
);
851 goto err_unregister_dsi0
;
858 lt9611_bridge_detach(bridge
);
859 drm_connector_cleanup(<9611
->connector
);
860 mipi_dsi_device_unregister(lt9611
->dsi0
);
865 static enum drm_mode_status
lt9611_bridge_mode_valid(struct drm_bridge
*bridge
,
866 const struct drm_display_info
*info
,
867 const struct drm_display_mode
*mode
)
869 struct lt9611_mode
*lt9611_mode
= lt9611_find_mode(mode
);
871 return lt9611_mode
? MODE_OK
: MODE_BAD
;
874 static void lt9611_bridge_pre_enable(struct drm_bridge
*bridge
)
876 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
881 lt9611_reset(lt9611
);
882 regmap_write(lt9611
->regmap
, 0x80ee, 0x01);
884 lt9611
->sleep
= false;
887 static void lt9611_bridge_post_disable(struct drm_bridge
*bridge
)
889 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
891 lt9611_sleep_setup(lt9611
);
894 static void lt9611_bridge_mode_set(struct drm_bridge
*bridge
,
895 const struct drm_display_mode
*mode
,
896 const struct drm_display_mode
*adj_mode
)
898 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
899 struct hdmi_avi_infoframe avi_frame
;
902 lt9611_bridge_pre_enable(bridge
);
904 lt9611_mipi_input_digital(lt9611
, mode
);
905 lt9611_pll_setup(lt9611
, mode
);
906 lt9611_mipi_video_setup(lt9611
, mode
);
907 lt9611_pcr_setup(lt9611
, mode
);
909 ret
= drm_hdmi_avi_infoframe_from_display_mode(&avi_frame
,
913 lt9611
->vic
= avi_frame
.video_code
;
916 static enum drm_connector_status
lt9611_bridge_detect(struct drm_bridge
*bridge
)
918 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
919 unsigned int reg_val
= 0;
922 regmap_read(lt9611
->regmap
, 0x825e, ®_val
);
923 connected
= reg_val
& BIT(2);
925 lt9611
->status
= connected
? connector_status_connected
:
926 connector_status_disconnected
;
928 return lt9611
->status
;
931 static struct edid
*lt9611_bridge_get_edid(struct drm_bridge
*bridge
,
932 struct drm_connector
*connector
)
934 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
936 lt9611_power_on(lt9611
);
937 return drm_do_get_edid(connector
, lt9611_get_edid_block
, lt9611
);
940 static void lt9611_bridge_hpd_enable(struct drm_bridge
*bridge
)
942 struct lt9611
*lt9611
= bridge_to_lt9611(bridge
);
944 lt9611_enable_hpd_interrupts(lt9611
);
947 static const struct drm_bridge_funcs lt9611_bridge_funcs
= {
948 .attach
= lt9611_bridge_attach
,
949 .detach
= lt9611_bridge_detach
,
950 .mode_valid
= lt9611_bridge_mode_valid
,
951 .enable
= lt9611_bridge_enable
,
952 .disable
= lt9611_bridge_disable
,
953 .post_disable
= lt9611_bridge_post_disable
,
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
,
960 static int lt9611_parse_dt(struct device
*dev
,
961 struct lt9611
*lt9611
)
963 lt9611
->dsi0_node
= of_graph_get_remote_node(dev
->of_node
, 0, -1);
964 if (!lt9611
->dsi0_node
) {
965 dev_err(lt9611
->dev
, "failed to get remote node for primary dsi\n");
969 lt9611
->dsi1_node
= of_graph_get_remote_node(dev
->of_node
, 1, -1);
971 lt9611
->ac_mode
= of_property_read_bool(dev
->of_node
, "lt,ac-mode");
976 static int lt9611_gpio_init(struct lt9611
*lt9611
)
978 struct device
*dev
= lt9611
->dev
;
980 lt9611
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
981 if (IS_ERR(lt9611
->reset_gpio
)) {
982 dev_err(dev
, "failed to acquire reset gpio\n");
983 return PTR_ERR(lt9611
->reset_gpio
);
986 lt9611
->enable_gpio
= devm_gpiod_get_optional(dev
, "enable",
988 if (IS_ERR(lt9611
->enable_gpio
)) {
989 dev_err(dev
, "failed to acquire enable gpio\n");
990 return PTR_ERR(lt9611
->enable_gpio
);
996 static int lt9611_read_device_rev(struct lt9611
*lt9611
)
1001 regmap_write(lt9611
->regmap
, 0x80ee, 0x01);
1002 ret
= regmap_read(lt9611
->regmap
, 0x8002, &rev
);
1004 dev_err(lt9611
->dev
, "failed to read revision: %d\n", ret
);
1006 dev_info(lt9611
->dev
, "LT9611 revision: 0x%x\n", rev
);
1011 static int lt9611_hdmi_hw_params(struct device
*dev
, void *data
,
1012 struct hdmi_codec_daifmt
*fmt
,
1013 struct hdmi_codec_params
*hparms
)
1015 struct lt9611
*lt9611
= data
;
1017 if (hparms
->sample_rate
== 48000)
1018 regmap_write(lt9611
->regmap
, 0x840f, 0x2b);
1019 else if (hparms
->sample_rate
== 96000)
1020 regmap_write(lt9611
->regmap
, 0x840f, 0xab);
1024 regmap_write(lt9611
->regmap
, 0x8435, 0x00);
1025 regmap_write(lt9611
->regmap
, 0x8436, 0x18);
1026 regmap_write(lt9611
->regmap
, 0x8437, 0x00);
1031 static int lt9611_audio_startup(struct device
*dev
, void *data
)
1033 struct lt9611
*lt9611
= data
;
1035 regmap_write(lt9611
->regmap
, 0x82d6, 0x8c);
1036 regmap_write(lt9611
->regmap
, 0x82d7, 0x04);
1038 regmap_write(lt9611
->regmap
, 0x8406, 0x08);
1039 regmap_write(lt9611
->regmap
, 0x8407, 0x10);
1041 regmap_write(lt9611
->regmap
, 0x8434, 0xd5);
1046 static void lt9611_audio_shutdown(struct device
*dev
, void *data
)
1048 struct lt9611
*lt9611
= data
;
1050 regmap_write(lt9611
->regmap
, 0x8406, 0x00);
1051 regmap_write(lt9611
->regmap
, 0x8407, 0x00);
1054 static int lt9611_hdmi_i2s_get_dai_id(struct snd_soc_component
*component
,
1055 struct device_node
*endpoint
)
1057 struct of_endpoint of_ep
;
1060 ret
= of_graph_parse_endpoint(endpoint
, &of_ep
);
1065 * HDMI sound should be located as reg = <2>
1066 * Then, it is sound port 0
1068 if (of_ep
.port
== 2)
1074 static const struct hdmi_codec_ops lt9611_codec_ops
= {
1075 .hw_params
= lt9611_hdmi_hw_params
,
1076 .audio_shutdown
= lt9611_audio_shutdown
,
1077 .audio_startup
= lt9611_audio_startup
,
1078 .get_dai_id
= lt9611_hdmi_i2s_get_dai_id
,
1081 static struct hdmi_codec_pdata codec_data
= {
1082 .ops
= <9611_codec_ops
,
1083 .max_i2s_channels
= 8,
1087 static int lt9611_audio_init(struct device
*dev
, struct lt9611
*lt9611
)
1089 codec_data
.data
= lt9611
;
1090 lt9611
->audio_pdev
=
1091 platform_device_register_data(dev
, HDMI_CODEC_DRV_NAME
,
1092 PLATFORM_DEVID_AUTO
,
1093 &codec_data
, sizeof(codec_data
));
1095 return PTR_ERR_OR_ZERO(lt9611
->audio_pdev
);
1098 static void lt9611_audio_exit(struct lt9611
*lt9611
)
1100 if (lt9611
->audio_pdev
) {
1101 platform_device_unregister(lt9611
->audio_pdev
);
1102 lt9611
->audio_pdev
= NULL
;
1106 static int lt9611_probe(struct i2c_client
*client
,
1107 const struct i2c_device_id
*id
)
1109 struct lt9611
*lt9611
;
1110 struct device
*dev
= &client
->dev
;
1113 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
1114 dev_err(dev
, "device doesn't support I2C\n");
1118 lt9611
= devm_kzalloc(dev
, sizeof(*lt9611
), GFP_KERNEL
);
1122 lt9611
->dev
= &client
->dev
;
1123 lt9611
->client
= client
;
1124 lt9611
->sleep
= false;
1126 lt9611
->regmap
= devm_regmap_init_i2c(client
, <9611_regmap_config
);
1127 if (IS_ERR(lt9611
->regmap
)) {
1128 dev_err(lt9611
->dev
, "regmap i2c init failed\n");
1129 return PTR_ERR(lt9611
->regmap
);
1132 ret
= lt9611_parse_dt(&client
->dev
, lt9611
);
1134 dev_err(dev
, "failed to parse device tree\n");
1138 ret
= lt9611_gpio_init(lt9611
);
1142 ret
= lt9611_regulator_init(lt9611
);
1146 lt9611_assert_5v(lt9611
);
1148 ret
= lt9611_regulator_enable(lt9611
);
1152 lt9611_reset(lt9611
);
1154 ret
= lt9611_read_device_rev(lt9611
);
1156 dev_err(dev
, "failed to read chip rev\n");
1157 goto err_disable_regulators
;
1160 ret
= devm_request_threaded_irq(dev
, client
->irq
, NULL
,
1161 lt9611_irq_thread_handler
,
1162 IRQF_ONESHOT
, "lt9611", lt9611
);
1164 dev_err(dev
, "failed to request irq\n");
1165 goto err_disable_regulators
;
1168 i2c_set_clientdata(client
, lt9611
);
1170 lt9611
->bridge
.funcs
= <9611_bridge_funcs
;
1171 lt9611
->bridge
.of_node
= client
->dev
.of_node
;
1172 lt9611
->bridge
.ops
= DRM_BRIDGE_OP_DETECT
| DRM_BRIDGE_OP_EDID
|
1173 DRM_BRIDGE_OP_HPD
| DRM_BRIDGE_OP_MODES
;
1174 lt9611
->bridge
.type
= DRM_MODE_CONNECTOR_HDMIA
;
1176 drm_bridge_add(<9611
->bridge
);
1178 lt9611_enable_hpd_interrupts(lt9611
);
1180 return lt9611_audio_init(dev
, lt9611
);
1182 err_disable_regulators
:
1183 regulator_bulk_disable(ARRAY_SIZE(lt9611
->supplies
), lt9611
->supplies
);
1186 of_node_put(lt9611
->dsi1_node
);
1187 of_node_put(lt9611
->dsi0_node
);
1192 static int lt9611_remove(struct i2c_client
*client
)
1194 struct lt9611
*lt9611
= i2c_get_clientdata(client
);
1196 disable_irq(client
->irq
);
1197 lt9611_audio_exit(lt9611
);
1198 drm_bridge_remove(<9611
->bridge
);
1200 regulator_bulk_disable(ARRAY_SIZE(lt9611
->supplies
), lt9611
->supplies
);
1202 of_node_put(lt9611
->dsi1_node
);
1203 of_node_put(lt9611
->dsi0_node
);
1208 static struct i2c_device_id lt9611_id
[] = {
1209 { "lontium,lt9611", 0 },
1213 static const struct of_device_id lt9611_match_table
[] = {
1214 { .compatible
= "lontium,lt9611" },
1217 MODULE_DEVICE_TABLE(of
, lt9611_match_table
);
1219 static struct i2c_driver lt9611_driver
= {
1222 .of_match_table
= lt9611_match_table
,
1224 .probe
= lt9611_probe
,
1225 .remove
= lt9611_remove
,
1226 .id_table
= lt9611_id
,
1228 module_i2c_driver(lt9611_driver
);
1230 MODULE_LICENSE("GPL v2");