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 struct i2c_adapter
*i2c
;
104 struct mii_bus
*i2c_mii
;
105 struct sfp_bus
*sfp_bus
;
106 struct phy_device
*mod_phy
;
108 unsigned int (*get_state
)(struct sfp
*);
109 void (*set_state
)(struct sfp
*, unsigned int);
110 int (*read
)(struct sfp
*, bool, u8
, void *, size_t);
112 struct gpio_desc
*gpio
[GPIO_MAX
];
115 struct delayed_work poll
;
116 struct delayed_work timeout
;
117 struct mutex sm_mutex
;
118 unsigned char sm_mod_state
;
119 unsigned char sm_dev_state
;
120 unsigned short sm_state
;
121 unsigned int sm_retries
;
123 struct sfp_eeprom_id id
;
126 static unsigned long poll_jiffies
;
128 static unsigned int sfp_gpio_get_state(struct sfp
*sfp
)
130 unsigned int i
, state
, v
;
132 for (i
= state
= 0; i
< GPIO_MAX
; i
++) {
133 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
136 v
= gpiod_get_value_cansleep(sfp
->gpio
[i
]);
144 static void sfp_gpio_set_state(struct sfp
*sfp
, unsigned int state
)
146 if (state
& SFP_F_PRESENT
) {
147 /* If the module is present, drive the signals */
148 if (sfp
->gpio
[GPIO_TX_DISABLE
])
149 gpiod_direction_output(sfp
->gpio
[GPIO_TX_DISABLE
],
150 state
& SFP_F_TX_DISABLE
);
151 if (state
& SFP_F_RATE_SELECT
)
152 gpiod_direction_output(sfp
->gpio
[GPIO_RATE_SELECT
],
153 state
& SFP_F_RATE_SELECT
);
155 /* Otherwise, let them float to the pull-ups */
156 if (sfp
->gpio
[GPIO_TX_DISABLE
])
157 gpiod_direction_input(sfp
->gpio
[GPIO_TX_DISABLE
]);
158 if (state
& SFP_F_RATE_SELECT
)
159 gpiod_direction_input(sfp
->gpio
[GPIO_RATE_SELECT
]);
163 static int sfp__i2c_read(struct i2c_adapter
*i2c
, u8 bus_addr
, u8 dev_addr
,
164 void *buf
, size_t len
)
166 struct i2c_msg msgs
[2];
169 msgs
[0].addr
= bus_addr
;
172 msgs
[0].buf
= &dev_addr
;
173 msgs
[1].addr
= bus_addr
;
174 msgs
[1].flags
= I2C_M_RD
;
178 ret
= i2c_transfer(i2c
, msgs
, ARRAY_SIZE(msgs
));
182 return ret
== ARRAY_SIZE(msgs
) ? len
: 0;
185 static int sfp_i2c_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
,
188 return sfp__i2c_read(sfp
->i2c
, a2
? 0x51 : 0x50, addr
, buf
, len
);
191 static int sfp_i2c_configure(struct sfp
*sfp
, struct i2c_adapter
*i2c
)
193 struct mii_bus
*i2c_mii
;
196 if (!i2c_check_functionality(i2c
, I2C_FUNC_I2C
))
200 sfp
->read
= sfp_i2c_read
;
202 i2c_mii
= mdio_i2c_alloc(sfp
->dev
, i2c
);
204 return PTR_ERR(i2c_mii
);
206 i2c_mii
->name
= "SFP I2C Bus";
207 i2c_mii
->phy_mask
= ~0;
209 ret
= mdiobus_register(i2c_mii
);
211 mdiobus_free(i2c_mii
);
215 sfp
->i2c_mii
= i2c_mii
;
221 static unsigned int sfp_get_state(struct sfp
*sfp
)
223 return sfp
->get_state(sfp
);
226 static void sfp_set_state(struct sfp
*sfp
, unsigned int state
)
228 sfp
->set_state(sfp
, state
);
231 static int sfp_read(struct sfp
*sfp
, bool a2
, u8 addr
, void *buf
, size_t len
)
233 return sfp
->read(sfp
, a2
, addr
, buf
, len
);
236 static unsigned int sfp_check(void *buf
, size_t len
)
240 for (p
= buf
, check
= 0; len
; p
++, len
--)
247 static void sfp_module_tx_disable(struct sfp
*sfp
)
249 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
250 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 1);
251 sfp
->state
|= SFP_F_TX_DISABLE
;
252 sfp_set_state(sfp
, sfp
->state
);
255 static void sfp_module_tx_enable(struct sfp
*sfp
)
257 dev_dbg(sfp
->dev
, "tx disable %u -> %u\n",
258 sfp
->state
& SFP_F_TX_DISABLE
? 1 : 0, 0);
259 sfp
->state
&= ~SFP_F_TX_DISABLE
;
260 sfp_set_state(sfp
, sfp
->state
);
263 static void sfp_module_tx_fault_reset(struct sfp
*sfp
)
265 unsigned int state
= sfp
->state
;
267 if (state
& SFP_F_TX_DISABLE
)
270 sfp_set_state(sfp
, state
| SFP_F_TX_DISABLE
);
274 sfp_set_state(sfp
, state
);
277 /* SFP state machine */
278 static void sfp_sm_set_timer(struct sfp
*sfp
, unsigned int timeout
)
281 mod_delayed_work(system_power_efficient_wq
, &sfp
->timeout
,
284 cancel_delayed_work(&sfp
->timeout
);
287 static void sfp_sm_next(struct sfp
*sfp
, unsigned int state
,
288 unsigned int timeout
)
290 sfp
->sm_state
= state
;
291 sfp_sm_set_timer(sfp
, timeout
);
294 static void sfp_sm_ins_next(struct sfp
*sfp
, unsigned int state
,
295 unsigned int timeout
)
297 sfp
->sm_mod_state
= state
;
298 sfp_sm_set_timer(sfp
, timeout
);
301 static void sfp_sm_phy_detach(struct sfp
*sfp
)
303 phy_stop(sfp
->mod_phy
);
304 sfp_remove_phy(sfp
->sfp_bus
);
305 phy_device_remove(sfp
->mod_phy
);
306 phy_device_free(sfp
->mod_phy
);
310 static void sfp_sm_probe_phy(struct sfp
*sfp
)
312 struct phy_device
*phy
;
315 msleep(T_PHY_RESET_MS
);
317 phy
= mdiobus_scan(sfp
->i2c_mii
, SFP_PHY_ADDR
);
319 dev_err(sfp
->dev
, "mdiobus scan returned %ld\n", PTR_ERR(phy
));
323 dev_info(sfp
->dev
, "no PHY detected\n");
327 err
= sfp_add_phy(sfp
->sfp_bus
, phy
);
329 phy_device_remove(phy
);
330 phy_device_free(phy
);
331 dev_err(sfp
->dev
, "sfp_add_phy failed: %d\n", err
);
339 static void sfp_sm_link_up(struct sfp
*sfp
)
341 sfp_link_up(sfp
->sfp_bus
);
342 sfp_sm_next(sfp
, SFP_S_LINK_UP
, 0);
345 static void sfp_sm_link_down(struct sfp
*sfp
)
347 sfp_link_down(sfp
->sfp_bus
);
350 static void sfp_sm_link_check_los(struct sfp
*sfp
)
352 unsigned int los
= sfp
->state
& SFP_F_LOS
;
354 /* FIXME: what if neither SFP_OPTIONS_LOS_INVERTED nor
355 * SFP_OPTIONS_LOS_NORMAL are set? For now, we assume
356 * the same as SFP_OPTIONS_LOS_NORMAL set.
358 if (sfp
->id
.ext
.options
& SFP_OPTIONS_LOS_INVERTED
)
362 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
367 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
369 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
371 "module persistently indicates fault, disabling\n");
372 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
375 dev_err(sfp
->dev
, "module transmit fault indicated\n");
377 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
381 static void sfp_sm_mod_init(struct sfp
*sfp
)
383 sfp_module_tx_enable(sfp
);
385 /* Wait t_init before indicating that the link is up, provided the
386 * current state indicates no TX_FAULT. If TX_FAULT clears before
387 * this time, that's fine too.
389 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
392 /* Setting the serdes link mode is guesswork: there's no
393 * field in the EEPROM which indicates what mode should
396 * If it's a gigabit-only fiber module, it probably does
397 * not have a PHY, so switch to 802.3z negotiation mode.
398 * Otherwise, switch to SGMII mode (which is required to
399 * support non-gigabit speeds) and probe for a PHY.
401 if (sfp
->id
.base
.e1000_base_t
||
402 sfp
->id
.base
.e100_base_lx
||
403 sfp
->id
.base
.e100_base_fx
)
404 sfp_sm_probe_phy(sfp
);
407 static int sfp_sm_mod_probe(struct sfp
*sfp
)
409 /* SFP module inserted - read I2C data */
410 struct sfp_eeprom_id id
;
419 err
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
421 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", err
);
425 if (err
!= sizeof(id
)) {
426 dev_err(sfp
->dev
, "EEPROM short read: %d\n", err
);
430 /* Validate the checksum over the base structure */
431 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
432 if (check
!= id
.base
.cc_base
) {
434 "EEPROM base structure checksum failure: 0x%02x\n",
436 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
437 16, 1, &id
, sizeof(id
.base
) - 1, true);
441 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
442 if (check
!= id
.ext
.cc_ext
) {
444 "EEPROM extended structure checksum failure: 0x%02x\n",
446 memset(&id
.ext
, 0, sizeof(id
.ext
));
451 memcpy(vendor
, sfp
->id
.base
.vendor_name
, 16);
453 memcpy(part
, sfp
->id
.base
.vendor_pn
, 16);
455 memcpy(rev
, sfp
->id
.base
.vendor_rev
, 4);
457 memcpy(sn
, sfp
->id
.ext
.vendor_sn
, 16);
459 memcpy(date
, sfp
->id
.ext
.datecode
, 8);
462 dev_info(sfp
->dev
, "module %s %s rev %s sn %s dc %s\n",
463 vendor
, part
, rev
, sn
, date
);
465 /* We only support SFP modules, not the legacy GBIC modules. */
466 if (sfp
->id
.base
.phys_id
!= SFP_PHYS_ID_SFP
||
467 sfp
->id
.base
.phys_ext_id
!= SFP_PHYS_EXT_ID_SFP
) {
468 dev_err(sfp
->dev
, "module is not SFP - phys id 0x%02x 0x%02x\n",
469 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
473 return sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
476 static void sfp_sm_mod_remove(struct sfp
*sfp
)
478 sfp_module_remove(sfp
->sfp_bus
);
481 sfp_sm_phy_detach(sfp
);
483 sfp_module_tx_disable(sfp
);
485 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
487 dev_info(sfp
->dev
, "module removed\n");
490 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
492 mutex_lock(&sfp
->sm_mutex
);
494 dev_dbg(sfp
->dev
, "SM: enter %u:%u:%u event %u\n",
495 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
, event
);
497 /* This state machine tracks the insert/remove state of
498 * the module, and handles probing the on-board EEPROM.
500 switch (sfp
->sm_mod_state
) {
502 if (event
== SFP_E_INSERT
) {
503 sfp_module_tx_disable(sfp
);
504 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
509 if (event
== SFP_E_REMOVE
) {
510 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
511 } else if (event
== SFP_E_TIMEOUT
) {
512 int err
= sfp_sm_mod_probe(sfp
);
515 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
516 else if (err
== -EAGAIN
)
517 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
519 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
523 case SFP_MOD_PRESENT
:
525 if (event
== SFP_E_REMOVE
) {
526 sfp_sm_mod_remove(sfp
);
527 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
532 /* This state machine tracks the netdev up/down state */
533 switch (sfp
->sm_dev_state
) {
535 if (event
== SFP_E_DEV_UP
)
536 sfp
->sm_dev_state
= SFP_DEV_UP
;
540 if (event
== SFP_E_DEV_DOWN
) {
541 /* If the module has a PHY, avoid raising TX disable
542 * as this resets the PHY. Otherwise, raise it to
543 * turn the laser off.
546 sfp_module_tx_disable(sfp
);
547 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
552 /* Some events are global */
553 if (sfp
->sm_state
!= SFP_S_DOWN
&&
554 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
555 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
556 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
557 sfp
->sm_dev_state
== SFP_DEV_UP
)
558 sfp_sm_link_down(sfp
);
560 sfp_sm_phy_detach(sfp
);
561 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
562 mutex_unlock(&sfp
->sm_mutex
);
566 /* The main state machine */
567 switch (sfp
->sm_state
) {
569 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
570 sfp
->sm_dev_state
== SFP_DEV_UP
)
571 sfp_sm_mod_init(sfp
);
575 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
576 sfp_sm_fault(sfp
, true);
577 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
578 sfp_sm_link_check_los(sfp
);
582 if (event
== SFP_E_TX_FAULT
)
583 sfp_sm_fault(sfp
, true);
585 (sfp
->id
.ext
.options
& SFP_OPTIONS_LOS_INVERTED
?
586 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
))
591 if (event
== SFP_E_TX_FAULT
) {
592 sfp_sm_link_down(sfp
);
593 sfp_sm_fault(sfp
, true);
595 (sfp
->id
.ext
.options
& SFP_OPTIONS_LOS_INVERTED
?
596 SFP_E_LOS_LOW
: SFP_E_LOS_HIGH
)) {
597 sfp_sm_link_down(sfp
);
598 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
603 if (event
== SFP_E_TIMEOUT
) {
604 sfp_module_tx_fault_reset(sfp
);
605 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
610 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
611 sfp_sm_fault(sfp
, false);
612 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
613 dev_info(sfp
->dev
, "module transmit fault recovered\n");
614 sfp_sm_link_check_los(sfp
);
618 case SFP_S_TX_DISABLE
:
622 dev_dbg(sfp
->dev
, "SM: exit %u:%u:%u\n",
623 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
);
625 mutex_unlock(&sfp
->sm_mutex
);
628 static void sfp_start(struct sfp
*sfp
)
630 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
633 static void sfp_stop(struct sfp
*sfp
)
635 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
638 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
640 /* locking... and check module is present */
642 if (sfp
->id
.ext
.sff8472_compliance
) {
643 modinfo
->type
= ETH_MODULE_SFF_8472
;
644 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
646 modinfo
->type
= ETH_MODULE_SFF_8079
;
647 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
652 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
655 unsigned int first
, last
, len
;
662 last
= ee
->offset
+ ee
->len
;
663 if (first
< ETH_MODULE_SFF_8079_LEN
) {
664 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
667 ret
= sfp
->read(sfp
, false, first
, data
, len
);
674 if (first
>= ETH_MODULE_SFF_8079_LEN
&&
675 first
< ETH_MODULE_SFF_8472_LEN
) {
676 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
678 first
-= ETH_MODULE_SFF_8079_LEN
;
680 ret
= sfp
->read(sfp
, true, first
, data
, len
);
687 static const struct sfp_socket_ops sfp_module_ops
= {
690 .module_info
= sfp_module_info
,
691 .module_eeprom
= sfp_module_eeprom
,
694 static void sfp_timeout(struct work_struct
*work
)
696 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
699 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
703 static void sfp_check_state(struct sfp
*sfp
)
705 unsigned int state
, i
, changed
;
707 state
= sfp_get_state(sfp
);
708 changed
= state
^ sfp
->state
;
709 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
711 for (i
= 0; i
< GPIO_MAX
; i
++)
712 if (changed
& BIT(i
))
713 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
714 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
716 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
720 if (changed
& SFP_F_PRESENT
)
721 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
722 SFP_E_INSERT
: SFP_E_REMOVE
);
724 if (changed
& SFP_F_TX_FAULT
)
725 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
726 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
728 if (changed
& SFP_F_LOS
)
729 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
730 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
734 static irqreturn_t
sfp_irq(int irq
, void *data
)
736 struct sfp
*sfp
= data
;
738 sfp_check_state(sfp
);
743 static void sfp_poll(struct work_struct
*work
)
745 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
747 sfp_check_state(sfp
);
748 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
751 static struct sfp
*sfp_alloc(struct device
*dev
)
755 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
757 return ERR_PTR(-ENOMEM
);
761 mutex_init(&sfp
->sm_mutex
);
762 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
763 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
768 static void sfp_cleanup(void *data
)
770 struct sfp
*sfp
= data
;
772 cancel_delayed_work_sync(&sfp
->poll
);
773 cancel_delayed_work_sync(&sfp
->timeout
);
775 mdiobus_unregister(sfp
->i2c_mii
);
776 mdiobus_free(sfp
->i2c_mii
);
779 i2c_put_adapter(sfp
->i2c
);
783 static int sfp_probe(struct platform_device
*pdev
)
789 sfp
= sfp_alloc(&pdev
->dev
);
793 platform_set_drvdata(pdev
, sfp
);
795 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
799 if (pdev
->dev
.of_node
) {
800 struct device_node
*node
= pdev
->dev
.of_node
;
801 struct device_node
*np
;
803 np
= of_parse_phandle(node
, "i2c-bus", 0);
805 struct i2c_adapter
*i2c
;
807 i2c
= of_find_i2c_adapter_by_node(np
);
810 return -EPROBE_DEFER
;
812 err
= sfp_i2c_configure(sfp
, i2c
);
814 i2c_put_adapter(i2c
);
819 for (i
= 0; i
< GPIO_MAX
; i
++) {
820 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
821 gpio_of_names
[i
], gpio_flags
[i
]);
822 if (IS_ERR(sfp
->gpio
[i
]))
823 return PTR_ERR(sfp
->gpio
[i
]);
826 sfp
->get_state
= sfp_gpio_get_state
;
827 sfp
->set_state
= sfp_gpio_set_state
;
830 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
834 /* Get the initial state, and always signal TX disable,
835 * since the network interface will not be up.
837 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
839 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
840 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
841 sfp
->state
|= SFP_F_RATE_SELECT
;
842 sfp_set_state(sfp
, sfp
->state
);
843 sfp_module_tx_disable(sfp
);
845 if (sfp
->state
& SFP_F_PRESENT
)
846 sfp_sm_event(sfp
, SFP_E_INSERT
);
849 for (i
= 0; i
< GPIO_MAX
; i
++) {
850 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
853 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
859 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
861 IRQF_TRIGGER_RISING
|
862 IRQF_TRIGGER_FALLING
,
863 dev_name(sfp
->dev
), sfp
);
869 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
874 static int sfp_remove(struct platform_device
*pdev
)
876 struct sfp
*sfp
= platform_get_drvdata(pdev
);
878 sfp_unregister_socket(sfp
->sfp_bus
);
883 static const struct of_device_id sfp_of_match
[] = {
884 { .compatible
= "sff,sfp", },
887 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
889 static struct platform_driver sfp_driver
= {
891 .remove
= sfp_remove
,
894 .of_match_table
= sfp_of_match
,
898 static int sfp_init(void)
900 poll_jiffies
= msecs_to_jiffies(100);
902 return platform_driver_register(&sfp_driver
);
904 module_init(sfp_init
);
906 static void sfp_exit(void)
908 platform_driver_unregister(&sfp_driver
);
910 module_exit(sfp_exit
);
912 MODULE_ALIAS("platform:sfp");
913 MODULE_AUTHOR("Russell King");
914 MODULE_LICENSE("GPL v2");