Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2022 Philippe Reynes <philippe.reynes@softathome.com> |
| 4 | * |
| 5 | * based on: |
| 6 | * drivers/led/led_bcm6858.c |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <errno.h> |
| 12 | #include <led.h> |
| 13 | #include <log.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <dm/lists.h> |
| 16 | #include <linux/bitops.h> |
| 17 | |
| 18 | #define LEDS_MAX 32 |
| 19 | #define LEDS_WAIT 100 |
| 20 | |
| 21 | /* LED Mode register */ |
| 22 | #define LED_MODE_REG 0x0 |
| 23 | #define LED_MODE_OFF 0 |
| 24 | #define LED_MODE_ON 1 |
| 25 | #define LED_MODE_MASK 1 |
| 26 | |
| 27 | /* LED Controller Global settings register */ |
| 28 | #define CLED_CTRL_REG 0x00 |
| 29 | #define CLED_CTRL_SERIAL_LED_DATA_PPOL BIT(1) |
| 30 | #define CLED_CTRL_SERIAL_LED_CLK_POL BIT(2) |
| 31 | #define CLED_CTRL_SERIAL_LED_EN_POL BIT(3) |
| 32 | #define CLED_CTRL_SERIAL_LED_MSB_FIRST BIT(4) |
| 33 | #define CLED_CTRL_MASK 0x1E |
| 34 | /* LED Controller IP LED source select register */ |
| 35 | #define CLED_HW_LED_EN_REG 0x04 |
| 36 | /* Hardware LED Polarity register */ |
| 37 | #define CLED_HW_LED_IP_PPOL_REG 0x0c |
| 38 | /* Soft LED Set Register */ |
| 39 | #define CLED_SW_LED_IP_SET_REG 0x10 |
| 40 | /* Parallel LED Output Polarity Register */ |
| 41 | #define CLED_PLED_OP_PPOL_REG 0x18 |
| 42 | /* LED Channel activate register */ |
| 43 | #define CLED_LED_CH_ACTIVATE_REG 0x1c |
| 44 | /* LED 0 Config 0 reg */ |
| 45 | #define CLED_LED_0_CONFIG_0 0x20 |
| 46 | /* Soft LED Clear Register */ |
| 47 | #define CLED_SW_LED_IP_CLEAR_REG 0x444 |
| 48 | /* Soft LED Status Register */ |
| 49 | #define CLED_SW_LED_IP_STATUS_REG 0x448 |
| 50 | |
| 51 | /* Size of all registers used for the config of one LED */ |
| 52 | #define CLED_CONFIG_SIZE (4 * sizeof(u32)) |
| 53 | |
| 54 | #define CLED_CONFIG0_MODE 0 |
| 55 | #define CLED_CONFIG0_MODE_MASK (BIT(0) | BIT(1)) |
| 56 | #define CLED_CONFIG0_MODE_STEADY 0 |
| 57 | #define CLED_CONFIG0_MODE_FADING 1 |
| 58 | #define CLED_CONFIG0_MODE_PULSATING 2 |
| 59 | |
| 60 | #define CLED_CONFIG0_FLASH_CTRL_SHIFT 3 |
| 61 | #define CLED_CONFIG0_FLASH_CTRL_MASK (BIT(3) | BIT(4) | BIT(5)) |
| 62 | |
| 63 | struct bcm6753_led_priv { |
| 64 | void __iomem *regs; |
| 65 | u8 pin; |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * The value for flash rate are: |
| 70 | * 0 : no blinking |
| 71 | * 1 : rate is 25 Hz => 40 ms (period) |
| 72 | * 2 : rate is 12.5 Hz => 80 ms (period) |
| 73 | * 3 : rate is 6.25 Hz => 160 ms (period) |
| 74 | * 4 : rate is 3.125 Hz => 320 ms (period) |
| 75 | * 5 : rate is 1.5625 Hz => 640 ms (period) |
| 76 | * 6 : rate is 0.7815 Hz => 1280 ms (period) |
| 77 | * 7 : rate is 0.390625 Hz => 2560 ms (period) |
| 78 | */ |
| 79 | static const int bcm6753_flash_rate[8] = { |
| 80 | 0, 40, 80, 160, 320, 640, 1280, 2560 |
| 81 | }; |
| 82 | |
| 83 | static u32 bcm6753_flash_rate_value(int period_ms) |
| 84 | { |
| 85 | unsigned long value = 7; |
| 86 | int i; |
| 87 | |
| 88 | for (i = 0; i < ARRAY_SIZE(bcm6753_flash_rate); i++) { |
| 89 | if (period_ms <= bcm6753_flash_rate[i]) { |
| 90 | value = i; |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return value; |
| 96 | } |
| 97 | |
| 98 | static int bcm6753_led_set_period(struct udevice *dev, int period_ms) |
| 99 | { |
| 100 | struct bcm6753_led_priv *priv = dev_get_priv(dev); |
| 101 | u32 offset, shift, value; |
| 102 | |
| 103 | offset = CLED_LED_0_CONFIG_0 + (CLED_CONFIG_SIZE * priv->pin); |
| 104 | value = bcm6753_flash_rate_value(period_ms); |
| 105 | shift = CLED_CONFIG0_FLASH_CTRL_SHIFT; |
| 106 | |
| 107 | /* set mode steady */ |
| 108 | clrbits_32(priv->regs + offset, CLED_CONFIG0_MODE_MASK); |
| 109 | setbits_32(priv->regs + offset, CLED_CONFIG0_MODE_STEADY); |
| 110 | |
| 111 | /* set flash rate */ |
| 112 | clrbits_32(priv->regs + offset, CLED_CONFIG0_FLASH_CTRL_MASK); |
| 113 | setbits_32(priv->regs + offset, value << shift); |
| 114 | |
| 115 | /* enable config */ |
| 116 | setbits_32(priv->regs + CLED_LED_CH_ACTIVATE_REG, 1 << priv->pin); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | static enum led_state_t bcm6753_led_get_state(struct udevice *dev) |
| 122 | { |
| 123 | struct bcm6753_led_priv *priv = dev_get_priv(dev); |
| 124 | enum led_state_t state = LEDST_OFF; |
| 125 | u32 sw_led_ip_status; |
| 126 | |
| 127 | sw_led_ip_status = readl(priv->regs + CLED_SW_LED_IP_STATUS_REG); |
| 128 | if (sw_led_ip_status & (1 << priv->pin)) |
| 129 | state = LEDST_ON; |
| 130 | |
| 131 | return state; |
| 132 | } |
| 133 | |
| 134 | static int bcm6753_led_set_state(struct udevice *dev, enum led_state_t state) |
| 135 | { |
| 136 | struct bcm6753_led_priv *priv = dev_get_priv(dev); |
| 137 | |
| 138 | switch (state) { |
| 139 | case LEDST_OFF: |
| 140 | setbits_32(priv->regs + CLED_SW_LED_IP_CLEAR_REG, (1 << priv->pin)); |
| 141 | if (IS_ENABLED(CONFIG_LED_BLINK)) |
| 142 | bcm6753_led_set_period(dev, 0); |
| 143 | break; |
| 144 | case LEDST_ON: |
| 145 | setbits_32(priv->regs + CLED_SW_LED_IP_SET_REG, (1 << priv->pin)); |
| 146 | if (IS_ENABLED(CONFIG_LED_BLINK)) |
| 147 | bcm6753_led_set_period(dev, 0); |
| 148 | break; |
| 149 | case LEDST_TOGGLE: |
| 150 | if (bcm6753_led_get_state(dev) == LEDST_OFF) |
| 151 | return bcm6753_led_set_state(dev, LEDST_ON); |
| 152 | else |
| 153 | return bcm6753_led_set_state(dev, LEDST_OFF); |
| 154 | break; |
| 155 | #ifdef CONFIG_LED_BLINK |
| 156 | case LEDST_BLINK: |
| 157 | setbits_32(priv->regs + CLED_SW_LED_IP_SET_REG, (1 << priv->pin)); |
| 158 | break; |
| 159 | #endif |
| 160 | default: |
| 161 | return -EINVAL; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static const struct led_ops bcm6753_led_ops = { |
| 168 | .get_state = bcm6753_led_get_state, |
| 169 | .set_state = bcm6753_led_set_state, |
| 170 | #ifdef CONFIG_LED_BLINK |
| 171 | .set_period = bcm6753_led_set_period, |
| 172 | #endif |
| 173 | }; |
| 174 | |
| 175 | static int bcm6753_led_probe(struct udevice *dev) |
| 176 | { |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 177 | struct bcm6753_led_priv *priv = dev_get_priv(dev); |
| 178 | void __iomem *regs; |
| 179 | unsigned int pin; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 180 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 181 | regs = dev_remap_addr(dev_get_parent(dev)); |
| 182 | if (!regs) |
| 183 | return -EINVAL; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 184 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 185 | pin = dev_read_u32_default(dev, "reg", LEDS_MAX); |
| 186 | if (pin >= LEDS_MAX) |
| 187 | return -EINVAL; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 188 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 189 | priv->regs = regs; |
| 190 | priv->pin = pin; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 191 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 192 | /* this led is managed by software */ |
| 193 | clrbits_32(regs + CLED_HW_LED_EN_REG, 1 << pin); |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 194 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 195 | /* configure the polarity */ |
| 196 | if (dev_read_bool(dev, "active-low")) |
| 197 | clrbits_32(regs + CLED_PLED_OP_PPOL_REG, 1 << pin); |
| 198 | else |
| 199 | setbits_32(regs + CLED_PLED_OP_PPOL_REG, 1 << pin); |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 204 | U_BOOT_DRIVER(bcm6753_led) = { |
| 205 | .name = "bcm6753-led", |
| 206 | .id = UCLASS_LED, |
| 207 | .probe = bcm6753_led_probe, |
| 208 | .priv_auto = sizeof(struct bcm6753_led_priv), |
| 209 | .ops = &bcm6753_led_ops, |
| 210 | }; |
| 211 | |
| 212 | static int bcm6753_led_wrap_probe(struct udevice *dev) |
| 213 | { |
| 214 | void __iomem *regs; |
| 215 | u32 set_bits = 0; |
| 216 | |
| 217 | regs = dev_remap_addr(dev); |
| 218 | if (!regs) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | if (dev_read_bool(dev, "brcm,serial-led-msb-first")) |
| 222 | set_bits |= CLED_CTRL_SERIAL_LED_MSB_FIRST; |
| 223 | if (dev_read_bool(dev, "brcm,serial-led-en-pol")) |
| 224 | set_bits |= CLED_CTRL_SERIAL_LED_EN_POL; |
| 225 | if (dev_read_bool(dev, "brcm,serial-led-clk-pol")) |
| 226 | set_bits |= CLED_CTRL_SERIAL_LED_CLK_POL; |
| 227 | if (dev_read_bool(dev, "brcm,serial-led-data-ppol")) |
| 228 | set_bits |= CLED_CTRL_SERIAL_LED_DATA_PPOL; |
| 229 | |
| 230 | clrsetbits_32(regs + CLED_CTRL_REG, CLED_CTRL_MASK, set_bits); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int bcm6753_led_wrap_bind(struct udevice *parent) |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 236 | { |
| 237 | ofnode node; |
| 238 | |
| 239 | dev_for_each_subnode(node, parent) { |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 240 | struct udevice *dev; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 241 | int ret; |
| 242 | |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 243 | ret = device_bind_driver_to_node(parent, "bcm6753-led", |
| 244 | ofnode_get_name(node), |
| 245 | node, &dev); |
| 246 | if (ret) |
| 247 | return ret; |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static const struct udevice_id bcm6753_led_ids[] = { |
| 254 | { .compatible = "brcm,bcm6753-leds" }, |
| 255 | { /* sentinel */ } |
| 256 | }; |
| 257 | |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 258 | U_BOOT_DRIVER(bcm6753_led_wrap) = { |
| 259 | .name = "bcm6753_led_wrap", |
| 260 | .id = UCLASS_NOP, |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 261 | .of_match = bcm6753_led_ids, |
Philippe Reynes | 910b01c | 2023-06-29 11:25:23 +0200 | [diff] [blame] | 262 | .probe = bcm6753_led_wrap_probe, |
| 263 | .bind = bcm6753_led_wrap_bind, |
Philippe Reynes | 0d2001b | 2022-02-17 17:17:04 +0100 | [diff] [blame] | 264 | }; |