blob: 430c001b3f1e6e0bbca7b372b340241a9a9b558a [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
Wolfram Sang687b81d2013-07-11 12:56:15 +010026 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053036#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/i2c.h>
39#include <linux/init.h>
40#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010041#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060043#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010044#include <linux/of_irq.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jean Delvare6629dcf2010-05-04 11:09:28 +020056/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020057 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000058 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010059static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_IDR(i2c_adapter_idr);
61
Jean Delvare4f8cf822009-09-18 22:45:46 +020062static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020063static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010064
65/* ------------------------------------------------------------------------- */
66
Jean Delvared2653e92008-04-29 23:11:39 +020067static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
68 const struct i2c_client *client)
69{
70 while (id->name[0]) {
71 if (strcmp(client->name, id->name) == 0)
72 return id;
73 id++;
74 }
75 return NULL;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int i2c_device_match(struct device *dev, struct device_driver *drv)
79{
Jean Delvare51298d12009-09-18 22:45:45 +020080 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020082
Jean Delvare51298d12009-09-18 22:45:45 +020083 if (!client)
84 return 0;
85
Grant Likely959e85f2010-06-08 07:48:19 -060086 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
88 return 1;
89
Mika Westerberg907ddf82012-11-23 12:23:40 +010090 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
93
Jean Delvare51298d12009-09-18 22:45:45 +020094 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020095 /* match on an id table if there is one */
96 if (driver->id_table)
97 return i2c_match_id(driver->id_table, client) != NULL;
98
Jean Delvareeb8a7902008-05-18 20:49:41 +020099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
103/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105{
106 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200107
Jean Delvareeb8a7902008-05-18 20:49:41 +0200108 if (add_uevent_var(env, "MODALIAS=%s%s",
109 I2C_MODULE_PREFIX, client->name))
110 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200111 dev_dbg(dev, "uevent\n");
112 return 0;
113}
114
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530115/* i2c bus recovery routines */
116static int get_scl_gpio_value(struct i2c_adapter *adap)
117{
118 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
119}
120
121static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
122{
123 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
124}
125
126static int get_sda_gpio_value(struct i2c_adapter *adap)
127{
128 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
129}
130
131static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
132{
133 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
134 struct device *dev = &adap->dev;
135 int ret = 0;
136
137 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
138 GPIOF_OUT_INIT_HIGH, "i2c-scl");
139 if (ret) {
140 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
141 return ret;
142 }
143
144 if (bri->get_sda) {
145 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
146 /* work without SDA polling */
147 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
148 bri->sda_gpio);
149 bri->get_sda = NULL;
150 }
151 }
152
153 return ret;
154}
155
156static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
157{
158 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
159
160 if (bri->get_sda)
161 gpio_free(bri->sda_gpio);
162
163 gpio_free(bri->scl_gpio);
164}
165
166/*
167 * We are generating clock pulses. ndelay() determines durating of clk pulses.
168 * We will generate clock with rate 100 KHz and so duration of both clock levels
169 * is: delay in ns = (10^6 / 100) / 2
170 */
171#define RECOVERY_NDELAY 5000
172#define RECOVERY_CLK_CNT 9
173
174static int i2c_generic_recovery(struct i2c_adapter *adap)
175{
176 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
177 int i = 0, val = 1, ret = 0;
178
179 if (bri->prepare_recovery)
180 bri->prepare_recovery(bri);
181
182 /*
183 * By this time SCL is high, as we need to give 9 falling-rising edges
184 */
185 while (i++ < RECOVERY_CLK_CNT * 2) {
186 if (val) {
187 /* Break if SDA is high */
188 if (bri->get_sda && bri->get_sda(adap))
189 break;
190 /* SCL shouldn't be low here */
191 if (!bri->get_scl(adap)) {
192 dev_err(&adap->dev,
193 "SCL is stuck low, exit recovery\n");
194 ret = -EBUSY;
195 break;
196 }
197 }
198
199 val = !val;
200 bri->set_scl(adap, val);
201 ndelay(RECOVERY_NDELAY);
202 }
203
204 if (bri->unprepare_recovery)
205 bri->unprepare_recovery(bri);
206
207 return ret;
208}
209
210int i2c_generic_scl_recovery(struct i2c_adapter *adap)
211{
212 adap->bus_recovery_info->set_scl(adap, 1);
213 return i2c_generic_recovery(adap);
214}
215
216int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
217{
218 int ret;
219
220 ret = i2c_get_gpios_for_recovery(adap);
221 if (ret)
222 return ret;
223
224 ret = i2c_generic_recovery(adap);
225 i2c_put_gpios_for_recovery(adap);
226
227 return ret;
228}
229
230int i2c_recover_bus(struct i2c_adapter *adap)
231{
232 if (!adap->bus_recovery_info)
233 return -EOPNOTSUPP;
234
235 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
236 return adap->bus_recovery_info->recover_bus(adap);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int i2c_device_probe(struct device *dev)
240{
Jean Delvare51298d12009-09-18 22:45:45 +0200241 struct i2c_client *client = i2c_verify_client(dev);
242 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100243 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200244
Jean Delvare51298d12009-09-18 22:45:45 +0200245 if (!client)
246 return 0;
247
248 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200249 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200250 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200251
Marc Pignatee354252008-08-28 08:33:22 +0200252 if (!device_can_wakeup(&client->dev))
253 device_init_wakeup(&client->dev,
254 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200255 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200256
Jean Delvaree0457442008-07-14 22:38:30 +0200257 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200258 if (status)
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200259 i2c_set_clientdata(client, NULL);
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200260
Hans Verkuil50c33042008-03-12 14:15:00 +0100261 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264static int i2c_device_remove(struct device *dev)
265{
Jean Delvare51298d12009-09-18 22:45:45 +0200266 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200267 struct i2c_driver *driver;
268 int status;
269
Jean Delvare51298d12009-09-18 22:45:45 +0200270 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200271 return 0;
272
273 driver = to_i2c_driver(dev->driver);
274 if (driver->remove) {
275 dev_dbg(dev, "remove\n");
276 status = driver->remove(client);
277 } else {
278 dev->driver = NULL;
279 status = 0;
280 }
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200281 if (status == 0)
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200282 i2c_set_clientdata(client, NULL);
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200283
David Brownella1d9e6e2007-05-01 23:26:30 +0200284 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
David Brownellf37dd802007-02-13 22:09:00 +0100287static void i2c_device_shutdown(struct device *dev)
288{
Jean Delvare51298d12009-09-18 22:45:45 +0200289 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100290 struct i2c_driver *driver;
291
Jean Delvare51298d12009-09-18 22:45:45 +0200292 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100293 return;
294 driver = to_i2c_driver(dev->driver);
295 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200296 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100297}
298
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200299#ifdef CONFIG_PM_SLEEP
300static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100301{
Jean Delvare51298d12009-09-18 22:45:45 +0200302 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100303 struct i2c_driver *driver;
304
Jean Delvare51298d12009-09-18 22:45:45 +0200305 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100306 return 0;
307 driver = to_i2c_driver(dev->driver);
308 if (!driver->suspend)
309 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200310 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100311}
312
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200313static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100314{
Jean Delvare51298d12009-09-18 22:45:45 +0200315 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100316 struct i2c_driver *driver;
317
Jean Delvare51298d12009-09-18 22:45:45 +0200318 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100319 return 0;
320 driver = to_i2c_driver(dev->driver);
321 if (!driver->resume)
322 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200323 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100324}
325
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200326static int i2c_device_pm_suspend(struct device *dev)
327{
328 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
329
Mark Brownd529de22011-01-14 22:03:49 +0100330 if (pm)
331 return pm_generic_suspend(dev);
332 else
333 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200334}
335
336static int i2c_device_pm_resume(struct device *dev)
337{
338 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200339
340 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100341 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200342 else
Mark Brownd529de22011-01-14 22:03:49 +0100343 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200344}
345
346static int i2c_device_pm_freeze(struct device *dev)
347{
348 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
349
Mark Brownd529de22011-01-14 22:03:49 +0100350 if (pm)
351 return pm_generic_freeze(dev);
352 else
353 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200354}
355
356static int i2c_device_pm_thaw(struct device *dev)
357{
358 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
359
Mark Brownd529de22011-01-14 22:03:49 +0100360 if (pm)
361 return pm_generic_thaw(dev);
362 else
363 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200364}
365
366static int i2c_device_pm_poweroff(struct device *dev)
367{
368 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
369
Mark Brownd529de22011-01-14 22:03:49 +0100370 if (pm)
371 return pm_generic_poweroff(dev);
372 else
373 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200374}
375
376static int i2c_device_pm_restore(struct device *dev)
377{
378 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200379
380 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100381 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200382 else
Mark Brownd529de22011-01-14 22:03:49 +0100383 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200384}
385#else /* !CONFIG_PM_SLEEP */
386#define i2c_device_pm_suspend NULL
387#define i2c_device_pm_resume NULL
388#define i2c_device_pm_freeze NULL
389#define i2c_device_pm_thaw NULL
390#define i2c_device_pm_poweroff NULL
391#define i2c_device_pm_restore NULL
392#endif /* !CONFIG_PM_SLEEP */
393
David Brownell9c1600e2007-05-01 23:26:31 +0200394static void i2c_client_dev_release(struct device *dev)
395{
396 kfree(to_i2c_client(dev));
397}
398
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100399static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200400show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200401{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200402 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
403 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200404}
405
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100406static ssize_t
407show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200408{
409 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200410 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200411}
412
Jean Delvare4f8cf822009-09-18 22:45:46 +0200413static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200414static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
415
416static struct attribute *i2c_dev_attrs[] = {
417 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200418 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200419 &dev_attr_modalias.attr,
420 NULL
421};
422
423static struct attribute_group i2c_dev_attr_group = {
424 .attrs = i2c_dev_attrs,
425};
426
427static const struct attribute_group *i2c_dev_attr_groups[] = {
428 &i2c_dev_attr_group,
429 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200430};
431
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100432static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100433 .suspend = i2c_device_pm_suspend,
434 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200435 .freeze = i2c_device_pm_freeze,
436 .thaw = i2c_device_pm_thaw,
437 .poweroff = i2c_device_pm_poweroff,
438 .restore = i2c_device_pm_restore,
439 SET_RUNTIME_PM_OPS(
440 pm_generic_runtime_suspend,
441 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200442 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200443 )
sonic zhang54067ee2009-12-14 21:17:30 +0100444};
445
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200446struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100447 .name = "i2c",
448 .match = i2c_device_match,
449 .probe = i2c_device_probe,
450 .remove = i2c_device_remove,
451 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100452 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000453};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200454EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000455
Jean Delvare51298d12009-09-18 22:45:45 +0200456static struct device_type i2c_client_type = {
457 .groups = i2c_dev_attr_groups,
458 .uevent = i2c_device_uevent,
459 .release = i2c_client_dev_release,
460};
461
David Brownell9b766b82008-01-27 18:14:51 +0100462
463/**
464 * i2c_verify_client - return parameter as i2c_client, or NULL
465 * @dev: device, probably from some driver model iterator
466 *
467 * When traversing the driver model tree, perhaps using driver model
468 * iterators like @device_for_each_child(), you can't assume very much
469 * about the nodes you find. Use this function to avoid oopses caused
470 * by wrongly treating some non-I2C device as an i2c_client.
471 */
472struct i2c_client *i2c_verify_client(struct device *dev)
473{
Jean Delvare51298d12009-09-18 22:45:45 +0200474 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100475 ? to_i2c_client(dev)
476 : NULL;
477}
478EXPORT_SYMBOL(i2c_verify_client);
479
480
Jean Delvare3a89db52010-06-03 11:33:52 +0200481/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300482 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200483static int i2c_check_client_addr_validity(const struct i2c_client *client)
484{
485 if (client->flags & I2C_CLIENT_TEN) {
486 /* 10-bit address, all values are valid */
487 if (client->addr > 0x3ff)
488 return -EINVAL;
489 } else {
490 /* 7-bit address, reject the general call address */
491 if (client->addr == 0x00 || client->addr > 0x7f)
492 return -EINVAL;
493 }
494 return 0;
495}
496
Jean Delvare656b8762010-06-03 11:33:53 +0200497/* And this is a strict address validity check, used when probing. If a
498 * device uses a reserved address, then it shouldn't be probed. 7-bit
499 * addressing is assumed, 10-bit address devices are rare and should be
500 * explicitly enumerated. */
501static int i2c_check_addr_validity(unsigned short addr)
502{
503 /*
504 * Reserved addresses per I2C specification:
505 * 0x00 General call address / START byte
506 * 0x01 CBUS address
507 * 0x02 Reserved for different bus format
508 * 0x03 Reserved for future purposes
509 * 0x04-0x07 Hs-mode master code
510 * 0x78-0x7b 10-bit slave addressing
511 * 0x7c-0x7f Reserved for future purposes
512 */
513 if (addr < 0x08 || addr > 0x77)
514 return -EINVAL;
515 return 0;
516}
517
Jean Delvare3b5f7942010-06-03 11:33:55 +0200518static int __i2c_check_addr_busy(struct device *dev, void *addrp)
519{
520 struct i2c_client *client = i2c_verify_client(dev);
521 int addr = *(int *)addrp;
522
523 if (client && client->addr == addr)
524 return -EBUSY;
525 return 0;
526}
527
Michael Lawnick08263742010-08-11 18:21:02 +0200528/* walk up mux tree */
529static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
530{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200531 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200532 int result;
533
534 result = device_for_each_child(&adapter->dev, &addr,
535 __i2c_check_addr_busy);
536
Jean Delvare97cc4d42010-10-24 18:16:57 +0200537 if (!result && parent)
538 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200539
540 return result;
541}
542
543/* recurse down mux tree */
544static int i2c_check_mux_children(struct device *dev, void *addrp)
545{
546 int result;
547
548 if (dev->type == &i2c_adapter_type)
549 result = device_for_each_child(dev, addrp,
550 i2c_check_mux_children);
551 else
552 result = __i2c_check_addr_busy(dev, addrp);
553
554 return result;
555}
556
Jean Delvare3b5f7942010-06-03 11:33:55 +0200557static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
558{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200559 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200560 int result = 0;
561
Jean Delvare97cc4d42010-10-24 18:16:57 +0200562 if (parent)
563 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200564
565 if (!result)
566 result = device_for_each_child(&adapter->dev, &addr,
567 i2c_check_mux_children);
568
569 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200570}
571
David Brownell9c1600e2007-05-01 23:26:31 +0200572/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200573 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
574 * @adapter: Target I2C bus segment
575 */
576void i2c_lock_adapter(struct i2c_adapter *adapter)
577{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200578 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
579
580 if (parent)
581 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200582 else
583 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200584}
585EXPORT_SYMBOL_GPL(i2c_lock_adapter);
586
587/**
588 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
589 * @adapter: Target I2C bus segment
590 */
591static int i2c_trylock_adapter(struct i2c_adapter *adapter)
592{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200593 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
594
595 if (parent)
596 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200597 else
598 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200599}
600
601/**
602 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
603 * @adapter: Target I2C bus segment
604 */
605void i2c_unlock_adapter(struct i2c_adapter *adapter)
606{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200607 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
608
609 if (parent)
610 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200611 else
612 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200613}
614EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
615
616/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200617 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200618 * @adap: the adapter managing the device
619 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200620 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200621 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200622 * Create an i2c device. Binding is handled through driver model
623 * probe()/remove() methods. A driver may be bound to this device when we
624 * return from this function, or any later moment (e.g. maybe hotplugging will
625 * load the driver module). This call is not appropriate for use by mainboard
626 * initialization logic, which usually runs during an arch_initcall() long
627 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200628 *
629 * This returns the new i2c client, which may be saved for later use with
630 * i2c_unregister_device(); or NULL to indicate an error.
631 */
632struct i2c_client *
633i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
634{
635 struct i2c_client *client;
636 int status;
637
638 client = kzalloc(sizeof *client, GFP_KERNEL);
639 if (!client)
640 return NULL;
641
642 client->adapter = adap;
643
644 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200645
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200646 if (info->archdata)
647 client->dev.archdata = *info->archdata;
648
Marc Pignatee354252008-08-28 08:33:22 +0200649 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200650 client->addr = info->addr;
651 client->irq = info->irq;
652
David Brownell9c1600e2007-05-01 23:26:31 +0200653 strlcpy(client->name, info->type, sizeof(client->name));
654
Jean Delvare3a89db52010-06-03 11:33:52 +0200655 /* Check for address validity */
656 status = i2c_check_client_addr_validity(client);
657 if (status) {
658 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
659 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
660 goto out_err_silent;
661 }
662
Jean Delvaref8a227e2009-06-19 16:58:18 +0200663 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200664 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200665 if (status)
666 goto out_err;
667
668 client->dev.parent = &client->adapter->dev;
669 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200670 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700671 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100672 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200673
Jean Delvarecbb44512011-11-23 11:33:07 +0100674 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200675 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100676 client->addr | ((client->flags & I2C_CLIENT_TEN)
677 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200678 status = device_register(&client->dev);
679 if (status)
680 goto out_err;
681
Jean Delvaref8a227e2009-06-19 16:58:18 +0200682 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
683 client->name, dev_name(&client->dev));
684
David Brownell9c1600e2007-05-01 23:26:31 +0200685 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200686
687out_err:
688 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
689 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200690out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200691 kfree(client);
692 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200693}
694EXPORT_SYMBOL_GPL(i2c_new_device);
695
696
697/**
698 * i2c_unregister_device - reverse effect of i2c_new_device()
699 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200700 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200701 */
702void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200703{
David Brownella1d9e6e2007-05-01 23:26:30 +0200704 device_unregister(&client->dev);
705}
David Brownell9c1600e2007-05-01 23:26:31 +0200706EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200707
708
Jean Delvare60b129d2008-05-11 20:37:06 +0200709static const struct i2c_device_id dummy_id[] = {
710 { "dummy", 0 },
711 { },
712};
713
Jean Delvared2653e92008-04-29 23:11:39 +0200714static int dummy_probe(struct i2c_client *client,
715 const struct i2c_device_id *id)
716{
717 return 0;
718}
719
720static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100721{
722 return 0;
723}
724
725static struct i2c_driver dummy_driver = {
726 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200727 .probe = dummy_probe,
728 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200729 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100730};
731
732/**
733 * i2c_new_dummy - return a new i2c device bound to a dummy driver
734 * @adapter: the adapter managing the device
735 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100736 * Context: can sleep
737 *
738 * This returns an I2C client bound to the "dummy" driver, intended for use
739 * with devices that consume multiple addresses. Examples of such chips
740 * include various EEPROMS (like 24c04 and 24c08 models).
741 *
742 * These dummy devices have two main uses. First, most I2C and SMBus calls
743 * except i2c_transfer() need a client handle; the dummy will be that handle.
744 * And second, this prevents the specified address from being bound to a
745 * different driver.
746 *
747 * This returns the new i2c client, which should be saved for later use with
748 * i2c_unregister_device(); or NULL to indicate an error.
749 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100750struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100751{
752 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200753 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100754 };
755
David Brownelle9f13732008-01-27 18:14:52 +0100756 return i2c_new_device(adapter, &info);
757}
758EXPORT_SYMBOL_GPL(i2c_new_dummy);
759
David Brownellf37dd802007-02-13 22:09:00 +0100760/* ------------------------------------------------------------------------- */
761
David Brownell16ffadf2007-05-01 23:26:28 +0200762/* I2C bus adapters -- one roots each I2C or SMBUS segment */
763
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200764static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
David Brownellef2c83212007-05-01 23:26:28 +0200766 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 complete(&adap->dev_released);
768}
769
Jean Delvare99cd8e22009-06-19 16:58:20 +0200770/*
Jean Delvare390946b2012-09-10 10:14:02 +0200771 * This function is only needed for mutex_lock_nested, so it is never
772 * called unless locking correctness checking is enabled. Thus we
773 * make it inline to avoid a compiler warning. That's what gcc ends up
774 * doing anyway.
775 */
776static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
777{
778 unsigned int depth = 0;
779
780 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
781 depth++;
782
783 return depth;
784}
785
786/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200787 * Let users instantiate I2C devices through sysfs. This can be used when
788 * platform initialization code doesn't contain the proper data for
789 * whatever reason. Also useful for drivers that do device detection and
790 * detection fails, either because the device uses an unexpected address,
791 * or this is a compatible device with different ID register values.
792 *
793 * Parameter checking may look overzealous, but we really don't want
794 * the user to provide incorrect parameters.
795 */
796static ssize_t
797i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
798 const char *buf, size_t count)
799{
800 struct i2c_adapter *adap = to_i2c_adapter(dev);
801 struct i2c_board_info info;
802 struct i2c_client *client;
803 char *blank, end;
804 int res;
805
Jean Delvare99cd8e22009-06-19 16:58:20 +0200806 memset(&info, 0, sizeof(struct i2c_board_info));
807
808 blank = strchr(buf, ' ');
809 if (!blank) {
810 dev_err(dev, "%s: Missing parameters\n", "new_device");
811 return -EINVAL;
812 }
813 if (blank - buf > I2C_NAME_SIZE - 1) {
814 dev_err(dev, "%s: Invalid device name\n", "new_device");
815 return -EINVAL;
816 }
817 memcpy(info.type, buf, blank - buf);
818
819 /* Parse remaining parameters, reject extra parameters */
820 res = sscanf(++blank, "%hi%c", &info.addr, &end);
821 if (res < 1) {
822 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
823 return -EINVAL;
824 }
825 if (res > 1 && end != '\n') {
826 dev_err(dev, "%s: Extra parameters\n", "new_device");
827 return -EINVAL;
828 }
829
Jean Delvare99cd8e22009-06-19 16:58:20 +0200830 client = i2c_new_device(adap, &info);
831 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200832 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200833
834 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200835 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200836 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200837 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200838 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
839 info.type, info.addr);
840
841 return count;
842}
843
844/*
845 * And of course let the users delete the devices they instantiated, if
846 * they got it wrong. This interface can only be used to delete devices
847 * instantiated by i2c_sysfs_new_device above. This guarantees that we
848 * don't delete devices to which some kernel code still has references.
849 *
850 * Parameter checking may look overzealous, but we really don't want
851 * the user to delete the wrong device.
852 */
853static ssize_t
854i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
855 const char *buf, size_t count)
856{
857 struct i2c_adapter *adap = to_i2c_adapter(dev);
858 struct i2c_client *client, *next;
859 unsigned short addr;
860 char end;
861 int res;
862
863 /* Parse parameters, reject extra parameters */
864 res = sscanf(buf, "%hi%c", &addr, &end);
865 if (res < 1) {
866 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
867 return -EINVAL;
868 }
869 if (res > 1 && end != '\n') {
870 dev_err(dev, "%s: Extra parameters\n", "delete_device");
871 return -EINVAL;
872 }
873
874 /* Make sure the device was added through sysfs */
875 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200876 mutex_lock_nested(&adap->userspace_clients_lock,
877 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200878 list_for_each_entry_safe(client, next, &adap->userspace_clients,
879 detected) {
880 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200881 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
882 "delete_device", client->name, client->addr);
883
884 list_del(&client->detected);
885 i2c_unregister_device(client);
886 res = count;
887 break;
888 }
889 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200890 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200891
892 if (res < 0)
893 dev_err(dev, "%s: Can't find device in list\n",
894 "delete_device");
895 return res;
896}
897
Jean Delvare4f8cf822009-09-18 22:45:46 +0200898static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200899static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
900 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200901
902static struct attribute *i2c_adapter_attrs[] = {
903 &dev_attr_name.attr,
904 &dev_attr_new_device.attr,
905 &dev_attr_delete_device.attr,
906 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200907};
908
Jean Delvare4f8cf822009-09-18 22:45:46 +0200909static struct attribute_group i2c_adapter_attr_group = {
910 .attrs = i2c_adapter_attrs,
911};
912
913static const struct attribute_group *i2c_adapter_attr_groups[] = {
914 &i2c_adapter_attr_group,
915 NULL
916};
917
Michael Lawnick08263742010-08-11 18:21:02 +0200918struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200919 .groups = i2c_adapter_attr_groups,
920 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200921};
Michael Lawnick08263742010-08-11 18:21:02 +0200922EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Stephen Warren643dd092012-04-17 12:43:33 -0600924/**
925 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
926 * @dev: device, probably from some driver model iterator
927 *
928 * When traversing the driver model tree, perhaps using driver model
929 * iterators like @device_for_each_child(), you can't assume very much
930 * about the nodes you find. Use this function to avoid oopses caused
931 * by wrongly treating some non-I2C device as an i2c_adapter.
932 */
933struct i2c_adapter *i2c_verify_adapter(struct device *dev)
934{
935 return (dev->type == &i2c_adapter_type)
936 ? to_i2c_adapter(dev)
937 : NULL;
938}
939EXPORT_SYMBOL(i2c_verify_adapter);
940
Jean Delvare2bb50952009-09-18 22:45:46 +0200941#ifdef CONFIG_I2C_COMPAT
942static struct class_compat *i2c_adapter_compat_class;
943#endif
944
David Brownell9c1600e2007-05-01 23:26:31 +0200945static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
946{
947 struct i2c_devinfo *devinfo;
948
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200949 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200950 list_for_each_entry(devinfo, &__i2c_board_list, list) {
951 if (devinfo->busnum == adapter->nr
952 && !i2c_new_device(adapter,
953 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100954 dev_err(&adapter->dev,
955 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200956 devinfo->board_info.addr);
957 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200958 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200959}
960
Wolfram Sang687b81d2013-07-11 12:56:15 +0100961/* OF support code */
962
963#if IS_ENABLED(CONFIG_OF)
964static void of_i2c_register_devices(struct i2c_adapter *adap)
965{
966 void *result;
967 struct device_node *node;
968
969 /* Only register child devices if the adapter has a node pointer set */
970 if (!adap->dev.of_node)
971 return;
972
973 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
974
975 for_each_available_child_of_node(adap->dev.of_node, node) {
976 struct i2c_board_info info = {};
977 struct dev_archdata dev_ad = {};
978 const __be32 *addr;
979 int len;
980
981 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
982
983 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
984 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
985 node->full_name);
986 continue;
987 }
988
989 addr = of_get_property(node, "reg", &len);
990 if (!addr || (len < sizeof(int))) {
991 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
992 node->full_name);
993 continue;
994 }
995
996 info.addr = be32_to_cpup(addr);
997 if (info.addr > (1 << 10) - 1) {
998 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
999 info.addr, node->full_name);
1000 continue;
1001 }
1002
1003 info.irq = irq_of_parse_and_map(node, 0);
1004 info.of_node = of_node_get(node);
1005 info.archdata = &dev_ad;
1006
1007 if (of_get_property(node, "wakeup-source", NULL))
1008 info.flags |= I2C_CLIENT_WAKE;
1009
1010 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1011
1012 result = i2c_new_device(adap, &info);
1013 if (result == NULL) {
1014 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1015 node->full_name);
1016 of_node_put(node);
1017 irq_dispose_mapping(info.irq);
1018 continue;
1019 }
1020 }
1021}
1022
1023static int of_dev_node_match(struct device *dev, void *data)
1024{
1025 return dev->of_node == data;
1026}
1027
1028/* must call put_device() when done with returned i2c_client device */
1029struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1030{
1031 struct device *dev;
1032
1033 dev = bus_find_device(&i2c_bus_type, NULL, node,
1034 of_dev_node_match);
1035 if (!dev)
1036 return NULL;
1037
1038 return i2c_verify_client(dev);
1039}
1040EXPORT_SYMBOL(of_find_i2c_device_by_node);
1041
1042/* must call put_device() when done with returned i2c_adapter device */
1043struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1044{
1045 struct device *dev;
1046
1047 dev = bus_find_device(&i2c_bus_type, NULL, node,
1048 of_dev_node_match);
1049 if (!dev)
1050 return NULL;
1051
1052 return i2c_verify_adapter(dev);
1053}
1054EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1055#else
1056static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1057#endif /* CONFIG_OF */
1058
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001059/* ACPI support code */
1060
1061#if IS_ENABLED(CONFIG_ACPI)
1062static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1063{
1064 struct i2c_board_info *info = data;
1065
1066 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1067 struct acpi_resource_i2c_serialbus *sb;
1068
1069 sb = &ares->data.i2c_serial_bus;
1070 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1071 info->addr = sb->slave_address;
1072 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1073 info->flags |= I2C_CLIENT_TEN;
1074 }
1075 } else if (info->irq < 0) {
1076 struct resource r;
1077
1078 if (acpi_dev_resource_interrupt(ares, 0, &r))
1079 info->irq = r.start;
1080 }
1081
1082 /* Tell the ACPI core to skip this resource */
1083 return 1;
1084}
1085
1086static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1087 void *data, void **return_value)
1088{
1089 struct i2c_adapter *adapter = data;
1090 struct list_head resource_list;
1091 struct i2c_board_info info;
1092 struct acpi_device *adev;
1093 int ret;
1094
1095 if (acpi_bus_get_device(handle, &adev))
1096 return AE_OK;
1097 if (acpi_bus_get_status(adev) || !adev->status.present)
1098 return AE_OK;
1099
1100 memset(&info, 0, sizeof(info));
1101 info.acpi_node.handle = handle;
1102 info.irq = -1;
1103
1104 INIT_LIST_HEAD(&resource_list);
1105 ret = acpi_dev_get_resources(adev, &resource_list,
1106 acpi_i2c_add_resource, &info);
1107 acpi_dev_free_resource_list(&resource_list);
1108
1109 if (ret < 0 || !info.addr)
1110 return AE_OK;
1111
1112 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1113 if (!i2c_new_device(adapter, &info)) {
1114 dev_err(&adapter->dev,
1115 "failed to add I2C device %s from ACPI\n",
1116 dev_name(&adev->dev));
1117 }
1118
1119 return AE_OK;
1120}
1121
1122/**
1123 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1124 * @adap: pointer to adapter
1125 *
1126 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1127 * namespace. When a device is found it will be added to the Linux device
1128 * model and bound to the corresponding ACPI handle.
1129 */
1130static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1131{
1132 acpi_handle handle;
1133 acpi_status status;
1134
1135 handle = ACPI_HANDLE(adap->dev.parent);
1136 if (!handle)
1137 return;
1138
1139 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1140 acpi_i2c_add_device, NULL,
1141 adap, NULL);
1142 if (ACPI_FAILURE(status))
1143 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1144}
1145#else
1146static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1147#endif /* CONFIG_ACPI */
1148
Jean Delvare69b00892009-12-06 17:06:27 +01001149static int i2c_do_add_adapter(struct i2c_driver *driver,
1150 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001151{
Jean Delvare4735c982008-07-14 22:38:36 +02001152 /* Detect supported devices on that bus, and instantiate them */
1153 i2c_detect(adap, driver);
1154
1155 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001156 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001157 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1158 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001159 dev_warn(&adap->dev, "Please use another way to instantiate "
1160 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001161 /* We ignore the return code; if it fails, too bad */
1162 driver->attach_adapter(adap);
1163 }
1164 return 0;
1165}
1166
Jean Delvare69b00892009-12-06 17:06:27 +01001167static int __process_new_adapter(struct device_driver *d, void *data)
1168{
1169 return i2c_do_add_adapter(to_i2c_driver(d), data);
1170}
1171
David Brownell6e13e642007-05-01 23:26:31 +02001172static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Jean Delvared6703282010-08-11 18:20:59 +02001174 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
David Brownell1d0b19c2008-10-14 17:30:05 +02001176 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001177 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1178 res = -EAGAIN;
1179 goto out_list;
1180 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001181
Jean Delvare2236baa2010-11-15 22:40:38 +01001182 /* Sanity checks */
1183 if (unlikely(adap->name[0] == '\0')) {
1184 pr_err("i2c-core: Attempt to register an adapter with "
1185 "no name!\n");
1186 return -EINVAL;
1187 }
1188 if (unlikely(!adap->algo)) {
1189 pr_err("i2c-core: Attempt to register adapter '%s' with "
1190 "no algo!\n", adap->name);
1191 return -EINVAL;
1192 }
1193
Mika Kuoppala194684e2009-12-06 17:06:22 +01001194 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001195 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001196 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Jean Delvare8fcfef62009-03-28 21:34:43 +01001198 /* Set default timeout to 1 second if not already set */
1199 if (adap->timeout == 0)
1200 adap->timeout = HZ;
1201
Kay Sievers27d9c182009-01-07 14:29:16 +01001202 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001203 adap->dev.bus = &i2c_bus_type;
1204 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001205 res = device_register(&adap->dev);
1206 if (res)
1207 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001209 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1210
Jean Delvare2bb50952009-09-18 22:45:46 +02001211#ifdef CONFIG_I2C_COMPAT
1212 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1213 adap->dev.parent);
1214 if (res)
1215 dev_warn(&adap->dev,
1216 "Failed to create compatibility class link\n");
1217#endif
1218
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301219 /* bus recovery specific initialization */
1220 if (adap->bus_recovery_info) {
1221 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1222
1223 if (!bri->recover_bus) {
1224 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1225 adap->bus_recovery_info = NULL;
1226 goto exit_recovery;
1227 }
1228
1229 /* Generic GPIO recovery */
1230 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1231 if (!gpio_is_valid(bri->scl_gpio)) {
1232 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1233 adap->bus_recovery_info = NULL;
1234 goto exit_recovery;
1235 }
1236
1237 if (gpio_is_valid(bri->sda_gpio))
1238 bri->get_sda = get_sda_gpio_value;
1239 else
1240 bri->get_sda = NULL;
1241
1242 bri->get_scl = get_scl_gpio_value;
1243 bri->set_scl = set_scl_gpio_value;
1244 } else if (!bri->set_scl || !bri->get_scl) {
1245 /* Generic SCL recovery */
1246 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1247 adap->bus_recovery_info = NULL;
1248 }
1249 }
1250
1251exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001252 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001253 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001254 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001255
David Brownell6e13e642007-05-01 23:26:31 +02001256 if (adap->nr < __i2c_first_dynamic_bus_num)
1257 i2c_scan_static_board_info(adap);
1258
Jean Delvare4735c982008-07-14 22:38:36 +02001259 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001260 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001261 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001262 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001263
1264 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001265
Jean Delvareb119c6c2006-08-15 18:26:30 +02001266out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001267 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001268 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001269 mutex_unlock(&core_lock);
1270 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
David Brownell6e13e642007-05-01 23:26:31 +02001273/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001274 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1275 * @adap: the adapter to register (with adap->nr initialized)
1276 * Context: can sleep
1277 *
1278 * See i2c_add_numbered_adapter() for details.
1279 */
1280static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1281{
1282 int id;
1283
1284 mutex_lock(&core_lock);
1285 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1286 GFP_KERNEL);
1287 mutex_unlock(&core_lock);
1288 if (id < 0)
1289 return id == -ENOSPC ? -EBUSY : id;
1290
1291 return i2c_register_adapter(adap);
1292}
1293
1294/**
David Brownell6e13e642007-05-01 23:26:31 +02001295 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1296 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001297 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001298 *
1299 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001300 * doesn't matter or when its bus number is specified by an dt alias.
1301 * Examples of bases when the bus number doesn't matter: I2C adapters
1302 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001303 *
1304 * When this returns zero, a new bus number was allocated and stored
1305 * in adap->nr, and the specified adapter became available for clients.
1306 * Otherwise, a negative errno value is returned.
1307 */
1308int i2c_add_adapter(struct i2c_adapter *adapter)
1309{
Doug Andersonee5c2742013-03-01 08:57:31 -08001310 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001311 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001312
Doug Andersonee5c2742013-03-01 08:57:31 -08001313 if (dev->of_node) {
1314 id = of_alias_get_id(dev->of_node, "i2c");
1315 if (id >= 0) {
1316 adapter->nr = id;
1317 return __i2c_add_numbered_adapter(adapter);
1318 }
1319 }
1320
Jean Delvarecaada322008-01-27 18:14:49 +01001321 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001322 id = idr_alloc(&i2c_adapter_idr, adapter,
1323 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001324 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001325 if (id < 0)
1326 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001327
1328 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001329
David Brownell6e13e642007-05-01 23:26:31 +02001330 return i2c_register_adapter(adapter);
1331}
1332EXPORT_SYMBOL(i2c_add_adapter);
1333
1334/**
1335 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1336 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001337 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001338 *
1339 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001340 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1341 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001342 * is used to properly configure I2C devices.
1343 *
Grant Likely488bf312011-07-25 17:49:43 +02001344 * If the requested bus number is set to -1, then this function will behave
1345 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1346 *
David Brownell6e13e642007-05-01 23:26:31 +02001347 * If no devices have pre-been declared for this bus, then be sure to
1348 * register the adapter before any dynamically allocated ones. Otherwise
1349 * the required bus ID may not be available.
1350 *
1351 * When this returns zero, the specified adapter became available for
1352 * clients using the bus number provided in adap->nr. Also, the table
1353 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1354 * and the appropriate driver model device nodes are created. Otherwise, a
1355 * negative errno value is returned.
1356 */
1357int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1358{
Grant Likely488bf312011-07-25 17:49:43 +02001359 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1360 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001361
Doug Andersonee5c2742013-03-01 08:57:31 -08001362 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001363}
1364EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1365
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001366static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001367 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001368{
Jean Delvare4735c982008-07-14 22:38:36 +02001369 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001370
Jean Delvareacec2112009-03-28 21:34:40 +01001371 /* Remove the devices we created ourselves as the result of hardware
1372 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001373 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1374 if (client->adapter == adapter) {
1375 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1376 client->name, client->addr);
1377 list_del(&client->detected);
1378 i2c_unregister_device(client);
1379 }
1380 }
Jean Delvare026526f2008-01-27 18:14:49 +01001381}
1382
Jean Delvaree549c2b2009-06-19 16:58:19 +02001383static int __unregister_client(struct device *dev, void *dummy)
1384{
1385 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001386 if (client && strcmp(client->name, "dummy"))
1387 i2c_unregister_device(client);
1388 return 0;
1389}
1390
1391static int __unregister_dummy(struct device *dev, void *dummy)
1392{
1393 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001394 if (client)
1395 i2c_unregister_device(client);
1396 return 0;
1397}
1398
Jean Delvare69b00892009-12-06 17:06:27 +01001399static int __process_removed_adapter(struct device_driver *d, void *data)
1400{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001401 i2c_do_del_adapter(to_i2c_driver(d), data);
1402 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001403}
1404
David Brownelld64f73b2007-07-12 14:12:28 +02001405/**
1406 * i2c_del_adapter - unregister I2C adapter
1407 * @adap: the adapter being unregistered
1408 * Context: can sleep
1409 *
1410 * This unregisters an I2C adapter which was previously registered
1411 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1412 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001413void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001415 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001416 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001419 mutex_lock(&core_lock);
1420 found = idr_find(&i2c_adapter_idr, adap->nr);
1421 mutex_unlock(&core_lock);
1422 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001423 pr_debug("i2c-core: attempting to delete unregistered "
1424 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001425 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Jean Delvare026526f2008-01-27 18:14:49 +01001428 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001429 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001430 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001431 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001432 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001434 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001435 mutex_lock_nested(&adap->userspace_clients_lock,
1436 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001437 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1438 detected) {
1439 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1440 client->addr);
1441 list_del(&client->detected);
1442 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001443 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001444 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001445
Jean Delvaree549c2b2009-06-19 16:58:19 +02001446 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001447 * check the returned value. This is a two-pass process, because
1448 * we can't remove the dummy devices during the first pass: they
1449 * could have been instantiated by real devices wishing to clean
1450 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001451 device_for_each_child(&adap->dev, NULL, __unregister_client);
1452 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jean Delvare2bb50952009-09-18 22:45:46 +02001454#ifdef CONFIG_I2C_COMPAT
1455 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1456 adap->dev.parent);
1457#endif
1458
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001459 /* device name is gone after device_unregister */
1460 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* clean up the sysfs representation */
1463 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 /* wait for sysfs to drop all references */
1467 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
David Brownell6e13e642007-05-01 23:26:31 +02001469 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001470 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001472 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001474 /* Clear the device structure in case this adapter is ever going to be
1475 added again */
1476 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
David Brownellc0564602007-05-01 23:26:31 +02001478EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
David Brownell7b4fbc52007-05-01 23:26:30 +02001480/* ------------------------------------------------------------------------- */
1481
Jean Delvare7ae31482011-03-20 14:50:52 +01001482int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1483{
1484 int res;
1485
1486 mutex_lock(&core_lock);
1487 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1488 mutex_unlock(&core_lock);
1489
1490 return res;
1491}
1492EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1493
Jean Delvare69b00892009-12-06 17:06:27 +01001494static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001495{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001496 if (dev->type != &i2c_adapter_type)
1497 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001498 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001499}
1500
David Brownell7b4fbc52007-05-01 23:26:30 +02001501/*
1502 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001503 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
1505
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001506int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001508 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
David Brownell1d0b19c2008-10-14 17:30:05 +02001510 /* Can't register until after driver model init */
1511 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1512 return -EAGAIN;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001515 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Jean Delvare729d6dd2009-06-19 16:58:18 +02001518 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001519 * will have called probe() for all matching-but-unbound devices.
1520 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 res = driver_register(&driver->driver);
1522 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001523 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001524
Mark Brownf4e8db32011-01-14 22:03:50 +01001525 /* Drivers should switch to dev_pm_ops instead. */
1526 if (driver->suspend)
1527 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1528 driver->driver.name);
1529 if (driver->resume)
1530 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1531 driver->driver.name);
1532
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001533 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Jean Delvare4735c982008-07-14 22:38:36 +02001535 INIT_LIST_HEAD(&driver->clients);
1536 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001537 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001538
Jean Delvare7eebcb72006-02-05 23:28:21 +01001539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001541EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Jean Delvare69b00892009-12-06 17:06:27 +01001543static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001544{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001545 if (dev->type == &i2c_adapter_type)
1546 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1547 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001548}
1549
David Brownella1d9e6e2007-05-01 23:26:30 +02001550/**
1551 * i2c_del_driver - unregister I2C driver
1552 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001553 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001554 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001555void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
Jean Delvare7ae31482011-03-20 14:50:52 +01001557 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001560 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
David Brownellc0564602007-05-01 23:26:31 +02001562EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Brownell7b4fbc52007-05-01 23:26:30 +02001564/* ------------------------------------------------------------------------- */
1565
Jean Delvaree48d3312008-01-27 18:14:48 +01001566/**
1567 * i2c_use_client - increments the reference count of the i2c client structure
1568 * @client: the client being referenced
1569 *
1570 * Each live reference to a client should be refcounted. The driver model does
1571 * that automatically as part of driver binding, so that most drivers don't
1572 * need to do this explicitly: they hold a reference until they're unbound
1573 * from the device.
1574 *
1575 * A pointer to the client with the incremented reference counter is returned.
1576 */
1577struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
David Brownell6ea438e2008-07-14 22:38:24 +02001579 if (client && get_device(&client->dev))
1580 return client;
1581 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
David Brownellc0564602007-05-01 23:26:31 +02001583EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Jean Delvaree48d3312008-01-27 18:14:48 +01001585/**
1586 * i2c_release_client - release a use of the i2c client structure
1587 * @client: the client being no longer referenced
1588 *
1589 * Must be called when a user of a client is finished with it.
1590 */
1591void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
David Brownell6ea438e2008-07-14 22:38:24 +02001593 if (client)
1594 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
David Brownellc0564602007-05-01 23:26:31 +02001596EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
David Brownell9b766b82008-01-27 18:14:51 +01001598struct i2c_cmd_arg {
1599 unsigned cmd;
1600 void *arg;
1601};
1602
1603static int i2c_cmd(struct device *dev, void *_arg)
1604{
1605 struct i2c_client *client = i2c_verify_client(dev);
1606 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001607 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001608
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001609 if (!client || !client->dev.driver)
1610 return 0;
1611
1612 driver = to_i2c_driver(client->dev.driver);
1613 if (driver->command)
1614 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001615 return 0;
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1619{
David Brownell9b766b82008-01-27 18:14:51 +01001620 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
David Brownell9b766b82008-01-27 18:14:51 +01001622 cmd_arg.cmd = cmd;
1623 cmd_arg.arg = arg;
1624 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
David Brownellc0564602007-05-01 23:26:31 +02001626EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628static int __init i2c_init(void)
1629{
1630 int retval;
1631
1632 retval = bus_register(&i2c_bus_type);
1633 if (retval)
1634 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001635#ifdef CONFIG_I2C_COMPAT
1636 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1637 if (!i2c_adapter_compat_class) {
1638 retval = -ENOMEM;
1639 goto bus_err;
1640 }
1641#endif
David Brownelle9f13732008-01-27 18:14:52 +01001642 retval = i2c_add_driver(&dummy_driver);
1643 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001644 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001645 return 0;
1646
Jean Delvare2bb50952009-09-18 22:45:46 +02001647class_err:
1648#ifdef CONFIG_I2C_COMPAT
1649 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001650bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001651#endif
David Brownelle9f13732008-01-27 18:14:52 +01001652 bus_unregister(&i2c_bus_type);
1653 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656static void __exit i2c_exit(void)
1657{
David Brownelle9f13732008-01-27 18:14:52 +01001658 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001659#ifdef CONFIG_I2C_COMPAT
1660 class_compat_unregister(i2c_adapter_compat_class);
1661#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 bus_unregister(&i2c_bus_type);
1663}
1664
David Brownella10f9e72008-10-14 17:30:06 +02001665/* We must initialize early, because some subsystems register i2c drivers
1666 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1667 */
1668postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669module_exit(i2c_exit);
1670
1671/* ----------------------------------------------------
1672 * the functional interface to the i2c busses.
1673 * ----------------------------------------------------
1674 */
1675
David Brownella1cdeda2008-07-14 22:38:24 +02001676/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001677 * __i2c_transfer - unlocked flavor of i2c_transfer
1678 * @adap: Handle to I2C bus
1679 * @msgs: One or more messages to execute before STOP is issued to
1680 * terminate the operation; each message begins with a START.
1681 * @num: Number of messages to be executed.
1682 *
1683 * Returns negative errno, else the number of messages executed.
1684 *
1685 * Adapter lock must be held when calling this function. No debug logging
1686 * takes place. adap->algo->master_xfer existence isn't checked.
1687 */
1688int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1689{
1690 unsigned long orig_jiffies;
1691 int ret, try;
1692
1693 /* Retry automatically on arbitration loss */
1694 orig_jiffies = jiffies;
1695 for (ret = 0, try = 0; try <= adap->retries; try++) {
1696 ret = adap->algo->master_xfer(adap, msgs, num);
1697 if (ret != -EAGAIN)
1698 break;
1699 if (time_after(jiffies, orig_jiffies + adap->timeout))
1700 break;
1701 }
1702
1703 return ret;
1704}
1705EXPORT_SYMBOL(__i2c_transfer);
1706
1707/**
David Brownella1cdeda2008-07-14 22:38:24 +02001708 * i2c_transfer - execute a single or combined I2C message
1709 * @adap: Handle to I2C bus
1710 * @msgs: One or more messages to execute before STOP is issued to
1711 * terminate the operation; each message begins with a START.
1712 * @num: Number of messages to be executed.
1713 *
1714 * Returns negative errno, else the number of messages executed.
1715 *
1716 * Note that there is no requirement that each message be sent to
1717 * the same slave address, although that is the most common model.
1718 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001719int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001721 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
David Brownella1cdeda2008-07-14 22:38:24 +02001723 /* REVISIT the fault reporting model here is weak:
1724 *
1725 * - When we get an error after receiving N bytes from a slave,
1726 * there is no way to report "N".
1727 *
1728 * - When we get a NAK after transmitting N bytes to a slave,
1729 * there is no way to report "N" ... or to let the master
1730 * continue executing the rest of this combined message, if
1731 * that's the appropriate response.
1732 *
1733 * - When for example "num" is two and we successfully complete
1734 * the first message but get an error part way through the
1735 * second, it's unclear whether that should be reported as
1736 * one (discarding status on the second message) or errno
1737 * (discarding status on the first one).
1738 */
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (adap->algo->master_xfer) {
1741#ifdef DEBUG
1742 for (ret = 0; ret < num; ret++) {
1743 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001744 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1745 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1746 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748#endif
1749
Mike Rapoportcea443a82008-01-27 18:14:50 +01001750 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001751 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001752 if (!ret)
1753 /* I2C activity is ongoing. */
1754 return -EAGAIN;
1755 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001756 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001757 }
1758
Jean Delvareb37d2a32012-06-29 07:47:19 -03001759 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001760 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 return ret;
1763 } else {
1764 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001765 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767}
David Brownellc0564602007-05-01 23:26:31 +02001768EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
David Brownella1cdeda2008-07-14 22:38:24 +02001770/**
1771 * i2c_master_send - issue a single I2C message in master transmit mode
1772 * @client: Handle to slave device
1773 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001774 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001775 *
1776 * Returns negative errno, or else the number of bytes written.
1777 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001778int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001781 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 struct i2c_msg msg;
1783
Jean Delvare815f55f2005-05-07 22:58:46 +02001784 msg.addr = client->addr;
1785 msg.flags = client->flags & I2C_M_TEN;
1786 msg.len = count;
1787 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001788
Jean Delvare815f55f2005-05-07 22:58:46 +02001789 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001791 /*
1792 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1793 * transmitted, else error code.
1794 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001795 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
David Brownellc0564602007-05-01 23:26:31 +02001797EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
David Brownella1cdeda2008-07-14 22:38:24 +02001799/**
1800 * i2c_master_recv - issue a single I2C message in master receive mode
1801 * @client: Handle to slave device
1802 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001803 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001804 *
1805 * Returns negative errno, or else the number of bytes read.
1806 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001807int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Farid Hammane7225acf2010-05-21 18:40:58 +02001809 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct i2c_msg msg;
1811 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Jean Delvare815f55f2005-05-07 22:58:46 +02001813 msg.addr = client->addr;
1814 msg.flags = client->flags & I2C_M_TEN;
1815 msg.flags |= I2C_M_RD;
1816 msg.len = count;
1817 msg.buf = buf;
1818
1819 ret = i2c_transfer(adap, &msg, 1);
1820
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001821 /*
1822 * If everything went ok (i.e. 1 msg received), return #bytes received,
1823 * else error code.
1824 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001825 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
David Brownellc0564602007-05-01 23:26:31 +02001827EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829/* ----------------------------------------------------
1830 * the i2c address scanning function
1831 * Will not work for 10-bit addresses!
1832 * ----------------------------------------------------
1833 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001834
Jean Delvare63e4e802010-06-03 11:33:51 +02001835/*
1836 * Legacy default probe function, mostly relevant for SMBus. The default
1837 * probe method is a quick write, but it is known to corrupt the 24RF08
1838 * EEPROMs due to a state machine bug, and could also irreversibly
1839 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1840 * we use a short byte read instead. Also, some bus drivers don't implement
1841 * quick write, so we fallback to a byte read in that case too.
1842 * On x86, there is another special case for FSC hardware monitoring chips,
1843 * which want regular byte reads (address 0x73.) Fortunately, these are the
1844 * only known chips using this I2C address on PC hardware.
1845 * Returns 1 if probe succeeded, 0 if not.
1846 */
1847static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1848{
1849 int err;
1850 union i2c_smbus_data dummy;
1851
1852#ifdef CONFIG_X86
1853 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1854 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1855 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1856 I2C_SMBUS_BYTE_DATA, &dummy);
1857 else
1858#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001859 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1860 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001861 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1862 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001863 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1864 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1865 I2C_SMBUS_BYTE, &dummy);
1866 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001867 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1868 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001869 err = -EOPNOTSUPP;
1870 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001871
1872 return err >= 0;
1873}
1874
Jean Delvareccfbbd02009-12-06 17:06:25 +01001875static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001876 struct i2c_driver *driver)
1877{
1878 struct i2c_board_info info;
1879 struct i2c_adapter *adapter = temp_client->adapter;
1880 int addr = temp_client->addr;
1881 int err;
1882
1883 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001884 err = i2c_check_addr_validity(addr);
1885 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001886 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1887 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001888 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001889 }
1890
1891 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001892 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001893 return 0;
1894
Jean Delvareccfbbd02009-12-06 17:06:25 +01001895 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001896 if (!i2c_default_probe(adapter, addr))
1897 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001898
1899 /* Finally call the custom detection function */
1900 memset(&info, 0, sizeof(struct i2c_board_info));
1901 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001902 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001903 if (err) {
1904 /* -ENODEV is returned if the detection fails. We catch it
1905 here as this isn't an error. */
1906 return err == -ENODEV ? 0 : err;
1907 }
1908
1909 /* Consistency check */
1910 if (info.type[0] == '\0') {
1911 dev_err(&adapter->dev, "%s detection function provided "
1912 "no name for 0x%x\n", driver->driver.name,
1913 addr);
1914 } else {
1915 struct i2c_client *client;
1916
1917 /* Detection succeeded, instantiate the device */
1918 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1919 info.type, info.addr);
1920 client = i2c_new_device(adapter, &info);
1921 if (client)
1922 list_add_tail(&client->detected, &driver->clients);
1923 else
1924 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1925 info.type, info.addr);
1926 }
1927 return 0;
1928}
1929
1930static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1931{
Jean Delvarec3813d62009-12-14 21:17:25 +01001932 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001933 struct i2c_client *temp_client;
1934 int i, err = 0;
1935 int adap_id = i2c_adapter_id(adapter);
1936
Jean Delvarec3813d62009-12-14 21:17:25 +01001937 address_list = driver->address_list;
1938 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001939 return 0;
1940
Jean Delvare51b54ba2010-10-24 18:16:58 +02001941 /* Stop here if the classes do not match */
1942 if (!(adapter->class & driver->class))
1943 return 0;
1944
Jean Delvare4735c982008-07-14 22:38:36 +02001945 /* Set up a temporary client to help detect callback */
1946 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1947 if (!temp_client)
1948 return -ENOMEM;
1949 temp_client->adapter = adapter;
1950
Jean Delvarec3813d62009-12-14 21:17:25 +01001951 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001952 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001953 "addr 0x%02x\n", adap_id, address_list[i]);
1954 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001955 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001956 if (unlikely(err))
1957 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001958 }
1959
Jean Delvare4735c982008-07-14 22:38:36 +02001960 kfree(temp_client);
1961 return err;
1962}
1963
Jean Delvared44f19d2010-08-11 18:20:57 +02001964int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1965{
1966 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1967 I2C_SMBUS_QUICK, NULL) >= 0;
1968}
1969EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1970
Jean Delvare12b5053a2007-05-01 23:26:31 +02001971struct i2c_client *
1972i2c_new_probed_device(struct i2c_adapter *adap,
1973 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001974 unsigned short const *addr_list,
1975 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001976{
1977 int i;
1978
Jean Delvare8031d792010-08-11 18:21:00 +02001979 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001980 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001981
Jean Delvare12b5053a2007-05-01 23:26:31 +02001982 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1983 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001984 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001985 dev_warn(&adap->dev, "Invalid 7-bit address "
1986 "0x%02x\n", addr_list[i]);
1987 continue;
1988 }
1989
1990 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001991 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001992 dev_dbg(&adap->dev, "Address 0x%02x already in "
1993 "use, not probing\n", addr_list[i]);
1994 continue;
1995 }
1996
Jean Delvare63e4e802010-06-03 11:33:51 +02001997 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001998 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001999 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002000 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002001
2002 if (addr_list[i] == I2C_CLIENT_END) {
2003 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2004 return NULL;
2005 }
2006
2007 info->addr = addr_list[i];
2008 return i2c_new_device(adap, info);
2009}
2010EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2011
Jean Delvared735b342011-03-20 14:50:52 +01002012struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002015
Jean Delvarecaada322008-01-27 18:14:49 +01002016 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002017 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002018 if (adapter && !try_module_get(adapter->owner))
2019 adapter = NULL;
2020
Jean Delvarecaada322008-01-27 18:14:49 +01002021 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e102005-06-28 00:21:30 -04002022 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023}
David Brownellc0564602007-05-01 23:26:31 +02002024EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026void i2c_put_adapter(struct i2c_adapter *adap)
2027{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002028 if (adap)
2029 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
David Brownellc0564602007-05-01 23:26:31 +02002031EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033/* The SMBus parts */
2034
David Brownell438d6c22006-12-10 21:21:31 +01002035#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002036static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
2038 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002039
Farid Hammane7225acf2010-05-21 18:40:58 +02002040 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002041 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 data = data ^ POLY;
2043 data = data << 1;
2044 }
2045 return (u8)(data >> 8);
2046}
2047
Jean Delvare421ef472005-10-26 21:28:55 +02002048/* Incremental CRC8 over count bytes in the array pointed to by p */
2049static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 int i;
2052
Farid Hammane7225acf2010-05-21 18:40:58 +02002053 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002054 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return crc;
2056}
2057
Jean Delvare421ef472005-10-26 21:28:55 +02002058/* Assume a 7-bit address, which is reasonable for SMBus */
2059static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Jean Delvare421ef472005-10-26 21:28:55 +02002061 /* The address will be sent first */
2062 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2063 pec = i2c_smbus_pec(pec, &addr, 1);
2064
2065 /* The data buffer follows */
2066 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Jean Delvare421ef472005-10-26 21:28:55 +02002069/* Used for write only transactions */
2070static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
Jean Delvare421ef472005-10-26 21:28:55 +02002072 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2073 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Jean Delvare421ef472005-10-26 21:28:55 +02002076/* Return <0 on CRC error
2077 If there was a write before this read (most cases) we need to take the
2078 partial CRC from the write part into account.
2079 Note that this function does modify the message (we need to decrease the
2080 message length to hide the CRC byte from the caller). */
2081static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
Jean Delvare421ef472005-10-26 21:28:55 +02002083 u8 rpec = msg->buf[--msg->len];
2084 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (rpec != cpec) {
2087 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2088 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002089 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
David Brownell438d6c22006-12-10 21:21:31 +01002091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
David Brownella1cdeda2008-07-14 22:38:24 +02002094/**
2095 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2096 * @client: Handle to slave device
2097 *
2098 * This executes the SMBus "receive byte" protocol, returning negative errno
2099 * else the byte received from the device.
2100 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002101s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
2103 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002104 int status;
2105
2106 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2107 I2C_SMBUS_READ, 0,
2108 I2C_SMBUS_BYTE, &data);
2109 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
David Brownellc0564602007-05-01 23:26:31 +02002111EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
David Brownella1cdeda2008-07-14 22:38:24 +02002113/**
2114 * i2c_smbus_write_byte - SMBus "send byte" protocol
2115 * @client: Handle to slave device
2116 * @value: Byte to be sent
2117 *
2118 * This executes the SMBus "send byte" protocol, returning negative errno
2119 * else zero on success.
2120 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002121s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Farid Hammane7225acf2010-05-21 18:40:58 +02002123 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002124 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
David Brownellc0564602007-05-01 23:26:31 +02002126EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
David Brownella1cdeda2008-07-14 22:38:24 +02002128/**
2129 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2130 * @client: Handle to slave device
2131 * @command: Byte interpreted by slave
2132 *
2133 * This executes the SMBus "read byte" protocol, returning negative errno
2134 * else a data byte received from the device.
2135 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002136s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
2138 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002139 int status;
2140
2141 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2142 I2C_SMBUS_READ, command,
2143 I2C_SMBUS_BYTE_DATA, &data);
2144 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
David Brownellc0564602007-05-01 23:26:31 +02002146EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
David Brownella1cdeda2008-07-14 22:38:24 +02002148/**
2149 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2150 * @client: Handle to slave device
2151 * @command: Byte interpreted by slave
2152 * @value: Byte being written
2153 *
2154 * This executes the SMBus "write byte" protocol, returning negative errno
2155 * else zero on success.
2156 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002157s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2158 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 union i2c_smbus_data data;
2161 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002162 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2163 I2C_SMBUS_WRITE, command,
2164 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
David Brownellc0564602007-05-01 23:26:31 +02002166EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
David Brownella1cdeda2008-07-14 22:38:24 +02002168/**
2169 * i2c_smbus_read_word_data - SMBus "read word" protocol
2170 * @client: Handle to slave device
2171 * @command: Byte interpreted by slave
2172 *
2173 * This executes the SMBus "read word" protocol, returning negative errno
2174 * else a 16-bit unsigned "word" received from the device.
2175 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002176s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002179 int status;
2180
2181 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2182 I2C_SMBUS_READ, command,
2183 I2C_SMBUS_WORD_DATA, &data);
2184 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
David Brownellc0564602007-05-01 23:26:31 +02002186EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
David Brownella1cdeda2008-07-14 22:38:24 +02002188/**
2189 * i2c_smbus_write_word_data - SMBus "write word" protocol
2190 * @client: Handle to slave device
2191 * @command: Byte interpreted by slave
2192 * @value: 16-bit "word" being written
2193 *
2194 * This executes the SMBus "write word" protocol, returning negative errno
2195 * else zero on success.
2196 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002197s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2198 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 union i2c_smbus_data data;
2201 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002202 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2203 I2C_SMBUS_WRITE, command,
2204 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
David Brownellc0564602007-05-01 23:26:31 +02002206EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
David Brownella64ec072007-10-13 23:56:31 +02002208/**
David Brownella1cdeda2008-07-14 22:38:24 +02002209 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002210 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002211 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002212 * @values: Byte array into which data will be read; big enough to hold
2213 * the data returned by the slave. SMBus allows at most 32 bytes.
2214 *
David Brownella1cdeda2008-07-14 22:38:24 +02002215 * This executes the SMBus "block read" protocol, returning negative errno
2216 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002217 *
2218 * Note that using this function requires that the client's adapter support
2219 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2220 * support this; its emulation through I2C messaging relies on a specific
2221 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2222 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002223s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002224 u8 *values)
2225{
2226 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002227 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002228
David Brownell24a5bb72008-07-14 22:38:23 +02002229 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2230 I2C_SMBUS_READ, command,
2231 I2C_SMBUS_BLOCK_DATA, &data);
2232 if (status)
2233 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002234
2235 memcpy(values, &data.block[1], data.block[0]);
2236 return data.block[0];
2237}
2238EXPORT_SYMBOL(i2c_smbus_read_block_data);
2239
David Brownella1cdeda2008-07-14 22:38:24 +02002240/**
2241 * i2c_smbus_write_block_data - SMBus "block write" protocol
2242 * @client: Handle to slave device
2243 * @command: Byte interpreted by slave
2244 * @length: Size of data block; SMBus allows at most 32 bytes
2245 * @values: Byte array which will be written.
2246 *
2247 * This executes the SMBus "block write" protocol, returning negative errno
2248 * else zero on success.
2249 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002250s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002251 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (length > I2C_SMBUS_BLOCK_MAX)
2256 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002258 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002259 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2260 I2C_SMBUS_WRITE, command,
2261 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
David Brownellc0564602007-05-01 23:26:31 +02002263EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002266s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002267 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002270 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002271
Jean Delvare4b2643d2007-07-12 14:12:29 +02002272 if (length > I2C_SMBUS_BLOCK_MAX)
2273 length = I2C_SMBUS_BLOCK_MAX;
2274 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002275 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2276 I2C_SMBUS_READ, command,
2277 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2278 if (status < 0)
2279 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002280
2281 memcpy(values, &data.block[1], data.block[0]);
2282 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
David Brownellc0564602007-05-01 23:26:31 +02002284EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Jean Delvare0cc43a12011-01-10 22:11:23 +01002286s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002287 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002288{
2289 union i2c_smbus_data data;
2290
2291 if (length > I2C_SMBUS_BLOCK_MAX)
2292 length = I2C_SMBUS_BLOCK_MAX;
2293 data.block[0] = length;
2294 memcpy(data.block + 1, values, length);
2295 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2296 I2C_SMBUS_WRITE, command,
2297 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2298}
David Brownellc0564602007-05-01 23:26:31 +02002299EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002300
David Brownell438d6c22006-12-10 21:21:31 +01002301/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002303static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2304 unsigned short flags,
2305 char read_write, u8 command, int size,
2306 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 /* So we need to generate a series of msgs. In the case of writing, we
2309 need to use only one message; when reading, we need two. We initialize
2310 most things with sane defaults, to keep the code below somewhat
2311 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002312 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2313 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002314 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002316 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002317 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002318 struct i2c_msg msg[2] = {
2319 {
2320 .addr = addr,
2321 .flags = flags,
2322 .len = 1,
2323 .buf = msgbuf0,
2324 }, {
2325 .addr = addr,
2326 .flags = flags | I2C_M_RD,
2327 .len = 0,
2328 .buf = msgbuf1,
2329 },
2330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002333 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 case I2C_SMBUS_QUICK:
2335 msg[0].len = 0;
2336 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002337 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2338 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 num = 1;
2340 break;
2341 case I2C_SMBUS_BYTE:
2342 if (read_write == I2C_SMBUS_READ) {
2343 /* Special case: only a read! */
2344 msg[0].flags = I2C_M_RD | flags;
2345 num = 1;
2346 }
2347 break;
2348 case I2C_SMBUS_BYTE_DATA:
2349 if (read_write == I2C_SMBUS_READ)
2350 msg[1].len = 1;
2351 else {
2352 msg[0].len = 2;
2353 msgbuf0[1] = data->byte;
2354 }
2355 break;
2356 case I2C_SMBUS_WORD_DATA:
2357 if (read_write == I2C_SMBUS_READ)
2358 msg[1].len = 2;
2359 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002360 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002362 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
2364 break;
2365 case I2C_SMBUS_PROC_CALL:
2366 num = 2; /* Special case */
2367 read_write = I2C_SMBUS_READ;
2368 msg[0].len = 3;
2369 msg[1].len = 2;
2370 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002371 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002375 msg[1].flags |= I2C_M_RECV_LEN;
2376 msg[1].len = 1; /* block length will be added by
2377 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 } else {
2379 msg[0].len = data->block[0] + 2;
2380 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002381 dev_err(&adapter->dev,
2382 "Invalid block write size %d\n",
2383 data->block[0]);
2384 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002386 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 msgbuf0[i] = data->block[i-1];
2388 }
2389 break;
2390 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002391 num = 2; /* Another special case */
2392 read_write = I2C_SMBUS_READ;
2393 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002394 dev_err(&adapter->dev,
2395 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002396 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002397 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002398 }
2399 msg[0].len = data->block[0] + 2;
2400 for (i = 1; i < msg[0].len; i++)
2401 msgbuf0[i] = data->block[i-1];
2402 msg[1].flags |= I2C_M_RECV_LEN;
2403 msg[1].len = 1; /* block length will be added by
2404 the underlying bus driver */
2405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 case I2C_SMBUS_I2C_BLOCK_DATA:
2407 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002408 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 } else {
2410 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002411 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002412 dev_err(&adapter->dev,
2413 "Invalid block write size %d\n",
2414 data->block[0]);
2415 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 for (i = 1; i <= data->block[0]; i++)
2418 msgbuf0[i] = data->block[i];
2419 }
2420 break;
2421 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002422 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2423 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425
Jean Delvare421ef472005-10-26 21:28:55 +02002426 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2427 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2428 if (i) {
2429 /* Compute PEC if first message is a write */
2430 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002431 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002432 i2c_smbus_add_pec(&msg[0]);
2433 else /* Write followed by read */
2434 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2435 }
2436 /* Ask for PEC if last message is a read */
2437 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002438 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002439 }
2440
David Brownell24a5bb72008-07-14 22:38:23 +02002441 status = i2c_transfer(adapter, msg, num);
2442 if (status < 0)
2443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jean Delvare421ef472005-10-26 21:28:55 +02002445 /* Check PEC if last message is a read */
2446 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002447 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2448 if (status < 0)
2449 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002450 }
2451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002453 switch (size) {
2454 case I2C_SMBUS_BYTE:
2455 data->byte = msgbuf0[0];
2456 break;
2457 case I2C_SMBUS_BYTE_DATA:
2458 data->byte = msgbuf1[0];
2459 break;
2460 case I2C_SMBUS_WORD_DATA:
2461 case I2C_SMBUS_PROC_CALL:
2462 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2463 break;
2464 case I2C_SMBUS_I2C_BLOCK_DATA:
2465 for (i = 0; i < data->block[0]; i++)
2466 data->block[i+1] = msgbuf1[i];
2467 break;
2468 case I2C_SMBUS_BLOCK_DATA:
2469 case I2C_SMBUS_BLOCK_PROC_CALL:
2470 for (i = 0; i < msgbuf1[0] + 1; i++)
2471 data->block[i] = msgbuf1[i];
2472 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474 return 0;
2475}
2476
David Brownella1cdeda2008-07-14 22:38:24 +02002477/**
2478 * i2c_smbus_xfer - execute SMBus protocol operations
2479 * @adapter: Handle to I2C bus
2480 * @addr: Address of SMBus slave on that bus
2481 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2482 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2483 * @command: Byte interpreted by slave, for protocols which use such bytes
2484 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2485 * @data: Data to be read or written
2486 *
2487 * This executes an SMBus protocol operation, and returns a negative
2488 * errno code else zero on success.
2489 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002490s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002491 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002492 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002494 unsigned long orig_jiffies;
2495 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002498 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002501 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002502
2503 /* Retry automatically on arbitration loss */
2504 orig_jiffies = jiffies;
2505 for (res = 0, try = 0; try <= adapter->retries; try++) {
2506 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2507 read_write, command,
2508 protocol, data);
2509 if (res != -EAGAIN)
2510 break;
2511 if (time_after(jiffies,
2512 orig_jiffies + adapter->timeout))
2513 break;
2514 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002515 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002517 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2518 return res;
2519 /*
2520 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2521 * implement native support for the SMBus operation.
2522 */
2523 }
2524
2525 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2526 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2531MODULE_DESCRIPTION("I2C-Bus main module");
2532MODULE_LICENSE("GPL");