• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "rockchip_rgb.h"
22 
23 #define encoder_to_rgb(c) container_of(c, struct rockchip_rgb, encoder)
24 
25 struct rockchip_rgb {
26 	struct device *dev;
27 	struct drm_device *drm_dev;
28 	struct drm_bridge *bridge;
29 	struct drm_encoder encoder;
30 	int output_mode;
31 };
32 
33 static int
rockchip_rgb_encoder_atomic_check(struct drm_encoder * encoder,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)34 rockchip_rgb_encoder_atomic_check(struct drm_encoder *encoder,
35 				   struct drm_crtc_state *crtc_state,
36 				   struct drm_connector_state *conn_state)
37 {
38 	struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
39 	struct drm_connector *connector = conn_state->connector;
40 	struct drm_display_info *info = &connector->display_info;
41 	u32 bus_format;
42 
43 	if (info->num_bus_formats)
44 		bus_format = info->bus_formats[0];
45 	else
46 		bus_format = MEDIA_BUS_FMT_RGB888_1X24;
47 
48 	switch (bus_format) {
49 	case MEDIA_BUS_FMT_RGB666_1X18:
50 		s->output_mode = ROCKCHIP_OUT_MODE_P666;
51 		break;
52 	case MEDIA_BUS_FMT_RGB565_1X16:
53 		s->output_mode = ROCKCHIP_OUT_MODE_P565;
54 		break;
55 	case MEDIA_BUS_FMT_RGB888_1X24:
56 	case MEDIA_BUS_FMT_RGB666_1X24_CPADHI:
57 	default:
58 		s->output_mode = ROCKCHIP_OUT_MODE_P888;
59 		break;
60 	}
61 
62 	s->output_type = DRM_MODE_CONNECTOR_LVDS;
63 
64 	return 0;
65 }
66 
67 static const
68 struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
69 	.atomic_check = rockchip_rgb_encoder_atomic_check,
70 };
71 
rockchip_rgb_init(struct device * dev,struct drm_crtc * crtc,struct drm_device * drm_dev)72 struct rockchip_rgb *rockchip_rgb_init(struct device *dev,
73 				       struct drm_crtc *crtc,
74 				       struct drm_device *drm_dev)
75 {
76 	struct rockchip_rgb *rgb;
77 	struct drm_encoder *encoder;
78 	struct device_node *port, *endpoint;
79 	u32 endpoint_id;
80 	int ret = 0, child_count = 0;
81 	struct drm_panel *panel;
82 	struct drm_bridge *bridge;
83 
84 	rgb = devm_kzalloc(dev, sizeof(*rgb), GFP_KERNEL);
85 	if (!rgb)
86 		return ERR_PTR(-ENOMEM);
87 
88 	rgb->dev = dev;
89 	rgb->drm_dev = drm_dev;
90 
91 	port = of_graph_get_port_by_id(dev->of_node, 0);
92 	if (!port)
93 		return ERR_PTR(-EINVAL);
94 
95 	for_each_child_of_node(port, endpoint) {
96 		if (of_property_read_u32(endpoint, "reg", &endpoint_id))
97 			endpoint_id = 0;
98 
99 		/* if subdriver (> 0) or error case (< 0), ignore entry */
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_simple_encoder_init(drm_dev, encoder, DRM_MODE_ENCODER_NONE);
128 	if (ret < 0) {
129 		DRM_DEV_ERROR(drm_dev->dev,
130 			      "failed to initialize encoder: %d\n", ret);
131 		return ERR_PTR(ret);
132 	}
133 
134 	drm_encoder_helper_add(encoder, &rockchip_rgb_encoder_helper_funcs);
135 
136 	if (panel) {
137 		bridge = drm_panel_bridge_add_typed(panel,
138 						    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, 0);
146 	if (ret)
147 		goto err_free_encoder;
148 
149 	return rgb;
150 
151 err_free_encoder:
152 	drm_encoder_cleanup(encoder);
153 	return ERR_PTR(ret);
154 }
155 EXPORT_SYMBOL_GPL(rockchip_rgb_init);
156 
rockchip_rgb_fini(struct rockchip_rgb * rgb)157 void rockchip_rgb_fini(struct rockchip_rgb *rgb)
158 {
159 	drm_panel_bridge_remove(rgb->bridge);
160 	drm_encoder_cleanup(&rgb->encoder);
161 }
162 EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
163