Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'backlight-next-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/backlight

Pull backlight updates from Lee Jones:
"Fix-ups:
- Improve bootloader/kernel device handover

Bug Fixes:
- Stabilise backlight in ktd253 driver"

* tag 'backlight-next-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/backlight:
backlight: pwm_bl: Improve bootloader/kernel device handover
backlight: ktd253: Stabilize backlight

+83 -46
+55 -20
drivers/video/backlight/ktd253-backlight.c
··· 25 25 26 26 #define KTD253_T_LOW_NS (200 + 10) /* Additional 10ns as safety factor */ 27 27 #define KTD253_T_HIGH_NS (200 + 10) /* Additional 10ns as safety factor */ 28 + #define KTD253_T_OFF_CRIT_NS 100000 /* 100 us, now it doesn't look good */ 28 29 #define KTD253_T_OFF_MS 3 29 30 30 31 struct ktd253_backlight { ··· 35 34 u16 ratio; 36 35 }; 37 36 37 + static void ktd253_backlight_set_max_ratio(struct ktd253_backlight *ktd253) 38 + { 39 + gpiod_set_value_cansleep(ktd253->gpiod, 1); 40 + ndelay(KTD253_T_HIGH_NS); 41 + /* We always fall back to this when we power on */ 42 + } 43 + 44 + static int ktd253_backlight_stepdown(struct ktd253_backlight *ktd253) 45 + { 46 + /* 47 + * These GPIO operations absolutely can NOT sleep so no _cansleep 48 + * suffixes, and no using GPIO expanders on slow buses for this! 49 + * 50 + * The maximum number of cycles of the loop is 32 so the time taken 51 + * should nominally be: 52 + * (T_LOW_NS + T_HIGH_NS + loop_time) * 32 53 + * 54 + * Architectures do not always support ndelay() and we will get a few us 55 + * instead. If we get to a critical time limit an interrupt has likely 56 + * occured in the low part of the loop and we need to restart from the 57 + * top so we have the backlight in a known state. 58 + */ 59 + u64 ns; 60 + 61 + ns = ktime_get_ns(); 62 + gpiod_set_value(ktd253->gpiod, 0); 63 + ndelay(KTD253_T_LOW_NS); 64 + gpiod_set_value(ktd253->gpiod, 1); 65 + ns = ktime_get_ns() - ns; 66 + if (ns >= KTD253_T_OFF_CRIT_NS) { 67 + dev_err(ktd253->dev, "PCM on backlight took too long (%llu ns)\n", ns); 68 + return -EAGAIN; 69 + } 70 + ndelay(KTD253_T_HIGH_NS); 71 + return 0; 72 + } 73 + 38 74 static int ktd253_backlight_update_status(struct backlight_device *bl) 39 75 { 40 76 struct ktd253_backlight *ktd253 = bl_get_data(bl); 41 77 int brightness = backlight_get_brightness(bl); 42 78 u16 target_ratio; 43 79 u16 current_ratio = ktd253->ratio; 44 - unsigned long flags; 80 + int ret; 45 81 46 82 dev_dbg(ktd253->dev, "new brightness/ratio: %d/32\n", brightness); 47 83 ··· 100 62 } 101 63 102 64 if (current_ratio == 0) { 103 - gpiod_set_value_cansleep(ktd253->gpiod, 1); 104 - ndelay(KTD253_T_HIGH_NS); 105 - /* We always fall back to this when we power on */ 65 + ktd253_backlight_set_max_ratio(ktd253); 106 66 current_ratio = KTD253_MAX_RATIO; 107 67 } 108 68 109 - /* 110 - * WARNING: 111 - * The loop to set the correct current level is performed 112 - * with interrupts disabled as it is timing critical. 113 - * The maximum number of cycles of the loop is 32 114 - * so the time taken will be (T_LOW_NS + T_HIGH_NS + loop_time) * 32, 115 - */ 116 - local_irq_save(flags); 117 69 while (current_ratio != target_ratio) { 118 70 /* 119 71 * These GPIO operations absolutely can NOT sleep so no 120 72 * _cansleep suffixes, and no using GPIO expanders on 121 73 * slow buses for this! 122 74 */ 123 - gpiod_set_value(ktd253->gpiod, 0); 124 - ndelay(KTD253_T_LOW_NS); 125 - gpiod_set_value(ktd253->gpiod, 1); 126 - ndelay(KTD253_T_HIGH_NS); 127 - /* After 1/32 we loop back to 32/32 */ 128 - if (current_ratio == KTD253_MIN_RATIO) 75 + ret = ktd253_backlight_stepdown(ktd253); 76 + if (ret == -EAGAIN) { 77 + /* 78 + * Something disturbed the backlight setting code when 79 + * running so we need to bring the PWM back to a known 80 + * state. This shouldn't happen too much. 81 + */ 82 + gpiod_set_value_cansleep(ktd253->gpiod, 0); 83 + msleep(KTD253_T_OFF_MS); 84 + ktd253_backlight_set_max_ratio(ktd253); 129 85 current_ratio = KTD253_MAX_RATIO; 130 - else 86 + } else if (current_ratio == KTD253_MIN_RATIO) { 87 + /* After 1/32 we loop back to 32/32 */ 88 + current_ratio = KTD253_MAX_RATIO; 89 + } else { 131 90 current_ratio--; 91 + } 132 92 } 133 - local_irq_restore(flags); 134 93 ktd253->ratio = current_ratio; 135 94 136 95 dev_dbg(ktd253->dev, "new ratio set to %d/32\n", target_ratio);
+28 -26
drivers/video/backlight/pwm_bl.c
··· 409 409 static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb) 410 410 { 411 411 struct device_node *node = pb->dev->of_node; 412 + bool active = true; 413 + 414 + /* 415 + * If the enable GPIO is present, observable (either as input 416 + * or output) and off then the backlight is not currently active. 417 + * */ 418 + if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) 419 + active = false; 420 + 421 + if (!regulator_is_enabled(pb->power_supply)) 422 + active = false; 423 + 424 + if (!pwm_is_enabled(pb->pwm)) 425 + active = false; 426 + 427 + /* 428 + * Synchronize the enable_gpio with the observed state of the 429 + * hardware. 430 + */ 431 + if (pb->enable_gpio) 432 + gpiod_direction_output(pb->enable_gpio, active); 433 + 434 + /* 435 + * Do not change pb->enabled here! pb->enabled essentially 436 + * tells us if we own one of the regulator's use counts and 437 + * right now we do not. 438 + */ 412 439 413 440 /* Not booted with device tree or no phandle link to the node */ 414 441 if (!node || !node->phandle) ··· 447 420 * assume that another driver will enable the backlight at the 448 421 * appropriate time. Therefore, if it is disabled, keep it so. 449 422 */ 450 - 451 - /* if the enable GPIO is disabled, do not enable the backlight */ 452 - if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0) 453 - return FB_BLANK_POWERDOWN; 454 - 455 - /* The regulator is disabled, do not enable the backlight */ 456 - if (!regulator_is_enabled(pb->power_supply)) 457 - return FB_BLANK_POWERDOWN; 458 - 459 - /* The PWM is disabled, keep it like this */ 460 - if (!pwm_is_enabled(pb->pwm)) 461 - return FB_BLANK_POWERDOWN; 462 - 463 - return FB_BLANK_UNBLANK; 423 + return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN; 464 424 } 465 425 466 426 static int pwm_backlight_probe(struct platform_device *pdev) ··· 499 485 ret = PTR_ERR(pb->enable_gpio); 500 486 goto err_alloc; 501 487 } 502 - 503 - /* 504 - * If the GPIO is not known to be already configured as output, that 505 - * is, if gpiod_get_direction returns either 1 or -EINVAL, change the 506 - * direction to output and set the GPIO as active. 507 - * Do not force the GPIO to active when it was already output as it 508 - * could cause backlight flickering or we would enable the backlight too 509 - * early. Leave the decision of the initial backlight state for later. 510 - */ 511 - if (pb->enable_gpio && 512 - gpiod_get_direction(pb->enable_gpio) != 0) 513 - gpiod_direction_output(pb->enable_gpio, 1); 514 488 515 489 pb->power_supply = devm_regulator_get(&pdev->dev, "power"); 516 490 if (IS_ERR(pb->power_supply)) {