diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index fed8da33424ee..1a13fa78877c7 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -197,17 +197,25 @@ struct dcp_brightness { u32 maximum; u32 dac; int nits; + /* The level the firmware reported last, in millinits. */ + u32 mnits; int scale; bool update; /* A level was written through the backlight device (desktop 26.x). */ bool requested; /* * Fade between levels where the firmware takes a DAC value in each - * swap. level is the one set last, in nits; crtc mutex. + * swap. level is the one set last, in the backlight device's units + * (see dcp_bl_dac_units()); crtc mutex. */ struct delayed_work fade_work; unsigned long fade_start; u32 level; + /* + * Level 0 was requested: the swap switches the backlight off (bl_power + * 0), as the poweroff swap does. + */ + bool off; u32 fade_from; u32 fade_to; bool fading; diff --git a/drivers/gpu/drm/apple/dcp_backlight.c b/drivers/gpu/drm/apple/dcp_backlight.c index b10ebd990dc31..36c9642889aa3 100644 --- a/drivers/gpu/drm/apple/dcp_backlight.c +++ b/drivers/gpu/drm/apple/dcp_backlight.c @@ -29,6 +29,14 @@ MODULE_PARM_DESC(v27_keep_level, "Firmware 27: keep the panel's level through a #define MIN_BRIGHTNESS_PART2 103U #define MAX_BRIGHTNESS_PART2 510U +/* + * Where a swap carries a DAC value, the backlight device counts in millinits. + * The DAC tables below are far finer than whole nits, and at the dim end of + * the range a whole nit is a step of 10 to 25 %: a slow change of level, such + * as one following the ambient light, was a visible staircase there. + */ +#define DCP_BL_DAC_UNITS 1000U + /* * lookup for display brightness 2 to 99 nits * */ @@ -244,6 +252,16 @@ int dcp_backlight_takeover(struct apple_dcp *dcp, u32 millinits) return iomfb_configure_backlight_h17p(dcp, maximum, true, nits, false, 0); } +/* + * The H17G panel shares the registration below but takes whole nits in its + * swaps; every other panel that gets here takes a DAC value. + */ +static u32 dcp_bl_dac_units(struct apple_dcp *dcp) +{ + return dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17G ? + 1 : DCP_BL_DAC_UNITS; +} + static int dcp_get_brightness(struct backlight_device *bd) { struct apple_dcp *dcp = bl_get_data(bd); @@ -273,7 +291,10 @@ static int dcp_get_brightness(struct backlight_device *bd) } spin_lock_irqsave(&dcp->backlight.lock, flags); + /* Without a present yet: the firmware's own report, in the device's units. */ actual = dcp_bl_presented_actual(&dcp->brightness.presented, + dcp_bl_dac_units(dcp) == DCP_BL_DAC_UNITS ? + READ_ONCE(dcp->brightness.mnits) : READ_ONCE(dcp->brightness.nits)); spin_unlock_irqrestore(&dcp->backlight.lock, flags); return actual; @@ -702,16 +723,22 @@ void dcp_backlight_desktop_finish(struct apple_dcp *dcp, u32 id, * into the lower one: for 370 nits the lower half became 0x10a, a 4 to 5 * nit value, and for most levels it left the DAC range altogether. */ -static u32 interpolate(int val, int min, int max, u32 *tbl, size_t tbl_size) +/* + * @val, @min and @max are millinits. At a whole nit the result is the one + * the whole-nit arithmetic gave: both divide the same ratio. + */ +static u32 interpolate(u32 val, u32 min, u32 max, u32 *tbl, size_t tbl_size) { u32 frac, word = 0; - u32 interpolated = (tbl_size - 1) * ((val - min) * SCALE_FACTOR) / (max - min); + u64 interpolated = div_u64((u64)(tbl_size - 1) * (val - min) * SCALE_FACTOR, + max - min); unsigned int shift; size_t index = interpolated / SCALE_FACTOR; - if (WARN(index + 1 >= tbl_size, "invalid index %zu for brightness %u\n", index, val)) - return tbl[tbl_size / 2]; + /* The end of the table is its last entry. */ + if (index + 1 >= tbl_size) + return tbl[tbl_size - 1]; frac = interpolated & (SCALE_FACTOR - 1); for (shift = 0; shift < 32; shift += 16) { @@ -724,27 +751,27 @@ static u32 interpolate(int val, int min, int max, u32 *tbl, size_t tbl_size) return word; } -static u32 calculate_dac(struct apple_dcp *dcp, int val) +/* @level is in the backlight device's units. */ +static u32 calculate_dac(struct apple_dcp *dcp, u32 level) { + const u32 min1 = MIN_BRIGHTNESS_PART1 * 1000, max1 = MAX_BRIGHTNESS_PART1 * 1000; + const u32 min2 = MIN_BRIGHTNESS_PART2 * 1000, max2 = MAX_BRIGHTNESS_PART2 * 1000; + u32 val = level * (1000 / dcp_bl_dac_units(dcp)); u32 dac; - if (val <= MIN_BRIGHTNESS_PART1) + if (val <= min1) return 16 * brightness_part1[0]; - else if (val == MAX_BRIGHTNESS_PART1) - return 16 * brightness_part1[ARRAY_SIZE(brightness_part1) - 1]; - else if (val == MIN_BRIGHTNESS_PART2) - return 16 * brightness_part2[0]; - else if (val >= MAX_BRIGHTNESS_PART2) + else if (val >= max2) return 16 * brightness_part2[ARRAY_SIZE(brightness_part2) - 1]; - if (val < MAX_BRIGHTNESS_PART1) { - dac = interpolate(val, MIN_BRIGHTNESS_PART1, MAX_BRIGHTNESS_PART1, + if (val <= max1) { + dac = interpolate(val, min1, max1, brightness_part1, ARRAY_SIZE(brightness_part1)); - } else if (val > MIN_BRIGHTNESS_PART2) { - dac = interpolate(val, MIN_BRIGHTNESS_PART2, MAX_BRIGHTNESS_PART2, + } else if (val >= min2) { + dac = interpolate(val, min2, max2, brightness_part2, ARRAY_SIZE(brightness_part2)); } else { - dac = interpolate(val, MAX_BRIGHTNESS_PART1, MIN_BRIGHTNESS_PART2, + dac = interpolate(val, max1, min2, brightness_part12, ARRAY_SIZE(brightness_part12)); } @@ -888,12 +915,12 @@ static void dcp_dac_fade_work(struct work_struct *work) } /* A lit panel with a mode fades to a new lit level; anything else jumps. */ -static bool dcp_dac_fade_starts(struct apple_dcp *dcp, u32 nits) +static bool dcp_dac_fade_starts(struct apple_dcp *dcp, u32 level) { struct dcp_brightness *b = &dcp->brightness; return dcp_has_panel(dcp) && READ_ONCE(dcp->mode_state.valid) && - b->level && nits && b->level != nits; + b->level && level && b->level != level; } static int dcp_set_brightness(struct backlight_device *bd) @@ -953,6 +980,7 @@ static int dcp_set_brightness(struct backlight_device *bd) return 0; } dcp->brightness.fading = false; + dcp->brightness.off = !brightness; dcp->brightness.level = brightness; dcp->brightness.dac = calculate_dac(dcp, brightness); dcp->brightness.update = true; @@ -1006,12 +1034,13 @@ int dcp_backlight_register(struct apple_dcp *dcp) struct backlight_device *bl_dev; int ret; unsigned long flags; + u32 units = dcp_bl_dac_units(dcp); struct backlight_properties props = { .type = BACKLIGHT_PLATFORM, - .brightness = dcp->brightness.nits, + .brightness = dcp->brightness.nits * units, .scale = BACKLIGHT_SCALE_LINEAR, }; - props.max_brightness = min(dcp->brightness.maximum, MAX_BRIGHTNESS_PART2 - 1); + props.max_brightness = min(dcp->brightness.maximum, MAX_BRIGHTNESS_PART2 - 1) * units; if ((dcp_backlight_presents_nits(dcp) || iomfb_is_m4(dcp)) && !dcp_backlight_active(dcp)) return -ENODATA; @@ -1076,8 +1105,8 @@ int dcp_backlight_register(struct apple_dcp *dcp) dcp_backlight_sync_hint(dcp); if (!dcp_backlight_active(dcp) && dcp->fw_compat != DCP_FIRMWARE_V_26_6_DESKTOP) { - dcp->brightness.level = dcp->brightness.nits; - dcp->brightness.dac = calculate_dac(dcp, dcp->brightness.nits); + dcp->brightness.level = dcp->brightness.nits * units; + dcp->brightness.dac = calculate_dac(dcp, dcp->brightness.level); } return 0; diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index c5ca1cb1f1408..d2a0c7a646a53 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -1117,6 +1117,8 @@ static void iomfb_cb_pr_publish(struct apple_dcp *dcp, struct iomfb_property *pr #endif if (dcp_has_panel(dcp)) { dcp->brightness.nits = prop->value / dcp->brightness.scale; + dcp->brightness.mnits = div_u64((u64)prop->value * 1000, + dcp->brightness.scale); /* notify backlight device of the initial brightness */ if (!dcp->brightness.bl_dev && dcp->brightness.maximum > 0) schedule_work(&dcp->bl_register_wq); @@ -4868,7 +4870,7 @@ void DCP_FW_NAME(iomfb_flush)(struct apple_dcp *dcp, struct drm_crtc *crtc, stru req->swap.bl_unk = 1; #endif req->swap.bl_value = dcp->brightness.dac; - req->swap.bl_power = 0x40; + req->swap.bl_power = dcp->brightness.off ? 0 : 0x40; dcp_backlight_presented(dcp, dcp->brightness.level); #endif dcp->brightness.update = false; diff --git a/drivers/gpu/drm/apple/systemep.c b/drivers/gpu/drm/apple/systemep.c index fc69580f4453e..f0bd772f535d6 100644 --- a/drivers/gpu/drm/apple/systemep.c +++ b/drivers/gpu/drm/apple/systemep.c @@ -125,6 +125,7 @@ static int powerlog_report(struct apple_epic_service *service, enum epic_subtype } dcp->brightness.nits = mnits.millinits / 1000; + dcp->brightness.mnits = mnits.millinits; /* H17G publishes no NITS property; its first report registers the device. */ if (dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17G && dcp_has_panel(dcp) && !dcp->brightness.bl_dev && dcp->brightness.maximum > 0) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py b/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py index 2973318e17192..888c2b5039765 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py @@ -204,7 +204,8 @@ def brightness_takeover(root): prefix = display.backlight(root).split('static void completed(')[0] prefix = prefix.replace('static inline ', 'static inline __attribute__((unused)) ') functions = '\n'.join(block(source, marker) for marker in ( - 'static bool dcp_backlight_desktop(', 'static int dcp_get_brightness(', + 'static bool dcp_backlight_desktop(', 'static u32 dcp_bl_dac_units(', + 'static int dcp_get_brightness(', 'void dcp_backlight_presented(', 'static void dcp_backlight_sync_hint(', 'int dcp_backlight_register(', 'static int dcp_set_brightness(', 'static bool dcp_backlight_controls_device(')) @@ -228,6 +229,7 @@ def brightness_takeover(root): static bool v27_keep_level = true; static bool pm_suspend_via_firmware(void) {return false;} #define MAX_BRIGHTNESS_PART2 510U +#define DCP_BL_DAC_UNITS 1000U #define BACKLIGHT_PLATFORM 1 #define BACKLIGHT_SCALE_LINEAR 1 #define BL_CORE_SUSPENDED 1 @@ -246,7 +248,7 @@ def brightness_takeover(root): struct apple_dcp { int fw_compat;bool quiescing,crashed; struct {int iomfb_method_profile;} hw; - struct {u32 maximum,nits,dac;bool update;struct backlight_device *bl_dev; + struct {u32 maximum,nits,mnits,dac;bool update,off;struct backlight_device *bl_dev; u32 level,fade_from,fade_to;bool fading;unsigned long fade_start;int fade_work; struct dcp_bl_presented presented;} brightness; struct {bool lock;struct dcp_backlight_state state;unsigned long fade_start; @@ -340,19 +342,24 @@ def brightness_takeover(root): assert(d.backlight.state.controlled && d.backlight.state.target==450); assert(registered.props.brightness==450 && !registered.ops_lock); /* - * DAC firmware (J414s, 13.5): the report of the loader's level until a - * present, then the presented level, through a display power cycle whose - * power-off report of 0 is not followed by one for the lit panel. + * DAC firmware (J414s, 13.5), in millinits: the report of the loader's + * level until a present, then the presented level, through a display + * power cycle whose power-off report of 0 is not followed by one for the + * lit panel. */ - struct apple_dcp dac={.fw_compat=0,.brightness={.maximum=500,.nits=140}}; + struct apple_dcp dac={.fw_compat=0,.brightness={.maximum=500,.nits=140,.mnits=140250}}; struct backlight_device dac_bl={.data=&dac}; - assert(dcp_get_brightness(&dac_bl)==140); - dcp_backlight_presented(&dac,500); - assert(dcp_get_brightness(&dac_bl)==500); - dcp_backlight_presented(&dac,0);dac.brightness.nits=0; + assert(dcp_get_brightness(&dac_bl)==140250); + dcp_backlight_presented(&dac,500000); + assert(dcp_get_brightness(&dac_bl)==500000); + dcp_backlight_presented(&dac,0);dac.brightness.nits=0;dac.brightness.mnits=0; assert(dcp_get_brightness(&dac_bl)==0); - dcp_backlight_presented(&dac,500); - assert(dcp_get_brightness(&dac_bl)==500 && !dac.backlight.lock); + dcp_backlight_presented(&dac,500000); + assert(dcp_get_brightness(&dac_bl)==500000 && !dac.backlight.lock); + /* H17G takes whole nits in its swaps and reports the level in them. */ + struct apple_dcp h17g={.hw={DCP_IOMFB_METHODS_H17G},.brightness={.maximum=500,.nits=140,.mnits=140250}}; + struct backlight_device h17g_bl={.data=&h17g}; + assert(dcp_get_brightness(&h17g_bl)==140); /* * A crashed processor discards every present: no level is reported, and * a new level is refused without changing the requested one. @@ -640,7 +647,7 @@ def powerlog_framing(root): enum epic_subtype { REPORT = 0xc0 }; struct work_struct { unsigned scheduled; }; static void __attribute__((unused)) schedule_work(struct work_struct *w) { w->scheduled++; } -struct apple_dcp {void *dev;int fw_compat;struct {int iomfb_method_profile;} hw;struct {u32 nits,maximum;void *bl_dev;} brightness;struct work_struct bl_register_wq;}; +struct apple_dcp {void *dev;int fw_compat;struct {int iomfb_method_profile;} hw;struct {u32 nits,mnits,maximum;void *bl_dev;} brightness;struct work_struct bl_register_wq;}; struct apple_epic_service; struct apple_dcp_afkep {struct apple_dcp *dcp;unsigned endpoint;struct apple_epic_service *service;}; struct apple_epic_service_ops {int (*report)(struct apple_epic_service *,enum epic_subtype,const void *,size_t);}; @@ -719,6 +726,10 @@ def powerlog_framing(root): memcpy(wire+16,report,sizeof(report)); afk_recv_compact_message(&ep,3,wire,16+sizeof(report)); assert(hints==4 && hint==123000 && !service.users); + /* DAC firmware keeps the report as the panel's level, also in millinits. */ + d.fw_compat=0; + afk_recv_compact_message(&ep,3,wire,16+sizeof(report)); + assert(hints==4 && d.brightness.nits==123 && d.brightness.mnits==123000 && !service.users); /* Bare and descriptor-wrapped EPIC service-call reports both preserve their header. */ { u8 cwire[16+sizeof(struct epic_compact_desc)+sizeof(struct epic_service_call)+0x10]={0}; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/display.py b/tools/testing/selftests/drivers/gpu/apple-dcp/display.py index a22159b54f62b..3f069ff504e58 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/display.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/display.py @@ -227,10 +227,11 @@ def dac_words(root): """The production DAC tables, interpolate() and calculate_dac() at every level.""" import re source = (root / 'drivers/gpu/drm/apple/dcp_backlight.c').read_text() - defines = '\n'.join(re.findall(r'^#define (?:MIN|MAX)_BRIGHTNESS_PART[12]\s.*$', source, re.M)) + defines = '\n'.join(re.findall(r'^#define (?:(?:MIN|MAX)_BRIGHTNESS_PART[12]|DCP_BL_DAC_UNITS)\s.*$', + source, re.M)) tables = '\n'.join(re.findall(r'^static u32 brightness_part[0-9]+\[\] = \{.*?\};', source, re.M | re.S)) functions = '\n'.join(block(source, marker) for marker in ( - 'static u32 interpolate(', 'static u32 calculate_dac(')) + 'static u32 dcp_bl_dac_units(', 'static u32 interpolate(', 'static u32 calculate_dac(')) return r""" #include #include @@ -241,7 +242,9 @@ def dac_words(root): #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define WARN(c, ...) (c) #define SCALE_FACTOR (1 << 10) -struct apple_dcp; +#define DCP_IOMFB_METHODS_H17G 1 +static u64 div_u64(u64 dividend, u32 divisor) { return dividend / divisor; } +struct apple_dcp { struct { int iomfb_method_profile; } hw; }; """ + defines + '\n' + tables + '\n' + functions + r""" /* Every word is two 16-bit DAC halves, each a table value times 16. */ static void check_word(u32 word, u32 *hi, u32 *lo) @@ -265,13 +268,20 @@ def dac_words(root): gap = widest(brightness_part1, ARRAY_SIZE(brightness_part1), 0); gap = widest(brightness_part12, ARRAY_SIZE(brightness_part12), gap); gap = widest(brightness_part2, ARRAY_SIZE(brightness_part2), gap); + /* A DAC panel counts in millinits, H17G in whole nits. */ + struct apple_dcp dac = {.hw = {.iomfb_method_profile = 0}}; + struct apple_dcp h17g = {.hw = {.iomfb_method_profile = DCP_IOMFB_METHODS_H17G}}; + const u32 k = DCP_BL_DAC_UNITS; /* The table points themselves, as before. */ - assert(calculate_dac(NULL, 2) == 0); - assert(calculate_dac(NULL, MAX_BRIGHTNESS_PART1) == 16 * brightness_part1[ARRAY_SIZE(brightness_part1) - 1]); - assert(calculate_dac(NULL, MIN_BRIGHTNESS_PART2) == 16 * brightness_part2[0]); - assert(calculate_dac(NULL, MAX_BRIGHTNESS_PART2) == 16 * brightness_part2[ARRAY_SIZE(brightness_part2) - 1]); - for (int nits = 0; nits <= (int)MAX_BRIGHTNESS_PART2; nits++) { - check_word(calculate_dac(NULL, nits), &hi, &lo); + assert(calculate_dac(&dac, 2 * k) == 0); + assert(calculate_dac(&dac, MAX_BRIGHTNESS_PART1 * k) == 16 * brightness_part1[ARRAY_SIZE(brightness_part1) - 1]); + assert(calculate_dac(&dac, MIN_BRIGHTNESS_PART2 * k) == 16 * brightness_part2[0]); + assert(calculate_dac(&dac, MAX_BRIGHTNESS_PART2 * k) == 16 * brightness_part2[ARRAY_SIZE(brightness_part2) - 1]); + for (u32 level = 0; level <= MAX_BRIGHTNESS_PART2 * k; level++) { + check_word(calculate_dac(&dac, level), &hi, &lo); + /* At a whole nit H17G gets the same word from its level in nits. */ + if (level % k == 0) + assert(calculate_dac(&h17g, level / k) == calculate_dac(&dac, level)); /* Both halves rise with the level; neither picks up the other's fraction. */ assert(hi >= prev_hi && lo >= prev_lo); /* The lower half stays the half step below, as in the table. */ @@ -280,7 +290,7 @@ def dac_words(root): prev_lo = lo; } /* 370 nits (J313): the lower half was 0x10a0, a 4 to 5 nit value. */ - check_word(calculate_dac(NULL, 370), &hi, &lo); + check_word(calculate_dac(&dac, 370 * k), &hi, &lo); assert(hi > 0x7800 && lo > 0x7800); return 0; }