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/gpio.h>
11 #include <linux/of_gpio.h>
12 #include <linux/regmap.h>
14 #include <drm/drm_probe_helper.h>
15 #include <drm/drm_atomic_helper.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;
51 static int lt8912_write_init_config(struct lt8912 *lt)
53 const struct reg_sequence seq[] = {
88 return regmap_multi_reg_write(lt->regmap[I2C_MAIN], seq, ARRAY_SIZE(seq));
91 static int lt8912_write_mipi_basic_config(struct lt8912 *lt)
93 const struct reg_sequence seq[] = {
101 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
104 static int lt8912_write_dds_config(struct lt8912 *lt)
106 const struct reg_sequence seq[] = {
154 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
157 static int lt8912_write_rxlogicres_config(struct lt8912 *lt)
161 ret = regmap_write(lt->regmap[I2C_MAIN], 0x03, 0x7f);
162 usleep_range(10000, 20000);
163 ret |= regmap_write(lt->regmap[I2C_MAIN], 0x03, 0xff);
168 static int lt8912_write_lvds_config(struct lt8912 *lt)
170 const struct reg_sequence seq[] = {
191 return regmap_multi_reg_write(lt->regmap[I2C_CEC_DSI], seq, ARRAY_SIZE(seq));
194 static inline struct lt8912 *bridge_to_lt8912(struct drm_bridge *b)
196 return container_of(b, struct lt8912, bridge);
199 static inline struct lt8912 *connector_to_lt8912(struct drm_connector *c)
201 return container_of(c, struct lt8912, connector);
204 static const struct regmap_config lt8912_regmap_config = {
207 .max_register = 0xff,
210 static int lt8912_init_i2c(struct lt8912 *lt, struct i2c_client *client)
214 * At this time we only initialize 2 chips, but the lt8912 provides
215 * a third interface for the audio over HDMI configuration.
217 struct i2c_board_info info[] = {
218 { I2C_BOARD_INFO("lt8912p0", I2C_ADDR_MAIN), },
219 { I2C_BOARD_INFO("lt8912p1", I2C_ADDR_CEC_DSI), },
225 for (i = 0; i < ARRAY_SIZE(info); i++) {
227 lt->i2c_client[i] = i2c_new_dummy_device(client->adapter,
229 if (IS_ERR(lt->i2c_client[i]))
230 return PTR_ERR(lt->i2c_client[i]);
233 lt->regmap[i] = devm_regmap_init_i2c(lt->i2c_client[i],
234 <8912_regmap_config);
235 if (IS_ERR(lt->regmap[i]))
236 return PTR_ERR(lt->regmap[i]);
241 static int lt8912_free_i2c(struct lt8912 *lt)
245 for (i = 1; i < I2C_MAX_IDX; i++)
246 i2c_unregister_device(lt->i2c_client[i]);
251 static int lt8912_hard_power_on(struct lt8912 *lt)
253 gpiod_set_value_cansleep(lt->gp_reset, 0);
259 static void lt8912_hard_power_off(struct lt8912 *lt)
261 gpiod_set_value_cansleep(lt->gp_reset, 1);
263 lt->is_power_on = false;
266 static int lt8912_video_setup(struct lt8912 *lt)
268 u32 hactive, h_total, hpw, hfp, hbp;
269 u32 vactive, v_total, vpw, vfp, vbp;
276 hactive = lt->mode.hactive;
277 hfp = lt->mode.hfront_porch;
278 hpw = lt->mode.hsync_len;
279 hbp = lt->mode.hback_porch;
280 h_total = hactive + hfp + hpw + hbp;
282 vactive = lt->mode.vactive;
283 vfp = lt->mode.vfront_porch;
284 vpw = lt->mode.vsync_len;
285 vbp = lt->mode.vback_porch;
286 v_total = vactive + vfp + vpw + vbp;
290 else if (vactive == 1080)
293 ret = regmap_write(lt->regmap[I2C_CEC_DSI], 0x10, 0x01);
294 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x11, settle);
295 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x18, hpw);
296 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x19, vpw);
297 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1c, hactive & 0xff);
298 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x1d, hactive >> 8);
300 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x2f, 0x0c);
302 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x34, h_total & 0xff);
303 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x35, h_total >> 8);
305 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x36, v_total & 0xff);
306 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x37, v_total >> 8);
308 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x38, vbp & 0xff);
309 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x39, vbp >> 8);
311 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3a, vfp & 0xff);
312 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3b, vfp >> 8);
314 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3c, hbp & 0xff);
315 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3d, hbp >> 8);
317 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3e, hfp & 0xff);
318 ret |= regmap_write(lt->regmap[I2C_CEC_DSI], 0x3f, hfp >> 8);
323 static int lt8912_soft_power_on(struct lt8912 *lt)
325 if (!lt->is_power_on) {
326 u32 lanes = lt->data_lanes;
328 lt8912_write_init_config(lt);
329 regmap_write(lt->regmap[I2C_CEC_DSI], 0x13, lanes & 3);
331 lt8912_write_mipi_basic_config(lt);
333 lt->is_power_on = true;
339 static int lt8912_video_on(struct lt8912 *lt)
343 ret = lt8912_video_setup(lt);
347 ret = lt8912_write_dds_config(lt);
351 ret = lt8912_write_rxlogicres_config(lt);
355 ret = lt8912_write_lvds_config(lt);
363 static enum drm_connector_status lt8912_check_cable_status(struct lt8912 *lt)
366 unsigned int reg_val;
368 ret = regmap_read(lt->regmap[I2C_MAIN], 0xC1, ®_val);
370 return connector_status_unknown;
372 if (reg_val & BIT(7))
373 return connector_status_connected;
375 return connector_status_disconnected;
378 static enum drm_connector_status
379 lt8912_connector_detect(struct drm_connector *connector, bool force)
381 struct lt8912 *lt = connector_to_lt8912(connector);
383 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
384 return drm_bridge_detect(lt->hdmi_port);
386 return lt8912_check_cable_status(lt);
389 static const struct drm_connector_funcs lt8912_connector_funcs = {
390 .detect = lt8912_connector_detect,
391 .fill_modes = drm_helper_probe_single_connector_modes,
392 .destroy = drm_connector_cleanup,
393 .reset = drm_atomic_helper_connector_reset,
394 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
395 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
398 static enum drm_mode_status
399 lt8912_connector_mode_valid(struct drm_connector *connector,
400 struct drm_display_mode *mode)
402 if (mode->clock > 150000)
403 return MODE_CLOCK_HIGH;
405 if (mode->hdisplay > 1920)
406 return MODE_BAD_HVALUE;
408 if (mode->vdisplay > 1080)
409 return MODE_BAD_VVALUE;
414 static int lt8912_connector_get_modes(struct drm_connector *connector)
419 struct lt8912 *lt = connector_to_lt8912(connector);
420 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
422 edid = drm_bridge_get_edid(lt->hdmi_port, connector);
424 drm_connector_update_edid_property(connector, edid);
425 num = drm_add_edid_modes(connector, edid);
430 ret = drm_display_info_set_bus_formats(&connector->display_info,
439 static const struct drm_connector_helper_funcs lt8912_connector_helper_funcs = {
440 .get_modes = lt8912_connector_get_modes,
441 .mode_valid = lt8912_connector_mode_valid,
444 static void lt8912_bridge_mode_set(struct drm_bridge *bridge,
445 const struct drm_display_mode *mode,
446 const struct drm_display_mode *adj)
448 struct lt8912 *lt = bridge_to_lt8912(bridge);
450 drm_display_mode_to_videomode(adj, <->mode);
453 static void lt8912_bridge_enable(struct drm_bridge *bridge)
455 struct lt8912 *lt = bridge_to_lt8912(bridge);
460 static int lt8912_attach_dsi(struct lt8912 *lt)
462 struct device *dev = lt->dev;
463 struct mipi_dsi_host *host;
464 struct mipi_dsi_device *dsi;
466 const struct mipi_dsi_device_info info = { .type = "lt8912",
471 host = of_find_mipi_dsi_host_by_node(lt->host_node);
473 dev_err(dev, "failed to find dsi host\n");
474 return -EPROBE_DEFER;
477 dsi = mipi_dsi_device_register_full(host, &info);
480 dev_err(dev, "failed to create dsi device (%d)\n", ret);
486 dsi->lanes = lt->data_lanes;
487 dsi->format = MIPI_DSI_FMT_RGB888;
489 dsi->mode_flags = MIPI_DSI_MODE_VIDEO |
490 MIPI_DSI_MODE_VIDEO_BURST |
492 MIPI_DSI_MODE_EOT_PACKET;
494 ret = mipi_dsi_attach(dsi);
496 dev_err(dev, "failed to attach dsi to host\n");
503 mipi_dsi_device_unregister(dsi);
508 static void lt8912_detach_dsi(struct lt8912 *lt)
510 mipi_dsi_detach(lt->dsi);
511 mipi_dsi_device_unregister(lt->dsi);
514 static int lt8912_bridge_connector_init(struct drm_bridge *bridge)
517 struct lt8912 *lt = bridge_to_lt8912(bridge);
518 struct drm_connector *connector = <->connector;
520 connector->polled = DRM_CONNECTOR_POLL_CONNECT |
521 DRM_CONNECTOR_POLL_DISCONNECT;
523 ret = drm_connector_init(bridge->dev, connector,
524 <8912_connector_funcs,
525 lt->hdmi_port->type);
529 drm_connector_helper_add(connector, <8912_connector_helper_funcs);
531 connector->dpms = DRM_MODE_DPMS_OFF;
532 drm_connector_attach_encoder(connector, bridge->encoder);
538 static int lt8912_bridge_attach(struct drm_bridge *bridge,
539 enum drm_bridge_attach_flags flags)
541 struct lt8912 *lt = bridge_to_lt8912(bridge);
544 if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) {
545 ret = lt8912_bridge_connector_init(bridge);
547 dev_err(lt->dev, "Failed to init bridge ! (%d)\n", ret);
552 ret = lt8912_hard_power_on(lt);
556 ret = lt8912_soft_power_on(lt);
560 ret = lt8912_attach_dsi(lt);
564 lt->is_attached = true;
569 lt8912_hard_power_off(lt);
573 static void lt8912_bridge_detach(struct drm_bridge *bridge)
575 struct lt8912 *lt = bridge_to_lt8912(bridge);
577 if (lt->is_attached) {
578 lt8912_detach_dsi(lt);
579 lt8912_hard_power_off(lt);
580 drm_connector_unregister(<->connector);
581 drm_connector_cleanup(<->connector);
585 static enum drm_connector_status
586 lt8912_bridge_detect(struct drm_bridge *bridge)
588 struct lt8912 *lt = bridge_to_lt8912(bridge);
590 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_DETECT)
591 return drm_bridge_detect(lt->hdmi_port);
593 return lt8912_check_cable_status(lt);
596 static struct edid *lt8912_bridge_get_edid(struct drm_bridge *bridge,
597 struct drm_connector *connector)
599 struct lt8912 *lt = bridge_to_lt8912(bridge);
602 * edid must be read through the ddc bus but it must be
603 * given to the hdmi connector node.
605 if (lt->hdmi_port->ops & DRM_BRIDGE_OP_EDID)
606 return drm_bridge_get_edid(lt->hdmi_port, connector);
608 dev_warn(lt->dev, "The connected bridge does not supports DRM_BRIDGE_OP_EDID\n");
612 static const struct drm_bridge_funcs lt8912_bridge_funcs = {
613 .attach = lt8912_bridge_attach,
614 .detach = lt8912_bridge_detach,
615 .mode_set = lt8912_bridge_mode_set,
616 .enable = lt8912_bridge_enable,
617 .detect = lt8912_bridge_detect,
618 .get_edid = lt8912_bridge_get_edid,
621 static int lt8912_parse_dt(struct lt8912 *lt)
623 struct gpio_desc *gp_reset;
624 struct device *dev = lt->dev;
627 struct device_node *port_node;
628 struct device_node *endpoint;
630 gp_reset = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
631 if (IS_ERR(gp_reset)) {
632 ret = PTR_ERR(gp_reset);
633 if (ret != -EPROBE_DEFER)
634 dev_err(dev, "Failed to get reset gpio: %d\n", ret);
637 lt->gp_reset = gp_reset;
639 endpoint = of_graph_get_endpoint_by_regs(dev->of_node, 0, -1);
643 data_lanes = of_property_count_u32_elems(endpoint, "data-lanes");
644 of_node_put(endpoint);
645 if (data_lanes < 0) {
646 dev_err(lt->dev, "%s: Bad data-lanes property\n", __func__);
649 lt->data_lanes = data_lanes;
651 lt->host_node = of_graph_get_remote_node(dev->of_node, 0, -1);
652 if (!lt->host_node) {
653 dev_err(lt->dev, "%s: Failed to get remote port\n", __func__);
657 port_node = of_graph_get_remote_node(dev->of_node, 1, -1);
659 dev_err(lt->dev, "%s: Failed to get connector port\n", __func__);
661 goto err_free_host_node;
664 lt->hdmi_port = of_drm_find_bridge(port_node);
665 if (!lt->hdmi_port) {
666 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
668 goto err_free_host_node;
671 if (!of_device_is_compatible(port_node, "hdmi-connector")) {
672 dev_err(lt->dev, "%s: Failed to get hdmi port\n", __func__);
674 goto err_free_host_node;
677 of_node_put(port_node);
681 of_node_put(port_node);
682 of_node_put(lt->host_node);
686 static int lt8912_put_dt(struct lt8912 *lt)
688 of_node_put(lt->host_node);
692 static int lt8912_probe(struct i2c_client *client,
693 const struct i2c_device_id *id)
695 static struct lt8912 *lt;
697 struct device *dev = &client->dev;
699 lt = devm_kzalloc(dev, sizeof(struct lt8912), GFP_KERNEL);
704 lt->i2c_client[0] = client;
706 ret = lt8912_parse_dt(lt);
710 ret = lt8912_init_i2c(lt, client);
714 i2c_set_clientdata(client, lt);
716 lt->bridge.funcs = <8912_bridge_funcs;
717 lt->bridge.of_node = dev->of_node;
718 lt->bridge.ops = (DRM_BRIDGE_OP_EDID |
719 DRM_BRIDGE_OP_DETECT);
721 drm_bridge_add(<->bridge);
731 static int lt8912_remove(struct i2c_client *client)
733 struct lt8912 *lt = i2c_get_clientdata(client);
735 lt8912_bridge_detach(<->bridge);
736 drm_bridge_remove(<->bridge);
742 static const struct of_device_id lt8912_dt_match[] = {
743 {.compatible = "lontium,lt8912b"},
746 MODULE_DEVICE_TABLE(of, lt8912_dt_match);
748 static const struct i2c_device_id lt8912_id[] = {
752 MODULE_DEVICE_TABLE(i2c, lt8912_id);
754 static struct i2c_driver lt8912_i2c_driver = {
757 .of_match_table = lt8912_dt_match,
758 .owner = THIS_MODULE,
760 .probe = lt8912_probe,
761 .remove = lt8912_remove,
762 .id_table = lt8912_id,
764 module_i2c_driver(lt8912_i2c_driver);
766 MODULE_AUTHOR("Adrien Grassein <adrien.grassein@gmail.com>");
767 MODULE_DESCRIPTION("lt8912 drm driver");
768 MODULE_LICENSE("GPL v2");