Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2019 Philippe Reynes <philippe.reynes@softathome.com> |
| 4 | * |
| 5 | * based on: |
| 6 | * drivers/led/led_bcm6328.c |
| 7 | * drivers/led/led_bcm6358.c |
| 8 | */ |
| 9 | |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <led.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | #include <dm/lists.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 17 | |
| 18 | #define LEDS_MAX 32 |
| 19 | #define LEDS_WAIT 100 |
Paul HENRYS | 877de2a | 2022-03-17 10:29:47 +0100 | [diff] [blame] | 20 | #define LEDS_MAX_BRIGHTNESS 7 |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 21 | |
| 22 | /* LED Mode register */ |
| 23 | #define LED_MODE_REG 0x0 |
| 24 | #define LED_MODE_OFF 0 |
| 25 | #define LED_MODE_ON 1 |
| 26 | #define LED_MODE_MASK 1 |
| 27 | |
| 28 | /* LED Controller Global settings register */ |
| 29 | #define LED_CTRL_REG 0x00 |
| 30 | #define LED_CTRL_MASK 0x1f |
| 31 | #define LED_CTRL_LED_TEST_MODE BIT(0) |
| 32 | #define LED_CTRL_SERIAL_LED_DATA_PPOL BIT(1) |
| 33 | #define LED_CTRL_SERIAL_LED_CLK_POL BIT(2) |
| 34 | #define LED_CTRL_SERIAL_LED_EN_POL BIT(3) |
| 35 | #define LED_CTRL_SERIAL_LED_MSB_FIRST BIT(4) |
| 36 | |
| 37 | /* LED Controller IP LED source select register */ |
| 38 | #define LED_HW_LED_EN_REG 0x08 |
| 39 | /* LED Flash control register0 */ |
| 40 | #define LED_FLASH_RATE_CONTROL_REG0 0x10 |
Paul HENRYS | 877de2a | 2022-03-17 10:29:47 +0100 | [diff] [blame] | 41 | /* LED Brightness control register0 */ |
| 42 | #define LED_BRIGHTNESS_CONTROL_REG0 0x20 |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 43 | /* Soft LED input register */ |
| 44 | #define LED_SW_LED_IP_REG 0xb8 |
Steven Lawrance | e040725 | 2020-10-29 18:27:34 +0100 | [diff] [blame] | 45 | /* Parallel LED Output Polarity Register */ |
| 46 | #define LED_PLED_OP_PPOL_REG 0xc0 |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 47 | |
| 48 | struct bcm6858_led_priv { |
| 49 | void __iomem *regs; |
| 50 | u8 pin; |
| 51 | }; |
| 52 | |
| 53 | #ifdef CONFIG_LED_BLINK |
| 54 | /* |
| 55 | * The value for flash rate are: |
| 56 | * 0 : no blinking |
| 57 | * 1 : rate is 25 Hz => 40 ms (period) |
| 58 | * 2 : rate is 12.5 Hz => 80 ms (period) |
| 59 | * 3 : rate is 6.25 Hz => 160 ms (period) |
| 60 | * 4 : rate is 3.125 Hz => 320 ms (period) |
| 61 | * 5 : rate is 1.5625 Hz => 640 ms (period) |
| 62 | * 6 : rate is 0.7815 Hz => 1280 ms (period) |
| 63 | * 7 : rate is 0.390625 Hz => 2560 ms (period) |
| 64 | */ |
| 65 | static const int bcm6858_flash_rate[8] = { |
| 66 | 0, 40, 80, 160, 320, 640, 1280, 2560 |
| 67 | }; |
| 68 | |
| 69 | static u32 bcm6858_flash_rate_value(int period_ms) |
| 70 | { |
| 71 | unsigned long value = 7; |
| 72 | int i; |
| 73 | |
| 74 | for (i = 0; i < ARRAY_SIZE(bcm6858_flash_rate); i++) { |
| 75 | if (period_ms <= bcm6858_flash_rate[i]) { |
| 76 | value = i; |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return value; |
| 82 | } |
| 83 | |
| 84 | static int bcm6858_led_set_period(struct udevice *dev, int period_ms) |
| 85 | { |
| 86 | struct bcm6858_led_priv *priv = dev_get_priv(dev); |
| 87 | u32 offset, shift, mask, value; |
| 88 | |
| 89 | offset = (priv->pin / 8) * 4; |
| 90 | shift = (priv->pin % 8) * 4; |
| 91 | mask = 0x7 << shift; |
| 92 | value = bcm6858_flash_rate_value(period_ms) << shift; |
| 93 | |
| 94 | clrbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, mask); |
| 95 | setbits_32(priv->regs + LED_FLASH_RATE_CONTROL_REG0 + offset, value); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | #endif |
| 100 | |
Paul HENRYS | 877de2a | 2022-03-17 10:29:47 +0100 | [diff] [blame] | 101 | static int led_set_brightness(struct udevice *dev, unsigned int brightness) |
| 102 | { |
| 103 | struct bcm6858_led_priv *priv = dev_get_priv(dev); |
| 104 | u32 offset, shift, mask, value; |
| 105 | |
| 106 | offset = (priv->pin / 8) * 4; |
| 107 | shift = (priv->pin % 8) * 4; |
| 108 | mask = 0xf << shift; |
| 109 | |
| 110 | /* 8 levels of brightness achieved through PWM */ |
| 111 | value = (brightness > LEDS_MAX_BRIGHTNESS ? |
| 112 | LEDS_MAX_BRIGHTNESS : brightness) << shift; |
| 113 | |
| 114 | debug("%s: %s brightness set to %u\n", __func__, dev->name, value >> shift); |
| 115 | |
| 116 | clrbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, mask); |
| 117 | setbits_32(priv->regs + LED_BRIGHTNESS_CONTROL_REG0 + offset, value); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 122 | static enum led_state_t bcm6858_led_get_state(struct udevice *dev) |
| 123 | { |
| 124 | struct bcm6858_led_priv *priv = dev_get_priv(dev); |
| 125 | enum led_state_t state = LEDST_OFF; |
| 126 | u32 sw_led_ip; |
| 127 | |
| 128 | sw_led_ip = readl(priv->regs + LED_SW_LED_IP_REG); |
| 129 | if (sw_led_ip & (1 << priv->pin)) |
| 130 | state = LEDST_ON; |
| 131 | |
| 132 | return state; |
| 133 | } |
| 134 | |
| 135 | static int bcm6858_led_set_state(struct udevice *dev, enum led_state_t state) |
| 136 | { |
| 137 | struct bcm6858_led_priv *priv = dev_get_priv(dev); |
| 138 | |
Paul HENRYS | 877de2a | 2022-03-17 10:29:47 +0100 | [diff] [blame] | 139 | debug("%s: Set led %s to %d\n", __func__, dev->name, state); |
| 140 | |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 141 | switch (state) { |
| 142 | case LEDST_OFF: |
| 143 | clrbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin)); |
| 144 | #ifdef CONFIG_LED_BLINK |
| 145 | bcm6858_led_set_period(dev, 0); |
| 146 | #endif |
| 147 | break; |
| 148 | case LEDST_ON: |
| 149 | setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin)); |
| 150 | #ifdef CONFIG_LED_BLINK |
| 151 | bcm6858_led_set_period(dev, 0); |
| 152 | #endif |
| 153 | break; |
| 154 | case LEDST_TOGGLE: |
| 155 | if (bcm6858_led_get_state(dev) == LEDST_OFF) |
| 156 | return bcm6858_led_set_state(dev, LEDST_ON); |
| 157 | else |
| 158 | return bcm6858_led_set_state(dev, LEDST_OFF); |
| 159 | break; |
| 160 | #ifdef CONFIG_LED_BLINK |
| 161 | case LEDST_BLINK: |
| 162 | setbits_32(priv->regs + LED_SW_LED_IP_REG, (1 << priv->pin)); |
| 163 | break; |
| 164 | #endif |
| 165 | default: |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static const struct led_ops bcm6858_led_ops = { |
| 173 | .get_state = bcm6858_led_get_state, |
| 174 | .set_state = bcm6858_led_set_state, |
| 175 | #ifdef CONFIG_LED_BLINK |
| 176 | .set_period = bcm6858_led_set_period, |
| 177 | #endif |
| 178 | }; |
| 179 | |
| 180 | static int bcm6858_led_probe(struct udevice *dev) |
| 181 | { |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 182 | struct bcm6858_led_priv *priv = dev_get_priv(dev); |
| 183 | void __iomem *regs; |
| 184 | unsigned int pin, brightness; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 185 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 186 | regs = dev_remap_addr(dev_get_parent(dev)); |
| 187 | if (!regs) |
| 188 | return -EINVAL; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 189 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 190 | pin = dev_read_u32_default(dev, "reg", LEDS_MAX); |
| 191 | if (pin >= LEDS_MAX) |
| 192 | return -EINVAL; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 193 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 194 | priv->regs = regs; |
| 195 | priv->pin = pin; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 196 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 197 | /* this led is managed by software */ |
| 198 | clrbits_32(regs + LED_HW_LED_EN_REG, 1 << pin); |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 199 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 200 | /* configure the polarity */ |
| 201 | if (dev_read_bool(dev, "active-low")) |
| 202 | clrbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin); |
| 203 | else |
| 204 | setbits_32(regs + LED_PLED_OP_PPOL_REG, 1 << pin); |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 205 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 206 | brightness = dev_read_u32_default(dev, "default-brightness", |
Paul HENRYS | 877de2a | 2022-03-17 10:29:47 +0100 | [diff] [blame] | 207 | LEDS_MAX_BRIGHTNESS); |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 208 | led_set_brightness(dev, brightness); |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 213 | U_BOOT_DRIVER(bcm6858_led) = { |
| 214 | .name = "bcm6858-led", |
| 215 | .id = UCLASS_LED, |
| 216 | .probe = bcm6858_led_probe, |
| 217 | .priv_auto = sizeof(struct bcm6858_led_priv), |
| 218 | .ops = &bcm6858_led_ops, |
| 219 | }; |
| 220 | |
| 221 | static int bcm6858_led_wrap_probe(struct udevice *dev) |
| 222 | { |
| 223 | void __iomem *regs; |
| 224 | u32 set_bits = 0; |
| 225 | |
| 226 | regs = dev_remap_addr(dev); |
| 227 | if (!regs) |
| 228 | return -EINVAL; |
| 229 | |
| 230 | if (dev_read_bool(dev, "brcm,serial-led-msb-first")) |
| 231 | set_bits |= LED_CTRL_SERIAL_LED_MSB_FIRST; |
| 232 | if (dev_read_bool(dev, "brcm,serial-led-en-pol")) |
| 233 | set_bits |= LED_CTRL_SERIAL_LED_EN_POL; |
| 234 | if (dev_read_bool(dev, "brcm,serial-led-clk-pol")) |
| 235 | set_bits |= LED_CTRL_SERIAL_LED_CLK_POL; |
| 236 | if (dev_read_bool(dev, "brcm,serial-led-data-ppol")) |
| 237 | set_bits |= LED_CTRL_SERIAL_LED_DATA_PPOL; |
| 238 | if (dev_read_bool(dev, "brcm,led-test-mode")) |
| 239 | set_bits |= LED_CTRL_LED_TEST_MODE; |
| 240 | |
| 241 | clrsetbits_32(regs + LED_CTRL_REG, ~0, set_bits); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int bcm6858_led_wrap_bind(struct udevice *parent) |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 247 | { |
| 248 | ofnode node; |
| 249 | |
| 250 | dev_for_each_subnode(node, parent) { |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 251 | struct udevice *dev; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 252 | int ret; |
| 253 | |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 254 | ret = device_bind_driver_to_node(parent, "bcm6858-led", |
| 255 | ofnode_get_name(node), |
| 256 | node, &dev); |
| 257 | if (ret) |
| 258 | return ret; |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static const struct udevice_id bcm6858_led_ids[] = { |
| 265 | { .compatible = "brcm,bcm6858-leds" }, |
| 266 | { /* sentinel */ } |
| 267 | }; |
| 268 | |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 269 | U_BOOT_DRIVER(bcm6858_led_wrap) = { |
| 270 | .name = "bcm6858_led_wrap", |
| 271 | .id = UCLASS_NOP, |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 272 | .of_match = bcm6858_led_ids, |
Philippe Reynes | 8fd6e64 | 2023-06-23 18:36:42 +0200 | [diff] [blame] | 273 | .probe = bcm6858_led_wrap_probe, |
| 274 | .bind = bcm6858_led_wrap_bind, |
Philippe Reynes | d00c6a2 | 2019-03-22 17:02:01 +0100 | [diff] [blame] | 275 | }; |