backlight.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 INTEL_KBL_GFX_GT2 0x5916 /* Yoga 720-13IKB HD 620 */
  27. #define INTEL_KBL_GFX_GT2_2 0x591e
  28. #define INTEL_KBL_GFX_GT2_3 0x5926
  29. #define VLV_DISPLAY_BASE 0x180000u
  30. #define VLV_BLC_PWM_CTL2(pipe) (VLV_DISPLAY_BASE + 0x61250u + \
  31. ((pipe) ? 0x100u : 0u))
  32. #define VLV_BLC_PWM_CTL(pipe) (VLV_DISPLAY_BASE + 0x61254u + \
  33. ((pipe) ? 0x100u : 0u))
  34. /* Sunrise Point-LP PCH display PWM, same register layout at 0xC825x. */
  35. #define PCH_BLC_PWM_CTL2(pipe) (0xc8250u + ((pipe) ? 0x100u : 0u))
  36. #define PCH_BLC_PWM_CTL(pipe) (0xc8254u + ((pipe) ? 0x100u : 0u))
  37. enum {
  38. BLC_KIND_VLV,
  39. BLC_KIND_PCH,
  40. };
  41. #define BLM_PWM_ENABLE (1u << 31)
  42. #define BLC_DUTY_MASK 0xFFFFu
  43. #define BLC_MIN_PERCENT 5
  44. #define BLC_STEP_PERCENT 10
  45. #define BLC_DEFAULT_PERIOD 6000u /* 19.2 MHz / (200 Hz * 16) */
  46. static unsigned int blc_bar; /* physical BAR (reported) */
  47. static unsigned int blc_regs; /* its kernel mapping */
  48. static int blc_kind = BLC_KIND_VLV;
  49. static unsigned int blc_pipe;
  50. static unsigned int blc_period;
  51. static unsigned int blc_duty;
  52. static int blc_level;
  53. static int blc_present;
  54. static int blc_enabled;
  55. static unsigned int blc_seq; /* bumped on every user-visible change */
  56. static unsigned int blc_ctl2_reg(unsigned int pipe)
  57. {
  58. return blc_kind == BLC_KIND_PCH ? PCH_BLC_PWM_CTL2(pipe) :
  59. VLV_BLC_PWM_CTL2(pipe);
  60. }
  61. static unsigned int blc_ctl_reg(unsigned int pipe)
  62. {
  63. return blc_kind == BLC_KIND_PCH ? PCH_BLC_PWM_CTL(pipe) :
  64. VLV_BLC_PWM_CTL(pipe);
  65. }
  66. static unsigned int blc_dsp_reg(unsigned int pipe)
  67. {
  68. unsigned int off = 0x70180u + (pipe ? 0x1000u : 0u);
  69. return blc_kind == BLC_KIND_PCH ? off : VLV_DISPLAY_BASE + off;
  70. }
  71. static unsigned int blc_read(unsigned int reg)
  72. {
  73. return *(volatile unsigned int *)(blc_regs + reg);
  74. }
  75. static void blc_write(unsigned int reg, unsigned int val)
  76. {
  77. *(volatile unsigned int *)(blc_regs + reg) = val;
  78. }
  79. void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id)
  80. {
  81. unsigned int bar0, ctl2[2], ctl[2], period, duty, i;
  82. int pipe, a_ok, b_ok;
  83. blc_bar = 0;
  84. blc_pipe = 0;
  85. blc_period = 0;
  86. blc_duty = 0;
  87. blc_level = 0;
  88. blc_present = 0;
  89. blc_enabled = 0;
  90. if (device_id == INTEL_CHV_GFX_B0 || device_id == INTEL_CHV_GFX_C0) {
  91. blc_kind = BLC_KIND_VLV;
  92. } else if (device_id == INTEL_KBL_GFX_GT2 ||
  93. device_id == INTEL_KBL_GFX_GT2_2 ||
  94. device_id == INTEL_KBL_GFX_GT2_3) {
  95. blc_kind = BLC_KIND_PCH;
  96. } else {
  97. return;
  98. }
  99. bar0 = pci_cfg_read32(bus, dev, 0, 0x10);
  100. if (bar0 & 1)
  101. return; /* I/O BAR, not MMIO */
  102. if (!(bar0 & 0xFFFFFFF0u))
  103. return;
  104. if ((bar0 & 0x6) == 0x4 &&
  105. pci_cfg_read32(bus, dev, 0, 0x14) != 0)
  106. return; /* 64-bit BAR above 4 GB */
  107. /* The backlight registers live below 2 MB in the display MMIO BAR. */
  108. blc_bar = bar0 & 0xFFFFFFF0u;
  109. blc_regs = (unsigned int)ioremap(blc_bar, 0x200000, IOREMAP_MMIO);
  110. if (!blc_regs) {
  111. blc_bar = 0;
  112. return;
  113. }
  114. for (i = 0; i < 2; i++) {
  115. ctl2[i] = blc_read(blc_ctl2_reg(i));
  116. ctl[i] = blc_read(blc_ctl_reg(i));
  117. }
  118. a_ok = !(ctl2[0] == 0xFFFFFFFFu && ctl[0] == 0xFFFFFFFFu);
  119. b_ok = !(ctl2[1] == 0xFFFFFFFFu && ctl[1] == 0xFFFFFFFFu);
  120. if (!a_ok && !b_ok) {
  121. serial_puts(SERIAL_COM1, "backlight: no display MMIO\n");
  122. return;
  123. }
  124. /* Follow the pipe the firmware enabled; when both or neither are
  125. * enabled, use the active display plane; otherwise fall back to the
  126. * VBT's controller 0 (pipe A). The internal panel is normally on A. */
  127. pipe = 0;
  128. if (!a_ok) {
  129. pipe = 1;
  130. } else if (b_ok &&
  131. (ctl2[0] & BLM_PWM_ENABLE) == (ctl2[1] & BLM_PWM_ENABLE)) {
  132. unsigned int dspa = blc_read(blc_dsp_reg(0));
  133. unsigned int dspb = blc_read(blc_dsp_reg(1));
  134. if (!(dspa & (1u << 31)) && (dspb & (1u << 31)))
  135. pipe = 1;
  136. } else if (b_ok && (ctl2[1] & BLM_PWM_ENABLE)) {
  137. pipe = 1;
  138. }
  139. period = ctl[pipe] >> 16;
  140. if (!period)
  141. period = BLC_DEFAULT_PERIOD;
  142. duty = ctl[pipe] & BLC_DUTY_MASK;
  143. if (duty > period)
  144. duty = period;
  145. blc_pipe = (unsigned int)pipe;
  146. blc_period = period;
  147. blc_duty = duty;
  148. blc_level = (int)((duty * 100u + period / 2u) / period);
  149. if (blc_level > 100)
  150. blc_level = 100;
  151. if (blc_level < BLC_MIN_PERCENT)
  152. blc_level = BLC_MIN_PERCENT;
  153. blc_enabled = (ctl2[pipe] & BLM_PWM_ENABLE) != 0;
  154. blc_present = 1;
  155. serial_puts(SERIAL_COM1, blc_kind == BLC_KIND_PCH ?
  156. "backlight: PCH PWM pipe " : "backlight: CHV PWM pipe ");
  157. serial_puts(SERIAL_COM1, pipe ? "B" : "A");
  158. serial_puts(SERIAL_COM1, ", period ");
  159. serial_putdec(SERIAL_COM1, period);
  160. serial_puts(SERIAL_COM1, ", duty ");
  161. serial_putdec(SERIAL_COM1, duty);
  162. serial_puts(SERIAL_COM1, " (");
  163. serial_putdec(SERIAL_COM1, (unsigned int)blc_level);
  164. serial_puts(SERIAL_COM1, "%), ");
  165. serial_puts(SERIAL_COM1, blc_enabled ? "enabled" : "disabled");
  166. serial_puts(SERIAL_COM1, ", ctl2 ");
  167. serial_puthex(SERIAL_COM1, ctl2[0]);
  168. serial_puthex(SERIAL_COM1, ctl2[1]);
  169. serial_puts(SERIAL_COM1, ", ctl ");
  170. serial_puthex(SERIAL_COM1, ctl[0]);
  171. serial_puthex(SERIAL_COM1, ctl[1]);
  172. serial_puts(SERIAL_COM1, "\n");
  173. }
  174. int backlight_present(void)
  175. {
  176. return blc_present;
  177. }
  178. int backlight_percent(void)
  179. {
  180. return blc_present ? blc_level : -1;
  181. }
  182. unsigned int backlight_seq(void)
  183. {
  184. return blc_seq;
  185. }
  186. void backlight_set_percent(int percent)
  187. {
  188. unsigned int ctl2, duty;
  189. if (!blc_present)
  190. return;
  191. if (percent < BLC_MIN_PERCENT)
  192. percent = BLC_MIN_PERCENT;
  193. if (percent > 100)
  194. percent = 100;
  195. blc_seq++;
  196. duty = (blc_period * (unsigned int)percent + 50u) / 100u;
  197. if (duty > blc_period)
  198. duty = blc_period;
  199. if (percent && !duty)
  200. duty = 1;
  201. if (!blc_enabled) {
  202. ctl2 = blc_read(blc_ctl2_reg(blc_pipe));
  203. blc_write(blc_ctl2_reg(blc_pipe), ctl2 & ~BLM_PWM_ENABLE);
  204. blc_write(blc_ctl_reg(blc_pipe),
  205. (blc_period << 16) | duty);
  206. (void)blc_read(blc_ctl_reg(blc_pipe));
  207. blc_write(blc_ctl2_reg(blc_pipe),
  208. (ctl2 & ~BLM_PWM_ENABLE) | BLM_PWM_ENABLE);
  209. (void)blc_read(blc_ctl2_reg(blc_pipe));
  210. blc_enabled = 1;
  211. } else {
  212. blc_write(blc_ctl_reg(blc_pipe),
  213. (blc_period << 16) | duty);
  214. (void)blc_read(blc_ctl_reg(blc_pipe));
  215. }
  216. blc_duty = duty;
  217. blc_level = percent;
  218. }
  219. void backlight_step(int dir)
  220. {
  221. int level;
  222. if (!blc_present)
  223. return;
  224. level = blc_level + (dir > 0 ? BLC_STEP_PERCENT : -BLC_STEP_PERCENT);
  225. backlight_set_percent(level);
  226. }
  227. int backlight_status(struct backlight_status *out)
  228. {
  229. unsigned int i;
  230. if (!out)
  231. return -1;
  232. for (i = 0; i < sizeof(*out); i++)
  233. ((unsigned char *)out)[i] = 0;
  234. out->present = blc_present;
  235. if (!blc_present)
  236. return -1;
  237. out->level = blc_level;
  238. out->enabled = blc_enabled;
  239. out->pipe = blc_pipe;
  240. out->period = blc_period;
  241. out->duty = blc_duty;
  242. out->bar = blc_bar;
  243. /* Keep the two pipe snapshots live for the `backlight probe` dump. */
  244. out->ctl2[0] = blc_read(blc_ctl2_reg(0));
  245. out->ctl2[1] = blc_read(blc_ctl2_reg(1));
  246. out->ctl[0] = blc_read(blc_ctl_reg(0));
  247. out->ctl[1] = blc_read(blc_ctl_reg(1));
  248. return 0;
  249. }