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;
46 struct regulator_bulk_data supplies[7];
52 static int lt8912_write_init_config(struct lt8912 *lt)
54 const struct reg_sequence seq[] = {
89 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
92 static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
94 const struct reg_sequence seq[] = {
102 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
105 static int lt8912_write_dds_config(struct lt8912 *lt)
107 const struct reg_sequence seq[] = {
155 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
158 static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
162 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
163 usleep_range(10000, 20000);
164 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
169 /* enable LVDS output with some hardcoded configuration, not required for the HDMI output */
170 static int lt8912_write_lvds_config(struct lt8912 *lt)
172 const struct reg_sequence seq[] = {
178 {0x50, 0x24}, // cp=50uA
179 {0x51, 0x2d}, // Pix_clk as reference, second order passive LPF PLL
180 {0x52, 0x04}, // loopdiv=0, use second-order PLL
181 {0x69, 0x0e}, // CP_PRESET_DIV_RATIO
184 {0x6c, 0xb8}, // RGD_CP_SOFT_K_EN,RGD_CP_SOFT_K[13:8]
187 {0x04, 0xfb}, // core pll reset
191 {0x7f, 0x00}, // disable scaler
192 {0xa8, 0x13}, // 0x13: JEIDA, 0x33: VESA
194 {0x02, 0xf7}, // lvds pll reset
200 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
203 static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
205 return container_of(b, struct lt8912, bridge);
208 static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
210 return container_of(c, struct lt8912, connector);
213 static const struct regmap_config lt8912_regmap_config = {
216 .max_register = 0xff,
219 static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
223 * At this time we only initialize 2 chips, but the lt8912 provides
224 * a third interface for the audio over HDMI configuration.
226 struct i2c_board_info info[] = {
227 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
228 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
234 for (i = 0; i < ARRAY_SIZE(info); i++) {
236 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
238 if (IS_ERR(lt->i2c_client[i]))
239 return PTR_ERR(lt->i2c_client[i]);
242 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
243 <8912_regmap_config);
244 if (IS_ERR(lt->regmap[i]))
245 return PTR_ERR(lt->regmap[i]);
250 static int lt8912_free_i2c(struct lt8912 *lt)
254 for (i = 1; i < I2C_MAX_IDX; i++)
255 i2c_unregister_device(lt->i2c_client[i]);
260 static int lt8912_hard_power_on(struct lt8912 *lt)
264 ret = regulator_bulk_enable(ARRAY_SIZE(lt->supplies), lt->supplies);
268 gpiod_set_value_cansleep(lt->gp_reset, 0);
274 static void lt8912_hard_power_off(struct lt8912 *lt)
276 gpiod_set_value_cansleep(lt->gp_reset, 1);
279 regulator_bulk_disable(ARRAY_SIZE(lt->supplies), lt->supplies);
281 lt->is_power_on = false;
284 static int lt8912_video_setup(struct lt8912 *lt)
286 u32 hactive, h_total, hpw, hfp, hbp;
287 u32 vactive, v_total, vpw, vfp, vbp;
289 int ret, hsync_activehigh, vsync_activehigh;
294 hactive = lt->mode.hactive;
295 hfp = lt->mode.hfront_porch;
296 hpw = lt->mode.hsync_len;
297 hbp = lt->mode.hback_porch;
298 h_total = hactive + hfp + hpw + hbp;
299 hsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_HSYNC_HIGH;
301 vactive = lt->mode.vactive;
302 vfp = lt->mode.vfront_porch;
303 vpw = lt->mode.vsync_len;
304 vbp = lt->mode.vback_porch;
305 v_total = vactive + vfp + vpw + vbp;
306 vsync_activehigh = lt->mode.flags & DISPLAY_FLAGS_VSYNC_HIGH;
310 else if (vactive == 1080)
313 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
314 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
316 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
317 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
318 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
320 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
322 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
323 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
325 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
326 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
328 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
329 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
331 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
332 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
334 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
335 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
337 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
338 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
340 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(0),
341 vsync_activehigh ? BIT(0) : 0);
342 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xab, BIT(1),
343 hsync_activehigh ? BIT(1) : 0);
344 ret |= regmap_update_bits(lt->regmap[I2C_MAIN], 0xb2, BIT(0),
345 lt->connector.display_info.is_hdmi ? BIT(0) : 0);
350 static int lt8912_soft_power_on(struct lt8912 *lt)
352 if (!lt->is_power_on) {
353 u32 lanes = lt->data_lanes;
355 lt8912_write_init_config(lt);
356 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
358 lt8912_write_mipi_basic_config(lt);
360 lt->is_power_on = true;
366 static int lt8912_video_on(struct lt8912 *lt)
370 ret = lt8912_video_setup(lt);
374 ret = lt8912_write_dds_config(lt);
378 ret = lt8912_write_rxlogicres_config(lt);
382 ret = lt8912_write_lvds_config(lt);
390 static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
393 unsigned int reg_val;
395 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
397 return connector_status_unknown;
399 if (reg_val & BIT(7))
400 return connector_status_connected;
402 return connector_status_disconnected;
405 static enum drm_connector_status
406 lt8912_connector_detect(struct drm_connector *connector, bool force)
408 struct lt8912 *lt = connector_to_lt8912(connector);
410 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
411 return drm_bridge_detect(lt->hdmi_port);
413 return lt8912_check_cable_status(lt);
416 static const struct drm_connector_funcs lt8912_connector_funcs = {
417 .detect = lt8912_connector_detect,
418 .fill_modes = drm_helper_probe_single_connector_modes,
419 .destroy = drm_connector_cleanup,
420 .reset = drm_atomic_helper_connector_reset,
421 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
422 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
425 static enum drm_mode_status
426 lt8912_connector_mode_valid(struct drm_connector *connector,
427 struct drm_display_mode *mode)
429 if (mode->clock > 150000)
430 return MODE_CLOCK_HIGH;
432 if (mode->hdisplay > 1920)
433 return MODE_BAD_HVALUE;
435 if (mode->vdisplay > 1080)
436 return MODE_BAD_VVALUE;
441 static int lt8912_connector_get_modes(struct drm_connector *connector)
443 const struct drm_edid *drm_edid;
446 struct lt8912 *lt = connector_to_lt8912(connector);
447 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
449 drm_edid = drm_bridge_edid_read(lt->hdmi_port, connector);
450 drm_edid_connector_update(connector, drm_edid);
452 num = drm_edid_connector_add_modes(connector);
457 ret = drm_display_info_set_bus_formats(&connector->display_info,
462 drm_edid_free(drm_edid);
466 static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
467 .get_modes = lt8912_connector_get_modes,
468 .mode_valid = lt8912_connector_mode_valid,
471 static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
472 const struct drm_display_mode *mode,
473 const struct drm_display_mode *adj)
475 struct lt8912 *lt = bridge_to_lt8912(bridge);
477 drm_display_mode_to_videomode(adj, <->mode);
480 static void lt8912_bridge_enable(struct drm_bridge *bridge)
482 struct lt8912 *lt = bridge_to_lt8912(bridge);
487 static int lt8912_attach_dsi(struct lt8912 *lt)
489 struct device *dev = lt->dev;
490 struct mipi_dsi_host *host;
491 struct mipi_dsi_device *dsi;
493 const struct mipi_dsi_device_info info = { .type = "lt8912",
498 host = of_find_mipi_dsi_host_by_node(lt->host_node);
500 dev_err(dev, "failed to find dsi host\n");
501 return -EPROBE_DEFER;
504 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
507 dev_err(dev, "failed to create dsi device (%d)\n", ret);
513 dsi->lanes = lt->data_lanes;
514 dsi->format = MIPI_DSI_FMT_RGB888;
516 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
518 MIPI_DSI_MODE_NO_EOT_PACKET;
520 ret = devm_mipi_dsi_attach(dev, dsi);
522 dev_err(dev, "failed to attach dsi to host\n");
529 static void lt8912_bridge_hpd_cb(void *data, enum drm_connector_status status)
531 struct lt8912 *lt = data;
534 drm_helper_hpd_irq_event(lt->bridge.dev);
537 static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
540 struct lt8912 *lt = bridge_to_lt8912(bridge);
541 struct drm_connector *connector = <->connector;
543 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD) {
544 drm_bridge_hpd_enable(lt->hdmi_port, lt8912_bridge_hpd_cb, lt);
545 connector->polled = DRM_CONNECTOR_POLL_HPD;
547 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
548 DRM_CONNECTOR_POLL_DISCONNECT;
551 ret = drm_connector_init(bridge->dev, connector,
552 <8912_connector_funcs,
553 lt->hdmi_port->type);
557 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
559 connector->dpms = DRM_MODE_DPMS_OFF;
560 drm_connector_attach_encoder(connector, bridge->encoder);
566 static int lt8912_bridge_attach(struct drm_bridge *bridge,
567 enum drm_bridge_attach_flags flags)
569 struct lt8912 *lt = bridge_to_lt8912(bridge);
572 ret = drm_bridge_attach(bridge->encoder, lt->hdmi_port, bridge,
573 DRM_BRIDGE_ATTACH_NO_CONNECTOR);
575 dev_err(lt->dev, "Failed to attach next bridge (%d)\n", ret);
579 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
580 ret = lt8912_bridge_connector_init(bridge);
582 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
587 ret = lt8912_hard_power_on(lt);
591 ret = lt8912_soft_power_on(lt);
598 lt8912_hard_power_off(lt);
602 static void lt8912_bridge_detach(struct drm_bridge *bridge)
604 struct lt8912 *lt = bridge_to_lt8912(bridge);
606 lt8912_hard_power_off(lt);
608 if (lt->connector.dev && lt->hdmi_port->ops & DRM_BRIDGE_OP_HPD)
609 drm_bridge_hpd_disable(lt->hdmi_port);
612 static enum drm_connector_status
613 lt8912_bridge_detect(struct drm_bridge *bridge)
615 struct lt8912 *lt = bridge_to_lt8912(bridge);
617 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
618 return drm_bridge_detect(lt->hdmi_port);
620 return lt8912_check_cable_status(lt);
623 static const struct drm_edid *lt8912_bridge_edid_read(struct drm_bridge *bridge,
624 struct drm_connector *connector)
626 struct lt8912 *lt = bridge_to_lt8912(bridge);
629 * edid must be read through the ddc bus but it must be
630 * given to the hdmi connector node.
632 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
633 return drm_bridge_edid_read(lt->hdmi_port, connector);
635 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
639 static const struct drm_bridge_funcs lt8912_bridge_funcs = {
640 .attach = lt8912_bridge_attach,
641 .detach = lt8912_bridge_detach,
642 .mode_set = lt8912_bridge_mode_set,
643 .enable = lt8912_bridge_enable,
644 .detect = lt8912_bridge_detect,
645 .edid_read = lt8912_bridge_edid_read,
648 static int lt8912_bridge_resume(struct device *dev)
650 struct lt8912 *lt = dev_get_drvdata(dev);
653 ret = lt8912_hard_power_on(lt);
657 ret = lt8912_soft_power_on(lt);
661 return lt8912_video_on(lt);
664 static int lt8912_bridge_suspend(struct device *dev)
666 struct lt8912 *lt = dev_get_drvdata(dev);
668 lt8912_hard_power_off(lt);
673 static DEFINE_SIMPLE_DEV_PM_OPS(lt8912_bridge_pm_ops, lt8912_bridge_suspend, lt8912_bridge_resume);
675 static int lt8912_get_regulators(struct lt8912 *lt)
678 const char * const supply_names[] = {
679 "vdd", "vccmipirx", "vccsysclk", "vcclvdstx",
680 "vcchdmitx", "vcclvdspll", "vcchdmipll"
683 for (i = 0; i < ARRAY_SIZE(lt->supplies); i++)
684 lt->supplies[i].supply = supply_names[i];
686 return devm_regulator_bulk_get(lt->dev, ARRAY_SIZE(lt->supplies),
690 static int lt8912_parse_dt(struct lt8912 *lt)
692 struct gpio_desc *gp_reset;
693 struct device *dev = lt->dev;
696 struct device_node *port_node;
698 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
699 if (IS_ERR(gp_reset)) {
700 ret = PTR_ERR(gp_reset);
701 if (ret != -EPROBE_DEFER)
702 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
705 lt->gp_reset = gp_reset;
707 data_lanes = drm_of_get_data_lanes_count_ep(dev->of_node, 0, -1, 1, 4);
708 if (data_lanes < 0) {
709 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
713 lt->data_lanes = data_lanes;
715 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
716 if (!lt->host_node) {
717 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
721 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
723 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
725 goto err_free_host_node;
728 lt->hdmi_port = of_drm_find_bridge(port_node);
729 if (!lt->hdmi_port) {
731 dev_err_probe(lt->dev, ret, "%s: Failed to get hdmi port\n", __func__);
732 goto err_free_host_node;
735 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
736 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
738 goto err_free_host_node;
741 ret = lt8912_get_regulators(lt);
743 goto err_free_host_node;
745 of_node_put(port_node);
749 of_node_put(port_node);
750 of_node_put(lt->host_node);
754 static int lt8912_put_dt(struct lt8912 *lt)
756 of_node_put(lt->host_node);
760 static int lt8912_probe(struct i2c_client *client)
762 static struct lt8912 *lt;
764 struct device *dev = &client->dev;
766 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
771 lt->i2c_client[0] = client;
773 ret = lt8912_parse_dt(lt);
777 ret = lt8912_init_i2c(lt, client);
781 i2c_set_clientdata(client, lt);
783 lt->bridge.funcs = <8912_bridge_funcs;
784 lt->bridge.of_node = dev->of_node;
785 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
786 DRM_BRIDGE_OP_DETECT);
788 drm_bridge_add(<->bridge);
790 ret = lt8912_attach_dsi(lt);
797 drm_bridge_remove(<->bridge);
805 static void lt8912_remove(struct i2c_client *client)
807 struct lt8912 *lt = i2c_get_clientdata(client);
809 drm_bridge_remove(<->bridge);
814 static const struct of_device_id lt8912_dt_match[] = {
815 {.compatible = "lontium,lt8912b"},
818 MODULE_DEVICE_TABLE(of, lt8912_dt_match);
820 static const struct i2c_device_id lt8912_id[] = {
824 MODULE_DEVICE_TABLE(i2c, lt8912_id);
826 static struct i2c_driver lt8912_i2c_driver = {
829 .of_match_table = lt8912_dt_match,
830 .pm = pm_sleep_ptr(<8912_bridge_pm_ops),
832 .probe = lt8912_probe,
833 .remove = lt8912_remove,
834 .id_table = lt8912_id,
836 module_i2c_driver(lt8912_i2c_driver);
838 MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
839 MODULE_DESCRIPTION("lt8912 drm driver");
840 MODULE_LICENSE("GPL v2");