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