1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
16 /* LED Mode register */
17 #define LED_MODE_REG 0x0
18 #define LED_MODE_OFF 0
20 #define LED_MODE_MASK 1
22 /* LED Control register */
23 #define LED_CTRL_REG 0x4
24 #define LED_CTRL_CLK_MASK 0x3
25 #define LED_CTRL_CLK_1 0
26 #define LED_CTRL_CLK_2 1
27 #define LED_CTRL_CLK_4 2
28 #define LED_CTRL_CLK_8 3
29 #define LED_CTRL_POL_SHIFT 2
30 #define LED_CTRL_POL_MASK (1 << LED_CTRL_POL_SHIFT)
31 #define LED_CTRL_BUSY_SHIFT 3
32 #define LED_CTRL_BUSY_MASK (1 << LED_CTRL_BUSY_SHIFT)
34 struct bcm6358_led_priv {
40 static void bcm6358_led_busy(void __iomem *regs)
42 while (readl_be(regs + LED_CTRL_REG) & LED_CTRL_BUSY_MASK)
46 static unsigned long bcm6358_led_get_mode(struct bcm6358_led_priv *priv)
48 bcm6358_led_busy(priv->regs);
50 return (readl_be(priv->regs + LED_MODE_REG) >> priv->pin) &
54 static int bcm6358_led_set_mode(struct bcm6358_led_priv *priv, uint8_t mode)
56 bcm6358_led_busy(priv->regs);
58 clrsetbits_be32(priv->regs + LED_MODE_REG,
59 (LED_MODE_MASK << priv->pin),
65 static enum led_state_t bcm6358_led_get_state(struct udevice *dev)
67 struct bcm6358_led_priv *priv = dev_get_priv(dev);
68 enum led_state_t state = LEDST_OFF;
70 switch (bcm6358_led_get_mode(priv)) {
72 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
75 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
82 static int bcm6358_led_set_state(struct udevice *dev, enum led_state_t state)
84 struct bcm6358_led_priv *priv = dev_get_priv(dev);
89 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
92 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
95 if (bcm6358_led_get_state(dev) == LEDST_OFF)
96 return bcm6358_led_set_state(dev, LEDST_ON);
98 return bcm6358_led_set_state(dev, LEDST_OFF);
104 return bcm6358_led_set_mode(priv, mode);
107 static const struct led_ops bcm6358_led_ops = {
108 .get_state = bcm6358_led_get_state,
109 .set_state = bcm6358_led_set_state,
112 static int bcm6358_led_probe(struct udevice *dev)
114 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
116 /* Top-level LED node */
117 if (!uc_plat->label) {
119 unsigned int clk_div;
122 regs = dev_remap_addr(dev);
126 if (dev_read_bool(dev, "brcm,clk-dat-low"))
127 set_bits |= LED_CTRL_POL_MASK;
128 clk_div = dev_read_u32_default(dev, "brcm,clk-div",
132 set_bits |= LED_CTRL_CLK_8;
135 set_bits |= LED_CTRL_CLK_4;
138 set_bits |= LED_CTRL_CLK_2;
141 set_bits |= LED_CTRL_CLK_1;
145 bcm6358_led_busy(regs);
146 clrsetbits_be32(regs + LED_CTRL_REG,
147 LED_CTRL_POL_MASK | LED_CTRL_CLK_MASK,
150 struct bcm6358_led_priv *priv = dev_get_priv(dev);
153 priv->regs = dev_remap_addr(dev);
157 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
163 if (dev_read_bool(dev, "active-low"))
164 priv->active_low = true;
170 static int bcm6358_led_bind(struct udevice *parent)
174 dev_for_each_subnode(node, parent) {
175 struct led_uc_plat *uc_plat;
180 label = ofnode_read_string(node, "label");
182 debug("%s: node %s has no label\n", __func__,
183 ofnode_get_name(node));
187 ret = device_bind_driver_to_node(parent, "bcm6358-led",
188 ofnode_get_name(node),
193 uc_plat = dev_get_uclass_platdata(dev);
194 uc_plat->label = label;
200 static const struct udevice_id bcm6358_led_ids[] = {
201 { .compatible = "brcm,bcm6358-leds" },
205 U_BOOT_DRIVER(bcm6358_led) = {
206 .name = "bcm6358-led",
208 .of_match = bcm6358_led_ids,
209 .bind = bcm6358_led_bind,
210 .probe = bcm6358_led_probe,
211 .priv_auto_alloc_size = sizeof(struct bcm6358_led_priv),
212 .ops = &bcm6358_led_ops,