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/regmap.h>
12 #include <drm/drm_probe_helper.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_mipi_dsi.h>
15 #include <drm/drm_of.h>
17 #include <video/videomode.h>
20 #define I2C_ADDR_MAIN 0x48
23 #define I2C_ADDR_CEC_DSI 0x49
29 struct drm_bridge bridge;
30 struct drm_connector connector;
32 struct i2c_client *i2c_client[I2C_MAX_IDX];
33 struct regmap *regmap[I2C_MAX_IDX];
35 struct device_node *host_node;
36 struct drm_bridge *hdmi_port;
38 struct mipi_dsi_device *dsi;
40 struct gpio_desc *gp_reset;
42 struct videomode mode;
49 static int lt8912_write_init_config(struct lt8912 *lt)
51 const struct reg_sequence seq[] = {
86 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
89 static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
91 const struct reg_sequence seq[] = {
99 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
102 static int lt8912_write_dds_config(struct lt8912 *lt)
104 const struct reg_sequence seq[] = {
152 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
155 static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
159 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
160 usleep_range(10000, 20000);
161 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
166 static int lt8912_write_lvds_config(struct lt8912 *lt)
168 const struct reg_sequence seq[] = {
189 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
192 static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
194 return container_of(b, struct lt8912, bridge);
197 static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
199 return container_of(c, struct lt8912, connector);
202 static const struct regmap_config lt8912_regmap_config = {
205 .max_register = 0xff,
208 static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
212 * At this time we only initialize 2 chips, but the lt8912 provides
213 * a third interface for the audio over HDMI configuration.
215 struct i2c_board_info info[] = {
216 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
217 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
223 for (i = 0; i < ARRAY_SIZE(info); i++) {
225 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
227 if (IS_ERR(lt->i2c_client[i]))
228 return PTR_ERR(lt->i2c_client[i]);
231 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
232 <8912_regmap_config);
233 if (IS_ERR(lt->regmap[i]))
234 return PTR_ERR(lt->regmap[i]);
239 static int lt8912_free_i2c(struct lt8912 *lt)
243 for (i = 1; i < I2C_MAX_IDX; i++)
244 i2c_unregister_device(lt->i2c_client[i]);
249 static int lt8912_hard_power_on(struct lt8912 *lt)
251 gpiod_set_value_cansleep(lt->gp_reset, 0);
257 static void lt8912_hard_power_off(struct lt8912 *lt)
259 gpiod_set_value_cansleep(lt->gp_reset, 1);
261 lt->is_power_on = false;
264 static int lt8912_video_setup(struct lt8912 *lt)
266 u32 hactive, h_total, hpw, hfp, hbp;
267 u32 vactive, v_total, vpw, vfp, vbp;
274 hactive = lt->mode.hactive;
275 hfp = lt->mode.hfront_porch;
276 hpw = lt->mode.hsync_len;
277 hbp = lt->mode.hback_porch;
278 h_total = hactive + hfp + hpw + hbp;
280 vactive = lt->mode.vactive;
281 vfp = lt->mode.vfront_porch;
282 vpw = lt->mode.vsync_len;
283 vbp = lt->mode.vback_porch;
284 v_total = vactive + vfp + vpw + vbp;
288 else if (vactive == 1080)
291 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
292 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
293 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
294 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
295 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
296 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
298 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
300 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
301 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
303 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
304 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
306 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
307 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
309 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
310 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
312 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
313 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
316 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
321 static int lt8912_soft_power_on(struct lt8912 *lt)
323 if (!lt->is_power_on) {
324 u32 lanes = lt->data_lanes;
326 lt8912_write_init_config(lt);
327 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
329 lt8912_write_mipi_basic_config(lt);
331 lt->is_power_on = true;
337 static int lt8912_video_on(struct lt8912 *lt)
341 ret = lt8912_video_setup(lt);
345 ret = lt8912_write_dds_config(lt);
349 ret = lt8912_write_rxlogicres_config(lt);
353 ret = lt8912_write_lvds_config(lt);
361 static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
364 unsigned int reg_val;
366 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
368 return connector_status_unknown;
370 if (reg_val & BIT(7))
371 return connector_status_connected;
373 return connector_status_disconnected;
376 static enum drm_connector_status
377 lt8912_connector_detect(struct drm_connector *connector, bool force)
379 struct lt8912 *lt = connector_to_lt8912(connector);
381 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
382 return drm_bridge_detect(lt->hdmi_port);
384 return lt8912_check_cable_status(lt);
387 static const struct drm_connector_funcs lt8912_connector_funcs = {
388 .detect = lt8912_connector_detect,
389 .fill_modes = drm_helper_probe_single_connector_modes,
390 .destroy = drm_connector_cleanup,
391 .reset = drm_atomic_helper_connector_reset,
392 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
393 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
396 static enum drm_mode_status
397 lt8912_connector_mode_valid(struct drm_connector *connector,
398 struct drm_display_mode *mode)
400 if (mode->clock > 150000)
401 return MODE_CLOCK_HIGH;
403 if (mode->hdisplay > 1920)
404 return MODE_BAD_HVALUE;
406 if (mode->vdisplay > 1080)
407 return MODE_BAD_VVALUE;
412 static int lt8912_connector_get_modes(struct drm_connector *connector)
417 struct lt8912 *lt = connector_to_lt8912(connector);
418 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
420 edid = drm_bridge_get_edid(lt->hdmi_port, connector);
422 drm_connector_update_edid_property(connector, edid);
423 num = drm_add_edid_modes(connector, edid);
428 ret = drm_display_info_set_bus_formats(&connector->display_info,
437 static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
438 .get_modes = lt8912_connector_get_modes,
439 .mode_valid = lt8912_connector_mode_valid,
442 static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
443 const struct drm_display_mode *mode,
444 const struct drm_display_mode *adj)
446 struct lt8912 *lt = bridge_to_lt8912(bridge);
448 drm_display_mode_to_videomode(adj, <->mode);
451 static void lt8912_bridge_enable(struct drm_bridge *bridge)
453 struct lt8912 *lt = bridge_to_lt8912(bridge);
458 static int lt8912_attach_dsi(struct lt8912 *lt)
460 struct device *dev = lt->dev;
461 struct mipi_dsi_host *host;
462 struct mipi_dsi_device *dsi;
464 const struct mipi_dsi_device_info info = { .type = "lt8912",
469 host = of_find_mipi_dsi_host_by_node(lt->host_node);
471 dev_err(dev, "failed to find dsi host\n");
472 return -EPROBE_DEFER;
475 dsi = devm_mipi_dsi_device_register_full(dev, host, &info);
478 dev_err(dev, "failed to create dsi device (%d)\n", ret);
484 dsi->lanes = lt->data_lanes;
485 dsi->format = MIPI_DSI_FMT_RGB888;
487 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
488 MIPI_DSI_MODE_VIDEO_BURST |
490 MIPI_DSI_MODE_NO_EOT_PACKET;
492 ret = devm_mipi_dsi_attach(dev, dsi);
494 dev_err(dev, "failed to attach dsi to host\n");
501 static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
504 struct lt8912 *lt = bridge_to_lt8912(bridge);
505 struct drm_connector *connector = <->connector;
507 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
508 DRM_CONNECTOR_POLL_DISCONNECT;
510 ret = drm_connector_init(bridge->dev, connector,
511 <8912_connector_funcs,
512 lt->hdmi_port->type);
516 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
518 connector->dpms = DRM_MODE_DPMS_OFF;
519 drm_connector_attach_encoder(connector, bridge->encoder);
525 static int lt8912_bridge_attach(struct drm_bridge *bridge,
526 enum drm_bridge_attach_flags flags)
528 struct lt8912 *lt = bridge_to_lt8912(bridge);
531 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
532 ret = lt8912_bridge_connector_init(bridge);
534 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
539 ret = lt8912_hard_power_on(lt);
543 ret = lt8912_soft_power_on(lt);
547 lt->is_attached = true;
552 lt8912_hard_power_off(lt);
556 static void lt8912_bridge_detach(struct drm_bridge *bridge)
558 struct lt8912 *lt = bridge_to_lt8912(bridge);
560 if (lt->is_attached) {
561 lt8912_hard_power_off(lt);
562 drm_connector_unregister(<->connector);
563 drm_connector_cleanup(<->connector);
567 static enum drm_connector_status
568 lt8912_bridge_detect(struct drm_bridge *bridge)
570 struct lt8912 *lt = bridge_to_lt8912(bridge);
572 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
573 return drm_bridge_detect(lt->hdmi_port);
575 return lt8912_check_cable_status(lt);
578 static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
579 struct drm_connector *connector)
581 struct lt8912 *lt = bridge_to_lt8912(bridge);
584 * edid must be read through the ddc bus but it must be
585 * given to the hdmi connector node.
587 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
588 return drm_bridge_get_edid(lt->hdmi_port, connector);
590 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
594 static const struct drm_bridge_funcs lt8912_bridge_funcs = {
595 .attach = lt8912_bridge_attach,
596 .detach = lt8912_bridge_detach,
597 .mode_set = lt8912_bridge_mode_set,
598 .enable = lt8912_bridge_enable,
599 .detect = lt8912_bridge_detect,
600 .get_edid = lt8912_bridge_get_edid,
603 static int lt8912_parse_dt(struct lt8912 *lt)
605 struct gpio_desc *gp_reset;
606 struct device *dev = lt->dev;
609 struct device_node *port_node;
610 struct device_node *endpoint;
612 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
613 if (IS_ERR(gp_reset)) {
614 ret = PTR_ERR(gp_reset);
615 if (ret != -EPROBE_DEFER)
616 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
619 lt->gp_reset = gp_reset;
621 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
625 data_lanes = of_property_count_u32_elems(endpoint, "data-lanes");
626 of_node_put(endpoint);
627 if (data_lanes < 0) {
628 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
631 lt->data_lanes = data_lanes;
633 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
634 if (!lt->host_node) {
635 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
639 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
641 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
643 goto err_free_host_node;
646 lt->hdmi_port = of_drm_find_bridge(port_node);
647 if (!lt->hdmi_port) {
648 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
650 goto err_free_host_node;
653 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
654 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
656 goto err_free_host_node;
659 of_node_put(port_node);
663 of_node_put(port_node);
664 of_node_put(lt->host_node);
668 static int lt8912_put_dt(struct lt8912 *lt)
670 of_node_put(lt->host_node);
674 static int lt8912_probe(struct i2c_client *client,
675 const struct i2c_device_id *id)
677 static struct lt8912 *lt;
679 struct device *dev = &client->dev;
681 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
686 lt->i2c_client[0] = client;
688 ret = lt8912_parse_dt(lt);
692 ret = lt8912_init_i2c(lt, client);
696 i2c_set_clientdata(client, lt);
698 lt->bridge.funcs = <8912_bridge_funcs;
699 lt->bridge.of_node = dev->of_node;
700 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
701 DRM_BRIDGE_OP_DETECT);
703 drm_bridge_add(<->bridge);
705 ret = lt8912_attach_dsi(lt);
712 drm_bridge_remove(<->bridge);
720 static int lt8912_remove(struct i2c_client *client)
722 struct lt8912 *lt = i2c_get_clientdata(client);
724 lt8912_bridge_detach(<->bridge);
725 drm_bridge_remove(<->bridge);
731 static const struct of_device_id lt8912_dt_match[] = {
732 {.compatible = "lontium,lt8912b"},
735 MODULE_DEVICE_TABLE(of, lt8912_dt_match);
737 static const struct i2c_device_id lt8912_id[] = {
741 MODULE_DEVICE_TABLE(i2c, lt8912_id);
743 static struct i2c_driver lt8912_i2c_driver = {
746 .of_match_table = lt8912_dt_match,
748 .probe = lt8912_probe,
749 .remove = lt8912_remove,
750 .id_table = lt8912_id,
752 module_i2c_driver(lt8912_i2c_driver);
754 MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
755 MODULE_DESCRIPTION("lt8912 drm driver");
756 MODULE_LICENSE("GPL v2");