1 // SPDX-License-Identifier: GPL-2.0-only
3 * This is the driver for the MGB4 video grabber card by Digiteq Automotive.
5 * Copyright (C) 2021-2023 Digiteq Automotive
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.
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.
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>
37 #include "mgb4_sysfs.h"
38 #include "mgb4_vout.h"
40 #include "mgb4_trigger.h"
41 #include "mgb4_core.h"
43 #define MGB4_USER_IRQS 16
45 #define DIGITEQ_VID 0x1ed8
46 #define T100_DID 0x0101
47 #define T200_DID 0x0201
49 ATTRIBUTE_GROUPS(mgb4_pci);
53 static struct xdma_chan_info h2c_chan_info = {
54 .dir = DMA_MEM_TO_DEV,
57 static struct xdma_chan_info c2h_chan_info = {
58 .dir = DMA_DEV_TO_MEM,
61 static struct xspi_platform_data spi_platform_data = {
66 static const struct i2c_board_info extender_info = {
67 I2C_BOARD_INFO("extender", 0x21)
70 #if IS_REACHABLE(CONFIG_HWMON)
71 static umode_t temp_is_visible(const void *data, enum hwmon_sensor_types type,
72 u32 attr, int channel)
74 if (type == hwmon_temp &&
75 (attr == hwmon_temp_input || attr == hwmon_temp_label))
81 static int temp_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
82 int channel, long *val)
84 struct mgb4_dev *mgbdev = dev_get_drvdata(dev);
87 if (type != hwmon_temp || attr != hwmon_temp_input)
90 raw = mgb4_read_reg(&mgbdev->video, 0xD0);
91 /* register value -> Celsius degrees formula given by Xilinx */
92 val10 = ((((raw >> 20) & 0xFFF) * 503975) - 1118822400) / 409600;
98 static int temp_read_string(struct device *dev, enum hwmon_sensor_types type,
99 u32 attr, int channel, const char **str)
101 if (type != hwmon_temp || attr != hwmon_temp_label)
104 *str = "FPGA Temperature";
109 static const struct hwmon_ops temp_ops = {
110 .is_visible = temp_is_visible,
112 .read_string = temp_read_string
115 static const struct hwmon_channel_info *temp_channel_info[] = {
116 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
120 static const struct hwmon_chip_info temp_chip_info = {
122 .info = temp_channel_info,
126 static int match_i2c_adap(struct device *dev, void *data)
128 return i2c_verify_adapter(dev) ? 1 : 0;
131 static struct i2c_adapter *get_i2c_adap(struct platform_device *pdev)
135 mutex_lock(&pdev->dev.mutex);
136 dev = device_find_child(&pdev->dev, NULL, match_i2c_adap);
137 mutex_unlock(&pdev->dev.mutex);
139 return dev ? to_i2c_adapter(dev) : NULL;
142 static int match_spi_adap(struct device *dev, void *data)
144 return to_spi_device(dev) ? 1 : 0;
147 static struct spi_controller *get_spi_adap(struct platform_device *pdev)
151 mutex_lock(&pdev->dev.mutex);
152 dev = device_find_child(&pdev->dev, NULL, match_spi_adap);
153 mutex_unlock(&pdev->dev.mutex);
155 return dev ? container_of(dev, struct spi_controller, dev) : NULL;
158 static int init_spi(struct mgb4_dev *mgbdev, u32 devid)
160 struct resource spi_resources[] = {
164 .flags = IORESOURCE_MEM,
170 .flags = IORESOURCE_IRQ,
174 struct spi_board_info spi_info = {
175 .max_speed_hz = 10000000,
176 .modalias = "m25p80",
180 struct pci_dev *pdev = mgbdev->pdev;
181 struct device *dev = &pdev->dev;
182 struct spi_controller *ctlr;
183 struct spi_device *spi_dev;
186 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
188 request_module("platform:xilinx_spi");
190 irq = xdma_get_user_irq(mgbdev->xdev, 14);
191 xdma_enable_user_irq(mgbdev->xdev, irq);
193 spi_resources[0].parent = &pdev->resource[MGB4_MGB4_BAR_ID];
194 spi_resources[0].start += mapbase;
195 spi_resources[0].end += mapbase;
196 spi_resources[1].start = irq;
197 spi_resources[1].end = irq;
199 id = pci_dev_id(pdev);
200 mgbdev->spi_pdev = platform_device_register_resndata(dev, "xilinx_spi",
202 ARRAY_SIZE(spi_resources),
204 sizeof(spi_platform_data));
205 if (IS_ERR(mgbdev->spi_pdev)) {
206 dev_err(dev, "failed to register SPI device\n");
207 return PTR_ERR(mgbdev->spi_pdev);
210 ctlr = get_spi_adap(mgbdev->spi_pdev);
212 dev_err(dev, "failed to get SPI adapter\n");
217 snprintf(mgbdev->fw_part_name, sizeof(mgbdev->fw_part_name),
218 "mgb4-fw.%d", flashid);
219 mgbdev->partitions[0].name = mgbdev->fw_part_name;
220 if (devid == T200_DID) {
221 mgbdev->partitions[0].size = 0x950000;
222 mgbdev->partitions[0].offset = 0x1000000;
224 mgbdev->partitions[0].size = 0x400000;
225 mgbdev->partitions[0].offset = 0x400000;
227 mgbdev->partitions[0].mask_flags = 0;
229 snprintf(mgbdev->data_part_name, sizeof(mgbdev->data_part_name),
230 "mgb4-data.%d", flashid);
231 mgbdev->partitions[1].name = mgbdev->data_part_name;
232 mgbdev->partitions[1].size = 0x10000;
233 mgbdev->partitions[1].offset = 0xFF0000;
234 mgbdev->partitions[1].mask_flags = MTD_CAP_NORFLASH;
236 snprintf(mgbdev->flash_name, sizeof(mgbdev->flash_name),
237 "mgb4-flash.%d", flashid);
238 mgbdev->flash_data.name = mgbdev->flash_name;
239 mgbdev->flash_data.parts = mgbdev->partitions;
240 mgbdev->flash_data.nr_parts = ARRAY_SIZE(mgbdev->partitions);
241 mgbdev->flash_data.type = "spi-nor";
243 spi_info.platform_data = &mgbdev->flash_data;
245 spi_dev = spi_new_device(ctlr, &spi_info);
246 put_device(&ctlr->dev);
248 dev_err(dev, "failed to create MTD device\n");
256 platform_device_unregister(mgbdev->spi_pdev);
261 static void free_spi(struct mgb4_dev *mgbdev)
263 platform_device_unregister(mgbdev->spi_pdev);
266 static int init_i2c(struct mgb4_dev *mgbdev)
268 struct resource i2c_resources[] = {
272 .flags = IORESOURCE_MEM,
278 .flags = IORESOURCE_IRQ,
282 struct pci_dev *pdev = mgbdev->pdev;
283 struct device *dev = &pdev->dev;
287 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
289 request_module("platform:xiic-i2c");
291 irq = xdma_get_user_irq(mgbdev->xdev, 15);
292 xdma_enable_user_irq(mgbdev->xdev, irq);
294 i2c_resources[0].parent = &pdev->resource[MGB4_MGB4_BAR_ID];
295 i2c_resources[0].start += mapbase;
296 i2c_resources[0].end += mapbase;
297 i2c_resources[1].start = irq;
298 i2c_resources[1].end = irq;
300 id = pci_dev_id(pdev);
302 /* create dummy clock required by the xiic-i2c adapter */
303 snprintf(clk_name, sizeof(clk_name), "xiic-i2c.%d", id);
304 mgbdev->i2c_clk = clk_hw_register_fixed_rate(NULL, clk_name, NULL,
306 if (IS_ERR(mgbdev->i2c_clk)) {
307 dev_err(dev, "failed to register I2C clock\n");
308 return PTR_ERR(mgbdev->i2c_clk);
310 mgbdev->i2c_cl = clkdev_hw_create(mgbdev->i2c_clk, NULL, "xiic-i2c.%d",
312 if (!mgbdev->i2c_cl) {
313 dev_err(dev, "failed to register I2C clockdev\n");
318 mgbdev->i2c_pdev = platform_device_register_resndata(dev, "xiic-i2c",
320 ARRAY_SIZE(i2c_resources),
322 if (IS_ERR(mgbdev->i2c_pdev)) {
323 dev_err(dev, "failed to register I2C device\n");
324 rv = PTR_ERR(mgbdev->i2c_pdev);
328 mgbdev->i2c_adap = get_i2c_adap(mgbdev->i2c_pdev);
329 if (!mgbdev->i2c_adap) {
330 dev_err(dev, "failed to get I2C adapter\n");
335 mutex_init(&mgbdev->i2c_lock);
340 platform_device_unregister(mgbdev->i2c_pdev);
342 clkdev_drop(mgbdev->i2c_cl);
344 clk_hw_unregister(mgbdev->i2c_clk);
349 static void free_i2c(struct mgb4_dev *mgbdev)
351 put_device(&mgbdev->i2c_adap->dev);
352 platform_device_unregister(mgbdev->i2c_pdev);
353 clkdev_drop(mgbdev->i2c_cl);
354 clk_hw_unregister(mgbdev->i2c_clk);
357 static int get_serial_number(struct mgb4_dev *mgbdev)
359 struct device *dev = &mgbdev->pdev->dev;
360 struct mtd_info *mtd;
364 mgbdev->serial_number = 0;
366 mtd = get_mtd_device_nm(mgbdev->data_part_name);
368 dev_warn(dev, "failed to get data MTD device\n");
371 rv = mtd_read(mtd, 0, sizeof(mgbdev->serial_number), &rs,
372 (u_char *)&mgbdev->serial_number);
374 if (rv < 0 || rs != sizeof(mgbdev->serial_number)) {
375 dev_warn(dev, "error reading MTD device\n");
382 static int get_module_version(struct mgb4_dev *mgbdev)
384 struct device *dev = &mgbdev->pdev->dev;
385 struct mgb4_i2c_client extender;
390 rv = mgb4_i2c_init(&extender, mgbdev->i2c_adap, &extender_info, 8);
392 dev_err(dev, "failed to create extender I2C device\n");
395 version = mgb4_i2c_read_byte(&extender, 0x00);
396 mgb4_i2c_free(&extender);
398 dev_err(dev, "error reading module version\n");
402 mgbdev->module_version = ~((u32)version) & 0xff;
403 if (!(MGB4_IS_FPDL3(mgbdev) || MGB4_IS_GMSL(mgbdev))) {
404 dev_err(dev, "unknown module type\n");
407 fw_version = mgb4_read_reg(&mgbdev->video, 0xC4);
408 if (fw_version >> 24 != mgbdev->module_version >> 4) {
409 dev_err(dev, "module/firmware type mismatch\n");
413 dev_info(dev, "%s module detected\n",
414 MGB4_IS_FPDL3(mgbdev) ? "FPDL3" : "GMSL");
419 static int map_regs(struct pci_dev *pdev, struct resource *res,
420 struct mgb4_regs *regs)
423 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
425 res->start += mapbase;
428 rv = mgb4_regs_map(res, regs);
430 dev_err(&pdev->dev, "failed to map %s registers\n", res->name);
437 static int init_xdma(struct mgb4_dev *mgbdev)
439 struct xdma_platdata data;
440 struct resource res[2] = { 0 };
441 struct dma_slave_map *map;
442 struct pci_dev *pdev = mgbdev->pdev;
443 struct device *dev = &pdev->dev;
446 res[0].start = pci_resource_start(pdev, MGB4_XDMA_BAR_ID);
447 res[0].end = pci_resource_end(pdev, MGB4_XDMA_BAR_ID);
448 res[0].flags = IORESOURCE_MEM;
449 res[0].parent = &pdev->resource[MGB4_XDMA_BAR_ID];
450 res[1].start = pci_irq_vector(pdev, 0);
451 res[1].end = res[1].start + MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES
452 + MGB4_USER_IRQS - 1;
453 res[1].flags = IORESOURCE_IRQ;
455 data.max_dma_channels = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
456 data.device_map = mgbdev->slave_map;
457 data.device_map_cnt = MGB4_VIN_DEVICES + MGB4_VOUT_DEVICES;
459 for (i = 0; i < MGB4_VIN_DEVICES; i++) {
460 sprintf(mgbdev->channel_names[i], "c2h%d", i);
461 map = &data.device_map[i];
462 map->slave = mgbdev->channel_names[i];
463 map->devname = dev_name(dev);
464 map->param = XDMA_FILTER_PARAM(&c2h_chan_info);
466 for (i = 0; i < MGB4_VOUT_DEVICES; i++) {
467 sprintf(mgbdev->channel_names[i + MGB4_VIN_DEVICES], "h2c%d", i);
468 map = &data.device_map[i + MGB4_VIN_DEVICES];
469 map->slave = mgbdev->channel_names[i + MGB4_VIN_DEVICES];
470 map->devname = dev_name(dev);
471 map->param = XDMA_FILTER_PARAM(&h2c_chan_info);
474 mgbdev->xdev = platform_device_register_resndata(dev, "xdma",
475 PLATFORM_DEVID_AUTO, res,
476 2, &data, sizeof(data));
477 if (IS_ERR(mgbdev->xdev)) {
478 dev_err(dev, "failed to register XDMA device\n");
479 return PTR_ERR(mgbdev->xdev);
485 static void free_xdma(struct mgb4_dev *mgbdev)
487 platform_device_unregister(mgbdev->xdev);
490 static int mgb4_probe(struct pci_dev *pdev, const struct pci_device_id *id)
493 struct mgb4_dev *mgbdev;
494 struct resource video = {
497 .flags = IORESOURCE_MEM,
498 .name = "mgb4-video",
500 struct resource cmt = {
503 .flags = IORESOURCE_MEM,
506 int irqs = pci_msix_vec_count(pdev);
508 mgbdev = kzalloc(sizeof(*mgbdev), GFP_KERNEL);
513 pci_set_drvdata(pdev, mgbdev);
515 /* PCIe related stuff */
516 rv = pci_enable_device(pdev);
518 dev_err(&pdev->dev, "error enabling PCI device\n");
522 rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
524 dev_warn(&pdev->dev, "error enabling PCIe relaxed ordering\n");
525 rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_EXT_TAG);
527 dev_warn(&pdev->dev, "error enabling PCIe extended tag field\n");
528 rv = pcie_set_readrq(pdev, 512);
530 dev_warn(&pdev->dev, "error setting PCIe max. memory read size\n");
531 pci_set_master(pdev);
533 rv = pci_alloc_irq_vectors(pdev, irqs, irqs, PCI_IRQ_MSIX);
535 dev_err(&pdev->dev, "error allocating MSI-X IRQs\n");
539 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
541 dev_err(&pdev->dev, "error setting DMA mask\n");
545 /* DMA + IRQ engine */
546 rv = init_xdma(mgbdev);
549 rv = mgb4_dma_channel_init(mgbdev);
553 /* mgb4 video registers */
554 rv = map_regs(pdev, &video, &mgbdev->video);
557 /* mgb4 cmt registers */
558 rv = map_regs(pdev, &cmt, &mgbdev->cmt);
563 rv = init_spi(mgbdev, id->device);
568 rv = init_i2c(mgbdev);
572 /* PCI card related sysfs attributes */
573 rv = device_add_groups(&pdev->dev, mgb4_pci_groups);
577 #if IS_REACHABLE(CONFIG_HWMON)
578 /* HWmon (card temperature) */
579 mgbdev->hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "mgb4",
585 #ifdef CONFIG_DEBUG_FS
586 mgbdev->debugfs = debugfs_create_dir(dev_name(&pdev->dev), NULL);
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...
593 if (get_serial_number(mgbdev) < 0)
594 dev_warn(&pdev->dev, "error reading card serial number\n");
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.
599 rv = get_module_version(mgbdev);
603 /* Video input v4l2 devices */
604 for (i = 0; i < MGB4_VIN_DEVICES; i++)
605 mgbdev->vin[i] = mgb4_vin_create(mgbdev, i);
607 /* Video output v4l2 devices */
608 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
609 mgbdev->vout[i] = mgb4_vout_create(mgbdev, i);
612 mgbdev->indio_dev = mgb4_trigger_create(mgbdev);
624 mgb4_regs_free(&mgbdev->cmt);
626 mgb4_regs_free(&mgbdev->video);
628 mgb4_dma_channel_free(mgbdev);
631 pci_disable_msix(pdev);
633 pci_disable_device(pdev);
640 static void mgb4_remove(struct pci_dev *pdev)
642 struct mgb4_dev *mgbdev = pci_get_drvdata(pdev);
645 #if IS_REACHABLE(CONFIG_HWMON)
646 hwmon_device_unregister(mgbdev->hwmon_dev);
649 if (mgbdev->indio_dev)
650 mgb4_trigger_free(mgbdev->indio_dev);
652 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
654 mgb4_vout_free(mgbdev->vout[i]);
655 for (i = 0; i < MGB4_VIN_DEVICES; i++)
657 mgb4_vin_free(mgbdev->vin[i]);
659 #ifdef CONFIG_DEBUG_FS
660 debugfs_remove_recursive(mgbdev->debugfs);
663 device_remove_groups(&mgbdev->pdev->dev, mgb4_pci_groups);
666 mgb4_regs_free(&mgbdev->video);
667 mgb4_regs_free(&mgbdev->cmt);
669 mgb4_dma_channel_free(mgbdev);
672 pci_disable_msix(mgbdev->pdev);
673 pci_disable_device(mgbdev->pdev);
678 static const struct pci_device_id mgb4_pci_ids[] = {
679 { PCI_DEVICE(DIGITEQ_VID, T100_DID), },
680 { PCI_DEVICE(DIGITEQ_VID, T200_DID), },
683 MODULE_DEVICE_TABLE(pci, mgb4_pci_ids);
685 static struct pci_driver mgb4_pci_driver = {
686 .name = KBUILD_MODNAME,
687 .id_table = mgb4_pci_ids,
689 .remove = mgb4_remove,
692 module_pci_driver(mgb4_pci_driver);
694 MODULE_AUTHOR("Digiteq Automotive s.r.o.");
695 MODULE_DESCRIPTION("Digiteq Automotive MGB4 Driver");
696 MODULE_LICENSE("GPL");
697 MODULE_SOFTDEP("pre: platform:xiic-i2c platform:xilinx_spi spi-nor");