blob: 9d539cbfc833b12cb1a030ed18d5092ebdb65956 [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}
Mark Brown0001a0c2015-04-15 19:18:39 +0100209EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530210
211int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
212{
213 int ret;
214
215 ret = i2c_get_gpios_for_recovery(adap);
216 if (ret)
217 return ret;
218
219 ret = i2c_generic_recovery(adap);
220 i2c_put_gpios_for_recovery(adap);
221
222 return ret;
223}
Mark Brown0001a0c2015-04-15 19:18:39 +0100224EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530225
226int i2c_recover_bus(struct i2c_adapter *adap)
227{
228 if (!adap->bus_recovery_info)
229 return -EOPNOTSUPP;
230
231 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
232 return adap->bus_recovery_info->recover_bus(adap);
233}
Mark Brown0001a0c2015-04-15 19:18:39 +0100234EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static int i2c_device_probe(struct device *dev)
237{
Jean Delvare51298d12009-09-18 22:45:45 +0200238 struct i2c_client *client = i2c_verify_client(dev);
239 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100240 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200241
Jean Delvare51298d12009-09-18 22:45:45 +0200242 if (!client)
243 return 0;
244
245 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200246 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200247 return -ENODEV;
248 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200249 if (!device_can_wakeup(&client->dev))
250 device_init_wakeup(&client->dev,
251 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200252 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200253
Jean Delvaree0457442008-07-14 22:38:30 +0200254 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200255 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100256 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200257 i2c_set_clientdata(client, NULL);
258 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100259 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262static int i2c_device_remove(struct device *dev)
263{
Jean Delvare51298d12009-09-18 22:45:45 +0200264 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200265 struct i2c_driver *driver;
266 int status;
267
Jean Delvare51298d12009-09-18 22:45:45 +0200268 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200269 return 0;
270
271 driver = to_i2c_driver(dev->driver);
272 if (driver->remove) {
273 dev_dbg(dev, "remove\n");
274 status = driver->remove(client);
275 } else {
276 dev->driver = NULL;
277 status = 0;
278 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200279 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200280 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200281 i2c_set_clientdata(client, NULL);
282 }
David Brownella1d9e6e2007-05-01 23:26:30 +0200283 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
David Brownellf37dd802007-02-13 22:09:00 +0100286static void i2c_device_shutdown(struct device *dev)
287{
Jean Delvare51298d12009-09-18 22:45:45 +0200288 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100289 struct i2c_driver *driver;
290
Jean Delvare51298d12009-09-18 22:45:45 +0200291 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100292 return;
293 driver = to_i2c_driver(dev->driver);
294 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200295 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100296}
297
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200298#ifdef CONFIG_PM_SLEEP
299static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100300{
Jean Delvare51298d12009-09-18 22:45:45 +0200301 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100302 struct i2c_driver *driver;
303
Jean Delvare51298d12009-09-18 22:45:45 +0200304 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100305 return 0;
306 driver = to_i2c_driver(dev->driver);
307 if (!driver->suspend)
308 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200309 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100310}
311
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200312static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100313{
Jean Delvare51298d12009-09-18 22:45:45 +0200314 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100315 struct i2c_driver *driver;
316
Jean Delvare51298d12009-09-18 22:45:45 +0200317 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100318 return 0;
319 driver = to_i2c_driver(dev->driver);
320 if (!driver->resume)
321 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200322 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100323}
324
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200325static int i2c_device_pm_suspend(struct device *dev)
326{
327 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
328
Mark Brownd529de22011-01-14 22:03:49 +0100329 if (pm)
330 return pm_generic_suspend(dev);
331 else
332 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200333}
334
335static int i2c_device_pm_resume(struct device *dev)
336{
337 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200338
339 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100340 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200341 else
Mark Brownd529de22011-01-14 22:03:49 +0100342 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200343}
344
345static int i2c_device_pm_freeze(struct device *dev)
346{
347 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
348
Mark Brownd529de22011-01-14 22:03:49 +0100349 if (pm)
350 return pm_generic_freeze(dev);
351 else
352 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200353}
354
355static int i2c_device_pm_thaw(struct device *dev)
356{
357 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
358
Mark Brownd529de22011-01-14 22:03:49 +0100359 if (pm)
360 return pm_generic_thaw(dev);
361 else
362 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200363}
364
365static int i2c_device_pm_poweroff(struct device *dev)
366{
367 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
368
Mark Brownd529de22011-01-14 22:03:49 +0100369 if (pm)
370 return pm_generic_poweroff(dev);
371 else
372 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200373}
374
375static int i2c_device_pm_restore(struct device *dev)
376{
377 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200378
379 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100380 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200381 else
Mark Brownd529de22011-01-14 22:03:49 +0100382 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200383}
384#else /* !CONFIG_PM_SLEEP */
385#define i2c_device_pm_suspend NULL
386#define i2c_device_pm_resume NULL
387#define i2c_device_pm_freeze NULL
388#define i2c_device_pm_thaw NULL
389#define i2c_device_pm_poweroff NULL
390#define i2c_device_pm_restore NULL
391#endif /* !CONFIG_PM_SLEEP */
392
David Brownell9c1600e2007-05-01 23:26:31 +0200393static void i2c_client_dev_release(struct device *dev)
394{
395 kfree(to_i2c_client(dev));
396}
397
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100398static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200399show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200400{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200401 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
402 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200403}
404
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100405static ssize_t
406show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200407{
408 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200409 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200410}
411
Jean Delvare4f8cf822009-09-18 22:45:46 +0200412static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200413static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
414
415static struct attribute *i2c_dev_attrs[] = {
416 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200417 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200418 &dev_attr_modalias.attr,
419 NULL
420};
421
422static struct attribute_group i2c_dev_attr_group = {
423 .attrs = i2c_dev_attrs,
424};
425
426static const struct attribute_group *i2c_dev_attr_groups[] = {
427 &i2c_dev_attr_group,
428 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200429};
430
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100431static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100432 .suspend = i2c_device_pm_suspend,
433 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200434 .freeze = i2c_device_pm_freeze,
435 .thaw = i2c_device_pm_thaw,
436 .poweroff = i2c_device_pm_poweroff,
437 .restore = i2c_device_pm_restore,
438 SET_RUNTIME_PM_OPS(
439 pm_generic_runtime_suspend,
440 pm_generic_runtime_resume,
441 pm_generic_runtime_idle
442 )
sonic zhang54067ee2009-12-14 21:17:30 +0100443};
444
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200445struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100446 .name = "i2c",
447 .match = i2c_device_match,
448 .probe = i2c_device_probe,
449 .remove = i2c_device_remove,
450 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100451 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000452};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200453EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000454
Jean Delvare51298d12009-09-18 22:45:45 +0200455static struct device_type i2c_client_type = {
456 .groups = i2c_dev_attr_groups,
457 .uevent = i2c_device_uevent,
458 .release = i2c_client_dev_release,
459};
460
David Brownell9b766b82008-01-27 18:14:51 +0100461
462/**
463 * i2c_verify_client - return parameter as i2c_client, or NULL
464 * @dev: device, probably from some driver model iterator
465 *
466 * When traversing the driver model tree, perhaps using driver model
467 * iterators like @device_for_each_child(), you can't assume very much
468 * about the nodes you find. Use this function to avoid oopses caused
469 * by wrongly treating some non-I2C device as an i2c_client.
470 */
471struct i2c_client *i2c_verify_client(struct device *dev)
472{
Jean Delvare51298d12009-09-18 22:45:45 +0200473 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100474 ? to_i2c_client(dev)
475 : NULL;
476}
477EXPORT_SYMBOL(i2c_verify_client);
478
479
Jean Delvare3a89db52010-06-03 11:33:52 +0200480/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300481 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200482static int i2c_check_client_addr_validity(const struct i2c_client *client)
483{
484 if (client->flags & I2C_CLIENT_TEN) {
485 /* 10-bit address, all values are valid */
486 if (client->addr > 0x3ff)
487 return -EINVAL;
488 } else {
489 /* 7-bit address, reject the general call address */
490 if (client->addr == 0x00 || client->addr > 0x7f)
491 return -EINVAL;
492 }
493 return 0;
494}
495
Jean Delvare656b8762010-06-03 11:33:53 +0200496/* And this is a strict address validity check, used when probing. If a
497 * device uses a reserved address, then it shouldn't be probed. 7-bit
498 * addressing is assumed, 10-bit address devices are rare and should be
499 * explicitly enumerated. */
500static int i2c_check_addr_validity(unsigned short addr)
501{
502 /*
503 * Reserved addresses per I2C specification:
504 * 0x00 General call address / START byte
505 * 0x01 CBUS address
506 * 0x02 Reserved for different bus format
507 * 0x03 Reserved for future purposes
508 * 0x04-0x07 Hs-mode master code
509 * 0x78-0x7b 10-bit slave addressing
510 * 0x7c-0x7f Reserved for future purposes
511 */
512 if (addr < 0x08 || addr > 0x77)
513 return -EINVAL;
514 return 0;
515}
516
Jean Delvare3b5f7942010-06-03 11:33:55 +0200517static int __i2c_check_addr_busy(struct device *dev, void *addrp)
518{
519 struct i2c_client *client = i2c_verify_client(dev);
520 int addr = *(int *)addrp;
521
522 if (client && client->addr == addr)
523 return -EBUSY;
524 return 0;
525}
526
Michael Lawnick08263742010-08-11 18:21:02 +0200527/* walk up mux tree */
528static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
529{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200530 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200531 int result;
532
533 result = device_for_each_child(&adapter->dev, &addr,
534 __i2c_check_addr_busy);
535
Jean Delvare97cc4d42010-10-24 18:16:57 +0200536 if (!result && parent)
537 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200538
539 return result;
540}
541
542/* recurse down mux tree */
543static int i2c_check_mux_children(struct device *dev, void *addrp)
544{
545 int result;
546
547 if (dev->type == &i2c_adapter_type)
548 result = device_for_each_child(dev, addrp,
549 i2c_check_mux_children);
550 else
551 result = __i2c_check_addr_busy(dev, addrp);
552
553 return result;
554}
555
Jean Delvare3b5f7942010-06-03 11:33:55 +0200556static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
557{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200558 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200559 int result = 0;
560
Jean Delvare97cc4d42010-10-24 18:16:57 +0200561 if (parent)
562 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200563
564 if (!result)
565 result = device_for_each_child(&adapter->dev, &addr,
566 i2c_check_mux_children);
567
568 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200569}
570
David Brownell9c1600e2007-05-01 23:26:31 +0200571/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200572 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
573 * @adapter: Target I2C bus segment
574 */
575void i2c_lock_adapter(struct i2c_adapter *adapter)
576{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200577 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
578
579 if (parent)
580 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200581 else
582 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200583}
584EXPORT_SYMBOL_GPL(i2c_lock_adapter);
585
586/**
587 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
588 * @adapter: Target I2C bus segment
589 */
590static int i2c_trylock_adapter(struct i2c_adapter *adapter)
591{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200592 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
593
594 if (parent)
595 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200596 else
597 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200598}
599
600/**
601 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
602 * @adapter: Target I2C bus segment
603 */
604void i2c_unlock_adapter(struct i2c_adapter *adapter)
605{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200606 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
607
608 if (parent)
609 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200610 else
611 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200612}
613EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
614
615/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200616 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200617 * @adap: the adapter managing the device
618 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200619 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200620 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200621 * Create an i2c device. Binding is handled through driver model
622 * probe()/remove() methods. A driver may be bound to this device when we
623 * return from this function, or any later moment (e.g. maybe hotplugging will
624 * load the driver module). This call is not appropriate for use by mainboard
625 * initialization logic, which usually runs during an arch_initcall() long
626 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200627 *
628 * This returns the new i2c client, which may be saved for later use with
629 * i2c_unregister_device(); or NULL to indicate an error.
630 */
631struct i2c_client *
632i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
633{
634 struct i2c_client *client;
635 int status;
636
637 client = kzalloc(sizeof *client, GFP_KERNEL);
638 if (!client)
639 return NULL;
640
641 client->adapter = adap;
642
643 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200644
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200645 if (info->archdata)
646 client->dev.archdata = *info->archdata;
647
Marc Pignatee354252008-08-28 08:33:22 +0200648 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200649 client->addr = info->addr;
650 client->irq = info->irq;
651
David Brownell9c1600e2007-05-01 23:26:31 +0200652 strlcpy(client->name, info->type, sizeof(client->name));
653
Jean Delvare3a89db52010-06-03 11:33:52 +0200654 /* Check for address validity */
655 status = i2c_check_client_addr_validity(client);
656 if (status) {
657 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
658 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
659 goto out_err_silent;
660 }
661
Jean Delvaref8a227e2009-06-19 16:58:18 +0200662 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200663 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200664 if (status)
665 goto out_err;
666
667 client->dev.parent = &client->adapter->dev;
668 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200669 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700670 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100671 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200672
Jean Delvarecbb44512011-11-23 11:33:07 +0100673 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200674 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100675 client->addr | ((client->flags & I2C_CLIENT_TEN)
676 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200677 status = device_register(&client->dev);
678 if (status)
679 goto out_err;
680
Jean Delvaref8a227e2009-06-19 16:58:18 +0200681 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
682 client->name, dev_name(&client->dev));
683
David Brownell9c1600e2007-05-01 23:26:31 +0200684 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200685
686out_err:
687 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
688 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200689out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200690 kfree(client);
691 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200692}
693EXPORT_SYMBOL_GPL(i2c_new_device);
694
695
696/**
697 * i2c_unregister_device - reverse effect of i2c_new_device()
698 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200699 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200700 */
701void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200702{
David Brownella1d9e6e2007-05-01 23:26:30 +0200703 device_unregister(&client->dev);
704}
David Brownell9c1600e2007-05-01 23:26:31 +0200705EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200706
707
Jean Delvare60b129d2008-05-11 20:37:06 +0200708static const struct i2c_device_id dummy_id[] = {
709 { "dummy", 0 },
710 { },
711};
712
Jean Delvared2653e92008-04-29 23:11:39 +0200713static int dummy_probe(struct i2c_client *client,
714 const struct i2c_device_id *id)
715{
716 return 0;
717}
718
719static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100720{
721 return 0;
722}
723
724static struct i2c_driver dummy_driver = {
725 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200726 .probe = dummy_probe,
727 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200728 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100729};
730
731/**
732 * i2c_new_dummy - return a new i2c device bound to a dummy driver
733 * @adapter: the adapter managing the device
734 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100735 * Context: can sleep
736 *
737 * This returns an I2C client bound to the "dummy" driver, intended for use
738 * with devices that consume multiple addresses. Examples of such chips
739 * include various EEPROMS (like 24c04 and 24c08 models).
740 *
741 * These dummy devices have two main uses. First, most I2C and SMBus calls
742 * except i2c_transfer() need a client handle; the dummy will be that handle.
743 * And second, this prevents the specified address from being bound to a
744 * different driver.
745 *
746 * This returns the new i2c client, which should be saved for later use with
747 * i2c_unregister_device(); or NULL to indicate an error.
748 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100749struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100750{
751 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200752 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100753 };
754
David Brownelle9f13732008-01-27 18:14:52 +0100755 return i2c_new_device(adapter, &info);
756}
757EXPORT_SYMBOL_GPL(i2c_new_dummy);
758
David Brownellf37dd802007-02-13 22:09:00 +0100759/* ------------------------------------------------------------------------- */
760
David Brownell16ffadf2007-05-01 23:26:28 +0200761/* I2C bus adapters -- one roots each I2C or SMBUS segment */
762
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200763static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
David Brownellef2c83212007-05-01 23:26:28 +0200765 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 complete(&adap->dev_released);
767}
768
Jean Delvare99cd8e22009-06-19 16:58:20 +0200769/*
Jean Delvare390946b2012-09-10 10:14:02 +0200770 * This function is only needed for mutex_lock_nested, so it is never
771 * called unless locking correctness checking is enabled. Thus we
772 * make it inline to avoid a compiler warning. That's what gcc ends up
773 * doing anyway.
774 */
775static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
776{
777 unsigned int depth = 0;
778
779 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
780 depth++;
781
782 return depth;
783}
784
785/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200786 * Let users instantiate I2C devices through sysfs. This can be used when
787 * platform initialization code doesn't contain the proper data for
788 * whatever reason. Also useful for drivers that do device detection and
789 * detection fails, either because the device uses an unexpected address,
790 * or this is a compatible device with different ID register values.
791 *
792 * Parameter checking may look overzealous, but we really don't want
793 * the user to provide incorrect parameters.
794 */
795static ssize_t
796i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
797 const char *buf, size_t count)
798{
799 struct i2c_adapter *adap = to_i2c_adapter(dev);
800 struct i2c_board_info info;
801 struct i2c_client *client;
802 char *blank, end;
803 int res;
804
Jean Delvare99cd8e22009-06-19 16:58:20 +0200805 memset(&info, 0, sizeof(struct i2c_board_info));
806
807 blank = strchr(buf, ' ');
808 if (!blank) {
809 dev_err(dev, "%s: Missing parameters\n", "new_device");
810 return -EINVAL;
811 }
812 if (blank - buf > I2C_NAME_SIZE - 1) {
813 dev_err(dev, "%s: Invalid device name\n", "new_device");
814 return -EINVAL;
815 }
816 memcpy(info.type, buf, blank - buf);
817
818 /* Parse remaining parameters, reject extra parameters */
819 res = sscanf(++blank, "%hi%c", &info.addr, &end);
820 if (res < 1) {
821 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
822 return -EINVAL;
823 }
824 if (res > 1 && end != '\n') {
825 dev_err(dev, "%s: Extra parameters\n", "new_device");
826 return -EINVAL;
827 }
828
Jean Delvare99cd8e22009-06-19 16:58:20 +0200829 client = i2c_new_device(adap, &info);
830 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200831 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200832
833 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200834 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200835 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200836 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200837 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
838 info.type, info.addr);
839
840 return count;
841}
842
843/*
844 * And of course let the users delete the devices they instantiated, if
845 * they got it wrong. This interface can only be used to delete devices
846 * instantiated by i2c_sysfs_new_device above. This guarantees that we
847 * don't delete devices to which some kernel code still has references.
848 *
849 * Parameter checking may look overzealous, but we really don't want
850 * the user to delete the wrong device.
851 */
852static ssize_t
853i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
854 const char *buf, size_t count)
855{
856 struct i2c_adapter *adap = to_i2c_adapter(dev);
857 struct i2c_client *client, *next;
858 unsigned short addr;
859 char end;
860 int res;
861
862 /* Parse parameters, reject extra parameters */
863 res = sscanf(buf, "%hi%c", &addr, &end);
864 if (res < 1) {
865 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
866 return -EINVAL;
867 }
868 if (res > 1 && end != '\n') {
869 dev_err(dev, "%s: Extra parameters\n", "delete_device");
870 return -EINVAL;
871 }
872
873 /* Make sure the device was added through sysfs */
874 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200875 mutex_lock_nested(&adap->userspace_clients_lock,
876 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200877 list_for_each_entry_safe(client, next, &adap->userspace_clients,
878 detected) {
879 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200880 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
881 "delete_device", client->name, client->addr);
882
883 list_del(&client->detected);
884 i2c_unregister_device(client);
885 res = count;
886 break;
887 }
888 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200889 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200890
891 if (res < 0)
892 dev_err(dev, "%s: Can't find device in list\n",
893 "delete_device");
894 return res;
895}
896
Jean Delvare4f8cf822009-09-18 22:45:46 +0200897static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200898static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
899 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200900
901static struct attribute *i2c_adapter_attrs[] = {
902 &dev_attr_name.attr,
903 &dev_attr_new_device.attr,
904 &dev_attr_delete_device.attr,
905 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200906};
907
Jean Delvare4f8cf822009-09-18 22:45:46 +0200908static struct attribute_group i2c_adapter_attr_group = {
909 .attrs = i2c_adapter_attrs,
910};
911
912static const struct attribute_group *i2c_adapter_attr_groups[] = {
913 &i2c_adapter_attr_group,
914 NULL
915};
916
Michael Lawnick08263742010-08-11 18:21:02 +0200917struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200918 .groups = i2c_adapter_attr_groups,
919 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200920};
Michael Lawnick08263742010-08-11 18:21:02 +0200921EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Stephen Warren643dd092012-04-17 12:43:33 -0600923/**
924 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
925 * @dev: device, probably from some driver model iterator
926 *
927 * When traversing the driver model tree, perhaps using driver model
928 * iterators like @device_for_each_child(), you can't assume very much
929 * about the nodes you find. Use this function to avoid oopses caused
930 * by wrongly treating some non-I2C device as an i2c_adapter.
931 */
932struct i2c_adapter *i2c_verify_adapter(struct device *dev)
933{
934 return (dev->type == &i2c_adapter_type)
935 ? to_i2c_adapter(dev)
936 : NULL;
937}
938EXPORT_SYMBOL(i2c_verify_adapter);
939
Jean Delvare2bb50952009-09-18 22:45:46 +0200940#ifdef CONFIG_I2C_COMPAT
941static struct class_compat *i2c_adapter_compat_class;
942#endif
943
David Brownell9c1600e2007-05-01 23:26:31 +0200944static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
945{
946 struct i2c_devinfo *devinfo;
947
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200948 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200949 list_for_each_entry(devinfo, &__i2c_board_list, list) {
950 if (devinfo->busnum == adapter->nr
951 && !i2c_new_device(adapter,
952 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100953 dev_err(&adapter->dev,
954 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200955 devinfo->board_info.addr);
956 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200957 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200958}
959
Jean Delvare69b00892009-12-06 17:06:27 +0100960static int i2c_do_add_adapter(struct i2c_driver *driver,
961 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +0100962{
Jean Delvare4735c982008-07-14 22:38:36 +0200963 /* Detect supported devices on that bus, and instantiate them */
964 i2c_detect(adap, driver);
965
966 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +0100967 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +0200968 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
969 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +0100970 dev_warn(&adap->dev, "Please use another way to instantiate "
971 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +0100972 /* We ignore the return code; if it fails, too bad */
973 driver->attach_adapter(adap);
974 }
975 return 0;
976}
977
Jean Delvare69b00892009-12-06 17:06:27 +0100978static int __process_new_adapter(struct device_driver *d, void *data)
979{
980 return i2c_do_add_adapter(to_i2c_driver(d), data);
981}
982
David Brownell6e13e642007-05-01 23:26:31 +0200983static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984{
Jean Delvared6703282010-08-11 18:20:59 +0200985 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
David Brownell1d0b19c2008-10-14 17:30:05 +0200987 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +0200988 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
989 res = -EAGAIN;
990 goto out_list;
991 }
David Brownell1d0b19c2008-10-14 17:30:05 +0200992
Jean Delvare2236baa2010-11-15 22:40:38 +0100993 /* Sanity checks */
994 if (unlikely(adap->name[0] == '\0')) {
995 pr_err("i2c-core: Attempt to register an adapter with "
996 "no name!\n");
997 return -EINVAL;
998 }
999 if (unlikely(!adap->algo)) {
1000 pr_err("i2c-core: Attempt to register adapter '%s' with "
1001 "no algo!\n", adap->name);
1002 return -EINVAL;
1003 }
1004
Mika Kuoppala194684e2009-12-06 17:06:22 +01001005 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001006 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001007 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Jean Delvare8fcfef62009-03-28 21:34:43 +01001009 /* Set default timeout to 1 second if not already set */
1010 if (adap->timeout == 0)
1011 adap->timeout = HZ;
1012
Kay Sievers27d9c182009-01-07 14:29:16 +01001013 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001014 adap->dev.bus = &i2c_bus_type;
1015 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001016 res = device_register(&adap->dev);
1017 if (res)
1018 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001020 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1021
Jean Delvare2bb50952009-09-18 22:45:46 +02001022#ifdef CONFIG_I2C_COMPAT
1023 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1024 adap->dev.parent);
1025 if (res)
1026 dev_warn(&adap->dev,
1027 "Failed to create compatibility class link\n");
1028#endif
1029
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301030 /* bus recovery specific initialization */
1031 if (adap->bus_recovery_info) {
1032 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1033
1034 if (!bri->recover_bus) {
1035 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1036 adap->bus_recovery_info = NULL;
1037 goto exit_recovery;
1038 }
1039
1040 /* Generic GPIO recovery */
1041 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1042 if (!gpio_is_valid(bri->scl_gpio)) {
1043 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1044 adap->bus_recovery_info = NULL;
1045 goto exit_recovery;
1046 }
1047
1048 if (gpio_is_valid(bri->sda_gpio))
1049 bri->get_sda = get_sda_gpio_value;
1050 else
1051 bri->get_sda = NULL;
1052
1053 bri->get_scl = get_scl_gpio_value;
1054 bri->set_scl = set_scl_gpio_value;
1055 } else if (!bri->set_scl || !bri->get_scl) {
1056 /* Generic SCL recovery */
1057 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1058 adap->bus_recovery_info = NULL;
1059 }
1060 }
1061
1062exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001063 /* create pre-declared device nodes */
David Brownell6e13e642007-05-01 23:26:31 +02001064 if (adap->nr < __i2c_first_dynamic_bus_num)
1065 i2c_scan_static_board_info(adap);
1066
Jean Delvare4735c982008-07-14 22:38:36 +02001067 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001068 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001069 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001070 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001071
1072 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001073
Jean Delvareb119c6c2006-08-15 18:26:30 +02001074out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001075 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001076 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001077 mutex_unlock(&core_lock);
1078 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
1080
David Brownell6e13e642007-05-01 23:26:31 +02001081/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001082 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1083 * @adap: the adapter to register (with adap->nr initialized)
1084 * Context: can sleep
1085 *
1086 * See i2c_add_numbered_adapter() for details.
1087 */
1088static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1089{
1090 int id;
1091
1092 mutex_lock(&core_lock);
1093 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1094 GFP_KERNEL);
1095 mutex_unlock(&core_lock);
1096 if (id < 0)
1097 return id == -ENOSPC ? -EBUSY : id;
1098
1099 return i2c_register_adapter(adap);
1100}
1101
1102/**
David Brownell6e13e642007-05-01 23:26:31 +02001103 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1104 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001105 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001106 *
1107 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001108 * doesn't matter or when its bus number is specified by an dt alias.
1109 * Examples of bases when the bus number doesn't matter: I2C adapters
1110 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001111 *
1112 * When this returns zero, a new bus number was allocated and stored
1113 * in adap->nr, and the specified adapter became available for clients.
1114 * Otherwise, a negative errno value is returned.
1115 */
1116int i2c_add_adapter(struct i2c_adapter *adapter)
1117{
Doug Andersonee5c2742013-03-01 08:57:31 -08001118 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001119 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001120
Doug Andersonee5c2742013-03-01 08:57:31 -08001121 if (dev->of_node) {
1122 id = of_alias_get_id(dev->of_node, "i2c");
1123 if (id >= 0) {
1124 adapter->nr = id;
1125 return __i2c_add_numbered_adapter(adapter);
1126 }
1127 }
1128
Jean Delvarecaada322008-01-27 18:14:49 +01001129 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001130 id = idr_alloc(&i2c_adapter_idr, adapter,
1131 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001132 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001133 if (id < 0)
1134 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001135
1136 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001137
David Brownell6e13e642007-05-01 23:26:31 +02001138 return i2c_register_adapter(adapter);
1139}
1140EXPORT_SYMBOL(i2c_add_adapter);
1141
1142/**
1143 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1144 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001145 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001146 *
1147 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001148 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1149 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001150 * is used to properly configure I2C devices.
1151 *
Grant Likely488bf312011-07-25 17:49:43 +02001152 * If the requested bus number is set to -1, then this function will behave
1153 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1154 *
David Brownell6e13e642007-05-01 23:26:31 +02001155 * If no devices have pre-been declared for this bus, then be sure to
1156 * register the adapter before any dynamically allocated ones. Otherwise
1157 * the required bus ID may not be available.
1158 *
1159 * When this returns zero, the specified adapter became available for
1160 * clients using the bus number provided in adap->nr. Also, the table
1161 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1162 * and the appropriate driver model device nodes are created. Otherwise, a
1163 * negative errno value is returned.
1164 */
1165int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1166{
Grant Likely488bf312011-07-25 17:49:43 +02001167 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1168 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001169
Doug Andersonee5c2742013-03-01 08:57:31 -08001170 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001171}
1172EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1173
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001174static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001175 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001176{
Jean Delvare4735c982008-07-14 22:38:36 +02001177 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001178
Jean Delvareacec2112009-03-28 21:34:40 +01001179 /* Remove the devices we created ourselves as the result of hardware
1180 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001181 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1182 if (client->adapter == adapter) {
1183 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1184 client->name, client->addr);
1185 list_del(&client->detected);
1186 i2c_unregister_device(client);
1187 }
1188 }
Jean Delvare026526f2008-01-27 18:14:49 +01001189}
1190
Jean Delvaree549c2b2009-06-19 16:58:19 +02001191static int __unregister_client(struct device *dev, void *dummy)
1192{
1193 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001194 if (client && strcmp(client->name, "dummy"))
1195 i2c_unregister_device(client);
1196 return 0;
1197}
1198
1199static int __unregister_dummy(struct device *dev, void *dummy)
1200{
1201 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001202 if (client)
1203 i2c_unregister_device(client);
1204 return 0;
1205}
1206
Jean Delvare69b00892009-12-06 17:06:27 +01001207static int __process_removed_adapter(struct device_driver *d, void *data)
1208{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001209 i2c_do_del_adapter(to_i2c_driver(d), data);
1210 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001211}
1212
David Brownelld64f73b2007-07-12 14:12:28 +02001213/**
1214 * i2c_del_adapter - unregister I2C adapter
1215 * @adap: the adapter being unregistered
1216 * Context: can sleep
1217 *
1218 * This unregisters an I2C adapter which was previously registered
1219 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1220 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001221void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001223 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001224 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001227 mutex_lock(&core_lock);
1228 found = idr_find(&i2c_adapter_idr, adap->nr);
1229 mutex_unlock(&core_lock);
1230 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001231 pr_debug("i2c-core: attempting to delete unregistered "
1232 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001233 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Jean Delvare026526f2008-01-27 18:14:49 +01001236 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001237 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001238 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001239 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001240 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001242 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001243 mutex_lock_nested(&adap->userspace_clients_lock,
1244 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001245 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1246 detected) {
1247 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1248 client->addr);
1249 list_del(&client->detected);
1250 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001251 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001252 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001253
Jean Delvaree549c2b2009-06-19 16:58:19 +02001254 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001255 * check the returned value. This is a two-pass process, because
1256 * we can't remove the dummy devices during the first pass: they
1257 * could have been instantiated by real devices wishing to clean
1258 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001259 device_for_each_child(&adap->dev, NULL, __unregister_client);
1260 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Jean Delvare2bb50952009-09-18 22:45:46 +02001262#ifdef CONFIG_I2C_COMPAT
1263 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1264 adap->dev.parent);
1265#endif
1266
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001267 /* device name is gone after device_unregister */
1268 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 /* clean up the sysfs representation */
1271 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /* wait for sysfs to drop all references */
1275 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
David Brownell6e13e642007-05-01 23:26:31 +02001277 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001278 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001280 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001282 /* Clear the device structure in case this adapter is ever going to be
1283 added again */
1284 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
David Brownellc0564602007-05-01 23:26:31 +02001286EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288
David Brownell7b4fbc52007-05-01 23:26:30 +02001289/* ------------------------------------------------------------------------- */
1290
Jean Delvare7ae31482011-03-20 14:50:52 +01001291int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1292{
1293 int res;
1294
1295 mutex_lock(&core_lock);
1296 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1297 mutex_unlock(&core_lock);
1298
1299 return res;
1300}
1301EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1302
Jean Delvare69b00892009-12-06 17:06:27 +01001303static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001304{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001305 if (dev->type != &i2c_adapter_type)
1306 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001307 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001308}
1309
David Brownell7b4fbc52007-05-01 23:26:30 +02001310/*
1311 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001312 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 */
1314
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001315int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001317 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
David Brownell1d0b19c2008-10-14 17:30:05 +02001319 /* Can't register until after driver model init */
1320 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1321 return -EAGAIN;
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001324 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Jean Delvare729d6dd2009-06-19 16:58:18 +02001327 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001328 * will have called probe() for all matching-but-unbound devices.
1329 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 res = driver_register(&driver->driver);
1331 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001332 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001333
Mark Brownf4e8db32011-01-14 22:03:50 +01001334 /* Drivers should switch to dev_pm_ops instead. */
1335 if (driver->suspend)
1336 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1337 driver->driver.name);
1338 if (driver->resume)
1339 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1340 driver->driver.name);
1341
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001342 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Jean Delvare4735c982008-07-14 22:38:36 +02001344 INIT_LIST_HEAD(&driver->clients);
1345 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001346 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001347
Jean Delvare7eebcb72006-02-05 23:28:21 +01001348 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001350EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Jean Delvare69b00892009-12-06 17:06:27 +01001352static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001353{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001354 if (dev->type == &i2c_adapter_type)
1355 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1356 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001357}
1358
David Brownella1d9e6e2007-05-01 23:26:30 +02001359/**
1360 * i2c_del_driver - unregister I2C driver
1361 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001362 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001363 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001364void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Jean Delvare7ae31482011-03-20 14:50:52 +01001366 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001369 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
David Brownellc0564602007-05-01 23:26:31 +02001371EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
David Brownell7b4fbc52007-05-01 23:26:30 +02001373/* ------------------------------------------------------------------------- */
1374
Jean Delvaree48d3312008-01-27 18:14:48 +01001375/**
1376 * i2c_use_client - increments the reference count of the i2c client structure
1377 * @client: the client being referenced
1378 *
1379 * Each live reference to a client should be refcounted. The driver model does
1380 * that automatically as part of driver binding, so that most drivers don't
1381 * need to do this explicitly: they hold a reference until they're unbound
1382 * from the device.
1383 *
1384 * A pointer to the client with the incremented reference counter is returned.
1385 */
1386struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
David Brownell6ea438e2008-07-14 22:38:24 +02001388 if (client && get_device(&client->dev))
1389 return client;
1390 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
David Brownellc0564602007-05-01 23:26:31 +02001392EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Jean Delvaree48d3312008-01-27 18:14:48 +01001394/**
1395 * i2c_release_client - release a use of the i2c client structure
1396 * @client: the client being no longer referenced
1397 *
1398 * Must be called when a user of a client is finished with it.
1399 */
1400void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
David Brownell6ea438e2008-07-14 22:38:24 +02001402 if (client)
1403 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
David Brownellc0564602007-05-01 23:26:31 +02001405EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
David Brownell9b766b82008-01-27 18:14:51 +01001407struct i2c_cmd_arg {
1408 unsigned cmd;
1409 void *arg;
1410};
1411
1412static int i2c_cmd(struct device *dev, void *_arg)
1413{
1414 struct i2c_client *client = i2c_verify_client(dev);
1415 struct i2c_cmd_arg *arg = _arg;
1416
1417 if (client && client->driver && client->driver->command)
1418 client->driver->command(client, arg->cmd, arg->arg);
1419 return 0;
1420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1423{
David Brownell9b766b82008-01-27 18:14:51 +01001424 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
David Brownell9b766b82008-01-27 18:14:51 +01001426 cmd_arg.cmd = cmd;
1427 cmd_arg.arg = arg;
1428 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
David Brownellc0564602007-05-01 23:26:31 +02001430EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432static int __init i2c_init(void)
1433{
1434 int retval;
1435
1436 retval = bus_register(&i2c_bus_type);
1437 if (retval)
1438 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001439#ifdef CONFIG_I2C_COMPAT
1440 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1441 if (!i2c_adapter_compat_class) {
1442 retval = -ENOMEM;
1443 goto bus_err;
1444 }
1445#endif
David Brownelle9f13732008-01-27 18:14:52 +01001446 retval = i2c_add_driver(&dummy_driver);
1447 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001448 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001449 return 0;
1450
Jean Delvare2bb50952009-09-18 22:45:46 +02001451class_err:
1452#ifdef CONFIG_I2C_COMPAT
1453 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001454bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001455#endif
David Brownelle9f13732008-01-27 18:14:52 +01001456 bus_unregister(&i2c_bus_type);
1457 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460static void __exit i2c_exit(void)
1461{
David Brownelle9f13732008-01-27 18:14:52 +01001462 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001463#ifdef CONFIG_I2C_COMPAT
1464 class_compat_unregister(i2c_adapter_compat_class);
1465#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 bus_unregister(&i2c_bus_type);
1467}
1468
David Brownella10f9e72008-10-14 17:30:06 +02001469/* We must initialize early, because some subsystems register i2c drivers
1470 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1471 */
1472postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473module_exit(i2c_exit);
1474
1475/* ----------------------------------------------------
1476 * the functional interface to the i2c busses.
1477 * ----------------------------------------------------
1478 */
1479
David Brownella1cdeda2008-07-14 22:38:24 +02001480/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001481 * __i2c_transfer - unlocked flavor of i2c_transfer
1482 * @adap: Handle to I2C bus
1483 * @msgs: One or more messages to execute before STOP is issued to
1484 * terminate the operation; each message begins with a START.
1485 * @num: Number of messages to be executed.
1486 *
1487 * Returns negative errno, else the number of messages executed.
1488 *
1489 * Adapter lock must be held when calling this function. No debug logging
1490 * takes place. adap->algo->master_xfer existence isn't checked.
1491 */
1492int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1493{
1494 unsigned long orig_jiffies;
1495 int ret, try;
1496
1497 /* Retry automatically on arbitration loss */
1498 orig_jiffies = jiffies;
1499 for (ret = 0, try = 0; try <= adap->retries; try++) {
1500 ret = adap->algo->master_xfer(adap, msgs, num);
1501 if (ret != -EAGAIN)
1502 break;
1503 if (time_after(jiffies, orig_jiffies + adap->timeout))
1504 break;
1505 }
1506
1507 return ret;
1508}
1509EXPORT_SYMBOL(__i2c_transfer);
1510
1511/**
David Brownella1cdeda2008-07-14 22:38:24 +02001512 * i2c_transfer - execute a single or combined I2C message
1513 * @adap: Handle to I2C bus
1514 * @msgs: One or more messages to execute before STOP is issued to
1515 * terminate the operation; each message begins with a START.
1516 * @num: Number of messages to be executed.
1517 *
1518 * Returns negative errno, else the number of messages executed.
1519 *
1520 * Note that there is no requirement that each message be sent to
1521 * the same slave address, although that is the most common model.
1522 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001523int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001525 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
David Brownella1cdeda2008-07-14 22:38:24 +02001527 /* REVISIT the fault reporting model here is weak:
1528 *
1529 * - When we get an error after receiving N bytes from a slave,
1530 * there is no way to report "N".
1531 *
1532 * - When we get a NAK after transmitting N bytes to a slave,
1533 * there is no way to report "N" ... or to let the master
1534 * continue executing the rest of this combined message, if
1535 * that's the appropriate response.
1536 *
1537 * - When for example "num" is two and we successfully complete
1538 * the first message but get an error part way through the
1539 * second, it's unclear whether that should be reported as
1540 * one (discarding status on the second message) or errno
1541 * (discarding status on the first one).
1542 */
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (adap->algo->master_xfer) {
1545#ifdef DEBUG
1546 for (ret = 0; ret < num; ret++) {
1547 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001548 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1549 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1550 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552#endif
1553
Mike Rapoportcea443a82008-01-27 18:14:50 +01001554 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001555 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001556 if (!ret)
1557 /* I2C activity is ongoing. */
1558 return -EAGAIN;
1559 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001560 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001561 }
1562
Jean Delvareb37d2a32012-06-29 07:47:19 -03001563 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001564 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 return ret;
1567 } else {
1568 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001569 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571}
David Brownellc0564602007-05-01 23:26:31 +02001572EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
David Brownella1cdeda2008-07-14 22:38:24 +02001574/**
1575 * i2c_master_send - issue a single I2C message in master transmit mode
1576 * @client: Handle to slave device
1577 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001578 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001579 *
1580 * Returns negative errno, or else the number of bytes written.
1581 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001582int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001585 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 struct i2c_msg msg;
1587
Jean Delvare815f55f2005-05-07 22:58:46 +02001588 msg.addr = client->addr;
1589 msg.flags = client->flags & I2C_M_TEN;
1590 msg.len = count;
1591 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001592
Jean Delvare815f55f2005-05-07 22:58:46 +02001593 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001595 /*
1596 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1597 * transmitted, else error code.
1598 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001599 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
David Brownellc0564602007-05-01 23:26:31 +02001601EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
David Brownella1cdeda2008-07-14 22:38:24 +02001603/**
1604 * i2c_master_recv - issue a single I2C message in master receive mode
1605 * @client: Handle to slave device
1606 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001607 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001608 *
1609 * Returns negative errno, or else the number of bytes read.
1610 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001611int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Farid Hammane7225acf2010-05-21 18:40:58 +02001613 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct i2c_msg msg;
1615 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Jean Delvare815f55f2005-05-07 22:58:46 +02001617 msg.addr = client->addr;
1618 msg.flags = client->flags & I2C_M_TEN;
1619 msg.flags |= I2C_M_RD;
1620 msg.len = count;
1621 msg.buf = buf;
1622
1623 ret = i2c_transfer(adap, &msg, 1);
1624
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001625 /*
1626 * If everything went ok (i.e. 1 msg received), return #bytes received,
1627 * else error code.
1628 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001629 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
David Brownellc0564602007-05-01 23:26:31 +02001631EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/* ----------------------------------------------------
1634 * the i2c address scanning function
1635 * Will not work for 10-bit addresses!
1636 * ----------------------------------------------------
1637 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001638
Jean Delvare63e4e802010-06-03 11:33:51 +02001639/*
1640 * Legacy default probe function, mostly relevant for SMBus. The default
1641 * probe method is a quick write, but it is known to corrupt the 24RF08
1642 * EEPROMs due to a state machine bug, and could also irreversibly
1643 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1644 * we use a short byte read instead. Also, some bus drivers don't implement
1645 * quick write, so we fallback to a byte read in that case too.
1646 * On x86, there is another special case for FSC hardware monitoring chips,
1647 * which want regular byte reads (address 0x73.) Fortunately, these are the
1648 * only known chips using this I2C address on PC hardware.
1649 * Returns 1 if probe succeeded, 0 if not.
1650 */
1651static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1652{
1653 int err;
1654 union i2c_smbus_data dummy;
1655
1656#ifdef CONFIG_X86
1657 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1658 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1659 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1660 I2C_SMBUS_BYTE_DATA, &dummy);
1661 else
1662#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001663 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1664 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001665 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1666 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001667 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1668 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1669 I2C_SMBUS_BYTE, &dummy);
1670 else {
1671 dev_warn(&adap->dev, "No suitable probing method supported\n");
1672 err = -EOPNOTSUPP;
1673 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001674
1675 return err >= 0;
1676}
1677
Jean Delvareccfbbd02009-12-06 17:06:25 +01001678static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001679 struct i2c_driver *driver)
1680{
1681 struct i2c_board_info info;
1682 struct i2c_adapter *adapter = temp_client->adapter;
1683 int addr = temp_client->addr;
1684 int err;
1685
1686 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001687 err = i2c_check_addr_validity(addr);
1688 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001689 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1690 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001691 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001692 }
1693
1694 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001695 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001696 return 0;
1697
Jean Delvareccfbbd02009-12-06 17:06:25 +01001698 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001699 if (!i2c_default_probe(adapter, addr))
1700 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001701
1702 /* Finally call the custom detection function */
1703 memset(&info, 0, sizeof(struct i2c_board_info));
1704 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001705 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001706 if (err) {
1707 /* -ENODEV is returned if the detection fails. We catch it
1708 here as this isn't an error. */
1709 return err == -ENODEV ? 0 : err;
1710 }
1711
1712 /* Consistency check */
1713 if (info.type[0] == '\0') {
1714 dev_err(&adapter->dev, "%s detection function provided "
1715 "no name for 0x%x\n", driver->driver.name,
1716 addr);
1717 } else {
1718 struct i2c_client *client;
1719
1720 /* Detection succeeded, instantiate the device */
1721 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1722 info.type, info.addr);
1723 client = i2c_new_device(adapter, &info);
1724 if (client)
1725 list_add_tail(&client->detected, &driver->clients);
1726 else
1727 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1728 info.type, info.addr);
1729 }
1730 return 0;
1731}
1732
1733static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1734{
Jean Delvarec3813d62009-12-14 21:17:25 +01001735 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001736 struct i2c_client *temp_client;
1737 int i, err = 0;
1738 int adap_id = i2c_adapter_id(adapter);
1739
Jean Delvarec3813d62009-12-14 21:17:25 +01001740 address_list = driver->address_list;
1741 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001742 return 0;
1743
Jean Delvare51b54ba2010-10-24 18:16:58 +02001744 /* Stop here if the classes do not match */
1745 if (!(adapter->class & driver->class))
1746 return 0;
1747
Jean Delvare4735c982008-07-14 22:38:36 +02001748 /* Set up a temporary client to help detect callback */
1749 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1750 if (!temp_client)
1751 return -ENOMEM;
1752 temp_client->adapter = adapter;
1753
Jean Delvarec3813d62009-12-14 21:17:25 +01001754 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001755 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001756 "addr 0x%02x\n", adap_id, address_list[i]);
1757 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001758 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001759 if (unlikely(err))
1760 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001761 }
1762
Jean Delvare4735c982008-07-14 22:38:36 +02001763 kfree(temp_client);
1764 return err;
1765}
1766
Jean Delvared44f19d2010-08-11 18:20:57 +02001767int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1768{
1769 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1770 I2C_SMBUS_QUICK, NULL) >= 0;
1771}
1772EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1773
Jean Delvare12b5053a2007-05-01 23:26:31 +02001774struct i2c_client *
1775i2c_new_probed_device(struct i2c_adapter *adap,
1776 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001777 unsigned short const *addr_list,
1778 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001779{
1780 int i;
1781
Jean Delvare8031d792010-08-11 18:21:00 +02001782 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001783 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001784
Jean Delvare12b5053a2007-05-01 23:26:31 +02001785 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1786 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001787 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001788 dev_warn(&adap->dev, "Invalid 7-bit address "
1789 "0x%02x\n", addr_list[i]);
1790 continue;
1791 }
1792
1793 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001794 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001795 dev_dbg(&adap->dev, "Address 0x%02x already in "
1796 "use, not probing\n", addr_list[i]);
1797 continue;
1798 }
1799
Jean Delvare63e4e802010-06-03 11:33:51 +02001800 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001801 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001802 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001803 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001804
1805 if (addr_list[i] == I2C_CLIENT_END) {
1806 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1807 return NULL;
1808 }
1809
1810 info->addr = addr_list[i];
1811 return i2c_new_device(adap, info);
1812}
1813EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1814
Jean Delvared735b342011-03-20 14:50:52 +01001815struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01001818
Jean Delvarecaada322008-01-27 18:14:49 +01001819 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01001820 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001821 if (adapter && !try_module_get(adapter->owner))
1822 adapter = NULL;
1823
Jean Delvarecaada322008-01-27 18:14:49 +01001824 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04001825 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
David Brownellc0564602007-05-01 23:26:31 +02001827EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829void i2c_put_adapter(struct i2c_adapter *adap)
1830{
1831 module_put(adap->owner);
1832}
David Brownellc0564602007-05-01 23:26:31 +02001833EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835/* The SMBus parts */
1836
David Brownell438d6c22006-12-10 21:21:31 +01001837#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001838static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
1840 int i;
David Brownell438d6c22006-12-10 21:21:31 +01001841
Farid Hammane7225acf2010-05-21 18:40:58 +02001842 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01001843 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 data = data ^ POLY;
1845 data = data << 1;
1846 }
1847 return (u8)(data >> 8);
1848}
1849
Jean Delvare421ef472005-10-26 21:28:55 +02001850/* Incremental CRC8 over count bytes in the array pointed to by p */
1851static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 int i;
1854
Farid Hammane7225acf2010-05-21 18:40:58 +02001855 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02001856 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return crc;
1858}
1859
Jean Delvare421ef472005-10-26 21:28:55 +02001860/* Assume a 7-bit address, which is reasonable for SMBus */
1861static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
Jean Delvare421ef472005-10-26 21:28:55 +02001863 /* The address will be sent first */
1864 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1865 pec = i2c_smbus_pec(pec, &addr, 1);
1866
1867 /* The data buffer follows */
1868 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
Jean Delvare421ef472005-10-26 21:28:55 +02001871/* Used for write only transactions */
1872static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Jean Delvare421ef472005-10-26 21:28:55 +02001874 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1875 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Jean Delvare421ef472005-10-26 21:28:55 +02001878/* Return <0 on CRC error
1879 If there was a write before this read (most cases) we need to take the
1880 partial CRC from the write part into account.
1881 Note that this function does modify the message (we need to decrease the
1882 message length to hide the CRC byte from the caller). */
1883static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Jean Delvare421ef472005-10-26 21:28:55 +02001885 u8 rpec = msg->buf[--msg->len];
1886 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (rpec != cpec) {
1889 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1890 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02001891 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
David Brownell438d6c22006-12-10 21:21:31 +01001893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
David Brownella1cdeda2008-07-14 22:38:24 +02001896/**
1897 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1898 * @client: Handle to slave device
1899 *
1900 * This executes the SMBus "receive byte" protocol, returning negative errno
1901 * else the byte received from the device.
1902 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001903s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001906 int status;
1907
1908 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1909 I2C_SMBUS_READ, 0,
1910 I2C_SMBUS_BYTE, &data);
1911 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
David Brownellc0564602007-05-01 23:26:31 +02001913EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
David Brownella1cdeda2008-07-14 22:38:24 +02001915/**
1916 * i2c_smbus_write_byte - SMBus "send byte" protocol
1917 * @client: Handle to slave device
1918 * @value: Byte to be sent
1919 *
1920 * This executes the SMBus "send byte" protocol, returning negative errno
1921 * else zero on success.
1922 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001923s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Farid Hammane7225acf2010-05-21 18:40:58 +02001925 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02001926 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
David Brownellc0564602007-05-01 23:26:31 +02001928EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
David Brownella1cdeda2008-07-14 22:38:24 +02001930/**
1931 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1932 * @client: Handle to slave device
1933 * @command: Byte interpreted by slave
1934 *
1935 * This executes the SMBus "read byte" protocol, returning negative errno
1936 * else a data byte received from the device.
1937 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001938s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001941 int status;
1942
1943 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1944 I2C_SMBUS_READ, command,
1945 I2C_SMBUS_BYTE_DATA, &data);
1946 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
David Brownellc0564602007-05-01 23:26:31 +02001948EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
David Brownella1cdeda2008-07-14 22:38:24 +02001950/**
1951 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1952 * @client: Handle to slave device
1953 * @command: Byte interpreted by slave
1954 * @value: Byte being written
1955 *
1956 * This executes the SMBus "write byte" protocol, returning negative errno
1957 * else zero on success.
1958 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001959s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
1960 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961{
1962 union i2c_smbus_data data;
1963 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02001964 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1965 I2C_SMBUS_WRITE, command,
1966 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
David Brownellc0564602007-05-01 23:26:31 +02001968EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
David Brownella1cdeda2008-07-14 22:38:24 +02001970/**
1971 * i2c_smbus_read_word_data - SMBus "read word" protocol
1972 * @client: Handle to slave device
1973 * @command: Byte interpreted by slave
1974 *
1975 * This executes the SMBus "read word" protocol, returning negative errno
1976 * else a 16-bit unsigned "word" received from the device.
1977 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001978s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02001981 int status;
1982
1983 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1984 I2C_SMBUS_READ, command,
1985 I2C_SMBUS_WORD_DATA, &data);
1986 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
David Brownellc0564602007-05-01 23:26:31 +02001988EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
David Brownella1cdeda2008-07-14 22:38:24 +02001990/**
1991 * i2c_smbus_write_word_data - SMBus "write word" protocol
1992 * @client: Handle to slave device
1993 * @command: Byte interpreted by slave
1994 * @value: 16-bit "word" being written
1995 *
1996 * This executes the SMBus "write word" protocol, returning negative errno
1997 * else zero on success.
1998 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001999s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2000 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 union i2c_smbus_data data;
2003 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002004 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2005 I2C_SMBUS_WRITE, command,
2006 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
David Brownellc0564602007-05-01 23:26:31 +02002008EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
David Brownella64ec072007-10-13 23:56:31 +02002010/**
David Brownella1cdeda2008-07-14 22:38:24 +02002011 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002012 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002013 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002014 * @values: Byte array into which data will be read; big enough to hold
2015 * the data returned by the slave. SMBus allows at most 32 bytes.
2016 *
David Brownella1cdeda2008-07-14 22:38:24 +02002017 * This executes the SMBus "block read" protocol, returning negative errno
2018 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002019 *
2020 * Note that using this function requires that the client's adapter support
2021 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2022 * support this; its emulation through I2C messaging relies on a specific
2023 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2024 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002025s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002026 u8 *values)
2027{
2028 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002029 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002030
David Brownell24a5bb72008-07-14 22:38:23 +02002031 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2032 I2C_SMBUS_READ, command,
2033 I2C_SMBUS_BLOCK_DATA, &data);
2034 if (status)
2035 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002036
2037 memcpy(values, &data.block[1], data.block[0]);
2038 return data.block[0];
2039}
2040EXPORT_SYMBOL(i2c_smbus_read_block_data);
2041
David Brownella1cdeda2008-07-14 22:38:24 +02002042/**
2043 * i2c_smbus_write_block_data - SMBus "block write" protocol
2044 * @client: Handle to slave device
2045 * @command: Byte interpreted by slave
2046 * @length: Size of data block; SMBus allows at most 32 bytes
2047 * @values: Byte array which will be written.
2048 *
2049 * This executes the SMBus "block write" protocol, returning negative errno
2050 * else zero on success.
2051 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002052s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002053 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054{
2055 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (length > I2C_SMBUS_BLOCK_MAX)
2058 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002060 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002061 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2062 I2C_SMBUS_WRITE, command,
2063 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
David Brownellc0564602007-05-01 23:26:31 +02002065EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002068s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002069 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002072 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002073
Jean Delvare4b2643d2007-07-12 14:12:29 +02002074 if (length > I2C_SMBUS_BLOCK_MAX)
2075 length = I2C_SMBUS_BLOCK_MAX;
2076 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002077 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2078 I2C_SMBUS_READ, command,
2079 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2080 if (status < 0)
2081 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002082
2083 memcpy(values, &data.block[1], data.block[0]);
2084 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
David Brownellc0564602007-05-01 23:26:31 +02002086EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Jean Delvare0cc43a12011-01-10 22:11:23 +01002088s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002089 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002090{
2091 union i2c_smbus_data data;
2092
2093 if (length > I2C_SMBUS_BLOCK_MAX)
2094 length = I2C_SMBUS_BLOCK_MAX;
2095 data.block[0] = length;
2096 memcpy(data.block + 1, values, length);
2097 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2098 I2C_SMBUS_WRITE, command,
2099 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2100}
David Brownellc0564602007-05-01 23:26:31 +02002101EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002102
David Brownell438d6c22006-12-10 21:21:31 +01002103/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002105static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2106 unsigned short flags,
2107 char read_write, u8 command, int size,
2108 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 /* So we need to generate a series of msgs. In the case of writing, we
2111 need to use only one message; when reading, we need two. We initialize
2112 most things with sane defaults, to keep the code below somewhat
2113 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002114 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2115 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002116 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002118 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002119 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002120 struct i2c_msg msg[2] = {
2121 {
2122 .addr = addr,
2123 .flags = flags,
2124 .len = 1,
2125 .buf = msgbuf0,
2126 }, {
2127 .addr = addr,
2128 .flags = flags | I2C_M_RD,
2129 .len = 0,
2130 .buf = msgbuf1,
2131 },
2132 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002135 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 case I2C_SMBUS_QUICK:
2137 msg[0].len = 0;
2138 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002139 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2140 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 num = 1;
2142 break;
2143 case I2C_SMBUS_BYTE:
2144 if (read_write == I2C_SMBUS_READ) {
2145 /* Special case: only a read! */
2146 msg[0].flags = I2C_M_RD | flags;
2147 num = 1;
2148 }
2149 break;
2150 case I2C_SMBUS_BYTE_DATA:
2151 if (read_write == I2C_SMBUS_READ)
2152 msg[1].len = 1;
2153 else {
2154 msg[0].len = 2;
2155 msgbuf0[1] = data->byte;
2156 }
2157 break;
2158 case I2C_SMBUS_WORD_DATA:
2159 if (read_write == I2C_SMBUS_READ)
2160 msg[1].len = 2;
2161 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002162 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002164 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166 break;
2167 case I2C_SMBUS_PROC_CALL:
2168 num = 2; /* Special case */
2169 read_write = I2C_SMBUS_READ;
2170 msg[0].len = 3;
2171 msg[1].len = 2;
2172 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002173 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002177 msg[1].flags |= I2C_M_RECV_LEN;
2178 msg[1].len = 1; /* block length will be added by
2179 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 } else {
2181 msg[0].len = data->block[0] + 2;
2182 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002183 dev_err(&adapter->dev,
2184 "Invalid block write size %d\n",
2185 data->block[0]);
2186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002188 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 msgbuf0[i] = data->block[i-1];
2190 }
2191 break;
2192 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002193 num = 2; /* Another special case */
2194 read_write = I2C_SMBUS_READ;
2195 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002196 dev_err(&adapter->dev,
2197 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002198 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002199 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002200 }
2201 msg[0].len = data->block[0] + 2;
2202 for (i = 1; i < msg[0].len; i++)
2203 msgbuf0[i] = data->block[i-1];
2204 msg[1].flags |= I2C_M_RECV_LEN;
2205 msg[1].len = 1; /* block length will be added by
2206 the underlying bus driver */
2207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 case I2C_SMBUS_I2C_BLOCK_DATA:
2209 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002210 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 } else {
2212 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002213 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002214 dev_err(&adapter->dev,
2215 "Invalid block write size %d\n",
2216 data->block[0]);
2217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219 for (i = 1; i <= data->block[0]; i++)
2220 msgbuf0[i] = data->block[i];
2221 }
2222 break;
2223 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002224 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2225 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
2227
Jean Delvare421ef472005-10-26 21:28:55 +02002228 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2229 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2230 if (i) {
2231 /* Compute PEC if first message is a write */
2232 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002233 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002234 i2c_smbus_add_pec(&msg[0]);
2235 else /* Write followed by read */
2236 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2237 }
2238 /* Ask for PEC if last message is a read */
2239 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002240 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002241 }
2242
David Brownell24a5bb72008-07-14 22:38:23 +02002243 status = i2c_transfer(adapter, msg, num);
2244 if (status < 0)
2245 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Jean Delvare421ef472005-10-26 21:28:55 +02002247 /* Check PEC if last message is a read */
2248 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002249 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2250 if (status < 0)
2251 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002252 }
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002255 switch (size) {
2256 case I2C_SMBUS_BYTE:
2257 data->byte = msgbuf0[0];
2258 break;
2259 case I2C_SMBUS_BYTE_DATA:
2260 data->byte = msgbuf1[0];
2261 break;
2262 case I2C_SMBUS_WORD_DATA:
2263 case I2C_SMBUS_PROC_CALL:
2264 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2265 break;
2266 case I2C_SMBUS_I2C_BLOCK_DATA:
2267 for (i = 0; i < data->block[0]; i++)
2268 data->block[i+1] = msgbuf1[i];
2269 break;
2270 case I2C_SMBUS_BLOCK_DATA:
2271 case I2C_SMBUS_BLOCK_PROC_CALL:
2272 for (i = 0; i < msgbuf1[0] + 1; i++)
2273 data->block[i] = msgbuf1[i];
2274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 }
2276 return 0;
2277}
2278
David Brownella1cdeda2008-07-14 22:38:24 +02002279/**
2280 * i2c_smbus_xfer - execute SMBus protocol operations
2281 * @adapter: Handle to I2C bus
2282 * @addr: Address of SMBus slave on that bus
2283 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2284 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2285 * @command: Byte interpreted by slave, for protocols which use such bytes
2286 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2287 * @data: Data to be read or written
2288 *
2289 * This executes an SMBus protocol operation, and returns a negative
2290 * errno code else zero on success.
2291 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002292s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002293 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002294 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002296 unsigned long orig_jiffies;
2297 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002300 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002303 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002304
2305 /* Retry automatically on arbitration loss */
2306 orig_jiffies = jiffies;
2307 for (res = 0, try = 0; try <= adapter->retries; try++) {
2308 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2309 read_write, command,
2310 protocol, data);
2311 if (res != -EAGAIN)
2312 break;
2313 if (time_after(jiffies,
2314 orig_jiffies + adapter->timeout))
2315 break;
2316 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002317 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002319 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2320 return res;
2321 /*
2322 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2323 * implement native support for the SMBus operation.
2324 */
2325 }
2326
2327 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2328 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2333MODULE_DESCRIPTION("I2C-Bus main module");
2334MODULE_LICENSE("GPL");