gma500: udelay(20000) it too long again
[pandora-kernel.git] / drivers / staging / gma500 / cdv_intel_hdmi.c
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  * FIXME:
27  *      We should probably make this generic and share it with Medfield
28  */
29
30 #include <drm/drmP.h>
31 #include <drm/drm.h>
32 #include <drm/drm_crtc.h>
33 #include <drm/drm_edid.h>
34 #include "psb_intel_drv.h"
35 #include "psb_drv.h"
36 #include "psb_intel_reg.h"
37 #include <linux/pm_runtime.h>
38
39 /* hdmi control bits */
40 #define HDMI_NULL_PACKETS_DURING_VSYNC  (1 << 9)
41 #define HDMI_BORDER_ENABLE              (1 << 7)
42 #define HDMI_AUDIO_ENABLE               (1 << 6)
43 #define HDMI_VSYNC_ACTIVE_HIGH          (1 << 4)
44 #define HDMI_HSYNC_ACTIVE_HIGH          (1 << 3)
45 /* hdmi-b control bits */
46 #define HDMIB_PIPE_B_SELECT             (1 << 30)
47
48
49 struct mid_intel_hdmi_priv {
50         u32 hdmi_reg;
51         u32 save_HDMIB;
52         bool has_hdmi_sink;
53         bool has_hdmi_audio;
54         /* Should set this when detect hotplug */
55         bool hdmi_device_connected;
56         struct mdfld_hdmi_i2c *i2c_bus;
57         struct i2c_adapter *hdmi_i2c_adapter;   /* for control functions */
58         struct drm_device *dev;
59 };
60
61 static void cdv_hdmi_mode_set(struct drm_encoder *encoder,
62                         struct drm_display_mode *mode,
63                         struct drm_display_mode *adjusted_mode)
64 {
65         struct drm_device *dev = encoder->dev;
66         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
67         struct mid_intel_hdmi_priv *hdmi_priv = output->dev_priv;
68         u32 hdmib;
69         struct drm_crtc *crtc = encoder->crtc;
70         struct psb_intel_crtc *intel_crtc = to_psb_intel_crtc(crtc);
71
72         hdmib = (2 << 10);
73
74         if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
75                 hdmib |= HDMI_VSYNC_ACTIVE_HIGH;
76         if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
77                 hdmib |= HDMI_HSYNC_ACTIVE_HIGH;
78
79         if (intel_crtc->pipe == 1)
80                 hdmib |= HDMIB_PIPE_B_SELECT;
81
82         if (hdmi_priv->has_hdmi_audio) {
83                 hdmib |= HDMI_AUDIO_ENABLE;
84                 hdmib |= HDMI_NULL_PACKETS_DURING_VSYNC;
85         }
86
87         REG_WRITE(hdmi_priv->hdmi_reg, hdmib);
88         REG_READ(hdmi_priv->hdmi_reg);
89 }
90
91 static bool cdv_hdmi_mode_fixup(struct drm_encoder *encoder,
92                                   struct drm_display_mode *mode,
93                                   struct drm_display_mode *adjusted_mode)
94 {
95         return true;
96 }
97
98 static void cdv_hdmi_dpms(struct drm_encoder *encoder, int mode)
99 {
100         struct drm_device *dev = encoder->dev;
101         struct psb_intel_output *output = enc_to_psb_intel_output(encoder);
102         struct mid_intel_hdmi_priv *hdmi_priv = output->dev_priv;
103         u32 hdmib;
104
105         hdmib = REG_READ(hdmi_priv->hdmi_reg);
106
107         if (mode != DRM_MODE_DPMS_ON)
108                 REG_WRITE(hdmi_priv->hdmi_reg, hdmib & ~HDMIB_PORT_EN);
109         else
110                 REG_WRITE(hdmi_priv->hdmi_reg, hdmib | HDMIB_PORT_EN);
111         REG_READ(hdmi_priv->hdmi_reg);
112 }
113
114 static void cdv_hdmi_save(struct drm_connector *connector)
115 {
116         struct drm_device *dev = connector->dev;
117         struct psb_intel_output *output = to_psb_intel_output(connector);
118         struct mid_intel_hdmi_priv *hdmi_priv = output->dev_priv;
119
120         hdmi_priv->save_HDMIB = REG_READ(hdmi_priv->hdmi_reg);
121 }
122
123 static void cdv_hdmi_restore(struct drm_connector *connector)
124 {
125         struct drm_device *dev = connector->dev;
126         struct psb_intel_output *output = to_psb_intel_output(connector);
127         struct mid_intel_hdmi_priv *hdmi_priv = output->dev_priv;
128
129         REG_WRITE(hdmi_priv->hdmi_reg, hdmi_priv->save_HDMIB);
130         REG_READ(hdmi_priv->hdmi_reg);
131 }
132
133 static enum drm_connector_status cdv_hdmi_detect(
134                                 struct drm_connector *connector, bool force)
135 {
136         struct psb_intel_output *psb_intel_output =
137                                                 to_psb_intel_output(connector);
138         struct mid_intel_hdmi_priv *hdmi_priv = psb_intel_output->dev_priv;
139         struct edid *edid = NULL;
140         enum drm_connector_status status = connector_status_disconnected;
141
142         edid = drm_get_edid(&psb_intel_output->base,
143                          psb_intel_output->hdmi_i2c_adapter);
144
145         hdmi_priv->has_hdmi_sink = false;
146         hdmi_priv->has_hdmi_audio = false;
147         if (edid) {
148                 if (edid->input & DRM_EDID_INPUT_DIGITAL) {
149                         status = connector_status_connected;
150                         hdmi_priv->has_hdmi_sink =
151                                                 drm_detect_hdmi_monitor(edid);
152                         hdmi_priv->has_hdmi_audio =
153                                                 drm_detect_monitor_audio(edid);
154                 }
155
156                 psb_intel_output->base.display_info.raw_edid = NULL;
157                 kfree(edid);
158         }
159         return status;
160 }
161
162 static int cdv_hdmi_set_property(struct drm_connector *connector,
163                                        struct drm_property *property,
164                                        uint64_t value)
165 {
166         struct drm_encoder *encoder = connector->encoder;
167
168         if (!strcmp(property->name, "scaling mode") && encoder) {
169                 struct psb_intel_crtc *crtc = to_psb_intel_crtc(encoder->crtc);
170                 bool centre;
171                 uint64_t curValue;
172
173                 if (!crtc)
174                         return -1;
175
176                 switch (value) {
177                 case DRM_MODE_SCALE_FULLSCREEN:
178                         break;
179                 case DRM_MODE_SCALE_NO_SCALE:
180                         break;
181                 case DRM_MODE_SCALE_ASPECT:
182                         break;
183                 default:
184                         return -1;
185                 }
186
187                 if (drm_connector_property_get_value(connector,
188                                                         property, &curValue))
189                         return -1;
190
191                 if (curValue == value)
192                         return 0;
193
194                 if (drm_connector_property_set_value(connector,
195                                                         property, value))
196                         return -1;
197
198                 centre = (curValue == DRM_MODE_SCALE_NO_SCALE) ||
199                         (value == DRM_MODE_SCALE_NO_SCALE);
200
201                 if (crtc->saved_mode.hdisplay != 0 &&
202                     crtc->saved_mode.vdisplay != 0) {
203                         if (centre) {
204                                 if (!drm_crtc_helper_set_mode(encoder->crtc, &crtc->saved_mode,
205                                             encoder->crtc->x, encoder->crtc->y, encoder->crtc->fb))
206                                         return -1;
207                         } else {
208                                 struct drm_encoder_helper_funcs *helpers
209                                                     = encoder->helper_private;
210                                 helpers->mode_set(encoder, &crtc->saved_mode,
211                                              &crtc->saved_adjusted_mode);
212                         }
213                 }
214         }
215         return 0;
216 }
217
218 /*
219  * Return the list of HDMI DDC modes if available.
220  */
221 static int cdv_hdmi_get_modes(struct drm_connector *connector)
222 {
223         struct psb_intel_output *psb_intel_output =
224                                         to_psb_intel_output(connector);
225         struct edid *edid = NULL;
226         int ret = 0;
227
228         edid = drm_get_edid(&psb_intel_output->base,
229                          psb_intel_output->hdmi_i2c_adapter);
230         if (edid) {
231                 drm_mode_connector_update_edid_property(&psb_intel_output->
232                                                         base, edid);
233                 ret = drm_add_edid_modes(&psb_intel_output->base, edid);
234                 kfree(edid);
235         }
236         return ret;
237 }
238
239 static int cdv_hdmi_mode_valid(struct drm_connector *connector,
240                                  struct drm_display_mode *mode)
241 {
242
243         if (mode->clock > 165000)
244                 return MODE_CLOCK_HIGH;
245         if (mode->clock < 20000)
246                 return MODE_CLOCK_HIGH;
247
248         /* just in case */
249         if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
250                 return MODE_NO_DBLESCAN;
251
252         /* just in case */
253         if (mode->flags & DRM_MODE_FLAG_INTERLACE)
254                 return MODE_NO_INTERLACE;
255
256         /*
257          * FIXME: for now we limit the size to 1680x1050 on CDV, otherwise it
258          * will go beyond the stolen memory size allocated to the framebuffer
259          */
260         if (mode->hdisplay > 1680)
261                 return MODE_PANEL;
262         if (mode->vdisplay > 1050)
263                 return MODE_PANEL;
264         return MODE_OK;
265 }
266
267 static void cdv_hdmi_destroy(struct drm_connector *connector)
268 {
269         struct psb_intel_output *psb_intel_output =
270                                         to_psb_intel_output(connector);
271
272         if (psb_intel_output->ddc_bus)
273                 psb_intel_i2c_destroy(psb_intel_output->ddc_bus);
274         drm_sysfs_connector_remove(connector);
275         drm_connector_cleanup(connector);
276         kfree(connector);
277 }
278
279 static const struct drm_encoder_helper_funcs cdv_hdmi_helper_funcs = {
280         .dpms = cdv_hdmi_dpms,
281         .mode_fixup = cdv_hdmi_mode_fixup,
282         .prepare = psb_intel_encoder_prepare,
283         .mode_set = cdv_hdmi_mode_set,
284         .commit = psb_intel_encoder_commit,
285 };
286
287 static const struct drm_connector_helper_funcs
288                                         cdv_hdmi_connector_helper_funcs = {
289         .get_modes = cdv_hdmi_get_modes,
290         .mode_valid = cdv_hdmi_mode_valid,
291         .best_encoder = psb_intel_best_encoder,
292 };
293
294 static const struct drm_connector_funcs cdv_hdmi_connector_funcs = {
295         .dpms = drm_helper_connector_dpms,
296         .save = cdv_hdmi_save,
297         .restore = cdv_hdmi_restore,
298         .detect = cdv_hdmi_detect,
299         .fill_modes = drm_helper_probe_single_connector_modes,
300         .set_property = cdv_hdmi_set_property,
301         .destroy = cdv_hdmi_destroy,
302 };
303
304 void cdv_hdmi_init(struct drm_device *dev,
305                         struct psb_intel_mode_device *mode_dev, int reg)
306 {
307         struct psb_intel_output *psb_intel_output;
308         struct drm_connector *connector;
309         struct drm_encoder *encoder;
310         struct mid_intel_hdmi_priv *hdmi_priv;
311         int ddc_bus;
312
313         psb_intel_output = kzalloc(sizeof(struct psb_intel_output) +
314                                sizeof(struct mid_intel_hdmi_priv), GFP_KERNEL);
315         if (!psb_intel_output)
316                 return;
317
318         hdmi_priv = (struct mid_intel_hdmi_priv *)(psb_intel_output + 1);
319         psb_intel_output->mode_dev = mode_dev;
320         connector = &psb_intel_output->base;
321         encoder = &psb_intel_output->enc;
322         drm_connector_init(dev, &psb_intel_output->base,
323                            &cdv_hdmi_connector_funcs,
324                            DRM_MODE_CONNECTOR_DVID);
325
326         drm_encoder_init(dev, &psb_intel_output->enc, &psb_intel_lvds_enc_funcs,
327                          DRM_MODE_ENCODER_TMDS);
328
329         drm_mode_connector_attach_encoder(&psb_intel_output->base,
330                                           &psb_intel_output->enc);
331         psb_intel_output->type = INTEL_OUTPUT_HDMI;
332         hdmi_priv->hdmi_reg = reg;
333         hdmi_priv->has_hdmi_sink = false;
334         psb_intel_output->dev_priv = hdmi_priv;
335
336         drm_encoder_helper_add(encoder, &cdv_hdmi_helper_funcs);
337         drm_connector_helper_add(connector,
338                                  &cdv_hdmi_connector_helper_funcs);
339         connector->display_info.subpixel_order = SubPixelHorizontalRGB;
340         connector->interlace_allowed = false;
341         connector->doublescan_allowed = false;
342
343         drm_connector_attach_property(connector,
344             dev->mode_config.scaling_mode_property, DRM_MODE_SCALE_FULLSCREEN);
345
346         switch (reg) {
347         case SDVOB:
348                 ddc_bus = GPIOE;
349                 break;
350         case SDVOC:
351                 ddc_bus = GPIOD;
352                 break;
353         default:
354                 DRM_ERROR("unknown reg 0x%x for HDMI\n", reg);
355                 goto failed_ddc;
356                 break;
357         }
358
359         psb_intel_output->ddc_bus = psb_intel_i2c_create(dev,
360                                 ddc_bus, (reg == SDVOB) ? "HDMIB" : "HDMIC");
361
362         if (!psb_intel_output->ddc_bus) {
363                 dev_err(dev->dev, "No ddc adapter available!\n");
364                 goto failed_ddc;
365         }
366         psb_intel_output->hdmi_i2c_adapter =
367                                 &(psb_intel_output->ddc_bus->adapter);
368         hdmi_priv->dev = dev;
369         drm_sysfs_connector_add(connector);
370         return;
371
372 failed_ddc:
373         drm_encoder_cleanup(&psb_intel_output->enc);
374         drm_connector_cleanup(&psb_intel_output->base);
375         kfree(psb_intel_output);
376 }