blob: 5923cfa390c86de6528559c229ace0a4b39b402c [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
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300257 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200258 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200259 if (status) {
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200260 i2c_set_clientdata(client, NULL);
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300261 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200262 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100263 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266static int i2c_device_remove(struct device *dev)
267{
Jean Delvare51298d12009-09-18 22:45:45 +0200268 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200269 struct i2c_driver *driver;
270 int status;
271
Jean Delvare51298d12009-09-18 22:45:45 +0200272 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200273 return 0;
274
275 driver = to_i2c_driver(dev->driver);
276 if (driver->remove) {
277 dev_dbg(dev, "remove\n");
278 status = driver->remove(client);
279 } else {
280 dev->driver = NULL;
281 status = 0;
282 }
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200283 if (status == 0)
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200284 i2c_set_clientdata(client, NULL);
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300285 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200286 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
David Brownellf37dd802007-02-13 22:09:00 +0100289static void i2c_device_shutdown(struct device *dev)
290{
Jean Delvare51298d12009-09-18 22:45:45 +0200291 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100292 struct i2c_driver *driver;
293
Jean Delvare51298d12009-09-18 22:45:45 +0200294 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100295 return;
296 driver = to_i2c_driver(dev->driver);
297 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200298 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100299}
300
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200301#ifdef CONFIG_PM_SLEEP
302static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100303{
Jean Delvare51298d12009-09-18 22:45:45 +0200304 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100305 struct i2c_driver *driver;
306
Jean Delvare51298d12009-09-18 22:45:45 +0200307 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100308 return 0;
309 driver = to_i2c_driver(dev->driver);
310 if (!driver->suspend)
311 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200312 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100313}
314
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200315static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100316{
Jean Delvare51298d12009-09-18 22:45:45 +0200317 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100318 struct i2c_driver *driver;
319
Jean Delvare51298d12009-09-18 22:45:45 +0200320 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100321 return 0;
322 driver = to_i2c_driver(dev->driver);
323 if (!driver->resume)
324 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200325 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100326}
327
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200328static int i2c_device_pm_suspend(struct device *dev)
329{
330 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
331
Mark Brownd529de22011-01-14 22:03:49 +0100332 if (pm)
333 return pm_generic_suspend(dev);
334 else
335 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200336}
337
338static int i2c_device_pm_resume(struct device *dev)
339{
340 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200341
342 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100343 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200344 else
Mark Brownd529de22011-01-14 22:03:49 +0100345 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200346}
347
348static int i2c_device_pm_freeze(struct device *dev)
349{
350 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
351
Mark Brownd529de22011-01-14 22:03:49 +0100352 if (pm)
353 return pm_generic_freeze(dev);
354 else
355 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200356}
357
358static int i2c_device_pm_thaw(struct device *dev)
359{
360 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
361
Mark Brownd529de22011-01-14 22:03:49 +0100362 if (pm)
363 return pm_generic_thaw(dev);
364 else
365 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200366}
367
368static int i2c_device_pm_poweroff(struct device *dev)
369{
370 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
371
Mark Brownd529de22011-01-14 22:03:49 +0100372 if (pm)
373 return pm_generic_poweroff(dev);
374 else
375 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200376}
377
378static int i2c_device_pm_restore(struct device *dev)
379{
380 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200381
382 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100383 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200384 else
Mark Brownd529de22011-01-14 22:03:49 +0100385 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200386}
387#else /* !CONFIG_PM_SLEEP */
388#define i2c_device_pm_suspend NULL
389#define i2c_device_pm_resume NULL
390#define i2c_device_pm_freeze NULL
391#define i2c_device_pm_thaw NULL
392#define i2c_device_pm_poweroff NULL
393#define i2c_device_pm_restore NULL
394#endif /* !CONFIG_PM_SLEEP */
395
David Brownell9c1600e2007-05-01 23:26:31 +0200396static void i2c_client_dev_release(struct device *dev)
397{
398 kfree(to_i2c_client(dev));
399}
400
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100401static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200402show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200403{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200404 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
405 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200406}
407
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100408static ssize_t
409show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200410{
411 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200412 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200413}
414
Jean Delvare4f8cf822009-09-18 22:45:46 +0200415static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200416static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
417
418static struct attribute *i2c_dev_attrs[] = {
419 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200420 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200421 &dev_attr_modalias.attr,
422 NULL
423};
424
425static struct attribute_group i2c_dev_attr_group = {
426 .attrs = i2c_dev_attrs,
427};
428
429static const struct attribute_group *i2c_dev_attr_groups[] = {
430 &i2c_dev_attr_group,
431 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200432};
433
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100434static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100435 .suspend = i2c_device_pm_suspend,
436 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200437 .freeze = i2c_device_pm_freeze,
438 .thaw = i2c_device_pm_thaw,
439 .poweroff = i2c_device_pm_poweroff,
440 .restore = i2c_device_pm_restore,
441 SET_RUNTIME_PM_OPS(
442 pm_generic_runtime_suspend,
443 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200444 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200445 )
sonic zhang54067ee2009-12-14 21:17:30 +0100446};
447
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200448struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100449 .name = "i2c",
450 .match = i2c_device_match,
451 .probe = i2c_device_probe,
452 .remove = i2c_device_remove,
453 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100454 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000455};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200456EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000457
Jean Delvare51298d12009-09-18 22:45:45 +0200458static struct device_type i2c_client_type = {
459 .groups = i2c_dev_attr_groups,
460 .uevent = i2c_device_uevent,
461 .release = i2c_client_dev_release,
462};
463
David Brownell9b766b82008-01-27 18:14:51 +0100464
465/**
466 * i2c_verify_client - return parameter as i2c_client, or NULL
467 * @dev: device, probably from some driver model iterator
468 *
469 * When traversing the driver model tree, perhaps using driver model
470 * iterators like @device_for_each_child(), you can't assume very much
471 * about the nodes you find. Use this function to avoid oopses caused
472 * by wrongly treating some non-I2C device as an i2c_client.
473 */
474struct i2c_client *i2c_verify_client(struct device *dev)
475{
Jean Delvare51298d12009-09-18 22:45:45 +0200476 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100477 ? to_i2c_client(dev)
478 : NULL;
479}
480EXPORT_SYMBOL(i2c_verify_client);
481
482
Jean Delvare3a89db52010-06-03 11:33:52 +0200483/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300484 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200485static int i2c_check_client_addr_validity(const struct i2c_client *client)
486{
487 if (client->flags & I2C_CLIENT_TEN) {
488 /* 10-bit address, all values are valid */
489 if (client->addr > 0x3ff)
490 return -EINVAL;
491 } else {
492 /* 7-bit address, reject the general call address */
493 if (client->addr == 0x00 || client->addr > 0x7f)
494 return -EINVAL;
495 }
496 return 0;
497}
498
Jean Delvare656b8762010-06-03 11:33:53 +0200499/* And this is a strict address validity check, used when probing. If a
500 * device uses a reserved address, then it shouldn't be probed. 7-bit
501 * addressing is assumed, 10-bit address devices are rare and should be
502 * explicitly enumerated. */
503static int i2c_check_addr_validity(unsigned short addr)
504{
505 /*
506 * Reserved addresses per I2C specification:
507 * 0x00 General call address / START byte
508 * 0x01 CBUS address
509 * 0x02 Reserved for different bus format
510 * 0x03 Reserved for future purposes
511 * 0x04-0x07 Hs-mode master code
512 * 0x78-0x7b 10-bit slave addressing
513 * 0x7c-0x7f Reserved for future purposes
514 */
515 if (addr < 0x08 || addr > 0x77)
516 return -EINVAL;
517 return 0;
518}
519
Jean Delvare3b5f7942010-06-03 11:33:55 +0200520static int __i2c_check_addr_busy(struct device *dev, void *addrp)
521{
522 struct i2c_client *client = i2c_verify_client(dev);
523 int addr = *(int *)addrp;
524
525 if (client && client->addr == addr)
526 return -EBUSY;
527 return 0;
528}
529
Michael Lawnick08263742010-08-11 18:21:02 +0200530/* walk up mux tree */
531static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
532{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200533 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200534 int result;
535
536 result = device_for_each_child(&adapter->dev, &addr,
537 __i2c_check_addr_busy);
538
Jean Delvare97cc4d42010-10-24 18:16:57 +0200539 if (!result && parent)
540 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200541
542 return result;
543}
544
545/* recurse down mux tree */
546static int i2c_check_mux_children(struct device *dev, void *addrp)
547{
548 int result;
549
550 if (dev->type == &i2c_adapter_type)
551 result = device_for_each_child(dev, addrp,
552 i2c_check_mux_children);
553 else
554 result = __i2c_check_addr_busy(dev, addrp);
555
556 return result;
557}
558
Jean Delvare3b5f7942010-06-03 11:33:55 +0200559static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
560{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200561 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200562 int result = 0;
563
Jean Delvare97cc4d42010-10-24 18:16:57 +0200564 if (parent)
565 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200566
567 if (!result)
568 result = device_for_each_child(&adapter->dev, &addr,
569 i2c_check_mux_children);
570
571 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200572}
573
David Brownell9c1600e2007-05-01 23:26:31 +0200574/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200575 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
576 * @adapter: Target I2C bus segment
577 */
578void i2c_lock_adapter(struct i2c_adapter *adapter)
579{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200580 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
581
582 if (parent)
583 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200584 else
585 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200586}
587EXPORT_SYMBOL_GPL(i2c_lock_adapter);
588
589/**
590 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
591 * @adapter: Target I2C bus segment
592 */
593static int i2c_trylock_adapter(struct i2c_adapter *adapter)
594{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200595 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
596
597 if (parent)
598 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200599 else
600 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200601}
602
603/**
604 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
605 * @adapter: Target I2C bus segment
606 */
607void i2c_unlock_adapter(struct i2c_adapter *adapter)
608{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200609 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
610
611 if (parent)
612 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200613 else
614 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200615}
616EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
617
618/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200619 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200620 * @adap: the adapter managing the device
621 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200622 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200623 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200624 * Create an i2c device. Binding is handled through driver model
625 * probe()/remove() methods. A driver may be bound to this device when we
626 * return from this function, or any later moment (e.g. maybe hotplugging will
627 * load the driver module). This call is not appropriate for use by mainboard
628 * initialization logic, which usually runs during an arch_initcall() long
629 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200630 *
631 * This returns the new i2c client, which may be saved for later use with
632 * i2c_unregister_device(); or NULL to indicate an error.
633 */
634struct i2c_client *
635i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
636{
637 struct i2c_client *client;
638 int status;
639
640 client = kzalloc(sizeof *client, GFP_KERNEL);
641 if (!client)
642 return NULL;
643
644 client->adapter = adap;
645
646 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200647
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200648 if (info->archdata)
649 client->dev.archdata = *info->archdata;
650
Marc Pignatee354252008-08-28 08:33:22 +0200651 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200652 client->addr = info->addr;
653 client->irq = info->irq;
654
David Brownell9c1600e2007-05-01 23:26:31 +0200655 strlcpy(client->name, info->type, sizeof(client->name));
656
Jean Delvare3a89db52010-06-03 11:33:52 +0200657 /* Check for address validity */
658 status = i2c_check_client_addr_validity(client);
659 if (status) {
660 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
661 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
662 goto out_err_silent;
663 }
664
Jean Delvaref8a227e2009-06-19 16:58:18 +0200665 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200666 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200667 if (status)
668 goto out_err;
669
670 client->dev.parent = &client->adapter->dev;
671 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200672 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700673 client->dev.of_node = info->of_node;
Mika Westerberg907ddf82012-11-23 12:23:40 +0100674 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200675
Jean Delvarecbb44512011-11-23 11:33:07 +0100676 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
Jean Delvaref8a227e2009-06-19 16:58:18 +0200677 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Jean Delvarecbb44512011-11-23 11:33:07 +0100678 client->addr | ((client->flags & I2C_CLIENT_TEN)
679 ? 0xa000 : 0));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200680 status = device_register(&client->dev);
681 if (status)
682 goto out_err;
683
Jean Delvaref8a227e2009-06-19 16:58:18 +0200684 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
685 client->name, dev_name(&client->dev));
686
David Brownell9c1600e2007-05-01 23:26:31 +0200687 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200688
689out_err:
690 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
691 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200692out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200693 kfree(client);
694 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200695}
696EXPORT_SYMBOL_GPL(i2c_new_device);
697
698
699/**
700 * i2c_unregister_device - reverse effect of i2c_new_device()
701 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200702 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200703 */
704void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200705{
David Brownella1d9e6e2007-05-01 23:26:30 +0200706 device_unregister(&client->dev);
707}
David Brownell9c1600e2007-05-01 23:26:31 +0200708EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200709
710
Jean Delvare60b129d2008-05-11 20:37:06 +0200711static const struct i2c_device_id dummy_id[] = {
712 { "dummy", 0 },
713 { },
714};
715
Jean Delvared2653e92008-04-29 23:11:39 +0200716static int dummy_probe(struct i2c_client *client,
717 const struct i2c_device_id *id)
718{
719 return 0;
720}
721
722static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100723{
724 return 0;
725}
726
727static struct i2c_driver dummy_driver = {
728 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200729 .probe = dummy_probe,
730 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200731 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100732};
733
734/**
735 * i2c_new_dummy - return a new i2c device bound to a dummy driver
736 * @adapter: the adapter managing the device
737 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100738 * Context: can sleep
739 *
740 * This returns an I2C client bound to the "dummy" driver, intended for use
741 * with devices that consume multiple addresses. Examples of such chips
742 * include various EEPROMS (like 24c04 and 24c08 models).
743 *
744 * These dummy devices have two main uses. First, most I2C and SMBus calls
745 * except i2c_transfer() need a client handle; the dummy will be that handle.
746 * And second, this prevents the specified address from being bound to a
747 * different driver.
748 *
749 * This returns the new i2c client, which should be saved for later use with
750 * i2c_unregister_device(); or NULL to indicate an error.
751 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100752struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100753{
754 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200755 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100756 };
757
David Brownelle9f13732008-01-27 18:14:52 +0100758 return i2c_new_device(adapter, &info);
759}
760EXPORT_SYMBOL_GPL(i2c_new_dummy);
761
David Brownellf37dd802007-02-13 22:09:00 +0100762/* ------------------------------------------------------------------------- */
763
David Brownell16ffadf2007-05-01 23:26:28 +0200764/* I2C bus adapters -- one roots each I2C or SMBUS segment */
765
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200766static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
David Brownellef2c83212007-05-01 23:26:28 +0200768 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 complete(&adap->dev_released);
770}
771
Jean Delvare99cd8e22009-06-19 16:58:20 +0200772/*
Jean Delvare390946b2012-09-10 10:14:02 +0200773 * This function is only needed for mutex_lock_nested, so it is never
774 * called unless locking correctness checking is enabled. Thus we
775 * make it inline to avoid a compiler warning. That's what gcc ends up
776 * doing anyway.
777 */
778static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
779{
780 unsigned int depth = 0;
781
782 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
783 depth++;
784
785 return depth;
786}
787
788/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200789 * Let users instantiate I2C devices through sysfs. This can be used when
790 * platform initialization code doesn't contain the proper data for
791 * whatever reason. Also useful for drivers that do device detection and
792 * detection fails, either because the device uses an unexpected address,
793 * or this is a compatible device with different ID register values.
794 *
795 * Parameter checking may look overzealous, but we really don't want
796 * the user to provide incorrect parameters.
797 */
798static ssize_t
799i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
800 const char *buf, size_t count)
801{
802 struct i2c_adapter *adap = to_i2c_adapter(dev);
803 struct i2c_board_info info;
804 struct i2c_client *client;
805 char *blank, end;
806 int res;
807
Jean Delvare99cd8e22009-06-19 16:58:20 +0200808 memset(&info, 0, sizeof(struct i2c_board_info));
809
810 blank = strchr(buf, ' ');
811 if (!blank) {
812 dev_err(dev, "%s: Missing parameters\n", "new_device");
813 return -EINVAL;
814 }
815 if (blank - buf > I2C_NAME_SIZE - 1) {
816 dev_err(dev, "%s: Invalid device name\n", "new_device");
817 return -EINVAL;
818 }
819 memcpy(info.type, buf, blank - buf);
820
821 /* Parse remaining parameters, reject extra parameters */
822 res = sscanf(++blank, "%hi%c", &info.addr, &end);
823 if (res < 1) {
824 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
825 return -EINVAL;
826 }
827 if (res > 1 && end != '\n') {
828 dev_err(dev, "%s: Extra parameters\n", "new_device");
829 return -EINVAL;
830 }
831
Jean Delvare99cd8e22009-06-19 16:58:20 +0200832 client = i2c_new_device(adap, &info);
833 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200834 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200835
836 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200837 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200838 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200839 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200840 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
841 info.type, info.addr);
842
843 return count;
844}
845
846/*
847 * And of course let the users delete the devices they instantiated, if
848 * they got it wrong. This interface can only be used to delete devices
849 * instantiated by i2c_sysfs_new_device above. This guarantees that we
850 * don't delete devices to which some kernel code still has references.
851 *
852 * Parameter checking may look overzealous, but we really don't want
853 * the user to delete the wrong device.
854 */
855static ssize_t
856i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
857 const char *buf, size_t count)
858{
859 struct i2c_adapter *adap = to_i2c_adapter(dev);
860 struct i2c_client *client, *next;
861 unsigned short addr;
862 char end;
863 int res;
864
865 /* Parse parameters, reject extra parameters */
866 res = sscanf(buf, "%hi%c", &addr, &end);
867 if (res < 1) {
868 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
869 return -EINVAL;
870 }
871 if (res > 1 && end != '\n') {
872 dev_err(dev, "%s: Extra parameters\n", "delete_device");
873 return -EINVAL;
874 }
875
876 /* Make sure the device was added through sysfs */
877 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200878 mutex_lock_nested(&adap->userspace_clients_lock,
879 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200880 list_for_each_entry_safe(client, next, &adap->userspace_clients,
881 detected) {
882 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200883 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
884 "delete_device", client->name, client->addr);
885
886 list_del(&client->detected);
887 i2c_unregister_device(client);
888 res = count;
889 break;
890 }
891 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200892 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200893
894 if (res < 0)
895 dev_err(dev, "%s: Can't find device in list\n",
896 "delete_device");
897 return res;
898}
899
Jean Delvare4f8cf822009-09-18 22:45:46 +0200900static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200901static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
902 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200903
904static struct attribute *i2c_adapter_attrs[] = {
905 &dev_attr_name.attr,
906 &dev_attr_new_device.attr,
907 &dev_attr_delete_device.attr,
908 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200909};
910
Jean Delvare4f8cf822009-09-18 22:45:46 +0200911static struct attribute_group i2c_adapter_attr_group = {
912 .attrs = i2c_adapter_attrs,
913};
914
915static const struct attribute_group *i2c_adapter_attr_groups[] = {
916 &i2c_adapter_attr_group,
917 NULL
918};
919
Michael Lawnick08263742010-08-11 18:21:02 +0200920struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200921 .groups = i2c_adapter_attr_groups,
922 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200923};
Michael Lawnick08263742010-08-11 18:21:02 +0200924EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Stephen Warren643dd092012-04-17 12:43:33 -0600926/**
927 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
928 * @dev: device, probably from some driver model iterator
929 *
930 * When traversing the driver model tree, perhaps using driver model
931 * iterators like @device_for_each_child(), you can't assume very much
932 * about the nodes you find. Use this function to avoid oopses caused
933 * by wrongly treating some non-I2C device as an i2c_adapter.
934 */
935struct i2c_adapter *i2c_verify_adapter(struct device *dev)
936{
937 return (dev->type == &i2c_adapter_type)
938 ? to_i2c_adapter(dev)
939 : NULL;
940}
941EXPORT_SYMBOL(i2c_verify_adapter);
942
Jean Delvare2bb50952009-09-18 22:45:46 +0200943#ifdef CONFIG_I2C_COMPAT
944static struct class_compat *i2c_adapter_compat_class;
945#endif
946
David Brownell9c1600e2007-05-01 23:26:31 +0200947static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
948{
949 struct i2c_devinfo *devinfo;
950
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200951 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200952 list_for_each_entry(devinfo, &__i2c_board_list, list) {
953 if (devinfo->busnum == adapter->nr
954 && !i2c_new_device(adapter,
955 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100956 dev_err(&adapter->dev,
957 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200958 devinfo->board_info.addr);
959 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200960 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200961}
962
Wolfram Sang687b81d2013-07-11 12:56:15 +0100963/* OF support code */
964
965#if IS_ENABLED(CONFIG_OF)
966static void of_i2c_register_devices(struct i2c_adapter *adap)
967{
968 void *result;
969 struct device_node *node;
970
971 /* Only register child devices if the adapter has a node pointer set */
972 if (!adap->dev.of_node)
973 return;
974
975 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
976
977 for_each_available_child_of_node(adap->dev.of_node, node) {
978 struct i2c_board_info info = {};
979 struct dev_archdata dev_ad = {};
980 const __be32 *addr;
981 int len;
982
983 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
984
985 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
986 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
987 node->full_name);
988 continue;
989 }
990
991 addr = of_get_property(node, "reg", &len);
992 if (!addr || (len < sizeof(int))) {
993 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
994 node->full_name);
995 continue;
996 }
997
998 info.addr = be32_to_cpup(addr);
999 if (info.addr > (1 << 10) - 1) {
1000 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1001 info.addr, node->full_name);
1002 continue;
1003 }
1004
1005 info.irq = irq_of_parse_and_map(node, 0);
1006 info.of_node = of_node_get(node);
1007 info.archdata = &dev_ad;
1008
1009 if (of_get_property(node, "wakeup-source", NULL))
1010 info.flags |= I2C_CLIENT_WAKE;
1011
1012 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1013
1014 result = i2c_new_device(adap, &info);
1015 if (result == NULL) {
1016 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1017 node->full_name);
1018 of_node_put(node);
1019 irq_dispose_mapping(info.irq);
1020 continue;
1021 }
1022 }
1023}
1024
1025static int of_dev_node_match(struct device *dev, void *data)
1026{
1027 return dev->of_node == data;
1028}
1029
1030/* must call put_device() when done with returned i2c_client device */
1031struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1032{
1033 struct device *dev;
1034
1035 dev = bus_find_device(&i2c_bus_type, NULL, node,
1036 of_dev_node_match);
1037 if (!dev)
1038 return NULL;
1039
1040 return i2c_verify_client(dev);
1041}
1042EXPORT_SYMBOL(of_find_i2c_device_by_node);
1043
1044/* must call put_device() when done with returned i2c_adapter device */
1045struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1046{
1047 struct device *dev;
1048
1049 dev = bus_find_device(&i2c_bus_type, NULL, node,
1050 of_dev_node_match);
1051 if (!dev)
1052 return NULL;
1053
1054 return i2c_verify_adapter(dev);
1055}
1056EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1057#else
1058static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1059#endif /* CONFIG_OF */
1060
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001061/* ACPI support code */
1062
1063#if IS_ENABLED(CONFIG_ACPI)
1064static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1065{
1066 struct i2c_board_info *info = data;
1067
1068 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1069 struct acpi_resource_i2c_serialbus *sb;
1070
1071 sb = &ares->data.i2c_serial_bus;
1072 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1073 info->addr = sb->slave_address;
1074 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1075 info->flags |= I2C_CLIENT_TEN;
1076 }
1077 } else if (info->irq < 0) {
1078 struct resource r;
1079
1080 if (acpi_dev_resource_interrupt(ares, 0, &r))
1081 info->irq = r.start;
1082 }
1083
1084 /* Tell the ACPI core to skip this resource */
1085 return 1;
1086}
1087
1088static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1089 void *data, void **return_value)
1090{
1091 struct i2c_adapter *adapter = data;
1092 struct list_head resource_list;
1093 struct i2c_board_info info;
1094 struct acpi_device *adev;
1095 int ret;
1096
1097 if (acpi_bus_get_device(handle, &adev))
1098 return AE_OK;
1099 if (acpi_bus_get_status(adev) || !adev->status.present)
1100 return AE_OK;
1101
1102 memset(&info, 0, sizeof(info));
1103 info.acpi_node.handle = handle;
1104 info.irq = -1;
1105
1106 INIT_LIST_HEAD(&resource_list);
1107 ret = acpi_dev_get_resources(adev, &resource_list,
1108 acpi_i2c_add_resource, &info);
1109 acpi_dev_free_resource_list(&resource_list);
1110
1111 if (ret < 0 || !info.addr)
1112 return AE_OK;
1113
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001114 adev->power.flags.ignore_parent = true;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001115 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1116 if (!i2c_new_device(adapter, &info)) {
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001117 adev->power.flags.ignore_parent = false;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001118 dev_err(&adapter->dev,
1119 "failed to add I2C device %s from ACPI\n",
1120 dev_name(&adev->dev));
1121 }
1122
1123 return AE_OK;
1124}
1125
1126/**
1127 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1128 * @adap: pointer to adapter
1129 *
1130 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1131 * namespace. When a device is found it will be added to the Linux device
1132 * model and bound to the corresponding ACPI handle.
1133 */
1134static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1135{
1136 acpi_handle handle;
1137 acpi_status status;
1138
Jean Delvare47b6e4772013-10-10 08:04:06 +02001139 if (!adap->dev.parent)
1140 return;
1141
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001142 handle = ACPI_HANDLE(adap->dev.parent);
1143 if (!handle)
1144 return;
1145
1146 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1147 acpi_i2c_add_device, NULL,
1148 adap, NULL);
1149 if (ACPI_FAILURE(status))
1150 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1151}
1152#else
1153static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1154#endif /* CONFIG_ACPI */
1155
Jean Delvare69b00892009-12-06 17:06:27 +01001156static int i2c_do_add_adapter(struct i2c_driver *driver,
1157 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001158{
Jean Delvare4735c982008-07-14 22:38:36 +02001159 /* Detect supported devices on that bus, and instantiate them */
1160 i2c_detect(adap, driver);
1161
1162 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001163 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001164 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1165 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001166 dev_warn(&adap->dev, "Please use another way to instantiate "
1167 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001168 /* We ignore the return code; if it fails, too bad */
1169 driver->attach_adapter(adap);
1170 }
1171 return 0;
1172}
1173
Jean Delvare69b00892009-12-06 17:06:27 +01001174static int __process_new_adapter(struct device_driver *d, void *data)
1175{
1176 return i2c_do_add_adapter(to_i2c_driver(d), data);
1177}
1178
David Brownell6e13e642007-05-01 23:26:31 +02001179static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Jean Delvared6703282010-08-11 18:20:59 +02001181 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
David Brownell1d0b19c2008-10-14 17:30:05 +02001183 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001184 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1185 res = -EAGAIN;
1186 goto out_list;
1187 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001188
Jean Delvare2236baa2010-11-15 22:40:38 +01001189 /* Sanity checks */
1190 if (unlikely(adap->name[0] == '\0')) {
1191 pr_err("i2c-core: Attempt to register an adapter with "
1192 "no name!\n");
1193 return -EINVAL;
1194 }
1195 if (unlikely(!adap->algo)) {
1196 pr_err("i2c-core: Attempt to register adapter '%s' with "
1197 "no algo!\n", adap->name);
1198 return -EINVAL;
1199 }
1200
Mika Kuoppala194684e2009-12-06 17:06:22 +01001201 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001202 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001203 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Jean Delvare8fcfef62009-03-28 21:34:43 +01001205 /* Set default timeout to 1 second if not already set */
1206 if (adap->timeout == 0)
1207 adap->timeout = HZ;
1208
Kay Sievers27d9c182009-01-07 14:29:16 +01001209 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001210 adap->dev.bus = &i2c_bus_type;
1211 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001212 res = device_register(&adap->dev);
1213 if (res)
1214 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001216 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1217
Jean Delvare2bb50952009-09-18 22:45:46 +02001218#ifdef CONFIG_I2C_COMPAT
1219 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1220 adap->dev.parent);
1221 if (res)
1222 dev_warn(&adap->dev,
1223 "Failed to create compatibility class link\n");
1224#endif
1225
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301226 /* bus recovery specific initialization */
1227 if (adap->bus_recovery_info) {
1228 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1229
1230 if (!bri->recover_bus) {
1231 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1232 adap->bus_recovery_info = NULL;
1233 goto exit_recovery;
1234 }
1235
1236 /* Generic GPIO recovery */
1237 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1238 if (!gpio_is_valid(bri->scl_gpio)) {
1239 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1240 adap->bus_recovery_info = NULL;
1241 goto exit_recovery;
1242 }
1243
1244 if (gpio_is_valid(bri->sda_gpio))
1245 bri->get_sda = get_sda_gpio_value;
1246 else
1247 bri->get_sda = NULL;
1248
1249 bri->get_scl = get_scl_gpio_value;
1250 bri->set_scl = set_scl_gpio_value;
1251 } else if (!bri->set_scl || !bri->get_scl) {
1252 /* Generic SCL recovery */
1253 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1254 adap->bus_recovery_info = NULL;
1255 }
1256 }
1257
1258exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001259 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001260 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001261 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001262
David Brownell6e13e642007-05-01 23:26:31 +02001263 if (adap->nr < __i2c_first_dynamic_bus_num)
1264 i2c_scan_static_board_info(adap);
1265
Jean Delvare4735c982008-07-14 22:38:36 +02001266 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001267 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001268 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001269 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001270
1271 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001272
Jean Delvareb119c6c2006-08-15 18:26:30 +02001273out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001274 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001275 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001276 mutex_unlock(&core_lock);
1277 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
David Brownell6e13e642007-05-01 23:26:31 +02001280/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001281 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1282 * @adap: the adapter to register (with adap->nr initialized)
1283 * Context: can sleep
1284 *
1285 * See i2c_add_numbered_adapter() for details.
1286 */
1287static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1288{
1289 int id;
1290
1291 mutex_lock(&core_lock);
1292 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1293 GFP_KERNEL);
1294 mutex_unlock(&core_lock);
1295 if (id < 0)
1296 return id == -ENOSPC ? -EBUSY : id;
1297
1298 return i2c_register_adapter(adap);
1299}
1300
1301/**
David Brownell6e13e642007-05-01 23:26:31 +02001302 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1303 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001304 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001305 *
1306 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001307 * doesn't matter or when its bus number is specified by an dt alias.
1308 * Examples of bases when the bus number doesn't matter: I2C adapters
1309 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001310 *
1311 * When this returns zero, a new bus number was allocated and stored
1312 * in adap->nr, and the specified adapter became available for clients.
1313 * Otherwise, a negative errno value is returned.
1314 */
1315int i2c_add_adapter(struct i2c_adapter *adapter)
1316{
Doug Andersonee5c2742013-03-01 08:57:31 -08001317 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001318 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001319
Doug Andersonee5c2742013-03-01 08:57:31 -08001320 if (dev->of_node) {
1321 id = of_alias_get_id(dev->of_node, "i2c");
1322 if (id >= 0) {
1323 adapter->nr = id;
1324 return __i2c_add_numbered_adapter(adapter);
1325 }
1326 }
1327
Jean Delvarecaada322008-01-27 18:14:49 +01001328 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001329 id = idr_alloc(&i2c_adapter_idr, adapter,
1330 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001331 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001332 if (id < 0)
1333 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001334
1335 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001336
David Brownell6e13e642007-05-01 23:26:31 +02001337 return i2c_register_adapter(adapter);
1338}
1339EXPORT_SYMBOL(i2c_add_adapter);
1340
1341/**
1342 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1343 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001344 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001345 *
1346 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001347 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1348 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001349 * is used to properly configure I2C devices.
1350 *
Grant Likely488bf312011-07-25 17:49:43 +02001351 * If the requested bus number is set to -1, then this function will behave
1352 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1353 *
David Brownell6e13e642007-05-01 23:26:31 +02001354 * If no devices have pre-been declared for this bus, then be sure to
1355 * register the adapter before any dynamically allocated ones. Otherwise
1356 * the required bus ID may not be available.
1357 *
1358 * When this returns zero, the specified adapter became available for
1359 * clients using the bus number provided in adap->nr. Also, the table
1360 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1361 * and the appropriate driver model device nodes are created. Otherwise, a
1362 * negative errno value is returned.
1363 */
1364int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1365{
Grant Likely488bf312011-07-25 17:49:43 +02001366 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1367 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001368
Doug Andersonee5c2742013-03-01 08:57:31 -08001369 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001370}
1371EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1372
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001373static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001374 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001375{
Jean Delvare4735c982008-07-14 22:38:36 +02001376 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001377
Jean Delvareacec2112009-03-28 21:34:40 +01001378 /* Remove the devices we created ourselves as the result of hardware
1379 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001380 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1381 if (client->adapter == adapter) {
1382 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1383 client->name, client->addr);
1384 list_del(&client->detected);
1385 i2c_unregister_device(client);
1386 }
1387 }
Jean Delvare026526f2008-01-27 18:14:49 +01001388}
1389
Jean Delvaree549c2b2009-06-19 16:58:19 +02001390static int __unregister_client(struct device *dev, void *dummy)
1391{
1392 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001393 if (client && strcmp(client->name, "dummy"))
1394 i2c_unregister_device(client);
1395 return 0;
1396}
1397
1398static int __unregister_dummy(struct device *dev, void *dummy)
1399{
1400 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001401 if (client)
1402 i2c_unregister_device(client);
1403 return 0;
1404}
1405
Jean Delvare69b00892009-12-06 17:06:27 +01001406static int __process_removed_adapter(struct device_driver *d, void *data)
1407{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001408 i2c_do_del_adapter(to_i2c_driver(d), data);
1409 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001410}
1411
David Brownelld64f73b2007-07-12 14:12:28 +02001412/**
1413 * i2c_del_adapter - unregister I2C adapter
1414 * @adap: the adapter being unregistered
1415 * Context: can sleep
1416 *
1417 * This unregisters an I2C adapter which was previously registered
1418 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1419 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001420void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001422 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001423 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001426 mutex_lock(&core_lock);
1427 found = idr_find(&i2c_adapter_idr, adap->nr);
1428 mutex_unlock(&core_lock);
1429 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001430 pr_debug("i2c-core: attempting to delete unregistered "
1431 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001432 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
Jean Delvare026526f2008-01-27 18:14:49 +01001435 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001436 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001437 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001438 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001439 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001441 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001442 mutex_lock_nested(&adap->userspace_clients_lock,
1443 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001444 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1445 detected) {
1446 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1447 client->addr);
1448 list_del(&client->detected);
1449 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001450 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001451 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001452
Jean Delvaree549c2b2009-06-19 16:58:19 +02001453 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001454 * check the returned value. This is a two-pass process, because
1455 * we can't remove the dummy devices during the first pass: they
1456 * could have been instantiated by real devices wishing to clean
1457 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001458 device_for_each_child(&adap->dev, NULL, __unregister_client);
1459 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Jean Delvare2bb50952009-09-18 22:45:46 +02001461#ifdef CONFIG_I2C_COMPAT
1462 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1463 adap->dev.parent);
1464#endif
1465
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001466 /* device name is gone after device_unregister */
1467 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* clean up the sysfs representation */
1470 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* wait for sysfs to drop all references */
1474 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
David Brownell6e13e642007-05-01 23:26:31 +02001476 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001477 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001479 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001481 /* Clear the device structure in case this adapter is ever going to be
1482 added again */
1483 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
David Brownellc0564602007-05-01 23:26:31 +02001485EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
David Brownell7b4fbc52007-05-01 23:26:30 +02001487/* ------------------------------------------------------------------------- */
1488
Jean Delvare7ae31482011-03-20 14:50:52 +01001489int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1490{
1491 int res;
1492
1493 mutex_lock(&core_lock);
1494 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1495 mutex_unlock(&core_lock);
1496
1497 return res;
1498}
1499EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1500
Jean Delvare69b00892009-12-06 17:06:27 +01001501static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001502{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001503 if (dev->type != &i2c_adapter_type)
1504 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001505 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001506}
1507
David Brownell7b4fbc52007-05-01 23:26:30 +02001508/*
1509 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001510 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
1512
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001513int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001515 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
David Brownell1d0b19c2008-10-14 17:30:05 +02001517 /* Can't register until after driver model init */
1518 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1519 return -EAGAIN;
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001522 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Jean Delvare729d6dd2009-06-19 16:58:18 +02001525 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001526 * will have called probe() for all matching-but-unbound devices.
1527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 res = driver_register(&driver->driver);
1529 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001530 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001531
Mark Brownf4e8db32011-01-14 22:03:50 +01001532 /* Drivers should switch to dev_pm_ops instead. */
1533 if (driver->suspend)
1534 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1535 driver->driver.name);
1536 if (driver->resume)
1537 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1538 driver->driver.name);
1539
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001540 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Jean Delvare4735c982008-07-14 22:38:36 +02001542 INIT_LIST_HEAD(&driver->clients);
1543 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001544 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001545
Jean Delvare7eebcb72006-02-05 23:28:21 +01001546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001548EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Jean Delvare69b00892009-12-06 17:06:27 +01001550static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001551{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001552 if (dev->type == &i2c_adapter_type)
1553 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1554 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001555}
1556
David Brownella1d9e6e2007-05-01 23:26:30 +02001557/**
1558 * i2c_del_driver - unregister I2C driver
1559 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001560 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001561 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001562void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Jean Delvare7ae31482011-03-20 14:50:52 +01001564 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001567 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
David Brownellc0564602007-05-01 23:26:31 +02001569EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
David Brownell7b4fbc52007-05-01 23:26:30 +02001571/* ------------------------------------------------------------------------- */
1572
Jean Delvaree48d3312008-01-27 18:14:48 +01001573/**
1574 * i2c_use_client - increments the reference count of the i2c client structure
1575 * @client: the client being referenced
1576 *
1577 * Each live reference to a client should be refcounted. The driver model does
1578 * that automatically as part of driver binding, so that most drivers don't
1579 * need to do this explicitly: they hold a reference until they're unbound
1580 * from the device.
1581 *
1582 * A pointer to the client with the incremented reference counter is returned.
1583 */
1584struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
David Brownell6ea438e2008-07-14 22:38:24 +02001586 if (client && get_device(&client->dev))
1587 return client;
1588 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
David Brownellc0564602007-05-01 23:26:31 +02001590EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Jean Delvaree48d3312008-01-27 18:14:48 +01001592/**
1593 * i2c_release_client - release a use of the i2c client structure
1594 * @client: the client being no longer referenced
1595 *
1596 * Must be called when a user of a client is finished with it.
1597 */
1598void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
David Brownell6ea438e2008-07-14 22:38:24 +02001600 if (client)
1601 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602}
David Brownellc0564602007-05-01 23:26:31 +02001603EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
David Brownell9b766b82008-01-27 18:14:51 +01001605struct i2c_cmd_arg {
1606 unsigned cmd;
1607 void *arg;
1608};
1609
1610static int i2c_cmd(struct device *dev, void *_arg)
1611{
1612 struct i2c_client *client = i2c_verify_client(dev);
1613 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001614 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001615
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001616 if (!client || !client->dev.driver)
1617 return 0;
1618
1619 driver = to_i2c_driver(client->dev.driver);
1620 if (driver->command)
1621 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001622 return 0;
1623}
1624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1626{
David Brownell9b766b82008-01-27 18:14:51 +01001627 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
David Brownell9b766b82008-01-27 18:14:51 +01001629 cmd_arg.cmd = cmd;
1630 cmd_arg.arg = arg;
1631 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
David Brownellc0564602007-05-01 23:26:31 +02001633EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635static int __init i2c_init(void)
1636{
1637 int retval;
1638
1639 retval = bus_register(&i2c_bus_type);
1640 if (retval)
1641 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001642#ifdef CONFIG_I2C_COMPAT
1643 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1644 if (!i2c_adapter_compat_class) {
1645 retval = -ENOMEM;
1646 goto bus_err;
1647 }
1648#endif
David Brownelle9f13732008-01-27 18:14:52 +01001649 retval = i2c_add_driver(&dummy_driver);
1650 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001651 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001652 return 0;
1653
Jean Delvare2bb50952009-09-18 22:45:46 +02001654class_err:
1655#ifdef CONFIG_I2C_COMPAT
1656 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001657bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001658#endif
David Brownelle9f13732008-01-27 18:14:52 +01001659 bus_unregister(&i2c_bus_type);
1660 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663static void __exit i2c_exit(void)
1664{
David Brownelle9f13732008-01-27 18:14:52 +01001665 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001666#ifdef CONFIG_I2C_COMPAT
1667 class_compat_unregister(i2c_adapter_compat_class);
1668#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 bus_unregister(&i2c_bus_type);
1670}
1671
David Brownella10f9e72008-10-14 17:30:06 +02001672/* We must initialize early, because some subsystems register i2c drivers
1673 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1674 */
1675postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676module_exit(i2c_exit);
1677
1678/* ----------------------------------------------------
1679 * the functional interface to the i2c busses.
1680 * ----------------------------------------------------
1681 */
1682
David Brownella1cdeda2008-07-14 22:38:24 +02001683/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001684 * __i2c_transfer - unlocked flavor of i2c_transfer
1685 * @adap: Handle to I2C bus
1686 * @msgs: One or more messages to execute before STOP is issued to
1687 * terminate the operation; each message begins with a START.
1688 * @num: Number of messages to be executed.
1689 *
1690 * Returns negative errno, else the number of messages executed.
1691 *
1692 * Adapter lock must be held when calling this function. No debug logging
1693 * takes place. adap->algo->master_xfer existence isn't checked.
1694 */
1695int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1696{
1697 unsigned long orig_jiffies;
1698 int ret, try;
1699
1700 /* Retry automatically on arbitration loss */
1701 orig_jiffies = jiffies;
1702 for (ret = 0, try = 0; try <= adap->retries; try++) {
1703 ret = adap->algo->master_xfer(adap, msgs, num);
1704 if (ret != -EAGAIN)
1705 break;
1706 if (time_after(jiffies, orig_jiffies + adap->timeout))
1707 break;
1708 }
1709
1710 return ret;
1711}
1712EXPORT_SYMBOL(__i2c_transfer);
1713
1714/**
David Brownella1cdeda2008-07-14 22:38:24 +02001715 * i2c_transfer - execute a single or combined I2C message
1716 * @adap: Handle to I2C bus
1717 * @msgs: One or more messages to execute before STOP is issued to
1718 * terminate the operation; each message begins with a START.
1719 * @num: Number of messages to be executed.
1720 *
1721 * Returns negative errno, else the number of messages executed.
1722 *
1723 * Note that there is no requirement that each message be sent to
1724 * the same slave address, although that is the most common model.
1725 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001726int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001728 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
David Brownella1cdeda2008-07-14 22:38:24 +02001730 /* REVISIT the fault reporting model here is weak:
1731 *
1732 * - When we get an error after receiving N bytes from a slave,
1733 * there is no way to report "N".
1734 *
1735 * - When we get a NAK after transmitting N bytes to a slave,
1736 * there is no way to report "N" ... or to let the master
1737 * continue executing the rest of this combined message, if
1738 * that's the appropriate response.
1739 *
1740 * - When for example "num" is two and we successfully complete
1741 * the first message but get an error part way through the
1742 * second, it's unclear whether that should be reported as
1743 * one (discarding status on the second message) or errno
1744 * (discarding status on the first one).
1745 */
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (adap->algo->master_xfer) {
1748#ifdef DEBUG
1749 for (ret = 0; ret < num; ret++) {
1750 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001751 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1752 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1753 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
1755#endif
1756
Mike Rapoportcea443a82008-01-27 18:14:50 +01001757 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001758 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001759 if (!ret)
1760 /* I2C activity is ongoing. */
1761 return -EAGAIN;
1762 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001763 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001764 }
1765
Jean Delvareb37d2a32012-06-29 07:47:19 -03001766 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001767 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 return ret;
1770 } else {
1771 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001772 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774}
David Brownellc0564602007-05-01 23:26:31 +02001775EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
David Brownella1cdeda2008-07-14 22:38:24 +02001777/**
1778 * i2c_master_send - issue a single I2C message in master transmit mode
1779 * @client: Handle to slave device
1780 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001781 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001782 *
1783 * Returns negative errno, or else the number of bytes written.
1784 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001785int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001788 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct i2c_msg msg;
1790
Jean Delvare815f55f2005-05-07 22:58:46 +02001791 msg.addr = client->addr;
1792 msg.flags = client->flags & I2C_M_TEN;
1793 msg.len = count;
1794 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001795
Jean Delvare815f55f2005-05-07 22:58:46 +02001796 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001798 /*
1799 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1800 * transmitted, else error code.
1801 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001802 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
David Brownellc0564602007-05-01 23:26:31 +02001804EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
David Brownella1cdeda2008-07-14 22:38:24 +02001806/**
1807 * i2c_master_recv - issue a single I2C message in master receive mode
1808 * @client: Handle to slave device
1809 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001810 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001811 *
1812 * Returns negative errno, or else the number of bytes read.
1813 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001814int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Farid Hammane7225acf2010-05-21 18:40:58 +02001816 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct i2c_msg msg;
1818 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
Jean Delvare815f55f2005-05-07 22:58:46 +02001820 msg.addr = client->addr;
1821 msg.flags = client->flags & I2C_M_TEN;
1822 msg.flags |= I2C_M_RD;
1823 msg.len = count;
1824 msg.buf = buf;
1825
1826 ret = i2c_transfer(adap, &msg, 1);
1827
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001828 /*
1829 * If everything went ok (i.e. 1 msg received), return #bytes received,
1830 * else error code.
1831 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001832 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
David Brownellc0564602007-05-01 23:26:31 +02001834EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/* ----------------------------------------------------
1837 * the i2c address scanning function
1838 * Will not work for 10-bit addresses!
1839 * ----------------------------------------------------
1840 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001841
Jean Delvare63e4e802010-06-03 11:33:51 +02001842/*
1843 * Legacy default probe function, mostly relevant for SMBus. The default
1844 * probe method is a quick write, but it is known to corrupt the 24RF08
1845 * EEPROMs due to a state machine bug, and could also irreversibly
1846 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1847 * we use a short byte read instead. Also, some bus drivers don't implement
1848 * quick write, so we fallback to a byte read in that case too.
1849 * On x86, there is another special case for FSC hardware monitoring chips,
1850 * which want regular byte reads (address 0x73.) Fortunately, these are the
1851 * only known chips using this I2C address on PC hardware.
1852 * Returns 1 if probe succeeded, 0 if not.
1853 */
1854static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1855{
1856 int err;
1857 union i2c_smbus_data dummy;
1858
1859#ifdef CONFIG_X86
1860 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1861 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1862 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1863 I2C_SMBUS_BYTE_DATA, &dummy);
1864 else
1865#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001866 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1867 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001868 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1869 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001870 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1871 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1872 I2C_SMBUS_BYTE, &dummy);
1873 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001874 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1875 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001876 err = -EOPNOTSUPP;
1877 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001878
1879 return err >= 0;
1880}
1881
Jean Delvareccfbbd02009-12-06 17:06:25 +01001882static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001883 struct i2c_driver *driver)
1884{
1885 struct i2c_board_info info;
1886 struct i2c_adapter *adapter = temp_client->adapter;
1887 int addr = temp_client->addr;
1888 int err;
1889
1890 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001891 err = i2c_check_addr_validity(addr);
1892 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001893 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1894 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001895 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001896 }
1897
1898 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001899 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001900 return 0;
1901
Jean Delvareccfbbd02009-12-06 17:06:25 +01001902 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001903 if (!i2c_default_probe(adapter, addr))
1904 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001905
1906 /* Finally call the custom detection function */
1907 memset(&info, 0, sizeof(struct i2c_board_info));
1908 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001909 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001910 if (err) {
1911 /* -ENODEV is returned if the detection fails. We catch it
1912 here as this isn't an error. */
1913 return err == -ENODEV ? 0 : err;
1914 }
1915
1916 /* Consistency check */
1917 if (info.type[0] == '\0') {
1918 dev_err(&adapter->dev, "%s detection function provided "
1919 "no name for 0x%x\n", driver->driver.name,
1920 addr);
1921 } else {
1922 struct i2c_client *client;
1923
1924 /* Detection succeeded, instantiate the device */
1925 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1926 info.type, info.addr);
1927 client = i2c_new_device(adapter, &info);
1928 if (client)
1929 list_add_tail(&client->detected, &driver->clients);
1930 else
1931 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1932 info.type, info.addr);
1933 }
1934 return 0;
1935}
1936
1937static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1938{
Jean Delvarec3813d62009-12-14 21:17:25 +01001939 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001940 struct i2c_client *temp_client;
1941 int i, err = 0;
1942 int adap_id = i2c_adapter_id(adapter);
1943
Jean Delvarec3813d62009-12-14 21:17:25 +01001944 address_list = driver->address_list;
1945 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001946 return 0;
1947
Jean Delvare51b54ba2010-10-24 18:16:58 +02001948 /* Stop here if the classes do not match */
1949 if (!(adapter->class & driver->class))
1950 return 0;
1951
Jean Delvare4735c982008-07-14 22:38:36 +02001952 /* Set up a temporary client to help detect callback */
1953 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1954 if (!temp_client)
1955 return -ENOMEM;
1956 temp_client->adapter = adapter;
1957
Jean Delvarec3813d62009-12-14 21:17:25 +01001958 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001959 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001960 "addr 0x%02x\n", adap_id, address_list[i]);
1961 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001962 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001963 if (unlikely(err))
1964 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001965 }
1966
Jean Delvare4735c982008-07-14 22:38:36 +02001967 kfree(temp_client);
1968 return err;
1969}
1970
Jean Delvared44f19d2010-08-11 18:20:57 +02001971int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1972{
1973 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1974 I2C_SMBUS_QUICK, NULL) >= 0;
1975}
1976EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1977
Jean Delvare12b5053a2007-05-01 23:26:31 +02001978struct i2c_client *
1979i2c_new_probed_device(struct i2c_adapter *adap,
1980 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001981 unsigned short const *addr_list,
1982 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001983{
1984 int i;
1985
Jean Delvare8031d792010-08-11 18:21:00 +02001986 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001987 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001988
Jean Delvare12b5053a2007-05-01 23:26:31 +02001989 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1990 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001991 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001992 dev_warn(&adap->dev, "Invalid 7-bit address "
1993 "0x%02x\n", addr_list[i]);
1994 continue;
1995 }
1996
1997 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001998 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001999 dev_dbg(&adap->dev, "Address 0x%02x already in "
2000 "use, not probing\n", addr_list[i]);
2001 continue;
2002 }
2003
Jean Delvare63e4e802010-06-03 11:33:51 +02002004 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002005 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002006 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002007 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002008
2009 if (addr_list[i] == I2C_CLIENT_END) {
2010 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2011 return NULL;
2012 }
2013
2014 info->addr = addr_list[i];
2015 return i2c_new_device(adap, info);
2016}
2017EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2018
Jean Delvared735b342011-03-20 14:50:52 +01002019struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002022
Jean Delvarecaada322008-01-27 18:14:49 +01002023 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002024 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002025 if (adapter && !try_module_get(adapter->owner))
2026 adapter = NULL;
2027
Jean Delvarecaada322008-01-27 18:14:49 +01002028 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002029 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
David Brownellc0564602007-05-01 23:26:31 +02002031EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
2033void i2c_put_adapter(struct i2c_adapter *adap)
2034{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002035 if (adap)
2036 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
David Brownellc0564602007-05-01 23:26:31 +02002038EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040/* The SMBus parts */
2041
David Brownell438d6c22006-12-10 21:21:31 +01002042#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002043static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002046
Farid Hammane7225acf2010-05-21 18:40:58 +02002047 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002048 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 data = data ^ POLY;
2050 data = data << 1;
2051 }
2052 return (u8)(data >> 8);
2053}
2054
Jean Delvare421ef472005-10-26 21:28:55 +02002055/* Incremental CRC8 over count bytes in the array pointed to by p */
2056static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 int i;
2059
Farid Hammane7225acf2010-05-21 18:40:58 +02002060 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002061 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return crc;
2063}
2064
Jean Delvare421ef472005-10-26 21:28:55 +02002065/* Assume a 7-bit address, which is reasonable for SMBus */
2066static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Jean Delvare421ef472005-10-26 21:28:55 +02002068 /* The address will be sent first */
2069 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2070 pec = i2c_smbus_pec(pec, &addr, 1);
2071
2072 /* The data buffer follows */
2073 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Jean Delvare421ef472005-10-26 21:28:55 +02002076/* Used for write only transactions */
2077static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Jean Delvare421ef472005-10-26 21:28:55 +02002079 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2080 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Jean Delvare421ef472005-10-26 21:28:55 +02002083/* Return <0 on CRC error
2084 If there was a write before this read (most cases) we need to take the
2085 partial CRC from the write part into account.
2086 Note that this function does modify the message (we need to decrease the
2087 message length to hide the CRC byte from the caller). */
2088static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Jean Delvare421ef472005-10-26 21:28:55 +02002090 u8 rpec = msg->buf[--msg->len];
2091 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (rpec != cpec) {
2094 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2095 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002096 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 }
David Brownell438d6c22006-12-10 21:21:31 +01002098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
David Brownella1cdeda2008-07-14 22:38:24 +02002101/**
2102 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2103 * @client: Handle to slave device
2104 *
2105 * This executes the SMBus "receive byte" protocol, returning negative errno
2106 * else the byte received from the device.
2107 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002108s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002111 int status;
2112
2113 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2114 I2C_SMBUS_READ, 0,
2115 I2C_SMBUS_BYTE, &data);
2116 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
David Brownellc0564602007-05-01 23:26:31 +02002118EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
David Brownella1cdeda2008-07-14 22:38:24 +02002120/**
2121 * i2c_smbus_write_byte - SMBus "send byte" protocol
2122 * @client: Handle to slave device
2123 * @value: Byte to be sent
2124 *
2125 * This executes the SMBus "send byte" protocol, returning negative errno
2126 * else zero on success.
2127 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002128s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Farid Hammane7225acf2010-05-21 18:40:58 +02002130 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002131 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
David Brownellc0564602007-05-01 23:26:31 +02002133EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
David Brownella1cdeda2008-07-14 22:38:24 +02002135/**
2136 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2137 * @client: Handle to slave device
2138 * @command: Byte interpreted by slave
2139 *
2140 * This executes the SMBus "read byte" protocol, returning negative errno
2141 * else a data byte received from the device.
2142 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002143s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
2145 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002146 int status;
2147
2148 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2149 I2C_SMBUS_READ, command,
2150 I2C_SMBUS_BYTE_DATA, &data);
2151 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
David Brownellc0564602007-05-01 23:26:31 +02002153EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
David Brownella1cdeda2008-07-14 22:38:24 +02002155/**
2156 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2157 * @client: Handle to slave device
2158 * @command: Byte interpreted by slave
2159 * @value: Byte being written
2160 *
2161 * This executes the SMBus "write byte" protocol, returning negative errno
2162 * else zero on success.
2163 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002164s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2165 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 union i2c_smbus_data data;
2168 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002169 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2170 I2C_SMBUS_WRITE, command,
2171 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
David Brownellc0564602007-05-01 23:26:31 +02002173EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
David Brownella1cdeda2008-07-14 22:38:24 +02002175/**
2176 * i2c_smbus_read_word_data - SMBus "read word" protocol
2177 * @client: Handle to slave device
2178 * @command: Byte interpreted by slave
2179 *
2180 * This executes the SMBus "read word" protocol, returning negative errno
2181 * else a 16-bit unsigned "word" received from the device.
2182 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002183s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002186 int status;
2187
2188 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2189 I2C_SMBUS_READ, command,
2190 I2C_SMBUS_WORD_DATA, &data);
2191 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
David Brownellc0564602007-05-01 23:26:31 +02002193EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
David Brownella1cdeda2008-07-14 22:38:24 +02002195/**
2196 * i2c_smbus_write_word_data - SMBus "write word" protocol
2197 * @client: Handle to slave device
2198 * @command: Byte interpreted by slave
2199 * @value: 16-bit "word" being written
2200 *
2201 * This executes the SMBus "write word" protocol, returning negative errno
2202 * else zero on success.
2203 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002204s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2205 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 union i2c_smbus_data data;
2208 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002209 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2210 I2C_SMBUS_WRITE, command,
2211 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
David Brownellc0564602007-05-01 23:26:31 +02002213EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
David Brownella64ec072007-10-13 23:56:31 +02002215/**
David Brownella1cdeda2008-07-14 22:38:24 +02002216 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002217 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002218 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002219 * @values: Byte array into which data will be read; big enough to hold
2220 * the data returned by the slave. SMBus allows at most 32 bytes.
2221 *
David Brownella1cdeda2008-07-14 22:38:24 +02002222 * This executes the SMBus "block read" protocol, returning negative errno
2223 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002224 *
2225 * Note that using this function requires that the client's adapter support
2226 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2227 * support this; its emulation through I2C messaging relies on a specific
2228 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2229 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002230s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002231 u8 *values)
2232{
2233 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002234 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002235
David Brownell24a5bb72008-07-14 22:38:23 +02002236 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2237 I2C_SMBUS_READ, command,
2238 I2C_SMBUS_BLOCK_DATA, &data);
2239 if (status)
2240 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002241
2242 memcpy(values, &data.block[1], data.block[0]);
2243 return data.block[0];
2244}
2245EXPORT_SYMBOL(i2c_smbus_read_block_data);
2246
David Brownella1cdeda2008-07-14 22:38:24 +02002247/**
2248 * i2c_smbus_write_block_data - SMBus "block write" protocol
2249 * @client: Handle to slave device
2250 * @command: Byte interpreted by slave
2251 * @length: Size of data block; SMBus allows at most 32 bytes
2252 * @values: Byte array which will be written.
2253 *
2254 * This executes the SMBus "block write" protocol, returning negative errno
2255 * else zero on success.
2256 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002257s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002258 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
2260 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (length > I2C_SMBUS_BLOCK_MAX)
2263 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002265 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002266 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2267 I2C_SMBUS_WRITE, command,
2268 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
David Brownellc0564602007-05-01 23:26:31 +02002270EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002273s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002274 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002277 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002278
Jean Delvare4b2643d2007-07-12 14:12:29 +02002279 if (length > I2C_SMBUS_BLOCK_MAX)
2280 length = I2C_SMBUS_BLOCK_MAX;
2281 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002282 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2283 I2C_SMBUS_READ, command,
2284 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2285 if (status < 0)
2286 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002287
2288 memcpy(values, &data.block[1], data.block[0]);
2289 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
David Brownellc0564602007-05-01 23:26:31 +02002291EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jean Delvare0cc43a12011-01-10 22:11:23 +01002293s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002294 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002295{
2296 union i2c_smbus_data data;
2297
2298 if (length > I2C_SMBUS_BLOCK_MAX)
2299 length = I2C_SMBUS_BLOCK_MAX;
2300 data.block[0] = length;
2301 memcpy(data.block + 1, values, length);
2302 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2303 I2C_SMBUS_WRITE, command,
2304 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2305}
David Brownellc0564602007-05-01 23:26:31 +02002306EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002307
David Brownell438d6c22006-12-10 21:21:31 +01002308/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002310static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2311 unsigned short flags,
2312 char read_write, u8 command, int size,
2313 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
2315 /* So we need to generate a series of msgs. In the case of writing, we
2316 need to use only one message; when reading, we need two. We initialize
2317 most things with sane defaults, to keep the code below somewhat
2318 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002319 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2320 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002321 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002323 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002324 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002325 struct i2c_msg msg[2] = {
2326 {
2327 .addr = addr,
2328 .flags = flags,
2329 .len = 1,
2330 .buf = msgbuf0,
2331 }, {
2332 .addr = addr,
2333 .flags = flags | I2C_M_RD,
2334 .len = 0,
2335 .buf = msgbuf1,
2336 },
2337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
2339 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002340 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 case I2C_SMBUS_QUICK:
2342 msg[0].len = 0;
2343 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002344 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2345 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 num = 1;
2347 break;
2348 case I2C_SMBUS_BYTE:
2349 if (read_write == I2C_SMBUS_READ) {
2350 /* Special case: only a read! */
2351 msg[0].flags = I2C_M_RD | flags;
2352 num = 1;
2353 }
2354 break;
2355 case I2C_SMBUS_BYTE_DATA:
2356 if (read_write == I2C_SMBUS_READ)
2357 msg[1].len = 1;
2358 else {
2359 msg[0].len = 2;
2360 msgbuf0[1] = data->byte;
2361 }
2362 break;
2363 case I2C_SMBUS_WORD_DATA:
2364 if (read_write == I2C_SMBUS_READ)
2365 msg[1].len = 2;
2366 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002367 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002369 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 break;
2372 case I2C_SMBUS_PROC_CALL:
2373 num = 2; /* Special case */
2374 read_write = I2C_SMBUS_READ;
2375 msg[0].len = 3;
2376 msg[1].len = 2;
2377 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002378 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 break;
2380 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002382 msg[1].flags |= I2C_M_RECV_LEN;
2383 msg[1].len = 1; /* block length will be added by
2384 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
2386 msg[0].len = data->block[0] + 2;
2387 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002388 dev_err(&adapter->dev,
2389 "Invalid block write size %d\n",
2390 data->block[0]);
2391 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002393 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 msgbuf0[i] = data->block[i-1];
2395 }
2396 break;
2397 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002398 num = 2; /* Another special case */
2399 read_write = I2C_SMBUS_READ;
2400 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002401 dev_err(&adapter->dev,
2402 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002403 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002404 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002405 }
2406 msg[0].len = data->block[0] + 2;
2407 for (i = 1; i < msg[0].len; i++)
2408 msgbuf0[i] = data->block[i-1];
2409 msg[1].flags |= I2C_M_RECV_LEN;
2410 msg[1].len = 1; /* block length will be added by
2411 the underlying bus driver */
2412 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 case I2C_SMBUS_I2C_BLOCK_DATA:
2414 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002415 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 } else {
2417 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002418 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002419 dev_err(&adapter->dev,
2420 "Invalid block write size %d\n",
2421 data->block[0]);
2422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
2424 for (i = 1; i <= data->block[0]; i++)
2425 msgbuf0[i] = data->block[i];
2426 }
2427 break;
2428 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002429 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2430 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432
Jean Delvare421ef472005-10-26 21:28:55 +02002433 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2434 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2435 if (i) {
2436 /* Compute PEC if first message is a write */
2437 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002438 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002439 i2c_smbus_add_pec(&msg[0]);
2440 else /* Write followed by read */
2441 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2442 }
2443 /* Ask for PEC if last message is a read */
2444 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002445 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002446 }
2447
David Brownell24a5bb72008-07-14 22:38:23 +02002448 status = i2c_transfer(adapter, msg, num);
2449 if (status < 0)
2450 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Jean Delvare421ef472005-10-26 21:28:55 +02002452 /* Check PEC if last message is a read */
2453 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002454 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2455 if (status < 0)
2456 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002457 }
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002460 switch (size) {
2461 case I2C_SMBUS_BYTE:
2462 data->byte = msgbuf0[0];
2463 break;
2464 case I2C_SMBUS_BYTE_DATA:
2465 data->byte = msgbuf1[0];
2466 break;
2467 case I2C_SMBUS_WORD_DATA:
2468 case I2C_SMBUS_PROC_CALL:
2469 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2470 break;
2471 case I2C_SMBUS_I2C_BLOCK_DATA:
2472 for (i = 0; i < data->block[0]; i++)
2473 data->block[i+1] = msgbuf1[i];
2474 break;
2475 case I2C_SMBUS_BLOCK_DATA:
2476 case I2C_SMBUS_BLOCK_PROC_CALL:
2477 for (i = 0; i < msgbuf1[0] + 1; i++)
2478 data->block[i] = msgbuf1[i];
2479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
2481 return 0;
2482}
2483
David Brownella1cdeda2008-07-14 22:38:24 +02002484/**
2485 * i2c_smbus_xfer - execute SMBus protocol operations
2486 * @adapter: Handle to I2C bus
2487 * @addr: Address of SMBus slave on that bus
2488 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2489 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2490 * @command: Byte interpreted by slave, for protocols which use such bytes
2491 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2492 * @data: Data to be read or written
2493 *
2494 * This executes an SMBus protocol operation, and returns a negative
2495 * errno code else zero on success.
2496 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002497s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002498 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002499 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002501 unsigned long orig_jiffies;
2502 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002505 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002508 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002509
2510 /* Retry automatically on arbitration loss */
2511 orig_jiffies = jiffies;
2512 for (res = 0, try = 0; try <= adapter->retries; try++) {
2513 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2514 read_write, command,
2515 protocol, data);
2516 if (res != -EAGAIN)
2517 break;
2518 if (time_after(jiffies,
2519 orig_jiffies + adapter->timeout))
2520 break;
2521 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002522 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002524 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2525 return res;
2526 /*
2527 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2528 * implement native support for the SMBus operation.
2529 */
2530 }
2531
2532 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2533 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2538MODULE_DESCRIPTION("I2C-Bus main module");
2539MODULE_LICENSE("GPL");