xref: /linux/drivers/gpu/drm/rockchip/rockchip_rgb.c (revision 9f2c9170934eace462499ba0bfe042cc72900173)
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 #define encoder_to_rgb(c) container_of(c, struct rockchip_rgb, encoder)
26 
27 struct rockchip_rgb {
28 	struct device *dev;
29 	struct drm_device *drm_dev;
30 	struct drm_bridge *bridge;
31 	struct drm_encoder encoder;
32 	struct drm_connector connector;
33 	int output_mode;
34 };
35 
36 static int
37 rockchip_rgb_encoder_atomic_check(struct drm_encoder *encoder,
38 				   struct drm_crtc_state *crtc_state,
39 				   struct drm_connector_state *conn_state)
40 {
41 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
42 	struct drm_connector *connector = conn_state->connector;
43 	struct drm_display_info *info = &connector->display_info;
44 	u32 bus_format;
45 
46 	if (info->num_bus_formats)
47 		bus_format = info->bus_formats[0];
48 	else
49 		bus_format = MEDIA_BUS_FMT_RGB888_1X24;
50 
51 	switch (bus_format) {
52 	case MEDIA_BUS_FMT_RGB666_1X18:
53 		s->output_mode = ROCKCHIP_OUT_MODE_P666;
54 		break;
55 	case MEDIA_BUS_FMT_RGB565_1X16:
56 		s->output_mode = ROCKCHIP_OUT_MODE_P565;
57 		break;
58 	case MEDIA_BUS_FMT_RGB888_1X24:
59 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
60 	default:
61 		s->output_mode = ROCKCHIP_OUT_MODE_P888;
62 		break;
63 	}
64 
65 	s->output_type = DRM_MODE_CONNECTOR_LVDS;
66 
67 	return 0;
68 }
69 
70 static const
71 struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
72 	.atomic_check = rockchip_rgb_encoder_atomic_check,
73 };
74 
75 struct rockchip_rgb *rockchip_rgb_init(struct device *dev,
76 				       struct drm_crtc *crtc,
77 				       struct drm_device *drm_dev)
78 {
79 	struct rockchip_rgb *rgb;
80 	struct drm_encoder *encoder;
81 	struct device_node *port, *endpoint;
82 	u32 endpoint_id;
83 	int ret = 0, child_count = 0;
84 	struct drm_panel *panel;
85 	struct drm_bridge *bridge;
86 	struct drm_connector *connector;
87 
88 	rgb = devm_kzalloc(dev, sizeof(*rgb), GFP_KERNEL);
89 	if (!rgb)
90 		return ERR_PTR(-ENOMEM);
91 
92 	rgb->dev = dev;
93 	rgb->drm_dev = drm_dev;
94 
95 	port = of_graph_get_port_by_id(dev->of_node, 0);
96 	if (!port)
97 		return ERR_PTR(-EINVAL);
98 
99 	for_each_child_of_node(port, endpoint) {
100 		if (of_property_read_u32(endpoint, "reg", &endpoint_id))
101 			endpoint_id = 0;
102 
103 		/* if subdriver (> 0) or error case (< 0), ignore entry */
104 		if (rockchip_drm_endpoint_is_subdriver(endpoint) != 0)
105 			continue;
106 
107 		child_count++;
108 		ret = drm_of_find_panel_or_bridge(dev->of_node, 0, endpoint_id,
109 						  &panel, &bridge);
110 		if (!ret) {
111 			of_node_put(endpoint);
112 			break;
113 		}
114 	}
115 
116 	of_node_put(port);
117 
118 	/* if the rgb output is not connected to anything, just return */
119 	if (!child_count)
120 		return NULL;
121 
122 	if (ret < 0) {
123 		if (ret != -EPROBE_DEFER)
124 			DRM_DEV_ERROR(dev, "failed to find panel or bridge %d\n", ret);
125 		return ERR_PTR(ret);
126 	}
127 
128 	encoder = &rgb->encoder;
129 	encoder->possible_crtcs = drm_crtc_mask(crtc);
130 
131 	ret = drm_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_NONE);
132 	if (ret < 0) {
133 		DRM_DEV_ERROR(drm_dev->dev,
134 			      "failed to initialize encoder: %d\n", ret);
135 		return ERR_PTR(ret);
136 	}
137 
138 	drm_encoder_helper_add(encoder, &rockchip_rgb_encoder_helper_funcs);
139 
140 	if (panel) {
141 		bridge = drm_panel_bridge_add_typed(panel,
142 						    DRM_MODE_CONNECTOR_LVDS);
143 		if (IS_ERR(bridge))
144 			return ERR_CAST(bridge);
145 	}
146 
147 	rgb->bridge = bridge;
148 
149 	ret = drm_bridge_attach(encoder, rgb->bridge, NULL,
150 				DRM_BRIDGE_ATTACH_NO_CONNECTOR);
151 	if (ret)
152 		goto err_free_encoder;
153 
154 	connector = &rgb->connector;
155 	connector = drm_bridge_connector_init(rgb->drm_dev, encoder);
156 	if (IS_ERR(connector)) {
157 		DRM_DEV_ERROR(drm_dev->dev,
158 			      "failed to initialize bridge connector: %pe\n",
159 			      connector);
160 		ret = PTR_ERR(connector);
161 		goto err_free_encoder;
162 	}
163 
164 	ret = drm_connector_attach_encoder(connector, encoder);
165 	if (ret < 0) {
166 		DRM_DEV_ERROR(drm_dev->dev,
167 			      "failed to attach encoder: %d\n", ret);
168 		goto err_free_connector;
169 	}
170 
171 	return rgb;
172 
173 err_free_connector:
174 	drm_connector_cleanup(connector);
175 err_free_encoder:
176 	drm_encoder_cleanup(encoder);
177 	return ERR_PTR(ret);
178 }
179 EXPORT_SYMBOL_GPL(rockchip_rgb_init);
180 
181 void rockchip_rgb_fini(struct rockchip_rgb *rgb)
182 {
183 	drm_panel_bridge_remove(rgb->bridge);
184 	drm_connector_cleanup(&rgb->connector);
185 	drm_encoder_cleanup(&rgb->encoder);
186 }
187 EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
188