Merge tag 'v4.11-rc7' into drm-next
[muen/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_drv.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_drv        *drv;
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_drv *drv = rgb->drv;
51         struct sun4i_tcon *tcon = drv->tcon;
52
53         return drm_panel_get_modes(tcon->panel);
54 }
55
56 static int sun4i_rgb_mode_valid(struct drm_connector *connector,
57                                 struct drm_display_mode *mode)
58 {
59         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
60         struct sun4i_drv *drv = rgb->drv;
61         struct sun4i_tcon *tcon = drv->tcon;
62         u32 hsync = mode->hsync_end - mode->hsync_start;
63         u32 vsync = mode->vsync_end - mode->vsync_start;
64         unsigned long rate = mode->clock * 1000;
65         long rounded_rate;
66
67         DRM_DEBUG_DRIVER("Validating modes...\n");
68
69         if (hsync < 1)
70                 return MODE_HSYNC_NARROW;
71
72         if (hsync > 0x3ff)
73                 return MODE_HSYNC_WIDE;
74
75         if ((mode->hdisplay < 1) || (mode->htotal < 1))
76                 return MODE_H_ILLEGAL;
77
78         if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
79                 return MODE_BAD_HVALUE;
80
81         DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
82
83         if (vsync < 1)
84                 return MODE_VSYNC_NARROW;
85
86         if (vsync > 0x3ff)
87                 return MODE_VSYNC_WIDE;
88
89         if ((mode->vdisplay < 1) || (mode->vtotal < 1))
90                 return MODE_V_ILLEGAL;
91
92         if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
93                 return MODE_BAD_VVALUE;
94
95         DRM_DEBUG_DRIVER("Vertical parameters OK\n");
96
97         rounded_rate = clk_round_rate(tcon->dclk, rate);
98         if (rounded_rate < rate)
99                 return MODE_CLOCK_LOW;
100
101         if (rounded_rate > rate)
102                 return MODE_CLOCK_HIGH;
103
104         DRM_DEBUG_DRIVER("Clock rate OK\n");
105
106         return MODE_OK;
107 }
108
109 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
110         .get_modes      = sun4i_rgb_get_modes,
111         .mode_valid     = sun4i_rgb_mode_valid,
112 };
113
114 static void
115 sun4i_rgb_connector_destroy(struct drm_connector *connector)
116 {
117         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
118         struct sun4i_drv *drv = rgb->drv;
119         struct sun4i_tcon *tcon = drv->tcon;
120
121         drm_panel_detach(tcon->panel);
122         drm_connector_cleanup(connector);
123 }
124
125 static struct drm_connector_funcs sun4i_rgb_con_funcs = {
126         .dpms                   = drm_atomic_helper_connector_dpms,
127         .fill_modes             = drm_helper_probe_single_connector_modes,
128         .destroy                = sun4i_rgb_connector_destroy,
129         .reset                  = drm_atomic_helper_connector_reset,
130         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
131         .atomic_destroy_state   = drm_atomic_helper_connector_destroy_state,
132 };
133
134 static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
135                                   struct drm_crtc_state *crtc_state,
136                                   struct drm_connector_state *conn_state)
137 {
138         return 0;
139 }
140
141 static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
142 {
143         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
144         struct sun4i_drv *drv = rgb->drv;
145         struct sun4i_tcon *tcon = drv->tcon;
146
147         DRM_DEBUG_DRIVER("Enabling RGB output\n");
148
149         if (!IS_ERR(tcon->panel))
150                 drm_panel_prepare(tcon->panel);
151
152         sun4i_tcon_channel_enable(tcon, 0);
153
154         if (!IS_ERR(tcon->panel))
155                 drm_panel_enable(tcon->panel);
156 }
157
158 static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
159 {
160         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
161         struct sun4i_drv *drv = rgb->drv;
162         struct sun4i_tcon *tcon = drv->tcon;
163
164         DRM_DEBUG_DRIVER("Disabling RGB output\n");
165
166         if (!IS_ERR(tcon->panel))
167                 drm_panel_disable(tcon->panel);
168
169         sun4i_tcon_channel_disable(tcon, 0);
170
171         if (!IS_ERR(tcon->panel))
172                 drm_panel_unprepare(tcon->panel);
173 }
174
175 static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
176                                        struct drm_display_mode *mode,
177                                        struct drm_display_mode *adjusted_mode)
178 {
179         struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
180         struct sun4i_drv *drv = rgb->drv;
181         struct sun4i_tcon *tcon = drv->tcon;
182
183         sun4i_tcon0_mode_set(tcon, mode);
184
185         clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
186
187         /* FIXME: This seems to be board specific */
188         clk_set_phase(tcon->dclk, 120);
189 }
190
191 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
192         .atomic_check   = sun4i_rgb_atomic_check,
193         .mode_set       = sun4i_rgb_encoder_mode_set,
194         .disable        = sun4i_rgb_encoder_disable,
195         .enable         = sun4i_rgb_encoder_enable,
196 };
197
198 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
199 {
200         drm_encoder_cleanup(encoder);
201 }
202
203 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
204         .destroy        = sun4i_rgb_enc_destroy,
205 };
206
207 int sun4i_rgb_init(struct drm_device *drm)
208 {
209         struct sun4i_drv *drv = drm->dev_private;
210         struct sun4i_tcon *tcon = drv->tcon;
211         struct drm_encoder *encoder;
212         struct drm_bridge *bridge;
213         struct sun4i_rgb *rgb;
214         int ret;
215
216         rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
217         if (!rgb)
218                 return -ENOMEM;
219         rgb->drv = drv;
220         encoder = &rgb->encoder;
221
222         ret = drm_of_find_panel_or_bridge(tcon->dev->of_node, 1, 0,
223                                           &tcon->panel, &bridge);
224         if (ret) {
225                 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
226                 return 0;
227         }
228
229         drm_encoder_helper_add(&rgb->encoder,
230                                &sun4i_rgb_enc_helper_funcs);
231         ret = drm_encoder_init(drm,
232                                &rgb->encoder,
233                                &sun4i_rgb_enc_funcs,
234                                DRM_MODE_ENCODER_NONE,
235                                NULL);
236         if (ret) {
237                 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
238                 goto err_out;
239         }
240
241         /* The RGB encoder can only work with the TCON channel 0 */
242         rgb->encoder.possible_crtcs = BIT(0);
243
244         if (tcon->panel) {
245                 drm_connector_helper_add(&rgb->connector,
246                                          &sun4i_rgb_con_helper_funcs);
247                 ret = drm_connector_init(drm, &rgb->connector,
248                                          &sun4i_rgb_con_funcs,
249                                          DRM_MODE_CONNECTOR_Unknown);
250                 if (ret) {
251                         dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
252                         goto err_cleanup_connector;
253                 }
254
255                 drm_mode_connector_attach_encoder(&rgb->connector,
256                                                   &rgb->encoder);
257
258                 ret = drm_panel_attach(tcon->panel, &rgb->connector);
259                 if (ret) {
260                         dev_err(drm->dev, "Couldn't attach our panel\n");
261                         goto err_cleanup_connector;
262                 }
263         }
264
265         if (bridge) {
266                 ret = drm_bridge_attach(encoder, bridge, NULL);
267                 if (ret) {
268                         dev_err(drm->dev, "Couldn't attach our bridge\n");
269                         goto err_cleanup_connector;
270                 }
271         }
272
273         return 0;
274
275 err_cleanup_connector:
276         drm_encoder_cleanup(&rgb->encoder);
277 err_out:
278         return ret;
279 }
280 EXPORT_SYMBOL(sun4i_rgb_init);