// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (C) 2019 CERN (www.cern.ch)
 * Author: Federico Vaga <federico.vaga@cern.ch>
 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/mfd/core.h>
#include <linux/mod_devicetable.h>

enum wrtd_s150a_dev_offsets {
	WRTD_S150A_FA100_MEM_START = 0x00002000,
	WRTD_S150A_FA100_MEM_END = 0x00003A00,
	WRTD_S150A_TRTL_MEM_START = 0x0001E000,
	WRTD_S150A_TRTL_MEM_END = 0x0003E000,
};

/* MFD devices */
enum spec_fpga_mfd_devs_enum {
	WRTD_S150A_MFD_TRTL = 0,
	WRTD_S150A_MFD_FA100,
};

static struct resource wrtd_s150a_fa100_res[] = {
	{
		.name = "fmc-adc-100m14b4ch-mem",
		.flags = IORESOURCE_MEM,
		.start = WRTD_S150A_FA100_MEM_START,
		.end = WRTD_S150A_FA100_MEM_END,
	}, {
		.name = "fmc-adc-100m14b4ch-irq",
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL,
		.start = 0,
		.end = 0,
	},
};

static struct resource wrtd_s150a_trtl_res[] = {
	{
		.name = "mock-turtle-mem",
		.flags = IORESOURCE_MEM,
		.start = WRTD_S150A_TRTL_MEM_START,
		.end = WRTD_S150A_TRTL_MEM_END,
	}, {
		.name = "mock-turtle-irq_in",
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL,
		.start = 1,
	}, {
		.name = "mock-turtle-irq_out",
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL,
		.start = 2,
	}, {
		.name = "mock-turtle-irq_con",
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL,
		.start = 3,
	}, {
		.name = "mock-turtle-irq_not",
		.flags = IORESOURCE_IRQ | IORESOURCE_IRQ_HIGHLEVEL,
		.start = 4,
	},
};

static const struct mfd_cell wrtd_s150a_mfd_devs[] = {
	[WRTD_S150A_MFD_TRTL] = {
		.name = "mock-turtle",
		.platform_data = NULL,
		.pdata_size = 0,
		.num_resources = ARRAY_SIZE(wrtd_s150a_trtl_res),
		.resources = wrtd_s150a_trtl_res,
	},
	[WRTD_S150A_MFD_FA100] = {
		.name = "adc-100m-spec",
		.platform_data = NULL,
		.pdata_size = 0,
		.num_resources = ARRAY_SIZE(wrtd_s150a_fa100_res),
		.resources = wrtd_s150a_fa100_res,
	},
};


static int wrtd_s150a_probe(struct platform_device *pdev)
{
	struct resource *rmem;
	int irq;

	rmem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!rmem) {
		dev_err(&pdev->dev, "Missing memory resource\n");
		return -EINVAL;
	}

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		dev_err(&pdev->dev, "Missing IRQ number\n");
		return -EINVAL;
	}

	/*
	 * We know that this design uses the HTVIC IRQ controller.
	 * This IRQ controller has a linear mapping, so it is enough
	 * to give the first one as input
	 */

	return mfd_add_devices(&pdev->dev, PLATFORM_DEVID_AUTO,
			       wrtd_s150a_mfd_devs,
			       ARRAY_SIZE(wrtd_s150a_mfd_devs),
			       rmem, irq, NULL);
}

static int wrtd_s150a_remove(struct platform_device *pdev)
{
	mfd_remove_devices(&pdev->dev);

	return 0;
}

/**
 * List of supported platform
 */
enum wrtd_s150a_version {
	WRTD_S150A_VER = 0,
};

static const struct platform_device_id wrtd_s150a_id_table[] = {
	{
		.name = "wrtd-s150a",
		.driver_data = WRTD_S150A_VER,
	}, {
		.name = "id:000010DC57544E01",
		.driver_data = WRTD_S150A_VER,
	}, {
		.name = "id:000010dc57544e01",
		.driver_data = WRTD_S150A_VER,
	},
	{},
};

static struct platform_driver wrtd_s150a_driver = {
	.driver = {
		.name = "wrtd-s150a",
		.owner = THIS_MODULE,
	},
	.id_table = wrtd_s150a_id_table,
	.probe = wrtd_s150a_probe,
	.remove = wrtd_s150a_remove,
};
module_platform_driver(wrtd_s150a_driver);

MODULE_AUTHOR("Federico Vaga <federico.vaga@cern.ch>");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRV_VERSION);
MODULE_DESCRIPTION("Driver for the WRTD SPEC 150T FMC ADC");
MODULE_DEVICE_TABLE(platform, wrtd_s150a_id_table);

MODULE_SOFTDEP("pre: spec_fmc_carrier mockturtle fmc-adc-100m14b4ch");
