1 /**************************************************************************
2  * Copyright (c) 2011, Intel Corporation.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  **************************************************************************/
19 
20 #include <linux/backlight.h>
21 #include <linux/module.h>
22 #include <linux/dmi.h>
23 #include <drm/drmP.h>
24 #include <drm/drm.h>
25 #include "gma_drm.h"
26 #include "psb_drv.h"
27 #include "psb_reg.h"
28 #include "psb_intel_reg.h"
29 #include <asm/mrst.h>
30 #include <asm/intel_scu_ipc.h>
31 #include "mid_bios.h"
32 #include "intel_bios.h"
33 
34 static int oaktrail_output_init(struct drm_device *dev)
35 {
36 	struct drm_psb_private *dev_priv = dev->dev_private;
37 	if (dev_priv->iLVDS_enable)
38 		oaktrail_lvds_init(dev, &dev_priv->mode_dev);
39 	else
40 		dev_err(dev->dev, "DSI is not supported\n");
41 	if (dev_priv->hdmi_priv)
42 		oaktrail_hdmi_init(dev, &dev_priv->mode_dev);
43 	return 0;
44 }
45 
46 /*
47  *	Provide the low level interfaces for the Moorestown backlight
48  */
49 
50 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
51 
52 #define MRST_BLC_MAX_PWM_REG_FREQ	    0xFFFF
53 #define BLC_PWM_PRECISION_FACTOR 100	/* 10000000 */
54 #define BLC_PWM_FREQ_CALC_CONSTANT 32
55 #define MHz 1000000
56 #define BLC_ADJUSTMENT_MAX 100
57 
58 static struct backlight_device *oaktrail_backlight_device;
59 static int oaktrail_brightness;
60 
61 static int oaktrail_set_brightness(struct backlight_device *bd)
62 {
63 	struct drm_device *dev = bl_get_data(oaktrail_backlight_device);
64 	struct drm_psb_private *dev_priv = dev->dev_private;
65 	int level = bd->props.brightness;
66 	u32 blc_pwm_ctl;
67 	u32 max_pwm_blc;
68 
69 	/* Percentage 1-100% being valid */
70 	if (level < 1)
71 		level = 1;
72 
73 	if (gma_power_begin(dev, 0)) {
74 		/* Calculate and set the brightness value */
75 		max_pwm_blc = REG_READ(BLC_PWM_CTL) >> 16;
76 		blc_pwm_ctl = level * max_pwm_blc / 100;
77 
78 		/* Adjust the backlight level with the percent in
79 		 * dev_priv->blc_adj1;
80 		 */
81 		blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj1;
82 		blc_pwm_ctl = blc_pwm_ctl / 100;
83 
84 		/* Adjust the backlight level with the percent in
85 		 * dev_priv->blc_adj2;
86 		 */
87 		blc_pwm_ctl = blc_pwm_ctl * dev_priv->blc_adj2;
88 		blc_pwm_ctl = blc_pwm_ctl / 100;
89 
90 		/* force PWM bit on */
91 		REG_WRITE(BLC_PWM_CTL2, (0x80000000 | REG_READ(BLC_PWM_CTL2)));
92 		REG_WRITE(BLC_PWM_CTL, (max_pwm_blc << 16) | blc_pwm_ctl);
93 		gma_power_end(dev);
94 	}
95 	oaktrail_brightness = level;
96 	return 0;
97 }
98 
99 static int oaktrail_get_brightness(struct backlight_device *bd)
100 {
101 	/* return locally cached var instead of HW read (due to DPST etc.) */
102 	/* FIXME: ideally return actual value in case firmware fiddled with
103 	   it */
104 	return oaktrail_brightness;
105 }
106 
107 static int device_backlight_init(struct drm_device *dev)
108 {
109 	struct drm_psb_private *dev_priv = dev->dev_private;
110 	unsigned long core_clock;
111 	u16 bl_max_freq;
112 	uint32_t value;
113 	uint32_t blc_pwm_precision_factor;
114 
115 	dev_priv->blc_adj1 = BLC_ADJUSTMENT_MAX;
116 	dev_priv->blc_adj2 = BLC_ADJUSTMENT_MAX;
117 	bl_max_freq = 256;
118 	/* this needs to be set elsewhere */
119 	blc_pwm_precision_factor = BLC_PWM_PRECISION_FACTOR;
120 
121 	core_clock = dev_priv->core_freq;
122 
123 	value = (core_clock * MHz) / BLC_PWM_FREQ_CALC_CONSTANT;
124 	value *= blc_pwm_precision_factor;
125 	value /= bl_max_freq;
126 	value /= blc_pwm_precision_factor;
127 
128 	if (value > (unsigned long long)MRST_BLC_MAX_PWM_REG_FREQ)
129 			return -ERANGE;
130 
131 	if (gma_power_begin(dev, false)) {
132 		REG_WRITE(BLC_PWM_CTL2, (0x80000000 | REG_READ(BLC_PWM_CTL2)));
133 		REG_WRITE(BLC_PWM_CTL, value | (value << 16));
134 		gma_power_end(dev);
135 	}
136 	return 0;
137 }
138 
139 static const struct backlight_ops oaktrail_ops = {
140 	.get_brightness = oaktrail_get_brightness,
141 	.update_status  = oaktrail_set_brightness,
142 };
143 
144 int oaktrail_backlight_init(struct drm_device *dev)
145 {
146 	struct drm_psb_private *dev_priv = dev->dev_private;
147 	int ret;
148 	struct backlight_properties props;
149 
150 	memset(&props, 0, sizeof(struct backlight_properties));
151 	props.max_brightness = 100;
152 	props.type = BACKLIGHT_PLATFORM;
153 
154 	oaktrail_backlight_device = backlight_device_register("oaktrail-bl",
155 				NULL, (void *)dev, &oaktrail_ops, &props);
156 
157 	if (IS_ERR(oaktrail_backlight_device))
158 		return PTR_ERR(oaktrail_backlight_device);
159 
160 	ret = device_backlight_init(dev);
161 	if (ret < 0) {
162 		backlight_device_unregister(oaktrail_backlight_device);
163 		return ret;
164 	}
165 	oaktrail_backlight_device->props.brightness = 100;
166 	oaktrail_backlight_device->props.max_brightness = 100;
167 	backlight_update_status(oaktrail_backlight_device);
168 	dev_priv->backlight_device = oaktrail_backlight_device;
169 	return 0;
170 }
171 
172 #endif
173 
174 /*
175  *	Provide the Moorestown specific chip logic and low level methods
176  *	for power management
177  */
178 
179 static void oaktrail_init_pm(struct drm_device *dev)
180 {
181 }
182 
183 /**
184  *	oaktrail_save_display_registers	-	save registers lost on suspend
185  *	@dev: our DRM device
186  *
187  *	Save the state we need in order to be able to restore the interface
188  *	upon resume from suspend
189  */
190 static int oaktrail_save_display_registers(struct drm_device *dev)
191 {
192 	struct drm_psb_private *dev_priv = dev->dev_private;
193 	struct psb_state *regs = &dev_priv->regs;
194 	int i;
195 	u32 pp_stat;
196 
197 	/* Display arbitration control + watermarks */
198 	regs->saveDSPARB = PSB_RVDC32(DSPARB);
199 	regs->saveDSPFW1 = PSB_RVDC32(DSPFW1);
200 	regs->saveDSPFW2 = PSB_RVDC32(DSPFW2);
201 	regs->saveDSPFW3 = PSB_RVDC32(DSPFW3);
202 	regs->saveDSPFW4 = PSB_RVDC32(DSPFW4);
203 	regs->saveDSPFW5 = PSB_RVDC32(DSPFW5);
204 	regs->saveDSPFW6 = PSB_RVDC32(DSPFW6);
205 	regs->saveCHICKENBIT = PSB_RVDC32(DSPCHICKENBIT);
206 
207 	/* Pipe & plane A info */
208 	regs->savePIPEACONF = PSB_RVDC32(PIPEACONF);
209 	regs->savePIPEASRC = PSB_RVDC32(PIPEASRC);
210 	regs->saveFPA0 = PSB_RVDC32(MRST_FPA0);
211 	regs->saveFPA1 = PSB_RVDC32(MRST_FPA1);
212 	regs->saveDPLL_A = PSB_RVDC32(MRST_DPLL_A);
213 	regs->saveHTOTAL_A = PSB_RVDC32(HTOTAL_A);
214 	regs->saveHBLANK_A = PSB_RVDC32(HBLANK_A);
215 	regs->saveHSYNC_A = PSB_RVDC32(HSYNC_A);
216 	regs->saveVTOTAL_A = PSB_RVDC32(VTOTAL_A);
217 	regs->saveVBLANK_A = PSB_RVDC32(VBLANK_A);
218 	regs->saveVSYNC_A = PSB_RVDC32(VSYNC_A);
219 	regs->saveBCLRPAT_A = PSB_RVDC32(BCLRPAT_A);
220 	regs->saveDSPACNTR = PSB_RVDC32(DSPACNTR);
221 	regs->saveDSPASTRIDE = PSB_RVDC32(DSPASTRIDE);
222 	regs->saveDSPAADDR = PSB_RVDC32(DSPABASE);
223 	regs->saveDSPASURF = PSB_RVDC32(DSPASURF);
224 	regs->saveDSPALINOFF = PSB_RVDC32(DSPALINOFF);
225 	regs->saveDSPATILEOFF = PSB_RVDC32(DSPATILEOFF);
226 
227 	/* Save cursor regs */
228 	regs->saveDSPACURSOR_CTRL = PSB_RVDC32(CURACNTR);
229 	regs->saveDSPACURSOR_BASE = PSB_RVDC32(CURABASE);
230 	regs->saveDSPACURSOR_POS = PSB_RVDC32(CURAPOS);
231 
232 	/* Save palette (gamma) */
233 	for (i = 0; i < 256; i++)
234 		regs->save_palette_a[i] = PSB_RVDC32(PALETTE_A + (i << 2));
235 
236 	if (dev_priv->hdmi_priv)
237 		oaktrail_hdmi_save(dev);
238 
239 	/* Save performance state */
240 	regs->savePERF_MODE = PSB_RVDC32(MRST_PERF_MODE);
241 
242 	/* LVDS state */
243 	regs->savePP_CONTROL = PSB_RVDC32(PP_CONTROL);
244 	regs->savePFIT_PGM_RATIOS = PSB_RVDC32(PFIT_PGM_RATIOS);
245 	regs->savePFIT_AUTO_RATIOS = PSB_RVDC32(PFIT_AUTO_RATIOS);
246 	regs->saveBLC_PWM_CTL = PSB_RVDC32(BLC_PWM_CTL);
247 	regs->saveBLC_PWM_CTL2 = PSB_RVDC32(BLC_PWM_CTL2);
248 	regs->saveLVDS = PSB_RVDC32(LVDS);
249 	regs->savePFIT_CONTROL = PSB_RVDC32(PFIT_CONTROL);
250 	regs->savePP_ON_DELAYS = PSB_RVDC32(LVDSPP_ON);
251 	regs->savePP_OFF_DELAYS = PSB_RVDC32(LVDSPP_OFF);
252 	regs->savePP_DIVISOR = PSB_RVDC32(PP_CYCLE);
253 
254 	/* HW overlay */
255 	regs->saveOV_OVADD = PSB_RVDC32(OV_OVADD);
256 	regs->saveOV_OGAMC0 = PSB_RVDC32(OV_OGAMC0);
257 	regs->saveOV_OGAMC1 = PSB_RVDC32(OV_OGAMC1);
258 	regs->saveOV_OGAMC2 = PSB_RVDC32(OV_OGAMC2);
259 	regs->saveOV_OGAMC3 = PSB_RVDC32(OV_OGAMC3);
260 	regs->saveOV_OGAMC4 = PSB_RVDC32(OV_OGAMC4);
261 	regs->saveOV_OGAMC5 = PSB_RVDC32(OV_OGAMC5);
262 
263 	/* DPST registers */
264 	regs->saveHISTOGRAM_INT_CONTROL_REG =
265 					PSB_RVDC32(HISTOGRAM_INT_CONTROL);
266 	regs->saveHISTOGRAM_LOGIC_CONTROL_REG =
267 					PSB_RVDC32(HISTOGRAM_LOGIC_CONTROL);
268 	regs->savePWM_CONTROL_LOGIC = PSB_RVDC32(PWM_CONTROL_LOGIC);
269 
270 	if (dev_priv->iLVDS_enable) {
271 		/* Shut down the panel */
272 		PSB_WVDC32(0, PP_CONTROL);
273 
274 		do {
275 			pp_stat = PSB_RVDC32(PP_STATUS);
276 		} while (pp_stat & 0x80000000);
277 
278 		/* Turn off the plane */
279 		PSB_WVDC32(0x58000000, DSPACNTR);
280 		/* Trigger the plane disable */
281 		PSB_WVDC32(0, DSPASURF);
282 
283 		/* Wait ~4 ticks */
284 		msleep(4);
285 
286 		/* Turn off pipe */
287 		PSB_WVDC32(0x0, PIPEACONF);
288 		/* Wait ~8 ticks */
289 		msleep(8);
290 
291 		/* Turn off PLLs */
292 		PSB_WVDC32(0, MRST_DPLL_A);
293 	}
294 	return 0;
295 }
296 
297 /**
298  *	oaktrail_restore_display_registers	-	restore lost register state
299  *	@dev: our DRM device
300  *
301  *	Restore register state that was lost during suspend and resume.
302  */
303 static int oaktrail_restore_display_registers(struct drm_device *dev)
304 {
305 	struct drm_psb_private *dev_priv = dev->dev_private;
306 	struct psb_state *regs = &dev_priv->regs;
307 	u32 pp_stat;
308 	int i;
309 
310 	/* Display arbitration + watermarks */
311 	PSB_WVDC32(regs->saveDSPARB, DSPARB);
312 	PSB_WVDC32(regs->saveDSPFW1, DSPFW1);
313 	PSB_WVDC32(regs->saveDSPFW2, DSPFW2);
314 	PSB_WVDC32(regs->saveDSPFW3, DSPFW3);
315 	PSB_WVDC32(regs->saveDSPFW4, DSPFW4);
316 	PSB_WVDC32(regs->saveDSPFW5, DSPFW5);
317 	PSB_WVDC32(regs->saveDSPFW6, DSPFW6);
318 	PSB_WVDC32(regs->saveCHICKENBIT, DSPCHICKENBIT);
319 
320 	/* Make sure VGA plane is off. it initializes to on after reset!*/
321 	PSB_WVDC32(0x80000000, VGACNTRL);
322 
323 	/* set the plls */
324 	PSB_WVDC32(regs->saveFPA0, MRST_FPA0);
325 	PSB_WVDC32(regs->saveFPA1, MRST_FPA1);
326 
327 	/* Actually enable it */
328 	PSB_WVDC32(regs->saveDPLL_A, MRST_DPLL_A);
329 	DRM_UDELAY(150);
330 
331 	/* Restore mode */
332 	PSB_WVDC32(regs->saveHTOTAL_A, HTOTAL_A);
333 	PSB_WVDC32(regs->saveHBLANK_A, HBLANK_A);
334 	PSB_WVDC32(regs->saveHSYNC_A, HSYNC_A);
335 	PSB_WVDC32(regs->saveVTOTAL_A, VTOTAL_A);
336 	PSB_WVDC32(regs->saveVBLANK_A, VBLANK_A);
337 	PSB_WVDC32(regs->saveVSYNC_A, VSYNC_A);
338 	PSB_WVDC32(regs->savePIPEASRC, PIPEASRC);
339 	PSB_WVDC32(regs->saveBCLRPAT_A, BCLRPAT_A);
340 
341 	/* Restore performance mode*/
342 	PSB_WVDC32(regs->savePERF_MODE, MRST_PERF_MODE);
343 
344 	/* Enable the pipe*/
345 	if (dev_priv->iLVDS_enable)
346 		PSB_WVDC32(regs->savePIPEACONF, PIPEACONF);
347 
348 	/* Set up the plane*/
349 	PSB_WVDC32(regs->saveDSPALINOFF, DSPALINOFF);
350 	PSB_WVDC32(regs->saveDSPASTRIDE, DSPASTRIDE);
351 	PSB_WVDC32(regs->saveDSPATILEOFF, DSPATILEOFF);
352 
353 	/* Enable the plane */
354 	PSB_WVDC32(regs->saveDSPACNTR, DSPACNTR);
355 	PSB_WVDC32(regs->saveDSPASURF, DSPASURF);
356 
357 	/* Enable Cursor A */
358 	PSB_WVDC32(regs->saveDSPACURSOR_CTRL, CURACNTR);
359 	PSB_WVDC32(regs->saveDSPACURSOR_POS, CURAPOS);
360 	PSB_WVDC32(regs->saveDSPACURSOR_BASE, CURABASE);
361 
362 	/* Restore palette (gamma) */
363 	for (i = 0; i < 256; i++)
364 		PSB_WVDC32(regs->save_palette_a[i], PALETTE_A + (i << 2));
365 
366 	if (dev_priv->hdmi_priv)
367 		oaktrail_hdmi_restore(dev);
368 
369 	if (dev_priv->iLVDS_enable) {
370 		PSB_WVDC32(regs->saveBLC_PWM_CTL2, BLC_PWM_CTL2);
371 		PSB_WVDC32(regs->saveLVDS, LVDS); /*port 61180h*/
372 		PSB_WVDC32(regs->savePFIT_CONTROL, PFIT_CONTROL);
373 		PSB_WVDC32(regs->savePFIT_PGM_RATIOS, PFIT_PGM_RATIOS);
374 		PSB_WVDC32(regs->savePFIT_AUTO_RATIOS, PFIT_AUTO_RATIOS);
375 		PSB_WVDC32(regs->saveBLC_PWM_CTL, BLC_PWM_CTL);
376 		PSB_WVDC32(regs->savePP_ON_DELAYS, LVDSPP_ON);
377 		PSB_WVDC32(regs->savePP_OFF_DELAYS, LVDSPP_OFF);
378 		PSB_WVDC32(regs->savePP_DIVISOR, PP_CYCLE);
379 		PSB_WVDC32(regs->savePP_CONTROL, PP_CONTROL);
380 	}
381 
382 	/* Wait for cycle delay */
383 	do {
384 		pp_stat = PSB_RVDC32(PP_STATUS);
385 	} while (pp_stat & 0x08000000);
386 
387 	/* Wait for panel power up */
388 	do {
389 		pp_stat = PSB_RVDC32(PP_STATUS);
390 	} while (pp_stat & 0x10000000);
391 
392 	/* Restore HW overlay */
393 	PSB_WVDC32(regs->saveOV_OVADD, OV_OVADD);
394 	PSB_WVDC32(regs->saveOV_OGAMC0, OV_OGAMC0);
395 	PSB_WVDC32(regs->saveOV_OGAMC1, OV_OGAMC1);
396 	PSB_WVDC32(regs->saveOV_OGAMC2, OV_OGAMC2);
397 	PSB_WVDC32(regs->saveOV_OGAMC3, OV_OGAMC3);
398 	PSB_WVDC32(regs->saveOV_OGAMC4, OV_OGAMC4);
399 	PSB_WVDC32(regs->saveOV_OGAMC5, OV_OGAMC5);
400 
401 	/* DPST registers */
402 	PSB_WVDC32(regs->saveHISTOGRAM_INT_CONTROL_REG,
403 						HISTOGRAM_INT_CONTROL);
404 	PSB_WVDC32(regs->saveHISTOGRAM_LOGIC_CONTROL_REG,
405 						HISTOGRAM_LOGIC_CONTROL);
406 	PSB_WVDC32(regs->savePWM_CONTROL_LOGIC, PWM_CONTROL_LOGIC);
407 
408 	return 0;
409 }
410 
411 /**
412  *	oaktrail_power_down	-	power down the display island
413  *	@dev: our DRM device
414  *
415  *	Power down the display interface of our device
416  */
417 static int oaktrail_power_down(struct drm_device *dev)
418 {
419 	struct drm_psb_private *dev_priv = dev->dev_private;
420 	u32 pwr_mask ;
421 	u32 pwr_sts;
422 
423 	pwr_mask = PSB_PWRGT_DISPLAY_MASK;
424 	outl(pwr_mask, dev_priv->ospm_base + PSB_PM_SSC);
425 
426 	while (true) {
427 		pwr_sts = inl(dev_priv->ospm_base + PSB_PM_SSS);
428 		if ((pwr_sts & pwr_mask) == pwr_mask)
429 			break;
430 		else
431 			udelay(10);
432 	}
433 	return 0;
434 }
435 
436 /*
437  * oaktrail_power_up
438  *
439  * Restore power to the specified island(s) (powergating)
440  */
441 static int oaktrail_power_up(struct drm_device *dev)
442 {
443 	struct drm_psb_private *dev_priv = dev->dev_private;
444 	u32 pwr_mask = PSB_PWRGT_DISPLAY_MASK;
445 	u32 pwr_sts, pwr_cnt;
446 
447 	pwr_cnt = inl(dev_priv->ospm_base + PSB_PM_SSC);
448 	pwr_cnt &= ~pwr_mask;
449 	outl(pwr_cnt, (dev_priv->ospm_base + PSB_PM_SSC));
450 
451 	while (true) {
452 		pwr_sts = inl(dev_priv->ospm_base + PSB_PM_SSS);
453 		if ((pwr_sts & pwr_mask) == 0)
454 			break;
455 		else
456 			udelay(10);
457 	}
458 	return 0;
459 }
460 
461 
462 static int oaktrail_chip_setup(struct drm_device *dev)
463 {
464 	struct drm_psb_private *dev_priv = dev->dev_private;
465 	struct oaktrail_vbt *vbt = &dev_priv->vbt_data;
466 	int ret;
467 
468 	ret = mid_chip_setup(dev);
469 	if (ret < 0)
470 		return ret;
471 	if (vbt->size == 0) {
472 		/* Now pull the BIOS data */
473 		gma_intel_opregion_init(dev);
474 		psb_intel_init_bios(dev);
475 	}
476 	return 0;
477 }
478 
479 static void oaktrail_teardown(struct drm_device *dev)
480 {
481 	struct drm_psb_private *dev_priv = dev->dev_private;
482 	struct oaktrail_vbt *vbt = &dev_priv->vbt_data;
483 
484 	oaktrail_hdmi_teardown(dev);
485 	if (vbt->size == 0)
486 		psb_intel_destroy_bios(dev);
487 }
488 
489 const struct psb_ops oaktrail_chip_ops = {
490 	.name = "Oaktrail",
491 	.accel_2d = 1,
492 	.pipes = 2,
493 	.crtcs = 2,
494 	.sgx_offset = MRST_SGX_OFFSET,
495 
496 	.chip_setup = oaktrail_chip_setup,
497 	.chip_teardown = oaktrail_teardown,
498 	.crtc_helper = &oaktrail_helper_funcs,
499 	.crtc_funcs = &psb_intel_crtc_funcs,
500 
501 	.output_init = oaktrail_output_init,
502 
503 #ifdef CONFIG_BACKLIGHT_CLASS_DEVICE
504 	.backlight_init = oaktrail_backlight_init,
505 #endif
506 
507 	.init_pm = oaktrail_init_pm,
508 	.save_regs = oaktrail_save_display_registers,
509 	.restore_regs = oaktrail_restore_display_registers,
510 	.power_down = oaktrail_power_down,
511 	.power_up = oaktrail_power_up,
512 
513 	.i2c_bus = 1,
514 };
515