blob: 4099b2b8c39254c4205d56e1e513acef65ff2df8 [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 Rapoportcea443a2008-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>
Mark Brown6de468a2010-03-02 12:23:46 +010037#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/uaccess.h>
39
David Brownell9c1600e2007-05-01 23:26:31 +020040#include "i2c-core.h"
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvare99cd8e22009-06-19 16:58:20 +020043/* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020044 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010046static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DEFINE_IDR(i2c_adapter_idr);
Jean Delvare99cd8e22009-06-19 16:58:20 +020048static LIST_HEAD(userspace_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jean Delvare4f8cf822009-09-18 22:45:46 +020050static struct device_type i2c_client_type;
Jean Delvaref8a227e2009-06-19 16:58:18 +020051static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
Jean Delvare4735c982008-07-14 22:38:36 +020052static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010053
54/* ------------------------------------------------------------------------- */
55
Jean Delvared2653e92008-04-29 23:11:39 +020056static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
57 const struct i2c_client *client)
58{
59 while (id->name[0]) {
60 if (strcmp(client->name, id->name) == 0)
61 return id;
62 id++;
63 }
64 return NULL;
65}
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int i2c_device_match(struct device *dev, struct device_driver *drv)
68{
Jean Delvare51298d12009-09-18 22:45:45 +020069 struct i2c_client *client = i2c_verify_client(dev);
70 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020071
Jean Delvare51298d12009-09-18 22:45:45 +020072 if (!client)
73 return 0;
74
75 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020076 /* match on an id table if there is one */
77 if (driver->id_table)
78 return i2c_match_id(driver->id_table, client) != NULL;
79
Jean Delvareeb8a7902008-05-18 20:49:41 +020080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
David Brownell7b4fbc52007-05-01 23:26:30 +020083#ifdef CONFIG_HOTPLUG
84
85/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020086static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020087{
88 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +020089
Jean Delvareeb8a7902008-05-18 20:49:41 +020090 if (add_uevent_var(env, "MODALIAS=%s%s",
91 I2C_MODULE_PREFIX, client->name))
92 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +020093 dev_dbg(dev, "uevent\n");
94 return 0;
95}
96
97#else
98#define i2c_device_uevent NULL
99#endif /* CONFIG_HOTPLUG */
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int i2c_device_probe(struct device *dev)
102{
Jean Delvare51298d12009-09-18 22:45:45 +0200103 struct i2c_client *client = i2c_verify_client(dev);
104 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100105 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200106
Jean Delvare51298d12009-09-18 22:45:45 +0200107 if (!client)
108 return 0;
109
110 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200111 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200112 return -ENODEV;
113 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200114 if (!device_can_wakeup(&client->dev))
115 device_init_wakeup(&client->dev,
116 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200117 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvaree0457442008-07-14 22:38:30 +0200119 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Hans Verkuil50c33042008-03-12 14:15:00 +0100120 if (status)
121 client->driver = NULL;
122 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125static int i2c_device_remove(struct device *dev)
126{
Jean Delvare51298d12009-09-18 22:45:45 +0200127 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200128 struct i2c_driver *driver;
129 int status;
130
Jean Delvare51298d12009-09-18 22:45:45 +0200131 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200132 return 0;
133
134 driver = to_i2c_driver(dev->driver);
135 if (driver->remove) {
136 dev_dbg(dev, "remove\n");
137 status = driver->remove(client);
138 } else {
139 dev->driver = NULL;
140 status = 0;
141 }
142 if (status == 0)
143 client->driver = NULL;
144 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
David Brownellf37dd802007-02-13 22:09:00 +0100147static void i2c_device_shutdown(struct device *dev)
148{
Jean Delvare51298d12009-09-18 22:45:45 +0200149 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100150 struct i2c_driver *driver;
151
Jean Delvare51298d12009-09-18 22:45:45 +0200152 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100153 return;
154 driver = to_i2c_driver(dev->driver);
155 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200156 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100157}
158
sonic zhang54067ee2009-12-14 21:17:30 +0100159#ifdef CONFIG_SUSPEND
160static int i2c_device_pm_suspend(struct device *dev)
161{
162 const struct dev_pm_ops *pm;
163
164 if (!dev->driver)
165 return 0;
166 pm = dev->driver->pm;
167 if (!pm || !pm->suspend)
168 return 0;
169 return pm->suspend(dev);
170}
171
172static int i2c_device_pm_resume(struct device *dev)
173{
174 const struct dev_pm_ops *pm;
175
176 if (!dev->driver)
177 return 0;
178 pm = dev->driver->pm;
179 if (!pm || !pm->resume)
180 return 0;
181 return pm->resume(dev);
182}
183#else
184#define i2c_device_pm_suspend NULL
185#define i2c_device_pm_resume NULL
186#endif
187
Mark Brown6de468a2010-03-02 12:23:46 +0100188#ifdef CONFIG_PM_RUNTIME
189static int i2c_device_runtime_suspend(struct device *dev)
190{
191 const struct dev_pm_ops *pm;
192
193 if (!dev->driver)
194 return 0;
195 pm = dev->driver->pm;
196 if (!pm || !pm->runtime_suspend)
197 return 0;
198 return pm->runtime_suspend(dev);
199}
200
201static int i2c_device_runtime_resume(struct device *dev)
202{
203 const struct dev_pm_ops *pm;
204
205 if (!dev->driver)
206 return 0;
207 pm = dev->driver->pm;
208 if (!pm || !pm->runtime_resume)
209 return 0;
210 return pm->runtime_resume(dev);
211}
212
213static int i2c_device_runtime_idle(struct device *dev)
214{
215 const struct dev_pm_ops *pm = NULL;
216 int ret;
217
218 if (dev->driver)
219 pm = dev->driver->pm;
220 if (pm && pm->runtime_idle) {
221 ret = pm->runtime_idle(dev);
222 if (ret)
223 return ret;
224 }
225
226 return pm_runtime_suspend(dev);
227}
228#else
229#define i2c_device_runtime_suspend NULL
230#define i2c_device_runtime_resume NULL
231#define i2c_device_runtime_idle NULL
232#endif
233
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100234static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100235{
Jean Delvare51298d12009-09-18 22:45:45 +0200236 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100237 struct i2c_driver *driver;
238
Jean Delvare51298d12009-09-18 22:45:45 +0200239 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100240 return 0;
241 driver = to_i2c_driver(dev->driver);
242 if (!driver->suspend)
243 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200244 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100245}
246
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100247static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100248{
Jean Delvare51298d12009-09-18 22:45:45 +0200249 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100250 struct i2c_driver *driver;
251
Jean Delvare51298d12009-09-18 22:45:45 +0200252 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100253 return 0;
254 driver = to_i2c_driver(dev->driver);
255 if (!driver->resume)
256 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200257 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100258}
259
David Brownell9c1600e2007-05-01 23:26:31 +0200260static void i2c_client_dev_release(struct device *dev)
261{
262 kfree(to_i2c_client(dev));
263}
264
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100265static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200266show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200267{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200268 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
269 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200270}
271
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100272static ssize_t
273show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200274{
275 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200276 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200277}
278
Jean Delvare4f8cf822009-09-18 22:45:46 +0200279static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200280static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
281
282static struct attribute *i2c_dev_attrs[] = {
283 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200284 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200285 &dev_attr_modalias.attr,
286 NULL
287};
288
289static struct attribute_group i2c_dev_attr_group = {
290 .attrs = i2c_dev_attrs,
291};
292
293static const struct attribute_group *i2c_dev_attr_groups[] = {
294 &i2c_dev_attr_group,
295 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200296};
297
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100298static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100299 .suspend = i2c_device_pm_suspend,
300 .resume = i2c_device_pm_resume,
Mark Brown6de468a2010-03-02 12:23:46 +0100301 .runtime_suspend = i2c_device_runtime_suspend,
302 .runtime_resume = i2c_device_runtime_resume,
303 .runtime_idle = i2c_device_runtime_idle,
sonic zhang54067ee2009-12-14 21:17:30 +0100304};
305
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200306struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100307 .name = "i2c",
308 .match = i2c_device_match,
309 .probe = i2c_device_probe,
310 .remove = i2c_device_remove,
311 .shutdown = i2c_device_shutdown,
312 .suspend = i2c_device_suspend,
313 .resume = i2c_device_resume,
sonic zhang54067ee2009-12-14 21:17:30 +0100314 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000315};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200316EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000317
Jean Delvare51298d12009-09-18 22:45:45 +0200318static struct device_type i2c_client_type = {
319 .groups = i2c_dev_attr_groups,
320 .uevent = i2c_device_uevent,
321 .release = i2c_client_dev_release,
322};
323
David Brownell9b766b82008-01-27 18:14:51 +0100324
325/**
326 * i2c_verify_client - return parameter as i2c_client, or NULL
327 * @dev: device, probably from some driver model iterator
328 *
329 * When traversing the driver model tree, perhaps using driver model
330 * iterators like @device_for_each_child(), you can't assume very much
331 * about the nodes you find. Use this function to avoid oopses caused
332 * by wrongly treating some non-I2C device as an i2c_client.
333 */
334struct i2c_client *i2c_verify_client(struct device *dev)
335{
Jean Delvare51298d12009-09-18 22:45:45 +0200336 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100337 ? to_i2c_client(dev)
338 : NULL;
339}
340EXPORT_SYMBOL(i2c_verify_client);
341
342
David Brownell9c1600e2007-05-01 23:26:31 +0200343/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200344 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200345 * @adap: the adapter managing the device
346 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200347 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200348 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200349 * Create an i2c device. Binding is handled through driver model
350 * probe()/remove() methods. A driver may be bound to this device when we
351 * return from this function, or any later moment (e.g. maybe hotplugging will
352 * load the driver module). This call is not appropriate for use by mainboard
353 * initialization logic, which usually runs during an arch_initcall() long
354 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200355 *
356 * This returns the new i2c client, which may be saved for later use with
357 * i2c_unregister_device(); or NULL to indicate an error.
358 */
359struct i2c_client *
360i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
361{
362 struct i2c_client *client;
363 int status;
364
365 client = kzalloc(sizeof *client, GFP_KERNEL);
366 if (!client)
367 return NULL;
368
369 client->adapter = adap;
370
371 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200372
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200373 if (info->archdata)
374 client->dev.archdata = *info->archdata;
375
Marc Pignatee354252008-08-28 08:33:22 +0200376 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200377 client->addr = info->addr;
378 client->irq = info->irq;
379
David Brownell9c1600e2007-05-01 23:26:31 +0200380 strlcpy(client->name, info->type, sizeof(client->name));
381
Jean Delvaref8a227e2009-06-19 16:58:18 +0200382 /* Check for address business */
383 status = i2c_check_addr(adap, client->addr);
384 if (status)
385 goto out_err;
386
387 client->dev.parent = &client->adapter->dev;
388 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200389 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700390#ifdef CONFIG_OF
391 client->dev.of_node = info->of_node;
392#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200393
394 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
395 client->addr);
396 status = device_register(&client->dev);
397 if (status)
398 goto out_err;
399
Jean Delvaref8a227e2009-06-19 16:58:18 +0200400 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
401 client->name, dev_name(&client->dev));
402
David Brownell9c1600e2007-05-01 23:26:31 +0200403 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200404
405out_err:
406 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
407 "(%d)\n", client->name, client->addr, status);
408 kfree(client);
409 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200410}
411EXPORT_SYMBOL_GPL(i2c_new_device);
412
413
414/**
415 * i2c_unregister_device - reverse effect of i2c_new_device()
416 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200417 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200418 */
419void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200420{
David Brownella1d9e6e2007-05-01 23:26:30 +0200421 device_unregister(&client->dev);
422}
David Brownell9c1600e2007-05-01 23:26:31 +0200423EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200424
425
Jean Delvare60b129d2008-05-11 20:37:06 +0200426static const struct i2c_device_id dummy_id[] = {
427 { "dummy", 0 },
428 { },
429};
430
Jean Delvared2653e92008-04-29 23:11:39 +0200431static int dummy_probe(struct i2c_client *client,
432 const struct i2c_device_id *id)
433{
434 return 0;
435}
436
437static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100438{
439 return 0;
440}
441
442static struct i2c_driver dummy_driver = {
443 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200444 .probe = dummy_probe,
445 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200446 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100447};
448
449/**
450 * i2c_new_dummy - return a new i2c device bound to a dummy driver
451 * @adapter: the adapter managing the device
452 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100453 * Context: can sleep
454 *
455 * This returns an I2C client bound to the "dummy" driver, intended for use
456 * with devices that consume multiple addresses. Examples of such chips
457 * include various EEPROMS (like 24c04 and 24c08 models).
458 *
459 * These dummy devices have two main uses. First, most I2C and SMBus calls
460 * except i2c_transfer() need a client handle; the dummy will be that handle.
461 * And second, this prevents the specified address from being bound to a
462 * different driver.
463 *
464 * This returns the new i2c client, which should be saved for later use with
465 * i2c_unregister_device(); or NULL to indicate an error.
466 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100467struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100468{
469 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200470 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100471 };
472
David Brownelle9f13732008-01-27 18:14:52 +0100473 return i2c_new_device(adapter, &info);
474}
475EXPORT_SYMBOL_GPL(i2c_new_dummy);
476
David Brownellf37dd802007-02-13 22:09:00 +0100477/* ------------------------------------------------------------------------- */
478
David Brownell16ffadf2007-05-01 23:26:28 +0200479/* I2C bus adapters -- one roots each I2C or SMBUS segment */
480
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200481static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David Brownellef2c83212007-05-01 23:26:28 +0200483 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 complete(&adap->dev_released);
485}
486
Jean Delvare99cd8e22009-06-19 16:58:20 +0200487/*
488 * Let users instantiate I2C devices through sysfs. This can be used when
489 * platform initialization code doesn't contain the proper data for
490 * whatever reason. Also useful for drivers that do device detection and
491 * detection fails, either because the device uses an unexpected address,
492 * or this is a compatible device with different ID register values.
493 *
494 * Parameter checking may look overzealous, but we really don't want
495 * the user to provide incorrect parameters.
496 */
497static ssize_t
498i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
500{
501 struct i2c_adapter *adap = to_i2c_adapter(dev);
502 struct i2c_board_info info;
503 struct i2c_client *client;
504 char *blank, end;
505 int res;
506
507 dev_warn(dev, "The new_device interface is still experimental "
508 "and may change in a near future\n");
509 memset(&info, 0, sizeof(struct i2c_board_info));
510
511 blank = strchr(buf, ' ');
512 if (!blank) {
513 dev_err(dev, "%s: Missing parameters\n", "new_device");
514 return -EINVAL;
515 }
516 if (blank - buf > I2C_NAME_SIZE - 1) {
517 dev_err(dev, "%s: Invalid device name\n", "new_device");
518 return -EINVAL;
519 }
520 memcpy(info.type, buf, blank - buf);
521
522 /* Parse remaining parameters, reject extra parameters */
523 res = sscanf(++blank, "%hi%c", &info.addr, &end);
524 if (res < 1) {
525 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
526 return -EINVAL;
527 }
528 if (res > 1 && end != '\n') {
529 dev_err(dev, "%s: Extra parameters\n", "new_device");
530 return -EINVAL;
531 }
532
533 if (info.addr < 0x03 || info.addr > 0x77) {
534 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
535 info.addr);
536 return -EINVAL;
537 }
538
539 client = i2c_new_device(adap, &info);
540 if (!client)
541 return -EEXIST;
542
543 /* Keep track of the added device */
544 mutex_lock(&core_lock);
545 list_add_tail(&client->detected, &userspace_devices);
546 mutex_unlock(&core_lock);
547 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
548 info.type, info.addr);
549
550 return count;
551}
552
553/*
554 * And of course let the users delete the devices they instantiated, if
555 * they got it wrong. This interface can only be used to delete devices
556 * instantiated by i2c_sysfs_new_device above. This guarantees that we
557 * don't delete devices to which some kernel code still has references.
558 *
559 * Parameter checking may look overzealous, but we really don't want
560 * the user to delete the wrong device.
561 */
562static ssize_t
563i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
565{
566 struct i2c_adapter *adap = to_i2c_adapter(dev);
567 struct i2c_client *client, *next;
568 unsigned short addr;
569 char end;
570 int res;
571
572 /* Parse parameters, reject extra parameters */
573 res = sscanf(buf, "%hi%c", &addr, &end);
574 if (res < 1) {
575 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
576 return -EINVAL;
577 }
578 if (res > 1 && end != '\n') {
579 dev_err(dev, "%s: Extra parameters\n", "delete_device");
580 return -EINVAL;
581 }
582
583 /* Make sure the device was added through sysfs */
584 res = -ENOENT;
585 mutex_lock(&core_lock);
586 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
587 if (client->addr == addr && client->adapter == adap) {
588 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
589 "delete_device", client->name, client->addr);
590
591 list_del(&client->detected);
592 i2c_unregister_device(client);
593 res = count;
594 break;
595 }
596 }
597 mutex_unlock(&core_lock);
598
599 if (res < 0)
600 dev_err(dev, "%s: Can't find device in list\n",
601 "delete_device");
602 return res;
603}
604
Jean Delvare4f8cf822009-09-18 22:45:46 +0200605static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
606static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
607
608static struct attribute *i2c_adapter_attrs[] = {
609 &dev_attr_name.attr,
610 &dev_attr_new_device.attr,
611 &dev_attr_delete_device.attr,
612 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200613};
614
Jean Delvare4f8cf822009-09-18 22:45:46 +0200615static struct attribute_group i2c_adapter_attr_group = {
616 .attrs = i2c_adapter_attrs,
617};
618
619static const struct attribute_group *i2c_adapter_attr_groups[] = {
620 &i2c_adapter_attr_group,
621 NULL
622};
623
624static struct device_type i2c_adapter_type = {
625 .groups = i2c_adapter_attr_groups,
626 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200627};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jean Delvare2bb50952009-09-18 22:45:46 +0200629#ifdef CONFIG_I2C_COMPAT
630static struct class_compat *i2c_adapter_compat_class;
631#endif
632
David Brownell9c1600e2007-05-01 23:26:31 +0200633static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
634{
635 struct i2c_devinfo *devinfo;
636
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200637 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200638 list_for_each_entry(devinfo, &__i2c_board_list, list) {
639 if (devinfo->busnum == adapter->nr
640 && !i2c_new_device(adapter,
641 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100642 dev_err(&adapter->dev,
643 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200644 devinfo->board_info.addr);
645 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200646 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200647}
648
Jean Delvare69b00892009-12-06 17:06:27 +0100649static int i2c_do_add_adapter(struct i2c_driver *driver,
650 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100651{
Jean Delvare4735c982008-07-14 22:38:36 +0200652 /* Detect supported devices on that bus, and instantiate them */
653 i2c_detect(adap, driver);
654
655 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100656 if (driver->attach_adapter) {
657 /* We ignore the return code; if it fails, too bad */
658 driver->attach_adapter(adap);
659 }
660 return 0;
661}
662
Jean Delvare69b00892009-12-06 17:06:27 +0100663static int __process_new_adapter(struct device_driver *d, void *data)
664{
665 return i2c_do_add_adapter(to_i2c_driver(d), data);
666}
667
David Brownell6e13e642007-05-01 23:26:31 +0200668static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Jean Delvare026526f2008-01-27 18:14:49 +0100670 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
David Brownell1d0b19c2008-10-14 17:30:05 +0200672 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200673 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
674 res = -EAGAIN;
675 goto out_list;
676 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200677
Mika Kuoppala194684e2009-12-06 17:06:22 +0100678 rt_mutex_init(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Jean Delvare8fcfef62009-03-28 21:34:43 +0100680 /* Set default timeout to 1 second if not already set */
681 if (adap->timeout == 0)
682 adap->timeout = HZ;
683
Kay Sievers27d9c182009-01-07 14:29:16 +0100684 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200685 adap->dev.bus = &i2c_bus_type;
686 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200687 res = device_register(&adap->dev);
688 if (res)
689 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200691 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
692
Jean Delvare2bb50952009-09-18 22:45:46 +0200693#ifdef CONFIG_I2C_COMPAT
694 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
695 adap->dev.parent);
696 if (res)
697 dev_warn(&adap->dev,
698 "Failed to create compatibility class link\n");
699#endif
700
Jean Delvare729d6dd2009-06-19 16:58:18 +0200701 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200702 if (adap->nr < __i2c_first_dynamic_bus_num)
703 i2c_scan_static_board_info(adap);
704
Jean Delvare4735c982008-07-14 22:38:36 +0200705 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200706 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100707 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100708 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100709 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200710
711 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200712
Jean Delvareb119c6c2006-08-15 18:26:30 +0200713out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200714 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200715 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200716 mutex_unlock(&core_lock);
717 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
David Brownell6e13e642007-05-01 23:26:31 +0200720/**
721 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
722 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200723 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200724 *
725 * This routine is used to declare an I2C adapter when its bus number
726 * doesn't matter. Examples: for I2C adapters dynamically added by
727 * USB links or PCI plugin cards.
728 *
729 * When this returns zero, a new bus number was allocated and stored
730 * in adap->nr, and the specified adapter became available for clients.
731 * Otherwise, a negative errno value is returned.
732 */
733int i2c_add_adapter(struct i2c_adapter *adapter)
734{
735 int id, res = 0;
736
737retry:
738 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
739 return -ENOMEM;
740
Jean Delvarecaada322008-01-27 18:14:49 +0100741 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200742 /* "above" here means "above or equal to", sigh */
743 res = idr_get_new_above(&i2c_adapter_idr, adapter,
744 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100745 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200746
747 if (res < 0) {
748 if (res == -EAGAIN)
749 goto retry;
750 return res;
751 }
752
753 adapter->nr = id;
754 return i2c_register_adapter(adapter);
755}
756EXPORT_SYMBOL(i2c_add_adapter);
757
758/**
759 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
760 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200761 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200762 *
763 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100764 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
765 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200766 * is used to properly configure I2C devices.
767 *
768 * If no devices have pre-been declared for this bus, then be sure to
769 * register the adapter before any dynamically allocated ones. Otherwise
770 * the required bus ID may not be available.
771 *
772 * When this returns zero, the specified adapter became available for
773 * clients using the bus number provided in adap->nr. Also, the table
774 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
775 * and the appropriate driver model device nodes are created. Otherwise, a
776 * negative errno value is returned.
777 */
778int i2c_add_numbered_adapter(struct i2c_adapter *adap)
779{
780 int id;
781 int status;
782
783 if (adap->nr & ~MAX_ID_MASK)
784 return -EINVAL;
785
786retry:
787 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
788 return -ENOMEM;
789
Jean Delvarecaada322008-01-27 18:14:49 +0100790 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200791 /* "above" here means "above or equal to", sigh;
792 * we need the "equal to" result to force the result
793 */
794 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
795 if (status == 0 && id != adap->nr) {
796 status = -EBUSY;
797 idr_remove(&i2c_adapter_idr, id);
798 }
Jean Delvarecaada322008-01-27 18:14:49 +0100799 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200800 if (status == -EAGAIN)
801 goto retry;
802
803 if (status == 0)
804 status = i2c_register_adapter(adap);
805 return status;
806}
807EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
808
Jean Delvare69b00892009-12-06 17:06:27 +0100809static int i2c_do_del_adapter(struct i2c_driver *driver,
810 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100811{
Jean Delvare4735c982008-07-14 22:38:36 +0200812 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100813 int res;
814
Jean Delvareacec2112009-03-28 21:34:40 +0100815 /* Remove the devices we created ourselves as the result of hardware
816 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200817 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
818 if (client->adapter == adapter) {
819 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
820 client->name, client->addr);
821 list_del(&client->detected);
822 i2c_unregister_device(client);
823 }
824 }
825
Jean Delvare026526f2008-01-27 18:14:49 +0100826 if (!driver->detach_adapter)
827 return 0;
828 res = driver->detach_adapter(adapter);
829 if (res)
830 dev_err(&adapter->dev, "detach_adapter failed (%d) "
831 "for driver [%s]\n", res, driver->driver.name);
832 return res;
833}
834
Jean Delvaree549c2b2009-06-19 16:58:19 +0200835static int __unregister_client(struct device *dev, void *dummy)
836{
837 struct i2c_client *client = i2c_verify_client(dev);
838 if (client)
839 i2c_unregister_device(client);
840 return 0;
841}
842
Jean Delvare69b00892009-12-06 17:06:27 +0100843static int __process_removed_adapter(struct device_driver *d, void *data)
844{
845 return i2c_do_del_adapter(to_i2c_driver(d), data);
846}
847
David Brownelld64f73b2007-07-12 14:12:28 +0200848/**
849 * i2c_del_adapter - unregister I2C adapter
850 * @adap: the adapter being unregistered
851 * Context: can sleep
852 *
853 * This unregisters an I2C adapter which was previously registered
854 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
855 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856int i2c_del_adapter(struct i2c_adapter *adap)
857{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200859 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100860 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200863 mutex_lock(&core_lock);
864 found = idr_find(&i2c_adapter_idr, adap->nr);
865 mutex_unlock(&core_lock);
866 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200867 pr_debug("i2c-core: attempting to delete unregistered "
868 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200869 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871
Jean Delvare026526f2008-01-27 18:14:49 +0100872 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200873 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100874 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100875 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200876 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100877 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200878 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100880 /* Remove devices instantiated from sysfs */
881 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
882 if (client->adapter == adap) {
883 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
884 client->name, client->addr);
885 list_del(&client->detected);
886 i2c_unregister_device(client);
887 }
888 }
889
Jean Delvaree549c2b2009-06-19 16:58:19 +0200890 /* Detach any active clients. This can't fail, thus we do not
891 checking the returned value. */
892 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jean Delvare2bb50952009-09-18 22:45:46 +0200894#ifdef CONFIG_I2C_COMPAT
895 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
896 adap->dev.parent);
897#endif
898
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100899 /* device name is gone after device_unregister */
900 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 /* clean up the sysfs representation */
903 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* wait for sysfs to drop all references */
907 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Brownell6e13e642007-05-01 23:26:31 +0200909 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200910 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200912 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200914 /* Clear the device structure in case this adapter is ever going to be
915 added again */
916 memset(&adap->dev, 0, sizeof(adap->dev));
917
Jean Delvare35fc37f2009-06-19 16:58:19 +0200918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919}
David Brownellc0564602007-05-01 23:26:31 +0200920EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922
David Brownell7b4fbc52007-05-01 23:26:30 +0200923/* ------------------------------------------------------------------------- */
924
Jean Delvare69b00892009-12-06 17:06:27 +0100925static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200926{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200927 if (dev->type != &i2c_adapter_type)
928 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100929 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200930}
931
David Brownell7b4fbc52007-05-01 23:26:30 +0200932/*
933 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200934 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
936
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800937int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100939 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
David Brownell1d0b19c2008-10-14 17:30:05 +0200941 /* Can't register until after driver model init */
942 if (unlikely(WARN_ON(!i2c_bus_type.p)))
943 return -EAGAIN;
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800946 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Jean Delvare729d6dd2009-06-19 16:58:18 +0200949 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200950 * will have called probe() for all matching-but-unbound devices.
951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 res = driver_register(&driver->driver);
953 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100954 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100955
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100956 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Jean Delvare4735c982008-07-14 22:38:36 +0200958 INIT_LIST_HEAD(&driver->clients);
959 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200960 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100961 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +0100962 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200963
Jean Delvare7eebcb72006-02-05 23:28:21 +0100964 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800966EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Jean Delvare69b00892009-12-06 17:06:27 +0100968static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200969{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200970 if (dev->type != &i2c_adapter_type)
971 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100972 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200973}
974
David Brownella1d9e6e2007-05-01 23:26:30 +0200975/**
976 * i2c_del_driver - unregister I2C driver
977 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200978 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200979 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200980void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Jean Delvarecaada322008-01-27 18:14:49 +0100982 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100983 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200984 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100987 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
David Brownellc0564602007-05-01 23:26:31 +0200989EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David Brownell7b4fbc52007-05-01 23:26:30 +0200991/* ------------------------------------------------------------------------- */
992
David Brownell9b766b82008-01-27 18:14:51 +0100993static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
David Brownell9b766b82008-01-27 18:14:51 +0100995 struct i2c_client *client = i2c_verify_client(dev);
996 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
David Brownell9b766b82008-01-27 18:14:51 +0100998 if (client && client->addr == addr)
999 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return 0;
1001}
1002
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001003static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
David Brownell9b766b82008-01-27 18:14:51 +01001005 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Jean Delvaree48d3312008-01-27 18:14:48 +01001008/**
1009 * i2c_use_client - increments the reference count of the i2c client structure
1010 * @client: the client being referenced
1011 *
1012 * Each live reference to a client should be refcounted. The driver model does
1013 * that automatically as part of driver binding, so that most drivers don't
1014 * need to do this explicitly: they hold a reference until they're unbound
1015 * from the device.
1016 *
1017 * A pointer to the client with the incremented reference counter is returned.
1018 */
1019struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
David Brownell6ea438e2008-07-14 22:38:24 +02001021 if (client && get_device(&client->dev))
1022 return client;
1023 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
David Brownellc0564602007-05-01 23:26:31 +02001025EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Jean Delvaree48d3312008-01-27 18:14:48 +01001027/**
1028 * i2c_release_client - release a use of the i2c client structure
1029 * @client: the client being no longer referenced
1030 *
1031 * Must be called when a user of a client is finished with it.
1032 */
1033void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
David Brownell6ea438e2008-07-14 22:38:24 +02001035 if (client)
1036 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
David Brownellc0564602007-05-01 23:26:31 +02001038EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
David Brownell9b766b82008-01-27 18:14:51 +01001040struct i2c_cmd_arg {
1041 unsigned cmd;
1042 void *arg;
1043};
1044
1045static int i2c_cmd(struct device *dev, void *_arg)
1046{
1047 struct i2c_client *client = i2c_verify_client(dev);
1048 struct i2c_cmd_arg *arg = _arg;
1049
1050 if (client && client->driver && client->driver->command)
1051 client->driver->command(client, arg->cmd, arg->arg);
1052 return 0;
1053}
1054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1056{
David Brownell9b766b82008-01-27 18:14:51 +01001057 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
David Brownell9b766b82008-01-27 18:14:51 +01001059 cmd_arg.cmd = cmd;
1060 cmd_arg.arg = arg;
1061 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
David Brownellc0564602007-05-01 23:26:31 +02001063EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065static int __init i2c_init(void)
1066{
1067 int retval;
1068
1069 retval = bus_register(&i2c_bus_type);
1070 if (retval)
1071 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001072#ifdef CONFIG_I2C_COMPAT
1073 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1074 if (!i2c_adapter_compat_class) {
1075 retval = -ENOMEM;
1076 goto bus_err;
1077 }
1078#endif
David Brownelle9f13732008-01-27 18:14:52 +01001079 retval = i2c_add_driver(&dummy_driver);
1080 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001081 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001082 return 0;
1083
Jean Delvare2bb50952009-09-18 22:45:46 +02001084class_err:
1085#ifdef CONFIG_I2C_COMPAT
1086 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001087bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001088#endif
David Brownelle9f13732008-01-27 18:14:52 +01001089 bus_unregister(&i2c_bus_type);
1090 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093static void __exit i2c_exit(void)
1094{
David Brownelle9f13732008-01-27 18:14:52 +01001095 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001096#ifdef CONFIG_I2C_COMPAT
1097 class_compat_unregister(i2c_adapter_compat_class);
1098#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 bus_unregister(&i2c_bus_type);
1100}
1101
David Brownella10f9e72008-10-14 17:30:06 +02001102/* We must initialize early, because some subsystems register i2c drivers
1103 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1104 */
1105postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106module_exit(i2c_exit);
1107
1108/* ----------------------------------------------------
1109 * the functional interface to the i2c busses.
1110 * ----------------------------------------------------
1111 */
1112
David Brownella1cdeda2008-07-14 22:38:24 +02001113/**
1114 * i2c_transfer - execute a single or combined I2C message
1115 * @adap: Handle to I2C bus
1116 * @msgs: One or more messages to execute before STOP is issued to
1117 * terminate the operation; each message begins with a START.
1118 * @num: Number of messages to be executed.
1119 *
1120 * Returns negative errno, else the number of messages executed.
1121 *
1122 * Note that there is no requirement that each message be sent to
1123 * the same slave address, although that is the most common model.
1124 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001125int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001127 unsigned long orig_jiffies;
1128 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
David Brownella1cdeda2008-07-14 22:38:24 +02001130 /* REVISIT the fault reporting model here is weak:
1131 *
1132 * - When we get an error after receiving N bytes from a slave,
1133 * there is no way to report "N".
1134 *
1135 * - When we get a NAK after transmitting N bytes to a slave,
1136 * there is no way to report "N" ... or to let the master
1137 * continue executing the rest of this combined message, if
1138 * that's the appropriate response.
1139 *
1140 * - When for example "num" is two and we successfully complete
1141 * the first message but get an error part way through the
1142 * second, it's unclear whether that should be reported as
1143 * one (discarding status on the second message) or errno
1144 * (discarding status on the first one).
1145 */
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (adap->algo->master_xfer) {
1148#ifdef DEBUG
1149 for (ret = 0; ret < num; ret++) {
1150 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001151 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1152 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1153 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155#endif
1156
Mike Rapoportcea443a2008-01-27 18:14:50 +01001157 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001158 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001159 if (!ret)
1160 /* I2C activity is ongoing. */
1161 return -EAGAIN;
1162 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001163 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001164 }
1165
Clifford Wolf66b650f2009-06-15 18:01:46 +02001166 /* Retry automatically on arbitration loss */
1167 orig_jiffies = jiffies;
1168 for (ret = 0, try = 0; try <= adap->retries; try++) {
1169 ret = adap->algo->master_xfer(adap, msgs, num);
1170 if (ret != -EAGAIN)
1171 break;
1172 if (time_after(jiffies, orig_jiffies + adap->timeout))
1173 break;
1174 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001175 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 return ret;
1178 } else {
1179 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001180 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182}
David Brownellc0564602007-05-01 23:26:31 +02001183EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
David Brownella1cdeda2008-07-14 22:38:24 +02001185/**
1186 * i2c_master_send - issue a single I2C message in master transmit mode
1187 * @client: Handle to slave device
1188 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001189 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001190 *
1191 * Returns negative errno, or else the number of bytes written.
1192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1194{
1195 int ret;
1196 struct i2c_adapter *adap=client->adapter;
1197 struct i2c_msg msg;
1198
Jean Delvare815f55f2005-05-07 22:58:46 +02001199 msg.addr = client->addr;
1200 msg.flags = client->flags & I2C_M_TEN;
1201 msg.len = count;
1202 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001203
Jean Delvare815f55f2005-05-07 22:58:46 +02001204 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Jean Delvare815f55f2005-05-07 22:58:46 +02001206 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1207 transmitted, else error code. */
1208 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
David Brownellc0564602007-05-01 23:26:31 +02001210EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
David Brownella1cdeda2008-07-14 22:38:24 +02001212/**
1213 * i2c_master_recv - issue a single I2C message in master receive mode
1214 * @client: Handle to slave device
1215 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001216 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001217 *
1218 * Returns negative errno, or else the number of bytes read.
1219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1221{
1222 struct i2c_adapter *adap=client->adapter;
1223 struct i2c_msg msg;
1224 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Jean Delvare815f55f2005-05-07 22:58:46 +02001226 msg.addr = client->addr;
1227 msg.flags = client->flags & I2C_M_TEN;
1228 msg.flags |= I2C_M_RD;
1229 msg.len = count;
1230 msg.buf = buf;
1231
1232 ret = i2c_transfer(adap, &msg, 1);
1233
1234 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1235 transmitted, else error code. */
1236 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
David Brownellc0564602007-05-01 23:26:31 +02001238EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240/* ----------------------------------------------------
1241 * the i2c address scanning function
1242 * Will not work for 10-bit addresses!
1243 * ----------------------------------------------------
1244 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001245
Jean Delvareccfbbd02009-12-06 17:06:25 +01001246static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001247 struct i2c_driver *driver)
1248{
1249 struct i2c_board_info info;
1250 struct i2c_adapter *adapter = temp_client->adapter;
1251 int addr = temp_client->addr;
1252 int err;
1253
1254 /* Make sure the address is valid */
1255 if (addr < 0x03 || addr > 0x77) {
1256 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1257 addr);
1258 return -EINVAL;
1259 }
1260
1261 /* Skip if already in use */
1262 if (i2c_check_addr(adapter, addr))
1263 return 0;
1264
Jean Delvareccfbbd02009-12-06 17:06:25 +01001265 /* Make sure there is something at this address */
1266 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL) < 0)
1267 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001268
Jean Delvareccfbbd02009-12-06 17:06:25 +01001269 /* Prevent 24RF08 corruption */
1270 if ((addr & ~0x0f) == 0x50)
1271 i2c_smbus_xfer(adapter, addr, 0, 0, 0, I2C_SMBUS_QUICK, NULL);
Jean Delvare4735c982008-07-14 22:38:36 +02001272
1273 /* Finally call the custom detection function */
1274 memset(&info, 0, sizeof(struct i2c_board_info));
1275 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001276 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001277 if (err) {
1278 /* -ENODEV is returned if the detection fails. We catch it
1279 here as this isn't an error. */
1280 return err == -ENODEV ? 0 : err;
1281 }
1282
1283 /* Consistency check */
1284 if (info.type[0] == '\0') {
1285 dev_err(&adapter->dev, "%s detection function provided "
1286 "no name for 0x%x\n", driver->driver.name,
1287 addr);
1288 } else {
1289 struct i2c_client *client;
1290
1291 /* Detection succeeded, instantiate the device */
1292 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1293 info.type, info.addr);
1294 client = i2c_new_device(adapter, &info);
1295 if (client)
1296 list_add_tail(&client->detected, &driver->clients);
1297 else
1298 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1299 info.type, info.addr);
1300 }
1301 return 0;
1302}
1303
1304static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1305{
Jean Delvarec3813d62009-12-14 21:17:25 +01001306 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001307 struct i2c_client *temp_client;
1308 int i, err = 0;
1309 int adap_id = i2c_adapter_id(adapter);
1310
Jean Delvarec3813d62009-12-14 21:17:25 +01001311 address_list = driver->address_list;
1312 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001313 return 0;
1314
1315 /* Set up a temporary client to help detect callback */
1316 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1317 if (!temp_client)
1318 return -ENOMEM;
1319 temp_client->adapter = adapter;
1320
Jean Delvare4329cf82008-08-28 08:33:23 +02001321 /* Stop here if the classes do not match */
1322 if (!(adapter->class & driver->class))
1323 goto exit_free;
1324
Jean Delvare4735c982008-07-14 22:38:36 +02001325 /* Stop here if we can't use SMBUS_QUICK */
1326 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001327 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001328 goto exit_free;
1329
1330 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1331 "can't probe for chips\n");
1332 err = -EOPNOTSUPP;
1333 goto exit_free;
1334 }
1335
Jean Delvarec3813d62009-12-14 21:17:25 +01001336 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001337 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001338 "addr 0x%02x\n", adap_id, address_list[i]);
1339 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001340 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001341 if (err)
1342 goto exit_free;
1343 }
1344
1345 exit_free:
1346 kfree(temp_client);
1347 return err;
1348}
1349
Jean Delvare12b5053a2007-05-01 23:26:31 +02001350struct i2c_client *
1351i2c_new_probed_device(struct i2c_adapter *adap,
1352 struct i2c_board_info *info,
1353 unsigned short const *addr_list)
1354{
1355 int i;
1356
1357 /* Stop here if the bus doesn't support probing */
1358 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1359 dev_err(&adap->dev, "Probing not supported\n");
1360 return NULL;
1361 }
1362
Jean Delvare12b5053a2007-05-01 23:26:31 +02001363 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1364 /* Check address validity */
1365 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1366 dev_warn(&adap->dev, "Invalid 7-bit address "
1367 "0x%02x\n", addr_list[i]);
1368 continue;
1369 }
1370
1371 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001372 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001373 dev_dbg(&adap->dev, "Address 0x%02x already in "
1374 "use, not probing\n", addr_list[i]);
1375 continue;
1376 }
1377
1378 /* Test address responsiveness
1379 The default probe method is a quick write, but it is known
1380 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1381 and could also irreversibly write-protect some EEPROMs, so
1382 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1383 read instead. Also, some bus drivers don't implement
1384 quick write, so we fallback to a byte read it that case
1385 too. */
1386 if ((addr_list[i] & ~0x07) == 0x30
1387 || (addr_list[i] & ~0x0f) == 0x50
1388 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001389 union i2c_smbus_data data;
1390
Jean Delvare12b5053a2007-05-01 23:26:31 +02001391 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1392 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001393 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b5053a2007-05-01 23:26:31 +02001394 break;
1395 } else {
1396 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1397 I2C_SMBUS_WRITE, 0,
1398 I2C_SMBUS_QUICK, NULL) >= 0)
1399 break;
1400 }
1401 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001402
1403 if (addr_list[i] == I2C_CLIENT_END) {
1404 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1405 return NULL;
1406 }
1407
1408 info->addr = addr_list[i];
1409 return i2c_new_device(adap, info);
1410}
1411EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413struct i2c_adapter* i2c_get_adapter(int id)
1414{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001416
Jean Delvarecaada322008-01-27 18:14:49 +01001417 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001418 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001419 if (adapter && !try_module_get(adapter->owner))
1420 adapter = NULL;
1421
Jean Delvarecaada322008-01-27 18:14:49 +01001422 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001423 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
David Brownellc0564602007-05-01 23:26:31 +02001425EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427void i2c_put_adapter(struct i2c_adapter *adap)
1428{
1429 module_put(adap->owner);
1430}
David Brownellc0564602007-05-01 23:26:31 +02001431EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433/* The SMBus parts */
1434
David Brownell438d6c22006-12-10 21:21:31 +01001435#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001436static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001441 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 data = data ^ POLY;
1443 data = data << 1;
1444 }
1445 return (u8)(data >> 8);
1446}
1447
Jean Delvare421ef472005-10-26 21:28:55 +02001448/* Incremental CRC8 over count bytes in the array pointed to by p */
1449static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 int i;
1452
1453 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001454 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return crc;
1456}
1457
Jean Delvare421ef472005-10-26 21:28:55 +02001458/* Assume a 7-bit address, which is reasonable for SMBus */
1459static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Jean Delvare421ef472005-10-26 21:28:55 +02001461 /* The address will be sent first */
1462 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1463 pec = i2c_smbus_pec(pec, &addr, 1);
1464
1465 /* The data buffer follows */
1466 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
Jean Delvare421ef472005-10-26 21:28:55 +02001469/* Used for write only transactions */
1470static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Jean Delvare421ef472005-10-26 21:28:55 +02001472 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1473 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Jean Delvare421ef472005-10-26 21:28:55 +02001476/* Return <0 on CRC error
1477 If there was a write before this read (most cases) we need to take the
1478 partial CRC from the write part into account.
1479 Note that this function does modify the message (we need to decrease the
1480 message length to hide the CRC byte from the caller). */
1481static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Jean Delvare421ef472005-10-26 21:28:55 +02001483 u8 rpec = msg->buf[--msg->len];
1484 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (rpec != cpec) {
1487 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1488 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001489 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
David Brownell438d6c22006-12-10 21:21:31 +01001491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
David Brownella1cdeda2008-07-14 22:38:24 +02001494/**
1495 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1496 * @client: Handle to slave device
1497 *
1498 * This executes the SMBus "receive byte" protocol, returning negative errno
1499 * else the byte received from the device.
1500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501s32 i2c_smbus_read_byte(struct i2c_client *client)
1502{
1503 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001504 int status;
1505
1506 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1507 I2C_SMBUS_READ, 0,
1508 I2C_SMBUS_BYTE, &data);
1509 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
David Brownella1cdeda2008-07-14 22:38:24 +02001513/**
1514 * i2c_smbus_write_byte - SMBus "send byte" protocol
1515 * @client: Handle to slave device
1516 * @value: Byte to be sent
1517 *
1518 * This executes the SMBus "send byte" protocol, returning negative errno
1519 * else zero on success.
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1522{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001524 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
David Brownellc0564602007-05-01 23:26:31 +02001526EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
David Brownella1cdeda2008-07-14 22:38:24 +02001528/**
1529 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1530 * @client: Handle to slave device
1531 * @command: Byte interpreted by slave
1532 *
1533 * This executes the SMBus "read byte" protocol, returning negative errno
1534 * else a data byte received from the device.
1535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1537{
1538 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001539 int status;
1540
1541 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1542 I2C_SMBUS_READ, command,
1543 I2C_SMBUS_BYTE_DATA, &data);
1544 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
David Brownellc0564602007-05-01 23:26:31 +02001546EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
David Brownella1cdeda2008-07-14 22:38:24 +02001548/**
1549 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1550 * @client: Handle to slave device
1551 * @command: Byte interpreted by slave
1552 * @value: Byte being written
1553 *
1554 * This executes the SMBus "write byte" protocol, returning negative errno
1555 * else zero on success.
1556 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1558{
1559 union i2c_smbus_data data;
1560 data.byte = value;
1561 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1562 I2C_SMBUS_WRITE,command,
1563 I2C_SMBUS_BYTE_DATA,&data);
1564}
David Brownellc0564602007-05-01 23:26:31 +02001565EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
David Brownella1cdeda2008-07-14 22:38:24 +02001567/**
1568 * i2c_smbus_read_word_data - SMBus "read word" protocol
1569 * @client: Handle to slave device
1570 * @command: Byte interpreted by slave
1571 *
1572 * This executes the SMBus "read word" protocol, returning negative errno
1573 * else a 16-bit unsigned "word" received from the device.
1574 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1576{
1577 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001578 int status;
1579
1580 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1581 I2C_SMBUS_READ, command,
1582 I2C_SMBUS_WORD_DATA, &data);
1583 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
David Brownellc0564602007-05-01 23:26:31 +02001585EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
David Brownella1cdeda2008-07-14 22:38:24 +02001587/**
1588 * i2c_smbus_write_word_data - SMBus "write word" protocol
1589 * @client: Handle to slave device
1590 * @command: Byte interpreted by slave
1591 * @value: 16-bit "word" being written
1592 *
1593 * This executes the SMBus "write word" protocol, returning negative errno
1594 * else zero on success.
1595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1597{
1598 union i2c_smbus_data data;
1599 data.word = value;
1600 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1601 I2C_SMBUS_WRITE,command,
1602 I2C_SMBUS_WORD_DATA,&data);
1603}
David Brownellc0564602007-05-01 23:26:31 +02001604EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
David Brownella64ec072007-10-13 23:56:31 +02001606/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001607 * i2c_smbus_process_call - SMBus "process call" protocol
1608 * @client: Handle to slave device
1609 * @command: Byte interpreted by slave
1610 * @value: 16-bit "word" being written
1611 *
1612 * This executes the SMBus "process call" protocol, returning negative errno
1613 * else a 16-bit unsigned "word" received from the device.
1614 */
1615s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1616{
1617 union i2c_smbus_data data;
1618 int status;
1619 data.word = value;
1620
1621 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1622 I2C_SMBUS_WRITE, command,
1623 I2C_SMBUS_PROC_CALL, &data);
1624 return (status < 0) ? status : data.word;
1625}
1626EXPORT_SYMBOL(i2c_smbus_process_call);
1627
1628/**
David Brownella1cdeda2008-07-14 22:38:24 +02001629 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001630 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001631 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001632 * @values: Byte array into which data will be read; big enough to hold
1633 * the data returned by the slave. SMBus allows at most 32 bytes.
1634 *
David Brownella1cdeda2008-07-14 22:38:24 +02001635 * This executes the SMBus "block read" protocol, returning negative errno
1636 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001637 *
1638 * Note that using this function requires that the client's adapter support
1639 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1640 * support this; its emulation through I2C messaging relies on a specific
1641 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1642 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001643s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1644 u8 *values)
1645{
1646 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001647 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001648
David Brownell24a5bb72008-07-14 22:38:23 +02001649 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1650 I2C_SMBUS_READ, command,
1651 I2C_SMBUS_BLOCK_DATA, &data);
1652 if (status)
1653 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001654
1655 memcpy(values, &data.block[1], data.block[0]);
1656 return data.block[0];
1657}
1658EXPORT_SYMBOL(i2c_smbus_read_block_data);
1659
David Brownella1cdeda2008-07-14 22:38:24 +02001660/**
1661 * i2c_smbus_write_block_data - SMBus "block write" protocol
1662 * @client: Handle to slave device
1663 * @command: Byte interpreted by slave
1664 * @length: Size of data block; SMBus allows at most 32 bytes
1665 * @values: Byte array which will be written.
1666 *
1667 * This executes the SMBus "block write" protocol, returning negative errno
1668 * else zero on success.
1669 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001671 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (length > I2C_SMBUS_BLOCK_MAX)
1676 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001678 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1680 I2C_SMBUS_WRITE,command,
1681 I2C_SMBUS_BLOCK_DATA,&data);
1682}
David Brownellc0564602007-05-01 23:26:31 +02001683EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001686s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1687 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
1689 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001690 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001691
Jean Delvare4b2643d2007-07-12 14:12:29 +02001692 if (length > I2C_SMBUS_BLOCK_MAX)
1693 length = I2C_SMBUS_BLOCK_MAX;
1694 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001695 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1696 I2C_SMBUS_READ, command,
1697 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1698 if (status < 0)
1699 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001700
1701 memcpy(values, &data.block[1], data.block[0]);
1702 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
David Brownellc0564602007-05-01 23:26:31 +02001704EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Jean Delvare21bbd692006-01-09 15:19:18 +11001706s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001707 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001708{
1709 union i2c_smbus_data data;
1710
1711 if (length > I2C_SMBUS_BLOCK_MAX)
1712 length = I2C_SMBUS_BLOCK_MAX;
1713 data.block[0] = length;
1714 memcpy(data.block + 1, values, length);
1715 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1716 I2C_SMBUS_WRITE, command,
1717 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1718}
David Brownellc0564602007-05-01 23:26:31 +02001719EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001720
David Brownell438d6c22006-12-10 21:21:31 +01001721/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001723static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001725 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 union i2c_smbus_data * data)
1727{
1728 /* So we need to generate a series of msgs. In the case of writing, we
1729 need to use only one message; when reading, we need two. We initialize
1730 most things with sane defaults, to keep the code below somewhat
1731 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001732 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1733 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001735 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1737 };
1738 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001739 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001740 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 msgbuf0[0] = command;
1743 switch(size) {
1744 case I2C_SMBUS_QUICK:
1745 msg[0].len = 0;
1746 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001747 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1748 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 num = 1;
1750 break;
1751 case I2C_SMBUS_BYTE:
1752 if (read_write == I2C_SMBUS_READ) {
1753 /* Special case: only a read! */
1754 msg[0].flags = I2C_M_RD | flags;
1755 num = 1;
1756 }
1757 break;
1758 case I2C_SMBUS_BYTE_DATA:
1759 if (read_write == I2C_SMBUS_READ)
1760 msg[1].len = 1;
1761 else {
1762 msg[0].len = 2;
1763 msgbuf0[1] = data->byte;
1764 }
1765 break;
1766 case I2C_SMBUS_WORD_DATA:
1767 if (read_write == I2C_SMBUS_READ)
1768 msg[1].len = 2;
1769 else {
1770 msg[0].len=3;
1771 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001772 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774 break;
1775 case I2C_SMBUS_PROC_CALL:
1776 num = 2; /* Special case */
1777 read_write = I2C_SMBUS_READ;
1778 msg[0].len = 3;
1779 msg[1].len = 2;
1780 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001781 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001785 msg[1].flags |= I2C_M_RECV_LEN;
1786 msg[1].len = 1; /* block length will be added by
1787 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 } else {
1789 msg[0].len = data->block[0] + 2;
1790 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001791 dev_err(&adapter->dev,
1792 "Invalid block write size %d\n",
1793 data->block[0]);
1794 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001796 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 msgbuf0[i] = data->block[i-1];
1798 }
1799 break;
1800 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001801 num = 2; /* Another special case */
1802 read_write = I2C_SMBUS_READ;
1803 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001804 dev_err(&adapter->dev,
1805 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001806 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001807 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001808 }
1809 msg[0].len = data->block[0] + 2;
1810 for (i = 1; i < msg[0].len; i++)
1811 msgbuf0[i] = data->block[i-1];
1812 msg[1].flags |= I2C_M_RECV_LEN;
1813 msg[1].len = 1; /* block length will be added by
1814 the underlying bus driver */
1815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 case I2C_SMBUS_I2C_BLOCK_DATA:
1817 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001818 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 } else {
1820 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001821 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001822 dev_err(&adapter->dev,
1823 "Invalid block write size %d\n",
1824 data->block[0]);
1825 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827 for (i = 1; i <= data->block[0]; i++)
1828 msgbuf0[i] = data->block[i];
1829 }
1830 break;
1831 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001832 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1833 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835
Jean Delvare421ef472005-10-26 21:28:55 +02001836 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1837 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1838 if (i) {
1839 /* Compute PEC if first message is a write */
1840 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001841 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001842 i2c_smbus_add_pec(&msg[0]);
1843 else /* Write followed by read */
1844 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1845 }
1846 /* Ask for PEC if last message is a read */
1847 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001848 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001849 }
1850
David Brownell24a5bb72008-07-14 22:38:23 +02001851 status = i2c_transfer(adapter, msg, num);
1852 if (status < 0)
1853 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jean Delvare421ef472005-10-26 21:28:55 +02001855 /* Check PEC if last message is a read */
1856 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001857 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1858 if (status < 0)
1859 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001860 }
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (read_write == I2C_SMBUS_READ)
1863 switch(size) {
1864 case I2C_SMBUS_BYTE:
1865 data->byte = msgbuf0[0];
1866 break;
1867 case I2C_SMBUS_BYTE_DATA:
1868 data->byte = msgbuf1[0];
1869 break;
David Brownell438d6c22006-12-10 21:21:31 +01001870 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 case I2C_SMBUS_PROC_CALL:
1872 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1873 break;
1874 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001875 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 data->block[i+1] = msgbuf1[i];
1877 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001878 case I2C_SMBUS_BLOCK_DATA:
1879 case I2C_SMBUS_BLOCK_PROC_CALL:
1880 for (i = 0; i < msgbuf1[0] + 1; i++)
1881 data->block[i] = msgbuf1[i];
1882 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884 return 0;
1885}
1886
David Brownella1cdeda2008-07-14 22:38:24 +02001887/**
1888 * i2c_smbus_xfer - execute SMBus protocol operations
1889 * @adapter: Handle to I2C bus
1890 * @addr: Address of SMBus slave on that bus
1891 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1892 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1893 * @command: Byte interpreted by slave, for protocols which use such bytes
1894 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1895 * @data: Data to be read or written
1896 *
1897 * This executes an SMBus protocol operation, and returns a negative
1898 * errno code else zero on success.
1899 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001900s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001901 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001902 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001904 unsigned long orig_jiffies;
1905 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001911 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001912
1913 /* Retry automatically on arbitration loss */
1914 orig_jiffies = jiffies;
1915 for (res = 0, try = 0; try <= adapter->retries; try++) {
1916 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1917 read_write, command,
1918 protocol, data);
1919 if (res != -EAGAIN)
1920 break;
1921 if (time_after(jiffies,
1922 orig_jiffies + adapter->timeout))
1923 break;
1924 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001925 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 } else
1927 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001928 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return res;
1931}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1935MODULE_DESCRIPTION("I2C-Bus main module");
1936MODULE_LICENSE("GPL");