1 /*
2 * Copyright © 2006-2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * jim liu <jim.liu@intel.com>
25 */
26
27 #include <linux/pm_runtime.h>
28
29 #include <drm/drm.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_edid.h>
33 #include <drm/drm_modeset_helper_vtables.h>
34 #include <drm/drm_print.h>
35 #include <drm/drm_simple_kms_helper.h>
36
37 #include "cdv_device.h"
38 #include "psb_drv.h"
39 #include "psb_intel_drv.h"
40 #include "psb_intel_reg.h"
41
42 /* hdmi control bits */
43 #define HDMI_NULL_PACKETS_DURING_VSYNC (1 << 9)
44 #define HDMI_BORDER_ENABLE (1 << 7)
45 #define HDMI_AUDIO_ENABLE (1 << 6)
46 #define HDMI_VSYNC_ACTIVE_HIGH (1 << 4)
47 #define HDMI_HSYNC_ACTIVE_HIGH (1 << 3)
48 /* hdmi-b control bits */
49 #define HDMIB_PIPE_B_SELECT (1 << 30)
50
51
52 struct mid_intel_hdmi_priv {
53 u32 hdmi_reg;
54 u32 save_HDMIB;
55 bool has_hdmi_sink;
56 bool has_hdmi_audio;
57 /* Should set this when detect hotplug */
58 bool hdmi_device_connected;
59 struct drm_device *dev;
60 };
61
cdv_hdmi_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)62 static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
63 struct drm_display_mode *mode,
64 struct drm_display_mode *adjusted_mode)
65 {
66 struct drm_device *dev = encoder->dev;
67 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
68 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
69 u32 hdmib;
70 struct drm_crtc *crtc = encoder->crtc;
71 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
72
73 hdmib = (2 << 10);
74
75 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
76 hdmib |= HDMI_VSYNC_ACTIVE_HIGH;
77 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
78 hdmib |= HDMI_HSYNC_ACTIVE_HIGH;
79
80 if (gma_crtc->pipe == 1)
81 hdmib |= HDMIB_PIPE_B_SELECT;
82
83 if (hdmi_priv->has_hdmi_audio) {
84 hdmib |= HDMI_AUDIO_ENABLE;
85 hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
86 }
87
88 REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
89 REG_READ(hdmi_priv->hdmi_reg);
90 }
91
cdv_hdmi_dpms(struct drm_encoder * encoder,int mode)92 static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
93 {
94 struct drm_device *dev = encoder->dev;
95 struct gma_encoder *gma_encoder = to_gma_encoder(encoder);
96 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
97 u32 hdmib;
98
99 hdmib = REG_READ(hdmi_priv->hdmi_reg);
100
101 if (mode != DRM_MODE_DPMS_ON)
102 REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
103 else
104 REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
105 REG_READ(hdmi_priv->hdmi_reg);
106 }
107
cdv_hdmi_save(struct drm_connector * connector)108 static void cdv_hdmi_save(struct drm_connector *connector)
109 {
110 struct drm_device *dev = connector->dev;
111 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
112 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
113
114 hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
115 }
116
cdv_hdmi_restore(struct drm_connector * connector)117 static void cdv_hdmi_restore(struct drm_connector *connector)
118 {
119 struct drm_device *dev = connector->dev;
120 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
121 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
122
123 REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
124 REG_READ(hdmi_priv->hdmi_reg);
125 }
126
cdv_hdmi_detect(struct drm_connector * connector,bool force)127 static enum drm_connector_status cdv_hdmi_detect(
128 struct drm_connector *connector, bool force)
129 {
130 struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
131 struct mid_intel_hdmi_priv *hdmi_priv = gma_encoder->dev_priv;
132 struct edid *edid = NULL;
133 enum drm_connector_status status = connector_status_disconnected;
134
135 edid = drm_get_edid(connector, connector->ddc);
136
137 hdmi_priv->has_hdmi_sink = false;
138 hdmi_priv->has_hdmi_audio = false;
139 if (edid) {
140 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
141 status = connector_status_connected;
142 hdmi_priv->has_hdmi_sink =
143 drm_detect_hdmi_monitor(edid);
144 hdmi_priv->has_hdmi_audio =
145 drm_detect_monitor_audio(edid);
146 }
147 kfree(edid);
148 }
149 return status;
150 }
151
cdv_hdmi_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t value)152 static int cdv_hdmi_set_property(struct drm_connector *connector,
153 struct drm_property *property,
154 uint64_t value)
155 {
156 struct drm_encoder *encoder = connector->encoder;
157
158 if (!strcmp(property->name, "scaling mode") && encoder) {
159 struct gma_crtc *crtc = to_gma_crtc(encoder->crtc);
160 bool centre;
161 uint64_t curValue;
162
163 if (!crtc)
164 return -1;
165
166 switch (value) {
167 case DRM_MODE_SCALE_FULLSCREEN:
168 break;
169 case DRM_MODE_SCALE_NO_SCALE:
170 break;
171 case DRM_MODE_SCALE_ASPECT:
172 break;
173 default:
174 return -1;
175 }
176
177 if (drm_object_property_get_value(&connector->base,
178 property, &curValue))
179 return -1;
180
181 if (curValue == value)
182 return 0;
183
184 if (drm_object_property_set_value(&connector->base,
185 property, value))
186 return -1;
187
188 centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
189 (value == DRM_MODE_SCALE_NO_SCALE);
190
191 if (crtc->saved_mode.hdisplay != 0 &&
192 crtc->saved_mode.vdisplay != 0) {
193 if (centre) {
194 if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
195 encoder->crtc->x, encoder->crtc->y, encoder->crtc->primary->fb))
196 return -1;
197 } else {
198 const struct drm_encoder_helper_funcs *helpers
199 = encoder->helper_private;
200 helpers->mode_set(encoder, &crtc->saved_mode,
201 &crtc->saved_adjusted_mode);
202 }
203 }
204 }
205 return 0;
206 }
207
208 /*
209 * Return the list of HDMI DDC modes if available.
210 */
cdv_hdmi_get_modes(struct drm_connector * connector)211 static int cdv_hdmi_get_modes(struct drm_connector *connector)
212 {
213 struct edid *edid = NULL;
214 int ret = 0;
215
216 edid = drm_get_edid(connector, connector->ddc);
217 if (edid) {
218 drm_connector_update_edid_property(connector, edid);
219 ret = drm_add_edid_modes(connector, edid);
220 kfree(edid);
221 }
222 return ret;
223 }
224
cdv_hdmi_mode_valid(struct drm_connector * connector,const struct drm_display_mode * mode)225 static enum drm_mode_status cdv_hdmi_mode_valid(struct drm_connector *connector,
226 const struct drm_display_mode *mode)
227 {
228 if (mode->clock > 165000)
229 return MODE_CLOCK_HIGH;
230 if (mode->clock < 20000)
231 return MODE_CLOCK_HIGH;
232
233 /* just in case */
234 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
235 return MODE_NO_DBLESCAN;
236
237 /* just in case */
238 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
239 return MODE_NO_INTERLACE;
240
241 return MODE_OK;
242 }
243
cdv_hdmi_destroy(struct drm_connector * connector)244 static void cdv_hdmi_destroy(struct drm_connector *connector)
245 {
246 struct gma_connector *gma_connector = to_gma_connector(connector);
247 struct gma_i2c_chan *ddc_bus = to_gma_i2c_chan(connector->ddc);
248
249 gma_i2c_destroy(ddc_bus);
250 drm_connector_cleanup(connector);
251 kfree(gma_connector);
252 }
253
254 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
255 .dpms = cdv_hdmi_dpms,
256 .prepare = gma_encoder_prepare,
257 .mode_set = cdv_hdmi_mode_set,
258 .commit = gma_encoder_commit,
259 };
260
261 static const struct drm_connector_helper_funcs
262 cdv_hdmi_connector_helper_funcs = {
263 .get_modes = cdv_hdmi_get_modes,
264 .mode_valid = cdv_hdmi_mode_valid,
265 .best_encoder = gma_best_encoder,
266 };
267
268 static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
269 .dpms = drm_helper_connector_dpms,
270 .detect = cdv_hdmi_detect,
271 .fill_modes = drm_helper_probe_single_connector_modes,
272 .set_property = cdv_hdmi_set_property,
273 .destroy = cdv_hdmi_destroy,
274 };
275
cdv_hdmi_init(struct drm_device * dev,struct psb_intel_mode_device * mode_dev,int reg)276 void cdv_hdmi_init(struct drm_device *dev,
277 struct psb_intel_mode_device *mode_dev, int reg)
278 {
279 struct gma_encoder *gma_encoder;
280 struct gma_connector *gma_connector;
281 struct drm_connector *connector;
282 struct mid_intel_hdmi_priv *hdmi_priv;
283 struct gma_i2c_chan *ddc_bus;
284 int ddc_reg;
285 int ret;
286
287 gma_encoder = kzalloc_obj(struct gma_encoder);
288 if (!gma_encoder)
289 return;
290
291 gma_connector = kzalloc_obj(struct gma_connector);
292 if (!gma_connector)
293 goto err_free_encoder;
294
295 hdmi_priv = kzalloc_obj(struct mid_intel_hdmi_priv);
296 if (!hdmi_priv)
297 goto err_free_connector;
298
299 connector = &gma_connector->base;
300 connector->polled = DRM_CONNECTOR_POLL_HPD;
301 gma_connector->save = cdv_hdmi_save;
302 gma_connector->restore = cdv_hdmi_restore;
303
304 switch (reg) {
305 case SDVOB:
306 ddc_reg = GPIOE;
307 gma_encoder->ddi_select = DDI0_SELECT;
308 break;
309 case SDVOC:
310 ddc_reg = GPIOD;
311 gma_encoder->ddi_select = DDI1_SELECT;
312 break;
313 default:
314 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
315 goto err_free_hdmi_priv;
316 }
317
318 ddc_bus = gma_i2c_create(dev, ddc_reg,
319 (reg == SDVOB) ? "HDMIB" : "HDMIC");
320 if (!ddc_bus) {
321 dev_err(dev->dev, "No ddc adapter available!\n");
322 goto err_free_hdmi_priv;
323 }
324
325 ret = drm_connector_init_with_ddc(dev, connector,
326 &cdv_hdmi_connector_funcs,
327 DRM_MODE_CONNECTOR_DVID,
328 &ddc_bus->base);
329 if (ret)
330 goto err_ddc_destroy;
331
332 ret = drm_simple_encoder_init(dev, &gma_encoder->base,
333 DRM_MODE_ENCODER_TMDS);
334 if (ret)
335 goto err_connector_cleanup;
336
337 gma_connector_attach_encoder(gma_connector, gma_encoder);
338 gma_encoder->type = INTEL_OUTPUT_HDMI;
339 hdmi_priv->hdmi_reg = reg;
340 hdmi_priv->has_hdmi_sink = false;
341 gma_encoder->dev_priv = hdmi_priv;
342
343 drm_encoder_helper_add(&gma_encoder->base, &cdv_hdmi_helper_funcs);
344 drm_connector_helper_add(connector,
345 &cdv_hdmi_connector_helper_funcs);
346 connector->display_info.subpixel_order = SubPixelHorizontalRGB;
347 connector->interlace_allowed = false;
348 connector->doublescan_allowed = false;
349
350 drm_object_attach_property(&connector->base,
351 dev->mode_config.scaling_mode_property,
352 DRM_MODE_SCALE_FULLSCREEN);
353
354 hdmi_priv->dev = dev;
355 return;
356
357 err_connector_cleanup:
358 drm_connector_cleanup(connector);
359 err_ddc_destroy:
360 gma_i2c_destroy(ddc_bus);
361 err_free_hdmi_priv:
362 kfree(hdmi_priv);
363 err_free_connector:
364 kfree(gma_connector);
365 err_free_encoder:
366 kfree(gma_encoder);
367 }
368