2 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0+
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)
32 /* LED Mode registers */
33 #define LED_MODE_REG_HI 0x04
34 #define LED_MODE_REG_LO 0x08
36 #define LED_MODE_FAST 1
37 #define LED_MODE_BLINK 2
38 #define LED_MODE_OFF 3
39 #define LED_MODE_MASK 0x3
41 DECLARE_GLOBAL_DATA_PTR;
43 struct bcm6328_led_priv {
50 static unsigned long bcm6328_led_get_mode(struct bcm6328_led_priv *priv)
52 return ((readl_be(priv->mode) >> priv->shift) & LED_MODE_MASK);
55 static int bcm6328_led_set_mode(struct bcm6328_led_priv *priv, uint8_t mode)
57 clrsetbits_be32(priv->mode, (LED_MODE_MASK << priv->shift),
58 (mode << priv->shift));
63 static enum led_state_t bcm6328_led_get_state(struct udevice *dev)
65 struct bcm6328_led_priv *priv = dev_get_priv(dev);
66 enum led_state_t state = LEDST_OFF;
68 switch (bcm6328_led_get_mode(priv)) {
69 #ifdef CONFIG_LED_BLINK
76 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
79 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
86 static int bcm6328_led_set_state(struct udevice *dev, enum led_state_t state)
88 struct bcm6328_led_priv *priv = dev_get_priv(dev);
92 #ifdef CONFIG_LED_BLINK
94 mode = LED_MODE_BLINK;
98 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
101 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
104 if (bcm6328_led_get_state(dev) == LEDST_OFF)
105 return bcm6328_led_set_state(dev, LEDST_ON);
107 return bcm6328_led_set_state(dev, LEDST_OFF);
113 return bcm6328_led_set_mode(priv, mode);
116 #ifdef CONFIG_LED_BLINK
117 static unsigned long bcm6328_blink_delay(int delay)
119 unsigned long bcm6328_delay = delay;
121 bcm6328_delay += (LED_INIT_FASTINTV_MS / 2);
122 bcm6328_delay /= LED_INIT_FASTINTV_MS;
123 bcm6328_delay <<= LED_INIT_FASTINTV_SHIFT;
125 if (bcm6328_delay > LED_INIT_FASTINTV_MASK)
126 return LED_INIT_FASTINTV_MASK;
128 return bcm6328_delay;
131 static int bcm6328_led_set_period(struct udevice *dev, int period_ms)
133 struct bcm6328_led_priv *priv = dev_get_priv(dev);
135 clrsetbits_be32(priv->regs + LED_INIT_REG, LED_INIT_FASTINTV_MASK,
136 bcm6328_blink_delay(period_ms));
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,
150 static int bcm6328_led_probe(struct udevice *dev)
152 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
156 /* Top-level LED node */
157 if (!uc_plat->label) {
161 addr = dev_get_addr_size_index(dev, 0, &size);
162 if (addr == FDT_ADDR_T_NONE)
165 regs = ioremap(addr, size);
167 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
169 set_bits |= LED_INIT_SLEDEN_MASK;
170 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
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;
183 clrsetbits_be32(regs + LED_INIT_REG, ~0, set_bits);
185 struct bcm6328_led_priv *priv = dev_get_priv(dev);
188 addr = dev_get_addr_size_index(dev_get_parent(dev), 0, &size);
189 if (addr == FDT_ADDR_T_NONE)
192 pin = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(dev), "reg",
197 priv->regs = ioremap(addr, size);
199 /* LEDs 0-7 (bits 47:32) */
200 priv->mode = priv->regs + LED_MODE_REG_HI;
201 priv->shift = (pin << 1);
203 /* LEDs 8-23 (bits 31:0) */
204 priv->mode = priv->regs + LED_MODE_REG_LO;
205 priv->shift = ((pin - 8) << 1);
208 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
210 priv->active_low = true;
216 static int bcm6328_led_bind(struct udevice *parent)
218 const void *blob = gd->fdt_blob;
221 for (node = fdt_first_subnode(blob, dev_of_offset(parent));
223 node = fdt_next_subnode(blob, node)) {
224 struct led_uc_plat *uc_plat;
229 label = fdt_getprop(blob, node, "label", NULL);
231 debug("%s: node %s has no label\n", __func__,
232 fdt_get_name(blob, node, NULL));
236 ret = device_bind_driver_to_node(parent, "bcm6328-led",
237 fdt_get_name(blob, node, NULL),
242 uc_plat = dev_get_uclass_platdata(dev);
243 uc_plat->label = label;
249 static const struct udevice_id bcm6328_led_ids[] = {
250 { .compatible = "brcm,bcm6328-leds" },
254 U_BOOT_DRIVER(bcm6328_led) = {
255 .name = "bcm6328-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),