i2c-sensor-detect.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. static unsigned int empty_isa[] = {I2C_CLIENT_ISA_END};
  24. /* Very inefficient for ISA detects, and won't work for 10-bit addresses! */
  25. int i2c_detect(struct i2c_adapter *adapter,
  26. struct i2c_address_data *address_data,
  27. int (*found_proc) (struct i2c_adapter *, int, int))
  28. {
  29. int addr, i, found, j, err;
  30. struct i2c_force_data *this_force;
  31. int is_isa = i2c_is_isa_adapter(adapter);
  32. int adapter_id =
  33. is_isa ? ANY_I2C_ISA_BUS : i2c_adapter_id(adapter);
  34. unsigned short *normal_i2c;
  35. unsigned int *normal_isa;
  36. unsigned short *probe;
  37. unsigned short *ignore;
  38. /* Forget it if we can't probe using SMBUS_QUICK */
  39. if ((!is_isa) &&
  40. !i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK))
  41. return -1;
  42. /* Use default "empty" list if the adapter doesn't specify any */
  43. normal_i2c = probe = ignore = empty;
  44. normal_isa = empty_isa;
  45. if (address_data->normal_i2c)
  46. normal_i2c = address_data->normal_i2c;
  47. if (address_data->normal_isa)
  48. normal_isa = address_data->normal_isa;
  49. if (address_data->probe)
  50. probe = address_data->probe;
  51. if (address_data->ignore)
  52. ignore = address_data->ignore;
  53. for (addr = 0x00; addr <= (is_isa ? 0xffff : 0x7f); addr++) {
  54. if (!is_isa && i2c_check_addr(adapter, addr))
  55. continue;
  56. /* If it is in one of the force entries, we don't do any
  57. detection at all */
  58. found = 0;
  59. for (i = 0; !found && (this_force = address_data->forces + i, this_force->force); i++) {
  60. for (j = 0; !found && (this_force->force[j] != I2C_CLIENT_END); j += 2) {
  61. if ( ((adapter_id == this_force->force[j]) ||
  62. ((this_force->force[j] == ANY_I2C_BUS) && !is_isa)) &&
  63. (addr == this_force->force[j + 1]) ) {
  64. dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n", adapter_id, addr);
  65. if ((err = found_proc(adapter, addr, this_force->kind)))
  66. return err;
  67. found = 1;
  68. }
  69. }
  70. }
  71. if (found)
  72. continue;
  73. /* If this address is in one of the ignores, we can forget about it
  74. right now */
  75. for (i = 0; !found && (ignore[i] != I2C_CLIENT_END); i += 2) {
  76. if ( ((adapter_id == ignore[i]) ||
  77. ((ignore[i] == ANY_I2C_BUS) &&
  78. !is_isa)) &&
  79. (addr == ignore[i + 1])) {
  80. dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, addr %04x\n", adapter_id, addr);
  81. found = 1;
  82. }
  83. }
  84. if (found)
  85. continue;
  86. /* Now, we will do a detection, but only if it is in the normal or
  87. probe entries */
  88. if (is_isa) {
  89. for (i = 0; !found && (normal_isa[i] != I2C_CLIENT_ISA_END); i += 1) {
  90. if (addr == normal_isa[i]) {
  91. dev_dbg(&adapter->dev, "found normal isa entry for adapter %d, addr %04x\n", adapter_id, addr);
  92. found = 1;
  93. }
  94. }
  95. } else {
  96. for (i = 0; !found && (normal_i2c[i] != I2C_CLIENT_END); i += 1) {
  97. if (addr == normal_i2c[i]) {
  98. found = 1;
  99. dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, addr %02x\n", adapter_id, addr);
  100. }
  101. }
  102. }
  103. for (i = 0;
  104. !found && (probe[i] != I2C_CLIENT_END);
  105. i += 2) {
  106. if (((adapter_id == probe[i]) ||
  107. ((probe[i] == ANY_I2C_BUS) && !is_isa))
  108. && (addr == probe[i + 1])) {
  109. dev_dbg(&adapter->dev, "found probe parameter for adapter %d, addr %04x\n", adapter_id, addr);
  110. found = 1;
  111. }
  112. }
  113. if (!found)
  114. continue;
  115. /* OK, so we really should examine this address. First check
  116. whether there is some client here at all! */
  117. if (is_isa ||
  118. (i2c_smbus_xfer (adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) >= 0))
  119. if ((err = found_proc(adapter, addr, -1)))
  120. return err;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(i2c_detect);
  125. MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
  126. "Rudolf Marek <r.marek@sh.cvut.cz>");
  127. MODULE_DESCRIPTION("i2c-sensor driver");
  128. MODULE_LICENSE("GPL");