backlight: pwm_bl: Switch to using "atomic" PWM API
The "atomic" API allows us to configure PWM period and duty_cycle and enable it in one call. The patch also moves the pwm_init_state just before any use of the pwm_state struct, this fixes a potential bug where pwm_get_state can be called before pwm_init_state. Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com> Reviewed-by: Daniel Thompson <daniel.thompson@linaro.org> Tested-by: Heiko Stuebner <heiko@sntech.de> Signed-off-by: Lee Jones <lee.jones@linaro.org>
This commit is contained in:
		
							parent
							
								
									0b193400b3
								
							
						
					
					
						commit
						e6bcca0890
					
				| @ -28,10 +28,8 @@ | |||||||
| struct pwm_bl_data { | struct pwm_bl_data { | ||||||
| 	struct pwm_device	*pwm; | 	struct pwm_device	*pwm; | ||||||
| 	struct device		*dev; | 	struct device		*dev; | ||||||
| 	unsigned int		period; |  | ||||||
| 	unsigned int		lth_brightness; | 	unsigned int		lth_brightness; | ||||||
| 	unsigned int		*levels; | 	unsigned int		*levels; | ||||||
| 	bool			enabled; |  | ||||||
| 	struct regulator	*power_supply; | 	struct regulator	*power_supply; | ||||||
| 	struct gpio_desc	*enable_gpio; | 	struct gpio_desc	*enable_gpio; | ||||||
| 	unsigned int		scale; | 	unsigned int		scale; | ||||||
| @ -46,31 +44,35 @@ struct pwm_bl_data { | |||||||
| 	void			(*exit)(struct device *); | 	void			(*exit)(struct device *); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness) | static void pwm_backlight_power_on(struct pwm_bl_data *pb) | ||||||
| { | { | ||||||
|  | 	struct pwm_state state; | ||||||
| 	int err; | 	int err; | ||||||
| 
 | 
 | ||||||
| 	if (pb->enabled) | 	pwm_get_state(pb->pwm, &state); | ||||||
|  | 	if (state.enabled) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	err = regulator_enable(pb->power_supply); | 	err = regulator_enable(pb->power_supply); | ||||||
| 	if (err < 0) | 	if (err < 0) | ||||||
| 		dev_err(pb->dev, "failed to enable power supply\n"); | 		dev_err(pb->dev, "failed to enable power supply\n"); | ||||||
| 
 | 
 | ||||||
| 	pwm_enable(pb->pwm); | 	state.enabled = true; | ||||||
|  | 	pwm_apply_state(pb->pwm, &state); | ||||||
| 
 | 
 | ||||||
| 	if (pb->post_pwm_on_delay) | 	if (pb->post_pwm_on_delay) | ||||||
| 		msleep(pb->post_pwm_on_delay); | 		msleep(pb->post_pwm_on_delay); | ||||||
| 
 | 
 | ||||||
| 	if (pb->enable_gpio) | 	if (pb->enable_gpio) | ||||||
| 		gpiod_set_value_cansleep(pb->enable_gpio, 1); | 		gpiod_set_value_cansleep(pb->enable_gpio, 1); | ||||||
| 
 |  | ||||||
| 	pb->enabled = true; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static void pwm_backlight_power_off(struct pwm_bl_data *pb) | static void pwm_backlight_power_off(struct pwm_bl_data *pb) | ||||||
| { | { | ||||||
| 	if (!pb->enabled) | 	struct pwm_state state; | ||||||
|  | 
 | ||||||
|  | 	pwm_get_state(pb->pwm, &state); | ||||||
|  | 	if (!state.enabled) | ||||||
| 		return; | 		return; | ||||||
| 
 | 
 | ||||||
| 	if (pb->enable_gpio) | 	if (pb->enable_gpio) | ||||||
| @ -79,24 +81,27 @@ static void pwm_backlight_power_off(struct pwm_bl_data *pb) | |||||||
| 	if (pb->pwm_off_delay) | 	if (pb->pwm_off_delay) | ||||||
| 		msleep(pb->pwm_off_delay); | 		msleep(pb->pwm_off_delay); | ||||||
| 
 | 
 | ||||||
| 	pwm_config(pb->pwm, 0, pb->period); | 	state.enabled = false; | ||||||
| 	pwm_disable(pb->pwm); | 	state.duty_cycle = 0; | ||||||
|  | 	pwm_apply_state(pb->pwm, &state); | ||||||
| 
 | 
 | ||||||
| 	regulator_disable(pb->power_supply); | 	regulator_disable(pb->power_supply); | ||||||
| 	pb->enabled = false; |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness) | static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness) | ||||||
| { | { | ||||||
| 	unsigned int lth = pb->lth_brightness; | 	unsigned int lth = pb->lth_brightness; | ||||||
|  | 	struct pwm_state state; | ||||||
| 	u64 duty_cycle; | 	u64 duty_cycle; | ||||||
| 
 | 
 | ||||||
|  | 	pwm_get_state(pb->pwm, &state); | ||||||
|  | 
 | ||||||
| 	if (pb->levels) | 	if (pb->levels) | ||||||
| 		duty_cycle = pb->levels[brightness]; | 		duty_cycle = pb->levels[brightness]; | ||||||
| 	else | 	else | ||||||
| 		duty_cycle = brightness; | 		duty_cycle = brightness; | ||||||
| 
 | 
 | ||||||
| 	duty_cycle *= pb->period - lth; | 	duty_cycle *= state.period - lth; | ||||||
| 	do_div(duty_cycle, pb->scale); | 	do_div(duty_cycle, pb->scale); | ||||||
| 
 | 
 | ||||||
| 	return duty_cycle + lth; | 	return duty_cycle + lth; | ||||||
| @ -106,7 +111,7 @@ static int pwm_backlight_update_status(struct backlight_device *bl) | |||||||
| { | { | ||||||
| 	struct pwm_bl_data *pb = bl_get_data(bl); | 	struct pwm_bl_data *pb = bl_get_data(bl); | ||||||
| 	int brightness = bl->props.brightness; | 	int brightness = bl->props.brightness; | ||||||
| 	int duty_cycle; | 	struct pwm_state state; | ||||||
| 
 | 
 | ||||||
| 	if (bl->props.power != FB_BLANK_UNBLANK || | 	if (bl->props.power != FB_BLANK_UNBLANK || | ||||||
| 	    bl->props.fb_blank != FB_BLANK_UNBLANK || | 	    bl->props.fb_blank != FB_BLANK_UNBLANK || | ||||||
| @ -117,9 +122,10 @@ static int pwm_backlight_update_status(struct backlight_device *bl) | |||||||
| 		brightness = pb->notify(pb->dev, brightness); | 		brightness = pb->notify(pb->dev, brightness); | ||||||
| 
 | 
 | ||||||
| 	if (brightness > 0) { | 	if (brightness > 0) { | ||||||
| 		duty_cycle = compute_duty_cycle(pb, brightness); | 		pwm_get_state(pb->pwm, &state); | ||||||
| 		pwm_config(pb->pwm, duty_cycle, pb->period); | 		state.duty_cycle = compute_duty_cycle(pb, brightness); | ||||||
| 		pwm_backlight_power_on(pb, brightness); | 		pwm_apply_state(pb->pwm, &state); | ||||||
|  | 		pwm_backlight_power_on(pb); | ||||||
| 	} else | 	} else | ||||||
| 		pwm_backlight_power_off(pb); | 		pwm_backlight_power_off(pb); | ||||||
| 
 | 
 | ||||||
| @ -447,7 +453,6 @@ static int pwm_backlight_probe(struct platform_device *pdev) | |||||||
| 	struct device_node *node = pdev->dev.of_node; | 	struct device_node *node = pdev->dev.of_node; | ||||||
| 	struct pwm_bl_data *pb; | 	struct pwm_bl_data *pb; | ||||||
| 	struct pwm_state state; | 	struct pwm_state state; | ||||||
| 	struct pwm_args pargs; |  | ||||||
| 	unsigned int i; | 	unsigned int i; | ||||||
| 	int ret; | 	int ret; | ||||||
| 
 | 
 | ||||||
| @ -478,7 +483,6 @@ static int pwm_backlight_probe(struct platform_device *pdev) | |||||||
| 	pb->check_fb = data->check_fb; | 	pb->check_fb = data->check_fb; | ||||||
| 	pb->exit = data->exit; | 	pb->exit = data->exit; | ||||||
| 	pb->dev = &pdev->dev; | 	pb->dev = &pdev->dev; | ||||||
| 	pb->enabled = false; |  | ||||||
| 	pb->post_pwm_on_delay = data->post_pwm_on_delay; | 	pb->post_pwm_on_delay = data->post_pwm_on_delay; | ||||||
| 	pb->pwm_off_delay = data->pwm_off_delay; | 	pb->pwm_off_delay = data->pwm_off_delay; | ||||||
| 
 | 
 | ||||||
| @ -539,10 +543,26 @@ static int pwm_backlight_probe(struct platform_device *pdev) | |||||||
| 
 | 
 | ||||||
| 	dev_dbg(&pdev->dev, "got pwm for backlight\n"); | 	dev_dbg(&pdev->dev, "got pwm for backlight\n"); | ||||||
| 
 | 
 | ||||||
| 	if (!data->levels) { | 	/* Sync up PWM state. */ | ||||||
| 		/* Get the PWM period (in nanoseconds) */ | 	pwm_init_state(pb->pwm, &state); | ||||||
| 		pwm_get_state(pb->pwm, &state); |  | ||||||
| 
 | 
 | ||||||
|  | 	/*
 | ||||||
|  | 	 * The DT case will set the pwm_period_ns field to 0 and store the | ||||||
|  | 	 * period, parsed from the DT, in the PWM device. For the non-DT case, | ||||||
|  | 	 * set the period from platform data if it has not already been set | ||||||
|  | 	 * via the PWM lookup table. | ||||||
|  | 	 */ | ||||||
|  | 	if (!state.period && (data->pwm_period_ns > 0)) | ||||||
|  | 		state.period = data->pwm_period_ns; | ||||||
|  | 
 | ||||||
|  | 	ret = pwm_apply_state(pb->pwm, &state); | ||||||
|  | 	if (ret) { | ||||||
|  | 		dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n", | ||||||
|  | 			ret); | ||||||
|  | 		goto err_alloc; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (!data->levels) { | ||||||
| 		ret = pwm_backlight_brightness_default(&pdev->dev, data, | 		ret = pwm_backlight_brightness_default(&pdev->dev, data, | ||||||
| 						       state.period); | 						       state.period); | ||||||
| 		if (ret < 0) { | 		if (ret < 0) { | ||||||
| @ -559,24 +579,7 @@ static int pwm_backlight_probe(struct platform_device *pdev) | |||||||
| 		pb->levels = data->levels; | 		pb->levels = data->levels; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	/*
 | 	pb->lth_brightness = data->lth_brightness * (state.period / pb->scale); | ||||||
| 	 * FIXME: pwm_apply_args() should be removed when switching to |  | ||||||
| 	 * the atomic PWM API. |  | ||||||
| 	 */ |  | ||||||
| 	pwm_apply_args(pb->pwm); |  | ||||||
| 
 |  | ||||||
| 	/*
 |  | ||||||
| 	 * The DT case will set the pwm_period_ns field to 0 and store the |  | ||||||
| 	 * period, parsed from the DT, in the PWM device. For the non-DT case, |  | ||||||
| 	 * set the period from platform data if it has not already been set |  | ||||||
| 	 * via the PWM lookup table. |  | ||||||
| 	 */ |  | ||||||
| 	pwm_get_args(pb->pwm, &pargs); |  | ||||||
| 	pb->period = pargs.period; |  | ||||||
| 	if (!pb->period && (data->pwm_period_ns > 0)) |  | ||||||
| 		pb->period = data->pwm_period_ns; |  | ||||||
| 
 |  | ||||||
| 	pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale); |  | ||||||
| 
 | 
 | ||||||
| 	memset(&props, 0, sizeof(struct backlight_properties)); | 	memset(&props, 0, sizeof(struct backlight_properties)); | ||||||
| 	props.type = BACKLIGHT_RAW; | 	props.type = BACKLIGHT_RAW; | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user