i2c-boardinfo.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * i2c-boardinfo.c - collect pre-declarations of I2C devices
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. * MA 02110-1301 USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/i2c.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <linux/rwsem.h>
  24. #include "i2c-core.h"
  25. /* These symbols are exported ONLY FOR the i2c core.
  26. * No other users will be supported.
  27. */
  28. DECLARE_RWSEM(__i2c_board_lock);
  29. EXPORT_SYMBOL_GPL(__i2c_board_lock);
  30. LIST_HEAD(__i2c_board_list);
  31. EXPORT_SYMBOL_GPL(__i2c_board_list);
  32. int __i2c_first_dynamic_bus_num;
  33. EXPORT_SYMBOL_GPL(__i2c_first_dynamic_bus_num);
  34. /**
  35. * i2c_register_board_info - statically declare I2C devices
  36. * @busnum: identifies the bus to which these devices belong
  37. * @info: vector of i2c device descriptors
  38. * @len: how many descriptors in the vector; may be zero to reserve
  39. * the specified bus number.
  40. *
  41. * Systems using the Linux I2C driver stack can declare tables of board info
  42. * while they initialize. This should be done in board-specific init code
  43. * near arch_initcall() time, or equivalent, before any I2C adapter driver is
  44. * registered. For example, mainboard init code could define several devices,
  45. * as could the init code for each daughtercard in a board stack.
  46. *
  47. * The I2C devices will be created later, after the adapter for the relevant
  48. * bus has been registered. After that moment, standard driver model tools
  49. * are used to bind "new style" I2C drivers to the devices. The bus number
  50. * for any device declared using this routine is not available for dynamic
  51. * allocation.
  52. *
  53. * The board info passed can safely be __initdata, but be careful of embedded
  54. * pointers (for platform_data, functions, etc) since that won't be copied.
  55. */
  56. int __init
  57. i2c_register_board_info(int busnum,
  58. struct i2c_board_info const *info, unsigned len)
  59. {
  60. int status;
  61. down_write(&__i2c_board_lock);
  62. /* dynamic bus numbers will be assigned after the last static one */
  63. if (busnum >= __i2c_first_dynamic_bus_num)
  64. __i2c_first_dynamic_bus_num = busnum + 1;
  65. for (status = 0; len; len--, info++) {
  66. struct i2c_devinfo *devinfo;
  67. devinfo = kzalloc(sizeof(*devinfo), GFP_KERNEL);
  68. if (!devinfo) {
  69. pr_debug("i2c-core: can't register boardinfo!\n");
  70. status = -ENOMEM;
  71. break;
  72. }
  73. devinfo->busnum = busnum;
  74. devinfo->board_info = *info;
  75. list_add_tail(&devinfo->list, &__i2c_board_list);
  76. }
  77. up_write(&__i2c_board_lock);
  78. return status;
  79. }