Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: hellcreek: Add PTP status LEDs

The switch has two controllable I/Os which are usually connected to LEDs. This
is useful to immediately visually see the PTP status.

These provide two signals:

* is_gm

This LED can be activated if the current device is the grand master in that
PTP domain.

* sync_good

This LED can be activated if the current device is in sync with the network
time.

Expose these via the LED framework to be controlled via user space
e.g. linuxptp.

Signed-off-by: Kurt Kanzenbach <kurt@linutronix.de>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Kurt Kanzenbach and committed by
Jakub Kicinski
7d9ee2e8 f0d4ba9e

+156
+4
drivers/net/dsa/hirschmann/hellcreek.h
··· 16 16 #include <linux/kernel.h> 17 17 #include <linux/mutex.h> 18 18 #include <linux/workqueue.h> 19 + #include <linux/leds.h> 19 20 #include <linux/platform_data/hirschmann-hellcreek.h> 20 21 #include <linux/ptp_clock_kernel.h> 21 22 #include <linux/timecounter.h> ··· 268 267 struct ptp_clock_info ptp_clock_info; 269 268 struct hellcreek_port *ports; 270 269 struct delayed_work overflow_work; 270 + struct led_classdev led_is_gm; 271 + struct led_classdev led_sync_good; 271 272 struct mutex reg_lock; /* Switch IP register lock */ 272 273 struct mutex vlan_lock; /* VLAN bitmaps lock */ 273 274 struct mutex ptp_lock; /* PTP IP register lock */ ··· 279 276 u8 *vidmbrcfg; /* vidmbrcfg shadow */ 280 277 u64 seconds; /* PTP seconds */ 281 278 u64 last_ts; /* Used for overflow detection */ 279 + u16 status_out; /* ptp.status_out shadow */ 282 280 size_t fdb_entries; 283 281 }; 284 282
+149
drivers/net/dsa/hirschmann/hellcreek_ptp.c
··· 239 239 HELLCREEK_OVERFLOW_PERIOD); 240 240 } 241 241 242 + static enum led_brightness hellcreek_get_brightness(struct hellcreek *hellcreek, 243 + int led) 244 + { 245 + return (hellcreek->status_out & led) ? 1 : 0; 246 + } 247 + 248 + static void hellcreek_set_brightness(struct hellcreek *hellcreek, int led, 249 + enum led_brightness b) 250 + { 251 + mutex_lock(&hellcreek->ptp_lock); 252 + 253 + if (b) 254 + hellcreek->status_out |= led; 255 + else 256 + hellcreek->status_out &= ~led; 257 + 258 + hellcreek_ptp_write(hellcreek, hellcreek->status_out, STATUS_OUT); 259 + 260 + mutex_unlock(&hellcreek->ptp_lock); 261 + } 262 + 263 + static void hellcreek_led_sync_good_set(struct led_classdev *ldev, 264 + enum led_brightness b) 265 + { 266 + struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_sync_good); 267 + 268 + hellcreek_set_brightness(hellcreek, STATUS_OUT_SYNC_GOOD, b); 269 + } 270 + 271 + static enum led_brightness hellcreek_led_sync_good_get(struct led_classdev *ldev) 272 + { 273 + struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_sync_good); 274 + 275 + return hellcreek_get_brightness(hellcreek, STATUS_OUT_SYNC_GOOD); 276 + } 277 + 278 + static void hellcreek_led_is_gm_set(struct led_classdev *ldev, 279 + enum led_brightness b) 280 + { 281 + struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_is_gm); 282 + 283 + hellcreek_set_brightness(hellcreek, STATUS_OUT_IS_GM, b); 284 + } 285 + 286 + static enum led_brightness hellcreek_led_is_gm_get(struct led_classdev *ldev) 287 + { 288 + struct hellcreek *hellcreek = led_to_hellcreek(ldev, led_is_gm); 289 + 290 + return hellcreek_get_brightness(hellcreek, STATUS_OUT_IS_GM); 291 + } 292 + 293 + /* There two available LEDs internally called sync_good and is_gm. However, the 294 + * user might want to use a different label and specify the default state. Take 295 + * those properties from device tree. 296 + */ 297 + static int hellcreek_led_setup(struct hellcreek *hellcreek) 298 + { 299 + struct device_node *leds, *led = NULL; 300 + const char *label, *state; 301 + int ret = -EINVAL; 302 + 303 + leds = of_find_node_by_name(hellcreek->dev->of_node, "leds"); 304 + if (!leds) { 305 + dev_err(hellcreek->dev, "No LEDs specified in device tree!\n"); 306 + return ret; 307 + } 308 + 309 + hellcreek->status_out = 0; 310 + 311 + led = of_get_next_available_child(leds, led); 312 + if (!led) { 313 + dev_err(hellcreek->dev, "First LED not specified!\n"); 314 + goto out; 315 + } 316 + 317 + ret = of_property_read_string(led, "label", &label); 318 + hellcreek->led_sync_good.name = ret ? "sync_good" : label; 319 + 320 + ret = of_property_read_string(led, "default-state", &state); 321 + if (!ret) { 322 + if (!strcmp(state, "on")) 323 + hellcreek->led_sync_good.brightness = 1; 324 + else if (!strcmp(state, "off")) 325 + hellcreek->led_sync_good.brightness = 0; 326 + else if (!strcmp(state, "keep")) 327 + hellcreek->led_sync_good.brightness = 328 + hellcreek_get_brightness(hellcreek, 329 + STATUS_OUT_SYNC_GOOD); 330 + } 331 + 332 + hellcreek->led_sync_good.max_brightness = 1; 333 + hellcreek->led_sync_good.brightness_set = hellcreek_led_sync_good_set; 334 + hellcreek->led_sync_good.brightness_get = hellcreek_led_sync_good_get; 335 + 336 + led = of_get_next_available_child(leds, led); 337 + if (!led) { 338 + dev_err(hellcreek->dev, "Second LED not specified!\n"); 339 + ret = -EINVAL; 340 + goto out; 341 + } 342 + 343 + ret = of_property_read_string(led, "label", &label); 344 + hellcreek->led_is_gm.name = ret ? "is_gm" : label; 345 + 346 + ret = of_property_read_string(led, "default-state", &state); 347 + if (!ret) { 348 + if (!strcmp(state, "on")) 349 + hellcreek->led_is_gm.brightness = 1; 350 + else if (!strcmp(state, "off")) 351 + hellcreek->led_is_gm.brightness = 0; 352 + else if (!strcmp(state, "keep")) 353 + hellcreek->led_is_gm.brightness = 354 + hellcreek_get_brightness(hellcreek, 355 + STATUS_OUT_IS_GM); 356 + } 357 + 358 + hellcreek->led_is_gm.max_brightness = 1; 359 + hellcreek->led_is_gm.brightness_set = hellcreek_led_is_gm_set; 360 + hellcreek->led_is_gm.brightness_get = hellcreek_led_is_gm_get; 361 + 362 + /* Set initial state */ 363 + if (hellcreek->led_sync_good.brightness == 1) 364 + hellcreek_set_brightness(hellcreek, STATUS_OUT_SYNC_GOOD, 1); 365 + if (hellcreek->led_is_gm.brightness == 1) 366 + hellcreek_set_brightness(hellcreek, STATUS_OUT_IS_GM, 1); 367 + 368 + /* Register both leds */ 369 + led_classdev_register(hellcreek->dev, &hellcreek->led_sync_good); 370 + led_classdev_register(hellcreek->dev, &hellcreek->led_is_gm); 371 + 372 + ret = 0; 373 + 374 + out: 375 + of_node_put(leds); 376 + 377 + return ret; 378 + } 379 + 242 380 int hellcreek_ptp_setup(struct hellcreek *hellcreek) 243 381 { 244 382 u16 status; 383 + int ret; 245 384 246 385 /* Set up the overflow work */ 247 386 INIT_DELAYED_WORK(&hellcreek->overflow_work, ··· 427 288 hellcreek_ptp_write(hellcreek, status | PR_CLOCK_STATUS_C_ENA_DRIFT, 428 289 PR_CLOCK_STATUS_C); 429 290 291 + /* LED setup */ 292 + ret = hellcreek_led_setup(hellcreek); 293 + if (ret) { 294 + if (hellcreek->ptp_clock) 295 + ptp_clock_unregister(hellcreek->ptp_clock); 296 + return ret; 297 + } 298 + 430 299 schedule_delayed_work(&hellcreek->overflow_work, 431 300 HELLCREEK_OVERFLOW_PERIOD); 432 301 ··· 443 296 444 297 void hellcreek_ptp_free(struct hellcreek *hellcreek) 445 298 { 299 + led_classdev_unregister(&hellcreek->led_is_gm); 300 + led_classdev_unregister(&hellcreek->led_sync_good); 446 301 cancel_delayed_work_sync(&hellcreek->overflow_work); 447 302 if (hellcreek->ptp_clock) 448 303 ptp_clock_unregister(hellcreek->ptp_clock);
+3
drivers/net/dsa/hirschmann/hellcreek_ptp.h
··· 70 70 #define dw_overflow_to_hellcreek(dw) \ 71 71 container_of(dw, struct hellcreek, overflow_work) 72 72 73 + #define led_to_hellcreek(ldev, led) \ 74 + container_of(ldev, struct hellcreek, led) 75 + 73 76 #endif /* _HELLCREEK_PTP_H_ */