2008-04-14 16:35:08 +08: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 13:30:37 +08:00
|
|
|
* 2) platform_data being correctly configured
|
2008-04-14 16:35:08 +08: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.
|
|
|
|
*/
|
|
|
|
|
2014-02-27 13:53:34 +08:00
|
|
|
#include <linux/gpio/consumer.h>
|
2013-08-30 18:32:18 +08:00
|
|
|
#include <linux/gpio.h>
|
2008-04-14 16:35:08 +08: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 18:38:34 +08: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 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2008-04-14 16:35:08 +08:00
|
|
|
|
|
|
|
struct pwm_bl_data {
|
|
|
|
struct pwm_device *pwm;
|
2009-11-11 01:20:40 +08:00
|
|
|
struct device *dev;
|
2008-04-14 16:35:08 +08:00
|
|
|
unsigned int period;
|
2010-11-12 06:05:28 +08:00
|
|
|
unsigned int lth_brightness;
|
2011-12-17 04:25:29 +08:00
|
|
|
unsigned int *levels;
|
2013-10-03 00:01:02 +08:00
|
|
|
bool enabled;
|
2013-08-30 18:38:34 +08:00
|
|
|
struct regulator *power_supply;
|
2014-02-27 13:53:34 +08:00
|
|
|
struct gpio_desc *enable_gpio;
|
2013-09-23 00:59:56 +08:00
|
|
|
unsigned int scale;
|
2014-10-11 21:46:26 +08:00
|
|
|
bool legacy;
|
2009-11-11 01:20:40 +08:00
|
|
|
int (*notify)(struct device *,
|
|
|
|
int brightness);
|
2011-08-26 06:59:17 +08:00
|
|
|
void (*notify_after)(struct device *,
|
|
|
|
int brightness);
|
2011-03-23 07:30:31 +08:00
|
|
|
int (*check_fb)(struct device *, struct fb_info *);
|
2011-12-17 04:25:29 +08:00
|
|
|
void (*exit)(struct device *);
|
2008-04-14 16:35:08 +08:00
|
|
|
};
|
|
|
|
|
2013-09-23 00:59:56 +08:00
|
|
|
static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
|
2013-10-07 17:32:02 +08:00
|
|
|
{
|
2013-10-22 15:37:05 +08:00
|
|
|
int err;
|
2013-10-07 17:32:02 +08:00
|
|
|
|
2013-10-03 00:01:02 +08:00
|
|
|
if (pb->enabled)
|
|
|
|
return;
|
|
|
|
|
2013-08-30 18:38:34 +08:00
|
|
|
err = regulator_enable(pb->power_supply);
|
|
|
|
if (err < 0)
|
|
|
|
dev_err(pb->dev, "failed to enable power supply\n");
|
|
|
|
|
2014-02-27 13:53:34 +08:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 16:18:12 +08:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 1);
|
2013-08-30 18:32:18 +08:00
|
|
|
|
2013-10-07 17:32:02 +08:00
|
|
|
pwm_enable(pb->pwm);
|
2013-10-03 00:01:02 +08:00
|
|
|
pb->enabled = true;
|
2013-10-07 17:32:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pwm_backlight_power_off(struct pwm_bl_data *pb)
|
|
|
|
{
|
2013-10-03 00:01:02 +08:00
|
|
|
if (!pb->enabled)
|
|
|
|
return;
|
|
|
|
|
2013-10-07 17:32:02 +08:00
|
|
|
pwm_config(pb->pwm, 0, pb->period);
|
|
|
|
pwm_disable(pb->pwm);
|
2013-10-03 00:01:02 +08:00
|
|
|
|
2014-02-27 13:53:34 +08:00
|
|
|
if (pb->enable_gpio)
|
2016-08-31 16:18:12 +08:00
|
|
|
gpiod_set_value_cansleep(pb->enable_gpio, 0);
|
2013-08-30 18:32:18 +08:00
|
|
|
|
2013-08-30 18:38:34 +08:00
|
|
|
regulator_disable(pb->power_supply);
|
2013-10-03 00:01:02 +08:00
|
|
|
pb->enabled = false;
|
2013-10-07 17:32:02 +08:00
|
|
|
}
|
|
|
|
|
2013-10-18 16:46:24 +08:00
|
|
|
static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
|
|
|
|
{
|
|
|
|
unsigned int lth = pb->lth_brightness;
|
2017-08-30 04:34:34 +08:00
|
|
|
u64 duty_cycle;
|
2013-10-18 16:46:24 +08:00
|
|
|
|
|
|
|
if (pb->levels)
|
|
|
|
duty_cycle = pb->levels[brightness];
|
|
|
|
else
|
|
|
|
duty_cycle = brightness;
|
|
|
|
|
2017-08-30 04:34:34 +08:00
|
|
|
duty_cycle *= pb->period - lth;
|
|
|
|
do_div(duty_cycle, pb->scale);
|
|
|
|
|
|
|
|
return duty_cycle + lth;
|
2013-10-18 16:46:24 +08:00
|
|
|
}
|
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
static int pwm_backlight_update_status(struct backlight_device *bl)
|
|
|
|
{
|
2013-02-22 08:43:46 +08:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 16:35:08 +08:00
|
|
|
int brightness = bl->props.brightness;
|
2013-10-18 16:46:24 +08:00
|
|
|
int duty_cycle;
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2013-01-30 14:19:15 +08:00
|
|
|
if (bl->props.power != FB_BLANK_UNBLANK ||
|
|
|
|
bl->props.fb_blank != FB_BLANK_UNBLANK ||
|
|
|
|
bl->props.state & BL_CORE_FBBLANK)
|
2008-04-14 16:35:08 +08:00
|
|
|
brightness = 0;
|
|
|
|
|
2008-05-22 21:18:40 +08:00
|
|
|
if (pb->notify)
|
2009-11-11 01:20:40 +08:00
|
|
|
brightness = pb->notify(pb->dev, brightness);
|
2008-05-22 21:18:40 +08:00
|
|
|
|
2013-10-18 16:46:24 +08:00
|
|
|
if (brightness > 0) {
|
|
|
|
duty_cycle = compute_duty_cycle(pb, brightness);
|
2012-07-09 14:04:23 +08:00
|
|
|
pwm_config(pb->pwm, duty_cycle, pb->period);
|
2013-09-23 00:59:56 +08:00
|
|
|
pwm_backlight_power_on(pb, brightness);
|
2013-10-18 16:46:24 +08:00
|
|
|
} else
|
2013-10-07 17:32:02 +08:00
|
|
|
pwm_backlight_power_off(pb);
|
2011-08-26 06:59:17 +08:00
|
|
|
|
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, brightness);
|
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-23 07:30:31 +08:00
|
|
|
static int pwm_backlight_check_fb(struct backlight_device *bl,
|
|
|
|
struct fb_info *info)
|
|
|
|
{
|
2013-02-22 08:43:46 +08:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2011-03-23 07:30:31 +08:00
|
|
|
|
|
|
|
return !pb->check_fb || pb->check_fb(pb->dev, info);
|
|
|
|
}
|
|
|
|
|
2009-12-14 07:58:57 +08:00
|
|
|
static const struct backlight_ops pwm_backlight_ops = {
|
2008-04-14 16:35:08 +08:00
|
|
|
.update_status = pwm_backlight_update_status,
|
2011-03-23 07:30:31 +08:00
|
|
|
.check_fb = pwm_backlight_check_fb,
|
2008-04-14 16:35:08 +08:00
|
|
|
};
|
|
|
|
|
2011-12-17 04:25:29 +08:00
|
|
|
#ifdef CONFIG_OF
|
|
|
|
static int pwm_backlight_parse_dt(struct device *dev,
|
|
|
|
struct platform_pwm_backlight_data *data)
|
|
|
|
{
|
|
|
|
struct device_node *node = dev->of_node;
|
|
|
|
struct property *prop;
|
|
|
|
int length;
|
|
|
|
u32 value;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!node)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
memset(data, 0, sizeof(*data));
|
|
|
|
|
|
|
|
/* determine the number of brightness levels */
|
|
|
|
prop = of_find_property(node, "brightness-levels", &length);
|
|
|
|
if (!prop)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
data->max_brightness--;
|
|
|
|
}
|
|
|
|
|
2014-08-20 14:38:36 +08:00
|
|
|
data->enable_gpio = -EINVAL;
|
2011-12-17 04:25:29 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:52:15 +08:00
|
|
|
static const struct of_device_id pwm_backlight_of_match[] = {
|
2011-12-17 04:25:29 +08: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;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-22 21:41:22 +08: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 21:41:23 +08:00
|
|
|
/* The PWM is disabled, keep it like this */
|
|
|
|
if (!pwm_is_enabled(pb->pwm))
|
|
|
|
return FB_BLANK_POWERDOWN;
|
|
|
|
|
2016-11-22 21:41:22 +08:00
|
|
|
return FB_BLANK_UNBLANK;
|
|
|
|
}
|
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
static int pwm_backlight_probe(struct platform_device *pdev)
|
|
|
|
{
|
2013-11-13 07:09:04 +08:00
|
|
|
struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
|
2011-12-17 04:25:29 +08:00
|
|
|
struct platform_pwm_backlight_data defdata;
|
|
|
|
struct backlight_properties props;
|
2008-04-14 16:35:08 +08:00
|
|
|
struct backlight_device *bl;
|
2015-12-10 17:09:06 +08:00
|
|
|
struct device_node *node = pdev->dev.of_node;
|
2008-04-14 16:35:08 +08:00
|
|
|
struct pwm_bl_data *pb;
|
2016-04-15 03:17:29 +08:00
|
|
|
struct pwm_args pargs;
|
2008-05-22 21:18:40 +08:00
|
|
|
int ret;
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2008-08-06 04:01:22 +08:00
|
|
|
if (!data) {
|
2011-12-17 04:25:29 +08: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 04:01:22 +08:00
|
|
|
}
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2008-05-22 21:18:40 +08:00
|
|
|
if (data->init) {
|
|
|
|
ret = data->init(&pdev->dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-24 06:02:00 +08:00
|
|
|
pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
|
2008-05-22 21:18:40 +08:00
|
|
|
if (!pb) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_alloc;
|
|
|
|
}
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2011-12-17 04:25:29 +08:00
|
|
|
if (data->levels) {
|
2013-09-23 00:59:56 +08:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= data->max_brightness; i++)
|
|
|
|
if (data->levels[i] > pb->scale)
|
|
|
|
pb->scale = data->levels[i];
|
|
|
|
|
2011-12-17 04:25:29 +08:00
|
|
|
pb->levels = data->levels;
|
|
|
|
} else
|
2013-09-23 00:59:56 +08:00
|
|
|
pb->scale = data->max_brightness;
|
2011-12-17 04:25:29 +08:00
|
|
|
|
2008-05-22 21:18:40 +08:00
|
|
|
pb->notify = data->notify;
|
2011-08-26 06:59:17 +08:00
|
|
|
pb->notify_after = data->notify_after;
|
2011-03-23 07:30:31 +08:00
|
|
|
pb->check_fb = data->check_fb;
|
2011-12-17 04:25:29 +08:00
|
|
|
pb->exit = data->exit;
|
2009-11-11 01:20:40 +08:00
|
|
|
pb->dev = &pdev->dev;
|
2013-10-03 00:01:02 +08:00
|
|
|
pb->enabled = false;
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2015-05-16 22:08:10 +08:00
|
|
|
pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
|
2015-11-19 01:12:25 +08:00
|
|
|
GPIOD_ASIS);
|
2014-02-27 13:53:34 +08:00
|
|
|
if (IS_ERR(pb->enable_gpio)) {
|
|
|
|
ret = PTR_ERR(pb->enable_gpio);
|
2014-06-25 17:18:18 +08:00
|
|
|
goto err_alloc;
|
2014-02-27 13:53:34 +08:00
|
|
|
}
|
2013-08-30 18:32:18 +08:00
|
|
|
|
2014-02-27 13:53:34 +08: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 18:32:18 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
|
2014-02-27 13:53:34 +08:00
|
|
|
data->enable_gpio, ret);
|
2013-08-30 18:32:18 +08:00
|
|
|
goto err_alloc;
|
|
|
|
}
|
2014-02-27 13:53:34 +08:00
|
|
|
|
|
|
|
pb->enable_gpio = gpio_to_desc(data->enable_gpio);
|
2013-08-30 18:32:18 +08:00
|
|
|
}
|
|
|
|
|
2016-11-22 21:41:22 +08:00
|
|
|
/*
|
2017-04-04 18:54:35 +08:00
|
|
|
* If the GPIO is not known to be already configured as output, that
|
|
|
|
* is, if gpiod_get_direction returns either GPIOF_DIR_IN or -EINVAL,
|
|
|
|
* change the direction to output and set the GPIO as active.
|
2016-11-22 21:41:22 +08: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 &&
|
2017-04-04 18:54:35 +08:00
|
|
|
gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
|
2016-11-22 21:41:22 +08:00
|
|
|
gpiod_direction_output(pb->enable_gpio, 1);
|
2015-11-19 01:12:25 +08:00
|
|
|
|
2013-08-30 18:38:34 +08: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 13:53:34 +08:00
|
|
|
goto err_alloc;
|
2013-08-30 18:38:34 +08:00
|
|
|
}
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2012-09-17 14:20:47 +08:00
|
|
|
pb->pwm = devm_pwm_get(&pdev->dev, NULL);
|
2015-12-10 17:09:06 +08:00
|
|
|
if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
|
2011-12-17 04:25:29 +08:00
|
|
|
dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
|
2014-10-11 21:46:26 +08:00
|
|
|
pb->legacy = true;
|
2011-12-17 04:25:29 +08:00
|
|
|
pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
|
2015-10-12 20:29:03 +08: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 04:25:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(&pdev->dev, "got pwm for backlight\n");
|
|
|
|
|
2016-04-15 03:17:29 +08:00
|
|
|
/*
|
|
|
|
* FIXME: pwm_apply_args() should be removed when switching to
|
|
|
|
* the atomic PWM API.
|
|
|
|
*/
|
|
|
|
pwm_apply_args(pb->pwm);
|
|
|
|
|
2011-12-17 04:25:29 +08: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,
|
2014-05-20 04:42:42 +08:00
|
|
|
* set the period from platform data if it has not already been set
|
|
|
|
* via the PWM lookup table.
|
2011-12-17 04:25:29 +08:00
|
|
|
*/
|
2016-04-15 03:17:29 +08:00
|
|
|
pwm_get_args(pb->pwm, &pargs);
|
|
|
|
pb->period = pargs.period;
|
2016-03-31 04:03:25 +08:00
|
|
|
if (!pb->period && (data->pwm_period_ns > 0))
|
2014-05-20 04:42:42 +08:00
|
|
|
pb->period = data->pwm_period_ns;
|
2011-12-17 04:25:29 +08:00
|
|
|
|
2013-09-23 00:59:56 +08:00
|
|
|
pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2010-02-18 05:39:44 +08:00
|
|
|
memset(&props, 0, sizeof(struct backlight_properties));
|
2011-03-23 07:30:21 +08:00
|
|
|
props.type = BACKLIGHT_RAW;
|
2010-02-18 05:39:44 +08: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 16:35:08 +08:00
|
|
|
if (IS_ERR(bl)) {
|
|
|
|
dev_err(&pdev->dev, "failed to register backlight\n");
|
2008-05-22 21:18:40 +08:00
|
|
|
ret = PTR_ERR(bl);
|
2015-06-14 22:32:14 +08:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2014-02-27 13:53:34 +08:00
|
|
|
goto err_alloc;
|
2008-04-14 16:35:08 +08:00
|
|
|
}
|
|
|
|
|
2013-01-22 21:39:54 +08: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 16:35:08 +08:00
|
|
|
bl->props.brightness = data->dft_brightness;
|
2016-11-22 21:41:22 +08:00
|
|
|
bl->props.power = pwm_backlight_initial_power_state(pb);
|
2008-04-14 16:35:08 +08:00
|
|
|
backlight_update_status(bl);
|
|
|
|
|
|
|
|
platform_set_drvdata(pdev, bl);
|
|
|
|
return 0;
|
2008-05-22 21:18:40 +08:00
|
|
|
|
|
|
|
err_alloc:
|
|
|
|
if (data->exit)
|
|
|
|
data->exit(&pdev->dev);
|
|
|
|
return ret;
|
2008-04-14 16:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pwm_backlight_remove(struct platform_device *pdev)
|
|
|
|
{
|
|
|
|
struct backlight_device *bl = platform_get_drvdata(pdev);
|
2013-02-22 08:43:46 +08:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 16:35:08 +08:00
|
|
|
|
|
|
|
backlight_device_unregister(bl);
|
2013-10-07 17:32:02 +08:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2011-12-17 04:25:29 +08:00
|
|
|
if (pb->exit)
|
|
|
|
pb->exit(&pdev->dev);
|
2014-10-11 21:46:26 +08:00
|
|
|
if (pb->legacy)
|
|
|
|
pwm_free(pb->pwm);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-29 23:28:59 +08: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 16:15:47 +08:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
2012-01-11 07:09:22 +08:00
|
|
|
static int pwm_backlight_suspend(struct device *dev)
|
2008-04-14 16:35:08 +08:00
|
|
|
{
|
2012-01-11 07:09:22 +08:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2013-02-22 08:43:46 +08:00
|
|
|
struct pwm_bl_data *pb = bl_get_data(bl);
|
2008-04-14 16:35:08 +08:00
|
|
|
|
2009-06-19 16:50:54 +08:00
|
|
|
if (pb->notify)
|
2009-11-11 01:20:40 +08:00
|
|
|
pb->notify(pb->dev, 0);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2013-10-07 17:32:02 +08:00
|
|
|
pwm_backlight_power_off(pb);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2011-08-26 06:59:17 +08:00
|
|
|
if (pb->notify_after)
|
|
|
|
pb->notify_after(pb->dev, 0);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-11 07:09:22 +08:00
|
|
|
static int pwm_backlight_resume(struct device *dev)
|
2008-04-14 16:35:08 +08:00
|
|
|
{
|
2012-01-11 07:09:22 +08:00
|
|
|
struct backlight_device *bl = dev_get_drvdata(dev);
|
2008-04-14 16:35:08 +08:00
|
|
|
|
|
|
|
backlight_update_status(bl);
|
2013-10-07 17:30:50 +08:00
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-25 16:15:47 +08:00
|
|
|
#endif
|
2012-01-11 07:09:22 +08:00
|
|
|
|
2013-10-09 10:33:02 +08: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 07:09:22 +08:00
|
|
|
|
2008-04-14 16:35:08 +08:00
|
|
|
static struct platform_driver pwm_backlight_driver = {
|
|
|
|
.driver = {
|
2011-12-17 04:25:29 +08:00
|
|
|
.name = "pwm-backlight",
|
|
|
|
.pm = &pwm_backlight_pm_ops,
|
|
|
|
.of_match_table = of_match_ptr(pwm_backlight_of_match),
|
2008-04-14 16:35:08 +08:00
|
|
|
},
|
|
|
|
.probe = pwm_backlight_probe,
|
|
|
|
.remove = pwm_backlight_remove,
|
2014-04-29 23:28:59 +08:00
|
|
|
.shutdown = pwm_backlight_shutdown,
|
2008-04-14 16:35:08 +08:00
|
|
|
};
|
|
|
|
|
2012-01-11 07:09:11 +08:00
|
|
|
module_platform_driver(pwm_backlight_driver);
|
2008-04-14 16:35:08 +08:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("PWM based Backlight Driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
2008-08-06 04:01:24 +08:00
|
|
|
MODULE_ALIAS("platform:pwm-backlight");
|