1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (C) 2020 Cortina-Access
5 * Author: Jway Lin <jway.lin@cortina-access.com>
16 #include <linux/bitops.h>
18 #define LED_MAX_HW_BLINK 127
19 #define LED_MAX_COUNT 16
21 /* LED_CONTROL fields */
22 #define LED_BLINK_RATE1_SHIFT 0
23 #define LED_BLINK_RATE1_MASK 0xff
24 #define LED_BLINK_RATE2_SHIFT 8
25 #define LED_BLINK_RATE2_MASK 0xff
26 #define LED_CLK_TEST BIT(16)
27 #define LED_CLK_POLARITY BIT(17)
28 #define LED_CLK_TEST_MODE BIT(16)
29 #define LED_CLK_TEST_RX_TEST BIT(30)
30 #define LED_CLK_TEST_TX_TEST BIT(31)
32 /* LED_CONFIG fields */
33 #define LED_EVENT_ON_SHIFT 0
34 #define LED_EVENT_ON_MASK 0x7
35 #define LED_EVENT_BLINK_SHIFT 3
36 #define LED_EVENT_BLINK_MASK 0x7
37 #define LED_EVENT_OFF_SHIFT 6
38 #define LED_EVENT_OFF_MASK 0x7
39 #define LED_OFF_ON_SHIFT 9
40 #define LED_OFF_ON_MASK 0x3
41 #define LED_PORT_SHIFT 11
42 #define LED_PORT_MASK 0x7
43 #define LED_OFF_VAL BIT(14)
44 #define LED_SW_EVENT BIT(15)
45 #define LED_BLINK_SEL BIT(16)
47 /* LED_CONFIG structures */
48 struct cortina_led_cfg {
50 u32 pin; /* LED pin nubmer */
51 bool active_low; /*Active-High or Active-Low*/
52 u32 off_event; /* set led off event (RX,TX,SW)*/
53 u32 blink_event; /* set led blink event (RX,TX,SW)*/
54 u32 on_event; /* set led on event (RX,TX,SW)*/
55 u32 port; /* corresponding ethernet port */
56 int blink_sel; /* select blink-rate1 or blink-rate2 */
59 /* LED_control structures */
60 struct cortina_led_plat {
61 void __iomem *ctrl_regs;
62 u16 rate1; /* blink rate setting 0 */
63 u16 rate2; /* blink rate setting 1 */
72 static void cortina_led_write(void __iomem *reg, unsigned long data)
77 static unsigned long cortina_led_read(void __iomem *reg)
82 static enum led_state_t cortina_led_get_state(struct udevice *dev)
84 struct cortina_led_cfg *priv = dev_get_priv(dev);
85 enum led_state_t state = LEDST_OFF;
88 val = readl(priv->regs);
90 if (val & LED_SW_EVENT)
96 static int cortina_led_set_state(struct udevice *dev, enum led_state_t state)
99 struct cortina_led_cfg *priv = dev_get_priv(dev);
101 val = readl(priv->regs);
102 val &= ~(LED_OFF_ON_MASK << LED_OFF_ON_SHIFT);
106 val &= ~LED_SW_EVENT;
107 val |= CA_LED_OFF << LED_OFF_ON_SHIFT;
108 cortina_led_write(priv->regs, val);
112 val |= CA_LED_ON << LED_OFF_ON_SHIFT;
113 cortina_led_write(priv->regs, val);
116 if (cortina_led_get_state(dev) == LEDST_OFF)
117 return cortina_led_set_state(dev, LEDST_ON);
119 return cortina_led_set_state(dev, LEDST_OFF);
128 static const struct led_ops cortina_led_ops = {
129 .get_state = cortina_led_get_state,
130 .set_state = cortina_led_set_state,
133 static int ca_led_of_to_plat(struct udevice *dev)
135 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
137 /* Top-level LED node */
138 if (!uc_plat->label) {
139 struct cortina_led_plat *plt = dev_get_plat(dev);
142 dev_read_u32_default(dev, "Cortina,blink-rate1", 256);
144 dev_read_u32_default(dev, "Cortina,blink-rate2", 512);
145 plt->ctrl_regs = dev_remap_addr(dev);
147 struct cortina_led_cfg *priv = dev_get_priv(dev);
149 priv->regs = dev_remap_addr(dev_get_parent(dev));
150 priv->pin = dev_read_u32_default(dev, "pin", LED_MAX_COUNT);
151 priv->blink_sel = dev_read_u32_default(dev, "blink-sel", 0);
152 priv->off_event = dev_read_u32_default(dev, "off-event", 0);
153 priv->blink_event = dev_read_u32_default(dev, "blink-event", 0);
154 priv->on_event = dev_read_u32_default(dev, "on-event", 0);
155 priv->port = dev_read_u32_default(dev, "port", 0);
157 if (dev_read_bool(dev, "active-low"))
158 priv->active_low = true;
160 priv->active_low = false;
166 static int cortina_led_probe(struct udevice *dev)
168 struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev);
170 /* Top-level LED node */
171 if (!uc_plat->label) {
172 struct cortina_led_plat *plat = dev_get_plat(dev);
176 if (!plat->ctrl_regs)
180 reg_value |= LED_CLK_POLARITY;
185 val = rate1 / 16 - 1;
186 rate1 = val > LED_MAX_HW_BLINK ?
187 LED_MAX_HW_BLINK : val;
188 reg_value |= (rate1 & LED_BLINK_RATE1_MASK) <<
189 LED_BLINK_RATE1_SHIFT;
191 val = rate2 / 16 - 1;
192 rate2 = val > LED_MAX_HW_BLINK ?
193 LED_MAX_HW_BLINK : val;
194 reg_value |= (rate2 & LED_BLINK_RATE2_MASK) <<
195 LED_BLINK_RATE2_SHIFT;
197 cortina_led_write(plat->ctrl_regs, reg_value);
200 struct cortina_led_cfg *priv = dev_get_priv(dev);
202 u32 val, port, off_event, blink_event, on_event;
208 if (priv->pin >= LED_MAX_COUNT)
211 priv->regs = regs + 4 + priv->pin * 4;
213 val = cortina_led_read(priv->regs);
215 if (priv->active_low)
220 if (priv->blink_sel == 0)
221 val &= ~LED_BLINK_SEL;
222 else if (priv->blink_sel == 1)
223 val |= LED_BLINK_SEL;
225 off_event = priv->off_event;
226 val &= ~(LED_EVENT_OFF_MASK << LED_EVENT_OFF_SHIFT);
228 val |= BIT(off_event) << LED_EVENT_OFF_SHIFT;
230 blink_event = priv->blink_event;
231 val &= ~(LED_EVENT_BLINK_MASK << LED_EVENT_BLINK_SHIFT);
232 if (blink_event != 0)
233 val |= BIT(blink_event) << LED_EVENT_BLINK_SHIFT;
235 on_event = priv->on_event;
236 val &= ~(LED_EVENT_ON_MASK << LED_EVENT_ON_SHIFT);
238 val |= BIT(on_event) << LED_EVENT_ON_SHIFT;
241 val &= ~(LED_PORT_MASK << LED_PORT_SHIFT);
242 val |= port << LED_PORT_SHIFT;
245 val &= ~(LED_OFF_ON_MASK << LED_OFF_ON_SHIFT);
246 val |= CA_LED_OFF << LED_OFF_ON_SHIFT;
248 cortina_led_write(priv->regs, val);
254 static int cortina_led_bind(struct udevice *parent)
258 dev_for_each_subnode(node, parent) {
262 ret = device_bind_driver_to_node(parent, "ca-leds",
263 ofnode_get_name(node),
272 static const struct udevice_id ca_led_ids[] = {
273 { .compatible = "cortina,ca-leds" },
277 U_BOOT_DRIVER(cortina_led) = {
280 .of_match = ca_led_ids,
281 .of_to_plat = ca_led_of_to_plat,
282 .bind = cortina_led_bind,
283 .probe = cortina_led_probe,
284 .plat_auto = sizeof(struct cortina_led_plat),
285 .priv_auto = sizeof(struct cortina_led_cfg),
286 .ops = &cortina_led_ops,