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