The LED function of the LAN9303 and LAN9354 is selected by a single LED_FUN field that applies to all six LED pins of the two external ports. On some boards the LED straps double as configuration straps for other functions, such as the PHY address or the EEPROM size, so the desired LED mode cannot be strapped and has to be programmed by the driver. The LED subsystem is not used here: it models each LED independently, while this hardware has one function field for all LEDs. Read the optional "smsc,led-mode", and configure the device accordingly. Signed-off-by: Paul Louvel (Schneider Electric) --- drivers/net/dsa/lan9303-core.c | 74 ++++++++++++++++++++++++++++++++++++++++-- include/linux/dsa/lan9303.h | 8 +++++ 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/drivers/net/dsa/lan9303-core.c b/drivers/net/dsa/lan9303-core.c index 8eeb8953ff24..6a9ade873e42 100644 --- a/drivers/net/dsa/lan9303-core.c +++ b/drivers/net/dsa/lan9303-core.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -63,6 +64,13 @@ #define LAN9303_SWITCH_CSR_CMD_BUSY BIT(31) #define LAN9303_SWITCH_CSR_CMD_RW BIT(30) #define LAN9303_SWITCH_CSR_CMD_LANES (BIT(19) | BIT(18) | BIT(17) | BIT(16)) +#define LAN9303_LED_CFG 0x6f +# define LAN9303_LED_CFG_LED_FUN_MAX_VAL 0x7 +# define LAN9303_LED_CFG_LED_FUN_SHIFT 8 +# define LAN9303_LED_CFG_LED_FUN_MASK GENMASK(9, 8) +# define LAN9303_LED_CFG_LED_MODES GENMASK(3, 0) +# define LAN9354_LED_CFG_LED_FUN_MASK GENMASK(10, 8) +# define LAN9354_LED_CFG_LED_MODES (GENMASK(5, 0) | BIT(7)) #define LAN9303_VIRT_PHY_BASE 0x70 #define LAN9303_VIRT_SPECIAL_CTRL 0x77 #define LAN9303_VIRT_SPECIAL_TURBO BIT(10) /*Turbo MII Enable*/ @@ -240,6 +248,16 @@ static unsigned int flow_ctl_reg[] = { LAN9303_MANUAL_FC_2 }; +static const struct lan9303_led_info lan9303_led_info = { + .fun_mask = LAN9303_LED_CFG_LED_FUN_MASK, + .avail_modes = LAN9303_LED_CFG_LED_MODES, +}; + +static const struct lan9303_led_info lan9354_led_info = { + .fun_mask = LAN9354_LED_CFG_LED_FUN_MASK, + .avail_modes = LAN9354_LED_CFG_LED_MODES, +}; + static int lan9303_read(struct regmap *regmap, unsigned int offset, u32 *reg) { int ret, i; @@ -778,6 +796,20 @@ static int lan9303_enable_processing_port(struct lan9303 *chip, LAN9303_MAC_TX_CFG_X_TX_ENABLE); } +static int lan9303_setup_leds(struct lan9303 *chip) +{ + unsigned int val; + + if (chip->led_sel_mode >= 0) { + val = chip->led_sel_mode << LAN9303_LED_CFG_LED_FUN_SHIFT; + + return regmap_update_bits(chip->regmap, LAN9303_LED_CFG, chip->led_info->fun_mask, + val); + } + + return 0; +} + /* forward special tagged packets from port 0 to port 1 *or* port 2 */ static int lan9303_setup_tagging(struct lan9303 *chip) { @@ -902,8 +934,14 @@ static int lan9303_check_device(struct lan9303 *chip) return ret; } - if (((reg >> 16) != LAN9303_CHIP_ID) && - ((reg >> 16) != LAN9354_CHIP_ID)) { + switch (reg >> 16) { + case LAN9303_CHIP_ID: + chip->led_info = &lan9303_led_info; + break; + case LAN9354_CHIP_ID: + chip->led_info = &lan9354_led_info; + break; + default: dev_err(chip->dev, "unexpected device found: LAN%4.4X\n", reg >> 16); return -ENODEV; @@ -932,6 +970,30 @@ static int lan9303_check_device(struct lan9303 *chip) return 0; } +static int lan9303_parse_dt(struct lan9303 *chip) +{ + u32 led_mode; + int ret; + + if (!device_property_present(chip->dev, "smsc,led-mode")) { + chip->led_sel_mode = -1; + } else { + ret = device_property_read_u32(chip->dev, "smsc,led-mode", &led_mode); + if (ret) + return ret; + + if (led_mode > LAN9303_LED_CFG_LED_FUN_MAX_VAL || + !(chip->led_info->avail_modes & BIT(led_mode))) { + dev_err(chip->dev, "invalid led mode %u\n", led_mode); + return -EINVAL; + } + + chip->led_sel_mode = led_mode; + } + + return 0; +} + /* ---------------------------- DSA -----------------------------------*/ static enum dsa_tag_protocol lan9303_get_tag_protocol(struct dsa_switch *ds, @@ -964,6 +1026,10 @@ static int lan9303_setup(struct dsa_switch *ds) regmap_write(chip->regmap, LAN9303_VIRT_SPECIAL_CTRL, reg); } + ret = lan9303_setup_leds(chip); + if (ret) + dev_err(chip->dev, "failed to setup leds %d\n", ret); + ret = lan9303_setup_tagging(chip); if (ret) dev_err(chip->dev, "failed to setup port tagging %d\n", ret); @@ -1477,6 +1543,10 @@ int lan9303_probe(struct lan9303 *chip, struct device_node *np) if (ret) return ret; + ret = lan9303_parse_dt(chip); + if (ret) + return ret; + ret = lan9303_register_switch(chip); if (ret) { dev_dbg(chip->dev, "Failed to register switch: %d\n", ret); diff --git a/include/linux/dsa/lan9303.h b/include/linux/dsa/lan9303.h index 3ce7cbcc37a3..f0111ad7fb7f 100644 --- a/include/linux/dsa/lan9303.h +++ b/include/linux/dsa/lan9303.h @@ -17,6 +17,11 @@ struct lan9303_alr_cache_entry { u8 stp_override; /* non zero if set LAN9303_ALR_DAT1_AGE_OVERRID */ }; +struct lan9303_led_info { + u32 fun_mask; + u32 avail_modes; +}; + struct lan9303 { struct device *dev; struct regmap *regmap; @@ -30,6 +35,9 @@ struct lan9303 { const struct lan9303_phy_ops *ops; bool is_bridged; /* true if port 1 and 2 are bridged */ + const struct lan9303_led_info *led_info; + int led_sel_mode; + /* remember LAN9303_SWE_PORT_STATE while not bridged */ u32 swe_port_state; /* LAN9303 do not offer reading specific ALR entry. Cache all -- 2.56.1