uboot/drivers/video/pwm_backlight.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * Copyright (c) 2016 Google, Inc
   4 * Written by Simon Glass <sjg@chromium.org>
   5 */
   6
   7#define LOG_CATEGORY UCLASS_PANEL_BACKLIGHT
   8
   9#include <common.h>
  10#include <dm.h>
  11#include <backlight.h>
  12#include <log.h>
  13#include <malloc.h>
  14#include <pwm.h>
  15#include <asm/gpio.h>
  16#include <linux/delay.h>
  17#include <power/regulator.h>
  18
  19/**
  20 * Private information for the PWM backlight
  21 *
  22 * If @num_levels is 0 then the levels are simple values with the backlight
  23 * value going between the minimum (default 0) and the maximum (default 255).
  24 * Otherwise the levels are an index into @levels (0..n-1).
  25 *
  26 * @reg: Regulator to enable to turn the backlight on (NULL if none)
  27 * @enable, GPIO to set to enable the backlight (can be missing)
  28 * @pwm: PWM to use to change the backlight brightness
  29 * @channel: PWM channel to use
  30 * @period_ns: Period of the backlight in nanoseconds
  31 * @levels: Levels for the backlight, or NULL if not using indexed levels
  32 * @num_levels: Number of levels
  33 * @cur_level: Current level for the backlight (index or value)
  34 * @default_level: Default level for the backlight (index or value)
  35 * @min_level: Minimum level of the backlight (full off)
  36 * @min_level: Maximum level of the backlight (full on)
  37 * @enabled: true if backlight is enabled
  38 */
  39struct pwm_backlight_priv {
  40        struct udevice *reg;
  41        struct gpio_desc enable;
  42        struct udevice *pwm;
  43        uint channel;
  44        uint period_ns;
  45        /*
  46         * the polarity of one PWM
  47         * 0: normal polarity
  48         * 1: inverted polarity
  49         */
  50        bool polarity;
  51        u32 *levels;
  52        int num_levels;
  53        uint default_level;
  54        int cur_level;
  55        uint min_level;
  56        uint max_level;
  57        bool enabled;
  58};
  59
  60static int set_pwm(struct pwm_backlight_priv *priv)
  61{
  62        uint duty_cycle;
  63        int ret;
  64
  65        duty_cycle = priv->period_ns * (priv->cur_level - priv->min_level) /
  66                (priv->max_level - priv->min_level + 1);
  67        ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
  68                             duty_cycle);
  69        if (ret)
  70                return log_ret(ret);
  71
  72        ret = pwm_set_invert(priv->pwm, priv->channel, priv->polarity);
  73        if (ret == -ENOSYS && !priv->polarity)
  74                ret = 0;
  75
  76        return log_ret(ret);
  77}
  78
  79static int enable_sequence(struct udevice *dev, int seq)
  80{
  81        struct pwm_backlight_priv *priv = dev_get_priv(dev);
  82        int ret;
  83
  84        switch (seq) {
  85        case 0:
  86                if (priv->reg) {
  87                        __maybe_unused struct dm_regulator_uclass_platdata
  88                                *plat;
  89
  90                        plat = dev_get_uclass_platdata(priv->reg);
  91                        log_debug("Enable '%s', regulator '%s'/'%s'\n",
  92                                  dev->name, priv->reg->name, plat->name);
  93                        ret = regulator_set_enable(priv->reg, true);
  94                        if (ret) {
  95                                log_debug("Cannot enable regulator for PWM '%s'\n",
  96                                          dev->name);
  97                                return log_ret(ret);
  98                        }
  99                        mdelay(120);
 100                }
 101                break;
 102        case 1:
 103                mdelay(10);
 104                dm_gpio_set_value(&priv->enable, 1);
 105                break;
 106        }
 107
 108        return 0;
 109}
 110
 111static int pwm_backlight_enable(struct udevice *dev)
 112{
 113        struct pwm_backlight_priv *priv = dev_get_priv(dev);
 114        int ret;
 115
 116        ret = enable_sequence(dev, 0);
 117        if (ret)
 118                return log_ret(ret);
 119        ret = set_pwm(priv);
 120        if (ret)
 121                return log_ret(ret);
 122        ret = pwm_set_enable(priv->pwm, priv->channel, true);
 123        if (ret)
 124                return log_ret(ret);
 125        ret = enable_sequence(dev, 1);
 126        if (ret)
 127                return log_ret(ret);
 128        priv->enabled = true;
 129
 130        return 0;
 131}
 132
 133static int pwm_backlight_set_brightness(struct udevice *dev, int percent)
 134{
 135        struct pwm_backlight_priv *priv = dev_get_priv(dev);
 136        bool disable = false;
 137        int level;
 138        int ret;
 139
 140        if (!priv->enabled) {
 141                ret = enable_sequence(dev, 0);
 142                if (ret)
 143                        return log_ret(ret);
 144        }
 145        if (percent == BACKLIGHT_OFF) {
 146                disable = true;
 147                percent = 0;
 148        }
 149        if (percent == BACKLIGHT_DEFAULT) {
 150                level = priv->default_level;
 151        } else {
 152                if (priv->levels) {
 153                        level = priv->levels[percent * (priv->num_levels - 1)
 154                                / 100];
 155                } else {
 156                        level = priv->min_level +
 157                                (priv->max_level - priv->min_level) *
 158                                percent / 100;
 159                }
 160        }
 161        priv->cur_level = level;
 162
 163        ret = set_pwm(priv);
 164        if (ret)
 165                return log_ret(ret);
 166        if (!priv->enabled) {
 167                ret = enable_sequence(dev, 1);
 168                if (ret)
 169                        return log_ret(ret);
 170                priv->enabled = true;
 171        }
 172        if (disable) {
 173                dm_gpio_set_value(&priv->enable, 0);
 174                if (priv->reg) {
 175                        ret = regulator_set_enable(priv->reg, false);
 176                        if (ret)
 177                                return log_ret(ret);
 178                }
 179                priv->enabled = false;
 180        }
 181
 182        return 0;
 183}
 184
 185static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
 186{
 187        struct pwm_backlight_priv *priv = dev_get_priv(dev);
 188        struct ofnode_phandle_args args;
 189        int index, ret, count, len;
 190        const u32 *cell;
 191
 192        log_debug("start\n");
 193        ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
 194                                           "power-supply", &priv->reg);
 195        if (ret)
 196                log_debug("Cannot get power supply: ret=%d\n", ret);
 197        ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
 198                                   GPIOD_IS_OUT);
 199        if (ret) {
 200                log_debug("Warning: cannot get enable GPIO: ret=%d\n", ret);
 201                if (ret != -ENOENT)
 202                        return log_ret(ret);
 203        }
 204        ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
 205                                         &args);
 206        if (ret) {
 207                log_debug("Cannot get PWM phandle: ret=%d\n", ret);
 208                return log_ret(ret);
 209        }
 210
 211        ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
 212        if (ret) {
 213                log_debug("Cannot get PWM: ret=%d\n", ret);
 214                return log_ret(ret);
 215        }
 216        if (args.args_count < 2)
 217                return log_msg_ret("Not enough arguments to pwm\n", -EINVAL);
 218        priv->channel = args.args[0];
 219        priv->period_ns = args.args[1];
 220        if (args.args_count > 2)
 221                priv->polarity = args.args[2];
 222
 223        index = dev_read_u32_default(dev, "default-brightness-level", 255);
 224        cell = dev_read_prop(dev, "brightness-levels", &len);
 225        count = len / sizeof(u32);
 226        if (cell && count > index) {
 227                priv->levels = malloc(len);
 228                if (!priv->levels)
 229                        return log_ret(-ENOMEM);
 230                dev_read_u32_array(dev, "brightness-levels", priv->levels,
 231                                   count);
 232                priv->num_levels = count;
 233                priv->default_level = priv->levels[index];
 234                priv->max_level = priv->levels[count - 1];
 235        } else {
 236                priv->default_level = index;
 237                priv->max_level = 255;
 238        }
 239        priv->cur_level = priv->default_level;
 240        log_debug("done\n");
 241
 242
 243        return 0;
 244}
 245
 246static int pwm_backlight_probe(struct udevice *dev)
 247{
 248        return 0;
 249}
 250
 251static const struct backlight_ops pwm_backlight_ops = {
 252        .enable         = pwm_backlight_enable,
 253        .set_brightness = pwm_backlight_set_brightness,
 254};
 255
 256static const struct udevice_id pwm_backlight_ids[] = {
 257        { .compatible = "pwm-backlight" },
 258        { }
 259};
 260
 261U_BOOT_DRIVER(pwm_backlight) = {
 262        .name   = "pwm_backlight",
 263        .id     = UCLASS_PANEL_BACKLIGHT,
 264        .of_match = pwm_backlight_ids,
 265        .ops    = &pwm_backlight_ops,
 266        .ofdata_to_platdata     = pwm_backlight_ofdata_to_platdata,
 267        .probe          = pwm_backlight_probe,
 268        .priv_auto_alloc_size   = sizeof(struct pwm_backlight_priv),
 269};
 270