Andrew Lunn | a2443fd | 2019-01-21 19:05:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2 | #include <linux/acpi.h> |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 3 | #include <linux/ctype.h> |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 4 | #include <linux/delay.h> |
Florian Fainelli | 54a2fc6 | 2017-10-30 21:42:58 -0700 | [diff] [blame] | 5 | #include <linux/gpio/consumer.h> |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 6 | #include <linux/hwmon.h> |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 7 | #include <linux/i2c.h> |
| 8 | #include <linux/interrupt.h> |
| 9 | #include <linux/jiffies.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/of.h> |
| 13 | #include <linux/phy.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/rtnetlink.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/workqueue.h> |
| 18 | |
| 19 | #include "mdio-i2c.h" |
| 20 | #include "sfp.h" |
| 21 | #include "swphy.h" |
| 22 | |
| 23 | enum { |
| 24 | GPIO_MODDEF0, |
| 25 | GPIO_LOS, |
| 26 | GPIO_TX_FAULT, |
| 27 | GPIO_TX_DISABLE, |
| 28 | GPIO_RATE_SELECT, |
| 29 | GPIO_MAX, |
| 30 | |
| 31 | SFP_F_PRESENT = BIT(GPIO_MODDEF0), |
| 32 | SFP_F_LOS = BIT(GPIO_LOS), |
| 33 | SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT), |
| 34 | SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE), |
| 35 | SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT), |
| 36 | |
| 37 | SFP_E_INSERT = 0, |
| 38 | SFP_E_REMOVE, |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 39 | SFP_E_DEV_ATTACH, |
| 40 | SFP_E_DEV_DETACH, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 41 | SFP_E_DEV_DOWN, |
| 42 | SFP_E_DEV_UP, |
| 43 | SFP_E_TX_FAULT, |
| 44 | SFP_E_TX_CLEAR, |
| 45 | SFP_E_LOS_HIGH, |
| 46 | SFP_E_LOS_LOW, |
| 47 | SFP_E_TIMEOUT, |
| 48 | |
| 49 | SFP_MOD_EMPTY = 0, |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 50 | SFP_MOD_ERROR, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 51 | SFP_MOD_PROBE, |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 52 | SFP_MOD_WAITDEV, |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 53 | SFP_MOD_HPOWER, |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 54 | SFP_MOD_WAITPWR, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 55 | SFP_MOD_PRESENT, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 56 | |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 57 | SFP_DEV_DETACHED = 0, |
| 58 | SFP_DEV_DOWN, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 59 | SFP_DEV_UP, |
| 60 | |
| 61 | SFP_S_DOWN = 0, |
Russell King | 74c551c | 2019-12-11 10:56:09 +0000 | [diff] [blame] | 62 | SFP_S_FAIL, |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 63 | SFP_S_WAIT, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 64 | SFP_S_INIT, |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 65 | SFP_S_INIT_PHY, |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 66 | SFP_S_INIT_TX_FAULT, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 67 | SFP_S_WAIT_LOS, |
| 68 | SFP_S_LINK_UP, |
| 69 | SFP_S_TX_FAULT, |
| 70 | SFP_S_REINIT, |
| 71 | SFP_S_TX_DISABLE, |
| 72 | }; |
| 73 | |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 74 | static const char * const mod_state_strings[] = { |
| 75 | [SFP_MOD_EMPTY] = "empty", |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 76 | [SFP_MOD_ERROR] = "error", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 77 | [SFP_MOD_PROBE] = "probe", |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 78 | [SFP_MOD_WAITDEV] = "waitdev", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 79 | [SFP_MOD_HPOWER] = "hpower", |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 80 | [SFP_MOD_WAITPWR] = "waitpwr", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 81 | [SFP_MOD_PRESENT] = "present", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | static const char *mod_state_to_str(unsigned short mod_state) |
| 85 | { |
| 86 | if (mod_state >= ARRAY_SIZE(mod_state_strings)) |
| 87 | return "Unknown module state"; |
| 88 | return mod_state_strings[mod_state]; |
| 89 | } |
| 90 | |
| 91 | static const char * const dev_state_strings[] = { |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 92 | [SFP_DEV_DETACHED] = "detached", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 93 | [SFP_DEV_DOWN] = "down", |
| 94 | [SFP_DEV_UP] = "up", |
| 95 | }; |
| 96 | |
| 97 | static const char *dev_state_to_str(unsigned short dev_state) |
| 98 | { |
| 99 | if (dev_state >= ARRAY_SIZE(dev_state_strings)) |
| 100 | return "Unknown device state"; |
| 101 | return dev_state_strings[dev_state]; |
| 102 | } |
| 103 | |
| 104 | static const char * const event_strings[] = { |
| 105 | [SFP_E_INSERT] = "insert", |
| 106 | [SFP_E_REMOVE] = "remove", |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 107 | [SFP_E_DEV_ATTACH] = "dev_attach", |
| 108 | [SFP_E_DEV_DETACH] = "dev_detach", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 109 | [SFP_E_DEV_DOWN] = "dev_down", |
| 110 | [SFP_E_DEV_UP] = "dev_up", |
| 111 | [SFP_E_TX_FAULT] = "tx_fault", |
| 112 | [SFP_E_TX_CLEAR] = "tx_clear", |
| 113 | [SFP_E_LOS_HIGH] = "los_high", |
| 114 | [SFP_E_LOS_LOW] = "los_low", |
| 115 | [SFP_E_TIMEOUT] = "timeout", |
| 116 | }; |
| 117 | |
| 118 | static const char *event_to_str(unsigned short event) |
| 119 | { |
| 120 | if (event >= ARRAY_SIZE(event_strings)) |
| 121 | return "Unknown event"; |
| 122 | return event_strings[event]; |
| 123 | } |
| 124 | |
| 125 | static const char * const sm_state_strings[] = { |
| 126 | [SFP_S_DOWN] = "down", |
Russell King | 74c551c | 2019-12-11 10:56:09 +0000 | [diff] [blame] | 127 | [SFP_S_FAIL] = "fail", |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 128 | [SFP_S_WAIT] = "wait", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 129 | [SFP_S_INIT] = "init", |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 130 | [SFP_S_INIT_PHY] = "init_phy", |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 131 | [SFP_S_INIT_TX_FAULT] = "init_tx_fault", |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 132 | [SFP_S_WAIT_LOS] = "wait_los", |
| 133 | [SFP_S_LINK_UP] = "link_up", |
| 134 | [SFP_S_TX_FAULT] = "tx_fault", |
| 135 | [SFP_S_REINIT] = "reinit", |
| 136 | [SFP_S_TX_DISABLE] = "rx_disable", |
| 137 | }; |
| 138 | |
| 139 | static const char *sm_state_to_str(unsigned short sm_state) |
| 140 | { |
| 141 | if (sm_state >= ARRAY_SIZE(sm_state_strings)) |
| 142 | return "Unknown state"; |
| 143 | return sm_state_strings[sm_state]; |
| 144 | } |
| 145 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 146 | static const char *gpio_of_names[] = { |
Baruch Siach | 25ee079 | 2017-09-07 12:25:50 +0300 | [diff] [blame] | 147 | "mod-def0", |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 148 | "los", |
| 149 | "tx-fault", |
| 150 | "tx-disable", |
Baruch Siach | 25ee079 | 2017-09-07 12:25:50 +0300 | [diff] [blame] | 151 | "rate-select0", |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | static const enum gpiod_flags gpio_flags[] = { |
| 155 | GPIOD_IN, |
| 156 | GPIOD_IN, |
| 157 | GPIOD_IN, |
| 158 | GPIOD_ASIS, |
| 159 | GPIOD_ASIS, |
| 160 | }; |
| 161 | |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 162 | /* t_start_up (SFF-8431) or t_init (SFF-8472) is the time required for a |
| 163 | * non-cooled module to initialise its laser safety circuitry. We wait |
| 164 | * an initial T_WAIT period before we check the tx fault to give any PHY |
| 165 | * on board (for a copper SFP) time to initialise. |
| 166 | */ |
| 167 | #define T_WAIT msecs_to_jiffies(50) |
| 168 | #define T_START_UP msecs_to_jiffies(300) |
| 169 | #define T_START_UP_BAD_GPON msecs_to_jiffies(60000) |
| 170 | |
| 171 | /* t_reset is the time required to assert the TX_DISABLE signal to reset |
| 172 | * an indicated TX_FAULT. |
| 173 | */ |
| 174 | #define T_RESET_US 10 |
| 175 | #define T_FAULT_RECOVER msecs_to_jiffies(1000) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 176 | |
Russell King | 65ef2d5 | 2019-12-09 14:15:55 +0000 | [diff] [blame] | 177 | /* N_FAULT_INIT is the number of recovery attempts at module initialisation |
| 178 | * time. If the TX_FAULT signal is not deasserted after this number of |
| 179 | * attempts at clearing it, we decide that the module is faulty. |
| 180 | * N_FAULT is the same but after the module has initialised. |
| 181 | */ |
| 182 | #define N_FAULT_INIT 5 |
| 183 | #define N_FAULT 5 |
| 184 | |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 185 | /* T_PHY_RETRY is the time interval between attempts to probe the PHY. |
| 186 | * R_PHY_RETRY is the number of attempts. |
| 187 | */ |
| 188 | #define T_PHY_RETRY msecs_to_jiffies(50) |
| 189 | #define R_PHY_RETRY 12 |
| 190 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 191 | /* SFP module presence detection is poor: the three MOD DEF signals are |
| 192 | * the same length on the PCB, which means it's possible for MOD DEF 0 to |
| 193 | * connect before the I2C bus on MOD DEF 1/2. |
| 194 | * |
Russell King | d900954 | 2019-11-10 14:06:33 +0000 | [diff] [blame] | 195 | * The SFF-8472 specifies t_serial ("Time from power on until module is |
| 196 | * ready for data transmission over the two wire serial bus.") as 300ms. |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 197 | */ |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 198 | #define T_SERIAL msecs_to_jiffies(300) |
| 199 | #define T_HPOWER_LEVEL msecs_to_jiffies(300) |
| 200 | #define T_PROBE_RETRY_INIT msecs_to_jiffies(100) |
| 201 | #define R_PROBE_RETRY_INIT 10 |
| 202 | #define T_PROBE_RETRY_SLOW msecs_to_jiffies(5000) |
| 203 | #define R_PROBE_RETRY_SLOW 12 |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 204 | |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 205 | /* SFP modules appear to always have their PHY configured for bus address |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 206 | * 0x56 (which with mdio-i2c, translates to a PHY address of 22). |
| 207 | */ |
| 208 | #define SFP_PHY_ADDR 22 |
| 209 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 210 | struct sff_data { |
| 211 | unsigned int gpios; |
| 212 | bool (*module_supported)(const struct sfp_eeprom_id *id); |
| 213 | }; |
| 214 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 215 | struct sfp { |
| 216 | struct device *dev; |
| 217 | struct i2c_adapter *i2c; |
| 218 | struct mii_bus *i2c_mii; |
| 219 | struct sfp_bus *sfp_bus; |
| 220 | struct phy_device *mod_phy; |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 221 | const struct sff_data *type; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 222 | u32 max_power_mW; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 223 | |
| 224 | unsigned int (*get_state)(struct sfp *); |
| 225 | void (*set_state)(struct sfp *, unsigned int); |
| 226 | int (*read)(struct sfp *, bool, u8, void *, size_t); |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 227 | int (*write)(struct sfp *, bool, u8, void *, size_t); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 228 | |
| 229 | struct gpio_desc *gpio[GPIO_MAX]; |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 230 | int gpio_irq[GPIO_MAX]; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 231 | |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 232 | bool need_poll; |
| 233 | |
Robert Hancock | 2158e85 | 2019-06-07 10:42:36 -0600 | [diff] [blame] | 234 | struct mutex st_mutex; /* Protects state */ |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 235 | unsigned int state_soft_mask; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 236 | unsigned int state; |
| 237 | struct delayed_work poll; |
| 238 | struct delayed_work timeout; |
Robert Hancock | 2158e85 | 2019-06-07 10:42:36 -0600 | [diff] [blame] | 239 | struct mutex sm_mutex; /* Protects state machine */ |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 240 | unsigned char sm_mod_state; |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 241 | unsigned char sm_mod_tries_init; |
| 242 | unsigned char sm_mod_tries; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 243 | unsigned char sm_dev_state; |
| 244 | unsigned short sm_state; |
Russell King | 281e4ea | 2019-12-09 14:16:00 +0000 | [diff] [blame] | 245 | unsigned char sm_fault_retries; |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 246 | unsigned char sm_phy_retries; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 247 | |
| 248 | struct sfp_eeprom_id id; |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 249 | unsigned int module_power_mW; |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 250 | unsigned int module_t_start_up; |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 251 | |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 252 | #if IS_ENABLED(CONFIG_HWMON) |
| 253 | struct sfp_diag diag; |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 254 | struct delayed_work hwmon_probe; |
| 255 | unsigned int hwmon_tries; |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 256 | struct device *hwmon_dev; |
| 257 | char *hwmon_name; |
| 258 | #endif |
| 259 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 260 | }; |
| 261 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 262 | static bool sff_module_supported(const struct sfp_eeprom_id *id) |
| 263 | { |
Russell King | 0fbd26a | 2019-12-11 10:56:04 +0000 | [diff] [blame] | 264 | return id->base.phys_id == SFF8024_ID_SFF_8472 && |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 265 | id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; |
| 266 | } |
| 267 | |
| 268 | static const struct sff_data sff_data = { |
| 269 | .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE, |
| 270 | .module_supported = sff_module_supported, |
| 271 | }; |
| 272 | |
| 273 | static bool sfp_module_supported(const struct sfp_eeprom_id *id) |
| 274 | { |
Russell King | 0fbd26a | 2019-12-11 10:56:04 +0000 | [diff] [blame] | 275 | return id->base.phys_id == SFF8024_ID_SFP && |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 276 | id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP; |
| 277 | } |
| 278 | |
| 279 | static const struct sff_data sfp_data = { |
| 280 | .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT | |
| 281 | SFP_F_TX_DISABLE | SFP_F_RATE_SELECT, |
| 282 | .module_supported = sfp_module_supported, |
| 283 | }; |
| 284 | |
| 285 | static const struct of_device_id sfp_of_match[] = { |
| 286 | { .compatible = "sff,sff", .data = &sff_data, }, |
| 287 | { .compatible = "sff,sfp", .data = &sfp_data, }, |
| 288 | { }, |
| 289 | }; |
| 290 | MODULE_DEVICE_TABLE(of, sfp_of_match); |
| 291 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 292 | static unsigned long poll_jiffies; |
| 293 | |
| 294 | static unsigned int sfp_gpio_get_state(struct sfp *sfp) |
| 295 | { |
| 296 | unsigned int i, state, v; |
| 297 | |
| 298 | for (i = state = 0; i < GPIO_MAX; i++) { |
| 299 | if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) |
| 300 | continue; |
| 301 | |
| 302 | v = gpiod_get_value_cansleep(sfp->gpio[i]); |
| 303 | if (v) |
| 304 | state |= BIT(i); |
| 305 | } |
| 306 | |
| 307 | return state; |
| 308 | } |
| 309 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 310 | static unsigned int sff_gpio_get_state(struct sfp *sfp) |
| 311 | { |
| 312 | return sfp_gpio_get_state(sfp) | SFP_F_PRESENT; |
| 313 | } |
| 314 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 315 | static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state) |
| 316 | { |
| 317 | if (state & SFP_F_PRESENT) { |
| 318 | /* If the module is present, drive the signals */ |
| 319 | if (sfp->gpio[GPIO_TX_DISABLE]) |
| 320 | gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE], |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 321 | state & SFP_F_TX_DISABLE); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 322 | if (state & SFP_F_RATE_SELECT) |
| 323 | gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT], |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 324 | state & SFP_F_RATE_SELECT); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 325 | } else { |
| 326 | /* Otherwise, let them float to the pull-ups */ |
| 327 | if (sfp->gpio[GPIO_TX_DISABLE]) |
| 328 | gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]); |
| 329 | if (state & SFP_F_RATE_SELECT) |
| 330 | gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]); |
| 331 | } |
| 332 | } |
| 333 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 334 | static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, |
| 335 | size_t len) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 336 | { |
| 337 | struct i2c_msg msgs[2]; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 338 | u8 bus_addr = a2 ? 0x51 : 0x50; |
Russell King | 28e74a7 | 2019-06-02 15:13:00 +0100 | [diff] [blame] | 339 | size_t this_len; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 340 | int ret; |
| 341 | |
| 342 | msgs[0].addr = bus_addr; |
| 343 | msgs[0].flags = 0; |
| 344 | msgs[0].len = 1; |
| 345 | msgs[0].buf = &dev_addr; |
| 346 | msgs[1].addr = bus_addr; |
| 347 | msgs[1].flags = I2C_M_RD; |
| 348 | msgs[1].len = len; |
| 349 | msgs[1].buf = buf; |
| 350 | |
Russell King | 28e74a7 | 2019-06-02 15:13:00 +0100 | [diff] [blame] | 351 | while (len) { |
| 352 | this_len = len; |
| 353 | if (this_len > 16) |
| 354 | this_len = 16; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 355 | |
Russell King | 28e74a7 | 2019-06-02 15:13:00 +0100 | [diff] [blame] | 356 | msgs[1].len = this_len; |
| 357 | |
| 358 | ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); |
| 359 | if (ret < 0) |
| 360 | return ret; |
| 361 | |
| 362 | if (ret != ARRAY_SIZE(msgs)) |
| 363 | break; |
| 364 | |
| 365 | msgs[1].buf += this_len; |
| 366 | dev_addr += this_len; |
| 367 | len -= this_len; |
| 368 | } |
| 369 | |
| 370 | return msgs[1].buf - (u8 *)buf; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 373 | static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf, |
| 374 | size_t len) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 375 | { |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 376 | struct i2c_msg msgs[1]; |
| 377 | u8 bus_addr = a2 ? 0x51 : 0x50; |
| 378 | int ret; |
| 379 | |
| 380 | msgs[0].addr = bus_addr; |
| 381 | msgs[0].flags = 0; |
| 382 | msgs[0].len = 1 + len; |
| 383 | msgs[0].buf = kmalloc(1 + len, GFP_KERNEL); |
| 384 | if (!msgs[0].buf) |
| 385 | return -ENOMEM; |
| 386 | |
| 387 | msgs[0].buf[0] = dev_addr; |
| 388 | memcpy(&msgs[0].buf[1], buf, len); |
| 389 | |
| 390 | ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs)); |
| 391 | |
| 392 | kfree(msgs[0].buf); |
| 393 | |
| 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | |
| 397 | return ret == ARRAY_SIZE(msgs) ? len : 0; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c) |
| 401 | { |
| 402 | struct mii_bus *i2c_mii; |
| 403 | int ret; |
| 404 | |
| 405 | if (!i2c_check_functionality(i2c, I2C_FUNC_I2C)) |
| 406 | return -EINVAL; |
| 407 | |
| 408 | sfp->i2c = i2c; |
| 409 | sfp->read = sfp_i2c_read; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 410 | sfp->write = sfp_i2c_write; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 411 | |
| 412 | i2c_mii = mdio_i2c_alloc(sfp->dev, i2c); |
| 413 | if (IS_ERR(i2c_mii)) |
| 414 | return PTR_ERR(i2c_mii); |
| 415 | |
| 416 | i2c_mii->name = "SFP I2C Bus"; |
| 417 | i2c_mii->phy_mask = ~0; |
| 418 | |
| 419 | ret = mdiobus_register(i2c_mii); |
| 420 | if (ret < 0) { |
| 421 | mdiobus_free(i2c_mii); |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | sfp->i2c_mii = i2c_mii; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 430 | /* Interface */ |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 431 | static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) |
| 432 | { |
| 433 | return sfp->read(sfp, a2, addr, buf, len); |
| 434 | } |
| 435 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 436 | static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len) |
| 437 | { |
| 438 | return sfp->write(sfp, a2, addr, buf, len); |
| 439 | } |
| 440 | |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 441 | static unsigned int sfp_soft_get_state(struct sfp *sfp) |
| 442 | { |
| 443 | unsigned int state = 0; |
| 444 | u8 status; |
Russell King | 0dea4d0 | 2019-12-17 13:50:29 +0000 | [diff] [blame] | 445 | int ret; |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 446 | |
Russell King | 0dea4d0 | 2019-12-17 13:50:29 +0000 | [diff] [blame] | 447 | ret = sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)); |
| 448 | if (ret == sizeof(status)) { |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 449 | if (status & SFP_STATUS_RX_LOS) |
| 450 | state |= SFP_F_LOS; |
| 451 | if (status & SFP_STATUS_TX_FAULT) |
| 452 | state |= SFP_F_TX_FAULT; |
Russell King | 0dea4d0 | 2019-12-17 13:50:29 +0000 | [diff] [blame] | 453 | } else { |
| 454 | dev_err_ratelimited(sfp->dev, |
| 455 | "failed to read SFP soft status: %d\n", |
| 456 | ret); |
| 457 | /* Preserve the current state */ |
| 458 | state = sfp->state; |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | return state & sfp->state_soft_mask; |
| 462 | } |
| 463 | |
| 464 | static void sfp_soft_set_state(struct sfp *sfp, unsigned int state) |
| 465 | { |
| 466 | u8 status; |
| 467 | |
| 468 | if (sfp_read(sfp, true, SFP_STATUS, &status, sizeof(status)) == |
| 469 | sizeof(status)) { |
| 470 | if (state & SFP_F_TX_DISABLE) |
| 471 | status |= SFP_STATUS_TX_DISABLE_FORCE; |
| 472 | else |
| 473 | status &= ~SFP_STATUS_TX_DISABLE_FORCE; |
| 474 | |
| 475 | sfp_write(sfp, true, SFP_STATUS, &status, sizeof(status)); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | static void sfp_soft_start_poll(struct sfp *sfp) |
| 480 | { |
| 481 | const struct sfp_eeprom_id *id = &sfp->id; |
| 482 | |
| 483 | sfp->state_soft_mask = 0; |
| 484 | if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_DISABLE && |
| 485 | !sfp->gpio[GPIO_TX_DISABLE]) |
| 486 | sfp->state_soft_mask |= SFP_F_TX_DISABLE; |
| 487 | if (id->ext.enhopts & SFP_ENHOPTS_SOFT_TX_FAULT && |
| 488 | !sfp->gpio[GPIO_TX_FAULT]) |
| 489 | sfp->state_soft_mask |= SFP_F_TX_FAULT; |
| 490 | if (id->ext.enhopts & SFP_ENHOPTS_SOFT_RX_LOS && |
| 491 | !sfp->gpio[GPIO_LOS]) |
| 492 | sfp->state_soft_mask |= SFP_F_LOS; |
| 493 | |
| 494 | if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) && |
| 495 | !sfp->need_poll) |
| 496 | mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); |
| 497 | } |
| 498 | |
| 499 | static void sfp_soft_stop_poll(struct sfp *sfp) |
| 500 | { |
| 501 | sfp->state_soft_mask = 0; |
| 502 | } |
| 503 | |
| 504 | static unsigned int sfp_get_state(struct sfp *sfp) |
| 505 | { |
| 506 | unsigned int state = sfp->get_state(sfp); |
| 507 | |
| 508 | if (state & SFP_F_PRESENT && |
| 509 | sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT)) |
| 510 | state |= sfp_soft_get_state(sfp); |
| 511 | |
| 512 | return state; |
| 513 | } |
| 514 | |
| 515 | static void sfp_set_state(struct sfp *sfp, unsigned int state) |
| 516 | { |
| 517 | sfp->set_state(sfp, state); |
| 518 | |
| 519 | if (state & SFP_F_PRESENT && |
| 520 | sfp->state_soft_mask & SFP_F_TX_DISABLE) |
| 521 | sfp_soft_set_state(sfp, state); |
| 522 | } |
| 523 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 524 | static unsigned int sfp_check(void *buf, size_t len) |
| 525 | { |
| 526 | u8 *p, check; |
| 527 | |
| 528 | for (p = buf, check = 0; len; p++, len--) |
| 529 | check += *p; |
| 530 | |
| 531 | return check; |
| 532 | } |
| 533 | |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 534 | /* hwmon */ |
| 535 | #if IS_ENABLED(CONFIG_HWMON) |
| 536 | static umode_t sfp_hwmon_is_visible(const void *data, |
| 537 | enum hwmon_sensor_types type, |
| 538 | u32 attr, int channel) |
| 539 | { |
| 540 | const struct sfp *sfp = data; |
| 541 | |
| 542 | switch (type) { |
| 543 | case hwmon_temp: |
| 544 | switch (attr) { |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 545 | case hwmon_temp_min_alarm: |
| 546 | case hwmon_temp_max_alarm: |
| 547 | case hwmon_temp_lcrit_alarm: |
| 548 | case hwmon_temp_crit_alarm: |
| 549 | case hwmon_temp_min: |
| 550 | case hwmon_temp_max: |
| 551 | case hwmon_temp_lcrit: |
| 552 | case hwmon_temp_crit: |
Andrew Lunn | a33710b | 2018-09-04 04:23:56 +0200 | [diff] [blame] | 553 | if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) |
| 554 | return 0; |
| 555 | /* fall through */ |
| 556 | case hwmon_temp_input: |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 557 | case hwmon_temp_label: |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 558 | return 0444; |
| 559 | default: |
| 560 | return 0; |
| 561 | } |
| 562 | case hwmon_in: |
| 563 | switch (attr) { |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 564 | case hwmon_in_min_alarm: |
| 565 | case hwmon_in_max_alarm: |
| 566 | case hwmon_in_lcrit_alarm: |
| 567 | case hwmon_in_crit_alarm: |
| 568 | case hwmon_in_min: |
| 569 | case hwmon_in_max: |
| 570 | case hwmon_in_lcrit: |
| 571 | case hwmon_in_crit: |
Andrew Lunn | a33710b | 2018-09-04 04:23:56 +0200 | [diff] [blame] | 572 | if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) |
| 573 | return 0; |
| 574 | /* fall through */ |
| 575 | case hwmon_in_input: |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 576 | case hwmon_in_label: |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 577 | return 0444; |
| 578 | default: |
| 579 | return 0; |
| 580 | } |
| 581 | case hwmon_curr: |
| 582 | switch (attr) { |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 583 | case hwmon_curr_min_alarm: |
| 584 | case hwmon_curr_max_alarm: |
| 585 | case hwmon_curr_lcrit_alarm: |
| 586 | case hwmon_curr_crit_alarm: |
| 587 | case hwmon_curr_min: |
| 588 | case hwmon_curr_max: |
| 589 | case hwmon_curr_lcrit: |
| 590 | case hwmon_curr_crit: |
Andrew Lunn | a33710b | 2018-09-04 04:23:56 +0200 | [diff] [blame] | 591 | if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) |
| 592 | return 0; |
| 593 | /* fall through */ |
| 594 | case hwmon_curr_input: |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 595 | case hwmon_curr_label: |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 596 | return 0444; |
| 597 | default: |
| 598 | return 0; |
| 599 | } |
| 600 | case hwmon_power: |
| 601 | /* External calibration of receive power requires |
| 602 | * floating point arithmetic. Doing that in the kernel |
| 603 | * is not easy, so just skip it. If the module does |
| 604 | * not require external calibration, we can however |
| 605 | * show receiver power, since FP is then not needed. |
| 606 | */ |
| 607 | if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL && |
| 608 | channel == 1) |
| 609 | return 0; |
| 610 | switch (attr) { |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 611 | case hwmon_power_min_alarm: |
| 612 | case hwmon_power_max_alarm: |
| 613 | case hwmon_power_lcrit_alarm: |
| 614 | case hwmon_power_crit_alarm: |
| 615 | case hwmon_power_min: |
| 616 | case hwmon_power_max: |
| 617 | case hwmon_power_lcrit: |
| 618 | case hwmon_power_crit: |
Andrew Lunn | a33710b | 2018-09-04 04:23:56 +0200 | [diff] [blame] | 619 | if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN)) |
| 620 | return 0; |
| 621 | /* fall through */ |
| 622 | case hwmon_power_input: |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 623 | case hwmon_power_label: |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 624 | return 0444; |
| 625 | default: |
| 626 | return 0; |
| 627 | } |
| 628 | default: |
| 629 | return 0; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value) |
| 634 | { |
| 635 | __be16 val; |
| 636 | int err; |
| 637 | |
| 638 | err = sfp_read(sfp, true, reg, &val, sizeof(val)); |
| 639 | if (err < 0) |
| 640 | return err; |
| 641 | |
| 642 | *value = be16_to_cpu(val); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static void sfp_hwmon_to_rx_power(long *value) |
| 648 | { |
Andrew Lunn | 0cea0e1 | 2019-07-21 18:50:08 +0200 | [diff] [blame] | 649 | *value = DIV_ROUND_CLOSEST(*value, 10); |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset, |
| 653 | long *value) |
| 654 | { |
| 655 | if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL) |
| 656 | *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset; |
| 657 | } |
| 658 | |
| 659 | static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value) |
| 660 | { |
| 661 | sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope), |
| 662 | be16_to_cpu(sfp->diag.cal_t_offset), value); |
| 663 | |
| 664 | if (*value >= 0x8000) |
| 665 | *value -= 0x10000; |
| 666 | |
| 667 | *value = DIV_ROUND_CLOSEST(*value * 1000, 256); |
| 668 | } |
| 669 | |
| 670 | static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value) |
| 671 | { |
| 672 | sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope), |
| 673 | be16_to_cpu(sfp->diag.cal_v_offset), value); |
| 674 | |
| 675 | *value = DIV_ROUND_CLOSEST(*value, 10); |
| 676 | } |
| 677 | |
| 678 | static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value) |
| 679 | { |
| 680 | sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope), |
| 681 | be16_to_cpu(sfp->diag.cal_txi_offset), value); |
| 682 | |
| 683 | *value = DIV_ROUND_CLOSEST(*value, 500); |
| 684 | } |
| 685 | |
| 686 | static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value) |
| 687 | { |
| 688 | sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope), |
| 689 | be16_to_cpu(sfp->diag.cal_txpwr_offset), value); |
| 690 | |
| 691 | *value = DIV_ROUND_CLOSEST(*value, 10); |
| 692 | } |
| 693 | |
| 694 | static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value) |
| 695 | { |
| 696 | int err; |
| 697 | |
| 698 | err = sfp_hwmon_read_sensor(sfp, reg, value); |
| 699 | if (err < 0) |
| 700 | return err; |
| 701 | |
| 702 | sfp_hwmon_calibrate_temp(sfp, value); |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value) |
| 708 | { |
| 709 | int err; |
| 710 | |
| 711 | err = sfp_hwmon_read_sensor(sfp, reg, value); |
| 712 | if (err < 0) |
| 713 | return err; |
| 714 | |
| 715 | sfp_hwmon_calibrate_vcc(sfp, value); |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value) |
| 721 | { |
| 722 | int err; |
| 723 | |
| 724 | err = sfp_hwmon_read_sensor(sfp, reg, value); |
| 725 | if (err < 0) |
| 726 | return err; |
| 727 | |
| 728 | sfp_hwmon_calibrate_bias(sfp, value); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value) |
| 734 | { |
| 735 | int err; |
| 736 | |
| 737 | err = sfp_hwmon_read_sensor(sfp, reg, value); |
| 738 | if (err < 0) |
| 739 | return err; |
| 740 | |
| 741 | sfp_hwmon_calibrate_tx_power(sfp, value); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value) |
| 747 | { |
| 748 | int err; |
| 749 | |
| 750 | err = sfp_hwmon_read_sensor(sfp, reg, value); |
| 751 | if (err < 0) |
| 752 | return err; |
| 753 | |
| 754 | sfp_hwmon_to_rx_power(value); |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value) |
| 760 | { |
| 761 | u8 status; |
| 762 | int err; |
| 763 | |
| 764 | switch (attr) { |
| 765 | case hwmon_temp_input: |
| 766 | return sfp_hwmon_read_temp(sfp, SFP_TEMP, value); |
| 767 | |
| 768 | case hwmon_temp_lcrit: |
| 769 | *value = be16_to_cpu(sfp->diag.temp_low_alarm); |
| 770 | sfp_hwmon_calibrate_temp(sfp, value); |
| 771 | return 0; |
| 772 | |
| 773 | case hwmon_temp_min: |
| 774 | *value = be16_to_cpu(sfp->diag.temp_low_warn); |
| 775 | sfp_hwmon_calibrate_temp(sfp, value); |
| 776 | return 0; |
| 777 | case hwmon_temp_max: |
| 778 | *value = be16_to_cpu(sfp->diag.temp_high_warn); |
| 779 | sfp_hwmon_calibrate_temp(sfp, value); |
| 780 | return 0; |
| 781 | |
| 782 | case hwmon_temp_crit: |
| 783 | *value = be16_to_cpu(sfp->diag.temp_high_alarm); |
| 784 | sfp_hwmon_calibrate_temp(sfp, value); |
| 785 | return 0; |
| 786 | |
| 787 | case hwmon_temp_lcrit_alarm: |
| 788 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 789 | if (err < 0) |
| 790 | return err; |
| 791 | |
| 792 | *value = !!(status & SFP_ALARM0_TEMP_LOW); |
| 793 | return 0; |
| 794 | |
| 795 | case hwmon_temp_min_alarm: |
| 796 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 797 | if (err < 0) |
| 798 | return err; |
| 799 | |
| 800 | *value = !!(status & SFP_WARN0_TEMP_LOW); |
| 801 | return 0; |
| 802 | |
| 803 | case hwmon_temp_max_alarm: |
| 804 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 805 | if (err < 0) |
| 806 | return err; |
| 807 | |
| 808 | *value = !!(status & SFP_WARN0_TEMP_HIGH); |
| 809 | return 0; |
| 810 | |
| 811 | case hwmon_temp_crit_alarm: |
| 812 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 813 | if (err < 0) |
| 814 | return err; |
| 815 | |
| 816 | *value = !!(status & SFP_ALARM0_TEMP_HIGH); |
| 817 | return 0; |
| 818 | default: |
| 819 | return -EOPNOTSUPP; |
| 820 | } |
| 821 | |
| 822 | return -EOPNOTSUPP; |
| 823 | } |
| 824 | |
| 825 | static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value) |
| 826 | { |
| 827 | u8 status; |
| 828 | int err; |
| 829 | |
| 830 | switch (attr) { |
| 831 | case hwmon_in_input: |
| 832 | return sfp_hwmon_read_vcc(sfp, SFP_VCC, value); |
| 833 | |
| 834 | case hwmon_in_lcrit: |
| 835 | *value = be16_to_cpu(sfp->diag.volt_low_alarm); |
| 836 | sfp_hwmon_calibrate_vcc(sfp, value); |
| 837 | return 0; |
| 838 | |
| 839 | case hwmon_in_min: |
| 840 | *value = be16_to_cpu(sfp->diag.volt_low_warn); |
| 841 | sfp_hwmon_calibrate_vcc(sfp, value); |
| 842 | return 0; |
| 843 | |
| 844 | case hwmon_in_max: |
| 845 | *value = be16_to_cpu(sfp->diag.volt_high_warn); |
| 846 | sfp_hwmon_calibrate_vcc(sfp, value); |
| 847 | return 0; |
| 848 | |
| 849 | case hwmon_in_crit: |
| 850 | *value = be16_to_cpu(sfp->diag.volt_high_alarm); |
| 851 | sfp_hwmon_calibrate_vcc(sfp, value); |
| 852 | return 0; |
| 853 | |
| 854 | case hwmon_in_lcrit_alarm: |
| 855 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 856 | if (err < 0) |
| 857 | return err; |
| 858 | |
| 859 | *value = !!(status & SFP_ALARM0_VCC_LOW); |
| 860 | return 0; |
| 861 | |
| 862 | case hwmon_in_min_alarm: |
| 863 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 864 | if (err < 0) |
| 865 | return err; |
| 866 | |
| 867 | *value = !!(status & SFP_WARN0_VCC_LOW); |
| 868 | return 0; |
| 869 | |
| 870 | case hwmon_in_max_alarm: |
| 871 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 872 | if (err < 0) |
| 873 | return err; |
| 874 | |
| 875 | *value = !!(status & SFP_WARN0_VCC_HIGH); |
| 876 | return 0; |
| 877 | |
| 878 | case hwmon_in_crit_alarm: |
| 879 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 880 | if (err < 0) |
| 881 | return err; |
| 882 | |
| 883 | *value = !!(status & SFP_ALARM0_VCC_HIGH); |
| 884 | return 0; |
| 885 | default: |
| 886 | return -EOPNOTSUPP; |
| 887 | } |
| 888 | |
| 889 | return -EOPNOTSUPP; |
| 890 | } |
| 891 | |
| 892 | static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value) |
| 893 | { |
| 894 | u8 status; |
| 895 | int err; |
| 896 | |
| 897 | switch (attr) { |
| 898 | case hwmon_curr_input: |
| 899 | return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value); |
| 900 | |
| 901 | case hwmon_curr_lcrit: |
| 902 | *value = be16_to_cpu(sfp->diag.bias_low_alarm); |
| 903 | sfp_hwmon_calibrate_bias(sfp, value); |
| 904 | return 0; |
| 905 | |
| 906 | case hwmon_curr_min: |
| 907 | *value = be16_to_cpu(sfp->diag.bias_low_warn); |
| 908 | sfp_hwmon_calibrate_bias(sfp, value); |
| 909 | return 0; |
| 910 | |
| 911 | case hwmon_curr_max: |
| 912 | *value = be16_to_cpu(sfp->diag.bias_high_warn); |
| 913 | sfp_hwmon_calibrate_bias(sfp, value); |
| 914 | return 0; |
| 915 | |
| 916 | case hwmon_curr_crit: |
| 917 | *value = be16_to_cpu(sfp->diag.bias_high_alarm); |
| 918 | sfp_hwmon_calibrate_bias(sfp, value); |
| 919 | return 0; |
| 920 | |
| 921 | case hwmon_curr_lcrit_alarm: |
| 922 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 923 | if (err < 0) |
| 924 | return err; |
| 925 | |
| 926 | *value = !!(status & SFP_ALARM0_TX_BIAS_LOW); |
| 927 | return 0; |
| 928 | |
| 929 | case hwmon_curr_min_alarm: |
| 930 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 931 | if (err < 0) |
| 932 | return err; |
| 933 | |
| 934 | *value = !!(status & SFP_WARN0_TX_BIAS_LOW); |
| 935 | return 0; |
| 936 | |
| 937 | case hwmon_curr_max_alarm: |
| 938 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 939 | if (err < 0) |
| 940 | return err; |
| 941 | |
| 942 | *value = !!(status & SFP_WARN0_TX_BIAS_HIGH); |
| 943 | return 0; |
| 944 | |
| 945 | case hwmon_curr_crit_alarm: |
| 946 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 947 | if (err < 0) |
| 948 | return err; |
| 949 | |
| 950 | *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH); |
| 951 | return 0; |
| 952 | default: |
| 953 | return -EOPNOTSUPP; |
| 954 | } |
| 955 | |
| 956 | return -EOPNOTSUPP; |
| 957 | } |
| 958 | |
| 959 | static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value) |
| 960 | { |
| 961 | u8 status; |
| 962 | int err; |
| 963 | |
| 964 | switch (attr) { |
| 965 | case hwmon_power_input: |
| 966 | return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value); |
| 967 | |
| 968 | case hwmon_power_lcrit: |
| 969 | *value = be16_to_cpu(sfp->diag.txpwr_low_alarm); |
| 970 | sfp_hwmon_calibrate_tx_power(sfp, value); |
| 971 | return 0; |
| 972 | |
| 973 | case hwmon_power_min: |
| 974 | *value = be16_to_cpu(sfp->diag.txpwr_low_warn); |
| 975 | sfp_hwmon_calibrate_tx_power(sfp, value); |
| 976 | return 0; |
| 977 | |
| 978 | case hwmon_power_max: |
| 979 | *value = be16_to_cpu(sfp->diag.txpwr_high_warn); |
| 980 | sfp_hwmon_calibrate_tx_power(sfp, value); |
| 981 | return 0; |
| 982 | |
| 983 | case hwmon_power_crit: |
| 984 | *value = be16_to_cpu(sfp->diag.txpwr_high_alarm); |
| 985 | sfp_hwmon_calibrate_tx_power(sfp, value); |
| 986 | return 0; |
| 987 | |
| 988 | case hwmon_power_lcrit_alarm: |
| 989 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 990 | if (err < 0) |
| 991 | return err; |
| 992 | |
| 993 | *value = !!(status & SFP_ALARM0_TXPWR_LOW); |
| 994 | return 0; |
| 995 | |
| 996 | case hwmon_power_min_alarm: |
| 997 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 998 | if (err < 0) |
| 999 | return err; |
| 1000 | |
| 1001 | *value = !!(status & SFP_WARN0_TXPWR_LOW); |
| 1002 | return 0; |
| 1003 | |
| 1004 | case hwmon_power_max_alarm: |
| 1005 | err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status)); |
| 1006 | if (err < 0) |
| 1007 | return err; |
| 1008 | |
| 1009 | *value = !!(status & SFP_WARN0_TXPWR_HIGH); |
| 1010 | return 0; |
| 1011 | |
| 1012 | case hwmon_power_crit_alarm: |
| 1013 | err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status)); |
| 1014 | if (err < 0) |
| 1015 | return err; |
| 1016 | |
| 1017 | *value = !!(status & SFP_ALARM0_TXPWR_HIGH); |
| 1018 | return 0; |
| 1019 | default: |
| 1020 | return -EOPNOTSUPP; |
| 1021 | } |
| 1022 | |
| 1023 | return -EOPNOTSUPP; |
| 1024 | } |
| 1025 | |
| 1026 | static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value) |
| 1027 | { |
| 1028 | u8 status; |
| 1029 | int err; |
| 1030 | |
| 1031 | switch (attr) { |
| 1032 | case hwmon_power_input: |
| 1033 | return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value); |
| 1034 | |
| 1035 | case hwmon_power_lcrit: |
| 1036 | *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm); |
| 1037 | sfp_hwmon_to_rx_power(value); |
| 1038 | return 0; |
| 1039 | |
| 1040 | case hwmon_power_min: |
| 1041 | *value = be16_to_cpu(sfp->diag.rxpwr_low_warn); |
| 1042 | sfp_hwmon_to_rx_power(value); |
| 1043 | return 0; |
| 1044 | |
| 1045 | case hwmon_power_max: |
| 1046 | *value = be16_to_cpu(sfp->diag.rxpwr_high_warn); |
| 1047 | sfp_hwmon_to_rx_power(value); |
| 1048 | return 0; |
| 1049 | |
| 1050 | case hwmon_power_crit: |
| 1051 | *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm); |
| 1052 | sfp_hwmon_to_rx_power(value); |
| 1053 | return 0; |
| 1054 | |
| 1055 | case hwmon_power_lcrit_alarm: |
| 1056 | err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); |
| 1057 | if (err < 0) |
| 1058 | return err; |
| 1059 | |
| 1060 | *value = !!(status & SFP_ALARM1_RXPWR_LOW); |
| 1061 | return 0; |
| 1062 | |
| 1063 | case hwmon_power_min_alarm: |
| 1064 | err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); |
| 1065 | if (err < 0) |
| 1066 | return err; |
| 1067 | |
| 1068 | *value = !!(status & SFP_WARN1_RXPWR_LOW); |
| 1069 | return 0; |
| 1070 | |
| 1071 | case hwmon_power_max_alarm: |
| 1072 | err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status)); |
| 1073 | if (err < 0) |
| 1074 | return err; |
| 1075 | |
| 1076 | *value = !!(status & SFP_WARN1_RXPWR_HIGH); |
| 1077 | return 0; |
| 1078 | |
| 1079 | case hwmon_power_crit_alarm: |
| 1080 | err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status)); |
| 1081 | if (err < 0) |
| 1082 | return err; |
| 1083 | |
| 1084 | *value = !!(status & SFP_ALARM1_RXPWR_HIGH); |
| 1085 | return 0; |
| 1086 | default: |
| 1087 | return -EOPNOTSUPP; |
| 1088 | } |
| 1089 | |
| 1090 | return -EOPNOTSUPP; |
| 1091 | } |
| 1092 | |
| 1093 | static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type, |
| 1094 | u32 attr, int channel, long *value) |
| 1095 | { |
| 1096 | struct sfp *sfp = dev_get_drvdata(dev); |
| 1097 | |
| 1098 | switch (type) { |
| 1099 | case hwmon_temp: |
| 1100 | return sfp_hwmon_temp(sfp, attr, value); |
| 1101 | case hwmon_in: |
| 1102 | return sfp_hwmon_vcc(sfp, attr, value); |
| 1103 | case hwmon_curr: |
| 1104 | return sfp_hwmon_bias(sfp, attr, value); |
| 1105 | case hwmon_power: |
| 1106 | switch (channel) { |
| 1107 | case 0: |
| 1108 | return sfp_hwmon_tx_power(sfp, attr, value); |
| 1109 | case 1: |
| 1110 | return sfp_hwmon_rx_power(sfp, attr, value); |
| 1111 | default: |
| 1112 | return -EOPNOTSUPP; |
| 1113 | } |
| 1114 | default: |
| 1115 | return -EOPNOTSUPP; |
| 1116 | } |
| 1117 | } |
| 1118 | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1119 | static const char *const sfp_hwmon_power_labels[] = { |
| 1120 | "TX_power", |
| 1121 | "RX_power", |
| 1122 | }; |
| 1123 | |
| 1124 | static int sfp_hwmon_read_string(struct device *dev, |
| 1125 | enum hwmon_sensor_types type, |
| 1126 | u32 attr, int channel, const char **str) |
| 1127 | { |
| 1128 | switch (type) { |
| 1129 | case hwmon_curr: |
| 1130 | switch (attr) { |
| 1131 | case hwmon_curr_label: |
| 1132 | *str = "bias"; |
| 1133 | return 0; |
| 1134 | default: |
| 1135 | return -EOPNOTSUPP; |
| 1136 | } |
| 1137 | break; |
| 1138 | case hwmon_temp: |
| 1139 | switch (attr) { |
| 1140 | case hwmon_temp_label: |
| 1141 | *str = "temperature"; |
| 1142 | return 0; |
| 1143 | default: |
| 1144 | return -EOPNOTSUPP; |
| 1145 | } |
| 1146 | break; |
| 1147 | case hwmon_in: |
| 1148 | switch (attr) { |
| 1149 | case hwmon_in_label: |
| 1150 | *str = "VCC"; |
| 1151 | return 0; |
| 1152 | default: |
| 1153 | return -EOPNOTSUPP; |
| 1154 | } |
| 1155 | break; |
| 1156 | case hwmon_power: |
| 1157 | switch (attr) { |
| 1158 | case hwmon_power_label: |
| 1159 | *str = sfp_hwmon_power_labels[channel]; |
| 1160 | return 0; |
| 1161 | default: |
| 1162 | return -EOPNOTSUPP; |
| 1163 | } |
| 1164 | break; |
| 1165 | default: |
| 1166 | return -EOPNOTSUPP; |
| 1167 | } |
| 1168 | |
| 1169 | return -EOPNOTSUPP; |
| 1170 | } |
| 1171 | |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1172 | static const struct hwmon_ops sfp_hwmon_ops = { |
| 1173 | .is_visible = sfp_hwmon_is_visible, |
| 1174 | .read = sfp_hwmon_read, |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1175 | .read_string = sfp_hwmon_read_string, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1176 | }; |
| 1177 | |
| 1178 | static u32 sfp_hwmon_chip_config[] = { |
| 1179 | HWMON_C_REGISTER_TZ, |
| 1180 | 0, |
| 1181 | }; |
| 1182 | |
| 1183 | static const struct hwmon_channel_info sfp_hwmon_chip = { |
| 1184 | .type = hwmon_chip, |
| 1185 | .config = sfp_hwmon_chip_config, |
| 1186 | }; |
| 1187 | |
| 1188 | static u32 sfp_hwmon_temp_config[] = { |
| 1189 | HWMON_T_INPUT | |
| 1190 | HWMON_T_MAX | HWMON_T_MIN | |
| 1191 | HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM | |
| 1192 | HWMON_T_CRIT | HWMON_T_LCRIT | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1193 | HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM | |
| 1194 | HWMON_T_LABEL, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1195 | 0, |
| 1196 | }; |
| 1197 | |
| 1198 | static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = { |
| 1199 | .type = hwmon_temp, |
| 1200 | .config = sfp_hwmon_temp_config, |
| 1201 | }; |
| 1202 | |
| 1203 | static u32 sfp_hwmon_vcc_config[] = { |
| 1204 | HWMON_I_INPUT | |
| 1205 | HWMON_I_MAX | HWMON_I_MIN | |
| 1206 | HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM | |
| 1207 | HWMON_I_CRIT | HWMON_I_LCRIT | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1208 | HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM | |
| 1209 | HWMON_I_LABEL, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1210 | 0, |
| 1211 | }; |
| 1212 | |
| 1213 | static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = { |
| 1214 | .type = hwmon_in, |
| 1215 | .config = sfp_hwmon_vcc_config, |
| 1216 | }; |
| 1217 | |
| 1218 | static u32 sfp_hwmon_bias_config[] = { |
| 1219 | HWMON_C_INPUT | |
| 1220 | HWMON_C_MAX | HWMON_C_MIN | |
| 1221 | HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM | |
| 1222 | HWMON_C_CRIT | HWMON_C_LCRIT | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1223 | HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM | |
| 1224 | HWMON_C_LABEL, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1225 | 0, |
| 1226 | }; |
| 1227 | |
| 1228 | static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = { |
| 1229 | .type = hwmon_curr, |
| 1230 | .config = sfp_hwmon_bias_config, |
| 1231 | }; |
| 1232 | |
| 1233 | static u32 sfp_hwmon_power_config[] = { |
| 1234 | /* Transmit power */ |
| 1235 | HWMON_P_INPUT | |
| 1236 | HWMON_P_MAX | HWMON_P_MIN | |
| 1237 | HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | |
| 1238 | HWMON_P_CRIT | HWMON_P_LCRIT | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1239 | HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | |
| 1240 | HWMON_P_LABEL, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1241 | /* Receive power */ |
| 1242 | HWMON_P_INPUT | |
| 1243 | HWMON_P_MAX | HWMON_P_MIN | |
| 1244 | HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM | |
| 1245 | HWMON_P_CRIT | HWMON_P_LCRIT | |
Andrew Lunn | c123697 | 2019-08-25 01:04:17 +0200 | [diff] [blame] | 1246 | HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM | |
| 1247 | HWMON_P_LABEL, |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1248 | 0, |
| 1249 | }; |
| 1250 | |
| 1251 | static const struct hwmon_channel_info sfp_hwmon_power_channel_info = { |
| 1252 | .type = hwmon_power, |
| 1253 | .config = sfp_hwmon_power_config, |
| 1254 | }; |
| 1255 | |
| 1256 | static const struct hwmon_channel_info *sfp_hwmon_info[] = { |
| 1257 | &sfp_hwmon_chip, |
| 1258 | &sfp_hwmon_vcc_channel_info, |
| 1259 | &sfp_hwmon_temp_channel_info, |
| 1260 | &sfp_hwmon_bias_channel_info, |
| 1261 | &sfp_hwmon_power_channel_info, |
| 1262 | NULL, |
| 1263 | }; |
| 1264 | |
| 1265 | static const struct hwmon_chip_info sfp_hwmon_chip_info = { |
| 1266 | .ops = &sfp_hwmon_ops, |
| 1267 | .info = sfp_hwmon_info, |
| 1268 | }; |
| 1269 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1270 | static void sfp_hwmon_probe(struct work_struct *work) |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1271 | { |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1272 | struct sfp *sfp = container_of(work, struct sfp, hwmon_probe.work); |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1273 | int err, i; |
| 1274 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1275 | err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag)); |
| 1276 | if (err < 0) { |
| 1277 | if (sfp->hwmon_tries--) { |
| 1278 | mod_delayed_work(system_wq, &sfp->hwmon_probe, |
| 1279 | T_PROBE_RETRY_SLOW); |
| 1280 | } else { |
| 1281 | dev_warn(sfp->dev, "hwmon probe failed: %d\n", err); |
| 1282 | } |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL); |
| 1287 | if (!sfp->hwmon_name) { |
| 1288 | dev_err(sfp->dev, "out of memory for hwmon name\n"); |
| 1289 | return; |
| 1290 | } |
| 1291 | |
| 1292 | for (i = 0; sfp->hwmon_name[i]; i++) |
| 1293 | if (hwmon_is_bad_char(sfp->hwmon_name[i])) |
| 1294 | sfp->hwmon_name[i] = '_'; |
| 1295 | |
| 1296 | sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev, |
| 1297 | sfp->hwmon_name, sfp, |
| 1298 | &sfp_hwmon_chip_info, |
| 1299 | NULL); |
| 1300 | if (IS_ERR(sfp->hwmon_dev)) |
| 1301 | dev_err(sfp->dev, "failed to register hwmon device: %ld\n", |
| 1302 | PTR_ERR(sfp->hwmon_dev)); |
| 1303 | } |
| 1304 | |
| 1305 | static int sfp_hwmon_insert(struct sfp *sfp) |
| 1306 | { |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1307 | if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE) |
| 1308 | return 0; |
| 1309 | |
| 1310 | if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) |
| 1311 | return 0; |
| 1312 | |
| 1313 | if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) |
| 1314 | /* This driver in general does not support address |
| 1315 | * change. |
| 1316 | */ |
| 1317 | return 0; |
| 1318 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1319 | mod_delayed_work(system_wq, &sfp->hwmon_probe, 1); |
| 1320 | sfp->hwmon_tries = R_PROBE_RETRY_SLOW; |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1321 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1322 | return 0; |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static void sfp_hwmon_remove(struct sfp *sfp) |
| 1326 | { |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1327 | cancel_delayed_work_sync(&sfp->hwmon_probe); |
Andrew Lunn | 3e32247 | 2018-09-25 01:50:00 +0200 | [diff] [blame] | 1328 | if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) { |
| 1329 | hwmon_device_unregister(sfp->hwmon_dev); |
| 1330 | sfp->hwmon_dev = NULL; |
| 1331 | kfree(sfp->hwmon_name); |
| 1332 | } |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1333 | } |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1334 | |
| 1335 | static int sfp_hwmon_init(struct sfp *sfp) |
| 1336 | { |
| 1337 | INIT_DELAYED_WORK(&sfp->hwmon_probe, sfp_hwmon_probe); |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static void sfp_hwmon_exit(struct sfp *sfp) |
| 1343 | { |
| 1344 | cancel_delayed_work_sync(&sfp->hwmon_probe); |
| 1345 | } |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1346 | #else |
| 1347 | static int sfp_hwmon_insert(struct sfp *sfp) |
| 1348 | { |
| 1349 | return 0; |
| 1350 | } |
| 1351 | |
| 1352 | static void sfp_hwmon_remove(struct sfp *sfp) |
| 1353 | { |
| 1354 | } |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 1355 | |
| 1356 | static int sfp_hwmon_init(struct sfp *sfp) |
| 1357 | { |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | static void sfp_hwmon_exit(struct sfp *sfp) |
| 1362 | { |
| 1363 | } |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1364 | #endif |
| 1365 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1366 | /* Helpers */ |
| 1367 | static void sfp_module_tx_disable(struct sfp *sfp) |
| 1368 | { |
| 1369 | dev_dbg(sfp->dev, "tx disable %u -> %u\n", |
| 1370 | sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1); |
| 1371 | sfp->state |= SFP_F_TX_DISABLE; |
| 1372 | sfp_set_state(sfp, sfp->state); |
| 1373 | } |
| 1374 | |
| 1375 | static void sfp_module_tx_enable(struct sfp *sfp) |
| 1376 | { |
| 1377 | dev_dbg(sfp->dev, "tx disable %u -> %u\n", |
| 1378 | sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0); |
| 1379 | sfp->state &= ~SFP_F_TX_DISABLE; |
| 1380 | sfp_set_state(sfp, sfp->state); |
| 1381 | } |
| 1382 | |
| 1383 | static void sfp_module_tx_fault_reset(struct sfp *sfp) |
| 1384 | { |
| 1385 | unsigned int state = sfp->state; |
| 1386 | |
| 1387 | if (state & SFP_F_TX_DISABLE) |
| 1388 | return; |
| 1389 | |
| 1390 | sfp_set_state(sfp, state | SFP_F_TX_DISABLE); |
| 1391 | |
| 1392 | udelay(T_RESET_US); |
| 1393 | |
| 1394 | sfp_set_state(sfp, state); |
| 1395 | } |
| 1396 | |
| 1397 | /* SFP state machine */ |
| 1398 | static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout) |
| 1399 | { |
| 1400 | if (timeout) |
| 1401 | mod_delayed_work(system_power_efficient_wq, &sfp->timeout, |
| 1402 | timeout); |
| 1403 | else |
| 1404 | cancel_delayed_work(&sfp->timeout); |
| 1405 | } |
| 1406 | |
| 1407 | static void sfp_sm_next(struct sfp *sfp, unsigned int state, |
| 1408 | unsigned int timeout) |
| 1409 | { |
| 1410 | sfp->sm_state = state; |
| 1411 | sfp_sm_set_timer(sfp, timeout); |
| 1412 | } |
| 1413 | |
Russell King | 0936ebc | 2019-11-10 14:06:23 +0000 | [diff] [blame] | 1414 | static void sfp_sm_mod_next(struct sfp *sfp, unsigned int state, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1415 | unsigned int timeout) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1416 | { |
| 1417 | sfp->sm_mod_state = state; |
| 1418 | sfp_sm_set_timer(sfp, timeout); |
| 1419 | } |
| 1420 | |
| 1421 | static void sfp_sm_phy_detach(struct sfp *sfp) |
| 1422 | { |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1423 | sfp_remove_phy(sfp->sfp_bus); |
| 1424 | phy_device_remove(sfp->mod_phy); |
| 1425 | phy_device_free(sfp->mod_phy); |
| 1426 | sfp->mod_phy = NULL; |
| 1427 | } |
| 1428 | |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1429 | static int sfp_sm_probe_phy(struct sfp *sfp, bool is_c45) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1430 | { |
| 1431 | struct phy_device *phy; |
| 1432 | int err; |
| 1433 | |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1434 | phy = get_phy_device(sfp->i2c_mii, SFP_PHY_ADDR, is_c45); |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 1435 | if (phy == ERR_PTR(-ENODEV)) |
| 1436 | return PTR_ERR(phy); |
Russell King | 20b56ed | 2017-12-15 16:09:36 +0000 | [diff] [blame] | 1437 | if (IS_ERR(phy)) { |
| 1438 | dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy)); |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1439 | return PTR_ERR(phy); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1442 | err = phy_device_register(phy); |
| 1443 | if (err) { |
| 1444 | phy_device_free(phy); |
| 1445 | dev_err(sfp->dev, "phy_device_register failed: %d\n", err); |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1446 | return err; |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1449 | err = sfp_add_phy(sfp->sfp_bus, phy); |
| 1450 | if (err) { |
| 1451 | phy_device_remove(phy); |
| 1452 | phy_device_free(phy); |
| 1453 | dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err); |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1454 | return err; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | sfp->mod_phy = phy; |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1458 | |
| 1459 | return 0; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static void sfp_sm_link_up(struct sfp *sfp) |
| 1463 | { |
| 1464 | sfp_link_up(sfp->sfp_bus); |
| 1465 | sfp_sm_next(sfp, SFP_S_LINK_UP, 0); |
| 1466 | } |
| 1467 | |
| 1468 | static void sfp_sm_link_down(struct sfp *sfp) |
| 1469 | { |
| 1470 | sfp_link_down(sfp->sfp_bus); |
| 1471 | } |
| 1472 | |
| 1473 | static void sfp_sm_link_check_los(struct sfp *sfp) |
| 1474 | { |
| 1475 | unsigned int los = sfp->state & SFP_F_LOS; |
| 1476 | |
Russell King | 710dfbb | 2017-11-30 13:59:16 +0000 | [diff] [blame] | 1477 | /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL |
| 1478 | * are set, we assume that no LOS signal is available. |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1479 | */ |
Russell King | acf1c02 | 2017-11-30 13:59:11 +0000 | [diff] [blame] | 1480 | if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED)) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1481 | los ^= SFP_F_LOS; |
Russell King | 710dfbb | 2017-11-30 13:59:16 +0000 | [diff] [blame] | 1482 | else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL))) |
| 1483 | los = 0; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1484 | |
| 1485 | if (los) |
| 1486 | sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); |
| 1487 | else |
| 1488 | sfp_sm_link_up(sfp); |
| 1489 | } |
| 1490 | |
Russell King | 710dfbb | 2017-11-30 13:59:16 +0000 | [diff] [blame] | 1491 | static bool sfp_los_event_active(struct sfp *sfp, unsigned int event) |
| 1492 | { |
| 1493 | return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) && |
| 1494 | event == SFP_E_LOS_LOW) || |
| 1495 | (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) && |
| 1496 | event == SFP_E_LOS_HIGH); |
| 1497 | } |
| 1498 | |
| 1499 | static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event) |
| 1500 | { |
| 1501 | return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) && |
| 1502 | event == SFP_E_LOS_HIGH) || |
| 1503 | (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) && |
| 1504 | event == SFP_E_LOS_LOW); |
| 1505 | } |
| 1506 | |
Russell King | 63ec1c7 | 2019-11-10 14:07:04 +0000 | [diff] [blame] | 1507 | static void sfp_sm_fault(struct sfp *sfp, unsigned int next_state, bool warn) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1508 | { |
Russell King | 281e4ea | 2019-12-09 14:16:00 +0000 | [diff] [blame] | 1509 | if (sfp->sm_fault_retries && !--sfp->sm_fault_retries) { |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 1510 | dev_err(sfp->dev, |
| 1511 | "module persistently indicates fault, disabling\n"); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1512 | sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0); |
| 1513 | } else { |
| 1514 | if (warn) |
| 1515 | dev_err(sfp->dev, "module transmit fault indicated\n"); |
| 1516 | |
Russell King | 63ec1c7 | 2019-11-10 14:07:04 +0000 | [diff] [blame] | 1517 | sfp_sm_next(sfp, next_state, T_FAULT_RECOVER); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1521 | /* Probe a SFP for a PHY device if the module supports copper - the PHY |
| 1522 | * normally sits at I2C bus address 0x56, and may either be a clause 22 |
| 1523 | * or clause 45 PHY. |
| 1524 | * |
| 1525 | * Clause 22 copper SFP modules normally operate in Cisco SGMII mode with |
| 1526 | * negotiation enabled, but some may be in 1000base-X - which is for the |
| 1527 | * PHY driver to determine. |
| 1528 | * |
| 1529 | * Clause 45 copper SFP+ modules (10G) appear to switch their interface |
| 1530 | * mode according to the negotiated line speed. |
| 1531 | */ |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1532 | static int sfp_sm_probe_for_phy(struct sfp *sfp) |
Russell King | 181f29d | 2019-11-10 14:06:54 +0000 | [diff] [blame] | 1533 | { |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1534 | int err = 0; |
| 1535 | |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1536 | switch (sfp->id.base.extended_cc) { |
| 1537 | case SFF8024_ECC_10GBASE_T_SFI: |
| 1538 | case SFF8024_ECC_10GBASE_T_SR: |
| 1539 | case SFF8024_ECC_5GBASE_T: |
| 1540 | case SFF8024_ECC_2_5GBASE_T: |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1541 | err = sfp_sm_probe_phy(sfp, true); |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | |
| 1544 | default: |
| 1545 | if (sfp->id.base.e1000_base_t) |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1546 | err = sfp_sm_probe_phy(sfp, false); |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | } |
Russell King | 256e43c | 2019-12-09 14:16:05 +0000 | [diff] [blame] | 1549 | return err; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1552 | static int sfp_module_parse_power(struct sfp *sfp) |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1553 | { |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1554 | u32 power_mW = 1000; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1555 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1556 | if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL)) |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1557 | power_mW = 1500; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1558 | if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL)) |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1559 | power_mW = 2000; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1560 | |
Russell King | 7cfa9c9 | 2019-11-10 14:06:44 +0000 | [diff] [blame] | 1561 | if (power_mW > sfp->max_power_mW) { |
| 1562 | /* Module power specification exceeds the allowed maximum. */ |
| 1563 | if (sfp->id.ext.sff8472_compliance == |
| 1564 | SFP_SFF8472_COMPLIANCE_NONE && |
| 1565 | !(sfp->id.ext.diagmon & SFP_DIAGMON_DDM)) { |
| 1566 | /* The module appears not to implement bus address |
| 1567 | * 0xa2, so assume that the module powers up in the |
| 1568 | * indicated mode. |
| 1569 | */ |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1570 | dev_err(sfp->dev, |
| 1571 | "Host does not support %u.%uW modules\n", |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1572 | power_mW / 1000, (power_mW / 100) % 10); |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1573 | return -EINVAL; |
Russell King | 7cfa9c9 | 2019-11-10 14:06:44 +0000 | [diff] [blame] | 1574 | } else { |
| 1575 | dev_warn(sfp->dev, |
| 1576 | "Host does not support %u.%uW modules, module left in power mode 1\n", |
| 1577 | power_mW / 1000, (power_mW / 100) % 10); |
| 1578 | return 0; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1579 | } |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Russell King | 7cfa9c9 | 2019-11-10 14:06:44 +0000 | [diff] [blame] | 1582 | /* If the module requires a higher power mode, but also requires |
| 1583 | * an address change sequence, warn the user that the module may |
| 1584 | * not be functional. |
| 1585 | */ |
| 1586 | if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE && power_mW > 1000) { |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1587 | dev_warn(sfp->dev, |
Colin Ian King | 07f23d9 | 2019-11-13 09:55:48 +0000 | [diff] [blame] | 1588 | "Address Change Sequence not supported but module requires %u.%uW, module may not be functional\n", |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1589 | power_mW / 1000, (power_mW / 100) % 10); |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1590 | return 0; |
| 1591 | } |
| 1592 | |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1593 | sfp->module_power_mW = power_mW; |
| 1594 | |
| 1595 | return 0; |
| 1596 | } |
| 1597 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1598 | static int sfp_sm_mod_hpower(struct sfp *sfp, bool enable) |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1599 | { |
| 1600 | u8 val; |
| 1601 | int err; |
| 1602 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1603 | err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); |
| 1604 | if (err != sizeof(val)) { |
| 1605 | dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err); |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1606 | return -EAGAIN; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Russell King | 9a48462 | 2019-12-11 10:57:01 +0000 | [diff] [blame] | 1609 | /* DM7052 reports as a high power module, responds to reads (with |
| 1610 | * all bytes 0xff) at 0x51 but does not accept writes. In any case, |
| 1611 | * if the bit is already set, we're already in high power mode. |
| 1612 | */ |
| 1613 | if (!!(val & BIT(0)) == enable) |
| 1614 | return 0; |
| 1615 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1616 | if (enable) |
| 1617 | val |= BIT(0); |
| 1618 | else |
| 1619 | val &= ~BIT(0); |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1620 | |
| 1621 | err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val)); |
| 1622 | if (err != sizeof(val)) { |
| 1623 | dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err); |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1624 | return -EAGAIN; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1627 | if (enable) |
| 1628 | dev_info(sfp->dev, "Module switched to %u.%uW power level\n", |
| 1629 | sfp->module_power_mW / 1000, |
| 1630 | (sfp->module_power_mW / 100) % 10); |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1631 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1632 | return 0; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1635 | static int sfp_sm_mod_probe(struct sfp *sfp, bool report) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1636 | { |
| 1637 | /* SFP module inserted - read I2C data */ |
| 1638 | struct sfp_eeprom_id id; |
Russell King | 981f1f8 | 2018-03-28 11:18:25 +0100 | [diff] [blame] | 1639 | bool cotsworks; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1640 | u8 check; |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1641 | int ret; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1642 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1643 | ret = sfp_read(sfp, false, 0, &id, sizeof(id)); |
| 1644 | if (ret < 0) { |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1645 | if (report) |
| 1646 | dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1647 | return -EAGAIN; |
| 1648 | } |
| 1649 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1650 | if (ret != sizeof(id)) { |
| 1651 | dev_err(sfp->dev, "EEPROM short read: %d\n", ret); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1652 | return -EAGAIN; |
| 1653 | } |
| 1654 | |
Russell King | 981f1f8 | 2018-03-28 11:18:25 +0100 | [diff] [blame] | 1655 | /* Cotsworks do not seem to update the checksums when they |
| 1656 | * do the final programming with the final module part number, |
| 1657 | * serial number and date code. |
| 1658 | */ |
| 1659 | cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16); |
| 1660 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1661 | /* Validate the checksum over the base structure */ |
| 1662 | check = sfp_check(&id.base, sizeof(id.base) - 1); |
| 1663 | if (check != id.base.cc_base) { |
Russell King | 981f1f8 | 2018-03-28 11:18:25 +0100 | [diff] [blame] | 1664 | if (cotsworks) { |
| 1665 | dev_warn(sfp->dev, |
| 1666 | "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n", |
| 1667 | check, id.base.cc_base); |
| 1668 | } else { |
| 1669 | dev_err(sfp->dev, |
| 1670 | "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n", |
| 1671 | check, id.base.cc_base); |
| 1672 | print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, |
| 1673 | 16, 1, &id, sizeof(id), true); |
| 1674 | return -EINVAL; |
| 1675 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1676 | } |
| 1677 | |
| 1678 | check = sfp_check(&id.ext, sizeof(id.ext) - 1); |
| 1679 | if (check != id.ext.cc_ext) { |
Russell King | 981f1f8 | 2018-03-28 11:18:25 +0100 | [diff] [blame] | 1680 | if (cotsworks) { |
| 1681 | dev_warn(sfp->dev, |
| 1682 | "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n", |
| 1683 | check, id.ext.cc_ext); |
| 1684 | } else { |
| 1685 | dev_err(sfp->dev, |
| 1686 | "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n", |
| 1687 | check, id.ext.cc_ext); |
| 1688 | print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET, |
| 1689 | 16, 1, &id, sizeof(id), true); |
| 1690 | memset(&id.ext, 0, sizeof(id.ext)); |
| 1691 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | sfp->id = id; |
| 1695 | |
Russell King | a2f247e | 2017-12-29 12:15:12 +0000 | [diff] [blame] | 1696 | dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n", |
| 1697 | (int)sizeof(id.base.vendor_name), id.base.vendor_name, |
| 1698 | (int)sizeof(id.base.vendor_pn), id.base.vendor_pn, |
| 1699 | (int)sizeof(id.base.vendor_rev), id.base.vendor_rev, |
| 1700 | (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn, |
| 1701 | (int)sizeof(id.ext.datecode), id.ext.datecode); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1702 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 1703 | /* Check whether we support this module */ |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 1704 | if (!sfp->type->module_supported(&id)) { |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 1705 | dev_err(sfp->dev, |
| 1706 | "module is not supported - phys id 0x%02x 0x%02x\n", |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1707 | sfp->id.base.phys_id, sfp->id.base.phys_ext_id); |
| 1708 | return -EINVAL; |
| 1709 | } |
| 1710 | |
Russell King | ec7681b | 2017-11-30 13:59:21 +0000 | [diff] [blame] | 1711 | /* If the module requires address swap mode, warn about it */ |
| 1712 | if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE) |
| 1713 | dev_warn(sfp->dev, |
| 1714 | "module address swap to access page 0xA2 is not supported.\n"); |
| 1715 | |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1716 | /* Parse the module power requirement */ |
| 1717 | ret = sfp_module_parse_power(sfp); |
| 1718 | if (ret < 0) |
| 1719 | return ret; |
| 1720 | |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 1721 | if (!memcmp(id.base.vendor_name, "ALCATELLUCENT ", 16) && |
| 1722 | !memcmp(id.base.vendor_pn, "3FE46541AA ", 16)) |
| 1723 | sfp->module_t_start_up = T_START_UP_BAD_GPON; |
| 1724 | else |
| 1725 | sfp->module_t_start_up = T_START_UP; |
| 1726 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1727 | return 0; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static void sfp_sm_mod_remove(struct sfp *sfp) |
| 1731 | { |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1732 | if (sfp->sm_mod_state > SFP_MOD_WAITDEV) |
| 1733 | sfp_module_remove(sfp->sfp_bus); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1734 | |
Andrew Lunn | 1323061 | 2018-07-17 21:48:13 +0200 | [diff] [blame] | 1735 | sfp_hwmon_remove(sfp); |
| 1736 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1737 | memset(&sfp->id, 0, sizeof(sfp->id)); |
Russell King | ed32abb | 2019-11-10 14:06:39 +0000 | [diff] [blame] | 1738 | sfp->module_power_mW = 0; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1739 | |
| 1740 | dev_info(sfp->dev, "module removed\n"); |
| 1741 | } |
| 1742 | |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 1743 | /* This state machine tracks the upstream's state */ |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1744 | static void sfp_sm_device(struct sfp *sfp, unsigned int event) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1745 | { |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1746 | switch (sfp->sm_dev_state) { |
| 1747 | default: |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 1748 | if (event == SFP_E_DEV_ATTACH) |
| 1749 | sfp->sm_dev_state = SFP_DEV_DOWN; |
| 1750 | break; |
| 1751 | |
| 1752 | case SFP_DEV_DOWN: |
| 1753 | if (event == SFP_E_DEV_DETACH) |
| 1754 | sfp->sm_dev_state = SFP_DEV_DETACHED; |
| 1755 | else if (event == SFP_E_DEV_UP) |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1756 | sfp->sm_dev_state = SFP_DEV_UP; |
| 1757 | break; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1758 | |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1759 | case SFP_DEV_UP: |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 1760 | if (event == SFP_E_DEV_DETACH) |
| 1761 | sfp->sm_dev_state = SFP_DEV_DETACHED; |
| 1762 | else if (event == SFP_E_DEV_DOWN) |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1763 | sfp->sm_dev_state = SFP_DEV_DOWN; |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1764 | break; |
| 1765 | } |
| 1766 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1767 | |
Russell King | d900954 | 2019-11-10 14:06:33 +0000 | [diff] [blame] | 1768 | /* This state machine tracks the insert/remove state of the module, probes |
| 1769 | * the on-board EEPROM, and sets up the power level. |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1770 | */ |
| 1771 | static void sfp_sm_module(struct sfp *sfp, unsigned int event) |
| 1772 | { |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1773 | int err; |
| 1774 | |
| 1775 | /* Handle remove event globally, it resets this state machine */ |
| 1776 | if (event == SFP_E_REMOVE) { |
Russell King | d2e816c | 2019-11-10 14:06:28 +0000 | [diff] [blame] | 1777 | if (sfp->sm_mod_state > SFP_MOD_PROBE) |
| 1778 | sfp_sm_mod_remove(sfp); |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1779 | sfp_sm_mod_next(sfp, SFP_MOD_EMPTY, 0); |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | /* Handle device detach globally */ |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1784 | if (sfp->sm_dev_state < SFP_DEV_DOWN && |
| 1785 | sfp->sm_mod_state > SFP_MOD_WAITDEV) { |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1786 | if (sfp->module_power_mW > 1000 && |
| 1787 | sfp->sm_mod_state > SFP_MOD_HPOWER) |
| 1788 | sfp_sm_mod_hpower(sfp, false); |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1789 | sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); |
Russell King | d2e816c | 2019-11-10 14:06:28 +0000 | [diff] [blame] | 1790 | return; |
| 1791 | } |
| 1792 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1793 | switch (sfp->sm_mod_state) { |
| 1794 | default: |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1795 | if (event == SFP_E_INSERT) { |
Russell King | d900954 | 2019-11-10 14:06:33 +0000 | [diff] [blame] | 1796 | sfp_sm_mod_next(sfp, SFP_MOD_PROBE, T_SERIAL); |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1797 | sfp->sm_mod_tries_init = R_PROBE_RETRY_INIT; |
| 1798 | sfp->sm_mod_tries = R_PROBE_RETRY_SLOW; |
| 1799 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1800 | break; |
| 1801 | |
| 1802 | case SFP_MOD_PROBE: |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1803 | /* Wait for T_PROBE_INIT to time out */ |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1804 | if (event != SFP_E_TIMEOUT) |
| 1805 | break; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1806 | |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1807 | err = sfp_sm_mod_probe(sfp, sfp->sm_mod_tries == 1); |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1808 | if (err == -EAGAIN) { |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1809 | if (sfp->sm_mod_tries_init && |
| 1810 | --sfp->sm_mod_tries_init) { |
| 1811 | sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); |
| 1812 | break; |
| 1813 | } else if (sfp->sm_mod_tries && --sfp->sm_mod_tries) { |
| 1814 | if (sfp->sm_mod_tries == R_PROBE_RETRY_SLOW - 1) |
| 1815 | dev_warn(sfp->dev, |
| 1816 | "please wait, module slow to respond\n"); |
| 1817 | sfp_sm_set_timer(sfp, T_PROBE_RETRY_SLOW); |
| 1818 | break; |
| 1819 | } |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 1820 | } |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1821 | if (err < 0) { |
| 1822 | sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); |
| 1823 | break; |
| 1824 | } |
| 1825 | |
Russell King | 38ecd70 | 2019-12-03 23:51:28 +0000 | [diff] [blame] | 1826 | err = sfp_hwmon_insert(sfp); |
| 1827 | if (err) |
| 1828 | dev_warn(sfp->dev, "hwmon probe failed: %d\n", err); |
| 1829 | |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1830 | sfp_sm_mod_next(sfp, SFP_MOD_WAITDEV, 0); |
| 1831 | /* fall through */ |
| 1832 | case SFP_MOD_WAITDEV: |
| 1833 | /* Ensure that the device is attached before proceeding */ |
| 1834 | if (sfp->sm_dev_state < SFP_DEV_DOWN) |
| 1835 | break; |
| 1836 | |
| 1837 | /* Report the module insertion to the upstream device */ |
| 1838 | err = sfp_module_insert(sfp->sfp_bus, &sfp->id); |
| 1839 | if (err < 0) { |
| 1840 | sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); |
| 1841 | break; |
| 1842 | } |
| 1843 | |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1844 | /* If this is a power level 1 module, we are done */ |
| 1845 | if (sfp->module_power_mW <= 1000) |
| 1846 | goto insert; |
| 1847 | |
| 1848 | sfp_sm_mod_next(sfp, SFP_MOD_HPOWER, 0); |
| 1849 | /* fall through */ |
| 1850 | case SFP_MOD_HPOWER: |
| 1851 | /* Enable high power mode */ |
| 1852 | err = sfp_sm_mod_hpower(sfp, true); |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1853 | if (err < 0) { |
| 1854 | if (err != -EAGAIN) { |
| 1855 | sfp_module_remove(sfp->sfp_bus); |
| 1856 | sfp_sm_mod_next(sfp, SFP_MOD_ERROR, 0); |
| 1857 | } else { |
Russell King | e117be7 | 2019-11-10 14:07:30 +0000 | [diff] [blame] | 1858 | sfp_sm_set_timer(sfp, T_PROBE_RETRY_INIT); |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 1859 | } |
| 1860 | break; |
| 1861 | } |
| 1862 | |
| 1863 | sfp_sm_mod_next(sfp, SFP_MOD_WAITPWR, T_HPOWER_LEVEL); |
Russell King | b036a55 | 2019-11-10 14:07:20 +0000 | [diff] [blame] | 1864 | break; |
| 1865 | |
| 1866 | case SFP_MOD_WAITPWR: |
| 1867 | /* Wait for T_HPOWER_LEVEL to time out */ |
| 1868 | if (event != SFP_E_TIMEOUT) |
| 1869 | break; |
| 1870 | |
| 1871 | insert: |
| 1872 | sfp_sm_mod_next(sfp, SFP_MOD_PRESENT, 0); |
| 1873 | break; |
| 1874 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1875 | case SFP_MOD_PRESENT: |
| 1876 | case SFP_MOD_ERROR: |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1877 | break; |
| 1878 | } |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1879 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1880 | |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 1881 | static void sfp_sm_main(struct sfp *sfp, unsigned int event) |
| 1882 | { |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1883 | unsigned long timeout; |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 1884 | int ret; |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1885 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1886 | /* Some events are global */ |
| 1887 | if (sfp->sm_state != SFP_S_DOWN && |
| 1888 | (sfp->sm_mod_state != SFP_MOD_PRESENT || |
| 1889 | sfp->sm_dev_state != SFP_DEV_UP)) { |
| 1890 | if (sfp->sm_state == SFP_S_LINK_UP && |
| 1891 | sfp->sm_dev_state == SFP_DEV_UP) |
| 1892 | sfp_sm_link_down(sfp); |
Russell King | 74c551c | 2019-12-11 10:56:09 +0000 | [diff] [blame] | 1893 | if (sfp->sm_state > SFP_S_INIT) |
| 1894 | sfp_module_stop(sfp->sfp_bus); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1895 | if (sfp->mod_phy) |
| 1896 | sfp_sm_phy_detach(sfp); |
Russell King | 1539e0d | 2019-11-10 14:06:18 +0000 | [diff] [blame] | 1897 | sfp_module_tx_disable(sfp); |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 1898 | sfp_soft_stop_poll(sfp); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1899 | sfp_sm_next(sfp, SFP_S_DOWN, 0); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1900 | return; |
| 1901 | } |
| 1902 | |
| 1903 | /* The main state machine */ |
| 1904 | switch (sfp->sm_state) { |
| 1905 | case SFP_S_DOWN: |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1906 | if (sfp->sm_mod_state != SFP_MOD_PRESENT || |
| 1907 | sfp->sm_dev_state != SFP_DEV_UP) |
| 1908 | break; |
Russell King | 181f29d | 2019-11-10 14:06:54 +0000 | [diff] [blame] | 1909 | |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 1910 | if (!(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) |
| 1911 | sfp_soft_start_poll(sfp); |
| 1912 | |
| 1913 | sfp_module_tx_enable(sfp); |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1914 | |
| 1915 | /* Initialise the fault clearance retries */ |
Russell King | 281e4ea | 2019-12-09 14:16:00 +0000 | [diff] [blame] | 1916 | sfp->sm_fault_retries = N_FAULT_INIT; |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1917 | |
| 1918 | /* We need to check the TX_FAULT state, which is not defined |
| 1919 | * while TX_DISABLE is asserted. The earliest we want to do |
| 1920 | * anything (such as probe for a PHY) is 50ms. |
| 1921 | */ |
| 1922 | sfp_sm_next(sfp, SFP_S_WAIT, T_WAIT); |
| 1923 | break; |
| 1924 | |
| 1925 | case SFP_S_WAIT: |
| 1926 | if (event != SFP_E_TIMEOUT) |
| 1927 | break; |
| 1928 | |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1929 | if (sfp->state & SFP_F_TX_FAULT) { |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 1930 | /* Wait up to t_init (SFF-8472) or t_start_up (SFF-8431) |
| 1931 | * from the TX_DISABLE deassertion for the module to |
| 1932 | * initialise, which is indicated by TX_FAULT |
| 1933 | * deasserting. |
Russell King | 181f29d | 2019-11-10 14:06:54 +0000 | [diff] [blame] | 1934 | */ |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 1935 | timeout = sfp->module_t_start_up; |
Russell King | eefa6f1 | 2019-11-10 14:06:59 +0000 | [diff] [blame] | 1936 | if (timeout > T_WAIT) |
| 1937 | timeout -= T_WAIT; |
| 1938 | else |
| 1939 | timeout = 1; |
| 1940 | |
| 1941 | sfp_sm_next(sfp, SFP_S_INIT, timeout); |
| 1942 | } else { |
| 1943 | /* TX_FAULT is not asserted, assume the module has |
| 1944 | * finished initialising. |
| 1945 | */ |
| 1946 | goto init_done; |
Russell King | 181f29d | 2019-11-10 14:06:54 +0000 | [diff] [blame] | 1947 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1948 | break; |
| 1949 | |
| 1950 | case SFP_S_INIT: |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1951 | if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 1952 | /* TX_FAULT is still asserted after t_init or |
| 1953 | * or t_start_up, so assume there is a fault. |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1954 | */ |
| 1955 | sfp_sm_fault(sfp, SFP_S_INIT_TX_FAULT, |
Russell King | 281e4ea | 2019-12-09 14:16:00 +0000 | [diff] [blame] | 1956 | sfp->sm_fault_retries == N_FAULT_INIT); |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1957 | } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 1958 | init_done: |
| 1959 | sfp->sm_phy_retries = R_PHY_RETRY; |
| 1960 | goto phy_probe; |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1961 | } |
| 1962 | break; |
| 1963 | |
Russell King | 1cb89a1 | 2019-12-09 14:16:11 +0000 | [diff] [blame] | 1964 | case SFP_S_INIT_PHY: |
| 1965 | if (event != SFP_E_TIMEOUT) |
| 1966 | break; |
| 1967 | phy_probe: |
| 1968 | /* TX_FAULT deasserted or we timed out with TX_FAULT |
| 1969 | * clear. Probe for the PHY and check the LOS state. |
| 1970 | */ |
| 1971 | ret = sfp_sm_probe_for_phy(sfp); |
| 1972 | if (ret == -ENODEV) { |
| 1973 | if (--sfp->sm_phy_retries) { |
| 1974 | sfp_sm_next(sfp, SFP_S_INIT_PHY, T_PHY_RETRY); |
| 1975 | break; |
| 1976 | } else { |
| 1977 | dev_info(sfp->dev, "no PHY detected\n"); |
| 1978 | } |
| 1979 | } else if (ret) { |
| 1980 | sfp_sm_next(sfp, SFP_S_FAIL, 0); |
| 1981 | break; |
| 1982 | } |
| 1983 | if (sfp_module_start(sfp->sfp_bus)) { |
| 1984 | sfp_sm_next(sfp, SFP_S_FAIL, 0); |
| 1985 | break; |
| 1986 | } |
| 1987 | sfp_sm_link_check_los(sfp); |
| 1988 | |
| 1989 | /* Reset the fault retry count */ |
| 1990 | sfp->sm_fault_retries = N_FAULT; |
| 1991 | break; |
| 1992 | |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1993 | case SFP_S_INIT_TX_FAULT: |
| 1994 | if (event == SFP_E_TIMEOUT) { |
| 1995 | sfp_module_tx_fault_reset(sfp); |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 1996 | sfp_sm_next(sfp, SFP_S_INIT, sfp->module_t_start_up); |
Russell King | d23751a | 2019-11-10 14:07:09 +0000 | [diff] [blame] | 1997 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 1998 | break; |
| 1999 | |
| 2000 | case SFP_S_WAIT_LOS: |
| 2001 | if (event == SFP_E_TX_FAULT) |
Russell King | 63ec1c7 | 2019-11-10 14:07:04 +0000 | [diff] [blame] | 2002 | sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); |
Russell King | 710dfbb | 2017-11-30 13:59:16 +0000 | [diff] [blame] | 2003 | else if (sfp_los_event_inactive(sfp, event)) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2004 | sfp_sm_link_up(sfp); |
| 2005 | break; |
| 2006 | |
| 2007 | case SFP_S_LINK_UP: |
| 2008 | if (event == SFP_E_TX_FAULT) { |
| 2009 | sfp_sm_link_down(sfp); |
Russell King | 63ec1c7 | 2019-11-10 14:07:04 +0000 | [diff] [blame] | 2010 | sfp_sm_fault(sfp, SFP_S_TX_FAULT, true); |
Russell King | 710dfbb | 2017-11-30 13:59:16 +0000 | [diff] [blame] | 2011 | } else if (sfp_los_event_active(sfp, event)) { |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2012 | sfp_sm_link_down(sfp); |
| 2013 | sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0); |
| 2014 | } |
| 2015 | break; |
| 2016 | |
| 2017 | case SFP_S_TX_FAULT: |
| 2018 | if (event == SFP_E_TIMEOUT) { |
| 2019 | sfp_module_tx_fault_reset(sfp); |
Russell King | 26c97a2 | 2019-12-09 13:40:23 +0000 | [diff] [blame] | 2020 | sfp_sm_next(sfp, SFP_S_REINIT, sfp->module_t_start_up); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2021 | } |
| 2022 | break; |
| 2023 | |
| 2024 | case SFP_S_REINIT: |
| 2025 | if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) { |
Russell King | 63ec1c7 | 2019-11-10 14:07:04 +0000 | [diff] [blame] | 2026 | sfp_sm_fault(sfp, SFP_S_TX_FAULT, false); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2027 | } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) { |
| 2028 | dev_info(sfp->dev, "module transmit fault recovered\n"); |
| 2029 | sfp_sm_link_check_los(sfp); |
| 2030 | } |
| 2031 | break; |
| 2032 | |
| 2033 | case SFP_S_TX_DISABLE: |
| 2034 | break; |
| 2035 | } |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | static void sfp_sm_event(struct sfp *sfp, unsigned int event) |
| 2039 | { |
| 2040 | mutex_lock(&sfp->sm_mutex); |
| 2041 | |
| 2042 | dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n", |
| 2043 | mod_state_to_str(sfp->sm_mod_state), |
| 2044 | dev_state_to_str(sfp->sm_dev_state), |
| 2045 | sm_state_to_str(sfp->sm_state), |
| 2046 | event_to_str(event)); |
| 2047 | |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 2048 | sfp_sm_device(sfp, event); |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 2049 | sfp_sm_module(sfp, event); |
Russell King | e85d81a | 2019-11-10 14:06:13 +0000 | [diff] [blame] | 2050 | sfp_sm_main(sfp, event); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2051 | |
Andrew Lunn | 4005a7c | 2018-08-08 20:54:12 +0200 | [diff] [blame] | 2052 | dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n", |
| 2053 | mod_state_to_str(sfp->sm_mod_state), |
| 2054 | dev_state_to_str(sfp->sm_dev_state), |
| 2055 | sm_state_to_str(sfp->sm_state)); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2056 | |
| 2057 | mutex_unlock(&sfp->sm_mutex); |
| 2058 | } |
| 2059 | |
Russell King | b5bfc21 | 2019-02-06 10:52:30 +0000 | [diff] [blame] | 2060 | static void sfp_attach(struct sfp *sfp) |
| 2061 | { |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 2062 | sfp_sm_event(sfp, SFP_E_DEV_ATTACH); |
Russell King | b5bfc21 | 2019-02-06 10:52:30 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | static void sfp_detach(struct sfp *sfp) |
| 2066 | { |
Russell King | 6b0da5c | 2019-11-10 14:07:14 +0000 | [diff] [blame] | 2067 | sfp_sm_event(sfp, SFP_E_DEV_DETACH); |
Russell King | b5bfc21 | 2019-02-06 10:52:30 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2070 | static void sfp_start(struct sfp *sfp) |
| 2071 | { |
| 2072 | sfp_sm_event(sfp, SFP_E_DEV_UP); |
| 2073 | } |
| 2074 | |
| 2075 | static void sfp_stop(struct sfp *sfp) |
| 2076 | { |
| 2077 | sfp_sm_event(sfp, SFP_E_DEV_DOWN); |
| 2078 | } |
| 2079 | |
| 2080 | static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo) |
| 2081 | { |
| 2082 | /* locking... and check module is present */ |
| 2083 | |
Russell King | ec7681b | 2017-11-30 13:59:21 +0000 | [diff] [blame] | 2084 | if (sfp->id.ext.sff8472_compliance && |
| 2085 | !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) { |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2086 | modinfo->type = ETH_MODULE_SFF_8472; |
| 2087 | modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN; |
| 2088 | } else { |
| 2089 | modinfo->type = ETH_MODULE_SFF_8079; |
| 2090 | modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN; |
| 2091 | } |
| 2092 | return 0; |
| 2093 | } |
| 2094 | |
| 2095 | static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee, |
Florian Fainelli | 516b29e | 2017-10-30 21:42:57 -0700 | [diff] [blame] | 2096 | u8 *data) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2097 | { |
| 2098 | unsigned int first, last, len; |
| 2099 | int ret; |
| 2100 | |
| 2101 | if (ee->len == 0) |
| 2102 | return -EINVAL; |
| 2103 | |
| 2104 | first = ee->offset; |
| 2105 | last = ee->offset + ee->len; |
| 2106 | if (first < ETH_MODULE_SFF_8079_LEN) { |
| 2107 | len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN); |
| 2108 | len -= first; |
| 2109 | |
Russell King | 2794ffc | 2017-12-15 16:09:41 +0000 | [diff] [blame] | 2110 | ret = sfp_read(sfp, false, first, data, len); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2111 | if (ret < 0) |
| 2112 | return ret; |
| 2113 | |
| 2114 | first += len; |
| 2115 | data += len; |
| 2116 | } |
Russell King | 2794ffc | 2017-12-15 16:09:41 +0000 | [diff] [blame] | 2117 | if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) { |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2118 | len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN); |
| 2119 | len -= first; |
| 2120 | first -= ETH_MODULE_SFF_8079_LEN; |
| 2121 | |
Russell King | 2794ffc | 2017-12-15 16:09:41 +0000 | [diff] [blame] | 2122 | ret = sfp_read(sfp, true, first, data, len); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2123 | if (ret < 0) |
| 2124 | return ret; |
| 2125 | } |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
| 2129 | static const struct sfp_socket_ops sfp_module_ops = { |
Russell King | b5bfc21 | 2019-02-06 10:52:30 +0000 | [diff] [blame] | 2130 | .attach = sfp_attach, |
| 2131 | .detach = sfp_detach, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2132 | .start = sfp_start, |
| 2133 | .stop = sfp_stop, |
| 2134 | .module_info = sfp_module_info, |
| 2135 | .module_eeprom = sfp_module_eeprom, |
| 2136 | }; |
| 2137 | |
| 2138 | static void sfp_timeout(struct work_struct *work) |
| 2139 | { |
| 2140 | struct sfp *sfp = container_of(work, struct sfp, timeout.work); |
| 2141 | |
| 2142 | rtnl_lock(); |
| 2143 | sfp_sm_event(sfp, SFP_E_TIMEOUT); |
| 2144 | rtnl_unlock(); |
| 2145 | } |
| 2146 | |
| 2147 | static void sfp_check_state(struct sfp *sfp) |
| 2148 | { |
| 2149 | unsigned int state, i, changed; |
| 2150 | |
Robert Hancock | 2158e85 | 2019-06-07 10:42:36 -0600 | [diff] [blame] | 2151 | mutex_lock(&sfp->st_mutex); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2152 | state = sfp_get_state(sfp); |
| 2153 | changed = state ^ sfp->state; |
| 2154 | changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT; |
| 2155 | |
| 2156 | for (i = 0; i < GPIO_MAX; i++) |
| 2157 | if (changed & BIT(i)) |
| 2158 | dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i], |
| 2159 | !!(sfp->state & BIT(i)), !!(state & BIT(i))); |
| 2160 | |
| 2161 | state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT); |
| 2162 | sfp->state = state; |
| 2163 | |
| 2164 | rtnl_lock(); |
| 2165 | if (changed & SFP_F_PRESENT) |
| 2166 | sfp_sm_event(sfp, state & SFP_F_PRESENT ? |
| 2167 | SFP_E_INSERT : SFP_E_REMOVE); |
| 2168 | |
| 2169 | if (changed & SFP_F_TX_FAULT) |
| 2170 | sfp_sm_event(sfp, state & SFP_F_TX_FAULT ? |
| 2171 | SFP_E_TX_FAULT : SFP_E_TX_CLEAR); |
| 2172 | |
| 2173 | if (changed & SFP_F_LOS) |
| 2174 | sfp_sm_event(sfp, state & SFP_F_LOS ? |
| 2175 | SFP_E_LOS_HIGH : SFP_E_LOS_LOW); |
| 2176 | rtnl_unlock(); |
Robert Hancock | 2158e85 | 2019-06-07 10:42:36 -0600 | [diff] [blame] | 2177 | mutex_unlock(&sfp->st_mutex); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | static irqreturn_t sfp_irq(int irq, void *data) |
| 2181 | { |
| 2182 | struct sfp *sfp = data; |
| 2183 | |
| 2184 | sfp_check_state(sfp); |
| 2185 | |
| 2186 | return IRQ_HANDLED; |
| 2187 | } |
| 2188 | |
| 2189 | static void sfp_poll(struct work_struct *work) |
| 2190 | { |
| 2191 | struct sfp *sfp = container_of(work, struct sfp, poll.work); |
| 2192 | |
| 2193 | sfp_check_state(sfp); |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 2194 | |
| 2195 | if (sfp->state_soft_mask & (SFP_F_LOS | SFP_F_TX_FAULT) || |
| 2196 | sfp->need_poll) |
| 2197 | mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | static struct sfp *sfp_alloc(struct device *dev) |
| 2201 | { |
| 2202 | struct sfp *sfp; |
| 2203 | |
| 2204 | sfp = kzalloc(sizeof(*sfp), GFP_KERNEL); |
| 2205 | if (!sfp) |
| 2206 | return ERR_PTR(-ENOMEM); |
| 2207 | |
| 2208 | sfp->dev = dev; |
| 2209 | |
| 2210 | mutex_init(&sfp->sm_mutex); |
Robert Hancock | 2158e85 | 2019-06-07 10:42:36 -0600 | [diff] [blame] | 2211 | mutex_init(&sfp->st_mutex); |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2212 | INIT_DELAYED_WORK(&sfp->poll, sfp_poll); |
| 2213 | INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout); |
| 2214 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 2215 | sfp_hwmon_init(sfp); |
| 2216 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2217 | return sfp; |
| 2218 | } |
| 2219 | |
| 2220 | static void sfp_cleanup(void *data) |
| 2221 | { |
| 2222 | struct sfp *sfp = data; |
| 2223 | |
Russell King | 139d3a2 | 2019-11-10 14:07:35 +0000 | [diff] [blame] | 2224 | sfp_hwmon_exit(sfp); |
| 2225 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2226 | cancel_delayed_work_sync(&sfp->poll); |
| 2227 | cancel_delayed_work_sync(&sfp->timeout); |
| 2228 | if (sfp->i2c_mii) { |
| 2229 | mdiobus_unregister(sfp->i2c_mii); |
| 2230 | mdiobus_free(sfp->i2c_mii); |
| 2231 | } |
| 2232 | if (sfp->i2c) |
| 2233 | i2c_put_adapter(sfp->i2c); |
| 2234 | kfree(sfp); |
| 2235 | } |
| 2236 | |
| 2237 | static int sfp_probe(struct platform_device *pdev) |
| 2238 | { |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2239 | const struct sff_data *sff; |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2240 | struct i2c_adapter *i2c; |
Chris Healy | 5411ca7 | 2020-07-06 18:27:07 -0700 | [diff] [blame^] | 2241 | char *sfp_irq_name; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2242 | struct sfp *sfp; |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2243 | int err, i; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2244 | |
| 2245 | sfp = sfp_alloc(&pdev->dev); |
| 2246 | if (IS_ERR(sfp)) |
| 2247 | return PTR_ERR(sfp); |
| 2248 | |
| 2249 | platform_set_drvdata(pdev, sfp); |
| 2250 | |
| 2251 | err = devm_add_action(sfp->dev, sfp_cleanup, sfp); |
| 2252 | if (err < 0) |
| 2253 | return err; |
| 2254 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2255 | sff = sfp->type = &sfp_data; |
| 2256 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2257 | if (pdev->dev.of_node) { |
| 2258 | struct device_node *node = pdev->dev.of_node; |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2259 | const struct of_device_id *id; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2260 | struct device_node *np; |
| 2261 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2262 | id = of_match_node(sfp_of_match, node); |
| 2263 | if (WARN_ON(!id)) |
| 2264 | return -EINVAL; |
| 2265 | |
| 2266 | sff = sfp->type = id->data; |
| 2267 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2268 | np = of_parse_phandle(node, "i2c-bus", 0); |
Antoine Tenart | 66ede1f | 2018-05-22 12:18:00 +0200 | [diff] [blame] | 2269 | if (!np) { |
| 2270 | dev_err(sfp->dev, "missing 'i2c-bus' property\n"); |
| 2271 | return -ENODEV; |
| 2272 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2273 | |
Antoine Tenart | 66ede1f | 2018-05-22 12:18:00 +0200 | [diff] [blame] | 2274 | i2c = of_find_i2c_adapter_by_node(np); |
| 2275 | of_node_put(np); |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2276 | } else if (has_acpi_companion(&pdev->dev)) { |
| 2277 | struct acpi_device *adev = ACPI_COMPANION(&pdev->dev); |
| 2278 | struct fwnode_handle *fw = acpi_fwnode_handle(adev); |
| 2279 | struct fwnode_reference_args args; |
| 2280 | struct acpi_handle *acpi_handle; |
| 2281 | int ret; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2282 | |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2283 | ret = acpi_node_get_property_reference(fw, "i2c-bus", 0, &args); |
Dan Carpenter | 1086ca3 | 2019-06-13 09:51:02 +0300 | [diff] [blame] | 2284 | if (ret || !is_acpi_device_node(args.fwnode)) { |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2285 | dev_err(&pdev->dev, "missing 'i2c-bus' property\n"); |
| 2286 | return -ENODEV; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2287 | } |
Ruslan Babayev | 7ce236fa | 2019-05-28 16:02:33 -0700 | [diff] [blame] | 2288 | |
| 2289 | acpi_handle = ACPI_HANDLE_FWNODE(args.fwnode); |
| 2290 | i2c = i2c_acpi_find_adapter_by_handle(acpi_handle); |
| 2291 | } else { |
| 2292 | return -EINVAL; |
| 2293 | } |
| 2294 | |
| 2295 | if (!i2c) |
| 2296 | return -EPROBE_DEFER; |
| 2297 | |
| 2298 | err = sfp_i2c_configure(sfp, i2c); |
| 2299 | if (err < 0) { |
| 2300 | i2c_put_adapter(i2c); |
| 2301 | return err; |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2302 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2303 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2304 | for (i = 0; i < GPIO_MAX; i++) |
| 2305 | if (sff->gpios & BIT(i)) { |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2306 | sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev, |
| 2307 | gpio_of_names[i], gpio_flags[i]); |
| 2308 | if (IS_ERR(sfp->gpio[i])) |
| 2309 | return PTR_ERR(sfp->gpio[i]); |
| 2310 | } |
| 2311 | |
Russell King | 259c861 | 2017-12-14 10:27:47 +0000 | [diff] [blame] | 2312 | sfp->get_state = sfp_gpio_get_state; |
| 2313 | sfp->set_state = sfp_gpio_set_state; |
| 2314 | |
| 2315 | /* Modules that have no detect signal are always present */ |
| 2316 | if (!(sfp->gpio[GPIO_MODDEF0])) |
| 2317 | sfp->get_state = sff_gpio_get_state; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2318 | |
Jon Nettleton | 3bb3526 | 2018-02-27 15:53:12 +0000 | [diff] [blame] | 2319 | device_property_read_u32(&pdev->dev, "maximum-power-milliwatt", |
| 2320 | &sfp->max_power_mW); |
| 2321 | if (!sfp->max_power_mW) |
| 2322 | sfp->max_power_mW = 1000; |
| 2323 | |
| 2324 | dev_info(sfp->dev, "Host maximum power %u.%uW\n", |
| 2325 | sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10); |
| 2326 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2327 | /* Get the initial state, and always signal TX disable, |
| 2328 | * since the network interface will not be up. |
| 2329 | */ |
| 2330 | sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE; |
| 2331 | |
| 2332 | if (sfp->gpio[GPIO_RATE_SELECT] && |
| 2333 | gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT])) |
| 2334 | sfp->state |= SFP_F_RATE_SELECT; |
| 2335 | sfp_set_state(sfp, sfp->state); |
| 2336 | sfp_module_tx_disable(sfp); |
Russell King | 73f5e84 | 2019-11-10 14:07:25 +0000 | [diff] [blame] | 2337 | if (sfp->state & SFP_F_PRESENT) { |
| 2338 | rtnl_lock(); |
| 2339 | sfp_sm_event(sfp, SFP_E_INSERT); |
| 2340 | rtnl_unlock(); |
| 2341 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2342 | |
| 2343 | for (i = 0; i < GPIO_MAX; i++) { |
| 2344 | if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i]) |
| 2345 | continue; |
| 2346 | |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2347 | sfp->gpio_irq[i] = gpiod_to_irq(sfp->gpio[i]); |
| 2348 | if (!sfp->gpio_irq[i]) { |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 2349 | sfp->need_poll = true; |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2350 | continue; |
| 2351 | } |
| 2352 | |
Chris Healy | 5411ca7 | 2020-07-06 18:27:07 -0700 | [diff] [blame^] | 2353 | sfp_irq_name = devm_kasprintf(sfp->dev, GFP_KERNEL, |
| 2354 | "%s-%s", dev_name(sfp->dev), |
| 2355 | gpio_of_names[i]); |
| 2356 | |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2357 | err = devm_request_threaded_irq(sfp->dev, sfp->gpio_irq[i], |
| 2358 | NULL, sfp_irq, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2359 | IRQF_ONESHOT | |
| 2360 | IRQF_TRIGGER_RISING | |
| 2361 | IRQF_TRIGGER_FALLING, |
Chris Healy | 5411ca7 | 2020-07-06 18:27:07 -0700 | [diff] [blame^] | 2362 | sfp_irq_name, sfp); |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2363 | if (err) { |
| 2364 | sfp->gpio_irq[i] = 0; |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 2365 | sfp->need_poll = true; |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2366 | } |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Russell King | f3c9a66 | 2019-11-20 12:29:59 +0000 | [diff] [blame] | 2369 | if (sfp->need_poll) |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2370 | mod_delayed_work(system_wq, &sfp->poll, poll_jiffies); |
| 2371 | |
Antoine Tenart | a1f5d1f | 2018-05-22 12:17:59 +0200 | [diff] [blame] | 2372 | /* We could have an issue in cases no Tx disable pin is available or |
| 2373 | * wired as modules using a laser as their light source will continue to |
| 2374 | * be active when the fiber is removed. This could be a safety issue and |
| 2375 | * we should at least warn the user about that. |
| 2376 | */ |
| 2377 | if (!sfp->gpio[GPIO_TX_DISABLE]) |
| 2378 | dev_warn(sfp->dev, |
| 2379 | "No tx_disable pin: SFP modules will always be emitting.\n"); |
| 2380 | |
Russell King | b5bfc21 | 2019-02-06 10:52:30 +0000 | [diff] [blame] | 2381 | sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops); |
| 2382 | if (!sfp->sfp_bus) |
| 2383 | return -ENOMEM; |
| 2384 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2385 | return 0; |
| 2386 | } |
| 2387 | |
| 2388 | static int sfp_remove(struct platform_device *pdev) |
| 2389 | { |
| 2390 | struct sfp *sfp = platform_get_drvdata(pdev); |
| 2391 | |
| 2392 | sfp_unregister_socket(sfp->sfp_bus); |
| 2393 | |
Russell King | 0cb96b5 | 2019-12-03 23:51:22 +0000 | [diff] [blame] | 2394 | rtnl_lock(); |
| 2395 | sfp_sm_event(sfp, SFP_E_REMOVE); |
| 2396 | rtnl_unlock(); |
| 2397 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2398 | return 0; |
| 2399 | } |
| 2400 | |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2401 | static void sfp_shutdown(struct platform_device *pdev) |
| 2402 | { |
| 2403 | struct sfp *sfp = platform_get_drvdata(pdev); |
| 2404 | int i; |
| 2405 | |
| 2406 | for (i = 0; i < GPIO_MAX; i++) { |
| 2407 | if (!sfp->gpio_irq[i]) |
| 2408 | continue; |
| 2409 | |
| 2410 | devm_free_irq(sfp->dev, sfp->gpio_irq[i], sfp); |
| 2411 | } |
| 2412 | |
| 2413 | cancel_delayed_work_sync(&sfp->poll); |
| 2414 | cancel_delayed_work_sync(&sfp->timeout); |
| 2415 | } |
| 2416 | |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2417 | static struct platform_driver sfp_driver = { |
| 2418 | .probe = sfp_probe, |
| 2419 | .remove = sfp_remove, |
Robert Hancock | 257c255 | 2019-06-07 10:42:35 -0600 | [diff] [blame] | 2420 | .shutdown = sfp_shutdown, |
Russell King | 7397005 | 2017-07-25 15:03:39 +0100 | [diff] [blame] | 2421 | .driver = { |
| 2422 | .name = "sfp", |
| 2423 | .of_match_table = sfp_of_match, |
| 2424 | }, |
| 2425 | }; |
| 2426 | |
| 2427 | static int sfp_init(void) |
| 2428 | { |
| 2429 | poll_jiffies = msecs_to_jiffies(100); |
| 2430 | |
| 2431 | return platform_driver_register(&sfp_driver); |
| 2432 | } |
| 2433 | module_init(sfp_init); |
| 2434 | |
| 2435 | static void sfp_exit(void) |
| 2436 | { |
| 2437 | platform_driver_unregister(&sfp_driver); |
| 2438 | } |
| 2439 | module_exit(sfp_exit); |
| 2440 | |
| 2441 | MODULE_ALIAS("platform:sfp"); |
| 2442 | MODULE_AUTHOR("Russell King"); |
| 2443 | MODULE_LICENSE("GPL v2"); |