Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * i2c-boardinfo.c - collect pre-declarations of I2C devices
0004  */
0005 
0006 #include <linux/export.h>
0007 #include <linux/i2c.h>
0008 #include <linux/kernel.h>
0009 #include <linux/property.h>
0010 #include <linux/rwsem.h>
0011 #include <linux/slab.h>
0012 
0013 #include "i2c-core.h"
0014 
0015 
0016 /* These symbols are exported ONLY FOR the i2c core.
0017  * No other users will be supported.
0018  */
0019 DECLARE_RWSEM(__i2c_board_lock);
0020 EXPORT_SYMBOL_GPL(__i2c_board_lock);
0021 
0022 LIST_HEAD(__i2c_board_list);
0023 EXPORT_SYMBOL_GPL(__i2c_board_list);
0024 
0025 int __i2c_first_dynamic_bus_num;
0026 EXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num);
0027 
0028 
0029 /**
0030  * i2c_register_board_info - statically declare I2C devices
0031  * @busnum: identifies the bus to which these devices belong
0032  * @info: vector of i2c device descriptors
0033  * @len: how many descriptors in the vector; may be zero to reserve
0034  *  the specified bus number.
0035  *
0036  * Systems using the Linux I2C driver stack can declare tables of board info
0037  * while they initialize.  This should be done in board-specific init code
0038  * near arch_initcall() time, or equivalent, before any I2C adapter driver is
0039  * registered.  For example, mainboard init code could define several devices,
0040  * as could the init code for each daughtercard in a board stack.
0041  *
0042  * The I2C devices will be created later, after the adapter for the relevant
0043  * bus has been registered.  After that moment, standard driver model tools
0044  * are used to bind "new style" I2C drivers to the devices.  The bus number
0045  * for any device declared using this routine is not available for dynamic
0046  * allocation.
0047  *
0048  * The board info passed can safely be __initdata, but be careful of embedded
0049  * pointers (for platform_data, functions, etc) since that won't be copied.
0050  */
0051 int i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned len)
0052 {
0053     int status;
0054 
0055     down_write(&__i2c_board_lock);
0056 
0057     /* dynamic bus numbers will be assigned after the last static one */
0058     if (busnum >= __i2c_first_dynamic_bus_num)
0059         __i2c_first_dynamic_bus_num = busnum + 1;
0060 
0061     for (status = 0; len; len--, info++) {
0062         struct i2c_devinfo  *devinfo;
0063 
0064         devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
0065         if (!devinfo) {
0066             pr_debug("i2c-core: can't register boardinfo!\n");
0067             status = -ENOMEM;
0068             break;
0069         }
0070 
0071         devinfo->busnum = busnum;
0072         devinfo->board_info = *info;
0073 
0074         if (info->resources) {
0075             devinfo->board_info.resources =
0076                 kmemdup(info->resources,
0077                     info->num_resources *
0078                         sizeof(*info->resources),
0079                     GFP_KERNEL);
0080             if (!devinfo->board_info.resources) {
0081                 status = -ENOMEM;
0082                 kfree(devinfo);
0083                 break;
0084             }
0085         }
0086 
0087         list_add_tail(&devinfo->list, &__i2c_board_list);
0088     }
0089 
0090     up_write(&__i2c_board_lock);
0091 
0092     return status;
0093 }