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