1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
6 #include <linux/device.h>
7 #include <linux/delay.h>
8 #include <linux/gpio/consumer.h>
10 #include <linux/media-bus-format.h>
11 #include <linux/regmap.h>
13 #include <drm/drm_probe_helper.h>
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_edid.h>
16 #include <drm/drm_mipi_dsi.h>
17 #include <drm/drm_of.h>
19 #include <video/videomode.h>
22 #define I2C_ADDR_MAIN 0x48
25 #define I2C_ADDR_CEC_DSI 0x49
31 struct drm_bridge bridge;
32 struct drm_connector connector;
34 struct i2c_client *i2c_client[I2C_MAX_IDX];
35 struct regmap *regmap[I2C_MAX_IDX];
37 struct device_node *host_node;
38 struct drm_bridge *hdmi_port;
40 struct mipi_dsi_device *dsi;
42 struct gpio_desc *gp_reset;
44 struct videomode mode;
50 static int lt8912_write_init_config(struct lt8912 *lt)
52 const struct reg_sequence seq[] = {
87 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
90 static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
92 const struct reg_sequence seq[] = {
100 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
103 static int lt8912_write_dds_config(struct lt8912 *lt)
105 const struct reg_sequence seq[] = {
153 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
156 static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
160 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
161 usleep_range(10000, 20000);
162 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
167 /* enable LVDS output with some hardcoded configuration, not required for the HDMI output */
168 static int lt8912_write_lvds_config(struct lt8912 *lt)
170 const struct reg_sequence seq[] = {
176 {0x50, 0x24}, // cp=50uA
177 {0x51, 0x2d}, // Pix_clk as reference, second order passive LPF PLL
178 {0x52, 0x04}, // loopdiv=0, use second-order PLL
179 {0x69, 0x0e}, // CP_PRESET_DIV_RATIO
182 {0x6c, 0xb8}, // RGD_CP_SOFT_K_EN,RGD_CP_SOFT_K[13:8]
185 {0x04, 0xfb}, // core pll reset
189 {0x7f, 0x00}, // disable scaler
190 {0xa8, 0x13}, // 0x13: JEIDA, 0x33: VESA
192 {0x02, 0xf7}, // lvds pll reset
198 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
201 static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
203 return container_of(b, struct lt8912, bridge);
206 static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
208 return container_of(c, struct lt8912, connector);
211 static const struct regmap_config lt8912_regmap_config = {
214 .max_register = 0xff,
217 static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
221 * At this time we only initialize 2 chips, but the lt8912 provides
222 * a third interface for the audio over HDMI configuration.
224 struct i2c_board_info info[] = {
225 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
226 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
232 for (i = 0; i < ARRAY_SIZE(info); i++) {
234 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
236 if (IS_ERR(lt->i2c_client[i]))
237 return PTR_ERR(lt->i2c_client[i]);
240 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
241 <8912_regmap_config);
242 if (IS_ERR(lt->regmap[i]))
243 return PTR_ERR(lt->regmap[i]);
248 static int lt8912_free_i2c(struct lt8912 *lt)
252 for (i = 1; i < I2C_MAX_IDX; i++)
253 i2c_unregister_device(lt->i2c_client[i]);
258 static int lt8912_hard_power_on(struct lt8912 *lt)
260 gpiod_set_value_cansleep(lt->gp_reset, 0);
266 static void lt8912_hard_power_off(struct lt8912 *lt)
268 gpiod_set_value_cansleep(lt->gp_reset, 1);
270 lt->is_power_on = false;
273 static int lt8912_video_setup(struct lt8912 *lt)
275 u32 hactive, h_total, hpw, hfp, hbp;
276 u32 vactive, v_total, vpw, vfp, vbp;
278 int ret, hsync_activehigh, vsync_activehigh;
283 hactive = lt->mode.hactive;
284 hfp = lt->mode.hfront_porch;
285 hpw = lt->mode.hsync_len;
286 hbp = lt->mode.hback_porch;
287 h_total = hactive + hfp + hpw + hbp;
288 hsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH;
290 vactive = lt->mode.vactive;
291 vfp = lt->mode.vfront_porch;
292 vpw = lt->mode.vsync_len;
293 vbp = lt->mode.vback_porch;
294 v_total = vactive + vfp + vpw + vbp;
295 vsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH;
299 else if (vactive == 1080)
302 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
303 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
304 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
305 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
306 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
307 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
309 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
311 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
312 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
314 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
317 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
318 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
320 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
321 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
323 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
324 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
326 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
327 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
329 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(0),
330 vsync_activehigh ? BIT(0) : 0);
331 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(1),
332 hsync_activehigh ? BIT(1) : 0);
333 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xb2, BIT(0),
334 lt->connector.display_info.is_hdmi ? BIT(0) : 0);
339 static int lt8912_soft_power_on(struct lt8912 *lt)
341 if (!lt->is_power_on) {
342 u32 lanes = lt->data_lanes;
344 lt8912_write_init_config(lt);
345 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
347 lt8912_write_mipi_basic_config(lt);
349 lt->is_power_on = true;
355 static int lt8912_video_on(struct lt8912 *lt)
359 ret = lt8912_video_setup(lt);
363 ret = lt8912_write_dds_config(lt);
367 ret = lt8912_write_rxlogicres_config(lt);
371 ret = lt8912_write_lvds_config(lt);
379 static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
382 unsigned int reg_val;
384 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
386 return connector_status_unknown;
388 if (reg_val & BIT(7))
389 return connector_status_connected;
391 return connector_status_disconnected;
394 static enum drm_connector_status
395 lt8912_connector_detect(struct drm_connector *connector, bool force)
397 struct lt8912 *lt = connector_to_lt8912(connector);
399 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
400 return drm_bridge_detect(lt->hdmi_port);
402 return lt8912_check_cable_status(lt);
405 static const struct drm_connector_funcs lt8912_connector_funcs = {
406 .detect = lt8912_connector_detect,
407 .fill_modes = drm_helper_probe_single_connector_modes,
408 .destroy = drm_connector_cleanup,
409 .reset = drm_atomic_helper_connector_reset,
410 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
411 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
414 static enum drm_mode_status
415 lt8912_connector_mode_valid(struct drm_connector *connector,
416 struct drm_display_mode *mode)
418 if (mode->clock > 150000)
419 return MODE_CLOCK_HIGH;
421 if (mode->hdisplay > 1920)
422 return MODE_BAD_HVALUE;
424 if (mode->vdisplay > 1080)
425 return MODE_BAD_VVALUE;
430 static int lt8912_connector_get_modes(struct drm_connector *connector)
435 struct lt8912 *lt = connector_to_lt8912(connector);
436 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
438 edid = drm_bridge_get_edid(lt->hdmi_port, connector);
440 drm_connector_update_edid_property(connector, edid);
441 num = drm_add_edid_modes(connector, edid);
446 ret = drm_display_info_set_bus_formats(&connector->display_info,
455 static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
456 .get_modes = lt8912_connector_get_modes,
457 .mode_valid = lt8912_connector_mode_valid,
460 static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
461 const struct drm_display_mode *mode,
462 const struct drm_display_mode *adj)
464 struct lt8912 *lt = bridge_to_lt8912(bridge);
466 drm_display_mode_to_videomode(adj, <->mode);
469 static void lt8912_bridge_enable(struct drm_bridge *bridge)
471 struct lt8912 *lt = bridge_to_lt8912(bridge);
476 static int lt8912_attach_dsi(struct lt8912 *lt)
478 struct device *dev = lt->dev;
479 struct mipi_dsi_host *host;
480 struct mipi_dsi_device *dsi;
482 const struct mipi_dsi_device_info info = { .type = "lt8912",
487 host = of_find_mipi_dsi_host_by_node(lt->host_node);
489 dev_err(dev, "failed to find dsi host\n");
490 return -EPROBE_DEFER;
493 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
496 dev_err(dev, "failed to create dsi device (%d)\n", ret);
502 dsi->lanes = lt->data_lanes;
503 dsi->format = MIPI_DSI_FMT_RGB888;
505 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
507 MIPI_DSI_MODE_NO_EOT_PACKET;
509 ret = devm_mipi_dsi_attach(dev, dsi);
511 dev_err(dev, "failed to attach dsi to host\n");
518 static void lt8912_bridge_hpd_cb(void *data, enum drm_connector_status status)
520 struct lt8912 *lt = data;
523 drm_helper_hpd_irq_event(lt->bridge.dev);
526 static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
529 struct lt8912 *lt = bridge_to_lt8912(bridge);
530 struct drm_connector *connector = <->connector;
532 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD) {
533 drm_bridge_hpd_enable(lt->hdmi_port, lt8912_bridge_hpd_cb, lt);
534 connector->polled = DRM_CONNECTOR_POLL_HPD;
536 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
537 DRM_CONNECTOR_POLL_DISCONNECT;
540 ret = drm_connector_init(bridge->dev, connector,
541 <8912_connector_funcs,
542 lt->hdmi_port->type);
546 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
548 connector->dpms = DRM_MODE_DPMS_OFF;
549 drm_connector_attach_encoder(connector, bridge->encoder);
555 static int lt8912_bridge_attach(struct drm_bridge *bridge,
556 enum drm_bridge_attach_flags flags)
558 struct lt8912 *lt = bridge_to_lt8912(bridge);
561 ret = drm_bridge_attach(bridge->encoder, lt->hdmi_port, bridge,
562 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
564 dev_err(lt->dev, "Failed to attach next bridge (%d)\n", ret);
568 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
569 ret = lt8912_bridge_connector_init(bridge);
571 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
576 ret = lt8912_hard_power_on(lt);
580 ret = lt8912_soft_power_on(lt);
587 lt8912_hard_power_off(lt);
591 static void lt8912_bridge_detach(struct drm_bridge *bridge)
593 struct lt8912 *lt = bridge_to_lt8912(bridge);
595 lt8912_hard_power_off(lt);
597 if (lt->connector.dev && lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD)
598 drm_bridge_hpd_disable(lt->hdmi_port);
601 static enum drm_connector_status
602 lt8912_bridge_detect(struct drm_bridge *bridge)
604 struct lt8912 *lt = bridge_to_lt8912(bridge);
606 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
607 return drm_bridge_detect(lt->hdmi_port);
609 return lt8912_check_cable_status(lt);
612 static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
613 struct drm_connector *connector)
615 struct lt8912 *lt = bridge_to_lt8912(bridge);
618 * edid must be read through the ddc bus but it must be
619 * given to the hdmi connector node.
621 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
622 return drm_bridge_get_edid(lt->hdmi_port, connector);
624 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
628 static const struct drm_bridge_funcs lt8912_bridge_funcs = {
629 .attach = lt8912_bridge_attach,
630 .detach = lt8912_bridge_detach,
631 .mode_set = lt8912_bridge_mode_set,
632 .enable = lt8912_bridge_enable,
633 .detect = lt8912_bridge_detect,
634 .get_edid = lt8912_bridge_get_edid,
637 static int lt8912_parse_dt(struct lt8912 *lt)
639 struct gpio_desc *gp_reset;
640 struct device *dev = lt->dev;
643 struct device_node *port_node;
645 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
646 if (IS_ERR(gp_reset)) {
647 ret = PTR_ERR(gp_reset);
648 if (ret != -EPROBE_DEFER)
649 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
652 lt->gp_reset = gp_reset;
654 data_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, -1, 1, 4);
655 if (data_lanes < 0) {
656 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
660 lt->data_lanes = data_lanes;
662 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
663 if (!lt->host_node) {
664 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
668 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
670 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
672 goto err_free_host_node;
675 lt->hdmi_port = of_drm_find_bridge(port_node);
676 if (!lt->hdmi_port) {
678 dev_err_probe(lt->dev, ret, "%s: Failed to get hdmi port\n", __func__);
679 goto err_free_host_node;
682 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
683 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
685 goto err_free_host_node;
688 of_node_put(port_node);
692 of_node_put(port_node);
693 of_node_put(lt->host_node);
697 static int lt8912_put_dt(struct lt8912 *lt)
699 of_node_put(lt->host_node);
703 static int lt8912_probe(struct i2c_client *client)
705 static struct lt8912 *lt;
707 struct device *dev = &client->dev;
709 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
714 lt->i2c_client[0] = client;
716 ret = lt8912_parse_dt(lt);
720 ret = lt8912_init_i2c(lt, client);
724 i2c_set_clientdata(client, lt);
726 lt->bridge.funcs = <8912_bridge_funcs;
727 lt->bridge.of_node = dev->of_node;
728 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
729 DRM_BRIDGE_OP_DETECT);
731 drm_bridge_add(<->bridge);
733 ret = lt8912_attach_dsi(lt);
740 drm_bridge_remove(<->bridge);
748 static void lt8912_remove(struct i2c_client *client)
750 struct lt8912 *lt = i2c_get_clientdata(client);
752 drm_bridge_remove(<->bridge);
757 static const struct of_device_id lt8912_dt_match[] = {
758 {.compatible = "lontium,lt8912b"},
761 MODULE_DEVICE_TABLE(of, lt8912_dt_match);
763 static const struct i2c_device_id lt8912_id[] = {
767 MODULE_DEVICE_TABLE(i2c, lt8912_id);
769 static struct i2c_driver lt8912_i2c_driver = {
772 .of_match_table = lt8912_dt_match,
774 .probe = lt8912_probe,
775 .remove = lt8912_remove,
776 .id_table = lt8912_id,
778 module_i2c_driver(lt8912_i2c_driver);
780 MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
781 MODULE_DESCRIPTION("lt8912 drm driver");
782 MODULE_LICENSE("GPL v2");