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
44 #define MGB4_MGB4_BAR_ID 0
45 #define MGB4_XDMA_BAR_ID 1
47 #define DIGITEQ_VID 0x1ed8
48 #define T100_DID 0x0101
49 #define T200_DID 0x0201
51 ATTRIBUTE_GROUPS(mgb4_pci);
55 static struct xdma_chan_info h2c_chan_info = {
56 .dir = DMA_MEM_TO_DEV,
59 static struct xdma_chan_info c2h_chan_info = {
60 .dir = DMA_DEV_TO_MEM,
63 static struct xspi_platform_data spi_platform_data = {
68 static const struct i2c_board_info extender_info = {
69 I2C_BOARD_INFO("extender", 0x21)
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)
76 if (type == hwmon_temp &&
77 (attr == hwmon_temp_input || attr == hwmon_temp_label))
83 static int temp_read(struct device *dev, enum hwmon_sensor_types type, u32 attr,
84 int channel, long *val)
86 struct mgb4_dev *mgbdev = dev_get_drvdata(dev);
89 if (type != hwmon_temp || attr != hwmon_temp_input)
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;
100 static int temp_read_string(struct device *dev, enum hwmon_sensor_types type,
101 u32 attr, int channel, const char **str)
103 if (type != hwmon_temp || attr != hwmon_temp_label)
106 *str = "FPGA Temperature";
111 static const struct hwmon_ops temp_ops = {
112 .is_visible = temp_is_visible,
114 .read_string = temp_read_string
117 static const struct hwmon_channel_info *temp_channel_info[] = {
118 HWMON_CHANNEL_INFO(temp, HWMON_T_INPUT | HWMON_T_LABEL),
122 static const struct hwmon_chip_info temp_chip_info = {
124 .info = temp_channel_info,
128 static int match_i2c_adap(struct device *dev, const void *data)
130 return i2c_verify_adapter(dev) ? 1 : 0;
133 static struct i2c_adapter *get_i2c_adap(struct platform_device *pdev)
137 mutex_lock(&pdev->dev.mutex);
138 dev = device_find_child(&pdev->dev, NULL, match_i2c_adap);
139 mutex_unlock(&pdev->dev.mutex);
141 return dev ? to_i2c_adapter(dev) : NULL;
144 static int match_spi_adap(struct device *dev, const void *data)
146 return to_spi_device(dev) ? 1 : 0;
149 static struct spi_controller *get_spi_adap(struct platform_device *pdev)
153 mutex_lock(&pdev->dev.mutex);
154 dev = device_find_child(&pdev->dev, NULL, match_spi_adap);
155 mutex_unlock(&pdev->dev.mutex);
157 return dev ? container_of(dev, struct spi_controller, dev) : NULL;
160 static int init_spi(struct mgb4_dev *mgbdev, u32 devid)
162 struct resource spi_resources[] = {
166 .flags = IORESOURCE_MEM,
172 .flags = IORESOURCE_IRQ,
176 struct spi_board_info spi_info = {
177 .max_speed_hz = 10000000,
178 .modalias = "m25p80",
182 struct pci_dev *pdev = mgbdev->pdev;
183 struct device *dev = &pdev->dev;
184 struct spi_controller *ctlr;
185 struct spi_device *spi_dev;
188 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
190 request_module("platform:xilinx_spi");
192 irq = xdma_get_user_irq(mgbdev->xdev, 14);
193 xdma_enable_user_irq(mgbdev->xdev, irq);
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;
201 id = pci_dev_id(pdev);
202 mgbdev->spi_pdev = platform_device_register_resndata(dev, "xilinx_spi",
204 ARRAY_SIZE(spi_resources),
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);
212 ctlr = get_spi_adap(mgbdev->spi_pdev);
214 dev_err(dev, "failed to get SPI adapter\n");
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;
226 mgbdev->partitions[0].size = 0x400000;
227 mgbdev->partitions[0].offset = 0x400000;
229 mgbdev->partitions[0].mask_flags = 0;
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;
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";
245 spi_info.platform_data = &mgbdev->flash_data;
247 spi_dev = spi_new_device(ctlr, &spi_info);
248 put_device(&ctlr->dev);
250 dev_err(dev, "failed to create MTD device\n");
258 platform_device_unregister(mgbdev->spi_pdev);
263 static void free_spi(struct mgb4_dev *mgbdev)
265 platform_device_unregister(mgbdev->spi_pdev);
268 static int init_i2c(struct mgb4_dev *mgbdev)
270 struct resource i2c_resources[] = {
274 .flags = IORESOURCE_MEM,
280 .flags = IORESOURCE_IRQ,
284 struct pci_dev *pdev = mgbdev->pdev;
285 struct device *dev = &pdev->dev;
289 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
291 request_module("platform:xiic-i2c");
293 irq = xdma_get_user_irq(mgbdev->xdev, 15);
294 xdma_enable_user_irq(mgbdev->xdev, irq);
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;
302 id = pci_dev_id(pdev);
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,
308 if (IS_ERR(mgbdev->i2c_clk)) {
309 dev_err(dev, "failed to register I2C clock\n");
310 return PTR_ERR(mgbdev->i2c_clk);
312 mgbdev->i2c_cl = clkdev_hw_create(mgbdev->i2c_clk, NULL, "xiic-i2c.%d",
314 if (!mgbdev->i2c_cl) {
315 dev_err(dev, "failed to register I2C clockdev\n");
320 mgbdev->i2c_pdev = platform_device_register_resndata(dev, "xiic-i2c",
322 ARRAY_SIZE(i2c_resources),
324 if (IS_ERR(mgbdev->i2c_pdev)) {
325 dev_err(dev, "failed to register I2C device\n");
326 rv = PTR_ERR(mgbdev->i2c_pdev);
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");
337 mutex_init(&mgbdev->i2c_lock);
342 platform_device_unregister(mgbdev->i2c_pdev);
344 clkdev_drop(mgbdev->i2c_cl);
346 clk_hw_unregister(mgbdev->i2c_clk);
351 static void free_i2c(struct mgb4_dev *mgbdev)
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);
359 static int get_serial_number(struct mgb4_dev *mgbdev)
361 struct device *dev = &mgbdev->pdev->dev;
362 struct mtd_info *mtd;
366 mgbdev->serial_number = 0;
368 mtd = get_mtd_device_nm(mgbdev->data_part_name);
370 dev_warn(dev, "failed to get data MTD device\n");
373 rv = mtd_read(mtd, 0, sizeof(mgbdev->serial_number), &rs,
374 (u_char *)&mgbdev->serial_number);
376 if (rv < 0 || rs != sizeof(mgbdev->serial_number)) {
377 dev_warn(dev, "error reading MTD device\n");
384 static int get_module_version(struct mgb4_dev *mgbdev)
386 struct device *dev = &mgbdev->pdev->dev;
387 struct mgb4_i2c_client extender;
392 rv = mgb4_i2c_init(&extender, mgbdev->i2c_adap, &extender_info, 8);
394 dev_err(dev, "failed to create extender I2C device\n");
397 version = mgb4_i2c_read_byte(&extender, 0x00);
398 mgb4_i2c_free(&extender);
400 dev_err(dev, "error reading module version\n");
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");
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");
415 dev_info(dev, "%s module detected\n",
416 MGB4_IS_FPDL3(mgbdev) ? "FPDL3" : "GMSL");
421 static int map_regs(struct pci_dev *pdev, struct resource *res,
422 struct mgb4_regs *regs)
425 resource_size_t mapbase = pci_resource_start(pdev, MGB4_MGB4_BAR_ID);
427 res->start += mapbase;
430 rv = mgb4_regs_map(res, regs);
432 dev_err(&pdev->dev, "failed to map %s registers\n", res->name);
439 static int init_xdma(struct mgb4_dev *mgbdev)
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;
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;
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;
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);
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);
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);
487 static void free_xdma(struct mgb4_dev *mgbdev)
489 platform_device_unregister(mgbdev->xdev);
492 static int mgb4_probe(struct pci_dev *pdev, const struct pci_device_id *id)
495 struct mgb4_dev *mgbdev;
496 struct resource video = {
499 .flags = IORESOURCE_MEM,
500 .name = "mgb4-video",
502 struct resource cmt = {
505 .flags = IORESOURCE_MEM,
508 int irqs = pci_msix_vec_count(pdev);
510 mgbdev = kzalloc(sizeof(*mgbdev), GFP_KERNEL);
515 pci_set_drvdata(pdev, mgbdev);
517 /* PCIe related stuff */
518 rv = pci_enable_device(pdev);
520 dev_err(&pdev->dev, "error enabling PCI device\n");
524 rv = pcie_capability_set_word(pdev, PCI_EXP_DEVCTL, PCI_EXP_DEVCTL_RELAX_EN);
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);
529 dev_warn(&pdev->dev, "error enabling PCIe extended tag field\n");
530 rv = pcie_set_readrq(pdev, 512);
532 dev_warn(&pdev->dev, "error setting PCIe max. memory read size\n");
533 pci_set_master(pdev);
535 rv = pci_alloc_irq_vectors(pdev, irqs, irqs, PCI_IRQ_MSIX);
537 dev_err(&pdev->dev, "error allocating MSI-X IRQs\n");
541 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
543 dev_err(&pdev->dev, "error setting DMA mask\n");
547 /* DMA + IRQ engine */
548 rv = init_xdma(mgbdev);
551 rv = mgb4_dma_channel_init(mgbdev);
555 /* mgb4 video registers */
556 rv = map_regs(pdev, &video, &mgbdev->video);
559 /* mgb4 cmt registers */
560 rv = map_regs(pdev, &cmt, &mgbdev->cmt);
565 rv = init_spi(mgbdev, id->device);
570 rv = init_i2c(mgbdev);
574 /* PCI card related sysfs attributes */
575 rv = device_add_groups(&pdev->dev, mgb4_pci_groups);
579 #if IS_REACHABLE(CONFIG_HWMON)
580 /* HWmon (card temperature) */
581 mgbdev->hwmon_dev = hwmon_device_register_with_info(&pdev->dev, "mgb4",
587 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 debugfs_remove_recursive(mgbdev->debugfs);
651 if (mgbdev->indio_dev)
652 mgb4_trigger_free(mgbdev->indio_dev);
654 for (i = 0; i < MGB4_VOUT_DEVICES; i++)
656 mgb4_vout_free(mgbdev->vout[i]);
657 for (i = 0; i < MGB4_VIN_DEVICES; i++)
659 mgb4_vin_free(mgbdev->vin[i]);
661 device_remove_groups(&mgbdev->pdev->dev, mgb4_pci_groups);
664 mgb4_regs_free(&mgbdev->video);
665 mgb4_regs_free(&mgbdev->cmt);
667 mgb4_dma_channel_free(mgbdev);
670 pci_disable_msix(mgbdev->pdev);
671 pci_disable_device(mgbdev->pdev);
676 static const struct pci_device_id mgb4_pci_ids[] = {
677 { PCI_DEVICE(DIGITEQ_VID, T100_DID), },
678 { PCI_DEVICE(DIGITEQ_VID, T200_DID), },
681 MODULE_DEVICE_TABLE(pci, mgb4_pci_ids);
683 static struct pci_driver mgb4_pci_driver = {
684 .name = KBUILD_MODNAME,
685 .id_table = mgb4_pci_ids,
687 .remove = mgb4_remove,
690 module_pci_driver(mgb4_pci_driver);
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");