Subversion Repositories Kolibri OS

Rev

Rev 1963 | Rev 4104 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1963 Rev 3031
Line 12... Line 12...
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
13
    GNU General Public License for more details.
Line 14... Line 14...
14
 
14
 
15
    You should have received a copy of the GNU General Public License
15
    You should have received a copy of the GNU General Public License
16
    along with this program; if not, write to the Free Software
16
    along with this program; if not, write to the Free Software
-
 
17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		     */
18
    MA 02110-1301 USA.							     */
Line 18... Line 19...
18
/* ------------------------------------------------------------------------- */
19
/* ------------------------------------------------------------------------- */
19
 
20
 
20
/* With some changes from Kyösti Mälkki .
21
/* With some changes from Kyösti Mälkki .
21
   All SMBus-related things are written by Frodo Looijaard 
22
   All SMBus-related things are written by Frodo Looijaard 
-
 
23
   SMBus 2.0 support by Mark Studebaker  and
-
 
24
   Jean Delvare 
22
   SMBus 2.0 support by Mark Studebaker  and
25
   Mux support by Rodolfo Giometti  and
-
 
26
   Michael Lawnick  */
23
   Jean Delvare  */
27
 
-
 
28
#include 
24
 
29
#include 
25
#include 
30
#include 
26
#include 
31
#include 
27
#include 
32
#include 
Line -... Line 33...
-
 
33
#include 
-
 
34
#include 
-
 
35
 
-
 
36
 
-
 
37
 
-
 
38
 
-
 
39
 
-
 
40
 
-
 
41
 
-
 
42
 
-
 
43
 
-
 
44
 
-
 
45
 
-
 
46
 
-
 
47
 
-
 
48
 
-
 
49
 
-
 
50
 
-
 
51
 
-
 
52
 
-
 
53
 
-
 
54
 
-
 
55
 
-
 
56
 
-
 
57
 
-
 
58
 
-
 
59
 
-
 
60
 
-
 
61
 
-
 
62
 
-
 
63
 
-
 
64
 
-
 
65
 
-
 
66
 
-
 
67
 
-
 
68
 
-
 
69
#if 0
-
 
70
 
-
 
71
static ssize_t
-
 
72
show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
-
 
73
{
-
 
74
	struct i2c_client *client = to_i2c_client(dev);
-
 
75
	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
-
 
76
}
-
 
77
 
-
 
78
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
-
 
79
static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
-
 
80
 
-
 
81
static struct attribute *i2c_dev_attrs[] = {
-
 
82
	&dev_attr_name.attr,
-
 
83
	/* modalias helps coldplug:  modprobe $(cat .../modalias) */
-
 
84
	&dev_attr_modalias.attr,
-
 
85
	NULL
-
 
86
};
-
 
87
 
-
 
88
static struct attribute_group i2c_dev_attr_group = {
-
 
89
	.attrs		= i2c_dev_attrs,
-
 
90
};
-
 
91
 
-
 
92
static const struct attribute_group *i2c_dev_attr_groups[] = {
-
 
93
	&i2c_dev_attr_group,
-
 
94
	NULL
-
 
95
};
-
 
96
 
-
 
97
static const struct dev_pm_ops i2c_device_pm_ops = {
-
 
98
	.suspend = i2c_device_pm_suspend,
-
 
99
	.resume = i2c_device_pm_resume,
-
 
100
	.freeze = i2c_device_pm_freeze,
-
 
101
	.thaw = i2c_device_pm_thaw,
-
 
102
	.poweroff = i2c_device_pm_poweroff,
-
 
103
	.restore = i2c_device_pm_restore,
-
 
104
	SET_RUNTIME_PM_OPS(
-
 
105
		pm_generic_runtime_suspend,
-
 
106
		pm_generic_runtime_resume,
-
 
107
		pm_generic_runtime_idle
-
 
108
	)
-
 
109
};
-
 
110
 
-
 
111
struct bus_type i2c_bus_type = {
-
 
112
	.name		= "i2c",
-
 
113
	.match		= i2c_device_match,
-
 
114
	.probe		= i2c_device_probe,
-
 
115
	.remove		= i2c_device_remove,
-
 
116
	.shutdown	= i2c_device_shutdown,
-
 
117
	.pm		= &i2c_device_pm_ops,
-
 
118
};
-
 
119
EXPORT_SYMBOL_GPL(i2c_bus_type);
-
 
120
 
-
 
121
static struct device_type i2c_client_type = {
-
 
122
	.groups		= i2c_dev_attr_groups,
-
 
123
	.uevent		= i2c_device_uevent,
-
 
124
	.release	= i2c_client_dev_release,
-
 
125
};
-
 
126
 
-
 
127
 
-
 
128
/**
-
 
129
 * i2c_verify_client - return parameter as i2c_client, or NULL
-
 
130
 * @dev: device, probably from some driver model iterator
-
 
131
 *
-
 
132
 * When traversing the driver model tree, perhaps using driver model
-
 
133
 * iterators like @device_for_each_child(), you can't assume very much
-
 
134
 * about the nodes you find.  Use this function to avoid oopses caused
-
 
135
 * by wrongly treating some non-I2C device as an i2c_client.
-
 
136
 */
-
 
137
struct i2c_client *i2c_verify_client(struct device *dev)
-
 
138
{
-
 
139
	return (dev->type == &i2c_client_type)
-
 
140
			? to_i2c_client(dev)
-
 
141
			: NULL;
-
 
142
}
-
 
143
EXPORT_SYMBOL(i2c_verify_client);
-
 
144
 
-
 
145
 
-
 
146
/* This is a permissive address validity check, I2C address map constraints
-
 
147
 * are purposely not enforced, except for the general call address. */
-
 
148
static int i2c_check_client_addr_validity(const struct i2c_client *client)
-
 
149
{
-
 
150
	if (client->flags & I2C_CLIENT_TEN) {
-
 
151
		/* 10-bit address, all values are valid */
-
 
152
		if (client->addr > 0x3ff)
-
 
153
			return -EINVAL;
-
 
154
	} else {
-
 
155
		/* 7-bit address, reject the general call address */
-
 
156
		if (client->addr == 0x00 || client->addr > 0x7f)
-
 
157
			return -EINVAL;
-
 
158
	}
-
 
159
	return 0;
-
 
160
}
-
 
161
 
-
 
162
/* And this is a strict address validity check, used when probing. If a
-
 
163
 * device uses a reserved address, then it shouldn't be probed. 7-bit
-
 
164
 * addressing is assumed, 10-bit address devices are rare and should be
-
 
165
 * explicitly enumerated. */
-
 
166
static int i2c_check_addr_validity(unsigned short addr)
-
 
167
{
-
 
168
	/*
-
 
169
	 * Reserved addresses per I2C specification:
-
 
170
	 *  0x00       General call address / START byte
-
 
171
	 *  0x01       CBUS address
-
 
172
	 *  0x02       Reserved for different bus format
-
 
173
	 *  0x03       Reserved for future purposes
-
 
174
	 *  0x04-0x07  Hs-mode master code
-
 
175
	 *  0x78-0x7b  10-bit slave addressing
-
 
176
	 *  0x7c-0x7f  Reserved for future purposes
-
 
177
	 */
-
 
178
	if (addr < 0x08 || addr > 0x77)
-
 
179
		return -EINVAL;
-
 
180
	return 0;
-
 
181
}
-
 
182
 
-
 
183
static int __i2c_check_addr_busy(struct device *dev, void *addrp)
-
 
184
{
-
 
185
	struct i2c_client	*client = i2c_verify_client(dev);
-
 
186
	int			addr = *(int *)addrp;
-
 
187
 
-
 
188
	if (client && client->addr == addr)
-
 
189
		return -EBUSY;
-
 
190
	return 0;
-
 
191
}
-
 
192
 
-
 
193
/* walk up mux tree */
-
 
194
static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
-
 
195
{
-
 
196
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
-
 
197
	int result;
-
 
198
 
-
 
199
	result = device_for_each_child(&adapter->dev, &addr,
-
 
200
					__i2c_check_addr_busy);
-
 
201
 
-
 
202
	if (!result && parent)
-
 
203
		result = i2c_check_mux_parents(parent, addr);
-
 
204
 
-
 
205
	return result;
-
 
206
}
-
 
207
 
-
 
208
/* recurse down mux tree */
-
 
209
static int i2c_check_mux_children(struct device *dev, void *addrp)
-
 
210
{
-
 
211
	int result;
-
 
212
 
-
 
213
	if (dev->type == &i2c_adapter_type)
-
 
214
		result = device_for_each_child(dev, addrp,
-
 
215
						i2c_check_mux_children);
-
 
216
	else
-
 
217
		result = __i2c_check_addr_busy(dev, addrp);
-
 
218
 
-
 
219
	return result;
-
 
220
}
-
 
221
 
-
 
222
static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
-
 
223
{
-
 
224
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
-
 
225
	int result = 0;
-
 
226
 
-
 
227
	if (parent)
-
 
228
		result = i2c_check_mux_parents(parent, addr);
-
 
229
 
-
 
230
	if (!result)
-
 
231
		result = device_for_each_child(&adapter->dev, &addr,
-
 
232
						i2c_check_mux_children);
-
 
233
 
-
 
234
	return result;
-
 
235
}
-
 
236
 
-
 
237
/**
-
 
238
 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
-
 
239
 * @adapter: Target I2C bus segment
-
 
240
 */
-
 
241
void i2c_lock_adapter(struct i2c_adapter *adapter)
-
 
242
{
-
 
243
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
-
 
244
 
-
 
245
	if (parent)
-
 
246
		i2c_lock_adapter(parent);
-
 
247
	else
-
 
248
		rt_mutex_lock(&adapter->bus_lock);
-
 
249
}
-
 
250
EXPORT_SYMBOL_GPL(i2c_lock_adapter);
-
 
251
 
-
 
252
/**
-
 
253
 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
-
 
254
 * @adapter: Target I2C bus segment
-
 
255
 */
-
 
256
static int i2c_trylock_adapter(struct i2c_adapter *adapter)
-
 
257
{
-
 
258
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
-
 
259
 
-
 
260
	if (parent)
-
 
261
		return i2c_trylock_adapter(parent);
-
 
262
	else
-
 
263
		return rt_mutex_trylock(&adapter->bus_lock);
-
 
264
}
-
 
265
 
-
 
266
/**
-
 
267
 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
-
 
268
 * @adapter: Target I2C bus segment
-
 
269
 */
-
 
270
void i2c_unlock_adapter(struct i2c_adapter *adapter)
-
 
271
{
-
 
272
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
-
 
273
 
-
 
274
	if (parent)
-
 
275
		i2c_unlock_adapter(parent);
-
 
276
	else
-
 
277
		rt_mutex_unlock(&adapter->bus_lock);
-
 
278
}
-
 
279
EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
28
#include 
280
 
29
#include 
281
#endif
30
 
282
 
31
 
283
 
32
/**
284
/**
Line 63... Line 315...
63
     *    (discarding status on the first one).
315
     *    (discarding status on the first one).
64
     */
316
     */
Line 65... Line 317...
65
 
317
 
Line 66... Line -...
66
    if (adap->algo->master_xfer) {
-
 
67
 
318
    if (adap->algo->master_xfer) {
68
 
319
 
-
 
320
        /* Retry automatically on arbitration loss */
69
        /* Retry automatically on arbitration loss */
321
        orig_jiffies = GetTimerTicks();
Line 70... Line 322...
70
        orig_jiffies = 0;
322
 
71
        for (ret = 0, try = 0; try <= adap->retries; try++) {
323
        for (ret = 0, try = 0; try <= adap->retries; try++) {
72
 
324
 
-
 
325
            ret = adap->algo->master_xfer(adap, msgs, num);
73
            ret = adap->algo->master_xfer(adap, msgs, num);
326
            if (ret != -EAGAIN)
74
            if (ret != -EAGAIN)
327
                break;
-
 
328
 
75
                break;
329
            if (time_after(GetTimerTicks(), orig_jiffies + adap->timeout))
76
//            if (time_after(jiffies, orig_jiffies + adap->timeout))
330
                break;
77
//                break;
331
 
78
                delay(1);
332
            delay(1);
79
        }
333
        }
80
//        mutex_unlock(&adap->bus_lock);
334
//        mutex_unlock(&adap->bus_lock);
81
        return ret;
335
        return ret;
82
    } else {
336
    } else {
83
//        dev_dbg(&adap->dev, "I2C level transfers not supported\n");
337
        dbgprintf("I2C level transfers not supported\n");
84
        return -EOPNOTSUPP;
338
        return -EOPNOTSUPP;