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