blob: 5661226cf75b129b15877747ced78be732240779 [file] [log] [blame]
Andrew Lunn13230612018-07-17 21:48:13 +02001#include <linux/ctype.h>
Russell King73970052017-07-25 15:03:39 +01002#include <linux/delay.h>
Florian Fainelli54a2fc62017-10-30 21:42:58 -07003#include <linux/gpio/consumer.h>
Andrew Lunn13230612018-07-17 21:48:13 +02004#include <linux/hwmon.h>
Russell King73970052017-07-25 15:03:39 +01005#include <linux/i2c.h>
6#include <linux/interrupt.h>
7#include <linux/jiffies.h>
8#include <linux/module.h>
9#include <linux/mutex.h>
10#include <linux/of.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>
16
17#include "mdio-i2c.h"
18#include "sfp.h"
19#include "swphy.h"
20
21enum {
22 GPIO_MODDEF0,
23 GPIO_LOS,
24 GPIO_TX_FAULT,
25 GPIO_TX_DISABLE,
26 GPIO_RATE_SELECT,
27 GPIO_MAX,
28
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),
34
35 SFP_E_INSERT = 0,
36 SFP_E_REMOVE,
37 SFP_E_DEV_DOWN,
38 SFP_E_DEV_UP,
39 SFP_E_TX_FAULT,
40 SFP_E_TX_CLEAR,
41 SFP_E_LOS_HIGH,
42 SFP_E_LOS_LOW,
43 SFP_E_TIMEOUT,
44
45 SFP_MOD_EMPTY = 0,
46 SFP_MOD_PROBE,
Jon Nettleton3bb35262018-02-27 15:53:12 +000047 SFP_MOD_HPOWER,
Russell King73970052017-07-25 15:03:39 +010048 SFP_MOD_PRESENT,
49 SFP_MOD_ERROR,
50
51 SFP_DEV_DOWN = 0,
52 SFP_DEV_UP,
53
54 SFP_S_DOWN = 0,
55 SFP_S_INIT,
56 SFP_S_WAIT_LOS,
57 SFP_S_LINK_UP,
58 SFP_S_TX_FAULT,
59 SFP_S_REINIT,
60 SFP_S_TX_DISABLE,
61};
62
63static const char *gpio_of_names[] = {
Baruch Siach25ee0792017-09-07 12:25:50 +030064 "mod-def0",
Russell King73970052017-07-25 15:03:39 +010065 "los",
66 "tx-fault",
67 "tx-disable",
Baruch Siach25ee0792017-09-07 12:25:50 +030068 "rate-select0",
Russell King73970052017-07-25 15:03:39 +010069};
70
71static const enum gpiod_flags gpio_flags[] = {
72 GPIOD_IN,
73 GPIOD_IN,
74 GPIOD_IN,
75 GPIOD_ASIS,
76 GPIOD_ASIS,
77};
78
79#define T_INIT_JIFFIES msecs_to_jiffies(300)
80#define T_RESET_US 10
81#define T_FAULT_RECOVER msecs_to_jiffies(1000)
82
83/* SFP module presence detection is poor: the three MOD DEF signals are
84 * the same length on the PCB, which means it's possible for MOD DEF 0 to
85 * connect before the I2C bus on MOD DEF 1/2.
86 *
87 * The SFP MSA specifies 300ms as t_init (the time taken for TX_FAULT to
88 * be deasserted) but makes no mention of the earliest time before we can
89 * access the I2C EEPROM. However, Avago modules require 300ms.
90 */
91#define T_PROBE_INIT msecs_to_jiffies(300)
Jon Nettleton3bb35262018-02-27 15:53:12 +000092#define T_HPOWER_LEVEL msecs_to_jiffies(300)
Russell King73970052017-07-25 15:03:39 +010093#define T_PROBE_RETRY msecs_to_jiffies(100)
94
Florian Fainelli516b29e2017-10-30 21:42:57 -070095/* SFP modules appear to always have their PHY configured for bus address
Russell King73970052017-07-25 15:03:39 +010096 * 0x56 (which with mdio-i2c, translates to a PHY address of 22).
97 */
98#define SFP_PHY_ADDR 22
99
Florian Fainelli516b29e2017-10-30 21:42:57 -0700100/* Give this long for the PHY to reset. */
Russell King73970052017-07-25 15:03:39 +0100101#define T_PHY_RESET_MS 50
102
103static DEFINE_MUTEX(sfp_mutex);
104
Russell King259c8612017-12-14 10:27:47 +0000105struct sff_data {
106 unsigned int gpios;
107 bool (*module_supported)(const struct sfp_eeprom_id *id);
108};
109
Russell King73970052017-07-25 15:03:39 +0100110struct sfp {
111 struct device *dev;
112 struct i2c_adapter *i2c;
113 struct mii_bus *i2c_mii;
114 struct sfp_bus *sfp_bus;
115 struct phy_device *mod_phy;
Russell King259c8612017-12-14 10:27:47 +0000116 const struct sff_data *type;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000117 u32 max_power_mW;
Russell King73970052017-07-25 15:03:39 +0100118
119 unsigned int (*get_state)(struct sfp *);
120 void (*set_state)(struct sfp *, unsigned int);
121 int (*read)(struct sfp *, bool, u8, void *, size_t);
Jon Nettleton3bb35262018-02-27 15:53:12 +0000122 int (*write)(struct sfp *, bool, u8, void *, size_t);
Russell King73970052017-07-25 15:03:39 +0100123
124 struct gpio_desc *gpio[GPIO_MAX];
125
126 unsigned int state;
127 struct delayed_work poll;
128 struct delayed_work timeout;
129 struct mutex sm_mutex;
130 unsigned char sm_mod_state;
131 unsigned char sm_dev_state;
132 unsigned short sm_state;
133 unsigned int sm_retries;
134
135 struct sfp_eeprom_id id;
Andrew Lunn13230612018-07-17 21:48:13 +0200136#if IS_ENABLED(CONFIG_HWMON)
137 struct sfp_diag diag;
138 struct device *hwmon_dev;
139 char *hwmon_name;
140#endif
141
Russell King73970052017-07-25 15:03:39 +0100142};
143
Russell King259c8612017-12-14 10:27:47 +0000144static bool sff_module_supported(const struct sfp_eeprom_id *id)
145{
146 return id->base.phys_id == SFP_PHYS_ID_SFF &&
147 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
148}
149
150static const struct sff_data sff_data = {
151 .gpios = SFP_F_LOS | SFP_F_TX_FAULT | SFP_F_TX_DISABLE,
152 .module_supported = sff_module_supported,
153};
154
155static bool sfp_module_supported(const struct sfp_eeprom_id *id)
156{
157 return id->base.phys_id == SFP_PHYS_ID_SFP &&
158 id->base.phys_ext_id == SFP_PHYS_EXT_ID_SFP;
159}
160
161static const struct sff_data sfp_data = {
162 .gpios = SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT |
163 SFP_F_TX_DISABLE | SFP_F_RATE_SELECT,
164 .module_supported = sfp_module_supported,
165};
166
167static const struct of_device_id sfp_of_match[] = {
168 { .compatible = "sff,sff", .data = &sff_data, },
169 { .compatible = "sff,sfp", .data = &sfp_data, },
170 { },
171};
172MODULE_DEVICE_TABLE(of, sfp_of_match);
173
Russell King73970052017-07-25 15:03:39 +0100174static unsigned long poll_jiffies;
175
176static unsigned int sfp_gpio_get_state(struct sfp *sfp)
177{
178 unsigned int i, state, v;
179
180 for (i = state = 0; i < GPIO_MAX; i++) {
181 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
182 continue;
183
184 v = gpiod_get_value_cansleep(sfp->gpio[i]);
185 if (v)
186 state |= BIT(i);
187 }
188
189 return state;
190}
191
Russell King259c8612017-12-14 10:27:47 +0000192static unsigned int sff_gpio_get_state(struct sfp *sfp)
193{
194 return sfp_gpio_get_state(sfp) | SFP_F_PRESENT;
195}
196
Russell King73970052017-07-25 15:03:39 +0100197static void sfp_gpio_set_state(struct sfp *sfp, unsigned int state)
198{
199 if (state & SFP_F_PRESENT) {
200 /* If the module is present, drive the signals */
201 if (sfp->gpio[GPIO_TX_DISABLE])
202 gpiod_direction_output(sfp->gpio[GPIO_TX_DISABLE],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700203 state & SFP_F_TX_DISABLE);
Russell King73970052017-07-25 15:03:39 +0100204 if (state & SFP_F_RATE_SELECT)
205 gpiod_direction_output(sfp->gpio[GPIO_RATE_SELECT],
Florian Fainelli516b29e2017-10-30 21:42:57 -0700206 state & SFP_F_RATE_SELECT);
Russell King73970052017-07-25 15:03:39 +0100207 } else {
208 /* Otherwise, let them float to the pull-ups */
209 if (sfp->gpio[GPIO_TX_DISABLE])
210 gpiod_direction_input(sfp->gpio[GPIO_TX_DISABLE]);
211 if (state & SFP_F_RATE_SELECT)
212 gpiod_direction_input(sfp->gpio[GPIO_RATE_SELECT]);
213 }
214}
215
Jon Nettleton3bb35262018-02-27 15:53:12 +0000216static int sfp_i2c_read(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
217 size_t len)
Russell King73970052017-07-25 15:03:39 +0100218{
219 struct i2c_msg msgs[2];
Jon Nettleton3bb35262018-02-27 15:53:12 +0000220 u8 bus_addr = a2 ? 0x51 : 0x50;
Russell King73970052017-07-25 15:03:39 +0100221 int ret;
222
223 msgs[0].addr = bus_addr;
224 msgs[0].flags = 0;
225 msgs[0].len = 1;
226 msgs[0].buf = &dev_addr;
227 msgs[1].addr = bus_addr;
228 msgs[1].flags = I2C_M_RD;
229 msgs[1].len = len;
230 msgs[1].buf = buf;
231
Jon Nettleton3bb35262018-02-27 15:53:12 +0000232 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
Russell King73970052017-07-25 15:03:39 +0100233 if (ret < 0)
234 return ret;
235
236 return ret == ARRAY_SIZE(msgs) ? len : 0;
237}
238
Jon Nettleton3bb35262018-02-27 15:53:12 +0000239static int sfp_i2c_write(struct sfp *sfp, bool a2, u8 dev_addr, void *buf,
240 size_t len)
Russell King73970052017-07-25 15:03:39 +0100241{
Jon Nettleton3bb35262018-02-27 15:53:12 +0000242 struct i2c_msg msgs[1];
243 u8 bus_addr = a2 ? 0x51 : 0x50;
244 int ret;
245
246 msgs[0].addr = bus_addr;
247 msgs[0].flags = 0;
248 msgs[0].len = 1 + len;
249 msgs[0].buf = kmalloc(1 + len, GFP_KERNEL);
250 if (!msgs[0].buf)
251 return -ENOMEM;
252
253 msgs[0].buf[0] = dev_addr;
254 memcpy(&msgs[0].buf[1], buf, len);
255
256 ret = i2c_transfer(sfp->i2c, msgs, ARRAY_SIZE(msgs));
257
258 kfree(msgs[0].buf);
259
260 if (ret < 0)
261 return ret;
262
263 return ret == ARRAY_SIZE(msgs) ? len : 0;
Russell King73970052017-07-25 15:03:39 +0100264}
265
266static int sfp_i2c_configure(struct sfp *sfp, struct i2c_adapter *i2c)
267{
268 struct mii_bus *i2c_mii;
269 int ret;
270
271 if (!i2c_check_functionality(i2c, I2C_FUNC_I2C))
272 return -EINVAL;
273
274 sfp->i2c = i2c;
275 sfp->read = sfp_i2c_read;
Jon Nettleton3bb35262018-02-27 15:53:12 +0000276 sfp->write = sfp_i2c_write;
Russell King73970052017-07-25 15:03:39 +0100277
278 i2c_mii = mdio_i2c_alloc(sfp->dev, i2c);
279 if (IS_ERR(i2c_mii))
280 return PTR_ERR(i2c_mii);
281
282 i2c_mii->name = "SFP I2C Bus";
283 i2c_mii->phy_mask = ~0;
284
285 ret = mdiobus_register(i2c_mii);
286 if (ret < 0) {
287 mdiobus_free(i2c_mii);
288 return ret;
289 }
290
291 sfp->i2c_mii = i2c_mii;
292
293 return 0;
294}
295
Russell King73970052017-07-25 15:03:39 +0100296/* Interface */
297static unsigned int sfp_get_state(struct sfp *sfp)
298{
299 return sfp->get_state(sfp);
300}
301
302static void sfp_set_state(struct sfp *sfp, unsigned int state)
303{
304 sfp->set_state(sfp, state);
305}
306
307static int sfp_read(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
308{
309 return sfp->read(sfp, a2, addr, buf, len);
310}
311
Jon Nettleton3bb35262018-02-27 15:53:12 +0000312static int sfp_write(struct sfp *sfp, bool a2, u8 addr, void *buf, size_t len)
313{
314 return sfp->write(sfp, a2, addr, buf, len);
315}
316
Russell King73970052017-07-25 15:03:39 +0100317static unsigned int sfp_check(void *buf, size_t len)
318{
319 u8 *p, check;
320
321 for (p = buf, check = 0; len; p++, len--)
322 check += *p;
323
324 return check;
325}
326
Andrew Lunn13230612018-07-17 21:48:13 +0200327/* hwmon */
328#if IS_ENABLED(CONFIG_HWMON)
329static umode_t sfp_hwmon_is_visible(const void *data,
330 enum hwmon_sensor_types type,
331 u32 attr, int channel)
332{
333 const struct sfp *sfp = data;
334
335 switch (type) {
336 case hwmon_temp:
337 switch (attr) {
338 case hwmon_temp_input:
339 case hwmon_temp_min_alarm:
340 case hwmon_temp_max_alarm:
341 case hwmon_temp_lcrit_alarm:
342 case hwmon_temp_crit_alarm:
343 case hwmon_temp_min:
344 case hwmon_temp_max:
345 case hwmon_temp_lcrit:
346 case hwmon_temp_crit:
347 return 0444;
348 default:
349 return 0;
350 }
351 case hwmon_in:
352 switch (attr) {
353 case hwmon_in_input:
354 case hwmon_in_min_alarm:
355 case hwmon_in_max_alarm:
356 case hwmon_in_lcrit_alarm:
357 case hwmon_in_crit_alarm:
358 case hwmon_in_min:
359 case hwmon_in_max:
360 case hwmon_in_lcrit:
361 case hwmon_in_crit:
362 return 0444;
363 default:
364 return 0;
365 }
366 case hwmon_curr:
367 switch (attr) {
368 case hwmon_curr_input:
369 case hwmon_curr_min_alarm:
370 case hwmon_curr_max_alarm:
371 case hwmon_curr_lcrit_alarm:
372 case hwmon_curr_crit_alarm:
373 case hwmon_curr_min:
374 case hwmon_curr_max:
375 case hwmon_curr_lcrit:
376 case hwmon_curr_crit:
377 return 0444;
378 default:
379 return 0;
380 }
381 case hwmon_power:
382 /* External calibration of receive power requires
383 * floating point arithmetic. Doing that in the kernel
384 * is not easy, so just skip it. If the module does
385 * not require external calibration, we can however
386 * show receiver power, since FP is then not needed.
387 */
388 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL &&
389 channel == 1)
390 return 0;
391 switch (attr) {
392 case hwmon_power_input:
393 case hwmon_power_min_alarm:
394 case hwmon_power_max_alarm:
395 case hwmon_power_lcrit_alarm:
396 case hwmon_power_crit_alarm:
397 case hwmon_power_min:
398 case hwmon_power_max:
399 case hwmon_power_lcrit:
400 case hwmon_power_crit:
401 return 0444;
402 default:
403 return 0;
404 }
405 default:
406 return 0;
407 }
408}
409
410static int sfp_hwmon_read_sensor(struct sfp *sfp, int reg, long *value)
411{
412 __be16 val;
413 int err;
414
415 err = sfp_read(sfp, true, reg, &val, sizeof(val));
416 if (err < 0)
417 return err;
418
419 *value = be16_to_cpu(val);
420
421 return 0;
422}
423
424static void sfp_hwmon_to_rx_power(long *value)
425{
426 *value = DIV_ROUND_CLOSEST(*value, 100);
427}
428
429static void sfp_hwmon_calibrate(struct sfp *sfp, unsigned int slope, int offset,
430 long *value)
431{
432 if (sfp->id.ext.diagmon & SFP_DIAGMON_EXT_CAL)
433 *value = DIV_ROUND_CLOSEST(*value * slope, 256) + offset;
434}
435
436static void sfp_hwmon_calibrate_temp(struct sfp *sfp, long *value)
437{
438 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_t_slope),
439 be16_to_cpu(sfp->diag.cal_t_offset), value);
440
441 if (*value >= 0x8000)
442 *value -= 0x10000;
443
444 *value = DIV_ROUND_CLOSEST(*value * 1000, 256);
445}
446
447static void sfp_hwmon_calibrate_vcc(struct sfp *sfp, long *value)
448{
449 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_v_slope),
450 be16_to_cpu(sfp->diag.cal_v_offset), value);
451
452 *value = DIV_ROUND_CLOSEST(*value, 10);
453}
454
455static void sfp_hwmon_calibrate_bias(struct sfp *sfp, long *value)
456{
457 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txi_slope),
458 be16_to_cpu(sfp->diag.cal_txi_offset), value);
459
460 *value = DIV_ROUND_CLOSEST(*value, 500);
461}
462
463static void sfp_hwmon_calibrate_tx_power(struct sfp *sfp, long *value)
464{
465 sfp_hwmon_calibrate(sfp, be16_to_cpu(sfp->diag.cal_txpwr_slope),
466 be16_to_cpu(sfp->diag.cal_txpwr_offset), value);
467
468 *value = DIV_ROUND_CLOSEST(*value, 10);
469}
470
471static int sfp_hwmon_read_temp(struct sfp *sfp, int reg, long *value)
472{
473 int err;
474
475 err = sfp_hwmon_read_sensor(sfp, reg, value);
476 if (err < 0)
477 return err;
478
479 sfp_hwmon_calibrate_temp(sfp, value);
480
481 return 0;
482}
483
484static int sfp_hwmon_read_vcc(struct sfp *sfp, int reg, long *value)
485{
486 int err;
487
488 err = sfp_hwmon_read_sensor(sfp, reg, value);
489 if (err < 0)
490 return err;
491
492 sfp_hwmon_calibrate_vcc(sfp, value);
493
494 return 0;
495}
496
497static int sfp_hwmon_read_bias(struct sfp *sfp, int reg, long *value)
498{
499 int err;
500
501 err = sfp_hwmon_read_sensor(sfp, reg, value);
502 if (err < 0)
503 return err;
504
505 sfp_hwmon_calibrate_bias(sfp, value);
506
507 return 0;
508}
509
510static int sfp_hwmon_read_tx_power(struct sfp *sfp, int reg, long *value)
511{
512 int err;
513
514 err = sfp_hwmon_read_sensor(sfp, reg, value);
515 if (err < 0)
516 return err;
517
518 sfp_hwmon_calibrate_tx_power(sfp, value);
519
520 return 0;
521}
522
523static int sfp_hwmon_read_rx_power(struct sfp *sfp, int reg, long *value)
524{
525 int err;
526
527 err = sfp_hwmon_read_sensor(sfp, reg, value);
528 if (err < 0)
529 return err;
530
531 sfp_hwmon_to_rx_power(value);
532
533 return 0;
534}
535
536static int sfp_hwmon_temp(struct sfp *sfp, u32 attr, long *value)
537{
538 u8 status;
539 int err;
540
541 switch (attr) {
542 case hwmon_temp_input:
543 return sfp_hwmon_read_temp(sfp, SFP_TEMP, value);
544
545 case hwmon_temp_lcrit:
546 *value = be16_to_cpu(sfp->diag.temp_low_alarm);
547 sfp_hwmon_calibrate_temp(sfp, value);
548 return 0;
549
550 case hwmon_temp_min:
551 *value = be16_to_cpu(sfp->diag.temp_low_warn);
552 sfp_hwmon_calibrate_temp(sfp, value);
553 return 0;
554 case hwmon_temp_max:
555 *value = be16_to_cpu(sfp->diag.temp_high_warn);
556 sfp_hwmon_calibrate_temp(sfp, value);
557 return 0;
558
559 case hwmon_temp_crit:
560 *value = be16_to_cpu(sfp->diag.temp_high_alarm);
561 sfp_hwmon_calibrate_temp(sfp, value);
562 return 0;
563
564 case hwmon_temp_lcrit_alarm:
565 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
566 if (err < 0)
567 return err;
568
569 *value = !!(status & SFP_ALARM0_TEMP_LOW);
570 return 0;
571
572 case hwmon_temp_min_alarm:
573 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
574 if (err < 0)
575 return err;
576
577 *value = !!(status & SFP_WARN0_TEMP_LOW);
578 return 0;
579
580 case hwmon_temp_max_alarm:
581 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
582 if (err < 0)
583 return err;
584
585 *value = !!(status & SFP_WARN0_TEMP_HIGH);
586 return 0;
587
588 case hwmon_temp_crit_alarm:
589 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
590 if (err < 0)
591 return err;
592
593 *value = !!(status & SFP_ALARM0_TEMP_HIGH);
594 return 0;
595 default:
596 return -EOPNOTSUPP;
597 }
598
599 return -EOPNOTSUPP;
600}
601
602static int sfp_hwmon_vcc(struct sfp *sfp, u32 attr, long *value)
603{
604 u8 status;
605 int err;
606
607 switch (attr) {
608 case hwmon_in_input:
609 return sfp_hwmon_read_vcc(sfp, SFP_VCC, value);
610
611 case hwmon_in_lcrit:
612 *value = be16_to_cpu(sfp->diag.volt_low_alarm);
613 sfp_hwmon_calibrate_vcc(sfp, value);
614 return 0;
615
616 case hwmon_in_min:
617 *value = be16_to_cpu(sfp->diag.volt_low_warn);
618 sfp_hwmon_calibrate_vcc(sfp, value);
619 return 0;
620
621 case hwmon_in_max:
622 *value = be16_to_cpu(sfp->diag.volt_high_warn);
623 sfp_hwmon_calibrate_vcc(sfp, value);
624 return 0;
625
626 case hwmon_in_crit:
627 *value = be16_to_cpu(sfp->diag.volt_high_alarm);
628 sfp_hwmon_calibrate_vcc(sfp, value);
629 return 0;
630
631 case hwmon_in_lcrit_alarm:
632 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
633 if (err < 0)
634 return err;
635
636 *value = !!(status & SFP_ALARM0_VCC_LOW);
637 return 0;
638
639 case hwmon_in_min_alarm:
640 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
641 if (err < 0)
642 return err;
643
644 *value = !!(status & SFP_WARN0_VCC_LOW);
645 return 0;
646
647 case hwmon_in_max_alarm:
648 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
649 if (err < 0)
650 return err;
651
652 *value = !!(status & SFP_WARN0_VCC_HIGH);
653 return 0;
654
655 case hwmon_in_crit_alarm:
656 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
657 if (err < 0)
658 return err;
659
660 *value = !!(status & SFP_ALARM0_VCC_HIGH);
661 return 0;
662 default:
663 return -EOPNOTSUPP;
664 }
665
666 return -EOPNOTSUPP;
667}
668
669static int sfp_hwmon_bias(struct sfp *sfp, u32 attr, long *value)
670{
671 u8 status;
672 int err;
673
674 switch (attr) {
675 case hwmon_curr_input:
676 return sfp_hwmon_read_bias(sfp, SFP_TX_BIAS, value);
677
678 case hwmon_curr_lcrit:
679 *value = be16_to_cpu(sfp->diag.bias_low_alarm);
680 sfp_hwmon_calibrate_bias(sfp, value);
681 return 0;
682
683 case hwmon_curr_min:
684 *value = be16_to_cpu(sfp->diag.bias_low_warn);
685 sfp_hwmon_calibrate_bias(sfp, value);
686 return 0;
687
688 case hwmon_curr_max:
689 *value = be16_to_cpu(sfp->diag.bias_high_warn);
690 sfp_hwmon_calibrate_bias(sfp, value);
691 return 0;
692
693 case hwmon_curr_crit:
694 *value = be16_to_cpu(sfp->diag.bias_high_alarm);
695 sfp_hwmon_calibrate_bias(sfp, value);
696 return 0;
697
698 case hwmon_curr_lcrit_alarm:
699 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
700 if (err < 0)
701 return err;
702
703 *value = !!(status & SFP_ALARM0_TX_BIAS_LOW);
704 return 0;
705
706 case hwmon_curr_min_alarm:
707 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
708 if (err < 0)
709 return err;
710
711 *value = !!(status & SFP_WARN0_TX_BIAS_LOW);
712 return 0;
713
714 case hwmon_curr_max_alarm:
715 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
716 if (err < 0)
717 return err;
718
719 *value = !!(status & SFP_WARN0_TX_BIAS_HIGH);
720 return 0;
721
722 case hwmon_curr_crit_alarm:
723 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
724 if (err < 0)
725 return err;
726
727 *value = !!(status & SFP_ALARM0_TX_BIAS_HIGH);
728 return 0;
729 default:
730 return -EOPNOTSUPP;
731 }
732
733 return -EOPNOTSUPP;
734}
735
736static int sfp_hwmon_tx_power(struct sfp *sfp, u32 attr, long *value)
737{
738 u8 status;
739 int err;
740
741 switch (attr) {
742 case hwmon_power_input:
743 return sfp_hwmon_read_tx_power(sfp, SFP_TX_POWER, value);
744
745 case hwmon_power_lcrit:
746 *value = be16_to_cpu(sfp->diag.txpwr_low_alarm);
747 sfp_hwmon_calibrate_tx_power(sfp, value);
748 return 0;
749
750 case hwmon_power_min:
751 *value = be16_to_cpu(sfp->diag.txpwr_low_warn);
752 sfp_hwmon_calibrate_tx_power(sfp, value);
753 return 0;
754
755 case hwmon_power_max:
756 *value = be16_to_cpu(sfp->diag.txpwr_high_warn);
757 sfp_hwmon_calibrate_tx_power(sfp, value);
758 return 0;
759
760 case hwmon_power_crit:
761 *value = be16_to_cpu(sfp->diag.txpwr_high_alarm);
762 sfp_hwmon_calibrate_tx_power(sfp, value);
763 return 0;
764
765 case hwmon_power_lcrit_alarm:
766 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
767 if (err < 0)
768 return err;
769
770 *value = !!(status & SFP_ALARM0_TXPWR_LOW);
771 return 0;
772
773 case hwmon_power_min_alarm:
774 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
775 if (err < 0)
776 return err;
777
778 *value = !!(status & SFP_WARN0_TXPWR_LOW);
779 return 0;
780
781 case hwmon_power_max_alarm:
782 err = sfp_read(sfp, true, SFP_WARN0, &status, sizeof(status));
783 if (err < 0)
784 return err;
785
786 *value = !!(status & SFP_WARN0_TXPWR_HIGH);
787 return 0;
788
789 case hwmon_power_crit_alarm:
790 err = sfp_read(sfp, true, SFP_ALARM0, &status, sizeof(status));
791 if (err < 0)
792 return err;
793
794 *value = !!(status & SFP_ALARM0_TXPWR_HIGH);
795 return 0;
796 default:
797 return -EOPNOTSUPP;
798 }
799
800 return -EOPNOTSUPP;
801}
802
803static int sfp_hwmon_rx_power(struct sfp *sfp, u32 attr, long *value)
804{
805 u8 status;
806 int err;
807
808 switch (attr) {
809 case hwmon_power_input:
810 return sfp_hwmon_read_rx_power(sfp, SFP_RX_POWER, value);
811
812 case hwmon_power_lcrit:
813 *value = be16_to_cpu(sfp->diag.rxpwr_low_alarm);
814 sfp_hwmon_to_rx_power(value);
815 return 0;
816
817 case hwmon_power_min:
818 *value = be16_to_cpu(sfp->diag.rxpwr_low_warn);
819 sfp_hwmon_to_rx_power(value);
820 return 0;
821
822 case hwmon_power_max:
823 *value = be16_to_cpu(sfp->diag.rxpwr_high_warn);
824 sfp_hwmon_to_rx_power(value);
825 return 0;
826
827 case hwmon_power_crit:
828 *value = be16_to_cpu(sfp->diag.rxpwr_high_alarm);
829 sfp_hwmon_to_rx_power(value);
830 return 0;
831
832 case hwmon_power_lcrit_alarm:
833 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
834 if (err < 0)
835 return err;
836
837 *value = !!(status & SFP_ALARM1_RXPWR_LOW);
838 return 0;
839
840 case hwmon_power_min_alarm:
841 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
842 if (err < 0)
843 return err;
844
845 *value = !!(status & SFP_WARN1_RXPWR_LOW);
846 return 0;
847
848 case hwmon_power_max_alarm:
849 err = sfp_read(sfp, true, SFP_WARN1, &status, sizeof(status));
850 if (err < 0)
851 return err;
852
853 *value = !!(status & SFP_WARN1_RXPWR_HIGH);
854 return 0;
855
856 case hwmon_power_crit_alarm:
857 err = sfp_read(sfp, true, SFP_ALARM1, &status, sizeof(status));
858 if (err < 0)
859 return err;
860
861 *value = !!(status & SFP_ALARM1_RXPWR_HIGH);
862 return 0;
863 default:
864 return -EOPNOTSUPP;
865 }
866
867 return -EOPNOTSUPP;
868}
869
870static int sfp_hwmon_read(struct device *dev, enum hwmon_sensor_types type,
871 u32 attr, int channel, long *value)
872{
873 struct sfp *sfp = dev_get_drvdata(dev);
874
875 switch (type) {
876 case hwmon_temp:
877 return sfp_hwmon_temp(sfp, attr, value);
878 case hwmon_in:
879 return sfp_hwmon_vcc(sfp, attr, value);
880 case hwmon_curr:
881 return sfp_hwmon_bias(sfp, attr, value);
882 case hwmon_power:
883 switch (channel) {
884 case 0:
885 return sfp_hwmon_tx_power(sfp, attr, value);
886 case 1:
887 return sfp_hwmon_rx_power(sfp, attr, value);
888 default:
889 return -EOPNOTSUPP;
890 }
891 default:
892 return -EOPNOTSUPP;
893 }
894}
895
896static const struct hwmon_ops sfp_hwmon_ops = {
897 .is_visible = sfp_hwmon_is_visible,
898 .read = sfp_hwmon_read,
899};
900
901static u32 sfp_hwmon_chip_config[] = {
902 HWMON_C_REGISTER_TZ,
903 0,
904};
905
906static const struct hwmon_channel_info sfp_hwmon_chip = {
907 .type = hwmon_chip,
908 .config = sfp_hwmon_chip_config,
909};
910
911static u32 sfp_hwmon_temp_config[] = {
912 HWMON_T_INPUT |
913 HWMON_T_MAX | HWMON_T_MIN |
914 HWMON_T_MAX_ALARM | HWMON_T_MIN_ALARM |
915 HWMON_T_CRIT | HWMON_T_LCRIT |
916 HWMON_T_CRIT_ALARM | HWMON_T_LCRIT_ALARM,
917 0,
918};
919
920static const struct hwmon_channel_info sfp_hwmon_temp_channel_info = {
921 .type = hwmon_temp,
922 .config = sfp_hwmon_temp_config,
923};
924
925static u32 sfp_hwmon_vcc_config[] = {
926 HWMON_I_INPUT |
927 HWMON_I_MAX | HWMON_I_MIN |
928 HWMON_I_MAX_ALARM | HWMON_I_MIN_ALARM |
929 HWMON_I_CRIT | HWMON_I_LCRIT |
930 HWMON_I_CRIT_ALARM | HWMON_I_LCRIT_ALARM,
931 0,
932};
933
934static const struct hwmon_channel_info sfp_hwmon_vcc_channel_info = {
935 .type = hwmon_in,
936 .config = sfp_hwmon_vcc_config,
937};
938
939static u32 sfp_hwmon_bias_config[] = {
940 HWMON_C_INPUT |
941 HWMON_C_MAX | HWMON_C_MIN |
942 HWMON_C_MAX_ALARM | HWMON_C_MIN_ALARM |
943 HWMON_C_CRIT | HWMON_C_LCRIT |
944 HWMON_C_CRIT_ALARM | HWMON_C_LCRIT_ALARM,
945 0,
946};
947
948static const struct hwmon_channel_info sfp_hwmon_bias_channel_info = {
949 .type = hwmon_curr,
950 .config = sfp_hwmon_bias_config,
951};
952
953static u32 sfp_hwmon_power_config[] = {
954 /* Transmit power */
955 HWMON_P_INPUT |
956 HWMON_P_MAX | HWMON_P_MIN |
957 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
958 HWMON_P_CRIT | HWMON_P_LCRIT |
959 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
960 /* Receive power */
961 HWMON_P_INPUT |
962 HWMON_P_MAX | HWMON_P_MIN |
963 HWMON_P_MAX_ALARM | HWMON_P_MIN_ALARM |
964 HWMON_P_CRIT | HWMON_P_LCRIT |
965 HWMON_P_CRIT_ALARM | HWMON_P_LCRIT_ALARM,
966 0,
967};
968
969static const struct hwmon_channel_info sfp_hwmon_power_channel_info = {
970 .type = hwmon_power,
971 .config = sfp_hwmon_power_config,
972};
973
974static const struct hwmon_channel_info *sfp_hwmon_info[] = {
975 &sfp_hwmon_chip,
976 &sfp_hwmon_vcc_channel_info,
977 &sfp_hwmon_temp_channel_info,
978 &sfp_hwmon_bias_channel_info,
979 &sfp_hwmon_power_channel_info,
980 NULL,
981};
982
983static const struct hwmon_chip_info sfp_hwmon_chip_info = {
984 .ops = &sfp_hwmon_ops,
985 .info = sfp_hwmon_info,
986};
987
988static int sfp_hwmon_insert(struct sfp *sfp)
989{
990 int err, i;
991
992 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE)
993 return 0;
994
995 if (!(sfp->id.ext.diagmon & SFP_DIAGMON_DDM))
996 return 0;
997
998 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
999 /* This driver in general does not support address
1000 * change.
1001 */
1002 return 0;
1003
1004 err = sfp_read(sfp, true, 0, &sfp->diag, sizeof(sfp->diag));
1005 if (err < 0)
1006 return err;
1007
1008 sfp->hwmon_name = kstrdup(dev_name(sfp->dev), GFP_KERNEL);
1009 if (!sfp->hwmon_name)
1010 return -ENODEV;
1011
1012 for (i = 0; sfp->hwmon_name[i]; i++)
1013 if (hwmon_is_bad_char(sfp->hwmon_name[i]))
1014 sfp->hwmon_name[i] = '_';
1015
1016 sfp->hwmon_dev = hwmon_device_register_with_info(sfp->dev,
1017 sfp->hwmon_name, sfp,
1018 &sfp_hwmon_chip_info,
1019 NULL);
1020
1021 return PTR_ERR_OR_ZERO(sfp->hwmon_dev);
1022}
1023
1024static void sfp_hwmon_remove(struct sfp *sfp)
1025{
1026 hwmon_device_unregister(sfp->hwmon_dev);
1027 kfree(sfp->hwmon_name);
1028}
1029#else
1030static int sfp_hwmon_insert(struct sfp *sfp)
1031{
1032 return 0;
1033}
1034
1035static void sfp_hwmon_remove(struct sfp *sfp)
1036{
1037}
1038#endif
1039
Russell King73970052017-07-25 15:03:39 +01001040/* Helpers */
1041static void sfp_module_tx_disable(struct sfp *sfp)
1042{
1043 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1044 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 1);
1045 sfp->state |= SFP_F_TX_DISABLE;
1046 sfp_set_state(sfp, sfp->state);
1047}
1048
1049static void sfp_module_tx_enable(struct sfp *sfp)
1050{
1051 dev_dbg(sfp->dev, "tx disable %u -> %u\n",
1052 sfp->state & SFP_F_TX_DISABLE ? 1 : 0, 0);
1053 sfp->state &= ~SFP_F_TX_DISABLE;
1054 sfp_set_state(sfp, sfp->state);
1055}
1056
1057static void sfp_module_tx_fault_reset(struct sfp *sfp)
1058{
1059 unsigned int state = sfp->state;
1060
1061 if (state & SFP_F_TX_DISABLE)
1062 return;
1063
1064 sfp_set_state(sfp, state | SFP_F_TX_DISABLE);
1065
1066 udelay(T_RESET_US);
1067
1068 sfp_set_state(sfp, state);
1069}
1070
1071/* SFP state machine */
1072static void sfp_sm_set_timer(struct sfp *sfp, unsigned int timeout)
1073{
1074 if (timeout)
1075 mod_delayed_work(system_power_efficient_wq, &sfp->timeout,
1076 timeout);
1077 else
1078 cancel_delayed_work(&sfp->timeout);
1079}
1080
1081static void sfp_sm_next(struct sfp *sfp, unsigned int state,
1082 unsigned int timeout)
1083{
1084 sfp->sm_state = state;
1085 sfp_sm_set_timer(sfp, timeout);
1086}
1087
Florian Fainelli516b29e2017-10-30 21:42:57 -07001088static void sfp_sm_ins_next(struct sfp *sfp, unsigned int state,
1089 unsigned int timeout)
Russell King73970052017-07-25 15:03:39 +01001090{
1091 sfp->sm_mod_state = state;
1092 sfp_sm_set_timer(sfp, timeout);
1093}
1094
1095static void sfp_sm_phy_detach(struct sfp *sfp)
1096{
1097 phy_stop(sfp->mod_phy);
1098 sfp_remove_phy(sfp->sfp_bus);
1099 phy_device_remove(sfp->mod_phy);
1100 phy_device_free(sfp->mod_phy);
1101 sfp->mod_phy = NULL;
1102}
1103
1104static void sfp_sm_probe_phy(struct sfp *sfp)
1105{
1106 struct phy_device *phy;
1107 int err;
1108
1109 msleep(T_PHY_RESET_MS);
1110
1111 phy = mdiobus_scan(sfp->i2c_mii, SFP_PHY_ADDR);
Russell King20b56ed2017-12-15 16:09:36 +00001112 if (phy == ERR_PTR(-ENODEV)) {
1113 dev_info(sfp->dev, "no PHY detected\n");
Russell King73970052017-07-25 15:03:39 +01001114 return;
1115 }
Russell King20b56ed2017-12-15 16:09:36 +00001116 if (IS_ERR(phy)) {
1117 dev_err(sfp->dev, "mdiobus scan returned %ld\n", PTR_ERR(phy));
Russell King73970052017-07-25 15:03:39 +01001118 return;
1119 }
1120
1121 err = sfp_add_phy(sfp->sfp_bus, phy);
1122 if (err) {
1123 phy_device_remove(phy);
1124 phy_device_free(phy);
1125 dev_err(sfp->dev, "sfp_add_phy failed: %d\n", err);
1126 return;
1127 }
1128
1129 sfp->mod_phy = phy;
1130 phy_start(phy);
1131}
1132
1133static void sfp_sm_link_up(struct sfp *sfp)
1134{
1135 sfp_link_up(sfp->sfp_bus);
1136 sfp_sm_next(sfp, SFP_S_LINK_UP, 0);
1137}
1138
1139static void sfp_sm_link_down(struct sfp *sfp)
1140{
1141 sfp_link_down(sfp->sfp_bus);
1142}
1143
1144static void sfp_sm_link_check_los(struct sfp *sfp)
1145{
1146 unsigned int los = sfp->state & SFP_F_LOS;
1147
Russell King710dfbb2017-11-30 13:59:16 +00001148 /* If neither SFP_OPTIONS_LOS_INVERTED nor SFP_OPTIONS_LOS_NORMAL
1149 * are set, we assume that no LOS signal is available.
Russell King73970052017-07-25 15:03:39 +01001150 */
Russell Kingacf1c022017-11-30 13:59:11 +00001151 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED))
Russell King73970052017-07-25 15:03:39 +01001152 los ^= SFP_F_LOS;
Russell King710dfbb2017-11-30 13:59:16 +00001153 else if (!(sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL)))
1154 los = 0;
Russell King73970052017-07-25 15:03:39 +01001155
1156 if (los)
1157 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1158 else
1159 sfp_sm_link_up(sfp);
1160}
1161
Russell King710dfbb2017-11-30 13:59:16 +00001162static bool sfp_los_event_active(struct sfp *sfp, unsigned int event)
1163{
1164 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1165 event == SFP_E_LOS_LOW) ||
1166 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1167 event == SFP_E_LOS_HIGH);
1168}
1169
1170static bool sfp_los_event_inactive(struct sfp *sfp, unsigned int event)
1171{
1172 return (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_INVERTED) &&
1173 event == SFP_E_LOS_HIGH) ||
1174 (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_LOS_NORMAL) &&
1175 event == SFP_E_LOS_LOW);
1176}
1177
Russell King73970052017-07-25 15:03:39 +01001178static void sfp_sm_fault(struct sfp *sfp, bool warn)
1179{
1180 if (sfp->sm_retries && !--sfp->sm_retries) {
Florian Fainelli516b29e2017-10-30 21:42:57 -07001181 dev_err(sfp->dev,
1182 "module persistently indicates fault, disabling\n");
Russell King73970052017-07-25 15:03:39 +01001183 sfp_sm_next(sfp, SFP_S_TX_DISABLE, 0);
1184 } else {
1185 if (warn)
1186 dev_err(sfp->dev, "module transmit fault indicated\n");
1187
1188 sfp_sm_next(sfp, SFP_S_TX_FAULT, T_FAULT_RECOVER);
1189 }
1190}
1191
1192static void sfp_sm_mod_init(struct sfp *sfp)
1193{
1194 sfp_module_tx_enable(sfp);
1195
1196 /* Wait t_init before indicating that the link is up, provided the
1197 * current state indicates no TX_FAULT. If TX_FAULT clears before
1198 * this time, that's fine too.
1199 */
1200 sfp_sm_next(sfp, SFP_S_INIT, T_INIT_JIFFIES);
1201 sfp->sm_retries = 5;
1202
1203 /* Setting the serdes link mode is guesswork: there's no
1204 * field in the EEPROM which indicates what mode should
1205 * be used.
1206 *
1207 * If it's a gigabit-only fiber module, it probably does
1208 * not have a PHY, so switch to 802.3z negotiation mode.
1209 * Otherwise, switch to SGMII mode (which is required to
1210 * support non-gigabit speeds) and probe for a PHY.
1211 */
1212 if (sfp->id.base.e1000_base_t ||
1213 sfp->id.base.e100_base_lx ||
1214 sfp->id.base.e100_base_fx)
1215 sfp_sm_probe_phy(sfp);
1216}
1217
Jon Nettleton3bb35262018-02-27 15:53:12 +00001218static int sfp_sm_mod_hpower(struct sfp *sfp)
1219{
1220 u32 power;
1221 u8 val;
1222 int err;
1223
1224 power = 1000;
1225 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_POWER_DECL))
1226 power = 1500;
1227 if (sfp->id.ext.options & cpu_to_be16(SFP_OPTIONS_HIGH_POWER_LEVEL))
1228 power = 2000;
1229
1230 if (sfp->id.ext.sff8472_compliance == SFP_SFF8472_COMPLIANCE_NONE &&
1231 (sfp->id.ext.diagmon & (SFP_DIAGMON_DDM | SFP_DIAGMON_ADDRMODE)) !=
1232 SFP_DIAGMON_DDM) {
1233 /* The module appears not to implement bus address 0xa2,
1234 * or requires an address change sequence, so assume that
1235 * the module powers up in the indicated power mode.
1236 */
1237 if (power > sfp->max_power_mW) {
1238 dev_err(sfp->dev,
1239 "Host does not support %u.%uW modules\n",
1240 power / 1000, (power / 100) % 10);
1241 return -EINVAL;
1242 }
1243 return 0;
1244 }
1245
1246 if (power > sfp->max_power_mW) {
1247 dev_warn(sfp->dev,
1248 "Host does not support %u.%uW modules, module left in power mode 1\n",
1249 power / 1000, (power / 100) % 10);
1250 return 0;
1251 }
1252
1253 if (power <= 1000)
1254 return 0;
1255
1256 err = sfp_read(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1257 if (err != sizeof(val)) {
1258 dev_err(sfp->dev, "Failed to read EEPROM: %d\n", err);
1259 err = -EAGAIN;
1260 goto err;
1261 }
1262
1263 val |= BIT(0);
1264
1265 err = sfp_write(sfp, true, SFP_EXT_STATUS, &val, sizeof(val));
1266 if (err != sizeof(val)) {
1267 dev_err(sfp->dev, "Failed to write EEPROM: %d\n", err);
1268 err = -EAGAIN;
1269 goto err;
1270 }
1271
1272 dev_info(sfp->dev, "Module switched to %u.%uW power level\n",
1273 power / 1000, (power / 100) % 10);
1274 return T_HPOWER_LEVEL;
1275
1276err:
1277 return err;
1278}
1279
Russell King73970052017-07-25 15:03:39 +01001280static int sfp_sm_mod_probe(struct sfp *sfp)
1281{
1282 /* SFP module inserted - read I2C data */
1283 struct sfp_eeprom_id id;
Russell King981f1f82018-03-28 11:18:25 +01001284 bool cotsworks;
Russell King73970052017-07-25 15:03:39 +01001285 u8 check;
Jon Nettleton3bb35262018-02-27 15:53:12 +00001286 int ret;
Russell King73970052017-07-25 15:03:39 +01001287
Jon Nettleton3bb35262018-02-27 15:53:12 +00001288 ret = sfp_read(sfp, false, 0, &id, sizeof(id));
1289 if (ret < 0) {
1290 dev_err(sfp->dev, "failed to read EEPROM: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001291 return -EAGAIN;
1292 }
1293
Jon Nettleton3bb35262018-02-27 15:53:12 +00001294 if (ret != sizeof(id)) {
1295 dev_err(sfp->dev, "EEPROM short read: %d\n", ret);
Russell King73970052017-07-25 15:03:39 +01001296 return -EAGAIN;
1297 }
1298
Russell King981f1f82018-03-28 11:18:25 +01001299 /* Cotsworks do not seem to update the checksums when they
1300 * do the final programming with the final module part number,
1301 * serial number and date code.
1302 */
1303 cotsworks = !memcmp(id.base.vendor_name, "COTSWORKS ", 16);
1304
Russell King73970052017-07-25 15:03:39 +01001305 /* Validate the checksum over the base structure */
1306 check = sfp_check(&id.base, sizeof(id.base) - 1);
1307 if (check != id.base.cc_base) {
Russell King981f1f82018-03-28 11:18:25 +01001308 if (cotsworks) {
1309 dev_warn(sfp->dev,
1310 "EEPROM base structure checksum failure (0x%02x != 0x%02x)\n",
1311 check, id.base.cc_base);
1312 } else {
1313 dev_err(sfp->dev,
1314 "EEPROM base structure checksum failure: 0x%02x != 0x%02x\n",
1315 check, id.base.cc_base);
1316 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1317 16, 1, &id, sizeof(id), true);
1318 return -EINVAL;
1319 }
Russell King73970052017-07-25 15:03:39 +01001320 }
1321
1322 check = sfp_check(&id.ext, sizeof(id.ext) - 1);
1323 if (check != id.ext.cc_ext) {
Russell King981f1f82018-03-28 11:18:25 +01001324 if (cotsworks) {
1325 dev_warn(sfp->dev,
1326 "EEPROM extended structure checksum failure (0x%02x != 0x%02x)\n",
1327 check, id.ext.cc_ext);
1328 } else {
1329 dev_err(sfp->dev,
1330 "EEPROM extended structure checksum failure: 0x%02x != 0x%02x\n",
1331 check, id.ext.cc_ext);
1332 print_hex_dump(KERN_ERR, "sfp EE: ", DUMP_PREFIX_OFFSET,
1333 16, 1, &id, sizeof(id), true);
1334 memset(&id.ext, 0, sizeof(id.ext));
1335 }
Russell King73970052017-07-25 15:03:39 +01001336 }
1337
1338 sfp->id = id;
1339
Russell Kinga2f247e2017-12-29 12:15:12 +00001340 dev_info(sfp->dev, "module %.*s %.*s rev %.*s sn %.*s dc %.*s\n",
1341 (int)sizeof(id.base.vendor_name), id.base.vendor_name,
1342 (int)sizeof(id.base.vendor_pn), id.base.vendor_pn,
1343 (int)sizeof(id.base.vendor_rev), id.base.vendor_rev,
1344 (int)sizeof(id.ext.vendor_sn), id.ext.vendor_sn,
1345 (int)sizeof(id.ext.datecode), id.ext.datecode);
Russell King73970052017-07-25 15:03:39 +01001346
Russell King259c8612017-12-14 10:27:47 +00001347 /* Check whether we support this module */
1348 if (!sfp->type->module_supported(&sfp->id)) {
1349 dev_err(sfp->dev,
1350 "module is not supported - phys id 0x%02x 0x%02x\n",
Russell King73970052017-07-25 15:03:39 +01001351 sfp->id.base.phys_id, sfp->id.base.phys_ext_id);
1352 return -EINVAL;
1353 }
1354
Russell Kingec7681b2017-11-30 13:59:21 +00001355 /* If the module requires address swap mode, warn about it */
1356 if (sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)
1357 dev_warn(sfp->dev,
1358 "module address swap to access page 0xA2 is not supported.\n");
1359
Andrew Lunn13230612018-07-17 21:48:13 +02001360 ret = sfp_hwmon_insert(sfp);
1361 if (ret < 0)
1362 return ret;
1363
Jon Nettleton3bb35262018-02-27 15:53:12 +00001364 ret = sfp_module_insert(sfp->sfp_bus, &sfp->id);
1365 if (ret < 0)
1366 return ret;
1367
1368 return sfp_sm_mod_hpower(sfp);
Russell King73970052017-07-25 15:03:39 +01001369}
1370
1371static void sfp_sm_mod_remove(struct sfp *sfp)
1372{
1373 sfp_module_remove(sfp->sfp_bus);
1374
Andrew Lunn13230612018-07-17 21:48:13 +02001375 sfp_hwmon_remove(sfp);
1376
Russell King73970052017-07-25 15:03:39 +01001377 if (sfp->mod_phy)
1378 sfp_sm_phy_detach(sfp);
1379
1380 sfp_module_tx_disable(sfp);
1381
1382 memset(&sfp->id, 0, sizeof(sfp->id));
1383
1384 dev_info(sfp->dev, "module removed\n");
1385}
1386
1387static void sfp_sm_event(struct sfp *sfp, unsigned int event)
1388{
1389 mutex_lock(&sfp->sm_mutex);
1390
1391 dev_dbg(sfp->dev, "SM: enter %u:%u:%u event %u\n",
1392 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state, event);
1393
1394 /* This state machine tracks the insert/remove state of
1395 * the module, and handles probing the on-board EEPROM.
1396 */
1397 switch (sfp->sm_mod_state) {
1398 default:
1399 if (event == SFP_E_INSERT) {
1400 sfp_module_tx_disable(sfp);
1401 sfp_sm_ins_next(sfp, SFP_MOD_PROBE, T_PROBE_INIT);
1402 }
1403 break;
1404
1405 case SFP_MOD_PROBE:
1406 if (event == SFP_E_REMOVE) {
1407 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1408 } else if (event == SFP_E_TIMEOUT) {
Jon Nettleton3bb35262018-02-27 15:53:12 +00001409 int val = sfp_sm_mod_probe(sfp);
Russell King73970052017-07-25 15:03:39 +01001410
Jon Nettleton3bb35262018-02-27 15:53:12 +00001411 if (val == 0)
Russell King73970052017-07-25 15:03:39 +01001412 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001413 else if (val > 0)
1414 sfp_sm_ins_next(sfp, SFP_MOD_HPOWER, val);
1415 else if (val != -EAGAIN)
Russell King73970052017-07-25 15:03:39 +01001416 sfp_sm_ins_next(sfp, SFP_MOD_ERROR, 0);
Jon Nettleton3bb35262018-02-27 15:53:12 +00001417 else
1418 sfp_sm_set_timer(sfp, T_PROBE_RETRY);
Russell King73970052017-07-25 15:03:39 +01001419 }
1420 break;
1421
Jon Nettleton3bb35262018-02-27 15:53:12 +00001422 case SFP_MOD_HPOWER:
1423 if (event == SFP_E_TIMEOUT) {
1424 sfp_sm_ins_next(sfp, SFP_MOD_PRESENT, 0);
1425 break;
1426 }
1427 /* fallthrough */
Russell King73970052017-07-25 15:03:39 +01001428 case SFP_MOD_PRESENT:
1429 case SFP_MOD_ERROR:
1430 if (event == SFP_E_REMOVE) {
1431 sfp_sm_mod_remove(sfp);
1432 sfp_sm_ins_next(sfp, SFP_MOD_EMPTY, 0);
1433 }
1434 break;
1435 }
1436
1437 /* This state machine tracks the netdev up/down state */
1438 switch (sfp->sm_dev_state) {
1439 default:
1440 if (event == SFP_E_DEV_UP)
1441 sfp->sm_dev_state = SFP_DEV_UP;
1442 break;
1443
1444 case SFP_DEV_UP:
1445 if (event == SFP_E_DEV_DOWN) {
1446 /* If the module has a PHY, avoid raising TX disable
1447 * as this resets the PHY. Otherwise, raise it to
1448 * turn the laser off.
1449 */
1450 if (!sfp->mod_phy)
1451 sfp_module_tx_disable(sfp);
1452 sfp->sm_dev_state = SFP_DEV_DOWN;
1453 }
1454 break;
1455 }
1456
1457 /* Some events are global */
1458 if (sfp->sm_state != SFP_S_DOWN &&
1459 (sfp->sm_mod_state != SFP_MOD_PRESENT ||
1460 sfp->sm_dev_state != SFP_DEV_UP)) {
1461 if (sfp->sm_state == SFP_S_LINK_UP &&
1462 sfp->sm_dev_state == SFP_DEV_UP)
1463 sfp_sm_link_down(sfp);
1464 if (sfp->mod_phy)
1465 sfp_sm_phy_detach(sfp);
1466 sfp_sm_next(sfp, SFP_S_DOWN, 0);
1467 mutex_unlock(&sfp->sm_mutex);
1468 return;
1469 }
1470
1471 /* The main state machine */
1472 switch (sfp->sm_state) {
1473 case SFP_S_DOWN:
1474 if (sfp->sm_mod_state == SFP_MOD_PRESENT &&
1475 sfp->sm_dev_state == SFP_DEV_UP)
1476 sfp_sm_mod_init(sfp);
1477 break;
1478
1479 case SFP_S_INIT:
1480 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT)
1481 sfp_sm_fault(sfp, true);
1482 else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR)
1483 sfp_sm_link_check_los(sfp);
1484 break;
1485
1486 case SFP_S_WAIT_LOS:
1487 if (event == SFP_E_TX_FAULT)
1488 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +00001489 else if (sfp_los_event_inactive(sfp, event))
Russell King73970052017-07-25 15:03:39 +01001490 sfp_sm_link_up(sfp);
1491 break;
1492
1493 case SFP_S_LINK_UP:
1494 if (event == SFP_E_TX_FAULT) {
1495 sfp_sm_link_down(sfp);
1496 sfp_sm_fault(sfp, true);
Russell King710dfbb2017-11-30 13:59:16 +00001497 } else if (sfp_los_event_active(sfp, event)) {
Russell King73970052017-07-25 15:03:39 +01001498 sfp_sm_link_down(sfp);
1499 sfp_sm_next(sfp, SFP_S_WAIT_LOS, 0);
1500 }
1501 break;
1502
1503 case SFP_S_TX_FAULT:
1504 if (event == SFP_E_TIMEOUT) {
1505 sfp_module_tx_fault_reset(sfp);
1506 sfp_sm_next(sfp, SFP_S_REINIT, T_INIT_JIFFIES);
1507 }
1508 break;
1509
1510 case SFP_S_REINIT:
1511 if (event == SFP_E_TIMEOUT && sfp->state & SFP_F_TX_FAULT) {
1512 sfp_sm_fault(sfp, false);
1513 } else if (event == SFP_E_TIMEOUT || event == SFP_E_TX_CLEAR) {
1514 dev_info(sfp->dev, "module transmit fault recovered\n");
1515 sfp_sm_link_check_los(sfp);
1516 }
1517 break;
1518
1519 case SFP_S_TX_DISABLE:
1520 break;
1521 }
1522
1523 dev_dbg(sfp->dev, "SM: exit %u:%u:%u\n",
1524 sfp->sm_mod_state, sfp->sm_dev_state, sfp->sm_state);
1525
1526 mutex_unlock(&sfp->sm_mutex);
1527}
1528
1529static void sfp_start(struct sfp *sfp)
1530{
1531 sfp_sm_event(sfp, SFP_E_DEV_UP);
1532}
1533
1534static void sfp_stop(struct sfp *sfp)
1535{
1536 sfp_sm_event(sfp, SFP_E_DEV_DOWN);
1537}
1538
1539static int sfp_module_info(struct sfp *sfp, struct ethtool_modinfo *modinfo)
1540{
1541 /* locking... and check module is present */
1542
Russell Kingec7681b2017-11-30 13:59:21 +00001543 if (sfp->id.ext.sff8472_compliance &&
1544 !(sfp->id.ext.diagmon & SFP_DIAGMON_ADDRMODE)) {
Russell King73970052017-07-25 15:03:39 +01001545 modinfo->type = ETH_MODULE_SFF_8472;
1546 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1547 } else {
1548 modinfo->type = ETH_MODULE_SFF_8079;
1549 modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1550 }
1551 return 0;
1552}
1553
1554static int sfp_module_eeprom(struct sfp *sfp, struct ethtool_eeprom *ee,
Florian Fainelli516b29e2017-10-30 21:42:57 -07001555 u8 *data)
Russell King73970052017-07-25 15:03:39 +01001556{
1557 unsigned int first, last, len;
1558 int ret;
1559
1560 if (ee->len == 0)
1561 return -EINVAL;
1562
1563 first = ee->offset;
1564 last = ee->offset + ee->len;
1565 if (first < ETH_MODULE_SFF_8079_LEN) {
1566 len = min_t(unsigned int, last, ETH_MODULE_SFF_8079_LEN);
1567 len -= first;
1568
Russell King2794ffc2017-12-15 16:09:41 +00001569 ret = sfp_read(sfp, false, first, data, len);
Russell King73970052017-07-25 15:03:39 +01001570 if (ret < 0)
1571 return ret;
1572
1573 first += len;
1574 data += len;
1575 }
Russell King2794ffc2017-12-15 16:09:41 +00001576 if (first < ETH_MODULE_SFF_8472_LEN && last > ETH_MODULE_SFF_8079_LEN) {
Russell King73970052017-07-25 15:03:39 +01001577 len = min_t(unsigned int, last, ETH_MODULE_SFF_8472_LEN);
1578 len -= first;
1579 first -= ETH_MODULE_SFF_8079_LEN;
1580
Russell King2794ffc2017-12-15 16:09:41 +00001581 ret = sfp_read(sfp, true, first, data, len);
Russell King73970052017-07-25 15:03:39 +01001582 if (ret < 0)
1583 return ret;
1584 }
1585 return 0;
1586}
1587
1588static const struct sfp_socket_ops sfp_module_ops = {
1589 .start = sfp_start,
1590 .stop = sfp_stop,
1591 .module_info = sfp_module_info,
1592 .module_eeprom = sfp_module_eeprom,
1593};
1594
1595static void sfp_timeout(struct work_struct *work)
1596{
1597 struct sfp *sfp = container_of(work, struct sfp, timeout.work);
1598
1599 rtnl_lock();
1600 sfp_sm_event(sfp, SFP_E_TIMEOUT);
1601 rtnl_unlock();
1602}
1603
1604static void sfp_check_state(struct sfp *sfp)
1605{
1606 unsigned int state, i, changed;
1607
1608 state = sfp_get_state(sfp);
1609 changed = state ^ sfp->state;
1610 changed &= SFP_F_PRESENT | SFP_F_LOS | SFP_F_TX_FAULT;
1611
1612 for (i = 0; i < GPIO_MAX; i++)
1613 if (changed & BIT(i))
1614 dev_dbg(sfp->dev, "%s %u -> %u\n", gpio_of_names[i],
1615 !!(sfp->state & BIT(i)), !!(state & BIT(i)));
1616
1617 state |= sfp->state & (SFP_F_TX_DISABLE | SFP_F_RATE_SELECT);
1618 sfp->state = state;
1619
1620 rtnl_lock();
1621 if (changed & SFP_F_PRESENT)
1622 sfp_sm_event(sfp, state & SFP_F_PRESENT ?
1623 SFP_E_INSERT : SFP_E_REMOVE);
1624
1625 if (changed & SFP_F_TX_FAULT)
1626 sfp_sm_event(sfp, state & SFP_F_TX_FAULT ?
1627 SFP_E_TX_FAULT : SFP_E_TX_CLEAR);
1628
1629 if (changed & SFP_F_LOS)
1630 sfp_sm_event(sfp, state & SFP_F_LOS ?
1631 SFP_E_LOS_HIGH : SFP_E_LOS_LOW);
1632 rtnl_unlock();
1633}
1634
1635static irqreturn_t sfp_irq(int irq, void *data)
1636{
1637 struct sfp *sfp = data;
1638
1639 sfp_check_state(sfp);
1640
1641 return IRQ_HANDLED;
1642}
1643
1644static void sfp_poll(struct work_struct *work)
1645{
1646 struct sfp *sfp = container_of(work, struct sfp, poll.work);
1647
1648 sfp_check_state(sfp);
1649 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1650}
1651
1652static struct sfp *sfp_alloc(struct device *dev)
1653{
1654 struct sfp *sfp;
1655
1656 sfp = kzalloc(sizeof(*sfp), GFP_KERNEL);
1657 if (!sfp)
1658 return ERR_PTR(-ENOMEM);
1659
1660 sfp->dev = dev;
1661
1662 mutex_init(&sfp->sm_mutex);
1663 INIT_DELAYED_WORK(&sfp->poll, sfp_poll);
1664 INIT_DELAYED_WORK(&sfp->timeout, sfp_timeout);
1665
1666 return sfp;
1667}
1668
1669static void sfp_cleanup(void *data)
1670{
1671 struct sfp *sfp = data;
1672
1673 cancel_delayed_work_sync(&sfp->poll);
1674 cancel_delayed_work_sync(&sfp->timeout);
1675 if (sfp->i2c_mii) {
1676 mdiobus_unregister(sfp->i2c_mii);
1677 mdiobus_free(sfp->i2c_mii);
1678 }
1679 if (sfp->i2c)
1680 i2c_put_adapter(sfp->i2c);
1681 kfree(sfp);
1682}
1683
1684static int sfp_probe(struct platform_device *pdev)
1685{
Russell King259c8612017-12-14 10:27:47 +00001686 const struct sff_data *sff;
Russell King73970052017-07-25 15:03:39 +01001687 struct sfp *sfp;
1688 bool poll = false;
1689 int irq, err, i;
1690
1691 sfp = sfp_alloc(&pdev->dev);
1692 if (IS_ERR(sfp))
1693 return PTR_ERR(sfp);
1694
1695 platform_set_drvdata(pdev, sfp);
1696
1697 err = devm_add_action(sfp->dev, sfp_cleanup, sfp);
1698 if (err < 0)
1699 return err;
1700
Russell King259c8612017-12-14 10:27:47 +00001701 sff = sfp->type = &sfp_data;
1702
Russell King73970052017-07-25 15:03:39 +01001703 if (pdev->dev.of_node) {
1704 struct device_node *node = pdev->dev.of_node;
Russell King259c8612017-12-14 10:27:47 +00001705 const struct of_device_id *id;
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001706 struct i2c_adapter *i2c;
Russell King73970052017-07-25 15:03:39 +01001707 struct device_node *np;
1708
Russell King259c8612017-12-14 10:27:47 +00001709 id = of_match_node(sfp_of_match, node);
1710 if (WARN_ON(!id))
1711 return -EINVAL;
1712
1713 sff = sfp->type = id->data;
1714
Russell King73970052017-07-25 15:03:39 +01001715 np = of_parse_phandle(node, "i2c-bus", 0);
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001716 if (!np) {
1717 dev_err(sfp->dev, "missing 'i2c-bus' property\n");
1718 return -ENODEV;
1719 }
Russell King73970052017-07-25 15:03:39 +01001720
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001721 i2c = of_find_i2c_adapter_by_node(np);
1722 of_node_put(np);
1723 if (!i2c)
1724 return -EPROBE_DEFER;
Russell King73970052017-07-25 15:03:39 +01001725
Antoine Tenart66ede1f2018-05-22 12:18:00 +02001726 err = sfp_i2c_configure(sfp, i2c);
1727 if (err < 0) {
1728 i2c_put_adapter(i2c);
1729 return err;
Russell King73970052017-07-25 15:03:39 +01001730 }
Russell King259c8612017-12-14 10:27:47 +00001731 }
Russell King73970052017-07-25 15:03:39 +01001732
Russell King259c8612017-12-14 10:27:47 +00001733 for (i = 0; i < GPIO_MAX; i++)
1734 if (sff->gpios & BIT(i)) {
Russell King73970052017-07-25 15:03:39 +01001735 sfp->gpio[i] = devm_gpiod_get_optional(sfp->dev,
1736 gpio_of_names[i], gpio_flags[i]);
1737 if (IS_ERR(sfp->gpio[i]))
1738 return PTR_ERR(sfp->gpio[i]);
1739 }
1740
Russell King259c8612017-12-14 10:27:47 +00001741 sfp->get_state = sfp_gpio_get_state;
1742 sfp->set_state = sfp_gpio_set_state;
1743
1744 /* Modules that have no detect signal are always present */
1745 if (!(sfp->gpio[GPIO_MODDEF0]))
1746 sfp->get_state = sff_gpio_get_state;
Russell King73970052017-07-25 15:03:39 +01001747
Jon Nettleton3bb35262018-02-27 15:53:12 +00001748 device_property_read_u32(&pdev->dev, "maximum-power-milliwatt",
1749 &sfp->max_power_mW);
1750 if (!sfp->max_power_mW)
1751 sfp->max_power_mW = 1000;
1752
1753 dev_info(sfp->dev, "Host maximum power %u.%uW\n",
1754 sfp->max_power_mW / 1000, (sfp->max_power_mW / 100) % 10);
1755
Russell King73970052017-07-25 15:03:39 +01001756 sfp->sfp_bus = sfp_register_socket(sfp->dev, sfp, &sfp_module_ops);
1757 if (!sfp->sfp_bus)
1758 return -ENOMEM;
1759
1760 /* Get the initial state, and always signal TX disable,
1761 * since the network interface will not be up.
1762 */
1763 sfp->state = sfp_get_state(sfp) | SFP_F_TX_DISABLE;
1764
1765 if (sfp->gpio[GPIO_RATE_SELECT] &&
1766 gpiod_get_value_cansleep(sfp->gpio[GPIO_RATE_SELECT]))
1767 sfp->state |= SFP_F_RATE_SELECT;
1768 sfp_set_state(sfp, sfp->state);
1769 sfp_module_tx_disable(sfp);
1770 rtnl_lock();
1771 if (sfp->state & SFP_F_PRESENT)
1772 sfp_sm_event(sfp, SFP_E_INSERT);
1773 rtnl_unlock();
1774
1775 for (i = 0; i < GPIO_MAX; i++) {
1776 if (gpio_flags[i] != GPIOD_IN || !sfp->gpio[i])
1777 continue;
1778
1779 irq = gpiod_to_irq(sfp->gpio[i]);
1780 if (!irq) {
1781 poll = true;
1782 continue;
1783 }
1784
1785 err = devm_request_threaded_irq(sfp->dev, irq, NULL, sfp_irq,
1786 IRQF_ONESHOT |
1787 IRQF_TRIGGER_RISING |
1788 IRQF_TRIGGER_FALLING,
1789 dev_name(sfp->dev), sfp);
1790 if (err)
1791 poll = true;
1792 }
1793
1794 if (poll)
1795 mod_delayed_work(system_wq, &sfp->poll, poll_jiffies);
1796
Antoine Tenarta1f5d1f2018-05-22 12:17:59 +02001797 /* We could have an issue in cases no Tx disable pin is available or
1798 * wired as modules using a laser as their light source will continue to
1799 * be active when the fiber is removed. This could be a safety issue and
1800 * we should at least warn the user about that.
1801 */
1802 if (!sfp->gpio[GPIO_TX_DISABLE])
1803 dev_warn(sfp->dev,
1804 "No tx_disable pin: SFP modules will always be emitting.\n");
1805
Russell King73970052017-07-25 15:03:39 +01001806 return 0;
1807}
1808
1809static int sfp_remove(struct platform_device *pdev)
1810{
1811 struct sfp *sfp = platform_get_drvdata(pdev);
1812
1813 sfp_unregister_socket(sfp->sfp_bus);
1814
1815 return 0;
1816}
1817
Russell King73970052017-07-25 15:03:39 +01001818static struct platform_driver sfp_driver = {
1819 .probe = sfp_probe,
1820 .remove = sfp_remove,
1821 .driver = {
1822 .name = "sfp",
1823 .of_match_table = sfp_of_match,
1824 },
1825};
1826
1827static int sfp_init(void)
1828{
1829 poll_jiffies = msecs_to_jiffies(100);
1830
1831 return platform_driver_register(&sfp_driver);
1832}
1833module_init(sfp_init);
1834
1835static void sfp_exit(void)
1836{
1837 platform_driver_unregister(&sfp_driver);
1838}
1839module_exit(sfp_exit);
1840
1841MODULE_ALIAS("platform:sfp");
1842MODULE_AUTHOR("Russell King");
1843MODULE_LICENSE("GPL v2");