Release 4.11 drivers/video/backlight/pwm_bl.c
/*
* 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
* 2) platform_data being correctly configured
*
* 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.
*/
#include <linux/gpio/consumer.h>
#include <linux/gpio.h>
#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>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
struct pwm_bl_data {
struct pwm_device *pwm;
struct device *dev;
unsigned int period;
unsigned int lth_brightness;
unsigned int *levels;
bool enabled;
struct regulator *power_supply;
struct gpio_desc *enable_gpio;
unsigned int scale;
bool legacy;
int (*notify)(struct device *,
int brightness);
void (*notify_after)(struct device *,
int brightness);
int (*check_fb)(struct device *, struct fb_info *);
void (*exit)(struct device *);
};
static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
{
int err;
if (pb->enabled)
return;
err = regulator_enable(pb->power_supply);
if (err < 0)
dev_err(pb->dev, "failed to enable power supply\n");
if (pb->enable_gpio)
gpiod_set_value_cansleep(pb->enable_gpio, 1);
pwm_enable(pb->pwm);
pb->enabled = true;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 75 | 98.68% | 4 | 80.00% |
Maxime Ripard | 1 | 1.32% | 1 | 20.00% |
Total | 76 | 100.00% | 5 | 100.00% |
static void pwm_backlight_power_off(struct pwm_bl_data *pb)
{
if (!pb->enabled)
return;
pwm_config(pb->pwm, 0, pb->period);
pwm_disable(pb->pwm);
if (pb->enable_gpio)
gpiod_set_value_cansleep(pb->enable_gpio, 0);
regulator_disable(pb->power_supply);
pb->enabled = false;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 66 | 98.51% | 4 | 80.00% |
Maxime Ripard | 1 | 1.49% | 1 | 20.00% |
Total | 67 | 100.00% | 5 | 100.00% |
static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
{
unsigned int lth = pb->lth_brightness;
int duty_cycle;
if (pb->levels)
duty_cycle = pb->levels[brightness];
else
duty_cycle = brightness;
return (duty_cycle * (pb->period - lth) / pb->scale) + lth;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 64 | 100.00% | 1 | 100.00% |
Total | 64 | 100.00% | 1 | 100.00% |
static int pwm_backlight_update_status(struct backlight_device *bl)
{
struct pwm_bl_data *pb = bl_get_data(bl);
int brightness = bl->props.brightness;
int duty_cycle;
if (bl->props.power != FB_BLANK_UNBLANK ||
bl->props.fb_blank != FB_BLANK_UNBLANK ||
bl->props.state & BL_CORE_FBBLANK)
brightness = 0;
if (pb->notify)
brightness = pb->notify(pb->dev, brightness);
if (brightness > 0) {
duty_cycle = compute_duty_cycle(pb, brightness);
pwm_config(pb->pwm, duty_cycle, pb->period);
pwm_backlight_power_on(pb, brightness);
} else
pwm_backlight_power_off(pb);
if (pb->notify_after)
pb->notify_after(pb->dev, brightness);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Eric Miao | 76 | 52.41% | 1 | 9.09% |
Dilan Lee | 17 | 11.72% | 1 | 9.09% |
Philipp Zabel | 15 | 10.34% | 1 | 9.09% |
Thierry Reding | 12 | 8.28% | 3 | 27.27% |
Alexandre Courbot | 11 | 7.59% | 2 | 18.18% |
Arun Murthy | 9 | 6.21% | 1 | 9.09% |
Ben Dooks | 4 | 2.76% | 1 | 9.09% |
Jingoo Han | 1 | 0.69% | 1 | 9.09% |
Total | 145 | 100.00% | 11 | 100.00% |
static int pwm_backlight_check_fb(struct backlight_device *bl,
struct fb_info *info)
{
struct pwm_bl_data *pb = bl_get_data(bl);
return !pb->check_fb || pb->check_fb(pb->dev, info);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Robert Morell | 42 | 97.67% | 1 | 50.00% |
Jingoo Han | 1 | 2.33% | 1 | 50.00% |
Total | 43 | 100.00% | 2 | 100.00% |
static const struct backlight_ops pwm_backlight_ops = {
.update_status = pwm_backlight_update_status,
.check_fb = pwm_backlight_check_fb,
};
#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--;
}
data->enable_gpio = -EINVAL;
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 205 | 96.70% | 1 | 50.00% |
Lothar Waßmann | 7 | 3.30% | 1 | 50.00% |
Total | 212 | 100.00% | 2 | 100.00% |
static struct of_device_id pwm_backlight_of_match[] = {
{ .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;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 20 | 100.00% | 1 | 100.00% |
Total | 20 | 100.00% | 1 | 100.00% |
#endif
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;
/* The PWM is disabled, keep it like this */
if (!pwm_is_enabled(pb->pwm))
return FB_BLANK_POWERDOWN;
return FB_BLANK_UNBLANK;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Peter Ujfalusi | 88 | 100.00% | 2 | 100.00% |
Total | 88 | 100.00% | 2 | 100.00% |
static int pwm_backlight_probe(struct platform_device *pdev)
{
struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
struct platform_pwm_backlight_data defdata;
struct backlight_properties props;
struct backlight_device *bl;
struct device_node *node = pdev->dev.of_node;
struct pwm_bl_data *pb;
struct pwm_args pargs;
int ret;
if (!data) {
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;
}
if (data->init) {
ret = data->init(&pdev->dev);
if (ret < 0)
return ret;
}
pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
if (!pb) {
ret = -ENOMEM;
goto err_alloc;
}
if (data->levels) {
unsigned int i;
for (i = 0; i <= data->max_brightness; i++)
if (data->levels[i] > pb->scale)
pb->scale = data->levels[i];
pb->levels = data->levels;
} else
pb->scale = data->max_brightness;
pb->notify = data->notify;
pb->notify_after = data->notify_after;
pb->check_fb = data->check_fb;
pb->exit = data->exit;
pb->dev = &pdev->dev;
pb->enabled = false;
pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
GPIOD_ASIS);
if (IS_ERR(pb->enable_gpio)) {
ret = PTR_ERR(pb->enable_gpio);
goto err_alloc;
}
/*
* 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");
if (ret < 0) {
dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
data->enable_gpio, ret);
goto err_alloc;
}
pb->enable_gpio = gpio_to_desc(data->enable_gpio);
}
/*
* 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.
* 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 &&
gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
gpiod_direction_output(pb->enable_gpio, 1);
pb->power_supply = devm_regulator_get(&pdev->dev, "power");
if (IS_ERR(pb->power_supply)) {
ret = PTR_ERR(pb->power_supply);
goto err_alloc;
}
pb->pwm = devm_pwm_get(&pdev->dev, NULL);
if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
pb->legacy = true;
pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
}
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;
}
dev_dbg(&pdev->dev, "got pwm for backlight\n");
/*
* FIXME: pwm_apply_args() should be removed when switching to
* the atomic PWM API.
*/
pwm_apply_args(pb->pwm);
/*
* The DT case will set the pwm_period_ns field to 0 and store the
* period, parsed from the DT, in the PWM device. For the non-DT case,
* set the period from platform data if it has not already been set
* via the PWM lookup table.
*/
pwm_get_args(pb->pwm, &pargs);
pb->period = pargs.period;
if (!pb->period && (data->pwm_period_ns > 0))
pb->period = data->pwm_period_ns;
pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
memset(&props, 0, sizeof(struct backlight_properties));
props.type = BACKLIGHT_RAW;
props.max_brightness = data->max_brightness;
bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
&pwm_backlight_ops, &props);
if (IS_ERR(bl)) {
dev_err(&pdev->dev, "failed to register backlight\n");
ret = PTR_ERR(bl);
if (pb->legacy)
pwm_free(pb->pwm);
goto err_alloc;
}
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;
}
bl->props.brightness = data->dft_brightness;
bl->props.power = pwm_backlight_initial_power_state(pb);
backlight_update_status(bl);
platform_set_drvdata(pdev, bl);
return 0;
err_alloc:
if (data->exit)
data->exit(&pdev->dev);
return ret;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 210 | 25.21% | 4 | 12.12% |
Eric Miao | 150 | 18.01% | 2 | 6.06% |
Philipp Zabel | 124 | 14.89% | 3 | 9.09% |
Alexandre Courbot | 67 | 8.04% | 2 | 6.06% |
Peter Ujfalusi | 42 | 5.04% | 2 | 6.06% |
Vladimir Zapolskiy | 42 | 5.04% | 3 | 9.09% |
Mike Dunn | 40 | 4.80% | 1 | 3.03% |
Ben Dooks | 39 | 4.68% | 3 | 9.09% |
Matthew Garrett | 31 | 3.72% | 2 | 6.06% |
Boris Brezillon | 23 | 2.76% | 1 | 3.03% |
Alexandre Belloni | 17 | 2.04% | 1 | 3.03% |
Nicolas Ferre | 9 | 1.08% | 1 | 3.03% |
Arun Murthy | 8 | 0.96% | 1 | 3.03% |
Robert Morell | 8 | 0.96% | 1 | 3.03% |
Dilan Lee | 8 | 0.96% | 1 | 3.03% |
Julia Lawall | 6 | 0.72% | 1 | 3.03% |
Jingoo Han | 4 | 0.48% | 1 | 3.03% |
Geert Uytterhoeven | 3 | 0.36% | 1 | 3.03% |
Axel Lin | 1 | 0.12% | 1 | 3.03% |
Sachin Kamat | 1 | 0.12% | 1 | 3.03% |
Total | 833 | 100.00% | 33 | 100.00% |
static int pwm_backlight_remove(struct platform_device *pdev)
{
struct backlight_device *bl = platform_get_drvdata(pdev);
struct pwm_bl_data *pb = bl_get_data(bl);
backlight_device_unregister(bl);
pwm_backlight_power_off(pb);
if (pb->exit)
pb->exit(&pdev->dev);
if (pb->legacy)
pwm_free(pb->pwm);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Eric Miao | 42 | 57.53% | 1 | 16.67% |
Philipp Zabel | 14 | 19.18% | 1 | 16.67% |
Vladimir Zapolskiy | 13 | 17.81% | 1 | 16.67% |
Thierry Reding | 3 | 4.11% | 2 | 33.33% |
Jingoo Han | 1 | 1.37% | 1 | 16.67% |
Total | 73 | 100.00% | 6 | 100.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);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 36 | 100.00% | 1 | 100.00% |
Total | 36 | 100.00% | 1 | 100.00% |
#ifdef CONFIG_PM_SLEEP
static int pwm_backlight_suspend(struct device *dev)
{
struct backlight_device *bl = dev_get_drvdata(dev);
struct pwm_bl_data *pb = bl_get_data(bl);
if (pb->notify)
pb->notify(pb->dev, 0);
pwm_backlight_power_off(pb);
if (pb->notify_after)
pb->notify_after(pb->dev, 0);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Eric Miao | 33 | 45.21% | 1 | 14.29% |
Dilan Lee | 17 | 23.29% | 1 | 14.29% |
Marc Zyngier | 13 | 17.81% | 1 | 14.29% |
Ben Dooks | 4 | 5.48% | 1 | 14.29% |
Mark Brown | 4 | 5.48% | 1 | 14.29% |
Jingoo Han | 1 | 1.37% | 1 | 14.29% |
Thierry Reding | 1 | 1.37% | 1 | 14.29% |
Total | 73 | 100.00% | 7 | 100.00% |
static int pwm_backlight_resume(struct device *dev)
{
struct backlight_device *bl = dev_get_drvdata(dev);
backlight_update_status(bl);
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Eric Miao | 25 | 86.21% | 1 | 50.00% |
Mark Brown | 4 | 13.79% | 1 | 50.00% |
Total | 29 | 100.00% | 2 | 100.00% |
#endif
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
};
static struct platform_driver pwm_backlight_driver = {
.driver = {
.name = "pwm-backlight",
.pm = &pwm_backlight_pm_ops,
.of_match_table = of_match_ptr(pwm_backlight_of_match),
},
.probe = pwm_backlight_probe,
.remove = pwm_backlight_remove,
.shutdown = pwm_backlight_shutdown,
};
module_platform_driver(pwm_backlight_driver);
MODULE_DESCRIPTION("PWM based Backlight Driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:pwm-backlight");
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Thierry Reding | 769 | 37.42% | 7 | 14.00% |
Eric Miao | 426 | 20.73% | 2 | 4.00% |
Philipp Zabel | 163 | 7.93% | 3 | 6.00% |
Peter Ujfalusi | 130 | 6.33% | 3 | 6.00% |
Alexandre Courbot | 83 | 4.04% | 4 | 8.00% |
Robert Morell | 68 | 3.31% | 1 | 2.00% |
Ben Dooks | 61 | 2.97% | 4 | 8.00% |
Vladimir Zapolskiy | 58 | 2.82% | 3 | 6.00% |
Dilan Lee | 56 | 2.73% | 1 | 2.00% |
Mike Dunn | 44 | 2.14% | 1 | 2.00% |
Matthew Garrett | 31 | 1.51% | 2 | 4.00% |
Huayi Li | 30 | 1.46% | 1 | 2.00% |
Boris Brezillon | 23 | 1.12% | 1 | 2.00% |
Arun Murthy | 21 | 1.02% | 1 | 2.00% |
Alexandre Belloni | 17 | 0.83% | 1 | 2.00% |
Mark Brown | 17 | 0.83% | 1 | 2.00% |
Marc Zyngier | 13 | 0.63% | 1 | 2.00% |
Jingoo Han | 11 | 0.54% | 3 | 6.00% |
Nicolas Ferre | 9 | 0.44% | 1 | 2.00% |
Lothar Waßmann | 7 | 0.34% | 1 | 2.00% |
Julia Lawall | 6 | 0.29% | 1 | 2.00% |
Geert Uytterhoeven | 3 | 0.15% | 1 | 2.00% |
Tejun Heo | 3 | 0.15% | 1 | 2.00% |
Axel Lin | 2 | 0.10% | 2 | 4.00% |
Maxime Ripard | 2 | 0.10% | 1 | 2.00% |
Sachin Kamat | 1 | 0.05% | 1 | 2.00% |
Emese Revfy | 1 | 0.05% | 1 | 2.00% |
Total | 2055 | 100.00% | 50 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.