blob: f744f73e3ff574072789d9d1912a6a6d2dd130f4 [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
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200162#ifdef CONFIG_PM_SLEEP
163static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100164{
Jean Delvare51298d12009-09-18 22:45:45 +0200165 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100166 struct i2c_driver *driver;
167
Jean Delvare51298d12009-09-18 22:45:45 +0200168 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100169 return 0;
170 driver = to_i2c_driver(dev->driver);
171 if (!driver->suspend)
172 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200173 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100174}
175
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200176static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100177{
Jean Delvare51298d12009-09-18 22:45:45 +0200178 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100179 struct i2c_driver *driver;
180
Jean Delvare51298d12009-09-18 22:45:45 +0200181 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100182 return 0;
183 driver = to_i2c_driver(dev->driver);
184 if (!driver->resume)
185 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200186 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100187}
188
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200189static int i2c_device_pm_suspend(struct device *dev)
190{
191 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
192
193 if (pm_runtime_suspended(dev))
194 return 0;
195
196 if (pm)
197 return pm->suspend ? pm->suspend(dev) : 0;
198
199 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
200}
201
202static int i2c_device_pm_resume(struct device *dev)
203{
204 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
205 int ret;
206
207 if (pm)
208 ret = pm->resume ? pm->resume(dev) : 0;
209 else
210 ret = i2c_legacy_resume(dev);
211
212 if (!ret) {
213 pm_runtime_disable(dev);
214 pm_runtime_set_active(dev);
215 pm_runtime_enable(dev);
216 }
217
218 return ret;
219}
220
221static int i2c_device_pm_freeze(struct device *dev)
222{
223 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
224
225 if (pm_runtime_suspended(dev))
226 return 0;
227
228 if (pm)
229 return pm->freeze ? pm->freeze(dev) : 0;
230
231 return i2c_legacy_suspend(dev, PMSG_FREEZE);
232}
233
234static int i2c_device_pm_thaw(struct device *dev)
235{
236 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
237
238 if (pm_runtime_suspended(dev))
239 return 0;
240
241 if (pm)
242 return pm->thaw ? pm->thaw(dev) : 0;
243
244 return i2c_legacy_resume(dev);
245}
246
247static int i2c_device_pm_poweroff(struct device *dev)
248{
249 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
250
251 if (pm_runtime_suspended(dev))
252 return 0;
253
254 if (pm)
255 return pm->poweroff ? pm->poweroff(dev) : 0;
256
257 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
258}
259
260static int i2c_device_pm_restore(struct device *dev)
261{
262 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
263 int ret;
264
265 if (pm)
266 ret = pm->restore ? pm->restore(dev) : 0;
267 else
268 ret = i2c_legacy_resume(dev);
269
270 if (!ret) {
271 pm_runtime_disable(dev);
272 pm_runtime_set_active(dev);
273 pm_runtime_enable(dev);
274 }
275
276 return ret;
277}
278#else /* !CONFIG_PM_SLEEP */
279#define i2c_device_pm_suspend NULL
280#define i2c_device_pm_resume NULL
281#define i2c_device_pm_freeze NULL
282#define i2c_device_pm_thaw NULL
283#define i2c_device_pm_poweroff NULL
284#define i2c_device_pm_restore NULL
285#endif /* !CONFIG_PM_SLEEP */
286
David Brownell9c1600e2007-05-01 23:26:31 +0200287static void i2c_client_dev_release(struct device *dev)
288{
289 kfree(to_i2c_client(dev));
290}
291
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100292static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200293show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200294{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200295 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
296 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200297}
298
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100299static ssize_t
300show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200301{
302 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200303 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200304}
305
Jean Delvare4f8cf822009-09-18 22:45:46 +0200306static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200307static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
308
309static struct attribute *i2c_dev_attrs[] = {
310 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200311 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200312 &dev_attr_modalias.attr,
313 NULL
314};
315
316static struct attribute_group i2c_dev_attr_group = {
317 .attrs = i2c_dev_attrs,
318};
319
320static const struct attribute_group *i2c_dev_attr_groups[] = {
321 &i2c_dev_attr_group,
322 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200323};
324
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100325static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100326 .suspend = i2c_device_pm_suspend,
327 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200328 .freeze = i2c_device_pm_freeze,
329 .thaw = i2c_device_pm_thaw,
330 .poweroff = i2c_device_pm_poweroff,
331 .restore = i2c_device_pm_restore,
332 SET_RUNTIME_PM_OPS(
333 pm_generic_runtime_suspend,
334 pm_generic_runtime_resume,
335 pm_generic_runtime_idle
336 )
sonic zhang54067ee2009-12-14 21:17:30 +0100337};
338
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200339struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100340 .name = "i2c",
341 .match = i2c_device_match,
342 .probe = i2c_device_probe,
343 .remove = i2c_device_remove,
344 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100345 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000346};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200347EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000348
Jean Delvare51298d12009-09-18 22:45:45 +0200349static struct device_type i2c_client_type = {
350 .groups = i2c_dev_attr_groups,
351 .uevent = i2c_device_uevent,
352 .release = i2c_client_dev_release,
353};
354
David Brownell9b766b82008-01-27 18:14:51 +0100355
356/**
357 * i2c_verify_client - return parameter as i2c_client, or NULL
358 * @dev: device, probably from some driver model iterator
359 *
360 * When traversing the driver model tree, perhaps using driver model
361 * iterators like @device_for_each_child(), you can't assume very much
362 * about the nodes you find. Use this function to avoid oopses caused
363 * by wrongly treating some non-I2C device as an i2c_client.
364 */
365struct i2c_client *i2c_verify_client(struct device *dev)
366{
Jean Delvare51298d12009-09-18 22:45:45 +0200367 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100368 ? to_i2c_client(dev)
369 : NULL;
370}
371EXPORT_SYMBOL(i2c_verify_client);
372
373
David Brownell9c1600e2007-05-01 23:26:31 +0200374/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200375 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200376 * @adap: the adapter managing the device
377 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200378 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200379 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200380 * Create an i2c device. Binding is handled through driver model
381 * probe()/remove() methods. A driver may be bound to this device when we
382 * return from this function, or any later moment (e.g. maybe hotplugging will
383 * load the driver module). This call is not appropriate for use by mainboard
384 * initialization logic, which usually runs during an arch_initcall() long
385 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200386 *
387 * This returns the new i2c client, which may be saved for later use with
388 * i2c_unregister_device(); or NULL to indicate an error.
389 */
390struct i2c_client *
391i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
392{
393 struct i2c_client *client;
394 int status;
395
396 client = kzalloc(sizeof *client, GFP_KERNEL);
397 if (!client)
398 return NULL;
399
400 client->adapter = adap;
401
402 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200403
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200404 if (info->archdata)
405 client->dev.archdata = *info->archdata;
406
Marc Pignatee354252008-08-28 08:33:22 +0200407 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200408 client->addr = info->addr;
409 client->irq = info->irq;
410
David Brownell9c1600e2007-05-01 23:26:31 +0200411 strlcpy(client->name, info->type, sizeof(client->name));
412
Jean Delvaref8a227e2009-06-19 16:58:18 +0200413 /* Check for address business */
414 status = i2c_check_addr(adap, client->addr);
415 if (status)
416 goto out_err;
417
418 client->dev.parent = &client->adapter->dev;
419 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200420 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700421#ifdef CONFIG_OF
422 client->dev.of_node = info->of_node;
423#endif
Jean Delvaref8a227e2009-06-19 16:58:18 +0200424
425 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
426 client->addr);
427 status = device_register(&client->dev);
428 if (status)
429 goto out_err;
430
Jean Delvaref8a227e2009-06-19 16:58:18 +0200431 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
432 client->name, dev_name(&client->dev));
433
David Brownell9c1600e2007-05-01 23:26:31 +0200434 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200435
436out_err:
437 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
438 "(%d)\n", client->name, client->addr, status);
439 kfree(client);
440 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200441}
442EXPORT_SYMBOL_GPL(i2c_new_device);
443
444
445/**
446 * i2c_unregister_device - reverse effect of i2c_new_device()
447 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200448 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200449 */
450void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200451{
David Brownella1d9e6e2007-05-01 23:26:30 +0200452 device_unregister(&client->dev);
453}
David Brownell9c1600e2007-05-01 23:26:31 +0200454EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200455
456
Jean Delvare60b129d2008-05-11 20:37:06 +0200457static const struct i2c_device_id dummy_id[] = {
458 { "dummy", 0 },
459 { },
460};
461
Jean Delvared2653e92008-04-29 23:11:39 +0200462static int dummy_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
464{
465 return 0;
466}
467
468static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100469{
470 return 0;
471}
472
473static struct i2c_driver dummy_driver = {
474 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200475 .probe = dummy_probe,
476 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200477 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100478};
479
480/**
481 * i2c_new_dummy - return a new i2c device bound to a dummy driver
482 * @adapter: the adapter managing the device
483 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100484 * Context: can sleep
485 *
486 * This returns an I2C client bound to the "dummy" driver, intended for use
487 * with devices that consume multiple addresses. Examples of such chips
488 * include various EEPROMS (like 24c04 and 24c08 models).
489 *
490 * These dummy devices have two main uses. First, most I2C and SMBus calls
491 * except i2c_transfer() need a client handle; the dummy will be that handle.
492 * And second, this prevents the specified address from being bound to a
493 * different driver.
494 *
495 * This returns the new i2c client, which should be saved for later use with
496 * i2c_unregister_device(); or NULL to indicate an error.
497 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100498struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100499{
500 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200501 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100502 };
503
David Brownelle9f13732008-01-27 18:14:52 +0100504 return i2c_new_device(adapter, &info);
505}
506EXPORT_SYMBOL_GPL(i2c_new_dummy);
507
David Brownellf37dd802007-02-13 22:09:00 +0100508/* ------------------------------------------------------------------------- */
509
David Brownell16ffadf2007-05-01 23:26:28 +0200510/* I2C bus adapters -- one roots each I2C or SMBUS segment */
511
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200512static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Brownellef2c83212007-05-01 23:26:28 +0200514 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 complete(&adap->dev_released);
516}
517
Jean Delvare99cd8e22009-06-19 16:58:20 +0200518/*
519 * Let users instantiate I2C devices through sysfs. This can be used when
520 * platform initialization code doesn't contain the proper data for
521 * whatever reason. Also useful for drivers that do device detection and
522 * detection fails, either because the device uses an unexpected address,
523 * or this is a compatible device with different ID register values.
524 *
525 * Parameter checking may look overzealous, but we really don't want
526 * the user to provide incorrect parameters.
527 */
528static ssize_t
529i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 struct i2c_adapter *adap = to_i2c_adapter(dev);
533 struct i2c_board_info info;
534 struct i2c_client *client;
535 char *blank, end;
536 int res;
537
538 dev_warn(dev, "The new_device interface is still experimental "
539 "and may change in a near future\n");
540 memset(&info, 0, sizeof(struct i2c_board_info));
541
542 blank = strchr(buf, ' ');
543 if (!blank) {
544 dev_err(dev, "%s: Missing parameters\n", "new_device");
545 return -EINVAL;
546 }
547 if (blank - buf > I2C_NAME_SIZE - 1) {
548 dev_err(dev, "%s: Invalid device name\n", "new_device");
549 return -EINVAL;
550 }
551 memcpy(info.type, buf, blank - buf);
552
553 /* Parse remaining parameters, reject extra parameters */
554 res = sscanf(++blank, "%hi%c", &info.addr, &end);
555 if (res < 1) {
556 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
557 return -EINVAL;
558 }
559 if (res > 1 && end != '\n') {
560 dev_err(dev, "%s: Extra parameters\n", "new_device");
561 return -EINVAL;
562 }
563
564 if (info.addr < 0x03 || info.addr > 0x77) {
565 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
566 info.addr);
567 return -EINVAL;
568 }
569
570 client = i2c_new_device(adap, &info);
571 if (!client)
572 return -EEXIST;
573
574 /* Keep track of the added device */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200575 i2c_lock_adapter(adap);
576 list_add_tail(&client->detected, &adap->userspace_clients);
577 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200578 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
579 info.type, info.addr);
580
581 return count;
582}
583
584/*
585 * And of course let the users delete the devices they instantiated, if
586 * they got it wrong. This interface can only be used to delete devices
587 * instantiated by i2c_sysfs_new_device above. This guarantees that we
588 * don't delete devices to which some kernel code still has references.
589 *
590 * Parameter checking may look overzealous, but we really don't want
591 * the user to delete the wrong device.
592 */
593static ssize_t
594i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
596{
597 struct i2c_adapter *adap = to_i2c_adapter(dev);
598 struct i2c_client *client, *next;
599 unsigned short addr;
600 char end;
601 int res;
602
603 /* Parse parameters, reject extra parameters */
604 res = sscanf(buf, "%hi%c", &addr, &end);
605 if (res < 1) {
606 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
607 return -EINVAL;
608 }
609 if (res > 1 && end != '\n') {
610 dev_err(dev, "%s: Extra parameters\n", "delete_device");
611 return -EINVAL;
612 }
613
614 /* Make sure the device was added through sysfs */
615 res = -ENOENT;
Jean Delvare6629dcf2010-05-04 11:09:28 +0200616 i2c_lock_adapter(adap);
617 list_for_each_entry_safe(client, next, &adap->userspace_clients,
618 detected) {
619 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200620 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
621 "delete_device", client->name, client->addr);
622
623 list_del(&client->detected);
624 i2c_unregister_device(client);
625 res = count;
626 break;
627 }
628 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200629 i2c_unlock_adapter(adap);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200630
631 if (res < 0)
632 dev_err(dev, "%s: Can't find device in list\n",
633 "delete_device");
634 return res;
635}
636
Jean Delvare4f8cf822009-09-18 22:45:46 +0200637static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
638static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
639
640static struct attribute *i2c_adapter_attrs[] = {
641 &dev_attr_name.attr,
642 &dev_attr_new_device.attr,
643 &dev_attr_delete_device.attr,
644 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200645};
646
Jean Delvare4f8cf822009-09-18 22:45:46 +0200647static struct attribute_group i2c_adapter_attr_group = {
648 .attrs = i2c_adapter_attrs,
649};
650
651static const struct attribute_group *i2c_adapter_attr_groups[] = {
652 &i2c_adapter_attr_group,
653 NULL
654};
655
656static struct device_type i2c_adapter_type = {
657 .groups = i2c_adapter_attr_groups,
658 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200659};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Jean Delvare2bb50952009-09-18 22:45:46 +0200661#ifdef CONFIG_I2C_COMPAT
662static struct class_compat *i2c_adapter_compat_class;
663#endif
664
David Brownell9c1600e2007-05-01 23:26:31 +0200665static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
666{
667 struct i2c_devinfo *devinfo;
668
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200669 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200670 list_for_each_entry(devinfo, &__i2c_board_list, list) {
671 if (devinfo->busnum == adapter->nr
672 && !i2c_new_device(adapter,
673 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100674 dev_err(&adapter->dev,
675 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200676 devinfo->board_info.addr);
677 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200678 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200679}
680
Jean Delvare69b00892009-12-06 17:06:27 +0100681static int i2c_do_add_adapter(struct i2c_driver *driver,
682 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100683{
Jean Delvare4735c982008-07-14 22:38:36 +0200684 /* Detect supported devices on that bus, and instantiate them */
685 i2c_detect(adap, driver);
686
687 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100688 if (driver->attach_adapter) {
689 /* We ignore the return code; if it fails, too bad */
690 driver->attach_adapter(adap);
691 }
692 return 0;
693}
694
Jean Delvare69b00892009-12-06 17:06:27 +0100695static int __process_new_adapter(struct device_driver *d, void *data)
696{
697 return i2c_do_add_adapter(to_i2c_driver(d), data);
698}
699
David Brownell6e13e642007-05-01 23:26:31 +0200700static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Jean Delvare026526f2008-01-27 18:14:49 +0100702 int res = 0, dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
David Brownell1d0b19c2008-10-14 17:30:05 +0200704 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200705 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
706 res = -EAGAIN;
707 goto out_list;
708 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200709
Mika Kuoppala194684e2009-12-06 17:06:22 +0100710 rt_mutex_init(&adap->bus_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200711 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Jean Delvare8fcfef62009-03-28 21:34:43 +0100713 /* Set default timeout to 1 second if not already set */
714 if (adap->timeout == 0)
715 adap->timeout = HZ;
716
Kay Sievers27d9c182009-01-07 14:29:16 +0100717 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200718 adap->dev.bus = &i2c_bus_type;
719 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200720 res = device_register(&adap->dev);
721 if (res)
722 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200724 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
725
Jean Delvare2bb50952009-09-18 22:45:46 +0200726#ifdef CONFIG_I2C_COMPAT
727 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
728 adap->dev.parent);
729 if (res)
730 dev_warn(&adap->dev,
731 "Failed to create compatibility class link\n");
732#endif
733
Jean Delvare729d6dd2009-06-19 16:58:18 +0200734 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +0200735 if (adap->nr < __i2c_first_dynamic_bus_num)
736 i2c_scan_static_board_info(adap);
737
Jean Delvare4735c982008-07-14 22:38:36 +0200738 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200739 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100740 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100741 __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +0100742 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200743
744 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +0200745
Jean Delvareb119c6c2006-08-15 18:26:30 +0200746out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +0200747 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +0200748 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200749 mutex_unlock(&core_lock);
750 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
David Brownell6e13e642007-05-01 23:26:31 +0200753/**
754 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
755 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +0200756 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200757 *
758 * This routine is used to declare an I2C adapter when its bus number
759 * doesn't matter. Examples: for I2C adapters dynamically added by
760 * USB links or PCI plugin cards.
761 *
762 * When this returns zero, a new bus number was allocated and stored
763 * in adap->nr, and the specified adapter became available for clients.
764 * Otherwise, a negative errno value is returned.
765 */
766int i2c_add_adapter(struct i2c_adapter *adapter)
767{
768 int id, res = 0;
769
770retry:
771 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
772 return -ENOMEM;
773
Jean Delvarecaada322008-01-27 18:14:49 +0100774 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200775 /* "above" here means "above or equal to", sigh */
776 res = idr_get_new_above(&i2c_adapter_idr, adapter,
777 __i2c_first_dynamic_bus_num, &id);
Jean Delvarecaada322008-01-27 18:14:49 +0100778 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200779
780 if (res < 0) {
781 if (res == -EAGAIN)
782 goto retry;
783 return res;
784 }
785
786 adapter->nr = id;
787 return i2c_register_adapter(adapter);
788}
789EXPORT_SYMBOL(i2c_add_adapter);
790
791/**
792 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
793 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +0200794 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +0200795 *
796 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +0100797 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
798 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +0200799 * is used to properly configure I2C devices.
800 *
801 * If no devices have pre-been declared for this bus, then be sure to
802 * register the adapter before any dynamically allocated ones. Otherwise
803 * the required bus ID may not be available.
804 *
805 * When this returns zero, the specified adapter became available for
806 * clients using the bus number provided in adap->nr. Also, the table
807 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
808 * and the appropriate driver model device nodes are created. Otherwise, a
809 * negative errno value is returned.
810 */
811int i2c_add_numbered_adapter(struct i2c_adapter *adap)
812{
813 int id;
814 int status;
815
816 if (adap->nr & ~MAX_ID_MASK)
817 return -EINVAL;
818
819retry:
820 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
821 return -ENOMEM;
822
Jean Delvarecaada322008-01-27 18:14:49 +0100823 mutex_lock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200824 /* "above" here means "above or equal to", sigh;
825 * we need the "equal to" result to force the result
826 */
827 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
828 if (status == 0 && id != adap->nr) {
829 status = -EBUSY;
830 idr_remove(&i2c_adapter_idr, id);
831 }
Jean Delvarecaada322008-01-27 18:14:49 +0100832 mutex_unlock(&core_lock);
David Brownell6e13e642007-05-01 23:26:31 +0200833 if (status == -EAGAIN)
834 goto retry;
835
836 if (status == 0)
837 status = i2c_register_adapter(adap);
838 return status;
839}
840EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
841
Jean Delvare69b00892009-12-06 17:06:27 +0100842static int i2c_do_del_adapter(struct i2c_driver *driver,
843 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +0100844{
Jean Delvare4735c982008-07-14 22:38:36 +0200845 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +0100846 int res;
847
Jean Delvareacec2112009-03-28 21:34:40 +0100848 /* Remove the devices we created ourselves as the result of hardware
849 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +0200850 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
851 if (client->adapter == adapter) {
852 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
853 client->name, client->addr);
854 list_del(&client->detected);
855 i2c_unregister_device(client);
856 }
857 }
858
Jean Delvare026526f2008-01-27 18:14:49 +0100859 if (!driver->detach_adapter)
860 return 0;
861 res = driver->detach_adapter(adapter);
862 if (res)
863 dev_err(&adapter->dev, "detach_adapter failed (%d) "
864 "for driver [%s]\n", res, driver->driver.name);
865 return res;
866}
867
Jean Delvaree549c2b2009-06-19 16:58:19 +0200868static int __unregister_client(struct device *dev, void *dummy)
869{
870 struct i2c_client *client = i2c_verify_client(dev);
871 if (client)
872 i2c_unregister_device(client);
873 return 0;
874}
875
Jean Delvare69b00892009-12-06 17:06:27 +0100876static int __process_removed_adapter(struct device_driver *d, void *data)
877{
878 return i2c_do_del_adapter(to_i2c_driver(d), data);
879}
880
David Brownelld64f73b2007-07-12 14:12:28 +0200881/**
882 * i2c_del_adapter - unregister I2C adapter
883 * @adap: the adapter being unregistered
884 * Context: can sleep
885 *
886 * This unregisters an I2C adapter which was previously registered
887 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
888 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889int i2c_del_adapter(struct i2c_adapter *adap)
890{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +0200892 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100893 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200896 mutex_lock(&core_lock);
897 found = idr_find(&i2c_adapter_idr, adap->nr);
898 mutex_unlock(&core_lock);
899 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +0200900 pr_debug("i2c-core: attempting to delete unregistered "
901 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200902 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904
Jean Delvare026526f2008-01-27 18:14:49 +0100905 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200906 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100907 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +0100908 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200909 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +0100910 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +0200911 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100913 /* Remove devices instantiated from sysfs */
Jean Delvare6629dcf2010-05-04 11:09:28 +0200914 i2c_lock_adapter(adap);
915 list_for_each_entry_safe(client, next, &adap->userspace_clients,
916 detected) {
917 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
918 client->addr);
919 list_del(&client->detected);
920 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100921 }
Jean Delvare6629dcf2010-05-04 11:09:28 +0200922 i2c_unlock_adapter(adap);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +0100923
Jean Delvaree549c2b2009-06-19 16:58:19 +0200924 /* Detach any active clients. This can't fail, thus we do not
925 checking the returned value. */
926 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Jean Delvare2bb50952009-09-18 22:45:46 +0200928#ifdef CONFIG_I2C_COMPAT
929 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
930 adap->dev.parent);
931#endif
932
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +0100933 /* device name is gone after device_unregister */
934 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /* clean up the sysfs representation */
937 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* wait for sysfs to drop all references */
941 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
David Brownell6e13e642007-05-01 23:26:31 +0200943 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200944 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200946 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Jean Delvarebd4bc3db2008-07-16 19:30:05 +0200948 /* Clear the device structure in case this adapter is ever going to be
949 added again */
950 memset(&adap->dev, 0, sizeof(adap->dev));
951
Jean Delvare35fc37f2009-06-19 16:58:19 +0200952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
David Brownellc0564602007-05-01 23:26:31 +0200954EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956
David Brownell7b4fbc52007-05-01 23:26:30 +0200957/* ------------------------------------------------------------------------- */
958
Jean Delvare69b00892009-12-06 17:06:27 +0100959static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +0200960{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200961 if (dev->type != &i2c_adapter_type)
962 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +0100963 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +0200964}
965
David Brownell7b4fbc52007-05-01 23:26:30 +0200966/*
967 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +0200968 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
970
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800971int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Jean Delvare7eebcb72006-02-05 23:28:21 +0100973 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
David Brownell1d0b19c2008-10-14 17:30:05 +0200975 /* Can't register until after driver model init */
976 if (unlikely(WARN_ON(!i2c_bus_type.p)))
977 return -EAGAIN;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -0800980 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Jean Delvare729d6dd2009-06-19 16:58:18 +0200983 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +0200984 * will have called probe() for all matching-but-unbound devices.
985 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 res = driver_register(&driver->driver);
987 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +0100988 return res;
David Brownell438d6c22006-12-10 21:21:31 +0100989
Laurent Riffard35d8b2e2005-11-26 20:34:05 +0100990 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jean Delvare4735c982008-07-14 22:38:36 +0200992 INIT_LIST_HEAD(&driver->clients);
993 /* Walk the adapters that are already present */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200994 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +0100995 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
Jean Delvarecaada322008-01-27 18:14:49 +0100996 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +0200997
Jean Delvare7eebcb72006-02-05 23:28:21 +0100998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001000EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Jean Delvare69b00892009-12-06 17:06:27 +01001002static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001003{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001004 if (dev->type != &i2c_adapter_type)
1005 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001006 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001007}
1008
David Brownella1d9e6e2007-05-01 23:26:30 +02001009/**
1010 * i2c_del_driver - unregister I2C driver
1011 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001012 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001013 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001014void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Jean Delvarecaada322008-01-27 18:14:49 +01001016 mutex_lock(&core_lock);
Jean Delvare69b00892009-12-06 17:06:27 +01001017 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001018 mutex_unlock(&core_lock);
David Brownella1d9e6e2007-05-01 23:26:30 +02001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001021 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
David Brownellc0564602007-05-01 23:26:31 +02001023EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
David Brownell7b4fbc52007-05-01 23:26:30 +02001025/* ------------------------------------------------------------------------- */
1026
David Brownell9b766b82008-01-27 18:14:51 +01001027static int __i2c_check_addr(struct device *dev, void *addrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
David Brownell9b766b82008-01-27 18:14:51 +01001029 struct i2c_client *client = i2c_verify_client(dev);
1030 int addr = *(int *)addrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
David Brownell9b766b82008-01-27 18:14:51 +01001032 if (client && client->addr == addr)
1033 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return 0;
1035}
1036
Jean Delvare5e31c2b2007-11-15 19:24:02 +01001037static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
David Brownell9b766b82008-01-27 18:14:51 +01001039 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Jean Delvaree48d3312008-01-27 18:14:48 +01001042/**
1043 * i2c_use_client - increments the reference count of the i2c client structure
1044 * @client: the client being referenced
1045 *
1046 * Each live reference to a client should be refcounted. The driver model does
1047 * that automatically as part of driver binding, so that most drivers don't
1048 * need to do this explicitly: they hold a reference until they're unbound
1049 * from the device.
1050 *
1051 * A pointer to the client with the incremented reference counter is returned.
1052 */
1053struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
David Brownell6ea438e2008-07-14 22:38:24 +02001055 if (client && get_device(&client->dev))
1056 return client;
1057 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
David Brownellc0564602007-05-01 23:26:31 +02001059EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Jean Delvaree48d3312008-01-27 18:14:48 +01001061/**
1062 * i2c_release_client - release a use of the i2c client structure
1063 * @client: the client being no longer referenced
1064 *
1065 * Must be called when a user of a client is finished with it.
1066 */
1067void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
David Brownell6ea438e2008-07-14 22:38:24 +02001069 if (client)
1070 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
David Brownellc0564602007-05-01 23:26:31 +02001072EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David Brownell9b766b82008-01-27 18:14:51 +01001074struct i2c_cmd_arg {
1075 unsigned cmd;
1076 void *arg;
1077};
1078
1079static int i2c_cmd(struct device *dev, void *_arg)
1080{
1081 struct i2c_client *client = i2c_verify_client(dev);
1082 struct i2c_cmd_arg *arg = _arg;
1083
1084 if (client && client->driver && client->driver->command)
1085 client->driver->command(client, arg->cmd, arg->arg);
1086 return 0;
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1090{
David Brownell9b766b82008-01-27 18:14:51 +01001091 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
David Brownell9b766b82008-01-27 18:14:51 +01001093 cmd_arg.cmd = cmd;
1094 cmd_arg.arg = arg;
1095 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
David Brownellc0564602007-05-01 23:26:31 +02001097EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099static int __init i2c_init(void)
1100{
1101 int retval;
1102
1103 retval = bus_register(&i2c_bus_type);
1104 if (retval)
1105 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001106#ifdef CONFIG_I2C_COMPAT
1107 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1108 if (!i2c_adapter_compat_class) {
1109 retval = -ENOMEM;
1110 goto bus_err;
1111 }
1112#endif
David Brownelle9f13732008-01-27 18:14:52 +01001113 retval = i2c_add_driver(&dummy_driver);
1114 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001115 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001116 return 0;
1117
Jean Delvare2bb50952009-09-18 22:45:46 +02001118class_err:
1119#ifdef CONFIG_I2C_COMPAT
1120 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001121bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001122#endif
David Brownelle9f13732008-01-27 18:14:52 +01001123 bus_unregister(&i2c_bus_type);
1124 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125}
1126
1127static void __exit i2c_exit(void)
1128{
David Brownelle9f13732008-01-27 18:14:52 +01001129 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001130#ifdef CONFIG_I2C_COMPAT
1131 class_compat_unregister(i2c_adapter_compat_class);
1132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 bus_unregister(&i2c_bus_type);
1134}
1135
David Brownella10f9e72008-10-14 17:30:06 +02001136/* We must initialize early, because some subsystems register i2c drivers
1137 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1138 */
1139postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140module_exit(i2c_exit);
1141
1142/* ----------------------------------------------------
1143 * the functional interface to the i2c busses.
1144 * ----------------------------------------------------
1145 */
1146
David Brownella1cdeda2008-07-14 22:38:24 +02001147/**
1148 * i2c_transfer - execute a single or combined I2C message
1149 * @adap: Handle to I2C bus
1150 * @msgs: One or more messages to execute before STOP is issued to
1151 * terminate the operation; each message begins with a START.
1152 * @num: Number of messages to be executed.
1153 *
1154 * Returns negative errno, else the number of messages executed.
1155 *
1156 * Note that there is no requirement that each message be sent to
1157 * the same slave address, although that is the most common model.
1158 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001159int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001161 unsigned long orig_jiffies;
1162 int ret, try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
David Brownella1cdeda2008-07-14 22:38:24 +02001164 /* REVISIT the fault reporting model here is weak:
1165 *
1166 * - When we get an error after receiving N bytes from a slave,
1167 * there is no way to report "N".
1168 *
1169 * - When we get a NAK after transmitting N bytes to a slave,
1170 * there is no way to report "N" ... or to let the master
1171 * continue executing the rest of this combined message, if
1172 * that's the appropriate response.
1173 *
1174 * - When for example "num" is two and we successfully complete
1175 * the first message but get an error part way through the
1176 * second, it's unclear whether that should be reported as
1177 * one (discarding status on the second message) or errno
1178 * (discarding status on the first one).
1179 */
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (adap->algo->master_xfer) {
1182#ifdef DEBUG
1183 for (ret = 0; ret < num; ret++) {
1184 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001185 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1186 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1187 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189#endif
1190
Mike Rapoportcea443a2008-01-27 18:14:50 +01001191 if (in_atomic() || irqs_disabled()) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001192 ret = rt_mutex_trylock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001193 if (!ret)
1194 /* I2C activity is ongoing. */
1195 return -EAGAIN;
1196 } else {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001197 rt_mutex_lock(&adap->bus_lock);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001198 }
1199
Clifford Wolf66b650f2009-06-15 18:01:46 +02001200 /* Retry automatically on arbitration loss */
1201 orig_jiffies = jiffies;
1202 for (ret = 0, try = 0; try <= adap->retries; try++) {
1203 ret = adap->algo->master_xfer(adap, msgs, num);
1204 if (ret != -EAGAIN)
1205 break;
1206 if (time_after(jiffies, orig_jiffies + adap->timeout))
1207 break;
1208 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001209 rt_mutex_unlock(&adap->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 return ret;
1212 } else {
1213 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001214 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
1216}
David Brownellc0564602007-05-01 23:26:31 +02001217EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
David Brownella1cdeda2008-07-14 22:38:24 +02001219/**
1220 * i2c_master_send - issue a single I2C message in master transmit mode
1221 * @client: Handle to slave device
1222 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001223 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001224 *
1225 * Returns negative errno, or else the number of bytes written.
1226 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001227int i2c_master_send(struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001230 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct i2c_msg msg;
1232
Jean Delvare815f55f2005-05-07 22:58:46 +02001233 msg.addr = client->addr;
1234 msg.flags = client->flags & I2C_M_TEN;
1235 msg.len = count;
1236 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001237
Jean Delvare815f55f2005-05-07 22:58:46 +02001238 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jean Delvare815f55f2005-05-07 22:58:46 +02001240 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1241 transmitted, else error code. */
1242 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
David Brownellc0564602007-05-01 23:26:31 +02001244EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
David Brownella1cdeda2008-07-14 22:38:24 +02001246/**
1247 * i2c_master_recv - issue a single I2C message in master receive mode
1248 * @client: Handle to slave device
1249 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001250 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001251 *
1252 * Returns negative errno, or else the number of bytes read.
1253 */
Farid Hammane7225acf2010-05-21 18:40:58 +02001254int i2c_master_recv(struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Farid Hammane7225acf2010-05-21 18:40:58 +02001256 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct i2c_msg msg;
1258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Jean Delvare815f55f2005-05-07 22:58:46 +02001260 msg.addr = client->addr;
1261 msg.flags = client->flags & I2C_M_TEN;
1262 msg.flags |= I2C_M_RD;
1263 msg.len = count;
1264 msg.buf = buf;
1265
1266 ret = i2c_transfer(adap, &msg, 1);
1267
1268 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1269 transmitted, else error code. */
1270 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
David Brownellc0564602007-05-01 23:26:31 +02001272EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/* ----------------------------------------------------
1275 * the i2c address scanning function
1276 * Will not work for 10-bit addresses!
1277 * ----------------------------------------------------
1278 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001279
Jean Delvare63e4e802010-06-03 11:33:51 +02001280/*
1281 * Legacy default probe function, mostly relevant for SMBus. The default
1282 * probe method is a quick write, but it is known to corrupt the 24RF08
1283 * EEPROMs due to a state machine bug, and could also irreversibly
1284 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1285 * we use a short byte read instead. Also, some bus drivers don't implement
1286 * quick write, so we fallback to a byte read in that case too.
1287 * On x86, there is another special case for FSC hardware monitoring chips,
1288 * which want regular byte reads (address 0x73.) Fortunately, these are the
1289 * only known chips using this I2C address on PC hardware.
1290 * Returns 1 if probe succeeded, 0 if not.
1291 */
1292static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1293{
1294 int err;
1295 union i2c_smbus_data dummy;
1296
1297#ifdef CONFIG_X86
1298 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1299 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1300 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1301 I2C_SMBUS_BYTE_DATA, &dummy);
1302 else
1303#endif
1304 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1305 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1306 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1307 I2C_SMBUS_BYTE, &dummy);
1308 else
1309 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1310 I2C_SMBUS_QUICK, NULL);
1311
1312 return err >= 0;
1313}
1314
Jean Delvareccfbbd02009-12-06 17:06:25 +01001315static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001316 struct i2c_driver *driver)
1317{
1318 struct i2c_board_info info;
1319 struct i2c_adapter *adapter = temp_client->adapter;
1320 int addr = temp_client->addr;
1321 int err;
1322
1323 /* Make sure the address is valid */
1324 if (addr < 0x03 || addr > 0x77) {
1325 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1326 addr);
1327 return -EINVAL;
1328 }
1329
1330 /* Skip if already in use */
1331 if (i2c_check_addr(adapter, addr))
1332 return 0;
1333
Jean Delvareccfbbd02009-12-06 17:06:25 +01001334 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001335 if (!i2c_default_probe(adapter, addr))
1336 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001337
1338 /* Finally call the custom detection function */
1339 memset(&info, 0, sizeof(struct i2c_board_info));
1340 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001341 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001342 if (err) {
1343 /* -ENODEV is returned if the detection fails. We catch it
1344 here as this isn't an error. */
1345 return err == -ENODEV ? 0 : err;
1346 }
1347
1348 /* Consistency check */
1349 if (info.type[0] == '\0') {
1350 dev_err(&adapter->dev, "%s detection function provided "
1351 "no name for 0x%x\n", driver->driver.name,
1352 addr);
1353 } else {
1354 struct i2c_client *client;
1355
1356 /* Detection succeeded, instantiate the device */
1357 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1358 info.type, info.addr);
1359 client = i2c_new_device(adapter, &info);
1360 if (client)
1361 list_add_tail(&client->detected, &driver->clients);
1362 else
1363 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1364 info.type, info.addr);
1365 }
1366 return 0;
1367}
1368
1369static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1370{
Jean Delvarec3813d62009-12-14 21:17:25 +01001371 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001372 struct i2c_client *temp_client;
1373 int i, err = 0;
1374 int adap_id = i2c_adapter_id(adapter);
1375
Jean Delvarec3813d62009-12-14 21:17:25 +01001376 address_list = driver->address_list;
1377 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001378 return 0;
1379
1380 /* Set up a temporary client to help detect callback */
1381 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1382 if (!temp_client)
1383 return -ENOMEM;
1384 temp_client->adapter = adapter;
1385
Jean Delvare4329cf82008-08-28 08:33:23 +02001386 /* Stop here if the classes do not match */
1387 if (!(adapter->class & driver->class))
1388 goto exit_free;
1389
Jean Delvare4735c982008-07-14 22:38:36 +02001390 /* Stop here if we can't use SMBUS_QUICK */
1391 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
Jean Delvarec3813d62009-12-14 21:17:25 +01001392 if (address_list[0] == I2C_CLIENT_END)
Jean Delvare4735c982008-07-14 22:38:36 +02001393 goto exit_free;
1394
1395 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1396 "can't probe for chips\n");
1397 err = -EOPNOTSUPP;
1398 goto exit_free;
1399 }
1400
Jean Delvarec3813d62009-12-14 21:17:25 +01001401 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001402 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001403 "addr 0x%02x\n", adap_id, address_list[i]);
1404 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001405 err = i2c_detect_address(temp_client, driver);
Jean Delvare4735c982008-07-14 22:38:36 +02001406 if (err)
1407 goto exit_free;
1408 }
1409
1410 exit_free:
1411 kfree(temp_client);
1412 return err;
1413}
1414
Jean Delvare12b5053a2007-05-01 23:26:31 +02001415struct i2c_client *
1416i2c_new_probed_device(struct i2c_adapter *adap,
1417 struct i2c_board_info *info,
1418 unsigned short const *addr_list)
1419{
1420 int i;
1421
1422 /* Stop here if the bus doesn't support probing */
1423 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1424 dev_err(&adap->dev, "Probing not supported\n");
1425 return NULL;
1426 }
1427
Jean Delvare12b5053a2007-05-01 23:26:31 +02001428 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1429 /* Check address validity */
1430 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1431 dev_warn(&adap->dev, "Invalid 7-bit address "
1432 "0x%02x\n", addr_list[i]);
1433 continue;
1434 }
1435
1436 /* Check address availability */
David Brownell9b766b82008-01-27 18:14:51 +01001437 if (i2c_check_addr(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001438 dev_dbg(&adap->dev, "Address 0x%02x already in "
1439 "use, not probing\n", addr_list[i]);
1440 continue;
1441 }
1442
Jean Delvare63e4e802010-06-03 11:33:51 +02001443 /* Test address responsiveness */
1444 if (i2c_default_probe(adap, addr_list[i]))
1445 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001446 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001447
1448 if (addr_list[i] == I2C_CLIENT_END) {
1449 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1450 return NULL;
1451 }
1452
1453 info->addr = addr_list[i];
1454 return i2c_new_device(adap, info);
1455}
1456EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1457
Farid Hammane7225acf2010-05-21 18:40:58 +02001458struct i2c_adapter *i2c_get_adapter(int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001461
Jean Delvarecaada322008-01-27 18:14:49 +01001462 mutex_lock(&core_lock);
Jack Stone1cf92b42009-06-15 18:01:46 +02001463 adapter = idr_find(&i2c_adapter_idr, id);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001464 if (adapter && !try_module_get(adapter->owner))
1465 adapter = NULL;
1466
Jean Delvarecaada322008-01-27 18:14:49 +01001467 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001468 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
David Brownellc0564602007-05-01 23:26:31 +02001470EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472void i2c_put_adapter(struct i2c_adapter *adap)
1473{
1474 module_put(adap->owner);
1475}
David Brownellc0564602007-05-01 23:26:31 +02001476EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478/* The SMBus parts */
1479
David Brownell438d6c22006-12-10 21:21:31 +01001480#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001481static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
1483 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001484
Farid Hammane7225acf2010-05-21 18:40:58 +02001485 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001486 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 data = data ^ POLY;
1488 data = data << 1;
1489 }
1490 return (u8)(data >> 8);
1491}
1492
Jean Delvare421ef472005-10-26 21:28:55 +02001493/* Incremental CRC8 over count bytes in the array pointed to by p */
1494static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
1496 int i;
1497
Farid Hammane7225acf2010-05-21 18:40:58 +02001498 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001499 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return crc;
1501}
1502
Jean Delvare421ef472005-10-26 21:28:55 +02001503/* Assume a 7-bit address, which is reasonable for SMBus */
1504static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Jean Delvare421ef472005-10-26 21:28:55 +02001506 /* The address will be sent first */
1507 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1508 pec = i2c_smbus_pec(pec, &addr, 1);
1509
1510 /* The data buffer follows */
1511 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Jean Delvare421ef472005-10-26 21:28:55 +02001514/* Used for write only transactions */
1515static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jean Delvare421ef472005-10-26 21:28:55 +02001517 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1518 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519}
1520
Jean Delvare421ef472005-10-26 21:28:55 +02001521/* Return <0 on CRC error
1522 If there was a write before this read (most cases) we need to take the
1523 partial CRC from the write part into account.
1524 Note that this function does modify the message (we need to decrease the
1525 message length to hide the CRC byte from the caller). */
1526static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Jean Delvare421ef472005-10-26 21:28:55 +02001528 u8 rpec = msg->buf[--msg->len];
1529 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (rpec != cpec) {
1532 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1533 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001534 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
David Brownell438d6c22006-12-10 21:21:31 +01001536 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
David Brownella1cdeda2008-07-14 22:38:24 +02001539/**
1540 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1541 * @client: Handle to slave device
1542 *
1543 * This executes the SMBus "receive byte" protocol, returning negative errno
1544 * else the byte received from the device.
1545 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546s32 i2c_smbus_read_byte(struct i2c_client *client)
1547{
1548 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001549 int status;
1550
1551 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1552 I2C_SMBUS_READ, 0,
1553 I2C_SMBUS_BYTE, &data);
1554 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
David Brownellc0564602007-05-01 23:26:31 +02001556EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
David Brownella1cdeda2008-07-14 22:38:24 +02001558/**
1559 * i2c_smbus_write_byte - SMBus "send byte" protocol
1560 * @client: Handle to slave device
1561 * @value: Byte to be sent
1562 *
1563 * This executes the SMBus "send byte" protocol, returning negative errno
1564 * else zero on success.
1565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1567{
Farid Hammane7225acf2010-05-21 18:40:58 +02001568 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001569 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
David Brownellc0564602007-05-01 23:26:31 +02001571EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
David Brownella1cdeda2008-07-14 22:38:24 +02001573/**
1574 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1575 * @client: Handle to slave device
1576 * @command: Byte interpreted by slave
1577 *
1578 * This executes the SMBus "read byte" protocol, returning negative errno
1579 * else a data byte received from the device.
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1582{
1583 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001584 int status;
1585
1586 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1587 I2C_SMBUS_READ, command,
1588 I2C_SMBUS_BYTE_DATA, &data);
1589 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
David Brownellc0564602007-05-01 23:26:31 +02001591EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
David Brownella1cdeda2008-07-14 22:38:24 +02001593/**
1594 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1595 * @client: Handle to slave device
1596 * @command: Byte interpreted by slave
1597 * @value: Byte being written
1598 *
1599 * This executes the SMBus "write byte" protocol, returning negative errno
1600 * else zero on success.
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1603{
1604 union i2c_smbus_data data;
1605 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001606 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1607 I2C_SMBUS_WRITE, command,
1608 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
David Brownellc0564602007-05-01 23:26:31 +02001610EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
David Brownella1cdeda2008-07-14 22:38:24 +02001612/**
1613 * i2c_smbus_read_word_data - SMBus "read word" protocol
1614 * @client: Handle to slave device
1615 * @command: Byte interpreted by slave
1616 *
1617 * This executes the SMBus "read word" protocol, returning negative errno
1618 * else a 16-bit unsigned "word" received from the device.
1619 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1621{
1622 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001623 int status;
1624
1625 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1626 I2C_SMBUS_READ, command,
1627 I2C_SMBUS_WORD_DATA, &data);
1628 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
David Brownellc0564602007-05-01 23:26:31 +02001630EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
David Brownella1cdeda2008-07-14 22:38:24 +02001632/**
1633 * i2c_smbus_write_word_data - SMBus "write word" protocol
1634 * @client: Handle to slave device
1635 * @command: Byte interpreted by slave
1636 * @value: 16-bit "word" being written
1637 *
1638 * This executes the SMBus "write word" protocol, returning negative errno
1639 * else zero on success.
1640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1642{
1643 union i2c_smbus_data data;
1644 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001645 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1646 I2C_SMBUS_WRITE, command,
1647 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
David Brownellc0564602007-05-01 23:26:31 +02001649EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
David Brownella64ec072007-10-13 23:56:31 +02001651/**
Prakash Mortha596c88f2008-10-14 17:30:06 +02001652 * i2c_smbus_process_call - SMBus "process call" protocol
1653 * @client: Handle to slave device
1654 * @command: Byte interpreted by slave
1655 * @value: 16-bit "word" being written
1656 *
1657 * This executes the SMBus "process call" protocol, returning negative errno
1658 * else a 16-bit unsigned "word" received from the device.
1659 */
1660s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1661{
1662 union i2c_smbus_data data;
1663 int status;
1664 data.word = value;
1665
1666 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1667 I2C_SMBUS_WRITE, command,
1668 I2C_SMBUS_PROC_CALL, &data);
1669 return (status < 0) ? status : data.word;
1670}
1671EXPORT_SYMBOL(i2c_smbus_process_call);
1672
1673/**
David Brownella1cdeda2008-07-14 22:38:24 +02001674 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02001675 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02001676 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02001677 * @values: Byte array into which data will be read; big enough to hold
1678 * the data returned by the slave. SMBus allows at most 32 bytes.
1679 *
David Brownella1cdeda2008-07-14 22:38:24 +02001680 * This executes the SMBus "block read" protocol, returning negative errno
1681 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02001682 *
1683 * Note that using this function requires that the client's adapter support
1684 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1685 * support this; its emulation through I2C messaging relies on a specific
1686 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1687 */
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001688s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1689 u8 *values)
1690{
1691 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001692 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001693
David Brownell24a5bb72008-07-14 22:38:23 +02001694 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1695 I2C_SMBUS_READ, command,
1696 I2C_SMBUS_BLOCK_DATA, &data);
1697 if (status)
1698 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02001699
1700 memcpy(values, &data.block[1], data.block[0]);
1701 return data.block[0];
1702}
1703EXPORT_SYMBOL(i2c_smbus_read_block_data);
1704
David Brownella1cdeda2008-07-14 22:38:24 +02001705/**
1706 * i2c_smbus_write_block_data - SMBus "block write" protocol
1707 * @client: Handle to slave device
1708 * @command: Byte interpreted by slave
1709 * @length: Size of data block; SMBus allows at most 32 bytes
1710 * @values: Byte array which will be written.
1711 *
1712 * This executes the SMBus "block write" protocol, returning negative errno
1713 * else zero on success.
1714 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001716 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717{
1718 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (length > I2C_SMBUS_BLOCK_MAX)
1721 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01001723 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02001724 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1725 I2C_SMBUS_WRITE, command,
1726 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
David Brownellc0564602007-05-01 23:26:31 +02001728EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730/* Returns the number of read bytes */
Jean Delvare4b2643d2007-07-12 14:12:29 +02001731s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1732 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001735 int status;
Jean Delvare76560322006-01-18 23:14:55 +01001736
Jean Delvare4b2643d2007-07-12 14:12:29 +02001737 if (length > I2C_SMBUS_BLOCK_MAX)
1738 length = I2C_SMBUS_BLOCK_MAX;
1739 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02001740 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1741 I2C_SMBUS_READ, command,
1742 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1743 if (status < 0)
1744 return status;
Jean Delvare76560322006-01-18 23:14:55 +01001745
1746 memcpy(values, &data.block[1], data.block[0]);
1747 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
David Brownellc0564602007-05-01 23:26:31 +02001749EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jean Delvare21bbd692006-01-09 15:19:18 +11001751s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02001752 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11001753{
1754 union i2c_smbus_data data;
1755
1756 if (length > I2C_SMBUS_BLOCK_MAX)
1757 length = I2C_SMBUS_BLOCK_MAX;
1758 data.block[0] = length;
1759 memcpy(data.block + 1, values, length);
1760 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1761 I2C_SMBUS_WRITE, command,
1762 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1763}
David Brownellc0564602007-05-01 23:26:31 +02001764EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11001765
David Brownell438d6c22006-12-10 21:21:31 +01001766/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02001768static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1769 unsigned short flags,
1770 char read_write, u8 command, int size,
1771 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 /* So we need to generate a series of msgs. In the case of writing, we
1774 need to use only one message; when reading, we need two. We initialize
1775 most things with sane defaults, to keep the code below somewhat
1776 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001777 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1778 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02001779 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
David Brownell438d6c22006-12-10 21:21:31 +01001780 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1782 };
1783 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02001784 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02001785 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02001788 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 case I2C_SMBUS_QUICK:
1790 msg[0].len = 0;
1791 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01001792 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1793 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 num = 1;
1795 break;
1796 case I2C_SMBUS_BYTE:
1797 if (read_write == I2C_SMBUS_READ) {
1798 /* Special case: only a read! */
1799 msg[0].flags = I2C_M_RD | flags;
1800 num = 1;
1801 }
1802 break;
1803 case I2C_SMBUS_BYTE_DATA:
1804 if (read_write == I2C_SMBUS_READ)
1805 msg[1].len = 1;
1806 else {
1807 msg[0].len = 2;
1808 msgbuf0[1] = data->byte;
1809 }
1810 break;
1811 case I2C_SMBUS_WORD_DATA:
1812 if (read_write == I2C_SMBUS_READ)
1813 msg[1].len = 2;
1814 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02001815 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001817 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 break;
1820 case I2C_SMBUS_PROC_CALL:
1821 num = 2; /* Special case */
1822 read_write = I2C_SMBUS_READ;
1823 msg[0].len = 3;
1824 msg[1].len = 2;
1825 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02001826 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02001830 msg[1].flags |= I2C_M_RECV_LEN;
1831 msg[1].len = 1; /* block length will be added by
1832 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 } else {
1834 msg[0].len = data->block[0] + 2;
1835 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
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 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02001841 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 msgbuf0[i] = data->block[i-1];
1843 }
1844 break;
1845 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02001846 num = 2; /* Another special case */
1847 read_write = I2C_SMBUS_READ;
1848 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02001849 dev_err(&adapter->dev,
1850 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02001851 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02001852 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02001853 }
1854 msg[0].len = data->block[0] + 2;
1855 for (i = 1; i < msg[0].len; i++)
1856 msgbuf0[i] = data->block[i-1];
1857 msg[1].flags |= I2C_M_RECV_LEN;
1858 msg[1].len = 1; /* block length will be added by
1859 the underlying bus driver */
1860 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 case I2C_SMBUS_I2C_BLOCK_DATA:
1862 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02001863 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 } else {
1865 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02001866 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02001867 dev_err(&adapter->dev,
1868 "Invalid block write size %d\n",
1869 data->block[0]);
1870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872 for (i = 1; i <= data->block[0]; i++)
1873 msgbuf0[i] = data->block[i];
1874 }
1875 break;
1876 default:
David Brownell24a5bb72008-07-14 22:38:23 +02001877 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1878 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
Jean Delvare421ef472005-10-26 21:28:55 +02001881 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1882 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1883 if (i) {
1884 /* Compute PEC if first message is a write */
1885 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01001886 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02001887 i2c_smbus_add_pec(&msg[0]);
1888 else /* Write followed by read */
1889 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1890 }
1891 /* Ask for PEC if last message is a read */
1892 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01001893 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02001894 }
1895
David Brownell24a5bb72008-07-14 22:38:23 +02001896 status = i2c_transfer(adapter, msg, num);
1897 if (status < 0)
1898 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jean Delvare421ef472005-10-26 21:28:55 +02001900 /* Check PEC if last message is a read */
1901 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02001902 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1903 if (status < 0)
1904 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02001905 }
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02001908 switch (size) {
1909 case I2C_SMBUS_BYTE:
1910 data->byte = msgbuf0[0];
1911 break;
1912 case I2C_SMBUS_BYTE_DATA:
1913 data->byte = msgbuf1[0];
1914 break;
1915 case I2C_SMBUS_WORD_DATA:
1916 case I2C_SMBUS_PROC_CALL:
1917 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1918 break;
1919 case I2C_SMBUS_I2C_BLOCK_DATA:
1920 for (i = 0; i < data->block[0]; i++)
1921 data->block[i+1] = msgbuf1[i];
1922 break;
1923 case I2C_SMBUS_BLOCK_DATA:
1924 case I2C_SMBUS_BLOCK_PROC_CALL:
1925 for (i = 0; i < msgbuf1[0] + 1; i++)
1926 data->block[i] = msgbuf1[i];
1927 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 return 0;
1930}
1931
David Brownella1cdeda2008-07-14 22:38:24 +02001932/**
1933 * i2c_smbus_xfer - execute SMBus protocol operations
1934 * @adapter: Handle to I2C bus
1935 * @addr: Address of SMBus slave on that bus
1936 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1937 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1938 * @command: Byte interpreted by slave, for protocols which use such bytes
1939 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1940 * @data: Data to be read or written
1941 *
1942 * This executes an SMBus protocol operation, and returns a negative
1943 * errno code else zero on success.
1944 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001945s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02001946 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001947 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Clifford Wolf66b650f2009-06-15 18:01:46 +02001949 unsigned long orig_jiffies;
1950 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 if (adapter->algo->smbus_xfer) {
Mika Kuoppala194684e2009-12-06 17:06:22 +01001956 rt_mutex_lock(&adapter->bus_lock);
Clifford Wolf66b650f2009-06-15 18:01:46 +02001957
1958 /* Retry automatically on arbitration loss */
1959 orig_jiffies = jiffies;
1960 for (res = 0, try = 0; try <= adapter->retries; try++) {
1961 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1962 read_write, command,
1963 protocol, data);
1964 if (res != -EAGAIN)
1965 break;
1966 if (time_after(jiffies,
1967 orig_jiffies + adapter->timeout))
1968 break;
1969 }
Mika Kuoppala194684e2009-12-06 17:06:22 +01001970 rt_mutex_unlock(&adapter->bus_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 } else
Farid Hammane7225acf2010-05-21 18:40:58 +02001972 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
David Brownella1cdeda2008-07-14 22:38:24 +02001973 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return res;
1976}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1980MODULE_DESCRIPTION("I2C-Bus main module");
1981MODULE_LICENSE("GPL");