2008-04-14 12:35:08 +04:00
|
|
|
/*
|
|
|
|
* linux/drivers/video/backlight/pwm_bl.c
|
|
|
|
*
|
|
|
|
* simple PWM based backlight control, board code has to setup
|
|
|
|
* 1) pin configuration so PWM waveforms can output
|
2009-02-10 08:30:37 +03:00
|
|
|
* 2) platform_data being correctly configured
|
2008-04-14 12:35:08 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2018-03-28 20:03:25 +03:00
|
|
|
#include <linux/delay.h>
|
2014-02-27 09:53:34 +04:00
|
|
|
#include <linux/gpio/consumer.h>
|
2013-08-30 14:32:18 +04:00
|
|
|
#include <linux/gpio.h>
|
2008-04-14 12:35:08 +04: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 14:38:34 +04: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 11:04:11 +03:00
|
|
|
#include <linux/slab.h>
|
2008-04-14 12:35:08 +04:00
|
|
|
|
|
|
|
struct pwm_bl_data {
|
|
|
|
struct pwm_device *pwm;
|
2009-11-10 20:20:40 +03:00
|
|
|
struct device *dev;
|
2010-11-12 01:05:28 +03:00
|
|
|
unsigned int lth_brightness;
|
2011-12-17 00:25:29 +04:00
|
|
|
unsigned int *levels;
|
2013-08-30 14:38:34 +04:00
|
|
|
struct regulator *power_supply;
|
2014-02-27 09:53:34 +04:00
|
|
|
struct gpio_desc *enable_gpio;
|
2013-09-22 20:59:56 +04:00
|
|
|
unsigned int scale;
|
2014-10-11 17:46:26 +04:00
|
|
|
bool legacy;
|
2018-03-28 20:03:25 +03:00
|
|
|
unsigned int post_pwm_on_delay;
|
|
|
|
unsigned int pwm_off_delay;
|
2009-11-10 20:20:40 +03:00
|
|
|
int (*notify)(struct device *,
|
|
|
|
int brightness);
|
2011-08-26 02:59:17 +04:00
|
|
|
void (*notify_after)(struct device *,
|
|
|
|
int brightness);
|
2011-03-23 02:30:31 +03:00
|
|
|
int (*check_fb)(struct device *, struct fb_info *);
|
2011-12-17 00:25:29 +04:00
|
|
|
void (*exit)(struct device *);
|
2008-04-14 12:35:08 +04:00
|
|
|
};
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
static void pwm_backlight_power_on(struct pwm_bl_data *pb)
|
2013-10-07 13:32:02 +04:00
|
|
|
{
|
2018-08-14 19:50:59 +03:00
|
|
|
struct pwm_state state;
|
2013-10-22 11:37:05 +04:00
|
|
|
int err;
|
2013-10-07 13:32:02 +04:00
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
if (state.enabled)
|
2013-10-02 20:01:02 +04:00
|
|
|
return;
|
|
|
|
|
2013-08-30 14:38:34 +04:00
|
|
|
err = regulator_enable(pb->power_supply);
|
|
|
|
if (err < 0)
|
|
|
|
dev_err(pb->dev, "failed to enable power supply\n");
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
state.enabled = true;
|
|
|
|
pwm_apply_state(pb->pwm, &state);
|
2018-03-28 20:03:23 +03:00
|
|
|
|
2018-03-28 20:03:25 +03:00
|
|
|
if (pb->post_pwm_on_delay)
|
|
|
|
msleep(pb->post_pwm_on_delay);
|
|
|
|
|
2014-02-27 09:53:34 +04:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 11:18:12 +03:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 1);
|
2013-10-07 13:32:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pwm_backlight_power_off(struct pwm_bl_data *pb)
|
|
|
|
{
|
2018-08-14 19:50:59 +03:00
|
|
|
struct pwm_state state;
|
|
|
|
|
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
if (!state.enabled)
|
2013-10-02 20:01:02 +04:00
|
|
|
return;
|
|
|
|
|
2014-02-27 09:53:34 +04:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 11:18:12 +03:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 0);
|
2013-08-30 14:32:18 +04:00
|
|
|
|
2018-03-28 20:03:25 +03:00
|
|
|
if (pb->pwm_off_delay)
|
|
|
|
msleep(pb->pwm_off_delay);
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
state.enabled = false;
|
|
|
|
state.duty_cycle = 0;
|
|
|
|
pwm_apply_state(pb->pwm, &state);
|
2018-03-28 20:03:23 +03:00
|
|
|
|
2013-08-30 14:38:34 +04:00
|
|
|
regulator_disable(pb->power_supply);
|
2013-10-07 13:32:02 +04:00
|
|
|
}
|
|
|
|
|
2013-10-18 12:46:24 +04:00
|
|
|
static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
|
|
|
|
{
|
|
|
|
unsigned int lth = pb->lth_brightness;
|
2018-08-14 19:50:59 +03:00
|
|
|
struct pwm_state state;
|
2017-08-29 23:34:34 +03:00
|
|
|
u64 duty_cycle;
|
2013-10-18 12:46:24 +04:00
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
pwm_get_state(pb->pwm, &state);
|
|
|
|
|
2013-10-18 12:46:24 +04:00
|
|
|
if (pb->levels)
|
|
|
|
duty_cycle = pb->levels[brightness];
|
|
|
|
else
|
|
|
|
duty_cycle = brightness;
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
duty_cycle *= state.period - lth;
|
2017-08-29 23:34:34 +03:00
|
|
|
do_div(duty_cycle, pb->scale);
|
|
|
|
|
|
|
|
return duty_cycle + lth;
|
2013-10-18 12:46:24 +04:00
|
|
|
}
|
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
static int pwm_backlight_update_status(struct backlight_device *bl)
|
|
|
|
{
|
2013-02-22 04:43:46 +04:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 12:35:08 +04:00
|
|
|
int brightness = bl->props.brightness;
|
2018-08-14 19:50:59 +03:00
|
|
|
struct pwm_state state;
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2013-01-30 10:19:15 +04:00
|
|
|
if (bl->props.power != FB_BLANK_UNBLANK ||
|
|
|
|
bl->props.fb_blank != FB_BLANK_UNBLANK ||
|
|
|
|
bl->props.state & BL_CORE_FBBLANK)
|
2008-04-14 12:35:08 +04:00
|
|
|
brightness = 0;
|
|
|
|
|
2008-05-22 17:18:40 +04:00
|
|
|
if (pb->notify)
|
2009-11-10 20:20:40 +03:00
|
|
|
brightness = pb->notify(pb->dev, brightness);
|
2008-05-22 17:18:40 +04:00
|
|
|
|
2013-10-18 12:46:24 +04:00
|
|
|
if (brightness > 0) {
|
2018-08-14 19:50:59 +03: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);
|
2013-10-18 12:46:24 +04:00
|
|
|
} else
|
2013-10-07 13:32:02 +04:00
|
|
|
pwm_backlight_power_off(pb);
|
2011-08-26 02:59:17 +04:00
|
|
|
|
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, brightness);
|
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-23 02:30:31 +03:00
|
|
|
static int pwm_backlight_check_fb(struct backlight_device *bl,
|
|
|
|
struct fb_info *info)
|
|
|
|
{
|
2013-02-22 04:43:46 +04:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2011-03-23 02:30:31 +03:00
|
|
|
|
|
|
|
return !pb->check_fb || pb->check_fb(pb->dev, info);
|
|
|
|
}
|
|
|
|
|
2009-12-14 02:58:57 +03:00
|
|
|
static const struct backlight_ops pwm_backlight_ops = {
|
2008-04-14 12:35:08 +04:00
|
|
|
.update_status = pwm_backlight_update_status,
|
2011-03-23 02:30:31 +03:00
|
|
|
.check_fb = pwm_backlight_check_fb,
|
2008-04-14 12:35:08 +04:00
|
|
|
};
|
|
|
|
|
2011-12-17 00:25:29 +04:00
|
|
|
#ifdef CONFIG_OF
|
2018-04-09 11:33:32 +03:00
|
|
|
#define PWM_LUMINANCE_SCALE 10000 /* luminance scale */
|
|
|
|
|
|
|
|
/* An integer based power function */
|
|
|
|
static u64 int_pow(u64 base, int exp)
|
|
|
|
{
|
|
|
|
u64 result = 1;
|
|
|
|
|
|
|
|
while (exp) {
|
|
|
|
if (exp & 1)
|
|
|
|
result *= base;
|
|
|
|
exp >>= 1;
|
|
|
|
base *= base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CIE lightness to PWM conversion.
|
|
|
|
*
|
|
|
|
* The CIE 1931 lightness formula is what actually describes how we perceive
|
|
|
|
* light:
|
|
|
|
* Y = (L* / 902.3) if L* ≤ 0.08856
|
|
|
|
* Y = ((L* + 16) / 116)^3 if L* > 0.08856
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
static u64 cie1931(unsigned int lightness, unsigned int scale)
|
|
|
|
{
|
|
|
|
u64 retval;
|
|
|
|
|
|
|
|
lightness *= 100;
|
|
|
|
if (lightness <= (8 * scale)) {
|
|
|
|
retval = DIV_ROUND_CLOSEST_ULL(lightness * 10, 9023);
|
|
|
|
} else {
|
|
|
|
retval = int_pow((lightness + (16 * scale)) / 116, 3);
|
|
|
|
retval = DIV_ROUND_CLOSEST_ULL(retval, (scale * scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
unsigned int counter = 0;
|
|
|
|
unsigned int i, n;
|
|
|
|
u64 retval;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the number of bits needed to represent the period number. The
|
|
|
|
* number of bits is used to calculate the number of levels used for the
|
|
|
|
* brightness-levels table, the purpose of this calculation is have a
|
|
|
|
* pre-computed table with enough levels to get linear brightness
|
|
|
|
* perception. The period is divided by the number of bits so for a
|
|
|
|
* 8-bit PWM we have 255 / 8 = 32 brightness levels or for a 16-bit PWM
|
|
|
|
* we have 65535 / 16 = 4096 brightness levels.
|
|
|
|
*
|
|
|
|
* Note that this method is based on empirical testing on different
|
|
|
|
* devices with PWM of 8 and 16 bits of resolution.
|
|
|
|
*/
|
|
|
|
n = period;
|
|
|
|
while (n) {
|
|
|
|
counter += n % 2;
|
|
|
|
n >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->max_brightness = DIV_ROUND_UP(period, counter);
|
|
|
|
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) /
|
|
|
|
data->max_brightness, PWM_LUMINANCE_SCALE) *
|
|
|
|
period;
|
|
|
|
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-17 00:25:29 +04:00
|
|
|
static int pwm_backlight_parse_dt(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data)
|
|
|
|
{
|
|
|
|
struct device_node *node = dev->of_node;
|
2018-04-09 11:33:30 +03:00
|
|
|
unsigned int num_levels = 0;
|
|
|
|
unsigned int levels_count;
|
2018-07-25 10:38:30 +03:00
|
|
|
unsigned int num_steps = 0;
|
2011-12-17 00:25:29 +04:00
|
|
|
struct property *prop;
|
2018-04-09 11:33:30 +03:00
|
|
|
unsigned int *table;
|
2011-12-17 00:25:29 +04:00
|
|
|
int length;
|
|
|
|
u32 value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
2018-04-09 11:33:32 +03:00
|
|
|
/*
|
|
|
|
* Determine the number of brightness levels, if this property is not
|
|
|
|
* set a default table of brightness levels will be used.
|
|
|
|
*/
|
2011-12-17 00:25:29 +04:00
|
|
|
prop = of_find_property(node, "brightness-levels", &length);
|
|
|
|
if (!prop)
|
2018-04-09 11:33:32 +03:00
|
|
|
return 0;
|
2011-12-17 00:25:29 +04:00
|
|
|
|
|
|
|
data->max_brightness = length / sizeof(u32);
|
|
|
|
|
|
|
|
/* read brightness levels from DT property */
|
|
|
|
if (data->max_brightness > 0) {
|
|
|
|
size_t size = sizeof(*data->levels) * data->max_brightness;
|
2018-04-09 11:33:30 +03:00
|
|
|
unsigned int i, j, n = 0;
|
2011-12-17 00:25:29 +04: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,
|
|
|
|
data->max_brightness);
|
|
|
|
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 11:33:30 +03: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) {
|
|
|
|
if (data->max_brightness < 2) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < data->max_brightness - 1; i++) {
|
|
|
|
if ((data->levels[i + 1] - data->levels[i]) /
|
|
|
|
num_steps)
|
|
|
|
num_levels += num_steps;
|
|
|
|
else
|
|
|
|
num_levels++;
|
|
|
|
}
|
|
|
|
num_levels++;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Fill the interpolated table. */
|
|
|
|
levels_count = 0;
|
|
|
|
for (i = 0; i < data->max_brightness - 1; i++) {
|
|
|
|
value = data->levels[i];
|
|
|
|
n = (data->levels[i + 1] - value) / num_steps;
|
|
|
|
if (n > 0) {
|
|
|
|
for (j = 0; j < num_steps; j++) {
|
|
|
|
table[levels_count] = value;
|
|
|
|
value += n;
|
|
|
|
levels_count++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
table[levels_count] = data->levels[i];
|
|
|
|
levels_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
table[levels_count] = data->levels[i];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reassign max_brightness value to the new total number
|
|
|
|
* of brightness levels.
|
|
|
|
*/
|
|
|
|
data->max_brightness = num_levels;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:25:29 +04:00
|
|
|
data->max_brightness--;
|
|
|
|
}
|
|
|
|
|
2018-03-28 20:03:25 +03: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);
|
|
|
|
|
2014-08-20 10:38:36 +04:00
|
|
|
data->enable_gpio = -EINVAL;
|
2011-12-17 00:25:29 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:52:15 +03:00
|
|
|
static const struct of_device_id pwm_backlight_of_match[] = {
|
2011-12-17 00:25:29 +04: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 11:33:32 +03:00
|
|
|
|
|
|
|
static
|
|
|
|
int pwm_backlight_brightness_default(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data,
|
|
|
|
unsigned int period)
|
|
|
|
{
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
2011-12-17 00:25:29 +04:00
|
|
|
#endif
|
|
|
|
|
2016-11-22 16:41:22 +03:00
|
|
|
static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
|
|
|
|
{
|
|
|
|
struct device_node *node = pb->dev->of_node;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* if the enable GPIO is disabled, do not enable the backlight */
|
|
|
|
if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
|
|
|
|
return FB_BLANK_POWERDOWN;
|
|
|
|
|
|
|
|
/* The regulator is disabled, do not enable the backlight */
|
|
|
|
if (!regulator_is_enabled(pb->power_supply))
|
|
|
|
return FB_BLANK_POWERDOWN;
|
|
|
|
|
2016-11-22 16:41:23 +03:00
|
|
|
/* The PWM is disabled, keep it like this */
|
|
|
|
if (!pwm_is_enabled(pb->pwm))
|
|
|
|
return FB_BLANK_POWERDOWN;
|
|
|
|
|
2016-11-22 16:41:22 +03:00
|
|
|
return FB_BLANK_UNBLANK;
|
|
|
|
}
|
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
static int pwm_backlight_probe(struct platform_device *pdev)
|
|
|
|
{
|
2013-11-13 03:09:04 +04:00
|
|
|
struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
|
2011-12-17 00:25:29 +04:00
|
|
|
struct platform_pwm_backlight_data defdata;
|
|
|
|
struct backlight_properties props;
|
2008-04-14 12:35:08 +04:00
|
|
|
struct backlight_device *bl;
|
2015-12-10 12:09:06 +03:00
|
|
|
struct device_node *node = pdev->dev.of_node;
|
2008-04-14 12:35:08 +04:00
|
|
|
struct pwm_bl_data *pb;
|
2018-04-09 11:33:32 +03:00
|
|
|
struct pwm_state state;
|
|
|
|
unsigned int i;
|
2008-05-22 17:18:40 +04:00
|
|
|
int ret;
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2008-08-06 00:01:22 +04:00
|
|
|
if (!data) {
|
2011-12-17 00:25:29 +04: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-06 00:01:22 +04:00
|
|
|
}
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2008-05-22 17:18:40 +04:00
|
|
|
if (data->init) {
|
|
|
|
ret = data->init(&pdev->dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-24 02:02:00 +04:00
|
|
|
pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
|
2008-05-22 17:18:40 +04:00
|
|
|
if (!pb) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2008-05-22 17:18:40 +04:00
|
|
|
pb->notify = data->notify;
|
2011-08-26 02:59:17 +04:00
|
|
|
pb->notify_after = data->notify_after;
|
2011-03-23 02:30:31 +03:00
|
|
|
pb->check_fb = data->check_fb;
|
2011-12-17 00:25:29 +04:00
|
|
|
pb->exit = data->exit;
|
2009-11-10 20:20:40 +03:00
|
|
|
pb->dev = &pdev->dev;
|
2018-03-28 20:03:25 +03:00
|
|
|
pb->post_pwm_on_delay = data->post_pwm_on_delay;
|
|
|
|
pb->pwm_off_delay = data->pwm_off_delay;
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2015-05-16 17:08:10 +03:00
|
|
|
pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
|
2015-11-18 20:12:25 +03:00
|
|
|
GPIOD_ASIS);
|
2014-02-27 09:53:34 +04:00
|
|
|
if (IS_ERR(pb->enable_gpio)) {
|
|
|
|
ret = PTR_ERR(pb->enable_gpio);
|
2014-06-25 13:18:18 +04:00
|
|
|
goto err_alloc;
|
2014-02-27 09:53:34 +04:00
|
|
|
}
|
2013-08-30 14:32:18 +04:00
|
|
|
|
2014-02-27 09:53:34 +04:00
|
|
|
/*
|
|
|
|
* Compatibility fallback for drivers still using the integer GPIO
|
|
|
|
* platform data. Must go away soon.
|
|
|
|
*/
|
|
|
|
if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
|
|
|
|
ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
|
|
|
|
GPIOF_OUT_INIT_HIGH, "enable");
|
2013-08-30 14:32:18 +04:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
|
2014-02-27 09:53:34 +04:00
|
|
|
data->enable_gpio, ret);
|
2013-08-30 14:32:18 +04:00
|
|
|
goto err_alloc;
|
|
|
|
}
|
2014-02-27 09:53:34 +04:00
|
|
|
|
|
|
|
pb->enable_gpio = gpio_to_desc(data->enable_gpio);
|
2013-08-30 14:32:18 +04:00
|
|
|
}
|
|
|
|
|
2016-11-22 16:41:22 +03:00
|
|
|
/*
|
2017-04-04 13:54:35 +03:00
|
|
|
* If the GPIO is not known to be already configured as output, that
|
2018-01-15 00:07:10 +03:00
|
|
|
* is, if gpiod_get_direction returns either 1 or -EINVAL, change the
|
|
|
|
* direction to output and set the GPIO as active.
|
2016-11-22 16:41:22 +03:00
|
|
|
* Do not force the GPIO to active when it was already output as it
|
|
|
|
* could cause backlight flickering or we would enable the backlight too
|
|
|
|
* early. Leave the decision of the initial backlight state for later.
|
|
|
|
*/
|
|
|
|
if (pb->enable_gpio &&
|
2018-01-15 00:07:10 +03:00
|
|
|
gpiod_get_direction(pb->enable_gpio) != 0)
|
2016-11-22 16:41:22 +03:00
|
|
|
gpiod_direction_output(pb->enable_gpio, 1);
|
2015-11-18 20:12:25 +03:00
|
|
|
|
2013-08-30 14:38:34 +04: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 09:53:34 +04:00
|
|
|
goto err_alloc;
|
2013-08-30 14:38:34 +04:00
|
|
|
}
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2012-09-17 10:20:47 +04:00
|
|
|
pb->pwm = devm_pwm_get(&pdev->dev, NULL);
|
2015-12-10 12:09:06 +03:00
|
|
|
if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
|
2011-12-17 00:25:29 +04:00
|
|
|
dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
|
2014-10-11 17:46:26 +04:00
|
|
|
pb->legacy = true;
|
2011-12-17 00:25:29 +04:00
|
|
|
pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
|
2015-10-12 15:29:03 +03: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-17 00:25:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "got pwm for backlight\n");
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
/* Sync up PWM state. */
|
|
|
|
pwm_init_state(pb->pwm, &state);
|
2018-04-09 11:33:32 +03:00
|
|
|
|
2018-08-14 19:50:59 +03: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;
|
|
|
|
}
|
|
|
|
|
2018-12-10 15:14:37 +03:00
|
|
|
if (data->levels) {
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
for (i = 0; i <= data->max_brightness; i++) {
|
|
|
|
if (data->levels[i] > pb->scale)
|
|
|
|
pb->scale = data->levels[i];
|
|
|
|
|
|
|
|
pb->levels = data->levels;
|
|
|
|
}
|
|
|
|
} 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 11:33:32 +03: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 15:14:37 +03:00
|
|
|
for (i = 0; i <= data->max_brightness; i++) {
|
|
|
|
if (data->levels[i] > pb->scale)
|
|
|
|
pb->scale = data->levels[i];
|
2018-04-09 11:33:32 +03:00
|
|
|
|
2018-12-10 15:14:37 +03:00
|
|
|
pb->levels = data->levels;
|
|
|
|
}
|
|
|
|
} 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 11:33:32 +03:00
|
|
|
}
|
|
|
|
|
2018-08-14 19:50:59 +03:00
|
|
|
pb->lth_brightness = data->lth_brightness * (state.period / pb->scale);
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2010-02-18 00:39:44 +03:00
|
|
|
memset(&props, 0, sizeof(struct backlight_properties));
|
2011-03-23 02:30:21 +03:00
|
|
|
props.type = BACKLIGHT_RAW;
|
2010-02-18 00:39:44 +03: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 12:35:08 +04:00
|
|
|
if (IS_ERR(bl)) {
|
|
|
|
dev_err(&pdev->dev, "failed to register backlight\n");
|
2008-05-22 17:18:40 +04:00
|
|
|
ret = PTR_ERR(bl);
|
2015-06-14 17:32:14 +03:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2014-02-27 09:53:34 +04:00
|
|
|
goto err_alloc;
|
2008-04-14 12:35:08 +04:00
|
|
|
}
|
|
|
|
|
2013-01-22 17:39:54 +04: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 12:35:08 +04:00
|
|
|
bl->props.brightness = data->dft_brightness;
|
2016-11-22 16:41:22 +03:00
|
|
|
bl->props.power = pwm_backlight_initial_power_state(pb);
|
2008-04-14 12:35:08 +04:00
|
|
|
backlight_update_status(bl);
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, bl);
|
|
|
|
return 0;
|
2008-05-22 17:18:40 +04:00
|
|
|
|
|
|
|
err_alloc:
|
|
|
|
if (data->exit)
|
|
|
|
data->exit(&pdev->dev);
|
|
|
|
return ret;
|
2008-04-14 12:35:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pwm_backlight_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct backlight_device *bl = platform_get_drvdata(pdev);
|
2013-02-22 04:43:46 +04:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 12:35:08 +04:00
|
|
|
|
|
|
|
backlight_device_unregister(bl);
|
2013-10-07 13:32:02 +04:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2011-12-17 00:25:29 +04:00
|
|
|
if (pb->exit)
|
|
|
|
pb->exit(&pdev->dev);
|
2014-10-11 17:46:26 +04:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 19:28:59 +04: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 12:15:47 +04:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2012-01-11 03:09:22 +04:00
|
|
|
static int pwm_backlight_suspend(struct device *dev)
|
2008-04-14 12:35:08 +04:00
|
|
|
{
|
2012-01-11 03:09:22 +04:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2013-02-22 04:43:46 +04:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 12:35:08 +04:00
|
|
|
|
2009-06-19 12:50:54 +04:00
|
|
|
if (pb->notify)
|
2009-11-10 20:20:40 +03:00
|
|
|
pb->notify(pb->dev, 0);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2013-10-07 13:32:02 +04:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2011-08-26 02:59:17 +04:00
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, 0);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-11 03:09:22 +04:00
|
|
|
static int pwm_backlight_resume(struct device *dev)
|
2008-04-14 12:35:08 +04:00
|
|
|
{
|
2012-01-11 03:09:22 +04:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2008-04-14 12:35:08 +04:00
|
|
|
|
|
|
|
backlight_update_status(bl);
|
2013-10-07 13:30:50 +04:00
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-25 12:15:47 +04:00
|
|
|
#endif
|
2012-01-11 03:09:22 +04:00
|
|
|
|
2013-10-09 06:33:02 +04: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-11 03:09:22 +04:00
|
|
|
|
2008-04-14 12:35:08 +04:00
|
|
|
static struct platform_driver pwm_backlight_driver = {
|
|
|
|
.driver = {
|
2011-12-17 00:25:29 +04:00
|
|
|
.name = "pwm-backlight",
|
|
|
|
.pm = &pwm_backlight_pm_ops,
|
|
|
|
.of_match_table = of_match_ptr(pwm_backlight_of_match),
|
2008-04-14 12:35:08 +04:00
|
|
|
},
|
|
|
|
.probe = pwm_backlight_probe,
|
|
|
|
.remove = pwm_backlight_remove,
|
2014-04-29 19:28:59 +04:00
|
|
|
.shutdown = pwm_backlight_shutdown,
|
2008-04-14 12:35:08 +04:00
|
|
|
};
|
|
|
|
|
2012-01-11 03:09:11 +04:00
|
|
|
module_platform_driver(pwm_backlight_driver);
|
2008-04-14 12:35:08 +04:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("PWM based Backlight Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
2008-08-06 00:01:24 +04:00
|
|
|
MODULE_ALIAS("platform:pwm-backlight");
|