backlight.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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; /* physical BAR (reported) */
  37. static unsigned int blc_regs; /* its kernel mapping */
  38. static unsigned int blc_pipe;
  39. static unsigned int blc_period;
  40. static unsigned int blc_duty;
  41. static int blc_level;
  42. static int blc_present;
  43. static int blc_enabled;
  44. static unsigned int blc_seq; /* bumped on every user-visible change */
  45. static unsigned int blc_read(unsigned int reg)
  46. {
  47. return *(volatile unsigned int *)(blc_regs + reg);
  48. }
  49. static void blc_write(unsigned int reg, unsigned int val)
  50. {
  51. *(volatile unsigned int *)(blc_regs + reg) = val;
  52. }
  53. void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  54. {
  55. unsigned int bar0, ctl2[2], ctl[2], period, duty, i;
  56. int pipe, a_ok, b_ok;
  57. blc_bar = 0;
  58. blc_pipe = 0;
  59. blc_period = 0;
  60. blc_duty = 0;
  61. blc_level = 0;
  62. blc_present = 0;
  63. blc_enabled = 0;
  64. if (device_id != INTEL_CHV_GFX_B0 && device_id != INTEL_CHV_GFX_C0)
  65. return;
  66. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  67. if (bar0 & 1)
  68. return; /* I/O BAR, not MMIO */
  69. if (!(bar0 & 0xFFFFFFF0u))
  70. return;
  71. if ((bar0 & 0x6) == 0x4 &&
  72. pci_cfg_read32(bus, dev, 0, 0x14) != 0)
  73. return; /* 64-bit BAR above 4 GB */
  74. /* The backlight registers live below 2 MB in the display MMIO BAR. */
  75. blc_bar = bar0 & 0xFFFFFFF0u;
  76. blc_regs = (unsigned int)ioremap(blc_bar, 0x200000, IOREMAP_MMIO);
  77. if (!blc_regs) {
  78. blc_bar = 0;
  79. return;
  80. }
  81. for (i = 0; i < 2; i++) {
  82. ctl2[i] = blc_read(VLV_BLC_PWM_CTL2(i));
  83. ctl[i] = blc_read(VLV_BLC_PWM_CTL(i));
  84. }
  85. a_ok = !(ctl2[0] == 0xFFFFFFFFu && ctl[0] == 0xFFFFFFFFu);
  86. b_ok = !(ctl2[1] == 0xFFFFFFFFu && ctl[1] == 0xFFFFFFFFu);
  87. if (!a_ok && !b_ok) {
  88. serial_puts(SERIAL_COM1, "backlight: no display MMIO\n");
  89. return;
  90. }
  91. /* Follow the pipe the firmware enabled; when both or neither are
  92. * enabled, use the active display plane; otherwise fall back to the
  93. * VBT's controller 0 (pipe A). The internal panel is normally on A. */
  94. pipe = 0;
  95. if (!a_ok) {
  96. pipe = 1;
  97. } else if (b_ok &&
  98. (ctl2[0] & BLM_PWM_ENABLE) == (ctl2[1] & BLM_PWM_ENABLE)) {
  99. unsigned int dspa = blc_read(VLV_DISPLAY_BASE + 0x70180);
  100. unsigned int dspb = blc_read(VLV_DISPLAY_BASE + 0x71180);
  101. if (!(dspa & (1u << 31)) && (dspb & (1u << 31)))
  102. pipe = 1;
  103. } else if (b_ok && (ctl2[1] & BLM_PWM_ENABLE)) {
  104. pipe = 1;
  105. }
  106. period = ctl[pipe] >> 16;
  107. if (!period)
  108. period = BLC_DEFAULT_PERIOD;
  109. duty = ctl[pipe] & BLC_DUTY_MASK;
  110. if (duty > period)
  111. duty = period;
  112. blc_pipe = (unsigned int)pipe;
  113. blc_period = period;
  114. blc_duty = duty;
  115. blc_level = (int)((duty * 100u + period / 2u) / period);
  116. if (blc_level > 100)
  117. blc_level = 100;
  118. if (blc_level < BLC_MIN_PERCENT)
  119. blc_level = BLC_MIN_PERCENT;
  120. blc_enabled = (ctl2[pipe] & BLM_PWM_ENABLE) != 0;
  121. blc_present = 1;
  122. serial_puts(SERIAL_COM1, "backlight: CHV PWM pipe ");
  123. serial_puts(SERIAL_COM1, pipe ? "B" : "A");
  124. serial_puts(SERIAL_COM1, ", period ");
  125. serial_putdec(SERIAL_COM1, period);
  126. serial_puts(SERIAL_COM1, ", duty ");
  127. serial_putdec(SERIAL_COM1, duty);
  128. serial_puts(SERIAL_COM1, " (");
  129. serial_putdec(SERIAL_COM1, (unsigned int)blc_level);
  130. serial_puts(SERIAL_COM1, "%), ");
  131. serial_puts(SERIAL_COM1, blc_enabled ? "enabled" : "disabled");
  132. serial_puts(SERIAL_COM1, ", ctl2 ");
  133. serial_puthex(SERIAL_COM1, ctl2[0]);
  134. serial_puthex(SERIAL_COM1, ctl2[1]);
  135. serial_puts(SERIAL_COM1, ", ctl ");
  136. serial_puthex(SERIAL_COM1, ctl[0]);
  137. serial_puthex(SERIAL_COM1, ctl[1]);
  138. serial_puts(SERIAL_COM1, "\n");
  139. }
  140. int backlight_present(void)
  141. {
  142. return blc_present;
  143. }
  144. int backlight_percent(void)
  145. {
  146. return blc_present ? blc_level : -1;
  147. }
  148. unsigned int backlight_seq(void)
  149. {
  150. return blc_seq;
  151. }
  152. void backlight_set_percent(int percent)
  153. {
  154. unsigned int ctl2, duty;
  155. if (!blc_present)
  156. return;
  157. if (percent < BLC_MIN_PERCENT)
  158. percent = BLC_MIN_PERCENT;
  159. if (percent > 100)
  160. percent = 100;
  161. blc_seq++;
  162. duty = (blc_period * (unsigned int)percent + 50u) / 100u;
  163. if (duty > blc_period)
  164. duty = blc_period;
  165. if (percent && !duty)
  166. duty = 1;
  167. if (!blc_enabled) {
  168. ctl2 = blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
  169. blc_write(VLV_BLC_PWM_CTL2(blc_pipe), ctl2 & ~BLM_PWM_ENABLE);
  170. blc_write(VLV_BLC_PWM_CTL(blc_pipe),
  171. (blc_period << 16) | duty);
  172. (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
  173. blc_write(VLV_BLC_PWM_CTL2(blc_pipe),
  174. (ctl2 & ~BLM_PWM_ENABLE) | BLM_PWM_ENABLE);
  175. (void)blc_read(VLV_BLC_PWM_CTL2(blc_pipe));
  176. blc_enabled = 1;
  177. } else {
  178. blc_write(VLV_BLC_PWM_CTL(blc_pipe),
  179. (blc_period << 16) | duty);
  180. (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe));
  181. }
  182. blc_duty = duty;
  183. blc_level = percent;
  184. }
  185. void backlight_step(int dir)
  186. {
  187. int level;
  188. if (!blc_present)
  189. return;
  190. level = blc_level + (dir > 0 ? BLC_STEP_PERCENT : -BLC_STEP_PERCENT);
  191. backlight_set_percent(level);
  192. }
  193. int backlight_status(struct backlight_status *out)
  194. {
  195. unsigned int i;
  196. if (!out)
  197. return -1;
  198. for (i = 0; i < sizeof(*out); i++)
  199. ((unsigned char *)out)[i] = 0;
  200. out->present = blc_present;
  201. if (!blc_present)
  202. return -1;
  203. out->level = blc_level;
  204. out->enabled = blc_enabled;
  205. out->pipe = blc_pipe;
  206. out->period = blc_period;
  207. out->duty = blc_duty;
  208. out->bar = blc_bar;
  209. /* Keep the two pipe snapshots live for the `backlight probe` dump. */
  210. out->ctl2[0] = blc_read(VLV_BLC_PWM_CTL2(0));
  211. out->ctl2[1] = blc_read(VLV_BLC_PWM_CTL2(1));
  212. out->ctl[0] = blc_read(VLV_BLC_PWM_CTL(0));
  213. out->ctl[1] = blc_read(VLV_BLC_PWM_CTL(1));
  214. return 0;
  215. }