linux/drivers/video/backlight/pwm_bl.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/video/backlight/pwm_bl.c
   3 *
   4 * simple PWM based backlight control, board code has to setup
   5 * 1) pin configuration so PWM waveforms can output
   6 * 2) platform_data being correctly configured
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License version 2 as
  10 * published by the Free Software Foundation.
  11 */
  12
  13#include <linux/gpio/consumer.h>
  14#include <linux/gpio.h>
  15#include <linux/module.h>
  16#include <linux/kernel.h>
  17#include <linux/init.h>
  18#include <linux/platform_device.h>
  19#include <linux/fb.h>
  20#include <linux/backlight.h>
  21#include <linux/err.h>
  22#include <linux/pwm.h>
  23#include <linux/pwm_backlight.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/slab.h>
  26
  27struct pwm_bl_data {
  28        struct pwm_device       *pwm;
  29        struct device           *dev;
  30        unsigned int            period;
  31        unsigned int            lth_brightness;
  32        unsigned int            *levels;
  33        bool                    enabled;
  34        struct regulator        *power_supply;
  35        struct gpio_desc        *enable_gpio;
  36        unsigned int            scale;
  37        bool                    legacy;
  38        int                     (*notify)(struct device *,
  39                                          int brightness);
  40        void                    (*notify_after)(struct device *,
  41                                        int brightness);
  42        int                     (*check_fb)(struct device *, struct fb_info *);
  43        void                    (*exit)(struct device *);
  44};
  45
  46static void pwm_backlight_power_on(struct pwm_bl_data *pb, int brightness)
  47{
  48        int err;
  49
  50        if (pb->enabled)
  51                return;
  52
  53        err = regulator_enable(pb->power_supply);
  54        if (err < 0)
  55                dev_err(pb->dev, "failed to enable power supply\n");
  56
  57        if (pb->enable_gpio)
  58                gpiod_set_value_cansleep(pb->enable_gpio, 1);
  59
  60        pwm_enable(pb->pwm);
  61        pb->enabled = true;
  62}
  63
  64static void pwm_backlight_power_off(struct pwm_bl_data *pb)
  65{
  66        if (!pb->enabled)
  67                return;
  68
  69        pwm_config(pb->pwm, 0, pb->period);
  70        pwm_disable(pb->pwm);
  71
  72        if (pb->enable_gpio)
  73                gpiod_set_value_cansleep(pb->enable_gpio, 0);
  74
  75        regulator_disable(pb->power_supply);
  76        pb->enabled = false;
  77}
  78
  79static int compute_duty_cycle(struct pwm_bl_data *pb, int brightness)
  80{
  81        unsigned int lth = pb->lth_brightness;
  82        u64 duty_cycle;
  83
  84        if (pb->levels)
  85                duty_cycle = pb->levels[brightness];
  86        else
  87                duty_cycle = brightness;
  88
  89        duty_cycle *= pb->period - lth;
  90        do_div(duty_cycle, pb->scale);
  91
  92        return duty_cycle + lth;
  93}
  94
  95static int pwm_backlight_update_status(struct backlight_device *bl)
  96{
  97        struct pwm_bl_data *pb = bl_get_data(bl);
  98        int brightness = bl->props.brightness;
  99        int duty_cycle;
 100
 101        if (bl->props.power != FB_BLANK_UNBLANK ||
 102            bl->props.fb_blank != FB_BLANK_UNBLANK ||
 103            bl->props.state & BL_CORE_FBBLANK)
 104                brightness = 0;
 105
 106        if (pb->notify)
 107                brightness = pb->notify(pb->dev, brightness);
 108
 109        if (brightness > 0) {
 110                duty_cycle = compute_duty_cycle(pb, brightness);
 111                pwm_config(pb->pwm, duty_cycle, pb->period);
 112                pwm_backlight_power_on(pb, brightness);
 113        } else
 114                pwm_backlight_power_off(pb);
 115
 116        if (pb->notify_after)
 117                pb->notify_after(pb->dev, brightness);
 118
 119        return 0;
 120}
 121
 122static int pwm_backlight_check_fb(struct backlight_device *bl,
 123                                  struct fb_info *info)
 124{
 125        struct pwm_bl_data *pb = bl_get_data(bl);
 126
 127        return !pb->check_fb || pb->check_fb(pb->dev, info);
 128}
 129
 130static const struct backlight_ops pwm_backlight_ops = {
 131        .update_status  = pwm_backlight_update_status,
 132        .check_fb       = pwm_backlight_check_fb,
 133};
 134
 135#ifdef CONFIG_OF
 136static int pwm_backlight_parse_dt(struct device *dev,
 137                                  struct platform_pwm_backlight_data *data)
 138{
 139        struct device_node *node = dev->of_node;
 140        struct property *prop;
 141        int length;
 142        u32 value;
 143        int ret;
 144
 145        if (!node)
 146                return -ENODEV;
 147
 148        memset(data, 0, sizeof(*data));
 149
 150        /* determine the number of brightness levels */
 151        prop = of_find_property(node, "brightness-levels", &length);
 152        if (!prop)
 153                return -EINVAL;
 154
 155        data->max_brightness = length / sizeof(u32);
 156
 157        /* read brightness levels from DT property */
 158        if (data->max_brightness > 0) {
 159                size_t size = sizeof(*data->levels) * data->max_brightness;
 160
 161                data->levels = devm_kzalloc(dev, size, GFP_KERNEL);
 162                if (!data->levels)
 163                        return -ENOMEM;
 164
 165                ret = of_property_read_u32_array(node, "brightness-levels",
 166                                                 data->levels,
 167                                                 data->max_brightness);
 168                if (ret < 0)
 169                        return ret;
 170
 171                ret = of_property_read_u32(node, "default-brightness-level",
 172                                           &value);
 173                if (ret < 0)
 174                        return ret;
 175
 176                data->dft_brightness = value;
 177                data->max_brightness--;
 178        }
 179
 180        data->enable_gpio = -EINVAL;
 181        return 0;
 182}
 183
 184static const struct of_device_id pwm_backlight_of_match[] = {
 185        { .compatible = "pwm-backlight" },
 186        { }
 187};
 188
 189MODULE_DEVICE_TABLE(of, pwm_backlight_of_match);
 190#else
 191static int pwm_backlight_parse_dt(struct device *dev,
 192                                  struct platform_pwm_backlight_data *data)
 193{
 194        return -ENODEV;
 195}
 196#endif
 197
 198static int pwm_backlight_initial_power_state(const struct pwm_bl_data *pb)
 199{
 200        struct device_node *node = pb->dev->of_node;
 201
 202        /* Not booted with device tree or no phandle link to the node */
 203        if (!node || !node->phandle)
 204                return FB_BLANK_UNBLANK;
 205
 206        /*
 207         * If the driver is probed from the device tree and there is a
 208         * phandle link pointing to the backlight node, it is safe to
 209         * assume that another driver will enable the backlight at the
 210         * appropriate time. Therefore, if it is disabled, keep it so.
 211         */
 212
 213        /* if the enable GPIO is disabled, do not enable the backlight */
 214        if (pb->enable_gpio && gpiod_get_value(pb->enable_gpio) == 0)
 215                return FB_BLANK_POWERDOWN;
 216
 217        /* The regulator is disabled, do not enable the backlight */
 218        if (!regulator_is_enabled(pb->power_supply))
 219                return FB_BLANK_POWERDOWN;
 220
 221        /* The PWM is disabled, keep it like this */
 222        if (!pwm_is_enabled(pb->pwm))
 223                return FB_BLANK_POWERDOWN;
 224
 225        return FB_BLANK_UNBLANK;
 226}
 227
 228static int pwm_backlight_probe(struct platform_device *pdev)
 229{
 230        struct platform_pwm_backlight_data *data = dev_get_platdata(&pdev->dev);
 231        struct platform_pwm_backlight_data defdata;
 232        struct backlight_properties props;
 233        struct backlight_device *bl;
 234        struct device_node *node = pdev->dev.of_node;
 235        struct pwm_bl_data *pb;
 236        struct pwm_args pargs;
 237        int ret;
 238
 239        if (!data) {
 240                ret = pwm_backlight_parse_dt(&pdev->dev, &defdata);
 241                if (ret < 0) {
 242                        dev_err(&pdev->dev, "failed to find platform data\n");
 243                        return ret;
 244                }
 245
 246                data = &defdata;
 247        }
 248
 249        if (data->init) {
 250                ret = data->init(&pdev->dev);
 251                if (ret < 0)
 252                        return ret;
 253        }
 254
 255        pb = devm_kzalloc(&pdev->dev, sizeof(*pb), GFP_KERNEL);
 256        if (!pb) {
 257                ret = -ENOMEM;
 258                goto err_alloc;
 259        }
 260
 261        if (data->levels) {
 262                unsigned int i;
 263
 264                for (i = 0; i <= data->max_brightness; i++)
 265                        if (data->levels[i] > pb->scale)
 266                                pb->scale = data->levels[i];
 267
 268                pb->levels = data->levels;
 269        } else
 270                pb->scale = data->max_brightness;
 271
 272        pb->notify = data->notify;
 273        pb->notify_after = data->notify_after;
 274        pb->check_fb = data->check_fb;
 275        pb->exit = data->exit;
 276        pb->dev = &pdev->dev;
 277        pb->enabled = false;
 278
 279        pb->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
 280                                                  GPIOD_ASIS);
 281        if (IS_ERR(pb->enable_gpio)) {
 282                ret = PTR_ERR(pb->enable_gpio);
 283                goto err_alloc;
 284        }
 285
 286        /*
 287         * Compatibility fallback for drivers still using the integer GPIO
 288         * platform data. Must go away soon.
 289         */
 290        if (!pb->enable_gpio && gpio_is_valid(data->enable_gpio)) {
 291                ret = devm_gpio_request_one(&pdev->dev, data->enable_gpio,
 292                                            GPIOF_OUT_INIT_HIGH, "enable");
 293                if (ret < 0) {
 294                        dev_err(&pdev->dev, "failed to request GPIO#%d: %d\n",
 295                                data->enable_gpio, ret);
 296                        goto err_alloc;
 297                }
 298
 299                pb->enable_gpio = gpio_to_desc(data->enable_gpio);
 300        }
 301
 302        /*
 303         * If the GPIO is not known to be already configured as output, that
 304         * is, if gpiod_get_direction returns either GPIOF_DIR_IN or -EINVAL,
 305         * change the direction to output and set the GPIO as active.
 306         * Do not force the GPIO to active when it was already output as it
 307         * could cause backlight flickering or we would enable the backlight too
 308         * early. Leave the decision of the initial backlight state for later.
 309         */
 310        if (pb->enable_gpio &&
 311            gpiod_get_direction(pb->enable_gpio) != GPIOF_DIR_OUT)
 312                gpiod_direction_output(pb->enable_gpio, 1);
 313
 314        pb->power_supply = devm_regulator_get(&pdev->dev, "power");
 315        if (IS_ERR(pb->power_supply)) {
 316                ret = PTR_ERR(pb->power_supply);
 317                goto err_alloc;
 318        }
 319
 320        pb->pwm = devm_pwm_get(&pdev->dev, NULL);
 321        if (IS_ERR(pb->pwm) && PTR_ERR(pb->pwm) != -EPROBE_DEFER && !node) {
 322                dev_err(&pdev->dev, "unable to request PWM, trying legacy API\n");
 323                pb->legacy = true;
 324                pb->pwm = pwm_request(data->pwm_id, "pwm-backlight");
 325        }
 326
 327        if (IS_ERR(pb->pwm)) {
 328                ret = PTR_ERR(pb->pwm);
 329                if (ret != -EPROBE_DEFER)
 330                        dev_err(&pdev->dev, "unable to request PWM\n");
 331                goto err_alloc;
 332        }
 333
 334        dev_dbg(&pdev->dev, "got pwm for backlight\n");
 335
 336        /*
 337         * FIXME: pwm_apply_args() should be removed when switching to
 338         * the atomic PWM API.
 339         */
 340        pwm_apply_args(pb->pwm);
 341
 342        /*
 343         * The DT case will set the pwm_period_ns field to 0 and store the
 344         * period, parsed from the DT, in the PWM device. For the non-DT case,
 345         * set the period from platform data if it has not already been set
 346         * via the PWM lookup table.
 347         */
 348        pwm_get_args(pb->pwm, &pargs);
 349        pb->period = pargs.period;
 350        if (!pb->period && (data->pwm_period_ns > 0))
 351                pb->period = data->pwm_period_ns;
 352
 353        pb->lth_brightness = data->lth_brightness * (pb->period / pb->scale);
 354
 355        memset(&props, 0, sizeof(struct backlight_properties));
 356        props.type = BACKLIGHT_RAW;
 357        props.max_brightness = data->max_brightness;
 358        bl = backlight_device_register(dev_name(&pdev->dev), &pdev->dev, pb,
 359                                       &pwm_backlight_ops, &props);
 360        if (IS_ERR(bl)) {
 361                dev_err(&pdev->dev, "failed to register backlight\n");
 362                ret = PTR_ERR(bl);
 363                if (pb->legacy)
 364                        pwm_free(pb->pwm);
 365                goto err_alloc;
 366        }
 367
 368        if (data->dft_brightness > data->max_brightness) {
 369                dev_warn(&pdev->dev,
 370                         "invalid default brightness level: %u, using %u\n",
 371                         data->dft_brightness, data->max_brightness);
 372                data->dft_brightness = data->max_brightness;
 373        }
 374
 375        bl->props.brightness = data->dft_brightness;
 376        bl->props.power = pwm_backlight_initial_power_state(pb);
 377        backlight_update_status(bl);
 378
 379        platform_set_drvdata(pdev, bl);
 380        return 0;
 381
 382err_alloc:
 383        if (data->exit)
 384                data->exit(&pdev->dev);
 385        return ret;
 386}
 387
 388static int pwm_backlight_remove(struct platform_device *pdev)
 389{
 390        struct backlight_device *bl = platform_get_drvdata(pdev);
 391        struct pwm_bl_data *pb = bl_get_data(bl);
 392
 393        backlight_device_unregister(bl);
 394        pwm_backlight_power_off(pb);
 395
 396        if (pb->exit)
 397                pb->exit(&pdev->dev);
 398        if (pb->legacy)
 399                pwm_free(pb->pwm);
 400
 401        return 0;
 402}
 403
 404static void pwm_backlight_shutdown(struct platform_device *pdev)
 405{
 406        struct backlight_device *bl = platform_get_drvdata(pdev);
 407        struct pwm_bl_data *pb = bl_get_data(bl);
 408
 409        pwm_backlight_power_off(pb);
 410}
 411
 412#ifdef CONFIG_PM_SLEEP
 413static int pwm_backlight_suspend(struct device *dev)
 414{
 415        struct backlight_device *bl = dev_get_drvdata(dev);
 416        struct pwm_bl_data *pb = bl_get_data(bl);
 417
 418        if (pb->notify)
 419                pb->notify(pb->dev, 0);
 420
 421        pwm_backlight_power_off(pb);
 422
 423        if (pb->notify_after)
 424                pb->notify_after(pb->dev, 0);
 425
 426        return 0;
 427}
 428
 429static int pwm_backlight_resume(struct device *dev)
 430{
 431        struct backlight_device *bl = dev_get_drvdata(dev);
 432
 433        backlight_update_status(bl);
 434
 435        return 0;
 436}
 437#endif
 438
 439static const struct dev_pm_ops pwm_backlight_pm_ops = {
 440#ifdef CONFIG_PM_SLEEP
 441        .suspend = pwm_backlight_suspend,
 442        .resume = pwm_backlight_resume,
 443        .poweroff = pwm_backlight_suspend,
 444        .restore = pwm_backlight_resume,
 445#endif
 446};
 447
 448static struct platform_driver pwm_backlight_driver = {
 449        .driver         = {
 450                .name           = "pwm-backlight",
 451                .pm             = &pwm_backlight_pm_ops,
 452                .of_match_table = of_match_ptr(pwm_backlight_of_match),
 453        },
 454        .probe          = pwm_backlight_probe,
 455        .remove         = pwm_backlight_remove,
 456        .shutdown       = pwm_backlight_shutdown,
 457};
 458
 459module_platform_driver(pwm_backlight_driver);
 460
 461MODULE_DESCRIPTION("PWM based Backlight Driver");
 462MODULE_LICENSE("GPL");
 463MODULE_ALIAS("platform:pwm-backlight");
 464