Skip to content

Commit 7613c92

Browse files
Peter UjfalusiLee Jones
Peter Ujfalusi
authored and
Lee Jones
committed
backlight: pwm_bl: Move the checks for initial power state to a separate function
Move the checks to select the initial state for the backlight to a new function and document the checks we are doing. With the separate function it is going to be easier to fix or improve the initial power state configuration later and it is easier to read the code. Signed-off-by: Peter Ujfalusi <[email protected]> Reviewed-by: Philipp Zabel <[email protected]> Reviewed-by: Thierry Reding <[email protected]> Signed-off-by: Lee Jones <[email protected]>
1 parent 0eb3fba commit 7613c92

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed

drivers/video/backlight/pwm_bl.c

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,32 @@ static int pwm_backlight_parse_dt(struct device *dev,
192192
}
193193
#endif
194194

195+
static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
196+
{
197+
struct device_node *node = pb->dev->of_node;
198+
199+
/* Not booted with device tree or no phandle link to the node */
200+
if (!node || !node->phandle)
201+
return FB_BLANK_UNBLANK;
202+
203+
/*
204+
* If the driver is probed from the device tree and there is a
205+
* phandle link pointing to the backlight node, it is safe to
206+
* assume that another driver will enable the backlight at the
207+
* appropriate time. Therefore, if it is disabled, keep it so.
208+
*/
209+
210+
/* if the enable GPIO is disabled, do not enable the backlight */
211+
if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
212+
return FB_BLANK_POWERDOWN;
213+
214+
/* The regulator is disabled, do not enable the backlight */
215+
if (!regulator_is_enabled(pb->power_supply))
216+
return FB_BLANK_POWERDOWN;
217+
218+
return FB_BLANK_UNBLANK;
219+
}
220+
195221
static int pwm_backlight_probe(struct platform_device *pdev)
196222
{
197223
struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
@@ -200,7 +226,6 @@ static int pwm_backlight_probe(struct platform_device *pdev)
200226
struct backlight_device *bl;
201227
struct device_node *node = pdev->dev.of_node;
202228
struct pwm_bl_data *pb;
203-
int initial_blank = FB_BLANK_UNBLANK;
204229
struct pwm_args pargs;
205230
int ret;
206231

@@ -267,30 +292,23 @@ static int pwm_backlight_probe(struct platform_device *pdev)
267292
pb->enable_gpio = gpio_to_desc(data->enable_gpio);
268293
}
269294

270-
if (pb->enable_gpio) {
271-
/*
272-
* If the driver is probed from the device tree and there is a
273-
* phandle link pointing to the backlight node, it is safe to
274-
* assume that another driver will enable the backlight at the
275-
* appropriate time. Therefore, if it is disabled, keep it so.
276-
*/
277-
if (node && node->phandle &&
278-
gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_OUT &&
279-
gpiod_get_value(pb->enable_gpio) == 0)
280-
initial_blank = FB_BLANK_POWERDOWN;
281-
else
282-
gpiod_direction_output(pb->enable_gpio, 1);
283-
}
295+
/*
296+
* If the GPIO is configured as input, change the direction to output
297+
* and set the GPIO as active.
298+
* Do not force the GPIO to active when it was already output as it
299+
* could cause backlight flickering or we would enable the backlight too
300+
* early. Leave the decision of the initial backlight state for later.
301+
*/
302+
if (pb->enable_gpio &&
303+
gpiod_get_direction(pb->enable_gpio) == GPIOF_DIR_IN)
304+
gpiod_direction_output(pb->enable_gpio, 1);
284305

285306
pb->power_supply = devm_regulator_get(&pdev->dev, "power");
286307
if (IS_ERR(pb->power_supply)) {
287308
ret = PTR_ERR(pb->power_supply);
288309
goto err_alloc;
289310
}
290311

291-
if (node && node->phandle && !regulator_is_enabled(pb->power_supply))
292-
initial_blank = FB_BLANK_POWERDOWN;
293-
294312
pb->pwm = devm_pwm_get(&pdev->dev, NULL);
295313
if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
296314
dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
@@ -347,7 +365,7 @@ static int pwm_backlight_probe(struct platform_device *pdev)
347365
}
348366

349367
bl->props.brightness = data->dft_brightness;
350-
bl->props.power = initial_blank;
368+
bl->props.power = pwm_backlight_initial_power_state(pb);
351369
backlight_update_status(bl);
352370

353371
platform_set_drvdata(pdev, bl);

0 commit comments

Comments
 (0)