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