Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <backlight.h> |
| 11 | #include <pwm.h> |
| 12 | #include <asm/gpio.h> |
| 13 | #include <power/regulator.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | struct pwm_backlight_priv { |
| 18 | struct udevice *reg; |
| 19 | struct gpio_desc enable; |
| 20 | struct udevice *pwm; |
| 21 | uint channel; |
| 22 | uint period_ns; |
| 23 | uint default_level; |
| 24 | uint min_level; |
| 25 | uint max_level; |
| 26 | }; |
| 27 | |
| 28 | static int pwm_backlight_enable(struct udevice *dev) |
| 29 | { |
| 30 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 31 | struct dm_regulator_uclass_platdata *plat; |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 32 | uint duty_cycle; |
| 33 | int ret; |
| 34 | |
Kever Yang | 19f124d | 2018-02-09 10:45:12 +0800 | [diff] [blame] | 35 | if (priv->reg) { |
| 36 | plat = dev_get_uclass_platdata(priv->reg); |
| 37 | debug("%s: Enable '%s', regulator '%s'/'%s'\n", __func__, |
| 38 | dev->name, priv->reg->name, plat->name); |
| 39 | ret = regulator_set_enable(priv->reg, true); |
| 40 | if (ret) { |
| 41 | debug("%s: Cannot enable regulator for PWM '%s'\n", |
| 42 | __func__, dev->name); |
| 43 | return ret; |
| 44 | } |
| 45 | mdelay(120); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 46 | } |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 47 | |
| 48 | duty_cycle = priv->period_ns * (priv->default_level - priv->min_level) / |
| 49 | (priv->max_level - priv->min_level + 1); |
| 50 | ret = pwm_set_config(priv->pwm, priv->channel, priv->period_ns, |
| 51 | duty_cycle); |
| 52 | if (ret) |
| 53 | return ret; |
| 54 | ret = pwm_set_enable(priv->pwm, priv->channel, true); |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | mdelay(10); |
| 58 | dm_gpio_set_value(&priv->enable, 1); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int pwm_backlight_ofdata_to_platdata(struct udevice *dev) |
| 64 | { |
| 65 | struct pwm_backlight_priv *priv = dev_get_priv(dev); |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 66 | struct ofnode_phandle_args args; |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 67 | int index, ret, count, len; |
| 68 | const u32 *cell; |
| 69 | |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 70 | debug("%s: start\n", __func__); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 71 | ret = uclass_get_device_by_phandle(UCLASS_REGULATOR, dev, |
| 72 | "power-supply", &priv->reg); |
Kever Yang | 19f124d | 2018-02-09 10:45:12 +0800 | [diff] [blame] | 73 | if (ret) |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 74 | debug("%s: Cannot get power supply: ret=%d\n", __func__, ret); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 75 | ret = gpio_request_by_name(dev, "enable-gpios", 0, &priv->enable, |
| 76 | GPIOD_IS_OUT); |
| 77 | if (ret) { |
| 78 | debug("%s: Warning: cannot get enable GPIO: ret=%d\n", |
| 79 | __func__, ret); |
| 80 | if (ret != -ENOENT) |
| 81 | return ret; |
| 82 | } |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 83 | ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, |
| 84 | &args); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 85 | if (ret) { |
| 86 | debug("%s: Cannot get PWM phandle: ret=%d\n", __func__, ret); |
| 87 | return ret; |
| 88 | } |
| 89 | |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 90 | ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 91 | if (ret) { |
| 92 | debug("%s: Cannot get PWM: ret=%d\n", __func__, ret); |
| 93 | return ret; |
| 94 | } |
| 95 | priv->channel = args.args[0]; |
| 96 | priv->period_ns = args.args[1]; |
| 97 | |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 98 | index = dev_read_u32_default(dev, "default-brightness-level", 255); |
| 99 | cell = dev_read_prop(dev, "brightness-levels", &len); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 100 | count = len / sizeof(u32); |
| 101 | if (cell && count > index) { |
| 102 | priv->default_level = fdt32_to_cpu(cell[index]); |
| 103 | priv->max_level = fdt32_to_cpu(cell[count - 1]); |
| 104 | } else { |
| 105 | priv->default_level = index; |
| 106 | priv->max_level = 255; |
| 107 | } |
Simon Glass | 7cf208d | 2017-06-12 06:21:37 -0600 | [diff] [blame] | 108 | debug("%s: done\n", __func__); |
Simon Glass | 65fba59 | 2016-01-21 19:44:57 -0700 | [diff] [blame] | 109 | |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int pwm_backlight_probe(struct udevice *dev) |
| 115 | { |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static const struct backlight_ops pwm_backlight_ops = { |
| 120 | .enable = pwm_backlight_enable, |
| 121 | }; |
| 122 | |
| 123 | static const struct udevice_id pwm_backlight_ids[] = { |
| 124 | { .compatible = "pwm-backlight" }, |
| 125 | { } |
| 126 | }; |
| 127 | |
| 128 | U_BOOT_DRIVER(pwm_backlight) = { |
| 129 | .name = "pwm_backlight", |
| 130 | .id = UCLASS_PANEL_BACKLIGHT, |
| 131 | .of_match = pwm_backlight_ids, |
| 132 | .ops = &pwm_backlight_ops, |
| 133 | .ofdata_to_platdata = pwm_backlight_ofdata_to_platdata, |
| 134 | .probe = pwm_backlight_probe, |
| 135 | .priv_auto_alloc_size = sizeof(struct pwm_backlight_priv), |
| 136 | }; |