xref: /linux/drivers/gpu/drm/gma500/cdv_intel_dp.c (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
18695b612SAlan Cox /*
28695b612SAlan Cox  * Copyright © 2012 Intel Corporation
38695b612SAlan Cox  *
48695b612SAlan Cox  * Permission is hereby granted, free of charge, to any person obtaining a
58695b612SAlan Cox  * copy of this software and associated documentation files (the "Software"),
68695b612SAlan Cox  * to deal in the Software without restriction, including without limitation
78695b612SAlan Cox  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88695b612SAlan Cox  * and/or sell copies of the Software, and to permit persons to whom the
98695b612SAlan Cox  * Software is furnished to do so, subject to the following conditions:
108695b612SAlan Cox  *
118695b612SAlan Cox  * The above copyright notice and this permission notice (including the next
128695b612SAlan Cox  * paragraph) shall be included in all copies or substantial portions of the
138695b612SAlan Cox  * Software.
148695b612SAlan Cox  *
158695b612SAlan Cox  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168695b612SAlan Cox  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178695b612SAlan Cox  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188695b612SAlan Cox  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198695b612SAlan Cox  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
208695b612SAlan Cox  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
218695b612SAlan Cox  * IN THE SOFTWARE.
228695b612SAlan Cox  *
238695b612SAlan Cox  * Authors:
248695b612SAlan Cox  *    Keith Packard <keithp@keithp.com>
258695b612SAlan Cox  *
268695b612SAlan Cox  */
278695b612SAlan Cox 
288695b612SAlan Cox #include <linux/i2c.h>
2916559ae4SGreg Kroah-Hartman #include <linux/module.h>
300c7b178aSSam Ravnborg #include <linux/slab.h>
310c7b178aSSam Ravnborg 
32da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
33612a9aabSLinus Torvalds #include <drm/drm_crtc.h>
34612a9aabSLinus Torvalds #include <drm/drm_crtc_helper.h>
35255490f9SVille Syrjälä #include <drm/drm_edid.h>
363599dfa1SThomas Zimmermann #include <drm/drm_modeset_helper_vtables.h>
37d088b69fSThomas Zimmermann #include <drm/drm_simple_kms_helper.h>
380c7b178aSSam Ravnborg 
390c7b178aSSam Ravnborg #include "gma_display.h"
408695b612SAlan Cox #include "psb_drv.h"
418695b612SAlan Cox #include "psb_intel_drv.h"
428695b612SAlan Cox #include "psb_intel_reg.h"
438695b612SAlan Cox 
44e4570897SDaniel Vetter /**
45e4570897SDaniel Vetter  * struct i2c_algo_dp_aux_data - driver interface structure for i2c over dp
46e4570897SDaniel Vetter  * 				 aux algorithm
47e4570897SDaniel Vetter  * @running: set by the algo indicating whether an i2c is ongoing or whether
48e4570897SDaniel Vetter  * 	     the i2c bus is quiescent
49e4570897SDaniel Vetter  * @address: i2c target address for the currently ongoing transfer
50e4570897SDaniel Vetter  * @aux_ch: driver callback to transfer a single byte of the i2c payload
51e4570897SDaniel Vetter  */
52e4570897SDaniel Vetter struct i2c_algo_dp_aux_data {
53e4570897SDaniel Vetter 	bool running;
54e4570897SDaniel Vetter 	u16 address;
55e4570897SDaniel Vetter 	int (*aux_ch) (struct i2c_adapter *adapter,
56e4570897SDaniel Vetter 		       int mode, uint8_t write_byte,
57e4570897SDaniel Vetter 		       uint8_t *read_byte);
58e4570897SDaniel Vetter };
59e4570897SDaniel Vetter 
60e4570897SDaniel Vetter /* Run a single AUX_CH I2C transaction, writing/reading data as necessary */
61e4570897SDaniel Vetter static int
i2c_algo_dp_aux_transaction(struct i2c_adapter * adapter,int mode,uint8_t write_byte,uint8_t * read_byte)62e4570897SDaniel Vetter i2c_algo_dp_aux_transaction(struct i2c_adapter *adapter, int mode,
63e4570897SDaniel Vetter 			    uint8_t write_byte, uint8_t *read_byte)
64e4570897SDaniel Vetter {
65e4570897SDaniel Vetter 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
66e4570897SDaniel Vetter 	int ret;
67e4570897SDaniel Vetter 
68e4570897SDaniel Vetter 	ret = (*algo_data->aux_ch)(adapter, mode,
69e4570897SDaniel Vetter 				   write_byte, read_byte);
70e4570897SDaniel Vetter 	return ret;
71e4570897SDaniel Vetter }
72e4570897SDaniel Vetter 
73e4570897SDaniel Vetter /*
74e4570897SDaniel Vetter  * I2C over AUX CH
75e4570897SDaniel Vetter  */
76e4570897SDaniel Vetter 
77e4570897SDaniel Vetter /*
78e4570897SDaniel Vetter  * Send the address. If the I2C link is running, this 'restarts'
79e4570897SDaniel Vetter  * the connection with the new address, this is used for doing
80e4570897SDaniel Vetter  * a write followed by a read (as needed for DDC)
81e4570897SDaniel Vetter  */
82e4570897SDaniel Vetter static int
i2c_algo_dp_aux_address(struct i2c_adapter * adapter,u16 address,bool reading)83e4570897SDaniel Vetter i2c_algo_dp_aux_address(struct i2c_adapter *adapter, u16 address, bool reading)
84e4570897SDaniel Vetter {
85e4570897SDaniel Vetter 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
86e4570897SDaniel Vetter 	int mode = MODE_I2C_START;
87e4570897SDaniel Vetter 
88e4570897SDaniel Vetter 	if (reading)
89e4570897SDaniel Vetter 		mode |= MODE_I2C_READ;
90e4570897SDaniel Vetter 	else
91e4570897SDaniel Vetter 		mode |= MODE_I2C_WRITE;
92e4570897SDaniel Vetter 	algo_data->address = address;
93e4570897SDaniel Vetter 	algo_data->running = true;
94e592dc32SMinghao Chi 	return i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
95e4570897SDaniel Vetter }
96e4570897SDaniel Vetter 
97e4570897SDaniel Vetter /*
98e4570897SDaniel Vetter  * Stop the I2C transaction. This closes out the link, sending
99e4570897SDaniel Vetter  * a bare address packet with the MOT bit turned off
100e4570897SDaniel Vetter  */
101e4570897SDaniel Vetter static void
i2c_algo_dp_aux_stop(struct i2c_adapter * adapter,bool reading)102e4570897SDaniel Vetter i2c_algo_dp_aux_stop(struct i2c_adapter *adapter, bool reading)
103e4570897SDaniel Vetter {
104e4570897SDaniel Vetter 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
105e4570897SDaniel Vetter 	int mode = MODE_I2C_STOP;
106e4570897SDaniel Vetter 
107e4570897SDaniel Vetter 	if (reading)
108e4570897SDaniel Vetter 		mode |= MODE_I2C_READ;
109e4570897SDaniel Vetter 	else
110e4570897SDaniel Vetter 		mode |= MODE_I2C_WRITE;
111e4570897SDaniel Vetter 	if (algo_data->running) {
112e4570897SDaniel Vetter 		(void) i2c_algo_dp_aux_transaction(adapter, mode, 0, NULL);
113e4570897SDaniel Vetter 		algo_data->running = false;
114e4570897SDaniel Vetter 	}
115e4570897SDaniel Vetter }
116e4570897SDaniel Vetter 
117e4570897SDaniel Vetter /*
118e4570897SDaniel Vetter  * Write a single byte to the current I2C address, the
119291f269aSJason Wang  * I2C link must be running or this returns -EIO
120e4570897SDaniel Vetter  */
121e4570897SDaniel Vetter static int
i2c_algo_dp_aux_put_byte(struct i2c_adapter * adapter,u8 byte)122e4570897SDaniel Vetter i2c_algo_dp_aux_put_byte(struct i2c_adapter *adapter, u8 byte)
123e4570897SDaniel Vetter {
124e4570897SDaniel Vetter 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
125e4570897SDaniel Vetter 
126e4570897SDaniel Vetter 	if (!algo_data->running)
127e4570897SDaniel Vetter 		return -EIO;
128e4570897SDaniel Vetter 
129e592dc32SMinghao Chi 	return i2c_algo_dp_aux_transaction(adapter, MODE_I2C_WRITE, byte, NULL);
130e4570897SDaniel Vetter }
131e4570897SDaniel Vetter 
132e4570897SDaniel Vetter /*
133e4570897SDaniel Vetter  * Read a single byte from the current I2C address, the
134e4570897SDaniel Vetter  * I2C link must be running or this returns -EIO
135e4570897SDaniel Vetter  */
136e4570897SDaniel Vetter static int
i2c_algo_dp_aux_get_byte(struct i2c_adapter * adapter,u8 * byte_ret)137e4570897SDaniel Vetter i2c_algo_dp_aux_get_byte(struct i2c_adapter *adapter, u8 *byte_ret)
138e4570897SDaniel Vetter {
139e4570897SDaniel Vetter 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
140e4570897SDaniel Vetter 
141e4570897SDaniel Vetter 	if (!algo_data->running)
142e4570897SDaniel Vetter 		return -EIO;
143e4570897SDaniel Vetter 
144e592dc32SMinghao Chi 	return i2c_algo_dp_aux_transaction(adapter, MODE_I2C_READ, 0, byte_ret);
145e4570897SDaniel Vetter }
146e4570897SDaniel Vetter 
147e4570897SDaniel Vetter static int
i2c_algo_dp_aux_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)148e4570897SDaniel Vetter i2c_algo_dp_aux_xfer(struct i2c_adapter *adapter,
149e4570897SDaniel Vetter 		     struct i2c_msg *msgs,
150e4570897SDaniel Vetter 		     int num)
151e4570897SDaniel Vetter {
152e4570897SDaniel Vetter 	int ret = 0;
153e4570897SDaniel Vetter 	bool reading = false;
154e4570897SDaniel Vetter 	int m;
155e4570897SDaniel Vetter 	int b;
156e4570897SDaniel Vetter 
157e4570897SDaniel Vetter 	for (m = 0; m < num; m++) {
158e4570897SDaniel Vetter 		u16 len = msgs[m].len;
159e4570897SDaniel Vetter 		u8 *buf = msgs[m].buf;
160e4570897SDaniel Vetter 		reading = (msgs[m].flags & I2C_M_RD) != 0;
161e4570897SDaniel Vetter 		ret = i2c_algo_dp_aux_address(adapter, msgs[m].addr, reading);
162e4570897SDaniel Vetter 		if (ret < 0)
163e4570897SDaniel Vetter 			break;
164e4570897SDaniel Vetter 		if (reading) {
165e4570897SDaniel Vetter 			for (b = 0; b < len; b++) {
166e4570897SDaniel Vetter 				ret = i2c_algo_dp_aux_get_byte(adapter, &buf[b]);
167e4570897SDaniel Vetter 				if (ret < 0)
168e4570897SDaniel Vetter 					break;
169e4570897SDaniel Vetter 			}
170e4570897SDaniel Vetter 		} else {
171e4570897SDaniel Vetter 			for (b = 0; b < len; b++) {
172e4570897SDaniel Vetter 				ret = i2c_algo_dp_aux_put_byte(adapter, buf[b]);
173e4570897SDaniel Vetter 				if (ret < 0)
174e4570897SDaniel Vetter 					break;
175e4570897SDaniel Vetter 			}
176e4570897SDaniel Vetter 		}
177e4570897SDaniel Vetter 		if (ret < 0)
178e4570897SDaniel Vetter 			break;
179e4570897SDaniel Vetter 	}
180e4570897SDaniel Vetter 	if (ret >= 0)
181e4570897SDaniel Vetter 		ret = num;
182e4570897SDaniel Vetter 	i2c_algo_dp_aux_stop(adapter, reading);
183e4570897SDaniel Vetter 	DRM_DEBUG_KMS("dp_aux_xfer return %d\n", ret);
184e4570897SDaniel Vetter 	return ret;
185e4570897SDaniel Vetter }
186e4570897SDaniel Vetter 
187e4570897SDaniel Vetter static u32
i2c_algo_dp_aux_functionality(struct i2c_adapter * adapter)188e4570897SDaniel Vetter i2c_algo_dp_aux_functionality(struct i2c_adapter *adapter)
189e4570897SDaniel Vetter {
190e4570897SDaniel Vetter 	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
191e4570897SDaniel Vetter 	       I2C_FUNC_SMBUS_READ_BLOCK_DATA |
192e4570897SDaniel Vetter 	       I2C_FUNC_SMBUS_BLOCK_PROC_CALL |
193e4570897SDaniel Vetter 	       I2C_FUNC_10BIT_ADDR;
194e4570897SDaniel Vetter }
195e4570897SDaniel Vetter 
196e4570897SDaniel Vetter static const struct i2c_algorithm i2c_dp_aux_algo = {
197e4570897SDaniel Vetter 	.master_xfer	= i2c_algo_dp_aux_xfer,
198e4570897SDaniel Vetter 	.functionality	= i2c_algo_dp_aux_functionality,
199e4570897SDaniel Vetter };
200e4570897SDaniel Vetter 
201e4570897SDaniel Vetter static void
i2c_dp_aux_reset_bus(struct i2c_adapter * adapter)202e4570897SDaniel Vetter i2c_dp_aux_reset_bus(struct i2c_adapter *adapter)
203e4570897SDaniel Vetter {
204e4570897SDaniel Vetter 	(void) i2c_algo_dp_aux_address(adapter, 0, false);
205e4570897SDaniel Vetter 	(void) i2c_algo_dp_aux_stop(adapter, false);
206e4570897SDaniel Vetter }
207e4570897SDaniel Vetter 
208e4570897SDaniel Vetter static int
i2c_dp_aux_prepare_bus(struct i2c_adapter * adapter)209e4570897SDaniel Vetter i2c_dp_aux_prepare_bus(struct i2c_adapter *adapter)
210e4570897SDaniel Vetter {
211e4570897SDaniel Vetter 	adapter->algo = &i2c_dp_aux_algo;
212e4570897SDaniel Vetter 	adapter->retries = 3;
213e4570897SDaniel Vetter 	i2c_dp_aux_reset_bus(adapter);
214e4570897SDaniel Vetter 	return 0;
215e4570897SDaniel Vetter }
216e4570897SDaniel Vetter 
217e4570897SDaniel Vetter /*
218e4570897SDaniel Vetter  * FIXME: This is the old dp aux helper, gma500 is the last driver that needs to
219e4570897SDaniel Vetter  * be ported over to the new helper code in drm_dp_helper.c like i915 or radeon.
220e4570897SDaniel Vetter  */
221166c5a6eSLinus Torvalds static int
i2c_dp_aux_add_bus(struct i2c_adapter * adapter)222e4570897SDaniel Vetter i2c_dp_aux_add_bus(struct i2c_adapter *adapter)
223e4570897SDaniel Vetter {
224e4570897SDaniel Vetter 	int error;
225e4570897SDaniel Vetter 
226e4570897SDaniel Vetter 	error = i2c_dp_aux_prepare_bus(adapter);
227e4570897SDaniel Vetter 	if (error)
228e4570897SDaniel Vetter 		return error;
229e4570897SDaniel Vetter 	error = i2c_add_adapter(adapter);
230e4570897SDaniel Vetter 	return error;
231e4570897SDaniel Vetter }
232e4570897SDaniel Vetter 
233d112a816SZhao Yakui #define _wait_for(COND, MS, W) ({ \
234d112a816SZhao Yakui         unsigned long timeout__ = jiffies + msecs_to_jiffies(MS);       \
235d112a816SZhao Yakui         int ret__ = 0;                                                  \
236d112a816SZhao Yakui         while (! (COND)) {                                              \
237d112a816SZhao Yakui                 if (time_after(jiffies, timeout__)) {                   \
238d112a816SZhao Yakui                         ret__ = -ETIMEDOUT;                             \
239d112a816SZhao Yakui                         break;                                          \
240d112a816SZhao Yakui                 }                                                       \
241d112a816SZhao Yakui                 if (W && !in_dbg_master()) msleep(W);                   \
242d112a816SZhao Yakui         }                                                               \
243d112a816SZhao Yakui         ret__;                                                          \
244d112a816SZhao Yakui })
245d112a816SZhao Yakui 
246d112a816SZhao Yakui #define wait_for(COND, MS) _wait_for(COND, MS, 1)
2478695b612SAlan Cox 
2488695b612SAlan Cox #define DP_LINK_CHECK_TIMEOUT	(10 * 1000)
2498695b612SAlan Cox 
2508695b612SAlan Cox #define DP_LINK_CONFIGURATION_SIZE	9
2518695b612SAlan Cox 
2528695b612SAlan Cox #define CDV_FAST_LINK_TRAIN	1
2538695b612SAlan Cox 
25437e7b184SAlan Cox struct cdv_intel_dp {
2558695b612SAlan Cox 	uint32_t output_reg;
2568695b612SAlan Cox 	uint32_t DP;
2578695b612SAlan Cox 	uint8_t  link_configuration[DP_LINK_CONFIGURATION_SIZE];
2588695b612SAlan Cox 	bool has_audio;
2598695b612SAlan Cox 	int force_audio;
2608695b612SAlan Cox 	uint32_t color_range;
2618695b612SAlan Cox 	uint8_t link_bw;
2628695b612SAlan Cox 	uint8_t lane_count;
2638695b612SAlan Cox 	uint8_t dpcd[4];
264367e4408SPatrik Jakobsson 	struct gma_encoder *encoder;
2658695b612SAlan Cox 	struct i2c_adapter adapter;
2668695b612SAlan Cox 	struct i2c_algo_dp_aux_data algo;
2678695b612SAlan Cox 	uint8_t	train_set[4];
2688695b612SAlan Cox 	uint8_t link_status[DP_LINK_STATUS_SIZE];
269d112a816SZhao Yakui 	int panel_power_up_delay;
270d112a816SZhao Yakui 	int panel_power_down_delay;
271d112a816SZhao Yakui 	int panel_power_cycle_delay;
272d112a816SZhao Yakui 	int backlight_on_delay;
273d112a816SZhao Yakui 	int backlight_off_delay;
274d112a816SZhao Yakui 	struct drm_display_mode *panel_fixed_mode;  /* for eDP */
275d112a816SZhao Yakui 	bool panel_on;
2768695b612SAlan Cox };
2778695b612SAlan Cox 
2788695b612SAlan Cox struct ddi_regoff {
2798695b612SAlan Cox 	uint32_t	PreEmph1;
2808695b612SAlan Cox 	uint32_t	PreEmph2;
2818695b612SAlan Cox 	uint32_t	VSwing1;
2828695b612SAlan Cox 	uint32_t	VSwing2;
2838695b612SAlan Cox 	uint32_t	VSwing3;
2848695b612SAlan Cox 	uint32_t	VSwing4;
2858695b612SAlan Cox 	uint32_t	VSwing5;
2868695b612SAlan Cox };
2878695b612SAlan Cox 
2888695b612SAlan Cox static struct ddi_regoff ddi_DP_train_table[] = {
2898695b612SAlan Cox 	{.PreEmph1 = 0x812c, .PreEmph2 = 0x8124, .VSwing1 = 0x8154,
2908695b612SAlan Cox 	.VSwing2 = 0x8148, .VSwing3 = 0x814C, .VSwing4 = 0x8150,
2918695b612SAlan Cox 	.VSwing5 = 0x8158,},
2928695b612SAlan Cox 	{.PreEmph1 = 0x822c, .PreEmph2 = 0x8224, .VSwing1 = 0x8254,
2938695b612SAlan Cox 	.VSwing2 = 0x8248, .VSwing3 = 0x824C, .VSwing4 = 0x8250,
2948695b612SAlan Cox 	.VSwing5 = 0x8258,},
2958695b612SAlan Cox };
2968695b612SAlan Cox 
2978695b612SAlan Cox static uint32_t dp_vswing_premph_table[] = {
2988695b612SAlan Cox         0x55338954,	0x4000,
2998695b612SAlan Cox         0x554d8954,	0x2000,
3008695b612SAlan Cox         0x55668954,	0,
3018695b612SAlan Cox         0x559ac0d4,	0x6000,
3028695b612SAlan Cox };
3038695b612SAlan Cox /**
3048695b612SAlan Cox  * is_edp - is the given port attached to an eDP panel (either CPU or PCH)
30577ee9c56SLee Jones  * @encoder: GMA encoder struct
3068695b612SAlan Cox  *
3078695b612SAlan Cox  * If a CPU or PCH DP output is attached to an eDP panel, this function
3088695b612SAlan Cox  * will return true, and false otherwise.
3098695b612SAlan Cox  */
is_edp(struct gma_encoder * encoder)310367e4408SPatrik Jakobsson static bool is_edp(struct gma_encoder *encoder)
3118695b612SAlan Cox {
31237e7b184SAlan Cox 	return encoder->type == INTEL_OUTPUT_EDP;
3138695b612SAlan Cox }
3148695b612SAlan Cox 
3158695b612SAlan Cox 
316367e4408SPatrik Jakobsson static void cdv_intel_dp_start_link_train(struct gma_encoder *encoder);
317367e4408SPatrik Jakobsson static void cdv_intel_dp_complete_link_train(struct gma_encoder *encoder);
318367e4408SPatrik Jakobsson static void cdv_intel_dp_link_down(struct gma_encoder *encoder);
3198695b612SAlan Cox 
3208695b612SAlan Cox static int
cdv_intel_dp_max_lane_count(struct gma_encoder * encoder)321367e4408SPatrik Jakobsson cdv_intel_dp_max_lane_count(struct gma_encoder *encoder)
3228695b612SAlan Cox {
32337e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
3248695b612SAlan Cox 	int max_lane_count = 4;
3258695b612SAlan Cox 
3268695b612SAlan Cox 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11) {
3278695b612SAlan Cox 		max_lane_count = intel_dp->dpcd[DP_MAX_LANE_COUNT] & 0x1f;
3288695b612SAlan Cox 		switch (max_lane_count) {
3298695b612SAlan Cox 		case 1: case 2: case 4:
3308695b612SAlan Cox 			break;
3318695b612SAlan Cox 		default:
3328695b612SAlan Cox 			max_lane_count = 4;
3338695b612SAlan Cox 		}
3348695b612SAlan Cox 	}
3358695b612SAlan Cox 	return max_lane_count;
3368695b612SAlan Cox }
3378695b612SAlan Cox 
3388695b612SAlan Cox static int
cdv_intel_dp_max_link_bw(struct gma_encoder * encoder)339367e4408SPatrik Jakobsson cdv_intel_dp_max_link_bw(struct gma_encoder *encoder)
3408695b612SAlan Cox {
34137e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
3428695b612SAlan Cox 	int max_link_bw = intel_dp->dpcd[DP_MAX_LINK_RATE];
3438695b612SAlan Cox 
3448695b612SAlan Cox 	switch (max_link_bw) {
3458695b612SAlan Cox 	case DP_LINK_BW_1_62:
3468695b612SAlan Cox 	case DP_LINK_BW_2_7:
3478695b612SAlan Cox 		break;
3488695b612SAlan Cox 	default:
3498695b612SAlan Cox 		max_link_bw = DP_LINK_BW_1_62;
3508695b612SAlan Cox 		break;
3518695b612SAlan Cox 	}
3528695b612SAlan Cox 	return max_link_bw;
3538695b612SAlan Cox }
3548695b612SAlan Cox 
3558695b612SAlan Cox static int
cdv_intel_dp_link_clock(uint8_t link_bw)35637e7b184SAlan Cox cdv_intel_dp_link_clock(uint8_t link_bw)
3578695b612SAlan Cox {
3588695b612SAlan Cox 	if (link_bw == DP_LINK_BW_2_7)
3598695b612SAlan Cox 		return 270000;
3608695b612SAlan Cox 	else
3618695b612SAlan Cox 		return 162000;
3628695b612SAlan Cox }
3638695b612SAlan Cox 
3648695b612SAlan Cox static int
cdv_intel_dp_link_required(int pixel_clock,int bpp)36537e7b184SAlan Cox cdv_intel_dp_link_required(int pixel_clock, int bpp)
3668695b612SAlan Cox {
3678695b612SAlan Cox 	return (pixel_clock * bpp + 7) / 8;
3688695b612SAlan Cox }
3698695b612SAlan Cox 
3708695b612SAlan Cox static int
cdv_intel_dp_max_data_rate(int max_link_clock,int max_lanes)37137e7b184SAlan Cox cdv_intel_dp_max_data_rate(int max_link_clock, int max_lanes)
3728695b612SAlan Cox {
3738695b612SAlan Cox 	return (max_link_clock * max_lanes * 19) / 20;
3748695b612SAlan Cox }
3758695b612SAlan Cox 
cdv_intel_edp_panel_vdd_on(struct gma_encoder * intel_encoder)376367e4408SPatrik Jakobsson static void cdv_intel_edp_panel_vdd_on(struct gma_encoder *intel_encoder)
377d112a816SZhao Yakui {
378d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
379d112a816SZhao Yakui 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
380d112a816SZhao Yakui 	u32 pp;
381d112a816SZhao Yakui 
382d112a816SZhao Yakui 	if (intel_dp->panel_on) {
383d112a816SZhao Yakui 		DRM_DEBUG_KMS("Skip VDD on because of panel on\n");
384d112a816SZhao Yakui 		return;
385d112a816SZhao Yakui 	}
386d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
387d112a816SZhao Yakui 
388d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
389d112a816SZhao Yakui 
390d112a816SZhao Yakui 	pp |= EDP_FORCE_VDD;
391d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
392d112a816SZhao Yakui 	REG_READ(PP_CONTROL);
393d112a816SZhao Yakui 	msleep(intel_dp->panel_power_up_delay);
394d112a816SZhao Yakui }
395d112a816SZhao Yakui 
cdv_intel_edp_panel_vdd_off(struct gma_encoder * intel_encoder)396367e4408SPatrik Jakobsson static void cdv_intel_edp_panel_vdd_off(struct gma_encoder *intel_encoder)
397d112a816SZhao Yakui {
398d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
399d112a816SZhao Yakui 	u32 pp;
400d112a816SZhao Yakui 
401d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
402d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
403d112a816SZhao Yakui 
404d112a816SZhao Yakui 	pp &= ~EDP_FORCE_VDD;
405d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
406d112a816SZhao Yakui 	REG_READ(PP_CONTROL);
407d112a816SZhao Yakui 
408d112a816SZhao Yakui }
409d112a816SZhao Yakui 
410d112a816SZhao Yakui /* Returns true if the panel was already on when called */
cdv_intel_edp_panel_on(struct gma_encoder * intel_encoder)411367e4408SPatrik Jakobsson static bool cdv_intel_edp_panel_on(struct gma_encoder *intel_encoder)
412d112a816SZhao Yakui {
413d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
414d112a816SZhao Yakui 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
415d112a816SZhao Yakui 	u32 pp, idle_on_mask = PP_ON | PP_SEQUENCE_NONE;
416d112a816SZhao Yakui 
417d112a816SZhao Yakui 	if (intel_dp->panel_on)
418d112a816SZhao Yakui 		return true;
419d112a816SZhao Yakui 
420d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
421d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
422d112a816SZhao Yakui 	pp &= ~PANEL_UNLOCK_MASK;
423d112a816SZhao Yakui 
424d112a816SZhao Yakui 	pp |= (PANEL_UNLOCK_REGS | POWER_TARGET_ON);
425d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
426d112a816SZhao Yakui 	REG_READ(PP_CONTROL);
427d112a816SZhao Yakui 
428d112a816SZhao Yakui 	if (wait_for(((REG_READ(PP_STATUS) & idle_on_mask) == idle_on_mask), 1000)) {
429d112a816SZhao Yakui 		DRM_DEBUG_KMS("Error in Powering up eDP panel, status %x\n", REG_READ(PP_STATUS));
430d112a816SZhao Yakui 		intel_dp->panel_on = false;
431d112a816SZhao Yakui 	} else
432d112a816SZhao Yakui 		intel_dp->panel_on = true;
433d112a816SZhao Yakui 	msleep(intel_dp->panel_power_up_delay);
434d112a816SZhao Yakui 
435d112a816SZhao Yakui 	return false;
436d112a816SZhao Yakui }
437d112a816SZhao Yakui 
cdv_intel_edp_panel_off(struct gma_encoder * intel_encoder)438367e4408SPatrik Jakobsson static void cdv_intel_edp_panel_off (struct gma_encoder *intel_encoder)
439d112a816SZhao Yakui {
440d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
441d112a816SZhao Yakui 	u32 pp, idle_off_mask = PP_ON ;
442d112a816SZhao Yakui 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
443d112a816SZhao Yakui 
444d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
445d112a816SZhao Yakui 
446d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
447d112a816SZhao Yakui 
448d112a816SZhao Yakui 	if ((pp & POWER_TARGET_ON) == 0)
449d112a816SZhao Yakui 		return;
450d112a816SZhao Yakui 
451d112a816SZhao Yakui 	intel_dp->panel_on = false;
452d112a816SZhao Yakui 	pp &= ~PANEL_UNLOCK_MASK;
453d112a816SZhao Yakui 	/* ILK workaround: disable reset around power sequence */
454d112a816SZhao Yakui 
455d112a816SZhao Yakui 	pp &= ~POWER_TARGET_ON;
456d112a816SZhao Yakui 	pp &= ~EDP_FORCE_VDD;
457d112a816SZhao Yakui 	pp &= ~EDP_BLC_ENABLE;
458d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
459d112a816SZhao Yakui 	REG_READ(PP_CONTROL);
460d112a816SZhao Yakui 	DRM_DEBUG_KMS("PP_STATUS %x\n", REG_READ(PP_STATUS));
461d112a816SZhao Yakui 
462d112a816SZhao Yakui 	if (wait_for((REG_READ(PP_STATUS) & idle_off_mask) == 0, 1000)) {
463d112a816SZhao Yakui 		DRM_DEBUG_KMS("Error in turning off Panel\n");
464d112a816SZhao Yakui 	}
465d112a816SZhao Yakui 
466d112a816SZhao Yakui 	msleep(intel_dp->panel_power_cycle_delay);
467d112a816SZhao Yakui 	DRM_DEBUG_KMS("Over\n");
468d112a816SZhao Yakui }
469d112a816SZhao Yakui 
cdv_intel_edp_backlight_on(struct gma_encoder * intel_encoder)470367e4408SPatrik Jakobsson static void cdv_intel_edp_backlight_on (struct gma_encoder *intel_encoder)
471d112a816SZhao Yakui {
472d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
473d112a816SZhao Yakui 	u32 pp;
474d112a816SZhao Yakui 
475d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
476d112a816SZhao Yakui 	/*
477d112a816SZhao Yakui 	 * If we enable the backlight right away following a panel power
478d112a816SZhao Yakui 	 * on, we may see slight flicker as the panel syncs with the eDP
479d112a816SZhao Yakui 	 * link.  So delay a bit to make sure the image is solid before
480d112a816SZhao Yakui 	 * allowing it to appear.
481d112a816SZhao Yakui 	 */
482d112a816SZhao Yakui 	msleep(300);
483d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
484d112a816SZhao Yakui 
485d112a816SZhao Yakui 	pp |= EDP_BLC_ENABLE;
486d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
487d112a816SZhao Yakui 	gma_backlight_enable(dev);
488d112a816SZhao Yakui }
489d112a816SZhao Yakui 
cdv_intel_edp_backlight_off(struct gma_encoder * intel_encoder)490367e4408SPatrik Jakobsson static void cdv_intel_edp_backlight_off (struct gma_encoder *intel_encoder)
491d112a816SZhao Yakui {
492d112a816SZhao Yakui 	struct drm_device *dev = intel_encoder->base.dev;
493d112a816SZhao Yakui 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
494d112a816SZhao Yakui 	u32 pp;
495d112a816SZhao Yakui 
496d112a816SZhao Yakui 	DRM_DEBUG_KMS("\n");
497d112a816SZhao Yakui 	gma_backlight_disable(dev);
498d112a816SZhao Yakui 	msleep(10);
499d112a816SZhao Yakui 	pp = REG_READ(PP_CONTROL);
500d112a816SZhao Yakui 
501d112a816SZhao Yakui 	pp &= ~EDP_BLC_ENABLE;
502d112a816SZhao Yakui 	REG_WRITE(PP_CONTROL, pp);
503d112a816SZhao Yakui 	msleep(intel_dp->backlight_off_delay);
504d112a816SZhao Yakui }
505d112a816SZhao Yakui 
50667772782SLuc Van Oostenryck static enum drm_mode_status
cdv_intel_dp_mode_valid(struct drm_connector * connector,const struct drm_display_mode * mode)50737e7b184SAlan Cox cdv_intel_dp_mode_valid(struct drm_connector *connector,
50826d6fd81SDmitry Baryshkov 		    const struct drm_display_mode *mode)
5098695b612SAlan Cox {
510367e4408SPatrik Jakobsson 	struct gma_encoder *encoder = gma_attached_encoder(connector);
511d112a816SZhao Yakui 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
51237e7b184SAlan Cox 	int max_link_clock = cdv_intel_dp_link_clock(cdv_intel_dp_max_link_bw(encoder));
51337e7b184SAlan Cox 	int max_lanes = cdv_intel_dp_max_lane_count(encoder);
514f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(connector->dev);
5158695b612SAlan Cox 
516d112a816SZhao Yakui 	if (is_edp(encoder) && intel_dp->panel_fixed_mode) {
517d112a816SZhao Yakui 		if (mode->hdisplay > intel_dp->panel_fixed_mode->hdisplay)
5188695b612SAlan Cox 			return MODE_PANEL;
519d112a816SZhao Yakui 		if (mode->vdisplay > intel_dp->panel_fixed_mode->vdisplay)
5208695b612SAlan Cox 			return MODE_PANEL;
5218695b612SAlan Cox 	}
5228695b612SAlan Cox 
5238695b612SAlan Cox 	/* only refuse the mode on non eDP since we have seen some weird eDP panels
5248695b612SAlan Cox 	   which are outside spec tolerances but somehow work by magic */
52537e7b184SAlan Cox 	if (!is_edp(encoder) &&
526d112a816SZhao Yakui 	    (cdv_intel_dp_link_required(mode->clock, dev_priv->edp.bpp)
52737e7b184SAlan Cox 	     > cdv_intel_dp_max_data_rate(max_link_clock, max_lanes)))
5288695b612SAlan Cox 		return MODE_CLOCK_HIGH;
5298695b612SAlan Cox 
530d112a816SZhao Yakui 	if (is_edp(encoder)) {
531d112a816SZhao Yakui 	    if (cdv_intel_dp_link_required(mode->clock, 24)
532d112a816SZhao Yakui 	     	> cdv_intel_dp_max_data_rate(max_link_clock, max_lanes))
533d112a816SZhao Yakui 		return MODE_CLOCK_HIGH;
534d112a816SZhao Yakui 
535d112a816SZhao Yakui 	}
5368695b612SAlan Cox 	if (mode->clock < 10000)
5378695b612SAlan Cox 		return MODE_CLOCK_LOW;
5388695b612SAlan Cox 
5398695b612SAlan Cox 	return MODE_OK;
5408695b612SAlan Cox }
5418695b612SAlan Cox 
5428695b612SAlan Cox static uint32_t
pack_aux(uint8_t * src,int src_bytes)5438695b612SAlan Cox pack_aux(uint8_t *src, int src_bytes)
5448695b612SAlan Cox {
5458695b612SAlan Cox 	int	i;
5468695b612SAlan Cox 	uint32_t v = 0;
5478695b612SAlan Cox 
5488695b612SAlan Cox 	if (src_bytes > 4)
5498695b612SAlan Cox 		src_bytes = 4;
5508695b612SAlan Cox 	for (i = 0; i < src_bytes; i++)
5518695b612SAlan Cox 		v |= ((uint32_t) src[i]) << ((3-i) * 8);
5528695b612SAlan Cox 	return v;
5538695b612SAlan Cox }
5548695b612SAlan Cox 
5558695b612SAlan Cox static void
unpack_aux(uint32_t src,uint8_t * dst,int dst_bytes)5568695b612SAlan Cox unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
5578695b612SAlan Cox {
5588695b612SAlan Cox 	int i;
5598695b612SAlan Cox 	if (dst_bytes > 4)
5608695b612SAlan Cox 		dst_bytes = 4;
5618695b612SAlan Cox 	for (i = 0; i < dst_bytes; i++)
5628695b612SAlan Cox 		dst[i] = src >> ((3-i) * 8);
5638695b612SAlan Cox }
5648695b612SAlan Cox 
5658695b612SAlan Cox static int
cdv_intel_dp_aux_ch(struct gma_encoder * encoder,uint8_t * send,int send_bytes,uint8_t * recv,int recv_size)566367e4408SPatrik Jakobsson cdv_intel_dp_aux_ch(struct gma_encoder *encoder,
5678695b612SAlan Cox 		uint8_t *send, int send_bytes,
5688695b612SAlan Cox 		uint8_t *recv, int recv_size)
5698695b612SAlan Cox {
57037e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
5718695b612SAlan Cox 	uint32_t output_reg = intel_dp->output_reg;
57237e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
5738695b612SAlan Cox 	uint32_t ch_ctl = output_reg + 0x10;
5748695b612SAlan Cox 	uint32_t ch_data = ch_ctl + 4;
5758695b612SAlan Cox 	int i;
5768695b612SAlan Cox 	int recv_bytes;
5778695b612SAlan Cox 	uint32_t status;
5788695b612SAlan Cox 	uint32_t aux_clock_divider;
5798695b612SAlan Cox 	int try, precharge;
5808695b612SAlan Cox 
5818695b612SAlan Cox 	/* The clock divider is based off the hrawclk,
5828695b612SAlan Cox 	 * and would like to run at 2MHz. So, take the
5838695b612SAlan Cox 	 * hrawclk value and divide by 2 and use that
5848695b612SAlan Cox 	 * On CDV platform it uses 200MHz as hrawclk.
5858695b612SAlan Cox 	 *
5868695b612SAlan Cox 	 */
5878695b612SAlan Cox 	aux_clock_divider = 200 / 2;
5888695b612SAlan Cox 
5898695b612SAlan Cox 	precharge = 4;
590d112a816SZhao Yakui 	if (is_edp(encoder))
591d112a816SZhao Yakui 		precharge = 10;
5928695b612SAlan Cox 
5938695b612SAlan Cox 	if (REG_READ(ch_ctl) & DP_AUX_CH_CTL_SEND_BUSY) {
5948695b612SAlan Cox 		DRM_ERROR("dp_aux_ch not started status 0x%08x\n",
5958695b612SAlan Cox 			  REG_READ(ch_ctl));
5968695b612SAlan Cox 		return -EBUSY;
5978695b612SAlan Cox 	}
5988695b612SAlan Cox 
5998695b612SAlan Cox 	/* Must try at least 3 times according to DP spec */
6008695b612SAlan Cox 	for (try = 0; try < 5; try++) {
6018695b612SAlan Cox 		/* Load the send data into the aux channel data registers */
6028695b612SAlan Cox 		for (i = 0; i < send_bytes; i += 4)
6038695b612SAlan Cox 			REG_WRITE(ch_data + i,
6048695b612SAlan Cox 				   pack_aux(send + i, send_bytes - i));
6058695b612SAlan Cox 
6068695b612SAlan Cox 		/* Send the command and wait for it to complete */
6078695b612SAlan Cox 		REG_WRITE(ch_ctl,
6088695b612SAlan Cox 			   DP_AUX_CH_CTL_SEND_BUSY |
6098695b612SAlan Cox 			   DP_AUX_CH_CTL_TIME_OUT_400us |
6108695b612SAlan Cox 			   (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
6118695b612SAlan Cox 			   (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
6128695b612SAlan Cox 			   (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
6138695b612SAlan Cox 			   DP_AUX_CH_CTL_DONE |
6148695b612SAlan Cox 			   DP_AUX_CH_CTL_TIME_OUT_ERROR |
6158695b612SAlan Cox 			   DP_AUX_CH_CTL_RECEIVE_ERROR);
6168695b612SAlan Cox 		for (;;) {
6178695b612SAlan Cox 			status = REG_READ(ch_ctl);
6188695b612SAlan Cox 			if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
6198695b612SAlan Cox 				break;
6208695b612SAlan Cox 			udelay(100);
6218695b612SAlan Cox 		}
6228695b612SAlan Cox 
6238695b612SAlan Cox 		/* Clear done status and any errors */
6248695b612SAlan Cox 		REG_WRITE(ch_ctl,
6258695b612SAlan Cox 			   status |
6268695b612SAlan Cox 			   DP_AUX_CH_CTL_DONE |
6278695b612SAlan Cox 			   DP_AUX_CH_CTL_TIME_OUT_ERROR |
6288695b612SAlan Cox 			   DP_AUX_CH_CTL_RECEIVE_ERROR);
6298695b612SAlan Cox 		if (status & DP_AUX_CH_CTL_DONE)
6308695b612SAlan Cox 			break;
6318695b612SAlan Cox 	}
6328695b612SAlan Cox 
6338695b612SAlan Cox 	if ((status & DP_AUX_CH_CTL_DONE) == 0) {
6348695b612SAlan Cox 		DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
6358695b612SAlan Cox 		return -EBUSY;
6368695b612SAlan Cox 	}
6378695b612SAlan Cox 
6388695b612SAlan Cox 	/* Check for timeout or receive error.
6398695b612SAlan Cox 	 * Timeouts occur when the sink is not connected
6408695b612SAlan Cox 	 */
6418695b612SAlan Cox 	if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
6428695b612SAlan Cox 		DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
6438695b612SAlan Cox 		return -EIO;
6448695b612SAlan Cox 	}
6458695b612SAlan Cox 
6468695b612SAlan Cox 	/* Timeouts occur when the device isn't connected, so they're
6478695b612SAlan Cox 	 * "normal" -- don't fill the kernel log with these */
6488695b612SAlan Cox 	if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
6498695b612SAlan Cox 		DRM_DEBUG_KMS("dp_aux_ch timeout status 0x%08x\n", status);
6508695b612SAlan Cox 		return -ETIMEDOUT;
6518695b612SAlan Cox 	}
6528695b612SAlan Cox 
6538695b612SAlan Cox 	/* Unload any bytes sent back from the other side */
6548695b612SAlan Cox 	recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
6558695b612SAlan Cox 		      DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
6568695b612SAlan Cox 	if (recv_bytes > recv_size)
6578695b612SAlan Cox 		recv_bytes = recv_size;
6588695b612SAlan Cox 
6598695b612SAlan Cox 	for (i = 0; i < recv_bytes; i += 4)
6608695b612SAlan Cox 		unpack_aux(REG_READ(ch_data + i),
6618695b612SAlan Cox 			   recv + i, recv_bytes - i);
6628695b612SAlan Cox 
6638695b612SAlan Cox 	return recv_bytes;
6648695b612SAlan Cox }
6658695b612SAlan Cox 
6668695b612SAlan Cox /* Write data to the aux channel in native mode */
6678695b612SAlan Cox static int
cdv_intel_dp_aux_native_write(struct gma_encoder * encoder,uint16_t address,uint8_t * send,int send_bytes)668367e4408SPatrik Jakobsson cdv_intel_dp_aux_native_write(struct gma_encoder *encoder,
6698695b612SAlan Cox 			  uint16_t address, uint8_t *send, int send_bytes)
6708695b612SAlan Cox {
6718695b612SAlan Cox 	int ret;
6728695b612SAlan Cox 	uint8_t	msg[20];
6738695b612SAlan Cox 	int msg_bytes;
6748695b612SAlan Cox 	uint8_t	ack;
6758695b612SAlan Cox 
6768695b612SAlan Cox 	if (send_bytes > 16)
6778695b612SAlan Cox 		return -1;
6786b27f7f0SThierry Reding 	msg[0] = DP_AUX_NATIVE_WRITE << 4;
6798695b612SAlan Cox 	msg[1] = address >> 8;
6808695b612SAlan Cox 	msg[2] = address & 0xff;
6818695b612SAlan Cox 	msg[3] = send_bytes - 1;
6828695b612SAlan Cox 	memcpy(&msg[4], send, send_bytes);
6838695b612SAlan Cox 	msg_bytes = send_bytes + 4;
6848695b612SAlan Cox 	for (;;) {
68537e7b184SAlan Cox 		ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes, &ack, 1);
6868695b612SAlan Cox 		if (ret < 0)
6878695b612SAlan Cox 			return ret;
6886b27f7f0SThierry Reding 		ack >>= 4;
6896b27f7f0SThierry Reding 		if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_ACK)
6908695b612SAlan Cox 			break;
6916b27f7f0SThierry Reding 		else if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_DEFER)
6928695b612SAlan Cox 			udelay(100);
6938695b612SAlan Cox 		else
6948695b612SAlan Cox 			return -EIO;
6958695b612SAlan Cox 	}
6968695b612SAlan Cox 	return send_bytes;
6978695b612SAlan Cox }
6988695b612SAlan Cox 
6998695b612SAlan Cox /* Write a single byte to the aux channel in native mode */
7008695b612SAlan Cox static int
cdv_intel_dp_aux_native_write_1(struct gma_encoder * encoder,uint16_t address,uint8_t byte)701367e4408SPatrik Jakobsson cdv_intel_dp_aux_native_write_1(struct gma_encoder *encoder,
7028695b612SAlan Cox 			    uint16_t address, uint8_t byte)
7038695b612SAlan Cox {
70437e7b184SAlan Cox 	return cdv_intel_dp_aux_native_write(encoder, address, &byte, 1);
7058695b612SAlan Cox }
7068695b612SAlan Cox 
7078695b612SAlan Cox /* read bytes from a native aux channel */
7088695b612SAlan Cox static int
cdv_intel_dp_aux_native_read(struct gma_encoder * encoder,uint16_t address,uint8_t * recv,int recv_bytes)709367e4408SPatrik Jakobsson cdv_intel_dp_aux_native_read(struct gma_encoder *encoder,
7108695b612SAlan Cox 			 uint16_t address, uint8_t *recv, int recv_bytes)
7118695b612SAlan Cox {
7128695b612SAlan Cox 	uint8_t msg[4];
7138695b612SAlan Cox 	int msg_bytes;
7148695b612SAlan Cox 	uint8_t reply[20];
7158695b612SAlan Cox 	int reply_bytes;
7168695b612SAlan Cox 	uint8_t ack;
7178695b612SAlan Cox 	int ret;
7188695b612SAlan Cox 
7196b27f7f0SThierry Reding 	msg[0] = DP_AUX_NATIVE_READ << 4;
7208695b612SAlan Cox 	msg[1] = address >> 8;
7218695b612SAlan Cox 	msg[2] = address & 0xff;
7228695b612SAlan Cox 	msg[3] = recv_bytes - 1;
7238695b612SAlan Cox 
7248695b612SAlan Cox 	msg_bytes = 4;
7258695b612SAlan Cox 	reply_bytes = recv_bytes + 1;
7268695b612SAlan Cox 
7278695b612SAlan Cox 	for (;;) {
72837e7b184SAlan Cox 		ret = cdv_intel_dp_aux_ch(encoder, msg, msg_bytes,
7298695b612SAlan Cox 				      reply, reply_bytes);
7308695b612SAlan Cox 		if (ret == 0)
7318695b612SAlan Cox 			return -EPROTO;
7328695b612SAlan Cox 		if (ret < 0)
7338695b612SAlan Cox 			return ret;
7346b27f7f0SThierry Reding 		ack = reply[0] >> 4;
7356b27f7f0SThierry Reding 		if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_ACK) {
7368695b612SAlan Cox 			memcpy(recv, reply + 1, ret - 1);
7378695b612SAlan Cox 			return ret - 1;
7388695b612SAlan Cox 		}
7396b27f7f0SThierry Reding 		else if ((ack & DP_AUX_NATIVE_REPLY_MASK) == DP_AUX_NATIVE_REPLY_DEFER)
7408695b612SAlan Cox 			udelay(100);
7418695b612SAlan Cox 		else
7428695b612SAlan Cox 			return -EIO;
7438695b612SAlan Cox 	}
7448695b612SAlan Cox }
7458695b612SAlan Cox 
7468695b612SAlan Cox static int
cdv_intel_dp_i2c_aux_ch(struct i2c_adapter * adapter,int mode,uint8_t write_byte,uint8_t * read_byte)74737e7b184SAlan Cox cdv_intel_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
7488695b612SAlan Cox 		    uint8_t write_byte, uint8_t *read_byte)
7498695b612SAlan Cox {
7508695b612SAlan Cox 	struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
75137e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = container_of(adapter,
75237e7b184SAlan Cox 						struct cdv_intel_dp,
7538695b612SAlan Cox 						adapter);
754367e4408SPatrik Jakobsson 	struct gma_encoder *encoder = intel_dp->encoder;
7558695b612SAlan Cox 	uint16_t address = algo_data->address;
7568695b612SAlan Cox 	uint8_t msg[5];
7578695b612SAlan Cox 	uint8_t reply[2];
7588695b612SAlan Cox 	unsigned retry;
7598695b612SAlan Cox 	int msg_bytes;
7608695b612SAlan Cox 	int reply_bytes;
7618695b612SAlan Cox 	int ret;
7628695b612SAlan Cox 
7638695b612SAlan Cox 	/* Set up the command byte */
7648695b612SAlan Cox 	if (mode & MODE_I2C_READ)
7656b27f7f0SThierry Reding 		msg[0] = DP_AUX_I2C_READ << 4;
7668695b612SAlan Cox 	else
7676b27f7f0SThierry Reding 		msg[0] = DP_AUX_I2C_WRITE << 4;
7688695b612SAlan Cox 
7698695b612SAlan Cox 	if (!(mode & MODE_I2C_STOP))
7706b27f7f0SThierry Reding 		msg[0] |= DP_AUX_I2C_MOT << 4;
7718695b612SAlan Cox 
7728695b612SAlan Cox 	msg[1] = address >> 8;
7738695b612SAlan Cox 	msg[2] = address;
7748695b612SAlan Cox 
7758695b612SAlan Cox 	switch (mode) {
7768695b612SAlan Cox 	case MODE_I2C_WRITE:
7778695b612SAlan Cox 		msg[3] = 0;
7788695b612SAlan Cox 		msg[4] = write_byte;
7798695b612SAlan Cox 		msg_bytes = 5;
7808695b612SAlan Cox 		reply_bytes = 1;
7818695b612SAlan Cox 		break;
7828695b612SAlan Cox 	case MODE_I2C_READ:
7838695b612SAlan Cox 		msg[3] = 0;
7848695b612SAlan Cox 		msg_bytes = 4;
7858695b612SAlan Cox 		reply_bytes = 2;
7868695b612SAlan Cox 		break;
7878695b612SAlan Cox 	default:
7888695b612SAlan Cox 		msg_bytes = 3;
7898695b612SAlan Cox 		reply_bytes = 1;
7908695b612SAlan Cox 		break;
7918695b612SAlan Cox 	}
7928695b612SAlan Cox 
7938695b612SAlan Cox 	for (retry = 0; retry < 5; retry++) {
79437e7b184SAlan Cox 		ret = cdv_intel_dp_aux_ch(encoder,
7958695b612SAlan Cox 				      msg, msg_bytes,
7968695b612SAlan Cox 				      reply, reply_bytes);
7978695b612SAlan Cox 		if (ret < 0) {
7988695b612SAlan Cox 			DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
7998695b612SAlan Cox 			return ret;
8008695b612SAlan Cox 		}
8018695b612SAlan Cox 
8026b27f7f0SThierry Reding 		switch ((reply[0] >> 4) & DP_AUX_NATIVE_REPLY_MASK) {
8036b27f7f0SThierry Reding 		case DP_AUX_NATIVE_REPLY_ACK:
8048695b612SAlan Cox 			/* I2C-over-AUX Reply field is only valid
8058695b612SAlan Cox 			 * when paired with AUX ACK.
8068695b612SAlan Cox 			 */
8078695b612SAlan Cox 			break;
8086b27f7f0SThierry Reding 		case DP_AUX_NATIVE_REPLY_NACK:
8098695b612SAlan Cox 			DRM_DEBUG_KMS("aux_ch native nack\n");
8108695b612SAlan Cox 			return -EREMOTEIO;
8116b27f7f0SThierry Reding 		case DP_AUX_NATIVE_REPLY_DEFER:
8128695b612SAlan Cox 			udelay(100);
8138695b612SAlan Cox 			continue;
8148695b612SAlan Cox 		default:
8158695b612SAlan Cox 			DRM_ERROR("aux_ch invalid native reply 0x%02x\n",
8168695b612SAlan Cox 				  reply[0]);
8178695b612SAlan Cox 			return -EREMOTEIO;
8188695b612SAlan Cox 		}
8198695b612SAlan Cox 
8206b27f7f0SThierry Reding 		switch ((reply[0] >> 4) & DP_AUX_I2C_REPLY_MASK) {
8216b27f7f0SThierry Reding 		case DP_AUX_I2C_REPLY_ACK:
8228695b612SAlan Cox 			if (mode == MODE_I2C_READ) {
8238695b612SAlan Cox 				*read_byte = reply[1];
8248695b612SAlan Cox 			}
8258695b612SAlan Cox 			return reply_bytes - 1;
8266b27f7f0SThierry Reding 		case DP_AUX_I2C_REPLY_NACK:
8278695b612SAlan Cox 			DRM_DEBUG_KMS("aux_i2c nack\n");
8288695b612SAlan Cox 			return -EREMOTEIO;
8296b27f7f0SThierry Reding 		case DP_AUX_I2C_REPLY_DEFER:
8308695b612SAlan Cox 			DRM_DEBUG_KMS("aux_i2c defer\n");
8318695b612SAlan Cox 			udelay(100);
8328695b612SAlan Cox 			break;
8338695b612SAlan Cox 		default:
8348695b612SAlan Cox 			DRM_ERROR("aux_i2c invalid reply 0x%02x\n", reply[0]);
8358695b612SAlan Cox 			return -EREMOTEIO;
8368695b612SAlan Cox 		}
8378695b612SAlan Cox 	}
8388695b612SAlan Cox 
8398695b612SAlan Cox 	DRM_ERROR("too many retries, giving up\n");
8408695b612SAlan Cox 	return -EREMOTEIO;
8418695b612SAlan Cox }
8428695b612SAlan Cox 
8438695b612SAlan Cox static int
cdv_intel_dp_i2c_init(struct gma_connector * connector,struct gma_encoder * encoder,const char * name)844367e4408SPatrik Jakobsson cdv_intel_dp_i2c_init(struct gma_connector *connector,
845367e4408SPatrik Jakobsson 		      struct gma_encoder *encoder, const char *name)
8468695b612SAlan Cox {
84737e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
848d112a816SZhao Yakui 	int ret;
849d112a816SZhao Yakui 
8508695b612SAlan Cox 	DRM_DEBUG_KMS("i2c_init %s\n", name);
851d112a816SZhao Yakui 
8528695b612SAlan Cox 	intel_dp->algo.running = false;
8538695b612SAlan Cox 	intel_dp->algo.address = 0;
85437e7b184SAlan Cox 	intel_dp->algo.aux_ch = cdv_intel_dp_i2c_aux_ch;
8558695b612SAlan Cox 
8568695b612SAlan Cox 	memset(&intel_dp->adapter, '\0', sizeof (intel_dp->adapter));
8578695b612SAlan Cox 	intel_dp->adapter.owner = THIS_MODULE;
858*ce43cf34SThorsten Blum 	strscpy(intel_dp->adapter.name, name);
8598695b612SAlan Cox 	intel_dp->adapter.algo_data = &intel_dp->algo;
8605bdebb18SDave Airlie 	intel_dp->adapter.dev.parent = connector->base.kdev;
8618695b612SAlan Cox 
862d112a816SZhao Yakui 	if (is_edp(encoder))
863d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_on(encoder);
864d112a816SZhao Yakui 	ret = i2c_dp_aux_add_bus(&intel_dp->adapter);
865d112a816SZhao Yakui 	if (is_edp(encoder))
866d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_off(encoder);
867d112a816SZhao Yakui 
868d112a816SZhao Yakui 	return ret;
869d112a816SZhao Yakui }
870d112a816SZhao Yakui 
cdv_intel_fixed_panel_mode(struct drm_display_mode * fixed_mode,struct drm_display_mode * adjusted_mode)871a4ab86bcSRashika Kheria static void cdv_intel_fixed_panel_mode(struct drm_display_mode *fixed_mode,
872d112a816SZhao Yakui 	struct drm_display_mode *adjusted_mode)
873d112a816SZhao Yakui {
874d112a816SZhao Yakui 	adjusted_mode->hdisplay = fixed_mode->hdisplay;
875d112a816SZhao Yakui 	adjusted_mode->hsync_start = fixed_mode->hsync_start;
876d112a816SZhao Yakui 	adjusted_mode->hsync_end = fixed_mode->hsync_end;
877d112a816SZhao Yakui 	adjusted_mode->htotal = fixed_mode->htotal;
878d112a816SZhao Yakui 
879d112a816SZhao Yakui 	adjusted_mode->vdisplay = fixed_mode->vdisplay;
880d112a816SZhao Yakui 	adjusted_mode->vsync_start = fixed_mode->vsync_start;
881d112a816SZhao Yakui 	adjusted_mode->vsync_end = fixed_mode->vsync_end;
882d112a816SZhao Yakui 	adjusted_mode->vtotal = fixed_mode->vtotal;
883d112a816SZhao Yakui 
884d112a816SZhao Yakui 	adjusted_mode->clock = fixed_mode->clock;
885d112a816SZhao Yakui 
886d112a816SZhao Yakui 	drm_mode_set_crtcinfo(adjusted_mode, CRTC_INTERLACE_HALVE_V);
8878695b612SAlan Cox }
8888695b612SAlan Cox 
8898695b612SAlan Cox static bool
cdv_intel_dp_mode_fixup(struct drm_encoder * encoder,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)89037e7b184SAlan Cox cdv_intel_dp_mode_fixup(struct drm_encoder *encoder, const struct drm_display_mode *mode,
8918695b612SAlan Cox 		    struct drm_display_mode *adjusted_mode)
8928695b612SAlan Cox {
893f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(encoder->dev);
894367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
89537e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
8968695b612SAlan Cox 	int lane_count, clock;
89737e7b184SAlan Cox 	int max_lane_count = cdv_intel_dp_max_lane_count(intel_encoder);
89837e7b184SAlan Cox 	int max_clock = cdv_intel_dp_max_link_bw(intel_encoder) == DP_LINK_BW_2_7 ? 1 : 0;
8998695b612SAlan Cox 	static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
900d112a816SZhao Yakui 	int refclock = mode->clock;
901d112a816SZhao Yakui 	int bpp = 24;
9028695b612SAlan Cox 
903d112a816SZhao Yakui 	if (is_edp(intel_encoder) && intel_dp->panel_fixed_mode) {
904d112a816SZhao Yakui 		cdv_intel_fixed_panel_mode(intel_dp->panel_fixed_mode, adjusted_mode);
905d112a816SZhao Yakui 		refclock = intel_dp->panel_fixed_mode->clock;
906d112a816SZhao Yakui 		bpp = dev_priv->edp.bpp;
907d112a816SZhao Yakui 	}
9088695b612SAlan Cox 
9098695b612SAlan Cox 	for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
9108695b612SAlan Cox 		for (clock = max_clock; clock >= 0; clock--) {
91137e7b184SAlan Cox 			int link_avail = cdv_intel_dp_max_data_rate(cdv_intel_dp_link_clock(bws[clock]), lane_count);
9128695b612SAlan Cox 
913d112a816SZhao Yakui 			if (cdv_intel_dp_link_required(refclock, bpp) <= link_avail) {
9148695b612SAlan Cox 				intel_dp->link_bw = bws[clock];
9158695b612SAlan Cox 				intel_dp->lane_count = lane_count;
91637e7b184SAlan Cox 				adjusted_mode->clock = cdv_intel_dp_link_clock(intel_dp->link_bw);
9178695b612SAlan Cox 				DRM_DEBUG_KMS("Display port link bw %02x lane "
9188695b612SAlan Cox 						"count %d clock %d\n",
9198695b612SAlan Cox 				       intel_dp->link_bw, intel_dp->lane_count,
9208695b612SAlan Cox 				       adjusted_mode->clock);
9218695b612SAlan Cox 				return true;
9228695b612SAlan Cox 			}
9238695b612SAlan Cox 		}
9248695b612SAlan Cox 	}
925d112a816SZhao Yakui 	if (is_edp(intel_encoder)) {
926d112a816SZhao Yakui 		/* okay we failed just pick the highest */
927d112a816SZhao Yakui 		intel_dp->lane_count = max_lane_count;
928d112a816SZhao Yakui 		intel_dp->link_bw = bws[max_clock];
929d112a816SZhao Yakui 		adjusted_mode->clock = cdv_intel_dp_link_clock(intel_dp->link_bw);
930d112a816SZhao Yakui 		DRM_DEBUG_KMS("Force picking display port link bw %02x lane "
931d112a816SZhao Yakui 			      "count %d clock %d\n",
932d112a816SZhao Yakui 			      intel_dp->link_bw, intel_dp->lane_count,
933d112a816SZhao Yakui 			      adjusted_mode->clock);
9348695b612SAlan Cox 
935d112a816SZhao Yakui 		return true;
936d112a816SZhao Yakui 	}
9378695b612SAlan Cox 	return false;
9388695b612SAlan Cox }
9398695b612SAlan Cox 
94037e7b184SAlan Cox struct cdv_intel_dp_m_n {
9418695b612SAlan Cox 	uint32_t	tu;
9428695b612SAlan Cox 	uint32_t	gmch_m;
9438695b612SAlan Cox 	uint32_t	gmch_n;
9448695b612SAlan Cox 	uint32_t	link_m;
9458695b612SAlan Cox 	uint32_t	link_n;
9468695b612SAlan Cox };
9478695b612SAlan Cox 
9488695b612SAlan Cox static void
cdv_intel_reduce_ratio(uint32_t * num,uint32_t * den)949d112a816SZhao Yakui cdv_intel_reduce_ratio(uint32_t *num, uint32_t *den)
9508695b612SAlan Cox {
9518695b612SAlan Cox 	/*
9528695b612SAlan Cox 	while (*num > 0xffffff || *den > 0xffffff) {
9538695b612SAlan Cox 		*num >>= 1;
9548695b612SAlan Cox 		*den >>= 1;
9558695b612SAlan Cox 	}*/
9568695b612SAlan Cox 	uint64_t value, m;
9578695b612SAlan Cox 	m = *num;
9588695b612SAlan Cox 	value = m * (0x800000);
9598695b612SAlan Cox 	m = do_div(value, *den);
9608695b612SAlan Cox 	*num = value;
9618695b612SAlan Cox 	*den = 0x800000;
9628695b612SAlan Cox }
9638695b612SAlan Cox 
9648695b612SAlan Cox static void
cdv_intel_dp_compute_m_n(int bpp,int nlanes,int pixel_clock,int link_clock,struct cdv_intel_dp_m_n * m_n)96537e7b184SAlan Cox cdv_intel_dp_compute_m_n(int bpp,
9668695b612SAlan Cox 		     int nlanes,
9678695b612SAlan Cox 		     int pixel_clock,
9688695b612SAlan Cox 		     int link_clock,
96937e7b184SAlan Cox 		     struct cdv_intel_dp_m_n *m_n)
9708695b612SAlan Cox {
9718695b612SAlan Cox 	m_n->tu = 64;
9728695b612SAlan Cox 	m_n->gmch_m = (pixel_clock * bpp + 7) >> 3;
9738695b612SAlan Cox 	m_n->gmch_n = link_clock * nlanes;
974d112a816SZhao Yakui 	cdv_intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
9758695b612SAlan Cox 	m_n->link_m = pixel_clock;
9768695b612SAlan Cox 	m_n->link_n = link_clock;
977d112a816SZhao Yakui 	cdv_intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
9788695b612SAlan Cox }
9798695b612SAlan Cox 
9808695b612SAlan Cox void
cdv_intel_dp_set_m_n(struct drm_crtc * crtc,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)98137e7b184SAlan Cox cdv_intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
9828695b612SAlan Cox 		 struct drm_display_mode *adjusted_mode)
9838695b612SAlan Cox {
9848695b612SAlan Cox 	struct drm_device *dev = crtc->dev;
985f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
9868695b612SAlan Cox 	struct drm_mode_config *mode_config = &dev->mode_config;
9878695b612SAlan Cox 	struct drm_encoder *encoder;
9886306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
9898695b612SAlan Cox 	int lane_count = 4, bpp = 24;
99037e7b184SAlan Cox 	struct cdv_intel_dp_m_n m_n;
9916306865dSPatrik Jakobsson 	int pipe = gma_crtc->pipe;
9928695b612SAlan Cox 
9938695b612SAlan Cox 	/*
9948695b612SAlan Cox 	 * Find the lane count in the intel_encoder private
9958695b612SAlan Cox 	 */
9968695b612SAlan Cox 	list_for_each_entry(encoder, &mode_config->encoder_list, head) {
997367e4408SPatrik Jakobsson 		struct gma_encoder *intel_encoder;
99837e7b184SAlan Cox 		struct cdv_intel_dp *intel_dp;
9998695b612SAlan Cox 
10008695b612SAlan Cox 		if (encoder->crtc != crtc)
10018695b612SAlan Cox 			continue;
10028695b612SAlan Cox 
1003367e4408SPatrik Jakobsson 		intel_encoder = to_gma_encoder(encoder);
100437e7b184SAlan Cox 		intel_dp = intel_encoder->dev_priv;
100537e7b184SAlan Cox 		if (intel_encoder->type == INTEL_OUTPUT_DISPLAYPORT) {
10068695b612SAlan Cox 			lane_count = intel_dp->lane_count;
10078695b612SAlan Cox 			break;
100837e7b184SAlan Cox 		} else if (is_edp(intel_encoder)) {
10098695b612SAlan Cox 			lane_count = intel_dp->lane_count;
1010d112a816SZhao Yakui 			bpp = dev_priv->edp.bpp;
10118695b612SAlan Cox 			break;
10128695b612SAlan Cox 		}
10138695b612SAlan Cox 	}
10148695b612SAlan Cox 
10158695b612SAlan Cox 	/*
10168695b612SAlan Cox 	 * Compute the GMCH and Link ratios. The '3' here is
10178695b612SAlan Cox 	 * the number of bytes_per_pixel post-LUT, which we always
10188695b612SAlan Cox 	 * set up for 8-bits of R/G/B, or 3 bytes total.
10198695b612SAlan Cox 	 */
102037e7b184SAlan Cox 	cdv_intel_dp_compute_m_n(bpp, lane_count,
10218695b612SAlan Cox 			     mode->clock, adjusted_mode->clock, &m_n);
10228695b612SAlan Cox 
10238695b612SAlan Cox 	{
10248695b612SAlan Cox 		REG_WRITE(PIPE_GMCH_DATA_M(pipe),
10258695b612SAlan Cox 			   ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
10268695b612SAlan Cox 			   m_n.gmch_m);
10278695b612SAlan Cox 		REG_WRITE(PIPE_GMCH_DATA_N(pipe), m_n.gmch_n);
10288695b612SAlan Cox 		REG_WRITE(PIPE_DP_LINK_M(pipe), m_n.link_m);
10298695b612SAlan Cox 		REG_WRITE(PIPE_DP_LINK_N(pipe), m_n.link_n);
10308695b612SAlan Cox 	}
10318695b612SAlan Cox }
10328695b612SAlan Cox 
10338695b612SAlan Cox static void
cdv_intel_dp_mode_set(struct drm_encoder * encoder,struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)103437e7b184SAlan Cox cdv_intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
10358695b612SAlan Cox 		  struct drm_display_mode *adjusted_mode)
10368695b612SAlan Cox {
1037367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
10388695b612SAlan Cox 	struct drm_crtc *crtc = encoder->crtc;
10396306865dSPatrik Jakobsson 	struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
104037e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
1041d112a816SZhao Yakui 	struct drm_device *dev = encoder->dev;
10428695b612SAlan Cox 
10438695b612SAlan Cox 	intel_dp->DP = DP_VOLTAGE_0_4 | DP_PRE_EMPHASIS_0;
10448695b612SAlan Cox 	intel_dp->DP |= intel_dp->color_range;
10458695b612SAlan Cox 
10468695b612SAlan Cox 	if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
10478695b612SAlan Cox 		intel_dp->DP |= DP_SYNC_HS_HIGH;
10488695b612SAlan Cox 	if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
10498695b612SAlan Cox 		intel_dp->DP |= DP_SYNC_VS_HIGH;
10508695b612SAlan Cox 
10518695b612SAlan Cox 	intel_dp->DP |= DP_LINK_TRAIN_OFF;
10528695b612SAlan Cox 
10538695b612SAlan Cox 	switch (intel_dp->lane_count) {
10548695b612SAlan Cox 	case 1:
10558695b612SAlan Cox 		intel_dp->DP |= DP_PORT_WIDTH_1;
10568695b612SAlan Cox 		break;
10578695b612SAlan Cox 	case 2:
10588695b612SAlan Cox 		intel_dp->DP |= DP_PORT_WIDTH_2;
10598695b612SAlan Cox 		break;
10608695b612SAlan Cox 	case 4:
10618695b612SAlan Cox 		intel_dp->DP |= DP_PORT_WIDTH_4;
10628695b612SAlan Cox 		break;
10638695b612SAlan Cox 	}
10648695b612SAlan Cox 	if (intel_dp->has_audio)
10658695b612SAlan Cox 		intel_dp->DP |= DP_AUDIO_OUTPUT_ENABLE;
10668695b612SAlan Cox 
10678695b612SAlan Cox 	memset(intel_dp->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
10688695b612SAlan Cox 	intel_dp->link_configuration[0] = intel_dp->link_bw;
10698695b612SAlan Cox 	intel_dp->link_configuration[1] = intel_dp->lane_count;
10708695b612SAlan Cox 
10718695b612SAlan Cox 	/*
10728695b612SAlan Cox 	 * Check for DPCD version > 1.1 and enhanced framing support
10738695b612SAlan Cox 	 */
10748695b612SAlan Cox 	if (intel_dp->dpcd[DP_DPCD_REV] >= 0x11 &&
10758695b612SAlan Cox 	    (intel_dp->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)) {
10768695b612SAlan Cox 		intel_dp->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
10778695b612SAlan Cox 		intel_dp->DP |= DP_ENHANCED_FRAMING;
10788695b612SAlan Cox 	}
10798695b612SAlan Cox 
10808695b612SAlan Cox 	/* CPT DP's pipe select is decided in TRANS_DP_CTL */
10816306865dSPatrik Jakobsson 	if (gma_crtc->pipe == 1)
10828695b612SAlan Cox 		intel_dp->DP |= DP_PIPEB_SELECT;
10838695b612SAlan Cox 
1084d112a816SZhao Yakui 	REG_WRITE(intel_dp->output_reg, (intel_dp->DP | DP_PORT_EN));
10858695b612SAlan Cox 	DRM_DEBUG_KMS("DP expected reg is %x\n", intel_dp->DP);
1086d112a816SZhao Yakui 	if (is_edp(intel_encoder)) {
1087d112a816SZhao Yakui 		uint32_t pfit_control;
1088d112a816SZhao Yakui 		cdv_intel_edp_panel_on(intel_encoder);
1089d112a816SZhao Yakui 
1090d112a816SZhao Yakui 		if (mode->hdisplay != adjusted_mode->hdisplay ||
1091d112a816SZhao Yakui 			    mode->vdisplay != adjusted_mode->vdisplay)
1092d112a816SZhao Yakui 			pfit_control = PFIT_ENABLE;
1093d112a816SZhao Yakui 		else
1094d112a816SZhao Yakui 			pfit_control = 0;
1095d112a816SZhao Yakui 
10966306865dSPatrik Jakobsson 		pfit_control |= gma_crtc->pipe << PFIT_PIPE_SHIFT;
1097d112a816SZhao Yakui 
1098d112a816SZhao Yakui 		REG_WRITE(PFIT_CONTROL, pfit_control);
1099d112a816SZhao Yakui 	}
11008695b612SAlan Cox }
11018695b612SAlan Cox 
11028695b612SAlan Cox 
11038695b612SAlan Cox /* If the sink supports it, try to set the power state appropriately */
cdv_intel_dp_sink_dpms(struct gma_encoder * encoder,int mode)1104367e4408SPatrik Jakobsson static void cdv_intel_dp_sink_dpms(struct gma_encoder *encoder, int mode)
11058695b612SAlan Cox {
110637e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
11078695b612SAlan Cox 	int ret, i;
11088695b612SAlan Cox 
11098695b612SAlan Cox 	/* Should have a valid DPCD by this point */
11108695b612SAlan Cox 	if (intel_dp->dpcd[DP_DPCD_REV] < 0x11)
11118695b612SAlan Cox 		return;
11128695b612SAlan Cox 
11138695b612SAlan Cox 	if (mode != DRM_MODE_DPMS_ON) {
111437e7b184SAlan Cox 		ret = cdv_intel_dp_aux_native_write_1(encoder, DP_SET_POWER,
11158695b612SAlan Cox 						  DP_SET_POWER_D3);
11168695b612SAlan Cox 		if (ret != 1)
11178695b612SAlan Cox 			DRM_DEBUG_DRIVER("failed to write sink power state\n");
11188695b612SAlan Cox 	} else {
11198695b612SAlan Cox 		/*
11208695b612SAlan Cox 		 * When turning on, we need to retry for 1ms to give the sink
11218695b612SAlan Cox 		 * time to wake up.
11228695b612SAlan Cox 		 */
11238695b612SAlan Cox 		for (i = 0; i < 3; i++) {
112437e7b184SAlan Cox 			ret = cdv_intel_dp_aux_native_write_1(encoder,
11258695b612SAlan Cox 							  DP_SET_POWER,
11268695b612SAlan Cox 							  DP_SET_POWER_D0);
11278695b612SAlan Cox 			if (ret == 1)
11288695b612SAlan Cox 				break;
11298695b612SAlan Cox 			udelay(1000);
11308695b612SAlan Cox 		}
11318695b612SAlan Cox 	}
11328695b612SAlan Cox }
11338695b612SAlan Cox 
cdv_intel_dp_prepare(struct drm_encoder * encoder)113437e7b184SAlan Cox static void cdv_intel_dp_prepare(struct drm_encoder *encoder)
11358695b612SAlan Cox {
1136367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
1137d112a816SZhao Yakui 	int edp = is_edp(intel_encoder);
11388695b612SAlan Cox 
1139d112a816SZhao Yakui 	if (edp) {
1140d112a816SZhao Yakui 		cdv_intel_edp_backlight_off(intel_encoder);
1141d112a816SZhao Yakui 		cdv_intel_edp_panel_off(intel_encoder);
1142d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_on(intel_encoder);
1143d112a816SZhao Yakui         }
11448695b612SAlan Cox 	/* Wake up the sink first */
114537e7b184SAlan Cox 	cdv_intel_dp_sink_dpms(intel_encoder, DRM_MODE_DPMS_ON);
114637e7b184SAlan Cox 	cdv_intel_dp_link_down(intel_encoder);
1147d112a816SZhao Yakui 	if (edp)
1148d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_off(intel_encoder);
11498695b612SAlan Cox }
11508695b612SAlan Cox 
cdv_intel_dp_commit(struct drm_encoder * encoder)115137e7b184SAlan Cox static void cdv_intel_dp_commit(struct drm_encoder *encoder)
11528695b612SAlan Cox {
1153367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
1154d112a816SZhao Yakui 	int edp = is_edp(intel_encoder);
11558695b612SAlan Cox 
1156d112a816SZhao Yakui 	if (edp)
1157d112a816SZhao Yakui 		cdv_intel_edp_panel_on(intel_encoder);
115837e7b184SAlan Cox 	cdv_intel_dp_start_link_train(intel_encoder);
115937e7b184SAlan Cox 	cdv_intel_dp_complete_link_train(intel_encoder);
1160d112a816SZhao Yakui 	if (edp)
1161d112a816SZhao Yakui 		cdv_intel_edp_backlight_on(intel_encoder);
11628695b612SAlan Cox }
11638695b612SAlan Cox 
11648695b612SAlan Cox static void
cdv_intel_dp_dpms(struct drm_encoder * encoder,int mode)116537e7b184SAlan Cox cdv_intel_dp_dpms(struct drm_encoder *encoder, int mode)
11668695b612SAlan Cox {
1167367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = to_gma_encoder(encoder);
116837e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
11698695b612SAlan Cox 	struct drm_device *dev = encoder->dev;
11708695b612SAlan Cox 	uint32_t dp_reg = REG_READ(intel_dp->output_reg);
1171d112a816SZhao Yakui 	int edp = is_edp(intel_encoder);
11728695b612SAlan Cox 
11738695b612SAlan Cox 	if (mode != DRM_MODE_DPMS_ON) {
1174d112a816SZhao Yakui 		if (edp) {
1175d112a816SZhao Yakui 			cdv_intel_edp_backlight_off(intel_encoder);
1176d112a816SZhao Yakui 			cdv_intel_edp_panel_vdd_on(intel_encoder);
1177d112a816SZhao Yakui 		}
117837e7b184SAlan Cox 		cdv_intel_dp_sink_dpms(intel_encoder, mode);
117937e7b184SAlan Cox 		cdv_intel_dp_link_down(intel_encoder);
1180d112a816SZhao Yakui 		if (edp) {
1181d112a816SZhao Yakui 			cdv_intel_edp_panel_vdd_off(intel_encoder);
1182d112a816SZhao Yakui 			cdv_intel_edp_panel_off(intel_encoder);
1183d112a816SZhao Yakui 		}
11848695b612SAlan Cox 	} else {
1185d112a816SZhao Yakui         	if (edp)
1186d112a816SZhao Yakui 			cdv_intel_edp_panel_on(intel_encoder);
118737e7b184SAlan Cox 		cdv_intel_dp_sink_dpms(intel_encoder, mode);
11888695b612SAlan Cox 		if (!(dp_reg & DP_PORT_EN)) {
118937e7b184SAlan Cox 			cdv_intel_dp_start_link_train(intel_encoder);
119037e7b184SAlan Cox 			cdv_intel_dp_complete_link_train(intel_encoder);
11918695b612SAlan Cox 		}
1192d112a816SZhao Yakui 		if (edp)
1193d112a816SZhao Yakui         		cdv_intel_edp_backlight_on(intel_encoder);
11948695b612SAlan Cox 	}
11958695b612SAlan Cox }
11968695b612SAlan Cox 
11978695b612SAlan Cox /*
11988695b612SAlan Cox  * Native read with retry for link status and receiver capability reads for
11998695b612SAlan Cox  * cases where the sink may still be asleep.
12008695b612SAlan Cox  */
12018695b612SAlan Cox static bool
cdv_intel_dp_aux_native_read_retry(struct gma_encoder * encoder,uint16_t address,uint8_t * recv,int recv_bytes)1202367e4408SPatrik Jakobsson cdv_intel_dp_aux_native_read_retry(struct gma_encoder *encoder, uint16_t address,
12038695b612SAlan Cox 			       uint8_t *recv, int recv_bytes)
12048695b612SAlan Cox {
12058695b612SAlan Cox 	int ret, i;
12068695b612SAlan Cox 
12078695b612SAlan Cox 	/*
12088695b612SAlan Cox 	 * Sinks are *supposed* to come up within 1ms from an off state,
12098695b612SAlan Cox 	 * but we're also supposed to retry 3 times per the spec.
12108695b612SAlan Cox 	 */
12118695b612SAlan Cox 	for (i = 0; i < 3; i++) {
121237e7b184SAlan Cox 		ret = cdv_intel_dp_aux_native_read(encoder, address, recv,
12138695b612SAlan Cox 					       recv_bytes);
12148695b612SAlan Cox 		if (ret == recv_bytes)
12158695b612SAlan Cox 			return true;
12168695b612SAlan Cox 		udelay(1000);
12178695b612SAlan Cox 	}
12188695b612SAlan Cox 
12198695b612SAlan Cox 	return false;
12208695b612SAlan Cox }
12218695b612SAlan Cox 
12228695b612SAlan Cox /*
12238695b612SAlan Cox  * Fetch AUX CH registers 0x202 - 0x207 which contain
12248695b612SAlan Cox  * link status information
12258695b612SAlan Cox  */
12268695b612SAlan Cox static bool
cdv_intel_dp_get_link_status(struct gma_encoder * encoder)1227367e4408SPatrik Jakobsson cdv_intel_dp_get_link_status(struct gma_encoder *encoder)
12288695b612SAlan Cox {
122937e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
123037e7b184SAlan Cox 	return cdv_intel_dp_aux_native_read_retry(encoder,
12318695b612SAlan Cox 					      DP_LANE0_1_STATUS,
12328695b612SAlan Cox 					      intel_dp->link_status,
12338695b612SAlan Cox 					      DP_LINK_STATUS_SIZE);
12348695b612SAlan Cox }
12358695b612SAlan Cox 
12368695b612SAlan Cox static uint8_t
cdv_intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],int r)123737e7b184SAlan Cox cdv_intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
12388695b612SAlan Cox 		     int r)
12398695b612SAlan Cox {
12408695b612SAlan Cox 	return link_status[r - DP_LANE0_1_STATUS];
12418695b612SAlan Cox }
12428695b612SAlan Cox 
12438695b612SAlan Cox static uint8_t
cdv_intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane)124437e7b184SAlan Cox cdv_intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
12458695b612SAlan Cox 				 int lane)
12468695b612SAlan Cox {
12478695b612SAlan Cox 	int	    i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
12488695b612SAlan Cox 	int	    s = ((lane & 1) ?
12498695b612SAlan Cox 			 DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
12508695b612SAlan Cox 			 DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
125137e7b184SAlan Cox 	uint8_t l = cdv_intel_dp_link_status(link_status, i);
12528695b612SAlan Cox 
12538695b612SAlan Cox 	return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
12548695b612SAlan Cox }
12558695b612SAlan Cox 
12568695b612SAlan Cox static uint8_t
cdv_intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane)125737e7b184SAlan Cox cdv_intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
12588695b612SAlan Cox 				      int lane)
12598695b612SAlan Cox {
12608695b612SAlan Cox 	int	    i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
12618695b612SAlan Cox 	int	    s = ((lane & 1) ?
12628695b612SAlan Cox 			 DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
12638695b612SAlan Cox 			 DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
126437e7b184SAlan Cox 	uint8_t l = cdv_intel_dp_link_status(link_status, i);
12658695b612SAlan Cox 
12668695b612SAlan Cox 	return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
12678695b612SAlan Cox }
12688695b612SAlan Cox 
126931160006SSonika Jindal #define CDV_DP_VOLTAGE_MAX	    DP_TRAIN_VOLTAGE_SWING_LEVEL_3
1270871c6015SSouptick Joarder 
12718695b612SAlan Cox static void
cdv_intel_get_adjust_train(struct gma_encoder * encoder)1272367e4408SPatrik Jakobsson cdv_intel_get_adjust_train(struct gma_encoder *encoder)
12738695b612SAlan Cox {
127437e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
12758695b612SAlan Cox 	uint8_t v = 0;
12768695b612SAlan Cox 	uint8_t p = 0;
12778695b612SAlan Cox 	int lane;
12788695b612SAlan Cox 
12798695b612SAlan Cox 	for (lane = 0; lane < intel_dp->lane_count; lane++) {
128037e7b184SAlan Cox 		uint8_t this_v = cdv_intel_get_adjust_request_voltage(intel_dp->link_status, lane);
128137e7b184SAlan Cox 		uint8_t this_p = cdv_intel_get_adjust_request_pre_emphasis(intel_dp->link_status, lane);
12828695b612SAlan Cox 
12838695b612SAlan Cox 		if (this_v > v)
12848695b612SAlan Cox 			v = this_v;
12858695b612SAlan Cox 		if (this_p > p)
12868695b612SAlan Cox 			p = this_p;
12878695b612SAlan Cox 	}
12888695b612SAlan Cox 
12898695b612SAlan Cox 	if (v >= CDV_DP_VOLTAGE_MAX)
12908695b612SAlan Cox 		v = CDV_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
12918695b612SAlan Cox 
12928695b612SAlan Cox 	if (p == DP_TRAIN_PRE_EMPHASIS_MASK)
12938695b612SAlan Cox 		p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
12948695b612SAlan Cox 
12958695b612SAlan Cox 	for (lane = 0; lane < 4; lane++)
12968695b612SAlan Cox 		intel_dp->train_set[lane] = v | p;
12978695b612SAlan Cox }
12988695b612SAlan Cox 
12998695b612SAlan Cox 
13008695b612SAlan Cox static uint8_t
cdv_intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane)130137e7b184SAlan Cox cdv_intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
13028695b612SAlan Cox 		      int lane)
13038695b612SAlan Cox {
13048695b612SAlan Cox 	int i = DP_LANE0_1_STATUS + (lane >> 1);
13058695b612SAlan Cox 	int s = (lane & 1) * 4;
130637e7b184SAlan Cox 	uint8_t l = cdv_intel_dp_link_status(link_status, i);
13078695b612SAlan Cox 
13088695b612SAlan Cox 	return (l >> s) & 0xf;
13098695b612SAlan Cox }
13108695b612SAlan Cox 
13118695b612SAlan Cox /* Check for clock recovery is done on all channels */
13128695b612SAlan Cox static bool
cdv_intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE],int lane_count)131337e7b184SAlan Cox cdv_intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
13148695b612SAlan Cox {
13158695b612SAlan Cox 	int lane;
13168695b612SAlan Cox 	uint8_t lane_status;
13178695b612SAlan Cox 
13188695b612SAlan Cox 	for (lane = 0; lane < lane_count; lane++) {
131937e7b184SAlan Cox 		lane_status = cdv_intel_get_lane_status(link_status, lane);
13208695b612SAlan Cox 		if ((lane_status & DP_LANE_CR_DONE) == 0)
13218695b612SAlan Cox 			return false;
13228695b612SAlan Cox 	}
13238695b612SAlan Cox 	return true;
13248695b612SAlan Cox }
13258695b612SAlan Cox 
13268695b612SAlan Cox /* Check to see if channel eq is done on all channels */
13278695b612SAlan Cox #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
13288695b612SAlan Cox 			 DP_LANE_CHANNEL_EQ_DONE|\
13298695b612SAlan Cox 			 DP_LANE_SYMBOL_LOCKED)
13308695b612SAlan Cox static bool
cdv_intel_channel_eq_ok(struct gma_encoder * encoder)1331367e4408SPatrik Jakobsson cdv_intel_channel_eq_ok(struct gma_encoder *encoder)
13328695b612SAlan Cox {
133337e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
13348695b612SAlan Cox 	uint8_t lane_align;
13358695b612SAlan Cox 	uint8_t lane_status;
13368695b612SAlan Cox 	int lane;
13378695b612SAlan Cox 
133837e7b184SAlan Cox 	lane_align = cdv_intel_dp_link_status(intel_dp->link_status,
13398695b612SAlan Cox 					  DP_LANE_ALIGN_STATUS_UPDATED);
13408695b612SAlan Cox 	if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
13418695b612SAlan Cox 		return false;
13428695b612SAlan Cox 	for (lane = 0; lane < intel_dp->lane_count; lane++) {
134337e7b184SAlan Cox 		lane_status = cdv_intel_get_lane_status(intel_dp->link_status, lane);
13448695b612SAlan Cox 		if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
13458695b612SAlan Cox 			return false;
13468695b612SAlan Cox 	}
13478695b612SAlan Cox 	return true;
13488695b612SAlan Cox }
13498695b612SAlan Cox 
13508695b612SAlan Cox static bool
cdv_intel_dp_set_link_train(struct gma_encoder * encoder,uint32_t dp_reg_value,uint8_t dp_train_pat)1351367e4408SPatrik Jakobsson cdv_intel_dp_set_link_train(struct gma_encoder *encoder,
13528695b612SAlan Cox 			uint32_t dp_reg_value,
13538695b612SAlan Cox 			uint8_t dp_train_pat)
13548695b612SAlan Cox {
135537e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
13568695b612SAlan Cox 	int ret;
135737e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
13588695b612SAlan Cox 
13598695b612SAlan Cox 	REG_WRITE(intel_dp->output_reg, dp_reg_value);
13608695b612SAlan Cox 	REG_READ(intel_dp->output_reg);
13618695b612SAlan Cox 
136237e7b184SAlan Cox 	ret = cdv_intel_dp_aux_native_write_1(encoder,
13638695b612SAlan Cox 				    DP_TRAINING_PATTERN_SET,
13648695b612SAlan Cox 				    dp_train_pat);
13658695b612SAlan Cox 
13668695b612SAlan Cox 	if (ret != 1) {
13678695b612SAlan Cox 		DRM_DEBUG_KMS("Failure in setting link pattern %x\n",
13688695b612SAlan Cox 				dp_train_pat);
13698695b612SAlan Cox 		return false;
13708695b612SAlan Cox 	}
13718695b612SAlan Cox 
13728695b612SAlan Cox 	return true;
13738695b612SAlan Cox }
13748695b612SAlan Cox 
13758695b612SAlan Cox 
13768695b612SAlan Cox static bool
cdv_intel_dplink_set_level(struct gma_encoder * encoder,uint8_t dp_train_pat)1377367e4408SPatrik Jakobsson cdv_intel_dplink_set_level(struct gma_encoder *encoder,
13788695b612SAlan Cox 			uint8_t dp_train_pat)
13798695b612SAlan Cox {
13808695b612SAlan Cox 	int ret;
138137e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
13828695b612SAlan Cox 
138337e7b184SAlan Cox 	ret = cdv_intel_dp_aux_native_write(encoder,
13848695b612SAlan Cox 					DP_TRAINING_LANE0_SET,
13858695b612SAlan Cox 					intel_dp->train_set,
13868695b612SAlan Cox 					intel_dp->lane_count);
13878695b612SAlan Cox 
13888695b612SAlan Cox 	if (ret != intel_dp->lane_count) {
13898695b612SAlan Cox 		DRM_DEBUG_KMS("Failure in setting level %d, lane_cnt= %d\n",
13908695b612SAlan Cox 				intel_dp->train_set[0], intel_dp->lane_count);
13918695b612SAlan Cox 		return false;
13928695b612SAlan Cox 	}
13938695b612SAlan Cox 	return true;
13948695b612SAlan Cox }
13958695b612SAlan Cox 
13968695b612SAlan Cox static void
cdv_intel_dp_set_vswing_premph(struct gma_encoder * encoder,uint8_t signal_level)1397367e4408SPatrik Jakobsson cdv_intel_dp_set_vswing_premph(struct gma_encoder *encoder, uint8_t signal_level)
13988695b612SAlan Cox {
139937e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
140037e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
14018695b612SAlan Cox 	struct ddi_regoff *ddi_reg;
14028695b612SAlan Cox 	int vswing, premph, index;
14038695b612SAlan Cox 
14048695b612SAlan Cox 	if (intel_dp->output_reg == DP_B)
14058695b612SAlan Cox 		ddi_reg = &ddi_DP_train_table[0];
14068695b612SAlan Cox 	else
14078695b612SAlan Cox 		ddi_reg = &ddi_DP_train_table[1];
14088695b612SAlan Cox 
14098695b612SAlan Cox 	vswing = (signal_level & DP_TRAIN_VOLTAGE_SWING_MASK);
14108695b612SAlan Cox 	premph = ((signal_level & DP_TRAIN_PRE_EMPHASIS_MASK)) >>
14118695b612SAlan Cox 				DP_TRAIN_PRE_EMPHASIS_SHIFT;
14128695b612SAlan Cox 
14138695b612SAlan Cox 	if (vswing + premph > 3)
14148695b612SAlan Cox 		return;
14158695b612SAlan Cox #ifdef CDV_FAST_LINK_TRAIN
14168695b612SAlan Cox 	return;
14178695b612SAlan Cox #endif
14188695b612SAlan Cox 	DRM_DEBUG_KMS("Test2\n");
14198695b612SAlan Cox 	//return ;
142037e7b184SAlan Cox 	cdv_sb_reset(dev);
14218695b612SAlan Cox 	/* ;Swing voltage programming
14228695b612SAlan Cox         ;gfx_dpio_set_reg(0xc058, 0x0505313A) */
142337e7b184SAlan Cox 	cdv_sb_write(dev, ddi_reg->VSwing5, 0x0505313A);
14248695b612SAlan Cox 
14258695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x8154, 0x43406055) */
142637e7b184SAlan Cox 	cdv_sb_write(dev, ddi_reg->VSwing1, 0x43406055);
14278695b612SAlan Cox 
14288695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x8148, 0x55338954)
14298695b612SAlan Cox 	 * The VSwing_PreEmph table is also considered based on the vswing/premp
14308695b612SAlan Cox 	 */
14318695b612SAlan Cox 	index = (vswing + premph) * 2;
14328695b612SAlan Cox 	if (premph == 1 && vswing == 1) {
143337e7b184SAlan Cox 		cdv_sb_write(dev, ddi_reg->VSwing2, 0x055738954);
14348695b612SAlan Cox 	} else
143537e7b184SAlan Cox 		cdv_sb_write(dev, ddi_reg->VSwing2, dp_vswing_premph_table[index]);
14368695b612SAlan Cox 
14378695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x814c, 0x40802040) */
143831160006SSonika Jindal 	if ((vswing + premph) == DP_TRAIN_VOLTAGE_SWING_LEVEL_3)
143937e7b184SAlan Cox 		cdv_sb_write(dev, ddi_reg->VSwing3, 0x70802040);
14408695b612SAlan Cox 	else
144137e7b184SAlan Cox 		cdv_sb_write(dev, ddi_reg->VSwing3, 0x40802040);
14428695b612SAlan Cox 
14438695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x8150, 0x2b405555) */
144437e7b184SAlan Cox 	/* cdv_sb_write(dev, ddi_reg->VSwing4, 0x2b405555); */
14458695b612SAlan Cox 
14468695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x8154, 0xc3406055) */
144737e7b184SAlan Cox 	cdv_sb_write(dev, ddi_reg->VSwing1, 0xc3406055);
14488695b612SAlan Cox 
14498695b612SAlan Cox 	/* ;Pre emphasis programming
14508695b612SAlan Cox 	 * ;gfx_dpio_set_reg(0xc02c, 0x1f030040)
14518695b612SAlan Cox 	 */
145237e7b184SAlan Cox 	cdv_sb_write(dev, ddi_reg->PreEmph1, 0x1f030040);
14538695b612SAlan Cox 
14548695b612SAlan Cox 	/* ;gfx_dpio_set_reg(0x8124, 0x00004000) */
14558695b612SAlan Cox 	index = 2 * premph + 1;
145637e7b184SAlan Cox 	cdv_sb_write(dev, ddi_reg->PreEmph2, dp_vswing_premph_table[index]);
14578695b612SAlan Cox 	return;
14588695b612SAlan Cox }
14598695b612SAlan Cox 
14608695b612SAlan Cox 
14618695b612SAlan Cox /* Enable corresponding port and start training pattern 1 */
14628695b612SAlan Cox static void
cdv_intel_dp_start_link_train(struct gma_encoder * encoder)1463367e4408SPatrik Jakobsson cdv_intel_dp_start_link_train(struct gma_encoder *encoder)
14648695b612SAlan Cox {
146537e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
146637e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
14678695b612SAlan Cox 	int i;
14688695b612SAlan Cox 	uint8_t voltage;
14698695b612SAlan Cox 	bool clock_recovery = false;
14708695b612SAlan Cox 	int tries;
14718695b612SAlan Cox 	u32 reg;
14728695b612SAlan Cox 	uint32_t DP = intel_dp->DP;
14738695b612SAlan Cox 
14748695b612SAlan Cox 	DP |= DP_PORT_EN;
14758695b612SAlan Cox 	DP &= ~DP_LINK_TRAIN_MASK;
14768695b612SAlan Cox 
14778695b612SAlan Cox 	reg = DP;
14788695b612SAlan Cox 	reg |= DP_LINK_TRAIN_PAT_1;
14798695b612SAlan Cox 	/* Enable output, wait for it to become active */
14808695b612SAlan Cox 	REG_WRITE(intel_dp->output_reg, reg);
14818695b612SAlan Cox 	REG_READ(intel_dp->output_reg);
1482d1fa08f3SPatrik Jakobsson 	gma_wait_for_vblank(dev);
14838695b612SAlan Cox 
14848695b612SAlan Cox 	DRM_DEBUG_KMS("Link config\n");
14858695b612SAlan Cox 	/* Write the link configuration data */
148637e7b184SAlan Cox 	cdv_intel_dp_aux_native_write(encoder, DP_LINK_BW_SET,
14878695b612SAlan Cox 				  intel_dp->link_configuration,
14888695b612SAlan Cox 				  2);
14898695b612SAlan Cox 
14908695b612SAlan Cox 	memset(intel_dp->train_set, 0, 4);
14918695b612SAlan Cox 	voltage = 0;
14928695b612SAlan Cox 	tries = 0;
14938695b612SAlan Cox 	clock_recovery = false;
14948695b612SAlan Cox 
14958695b612SAlan Cox 	DRM_DEBUG_KMS("Start train\n");
14968695b612SAlan Cox 	reg = DP | DP_LINK_TRAIN_PAT_1;
14978695b612SAlan Cox 
14988695b612SAlan Cox 	for (;;) {
14998695b612SAlan Cox 		/* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
1500d112a816SZhao Yakui 		DRM_DEBUG_KMS("DP Link Train Set %x, Link_config %x, %x\n",
1501d112a816SZhao Yakui 				intel_dp->train_set[0],
1502d112a816SZhao Yakui 				intel_dp->link_configuration[0],
1503d112a816SZhao Yakui 				intel_dp->link_configuration[1]);
15048695b612SAlan Cox 
150537e7b184SAlan Cox 		if (!cdv_intel_dp_set_link_train(encoder, reg, DP_TRAINING_PATTERN_1)) {
15068695b612SAlan Cox 			DRM_DEBUG_KMS("Failure in aux-transfer setting pattern 1\n");
15078695b612SAlan Cox 		}
150837e7b184SAlan Cox 		cdv_intel_dp_set_vswing_premph(encoder, intel_dp->train_set[0]);
15098695b612SAlan Cox 		/* Set training pattern 1 */
15108695b612SAlan Cox 
151137e7b184SAlan Cox 		cdv_intel_dplink_set_level(encoder, DP_TRAINING_PATTERN_1);
15128695b612SAlan Cox 
15138695b612SAlan Cox 		udelay(200);
151437e7b184SAlan Cox 		if (!cdv_intel_dp_get_link_status(encoder))
15158695b612SAlan Cox 			break;
15168695b612SAlan Cox 
1517d112a816SZhao Yakui 		DRM_DEBUG_KMS("DP Link status %x, %x, %x, %x, %x, %x\n",
1518d112a816SZhao Yakui 				intel_dp->link_status[0], intel_dp->link_status[1], intel_dp->link_status[2],
1519d112a816SZhao Yakui 				intel_dp->link_status[3], intel_dp->link_status[4], intel_dp->link_status[5]);
1520d112a816SZhao Yakui 
152137e7b184SAlan Cox 		if (cdv_intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
15228695b612SAlan Cox 			DRM_DEBUG_KMS("PT1 train is done\n");
15238695b612SAlan Cox 			clock_recovery = true;
15248695b612SAlan Cox 			break;
15258695b612SAlan Cox 		}
15268695b612SAlan Cox 
15278695b612SAlan Cox 		/* Check to see if we've tried the max voltage */
15288695b612SAlan Cox 		for (i = 0; i < intel_dp->lane_count; i++)
15298695b612SAlan Cox 			if ((intel_dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
15308695b612SAlan Cox 				break;
15318695b612SAlan Cox 		if (i == intel_dp->lane_count)
15328695b612SAlan Cox 			break;
15338695b612SAlan Cox 
15348695b612SAlan Cox 		/* Check to see if we've tried the same voltage 5 times */
15358695b612SAlan Cox 		if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
15368695b612SAlan Cox 			++tries;
15378695b612SAlan Cox 			if (tries == 5)
15388695b612SAlan Cox 				break;
15398695b612SAlan Cox 		} else
15408695b612SAlan Cox 			tries = 0;
15418695b612SAlan Cox 		voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
15428695b612SAlan Cox 
15438695b612SAlan Cox 		/* Compute new intel_dp->train_set as requested by target */
154437e7b184SAlan Cox 		cdv_intel_get_adjust_train(encoder);
15458695b612SAlan Cox 
15468695b612SAlan Cox 	}
15478695b612SAlan Cox 
15488695b612SAlan Cox 	if (!clock_recovery) {
15490fe113adSColin Ian King 		DRM_DEBUG_KMS("failure in DP pattern 1 training, train set %x\n", intel_dp->train_set[0]);
15508695b612SAlan Cox 	}
15518695b612SAlan Cox 
15528695b612SAlan Cox 	intel_dp->DP = DP;
15538695b612SAlan Cox }
15548695b612SAlan Cox 
15558695b612SAlan Cox static void
cdv_intel_dp_complete_link_train(struct gma_encoder * encoder)1556367e4408SPatrik Jakobsson cdv_intel_dp_complete_link_train(struct gma_encoder *encoder)
15578695b612SAlan Cox {
155837e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
155937e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
15608695b612SAlan Cox 	int tries, cr_tries;
15618695b612SAlan Cox 	u32 reg;
15628695b612SAlan Cox 	uint32_t DP = intel_dp->DP;
15638695b612SAlan Cox 
15648695b612SAlan Cox 	/* channel equalization */
15658695b612SAlan Cox 	tries = 0;
15668695b612SAlan Cox 	cr_tries = 0;
15678695b612SAlan Cox 
15688695b612SAlan Cox 	DRM_DEBUG_KMS("\n");
15698695b612SAlan Cox 	reg = DP | DP_LINK_TRAIN_PAT_2;
15708695b612SAlan Cox 
15718695b612SAlan Cox 	for (;;) {
1572d112a816SZhao Yakui 
1573d112a816SZhao Yakui 		DRM_DEBUG_KMS("DP Link Train Set %x, Link_config %x, %x\n",
1574d112a816SZhao Yakui 				intel_dp->train_set[0],
1575d112a816SZhao Yakui 				intel_dp->link_configuration[0],
1576d112a816SZhao Yakui 				intel_dp->link_configuration[1]);
15778695b612SAlan Cox         	/* channel eq pattern */
1578d112a816SZhao Yakui 
157937e7b184SAlan Cox 		if (!cdv_intel_dp_set_link_train(encoder, reg,
15808695b612SAlan Cox 					     DP_TRAINING_PATTERN_2)) {
15818695b612SAlan Cox 			DRM_DEBUG_KMS("Failure in aux-transfer setting pattern 2\n");
15828695b612SAlan Cox 		}
15838695b612SAlan Cox 		/* Use intel_dp->train_set[0] to set the voltage and pre emphasis values */
15848695b612SAlan Cox 
15858695b612SAlan Cox 		if (cr_tries > 5) {
15868695b612SAlan Cox 			DRM_ERROR("failed to train DP, aborting\n");
158737e7b184SAlan Cox 			cdv_intel_dp_link_down(encoder);
15888695b612SAlan Cox 			break;
15898695b612SAlan Cox 		}
15908695b612SAlan Cox 
159137e7b184SAlan Cox 		cdv_intel_dp_set_vswing_premph(encoder, intel_dp->train_set[0]);
15928695b612SAlan Cox 
159337e7b184SAlan Cox 		cdv_intel_dplink_set_level(encoder, DP_TRAINING_PATTERN_2);
15948695b612SAlan Cox 
15958695b612SAlan Cox 		udelay(1000);
159637e7b184SAlan Cox 		if (!cdv_intel_dp_get_link_status(encoder))
15978695b612SAlan Cox 			break;
15988695b612SAlan Cox 
1599d112a816SZhao Yakui 		DRM_DEBUG_KMS("DP Link status %x, %x, %x, %x, %x, %x\n",
1600d112a816SZhao Yakui 				intel_dp->link_status[0], intel_dp->link_status[1], intel_dp->link_status[2],
1601d112a816SZhao Yakui 				intel_dp->link_status[3], intel_dp->link_status[4], intel_dp->link_status[5]);
1602d112a816SZhao Yakui 
16038695b612SAlan Cox 		/* Make sure clock is still ok */
160437e7b184SAlan Cox 		if (!cdv_intel_clock_recovery_ok(intel_dp->link_status, intel_dp->lane_count)) {
160537e7b184SAlan Cox 			cdv_intel_dp_start_link_train(encoder);
16068695b612SAlan Cox 			cr_tries++;
16078695b612SAlan Cox 			continue;
16088695b612SAlan Cox 		}
16098695b612SAlan Cox 
161037e7b184SAlan Cox 		if (cdv_intel_channel_eq_ok(encoder)) {
16118695b612SAlan Cox 			DRM_DEBUG_KMS("PT2 train is done\n");
16128695b612SAlan Cox 			break;
16138695b612SAlan Cox 		}
16148695b612SAlan Cox 
16158695b612SAlan Cox 		/* Try 5 times, then try clock recovery if that fails */
16168695b612SAlan Cox 		if (tries > 5) {
161737e7b184SAlan Cox 			cdv_intel_dp_link_down(encoder);
161837e7b184SAlan Cox 			cdv_intel_dp_start_link_train(encoder);
16198695b612SAlan Cox 			tries = 0;
16208695b612SAlan Cox 			cr_tries++;
16218695b612SAlan Cox 			continue;
16228695b612SAlan Cox 		}
16238695b612SAlan Cox 
16248695b612SAlan Cox 		/* Compute new intel_dp->train_set as requested by target */
162537e7b184SAlan Cox 		cdv_intel_get_adjust_train(encoder);
16268695b612SAlan Cox 		++tries;
16278695b612SAlan Cox 
16288695b612SAlan Cox 	}
16298695b612SAlan Cox 
16308695b612SAlan Cox 	reg = DP | DP_LINK_TRAIN_OFF;
16318695b612SAlan Cox 
16328695b612SAlan Cox 	REG_WRITE(intel_dp->output_reg, reg);
16338695b612SAlan Cox 	REG_READ(intel_dp->output_reg);
163437e7b184SAlan Cox 	cdv_intel_dp_aux_native_write_1(encoder,
16358695b612SAlan Cox 				    DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
16368695b612SAlan Cox }
16378695b612SAlan Cox 
16388695b612SAlan Cox static void
cdv_intel_dp_link_down(struct gma_encoder * encoder)1639367e4408SPatrik Jakobsson cdv_intel_dp_link_down(struct gma_encoder *encoder)
16408695b612SAlan Cox {
164137e7b184SAlan Cox 	struct drm_device *dev = encoder->base.dev;
164237e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
16438695b612SAlan Cox 	uint32_t DP = intel_dp->DP;
16448695b612SAlan Cox 
16458695b612SAlan Cox 	if ((REG_READ(intel_dp->output_reg) & DP_PORT_EN) == 0)
16468695b612SAlan Cox 		return;
16478695b612SAlan Cox 
16488695b612SAlan Cox 	DRM_DEBUG_KMS("\n");
16498695b612SAlan Cox 
16508695b612SAlan Cox 
16518695b612SAlan Cox 	{
16528695b612SAlan Cox 		DP &= ~DP_LINK_TRAIN_MASK;
16538695b612SAlan Cox 		REG_WRITE(intel_dp->output_reg, DP | DP_LINK_TRAIN_PAT_IDLE);
16548695b612SAlan Cox 	}
16558695b612SAlan Cox 	REG_READ(intel_dp->output_reg);
16568695b612SAlan Cox 
16578695b612SAlan Cox 	msleep(17);
16588695b612SAlan Cox 
16598695b612SAlan Cox 	REG_WRITE(intel_dp->output_reg, DP & ~DP_PORT_EN);
16608695b612SAlan Cox 	REG_READ(intel_dp->output_reg);
16618695b612SAlan Cox }
16628695b612SAlan Cox 
cdv_dp_detect(struct gma_encoder * encoder)1663367e4408SPatrik Jakobsson static enum drm_connector_status cdv_dp_detect(struct gma_encoder *encoder)
16648695b612SAlan Cox {
166537e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
16668695b612SAlan Cox 	enum drm_connector_status status;
16678695b612SAlan Cox 
16688695b612SAlan Cox 	status = connector_status_disconnected;
166937e7b184SAlan Cox 	if (cdv_intel_dp_aux_native_read(encoder, 0x000, intel_dp->dpcd,
16708695b612SAlan Cox 				     sizeof (intel_dp->dpcd)) == sizeof (intel_dp->dpcd))
16718695b612SAlan Cox 	{
16728695b612SAlan Cox 		if (intel_dp->dpcd[DP_DPCD_REV] != 0)
16738695b612SAlan Cox 			status = connector_status_connected;
16748695b612SAlan Cox 	}
16758695b612SAlan Cox 	if (status == connector_status_connected)
16768695b612SAlan Cox 		DRM_DEBUG_KMS("DPCD: Rev=%x LN_Rate=%x LN_CNT=%x LN_DOWNSP=%x\n",
16778695b612SAlan Cox 			intel_dp->dpcd[0], intel_dp->dpcd[1],
16788695b612SAlan Cox 			intel_dp->dpcd[2], intel_dp->dpcd[3]);
16798695b612SAlan Cox 	return status;
16808695b612SAlan Cox }
16818695b612SAlan Cox 
168277ee9c56SLee Jones /*
16838695b612SAlan Cox  * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
16848695b612SAlan Cox  *
16858695b612SAlan Cox  * \return true if DP port is connected.
16868695b612SAlan Cox  * \return false if DP port is disconnected.
16878695b612SAlan Cox  */
16888695b612SAlan Cox static enum drm_connector_status
cdv_intel_dp_detect(struct drm_connector * connector,bool force)168937e7b184SAlan Cox cdv_intel_dp_detect(struct drm_connector *connector, bool force)
16908695b612SAlan Cox {
1691367e4408SPatrik Jakobsson 	struct gma_encoder *encoder = gma_attached_encoder(connector);
169237e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
16938695b612SAlan Cox 	enum drm_connector_status status;
16948695b612SAlan Cox 	struct edid *edid = NULL;
1695d112a816SZhao Yakui 	int edp = is_edp(encoder);
16968695b612SAlan Cox 
16978695b612SAlan Cox 	intel_dp->has_audio = false;
16988695b612SAlan Cox 
1699d112a816SZhao Yakui 	if (edp)
1700d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_on(encoder);
170137e7b184SAlan Cox 	status = cdv_dp_detect(encoder);
1702d112a816SZhao Yakui 	if (status != connector_status_connected) {
1703d112a816SZhao Yakui 		if (edp)
1704d112a816SZhao Yakui 			cdv_intel_edp_panel_vdd_off(encoder);
17058695b612SAlan Cox 		return status;
1706d112a816SZhao Yakui         }
17078695b612SAlan Cox 
17088695b612SAlan Cox 	if (intel_dp->force_audio) {
17098695b612SAlan Cox 		intel_dp->has_audio = intel_dp->force_audio > 0;
17108695b612SAlan Cox 	} else {
17118695b612SAlan Cox 		edid = drm_get_edid(connector, &intel_dp->adapter);
17128695b612SAlan Cox 		if (edid) {
17138695b612SAlan Cox 			intel_dp->has_audio = drm_detect_monitor_audio(edid);
17148695b612SAlan Cox 			kfree(edid);
17158695b612SAlan Cox 		}
17168695b612SAlan Cox 	}
1717d112a816SZhao Yakui 	if (edp)
1718d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_off(encoder);
17198695b612SAlan Cox 
17208695b612SAlan Cox 	return connector_status_connected;
17218695b612SAlan Cox }
17228695b612SAlan Cox 
cdv_intel_dp_get_modes(struct drm_connector * connector)172337e7b184SAlan Cox static int cdv_intel_dp_get_modes(struct drm_connector *connector)
17248695b612SAlan Cox {
1725367e4408SPatrik Jakobsson 	struct gma_encoder *intel_encoder = gma_attached_encoder(connector);
172637e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = intel_encoder->dev_priv;
17278695b612SAlan Cox 	struct edid *edid = NULL;
17288695b612SAlan Cox 	int ret = 0;
1729d112a816SZhao Yakui 	int edp = is_edp(intel_encoder);
17308695b612SAlan Cox 
17318695b612SAlan Cox 
173237e7b184SAlan Cox 	edid = drm_get_edid(connector, &intel_dp->adapter);
17338695b612SAlan Cox 	if (edid) {
1734c555f023SDaniel Vetter 		drm_connector_update_edid_property(connector, edid);
173537e7b184SAlan Cox 		ret = drm_add_edid_modes(connector, edid);
17368695b612SAlan Cox 		kfree(edid);
17378695b612SAlan Cox 	}
17388695b612SAlan Cox 
1739d112a816SZhao Yakui 	if (is_edp(intel_encoder)) {
1740d112a816SZhao Yakui 		struct drm_device *dev = connector->dev;
1741f71635e8SThomas Zimmermann 		struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
1742d112a816SZhao Yakui 
1743d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_off(intel_encoder);
1744d112a816SZhao Yakui 		if (ret) {
1745d112a816SZhao Yakui 			if (edp && !intel_dp->panel_fixed_mode) {
1746d112a816SZhao Yakui 				struct drm_display_mode *newmode;
1747d112a816SZhao Yakui 				list_for_each_entry(newmode, &connector->probed_modes,
1748d112a816SZhao Yakui 					    head) {
1749d112a816SZhao Yakui 					if (newmode->type & DRM_MODE_TYPE_PREFERRED) {
1750d112a816SZhao Yakui 						intel_dp->panel_fixed_mode =
1751d112a816SZhao Yakui 							drm_mode_duplicate(dev, newmode);
1752d112a816SZhao Yakui 						break;
1753d112a816SZhao Yakui 					}
1754d112a816SZhao Yakui 				}
1755d112a816SZhao Yakui 			}
1756d112a816SZhao Yakui 
1757d112a816SZhao Yakui 			return ret;
1758d112a816SZhao Yakui 		}
1759d112a816SZhao Yakui 		if (!intel_dp->panel_fixed_mode && dev_priv->lfp_lvds_vbt_mode) {
1760d112a816SZhao Yakui 			intel_dp->panel_fixed_mode =
1761d112a816SZhao Yakui 				drm_mode_duplicate(dev, dev_priv->lfp_lvds_vbt_mode);
1762d112a816SZhao Yakui 			if (intel_dp->panel_fixed_mode) {
1763d112a816SZhao Yakui 				intel_dp->panel_fixed_mode->type |=
1764d112a816SZhao Yakui 					DRM_MODE_TYPE_PREFERRED;
1765d112a816SZhao Yakui 			}
1766d112a816SZhao Yakui 		}
1767d112a816SZhao Yakui 		if (intel_dp->panel_fixed_mode != NULL) {
1768d112a816SZhao Yakui 			struct drm_display_mode *mode;
1769d112a816SZhao Yakui 			mode = drm_mode_duplicate(dev, intel_dp->panel_fixed_mode);
1770d112a816SZhao Yakui 			drm_mode_probed_add(connector, mode);
1771d112a816SZhao Yakui 			return 1;
1772d112a816SZhao Yakui 		}
1773d112a816SZhao Yakui 	}
1774d112a816SZhao Yakui 
17758695b612SAlan Cox 	return ret;
17768695b612SAlan Cox }
17778695b612SAlan Cox 
17788695b612SAlan Cox static bool
cdv_intel_dp_detect_audio(struct drm_connector * connector)177937e7b184SAlan Cox cdv_intel_dp_detect_audio(struct drm_connector *connector)
17808695b612SAlan Cox {
1781367e4408SPatrik Jakobsson 	struct gma_encoder *encoder = gma_attached_encoder(connector);
178237e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
17838695b612SAlan Cox 	struct edid *edid;
17848695b612SAlan Cox 	bool has_audio = false;
1785d112a816SZhao Yakui 	int edp = is_edp(encoder);
1786d112a816SZhao Yakui 
1787d112a816SZhao Yakui 	if (edp)
1788d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_on(encoder);
17898695b612SAlan Cox 
17908695b612SAlan Cox 	edid = drm_get_edid(connector, &intel_dp->adapter);
17918695b612SAlan Cox 	if (edid) {
17928695b612SAlan Cox 		has_audio = drm_detect_monitor_audio(edid);
17938695b612SAlan Cox 		kfree(edid);
17948695b612SAlan Cox 	}
1795d112a816SZhao Yakui 	if (edp)
1796d112a816SZhao Yakui 		cdv_intel_edp_panel_vdd_off(encoder);
17978695b612SAlan Cox 
17988695b612SAlan Cox 	return has_audio;
17998695b612SAlan Cox }
18008695b612SAlan Cox 
18018695b612SAlan Cox static int
cdv_intel_dp_set_property(struct drm_connector * connector,struct drm_property * property,uint64_t val)180237e7b184SAlan Cox cdv_intel_dp_set_property(struct drm_connector *connector,
18038695b612SAlan Cox 		      struct drm_property *property,
18048695b612SAlan Cox 		      uint64_t val)
18058695b612SAlan Cox {
1806f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(connector->dev);
1807367e4408SPatrik Jakobsson 	struct gma_encoder *encoder = gma_attached_encoder(connector);
180837e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp = encoder->dev_priv;
18098695b612SAlan Cox 	int ret;
18108695b612SAlan Cox 
1811a69ac9eaSRob Clark 	ret = drm_object_property_set_value(&connector->base, property, val);
18128695b612SAlan Cox 	if (ret)
18138695b612SAlan Cox 		return ret;
18148695b612SAlan Cox 
18158695b612SAlan Cox 	if (property == dev_priv->force_audio_property) {
18168695b612SAlan Cox 		int i = val;
18178695b612SAlan Cox 		bool has_audio;
18188695b612SAlan Cox 
18198695b612SAlan Cox 		if (i == intel_dp->force_audio)
18208695b612SAlan Cox 			return 0;
18218695b612SAlan Cox 
18228695b612SAlan Cox 		intel_dp->force_audio = i;
18238695b612SAlan Cox 
18248695b612SAlan Cox 		if (i == 0)
182537e7b184SAlan Cox 			has_audio = cdv_intel_dp_detect_audio(connector);
18268695b612SAlan Cox 		else
18278695b612SAlan Cox 			has_audio = i > 0;
18288695b612SAlan Cox 
18298695b612SAlan Cox 		if (has_audio == intel_dp->has_audio)
18308695b612SAlan Cox 			return 0;
18318695b612SAlan Cox 
18328695b612SAlan Cox 		intel_dp->has_audio = has_audio;
18338695b612SAlan Cox 		goto done;
18348695b612SAlan Cox 	}
18358695b612SAlan Cox 
18368695b612SAlan Cox 	if (property == dev_priv->broadcast_rgb_property) {
18378695b612SAlan Cox 		if (val == !!intel_dp->color_range)
18388695b612SAlan Cox 			return 0;
18398695b612SAlan Cox 
18408695b612SAlan Cox 		intel_dp->color_range = val ? DP_COLOR_RANGE_16_235 : 0;
18418695b612SAlan Cox 		goto done;
18428695b612SAlan Cox 	}
18438695b612SAlan Cox 
18448695b612SAlan Cox 	return -EINVAL;
18458695b612SAlan Cox 
18468695b612SAlan Cox done:
184737e7b184SAlan Cox 	if (encoder->base.crtc) {
184837e7b184SAlan Cox 		struct drm_crtc *crtc = encoder->base.crtc;
18498695b612SAlan Cox 		drm_crtc_helper_set_mode(crtc, &crtc->mode,
18508695b612SAlan Cox 					 crtc->x, crtc->y,
1851f4510a27SMatt Roper 					 crtc->primary->fb);
18528695b612SAlan Cox 	}
18538695b612SAlan Cox 
18548695b612SAlan Cox 	return 0;
18558695b612SAlan Cox }
18568695b612SAlan Cox 
18578695b612SAlan Cox static void
cdv_intel_dp_destroy(struct drm_connector * connector)185837e7b184SAlan Cox cdv_intel_dp_destroy(struct drm_connector *connector)
18598695b612SAlan Cox {
186012e67ccaSPatrik Jakobsson 	struct gma_connector *gma_connector = to_gma_connector(connector);
1861367e4408SPatrik Jakobsson 	struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
1862367e4408SPatrik Jakobsson 	struct cdv_intel_dp *intel_dp = gma_encoder->dev_priv;
18638695b612SAlan Cox 
1864367e4408SPatrik Jakobsson 	if (is_edp(gma_encoder)) {
1865d112a816SZhao Yakui 	/*	cdv_intel_panel_destroy_backlight(connector->dev); */
1866d112a816SZhao Yakui 		kfree(intel_dp->panel_fixed_mode);
1867d112a816SZhao Yakui 		intel_dp->panel_fixed_mode = NULL;
1868d112a816SZhao Yakui 	}
18698695b612SAlan Cox 	i2c_del_adapter(&intel_dp->adapter);
18708695b612SAlan Cox 	drm_connector_cleanup(connector);
187112e67ccaSPatrik Jakobsson 	kfree(gma_connector);
18728695b612SAlan Cox }
18738695b612SAlan Cox 
187437e7b184SAlan Cox static const struct drm_encoder_helper_funcs cdv_intel_dp_helper_funcs = {
187537e7b184SAlan Cox 	.dpms = cdv_intel_dp_dpms,
187637e7b184SAlan Cox 	.mode_fixup = cdv_intel_dp_mode_fixup,
187737e7b184SAlan Cox 	.prepare = cdv_intel_dp_prepare,
187837e7b184SAlan Cox 	.mode_set = cdv_intel_dp_mode_set,
187937e7b184SAlan Cox 	.commit = cdv_intel_dp_commit,
18808695b612SAlan Cox };
18818695b612SAlan Cox 
188237e7b184SAlan Cox static const struct drm_connector_funcs cdv_intel_dp_connector_funcs = {
18838695b612SAlan Cox 	.dpms = drm_helper_connector_dpms,
188437e7b184SAlan Cox 	.detect = cdv_intel_dp_detect,
18858695b612SAlan Cox 	.fill_modes = drm_helper_probe_single_connector_modes,
188637e7b184SAlan Cox 	.set_property = cdv_intel_dp_set_property,
188737e7b184SAlan Cox 	.destroy = cdv_intel_dp_destroy,
18888695b612SAlan Cox };
18898695b612SAlan Cox 
189037e7b184SAlan Cox static const struct drm_connector_helper_funcs cdv_intel_dp_connector_helper_funcs = {
189137e7b184SAlan Cox 	.get_modes = cdv_intel_dp_get_modes,
189237e7b184SAlan Cox 	.mode_valid = cdv_intel_dp_mode_valid,
1893c9d49590SPatrik Jakobsson 	.best_encoder = gma_best_encoder,
18948695b612SAlan Cox };
18958695b612SAlan Cox 
cdv_intel_dp_add_properties(struct drm_connector * connector)189637e7b184SAlan Cox static void cdv_intel_dp_add_properties(struct drm_connector *connector)
18978695b612SAlan Cox {
189837e7b184SAlan Cox 	cdv_intel_attach_force_audio_property(connector);
189937e7b184SAlan Cox 	cdv_intel_attach_broadcast_rgb_property(connector);
19008695b612SAlan Cox }
19018695b612SAlan Cox 
1902d112a816SZhao Yakui /* check the VBT to see whether the eDP is on DP-D port */
cdv_intel_dpc_is_edp(struct drm_device * dev)1903d112a816SZhao Yakui static bool cdv_intel_dpc_is_edp(struct drm_device *dev)
1904d112a816SZhao Yakui {
1905f71635e8SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
1906d112a816SZhao Yakui 	struct child_device_config *p_child;
1907d112a816SZhao Yakui 	int i;
1908d112a816SZhao Yakui 
1909d112a816SZhao Yakui 	if (!dev_priv->child_dev_num)
1910d112a816SZhao Yakui 		return false;
1911d112a816SZhao Yakui 
1912d112a816SZhao Yakui 	for (i = 0; i < dev_priv->child_dev_num; i++) {
1913d112a816SZhao Yakui 		p_child = dev_priv->child_dev + i;
1914d112a816SZhao Yakui 
1915d112a816SZhao Yakui 		if (p_child->dvo_port == PORT_IDPC &&
1916d112a816SZhao Yakui 		    p_child->device_type == DEVICE_TYPE_eDP)
1917d112a816SZhao Yakui 			return true;
1918d112a816SZhao Yakui 	}
1919d112a816SZhao Yakui 	return false;
1920d112a816SZhao Yakui }
1921d112a816SZhao Yakui 
19229a9f5786SZhao Yakui /* Cedarview display clock gating
19239a9f5786SZhao Yakui 
19249a9f5786SZhao Yakui    We need this disable dot get correct behaviour while enabling
19259a9f5786SZhao Yakui    DP/eDP. TODO - investigate if we can turn it back to normality
19269a9f5786SZhao Yakui    after enabling */
cdv_disable_intel_clock_gating(struct drm_device * dev)19279a9f5786SZhao Yakui static void cdv_disable_intel_clock_gating(struct drm_device *dev)
19289a9f5786SZhao Yakui {
19299a9f5786SZhao Yakui 	u32 reg_value;
19309a9f5786SZhao Yakui 	reg_value = REG_READ(DSPCLK_GATE_D);
19319a9f5786SZhao Yakui 
19329a9f5786SZhao Yakui 	reg_value |= (DPUNIT_PIPEB_GATE_DISABLE |
19339a9f5786SZhao Yakui 			DPUNIT_PIPEA_GATE_DISABLE |
19349a9f5786SZhao Yakui 			DPCUNIT_CLOCK_GATE_DISABLE |
19359a9f5786SZhao Yakui 			DPLSUNIT_CLOCK_GATE_DISABLE |
19369a9f5786SZhao Yakui 			DPOUNIT_CLOCK_GATE_DISABLE |
19379a9f5786SZhao Yakui 			DPIOUNIT_CLOCK_GATE_DISABLE);
19389a9f5786SZhao Yakui 
19399a9f5786SZhao Yakui 	REG_WRITE(DSPCLK_GATE_D, reg_value);
19409a9f5786SZhao Yakui 
19419a9f5786SZhao Yakui 	udelay(500);
19429a9f5786SZhao Yakui }
19439a9f5786SZhao Yakui 
19448695b612SAlan Cox void
cdv_intel_dp_init(struct drm_device * dev,struct psb_intel_mode_device * mode_dev,int output_reg)194537e7b184SAlan Cox cdv_intel_dp_init(struct drm_device *dev, struct psb_intel_mode_device *mode_dev, int output_reg)
19468695b612SAlan Cox {
1947367e4408SPatrik Jakobsson 	struct gma_encoder *gma_encoder;
1948a3d5d75fSPatrik Jakobsson 	struct gma_connector *gma_connector;
19498695b612SAlan Cox 	struct drm_connector *connector;
19508695b612SAlan Cox 	struct drm_encoder *encoder;
195137e7b184SAlan Cox 	struct cdv_intel_dp *intel_dp;
19528695b612SAlan Cox 	const char *name = NULL;
1953d112a816SZhao Yakui 	int type = DRM_MODE_CONNECTOR_DisplayPort;
19548695b612SAlan Cox 
1955367e4408SPatrik Jakobsson 	gma_encoder = kzalloc(sizeof(struct gma_encoder), GFP_KERNEL);
1956367e4408SPatrik Jakobsson 	if (!gma_encoder)
19578695b612SAlan Cox 		return;
1958a3d5d75fSPatrik Jakobsson         gma_connector = kzalloc(sizeof(struct gma_connector), GFP_KERNEL);
1959a3d5d75fSPatrik Jakobsson         if (!gma_connector)
196037e7b184SAlan Cox                 goto err_connector;
196137e7b184SAlan Cox 	intel_dp = kzalloc(sizeof(struct cdv_intel_dp), GFP_KERNEL);
196237e7b184SAlan Cox 	if (!intel_dp)
196337e7b184SAlan Cox 	        goto err_priv;
19648695b612SAlan Cox 
1965d112a816SZhao Yakui 	if ((output_reg == DP_C) && cdv_intel_dpc_is_edp(dev))
1966d112a816SZhao Yakui 		type = DRM_MODE_CONNECTOR_eDP;
1967d112a816SZhao Yakui 
1968a3d5d75fSPatrik Jakobsson 	connector = &gma_connector->base;
1969367e4408SPatrik Jakobsson 	encoder = &gma_encoder->base;
19708695b612SAlan Cox 
1971d112a816SZhao Yakui 	drm_connector_init(dev, connector, &cdv_intel_dp_connector_funcs, type);
1972d088b69fSThomas Zimmermann 	drm_simple_encoder_init(dev, encoder, DRM_MODE_ENCODER_TMDS);
197337e7b184SAlan Cox 
1974367e4408SPatrik Jakobsson 	gma_connector_attach_encoder(gma_connector, gma_encoder);
1975d112a816SZhao Yakui 
1976d112a816SZhao Yakui 	if (type == DRM_MODE_CONNECTOR_DisplayPort)
1977367e4408SPatrik Jakobsson 		gma_encoder->type = INTEL_OUTPUT_DISPLAYPORT;
1978d112a816SZhao Yakui         else
1979367e4408SPatrik Jakobsson 		gma_encoder->type = INTEL_OUTPUT_EDP;
1980d112a816SZhao Yakui 
198137e7b184SAlan Cox 
1982367e4408SPatrik Jakobsson 	gma_encoder->dev_priv=intel_dp;
1983367e4408SPatrik Jakobsson 	intel_dp->encoder = gma_encoder;
19848695b612SAlan Cox 	intel_dp->output_reg = output_reg;
19858695b612SAlan Cox 
198637e7b184SAlan Cox 	drm_encoder_helper_add(encoder, &cdv_intel_dp_helper_funcs);
198737e7b184SAlan Cox 	drm_connector_helper_add(connector, &cdv_intel_dp_connector_helper_funcs);
19888695b612SAlan Cox 
19898695b612SAlan Cox 	connector->polled = DRM_CONNECTOR_POLL_HPD;
199037e7b184SAlan Cox 	connector->interlace_allowed = false;
199137e7b184SAlan Cox 	connector->doublescan_allowed = false;
19928695b612SAlan Cox 
19938695b612SAlan Cox 	/* Set up the DDC bus. */
19948695b612SAlan Cox 	switch (output_reg) {
19958695b612SAlan Cox 		case DP_B:
19968695b612SAlan Cox 			name = "DPDDC-B";
1997367e4408SPatrik Jakobsson 			gma_encoder->ddi_select = (DP_MASK | DDI0_SELECT);
19988695b612SAlan Cox 			break;
19998695b612SAlan Cox 		case DP_C:
20008695b612SAlan Cox 			name = "DPDDC-C";
2001367e4408SPatrik Jakobsson 			gma_encoder->ddi_select = (DP_MASK | DDI1_SELECT);
20028695b612SAlan Cox 			break;
20038695b612SAlan Cox 	}
20048695b612SAlan Cox 
20059a9f5786SZhao Yakui 	cdv_disable_intel_clock_gating(dev);
20069a9f5786SZhao Yakui 
2007367e4408SPatrik Jakobsson 	cdv_intel_dp_i2c_init(gma_connector, gma_encoder, name);
200837e7b184SAlan Cox         /* FIXME:fail check */
200937e7b184SAlan Cox 	cdv_intel_dp_add_properties(connector);
2010d112a816SZhao Yakui 
2011367e4408SPatrik Jakobsson 	if (is_edp(gma_encoder)) {
2012d112a816SZhao Yakui 		int ret;
2013d112a816SZhao Yakui 		struct edp_power_seq cur;
2014d112a816SZhao Yakui                 u32 pp_on, pp_off, pp_div;
2015d112a816SZhao Yakui 		u32 pwm_ctrl;
2016d112a816SZhao Yakui 
2017d112a816SZhao Yakui 		pp_on = REG_READ(PP_CONTROL);
2018d112a816SZhao Yakui 		pp_on &= ~PANEL_UNLOCK_MASK;
2019d112a816SZhao Yakui 	        pp_on |= PANEL_UNLOCK_REGS;
2020d112a816SZhao Yakui 
2021d112a816SZhao Yakui 		REG_WRITE(PP_CONTROL, pp_on);
2022d112a816SZhao Yakui 
2023d112a816SZhao Yakui 		pwm_ctrl = REG_READ(BLC_PWM_CTL2);
2024d112a816SZhao Yakui 		pwm_ctrl |= PWM_PIPE_B;
2025d112a816SZhao Yakui 		REG_WRITE(BLC_PWM_CTL2, pwm_ctrl);
2026d112a816SZhao Yakui 
2027d112a816SZhao Yakui                 pp_on = REG_READ(PP_ON_DELAYS);
2028d112a816SZhao Yakui                 pp_off = REG_READ(PP_OFF_DELAYS);
2029d112a816SZhao Yakui                 pp_div = REG_READ(PP_DIVISOR);
2030d112a816SZhao Yakui 
2031d112a816SZhao Yakui 		/* Pull timing values out of registers */
2032d112a816SZhao Yakui                 cur.t1_t3 = (pp_on & PANEL_POWER_UP_DELAY_MASK) >>
2033d112a816SZhao Yakui                         PANEL_POWER_UP_DELAY_SHIFT;
2034d112a816SZhao Yakui 
2035d112a816SZhao Yakui                 cur.t8 = (pp_on & PANEL_LIGHT_ON_DELAY_MASK) >>
2036d112a816SZhao Yakui                         PANEL_LIGHT_ON_DELAY_SHIFT;
2037d112a816SZhao Yakui 
2038d112a816SZhao Yakui                 cur.t9 = (pp_off & PANEL_LIGHT_OFF_DELAY_MASK) >>
2039d112a816SZhao Yakui                         PANEL_LIGHT_OFF_DELAY_SHIFT;
2040d112a816SZhao Yakui 
2041d112a816SZhao Yakui                 cur.t10 = (pp_off & PANEL_POWER_DOWN_DELAY_MASK) >>
2042d112a816SZhao Yakui                         PANEL_POWER_DOWN_DELAY_SHIFT;
2043d112a816SZhao Yakui 
2044d112a816SZhao Yakui                 cur.t11_t12 = ((pp_div & PANEL_POWER_CYCLE_DELAY_MASK) >>
2045d112a816SZhao Yakui                                PANEL_POWER_CYCLE_DELAY_SHIFT);
2046d112a816SZhao Yakui 
2047d112a816SZhao Yakui                 DRM_DEBUG_KMS("cur t1_t3 %d t8 %d t9 %d t10 %d t11_t12 %d\n",
2048d112a816SZhao Yakui                               cur.t1_t3, cur.t8, cur.t9, cur.t10, cur.t11_t12);
2049d112a816SZhao Yakui 
2050d112a816SZhao Yakui 
2051d112a816SZhao Yakui 		intel_dp->panel_power_up_delay = cur.t1_t3 / 10;
2052d112a816SZhao Yakui                 intel_dp->backlight_on_delay = cur.t8 / 10;
2053d112a816SZhao Yakui                 intel_dp->backlight_off_delay = cur.t9 / 10;
2054d112a816SZhao Yakui                 intel_dp->panel_power_down_delay = cur.t10 / 10;
2055d112a816SZhao Yakui                 intel_dp->panel_power_cycle_delay = (cur.t11_t12 - 1) * 100;
2056d112a816SZhao Yakui 
2057d112a816SZhao Yakui                 DRM_DEBUG_KMS("panel power up delay %d, power down delay %d, power cycle delay %d\n",
2058d112a816SZhao Yakui                               intel_dp->panel_power_up_delay, intel_dp->panel_power_down_delay,
2059d112a816SZhao Yakui                               intel_dp->panel_power_cycle_delay);
2060d112a816SZhao Yakui 
2061d112a816SZhao Yakui                 DRM_DEBUG_KMS("backlight on delay %d, off delay %d\n",
2062d112a816SZhao Yakui                               intel_dp->backlight_on_delay, intel_dp->backlight_off_delay);
2063d112a816SZhao Yakui 
2064d112a816SZhao Yakui 
2065367e4408SPatrik Jakobsson 		cdv_intel_edp_panel_vdd_on(gma_encoder);
2066367e4408SPatrik Jakobsson 		ret = cdv_intel_dp_aux_native_read(gma_encoder, DP_DPCD_REV,
2067d112a816SZhao Yakui 					       intel_dp->dpcd,
2068d112a816SZhao Yakui 					       sizeof(intel_dp->dpcd));
2069367e4408SPatrik Jakobsson 		cdv_intel_edp_panel_vdd_off(gma_encoder);
2070cdd296cdSTom Rix 		if (ret <= 0) {
2071d112a816SZhao Yakui 			/* if this fails, presume the device is a ghost */
2072d112a816SZhao Yakui 			DRM_INFO("failed to retrieve link info, disabling eDP\n");
2073d088b69fSThomas Zimmermann 			drm_encoder_cleanup(encoder);
2074d112a816SZhao Yakui 			cdv_intel_dp_destroy(connector);
20754e19d51cSTom Rix 			goto err_connector;
2076d112a816SZhao Yakui 		} else {
2077d112a816SZhao Yakui         		DRM_DEBUG_KMS("DPCD: Rev=%x LN_Rate=%x LN_CNT=%x LN_DOWNSP=%x\n",
2078d112a816SZhao Yakui 				intel_dp->dpcd[0], intel_dp->dpcd[1],
2079d112a816SZhao Yakui 				intel_dp->dpcd[2], intel_dp->dpcd[3]);
2080d112a816SZhao Yakui 
2081d112a816SZhao Yakui 		}
2082d112a816SZhao Yakui 		/* The CDV reference driver moves pnale backlight setup into the displays that
2083d112a816SZhao Yakui 		   have a backlight: this is a good idea and one we should probably adopt, however
2084d112a816SZhao Yakui 		   we need to migrate all the drivers before we can do that */
2085d112a816SZhao Yakui                 /*cdv_intel_panel_setup_backlight(dev); */
2086d112a816SZhao Yakui 	}
208737e7b184SAlan Cox 	return;
20888695b612SAlan Cox 
208937e7b184SAlan Cox err_priv:
2090a3d5d75fSPatrik Jakobsson 	kfree(gma_connector);
209137e7b184SAlan Cox err_connector:
2092367e4408SPatrik Jakobsson 	kfree(gma_encoder);
20938695b612SAlan Cox }
2094