1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
12 #include <power/regulator.h>
14 struct pwm_backlight_priv {
16 struct gpio_desc enable;
25 static int pwm_backlight_enable(struct udevice *dev)
27 struct pwm_backlight_priv *priv = dev_get_priv(dev);
28 struct dm_regulator_uclass_platdata *plat;
33 plat = dev_get_uclass_platdata(priv->reg);
34 debug("%s: Enable '%s', regulator '%s'/'%s'\n", __func__,
35 dev->name, priv->reg->name, plat->name);
36 ret = regulator_set_enable(priv->reg, true);
38 debug("%s: Cannot enable regulator for PWM '%s'\n",
45 duty_cycle = priv->period_ns * (priv->default_level - priv->min_level) /
46 (priv->max_level - priv->min_level + 1);
47 ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns,
51 ret = pwm_set_enable(priv->pwm, priv->channel, true);
55 dm_gpio_set_value(&priv->enable, 1);
60 static int pwm_backlight_ofdata_to_platdata(struct udevice *dev)
62 struct pwm_backlight_priv *priv = dev_get_priv(dev);
63 struct ofnode_phandle_args args;
64 int index, ret, count, len;
67 debug("%s: start\n", __func__);
68 ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev,
69 "power-supply", &priv->reg);
71 debug("%s: Cannot get power supply: ret=%d\n", __func__, ret);
72 ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable,
75 debug("%s: Warning: cannot get enable GPIO: ret=%d\n",
80 ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0,
83 debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret);
87 ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm);
89 debug("%s: Cannot get PWM: ret=%d\n", __func__, ret);
92 priv->channel = args.args[0];
93 priv->period_ns = args.args[1];
95 index = dev_read_u32_default(dev, "default-brightness-level", 255);
96 cell = dev_read_prop(dev, "brightness-levels", &len);
97 count = len / sizeof(u32);
98 if (cell && count > index) {
99 priv->default_level = fdt32_to_cpu(cell[index]);
100 priv->max_level = fdt32_to_cpu(cell[count - 1]);
102 priv->default_level = index;
103 priv->max_level = 255;
105 debug("%s: done\n", __func__);
111 static int pwm_backlight_probe(struct udevice *dev)
116 static const struct backlight_ops pwm_backlight_ops = {
117 .enable = pwm_backlight_enable,
120 static const struct udevice_id pwm_backlight_ids[] = {
121 { .compatible = "pwm-backlight" },
125 U_BOOT_DRIVER(pwm_backlight) = {
126 .name = "pwm_backlight",
127 .id = UCLASS_PANEL_BACKLIGHT,
128 .of_match = pwm_backlight_ids,
129 .ops = &pwm_backlight_ops,
130 .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata,
131 .probe = pwm_backlight_probe,
132 .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv),