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
29 /**
30 * Atmel HLCDC RGB output mode
31 */
32 enum atmel_hlcdc_connector_rgb_mode {
33 ATMEL_HLCDC_CONNECTOR_RGB444,
34 ATMEL_HLCDC_CONNECTOR_RGB565,
35 ATMEL_HLCDC_CONNECTOR_RGB666,
36 ATMEL_HLCDC_CONNECTOR_RGB888,
37 };
38
39 /**
40 * Atmel HLCDC RGB connector structure
41 *
42 * This structure stores RGB slave device information.
43 *
44 * @connector: DRM connector
45 * @encoder: DRM encoder
46 * @dc: pointer to the atmel_hlcdc_dc structure
47 * @dpms: current DPMS mode
48 */
49 struct atmel_hlcdc_rgb_output {
50 struct drm_connector connector;
51 struct drm_encoder encoder;
52 struct atmel_hlcdc_dc *dc;
53 int dpms;
54 };
55
56 static inline struct atmel_hlcdc_rgb_output *
drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector * connector)57 drm_connector_to_atmel_hlcdc_rgb_output(struct drm_connector *connector)
58 {
59 return container_of(connector, struct atmel_hlcdc_rgb_output,
60 connector);
61 }
62
63 static inline struct atmel_hlcdc_rgb_output *
drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder * encoder)64 drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
65 {
66 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
67 }
68
69 /**
70 * Atmel HLCDC Panel device structure
71 *
72 * This structure is specialization of the slave device structure to
73 * interface with drm panels.
74 *
75 * @base: base slave device fields
76 * @panel: drm panel attached to this slave device
77 */
78 struct atmel_hlcdc_panel {
79 struct atmel_hlcdc_rgb_output base;
80 struct drm_panel *panel;
81 };
82
83 static inline struct atmel_hlcdc_panel *
atmel_hlcdc_rgb_output_to_panel(struct atmel_hlcdc_rgb_output * output)84 atmel_hlcdc_rgb_output_to_panel(struct atmel_hlcdc_rgb_output *output)
85 {
86 return container_of(output, struct atmel_hlcdc_panel, base);
87 }
88
atmel_hlcdc_panel_encoder_enable(struct drm_encoder * encoder)89 static void atmel_hlcdc_panel_encoder_enable(struct drm_encoder *encoder)
90 {
91 struct atmel_hlcdc_rgb_output *rgb =
92 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
93 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
94
95 drm_panel_enable(panel->panel);
96 }
97
atmel_hlcdc_panel_encoder_disable(struct drm_encoder * encoder)98 static void atmel_hlcdc_panel_encoder_disable(struct drm_encoder *encoder)
99 {
100 struct atmel_hlcdc_rgb_output *rgb =
101 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
102 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
103
104 drm_panel_disable(panel->panel);
105 }
106
107 static bool
atmel_hlcdc_panel_encoder_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted)108 atmel_hlcdc_panel_encoder_mode_fixup(struct drm_encoder *encoder,
109 const struct drm_display_mode *mode,
110 struct drm_display_mode *adjusted)
111 {
112 return true;
113 }
114
115 static void
atmel_hlcdc_rgb_encoder_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted)116 atmel_hlcdc_rgb_encoder_mode_set(struct drm_encoder *encoder,
117 struct drm_display_mode *mode,
118 struct drm_display_mode *adjusted)
119 {
120 struct atmel_hlcdc_rgb_output *rgb =
121 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
122 struct drm_display_info *info = &rgb->connector.display_info;
123 unsigned int cfg;
124
125 cfg = 0;
126
127 if (info->num_bus_formats) {
128 switch (info->bus_formats[0]) {
129 case MEDIA_BUS_FMT_RGB666_1X18:
130 cfg |= ATMEL_HLCDC_CONNECTOR_RGB666 << 8;
131 break;
132 case MEDIA_BUS_FMT_RGB888_1X24:
133 cfg |= ATMEL_HLCDC_CONNECTOR_RGB888 << 8;
134 break;
135 default:
136 break;
137 }
138 }
139
140 regmap_update_bits(rgb->dc->hlcdc->regmap, ATMEL_HLCDC_CFG(5),
141 ATMEL_HLCDC_MODE_MASK,
142 cfg);
143 }
144
145 static struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
146 .mode_fixup = atmel_hlcdc_panel_encoder_mode_fixup,
147 .mode_set = atmel_hlcdc_rgb_encoder_mode_set,
148 .disable = atmel_hlcdc_panel_encoder_disable,
149 .enable = atmel_hlcdc_panel_encoder_enable,
150 };
151
atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder * encoder)152 static void atmel_hlcdc_rgb_encoder_destroy(struct drm_encoder *encoder)
153 {
154 drm_encoder_cleanup(encoder);
155 memset(encoder, 0, sizeof(*encoder));
156 }
157
158 static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
159 .destroy = atmel_hlcdc_rgb_encoder_destroy,
160 };
161
atmel_hlcdc_panel_get_modes(struct drm_connector * connector)162 static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
163 {
164 struct atmel_hlcdc_rgb_output *rgb =
165 drm_connector_to_atmel_hlcdc_rgb_output(connector);
166 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
167
168 return panel->panel->funcs->get_modes(panel->panel);
169 }
170
atmel_hlcdc_rgb_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)171 static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
172 struct drm_display_mode *mode)
173 {
174 struct atmel_hlcdc_rgb_output *rgb =
175 drm_connector_to_atmel_hlcdc_rgb_output(connector);
176
177 return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
178 }
179
180
181
182 static struct drm_encoder *
atmel_hlcdc_rgb_best_encoder(struct drm_connector * connector)183 atmel_hlcdc_rgb_best_encoder(struct drm_connector *connector)
184 {
185 struct atmel_hlcdc_rgb_output *rgb =
186 drm_connector_to_atmel_hlcdc_rgb_output(connector);
187
188 return &rgb->encoder;
189 }
190
191 static struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
192 .get_modes = atmel_hlcdc_panel_get_modes,
193 .mode_valid = atmel_hlcdc_rgb_mode_valid,
194 .best_encoder = atmel_hlcdc_rgb_best_encoder,
195 };
196
197 static enum drm_connector_status
atmel_hlcdc_panel_connector_detect(struct drm_connector * connector,bool force)198 atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
199 {
200 return connector_status_connected;
201 }
202
203 static void
atmel_hlcdc_panel_connector_destroy(struct drm_connector * connector)204 atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
205 {
206 struct atmel_hlcdc_rgb_output *rgb =
207 drm_connector_to_atmel_hlcdc_rgb_output(connector);
208 struct atmel_hlcdc_panel *panel = atmel_hlcdc_rgb_output_to_panel(rgb);
209
210 drm_panel_detach(panel->panel);
211 drm_connector_cleanup(connector);
212 }
213
214 static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
215 .dpms = drm_atomic_helper_connector_dpms,
216 .detect = atmel_hlcdc_panel_connector_detect,
217 .fill_modes = drm_helper_probe_single_connector_modes,
218 .destroy = atmel_hlcdc_panel_connector_destroy,
219 .reset = drm_atomic_helper_connector_reset,
220 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
221 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
222 };
223
atmel_hlcdc_create_panel_output(struct drm_device * dev,struct of_endpoint * ep)224 static int atmel_hlcdc_create_panel_output(struct drm_device *dev,
225 struct of_endpoint *ep)
226 {
227 struct atmel_hlcdc_dc *dc = dev->dev_private;
228 struct device_node *np;
229 struct drm_panel *p = NULL;
230 struct atmel_hlcdc_panel *panel;
231 int ret;
232
233 np = of_graph_get_remote_port_parent(ep->local_node);
234 if (!np)
235 return -EINVAL;
236
237 p = of_drm_find_panel(np);
238 of_node_put(np);
239
240 if (!p)
241 return -EPROBE_DEFER;
242
243 panel = devm_kzalloc(dev->dev, sizeof(*panel), GFP_KERNEL);
244 if (!panel)
245 return -EINVAL;
246
247 panel->base.dpms = DRM_MODE_DPMS_OFF;
248
249 panel->base.dc = dc;
250
251 drm_encoder_helper_add(&panel->base.encoder,
252 &atmel_hlcdc_panel_encoder_helper_funcs);
253 ret = drm_encoder_init(dev, &panel->base.encoder,
254 &atmel_hlcdc_panel_encoder_funcs,
255 DRM_MODE_ENCODER_LVDS);
256 if (ret)
257 return ret;
258
259 panel->base.connector.dpms = DRM_MODE_DPMS_OFF;
260 panel->base.connector.polled = DRM_CONNECTOR_POLL_CONNECT;
261 drm_connector_helper_add(&panel->base.connector,
262 &atmel_hlcdc_panel_connector_helper_funcs);
263 ret = drm_connector_init(dev, &panel->base.connector,
264 &atmel_hlcdc_panel_connector_funcs,
265 DRM_MODE_CONNECTOR_LVDS);
266 if (ret)
267 goto err_encoder_cleanup;
268
269 drm_mode_connector_attach_encoder(&panel->base.connector,
270 &panel->base.encoder);
271 panel->base.encoder.possible_crtcs = 0x1;
272
273 drm_panel_attach(p, &panel->base.connector);
274 panel->panel = p;
275
276 return 0;
277
278 err_encoder_cleanup:
279 drm_encoder_cleanup(&panel->base.encoder);
280
281 return ret;
282 }
283
atmel_hlcdc_create_outputs(struct drm_device * dev)284 int atmel_hlcdc_create_outputs(struct drm_device *dev)
285 {
286 struct device_node *port_np, *np;
287 struct of_endpoint ep;
288 int ret;
289
290 port_np = of_get_child_by_name(dev->dev->of_node, "port");
291 if (!port_np)
292 return -EINVAL;
293
294 np = of_get_child_by_name(port_np, "endpoint");
295 of_node_put(port_np);
296
297 if (!np)
298 return -EINVAL;
299
300 ret = of_graph_parse_endpoint(np, &ep);
301 of_node_put(port_np);
302
303 if (ret)
304 return ret;
305
306 /* We currently only support panel output */
307 return atmel_hlcdc_create_panel_output(dev, &ep);
308 }
309