xref: /linux/drivers/media/pci/mgb4/mgb4_core.c (revision 0924f7ed0c17c9aee75c269d8ed825b8fbfb3fa3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This is the driver for the MGB4 video grabber card by Digiteq Automotive.
4  *
5  * Copyright (C) 2021-2023 Digiteq Automotive
6  *     author: Martin Tuma <martin.tuma@digiteqautomotive.com>
7  *
8  * This is the main driver module. The DMA, I2C and SPI sub-drivers are
9  * initialized here and the input/output v4l2 devices are created.
10  *
11  * The mgb4 card uses different expansion modules for different video sources
12  * (GMSL and FPDL3 for now) so in probe() we detect the module type based on
13  * what we see on the I2C bus and check if it matches the FPGA bitstream (there
14  * are different bitstreams for different expansion modules). When no expansion
15  * module is present, we still let the driver initialize to allow flashing of
16  * the FPGA firmware using the SPI FLASH device. No v4l2 video devices are
17  * created in this case.
18  */
19 
20 #include <linux/types.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
25 #include <linux/clk-provider.h>
26 #include <linux/clkdev.h>
27 #include <linux/i2c.h>
28 #include <linux/delay.h>
29 #include <linux/dma/amd_xdma.h>
30 #include <linux/platform_data/amd_xdma.h>
31 #include <linux/spi/xilinx_spi.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/hwmon.h>
34 #include <linux/debugfs.h>
35 #include "mgb4_dma.h"
36 #include "mgb4_i2c.h"
37 #include "mgb4_sysfs.h"
38 #include "mgb4_vout.h"
39 #include "mgb4_vin.h"
40 #include "mgb4_trigger.h"
41 #include "mgb4_core.h"
42 
43 #define MGB4_USER_IRQS  16
44 #define MGB4_MGB4_BAR_ID 0
45 #define MGB4_XDMA_BAR_ID 1
46 
47 #define DIGITEQ_VID 0x1ed8
48 #define T100_DID    0x0101
49 #define T200_DID    0x0201
50 
51 ATTRIBUTE_GROUPS(mgb4_pci);
52 
53 static int flashid;
54 
55 static struct xdma_chan_info h2c_chan_info = {
56 	.dir = DMA_MEM_TO_DEV,
57 };
58 
59 static struct xdma_chan_info c2h_chan_info = {
60 	.dir = DMA_DEV_TO_MEM,
61 };
62 
63 static struct xspi_platform_data spi_platform_data = {
64 	.num_chipselect = 1,
65 	.bits_per_word = 8
66 };
67 
68 static const struct i2c_board_info extender_info = {
69 	I2C_BOARD_INFO("extender", 0x21)
70 };
71 
72 #if IS_REACHABLE(CONFIG_HWMON)
73 static umode_t temp_is_visible(const void *data, enum hwmon_sensor_types type,
74 			       u32 attr, int channel)
75 {
76 	if (type == hwmon_temp &&
77 	    (attr == hwmon_temp_input || attr == hwmon_temp_label))
78 		return 0444;
79 	else
80 		return 0;
81 }
82 
83 static int temp_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
84 		     int channel, long *val)
85 {
86 	struct mgb4_dev *mgbdev = dev_get_drvdata(dev);
87 	u32 raw;
88 	int val10;
89 
90 	if (type != hwmon_temp || attr != hwmon_temp_input)
91 		return -EOPNOTSUPP;
92 
93 	raw = mgb4_read_reg(&mgbdev->video, 0xD0);
94 	/* register value -> Celsius degrees formula given by Xilinx */
95 	val10 = ((((raw >> 20) & 0xFFF) * 503975) - 1118822400) / 409600;
96 	*val = val10 * 100;
97 
98 	return 0;
99 }
100 
101 static int temp_read_string(struct device *dev, enum hwmon_sensor_types type,
102 			    u32 attr, int channel, const char **str)
103 {
104 	if (type != hwmon_temp || attr != hwmon_temp_label)
105 		return -EOPNOTSUPP;
106 
107 	*str = "FPGA Temperature";
108 
109 	return 0;
110 }
111 
112 static const struct hwmon_ops temp_ops = {
113 	.is_visible = temp_is_visible,
114 	.read = temp_read,
115 	.read_string = temp_read_string
116 };
117 
118 static const struct hwmon_channel_info *temp_channel_info[] = {
119 	HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
120 	NULL
121 };
122 
123 static const struct hwmon_chip_info temp_chip_info = {
124 	.ops = &temp_ops,
125 	.info = temp_channel_info,
126 };
127 #endif
128 
129 static int match_i2c_adap(struct device *dev, const void *data)
130 {
131 	return i2c_verify_adapter(dev) ? 1 : 0;
132 }
133 
134 static struct i2c_adapter *get_i2c_adap(struct platform_device *pdev)
135 {
136 	struct device *dev;
137 
138 	mutex_lock(&pdev->dev.mutex);
139 	dev = device_find_child(&pdev->dev, NULL, match_i2c_adap);
140 	mutex_unlock(&pdev->dev.mutex);
141 
142 	return dev ? to_i2c_adapter(dev) : NULL;
143 }
144 
145 static int match_spi_adap(struct device *dev, const void *data)
146 {
147 	return to_spi_device(dev) ? 1 : 0;
148 }
149 
150 static struct spi_controller *get_spi_adap(struct platform_device *pdev)
151 {
152 	struct device *dev;
153 
154 	mutex_lock(&pdev->dev.mutex);
155 	dev = device_find_child(&pdev->dev, NULL, match_spi_adap);
156 	mutex_unlock(&pdev->dev.mutex);
157 
158 	return dev ? container_of(dev, struct spi_controller, dev) : NULL;
159 }
160 
161 static int init_spi(struct mgb4_dev *mgbdev, u32 devid)
162 {
163 	struct resource spi_resources[] = {
164 		{
165 			.start	= 0x400,
166 			.end	= 0x47f,
167 			.flags	= IORESOURCE_MEM,
168 			.name	= "io-memory",
169 		},
170 		{
171 			.start	= 14,
172 			.end	= 14,
173 			.flags	= IORESOURCE_IRQ,
174 			.name	= "irq",
175 		},
176 	};
177 	struct spi_board_info spi_info = {
178 		.max_speed_hz = 10000000,
179 		.modalias = "m25p80",
180 		.chip_select = 0,
181 		.mode = SPI_MODE_3,
182 	};
183 	struct pci_dev *pdev = mgbdev->pdev;
184 	struct device *dev = &pdev->dev;
185 	struct spi_controller *ctlr;
186 	struct spi_device *spi_dev;
187 	u32 irq;
188 	int rv, id;
189 	resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
190 
191 	request_module("platform:xilinx_spi");
192 
193 	irq = xdma_get_user_irq(mgbdev->xdev, 14);
194 	xdma_enable_user_irq(mgbdev->xdev, irq);
195 
196 	spi_resources[0].parent = &pdev->resource[MGB4_MGB4_BAR_ID];
197 	spi_resources[0].start += mapbase;
198 	spi_resources[0].end += mapbase;
199 	spi_resources[1].start = irq;
200 	spi_resources[1].end = irq;
201 
202 	id = pci_dev_id(pdev);
203 	mgbdev->spi_pdev = platform_device_register_resndata(dev, "xilinx_spi",
204 							     id, spi_resources,
205 							     ARRAY_SIZE(spi_resources),
206 							     &spi_platform_data,
207 							     sizeof(spi_platform_data));
208 	if (IS_ERR(mgbdev->spi_pdev)) {
209 		dev_err(dev, "failed to register SPI device\n");
210 		return PTR_ERR(mgbdev->spi_pdev);
211 	}
212 
213 	ctlr = get_spi_adap(mgbdev->spi_pdev);
214 	if (!ctlr) {
215 		dev_err(dev, "failed to get SPI adapter\n");
216 		rv = -EINVAL;
217 		goto err_pdev;
218 	}
219 
220 	snprintf(mgbdev->fw_part_name, sizeof(mgbdev->fw_part_name),
221 		 "mgb4-fw.%d", flashid);
222 	mgbdev->partitions[0].name = mgbdev->fw_part_name;
223 	if (devid == T200_DID) {
224 		mgbdev->partitions[0].size = 0x950000;
225 		mgbdev->partitions[0].offset = 0x1000000;
226 	} else {
227 		mgbdev->partitions[0].size = 0x400000;
228 		mgbdev->partitions[0].offset = 0x400000;
229 	}
230 	mgbdev->partitions[0].mask_flags = 0;
231 
232 	snprintf(mgbdev->data_part_name, sizeof(mgbdev->data_part_name),
233 		 "mgb4-data.%d", flashid);
234 	mgbdev->partitions[1].name = mgbdev->data_part_name;
235 	mgbdev->partitions[1].size = 0x10000;
236 	mgbdev->partitions[1].offset = 0xFF0000;
237 	mgbdev->partitions[1].mask_flags = MTD_CAP_NORFLASH;
238 
239 	snprintf(mgbdev->flash_name, sizeof(mgbdev->flash_name),
240 		 "mgb4-flash.%d", flashid);
241 	mgbdev->flash_data.name = mgbdev->flash_name;
242 	mgbdev->flash_data.parts = mgbdev->partitions;
243 	mgbdev->flash_data.nr_parts = ARRAY_SIZE(mgbdev->partitions);
244 	mgbdev->flash_data.type = "spi-nor";
245 
246 	spi_info.platform_data = &mgbdev->flash_data;
247 
248 	spi_dev = spi_new_device(ctlr, &spi_info);
249 	put_device(&ctlr->dev);
250 	if (!spi_dev) {
251 		dev_err(dev, "failed to create MTD device\n");
252 		rv = -EINVAL;
253 		goto err_pdev;
254 	}
255 
256 	return 0;
257 
258 err_pdev:
259 	platform_device_unregister(mgbdev->spi_pdev);
260 
261 	return rv;
262 }
263 
264 static void free_spi(struct mgb4_dev *mgbdev)
265 {
266 	platform_device_unregister(mgbdev->spi_pdev);
267 }
268 
269 static int init_i2c(struct mgb4_dev *mgbdev)
270 {
271 	struct resource i2c_resources[] = {
272 		{
273 			.start	= 0x200,
274 			.end	= 0x3ff,
275 			.flags	= IORESOURCE_MEM,
276 			.name	= "io-memory",
277 		},
278 		{
279 			.start	= 15,
280 			.end	= 15,
281 			.flags	= IORESOURCE_IRQ,
282 			.name	= "irq",
283 		},
284 	};
285 	struct pci_dev *pdev = mgbdev->pdev;
286 	struct device *dev = &pdev->dev;
287 	char clk_name[16];
288 	u32 irq;
289 	int rv, id;
290 	resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
291 
292 	request_module("platform:xiic-i2c");
293 
294 	irq = xdma_get_user_irq(mgbdev->xdev, 15);
295 	xdma_enable_user_irq(mgbdev->xdev, irq);
296 
297 	i2c_resources[0].parent = &pdev->resource[MGB4_MGB4_BAR_ID];
298 	i2c_resources[0].start += mapbase;
299 	i2c_resources[0].end += mapbase;
300 	i2c_resources[1].start = irq;
301 	i2c_resources[1].end = irq;
302 
303 	id = pci_dev_id(pdev);
304 
305 	/* create dummy clock required by the xiic-i2c adapter */
306 	snprintf(clk_name, sizeof(clk_name), "xiic-i2c.%d", id);
307 	mgbdev->i2c_clk = clk_hw_register_fixed_rate(NULL, clk_name, NULL,
308 						     0, MGB4_HW_FREQ);
309 	if (IS_ERR(mgbdev->i2c_clk)) {
310 		dev_err(dev, "failed to register I2C clock\n");
311 		return PTR_ERR(mgbdev->i2c_clk);
312 	}
313 	mgbdev->i2c_cl = clkdev_hw_create(mgbdev->i2c_clk, NULL, "xiic-i2c.%d",
314 					  id);
315 	if (!mgbdev->i2c_cl) {
316 		dev_err(dev, "failed to register I2C clockdev\n");
317 		rv = -ENOMEM;
318 		goto err_clk;
319 	}
320 
321 	mgbdev->i2c_pdev = platform_device_register_resndata(dev, "xiic-i2c",
322 							     id, i2c_resources,
323 							     ARRAY_SIZE(i2c_resources),
324 							     NULL, 0);
325 	if (IS_ERR(mgbdev->i2c_pdev)) {
326 		dev_err(dev, "failed to register I2C device\n");
327 		rv = PTR_ERR(mgbdev->i2c_pdev);
328 		goto err_clkdev;
329 	}
330 
331 	mgbdev->i2c_adap = get_i2c_adap(mgbdev->i2c_pdev);
332 	if (!mgbdev->i2c_adap) {
333 		dev_err(dev, "failed to get I2C adapter\n");
334 		rv = -EINVAL;
335 		goto err_pdev;
336 	}
337 
338 	mutex_init(&mgbdev->i2c_lock);
339 
340 	return 0;
341 
342 err_pdev:
343 	platform_device_unregister(mgbdev->i2c_pdev);
344 err_clkdev:
345 	clkdev_drop(mgbdev->i2c_cl);
346 err_clk:
347 	clk_hw_unregister(mgbdev->i2c_clk);
348 
349 	return rv;
350 }
351 
352 static void free_i2c(struct mgb4_dev *mgbdev)
353 {
354 	put_device(&mgbdev->i2c_adap->dev);
355 	platform_device_unregister(mgbdev->i2c_pdev);
356 	clkdev_drop(mgbdev->i2c_cl);
357 	clk_hw_unregister(mgbdev->i2c_clk);
358 }
359 
360 static int get_serial_number(struct mgb4_dev *mgbdev)
361 {
362 	struct device *dev = &mgbdev->pdev->dev;
363 	struct mtd_info *mtd;
364 	size_t rs;
365 	int rv;
366 
367 	mgbdev->serial_number = 0;
368 
369 	mtd = get_mtd_device_nm(mgbdev->data_part_name);
370 	if (IS_ERR(mtd)) {
371 		dev_warn(dev, "failed to get data MTD device\n");
372 		return -ENOENT;
373 	}
374 	rv = mtd_read(mtd, 0, sizeof(mgbdev->serial_number), &rs,
375 		      (u_char *)&mgbdev->serial_number);
376 	put_mtd_device(mtd);
377 	if (rv < 0 || rs != sizeof(mgbdev->serial_number)) {
378 		dev_warn(dev, "error reading MTD device\n");
379 		return -EIO;
380 	}
381 
382 	return 0;
383 }
384 
385 static const char *module_type_str(struct mgb4_dev *mgbdev)
386 {
387 	if (MGB4_IS_FPDL3(mgbdev))
388 		return "FPDL3";
389 	else if (MGB4_IS_GMSL3(mgbdev))
390 		return "GMSL3";
391 	else if (MGB4_IS_GMSL1(mgbdev))
392 		return "GMSL1";
393 	else
394 		return "UNKNOWN";
395 }
396 
397 static int get_module_version(struct mgb4_dev *mgbdev)
398 {
399 	struct device *dev = &mgbdev->pdev->dev;
400 	struct mgb4_i2c_client extender;
401 	s32 version;
402 	u32 fw_version;
403 	int rv;
404 
405 	rv = mgb4_i2c_init(&extender, mgbdev->i2c_adap, &extender_info, 8);
406 	if (rv < 0) {
407 		dev_err(dev, "failed to create extender I2C device\n");
408 		return rv;
409 	}
410 	version = mgb4_i2c_read_byte(&extender, 0x00);
411 	mgb4_i2c_free(&extender);
412 	if (version < 0) {
413 		dev_err(dev, "error reading module version\n");
414 		return -EIO;
415 	}
416 
417 	mgbdev->module_version = ~((u32)version) & 0xff;
418 	if (!(MGB4_IS_FPDL3(mgbdev) ||
419 	      MGB4_IS_GMSL3(mgbdev) ||
420 	      MGB4_IS_GMSL1(mgbdev))) {
421 		dev_err(dev, "unknown module type\n");
422 		return -EINVAL;
423 	}
424 	fw_version = mgb4_read_reg(&mgbdev->video, 0xC4) >> 24;
425 	if ((MGB4_IS_FPDL3(mgbdev) && fw_version != 1) ||
426 	    (MGB4_IS_GMSL3(mgbdev) && fw_version != 2) ||
427 	    (MGB4_IS_GMSL1(mgbdev) && fw_version != 3)) {
428 		dev_err(dev, "module/firmware type mismatch\n");
429 		return -EINVAL;
430 	}
431 
432 	dev_info(dev, "%s module detected\n", module_type_str(mgbdev));
433 
434 	return 0;
435 }
436 
437 static int map_regs(struct pci_dev *pdev, struct resource *res,
438 		    struct mgb4_regs *regs)
439 {
440 	int rv;
441 	resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
442 
443 	res->start += mapbase;
444 	res->end += mapbase;
445 
446 	rv = mgb4_regs_map(res, regs);
447 	if (rv < 0) {
448 		dev_err(&pdev->dev, "failed to map %s registers\n", res->name);
449 		return rv;
450 	}
451 
452 	return 0;
453 }
454 
455 static int init_xdma(struct mgb4_dev *mgbdev)
456 {
457 	struct xdma_platdata data;
458 	struct resource res[2] = { 0 };
459 	struct dma_slave_map *map;
460 	struct pci_dev *pdev = mgbdev->pdev;
461 	struct device *dev = &pdev->dev;
462 	int i;
463 
464 	res[0].start = pci_resource_start(pdev, MGB4_XDMA_BAR_ID);
465 	res[0].end = pci_resource_end(pdev, MGB4_XDMA_BAR_ID);
466 	res[0].flags = IORESOURCE_MEM;
467 	res[0].parent = &pdev->resource[MGB4_XDMA_BAR_ID];
468 	res[1].start = pci_irq_vector(pdev, 0);
469 	res[1].end = res[1].start + MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES
470 		     + MGB4_USER_IRQS - 1;
471 	res[1].flags = IORESOURCE_IRQ;
472 
473 	data.max_dma_channels = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
474 	data.device_map = mgbdev->slave_map;
475 	data.device_map_cnt = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
476 
477 	for (i = 0; i < MGB4_VIN_DEVICES; i++) {
478 		sprintf(mgbdev->channel_names[i], "c2h%d", i);
479 		map = &data.device_map[i];
480 		map->slave = mgbdev->channel_names[i];
481 		map->devname = dev_name(dev);
482 		map->param = XDMA_FILTER_PARAM(&c2h_chan_info);
483 	}
484 	for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
485 		sprintf(mgbdev->channel_names[i + MGB4_VIN_DEVICES], "h2c%d", i);
486 		map = &data.device_map[i + MGB4_VIN_DEVICES];
487 		map->slave = mgbdev->channel_names[i + MGB4_VIN_DEVICES];
488 		map->devname = dev_name(dev);
489 		map->param = XDMA_FILTER_PARAM(&h2c_chan_info);
490 	}
491 
492 	mgbdev->xdev = platform_device_register_resndata(dev, "xdma",
493 							 PLATFORM_DEVID_AUTO, res,
494 							 2, &data, sizeof(data));
495 	if (IS_ERR(mgbdev->xdev)) {
496 		dev_err(dev, "failed to register XDMA device\n");
497 		return PTR_ERR(mgbdev->xdev);
498 	}
499 
500 	return 0;
501 }
502 
503 static void free_xdma(struct mgb4_dev *mgbdev)
504 {
505 	platform_device_unregister(mgbdev->xdev);
506 }
507 
508 static int mgb4_probe(struct pci_dev *pdev, const struct pci_device_id *id)
509 {
510 	int i, rv;
511 	struct mgb4_dev *mgbdev;
512 	struct resource video = {
513 		.start	= 0x0,
514 		.end	= 0xff,
515 		.flags	= IORESOURCE_MEM,
516 		.name	= "mgb4-video",
517 	};
518 	struct resource cmt = {
519 		.start	= 0x1000,
520 		.end	= 0x17ff,
521 		.flags	= IORESOURCE_MEM,
522 		.name	= "mgb4-cmt",
523 	};
524 	int irqs = pci_msix_vec_count(pdev);
525 
526 	mgbdev = kzalloc_obj(*mgbdev);
527 	if (!mgbdev)
528 		return -ENOMEM;
529 
530 	mgbdev->pdev = pdev;
531 	pci_set_drvdata(pdev, mgbdev);
532 
533 	/* PCIe related stuff */
534 	rv = pci_enable_device(pdev);
535 	if (rv) {
536 		dev_err(&pdev->dev, "error enabling PCI device\n");
537 		goto err_mgbdev;
538 	}
539 
540 	rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
541 	if (rv)
542 		dev_warn(&pdev->dev, "error enabling PCIe relaxed ordering\n");
543 	rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_EXT_TAG);
544 	if (rv)
545 		dev_warn(&pdev->dev, "error enabling PCIe extended tag field\n");
546 	rv = pcie_set_readrq(pdev, 512);
547 	if (rv)
548 		dev_warn(&pdev->dev, "error setting PCIe max. memory read size\n");
549 	pci_set_master(pdev);
550 
551 	rv = pci_alloc_irq_vectors(pdev, irqs, irqs, PCI_IRQ_MSIX);
552 	if (rv < 0) {
553 		dev_err(&pdev->dev, "error allocating MSI-X IRQs\n");
554 		goto err_enable_pci;
555 	}
556 
557 	rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
558 	if (rv) {
559 		dev_err(&pdev->dev, "error setting DMA mask\n");
560 		goto err_enable_pci;
561 	}
562 
563 	/* DMA + IRQ engine */
564 	rv = init_xdma(mgbdev);
565 	if (rv)
566 		goto err_alloc_irq;
567 	rv = mgb4_dma_channel_init(mgbdev);
568 	if (rv)
569 		goto err_dma_chan;
570 
571 	/* mgb4 video registers */
572 	rv = map_regs(pdev, &video, &mgbdev->video);
573 	if (rv < 0)
574 		goto err_dma_chan;
575 	/* mgb4 cmt registers */
576 	rv = map_regs(pdev, &cmt, &mgbdev->cmt);
577 	if (rv < 0)
578 		goto err_video_regs;
579 
580 	/* SPI FLASH */
581 	rv = init_spi(mgbdev, id->device);
582 	if (rv < 0)
583 		goto err_cmt_regs;
584 
585 	/* I2C controller */
586 	rv = init_i2c(mgbdev);
587 	if (rv < 0)
588 		goto err_spi;
589 
590 	/* PCI card related sysfs attributes */
591 	rv = device_add_groups(&pdev->dev, mgb4_pci_groups);
592 	if (rv < 0)
593 		goto err_i2c;
594 
595 #if IS_REACHABLE(CONFIG_HWMON)
596 	/* HWmon (card temperature) */
597 	mgbdev->hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "mgb4",
598 							    mgbdev,
599 							    &temp_chip_info,
600 							    NULL);
601 #endif
602 
603 	mgbdev->debugfs = debugfs_create_dir(dev_name(&pdev->dev), NULL);
604 
605 	/* Get card serial number. On systems without MTD flash support we may
606 	 * get an error thus ignore the return value. An invalid serial number
607 	 * should not break anything...
608 	 */
609 	if (get_serial_number(mgbdev) < 0)
610 		dev_warn(&pdev->dev, "error reading card serial number\n");
611 
612 	/* Get module type. If no valid module is found, skip the video device
613 	 * creation part but do not exit with error to allow flashing the card.
614 	 */
615 	rv = get_module_version(mgbdev);
616 	if (rv < 0)
617 		goto exit;
618 	/* Propagate the module type(version) to the FPGA */
619 	mgb4_write_reg(&mgbdev->video, 0xD4, mgbdev->module_version);
620 
621 	/* Video input v4l2 devices */
622 	for (i = 0; i < MGB4_VIN_DEVICES; i++)
623 		mgbdev->vin[i] = mgb4_vin_create(mgbdev, i);
624 
625 	/* Video output v4l2 devices */
626 	if (MGB4_HAS_VOUT(mgbdev)) {
627 		for (i = 0; i < MGB4_VOUT_DEVICES; i++)
628 			mgbdev->vout[i] = mgb4_vout_create(mgbdev, i);
629 	}
630 
631 	/* Triggers */
632 	mgbdev->indio_dev = mgb4_trigger_create(mgbdev);
633 
634 exit:
635 	flashid++;
636 
637 	return 0;
638 
639 err_i2c:
640 	free_i2c(mgbdev);
641 err_spi:
642 	free_spi(mgbdev);
643 err_cmt_regs:
644 	mgb4_regs_free(&mgbdev->cmt);
645 err_video_regs:
646 	mgb4_regs_free(&mgbdev->video);
647 err_dma_chan:
648 	mgb4_dma_channel_free(mgbdev);
649 	free_xdma(mgbdev);
650 err_alloc_irq:
651 	pci_disable_msix(pdev);
652 err_enable_pci:
653 	pci_disable_device(pdev);
654 err_mgbdev:
655 	kfree(mgbdev);
656 
657 	return rv;
658 }
659 
660 static void mgb4_remove(struct pci_dev *pdev)
661 {
662 	struct mgb4_dev *mgbdev = pci_get_drvdata(pdev);
663 	int i;
664 
665 #if IS_REACHABLE(CONFIG_HWMON)
666 	hwmon_device_unregister(mgbdev->hwmon_dev);
667 #endif
668 
669 	debugfs_remove_recursive(mgbdev->debugfs);
670 
671 	if (mgbdev->indio_dev)
672 		mgb4_trigger_free(mgbdev->indio_dev);
673 
674 	for (i = 0; i < MGB4_VOUT_DEVICES; i++)
675 		if (mgbdev->vout[i])
676 			mgb4_vout_free(mgbdev->vout[i]);
677 	for (i = 0; i < MGB4_VIN_DEVICES; i++)
678 		if (mgbdev->vin[i])
679 			mgb4_vin_free(mgbdev->vin[i]);
680 
681 	device_remove_groups(&mgbdev->pdev->dev, mgb4_pci_groups);
682 	free_spi(mgbdev);
683 	free_i2c(mgbdev);
684 	mgb4_regs_free(&mgbdev->video);
685 	mgb4_regs_free(&mgbdev->cmt);
686 
687 	mgb4_dma_channel_free(mgbdev);
688 	free_xdma(mgbdev);
689 
690 	pci_disable_msix(mgbdev->pdev);
691 	pci_disable_device(mgbdev->pdev);
692 
693 	kfree(mgbdev);
694 }
695 
696 static const struct pci_device_id mgb4_pci_ids[] = {
697 	{ PCI_DEVICE(DIGITEQ_VID, T100_DID), },
698 	{ PCI_DEVICE(DIGITEQ_VID, T200_DID), },
699 	{ 0, }
700 };
701 MODULE_DEVICE_TABLE(pci, mgb4_pci_ids);
702 
703 static struct pci_driver mgb4_pci_driver = {
704 	.name = KBUILD_MODNAME,
705 	.id_table = mgb4_pci_ids,
706 	.probe = mgb4_probe,
707 	.remove = mgb4_remove,
708 };
709 
710 module_pci_driver(mgb4_pci_driver);
711 
712 MODULE_AUTHOR("Digiteq Automotive s.r.o.");
713 MODULE_DESCRIPTION("Digiteq Automotive MGB4 Driver");
714 MODULE_LICENSE("GPL");
715 MODULE_SOFTDEP("pre: platform:xiic-i2c platform:xilinx_spi spi-nor");
716