blob: 3aac1b5c7cbdf9d48886a95dbcbdb4f26d8d54b6 [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 Brownella1d9e6e2007-05-01 23:26:30 +020044#define is_newstyle_driver(d) ((d)->probe || (d)->remove)
David Brownellf37dd802007-02-13 22:09:00 +010045
46/* ------------------------------------------------------------------------- */
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int i2c_device_match(struct device *dev, struct device_driver *drv)
49{
David Brownell7b4fbc52007-05-01 23:26:30 +020050 struct i2c_client *client = to_i2c_client(dev);
51 struct i2c_driver *driver = to_i2c_driver(drv);
52
53 /* make legacy i2c drivers bypass driver model probing entirely;
54 * such drivers scan each i2c adapter/bus themselves.
55 */
David Brownella1d9e6e2007-05-01 23:26:30 +020056 if (!is_newstyle_driver(driver))
David Brownell7b4fbc52007-05-01 23:26:30 +020057 return 0;
58
59 /* new style drivers use the same kind of driver matching policy
60 * as platform devices or SPI: compare device and driver IDs.
61 */
62 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
David Brownell7b4fbc52007-05-01 23:26:30 +020065#ifdef CONFIG_HOTPLUG
66
67/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
68static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
69 char *buffer, int buffer_size)
70{
71 struct i2c_client *client = to_i2c_client(dev);
72 int i = 0, length = 0;
73
74 /* by definition, legacy drivers can't hotplug */
75 if (dev->driver || !client->driver_name)
76 return 0;
77
78 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
79 "MODALIAS=%s", client->driver_name))
80 return -ENOMEM;
81 envp[i] = NULL;
82 dev_dbg(dev, "uevent\n");
83 return 0;
84}
85
86#else
87#define i2c_device_uevent NULL
88#endif /* CONFIG_HOTPLUG */
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static int i2c_device_probe(struct device *dev)
91{
David Brownell7b4fbc52007-05-01 23:26:30 +020092 struct i2c_client *client = to_i2c_client(dev);
93 struct i2c_driver *driver = to_i2c_driver(dev->driver);
94
95 if (!driver->probe)
96 return -ENODEV;
97 client->driver = driver;
98 dev_dbg(dev, "probe\n");
99 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static int i2c_device_remove(struct device *dev)
103{
David Brownella1d9e6e2007-05-01 23:26:30 +0200104 struct i2c_client *client = to_i2c_client(dev);
105 struct i2c_driver *driver;
106 int status;
107
108 if (!dev->driver)
109 return 0;
110
111 driver = to_i2c_driver(dev->driver);
112 if (driver->remove) {
113 dev_dbg(dev, "remove\n");
114 status = driver->remove(client);
115 } else {
116 dev->driver = NULL;
117 status = 0;
118 }
119 if (status == 0)
120 client->driver = NULL;
121 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
David Brownellf37dd802007-02-13 22:09:00 +0100124static void i2c_device_shutdown(struct device *dev)
125{
126 struct i2c_driver *driver;
127
128 if (!dev->driver)
129 return;
130 driver = to_i2c_driver(dev->driver);
131 if (driver->shutdown)
132 driver->shutdown(to_i2c_client(dev));
133}
134
135static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
136{
137 struct i2c_driver *driver;
138
139 if (!dev->driver)
140 return 0;
141 driver = to_i2c_driver(dev->driver);
142 if (!driver->suspend)
143 return 0;
144 return driver->suspend(to_i2c_client(dev), mesg);
145}
146
147static int i2c_device_resume(struct device * dev)
148{
149 struct i2c_driver *driver;
150
151 if (!dev->driver)
152 return 0;
153 driver = to_i2c_driver(dev->driver);
154 if (!driver->resume)
155 return 0;
156 return driver->resume(to_i2c_client(dev));
157}
158
David Brownell7b4fbc52007-05-01 23:26:30 +0200159static void i2c_client_release(struct device *dev)
160{
161 struct i2c_client *client = to_i2c_client(dev);
162 complete(&client->released);
163}
164
165static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
166{
167 struct i2c_client *client = to_i2c_client(dev);
168 return sprintf(buf, "%s\n", client->name);
169}
170
171static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
172{
173 struct i2c_client *client = to_i2c_client(dev);
174 return client->driver_name
175 ? sprintf(buf, "%s\n", client->driver_name)
176 : 0;
177}
178
179static struct device_attribute i2c_dev_attrs[] = {
180 __ATTR(name, S_IRUGO, show_client_name, NULL),
181 /* modalias helps coldplug: modprobe $(cat .../modalias) */
182 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
183 { },
184};
185
Russell Kingb864c7d2006-01-05 14:37:50 +0000186struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100187 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200188 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100189 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200190 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100191 .probe = i2c_device_probe,
192 .remove = i2c_device_remove,
193 .shutdown = i2c_device_shutdown,
194 .suspend = i2c_device_suspend,
195 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000196};
197
David Brownella1d9e6e2007-05-01 23:26:30 +0200198static void i2c_unregister_device(struct i2c_client *client)
199{
200 struct i2c_adapter *adapter = client->adapter;
201 struct i2c_driver *driver = client->driver;
202
203 if (driver && !is_newstyle_driver(driver)) {
204 dev_err(&client->dev, "can't unregister devices "
205 "with legacy drivers\n");
206 WARN_ON(1);
207 return;
208 }
209
210 mutex_lock(&adapter->clist_lock);
211 list_del(&client->list);
212 mutex_unlock(&adapter->clist_lock);
213
214 device_unregister(&client->dev);
215}
216
217
David Brownellf37dd802007-02-13 22:09:00 +0100218/* ------------------------------------------------------------------------- */
219
David Brownell16ffadf2007-05-01 23:26:28 +0200220/* I2C bus adapters -- one roots each I2C or SMBUS segment */
221
Jean Delvareefde7232005-07-20 23:03:50 +0200222void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
David Brownellef2c8322007-05-01 23:26:28 +0200224 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 complete(&adap->dev_released);
226}
227
David Brownell16ffadf2007-05-01 23:26:28 +0200228static ssize_t
229show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
David Brownellef2c8322007-05-01 23:26:28 +0200231 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return sprintf(buf, "%s\n", adap->name);
233}
David Brownell16ffadf2007-05-01 23:26:28 +0200234
235static struct device_attribute i2c_adapter_attrs[] = {
236 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
237 { },
238};
239
240struct class i2c_adapter_class = {
241 .owner = THIS_MODULE,
242 .name = "i2c-adapter",
243 .dev_attrs = i2c_adapter_attrs,
244};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247/* -----
248 * i2c_add_adapter is called from within the algorithm layer,
249 * when a new hw adapter registers. A new device is register to be
250 * available for clients.
251 */
252int i2c_add_adapter(struct i2c_adapter *adap)
253{
254 int id, res = 0;
255 struct list_head *item;
256 struct i2c_driver *driver;
257
Arjan van de Venb3585e42006-01-11 10:50:26 +0100258 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
261 res = -ENOMEM;
262 goto out_unlock;
263 }
264
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400265 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 if (res < 0) {
267 if (res == -EAGAIN)
268 res = -ENOMEM;
269 goto out_unlock;
270 }
271
272 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100273 mutex_init(&adap->bus_lock);
274 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 list_add_tail(&adap->list,&adapters);
276 INIT_LIST_HEAD(&adap->clients);
277
278 /* Add the adapter to the driver core.
279 * If the parent pointer is not set up,
280 * we add this adapter to the host bus.
281 */
David Brownellb119dc32007-01-04 13:07:04 +0100282 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100284 pr_debug("I2C adapter driver [%s] forgot to specify "
285 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200289 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200290 res = device_register(&adap->dev);
291 if (res)
292 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200294 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
295
David Brownell7b4fbc52007-05-01 23:26:30 +0200296 /* let legacy drivers scan this bus for matching devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 list_for_each(item,&drivers) {
298 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100299 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 /* We ignore the return code; if it fails, too bad */
301 driver->attach_adapter(adap);
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100305 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200307
Jean Delvareb119c6c2006-08-15 18:26:30 +0200308out_list:
309 list_del(&adap->list);
310 idr_remove(&i2c_adapter_idr, adap->nr);
311 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314
315int i2c_del_adapter(struct i2c_adapter *adap)
316{
317 struct list_head *item, *_n;
318 struct i2c_adapter *adap_from_list;
319 struct i2c_driver *driver;
320 struct i2c_client *client;
321 int res = 0;
322
Arjan van de Venb3585e42006-01-11 10:50:26 +0100323 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* First make sure that this adapter was ever added */
326 list_for_each_entry(adap_from_list, &adapters, list) {
327 if (adap_from_list == adap)
328 break;
329 }
330 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200331 pr_debug("i2c-core: attempting to delete unregistered "
332 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 res = -EINVAL;
334 goto out_unlock;
335 }
336
337 list_for_each(item,&drivers) {
338 driver = list_entry(item, struct i2c_driver, list);
339 if (driver->detach_adapter)
340 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200341 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100342 "for driver [%s]\n",
343 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto out_unlock;
345 }
346 }
347
348 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200349 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 list_for_each_safe(item, _n, &adap->clients) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200351 struct i2c_driver *driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
David Brownella1d9e6e2007-05-01 23:26:30 +0200353 client = list_entry(item, struct i2c_client, list);
354 driver = client->driver;
355
356 /* new style, follow standard driver model */
357 if (!driver || is_newstyle_driver(driver)) {
358 i2c_unregister_device(client);
359 continue;
360 }
361
362 /* legacy drivers create and remove clients themselves */
363 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200364 dev_err(&adap->dev, "detach_client failed for client "
365 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 client->addr);
367 goto out_unlock;
368 }
369 }
370
371 /* clean up the sysfs representation */
372 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 device_unregister(&adap->dev);
374 list_del(&adap->list);
375
376 /* wait for sysfs to drop all references */
377 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* free dynamically allocated bus id */
380 idr_remove(&i2c_adapter_idr, adap->nr);
381
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200382 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100385 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return res;
387}
388
389
David Brownell7b4fbc52007-05-01 23:26:30 +0200390/* ------------------------------------------------------------------------- */
391
392/*
393 * An i2c_driver is used with one or more i2c_client (device) nodes to access
394 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
395 * are two models for binding the driver to its device: "new style" drivers
396 * follow the standard Linux driver model and just respond to probe() calls
397 * issued if the driver core sees they match(); "legacy" drivers create device
398 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
400
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800401int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100403 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
David Brownell7b4fbc52007-05-01 23:26:30 +0200405 /* new style driver methods can't mix with legacy ones */
David Brownella1d9e6e2007-05-01 23:26:30 +0200406 if (is_newstyle_driver(driver)) {
David Brownell7b4fbc52007-05-01 23:26:30 +0200407 if (driver->attach_adapter || driver->detach_adapter
408 || driver->detach_client) {
409 printk(KERN_WARNING
410 "i2c-core: driver [%s] is confused\n",
411 driver->driver.name);
412 return -EINVAL;
413 }
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800417 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 res = driver_register(&driver->driver);
421 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100422 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100423
Jean Delvare7eebcb72006-02-05 23:28:21 +0100424 mutex_lock(&core_lists);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100427 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Brownell7b4fbc52007-05-01 23:26:30 +0200429 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100430 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200431 struct i2c_adapter *adapter;
432
433 list_for_each_entry(adapter, &adapters, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 driver->attach_adapter(adapter);
435 }
436 }
437
Arjan van de Venb3585e42006-01-11 10:50:26 +0100438 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800441EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
David Brownella1d9e6e2007-05-01 23:26:30 +0200443/**
444 * i2c_del_driver - unregister I2C driver
445 * @driver: the driver being unregistered
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447int i2c_del_driver(struct i2c_driver *driver)
448{
449 struct list_head *item1, *item2, *_n;
450 struct i2c_client *client;
451 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int res = 0;
454
Arjan van de Venb3585e42006-01-11 10:50:26 +0100455 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
David Brownella1d9e6e2007-05-01 23:26:30 +0200457 /* new-style driver? */
458 if (is_newstyle_driver(driver))
459 goto unregister;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100462 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
465 list_for_each(item1,&adapters) {
466 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (driver->detach_adapter) {
468 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200469 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100470 "for driver [%s]\n",
471 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto out_unlock;
473 }
474 } else {
475 list_for_each_safe(item2, _n, &adap->clients) {
476 client = list_entry(item2, struct i2c_client, list);
477 if (client->driver != driver)
478 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200479 dev_dbg(&adap->dev, "detaching client [%s] "
480 "at 0x%02x\n", client->name,
481 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200483 dev_err(&adap->dev, "detach_client "
484 "failed for client [%s] at "
485 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 client->addr);
487 goto out_unlock;
488 }
489 }
490 }
491 }
492
David Brownella1d9e6e2007-05-01 23:26:30 +0200493 unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 driver_unregister(&driver->driver);
495 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100496 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100499 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
David Brownell7b4fbc52007-05-01 23:26:30 +0200503/* ------------------------------------------------------------------------- */
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
506{
507 struct list_head *item;
508 struct i2c_client *client;
509
510 list_for_each(item,&adapter->clients) {
511 client = list_entry(item, struct i2c_client, list);
512 if (client->addr == addr)
513 return -EBUSY;
514 }
515 return 0;
516}
517
518int i2c_check_addr(struct i2c_adapter *adapter, int addr)
519{
520 int rval;
521
Ingo Molnar5c085d32006-01-18 23:16:04 +0100522 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100524 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 return rval;
527}
528
529int i2c_attach_client(struct i2c_client *client)
530{
531 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200532 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Ingo Molnar5c085d32006-01-18 23:16:04 +0100534 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200536 res = -EBUSY;
537 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100540
Jean Delvarecde78592005-11-26 21:00:54 +0100541 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 client->dev.parent = &client->adapter->dev;
544 client->dev.driver = &client->driver->driver;
545 client->dev.bus = &i2c_bus_type;
546 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
549 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200550 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
551 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200552 res = device_register(&client->dev);
553 if (res)
554 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200555 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200556
557 if (adapter->client_register) {
558 if (adapter->client_register(client)) {
559 dev_dbg(&adapter->dev, "client_register "
560 "failed for client [%s] at 0x%02x\n",
561 client->name, client->addr);
562 }
563 }
564
565 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200566
Jean Delvareb119c6c2006-08-15 18:26:30 +0200567out_list:
568 list_del(&client->list);
569 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
570 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200571out_unlock:
572 mutex_unlock(&adapter->clist_lock);
573 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576
577int i2c_detach_client(struct i2c_client *client)
578{
579 struct i2c_adapter *adapter = client->adapter;
580 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100581
Jean Delvarecde78592005-11-26 21:00:54 +0100582 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200583 dev_warn(&client->dev, "Client [%s] still busy, "
584 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (adapter->client_unregister) {
589 res = adapter->client_unregister(client);
590 if (res) {
591 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700592 "client_unregister [%s] failed, "
593 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 goto out;
595 }
596 }
597
Ingo Molnar5c085d32006-01-18 23:16:04 +0100598 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 list_del(&client->list);
600 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100602 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 wait_for_completion(&client->released);
604
605 out:
606 return res;
607}
608
609static int i2c_inc_use_client(struct i2c_client *client)
610{
611
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100612 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -ENODEV;
614 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100615 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -ENODEV;
617 }
618
619 return 0;
620}
621
622static void i2c_dec_use_client(struct i2c_client *client)
623{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100624 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 module_put(client->adapter->owner);
626}
627
628int i2c_use_client(struct i2c_client *client)
629{
630 int ret;
631
632 ret = i2c_inc_use_client(client);
633 if (ret)
634 return ret;
635
Jean Delvarecde78592005-11-26 21:00:54 +0100636 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641int i2c_release_client(struct i2c_client *client)
642{
Jean Delvarecde78592005-11-26 21:00:54 +0100643 if (!client->usage_count) {
644 pr_debug("i2c-core: %s used one too many times\n",
645 __FUNCTION__);
646 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
David Brownell438d6c22006-12-10 21:21:31 +0100648
Jean Delvarecde78592005-11-26 21:00:54 +0100649 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return 0;
653}
654
655void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
656{
657 struct list_head *item;
658 struct i2c_client *client;
659
Ingo Molnar5c085d32006-01-18 23:16:04 +0100660 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 list_for_each(item,&adap->clients) {
662 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100663 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 continue;
665 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100666 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100668 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100670 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100672 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675static int __init i2c_init(void)
676{
677 int retval;
678
679 retval = bus_register(&i2c_bus_type);
680 if (retval)
681 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return class_register(&i2c_adapter_class);
683}
684
685static void __exit i2c_exit(void)
686{
687 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 bus_unregister(&i2c_bus_type);
689}
690
691subsys_initcall(i2c_init);
692module_exit(i2c_exit);
693
694/* ----------------------------------------------------
695 * the functional interface to the i2c busses.
696 * ----------------------------------------------------
697 */
698
699int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
700{
701 int ret;
702
703 if (adap->algo->master_xfer) {
704#ifdef DEBUG
705 for (ret = 0; ret < num; ret++) {
706 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200707 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
708 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
709 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711#endif
712
Jiri Kosina6ea23032006-12-10 21:21:30 +0100713 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100715 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 return ret;
718 } else {
719 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
720 return -ENOSYS;
721 }
722}
723
724int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
725{
726 int ret;
727 struct i2c_adapter *adap=client->adapter;
728 struct i2c_msg msg;
729
Jean Delvare815f55f2005-05-07 22:58:46 +0200730 msg.addr = client->addr;
731 msg.flags = client->flags & I2C_M_TEN;
732 msg.len = count;
733 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100734
Jean Delvare815f55f2005-05-07 22:58:46 +0200735 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Jean Delvare815f55f2005-05-07 22:58:46 +0200737 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
738 transmitted, else error code. */
739 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
743{
744 struct i2c_adapter *adap=client->adapter;
745 struct i2c_msg msg;
746 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Jean Delvare815f55f2005-05-07 22:58:46 +0200748 msg.addr = client->addr;
749 msg.flags = client->flags & I2C_M_TEN;
750 msg.flags |= I2C_M_RD;
751 msg.len = count;
752 msg.buf = buf;
753
754 ret = i2c_transfer(adap, &msg, 1);
755
756 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
757 transmitted, else error code. */
758 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761
762int i2c_control(struct i2c_client *client,
763 unsigned int cmd, unsigned long arg)
764{
765 int ret = 0;
766 struct i2c_adapter *adap = client->adapter;
767
768 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
769 switch (cmd) {
770 case I2C_RETRIES:
771 adap->retries = arg;
772 break;
773 case I2C_TIMEOUT:
774 adap->timeout = arg;
775 break;
776 default:
777 if (adap->algo->algo_control!=NULL)
778 ret = adap->algo->algo_control(adap,cmd,arg);
779 }
780 return ret;
781}
782
783/* ----------------------------------------------------
784 * the i2c address scanning function
785 * Will not work for 10-bit addresses!
786 * ----------------------------------------------------
787 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200788static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
789 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200790{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200791 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200792
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200793 /* Make sure the address is valid */
794 if (addr < 0x03 || addr > 0x77) {
795 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
796 addr);
797 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200798 }
799
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200800 /* Skip if already in use */
801 if (i2c_check_addr(adapter, addr))
802 return 0;
803
804 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200805 if (kind < 0) {
806 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
807 I2C_SMBUS_QUICK, NULL) < 0)
808 return 0;
809
810 /* prevent 24RF08 corruption */
811 if ((addr & ~0x0f) == 0x50)
812 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
813 I2C_SMBUS_QUICK, NULL);
814 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200815
816 /* Finally call the custom detection function */
817 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200818 /* -ENODEV can be returned if there is a chip at the given address
819 but it isn't supported by this chip driver. We catch it here as
820 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200821 if (err == -ENODEV)
822 err = 0;
823
824 if (err)
825 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
826 addr, err);
827 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830int i2c_probe(struct i2c_adapter *adapter,
831 struct i2c_client_address_data *address_data,
832 int (*found_proc) (struct i2c_adapter *, int, int))
833{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200834 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int adap_id = i2c_adapter_id(adapter);
836
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200837 /* Force entries are done first, and are not affected by ignore
838 entries */
839 if (address_data->forces) {
840 unsigned short **forces = address_data->forces;
841 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200843 for (kind = 0; forces[kind]; kind++) {
844 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
845 i += 2) {
846 if (forces[kind][i] == adap_id
847 || forces[kind][i] == ANY_I2C_BUS) {
848 dev_dbg(&adapter->dev, "found force "
849 "parameter for adapter %d, "
850 "addr 0x%02x, kind %d\n",
851 adap_id, forces[kind][i + 1],
852 kind);
853 err = i2c_probe_address(adapter,
854 forces[kind][i + 1],
855 kind, found_proc);
856 if (err)
857 return err;
858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200862
Jean Delvare4366dc92005-09-25 16:50:06 +0200863 /* Stop here if we can't use SMBUS_QUICK */
864 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
865 if (address_data->probe[0] == I2C_CLIENT_END
866 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100867 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200868
869 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
870 "can't probe for chips\n");
871 return -1;
872 }
873
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200874 /* Probe entries are done second, and are not affected by ignore
875 entries either */
876 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
877 if (address_data->probe[i] == adap_id
878 || address_data->probe[i] == ANY_I2C_BUS) {
879 dev_dbg(&adapter->dev, "found probe parameter for "
880 "adapter %d, addr 0x%02x\n", adap_id,
881 address_data->probe[i + 1]);
882 err = i2c_probe_address(adapter,
883 address_data->probe[i + 1],
884 -1, found_proc);
885 if (err)
886 return err;
887 }
888 }
889
890 /* Normal entries are done last, unless shadowed by an ignore entry */
891 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
892 int j, ignore;
893
894 ignore = 0;
895 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
896 j += 2) {
897 if ((address_data->ignore[j] == adap_id ||
898 address_data->ignore[j] == ANY_I2C_BUS)
899 && address_data->ignore[j + 1]
900 == address_data->normal_i2c[i]) {
901 dev_dbg(&adapter->dev, "found ignore "
902 "parameter for adapter %d, "
903 "addr 0x%02x\n", adap_id,
904 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200905 ignore = 1;
906 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200907 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200908 }
909 if (ignore)
910 continue;
911
912 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
913 "addr 0x%02x\n", adap_id,
914 address_data->normal_i2c[i]);
915 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
916 -1, found_proc);
917 if (err)
918 return err;
919 }
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 return 0;
922}
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924struct i2c_adapter* i2c_get_adapter(int id)
925{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100927
Arjan van de Venb3585e42006-01-11 10:50:26 +0100928 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400929 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
930 if (adapter && !try_module_get(adapter->owner))
931 adapter = NULL;
932
Arjan van de Venb3585e42006-01-11 10:50:26 +0100933 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400934 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
937void i2c_put_adapter(struct i2c_adapter *adap)
938{
939 module_put(adap->owner);
940}
941
942/* The SMBus parts */
943
David Brownell438d6c22006-12-10 21:21:31 +0100944#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static u8
946crc8(u16 data)
947{
948 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100951 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 data = data ^ POLY;
953 data = data << 1;
954 }
955 return (u8)(data >> 8);
956}
957
Jean Delvare421ef472005-10-26 21:28:55 +0200958/* Incremental CRC8 over count bytes in the array pointed to by p */
959static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int i;
962
963 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200964 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return crc;
966}
967
Jean Delvare421ef472005-10-26 21:28:55 +0200968/* Assume a 7-bit address, which is reasonable for SMBus */
969static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Jean Delvare421ef472005-10-26 21:28:55 +0200971 /* The address will be sent first */
972 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
973 pec = i2c_smbus_pec(pec, &addr, 1);
974
975 /* The data buffer follows */
976 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Jean Delvare421ef472005-10-26 21:28:55 +0200979/* Used for write only transactions */
980static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jean Delvare421ef472005-10-26 21:28:55 +0200982 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
983 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Jean Delvare421ef472005-10-26 21:28:55 +0200986/* Return <0 on CRC error
987 If there was a write before this read (most cases) we need to take the
988 partial CRC from the write part into account.
989 Note that this function does modify the message (we need to decrease the
990 message length to hide the CRC byte from the caller). */
991static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Jean Delvare421ef472005-10-26 21:28:55 +0200993 u8 rpec = msg->buf[--msg->len];
994 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (rpec != cpec) {
997 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
998 rpec, cpec);
999 return -1;
1000 }
David Brownell438d6c22006-12-10 21:21:31 +01001001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
1005{
1006 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +01001007 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010s32 i2c_smbus_read_byte(struct i2c_client *client)
1011{
1012 union i2c_smbus_data data;
1013 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1014 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
1015 return -1;
1016 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001017 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1021{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001023 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1027{
1028 union i2c_smbus_data data;
1029 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1030 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
1031 return -1;
1032 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001033 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1037{
1038 union i2c_smbus_data data;
1039 data.byte = value;
1040 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1041 I2C_SMBUS_WRITE,command,
1042 I2C_SMBUS_BYTE_DATA,&data);
1043}
1044
1045s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1046{
1047 union i2c_smbus_data data;
1048 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1049 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
1050 return -1;
1051 else
Jean Delvare7eff82c2006-09-03 22:24:00 +02001052 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1056{
1057 union i2c_smbus_data data;
1058 data.word = value;
1059 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1060 I2C_SMBUS_WRITE,command,
1061 I2C_SMBUS_WORD_DATA,&data);
1062}
1063
1064s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001065 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (length > I2C_SMBUS_BLOCK_MAX)
1070 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001072 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1074 I2C_SMBUS_WRITE,command,
1075 I2C_SMBUS_BLOCK_DATA,&data);
1076}
1077
1078/* Returns the number of read bytes */
1079s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1080{
1081 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1084 I2C_SMBUS_READ,command,
1085 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1086 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001087
1088 memcpy(values, &data.block[1], data.block[0]);
1089 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Jean Delvare21bbd692006-01-09 15:19:18 +11001092s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001093 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001094{
1095 union i2c_smbus_data data;
1096
1097 if (length > I2C_SMBUS_BLOCK_MAX)
1098 length = I2C_SMBUS_BLOCK_MAX;
1099 data.block[0] = length;
1100 memcpy(data.block + 1, values, length);
1101 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1102 I2C_SMBUS_WRITE, command,
1103 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1104}
1105
David Brownell438d6c22006-12-10 21:21:31 +01001106/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001108static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001110 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 union i2c_smbus_data * data)
1112{
1113 /* So we need to generate a series of msgs. In the case of writing, we
1114 need to use only one message; when reading, we need two. We initialize
1115 most things with sane defaults, to keep the code below somewhat
1116 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001117 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1118 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001120 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1122 };
1123 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001124 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 msgbuf0[0] = command;
1127 switch(size) {
1128 case I2C_SMBUS_QUICK:
1129 msg[0].len = 0;
1130 /* Special case: The read/write field is used as data */
1131 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1132 num = 1;
1133 break;
1134 case I2C_SMBUS_BYTE:
1135 if (read_write == I2C_SMBUS_READ) {
1136 /* Special case: only a read! */
1137 msg[0].flags = I2C_M_RD | flags;
1138 num = 1;
1139 }
1140 break;
1141 case I2C_SMBUS_BYTE_DATA:
1142 if (read_write == I2C_SMBUS_READ)
1143 msg[1].len = 1;
1144 else {
1145 msg[0].len = 2;
1146 msgbuf0[1] = data->byte;
1147 }
1148 break;
1149 case I2C_SMBUS_WORD_DATA:
1150 if (read_write == I2C_SMBUS_READ)
1151 msg[1].len = 2;
1152 else {
1153 msg[0].len=3;
1154 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001155 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 break;
1158 case I2C_SMBUS_PROC_CALL:
1159 num = 2; /* Special case */
1160 read_write = I2C_SMBUS_READ;
1161 msg[0].len = 3;
1162 msg[1].len = 2;
1163 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001164 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 break;
1166 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001168 msg[1].flags |= I2C_M_RECV_LEN;
1169 msg[1].len = 1; /* block length will be added by
1170 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 } else {
1172 msg[0].len = data->block[0] + 2;
1173 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1174 dev_err(&adapter->dev, "smbus_access called with "
1175 "invalid block write size (%d)\n",
1176 data->block[0]);
1177 return -1;
1178 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001179 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 msgbuf0[i] = data->block[i-1];
1181 }
1182 break;
1183 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001184 num = 2; /* Another special case */
1185 read_write = I2C_SMBUS_READ;
1186 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1187 dev_err(&adapter->dev, "%s called with invalid "
1188 "block proc call size (%d)\n", __FUNCTION__,
1189 data->block[0]);
1190 return -1;
1191 }
1192 msg[0].len = data->block[0] + 2;
1193 for (i = 1; i < msg[0].len; i++)
1194 msgbuf0[i] = data->block[i-1];
1195 msg[1].flags |= I2C_M_RECV_LEN;
1196 msg[1].len = 1; /* block length will be added by
1197 the underlying bus driver */
1198 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 case I2C_SMBUS_I2C_BLOCK_DATA:
1200 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001201 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 } else {
1203 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001204 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1206 "invalid block write size (%d)\n",
1207 data->block[0]);
1208 return -1;
1209 }
1210 for (i = 1; i <= data->block[0]; i++)
1211 msgbuf0[i] = data->block[i];
1212 }
1213 break;
1214 default:
1215 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1216 size);
1217 return -1;
1218 }
1219
Jean Delvare421ef472005-10-26 21:28:55 +02001220 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1221 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1222 if (i) {
1223 /* Compute PEC if first message is a write */
1224 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001225 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001226 i2c_smbus_add_pec(&msg[0]);
1227 else /* Write followed by read */
1228 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1229 }
1230 /* Ask for PEC if last message is a read */
1231 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001232 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001233 }
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (i2c_transfer(adapter, msg, num) < 0)
1236 return -1;
1237
Jean Delvare421ef472005-10-26 21:28:55 +02001238 /* Check PEC if last message is a read */
1239 if (i && (msg[num-1].flags & I2C_M_RD)) {
1240 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1241 return -1;
1242 }
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (read_write == I2C_SMBUS_READ)
1245 switch(size) {
1246 case I2C_SMBUS_BYTE:
1247 data->byte = msgbuf0[0];
1248 break;
1249 case I2C_SMBUS_BYTE_DATA:
1250 data->byte = msgbuf1[0];
1251 break;
David Brownell438d6c22006-12-10 21:21:31 +01001252 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 case I2C_SMBUS_PROC_CALL:
1254 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1255 break;
1256 case I2C_SMBUS_I2C_BLOCK_DATA:
1257 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001258 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1259 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 data->block[i+1] = msgbuf1[i];
1261 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001262 case I2C_SMBUS_BLOCK_DATA:
1263 case I2C_SMBUS_BLOCK_PROC_CALL:
1264 for (i = 0; i < msgbuf1[0] + 1; i++)
1265 data->block[i] = msgbuf1[i];
1266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268 return 0;
1269}
1270
1271
1272s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001273 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 union i2c_smbus_data * data)
1275{
1276 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001281 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1283 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001284 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 } else
1286 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1287 command,size,data);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return res;
1290}
1291
1292
Jean Delvareb31366f2007-05-01 23:26:28 +02001293/* Next three are needed by i2c-isa */
Jean Delvareefde7232005-07-20 23:03:50 +02001294EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
Jean Delvareefde7232005-07-20 23:03:50 +02001295EXPORT_SYMBOL_GPL(i2c_adapter_class);
1296EXPORT_SYMBOL_GPL(i2c_bus_type);
1297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298EXPORT_SYMBOL(i2c_add_adapter);
1299EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300EXPORT_SYMBOL(i2c_del_driver);
1301EXPORT_SYMBOL(i2c_attach_client);
1302EXPORT_SYMBOL(i2c_detach_client);
1303EXPORT_SYMBOL(i2c_use_client);
1304EXPORT_SYMBOL(i2c_release_client);
1305EXPORT_SYMBOL(i2c_clients_command);
1306EXPORT_SYMBOL(i2c_check_addr);
1307
1308EXPORT_SYMBOL(i2c_master_send);
1309EXPORT_SYMBOL(i2c_master_recv);
1310EXPORT_SYMBOL(i2c_control);
1311EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312EXPORT_SYMBOL(i2c_get_adapter);
1313EXPORT_SYMBOL(i2c_put_adapter);
1314EXPORT_SYMBOL(i2c_probe);
1315
1316EXPORT_SYMBOL(i2c_smbus_xfer);
1317EXPORT_SYMBOL(i2c_smbus_write_quick);
1318EXPORT_SYMBOL(i2c_smbus_read_byte);
1319EXPORT_SYMBOL(i2c_smbus_write_byte);
1320EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1321EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1322EXPORT_SYMBOL(i2c_smbus_read_word_data);
1323EXPORT_SYMBOL(i2c_smbus_write_word_data);
1324EXPORT_SYMBOL(i2c_smbus_write_block_data);
1325EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001326EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1329MODULE_DESCRIPTION("I2C-Bus main module");
1330MODULE_LICENSE("GPL");