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