xref: /linux/drivers/ptp/ptp_ocp.c (revision 085ca5d2017116ce3102eafa760ee8eb91fb1eeb)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2020 Facebook */
3 
4 #include <linux/bits.h>
5 #include <linux/err.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/debugfs.h>
9 #include <linux/init.h>
10 #include <linux/pci.h>
11 #include <linux/serial_8250.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk-provider.h>
14 #include <linux/platform_device.h>
15 #include <linux/platform_data/i2c-xiic.h>
16 #include <linux/platform_data/i2c-ocores.h>
17 #include <linux/ptp_clock_kernel.h>
18 #include <linux/spi/spi.h>
19 #include <linux/spi/xilinx_spi.h>
20 #include <linux/spi/altera.h>
21 #include <net/devlink.h>
22 #include <linux/i2c.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/nvmem-consumer.h>
25 #include <linux/crc16.h>
26 #include <linux/dpll.h>
27 
28 #define PCI_DEVICE_ID_META_TIMECARD		0x0400
29 
30 #define PCI_VENDOR_ID_CELESTICA			0x18d4
31 #define PCI_DEVICE_ID_CELESTICA_TIMECARD	0x1008
32 
33 #define PCI_VENDOR_ID_OROLIA			0x1ad7
34 #define PCI_DEVICE_ID_OROLIA_ARTCARD		0xa000
35 
36 #define PCI_VENDOR_ID_ADVA			0xad5a
37 #define PCI_DEVICE_ID_ADVA_TIMECARD		0x0400
38 
39 static struct class timecard_class = {
40 	.name		= "timecard",
41 };
42 
43 struct ocp_reg {
44 	u32	ctrl;
45 	u32	status;
46 	u32	select;
47 	u32	version;
48 	u32	time_ns;
49 	u32	time_sec;
50 	u32	__pad0[2];
51 	u32	adjust_ns;
52 	u32	adjust_sec;
53 	u32	__pad1[2];
54 	u32	offset_ns;
55 	u32	offset_window_ns;
56 	u32	__pad2[2];
57 	u32	drift_ns;
58 	u32	drift_window_ns;
59 	u32	__pad3[6];
60 	u32	servo_offset_p;
61 	u32	servo_offset_i;
62 	u32	servo_drift_p;
63 	u32	servo_drift_i;
64 	u32	status_offset;
65 	u32	status_drift;
66 };
67 
68 struct ptp_ocp_servo_conf {
69 	u32	servo_offset_p;
70 	u32	servo_offset_i;
71 	u32	servo_drift_p;
72 	u32	servo_drift_i;
73 };
74 
75 #define OCP_CTRL_ENABLE		BIT(0)
76 #define OCP_CTRL_ADJUST_TIME	BIT(1)
77 #define OCP_CTRL_ADJUST_OFFSET	BIT(2)
78 #define OCP_CTRL_ADJUST_DRIFT	BIT(3)
79 #define OCP_CTRL_ADJUST_SERVO	BIT(8)
80 #define OCP_CTRL_READ_TIME_REQ	BIT(30)
81 #define OCP_CTRL_READ_TIME_DONE	BIT(31)
82 
83 #define OCP_STATUS_IN_SYNC	BIT(0)
84 #define OCP_STATUS_IN_HOLDOVER	BIT(1)
85 
86 #define OCP_SELECT_CLK_NONE	0
87 #define OCP_SELECT_CLK_REG	0xfe
88 
89 struct tod_reg {
90 	u32	ctrl;
91 	u32	status;
92 	u32	uart_polarity;
93 	u32	version;
94 	u32	adj_sec;
95 	u32	__pad0[3];
96 	u32	uart_baud;
97 	u32	__pad1[3];
98 	u32	utc_status;
99 	u32	leap;
100 };
101 
102 #define TOD_CTRL_PROTOCOL	BIT(28)
103 #define TOD_CTRL_DISABLE_FMT_A	BIT(17)
104 #define TOD_CTRL_DISABLE_FMT_B	BIT(16)
105 #define TOD_CTRL_ENABLE		BIT(0)
106 #define TOD_CTRL_GNSS_MASK	GENMASK(3, 0)
107 #define TOD_CTRL_GNSS_SHIFT	24
108 
109 #define TOD_STATUS_UTC_MASK		GENMASK(7, 0)
110 #define TOD_STATUS_UTC_VALID		BIT(8)
111 #define TOD_STATUS_LEAP_ANNOUNCE	BIT(12)
112 #define TOD_STATUS_LEAP_VALID		BIT(16)
113 
114 struct ts_reg {
115 	u32	enable;
116 	u32	error;
117 	u32	polarity;
118 	u32	version;
119 	u32	__pad0[4];
120 	u32	cable_delay;
121 	u32	__pad1[3];
122 	u32	intr;
123 	u32	intr_mask;
124 	u32	event_count;
125 	u32	__pad2[1];
126 	u32	ts_count;
127 	u32	time_ns;
128 	u32	time_sec;
129 	u32	data_width;
130 	u32	data;
131 };
132 
133 struct pps_reg {
134 	u32	ctrl;
135 	u32	status;
136 	u32	__pad0[6];
137 	u32	cable_delay;
138 };
139 
140 #define PPS_STATUS_FILTER_ERR	BIT(0)
141 #define PPS_STATUS_SUPERV_ERR	BIT(1)
142 
143 struct img_reg {
144 	u32	version;
145 };
146 
147 struct gpio_reg {
148 	u32	gpio1;
149 	u32	__pad0;
150 	u32	gpio2;
151 	u32	__pad1;
152 };
153 
154 struct irig_master_reg {
155 	u32	ctrl;
156 	u32	status;
157 	u32	__pad0;
158 	u32	version;
159 	u32	adj_sec;
160 	u32	mode_ctrl;
161 };
162 
163 #define IRIG_M_CTRL_ENABLE	BIT(0)
164 
165 struct irig_slave_reg {
166 	u32	ctrl;
167 	u32	status;
168 	u32	__pad0;
169 	u32	version;
170 	u32	adj_sec;
171 	u32	mode_ctrl;
172 };
173 
174 #define IRIG_S_CTRL_ENABLE	BIT(0)
175 
176 struct dcf_master_reg {
177 	u32	ctrl;
178 	u32	status;
179 	u32	__pad0;
180 	u32	version;
181 	u32	adj_sec;
182 };
183 
184 #define DCF_M_CTRL_ENABLE	BIT(0)
185 
186 struct dcf_slave_reg {
187 	u32	ctrl;
188 	u32	status;
189 	u32	__pad0;
190 	u32	version;
191 	u32	adj_sec;
192 };
193 
194 #define DCF_S_CTRL_ENABLE	BIT(0)
195 
196 struct signal_reg {
197 	u32	enable;
198 	u32	status;
199 	u32	polarity;
200 	u32	version;
201 	u32	__pad0[4];
202 	u32	cable_delay;
203 	u32	__pad1[3];
204 	u32	intr;
205 	u32	intr_mask;
206 	u32	__pad2[2];
207 	u32	start_ns;
208 	u32	start_sec;
209 	u32	pulse_ns;
210 	u32	pulse_sec;
211 	u32	period_ns;
212 	u32	period_sec;
213 	u32	repeat_count;
214 };
215 
216 struct frequency_reg {
217 	u32	ctrl;
218 	u32	status;
219 };
220 
221 struct board_config_reg {
222 	u32 mro50_serial_activate;
223 };
224 
225 #define FREQ_STATUS_VALID	BIT(31)
226 #define FREQ_STATUS_ERROR	BIT(30)
227 #define FREQ_STATUS_OVERRUN	BIT(29)
228 #define FREQ_STATUS_MASK	GENMASK(23, 0)
229 
230 struct ptp_ocp_flash_info {
231 	const char *name;
232 	int pci_offset;
233 	int data_size;
234 	void *data;
235 };
236 
237 struct ptp_ocp_firmware_header {
238 	char magic[4];
239 	__be16 pci_vendor_id;
240 	__be16 pci_device_id;
241 	__be32 image_size;
242 	__be16 hw_revision;
243 	__be16 crc;
244 };
245 
246 #define OCP_FIRMWARE_MAGIC_HEADER "OCPC"
247 
248 struct ptp_ocp_i2c_info {
249 	const char *name;
250 	unsigned long fixed_rate;
251 	size_t data_size;
252 	void *data;
253 };
254 
255 struct ptp_ocp_ext_info {
256 	int index;
257 	irqreturn_t (*irq_fcn)(int irq, void *priv);
258 	int (*enable)(void *priv, u32 req, bool enable);
259 };
260 
261 struct ptp_ocp_ext_src {
262 	void __iomem		*mem;
263 	struct ptp_ocp		*bp;
264 	struct ptp_ocp_ext_info	*info;
265 	int			irq_vec;
266 };
267 
268 enum ptp_ocp_sma_mode {
269 	SMA_MODE_IN,
270 	SMA_MODE_OUT,
271 };
272 
273 static struct dpll_pin_frequency ptp_ocp_sma_freq[] = {
274 	DPLL_PIN_FREQUENCY_1PPS,
275 	DPLL_PIN_FREQUENCY_10MHZ,
276 	DPLL_PIN_FREQUENCY_IRIG_B,
277 	DPLL_PIN_FREQUENCY_DCF77,
278 };
279 
280 struct ptp_ocp_sma_connector {
281 	enum	ptp_ocp_sma_mode mode;
282 	bool	fixed_fcn;
283 	bool	fixed_dir;
284 	bool	disabled;
285 	u8	default_fcn;
286 	struct dpll_pin		   *dpll_pin;
287 	struct dpll_pin_properties dpll_prop;
288 	dpll_tracker		   tracker;
289 };
290 
291 struct ocp_attr_group {
292 	u64 cap;
293 	const struct attribute_group *group;
294 };
295 
296 #define OCP_CAP_BASIC	BIT(0)
297 #define OCP_CAP_SIGNAL	BIT(1)
298 #define OCP_CAP_FREQ	BIT(2)
299 
300 struct ptp_ocp_signal {
301 	ktime_t		period;
302 	ktime_t		pulse;
303 	ktime_t		phase;
304 	ktime_t		start;
305 	int		duty;
306 	bool		polarity;
307 	bool		running;
308 };
309 
310 struct ptp_ocp_serial_port {
311 	int line;
312 	int baud;
313 };
314 
315 #define OCP_BOARD_ID_LEN		13
316 #define OCP_SERIAL_LEN			6
317 #define OCP_SMA_NUM			4
318 #define OCP_SIGNAL_NUM			4
319 #define OCP_FREQ_NUM			4
320 
321 enum {
322 	PORT_GNSS,
323 	PORT_GNSS2,
324 	PORT_MAC, /* miniature atomic clock */
325 	PORT_NMEA,
326 
327 	__PORT_COUNT,
328 };
329 
330 struct ptp_ocp {
331 	struct pci_dev		*pdev;
332 	struct device		dev;
333 	spinlock_t		lock;
334 	struct ocp_reg __iomem	*reg;
335 	struct tod_reg __iomem	*tod;
336 	struct pps_reg __iomem	*pps_to_ext;
337 	struct pps_reg __iomem	*pps_to_clk;
338 	struct board_config_reg __iomem	*board_config;
339 	struct gpio_reg __iomem	*pps_select;
340 	struct gpio_reg __iomem	*sma_map1;
341 	struct gpio_reg __iomem	*sma_map2;
342 	struct irig_master_reg	__iomem *irig_out;
343 	struct irig_slave_reg	__iomem *irig_in;
344 	struct dcf_master_reg	__iomem *dcf_out;
345 	struct dcf_slave_reg	__iomem *dcf_in;
346 	struct tod_reg		__iomem *nmea_out;
347 	struct frequency_reg	__iomem *freq_in[OCP_FREQ_NUM];
348 	struct ptp_ocp_ext_src	*signal_out[OCP_SIGNAL_NUM];
349 	struct ptp_ocp_ext_src	*pps;
350 	struct ptp_ocp_ext_src	*ts0;
351 	struct ptp_ocp_ext_src	*ts1;
352 	struct ptp_ocp_ext_src	*ts2;
353 	struct ptp_ocp_ext_src	*ts3;
354 	struct ptp_ocp_ext_src	*ts4;
355 	struct ocp_art_gpio_reg __iomem *art_sma;
356 	struct img_reg __iomem	*image;
357 	struct ptp_clock	*ptp;
358 	struct ptp_clock_info	ptp_info;
359 	struct platform_device	*i2c_ctrl;
360 	struct platform_device	*spi_flash;
361 	struct clk_hw		*i2c_clk;
362 	struct timer_list	watchdog;
363 	const struct attribute_group **attr_group;
364 	const struct ptp_ocp_eeprom_map *eeprom_map;
365 	struct dentry		*debug_root;
366 	bool			sync;
367 	time64_t		gnss_lost;
368 	struct delayed_work	sync_work;
369 	int			id;
370 	int			n_irqs;
371 	struct ptp_ocp_serial_port	port[__PORT_COUNT];
372 	bool			fw_loader;
373 	u8			fw_tag;
374 	u16			fw_version;
375 	u8			board_id[OCP_BOARD_ID_LEN];
376 	u8			serial[OCP_SERIAL_LEN];
377 	bool			has_eeprom_data;
378 	u32			pps_req_map;
379 	int			flash_start;
380 	u32			utc_tai_offset;
381 	u32			ts_window_adjust;
382 	u64			fw_cap;
383 	struct ptp_ocp_signal	signal[OCP_SIGNAL_NUM];
384 	struct ptp_ocp_sma_connector sma[OCP_SMA_NUM];
385 	const struct ocp_sma_op *sma_op;
386 	struct dpll_device *dpll;
387 	dpll_tracker tracker;
388 	int signals_nr;
389 	int freq_in_nr;
390 };
391 
392 #define OCP_REQ_TIMESTAMP	BIT(0)
393 #define OCP_REQ_PPS		BIT(1)
394 
395 struct ocp_resource {
396 	unsigned long offset;
397 	int size;
398 	int irq_vec;
399 	int (*setup)(struct ptp_ocp *bp, struct ocp_resource *r);
400 	void *extra;
401 	unsigned long bp_offset;
402 	const char * const name;
403 };
404 
405 static int ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r);
406 static int ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r);
407 static int ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r);
408 static int ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r);
409 static int ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r);
410 static int ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
411 static irqreturn_t ptp_ocp_ts_irq(int irq, void *priv);
412 static irqreturn_t ptp_ocp_signal_irq(int irq, void *priv);
413 static int ptp_ocp_ts_enable(void *priv, u32 req, bool enable);
414 static int ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
415 				      struct ptp_perout_request *req);
416 static int ptp_ocp_signal_enable(void *priv, u32 req, bool enable);
417 static int ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr);
418 
419 static int ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
420 
421 static int ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r);
422 
423 static const struct ocp_attr_group fb_timecard_groups[];
424 
425 static const struct ocp_attr_group art_timecard_groups[];
426 
427 static const struct ocp_attr_group adva_timecard_groups[];
428 
429 struct ptp_ocp_eeprom_map {
430 	u16	off;
431 	u16	len;
432 	u32	bp_offset;
433 	const void * const tag;
434 };
435 
436 #define EEPROM_ENTRY(addr, member)				\
437 	.off = addr,						\
438 	.len = sizeof_field(struct ptp_ocp, member),		\
439 	.bp_offset = offsetof(struct ptp_ocp, member)
440 
441 #define BP_MAP_ENTRY_ADDR(bp, map) ({				\
442 	(void *)((uintptr_t)(bp) + (map)->bp_offset);		\
443 })
444 
445 static struct ptp_ocp_eeprom_map fb_eeprom_map[] = {
446 	{ EEPROM_ENTRY(0x43, board_id) },
447 	{ EEPROM_ENTRY(0x00, serial), .tag = "mac" },
448 	{ }
449 };
450 
451 static struct ptp_ocp_eeprom_map art_eeprom_map[] = {
452 	{ EEPROM_ENTRY(0x200 + 0x43, board_id) },
453 	{ EEPROM_ENTRY(0x200 + 0x63, serial) },
454 	{ }
455 };
456 
457 #define bp_assign_entry(bp, res, val) ({				\
458 	uintptr_t addr = (uintptr_t)(bp) + (res)->bp_offset;		\
459 	*(typeof(val) *)addr = val;					\
460 })
461 
462 #define OCP_RES_LOCATION(member) \
463 	.name = #member, .bp_offset = offsetof(struct ptp_ocp, member)
464 
465 #define OCP_MEM_RESOURCE(member) \
466 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_mem
467 
468 #define OCP_SERIAL_RESOURCE(member) \
469 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_serial
470 
471 #define OCP_I2C_RESOURCE(member) \
472 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_i2c
473 
474 #define OCP_SPI_RESOURCE(member) \
475 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_spi
476 
477 #define OCP_EXT_RESOURCE(member) \
478 	OCP_RES_LOCATION(member), .setup = ptp_ocp_register_ext
479 
480 /* This is the MSI vector mapping used.
481  * 0: PPS (TS5)
482  * 1: TS0
483  * 2: TS1
484  * 3: GNSS1
485  * 4: GNSS2
486  * 5: MAC
487  * 6: TS2
488  * 7: I2C controller
489  * 8: HWICAP (notused)
490  * 9: SPI Flash
491  * 10: NMEA
492  * 11: Signal Generator 1
493  * 12: Signal Generator 2
494  * 13: Signal Generator 3
495  * 14: Signal Generator 4
496  * 15: TS3
497  * 16: TS4
498  --
499  * 8: Orolia TS1
500  * 10: Orolia TS2
501  * 11: Orolia TS0 (GNSS)
502  * 12: Orolia PPS
503  * 14: Orolia TS3
504  * 15: Orolia TS4
505  */
506 
507 static struct ocp_resource ocp_fb_resource[] = {
508 	{
509 		OCP_MEM_RESOURCE(reg),
510 		.offset = 0x01000000, .size = 0x10000,
511 	},
512 	{
513 		OCP_EXT_RESOURCE(ts0),
514 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
515 		.extra = &(struct ptp_ocp_ext_info) {
516 			.index = 0,
517 			.irq_fcn = ptp_ocp_ts_irq,
518 			.enable = ptp_ocp_ts_enable,
519 		},
520 	},
521 	{
522 		OCP_EXT_RESOURCE(ts1),
523 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
524 		.extra = &(struct ptp_ocp_ext_info) {
525 			.index = 1,
526 			.irq_fcn = ptp_ocp_ts_irq,
527 			.enable = ptp_ocp_ts_enable,
528 		},
529 	},
530 	{
531 		OCP_EXT_RESOURCE(ts2),
532 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
533 		.extra = &(struct ptp_ocp_ext_info) {
534 			.index = 2,
535 			.irq_fcn = ptp_ocp_ts_irq,
536 			.enable = ptp_ocp_ts_enable,
537 		},
538 	},
539 	{
540 		OCP_EXT_RESOURCE(ts3),
541 		.offset = 0x01110000, .size = 0x10000, .irq_vec = 15,
542 		.extra = &(struct ptp_ocp_ext_info) {
543 			.index = 3,
544 			.irq_fcn = ptp_ocp_ts_irq,
545 			.enable = ptp_ocp_ts_enable,
546 		},
547 	},
548 	{
549 		OCP_EXT_RESOURCE(ts4),
550 		.offset = 0x01120000, .size = 0x10000, .irq_vec = 16,
551 		.extra = &(struct ptp_ocp_ext_info) {
552 			.index = 4,
553 			.irq_fcn = ptp_ocp_ts_irq,
554 			.enable = ptp_ocp_ts_enable,
555 		},
556 	},
557 	/* Timestamp for PHC and/or PPS generator */
558 	{
559 		OCP_EXT_RESOURCE(pps),
560 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
561 		.extra = &(struct ptp_ocp_ext_info) {
562 			.index = 5,
563 			.irq_fcn = ptp_ocp_ts_irq,
564 			.enable = ptp_ocp_ts_enable,
565 		},
566 	},
567 	{
568 		OCP_EXT_RESOURCE(signal_out[0]),
569 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
570 		.extra = &(struct ptp_ocp_ext_info) {
571 			.index = 1,
572 			.irq_fcn = ptp_ocp_signal_irq,
573 			.enable = ptp_ocp_signal_enable,
574 		},
575 	},
576 	{
577 		OCP_EXT_RESOURCE(signal_out[1]),
578 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
579 		.extra = &(struct ptp_ocp_ext_info) {
580 			.index = 2,
581 			.irq_fcn = ptp_ocp_signal_irq,
582 			.enable = ptp_ocp_signal_enable,
583 		},
584 	},
585 	{
586 		OCP_EXT_RESOURCE(signal_out[2]),
587 		.offset = 0x010F0000, .size = 0x10000, .irq_vec = 13,
588 		.extra = &(struct ptp_ocp_ext_info) {
589 			.index = 3,
590 			.irq_fcn = ptp_ocp_signal_irq,
591 			.enable = ptp_ocp_signal_enable,
592 		},
593 	},
594 	{
595 		OCP_EXT_RESOURCE(signal_out[3]),
596 		.offset = 0x01100000, .size = 0x10000, .irq_vec = 14,
597 		.extra = &(struct ptp_ocp_ext_info) {
598 			.index = 4,
599 			.irq_fcn = ptp_ocp_signal_irq,
600 			.enable = ptp_ocp_signal_enable,
601 		},
602 	},
603 	{
604 		OCP_MEM_RESOURCE(pps_to_ext),
605 		.offset = 0x01030000, .size = 0x10000,
606 	},
607 	{
608 		OCP_MEM_RESOURCE(pps_to_clk),
609 		.offset = 0x01040000, .size = 0x10000,
610 	},
611 	{
612 		OCP_MEM_RESOURCE(tod),
613 		.offset = 0x01050000, .size = 0x10000,
614 	},
615 	{
616 		OCP_MEM_RESOURCE(irig_in),
617 		.offset = 0x01070000, .size = 0x10000,
618 	},
619 	{
620 		OCP_MEM_RESOURCE(irig_out),
621 		.offset = 0x01080000, .size = 0x10000,
622 	},
623 	{
624 		OCP_MEM_RESOURCE(dcf_in),
625 		.offset = 0x01090000, .size = 0x10000,
626 	},
627 	{
628 		OCP_MEM_RESOURCE(dcf_out),
629 		.offset = 0x010A0000, .size = 0x10000,
630 	},
631 	{
632 		OCP_MEM_RESOURCE(nmea_out),
633 		.offset = 0x010B0000, .size = 0x10000,
634 	},
635 	{
636 		OCP_MEM_RESOURCE(image),
637 		.offset = 0x00020000, .size = 0x1000,
638 	},
639 	{
640 		OCP_MEM_RESOURCE(pps_select),
641 		.offset = 0x00130000, .size = 0x1000,
642 	},
643 	{
644 		OCP_MEM_RESOURCE(sma_map1),
645 		.offset = 0x00140000, .size = 0x1000,
646 	},
647 	{
648 		OCP_MEM_RESOURCE(sma_map2),
649 		.offset = 0x00220000, .size = 0x1000,
650 	},
651 	{
652 		OCP_I2C_RESOURCE(i2c_ctrl),
653 		.offset = 0x00150000, .size = 0x10000, .irq_vec = 7,
654 		.extra = &(struct ptp_ocp_i2c_info) {
655 			.name = "xiic-i2c",
656 			.fixed_rate = 50000000,
657 			.data_size = sizeof(struct xiic_i2c_platform_data),
658 			.data = &(struct xiic_i2c_platform_data) {
659 				.num_devices = 2,
660 				.devices = (struct i2c_board_info[]) {
661 					{ I2C_BOARD_INFO("24c02", 0x50) },
662 					{ I2C_BOARD_INFO("24mac402", 0x58),
663 					  .platform_data = "mac" },
664 				},
665 			},
666 		},
667 	},
668 	{
669 		OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
670 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
671 		.extra = &(struct ptp_ocp_serial_port) {
672 			.baud = 115200,
673 		},
674 	},
675 	{
676 		OCP_SERIAL_RESOURCE(port[PORT_GNSS2]),
677 		.offset = 0x00170000 + 0x1000, .irq_vec = 4,
678 		.extra = &(struct ptp_ocp_serial_port) {
679 			.baud = 115200,
680 		},
681 	},
682 	{
683 		OCP_SERIAL_RESOURCE(port[PORT_MAC]),
684 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
685 		.extra = &(struct ptp_ocp_serial_port) {
686 			.baud = 57600,
687 		},
688 	},
689 	{
690 		OCP_SERIAL_RESOURCE(port[PORT_NMEA]),
691 		.offset = 0x00190000 + 0x1000, .irq_vec = 10,
692 	},
693 	{
694 		OCP_SPI_RESOURCE(spi_flash),
695 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
696 		.extra = &(struct ptp_ocp_flash_info) {
697 			.name = "xilinx_spi", .pci_offset = 0,
698 			.data_size = sizeof(struct xspi_platform_data),
699 			.data = &(struct xspi_platform_data) {
700 				.num_chipselect = 1,
701 				.bits_per_word = 8,
702 				.num_devices = 1,
703 				.force_irq = true,
704 				.devices = &(struct spi_board_info) {
705 					.modalias = "spi-nor",
706 				},
707 			},
708 		},
709 	},
710 	{
711 		OCP_MEM_RESOURCE(freq_in[0]),
712 		.offset = 0x01200000, .size = 0x10000,
713 	},
714 	{
715 		OCP_MEM_RESOURCE(freq_in[1]),
716 		.offset = 0x01210000, .size = 0x10000,
717 	},
718 	{
719 		OCP_MEM_RESOURCE(freq_in[2]),
720 		.offset = 0x01220000, .size = 0x10000,
721 	},
722 	{
723 		OCP_MEM_RESOURCE(freq_in[3]),
724 		.offset = 0x01230000, .size = 0x10000,
725 	},
726 	{
727 		.setup = ptp_ocp_fb_board_init,
728 		.extra = &(struct ptp_ocp_servo_conf) {
729 			.servo_offset_p = 0x2000,
730 			.servo_offset_i = 0x1000,
731 			.servo_drift_p = 0,
732 			.servo_drift_i = 0,
733 		},
734 	},
735 	{ }
736 };
737 
738 #define OCP_ART_CONFIG_SIZE		144
739 #define OCP_ART_TEMP_TABLE_SIZE		368
740 
741 struct ocp_art_gpio_reg {
742 	struct {
743 		u32	gpio;
744 		u32	__pad[3];
745 	} map[4];
746 };
747 
748 static struct ocp_resource ocp_art_resource[] = {
749 	{
750 		OCP_MEM_RESOURCE(reg),
751 		.offset = 0x01000000, .size = 0x10000,
752 	},
753 	{
754 		OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
755 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
756 		.extra = &(struct ptp_ocp_serial_port) {
757 			.baud = 115200,
758 		},
759 	},
760 	{
761 		OCP_MEM_RESOURCE(art_sma),
762 		.offset = 0x003C0000, .size = 0x1000,
763 	},
764 	/* Timestamp associated with GNSS1 receiver PPS */
765 	{
766 		OCP_EXT_RESOURCE(ts0),
767 		.offset = 0x360000, .size = 0x20, .irq_vec = 12,
768 		.extra = &(struct ptp_ocp_ext_info) {
769 			.index = 0,
770 			.irq_fcn = ptp_ocp_ts_irq,
771 			.enable = ptp_ocp_ts_enable,
772 		},
773 	},
774 	{
775 		OCP_EXT_RESOURCE(ts1),
776 		.offset = 0x380000, .size = 0x20, .irq_vec = 8,
777 		.extra = &(struct ptp_ocp_ext_info) {
778 			.index = 1,
779 			.irq_fcn = ptp_ocp_ts_irq,
780 			.enable = ptp_ocp_ts_enable,
781 		},
782 	},
783 	{
784 		OCP_EXT_RESOURCE(ts2),
785 		.offset = 0x390000, .size = 0x20, .irq_vec = 10,
786 		.extra = &(struct ptp_ocp_ext_info) {
787 			.index = 2,
788 			.irq_fcn = ptp_ocp_ts_irq,
789 			.enable = ptp_ocp_ts_enable,
790 		},
791 	},
792 	{
793 		OCP_EXT_RESOURCE(ts3),
794 		.offset = 0x3A0000, .size = 0x20, .irq_vec = 14,
795 		.extra = &(struct ptp_ocp_ext_info) {
796 			.index = 3,
797 			.irq_fcn = ptp_ocp_ts_irq,
798 			.enable = ptp_ocp_ts_enable,
799 		},
800 	},
801 	{
802 		OCP_EXT_RESOURCE(ts4),
803 		.offset = 0x3B0000, .size = 0x20, .irq_vec = 15,
804 		.extra = &(struct ptp_ocp_ext_info) {
805 			.index = 4,
806 			.irq_fcn = ptp_ocp_ts_irq,
807 			.enable = ptp_ocp_ts_enable,
808 		},
809 	},
810 	/* Timestamp associated with Internal PPS of the card */
811 	{
812 		OCP_EXT_RESOURCE(pps),
813 		.offset = 0x00330000, .size = 0x20, .irq_vec = 11,
814 		.extra = &(struct ptp_ocp_ext_info) {
815 			.index = 5,
816 			.irq_fcn = ptp_ocp_ts_irq,
817 			.enable = ptp_ocp_ts_enable,
818 		},
819 	},
820 	{
821 		OCP_SPI_RESOURCE(spi_flash),
822 		.offset = 0x00310000, .size = 0x10000, .irq_vec = 9,
823 		.extra = &(struct ptp_ocp_flash_info) {
824 			.name = "spi_altera", .pci_offset = 0,
825 			.data_size = sizeof(struct altera_spi_platform_data),
826 			.data = &(struct altera_spi_platform_data) {
827 				.num_chipselect = 1,
828 				.num_devices = 1,
829 				.devices = &(struct spi_board_info) {
830 					.modalias = "spi-nor",
831 				},
832 			},
833 		},
834 	},
835 	{
836 		OCP_I2C_RESOURCE(i2c_ctrl),
837 		.offset = 0x350000, .size = 0x100, .irq_vec = 4,
838 		.extra = &(struct ptp_ocp_i2c_info) {
839 			.name = "ocores-i2c",
840 			.fixed_rate = 400000,
841 			.data_size = sizeof(struct ocores_i2c_platform_data),
842 			.data = &(struct ocores_i2c_platform_data) {
843 				.clock_khz = 125000,
844 				.bus_khz = 400,
845 				.num_devices = 1,
846 				.devices = &(struct i2c_board_info) {
847 					I2C_BOARD_INFO("24c08", 0x50),
848 				},
849 			},
850 		},
851 	},
852 	{
853 		OCP_SERIAL_RESOURCE(port[PORT_MAC]),
854 		.offset = 0x00190000, .irq_vec = 7,
855 		.extra = &(struct ptp_ocp_serial_port) {
856 			.baud = 9600,
857 		},
858 	},
859 	{
860 		OCP_MEM_RESOURCE(board_config),
861 		.offset = 0x210000, .size = 0x1000,
862 	},
863 	{
864 		.setup = ptp_ocp_art_board_init,
865 		.extra = &(struct ptp_ocp_servo_conf) {
866 			.servo_offset_p = 0x2000,
867 			.servo_offset_i = 0x1000,
868 			.servo_drift_p = 0,
869 			.servo_drift_i = 0,
870 		},
871 	},
872 	{ }
873 };
874 
875 static struct ocp_resource ocp_adva_resource[] = {
876 	{
877 		OCP_MEM_RESOURCE(reg),
878 		.offset = 0x01000000, .size = 0x10000,
879 	},
880 	{
881 		OCP_EXT_RESOURCE(ts0),
882 		.offset = 0x01010000, .size = 0x10000, .irq_vec = 1,
883 		.extra = &(struct ptp_ocp_ext_info) {
884 			.index = 0,
885 			.irq_fcn = ptp_ocp_ts_irq,
886 			.enable = ptp_ocp_ts_enable,
887 		},
888 	},
889 	{
890 		OCP_EXT_RESOURCE(ts1),
891 		.offset = 0x01020000, .size = 0x10000, .irq_vec = 2,
892 		.extra = &(struct ptp_ocp_ext_info) {
893 			.index = 1,
894 			.irq_fcn = ptp_ocp_ts_irq,
895 			.enable = ptp_ocp_ts_enable,
896 		},
897 	},
898 	{
899 		OCP_EXT_RESOURCE(ts2),
900 		.offset = 0x01060000, .size = 0x10000, .irq_vec = 6,
901 		.extra = &(struct ptp_ocp_ext_info) {
902 			.index = 2,
903 			.irq_fcn = ptp_ocp_ts_irq,
904 			.enable = ptp_ocp_ts_enable,
905 		},
906 	},
907 	/* Timestamp for PHC and/or PPS generator */
908 	{
909 		OCP_EXT_RESOURCE(pps),
910 		.offset = 0x010C0000, .size = 0x10000, .irq_vec = 0,
911 		.extra = &(struct ptp_ocp_ext_info) {
912 			.index = 5,
913 			.irq_fcn = ptp_ocp_ts_irq,
914 			.enable = ptp_ocp_ts_enable,
915 		},
916 	},
917 	{
918 		OCP_EXT_RESOURCE(signal_out[0]),
919 		.offset = 0x010D0000, .size = 0x10000, .irq_vec = 11,
920 		.extra = &(struct ptp_ocp_ext_info) {
921 			.index = 1,
922 			.irq_fcn = ptp_ocp_signal_irq,
923 			.enable = ptp_ocp_signal_enable,
924 		},
925 	},
926 	{
927 		OCP_EXT_RESOURCE(signal_out[1]),
928 		.offset = 0x010E0000, .size = 0x10000, .irq_vec = 12,
929 		.extra = &(struct ptp_ocp_ext_info) {
930 			.index = 2,
931 			.irq_fcn = ptp_ocp_signal_irq,
932 			.enable = ptp_ocp_signal_enable,
933 		},
934 	},
935 	{
936 		OCP_MEM_RESOURCE(pps_to_ext),
937 		.offset = 0x01030000, .size = 0x10000,
938 	},
939 	{
940 		OCP_MEM_RESOURCE(pps_to_clk),
941 		.offset = 0x01040000, .size = 0x10000,
942 	},
943 	{
944 		OCP_MEM_RESOURCE(tod),
945 		.offset = 0x01050000, .size = 0x10000,
946 	},
947 	{
948 		OCP_MEM_RESOURCE(image),
949 		.offset = 0x00020000, .size = 0x1000,
950 	},
951 	{
952 		OCP_MEM_RESOURCE(pps_select),
953 		.offset = 0x00130000, .size = 0x1000,
954 	},
955 	{
956 		OCP_MEM_RESOURCE(sma_map1),
957 		.offset = 0x00140000, .size = 0x1000,
958 	},
959 	{
960 		OCP_MEM_RESOURCE(sma_map2),
961 		.offset = 0x00220000, .size = 0x1000,
962 	},
963 	{
964 		OCP_SERIAL_RESOURCE(port[PORT_GNSS]),
965 		.offset = 0x00160000 + 0x1000, .irq_vec = 3,
966 		.extra = &(struct ptp_ocp_serial_port) {
967 			.baud = 9600,
968 		},
969 	},
970 	{
971 		OCP_SERIAL_RESOURCE(port[PORT_MAC]),
972 		.offset = 0x00180000 + 0x1000, .irq_vec = 5,
973 		.extra = &(struct ptp_ocp_serial_port) {
974 			.baud = 115200,
975 		},
976 	},
977 	{
978 		OCP_MEM_RESOURCE(freq_in[0]),
979 		.offset = 0x01200000, .size = 0x10000,
980 	},
981 	{
982 		OCP_MEM_RESOURCE(freq_in[1]),
983 		.offset = 0x01210000, .size = 0x10000,
984 	},
985 	{
986 		OCP_SPI_RESOURCE(spi_flash),
987 		.offset = 0x00310400, .size = 0x10000, .irq_vec = 9,
988 		.extra = &(struct ptp_ocp_flash_info) {
989 			.name = "spi_altera", .pci_offset = 0,
990 			.data_size = sizeof(struct altera_spi_platform_data),
991 			.data = &(struct altera_spi_platform_data) {
992 				.num_chipselect = 1,
993 				.num_devices = 1,
994 				.devices = &(struct spi_board_info) {
995 					.modalias = "spi-nor",
996 				},
997 			},
998 		},
999 	},
1000 	{
1001 		OCP_I2C_RESOURCE(i2c_ctrl),
1002 		.offset = 0x150000, .size = 0x100, .irq_vec = 7,
1003 		.extra = &(struct ptp_ocp_i2c_info) {
1004 			.name = "ocores-i2c",
1005 			.fixed_rate = 50000000,
1006 			.data_size = sizeof(struct ocores_i2c_platform_data),
1007 			.data = &(struct ocores_i2c_platform_data) {
1008 				.clock_khz = 50000,
1009 				.bus_khz = 100,
1010 				.reg_io_width = 4, // 32-bit/4-byte
1011 				.reg_shift = 2, // 32-bit addressing
1012 				.num_devices = 2,
1013 				.devices = (struct i2c_board_info[]) {
1014 					{ I2C_BOARD_INFO("24c02", 0x50) },
1015 					{ I2C_BOARD_INFO("24mac402", 0x58),
1016 					 .platform_data = "mac" },
1017 				},
1018 			},
1019 		},
1020 	},
1021 	{
1022 		.setup = ptp_ocp_adva_board_init,
1023 		.extra = &(struct ptp_ocp_servo_conf) {
1024 			.servo_offset_p = 0xc000,
1025 			.servo_offset_i = 0x1000,
1026 			.servo_drift_p = 0,
1027 			.servo_drift_i = 0,
1028 		},
1029 	},
1030 	{ }
1031 };
1032 
1033 static const struct pci_device_id ptp_ocp_pcidev_id[] = {
1034 	{ PCI_DEVICE_DATA(META, TIMECARD, &ocp_fb_resource) },
1035 	{ PCI_DEVICE_DATA(CELESTICA, TIMECARD, &ocp_fb_resource) },
1036 	{ PCI_DEVICE_DATA(OROLIA, ARTCARD, &ocp_art_resource) },
1037 	{ PCI_DEVICE_DATA(ADVA, TIMECARD, &ocp_adva_resource) },
1038 	{ }
1039 };
1040 MODULE_DEVICE_TABLE(pci, ptp_ocp_pcidev_id);
1041 
1042 static DEFINE_MUTEX(ptp_ocp_lock);
1043 static DEFINE_IDR(ptp_ocp_idr);
1044 
1045 struct ocp_selector {
1046 	const char *name;
1047 	int value;
1048 	u64 frequency;
1049 };
1050 
1051 static const struct ocp_selector ptp_ocp_clock[] = {
1052 	{ .name = "NONE",	.value = 0 },
1053 	{ .name = "TOD",	.value = 1 },
1054 	{ .name = "IRIG",	.value = 2 },
1055 	{ .name = "PPS",	.value = 3 },
1056 	{ .name = "PTP",	.value = 4 },
1057 	{ .name = "RTC",	.value = 5 },
1058 	{ .name = "DCF",	.value = 6 },
1059 	{ .name = "REGS",	.value = 0xfe },
1060 	{ .name = "EXT",	.value = 0xff },
1061 	{ }
1062 };
1063 
1064 #define SMA_DISABLE		BIT(16)
1065 #define SMA_ENABLE		BIT(15)
1066 #define SMA_SELECT_MASK		GENMASK(14, 0)
1067 
1068 static const struct ocp_selector ptp_ocp_sma_in[] = {
1069 	{ .name = "10Mhz",  .value = 0x0000,      .frequency = 10000000 },
1070 	{ .name = "PPS1",   .value = 0x0001,      .frequency = 1 },
1071 	{ .name = "PPS2",   .value = 0x0002,      .frequency = 1 },
1072 	{ .name = "TS1",    .value = 0x0004,      .frequency = 0 },
1073 	{ .name = "TS2",    .value = 0x0008,      .frequency = 0 },
1074 	{ .name = "IRIG",   .value = 0x0010,      .frequency = 10000 },
1075 	{ .name = "DCF",    .value = 0x0020,      .frequency = 77500 },
1076 	{ .name = "TS3",    .value = 0x0040,      .frequency = 0 },
1077 	{ .name = "TS4",    .value = 0x0080,      .frequency = 0 },
1078 	{ .name = "FREQ1",  .value = 0x0100,      .frequency = 0 },
1079 	{ .name = "FREQ2",  .value = 0x0200,      .frequency = 0 },
1080 	{ .name = "FREQ3",  .value = 0x0400,      .frequency = 0 },
1081 	{ .name = "FREQ4",  .value = 0x0800,      .frequency = 0 },
1082 	{ .name = "None",   .value = SMA_DISABLE, .frequency = 0 },
1083 	{ }
1084 };
1085 
1086 static const struct ocp_selector ptp_ocp_sma_out[] = {
1087 	{ .name = "10Mhz",	.value = 0x0000,  .frequency = 10000000 },
1088 	{ .name = "PHC",	.value = 0x0001,  .frequency = 1 },
1089 	{ .name = "MAC",	.value = 0x0002,  .frequency = 1 },
1090 	{ .name = "GNSS1",	.value = 0x0004,  .frequency = 1 },
1091 	{ .name = "GNSS2",	.value = 0x0008,  .frequency = 1 },
1092 	{ .name = "IRIG",	.value = 0x0010,  .frequency = 10000 },
1093 	{ .name = "DCF",	.value = 0x0020,  .frequency = 77000 },
1094 	{ .name = "GEN1",	.value = 0x0040 },
1095 	{ .name = "GEN2",	.value = 0x0080 },
1096 	{ .name = "GEN3",	.value = 0x0100 },
1097 	{ .name = "GEN4",	.value = 0x0200 },
1098 	{ .name = "GND",	.value = 0x2000 },
1099 	{ .name = "VCC",	.value = 0x4000 },
1100 	{ }
1101 };
1102 
1103 static const struct ocp_selector ptp_ocp_art_sma_in[] = {
1104 	{ .name = "PPS1",	.value = 0x0001,  .frequency = 1 },
1105 	{ .name = "10Mhz",	.value = 0x0008,  .frequency = 1000000 },
1106 	{ }
1107 };
1108 
1109 static const struct ocp_selector ptp_ocp_art_sma_out[] = {
1110 	{ .name = "PHC",	.value = 0x0002,  .frequency = 1 },
1111 	{ .name = "GNSS",	.value = 0x0004,  .frequency = 1 },
1112 	{ .name = "10Mhz",	.value = 0x0010,  .frequency = 10000000 },
1113 	{ }
1114 };
1115 
1116 static const struct ocp_selector ptp_ocp_adva_sma_in[] = {
1117 	{ .name = "10Mhz",	.value = 0x0000,      .frequency = 10000000},
1118 	{ .name = "PPS1",	.value = 0x0001,      .frequency = 1 },
1119 	{ .name = "PPS2",	.value = 0x0002,      .frequency = 1 },
1120 	{ .name = "TS1",	.value = 0x0004,      .frequency = 0 },
1121 	{ .name = "TS2",	.value = 0x0008,      .frequency = 0 },
1122 	{ .name = "FREQ1",	.value = 0x0100,      .frequency = 0 },
1123 	{ .name = "FREQ2",	.value = 0x0200,      .frequency = 0 },
1124 	{ .name = "None",	.value = SMA_DISABLE, .frequency = 0 },
1125 	{ }
1126 };
1127 
1128 static const struct ocp_selector ptp_ocp_adva_sma_out[] = {
1129 	{ .name = "10Mhz",	.value = 0x0000,  .frequency = 10000000},
1130 	{ .name = "PHC",	.value = 0x0001,  .frequency = 1 },
1131 	{ .name = "MAC",	.value = 0x0002,  .frequency = 1 },
1132 	{ .name = "GNSS1",	.value = 0x0004,  .frequency = 1 },
1133 	{ .name = "GEN1",	.value = 0x0040 },
1134 	{ .name = "GEN2",	.value = 0x0080 },
1135 	{ .name = "GND",	.value = 0x2000 },
1136 	{ .name = "VCC",	.value = 0x4000 },
1137 	{ }
1138 };
1139 
1140 struct ocp_sma_op {
1141 	const struct ocp_selector *tbl[2];
1142 	void (*init)(struct ptp_ocp *bp);
1143 	u32 (*get)(struct ptp_ocp *bp, int sma_nr);
1144 	int (*set_inputs)(struct ptp_ocp *bp, int sma_nr, u32 val);
1145 	int (*set_output)(struct ptp_ocp *bp, int sma_nr, u32 val);
1146 };
1147 
1148 static void
1149 ptp_ocp_sma_init(struct ptp_ocp *bp)
1150 {
1151 	return bp->sma_op->init(bp);
1152 }
1153 
1154 static u32
1155 ptp_ocp_sma_get(struct ptp_ocp *bp, int sma_nr)
1156 {
1157 	return bp->sma_op->get(bp, sma_nr);
1158 }
1159 
1160 static int
1161 ptp_ocp_sma_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
1162 {
1163 	return bp->sma_op->set_inputs(bp, sma_nr, val);
1164 }
1165 
1166 static int
1167 ptp_ocp_sma_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
1168 {
1169 	return bp->sma_op->set_output(bp, sma_nr, val);
1170 }
1171 
1172 static const char *
1173 ptp_ocp_select_name_from_val(const struct ocp_selector *tbl, int val)
1174 {
1175 	int i;
1176 
1177 	for (i = 0; tbl[i].name; i++)
1178 		if (tbl[i].value == val)
1179 			return tbl[i].name;
1180 	return NULL;
1181 }
1182 
1183 static int
1184 ptp_ocp_select_val_from_name(const struct ocp_selector *tbl, const char *name)
1185 {
1186 	const char *select;
1187 	int i;
1188 
1189 	for (i = 0; tbl[i].name; i++) {
1190 		select = tbl[i].name;
1191 		if (!strncasecmp(name, select, strlen(select)))
1192 			return tbl[i].value;
1193 	}
1194 	return -EINVAL;
1195 }
1196 
1197 static ssize_t
1198 ptp_ocp_select_table_show(const struct ocp_selector *tbl, char *buf)
1199 {
1200 	ssize_t count;
1201 	int i;
1202 
1203 	count = 0;
1204 	for (i = 0; tbl[i].name; i++)
1205 		count += sysfs_emit_at(buf, count, "%s ", tbl[i].name);
1206 	if (count)
1207 		count--;
1208 	count += sysfs_emit_at(buf, count, "\n");
1209 	return count;
1210 }
1211 
1212 static int
1213 __ptp_ocp_gettime_locked(struct ptp_ocp *bp, struct timespec64 *ts,
1214 			 struct ptp_system_timestamp *sts)
1215 {
1216 	u32 ctrl, time_sec, time_ns;
1217 	int i;
1218 
1219 	ptp_read_system_prets(sts);
1220 
1221 	ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1222 	iowrite32(ctrl, &bp->reg->ctrl);
1223 
1224 	for (i = 0; i < 100; i++) {
1225 		ctrl = ioread32(&bp->reg->ctrl);
1226 		if (ctrl & OCP_CTRL_READ_TIME_DONE)
1227 			break;
1228 	}
1229 	ptp_read_system_postts(sts);
1230 
1231 	if (sts && bp->ts_window_adjust) {
1232 		s64 ns = timespec64_to_ns(&sts->post_ts);
1233 
1234 		sts->post_ts = ns_to_timespec64(ns - bp->ts_window_adjust);
1235 	}
1236 
1237 	time_ns = ioread32(&bp->reg->time_ns);
1238 	time_sec = ioread32(&bp->reg->time_sec);
1239 
1240 	ts->tv_sec = time_sec;
1241 	ts->tv_nsec = time_ns;
1242 
1243 	return ctrl & OCP_CTRL_READ_TIME_DONE ? 0 : -ETIMEDOUT;
1244 }
1245 
1246 static int
1247 ptp_ocp_gettimex(struct ptp_clock_info *ptp_info, struct timespec64 *ts,
1248 		 struct ptp_system_timestamp *sts)
1249 {
1250 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1251 	unsigned long flags;
1252 	int err;
1253 
1254 	spin_lock_irqsave(&bp->lock, flags);
1255 	err = __ptp_ocp_gettime_locked(bp, ts, sts);
1256 	spin_unlock_irqrestore(&bp->lock, flags);
1257 
1258 	return err;
1259 }
1260 
1261 static void
1262 __ptp_ocp_settime_locked(struct ptp_ocp *bp, const struct timespec64 *ts)
1263 {
1264 	u32 ctrl, time_sec, time_ns;
1265 	u32 select;
1266 
1267 	time_ns = ts->tv_nsec;
1268 	time_sec = ts->tv_sec;
1269 
1270 	select = ioread32(&bp->reg->select);
1271 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1272 
1273 	iowrite32(time_ns, &bp->reg->adjust_ns);
1274 	iowrite32(time_sec, &bp->reg->adjust_sec);
1275 
1276 	ctrl = OCP_CTRL_ADJUST_TIME | OCP_CTRL_ENABLE;
1277 	iowrite32(ctrl, &bp->reg->ctrl);
1278 
1279 	/* restore clock selection */
1280 	iowrite32(select >> 16, &bp->reg->select);
1281 }
1282 
1283 static int
1284 ptp_ocp_settime(struct ptp_clock_info *ptp_info, const struct timespec64 *ts)
1285 {
1286 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1287 	unsigned long flags;
1288 
1289 	spin_lock_irqsave(&bp->lock, flags);
1290 	__ptp_ocp_settime_locked(bp, ts);
1291 	spin_unlock_irqrestore(&bp->lock, flags);
1292 
1293 	return 0;
1294 }
1295 
1296 static void
1297 __ptp_ocp_adjtime_locked(struct ptp_ocp *bp, u32 adj_val)
1298 {
1299 	u32 select, ctrl;
1300 
1301 	select = ioread32(&bp->reg->select);
1302 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1303 
1304 	iowrite32(adj_val, &bp->reg->offset_ns);
1305 	iowrite32(NSEC_PER_SEC, &bp->reg->offset_window_ns);
1306 
1307 	ctrl = OCP_CTRL_ADJUST_OFFSET | OCP_CTRL_ENABLE;
1308 	iowrite32(ctrl, &bp->reg->ctrl);
1309 
1310 	/* restore clock selection */
1311 	iowrite32(select >> 16, &bp->reg->select);
1312 }
1313 
1314 static void
1315 ptp_ocp_adjtime_coarse(struct ptp_ocp *bp, s64 delta_ns)
1316 {
1317 	struct timespec64 ts;
1318 	unsigned long flags;
1319 	int err;
1320 
1321 	spin_lock_irqsave(&bp->lock, flags);
1322 	err = __ptp_ocp_gettime_locked(bp, &ts, NULL);
1323 	if (likely(!err)) {
1324 		set_normalized_timespec64(&ts, ts.tv_sec,
1325 					  ts.tv_nsec + delta_ns);
1326 		__ptp_ocp_settime_locked(bp, &ts);
1327 	}
1328 	spin_unlock_irqrestore(&bp->lock, flags);
1329 }
1330 
1331 static int
1332 ptp_ocp_adjtime(struct ptp_clock_info *ptp_info, s64 delta_ns)
1333 {
1334 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1335 	unsigned long flags;
1336 	u32 adj_ns, sign;
1337 
1338 	if (delta_ns > NSEC_PER_SEC || -delta_ns > NSEC_PER_SEC) {
1339 		ptp_ocp_adjtime_coarse(bp, delta_ns);
1340 		return 0;
1341 	}
1342 
1343 	sign = delta_ns < 0 ? BIT(31) : 0;
1344 	adj_ns = sign ? -delta_ns : delta_ns;
1345 
1346 	spin_lock_irqsave(&bp->lock, flags);
1347 	__ptp_ocp_adjtime_locked(bp, sign | adj_ns);
1348 	spin_unlock_irqrestore(&bp->lock, flags);
1349 
1350 	return 0;
1351 }
1352 
1353 static int
1354 ptp_ocp_null_adjfine(struct ptp_clock_info *ptp_info, long scaled_ppm)
1355 {
1356 	if (scaled_ppm == 0)
1357 		return 0;
1358 
1359 	return -EOPNOTSUPP;
1360 }
1361 
1362 static s32
1363 ptp_ocp_null_getmaxphase(struct ptp_clock_info *ptp_info)
1364 {
1365 	return 0;
1366 }
1367 
1368 static int
1369 ptp_ocp_null_adjphase(struct ptp_clock_info *ptp_info, s32 phase_ns)
1370 {
1371 	return -EOPNOTSUPP;
1372 }
1373 
1374 static int
1375 ptp_ocp_enable(struct ptp_clock_info *ptp_info, struct ptp_clock_request *rq,
1376 	       int on)
1377 {
1378 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1379 	struct ptp_ocp_ext_src *ext = NULL;
1380 	u32 req;
1381 	int err;
1382 
1383 	switch (rq->type) {
1384 	case PTP_CLK_REQ_EXTTS:
1385 		req = OCP_REQ_TIMESTAMP;
1386 		switch (rq->extts.index) {
1387 		case 0:
1388 			ext = bp->ts0;
1389 			break;
1390 		case 1:
1391 			ext = bp->ts1;
1392 			break;
1393 		case 2:
1394 			ext = bp->ts2;
1395 			break;
1396 		case 3:
1397 			ext = bp->ts3;
1398 			break;
1399 		case 4:
1400 			ext = bp->ts4;
1401 			break;
1402 		case 5:
1403 			ext = bp->pps;
1404 			break;
1405 		}
1406 		break;
1407 	case PTP_CLK_REQ_PPS:
1408 		req = OCP_REQ_PPS;
1409 		ext = bp->pps;
1410 		break;
1411 	case PTP_CLK_REQ_PEROUT:
1412 		switch (rq->perout.index) {
1413 		case 0:
1414 			/* This is a request for 1PPS on an output SMA.
1415 			 * Allow, but assume manual configuration.
1416 			 */
1417 			if (on && (rq->perout.period.sec != 1 ||
1418 				   rq->perout.period.nsec != 0))
1419 				return -EINVAL;
1420 			return 0;
1421 		case 1:
1422 		case 2:
1423 		case 3:
1424 		case 4:
1425 			req = rq->perout.index - 1;
1426 			ext = bp->signal_out[req];
1427 			err = ptp_ocp_signal_from_perout(bp, req, &rq->perout);
1428 			if (err)
1429 				return err;
1430 			break;
1431 		}
1432 		break;
1433 	default:
1434 		return -EOPNOTSUPP;
1435 	}
1436 
1437 	err = -ENXIO;
1438 	if (ext)
1439 		err = ext->info->enable(ext, req, on);
1440 
1441 	return err;
1442 }
1443 
1444 static int
1445 ptp_ocp_verify(struct ptp_clock_info *ptp_info, unsigned pin,
1446 	       enum ptp_pin_function func, unsigned chan)
1447 {
1448 	struct ptp_ocp *bp = container_of(ptp_info, struct ptp_ocp, ptp_info);
1449 	char buf[16];
1450 
1451 	switch (func) {
1452 	case PTP_PF_NONE:
1453 		snprintf(buf, sizeof(buf), "IN: None");
1454 		break;
1455 	case PTP_PF_EXTTS:
1456 		/* Allow timestamps, but require sysfs configuration. */
1457 		return 0;
1458 	case PTP_PF_PEROUT:
1459 		/* channel 0 is 1PPS from PHC.
1460 		 * channels 1..4 are the frequency generators.
1461 		 */
1462 		if (chan)
1463 			snprintf(buf, sizeof(buf), "OUT: GEN%d", chan);
1464 		else
1465 			snprintf(buf, sizeof(buf), "OUT: PHC");
1466 		break;
1467 	default:
1468 		return -EOPNOTSUPP;
1469 	}
1470 
1471 	return ptp_ocp_sma_store(bp, buf, pin + 1);
1472 }
1473 
1474 static const struct ptp_clock_info ptp_ocp_clock_info = {
1475 	.owner		= THIS_MODULE,
1476 	.name		= KBUILD_MODNAME,
1477 	.max_adj	= 100000000,
1478 	.gettimex64	= ptp_ocp_gettimex,
1479 	.settime64	= ptp_ocp_settime,
1480 	.adjtime	= ptp_ocp_adjtime,
1481 	.adjfine	= ptp_ocp_null_adjfine,
1482 	.adjphase	= ptp_ocp_null_adjphase,
1483 	.getmaxphase	= ptp_ocp_null_getmaxphase,
1484 	.enable		= ptp_ocp_enable,
1485 	.verify		= ptp_ocp_verify,
1486 	.pps		= true,
1487 	.n_ext_ts	= 6,
1488 	.n_per_out	= 5,
1489 	.supported_extts_flags = PTP_STRICT_FLAGS | PTP_RISING_EDGE,
1490 	.supported_perout_flags = PTP_PEROUT_DUTY_CYCLE | PTP_PEROUT_PHASE,
1491 };
1492 
1493 static void
1494 __ptp_ocp_clear_drift_locked(struct ptp_ocp *bp)
1495 {
1496 	u32 ctrl, select;
1497 
1498 	select = ioread32(&bp->reg->select);
1499 	iowrite32(OCP_SELECT_CLK_REG, &bp->reg->select);
1500 
1501 	iowrite32(0, &bp->reg->drift_ns);
1502 
1503 	ctrl = OCP_CTRL_ADJUST_DRIFT | OCP_CTRL_ENABLE;
1504 	iowrite32(ctrl, &bp->reg->ctrl);
1505 
1506 	/* restore clock selection */
1507 	iowrite32(select >> 16, &bp->reg->select);
1508 }
1509 
1510 static void
1511 ptp_ocp_utc_distribute(struct ptp_ocp *bp, u32 val)
1512 {
1513 	unsigned long flags;
1514 
1515 	spin_lock_irqsave(&bp->lock, flags);
1516 
1517 	bp->utc_tai_offset = val;
1518 
1519 	if (bp->irig_out)
1520 		iowrite32(val, &bp->irig_out->adj_sec);
1521 	if (bp->dcf_out)
1522 		iowrite32(val, &bp->dcf_out->adj_sec);
1523 	if (bp->nmea_out)
1524 		iowrite32(val, &bp->nmea_out->adj_sec);
1525 
1526 	spin_unlock_irqrestore(&bp->lock, flags);
1527 }
1528 
1529 static void
1530 ptp_ocp_watchdog(struct timer_list *t)
1531 {
1532 	struct ptp_ocp *bp = timer_container_of(bp, t, watchdog);
1533 	unsigned long flags;
1534 	u32 status, utc_offset;
1535 
1536 	status = ioread32(&bp->pps_to_clk->status);
1537 
1538 	if (status & PPS_STATUS_SUPERV_ERR) {
1539 		iowrite32(status, &bp->pps_to_clk->status);
1540 		if (!bp->gnss_lost) {
1541 			spin_lock_irqsave(&bp->lock, flags);
1542 			__ptp_ocp_clear_drift_locked(bp);
1543 			spin_unlock_irqrestore(&bp->lock, flags);
1544 			bp->gnss_lost = ktime_get_real_seconds();
1545 		}
1546 
1547 	} else if (bp->gnss_lost) {
1548 		bp->gnss_lost = 0;
1549 	}
1550 
1551 	/* if GNSS provides correct data we can rely on
1552 	 * it to get leap second information
1553 	 */
1554 	if (bp->tod) {
1555 		status = ioread32(&bp->tod->utc_status);
1556 		utc_offset = status & TOD_STATUS_UTC_MASK;
1557 		if (status & TOD_STATUS_UTC_VALID &&
1558 		    utc_offset != bp->utc_tai_offset)
1559 			ptp_ocp_utc_distribute(bp, utc_offset);
1560 	}
1561 
1562 	mod_timer(&bp->watchdog, jiffies + HZ);
1563 }
1564 
1565 static void
1566 ptp_ocp_estimate_pci_timing(struct ptp_ocp *bp)
1567 {
1568 	ktime_t start, end, delay = U64_MAX;
1569 	u32 ctrl;
1570 	int i;
1571 
1572 	for (i = 0; i < 3; i++) {
1573 		ctrl = ioread32(&bp->reg->ctrl);
1574 		ctrl = OCP_CTRL_READ_TIME_REQ | OCP_CTRL_ENABLE;
1575 
1576 		iowrite32(ctrl, &bp->reg->ctrl);
1577 
1578 		start = ktime_get_raw_ns();
1579 
1580 		ctrl = ioread32(&bp->reg->ctrl);
1581 
1582 		end = ktime_get_raw_ns();
1583 
1584 		delay = min(delay, end - start);
1585 	}
1586 	bp->ts_window_adjust = (delay >> 5) * 3;
1587 }
1588 
1589 static int
1590 ptp_ocp_init_clock(struct ptp_ocp *bp, struct ptp_ocp_servo_conf *servo_conf)
1591 {
1592 	struct timespec64 ts;
1593 	u32 ctrl;
1594 
1595 	ctrl = OCP_CTRL_ENABLE;
1596 	iowrite32(ctrl, &bp->reg->ctrl);
1597 
1598 	/* servo configuration */
1599 	iowrite32(servo_conf->servo_offset_p, &bp->reg->servo_offset_p);
1600 	iowrite32(servo_conf->servo_offset_i, &bp->reg->servo_offset_i);
1601 	iowrite32(servo_conf->servo_drift_p, &bp->reg->servo_drift_p);
1602 	iowrite32(servo_conf->servo_drift_p, &bp->reg->servo_drift_i);
1603 
1604 	/* latch servo values */
1605 	ctrl |= OCP_CTRL_ADJUST_SERVO;
1606 	iowrite32(ctrl, &bp->reg->ctrl);
1607 
1608 	if ((ioread32(&bp->reg->ctrl) & OCP_CTRL_ENABLE) == 0) {
1609 		dev_err(&bp->pdev->dev, "clock not enabled\n");
1610 		return -ENODEV;
1611 	}
1612 
1613 	ptp_ocp_estimate_pci_timing(bp);
1614 
1615 	bp->sync = ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC;
1616 	if (!bp->sync) {
1617 		ktime_get_clocktai_ts64(&ts);
1618 		ptp_ocp_settime(&bp->ptp_info, &ts);
1619 	}
1620 
1621 	/* If there is a clock supervisor, then enable the watchdog */
1622 	if (bp->pps_to_clk) {
1623 		timer_setup(&bp->watchdog, ptp_ocp_watchdog, 0);
1624 		mod_timer(&bp->watchdog, jiffies + HZ);
1625 	}
1626 
1627 	return 0;
1628 }
1629 
1630 static void
1631 ptp_ocp_tod_init(struct ptp_ocp *bp)
1632 {
1633 	u32 ctrl, reg;
1634 
1635 	ctrl = ioread32(&bp->tod->ctrl);
1636 	ctrl |= TOD_CTRL_PROTOCOL | TOD_CTRL_ENABLE;
1637 	ctrl &= ~(TOD_CTRL_DISABLE_FMT_A | TOD_CTRL_DISABLE_FMT_B);
1638 	iowrite32(ctrl, &bp->tod->ctrl);
1639 
1640 	reg = ioread32(&bp->tod->utc_status);
1641 	if (reg & TOD_STATUS_UTC_VALID)
1642 		ptp_ocp_utc_distribute(bp, reg & TOD_STATUS_UTC_MASK);
1643 }
1644 
1645 static const char *
1646 ptp_ocp_tod_proto_name(const int idx)
1647 {
1648 	static const char * const proto_name[] = {
1649 		"NMEA", "NMEA_ZDA", "NMEA_RMC", "NMEA_none",
1650 		"UBX", "UBX_UTC", "UBX_LS", "UBX_none"
1651 	};
1652 	return proto_name[idx];
1653 }
1654 
1655 static const char *
1656 ptp_ocp_tod_gnss_name(int idx)
1657 {
1658 	static const char * const gnss_name[] = {
1659 		"ALL", "COMBINED", "GPS", "GLONASS", "GALILEO", "BEIDOU",
1660 		"Unknown"
1661 	};
1662 	if (idx >= ARRAY_SIZE(gnss_name))
1663 		idx = ARRAY_SIZE(gnss_name) - 1;
1664 	return gnss_name[idx];
1665 }
1666 
1667 static const char *
1668 ptp_ocp_tty_port_name(int idx)
1669 {
1670 	static const char * const tty_name[] = {
1671 		"GNSS", "GNSS2", "MAC", "NMEA"
1672 	};
1673 	return tty_name[idx];
1674 }
1675 
1676 struct ptp_ocp_nvmem_match_info {
1677 	struct ptp_ocp *bp;
1678 	const void * const tag;
1679 };
1680 
1681 static int
1682 ptp_ocp_nvmem_match(struct device *dev, const void *data)
1683 {
1684 	const struct ptp_ocp_nvmem_match_info *info = data;
1685 
1686 	dev = dev->parent;
1687 	if (!i2c_verify_client(dev) || info->tag != dev->platform_data)
1688 		return 0;
1689 
1690 	while ((dev = dev->parent))
1691 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
1692 			return info->bp == dev_get_drvdata(dev);
1693 	return 0;
1694 }
1695 
1696 static inline struct nvmem_device *
1697 ptp_ocp_nvmem_device_get(struct ptp_ocp *bp, const void * const tag)
1698 {
1699 	struct ptp_ocp_nvmem_match_info info = { .bp = bp, .tag = tag };
1700 
1701 	return nvmem_device_find(&info, ptp_ocp_nvmem_match);
1702 }
1703 
1704 static inline void
1705 ptp_ocp_nvmem_device_put(struct nvmem_device **nvmemp)
1706 {
1707 	if (!IS_ERR_OR_NULL(*nvmemp))
1708 		nvmem_device_put(*nvmemp);
1709 	*nvmemp = NULL;
1710 }
1711 
1712 static void
1713 ptp_ocp_read_eeprom(struct ptp_ocp *bp)
1714 {
1715 	const struct ptp_ocp_eeprom_map *map;
1716 	struct nvmem_device *nvmem;
1717 	const void *tag;
1718 	int ret;
1719 
1720 	if (!bp->i2c_ctrl)
1721 		return;
1722 
1723 	tag = NULL;
1724 	nvmem = NULL;
1725 
1726 	for (map = bp->eeprom_map; map->len; map++) {
1727 		if (map->tag != tag) {
1728 			tag = map->tag;
1729 			ptp_ocp_nvmem_device_put(&nvmem);
1730 		}
1731 		if (!nvmem) {
1732 			nvmem = ptp_ocp_nvmem_device_get(bp, tag);
1733 			if (IS_ERR(nvmem)) {
1734 				ret = PTR_ERR(nvmem);
1735 				goto fail;
1736 			}
1737 		}
1738 		ret = nvmem_device_read(nvmem, map->off, map->len,
1739 					BP_MAP_ENTRY_ADDR(bp, map));
1740 		if (ret != map->len)
1741 			goto fail;
1742 	}
1743 
1744 	bp->has_eeprom_data = true;
1745 
1746 out:
1747 	ptp_ocp_nvmem_device_put(&nvmem);
1748 	return;
1749 
1750 fail:
1751 	dev_err(&bp->pdev->dev, "could not read eeprom: %d\n", ret);
1752 	goto out;
1753 }
1754 
1755 static struct device *
1756 ptp_ocp_find_flash(struct ptp_ocp *bp)
1757 {
1758 	struct device *dev, *last;
1759 
1760 	last = NULL;
1761 	dev = &bp->spi_flash->dev;
1762 
1763 	while ((dev = device_find_any_child(dev))) {
1764 		if (!strcmp("mtd", dev_bus_name(dev)))
1765 			break;
1766 		put_device(last);
1767 		last = dev;
1768 	}
1769 	put_device(last);
1770 
1771 	return dev;
1772 }
1773 
1774 static int
1775 ptp_ocp_devlink_fw_image(struct devlink *devlink, const struct firmware *fw,
1776 			 const u8 **data, size_t *size)
1777 {
1778 	struct ptp_ocp *bp = devlink_priv(devlink);
1779 	const struct ptp_ocp_firmware_header *hdr;
1780 	size_t offset, length;
1781 	u16 crc;
1782 
1783 	hdr = (const struct ptp_ocp_firmware_header *)fw->data;
1784 	if (memcmp(hdr->magic, OCP_FIRMWARE_MAGIC_HEADER, 4)) {
1785 		devlink_flash_update_status_notify(devlink,
1786 			"No firmware header found, cancel firmware upgrade",
1787 			NULL, 0, 0);
1788 		return -EINVAL;
1789 	}
1790 
1791 	if (be16_to_cpu(hdr->pci_vendor_id) != bp->pdev->vendor ||
1792 	    be16_to_cpu(hdr->pci_device_id) != bp->pdev->device) {
1793 		devlink_flash_update_status_notify(devlink,
1794 			"Firmware image compatibility check failed",
1795 			NULL, 0, 0);
1796 		return -EINVAL;
1797 	}
1798 
1799 	offset = sizeof(*hdr);
1800 	length = be32_to_cpu(hdr->image_size);
1801 	if (length != (fw->size - offset)) {
1802 		devlink_flash_update_status_notify(devlink,
1803 			"Firmware image size check failed",
1804 			NULL, 0, 0);
1805 		return -EINVAL;
1806 	}
1807 
1808 	crc = crc16(0xffff, &fw->data[offset], length);
1809 	if (be16_to_cpu(hdr->crc) != crc) {
1810 		devlink_flash_update_status_notify(devlink,
1811 			"Firmware image CRC check failed",
1812 			NULL, 0, 0);
1813 		return -EINVAL;
1814 	}
1815 
1816 	*data = &fw->data[offset];
1817 	*size = length;
1818 
1819 	return 0;
1820 }
1821 
1822 static int
1823 ptp_ocp_devlink_flash(struct devlink *devlink, struct device *dev,
1824 		      const struct firmware *fw)
1825 {
1826 	struct mtd_info *mtd = dev_get_drvdata(dev);
1827 	struct ptp_ocp *bp = devlink_priv(devlink);
1828 	size_t off, len, size, resid, wrote;
1829 	struct erase_info erase;
1830 	size_t base, blksz;
1831 	const u8 *data;
1832 	int err;
1833 
1834 	err = ptp_ocp_devlink_fw_image(devlink, fw, &data, &size);
1835 	if (err)
1836 		goto out;
1837 
1838 	off = 0;
1839 	base = bp->flash_start;
1840 	blksz = 4096;
1841 	resid = size;
1842 
1843 	while (resid) {
1844 		devlink_flash_update_status_notify(devlink, "Flashing",
1845 						   NULL, off, size);
1846 
1847 		len = min_t(size_t, resid, blksz);
1848 		erase.addr = base + off;
1849 		erase.len = blksz;
1850 
1851 		err = mtd_erase(mtd, &erase);
1852 		if (err)
1853 			goto out;
1854 
1855 		err = mtd_write(mtd, base + off, len, &wrote, data + off);
1856 		if (err)
1857 			goto out;
1858 
1859 		off += blksz;
1860 		resid -= len;
1861 	}
1862 out:
1863 	return err;
1864 }
1865 
1866 static int
1867 ptp_ocp_devlink_flash_update(struct devlink *devlink,
1868 			     struct devlink_flash_update_params *params,
1869 			     struct netlink_ext_ack *extack)
1870 {
1871 	struct ptp_ocp *bp = devlink_priv(devlink);
1872 	struct device *dev;
1873 	const char *msg;
1874 	int err;
1875 
1876 	dev = ptp_ocp_find_flash(bp);
1877 	if (!dev) {
1878 		dev_err(&bp->pdev->dev, "Can't find Flash SPI adapter\n");
1879 		return -ENODEV;
1880 	}
1881 
1882 	devlink_flash_update_status_notify(devlink, "Preparing to flash",
1883 					   NULL, 0, 0);
1884 
1885 	err = ptp_ocp_devlink_flash(devlink, dev, params->fw);
1886 
1887 	msg = err ? "Flash error" : "Flash complete";
1888 	devlink_flash_update_status_notify(devlink, msg, NULL, 0, 0);
1889 
1890 	put_device(dev);
1891 	return err;
1892 }
1893 
1894 static int
1895 ptp_ocp_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1896 			 struct netlink_ext_ack *extack)
1897 {
1898 	struct ptp_ocp *bp = devlink_priv(devlink);
1899 	const char *fw_image;
1900 	char buf[32];
1901 	int err;
1902 
1903 	fw_image = bp->fw_loader ? "loader" : "fw";
1904 	sprintf(buf, "%d.%d", bp->fw_tag, bp->fw_version);
1905 	err = devlink_info_version_running_put(req, fw_image, buf);
1906 	if (err)
1907 		return err;
1908 
1909 	if (!bp->has_eeprom_data) {
1910 		ptp_ocp_read_eeprom(bp);
1911 		if (!bp->has_eeprom_data)
1912 			return 0;
1913 	}
1914 
1915 	sprintf(buf, "%pM", bp->serial);
1916 	err = devlink_info_serial_number_put(req, buf);
1917 	if (err)
1918 		return err;
1919 
1920 	err = devlink_info_version_fixed_put(req,
1921 			DEVLINK_INFO_VERSION_GENERIC_BOARD_ID,
1922 			bp->board_id);
1923 	if (err)
1924 		return err;
1925 
1926 	return 0;
1927 }
1928 
1929 static const struct devlink_ops ptp_ocp_devlink_ops = {
1930 	.flash_update = ptp_ocp_devlink_flash_update,
1931 	.info_get = ptp_ocp_devlink_info_get,
1932 };
1933 
1934 static void __iomem *
1935 __ptp_ocp_get_mem(struct ptp_ocp *bp, resource_size_t start, int size)
1936 {
1937 	struct resource res = DEFINE_RES_MEM_NAMED(start, size, "ptp_ocp");
1938 
1939 	return devm_ioremap_resource(&bp->pdev->dev, &res);
1940 }
1941 
1942 static void __iomem *
1943 ptp_ocp_get_mem(struct ptp_ocp *bp, struct ocp_resource *r)
1944 {
1945 	resource_size_t start;
1946 
1947 	start = pci_resource_start(bp->pdev, 0) + r->offset;
1948 	return __ptp_ocp_get_mem(bp, start, r->size);
1949 }
1950 
1951 static int
1952 ptp_ocp_register_spi(struct ptp_ocp *bp, struct ocp_resource *r)
1953 {
1954 	struct ptp_ocp_flash_info *info;
1955 	struct pci_dev *pdev = bp->pdev;
1956 	struct platform_device *p;
1957 	struct resource res[2];
1958 	resource_size_t start;
1959 	int id;
1960 
1961 	start = pci_resource_start(pdev, 0) + r->offset;
1962 	res[0] = DEFINE_RES_MEM(start, r->size);
1963 	res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
1964 
1965 	info = r->extra;
1966 	id = pci_dev_id(pdev) << 1;
1967 	id += info->pci_offset;
1968 
1969 	p = platform_device_register_resndata(&pdev->dev, info->name, id,
1970 					      res, ARRAY_SIZE(res), info->data,
1971 					      info->data_size);
1972 	if (IS_ERR(p))
1973 		return PTR_ERR(p);
1974 
1975 	bp_assign_entry(bp, r, p);
1976 
1977 	return 0;
1978 }
1979 
1980 static struct platform_device *
1981 ptp_ocp_i2c_bus(struct pci_dev *pdev, struct ocp_resource *r, int id)
1982 {
1983 	struct ptp_ocp_i2c_info *info;
1984 	struct resource res[2];
1985 	resource_size_t start;
1986 
1987 	info = r->extra;
1988 	start = pci_resource_start(pdev, 0) + r->offset;
1989 	res[0] = DEFINE_RES_MEM(start, r->size);
1990 	res[1] = DEFINE_RES_IRQ(pci_irq_vector(pdev, r->irq_vec));
1991 
1992 	return platform_device_register_resndata(&pdev->dev, info->name,
1993 						 id, res, ARRAY_SIZE(res),
1994 						 info->data, info->data_size);
1995 }
1996 
1997 static int
1998 ptp_ocp_register_i2c(struct ptp_ocp *bp, struct ocp_resource *r)
1999 {
2000 	struct pci_dev *pdev = bp->pdev;
2001 	struct ptp_ocp_i2c_info *info;
2002 	struct platform_device *p;
2003 	struct clk_hw *clk;
2004 	char buf[32];
2005 	int id;
2006 
2007 	info = r->extra;
2008 	id = pci_dev_id(bp->pdev);
2009 
2010 	sprintf(buf, "AXI.%d", id);
2011 	clk = clk_hw_register_fixed_rate(&pdev->dev, buf, NULL, 0,
2012 					 info->fixed_rate);
2013 	if (IS_ERR(clk))
2014 		return PTR_ERR(clk);
2015 	bp->i2c_clk = clk;
2016 
2017 	sprintf(buf, "%s.%d", info->name, id);
2018 	devm_clk_hw_register_clkdev(&pdev->dev, clk, NULL, buf);
2019 	p = ptp_ocp_i2c_bus(bp->pdev, r, id);
2020 	if (IS_ERR(p))
2021 		return PTR_ERR(p);
2022 
2023 	bp_assign_entry(bp, r, p);
2024 
2025 	return 0;
2026 }
2027 
2028 /* The expectation is that this is triggered only on error. */
2029 static irqreturn_t
2030 ptp_ocp_signal_irq(int irq, void *priv)
2031 {
2032 	struct ptp_ocp_ext_src *ext = priv;
2033 	struct signal_reg __iomem *reg = ext->mem;
2034 	struct ptp_ocp *bp = ext->bp;
2035 	u32 enable, status;
2036 	int gen;
2037 
2038 	gen = ext->info->index - 1;
2039 
2040 	enable = ioread32(&reg->enable);
2041 	status = ioread32(&reg->status);
2042 
2043 	/* disable generator on error */
2044 	if (status || !enable) {
2045 		iowrite32(0, &reg->intr_mask);
2046 		iowrite32(0, &reg->enable);
2047 		bp->signal[gen].running = false;
2048 	}
2049 
2050 	iowrite32(0, &reg->intr);	/* ack interrupt */
2051 
2052 	return IRQ_HANDLED;
2053 }
2054 
2055 static int
2056 ptp_ocp_signal_set(struct ptp_ocp *bp, int gen, struct ptp_ocp_signal *s)
2057 {
2058 	struct ptp_system_timestamp sts;
2059 	struct timespec64 ts;
2060 	ktime_t start_ns;
2061 	int err;
2062 
2063 	if (!s->period)
2064 		return 0;
2065 
2066 	if (!s->pulse)
2067 		s->pulse = ktime_divns(s->period * s->duty, 100);
2068 
2069 	err = ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts);
2070 	if (err)
2071 		return err;
2072 
2073 	start_ns = ktime_set(ts.tv_sec, ts.tv_nsec) + NSEC_PER_MSEC;
2074 	if (!s->start) {
2075 		/* roundup() does not work on 32-bit systems */
2076 		s->start = DIV64_U64_ROUND_UP(start_ns, s->period);
2077 		s->start *= s->period;
2078 		s->start = ktime_add(s->start, s->phase);
2079 	}
2080 
2081 	if (s->duty < 1 || s->duty > 99)
2082 		return -EINVAL;
2083 
2084 	if (s->pulse < 1 || s->pulse > s->period)
2085 		return -EINVAL;
2086 
2087 	if (s->start < start_ns)
2088 		return -EINVAL;
2089 
2090 	bp->signal[gen] = *s;
2091 
2092 	return 0;
2093 }
2094 
2095 static int
2096 ptp_ocp_signal_from_perout(struct ptp_ocp *bp, int gen,
2097 			   struct ptp_perout_request *req)
2098 {
2099 	struct ptp_ocp_signal s = { };
2100 
2101 	s.polarity = bp->signal[gen].polarity;
2102 	s.period = ktime_set(req->period.sec, req->period.nsec);
2103 	if (!s.period)
2104 		return 0;
2105 
2106 	if (req->flags & PTP_PEROUT_DUTY_CYCLE) {
2107 		s.pulse = ktime_set(req->on.sec, req->on.nsec);
2108 		s.duty = ktime_divns(s.pulse * 100, s.period);
2109 	}
2110 
2111 	if (req->flags & PTP_PEROUT_PHASE)
2112 		s.phase = ktime_set(req->phase.sec, req->phase.nsec);
2113 	else
2114 		s.start = ktime_set(req->start.sec, req->start.nsec);
2115 
2116 	return ptp_ocp_signal_set(bp, gen, &s);
2117 }
2118 
2119 static int
2120 ptp_ocp_signal_enable(void *priv, u32 req, bool enable)
2121 {
2122 	struct ptp_ocp_ext_src *ext = priv;
2123 	struct signal_reg __iomem *reg = ext->mem;
2124 	struct ptp_ocp *bp = ext->bp;
2125 	struct timespec64 ts;
2126 	int gen;
2127 
2128 	gen = ext->info->index - 1;
2129 
2130 	iowrite32(0, &reg->intr_mask);
2131 	iowrite32(0, &reg->enable);
2132 	bp->signal[gen].running = false;
2133 	if (!enable)
2134 		return 0;
2135 
2136 	ts = ktime_to_timespec64(bp->signal[gen].start);
2137 	iowrite32(ts.tv_sec, &reg->start_sec);
2138 	iowrite32(ts.tv_nsec, &reg->start_ns);
2139 
2140 	ts = ktime_to_timespec64(bp->signal[gen].period);
2141 	iowrite32(ts.tv_sec, &reg->period_sec);
2142 	iowrite32(ts.tv_nsec, &reg->period_ns);
2143 
2144 	ts = ktime_to_timespec64(bp->signal[gen].pulse);
2145 	iowrite32(ts.tv_sec, &reg->pulse_sec);
2146 	iowrite32(ts.tv_nsec, &reg->pulse_ns);
2147 
2148 	iowrite32(bp->signal[gen].polarity, &reg->polarity);
2149 	iowrite32(0, &reg->repeat_count);
2150 
2151 	iowrite32(0, &reg->intr);		/* clear interrupt state */
2152 	iowrite32(1, &reg->intr_mask);		/* enable interrupt */
2153 	iowrite32(3, &reg->enable);		/* valid & enable */
2154 
2155 	bp->signal[gen].running = true;
2156 
2157 	return 0;
2158 }
2159 
2160 static irqreturn_t
2161 ptp_ocp_ts_irq(int irq, void *priv)
2162 {
2163 	struct ptp_ocp_ext_src *ext = priv;
2164 	struct ts_reg __iomem *reg = ext->mem;
2165 	struct ptp_clock_event ev;
2166 	u32 sec, nsec;
2167 
2168 	if (ext == ext->bp->pps) {
2169 		if (ext->bp->pps_req_map & OCP_REQ_PPS) {
2170 			ev.type = PTP_CLOCK_PPS;
2171 			ptp_clock_event(ext->bp->ptp, &ev);
2172 		}
2173 
2174 		if ((ext->bp->pps_req_map & ~OCP_REQ_PPS) == 0)
2175 			goto out;
2176 	}
2177 
2178 	/* XXX should fix API - this converts s/ns -> ts -> s/ns */
2179 	sec = ioread32(&reg->time_sec);
2180 	nsec = ioread32(&reg->time_ns);
2181 
2182 	ev.type = PTP_CLOCK_EXTTS;
2183 	ev.index = ext->info->index;
2184 	ev.timestamp = sec * NSEC_PER_SEC + nsec;
2185 
2186 	ptp_clock_event(ext->bp->ptp, &ev);
2187 
2188 out:
2189 	iowrite32(1, &reg->intr);	/* write 1 to ack */
2190 
2191 	return IRQ_HANDLED;
2192 }
2193 
2194 static int
2195 ptp_ocp_ts_enable(void *priv, u32 req, bool enable)
2196 {
2197 	struct ptp_ocp_ext_src *ext = priv;
2198 	struct ts_reg __iomem *reg = ext->mem;
2199 	struct ptp_ocp *bp = ext->bp;
2200 
2201 	if (ext == bp->pps) {
2202 		u32 old_map = bp->pps_req_map;
2203 
2204 		if (enable)
2205 			bp->pps_req_map |= req;
2206 		else
2207 			bp->pps_req_map &= ~req;
2208 
2209 		/* if no state change, just return */
2210 		if ((!!old_map ^ !!bp->pps_req_map) == 0)
2211 			return 0;
2212 	}
2213 
2214 	if (enable) {
2215 		iowrite32(1, &reg->enable);
2216 		iowrite32(1, &reg->intr_mask);
2217 		iowrite32(1, &reg->intr);
2218 	} else {
2219 		iowrite32(0, &reg->intr_mask);
2220 		iowrite32(0, &reg->enable);
2221 	}
2222 
2223 	return 0;
2224 }
2225 
2226 static void
2227 ptp_ocp_unregister_ext(struct ptp_ocp_ext_src *ext)
2228 {
2229 	if (!ext)
2230 		return;
2231 
2232 	ext->info->enable(ext, ~0, false);
2233 	pci_free_irq(ext->bp->pdev, ext->irq_vec, ext);
2234 	kfree(ext);
2235 }
2236 
2237 static int
2238 ptp_ocp_register_ext(struct ptp_ocp *bp, struct ocp_resource *r)
2239 {
2240 	struct pci_dev *pdev = bp->pdev;
2241 	struct ptp_ocp_ext_src *ext;
2242 	int err;
2243 
2244 	ext = kzalloc(sizeof(*ext), GFP_KERNEL);
2245 	if (!ext)
2246 		return -ENOMEM;
2247 
2248 	ext->mem = ptp_ocp_get_mem(bp, r);
2249 	if (IS_ERR(ext->mem)) {
2250 		err = PTR_ERR(ext->mem);
2251 		goto out;
2252 	}
2253 
2254 	ext->bp = bp;
2255 	ext->info = r->extra;
2256 	ext->irq_vec = r->irq_vec;
2257 
2258 	err = pci_request_irq(pdev, r->irq_vec, ext->info->irq_fcn, NULL,
2259 			      ext, "ocp%d.%s", bp->id, r->name);
2260 	if (err) {
2261 		dev_err(&pdev->dev, "Could not get irq %d\n", r->irq_vec);
2262 		goto out;
2263 	}
2264 
2265 	bp_assign_entry(bp, r, ext);
2266 
2267 	return 0;
2268 
2269 out:
2270 	kfree(ext);
2271 	return err;
2272 }
2273 
2274 static int
2275 ptp_ocp_serial_line(struct ptp_ocp *bp, struct ocp_resource *r)
2276 {
2277 	struct pci_dev *pdev = bp->pdev;
2278 	struct uart_8250_port uart;
2279 
2280 	/* Setting UPF_IOREMAP and leaving port.membase unspecified lets
2281 	 * the serial port device claim and release the pci resource.
2282 	 */
2283 	memset(&uart, 0, sizeof(uart));
2284 	uart.port.dev = &pdev->dev;
2285 	uart.port.iotype = UPIO_MEM;
2286 	uart.port.regshift = 2;
2287 	uart.port.mapbase = pci_resource_start(pdev, 0) + r->offset;
2288 	uart.port.irq = pci_irq_vector(pdev, r->irq_vec);
2289 	uart.port.uartclk = 50000000;
2290 	uart.port.flags = UPF_FIXED_TYPE | UPF_IOREMAP | UPF_NO_THRE_TEST;
2291 	uart.port.type = PORT_16550A;
2292 
2293 	return serial8250_register_8250_port(&uart);
2294 }
2295 
2296 static int
2297 ptp_ocp_register_serial(struct ptp_ocp *bp, struct ocp_resource *r)
2298 {
2299 	struct ptp_ocp_serial_port *p = (struct ptp_ocp_serial_port *)r->extra;
2300 	struct ptp_ocp_serial_port port = {};
2301 
2302 	port.line = ptp_ocp_serial_line(bp, r);
2303 	if (port.line < 0)
2304 		return port.line;
2305 
2306 	if (p)
2307 		port.baud = p->baud;
2308 
2309 	bp_assign_entry(bp, r, port);
2310 
2311 	return 0;
2312 }
2313 
2314 static int
2315 ptp_ocp_register_mem(struct ptp_ocp *bp, struct ocp_resource *r)
2316 {
2317 	void __iomem *mem;
2318 
2319 	mem = ptp_ocp_get_mem(bp, r);
2320 	if (IS_ERR(mem))
2321 		return PTR_ERR(mem);
2322 
2323 	bp_assign_entry(bp, r, mem);
2324 
2325 	return 0;
2326 }
2327 
2328 static void
2329 ptp_ocp_nmea_out_init(struct ptp_ocp *bp)
2330 {
2331 	if (!bp->nmea_out)
2332 		return;
2333 
2334 	iowrite32(0, &bp->nmea_out->ctrl);		/* disable */
2335 	iowrite32(7, &bp->nmea_out->uart_baud);		/* 115200 */
2336 	iowrite32(1, &bp->nmea_out->ctrl);		/* enable */
2337 }
2338 
2339 static void
2340 _ptp_ocp_signal_init(struct ptp_ocp_signal *s, struct signal_reg __iomem *reg)
2341 {
2342 	u32 val;
2343 
2344 	iowrite32(0, &reg->enable);		/* disable */
2345 
2346 	val = ioread32(&reg->polarity);
2347 	s->polarity = val ? true : false;
2348 	s->duty = 50;
2349 }
2350 
2351 static void
2352 ptp_ocp_signal_init(struct ptp_ocp *bp)
2353 {
2354 	int i;
2355 
2356 	for (i = 0; i < 4; i++)
2357 		if (bp->signal_out[i])
2358 			_ptp_ocp_signal_init(&bp->signal[i],
2359 					     bp->signal_out[i]->mem);
2360 }
2361 
2362 static void
2363 ptp_ocp_attr_group_del(struct ptp_ocp *bp)
2364 {
2365 	sysfs_remove_groups(&bp->dev.kobj, bp->attr_group);
2366 	kfree(bp->attr_group);
2367 }
2368 
2369 static int
2370 ptp_ocp_attr_group_add(struct ptp_ocp *bp,
2371 		       const struct ocp_attr_group *attr_tbl)
2372 {
2373 	int count, i;
2374 	int err;
2375 
2376 	count = 0;
2377 	for (i = 0; attr_tbl[i].cap; i++)
2378 		if (attr_tbl[i].cap & bp->fw_cap)
2379 			count++;
2380 
2381 	bp->attr_group = kcalloc(count + 1, sizeof(*bp->attr_group),
2382 				 GFP_KERNEL);
2383 	if (!bp->attr_group)
2384 		return -ENOMEM;
2385 
2386 	count = 0;
2387 	for (i = 0; attr_tbl[i].cap; i++)
2388 		if (attr_tbl[i].cap & bp->fw_cap)
2389 			bp->attr_group[count++] = attr_tbl[i].group;
2390 
2391 	err = sysfs_create_groups(&bp->dev.kobj, bp->attr_group);
2392 	if (err)
2393 		bp->attr_group[0] = NULL;
2394 
2395 	return err;
2396 }
2397 
2398 static void
2399 ptp_ocp_enable_fpga(u32 __iomem *reg, u32 bit, bool enable)
2400 {
2401 	u32 ctrl;
2402 	bool on;
2403 
2404 	ctrl = ioread32(reg);
2405 	on = ctrl & bit;
2406 	if (on ^ enable) {
2407 		ctrl &= ~bit;
2408 		ctrl |= enable ? bit : 0;
2409 		iowrite32(ctrl, reg);
2410 	}
2411 }
2412 
2413 static void
2414 ptp_ocp_irig_out(struct ptp_ocp *bp, bool enable)
2415 {
2416 	return ptp_ocp_enable_fpga(&bp->irig_out->ctrl,
2417 				   IRIG_M_CTRL_ENABLE, enable);
2418 }
2419 
2420 static void
2421 ptp_ocp_irig_in(struct ptp_ocp *bp, bool enable)
2422 {
2423 	return ptp_ocp_enable_fpga(&bp->irig_in->ctrl,
2424 				   IRIG_S_CTRL_ENABLE, enable);
2425 }
2426 
2427 static void
2428 ptp_ocp_dcf_out(struct ptp_ocp *bp, bool enable)
2429 {
2430 	return ptp_ocp_enable_fpga(&bp->dcf_out->ctrl,
2431 				   DCF_M_CTRL_ENABLE, enable);
2432 }
2433 
2434 static void
2435 ptp_ocp_dcf_in(struct ptp_ocp *bp, bool enable)
2436 {
2437 	return ptp_ocp_enable_fpga(&bp->dcf_in->ctrl,
2438 				   DCF_S_CTRL_ENABLE, enable);
2439 }
2440 
2441 static void
2442 __handle_signal_outputs(struct ptp_ocp *bp, u32 val)
2443 {
2444 	ptp_ocp_irig_out(bp, val & 0x00100010);
2445 	ptp_ocp_dcf_out(bp, val & 0x00200020);
2446 }
2447 
2448 static void
2449 __handle_signal_inputs(struct ptp_ocp *bp, u32 val)
2450 {
2451 	ptp_ocp_irig_in(bp, val & 0x00100010);
2452 	ptp_ocp_dcf_in(bp, val & 0x00200020);
2453 }
2454 
2455 static u32
2456 ptp_ocp_sma_fb_get(struct ptp_ocp *bp, int sma_nr)
2457 {
2458 	u32 __iomem *gpio;
2459 	u32 shift;
2460 
2461 	if (bp->sma[sma_nr - 1].fixed_fcn)
2462 		return (sma_nr - 1) & 1;
2463 
2464 	if (bp->sma[sma_nr - 1].mode == SMA_MODE_IN)
2465 		gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2466 	else
2467 		gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2468 	shift = sma_nr & 1 ? 0 : 16;
2469 
2470 	return (ioread32(gpio) >> shift) & 0xffff;
2471 }
2472 
2473 static int
2474 ptp_ocp_sma_fb_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2475 {
2476 	u32 reg, mask, shift;
2477 	unsigned long flags;
2478 	u32 __iomem *gpio;
2479 
2480 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2481 	shift = sma_nr & 1 ? 0 : 16;
2482 
2483 	mask = 0xffff << (16 - shift);
2484 
2485 	spin_lock_irqsave(&bp->lock, flags);
2486 
2487 	reg = ioread32(gpio);
2488 	reg = (reg & mask) | (val << shift);
2489 
2490 	__handle_signal_outputs(bp, reg);
2491 
2492 	iowrite32(reg, gpio);
2493 
2494 	spin_unlock_irqrestore(&bp->lock, flags);
2495 
2496 	return 0;
2497 }
2498 
2499 static int
2500 ptp_ocp_sma_fb_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2501 {
2502 	u32 reg, mask, shift;
2503 	unsigned long flags;
2504 	u32 __iomem *gpio;
2505 
2506 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2507 	shift = sma_nr & 1 ? 0 : 16;
2508 
2509 	mask = 0xffff << (16 - shift);
2510 
2511 	spin_lock_irqsave(&bp->lock, flags);
2512 
2513 	reg = ioread32(gpio);
2514 	reg = (reg & mask) | (val << shift);
2515 
2516 	__handle_signal_inputs(bp, reg);
2517 
2518 	iowrite32(reg, gpio);
2519 
2520 	spin_unlock_irqrestore(&bp->lock, flags);
2521 
2522 	return 0;
2523 }
2524 
2525 static void
2526 ptp_ocp_sma_fb_init(struct ptp_ocp *bp)
2527 {
2528 	struct dpll_pin_properties prop = {
2529 		.board_label = NULL,
2530 		.type = DPLL_PIN_TYPE_EXT,
2531 		.capabilities = DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE,
2532 		.freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
2533 		.freq_supported = ptp_ocp_sma_freq,
2534 
2535 	};
2536 	u32 reg;
2537 	int i;
2538 
2539 	/* defaults */
2540 	for (i = 0; i < OCP_SMA_NUM; i++) {
2541 		bp->sma[i].default_fcn = i & 1;
2542 		bp->sma[i].dpll_prop = prop;
2543 		bp->sma[i].dpll_prop.board_label =
2544 			bp->ptp_info.pin_config[i].name;
2545 	}
2546 	bp->sma[0].mode = SMA_MODE_IN;
2547 	bp->sma[1].mode = SMA_MODE_IN;
2548 	bp->sma[2].mode = SMA_MODE_OUT;
2549 	bp->sma[3].mode = SMA_MODE_OUT;
2550 	/* If no SMA1 map, the pin functions and directions are fixed. */
2551 	if (!bp->sma_map1) {
2552 		for (i = 0; i < OCP_SMA_NUM; i++) {
2553 			bp->sma[i].fixed_fcn = true;
2554 			bp->sma[i].fixed_dir = true;
2555 			bp->sma[i].dpll_prop.capabilities &=
2556 				~DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2557 		}
2558 		return;
2559 	}
2560 
2561 	/* If SMA2 GPIO output map is all 1, it is not present.
2562 	 * This indicates the firmware has fixed direction SMA pins.
2563 	 */
2564 	reg = ioread32(&bp->sma_map2->gpio2);
2565 	if (reg == 0xffffffff) {
2566 		for (i = 0; i < OCP_SMA_NUM; i++)
2567 			bp->sma[i].fixed_dir = true;
2568 	} else {
2569 		reg = ioread32(&bp->sma_map1->gpio1);
2570 		bp->sma[0].mode = reg & BIT(15) ? SMA_MODE_IN : SMA_MODE_OUT;
2571 		bp->sma[1].mode = reg & BIT(31) ? SMA_MODE_IN : SMA_MODE_OUT;
2572 
2573 		reg = ioread32(&bp->sma_map1->gpio2);
2574 		bp->sma[2].mode = reg & BIT(15) ? SMA_MODE_OUT : SMA_MODE_IN;
2575 		bp->sma[3].mode = reg & BIT(31) ? SMA_MODE_OUT : SMA_MODE_IN;
2576 	}
2577 }
2578 
2579 static const struct ocp_sma_op ocp_fb_sma_op = {
2580 	.tbl		= { ptp_ocp_sma_in, ptp_ocp_sma_out },
2581 	.init		= ptp_ocp_sma_fb_init,
2582 	.get		= ptp_ocp_sma_fb_get,
2583 	.set_inputs	= ptp_ocp_sma_fb_set_inputs,
2584 	.set_output	= ptp_ocp_sma_fb_set_output,
2585 };
2586 
2587 static int
2588 ptp_ocp_sma_adva_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
2589 {
2590 	u32 reg, mask, shift;
2591 	unsigned long flags;
2592 	u32 __iomem *gpio;
2593 
2594 	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
2595 	shift = sma_nr & 1 ? 0 : 16;
2596 
2597 	mask = 0xffff << (16 - shift);
2598 
2599 	spin_lock_irqsave(&bp->lock, flags);
2600 
2601 	reg = ioread32(gpio);
2602 	reg = (reg & mask) | (val << shift);
2603 
2604 	iowrite32(reg, gpio);
2605 
2606 	spin_unlock_irqrestore(&bp->lock, flags);
2607 
2608 	return 0;
2609 }
2610 
2611 static int
2612 ptp_ocp_sma_adva_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
2613 {
2614 	u32 reg, mask, shift;
2615 	unsigned long flags;
2616 	u32 __iomem *gpio;
2617 
2618 	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
2619 	shift = sma_nr & 1 ? 0 : 16;
2620 
2621 	mask = 0xffff << (16 - shift);
2622 
2623 	spin_lock_irqsave(&bp->lock, flags);
2624 
2625 	reg = ioread32(gpio);
2626 	reg = (reg & mask) | (val << shift);
2627 
2628 	iowrite32(reg, gpio);
2629 
2630 	spin_unlock_irqrestore(&bp->lock, flags);
2631 
2632 	return 0;
2633 }
2634 
2635 static const struct ocp_sma_op ocp_adva_sma_op = {
2636 	.tbl		= { ptp_ocp_adva_sma_in, ptp_ocp_adva_sma_out },
2637 	.init		= ptp_ocp_sma_fb_init,
2638 	.get		= ptp_ocp_sma_fb_get,
2639 	.set_inputs	= ptp_ocp_sma_adva_set_inputs,
2640 	.set_output	= ptp_ocp_sma_adva_set_output,
2641 };
2642 
2643 static int
2644 ptp_ocp_set_pins(struct ptp_ocp *bp)
2645 {
2646 	struct ptp_pin_desc *config;
2647 	int i;
2648 
2649 	config = kcalloc(4, sizeof(*config), GFP_KERNEL);
2650 	if (!config)
2651 		return -ENOMEM;
2652 
2653 	for (i = 0; i < 4; i++) {
2654 		sprintf(config[i].name, "sma%d", i + 1);
2655 		config[i].index = i;
2656 	}
2657 
2658 	bp->ptp_info.n_pins = 4;
2659 	bp->ptp_info.pin_config = config;
2660 
2661 	return 0;
2662 }
2663 
2664 static void
2665 ptp_ocp_fb_set_version(struct ptp_ocp *bp)
2666 {
2667 	u64 cap = OCP_CAP_BASIC;
2668 	u32 version;
2669 
2670 	version = ioread32(&bp->image->version);
2671 
2672 	/* if lower 16 bits are empty, this is the fw loader. */
2673 	if ((version & 0xffff) == 0) {
2674 		version = version >> 16;
2675 		bp->fw_loader = true;
2676 	}
2677 
2678 	bp->fw_tag = version >> 15;
2679 	bp->fw_version = version & 0x7fff;
2680 
2681 	if (bp->fw_tag) {
2682 		/* FPGA firmware */
2683 		if (version >= 5)
2684 			cap |= OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2685 	} else {
2686 		/* SOM firmware */
2687 		if (version >= 19)
2688 			cap |= OCP_CAP_SIGNAL;
2689 		if (version >= 20)
2690 			cap |= OCP_CAP_FREQ;
2691 	}
2692 
2693 	bp->fw_cap = cap;
2694 }
2695 
2696 /* FB specific board initializers; last "resource" registered. */
2697 static int
2698 ptp_ocp_fb_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2699 {
2700 	int err;
2701 
2702 	bp->flash_start = 1024 * 4096;
2703 	bp->eeprom_map = fb_eeprom_map;
2704 	bp->fw_version = ioread32(&bp->image->version);
2705 	bp->sma_op = &ocp_fb_sma_op;
2706 	bp->signals_nr = 4;
2707 	bp->freq_in_nr = 4;
2708 
2709 	ptp_ocp_fb_set_version(bp);
2710 
2711 	ptp_ocp_tod_init(bp);
2712 	ptp_ocp_nmea_out_init(bp);
2713 	ptp_ocp_signal_init(bp);
2714 
2715 	err = ptp_ocp_attr_group_add(bp, fb_timecard_groups);
2716 	if (err)
2717 		return err;
2718 
2719 	err = ptp_ocp_set_pins(bp);
2720 	if (err)
2721 		return err;
2722 	ptp_ocp_sma_init(bp);
2723 
2724 	return ptp_ocp_init_clock(bp, r->extra);
2725 }
2726 
2727 static bool
2728 ptp_ocp_allow_irq(struct ptp_ocp *bp, struct ocp_resource *r)
2729 {
2730 	bool allow = !r->irq_vec || r->irq_vec < bp->n_irqs;
2731 
2732 	if (!allow)
2733 		dev_err(&bp->pdev->dev, "irq %d out of range, skipping %s\n",
2734 			r->irq_vec, r->name);
2735 	return allow;
2736 }
2737 
2738 static int
2739 ptp_ocp_register_resources(struct ptp_ocp *bp, kernel_ulong_t driver_data)
2740 {
2741 	struct ocp_resource *r, *table;
2742 	int err = 0;
2743 
2744 	table = (struct ocp_resource *)driver_data;
2745 	for (r = table; r->setup; r++) {
2746 		if (!ptp_ocp_allow_irq(bp, r))
2747 			continue;
2748 		err = r->setup(bp, r);
2749 		if (err) {
2750 			dev_err(&bp->pdev->dev,
2751 				"Could not register %s: err %d\n",
2752 				r->name, err);
2753 			break;
2754 		}
2755 	}
2756 	return err;
2757 }
2758 
2759 static void
2760 ptp_ocp_art_sma_init(struct ptp_ocp *bp)
2761 {
2762 	struct dpll_pin_properties prop = {
2763 		.board_label = NULL,
2764 		.type = DPLL_PIN_TYPE_EXT,
2765 		.capabilities = 0,
2766 		.freq_supported_num = ARRAY_SIZE(ptp_ocp_sma_freq),
2767 		.freq_supported = ptp_ocp_sma_freq,
2768 
2769 	};
2770 	u32 reg;
2771 	int i;
2772 
2773 	/* defaults */
2774 	bp->sma[0].mode = SMA_MODE_IN;
2775 	bp->sma[1].mode = SMA_MODE_IN;
2776 	bp->sma[2].mode = SMA_MODE_OUT;
2777 	bp->sma[3].mode = SMA_MODE_OUT;
2778 
2779 	bp->sma[0].default_fcn = 0x08;	/* IN: 10Mhz */
2780 	bp->sma[1].default_fcn = 0x01;	/* IN: PPS1 */
2781 	bp->sma[2].default_fcn = 0x10;	/* OUT: 10Mhz */
2782 	bp->sma[3].default_fcn = 0x02;	/* OUT: PHC */
2783 
2784 	for (i = 0; i < OCP_SMA_NUM; i++) {
2785 		/* If no SMA map, the pin functions and directions are fixed. */
2786 		bp->sma[i].dpll_prop = prop;
2787 		bp->sma[i].dpll_prop.board_label =
2788 			bp->ptp_info.pin_config[i].name;
2789 		if (!bp->art_sma) {
2790 			bp->sma[i].fixed_fcn = true;
2791 			bp->sma[i].fixed_dir = true;
2792 			continue;
2793 		}
2794 		reg = ioread32(&bp->art_sma->map[i].gpio);
2795 
2796 		switch (reg & 0xff) {
2797 		case 0:
2798 			bp->sma[i].fixed_fcn = true;
2799 			bp->sma[i].fixed_dir = true;
2800 			break;
2801 		case 1:
2802 		case 8:
2803 			bp->sma[i].mode = SMA_MODE_IN;
2804 			bp->sma[i].dpll_prop.capabilities =
2805 				DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2806 			break;
2807 		default:
2808 			bp->sma[i].mode = SMA_MODE_OUT;
2809 			bp->sma[i].dpll_prop.capabilities =
2810 				DPLL_PIN_CAPABILITIES_DIRECTION_CAN_CHANGE;
2811 			break;
2812 		}
2813 	}
2814 }
2815 
2816 static u32
2817 ptp_ocp_art_sma_get(struct ptp_ocp *bp, int sma_nr)
2818 {
2819 	if (bp->sma[sma_nr - 1].fixed_fcn)
2820 		return bp->sma[sma_nr - 1].default_fcn;
2821 
2822 	return ioread32(&bp->art_sma->map[sma_nr - 1].gpio) & 0xff;
2823 }
2824 
2825 /* note: store 0 is considered invalid. */
2826 static int
2827 ptp_ocp_art_sma_set(struct ptp_ocp *bp, int sma_nr, u32 val)
2828 {
2829 	unsigned long flags;
2830 	u32 __iomem *gpio;
2831 	int err = 0;
2832 	u32 reg;
2833 
2834 	val &= SMA_SELECT_MASK;
2835 	if (hweight32(val) > 1)
2836 		return -EINVAL;
2837 
2838 	gpio = &bp->art_sma->map[sma_nr - 1].gpio;
2839 
2840 	spin_lock_irqsave(&bp->lock, flags);
2841 	reg = ioread32(gpio);
2842 	if (((reg >> 16) & val) == 0) {
2843 		err = -EOPNOTSUPP;
2844 	} else {
2845 		reg = (reg & 0xff00) | (val & 0xff);
2846 		iowrite32(reg, gpio);
2847 	}
2848 	spin_unlock_irqrestore(&bp->lock, flags);
2849 
2850 	return err;
2851 }
2852 
2853 static const struct ocp_sma_op ocp_art_sma_op = {
2854 	.tbl		= { ptp_ocp_art_sma_in, ptp_ocp_art_sma_out },
2855 	.init		= ptp_ocp_art_sma_init,
2856 	.get		= ptp_ocp_art_sma_get,
2857 	.set_inputs	= ptp_ocp_art_sma_set,
2858 	.set_output	= ptp_ocp_art_sma_set,
2859 };
2860 
2861 /* ART specific board initializers; last "resource" registered. */
2862 static int
2863 ptp_ocp_art_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2864 {
2865 	int err;
2866 
2867 	bp->flash_start = 0x1000000;
2868 	bp->eeprom_map = art_eeprom_map;
2869 	bp->fw_cap = OCP_CAP_BASIC;
2870 	bp->fw_version = ioread32(&bp->reg->version);
2871 	bp->fw_tag = 2;
2872 	bp->sma_op = &ocp_art_sma_op;
2873 	bp->signals_nr = 4;
2874 	bp->freq_in_nr = 4;
2875 
2876 	/* Enable MAC serial port during initialisation */
2877 	iowrite32(1, &bp->board_config->mro50_serial_activate);
2878 
2879 	err = ptp_ocp_set_pins(bp);
2880 	if (err)
2881 		return err;
2882 	ptp_ocp_sma_init(bp);
2883 
2884 	err = ptp_ocp_attr_group_add(bp, art_timecard_groups);
2885 	if (err)
2886 		return err;
2887 
2888 	return ptp_ocp_init_clock(bp, r->extra);
2889 }
2890 
2891 /* ADVA specific board initializers; last "resource" registered. */
2892 static int
2893 ptp_ocp_adva_board_init(struct ptp_ocp *bp, struct ocp_resource *r)
2894 {
2895 	int err;
2896 	u32 version;
2897 
2898 	bp->flash_start = 0xA00000;
2899 	bp->eeprom_map = fb_eeprom_map;
2900 	bp->sma_op = &ocp_adva_sma_op;
2901 	bp->signals_nr = 2;
2902 	bp->freq_in_nr = 2;
2903 
2904 	version = ioread32(&bp->image->version);
2905 	/* if lower 16 bits are empty, this is the fw loader. */
2906 	if ((version & 0xffff) == 0) {
2907 		version = version >> 16;
2908 		bp->fw_loader = true;
2909 	}
2910 	bp->fw_tag = 3;
2911 	bp->fw_version = version & 0xffff;
2912 	bp->fw_cap = OCP_CAP_BASIC | OCP_CAP_SIGNAL | OCP_CAP_FREQ;
2913 
2914 	ptp_ocp_tod_init(bp);
2915 	ptp_ocp_nmea_out_init(bp);
2916 	ptp_ocp_signal_init(bp);
2917 
2918 	err = ptp_ocp_attr_group_add(bp, adva_timecard_groups);
2919 	if (err)
2920 		return err;
2921 
2922 	err = ptp_ocp_set_pins(bp);
2923 	if (err)
2924 		return err;
2925 	ptp_ocp_sma_init(bp);
2926 
2927 	return ptp_ocp_init_clock(bp, r->extra);
2928 }
2929 
2930 static ssize_t
2931 ptp_ocp_show_output(const struct ocp_selector *tbl, u32 val, char *buf,
2932 		    int def_val)
2933 {
2934 	const char *name;
2935 	ssize_t count;
2936 
2937 	count = sysfs_emit(buf, "OUT: ");
2938 	name = ptp_ocp_select_name_from_val(tbl, val);
2939 	if (!name)
2940 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2941 	count += sysfs_emit_at(buf, count, "%s\n", name);
2942 	return count;
2943 }
2944 
2945 static ssize_t
2946 ptp_ocp_show_inputs(const struct ocp_selector *tbl, u32 val, char *buf,
2947 		    int def_val)
2948 {
2949 	const char *name;
2950 	ssize_t count;
2951 	int i;
2952 
2953 	count = sysfs_emit(buf, "IN: ");
2954 	for (i = 0; tbl[i].name; i++) {
2955 		if (val & tbl[i].value) {
2956 			name = tbl[i].name;
2957 			count += sysfs_emit_at(buf, count, "%s ", name);
2958 		}
2959 	}
2960 	if (!val && def_val >= 0) {
2961 		name = ptp_ocp_select_name_from_val(tbl, def_val);
2962 		count += sysfs_emit_at(buf, count, "%s ", name);
2963 	}
2964 	if (count)
2965 		count--;
2966 	count += sysfs_emit_at(buf, count, "\n");
2967 	return count;
2968 }
2969 
2970 static int
2971 sma_parse_inputs(const struct ocp_selector * const tbl[], const char *buf,
2972 		 enum ptp_ocp_sma_mode *mode)
2973 {
2974 	int idx, count, dir;
2975 	char **argv;
2976 	int ret;
2977 
2978 	argv = argv_split(GFP_KERNEL, buf, &count);
2979 	if (!argv)
2980 		return -ENOMEM;
2981 
2982 	ret = -EINVAL;
2983 	if (!count)
2984 		goto out;
2985 
2986 	idx = 0;
2987 	dir = *mode == SMA_MODE_IN ? 0 : 1;
2988 	if (!strcasecmp("IN:", argv[0])) {
2989 		dir = 0;
2990 		idx++;
2991 	}
2992 	if (!strcasecmp("OUT:", argv[0])) {
2993 		dir = 1;
2994 		idx++;
2995 	}
2996 	*mode = dir == 0 ? SMA_MODE_IN : SMA_MODE_OUT;
2997 
2998 	ret = 0;
2999 	for (; idx < count; idx++)
3000 		ret |= ptp_ocp_select_val_from_name(tbl[dir], argv[idx]);
3001 	if (ret < 0)
3002 		ret = -EINVAL;
3003 
3004 out:
3005 	argv_free(argv);
3006 	return ret;
3007 }
3008 
3009 static ssize_t
3010 ptp_ocp_sma_show(struct ptp_ocp *bp, int sma_nr, char *buf,
3011 		 int default_in_val, int default_out_val)
3012 {
3013 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
3014 	const struct ocp_selector * const *tbl;
3015 	u32 val;
3016 
3017 	tbl = bp->sma_op->tbl;
3018 	val = ptp_ocp_sma_get(bp, sma_nr) & SMA_SELECT_MASK;
3019 
3020 	if (sma->mode == SMA_MODE_IN) {
3021 		if (sma->disabled)
3022 			val = SMA_DISABLE;
3023 		return ptp_ocp_show_inputs(tbl[0], val, buf, default_in_val);
3024 	}
3025 
3026 	return ptp_ocp_show_output(tbl[1], val, buf, default_out_val);
3027 }
3028 
3029 static ssize_t
3030 sma1_show(struct device *dev, struct device_attribute *attr, char *buf)
3031 {
3032 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3033 
3034 	return ptp_ocp_sma_show(bp, 1, buf, 0, 1);
3035 }
3036 
3037 static ssize_t
3038 sma2_show(struct device *dev, struct device_attribute *attr, char *buf)
3039 {
3040 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3041 
3042 	return ptp_ocp_sma_show(bp, 2, buf, -1, 1);
3043 }
3044 
3045 static ssize_t
3046 sma3_show(struct device *dev, struct device_attribute *attr, char *buf)
3047 {
3048 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3049 
3050 	return ptp_ocp_sma_show(bp, 3, buf, -1, 0);
3051 }
3052 
3053 static ssize_t
3054 sma4_show(struct device *dev, struct device_attribute *attr, char *buf)
3055 {
3056 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3057 
3058 	return ptp_ocp_sma_show(bp, 4, buf, -1, 1);
3059 }
3060 
3061 static int
3062 ptp_ocp_sma_store_val(struct ptp_ocp *bp, int val, enum ptp_ocp_sma_mode mode, int sma_nr)
3063 {
3064 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
3065 
3066 	if (sma->fixed_dir && (mode != sma->mode || val & SMA_DISABLE))
3067 		return -EOPNOTSUPP;
3068 
3069 	if (sma->fixed_fcn) {
3070 		if (val != sma->default_fcn)
3071 			return -EOPNOTSUPP;
3072 		return 0;
3073 	}
3074 
3075 	sma->disabled = !!(val & SMA_DISABLE);
3076 
3077 	if (mode != sma->mode) {
3078 		if (mode == SMA_MODE_IN)
3079 			ptp_ocp_sma_set_output(bp, sma_nr, 0);
3080 		else
3081 			ptp_ocp_sma_set_inputs(bp, sma_nr, 0);
3082 		sma->mode = mode;
3083 	}
3084 
3085 	if (!sma->fixed_dir)
3086 		val |= SMA_ENABLE;		/* add enable bit */
3087 
3088 	if (sma->disabled)
3089 		val = 0;
3090 
3091 	if (mode == SMA_MODE_IN)
3092 		val = ptp_ocp_sma_set_inputs(bp, sma_nr, val);
3093 	else
3094 		val = ptp_ocp_sma_set_output(bp, sma_nr, val);
3095 
3096 	return val;
3097 }
3098 
3099 static int
3100 ptp_ocp_sma_store(struct ptp_ocp *bp, const char *buf, int sma_nr)
3101 {
3102 	struct ptp_ocp_sma_connector *sma = &bp->sma[sma_nr - 1];
3103 	enum ptp_ocp_sma_mode mode;
3104 	int val;
3105 
3106 	mode = sma->mode;
3107 	val = sma_parse_inputs(bp->sma_op->tbl, buf, &mode);
3108 	if (val < 0)
3109 		return val;
3110 	return ptp_ocp_sma_store_val(bp, val, mode, sma_nr);
3111 }
3112 
3113 static ssize_t
3114 sma1_store(struct device *dev, struct device_attribute *attr,
3115 	   const char *buf, size_t count)
3116 {
3117 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3118 	int err;
3119 
3120 	err = ptp_ocp_sma_store(bp, buf, 1);
3121 	return err ? err : count;
3122 }
3123 
3124 static ssize_t
3125 sma2_store(struct device *dev, struct device_attribute *attr,
3126 	   const char *buf, size_t count)
3127 {
3128 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3129 	int err;
3130 
3131 	err = ptp_ocp_sma_store(bp, buf, 2);
3132 	return err ? err : count;
3133 }
3134 
3135 static ssize_t
3136 sma3_store(struct device *dev, struct device_attribute *attr,
3137 	   const char *buf, size_t count)
3138 {
3139 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3140 	int err;
3141 
3142 	err = ptp_ocp_sma_store(bp, buf, 3);
3143 	return err ? err : count;
3144 }
3145 
3146 static ssize_t
3147 sma4_store(struct device *dev, struct device_attribute *attr,
3148 	   const char *buf, size_t count)
3149 {
3150 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3151 	int err;
3152 
3153 	err = ptp_ocp_sma_store(bp, buf, 4);
3154 	return err ? err : count;
3155 }
3156 static DEVICE_ATTR_RW(sma1);
3157 static DEVICE_ATTR_RW(sma2);
3158 static DEVICE_ATTR_RW(sma3);
3159 static DEVICE_ATTR_RW(sma4);
3160 
3161 static ssize_t
3162 available_sma_inputs_show(struct device *dev,
3163 			  struct device_attribute *attr, char *buf)
3164 {
3165 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3166 
3167 	return ptp_ocp_select_table_show(bp->sma_op->tbl[0], buf);
3168 }
3169 static DEVICE_ATTR_RO(available_sma_inputs);
3170 
3171 static ssize_t
3172 available_sma_outputs_show(struct device *dev,
3173 			   struct device_attribute *attr, char *buf)
3174 {
3175 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3176 
3177 	return ptp_ocp_select_table_show(bp->sma_op->tbl[1], buf);
3178 }
3179 static DEVICE_ATTR_RO(available_sma_outputs);
3180 
3181 #define EXT_ATTR_RO(_group, _name, _val)				\
3182 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
3183 		{ __ATTR_RO(_name), (void *)_val }
3184 #define EXT_ATTR_RW(_group, _name, _val)				\
3185 	struct dev_ext_attribute dev_attr_##_group##_val##_##_name =	\
3186 		{ __ATTR_RW(_name), (void *)_val }
3187 #define to_ext_attr(x) container_of(x, struct dev_ext_attribute, attr)
3188 
3189 /* period [duty [phase [polarity]]] */
3190 static ssize_t
3191 signal_store(struct device *dev, struct device_attribute *attr,
3192 	     const char *buf, size_t count)
3193 {
3194 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3195 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3196 	struct ptp_ocp_signal s = { };
3197 	int gen = (uintptr_t)ea->var;
3198 	int argc, err;
3199 	char **argv;
3200 
3201 	argv = argv_split(GFP_KERNEL, buf, &argc);
3202 	if (!argv)
3203 		return -ENOMEM;
3204 
3205 	err = -EINVAL;
3206 	s.duty = bp->signal[gen].duty;
3207 	s.phase = bp->signal[gen].phase;
3208 	s.period = bp->signal[gen].period;
3209 	s.polarity = bp->signal[gen].polarity;
3210 
3211 	switch (argc) {
3212 	case 4:
3213 		argc--;
3214 		err = kstrtobool(argv[argc], &s.polarity);
3215 		if (err)
3216 			goto out;
3217 		fallthrough;
3218 	case 3:
3219 		argc--;
3220 		err = kstrtou64(argv[argc], 0, &s.phase);
3221 		if (err)
3222 			goto out;
3223 		fallthrough;
3224 	case 2:
3225 		argc--;
3226 		err = kstrtoint(argv[argc], 0, &s.duty);
3227 		if (err)
3228 			goto out;
3229 		fallthrough;
3230 	case 1:
3231 		argc--;
3232 		err = kstrtou64(argv[argc], 0, &s.period);
3233 		if (err)
3234 			goto out;
3235 		break;
3236 	default:
3237 		goto out;
3238 	}
3239 
3240 	err = ptp_ocp_signal_set(bp, gen, &s);
3241 	if (err)
3242 		goto out;
3243 
3244 	err = ptp_ocp_signal_enable(bp->signal_out[gen], gen, s.period != 0);
3245 
3246 out:
3247 	argv_free(argv);
3248 	return err ? err : count;
3249 }
3250 
3251 static ssize_t
3252 signal_show(struct device *dev, struct device_attribute *attr, char *buf)
3253 {
3254 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3255 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3256 	struct ptp_ocp_signal *signal;
3257 	int gen = (uintptr_t)ea->var;
3258 	struct timespec64 ts;
3259 
3260 	signal = &bp->signal[gen];
3261 
3262 	ts = ktime_to_timespec64(signal->start);
3263 
3264 	return sysfs_emit(buf, "%llu %d %llu %d %ptT TAI\n",
3265 			  signal->period, signal->duty, signal->phase, signal->polarity,
3266 			  &ts.tv_sec);
3267 }
3268 static EXT_ATTR_RW(signal, signal, 0);
3269 static EXT_ATTR_RW(signal, signal, 1);
3270 static EXT_ATTR_RW(signal, signal, 2);
3271 static EXT_ATTR_RW(signal, signal, 3);
3272 
3273 static ssize_t
3274 duty_show(struct device *dev, struct device_attribute *attr, char *buf)
3275 {
3276 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3277 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3278 	int i = (uintptr_t)ea->var;
3279 
3280 	return sysfs_emit(buf, "%d\n", bp->signal[i].duty);
3281 }
3282 static EXT_ATTR_RO(signal, duty, 0);
3283 static EXT_ATTR_RO(signal, duty, 1);
3284 static EXT_ATTR_RO(signal, duty, 2);
3285 static EXT_ATTR_RO(signal, duty, 3);
3286 
3287 static ssize_t
3288 period_show(struct device *dev, struct device_attribute *attr, char *buf)
3289 {
3290 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3291 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3292 	int i = (uintptr_t)ea->var;
3293 
3294 	return sysfs_emit(buf, "%llu\n", bp->signal[i].period);
3295 }
3296 static EXT_ATTR_RO(signal, period, 0);
3297 static EXT_ATTR_RO(signal, period, 1);
3298 static EXT_ATTR_RO(signal, period, 2);
3299 static EXT_ATTR_RO(signal, period, 3);
3300 
3301 static ssize_t
3302 phase_show(struct device *dev, struct device_attribute *attr, char *buf)
3303 {
3304 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3305 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3306 	int i = (uintptr_t)ea->var;
3307 
3308 	return sysfs_emit(buf, "%llu\n", bp->signal[i].phase);
3309 }
3310 static EXT_ATTR_RO(signal, phase, 0);
3311 static EXT_ATTR_RO(signal, phase, 1);
3312 static EXT_ATTR_RO(signal, phase, 2);
3313 static EXT_ATTR_RO(signal, phase, 3);
3314 
3315 static ssize_t
3316 polarity_show(struct device *dev, struct device_attribute *attr,
3317 	      char *buf)
3318 {
3319 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3320 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3321 	int i = (uintptr_t)ea->var;
3322 
3323 	return sysfs_emit(buf, "%d\n", bp->signal[i].polarity);
3324 }
3325 static EXT_ATTR_RO(signal, polarity, 0);
3326 static EXT_ATTR_RO(signal, polarity, 1);
3327 static EXT_ATTR_RO(signal, polarity, 2);
3328 static EXT_ATTR_RO(signal, polarity, 3);
3329 
3330 static ssize_t
3331 running_show(struct device *dev, struct device_attribute *attr, char *buf)
3332 {
3333 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3334 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3335 	int i = (uintptr_t)ea->var;
3336 
3337 	return sysfs_emit(buf, "%d\n", bp->signal[i].running);
3338 }
3339 static EXT_ATTR_RO(signal, running, 0);
3340 static EXT_ATTR_RO(signal, running, 1);
3341 static EXT_ATTR_RO(signal, running, 2);
3342 static EXT_ATTR_RO(signal, running, 3);
3343 
3344 static ssize_t
3345 start_show(struct device *dev, struct device_attribute *attr, char *buf)
3346 {
3347 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3348 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3349 	int i = (uintptr_t)ea->var;
3350 	struct timespec64 ts;
3351 
3352 	ts = ktime_to_timespec64(bp->signal[i].start);
3353 	return sysfs_emit(buf, "%llu.%lu\n", ts.tv_sec, ts.tv_nsec);
3354 }
3355 static EXT_ATTR_RO(signal, start, 0);
3356 static EXT_ATTR_RO(signal, start, 1);
3357 static EXT_ATTR_RO(signal, start, 2);
3358 static EXT_ATTR_RO(signal, start, 3);
3359 
3360 static ssize_t
3361 seconds_store(struct device *dev, struct device_attribute *attr,
3362 	      const char *buf, size_t count)
3363 {
3364 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3365 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3366 	int idx = (uintptr_t)ea->var;
3367 	u32 val;
3368 	int err;
3369 
3370 	err = kstrtou32(buf, 0, &val);
3371 	if (err)
3372 		return err;
3373 	if (val > 0xff)
3374 		return -EINVAL;
3375 
3376 	if (val)
3377 		val = (val << 8) | 0x1;
3378 
3379 	iowrite32(val, &bp->freq_in[idx]->ctrl);
3380 
3381 	return count;
3382 }
3383 
3384 static ssize_t
3385 seconds_show(struct device *dev, struct device_attribute *attr, char *buf)
3386 {
3387 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3388 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3389 	int idx = (uintptr_t)ea->var;
3390 	u32 val;
3391 
3392 	val = ioread32(&bp->freq_in[idx]->ctrl);
3393 	if (val & 1)
3394 		val = (val >> 8) & 0xff;
3395 	else
3396 		val = 0;
3397 
3398 	return sysfs_emit(buf, "%u\n", val);
3399 }
3400 static EXT_ATTR_RW(freq, seconds, 0);
3401 static EXT_ATTR_RW(freq, seconds, 1);
3402 static EXT_ATTR_RW(freq, seconds, 2);
3403 static EXT_ATTR_RW(freq, seconds, 3);
3404 
3405 static ssize_t
3406 frequency_show(struct device *dev, struct device_attribute *attr, char *buf)
3407 {
3408 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3409 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3410 	int idx = (uintptr_t)ea->var;
3411 	u32 val;
3412 
3413 	val = ioread32(&bp->freq_in[idx]->status);
3414 	if (val & FREQ_STATUS_ERROR)
3415 		return sysfs_emit(buf, "error\n");
3416 	if (val & FREQ_STATUS_OVERRUN)
3417 		return sysfs_emit(buf, "overrun\n");
3418 	if (val & FREQ_STATUS_VALID)
3419 		return sysfs_emit(buf, "%lu\n", val & FREQ_STATUS_MASK);
3420 	return 0;
3421 }
3422 static EXT_ATTR_RO(freq, frequency, 0);
3423 static EXT_ATTR_RO(freq, frequency, 1);
3424 static EXT_ATTR_RO(freq, frequency, 2);
3425 static EXT_ATTR_RO(freq, frequency, 3);
3426 
3427 static ssize_t
3428 ptp_ocp_tty_show(struct device *dev, struct device_attribute *attr, char *buf)
3429 {
3430 	struct dev_ext_attribute *ea = to_ext_attr(attr);
3431 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3432 
3433 	/*
3434 	 * NOTE: This output does not include a trailing newline for backward
3435 	 * compatibility. Existing userspace software uses this value directly
3436 	 * as a device path (e.g., "/dev/ttyS4"), and adding a newline would
3437 	 * break those applications. Do not add a newline to this output.
3438 	 */
3439 	return sysfs_emit(buf, "ttyS%d", bp->port[(uintptr_t)ea->var].line);
3440 }
3441 
3442 static umode_t
3443 ptp_ocp_timecard_tty_is_visible(struct kobject *kobj, struct attribute *attr, int n)
3444 {
3445 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3446 	struct ptp_ocp_serial_port *port;
3447 	struct device_attribute *dattr;
3448 	struct dev_ext_attribute *ea;
3449 
3450 	if (strncmp(attr->name, "tty", 3))
3451 		return attr->mode;
3452 
3453 	dattr = container_of(attr, struct device_attribute, attr);
3454 	ea = container_of(dattr, struct dev_ext_attribute, attr);
3455 	port = &bp->port[(uintptr_t)ea->var];
3456 	return port->line == -1 ? 0 : 0444;
3457 }
3458 
3459 #define EXT_TTY_ATTR_RO(_name, _val)			\
3460 	struct dev_ext_attribute dev_attr_tty##_name =	\
3461 		{ __ATTR(tty##_name, 0444, ptp_ocp_tty_show, NULL), (void *)_val }
3462 
3463 static EXT_TTY_ATTR_RO(GNSS, PORT_GNSS);
3464 static EXT_TTY_ATTR_RO(GNSS2, PORT_GNSS2);
3465 static EXT_TTY_ATTR_RO(MAC, PORT_MAC);
3466 static EXT_TTY_ATTR_RO(NMEA, PORT_NMEA);
3467 static struct attribute *ptp_ocp_timecard_tty_attrs[] = {
3468 	&dev_attr_ttyGNSS.attr.attr,
3469 	&dev_attr_ttyGNSS2.attr.attr,
3470 	&dev_attr_ttyMAC.attr.attr,
3471 	&dev_attr_ttyNMEA.attr.attr,
3472 	NULL,
3473 };
3474 
3475 static const struct attribute_group ptp_ocp_timecard_tty_group = {
3476 	.name = "tty",
3477 	.attrs = ptp_ocp_timecard_tty_attrs,
3478 	.is_visible = ptp_ocp_timecard_tty_is_visible,
3479 };
3480 
3481 static ssize_t
3482 serialnum_show(struct device *dev, struct device_attribute *attr, char *buf)
3483 {
3484 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3485 
3486 	if (!bp->has_eeprom_data)
3487 		ptp_ocp_read_eeprom(bp);
3488 
3489 	return sysfs_emit(buf, "%pM\n", bp->serial);
3490 }
3491 static DEVICE_ATTR_RO(serialnum);
3492 
3493 static ssize_t
3494 gnss_sync_show(struct device *dev, struct device_attribute *attr, char *buf)
3495 {
3496 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3497 	ssize_t ret;
3498 
3499 	if (bp->gnss_lost)
3500 		ret = sysfs_emit(buf, "LOST @ %ptT\n", &bp->gnss_lost);
3501 	else
3502 		ret = sysfs_emit(buf, "SYNC\n");
3503 
3504 	return ret;
3505 }
3506 static DEVICE_ATTR_RO(gnss_sync);
3507 
3508 static ssize_t
3509 utc_tai_offset_show(struct device *dev,
3510 		    struct device_attribute *attr, char *buf)
3511 {
3512 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3513 
3514 	return sysfs_emit(buf, "%d\n", bp->utc_tai_offset);
3515 }
3516 
3517 static ssize_t
3518 utc_tai_offset_store(struct device *dev,
3519 		     struct device_attribute *attr,
3520 		     const char *buf, size_t count)
3521 {
3522 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3523 	int err;
3524 	u32 val;
3525 
3526 	err = kstrtou32(buf, 0, &val);
3527 	if (err)
3528 		return err;
3529 
3530 	ptp_ocp_utc_distribute(bp, val);
3531 
3532 	return count;
3533 }
3534 static DEVICE_ATTR_RW(utc_tai_offset);
3535 
3536 static ssize_t
3537 ts_window_adjust_show(struct device *dev,
3538 		      struct device_attribute *attr, char *buf)
3539 {
3540 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3541 
3542 	return sysfs_emit(buf, "%d\n", bp->ts_window_adjust);
3543 }
3544 
3545 static ssize_t
3546 ts_window_adjust_store(struct device *dev,
3547 		       struct device_attribute *attr,
3548 		       const char *buf, size_t count)
3549 {
3550 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3551 	int err;
3552 	u32 val;
3553 
3554 	err = kstrtou32(buf, 0, &val);
3555 	if (err)
3556 		return err;
3557 
3558 	bp->ts_window_adjust = val;
3559 
3560 	return count;
3561 }
3562 static DEVICE_ATTR_RW(ts_window_adjust);
3563 
3564 static ssize_t
3565 irig_b_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
3566 {
3567 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3568 	u32 val;
3569 
3570 	val = ioread32(&bp->irig_out->ctrl);
3571 	val = (val >> 16) & 0x07;
3572 	return sysfs_emit(buf, "%d\n", val);
3573 }
3574 
3575 static ssize_t
3576 irig_b_mode_store(struct device *dev,
3577 		  struct device_attribute *attr,
3578 		  const char *buf, size_t count)
3579 {
3580 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3581 	unsigned long flags;
3582 	int err;
3583 	u32 reg;
3584 	u8 val;
3585 
3586 	err = kstrtou8(buf, 0, &val);
3587 	if (err)
3588 		return err;
3589 	if (val > 7)
3590 		return -EINVAL;
3591 
3592 	reg = ((val & 0x7) << 16);
3593 
3594 	spin_lock_irqsave(&bp->lock, flags);
3595 	iowrite32(0, &bp->irig_out->ctrl);		/* disable */
3596 	iowrite32(reg, &bp->irig_out->ctrl);		/* change mode */
3597 	iowrite32(reg | IRIG_M_CTRL_ENABLE, &bp->irig_out->ctrl);
3598 	spin_unlock_irqrestore(&bp->lock, flags);
3599 
3600 	return count;
3601 }
3602 static DEVICE_ATTR_RW(irig_b_mode);
3603 
3604 static ssize_t
3605 clock_source_show(struct device *dev, struct device_attribute *attr, char *buf)
3606 {
3607 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3608 	const char *p;
3609 	u32 select;
3610 
3611 	select = ioread32(&bp->reg->select);
3612 	p = ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16);
3613 
3614 	return sysfs_emit(buf, "%s\n", p);
3615 }
3616 
3617 static ssize_t
3618 clock_source_store(struct device *dev, struct device_attribute *attr,
3619 		   const char *buf, size_t count)
3620 {
3621 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3622 	unsigned long flags;
3623 	int val;
3624 
3625 	val = ptp_ocp_select_val_from_name(ptp_ocp_clock, buf);
3626 	if (val < 0)
3627 		return val;
3628 
3629 	spin_lock_irqsave(&bp->lock, flags);
3630 	iowrite32(val, &bp->reg->select);
3631 	spin_unlock_irqrestore(&bp->lock, flags);
3632 
3633 	return count;
3634 }
3635 static DEVICE_ATTR_RW(clock_source);
3636 
3637 static ssize_t
3638 available_clock_sources_show(struct device *dev,
3639 			     struct device_attribute *attr, char *buf)
3640 {
3641 	return ptp_ocp_select_table_show(ptp_ocp_clock, buf);
3642 }
3643 static DEVICE_ATTR_RO(available_clock_sources);
3644 
3645 static ssize_t
3646 clock_status_drift_show(struct device *dev,
3647 			struct device_attribute *attr, char *buf)
3648 {
3649 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3650 	u32 val;
3651 	int res;
3652 
3653 	val = ioread32(&bp->reg->status_drift);
3654 	res = (val & ~INT_MAX) ? -1 : 1;
3655 	res *= (val & INT_MAX);
3656 	return sysfs_emit(buf, "%d\n", res);
3657 }
3658 static DEVICE_ATTR_RO(clock_status_drift);
3659 
3660 static ssize_t
3661 clock_status_offset_show(struct device *dev,
3662 			 struct device_attribute *attr, char *buf)
3663 {
3664 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3665 	u32 val;
3666 	int res;
3667 
3668 	val = ioread32(&bp->reg->status_offset);
3669 	res = (val & ~INT_MAX) ? -1 : 1;
3670 	res *= (val & INT_MAX);
3671 	return sysfs_emit(buf, "%d\n", res);
3672 }
3673 static DEVICE_ATTR_RO(clock_status_offset);
3674 
3675 static ssize_t
3676 tod_correction_show(struct device *dev,
3677 		    struct device_attribute *attr, char *buf)
3678 {
3679 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3680 	u32 val;
3681 	int res;
3682 
3683 	val = ioread32(&bp->tod->adj_sec);
3684 	res = (val & ~INT_MAX) ? -1 : 1;
3685 	res *= (val & INT_MAX);
3686 	return sysfs_emit(buf, "%d\n", res);
3687 }
3688 
3689 static ssize_t
3690 tod_correction_store(struct device *dev, struct device_attribute *attr,
3691 		     const char *buf, size_t count)
3692 {
3693 	struct ptp_ocp *bp = dev_get_drvdata(dev);
3694 	unsigned long flags;
3695 	int err, res;
3696 	u32 val = 0;
3697 
3698 	err = kstrtos32(buf, 0, &res);
3699 	if (err)
3700 		return err;
3701 	if (res < 0) {
3702 		res *= -1;
3703 		val |= BIT(31);
3704 	}
3705 	val |= res;
3706 
3707 	spin_lock_irqsave(&bp->lock, flags);
3708 	iowrite32(val, &bp->tod->adj_sec);
3709 	spin_unlock_irqrestore(&bp->lock, flags);
3710 
3711 	return count;
3712 }
3713 static DEVICE_ATTR_RW(tod_correction);
3714 
3715 #define _DEVICE_SIGNAL_GROUP_ATTRS(_nr)					\
3716 	static struct attribute *fb_timecard_signal##_nr##_attrs[] = {	\
3717 		&dev_attr_signal##_nr##_signal.attr.attr,		\
3718 		&dev_attr_signal##_nr##_duty.attr.attr,			\
3719 		&dev_attr_signal##_nr##_phase.attr.attr,		\
3720 		&dev_attr_signal##_nr##_period.attr.attr,		\
3721 		&dev_attr_signal##_nr##_polarity.attr.attr,		\
3722 		&dev_attr_signal##_nr##_running.attr.attr,		\
3723 		&dev_attr_signal##_nr##_start.attr.attr,		\
3724 		NULL,							\
3725 	}
3726 
3727 #define DEVICE_SIGNAL_GROUP(_name, _nr)					\
3728 	_DEVICE_SIGNAL_GROUP_ATTRS(_nr);				\
3729 	static const struct attribute_group				\
3730 			fb_timecard_signal##_nr##_group = {		\
3731 		.name = #_name,						\
3732 		.attrs = fb_timecard_signal##_nr##_attrs,		\
3733 }
3734 
3735 DEVICE_SIGNAL_GROUP(gen1, 0);
3736 DEVICE_SIGNAL_GROUP(gen2, 1);
3737 DEVICE_SIGNAL_GROUP(gen3, 2);
3738 DEVICE_SIGNAL_GROUP(gen4, 3);
3739 
3740 #define _DEVICE_FREQ_GROUP_ATTRS(_nr)					\
3741 	static struct attribute *fb_timecard_freq##_nr##_attrs[] = {	\
3742 		&dev_attr_freq##_nr##_seconds.attr.attr,		\
3743 		&dev_attr_freq##_nr##_frequency.attr.attr,		\
3744 		NULL,							\
3745 	}
3746 
3747 #define DEVICE_FREQ_GROUP(_name, _nr)					\
3748 	_DEVICE_FREQ_GROUP_ATTRS(_nr);					\
3749 	static const struct attribute_group				\
3750 			fb_timecard_freq##_nr##_group = {		\
3751 		.name = #_name,						\
3752 		.attrs = fb_timecard_freq##_nr##_attrs,			\
3753 }
3754 
3755 DEVICE_FREQ_GROUP(freq1, 0);
3756 DEVICE_FREQ_GROUP(freq2, 1);
3757 DEVICE_FREQ_GROUP(freq3, 2);
3758 DEVICE_FREQ_GROUP(freq4, 3);
3759 
3760 static ssize_t
3761 disciplining_config_read(struct file *filp, struct kobject *kobj,
3762 			 const struct bin_attribute *bin_attr, char *buf,
3763 			 loff_t off, size_t count)
3764 {
3765 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3766 	size_t size = OCP_ART_CONFIG_SIZE;
3767 	struct nvmem_device *nvmem;
3768 	ssize_t err;
3769 
3770 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3771 	if (IS_ERR(nvmem))
3772 		return PTR_ERR(nvmem);
3773 
3774 	if (off > size) {
3775 		err = 0;
3776 		goto out;
3777 	}
3778 
3779 	if (off + count > size)
3780 		count = size - off;
3781 
3782 	// the configuration is in the very beginning of the EEPROM
3783 	err = nvmem_device_read(nvmem, off, count, buf);
3784 	if (err != count) {
3785 		err = -EFAULT;
3786 		goto out;
3787 	}
3788 
3789 out:
3790 	ptp_ocp_nvmem_device_put(&nvmem);
3791 
3792 	return err;
3793 }
3794 
3795 static ssize_t
3796 disciplining_config_write(struct file *filp, struct kobject *kobj,
3797 			  const struct bin_attribute *bin_attr, char *buf,
3798 			  loff_t off, size_t count)
3799 {
3800 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3801 	struct nvmem_device *nvmem;
3802 	ssize_t err;
3803 
3804 	/* Allow write of the whole area only */
3805 	if (off || count != OCP_ART_CONFIG_SIZE)
3806 		return -EFAULT;
3807 
3808 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3809 	if (IS_ERR(nvmem))
3810 		return PTR_ERR(nvmem);
3811 
3812 	err = nvmem_device_write(nvmem, 0x00, count, buf);
3813 	if (err != count)
3814 		err = -EFAULT;
3815 
3816 	ptp_ocp_nvmem_device_put(&nvmem);
3817 
3818 	return err;
3819 }
3820 static const BIN_ATTR_RW(disciplining_config, OCP_ART_CONFIG_SIZE);
3821 
3822 static ssize_t
3823 temperature_table_read(struct file *filp, struct kobject *kobj,
3824 		       const struct bin_attribute *bin_attr, char *buf,
3825 		       loff_t off, size_t count)
3826 {
3827 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3828 	size_t size = OCP_ART_TEMP_TABLE_SIZE;
3829 	struct nvmem_device *nvmem;
3830 	ssize_t err;
3831 
3832 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3833 	if (IS_ERR(nvmem))
3834 		return PTR_ERR(nvmem);
3835 
3836 	if (off > size) {
3837 		err = 0;
3838 		goto out;
3839 	}
3840 
3841 	if (off + count > size)
3842 		count = size - off;
3843 
3844 	// the configuration is in the very beginning of the EEPROM
3845 	err = nvmem_device_read(nvmem, 0x90 + off, count, buf);
3846 	if (err != count) {
3847 		err = -EFAULT;
3848 		goto out;
3849 	}
3850 
3851 out:
3852 	ptp_ocp_nvmem_device_put(&nvmem);
3853 
3854 	return err;
3855 }
3856 
3857 static ssize_t
3858 temperature_table_write(struct file *filp, struct kobject *kobj,
3859 			const struct bin_attribute *bin_attr, char *buf,
3860 			loff_t off, size_t count)
3861 {
3862 	struct ptp_ocp *bp = dev_get_drvdata(kobj_to_dev(kobj));
3863 	struct nvmem_device *nvmem;
3864 	ssize_t err;
3865 
3866 	/* Allow write of the whole area only */
3867 	if (off || count != OCP_ART_TEMP_TABLE_SIZE)
3868 		return -EFAULT;
3869 
3870 	nvmem = ptp_ocp_nvmem_device_get(bp, NULL);
3871 	if (IS_ERR(nvmem))
3872 		return PTR_ERR(nvmem);
3873 
3874 	err = nvmem_device_write(nvmem, 0x90, count, buf);
3875 	if (err != count)
3876 		err = -EFAULT;
3877 
3878 	ptp_ocp_nvmem_device_put(&nvmem);
3879 
3880 	return err;
3881 }
3882 static const BIN_ATTR_RW(temperature_table, OCP_ART_TEMP_TABLE_SIZE);
3883 
3884 static struct attribute *fb_timecard_attrs[] = {
3885 	&dev_attr_serialnum.attr,
3886 	&dev_attr_gnss_sync.attr,
3887 	&dev_attr_clock_source.attr,
3888 	&dev_attr_available_clock_sources.attr,
3889 	&dev_attr_sma1.attr,
3890 	&dev_attr_sma2.attr,
3891 	&dev_attr_sma3.attr,
3892 	&dev_attr_sma4.attr,
3893 	&dev_attr_available_sma_inputs.attr,
3894 	&dev_attr_available_sma_outputs.attr,
3895 	&dev_attr_clock_status_drift.attr,
3896 	&dev_attr_clock_status_offset.attr,
3897 	&dev_attr_irig_b_mode.attr,
3898 	&dev_attr_utc_tai_offset.attr,
3899 	&dev_attr_ts_window_adjust.attr,
3900 	&dev_attr_tod_correction.attr,
3901 	NULL,
3902 };
3903 
3904 static const struct attribute_group fb_timecard_group = {
3905 	.attrs = fb_timecard_attrs,
3906 };
3907 
3908 static const struct ocp_attr_group fb_timecard_groups[] = {
3909 	{ .cap = OCP_CAP_BASIC,	    .group = &fb_timecard_group },
3910 	{ .cap = OCP_CAP_BASIC,	    .group = &ptp_ocp_timecard_tty_group },
3911 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
3912 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
3913 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal2_group },
3914 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal3_group },
3915 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
3916 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
3917 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq2_group },
3918 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq3_group },
3919 	{ },
3920 };
3921 
3922 static struct attribute *art_timecard_attrs[] = {
3923 	&dev_attr_serialnum.attr,
3924 	&dev_attr_clock_source.attr,
3925 	&dev_attr_available_clock_sources.attr,
3926 	&dev_attr_utc_tai_offset.attr,
3927 	&dev_attr_ts_window_adjust.attr,
3928 	&dev_attr_sma1.attr,
3929 	&dev_attr_sma2.attr,
3930 	&dev_attr_sma3.attr,
3931 	&dev_attr_sma4.attr,
3932 	&dev_attr_available_sma_inputs.attr,
3933 	&dev_attr_available_sma_outputs.attr,
3934 	NULL,
3935 };
3936 
3937 static const struct bin_attribute *const bin_art_timecard_attrs[] = {
3938 	&bin_attr_disciplining_config,
3939 	&bin_attr_temperature_table,
3940 	NULL,
3941 };
3942 
3943 static const struct attribute_group art_timecard_group = {
3944 	.attrs = art_timecard_attrs,
3945 	.bin_attrs = bin_art_timecard_attrs,
3946 };
3947 
3948 static const struct ocp_attr_group art_timecard_groups[] = {
3949 	{ .cap = OCP_CAP_BASIC,	    .group = &art_timecard_group },
3950 	{ .cap = OCP_CAP_BASIC,	    .group = &ptp_ocp_timecard_tty_group },
3951 	{ },
3952 };
3953 
3954 static struct attribute *adva_timecard_attrs[] = {
3955 	&dev_attr_serialnum.attr,
3956 	&dev_attr_gnss_sync.attr,
3957 	&dev_attr_clock_source.attr,
3958 	&dev_attr_available_clock_sources.attr,
3959 	&dev_attr_sma1.attr,
3960 	&dev_attr_sma2.attr,
3961 	&dev_attr_sma3.attr,
3962 	&dev_attr_sma4.attr,
3963 	&dev_attr_available_sma_inputs.attr,
3964 	&dev_attr_available_sma_outputs.attr,
3965 	&dev_attr_clock_status_drift.attr,
3966 	&dev_attr_clock_status_offset.attr,
3967 	&dev_attr_ts_window_adjust.attr,
3968 	&dev_attr_tod_correction.attr,
3969 	NULL,
3970 };
3971 
3972 static const struct attribute_group adva_timecard_group = {
3973 	.attrs = adva_timecard_attrs,
3974 };
3975 
3976 static const struct ocp_attr_group adva_timecard_groups[] = {
3977 	{ .cap = OCP_CAP_BASIC,	    .group = &adva_timecard_group },
3978 	{ .cap = OCP_CAP_BASIC,	    .group = &ptp_ocp_timecard_tty_group },
3979 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal0_group },
3980 	{ .cap = OCP_CAP_SIGNAL,    .group = &fb_timecard_signal1_group },
3981 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq0_group },
3982 	{ .cap = OCP_CAP_FREQ,	    .group = &fb_timecard_freq1_group },
3983 	{ },
3984 };
3985 
3986 static void
3987 gpio_input_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit,
3988 	       const char *def)
3989 {
3990 	int i;
3991 
3992 	for (i = 0; i < 4; i++) {
3993 		if (bp->sma[i].mode != SMA_MODE_IN)
3994 			continue;
3995 		if (map[i][0] & (1 << bit)) {
3996 			sprintf(buf, "sma%d", i + 1);
3997 			return;
3998 		}
3999 	}
4000 	if (!def)
4001 		def = "----";
4002 	strcpy(buf, def);
4003 }
4004 
4005 static void
4006 gpio_output_map(char *buf, struct ptp_ocp *bp, u16 map[][2], u16 bit)
4007 {
4008 	char *ans = buf;
4009 	int i;
4010 
4011 	strcpy(ans, "----");
4012 	for (i = 0; i < 4; i++) {
4013 		if (bp->sma[i].mode != SMA_MODE_OUT)
4014 			continue;
4015 		if (map[i][1] & (1 << bit))
4016 			ans += sprintf(ans, "sma%d ", i + 1);
4017 	}
4018 }
4019 
4020 static void
4021 _signal_summary_show(struct seq_file *s, struct ptp_ocp *bp, int nr)
4022 {
4023 	struct signal_reg __iomem *reg = bp->signal_out[nr]->mem;
4024 	struct ptp_ocp_signal *signal = &bp->signal[nr];
4025 	char label[16];
4026 	bool on;
4027 	u32 val;
4028 
4029 	on = signal->running;
4030 	sprintf(label, "GEN%d", nr + 1);
4031 	seq_printf(s, "%7s: %s, period:%llu duty:%d%% phase:%llu pol:%d",
4032 		   label, on ? " ON" : "OFF",
4033 		   signal->period, signal->duty, signal->phase,
4034 		   signal->polarity);
4035 
4036 	val = ioread32(&reg->enable);
4037 	seq_printf(s, " [%x", val);
4038 	val = ioread32(&reg->status);
4039 	seq_printf(s, " %x]", val);
4040 
4041 	seq_printf(s, " start:%llu\n", signal->start);
4042 }
4043 
4044 static void
4045 _frequency_summary_show(struct seq_file *s, int nr,
4046 			struct frequency_reg __iomem *reg)
4047 {
4048 	char label[16];
4049 	bool on;
4050 	u32 val;
4051 
4052 	if (!reg)
4053 		return;
4054 
4055 	sprintf(label, "FREQ%d", nr + 1);
4056 	val = ioread32(&reg->ctrl);
4057 	on = val & 1;
4058 	val = (val >> 8) & 0xff;
4059 	seq_printf(s, "%7s: %s, sec:%u",
4060 		   label,
4061 		   on ? " ON" : "OFF",
4062 		   val);
4063 
4064 	val = ioread32(&reg->status);
4065 	if (val & FREQ_STATUS_ERROR)
4066 		seq_printf(s, ", error");
4067 	if (val & FREQ_STATUS_OVERRUN)
4068 		seq_printf(s, ", overrun");
4069 	if (val & FREQ_STATUS_VALID)
4070 		seq_printf(s, ", freq %lu Hz", val & FREQ_STATUS_MASK);
4071 	seq_printf(s, "  reg:%x\n", val);
4072 }
4073 
4074 static int
4075 ptp_ocp_summary_show(struct seq_file *s, void *data)
4076 {
4077 	struct device *dev = s->private;
4078 	struct ptp_system_timestamp sts;
4079 	struct ts_reg __iomem *ts_reg;
4080 	char *buf, *src, *mac_src;
4081 	struct timespec64 ts;
4082 	struct ptp_ocp *bp;
4083 	u16 sma_val[4][2];
4084 	u32 ctrl, val;
4085 	bool on, map;
4086 	int i;
4087 
4088 	buf = (char *)__get_free_page(GFP_KERNEL);
4089 	if (!buf)
4090 		return -ENOMEM;
4091 
4092 	bp = dev_get_drvdata(dev);
4093 
4094 	seq_printf(s, "%7s: /dev/ptp%d\n", "PTP", ptp_clock_index(bp->ptp));
4095 	for (i = 0; i < __PORT_COUNT; i++) {
4096 		if (bp->port[i].line != -1)
4097 			seq_printf(s, "%7s: /dev/ttyS%d\n", ptp_ocp_tty_port_name(i),
4098 				   bp->port[i].line);
4099 	}
4100 
4101 	memset(sma_val, 0xff, sizeof(sma_val));
4102 	if (bp->sma_map1) {
4103 		u32 reg;
4104 
4105 		reg = ioread32(&bp->sma_map1->gpio1);
4106 		sma_val[0][0] = reg & 0xffff;
4107 		sma_val[1][0] = reg >> 16;
4108 
4109 		reg = ioread32(&bp->sma_map1->gpio2);
4110 		sma_val[2][1] = reg & 0xffff;
4111 		sma_val[3][1] = reg >> 16;
4112 
4113 		reg = ioread32(&bp->sma_map2->gpio1);
4114 		sma_val[2][0] = reg & 0xffff;
4115 		sma_val[3][0] = reg >> 16;
4116 
4117 		reg = ioread32(&bp->sma_map2->gpio2);
4118 		sma_val[0][1] = reg & 0xffff;
4119 		sma_val[1][1] = reg >> 16;
4120 	}
4121 
4122 	sma1_show(dev, NULL, buf);
4123 	seq_printf(s, "   sma1: %04x,%04x %s",
4124 		   sma_val[0][0], sma_val[0][1], buf);
4125 
4126 	sma2_show(dev, NULL, buf);
4127 	seq_printf(s, "   sma2: %04x,%04x %s",
4128 		   sma_val[1][0], sma_val[1][1], buf);
4129 
4130 	sma3_show(dev, NULL, buf);
4131 	seq_printf(s, "   sma3: %04x,%04x %s",
4132 		   sma_val[2][0], sma_val[2][1], buf);
4133 
4134 	sma4_show(dev, NULL, buf);
4135 	seq_printf(s, "   sma4: %04x,%04x %s",
4136 		   sma_val[3][0], sma_val[3][1], buf);
4137 
4138 	if (bp->ts0) {
4139 		ts_reg = bp->ts0->mem;
4140 		on = ioread32(&ts_reg->enable);
4141 		src = "GNSS1";
4142 		seq_printf(s, "%7s: %s, src: %s\n", "TS0",
4143 			   on ? " ON" : "OFF", src);
4144 	}
4145 
4146 	if (bp->ts1) {
4147 		ts_reg = bp->ts1->mem;
4148 		on = ioread32(&ts_reg->enable);
4149 		gpio_input_map(buf, bp, sma_val, 2, NULL);
4150 		seq_printf(s, "%7s: %s, src: %s\n", "TS1",
4151 			   on ? " ON" : "OFF", buf);
4152 	}
4153 
4154 	if (bp->ts2) {
4155 		ts_reg = bp->ts2->mem;
4156 		on = ioread32(&ts_reg->enable);
4157 		gpio_input_map(buf, bp, sma_val, 3, NULL);
4158 		seq_printf(s, "%7s: %s, src: %s\n", "TS2",
4159 			   on ? " ON" : "OFF", buf);
4160 	}
4161 
4162 	if (bp->ts3) {
4163 		ts_reg = bp->ts3->mem;
4164 		on = ioread32(&ts_reg->enable);
4165 		gpio_input_map(buf, bp, sma_val, 6, NULL);
4166 		seq_printf(s, "%7s: %s, src: %s\n", "TS3",
4167 			   on ? " ON" : "OFF", buf);
4168 	}
4169 
4170 	if (bp->ts4) {
4171 		ts_reg = bp->ts4->mem;
4172 		on = ioread32(&ts_reg->enable);
4173 		gpio_input_map(buf, bp, sma_val, 7, NULL);
4174 		seq_printf(s, "%7s: %s, src: %s\n", "TS4",
4175 			   on ? " ON" : "OFF", buf);
4176 	}
4177 
4178 	if (bp->pps) {
4179 		ts_reg = bp->pps->mem;
4180 		src = "PHC";
4181 		on = ioread32(&ts_reg->enable);
4182 		map = !!(bp->pps_req_map & OCP_REQ_TIMESTAMP);
4183 		seq_printf(s, "%7s: %s, src: %s\n", "TS5",
4184 			   on && map ? " ON" : "OFF", src);
4185 
4186 		map = !!(bp->pps_req_map & OCP_REQ_PPS);
4187 		seq_printf(s, "%7s: %s, src: %s\n", "PPS",
4188 			   on && map ? " ON" : "OFF", src);
4189 	}
4190 
4191 	if (bp->fw_cap & OCP_CAP_SIGNAL)
4192 		for (i = 0; i < bp->signals_nr; i++)
4193 			_signal_summary_show(s, bp, i);
4194 
4195 	if (bp->fw_cap & OCP_CAP_FREQ)
4196 		for (i = 0; i < bp->freq_in_nr; i++)
4197 			_frequency_summary_show(s, i, bp->freq_in[i]);
4198 
4199 	if (bp->irig_out) {
4200 		ctrl = ioread32(&bp->irig_out->ctrl);
4201 		on = ctrl & IRIG_M_CTRL_ENABLE;
4202 		val = ioread32(&bp->irig_out->status);
4203 		gpio_output_map(buf, bp, sma_val, 4);
4204 		seq_printf(s, "%7s: %s, error: %d, mode %d, out: %s\n", "IRIG",
4205 			   on ? " ON" : "OFF", val, (ctrl >> 16), buf);
4206 	}
4207 
4208 	if (bp->irig_in) {
4209 		on = ioread32(&bp->irig_in->ctrl) & IRIG_S_CTRL_ENABLE;
4210 		val = ioread32(&bp->irig_in->status);
4211 		gpio_input_map(buf, bp, sma_val, 4, NULL);
4212 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "IRIG in",
4213 			   on ? " ON" : "OFF", val, buf);
4214 	}
4215 
4216 	if (bp->dcf_out) {
4217 		on = ioread32(&bp->dcf_out->ctrl) & DCF_M_CTRL_ENABLE;
4218 		val = ioread32(&bp->dcf_out->status);
4219 		gpio_output_map(buf, bp, sma_val, 5);
4220 		seq_printf(s, "%7s: %s, error: %d, out: %s\n", "DCF",
4221 			   on ? " ON" : "OFF", val, buf);
4222 	}
4223 
4224 	if (bp->dcf_in) {
4225 		on = ioread32(&bp->dcf_in->ctrl) & DCF_S_CTRL_ENABLE;
4226 		val = ioread32(&bp->dcf_in->status);
4227 		gpio_input_map(buf, bp, sma_val, 5, NULL);
4228 		seq_printf(s, "%7s: %s, error: %d, src: %s\n", "DCF in",
4229 			   on ? " ON" : "OFF", val, buf);
4230 	}
4231 
4232 	if (bp->nmea_out) {
4233 		on = ioread32(&bp->nmea_out->ctrl) & 1;
4234 		val = ioread32(&bp->nmea_out->status);
4235 		seq_printf(s, "%7s: %s, error: %d\n", "NMEA",
4236 			   on ? " ON" : "OFF", val);
4237 	}
4238 
4239 	/* compute src for PPS1, used below. */
4240 	if (bp->pps_select) {
4241 		val = ioread32(&bp->pps_select->gpio1);
4242 		src = &buf[80];
4243 		mac_src = "GNSS1";
4244 		if (val & 0x01) {
4245 			gpio_input_map(src, bp, sma_val, 0, NULL);
4246 			mac_src = src;
4247 		} else if (val & 0x02) {
4248 			src = "MAC";
4249 		} else if (val & 0x04) {
4250 			src = "GNSS1";
4251 		} else {
4252 			src = "----";
4253 			mac_src = src;
4254 		}
4255 	} else {
4256 		src = "?";
4257 		mac_src = src;
4258 	}
4259 	seq_printf(s, "MAC PPS1 src: %s\n", mac_src);
4260 
4261 	gpio_input_map(buf, bp, sma_val, 1, "GNSS2");
4262 	seq_printf(s, "MAC PPS2 src: %s\n", buf);
4263 
4264 	/* assumes automatic switchover/selection */
4265 	val = ioread32(&bp->reg->select);
4266 	switch (val >> 16) {
4267 	case 0:
4268 		sprintf(buf, "----");
4269 		break;
4270 	case 2:
4271 		sprintf(buf, "IRIG");
4272 		break;
4273 	case 3:
4274 		sprintf(buf, "%s via PPS1", src);
4275 		break;
4276 	case 6:
4277 		sprintf(buf, "DCF");
4278 		break;
4279 	default:
4280 		strcpy(buf, "unknown");
4281 		break;
4282 	}
4283 	seq_printf(s, "%7s: %s, state: %s\n", "PHC src", buf,
4284 		   bp->sync ? "sync" : "unsynced");
4285 
4286 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, &sts)) {
4287 		struct timespec64 sys_ts;
4288 		s64 pre_ns, post_ns, ns;
4289 
4290 		pre_ns = timespec64_to_ns(&sts.pre_ts);
4291 		post_ns = timespec64_to_ns(&sts.post_ts);
4292 		ns = (pre_ns + post_ns) / 2;
4293 		ns += (s64)bp->utc_tai_offset * NSEC_PER_SEC;
4294 		sys_ts = ns_to_timespec64(ns);
4295 
4296 		seq_printf(s, "%7s: %ptSp == %ptS TAI\n", "PHC", &ts, &ts);
4297 		seq_printf(s, "%7s: %ptSp == %ptS UTC offset %d\n", "SYS",
4298 			   &sys_ts, &sys_ts, bp->utc_tai_offset);
4299 		seq_printf(s, "%7s: PHC:SYS offset: %lld  window: %lld\n", "",
4300 			   timespec64_to_ns(&ts) - ns,
4301 			   post_ns - pre_ns);
4302 	}
4303 
4304 	free_page((unsigned long)buf);
4305 	return 0;
4306 }
4307 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_summary);
4308 
4309 static int
4310 ptp_ocp_tod_status_show(struct seq_file *s, void *data)
4311 {
4312 	struct device *dev = s->private;
4313 	struct ptp_ocp *bp;
4314 	u32 val;
4315 	int idx;
4316 
4317 	bp = dev_get_drvdata(dev);
4318 
4319 	val = ioread32(&bp->tod->ctrl);
4320 	if (!(val & TOD_CTRL_ENABLE)) {
4321 		seq_printf(s, "TOD Slave disabled\n");
4322 		return 0;
4323 	}
4324 	seq_printf(s, "TOD Slave enabled, Control Register 0x%08X\n", val);
4325 
4326 	idx = val & TOD_CTRL_PROTOCOL ? 4 : 0;
4327 	idx += (val >> 16) & 3;
4328 	seq_printf(s, "Protocol %s\n", ptp_ocp_tod_proto_name(idx));
4329 
4330 	idx = (val >> TOD_CTRL_GNSS_SHIFT) & TOD_CTRL_GNSS_MASK;
4331 	seq_printf(s, "GNSS %s\n", ptp_ocp_tod_gnss_name(idx));
4332 
4333 	val = ioread32(&bp->tod->version);
4334 	seq_printf(s, "TOD Version %d.%d.%d\n",
4335 		val >> 24, (val >> 16) & 0xff, val & 0xffff);
4336 
4337 	val = ioread32(&bp->tod->status);
4338 	seq_printf(s, "Status register: 0x%08X\n", val);
4339 
4340 	val = ioread32(&bp->tod->adj_sec);
4341 	idx = (val & ~INT_MAX) ? -1 : 1;
4342 	idx *= (val & INT_MAX);
4343 	seq_printf(s, "Correction seconds: %d\n", idx);
4344 
4345 	val = ioread32(&bp->tod->utc_status);
4346 	seq_printf(s, "UTC status register: 0x%08X\n", val);
4347 	seq_printf(s, "UTC offset: %ld  valid:%d\n",
4348 		val & TOD_STATUS_UTC_MASK, val & TOD_STATUS_UTC_VALID ? 1 : 0);
4349 	seq_printf(s, "Leap second info valid:%d, Leap second announce %d\n",
4350 		val & TOD_STATUS_LEAP_VALID ? 1 : 0,
4351 		val & TOD_STATUS_LEAP_ANNOUNCE ? 1 : 0);
4352 
4353 	val = ioread32(&bp->tod->leap);
4354 	seq_printf(s, "Time to next leap second (in sec): %d\n", (s32) val);
4355 
4356 	return 0;
4357 }
4358 DEFINE_SHOW_ATTRIBUTE(ptp_ocp_tod_status);
4359 
4360 static struct dentry *ptp_ocp_debugfs_root;
4361 
4362 static void
4363 ptp_ocp_debugfs_add_device(struct ptp_ocp *bp)
4364 {
4365 	struct dentry *d;
4366 
4367 	d = debugfs_create_dir(dev_name(&bp->dev), ptp_ocp_debugfs_root);
4368 	bp->debug_root = d;
4369 	debugfs_create_file("summary", 0444, bp->debug_root,
4370 			    &bp->dev, &ptp_ocp_summary_fops);
4371 	if (bp->tod)
4372 		debugfs_create_file("tod_status", 0444, bp->debug_root,
4373 				    &bp->dev, &ptp_ocp_tod_status_fops);
4374 }
4375 
4376 static void
4377 ptp_ocp_debugfs_remove_device(struct ptp_ocp *bp)
4378 {
4379 	debugfs_remove_recursive(bp->debug_root);
4380 }
4381 
4382 static void
4383 ptp_ocp_debugfs_init(void)
4384 {
4385 	ptp_ocp_debugfs_root = debugfs_create_dir("timecard", NULL);
4386 }
4387 
4388 static void
4389 ptp_ocp_debugfs_fini(void)
4390 {
4391 	debugfs_remove_recursive(ptp_ocp_debugfs_root);
4392 }
4393 
4394 static void
4395 ptp_ocp_dev_release(struct device *dev)
4396 {
4397 	struct ptp_ocp *bp = dev_get_drvdata(dev);
4398 
4399 	mutex_lock(&ptp_ocp_lock);
4400 	idr_remove(&ptp_ocp_idr, bp->id);
4401 	mutex_unlock(&ptp_ocp_lock);
4402 }
4403 
4404 static int
4405 ptp_ocp_device_init(struct ptp_ocp *bp, struct pci_dev *pdev)
4406 {
4407 	int i, err;
4408 
4409 	mutex_lock(&ptp_ocp_lock);
4410 	err = idr_alloc(&ptp_ocp_idr, bp, 0, 0, GFP_KERNEL);
4411 	mutex_unlock(&ptp_ocp_lock);
4412 	if (err < 0) {
4413 		dev_err(&pdev->dev, "idr_alloc failed: %d\n", err);
4414 		return err;
4415 	}
4416 	bp->id = err;
4417 
4418 	bp->ptp_info = ptp_ocp_clock_info;
4419 	spin_lock_init(&bp->lock);
4420 
4421 	for (i = 0; i < __PORT_COUNT; i++)
4422 		bp->port[i].line = -1;
4423 
4424 	bp->pdev = pdev;
4425 
4426 	device_initialize(&bp->dev);
4427 	dev_set_name(&bp->dev, "ocp%d", bp->id);
4428 	bp->dev.class = &timecard_class;
4429 	bp->dev.parent = &pdev->dev;
4430 	bp->dev.release = ptp_ocp_dev_release;
4431 	dev_set_drvdata(&bp->dev, bp);
4432 
4433 	err = device_add(&bp->dev);
4434 	if (err) {
4435 		dev_err(&bp->dev, "device add failed: %d\n", err);
4436 		goto out;
4437 	}
4438 
4439 	pci_set_drvdata(pdev, bp);
4440 
4441 	return 0;
4442 
4443 out:
4444 	put_device(&bp->dev);
4445 	return err;
4446 }
4447 
4448 static void
4449 ptp_ocp_symlink(struct ptp_ocp *bp, struct device *child, const char *link)
4450 {
4451 	struct device *dev = &bp->dev;
4452 
4453 	if (sysfs_create_link(&dev->kobj, &child->kobj, link))
4454 		dev_err(dev, "%s symlink failed\n", link);
4455 }
4456 
4457 static void
4458 ptp_ocp_link_child(struct ptp_ocp *bp, const char *name, const char *link)
4459 {
4460 	struct device *dev, *child;
4461 
4462 	dev = &bp->pdev->dev;
4463 
4464 	child = device_find_child_by_name(dev, name);
4465 	if (!child) {
4466 		dev_err(dev, "Could not find device %s\n", name);
4467 		return;
4468 	}
4469 
4470 	ptp_ocp_symlink(bp, child, link);
4471 	put_device(child);
4472 }
4473 
4474 static int
4475 ptp_ocp_complete(struct ptp_ocp *bp)
4476 {
4477 	struct pps_device *pps;
4478 	char buf[32];
4479 
4480 	sprintf(buf, "ptp%d", ptp_clock_index(bp->ptp));
4481 	ptp_ocp_link_child(bp, buf, "ptp");
4482 
4483 	pps = pps_lookup_dev(bp->ptp);
4484 	if (pps)
4485 		ptp_ocp_symlink(bp, &pps->dev, "pps");
4486 
4487 	ptp_ocp_debugfs_add_device(bp);
4488 
4489 	return 0;
4490 }
4491 
4492 static void
4493 ptp_ocp_phc_info(struct ptp_ocp *bp)
4494 {
4495 	struct timespec64 ts;
4496 	u32 version, select;
4497 
4498 	version = ioread32(&bp->reg->version);
4499 	select = ioread32(&bp->reg->select);
4500 	dev_info(&bp->pdev->dev, "Version %d.%d.%d, clock %s, device ptp%d\n",
4501 		 version >> 24, (version >> 16) & 0xff, version & 0xffff,
4502 		 ptp_ocp_select_name_from_val(ptp_ocp_clock, select >> 16),
4503 		 ptp_clock_index(bp->ptp));
4504 
4505 	if (!ptp_ocp_gettimex(&bp->ptp_info, &ts, NULL))
4506 		dev_info(&bp->pdev->dev, "Time: %ptSp, %s\n",
4507 			 &ts, bp->sync ? "in-sync" : "UNSYNCED");
4508 }
4509 
4510 static void
4511 ptp_ocp_serial_info(struct device *dev, const char *name, int port, int baud)
4512 {
4513 	if (port != -1)
4514 		dev_info(dev, "%5s: /dev/ttyS%-2d @ %6d\n", name, port, baud);
4515 }
4516 
4517 static void
4518 ptp_ocp_info(struct ptp_ocp *bp)
4519 {
4520 	static int nmea_baud[] = {
4521 		1200, 2400, 4800, 9600, 19200, 38400,
4522 		57600, 115200, 230400, 460800, 921600,
4523 		1000000, 2000000
4524 	};
4525 	struct device *dev = &bp->pdev->dev;
4526 	u32 reg;
4527 	int i;
4528 
4529 	ptp_ocp_phc_info(bp);
4530 
4531 	for (i = 0; i < __PORT_COUNT; i++) {
4532 		if (i == PORT_NMEA && bp->nmea_out && bp->port[PORT_NMEA].line != -1) {
4533 			bp->port[PORT_NMEA].baud = -1;
4534 
4535 			reg = ioread32(&bp->nmea_out->uart_baud);
4536 			if (reg < ARRAY_SIZE(nmea_baud))
4537 				bp->port[PORT_NMEA].baud = nmea_baud[reg];
4538 		}
4539 		ptp_ocp_serial_info(dev, ptp_ocp_tty_port_name(i), bp->port[i].line,
4540 				    bp->port[i].baud);
4541 	}
4542 }
4543 
4544 static void
4545 ptp_ocp_detach_sysfs(struct ptp_ocp *bp)
4546 {
4547 	struct device *dev = &bp->dev;
4548 
4549 	sysfs_remove_link(&dev->kobj, "ptp");
4550 	sysfs_remove_link(&dev->kobj, "pps");
4551 }
4552 
4553 static void
4554 ptp_ocp_detach(struct ptp_ocp *bp)
4555 {
4556 	int i;
4557 
4558 	ptp_ocp_debugfs_remove_device(bp);
4559 	ptp_ocp_detach_sysfs(bp);
4560 	ptp_ocp_attr_group_del(bp);
4561 	timer_delete_sync(&bp->watchdog);
4562 	ptp_ocp_unregister_ext(bp->ts0);
4563 	ptp_ocp_unregister_ext(bp->ts1);
4564 	ptp_ocp_unregister_ext(bp->ts2);
4565 	ptp_ocp_unregister_ext(bp->ts3);
4566 	ptp_ocp_unregister_ext(bp->ts4);
4567 	ptp_ocp_unregister_ext(bp->pps);
4568 	for (i = 0; i < 4; i++)
4569 		ptp_ocp_unregister_ext(bp->signal_out[i]);
4570 	for (i = 0; i < __PORT_COUNT; i++)
4571 		if (bp->port[i].line != -1)
4572 			serial8250_unregister_port(bp->port[i].line);
4573 	platform_device_unregister(bp->spi_flash);
4574 	platform_device_unregister(bp->i2c_ctrl);
4575 	if (bp->i2c_clk)
4576 		clk_hw_unregister_fixed_rate(bp->i2c_clk);
4577 	if (bp->n_irqs)
4578 		pci_free_irq_vectors(bp->pdev);
4579 	if (bp->ptp)
4580 		ptp_clock_unregister(bp->ptp);
4581 	kfree(bp->ptp_info.pin_config);
4582 	device_unregister(&bp->dev);
4583 }
4584 
4585 static int
4586 ptp_ocp_dpll_lock_status_get(const struct dpll_device *dpll, void *priv,
4587 			     enum dpll_lock_status *status,
4588 			     enum dpll_lock_status_error *status_error,
4589 			     struct netlink_ext_ack *extack)
4590 {
4591 	struct ptp_ocp *bp = priv;
4592 
4593 	*status = bp->sync ? DPLL_LOCK_STATUS_LOCKED : DPLL_LOCK_STATUS_UNLOCKED;
4594 
4595 	return 0;
4596 }
4597 
4598 static int ptp_ocp_dpll_state_get(const struct dpll_pin *pin, void *pin_priv,
4599 				  const struct dpll_device *dpll, void *priv,
4600 				  enum dpll_pin_state *state,
4601 				  struct netlink_ext_ack *extack)
4602 {
4603 	struct ptp_ocp *bp = priv;
4604 	int idx;
4605 
4606 	if (bp->pps_select) {
4607 		idx = ioread32(&bp->pps_select->gpio1);
4608 		*state = (&bp->sma[idx] == pin_priv) ? DPLL_PIN_STATE_CONNECTED :
4609 						      DPLL_PIN_STATE_SELECTABLE;
4610 		return 0;
4611 	}
4612 	NL_SET_ERR_MSG(extack, "pin selection is not supported on current HW");
4613 	return -EINVAL;
4614 }
4615 
4616 static int ptp_ocp_dpll_mode_get(const struct dpll_device *dpll, void *priv,
4617 				 enum dpll_mode *mode, struct netlink_ext_ack *extack)
4618 {
4619 	*mode = DPLL_MODE_AUTOMATIC;
4620 	return 0;
4621 }
4622 
4623 static int ptp_ocp_dpll_direction_get(const struct dpll_pin *pin,
4624 				      void *pin_priv,
4625 				      const struct dpll_device *dpll,
4626 				      void *priv,
4627 				      enum dpll_pin_direction *direction,
4628 				      struct netlink_ext_ack *extack)
4629 {
4630 	struct ptp_ocp_sma_connector *sma = pin_priv;
4631 
4632 	*direction = sma->mode == SMA_MODE_IN ?
4633 				  DPLL_PIN_DIRECTION_INPUT :
4634 				  DPLL_PIN_DIRECTION_OUTPUT;
4635 	return 0;
4636 }
4637 
4638 static int ptp_ocp_dpll_direction_set(const struct dpll_pin *pin,
4639 				      void *pin_priv,
4640 				      const struct dpll_device *dpll,
4641 				      void *dpll_priv,
4642 				      enum dpll_pin_direction direction,
4643 				      struct netlink_ext_ack *extack)
4644 {
4645 	struct ptp_ocp_sma_connector *sma = pin_priv;
4646 	struct ptp_ocp *bp = dpll_priv;
4647 	enum ptp_ocp_sma_mode mode;
4648 	int sma_nr = (sma - bp->sma);
4649 
4650 	if (sma->fixed_dir)
4651 		return -EOPNOTSUPP;
4652 	mode = direction == DPLL_PIN_DIRECTION_INPUT ?
4653 			    SMA_MODE_IN : SMA_MODE_OUT;
4654 	return ptp_ocp_sma_store_val(bp, 0, mode, sma_nr + 1);
4655 }
4656 
4657 static int ptp_ocp_dpll_frequency_set(const struct dpll_pin *pin,
4658 				      void *pin_priv,
4659 				      const struct dpll_device *dpll,
4660 				      void *dpll_priv, u64 frequency,
4661 				      struct netlink_ext_ack *extack)
4662 {
4663 	struct ptp_ocp_sma_connector *sma = pin_priv;
4664 	struct ptp_ocp *bp = dpll_priv;
4665 	const struct ocp_selector *tbl;
4666 	int sma_nr = (sma - bp->sma);
4667 	int i;
4668 
4669 	if (sma->fixed_fcn)
4670 		return -EOPNOTSUPP;
4671 
4672 	tbl = bp->sma_op->tbl[sma->mode];
4673 	for (i = 0; tbl[i].name; i++)
4674 		if (tbl[i].frequency == frequency)
4675 			return ptp_ocp_sma_store_val(bp, i, sma->mode, sma_nr + 1);
4676 	return -EINVAL;
4677 }
4678 
4679 static int ptp_ocp_dpll_frequency_get(const struct dpll_pin *pin,
4680 				      void *pin_priv,
4681 				      const struct dpll_device *dpll,
4682 				      void *dpll_priv, u64 *frequency,
4683 				      struct netlink_ext_ack *extack)
4684 {
4685 	struct ptp_ocp_sma_connector *sma = pin_priv;
4686 	struct ptp_ocp *bp = dpll_priv;
4687 	const struct ocp_selector *tbl;
4688 	int sma_nr = (sma - bp->sma);
4689 	u32 val;
4690 	int i;
4691 
4692 	val = bp->sma_op->get(bp, sma_nr + 1);
4693 	tbl = bp->sma_op->tbl[sma->mode];
4694 	for (i = 0; tbl[i].name; i++)
4695 		if (val == tbl[i].value) {
4696 			*frequency = tbl[i].frequency;
4697 			return 0;
4698 		}
4699 
4700 	return -EINVAL;
4701 }
4702 
4703 static const struct dpll_device_ops dpll_ops = {
4704 	.lock_status_get = ptp_ocp_dpll_lock_status_get,
4705 	.mode_get = ptp_ocp_dpll_mode_get,
4706 };
4707 
4708 static const struct dpll_pin_ops dpll_pins_ops = {
4709 	.frequency_get = ptp_ocp_dpll_frequency_get,
4710 	.frequency_set = ptp_ocp_dpll_frequency_set,
4711 	.direction_get = ptp_ocp_dpll_direction_get,
4712 	.direction_set = ptp_ocp_dpll_direction_set,
4713 	.state_on_dpll_get = ptp_ocp_dpll_state_get,
4714 };
4715 
4716 static void
4717 ptp_ocp_sync_work(struct work_struct *work)
4718 {
4719 	struct ptp_ocp *bp;
4720 	bool sync;
4721 
4722 	bp = container_of(work, struct ptp_ocp, sync_work.work);
4723 	sync = !!(ioread32(&bp->reg->status) & OCP_STATUS_IN_SYNC);
4724 
4725 	if (bp->sync != sync)
4726 		dpll_device_change_ntf(bp->dpll);
4727 
4728 	bp->sync = sync;
4729 
4730 	queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
4731 }
4732 
4733 static int
4734 ptp_ocp_probe(struct pci_dev *pdev, const struct pci_device_id *id)
4735 {
4736 	struct devlink *devlink;
4737 	struct ptp_ocp *bp;
4738 	int err, i;
4739 	u64 clkid;
4740 
4741 	devlink = devlink_alloc(&ptp_ocp_devlink_ops, sizeof(*bp), &pdev->dev);
4742 	if (!devlink) {
4743 		dev_err(&pdev->dev, "devlink_alloc failed\n");
4744 		return -ENOMEM;
4745 	}
4746 
4747 	err = pci_enable_device(pdev);
4748 	if (err) {
4749 		dev_err(&pdev->dev, "pci_enable_device\n");
4750 		goto out_free;
4751 	}
4752 
4753 	bp = devlink_priv(devlink);
4754 	err = ptp_ocp_device_init(bp, pdev);
4755 	if (err)
4756 		goto out_disable;
4757 
4758 	INIT_DELAYED_WORK(&bp->sync_work, ptp_ocp_sync_work);
4759 
4760 	/* compat mode.
4761 	 * Older FPGA firmware only returns 2 irq's.
4762 	 * allow this - if not all of the IRQ's are returned, skip the
4763 	 * extra devices and just register the clock.
4764 	 */
4765 	err = pci_alloc_irq_vectors(pdev, 1, 17, PCI_IRQ_MSI | PCI_IRQ_MSIX);
4766 	if (err < 0) {
4767 		dev_err(&pdev->dev, "alloc_irq_vectors err: %d\n", err);
4768 		goto out;
4769 	}
4770 	bp->n_irqs = err;
4771 	pci_set_master(pdev);
4772 
4773 	err = ptp_ocp_register_resources(bp, id->driver_data);
4774 	if (err)
4775 		goto out;
4776 
4777 	bp->ptp = ptp_clock_register(&bp->ptp_info, &pdev->dev);
4778 	if (IS_ERR(bp->ptp)) {
4779 		err = PTR_ERR(bp->ptp);
4780 		dev_err(&pdev->dev, "ptp_clock_register: %d\n", err);
4781 		bp->ptp = NULL;
4782 		goto out;
4783 	}
4784 
4785 	err = ptp_ocp_complete(bp);
4786 	if (err)
4787 		goto out;
4788 
4789 	ptp_ocp_info(bp);
4790 	devlink_register(devlink);
4791 
4792 	clkid = pci_get_dsn(pdev);
4793 	bp->dpll = dpll_device_get(clkid, 0, THIS_MODULE, &bp->tracker);
4794 	if (IS_ERR(bp->dpll)) {
4795 		err = PTR_ERR(bp->dpll);
4796 		dev_err(&pdev->dev, "dpll_device_alloc failed\n");
4797 		goto out;
4798 	}
4799 
4800 	err = dpll_device_register(bp->dpll, DPLL_TYPE_PPS, &dpll_ops, bp);
4801 	if (err)
4802 		goto out;
4803 
4804 	for (i = 0; i < OCP_SMA_NUM; i++) {
4805 		bp->sma[i].dpll_pin = dpll_pin_get(clkid, i, THIS_MODULE,
4806 						   &bp->sma[i].dpll_prop,
4807 						   &bp->sma[i].tracker);
4808 		if (IS_ERR(bp->sma[i].dpll_pin)) {
4809 			err = PTR_ERR(bp->sma[i].dpll_pin);
4810 			goto out_dpll;
4811 		}
4812 
4813 		err = dpll_pin_register(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops,
4814 					&bp->sma[i]);
4815 		if (err) {
4816 			dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
4817 			goto out_dpll;
4818 		}
4819 	}
4820 	queue_delayed_work(system_power_efficient_wq, &bp->sync_work, HZ);
4821 
4822 	return 0;
4823 out_dpll:
4824 	while (i--) {
4825 		dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
4826 		dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
4827 	}
4828 	dpll_device_put(bp->dpll, &bp->tracker);
4829 out:
4830 	ptp_ocp_detach(bp);
4831 out_disable:
4832 	pci_disable_device(pdev);
4833 out_free:
4834 	devlink_free(devlink);
4835 	return err;
4836 }
4837 
4838 static void
4839 ptp_ocp_remove(struct pci_dev *pdev)
4840 {
4841 	struct ptp_ocp *bp = pci_get_drvdata(pdev);
4842 	struct devlink *devlink = priv_to_devlink(bp);
4843 	int i;
4844 
4845 	cancel_delayed_work_sync(&bp->sync_work);
4846 	for (i = 0; i < OCP_SMA_NUM; i++) {
4847 		if (bp->sma[i].dpll_pin) {
4848 			dpll_pin_unregister(bp->dpll, bp->sma[i].dpll_pin, &dpll_pins_ops, &bp->sma[i]);
4849 			dpll_pin_put(bp->sma[i].dpll_pin, &bp->sma[i].tracker);
4850 		}
4851 	}
4852 	dpll_device_unregister(bp->dpll, &dpll_ops, bp);
4853 	dpll_device_put(bp->dpll, &bp->tracker);
4854 	devlink_unregister(devlink);
4855 	ptp_ocp_detach(bp);
4856 	pci_disable_device(pdev);
4857 
4858 	devlink_free(devlink);
4859 }
4860 
4861 static struct pci_driver ptp_ocp_driver = {
4862 	.name		= KBUILD_MODNAME,
4863 	.id_table	= ptp_ocp_pcidev_id,
4864 	.probe		= ptp_ocp_probe,
4865 	.remove		= ptp_ocp_remove,
4866 };
4867 
4868 static int
4869 ptp_ocp_i2c_notifier_call(struct notifier_block *nb,
4870 			  unsigned long action, void *data)
4871 {
4872 	struct device *dev, *child = data;
4873 	struct ptp_ocp *bp;
4874 	bool add;
4875 
4876 	switch (action) {
4877 	case BUS_NOTIFY_ADD_DEVICE:
4878 	case BUS_NOTIFY_DEL_DEVICE:
4879 		add = action == BUS_NOTIFY_ADD_DEVICE;
4880 		break;
4881 	default:
4882 		return 0;
4883 	}
4884 
4885 	if (!i2c_verify_adapter(child))
4886 		return 0;
4887 
4888 	dev = child;
4889 	while ((dev = dev->parent))
4890 		if (dev->driver && !strcmp(dev->driver->name, KBUILD_MODNAME))
4891 			goto found;
4892 	return 0;
4893 
4894 found:
4895 	bp = dev_get_drvdata(dev);
4896 	if (add)
4897 		ptp_ocp_symlink(bp, child, "i2c");
4898 	else
4899 		sysfs_remove_link(&bp->dev.kobj, "i2c");
4900 
4901 	return 0;
4902 }
4903 
4904 static struct notifier_block ptp_ocp_i2c_notifier = {
4905 	.notifier_call = ptp_ocp_i2c_notifier_call,
4906 };
4907 
4908 static int __init
4909 ptp_ocp_init(void)
4910 {
4911 	const char *what;
4912 	int err;
4913 
4914 	ptp_ocp_debugfs_init();
4915 
4916 	what = "timecard class";
4917 	err = class_register(&timecard_class);
4918 	if (err)
4919 		goto out;
4920 
4921 	what = "i2c notifier";
4922 	err = bus_register_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4923 	if (err)
4924 		goto out_notifier;
4925 
4926 	what = "ptp_ocp driver";
4927 	err = pci_register_driver(&ptp_ocp_driver);
4928 	if (err)
4929 		goto out_register;
4930 
4931 	return 0;
4932 
4933 out_register:
4934 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4935 out_notifier:
4936 	class_unregister(&timecard_class);
4937 out:
4938 	ptp_ocp_debugfs_fini();
4939 	pr_err(KBUILD_MODNAME ": failed to register %s: %d\n", what, err);
4940 	return err;
4941 }
4942 
4943 static void __exit
4944 ptp_ocp_fini(void)
4945 {
4946 	bus_unregister_notifier(&i2c_bus_type, &ptp_ocp_i2c_notifier);
4947 	pci_unregister_driver(&ptp_ocp_driver);
4948 	class_unregister(&timecard_class);
4949 	ptp_ocp_debugfs_fini();
4950 }
4951 
4952 module_init(ptp_ocp_init);
4953 module_exit(ptp_ocp_fini);
4954 
4955 MODULE_DESCRIPTION("OpenCompute TimeCard driver");
4956 MODULE_LICENSE("GPL v2");
4957