blob: 0d873ba2e82e61b87584b5d7ae5af09e4f38f27b [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
Jean Delvare5694f8a2012-03-26 21:47:19 +020017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* ------------------------------------------------------------------------- */
20
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020023 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Michael Lawnick08263742010-08-11 18:21:02 +020024 Jean Delvare <khali@linux-fr.org>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
26 Michael Lawnick <michael.lawnick.ext@nsn.com> */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053032#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/slab.h>
34#include <linux/i2c.h>
35#include <linux/init.h>
36#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010037#include <linux/mutex.h>
Grant Likely959e85f2010-06-08 07:48:19 -060038#include <linux/of_device.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010039#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010040#include <linux/hardirq.h>
41#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020042#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010043#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010044#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
David Brownell9c1600e2007-05-01 23:26:31 +020047#include "i2c-core.h"
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jean Delvare6629dcf2010-05-04 11:09:28 +020050/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020051 that device detection, deletion of detected devices, and attach_adapter
52 and detach_adapter calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010053static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static DEFINE_IDR(i2c_adapter_idr);
55
Jean Delvare4f8cf822009-09-18 22:45:46 +020056static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020057static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010058
59/* ------------------------------------------------------------------------- */
60
Jean Delvared2653e92008-04-29 23:11:39 +020061static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
62 const struct i2c_client *client)
63{
64 while (id->name[0]) {
65 if (strcmp(client->name, id->name) == 0)
66 return id;
67 id++;
68 }
69 return NULL;
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int i2c_device_match(struct device *dev, struct device_driver *drv)
73{
Jean Delvare51298d12009-09-18 22:45:45 +020074 struct i2c_client *client = i2c_verify_client(dev);
75 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020076
Jean Delvare51298d12009-09-18 22:45:45 +020077 if (!client)
78 return 0;
79
Grant Likely959e85f2010-06-08 07:48:19 -060080 /* Attempt an OF style match */
81 if (of_driver_match_device(dev, drv))
82 return 1;
83
Mika Westerberg907ddf82012-11-23 12:23:40 +010084 /* Then ACPI style match */
85 if (acpi_driver_match_device(dev, drv))
86 return 1;
87
Jean Delvare51298d12009-09-18 22:45:45 +020088 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020089 /* match on an id table if there is one */
90 if (driver->id_table)
91 return i2c_match_id(driver->id_table, client) != NULL;
92
Jean Delvareeb8a7902008-05-18 20:49:41 +020093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
David Brownell7b4fbc52007-05-01 23:26:30 +020096#ifdef CONFIG_HOTPLUG
97
98/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020099static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200100{
101 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
Jean Delvareeb8a7902008-05-18 20:49:41 +0200103 if (add_uevent_var(env, "MODALIAS=%s%s",
104 I2C_MODULE_PREFIX, client->name))
105 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200106 dev_dbg(dev, "uevent\n");
107 return 0;
108}
109
110#else
111#define i2c_device_uevent NULL
112#endif /* CONFIG_HOTPLUG */
113
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530114/* i2c bus recovery routines */
115static int get_scl_gpio_value(struct i2c_adapter *adap)
116{
117 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
118}
119
120static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
121{
122 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
123}
124
125static int get_sda_gpio_value(struct i2c_adapter *adap)
126{
127 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
128}
129
130static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
131{
132 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
133 struct device *dev = &adap->dev;
134 int ret = 0;
135
136 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
137 GPIOF_OUT_INIT_HIGH, "i2c-scl");
138 if (ret) {
139 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
140 return ret;
141 }
142
143 if (bri->get_sda) {
144 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
145 /* work without SDA polling */
146 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
147 bri->sda_gpio);
148 bri->get_sda = NULL;
149 }
150 }
151
152 return ret;
153}
154
155static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
156{
157 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
158
159 if (bri->get_sda)
160 gpio_free(bri->sda_gpio);
161
162 gpio_free(bri->scl_gpio);
163}
164
165/*
166 * We are generating clock pulses. ndelay() determines durating of clk pulses.
167 * We will generate clock with rate 100 KHz and so duration of both clock levels
168 * is: delay in ns = (10^6 / 100) / 2
169 */
170#define RECOVERY_NDELAY 5000
171#define RECOVERY_CLK_CNT 9
172
173static int i2c_generic_recovery(struct i2c_adapter *adap)
174{
175 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
176 int i = 0, val = 1, ret = 0;
177
178 if (bri->prepare_recovery)
179 bri->prepare_recovery(bri);
180
181 /*
182 * By this time SCL is high, as we need to give 9 falling-rising edges
183 */
184 while (i++ < RECOVERY_CLK_CNT * 2) {
185 if (val) {
186 /* Break if SDA is high */
187 if (bri->get_sda && bri->get_sda(adap))
188 break;
189 /* SCL shouldn't be low here */
190 if (!bri->get_scl(adap)) {
191 dev_err(&adap->dev,
192 "SCL is stuck low, exit recovery\n");
193 ret = -EBUSY;
194 break;
195 }
196 }
197
198 val = !val;
199 bri->set_scl(adap, val);
200 ndelay(RECOVERY_NDELAY);
201 }
202
203 if (bri->unprepare_recovery)
204 bri->unprepare_recovery(bri);
205
206 return ret;
207}
208
209int i2c_generic_scl_recovery(struct i2c_adapter *adap)
210{
211 adap->bus_recovery_info->set_scl(adap, 1);
212 return i2c_generic_recovery(adap);
213}
214
215int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
216{
217 int ret;
218
219 ret = i2c_get_gpios_for_recovery(adap);
220 if (ret)
221 return ret;
222
223 ret = i2c_generic_recovery(adap);
224 i2c_put_gpios_for_recovery(adap);
225
226 return ret;
227}
228
229int i2c_recover_bus(struct i2c_adapter *adap)
230{
231 if (!adap->bus_recovery_info)
232 return -EOPNOTSUPP;
233
234 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
235 return adap->bus_recovery_info->recover_bus(adap);
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static int i2c_device_probe(struct device *dev)
239{
Jean Delvare51298d12009-09-18 22:45:45 +0200240 struct i2c_client *client = i2c_verify_client(dev);
241 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100242 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200243
Jean Delvare51298d12009-09-18 22:45:45 +0200244 if (!client)
245 return 0;
246
247 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200248 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200249 return -ENODEV;
250 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200251 if (!device_can_wakeup(&client->dev))
252 device_init_wakeup(&client->dev,
253 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200254 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200255
Jean Delvaree0457442008-07-14 22:38:30 +0200256 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200257 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100258 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200259 i2c_set_clientdata(client, NULL);
260 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264static int i2c_device_remove(struct device *dev)
265{
Jean Delvare51298d12009-09-18 22:45:45 +0200266 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200267 struct i2c_driver *driver;
268 int status;
269
Jean Delvare51298d12009-09-18 22:45:45 +0200270 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200271 return 0;
272
273 driver = to_i2c_driver(dev->driver);
274 if (driver->remove) {
275 dev_dbg(dev, "remove\n");
276 status = driver->remove(client);
277 } else {
278 dev->driver = NULL;
279 status = 0;
280 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200281 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200282 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200283 i2c_set_clientdata(client, NULL);
284 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200285 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
David Brownellf37dd802007-02-13 22:09:00 +0100288static void i2c_device_shutdown(struct device *dev)
289{
Jean Delvare51298d12009-09-18 22:45:45 +0200290 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100291 struct i2c_driver *driver;
292
Jean Delvare51298d12009-09-18 22:45:45 +0200293 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100294 return;
295 driver = to_i2c_driver(dev->driver);
296 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200297 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100298}
299
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200300#ifdef CONFIG_PM_SLEEP
301static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100302{
Jean Delvare51298d12009-09-18 22:45:45 +0200303 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100304 struct i2c_driver *driver;
305
Jean Delvare51298d12009-09-18 22:45:45 +0200306 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100307 return 0;
308 driver = to_i2c_driver(dev->driver);
309 if (!driver->suspend)
310 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200311 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100312}
313
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200314static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100315{
Jean Delvare51298d12009-09-18 22:45:45 +0200316 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100317 struct i2c_driver *driver;
318
Jean Delvare51298d12009-09-18 22:45:45 +0200319 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100320 return 0;
321 driver = to_i2c_driver(dev->driver);
322 if (!driver->resume)
323 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200324 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100325}
326
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200327static int i2c_device_pm_suspend(struct device *dev)
328{
329 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
330
Mark Brownd529de22011-01-14 22:03:49 +0100331 if (pm)
332 return pm_generic_suspend(dev);
333 else
334 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200335}
336
337static int i2c_device_pm_resume(struct device *dev)
338{
339 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200340
341 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100342 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200343 else
Mark Brownd529de22011-01-14 22:03:49 +0100344 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200345}
346
347static int i2c_device_pm_freeze(struct device *dev)
348{
349 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
350
Mark Brownd529de22011-01-14 22:03:49 +0100351 if (pm)
352 return pm_generic_freeze(dev);
353 else
354 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200355}
356
357static int i2c_device_pm_thaw(struct device *dev)
358{
359 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
360
Mark Brownd529de22011-01-14 22:03:49 +0100361 if (pm)
362 return pm_generic_thaw(dev);
363 else
364 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200365}
366
367static int i2c_device_pm_poweroff(struct device *dev)
368{
369 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
370
Mark Brownd529de22011-01-14 22:03:49 +0100371 if (pm)
372 return pm_generic_poweroff(dev);
373 else
374 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200375}
376
377static int i2c_device_pm_restore(struct device *dev)
378{
379 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200380
381 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100382 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200383 else
Mark Brownd529de22011-01-14 22:03:49 +0100384 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200385}
386#else /* !CONFIG_PM_SLEEP */
387#define i2c_device_pm_suspend NULL
388#define i2c_device_pm_resume NULL
389#define i2c_device_pm_freeze NULL
390#define i2c_device_pm_thaw NULL
391#define i2c_device_pm_poweroff NULL
392#define i2c_device_pm_restore NULL
393#endif /* !CONFIG_PM_SLEEP */
394
David Brownell9c1600e2007-05-01 23:26:31 +0200395static void i2c_client_dev_release(struct device *dev)
396{
397 kfree(to_i2c_client(dev));
398}
399
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100400static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200401show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200402{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200403 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
404 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200405}
406
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100407static ssize_t
408show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200409{
410 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200411 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200412}
413
Jean Delvare4f8cf822009-09-18 22:45:46 +0200414static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200415static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
416
417static struct attribute *i2c_dev_attrs[] = {
418 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200419 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200420 &dev_attr_modalias.attr,
421 NULL
422};
423
424static struct attribute_group i2c_dev_attr_group = {
425 .attrs = i2c_dev_attrs,
426};
427
428static const struct attribute_group *i2c_dev_attr_groups[] = {
429 &i2c_dev_attr_group,
430 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200431};
432
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100433static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100434 .suspend = i2c_device_pm_suspend,
435 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200436 .freeze = i2c_device_pm_freeze,
437 .thaw = i2c_device_pm_thaw,
438 .poweroff = i2c_device_pm_poweroff,
439 .restore = i2c_device_pm_restore,
440 SET_RUNTIME_PM_OPS(
441 pm_generic_runtime_suspend,
442 pm_generic_runtime_resume,
443 pm_generic_runtime_idle
444 )
sonic zhang54067ee2009-12-14 21:17:30 +0100445};
446
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200447struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100448 .name = "i2c",
449 .match = i2c_device_match,
450 .probe = i2c_device_probe,
451 .remove = i2c_device_remove,
452 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100453 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000454};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200455EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000456
Jean Delvare51298d12009-09-18 22:45:45 +0200457static struct device_type i2c_client_type = {
458 .groups = i2c_dev_attr_groups,
459 .uevent = i2c_device_uevent,
460 .release = i2c_client_dev_release,
461};
462
David Brownell9b766b82008-01-27 18:14:51 +0100463
464/**
465 * i2c_verify_client - return parameter as i2c_client, or NULL
466 * @dev: device, probably from some driver model iterator
467 *
468 * When traversing the driver model tree, perhaps using driver model
469 * iterators like @device_for_each_child(), you can't assume very much
470 * about the nodes you find. Use this function to avoid oopses caused
471 * by wrongly treating some non-I2C device as an i2c_client.
472 */
473struct i2c_client *i2c_verify_client(struct device *dev)
474{
Jean Delvare51298d12009-09-18 22:45:45 +0200475 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100476 ? to_i2c_client(dev)
477 : NULL;
478}
479EXPORT_SYMBOL(i2c_verify_client);
480
481
Jean Delvare3a89db52010-06-03 11:33:52 +0200482/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300483 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200484static int i2c_check_client_addr_validity(const struct i2c_client *client)
485{
486 if (client->flags & I2C_CLIENT_TEN) {
487 /* 10-bit address, all values are valid */
488 if (client->addr > 0x3ff)
489 return -EINVAL;
490 } else {
491 /* 7-bit address, reject the general call address */
492 if (client->addr == 0x00 || client->addr > 0x7f)
493 return -EINVAL;
494 }
495 return 0;
496}
497
Jean Delvare656b8762010-06-03 11:33:53 +0200498/* And this is a strict address validity check, used when probing. If a
499 * device uses a reserved address, then it shouldn't be probed. 7-bit
500 * addressing is assumed, 10-bit address devices are rare and should be
501 * explicitly enumerated. */
502static int i2c_check_addr_validity(unsigned short addr)
503{
504 /*
505 * Reserved addresses per I2C specification:
506 * 0x00 General call address / START byte
507 * 0x01 CBUS address
508 * 0x02 Reserved for different bus format
509 * 0x03 Reserved for future purposes
510 * 0x04-0x07 Hs-mode master code
511 * 0x78-0x7b 10-bit slave addressing
512 * 0x7c-0x7f Reserved for future purposes
513 */
514 if (addr < 0x08 || addr > 0x77)
515 return -EINVAL;
516 return 0;
517}
518
Jean Delvare3b5f7942010-06-03 11:33:55 +0200519static int __i2c_check_addr_busy(struct device *dev, void *addrp)
520{
521 struct i2c_client *client = i2c_verify_client(dev);
522 int addr = *(int *)addrp;
523
524 if (client && client->addr == addr)
525 return -EBUSY;
526 return 0;
527}
528
Michael Lawnick08263742010-08-11 18:21:02 +0200529/* walk up mux tree */
530static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
531{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200532 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200533 int result;
534
535 result = device_for_each_child(&adapter->dev, &addr,
536 __i2c_check_addr_busy);
537
Jean Delvare97cc4d42010-10-24 18:16:57 +0200538 if (!result && parent)
539 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200540
541 return result;
542}
543
544/* recurse down mux tree */
545static int i2c_check_mux_children(struct device *dev, void *addrp)
546{
547 int result;
548
549 if (dev->type == &i2c_adapter_type)
550 result = device_for_each_child(dev, addrp,
551 i2c_check_mux_children);
552 else
553 result = __i2c_check_addr_busy(dev, addrp);
554
555 return result;
556}
557
Jean Delvare3b5f7942010-06-03 11:33:55 +0200558static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
559{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200560 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200561 int result = 0;
562
Jean Delvare97cc4d42010-10-24 18:16:57 +0200563 if (parent)
564 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200565
566 if (!result)
567 result = device_for_each_child(&adapter->dev, &addr,
568 i2c_check_mux_children);
569
570 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200571}
572
David Brownell9c1600e2007-05-01 23:26:31 +0200573/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200574 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
575 * @adapter: Target I2C bus segment
576 */
577void i2c_lock_adapter(struct i2c_adapter *adapter)
578{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200579 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
580
581 if (parent)
582 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200583 else
584 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200585}
586EXPORT_SYMBOL_GPL(i2c_lock_adapter);
587
588/**
589 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
590 * @adapter: Target I2C bus segment
591 */
592static int i2c_trylock_adapter(struct i2c_adapter *adapter)
593{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200594 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
595
596 if (parent)
597 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200598 else
599 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200600}
601
602/**
603 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
604 * @adapter: Target I2C bus segment
605 */
606void i2c_unlock_adapter(struct i2c_adapter *adapter)
607{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200608 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
609
610 if (parent)
611 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200612 else
613 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200614}
615EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
616
617/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200618 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200619 * @adap: the adapter managing the device
620 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200621 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200622 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200623 * Create an i2c device. Binding is handled through driver model
624 * probe()/remove() methods. A driver may be bound to this device when we
625 * return from this function, or any later moment (e.g. maybe hotplugging will
626 * load the driver module). This call is not appropriate for use by mainboard
627 * initialization logic, which usually runs during an arch_initcall() long
628 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200629 *
630 * This returns the new i2c client, which may be saved for later use with
631 * i2c_unregister_device(); or NULL to indicate an error.
632 */
633struct i2c_client *
634i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
635{
636 struct i2c_client *client;
637 int status;
638
639 client = kzalloc(sizeof *client, GFP_KERNEL);
640 if (!client)
641 return NULL;
642
643 client->adapter = adap;
644
645 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200646
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200647 if (info->archdata)
648 client->dev.archdata = *info->archdata;
649
Marc Pignatee354252008-08-28 08:33:22 +0200650 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200651 client->addr = info->addr;
652 client->irq = info->irq;
653
David Brownell9c1600e2007-05-01 23:26:31 +0200654 strlcpy(client->name, info->type, sizeof(client->name));
655
Jean Delvare3a89db52010-06-03 11:33:52 +0200656 /* Check for address validity */
657 status = i2c_check_client_addr_validity(client);
658 if (status) {
659 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
660 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
661 goto out_err_silent;
662 }
663
Jean Delvaref8a227e2009-06-19 16:58:18 +0200664 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200665 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200666 if (status)
667 goto out_err;
668
669 client->dev.parent = &client->adapter->dev;
670 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200671 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700672 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100673 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200674
Jean Delvarecbb44512011-11-23 11:33:07 +0100675 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200676 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100677 client->addr | ((client->flags & I2C_CLIENT_TEN)
678 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200679 status = device_register(&client->dev);
680 if (status)
681 goto out_err;
682
Jean Delvaref8a227e2009-06-19 16:58:18 +0200683 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
684 client->name, dev_name(&client->dev));
685
David Brownell9c1600e2007-05-01 23:26:31 +0200686 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200687
688out_err:
689 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
690 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200691out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200692 kfree(client);
693 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200694}
695EXPORT_SYMBOL_GPL(i2c_new_device);
696
697
698/**
699 * i2c_unregister_device - reverse effect of i2c_new_device()
700 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200701 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200702 */
703void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200704{
David Brownella1d9e6e2007-05-01 23:26:30 +0200705 device_unregister(&client->dev);
706}
David Brownell9c1600e2007-05-01 23:26:31 +0200707EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200708
709
Jean Delvare60b129d2008-05-11 20:37:06 +0200710static const struct i2c_device_id dummy_id[] = {
711 { "dummy", 0 },
712 { },
713};
714
Jean Delvared2653e92008-04-29 23:11:39 +0200715static int dummy_probe(struct i2c_client *client,
716 const struct i2c_device_id *id)
717{
718 return 0;
719}
720
721static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100722{
723 return 0;
724}
725
726static struct i2c_driver dummy_driver = {
727 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200728 .probe = dummy_probe,
729 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200730 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100731};
732
733/**
734 * i2c_new_dummy - return a new i2c device bound to a dummy driver
735 * @adapter: the adapter managing the device
736 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100737 * Context: can sleep
738 *
739 * This returns an I2C client bound to the "dummy" driver, intended for use
740 * with devices that consume multiple addresses. Examples of such chips
741 * include various EEPROMS (like 24c04 and 24c08 models).
742 *
743 * These dummy devices have two main uses. First, most I2C and SMBus calls
744 * except i2c_transfer() need a client handle; the dummy will be that handle.
745 * And second, this prevents the specified address from being bound to a
746 * different driver.
747 *
748 * This returns the new i2c client, which should be saved for later use with
749 * i2c_unregister_device(); or NULL to indicate an error.
750 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100751struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100752{
753 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200754 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100755 };
756
David Brownelle9f13732008-01-27 18:14:52 +0100757 return i2c_new_device(adapter, &info);
758}
759EXPORT_SYMBOL_GPL(i2c_new_dummy);
760
David Brownellf37dd802007-02-13 22:09:00 +0100761/* ------------------------------------------------------------------------- */
762
David Brownell16ffadf2007-05-01 23:26:28 +0200763/* I2C bus adapters -- one roots each I2C or SMBUS segment */
764
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200765static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
David Brownellef2c83212007-05-01 23:26:28 +0200767 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 complete(&adap->dev_released);
769}
770
Jean Delvare99cd8e22009-06-19 16:58:20 +0200771/*
Jean Delvare390946b2012-09-10 10:14:02 +0200772 * This function is only needed for mutex_lock_nested, so it is never
773 * called unless locking correctness checking is enabled. Thus we
774 * make it inline to avoid a compiler warning. That's what gcc ends up
775 * doing anyway.
776 */
777static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
778{
779 unsigned int depth = 0;
780
781 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
782 depth++;
783
784 return depth;
785}
786
787/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200788 * Let users instantiate I2C devices through sysfs. This can be used when
789 * platform initialization code doesn't contain the proper data for
790 * whatever reason. Also useful for drivers that do device detection and
791 * detection fails, either because the device uses an unexpected address,
792 * or this is a compatible device with different ID register values.
793 *
794 * Parameter checking may look overzealous, but we really don't want
795 * the user to provide incorrect parameters.
796 */
797static ssize_t
798i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
799 const char *buf, size_t count)
800{
801 struct i2c_adapter *adap = to_i2c_adapter(dev);
802 struct i2c_board_info info;
803 struct i2c_client *client;
804 char *blank, end;
805 int res;
806
Jean Delvare99cd8e22009-06-19 16:58:20 +0200807 memset(&info, 0, sizeof(struct i2c_board_info));
808
809 blank = strchr(buf, ' ');
810 if (!blank) {
811 dev_err(dev, "%s: Missing parameters\n", "new_device");
812 return -EINVAL;
813 }
814 if (blank - buf > I2C_NAME_SIZE - 1) {
815 dev_err(dev, "%s: Invalid device name\n", "new_device");
816 return -EINVAL;
817 }
818 memcpy(info.type, buf, blank - buf);
819
820 /* Parse remaining parameters, reject extra parameters */
821 res = sscanf(++blank, "%hi%c", &info.addr, &end);
822 if (res < 1) {
823 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
824 return -EINVAL;
825 }
826 if (res > 1 && end != '\n') {
827 dev_err(dev, "%s: Extra parameters\n", "new_device");
828 return -EINVAL;
829 }
830
Jean Delvare99cd8e22009-06-19 16:58:20 +0200831 client = i2c_new_device(adap, &info);
832 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200833 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200834
835 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200836 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200837 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200838 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200839 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
840 info.type, info.addr);
841
842 return count;
843}
844
845/*
846 * And of course let the users delete the devices they instantiated, if
847 * they got it wrong. This interface can only be used to delete devices
848 * instantiated by i2c_sysfs_new_device above. This guarantees that we
849 * don't delete devices to which some kernel code still has references.
850 *
851 * Parameter checking may look overzealous, but we really don't want
852 * the user to delete the wrong device.
853 */
854static ssize_t
855i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
856 const char *buf, size_t count)
857{
858 struct i2c_adapter *adap = to_i2c_adapter(dev);
859 struct i2c_client *client, *next;
860 unsigned short addr;
861 char end;
862 int res;
863
864 /* Parse parameters, reject extra parameters */
865 res = sscanf(buf, "%hi%c", &addr, &end);
866 if (res < 1) {
867 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
868 return -EINVAL;
869 }
870 if (res > 1 && end != '\n') {
871 dev_err(dev, "%s: Extra parameters\n", "delete_device");
872 return -EINVAL;
873 }
874
875 /* Make sure the device was added through sysfs */
876 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200877 mutex_lock_nested(&adap->userspace_clients_lock,
878 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200879 list_for_each_entry_safe(client, next, &adap->userspace_clients,
880 detected) {
881 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200882 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
883 "delete_device", client->name, client->addr);
884
885 list_del(&client->detected);
886 i2c_unregister_device(client);
887 res = count;
888 break;
889 }
890 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200891 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200892
893 if (res < 0)
894 dev_err(dev, "%s: Can't find device in list\n",
895 "delete_device");
896 return res;
897}
898
Jean Delvare4f8cf822009-09-18 22:45:46 +0200899static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
900static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
901
902static struct attribute *i2c_adapter_attrs[] = {
903 &dev_attr_name.attr,
904 &dev_attr_new_device.attr,
905 &dev_attr_delete_device.attr,
906 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200907};
908
Jean Delvare4f8cf822009-09-18 22:45:46 +0200909static struct attribute_group i2c_adapter_attr_group = {
910 .attrs = i2c_adapter_attrs,
911};
912
913static const struct attribute_group *i2c_adapter_attr_groups[] = {
914 &i2c_adapter_attr_group,
915 NULL
916};
917
Michael Lawnick08263742010-08-11 18:21:02 +0200918struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200919 .groups = i2c_adapter_attr_groups,
920 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200921};
Michael Lawnick08263742010-08-11 18:21:02 +0200922EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Stephen Warren643dd092012-04-17 12:43:33 -0600924/**
925 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
926 * @dev: device, probably from some driver model iterator
927 *
928 * When traversing the driver model tree, perhaps using driver model
929 * iterators like @device_for_each_child(), you can't assume very much
930 * about the nodes you find. Use this function to avoid oopses caused
931 * by wrongly treating some non-I2C device as an i2c_adapter.
932 */
933struct i2c_adapter *i2c_verify_adapter(struct device *dev)
934{
935 return (dev->type == &i2c_adapter_type)
936 ? to_i2c_adapter(dev)
937 : NULL;
938}
939EXPORT_SYMBOL(i2c_verify_adapter);
940
Jean Delvare2bb50952009-09-18 22:45:46 +0200941#ifdef CONFIG_I2C_COMPAT
942static struct class_compat *i2c_adapter_compat_class;
943#endif
944
David Brownell9c1600e2007-05-01 23:26:31 +0200945static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
946{
947 struct i2c_devinfo *devinfo;
948
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200949 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200950 list_for_each_entry(devinfo, &__i2c_board_list, list) {
951 if (devinfo->busnum == adapter->nr
952 && !i2c_new_device(adapter,
953 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100954 dev_err(&adapter->dev,
955 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200956 devinfo->board_info.addr);
957 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200958 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200959}
960
Jean Delvare69b00892009-12-06 17:06:27 +0100961static int i2c_do_add_adapter(struct i2c_driver *driver,
962 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100963{
Jean Delvare4735c982008-07-14 22:38:36 +0200964 /* Detect supported devices on that bus, and instantiate them */
965 i2c_detect(adap, driver);
966
967 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100968 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +0200969 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
970 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +0100971 dev_warn(&adap->dev, "Please use another way to instantiate "
972 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +0100973 /* We ignore the return code; if it fails, too bad */
974 driver->attach_adapter(adap);
975 }
976 return 0;
977}
978
Jean Delvare69b00892009-12-06 17:06:27 +0100979static int __process_new_adapter(struct device_driver *d, void *data)
980{
981 return i2c_do_add_adapter(to_i2c_driver(d), data);
982}
983
David Brownell6e13e642007-05-01 23:26:31 +0200984static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Jean Delvared6703282010-08-11 18:20:59 +0200986 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
David Brownell1d0b19c2008-10-14 17:30:05 +0200988 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200989 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
990 res = -EAGAIN;
991 goto out_list;
992 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200993
Jean Delvare2236baa2010-11-15 22:40:38 +0100994 /* Sanity checks */
995 if (unlikely(adap->name[0] == '\0')) {
996 pr_err("i2c-core: Attempt to register an adapter with "
997 "no name!\n");
998 return -EINVAL;
999 }
1000 if (unlikely(!adap->algo)) {
1001 pr_err("i2c-core: Attempt to register adapter '%s' with "
1002 "no algo!\n", adap->name);
1003 return -EINVAL;
1004 }
1005
Mika Kuoppala194684e2009-12-06 17:06:22 +01001006 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001007 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001008 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Jean Delvare8fcfef62009-03-28 21:34:43 +01001010 /* Set default timeout to 1 second if not already set */
1011 if (adap->timeout == 0)
1012 adap->timeout = HZ;
1013
Kay Sievers27d9c182009-01-07 14:29:16 +01001014 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001015 adap->dev.bus = &i2c_bus_type;
1016 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001017 res = device_register(&adap->dev);
1018 if (res)
1019 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001021 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1022
Jean Delvare2bb50952009-09-18 22:45:46 +02001023#ifdef CONFIG_I2C_COMPAT
1024 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1025 adap->dev.parent);
1026 if (res)
1027 dev_warn(&adap->dev,
1028 "Failed to create compatibility class link\n");
1029#endif
1030
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301031 /* bus recovery specific initialization */
1032 if (adap->bus_recovery_info) {
1033 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1034
1035 if (!bri->recover_bus) {
1036 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1037 adap->bus_recovery_info = NULL;
1038 goto exit_recovery;
1039 }
1040
1041 /* Generic GPIO recovery */
1042 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1043 if (!gpio_is_valid(bri->scl_gpio)) {
1044 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1045 adap->bus_recovery_info = NULL;
1046 goto exit_recovery;
1047 }
1048
1049 if (gpio_is_valid(bri->sda_gpio))
1050 bri->get_sda = get_sda_gpio_value;
1051 else
1052 bri->get_sda = NULL;
1053
1054 bri->get_scl = get_scl_gpio_value;
1055 bri->set_scl = set_scl_gpio_value;
1056 } else if (!bri->set_scl || !bri->get_scl) {
1057 /* Generic SCL recovery */
1058 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1059 adap->bus_recovery_info = NULL;
1060 }
1061 }
1062
1063exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001064 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +02001065 if (adap->nr < __i2c_first_dynamic_bus_num)
1066 i2c_scan_static_board_info(adap);
1067
Jean Delvare4735c982008-07-14 22:38:36 +02001068 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001069 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001070 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001071 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001072
1073 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001074
Jean Delvareb119c6c2006-08-15 18:26:30 +02001075out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001076 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001077 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001078 mutex_unlock(&core_lock);
1079 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
David Brownell6e13e642007-05-01 23:26:31 +02001082/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001083 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1084 * @adap: the adapter to register (with adap->nr initialized)
1085 * Context: can sleep
1086 *
1087 * See i2c_add_numbered_adapter() for details.
1088 */
1089static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1090{
1091 int id;
1092
1093 mutex_lock(&core_lock);
1094 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1095 GFP_KERNEL);
1096 mutex_unlock(&core_lock);
1097 if (id < 0)
1098 return id == -ENOSPC ? -EBUSY : id;
1099
1100 return i2c_register_adapter(adap);
1101}
1102
1103/**
David Brownell6e13e642007-05-01 23:26:31 +02001104 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1105 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001106 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001107 *
1108 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001109 * doesn't matter or when its bus number is specified by an dt alias.
1110 * Examples of bases when the bus number doesn't matter: I2C adapters
1111 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001112 *
1113 * When this returns zero, a new bus number was allocated and stored
1114 * in adap->nr, and the specified adapter became available for clients.
1115 * Otherwise, a negative errno value is returned.
1116 */
1117int i2c_add_adapter(struct i2c_adapter *adapter)
1118{
Doug Andersonee5c2742013-03-01 08:57:31 -08001119 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001120 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001121
Doug Andersonee5c2742013-03-01 08:57:31 -08001122 if (dev->of_node) {
1123 id = of_alias_get_id(dev->of_node, "i2c");
1124 if (id >= 0) {
1125 adapter->nr = id;
1126 return __i2c_add_numbered_adapter(adapter);
1127 }
1128 }
1129
Jean Delvarecaada322008-01-27 18:14:49 +01001130 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001131 id = idr_alloc(&i2c_adapter_idr, adapter,
1132 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001133 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001134 if (id < 0)
1135 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001136
1137 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001138
David Brownell6e13e642007-05-01 23:26:31 +02001139 return i2c_register_adapter(adapter);
1140}
1141EXPORT_SYMBOL(i2c_add_adapter);
1142
1143/**
1144 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1145 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001146 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001147 *
1148 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001149 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1150 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001151 * is used to properly configure I2C devices.
1152 *
Grant Likely488bf312011-07-25 17:49:43 +02001153 * If the requested bus number is set to -1, then this function will behave
1154 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1155 *
David Brownell6e13e642007-05-01 23:26:31 +02001156 * If no devices have pre-been declared for this bus, then be sure to
1157 * register the adapter before any dynamically allocated ones. Otherwise
1158 * the required bus ID may not be available.
1159 *
1160 * When this returns zero, the specified adapter became available for
1161 * clients using the bus number provided in adap->nr. Also, the table
1162 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1163 * and the appropriate driver model device nodes are created. Otherwise, a
1164 * negative errno value is returned.
1165 */
1166int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1167{
Grant Likely488bf312011-07-25 17:49:43 +02001168 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1169 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001170
Doug Andersonee5c2742013-03-01 08:57:31 -08001171 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001172}
1173EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1174
Jean Delvare69b00892009-12-06 17:06:27 +01001175static int i2c_do_del_adapter(struct i2c_driver *driver,
1176 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001177{
Jean Delvare4735c982008-07-14 22:38:36 +02001178 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001179 int res;
1180
Jean Delvareacec2112009-03-28 21:34:40 +01001181 /* Remove the devices we created ourselves as the result of hardware
1182 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001183 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1184 if (client->adapter == adapter) {
1185 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1186 client->name, client->addr);
1187 list_del(&client->detected);
1188 i2c_unregister_device(client);
1189 }
1190 }
1191
Jean Delvare026526f2008-01-27 18:14:49 +01001192 if (!driver->detach_adapter)
1193 return 0;
Jean Delvarea920ff42011-04-17 10:20:19 +02001194 dev_warn(&adapter->dev, "%s: detach_adapter method is deprecated\n",
1195 driver->driver.name);
Jean Delvare026526f2008-01-27 18:14:49 +01001196 res = driver->detach_adapter(adapter);
1197 if (res)
1198 dev_err(&adapter->dev, "detach_adapter failed (%d) "
1199 "for driver [%s]\n", res, driver->driver.name);
1200 return res;
1201}
1202
Jean Delvaree549c2b2009-06-19 16:58:19 +02001203static int __unregister_client(struct device *dev, void *dummy)
1204{
1205 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001206 if (client && strcmp(client->name, "dummy"))
1207 i2c_unregister_device(client);
1208 return 0;
1209}
1210
1211static int __unregister_dummy(struct device *dev, void *dummy)
1212{
1213 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001214 if (client)
1215 i2c_unregister_device(client);
1216 return 0;
1217}
1218
Jean Delvare69b00892009-12-06 17:06:27 +01001219static int __process_removed_adapter(struct device_driver *d, void *data)
1220{
1221 return i2c_do_del_adapter(to_i2c_driver(d), data);
1222}
1223
David Brownelld64f73b2007-07-12 14:12:28 +02001224/**
1225 * i2c_del_adapter - unregister I2C adapter
1226 * @adap: the adapter being unregistered
1227 * Context: can sleep
1228 *
1229 * This unregisters an I2C adapter which was previously registered
1230 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232int i2c_del_adapter(struct i2c_adapter *adap)
1233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 int res = 0;
Jean Delvare35fc37f2009-06-19 16:58:19 +02001235 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001236 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001239 mutex_lock(&core_lock);
1240 found = idr_find(&i2c_adapter_idr, adap->nr);
1241 mutex_unlock(&core_lock);
1242 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001243 pr_debug("i2c-core: attempting to delete unregistered "
1244 "adapter [%s]\n", adap->name);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001245 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Jean Delvare026526f2008-01-27 18:14:49 +01001248 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001249 mutex_lock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001250 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001251 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001252 mutex_unlock(&core_lock);
Jean Delvare026526f2008-01-27 18:14:49 +01001253 if (res)
Jean Delvare35fc37f2009-06-19 16:58:19 +02001254 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001256 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001257 mutex_lock_nested(&adap->userspace_clients_lock,
1258 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001259 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1260 detected) {
1261 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1262 client->addr);
1263 list_del(&client->detected);
1264 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001265 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001266 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001267
Jean Delvaree549c2b2009-06-19 16:58:19 +02001268 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001269 * check the returned value. This is a two-pass process, because
1270 * we can't remove the dummy devices during the first pass: they
1271 * could have been instantiated by real devices wishing to clean
1272 * them up properly, so we give them a chance to do that first. */
Jean Delvaree549c2b2009-06-19 16:58:19 +02001273 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
Jean Delvare5219bf82011-01-14 22:03:49 +01001274 res = device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Jean Delvare2bb50952009-09-18 22:45:46 +02001276#ifdef CONFIG_I2C_COMPAT
1277 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1278 adap->dev.parent);
1279#endif
1280
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001281 /* device name is gone after device_unregister */
1282 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 /* clean up the sysfs representation */
1285 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* wait for sysfs to drop all references */
1289 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
David Brownell6e13e642007-05-01 23:26:31 +02001291 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001292 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001294 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001296 /* Clear the device structure in case this adapter is ever going to be
1297 added again */
1298 memset(&adap->dev, 0, sizeof(adap->dev));
1299
Jean Delvare35fc37f2009-06-19 16:58:19 +02001300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
David Brownellc0564602007-05-01 23:26:31 +02001302EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304
David Brownell7b4fbc52007-05-01 23:26:30 +02001305/* ------------------------------------------------------------------------- */
1306
Jean Delvare7ae31482011-03-20 14:50:52 +01001307int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1308{
1309 int res;
1310
1311 mutex_lock(&core_lock);
1312 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1313 mutex_unlock(&core_lock);
1314
1315 return res;
1316}
1317EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1318
Jean Delvare69b00892009-12-06 17:06:27 +01001319static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001320{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001321 if (dev->type != &i2c_adapter_type)
1322 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001323 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001324}
1325
David Brownell7b4fbc52007-05-01 23:26:30 +02001326/*
1327 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001328 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 */
1330
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001331int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001333 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
David Brownell1d0b19c2008-10-14 17:30:05 +02001335 /* Can't register until after driver model init */
1336 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1337 return -EAGAIN;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001340 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Jean Delvare729d6dd2009-06-19 16:58:18 +02001343 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001344 * will have called probe() for all matching-but-unbound devices.
1345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 res = driver_register(&driver->driver);
1347 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001348 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001349
Mark Brownf4e8db32011-01-14 22:03:50 +01001350 /* Drivers should switch to dev_pm_ops instead. */
1351 if (driver->suspend)
1352 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1353 driver->driver.name);
1354 if (driver->resume)
1355 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1356 driver->driver.name);
1357
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001358 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Jean Delvare4735c982008-07-14 22:38:36 +02001360 INIT_LIST_HEAD(&driver->clients);
1361 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001362 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001363
Jean Delvare7eebcb72006-02-05 23:28:21 +01001364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001366EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Jean Delvare69b00892009-12-06 17:06:27 +01001368static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001369{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001370 if (dev->type != &i2c_adapter_type)
1371 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001372 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001373}
1374
David Brownella1d9e6e2007-05-01 23:26:30 +02001375/**
1376 * i2c_del_driver - unregister I2C driver
1377 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001378 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001379 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001380void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Jean Delvare7ae31482011-03-20 14:50:52 +01001382 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001385 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
David Brownellc0564602007-05-01 23:26:31 +02001387EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
David Brownell7b4fbc52007-05-01 23:26:30 +02001389/* ------------------------------------------------------------------------- */
1390
Jean Delvaree48d3312008-01-27 18:14:48 +01001391/**
1392 * i2c_use_client - increments the reference count of the i2c client structure
1393 * @client: the client being referenced
1394 *
1395 * Each live reference to a client should be refcounted. The driver model does
1396 * that automatically as part of driver binding, so that most drivers don't
1397 * need to do this explicitly: they hold a reference until they're unbound
1398 * from the device.
1399 *
1400 * A pointer to the client with the incremented reference counter is returned.
1401 */
1402struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
David Brownell6ea438e2008-07-14 22:38:24 +02001404 if (client && get_device(&client->dev))
1405 return client;
1406 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407}
David Brownellc0564602007-05-01 23:26:31 +02001408EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Jean Delvaree48d3312008-01-27 18:14:48 +01001410/**
1411 * i2c_release_client - release a use of the i2c client structure
1412 * @client: the client being no longer referenced
1413 *
1414 * Must be called when a user of a client is finished with it.
1415 */
1416void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
David Brownell6ea438e2008-07-14 22:38:24 +02001418 if (client)
1419 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
David Brownellc0564602007-05-01 23:26:31 +02001421EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
David Brownell9b766b82008-01-27 18:14:51 +01001423struct i2c_cmd_arg {
1424 unsigned cmd;
1425 void *arg;
1426};
1427
1428static int i2c_cmd(struct device *dev, void *_arg)
1429{
1430 struct i2c_client *client = i2c_verify_client(dev);
1431 struct i2c_cmd_arg *arg = _arg;
1432
1433 if (client && client->driver && client->driver->command)
1434 client->driver->command(client, arg->cmd, arg->arg);
1435 return 0;
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1439{
David Brownell9b766b82008-01-27 18:14:51 +01001440 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
David Brownell9b766b82008-01-27 18:14:51 +01001442 cmd_arg.cmd = cmd;
1443 cmd_arg.arg = arg;
1444 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
David Brownellc0564602007-05-01 23:26:31 +02001446EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448static int __init i2c_init(void)
1449{
1450 int retval;
1451
1452 retval = bus_register(&i2c_bus_type);
1453 if (retval)
1454 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001455#ifdef CONFIG_I2C_COMPAT
1456 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1457 if (!i2c_adapter_compat_class) {
1458 retval = -ENOMEM;
1459 goto bus_err;
1460 }
1461#endif
David Brownelle9f13732008-01-27 18:14:52 +01001462 retval = i2c_add_driver(&dummy_driver);
1463 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001464 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001465 return 0;
1466
Jean Delvare2bb50952009-09-18 22:45:46 +02001467class_err:
1468#ifdef CONFIG_I2C_COMPAT
1469 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001470bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001471#endif
David Brownelle9f13732008-01-27 18:14:52 +01001472 bus_unregister(&i2c_bus_type);
1473 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
1476static void __exit i2c_exit(void)
1477{
David Brownelle9f13732008-01-27 18:14:52 +01001478 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001479#ifdef CONFIG_I2C_COMPAT
1480 class_compat_unregister(i2c_adapter_compat_class);
1481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 bus_unregister(&i2c_bus_type);
1483}
1484
David Brownella10f9e72008-10-14 17:30:06 +02001485/* We must initialize early, because some subsystems register i2c drivers
1486 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1487 */
1488postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489module_exit(i2c_exit);
1490
1491/* ----------------------------------------------------
1492 * the functional interface to the i2c busses.
1493 * ----------------------------------------------------
1494 */
1495
David Brownella1cdeda2008-07-14 22:38:24 +02001496/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001497 * __i2c_transfer - unlocked flavor of i2c_transfer
1498 * @adap: Handle to I2C bus
1499 * @msgs: One or more messages to execute before STOP is issued to
1500 * terminate the operation; each message begins with a START.
1501 * @num: Number of messages to be executed.
1502 *
1503 * Returns negative errno, else the number of messages executed.
1504 *
1505 * Adapter lock must be held when calling this function. No debug logging
1506 * takes place. adap->algo->master_xfer existence isn't checked.
1507 */
1508int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1509{
1510 unsigned long orig_jiffies;
1511 int ret, try;
1512
1513 /* Retry automatically on arbitration loss */
1514 orig_jiffies = jiffies;
1515 for (ret = 0, try = 0; try <= adap->retries; try++) {
1516 ret = adap->algo->master_xfer(adap, msgs, num);
1517 if (ret != -EAGAIN)
1518 break;
1519 if (time_after(jiffies, orig_jiffies + adap->timeout))
1520 break;
1521 }
1522
1523 return ret;
1524}
1525EXPORT_SYMBOL(__i2c_transfer);
1526
1527/**
David Brownella1cdeda2008-07-14 22:38:24 +02001528 * i2c_transfer - execute a single or combined I2C message
1529 * @adap: Handle to I2C bus
1530 * @msgs: One or more messages to execute before STOP is issued to
1531 * terminate the operation; each message begins with a START.
1532 * @num: Number of messages to be executed.
1533 *
1534 * Returns negative errno, else the number of messages executed.
1535 *
1536 * Note that there is no requirement that each message be sent to
1537 * the same slave address, although that is the most common model.
1538 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001539int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001541 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
David Brownella1cdeda2008-07-14 22:38:24 +02001543 /* REVISIT the fault reporting model here is weak:
1544 *
1545 * - When we get an error after receiving N bytes from a slave,
1546 * there is no way to report "N".
1547 *
1548 * - When we get a NAK after transmitting N bytes to a slave,
1549 * there is no way to report "N" ... or to let the master
1550 * continue executing the rest of this combined message, if
1551 * that's the appropriate response.
1552 *
1553 * - When for example "num" is two and we successfully complete
1554 * the first message but get an error part way through the
1555 * second, it's unclear whether that should be reported as
1556 * one (discarding status on the second message) or errno
1557 * (discarding status on the first one).
1558 */
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (adap->algo->master_xfer) {
1561#ifdef DEBUG
1562 for (ret = 0; ret < num; ret++) {
1563 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001564 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1565 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1566 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568#endif
1569
Mike Rapoportcea443a2008-01-27 18:14:50 +01001570 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001571 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001572 if (!ret)
1573 /* I2C activity is ongoing. */
1574 return -EAGAIN;
1575 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001576 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001577 }
1578
Jean Delvareb37d2a32012-06-29 07:47:19 -03001579 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001580 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 return ret;
1583 } else {
1584 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001585 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587}
David Brownellc0564602007-05-01 23:26:31 +02001588EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
David Brownella1cdeda2008-07-14 22:38:24 +02001590/**
1591 * i2c_master_send - issue a single I2C message in master transmit mode
1592 * @client: Handle to slave device
1593 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001594 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001595 *
1596 * Returns negative errno, or else the number of bytes written.
1597 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001598int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001601 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct i2c_msg msg;
1603
Jean Delvare815f55f2005-05-07 22:58:46 +02001604 msg.addr = client->addr;
1605 msg.flags = client->flags & I2C_M_TEN;
1606 msg.len = count;
1607 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001608
Jean Delvare815f55f2005-05-07 22:58:46 +02001609 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001611 /*
1612 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1613 * transmitted, else error code.
1614 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001615 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
David Brownellc0564602007-05-01 23:26:31 +02001617EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
David Brownella1cdeda2008-07-14 22:38:24 +02001619/**
1620 * i2c_master_recv - issue a single I2C message in master receive mode
1621 * @client: Handle to slave device
1622 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001623 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001624 *
1625 * Returns negative errno, or else the number of bytes read.
1626 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001627int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Farid Hammane7225acf2010-05-21 18:40:58 +02001629 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct i2c_msg msg;
1631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Jean Delvare815f55f2005-05-07 22:58:46 +02001633 msg.addr = client->addr;
1634 msg.flags = client->flags & I2C_M_TEN;
1635 msg.flags |= I2C_M_RD;
1636 msg.len = count;
1637 msg.buf = buf;
1638
1639 ret = i2c_transfer(adap, &msg, 1);
1640
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001641 /*
1642 * If everything went ok (i.e. 1 msg received), return #bytes received,
1643 * else error code.
1644 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001645 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
David Brownellc0564602007-05-01 23:26:31 +02001647EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/* ----------------------------------------------------
1650 * the i2c address scanning function
1651 * Will not work for 10-bit addresses!
1652 * ----------------------------------------------------
1653 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001654
Jean Delvare63e4e802010-06-03 11:33:51 +02001655/*
1656 * Legacy default probe function, mostly relevant for SMBus. The default
1657 * probe method is a quick write, but it is known to corrupt the 24RF08
1658 * EEPROMs due to a state machine bug, and could also irreversibly
1659 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1660 * we use a short byte read instead. Also, some bus drivers don't implement
1661 * quick write, so we fallback to a byte read in that case too.
1662 * On x86, there is another special case for FSC hardware monitoring chips,
1663 * which want regular byte reads (address 0x73.) Fortunately, these are the
1664 * only known chips using this I2C address on PC hardware.
1665 * Returns 1 if probe succeeded, 0 if not.
1666 */
1667static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1668{
1669 int err;
1670 union i2c_smbus_data dummy;
1671
1672#ifdef CONFIG_X86
1673 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1674 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1675 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1676 I2C_SMBUS_BYTE_DATA, &dummy);
1677 else
1678#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001679 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1680 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001681 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1682 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001683 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1684 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1685 I2C_SMBUS_BYTE, &dummy);
1686 else {
1687 dev_warn(&adap->dev, "No suitable probing method supported\n");
1688 err = -EOPNOTSUPP;
1689 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001690
1691 return err >= 0;
1692}
1693
Jean Delvareccfbbd02009-12-06 17:06:25 +01001694static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001695 struct i2c_driver *driver)
1696{
1697 struct i2c_board_info info;
1698 struct i2c_adapter *adapter = temp_client->adapter;
1699 int addr = temp_client->addr;
1700 int err;
1701
1702 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001703 err = i2c_check_addr_validity(addr);
1704 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001705 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1706 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001707 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001708 }
1709
1710 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001711 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001712 return 0;
1713
Jean Delvareccfbbd02009-12-06 17:06:25 +01001714 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001715 if (!i2c_default_probe(adapter, addr))
1716 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001717
1718 /* Finally call the custom detection function */
1719 memset(&info, 0, sizeof(struct i2c_board_info));
1720 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001721 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001722 if (err) {
1723 /* -ENODEV is returned if the detection fails. We catch it
1724 here as this isn't an error. */
1725 return err == -ENODEV ? 0 : err;
1726 }
1727
1728 /* Consistency check */
1729 if (info.type[0] == '\0') {
1730 dev_err(&adapter->dev, "%s detection function provided "
1731 "no name for 0x%x\n", driver->driver.name,
1732 addr);
1733 } else {
1734 struct i2c_client *client;
1735
1736 /* Detection succeeded, instantiate the device */
1737 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1738 info.type, info.addr);
1739 client = i2c_new_device(adapter, &info);
1740 if (client)
1741 list_add_tail(&client->detected, &driver->clients);
1742 else
1743 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1744 info.type, info.addr);
1745 }
1746 return 0;
1747}
1748
1749static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1750{
Jean Delvarec3813d62009-12-14 21:17:25 +01001751 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001752 struct i2c_client *temp_client;
1753 int i, err = 0;
1754 int adap_id = i2c_adapter_id(adapter);
1755
Jean Delvarec3813d62009-12-14 21:17:25 +01001756 address_list = driver->address_list;
1757 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001758 return 0;
1759
Jean Delvare51b54ba2010-10-24 18:16:58 +02001760 /* Stop here if the classes do not match */
1761 if (!(adapter->class & driver->class))
1762 return 0;
1763
Jean Delvare4735c982008-07-14 22:38:36 +02001764 /* Set up a temporary client to help detect callback */
1765 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1766 if (!temp_client)
1767 return -ENOMEM;
1768 temp_client->adapter = adapter;
1769
Jean Delvarec3813d62009-12-14 21:17:25 +01001770 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001771 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001772 "addr 0x%02x\n", adap_id, address_list[i]);
1773 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001774 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001775 if (unlikely(err))
1776 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001777 }
1778
Jean Delvare4735c982008-07-14 22:38:36 +02001779 kfree(temp_client);
1780 return err;
1781}
1782
Jean Delvared44f19d2010-08-11 18:20:57 +02001783int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1784{
1785 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1786 I2C_SMBUS_QUICK, NULL) >= 0;
1787}
1788EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1789
Jean Delvare12b5053a2007-05-01 23:26:31 +02001790struct i2c_client *
1791i2c_new_probed_device(struct i2c_adapter *adap,
1792 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001793 unsigned short const *addr_list,
1794 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001795{
1796 int i;
1797
Jean Delvare8031d792010-08-11 18:21:00 +02001798 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001799 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001800
Jean Delvare12b5053a2007-05-01 23:26:31 +02001801 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1802 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001803 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001804 dev_warn(&adap->dev, "Invalid 7-bit address "
1805 "0x%02x\n", addr_list[i]);
1806 continue;
1807 }
1808
1809 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001810 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001811 dev_dbg(&adap->dev, "Address 0x%02x already in "
1812 "use, not probing\n", addr_list[i]);
1813 continue;
1814 }
1815
Jean Delvare63e4e802010-06-03 11:33:51 +02001816 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001817 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001818 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001819 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001820
1821 if (addr_list[i] == I2C_CLIENT_END) {
1822 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1823 return NULL;
1824 }
1825
1826 info->addr = addr_list[i];
1827 return i2c_new_device(adap, info);
1828}
1829EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1830
Jean Delvared735b342011-03-20 14:50:52 +01001831struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001834
Jean Delvarecaada322008-01-27 18:14:49 +01001835 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001836 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001837 if (adapter && !try_module_get(adapter->owner))
1838 adapter = NULL;
1839
Jean Delvarecaada322008-01-27 18:14:49 +01001840 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001841 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
David Brownellc0564602007-05-01 23:26:31 +02001843EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845void i2c_put_adapter(struct i2c_adapter *adap)
1846{
1847 module_put(adap->owner);
1848}
David Brownellc0564602007-05-01 23:26:31 +02001849EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851/* The SMBus parts */
1852
David Brownell438d6c22006-12-10 21:21:31 +01001853#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001854static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001857
Farid Hammane7225acf2010-05-21 18:40:58 +02001858 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001859 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 data = data ^ POLY;
1861 data = data << 1;
1862 }
1863 return (u8)(data >> 8);
1864}
1865
Jean Delvare421ef472005-10-26 21:28:55 +02001866/* Incremental CRC8 over count bytes in the array pointed to by p */
1867static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 int i;
1870
Farid Hammane7225acf2010-05-21 18:40:58 +02001871 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001872 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 return crc;
1874}
1875
Jean Delvare421ef472005-10-26 21:28:55 +02001876/* Assume a 7-bit address, which is reasonable for SMBus */
1877static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Jean Delvare421ef472005-10-26 21:28:55 +02001879 /* The address will be sent first */
1880 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1881 pec = i2c_smbus_pec(pec, &addr, 1);
1882
1883 /* The data buffer follows */
1884 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Jean Delvare421ef472005-10-26 21:28:55 +02001887/* Used for write only transactions */
1888static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Jean Delvare421ef472005-10-26 21:28:55 +02001890 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1891 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Jean Delvare421ef472005-10-26 21:28:55 +02001894/* Return <0 on CRC error
1895 If there was a write before this read (most cases) we need to take the
1896 partial CRC from the write part into account.
1897 Note that this function does modify the message (we need to decrease the
1898 message length to hide the CRC byte from the caller). */
1899static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Jean Delvare421ef472005-10-26 21:28:55 +02001901 u8 rpec = msg->buf[--msg->len];
1902 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (rpec != cpec) {
1905 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1906 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001907 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
David Brownell438d6c22006-12-10 21:21:31 +01001909 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
David Brownella1cdeda2008-07-14 22:38:24 +02001912/**
1913 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1914 * @client: Handle to slave device
1915 *
1916 * This executes the SMBus "receive byte" protocol, returning negative errno
1917 * else the byte received from the device.
1918 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001919s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
1921 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001922 int status;
1923
1924 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1925 I2C_SMBUS_READ, 0,
1926 I2C_SMBUS_BYTE, &data);
1927 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
David Brownellc0564602007-05-01 23:26:31 +02001929EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
David Brownella1cdeda2008-07-14 22:38:24 +02001931/**
1932 * i2c_smbus_write_byte - SMBus "send byte" protocol
1933 * @client: Handle to slave device
1934 * @value: Byte to be sent
1935 *
1936 * This executes the SMBus "send byte" protocol, returning negative errno
1937 * else zero on success.
1938 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001939s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
Farid Hammane7225acf2010-05-21 18:40:58 +02001941 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001942 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
David Brownellc0564602007-05-01 23:26:31 +02001944EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
David Brownella1cdeda2008-07-14 22:38:24 +02001946/**
1947 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1948 * @client: Handle to slave device
1949 * @command: Byte interpreted by slave
1950 *
1951 * This executes the SMBus "read byte" protocol, returning negative errno
1952 * else a data byte received from the device.
1953 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001954s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001957 int status;
1958
1959 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1960 I2C_SMBUS_READ, command,
1961 I2C_SMBUS_BYTE_DATA, &data);
1962 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
David Brownellc0564602007-05-01 23:26:31 +02001964EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
David Brownella1cdeda2008-07-14 22:38:24 +02001966/**
1967 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1968 * @client: Handle to slave device
1969 * @command: Byte interpreted by slave
1970 * @value: Byte being written
1971 *
1972 * This executes the SMBus "write byte" protocol, returning negative errno
1973 * else zero on success.
1974 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001975s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
1976 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 union i2c_smbus_data data;
1979 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001980 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1981 I2C_SMBUS_WRITE, command,
1982 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
David Brownellc0564602007-05-01 23:26:31 +02001984EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
David Brownella1cdeda2008-07-14 22:38:24 +02001986/**
1987 * i2c_smbus_read_word_data - SMBus "read word" protocol
1988 * @client: Handle to slave device
1989 * @command: Byte interpreted by slave
1990 *
1991 * This executes the SMBus "read word" protocol, returning negative errno
1992 * else a 16-bit unsigned "word" received from the device.
1993 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001994s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001997 int status;
1998
1999 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2000 I2C_SMBUS_READ, command,
2001 I2C_SMBUS_WORD_DATA, &data);
2002 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
David Brownellc0564602007-05-01 23:26:31 +02002004EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
David Brownella1cdeda2008-07-14 22:38:24 +02002006/**
2007 * i2c_smbus_write_word_data - SMBus "write word" protocol
2008 * @client: Handle to slave device
2009 * @command: Byte interpreted by slave
2010 * @value: 16-bit "word" being written
2011 *
2012 * This executes the SMBus "write word" protocol, returning negative errno
2013 * else zero on success.
2014 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002015s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2016 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 union i2c_smbus_data data;
2019 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002020 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2021 I2C_SMBUS_WRITE, command,
2022 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
David Brownellc0564602007-05-01 23:26:31 +02002024EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
David Brownella64ec072007-10-13 23:56:31 +02002026/**
David Brownella1cdeda2008-07-14 22:38:24 +02002027 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002028 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002029 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002030 * @values: Byte array into which data will be read; big enough to hold
2031 * the data returned by the slave. SMBus allows at most 32 bytes.
2032 *
David Brownella1cdeda2008-07-14 22:38:24 +02002033 * This executes the SMBus "block read" protocol, returning negative errno
2034 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002035 *
2036 * Note that using this function requires that the client's adapter support
2037 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2038 * support this; its emulation through I2C messaging relies on a specific
2039 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2040 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002041s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002042 u8 *values)
2043{
2044 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002045 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002046
David Brownell24a5bb72008-07-14 22:38:23 +02002047 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2048 I2C_SMBUS_READ, command,
2049 I2C_SMBUS_BLOCK_DATA, &data);
2050 if (status)
2051 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002052
2053 memcpy(values, &data.block[1], data.block[0]);
2054 return data.block[0];
2055}
2056EXPORT_SYMBOL(i2c_smbus_read_block_data);
2057
David Brownella1cdeda2008-07-14 22:38:24 +02002058/**
2059 * i2c_smbus_write_block_data - SMBus "block write" protocol
2060 * @client: Handle to slave device
2061 * @command: Byte interpreted by slave
2062 * @length: Size of data block; SMBus allows at most 32 bytes
2063 * @values: Byte array which will be written.
2064 *
2065 * This executes the SMBus "block write" protocol, returning negative errno
2066 * else zero on success.
2067 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002068s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002069 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (length > I2C_SMBUS_BLOCK_MAX)
2074 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002076 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002077 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2078 I2C_SMBUS_WRITE, command,
2079 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}
David Brownellc0564602007-05-01 23:26:31 +02002081EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002084s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002085 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002088 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002089
Jean Delvare4b2643d2007-07-12 14:12:29 +02002090 if (length > I2C_SMBUS_BLOCK_MAX)
2091 length = I2C_SMBUS_BLOCK_MAX;
2092 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002093 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2094 I2C_SMBUS_READ, command,
2095 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2096 if (status < 0)
2097 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002098
2099 memcpy(values, &data.block[1], data.block[0]);
2100 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
David Brownellc0564602007-05-01 23:26:31 +02002102EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Jean Delvare0cc43a12011-01-10 22:11:23 +01002104s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002105 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002106{
2107 union i2c_smbus_data data;
2108
2109 if (length > I2C_SMBUS_BLOCK_MAX)
2110 length = I2C_SMBUS_BLOCK_MAX;
2111 data.block[0] = length;
2112 memcpy(data.block + 1, values, length);
2113 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2114 I2C_SMBUS_WRITE, command,
2115 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2116}
David Brownellc0564602007-05-01 23:26:31 +02002117EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002118
David Brownell438d6c22006-12-10 21:21:31 +01002119/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002121static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2122 unsigned short flags,
2123 char read_write, u8 command, int size,
2124 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 /* So we need to generate a series of msgs. In the case of writing, we
2127 need to use only one message; when reading, we need two. We initialize
2128 most things with sane defaults, to keep the code below somewhat
2129 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002130 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2131 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002132 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002134 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002135 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002136 struct i2c_msg msg[2] = {
2137 {
2138 .addr = addr,
2139 .flags = flags,
2140 .len = 1,
2141 .buf = msgbuf0,
2142 }, {
2143 .addr = addr,
2144 .flags = flags | I2C_M_RD,
2145 .len = 0,
2146 .buf = msgbuf1,
2147 },
2148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002151 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 case I2C_SMBUS_QUICK:
2153 msg[0].len = 0;
2154 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002155 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2156 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 num = 1;
2158 break;
2159 case I2C_SMBUS_BYTE:
2160 if (read_write == I2C_SMBUS_READ) {
2161 /* Special case: only a read! */
2162 msg[0].flags = I2C_M_RD | flags;
2163 num = 1;
2164 }
2165 break;
2166 case I2C_SMBUS_BYTE_DATA:
2167 if (read_write == I2C_SMBUS_READ)
2168 msg[1].len = 1;
2169 else {
2170 msg[0].len = 2;
2171 msgbuf0[1] = data->byte;
2172 }
2173 break;
2174 case I2C_SMBUS_WORD_DATA:
2175 if (read_write == I2C_SMBUS_READ)
2176 msg[1].len = 2;
2177 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002178 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002180 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182 break;
2183 case I2C_SMBUS_PROC_CALL:
2184 num = 2; /* Special case */
2185 read_write = I2C_SMBUS_READ;
2186 msg[0].len = 3;
2187 msg[1].len = 2;
2188 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002189 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 break;
2191 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002193 msg[1].flags |= I2C_M_RECV_LEN;
2194 msg[1].len = 1; /* block length will be added by
2195 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 } else {
2197 msg[0].len = data->block[0] + 2;
2198 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002199 dev_err(&adapter->dev,
2200 "Invalid block write size %d\n",
2201 data->block[0]);
2202 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002204 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 msgbuf0[i] = data->block[i-1];
2206 }
2207 break;
2208 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002209 num = 2; /* Another special case */
2210 read_write = I2C_SMBUS_READ;
2211 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002212 dev_err(&adapter->dev,
2213 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002214 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002215 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002216 }
2217 msg[0].len = data->block[0] + 2;
2218 for (i = 1; i < msg[0].len; i++)
2219 msgbuf0[i] = data->block[i-1];
2220 msg[1].flags |= I2C_M_RECV_LEN;
2221 msg[1].len = 1; /* block length will be added by
2222 the underlying bus driver */
2223 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 case I2C_SMBUS_I2C_BLOCK_DATA:
2225 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002226 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 } else {
2228 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002229 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002230 dev_err(&adapter->dev,
2231 "Invalid block write size %d\n",
2232 data->block[0]);
2233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 }
2235 for (i = 1; i <= data->block[0]; i++)
2236 msgbuf0[i] = data->block[i];
2237 }
2238 break;
2239 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002240 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2241 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243
Jean Delvare421ef472005-10-26 21:28:55 +02002244 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2245 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2246 if (i) {
2247 /* Compute PEC if first message is a write */
2248 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002249 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002250 i2c_smbus_add_pec(&msg[0]);
2251 else /* Write followed by read */
2252 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2253 }
2254 /* Ask for PEC if last message is a read */
2255 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002256 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002257 }
2258
David Brownell24a5bb72008-07-14 22:38:23 +02002259 status = i2c_transfer(adapter, msg, num);
2260 if (status < 0)
2261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jean Delvare421ef472005-10-26 21:28:55 +02002263 /* Check PEC if last message is a read */
2264 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002265 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2266 if (status < 0)
2267 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002268 }
2269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002271 switch (size) {
2272 case I2C_SMBUS_BYTE:
2273 data->byte = msgbuf0[0];
2274 break;
2275 case I2C_SMBUS_BYTE_DATA:
2276 data->byte = msgbuf1[0];
2277 break;
2278 case I2C_SMBUS_WORD_DATA:
2279 case I2C_SMBUS_PROC_CALL:
2280 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2281 break;
2282 case I2C_SMBUS_I2C_BLOCK_DATA:
2283 for (i = 0; i < data->block[0]; i++)
2284 data->block[i+1] = msgbuf1[i];
2285 break;
2286 case I2C_SMBUS_BLOCK_DATA:
2287 case I2C_SMBUS_BLOCK_PROC_CALL:
2288 for (i = 0; i < msgbuf1[0] + 1; i++)
2289 data->block[i] = msgbuf1[i];
2290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 return 0;
2293}
2294
David Brownella1cdeda2008-07-14 22:38:24 +02002295/**
2296 * i2c_smbus_xfer - execute SMBus protocol operations
2297 * @adapter: Handle to I2C bus
2298 * @addr: Address of SMBus slave on that bus
2299 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2300 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2301 * @command: Byte interpreted by slave, for protocols which use such bytes
2302 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2303 * @data: Data to be read or written
2304 *
2305 * This executes an SMBus protocol operation, and returns a negative
2306 * errno code else zero on success.
2307 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002308s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002309 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002310 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002312 unsigned long orig_jiffies;
2313 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002316 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002319 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002320
2321 /* Retry automatically on arbitration loss */
2322 orig_jiffies = jiffies;
2323 for (res = 0, try = 0; try <= adapter->retries; try++) {
2324 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2325 read_write, command,
2326 protocol, data);
2327 if (res != -EAGAIN)
2328 break;
2329 if (time_after(jiffies,
2330 orig_jiffies + adapter->timeout))
2331 break;
2332 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002333 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002335 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2336 return res;
2337 /*
2338 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2339 * implement native support for the SMBus operation.
2340 */
2341 }
2342
2343 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2344 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2349MODULE_DESCRIPTION("I2C-Bus main module");
2350MODULE_LICENSE("GPL");