blob: 7be4badbac20d6a2b5e88ede9ff0aac59a94adba [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +02002/*
3 * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com>
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +02004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <led.h>
10#include <asm/io.h>
11#include <dm/lists.h>
12
13#define LEDS_MAX 24
14
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)
30
31/* LED Mode registers */
32#define LED_MODE_REG_HI 0x04
33#define LED_MODE_REG_LO 0x08
34#define LED_MODE_ON 0
35#define LED_MODE_FAST 1
36#define LED_MODE_BLINK 2
37#define LED_MODE_OFF 3
38#define LED_MODE_MASK 0x3
39
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +020040struct bcm6328_led_priv {
41 void __iomem *regs;
42 void __iomem *mode;
43 uint8_t shift;
44 bool active_low;
45};
46
47static unsigned long bcm6328_led_get_mode(struct bcm6328_led_priv *priv)
48{
49 return ((readl_be(priv->mode) >> priv->shift) & LED_MODE_MASK);
50}
51
52static int bcm6328_led_set_mode(struct bcm6328_led_priv *priv, uint8_t mode)
53{
54 clrsetbits_be32(priv->mode, (LED_MODE_MASK << priv->shift),
55 (mode << priv->shift));
56
57 return 0;
58}
59
60static enum led_state_t bcm6328_led_get_state(struct udevice *dev)
61{
62 struct bcm6328_led_priv *priv = dev_get_priv(dev);
63 enum led_state_t state = LEDST_OFF;
64
65 switch (bcm6328_led_get_mode(priv)) {
66#ifdef CONFIG_LED_BLINK
67 case LED_MODE_BLINK:
68 case LED_MODE_FAST:
69 state = LEDST_BLINK;
70 break;
71#endif
72 case LED_MODE_OFF:
73 state = (priv->active_low ? LEDST_ON : LEDST_OFF);
74 break;
75 case LED_MODE_ON:
76 state = (priv->active_low ? LEDST_OFF : LEDST_ON);
77 break;
78 }
79
80 return state;
81}
82
83static int bcm6328_led_set_state(struct udevice *dev, enum led_state_t state)
84{
85 struct bcm6328_led_priv *priv = dev_get_priv(dev);
86 unsigned long mode;
87
88 switch (state) {
89#ifdef CONFIG_LED_BLINK
90 case LEDST_BLINK:
91 mode = LED_MODE_BLINK;
92 break;
93#endif
94 case LEDST_OFF:
95 mode = (priv->active_low ? LED_MODE_ON : LED_MODE_OFF);
96 break;
97 case LEDST_ON:
98 mode = (priv->active_low ? LED_MODE_OFF : LED_MODE_ON);
99 break;
100 case LEDST_TOGGLE:
101 if (bcm6328_led_get_state(dev) == LEDST_OFF)
102 return bcm6328_led_set_state(dev, LEDST_ON);
103 else
104 return bcm6328_led_set_state(dev, LEDST_OFF);
105 break;
106 default:
107 return -ENOSYS;
108 }
109
110 return bcm6328_led_set_mode(priv, mode);
111}
112
113#ifdef CONFIG_LED_BLINK
114static unsigned long bcm6328_blink_delay(int delay)
115{
116 unsigned long bcm6328_delay = delay;
117
118 bcm6328_delay += (LED_INIT_FASTINTV_MS / 2);
119 bcm6328_delay /= LED_INIT_FASTINTV_MS;
120 bcm6328_delay <<= LED_INIT_FASTINTV_SHIFT;
121
122 if (bcm6328_delay > LED_INIT_FASTINTV_MASK)
123 return LED_INIT_FASTINTV_MASK;
124 else
125 return bcm6328_delay;
126}
127
128static int bcm6328_led_set_period(struct udevice *dev, int period_ms)
129{
130 struct bcm6328_led_priv *priv = dev_get_priv(dev);
131
132 clrsetbits_be32(priv->regs + LED_INIT_REG, LED_INIT_FASTINTV_MASK,
133 bcm6328_blink_delay(period_ms));
134
135 return 0;
136}
137#endif
138
139static 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,
144#endif
145};
146
147static int bcm6328_led_probe(struct udevice *dev)
148{
149 struct led_uc_plat *uc_plat = dev_get_uclass_platdata(dev);
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200150
151 /* Top-level LED node */
152 if (!uc_plat->label) {
153 void __iomem *regs;
154 u32 set_bits = 0;
155
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100156 regs = dev_remap_addr(dev);
157 if (!regs)
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200158 return -EINVAL;
159
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100160 if (dev_read_bool(dev, "brcm,serial-leds"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200161 set_bits |= LED_INIT_SLEDEN_MASK;
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100162 if (dev_read_bool(dev, "brcm,serial-mux"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200163 set_bits |= LED_INIT_SLEDMUX_MASK;
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100164 if (dev_read_bool(dev, "brcm,serial-clk-low"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200165 set_bits |= LED_INIT_SLEDCLKNPOL_MASK;
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100166 if (!dev_read_bool(dev, "brcm,serial-dat-low"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200167 set_bits |= LED_INIT_SLEDDATANPOL_MASK;
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100168 if (!dev_read_bool(dev, "brcm,serial-shift-inv"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200169 set_bits |= LED_INIT_SLEDSHIFTDIR_MASK;
170
171 clrsetbits_be32(regs + LED_INIT_REG, ~0, set_bits);
172 } else {
173 struct bcm6328_led_priv *priv = dev_get_priv(dev);
174 unsigned int pin;
175
Philippe Reynesc38abed2018-06-28 15:26:16 +0200176 priv->regs = dev_remap_addr(dev_get_parent(dev));
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100177 if (!priv->regs)
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200178 return -EINVAL;
179
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100180 pin = dev_read_u32_default(dev, "reg", LEDS_MAX);
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200181 if (pin >= LEDS_MAX)
182 return -EINVAL;
183
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200184 if (pin < 8) {
185 /* LEDs 0-7 (bits 47:32) */
186 priv->mode = priv->regs + LED_MODE_REG_HI;
187 priv->shift = (pin << 1);
188 } else {
189 /* LEDs 8-23 (bits 31:0) */
190 priv->mode = priv->regs + LED_MODE_REG_LO;
191 priv->shift = ((pin - 8) << 1);
192 }
193
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100194 if (dev_read_bool(dev, "active-low"))
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200195 priv->active_low = true;
196 }
197
198 return 0;
199}
200
201static int bcm6328_led_bind(struct udevice *parent)
202{
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100203 ofnode node;
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200204
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100205 dev_for_each_subnode(node, parent) {
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200206 struct led_uc_plat *uc_plat;
207 struct udevice *dev;
208 const char *label;
209 int ret;
210
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100211 label = ofnode_read_string(node, "label");
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200212 if (!label) {
213 debug("%s: node %s has no label\n", __func__,
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100214 ofnode_get_name(node));
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200215 return -EINVAL;
216 }
217
218 ret = device_bind_driver_to_node(parent, "bcm6328-led",
Álvaro Fernández Rojas89945512018-03-22 19:39:34 +0100219 ofnode_get_name(node),
220 node, &dev);
Álvaro Fernández Rojas28300dc2017-05-07 20:10:24 +0200221 if (ret)
222 return ret;
223
224 uc_plat = dev_get_uclass_platdata(dev);
225 uc_plat->label = label;
226 }
227
228 return 0;
229}
230
231static const struct udevice_id bcm6328_led_ids[] = {
232 { .compatible = "brcm,bcm6328-leds" },
233 { /* sentinel */ }
234};
235
236U_BOOT_DRIVER(bcm6328_led) = {
237 .name = "bcm6328-led",
238 .id = UCLASS_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),
244};