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