1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd 4 * Author: 5 * Sandy Huang <hjc@rock-chips.com> 6 */ 7 8 #include <linux/component.h> 9 #include <linux/of_graph.h> 10 11 #include <drm/drm_atomic_helper.h> 12 #include <drm/drm_bridge.h> 13 #include <drm/drm_dp_helper.h> 14 #include <drm/drm_of.h> 15 #include <drm/drm_panel.h> 16 #include <drm/drm_probe_helper.h> 17 18 #include "rockchip_drm_drv.h" 19 #include "rockchip_drm_vop.h" 20 21 #define encoder_to_rgb(c) container_of(c, struct rockchip_rgb, encoder) 22 23 struct rockchip_rgb { 24 struct device *dev; 25 struct drm_device *drm_dev; 26 struct drm_bridge *bridge; 27 struct drm_encoder encoder; 28 int output_mode; 29 }; 30 31 static int 32 rockchip_rgb_encoder_atomic_check(struct drm_encoder *encoder, 33 struct drm_crtc_state *crtc_state, 34 struct drm_connector_state *conn_state) 35 { 36 struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state); 37 struct drm_connector *connector = conn_state->connector; 38 struct drm_display_info *info = &connector->display_info; 39 u32 bus_format; 40 41 if (info->num_bus_formats) 42 bus_format = info->bus_formats[0]; 43 else 44 bus_format = MEDIA_BUS_FMT_RGB888_1X24; 45 46 switch (bus_format) { 47 case MEDIA_BUS_FMT_RGB666_1X18: 48 s->output_mode = ROCKCHIP_OUT_MODE_P666; 49 break; 50 case MEDIA_BUS_FMT_RGB565_1X16: 51 s->output_mode = ROCKCHIP_OUT_MODE_P565; 52 break; 53 case MEDIA_BUS_FMT_RGB888_1X24: 54 case MEDIA_BUS_FMT_RGB666_1X24_CPADHI: 55 default: 56 s->output_mode = ROCKCHIP_OUT_MODE_P888; 57 break; 58 } 59 60 s->output_type = DRM_MODE_CONNECTOR_LVDS; 61 62 return 0; 63 } 64 65 static const 66 struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = { 67 .atomic_check = rockchip_rgb_encoder_atomic_check, 68 }; 69 70 static const struct drm_encoder_funcs rockchip_rgb_encoder_funcs = { 71 .destroy = drm_encoder_cleanup, 72 }; 73 74 struct rockchip_rgb *rockchip_rgb_init(struct device *dev, 75 struct drm_crtc *crtc, 76 struct drm_device *drm_dev) 77 { 78 struct rockchip_rgb *rgb; 79 struct drm_encoder *encoder; 80 struct device_node *port, *endpoint; 81 u32 endpoint_id; 82 int ret = 0, child_count = 0; 83 struct drm_panel *panel; 84 struct drm_bridge *bridge; 85 86 rgb = devm_kzalloc(dev, sizeof(*rgb), GFP_KERNEL); 87 if (!rgb) 88 return ERR_PTR(-ENOMEM); 89 90 rgb->dev = dev; 91 rgb->drm_dev = drm_dev; 92 93 port = of_graph_get_port_by_id(dev->of_node, 0); 94 if (!port) 95 return ERR_PTR(-EINVAL); 96 97 for_each_child_of_node(port, endpoint) { 98 if (of_property_read_u32(endpoint, "reg", &endpoint_id)) 99 endpoint_id = 0; 100 101 /* if subdriver (> 0) or error case (< 0), ignore entry */ 102 if (rockchip_drm_endpoint_is_subdriver(endpoint) != 0) 103 continue; 104 105 child_count++; 106 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, endpoint_id, 107 &panel, &bridge); 108 if (!ret) { 109 of_node_put(endpoint); 110 break; 111 } 112 } 113 114 of_node_put(port); 115 116 /* if the rgb output is not connected to anything, just return */ 117 if (!child_count) 118 return NULL; 119 120 if (ret < 0) { 121 if (ret != -EPROBE_DEFER) 122 DRM_DEV_ERROR(dev, "failed to find panel or bridge %d\n", ret); 123 return ERR_PTR(ret); 124 } 125 126 encoder = &rgb->encoder; 127 encoder->possible_crtcs = drm_crtc_mask(crtc); 128 129 ret = drm_encoder_init(drm_dev, encoder, &rockchip_rgb_encoder_funcs, 130 DRM_MODE_ENCODER_NONE, NULL); 131 if (ret < 0) { 132 DRM_DEV_ERROR(drm_dev->dev, 133 "failed to initialize encoder: %d\n", ret); 134 return ERR_PTR(ret); 135 } 136 137 drm_encoder_helper_add(encoder, &rockchip_rgb_encoder_helper_funcs); 138 139 if (panel) { 140 bridge = drm_panel_bridge_add_typed(panel, 141 DRM_MODE_CONNECTOR_LVDS); 142 if (IS_ERR(bridge)) 143 return ERR_CAST(bridge); 144 } 145 146 rgb->bridge = bridge; 147 148 ret = drm_bridge_attach(encoder, rgb->bridge, NULL, 0); 149 if (ret) { 150 DRM_DEV_ERROR(drm_dev->dev, 151 "failed to attach bridge: %d\n", ret); 152 goto err_free_encoder; 153 } 154 155 return rgb; 156 157 err_free_encoder: 158 drm_encoder_cleanup(encoder); 159 return ERR_PTR(ret); 160 } 161 EXPORT_SYMBOL_GPL(rockchip_rgb_init); 162 163 void rockchip_rgb_fini(struct rockchip_rgb *rgb) 164 { 165 drm_panel_bridge_remove(rgb->bridge); 166 drm_encoder_cleanup(&rgb->encoder); 167 } 168 EXPORT_SYMBOL_GPL(rockchip_rgb_fini); 169