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/media-bus-format.h>
10 #include <linux/of_graph.h>
11 
12 #include <drm/display/drm_dp_helper.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_bridge.h>
15 #include <drm/drm_bridge_connector.h>
16 #include <drm/drm_of.h>
17 #include <drm/drm_panel.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_simple_kms_helper.h>
20 
21 #include "rockchip_drm_drv.h"
22 #include "rockchip_drm_vop.h"
23 #include "rockchip_rgb.h"
24 
25 struct rockchip_rgb {
26 	struct device *dev;
27 	struct drm_device *drm_dev;
28 	struct drm_bridge *bridge;
29 	struct rockchip_encoder encoder;
30 	struct drm_connector connector;
31 	int output_mode;
32 };
33 
34 static int
35 rockchip_rgb_encoder_atomic_check(struct drm_encoder *encoder,
36 				   struct drm_crtc_state *crtc_state,
37 				   struct drm_connector_state *conn_state)
38 {
39 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
40 	struct drm_connector *connector = conn_state->connector;
41 	struct drm_display_info *info = &connector->display_info;
42 	u32 bus_format;
43 
44 	if (info->num_bus_formats)
45 		bus_format = info->bus_formats[0];
46 	else
47 		bus_format = MEDIA_BUS_FMT_RGB888_1X24;
48 
49 	switch (bus_format) {
50 	case MEDIA_BUS_FMT_RGB666_1X18:
51 		s->output_mode = ROCKCHIP_OUT_MODE_P666;
52 		break;
53 	case MEDIA_BUS_FMT_RGB565_1X16:
54 		s->output_mode = ROCKCHIP_OUT_MODE_P565;
55 		break;
56 	case MEDIA_BUS_FMT_RGB888_1X24:
57 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
58 	default:
59 		s->output_mode = ROCKCHIP_OUT_MODE_P888;
60 		break;
61 	}
62 
63 	s->output_type = DRM_MODE_CONNECTOR_LVDS;
64 
65 	return 0;
66 }
67 
68 static const
69 struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
70 	.atomic_check = rockchip_rgb_encoder_atomic_check,
71 };
72 
73 struct rockchip_rgb *rockchip_rgb_init(struct device *dev,
74 				       struct drm_crtc *crtc,
75 				       struct drm_device *drm_dev,
76 				       int video_port)
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 	struct drm_connector *connector;
86 
87 	rgb = devm_kzalloc(dev, sizeof(*rgb), GFP_KERNEL);
88 	if (!rgb)
89 		return ERR_PTR(-ENOMEM);
90 
91 	rgb->dev = dev;
92 	rgb->drm_dev = drm_dev;
93 
94 	port = of_graph_get_port_by_id(dev->of_node, video_port);
95 	if (!port)
96 		return ERR_PTR(-EINVAL);
97 
98 	for_each_child_of_node(port, endpoint) {
99 		if (of_property_read_u32(endpoint, "reg", &endpoint_id))
100 			endpoint_id = 0;
101 
102 		/* if subdriver (> 0) or error case (< 0), ignore entry */
103 		if (rockchip_drm_endpoint_is_subdriver(endpoint) != 0)
104 			continue;
105 
106 		child_count++;
107 		ret = drm_of_find_panel_or_bridge(dev->of_node, video_port,
108 						  endpoint_id, &panel, &bridge);
109 		if (!ret) {
110 			of_node_put(endpoint);
111 			break;
112 		}
113 	}
114 
115 	of_node_put(port);
116 
117 	/* if the rgb output is not connected to anything, just return */
118 	if (!child_count)
119 		return NULL;
120 
121 	if (ret < 0) {
122 		if (ret != -EPROBE_DEFER)
123 			DRM_DEV_ERROR(dev, "failed to find panel or bridge %d\n", ret);
124 		return ERR_PTR(ret);
125 	}
126 
127 	encoder = &rgb->encoder.encoder;
128 	encoder->possible_crtcs = drm_crtc_mask(crtc);
129 
130 	ret = drm_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_NONE);
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,
149 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
150 	if (ret)
151 		goto err_free_encoder;
152 
153 	connector = &rgb->connector;
154 	connector = drm_bridge_connector_init(rgb->drm_dev, encoder);
155 	if (IS_ERR(connector)) {
156 		DRM_DEV_ERROR(drm_dev->dev,
157 			      "failed to initialize bridge connector: %pe\n",
158 			      connector);
159 		ret = PTR_ERR(connector);
160 		goto err_free_encoder;
161 	}
162 
163 	rgb->encoder.crtc_endpoint_id = endpoint_id;
164 
165 	ret = drm_connector_attach_encoder(connector, encoder);
166 	if (ret < 0) {
167 		DRM_DEV_ERROR(drm_dev->dev,
168 			      "failed to attach encoder: %d\n", ret);
169 		goto err_free_connector;
170 	}
171 
172 	return rgb;
173 
174 err_free_connector:
175 	drm_connector_cleanup(connector);
176 err_free_encoder:
177 	drm_encoder_cleanup(encoder);
178 	return ERR_PTR(ret);
179 }
180 EXPORT_SYMBOL_GPL(rockchip_rgb_init);
181 
182 void rockchip_rgb_fini(struct rockchip_rgb *rgb)
183 {
184 	drm_panel_bridge_remove(rgb->bridge);
185 	drm_connector_cleanup(&rgb->connector);
186 	drm_encoder_cleanup(&rgb->encoder.encoder);
187 }
188 EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
189