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
),
60 static const char *gpio_of_names
[] = {
68 static const enum gpiod_flags gpio_flags
[] = {
76 #define T_INIT_JIFFIES msecs_to_jiffies(300)
78 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
80 /* SFP module presence detection is poor: the three MOD DEF signals are
81 * the same length on the PCB, which means it's possible for MOD DEF 0 to
82 * connect before the I2C bus on MOD DEF 1/2.
84 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
85 * be deasserted) but makes no mention of the earliest time before we can
86 * access the I2C EEPROM. However, Avago modules require 300ms.
88 #define T_PROBE_INIT msecs_to_jiffies(300)
89 #define T_PROBE_RETRY msecs_to_jiffies(100)
91 /* SFP modules appear to always have their PHY configured for bus address
92 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
94 #define SFP_PHY_ADDR 22
96 /* Give this long for the PHY to reset. */
97 #define T_PHY_RESET_MS 50
99 static DEFINE_MUTEX(sfp_mutex
);
103 bool (*module_supported
)(const struct sfp_eeprom_id
*id
);
108 struct i2c_adapter
*i2c
;
109 struct mii_bus
*i2c_mii
;
110 struct sfp_bus
*sfp_bus
;
111 struct phy_device
*mod_phy
;
112 const struct sff_data
*type
;
114 unsigned int (*get_state
)(struct sfp
*);
115 void (*set_state
)(struct sfp
*, unsigned int);
116 int (*read
)(struct sfp
*, bool, u8
, void *, size_t);
118 struct gpio_desc
*gpio
[GPIO_MAX
];
121 struct delayed_work poll
;
122 struct delayed_work timeout
;
123 struct mutex sm_mutex
;
124 unsigned char sm_mod_state
;
125 unsigned char sm_dev_state
;
126 unsigned short sm_state
;
127 unsigned int sm_retries
;
129 struct sfp_eeprom_id id
;
132 static bool sff_module_supported(const struct sfp_eeprom_id
*id
)
134 return id
->base
.phys_id
== SFP_PHYS_ID_SFF
&&
135 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
138 static const struct sff_data sff_data
= {
139 .gpios
= SFP_F_LOS
| SFP_F_TX_FAULT
| SFP_F_TX_DISABLE
,
140 .module_supported
= sff_module_supported
,
143 static bool sfp_module_supported(const struct sfp_eeprom_id
*id
)
145 return id
->base
.phys_id
== SFP_PHYS_ID_SFP
&&
146 id
->base
.phys_ext_id
== SFP_PHYS_EXT_ID_SFP
;
149 static const struct sff_data sfp_data
= {
150 .gpios
= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
|
151 SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
,
152 .module_supported
= sfp_module_supported
,
155 static const struct of_device_id sfp_of_match
[] = {
156 { .compatible
= "sff,sff", .data
= &sff_data
, },
157 { .compatible
= "sff,sfp", .data
= &sfp_data
, },
160 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
162 static unsigned long poll_jiffies
;
164 static unsigned int sfp_gpio_get_state(struct sfp
*sfp
)
166 unsigned int i
, state
, v
;
168 for (i
= state
= 0; i
< GPIO_MAX
; i
++) {
169 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
172 v
= gpiod_get_value_cansleep(sfp
->gpio
[i
]);
180 static unsigned int sff_gpio_get_state(struct sfp
*sfp
)
182 return sfp_gpio_get_state(sfp
) | SFP_F_PRESENT
;
185 static void sfp_gpio_set_state(struct sfp
*sfp
, unsigned int state
)
187 if (state
& SFP_F_PRESENT
) {
188 /* If the module is present, drive the signals */
189 if (sfp
->gpio
[GPIO_TX_DISABLE
])
190 gpiod_direction_output(sfp
->gpio
[GPIO_TX_DISABLE
],
191 state
& SFP_F_TX_DISABLE
);
192 if (state
& SFP_F_RATE_SELECT
)
193 gpiod_direction_output(sfp
->gpio
[GPIO_RATE_SELECT
],
194 state
& SFP_F_RATE_SELECT
);
196 /* Otherwise, let them float to the pull-ups */
197 if (sfp
->gpio
[GPIO_TX_DISABLE
])
198 gpiod_direction_input(sfp
->gpio
[GPIO_TX_DISABLE
]);
199 if (state
& SFP_F_RATE_SELECT
)
200 gpiod_direction_input(sfp
->gpio
[GPIO_RATE_SELECT
]);
204 static int sfp__i2c_read(struct i2c_adapter
*i2c
, u8 bus_addr
, u8 dev_addr
,
205 void *buf
, size_t len
)
207 struct i2c_msg msgs
[2];
210 msgs
[0].addr
= bus_addr
;
213 msgs
[0].buf
= &dev_addr
;
214 msgs
[1].addr
= bus_addr
;
215 msgs
[1].flags
= I2C_M_RD
;
219 ret
= i2c_transfer(i2c
, msgs
, ARRAY_SIZE(msgs
));
223 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
226 static int sfp_i2c_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
,
229 return sfp__i2c_read(sfp
->i2c
, a2
? 0x51 : 0x50, addr
, buf
, len
);
232 static int sfp_i2c_configure(struct sfp
*sfp
, struct i2c_adapter
*i2c
)
234 struct mii_bus
*i2c_mii
;
237 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
241 sfp
->read
= sfp_i2c_read
;
243 i2c_mii
= mdio_i2c_alloc(sfp
->dev
, i2c
);
245 return PTR_ERR(i2c_mii
);
247 i2c_mii
->name
= "SFP I2C Bus";
248 i2c_mii
->phy_mask
= ~0;
250 ret
= mdiobus_register(i2c_mii
);
252 mdiobus_free(i2c_mii
);
256 sfp
->i2c_mii
= i2c_mii
;
262 static unsigned int sfp_get_state(struct sfp
*sfp
)
264 return sfp
->get_state(sfp
);
267 static void sfp_set_state(struct sfp
*sfp
, unsigned int state
)
269 sfp
->set_state(sfp
, state
);
272 static int sfp_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
274 return sfp
->read(sfp
, a2
, addr
, buf
, len
);
277 static unsigned int sfp_check(void *buf
, size_t len
)
281 for (p
= buf
, check
= 0; len
; p
++, len
--)
288 static void sfp_module_tx_disable(struct sfp
*sfp
)
290 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
291 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 1);
292 sfp
->state
|= SFP_F_TX_DISABLE
;
293 sfp_set_state(sfp
, sfp
->state
);
296 static void sfp_module_tx_enable(struct sfp
*sfp
)
298 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
299 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 0);
300 sfp
->state
&= ~SFP_F_TX_DISABLE
;
301 sfp_set_state(sfp
, sfp
->state
);
304 static void sfp_module_tx_fault_reset(struct sfp
*sfp
)
306 unsigned int state
= sfp
->state
;
308 if (state
& SFP_F_TX_DISABLE
)
311 sfp_set_state(sfp
, state
| SFP_F_TX_DISABLE
);
315 sfp_set_state(sfp
, state
);
318 /* SFP state machine */
319 static void sfp_sm_set_timer(struct sfp
*sfp
, unsigned int timeout
)
322 mod_delayed_work(system_power_efficient_wq
, &sfp
->timeout
,
325 cancel_delayed_work(&sfp
->timeout
);
328 static void sfp_sm_next(struct sfp
*sfp
, unsigned int state
,
329 unsigned int timeout
)
331 sfp
->sm_state
= state
;
332 sfp_sm_set_timer(sfp
, timeout
);
335 static void sfp_sm_ins_next(struct sfp
*sfp
, unsigned int state
,
336 unsigned int timeout
)
338 sfp
->sm_mod_state
= state
;
339 sfp_sm_set_timer(sfp
, timeout
);
342 static void sfp_sm_phy_detach(struct sfp
*sfp
)
344 phy_stop(sfp
->mod_phy
);
345 sfp_remove_phy(sfp
->sfp_bus
);
346 phy_device_remove(sfp
->mod_phy
);
347 phy_device_free(sfp
->mod_phy
);
351 static void sfp_sm_probe_phy(struct sfp
*sfp
)
353 struct phy_device
*phy
;
356 msleep(T_PHY_RESET_MS
);
358 phy
= mdiobus_scan(sfp
->i2c_mii
, SFP_PHY_ADDR
);
359 if (phy
== ERR_PTR(-ENODEV
)) {
360 dev_info(sfp
->dev
, "no PHY detected\n");
364 dev_err(sfp
->dev
, "mdiobus scan returned %ld\n", PTR_ERR(phy
));
368 err
= sfp_add_phy(sfp
->sfp_bus
, phy
);
370 phy_device_remove(phy
);
371 phy_device_free(phy
);
372 dev_err(sfp
->dev
, "sfp_add_phy failed: %d\n", err
);
380 static void sfp_sm_link_up(struct sfp
*sfp
)
382 sfp_link_up(sfp
->sfp_bus
);
383 sfp_sm_next(sfp
, SFP_S_LINK_UP
, 0);
386 static void sfp_sm_link_down(struct sfp
*sfp
)
388 sfp_link_down(sfp
->sfp_bus
);
391 static void sfp_sm_link_check_los(struct sfp
*sfp
)
393 unsigned int los
= sfp
->state
& SFP_F_LOS
;
395 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
396 * are set, we assume that no LOS signal is available.
398 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
))
400 else if (!(sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
)))
404 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
409 static bool sfp_los_event_active(struct sfp
*sfp
, unsigned int event
)
411 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
412 event
== SFP_E_LOS_LOW
) ||
413 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
414 event
== SFP_E_LOS_HIGH
);
417 static bool sfp_los_event_inactive(struct sfp
*sfp
, unsigned int event
)
419 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
420 event
== SFP_E_LOS_HIGH
) ||
421 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
422 event
== SFP_E_LOS_LOW
);
425 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
427 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
429 "module persistently indicates fault, disabling\n");
430 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
433 dev_err(sfp
->dev
, "module transmit fault indicated\n");
435 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
439 static void sfp_sm_mod_init(struct sfp
*sfp
)
441 sfp_module_tx_enable(sfp
);
443 /* Wait t_init before indicating that the link is up, provided the
444 * current state indicates no TX_FAULT. If TX_FAULT clears before
445 * this time, that's fine too.
447 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
450 /* Setting the serdes link mode is guesswork: there's no
451 * field in the EEPROM which indicates what mode should
454 * If it's a gigabit-only fiber module, it probably does
455 * not have a PHY, so switch to 802.3z negotiation mode.
456 * Otherwise, switch to SGMII mode (which is required to
457 * support non-gigabit speeds) and probe for a PHY.
459 if (sfp
->id
.base
.e1000_base_t
||
460 sfp
->id
.base
.e100_base_lx
||
461 sfp
->id
.base
.e100_base_fx
)
462 sfp_sm_probe_phy(sfp
);
465 static int sfp_sm_mod_probe(struct sfp
*sfp
)
467 /* SFP module inserted - read I2C data */
468 struct sfp_eeprom_id id
;
472 err
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
474 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", err
);
478 if (err
!= sizeof(id
)) {
479 dev_err(sfp
->dev
, "EEPROM short read: %d\n", err
);
483 /* Validate the checksum over the base structure */
484 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
485 if (check
!= id
.base
.cc_base
) {
487 "EEPROM base structure checksum failure: 0x%02x\n",
489 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
490 16, 1, &id
, sizeof(id
.base
) - 1, true);
494 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
495 if (check
!= id
.ext
.cc_ext
) {
497 "EEPROM extended structure checksum failure: 0x%02x\n",
499 memset(&id
.ext
, 0, sizeof(id
.ext
));
504 dev_info(sfp
->dev
, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
505 (int)sizeof(id
.base
.vendor_name
), id
.base
.vendor_name
,
506 (int)sizeof(id
.base
.vendor_pn
), id
.base
.vendor_pn
,
507 (int)sizeof(id
.base
.vendor_rev
), id
.base
.vendor_rev
,
508 (int)sizeof(id
.ext
.vendor_sn
), id
.ext
.vendor_sn
,
509 (int)sizeof(id
.ext
.datecode
), id
.ext
.datecode
);
511 /* Check whether we support this module */
512 if (!sfp
->type
->module_supported(&sfp
->id
)) {
514 "module is not supported - phys id 0x%02x 0x%02x\n",
515 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
519 /* If the module requires address swap mode, warn about it */
520 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
522 "module address swap to access page 0xA2 is not supported.\n");
524 return sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
527 static void sfp_sm_mod_remove(struct sfp
*sfp
)
529 sfp_module_remove(sfp
->sfp_bus
);
532 sfp_sm_phy_detach(sfp
);
534 sfp_module_tx_disable(sfp
);
536 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
538 dev_info(sfp
->dev
, "module removed\n");
541 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
543 mutex_lock(&sfp
->sm_mutex
);
545 dev_dbg(sfp
->dev
, "SM: enter %u:%u:%u event %u\n",
546 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
, event
);
548 /* This state machine tracks the insert/remove state of
549 * the module, and handles probing the on-board EEPROM.
551 switch (sfp
->sm_mod_state
) {
553 if (event
== SFP_E_INSERT
) {
554 sfp_module_tx_disable(sfp
);
555 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
560 if (event
== SFP_E_REMOVE
) {
561 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
562 } else if (event
== SFP_E_TIMEOUT
) {
563 int err
= sfp_sm_mod_probe(sfp
);
566 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
567 else if (err
== -EAGAIN
)
568 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
570 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
574 case SFP_MOD_PRESENT
:
576 if (event
== SFP_E_REMOVE
) {
577 sfp_sm_mod_remove(sfp
);
578 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
583 /* This state machine tracks the netdev up/down state */
584 switch (sfp
->sm_dev_state
) {
586 if (event
== SFP_E_DEV_UP
)
587 sfp
->sm_dev_state
= SFP_DEV_UP
;
591 if (event
== SFP_E_DEV_DOWN
) {
592 /* If the module has a PHY, avoid raising TX disable
593 * as this resets the PHY. Otherwise, raise it to
594 * turn the laser off.
597 sfp_module_tx_disable(sfp
);
598 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
603 /* Some events are global */
604 if (sfp
->sm_state
!= SFP_S_DOWN
&&
605 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
606 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
607 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
608 sfp
->sm_dev_state
== SFP_DEV_UP
)
609 sfp_sm_link_down(sfp
);
611 sfp_sm_phy_detach(sfp
);
612 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
613 mutex_unlock(&sfp
->sm_mutex
);
617 /* The main state machine */
618 switch (sfp
->sm_state
) {
620 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
621 sfp
->sm_dev_state
== SFP_DEV_UP
)
622 sfp_sm_mod_init(sfp
);
626 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
627 sfp_sm_fault(sfp
, true);
628 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
629 sfp_sm_link_check_los(sfp
);
633 if (event
== SFP_E_TX_FAULT
)
634 sfp_sm_fault(sfp
, true);
635 else if (sfp_los_event_inactive(sfp
, event
))
640 if (event
== SFP_E_TX_FAULT
) {
641 sfp_sm_link_down(sfp
);
642 sfp_sm_fault(sfp
, true);
643 } else if (sfp_los_event_active(sfp
, event
)) {
644 sfp_sm_link_down(sfp
);
645 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
650 if (event
== SFP_E_TIMEOUT
) {
651 sfp_module_tx_fault_reset(sfp
);
652 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
657 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
658 sfp_sm_fault(sfp
, false);
659 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
660 dev_info(sfp
->dev
, "module transmit fault recovered\n");
661 sfp_sm_link_check_los(sfp
);
665 case SFP_S_TX_DISABLE
:
669 dev_dbg(sfp
->dev
, "SM: exit %u:%u:%u\n",
670 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
);
672 mutex_unlock(&sfp
->sm_mutex
);
675 static void sfp_start(struct sfp
*sfp
)
677 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
680 static void sfp_stop(struct sfp
*sfp
)
682 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
685 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
687 /* locking... and check module is present */
689 if (sfp
->id
.ext
.sff8472_compliance
&&
690 !(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)) {
691 modinfo
->type
= ETH_MODULE_SFF_8472
;
692 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
694 modinfo
->type
= ETH_MODULE_SFF_8079
;
695 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
700 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
703 unsigned int first
, last
, len
;
710 last
= ee
->offset
+ ee
->len
;
711 if (first
< ETH_MODULE_SFF_8079_LEN
) {
712 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
715 ret
= sfp_read(sfp
, false, first
, data
, len
);
722 if (first
< ETH_MODULE_SFF_8472_LEN
&& last
> ETH_MODULE_SFF_8079_LEN
) {
723 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
725 first
-= ETH_MODULE_SFF_8079_LEN
;
727 ret
= sfp_read(sfp
, true, first
, data
, len
);
734 static const struct sfp_socket_ops sfp_module_ops
= {
737 .module_info
= sfp_module_info
,
738 .module_eeprom
= sfp_module_eeprom
,
741 static void sfp_timeout(struct work_struct
*work
)
743 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
746 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
750 static void sfp_check_state(struct sfp
*sfp
)
752 unsigned int state
, i
, changed
;
754 state
= sfp_get_state(sfp
);
755 changed
= state
^ sfp
->state
;
756 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
758 for (i
= 0; i
< GPIO_MAX
; i
++)
759 if (changed
& BIT(i
))
760 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
761 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
763 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
767 if (changed
& SFP_F_PRESENT
)
768 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
769 SFP_E_INSERT
: SFP_E_REMOVE
);
771 if (changed
& SFP_F_TX_FAULT
)
772 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
773 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
775 if (changed
& SFP_F_LOS
)
776 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
777 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
781 static irqreturn_t
sfp_irq(int irq
, void *data
)
783 struct sfp
*sfp
= data
;
785 sfp_check_state(sfp
);
790 static void sfp_poll(struct work_struct
*work
)
792 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
794 sfp_check_state(sfp
);
795 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
798 static struct sfp
*sfp_alloc(struct device
*dev
)
802 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
804 return ERR_PTR(-ENOMEM
);
808 mutex_init(&sfp
->sm_mutex
);
809 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
810 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
815 static void sfp_cleanup(void *data
)
817 struct sfp
*sfp
= data
;
819 cancel_delayed_work_sync(&sfp
->poll
);
820 cancel_delayed_work_sync(&sfp
->timeout
);
822 mdiobus_unregister(sfp
->i2c_mii
);
823 mdiobus_free(sfp
->i2c_mii
);
826 i2c_put_adapter(sfp
->i2c
);
830 static int sfp_probe(struct platform_device
*pdev
)
832 const struct sff_data
*sff
;
837 sfp
= sfp_alloc(&pdev
->dev
);
841 platform_set_drvdata(pdev
, sfp
);
843 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
847 sff
= sfp
->type
= &sfp_data
;
849 if (pdev
->dev
.of_node
) {
850 struct device_node
*node
= pdev
->dev
.of_node
;
851 const struct of_device_id
*id
;
852 struct device_node
*np
;
854 id
= of_match_node(sfp_of_match
, node
);
858 sff
= sfp
->type
= id
->data
;
860 np
= of_parse_phandle(node
, "i2c-bus", 0);
862 struct i2c_adapter
*i2c
;
864 i2c
= of_find_i2c_adapter_by_node(np
);
867 return -EPROBE_DEFER
;
869 err
= sfp_i2c_configure(sfp
, i2c
);
871 i2c_put_adapter(i2c
);
877 for (i
= 0; i
< GPIO_MAX
; i
++)
878 if (sff
->gpios
& BIT(i
)) {
879 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
880 gpio_of_names
[i
], gpio_flags
[i
]);
881 if (IS_ERR(sfp
->gpio
[i
]))
882 return PTR_ERR(sfp
->gpio
[i
]);
885 sfp
->get_state
= sfp_gpio_get_state
;
886 sfp
->set_state
= sfp_gpio_set_state
;
888 /* Modules that have no detect signal are always present */
889 if (!(sfp
->gpio
[GPIO_MODDEF0
]))
890 sfp
->get_state
= sff_gpio_get_state
;
892 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
896 /* Get the initial state, and always signal TX disable,
897 * since the network interface will not be up.
899 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
901 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
902 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
903 sfp
->state
|= SFP_F_RATE_SELECT
;
904 sfp_set_state(sfp
, sfp
->state
);
905 sfp_module_tx_disable(sfp
);
907 if (sfp
->state
& SFP_F_PRESENT
)
908 sfp_sm_event(sfp
, SFP_E_INSERT
);
911 for (i
= 0; i
< GPIO_MAX
; i
++) {
912 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
915 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
921 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
923 IRQF_TRIGGER_RISING
|
924 IRQF_TRIGGER_FALLING
,
925 dev_name(sfp
->dev
), sfp
);
931 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
936 static int sfp_remove(struct platform_device
*pdev
)
938 struct sfp
*sfp
= platform_get_drvdata(pdev
);
940 sfp_unregister_socket(sfp
->sfp_bus
);
945 static struct platform_driver sfp_driver
= {
947 .remove
= sfp_remove
,
950 .of_match_table
= sfp_of_match
,
954 static int sfp_init(void)
956 poll_jiffies
= msecs_to_jiffies(100);
958 return platform_driver_register(&sfp_driver
);
960 module_init(sfp_init
);
962 static void sfp_exit(void)
964 platform_driver_unregister(&sfp_driver
);
966 module_exit(sfp_exit
);
968 MODULE_ALIAS("platform:sfp");
969 MODULE_AUTHOR("Russell King");
970 MODULE_LICENSE("GPL v2");