Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2020 Cortina-Access |
| 5 | * Author: Jway Lin <jway.lin@cortina-access.com> |
| 6 | * |
| 7 | */ |
| 8 | |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <errno.h> |
| 11 | #include <led.h> |
| 12 | #include <log.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <linux/bitops.h> |
| 16 | |
| 17 | #define LED_MAX_HW_BLINK 127 |
| 18 | #define LED_MAX_COUNT 16 |
| 19 | |
| 20 | /* LED_CONTROL fields */ |
| 21 | #define LED_BLINK_RATE1_SHIFT 0 |
| 22 | #define LED_BLINK_RATE1_MASK 0xff |
| 23 | #define LED_BLINK_RATE2_SHIFT 8 |
| 24 | #define LED_BLINK_RATE2_MASK 0xff |
| 25 | #define LED_CLK_TEST BIT(16) |
| 26 | #define LED_CLK_POLARITY BIT(17) |
| 27 | #define LED_CLK_TEST_MODE BIT(16) |
| 28 | #define LED_CLK_TEST_RX_TEST BIT(30) |
| 29 | #define LED_CLK_TEST_TX_TEST BIT(31) |
| 30 | |
| 31 | /* LED_CONFIG fields */ |
| 32 | #define LED_EVENT_ON_SHIFT 0 |
| 33 | #define LED_EVENT_ON_MASK 0x7 |
| 34 | #define LED_EVENT_BLINK_SHIFT 3 |
| 35 | #define LED_EVENT_BLINK_MASK 0x7 |
| 36 | #define LED_EVENT_OFF_SHIFT 6 |
| 37 | #define LED_EVENT_OFF_MASK 0x7 |
| 38 | #define LED_OFF_ON_SHIFT 9 |
| 39 | #define LED_OFF_ON_MASK 0x3 |
| 40 | #define LED_PORT_SHIFT 11 |
| 41 | #define LED_PORT_MASK 0x7 |
| 42 | #define LED_OFF_VAL BIT(14) |
| 43 | #define LED_SW_EVENT BIT(15) |
| 44 | #define LED_BLINK_SEL BIT(16) |
| 45 | |
| 46 | /* LED_CONFIG structures */ |
| 47 | struct cortina_led_cfg { |
| 48 | void __iomem *regs; |
| 49 | u32 pin; /* LED pin nubmer */ |
| 50 | bool active_low; /*Active-High or Active-Low*/ |
| 51 | u32 off_event; /* set led off event (RX,TX,SW)*/ |
| 52 | u32 blink_event; /* set led blink event (RX,TX,SW)*/ |
| 53 | u32 on_event; /* set led on event (RX,TX,SW)*/ |
| 54 | u32 port; /* corresponding ethernet port */ |
| 55 | int blink_sel; /* select blink-rate1 or blink-rate2 */ |
| 56 | }; |
| 57 | |
| 58 | /* LED_control structures */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 59 | struct cortina_led_plat { |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 60 | void __iomem *ctrl_regs; |
| 61 | u16 rate1; /* blink rate setting 0 */ |
| 62 | u16 rate2; /* blink rate setting 1 */ |
| 63 | }; |
| 64 | |
| 65 | enum ca_led_state_t { |
| 66 | CA_EVENT_MODE = 0, |
| 67 | CA_LED_ON = 1, |
| 68 | CA_LED_OFF, |
| 69 | }; |
| 70 | |
| 71 | static void cortina_led_write(void __iomem *reg, unsigned long data) |
| 72 | { |
| 73 | writel(data, reg); |
| 74 | } |
| 75 | |
| 76 | static unsigned long cortina_led_read(void __iomem *reg) |
| 77 | { |
| 78 | return readl(reg); |
| 79 | } |
| 80 | |
| 81 | static enum led_state_t cortina_led_get_state(struct udevice *dev) |
| 82 | { |
| 83 | struct cortina_led_cfg *priv = dev_get_priv(dev); |
| 84 | enum led_state_t state = LEDST_OFF; |
| 85 | u32 val; |
| 86 | |
| 87 | val = readl(priv->regs); |
| 88 | |
| 89 | if (val & LED_SW_EVENT) |
| 90 | state = LEDST_ON; |
| 91 | |
| 92 | return state; |
| 93 | } |
| 94 | |
| 95 | static int cortina_led_set_state(struct udevice *dev, enum led_state_t state) |
| 96 | { |
| 97 | u32 val; |
| 98 | struct cortina_led_cfg *priv = dev_get_priv(dev); |
| 99 | |
| 100 | val = readl(priv->regs); |
| 101 | val &= ~(LED_OFF_ON_MASK << LED_OFF_ON_SHIFT); |
| 102 | |
| 103 | switch (state) { |
| 104 | case LEDST_OFF: |
| 105 | val &= ~LED_SW_EVENT; |
| 106 | val |= CA_LED_OFF << LED_OFF_ON_SHIFT; |
| 107 | cortina_led_write(priv->regs, val); |
| 108 | break; |
| 109 | case LEDST_ON: |
| 110 | val |= LED_SW_EVENT; |
| 111 | val |= CA_LED_ON << LED_OFF_ON_SHIFT; |
| 112 | cortina_led_write(priv->regs, val); |
| 113 | break; |
| 114 | case LEDST_TOGGLE: |
| 115 | if (cortina_led_get_state(dev) == LEDST_OFF) |
| 116 | return cortina_led_set_state(dev, LEDST_ON); |
| 117 | else |
| 118 | return cortina_led_set_state(dev, LEDST_OFF); |
| 119 | break; |
| 120 | default: |
| 121 | return -EINVAL; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static const struct led_ops cortina_led_ops = { |
| 128 | .get_state = cortina_led_get_state, |
| 129 | .set_state = cortina_led_set_state, |
| 130 | }; |
| 131 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 132 | static int ca_led_of_to_plat(struct udevice *dev) |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 133 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 134 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 135 | |
| 136 | /* Top-level LED node */ |
| 137 | if (!uc_plat->label) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 138 | struct cortina_led_plat *plt = dev_get_plat(dev); |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 139 | |
| 140 | plt->rate1 = |
| 141 | dev_read_u32_default(dev, "Cortina,blink-rate1", 256); |
| 142 | plt->rate2 = |
| 143 | dev_read_u32_default(dev, "Cortina,blink-rate2", 512); |
| 144 | plt->ctrl_regs = dev_remap_addr(dev); |
| 145 | } else { |
| 146 | struct cortina_led_cfg *priv = dev_get_priv(dev); |
| 147 | |
| 148 | priv->regs = dev_remap_addr(dev_get_parent(dev)); |
| 149 | priv->pin = dev_read_u32_default(dev, "pin", LED_MAX_COUNT); |
| 150 | priv->blink_sel = dev_read_u32_default(dev, "blink-sel", 0); |
| 151 | priv->off_event = dev_read_u32_default(dev, "off-event", 0); |
| 152 | priv->blink_event = dev_read_u32_default(dev, "blink-event", 0); |
| 153 | priv->on_event = dev_read_u32_default(dev, "on-event", 0); |
| 154 | priv->port = dev_read_u32_default(dev, "port", 0); |
| 155 | |
| 156 | if (dev_read_bool(dev, "active-low")) |
| 157 | priv->active_low = true; |
| 158 | else |
| 159 | priv->active_low = false; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int cortina_led_probe(struct udevice *dev) |
| 166 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 167 | struct led_uc_plat *uc_plat = dev_get_uclass_plat(dev); |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 168 | |
| 169 | /* Top-level LED node */ |
| 170 | if (!uc_plat->label) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 171 | struct cortina_led_plat *plat = dev_get_plat(dev); |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 172 | u32 reg_value, val; |
| 173 | u16 rate1, rate2; |
| 174 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 175 | if (!plat->ctrl_regs) |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 176 | return -EINVAL; |
| 177 | |
| 178 | reg_value = 0; |
| 179 | reg_value |= LED_CLK_POLARITY; |
| 180 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 181 | rate1 = plat->rate1; |
| 182 | rate2 = plat->rate2; |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 183 | |
| 184 | val = rate1 / 16 - 1; |
| 185 | rate1 = val > LED_MAX_HW_BLINK ? |
| 186 | LED_MAX_HW_BLINK : val; |
| 187 | reg_value |= (rate1 & LED_BLINK_RATE1_MASK) << |
| 188 | LED_BLINK_RATE1_SHIFT; |
| 189 | |
| 190 | val = rate2 / 16 - 1; |
| 191 | rate2 = val > LED_MAX_HW_BLINK ? |
| 192 | LED_MAX_HW_BLINK : val; |
| 193 | reg_value |= (rate2 & LED_BLINK_RATE2_MASK) << |
| 194 | LED_BLINK_RATE2_SHIFT; |
| 195 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 196 | cortina_led_write(plat->ctrl_regs, reg_value); |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 197 | |
| 198 | } else { |
| 199 | struct cortina_led_cfg *priv = dev_get_priv(dev); |
| 200 | void __iomem *regs; |
| 201 | u32 val, port, off_event, blink_event, on_event; |
| 202 | |
| 203 | regs = priv->regs; |
| 204 | if (!regs) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | if (priv->pin >= LED_MAX_COUNT) |
| 208 | return -EINVAL; |
| 209 | |
| 210 | priv->regs = regs + 4 + priv->pin * 4; |
| 211 | |
| 212 | val = cortina_led_read(priv->regs); |
| 213 | |
| 214 | if (priv->active_low) |
| 215 | val |= LED_OFF_VAL; |
| 216 | else |
| 217 | val &= ~LED_OFF_VAL; |
| 218 | |
| 219 | if (priv->blink_sel == 0) |
| 220 | val &= ~LED_BLINK_SEL; |
| 221 | else if (priv->blink_sel == 1) |
| 222 | val |= LED_BLINK_SEL; |
| 223 | |
| 224 | off_event = priv->off_event; |
| 225 | val &= ~(LED_EVENT_OFF_MASK << LED_EVENT_OFF_SHIFT); |
| 226 | if (off_event != 0) |
| 227 | val |= BIT(off_event) << LED_EVENT_OFF_SHIFT; |
| 228 | |
| 229 | blink_event = priv->blink_event; |
| 230 | val &= ~(LED_EVENT_BLINK_MASK << LED_EVENT_BLINK_SHIFT); |
| 231 | if (blink_event != 0) |
| 232 | val |= BIT(blink_event) << LED_EVENT_BLINK_SHIFT; |
| 233 | |
| 234 | on_event = priv->on_event; |
| 235 | val &= ~(LED_EVENT_ON_MASK << LED_EVENT_ON_SHIFT); |
| 236 | if (on_event != 0) |
| 237 | val |= BIT(on_event) << LED_EVENT_ON_SHIFT; |
| 238 | |
| 239 | port = priv->port; |
| 240 | val &= ~(LED_PORT_MASK << LED_PORT_SHIFT); |
| 241 | val |= port << LED_PORT_SHIFT; |
| 242 | |
| 243 | /* force off */ |
| 244 | val &= ~(LED_OFF_ON_MASK << LED_OFF_ON_SHIFT); |
| 245 | val |= CA_LED_OFF << LED_OFF_ON_SHIFT; |
| 246 | |
| 247 | cortina_led_write(priv->regs, val); |
| 248 | } |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int cortina_led_bind(struct udevice *parent) |
| 254 | { |
| 255 | ofnode node; |
| 256 | |
| 257 | dev_for_each_subnode(node, parent) { |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 258 | struct udevice *dev; |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 259 | int ret; |
| 260 | |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 261 | ret = device_bind_driver_to_node(parent, "ca-leds", |
| 262 | ofnode_get_name(node), |
| 263 | node, &dev); |
| 264 | if (ret) |
| 265 | return ret; |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static const struct udevice_id ca_led_ids[] = { |
| 272 | { .compatible = "cortina,ca-leds" }, |
| 273 | { /* sentinel */ } |
| 274 | }; |
| 275 | |
| 276 | U_BOOT_DRIVER(cortina_led) = { |
| 277 | .name = "ca-leds", |
| 278 | .id = UCLASS_LED, |
| 279 | .of_match = ca_led_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 280 | .of_to_plat = ca_led_of_to_plat, |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 281 | .bind = cortina_led_bind, |
| 282 | .probe = cortina_led_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 283 | .plat_auto = sizeof(struct cortina_led_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 284 | .priv_auto = sizeof(struct cortina_led_cfg), |
Jway Lin | c161200 | 2020-06-30 21:08:06 -0700 | [diff] [blame] | 285 | .ops = &cortina_led_ops, |
| 286 | }; |