]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/sun4i/sun4i_rgb.c
drm/sun4i: make drm_connector_funcs structures const
[linux.git] / drivers / gpu / drm / sun4i / sun4i_rgb.c
1 /*
2  * Copyright (C) 2015 Free Electrons
3  * Copyright (C) 2015 NextThing Co
4  *
5  * Maxime Ripard <maxime.ripard@free-electrons.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  */
12
13 #include <linux/clk.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_of.h>
19 #include <drm/drm_panel.h>
20
21 #include "sun4i_crtc.h"
22 #include "sun4i_tcon.h"
23 #include "sun4i_rgb.h"
24
25 struct sun4i_rgb {
26         struct drm_connector    connector;
27         struct drm_encoder      encoder;
28
29         struct sun4i_tcon       *tcon;
30 };
31
32 static inline struct sun4i_rgb *
33 drm_connector_to_sun4i_rgb(struct drm_connector *connector)
34 {
35         return container_of(connector, struct sun4i_rgb,
36                             connector);
37 }
38
39 static inline struct sun4i_rgb *
40 drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
41 {
42         return container_of(encoder, struct sun4i_rgb,
43                             encoder);
44 }
45
46 static int sun4i_rgb_get_modes(struct drm_connector *connector)
47 {
48         struct sun4i_rgb *rgb =
49                 drm_connector_to_sun4i_rgb(connector);
50         struct sun4i_tcon *tcon = rgb->tcon;
51
52         return drm_panel_get_modes(tcon->panel);
53 }
54
55 static int sun4i_rgb_mode_valid(struct drm_connector *connector,
56                                 struct drm_display_mode *mode)
57 {
58         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
59         struct sun4i_tcon *tcon = rgb->tcon;
60         u32 hsync = mode->hsync_end - mode->hsync_start;
61         u32 vsync = mode->vsync_end - mode->vsync_start;
62         unsigned long rate = mode->clock * 1000;
63         long rounded_rate;
64
65         DRM_DEBUG_DRIVER("Validating modes...\n");
66
67         if (hsync < 1)
68                 return MODE_HSYNC_NARROW;
69
70         if (hsync > 0x3ff)
71                 return MODE_HSYNC_WIDE;
72
73         if ((mode->hdisplay < 1) || (mode->htotal < 1))
74                 return MODE_H_ILLEGAL;
75
76         if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
77                 return MODE_BAD_HVALUE;
78
79         DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
80
81         if (vsync < 1)
82                 return MODE_VSYNC_NARROW;
83
84         if (vsync > 0x3ff)
85                 return MODE_VSYNC_WIDE;
86
87         if ((mode->vdisplay < 1) || (mode->vtotal < 1))
88                 return MODE_V_ILLEGAL;
89
90         if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
91                 return MODE_BAD_VVALUE;
92
93         DRM_DEBUG_DRIVER("Vertical parameters OK\n");
94
95         rounded_rate = clk_round_rate(tcon->dclk, rate);
96         if (rounded_rate < rate)
97                 return MODE_CLOCK_LOW;
98
99         if (rounded_rate > rate)
100                 return MODE_CLOCK_HIGH;
101
102         DRM_DEBUG_DRIVER("Clock rate OK\n");
103
104         return MODE_OK;
105 }
106
107 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
108         .get_modes      = sun4i_rgb_get_modes,
109         .mode_valid     = sun4i_rgb_mode_valid,
110 };
111
112 static void
113 sun4i_rgb_connector_destroy(struct drm_connector *connector)
114 {
115         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
116         struct sun4i_tcon *tcon = rgb->tcon;
117
118         drm_panel_detach(tcon->panel);
119         drm_connector_cleanup(connector);
120 }
121
122 static const struct drm_connector_funcs sun4i_rgb_con_funcs = {
123         .fill_modes             = drm_helper_probe_single_connector_modes,
124         .destroy                = sun4i_rgb_connector_destroy,
125         .reset                  = drm_atomic_helper_connector_reset,
126         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
127         .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
128 };
129
130 static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
131                                   struct drm_crtc_state *crtc_state,
132                                   struct drm_connector_state *conn_state)
133 {
134         return 0;
135 }
136
137 static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
138 {
139         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
140         struct sun4i_tcon *tcon = rgb->tcon;
141
142         DRM_DEBUG_DRIVER("Enabling RGB output\n");
143
144         if (!IS_ERR(tcon->panel))
145                 drm_panel_prepare(tcon->panel);
146
147         sun4i_tcon_channel_enable(tcon, 0);
148
149         if (!IS_ERR(tcon->panel))
150                 drm_panel_enable(tcon->panel);
151 }
152
153 static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
154 {
155         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
156         struct sun4i_tcon *tcon = rgb->tcon;
157
158         DRM_DEBUG_DRIVER("Disabling RGB output\n");
159
160         if (!IS_ERR(tcon->panel))
161                 drm_panel_disable(tcon->panel);
162
163         sun4i_tcon_channel_disable(tcon, 0);
164
165         if (!IS_ERR(tcon->panel))
166                 drm_panel_unprepare(tcon->panel);
167 }
168
169 static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
170                                        struct drm_display_mode *mode,
171                                        struct drm_display_mode *adjusted_mode)
172 {
173         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
174         struct sun4i_tcon *tcon = rgb->tcon;
175
176         sun4i_tcon0_mode_set(tcon, mode);
177         sun4i_tcon_set_mux(tcon, 0, encoder);
178
179         /* FIXME: This seems to be board specific */
180         clk_set_phase(tcon->dclk, 120);
181 }
182
183 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
184         .atomic_check   = sun4i_rgb_atomic_check,
185         .mode_set       = sun4i_rgb_encoder_mode_set,
186         .disable        = sun4i_rgb_encoder_disable,
187         .enable         = sun4i_rgb_encoder_enable,
188 };
189
190 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
191 {
192         drm_encoder_cleanup(encoder);
193 }
194
195 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
196         .destroy        = sun4i_rgb_enc_destroy,
197 };
198
199 int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
200 {
201         struct drm_encoder *encoder;
202         struct drm_bridge *bridge;
203         struct sun4i_rgb *rgb;
204         int ret;
205
206         rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
207         if (!rgb)
208                 return -ENOMEM;
209         rgb->tcon = tcon;
210         encoder = &rgb->encoder;
211
212         ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
213                                           &tcon->panel, &bridge);
214         if (ret) {
215                 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
216                 return 0;
217         }
218
219         drm_encoder_helper_add(&rgb->encoder,
220                                &sun4i_rgb_enc_helper_funcs);
221         ret = drm_encoder_init(drm,
222                                &rgb->encoder,
223                                &sun4i_rgb_enc_funcs,
224                                DRM_MODE_ENCODER_NONE,
225                                NULL);
226         if (ret) {
227                 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
228                 goto err_out;
229         }
230
231         /* The RGB encoder can only work with the TCON channel 0 */
232         rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
233
234         if (tcon->panel) {
235                 drm_connector_helper_add(&rgb->connector,
236                                          &sun4i_rgb_con_helper_funcs);
237                 ret = drm_connector_init(drm, &rgb->connector,
238                                          &sun4i_rgb_con_funcs,
239                                          DRM_MODE_CONNECTOR_Unknown);
240                 if (ret) {
241                         dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
242                         goto err_cleanup_connector;
243                 }
244
245                 drm_mode_connector_attach_encoder(&rgb->connector,
246                                                   &rgb->encoder);
247
248                 ret = drm_panel_attach(tcon->panel, &rgb->connector);
249                 if (ret) {
250                         dev_err(drm->dev, "Couldn't attach our panel\n");
251                         goto err_cleanup_connector;
252                 }
253         }
254
255         if (bridge) {
256                 ret = drm_bridge_attach(encoder, bridge, NULL);
257                 if (ret) {
258                         dev_err(drm->dev, "Couldn't attach our bridge\n");
259                         goto err_cleanup_connector;
260                 }
261         }
262
263         return 0;
264
265 err_cleanup_connector:
266         drm_encoder_cleanup(&rgb->encoder);
267 err_out:
268         return ret;
269 }
270 EXPORT_SYMBOL(sun4i_rgb_init);