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