xref: /linux/arch/powerpc/platforms/pasemi/misc.c (revision fc30eea1542dd787c6aa46e970014e97e390c5b2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2007 PA Semi, Inc
4  *
5  * Parts based on arch/powerpc/sysdev/fsl_soc.c:
6  *
7  * 2006 (c) MontaVista Software, Inc.
8  */
9 
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/pci.h>
13 #include <linux/of.h>
14 #include <linux/of_irq.h>
15 #include <linux/i2c.h>
16 
17 #ifdef CONFIG_I2C_BOARDINFO
18 /* The below is from fsl_soc.c.  It's copied because since there are no
19  * official bus bindings at this time it doesn't make sense to share across
20  * the platforms, even though they happen to be common.
21  */
22 struct i2c_driver_device {
23 	char    *of_device;
24 	char    *i2c_type;
25 };
26 
27 static struct i2c_driver_device i2c_devices[] __initdata = {
28 	{"dallas,ds1338",  "ds1338"},
29 };
30 
31 static int __init find_i2c_driver(struct device_node *node,
32 				     struct i2c_board_info *info)
33 {
34 	int i;
35 
36 	for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
37 		if (!of_device_is_compatible(node, i2c_devices[i].of_device))
38 			continue;
39 		if (strlcpy(info->type, i2c_devices[i].i2c_type,
40 			    I2C_NAME_SIZE) >= I2C_NAME_SIZE)
41 			return -ENOMEM;
42 		return 0;
43 	}
44 	return -ENODEV;
45 }
46 
47 static int __init pasemi_register_i2c_devices(void)
48 {
49 	struct pci_dev *pdev;
50 	struct device_node *adap_node;
51 	struct device_node *node;
52 
53 	pdev = NULL;
54 	while ((pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa003, pdev))) {
55 		adap_node = pci_device_to_OF_node(pdev);
56 
57 		if (!adap_node)
58 			continue;
59 
60 		for_each_child_of_node(adap_node, node) {
61 			struct i2c_board_info info = {};
62 			const u32 *addr;
63 			int len;
64 
65 			addr = of_get_property(node, "reg", &len);
66 			if (!addr || len < sizeof(int) ||
67 			    *addr > (1 << 10) - 1) {
68 				pr_warn("pasemi_register_i2c_devices: invalid i2c device entry\n");
69 				continue;
70 			}
71 
72 			info.irq = irq_of_parse_and_map(node, 0);
73 			if (!info.irq)
74 				info.irq = -1;
75 
76 			if (find_i2c_driver(node, &info) < 0)
77 				continue;
78 
79 			info.addr = *addr;
80 
81 			i2c_register_board_info(PCI_FUNC(pdev->devfn), &info,
82 						1);
83 		}
84 	}
85 	return 0;
86 }
87 device_initcall(pasemi_register_i2c_devices);
88 #endif
89