blob: 37df0f14c38c87a45c5848a2e0f3e7956f263d5a [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
Jan Engelhardt96de0e22007-10-19 23:21:04 +020020/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 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>
Arjan van de Venb3585e42006-01-11 10:50:26 +010032#include <linux/mutex.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010033#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010034#include <linux/hardirq.h>
35#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020036#include <linux/rwsem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <asm/uaccess.h>
38
David Brownell9c1600e2007-05-01 23:26:31 +020039#include "i2c-core.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Jean Delvare99cd8e22009-06-19 16:58:20 +020042/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020043 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010045static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020047static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Jean Delvare4f8cf822009-09-18 22:45:46 +020049static struct device_type i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +020050static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020051static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010052
53/* ------------------------------------------------------------------------- */
54
Jean Delvared2653e92008-04-29 23:11:39 +020055static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
57{
58 while (id->name[0]) {
59 if (strcmp(client->name, id->name) == 0)
60 return id;
61 id++;
62 }
63 return NULL;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static int i2c_device_match(struct device *dev, struct device_driver *drv)
67{
Jean Delvare51298d12009-09-18 22:45:45 +020068 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020070
Jean Delvare51298d12009-09-18 22:45:45 +020071 if (!client)
72 return 0;
73
74 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020075 /* match on an id table if there is one */
76 if (driver->id_table)
77 return i2c_match_id(driver->id_table, client) != NULL;
78
Jean Delvareeb8a7902008-05-18 20:49:41 +020079 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
David Brownell7b4fbc52007-05-01 23:26:30 +020082#ifdef CONFIG_HOTPLUG
83
84/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020085static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020086{
87 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020088
Jean Delvareeb8a7902008-05-18 20:49:41 +020089 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
91 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020092 dev_dbg(dev, "uevent\n");
93 return 0;
94}
95
96#else
97#define i2c_device_uevent NULL
98#endif /* CONFIG_HOTPLUG */
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int i2c_device_probe(struct device *dev)
101{
Jean Delvare51298d12009-09-18 22:45:45 +0200102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100104 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200105
Jean Delvare51298d12009-09-18 22:45:45 +0200106 if (!client)
107 return 0;
108
109 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200110 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200111 return -ENODEV;
112 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200116 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200117
Jean Delvaree0457442008-07-14 22:38:30 +0200118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100119 if (status)
120 client->driver = NULL;
121 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124static int i2c_device_remove(struct device *dev)
125{
Jean Delvare51298d12009-09-18 22:45:45 +0200126 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200127 struct i2c_driver *driver;
128 int status;
129
Jean Delvare51298d12009-09-18 22:45:45 +0200130 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200131 return 0;
132
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
137 } else {
138 dev->driver = NULL;
139 status = 0;
140 }
141 if (status == 0)
142 client->driver = NULL;
143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
David Brownellf37dd802007-02-13 22:09:00 +0100146static void i2c_device_shutdown(struct device *dev)
147{
Jean Delvare51298d12009-09-18 22:45:45 +0200148 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100149 struct i2c_driver *driver;
150
Jean Delvare51298d12009-09-18 22:45:45 +0200151 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100152 return;
153 driver = to_i2c_driver(dev->driver);
154 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200155 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100156}
157
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100158static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100159{
Jean Delvare51298d12009-09-18 22:45:45 +0200160 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100161 struct i2c_driver *driver;
162
Jean Delvare51298d12009-09-18 22:45:45 +0200163 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100164 return 0;
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->suspend)
167 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200168 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100169}
170
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100171static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100172{
Jean Delvare51298d12009-09-18 22:45:45 +0200173 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100174 struct i2c_driver *driver;
175
Jean Delvare51298d12009-09-18 22:45:45 +0200176 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100177 return 0;
178 driver = to_i2c_driver(dev->driver);
179 if (!driver->resume)
180 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200181 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100182}
183
David Brownell9c1600e2007-05-01 23:26:31 +0200184static void i2c_client_dev_release(struct device *dev)
185{
186 kfree(to_i2c_client(dev));
187}
188
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100189static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200190show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200191{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200192 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
193 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200194}
195
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100196static ssize_t
197show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200198{
199 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200201}
202
Jean Delvare4f8cf822009-09-18 22:45:46 +0200203static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200204static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
205
206static struct attribute *i2c_dev_attrs[] = {
207 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200208 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200209 &dev_attr_modalias.attr,
210 NULL
211};
212
213static struct attribute_group i2c_dev_attr_group = {
214 .attrs = i2c_dev_attrs,
215};
216
217static const struct attribute_group *i2c_dev_attr_groups[] = {
218 &i2c_dev_attr_group,
219 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200220};
221
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200222struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100223 .name = "i2c",
224 .match = i2c_device_match,
225 .probe = i2c_device_probe,
226 .remove = i2c_device_remove,
227 .shutdown = i2c_device_shutdown,
228 .suspend = i2c_device_suspend,
229 .resume = i2c_device_resume,
Russell Kingb864c7d2006-01-05 14:37:50 +0000230};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200231EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000232
Jean Delvare51298d12009-09-18 22:45:45 +0200233static struct device_type i2c_client_type = {
234 .groups = i2c_dev_attr_groups,
235 .uevent = i2c_device_uevent,
236 .release = i2c_client_dev_release,
237};
238
David Brownell9b766b82008-01-27 18:14:51 +0100239
240/**
241 * i2c_verify_client - return parameter as i2c_client, or NULL
242 * @dev: device, probably from some driver model iterator
243 *
244 * When traversing the driver model tree, perhaps using driver model
245 * iterators like @device_for_each_child(), you can't assume very much
246 * about the nodes you find. Use this function to avoid oopses caused
247 * by wrongly treating some non-I2C device as an i2c_client.
248 */
249struct i2c_client *i2c_verify_client(struct device *dev)
250{
Jean Delvare51298d12009-09-18 22:45:45 +0200251 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100252 ? to_i2c_client(dev)
253 : NULL;
254}
255EXPORT_SYMBOL(i2c_verify_client);
256
257
David Brownell9c1600e2007-05-01 23:26:31 +0200258/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200259 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200260 * @adap: the adapter managing the device
261 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200262 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200263 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200264 * Create an i2c device. Binding is handled through driver model
265 * probe()/remove() methods. A driver may be bound to this device when we
266 * return from this function, or any later moment (e.g. maybe hotplugging will
267 * load the driver module). This call is not appropriate for use by mainboard
268 * initialization logic, which usually runs during an arch_initcall() long
269 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200270 *
271 * This returns the new i2c client, which may be saved for later use with
272 * i2c_unregister_device(); or NULL to indicate an error.
273 */
274struct i2c_client *
275i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
276{
277 struct i2c_client *client;
278 int status;
279
280 client = kzalloc(sizeof *client, GFP_KERNEL);
281 if (!client)
282 return NULL;
283
284 client->adapter = adap;
285
286 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200287
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200288 if (info->archdata)
289 client->dev.archdata = *info->archdata;
290
Marc Pignatee354252008-08-28 08:33:22 +0200291 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200292 client->addr = info->addr;
293 client->irq = info->irq;
294
David Brownell9c1600e2007-05-01 23:26:31 +0200295 strlcpy(client->name, info->type, sizeof(client->name));
296
Jean Delvaref8a227e2009-06-19 16:58:18 +0200297 /* Check for address business */
298 status = i2c_check_addr(adap, client->addr);
299 if (status)
300 goto out_err;
301
302 client->dev.parent = &client->adapter->dev;
303 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200304 client->dev.type = &i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200305
306 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
307 client->addr);
308 status = device_register(&client->dev);
309 if (status)
310 goto out_err;
311
Jean Delvaref8a227e2009-06-19 16:58:18 +0200312 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
313 client->name, dev_name(&client->dev));
314
David Brownell9c1600e2007-05-01 23:26:31 +0200315 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200316
317out_err:
318 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
319 "(%d)\n", client->name, client->addr, status);
320 kfree(client);
321 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200322}
323EXPORT_SYMBOL_GPL(i2c_new_device);
324
325
326/**
327 * i2c_unregister_device - reverse effect of i2c_new_device()
328 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200329 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200330 */
331void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200332{
David Brownella1d9e6e2007-05-01 23:26:30 +0200333 device_unregister(&client->dev);
334}
David Brownell9c1600e2007-05-01 23:26:31 +0200335EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200336
337
Jean Delvare60b129d2008-05-11 20:37:06 +0200338static const struct i2c_device_id dummy_id[] = {
339 { "dummy", 0 },
340 { },
341};
342
Jean Delvared2653e92008-04-29 23:11:39 +0200343static int dummy_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
345{
346 return 0;
347}
348
349static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100350{
351 return 0;
352}
353
354static struct i2c_driver dummy_driver = {
355 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200356 .probe = dummy_probe,
357 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200358 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100359};
360
361/**
362 * i2c_new_dummy - return a new i2c device bound to a dummy driver
363 * @adapter: the adapter managing the device
364 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100365 * Context: can sleep
366 *
367 * This returns an I2C client bound to the "dummy" driver, intended for use
368 * with devices that consume multiple addresses. Examples of such chips
369 * include various EEPROMS (like 24c04 and 24c08 models).
370 *
371 * These dummy devices have two main uses. First, most I2C and SMBus calls
372 * except i2c_transfer() need a client handle; the dummy will be that handle.
373 * And second, this prevents the specified address from being bound to a
374 * different driver.
375 *
376 * This returns the new i2c client, which should be saved for later use with
377 * i2c_unregister_device(); or NULL to indicate an error.
378 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100379struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100380{
381 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200382 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100383 };
384
David Brownelle9f13732008-01-27 18:14:52 +0100385 return i2c_new_device(adapter, &info);
386}
387EXPORT_SYMBOL_GPL(i2c_new_dummy);
388
David Brownellf37dd802007-02-13 22:09:00 +0100389/* ------------------------------------------------------------------------- */
390
David Brownell16ffadf2007-05-01 23:26:28 +0200391/* I2C bus adapters -- one roots each I2C or SMBUS segment */
392
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200393static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
David Brownellef2c83212007-05-01 23:26:28 +0200395 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 complete(&adap->dev_released);
397}
398
Jean Delvare99cd8e22009-06-19 16:58:20 +0200399/*
400 * Let users instantiate I2C devices through sysfs. This can be used when
401 * platform initialization code doesn't contain the proper data for
402 * whatever reason. Also useful for drivers that do device detection and
403 * detection fails, either because the device uses an unexpected address,
404 * or this is a compatible device with different ID register values.
405 *
406 * Parameter checking may look overzealous, but we really don't want
407 * the user to provide incorrect parameters.
408 */
409static ssize_t
410i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
412{
413 struct i2c_adapter *adap = to_i2c_adapter(dev);
414 struct i2c_board_info info;
415 struct i2c_client *client;
416 char *blank, end;
417 int res;
418
419 dev_warn(dev, "The new_device interface is still experimental "
420 "and may change in a near future\n");
421 memset(&info, 0, sizeof(struct i2c_board_info));
422
423 blank = strchr(buf, ' ');
424 if (!blank) {
425 dev_err(dev, "%s: Missing parameters\n", "new_device");
426 return -EINVAL;
427 }
428 if (blank - buf > I2C_NAME_SIZE - 1) {
429 dev_err(dev, "%s: Invalid device name\n", "new_device");
430 return -EINVAL;
431 }
432 memcpy(info.type, buf, blank - buf);
433
434 /* Parse remaining parameters, reject extra parameters */
435 res = sscanf(++blank, "%hi%c", &info.addr, &end);
436 if (res < 1) {
437 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
438 return -EINVAL;
439 }
440 if (res > 1 && end != '\n') {
441 dev_err(dev, "%s: Extra parameters\n", "new_device");
442 return -EINVAL;
443 }
444
445 if (info.addr < 0x03 || info.addr > 0x77) {
446 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
447 info.addr);
448 return -EINVAL;
449 }
450
451 client = i2c_new_device(adap, &info);
452 if (!client)
453 return -EEXIST;
454
455 /* Keep track of the added device */
456 mutex_lock(&core_lock);
457 list_add_tail(&client->detected, &userspace_devices);
458 mutex_unlock(&core_lock);
459 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
460 info.type, info.addr);
461
462 return count;
463}
464
465/*
466 * And of course let the users delete the devices they instantiated, if
467 * they got it wrong. This interface can only be used to delete devices
468 * instantiated by i2c_sysfs_new_device above. This guarantees that we
469 * don't delete devices to which some kernel code still has references.
470 *
471 * Parameter checking may look overzealous, but we really don't want
472 * the user to delete the wrong device.
473 */
474static ssize_t
475i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
477{
478 struct i2c_adapter *adap = to_i2c_adapter(dev);
479 struct i2c_client *client, *next;
480 unsigned short addr;
481 char end;
482 int res;
483
484 /* Parse parameters, reject extra parameters */
485 res = sscanf(buf, "%hi%c", &addr, &end);
486 if (res < 1) {
487 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
488 return -EINVAL;
489 }
490 if (res > 1 && end != '\n') {
491 dev_err(dev, "%s: Extra parameters\n", "delete_device");
492 return -EINVAL;
493 }
494
495 /* Make sure the device was added through sysfs */
496 res = -ENOENT;
497 mutex_lock(&core_lock);
498 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
499 if (client->addr == addr && client->adapter == adap) {
500 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
501 "delete_device", client->name, client->addr);
502
503 list_del(&client->detected);
504 i2c_unregister_device(client);
505 res = count;
506 break;
507 }
508 }
509 mutex_unlock(&core_lock);
510
511 if (res < 0)
512 dev_err(dev, "%s: Can't find device in list\n",
513 "delete_device");
514 return res;
515}
516
Jean Delvare4f8cf822009-09-18 22:45:46 +0200517static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
518static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
519
520static struct attribute *i2c_adapter_attrs[] = {
521 &dev_attr_name.attr,
522 &dev_attr_new_device.attr,
523 &dev_attr_delete_device.attr,
524 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200525};
526
Jean Delvare4f8cf822009-09-18 22:45:46 +0200527static struct attribute_group i2c_adapter_attr_group = {
528 .attrs = i2c_adapter_attrs,
529};
530
531static const struct attribute_group *i2c_adapter_attr_groups[] = {
532 &i2c_adapter_attr_group,
533 NULL
534};
535
536static struct device_type i2c_adapter_type = {
537 .groups = i2c_adapter_attr_groups,
538 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200539};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jean Delvare2bb50952009-09-18 22:45:46 +0200541#ifdef CONFIG_I2C_COMPAT
542static struct class_compat *i2c_adapter_compat_class;
543#endif
544
David Brownell9c1600e2007-05-01 23:26:31 +0200545static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
546{
547 struct i2c_devinfo *devinfo;
548
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200549 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200550 list_for_each_entry(devinfo, &__i2c_board_list, list) {
551 if (devinfo->busnum == adapter->nr
552 && !i2c_new_device(adapter,
553 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100554 dev_err(&adapter->dev,
555 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200556 devinfo->board_info.addr);
557 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200558 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200559}
560
Jean Delvare026526f2008-01-27 18:14:49 +0100561static int i2c_do_add_adapter(struct device_driver *d, void *data)
562{
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adap = data;
565
Jean Delvare4735c982008-07-14 22:38:36 +0200566 /* Detect supported devices on that bus, and instantiate them */
567 i2c_detect(adap, driver);
568
569 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100570 if (driver->attach_adapter) {
571 /* We ignore the return code; if it fails, too bad */
572 driver->attach_adapter(adap);
573 }
574 return 0;
575}
576
David Brownell6e13e642007-05-01 23:26:31 +0200577static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvare026526f2008-01-27 18:14:49 +0100579 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Brownell1d0b19c2008-10-14 17:30:05 +0200581 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200582 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
583 res = -EAGAIN;
584 goto out_list;
585 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200586
Mika Kuoppala194684e2009-12-06 17:06:22 +0100587 rt_mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Jean Delvare8fcfef62009-03-28 21:34:43 +0100589 /* Set default timeout to 1 second if not already set */
590 if (adap->timeout == 0)
591 adap->timeout = HZ;
592
Kay Sievers27d9c182009-01-07 14:29:16 +0100593 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200594 adap->dev.bus = &i2c_bus_type;
595 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200596 res = device_register(&adap->dev);
597 if (res)
598 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200600 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
601
Jean Delvare2bb50952009-09-18 22:45:46 +0200602#ifdef CONFIG_I2C_COMPAT
603 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
604 adap->dev.parent);
605 if (res)
606 dev_warn(&adap->dev,
607 "Failed to create compatibility class link\n");
608#endif
609
Jean Delvare729d6dd2009-06-19 16:58:18 +0200610 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200611 if (adap->nr < __i2c_first_dynamic_bus_num)
612 i2c_scan_static_board_info(adap);
613
Jean Delvare4735c982008-07-14 22:38:36 +0200614 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200615 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100616 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 i2c_do_add_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100618 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200619
620 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200621
Jean Delvareb119c6c2006-08-15 18:26:30 +0200622out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200623 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200624 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200625 mutex_unlock(&core_lock);
626 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
David Brownell6e13e642007-05-01 23:26:31 +0200629/**
630 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
631 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200632 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200633 *
634 * This routine is used to declare an I2C adapter when its bus number
635 * doesn't matter. Examples: for I2C adapters dynamically added by
636 * USB links or PCI plugin cards.
637 *
638 * When this returns zero, a new bus number was allocated and stored
639 * in adap->nr, and the specified adapter became available for clients.
640 * Otherwise, a negative errno value is returned.
641 */
642int i2c_add_adapter(struct i2c_adapter *adapter)
643{
644 int id, res = 0;
645
646retry:
647 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
648 return -ENOMEM;
649
Jean Delvarecaada322008-01-27 18:14:49 +0100650 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200651 /* "above" here means "above or equal to", sigh */
652 res = idr_get_new_above(&i2c_adapter_idr, adapter,
653 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100654 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200655
656 if (res < 0) {
657 if (res == -EAGAIN)
658 goto retry;
659 return res;
660 }
661
662 adapter->nr = id;
663 return i2c_register_adapter(adapter);
664}
665EXPORT_SYMBOL(i2c_add_adapter);
666
667/**
668 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
669 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200670 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200671 *
672 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100673 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
674 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200675 * is used to properly configure I2C devices.
676 *
677 * If no devices have pre-been declared for this bus, then be sure to
678 * register the adapter before any dynamically allocated ones. Otherwise
679 * the required bus ID may not be available.
680 *
681 * When this returns zero, the specified adapter became available for
682 * clients using the bus number provided in adap->nr. Also, the table
683 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
684 * and the appropriate driver model device nodes are created. Otherwise, a
685 * negative errno value is returned.
686 */
687int i2c_add_numbered_adapter(struct i2c_adapter *adap)
688{
689 int id;
690 int status;
691
692 if (adap->nr & ~MAX_ID_MASK)
693 return -EINVAL;
694
695retry:
696 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
697 return -ENOMEM;
698
Jean Delvarecaada322008-01-27 18:14:49 +0100699 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200700 /* "above" here means "above or equal to", sigh;
701 * we need the "equal to" result to force the result
702 */
703 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
704 if (status == 0 && id != adap->nr) {
705 status = -EBUSY;
706 idr_remove(&i2c_adapter_idr, id);
707 }
Jean Delvarecaada322008-01-27 18:14:49 +0100708 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200709 if (status == -EAGAIN)
710 goto retry;
711
712 if (status == 0)
713 status = i2c_register_adapter(adap);
714 return status;
715}
716EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
717
Jean Delvare026526f2008-01-27 18:14:49 +0100718static int i2c_do_del_adapter(struct device_driver *d, void *data)
719{
720 struct i2c_driver *driver = to_i2c_driver(d);
721 struct i2c_adapter *adapter = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200722 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100723 int res;
724
Jean Delvareacec2112009-03-28 21:34:40 +0100725 /* Remove the devices we created ourselves as the result of hardware
726 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200727 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
728 if (client->adapter == adapter) {
729 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
730 client->name, client->addr);
731 list_del(&client->detected);
732 i2c_unregister_device(client);
733 }
734 }
735
Jean Delvare026526f2008-01-27 18:14:49 +0100736 if (!driver->detach_adapter)
737 return 0;
738 res = driver->detach_adapter(adapter);
739 if (res)
740 dev_err(&adapter->dev, "detach_adapter failed (%d) "
741 "for driver [%s]\n", res, driver->driver.name);
742 return res;
743}
744
Jean Delvaree549c2b2009-06-19 16:58:19 +0200745static int __unregister_client(struct device *dev, void *dummy)
746{
747 struct i2c_client *client = i2c_verify_client(dev);
748 if (client)
749 i2c_unregister_device(client);
750 return 0;
751}
752
David Brownelld64f73b2007-07-12 14:12:28 +0200753/**
754 * i2c_del_adapter - unregister I2C adapter
755 * @adap: the adapter being unregistered
756 * Context: can sleep
757 *
758 * This unregisters an I2C adapter which was previously registered
759 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
760 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761int i2c_del_adapter(struct i2c_adapter *adap)
762{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200764 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100765 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200768 mutex_lock(&core_lock);
769 found = idr_find(&i2c_adapter_idr, adap->nr);
770 mutex_unlock(&core_lock);
771 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200772 pr_debug("i2c-core: attempting to delete unregistered "
773 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200774 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Jean Delvare026526f2008-01-27 18:14:49 +0100777 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200778 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100779 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
780 i2c_do_del_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200781 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100782 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200783 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100785 /* Remove devices instantiated from sysfs */
786 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
787 if (client->adapter == adap) {
788 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
789 client->name, client->addr);
790 list_del(&client->detected);
791 i2c_unregister_device(client);
792 }
793 }
794
Jean Delvaree549c2b2009-06-19 16:58:19 +0200795 /* Detach any active clients. This can't fail, thus we do not
796 checking the returned value. */
797 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Jean Delvare2bb50952009-09-18 22:45:46 +0200799#ifdef CONFIG_I2C_COMPAT
800 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
801 adap->dev.parent);
802#endif
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* clean up the sysfs representation */
805 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* wait for sysfs to drop all references */
809 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
David Brownell6e13e642007-05-01 23:26:31 +0200811 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200812 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200814 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200816 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200818 /* Clear the device structure in case this adapter is ever going to be
819 added again */
820 memset(&adap->dev, 0, sizeof(adap->dev));
821
Jean Delvare35fc37f2009-06-19 16:58:19 +0200822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
David Brownellc0564602007-05-01 23:26:31 +0200824EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826
David Brownell7b4fbc52007-05-01 23:26:30 +0200827/* ------------------------------------------------------------------------- */
828
Dave Young7f101a92008-07-14 22:38:19 +0200829static int __attach_adapter(struct device *dev, void *data)
830{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200831 struct i2c_adapter *adapter;
Dave Young7f101a92008-07-14 22:38:19 +0200832 struct i2c_driver *driver = data;
833
Jean Delvare4f8cf822009-09-18 22:45:46 +0200834 if (dev->type != &i2c_adapter_type)
835 return 0;
836 adapter = to_i2c_adapter(dev);
837
Jean Delvare4735c982008-07-14 22:38:36 +0200838 i2c_detect(adapter, driver);
839
840 /* Legacy drivers scan i2c busses directly */
841 if (driver->attach_adapter)
842 driver->attach_adapter(adapter);
Dave Young7f101a92008-07-14 22:38:19 +0200843
844 return 0;
845}
846
David Brownell7b4fbc52007-05-01 23:26:30 +0200847/*
848 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200849 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 */
851
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800852int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100854 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
David Brownell1d0b19c2008-10-14 17:30:05 +0200856 /* Can't register until after driver model init */
857 if (unlikely(WARN_ON(!i2c_bus_type.p)))
858 return -EAGAIN;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800861 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Jean Delvare729d6dd2009-06-19 16:58:18 +0200864 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200865 * will have called probe() for all matching-but-unbound devices.
866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 res = driver_register(&driver->driver);
868 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100869 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100870
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100871 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Jean Delvare4735c982008-07-14 22:38:36 +0200873 INIT_LIST_HEAD(&driver->clients);
874 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200875 mutex_lock(&core_lock);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200876 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100877 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200878
Jean Delvare7eebcb72006-02-05 23:28:21 +0100879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800881EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Young7f101a92008-07-14 22:38:19 +0200883static int __detach_adapter(struct device *dev, void *data)
884{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200885 struct i2c_adapter *adapter;
Dave Young7f101a92008-07-14 22:38:19 +0200886 struct i2c_driver *driver = data;
Jean Delvare4735c982008-07-14 22:38:36 +0200887 struct i2c_client *client, *_n;
888
Jean Delvare4f8cf822009-09-18 22:45:46 +0200889 if (dev->type != &i2c_adapter_type)
890 return 0;
891 adapter = to_i2c_adapter(dev);
892
Jean Delvareacec2112009-03-28 21:34:40 +0100893 /* Remove the devices we created ourselves as the result of hardware
894 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200895 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
896 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
897 client->name, client->addr);
898 list_del(&client->detected);
899 i2c_unregister_device(client);
900 }
901
Dave Young7f101a92008-07-14 22:38:19 +0200902 if (driver->detach_adapter) {
903 if (driver->detach_adapter(adapter))
904 dev_err(&adapter->dev,
905 "detach_adapter failed for driver [%s]\n",
906 driver->driver.name);
Dave Young7f101a92008-07-14 22:38:19 +0200907 }
908
909 return 0;
910}
911
David Brownella1d9e6e2007-05-01 23:26:30 +0200912/**
913 * i2c_del_driver - unregister I2C driver
914 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200915 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200916 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200917void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Jean Delvarecaada322008-01-27 18:14:49 +0100919 mutex_lock(&core_lock);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200920 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200921 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100924 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
David Brownellc0564602007-05-01 23:26:31 +0200926EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
David Brownell7b4fbc52007-05-01 23:26:30 +0200928/* ------------------------------------------------------------------------- */
929
David Brownell9b766b82008-01-27 18:14:51 +0100930static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
David Brownell9b766b82008-01-27 18:14:51 +0100932 struct i2c_client *client = i2c_verify_client(dev);
933 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
David Brownell9b766b82008-01-27 18:14:51 +0100935 if (client && client->addr == addr)
936 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return 0;
938}
939
Jean Delvare5e31c2b2007-11-15 19:24:02 +0100940static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
David Brownell9b766b82008-01-27 18:14:51 +0100942 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Jean Delvaree48d3312008-01-27 18:14:48 +0100945/**
946 * i2c_use_client - increments the reference count of the i2c client structure
947 * @client: the client being referenced
948 *
949 * Each live reference to a client should be refcounted. The driver model does
950 * that automatically as part of driver binding, so that most drivers don't
951 * need to do this explicitly: they hold a reference until they're unbound
952 * from the device.
953 *
954 * A pointer to the client with the incremented reference counter is returned.
955 */
956struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957{
David Brownell6ea438e2008-07-14 22:38:24 +0200958 if (client && get_device(&client->dev))
959 return client;
960 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
David Brownellc0564602007-05-01 23:26:31 +0200962EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Jean Delvaree48d3312008-01-27 18:14:48 +0100964/**
965 * i2c_release_client - release a use of the i2c client structure
966 * @client: the client being no longer referenced
967 *
968 * Must be called when a user of a client is finished with it.
969 */
970void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
David Brownell6ea438e2008-07-14 22:38:24 +0200972 if (client)
973 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
David Brownellc0564602007-05-01 23:26:31 +0200975EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
David Brownell9b766b82008-01-27 18:14:51 +0100977struct i2c_cmd_arg {
978 unsigned cmd;
979 void *arg;
980};
981
982static int i2c_cmd(struct device *dev, void *_arg)
983{
984 struct i2c_client *client = i2c_verify_client(dev);
985 struct i2c_cmd_arg *arg = _arg;
986
987 if (client && client->driver && client->driver->command)
988 client->driver->command(client, arg->cmd, arg->arg);
989 return 0;
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
993{
David Brownell9b766b82008-01-27 18:14:51 +0100994 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
David Brownell9b766b82008-01-27 18:14:51 +0100996 cmd_arg.cmd = cmd;
997 cmd_arg.arg = arg;
998 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
David Brownellc0564602007-05-01 23:26:31 +02001000EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002static int __init i2c_init(void)
1003{
1004 int retval;
1005
1006 retval = bus_register(&i2c_bus_type);
1007 if (retval)
1008 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001009#ifdef CONFIG_I2C_COMPAT
1010 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1011 if (!i2c_adapter_compat_class) {
1012 retval = -ENOMEM;
1013 goto bus_err;
1014 }
1015#endif
David Brownelle9f13732008-01-27 18:14:52 +01001016 retval = i2c_add_driver(&dummy_driver);
1017 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001018 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001019 return 0;
1020
Jean Delvare2bb50952009-09-18 22:45:46 +02001021class_err:
1022#ifdef CONFIG_I2C_COMPAT
1023 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001024bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001025#endif
David Brownelle9f13732008-01-27 18:14:52 +01001026 bus_unregister(&i2c_bus_type);
1027 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030static void __exit i2c_exit(void)
1031{
David Brownelle9f13732008-01-27 18:14:52 +01001032 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001033#ifdef CONFIG_I2C_COMPAT
1034 class_compat_unregister(i2c_adapter_compat_class);
1035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 bus_unregister(&i2c_bus_type);
1037}
1038
David Brownella10f9e72008-10-14 17:30:06 +02001039/* We must initialize early, because some subsystems register i2c drivers
1040 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1041 */
1042postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043module_exit(i2c_exit);
1044
1045/* ----------------------------------------------------
1046 * the functional interface to the i2c busses.
1047 * ----------------------------------------------------
1048 */
1049
David Brownella1cdeda2008-07-14 22:38:24 +02001050/**
1051 * i2c_transfer - execute a single or combined I2C message
1052 * @adap: Handle to I2C bus
1053 * @msgs: One or more messages to execute before STOP is issued to
1054 * terminate the operation; each message begins with a START.
1055 * @num: Number of messages to be executed.
1056 *
1057 * Returns negative errno, else the number of messages executed.
1058 *
1059 * Note that there is no requirement that each message be sent to
1060 * the same slave address, although that is the most common model.
1061 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001062int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001064 unsigned long orig_jiffies;
1065 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
David Brownella1cdeda2008-07-14 22:38:24 +02001067 /* REVISIT the fault reporting model here is weak:
1068 *
1069 * - When we get an error after receiving N bytes from a slave,
1070 * there is no way to report "N".
1071 *
1072 * - When we get a NAK after transmitting N bytes to a slave,
1073 * there is no way to report "N" ... or to let the master
1074 * continue executing the rest of this combined message, if
1075 * that's the appropriate response.
1076 *
1077 * - When for example "num" is two and we successfully complete
1078 * the first message but get an error part way through the
1079 * second, it's unclear whether that should be reported as
1080 * one (discarding status on the second message) or errno
1081 * (discarding status on the first one).
1082 */
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (adap->algo->master_xfer) {
1085#ifdef DEBUG
1086 for (ret = 0; ret < num; ret++) {
1087 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001088 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1089 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1090 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092#endif
1093
Mike Rapoportcea443a82008-01-27 18:14:50 +01001094 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001095 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001096 if (!ret)
1097 /* I2C activity is ongoing. */
1098 return -EAGAIN;
1099 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001100 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001101 }
1102
Clifford Wolf66b650f2009-06-15 18:01:46 +02001103 /* Retry automatically on arbitration loss */
1104 orig_jiffies = jiffies;
1105 for (ret = 0, try = 0; try <= adap->retries; try++) {
1106 ret = adap->algo->master_xfer(adap, msgs, num);
1107 if (ret != -EAGAIN)
1108 break;
1109 if (time_after(jiffies, orig_jiffies + adap->timeout))
1110 break;
1111 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001112 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 return ret;
1115 } else {
1116 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001117 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
1119}
David Brownellc0564602007-05-01 23:26:31 +02001120EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
David Brownella1cdeda2008-07-14 22:38:24 +02001122/**
1123 * i2c_master_send - issue a single I2C message in master transmit mode
1124 * @client: Handle to slave device
1125 * @buf: Data that will be written to the slave
1126 * @count: How many bytes to write
1127 *
1128 * Returns negative errno, or else the number of bytes written.
1129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1131{
1132 int ret;
1133 struct i2c_adapter *adap=client->adapter;
1134 struct i2c_msg msg;
1135
Jean Delvare815f55f2005-05-07 22:58:46 +02001136 msg.addr = client->addr;
1137 msg.flags = client->flags & I2C_M_TEN;
1138 msg.len = count;
1139 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001140
Jean Delvare815f55f2005-05-07 22:58:46 +02001141 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Jean Delvare815f55f2005-05-07 22:58:46 +02001143 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1144 transmitted, else error code. */
1145 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
David Brownellc0564602007-05-01 23:26:31 +02001147EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
David Brownella1cdeda2008-07-14 22:38:24 +02001149/**
1150 * i2c_master_recv - issue a single I2C message in master receive mode
1151 * @client: Handle to slave device
1152 * @buf: Where to store data read from slave
1153 * @count: How many bytes to read
1154 *
1155 * Returns negative errno, or else the number of bytes read.
1156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1158{
1159 struct i2c_adapter *adap=client->adapter;
1160 struct i2c_msg msg;
1161 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jean Delvare815f55f2005-05-07 22:58:46 +02001163 msg.addr = client->addr;
1164 msg.flags = client->flags & I2C_M_TEN;
1165 msg.flags |= I2C_M_RD;
1166 msg.len = count;
1167 msg.buf = buf;
1168
1169 ret = i2c_transfer(adap, &msg, 1);
1170
1171 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1172 transmitted, else error code. */
1173 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
David Brownellc0564602007-05-01 23:26:31 +02001175EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177/* ----------------------------------------------------
1178 * the i2c address scanning function
1179 * Will not work for 10-bit addresses!
1180 * ----------------------------------------------------
1181 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001182
Jean Delvareccfbbd02009-12-06 17:06:25 +01001183static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001184 struct i2c_driver *driver)
1185{
1186 struct i2c_board_info info;
1187 struct i2c_adapter *adapter = temp_client->adapter;
1188 int addr = temp_client->addr;
1189 int err;
1190
1191 /* Make sure the address is valid */
1192 if (addr < 0x03 || addr > 0x77) {
1193 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1194 addr);
1195 return -EINVAL;
1196 }
1197
1198 /* Skip if already in use */
1199 if (i2c_check_addr(adapter, addr))
1200 return 0;
1201
Jean Delvareccfbbd02009-12-06 17:06:25 +01001202 /* Make sure there is something at this address */
1203 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1204 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001205
Jean Delvareccfbbd02009-12-06 17:06:25 +01001206 /* Prevent 24RF08 corruption */
1207 if ((addr & ~0x0f) == 0x50)
1208 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
Jean Delvare4735c982008-07-14 22:38:36 +02001209
1210 /* Finally call the custom detection function */
1211 memset(&info, 0, sizeof(struct i2c_board_info));
1212 info.addr = addr;
Jean Delvareccfbbd02009-12-06 17:06:25 +01001213 err = driver->detect(temp_client, -1, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001214 if (err) {
1215 /* -ENODEV is returned if the detection fails. We catch it
1216 here as this isn't an error. */
1217 return err == -ENODEV ? 0 : err;
1218 }
1219
1220 /* Consistency check */
1221 if (info.type[0] == '\0') {
1222 dev_err(&adapter->dev, "%s detection function provided "
1223 "no name for 0x%x\n", driver->driver.name,
1224 addr);
1225 } else {
1226 struct i2c_client *client;
1227
1228 /* Detection succeeded, instantiate the device */
1229 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1230 info.type, info.addr);
1231 client = i2c_new_device(adapter, &info);
1232 if (client)
1233 list_add_tail(&client->detected, &driver->clients);
1234 else
1235 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1236 info.type, info.addr);
1237 }
1238 return 0;
1239}
1240
1241static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1242{
1243 const struct i2c_client_address_data *address_data;
1244 struct i2c_client *temp_client;
1245 int i, err = 0;
1246 int adap_id = i2c_adapter_id(adapter);
1247
1248 address_data = driver->address_data;
1249 if (!driver->detect || !address_data)
1250 return 0;
1251
1252 /* Set up a temporary client to help detect callback */
1253 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1254 if (!temp_client)
1255 return -ENOMEM;
1256 temp_client->adapter = adapter;
1257
Jean Delvare4329cf82008-08-28 08:33:23 +02001258 /* Stop here if the classes do not match */
1259 if (!(adapter->class & driver->class))
1260 goto exit_free;
1261
Jean Delvare4735c982008-07-14 22:38:36 +02001262 /* Stop here if we can't use SMBUS_QUICK */
1263 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec7b25a92009-12-06 17:06:24 +01001264 if (address_data->normal_i2c[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001265 goto exit_free;
1266
1267 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1268 "can't probe for chips\n");
1269 err = -EOPNOTSUPP;
1270 goto exit_free;
1271 }
1272
Jean Delvare4735c982008-07-14 22:38:36 +02001273 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001274 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1275 "addr 0x%02x\n", adap_id,
1276 address_data->normal_i2c[i]);
1277 temp_client->addr = address_data->normal_i2c[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001278 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001279 if (err)
1280 goto exit_free;
1281 }
1282
1283 exit_free:
1284 kfree(temp_client);
1285 return err;
1286}
1287
Jean Delvare12b5053a2007-05-01 23:26:31 +02001288struct i2c_client *
1289i2c_new_probed_device(struct i2c_adapter *adap,
1290 struct i2c_board_info *info,
1291 unsigned short const *addr_list)
1292{
1293 int i;
1294
1295 /* Stop here if the bus doesn't support probing */
1296 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1297 dev_err(&adap->dev, "Probing not supported\n");
1298 return NULL;
1299 }
1300
Jean Delvare12b5053a2007-05-01 23:26:31 +02001301 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1302 /* Check address validity */
1303 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1304 dev_warn(&adap->dev, "Invalid 7-bit address "
1305 "0x%02x\n", addr_list[i]);
1306 continue;
1307 }
1308
1309 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001310 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001311 dev_dbg(&adap->dev, "Address 0x%02x already in "
1312 "use, not probing\n", addr_list[i]);
1313 continue;
1314 }
1315
1316 /* Test address responsiveness
1317 The default probe method is a quick write, but it is known
1318 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1319 and could also irreversibly write-protect some EEPROMs, so
1320 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1321 read instead. Also, some bus drivers don't implement
1322 quick write, so we fallback to a byte read it that case
1323 too. */
1324 if ((addr_list[i] & ~0x07) == 0x30
1325 || (addr_list[i] & ~0x0f) == 0x50
1326 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001327 union i2c_smbus_data data;
1328
Jean Delvare12b5053a2007-05-01 23:26:31 +02001329 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1330 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001331 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001332 break;
1333 } else {
1334 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1335 I2C_SMBUS_WRITE, 0,
1336 I2C_SMBUS_QUICK, NULL) >= 0)
1337 break;
1338 }
1339 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001340
1341 if (addr_list[i] == I2C_CLIENT_END) {
1342 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1343 return NULL;
1344 }
1345
1346 info->addr = addr_list[i];
1347 return i2c_new_device(adap, info);
1348}
1349EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351struct i2c_adapter* i2c_get_adapter(int id)
1352{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001354
Jean Delvarecaada322008-01-27 18:14:49 +01001355 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001356 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001357 if (adapter && !try_module_get(adapter->owner))
1358 adapter = NULL;
1359
Jean Delvarecaada322008-01-27 18:14:49 +01001360 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001361 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
David Brownellc0564602007-05-01 23:26:31 +02001363EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365void i2c_put_adapter(struct i2c_adapter *adap)
1366{
1367 module_put(adap->owner);
1368}
David Brownellc0564602007-05-01 23:26:31 +02001369EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371/* The SMBus parts */
1372
David Brownell438d6c22006-12-10 21:21:31 +01001373#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001374static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001379 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 data = data ^ POLY;
1381 data = data << 1;
1382 }
1383 return (u8)(data >> 8);
1384}
1385
Jean Delvare421ef472005-10-26 21:28:55 +02001386/* Incremental CRC8 over count bytes in the array pointed to by p */
1387static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388{
1389 int i;
1390
1391 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001392 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return crc;
1394}
1395
Jean Delvare421ef472005-10-26 21:28:55 +02001396/* Assume a 7-bit address, which is reasonable for SMBus */
1397static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Jean Delvare421ef472005-10-26 21:28:55 +02001399 /* The address will be sent first */
1400 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1401 pec = i2c_smbus_pec(pec, &addr, 1);
1402
1403 /* The data buffer follows */
1404 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406
Jean Delvare421ef472005-10-26 21:28:55 +02001407/* Used for write only transactions */
1408static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Jean Delvare421ef472005-10-26 21:28:55 +02001410 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1411 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Jean Delvare421ef472005-10-26 21:28:55 +02001414/* Return <0 on CRC error
1415 If there was a write before this read (most cases) we need to take the
1416 partial CRC from the write part into account.
1417 Note that this function does modify the message (we need to decrease the
1418 message length to hide the CRC byte from the caller). */
1419static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Jean Delvare421ef472005-10-26 21:28:55 +02001421 u8 rpec = msg->buf[--msg->len];
1422 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (rpec != cpec) {
1425 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1426 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001427 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
David Brownell438d6c22006-12-10 21:21:31 +01001429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
David Brownella1cdeda2008-07-14 22:38:24 +02001432/**
1433 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1434 * @client: Handle to slave device
1435 *
1436 * This executes the SMBus "receive byte" protocol, returning negative errno
1437 * else the byte received from the device.
1438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439s32 i2c_smbus_read_byte(struct i2c_client *client)
1440{
1441 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001442 int status;
1443
1444 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1445 I2C_SMBUS_READ, 0,
1446 I2C_SMBUS_BYTE, &data);
1447 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448}
David Brownellc0564602007-05-01 23:26:31 +02001449EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
David Brownella1cdeda2008-07-14 22:38:24 +02001451/**
1452 * i2c_smbus_write_byte - SMBus "send byte" protocol
1453 * @client: Handle to slave device
1454 * @value: Byte to be sent
1455 *
1456 * This executes the SMBus "send byte" protocol, returning negative errno
1457 * else zero on success.
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001462 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
David Brownellc0564602007-05-01 23:26:31 +02001464EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
David Brownella1cdeda2008-07-14 22:38:24 +02001466/**
1467 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1468 * @client: Handle to slave device
1469 * @command: Byte interpreted by slave
1470 *
1471 * This executes the SMBus "read byte" protocol, returning negative errno
1472 * else a data byte received from the device.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1475{
1476 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001477 int status;
1478
1479 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1480 I2C_SMBUS_READ, command,
1481 I2C_SMBUS_BYTE_DATA, &data);
1482 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
David Brownellc0564602007-05-01 23:26:31 +02001484EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
David Brownella1cdeda2008-07-14 22:38:24 +02001486/**
1487 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1488 * @client: Handle to slave device
1489 * @command: Byte interpreted by slave
1490 * @value: Byte being written
1491 *
1492 * This executes the SMBus "write byte" protocol, returning negative errno
1493 * else zero on success.
1494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1496{
1497 union i2c_smbus_data data;
1498 data.byte = value;
1499 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1500 I2C_SMBUS_WRITE,command,
1501 I2C_SMBUS_BYTE_DATA,&data);
1502}
David Brownellc0564602007-05-01 23:26:31 +02001503EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
David Brownella1cdeda2008-07-14 22:38:24 +02001505/**
1506 * i2c_smbus_read_word_data - SMBus "read word" protocol
1507 * @client: Handle to slave device
1508 * @command: Byte interpreted by slave
1509 *
1510 * This executes the SMBus "read word" protocol, returning negative errno
1511 * else a 16-bit unsigned "word" received from the device.
1512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1514{
1515 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001516 int status;
1517
1518 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1519 I2C_SMBUS_READ, command,
1520 I2C_SMBUS_WORD_DATA, &data);
1521 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
David Brownellc0564602007-05-01 23:26:31 +02001523EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
David Brownella1cdeda2008-07-14 22:38:24 +02001525/**
1526 * i2c_smbus_write_word_data - SMBus "write word" protocol
1527 * @client: Handle to slave device
1528 * @command: Byte interpreted by slave
1529 * @value: 16-bit "word" being written
1530 *
1531 * This executes the SMBus "write word" protocol, returning negative errno
1532 * else zero on success.
1533 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1535{
1536 union i2c_smbus_data data;
1537 data.word = value;
1538 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1539 I2C_SMBUS_WRITE,command,
1540 I2C_SMBUS_WORD_DATA,&data);
1541}
David Brownellc0564602007-05-01 23:26:31 +02001542EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
David Brownella64ec072007-10-13 23:56:31 +02001544/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001545 * i2c_smbus_process_call - SMBus "process call" protocol
1546 * @client: Handle to slave device
1547 * @command: Byte interpreted by slave
1548 * @value: 16-bit "word" being written
1549 *
1550 * This executes the SMBus "process call" protocol, returning negative errno
1551 * else a 16-bit unsigned "word" received from the device.
1552 */
1553s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1554{
1555 union i2c_smbus_data data;
1556 int status;
1557 data.word = value;
1558
1559 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1560 I2C_SMBUS_WRITE, command,
1561 I2C_SMBUS_PROC_CALL, &data);
1562 return (status < 0) ? status : data.word;
1563}
1564EXPORT_SYMBOL(i2c_smbus_process_call);
1565
1566/**
David Brownella1cdeda2008-07-14 22:38:24 +02001567 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001568 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001569 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001570 * @values: Byte array into which data will be read; big enough to hold
1571 * the data returned by the slave. SMBus allows at most 32 bytes.
1572 *
David Brownella1cdeda2008-07-14 22:38:24 +02001573 * This executes the SMBus "block read" protocol, returning negative errno
1574 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001575 *
1576 * Note that using this function requires that the client's adapter support
1577 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1578 * support this; its emulation through I2C messaging relies on a specific
1579 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1580 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001581s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1582 u8 *values)
1583{
1584 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001585 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001586
David Brownell24a5bb72008-07-14 22:38:23 +02001587 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1588 I2C_SMBUS_READ, command,
1589 I2C_SMBUS_BLOCK_DATA, &data);
1590 if (status)
1591 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001592
1593 memcpy(values, &data.block[1], data.block[0]);
1594 return data.block[0];
1595}
1596EXPORT_SYMBOL(i2c_smbus_read_block_data);
1597
David Brownella1cdeda2008-07-14 22:38:24 +02001598/**
1599 * i2c_smbus_write_block_data - SMBus "block write" protocol
1600 * @client: Handle to slave device
1601 * @command: Byte interpreted by slave
1602 * @length: Size of data block; SMBus allows at most 32 bytes
1603 * @values: Byte array which will be written.
1604 *
1605 * This executes the SMBus "block write" protocol, returning negative errno
1606 * else zero on success.
1607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001609 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
1611 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (length > I2C_SMBUS_BLOCK_MAX)
1614 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001616 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1618 I2C_SMBUS_WRITE,command,
1619 I2C_SMBUS_BLOCK_DATA,&data);
1620}
David Brownellc0564602007-05-01 23:26:31 +02001621EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001624s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1625 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001628 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001629
Jean Delvare4b2643d2007-07-12 14:12:29 +02001630 if (length > I2C_SMBUS_BLOCK_MAX)
1631 length = I2C_SMBUS_BLOCK_MAX;
1632 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001633 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1634 I2C_SMBUS_READ, command,
1635 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1636 if (status < 0)
1637 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001638
1639 memcpy(values, &data.block[1], data.block[0]);
1640 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
David Brownellc0564602007-05-01 23:26:31 +02001642EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jean Delvare21bbd692006-01-09 15:19:18 +11001644s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001645 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001646{
1647 union i2c_smbus_data data;
1648
1649 if (length > I2C_SMBUS_BLOCK_MAX)
1650 length = I2C_SMBUS_BLOCK_MAX;
1651 data.block[0] = length;
1652 memcpy(data.block + 1, values, length);
1653 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1654 I2C_SMBUS_WRITE, command,
1655 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1656}
David Brownellc0564602007-05-01 23:26:31 +02001657EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001658
David Brownell438d6c22006-12-10 21:21:31 +01001659/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001661static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001663 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 union i2c_smbus_data * data)
1665{
1666 /* So we need to generate a series of msgs. In the case of writing, we
1667 need to use only one message; when reading, we need two. We initialize
1668 most things with sane defaults, to keep the code below somewhat
1669 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001670 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1671 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001673 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1675 };
1676 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001677 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001678 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 msgbuf0[0] = command;
1681 switch(size) {
1682 case I2C_SMBUS_QUICK:
1683 msg[0].len = 0;
1684 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001685 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1686 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 num = 1;
1688 break;
1689 case I2C_SMBUS_BYTE:
1690 if (read_write == I2C_SMBUS_READ) {
1691 /* Special case: only a read! */
1692 msg[0].flags = I2C_M_RD | flags;
1693 num = 1;
1694 }
1695 break;
1696 case I2C_SMBUS_BYTE_DATA:
1697 if (read_write == I2C_SMBUS_READ)
1698 msg[1].len = 1;
1699 else {
1700 msg[0].len = 2;
1701 msgbuf0[1] = data->byte;
1702 }
1703 break;
1704 case I2C_SMBUS_WORD_DATA:
1705 if (read_write == I2C_SMBUS_READ)
1706 msg[1].len = 2;
1707 else {
1708 msg[0].len=3;
1709 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001710 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712 break;
1713 case I2C_SMBUS_PROC_CALL:
1714 num = 2; /* Special case */
1715 read_write = I2C_SMBUS_READ;
1716 msg[0].len = 3;
1717 msg[1].len = 2;
1718 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001719 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001723 msg[1].flags |= I2C_M_RECV_LEN;
1724 msg[1].len = 1; /* block length will be added by
1725 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 } else {
1727 msg[0].len = data->block[0] + 2;
1728 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001729 dev_err(&adapter->dev,
1730 "Invalid block write size %d\n",
1731 data->block[0]);
1732 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001734 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 msgbuf0[i] = data->block[i-1];
1736 }
1737 break;
1738 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001739 num = 2; /* Another special case */
1740 read_write = I2C_SMBUS_READ;
1741 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001742 dev_err(&adapter->dev,
1743 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001744 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001745 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001746 }
1747 msg[0].len = data->block[0] + 2;
1748 for (i = 1; i < msg[0].len; i++)
1749 msgbuf0[i] = data->block[i-1];
1750 msg[1].flags |= I2C_M_RECV_LEN;
1751 msg[1].len = 1; /* block length will be added by
1752 the underlying bus driver */
1753 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 case I2C_SMBUS_I2C_BLOCK_DATA:
1755 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001756 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 } else {
1758 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001759 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001760 dev_err(&adapter->dev,
1761 "Invalid block write size %d\n",
1762 data->block[0]);
1763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765 for (i = 1; i <= data->block[0]; i++)
1766 msgbuf0[i] = data->block[i];
1767 }
1768 break;
1769 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001770 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1771 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Jean Delvare421ef472005-10-26 21:28:55 +02001774 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1775 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1776 if (i) {
1777 /* Compute PEC if first message is a write */
1778 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001779 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001780 i2c_smbus_add_pec(&msg[0]);
1781 else /* Write followed by read */
1782 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1783 }
1784 /* Ask for PEC if last message is a read */
1785 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001786 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001787 }
1788
David Brownell24a5bb72008-07-14 22:38:23 +02001789 status = i2c_transfer(adapter, msg, num);
1790 if (status < 0)
1791 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Jean Delvare421ef472005-10-26 21:28:55 +02001793 /* Check PEC if last message is a read */
1794 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001795 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1796 if (status < 0)
1797 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001798 }
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (read_write == I2C_SMBUS_READ)
1801 switch(size) {
1802 case I2C_SMBUS_BYTE:
1803 data->byte = msgbuf0[0];
1804 break;
1805 case I2C_SMBUS_BYTE_DATA:
1806 data->byte = msgbuf1[0];
1807 break;
David Brownell438d6c22006-12-10 21:21:31 +01001808 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 case I2C_SMBUS_PROC_CALL:
1810 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1811 break;
1812 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001813 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 data->block[i+1] = msgbuf1[i];
1815 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001816 case I2C_SMBUS_BLOCK_DATA:
1817 case I2C_SMBUS_BLOCK_PROC_CALL:
1818 for (i = 0; i < msgbuf1[0] + 1; i++)
1819 data->block[i] = msgbuf1[i];
1820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 return 0;
1823}
1824
David Brownella1cdeda2008-07-14 22:38:24 +02001825/**
1826 * i2c_smbus_xfer - execute SMBus protocol operations
1827 * @adapter: Handle to I2C bus
1828 * @addr: Address of SMBus slave on that bus
1829 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1830 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1831 * @command: Byte interpreted by slave, for protocols which use such bytes
1832 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1833 * @data: Data to be read or written
1834 *
1835 * This executes an SMBus protocol operation, and returns a negative
1836 * errno code else zero on success.
1837 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001838s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001839 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001840 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001842 unsigned long orig_jiffies;
1843 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001849 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001850
1851 /* Retry automatically on arbitration loss */
1852 orig_jiffies = jiffies;
1853 for (res = 0, try = 0; try <= adapter->retries; try++) {
1854 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1855 read_write, command,
1856 protocol, data);
1857 if (res != -EAGAIN)
1858 break;
1859 if (time_after(jiffies,
1860 orig_jiffies + adapter->timeout))
1861 break;
1862 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001863 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 } else
1865 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001866 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 return res;
1869}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1873MODULE_DESCRIPTION("I2C-Bus main module");
1874MODULE_LICENSE("GPL");