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