Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com> |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <led.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <dm/lists.h> |
| 13 | |
| 14 | #define LEDS_MAX 24 |
| 15 | |
| 16 | /* LED Init register */ |
| 17 | #define LED_INIT_REG 0x00 |
| 18 | #define LED_INIT_FASTINTV_MS 20 |
| 19 | #define LED_INIT_FASTINTV_SHIFT 6 |
| 20 | #define LED_INIT_FASTINTV_MASK (0x3f << LED_INIT_FASTINTV_SHIFT) |
| 21 | #define LED_INIT_SLEDEN_SHIFT 12 |
| 22 | #define LED_INIT_SLEDEN_MASK (1 << LED_INIT_SLEDEN_SHIFT) |
| 23 | #define LED_INIT_SLEDMUX_SHIFT 13 |
| 24 | #define LED_INIT_SLEDMUX_MASK (1 << LED_INIT_SLEDMUX_SHIFT) |
| 25 | #define LED_INIT_SLEDCLKNPOL_SHIFT 14 |
| 26 | #define LED_INIT_SLEDCLKNPOL_MASK (1 << LED_INIT_SLEDCLKNPOL_SHIFT) |
| 27 | #define LED_INIT_SLEDDATAPPOL_SHIFT 15 |
| 28 | #define LED_INIT_SLEDDATANPOL_MASK (1 << LED_INIT_SLEDDATAPPOL_SHIFT) |
| 29 | #define LED_INIT_SLEDSHIFTDIR_SHIFT 16 |
| 30 | #define LED_INIT_SLEDSHIFTDIR_MASK (1 << LED_INIT_SLEDSHIFTDIR_SHIFT) |
| 31 | |
| 32 | /* LED Mode registers */ |
| 33 | #define LED_MODE_REG_HI 0x04 |
| 34 | #define LED_MODE_REG_LO 0x08 |
| 35 | #define LED_MODE_ON 0 |
| 36 | #define LED_MODE_FAST 1 |
| 37 | #define LED_MODE_BLINK 2 |
| 38 | #define LED_MODE_OFF 3 |
| 39 | #define LED_MODE_MASK 0x3 |
| 40 | |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 41 | struct bcm6328_led_priv { |
| 42 | void __iomem *regs; |
| 43 | void __iomem *mode; |
| 44 | uint8_t shift; |
| 45 | bool active_low; |
| 46 | }; |
| 47 | |
| 48 | static unsigned long bcm6328_led_get_mode(struct bcm6328_led_priv *priv) |
| 49 | { |
| 50 | return ((readl_be(priv->mode) >> priv->shift) & LED_MODE_MASK); |
| 51 | } |
| 52 | |
| 53 | static int bcm6328_led_set_mode(struct bcm6328_led_priv *priv, uint8_t mode) |
| 54 | { |
| 55 | clrsetbits_be32(priv->mode, (LED_MODE_MASK << priv->shift), |
| 56 | (mode << priv->shift)); |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static enum led_state_t bcm6328_led_get_state(struct udevice *dev) |
| 62 | { |
| 63 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 64 | enum led_state_t state = LEDST_OFF; |
| 65 | |
| 66 | switch (bcm6328_led_get_mode(priv)) { |
| 67 | #ifdef CONFIG_LED_BLINK |
| 68 | case LED_MODE_BLINK: |
| 69 | case LED_MODE_FAST: |
| 70 | state = LEDST_BLINK; |
| 71 | break; |
| 72 | #endif |
| 73 | case LED_MODE_OFF: |
| 74 | state = (priv->active_low ? LEDST_ON : LEDST_OFF); |
| 75 | break; |
| 76 | case LED_MODE_ON: |
| 77 | state = (priv->active_low ? LEDST_OFF : LEDST_ON); |
| 78 | break; |
| 79 | } |
| 80 | |
| 81 | return state; |
| 82 | } |
| 83 | |
| 84 | static int bcm6328_led_set_state(struct udevice *dev, enum led_state_t state) |
| 85 | { |
| 86 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 87 | unsigned long mode; |
| 88 | |
| 89 | switch (state) { |
| 90 | #ifdef CONFIG_LED_BLINK |
| 91 | case LEDST_BLINK: |
| 92 | mode = LED_MODE_BLINK; |
| 93 | break; |
| 94 | #endif |
| 95 | case LEDST_OFF: |
| 96 | mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF); |
| 97 | break; |
| 98 | case LEDST_ON: |
| 99 | mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON); |
| 100 | break; |
| 101 | case LEDST_TOGGLE: |
| 102 | if (bcm6328_led_get_state(dev) == LEDST_OFF) |
| 103 | return bcm6328_led_set_state(dev, LEDST_ON); |
| 104 | else |
| 105 | return bcm6328_led_set_state(dev, LEDST_OFF); |
| 106 | break; |
| 107 | default: |
| 108 | return -ENOSYS; |
| 109 | } |
| 110 | |
| 111 | return bcm6328_led_set_mode(priv, mode); |
| 112 | } |
| 113 | |
| 114 | #ifdef CONFIG_LED_BLINK |
| 115 | static unsigned long bcm6328_blink_delay(int delay) |
| 116 | { |
| 117 | unsigned long bcm6328_delay = delay; |
| 118 | |
| 119 | bcm6328_delay += (LED_INIT_FASTINTV_MS / 2); |
| 120 | bcm6328_delay /= LED_INIT_FASTINTV_MS; |
| 121 | bcm6328_delay <<= LED_INIT_FASTINTV_SHIFT; |
| 122 | |
| 123 | if (bcm6328_delay > LED_INIT_FASTINTV_MASK) |
| 124 | return LED_INIT_FASTINTV_MASK; |
| 125 | else |
| 126 | return bcm6328_delay; |
| 127 | } |
| 128 | |
| 129 | static int bcm6328_led_set_period(struct udevice *dev, int period_ms) |
| 130 | { |
| 131 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 132 | |
| 133 | clrsetbits_be32(priv->regs + LED_INIT_REG, LED_INIT_FASTINTV_MASK, |
| 134 | bcm6328_blink_delay(period_ms)); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | static const struct led_ops bcm6328_led_ops = { |
| 141 | .get_state = bcm6328_led_get_state, |
| 142 | .set_state = bcm6328_led_set_state, |
| 143 | #ifdef CONFIG_LED_BLINK |
| 144 | .set_period = bcm6328_led_set_period, |
| 145 | #endif |
| 146 | }; |
| 147 | |
| 148 | static int bcm6328_led_probe(struct udevice *dev) |
| 149 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 150 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 151 | |
| 152 | /* Top-level LED node */ |
| 153 | if (!uc_plat->label) { |
| 154 | void __iomem *regs; |
| 155 | u32 set_bits = 0; |
| 156 | |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 157 | regs = dev_remap_addr(dev); |
| 158 | if (!regs) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 159 | return -EINVAL; |
| 160 | |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 161 | if (dev_read_bool(dev, "brcm,serial-leds")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 162 | set_bits |= LED_INIT_SLEDEN_MASK; |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 163 | if (dev_read_bool(dev, "brcm,serial-mux")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 164 | set_bits |= LED_INIT_SLEDMUX_MASK; |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 165 | if (dev_read_bool(dev, "brcm,serial-clk-low")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 166 | set_bits |= LED_INIT_SLEDCLKNPOL_MASK; |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 167 | if (!dev_read_bool(dev, "brcm,serial-dat-low")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 168 | set_bits |= LED_INIT_SLEDDATANPOL_MASK; |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 169 | if (!dev_read_bool(dev, "brcm,serial-shift-inv")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 170 | set_bits |= LED_INIT_SLEDSHIFTDIR_MASK; |
| 171 | |
| 172 | clrsetbits_be32(regs + LED_INIT_REG, ~0, set_bits); |
| 173 | } else { |
| 174 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 175 | unsigned int pin; |
| 176 | |
Philippe Reynes | 19bfd3a | 2018-06-28 15:26:16 +0200 | [diff] [blame] | 177 | priv->regs = dev_remap_addr(dev_get_parent(dev)); |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 178 | if (!priv->regs) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 181 | pin = dev_read_u32_default(dev, "reg", LEDS_MAX); |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 182 | if (pin >= LEDS_MAX) |
| 183 | return -EINVAL; |
| 184 | |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 185 | if (pin < 8) { |
| 186 | /* LEDs 0-7 (bits 47:32) */ |
| 187 | priv->mode = priv->regs + LED_MODE_REG_HI; |
| 188 | priv->shift = (pin << 1); |
| 189 | } else { |
| 190 | /* LEDs 8-23 (bits 31:0) */ |
| 191 | priv->mode = priv->regs + LED_MODE_REG_LO; |
| 192 | priv->shift = ((pin - 8) << 1); |
| 193 | } |
| 194 | |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 195 | if (dev_read_bool(dev, "active-low")) |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 196 | priv->active_low = true; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | static int bcm6328_led_bind(struct udevice *parent) |
| 203 | { |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 204 | ofnode node; |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 205 | |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 206 | dev_for_each_subnode(node, parent) { |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 207 | struct udevice *dev; |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 208 | int ret; |
| 209 | |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 210 | ret = device_bind_driver_to_node(parent, "bcm6328-led", |
Álvaro Fernández Rojas | 0ec78af | 2018-03-22 19:39:34 +0100 | [diff] [blame] | 211 | ofnode_get_name(node), |
| 212 | node, &dev); |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 213 | if (ret) |
| 214 | return ret; |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static const struct udevice_id bcm6328_led_ids[] = { |
| 221 | { .compatible = "brcm,bcm6328-leds" }, |
| 222 | { /* sentinel */ } |
| 223 | }; |
| 224 | |
| 225 | U_BOOT_DRIVER(bcm6328_led) = { |
| 226 | .name = "bcm6328-led", |
| 227 | .id = UCLASS_LED, |
| 228 | .of_match = bcm6328_led_ids, |
| 229 | .ops = &bcm6328_led_ops, |
| 230 | .bind = bcm6328_led_bind, |
| 231 | .probe = bcm6328_led_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 232 | .priv_auto = sizeof(struct bcm6328_led_priv), |
Álvaro Fernández Rojas | c7c859c | 2017-05-07 20:10:24 +0200 | [diff] [blame] | 233 | }; |