Álvaro Fernández Rojas | 28300dc | 2017-05-07 20:10:24 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <led.h> |
| 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 | |
| 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
| 43 | struct bcm6328_led_priv { |
| 44 | void __iomem *regs; |
| 45 | void __iomem *mode; |
| 46 | uint8_t shift; |
| 47 | bool active_low; |
| 48 | }; |
| 49 | |
| 50 | static unsigned long bcm6328_led_get_mode(struct bcm6328_led_priv *priv) |
| 51 | { |
| 52 | return ((readl_be(priv->mode) >> priv->shift) & LED_MODE_MASK); |
| 53 | } |
| 54 | |
| 55 | static int bcm6328_led_set_mode(struct bcm6328_led_priv *priv, uint8_t mode) |
| 56 | { |
| 57 | clrsetbits_be32(priv->mode, (LED_MODE_MASK << priv->shift), |
| 58 | (mode << priv->shift)); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static enum led_state_t bcm6328_led_get_state(struct udevice *dev) |
| 64 | { |
| 65 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 66 | enum led_state_t state = LEDST_OFF; |
| 67 | |
| 68 | switch (bcm6328_led_get_mode(priv)) { |
| 69 | #ifdef CONFIG_LED_BLINK |
| 70 | case LED_MODE_BLINK: |
| 71 | case LED_MODE_FAST: |
| 72 | state = LEDST_BLINK; |
| 73 | break; |
| 74 | #endif |
| 75 | case LED_MODE_OFF: |
| 76 | state = (priv->active_low ? LEDST_ON : LEDST_OFF); |
| 77 | break; |
| 78 | case LED_MODE_ON: |
| 79 | state = (priv->active_low ? LEDST_OFF : LEDST_ON); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return state; |
| 84 | } |
| 85 | |
| 86 | static int bcm6328_led_set_state(struct udevice *dev, enum led_state_t state) |
| 87 | { |
| 88 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 89 | unsigned long mode; |
| 90 | |
| 91 | switch (state) { |
| 92 | #ifdef CONFIG_LED_BLINK |
| 93 | case LEDST_BLINK: |
| 94 | mode = LED_MODE_BLINK; |
| 95 | break; |
| 96 | #endif |
| 97 | case LEDST_OFF: |
| 98 | mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF); |
| 99 | break; |
| 100 | case LEDST_ON: |
| 101 | mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON); |
| 102 | break; |
| 103 | case LEDST_TOGGLE: |
| 104 | if (bcm6328_led_get_state(dev) == LEDST_OFF) |
| 105 | return bcm6328_led_set_state(dev, LEDST_ON); |
| 106 | else |
| 107 | return bcm6328_led_set_state(dev, LEDST_OFF); |
| 108 | break; |
| 109 | default: |
| 110 | return -ENOSYS; |
| 111 | } |
| 112 | |
| 113 | return bcm6328_led_set_mode(priv, mode); |
| 114 | } |
| 115 | |
| 116 | #ifdef CONFIG_LED_BLINK |
| 117 | static unsigned long bcm6328_blink_delay(int delay) |
| 118 | { |
| 119 | unsigned long bcm6328_delay = delay; |
| 120 | |
| 121 | bcm6328_delay += (LED_INIT_FASTINTV_MS / 2); |
| 122 | bcm6328_delay /= LED_INIT_FASTINTV_MS; |
| 123 | bcm6328_delay <<= LED_INIT_FASTINTV_SHIFT; |
| 124 | |
| 125 | if (bcm6328_delay > LED_INIT_FASTINTV_MASK) |
| 126 | return LED_INIT_FASTINTV_MASK; |
| 127 | else |
| 128 | return bcm6328_delay; |
| 129 | } |
| 130 | |
| 131 | static int bcm6328_led_set_period(struct udevice *dev, int period_ms) |
| 132 | { |
| 133 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 134 | |
| 135 | clrsetbits_be32(priv->regs + LED_INIT_REG, LED_INIT_FASTINTV_MASK, |
| 136 | bcm6328_blink_delay(period_ms)); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | static const struct led_ops bcm6328_led_ops = { |
| 143 | .get_state = bcm6328_led_get_state, |
| 144 | .set_state = bcm6328_led_set_state, |
| 145 | #ifdef CONFIG_LED_BLINK |
| 146 | .set_period = bcm6328_led_set_period, |
| 147 | #endif |
| 148 | }; |
| 149 | |
| 150 | static int bcm6328_led_probe(struct udevice *dev) |
| 151 | { |
| 152 | struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev); |
| 153 | fdt_addr_t addr; |
| 154 | fdt_size_t size; |
| 155 | |
| 156 | /* Top-level LED node */ |
| 157 | if (!uc_plat->label) { |
| 158 | void __iomem *regs; |
| 159 | u32 set_bits = 0; |
| 160 | |
| 161 | addr = dev_get_addr_size_index(dev, 0, &size); |
| 162 | if (addr == FDT_ADDR_T_NONE) |
| 163 | return -EINVAL; |
| 164 | |
| 165 | regs = ioremap(addr, size); |
| 166 | |
| 167 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 168 | "brcm,serial-leds")) |
| 169 | set_bits |= LED_INIT_SLEDEN_MASK; |
| 170 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 171 | "brcm,serial-mux")) |
| 172 | set_bits |= LED_INIT_SLEDMUX_MASK; |
| 173 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 174 | "brcm,serial-clk-low")) |
| 175 | set_bits |= LED_INIT_SLEDCLKNPOL_MASK; |
| 176 | if (!fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 177 | "brcm,serial-dat-low")) |
| 178 | set_bits |= LED_INIT_SLEDDATANPOL_MASK; |
| 179 | if (!fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 180 | "brcm,serial-shift-inv")) |
| 181 | set_bits |= LED_INIT_SLEDSHIFTDIR_MASK; |
| 182 | |
| 183 | clrsetbits_be32(regs + LED_INIT_REG, ~0, set_bits); |
| 184 | } else { |
| 185 | struct bcm6328_led_priv *priv = dev_get_priv(dev); |
| 186 | unsigned int pin; |
| 187 | |
| 188 | addr = dev_get_addr_size_index(dev_get_parent(dev), 0, &size); |
| 189 | if (addr == FDT_ADDR_T_NONE) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | pin = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), "reg", |
| 193 | LEDS_MAX); |
| 194 | if (pin >= LEDS_MAX) |
| 195 | return -EINVAL; |
| 196 | |
| 197 | priv->regs = ioremap(addr, size); |
| 198 | if (pin < 8) { |
| 199 | /* LEDs 0-7 (bits 47:32) */ |
| 200 | priv->mode = priv->regs + LED_MODE_REG_HI; |
| 201 | priv->shift = (pin << 1); |
| 202 | } else { |
| 203 | /* LEDs 8-23 (bits 31:0) */ |
| 204 | priv->mode = priv->regs + LED_MODE_REG_LO; |
| 205 | priv->shift = ((pin - 8) << 1); |
| 206 | } |
| 207 | |
| 208 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
| 209 | "active-low")) |
| 210 | priv->active_low = true; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int bcm6328_led_bind(struct udevice *parent) |
| 217 | { |
| 218 | const void *blob = gd->fdt_blob; |
| 219 | int node; |
| 220 | |
| 221 | for (node = fdt_first_subnode(blob, dev_of_offset(parent)); |
| 222 | node > 0; |
| 223 | node = fdt_next_subnode(blob, node)) { |
| 224 | struct led_uc_plat *uc_plat; |
| 225 | struct udevice *dev; |
| 226 | const char *label; |
| 227 | int ret; |
| 228 | |
| 229 | label = fdt_getprop(blob, node, "label", NULL); |
| 230 | if (!label) { |
| 231 | debug("%s: node %s has no label\n", __func__, |
| 232 | fdt_get_name(blob, node, NULL)); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | ret = device_bind_driver_to_node(parent, "bcm6328-led", |
| 237 | fdt_get_name(blob, node, NULL), |
| 238 | node, &dev); |
| 239 | if (ret) |
| 240 | return ret; |
| 241 | |
| 242 | uc_plat = dev_get_uclass_platdata(dev); |
| 243 | uc_plat->label = label; |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static const struct udevice_id bcm6328_led_ids[] = { |
| 250 | { .compatible = "brcm,bcm6328-leds" }, |
| 251 | { /* sentinel */ } |
| 252 | }; |
| 253 | |
| 254 | U_BOOT_DRIVER(bcm6328_led) = { |
| 255 | .name = "bcm6328-led", |
| 256 | .id = UCLASS_LED, |
| 257 | .of_match = bcm6328_led_ids, |
| 258 | .ops = &bcm6328_led_ops, |
| 259 | .bind = bcm6328_led_bind, |
| 260 | .probe = bcm6328_led_probe, |
| 261 | .priv_auto_alloc_size = sizeof(struct bcm6328_led_priv), |
| 262 | }; |