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 <drm/drmP.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_dp_helper.h>
11 #include <drm/drm_panel.h>
12 #include <drm/drm_of.h>
13 #include <drm/drm_probe_helper.h>
14 
15 #include <linux/component.h>
16 #include <linux/of_graph.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 (rockchip_drm_endpoint_is_subdriver(endpoint) > 0)
102 			continue;
103 
104 		child_count++;
105 		ret = drm_of_find_panel_or_bridge(dev->of_node, 0, endpoint_id,
106 						  &panel, &bridge);
107 		if (!ret) {
108 			of_node_put(endpoint);
109 			break;
110 		}
111 	}
112 
113 	of_node_put(port);
114 
115 	/* if the rgb output is not connected to anything, just return */
116 	if (!child_count)
117 		return NULL;
118 
119 	if (ret < 0) {
120 		if (ret != -EPROBE_DEFER)
121 			DRM_DEV_ERROR(dev, "failed to find panel or bridge %d\n", ret);
122 		return ERR_PTR(ret);
123 	}
124 
125 	encoder = &rgb->encoder;
126 	encoder->possible_crtcs = drm_crtc_mask(crtc);
127 
128 	ret = drm_encoder_init(drm_dev, encoder, &rockchip_rgb_encoder_funcs,
129 			       DRM_MODE_ENCODER_NONE, NULL);
130 	if (ret < 0) {
131 		DRM_DEV_ERROR(drm_dev->dev,
132 			      "failed to initialize encoder: %d\n", ret);
133 		return ERR_PTR(ret);
134 	}
135 
136 	drm_encoder_helper_add(encoder, &rockchip_rgb_encoder_helper_funcs);
137 
138 	if (panel) {
139 		bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_LVDS);
140 		if (IS_ERR(bridge))
141 			return ERR_CAST(bridge);
142 	}
143 
144 	rgb->bridge = bridge;
145 
146 	ret = drm_bridge_attach(encoder, rgb->bridge, NULL);
147 	if (ret) {
148 		DRM_DEV_ERROR(drm_dev->dev,
149 			      "failed to attach bridge: %d\n", ret);
150 		goto err_free_encoder;
151 	}
152 
153 	return rgb;
154 
155 err_free_encoder:
156 	drm_encoder_cleanup(encoder);
157 	return ERR_PTR(ret);
158 }
159 EXPORT_SYMBOL_GPL(rockchip_rgb_init);
160 
161 void rockchip_rgb_fini(struct rockchip_rgb *rgb)
162 {
163 	drm_panel_bridge_remove(rgb->bridge);
164 	drm_encoder_cleanup(&rgb->encoder);
165 }
166 EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
167