Linux Audio

Check our new training course

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