i2c-sensor-detect.c 4.0 KB

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