Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2007 PA Semi, Inc
0004  *
0005  * Parts based on arch/powerpc/sysdev/fsl_soc.c:
0006  *
0007  * 2006 (c) MontaVista Software, Inc.
0008  */
0009 
0010 #include <linux/errno.h>
0011 #include <linux/kernel.h>
0012 #include <linux/pci.h>
0013 #include <linux/of.h>
0014 #include <linux/of_irq.h>
0015 #include <linux/i2c.h>
0016 
0017 #ifdef CONFIG_I2C_BOARDINFO
0018 /* The below is from fsl_soc.c.  It's copied because since there are no
0019  * official bus bindings at this time it doesn't make sense to share across
0020  * the platforms, even though they happen to be common.
0021  */
0022 struct i2c_driver_device {
0023     char    *of_device;
0024     char    *i2c_type;
0025 };
0026 
0027 static struct i2c_driver_device i2c_devices[] __initdata = {
0028     {"dallas,ds1338",  "ds1338"},
0029 };
0030 
0031 static int __init find_i2c_driver(struct device_node *node,
0032                      struct i2c_board_info *info)
0033 {
0034     int i;
0035 
0036     for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
0037         if (!of_device_is_compatible(node, i2c_devices[i].of_device))
0038             continue;
0039         if (strlcpy(info->type, i2c_devices[i].i2c_type,
0040                 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
0041             return -ENOMEM;
0042         return 0;
0043     }
0044     return -ENODEV;
0045 }
0046 
0047 static int __init pasemi_register_i2c_devices(void)
0048 {
0049     struct pci_dev *pdev;
0050     struct device_node *adap_node;
0051     struct device_node *node;
0052 
0053     pdev = NULL;
0054     while ((pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa003, pdev))) {
0055         adap_node = pci_device_to_OF_node(pdev);
0056 
0057         if (!adap_node)
0058             continue;
0059 
0060         for_each_child_of_node(adap_node, node) {
0061             struct i2c_board_info info = {};
0062             const u32 *addr;
0063             int len;
0064 
0065             addr = of_get_property(node, "reg", &len);
0066             if (!addr || len < sizeof(int) ||
0067                 *addr > (1 << 10) - 1) {
0068                 pr_warn("pasemi_register_i2c_devices: invalid i2c device entry\n");
0069                 continue;
0070             }
0071 
0072             info.irq = irq_of_parse_and_map(node, 0);
0073             if (!info.irq)
0074                 info.irq = -1;
0075 
0076             if (find_i2c_driver(node, &info) < 0)
0077                 continue;
0078 
0079             info.addr = *addr;
0080 
0081             i2c_register_board_info(PCI_FUNC(pdev->devfn), &info,
0082                         1);
0083         }
0084     }
0085     return 0;
0086 }
0087 device_initcall(pasemi_register_i2c_devices);
0088 #endif