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