Subversion Repositories Kolibri OS

Rev

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