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