Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Simple PWM based backlight control, board code has to setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * 1) pin configuration so PWM waveforms can output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * 2) platform_data being correctly configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pwm_backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) struct pwm_bl_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	struct pwm_device	*pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	struct device		*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	unsigned int		lth_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int		*levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	bool			enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct regulator	*power_supply;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct gpio_desc	*enable_gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	unsigned int		scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	bool			legacy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	unsigned int		post_pwm_on_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	unsigned int		pwm_off_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	int			(*notify)(struct device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 					  int brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	void			(*notify_after)(struct device *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 					int brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int			(*check_fb)(struct device *, struct fb_info *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	void			(*exit)(struct device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static void pwm_backlight_power_on(struct pwm_bl_data *pb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	pwm_get_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (pb->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	err = regulator_enable(pb->power_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		dev_err(pb->dev, "failed to enable power supply\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	state.enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	pwm_apply_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (pb->post_pwm_on_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		msleep(pb->post_pwm_on_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (pb->enable_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		gpiod_set_value_cansleep(pb->enable_gpio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	pb->enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) static void pwm_backlight_power_off(struct pwm_bl_data *pb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	pwm_get_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!pb->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (pb->enable_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		gpiod_set_value_cansleep(pb->enable_gpio, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (pb->pwm_off_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		msleep(pb->pwm_off_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	state.enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	state.duty_cycle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	pwm_apply_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	regulator_disable(pb->power_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	pb->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	unsigned int lth = pb->lth_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	u64 duty_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	pwm_get_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (pb->levels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		duty_cycle = pb->levels[brightness];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		duty_cycle = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	duty_cycle *= state.period - lth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	do_div(duty_cycle, pb->scale);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	return duty_cycle + lth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int pwm_backlight_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct pwm_bl_data *pb = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int brightness = backlight_get_brightness(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	if (pb->notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		brightness = pb->notify(pb->dev, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (brightness > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		pwm_get_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		state.duty_cycle = compute_duty_cycle(pb, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		pwm_apply_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		pwm_backlight_power_on(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		pwm_backlight_power_off(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (pb->notify_after)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		pb->notify_after(pb->dev, brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static int pwm_backlight_check_fb(struct backlight_device *bl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				  struct fb_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	struct pwm_bl_data *pb = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	return !pb->check_fb || pb->check_fb(pb->dev, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static const struct backlight_ops pwm_backlight_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	.update_status	= pwm_backlight_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	.check_fb	= pwm_backlight_check_fb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define PWM_LUMINANCE_SHIFT	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define PWM_LUMINANCE_SCALE	(1 << PWM_LUMINANCE_SHIFT) /* luminance scale */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * CIE lightness to PWM conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * The CIE 1931 lightness formula is what actually describes how we perceive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * light:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  *          Y = (L* / 903.3)           if L* ≤ 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  *          Y = ((L* + 16) / 116)^3    if L* > 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * Where Y is the luminance, the amount of light coming out of the screen, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * perceives the screen to be, and is a number between 0 and 100.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * The following function does the fixed point maths needed to implement the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * above formula.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) static u64 cie1931(unsigned int lightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	u64 retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	 * @lightness is given as a number between 0 and 1, expressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	 * as a fixed-point number in scale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	 * PWM_LUMINANCE_SCALE. Convert to a percentage, still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	 * expressed as a fixed-point number, so the above formulas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	 * can be applied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	lightness *= 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (lightness <= (8 * PWM_LUMINANCE_SCALE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		retval = DIV_ROUND_CLOSEST(lightness * 10, 9033);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		retval = (lightness + (16 * PWM_LUMINANCE_SCALE)) / 116;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		retval *= retval * retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		retval += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		retval >>= 2*PWM_LUMINANCE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * Create a default correction table for PWM values to create linear brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * for LED based backlights using the CIE1931 algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) int pwm_backlight_brightness_default(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 				     struct platform_pwm_backlight_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 				     unsigned int period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	u64 retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * Once we have 4096 levels there's little point going much higher...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * neither interactive sliders nor animation benefits from having
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * more values in the table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	data->max_brightness =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		min((int)DIV_ROUND_UP(period, fls(period)), 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	data->levels = devm_kcalloc(dev, data->max_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 				    sizeof(*data->levels), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (!data->levels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	/* Fill the table using the cie1931 algorithm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	for (i = 0; i < data->max_brightness; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		retval = cie1931((i * PWM_LUMINANCE_SCALE) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 				 data->max_brightness) * period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		if (retval > UINT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		data->levels[i] = (unsigned int)retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	data->dft_brightness = data->max_brightness / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	data->max_brightness--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int pwm_backlight_parse_dt(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				  struct platform_pwm_backlight_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct device_node *node = dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	unsigned int num_levels = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	unsigned int levels_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	unsigned int num_steps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	unsigned int *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	u32 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	memset(data, 0, sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	 * These values are optional and set as 0 by default, the out values
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	 * are modified only if a valid u32 value can be decoded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	of_property_read_u32(node, "post-pwm-on-delay-ms",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			     &data->post_pwm_on_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	 * Determine the number of brightness levels, if this property is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	 * set a default table of brightness levels will be used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	prop = of_find_property(node, "brightness-levels", &length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	data->max_brightness = length / sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	/* read brightness levels from DT property */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (data->max_brightness > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		size_t size = sizeof(*data->levels) * data->max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		unsigned int i, j, n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		if (!data->levels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		ret = of_property_read_u32_array(node, "brightness-levels",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 						 data->levels,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 						 data->max_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		ret = of_property_read_u32(node, "default-brightness-level",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 					   &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		data->dft_brightness = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		 * This property is optional, if is set enables linear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		 * interpolation between each of the values of brightness levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		 * and creates a new pre-computed table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		of_property_read_u32(node, "num-interpolated-steps",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 				     &num_steps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		 * Make sure that there is at least two entries in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		 * brightness-levels table, otherwise we can't interpolate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		 * between two points.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		if (num_steps) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			if (data->max_brightness < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 				dev_err(dev, "can't interpolate\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			 * Recalculate the number of brightness levels, now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			 * taking in consideration the number of interpolated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 			 * steps between two levels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 			for (i = 0; i < data->max_brightness - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 				if ((data->levels[i + 1] - data->levels[i]) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 				   num_steps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 					num_levels += num_steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 				else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					num_levels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			num_levels++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			dev_dbg(dev, "new number of brightness levels: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				num_levels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 			 * Create a new table of brightness levels with all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			 * interpolated steps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 			size = sizeof(*table) * num_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			table = devm_kzalloc(dev, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			if (!table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 			/* Fill the interpolated table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			levels_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			for (i = 0; i < data->max_brightness - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 				value = data->levels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 				n = (data->levels[i + 1] - value) / num_steps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 				if (n > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 					for (j = 0; j < num_steps; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 						table[levels_count] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 						value += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 						levels_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 					}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 					table[levels_count] = data->levels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 					levels_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			table[levels_count] = data->levels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			 * As we use interpolation lets remove current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			 * brightness levels table and replace for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 			 * new interpolated table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			devm_kfree(dev, data->levels);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 			data->levels = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			 * Reassign max_brightness value to the new total number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			 * of brightness levels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			data->max_brightness = num_levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 		data->max_brightness--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static const struct of_device_id pwm_backlight_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	{ .compatible = "pwm-backlight" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int pwm_backlight_parse_dt(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 				  struct platform_pwm_backlight_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) int pwm_backlight_brightness_default(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 				     struct platform_pwm_backlight_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 				     unsigned int period)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	unsigned int nlevels = data->max_brightness + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	unsigned int min_val = data->levels[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	unsigned int max_val = data->levels[nlevels - 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	 * Multiplying by 128 means that even in pathological cases such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	 * as (max_val - min_val) == nlevels the error at max_val is less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	 * than 1%.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	unsigned int slope = (128 * (max_val - min_val)) / nlevels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	unsigned int margin = (max_val - min_val) / 20; /* 5% */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	for (i = 1; i < nlevels; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		unsigned int linear_value = min_val + ((i * slope) / 128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		unsigned int delta = abs(linear_value - data->levels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		if (delta > margin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	struct device_node *node = pb->dev->of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	bool active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	 * If the enable GPIO is present, observable (either as input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 * or output) and off then the backlight is not currently active.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	 * */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (!regulator_is_enabled(pb->power_supply))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (!pwm_is_enabled(pb->pwm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	 * Synchronize the enable_gpio with the observed state of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	 * hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (pb->enable_gpio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		gpiod_direction_output(pb->enable_gpio, active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	 * Do not change pb->enabled here! pb->enabled essentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	 * tells us if we own one of the regulator's use counts and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * right now we do not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	/* Not booted with device tree or no phandle link to the node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	if (!node || !node->phandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		return FB_BLANK_UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 * If the driver is probed from the device tree and there is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	 * phandle link pointing to the backlight node, it is safe to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	 * assume that another driver will enable the backlight at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 * appropriate time. Therefore, if it is disabled, keep it so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) static int pwm_backlight_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	struct platform_pwm_backlight_data defdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct device_node *node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	struct pwm_bl_data *pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	struct pwm_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 			dev_err(&pdev->dev, "failed to find platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		data = &defdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	if (data->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		ret = data->init(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (!pb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	pb->notify = data->notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	pb->notify_after = data->notify_after;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	pb->check_fb = data->check_fb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	pb->exit = data->exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	pb->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	pb->enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	pb->post_pwm_on_delay = data->post_pwm_on_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	pb->pwm_off_delay = data->pwm_off_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 						  GPIOD_ASIS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (IS_ERR(pb->enable_gpio)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		ret = PTR_ERR(pb->enable_gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	pb->power_supply = devm_regulator_get(&pdev->dev, "power");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	if (IS_ERR(pb->power_supply)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		ret = PTR_ERR(pb->power_supply);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	pb->pwm = devm_pwm_get(&pdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		pb->legacy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (IS_ERR(pb->pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		ret = PTR_ERR(pb->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		if (ret != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 			dev_err(&pdev->dev, "unable to request PWM\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	dev_dbg(&pdev->dev, "got pwm for backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	/* Sync up PWM state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	pwm_init_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	 * The DT case will set the pwm_period_ns field to 0 and store the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	 * period, parsed from the DT, in the PWM device. For the non-DT case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	 * set the period from platform data if it has not already been set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	 * via the PWM lookup table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	if (!state.period && (data->pwm_period_ns > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		state.period = data->pwm_period_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	ret = pwm_apply_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		dev_err(&pdev->dev, "failed to apply initial PWM state: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 			ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	memset(&props, 0, sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	if (data->levels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		pb->levels = data->levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		 * For the DT case, only when brightness levels is defined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		 * data->levels is filled. For the non-DT case, data->levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		 * can come from platform data, however is not usual.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		for (i = 0; i <= data->max_brightness; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			if (data->levels[i] > pb->scale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 				pb->scale = data->levels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		if (pwm_backlight_is_linear(data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			props.scale = BACKLIGHT_SCALE_LINEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			props.scale = BACKLIGHT_SCALE_NON_LINEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	} else if (!data->max_brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		 * If no brightness levels are provided and max_brightness is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		 * not set, use the default brightness table. For the DT case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		 * max_brightness is set to 0 when brightness levels is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		 * specified. For the non-DT case, max_brightness is usually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		 * set to some value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		/* Get the PWM period (in nanoseconds) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		pwm_get_state(pb->pwm, &state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		ret = pwm_backlight_brightness_default(&pdev->dev, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 						       state.period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 			dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 				"failed to setup default brightness table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		for (i = 0; i <= data->max_brightness; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			if (data->levels[i] > pb->scale)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 				pb->scale = data->levels[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 			pb->levels = data->levels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		props.scale = BACKLIGHT_SCALE_NON_LINEAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		 * That only happens for the non-DT case, where platform data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		 * sets the max_brightness value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 		pb->scale = data->max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	pwm_adjust_config(pb->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 				pb->scale));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	props.max_brightness = data->max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 				       &pwm_backlight_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	if (IS_ERR(bl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 		dev_err(&pdev->dev, "failed to register backlight\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 		ret = PTR_ERR(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		if (pb->legacy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 			pwm_free(pb->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if (data->dft_brightness > data->max_brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		dev_warn(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 			 "invalid default brightness level: %u, using %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 			 data->dft_brightness, data->max_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		data->dft_brightness = data->max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	bl->props.brightness = data->dft_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	bl->props.power = pwm_backlight_initial_power_state(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	platform_set_drvdata(pdev, bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	if (data->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		data->exit(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) static int pwm_backlight_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	struct backlight_device *bl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	struct pwm_bl_data *pb = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	backlight_device_unregister(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	pwm_backlight_power_off(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (pb->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		pb->exit(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	if (pb->legacy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		pwm_free(pb->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static void pwm_backlight_shutdown(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	struct backlight_device *bl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	struct pwm_bl_data *pb = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	pwm_backlight_power_off(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) static int pwm_backlight_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	struct backlight_device *bl = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	struct pwm_bl_data *pb = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (pb->notify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		pb->notify(pb->dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	pwm_backlight_power_off(pb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	if (pb->notify_after)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		pb->notify_after(pb->dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static int pwm_backlight_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	struct backlight_device *bl = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	backlight_update_status(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) static const struct dev_pm_ops pwm_backlight_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	.suspend = pwm_backlight_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	.resume = pwm_backlight_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	.poweroff = pwm_backlight_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	.restore = pwm_backlight_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static struct platform_driver pwm_backlight_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	.driver		= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		.name		= "pwm-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		.pm		= &pwm_backlight_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		.of_match_table	= of_match_ptr(pwm_backlight_of_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	.probe		= pwm_backlight_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	.remove		= pwm_backlight_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	.shutdown	= pwm_backlight_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) module_platform_driver(pwm_backlight_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) MODULE_DESCRIPTION("PWM based Backlight Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) MODULE_ALIAS("platform:pwm-backlight");