i2c-sensor-detect.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. i2c-sensor-detect.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (c) 1998 - 2001 Frodo Looijaard <frodol@dds.nl> and
  5. Mark D. Studebaker <mdsxyz123@yahoo.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/i2c.h>
  21. #include <linux/i2c-sensor.h>
  22. static unsigned short empty[] = {I2C_CLIENT_END};
  23. /* Won't work for 10-bit addresses! */
  24. int i2c_detect(struct i2c_adapter *adapter,
  25. struct i2c_address_data *address_data,
  26. int (*found_proc) (struct i2c_adapter *, int, int))
  27. {
  28. int addr, i, found, j, err;
  29. struct i2c_force_data *this_force;
  30. int adapter_id = i2c_adapter_id(adapter);
  31. unsigned short *normal_i2c;
  32. unsigned short *probe;
  33. unsigned short *ignore;
  34. /* Forget it if we can't probe using SMBUS_QUICK */
  35. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK))
  36. return -1;
  37. /* Use default "empty" list if the adapter doesn't specify any */
  38. normal_i2c = probe = ignore = empty;
  39. if (address_data->normal_i2c)
  40. normal_i2c = address_data->normal_i2c;
  41. if (address_data->probe)
  42. probe = address_data->probe;
  43. if (address_data->ignore)
  44. ignore = address_data->ignore;
  45. for (addr = 0x00; addr <= 0x7f; addr++) {
  46. if (i2c_check_addr(adapter, addr))
  47. continue;
  48. /* If it is in one of the force entries, we don't do any
  49. detection at all */
  50. found = 0;
  51. for (i = 0; !found && (this_force = address_data->forces + i, this_force->force); i++) {
  52. for (j = 0; !found && (this_force->force[j] != I2C_CLIENT_END); j += 2) {
  53. if ( ((adapter_id == this_force->force[j]) ||
  54. (this_force->force[j] == ANY_I2C_BUS)) &&
  55. (addr == this_force->force[j + 1]) ) {
  56. dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n", adapter_id, addr);
  57. if ((err = found_proc(adapter, addr, this_force->kind)))
  58. return err;
  59. found = 1;
  60. }
  61. }
  62. }
  63. if (found)
  64. continue;
  65. /* If this address is in one of the ignores, we can forget about it
  66. right now */
  67. for (i = 0; !found && (ignore[i] != I2C_CLIENT_END); i += 2) {
  68. if ( ((adapter_id == ignore[i]) ||
  69. (ignore[i] == ANY_I2C_BUS)) &&
  70. (addr == ignore[i + 1])) {
  71. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, addr %04x\n", adapter_id, addr);
  72. found = 1;
  73. }
  74. }
  75. if (found)
  76. continue;
  77. /* Now, we will do a detection, but only if it is in the normal or
  78. probe entries */
  79. for (i = 0; !found && (normal_i2c[i] != I2C_CLIENT_END); i += 1) {
  80. if (addr == normal_i2c[i]) {
  81. found = 1;
  82. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, addr %02x\n", adapter_id, addr);
  83. }
  84. }
  85. for (i = 0;
  86. !found && (probe[i] != I2C_CLIENT_END);
  87. i += 2) {
  88. if (((adapter_id == probe[i]) ||
  89. (probe[i] == ANY_I2C_BUS))
  90. && (addr == probe[i + 1])) {
  91. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, addr %04x\n", adapter_id, addr);
  92. found = 1;
  93. }
  94. }
  95. if (!found)
  96. continue;
  97. /* OK, so we really should examine this address. First check
  98. whether there is some client here at all! */
  99. if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0)
  100. if ((err = found_proc(adapter, addr, -1)))
  101. return err;
  102. }
  103. return 0;
  104. }
  105. EXPORT_SYMBOL(i2c_detect);
  106. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  107. "Rudolf Marek <r.marek@sh.cvut.cz>");
  108. MODULE_DESCRIPTION("i2c-sensor driver");
  109. MODULE_LICENSE("GPL");