dce_abm.c 14.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright 2012-16 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: AMD
 *
 */

26 27
#include <linux/slab.h>

28 29 30
#include "dce_abm.h"
#include "dm_services.h"
#include "reg_helper.h"
31
#include "fixed31_32.h"
32 33
#include "dc.h"

34 35 36
#include "atom.h"


37 38 39 40 41 42 43 44 45 46
#define TO_DCE_ABM(abm)\
	container_of(abm, struct dce_abm, base)

#define REG(reg) \
	(abm_dce->regs->reg)

#undef FN
#define FN(reg_name, field_name) \
	abm_dce->abm_shift->field_name, abm_dce->abm_mask->field_name

47 48
#define DC_LOGGER \
	abm->ctx->logger
49 50 51 52
#define CTX \
	abm_dce->base.ctx

#define MCP_ABM_LEVEL_SET 0x65
53 54 55
#define MCP_ABM_PIPE_SET 0x66
#define MCP_BL_SET 0x67

56 57
#define MCP_DISABLE_ABM_IMMEDIATELY 255

58
static bool dce_abm_set_pipe(struct abm *abm, uint32_t controller_id, uint32_t panel_inst)
59 60 61 62
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);
	uint32_t rampingBoundary = 0xFFFF;

63 64 65
	if (abm->dmcu_is_running == false)
		return true;

66 67 68 69 70 71 72 73 74 75 76 77 78 79
	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
			1, 80000);

	/* set ramping boundary */
	REG_WRITE(MASTER_COMM_DATA_REG1, rampingBoundary);

	/* setDMCUParam_Pipe */
	REG_UPDATE_2(MASTER_COMM_CMD_REG,
			MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_PIPE_SET,
			MASTER_COMM_CMD_REG_BYTE1, controller_id);

	/* notifyDMCUMsg */
	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);

80 81 82
	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
			1, 80000);

83 84
	return true;
}
85

86
static unsigned int calculate_16_bit_backlight_from_pwm(struct dce_abm *abm_dce)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
{
	uint64_t current_backlight;
	uint32_t round_result;
	uint32_t pwm_period_cntl, bl_period, bl_int_count;
	uint32_t bl_pwm_cntl, bl_pwm, fractional_duty_cycle_en;
	uint32_t bl_period_mask, bl_pwm_mask;

	pwm_period_cntl = REG_READ(BL_PWM_PERIOD_CNTL);
	REG_GET(BL_PWM_PERIOD_CNTL, BL_PWM_PERIOD, &bl_period);
	REG_GET(BL_PWM_PERIOD_CNTL, BL_PWM_PERIOD_BITCNT, &bl_int_count);

	bl_pwm_cntl = REG_READ(BL_PWM_CNTL);
	REG_GET(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, (uint32_t *)(&bl_pwm));
	REG_GET(BL_PWM_CNTL, BL_PWM_FRACTIONAL_EN, &fractional_duty_cycle_en);

	if (bl_int_count == 0)
		bl_int_count = 16;

	bl_period_mask = (1 << bl_int_count) - 1;
	bl_period &= bl_period_mask;

	bl_pwm_mask = bl_period_mask << (16 - bl_int_count);

	if (fractional_duty_cycle_en == 0)
		bl_pwm &= bl_pwm_mask;
	else
		bl_pwm &= 0xFFFF;

	current_backlight = bl_pwm << (1 + bl_int_count);

	if (bl_period == 0)
		bl_period = 0xFFFF;

120
	current_backlight = div_u64(current_backlight, bl_period);
121 122 123 124 125 126 127 128 129 130 131 132 133 134
	current_backlight = (current_backlight + 1) >> 1;

	current_backlight = (uint64_t)(current_backlight) * bl_period;

	round_result = (uint32_t)(current_backlight & 0xFFFFFFFF);

	round_result = (round_result >> (bl_int_count-1)) & 1;

	current_backlight >>= bl_int_count;
	current_backlight += round_result;

	return (uint32_t)(current_backlight);
}

135 136
static void driver_set_backlight_level(struct dce_abm *abm_dce,
		uint32_t backlight_pwm_u16_16)
137 138 139 140 141 142 143 144
{
	uint32_t backlight_16bit;
	uint32_t masked_pwm_period;
	uint8_t bit_count;
	uint64_t active_duty_cycle;
	uint32_t pwm_period_bitcnt;

	/*
145
	 * 1. Find  16 bit backlight active duty cycle, where 0 <= backlight
146 147 148
	 * active duty cycle <= backlight period
	 */

149
	/* 1.1 Apply bitmask for backlight period value based on value of BITCNT
150 151 152 153 154 155 156 157 158 159 160 161 162
	 */
	REG_GET_2(BL_PWM_PERIOD_CNTL,
			BL_PWM_PERIOD_BITCNT, &pwm_period_bitcnt,
			BL_PWM_PERIOD, &masked_pwm_period);

	if (pwm_period_bitcnt == 0)
		bit_count = 16;
	else
		bit_count = pwm_period_bitcnt;

	/* e.g. maskedPwmPeriod = 0x24 when bitCount is 6 */
	masked_pwm_period = masked_pwm_period & ((1 << bit_count) - 1);

163
	/* 1.2 Calculate integer active duty cycle required upper 16 bits
164 165 166
	 * contain integer component, lower 16 bits contain fractional component
	 * of active duty cycle e.g. 0x21BDC0 = 0xEFF0 * 0x24
	 */
167
	active_duty_cycle = backlight_pwm_u16_16 * masked_pwm_period;
168

169
	/* 1.3 Calculate 16 bit active duty cycle from integer and fractional
170 171 172 173 174 175 176 177
	 * components shift by bitCount then mask 16 bits and add rounding bit
	 * from MSB of fraction e.g. 0x86F7 = ((0x21BDC0 >> 6) & 0xFFF) + 0
	 */
	backlight_16bit = active_duty_cycle >> bit_count;
	backlight_16bit &= 0xFFFF;
	backlight_16bit += (active_duty_cycle >> (bit_count - 1)) & 0x1;

	/*
178
	 * 2. Program register with updated value
179 180
	 */

181
	/* 2.1 Lock group 2 backlight registers */
182 183 184 185 186

	REG_UPDATE_2(BL_PWM_GRP1_REG_LOCK,
			BL_PWM_GRP1_IGNORE_MASTER_LOCK_EN, 1,
			BL_PWM_GRP1_REG_LOCK, 1);

187
	// 2.2 Write new active duty cycle
188 189
	REG_UPDATE(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, backlight_16bit);

190
	/* 2.3 Unlock group 2 backlight registers */
191 192 193
	REG_UPDATE(BL_PWM_GRP1_REG_LOCK,
			BL_PWM_GRP1_REG_LOCK, 0);

194
	/* 3 Wait for pending bit to be cleared */
195 196 197
	REG_WAIT(BL_PWM_GRP1_REG_LOCK,
			BL_PWM_GRP1_REG_UPDATE_PENDING, 0,
			1, 10000);
198 199 200 201
}

static void dmcu_set_backlight_level(
	struct dce_abm *abm_dce,
202
	uint32_t backlight_pwm_u16_16,
203
	uint32_t frame_ramp,
204 205
	uint32_t controller_id,
	uint32_t panel_id)
206
{
207
	unsigned int backlight_8_bit = 0;
208 209
	uint32_t s2;

210 211 212 213 214 215 216
	if (backlight_pwm_u16_16 & 0x10000)
		// Check for max backlight condition
		backlight_8_bit = 0xFF;
	else
		// Take MSB of fractional part since backlight is not max
		backlight_8_bit = (backlight_pwm_u16_16 >> 8) & 0xFF;

217
	dce_abm_set_pipe(&abm_dce->base, controller_id, panel_id);
218

219 220
	/* waitDMCUReadyForCmd */
	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT,
221
			0, 1, 80000);
222 223

	/* setDMCUParam_BL */
224
	REG_UPDATE(BL1_PWM_USER_LEVEL, BL1_PWM_USER_LEVEL, backlight_pwm_u16_16);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

	/* write ramp */
	if (controller_id == 0)
		frame_ramp = 0;
	REG_WRITE(MASTER_COMM_DATA_REG1, frame_ramp);

	/* setDMCUParam_Cmd */
	REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, MCP_BL_SET);

	/* notifyDMCUMsg */
	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);

	/* UpdateRequestedBacklightLevel */
	s2 = REG_READ(BIOS_SCRATCH_2);

	s2 &= ~ATOM_S2_CURRENT_BL_LEVEL_MASK;
241
	backlight_8_bit &= (ATOM_S2_CURRENT_BL_LEVEL_MASK >>
242
				ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
243
	s2 |= (backlight_8_bit << ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
244 245

	REG_WRITE(BIOS_SCRATCH_2, s2);
246 247 248 249

	/* waitDMCUReadyForCmd */
	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT,
			0, 1, 80000);
250 251 252
}

static void dce_abm_init(struct abm *abm)
253 254
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);
255
	unsigned int backlight = calculate_16_bit_backlight_from_pwm(abm_dce);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291

	REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x103);
	REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x101);
	REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x103);
	REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x101);
	REG_WRITE(BL1_PWM_BL_UPDATE_SAMPLE_RATE, 0x101);

	REG_SET_3(DC_ABM1_HG_MISC_CTRL, 0,
			ABM1_HG_NUM_OF_BINS_SEL, 0,
			ABM1_HG_VMAX_SEL, 1,
			ABM1_HG_BIN_BITWIDTH_SIZE_SEL, 0);

	REG_SET_3(DC_ABM1_IPCSC_COEFF_SEL, 0,
			ABM1_IPCSC_COEFF_SEL_R, 2,
			ABM1_IPCSC_COEFF_SEL_G, 4,
			ABM1_IPCSC_COEFF_SEL_B, 2);

	REG_UPDATE(BL1_PWM_CURRENT_ABM_LEVEL,
			BL1_PWM_CURRENT_ABM_LEVEL, backlight);

	REG_UPDATE(BL1_PWM_TARGET_ABM_LEVEL,
			BL1_PWM_TARGET_ABM_LEVEL, backlight);

	REG_UPDATE(BL1_PWM_USER_LEVEL,
			BL1_PWM_USER_LEVEL, backlight);

	REG_UPDATE_2(DC_ABM1_LS_MIN_MAX_PIXEL_VALUE_THRES,
			ABM1_LS_MIN_PIXEL_VALUE_THRES, 0,
			ABM1_LS_MAX_PIXEL_VALUE_THRES, 1000);

	REG_SET_3(DC_ABM1_HGLS_REG_READ_PROGRESS, 0,
			ABM1_HG_REG_READ_MISSED_FRAME_CLEAR, 1,
			ABM1_LS_REG_READ_MISSED_FRAME_CLEAR, 1,
			ABM1_BL_REG_READ_MISSED_FRAME_CLEAR, 1);
}

292
static unsigned int dce_abm_get_current_backlight(struct abm *abm)
293 294 295 296
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);
	unsigned int backlight = REG_READ(BL1_PWM_CURRENT_ABM_LEVEL);

297 298 299 300
	/* return backlight in hardware format which is unsigned 17 bits, with
	 * 1 bit integer and 16 bit fractional
	 */
	return backlight;
301 302
}

303 304 305 306 307 308 309 310 311 312 313
static unsigned int dce_abm_get_target_backlight(struct abm *abm)
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);
	unsigned int backlight = REG_READ(BL1_PWM_TARGET_ABM_LEVEL);

	/* return backlight in hardware format which is unsigned 17 bits, with
	 * 1 bit integer and 16 bit fractional
	 */
	return backlight;
}

314
static bool dce_abm_set_level(struct abm *abm, uint32_t level)
315 316 317
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);

318 319 320
	if (abm->dmcu_is_running == false)
		return true;

321
	REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
322
			1, 80000);
323 324 325 326 327 328 329 330 331 332 333 334

	/* setDMCUParam_ABMLevel */
	REG_UPDATE_2(MASTER_COMM_CMD_REG,
			MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_LEVEL_SET,
			MASTER_COMM_CMD_REG_BYTE2, level);

	/* notifyDMCUMsg */
	REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);

	return true;
}

335
static bool dce_abm_immediate_disable(struct abm *abm, uint32_t panel_inst)
336 337 338
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);

339 340 341
	if (abm->dmcu_is_running == false)
		return true;

342
	dce_abm_set_pipe(abm, MCP_DISABLE_ABM_IMMEDIATELY, panel_inst);
343

344 345 346 347 348 349 350 351 352
	abm->stored_backlight_registers.BL_PWM_CNTL =
		REG_READ(BL_PWM_CNTL);
	abm->stored_backlight_registers.BL_PWM_CNTL2 =
		REG_READ(BL_PWM_CNTL2);
	abm->stored_backlight_registers.BL_PWM_PERIOD_CNTL =
		REG_READ(BL_PWM_PERIOD_CNTL);

	REG_GET(LVTMA_PWRSEQ_REF_DIV, BL_PWM_REF_DIV,
		&abm->stored_backlight_registers.LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV);
353 354 355
	return true;
}

356 357 358 359 360 361 362 363 364 365 366
static bool dce_abm_init_backlight(struct abm *abm)
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);
	uint32_t value;

	/* It must not be 0, so we have to restore them
	 * Bios bug w/a - period resets to zero,
	 * restoring to cache values which is always correct
	 */
	REG_GET(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, &value);
	if (value == 0 || value == 1) {
367
		if (abm->stored_backlight_registers.BL_PWM_CNTL != 0) {
368
			REG_WRITE(BL_PWM_CNTL,
369
				abm->stored_backlight_registers.BL_PWM_CNTL);
370
			REG_WRITE(BL_PWM_CNTL2,
371
				abm->stored_backlight_registers.BL_PWM_CNTL2);
372
			REG_WRITE(BL_PWM_PERIOD_CNTL,
373
				abm->stored_backlight_registers.BL_PWM_PERIOD_CNTL);
374 375
			REG_UPDATE(LVTMA_PWRSEQ_REF_DIV,
				BL_PWM_REF_DIV,
376
				abm->stored_backlight_registers.
377 378 379 380 381 382 383 384 385
				LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV);
		} else {
			/* TODO: Note: This should not really happen since VBIOS
			 * should have initialized PWM registers on boot.
			 */
			REG_WRITE(BL_PWM_CNTL, 0xC000FA00);
			REG_WRITE(BL_PWM_PERIOD_CNTL, 0x000C0FA0);
		}
	} else {
386
		abm->stored_backlight_registers.BL_PWM_CNTL =
387
				REG_READ(BL_PWM_CNTL);
388
		abm->stored_backlight_registers.BL_PWM_CNTL2 =
389
				REG_READ(BL_PWM_CNTL2);
390
		abm->stored_backlight_registers.BL_PWM_PERIOD_CNTL =
391 392 393
				REG_READ(BL_PWM_PERIOD_CNTL);

		REG_GET(LVTMA_PWRSEQ_REF_DIV, BL_PWM_REF_DIV,
394
				&abm->stored_backlight_registers.
395 396 397 398 399 400 401 402 403 404 405 406 407
				LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV);
	}

	/* Have driver take backlight control
	 * TakeBacklightControl(true)
	 */
	value = REG_READ(BIOS_SCRATCH_2);
	value |= ATOM_S2_VRI_BRIGHT_ENABLE;
	REG_WRITE(BIOS_SCRATCH_2, value);

	/* Enable the backlight output */
	REG_UPDATE(BL_PWM_CNTL, BL_PWM_EN, 1);

408 409 410 411
	/* Disable fractional pwm if configured */
	REG_UPDATE(BL_PWM_CNTL, BL_PWM_FRACTIONAL_EN,
		   abm->ctx->dc->config.disable_fractional_pwm ? 0 : 1);

412 413 414 415 416 417 418
	/* Unlock group 2 backlight registers */
	REG_UPDATE(BL_PWM_GRP1_REG_LOCK,
			BL_PWM_GRP1_REG_LOCK, 0);

	return true;
}

419
static bool dce_abm_set_backlight_level_pwm(
420
		struct abm *abm,
421
		unsigned int backlight_pwm_u16_16,
422
		unsigned int frame_ramp,
423
		unsigned int controller_id,
424
		unsigned int panel_inst,
425
		bool fw_set_brightness)
426 427 428
{
	struct dce_abm *abm_dce = TO_DCE_ABM(abm);

429
	DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n",
430
			backlight_pwm_u16_16, backlight_pwm_u16_16);
431 432

	/* If DMCU is in reset state, DMCU is uninitialized */
433
	if (fw_set_brightness)
434
		dmcu_set_backlight_level(abm_dce,
435
				backlight_pwm_u16_16,
436
				frame_ramp,
437 438
				controller_id,
				panel_inst);
439
	else
440
		driver_set_backlight_level(abm_dce, backlight_pwm_u16_16);
441 442 443 444

	return true;
}

445 446 447
static const struct abm_funcs dce_funcs = {
	.abm_init = dce_abm_init,
	.set_abm_level = dce_abm_set_level,
448
	.init_backlight = dce_abm_init_backlight,
449
	.set_pipe = dce_abm_set_pipe,
450 451
	.set_backlight_level_pwm = dce_abm_set_backlight_level_pwm,
	.get_current_backlight = dce_abm_get_current_backlight,
452
	.get_target_backlight = dce_abm_get_target_backlight,
453
	.init_abm_config = NULL,
454
	.set_abm_immediate_disable = dce_abm_immediate_disable
455 456 457 458 459 460 461 462 463 464 465 466 467
};

static void dce_abm_construct(
	struct dce_abm *abm_dce,
	struct dc_context *ctx,
	const struct dce_abm_registers *regs,
	const struct dce_abm_shift *abm_shift,
	const struct dce_abm_mask *abm_mask)
{
	struct abm *base = &abm_dce->base;

	base->ctx = ctx;
	base->funcs = &dce_funcs;
468 469 470 471
	base->stored_backlight_registers.BL_PWM_CNTL = 0;
	base->stored_backlight_registers.BL_PWM_CNTL2 = 0;
	base->stored_backlight_registers.BL_PWM_PERIOD_CNTL = 0;
	base->stored_backlight_registers.LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV = 0;
472
	base->dmcu_is_running = false;
473 474 475 476 477 478 479 480 481 482 483 484

	abm_dce->regs = regs;
	abm_dce->abm_shift = abm_shift;
	abm_dce->abm_mask = abm_mask;
}

struct abm *dce_abm_create(
	struct dc_context *ctx,
	const struct dce_abm_registers *regs,
	const struct dce_abm_shift *abm_shift,
	const struct dce_abm_mask *abm_mask)
{
485
	struct dce_abm *abm_dce = kzalloc(sizeof(*abm_dce), GFP_KERNEL);
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

	if (abm_dce == NULL) {
		BREAK_TO_DEBUGGER();
		return NULL;
	}

	dce_abm_construct(abm_dce, ctx, regs, abm_shift, abm_mask);

	abm_dce->base.funcs = &dce_funcs;

	return &abm_dce->base;
}

void dce_abm_destroy(struct abm **abm)
{
	struct dce_abm *abm_dce = TO_DCE_ABM(*abm);

503
	kfree(abm_dce);
504 505
	*abm = NULL;
}