162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * i2c-boardinfo.c - collect pre-declarations of I2C devices 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/export.h> 762306a36Sopenharmony_ci#include <linux/i2c.h> 862306a36Sopenharmony_ci#include <linux/kernel.h> 962306a36Sopenharmony_ci#include <linux/property.h> 1062306a36Sopenharmony_ci#include <linux/rwsem.h> 1162306a36Sopenharmony_ci#include <linux/slab.h> 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#include "i2c-core.h" 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci/* These symbols are exported ONLY FOR the i2c core. 1762306a36Sopenharmony_ci * No other users will be supported. 1862306a36Sopenharmony_ci */ 1962306a36Sopenharmony_ciDECLARE_RWSEM(__i2c_board_lock); 2062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__i2c_board_lock); 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciLIST_HEAD(__i2c_board_list); 2362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__i2c_board_list); 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ciint __i2c_first_dynamic_bus_num; 2662306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num); 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci/** 3062306a36Sopenharmony_ci * i2c_register_board_info - statically declare I2C devices 3162306a36Sopenharmony_ci * @busnum: identifies the bus to which these devices belong 3262306a36Sopenharmony_ci * @info: vector of i2c device descriptors 3362306a36Sopenharmony_ci * @len: how many descriptors in the vector; may be zero to reserve 3462306a36Sopenharmony_ci * the specified bus number. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * Systems using the Linux I2C driver stack can declare tables of board info 3762306a36Sopenharmony_ci * while they initialize. This should be done in board-specific init code 3862306a36Sopenharmony_ci * near arch_initcall() time, or equivalent, before any I2C adapter driver is 3962306a36Sopenharmony_ci * registered. For example, mainboard init code could define several devices, 4062306a36Sopenharmony_ci * as could the init code for each daughtercard in a board stack. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * The I2C devices will be created later, after the adapter for the relevant 4362306a36Sopenharmony_ci * bus has been registered. After that moment, standard driver model tools 4462306a36Sopenharmony_ci * are used to bind "new style" I2C drivers to the devices. The bus number 4562306a36Sopenharmony_ci * for any device declared using this routine is not available for dynamic 4662306a36Sopenharmony_ci * allocation. 4762306a36Sopenharmony_ci * 4862306a36Sopenharmony_ci * The board info passed can safely be __initdata, but be careful of embedded 4962306a36Sopenharmony_ci * pointers (for platform_data, functions, etc) since that won't be copied. 5062306a36Sopenharmony_ci */ 5162306a36Sopenharmony_ciint i2c_register_board_info(int busnum, struct i2c_board_info const *info, unsigned len) 5262306a36Sopenharmony_ci{ 5362306a36Sopenharmony_ci int status; 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_ci down_write(&__i2c_board_lock); 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ci /* dynamic bus numbers will be assigned after the last static one */ 5862306a36Sopenharmony_ci if (busnum >= __i2c_first_dynamic_bus_num) 5962306a36Sopenharmony_ci __i2c_first_dynamic_bus_num = busnum + 1; 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci for (status = 0; len; len--, info++) { 6262306a36Sopenharmony_ci struct i2c_devinfo *devinfo; 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_ci devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL); 6562306a36Sopenharmony_ci if (!devinfo) { 6662306a36Sopenharmony_ci pr_debug("i2c-core: can't register boardinfo!\n"); 6762306a36Sopenharmony_ci status = -ENOMEM; 6862306a36Sopenharmony_ci break; 6962306a36Sopenharmony_ci } 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci devinfo->busnum = busnum; 7262306a36Sopenharmony_ci devinfo->board_info = *info; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ci if (info->resources) { 7562306a36Sopenharmony_ci devinfo->board_info.resources = 7662306a36Sopenharmony_ci kmemdup(info->resources, 7762306a36Sopenharmony_ci info->num_resources * 7862306a36Sopenharmony_ci sizeof(*info->resources), 7962306a36Sopenharmony_ci GFP_KERNEL); 8062306a36Sopenharmony_ci if (!devinfo->board_info.resources) { 8162306a36Sopenharmony_ci status = -ENOMEM; 8262306a36Sopenharmony_ci kfree(devinfo); 8362306a36Sopenharmony_ci break; 8462306a36Sopenharmony_ci } 8562306a36Sopenharmony_ci } 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci list_add_tail(&devinfo->list, &__i2c_board_list); 8862306a36Sopenharmony_ci } 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci up_write(&__i2c_board_lock); 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci return status; 9362306a36Sopenharmony_ci} 94