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