Loading...
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
22struct 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
30static int
31rockchip_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
64static const
65struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
66 .atomic_check = rockchip_rgb_encoder_atomic_check,
67};
68
69static const struct drm_encoder_funcs rockchip_rgb_encoder_funcs = {
70 .destroy = drm_encoder_cleanup,
71};
72
73struct 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
154err_free_encoder:
155 drm_encoder_cleanup(encoder);
156 return ERR_PTR(ret);
157}
158EXPORT_SYMBOL_GPL(rockchip_rgb_init);
159
160void rockchip_rgb_fini(struct rockchip_rgb *rgb)
161{
162 drm_panel_bridge_remove(rgb->bridge);
163 drm_encoder_cleanup(&rgb->encoder);
164}
165EXPORT_SYMBOL_GPL(rockchip_rgb_fini);
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
27struct 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
36static int
37rockchip_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
70static const
71struct drm_encoder_helper_funcs rockchip_rgb_encoder_helper_funcs = {
72 .atomic_check = rockchip_rgb_encoder_atomic_check,
73};
74
75struct 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
173err_free_connector:
174 drm_connector_cleanup(connector);
175err_free_encoder:
176 drm_encoder_cleanup(encoder);
177 return ERR_PTR(ret);
178}
179EXPORT_SYMBOL_GPL(rockchip_rgb_init);
180
181void 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}
187EXPORT_SYMBOL_GPL(rockchip_rgb_fini);