1 /* 2 * Copyright 2012-16 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: AMD 23 * 24 */ 25 26 #include <linux/slab.h> 27 28 #include "dce_abm.h" 29 #include "dm_services.h" 30 #include "reg_helper.h" 31 #include "fixed31_32.h" 32 #include "dc.h" 33 34 #include "atom.h" 35 36 37 #define TO_DCE_ABM(abm)\ 38 container_of(abm, struct dce_abm, base) 39 40 #define REG(reg) \ 41 (abm_dce->regs->reg) 42 43 #undef FN 44 #define FN(reg_name, field_name) \ 45 abm_dce->abm_shift->field_name, abm_dce->abm_mask->field_name 46 47 #define DC_LOGGER \ 48 abm->ctx->logger 49 #define CTX \ 50 abm_dce->base.ctx 51 52 #define MCP_ABM_LEVEL_SET 0x65 53 #define MCP_ABM_PIPE_SET 0x66 54 #define MCP_BL_SET 0x67 55 56 #define MCP_DISABLE_ABM_IMMEDIATELY 255 57 58 static bool dce_abm_set_pipe(struct abm *abm, uint32_t controller_id, uint32_t panel_inst) 59 { 60 (void)panel_inst; 61 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 62 uint32_t rampingBoundary = 0xFFFF; 63 64 if (abm->dmcu_is_running == false) 65 return true; 66 67 REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 68 1, 80000); 69 70 /* set ramping boundary */ 71 REG_WRITE(MASTER_COMM_DATA_REG1, rampingBoundary); 72 73 /* setDMCUParam_Pipe */ 74 REG_UPDATE_2(MASTER_COMM_CMD_REG, 75 MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_PIPE_SET, 76 MASTER_COMM_CMD_REG_BYTE1, controller_id); 77 78 /* notifyDMCUMsg */ 79 REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1); 80 81 REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 82 1, 80000); 83 84 return true; 85 } 86 87 static void dmcu_set_backlight_level( 88 struct dce_abm *abm_dce, 89 uint32_t backlight_pwm_u16_16, 90 uint32_t frame_ramp, 91 uint32_t controller_id, 92 uint32_t panel_id) 93 { 94 unsigned int backlight_8_bit = 0; 95 uint32_t s2; 96 97 if (backlight_pwm_u16_16 & 0x10000) 98 // Check for max backlight condition 99 backlight_8_bit = 0xFF; 100 else 101 // Take MSB of fractional part since backlight is not max 102 backlight_8_bit = (backlight_pwm_u16_16 >> 8) & 0xFF; 103 104 dce_abm_set_pipe(&abm_dce->base, controller_id, panel_id); 105 106 /* waitDMCUReadyForCmd */ 107 REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 108 0, 1, 80000); 109 110 /* setDMCUParam_BL */ 111 REG_UPDATE(BL1_PWM_USER_LEVEL, BL1_PWM_USER_LEVEL, backlight_pwm_u16_16); 112 113 /* write ramp */ 114 if (controller_id == 0) 115 frame_ramp = 0; 116 REG_WRITE(MASTER_COMM_DATA_REG1, frame_ramp); 117 118 /* setDMCUParam_Cmd */ 119 REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, MCP_BL_SET); 120 121 /* notifyDMCUMsg */ 122 REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1); 123 124 /* UpdateRequestedBacklightLevel */ 125 s2 = REG_READ(BIOS_SCRATCH_2); 126 127 s2 &= ~ATOM_S2_CURRENT_BL_LEVEL_MASK; 128 backlight_8_bit &= (ATOM_S2_CURRENT_BL_LEVEL_MASK >> 129 ATOM_S2_CURRENT_BL_LEVEL_SHIFT); 130 s2 |= (backlight_8_bit << ATOM_S2_CURRENT_BL_LEVEL_SHIFT); 131 132 REG_WRITE(BIOS_SCRATCH_2, s2); 133 134 /* waitDMCUReadyForCmd */ 135 REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 136 0, 1, 80000); 137 } 138 139 static void dce_abm_init(struct abm *abm, uint32_t backlight, uint32_t user_level) 140 { 141 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 142 143 REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x103); 144 REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x101); 145 REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x103); 146 REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x101); 147 REG_WRITE(BL1_PWM_BL_UPDATE_SAMPLE_RATE, 0x101); 148 149 REG_SET_3(DC_ABM1_HG_MISC_CTRL, 0, 150 ABM1_HG_NUM_OF_BINS_SEL, 0, 151 ABM1_HG_VMAX_SEL, 1, 152 ABM1_HG_BIN_BITWIDTH_SIZE_SEL, 0); 153 154 REG_SET_3(DC_ABM1_IPCSC_COEFF_SEL, 0, 155 ABM1_IPCSC_COEFF_SEL_R, 2, 156 ABM1_IPCSC_COEFF_SEL_G, 4, 157 ABM1_IPCSC_COEFF_SEL_B, 2); 158 159 REG_UPDATE(BL1_PWM_CURRENT_ABM_LEVEL, 160 BL1_PWM_CURRENT_ABM_LEVEL, backlight); 161 162 REG_UPDATE(BL1_PWM_TARGET_ABM_LEVEL, 163 BL1_PWM_TARGET_ABM_LEVEL, backlight); 164 165 REG_UPDATE(BL1_PWM_USER_LEVEL, 166 BL1_PWM_USER_LEVEL, user_level); 167 168 REG_UPDATE_2(DC_ABM1_LS_MIN_MAX_PIXEL_VALUE_THRES, 169 ABM1_LS_MIN_PIXEL_VALUE_THRES, 0, 170 ABM1_LS_MAX_PIXEL_VALUE_THRES, 1000); 171 172 REG_SET_3(DC_ABM1_HGLS_REG_READ_PROGRESS, 0, 173 ABM1_HG_REG_READ_MISSED_FRAME_CLEAR, 1, 174 ABM1_LS_REG_READ_MISSED_FRAME_CLEAR, 1, 175 ABM1_BL_REG_READ_MISSED_FRAME_CLEAR, 1); 176 } 177 178 static unsigned int dce_abm_get_current_backlight(struct abm *abm) 179 { 180 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 181 unsigned int backlight = REG_READ(BL1_PWM_CURRENT_ABM_LEVEL); 182 183 /* return backlight in hardware format which is unsigned 17 bits, with 184 * 1 bit integer and 16 bit fractional 185 */ 186 return backlight; 187 } 188 189 static unsigned int dce_abm_get_target_backlight(struct abm *abm) 190 { 191 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 192 unsigned int backlight = REG_READ(BL1_PWM_TARGET_ABM_LEVEL); 193 194 /* return backlight in hardware format which is unsigned 17 bits, with 195 * 1 bit integer and 16 bit fractional 196 */ 197 return backlight; 198 } 199 200 static bool dce_abm_set_level(struct abm *abm, uint32_t level) 201 { 202 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 203 204 if (abm->dmcu_is_running == false) 205 return true; 206 207 REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0, 208 1, 80000); 209 210 /* setDMCUParam_ABMLevel */ 211 REG_UPDATE_2(MASTER_COMM_CMD_REG, 212 MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_LEVEL_SET, 213 MASTER_COMM_CMD_REG_BYTE2, level); 214 215 /* notifyDMCUMsg */ 216 REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1); 217 218 return true; 219 } 220 221 static bool dce_abm_immediate_disable(struct abm *abm, uint32_t panel_inst) 222 { 223 224 if (abm->dmcu_is_running == false) 225 return true; 226 227 dce_abm_set_pipe(abm, MCP_DISABLE_ABM_IMMEDIATELY, panel_inst); 228 229 return true; 230 } 231 232 static bool dce_abm_set_backlight_level_pwm( 233 struct abm *abm, 234 unsigned int backlight_pwm_u16_16, 235 unsigned int frame_ramp, 236 unsigned int controller_id, 237 unsigned int panel_inst) 238 { 239 struct dce_abm *abm_dce = TO_DCE_ABM(abm); 240 241 DC_LOG_BACKLIGHT("New Backlight level: %d (0x%X)\n", 242 backlight_pwm_u16_16, backlight_pwm_u16_16); 243 244 dmcu_set_backlight_level(abm_dce, 245 backlight_pwm_u16_16, 246 frame_ramp, 247 controller_id, 248 panel_inst); 249 250 return true; 251 } 252 253 static const struct abm_funcs dce_funcs = { 254 .abm_init = dce_abm_init, 255 .set_abm_level = dce_abm_set_level, 256 .set_pipe = dce_abm_set_pipe, 257 .set_backlight_level_pwm = dce_abm_set_backlight_level_pwm, 258 .get_current_backlight = dce_abm_get_current_backlight, 259 .get_target_backlight = dce_abm_get_target_backlight, 260 .init_abm_config = NULL, 261 .set_abm_immediate_disable = dce_abm_immediate_disable, 262 }; 263 264 static void dce_abm_construct( 265 struct dce_abm *abm_dce, 266 struct dc_context *ctx, 267 const struct dce_abm_registers *regs, 268 const struct dce_abm_shift *abm_shift, 269 const struct dce_abm_mask *abm_mask) 270 { 271 struct abm *base = &abm_dce->base; 272 273 base->ctx = ctx; 274 base->funcs = &dce_funcs; 275 base->dmcu_is_running = false; 276 base->inst = 0; 277 278 abm_dce->regs = regs; 279 abm_dce->abm_shift = abm_shift; 280 abm_dce->abm_mask = abm_mask; 281 } 282 283 struct abm *dce_abm_create( 284 struct dc_context *ctx, 285 const struct dce_abm_registers *regs, 286 const struct dce_abm_shift *abm_shift, 287 const struct dce_abm_mask *abm_mask) 288 { 289 struct dce_abm *abm_dce = kzalloc_obj(*abm_dce); 290 291 if (abm_dce == NULL) { 292 BREAK_TO_DEBUGGER(); 293 return NULL; 294 } 295 296 dce_abm_construct(abm_dce, ctx, regs, abm_shift, abm_mask); 297 298 abm_dce->base.funcs = &dce_funcs; 299 300 return &abm_dce->base; 301 } 302 303 void dce_abm_destroy(struct abm **abm) 304 { 305 struct dce_abm *abm_dce = TO_DCE_ABM(*abm); 306 307 kfree(abm_dce); 308 *abm = NULL; 309 } 310