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 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
355 * are set, we assume that no LOS signal is available.
357 if (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
))
359 else if (!(sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
)))
363 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
368 static bool sfp_los_event_active(struct sfp
*sfp
, unsigned int event
)
370 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
371 event
== SFP_E_LOS_LOW
) ||
372 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
373 event
== SFP_E_LOS_HIGH
);
376 static bool sfp_los_event_inactive(struct sfp
*sfp
, unsigned int event
)
378 return (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_INVERTED
) &&
379 event
== SFP_E_LOS_HIGH
) ||
380 (sfp
->id
.ext
.options
& cpu_to_be16(SFP_OPTIONS_LOS_NORMAL
) &&
381 event
== SFP_E_LOS_LOW
);
384 static void sfp_sm_fault(struct sfp
*sfp
, bool warn
)
386 if (sfp
->sm_retries
&& !--sfp
->sm_retries
) {
388 "module persistently indicates fault, disabling\n");
389 sfp_sm_next(sfp
, SFP_S_TX_DISABLE
, 0);
392 dev_err(sfp
->dev
, "module transmit fault indicated\n");
394 sfp_sm_next(sfp
, SFP_S_TX_FAULT
, T_FAULT_RECOVER
);
398 static void sfp_sm_mod_init(struct sfp
*sfp
)
400 sfp_module_tx_enable(sfp
);
402 /* Wait t_init before indicating that the link is up, provided the
403 * current state indicates no TX_FAULT. If TX_FAULT clears before
404 * this time, that's fine too.
406 sfp_sm_next(sfp
, SFP_S_INIT
, T_INIT_JIFFIES
);
409 /* Setting the serdes link mode is guesswork: there's no
410 * field in the EEPROM which indicates what mode should
413 * If it's a gigabit-only fiber module, it probably does
414 * not have a PHY, so switch to 802.3z negotiation mode.
415 * Otherwise, switch to SGMII mode (which is required to
416 * support non-gigabit speeds) and probe for a PHY.
418 if (sfp
->id
.base
.e1000_base_t
||
419 sfp
->id
.base
.e100_base_lx
||
420 sfp
->id
.base
.e100_base_fx
)
421 sfp_sm_probe_phy(sfp
);
424 static int sfp_sm_mod_probe(struct sfp
*sfp
)
426 /* SFP module inserted - read I2C data */
427 struct sfp_eeprom_id id
;
436 err
= sfp_read(sfp
, false, 0, &id
, sizeof(id
));
438 dev_err(sfp
->dev
, "failed to read EEPROM: %d\n", err
);
442 if (err
!= sizeof(id
)) {
443 dev_err(sfp
->dev
, "EEPROM short read: %d\n", err
);
447 /* Validate the checksum over the base structure */
448 check
= sfp_check(&id
.base
, sizeof(id
.base
) - 1);
449 if (check
!= id
.base
.cc_base
) {
451 "EEPROM base structure checksum failure: 0x%02x\n",
453 print_hex_dump(KERN_ERR
, "sfp EE: ", DUMP_PREFIX_OFFSET
,
454 16, 1, &id
, sizeof(id
.base
) - 1, true);
458 check
= sfp_check(&id
.ext
, sizeof(id
.ext
) - 1);
459 if (check
!= id
.ext
.cc_ext
) {
461 "EEPROM extended structure checksum failure: 0x%02x\n",
463 memset(&id
.ext
, 0, sizeof(id
.ext
));
468 memcpy(vendor
, sfp
->id
.base
.vendor_name
, 16);
470 memcpy(part
, sfp
->id
.base
.vendor_pn
, 16);
472 memcpy(rev
, sfp
->id
.base
.vendor_rev
, 4);
474 memcpy(sn
, sfp
->id
.ext
.vendor_sn
, 16);
476 memcpy(date
, sfp
->id
.ext
.datecode
, 8);
479 dev_info(sfp
->dev
, "module %s %s rev %s sn %s dc %s\n",
480 vendor
, part
, rev
, sn
, date
);
482 /* We only support SFP modules, not the legacy GBIC modules. */
483 if (sfp
->id
.base
.phys_id
!= SFP_PHYS_ID_SFP
||
484 sfp
->id
.base
.phys_ext_id
!= SFP_PHYS_EXT_ID_SFP
) {
485 dev_err(sfp
->dev
, "module is not SFP - phys id 0x%02x 0x%02x\n",
486 sfp
->id
.base
.phys_id
, sfp
->id
.base
.phys_ext_id
);
490 /* If the module requires address swap mode, warn about it */
491 if (sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)
493 "module address swap to access page 0xA2 is not supported.\n");
495 return sfp_module_insert(sfp
->sfp_bus
, &sfp
->id
);
498 static void sfp_sm_mod_remove(struct sfp
*sfp
)
500 sfp_module_remove(sfp
->sfp_bus
);
503 sfp_sm_phy_detach(sfp
);
505 sfp_module_tx_disable(sfp
);
507 memset(&sfp
->id
, 0, sizeof(sfp
->id
));
509 dev_info(sfp
->dev
, "module removed\n");
512 static void sfp_sm_event(struct sfp
*sfp
, unsigned int event
)
514 mutex_lock(&sfp
->sm_mutex
);
516 dev_dbg(sfp
->dev
, "SM: enter %u:%u:%u event %u\n",
517 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
, event
);
519 /* This state machine tracks the insert/remove state of
520 * the module, and handles probing the on-board EEPROM.
522 switch (sfp
->sm_mod_state
) {
524 if (event
== SFP_E_INSERT
) {
525 sfp_module_tx_disable(sfp
);
526 sfp_sm_ins_next(sfp
, SFP_MOD_PROBE
, T_PROBE_INIT
);
531 if (event
== SFP_E_REMOVE
) {
532 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
533 } else if (event
== SFP_E_TIMEOUT
) {
534 int err
= sfp_sm_mod_probe(sfp
);
537 sfp_sm_ins_next(sfp
, SFP_MOD_PRESENT
, 0);
538 else if (err
== -EAGAIN
)
539 sfp_sm_set_timer(sfp
, T_PROBE_RETRY
);
541 sfp_sm_ins_next(sfp
, SFP_MOD_ERROR
, 0);
545 case SFP_MOD_PRESENT
:
547 if (event
== SFP_E_REMOVE
) {
548 sfp_sm_mod_remove(sfp
);
549 sfp_sm_ins_next(sfp
, SFP_MOD_EMPTY
, 0);
554 /* This state machine tracks the netdev up/down state */
555 switch (sfp
->sm_dev_state
) {
557 if (event
== SFP_E_DEV_UP
)
558 sfp
->sm_dev_state
= SFP_DEV_UP
;
562 if (event
== SFP_E_DEV_DOWN
) {
563 /* If the module has a PHY, avoid raising TX disable
564 * as this resets the PHY. Otherwise, raise it to
565 * turn the laser off.
568 sfp_module_tx_disable(sfp
);
569 sfp
->sm_dev_state
= SFP_DEV_DOWN
;
574 /* Some events are global */
575 if (sfp
->sm_state
!= SFP_S_DOWN
&&
576 (sfp
->sm_mod_state
!= SFP_MOD_PRESENT
||
577 sfp
->sm_dev_state
!= SFP_DEV_UP
)) {
578 if (sfp
->sm_state
== SFP_S_LINK_UP
&&
579 sfp
->sm_dev_state
== SFP_DEV_UP
)
580 sfp_sm_link_down(sfp
);
582 sfp_sm_phy_detach(sfp
);
583 sfp_sm_next(sfp
, SFP_S_DOWN
, 0);
584 mutex_unlock(&sfp
->sm_mutex
);
588 /* The main state machine */
589 switch (sfp
->sm_state
) {
591 if (sfp
->sm_mod_state
== SFP_MOD_PRESENT
&&
592 sfp
->sm_dev_state
== SFP_DEV_UP
)
593 sfp_sm_mod_init(sfp
);
597 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
)
598 sfp_sm_fault(sfp
, true);
599 else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
)
600 sfp_sm_link_check_los(sfp
);
604 if (event
== SFP_E_TX_FAULT
)
605 sfp_sm_fault(sfp
, true);
606 else if (sfp_los_event_inactive(sfp
, event
))
611 if (event
== SFP_E_TX_FAULT
) {
612 sfp_sm_link_down(sfp
);
613 sfp_sm_fault(sfp
, true);
614 } else if (sfp_los_event_active(sfp
, event
)) {
615 sfp_sm_link_down(sfp
);
616 sfp_sm_next(sfp
, SFP_S_WAIT_LOS
, 0);
621 if (event
== SFP_E_TIMEOUT
) {
622 sfp_module_tx_fault_reset(sfp
);
623 sfp_sm_next(sfp
, SFP_S_REINIT
, T_INIT_JIFFIES
);
628 if (event
== SFP_E_TIMEOUT
&& sfp
->state
& SFP_F_TX_FAULT
) {
629 sfp_sm_fault(sfp
, false);
630 } else if (event
== SFP_E_TIMEOUT
|| event
== SFP_E_TX_CLEAR
) {
631 dev_info(sfp
->dev
, "module transmit fault recovered\n");
632 sfp_sm_link_check_los(sfp
);
636 case SFP_S_TX_DISABLE
:
640 dev_dbg(sfp
->dev
, "SM: exit %u:%u:%u\n",
641 sfp
->sm_mod_state
, sfp
->sm_dev_state
, sfp
->sm_state
);
643 mutex_unlock(&sfp
->sm_mutex
);
646 static void sfp_start(struct sfp
*sfp
)
648 sfp_sm_event(sfp
, SFP_E_DEV_UP
);
651 static void sfp_stop(struct sfp
*sfp
)
653 sfp_sm_event(sfp
, SFP_E_DEV_DOWN
);
656 static int sfp_module_info(struct sfp
*sfp
, struct ethtool_modinfo
*modinfo
)
658 /* locking... and check module is present */
660 if (sfp
->id
.ext
.sff8472_compliance
&&
661 !(sfp
->id
.ext
.diagmon
& SFP_DIAGMON_ADDRMODE
)) {
662 modinfo
->type
= ETH_MODULE_SFF_8472
;
663 modinfo
->eeprom_len
= ETH_MODULE_SFF_8472_LEN
;
665 modinfo
->type
= ETH_MODULE_SFF_8079
;
666 modinfo
->eeprom_len
= ETH_MODULE_SFF_8079_LEN
;
671 static int sfp_module_eeprom(struct sfp
*sfp
, struct ethtool_eeprom
*ee
,
674 unsigned int first
, last
, len
;
681 last
= ee
->offset
+ ee
->len
;
682 if (first
< ETH_MODULE_SFF_8079_LEN
) {
683 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8079_LEN
);
686 ret
= sfp
->read(sfp
, false, first
, data
, len
);
693 if (first
>= ETH_MODULE_SFF_8079_LEN
&&
694 first
< ETH_MODULE_SFF_8472_LEN
) {
695 len
= min_t(unsigned int, last
, ETH_MODULE_SFF_8472_LEN
);
697 first
-= ETH_MODULE_SFF_8079_LEN
;
699 ret
= sfp
->read(sfp
, true, first
, data
, len
);
706 static const struct sfp_socket_ops sfp_module_ops
= {
709 .module_info
= sfp_module_info
,
710 .module_eeprom
= sfp_module_eeprom
,
713 static void sfp_timeout(struct work_struct
*work
)
715 struct sfp
*sfp
= container_of(work
, struct sfp
, timeout
.work
);
718 sfp_sm_event(sfp
, SFP_E_TIMEOUT
);
722 static void sfp_check_state(struct sfp
*sfp
)
724 unsigned int state
, i
, changed
;
726 state
= sfp_get_state(sfp
);
727 changed
= state
^ sfp
->state
;
728 changed
&= SFP_F_PRESENT
| SFP_F_LOS
| SFP_F_TX_FAULT
;
730 for (i
= 0; i
< GPIO_MAX
; i
++)
731 if (changed
& BIT(i
))
732 dev_dbg(sfp
->dev
, "%s %u -> %u\n", gpio_of_names
[i
],
733 !!(sfp
->state
& BIT(i
)), !!(state
& BIT(i
)));
735 state
|= sfp
->state
& (SFP_F_TX_DISABLE
| SFP_F_RATE_SELECT
);
739 if (changed
& SFP_F_PRESENT
)
740 sfp_sm_event(sfp
, state
& SFP_F_PRESENT
?
741 SFP_E_INSERT
: SFP_E_REMOVE
);
743 if (changed
& SFP_F_TX_FAULT
)
744 sfp_sm_event(sfp
, state
& SFP_F_TX_FAULT
?
745 SFP_E_TX_FAULT
: SFP_E_TX_CLEAR
);
747 if (changed
& SFP_F_LOS
)
748 sfp_sm_event(sfp
, state
& SFP_F_LOS
?
749 SFP_E_LOS_HIGH
: SFP_E_LOS_LOW
);
753 static irqreturn_t
sfp_irq(int irq
, void *data
)
755 struct sfp
*sfp
= data
;
757 sfp_check_state(sfp
);
762 static void sfp_poll(struct work_struct
*work
)
764 struct sfp
*sfp
= container_of(work
, struct sfp
, poll
.work
);
766 sfp_check_state(sfp
);
767 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
770 static struct sfp
*sfp_alloc(struct device
*dev
)
774 sfp
= kzalloc(sizeof(*sfp
), GFP_KERNEL
);
776 return ERR_PTR(-ENOMEM
);
780 mutex_init(&sfp
->sm_mutex
);
781 INIT_DELAYED_WORK(&sfp
->poll
, sfp_poll
);
782 INIT_DELAYED_WORK(&sfp
->timeout
, sfp_timeout
);
787 static void sfp_cleanup(void *data
)
789 struct sfp
*sfp
= data
;
791 cancel_delayed_work_sync(&sfp
->poll
);
792 cancel_delayed_work_sync(&sfp
->timeout
);
794 mdiobus_unregister(sfp
->i2c_mii
);
795 mdiobus_free(sfp
->i2c_mii
);
798 i2c_put_adapter(sfp
->i2c
);
802 static int sfp_probe(struct platform_device
*pdev
)
808 sfp
= sfp_alloc(&pdev
->dev
);
812 platform_set_drvdata(pdev
, sfp
);
814 err
= devm_add_action(sfp
->dev
, sfp_cleanup
, sfp
);
818 if (pdev
->dev
.of_node
) {
819 struct device_node
*node
= pdev
->dev
.of_node
;
820 struct device_node
*np
;
822 np
= of_parse_phandle(node
, "i2c-bus", 0);
824 struct i2c_adapter
*i2c
;
826 i2c
= of_find_i2c_adapter_by_node(np
);
829 return -EPROBE_DEFER
;
831 err
= sfp_i2c_configure(sfp
, i2c
);
833 i2c_put_adapter(i2c
);
838 for (i
= 0; i
< GPIO_MAX
; i
++) {
839 sfp
->gpio
[i
] = devm_gpiod_get_optional(sfp
->dev
,
840 gpio_of_names
[i
], gpio_flags
[i
]);
841 if (IS_ERR(sfp
->gpio
[i
]))
842 return PTR_ERR(sfp
->gpio
[i
]);
845 sfp
->get_state
= sfp_gpio_get_state
;
846 sfp
->set_state
= sfp_gpio_set_state
;
849 sfp
->sfp_bus
= sfp_register_socket(sfp
->dev
, sfp
, &sfp_module_ops
);
853 /* Get the initial state, and always signal TX disable,
854 * since the network interface will not be up.
856 sfp
->state
= sfp_get_state(sfp
) | SFP_F_TX_DISABLE
;
858 if (sfp
->gpio
[GPIO_RATE_SELECT
] &&
859 gpiod_get_value_cansleep(sfp
->gpio
[GPIO_RATE_SELECT
]))
860 sfp
->state
|= SFP_F_RATE_SELECT
;
861 sfp_set_state(sfp
, sfp
->state
);
862 sfp_module_tx_disable(sfp
);
864 if (sfp
->state
& SFP_F_PRESENT
)
865 sfp_sm_event(sfp
, SFP_E_INSERT
);
868 for (i
= 0; i
< GPIO_MAX
; i
++) {
869 if (gpio_flags
[i
] != GPIOD_IN
|| !sfp
->gpio
[i
])
872 irq
= gpiod_to_irq(sfp
->gpio
[i
]);
878 err
= devm_request_threaded_irq(sfp
->dev
, irq
, NULL
, sfp_irq
,
880 IRQF_TRIGGER_RISING
|
881 IRQF_TRIGGER_FALLING
,
882 dev_name(sfp
->dev
), sfp
);
888 mod_delayed_work(system_wq
, &sfp
->poll
, poll_jiffies
);
893 static int sfp_remove(struct platform_device
*pdev
)
895 struct sfp
*sfp
= platform_get_drvdata(pdev
);
897 sfp_unregister_socket(sfp
->sfp_bus
);
902 static const struct of_device_id sfp_of_match
[] = {
903 { .compatible
= "sff,sfp", },
906 MODULE_DEVICE_TABLE(of
, sfp_of_match
);
908 static struct platform_driver sfp_driver
= {
910 .remove
= sfp_remove
,
913 .of_match_table
= sfp_of_match
,
917 static int sfp_init(void)
919 poll_jiffies
= msecs_to_jiffies(100);
921 return platform_driver_register(&sfp_driver
);
923 module_init(sfp_init
);
925 static void sfp_exit(void)
927 platform_driver_unregister(&sfp_driver
);
929 module_exit(sfp_exit
);
931 MODULE_ALIAS("platform:sfp");
932 MODULE_AUTHOR("Russell King");
933 MODULE_LICENSE("GPL v2");