2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-04-14 08:35:08 +00:00
|
|
|
/*
|
2019-06-19 13:59:27 +00:00
|
|
|
* Simple PWM based backlight control, board code has to setup
|
2008-04-14 08:35:08 +00:00
|
|
|
* 1) pin configuration so PWM waveforms can output
|
2009-02-10 05:30:37 +00:00
|
|
|
* 2) platform_data being correctly configured
|
2008-04-14 08:35:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-03-28 17:03:25 +00:00
|
|
|
#include <linux/delay.h>
|
2014-02-27 05:53:34 +00:00
|
|
|
#include <linux/gpio/consumer.h>
|
2008-04-14 08:35:08 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/fb.h>
|
|
|
|
#include <linux/backlight.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/pwm.h>
|
|
|
|
#include <linux/pwm_backlight.h>
|
2013-08-30 10:38:34 +00:00
|
|
|
#include <linux/regulator/consumer.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2008-04-14 08:35:08 +00:00
|
|
|
|
|
|
|
struct pwm_bl_data {
|
|
|
|
struct pwm_device *pwm;
|
2009-11-10 17:20:40 +00:00
|
|
|
struct device *dev;
|
2010-11-11 22:05:28 +00:00
|
|
|
unsigned int lth_brightness;
|
2011-12-16 20:25:29 +00:00
|
|
|
unsigned int *levels;
|
2018-11-09 09:48:57 +00:00
|
|
|
bool enabled;
|
2013-08-30 10:38:34 +00:00
|
|
|
struct regulator *power_supply;
|
2014-02-27 05:53:34 +00:00
|
|
|
struct gpio_desc *enable_gpio;
|
2013-09-22 16:59:56 +00:00
|
|
|
unsigned int scale;
|
2014-10-11 13:46:26 +00:00
|
|
|
bool legacy;
|
2018-03-28 17:03:25 +00:00
|
|
|
unsigned int post_pwm_on_delay;
|
|
|
|
unsigned int pwm_off_delay;
|
2009-11-10 17:20:40 +00:00
|
|
|
int (*notify)(struct device *,
|
|
|
|
int brightness);
|
2011-08-25 22:59:17 +00:00
|
|
|
void (*notify_after)(struct device *,
|
|
|
|
int brightness);
|
2011-03-22 23:30:31 +00:00
|
|
|
int (*check_fb)(struct device *, struct fb_info *);
|
2011-12-16 20:25:29 +00:00
|
|
|
void (*exit)(struct device *);
|
2008-04-14 08:35:08 +00:00
|
|
|
};
|
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
static void pwm_backlight_power_on(struct pwm_bl_data *pb)
|
2013-10-07 09:32:02 +00:00
|
|
|
{
|
2018-08-14 16:50:59 +00:00
|
|
|
struct pwm_state state;
|
2013-10-22 07:37:05 +00:00
|
|
|
int err;
|
2013-10-07 09:32:02 +00:00
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
pwm_get_state(pb->pwm, &state);
|
2018-11-09 09:48:57 +00:00
|
|
|
if (pb->enabled)
|
2013-10-02 16:01:02 +00:00
|
|
|
return;
|
|
|
|
|
2013-08-30 10:38:34 +00:00
|
|
|
err = regulator_enable(pb->power_supply);
|
|
|
|
if (err < 0)
|
|
|
|
dev_err(pb->dev, "failed to enable power supply\n");
|
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
state.enabled = true;
|
|
|
|
pwm_apply_state(pb->pwm, &state);
|
2018-03-28 17:03:23 +00:00
|
|
|
|
2018-03-28 17:03:25 +00:00
|
|
|
if (pb->post_pwm_on_delay)
|
|
|
|
msleep(pb->post_pwm_on_delay);
|
|
|
|
|
2014-02-27 05:53:34 +00:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 08:18:12 +00:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 1);
|
2018-11-09 09:48:57 +00:00
|
|
|
|
|
|
|
pb->enabled = true;
|
2013-10-07 09:32:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pwm_backlight_power_off(struct pwm_bl_data *pb)
|
|
|
|
{
|
2018-08-14 16:50:59 +00:00
|
|
|
struct pwm_state state;
|
|
|
|
|
|
|
|
pwm_get_state(pb->pwm, &state);
|
2018-11-09 09:48:57 +00:00
|
|
|
if (!pb->enabled)
|
2013-10-02 16:01:02 +00:00
|
|
|
return;
|
|
|
|
|
2014-02-27 05:53:34 +00:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 08:18:12 +00:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 0);
|
2013-08-30 10:32:18 +00:00
|
|
|
|
2018-03-28 17:03:25 +00:00
|
|
|
if (pb->pwm_off_delay)
|
|
|
|
msleep(pb->pwm_off_delay);
|
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
state.enabled = false;
|
|
|
|
state.duty_cycle = 0;
|
|
|
|
pwm_apply_state(pb->pwm, &state);
|
2018-03-28 17:03:23 +00:00
|
|
|
|
2013-08-30 10:38:34 +00:00
|
|
|
regulator_disable(pb->power_supply);
|
2018-11-09 09:48:57 +00:00
|
|
|
pb->enabled = false;
|
2013-10-07 09:32:02 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 08:46:24 +00:00
|
|
|
static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
|
|
|
|
{
|
|
|
|
unsigned int lth = pb->lth_brightness;
|
2018-08-14 16:50:59 +00:00
|
|
|
struct pwm_state state;
|
2017-08-29 20:34:34 +00:00
|
|
|
u64 duty_cycle;
|
2013-10-18 08:46:24 +00:00
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
|
2013-10-18 08:46:24 +00:00
|
|
|
if (pb->levels)
|
|
|
|
duty_cycle = pb->levels[brightness];
|
|
|
|
else
|
|
|
|
duty_cycle = brightness;
|
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
duty_cycle *= state.period - lth;
|
2017-08-29 20:34:34 +00:00
|
|
|
do_div(duty_cycle, pb->scale);
|
|
|
|
|
|
|
|
return duty_cycle + lth;
|
2013-10-18 08:46:24 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
static int pwm_backlight_update_status(struct backlight_device *bl)
|
|
|
|
{
|
2013-02-22 00:43:46 +00:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2020-07-19 08:07:41 +00:00
|
|
|
int brightness = backlight_get_brightness(bl);
|
2018-08-14 16:50:59 +00:00
|
|
|
struct pwm_state state;
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2008-05-22 13:18:40 +00:00
|
|
|
if (pb->notify)
|
2009-11-10 17:20:40 +00:00
|
|
|
brightness = pb->notify(pb->dev, brightness);
|
2008-05-22 13:18:40 +00:00
|
|
|
|
2013-10-18 08:46:24 +00:00
|
|
|
if (brightness > 0) {
|
2018-08-14 16:50:59 +00:00
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
state.duty_cycle = compute_duty_cycle(pb, brightness);
|
|
|
|
pwm_apply_state(pb->pwm, &state);
|
|
|
|
pwm_backlight_power_on(pb);
|
2019-10-03 21:35:02 +00:00
|
|
|
} else {
|
2013-10-07 09:32:02 +00:00
|
|
|
pwm_backlight_power_off(pb);
|
2019-10-03 21:35:02 +00:00
|
|
|
}
|
2011-08-25 22:59:17 +00:00
|
|
|
|
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, brightness);
|
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-22 23:30:31 +00:00
|
|
|
static int pwm_backlight_check_fb(struct backlight_device *bl,
|
|
|
|
struct fb_info *info)
|
|
|
|
{
|
2013-02-22 00:43:46 +00:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2011-03-22 23:30:31 +00:00
|
|
|
|
|
|
|
return !pb->check_fb || pb->check_fb(pb->dev, info);
|
|
|
|
}
|
|
|
|
|
2009-12-13 23:58:57 +00:00
|
|
|
static const struct backlight_ops pwm_backlight_ops = {
|
2008-04-14 08:35:08 +00:00
|
|
|
.update_status = pwm_backlight_update_status,
|
2011-03-22 23:30:31 +00:00
|
|
|
.check_fb = pwm_backlight_check_fb,
|
2008-04-14 08:35:08 +00:00
|
|
|
};
|
|
|
|
|
2011-12-16 20:25:29 +00:00
|
|
|
#ifdef CONFIG_OF
|
2019-10-08 12:03:27 +00:00
|
|
|
#define PWM_LUMINANCE_SHIFT 16
|
|
|
|
#define PWM_LUMINANCE_SCALE (1 << PWM_LUMINANCE_SHIFT) /* luminance scale */
|
2018-04-09 08:33:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CIE lightness to PWM conversion.
|
|
|
|
*
|
|
|
|
* The CIE 1931 lightness formula is what actually describes how we perceive
|
|
|
|
* light:
|
2019-10-08 12:03:24 +00:00
|
|
|
* Y = (L* / 903.3) if L* ≤ 8
|
|
|
|
* Y = ((L* + 16) / 116)^3 if L* > 8
|
2018-04-09 08:33:32 +00:00
|
|
|
*
|
|
|
|
* Where Y is the luminance, the amount of light coming out of the screen, and
|
|
|
|
* is a number between 0.0 and 1.0; and L* is the lightness, how bright a human
|
|
|
|
* perceives the screen to be, and is a number between 0 and 100.
|
|
|
|
*
|
|
|
|
* The following function does the fixed point maths needed to implement the
|
|
|
|
* above formula.
|
|
|
|
*/
|
2019-10-08 12:03:27 +00:00
|
|
|
static u64 cie1931(unsigned int lightness)
|
2018-04-09 08:33:32 +00:00
|
|
|
{
|
|
|
|
u64 retval;
|
|
|
|
|
2019-10-08 12:03:24 +00:00
|
|
|
/*
|
|
|
|
* @lightness is given as a number between 0 and 1, expressed
|
2019-10-08 12:03:27 +00:00
|
|
|
* as a fixed-point number in scale
|
|
|
|
* PWM_LUMINANCE_SCALE. Convert to a percentage, still
|
|
|
|
* expressed as a fixed-point number, so the above formulas
|
|
|
|
* can be applied.
|
2019-10-08 12:03:24 +00:00
|
|
|
*/
|
2018-04-09 08:33:32 +00:00
|
|
|
lightness *= 100;
|
2019-10-08 12:03:27 +00:00
|
|
|
if (lightness <= (8 * PWM_LUMINANCE_SCALE)) {
|
2019-10-08 12:03:25 +00:00
|
|
|
retval = DIV_ROUND_CLOSEST(lightness * 10, 9033);
|
2018-04-09 08:33:32 +00:00
|
|
|
} else {
|
2019-10-08 12:03:27 +00:00
|
|
|
retval = (lightness + (16 * PWM_LUMINANCE_SCALE)) / 116;
|
2019-10-08 12:03:26 +00:00
|
|
|
retval *= retval * retval;
|
2019-10-08 12:03:27 +00:00
|
|
|
retval += 1ULL << (2*PWM_LUMINANCE_SHIFT - 1);
|
|
|
|
retval >>= 2*PWM_LUMINANCE_SHIFT;
|
2018-04-09 08:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a default correction table for PWM values to create linear brightness
|
|
|
|
* for LED based backlights using the CIE1931 algorithm.
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int pwm_backlight_brightness_default(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data,
|
|
|
|
unsigned int period)
|
|
|
|
{
|
2019-06-12 18:00:03 +00:00
|
|
|
unsigned int i;
|
2018-04-09 08:33:32 +00:00
|
|
|
u64 retval;
|
|
|
|
|
|
|
|
/*
|
2019-06-12 18:00:03 +00:00
|
|
|
* Once we have 4096 levels there's little point going much higher...
|
|
|
|
* neither interactive sliders nor animation benefits from having
|
|
|
|
* more values in the table.
|
2018-04-09 08:33:32 +00:00
|
|
|
*/
|
2019-06-12 18:00:03 +00:00
|
|
|
data->max_brightness =
|
|
|
|
min((int)DIV_ROUND_UP(period, fls(period)), 4096);
|
2018-04-09 08:33:32 +00:00
|
|
|
|
|
|
|
data->levels = devm_kcalloc(dev, data->max_brightness,
|
|
|
|
sizeof(*data->levels), GFP_KERNEL);
|
|
|
|
if (!data->levels)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Fill the table using the cie1931 algorithm */
|
|
|
|
for (i = 0; i < data->max_brightness; i++) {
|
|
|
|
retval = cie1931((i * PWM_LUMINANCE_SCALE) /
|
2019-10-08 12:03:27 +00:00
|
|
|
data->max_brightness) * period;
|
2018-04-09 08:33:32 +00:00
|
|
|
retval = DIV_ROUND_CLOSEST_ULL(retval, PWM_LUMINANCE_SCALE);
|
|
|
|
if (retval > UINT_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
data->levels[i] = (unsigned int)retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->dft_brightness = data->max_brightness / 2;
|
|
|
|
data->max_brightness--;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-16 20:25:29 +00:00
|
|
|
static int pwm_backlight_parse_dt(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data)
|
|
|
|
{
|
|
|
|
struct device_node *node = dev->of_node;
|
2020-10-22 05:04:45 +00:00
|
|
|
unsigned int num_levels;
|
2018-07-25 07:38:30 +00:00
|
|
|
unsigned int num_steps = 0;
|
2011-12-16 20:25:29 +00:00
|
|
|
struct property *prop;
|
2018-04-09 08:33:30 +00:00
|
|
|
unsigned int *table;
|
2011-12-16 20:25:29 +00:00
|
|
|
int length;
|
|
|
|
u32 value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
2018-11-12 09:02:57 +00:00
|
|
|
/*
|
|
|
|
* These values are optional and set as 0 by default, the out values
|
|
|
|
* are modified only if a valid u32 value can be decoded.
|
|
|
|
*/
|
|
|
|
of_property_read_u32(node, "post-pwm-on-delay-ms",
|
|
|
|
&data->post_pwm_on_delay);
|
|
|
|
of_property_read_u32(node, "pwm-off-delay-ms", &data->pwm_off_delay);
|
|
|
|
|
2018-04-09 08:33:32 +00:00
|
|
|
/*
|
|
|
|
* Determine the number of brightness levels, if this property is not
|
|
|
|
* set a default table of brightness levels will be used.
|
|
|
|
*/
|
2011-12-16 20:25:29 +00:00
|
|
|
prop = of_find_property(node, "brightness-levels", &length);
|
|
|
|
if (!prop)
|
2018-04-09 08:33:32 +00:00
|
|
|
return 0;
|
2011-12-16 20:25:29 +00:00
|
|
|
|
2020-10-22 05:04:45 +00:00
|
|
|
num_levels = length / sizeof(u32);
|
2011-12-16 20:25:29 +00:00
|
|
|
|
|
|
|
/* read brightness levels from DT property */
|
2020-10-22 05:04:45 +00:00
|
|
|
if (num_levels > 0) {
|
|
|
|
size_t size = sizeof(*data->levels) * num_levels;
|
2011-12-16 20:25:29 +00:00
|
|
|
|
|
|
|
data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
|
|
|
|
if (!data->levels)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = of_property_read_u32_array(node, "brightness-levels",
|
|
|
|
data->levels,
|
2020-10-22 05:04:45 +00:00
|
|
|
num_levels);
|
2011-12-16 20:25:29 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = of_property_read_u32(node, "default-brightness-level",
|
|
|
|
&value);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
data->dft_brightness = value;
|
2018-04-09 08:33:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This property is optional, if is set enables linear
|
|
|
|
* interpolation between each of the values of brightness levels
|
|
|
|
* and creates a new pre-computed table.
|
|
|
|
*/
|
|
|
|
of_property_read_u32(node, "num-interpolated-steps",
|
|
|
|
&num_steps);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that there is at least two entries in the
|
|
|
|
* brightness-levels table, otherwise we can't interpolate
|
|
|
|
* between two points.
|
|
|
|
*/
|
|
|
|
if (num_steps) {
|
2020-10-22 05:04:45 +00:00
|
|
|
unsigned int num_input_levels = num_levels;
|
|
|
|
unsigned int i;
|
|
|
|
u32 x1, x2, x, dx;
|
|
|
|
u32 y1, y2;
|
|
|
|
s64 dy;
|
|
|
|
|
|
|
|
if (num_input_levels < 2) {
|
2018-04-09 08:33:30 +00:00
|
|
|
dev_err(dev, "can't interpolate\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recalculate the number of brightness levels, now
|
|
|
|
* taking in consideration the number of interpolated
|
|
|
|
* steps between two levels.
|
|
|
|
*/
|
2020-10-22 05:04:45 +00:00
|
|
|
num_levels = (num_input_levels - 1) * num_steps + 1;
|
2018-04-09 08:33:30 +00:00
|
|
|
dev_dbg(dev, "new number of brightness levels: %d\n",
|
|
|
|
num_levels);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new table of brightness levels with all the
|
|
|
|
* interpolated steps.
|
|
|
|
*/
|
|
|
|
size = sizeof(*table) * num_levels;
|
|
|
|
table = devm_kzalloc(dev, size, GFP_KERNEL);
|
|
|
|
if (!table)
|
|
|
|
return -ENOMEM;
|
2020-10-22 05:04:45 +00:00
|
|
|
/*
|
|
|
|
* Fill the interpolated table[x] = y
|
|
|
|
* by draw lines between each (x1, y1) to (x2, y2).
|
|
|
|
*/
|
|
|
|
dx = num_steps;
|
|
|
|
for (i = 0; i < num_input_levels - 1; i++) {
|
|
|
|
x1 = i * dx;
|
|
|
|
x2 = x1 + dx;
|
|
|
|
y1 = data->levels[i];
|
|
|
|
y2 = data->levels[i + 1];
|
|
|
|
dy = (s64)y2 - y1;
|
|
|
|
|
|
|
|
for (x = x1; x < x2; x++) {
|
|
|
|
table[x] = y1 +
|
|
|
|
div_s64(dy * (x - x1), dx);
|
2018-04-09 08:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-22 05:04:45 +00:00
|
|
|
/* Fill in the last point, since no line starts here. */
|
|
|
|
table[x2] = y2;
|
2018-04-09 08:33:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* As we use interpolation lets remove current
|
|
|
|
* brightness levels table and replace for the
|
|
|
|
* new interpolated table.
|
|
|
|
*/
|
|
|
|
devm_kfree(dev, data->levels);
|
|
|
|
data->levels = table;
|
|
|
|
}
|
|
|
|
|
2020-10-22 05:04:45 +00:00
|
|
|
data->max_brightness = num_levels - 1;
|
2011-12-16 20:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:52:15 +00:00
|
|
|
static const struct of_device_id pwm_backlight_of_match[] = {
|
2011-12-16 20:25:29 +00:00
|
|
|
{ .compatible = "pwm-backlight" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
|
|
|
|
#else
|
|
|
|
static int pwm_backlight_parse_dt(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2018-04-09 08:33:32 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
int pwm_backlight_brightness_default(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data,
|
|
|
|
unsigned int period)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2011-12-16 20:25:29 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-09 19:00:07 +00:00
|
|
|
static bool pwm_backlight_is_linear(struct platform_pwm_backlight_data *data)
|
|
|
|
{
|
|
|
|
unsigned int nlevels = data->max_brightness + 1;
|
|
|
|
unsigned int min_val = data->levels[0];
|
|
|
|
unsigned int max_val = data->levels[nlevels - 1];
|
|
|
|
/*
|
|
|
|
* Multiplying by 128 means that even in pathological cases such
|
|
|
|
* as (max_val - min_val) == nlevels the error at max_val is less
|
|
|
|
* than 1%.
|
|
|
|
*/
|
|
|
|
unsigned int slope = (128 * (max_val - min_val)) / nlevels;
|
|
|
|
unsigned int margin = (max_val - min_val) / 20; /* 5% */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 1; i < nlevels; i++) {
|
|
|
|
unsigned int linear_value = min_val + ((i * slope) / 128);
|
|
|
|
unsigned int delta = abs(linear_value - data->levels[i]);
|
|
|
|
|
|
|
|
if (delta > margin)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:41:22 +00:00
|
|
|
static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
|
|
|
|
{
|
|
|
|
struct device_node *node = pb->dev->of_node;
|
2021-07-22 14:46:23 +00:00
|
|
|
bool active = true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the enable GPIO is present, observable (either as input
|
|
|
|
* or output) and off then the backlight is not currently active.
|
|
|
|
* */
|
|
|
|
if (pb->enable_gpio && gpiod_get_value_cansleep(pb->enable_gpio) == 0)
|
|
|
|
active = false;
|
|
|
|
|
|
|
|
if (!regulator_is_enabled(pb->power_supply))
|
|
|
|
active = false;
|
|
|
|
|
|
|
|
if (!pwm_is_enabled(pb->pwm))
|
|
|
|
active = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synchronize the enable_gpio with the observed state of the
|
|
|
|
* hardware.
|
|
|
|
*/
|
|
|
|
if (pb->enable_gpio)
|
|
|
|
gpiod_direction_output(pb->enable_gpio, active);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not change pb->enabled here! pb->enabled essentially
|
|
|
|
* tells us if we own one of the regulator's use counts and
|
|
|
|
* right now we do not.
|
|
|
|
*/
|
2016-11-22 13:41:22 +00:00
|
|
|
|
|
|
|
/* Not booted with device tree or no phandle link to the node */
|
|
|
|
if (!node || !node->phandle)
|
|
|
|
return FB_BLANK_UNBLANK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the driver is probed from the device tree and there is a
|
|
|
|
* phandle link pointing to the backlight node, it is safe to
|
|
|
|
* assume that another driver will enable the backlight at the
|
|
|
|
* appropriate time. Therefore, if it is disabled, keep it so.
|
|
|
|
*/
|
2021-07-22 14:46:23 +00:00
|
|
|
return active ? FB_BLANK_UNBLANK: FB_BLANK_POWERDOWN;
|
2016-11-22 13:41:22 +00:00
|
|
|
}
|
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
static int pwm_backlight_probe(struct platform_device *pdev)
|
|
|
|
{
|
2013-11-12 23:09:04 +00:00
|
|
|
struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
|
2011-12-16 20:25:29 +00:00
|
|
|
struct platform_pwm_backlight_data defdata;
|
|
|
|
struct backlight_properties props;
|
2008-04-14 08:35:08 +00:00
|
|
|
struct backlight_device *bl;
|
2015-12-10 09:09:06 +00:00
|
|
|
struct device_node *node = pdev->dev.of_node;
|
2008-04-14 08:35:08 +00:00
|
|
|
struct pwm_bl_data *pb;
|
2018-04-09 08:33:32 +00:00
|
|
|
struct pwm_state state;
|
|
|
|
unsigned int i;
|
2008-05-22 13:18:40 +00:00
|
|
|
int ret;
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2008-08-05 20:01:22 +00:00
|
|
|
if (!data) {
|
2011-12-16 20:25:29 +00:00
|
|
|
ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to find platform data\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = &defdata;
|
2008-08-05 20:01:22 +00:00
|
|
|
}
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2008-05-22 13:18:40 +00:00
|
|
|
if (data->init) {
|
|
|
|
ret = data->init(&pdev->dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-23 22:02:00 +00:00
|
|
|
pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
|
2008-05-22 13:18:40 +00:00
|
|
|
if (!pb) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2008-05-22 13:18:40 +00:00
|
|
|
pb->notify = data->notify;
|
2011-08-25 22:59:17 +00:00
|
|
|
pb->notify_after = data->notify_after;
|
2011-03-22 23:30:31 +00:00
|
|
|
pb->check_fb = data->check_fb;
|
2011-12-16 20:25:29 +00:00
|
|
|
pb->exit = data->exit;
|
2009-11-10 17:20:40 +00:00
|
|
|
pb->dev = &pdev->dev;
|
2018-11-09 09:48:57 +00:00
|
|
|
pb->enabled = false;
|
2018-03-28 17:03:25 +00:00
|
|
|
pb->post_pwm_on_delay = data->post_pwm_on_delay;
|
|
|
|
pb->pwm_off_delay = data->pwm_off_delay;
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2015-05-16 14:08:10 +00:00
|
|
|
pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
|
2015-11-18 17:12:25 +00:00
|
|
|
GPIOD_ASIS);
|
2014-02-27 05:53:34 +00:00
|
|
|
if (IS_ERR(pb->enable_gpio)) {
|
|
|
|
ret = PTR_ERR(pb->enable_gpio);
|
2014-06-25 09:18:18 +00:00
|
|
|
goto err_alloc;
|
2014-02-27 05:53:34 +00:00
|
|
|
}
|
2013-08-30 10:32:18 +00:00
|
|
|
|
2013-08-30 10:38:34 +00:00
|
|
|
pb->power_supply = devm_regulator_get(&pdev->dev, "power");
|
|
|
|
if (IS_ERR(pb->power_supply)) {
|
|
|
|
ret = PTR_ERR(pb->power_supply);
|
2014-02-27 05:53:34 +00:00
|
|
|
goto err_alloc;
|
2013-08-30 10:38:34 +00:00
|
|
|
}
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2012-09-17 06:20:47 +00:00
|
|
|
pb->pwm = devm_pwm_get(&pdev->dev, NULL);
|
2015-12-10 09:09:06 +00:00
|
|
|
if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
|
2011-12-16 20:25:29 +00:00
|
|
|
dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
|
2014-10-11 13:46:26 +00:00
|
|
|
pb->legacy = true;
|
2011-12-16 20:25:29 +00:00
|
|
|
pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
|
2015-10-12 12:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ERR(pb->pwm)) {
|
|
|
|
ret = PTR_ERR(pb->pwm);
|
|
|
|
if (ret != -EPROBE_DEFER)
|
|
|
|
dev_err(&pdev->dev, "unable to request PWM\n");
|
|
|
|
goto err_alloc;
|
2011-12-16 20:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "got pwm for backlight\n");
|
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
/* Sync up PWM state. */
|
|
|
|
pwm_init_state(pb->pwm, &state);
|
2018-04-09 08:33:32 +00:00
|
|
|
|
2018-08-14 16:50:59 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2019-07-09 19:00:06 +00:00
|
|
|
memset(&props, 0, sizeof(struct backlight_properties));
|
|
|
|
|
2018-12-10 12:14:37 +00:00
|
|
|
if (data->levels) {
|
2019-10-02 16:56:01 +00:00
|
|
|
pb->levels = data->levels;
|
|
|
|
|
2018-12-10 12:14:37 +00:00
|
|
|
/*
|
|
|
|
* For the DT case, only when brightness levels is defined
|
|
|
|
* data->levels is filled. For the non-DT case, data->levels
|
|
|
|
* can come from platform data, however is not usual.
|
|
|
|
*/
|
2019-10-02 16:56:01 +00:00
|
|
|
for (i = 0; i <= data->max_brightness; i++)
|
2018-12-10 12:14:37 +00:00
|
|
|
if (data->levels[i] > pb->scale)
|
|
|
|
pb->scale = data->levels[i];
|
|
|
|
|
2019-07-09 19:00:07 +00:00
|
|
|
if (pwm_backlight_is_linear(data))
|
|
|
|
props.scale = BACKLIGHT_SCALE_LINEAR;
|
|
|
|
else
|
|
|
|
props.scale = BACKLIGHT_SCALE_NON_LINEAR;
|
2018-12-10 12:14:37 +00:00
|
|
|
} else if (!data->max_brightness) {
|
|
|
|
/*
|
|
|
|
* If no brightness levels are provided and max_brightness is
|
|
|
|
* not set, use the default brightness table. For the DT case,
|
|
|
|
* max_brightness is set to 0 when brightness levels is not
|
|
|
|
* specified. For the non-DT case, max_brightness is usually
|
|
|
|
* set to some value.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get the PWM period (in nanoseconds) */
|
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
|
2018-04-09 08:33:32 +00:00
|
|
|
ret = pwm_backlight_brightness_default(&pdev->dev, data,
|
|
|
|
state.period);
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev,
|
|
|
|
"failed to setup default brightness table\n");
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:14:37 +00:00
|
|
|
for (i = 0; i <= data->max_brightness; i++) {
|
|
|
|
if (data->levels[i] > pb->scale)
|
|
|
|
pb->scale = data->levels[i];
|
2018-04-09 08:33:32 +00:00
|
|
|
|
2018-12-10 12:14:37 +00:00
|
|
|
pb->levels = data->levels;
|
|
|
|
}
|
2019-07-09 19:00:06 +00:00
|
|
|
|
|
|
|
props.scale = BACKLIGHT_SCALE_NON_LINEAR;
|
2018-12-10 12:14:37 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* That only happens for the non-DT case, where platform data
|
|
|
|
* sets the max_brightness value.
|
|
|
|
*/
|
|
|
|
pb->scale = data->max_brightness;
|
2018-04-09 08:33:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 22:31:14 +00:00
|
|
|
pb->lth_brightness = data->lth_brightness * (div_u64(state.period,
|
|
|
|
pb->scale));
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2011-03-22 23:30:21 +00:00
|
|
|
props.type = BACKLIGHT_RAW;
|
2010-02-17 21:39:44 +00:00
|
|
|
props.max_brightness = data->max_brightness;
|
|
|
|
bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
|
|
|
|
&pwm_backlight_ops, &props);
|
2008-04-14 08:35:08 +00:00
|
|
|
if (IS_ERR(bl)) {
|
|
|
|
dev_err(&pdev->dev, "failed to register backlight\n");
|
2008-05-22 13:18:40 +00:00
|
|
|
ret = PTR_ERR(bl);
|
2015-06-14 14:32:14 +00:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2014-02-27 05:53:34 +00:00
|
|
|
goto err_alloc;
|
2008-04-14 08:35:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-22 13:39:54 +00:00
|
|
|
if (data->dft_brightness > data->max_brightness) {
|
|
|
|
dev_warn(&pdev->dev,
|
|
|
|
"invalid default brightness level: %u, using %u\n",
|
|
|
|
data->dft_brightness, data->max_brightness);
|
|
|
|
data->dft_brightness = data->max_brightness;
|
|
|
|
}
|
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
bl->props.brightness = data->dft_brightness;
|
2016-11-22 13:41:22 +00:00
|
|
|
bl->props.power = pwm_backlight_initial_power_state(pb);
|
2008-04-14 08:35:08 +00:00
|
|
|
backlight_update_status(bl);
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, bl);
|
|
|
|
return 0;
|
2008-05-22 13:18:40 +00:00
|
|
|
|
|
|
|
err_alloc:
|
|
|
|
if (data->exit)
|
|
|
|
data->exit(&pdev->dev);
|
|
|
|
return ret;
|
2008-04-14 08:35:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pwm_backlight_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct backlight_device *bl = platform_get_drvdata(pdev);
|
2013-02-22 00:43:46 +00:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 08:35:08 +00:00
|
|
|
|
|
|
|
backlight_device_unregister(bl);
|
2013-10-07 09:32:02 +00:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2011-12-16 20:25:29 +00:00
|
|
|
if (pb->exit)
|
|
|
|
pb->exit(&pdev->dev);
|
2014-10-11 13:46:26 +00:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 15:28:59 +00:00
|
|
|
static void pwm_backlight_shutdown(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct backlight_device *bl = platform_get_drvdata(pdev);
|
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
|
|
|
|
|
|
|
pwm_backlight_power_off(pb);
|
|
|
|
}
|
|
|
|
|
2013-02-25 08:15:47 +00:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2012-01-10 23:09:22 +00:00
|
|
|
static int pwm_backlight_suspend(struct device *dev)
|
2008-04-14 08:35:08 +00:00
|
|
|
{
|
2012-01-10 23:09:22 +00:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2013-02-22 00:43:46 +00:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 08:35:08 +00:00
|
|
|
|
2009-06-19 08:50:54 +00:00
|
|
|
if (pb->notify)
|
2009-11-10 17:20:40 +00:00
|
|
|
pb->notify(pb->dev, 0);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2013-10-07 09:32:02 +00:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2011-08-25 22:59:17 +00:00
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, 0);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-10 23:09:22 +00:00
|
|
|
static int pwm_backlight_resume(struct device *dev)
|
2008-04-14 08:35:08 +00:00
|
|
|
{
|
2012-01-10 23:09:22 +00:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2008-04-14 08:35:08 +00:00
|
|
|
|
|
|
|
backlight_update_status(bl);
|
2013-10-07 09:30:50 +00:00
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-25 08:15:47 +00:00
|
|
|
#endif
|
2012-01-10 23:09:22 +00:00
|
|
|
|
2013-10-09 02:33:02 +00:00
|
|
|
static const struct dev_pm_ops pwm_backlight_pm_ops = {
|
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
.suspend = pwm_backlight_suspend,
|
|
|
|
.resume = pwm_backlight_resume,
|
|
|
|
.poweroff = pwm_backlight_suspend,
|
|
|
|
.restore = pwm_backlight_resume,
|
|
|
|
#endif
|
|
|
|
};
|
2012-01-10 23:09:22 +00:00
|
|
|
|
2008-04-14 08:35:08 +00:00
|
|
|
static struct platform_driver pwm_backlight_driver = {
|
|
|
|
.driver = {
|
2011-12-16 20:25:29 +00:00
|
|
|
.name = "pwm-backlight",
|
|
|
|
.pm = &pwm_backlight_pm_ops,
|
|
|
|
.of_match_table = of_match_ptr(pwm_backlight_of_match),
|
2008-04-14 08:35:08 +00:00
|
|
|
},
|
|
|
|
.probe = pwm_backlight_probe,
|
|
|
|
.remove = pwm_backlight_remove,
|
2014-04-29 15:28:59 +00:00
|
|
|
.shutdown = pwm_backlight_shutdown,
|
2008-04-14 08:35:08 +00:00
|
|
|
};
|
|
|
|
|
2012-01-10 23:09:11 +00:00
|
|
|
module_platform_driver(pwm_backlight_driver);
|
2008-04-14 08:35:08 +00:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("PWM based Backlight Driver");
|
2019-06-19 13:59:27 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|
2008-08-05 20:01:24 +00:00
|
|
|
MODULE_ALIAS("platform:pwm-backlight");
|