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