blob: b2661f3a3047b6a761992e4e011ecd6ca22d3963 [file] [log] [blame]
Alan Cox6a227d52011-11-03 18:22:37 +00001/*
2 * Copyright © 2006-2007 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 * Eric Anholt <eric@anholt.net>
25 */
26
27#include <linux/i2c.h>
28#include <drm/drmP.h>
29
30#include "intel_bios.h"
31#include "psb_drv.h"
32#include "psb_intel_drv.h"
33#include "psb_intel_reg.h"
34#include "power.h"
Kirill A. Shutemov2acdc9f2012-03-08 16:09:45 +000035#include "cdv_device.h"
Alan Cox6a227d52011-11-03 18:22:37 +000036#include <linux/pm_runtime.h>
37
38
39static void cdv_intel_crt_dpms(struct drm_encoder *encoder, int mode)
40{
41 struct drm_device *dev = encoder->dev;
42 u32 temp, reg;
43 reg = ADPA;
44
45 temp = REG_READ(reg);
46 temp &= ~(ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE);
47 temp &= ~ADPA_DAC_ENABLE;
48
49 switch (mode) {
50 case DRM_MODE_DPMS_ON:
51 temp |= ADPA_DAC_ENABLE;
52 break;
53 case DRM_MODE_DPMS_STANDBY:
54 temp |= ADPA_DAC_ENABLE | ADPA_HSYNC_CNTL_DISABLE;
55 break;
56 case DRM_MODE_DPMS_SUSPEND:
57 temp |= ADPA_DAC_ENABLE | ADPA_VSYNC_CNTL_DISABLE;
58 break;
59 case DRM_MODE_DPMS_OFF:
60 temp |= ADPA_HSYNC_CNTL_DISABLE | ADPA_VSYNC_CNTL_DISABLE;
61 break;
62 }
63
64 REG_WRITE(reg, temp);
65}
66
67static int cdv_intel_crt_mode_valid(struct drm_connector *connector,
68 struct drm_display_mode *mode)
69{
Alan Cox6a227d52011-11-03 18:22:37 +000070 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
71 return MODE_NO_DBLESCAN;
72
73 /* The lowest clock for CDV is 20000KHz */
74 if (mode->clock < 20000)
75 return MODE_CLOCK_LOW;
76
77 /* The max clock for CDV is 355 instead of 400 */
Alan Coxb60bfb62012-04-25 14:37:53 +010078 if (mode->clock > 355000)
Alan Cox6a227d52011-11-03 18:22:37 +000079 return MODE_CLOCK_HIGH;
80
Alan Cox6a227d52011-11-03 18:22:37 +000081 return MODE_OK;
82}
83
84static bool cdv_intel_crt_mode_fixup(struct drm_encoder *encoder,
Laurent Pincharte811f5a2012-07-17 17:56:50 +020085 const struct drm_display_mode *mode,
Alan Cox6a227d52011-11-03 18:22:37 +000086 struct drm_display_mode *adjusted_mode)
87{
88 return true;
89}
90
91static void cdv_intel_crt_mode_set(struct drm_encoder *encoder,
92 struct drm_display_mode *mode,
93 struct drm_display_mode *adjusted_mode)
94{
95
96 struct drm_device *dev = encoder->dev;
97 struct drm_crtc *crtc = encoder->crtc;
Patrik Jakobsson63068652013-07-22 01:31:23 +020098 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
Alan Cox6a227d52011-11-03 18:22:37 +000099 int dpll_md_reg;
100 u32 adpa, dpll_md;
101 u32 adpa_reg;
102
Patrik Jakobsson63068652013-07-22 01:31:23 +0200103 if (gma_crtc->pipe == 0)
Alan Cox6a227d52011-11-03 18:22:37 +0000104 dpll_md_reg = DPLL_A_MD;
105 else
106 dpll_md_reg = DPLL_B_MD;
107
108 adpa_reg = ADPA;
109
110 /*
111 * Disable separate mode multiplier used when cloning SDVO to CRT
112 * XXX this needs to be adjusted when we really are cloning
113 */
114 {
115 dpll_md = REG_READ(dpll_md_reg);
116 REG_WRITE(dpll_md_reg,
117 dpll_md & ~DPLL_MD_UDI_MULTIPLIER_MASK);
118 }
119
120 adpa = 0;
121 if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
122 adpa |= ADPA_HSYNC_ACTIVE_HIGH;
123 if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
124 adpa |= ADPA_VSYNC_ACTIVE_HIGH;
125
Patrik Jakobsson63068652013-07-22 01:31:23 +0200126 if (gma_crtc->pipe == 0)
Alan Cox6a227d52011-11-03 18:22:37 +0000127 adpa |= ADPA_PIPE_A_SELECT;
128 else
129 adpa |= ADPA_PIPE_B_SELECT;
130
131 REG_WRITE(adpa_reg, adpa);
132}
133
134
135/**
136 * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect CRT presence.
137 *
138 * \return true if CRT is connected.
139 * \return false if CRT is disconnected.
140 */
141static bool cdv_intel_crt_detect_hotplug(struct drm_connector *connector,
142 bool force)
143{
144 struct drm_device *dev = connector->dev;
145 u32 hotplug_en;
146 int i, tries = 0, ret = false;
Alan Coxd235e642012-04-25 14:38:07 +0100147 u32 orig;
Alan Cox6a227d52011-11-03 18:22:37 +0000148
149 /*
150 * On a CDV thep, CRT detect sequence need to be done twice
151 * to get a reliable result.
152 */
153 tries = 2;
154
Alan Coxd235e642012-04-25 14:38:07 +0100155 orig = hotplug_en = REG_READ(PORT_HOTPLUG_EN);
Alan Cox6a227d52011-11-03 18:22:37 +0000156 hotplug_en &= ~(CRT_HOTPLUG_DETECT_MASK);
157 hotplug_en |= CRT_HOTPLUG_FORCE_DETECT;
158
159 hotplug_en |= CRT_HOTPLUG_ACTIVATION_PERIOD_64;
160 hotplug_en |= CRT_HOTPLUG_VOLTAGE_COMPARE_50;
161
162 for (i = 0; i < tries ; i++) {
163 unsigned long timeout;
164 /* turn on the FORCE_DETECT */
165 REG_WRITE(PORT_HOTPLUG_EN, hotplug_en);
166 timeout = jiffies + msecs_to_jiffies(1000);
167 /* wait for FORCE_DETECT to go off */
168 do {
169 if (!(REG_READ(PORT_HOTPLUG_EN) &
170 CRT_HOTPLUG_FORCE_DETECT))
171 break;
172 msleep(1);
173 } while (time_after(timeout, jiffies));
174 }
175
176 if ((REG_READ(PORT_HOTPLUG_STAT) & CRT_HOTPLUG_MONITOR_MASK) !=
177 CRT_HOTPLUG_MONITOR_NONE)
178 ret = true;
179
Alan Coxd235e642012-04-25 14:38:07 +0100180 /* clear the interrupt we just generated, if any */
181 REG_WRITE(PORT_HOTPLUG_STAT, CRT_HOTPLUG_INT_STATUS);
182
183 /* and put the bits back */
184 REG_WRITE(PORT_HOTPLUG_EN, orig);
Alan Cox6a227d52011-11-03 18:22:37 +0000185 return ret;
186}
187
188static enum drm_connector_status cdv_intel_crt_detect(
189 struct drm_connector *connector, bool force)
190{
191 if (cdv_intel_crt_detect_hotplug(connector, force))
192 return connector_status_connected;
193 else
194 return connector_status_disconnected;
195}
196
197static void cdv_intel_crt_destroy(struct drm_connector *connector)
198{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000199 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200200 gma_attached_encoder(connector);
Alan Cox6a227d52011-11-03 18:22:37 +0000201
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000202 psb_intel_i2c_destroy(psb_intel_encoder->ddc_bus);
Alan Cox6a227d52011-11-03 18:22:37 +0000203 drm_sysfs_connector_remove(connector);
204 drm_connector_cleanup(connector);
205 kfree(connector);
206}
207
208static int cdv_intel_crt_get_modes(struct drm_connector *connector)
209{
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000210 struct psb_intel_encoder *psb_intel_encoder =
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200211 gma_attached_encoder(connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000212 return psb_intel_ddc_get_modes(connector, &psb_intel_encoder->ddc_bus->adapter);
Alan Cox6a227d52011-11-03 18:22:37 +0000213}
214
215static int cdv_intel_crt_set_property(struct drm_connector *connector,
216 struct drm_property *property,
217 uint64_t value)
218{
219 return 0;
220}
221
222/*
223 * Routines for controlling stuff on the analog port
224 */
225
226static const struct drm_encoder_helper_funcs cdv_intel_crt_helper_funcs = {
227 .dpms = cdv_intel_crt_dpms,
228 .mode_fixup = cdv_intel_crt_mode_fixup,
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200229 .prepare = gma_encoder_prepare,
230 .commit = gma_encoder_commit,
Alan Cox6a227d52011-11-03 18:22:37 +0000231 .mode_set = cdv_intel_crt_mode_set,
232};
233
234static const struct drm_connector_funcs cdv_intel_crt_connector_funcs = {
235 .dpms = drm_helper_connector_dpms,
236 .detect = cdv_intel_crt_detect,
237 .fill_modes = drm_helper_probe_single_connector_modes,
238 .destroy = cdv_intel_crt_destroy,
239 .set_property = cdv_intel_crt_set_property,
240};
241
242static const struct drm_connector_helper_funcs
243 cdv_intel_crt_connector_helper_funcs = {
244 .mode_valid = cdv_intel_crt_mode_valid,
245 .get_modes = cdv_intel_crt_get_modes,
Patrik Jakobssonc9d49592013-07-11 01:02:01 +0200246 .best_encoder = gma_best_encoder,
Alan Cox6a227d52011-11-03 18:22:37 +0000247};
248
249static void cdv_intel_crt_enc_destroy(struct drm_encoder *encoder)
250{
251 drm_encoder_cleanup(encoder);
252}
253
254static const struct drm_encoder_funcs cdv_intel_crt_enc_funcs = {
255 .destroy = cdv_intel_crt_enc_destroy,
256};
257
258void cdv_intel_crt_init(struct drm_device *dev,
259 struct psb_intel_mode_device *mode_dev)
260{
261
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200262 struct gma_connector *gma_connector;
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000263 struct psb_intel_encoder *psb_intel_encoder;
Alan Cox6a227d52011-11-03 18:22:37 +0000264 struct drm_connector *connector;
265 struct drm_encoder *encoder;
266
267 u32 i2c_reg;
268
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000269 psb_intel_encoder = kzalloc(sizeof(struct psb_intel_encoder), GFP_KERNEL);
270 if (!psb_intel_encoder)
Alan Cox6a227d52011-11-03 18:22:37 +0000271 return;
272
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200273 gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
274 if (!gma_connector)
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000275 goto failed_connector;
276
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200277 connector = &gma_connector->base;
Kero van Gelderbda50032013-03-31 13:38:44 +0200278 connector->polled = DRM_CONNECTOR_POLL_HPD;
Alan Cox6a227d52011-11-03 18:22:37 +0000279 drm_connector_init(dev, connector,
280 &cdv_intel_crt_connector_funcs, DRM_MODE_CONNECTOR_VGA);
281
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000282 encoder = &psb_intel_encoder->base;
Alan Cox6a227d52011-11-03 18:22:37 +0000283 drm_encoder_init(dev, encoder,
284 &cdv_intel_crt_enc_funcs, DRM_MODE_ENCODER_DAC);
285
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200286 gma_connector_attach_encoder(gma_connector, psb_intel_encoder);
Alan Cox6a227d52011-11-03 18:22:37 +0000287
288 /* Set up the DDC bus. */
289 i2c_reg = GPIOA;
290 /* Remove the following code for CDV */
291 /*
292 if (dev_priv->crt_ddc_bus != 0)
293 i2c_reg = dev_priv->crt_ddc_bus;
294 }*/
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000295 psb_intel_encoder->ddc_bus = psb_intel_i2c_create(dev,
296 i2c_reg, "CRTDDC_A");
297 if (!psb_intel_encoder->ddc_bus) {
Alan Cox6a227d52011-11-03 18:22:37 +0000298 dev_printk(KERN_ERR, &dev->pdev->dev, "DDC bus registration "
299 "failed.\n");
300 goto failed_ddc;
301 }
302
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000303 psb_intel_encoder->type = INTEL_OUTPUT_ANALOG;
Alan Cox6a227d52011-11-03 18:22:37 +0000304 /*
305 psb_intel_output->clone_mask = (1 << INTEL_ANALOG_CLONE_BIT);
306 psb_intel_output->crtc_mask = (1 << 0) | (1 << 1);
307 */
308 connector->interlace_allowed = 0;
309 connector->doublescan_allowed = 0;
310
311 drm_encoder_helper_add(encoder, &cdv_intel_crt_helper_funcs);
312 drm_connector_helper_add(connector,
313 &cdv_intel_crt_connector_helper_funcs);
314
315 drm_sysfs_connector_add(connector);
316
317 return;
318failed_ddc:
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000319 drm_encoder_cleanup(&psb_intel_encoder->base);
Patrik Jakobssona3d5d752013-07-22 17:05:25 +0200320 drm_connector_cleanup(&gma_connector->base);
321 kfree(gma_connector);
Patrik Jakobssona12d6a02011-12-19 21:41:22 +0000322failed_connector:
323 kfree(psb_intel_encoder);
Alan Cox6a227d52011-11-03 18:22:37 +0000324 return;
325}