Subversion Repositories Kolibri OS

Rev

Rev 3031 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1125 serge 1
/* i2c-core.c - a device driver for the iic-bus interface		     */
2
/* ------------------------------------------------------------------------- */
3
/*   Copyright (C) 1995-99 Simon G. Vogl
4
 
5
    This program is free software; you can redistribute it and/or modify
6
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
9
 
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
 
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
3031 serge 17
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
    MA 02110-1301 USA.							     */
1125 serge 19
/* ------------------------------------------------------------------------- */
20
 
21
/* With some changes from Kyösti Mälkki .
22
   All SMBus-related things are written by Frodo Looijaard 
23
   SMBus 2.0 support by Mark Studebaker  and
3031 serge 24
   Jean Delvare 
25
   Mux support by Rodolfo Giometti  and
26
   Michael Lawnick  */
1125 serge 27
 
3031 serge 28
#include 
29
#include 
30
#include 
1125 serge 31
#include 
32
#include 
33
#include 
34
#include 
4104 Serge 35
#include 
1125 serge 36
 
37
 
3031 serge 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
 
70
#if 0
71
 
72
static ssize_t
73
show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
74
{
75
	struct i2c_client *client = to_i2c_client(dev);
76
	return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
77
}
78
 
79
static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
80
static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
81
 
82
static struct attribute *i2c_dev_attrs[] = {
83
	&dev_attr_name.attr,
84
	/* modalias helps coldplug:  modprobe $(cat .../modalias) */
85
	&dev_attr_modalias.attr,
86
	NULL
87
};
88
 
89
static struct attribute_group i2c_dev_attr_group = {
90
	.attrs		= i2c_dev_attrs,
91
};
92
 
93
static const struct attribute_group *i2c_dev_attr_groups[] = {
94
	&i2c_dev_attr_group,
95
	NULL
96
};
97
 
98
static const struct dev_pm_ops i2c_device_pm_ops = {
99
	.suspend = i2c_device_pm_suspend,
100
	.resume = i2c_device_pm_resume,
101
	.freeze = i2c_device_pm_freeze,
102
	.thaw = i2c_device_pm_thaw,
103
	.poweroff = i2c_device_pm_poweroff,
104
	.restore = i2c_device_pm_restore,
105
	SET_RUNTIME_PM_OPS(
106
		pm_generic_runtime_suspend,
107
		pm_generic_runtime_resume,
4104 Serge 108
		NULL
3031 serge 109
	)
110
};
111
 
112
struct bus_type i2c_bus_type = {
113
	.name		= "i2c",
114
	.match		= i2c_device_match,
115
	.probe		= i2c_device_probe,
116
	.remove		= i2c_device_remove,
117
	.shutdown	= i2c_device_shutdown,
118
	.pm		= &i2c_device_pm_ops,
119
};
120
EXPORT_SYMBOL_GPL(i2c_bus_type);
121
 
122
static struct device_type i2c_client_type = {
123
	.groups		= i2c_dev_attr_groups,
124
	.uevent		= i2c_device_uevent,
125
	.release	= i2c_client_dev_release,
126
};
127
 
128
 
1125 serge 129
/**
3031 serge 130
 * i2c_verify_client - return parameter as i2c_client, or NULL
131
 * @dev: device, probably from some driver model iterator
132
 *
133
 * When traversing the driver model tree, perhaps using driver model
134
 * iterators like @device_for_each_child(), you can't assume very much
135
 * about the nodes you find.  Use this function to avoid oopses caused
136
 * by wrongly treating some non-I2C device as an i2c_client.
137
 */
138
struct i2c_client *i2c_verify_client(struct device *dev)
139
{
140
	return (dev->type == &i2c_client_type)
141
			? to_i2c_client(dev)
142
			: NULL;
143
}
144
EXPORT_SYMBOL(i2c_verify_client);
145
 
146
 
147
/* This is a permissive address validity check, I2C address map constraints
148
 * are purposely not enforced, except for the general call address. */
149
static int i2c_check_client_addr_validity(const struct i2c_client *client)
150
{
151
	if (client->flags & I2C_CLIENT_TEN) {
152
		/* 10-bit address, all values are valid */
153
		if (client->addr > 0x3ff)
154
			return -EINVAL;
155
	} else {
156
		/* 7-bit address, reject the general call address */
157
		if (client->addr == 0x00 || client->addr > 0x7f)
158
			return -EINVAL;
159
	}
160
	return 0;
161
}
162
 
163
/* And this is a strict address validity check, used when probing. If a
164
 * device uses a reserved address, then it shouldn't be probed. 7-bit
165
 * addressing is assumed, 10-bit address devices are rare and should be
166
 * explicitly enumerated. */
167
static int i2c_check_addr_validity(unsigned short addr)
168
{
169
	/*
170
	 * Reserved addresses per I2C specification:
171
	 *  0x00       General call address / START byte
172
	 *  0x01       CBUS address
173
	 *  0x02       Reserved for different bus format
174
	 *  0x03       Reserved for future purposes
175
	 *  0x04-0x07  Hs-mode master code
176
	 *  0x78-0x7b  10-bit slave addressing
177
	 *  0x7c-0x7f  Reserved for future purposes
178
	 */
179
	if (addr < 0x08 || addr > 0x77)
180
		return -EINVAL;
181
	return 0;
182
}
183
 
184
static int __i2c_check_addr_busy(struct device *dev, void *addrp)
185
{
186
	struct i2c_client	*client = i2c_verify_client(dev);
187
	int			addr = *(int *)addrp;
188
 
189
	if (client && client->addr == addr)
190
		return -EBUSY;
191
	return 0;
192
}
193
 
194
/* walk up mux tree */
195
static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
196
{
197
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
198
	int result;
199
 
200
	result = device_for_each_child(&adapter->dev, &addr,
201
					__i2c_check_addr_busy);
202
 
203
	if (!result && parent)
204
		result = i2c_check_mux_parents(parent, addr);
205
 
206
	return result;
207
}
208
 
209
/* recurse down mux tree */
210
static int i2c_check_mux_children(struct device *dev, void *addrp)
211
{
212
	int result;
213
 
214
	if (dev->type == &i2c_adapter_type)
215
		result = device_for_each_child(dev, addrp,
216
						i2c_check_mux_children);
217
	else
218
		result = __i2c_check_addr_busy(dev, addrp);
219
 
220
	return result;
221
}
222
 
223
static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
224
{
225
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
226
	int result = 0;
227
 
228
	if (parent)
229
		result = i2c_check_mux_parents(parent, addr);
230
 
231
	if (!result)
232
		result = device_for_each_child(&adapter->dev, &addr,
233
						i2c_check_mux_children);
234
 
235
	return result;
236
}
237
 
238
/**
239
 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
240
 * @adapter: Target I2C bus segment
241
 */
242
void i2c_lock_adapter(struct i2c_adapter *adapter)
243
{
244
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
245
 
246
	if (parent)
247
		i2c_lock_adapter(parent);
248
	else
249
		rt_mutex_lock(&adapter->bus_lock);
250
}
251
EXPORT_SYMBOL_GPL(i2c_lock_adapter);
252
 
253
/**
254
 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
255
 * @adapter: Target I2C bus segment
256
 */
257
static int i2c_trylock_adapter(struct i2c_adapter *adapter)
258
{
259
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
260
 
261
	if (parent)
262
		return i2c_trylock_adapter(parent);
263
	else
264
		return rt_mutex_trylock(&adapter->bus_lock);
265
}
266
 
267
/**
268
 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
269
 * @adapter: Target I2C bus segment
270
 */
271
void i2c_unlock_adapter(struct i2c_adapter *adapter)
272
{
273
	struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
274
 
275
	if (parent)
276
		i2c_unlock_adapter(parent);
277
	else
278
		rt_mutex_unlock(&adapter->bus_lock);
279
}
280
EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
281
 
282
#endif
283
 
284
 
285
/**
1125 serge 286
 * i2c_transfer - execute a single or combined I2C message
287
 * @adap: Handle to I2C bus
288
 * @msgs: One or more messages to execute before STOP is issued to
289
 *  terminate the operation; each message begins with a START.
290
 * @num: Number of messages to be executed.
291
 *
292
 * Returns negative errno, else the number of messages executed.
293
 *
294
 * Note that there is no requirement that each message be sent to
295
 * the same slave address, although that is the most common model.
296
 */
297
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
298
{
299
    unsigned long orig_jiffies;
300
    int ret, try;
301
 
302
    /* REVISIT the fault reporting model here is weak:
303
     *
304
     *  - When we get an error after receiving N bytes from a slave,
305
     *    there is no way to report "N".
306
     *
307
     *  - When we get a NAK after transmitting N bytes to a slave,
308
     *    there is no way to report "N" ... or to let the master
309
     *    continue executing the rest of this combined message, if
310
     *    that's the appropriate response.
311
     *
312
     *  - When for example "num" is two and we successfully complete
313
     *    the first message but get an error part way through the
314
     *    second, it's unclear whether that should be reported as
315
     *    one (discarding status on the second message) or errno
316
     *    (discarding status on the first one).
317
     */
318
 
319
    if (adap->algo->master_xfer) {
320
 
3031 serge 321
        /* Retry automatically on arbitration loss */
322
        orig_jiffies = GetTimerTicks();
1125 serge 323
 
324
        for (ret = 0, try = 0; try <= adap->retries; try++) {
1963 serge 325
 
1125 serge 326
            ret = adap->algo->master_xfer(adap, msgs, num);
327
            if (ret != -EAGAIN)
328
                break;
3031 serge 329
 
330
            if (time_after(GetTimerTicks(), orig_jiffies + adap->timeout))
331
                break;
332
 
333
            delay(1);
1125 serge 334
        }
335
//        mutex_unlock(&adap->bus_lock);
336
        return ret;
337
    } else {
3031 serge 338
        dbgprintf("I2C level transfers not supported\n");
1125 serge 339
        return -EOPNOTSUPP;
340
    }
341
}
342
EXPORT_SYMBOL(i2c_transfer);
343
 
344
 
1963 serge 345
/**
346
 * i2c_new_device - instantiate an i2c device
347
 * @adap: the adapter managing the device
348
 * @info: describes one I2C device; bus_num is ignored
349
 * Context: can sleep
350
 *
351
 * Create an i2c device. Binding is handled through driver model
352
 * probe()/remove() methods.  A driver may be bound to this device when we
353
 * return from this function, or any later moment (e.g. maybe hotplugging will
354
 * load the driver module).  This call is not appropriate for use by mainboard
355
 * initialization logic, which usually runs during an arch_initcall() long
356
 * before any i2c_adapter could exist.
357
 *
358
 * This returns the new i2c client, which may be saved for later use with
359
 * i2c_unregister_device(); or NULL to indicate an error.
360
 */
361
struct i2c_client *
362
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
363
{
364
    struct i2c_client   *client;
365
    int         status;
1125 serge 366
 
1963 serge 367
    client = kzalloc(sizeof *client, GFP_KERNEL);
368
    if (!client)
369
        return NULL;
1125 serge 370
 
1963 serge 371
    client->adapter = adap;
1125 serge 372
 
1963 serge 373
    client->flags = info->flags;
374
    client->addr = info->addr;
375
    client->irq = info->irq;
376
 
377
    strlcpy(client->name, info->type, sizeof(client->name));
378
 
379
    return client;
380
}
381