1 #include <linux/delay.h>
2 #include <linux/gpio/consumer.h>
4 #include <linux/interrupt.h>
5 #include <linux/jiffies.h>
6 #include <linux/module.h>
7 #include <linux/mutex.h>
10 #include <linux/platform_device.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
27 SFP_F_PRESENT
= BIT(GPIO_MODDEF0
),
28 SFP_F_LOS
= BIT(GPIO_LOS
),
29 SFP_F_TX_FAULT
= BIT(GPIO_TX_FAULT
),
30 SFP_F_TX_DISABLE
= BIT(GPIO_TX_DISABLE
),
31 SFP_F_RATE_SELECT
= BIT(GPIO_RATE_SELECT
),
61 static const char *gpio_of_names
[] = {
69 static const enum gpiod_flags gpio_flags
[] = {
77 #define T_INIT_JIFFIES msecs_to_jiffies(300)
79 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
81 /* SFP module presence detection is poor: the three MOD DEF signals are
82 * the same length on the PCB, which means it's possible for MOD DEF 0 to
83 * connect before the I2C bus on MOD DEF 1/2.
85 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
86 * be deasserted) but makes no mention of the earliest time before we can
87 * access the I2C EEPROM. However, Avago modules require 300ms.
89 #define T_PROBE_INIT msecs_to_jiffies(300)
90 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
91 #define T_PROBE_RETRY msecs_to_jiffies(100)
93 /* SFP modules appear to always have their PHY configured for bus address
94 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
96 #define SFP_PHY_ADDR 22
98 /* Give this long for the PHY to reset. */
99 #define T_PHY_RESET_MS 50
101 static DEFINE_MUTEX(sfp_mutex
);
105 bool (*module_supported
)(const struct sfp_eeprom_id
*id
);
110 struct i2c_adapter
*i2c
;
111 struct mii_bus
*i2c_mii
;
112 struct sfp_bus
*sfp_bus
;
113 struct phy_device
*mod_phy
;
114 const struct sff_data
*type
;
117 unsigned int (*get_state
)(struct sfp
*);
118 void (*set_state
)(struct sfp
*, unsigned int);
119 int (*read
)(struct sfp
*, bool, u8
, void *, size_t);
120 int (*write
)(struct sfp
*, bool, u8
, void *, size_t);
122 struct gpio_desc
*gpio
[GPIO_MAX
];
125 struct delayed_work poll
;
126 struct delayed_work timeout
;
127 struct mutex sm_mutex
;
128 unsigned char sm_mod_state
;
129 unsigned char sm_dev_state
;
130 unsigned short sm_state
;
131 unsigned int sm_retries
;
133 struct sfp_eeprom_id id
;
136 static bool sff_module_supported(const struct sfp_eeprom_id
*id
)
138 return id
->base
.phys_id
== SFP_PHYS_ID_SFF
&&
139 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
142 static const struct sff_data sff_data
= {
143 .gpios
= SFP_F_LOS
| SFP_F_TX_FAULT
| SFP_F_TX_DISABLE
,
144 .module_supported
= sff_module_supported
,
147 static bool sfp_module_supported(const struct sfp_eeprom_id
*id
)
149 return id
->base
.phys_id
== SFP_PHYS_ID_SFP
&&
150 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
153 static const struct sff_data sfp_data
= {
154 .gpios
= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
|
155 SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
,
156 .module_supported
= sfp_module_supported
,
159 static const struct of_device_id sfp_of_match
[] = {
160 { .compatible
= "sff,sff", .data
= &sff_data
, },
161 { .compatible
= "sff,sfp", .data
= &sfp_data
, },
164 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
166 static unsigned long poll_jiffies
;
168 static unsigned int sfp_gpio_get_state(struct sfp
*sfp
)
170 unsigned int i
, state
, v
;
172 for (i
= state
= 0; i
< GPIO_MAX
; i
++) {
173 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
176 v
= gpiod_get_value_cansleep(sfp
->gpio
[i
]);
184 static unsigned int sff_gpio_get_state(struct sfp
*sfp
)
186 return sfp_gpio_get_state(sfp
) | SFP_F_PRESENT
;
189 static void sfp_gpio_set_state(struct sfp
*sfp
, unsigned int state
)
191 if (state
& SFP_F_PRESENT
) {
192 /* If the module is present, drive the signals */
193 if (sfp
->gpio
[GPIO_TX_DISABLE
])
194 gpiod_direction_output(sfp
->gpio
[GPIO_TX_DISABLE
],
195 state
& SFP_F_TX_DISABLE
);
196 if (state
& SFP_F_RATE_SELECT
)
197 gpiod_direction_output(sfp
->gpio
[GPIO_RATE_SELECT
],
198 state
& SFP_F_RATE_SELECT
);
200 /* Otherwise, let them float to the pull-ups */
201 if (sfp
->gpio
[GPIO_TX_DISABLE
])
202 gpiod_direction_input(sfp
->gpio
[GPIO_TX_DISABLE
]);
203 if (state
& SFP_F_RATE_SELECT
)
204 gpiod_direction_input(sfp
->gpio
[GPIO_RATE_SELECT
]);
208 static int sfp_i2c_read(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
211 struct i2c_msg msgs
[2];
212 u8 bus_addr
= a2
? 0x51 : 0x50;
215 msgs
[0].addr
= bus_addr
;
218 msgs
[0].buf
= &dev_addr
;
219 msgs
[1].addr
= bus_addr
;
220 msgs
[1].flags
= I2C_M_RD
;
224 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
228 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
231 static int sfp_i2c_write(struct sfp
*sfp
, bool a2
, u8 dev_addr
, void *buf
,
234 struct i2c_msg msgs
[1];
235 u8 bus_addr
= a2
? 0x51 : 0x50;
238 msgs
[0].addr
= bus_addr
;
240 msgs
[0].len
= 1 + len
;
241 msgs
[0].buf
= kmalloc(1 + len
, GFP_KERNEL
);
245 msgs
[0].buf
[0] = dev_addr
;
246 memcpy(&msgs
[0].buf
[1], buf
, len
);
248 ret
= i2c_transfer(sfp
->i2c
, msgs
, ARRAY_SIZE(msgs
));
255 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
258 static int sfp_i2c_configure(struct sfp
*sfp
, struct i2c_adapter
*i2c
)
260 struct mii_bus
*i2c_mii
;
263 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
267 sfp
->read
= sfp_i2c_read
;
268 sfp
->write
= sfp_i2c_write
;
270 i2c_mii
= mdio_i2c_alloc(sfp
->dev
, i2c
);
272 return PTR_ERR(i2c_mii
);
274 i2c_mii
->name
= "SFP I2C Bus";
275 i2c_mii
->phy_mask
= ~0;
277 ret
= mdiobus_register(i2c_mii
);
279 mdiobus_free(i2c_mii
);
283 sfp
->i2c_mii
= i2c_mii
;
289 static unsigned int sfp_get_state(struct sfp
*sfp
)
291 return sfp
->get_state(sfp
);
294 static void sfp_set_state(struct sfp
*sfp
, unsigned int state
)
296 sfp
->set_state(sfp
, state
);
299 static int sfp_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
301 return sfp
->read(sfp
, a2
, addr
, buf
, len
);
304 static int sfp_write(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
306 return sfp
->write(sfp
, a2
, addr
, buf
, len
);
309 static unsigned int sfp_check(void *buf
, size_t len
)
313 for (p
= buf
, check
= 0; len
; p
++, len
--)
320 static void sfp_module_tx_disable(struct sfp
*sfp
)
322 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
323 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 1);
324 sfp
->state
|= SFP_F_TX_DISABLE
;
325 sfp_set_state(sfp
, sfp
->state
);
328 static void sfp_module_tx_enable(struct sfp
*sfp
)
330 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
331 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 0);
332 sfp
->state
&= ~SFP_F_TX_DISABLE
;
333 sfp_set_state(sfp
, sfp
->state
);
336 static void sfp_module_tx_fault_reset(struct sfp
*sfp
)
338 unsigned int state
= sfp
->state
;
340 if (state
& SFP_F_TX_DISABLE
)
343 sfp_set_state(sfp
, state
| SFP_F_TX_DISABLE
);
347 sfp_set_state(sfp
, state
);
350 /* SFP state machine */
351 static void sfp_sm_set_timer(struct sfp
*sfp
, unsigned int timeout
)
354 mod_delayed_work(system_power_efficient_wq
, &sfp
->timeout
,
357 cancel_delayed_work(&sfp
->timeout
);
360 static void sfp_sm_next(struct sfp
*sfp
, unsigned int state
,
361 unsigned int timeout
)
363 sfp
->sm_state
= state
;
364 sfp_sm_set_timer(sfp
, timeout
);
367 static void sfp_sm_ins_next(struct sfp
*sfp
, unsigned int state
,
368 unsigned int timeout
)
370 sfp
->sm_mod_state
= state
;
371 sfp_sm_set_timer(sfp
, timeout
);
374 static void sfp_sm_phy_detach(struct sfp
*sfp
)
376 phy_stop(sfp
->mod_phy
);
377 sfp_remove_phy(sfp
->sfp_bus
);
378 phy_device_remove(sfp
->mod_phy
);
379 phy_device_free(sfp
->mod_phy
);
383 static void sfp_sm_probe_phy(struct sfp
*sfp
)
385 struct phy_device
*phy
;
388 msleep(T_PHY_RESET_MS
);
390 phy
= mdiobus_scan(sfp
->i2c_mii
, SFP_PHY_ADDR
);
391 if (phy
== ERR_PTR(-ENODEV
)) {
392 dev_info(sfp
->dev
, "no PHY detected\n");
396 dev_err(sfp
->dev
, "mdiobus scan returned %ld\n", PTR_ERR(phy
));
400 err
= sfp_add_phy(sfp
->sfp_bus
, phy
);
402 phy_device_remove(phy
);
403 phy_device_free(phy
);
404 dev_err(sfp
->dev
, "sfp_add_phy failed: %d\n", err
);
412 static void sfp_sm_link_up(struct sfp
*sfp
)
414 sfp_link_up(sfp
->sfp_bus
);
415 sfp_sm_next(sfp
, SFP_S_LINK_UP
, 0);
418 static void sfp_sm_link_down(struct sfp
*sfp
)
420 sfp_link_down(sfp
->sfp_bus
);
423 static void sfp_sm_link_check_los(struct sfp
*sfp
)
425 unsigned int los
= sfp
->state
& SFP_F_LOS
;
427 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
428 * are set, we assume that no LOS signal is available.
430 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
))
432 else if (!(sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
)))
436 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
441 static bool sfp_los_event_active(struct sfp
*sfp
, unsigned int event
)
443 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
444 event
== SFP_E_LOS_LOW
) ||
445 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
446 event
== SFP_E_LOS_HIGH
);
449 static bool sfp_los_event_inactive(struct sfp
*sfp
, unsigned int event
)
451 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
452 event
== SFP_E_LOS_HIGH
) ||
453 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
454 event
== SFP_E_LOS_LOW
);
457 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
459 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
461 "module persistently indicates fault, disabling\n");
462 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
465 dev_err(sfp
->dev
, "module transmit fault indicated\n");
467 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
471 static void sfp_sm_mod_init(struct sfp
*sfp
)
473 sfp_module_tx_enable(sfp
);
475 /* Wait t_init before indicating that the link is up, provided the
476 * current state indicates no TX_FAULT. If TX_FAULT clears before
477 * this time, that's fine too.
479 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
482 /* Setting the serdes link mode is guesswork: there's no
483 * field in the EEPROM which indicates what mode should
486 * If it's a gigabit-only fiber module, it probably does
487 * not have a PHY, so switch to 802.3z negotiation mode.
488 * Otherwise, switch to SGMII mode (which is required to
489 * support non-gigabit speeds) and probe for a PHY.
491 if (sfp
->id
.base
.e1000_base_t
||
492 sfp
->id
.base
.e100_base_lx
||
493 sfp
->id
.base
.e100_base_fx
)
494 sfp_sm_probe_phy(sfp
);
497 static int sfp_sm_mod_hpower(struct sfp
*sfp
)
504 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_POWER_DECL
))
506 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL
))
509 if (sfp
->id
.ext
.sff8472_compliance
== SFP_SFF8472_COMPLIANCE_NONE
&&
510 (sfp
->id
.ext
.diagmon
& (SFP_DIAGMON_DDM
| SFP_DIAGMON_ADDRMODE
)) !=
512 /* The module appears not to implement bus address 0xa2,
513 * or requires an address change sequence, so assume that
514 * the module powers up in the indicated power mode.
516 if (power
> sfp
->max_power_mW
) {
518 "Host does not support %u.%uW modules\n",
519 power
/ 1000, (power
/ 100) % 10);
525 if (power
> sfp
->max_power_mW
) {
527 "Host does not support %u.%uW modules, module left in power mode 1\n",
528 power
/ 1000, (power
/ 100) % 10);
535 err
= sfp_read(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
536 if (err
!= sizeof(val
)) {
537 dev_err(sfp
->dev
, "Failed to read EEPROM: %d\n", err
);
544 err
= sfp_write(sfp
, true, SFP_EXT_STATUS
, &val
, sizeof(val
));
545 if (err
!= sizeof(val
)) {
546 dev_err(sfp
->dev
, "Failed to write EEPROM: %d\n", err
);
551 dev_info(sfp
->dev
, "Module switched to %u.%uW power level\n",
552 power
/ 1000, (power
/ 100) % 10);
553 return T_HPOWER_LEVEL
;
559 static int sfp_sm_mod_probe(struct sfp
*sfp
)
561 /* SFP module inserted - read I2C data */
562 struct sfp_eeprom_id id
;
566 ret
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
568 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", ret
);
572 if (ret
!= sizeof(id
)) {
573 dev_err(sfp
->dev
, "EEPROM short read: %d\n", ret
);
577 /* Validate the checksum over the base structure */
578 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
579 if (check
!= id
.base
.cc_base
) {
581 "EEPROM base structure checksum failure: 0x%02x\n",
583 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
584 16, 1, &id
, sizeof(id
.base
) - 1, true);
588 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
589 if (check
!= id
.ext
.cc_ext
) {
591 "EEPROM extended structure checksum failure: 0x%02x\n",
593 memset(&id
.ext
, 0, sizeof(id
.ext
));
598 dev_info(sfp
->dev
, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
599 (int)sizeof(id
.base
.vendor_name
), id
.base
.vendor_name
,
600 (int)sizeof(id
.base
.vendor_pn
), id
.base
.vendor_pn
,
601 (int)sizeof(id
.base
.vendor_rev
), id
.base
.vendor_rev
,
602 (int)sizeof(id
.ext
.vendor_sn
), id
.ext
.vendor_sn
,
603 (int)sizeof(id
.ext
.datecode
), id
.ext
.datecode
);
605 /* Check whether we support this module */
606 if (!sfp
->type
->module_supported(&sfp
->id
)) {
608 "module is not supported - phys id 0x%02x 0x%02x\n",
609 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
613 /* If the module requires address swap mode, warn about it */
614 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
616 "module address swap to access page 0xA2 is not supported.\n");
618 ret
= sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
622 return sfp_sm_mod_hpower(sfp
);
625 static void sfp_sm_mod_remove(struct sfp
*sfp
)
627 sfp_module_remove(sfp
->sfp_bus
);
630 sfp_sm_phy_detach(sfp
);
632 sfp_module_tx_disable(sfp
);
634 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
636 dev_info(sfp
->dev
, "module removed\n");
639 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
641 mutex_lock(&sfp
->sm_mutex
);
643 dev_dbg(sfp
->dev
, "SM: enter %u:%u:%u event %u\n",
644 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
, event
);
646 /* This state machine tracks the insert/remove state of
647 * the module, and handles probing the on-board EEPROM.
649 switch (sfp
->sm_mod_state
) {
651 if (event
== SFP_E_INSERT
) {
652 sfp_module_tx_disable(sfp
);
653 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
658 if (event
== SFP_E_REMOVE
) {
659 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
660 } else if (event
== SFP_E_TIMEOUT
) {
661 int val
= sfp_sm_mod_probe(sfp
);
664 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
666 sfp_sm_ins_next(sfp
, SFP_MOD_HPOWER
, val
);
667 else if (val
!= -EAGAIN
)
668 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
670 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
675 if (event
== SFP_E_TIMEOUT
) {
676 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
680 case SFP_MOD_PRESENT
:
682 if (event
== SFP_E_REMOVE
) {
683 sfp_sm_mod_remove(sfp
);
684 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
689 /* This state machine tracks the netdev up/down state */
690 switch (sfp
->sm_dev_state
) {
692 if (event
== SFP_E_DEV_UP
)
693 sfp
->sm_dev_state
= SFP_DEV_UP
;
697 if (event
== SFP_E_DEV_DOWN
) {
698 /* If the module has a PHY, avoid raising TX disable
699 * as this resets the PHY. Otherwise, raise it to
700 * turn the laser off.
703 sfp_module_tx_disable(sfp
);
704 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
709 /* Some events are global */
710 if (sfp
->sm_state
!= SFP_S_DOWN
&&
711 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
712 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
713 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
714 sfp
->sm_dev_state
== SFP_DEV_UP
)
715 sfp_sm_link_down(sfp
);
717 sfp_sm_phy_detach(sfp
);
718 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
719 mutex_unlock(&sfp
->sm_mutex
);
723 /* The main state machine */
724 switch (sfp
->sm_state
) {
726 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
727 sfp
->sm_dev_state
== SFP_DEV_UP
)
728 sfp_sm_mod_init(sfp
);
732 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
733 sfp_sm_fault(sfp
, true);
734 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
735 sfp_sm_link_check_los(sfp
);
739 if (event
== SFP_E_TX_FAULT
)
740 sfp_sm_fault(sfp
, true);
741 else if (sfp_los_event_inactive(sfp
, event
))
746 if (event
== SFP_E_TX_FAULT
) {
747 sfp_sm_link_down(sfp
);
748 sfp_sm_fault(sfp
, true);
749 } else if (sfp_los_event_active(sfp
, event
)) {
750 sfp_sm_link_down(sfp
);
751 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
756 if (event
== SFP_E_TIMEOUT
) {
757 sfp_module_tx_fault_reset(sfp
);
758 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
763 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
764 sfp_sm_fault(sfp
, false);
765 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
766 dev_info(sfp
->dev
, "module transmit fault recovered\n");
767 sfp_sm_link_check_los(sfp
);
771 case SFP_S_TX_DISABLE
:
775 dev_dbg(sfp
->dev
, "SM: exit %u:%u:%u\n",
776 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
);
778 mutex_unlock(&sfp
->sm_mutex
);
781 static void sfp_start(struct sfp
*sfp
)
783 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
786 static void sfp_stop(struct sfp
*sfp
)
788 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
791 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
793 /* locking... and check module is present */
795 if (sfp
->id
.ext
.sff8472_compliance
&&
796 !(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)) {
797 modinfo
->type
= ETH_MODULE_SFF_8472
;
798 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
800 modinfo
->type
= ETH_MODULE_SFF_8079
;
801 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
806 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
809 unsigned int first
, last
, len
;
816 last
= ee
->offset
+ ee
->len
;
817 if (first
< ETH_MODULE_SFF_8079_LEN
) {
818 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
821 ret
= sfp_read(sfp
, false, first
, data
, len
);
828 if (first
< ETH_MODULE_SFF_8472_LEN
&& last
> ETH_MODULE_SFF_8079_LEN
) {
829 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
831 first
-= ETH_MODULE_SFF_8079_LEN
;
833 ret
= sfp_read(sfp
, true, first
, data
, len
);
840 static const struct sfp_socket_ops sfp_module_ops
= {
843 .module_info
= sfp_module_info
,
844 .module_eeprom
= sfp_module_eeprom
,
847 static void sfp_timeout(struct work_struct
*work
)
849 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
852 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
856 static void sfp_check_state(struct sfp
*sfp
)
858 unsigned int state
, i
, changed
;
860 state
= sfp_get_state(sfp
);
861 changed
= state
^ sfp
->state
;
862 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
864 for (i
= 0; i
< GPIO_MAX
; i
++)
865 if (changed
& BIT(i
))
866 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
867 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
869 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
873 if (changed
& SFP_F_PRESENT
)
874 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
875 SFP_E_INSERT
: SFP_E_REMOVE
);
877 if (changed
& SFP_F_TX_FAULT
)
878 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
879 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
881 if (changed
& SFP_F_LOS
)
882 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
883 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
887 static irqreturn_t
sfp_irq(int irq
, void *data
)
889 struct sfp
*sfp
= data
;
891 sfp_check_state(sfp
);
896 static void sfp_poll(struct work_struct
*work
)
898 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
900 sfp_check_state(sfp
);
901 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
904 static struct sfp
*sfp_alloc(struct device
*dev
)
908 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
910 return ERR_PTR(-ENOMEM
);
914 mutex_init(&sfp
->sm_mutex
);
915 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
916 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
921 static void sfp_cleanup(void *data
)
923 struct sfp
*sfp
= data
;
925 cancel_delayed_work_sync(&sfp
->poll
);
926 cancel_delayed_work_sync(&sfp
->timeout
);
928 mdiobus_unregister(sfp
->i2c_mii
);
929 mdiobus_free(sfp
->i2c_mii
);
932 i2c_put_adapter(sfp
->i2c
);
936 static int sfp_probe(struct platform_device
*pdev
)
938 const struct sff_data
*sff
;
943 sfp
= sfp_alloc(&pdev
->dev
);
947 platform_set_drvdata(pdev
, sfp
);
949 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
953 sff
= sfp
->type
= &sfp_data
;
955 if (pdev
->dev
.of_node
) {
956 struct device_node
*node
= pdev
->dev
.of_node
;
957 const struct of_device_id
*id
;
958 struct device_node
*np
;
960 id
= of_match_node(sfp_of_match
, node
);
964 sff
= sfp
->type
= id
->data
;
966 np
= of_parse_phandle(node
, "i2c-bus", 0);
968 struct i2c_adapter
*i2c
;
970 i2c
= of_find_i2c_adapter_by_node(np
);
973 return -EPROBE_DEFER
;
975 err
= sfp_i2c_configure(sfp
, i2c
);
977 i2c_put_adapter(i2c
);
983 for (i
= 0; i
< GPIO_MAX
; i
++)
984 if (sff
->gpios
& BIT(i
)) {
985 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
986 gpio_of_names
[i
], gpio_flags
[i
]);
987 if (IS_ERR(sfp
->gpio
[i
]))
988 return PTR_ERR(sfp
->gpio
[i
]);
991 sfp
->get_state
= sfp_gpio_get_state
;
992 sfp
->set_state
= sfp_gpio_set_state
;
994 /* Modules that have no detect signal are always present */
995 if (!(sfp
->gpio
[GPIO_MODDEF0
]))
996 sfp
->get_state
= sff_gpio_get_state
;
998 device_property_read_u32(&pdev
->dev
, "maximum-power-milliwatt",
1000 if (!sfp
->max_power_mW
)
1001 sfp
->max_power_mW
= 1000;
1003 dev_info(sfp
->dev
, "Host maximum power %u.%uW\n",
1004 sfp
->max_power_mW
/ 1000, (sfp
->max_power_mW
/ 100) % 10);
1006 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
1010 /* Get the initial state, and always signal TX disable,
1011 * since the network interface will not be up.
1013 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
1015 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
1016 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
1017 sfp
->state
|= SFP_F_RATE_SELECT
;
1018 sfp_set_state(sfp
, sfp
->state
);
1019 sfp_module_tx_disable(sfp
);
1021 if (sfp
->state
& SFP_F_PRESENT
)
1022 sfp_sm_event(sfp
, SFP_E_INSERT
);
1025 for (i
= 0; i
< GPIO_MAX
; i
++) {
1026 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
1029 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
1035 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
1037 IRQF_TRIGGER_RISING
|
1038 IRQF_TRIGGER_FALLING
,
1039 dev_name(sfp
->dev
), sfp
);
1045 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
1050 static int sfp_remove(struct platform_device
*pdev
)
1052 struct sfp
*sfp
= platform_get_drvdata(pdev
);
1054 sfp_unregister_socket(sfp
->sfp_bus
);
1059 static struct platform_driver sfp_driver
= {
1061 .remove
= sfp_remove
,
1064 .of_match_table
= sfp_of_match
,
1068 static int sfp_init(void)
1070 poll_jiffies
= msecs_to_jiffies(100);
1072 return platform_driver_register(&sfp_driver
);
1074 module_init(sfp_init
);
1076 static void sfp_exit(void)
1078 platform_driver_unregister(&sfp_driver
);
1080 module_exit(sfp_exit
);
1082 MODULE_ALIAS("platform:sfp");
1083 MODULE_AUTHOR("Russell King");
1084 MODULE_LICENSE("GPL v2");