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