xref: /openbmc/linux/drivers/gpu/drm/fsl-dcu/fsl_dcu_drm_rgb.c (revision 4ed91d48259d9ddd378424d008f2e6559f7e78f8)
1 /*
2  * Copyright 2015 Freescale Semiconductor, Inc.
3  *
4  * Freescale DCU drm device driver
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/backlight.h>
13 #include <linux/of_graph.h>
14 
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_panel.h>
19 
20 #include "fsl_dcu_drm_drv.h"
21 #include "fsl_tcon.h"
22 
23 static void fsl_dcu_drm_encoder_destroy(struct drm_encoder *encoder)
24 {
25 	drm_encoder_cleanup(encoder);
26 }
27 
28 static const struct drm_encoder_funcs encoder_funcs = {
29 	.destroy = fsl_dcu_drm_encoder_destroy,
30 };
31 
32 int fsl_dcu_drm_encoder_create(struct fsl_dcu_drm_device *fsl_dev,
33 			       struct drm_crtc *crtc)
34 {
35 	struct drm_encoder *encoder = &fsl_dev->encoder;
36 	int ret;
37 
38 	encoder->possible_crtcs = 1;
39 
40 	/* Use bypass mode for parallel RGB/LVDS encoder */
41 	if (fsl_dev->tcon)
42 		fsl_tcon_bypass_enable(fsl_dev->tcon);
43 
44 	ret = drm_encoder_init(fsl_dev->drm, encoder, &encoder_funcs,
45 			       DRM_MODE_ENCODER_LVDS, NULL);
46 	if (ret < 0)
47 		return ret;
48 
49 	return 0;
50 }
51 
52 static void fsl_dcu_drm_connector_destroy(struct drm_connector *connector)
53 {
54 	struct fsl_dcu_drm_connector *fsl_con = to_fsl_dcu_connector(connector);
55 
56 	drm_connector_unregister(connector);
57 	drm_panel_detach(fsl_con->panel);
58 	drm_connector_cleanup(connector);
59 }
60 
61 static const struct drm_connector_funcs fsl_dcu_drm_connector_funcs = {
62 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
63 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
64 	.destroy = fsl_dcu_drm_connector_destroy,
65 	.dpms = drm_atomic_helper_connector_dpms,
66 	.fill_modes = drm_helper_probe_single_connector_modes,
67 	.reset = drm_atomic_helper_connector_reset,
68 };
69 
70 static int fsl_dcu_drm_connector_get_modes(struct drm_connector *connector)
71 {
72 	struct fsl_dcu_drm_connector *fsl_connector;
73 	int (*get_modes)(struct drm_panel *panel);
74 	int num_modes = 0;
75 
76 	fsl_connector = to_fsl_dcu_connector(connector);
77 	if (fsl_connector->panel && fsl_connector->panel->funcs &&
78 	    fsl_connector->panel->funcs->get_modes) {
79 		get_modes = fsl_connector->panel->funcs->get_modes;
80 		num_modes = get_modes(fsl_connector->panel);
81 	}
82 
83 	return num_modes;
84 }
85 
86 static int fsl_dcu_drm_connector_mode_valid(struct drm_connector *connector,
87 					    struct drm_display_mode *mode)
88 {
89 	if (mode->hdisplay & 0xf)
90 		return MODE_ERROR;
91 
92 	return MODE_OK;
93 }
94 
95 static const struct drm_connector_helper_funcs connector_helper_funcs = {
96 	.get_modes = fsl_dcu_drm_connector_get_modes,
97 	.mode_valid = fsl_dcu_drm_connector_mode_valid,
98 };
99 
100 static int fsl_dcu_attach_panel(struct fsl_dcu_drm_device *fsl_dev,
101 				 struct drm_panel *panel)
102 {
103 	struct drm_encoder *encoder = &fsl_dev->encoder;
104 	struct drm_connector *connector = &fsl_dev->connector.base;
105 	struct drm_mode_config *mode_config = &fsl_dev->drm->mode_config;
106 	int ret;
107 
108 	fsl_dev->connector.encoder = encoder;
109 
110 	ret = drm_connector_init(fsl_dev->drm, connector,
111 				 &fsl_dcu_drm_connector_funcs,
112 				 DRM_MODE_CONNECTOR_LVDS);
113 	if (ret < 0)
114 		return ret;
115 
116 	drm_connector_helper_add(connector, &connector_helper_funcs);
117 	ret = drm_connector_register(connector);
118 	if (ret < 0)
119 		goto err_cleanup;
120 
121 	ret = drm_mode_connector_attach_encoder(connector, encoder);
122 	if (ret < 0)
123 		goto err_sysfs;
124 
125 	drm_object_property_set_value(&connector->base,
126 				      mode_config->dpms_property,
127 				      DRM_MODE_DPMS_OFF);
128 
129 	ret = drm_panel_attach(panel, connector);
130 	if (ret) {
131 		dev_err(fsl_dev->dev, "failed to attach panel\n");
132 		goto err_sysfs;
133 	}
134 
135 	return 0;
136 
137 err_sysfs:
138 	drm_connector_unregister(connector);
139 err_cleanup:
140 	drm_connector_cleanup(connector);
141 	return ret;
142 }
143 
144 static int fsl_dcu_attach_endpoint(struct fsl_dcu_drm_device *fsl_dev,
145 				   const struct of_endpoint *ep)
146 {
147 	struct drm_bridge *bridge;
148 	struct device_node *np;
149 
150 	np = of_graph_get_remote_port_parent(ep->local_node);
151 
152 	fsl_dev->connector.panel = of_drm_find_panel(np);
153 	if (fsl_dev->connector.panel) {
154 		of_node_put(np);
155 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
156 	}
157 
158 	bridge = of_drm_find_bridge(np);
159 	of_node_put(np);
160 	if (!bridge)
161 		return -ENODEV;
162 
163 	return drm_bridge_attach(&fsl_dev->encoder, bridge, NULL);
164 }
165 
166 int fsl_dcu_create_outputs(struct fsl_dcu_drm_device *fsl_dev)
167 {
168 	struct of_endpoint ep;
169 	struct device_node *ep_node, *panel_node;
170 	int ret;
171 
172 	/* This is for backward compatibility */
173 	panel_node = of_parse_phandle(fsl_dev->np, "fsl,panel", 0);
174 	if (panel_node) {
175 		fsl_dev->connector.panel = of_drm_find_panel(panel_node);
176 		of_node_put(panel_node);
177 		if (!fsl_dev->connector.panel)
178 			return -EPROBE_DEFER;
179 		return fsl_dcu_attach_panel(fsl_dev, fsl_dev->connector.panel);
180 	}
181 
182 	ep_node = of_graph_get_next_endpoint(fsl_dev->np, NULL);
183 	if (!ep_node)
184 		return -ENODEV;
185 
186 	ret = of_graph_parse_endpoint(ep_node, &ep);
187 	of_node_put(ep_node);
188 	if (ret)
189 		return -ENODEV;
190 
191 	return fsl_dcu_attach_endpoint(fsl_dev, &ep);
192 }
193