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