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 <[email protected]>
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)
temp_is_visible(const void * data,enum hwmon_sensor_types type,u32 attr,int channel)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 
temp_read(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,long * val)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 
temp_read_string(struct device * dev,enum hwmon_sensor_types type,u32 attr,int channel,const char ** str)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 
match_i2c_adap(struct device * dev,const void * data)128 static int match_i2c_adap(struct device *dev, const void *data)
129 {
130 	return i2c_verify_adapter(dev) ? 1 : 0;
131 }
132 
get_i2c_adap(struct platform_device * pdev)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 
match_spi_adap(struct device * dev,const void * data)144 static int match_spi_adap(struct device *dev, const void *data)
145 {
146 	return to_spi_device(dev) ? 1 : 0;
147 }
148 
get_spi_adap(struct platform_device * pdev)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 
init_spi(struct mgb4_dev * mgbdev,u32 devid)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 
free_spi(struct mgb4_dev * mgbdev)263 static void free_spi(struct mgb4_dev *mgbdev)
264 {
265 	platform_device_unregister(mgbdev->spi_pdev);
266 }
267 
init_i2c(struct mgb4_dev * mgbdev)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 
free_i2c(struct mgb4_dev * mgbdev)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 
get_serial_number(struct mgb4_dev * mgbdev)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 
get_module_version(struct mgb4_dev * mgbdev)384 static int get_module_version(struct mgb4_dev *mgbdev)
385 {
386 	struct device *dev = &mgbdev->pdev->dev;
387 	struct mgb4_i2c_client extender;
388 	s32 version;
389 	u32 fw_version;
390 	int rv;
391 
392 	rv = mgb4_i2c_init(&extender, mgbdev->i2c_adap, &extender_info, 8);
393 	if (rv < 0) {
394 		dev_err(dev, "failed to create extender I2C device\n");
395 		return rv;
396 	}
397 	version = mgb4_i2c_read_byte(&extender, 0x00);
398 	mgb4_i2c_free(&extender);
399 	if (version < 0) {
400 		dev_err(dev, "error reading module version\n");
401 		return -EIO;
402 	}
403 
404 	mgbdev->module_version = ~((u32)version) & 0xff;
405 	if (!(MGB4_IS_FPDL3(mgbdev) || MGB4_IS_GMSL(mgbdev))) {
406 		dev_err(dev, "unknown module type\n");
407 		return -EINVAL;
408 	}
409 	fw_version = mgb4_read_reg(&mgbdev->video, 0xC4);
410 	if (fw_version >> 24 != mgbdev->module_version >> 4) {
411 		dev_err(dev, "module/firmware type mismatch\n");
412 		return -EINVAL;
413 	}
414 
415 	dev_info(dev, "%s module detected\n",
416 		 MGB4_IS_FPDL3(mgbdev) ? "FPDL3" : "GMSL");
417 
418 	return 0;
419 }
420 
map_regs(struct pci_dev * pdev,struct resource * res,struct mgb4_regs * regs)421 static int map_regs(struct pci_dev *pdev, struct resource *res,
422 		    struct mgb4_regs *regs)
423 {
424 	int rv;
425 	resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
426 
427 	res->start += mapbase;
428 	res->end += mapbase;
429 
430 	rv = mgb4_regs_map(res, regs);
431 	if (rv < 0) {
432 		dev_err(&pdev->dev, "failed to map %s registers\n", res->name);
433 		return rv;
434 	}
435 
436 	return 0;
437 }
438 
init_xdma(struct mgb4_dev * mgbdev)439 static int init_xdma(struct mgb4_dev *mgbdev)
440 {
441 	struct xdma_platdata data;
442 	struct resource res[2] = { 0 };
443 	struct dma_slave_map *map;
444 	struct pci_dev *pdev = mgbdev->pdev;
445 	struct device *dev = &pdev->dev;
446 	int i;
447 
448 	res[0].start = pci_resource_start(pdev, MGB4_XDMA_BAR_ID);
449 	res[0].end = pci_resource_end(pdev, MGB4_XDMA_BAR_ID);
450 	res[0].flags = IORESOURCE_MEM;
451 	res[0].parent = &pdev->resource[MGB4_XDMA_BAR_ID];
452 	res[1].start = pci_irq_vector(pdev, 0);
453 	res[1].end = res[1].start + MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES
454 		     + MGB4_USER_IRQS - 1;
455 	res[1].flags = IORESOURCE_IRQ;
456 
457 	data.max_dma_channels = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
458 	data.device_map = mgbdev->slave_map;
459 	data.device_map_cnt = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
460 
461 	for (i = 0; i < MGB4_VIN_DEVICES; i++) {
462 		sprintf(mgbdev->channel_names[i], "c2h%d", i);
463 		map = &data.device_map[i];
464 		map->slave = mgbdev->channel_names[i];
465 		map->devname = dev_name(dev);
466 		map->param = XDMA_FILTER_PARAM(&c2h_chan_info);
467 	}
468 	for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
469 		sprintf(mgbdev->channel_names[i + MGB4_VIN_DEVICES], "h2c%d", i);
470 		map = &data.device_map[i + MGB4_VIN_DEVICES];
471 		map->slave = mgbdev->channel_names[i + MGB4_VIN_DEVICES];
472 		map->devname = dev_name(dev);
473 		map->param = XDMA_FILTER_PARAM(&h2c_chan_info);
474 	}
475 
476 	mgbdev->xdev = platform_device_register_resndata(dev, "xdma",
477 							 PLATFORM_DEVID_AUTO, res,
478 							 2, &data, sizeof(data));
479 	if (IS_ERR(mgbdev->xdev)) {
480 		dev_err(dev, "failed to register XDMA device\n");
481 		return PTR_ERR(mgbdev->xdev);
482 	}
483 
484 	return 0;
485 }
486 
free_xdma(struct mgb4_dev * mgbdev)487 static void free_xdma(struct mgb4_dev *mgbdev)
488 {
489 	platform_device_unregister(mgbdev->xdev);
490 }
491 
mgb4_probe(struct pci_dev * pdev,const struct pci_device_id * id)492 static int mgb4_probe(struct pci_dev *pdev, const struct pci_device_id *id)
493 {
494 	int i, rv;
495 	struct mgb4_dev *mgbdev;
496 	struct resource video = {
497 		.start	= 0x0,
498 		.end	= 0xff,
499 		.flags	= IORESOURCE_MEM,
500 		.name	= "mgb4-video",
501 	};
502 	struct resource cmt = {
503 		.start	= 0x1000,
504 		.end	= 0x17ff,
505 		.flags	= IORESOURCE_MEM,
506 		.name	= "mgb4-cmt",
507 	};
508 	int irqs = pci_msix_vec_count(pdev);
509 
510 	mgbdev = kzalloc(sizeof(*mgbdev), GFP_KERNEL);
511 	if (!mgbdev)
512 		return -ENOMEM;
513 
514 	mgbdev->pdev = pdev;
515 	pci_set_drvdata(pdev, mgbdev);
516 
517 	/* PCIe related stuff */
518 	rv = pci_enable_device(pdev);
519 	if (rv) {
520 		dev_err(&pdev->dev, "error enabling PCI device\n");
521 		goto err_mgbdev;
522 	}
523 
524 	rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
525 	if (rv)
526 		dev_warn(&pdev->dev, "error enabling PCIe relaxed ordering\n");
527 	rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_EXT_TAG);
528 	if (rv)
529 		dev_warn(&pdev->dev, "error enabling PCIe extended tag field\n");
530 	rv = pcie_set_readrq(pdev, 512);
531 	if (rv)
532 		dev_warn(&pdev->dev, "error setting PCIe max. memory read size\n");
533 	pci_set_master(pdev);
534 
535 	rv = pci_alloc_irq_vectors(pdev, irqs, irqs, PCI_IRQ_MSIX);
536 	if (rv < 0) {
537 		dev_err(&pdev->dev, "error allocating MSI-X IRQs\n");
538 		goto err_enable_pci;
539 	}
540 
541 	rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
542 	if (rv) {
543 		dev_err(&pdev->dev, "error setting DMA mask\n");
544 		goto err_enable_pci;
545 	}
546 
547 	/* DMA + IRQ engine */
548 	rv = init_xdma(mgbdev);
549 	if (rv)
550 		goto err_alloc_irq;
551 	rv = mgb4_dma_channel_init(mgbdev);
552 	if (rv)
553 		goto err_dma_chan;
554 
555 	/* mgb4 video registers */
556 	rv = map_regs(pdev, &video, &mgbdev->video);
557 	if (rv < 0)
558 		goto err_dma_chan;
559 	/* mgb4 cmt registers */
560 	rv = map_regs(pdev, &cmt, &mgbdev->cmt);
561 	if (rv < 0)
562 		goto err_video_regs;
563 
564 	/* SPI FLASH */
565 	rv = init_spi(mgbdev, id->device);
566 	if (rv < 0)
567 		goto err_cmt_regs;
568 
569 	/* I2C controller */
570 	rv = init_i2c(mgbdev);
571 	if (rv < 0)
572 		goto err_spi;
573 
574 	/* PCI card related sysfs attributes */
575 	rv = device_add_groups(&pdev->dev, mgb4_pci_groups);
576 	if (rv < 0)
577 		goto err_i2c;
578 
579 #if IS_REACHABLE(CONFIG_HWMON)
580 	/* HWmon (card temperature) */
581 	mgbdev->hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "mgb4",
582 							    mgbdev,
583 							    &temp_chip_info,
584 							    NULL);
585 #endif
586 
587 	mgbdev->debugfs = debugfs_create_dir(dev_name(&pdev->dev), NULL);
588 
589 	/* Get card serial number. On systems without MTD flash support we may
590 	 * get an error thus ignore the return value. An invalid serial number
591 	 * should not break anything...
592 	 */
593 	if (get_serial_number(mgbdev) < 0)
594 		dev_warn(&pdev->dev, "error reading card serial number\n");
595 
596 	/* Get module type. If no valid module is found, skip the video device
597 	 * creation part but do not exit with error to allow flashing the card.
598 	 */
599 	rv = get_module_version(mgbdev);
600 	if (rv < 0)
601 		goto exit;
602 
603 	/* Video input v4l2 devices */
604 	for (i = 0; i < MGB4_VIN_DEVICES; i++)
605 		mgbdev->vin[i] = mgb4_vin_create(mgbdev, i);
606 
607 	/* Video output v4l2 devices */
608 	for (i = 0; i < MGB4_VOUT_DEVICES; i++)
609 		mgbdev->vout[i] = mgb4_vout_create(mgbdev, i);
610 
611 	/* Triggers */
612 	mgbdev->indio_dev = mgb4_trigger_create(mgbdev);
613 
614 exit:
615 	flashid++;
616 
617 	return 0;
618 
619 err_i2c:
620 	free_i2c(mgbdev);
621 err_spi:
622 	free_spi(mgbdev);
623 err_cmt_regs:
624 	mgb4_regs_free(&mgbdev->cmt);
625 err_video_regs:
626 	mgb4_regs_free(&mgbdev->video);
627 err_dma_chan:
628 	mgb4_dma_channel_free(mgbdev);
629 	free_xdma(mgbdev);
630 err_alloc_irq:
631 	pci_disable_msix(pdev);
632 err_enable_pci:
633 	pci_disable_device(pdev);
634 err_mgbdev:
635 	kfree(mgbdev);
636 
637 	return rv;
638 }
639 
mgb4_remove(struct pci_dev * pdev)640 static void mgb4_remove(struct pci_dev *pdev)
641 {
642 	struct mgb4_dev *mgbdev = pci_get_drvdata(pdev);
643 	int i;
644 
645 #if IS_REACHABLE(CONFIG_HWMON)
646 	hwmon_device_unregister(mgbdev->hwmon_dev);
647 #endif
648 
649 	debugfs_remove_recursive(mgbdev->debugfs);
650 
651 	if (mgbdev->indio_dev)
652 		mgb4_trigger_free(mgbdev->indio_dev);
653 
654 	for (i = 0; i < MGB4_VOUT_DEVICES; i++)
655 		if (mgbdev->vout[i])
656 			mgb4_vout_free(mgbdev->vout[i]);
657 	for (i = 0; i < MGB4_VIN_DEVICES; i++)
658 		if (mgbdev->vin[i])
659 			mgb4_vin_free(mgbdev->vin[i]);
660 
661 	device_remove_groups(&mgbdev->pdev->dev, mgb4_pci_groups);
662 	free_spi(mgbdev);
663 	free_i2c(mgbdev);
664 	mgb4_regs_free(&mgbdev->video);
665 	mgb4_regs_free(&mgbdev->cmt);
666 
667 	mgb4_dma_channel_free(mgbdev);
668 	free_xdma(mgbdev);
669 
670 	pci_disable_msix(mgbdev->pdev);
671 	pci_disable_device(mgbdev->pdev);
672 
673 	kfree(mgbdev);
674 }
675 
676 static const struct pci_device_id mgb4_pci_ids[] = {
677 	{ PCI_DEVICE(DIGITEQ_VID, T100_DID), },
678 	{ PCI_DEVICE(DIGITEQ_VID, T200_DID), },
679 	{ 0, }
680 };
681 MODULE_DEVICE_TABLE(pci, mgb4_pci_ids);
682 
683 static struct pci_driver mgb4_pci_driver = {
684 	.name = KBUILD_MODNAME,
685 	.id_table = mgb4_pci_ids,
686 	.probe = mgb4_probe,
687 	.remove = mgb4_remove,
688 };
689 
690 module_pci_driver(mgb4_pci_driver);
691 
692 MODULE_AUTHOR("Digiteq Automotive s.r.o.");
693 MODULE_DESCRIPTION("Digiteq Automotive MGB4 Driver");
694 MODULE_LICENSE("GPL");
695 MODULE_SOFTDEP("pre: platform:xiic-i2c platform:xilinx_spi spi-nor");
696