blob: ae07b874a5d7b7cc0b1aa922cc5d893bc25ab574 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18/* ------------------------------------------------------------------------- */
19
20/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020022 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/slab.h>
29#include <linux/i2c.h>
30#include <linux/init.h>
31#include <linux/idr.h>
32#include <linux/seq_file.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010033#include <linux/platform_device.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010034#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010035#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/uaccess.h>
37
38
39static LIST_HEAD(adapters);
40static LIST_HEAD(drivers);
Arjan van de Venb3585e42006-01-11 10:50:26 +010041static DEFINE_MUTEX(core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_IDR(i2c_adapter_idr);
43
David Brownellf37dd802007-02-13 22:09:00 +010044
45/* ------------------------------------------------------------------------- */
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* match always succeeds, as we want the probe() to tell if we really accept this match */
48static int i2c_device_match(struct device *dev, struct device_driver *drv)
49{
50 return 1;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static int i2c_device_probe(struct device *dev)
54{
55 return -ENODEV;
56}
57
58static int i2c_device_remove(struct device *dev)
59{
60 return 0;
61}
62
David Brownellf37dd802007-02-13 22:09:00 +010063static void i2c_device_shutdown(struct device *dev)
64{
65 struct i2c_driver *driver;
66
67 if (!dev->driver)
68 return;
69 driver = to_i2c_driver(dev->driver);
70 if (driver->shutdown)
71 driver->shutdown(to_i2c_client(dev));
72}
73
74static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
75{
76 struct i2c_driver *driver;
77
78 if (!dev->driver)
79 return 0;
80 driver = to_i2c_driver(dev->driver);
81 if (!driver->suspend)
82 return 0;
83 return driver->suspend(to_i2c_client(dev), mesg);
84}
85
86static int i2c_device_resume(struct device * dev)
87{
88 struct i2c_driver *driver;
89
90 if (!dev->driver)
91 return 0;
92 driver = to_i2c_driver(dev->driver);
93 if (!driver->resume)
94 return 0;
95 return driver->resume(to_i2c_client(dev));
96}
97
Russell Kingb864c7d2006-01-05 14:37:50 +000098struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +010099 .name = "i2c",
100 .match = i2c_device_match,
101 .probe = i2c_device_probe,
102 .remove = i2c_device_remove,
103 .shutdown = i2c_device_shutdown,
104 .suspend = i2c_device_suspend,
105 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000106};
107
David Brownellf37dd802007-02-13 22:09:00 +0100108/* ------------------------------------------------------------------------- */
109
David Brownell16ffadf2007-05-01 23:26:28 +0200110/* I2C bus adapters -- one roots each I2C or SMBUS segment */
111
Jean Delvareefde7232005-07-20 23:03:50 +0200112void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
David Brownellef2c83212007-05-01 23:26:28 +0200114 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 complete(&adap->dev_released);
116}
117
David Brownell16ffadf2007-05-01 23:26:28 +0200118static ssize_t
119show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
David Brownellef2c83212007-05-01 23:26:28 +0200121 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return sprintf(buf, "%s\n", adap->name);
123}
David Brownell16ffadf2007-05-01 23:26:28 +0200124
125static struct device_attribute i2c_adapter_attrs[] = {
126 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
127 { },
128};
129
130struct class i2c_adapter_class = {
131 .owner = THIS_MODULE,
132 .name = "i2c-adapter",
133 .dev_attrs = i2c_adapter_attrs,
134};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136
137static void i2c_client_release(struct device *dev)
138{
139 struct i2c_client *client = to_i2c_client(dev);
140 complete(&client->released);
141}
142
Yani Ioannoue404e272005-05-17 06:42:58 -0400143static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct i2c_client *client = to_i2c_client(dev);
146 return sprintf(buf, "%s\n", client->name);
147}
148
David Brownell438d6c22006-12-10 21:21:31 +0100149/*
Jean Delvare7b77d062006-12-10 21:21:31 +0100150 * We can't use the DEVICE_ATTR() macro here, as we used the same name for
151 * an i2c adapter attribute (above).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Jean Delvare7b77d062006-12-10 21:21:31 +0100153static struct device_attribute dev_attr_client_name =
154 __ATTR(name, S_IRUGO, &show_client_name, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156
157/* ---------------------------------------------------
David Brownell438d6c22006-12-10 21:21:31 +0100158 * registering functions
159 * ---------------------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
161
162/* -----
163 * i2c_add_adapter is called from within the algorithm layer,
164 * when a new hw adapter registers. A new device is register to be
165 * available for clients.
166 */
167int i2c_add_adapter(struct i2c_adapter *adap)
168{
169 int id, res = 0;
170 struct list_head *item;
171 struct i2c_driver *driver;
172
Arjan van de Venb3585e42006-01-11 10:50:26 +0100173 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
176 res = -ENOMEM;
177 goto out_unlock;
178 }
179
Mark M. Hoffmana0920e102005-06-28 00:21:30 -0400180 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (res < 0) {
182 if (res == -EAGAIN)
183 res = -ENOMEM;
184 goto out_unlock;
185 }
186
187 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100188 mutex_init(&adap->bus_lock);
189 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 list_add_tail(&adap->list,&adapters);
191 INIT_LIST_HEAD(&adap->clients);
192
193 /* Add the adapter to the driver core.
194 * If the parent pointer is not set up,
195 * we add this adapter to the host bus.
196 */
David Brownellb119dc32007-01-04 13:07:04 +0100197 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100199 pr_debug("I2C adapter driver [%s] forgot to specify "
200 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200204 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200205 res = device_register(&adap->dev);
206 if (res)
207 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200209 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* inform drivers of new adapters */
212 list_for_each(item,&drivers) {
213 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100214 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* We ignore the return code; if it fails, too bad */
216 driver->attach_adapter(adap);
217 }
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100220 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200222
Jean Delvareb119c6c2006-08-15 18:26:30 +0200223out_list:
224 list_del(&adap->list);
225 idr_remove(&i2c_adapter_idr, adap->nr);
226 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229
230int i2c_del_adapter(struct i2c_adapter *adap)
231{
232 struct list_head *item, *_n;
233 struct i2c_adapter *adap_from_list;
234 struct i2c_driver *driver;
235 struct i2c_client *client;
236 int res = 0;
237
Arjan van de Venb3585e42006-01-11 10:50:26 +0100238 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* First make sure that this adapter was ever added */
241 list_for_each_entry(adap_from_list, &adapters, list) {
242 if (adap_from_list == adap)
243 break;
244 }
245 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200246 pr_debug("i2c-core: attempting to delete unregistered "
247 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 res = -EINVAL;
249 goto out_unlock;
250 }
251
252 list_for_each(item,&drivers) {
253 driver = list_entry(item, struct i2c_driver, list);
254 if (driver->detach_adapter)
255 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200256 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100257 "for driver [%s]\n",
258 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 goto out_unlock;
260 }
261 }
262
263 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200264 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 list_for_each_safe(item, _n, &adap->clients) {
266 client = list_entry(item, struct i2c_client, list);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200269 dev_err(&adap->dev, "detach_client failed for client "
270 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 client->addr);
272 goto out_unlock;
273 }
274 }
275
276 /* clean up the sysfs representation */
277 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 device_unregister(&adap->dev);
279 list_del(&adap->list);
280
281 /* wait for sysfs to drop all references */
282 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 /* free dynamically allocated bus id */
285 idr_remove(&i2c_adapter_idr, adap->nr);
286
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200287 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100290 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return res;
292}
293
294
295/* -----
296 * What follows is the "upwards" interface: commands for talking to clients,
297 * which implement the functions to access the physical information of the
298 * chips.
299 */
300
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800301int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100303 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800306 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 res = driver_register(&driver->driver);
310 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100311 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100312
Jean Delvare7eebcb72006-02-05 23:28:21 +0100313 mutex_lock(&core_lists);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100316 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 /* now look for instances of driver on our adapters */
Jean Delvare8a994752005-11-26 20:28:06 +0100319 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200320 struct i2c_adapter *adapter;
321
322 list_for_each_entry(adapter, &adapters, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 driver->attach_adapter(adapter);
324 }
325 }
326
Arjan van de Venb3585e42006-01-11 10:50:26 +0100327 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800330EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332int i2c_del_driver(struct i2c_driver *driver)
333{
334 struct list_head *item1, *item2, *_n;
335 struct i2c_client *client;
336 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int res = 0;
339
Arjan van de Venb3585e42006-01-11 10:50:26 +0100340 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100343 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
346 list_for_each(item1,&adapters) {
347 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (driver->detach_adapter) {
349 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200350 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100351 "for driver [%s]\n",
352 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 goto out_unlock;
354 }
355 } else {
356 list_for_each_safe(item2, _n, &adap->clients) {
357 client = list_entry(item2, struct i2c_client, list);
358 if (client->driver != driver)
359 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200360 dev_dbg(&adap->dev, "detaching client [%s] "
361 "at 0x%02x\n", client->name,
362 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200364 dev_err(&adap->dev, "detach_client "
365 "failed for client [%s] at "
366 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 client->addr);
368 goto out_unlock;
369 }
370 }
371 }
372 }
373
374 driver_unregister(&driver->driver);
375 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100376 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100379 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return 0;
381}
382
383static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
384{
385 struct list_head *item;
386 struct i2c_client *client;
387
388 list_for_each(item,&adapter->clients) {
389 client = list_entry(item, struct i2c_client, list);
390 if (client->addr == addr)
391 return -EBUSY;
392 }
393 return 0;
394}
395
396int i2c_check_addr(struct i2c_adapter *adapter, int addr)
397{
398 int rval;
399
Ingo Molnar5c085d32006-01-18 23:16:04 +0100400 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100402 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 return rval;
405}
406
407int i2c_attach_client(struct i2c_client *client)
408{
409 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200410 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ingo Molnar5c085d32006-01-18 23:16:04 +0100412 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200414 res = -EBUSY;
415 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100418
Jean Delvarecde78592005-11-26 21:00:54 +0100419 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 client->dev.parent = &client->adapter->dev;
422 client->dev.driver = &client->driver->driver;
423 client->dev.bus = &i2c_bus_type;
424 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
427 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200428 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
429 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200430 res = device_register(&client->dev);
431 if (res)
432 goto out_list;
433 res = device_create_file(&client->dev, &dev_attr_client_name);
434 if (res)
435 goto out_unregister;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200436 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200437
438 if (adapter->client_register) {
439 if (adapter->client_register(client)) {
440 dev_dbg(&adapter->dev, "client_register "
441 "failed for client [%s] at 0x%02x\n",
442 client->name, client->addr);
443 }
444 }
445
446 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200447
448out_unregister:
449 init_completion(&client->released); /* Needed? */
450 device_unregister(&client->dev);
451 wait_for_completion(&client->released);
452out_list:
453 list_del(&client->list);
454 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
455 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200456out_unlock:
457 mutex_unlock(&adapter->clist_lock);
458 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461
462int i2c_detach_client(struct i2c_client *client)
463{
464 struct i2c_adapter *adapter = client->adapter;
465 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100466
Jean Delvarecde78592005-11-26 21:00:54 +0100467 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200468 dev_warn(&client->dev, "Client [%s] still busy, "
469 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if (adapter->client_unregister) {
474 res = adapter->client_unregister(client);
475 if (res) {
476 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700477 "client_unregister [%s] failed, "
478 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
480 }
481 }
482
Ingo Molnar5c085d32006-01-18 23:16:04 +0100483 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 list_del(&client->list);
485 init_completion(&client->released);
486 device_remove_file(&client->dev, &dev_attr_client_name);
487 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100488 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 wait_for_completion(&client->released);
490
491 out:
492 return res;
493}
494
495static int i2c_inc_use_client(struct i2c_client *client)
496{
497
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100498 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return -ENODEV;
500 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100501 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -ENODEV;
503 }
504
505 return 0;
506}
507
508static void i2c_dec_use_client(struct i2c_client *client)
509{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100510 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 module_put(client->adapter->owner);
512}
513
514int i2c_use_client(struct i2c_client *client)
515{
516 int ret;
517
518 ret = i2c_inc_use_client(client);
519 if (ret)
520 return ret;
521
Jean Delvarecde78592005-11-26 21:00:54 +0100522 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527int i2c_release_client(struct i2c_client *client)
528{
Jean Delvarecde78592005-11-26 21:00:54 +0100529 if (!client->usage_count) {
530 pr_debug("i2c-core: %s used one too many times\n",
531 __FUNCTION__);
532 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
David Brownell438d6c22006-12-10 21:21:31 +0100534
Jean Delvarecde78592005-11-26 21:00:54 +0100535 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539}
540
541void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
542{
543 struct list_head *item;
544 struct i2c_client *client;
545
Ingo Molnar5c085d32006-01-18 23:16:04 +0100546 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 list_for_each(item,&adap->clients) {
548 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100549 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 continue;
551 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100552 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100554 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100556 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100558 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561static int __init i2c_init(void)
562{
563 int retval;
564
565 retval = bus_register(&i2c_bus_type);
566 if (retval)
567 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return class_register(&i2c_adapter_class);
569}
570
571static void __exit i2c_exit(void)
572{
573 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 bus_unregister(&i2c_bus_type);
575}
576
577subsys_initcall(i2c_init);
578module_exit(i2c_exit);
579
580/* ----------------------------------------------------
581 * the functional interface to the i2c busses.
582 * ----------------------------------------------------
583 */
584
585int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
586{
587 int ret;
588
589 if (adap->algo->master_xfer) {
590#ifdef DEBUG
591 for (ret = 0; ret < num; ret++) {
592 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
593 "len=%d\n", ret, msgs[ret].flags & I2C_M_RD ?
594 'R' : 'W', msgs[ret].addr, msgs[ret].len);
595 }
596#endif
597
Jiri Kosina6ea23032006-12-10 21:21:30 +0100598 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100600 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 return ret;
603 } else {
604 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
605 return -ENOSYS;
606 }
607}
608
609int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
610{
611 int ret;
612 struct i2c_adapter *adap=client->adapter;
613 struct i2c_msg msg;
614
Jean Delvare815f55f2005-05-07 22:58:46 +0200615 msg.addr = client->addr;
616 msg.flags = client->flags & I2C_M_TEN;
617 msg.len = count;
618 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100619
Jean Delvare815f55f2005-05-07 22:58:46 +0200620 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Jean Delvare815f55f2005-05-07 22:58:46 +0200622 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
623 transmitted, else error code. */
624 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
627int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
628{
629 struct i2c_adapter *adap=client->adapter;
630 struct i2c_msg msg;
631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Jean Delvare815f55f2005-05-07 22:58:46 +0200633 msg.addr = client->addr;
634 msg.flags = client->flags & I2C_M_TEN;
635 msg.flags |= I2C_M_RD;
636 msg.len = count;
637 msg.buf = buf;
638
639 ret = i2c_transfer(adap, &msg, 1);
640
641 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
642 transmitted, else error code. */
643 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646
647int i2c_control(struct i2c_client *client,
648 unsigned int cmd, unsigned long arg)
649{
650 int ret = 0;
651 struct i2c_adapter *adap = client->adapter;
652
653 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
654 switch (cmd) {
655 case I2C_RETRIES:
656 adap->retries = arg;
657 break;
658 case I2C_TIMEOUT:
659 adap->timeout = arg;
660 break;
661 default:
662 if (adap->algo->algo_control!=NULL)
663 ret = adap->algo->algo_control(adap,cmd,arg);
664 }
665 return ret;
666}
667
668/* ----------------------------------------------------
669 * the i2c address scanning function
670 * Will not work for 10-bit addresses!
671 * ----------------------------------------------------
672 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200673static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
674 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200675{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200676 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200677
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200678 /* Make sure the address is valid */
679 if (addr < 0x03 || addr > 0x77) {
680 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
681 addr);
682 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200683 }
684
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200685 /* Skip if already in use */
686 if (i2c_check_addr(adapter, addr))
687 return 0;
688
689 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200690 if (kind < 0) {
691 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
692 I2C_SMBUS_QUICK, NULL) < 0)
693 return 0;
694
695 /* prevent 24RF08 corruption */
696 if ((addr & ~0x0f) == 0x50)
697 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
698 I2C_SMBUS_QUICK, NULL);
699 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200700
701 /* Finally call the custom detection function */
702 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200703 /* -ENODEV can be returned if there is a chip at the given address
704 but it isn't supported by this chip driver. We catch it here as
705 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200706 if (err == -ENODEV)
707 err = 0;
708
709 if (err)
710 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
711 addr, err);
712 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715int i2c_probe(struct i2c_adapter *adapter,
716 struct i2c_client_address_data *address_data,
717 int (*found_proc) (struct i2c_adapter *, int, int))
718{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200719 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int adap_id = i2c_adapter_id(adapter);
721
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200722 /* Force entries are done first, and are not affected by ignore
723 entries */
724 if (address_data->forces) {
725 unsigned short **forces = address_data->forces;
726 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200728 for (kind = 0; forces[kind]; kind++) {
729 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
730 i += 2) {
731 if (forces[kind][i] == adap_id
732 || forces[kind][i] == ANY_I2C_BUS) {
733 dev_dbg(&adapter->dev, "found force "
734 "parameter for adapter %d, "
735 "addr 0x%02x, kind %d\n",
736 adap_id, forces[kind][i + 1],
737 kind);
738 err = i2c_probe_address(adapter,
739 forces[kind][i + 1],
740 kind, found_proc);
741 if (err)
742 return err;
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200747
Jean Delvare4366dc92005-09-25 16:50:06 +0200748 /* Stop here if we can't use SMBUS_QUICK */
749 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
750 if (address_data->probe[0] == I2C_CLIENT_END
751 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100752 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200753
754 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
755 "can't probe for chips\n");
756 return -1;
757 }
758
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200759 /* Probe entries are done second, and are not affected by ignore
760 entries either */
761 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
762 if (address_data->probe[i] == adap_id
763 || address_data->probe[i] == ANY_I2C_BUS) {
764 dev_dbg(&adapter->dev, "found probe parameter for "
765 "adapter %d, addr 0x%02x\n", adap_id,
766 address_data->probe[i + 1]);
767 err = i2c_probe_address(adapter,
768 address_data->probe[i + 1],
769 -1, found_proc);
770 if (err)
771 return err;
772 }
773 }
774
775 /* Normal entries are done last, unless shadowed by an ignore entry */
776 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
777 int j, ignore;
778
779 ignore = 0;
780 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
781 j += 2) {
782 if ((address_data->ignore[j] == adap_id ||
783 address_data->ignore[j] == ANY_I2C_BUS)
784 && address_data->ignore[j + 1]
785 == address_data->normal_i2c[i]) {
786 dev_dbg(&adapter->dev, "found ignore "
787 "parameter for adapter %d, "
788 "addr 0x%02x\n", adap_id,
789 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200790 ignore = 1;
791 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200792 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200793 }
794 if (ignore)
795 continue;
796
797 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
798 "addr 0x%02x\n", adap_id,
799 address_data->normal_i2c[i]);
800 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
801 -1, found_proc);
802 if (err)
803 return err;
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809struct i2c_adapter* i2c_get_adapter(int id)
810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100812
Arjan van de Venb3585e42006-01-11 10:50:26 +0100813 mutex_lock(&core_lists);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -0400814 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
815 if (adapter && !try_module_get(adapter->owner))
816 adapter = NULL;
817
Arjan van de Venb3585e42006-01-11 10:50:26 +0100818 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -0400819 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822void i2c_put_adapter(struct i2c_adapter *adap)
823{
824 module_put(adap->owner);
825}
826
827/* The SMBus parts */
828
David Brownell438d6c22006-12-10 21:21:31 +0100829#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static u8
831crc8(u16 data)
832{
833 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100836 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 data = data ^ POLY;
838 data = data << 1;
839 }
840 return (u8)(data >> 8);
841}
842
Jean Delvare421ef472005-10-26 21:28:55 +0200843/* Incremental CRC8 over count bytes in the array pointed to by p */
844static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
846 int i;
847
848 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200849 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return crc;
851}
852
Jean Delvare421ef472005-10-26 21:28:55 +0200853/* Assume a 7-bit address, which is reasonable for SMBus */
854static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Jean Delvare421ef472005-10-26 21:28:55 +0200856 /* The address will be sent first */
857 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
858 pec = i2c_smbus_pec(pec, &addr, 1);
859
860 /* The data buffer follows */
861 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Jean Delvare421ef472005-10-26 21:28:55 +0200864/* Used for write only transactions */
865static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Jean Delvare421ef472005-10-26 21:28:55 +0200867 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
868 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Jean Delvare421ef472005-10-26 21:28:55 +0200871/* Return <0 on CRC error
872 If there was a write before this read (most cases) we need to take the
873 partial CRC from the write part into account.
874 Note that this function does modify the message (we need to decrease the
875 message length to hide the CRC byte from the caller). */
876static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Jean Delvare421ef472005-10-26 21:28:55 +0200878 u8 rpec = msg->buf[--msg->len];
879 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (rpec != cpec) {
882 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
883 rpec, cpec);
884 return -1;
885 }
David Brownell438d6c22006-12-10 21:21:31 +0100886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
890{
891 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100892 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895s32 i2c_smbus_read_byte(struct i2c_client *client)
896{
897 union i2c_smbus_data data;
898 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
899 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
900 return -1;
901 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200902 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
906{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200908 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
912{
913 union i2c_smbus_data data;
914 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
915 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
916 return -1;
917 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200918 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
920
921s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
922{
923 union i2c_smbus_data data;
924 data.byte = value;
925 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
926 I2C_SMBUS_WRITE,command,
927 I2C_SMBUS_BYTE_DATA,&data);
928}
929
930s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
931{
932 union i2c_smbus_data data;
933 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
934 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
935 return -1;
936 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200937 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
941{
942 union i2c_smbus_data data;
943 data.word = value;
944 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
945 I2C_SMBUS_WRITE,command,
946 I2C_SMBUS_WORD_DATA,&data);
947}
948
949s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200950 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (length > I2C_SMBUS_BLOCK_MAX)
955 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +0100957 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
959 I2C_SMBUS_WRITE,command,
960 I2C_SMBUS_BLOCK_DATA,&data);
961}
962
963/* Returns the number of read bytes */
964s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
965{
966 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +0100967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
969 I2C_SMBUS_READ,command,
970 I2C_SMBUS_I2C_BLOCK_DATA,&data))
971 return -1;
Jean Delvare76560322006-01-18 23:14:55 +0100972
973 memcpy(values, &data.block[1], data.block[0]);
974 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
Jean Delvare21bbd692006-01-09 15:19:18 +1100977s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +0200978 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +1100979{
980 union i2c_smbus_data data;
981
982 if (length > I2C_SMBUS_BLOCK_MAX)
983 length = I2C_SMBUS_BLOCK_MAX;
984 data.block[0] = length;
985 memcpy(data.block + 1, values, length);
986 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
987 I2C_SMBUS_WRITE, command,
988 I2C_SMBUS_I2C_BLOCK_DATA, &data);
989}
990
David Brownell438d6c22006-12-10 21:21:31 +0100991/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +0100993static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +0100995 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 union i2c_smbus_data * data)
997{
998 /* So we need to generate a series of msgs. In the case of writing, we
999 need to use only one message; when reading, we need two. We initialize
1000 most things with sane defaults, to keep the code below somewhat
1001 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001002 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1003 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001005 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1007 };
1008 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001009 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 msgbuf0[0] = command;
1012 switch(size) {
1013 case I2C_SMBUS_QUICK:
1014 msg[0].len = 0;
1015 /* Special case: The read/write field is used as data */
1016 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1017 num = 1;
1018 break;
1019 case I2C_SMBUS_BYTE:
1020 if (read_write == I2C_SMBUS_READ) {
1021 /* Special case: only a read! */
1022 msg[0].flags = I2C_M_RD | flags;
1023 num = 1;
1024 }
1025 break;
1026 case I2C_SMBUS_BYTE_DATA:
1027 if (read_write == I2C_SMBUS_READ)
1028 msg[1].len = 1;
1029 else {
1030 msg[0].len = 2;
1031 msgbuf0[1] = data->byte;
1032 }
1033 break;
1034 case I2C_SMBUS_WORD_DATA:
1035 if (read_write == I2C_SMBUS_READ)
1036 msg[1].len = 2;
1037 else {
1038 msg[0].len=3;
1039 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001040 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042 break;
1043 case I2C_SMBUS_PROC_CALL:
1044 num = 2; /* Special case */
1045 read_write = I2C_SMBUS_READ;
1046 msg[0].len = 3;
1047 msg[1].len = 2;
1048 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001049 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
1051 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (read_write == I2C_SMBUS_READ) {
1053 dev_err(&adapter->dev, "Block read not supported "
1054 "under I2C emulation!\n");
1055 return -1;
1056 } else {
1057 msg[0].len = data->block[0] + 2;
1058 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1059 dev_err(&adapter->dev, "smbus_access called with "
1060 "invalid block write size (%d)\n",
1061 data->block[0]);
1062 return -1;
1063 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001064 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 msgbuf0[i] = data->block[i-1];
1066 }
1067 break;
1068 case I2C_SMBUS_BLOCK_PROC_CALL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 dev_dbg(&adapter->dev, "Block process call not supported "
1070 "under I2C emulation!\n");
1071 return -1;
1072 case I2C_SMBUS_I2C_BLOCK_DATA:
1073 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001074 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 } else {
1076 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001077 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1079 "invalid block write size (%d)\n",
1080 data->block[0]);
1081 return -1;
1082 }
1083 for (i = 1; i <= data->block[0]; i++)
1084 msgbuf0[i] = data->block[i];
1085 }
1086 break;
1087 default:
1088 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1089 size);
1090 return -1;
1091 }
1092
Jean Delvare421ef472005-10-26 21:28:55 +02001093 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1094 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1095 if (i) {
1096 /* Compute PEC if first message is a write */
1097 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001098 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001099 i2c_smbus_add_pec(&msg[0]);
1100 else /* Write followed by read */
1101 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1102 }
1103 /* Ask for PEC if last message is a read */
1104 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001105 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (i2c_transfer(adapter, msg, num) < 0)
1109 return -1;
1110
Jean Delvare421ef472005-10-26 21:28:55 +02001111 /* Check PEC if last message is a read */
1112 if (i && (msg[num-1].flags & I2C_M_RD)) {
1113 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1114 return -1;
1115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (read_write == I2C_SMBUS_READ)
1118 switch(size) {
1119 case I2C_SMBUS_BYTE:
1120 data->byte = msgbuf0[0];
1121 break;
1122 case I2C_SMBUS_BYTE_DATA:
1123 data->byte = msgbuf1[0];
1124 break;
David Brownell438d6c22006-12-10 21:21:31 +01001125 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 case I2C_SMBUS_PROC_CALL:
1127 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1128 break;
1129 case I2C_SMBUS_I2C_BLOCK_DATA:
1130 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001131 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1132 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 data->block[i+1] = msgbuf1[i];
1134 break;
1135 }
1136 return 0;
1137}
1138
1139
1140s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001141 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 union i2c_smbus_data * data)
1143{
1144 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001149 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1151 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001152 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 } else
1154 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1155 command,size,data);
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return res;
1158}
1159
1160
Jean Delvareb31366f2007-05-01 23:26:28 +02001161/* Next three are needed by i2c-isa */
Jean Delvareefde7232005-07-20 23:03:50 +02001162EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
Jean Delvareefde7232005-07-20 23:03:50 +02001163EXPORT_SYMBOL_GPL(i2c_adapter_class);
1164EXPORT_SYMBOL_GPL(i2c_bus_type);
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166EXPORT_SYMBOL(i2c_add_adapter);
1167EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168EXPORT_SYMBOL(i2c_del_driver);
1169EXPORT_SYMBOL(i2c_attach_client);
1170EXPORT_SYMBOL(i2c_detach_client);
1171EXPORT_SYMBOL(i2c_use_client);
1172EXPORT_SYMBOL(i2c_release_client);
1173EXPORT_SYMBOL(i2c_clients_command);
1174EXPORT_SYMBOL(i2c_check_addr);
1175
1176EXPORT_SYMBOL(i2c_master_send);
1177EXPORT_SYMBOL(i2c_master_recv);
1178EXPORT_SYMBOL(i2c_control);
1179EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180EXPORT_SYMBOL(i2c_get_adapter);
1181EXPORT_SYMBOL(i2c_put_adapter);
1182EXPORT_SYMBOL(i2c_probe);
1183
1184EXPORT_SYMBOL(i2c_smbus_xfer);
1185EXPORT_SYMBOL(i2c_smbus_write_quick);
1186EXPORT_SYMBOL(i2c_smbus_read_byte);
1187EXPORT_SYMBOL(i2c_smbus_write_byte);
1188EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1189EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1190EXPORT_SYMBOL(i2c_smbus_read_word_data);
1191EXPORT_SYMBOL(i2c_smbus_write_word_data);
1192EXPORT_SYMBOL(i2c_smbus_write_block_data);
1193EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001194EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1197MODULE_DESCRIPTION("I2C-Bus main module");
1198MODULE_LICENSE("GPL");