Merge tag 'samsung-fixes-4.7' of git://git.kernel.org/pub/scm/linux/kernel/git/krzk...
[deliverable/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_drv.h"
21 #include "sun4i_tcon.h"
22
23 struct sun4i_rgb {
24 struct drm_connector connector;
25 struct drm_encoder encoder;
26
27 struct sun4i_drv *drv;
28 };
29
30 static inline struct sun4i_rgb *
31 drm_connector_to_sun4i_rgb(struct drm_connector *connector)
32 {
33 return container_of(connector, struct sun4i_rgb,
34 connector);
35 }
36
37 static inline struct sun4i_rgb *
38 drm_encoder_to_sun4i_rgb(struct drm_encoder *encoder)
39 {
40 return container_of(encoder, struct sun4i_rgb,
41 encoder);
42 }
43
44 static int sun4i_rgb_get_modes(struct drm_connector *connector)
45 {
46 struct sun4i_rgb *rgb =
47 drm_connector_to_sun4i_rgb(connector);
48 struct sun4i_drv *drv = rgb->drv;
49 struct sun4i_tcon *tcon = drv->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 u32 hsync = mode->hsync_end - mode->hsync_start;
58 u32 vsync = mode->vsync_end - mode->vsync_start;
59
60 DRM_DEBUG_DRIVER("Validating modes...\n");
61
62 if (hsync < 1)
63 return MODE_HSYNC_NARROW;
64
65 if (hsync > 0x3ff)
66 return MODE_HSYNC_WIDE;
67
68 if ((mode->hdisplay < 1) || (mode->htotal < 1))
69 return MODE_H_ILLEGAL;
70
71 if ((mode->hdisplay > 0x7ff) || (mode->htotal > 0xfff))
72 return MODE_BAD_HVALUE;
73
74 DRM_DEBUG_DRIVER("Horizontal parameters OK\n");
75
76 if (vsync < 1)
77 return MODE_VSYNC_NARROW;
78
79 if (vsync > 0x3ff)
80 return MODE_VSYNC_WIDE;
81
82 if ((mode->vdisplay < 1) || (mode->vtotal < 1))
83 return MODE_V_ILLEGAL;
84
85 if ((mode->vdisplay > 0x7ff) || (mode->vtotal > 0xfff))
86 return MODE_BAD_VVALUE;
87
88 DRM_DEBUG_DRIVER("Vertical parameters OK\n");
89
90 return MODE_OK;
91 }
92
93 static struct drm_encoder *
94 sun4i_rgb_best_encoder(struct drm_connector *connector)
95 {
96 struct sun4i_rgb *rgb =
97 drm_connector_to_sun4i_rgb(connector);
98
99 return &rgb->encoder;
100 }
101
102 static struct drm_connector_helper_funcs sun4i_rgb_con_helper_funcs = {
103 .get_modes = sun4i_rgb_get_modes,
104 .mode_valid = sun4i_rgb_mode_valid,
105 .best_encoder = sun4i_rgb_best_encoder,
106 };
107
108 static enum drm_connector_status
109 sun4i_rgb_connector_detect(struct drm_connector *connector, bool force)
110 {
111 return connector_status_connected;
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 .detect = sun4i_rgb_connector_detect,
128 .fill_modes = drm_helper_probe_single_connector_modes,
129 .destroy = sun4i_rgb_connector_destroy,
130 .reset = drm_atomic_helper_connector_reset,
131 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
132 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
133 };
134
135 static int sun4i_rgb_atomic_check(struct drm_encoder *encoder,
136 struct drm_crtc_state *crtc_state,
137 struct drm_connector_state *conn_state)
138 {
139 return 0;
140 }
141
142 static void sun4i_rgb_encoder_enable(struct drm_encoder *encoder)
143 {
144 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
145 struct sun4i_drv *drv = rgb->drv;
146 struct sun4i_tcon *tcon = drv->tcon;
147
148 DRM_DEBUG_DRIVER("Enabling RGB output\n");
149
150 drm_panel_enable(tcon->panel);
151 sun4i_tcon_channel_enable(tcon, 0);
152 }
153
154 static void sun4i_rgb_encoder_disable(struct drm_encoder *encoder)
155 {
156 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
157 struct sun4i_drv *drv = rgb->drv;
158 struct sun4i_tcon *tcon = drv->tcon;
159
160 DRM_DEBUG_DRIVER("Disabling RGB output\n");
161
162 sun4i_tcon_channel_disable(tcon, 0);
163 drm_panel_disable(tcon->panel);
164 }
165
166 static void sun4i_rgb_encoder_mode_set(struct drm_encoder *encoder,
167 struct drm_display_mode *mode,
168 struct drm_display_mode *adjusted_mode)
169 {
170 struct sun4i_rgb *rgb = drm_encoder_to_sun4i_rgb(encoder);
171 struct sun4i_drv *drv = rgb->drv;
172 struct sun4i_tcon *tcon = drv->tcon;
173
174 sun4i_tcon0_mode_set(tcon, mode);
175
176 clk_set_rate(tcon->dclk, mode->crtc_clock * 1000);
177
178 /* FIXME: This seems to be board specific */
179 clk_set_phase(tcon->dclk, 120);
180 }
181
182 static struct drm_encoder_helper_funcs sun4i_rgb_enc_helper_funcs = {
183 .atomic_check = sun4i_rgb_atomic_check,
184 .mode_set = sun4i_rgb_encoder_mode_set,
185 .disable = sun4i_rgb_encoder_disable,
186 .enable = sun4i_rgb_encoder_enable,
187 };
188
189 static void sun4i_rgb_enc_destroy(struct drm_encoder *encoder)
190 {
191 drm_encoder_cleanup(encoder);
192 }
193
194 static struct drm_encoder_funcs sun4i_rgb_enc_funcs = {
195 .destroy = sun4i_rgb_enc_destroy,
196 };
197
198 int sun4i_rgb_init(struct drm_device *drm)
199 {
200 struct sun4i_drv *drv = drm->dev_private;
201 struct sun4i_tcon *tcon = drv->tcon;
202 struct sun4i_rgb *rgb;
203 int ret;
204
205 /* If we don't have a panel, there's no point in going on */
206 if (!tcon->panel)
207 return -ENODEV;
208
209 rgb = devm_kzalloc(drm->dev, sizeof(*rgb), GFP_KERNEL);
210 if (!rgb)
211 return -ENOMEM;
212 rgb->drv = drv;
213
214 drm_encoder_helper_add(&rgb->encoder,
215 &sun4i_rgb_enc_helper_funcs);
216 ret = drm_encoder_init(drm,
217 &rgb->encoder,
218 &sun4i_rgb_enc_funcs,
219 DRM_MODE_ENCODER_NONE,
220 NULL);
221 if (ret) {
222 dev_err(drm->dev, "Couldn't initialise the rgb encoder\n");
223 goto err_out;
224 }
225
226 /* The RGB encoder can only work with the TCON channel 0 */
227 rgb->encoder.possible_crtcs = BIT(0);
228
229 drm_connector_helper_add(&rgb->connector,
230 &sun4i_rgb_con_helper_funcs);
231 ret = drm_connector_init(drm, &rgb->connector,
232 &sun4i_rgb_con_funcs,
233 DRM_MODE_CONNECTOR_Unknown);
234 if (ret) {
235 dev_err(drm->dev, "Couldn't initialise the rgb connector\n");
236 goto err_cleanup_connector;
237 }
238
239 drm_mode_connector_attach_encoder(&rgb->connector, &rgb->encoder);
240
241 drm_panel_attach(tcon->panel, &rgb->connector);
242
243 return 0;
244
245 err_cleanup_connector:
246 drm_encoder_cleanup(&rgb->encoder);
247 err_out:
248 return ret;
249 }
250 EXPORT_SYMBOL(sun4i_rgb_init);
This page took 0.037534 seconds and 6 git commands to generate.