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_bridge_connector.h>
14 #include <drm/dp/drm_dp_helper.h>
15 #include <drm/drm_of.h>
16 #include <drm/drm_panel.h>
17 #include <drm/drm_probe_helper.h>
18 #include <drm/drm_simple_kms_helper.h>
19 
20 #include "rockchip_drm_drv.h"
21 #include "rockchip_drm_vop.h"
22 #include "rockchip_rgb.h"
23 
24 #define encoder_to_rgb(c) container_of(c, struct rockchip_rgb, encoder)
25 
26 struct rockchip_rgb {
27 	struct device *dev;
28 	struct drm_device *drm_dev;
29 	struct drm_bridge *bridge;
30 	struct drm_encoder encoder;
31 	struct drm_connector connector;
32 	int output_mode;
33 };
34 
35 static int
36 rockchip_rgb_encoder_atomic_check(struct drm_encoder *encoder,
37 				   struct drm_crtc_state *crtc_state,
38 				   struct drm_connector_state *conn_state)
39 {
40 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
41 	struct drm_connector *connector = conn_state->connector;
42 	struct drm_display_info *info = &connector->display_info;
43 	u32 bus_format;
44 
45 	if (info->num_bus_formats)
46 		bus_format = info->bus_formats[0];
47 	else
48 		bus_format = MEDIA_BUS_FMT_RGB888_1X24;
49 
50 	switch (bus_format) {
51 	case MEDIA_BUS_FMT_RGB666_1X18:
52 		s->output_mode = ROCKCHIP_OUT_MODE_P666;
53 		break;
54 	case MEDIA_BUS_FMT_RGB565_1X16:
55 		s->output_mode = ROCKCHIP_OUT_MODE_P565;
56 		break;
57 	case MEDIA_BUS_FMT_RGB888_1X24:
58 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
59 	default:
60 		s->output_mode = ROCKCHIP_OUT_MODE_P888;
61 		break;
62 	}
63 
64 	s->output_type = DRM_MODE_CONNECTOR_LVDS;
65 
66 	return 0;
67 }
68 
69 static const
70 struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
71 	.atomic_check = rockchip_rgb_encoder_atomic_check,
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 	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, 0);
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, 0, endpoint_id,
108 						  &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;
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 	ret = drm_connector_attach_encoder(connector, encoder);
164 	if (ret < 0) {
165 		DRM_DEV_ERROR(drm_dev->dev,
166 			      "failed to attach encoder: %d\n", ret);
167 		goto err_free_connector;
168 	}
169 
170 	return rgb;
171 
172 err_free_connector:
173 	drm_connector_cleanup(connector);
174 err_free_encoder:
175 	drm_encoder_cleanup(encoder);
176 	return ERR_PTR(ret);
177 }
178 EXPORT_SYMBOL_GPL(rockchip_rgb_init);
179 
180 void rockchip_rgb_fini(struct rockchip_rgb *rgb)
181 {
182 	drm_panel_bridge_remove(rgb->bridge);
183 	drm_connector_cleanup(&rgb->connector);
184 	drm_encoder_cleanup(&rgb->encoder);
185 }
186 EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
187