backlight.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Intel Cherryview/VLV display PWM panel backlight.
  2. *
  3. * Braswell/Cherryview (and Bay Trail/Valleyview) drive the internal panel
  4. * backlight from the display engine, not from the Chrome EC. The VBT
  5. * (block 43, LFP backlight) on google/setzer selects PWM controller 0 at
  6. * 200 Hz, active high. Linux drives the same registers from i915
  7. * (vlv_setup_backlight()/vlv_set_backlight()):
  8. *
  9. * BLC_PWM_CTL2 pipe A VLV_DISPLAY_BASE + 0x61250 (0x1E1250)
  10. * BLC_PWM_CTL pipe A VLV_DISPLAY_BASE + 0x61254 (0x1E1254)
  11. * pipe B adds 0x100.
  12. *
  13. * BLC_PWM_CTL2 bit 31 enables the PWM, bit 28 is the polarity.
  14. * BLC_PWM_CTL bits 31:16 are the period and bits 15:0 the duty cycle.
  15. *
  16. * The PWM clock is 19.2 MHz with a fixed /16 increment on Cherryview, so
  17. * 200 Hz is a period of 6000. At probe time the period the firmware left
  18. * in the register wins; 6000 is only the fallback when it reads as zero.
  19. */
  20. #include "backlight.h"
  21. #include "paging.h"
  22. #include "pci.h"
  23. #include "serial.h"
  24. #define INTEL_CHV_GFX_B0 0x22b0
  25. #define INTEL_CHV_GFX_C0 0x22b1
  26. #define VLV_DISPLAY_BASE 0x180000u
  27. #define VLV_BLC_PWM_CTL2(pipe) (VLV_DISPLAY_BASE + 0x61250u + \
  28. ((pipe) ? 0x100u : 0u))
  29. #define VLV_BLC_PWM_CTL(pipe) (VLV_DISPLAY_BASE + 0x61254u + \
  30. ((pipe) ? 0x100u : 0u))
  31. #define BLM_PWM_ENABLE (1u << 31)
  32. #define BLC_DUTY_MASK 0xFFFFu
  33. #define BLC_MIN_PERCENT 5
  34. #define BLC_STEP_PERCENT 10
  35. #define BLC_DEFAULT_PERIOD 6000u /* 19.2 MHz / (200 Hz * 16) */
  36. static unsigned int blc_bar;
  37. static unsigned int blc_pipe;
  38. static unsigned int blc_period;
  39. static unsigned int blc_duty;
  40. static int blc_level;
  41. static int blc_present;
  42. static int blc_enabled;
  43. static unsigned int blc_seq; /* bumped on every user-visible change */
  44. static unsigned int blc_read(unsigned int reg)
  45. {
  46. return *(volatile unsigned int *)(blc_bar + reg);
  47. }
  48. static void blc_write(unsigned int reg, unsigned int val)
  49. {
  50. *(volatile unsigned int *)(blc_bar + reg) = val;
  51. }
  52. void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  53. {
  54. unsigned int bar0, ctl2[2], ctl[2], period, duty, i;
  55. int pipe, a_ok, b_ok;
  56. blc_bar = 0;
  57. blc_pipe = 0;
  58. blc_period = 0;
  59. blc_duty = 0;
  60. blc_level = 0;
  61. blc_present = 0;
  62. blc_enabled = 0;
  63. if (device_id != INTEL_CHV_GFX_B0 && device_id != INTEL_CHV_GFX_C0)
  64. return;
  65. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  66. if (bar0 & 1)
  67. return; /* I/O BAR, not MMIO */
  68. if (!(bar0 & 0xFFFFFFF0u))
  69. return;
  70. if ((bar0 & 0x6) == 0x4 &&
  71. pci_cfg_read32(bus, dev, 0, 0x14) != 0)
  72. return; /* 64-bit BAR above 4 GB */
  73. /* The backlight registers live below 2 MB in the display MMIO BAR. */
  74. blc_bar = bar0 & 0xFFFFFFF0u;
  75. paging_map_mmio(blc_bar, 0x200000);
  76. for (i = 0; i < 2; i++) {
  77. ctl2[i] = blc_read(VLV_BLC_PWM_CTL2(i));
  78. ctl[i] = blc_read(VLV_BLC_PWM_CTL(i));
  79. }
  80. a_ok = !(ctl2[0] == 0xFFFFFFFFu && ctl[0] == 0xFFFFFFFFu);
  81. b_ok = !(ctl2[1] == 0xFFFFFFFFu && ctl[1] == 0xFFFFFFFFu);
  82. if (!a_ok && !b_ok) {
  83. serial_puts(SERIAL_COM1, "backlight: no display MMIO\n");
  84. return;
  85. }
  86. /* Follow the pipe the firmware enabled; when both or neither are
  87. * enabled, use the active display plane; otherwise fall back to the
  88. * VBT's controller 0 (pipe A). The internal panel is normally on A. */
  89. pipe = 0;
  90. if (!a_ok) {
  91. pipe = 1;
  92. } else if (b_ok &&
  93. (ctl2[0] & BLM_PWM_ENABLE) == (ctl2[1] & BLM_PWM_ENABLE)) {
  94. unsigned int dspa = blc_read(VLV_DISPLAY_BASE + 0x70180);
  95. unsigned int dspb = blc_read(VLV_DISPLAY_BASE + 0x71180);
  96. if (!(dspa & (1u << 31)) && (dspb & (1u << 31)))
  97. pipe = 1;
  98. } else if (b_ok && (ctl2[1] & BLM_PWM_ENABLE)) {
  99. pipe = 1;
  100. }
  101. period = ctl[pipe] >> 16;
  102. if (!period)
  103. period = BLC_DEFAULT_PERIOD;
  104. duty = ctl[pipe] & BLC_DUTY_MASK;
  105. if (duty > period)
  106. duty = period;
  107. blc_pipe = (unsigned int)pipe;
  108. blc_period = period;
  109. blc_duty = duty;
  110. blc_level = (int)((duty * 100u + period / 2u) / period);
  111. if (blc_level > 100)
  112. blc_level = 100;
  113. if (blc_level < BLC_MIN_PERCENT)
  114. blc_level = BLC_MIN_PERCENT;
  115. blc_enabled = (ctl2[pipe] & BLM_PWM_ENABLE) != 0;
  116. blc_present = 1;
  117. serial_puts(SERIAL_COM1, "backlight: CHV PWM pipe ");
  118. serial_puts(SERIAL_COM1, pipe ? "B" : "A");
  119. serial_puts(SERIAL_COM1, ", period ");
  120. serial_putdec(SERIAL_COM1, period);
  121. serial_puts(SERIAL_COM1, ", duty ");
  122. serial_putdec(SERIAL_COM1, duty);
  123. serial_puts(SERIAL_COM1, " (");
  124. serial_putdec(SERIAL_COM1, (unsigned int)blc_level);
  125. serial_puts(SERIAL_COM1, "%), ");
  126. serial_puts(SERIAL_COM1, blc_enabled ? "enabled" : "disabled");
  127. serial_puts(SERIAL_COM1, ", ctl2 ");
  128. serial_puthex(SERIAL_COM1, ctl2[0]);
  129. serial_puthex(SERIAL_COM1, ctl2[1]);
  130. serial_puts(SERIAL_COM1, ", ctl ");
  131. serial_puthex(SERIAL_COM1, ctl[0]);
  132. serial_puthex(SERIAL_COM1, ctl[1]);
  133. serial_puts(SERIAL_COM1, "\n");
  134. }
  135. int backlight_present(void)
  136. {
  137. return blc_present;
  138. }
  139. int backlight_percent(void)
  140. {
  141. return blc_present ? blc_level : -1;
  142. }
  143. unsigned int backlight_seq(void)
  144. {
  145. return blc_seq;
  146. }
  147. void backlight_set_percent(int percent)
  148. {
  149. unsigned int ctl2, duty;
  150. if (!blc_present)
  151. return;
  152. if (percent < BLC_MIN_PERCENT)
  153. percent = BLC_MIN_PERCENT;
  154. if (percent > 100)
  155. percent = 100;
  156. blc_seq++;
  157. duty = (blc_period * (unsigned int)percent + 50u) / 100u;
  158. if (duty > blc_period)
  159. duty = blc_period;
  160. if (percent && !duty)
  161. duty = 1;
  162. if (!blc_enabled) {
  163. ctl2 = blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
  164. blc_write(VLV_BLC_PWM_CTL2(blc_pipe), ctl2 & ~BLM_PWM_ENABLE);
  165. blc_write(VLV_BLC_PWM_CTL(blc_pipe),
  166. (blc_period << 16) | duty);
  167. (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
  168. blc_write(VLV_BLC_PWM_CTL2(blc_pipe),
  169. (ctl2 & ~BLM_PWM_ENABLE) | BLM_PWM_ENABLE);
  170. (void)blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
  171. blc_enabled = 1;
  172. } else {
  173. blc_write(VLV_BLC_PWM_CTL(blc_pipe),
  174. (blc_period << 16) | duty);
  175. (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
  176. }
  177. blc_duty = duty;
  178. blc_level = percent;
  179. }
  180. void backlight_step(int dir)
  181. {
  182. int level;
  183. if (!blc_present)
  184. return;
  185. level = blc_level + (dir > 0 ? BLC_STEP_PERCENT : -BLC_STEP_PERCENT);
  186. backlight_set_percent(level);
  187. }
  188. int backlight_status(struct backlight_status *out)
  189. {
  190. unsigned int i;
  191. if (!out)
  192. return -1;
  193. for (i = 0; i < sizeof(*out); i++)
  194. ((unsigned char *)out)[i] = 0;
  195. out->present = blc_present;
  196. if (!blc_present)
  197. return -1;
  198. out->level = blc_level;
  199. out->enabled = blc_enabled;
  200. out->pipe = blc_pipe;
  201. out->period = blc_period;
  202. out->duty = blc_duty;
  203. out->bar = blc_bar;
  204. /* Keep the two pipe snapshots live for the `backlight probe` dump. */
  205. out->ctl2[0] = blc_read(VLV_BLC_PWM_CTL2(0));
  206. out->ctl2[1] = blc_read(VLV_BLC_PWM_CTL2(1));
  207. out->ctl[0] = blc_read(VLV_BLC_PWM_CTL(0));
  208. out->ctl[1] = blc_read(VLV_BLC_PWM_CTL(1));
  209. return 0;
  210. }