dce_abm.c 13 KB

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