Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 VK |
| 4 | * Author: Ivan Vozvakhov <i.vozvakhov@vk.team> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <led.h> |
| 11 | #include <malloc.h> |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 12 | #include <pwm.h> |
| 13 | |
| 14 | #define LEDS_PWM_DRIVER_NAME "led_pwm" |
| 15 | |
| 16 | struct led_pwm_priv { |
| 17 | struct udevice *pwm; |
| 18 | uint period; /* period in ns */ |
| 19 | uint duty; /* duty cycle in ns */ |
| 20 | uint channel; /* pwm channel number */ |
| 21 | bool active_low; /* pwm polarity */ |
| 22 | bool enabled; |
| 23 | }; |
| 24 | |
| 25 | static int led_pwm_enable(struct udevice *dev) |
| 26 | { |
| 27 | struct led_pwm_priv *priv = dev_get_priv(dev); |
| 28 | int ret; |
| 29 | |
| 30 | ret = pwm_set_invert(priv->pwm, priv->channel, priv->active_low); |
| 31 | if (ret) |
| 32 | return ret; |
| 33 | |
| 34 | ret = pwm_set_config(priv->pwm, priv->channel, priv->period, priv->duty); |
| 35 | if (ret) |
| 36 | return ret; |
| 37 | |
| 38 | ret = pwm_set_enable(priv->pwm, priv->channel, true); |
| 39 | if (ret) |
| 40 | return ret; |
| 41 | |
| 42 | priv->enabled = true; |
| 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static int led_pwm_disable(struct udevice *dev) |
| 48 | { |
| 49 | struct led_pwm_priv *priv = dev_get_priv(dev); |
| 50 | int ret; |
| 51 | |
| 52 | ret = pwm_set_config(priv->pwm, priv->channel, priv->period, 0); |
| 53 | if (ret) |
| 54 | return ret; |
| 55 | |
| 56 | ret = pwm_set_enable(priv->pwm, priv->channel, false); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
| 60 | priv->enabled = false; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int led_pwm_set_state(struct udevice *dev, enum led_state_t state) |
| 66 | { |
| 67 | struct led_pwm_priv *priv = dev_get_priv(dev); |
| 68 | int ret; |
| 69 | |
| 70 | switch (state) { |
| 71 | case LEDST_OFF: |
| 72 | ret = led_pwm_disable(dev); |
| 73 | break; |
| 74 | case LEDST_ON: |
| 75 | ret = led_pwm_enable(dev); |
| 76 | break; |
| 77 | case LEDST_TOGGLE: |
| 78 | ret = (priv->enabled) ? led_pwm_disable(dev) : led_pwm_enable(dev); |
| 79 | break; |
| 80 | default: |
| 81 | ret = -ENOSYS; |
| 82 | } |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | static enum led_state_t led_pwm_get_state(struct udevice *dev) |
| 88 | { |
| 89 | struct led_pwm_priv *priv = dev_get_priv(dev); |
| 90 | |
| 91 | return (priv->enabled) ? LEDST_ON : LEDST_OFF; |
| 92 | } |
| 93 | |
| 94 | static int led_pwm_probe(struct udevice *dev) |
| 95 | { |
| 96 | struct led_pwm_priv *priv = dev_get_priv(dev); |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 97 | |
| 98 | return led_pwm_set_state(dev, (priv->enabled) ? LEDST_ON : LEDST_OFF); |
| 99 | } |
| 100 | |
| 101 | static int led_pwm_of_to_plat(struct udevice *dev) |
| 102 | { |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 103 | struct led_pwm_priv *priv = dev_get_priv(dev); |
| 104 | struct ofnode_phandle_args args; |
| 105 | uint def_brightness, max_brightness; |
| 106 | int ret; |
| 107 | |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 108 | ret = dev_read_phandle_with_args(dev, "pwms", "#pwm-cells", 0, 0, &args); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | |
| 112 | ret = uclass_get_device_by_ofnode(UCLASS_PWM, args.node, &priv->pwm); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | priv->channel = args.args[0]; |
| 117 | priv->period = args.args[1]; |
| 118 | priv->active_low = dev_read_bool(dev, "active-low"); |
| 119 | |
| 120 | def_brightness = dev_read_u32_default(dev, "u-boot,default-brightness", 0); |
| 121 | max_brightness = dev_read_u32_default(dev, "max-brightness", 255); |
| 122 | priv->enabled = !!def_brightness; |
| 123 | |
| 124 | /* |
Nylon Chen | 3708739 | 2022-10-27 14:25:37 +0800 | [diff] [blame] | 125 | * No need to handle pwm inverted case (active_low) |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 126 | * because of pwm_set_invert function |
| 127 | */ |
| 128 | if (def_brightness < max_brightness) |
| 129 | priv->duty = priv->period * def_brightness / max_brightness; |
| 130 | else |
| 131 | priv->duty = priv->period; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int led_pwm_bind(struct udevice *parent) |
| 137 | { |
Rasmus Villemoes | d45e712 | 2023-11-17 12:38:10 +0100 | [diff] [blame] | 138 | return led_bind_generic(parent, LEDS_PWM_DRIVER_NAME); |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static const struct led_ops led_pwm_ops = { |
| 142 | .set_state = led_pwm_set_state, |
| 143 | .get_state = led_pwm_get_state, |
| 144 | }; |
| 145 | |
| 146 | static const struct udevice_id led_pwm_ids[] = { |
| 147 | { .compatible = "pwm-leds" }, |
| 148 | { } |
| 149 | }; |
| 150 | |
| 151 | U_BOOT_DRIVER(led_pwm) = { |
| 152 | .name = LEDS_PWM_DRIVER_NAME, |
| 153 | .id = UCLASS_LED, |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 154 | .ops = &led_pwm_ops, |
| 155 | .priv_auto = sizeof(struct led_pwm_priv), |
Ivan Vozvakhov | 067cfc1 | 2022-03-12 13:03:14 +0300 | [diff] [blame] | 156 | .probe = led_pwm_probe, |
| 157 | .of_to_plat = led_pwm_of_to_plat, |
| 158 | }; |
Stefan Herbrechtsmeier | e3812b5 | 2022-06-23 11:25:29 +0200 | [diff] [blame] | 159 | |
| 160 | U_BOOT_DRIVER(led_pwm_wrap) = { |
| 161 | .name = LEDS_PWM_DRIVER_NAME "_wrap", |
| 162 | .id = UCLASS_NOP, |
| 163 | .of_match = led_pwm_ids, |
| 164 | .bind = led_pwm_bind, |
| 165 | }; |