dce_abm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. #include "dce_abm.h"
  26. #include "dm_services.h"
  27. #include "reg_helper.h"
  28. #include "fixed32_32.h"
  29. #include "dc.h"
  30. #include "atom.h"
  31. #define TO_DCE_ABM(abm)\
  32. container_of(abm, struct dce_abm, base)
  33. #define REG(reg) \
  34. (abm_dce->regs->reg)
  35. #undef FN
  36. #define FN(reg_name, field_name) \
  37. abm_dce->abm_shift->field_name, abm_dce->abm_mask->field_name
  38. #define CTX \
  39. abm_dce->base.ctx
  40. #define MCP_ABM_LEVEL_SET 0x65
  41. #define MCP_ABM_PIPE_SET 0x66
  42. #define MCP_BL_SET 0x67
  43. #define MCP_DISABLE_ABM_IMMEDIATELY 255
  44. struct abm_backlight_registers {
  45. unsigned int BL_PWM_CNTL;
  46. unsigned int BL_PWM_CNTL2;
  47. unsigned int BL_PWM_PERIOD_CNTL;
  48. unsigned int LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV;
  49. };
  50. /* registers setting needs to be save and restored used at InitBacklight */
  51. static struct abm_backlight_registers stored_backlight_registers = {0};
  52. static unsigned int get_current_backlight_16_bit(struct dce_abm *abm_dce)
  53. {
  54. uint64_t current_backlight;
  55. uint32_t round_result;
  56. uint32_t pwm_period_cntl, bl_period, bl_int_count;
  57. uint32_t bl_pwm_cntl, bl_pwm, fractional_duty_cycle_en;
  58. uint32_t bl_period_mask, bl_pwm_mask;
  59. pwm_period_cntl = REG_READ(BL_PWM_PERIOD_CNTL);
  60. REG_GET(BL_PWM_PERIOD_CNTL, BL_PWM_PERIOD, &bl_period);
  61. REG_GET(BL_PWM_PERIOD_CNTL, BL_PWM_PERIOD_BITCNT, &bl_int_count);
  62. bl_pwm_cntl = REG_READ(BL_PWM_CNTL);
  63. REG_GET(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, (uint32_t *)(&bl_pwm));
  64. REG_GET(BL_PWM_CNTL, BL_PWM_FRACTIONAL_EN, &fractional_duty_cycle_en);
  65. if (bl_int_count == 0)
  66. bl_int_count = 16;
  67. bl_period_mask = (1 << bl_int_count) - 1;
  68. bl_period &= bl_period_mask;
  69. bl_pwm_mask = bl_period_mask << (16 - bl_int_count);
  70. if (fractional_duty_cycle_en == 0)
  71. bl_pwm &= bl_pwm_mask;
  72. else
  73. bl_pwm &= 0xFFFF;
  74. current_backlight = bl_pwm << (1 + bl_int_count);
  75. if (bl_period == 0)
  76. bl_period = 0xFFFF;
  77. current_backlight = div_u64(current_backlight, bl_period);
  78. current_backlight = (current_backlight + 1) >> 1;
  79. current_backlight = (uint64_t)(current_backlight) * bl_period;
  80. round_result = (uint32_t)(current_backlight & 0xFFFFFFFF);
  81. round_result = (round_result >> (bl_int_count-1)) & 1;
  82. current_backlight >>= bl_int_count;
  83. current_backlight += round_result;
  84. return (uint32_t)(current_backlight);
  85. }
  86. static void driver_set_backlight_level(struct dce_abm *abm_dce, uint32_t level)
  87. {
  88. uint32_t backlight_24bit;
  89. uint32_t backlight_17bit;
  90. uint32_t backlight_16bit;
  91. uint32_t masked_pwm_period;
  92. uint8_t rounding_bit;
  93. uint8_t bit_count;
  94. uint64_t active_duty_cycle;
  95. uint32_t pwm_period_bitcnt;
  96. /*
  97. * 1. Convert 8-bit value to 17 bit U1.16 format
  98. * (1 integer, 16 fractional bits)
  99. */
  100. /* 1.1 multiply 8 bit value by 0x10101 to get a 24 bit value,
  101. * effectively multiplying value by 256/255
  102. * eg. for a level of 0xEF, backlight_24bit = 0xEF * 0x10101 = 0xEFEFEF
  103. */
  104. backlight_24bit = level * 0x10101;
  105. /* 1.2 The upper 16 bits of the 24 bit value is the fraction, lower 8
  106. * used for rounding, take most significant bit of fraction for
  107. * rounding, e.g. for 0xEFEFEF, rounding bit is 1
  108. */
  109. rounding_bit = (backlight_24bit >> 7) & 1;
  110. /* 1.3 Add the upper 16 bits of the 24 bit value with the rounding bit
  111. * resulting in a 17 bit value e.g. 0xEFF0 = (0xEFEFEF >> 8) + 1
  112. */
  113. backlight_17bit = (backlight_24bit >> 8) + rounding_bit;
  114. /*
  115. * 2. Find 16 bit backlight active duty cycle, where 0 <= backlight
  116. * active duty cycle <= backlight period
  117. */
  118. /* 2.1 Apply bitmask for backlight period value based on value of BITCNT
  119. */
  120. REG_GET_2(BL_PWM_PERIOD_CNTL,
  121. BL_PWM_PERIOD_BITCNT, &pwm_period_bitcnt,
  122. BL_PWM_PERIOD, &masked_pwm_period);
  123. if (pwm_period_bitcnt == 0)
  124. bit_count = 16;
  125. else
  126. bit_count = pwm_period_bitcnt;
  127. /* e.g. maskedPwmPeriod = 0x24 when bitCount is 6 */
  128. masked_pwm_period = masked_pwm_period & ((1 << bit_count) - 1);
  129. /* 2.2 Calculate integer active duty cycle required upper 16 bits
  130. * contain integer component, lower 16 bits contain fractional component
  131. * of active duty cycle e.g. 0x21BDC0 = 0xEFF0 * 0x24
  132. */
  133. active_duty_cycle = backlight_17bit * masked_pwm_period;
  134. /* 2.3 Calculate 16 bit active duty cycle from integer and fractional
  135. * components shift by bitCount then mask 16 bits and add rounding bit
  136. * from MSB of fraction e.g. 0x86F7 = ((0x21BDC0 >> 6) & 0xFFF) + 0
  137. */
  138. backlight_16bit = active_duty_cycle >> bit_count;
  139. backlight_16bit &= 0xFFFF;
  140. backlight_16bit += (active_duty_cycle >> (bit_count - 1)) & 0x1;
  141. /*
  142. * 3. Program register with updated value
  143. */
  144. /* 3.1 Lock group 2 backlight registers */
  145. REG_UPDATE_2(BL_PWM_GRP1_REG_LOCK,
  146. BL_PWM_GRP1_IGNORE_MASTER_LOCK_EN, 1,
  147. BL_PWM_GRP1_REG_LOCK, 1);
  148. // 3.2 Write new active duty cycle
  149. REG_UPDATE(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, backlight_16bit);
  150. /* 3.3 Unlock group 2 backlight registers */
  151. REG_UPDATE(BL_PWM_GRP1_REG_LOCK,
  152. BL_PWM_GRP1_REG_LOCK, 0);
  153. /* 5.4.4 Wait for pending bit to be cleared */
  154. REG_WAIT(BL_PWM_GRP1_REG_LOCK, BL_PWM_GRP1_REG_UPDATE_PENDING,
  155. 0, 10, 1000);
  156. }
  157. static void dmcu_set_backlight_level(
  158. struct dce_abm *abm_dce,
  159. uint32_t level,
  160. uint32_t frame_ramp,
  161. uint32_t controller_id)
  162. {
  163. unsigned int backlight_16_bit = (level * 0x10101) >> 8;
  164. unsigned int backlight_17_bit = backlight_16_bit +
  165. (((backlight_16_bit & 0x80) >> 7) & 1);
  166. uint32_t rampingBoundary = 0xFFFF;
  167. uint32_t s2;
  168. /* set ramping boundary */
  169. REG_WRITE(MASTER_COMM_DATA_REG1, rampingBoundary);
  170. /* setDMCUParam_Pipe */
  171. REG_UPDATE_2(MASTER_COMM_CMD_REG,
  172. MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_PIPE_SET,
  173. MASTER_COMM_CMD_REG_BYTE1, controller_id);
  174. /* notifyDMCUMsg */
  175. REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
  176. /* waitDMCUReadyForCmd */
  177. REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT,
  178. 0, 100, 800);
  179. /* setDMCUParam_BL */
  180. REG_UPDATE(BL1_PWM_USER_LEVEL, BL1_PWM_USER_LEVEL, backlight_17_bit);
  181. /* write ramp */
  182. if (controller_id == 0)
  183. frame_ramp = 0;
  184. REG_WRITE(MASTER_COMM_DATA_REG1, frame_ramp);
  185. /* setDMCUParam_Cmd */
  186. REG_UPDATE(MASTER_COMM_CMD_REG, MASTER_COMM_CMD_REG_BYTE0, MCP_BL_SET);
  187. /* notifyDMCUMsg */
  188. REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
  189. /* UpdateRequestedBacklightLevel */
  190. s2 = REG_READ(BIOS_SCRATCH_2);
  191. s2 &= ~ATOM_S2_CURRENT_BL_LEVEL_MASK;
  192. level &= (ATOM_S2_CURRENT_BL_LEVEL_MASK >>
  193. ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
  194. s2 |= (level << ATOM_S2_CURRENT_BL_LEVEL_SHIFT);
  195. REG_WRITE(BIOS_SCRATCH_2, s2);
  196. }
  197. static void dce_abm_init(struct abm *abm)
  198. {
  199. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  200. unsigned int backlight = get_current_backlight_16_bit(abm_dce);
  201. REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x103);
  202. REG_WRITE(DC_ABM1_HG_SAMPLE_RATE, 0x101);
  203. REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x103);
  204. REG_WRITE(DC_ABM1_LS_SAMPLE_RATE, 0x101);
  205. REG_WRITE(BL1_PWM_BL_UPDATE_SAMPLE_RATE, 0x101);
  206. REG_SET_3(DC_ABM1_HG_MISC_CTRL, 0,
  207. ABM1_HG_NUM_OF_BINS_SEL, 0,
  208. ABM1_HG_VMAX_SEL, 1,
  209. ABM1_HG_BIN_BITWIDTH_SIZE_SEL, 0);
  210. REG_SET_3(DC_ABM1_IPCSC_COEFF_SEL, 0,
  211. ABM1_IPCSC_COEFF_SEL_R, 2,
  212. ABM1_IPCSC_COEFF_SEL_G, 4,
  213. ABM1_IPCSC_COEFF_SEL_B, 2);
  214. REG_UPDATE(BL1_PWM_CURRENT_ABM_LEVEL,
  215. BL1_PWM_CURRENT_ABM_LEVEL, backlight);
  216. REG_UPDATE(BL1_PWM_TARGET_ABM_LEVEL,
  217. BL1_PWM_TARGET_ABM_LEVEL, backlight);
  218. REG_UPDATE(BL1_PWM_USER_LEVEL,
  219. BL1_PWM_USER_LEVEL, backlight);
  220. REG_UPDATE_2(DC_ABM1_LS_MIN_MAX_PIXEL_VALUE_THRES,
  221. ABM1_LS_MIN_PIXEL_VALUE_THRES, 0,
  222. ABM1_LS_MAX_PIXEL_VALUE_THRES, 1000);
  223. REG_SET_3(DC_ABM1_HGLS_REG_READ_PROGRESS, 0,
  224. ABM1_HG_REG_READ_MISSED_FRAME_CLEAR, 1,
  225. ABM1_LS_REG_READ_MISSED_FRAME_CLEAR, 1,
  226. ABM1_BL_REG_READ_MISSED_FRAME_CLEAR, 1);
  227. }
  228. static unsigned int dce_abm_get_current_backlight_8_bit(struct abm *abm)
  229. {
  230. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  231. unsigned int backlight = REG_READ(BL1_PWM_CURRENT_ABM_LEVEL);
  232. return (backlight >> 8);
  233. }
  234. static bool dce_abm_set_level(struct abm *abm, uint32_t level)
  235. {
  236. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  237. REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
  238. 100, 800);
  239. /* setDMCUParam_ABMLevel */
  240. REG_UPDATE_2(MASTER_COMM_CMD_REG,
  241. MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_LEVEL_SET,
  242. MASTER_COMM_CMD_REG_BYTE2, level);
  243. /* notifyDMCUMsg */
  244. REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
  245. return true;
  246. }
  247. static bool dce_abm_immediate_disable(struct abm *abm)
  248. {
  249. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  250. REG_WAIT(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 0,
  251. 100, 800);
  252. /* setDMCUParam_ABMLevel */
  253. REG_UPDATE_2(MASTER_COMM_CMD_REG,
  254. MASTER_COMM_CMD_REG_BYTE0, MCP_ABM_LEVEL_SET,
  255. MASTER_COMM_CMD_REG_BYTE2, MCP_DISABLE_ABM_IMMEDIATELY);
  256. /* notifyDMCUMsg */
  257. REG_UPDATE(MASTER_COMM_CNTL_REG, MASTER_COMM_INTERRUPT, 1);
  258. return true;
  259. }
  260. static bool dce_abm_init_backlight(struct abm *abm)
  261. {
  262. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  263. uint32_t value;
  264. /* It must not be 0, so we have to restore them
  265. * Bios bug w/a - period resets to zero,
  266. * restoring to cache values which is always correct
  267. */
  268. REG_GET(BL_PWM_CNTL, BL_ACTIVE_INT_FRAC_CNT, &value);
  269. if (value == 0 || value == 1) {
  270. if (stored_backlight_registers.BL_PWM_CNTL != 0) {
  271. REG_WRITE(BL_PWM_CNTL,
  272. stored_backlight_registers.BL_PWM_CNTL);
  273. REG_WRITE(BL_PWM_CNTL2,
  274. stored_backlight_registers.BL_PWM_CNTL2);
  275. REG_WRITE(BL_PWM_PERIOD_CNTL,
  276. stored_backlight_registers.BL_PWM_PERIOD_CNTL);
  277. REG_UPDATE(LVTMA_PWRSEQ_REF_DIV,
  278. BL_PWM_REF_DIV,
  279. stored_backlight_registers.
  280. LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV);
  281. } else {
  282. /* TODO: Note: This should not really happen since VBIOS
  283. * should have initialized PWM registers on boot.
  284. */
  285. REG_WRITE(BL_PWM_CNTL, 0xC000FA00);
  286. REG_WRITE(BL_PWM_PERIOD_CNTL, 0x000C0FA0);
  287. }
  288. } else {
  289. stored_backlight_registers.BL_PWM_CNTL =
  290. REG_READ(BL_PWM_CNTL);
  291. stored_backlight_registers.BL_PWM_CNTL2 =
  292. REG_READ(BL_PWM_CNTL2);
  293. stored_backlight_registers.BL_PWM_PERIOD_CNTL =
  294. REG_READ(BL_PWM_PERIOD_CNTL);
  295. REG_GET(LVTMA_PWRSEQ_REF_DIV, BL_PWM_REF_DIV,
  296. &stored_backlight_registers.
  297. LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV);
  298. }
  299. /* Have driver take backlight control
  300. * TakeBacklightControl(true)
  301. */
  302. value = REG_READ(BIOS_SCRATCH_2);
  303. value |= ATOM_S2_VRI_BRIGHT_ENABLE;
  304. REG_WRITE(BIOS_SCRATCH_2, value);
  305. /* Enable the backlight output */
  306. REG_UPDATE(BL_PWM_CNTL, BL_PWM_EN, 1);
  307. /* Unlock group 2 backlight registers */
  308. REG_UPDATE(BL_PWM_GRP1_REG_LOCK,
  309. BL_PWM_GRP1_REG_LOCK, 0);
  310. return true;
  311. }
  312. static bool is_dmcu_initialized(struct abm *abm)
  313. {
  314. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  315. unsigned int dmcu_uc_reset;
  316. REG_GET(DMCU_STATUS, UC_IN_RESET, &dmcu_uc_reset);
  317. return !dmcu_uc_reset;
  318. }
  319. static bool dce_abm_set_backlight_level(
  320. struct abm *abm,
  321. unsigned int backlight_level,
  322. unsigned int frame_ramp,
  323. unsigned int controller_id)
  324. {
  325. struct dce_abm *abm_dce = TO_DCE_ABM(abm);
  326. dm_logger_write(abm->ctx->logger, LOG_BACKLIGHT,
  327. "New Backlight level: %d (0x%X)\n",
  328. backlight_level, backlight_level);
  329. /* If DMCU is in reset state, DMCU is uninitialized */
  330. if (is_dmcu_initialized(abm))
  331. dmcu_set_backlight_level(abm_dce,
  332. backlight_level,
  333. frame_ramp,
  334. controller_id);
  335. else
  336. driver_set_backlight_level(abm_dce, backlight_level);
  337. return true;
  338. }
  339. static const struct abm_funcs dce_funcs = {
  340. .abm_init = dce_abm_init,
  341. .set_abm_level = dce_abm_set_level,
  342. .init_backlight = dce_abm_init_backlight,
  343. .set_backlight_level = dce_abm_set_backlight_level,
  344. .get_current_backlight_8_bit = dce_abm_get_current_backlight_8_bit,
  345. .set_abm_immediate_disable = dce_abm_immediate_disable,
  346. .is_dmcu_initialized = is_dmcu_initialized
  347. };
  348. static void dce_abm_construct(
  349. struct dce_abm *abm_dce,
  350. struct dc_context *ctx,
  351. const struct dce_abm_registers *regs,
  352. const struct dce_abm_shift *abm_shift,
  353. const struct dce_abm_mask *abm_mask)
  354. {
  355. struct abm *base = &abm_dce->base;
  356. base->ctx = ctx;
  357. base->funcs = &dce_funcs;
  358. abm_dce->regs = regs;
  359. abm_dce->abm_shift = abm_shift;
  360. abm_dce->abm_mask = abm_mask;
  361. }
  362. struct abm *dce_abm_create(
  363. struct dc_context *ctx,
  364. const struct dce_abm_registers *regs,
  365. const struct dce_abm_shift *abm_shift,
  366. const struct dce_abm_mask *abm_mask)
  367. {
  368. struct dce_abm *abm_dce = dm_alloc(sizeof(*abm_dce));
  369. if (abm_dce == NULL) {
  370. BREAK_TO_DEBUGGER();
  371. return NULL;
  372. }
  373. dce_abm_construct(abm_dce, ctx, regs, abm_shift, abm_mask);
  374. abm_dce->base.funcs = &dce_funcs;
  375. return &abm_dce->base;
  376. }
  377. void dce_abm_destroy(struct abm **abm)
  378. {
  379. struct dce_abm *abm_dce = TO_DCE_ABM(*abm);
  380. dm_free(abm_dce);
  381. *abm = NULL;
  382. }