1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
15 /* LED Init register */
16 #define LED_INIT_REG 0x00
17 #define LED_INIT_FASTINTV_MS 20
18 #define LED_INIT_FASTINTV_SHIFT 6
19 #define LED_INIT_FASTINTV_MASK (0x3f << LED_INIT_FASTINTV_SHIFT)
20 #define LED_INIT_SLEDEN_SHIFT 12
21 #define LED_INIT_SLEDEN_MASK (1 << LED_INIT_SLEDEN_SHIFT)
22 #define LED_INIT_SLEDMUX_SHIFT 13
23 #define LED_INIT_SLEDMUX_MASK (1 << LED_INIT_SLEDMUX_SHIFT)
24 #define LED_INIT_SLEDCLKNPOL_SHIFT 14
25 #define LED_INIT_SLEDCLKNPOL_MASK (1 << LED_INIT_SLEDCLKNPOL_SHIFT)
26 #define LED_INIT_SLEDDATAPPOL_SHIFT 15
27 #define LED_INIT_SLEDDATANPOL_MASK (1 << LED_INIT_SLEDDATAPPOL_SHIFT)
28 #define LED_INIT_SLEDSHIFTDIR_SHIFT 16
29 #define LED_INIT_SLEDSHIFTDIR_MASK (1 << LED_INIT_SLEDSHIFTDIR_SHIFT)
31 /* LED Mode registers */
32 #define LED_MODE_REG_HI 0x04
33 #define LED_MODE_REG_LO 0x08
35 #define LED_MODE_FAST 1
36 #define LED_MODE_BLINK 2
37 #define LED_MODE_OFF 3
38 #define LED_MODE_MASK 0x3
40 struct bcm6328_led_priv {
47 static unsigned long bcm6328_led_get_mode(struct bcm6328_led_priv *priv)
49 return ((readl_be(priv->mode) >> priv->shift) & LED_MODE_MASK);
52 static int bcm6328_led_set_mode(struct bcm6328_led_priv *priv, uint8_t mode)
54 clrsetbits_be32(priv->mode, (LED_MODE_MASK << priv->shift),
55 (mode << priv->shift));
60 static enum led_state_t bcm6328_led_get_state(struct udevice *dev)
62 struct bcm6328_led_priv *priv = dev_get_priv(dev);
63 enum led_state_t state = LEDST_OFF;
65 switch (bcm6328_led_get_mode(priv)) {
66 #ifdef CONFIG_LED_BLINK
73 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
76 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
83 static int bcm6328_led_set_state(struct udevice *dev, enum led_state_t state)
85 struct bcm6328_led_priv *priv = dev_get_priv(dev);
89 #ifdef CONFIG_LED_BLINK
91 mode = LED_MODE_BLINK;
95 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
98 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
101 if (bcm6328_led_get_state(dev) == LEDST_OFF)
102 return bcm6328_led_set_state(dev, LEDST_ON);
104 return bcm6328_led_set_state(dev, LEDST_OFF);
110 return bcm6328_led_set_mode(priv, mode);
113 #ifdef CONFIG_LED_BLINK
114 static unsigned long bcm6328_blink_delay(int delay)
116 unsigned long bcm6328_delay = delay;
118 bcm6328_delay += (LED_INIT_FASTINTV_MS / 2);
119 bcm6328_delay /= LED_INIT_FASTINTV_MS;
120 bcm6328_delay <<= LED_INIT_FASTINTV_SHIFT;
122 if (bcm6328_delay > LED_INIT_FASTINTV_MASK)
123 return LED_INIT_FASTINTV_MASK;
125 return bcm6328_delay;
128 static int bcm6328_led_set_period(struct udevice *dev, int period_ms)
130 struct bcm6328_led_priv *priv = dev_get_priv(dev);
132 clrsetbits_be32(priv->regs + LED_INIT_REG, LED_INIT_FASTINTV_MASK,
133 bcm6328_blink_delay(period_ms));
139 static const struct led_ops bcm6328_led_ops = {
140 .get_state = bcm6328_led_get_state,
141 .set_state = bcm6328_led_set_state,
142 #ifdef CONFIG_LED_BLINK
143 .set_period = bcm6328_led_set_period,
147 static int bcm6328_led_probe(struct udevice *dev)
149 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
151 /* Top-level LED node */
152 if (!uc_plat->label) {
156 regs = dev_remap_addr(dev);
160 if (dev_read_bool(dev, "brcm,serial-leds"))
161 set_bits |= LED_INIT_SLEDEN_MASK;
162 if (dev_read_bool(dev, "brcm,serial-mux"))
163 set_bits |= LED_INIT_SLEDMUX_MASK;
164 if (dev_read_bool(dev, "brcm,serial-clk-low"))
165 set_bits |= LED_INIT_SLEDCLKNPOL_MASK;
166 if (!dev_read_bool(dev, "brcm,serial-dat-low"))
167 set_bits |= LED_INIT_SLEDDATANPOL_MASK;
168 if (!dev_read_bool(dev, "brcm,serial-shift-inv"))
169 set_bits |= LED_INIT_SLEDSHIFTDIR_MASK;
171 clrsetbits_be32(regs + LED_INIT_REG, ~0, set_bits);
173 struct bcm6328_led_priv *priv = dev_get_priv(dev);
176 priv->regs = dev_remap_addr(dev_get_parent(dev));
180 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
185 /* LEDs 0-7 (bits 47:32) */
186 priv->mode = priv->regs + LED_MODE_REG_HI;
187 priv->shift = (pin << 1);
189 /* LEDs 8-23 (bits 31:0) */
190 priv->mode = priv->regs + LED_MODE_REG_LO;
191 priv->shift = ((pin - 8) << 1);
194 if (dev_read_bool(dev, "active-low"))
195 priv->active_low = true;
201 static int bcm6328_led_bind(struct udevice *parent)
205 dev_for_each_subnode(node, parent) {
206 struct led_uc_plat *uc_plat;
211 label = ofnode_read_string(node, "label");
213 debug("%s: node %s has no label\n", __func__,
214 ofnode_get_name(node));
218 ret = device_bind_driver_to_node(parent, "bcm6328-led",
219 ofnode_get_name(node),
224 uc_plat = dev_get_uclass_platdata(dev);
225 uc_plat->label = label;
231 static const struct udevice_id bcm6328_led_ids[] = {
232 { .compatible = "brcm,bcm6328-leds" },
236 U_BOOT_DRIVER(bcm6328_led) = {
237 .name = "bcm6328-led",
239 .of_match = bcm6328_led_ids,
240 .ops = &bcm6328_led_ops,
241 .bind = bcm6328_led_bind,
242 .probe = bcm6328_led_probe,
243 .priv_auto_alloc_size = sizeof(struct bcm6328_led_priv),