MAINTAINERS: Add sun4i-drm git repo
[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_panel.h>
19
20 #include "sun4i_crtc.h"
21 #include "sun4i_tcon.h"
22 #include "sun4i_rgb.h"
23
24 struct sun4i_rgb {
25         struct drm_connector    connector;
26         struct drm_encoder      encoder;
27
28         struct sun4i_tcon       *tcon;
29 };
30
31 static inline struct sun4i_rgb *
32 drm_connector_to_sun4i_rgb(struct drm_connector *connector)
33 {
34         return container_of(connector, struct sun4i_rgb,
35                             connector);
36 }
37
38 static inline struct sun4i_rgb *
39 drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
40 {
41         return container_of(encoder, struct sun4i_rgb,
42                             encoder);
43 }
44
45 static int sun4i_rgb_get_modes(struct drm_connector *connector)
46 {
47         struct sun4i_rgb *rgb =
48                 drm_connector_to_sun4i_rgb(connector);
49         struct sun4i_tcon *tcon = rgb->tcon;
50
51         return drm_panel_get_modes(tcon->panel);
52 }
53
54 static int sun4i_rgb_mode_valid(struct drm_connector *connector,
55                                 struct drm_display_mode *mode)
56 {
57         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
58         struct sun4i_tcon *tcon = rgb->tcon;
59         u32 hsync = mode->hsync_end - mode->hsync_start;
60         u32 vsync = mode->vsync_end - mode->vsync_start;
61         unsigned long rate = mode->clock * 1000;
62         long rounded_rate;
63
64         DRM_DEBUG_DRIVER("Validating modes...\n");
65
66         if (hsync < 1)
67                 return MODE_HSYNC_NARROW;
68
69         if (hsync > 0x3ff)
70                 return MODE_HSYNC_WIDE;
71
72         if ((mode->hdisplay < 1) || (mode->htotal < 1))
73                 return MODE_H_ILLEGAL;
74
75         if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
76                 return MODE_BAD_HVALUE;
77
78         DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
79
80         if (vsync < 1)
81                 return MODE_VSYNC_NARROW;
82
83         if (vsync > 0x3ff)
84                 return MODE_VSYNC_WIDE;
85
86         if ((mode->vdisplay < 1) || (mode->vtotal < 1))
87                 return MODE_V_ILLEGAL;
88
89         if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
90                 return MODE_BAD_VVALUE;
91
92         DRM_DEBUG_DRIVER("Vertical parameters OK\n");
93
94         rounded_rate = clk_round_rate(tcon->dclk, rate);
95         if (rounded_rate < rate)
96                 return MODE_CLOCK_LOW;
97
98         if (rounded_rate > rate)
99                 return MODE_CLOCK_HIGH;
100
101         DRM_DEBUG_DRIVER("Clock rate OK\n");
102
103         return MODE_OK;
104 }
105
106 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
107         .get_modes      = sun4i_rgb_get_modes,
108         .mode_valid     = sun4i_rgb_mode_valid,
109 };
110
111 static void
112 sun4i_rgb_connector_destroy(struct drm_connector *connector)
113 {
114         struct sun4i_rgb *rgb = drm_connector_to_sun4i_rgb(connector);
115         struct sun4i_tcon *tcon = rgb->tcon;
116
117         drm_panel_detach(tcon->panel);
118         drm_connector_cleanup(connector);
119 }
120
121 static struct drm_connector_funcs sun4i_rgb_con_funcs = {
122         .dpms                   = drm_atomic_helper_connector_dpms,
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
178         clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
179
180         /* FIXME: This seems to be board specific */
181         clk_set_phase(tcon->dclk, 120);
182 }
183
184 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
185         .atomic_check   = sun4i_rgb_atomic_check,
186         .mode_set       = sun4i_rgb_encoder_mode_set,
187         .disable        = sun4i_rgb_encoder_disable,
188         .enable         = sun4i_rgb_encoder_enable,
189 };
190
191 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
192 {
193         drm_encoder_cleanup(encoder);
194 }
195
196 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
197         .destroy        = sun4i_rgb_enc_destroy,
198 };
199
200 int sun4i_rgb_init(struct drm_device *drm, struct sun4i_tcon *tcon)
201 {
202         struct drm_encoder *encoder;
203         struct drm_bridge *bridge;
204         struct sun4i_rgb *rgb;
205         int ret;
206
207         rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
208         if (!rgb)
209                 return -ENOMEM;
210         rgb->tcon = tcon;
211         encoder = &rgb->encoder;
212
213         tcon->panel = sun4i_tcon_find_panel(tcon->dev->of_node);
214         bridge = sun4i_tcon_find_bridge(tcon->dev->of_node);
215         if (IS_ERR(tcon->panel) && IS_ERR(bridge)) {
216                 dev_info(drm->dev, "No panel or bridge found... RGB output disabled\n");
217                 return 0;
218         }
219
220         drm_encoder_helper_add(&rgb->encoder,
221                                &sun4i_rgb_enc_helper_funcs);
222         ret = drm_encoder_init(drm,
223                                &rgb->encoder,
224                                &sun4i_rgb_enc_funcs,
225                                DRM_MODE_ENCODER_NONE,
226                                NULL);
227         if (ret) {
228                 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
229                 goto err_out;
230         }
231
232         /* The RGB encoder can only work with the TCON channel 0 */
233         rgb->encoder.possible_crtcs = BIT(drm_crtc_index(&tcon->crtc->crtc));
234
235         if (!IS_ERR(tcon->panel)) {
236                 drm_connector_helper_add(&rgb->connector,
237                                          &sun4i_rgb_con_helper_funcs);
238                 ret = drm_connector_init(drm, &rgb->connector,
239                                          &sun4i_rgb_con_funcs,
240                                          DRM_MODE_CONNECTOR_Unknown);
241                 if (ret) {
242                         dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
243                         goto err_cleanup_connector;
244                 }
245
246                 drm_mode_connector_attach_encoder(&rgb->connector,
247                                                   &rgb->encoder);
248
249                 ret = drm_panel_attach(tcon->panel, &rgb->connector);
250                 if (ret) {
251                         dev_err(drm->dev, "Couldn't attach our panel\n");
252                         goto err_cleanup_connector;
253                 }
254         }
255
256         if (!IS_ERR(bridge)) {
257                 ret = drm_bridge_attach(encoder, bridge, NULL);
258                 if (ret) {
259                         dev_err(drm->dev, "Couldn't attach our bridge\n");
260                         goto err_cleanup_connector;
261                 }
262         }
263
264         return 0;
265
266 err_cleanup_connector:
267         drm_encoder_cleanup(&rgb->encoder);
268 err_out:
269         return ret;
270 }
271 EXPORT_SYMBOL(sun4i_rgb_init);