Subversion Repositories Kolibri OS

Rev

Rev 1963 | Rev 4104 | 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 
35
 
36
 
3031 serge 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
 
1125 serge 128
/**
3031 serge 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);
280
 
281
#endif
282
 
283
 
284
/**
1125 serge 285
 * i2c_transfer - execute a single or combined I2C message
286
 * @adap: Handle to I2C bus
287
 * @msgs: One or more messages to execute before STOP is issued to
288
 *  terminate the operation; each message begins with a START.
289
 * @num: Number of messages to be executed.
290
 *
291
 * Returns negative errno, else the number of messages executed.
292
 *
293
 * Note that there is no requirement that each message be sent to
294
 * the same slave address, although that is the most common model.
295
 */
296
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
297
{
298
    unsigned long orig_jiffies;
299
    int ret, try;
300
 
301
    /* REVISIT the fault reporting model here is weak:
302
     *
303
     *  - When we get an error after receiving N bytes from a slave,
304
     *    there is no way to report "N".
305
     *
306
     *  - When we get a NAK after transmitting N bytes to a slave,
307
     *    there is no way to report "N" ... or to let the master
308
     *    continue executing the rest of this combined message, if
309
     *    that's the appropriate response.
310
     *
311
     *  - When for example "num" is two and we successfully complete
312
     *    the first message but get an error part way through the
313
     *    second, it's unclear whether that should be reported as
314
     *    one (discarding status on the second message) or errno
315
     *    (discarding status on the first one).
316
     */
317
 
318
    if (adap->algo->master_xfer) {
319
 
3031 serge 320
        /* Retry automatically on arbitration loss */
321
        orig_jiffies = GetTimerTicks();
1125 serge 322
 
323
        for (ret = 0, try = 0; try <= adap->retries; try++) {
1963 serge 324
 
1125 serge 325
            ret = adap->algo->master_xfer(adap, msgs, num);
326
            if (ret != -EAGAIN)
327
                break;
3031 serge 328
 
329
            if (time_after(GetTimerTicks(), orig_jiffies + adap->timeout))
330
                break;
331
 
332
            delay(1);
1125 serge 333
        }
334
//        mutex_unlock(&adap->bus_lock);
335
        return ret;
336
    } else {
3031 serge 337
        dbgprintf("I2C level transfers not supported\n");
1125 serge 338
        return -EOPNOTSUPP;
339
    }
340
}
341
EXPORT_SYMBOL(i2c_transfer);
342
 
343
 
1963 serge 344
/**
345
 * i2c_new_device - instantiate an i2c device
346
 * @adap: the adapter managing the device
347
 * @info: describes one I2C device; bus_num is ignored
348
 * Context: can sleep
349
 *
350
 * Create an i2c device. Binding is handled through driver model
351
 * probe()/remove() methods.  A driver may be bound to this device when we
352
 * return from this function, or any later moment (e.g. maybe hotplugging will
353
 * load the driver module).  This call is not appropriate for use by mainboard
354
 * initialization logic, which usually runs during an arch_initcall() long
355
 * before any i2c_adapter could exist.
356
 *
357
 * This returns the new i2c client, which may be saved for later use with
358
 * i2c_unregister_device(); or NULL to indicate an error.
359
 */
360
struct i2c_client *
361
i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
362
{
363
    struct i2c_client   *client;
364
    int         status;
1125 serge 365
 
1963 serge 366
    client = kzalloc(sizeof *client, GFP_KERNEL);
367
    if (!client)
368
        return NULL;
1125 serge 369
 
1963 serge 370
    client->adapter = adap;
1125 serge 371
 
1963 serge 372
    client->flags = info->flags;
373
    client->addr = info->addr;
374
    client->irq = info->irq;
375
 
376
    strlcpy(client->name, info->type, sizeof(client->name));
377
 
378
    return client;
379
}
380