i2c-boardinfo.c 2.6 KB

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