blob: 4b2cfbd0d43f150527626acfa6c5c36b0f5eee83 [file] [log] [blame]
Boris Brezillon1a396782015-01-06 11:13:28 +01001/*
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>
Rob Herringebc94462017-03-29 13:55:46 -050025#include <drm/drm_of.h>
Boris Brezillon1a396782015-01-06 11:13:28 +010026
27#include "atmel_hlcdc_dc.h"
28
29/**
Boris Brezillon1a396782015-01-06 11:13:28 +010030 * 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
Boris Brezillon17a8e032016-01-06 10:16:32 +010037 * @panel: panel connected on the RGB output
Boris Brezillon1a396782015-01-06 11:13:28 +010038 */
39struct atmel_hlcdc_rgb_output {
40 struct drm_connector connector;
41 struct drm_encoder encoder;
42 struct atmel_hlcdc_dc *dc;
Boris Brezillon17a8e032016-01-06 10:16:32 +010043 struct drm_panel *panel;
Boris Brezillon1a396782015-01-06 11:13:28 +010044};
45
46static inline struct atmel_hlcdc_rgb_output *
47drm_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
53static inline struct atmel_hlcdc_rgb_output *
54drm_encoder_to_atmel_hlcdc_rgb_output(struct drm_encoder *encoder)
55{
56 return container_of(encoder, struct atmel_hlcdc_rgb_output, encoder);
57}
58
Boris Brezillon17a8e032016-01-06 10:16:32 +010059static void atmel_hlcdc_rgb_encoder_enable(struct drm_encoder *encoder)
Boris Brezillon1a396782015-01-06 11:13:28 +010060{
61 struct atmel_hlcdc_rgb_output *rgb =
62 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
Boris Brezillon17a8e032016-01-06 10:16:32 +010063
64 if (rgb->panel) {
65 drm_panel_prepare(rgb->panel);
66 drm_panel_enable(rgb->panel);
67 }
Boris Brezillon2389fc12015-02-05 16:32:33 +010068}
Boris Brezillon1a396782015-01-06 11:13:28 +010069
Boris Brezillon17a8e032016-01-06 10:16:32 +010070static void atmel_hlcdc_rgb_encoder_disable(struct drm_encoder *encoder)
Boris Brezillon2389fc12015-02-05 16:32:33 +010071{
72 struct atmel_hlcdc_rgb_output *rgb =
73 drm_encoder_to_atmel_hlcdc_rgb_output(encoder);
Boris Brezillon1a396782015-01-06 11:13:28 +010074
Boris Brezillon17a8e032016-01-06 10:16:32 +010075 if (rgb->panel) {
76 drm_panel_disable(rgb->panel);
77 drm_panel_unprepare(rgb->panel);
78 }
Boris Brezillon1a396782015-01-06 11:13:28 +010079}
80
Ville Syrjäläe9becd42015-12-15 12:21:00 +010081static const struct drm_encoder_helper_funcs atmel_hlcdc_panel_encoder_helper_funcs = {
Boris Brezillon17a8e032016-01-06 10:16:32 +010082 .disable = atmel_hlcdc_rgb_encoder_disable,
83 .enable = atmel_hlcdc_rgb_encoder_enable,
Boris Brezillon1a396782015-01-06 11:13:28 +010084};
85
Boris Brezillon1a396782015-01-06 11:13:28 +010086static const struct drm_encoder_funcs atmel_hlcdc_panel_encoder_funcs = {
Eric Anholt510fc3c2017-05-11 11:31:27 -070087 .destroy = drm_encoder_cleanup,
Boris Brezillon1a396782015-01-06 11:13:28 +010088};
89
90static int atmel_hlcdc_panel_get_modes(struct drm_connector *connector)
91{
92 struct atmel_hlcdc_rgb_output *rgb =
93 drm_connector_to_atmel_hlcdc_rgb_output(connector);
Boris Brezillon1a396782015-01-06 11:13:28 +010094
Boris Brezillon17a8e032016-01-06 10:16:32 +010095 if (rgb->panel)
96 return rgb->panel->funcs->get_modes(rgb->panel);
97
98 return 0;
Boris Brezillon1a396782015-01-06 11:13:28 +010099}
100
101static int atmel_hlcdc_rgb_mode_valid(struct drm_connector *connector,
102 struct drm_display_mode *mode)
103{
104 struct atmel_hlcdc_rgb_output *rgb =
105 drm_connector_to_atmel_hlcdc_rgb_output(connector);
106
107 return atmel_hlcdc_dc_mode_valid(rgb->dc, mode);
108}
109
Ville Syrjäläe9becd42015-12-15 12:21:00 +0100110static const struct drm_connector_helper_funcs atmel_hlcdc_panel_connector_helper_funcs = {
Boris Brezillon1a396782015-01-06 11:13:28 +0100111 .get_modes = atmel_hlcdc_panel_get_modes,
112 .mode_valid = atmel_hlcdc_rgb_mode_valid,
Boris Brezillon1a396782015-01-06 11:13:28 +0100113};
114
115static enum drm_connector_status
116atmel_hlcdc_panel_connector_detect(struct drm_connector *connector, bool force)
117{
Boris Brezillon17a8e032016-01-06 10:16:32 +0100118 struct atmel_hlcdc_rgb_output *rgb =
119 drm_connector_to_atmel_hlcdc_rgb_output(connector);
120
121 if (rgb->panel)
122 return connector_status_connected;
123
124 return connector_status_disconnected;
Boris Brezillon1a396782015-01-06 11:13:28 +0100125}
126
127static void
128atmel_hlcdc_panel_connector_destroy(struct drm_connector *connector)
129{
130 struct atmel_hlcdc_rgb_output *rgb =
131 drm_connector_to_atmel_hlcdc_rgb_output(connector);
Boris Brezillon1a396782015-01-06 11:13:28 +0100132
Boris Brezillon17a8e032016-01-06 10:16:32 +0100133 if (rgb->panel)
134 drm_panel_detach(rgb->panel);
135
Boris Brezillon1a396782015-01-06 11:13:28 +0100136 drm_connector_cleanup(connector);
137}
138
139static const struct drm_connector_funcs atmel_hlcdc_panel_connector_funcs = {
Boris Brezillon2389fc12015-02-05 16:32:33 +0100140 .dpms = drm_atomic_helper_connector_dpms,
Boris Brezillon1a396782015-01-06 11:13:28 +0100141 .detect = atmel_hlcdc_panel_connector_detect,
142 .fill_modes = drm_helper_probe_single_connector_modes,
143 .destroy = atmel_hlcdc_panel_connector_destroy,
Boris Brezillon2389fc12015-02-05 16:32:33 +0100144 .reset = drm_atomic_helper_connector_reset,
145 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
146 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Boris Brezillon1a396782015-01-06 11:13:28 +0100147};
148
Boris Brezillon17a8e032016-01-06 10:16:32 +0100149static int atmel_hlcdc_attach_endpoint(struct drm_device *dev,
Rob Herringebc94462017-03-29 13:55:46 -0500150 const struct device_node *np)
Boris Brezillon17a8e032016-01-06 10:16:32 +0100151{
152 struct atmel_hlcdc_dc *dc = dev->dev_private;
153 struct atmel_hlcdc_rgb_output *output;
Boris Brezillon17a8e032016-01-06 10:16:32 +0100154 struct drm_panel *panel;
155 struct drm_bridge *bridge;
156 int ret;
157
158 output = devm_kzalloc(dev->dev, sizeof(*output), GFP_KERNEL);
159 if (!output)
Boris Brezillon1a396782015-01-06 11:13:28 +0100160 return -EINVAL;
161
Boris Brezillon17a8e032016-01-06 10:16:32 +0100162 output->dc = dc;
Boris Brezillon1a396782015-01-06 11:13:28 +0100163
Boris Brezillon17a8e032016-01-06 10:16:32 +0100164 drm_encoder_helper_add(&output->encoder,
Boris Brezillon1a396782015-01-06 11:13:28 +0100165 &atmel_hlcdc_panel_encoder_helper_funcs);
Boris Brezillon17a8e032016-01-06 10:16:32 +0100166 ret = drm_encoder_init(dev, &output->encoder,
Boris Brezillon1a396782015-01-06 11:13:28 +0100167 &atmel_hlcdc_panel_encoder_funcs,
Boris Brezillonabfc9362015-12-31 18:24:09 +0100168 DRM_MODE_ENCODER_NONE, NULL);
Boris Brezillon1a396782015-01-06 11:13:28 +0100169 if (ret)
170 return ret;
171
Boris Brezillon17a8e032016-01-06 10:16:32 +0100172 output->encoder.possible_crtcs = 0x1;
Boris Brezillon1a396782015-01-06 11:13:28 +0100173
Rob Herringebc94462017-03-29 13:55:46 -0500174 ret = drm_of_find_panel_or_bridge(np, 0, 0, &panel, &bridge);
175 if (ret)
176 return ret;
Boris Brezillon1a396782015-01-06 11:13:28 +0100177
Boris Brezillon17a8e032016-01-06 10:16:32 +0100178 if (panel) {
Boris Brezillon17a8e032016-01-06 10:16:32 +0100179 output->connector.dpms = DRM_MODE_DPMS_OFF;
180 output->connector.polled = DRM_CONNECTOR_POLL_CONNECT;
181 drm_connector_helper_add(&output->connector,
182 &atmel_hlcdc_panel_connector_helper_funcs);
183 ret = drm_connector_init(dev, &output->connector,
184 &atmel_hlcdc_panel_connector_funcs,
185 DRM_MODE_CONNECTOR_Unknown);
186 if (ret)
187 goto err_encoder_cleanup;
188
189 drm_mode_connector_attach_encoder(&output->connector,
190 &output->encoder);
191
192 ret = drm_panel_attach(panel, &output->connector);
193 if (ret) {
194 drm_connector_cleanup(&output->connector);
195 goto err_encoder_cleanup;
196 }
197
198 output->panel = panel;
199
200 return 0;
201 }
202
Boris Brezillon17a8e032016-01-06 10:16:32 +0100203 if (bridge) {
Laurent Pinchart3bb80f22016-11-28 17:59:08 +0200204 ret = drm_bridge_attach(&output->encoder, bridge, NULL);
Boris Brezillon17a8e032016-01-06 10:16:32 +0100205 if (!ret)
206 return 0;
207 }
Boris Brezillon1a396782015-01-06 11:13:28 +0100208
209err_encoder_cleanup:
Boris Brezillon17a8e032016-01-06 10:16:32 +0100210 drm_encoder_cleanup(&output->encoder);
Boris Brezillon1a396782015-01-06 11:13:28 +0100211
212 return ret;
213}
214
215int atmel_hlcdc_create_outputs(struct drm_device *dev)
216{
Rob Herringebc94462017-03-29 13:55:46 -0500217 struct device_node *remote;
Dan Carpenter418d59e2017-04-15 22:21:42 +0300218 int ret = -ENODEV;
219 int endpoint = 0;
Boris Brezillon1a396782015-01-06 11:13:28 +0100220
Rob Herringebc94462017-03-29 13:55:46 -0500221 while (true) {
222 /* Loop thru possible multiple connections to the output */
223 remote = of_graph_get_remote_node(dev->dev->of_node, 0,
224 endpoint++);
225 if (!remote)
226 break;
Boris Brezillon1a396782015-01-06 11:13:28 +0100227
Rob Herringebc94462017-03-29 13:55:46 -0500228 ret = atmel_hlcdc_attach_endpoint(dev, remote);
229 of_node_put(remote);
230 if (ret)
Boris Brezillon17a8e032016-01-06 10:16:32 +0100231 return ret;
232 }
Boris Brezillon1a396782015-01-06 11:13:28 +0100233
Rob Herringebc94462017-03-29 13:55:46 -0500234 return ret;
Boris Brezillon1a396782015-01-06 11:13:28 +0100235}