blob: c2258a51fe0ce3bb09055220c645602ab467c876 [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 Delvare6629dcf2010-05-04 11:09:28 +020043/* core_lock protects i2c_adapter_idr, 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);
48
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));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200119 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100120 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200121 i2c_set_clientdata(client, NULL);
122 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100123 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static int i2c_device_remove(struct device *dev)
127{
Jean Delvare51298d12009-09-18 22:45:45 +0200128 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200129 struct i2c_driver *driver;
130 int status;
131
Jean Delvare51298d12009-09-18 22:45:45 +0200132 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200133 return 0;
134
135 driver = to_i2c_driver(dev->driver);
136 if (driver->remove) {
137 dev_dbg(dev, "remove\n");
138 status = driver->remove(client);
139 } else {
140 dev->driver = NULL;
141 status = 0;
142 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200143 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200144 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200145 i2c_set_clientdata(client, NULL);
146 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200147 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
David Brownellf37dd802007-02-13 22:09:00 +0100150static void i2c_device_shutdown(struct device *dev)
151{
Jean Delvare51298d12009-09-18 22:45:45 +0200152 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100153 struct i2c_driver *driver;
154
Jean Delvare51298d12009-09-18 22:45:45 +0200155 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100156 return;
157 driver = to_i2c_driver(dev->driver);
158 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200159 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100160}
161
sonic zhang54067ee2009-12-14 21:17:30 +0100162#ifdef CONFIG_SUSPEND
163static int i2c_device_pm_suspend(struct device *dev)
164{
165 const struct dev_pm_ops *pm;
166
167 if (!dev->driver)
168 return 0;
169 pm = dev->driver->pm;
170 if (!pm || !pm->suspend)
171 return 0;
172 return pm->suspend(dev);
173}
174
175static int i2c_device_pm_resume(struct device *dev)
176{
177 const struct dev_pm_ops *pm;
178
179 if (!dev->driver)
180 return 0;
181 pm = dev->driver->pm;
182 if (!pm || !pm->resume)
183 return 0;
184 return pm->resume(dev);
185}
186#else
187#define i2c_device_pm_suspend NULL
188#define i2c_device_pm_resume NULL
189#endif
190
Mark Brown6de468a2010-03-02 12:23:46 +0100191#ifdef CONFIG_PM_RUNTIME
192static int i2c_device_runtime_suspend(struct device *dev)
193{
194 const struct dev_pm_ops *pm;
195
196 if (!dev->driver)
197 return 0;
198 pm = dev->driver->pm;
199 if (!pm || !pm->runtime_suspend)
200 return 0;
201 return pm->runtime_suspend(dev);
202}
203
204static int i2c_device_runtime_resume(struct device *dev)
205{
206 const struct dev_pm_ops *pm;
207
208 if (!dev->driver)
209 return 0;
210 pm = dev->driver->pm;
211 if (!pm || !pm->runtime_resume)
212 return 0;
213 return pm->runtime_resume(dev);
214}
215
216static int i2c_device_runtime_idle(struct device *dev)
217{
218 const struct dev_pm_ops *pm = NULL;
219 int ret;
220
221 if (dev->driver)
222 pm = dev->driver->pm;
223 if (pm && pm->runtime_idle) {
224 ret = pm->runtime_idle(dev);
225 if (ret)
226 return ret;
227 }
228
229 return pm_runtime_suspend(dev);
230}
231#else
232#define i2c_device_runtime_suspend NULL
233#define i2c_device_runtime_resume NULL
234#define i2c_device_runtime_idle NULL
235#endif
236
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100237static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100238{
Jean Delvare51298d12009-09-18 22:45:45 +0200239 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100240 struct i2c_driver *driver;
241
Jean Delvare51298d12009-09-18 22:45:45 +0200242 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100243 return 0;
244 driver = to_i2c_driver(dev->driver);
245 if (!driver->suspend)
246 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200247 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100248}
249
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100250static int i2c_device_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100251{
Jean Delvare51298d12009-09-18 22:45:45 +0200252 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100253 struct i2c_driver *driver;
254
Jean Delvare51298d12009-09-18 22:45:45 +0200255 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100256 return 0;
257 driver = to_i2c_driver(dev->driver);
258 if (!driver->resume)
259 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200260 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100261}
262
David Brownell9c1600e2007-05-01 23:26:31 +0200263static void i2c_client_dev_release(struct device *dev)
264{
265 kfree(to_i2c_client(dev));
266}
267
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100268static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200269show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200270{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200271 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
272 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200273}
274
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100275static ssize_t
276show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200277{
278 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200279 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200280}
281
Jean Delvare4f8cf822009-09-18 22:45:46 +0200282static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200283static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
284
285static struct attribute *i2c_dev_attrs[] = {
286 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200287 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200288 &dev_attr_modalias.attr,
289 NULL
290};
291
292static struct attribute_group i2c_dev_attr_group = {
293 .attrs = i2c_dev_attrs,
294};
295
296static const struct attribute_group *i2c_dev_attr_groups[] = {
297 &i2c_dev_attr_group,
298 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200299};
300
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100301static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100302 .suspend = i2c_device_pm_suspend,
303 .resume = i2c_device_pm_resume,
Mark Brown6de468a2010-03-02 12:23:46 +0100304 .runtime_suspend = i2c_device_runtime_suspend,
305 .runtime_resume = i2c_device_runtime_resume,
306 .runtime_idle = i2c_device_runtime_idle,
sonic zhang54067ee2009-12-14 21:17:30 +0100307};
308
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200309struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100310 .name = "i2c",
311 .match = i2c_device_match,
312 .probe = i2c_device_probe,
313 .remove = i2c_device_remove,
314 .shutdown = i2c_device_shutdown,
315 .suspend = i2c_device_suspend,
316 .resume = i2c_device_resume,
sonic zhang54067ee2009-12-14 21:17:30 +0100317 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000318};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200319EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000320
Jean Delvare51298d12009-09-18 22:45:45 +0200321static struct device_type i2c_client_type = {
322 .groups = i2c_dev_attr_groups,
323 .uevent = i2c_device_uevent,
324 .release = i2c_client_dev_release,
325};
326
David Brownell9b766b82008-01-27 18:14:51 +0100327
328/**
329 * i2c_verify_client - return parameter as i2c_client, or NULL
330 * @dev: device, probably from some driver model iterator
331 *
332 * When traversing the driver model tree, perhaps using driver model
333 * iterators like @device_for_each_child(), you can't assume very much
334 * about the nodes you find. Use this function to avoid oopses caused
335 * by wrongly treating some non-I2C device as an i2c_client.
336 */
337struct i2c_client *i2c_verify_client(struct device *dev)
338{
Jean Delvare51298d12009-09-18 22:45:45 +0200339 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100340 ? to_i2c_client(dev)
341 : NULL;
342}
343EXPORT_SYMBOL(i2c_verify_client);
344
345
David Brownell9c1600e2007-05-01 23:26:31 +0200346/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200347 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200348 * @adap: the adapter managing the device
349 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200350 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200351 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200352 * Create an i2c device. Binding is handled through driver model
353 * probe()/remove() methods. A driver may be bound to this device when we
354 * return from this function, or any later moment (e.g. maybe hotplugging will
355 * load the driver module). This call is not appropriate for use by mainboard
356 * initialization logic, which usually runs during an arch_initcall() long
357 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200358 *
359 * This returns the new i2c client, which may be saved for later use with
360 * i2c_unregister_device(); or NULL to indicate an error.
361 */
362struct i2c_client *
363i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
364{
365 struct i2c_client *client;
366 int status;
367
368 client = kzalloc(sizeof *client, GFP_KERNEL);
369 if (!client)
370 return NULL;
371
372 client->adapter = adap;
373
374 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200375
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200376 if (info->archdata)
377 client->dev.archdata = *info->archdata;
378
Marc Pignatee354252008-08-28 08:33:22 +0200379 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200380 client->addr = info->addr;
381 client->irq = info->irq;
382
David Brownell9c1600e2007-05-01 23:26:31 +0200383 strlcpy(client->name, info->type, sizeof(client->name));
384
Jean Delvaref8a227e2009-06-19 16:58:18 +0200385 /* Check for address business */
386 status = i2c_check_addr(adap, client->addr);
387 if (status)
388 goto out_err;
389
390 client->dev.parent = &client->adapter->dev;
391 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200392 client->dev.type = &i2c_client_type;
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 Brownellef2c8322007-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 */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200544 i2c_lock_adapter(adap);
545 list_add_tail(&client->detected, &adap->userspace_clients);
546 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200547 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;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200585 i2c_lock_adapter(adap);
586 list_for_each_entry_safe(client, next, &adap->userspace_clients,
587 detected) {
588 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200589 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
590 "delete_device", client->name, client->addr);
591
592 list_del(&client->detected);
593 i2c_unregister_device(client);
594 res = count;
595 break;
596 }
597 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200598 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200599
600 if (res < 0)
601 dev_err(dev, "%s: Can't find device in list\n",
602 "delete_device");
603 return res;
604}
605
Jean Delvare4f8cf822009-09-18 22:45:46 +0200606static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
607static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
608
609static struct attribute *i2c_adapter_attrs[] = {
610 &dev_attr_name.attr,
611 &dev_attr_new_device.attr,
612 &dev_attr_delete_device.attr,
613 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200614};
615
Jean Delvare4f8cf822009-09-18 22:45:46 +0200616static struct attribute_group i2c_adapter_attr_group = {
617 .attrs = i2c_adapter_attrs,
618};
619
620static const struct attribute_group *i2c_adapter_attr_groups[] = {
621 &i2c_adapter_attr_group,
622 NULL
623};
624
625static struct device_type i2c_adapter_type = {
626 .groups = i2c_adapter_attr_groups,
627 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200628};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Jean Delvare2bb50952009-09-18 22:45:46 +0200630#ifdef CONFIG_I2C_COMPAT
631static struct class_compat *i2c_adapter_compat_class;
632#endif
633
David Brownell9c1600e2007-05-01 23:26:31 +0200634static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
635{
636 struct i2c_devinfo *devinfo;
637
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200638 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200639 list_for_each_entry(devinfo, &__i2c_board_list, list) {
640 if (devinfo->busnum == adapter->nr
641 && !i2c_new_device(adapter,
642 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100643 dev_err(&adapter->dev,
644 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200645 devinfo->board_info.addr);
646 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200647 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200648}
649
Jean Delvare69b00892009-12-06 17:06:27 +0100650static int i2c_do_add_adapter(struct i2c_driver *driver,
651 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100652{
Jean Delvare4735c982008-07-14 22:38:36 +0200653 /* Detect supported devices on that bus, and instantiate them */
654 i2c_detect(adap, driver);
655
656 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100657 if (driver->attach_adapter) {
658 /* We ignore the return code; if it fails, too bad */
659 driver->attach_adapter(adap);
660 }
661 return 0;
662}
663
Jean Delvare69b00892009-12-06 17:06:27 +0100664static int __process_new_adapter(struct device_driver *d, void *data)
665{
666 return i2c_do_add_adapter(to_i2c_driver(d), data);
667}
668
David Brownell6e13e642007-05-01 23:26:31 +0200669static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Jean Delvare026526f2008-01-27 18:14:49 +0100671 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David Brownell1d0b19c2008-10-14 17:30:05 +0200673 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200674 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
675 res = -EAGAIN;
676 goto out_list;
677 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200678
Mika Kuoppala194684e2009-12-06 17:06:22 +0100679 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200680 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Jean Delvare8fcfef62009-03-28 21:34:43 +0100682 /* Set default timeout to 1 second if not already set */
683 if (adap->timeout == 0)
684 adap->timeout = HZ;
685
Kay Sievers27d9c182009-01-07 14:29:16 +0100686 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200687 adap->dev.bus = &i2c_bus_type;
688 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200689 res = device_register(&adap->dev);
690 if (res)
691 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200693 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
694
Jean Delvare2bb50952009-09-18 22:45:46 +0200695#ifdef CONFIG_I2C_COMPAT
696 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
697 adap->dev.parent);
698 if (res)
699 dev_warn(&adap->dev,
700 "Failed to create compatibility class link\n");
701#endif
702
Jean Delvare729d6dd2009-06-19 16:58:18 +0200703 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200704 if (adap->nr < __i2c_first_dynamic_bus_num)
705 i2c_scan_static_board_info(adap);
706
Jean Delvare4735c982008-07-14 22:38:36 +0200707 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200708 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100709 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100710 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100711 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200712
713 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200714
Jean Delvareb119c6c2006-08-15 18:26:30 +0200715out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200716 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200717 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200718 mutex_unlock(&core_lock);
719 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
David Brownell6e13e642007-05-01 23:26:31 +0200722/**
723 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
724 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200725 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200726 *
727 * This routine is used to declare an I2C adapter when its bus number
728 * doesn't matter. Examples: for I2C adapters dynamically added by
729 * USB links or PCI plugin cards.
730 *
731 * When this returns zero, a new bus number was allocated and stored
732 * in adap->nr, and the specified adapter became available for clients.
733 * Otherwise, a negative errno value is returned.
734 */
735int i2c_add_adapter(struct i2c_adapter *adapter)
736{
737 int id, res = 0;
738
739retry:
740 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
741 return -ENOMEM;
742
Jean Delvarecaada322008-01-27 18:14:49 +0100743 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200744 /* "above" here means "above or equal to", sigh */
745 res = idr_get_new_above(&i2c_adapter_idr, adapter,
746 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100747 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200748
749 if (res < 0) {
750 if (res == -EAGAIN)
751 goto retry;
752 return res;
753 }
754
755 adapter->nr = id;
756 return i2c_register_adapter(adapter);
757}
758EXPORT_SYMBOL(i2c_add_adapter);
759
760/**
761 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
762 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200763 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200764 *
765 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100766 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
767 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200768 * is used to properly configure I2C devices.
769 *
770 * If no devices have pre-been declared for this bus, then be sure to
771 * register the adapter before any dynamically allocated ones. Otherwise
772 * the required bus ID may not be available.
773 *
774 * When this returns zero, the specified adapter became available for
775 * clients using the bus number provided in adap->nr. Also, the table
776 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
777 * and the appropriate driver model device nodes are created. Otherwise, a
778 * negative errno value is returned.
779 */
780int i2c_add_numbered_adapter(struct i2c_adapter *adap)
781{
782 int id;
783 int status;
784
785 if (adap->nr & ~MAX_ID_MASK)
786 return -EINVAL;
787
788retry:
789 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
790 return -ENOMEM;
791
Jean Delvarecaada322008-01-27 18:14:49 +0100792 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200793 /* "above" here means "above or equal to", sigh;
794 * we need the "equal to" result to force the result
795 */
796 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
797 if (status == 0 && id != adap->nr) {
798 status = -EBUSY;
799 idr_remove(&i2c_adapter_idr, id);
800 }
Jean Delvarecaada322008-01-27 18:14:49 +0100801 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200802 if (status == -EAGAIN)
803 goto retry;
804
805 if (status == 0)
806 status = i2c_register_adapter(adap);
807 return status;
808}
809EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
810
Jean Delvare69b00892009-12-06 17:06:27 +0100811static int i2c_do_del_adapter(struct i2c_driver *driver,
812 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100813{
Jean Delvare4735c982008-07-14 22:38:36 +0200814 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100815 int res;
816
Jean Delvareacec2112009-03-28 21:34:40 +0100817 /* Remove the devices we created ourselves as the result of hardware
818 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200819 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
820 if (client->adapter == adapter) {
821 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
822 client->name, client->addr);
823 list_del(&client->detected);
824 i2c_unregister_device(client);
825 }
826 }
827
Jean Delvare026526f2008-01-27 18:14:49 +0100828 if (!driver->detach_adapter)
829 return 0;
830 res = driver->detach_adapter(adapter);
831 if (res)
832 dev_err(&adapter->dev, "detach_adapter failed (%d) "
833 "for driver [%s]\n", res, driver->driver.name);
834 return res;
835}
836
Jean Delvaree549c2b2009-06-19 16:58:19 +0200837static int __unregister_client(struct device *dev, void *dummy)
838{
839 struct i2c_client *client = i2c_verify_client(dev);
840 if (client)
841 i2c_unregister_device(client);
842 return 0;
843}
844
Jean Delvare69b00892009-12-06 17:06:27 +0100845static int __process_removed_adapter(struct device_driver *d, void *data)
846{
847 return i2c_do_del_adapter(to_i2c_driver(d), data);
848}
849
David Brownelld64f73b2007-07-12 14:12:28 +0200850/**
851 * i2c_del_adapter - unregister I2C adapter
852 * @adap: the adapter being unregistered
853 * Context: can sleep
854 *
855 * This unregisters an I2C adapter which was previously registered
856 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858int i2c_del_adapter(struct i2c_adapter *adap)
859{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200861 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100862 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200865 mutex_lock(&core_lock);
866 found = idr_find(&i2c_adapter_idr, adap->nr);
867 mutex_unlock(&core_lock);
868 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200869 pr_debug("i2c-core: attempting to delete unregistered "
870 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200871 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
Jean Delvare026526f2008-01-27 18:14:49 +0100874 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200875 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100876 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100877 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200878 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100879 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200880 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100882 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200883 i2c_lock_adapter(adap);
884 list_for_each_entry_safe(client, next, &adap->userspace_clients,
885 detected) {
886 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
887 client->addr);
888 list_del(&client->detected);
889 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100890 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200891 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100892
Jean Delvaree549c2b2009-06-19 16:58:19 +0200893 /* Detach any active clients. This can't fail, thus we do not
894 checking the returned value. */
895 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Jean Delvare2bb50952009-09-18 22:45:46 +0200897#ifdef CONFIG_I2C_COMPAT
898 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
899 adap->dev.parent);
900#endif
901
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100902 /* device name is gone after device_unregister */
903 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 /* clean up the sysfs representation */
906 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* wait for sysfs to drop all references */
910 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
David Brownell6e13e642007-05-01 23:26:31 +0200912 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200913 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200915 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Jean Delvarebd4bc3d2008-07-16 19:30:05 +0200917 /* Clear the device structure in case this adapter is ever going to be
918 added again */
919 memset(&adap->dev, 0, sizeof(adap->dev));
920
Jean Delvare35fc37f2009-06-19 16:58:19 +0200921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
David Brownellc0564602007-05-01 23:26:31 +0200923EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925
David Brownell7b4fbc52007-05-01 23:26:30 +0200926/* ------------------------------------------------------------------------- */
927
Jean Delvare69b00892009-12-06 17:06:27 +0100928static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200929{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200930 if (dev->type != &i2c_adapter_type)
931 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100932 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200933}
934
David Brownell7b4fbc52007-05-01 23:26:30 +0200935/*
936 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200937 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800940int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100942 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
David Brownell1d0b19c2008-10-14 17:30:05 +0200944 /* Can't register until after driver model init */
945 if (unlikely(WARN_ON(!i2c_bus_type.p)))
946 return -EAGAIN;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800949 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Jean Delvare729d6dd2009-06-19 16:58:18 +0200952 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200953 * will have called probe() for all matching-but-unbound devices.
954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 res = driver_register(&driver->driver);
956 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100957 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100958
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100959 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Jean Delvare4735c982008-07-14 22:38:36 +0200961 INIT_LIST_HEAD(&driver->clients);
962 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200963 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100964 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +0100965 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200966
Jean Delvare7eebcb72006-02-05 23:28:21 +0100967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800969EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Jean Delvare69b00892009-12-06 17:06:27 +0100971static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200972{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200973 if (dev->type != &i2c_adapter_type)
974 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100975 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200976}
977
David Brownella1d9e6e2007-05-01 23:26:30 +0200978/**
979 * i2c_del_driver - unregister I2C driver
980 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +0200981 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +0200982 */
Jean Delvareb3e82092007-05-01 23:26:32 +0200983void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jean Delvarecaada322008-01-27 18:14:49 +0100985 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100986 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200987 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +0200988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100990 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
David Brownellc0564602007-05-01 23:26:31 +0200992EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
David Brownell7b4fbc52007-05-01 23:26:30 +0200994/* ------------------------------------------------------------------------- */
995
David Brownell9b766b82008-01-27 18:14:51 +0100996static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
David Brownell9b766b82008-01-27 18:14:51 +0100998 struct i2c_client *client = i2c_verify_client(dev);
999 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
David Brownell9b766b82008-01-27 18:14:51 +01001001 if (client && client->addr == addr)
1002 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return 0;
1004}
1005
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001006static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
David Brownell9b766b82008-01-27 18:14:51 +01001008 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Jean Delvaree48d3312008-01-27 18:14:48 +01001011/**
1012 * i2c_use_client - increments the reference count of the i2c client structure
1013 * @client: the client being referenced
1014 *
1015 * Each live reference to a client should be refcounted. The driver model does
1016 * that automatically as part of driver binding, so that most drivers don't
1017 * need to do this explicitly: they hold a reference until they're unbound
1018 * from the device.
1019 *
1020 * A pointer to the client with the incremented reference counter is returned.
1021 */
1022struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
David Brownell6ea438e2008-07-14 22:38:24 +02001024 if (client && get_device(&client->dev))
1025 return client;
1026 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
David Brownellc0564602007-05-01 23:26:31 +02001028EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Jean Delvaree48d3312008-01-27 18:14:48 +01001030/**
1031 * i2c_release_client - release a use of the i2c client structure
1032 * @client: the client being no longer referenced
1033 *
1034 * Must be called when a user of a client is finished with it.
1035 */
1036void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
David Brownell6ea438e2008-07-14 22:38:24 +02001038 if (client)
1039 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
David Brownellc0564602007-05-01 23:26:31 +02001041EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
David Brownell9b766b82008-01-27 18:14:51 +01001043struct i2c_cmd_arg {
1044 unsigned cmd;
1045 void *arg;
1046};
1047
1048static int i2c_cmd(struct device *dev, void *_arg)
1049{
1050 struct i2c_client *client = i2c_verify_client(dev);
1051 struct i2c_cmd_arg *arg = _arg;
1052
1053 if (client && client->driver && client->driver->command)
1054 client->driver->command(client, arg->cmd, arg->arg);
1055 return 0;
1056}
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1059{
David Brownell9b766b82008-01-27 18:14:51 +01001060 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
David Brownell9b766b82008-01-27 18:14:51 +01001062 cmd_arg.cmd = cmd;
1063 cmd_arg.arg = arg;
1064 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
David Brownellc0564602007-05-01 23:26:31 +02001066EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068static int __init i2c_init(void)
1069{
1070 int retval;
1071
1072 retval = bus_register(&i2c_bus_type);
1073 if (retval)
1074 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001075#ifdef CONFIG_I2C_COMPAT
1076 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1077 if (!i2c_adapter_compat_class) {
1078 retval = -ENOMEM;
1079 goto bus_err;
1080 }
1081#endif
David Brownelle9f13732008-01-27 18:14:52 +01001082 retval = i2c_add_driver(&dummy_driver);
1083 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001084 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001085 return 0;
1086
Jean Delvare2bb50952009-09-18 22:45:46 +02001087class_err:
1088#ifdef CONFIG_I2C_COMPAT
1089 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001090bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001091#endif
David Brownelle9f13732008-01-27 18:14:52 +01001092 bus_unregister(&i2c_bus_type);
1093 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096static void __exit i2c_exit(void)
1097{
David Brownelle9f13732008-01-27 18:14:52 +01001098 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001099#ifdef CONFIG_I2C_COMPAT
1100 class_compat_unregister(i2c_adapter_compat_class);
1101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 bus_unregister(&i2c_bus_type);
1103}
1104
David Brownella10f9e72008-10-14 17:30:06 +02001105/* We must initialize early, because some subsystems register i2c drivers
1106 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1107 */
1108postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109module_exit(i2c_exit);
1110
1111/* ----------------------------------------------------
1112 * the functional interface to the i2c busses.
1113 * ----------------------------------------------------
1114 */
1115
David Brownella1cdeda2008-07-14 22:38:24 +02001116/**
1117 * i2c_transfer - execute a single or combined I2C message
1118 * @adap: Handle to I2C bus
1119 * @msgs: One or more messages to execute before STOP is issued to
1120 * terminate the operation; each message begins with a START.
1121 * @num: Number of messages to be executed.
1122 *
1123 * Returns negative errno, else the number of messages executed.
1124 *
1125 * Note that there is no requirement that each message be sent to
1126 * the same slave address, although that is the most common model.
1127 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001128int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001130 unsigned long orig_jiffies;
1131 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David Brownella1cdeda2008-07-14 22:38:24 +02001133 /* REVISIT the fault reporting model here is weak:
1134 *
1135 * - When we get an error after receiving N bytes from a slave,
1136 * there is no way to report "N".
1137 *
1138 * - When we get a NAK after transmitting N bytes to a slave,
1139 * there is no way to report "N" ... or to let the master
1140 * continue executing the rest of this combined message, if
1141 * that's the appropriate response.
1142 *
1143 * - When for example "num" is two and we successfully complete
1144 * the first message but get an error part way through the
1145 * second, it's unclear whether that should be reported as
1146 * one (discarding status on the second message) or errno
1147 * (discarding status on the first one).
1148 */
1149
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (adap->algo->master_xfer) {
1151#ifdef DEBUG
1152 for (ret = 0; ret < num; ret++) {
1153 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001154 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1155 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1156 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158#endif
1159
Mike Rapoportcea443a2008-01-27 18:14:50 +01001160 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001161 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001162 if (!ret)
1163 /* I2C activity is ongoing. */
1164 return -EAGAIN;
1165 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001166 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001167 }
1168
Clifford Wolf66b650f2009-06-15 18:01:46 +02001169 /* Retry automatically on arbitration loss */
1170 orig_jiffies = jiffies;
1171 for (ret = 0, try = 0; try <= adap->retries; try++) {
1172 ret = adap->algo->master_xfer(adap, msgs, num);
1173 if (ret != -EAGAIN)
1174 break;
1175 if (time_after(jiffies, orig_jiffies + adap->timeout))
1176 break;
1177 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001178 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 return ret;
1181 } else {
1182 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001183 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
1185}
David Brownellc0564602007-05-01 23:26:31 +02001186EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
David Brownella1cdeda2008-07-14 22:38:24 +02001188/**
1189 * i2c_master_send - issue a single I2C message in master transmit mode
1190 * @client: Handle to slave device
1191 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001192 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001193 *
1194 * Returns negative errno, or else the number of bytes written.
1195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1197{
1198 int ret;
1199 struct i2c_adapter *adap=client->adapter;
1200 struct i2c_msg msg;
1201
Jean Delvare815f55f2005-05-07 22:58:46 +02001202 msg.addr = client->addr;
1203 msg.flags = client->flags & I2C_M_TEN;
1204 msg.len = count;
1205 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001206
Jean Delvare815f55f2005-05-07 22:58:46 +02001207 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jean Delvare815f55f2005-05-07 22:58:46 +02001209 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1210 transmitted, else error code. */
1211 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
David Brownellc0564602007-05-01 23:26:31 +02001213EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
David Brownella1cdeda2008-07-14 22:38:24 +02001215/**
1216 * i2c_master_recv - issue a single I2C message in master receive mode
1217 * @client: Handle to slave device
1218 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001219 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001220 *
1221 * Returns negative errno, or else the number of bytes read.
1222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1224{
1225 struct i2c_adapter *adap=client->adapter;
1226 struct i2c_msg msg;
1227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Jean Delvare815f55f2005-05-07 22:58:46 +02001229 msg.addr = client->addr;
1230 msg.flags = client->flags & I2C_M_TEN;
1231 msg.flags |= I2C_M_RD;
1232 msg.len = count;
1233 msg.buf = buf;
1234
1235 ret = i2c_transfer(adap, &msg, 1);
1236
1237 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1238 transmitted, else error code. */
1239 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
David Brownellc0564602007-05-01 23:26:31 +02001241EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/* ----------------------------------------------------
1244 * the i2c address scanning function
1245 * Will not work for 10-bit addresses!
1246 * ----------------------------------------------------
1247 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001248
Jean Delvareccfbbd02009-12-06 17:06:25 +01001249static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001250 struct i2c_driver *driver)
1251{
1252 struct i2c_board_info info;
1253 struct i2c_adapter *adapter = temp_client->adapter;
1254 int addr = temp_client->addr;
1255 int err;
1256
1257 /* Make sure the address is valid */
1258 if (addr < 0x03 || addr > 0x77) {
1259 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1260 addr);
1261 return -EINVAL;
1262 }
1263
1264 /* Skip if already in use */
1265 if (i2c_check_addr(adapter, addr))
1266 return 0;
1267
Jean Delvareccfbbd02009-12-06 17:06:25 +01001268 /* Make sure there is something at this address */
Jean Delvareb1d4b392010-05-04 11:09:28 +02001269 if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
1270 /* Special probe for FSC hwmon chips */
1271 union i2c_smbus_data dummy;
Jean Delvare4735c982008-07-14 22:38:36 +02001272
Jean Delvareb1d4b392010-05-04 11:09:28 +02001273 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
1274 I2C_SMBUS_BYTE_DATA, &dummy) < 0)
1275 return 0;
1276 } else {
1277 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1278 I2C_SMBUS_QUICK, NULL) < 0)
1279 return 0;
1280
1281 /* Prevent 24RF08 corruption */
1282 if ((addr & ~0x0f) == 0x50)
1283 i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1284 I2C_SMBUS_QUICK, NULL);
1285 }
Jean Delvare4735c982008-07-14 22:38:36 +02001286
1287 /* Finally call the custom detection function */
1288 memset(&info, 0, sizeof(struct i2c_board_info));
1289 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001290 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001291 if (err) {
1292 /* -ENODEV is returned if the detection fails. We catch it
1293 here as this isn't an error. */
1294 return err == -ENODEV ? 0 : err;
1295 }
1296
1297 /* Consistency check */
1298 if (info.type[0] == '\0') {
1299 dev_err(&adapter->dev, "%s detection function provided "
1300 "no name for 0x%x\n", driver->driver.name,
1301 addr);
1302 } else {
1303 struct i2c_client *client;
1304
1305 /* Detection succeeded, instantiate the device */
1306 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1307 info.type, info.addr);
1308 client = i2c_new_device(adapter, &info);
1309 if (client)
1310 list_add_tail(&client->detected, &driver->clients);
1311 else
1312 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1313 info.type, info.addr);
1314 }
1315 return 0;
1316}
1317
1318static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1319{
Jean Delvarec3813d62009-12-14 21:17:25 +01001320 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001321 struct i2c_client *temp_client;
1322 int i, err = 0;
1323 int adap_id = i2c_adapter_id(adapter);
1324
Jean Delvarec3813d62009-12-14 21:17:25 +01001325 address_list = driver->address_list;
1326 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001327 return 0;
1328
1329 /* Set up a temporary client to help detect callback */
1330 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1331 if (!temp_client)
1332 return -ENOMEM;
1333 temp_client->adapter = adapter;
1334
Jean Delvare4329cf82008-08-28 08:33:23 +02001335 /* Stop here if the classes do not match */
1336 if (!(adapter->class & driver->class))
1337 goto exit_free;
1338
Jean Delvare4735c982008-07-14 22:38:36 +02001339 /* Stop here if we can't use SMBUS_QUICK */
1340 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001341 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001342 goto exit_free;
1343
1344 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1345 "can't probe for chips\n");
1346 err = -EOPNOTSUPP;
1347 goto exit_free;
1348 }
1349
Jean Delvarec3813d62009-12-14 21:17:25 +01001350 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001351 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001352 "addr 0x%02x\n", adap_id, address_list[i]);
1353 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001354 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001355 if (err)
1356 goto exit_free;
1357 }
1358
1359 exit_free:
1360 kfree(temp_client);
1361 return err;
1362}
1363
Jean Delvare12b50532007-05-01 23:26:31 +02001364struct i2c_client *
1365i2c_new_probed_device(struct i2c_adapter *adap,
1366 struct i2c_board_info *info,
1367 unsigned short const *addr_list)
1368{
1369 int i;
1370
1371 /* Stop here if the bus doesn't support probing */
1372 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1373 dev_err(&adap->dev, "Probing not supported\n");
1374 return NULL;
1375 }
1376
Jean Delvare12b50532007-05-01 23:26:31 +02001377 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1378 /* Check address validity */
1379 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1380 dev_warn(&adap->dev, "Invalid 7-bit address "
1381 "0x%02x\n", addr_list[i]);
1382 continue;
1383 }
1384
1385 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001386 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b50532007-05-01 23:26:31 +02001387 dev_dbg(&adap->dev, "Address 0x%02x already in "
1388 "use, not probing\n", addr_list[i]);
1389 continue;
1390 }
1391
1392 /* Test address responsiveness
1393 The default probe method is a quick write, but it is known
1394 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1395 and could also irreversibly write-protect some EEPROMs, so
1396 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1397 read instead. Also, some bus drivers don't implement
1398 quick write, so we fallback to a byte read it that case
1399 too. */
1400 if ((addr_list[i] & ~0x07) == 0x30
1401 || (addr_list[i] & ~0x0f) == 0x50
1402 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
Hans Verkuilb25b7912008-08-10 22:56:15 +02001403 union i2c_smbus_data data;
1404
Jean Delvare12b50532007-05-01 23:26:31 +02001405 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1406 I2C_SMBUS_READ, 0,
Hans Verkuilb25b7912008-08-10 22:56:15 +02001407 I2C_SMBUS_BYTE, &data) >= 0)
Jean Delvare12b50532007-05-01 23:26:31 +02001408 break;
1409 } else {
1410 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1411 I2C_SMBUS_WRITE, 0,
1412 I2C_SMBUS_QUICK, NULL) >= 0)
1413 break;
1414 }
1415 }
Jean Delvare12b50532007-05-01 23:26:31 +02001416
1417 if (addr_list[i] == I2C_CLIENT_END) {
1418 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1419 return NULL;
1420 }
1421
1422 info->addr = addr_list[i];
1423 return i2c_new_device(adap, info);
1424}
1425EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427struct i2c_adapter* i2c_get_adapter(int id)
1428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001430
Jean Delvarecaada322008-01-27 18:14:49 +01001431 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001432 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001433 if (adapter && !try_module_get(adapter->owner))
1434 adapter = NULL;
1435
Jean Delvarecaada322008-01-27 18:14:49 +01001436 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001437 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438}
David Brownellc0564602007-05-01 23:26:31 +02001439EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441void i2c_put_adapter(struct i2c_adapter *adap)
1442{
1443 module_put(adap->owner);
1444}
David Brownellc0564602007-05-01 23:26:31 +02001445EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447/* The SMBus parts */
1448
David Brownell438d6c22006-12-10 21:21:31 +01001449#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001450static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
1452 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 for(i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001455 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 data = data ^ POLY;
1457 data = data << 1;
1458 }
1459 return (u8)(data >> 8);
1460}
1461
Jean Delvare421ef472005-10-26 21:28:55 +02001462/* Incremental CRC8 over count bytes in the array pointed to by p */
1463static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 int i;
1466
1467 for(i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001468 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return crc;
1470}
1471
Jean Delvare421ef472005-10-26 21:28:55 +02001472/* Assume a 7-bit address, which is reasonable for SMBus */
1473static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jean Delvare421ef472005-10-26 21:28:55 +02001475 /* The address will be sent first */
1476 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1477 pec = i2c_smbus_pec(pec, &addr, 1);
1478
1479 /* The data buffer follows */
1480 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Jean Delvare421ef472005-10-26 21:28:55 +02001483/* Used for write only transactions */
1484static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Jean Delvare421ef472005-10-26 21:28:55 +02001486 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1487 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Jean Delvare421ef472005-10-26 21:28:55 +02001490/* Return <0 on CRC error
1491 If there was a write before this read (most cases) we need to take the
1492 partial CRC from the write part into account.
1493 Note that this function does modify the message (we need to decrease the
1494 message length to hide the CRC byte from the caller). */
1495static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Jean Delvare421ef472005-10-26 21:28:55 +02001497 u8 rpec = msg->buf[--msg->len];
1498 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (rpec != cpec) {
1501 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1502 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001503 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
David Brownell438d6c22006-12-10 21:21:31 +01001505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
David Brownella1cdeda2008-07-14 22:38:24 +02001508/**
1509 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1510 * @client: Handle to slave device
1511 *
1512 * This executes the SMBus "receive byte" protocol, returning negative errno
1513 * else the byte received from the device.
1514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515s32 i2c_smbus_read_byte(struct i2c_client *client)
1516{
1517 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001518 int status;
1519
1520 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1521 I2C_SMBUS_READ, 0,
1522 I2C_SMBUS_BYTE, &data);
1523 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
David Brownellc0564602007-05-01 23:26:31 +02001525EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
David Brownella1cdeda2008-07-14 22:38:24 +02001527/**
1528 * i2c_smbus_write_byte - SMBus "send byte" protocol
1529 * @client: Handle to slave device
1530 * @value: Byte to be sent
1531 *
1532 * This executes the SMBus "send byte" protocol, returning negative errno
1533 * else zero on success.
1534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001538 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
David Brownellc0564602007-05-01 23:26:31 +02001540EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
David Brownella1cdeda2008-07-14 22:38:24 +02001542/**
1543 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1544 * @client: Handle to slave device
1545 * @command: Byte interpreted by slave
1546 *
1547 * This executes the SMBus "read byte" protocol, returning negative errno
1548 * else a data byte received from the device.
1549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1551{
1552 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001553 int status;
1554
1555 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1556 I2C_SMBUS_READ, command,
1557 I2C_SMBUS_BYTE_DATA, &data);
1558 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
David Brownellc0564602007-05-01 23:26:31 +02001560EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
David Brownella1cdeda2008-07-14 22:38:24 +02001562/**
1563 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1564 * @client: Handle to slave device
1565 * @command: Byte interpreted by slave
1566 * @value: Byte being written
1567 *
1568 * This executes the SMBus "write byte" protocol, returning negative errno
1569 * else zero on success.
1570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1572{
1573 union i2c_smbus_data data;
1574 data.byte = value;
1575 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1576 I2C_SMBUS_WRITE,command,
1577 I2C_SMBUS_BYTE_DATA,&data);
1578}
David Brownellc0564602007-05-01 23:26:31 +02001579EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
David Brownella1cdeda2008-07-14 22:38:24 +02001581/**
1582 * i2c_smbus_read_word_data - SMBus "read word" protocol
1583 * @client: Handle to slave device
1584 * @command: Byte interpreted by slave
1585 *
1586 * This executes the SMBus "read word" protocol, returning negative errno
1587 * else a 16-bit unsigned "word" received from the device.
1588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1590{
1591 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001592 int status;
1593
1594 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1595 I2C_SMBUS_READ, command,
1596 I2C_SMBUS_WORD_DATA, &data);
1597 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
David Brownellc0564602007-05-01 23:26:31 +02001599EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
David Brownella1cdeda2008-07-14 22:38:24 +02001601/**
1602 * i2c_smbus_write_word_data - SMBus "write word" protocol
1603 * @client: Handle to slave device
1604 * @command: Byte interpreted by slave
1605 * @value: 16-bit "word" being written
1606 *
1607 * This executes the SMBus "write word" protocol, returning negative errno
1608 * else zero on success.
1609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1611{
1612 union i2c_smbus_data data;
1613 data.word = value;
1614 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1615 I2C_SMBUS_WRITE,command,
1616 I2C_SMBUS_WORD_DATA,&data);
1617}
David Brownellc0564602007-05-01 23:26:31 +02001618EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
David Brownella64ec072007-10-13 23:56:31 +02001620/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001621 * i2c_smbus_process_call - SMBus "process call" protocol
1622 * @client: Handle to slave device
1623 * @command: Byte interpreted by slave
1624 * @value: 16-bit "word" being written
1625 *
1626 * This executes the SMBus "process call" protocol, returning negative errno
1627 * else a 16-bit unsigned "word" received from the device.
1628 */
1629s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1630{
1631 union i2c_smbus_data data;
1632 int status;
1633 data.word = value;
1634
1635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1636 I2C_SMBUS_WRITE, command,
1637 I2C_SMBUS_PROC_CALL, &data);
1638 return (status < 0) ? status : data.word;
1639}
1640EXPORT_SYMBOL(i2c_smbus_process_call);
1641
1642/**
David Brownella1cdeda2008-07-14 22:38:24 +02001643 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001644 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001645 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001646 * @values: Byte array into which data will be read; big enough to hold
1647 * the data returned by the slave. SMBus allows at most 32 bytes.
1648 *
David Brownella1cdeda2008-07-14 22:38:24 +02001649 * This executes the SMBus "block read" protocol, returning negative errno
1650 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001651 *
1652 * Note that using this function requires that the client's adapter support
1653 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1654 * support this; its emulation through I2C messaging relies on a specific
1655 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1656 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001657s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1658 u8 *values)
1659{
1660 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001661 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001662
David Brownell24a5bb72008-07-14 22:38:23 +02001663 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1664 I2C_SMBUS_READ, command,
1665 I2C_SMBUS_BLOCK_DATA, &data);
1666 if (status)
1667 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001668
1669 memcpy(values, &data.block[1], data.block[0]);
1670 return data.block[0];
1671}
1672EXPORT_SYMBOL(i2c_smbus_read_block_data);
1673
David Brownella1cdeda2008-07-14 22:38:24 +02001674/**
1675 * i2c_smbus_write_block_data - SMBus "block write" protocol
1676 * @client: Handle to slave device
1677 * @command: Byte interpreted by slave
1678 * @length: Size of data block; SMBus allows at most 32 bytes
1679 * @values: Byte array which will be written.
1680 *
1681 * This executes the SMBus "block write" protocol, returning negative errno
1682 * else zero on success.
1683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001685 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (length > I2C_SMBUS_BLOCK_MAX)
1690 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001692 memcpy(&data.block[1], values, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1694 I2C_SMBUS_WRITE,command,
1695 I2C_SMBUS_BLOCK_DATA,&data);
1696}
David Brownellc0564602007-05-01 23:26:31 +02001697EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001700s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1701 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
1703 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001704 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001705
Jean Delvare4b2643d2007-07-12 14:12:29 +02001706 if (length > I2C_SMBUS_BLOCK_MAX)
1707 length = I2C_SMBUS_BLOCK_MAX;
1708 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001709 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1710 I2C_SMBUS_READ, command,
1711 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1712 if (status < 0)
1713 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001714
1715 memcpy(values, &data.block[1], data.block[0]);
1716 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
David Brownellc0564602007-05-01 23:26:31 +02001718EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jean Delvare21bbd692006-01-09 15:19:18 +11001720s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001721 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001722{
1723 union i2c_smbus_data data;
1724
1725 if (length > I2C_SMBUS_BLOCK_MAX)
1726 length = I2C_SMBUS_BLOCK_MAX;
1727 data.block[0] = length;
1728 memcpy(data.block + 1, values, length);
1729 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1730 I2C_SMBUS_WRITE, command,
1731 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1732}
David Brownellc0564602007-05-01 23:26:31 +02001733EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001734
David Brownell438d6c22006-12-10 21:21:31 +01001735/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 No checking of parameters is done! */
David Brownell438d6c22006-12-10 21:21:31 +01001737static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 unsigned short flags,
David Brownell438d6c22006-12-10 21:21:31 +01001739 char read_write, u8 command, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 union i2c_smbus_data * data)
1741{
1742 /* So we need to generate a series of msgs. In the case of writing, we
1743 need to use only one message; when reading, we need two. We initialize
1744 most things with sane defaults, to keep the code below somewhat
1745 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001746 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1747 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 int num = read_write == I2C_SMBUS_READ?2:1;
David Brownell438d6c22006-12-10 21:21:31 +01001749 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1751 };
1752 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001753 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001754 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 msgbuf0[0] = command;
1757 switch(size) {
1758 case I2C_SMBUS_QUICK:
1759 msg[0].len = 0;
1760 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001761 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1762 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 num = 1;
1764 break;
1765 case I2C_SMBUS_BYTE:
1766 if (read_write == I2C_SMBUS_READ) {
1767 /* Special case: only a read! */
1768 msg[0].flags = I2C_M_RD | flags;
1769 num = 1;
1770 }
1771 break;
1772 case I2C_SMBUS_BYTE_DATA:
1773 if (read_write == I2C_SMBUS_READ)
1774 msg[1].len = 1;
1775 else {
1776 msg[0].len = 2;
1777 msgbuf0[1] = data->byte;
1778 }
1779 break;
1780 case I2C_SMBUS_WORD_DATA:
1781 if (read_write == I2C_SMBUS_READ)
1782 msg[1].len = 2;
1783 else {
1784 msg[0].len=3;
1785 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001786 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 }
1788 break;
1789 case I2C_SMBUS_PROC_CALL:
1790 num = 2; /* Special case */
1791 read_write = I2C_SMBUS_READ;
1792 msg[0].len = 3;
1793 msg[1].len = 2;
1794 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001795 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 break;
1797 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001799 msg[1].flags |= I2C_M_RECV_LEN;
1800 msg[1].len = 1; /* block length will be added by
1801 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 } else {
1803 msg[0].len = data->block[0] + 2;
1804 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02001805 dev_err(&adapter->dev,
1806 "Invalid block write size %d\n",
1807 data->block[0]);
1808 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001810 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 msgbuf0[i] = data->block[i-1];
1812 }
1813 break;
1814 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001815 num = 2; /* Another special case */
1816 read_write = I2C_SMBUS_READ;
1817 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001818 dev_err(&adapter->dev,
1819 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001820 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001821 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001822 }
1823 msg[0].len = data->block[0] + 2;
1824 for (i = 1; i < msg[0].len; i++)
1825 msgbuf0[i] = data->block[i-1];
1826 msg[1].flags |= I2C_M_RECV_LEN;
1827 msg[1].len = 1; /* block length will be added by
1828 the underlying bus driver */
1829 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 case I2C_SMBUS_I2C_BLOCK_DATA:
1831 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001832 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 } else {
1834 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001835 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001836 dev_err(&adapter->dev,
1837 "Invalid block write size %d\n",
1838 data->block[0]);
1839 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841 for (i = 1; i <= data->block[0]; i++)
1842 msgbuf0[i] = data->block[i];
1843 }
1844 break;
1845 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001846 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1847 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 }
1849
Jean Delvare421ef472005-10-26 21:28:55 +02001850 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1851 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1852 if (i) {
1853 /* Compute PEC if first message is a write */
1854 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001855 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001856 i2c_smbus_add_pec(&msg[0]);
1857 else /* Write followed by read */
1858 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1859 }
1860 /* Ask for PEC if last message is a read */
1861 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001862 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001863 }
1864
David Brownell24a5bb72008-07-14 22:38:23 +02001865 status = i2c_transfer(adapter, msg, num);
1866 if (status < 0)
1867 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Jean Delvare421ef472005-10-26 21:28:55 +02001869 /* Check PEC if last message is a read */
1870 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001871 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1872 if (status < 0)
1873 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001874 }
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (read_write == I2C_SMBUS_READ)
1877 switch(size) {
1878 case I2C_SMBUS_BYTE:
1879 data->byte = msgbuf0[0];
1880 break;
1881 case I2C_SMBUS_BYTE_DATA:
1882 data->byte = msgbuf1[0];
1883 break;
David Brownell438d6c22006-12-10 21:21:31 +01001884 case I2C_SMBUS_WORD_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 case I2C_SMBUS_PROC_CALL:
1886 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1887 break;
1888 case I2C_SMBUS_I2C_BLOCK_DATA:
Jean Delvare4b2643d2007-07-12 14:12:29 +02001889 for (i = 0; i < data->block[0]; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 data->block[i+1] = msgbuf1[i];
1891 break;
Jean Delvare209d27c2007-05-01 23:26:29 +02001892 case I2C_SMBUS_BLOCK_DATA:
1893 case I2C_SMBUS_BLOCK_PROC_CALL:
1894 for (i = 0; i < msgbuf1[0] + 1; i++)
1895 data->block[i] = msgbuf1[i];
1896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898 return 0;
1899}
1900
David Brownella1cdeda2008-07-14 22:38:24 +02001901/**
1902 * i2c_smbus_xfer - execute SMBus protocol operations
1903 * @adapter: Handle to I2C bus
1904 * @addr: Address of SMBus slave on that bus
1905 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1906 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1907 * @command: Byte interpreted by slave, for protocols which use such bytes
1908 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1909 * @data: Data to be read or written
1910 *
1911 * This executes an SMBus protocol operation, and returns a negative
1912 * errno code else zero on success.
1913 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001914s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001915 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001916 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001918 unsigned long orig_jiffies;
1919 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001925 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001926
1927 /* Retry automatically on arbitration loss */
1928 orig_jiffies = jiffies;
1929 for (res = 0, try = 0; try <= adapter->retries; try++) {
1930 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1931 read_write, command,
1932 protocol, data);
1933 if (res != -EAGAIN)
1934 break;
1935 if (time_after(jiffies,
1936 orig_jiffies + adapter->timeout))
1937 break;
1938 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001939 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 } else
1941 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001942 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return res;
1945}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1949MODULE_DESCRIPTION("I2C-Bus main module");
1950MODULE_LICENSE("GPL");