/* Intel Cherryview/VLV display PWM panel backlight. * * Braswell/Cherryview (and Bay Trail/Valleyview) drive the internal panel * backlight from the display engine, not from the Chrome EC. The VBT * (block 43, LFP backlight) on google/setzer selects PWM controller 0 at * 200 Hz, active high. Linux drives the same registers from i915 * (vlv_setup_backlight()/vlv_set_backlight()): * * BLC_PWM_CTL2 pipe A VLV_DISPLAY_BASE + 0x61250 (0x1E1250) * BLC_PWM_CTL pipe A VLV_DISPLAY_BASE + 0x61254 (0x1E1254) * pipe B adds 0x100. * * BLC_PWM_CTL2 bit 31 enables the PWM, bit 28 is the polarity. * BLC_PWM_CTL bits 31:16 are the period and bits 15:0 the duty cycle. * * The PWM clock is 19.2 MHz with a fixed /16 increment on Cherryview, so * 200 Hz is a period of 6000. At probe time the period the firmware left * in the register wins; 6000 is only the fallback when it reads as zero. */ #include "backlight.h" #include "paging.h" #include "pci.h" #include "serial.h" #define INTEL_CHV_GFX_B0 0x22b0 #define INTEL_CHV_GFX_C0 0x22b1 #define VLV_DISPLAY_BASE 0x180000u #define VLV_BLC_PWM_CTL2(pipe) (VLV_DISPLAY_BASE + 0x61250u + \ ((pipe) ? 0x100u : 0u)) #define VLV_BLC_PWM_CTL(pipe) (VLV_DISPLAY_BASE + 0x61254u + \ ((pipe) ? 0x100u : 0u)) #define BLM_PWM_ENABLE (1u << 31) #define BLC_DUTY_MASK 0xFFFFu #define BLC_MIN_PERCENT 5 #define BLC_STEP_PERCENT 10 #define BLC_DEFAULT_PERIOD 6000u /* 19.2 MHz / (200 Hz * 16) */ static unsigned int blc_bar; /* physical BAR (reported) */ static unsigned int blc_regs; /* its kernel mapping */ static unsigned int blc_pipe; static unsigned int blc_period; static unsigned int blc_duty; static int blc_level; static int blc_present; static int blc_enabled; static unsigned int blc_seq; /* bumped on every user-visible change */ static unsigned int blc_read(unsigned int reg) { return *(volatile unsigned int *)(blc_regs + reg); } static void blc_write(unsigned int reg, unsigned int val) { *(volatile unsigned int *)(blc_regs + reg) = val; } void backlight_init(unsigned int bus, unsigned int dev, unsigned int device_id) { unsigned int bar0, ctl2[2], ctl[2], period, duty, i; int pipe, a_ok, b_ok; blc_bar = 0; blc_pipe = 0; blc_period = 0; blc_duty = 0; blc_level = 0; blc_present = 0; blc_enabled = 0; if (device_id != INTEL_CHV_GFX_B0 && device_id != INTEL_CHV_GFX_C0) return; bar0 = pci_cfg_read32(bus, dev, 0, 0x10); if (bar0 & 1) return; /* I/O BAR, not MMIO */ if (!(bar0 & 0xFFFFFFF0u)) return; if ((bar0 & 0x6) == 0x4 && pci_cfg_read32(bus, dev, 0, 0x14) != 0) return; /* 64-bit BAR above 4 GB */ /* The backlight registers live below 2 MB in the display MMIO BAR. */ blc_bar = bar0 & 0xFFFFFFF0u; blc_regs = (unsigned int)ioremap(blc_bar, 0x200000, IOREMAP_MMIO); if (!blc_regs) { blc_bar = 0; return; } for (i = 0; i < 2; i++) { ctl2[i] = blc_read(VLV_BLC_PWM_CTL2(i)); ctl[i] = blc_read(VLV_BLC_PWM_CTL(i)); } a_ok = !(ctl2[0] == 0xFFFFFFFFu && ctl[0] == 0xFFFFFFFFu); b_ok = !(ctl2[1] == 0xFFFFFFFFu && ctl[1] == 0xFFFFFFFFu); if (!a_ok && !b_ok) { serial_puts(SERIAL_COM1, "backlight: no display MMIO\n"); return; } /* Follow the pipe the firmware enabled; when both or neither are * enabled, use the active display plane; otherwise fall back to the * VBT's controller 0 (pipe A). The internal panel is normally on A. */ pipe = 0; if (!a_ok) { pipe = 1; } else if (b_ok && (ctl2[0] & BLM_PWM_ENABLE) == (ctl2[1] & BLM_PWM_ENABLE)) { unsigned int dspa = blc_read(VLV_DISPLAY_BASE + 0x70180); unsigned int dspb = blc_read(VLV_DISPLAY_BASE + 0x71180); if (!(dspa & (1u << 31)) && (dspb & (1u << 31))) pipe = 1; } else if (b_ok && (ctl2[1] & BLM_PWM_ENABLE)) { pipe = 1; } period = ctl[pipe] >> 16; if (!period) period = BLC_DEFAULT_PERIOD; duty = ctl[pipe] & BLC_DUTY_MASK; if (duty > period) duty = period; blc_pipe = (unsigned int)pipe; blc_period = period; blc_duty = duty; blc_level = (int)((duty * 100u + period / 2u) / period); if (blc_level > 100) blc_level = 100; if (blc_level < BLC_MIN_PERCENT) blc_level = BLC_MIN_PERCENT; blc_enabled = (ctl2[pipe] & BLM_PWM_ENABLE) != 0; blc_present = 1; serial_puts(SERIAL_COM1, "backlight: CHV PWM pipe "); serial_puts(SERIAL_COM1, pipe ? "B" : "A"); serial_puts(SERIAL_COM1, ", period "); serial_putdec(SERIAL_COM1, period); serial_puts(SERIAL_COM1, ", duty "); serial_putdec(SERIAL_COM1, duty); serial_puts(SERIAL_COM1, " ("); serial_putdec(SERIAL_COM1, (unsigned int)blc_level); serial_puts(SERIAL_COM1, "%), "); serial_puts(SERIAL_COM1, blc_enabled ? "enabled" : "disabled"); serial_puts(SERIAL_COM1, ", ctl2 "); serial_puthex(SERIAL_COM1, ctl2[0]); serial_puthex(SERIAL_COM1, ctl2[1]); serial_puts(SERIAL_COM1, ", ctl "); serial_puthex(SERIAL_COM1, ctl[0]); serial_puthex(SERIAL_COM1, ctl[1]); serial_puts(SERIAL_COM1, "\n"); } int backlight_present(void) { return blc_present; } int backlight_percent(void) { return blc_present ? blc_level : -1; } unsigned int backlight_seq(void) { return blc_seq; } void backlight_set_percent(int percent) { unsigned int ctl2, duty; if (!blc_present) return; if (percent < BLC_MIN_PERCENT) percent = BLC_MIN_PERCENT; if (percent > 100) percent = 100; blc_seq++; duty = (blc_period * (unsigned int)percent + 50u) / 100u; if (duty > blc_period) duty = blc_period; if (percent && !duty) duty = 1; if (!blc_enabled) { ctl2 = blc_read(VLV_BLC_PWM_CTL2(blc_pipe)); blc_write(VLV_BLC_PWM_CTL2(blc_pipe), ctl2 & ~BLM_PWM_ENABLE); blc_write(VLV_BLC_PWM_CTL(blc_pipe), (blc_period << 16) | duty); (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe)); blc_write(VLV_BLC_PWM_CTL2(blc_pipe), (ctl2 & ~BLM_PWM_ENABLE) | BLM_PWM_ENABLE); (void)blc_read(VLV_BLC_PWM_CTL2(blc_pipe)); blc_enabled = 1; } else { blc_write(VLV_BLC_PWM_CTL(blc_pipe), (blc_period << 16) | duty); (void)blc_read(VLV_BLC_PWM_CTL(blc_pipe)); } blc_duty = duty; blc_level = percent; } void backlight_step(int dir) { int level; if (!blc_present) return; level = blc_level + (dir > 0 ? BLC_STEP_PERCENT : -BLC_STEP_PERCENT); backlight_set_percent(level); } int backlight_status(struct backlight_status *out) { unsigned int i; if (!out) return -1; for (i = 0; i < sizeof(*out); i++) ((unsigned char *)out)[i] = 0; out->present = blc_present; if (!blc_present) return -1; out->level = blc_level; out->enabled = blc_enabled; out->pipe = blc_pipe; out->period = blc_period; out->duty = blc_duty; out->bar = blc_bar; /* Keep the two pipe snapshots live for the `backlight probe` dump. */ out->ctl2[0] = blc_read(VLV_BLC_PWM_CTL2(0)); out->ctl2[1] = blc_read(VLV_BLC_PWM_CTL2(1)); out->ctl[0] = blc_read(VLV_BLC_PWM_CTL(0)); out->ctl[1] = blc_read(VLV_BLC_PWM_CTL(1)); return 0; }