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;
567 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
569 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
573 if (ret != sizeof(id)) {
574 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
578 /* Cotsworks do not seem to update the checksums when they
579 * do the final programming with the final module part number,
580 * serial number and date code.
582 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
584 /* Validate the checksum over the base structure */
585 check = sfp_check(&id.base, sizeof(id.base) - 1);
586 if (check != id.base.cc_base) {
589 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
590 check, id.base.cc_base);
593 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
594 check, id.base.cc_base);
595 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
596 16, 1, &id, sizeof(id), true);
601 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
602 if (check != id.ext.cc_ext) {
605 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
606 check, id.ext.cc_ext);
609 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
610 check, id.ext.cc_ext);
611 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
612 16, 1, &id, sizeof(id), true);
613 memset(&id.ext, 0, sizeof(id.ext));
619 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
620 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
621 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
622 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
623 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
624 (int)sizeof(id.ext.datecode), id.ext.datecode);
626 /* Check whether we support this module */
627 if (!sfp->type->module_supported(&sfp->id)) {
629 "module is not supported - phys id 0x%02x 0x%02x\n",
630 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
634 /* If the module requires address swap mode, warn about it */
635 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
637 "module address swap to access page 0xA2 is not supported.\n");
639 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
643 return sfp_sm_mod_hpower(sfp);
646 static void sfp_sm_mod_remove(struct sfp *sfp)
648 sfp_module_remove(sfp->sfp_bus);
651 sfp_sm_phy_detach(sfp);
653 sfp_module_tx_disable(sfp);
655 memset(&sfp->id, 0, sizeof(sfp->id));
657 dev_info(sfp->dev, "module removed\n");
660 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
662 mutex_lock(&sfp->sm_mutex);
664 dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
665 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
667 /* This state machine tracks the insert/remove state of
668 * the module, and handles probing the on-board EEPROM.
670 switch (sfp->sm_mod_state) {
672 if (event == SFP_E_INSERT) {
673 sfp_module_tx_disable(sfp);
674 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
679 if (event == SFP_E_REMOVE) {
680 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
681 } else if (event == SFP_E_TIMEOUT) {
682 int val = sfp_sm_mod_probe(sfp);
685 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
687 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
688 else if (val != -EAGAIN)
689 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
691 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
696 if (event == SFP_E_TIMEOUT) {
697 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
701 case SFP_MOD_PRESENT:
703 if (event == SFP_E_REMOVE) {
704 sfp_sm_mod_remove(sfp);
705 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
710 /* This state machine tracks the netdev up/down state */
711 switch (sfp->sm_dev_state) {
713 if (event == SFP_E_DEV_UP)
714 sfp->sm_dev_state = SFP_DEV_UP;
718 if (event == SFP_E_DEV_DOWN) {
719 /* If the module has a PHY, avoid raising TX disable
720 * as this resets the PHY. Otherwise, raise it to
721 * turn the laser off.
724 sfp_module_tx_disable(sfp);
725 sfp->sm_dev_state = SFP_DEV_DOWN;
730 /* Some events are global */
731 if (sfp->sm_state != SFP_S_DOWN &&
732 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
733 sfp->sm_dev_state != SFP_DEV_UP)) {
734 if (sfp->sm_state == SFP_S_LINK_UP &&
735 sfp->sm_dev_state == SFP_DEV_UP)
736 sfp_sm_link_down(sfp);
738 sfp_sm_phy_detach(sfp);
739 sfp_sm_next(sfp, SFP_S_DOWN, 0);
740 mutex_unlock(&sfp->sm_mutex);
744 /* The main state machine */
745 switch (sfp->sm_state) {
747 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
748 sfp->sm_dev_state == SFP_DEV_UP)
749 sfp_sm_mod_init(sfp);
753 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
754 sfp_sm_fault(sfp, true);
755 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
756 sfp_sm_link_check_los(sfp);
760 if (event == SFP_E_TX_FAULT)
761 sfp_sm_fault(sfp, true);
762 else if (sfp_los_event_inactive(sfp, event))
767 if (event == SFP_E_TX_FAULT) {
768 sfp_sm_link_down(sfp);
769 sfp_sm_fault(sfp, true);
770 } else if (sfp_los_event_active(sfp, event)) {
771 sfp_sm_link_down(sfp);
772 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
777 if (event == SFP_E_TIMEOUT) {
778 sfp_module_tx_fault_reset(sfp);
779 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
784 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
785 sfp_sm_fault(sfp, false);
786 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
787 dev_info(sfp->dev, "module transmit fault recovered\n");
788 sfp_sm_link_check_los(sfp);
792 case SFP_S_TX_DISABLE:
796 dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
797 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
799 mutex_unlock(&sfp->sm_mutex);
802 static void sfp_start(struct sfp *sfp)
804 sfp_sm_event(sfp, SFP_E_DEV_UP);
807 static void sfp_stop(struct sfp *sfp)
809 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
812 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
814 /* locking... and check module is present */
816 if (sfp->id.ext.sff8472_compliance &&
817 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
818 modinfo->type = ETH_MODULE_SFF_8472;
819 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
821 modinfo->type = ETH_MODULE_SFF_8079;
822 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
827 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
830 unsigned int first, last, len;
837 last = ee->offset + ee->len;
838 if (first < ETH_MODULE_SFF_8079_LEN) {
839 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
842 ret = sfp_read(sfp, false, first, data, len);
849 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
850 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
852 first -= ETH_MODULE_SFF_8079_LEN;
854 ret = sfp_read(sfp, true, first, data, len);
861 static const struct sfp_socket_ops sfp_module_ops = {
864 .module_info = sfp_module_info,
865 .module_eeprom = sfp_module_eeprom,
868 static void sfp_timeout(struct work_struct *work)
870 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
873 sfp_sm_event(sfp, SFP_E_TIMEOUT);
877 static void sfp_check_state(struct sfp *sfp)
879 unsigned int state, i, changed;
881 state = sfp_get_state(sfp);
882 changed = state ^ sfp->state;
883 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
885 for (i = 0; i < GPIO_MAX; i++)
886 if (changed & BIT(i))
887 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
888 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
890 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
894 if (changed & SFP_F_PRESENT)
895 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
896 SFP_E_INSERT : SFP_E_REMOVE);
898 if (changed & SFP_F_TX_FAULT)
899 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
900 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
902 if (changed & SFP_F_LOS)
903 sfp_sm_event(sfp, state & SFP_F_LOS ?
904 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
908 static irqreturn_t sfp_irq(int irq, void *data)
910 struct sfp *sfp = data;
912 sfp_check_state(sfp);
917 static void sfp_poll(struct work_struct *work)
919 struct sfp *sfp = container_of(work, struct sfp, poll.work);
921 sfp_check_state(sfp);
922 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
925 static struct sfp *sfp_alloc(struct device *dev)
929 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
931 return ERR_PTR(-ENOMEM);
935 mutex_init(&sfp->sm_mutex);
936 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
937 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
942 static void sfp_cleanup(void *data)
944 struct sfp *sfp = data;
946 cancel_delayed_work_sync(&sfp->poll);
947 cancel_delayed_work_sync(&sfp->timeout);
949 mdiobus_unregister(sfp->i2c_mii);
950 mdiobus_free(sfp->i2c_mii);
953 i2c_put_adapter(sfp->i2c);
957 static int sfp_probe(struct platform_device *pdev)
959 const struct sff_data *sff;
964 sfp = sfp_alloc(&pdev->dev);
968 platform_set_drvdata(pdev, sfp);
970 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
974 sff = sfp->type = &sfp_data;
976 if (pdev->dev.of_node) {
977 struct device_node *node = pdev->dev.of_node;
978 const struct of_device_id *id;
979 struct device_node *np;
981 id = of_match_node(sfp_of_match, node);
985 sff = sfp->type = id->data;
987 np = of_parse_phandle(node, "i2c-bus", 0);
989 struct i2c_adapter *i2c;
991 i2c = of_find_i2c_adapter_by_node(np);
994 return -EPROBE_DEFER;
996 err = sfp_i2c_configure(sfp, i2c);
998 i2c_put_adapter(i2c);
1004 for (i = 0; i < GPIO_MAX; i++)
1005 if (sff->gpios & BIT(i)) {
1006 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1007 gpio_of_names[i], gpio_flags[i]);
1008 if (IS_ERR(sfp->gpio[i]))
1009 return PTR_ERR(sfp->gpio[i]);
1012 sfp->get_state = sfp_gpio_get_state;
1013 sfp->set_state = sfp_gpio_set_state;
1015 /* Modules that have no detect signal are always present */
1016 if (!(sfp->gpio[GPIO_MODDEF0]))
1017 sfp->get_state = sff_gpio_get_state;
1019 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
1020 &sfp->max_power_mW);
1021 if (!sfp->max_power_mW)
1022 sfp->max_power_mW = 1000;
1024 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1025 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1027 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1031 /* Get the initial state, and always signal TX disable,
1032 * since the network interface will not be up.
1034 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1036 if (sfp->gpio[GPIO_RATE_SELECT] &&
1037 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1038 sfp->state |= SFP_F_RATE_SELECT;
1039 sfp_set_state(sfp, sfp->state);
1040 sfp_module_tx_disable(sfp);
1042 if (sfp->state & SFP_F_PRESENT)
1043 sfp_sm_event(sfp, SFP_E_INSERT);
1046 for (i = 0; i < GPIO_MAX; i++) {
1047 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1050 irq = gpiod_to_irq(sfp->gpio[i]);
1056 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1058 IRQF_TRIGGER_RISING |
1059 IRQF_TRIGGER_FALLING,
1060 dev_name(sfp->dev), sfp);
1066 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1068 /* We could have an issue in cases no Tx disable pin is available or
1069 * wired as modules using a laser as their light source will continue to
1070 * be active when the fiber is removed. This could be a safety issue and
1071 * we should at least warn the user about that.
1073 if (!sfp->gpio[GPIO_TX_DISABLE])
1075 "No tx_disable pin: SFP modules will always be emitting.\n");
1080 static int sfp_remove(struct platform_device *pdev)
1082 struct sfp *sfp = platform_get_drvdata(pdev);
1084 sfp_unregister_socket(sfp->sfp_bus);
1089 static struct platform_driver sfp_driver = {
1091 .remove = sfp_remove,
1094 .of_match_table = sfp_of_match,
1098 static int sfp_init(void)
1100 poll_jiffies = msecs_to_jiffies(100);
1102 return platform_driver_register(&sfp_driver);
1104 module_init(sfp_init);
1106 static void sfp_exit(void)
1108 platform_driver_unregister(&sfp_driver);
1110 module_exit(sfp_exit);
1112 MODULE_ALIAS("platform:sfp");
1113 MODULE_AUTHOR("Russell King");
1114 MODULE_LICENSE("GPL v2");