Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * CE4100 PCI-I2C glue code for PXA's driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * The CE4100's I2C device is more or less the same one as found on PXA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * It does not support slave mode, the register slightly moved. This PCI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * device provides three bars, every contains a single I2C controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/platform_data/i2c-pxa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #define CE4100_PCI_I2C_DEVS	3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) struct ce4100_devices {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct platform_device *pdev[CE4100_PCI_I2C_DEVS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static struct platform_device *add_i2c_device(struct pci_dev *dev, int bar)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct i2c_pxa_platform_data pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct resource res[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	static int devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	memset(&pdata, 0, sizeof(struct i2c_pxa_platform_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	memset(&res, 0, sizeof(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	res[0].flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	res[0].start = pci_resource_start(dev, bar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	res[0].end = pci_resource_end(dev, bar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	res[1].flags = IORESOURCE_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	res[1].start = dev->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	res[1].end = dev->irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	for_each_child_of_node(dev->dev.of_node, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 		const void *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		struct resource r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		ret = of_address_to_resource(child, 0, &r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		if (r.start != res[0].start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (r.end != res[0].end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		if (r.flags != res[0].flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		prop = of_get_property(child, "fast-mode", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		if (prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 			pdata.fast_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (!child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		dev_err(&dev->dev, "failed to match a DT node for bar %d.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 				bar);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	pdev = platform_device_alloc("ce4100-i2c", devnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!pdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	pdev->dev.parent = &dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	pdev->dev.of_node = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	ret = platform_device_add_resources(pdev, res, ARRAY_SIZE(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	ret = platform_device_add_data(pdev, &pdata, sizeof(pdata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	ret = platform_device_add(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	devnum++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	platform_device_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static int ce4100_i2c_probe(struct pci_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		const struct pci_device_id *ent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct ce4100_devices *sds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ret = pci_enable_device_mem(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (!dev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		dev_err(&dev->dev, "Missing device tree node.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	sds = kzalloc(sizeof(*sds), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!sds) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		goto err_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	for (i = 0; i < ARRAY_SIZE(sds->pdev); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		sds->pdev[i] = add_i2c_device(dev, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		if (IS_ERR(sds->pdev[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			ret = PTR_ERR(sds->pdev[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				platform_device_unregister(sds->pdev[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			goto err_dev_add;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	pci_set_drvdata(dev, sds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) err_dev_add:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	kfree(sds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	pci_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static const struct pci_device_id ce4100_i2c_devices[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x2e68)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static struct pci_driver ce4100_i2c_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		.suppress_bind_attrs = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	.name           = "ce4100_i2c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	.id_table       = ce4100_i2c_devices,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	.probe          = ce4100_i2c_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) builtin_pci_driver(ce4100_i2c_driver);