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