xref: /OK3568_Linux_fs/kernel/drivers/i2c/i2c-boardinfo.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * i2c-boardinfo.c - collect pre-declarations of I2C devices
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/export.h>
7*4882a593Smuzhiyun #include <linux/i2c.h>
8*4882a593Smuzhiyun #include <linux/kernel.h>
9*4882a593Smuzhiyun #include <linux/property.h>
10*4882a593Smuzhiyun #include <linux/rwsem.h>
11*4882a593Smuzhiyun #include <linux/slab.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "i2c-core.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /* These symbols are exported ONLY FOR the i2c core.
17*4882a593Smuzhiyun  * No other users will be supported.
18*4882a593Smuzhiyun  */
19*4882a593Smuzhiyun DECLARE_RWSEM(__i2c_board_lock);
20*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__i2c_board_lock);
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun LIST_HEAD(__i2c_board_list);
23*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__i2c_board_list);
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun int __i2c_first_dynamic_bus_num;
26*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num);
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * i2c_register_board_info - statically declare I2C devices
31*4882a593Smuzhiyun  * @busnum: identifies the bus to which these devices belong
32*4882a593Smuzhiyun  * @info: vector of i2c device descriptors
33*4882a593Smuzhiyun  * @len: how many descriptors in the vector; may be zero to reserve
34*4882a593Smuzhiyun  *	the specified bus number.
35*4882a593Smuzhiyun  *
36*4882a593Smuzhiyun  * Systems using the Linux I2C driver stack can declare tables of board info
37*4882a593Smuzhiyun  * while they initialize.  This should be done in board-specific init code
38*4882a593Smuzhiyun  * near arch_initcall() time, or equivalent, before any I2C adapter driver is
39*4882a593Smuzhiyun  * registered.  For example, mainboard init code could define several devices,
40*4882a593Smuzhiyun  * as could the init code for each daughtercard in a board stack.
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * The I2C devices will be created later, after the adapter for the relevant
43*4882a593Smuzhiyun  * bus has been registered.  After that moment, standard driver model tools
44*4882a593Smuzhiyun  * are used to bind "new style" I2C drivers to the devices.  The bus number
45*4882a593Smuzhiyun  * for any device declared using this routine is not available for dynamic
46*4882a593Smuzhiyun  * allocation.
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * The board info passed can safely be __initdata, but be careful of embedded
49*4882a593Smuzhiyun  * pointers (for platform_data, functions, etc) since that won't be copied.
50*4882a593Smuzhiyun  * Device properties are deep-copied though.
51*4882a593Smuzhiyun  */
i2c_register_board_info(int busnum,struct i2c_board_info const * info,unsigned len)52*4882a593Smuzhiyun int i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned len)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	int status;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	down_write(&__i2c_board_lock);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* dynamic bus numbers will be assigned after the last static one */
59*4882a593Smuzhiyun 	if (busnum >= __i2c_first_dynamic_bus_num)
60*4882a593Smuzhiyun 		__i2c_first_dynamic_bus_num = busnum + 1;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	for (status = 0; len; len--, info++) {
63*4882a593Smuzhiyun 		struct i2c_devinfo	*devinfo;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 		devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
66*4882a593Smuzhiyun 		if (!devinfo) {
67*4882a593Smuzhiyun 			pr_debug("i2c-core: can't register boardinfo!\n");
68*4882a593Smuzhiyun 			status = -ENOMEM;
69*4882a593Smuzhiyun 			break;
70*4882a593Smuzhiyun 		}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 		devinfo->busnum = busnum;
73*4882a593Smuzhiyun 		devinfo->board_info = *info;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 		if (info->properties) {
76*4882a593Smuzhiyun 			devinfo->board_info.properties =
77*4882a593Smuzhiyun 					property_entries_dup(info->properties);
78*4882a593Smuzhiyun 			if (IS_ERR(devinfo->board_info.properties)) {
79*4882a593Smuzhiyun 				status = PTR_ERR(devinfo->board_info.properties);
80*4882a593Smuzhiyun 				kfree(devinfo);
81*4882a593Smuzhiyun 				break;
82*4882a593Smuzhiyun 			}
83*4882a593Smuzhiyun 		}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 		if (info->resources) {
86*4882a593Smuzhiyun 			devinfo->board_info.resources =
87*4882a593Smuzhiyun 				kmemdup(info->resources,
88*4882a593Smuzhiyun 					info->num_resources *
89*4882a593Smuzhiyun 						sizeof(*info->resources),
90*4882a593Smuzhiyun 					GFP_KERNEL);
91*4882a593Smuzhiyun 			if (!devinfo->board_info.resources) {
92*4882a593Smuzhiyun 				status = -ENOMEM;
93*4882a593Smuzhiyun 				kfree(devinfo);
94*4882a593Smuzhiyun 				break;
95*4882a593Smuzhiyun 			}
96*4882a593Smuzhiyun 		}
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 		list_add_tail(&devinfo->list, &__i2c_board_list);
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	up_write(&__i2c_board_lock);
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	return status;
104*4882a593Smuzhiyun }
105