blob: a2ad83ad0f5346474fe83cbd8daee5d963cfc72b [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 -070047static int i2c_device_match(struct device *dev, struct device_driver *drv)
48{
David Brownell7b4fbc52007-05-01 23:26:30 +020049 struct i2c_client *client = to_i2c_client(dev);
50 struct i2c_driver *driver = to_i2c_driver(drv);
51
52 /* make legacy i2c drivers bypass driver model probing entirely;
53 * such drivers scan each i2c adapter/bus themselves.
54 */
55 if (!driver->probe)
56 return 0;
57
58 /* new style drivers use the same kind of driver matching policy
59 * as platform devices or SPI: compare device and driver IDs.
60 */
61 return strcmp(client->driver_name, drv->name) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
David Brownell7b4fbc52007-05-01 23:26:30 +020064#ifdef CONFIG_HOTPLUG
65
66/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
67static int i2c_device_uevent(struct device *dev, char **envp, int num_envp,
68 char *buffer, int buffer_size)
69{
70 struct i2c_client *client = to_i2c_client(dev);
71 int i = 0, length = 0;
72
73 /* by definition, legacy drivers can't hotplug */
74 if (dev->driver || !client->driver_name)
75 return 0;
76
77 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &length,
78 "MODALIAS=%s", client->driver_name))
79 return -ENOMEM;
80 envp[i] = NULL;
81 dev_dbg(dev, "uevent\n");
82 return 0;
83}
84
85#else
86#define i2c_device_uevent NULL
87#endif /* CONFIG_HOTPLUG */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static int i2c_device_probe(struct device *dev)
90{
David Brownell7b4fbc52007-05-01 23:26:30 +020091 struct i2c_client *client = to_i2c_client(dev);
92 struct i2c_driver *driver = to_i2c_driver(dev->driver);
93
94 if (!driver->probe)
95 return -ENODEV;
96 client->driver = driver;
97 dev_dbg(dev, "probe\n");
98 return driver->probe(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static int i2c_device_remove(struct device *dev)
102{
103 return 0;
104}
105
David Brownellf37dd802007-02-13 22:09:00 +0100106static void i2c_device_shutdown(struct device *dev)
107{
108 struct i2c_driver *driver;
109
110 if (!dev->driver)
111 return;
112 driver = to_i2c_driver(dev->driver);
113 if (driver->shutdown)
114 driver->shutdown(to_i2c_client(dev));
115}
116
117static int i2c_device_suspend(struct device * dev, pm_message_t mesg)
118{
119 struct i2c_driver *driver;
120
121 if (!dev->driver)
122 return 0;
123 driver = to_i2c_driver(dev->driver);
124 if (!driver->suspend)
125 return 0;
126 return driver->suspend(to_i2c_client(dev), mesg);
127}
128
129static int i2c_device_resume(struct device * dev)
130{
131 struct i2c_driver *driver;
132
133 if (!dev->driver)
134 return 0;
135 driver = to_i2c_driver(dev->driver);
136 if (!driver->resume)
137 return 0;
138 return driver->resume(to_i2c_client(dev));
139}
140
David Brownell7b4fbc52007-05-01 23:26:30 +0200141static void i2c_client_release(struct device *dev)
142{
143 struct i2c_client *client = to_i2c_client(dev);
144 complete(&client->released);
145}
146
147static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
148{
149 struct i2c_client *client = to_i2c_client(dev);
150 return sprintf(buf, "%s\n", client->name);
151}
152
153static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
154{
155 struct i2c_client *client = to_i2c_client(dev);
156 return client->driver_name
157 ? sprintf(buf, "%s\n", client->driver_name)
158 : 0;
159}
160
161static struct device_attribute i2c_dev_attrs[] = {
162 __ATTR(name, S_IRUGO, show_client_name, NULL),
163 /* modalias helps coldplug: modprobe $(cat .../modalias) */
164 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
165 { },
166};
167
Russell Kingb864c7d2006-01-05 14:37:50 +0000168struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100169 .name = "i2c",
David Brownell7b4fbc52007-05-01 23:26:30 +0200170 .dev_attrs = i2c_dev_attrs,
David Brownellf37dd802007-02-13 22:09:00 +0100171 .match = i2c_device_match,
David Brownell7b4fbc52007-05-01 23:26:30 +0200172 .uevent = i2c_device_uevent,
David Brownellf37dd802007-02-13 22:09:00 +0100173 .probe = i2c_device_probe,
174 .remove = i2c_device_remove,
175 .shutdown = i2c_device_shutdown,
176 .suspend = i2c_device_suspend,
177 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000178};
179
David Brownellf37dd802007-02-13 22:09:00 +0100180/* ------------------------------------------------------------------------- */
181
David Brownell16ffadf2007-05-01 23:26:28 +0200182/* I2C bus adapters -- one roots each I2C or SMBUS segment */
183
Jean Delvareefde7232005-07-20 23:03:50 +0200184void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
David Brownellef2c8322007-05-01 23:26:28 +0200186 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 complete(&adap->dev_released);
188}
189
David Brownell16ffadf2007-05-01 23:26:28 +0200190static ssize_t
191show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
David Brownellef2c8322007-05-01 23:26:28 +0200193 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return sprintf(buf, "%s\n", adap->name);
195}
David Brownell16ffadf2007-05-01 23:26:28 +0200196
197static struct device_attribute i2c_adapter_attrs[] = {
198 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
199 { },
200};
201
202struct class i2c_adapter_class = {
203 .owner = THIS_MODULE,
204 .name = "i2c-adapter",
205 .dev_attrs = i2c_adapter_attrs,
206};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209/* -----
210 * i2c_add_adapter is called from within the algorithm layer,
211 * when a new hw adapter registers. A new device is register to be
212 * available for clients.
213 */
214int i2c_add_adapter(struct i2c_adapter *adap)
215{
216 int id, res = 0;
217 struct list_head *item;
218 struct i2c_driver *driver;
219
Arjan van de Venb3585e42006-01-11 10:50:26 +0100220 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) {
223 res = -ENOMEM;
224 goto out_unlock;
225 }
226
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400227 res = idr_get_new(&i2c_adapter_idr, adap, &id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (res < 0) {
229 if (res == -EAGAIN)
230 res = -ENOMEM;
231 goto out_unlock;
232 }
233
234 adap->nr = id & MAX_ID_MASK;
Ingo Molnar5c085d32006-01-18 23:16:04 +0100235 mutex_init(&adap->bus_lock);
236 mutex_init(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 list_add_tail(&adap->list,&adapters);
238 INIT_LIST_HEAD(&adap->clients);
239
240 /* Add the adapter to the driver core.
241 * If the parent pointer is not set up,
242 * we add this adapter to the host bus.
243 */
David Brownellb119dc32007-01-04 13:07:04 +0100244 if (adap->dev.parent == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 adap->dev.parent = &platform_bus;
Jean Delvarefe2c8d52007-02-13 22:09:04 +0100246 pr_debug("I2C adapter driver [%s] forgot to specify "
247 "physical device\n", adap->name);
David Brownellb119dc32007-01-04 13:07:04 +0100248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 sprintf(adap->dev.bus_id, "i2c-%d", adap->nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 adap->dev.release = &i2c_adapter_dev_release;
Jean Delvarefccb56e2007-05-01 23:26:27 +0200251 adap->dev.class = &i2c_adapter_class;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200252 res = device_register(&adap->dev);
253 if (res)
254 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200256 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
257
David Brownell7b4fbc52007-05-01 23:26:30 +0200258 /* let legacy drivers scan this bus for matching devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 list_for_each(item,&drivers) {
260 driver = list_entry(item, struct i2c_driver, list);
Jean Delvare8a994752005-11-26 20:28:06 +0100261 if (driver->attach_adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 /* We ignore the return code; if it fails, too bad */
263 driver->attach_adapter(adap);
264 }
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100267 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return res;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200269
Jean Delvareb119c6c2006-08-15 18:26:30 +0200270out_list:
271 list_del(&adap->list);
272 idr_remove(&i2c_adapter_idr, adap->nr);
273 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
276
277int i2c_del_adapter(struct i2c_adapter *adap)
278{
279 struct list_head *item, *_n;
280 struct i2c_adapter *adap_from_list;
281 struct i2c_driver *driver;
282 struct i2c_client *client;
283 int res = 0;
284
Arjan van de Venb3585e42006-01-11 10:50:26 +0100285 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 /* First make sure that this adapter was ever added */
288 list_for_each_entry(adap_from_list, &adapters, list) {
289 if (adap_from_list == adap)
290 break;
291 }
292 if (adap_from_list != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200293 pr_debug("i2c-core: attempting to delete unregistered "
294 "adapter [%s]\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 res = -EINVAL;
296 goto out_unlock;
297 }
298
299 list_for_each(item,&drivers) {
300 driver = list_entry(item, struct i2c_driver, list);
301 if (driver->detach_adapter)
302 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200303 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100304 "for driver [%s]\n",
305 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto out_unlock;
307 }
308 }
309
310 /* detach any active clients. This must be done first, because
Tobias Klausera551acc2005-05-19 21:40:38 +0200311 * it can fail; in which case we give up. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 list_for_each_safe(item, _n, &adap->clients) {
313 client = list_entry(item, struct i2c_client, list);
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if ((res=client->driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200316 dev_err(&adap->dev, "detach_client failed for client "
317 "[%s] at address 0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 client->addr);
319 goto out_unlock;
320 }
321 }
322
323 /* clean up the sysfs representation */
324 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 device_unregister(&adap->dev);
326 list_del(&adap->list);
327
328 /* wait for sysfs to drop all references */
329 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* free dynamically allocated bus id */
332 idr_remove(&i2c_adapter_idr, adap->nr);
333
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200334 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100337 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return res;
339}
340
341
David Brownell7b4fbc52007-05-01 23:26:30 +0200342/* ------------------------------------------------------------------------- */
343
344/*
345 * An i2c_driver is used with one or more i2c_client (device) nodes to access
346 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
347 * are two models for binding the driver to its device: "new style" drivers
348 * follow the standard Linux driver model and just respond to probe() calls
349 * issued if the driver core sees they match(); "legacy" drivers create device
350 * nodes themselves.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
352
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800353int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100355 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
David Brownell7b4fbc52007-05-01 23:26:30 +0200357 /* new style driver methods can't mix with legacy ones */
358 if (driver->probe) {
359 if (driver->attach_adapter || driver->detach_adapter
360 || driver->detach_client) {
361 printk(KERN_WARNING
362 "i2c-core: driver [%s] is confused\n",
363 driver->driver.name);
364 return -EINVAL;
365 }
366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800369 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 res = driver_register(&driver->driver);
373 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100374 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100375
Jean Delvare7eebcb72006-02-05 23:28:21 +0100376 mutex_lock(&core_lists);
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 list_add_tail(&driver->list,&drivers);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100379 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
David Brownell7b4fbc52007-05-01 23:26:30 +0200381 /* legacy drivers scan i2c busses directly */
Jean Delvare8a994752005-11-26 20:28:06 +0100382 if (driver->attach_adapter) {
David Brownell4ad4eac2007-05-01 23:26:28 +0200383 struct i2c_adapter *adapter;
384
385 list_for_each_entry(adapter, &adapters, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 driver->attach_adapter(adapter);
387 }
388 }
389
Arjan van de Venb3585e42006-01-11 10:50:26 +0100390 mutex_unlock(&core_lists);
Jean Delvare7eebcb72006-02-05 23:28:21 +0100391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800393EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395int i2c_del_driver(struct i2c_driver *driver)
396{
397 struct list_head *item1, *item2, *_n;
398 struct i2c_client *client;
399 struct i2c_adapter *adap;
David Brownell438d6c22006-12-10 21:21:31 +0100400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 int res = 0;
402
Arjan van de Venb3585e42006-01-11 10:50:26 +0100403 mutex_lock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* Have a look at each adapter, if clients of this driver are still
David Brownell438d6c22006-12-10 21:21:31 +0100406 * attached. If so, detach them to be able to kill the driver
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 * afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409 list_for_each(item1,&adapters) {
410 adap = list_entry(item1, struct i2c_adapter, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (driver->detach_adapter) {
412 if ((res = driver->detach_adapter(adap))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200413 dev_err(&adap->dev, "detach_adapter failed "
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100414 "for driver [%s]\n",
415 driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 goto out_unlock;
417 }
418 } else {
419 list_for_each_safe(item2, _n, &adap->clients) {
420 client = list_entry(item2, struct i2c_client, list);
421 if (client->driver != driver)
422 continue;
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200423 dev_dbg(&adap->dev, "detaching client [%s] "
424 "at 0x%02x\n", client->name,
425 client->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if ((res = driver->detach_client(client))) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200427 dev_err(&adap->dev, "detach_client "
428 "failed for client [%s] at "
429 "0x%02x\n", client->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 client->addr);
431 goto out_unlock;
432 }
433 }
434 }
435 }
436
437 driver_unregister(&driver->driver);
438 list_del(&driver->list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100439 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 out_unlock:
Arjan van de Venb3585e42006-01-11 10:50:26 +0100442 mutex_unlock(&core_lists);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
444}
445
David Brownell7b4fbc52007-05-01 23:26:30 +0200446/* ------------------------------------------------------------------------- */
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int __i2c_check_addr(struct i2c_adapter *adapter, unsigned int addr)
449{
450 struct list_head *item;
451 struct i2c_client *client;
452
453 list_for_each(item,&adapter->clients) {
454 client = list_entry(item, struct i2c_client, list);
455 if (client->addr == addr)
456 return -EBUSY;
457 }
458 return 0;
459}
460
461int i2c_check_addr(struct i2c_adapter *adapter, int addr)
462{
463 int rval;
464
Ingo Molnar5c085d32006-01-18 23:16:04 +0100465 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 rval = __i2c_check_addr(adapter, addr);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100467 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 return rval;
470}
471
472int i2c_attach_client(struct i2c_client *client)
473{
474 struct i2c_adapter *adapter = client->adapter;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200475 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Ingo Molnar5c085d32006-01-18 23:16:04 +0100477 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (__i2c_check_addr(client->adapter, client->addr)) {
Jean Delvareb119c6c2006-08-15 18:26:30 +0200479 res = -EBUSY;
480 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482 list_add_tail(&client->list,&adapter->clients);
David Brownell438d6c22006-12-10 21:21:31 +0100483
Jean Delvarecde78592005-11-26 21:00:54 +0100484 client->usage_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 client->dev.parent = &client->adapter->dev;
487 client->dev.driver = &client->driver->driver;
488 client->dev.bus = &i2c_bus_type;
489 client->dev.release = &i2c_client_release;
David Brownell438d6c22006-12-10 21:21:31 +0100490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
492 "%d-%04x", i2c_adapter_id(adapter), client->addr);
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200493 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
494 client->name, client->dev.bus_id);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200495 res = device_register(&client->dev);
496 if (res)
497 goto out_list;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200498 mutex_unlock(&adapter->clist_lock);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200499
500 if (adapter->client_register) {
501 if (adapter->client_register(client)) {
502 dev_dbg(&adapter->dev, "client_register "
503 "failed for client [%s] at 0x%02x\n",
504 client->name, client->addr);
505 }
506 }
507
508 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200509
Jean Delvareb119c6c2006-08-15 18:26:30 +0200510out_list:
511 list_del(&client->list);
512 dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x "
513 "(%d)\n", client->name, client->addr, res);
Jean Delvare77ed74d2006-09-30 17:18:59 +0200514out_unlock:
515 mutex_unlock(&adapter->clist_lock);
516 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519
520int i2c_detach_client(struct i2c_client *client)
521{
522 struct i2c_adapter *adapter = client->adapter;
523 int res = 0;
David Brownell438d6c22006-12-10 21:21:31 +0100524
Jean Delvarecde78592005-11-26 21:00:54 +0100525 if (client->usage_count > 0) {
Jean Delvare7bef5592005-07-27 22:14:49 +0200526 dev_warn(&client->dev, "Client [%s] still busy, "
527 "can't detach\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EBUSY;
Jean Delvare7bef5592005-07-27 22:14:49 +0200529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (adapter->client_unregister) {
532 res = adapter->client_unregister(client);
533 if (res) {
534 dev_err(&client->dev,
Jean Delvare86749e82005-07-29 12:15:29 -0700535 "client_unregister [%s] failed, "
536 "client not detached\n", client->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto out;
538 }
539 }
540
Ingo Molnar5c085d32006-01-18 23:16:04 +0100541 mutex_lock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 list_del(&client->list);
543 init_completion(&client->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 device_unregister(&client->dev);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100545 mutex_unlock(&adapter->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 wait_for_completion(&client->released);
547
548 out:
549 return res;
550}
551
552static int i2c_inc_use_client(struct i2c_client *client)
553{
554
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100555 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -ENODEV;
557 if (!try_module_get(client->adapter->owner)) {
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100558 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENODEV;
560 }
561
562 return 0;
563}
564
565static void i2c_dec_use_client(struct i2c_client *client)
566{
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100567 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 module_put(client->adapter->owner);
569}
570
571int i2c_use_client(struct i2c_client *client)
572{
573 int ret;
574
575 ret = i2c_inc_use_client(client);
576 if (ret)
577 return ret;
578
Jean Delvarecde78592005-11-26 21:00:54 +0100579 client->usage_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584int i2c_release_client(struct i2c_client *client)
585{
Jean Delvarecde78592005-11-26 21:00:54 +0100586 if (!client->usage_count) {
587 pr_debug("i2c-core: %s used one too many times\n",
588 __FUNCTION__);
589 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
David Brownell438d6c22006-12-10 21:21:31 +0100591
Jean Delvarecde78592005-11-26 21:00:54 +0100592 client->usage_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 i2c_dec_use_client(client);
David Brownell438d6c22006-12-10 21:21:31 +0100594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return 0;
596}
597
598void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
599{
600 struct list_head *item;
601 struct i2c_client *client;
602
Ingo Molnar5c085d32006-01-18 23:16:04 +0100603 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 list_for_each(item,&adap->clients) {
605 client = list_entry(item, struct i2c_client, list);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100606 if (!try_module_get(client->driver->driver.owner))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 continue;
608 if (NULL != client->driver->command) {
Ingo Molnar5c085d32006-01-18 23:16:04 +0100609 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 client->driver->command(client,cmd,arg);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100611 mutex_lock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100613 module_put(client->driver->driver.owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Ingo Molnar5c085d32006-01-18 23:16:04 +0100615 mutex_unlock(&adap->clist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
618static int __init i2c_init(void)
619{
620 int retval;
621
622 retval = bus_register(&i2c_bus_type);
623 if (retval)
624 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return class_register(&i2c_adapter_class);
626}
627
628static void __exit i2c_exit(void)
629{
630 class_unregister(&i2c_adapter_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 bus_unregister(&i2c_bus_type);
632}
633
634subsys_initcall(i2c_init);
635module_exit(i2c_exit);
636
637/* ----------------------------------------------------
638 * the functional interface to the i2c busses.
639 * ----------------------------------------------------
640 */
641
642int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num)
643{
644 int ret;
645
646 if (adap->algo->master_xfer) {
647#ifdef DEBUG
648 for (ret = 0; ret < num; ret++) {
649 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +0200650 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
651 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
652 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654#endif
655
Jiri Kosina6ea23032006-12-10 21:21:30 +0100656 mutex_lock_nested(&adap->bus_lock, adap->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ret = adap->algo->master_xfer(adap,msgs,num);
Ingo Molnar5c085d32006-01-18 23:16:04 +0100658 mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 return ret;
661 } else {
662 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
663 return -ENOSYS;
664 }
665}
666
667int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
668{
669 int ret;
670 struct i2c_adapter *adap=client->adapter;
671 struct i2c_msg msg;
672
Jean Delvare815f55f2005-05-07 22:58:46 +0200673 msg.addr = client->addr;
674 msg.flags = client->flags & I2C_M_TEN;
675 msg.len = count;
676 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +0100677
Jean Delvare815f55f2005-05-07 22:58:46 +0200678 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jean Delvare815f55f2005-05-07 22:58:46 +0200680 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
681 transmitted, else error code. */
682 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
686{
687 struct i2c_adapter *adap=client->adapter;
688 struct i2c_msg msg;
689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jean Delvare815f55f2005-05-07 22:58:46 +0200691 msg.addr = client->addr;
692 msg.flags = client->flags & I2C_M_TEN;
693 msg.flags |= I2C_M_RD;
694 msg.len = count;
695 msg.buf = buf;
696
697 ret = i2c_transfer(adap, &msg, 1);
698
699 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
700 transmitted, else error code. */
701 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704
705int i2c_control(struct i2c_client *client,
706 unsigned int cmd, unsigned long arg)
707{
708 int ret = 0;
709 struct i2c_adapter *adap = client->adapter;
710
711 dev_dbg(&client->adapter->dev, "i2c ioctl, cmd: 0x%x, arg: %#lx\n", cmd, arg);
712 switch (cmd) {
713 case I2C_RETRIES:
714 adap->retries = arg;
715 break;
716 case I2C_TIMEOUT:
717 adap->timeout = arg;
718 break;
719 default:
720 if (adap->algo->algo_control!=NULL)
721 ret = adap->algo->algo_control(adap,cmd,arg);
722 }
723 return ret;
724}
725
726/* ----------------------------------------------------
727 * the i2c address scanning function
728 * Will not work for 10-bit addresses!
729 * ----------------------------------------------------
730 */
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200731static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
732 int (*found_proc) (struct i2c_adapter *, int, int))
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200733{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200734 int err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200735
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200736 /* Make sure the address is valid */
737 if (addr < 0x03 || addr > 0x77) {
738 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
739 addr);
740 return -EINVAL;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200741 }
742
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200743 /* Skip if already in use */
744 if (i2c_check_addr(adapter, addr))
745 return 0;
746
747 /* Make sure there is something at this address, unless forced */
Jean Delvare4c9337d2005-08-09 20:28:10 +0200748 if (kind < 0) {
749 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
750 I2C_SMBUS_QUICK, NULL) < 0)
751 return 0;
752
753 /* prevent 24RF08 corruption */
754 if ((addr & ~0x0f) == 0x50)
755 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
756 I2C_SMBUS_QUICK, NULL);
757 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200758
759 /* Finally call the custom detection function */
760 err = found_proc(adapter, addr, kind);
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200761 /* -ENODEV can be returned if there is a chip at the given address
762 but it isn't supported by this chip driver. We catch it here as
763 this isn't an error. */
Jean Delvare114fd182006-09-03 22:25:04 +0200764 if (err == -ENODEV)
765 err = 0;
766
767 if (err)
768 dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n",
769 addr, err);
770 return err;
Jean Delvare9fc6adf2005-07-31 21:20:43 +0200771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773int i2c_probe(struct i2c_adapter *adapter,
774 struct i2c_client_address_data *address_data,
775 int (*found_proc) (struct i2c_adapter *, int, int))
776{
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200777 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int adap_id = i2c_adapter_id(adapter);
779
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200780 /* Force entries are done first, and are not affected by ignore
781 entries */
782 if (address_data->forces) {
783 unsigned short **forces = address_data->forces;
784 int kind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200786 for (kind = 0; forces[kind]; kind++) {
787 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
788 i += 2) {
789 if (forces[kind][i] == adap_id
790 || forces[kind][i] == ANY_I2C_BUS) {
791 dev_dbg(&adapter->dev, "found force "
792 "parameter for adapter %d, "
793 "addr 0x%02x, kind %d\n",
794 adap_id, forces[kind][i + 1],
795 kind);
796 err = i2c_probe_address(adapter,
797 forces[kind][i + 1],
798 kind, found_proc);
799 if (err)
800 return err;
801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200805
Jean Delvare4366dc92005-09-25 16:50:06 +0200806 /* Stop here if we can't use SMBUS_QUICK */
807 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
808 if (address_data->probe[0] == I2C_CLIENT_END
809 && address_data->normal_i2c[0] == I2C_CLIENT_END)
David Brownell438d6c22006-12-10 21:21:31 +0100810 return 0;
Jean Delvare4366dc92005-09-25 16:50:06 +0200811
812 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
813 "can't probe for chips\n");
814 return -1;
815 }
816
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200817 /* Probe entries are done second, and are not affected by ignore
818 entries either */
819 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
820 if (address_data->probe[i] == adap_id
821 || address_data->probe[i] == ANY_I2C_BUS) {
822 dev_dbg(&adapter->dev, "found probe parameter for "
823 "adapter %d, addr 0x%02x\n", adap_id,
824 address_data->probe[i + 1]);
825 err = i2c_probe_address(adapter,
826 address_data->probe[i + 1],
827 -1, found_proc);
828 if (err)
829 return err;
830 }
831 }
832
833 /* Normal entries are done last, unless shadowed by an ignore entry */
834 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
835 int j, ignore;
836
837 ignore = 0;
838 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
839 j += 2) {
840 if ((address_data->ignore[j] == adap_id ||
841 address_data->ignore[j] == ANY_I2C_BUS)
842 && address_data->ignore[j + 1]
843 == address_data->normal_i2c[i]) {
844 dev_dbg(&adapter->dev, "found ignore "
845 "parameter for adapter %d, "
846 "addr 0x%02x\n", adap_id,
847 address_data->ignore[j + 1]);
Mark M. Hoffman2369df92006-07-01 17:01:59 +0200848 ignore = 1;
849 break;
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200850 }
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200851 }
852 if (ignore)
853 continue;
854
855 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
856 "addr 0x%02x\n", adap_id,
857 address_data->normal_i2c[i]);
858 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
859 -1, found_proc);
860 if (err)
861 return err;
862 }
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867struct i2c_adapter* i2c_get_adapter(int id)
868{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +0100870
Arjan van de Venb3585e42006-01-11 10:50:26 +0100871 mutex_lock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400872 adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id);
873 if (adapter && !try_module_get(adapter->owner))
874 adapter = NULL;
875
Arjan van de Venb3585e42006-01-11 10:50:26 +0100876 mutex_unlock(&core_lists);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -0400877 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880void i2c_put_adapter(struct i2c_adapter *adap)
881{
882 module_put(adap->owner);
883}
884
885/* The SMBus parts */
886
David Brownell438d6c22006-12-10 21:21:31 +0100887#define POLY (0x1070U << 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888static u8
889crc8(u16 data)
890{
891 int i;
David Brownell438d6c22006-12-10 21:21:31 +0100892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +0100894 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 data = data ^ POLY;
896 data = data << 1;
897 }
898 return (u8)(data >> 8);
899}
900
Jean Delvare421ef472005-10-26 21:28:55 +0200901/* Incremental CRC8 over count bytes in the array pointed to by p */
902static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 int i;
905
906 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +0200907 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return crc;
909}
910
Jean Delvare421ef472005-10-26 21:28:55 +0200911/* Assume a 7-bit address, which is reasonable for SMBus */
912static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Jean Delvare421ef472005-10-26 21:28:55 +0200914 /* The address will be sent first */
915 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
916 pec = i2c_smbus_pec(pec, &addr, 1);
917
918 /* The data buffer follows */
919 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Jean Delvare421ef472005-10-26 21:28:55 +0200922/* Used for write only transactions */
923static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924{
Jean Delvare421ef472005-10-26 21:28:55 +0200925 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
926 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Jean Delvare421ef472005-10-26 21:28:55 +0200929/* Return <0 on CRC error
930 If there was a write before this read (most cases) we need to take the
931 partial CRC from the write part into account.
932 Note that this function does modify the message (we need to decrease the
933 message length to hide the CRC byte from the caller). */
934static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Jean Delvare421ef472005-10-26 21:28:55 +0200936 u8 rpec = msg->buf[--msg->len];
937 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (rpec != cpec) {
940 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
941 rpec, cpec);
942 return -1;
943 }
David Brownell438d6c22006-12-10 21:21:31 +0100944 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value)
948{
949 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
David Brownell438d6c22006-12-10 21:21:31 +0100950 value,0,I2C_SMBUS_QUICK,NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
953s32 i2c_smbus_read_byte(struct i2c_client *client)
954{
955 union i2c_smbus_data data;
956 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
957 I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data))
958 return -1;
959 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200960 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +0200966 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
968
969s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
970{
971 union i2c_smbus_data data;
972 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
973 I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data))
974 return -1;
975 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200976 return data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
980{
981 union i2c_smbus_data data;
982 data.byte = value;
983 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
984 I2C_SMBUS_WRITE,command,
985 I2C_SMBUS_BYTE_DATA,&data);
986}
987
988s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
989{
990 union i2c_smbus_data data;
991 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
992 I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data))
993 return -1;
994 else
Jean Delvare7eff82c2006-09-03 22:24:00 +0200995 return data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
999{
1000 union i2c_smbus_data data;
1001 data.word = value;
1002 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1003 I2C_SMBUS_WRITE,command,
1004 I2C_SMBUS_WORD_DATA,&data);
1005}
1006
1007s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001008 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (length > I2C_SMBUS_BLOCK_MAX)
1013 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001015 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1017 I2C_SMBUS_WRITE,command,
1018 I2C_SMBUS_BLOCK_DATA,&data);
1019}
1020
1021/* Returns the number of read bytes */
1022s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, u8 *values)
1023{
1024 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1027 I2C_SMBUS_READ,command,
1028 I2C_SMBUS_I2C_BLOCK_DATA,&data))
1029 return -1;
Jean Delvare76560322006-01-18 23:14:55 +01001030
1031 memcpy(values, &data.block[1], data.block[0]);
1032 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Jean Delvare21bbd692006-01-09 15:19:18 +11001035s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001036 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001037{
1038 union i2c_smbus_data data;
1039
1040 if (length > I2C_SMBUS_BLOCK_MAX)
1041 length = I2C_SMBUS_BLOCK_MAX;
1042 data.block[0] = length;
1043 memcpy(data.block + 1, values, length);
1044 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1045 I2C_SMBUS_WRITE, command,
1046 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1047}
1048
David Brownell438d6c22006-12-10 21:21:31 +01001049/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001051static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001053 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 union i2c_smbus_data * data)
1055{
1056 /* So we need to generate a series of msgs. In the case of writing, we
1057 need to use only one message; when reading, we need two. We initialize
1058 most things with sane defaults, to keep the code below somewhat
1059 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001060 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1061 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001063 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1065 };
1066 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001067 u8 partial_pec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 msgbuf0[0] = command;
1070 switch(size) {
1071 case I2C_SMBUS_QUICK:
1072 msg[0].len = 0;
1073 /* Special case: The read/write field is used as data */
1074 msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0;
1075 num = 1;
1076 break;
1077 case I2C_SMBUS_BYTE:
1078 if (read_write == I2C_SMBUS_READ) {
1079 /* Special case: only a read! */
1080 msg[0].flags = I2C_M_RD | flags;
1081 num = 1;
1082 }
1083 break;
1084 case I2C_SMBUS_BYTE_DATA:
1085 if (read_write == I2C_SMBUS_READ)
1086 msg[1].len = 1;
1087 else {
1088 msg[0].len = 2;
1089 msgbuf0[1] = data->byte;
1090 }
1091 break;
1092 case I2C_SMBUS_WORD_DATA:
1093 if (read_write == I2C_SMBUS_READ)
1094 msg[1].len = 2;
1095 else {
1096 msg[0].len=3;
1097 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001098 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 break;
1101 case I2C_SMBUS_PROC_CALL:
1102 num = 2; /* Special case */
1103 read_write = I2C_SMBUS_READ;
1104 msg[0].len = 3;
1105 msg[1].len = 2;
1106 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001107 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 break;
1109 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001111 msg[1].flags |= I2C_M_RECV_LEN;
1112 msg[1].len = 1; /* block length will be added by
1113 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 } else {
1115 msg[0].len = data->block[0] + 2;
1116 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1117 dev_err(&adapter->dev, "smbus_access called with "
1118 "invalid block write size (%d)\n",
1119 data->block[0]);
1120 return -1;
1121 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001122 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 msgbuf0[i] = data->block[i-1];
1124 }
1125 break;
1126 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001127 num = 2; /* Another special case */
1128 read_write = I2C_SMBUS_READ;
1129 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1130 dev_err(&adapter->dev, "%s called with invalid "
1131 "block proc call size (%d)\n", __FUNCTION__,
1132 data->block[0]);
1133 return -1;
1134 }
1135 msg[0].len = data->block[0] + 2;
1136 for (i = 1; i < msg[0].len; i++)
1137 msgbuf0[i] = data->block[i-1];
1138 msg[1].flags |= I2C_M_RECV_LEN;
1139 msg[1].len = 1; /* block length will be added by
1140 the underlying bus driver */
1141 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 case I2C_SMBUS_I2C_BLOCK_DATA:
1143 if (read_write == I2C_SMBUS_READ) {
Jean Delvare30dac742005-10-08 00:15:59 +02001144 msg[1].len = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
1146 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001147 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with "
1149 "invalid block write size (%d)\n",
1150 data->block[0]);
1151 return -1;
1152 }
1153 for (i = 1; i <= data->block[0]; i++)
1154 msgbuf0[i] = data->block[i];
1155 }
1156 break;
1157 default:
1158 dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n",
1159 size);
1160 return -1;
1161 }
1162
Jean Delvare421ef472005-10-26 21:28:55 +02001163 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1164 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1165 if (i) {
1166 /* Compute PEC if first message is a write */
1167 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001168 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001169 i2c_smbus_add_pec(&msg[0]);
1170 else /* Write followed by read */
1171 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1172 }
1173 /* Ask for PEC if last message is a read */
1174 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001175 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001176 }
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (i2c_transfer(adapter, msg, num) < 0)
1179 return -1;
1180
Jean Delvare421ef472005-10-26 21:28:55 +02001181 /* Check PEC if last message is a read */
1182 if (i && (msg[num-1].flags & I2C_M_RD)) {
1183 if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0)
1184 return -1;
1185 }
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (read_write == I2C_SMBUS_READ)
1188 switch(size) {
1189 case I2C_SMBUS_BYTE:
1190 data->byte = msgbuf0[0];
1191 break;
1192 case I2C_SMBUS_BYTE_DATA:
1193 data->byte = msgbuf1[0];
1194 break;
David Brownell438d6c22006-12-10 21:21:31 +01001195 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case I2C_SMBUS_PROC_CALL:
1197 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1198 break;
1199 case I2C_SMBUS_I2C_BLOCK_DATA:
1200 /* fixed at 32 for now */
Jean Delvare30dac742005-10-08 00:15:59 +02001201 data->block[0] = I2C_SMBUS_BLOCK_MAX;
1202 for (i = 0; i < I2C_SMBUS_BLOCK_MAX; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 data->block[i+1] = msgbuf1[i];
1204 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001205 case I2C_SMBUS_BLOCK_DATA:
1206 case I2C_SMBUS_BLOCK_PROC_CALL:
1207 for (i = 0; i < msgbuf1[0] + 1; i++)
1208 data->block[i] = msgbuf1[i];
1209 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211 return 0;
1212}
1213
1214
1215s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001216 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 union i2c_smbus_data * data)
1218{
1219 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (adapter->algo->smbus_xfer) {
Ingo Molnar5c085d32006-01-18 23:16:04 +01001224 mutex_lock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write,
1226 command,size,data);
Ingo Molnar5c085d32006-01-18 23:16:04 +01001227 mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 } else
1229 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1230 command,size,data);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return res;
1233}
1234
1235
Jean Delvareb31366f2007-05-01 23:26:28 +02001236/* Next three are needed by i2c-isa */
Jean Delvareefde7232005-07-20 23:03:50 +02001237EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
Jean Delvareefde7232005-07-20 23:03:50 +02001238EXPORT_SYMBOL_GPL(i2c_adapter_class);
1239EXPORT_SYMBOL_GPL(i2c_bus_type);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241EXPORT_SYMBOL(i2c_add_adapter);
1242EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243EXPORT_SYMBOL(i2c_del_driver);
1244EXPORT_SYMBOL(i2c_attach_client);
1245EXPORT_SYMBOL(i2c_detach_client);
1246EXPORT_SYMBOL(i2c_use_client);
1247EXPORT_SYMBOL(i2c_release_client);
1248EXPORT_SYMBOL(i2c_clients_command);
1249EXPORT_SYMBOL(i2c_check_addr);
1250
1251EXPORT_SYMBOL(i2c_master_send);
1252EXPORT_SYMBOL(i2c_master_recv);
1253EXPORT_SYMBOL(i2c_control);
1254EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255EXPORT_SYMBOL(i2c_get_adapter);
1256EXPORT_SYMBOL(i2c_put_adapter);
1257EXPORT_SYMBOL(i2c_probe);
1258
1259EXPORT_SYMBOL(i2c_smbus_xfer);
1260EXPORT_SYMBOL(i2c_smbus_write_quick);
1261EXPORT_SYMBOL(i2c_smbus_read_byte);
1262EXPORT_SYMBOL(i2c_smbus_write_byte);
1263EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1264EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1265EXPORT_SYMBOL(i2c_smbus_read_word_data);
1266EXPORT_SYMBOL(i2c_smbus_write_word_data);
1267EXPORT_SYMBOL(i2c_smbus_write_block_data);
1268EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001269EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1272MODULE_DESCRIPTION("I2C-Bus main module");
1273MODULE_LICENSE("GPL");