Commit | Line | Data |
---|---|---|
1a396789 BB |
1 | /* |
2 | * Copyright (C) 2014 Traphandler | |
3 | * Copyright (C) 2014 Free Electrons | |
4 | * Copyright (C) 2014 Atmel | |
5 | * | |
6 | * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com> | |
7 | * Author: Boris BREZILLON <boris.brezillon@free-electrons.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License version 2 as published by | |
11 | * the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
16 | * more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along with | |
19 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | #include <linux/of_graph.h> | |
23 | ||
24 | #include <drm/drmP.h> | |
25 | #include <drm/drm_panel.h> | |
26 | ||
27 | #include "atmel_hlcdc_dc.h" | |
28 | ||
1a396789 BB |
29 | /** |
30 | * Atmel HLCDC RGB connector structure | |
31 | * | |
32 | * This structure stores RGB slave device information. | |
33 | * | |
34 | * @connector: DRM connector | |
35 | * @encoder: DRM encoder | |
36 | * @dc: pointer to the atmel_hlcdc_dc structure | |
17a8e03e | 37 | * @panel: panel connected on the RGB output |
1a396789 BB |
38 | */ |
39 | struct atmel_hlcdc_rgb_output { | |
40 | struct drm_connector connector; | |
41 | struct drm_encoder encoder; | |
42 | struct atmel_hlcdc_dc *dc; | |
17a8e03e | 43 | struct drm_panel *panel; |
1a396789 BB |
44 | }; |
45 | ||
46 | static inline struct atmel_hlcdc_rgb_output * | |
47 | drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector) | |
48 | { | |
49 | return container_of(connector, struct atmel_hlcdc_rgb_output, | |
50 | connector); | |
51 | } | |
52 | ||
53 | static inline struct atmel_hlcdc_rgb_output * | |
54 | drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder) | |
55 | { | |
56 | return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder); | |
57 | } | |
58 | ||
17a8e03e | 59 | static void atmel_hlcdc_rgb_encoder_enable(struct drm_encoder *encoder) |
1a396789 BB |
60 | { |
61 | struct atmel_hlcdc_rgb_output *rgb = | |
62 | drm_encoder_to_atmel_hlcdc_rgb_output(encoder); | |
17a8e03e BB |
63 | |
64 | if (rgb->panel) { | |
65 | drm_panel_prepare(rgb->panel); | |
66 | drm_panel_enable(rgb->panel); | |
67 | } | |
2389fc13 | 68 | } |
1a396789 | 69 | |
17a8e03e | 70 | static void atmel_hlcdc_rgb_encoder_disable(struct drm_encoder *encoder) |
2389fc13 BB |
71 | { |
72 | struct atmel_hlcdc_rgb_output *rgb = | |
73 | drm_encoder_to_atmel_hlcdc_rgb_output(encoder); | |
1a396789 | 74 | |
17a8e03e BB |
75 | if (rgb->panel) { |
76 | drm_panel_disable(rgb->panel); | |
77 | drm_panel_unprepare(rgb->panel); | |
78 | } | |
1a396789 BB |
79 | } |
80 | ||
e9becd46 | 81 | static const struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = { |
17a8e03e BB |
82 | .disable = atmel_hlcdc_rgb_encoder_disable, |
83 | .enable = atmel_hlcdc_rgb_encoder_enable, | |
1a396789 BB |
84 | }; |
85 | ||
86 | static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder) | |
87 | { | |
88 | drm_encoder_cleanup(encoder); | |
89 | memset(encoder, 0, sizeof(*encoder)); | |
90 | } | |
91 | ||
92 | static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = { | |
93 | .destroy = atmel_hlcdc_rgb_encoder_destroy, | |
94 | }; | |
95 | ||
96 | static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector) | |
97 | { | |
98 | struct atmel_hlcdc_rgb_output *rgb = | |
99 | drm_connector_to_atmel_hlcdc_rgb_output(connector); | |
1a396789 | 100 | |
17a8e03e BB |
101 | if (rgb->panel) |
102 | return rgb->panel->funcs->get_modes(rgb->panel); | |
103 | ||
104 | return 0; | |
1a396789 BB |
105 | } |
106 | ||
107 | static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector, | |
108 | struct drm_display_mode *mode) | |
109 | { | |
110 | struct atmel_hlcdc_rgb_output *rgb = | |
111 | drm_connector_to_atmel_hlcdc_rgb_output(connector); | |
112 | ||
113 | return atmel_hlcdc_dc_mode_valid(rgb->dc, mode); | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | static struct drm_encoder * | |
119 | atmel_hlcdc_rgb_best_encoder(struct drm_connector *connector) | |
120 | { | |
121 | struct atmel_hlcdc_rgb_output *rgb = | |
122 | drm_connector_to_atmel_hlcdc_rgb_output(connector); | |
123 | ||
124 | return &rgb->encoder; | |
125 | } | |
126 | ||
e9becd46 | 127 | static const struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = { |
1a396789 BB |
128 | .get_modes = atmel_hlcdc_panel_get_modes, |
129 | .mode_valid = atmel_hlcdc_rgb_mode_valid, | |
130 | .best_encoder = atmel_hlcdc_rgb_best_encoder, | |
131 | }; | |
132 | ||
133 | static enum drm_connector_status | |
134 | atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force) | |
135 | { | |
17a8e03e BB |
136 | struct atmel_hlcdc_rgb_output *rgb = |
137 | drm_connector_to_atmel_hlcdc_rgb_output(connector); | |
138 | ||
139 | if (rgb->panel) | |
140 | return connector_status_connected; | |
141 | ||
142 | return connector_status_disconnected; | |
1a396789 BB |
143 | } |
144 | ||
145 | static void | |
146 | atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector) | |
147 | { | |
148 | struct atmel_hlcdc_rgb_output *rgb = | |
149 | drm_connector_to_atmel_hlcdc_rgb_output(connector); | |
1a396789 | 150 | |
17a8e03e BB |
151 | if (rgb->panel) |
152 | drm_panel_detach(rgb->panel); | |
153 | ||
1a396789 BB |
154 | drm_connector_cleanup(connector); |
155 | } | |
156 | ||
157 | static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = { | |
2389fc13 | 158 | .dpms = drm_atomic_helper_connector_dpms, |
1a396789 BB |
159 | .detect = atmel_hlcdc_panel_connector_detect, |
160 | .fill_modes = drm_helper_probe_single_connector_modes, | |
161 | .destroy = atmel_hlcdc_panel_connector_destroy, | |
2389fc13 BB |
162 | .reset = drm_atomic_helper_connector_reset, |
163 | .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, | |
164 | .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, | |
1a396789 BB |
165 | }; |
166 | ||
17a8e03e BB |
167 | static int atmel_hlcdc_check_endpoint(struct drm_device *dev, |
168 | const struct of_endpoint *ep) | |
1a396789 | 169 | { |
1a396789 | 170 | struct device_node *np; |
17a8e03e | 171 | void *obj; |
1a396789 BB |
172 | |
173 | np = of_graph_get_remote_port_parent(ep->local_node); | |
1a396789 | 174 | |
17a8e03e BB |
175 | obj = of_drm_find_panel(np); |
176 | if (!obj) | |
177 | obj = of_drm_find_bridge(np); | |
178 | ||
1a396789 BB |
179 | of_node_put(np); |
180 | ||
17a8e03e BB |
181 | return obj ? 0 : -EPROBE_DEFER; |
182 | } | |
1a396789 | 183 | |
17a8e03e BB |
184 | static int atmel_hlcdc_attach_endpoint(struct drm_device *dev, |
185 | const struct of_endpoint *ep) | |
186 | { | |
187 | struct atmel_hlcdc_dc *dc = dev->dev_private; | |
188 | struct atmel_hlcdc_rgb_output *output; | |
189 | struct device_node *np; | |
190 | struct drm_panel *panel; | |
191 | struct drm_bridge *bridge; | |
192 | int ret; | |
1a396789 | 193 | |
17a8e03e BB |
194 | output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL); |
195 | if (!output) | |
196 | return -EINVAL; | |
1a396789 | 197 | |
17a8e03e | 198 | output->dc = dc; |
1a396789 | 199 | |
17a8e03e | 200 | drm_encoder_helper_add(&output->encoder, |
1a396789 | 201 | &atmel_hlcdc_panel_encoder_helper_funcs); |
17a8e03e | 202 | ret = drm_encoder_init(dev, &output->encoder, |
1a396789 | 203 | &atmel_hlcdc_panel_encoder_funcs, |
abfc936e | 204 | DRM_MODE_ENCODER_NONE, NULL); |
1a396789 BB |
205 | if (ret) |
206 | return ret; | |
207 | ||
17a8e03e BB |
208 | output->encoder.possible_crtcs = 0x1; |
209 | ||
210 | np = of_graph_get_remote_port_parent(ep->local_node); | |
1a396789 | 211 | |
17a8e03e | 212 | ret = -EPROBE_DEFER; |
1a396789 | 213 | |
17a8e03e BB |
214 | panel = of_drm_find_panel(np); |
215 | if (panel) { | |
216 | of_node_put(np); | |
217 | output->connector.dpms = DRM_MODE_DPMS_OFF; | |
218 | output->connector.polled = DRM_CONNECTOR_POLL_CONNECT; | |
219 | drm_connector_helper_add(&output->connector, | |
220 | &atmel_hlcdc_panel_connector_helper_funcs); | |
221 | ret = drm_connector_init(dev, &output->connector, | |
222 | &atmel_hlcdc_panel_connector_funcs, | |
223 | DRM_MODE_CONNECTOR_Unknown); | |
224 | if (ret) | |
225 | goto err_encoder_cleanup; | |
1a396789 | 226 | |
17a8e03e BB |
227 | drm_mode_connector_attach_encoder(&output->connector, |
228 | &output->encoder); | |
229 | ||
230 | ret = drm_panel_attach(panel, &output->connector); | |
231 | if (ret) { | |
232 | drm_connector_cleanup(&output->connector); | |
233 | goto err_encoder_cleanup; | |
234 | } | |
235 | ||
236 | output->panel = panel; | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | bridge = of_drm_find_bridge(np); | |
242 | of_node_put(np); | |
243 | ||
244 | if (bridge) { | |
245 | output->encoder.bridge = bridge; | |
246 | bridge->encoder = &output->encoder; | |
247 | ret = drm_bridge_attach(dev, bridge); | |
248 | if (!ret) | |
249 | return 0; | |
250 | } | |
1a396789 BB |
251 | |
252 | err_encoder_cleanup: | |
17a8e03e | 253 | drm_encoder_cleanup(&output->encoder); |
1a396789 BB |
254 | |
255 | return ret; | |
256 | } | |
257 | ||
258 | int atmel_hlcdc_create_outputs(struct drm_device *dev) | |
259 | { | |
17a8e03e | 260 | struct device_node *ep_np = NULL; |
1a396789 BB |
261 | struct of_endpoint ep; |
262 | int ret; | |
263 | ||
17a8e03e BB |
264 | for_each_endpoint_of_node(dev->dev->of_node, ep_np) { |
265 | ret = of_graph_parse_endpoint(ep_np, &ep); | |
266 | if (!ret) | |
267 | ret = atmel_hlcdc_check_endpoint(dev, &ep); | |
1a396789 | 268 | |
0b1e1eb7 BB |
269 | if (ret) { |
270 | of_node_put(ep_np); | |
17a8e03e | 271 | return ret; |
0b1e1eb7 | 272 | } |
17a8e03e | 273 | } |
1a396789 | 274 | |
17a8e03e BB |
275 | for_each_endpoint_of_node(dev->dev->of_node, ep_np) { |
276 | ret = of_graph_parse_endpoint(ep_np, &ep); | |
277 | if (!ret) | |
278 | ret = atmel_hlcdc_attach_endpoint(dev, &ep); | |
1a396789 | 279 | |
0b1e1eb7 BB |
280 | if (ret) { |
281 | of_node_put(ep_np); | |
17a8e03e | 282 | return ret; |
0b1e1eb7 | 283 | } |
17a8e03e | 284 | } |
1a396789 | 285 | |
17a8e03e | 286 | return 0; |
1a396789 | 287 | } |