blob: 6b63cc7eb71e38ff31253c0e6af496b3b3b0bccb [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 Rapoportcea443a82008-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
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000052 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
97/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +020098static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +020099{
100 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200101
Jean Delvareeb8a7902008-05-18 20:49:41 +0200102 if (add_uevent_var(env, "MODALIAS=%s%s",
103 I2C_MODULE_PREFIX, client->name))
104 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200105 dev_dbg(dev, "uevent\n");
106 return 0;
107}
108
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530109/* i2c bus recovery routines */
110static int get_scl_gpio_value(struct i2c_adapter *adap)
111{
112 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
113}
114
115static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
116{
117 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
118}
119
120static int get_sda_gpio_value(struct i2c_adapter *adap)
121{
122 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
123}
124
125static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
126{
127 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
128 struct device *dev = &adap->dev;
129 int ret = 0;
130
131 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
132 GPIOF_OUT_INIT_HIGH, "i2c-scl");
133 if (ret) {
134 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
135 return ret;
136 }
137
138 if (bri->get_sda) {
139 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
140 /* work without SDA polling */
141 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
142 bri->sda_gpio);
143 bri->get_sda = NULL;
144 }
145 }
146
147 return ret;
148}
149
150static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
151{
152 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
153
154 if (bri->get_sda)
155 gpio_free(bri->sda_gpio);
156
157 gpio_free(bri->scl_gpio);
158}
159
160/*
161 * We are generating clock pulses. ndelay() determines durating of clk pulses.
162 * We will generate clock with rate 100 KHz and so duration of both clock levels
163 * is: delay in ns = (10^6 / 100) / 2
164 */
165#define RECOVERY_NDELAY 5000
166#define RECOVERY_CLK_CNT 9
167
168static int i2c_generic_recovery(struct i2c_adapter *adap)
169{
170 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
171 int i = 0, val = 1, ret = 0;
172
173 if (bri->prepare_recovery)
174 bri->prepare_recovery(bri);
175
176 /*
177 * By this time SCL is high, as we need to give 9 falling-rising edges
178 */
179 while (i++ < RECOVERY_CLK_CNT * 2) {
180 if (val) {
181 /* Break if SDA is high */
182 if (bri->get_sda && bri->get_sda(adap))
183 break;
184 /* SCL shouldn't be low here */
185 if (!bri->get_scl(adap)) {
186 dev_err(&adap->dev,
187 "SCL is stuck low, exit recovery\n");
188 ret = -EBUSY;
189 break;
190 }
191 }
192
193 val = !val;
194 bri->set_scl(adap, val);
195 ndelay(RECOVERY_NDELAY);
196 }
197
198 if (bri->unprepare_recovery)
199 bri->unprepare_recovery(bri);
200
201 return ret;
202}
203
204int i2c_generic_scl_recovery(struct i2c_adapter *adap)
205{
206 adap->bus_recovery_info->set_scl(adap, 1);
207 return i2c_generic_recovery(adap);
208}
209
210int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
211{
212 int ret;
213
214 ret = i2c_get_gpios_for_recovery(adap);
215 if (ret)
216 return ret;
217
218 ret = i2c_generic_recovery(adap);
219 i2c_put_gpios_for_recovery(adap);
220
221 return ret;
222}
223
224int i2c_recover_bus(struct i2c_adapter *adap)
225{
226 if (!adap->bus_recovery_info)
227 return -EOPNOTSUPP;
228
229 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
230 return adap->bus_recovery_info->recover_bus(adap);
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static int i2c_device_probe(struct device *dev)
234{
Jean Delvare51298d12009-09-18 22:45:45 +0200235 struct i2c_client *client = i2c_verify_client(dev);
236 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100237 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200238
Jean Delvare51298d12009-09-18 22:45:45 +0200239 if (!client)
240 return 0;
241
242 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200243 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200244 return -ENODEV;
245 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200246 if (!device_can_wakeup(&client->dev))
247 device_init_wakeup(&client->dev,
248 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200249 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200250
Jean Delvaree0457442008-07-14 22:38:30 +0200251 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200252 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100253 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200254 i2c_set_clientdata(client, NULL);
255 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100256 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
259static int i2c_device_remove(struct device *dev)
260{
Jean Delvare51298d12009-09-18 22:45:45 +0200261 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200262 struct i2c_driver *driver;
263 int status;
264
Jean Delvare51298d12009-09-18 22:45:45 +0200265 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200266 return 0;
267
268 driver = to_i2c_driver(dev->driver);
269 if (driver->remove) {
270 dev_dbg(dev, "remove\n");
271 status = driver->remove(client);
272 } else {
273 dev->driver = NULL;
274 status = 0;
275 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200276 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200277 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200278 i2c_set_clientdata(client, NULL);
279 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200280 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
David Brownellf37dd802007-02-13 22:09:00 +0100283static void i2c_device_shutdown(struct device *dev)
284{
Jean Delvare51298d12009-09-18 22:45:45 +0200285 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100286 struct i2c_driver *driver;
287
Jean Delvare51298d12009-09-18 22:45:45 +0200288 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100289 return;
290 driver = to_i2c_driver(dev->driver);
291 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200292 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100293}
294
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200295#ifdef CONFIG_PM_SLEEP
296static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100297{
Jean Delvare51298d12009-09-18 22:45:45 +0200298 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100299 struct i2c_driver *driver;
300
Jean Delvare51298d12009-09-18 22:45:45 +0200301 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100302 return 0;
303 driver = to_i2c_driver(dev->driver);
304 if (!driver->suspend)
305 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200306 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100307}
308
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200309static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100310{
Jean Delvare51298d12009-09-18 22:45:45 +0200311 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100312 struct i2c_driver *driver;
313
Jean Delvare51298d12009-09-18 22:45:45 +0200314 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100315 return 0;
316 driver = to_i2c_driver(dev->driver);
317 if (!driver->resume)
318 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200319 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100320}
321
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200322static int i2c_device_pm_suspend(struct device *dev)
323{
324 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
325
Mark Brownd529de22011-01-14 22:03:49 +0100326 if (pm)
327 return pm_generic_suspend(dev);
328 else
329 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200330}
331
332static int i2c_device_pm_resume(struct device *dev)
333{
334 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200335
336 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100337 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200338 else
Mark Brownd529de22011-01-14 22:03:49 +0100339 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200340}
341
342static int i2c_device_pm_freeze(struct device *dev)
343{
344 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
345
Mark Brownd529de22011-01-14 22:03:49 +0100346 if (pm)
347 return pm_generic_freeze(dev);
348 else
349 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200350}
351
352static int i2c_device_pm_thaw(struct device *dev)
353{
354 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
355
Mark Brownd529de22011-01-14 22:03:49 +0100356 if (pm)
357 return pm_generic_thaw(dev);
358 else
359 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200360}
361
362static int i2c_device_pm_poweroff(struct device *dev)
363{
364 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
365
Mark Brownd529de22011-01-14 22:03:49 +0100366 if (pm)
367 return pm_generic_poweroff(dev);
368 else
369 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200370}
371
372static int i2c_device_pm_restore(struct device *dev)
373{
374 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200375
376 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100377 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200378 else
Mark Brownd529de22011-01-14 22:03:49 +0100379 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200380}
381#else /* !CONFIG_PM_SLEEP */
382#define i2c_device_pm_suspend NULL
383#define i2c_device_pm_resume NULL
384#define i2c_device_pm_freeze NULL
385#define i2c_device_pm_thaw NULL
386#define i2c_device_pm_poweroff NULL
387#define i2c_device_pm_restore NULL
388#endif /* !CONFIG_PM_SLEEP */
389
David Brownell9c1600e2007-05-01 23:26:31 +0200390static void i2c_client_dev_release(struct device *dev)
391{
392 kfree(to_i2c_client(dev));
393}
394
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100395static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200396show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200397{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200398 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
399 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200400}
401
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100402static ssize_t
403show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200404{
405 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200406 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200407}
408
Jean Delvare4f8cf822009-09-18 22:45:46 +0200409static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200410static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
411
412static struct attribute *i2c_dev_attrs[] = {
413 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200414 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200415 &dev_attr_modalias.attr,
416 NULL
417};
418
419static struct attribute_group i2c_dev_attr_group = {
420 .attrs = i2c_dev_attrs,
421};
422
423static const struct attribute_group *i2c_dev_attr_groups[] = {
424 &i2c_dev_attr_group,
425 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200426};
427
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100428static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100429 .suspend = i2c_device_pm_suspend,
430 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200431 .freeze = i2c_device_pm_freeze,
432 .thaw = i2c_device_pm_thaw,
433 .poweroff = i2c_device_pm_poweroff,
434 .restore = i2c_device_pm_restore,
435 SET_RUNTIME_PM_OPS(
436 pm_generic_runtime_suspend,
437 pm_generic_runtime_resume,
438 pm_generic_runtime_idle
439 )
sonic zhang54067ee2009-12-14 21:17:30 +0100440};
441
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200442struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100443 .name = "i2c",
444 .match = i2c_device_match,
445 .probe = i2c_device_probe,
446 .remove = i2c_device_remove,
447 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100448 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000449};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200450EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000451
Jean Delvare51298d12009-09-18 22:45:45 +0200452static struct device_type i2c_client_type = {
453 .groups = i2c_dev_attr_groups,
454 .uevent = i2c_device_uevent,
455 .release = i2c_client_dev_release,
456};
457
David Brownell9b766b82008-01-27 18:14:51 +0100458
459/**
460 * i2c_verify_client - return parameter as i2c_client, or NULL
461 * @dev: device, probably from some driver model iterator
462 *
463 * When traversing the driver model tree, perhaps using driver model
464 * iterators like @device_for_each_child(), you can't assume very much
465 * about the nodes you find. Use this function to avoid oopses caused
466 * by wrongly treating some non-I2C device as an i2c_client.
467 */
468struct i2c_client *i2c_verify_client(struct device *dev)
469{
Jean Delvare51298d12009-09-18 22:45:45 +0200470 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100471 ? to_i2c_client(dev)
472 : NULL;
473}
474EXPORT_SYMBOL(i2c_verify_client);
475
476
Jean Delvare3a89db52010-06-03 11:33:52 +0200477/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300478 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200479static int i2c_check_client_addr_validity(const struct i2c_client *client)
480{
481 if (client->flags & I2C_CLIENT_TEN) {
482 /* 10-bit address, all values are valid */
483 if (client->addr > 0x3ff)
484 return -EINVAL;
485 } else {
486 /* 7-bit address, reject the general call address */
487 if (client->addr == 0x00 || client->addr > 0x7f)
488 return -EINVAL;
489 }
490 return 0;
491}
492
Jean Delvare656b8762010-06-03 11:33:53 +0200493/* And this is a strict address validity check, used when probing. If a
494 * device uses a reserved address, then it shouldn't be probed. 7-bit
495 * addressing is assumed, 10-bit address devices are rare and should be
496 * explicitly enumerated. */
497static int i2c_check_addr_validity(unsigned short addr)
498{
499 /*
500 * Reserved addresses per I2C specification:
501 * 0x00 General call address / START byte
502 * 0x01 CBUS address
503 * 0x02 Reserved for different bus format
504 * 0x03 Reserved for future purposes
505 * 0x04-0x07 Hs-mode master code
506 * 0x78-0x7b 10-bit slave addressing
507 * 0x7c-0x7f Reserved for future purposes
508 */
509 if (addr < 0x08 || addr > 0x77)
510 return -EINVAL;
511 return 0;
512}
513
Jean Delvare3b5f7942010-06-03 11:33:55 +0200514static int __i2c_check_addr_busy(struct device *dev, void *addrp)
515{
516 struct i2c_client *client = i2c_verify_client(dev);
517 int addr = *(int *)addrp;
518
519 if (client && client->addr == addr)
520 return -EBUSY;
521 return 0;
522}
523
Michael Lawnick08263742010-08-11 18:21:02 +0200524/* walk up mux tree */
525static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
526{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200527 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200528 int result;
529
530 result = device_for_each_child(&adapter->dev, &addr,
531 __i2c_check_addr_busy);
532
Jean Delvare97cc4d42010-10-24 18:16:57 +0200533 if (!result && parent)
534 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200535
536 return result;
537}
538
539/* recurse down mux tree */
540static int i2c_check_mux_children(struct device *dev, void *addrp)
541{
542 int result;
543
544 if (dev->type == &i2c_adapter_type)
545 result = device_for_each_child(dev, addrp,
546 i2c_check_mux_children);
547 else
548 result = __i2c_check_addr_busy(dev, addrp);
549
550 return result;
551}
552
Jean Delvare3b5f7942010-06-03 11:33:55 +0200553static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
554{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200555 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200556 int result = 0;
557
Jean Delvare97cc4d42010-10-24 18:16:57 +0200558 if (parent)
559 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200560
561 if (!result)
562 result = device_for_each_child(&adapter->dev, &addr,
563 i2c_check_mux_children);
564
565 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200566}
567
David Brownell9c1600e2007-05-01 23:26:31 +0200568/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200569 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
570 * @adapter: Target I2C bus segment
571 */
572void i2c_lock_adapter(struct i2c_adapter *adapter)
573{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200574 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
575
576 if (parent)
577 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200578 else
579 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200580}
581EXPORT_SYMBOL_GPL(i2c_lock_adapter);
582
583/**
584 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
585 * @adapter: Target I2C bus segment
586 */
587static int i2c_trylock_adapter(struct i2c_adapter *adapter)
588{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200589 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
590
591 if (parent)
592 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200593 else
594 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200595}
596
597/**
598 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
599 * @adapter: Target I2C bus segment
600 */
601void i2c_unlock_adapter(struct i2c_adapter *adapter)
602{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200603 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
604
605 if (parent)
606 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200607 else
608 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200609}
610EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
611
612/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200613 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200614 * @adap: the adapter managing the device
615 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200616 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200617 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200618 * Create an i2c device. Binding is handled through driver model
619 * probe()/remove() methods. A driver may be bound to this device when we
620 * return from this function, or any later moment (e.g. maybe hotplugging will
621 * load the driver module). This call is not appropriate for use by mainboard
622 * initialization logic, which usually runs during an arch_initcall() long
623 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200624 *
625 * This returns the new i2c client, which may be saved for later use with
626 * i2c_unregister_device(); or NULL to indicate an error.
627 */
628struct i2c_client *
629i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
630{
631 struct i2c_client *client;
632 int status;
633
634 client = kzalloc(sizeof *client, GFP_KERNEL);
635 if (!client)
636 return NULL;
637
638 client->adapter = adap;
639
640 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200641
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200642 if (info->archdata)
643 client->dev.archdata = *info->archdata;
644
Marc Pignatee354252008-08-28 08:33:22 +0200645 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200646 client->addr = info->addr;
647 client->irq = info->irq;
648
David Brownell9c1600e2007-05-01 23:26:31 +0200649 strlcpy(client->name, info->type, sizeof(client->name));
650
Jean Delvare3a89db52010-06-03 11:33:52 +0200651 /* Check for address validity */
652 status = i2c_check_client_addr_validity(client);
653 if (status) {
654 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
655 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
656 goto out_err_silent;
657 }
658
Jean Delvaref8a227e2009-06-19 16:58:18 +0200659 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200660 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200661 if (status)
662 goto out_err;
663
664 client->dev.parent = &client->adapter->dev;
665 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200666 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700667 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100668 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200669
Jean Delvarecbb44512011-11-23 11:33:07 +0100670 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200671 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100672 client->addr | ((client->flags & I2C_CLIENT_TEN)
673 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200674 status = device_register(&client->dev);
675 if (status)
676 goto out_err;
677
Jean Delvaref8a227e2009-06-19 16:58:18 +0200678 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
679 client->name, dev_name(&client->dev));
680
David Brownell9c1600e2007-05-01 23:26:31 +0200681 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200682
683out_err:
684 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
685 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200686out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200687 kfree(client);
688 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200689}
690EXPORT_SYMBOL_GPL(i2c_new_device);
691
692
693/**
694 * i2c_unregister_device - reverse effect of i2c_new_device()
695 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200696 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200697 */
698void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200699{
David Brownella1d9e6e2007-05-01 23:26:30 +0200700 device_unregister(&client->dev);
701}
David Brownell9c1600e2007-05-01 23:26:31 +0200702EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200703
704
Jean Delvare60b129d2008-05-11 20:37:06 +0200705static const struct i2c_device_id dummy_id[] = {
706 { "dummy", 0 },
707 { },
708};
709
Jean Delvared2653e92008-04-29 23:11:39 +0200710static int dummy_probe(struct i2c_client *client,
711 const struct i2c_device_id *id)
712{
713 return 0;
714}
715
716static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100717{
718 return 0;
719}
720
721static struct i2c_driver dummy_driver = {
722 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200723 .probe = dummy_probe,
724 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200725 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100726};
727
728/**
729 * i2c_new_dummy - return a new i2c device bound to a dummy driver
730 * @adapter: the adapter managing the device
731 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100732 * Context: can sleep
733 *
734 * This returns an I2C client bound to the "dummy" driver, intended for use
735 * with devices that consume multiple addresses. Examples of such chips
736 * include various EEPROMS (like 24c04 and 24c08 models).
737 *
738 * These dummy devices have two main uses. First, most I2C and SMBus calls
739 * except i2c_transfer() need a client handle; the dummy will be that handle.
740 * And second, this prevents the specified address from being bound to a
741 * different driver.
742 *
743 * This returns the new i2c client, which should be saved for later use with
744 * i2c_unregister_device(); or NULL to indicate an error.
745 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100746struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100747{
748 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200749 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100750 };
751
David Brownelle9f13732008-01-27 18:14:52 +0100752 return i2c_new_device(adapter, &info);
753}
754EXPORT_SYMBOL_GPL(i2c_new_dummy);
755
David Brownellf37dd802007-02-13 22:09:00 +0100756/* ------------------------------------------------------------------------- */
757
David Brownell16ffadf2007-05-01 23:26:28 +0200758/* I2C bus adapters -- one roots each I2C or SMBUS segment */
759
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200760static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
David Brownellef2c83212007-05-01 23:26:28 +0200762 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 complete(&adap->dev_released);
764}
765
Jean Delvare99cd8e22009-06-19 16:58:20 +0200766/*
Jean Delvare390946b2012-09-10 10:14:02 +0200767 * This function is only needed for mutex_lock_nested, so it is never
768 * called unless locking correctness checking is enabled. Thus we
769 * make it inline to avoid a compiler warning. That's what gcc ends up
770 * doing anyway.
771 */
772static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
773{
774 unsigned int depth = 0;
775
776 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
777 depth++;
778
779 return depth;
780}
781
782/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200783 * Let users instantiate I2C devices through sysfs. This can be used when
784 * platform initialization code doesn't contain the proper data for
785 * whatever reason. Also useful for drivers that do device detection and
786 * detection fails, either because the device uses an unexpected address,
787 * or this is a compatible device with different ID register values.
788 *
789 * Parameter checking may look overzealous, but we really don't want
790 * the user to provide incorrect parameters.
791 */
792static ssize_t
793i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
794 const char *buf, size_t count)
795{
796 struct i2c_adapter *adap = to_i2c_adapter(dev);
797 struct i2c_board_info info;
798 struct i2c_client *client;
799 char *blank, end;
800 int res;
801
Jean Delvare99cd8e22009-06-19 16:58:20 +0200802 memset(&info, 0, sizeof(struct i2c_board_info));
803
804 blank = strchr(buf, ' ');
805 if (!blank) {
806 dev_err(dev, "%s: Missing parameters\n", "new_device");
807 return -EINVAL;
808 }
809 if (blank - buf > I2C_NAME_SIZE - 1) {
810 dev_err(dev, "%s: Invalid device name\n", "new_device");
811 return -EINVAL;
812 }
813 memcpy(info.type, buf, blank - buf);
814
815 /* Parse remaining parameters, reject extra parameters */
816 res = sscanf(++blank, "%hi%c", &info.addr, &end);
817 if (res < 1) {
818 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
819 return -EINVAL;
820 }
821 if (res > 1 && end != '\n') {
822 dev_err(dev, "%s: Extra parameters\n", "new_device");
823 return -EINVAL;
824 }
825
Jean Delvare99cd8e22009-06-19 16:58:20 +0200826 client = i2c_new_device(adap, &info);
827 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200828 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200829
830 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200831 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200832 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200833 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200834 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
835 info.type, info.addr);
836
837 return count;
838}
839
840/*
841 * And of course let the users delete the devices they instantiated, if
842 * they got it wrong. This interface can only be used to delete devices
843 * instantiated by i2c_sysfs_new_device above. This guarantees that we
844 * don't delete devices to which some kernel code still has references.
845 *
846 * Parameter checking may look overzealous, but we really don't want
847 * the user to delete the wrong device.
848 */
849static ssize_t
850i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
851 const char *buf, size_t count)
852{
853 struct i2c_adapter *adap = to_i2c_adapter(dev);
854 struct i2c_client *client, *next;
855 unsigned short addr;
856 char end;
857 int res;
858
859 /* Parse parameters, reject extra parameters */
860 res = sscanf(buf, "%hi%c", &addr, &end);
861 if (res < 1) {
862 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
863 return -EINVAL;
864 }
865 if (res > 1 && end != '\n') {
866 dev_err(dev, "%s: Extra parameters\n", "delete_device");
867 return -EINVAL;
868 }
869
870 /* Make sure the device was added through sysfs */
871 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200872 mutex_lock_nested(&adap->userspace_clients_lock,
873 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200874 list_for_each_entry_safe(client, next, &adap->userspace_clients,
875 detected) {
876 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200877 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
878 "delete_device", client->name, client->addr);
879
880 list_del(&client->detected);
881 i2c_unregister_device(client);
882 res = count;
883 break;
884 }
885 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200886 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200887
888 if (res < 0)
889 dev_err(dev, "%s: Can't find device in list\n",
890 "delete_device");
891 return res;
892}
893
Jean Delvare4f8cf822009-09-18 22:45:46 +0200894static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
895static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
896
897static struct attribute *i2c_adapter_attrs[] = {
898 &dev_attr_name.attr,
899 &dev_attr_new_device.attr,
900 &dev_attr_delete_device.attr,
901 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200902};
903
Jean Delvare4f8cf822009-09-18 22:45:46 +0200904static struct attribute_group i2c_adapter_attr_group = {
905 .attrs = i2c_adapter_attrs,
906};
907
908static const struct attribute_group *i2c_adapter_attr_groups[] = {
909 &i2c_adapter_attr_group,
910 NULL
911};
912
Michael Lawnick08263742010-08-11 18:21:02 +0200913struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200914 .groups = i2c_adapter_attr_groups,
915 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200916};
Michael Lawnick08263742010-08-11 18:21:02 +0200917EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Stephen Warren643dd092012-04-17 12:43:33 -0600919/**
920 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
921 * @dev: device, probably from some driver model iterator
922 *
923 * When traversing the driver model tree, perhaps using driver model
924 * iterators like @device_for_each_child(), you can't assume very much
925 * about the nodes you find. Use this function to avoid oopses caused
926 * by wrongly treating some non-I2C device as an i2c_adapter.
927 */
928struct i2c_adapter *i2c_verify_adapter(struct device *dev)
929{
930 return (dev->type == &i2c_adapter_type)
931 ? to_i2c_adapter(dev)
932 : NULL;
933}
934EXPORT_SYMBOL(i2c_verify_adapter);
935
Jean Delvare2bb50952009-09-18 22:45:46 +0200936#ifdef CONFIG_I2C_COMPAT
937static struct class_compat *i2c_adapter_compat_class;
938#endif
939
David Brownell9c1600e2007-05-01 23:26:31 +0200940static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
941{
942 struct i2c_devinfo *devinfo;
943
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200944 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200945 list_for_each_entry(devinfo, &__i2c_board_list, list) {
946 if (devinfo->busnum == adapter->nr
947 && !i2c_new_device(adapter,
948 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100949 dev_err(&adapter->dev,
950 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200951 devinfo->board_info.addr);
952 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200953 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200954}
955
Jean Delvare69b00892009-12-06 17:06:27 +0100956static int i2c_do_add_adapter(struct i2c_driver *driver,
957 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100958{
Jean Delvare4735c982008-07-14 22:38:36 +0200959 /* Detect supported devices on that bus, and instantiate them */
960 i2c_detect(adap, driver);
961
962 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100963 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +0200964 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
965 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +0100966 dev_warn(&adap->dev, "Please use another way to instantiate "
967 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +0100968 /* We ignore the return code; if it fails, too bad */
969 driver->attach_adapter(adap);
970 }
971 return 0;
972}
973
Jean Delvare69b00892009-12-06 17:06:27 +0100974static int __process_new_adapter(struct device_driver *d, void *data)
975{
976 return i2c_do_add_adapter(to_i2c_driver(d), data);
977}
978
David Brownell6e13e642007-05-01 23:26:31 +0200979static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Jean Delvared6703282010-08-11 18:20:59 +0200981 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
David Brownell1d0b19c2008-10-14 17:30:05 +0200983 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200984 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
985 res = -EAGAIN;
986 goto out_list;
987 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200988
Jean Delvare2236baa2010-11-15 22:40:38 +0100989 /* Sanity checks */
990 if (unlikely(adap->name[0] == '\0')) {
991 pr_err("i2c-core: Attempt to register an adapter with "
992 "no name!\n");
993 return -EINVAL;
994 }
995 if (unlikely(!adap->algo)) {
996 pr_err("i2c-core: Attempt to register adapter '%s' with "
997 "no algo!\n", adap->name);
998 return -EINVAL;
999 }
1000
Mika Kuoppala194684e2009-12-06 17:06:22 +01001001 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001002 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001003 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Jean Delvare8fcfef62009-03-28 21:34:43 +01001005 /* Set default timeout to 1 second if not already set */
1006 if (adap->timeout == 0)
1007 adap->timeout = HZ;
1008
Kay Sievers27d9c182009-01-07 14:29:16 +01001009 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001010 adap->dev.bus = &i2c_bus_type;
1011 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001012 res = device_register(&adap->dev);
1013 if (res)
1014 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001016 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1017
Jean Delvare2bb50952009-09-18 22:45:46 +02001018#ifdef CONFIG_I2C_COMPAT
1019 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1020 adap->dev.parent);
1021 if (res)
1022 dev_warn(&adap->dev,
1023 "Failed to create compatibility class link\n");
1024#endif
1025
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301026 /* bus recovery specific initialization */
1027 if (adap->bus_recovery_info) {
1028 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1029
1030 if (!bri->recover_bus) {
1031 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1032 adap->bus_recovery_info = NULL;
1033 goto exit_recovery;
1034 }
1035
1036 /* Generic GPIO recovery */
1037 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1038 if (!gpio_is_valid(bri->scl_gpio)) {
1039 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1040 adap->bus_recovery_info = NULL;
1041 goto exit_recovery;
1042 }
1043
1044 if (gpio_is_valid(bri->sda_gpio))
1045 bri->get_sda = get_sda_gpio_value;
1046 else
1047 bri->get_sda = NULL;
1048
1049 bri->get_scl = get_scl_gpio_value;
1050 bri->set_scl = set_scl_gpio_value;
1051 } else if (!bri->set_scl || !bri->get_scl) {
1052 /* Generic SCL recovery */
1053 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1054 adap->bus_recovery_info = NULL;
1055 }
1056 }
1057
1058exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001059 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +02001060 if (adap->nr < __i2c_first_dynamic_bus_num)
1061 i2c_scan_static_board_info(adap);
1062
Jean Delvare4735c982008-07-14 22:38:36 +02001063 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001064 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001065 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001066 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001067
1068 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001069
Jean Delvareb119c6c2006-08-15 18:26:30 +02001070out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001071 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001072 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001073 mutex_unlock(&core_lock);
1074 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
David Brownell6e13e642007-05-01 23:26:31 +02001077/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001078 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1079 * @adap: the adapter to register (with adap->nr initialized)
1080 * Context: can sleep
1081 *
1082 * See i2c_add_numbered_adapter() for details.
1083 */
1084static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1085{
1086 int id;
1087
1088 mutex_lock(&core_lock);
1089 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1090 GFP_KERNEL);
1091 mutex_unlock(&core_lock);
1092 if (id < 0)
1093 return id == -ENOSPC ? -EBUSY : id;
1094
1095 return i2c_register_adapter(adap);
1096}
1097
1098/**
David Brownell6e13e642007-05-01 23:26:31 +02001099 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1100 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001101 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001102 *
1103 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001104 * doesn't matter or when its bus number is specified by an dt alias.
1105 * Examples of bases when the bus number doesn't matter: I2C adapters
1106 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001107 *
1108 * When this returns zero, a new bus number was allocated and stored
1109 * in adap->nr, and the specified adapter became available for clients.
1110 * Otherwise, a negative errno value is returned.
1111 */
1112int i2c_add_adapter(struct i2c_adapter *adapter)
1113{
Doug Andersonee5c2742013-03-01 08:57:31 -08001114 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001115 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001116
Doug Andersonee5c2742013-03-01 08:57:31 -08001117 if (dev->of_node) {
1118 id = of_alias_get_id(dev->of_node, "i2c");
1119 if (id >= 0) {
1120 adapter->nr = id;
1121 return __i2c_add_numbered_adapter(adapter);
1122 }
1123 }
1124
Jean Delvarecaada322008-01-27 18:14:49 +01001125 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001126 id = idr_alloc(&i2c_adapter_idr, adapter,
1127 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001128 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001129 if (id < 0)
1130 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001131
1132 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001133
David Brownell6e13e642007-05-01 23:26:31 +02001134 return i2c_register_adapter(adapter);
1135}
1136EXPORT_SYMBOL(i2c_add_adapter);
1137
1138/**
1139 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1140 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001141 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001142 *
1143 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001144 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1145 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001146 * is used to properly configure I2C devices.
1147 *
Grant Likely488bf312011-07-25 17:49:43 +02001148 * If the requested bus number is set to -1, then this function will behave
1149 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1150 *
David Brownell6e13e642007-05-01 23:26:31 +02001151 * If no devices have pre-been declared for this bus, then be sure to
1152 * register the adapter before any dynamically allocated ones. Otherwise
1153 * the required bus ID may not be available.
1154 *
1155 * When this returns zero, the specified adapter became available for
1156 * clients using the bus number provided in adap->nr. Also, the table
1157 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1158 * and the appropriate driver model device nodes are created. Otherwise, a
1159 * negative errno value is returned.
1160 */
1161int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1162{
Grant Likely488bf312011-07-25 17:49:43 +02001163 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1164 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001165
Doug Andersonee5c2742013-03-01 08:57:31 -08001166 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001167}
1168EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1169
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001170static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001171 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001172{
Jean Delvare4735c982008-07-14 22:38:36 +02001173 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001174
Jean Delvareacec2112009-03-28 21:34:40 +01001175 /* Remove the devices we created ourselves as the result of hardware
1176 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001177 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1178 if (client->adapter == adapter) {
1179 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1180 client->name, client->addr);
1181 list_del(&client->detected);
1182 i2c_unregister_device(client);
1183 }
1184 }
Jean Delvare026526f2008-01-27 18:14:49 +01001185}
1186
Jean Delvaree549c2b2009-06-19 16:58:19 +02001187static int __unregister_client(struct device *dev, void *dummy)
1188{
1189 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001190 if (client && strcmp(client->name, "dummy"))
1191 i2c_unregister_device(client);
1192 return 0;
1193}
1194
1195static int __unregister_dummy(struct device *dev, void *dummy)
1196{
1197 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001198 if (client)
1199 i2c_unregister_device(client);
1200 return 0;
1201}
1202
Jean Delvare69b00892009-12-06 17:06:27 +01001203static int __process_removed_adapter(struct device_driver *d, void *data)
1204{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001205 i2c_do_del_adapter(to_i2c_driver(d), data);
1206 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001207}
1208
David Brownelld64f73b2007-07-12 14:12:28 +02001209/**
1210 * i2c_del_adapter - unregister I2C adapter
1211 * @adap: the adapter being unregistered
1212 * Context: can sleep
1213 *
1214 * This unregisters an I2C adapter which was previously registered
1215 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1216 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001217void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001219 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001220 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001223 mutex_lock(&core_lock);
1224 found = idr_find(&i2c_adapter_idr, adap->nr);
1225 mutex_unlock(&core_lock);
1226 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001227 pr_debug("i2c-core: attempting to delete unregistered "
1228 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Jean Delvare026526f2008-01-27 18:14:49 +01001232 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001233 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001234 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001235 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001236 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001238 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001239 mutex_lock_nested(&adap->userspace_clients_lock,
1240 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001241 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1242 detected) {
1243 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1244 client->addr);
1245 list_del(&client->detected);
1246 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001247 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001248 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001249
Jean Delvaree549c2b2009-06-19 16:58:19 +02001250 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001251 * check the returned value. This is a two-pass process, because
1252 * we can't remove the dummy devices during the first pass: they
1253 * could have been instantiated by real devices wishing to clean
1254 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001255 device_for_each_child(&adap->dev, NULL, __unregister_client);
1256 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jean Delvare2bb50952009-09-18 22:45:46 +02001258#ifdef CONFIG_I2C_COMPAT
1259 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1260 adap->dev.parent);
1261#endif
1262
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001263 /* device name is gone after device_unregister */
1264 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 /* clean up the sysfs representation */
1267 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* wait for sysfs to drop all references */
1271 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
David Brownell6e13e642007-05-01 23:26:31 +02001273 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001274 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001276 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001278 /* Clear the device structure in case this adapter is ever going to be
1279 added again */
1280 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
David Brownellc0564602007-05-01 23:26:31 +02001282EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284
David Brownell7b4fbc52007-05-01 23:26:30 +02001285/* ------------------------------------------------------------------------- */
1286
Jean Delvare7ae31482011-03-20 14:50:52 +01001287int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1288{
1289 int res;
1290
1291 mutex_lock(&core_lock);
1292 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1293 mutex_unlock(&core_lock);
1294
1295 return res;
1296}
1297EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1298
Jean Delvare69b00892009-12-06 17:06:27 +01001299static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001300{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001301 if (dev->type != &i2c_adapter_type)
1302 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001303 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001304}
1305
David Brownell7b4fbc52007-05-01 23:26:30 +02001306/*
1307 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001308 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
1310
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001311int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001313 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
David Brownell1d0b19c2008-10-14 17:30:05 +02001315 /* Can't register until after driver model init */
1316 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1317 return -EAGAIN;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001320 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jean Delvare729d6dd2009-06-19 16:58:18 +02001323 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001324 * will have called probe() for all matching-but-unbound devices.
1325 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 res = driver_register(&driver->driver);
1327 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001328 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001329
Mark Brownf4e8db32011-01-14 22:03:50 +01001330 /* Drivers should switch to dev_pm_ops instead. */
1331 if (driver->suspend)
1332 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1333 driver->driver.name);
1334 if (driver->resume)
1335 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1336 driver->driver.name);
1337
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001338 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Jean Delvare4735c982008-07-14 22:38:36 +02001340 INIT_LIST_HEAD(&driver->clients);
1341 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001342 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001343
Jean Delvare7eebcb72006-02-05 23:28:21 +01001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001346EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jean Delvare69b00892009-12-06 17:06:27 +01001348static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001349{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001350 if (dev->type == &i2c_adapter_type)
1351 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1352 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001353}
1354
David Brownella1d9e6e2007-05-01 23:26:30 +02001355/**
1356 * i2c_del_driver - unregister I2C driver
1357 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001358 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001359 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001360void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Jean Delvare7ae31482011-03-20 14:50:52 +01001362 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001365 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
David Brownellc0564602007-05-01 23:26:31 +02001367EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
David Brownell7b4fbc52007-05-01 23:26:30 +02001369/* ------------------------------------------------------------------------- */
1370
Jean Delvaree48d3312008-01-27 18:14:48 +01001371/**
1372 * i2c_use_client - increments the reference count of the i2c client structure
1373 * @client: the client being referenced
1374 *
1375 * Each live reference to a client should be refcounted. The driver model does
1376 * that automatically as part of driver binding, so that most drivers don't
1377 * need to do this explicitly: they hold a reference until they're unbound
1378 * from the device.
1379 *
1380 * A pointer to the client with the incremented reference counter is returned.
1381 */
1382struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
David Brownell6ea438e2008-07-14 22:38:24 +02001384 if (client && get_device(&client->dev))
1385 return client;
1386 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
David Brownellc0564602007-05-01 23:26:31 +02001388EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Jean Delvaree48d3312008-01-27 18:14:48 +01001390/**
1391 * i2c_release_client - release a use of the i2c client structure
1392 * @client: the client being no longer referenced
1393 *
1394 * Must be called when a user of a client is finished with it.
1395 */
1396void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
David Brownell6ea438e2008-07-14 22:38:24 +02001398 if (client)
1399 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
David Brownellc0564602007-05-01 23:26:31 +02001401EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
David Brownell9b766b82008-01-27 18:14:51 +01001403struct i2c_cmd_arg {
1404 unsigned cmd;
1405 void *arg;
1406};
1407
1408static int i2c_cmd(struct device *dev, void *_arg)
1409{
1410 struct i2c_client *client = i2c_verify_client(dev);
1411 struct i2c_cmd_arg *arg = _arg;
1412
1413 if (client && client->driver && client->driver->command)
1414 client->driver->command(client, arg->cmd, arg->arg);
1415 return 0;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1419{
David Brownell9b766b82008-01-27 18:14:51 +01001420 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
David Brownell9b766b82008-01-27 18:14:51 +01001422 cmd_arg.cmd = cmd;
1423 cmd_arg.arg = arg;
1424 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
David Brownellc0564602007-05-01 23:26:31 +02001426EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428static int __init i2c_init(void)
1429{
1430 int retval;
1431
1432 retval = bus_register(&i2c_bus_type);
1433 if (retval)
1434 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001435#ifdef CONFIG_I2C_COMPAT
1436 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1437 if (!i2c_adapter_compat_class) {
1438 retval = -ENOMEM;
1439 goto bus_err;
1440 }
1441#endif
David Brownelle9f13732008-01-27 18:14:52 +01001442 retval = i2c_add_driver(&dummy_driver);
1443 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001444 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001445 return 0;
1446
Jean Delvare2bb50952009-09-18 22:45:46 +02001447class_err:
1448#ifdef CONFIG_I2C_COMPAT
1449 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001450bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001451#endif
David Brownelle9f13732008-01-27 18:14:52 +01001452 bus_unregister(&i2c_bus_type);
1453 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456static void __exit i2c_exit(void)
1457{
David Brownelle9f13732008-01-27 18:14:52 +01001458 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001459#ifdef CONFIG_I2C_COMPAT
1460 class_compat_unregister(i2c_adapter_compat_class);
1461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 bus_unregister(&i2c_bus_type);
1463}
1464
David Brownella10f9e72008-10-14 17:30:06 +02001465/* We must initialize early, because some subsystems register i2c drivers
1466 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1467 */
1468postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469module_exit(i2c_exit);
1470
1471/* ----------------------------------------------------
1472 * the functional interface to the i2c busses.
1473 * ----------------------------------------------------
1474 */
1475
David Brownella1cdeda2008-07-14 22:38:24 +02001476/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001477 * __i2c_transfer - unlocked flavor of i2c_transfer
1478 * @adap: Handle to I2C bus
1479 * @msgs: One or more messages to execute before STOP is issued to
1480 * terminate the operation; each message begins with a START.
1481 * @num: Number of messages to be executed.
1482 *
1483 * Returns negative errno, else the number of messages executed.
1484 *
1485 * Adapter lock must be held when calling this function. No debug logging
1486 * takes place. adap->algo->master_xfer existence isn't checked.
1487 */
1488int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1489{
1490 unsigned long orig_jiffies;
1491 int ret, try;
1492
1493 /* Retry automatically on arbitration loss */
1494 orig_jiffies = jiffies;
1495 for (ret = 0, try = 0; try <= adap->retries; try++) {
1496 ret = adap->algo->master_xfer(adap, msgs, num);
1497 if (ret != -EAGAIN)
1498 break;
1499 if (time_after(jiffies, orig_jiffies + adap->timeout))
1500 break;
1501 }
1502
1503 return ret;
1504}
1505EXPORT_SYMBOL(__i2c_transfer);
1506
1507/**
David Brownella1cdeda2008-07-14 22:38:24 +02001508 * i2c_transfer - execute a single or combined I2C message
1509 * @adap: Handle to I2C bus
1510 * @msgs: One or more messages to execute before STOP is issued to
1511 * terminate the operation; each message begins with a START.
1512 * @num: Number of messages to be executed.
1513 *
1514 * Returns negative errno, else the number of messages executed.
1515 *
1516 * Note that there is no requirement that each message be sent to
1517 * the same slave address, although that is the most common model.
1518 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001519int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001521 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David Brownella1cdeda2008-07-14 22:38:24 +02001523 /* REVISIT the fault reporting model here is weak:
1524 *
1525 * - When we get an error after receiving N bytes from a slave,
1526 * there is no way to report "N".
1527 *
1528 * - When we get a NAK after transmitting N bytes to a slave,
1529 * there is no way to report "N" ... or to let the master
1530 * continue executing the rest of this combined message, if
1531 * that's the appropriate response.
1532 *
1533 * - When for example "num" is two and we successfully complete
1534 * the first message but get an error part way through the
1535 * second, it's unclear whether that should be reported as
1536 * one (discarding status on the second message) or errno
1537 * (discarding status on the first one).
1538 */
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (adap->algo->master_xfer) {
1541#ifdef DEBUG
1542 for (ret = 0; ret < num; ret++) {
1543 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001544 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1545 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1546 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548#endif
1549
Mike Rapoportcea443a82008-01-27 18:14:50 +01001550 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001551 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001552 if (!ret)
1553 /* I2C activity is ongoing. */
1554 return -EAGAIN;
1555 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001556 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001557 }
1558
Jean Delvareb37d2a32012-06-29 07:47:19 -03001559 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001560 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 return ret;
1563 } else {
1564 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001565 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567}
David Brownellc0564602007-05-01 23:26:31 +02001568EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
David Brownella1cdeda2008-07-14 22:38:24 +02001570/**
1571 * i2c_master_send - issue a single I2C message in master transmit mode
1572 * @client: Handle to slave device
1573 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001574 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001575 *
1576 * Returns negative errno, or else the number of bytes written.
1577 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001578int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001581 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct i2c_msg msg;
1583
Jean Delvare815f55f2005-05-07 22:58:46 +02001584 msg.addr = client->addr;
1585 msg.flags = client->flags & I2C_M_TEN;
1586 msg.len = count;
1587 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001588
Jean Delvare815f55f2005-05-07 22:58:46 +02001589 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001591 /*
1592 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1593 * transmitted, else error code.
1594 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001595 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
David Brownellc0564602007-05-01 23:26:31 +02001597EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
David Brownella1cdeda2008-07-14 22:38:24 +02001599/**
1600 * i2c_master_recv - issue a single I2C message in master receive mode
1601 * @client: Handle to slave device
1602 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001603 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001604 *
1605 * Returns negative errno, or else the number of bytes read.
1606 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001607int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
Farid Hammane7225acf2010-05-21 18:40:58 +02001609 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct i2c_msg msg;
1611 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Jean Delvare815f55f2005-05-07 22:58:46 +02001613 msg.addr = client->addr;
1614 msg.flags = client->flags & I2C_M_TEN;
1615 msg.flags |= I2C_M_RD;
1616 msg.len = count;
1617 msg.buf = buf;
1618
1619 ret = i2c_transfer(adap, &msg, 1);
1620
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001621 /*
1622 * If everything went ok (i.e. 1 msg received), return #bytes received,
1623 * else error code.
1624 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001625 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
David Brownellc0564602007-05-01 23:26:31 +02001627EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* ----------------------------------------------------
1630 * the i2c address scanning function
1631 * Will not work for 10-bit addresses!
1632 * ----------------------------------------------------
1633 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001634
Jean Delvare63e4e802010-06-03 11:33:51 +02001635/*
1636 * Legacy default probe function, mostly relevant for SMBus. The default
1637 * probe method is a quick write, but it is known to corrupt the 24RF08
1638 * EEPROMs due to a state machine bug, and could also irreversibly
1639 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1640 * we use a short byte read instead. Also, some bus drivers don't implement
1641 * quick write, so we fallback to a byte read in that case too.
1642 * On x86, there is another special case for FSC hardware monitoring chips,
1643 * which want regular byte reads (address 0x73.) Fortunately, these are the
1644 * only known chips using this I2C address on PC hardware.
1645 * Returns 1 if probe succeeded, 0 if not.
1646 */
1647static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1648{
1649 int err;
1650 union i2c_smbus_data dummy;
1651
1652#ifdef CONFIG_X86
1653 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1654 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1655 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1656 I2C_SMBUS_BYTE_DATA, &dummy);
1657 else
1658#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001659 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1660 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001661 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1662 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001663 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1664 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1665 I2C_SMBUS_BYTE, &dummy);
1666 else {
1667 dev_warn(&adap->dev, "No suitable probing method supported\n");
1668 err = -EOPNOTSUPP;
1669 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001670
1671 return err >= 0;
1672}
1673
Jean Delvareccfbbd02009-12-06 17:06:25 +01001674static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001675 struct i2c_driver *driver)
1676{
1677 struct i2c_board_info info;
1678 struct i2c_adapter *adapter = temp_client->adapter;
1679 int addr = temp_client->addr;
1680 int err;
1681
1682 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001683 err = i2c_check_addr_validity(addr);
1684 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001685 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1686 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001687 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001688 }
1689
1690 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001691 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001692 return 0;
1693
Jean Delvareccfbbd02009-12-06 17:06:25 +01001694 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001695 if (!i2c_default_probe(adapter, addr))
1696 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001697
1698 /* Finally call the custom detection function */
1699 memset(&info, 0, sizeof(struct i2c_board_info));
1700 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001701 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001702 if (err) {
1703 /* -ENODEV is returned if the detection fails. We catch it
1704 here as this isn't an error. */
1705 return err == -ENODEV ? 0 : err;
1706 }
1707
1708 /* Consistency check */
1709 if (info.type[0] == '\0') {
1710 dev_err(&adapter->dev, "%s detection function provided "
1711 "no name for 0x%x\n", driver->driver.name,
1712 addr);
1713 } else {
1714 struct i2c_client *client;
1715
1716 /* Detection succeeded, instantiate the device */
1717 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1718 info.type, info.addr);
1719 client = i2c_new_device(adapter, &info);
1720 if (client)
1721 list_add_tail(&client->detected, &driver->clients);
1722 else
1723 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1724 info.type, info.addr);
1725 }
1726 return 0;
1727}
1728
1729static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1730{
Jean Delvarec3813d62009-12-14 21:17:25 +01001731 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001732 struct i2c_client *temp_client;
1733 int i, err = 0;
1734 int adap_id = i2c_adapter_id(adapter);
1735
Jean Delvarec3813d62009-12-14 21:17:25 +01001736 address_list = driver->address_list;
1737 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001738 return 0;
1739
Jean Delvare51b54ba2010-10-24 18:16:58 +02001740 /* Stop here if the classes do not match */
1741 if (!(adapter->class & driver->class))
1742 return 0;
1743
Jean Delvare4735c982008-07-14 22:38:36 +02001744 /* Set up a temporary client to help detect callback */
1745 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1746 if (!temp_client)
1747 return -ENOMEM;
1748 temp_client->adapter = adapter;
1749
Jean Delvarec3813d62009-12-14 21:17:25 +01001750 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001751 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001752 "addr 0x%02x\n", adap_id, address_list[i]);
1753 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001754 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001755 if (unlikely(err))
1756 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001757 }
1758
Jean Delvare4735c982008-07-14 22:38:36 +02001759 kfree(temp_client);
1760 return err;
1761}
1762
Jean Delvared44f19d2010-08-11 18:20:57 +02001763int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1764{
1765 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1766 I2C_SMBUS_QUICK, NULL) >= 0;
1767}
1768EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1769
Jean Delvare12b5053a2007-05-01 23:26:31 +02001770struct i2c_client *
1771i2c_new_probed_device(struct i2c_adapter *adap,
1772 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001773 unsigned short const *addr_list,
1774 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001775{
1776 int i;
1777
Jean Delvare8031d792010-08-11 18:21:00 +02001778 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001779 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001780
Jean Delvare12b5053a2007-05-01 23:26:31 +02001781 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1782 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001783 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001784 dev_warn(&adap->dev, "Invalid 7-bit address "
1785 "0x%02x\n", addr_list[i]);
1786 continue;
1787 }
1788
1789 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001790 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001791 dev_dbg(&adap->dev, "Address 0x%02x already in "
1792 "use, not probing\n", addr_list[i]);
1793 continue;
1794 }
1795
Jean Delvare63e4e802010-06-03 11:33:51 +02001796 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001797 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001798 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001799 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001800
1801 if (addr_list[i] == I2C_CLIENT_END) {
1802 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1803 return NULL;
1804 }
1805
1806 info->addr = addr_list[i];
1807 return i2c_new_device(adap, info);
1808}
1809EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1810
Jean Delvared735b342011-03-20 14:50:52 +01001811struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001814
Jean Delvarecaada322008-01-27 18:14:49 +01001815 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001816 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001817 if (adapter && !try_module_get(adapter->owner))
1818 adapter = NULL;
1819
Jean Delvarecaada322008-01-27 18:14:49 +01001820 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001821 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
David Brownellc0564602007-05-01 23:26:31 +02001823EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825void i2c_put_adapter(struct i2c_adapter *adap)
1826{
1827 module_put(adap->owner);
1828}
David Brownellc0564602007-05-01 23:26:31 +02001829EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831/* The SMBus parts */
1832
David Brownell438d6c22006-12-10 21:21:31 +01001833#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001834static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
1836 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001837
Farid Hammane7225acf2010-05-21 18:40:58 +02001838 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001839 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 data = data ^ POLY;
1841 data = data << 1;
1842 }
1843 return (u8)(data >> 8);
1844}
1845
Jean Delvare421ef472005-10-26 21:28:55 +02001846/* Incremental CRC8 over count bytes in the array pointed to by p */
1847static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 int i;
1850
Farid Hammane7225acf2010-05-21 18:40:58 +02001851 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001852 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return crc;
1854}
1855
Jean Delvare421ef472005-10-26 21:28:55 +02001856/* Assume a 7-bit address, which is reasonable for SMBus */
1857static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Jean Delvare421ef472005-10-26 21:28:55 +02001859 /* The address will be sent first */
1860 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1861 pec = i2c_smbus_pec(pec, &addr, 1);
1862
1863 /* The data buffer follows */
1864 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Jean Delvare421ef472005-10-26 21:28:55 +02001867/* Used for write only transactions */
1868static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Jean Delvare421ef472005-10-26 21:28:55 +02001870 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1871 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
Jean Delvare421ef472005-10-26 21:28:55 +02001874/* Return <0 on CRC error
1875 If there was a write before this read (most cases) we need to take the
1876 partial CRC from the write part into account.
1877 Note that this function does modify the message (we need to decrease the
1878 message length to hide the CRC byte from the caller). */
1879static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Jean Delvare421ef472005-10-26 21:28:55 +02001881 u8 rpec = msg->buf[--msg->len];
1882 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (rpec != cpec) {
1885 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1886 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001887 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
David Brownell438d6c22006-12-10 21:21:31 +01001889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
David Brownella1cdeda2008-07-14 22:38:24 +02001892/**
1893 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1894 * @client: Handle to slave device
1895 *
1896 * This executes the SMBus "receive byte" protocol, returning negative errno
1897 * else the byte received from the device.
1898 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001899s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001902 int status;
1903
1904 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1905 I2C_SMBUS_READ, 0,
1906 I2C_SMBUS_BYTE, &data);
1907 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
David Brownellc0564602007-05-01 23:26:31 +02001909EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
David Brownella1cdeda2008-07-14 22:38:24 +02001911/**
1912 * i2c_smbus_write_byte - SMBus "send byte" protocol
1913 * @client: Handle to slave device
1914 * @value: Byte to be sent
1915 *
1916 * This executes the SMBus "send byte" protocol, returning negative errno
1917 * else zero on success.
1918 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001919s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Farid Hammane7225acf2010-05-21 18:40:58 +02001921 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001922 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
David Brownellc0564602007-05-01 23:26:31 +02001924EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
David Brownella1cdeda2008-07-14 22:38:24 +02001926/**
1927 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1928 * @client: Handle to slave device
1929 * @command: Byte interpreted by slave
1930 *
1931 * This executes the SMBus "read byte" protocol, returning negative errno
1932 * else a data byte received from the device.
1933 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001934s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001937 int status;
1938
1939 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1940 I2C_SMBUS_READ, command,
1941 I2C_SMBUS_BYTE_DATA, &data);
1942 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
David Brownellc0564602007-05-01 23:26:31 +02001944EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
David Brownella1cdeda2008-07-14 22:38:24 +02001946/**
1947 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1948 * @client: Handle to slave device
1949 * @command: Byte interpreted by slave
1950 * @value: Byte being written
1951 *
1952 * This executes the SMBus "write byte" protocol, returning negative errno
1953 * else zero on success.
1954 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001955s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
1956 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 union i2c_smbus_data data;
1959 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001960 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1961 I2C_SMBUS_WRITE, command,
1962 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
David Brownellc0564602007-05-01 23:26:31 +02001964EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
David Brownella1cdeda2008-07-14 22:38:24 +02001966/**
1967 * i2c_smbus_read_word_data - SMBus "read word" protocol
1968 * @client: Handle to slave device
1969 * @command: Byte interpreted by slave
1970 *
1971 * This executes the SMBus "read word" protocol, returning negative errno
1972 * else a 16-bit unsigned "word" received from the device.
1973 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001974s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
1976 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001977 int status;
1978
1979 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1980 I2C_SMBUS_READ, command,
1981 I2C_SMBUS_WORD_DATA, &data);
1982 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
David Brownellc0564602007-05-01 23:26:31 +02001984EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
David Brownella1cdeda2008-07-14 22:38:24 +02001986/**
1987 * i2c_smbus_write_word_data - SMBus "write word" protocol
1988 * @client: Handle to slave device
1989 * @command: Byte interpreted by slave
1990 * @value: 16-bit "word" being written
1991 *
1992 * This executes the SMBus "write word" protocol, returning negative errno
1993 * else zero on success.
1994 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001995s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
1996 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 union i2c_smbus_data data;
1999 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002000 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2001 I2C_SMBUS_WRITE, command,
2002 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
David Brownellc0564602007-05-01 23:26:31 +02002004EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
David Brownella64ec072007-10-13 23:56:31 +02002006/**
David Brownella1cdeda2008-07-14 22:38:24 +02002007 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002008 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002009 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002010 * @values: Byte array into which data will be read; big enough to hold
2011 * the data returned by the slave. SMBus allows at most 32 bytes.
2012 *
David Brownella1cdeda2008-07-14 22:38:24 +02002013 * This executes the SMBus "block read" protocol, returning negative errno
2014 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002015 *
2016 * Note that using this function requires that the client's adapter support
2017 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2018 * support this; its emulation through I2C messaging relies on a specific
2019 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2020 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002021s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002022 u8 *values)
2023{
2024 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002025 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002026
David Brownell24a5bb72008-07-14 22:38:23 +02002027 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2028 I2C_SMBUS_READ, command,
2029 I2C_SMBUS_BLOCK_DATA, &data);
2030 if (status)
2031 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002032
2033 memcpy(values, &data.block[1], data.block[0]);
2034 return data.block[0];
2035}
2036EXPORT_SYMBOL(i2c_smbus_read_block_data);
2037
David Brownella1cdeda2008-07-14 22:38:24 +02002038/**
2039 * i2c_smbus_write_block_data - SMBus "block write" protocol
2040 * @client: Handle to slave device
2041 * @command: Byte interpreted by slave
2042 * @length: Size of data block; SMBus allows at most 32 bytes
2043 * @values: Byte array which will be written.
2044 *
2045 * This executes the SMBus "block write" protocol, returning negative errno
2046 * else zero on success.
2047 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002048s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002049 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (length > I2C_SMBUS_BLOCK_MAX)
2054 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002056 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002057 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2058 I2C_SMBUS_WRITE, command,
2059 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
David Brownellc0564602007-05-01 23:26:31 +02002061EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002064s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002065 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002068 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002069
Jean Delvare4b2643d2007-07-12 14:12:29 +02002070 if (length > I2C_SMBUS_BLOCK_MAX)
2071 length = I2C_SMBUS_BLOCK_MAX;
2072 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002073 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2074 I2C_SMBUS_READ, command,
2075 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2076 if (status < 0)
2077 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002078
2079 memcpy(values, &data.block[1], data.block[0]);
2080 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
David Brownellc0564602007-05-01 23:26:31 +02002082EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Jean Delvare0cc43a12011-01-10 22:11:23 +01002084s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002085 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002086{
2087 union i2c_smbus_data data;
2088
2089 if (length > I2C_SMBUS_BLOCK_MAX)
2090 length = I2C_SMBUS_BLOCK_MAX;
2091 data.block[0] = length;
2092 memcpy(data.block + 1, values, length);
2093 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2094 I2C_SMBUS_WRITE, command,
2095 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2096}
David Brownellc0564602007-05-01 23:26:31 +02002097EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002098
David Brownell438d6c22006-12-10 21:21:31 +01002099/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002101static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2102 unsigned short flags,
2103 char read_write, u8 command, int size,
2104 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 /* So we need to generate a series of msgs. In the case of writing, we
2107 need to use only one message; when reading, we need two. We initialize
2108 most things with sane defaults, to keep the code below somewhat
2109 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002110 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2111 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002112 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002114 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002115 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002116 struct i2c_msg msg[2] = {
2117 {
2118 .addr = addr,
2119 .flags = flags,
2120 .len = 1,
2121 .buf = msgbuf0,
2122 }, {
2123 .addr = addr,
2124 .flags = flags | I2C_M_RD,
2125 .len = 0,
2126 .buf = msgbuf1,
2127 },
2128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002131 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 case I2C_SMBUS_QUICK:
2133 msg[0].len = 0;
2134 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002135 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2136 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 num = 1;
2138 break;
2139 case I2C_SMBUS_BYTE:
2140 if (read_write == I2C_SMBUS_READ) {
2141 /* Special case: only a read! */
2142 msg[0].flags = I2C_M_RD | flags;
2143 num = 1;
2144 }
2145 break;
2146 case I2C_SMBUS_BYTE_DATA:
2147 if (read_write == I2C_SMBUS_READ)
2148 msg[1].len = 1;
2149 else {
2150 msg[0].len = 2;
2151 msgbuf0[1] = data->byte;
2152 }
2153 break;
2154 case I2C_SMBUS_WORD_DATA:
2155 if (read_write == I2C_SMBUS_READ)
2156 msg[1].len = 2;
2157 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002158 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002160 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 }
2162 break;
2163 case I2C_SMBUS_PROC_CALL:
2164 num = 2; /* Special case */
2165 read_write = I2C_SMBUS_READ;
2166 msg[0].len = 3;
2167 msg[1].len = 2;
2168 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002169 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 break;
2171 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002173 msg[1].flags |= I2C_M_RECV_LEN;
2174 msg[1].len = 1; /* block length will be added by
2175 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 } else {
2177 msg[0].len = data->block[0] + 2;
2178 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002179 dev_err(&adapter->dev,
2180 "Invalid block write size %d\n",
2181 data->block[0]);
2182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002184 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 msgbuf0[i] = data->block[i-1];
2186 }
2187 break;
2188 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002189 num = 2; /* Another special case */
2190 read_write = I2C_SMBUS_READ;
2191 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002192 dev_err(&adapter->dev,
2193 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002194 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002195 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002196 }
2197 msg[0].len = data->block[0] + 2;
2198 for (i = 1; i < msg[0].len; i++)
2199 msgbuf0[i] = data->block[i-1];
2200 msg[1].flags |= I2C_M_RECV_LEN;
2201 msg[1].len = 1; /* block length will be added by
2202 the underlying bus driver */
2203 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 case I2C_SMBUS_I2C_BLOCK_DATA:
2205 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002206 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 } else {
2208 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002209 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002210 dev_err(&adapter->dev,
2211 "Invalid block write size %d\n",
2212 data->block[0]);
2213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
2215 for (i = 1; i <= data->block[0]; i++)
2216 msgbuf0[i] = data->block[i];
2217 }
2218 break;
2219 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002220 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2221 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
2223
Jean Delvare421ef472005-10-26 21:28:55 +02002224 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2225 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2226 if (i) {
2227 /* Compute PEC if first message is a write */
2228 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002229 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002230 i2c_smbus_add_pec(&msg[0]);
2231 else /* Write followed by read */
2232 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2233 }
2234 /* Ask for PEC if last message is a read */
2235 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002236 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002237 }
2238
David Brownell24a5bb72008-07-14 22:38:23 +02002239 status = i2c_transfer(adapter, msg, num);
2240 if (status < 0)
2241 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Jean Delvare421ef472005-10-26 21:28:55 +02002243 /* Check PEC if last message is a read */
2244 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002245 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2246 if (status < 0)
2247 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002248 }
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002251 switch (size) {
2252 case I2C_SMBUS_BYTE:
2253 data->byte = msgbuf0[0];
2254 break;
2255 case I2C_SMBUS_BYTE_DATA:
2256 data->byte = msgbuf1[0];
2257 break;
2258 case I2C_SMBUS_WORD_DATA:
2259 case I2C_SMBUS_PROC_CALL:
2260 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2261 break;
2262 case I2C_SMBUS_I2C_BLOCK_DATA:
2263 for (i = 0; i < data->block[0]; i++)
2264 data->block[i+1] = msgbuf1[i];
2265 break;
2266 case I2C_SMBUS_BLOCK_DATA:
2267 case I2C_SMBUS_BLOCK_PROC_CALL:
2268 for (i = 0; i < msgbuf1[0] + 1; i++)
2269 data->block[i] = msgbuf1[i];
2270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272 return 0;
2273}
2274
David Brownella1cdeda2008-07-14 22:38:24 +02002275/**
2276 * i2c_smbus_xfer - execute SMBus protocol operations
2277 * @adapter: Handle to I2C bus
2278 * @addr: Address of SMBus slave on that bus
2279 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2280 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2281 * @command: Byte interpreted by slave, for protocols which use such bytes
2282 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2283 * @data: Data to be read or written
2284 *
2285 * This executes an SMBus protocol operation, and returns a negative
2286 * errno code else zero on success.
2287 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002288s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002289 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002290 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002292 unsigned long orig_jiffies;
2293 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002296 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002299 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002300
2301 /* Retry automatically on arbitration loss */
2302 orig_jiffies = jiffies;
2303 for (res = 0, try = 0; try <= adapter->retries; try++) {
2304 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2305 read_write, command,
2306 protocol, data);
2307 if (res != -EAGAIN)
2308 break;
2309 if (time_after(jiffies,
2310 orig_jiffies + adapter->timeout))
2311 break;
2312 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002313 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002315 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2316 return res;
2317 /*
2318 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2319 * implement native support for the SMBus operation.
2320 */
2321 }
2322
2323 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2324 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2329MODULE_DESCRIPTION("I2C-Bus main module");
2330MODULE_LICENSE("GPL");