1 #include <linux/ctype.h>
2 #include <linux/delay.h>
3 #include <linux/gpio/consumer.h>
4 #include <linux/hwmon.h>
6 #include <linux/interrupt.h>
7 #include <linux/jiffies.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
11 #include <linux/phy.h>
12 #include <linux/platform_device.h>
13 #include <linux/rtnetlink.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
29 SFP_F_PRESENT = BIT(GPIO_MODDEF0),
30 SFP_F_LOS = BIT(GPIO_LOS),
31 SFP_F_TX_FAULT = BIT(GPIO_TX_FAULT),
32 SFP_F_TX_DISABLE = BIT(GPIO_TX_DISABLE),
33 SFP_F_RATE_SELECT = BIT(GPIO_RATE_SELECT),
63 static const char * const mod_state_strings[] = {
64 [SFP_MOD_EMPTY] = "empty",
65 [SFP_MOD_PROBE] = "probe",
66 [SFP_MOD_HPOWER] = "hpower",
67 [SFP_MOD_PRESENT] = "present",
68 [SFP_MOD_ERROR] = "error",
71 static const char *mod_state_to_str(unsigned short mod_state)
73 if (mod_state >= ARRAY_SIZE(mod_state_strings))
74 return "Unknown module state";
75 return mod_state_strings[mod_state];
78 static const char * const dev_state_strings[] = {
79 [SFP_DEV_DOWN] = "down",
83 static const char *dev_state_to_str(unsigned short dev_state)
85 if (dev_state >= ARRAY_SIZE(dev_state_strings))
86 return "Unknown device state";
87 return dev_state_strings[dev_state];
90 static const char * const event_strings[] = {
91 [SFP_E_INSERT] = "insert",
92 [SFP_E_REMOVE] = "remove",
93 [SFP_E_DEV_DOWN] = "dev_down",
94 [SFP_E_DEV_UP] = "dev_up",
95 [SFP_E_TX_FAULT] = "tx_fault",
96 [SFP_E_TX_CLEAR] = "tx_clear",
97 [SFP_E_LOS_HIGH] = "los_high",
98 [SFP_E_LOS_LOW] = "los_low",
99 [SFP_E_TIMEOUT] = "timeout",
102 static const char *event_to_str(unsigned short event)
104 if (event >= ARRAY_SIZE(event_strings))
105 return "Unknown event";
106 return event_strings[event];
109 static const char * const sm_state_strings[] = {
110 [SFP_S_DOWN] = "down",
111 [SFP_S_INIT] = "init",
112 [SFP_S_WAIT_LOS] = "wait_los",
113 [SFP_S_LINK_UP] = "link_up",
114 [SFP_S_TX_FAULT] = "tx_fault",
115 [SFP_S_REINIT] = "reinit",
116 [SFP_S_TX_DISABLE] = "tx_disable",
119 static const char *sm_state_to_str(unsigned short sm_state)
121 if (sm_state >= ARRAY_SIZE(sm_state_strings))
122 return "Unknown state";
123 return sm_state_strings[sm_state];
126 static const char *gpio_of_names[] = {
134 static const enum gpiod_flags gpio_flags[] = {
142 #define T_INIT_JIFFIES msecs_to_jiffies(300)
143 #define T_RESET_US 10
144 #define T_FAULT_RECOVER msecs_to_jiffies(1000)
146 /* SFP module presence detection is poor: the three MOD DEF signals are
147 * the same length on the PCB, which means it's possible for MOD DEF 0 to
148 * connect before the I2C bus on MOD DEF 1/2.
150 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
151 * be deasserted) but makes no mention of the earliest time before we can
152 * access the I2C EEPROM. However, Avago modules require 300ms.
154 #define T_PROBE_INIT msecs_to_jiffies(300)
155 #define T_HPOWER_LEVEL msecs_to_jiffies(300)
156 #define T_PROBE_RETRY msecs_to_jiffies(100)
158 /* SFP modules appear to always have their PHY configured for bus address
159 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
161 #define SFP_PHY_ADDR 22
163 /* Give this long for the PHY to reset. */
164 #define T_PHY_RESET_MS 50
168 bool (*module_supported)(const struct sfp_eeprom_id *id);
173 struct i2c_adapter *i2c;
174 struct mii_bus *i2c_mii;
175 struct sfp_bus *sfp_bus;
176 struct phy_device *mod_phy;
177 const struct sff_data *type;
180 unsigned int (*get_state)(struct sfp *);
181 void (*set_state)(struct sfp *, unsigned int);
182 int (*read)(struct sfp *, bool, u8, void *, size_t);
183 int (*write)(struct sfp *, bool, u8, void *, size_t);
185 struct gpio_desc *gpio[GPIO_MAX];
188 struct mutex st_mutex; /* Protects state */
190 struct delayed_work poll;
191 struct delayed_work timeout;
192 struct mutex sm_mutex; /* Protects state machine */
193 unsigned char sm_mod_state;
194 unsigned char sm_dev_state;
195 unsigned short sm_state;
196 unsigned int sm_retries;
198 struct sfp_eeprom_id id;
199 #if IS_ENABLED(CONFIG_HWMON)
200 struct sfp_diag diag;
201 struct device *hwmon_dev;
207 static bool sff_module_supported(const struct sfp_eeprom_id *id)
209 return id->base.phys_id == SFP_PHYS_ID_SFF &&
210 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
213 static const struct sff_data sff_data = {
214 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
215 .module_supported = sff_module_supported,
218 static bool sfp_module_supported(const struct sfp_eeprom_id *id)
220 return id->base.phys_id == SFP_PHYS_ID_SFP &&
221 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
224 static const struct sff_data sfp_data = {
225 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
226 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
227 .module_supported = sfp_module_supported,
230 static const struct of_device_id sfp_of_match[] = {
231 { .compatible = "sff,sff", .data = &sff_data, },
232 { .compatible = "sff,sfp", .data = &sfp_data, },
235 MODULE_DEVICE_TABLE(of, sfp_of_match);
237 static unsigned long poll_jiffies;
239 static unsigned int sfp_gpio_get_state(struct sfp *sfp)
241 unsigned int i, state, v;
243 for (i = state = 0; i < GPIO_MAX; i++) {
244 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
247 v = gpiod_get_value_cansleep(sfp->gpio[i]);
255 static unsigned int sff_gpio_get_state(struct sfp *sfp)
257 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
260 static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
262 if (state & SFP_F_PRESENT) {
263 /* If the module is present, drive the signals */
264 if (sfp->gpio[GPIO_TX_DISABLE])
265 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
266 state & SFP_F_TX_DISABLE);
267 if (state & SFP_F_RATE_SELECT)
268 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
269 state & SFP_F_RATE_SELECT);
271 /* Otherwise, let them float to the pull-ups */
272 if (sfp->gpio[GPIO_TX_DISABLE])
273 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
274 if (state & SFP_F_RATE_SELECT)
275 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
279 static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
282 struct i2c_msg msgs[2];
283 u8 bus_addr = a2 ? 0x51 : 0x50;
287 msgs[0].addr = bus_addr;
290 msgs[0].buf = &dev_addr;
291 msgs[1].addr = bus_addr;
292 msgs[1].flags = I2C_M_RD;
301 msgs[1].len = this_len;
303 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
307 if (ret != ARRAY_SIZE(msgs))
310 msgs[1].buf += this_len;
311 dev_addr += this_len;
315 return msgs[1].buf - (u8 *)buf;
318 static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
321 struct i2c_msg msgs[1];
322 u8 bus_addr = a2 ? 0x51 : 0x50;
325 msgs[0].addr = bus_addr;
327 msgs[0].len = 1 + len;
328 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
332 msgs[0].buf[0] = dev_addr;
333 memcpy(&msgs[0].buf[1], buf, len);
335 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
342 return ret == ARRAY_SIZE(msgs) ? len : 0;
345 static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
347 struct mii_bus *i2c_mii;
350 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
354 sfp->read = sfp_i2c_read;
355 sfp->write = sfp_i2c_write;
357 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
359 return PTR_ERR(i2c_mii);
361 i2c_mii->name = "SFP I2C Bus";
362 i2c_mii->phy_mask = ~0;
364 ret = mdiobus_register(i2c_mii);
366 mdiobus_free(i2c_mii);
370 sfp->i2c_mii = i2c_mii;
376 static unsigned int sfp_get_state(struct sfp *sfp)
378 return sfp->get_state(sfp);
381 static void sfp_set_state(struct sfp *sfp, unsigned int state)
383 sfp->set_state(sfp, state);
386 static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
388 return sfp->read(sfp, a2, addr, buf, len);
391 static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
393 return sfp->write(sfp, a2, addr, buf, len);
396 static unsigned int sfp_check(void *buf, size_t len)
400 for (p = buf, check = 0; len; p++, len--)
407 #if IS_ENABLED(CONFIG_HWMON)
408 static umode_t sfp_hwmon_is_visible(const void *data,
409 enum hwmon_sensor_types type,
410 u32 attr, int channel)
412 const struct sfp *sfp = data;
417 case hwmon_temp_min_alarm:
418 case hwmon_temp_max_alarm:
419 case hwmon_temp_lcrit_alarm:
420 case hwmon_temp_crit_alarm:
423 case hwmon_temp_lcrit:
424 case hwmon_temp_crit:
425 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
428 case hwmon_temp_input:
435 case hwmon_in_min_alarm:
436 case hwmon_in_max_alarm:
437 case hwmon_in_lcrit_alarm:
438 case hwmon_in_crit_alarm:
443 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
453 case hwmon_curr_min_alarm:
454 case hwmon_curr_max_alarm:
455 case hwmon_curr_lcrit_alarm:
456 case hwmon_curr_crit_alarm:
459 case hwmon_curr_lcrit:
460 case hwmon_curr_crit:
461 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
464 case hwmon_curr_input:
470 /* External calibration of receive power requires
471 * floating point arithmetic. Doing that in the kernel
472 * is not easy, so just skip it. If the module does
473 * not require external calibration, we can however
474 * show receiver power, since FP is then not needed.
476 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
480 case hwmon_power_min_alarm:
481 case hwmon_power_max_alarm:
482 case hwmon_power_lcrit_alarm:
483 case hwmon_power_crit_alarm:
484 case hwmon_power_min:
485 case hwmon_power_max:
486 case hwmon_power_lcrit:
487 case hwmon_power_crit:
488 if (!(sfp->id.ext.enhopts & SFP_ENHOPTS_ALARMWARN))
491 case hwmon_power_input:
501 static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
506 err = sfp_read(sfp, true, reg, &val, sizeof(val));
510 *value = be16_to_cpu(val);
515 static void sfp_hwmon_to_rx_power(long *value)
517 *value = DIV_ROUND_CLOSEST(*value, 10);
520 static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
523 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
524 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
527 static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
529 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
530 be16_to_cpu(sfp->diag.cal_t_offset), value);
532 if (*value >= 0x8000)
535 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
538 static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
540 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
541 be16_to_cpu(sfp->diag.cal_v_offset), value);
543 *value = DIV_ROUND_CLOSEST(*value, 10);
546 static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
548 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
549 be16_to_cpu(sfp->diag.cal_txi_offset), value);
551 *value = DIV_ROUND_CLOSEST(*value, 500);
554 static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
556 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
557 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
559 *value = DIV_ROUND_CLOSEST(*value, 10);
562 static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
566 err = sfp_hwmon_read_sensor(sfp, reg, value);
570 sfp_hwmon_calibrate_temp(sfp, value);
575 static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
579 err = sfp_hwmon_read_sensor(sfp, reg, value);
583 sfp_hwmon_calibrate_vcc(sfp, value);
588 static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
592 err = sfp_hwmon_read_sensor(sfp, reg, value);
596 sfp_hwmon_calibrate_bias(sfp, value);
601 static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
605 err = sfp_hwmon_read_sensor(sfp, reg, value);
609 sfp_hwmon_calibrate_tx_power(sfp, value);
614 static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
618 err = sfp_hwmon_read_sensor(sfp, reg, value);
622 sfp_hwmon_to_rx_power(value);
627 static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
633 case hwmon_temp_input:
634 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
636 case hwmon_temp_lcrit:
637 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
638 sfp_hwmon_calibrate_temp(sfp, value);
642 *value = be16_to_cpu(sfp->diag.temp_low_warn);
643 sfp_hwmon_calibrate_temp(sfp, value);
646 *value = be16_to_cpu(sfp->diag.temp_high_warn);
647 sfp_hwmon_calibrate_temp(sfp, value);
650 case hwmon_temp_crit:
651 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
652 sfp_hwmon_calibrate_temp(sfp, value);
655 case hwmon_temp_lcrit_alarm:
656 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
660 *value = !!(status & SFP_ALARM0_TEMP_LOW);
663 case hwmon_temp_min_alarm:
664 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
668 *value = !!(status & SFP_WARN0_TEMP_LOW);
671 case hwmon_temp_max_alarm:
672 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
676 *value = !!(status & SFP_WARN0_TEMP_HIGH);
679 case hwmon_temp_crit_alarm:
680 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
684 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
693 static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
700 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
703 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
704 sfp_hwmon_calibrate_vcc(sfp, value);
708 *value = be16_to_cpu(sfp->diag.volt_low_warn);
709 sfp_hwmon_calibrate_vcc(sfp, value);
713 *value = be16_to_cpu(sfp->diag.volt_high_warn);
714 sfp_hwmon_calibrate_vcc(sfp, value);
718 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
719 sfp_hwmon_calibrate_vcc(sfp, value);
722 case hwmon_in_lcrit_alarm:
723 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
727 *value = !!(status & SFP_ALARM0_VCC_LOW);
730 case hwmon_in_min_alarm:
731 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
735 *value = !!(status & SFP_WARN0_VCC_LOW);
738 case hwmon_in_max_alarm:
739 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
743 *value = !!(status & SFP_WARN0_VCC_HIGH);
746 case hwmon_in_crit_alarm:
747 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
751 *value = !!(status & SFP_ALARM0_VCC_HIGH);
760 static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
766 case hwmon_curr_input:
767 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
769 case hwmon_curr_lcrit:
770 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
771 sfp_hwmon_calibrate_bias(sfp, value);
775 *value = be16_to_cpu(sfp->diag.bias_low_warn);
776 sfp_hwmon_calibrate_bias(sfp, value);
780 *value = be16_to_cpu(sfp->diag.bias_high_warn);
781 sfp_hwmon_calibrate_bias(sfp, value);
784 case hwmon_curr_crit:
785 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
786 sfp_hwmon_calibrate_bias(sfp, value);
789 case hwmon_curr_lcrit_alarm:
790 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
794 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
797 case hwmon_curr_min_alarm:
798 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
802 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
805 case hwmon_curr_max_alarm:
806 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
810 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
813 case hwmon_curr_crit_alarm:
814 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
818 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
827 static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
833 case hwmon_power_input:
834 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
836 case hwmon_power_lcrit:
837 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
838 sfp_hwmon_calibrate_tx_power(sfp, value);
841 case hwmon_power_min:
842 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
843 sfp_hwmon_calibrate_tx_power(sfp, value);
846 case hwmon_power_max:
847 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
848 sfp_hwmon_calibrate_tx_power(sfp, value);
851 case hwmon_power_crit:
852 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
853 sfp_hwmon_calibrate_tx_power(sfp, value);
856 case hwmon_power_lcrit_alarm:
857 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
861 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
864 case hwmon_power_min_alarm:
865 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
869 *value = !!(status & SFP_WARN0_TXPWR_LOW);
872 case hwmon_power_max_alarm:
873 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
877 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
880 case hwmon_power_crit_alarm:
881 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
885 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
894 static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
900 case hwmon_power_input:
901 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
903 case hwmon_power_lcrit:
904 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
905 sfp_hwmon_to_rx_power(value);
908 case hwmon_power_min:
909 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
910 sfp_hwmon_to_rx_power(value);
913 case hwmon_power_max:
914 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
915 sfp_hwmon_to_rx_power(value);
918 case hwmon_power_crit:
919 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
920 sfp_hwmon_to_rx_power(value);
923 case hwmon_power_lcrit_alarm:
924 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
928 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
931 case hwmon_power_min_alarm:
932 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
936 *value = !!(status & SFP_WARN1_RXPWR_LOW);
939 case hwmon_power_max_alarm:
940 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
944 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
947 case hwmon_power_crit_alarm:
948 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
952 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
961 static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
962 u32 attr, int channel, long *value)
964 struct sfp *sfp = dev_get_drvdata(dev);
968 return sfp_hwmon_temp(sfp, attr, value);
970 return sfp_hwmon_vcc(sfp, attr, value);
972 return sfp_hwmon_bias(sfp, attr, value);
976 return sfp_hwmon_tx_power(sfp, attr, value);
978 return sfp_hwmon_rx_power(sfp, attr, value);
987 static const struct hwmon_ops sfp_hwmon_ops = {
988 .is_visible = sfp_hwmon_is_visible,
989 .read = sfp_hwmon_read,
992 static u32 sfp_hwmon_chip_config[] = {
997 static const struct hwmon_channel_info sfp_hwmon_chip = {
999 .config = sfp_hwmon_chip_config,
1002 static u32 sfp_hwmon_temp_config[] = {
1004 HWMON_T_MAX | HWMON_T_MIN |
1005 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
1006 HWMON_T_CRIT | HWMON_T_LCRIT |
1007 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM,
1011 static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
1013 .config = sfp_hwmon_temp_config,
1016 static u32 sfp_hwmon_vcc_config[] = {
1018 HWMON_I_MAX | HWMON_I_MIN |
1019 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
1020 HWMON_I_CRIT | HWMON_I_LCRIT |
1021 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM,
1025 static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
1027 .config = sfp_hwmon_vcc_config,
1030 static u32 sfp_hwmon_bias_config[] = {
1032 HWMON_C_MAX | HWMON_C_MIN |
1033 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
1034 HWMON_C_CRIT | HWMON_C_LCRIT |
1035 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM,
1039 static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
1041 .config = sfp_hwmon_bias_config,
1044 static u32 sfp_hwmon_power_config[] = {
1045 /* Transmit power */
1047 HWMON_P_MAX | HWMON_P_MIN |
1048 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1049 HWMON_P_CRIT | HWMON_P_LCRIT |
1050 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
1053 HWMON_P_MAX | HWMON_P_MIN |
1054 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
1055 HWMON_P_CRIT | HWMON_P_LCRIT |
1056 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
1060 static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
1061 .type = hwmon_power,
1062 .config = sfp_hwmon_power_config,
1065 static const struct hwmon_channel_info *sfp_hwmon_info[] = {
1067 &sfp_hwmon_vcc_channel_info,
1068 &sfp_hwmon_temp_channel_info,
1069 &sfp_hwmon_bias_channel_info,
1070 &sfp_hwmon_power_channel_info,
1074 static const struct hwmon_chip_info sfp_hwmon_chip_info = {
1075 .ops = &sfp_hwmon_ops,
1076 .info = sfp_hwmon_info,
1079 static int sfp_hwmon_insert(struct sfp *sfp)
1083 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
1086 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
1089 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1090 /* This driver in general does not support address
1095 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1099 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1100 if (!sfp->hwmon_name)
1103 for (i = 0; sfp->hwmon_name[i]; i++)
1104 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1105 sfp->hwmon_name[i] = '_';
1107 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1108 sfp->hwmon_name, sfp,
1109 &sfp_hwmon_chip_info,
1112 return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
1115 static void sfp_hwmon_remove(struct sfp *sfp)
1117 if (!IS_ERR_OR_NULL(sfp->hwmon_dev)) {
1118 hwmon_device_unregister(sfp->hwmon_dev);
1119 sfp->hwmon_dev = NULL;
1120 kfree(sfp->hwmon_name);
1124 static int sfp_hwmon_insert(struct sfp *sfp)
1129 static void sfp_hwmon_remove(struct sfp *sfp)
1135 static void sfp_module_tx_disable(struct sfp *sfp)
1137 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1138 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1139 sfp->state |= SFP_F_TX_DISABLE;
1140 sfp_set_state(sfp, sfp->state);
1143 static void sfp_module_tx_enable(struct sfp *sfp)
1145 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1146 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1147 sfp->state &= ~SFP_F_TX_DISABLE;
1148 sfp_set_state(sfp, sfp->state);
1151 static void sfp_module_tx_fault_reset(struct sfp *sfp)
1153 unsigned int state = sfp->state;
1155 if (state & SFP_F_TX_DISABLE)
1158 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1162 sfp_set_state(sfp, state);
1165 /* SFP state machine */
1166 static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1169 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1172 cancel_delayed_work(&sfp->timeout);
1175 static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1176 unsigned int timeout)
1178 sfp->sm_state = state;
1179 sfp_sm_set_timer(sfp, timeout);
1182 static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
1183 unsigned int timeout)
1185 sfp->sm_mod_state = state;
1186 sfp_sm_set_timer(sfp, timeout);
1189 static void sfp_sm_phy_detach(struct sfp *sfp)
1191 phy_stop(sfp->mod_phy);
1192 sfp_remove_phy(sfp->sfp_bus);
1193 phy_device_remove(sfp->mod_phy);
1194 phy_device_free(sfp->mod_phy);
1195 sfp->mod_phy = NULL;
1198 static void sfp_sm_probe_phy(struct sfp *sfp)
1200 struct phy_device *phy;
1203 msleep(T_PHY_RESET_MS);
1205 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
1206 if (phy == ERR_PTR(-ENODEV)) {
1207 dev_info(sfp->dev, "no PHY detected\n");
1211 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
1215 err = sfp_add_phy(sfp->sfp_bus, phy);
1217 phy_device_remove(phy);
1218 phy_device_free(phy);
1219 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1227 static void sfp_sm_link_up(struct sfp *sfp)
1229 sfp_link_up(sfp->sfp_bus);
1230 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1233 static void sfp_sm_link_down(struct sfp *sfp)
1235 sfp_link_down(sfp->sfp_bus);
1238 static void sfp_sm_link_check_los(struct sfp *sfp)
1240 unsigned int los = sfp->state & SFP_F_LOS;
1242 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1243 * are set, we assume that no LOS signal is available.
1245 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
1247 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1251 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1253 sfp_sm_link_up(sfp);
1256 static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1258 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1259 event == SFP_E_LOS_LOW) ||
1260 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1261 event == SFP_E_LOS_HIGH);
1264 static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1266 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1267 event == SFP_E_LOS_HIGH) ||
1268 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1269 event == SFP_E_LOS_LOW);
1272 static void sfp_sm_fault(struct sfp *sfp, bool warn)
1274 if (sfp->sm_retries && !--sfp->sm_retries) {
1276 "module persistently indicates fault, disabling\n");
1277 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1280 dev_err(sfp->dev, "module transmit fault indicated\n");
1282 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
1286 static void sfp_sm_mod_init(struct sfp *sfp)
1288 sfp_module_tx_enable(sfp);
1290 /* Wait t_init before indicating that the link is up, provided the
1291 * current state indicates no TX_FAULT. If TX_FAULT clears before
1292 * this time, that's fine too.
1294 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
1295 sfp->sm_retries = 5;
1297 /* Setting the serdes link mode is guesswork: there's no
1298 * field in the EEPROM which indicates what mode should
1301 * If it's a gigabit-only fiber module, it probably does
1302 * not have a PHY, so switch to 802.3z negotiation mode.
1303 * Otherwise, switch to SGMII mode (which is required to
1304 * support non-gigabit speeds) and probe for a PHY.
1306 if (sfp->id.base.e1000_base_t ||
1307 sfp->id.base.e100_base_lx ||
1308 sfp->id.base.e100_base_fx)
1309 sfp_sm_probe_phy(sfp);
1312 static int sfp_sm_mod_hpower(struct sfp *sfp)
1319 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1321 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1324 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
1325 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
1327 /* The module appears not to implement bus address 0xa2,
1328 * or requires an address change sequence, so assume that
1329 * the module powers up in the indicated power mode.
1331 if (power > sfp->max_power_mW) {
1333 "Host does not support %u.%uW modules\n",
1334 power / 1000, (power / 100) % 10);
1340 if (power > sfp->max_power_mW) {
1342 "Host does not support %u.%uW modules, module left in power mode 1\n",
1343 power / 1000, (power / 100) % 10);
1350 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1351 if (err != sizeof(val)) {
1352 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1359 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1360 if (err != sizeof(val)) {
1361 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1366 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1367 power / 1000, (power / 100) % 10);
1368 return T_HPOWER_LEVEL;
1374 static int sfp_sm_mod_probe(struct sfp *sfp)
1376 /* SFP module inserted - read I2C data */
1377 struct sfp_eeprom_id id;
1382 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1384 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
1388 if (ret != sizeof(id)) {
1389 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
1393 /* Cotsworks do not seem to update the checksums when they
1394 * do the final programming with the final module part number,
1395 * serial number and date code.
1397 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1399 /* Validate the checksum over the base structure */
1400 check = sfp_check(&id.base, sizeof(id.base) - 1);
1401 if (check != id.base.cc_base) {
1404 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1405 check, id.base.cc_base);
1408 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1409 check, id.base.cc_base);
1410 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1411 16, 1, &id, sizeof(id), true);
1416 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1417 if (check != id.ext.cc_ext) {
1420 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1421 check, id.ext.cc_ext);
1424 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1425 check, id.ext.cc_ext);
1426 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1427 16, 1, &id, sizeof(id), true);
1428 memset(&id.ext, 0, sizeof(id.ext));
1434 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1435 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1436 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1437 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1438 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1439 (int)sizeof(id.ext.datecode), id.ext.datecode);
1441 /* Check whether we support this module */
1442 if (!sfp->type->module_supported(&sfp->id)) {
1444 "module is not supported - phys id 0x%02x 0x%02x\n",
1445 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1449 /* If the module requires address swap mode, warn about it */
1450 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1452 "module address swap to access page 0xA2 is not supported.\n");
1454 ret = sfp_hwmon_insert(sfp);
1458 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1462 return sfp_sm_mod_hpower(sfp);
1465 static void sfp_sm_mod_remove(struct sfp *sfp)
1467 sfp_module_remove(sfp->sfp_bus);
1469 sfp_hwmon_remove(sfp);
1472 sfp_sm_phy_detach(sfp);
1474 sfp_module_tx_disable(sfp);
1476 memset(&sfp->id, 0, sizeof(sfp->id));
1478 dev_info(sfp->dev, "module removed\n");
1481 static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1483 mutex_lock(&sfp->sm_mutex);
1485 dev_dbg(sfp->dev, "SM: enter %s:%s:%s event %s\n",
1486 mod_state_to_str(sfp->sm_mod_state),
1487 dev_state_to_str(sfp->sm_dev_state),
1488 sm_state_to_str(sfp->sm_state),
1489 event_to_str(event));
1491 /* This state machine tracks the insert/remove state of
1492 * the module, and handles probing the on-board EEPROM.
1494 switch (sfp->sm_mod_state) {
1496 if (event == SFP_E_INSERT && sfp->attached) {
1497 sfp_module_tx_disable(sfp);
1498 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
1503 if (event == SFP_E_REMOVE) {
1504 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1505 } else if (event == SFP_E_TIMEOUT) {
1506 int val = sfp_sm_mod_probe(sfp);
1509 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1511 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
1512 else if (val != -EAGAIN)
1513 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
1515 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
1519 case SFP_MOD_HPOWER:
1520 if (event == SFP_E_TIMEOUT) {
1521 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1525 case SFP_MOD_PRESENT:
1527 if (event == SFP_E_REMOVE) {
1528 sfp_sm_mod_remove(sfp);
1529 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1534 /* This state machine tracks the netdev up/down state */
1535 switch (sfp->sm_dev_state) {
1537 if (event == SFP_E_DEV_UP)
1538 sfp->sm_dev_state = SFP_DEV_UP;
1542 if (event == SFP_E_DEV_DOWN) {
1543 /* If the module has a PHY, avoid raising TX disable
1544 * as this resets the PHY. Otherwise, raise it to
1545 * turn the laser off.
1548 sfp_module_tx_disable(sfp);
1549 sfp->sm_dev_state = SFP_DEV_DOWN;
1554 /* Some events are global */
1555 if (sfp->sm_state != SFP_S_DOWN &&
1556 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1557 sfp->sm_dev_state != SFP_DEV_UP)) {
1558 if (sfp->sm_state == SFP_S_LINK_UP &&
1559 sfp->sm_dev_state == SFP_DEV_UP)
1560 sfp_sm_link_down(sfp);
1562 sfp_sm_phy_detach(sfp);
1563 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1564 mutex_unlock(&sfp->sm_mutex);
1568 /* The main state machine */
1569 switch (sfp->sm_state) {
1571 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
1572 sfp->sm_dev_state == SFP_DEV_UP)
1573 sfp_sm_mod_init(sfp);
1577 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
1578 sfp_sm_fault(sfp, true);
1579 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
1580 sfp_sm_link_check_los(sfp);
1583 case SFP_S_WAIT_LOS:
1584 if (event == SFP_E_TX_FAULT)
1585 sfp_sm_fault(sfp, true);
1586 else if (sfp_los_event_inactive(sfp, event))
1587 sfp_sm_link_up(sfp);
1591 if (event == SFP_E_TX_FAULT) {
1592 sfp_sm_link_down(sfp);
1593 sfp_sm_fault(sfp, true);
1594 } else if (sfp_los_event_active(sfp, event)) {
1595 sfp_sm_link_down(sfp);
1596 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1600 case SFP_S_TX_FAULT:
1601 if (event == SFP_E_TIMEOUT) {
1602 sfp_module_tx_fault_reset(sfp);
1603 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1608 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1609 sfp_sm_fault(sfp, false);
1610 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1611 dev_info(sfp->dev, "module transmit fault recovered\n");
1612 sfp_sm_link_check_los(sfp);
1616 case SFP_S_TX_DISABLE:
1620 dev_dbg(sfp->dev, "SM: exit %s:%s:%s\n",
1621 mod_state_to_str(sfp->sm_mod_state),
1622 dev_state_to_str(sfp->sm_dev_state),
1623 sm_state_to_str(sfp->sm_state));
1625 mutex_unlock(&sfp->sm_mutex);
1628 static void sfp_attach(struct sfp *sfp)
1630 sfp->attached = true;
1631 if (sfp->state & SFP_F_PRESENT)
1632 sfp_sm_event(sfp, SFP_E_INSERT);
1635 static void sfp_detach(struct sfp *sfp)
1637 sfp->attached = false;
1638 sfp_sm_event(sfp, SFP_E_REMOVE);
1641 static void sfp_start(struct sfp *sfp)
1643 sfp_sm_event(sfp, SFP_E_DEV_UP);
1646 static void sfp_stop(struct sfp *sfp)
1648 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1651 static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1653 /* locking... and check module is present */
1655 if (sfp->id.ext.sff8472_compliance &&
1656 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
1657 modinfo->type = ETH_MODULE_SFF_8472;
1658 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1660 modinfo->type = ETH_MODULE_SFF_8079;
1661 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1666 static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
1669 unsigned int first, last, len;
1676 last = ee->offset + ee->len;
1677 if (first < ETH_MODULE_SFF_8079_LEN) {
1678 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
1681 ret = sfp_read(sfp, false, first, data, len);
1688 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
1689 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
1691 first -= ETH_MODULE_SFF_8079_LEN;
1693 ret = sfp_read(sfp, true, first, data, len);
1700 static const struct sfp_socket_ops sfp_module_ops = {
1701 .attach = sfp_attach,
1702 .detach = sfp_detach,
1705 .module_info = sfp_module_info,
1706 .module_eeprom = sfp_module_eeprom,
1709 static void sfp_timeout(struct work_struct *work)
1711 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
1714 sfp_sm_event(sfp, SFP_E_TIMEOUT);
1718 static void sfp_check_state(struct sfp *sfp)
1720 unsigned int state, i, changed;
1722 mutex_lock(&sfp->st_mutex);
1723 state = sfp_get_state(sfp);
1724 changed = state ^ sfp->state;
1725 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
1727 for (i = 0; i < GPIO_MAX; i++)
1728 if (changed & BIT(i))
1729 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
1730 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
1732 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
1736 if (changed & SFP_F_PRESENT)
1737 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
1738 SFP_E_INSERT : SFP_E_REMOVE);
1740 if (changed & SFP_F_TX_FAULT)
1741 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
1742 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
1744 if (changed & SFP_F_LOS)
1745 sfp_sm_event(sfp, state & SFP_F_LOS ?
1746 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
1748 mutex_unlock(&sfp->st_mutex);
1751 static irqreturn_t sfp_irq(int irq, void *data)
1753 struct sfp *sfp = data;
1755 sfp_check_state(sfp);
1760 static void sfp_poll(struct work_struct *work)
1762 struct sfp *sfp = container_of(work, struct sfp, poll.work);
1764 sfp_check_state(sfp);
1765 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1768 static struct sfp *sfp_alloc(struct device *dev)
1772 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1774 return ERR_PTR(-ENOMEM);
1778 mutex_init(&sfp->sm_mutex);
1779 mutex_init(&sfp->st_mutex);
1780 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1781 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1786 static void sfp_cleanup(void *data)
1788 struct sfp *sfp = data;
1790 cancel_delayed_work_sync(&sfp->poll);
1791 cancel_delayed_work_sync(&sfp->timeout);
1793 mdiobus_unregister(sfp->i2c_mii);
1794 mdiobus_free(sfp->i2c_mii);
1797 i2c_put_adapter(sfp->i2c);
1801 static int sfp_probe(struct platform_device *pdev)
1803 const struct sff_data *sff;
1808 sfp = sfp_alloc(&pdev->dev);
1810 return PTR_ERR(sfp);
1812 platform_set_drvdata(pdev, sfp);
1814 err = devm_add_action_or_reset(sfp->dev, sfp_cleanup, sfp);
1818 sff = sfp->type = &sfp_data;
1820 if (pdev->dev.of_node) {
1821 struct device_node *node = pdev->dev.of_node;
1822 const struct of_device_id *id;
1823 struct i2c_adapter *i2c;
1824 struct device_node *np;
1826 id = of_match_node(sfp_of_match, node);
1830 sff = sfp->type = id->data;
1832 np = of_parse_phandle(node, "i2c-bus", 0);
1834 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1838 i2c = of_find_i2c_adapter_by_node(np);
1841 return -EPROBE_DEFER;
1843 err = sfp_i2c_configure(sfp, i2c);
1845 i2c_put_adapter(i2c);
1850 for (i = 0; i < GPIO_MAX; i++)
1851 if (sff->gpios & BIT(i)) {
1852 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1853 gpio_of_names[i], gpio_flags[i]);
1854 if (IS_ERR(sfp->gpio[i]))
1855 return PTR_ERR(sfp->gpio[i]);
1858 sfp->get_state = sfp_gpio_get_state;
1859 sfp->set_state = sfp_gpio_set_state;
1861 /* Modules that have no detect signal are always present */
1862 if (!(sfp->gpio[GPIO_MODDEF0]))
1863 sfp->get_state = sff_gpio_get_state;
1865 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
1866 &sfp->max_power_mW);
1867 if (!sfp->max_power_mW)
1868 sfp->max_power_mW = 1000;
1870 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1871 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1873 /* Get the initial state, and always signal TX disable,
1874 * since the network interface will not be up.
1876 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1878 if (sfp->gpio[GPIO_RATE_SELECT] &&
1879 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1880 sfp->state |= SFP_F_RATE_SELECT;
1881 sfp_set_state(sfp, sfp->state);
1882 sfp_module_tx_disable(sfp);
1884 for (i = 0; i < GPIO_MAX; i++) {
1885 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1888 irq = gpiod_to_irq(sfp->gpio[i]);
1895 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1897 IRQF_TRIGGER_RISING |
1898 IRQF_TRIGGER_FALLING,
1899 dev_name(sfp->dev), sfp);
1905 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1907 /* We could have an issue in cases no Tx disable pin is available or
1908 * wired as modules using a laser as their light source will continue to
1909 * be active when the fiber is removed. This could be a safety issue and
1910 * we should at least warn the user about that.
1912 if (!sfp->gpio[GPIO_TX_DISABLE])
1914 "No tx_disable pin: SFP modules will always be emitting.\n");
1916 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1923 static int sfp_remove(struct platform_device *pdev)
1925 struct sfp *sfp = platform_get_drvdata(pdev);
1927 sfp_unregister_socket(sfp->sfp_bus);
1932 static struct platform_driver sfp_driver = {
1934 .remove = sfp_remove,
1937 .of_match_table = sfp_of_match,
1941 static int sfp_init(void)
1943 poll_jiffies = msecs_to_jiffies(100);
1945 return platform_driver_register(&sfp_driver);
1947 module_init(sfp_init);
1949 static void sfp_exit(void)
1951 platform_driver_unregister(&sfp_driver);
1953 module_exit(sfp_exit);
1955 MODULE_ALIAS("platform:sfp");
1956 MODULE_AUTHOR("Russell King");
1957 MODULE_LICENSE("GPL v2");