blob: 3cd8f11f1b5ff6ebc25ac12a7267c52b54f506f5 [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
Jean Delvare7c81c602014-01-29 20:40:08 +010024 Jean Delvare <jdelvare@suse.de>
Michael Lawnick08263742010-08-11 18:21:02 +020025 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>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020045#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010046#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010047#include <linux/hardirq.h>
48#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020049#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010050#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010051#include <linux/acpi.h>
David Howellsd9a83d62014-03-06 13:35:59 +000052#include <linux/jump_label.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54
David Brownell9c1600e2007-05-01 23:26:31 +020055#include "i2c-core.h"
56
David Howellsd9a83d62014-03-06 13:35:59 +000057#define CREATE_TRACE_POINTS
58#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jean Delvare6629dcf2010-05-04 11:09:28 +020060/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020061 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000062 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010063static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_IDR(i2c_adapter_idr);
65
Jean Delvare4f8cf822009-09-18 22:45:46 +020066static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
70
71void i2c_transfer_trace_reg(void)
72{
73 static_key_slow_inc(&i2c_trace_msg);
74}
75
76void i2c_transfer_trace_unreg(void)
77{
78 static_key_slow_dec(&i2c_trace_msg);
79}
80
David Brownellf37dd802007-02-13 22:09:00 +010081/* ------------------------------------------------------------------------- */
82
Jean Delvared2653e92008-04-29 23:11:39 +020083static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
84 const struct i2c_client *client)
85{
86 while (id->name[0]) {
87 if (strcmp(client->name, id->name) == 0)
88 return id;
89 id++;
90 }
91 return NULL;
92}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int i2c_device_match(struct device *dev, struct device_driver *drv)
95{
Jean Delvare51298d12009-09-18 22:45:45 +020096 struct i2c_client *client = i2c_verify_client(dev);
97 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020098
Jean Delvare51298d12009-09-18 22:45:45 +020099 if (!client)
100 return 0;
101
Grant Likely959e85f2010-06-08 07:48:19 -0600102 /* Attempt an OF style match */
103 if (of_driver_match_device(dev, drv))
104 return 1;
105
Mika Westerberg907ddf82012-11-23 12:23:40 +0100106 /* Then ACPI style match */
107 if (acpi_driver_match_device(dev, drv))
108 return 1;
109
Jean Delvare51298d12009-09-18 22:45:45 +0200110 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +0200111 /* match on an id table if there is one */
112 if (driver->id_table)
113 return i2c_match_id(driver->id_table, client) != NULL;
114
Jean Delvareeb8a7902008-05-18 20:49:41 +0200115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
David Brownell7b4fbc52007-05-01 23:26:30 +0200118
119/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200121{
122 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800123 int rc;
124
125 rc = acpi_device_uevent_modalias(dev, env);
126 if (rc != -ENODEV)
127 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200128
Jean Delvareeb8a7902008-05-18 20:49:41 +0200129 if (add_uevent_var(env, "MODALIAS=%s%s",
130 I2C_MODULE_PREFIX, client->name))
131 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200132 dev_dbg(dev, "uevent\n");
133 return 0;
134}
135
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530136/* i2c bus recovery routines */
137static int get_scl_gpio_value(struct i2c_adapter *adap)
138{
139 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
140}
141
142static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
143{
144 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
145}
146
147static int get_sda_gpio_value(struct i2c_adapter *adap)
148{
149 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
150}
151
152static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
153{
154 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
155 struct device *dev = &adap->dev;
156 int ret = 0;
157
158 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
159 GPIOF_OUT_INIT_HIGH, "i2c-scl");
160 if (ret) {
161 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
162 return ret;
163 }
164
165 if (bri->get_sda) {
166 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
167 /* work without SDA polling */
168 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
169 bri->sda_gpio);
170 bri->get_sda = NULL;
171 }
172 }
173
174 return ret;
175}
176
177static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
178{
179 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
180
181 if (bri->get_sda)
182 gpio_free(bri->sda_gpio);
183
184 gpio_free(bri->scl_gpio);
185}
186
187/*
188 * We are generating clock pulses. ndelay() determines durating of clk pulses.
189 * We will generate clock with rate 100 KHz and so duration of both clock levels
190 * is: delay in ns = (10^6 / 100) / 2
191 */
192#define RECOVERY_NDELAY 5000
193#define RECOVERY_CLK_CNT 9
194
195static int i2c_generic_recovery(struct i2c_adapter *adap)
196{
197 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
198 int i = 0, val = 1, ret = 0;
199
200 if (bri->prepare_recovery)
201 bri->prepare_recovery(bri);
202
203 /*
204 * By this time SCL is high, as we need to give 9 falling-rising edges
205 */
206 while (i++ < RECOVERY_CLK_CNT * 2) {
207 if (val) {
208 /* Break if SDA is high */
209 if (bri->get_sda && bri->get_sda(adap))
210 break;
211 /* SCL shouldn't be low here */
212 if (!bri->get_scl(adap)) {
213 dev_err(&adap->dev,
214 "SCL is stuck low, exit recovery\n");
215 ret = -EBUSY;
216 break;
217 }
218 }
219
220 val = !val;
221 bri->set_scl(adap, val);
222 ndelay(RECOVERY_NDELAY);
223 }
224
225 if (bri->unprepare_recovery)
226 bri->unprepare_recovery(bri);
227
228 return ret;
229}
230
231int i2c_generic_scl_recovery(struct i2c_adapter *adap)
232{
233 adap->bus_recovery_info->set_scl(adap, 1);
234 return i2c_generic_recovery(adap);
235}
236
237int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
238{
239 int ret;
240
241 ret = i2c_get_gpios_for_recovery(adap);
242 if (ret)
243 return ret;
244
245 ret = i2c_generic_recovery(adap);
246 i2c_put_gpios_for_recovery(adap);
247
248 return ret;
249}
250
251int i2c_recover_bus(struct i2c_adapter *adap)
252{
253 if (!adap->bus_recovery_info)
254 return -EOPNOTSUPP;
255
256 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
257 return adap->bus_recovery_info->recover_bus(adap);
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static int i2c_device_probe(struct device *dev)
261{
Jean Delvare51298d12009-09-18 22:45:45 +0200262 struct i2c_client *client = i2c_verify_client(dev);
263 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100264 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200265
Jean Delvare51298d12009-09-18 22:45:45 +0200266 if (!client)
267 return 0;
268
269 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200270 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200271 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200272
Marc Pignatee354252008-08-28 08:33:22 +0200273 if (!device_can_wakeup(&client->dev))
274 device_init_wakeup(&client->dev,
275 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200276 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200277
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200278 status = of_clk_set_defaults(dev->of_node, false);
279 if (status < 0)
280 return status;
281
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200282 status = dev_pm_domain_attach(&client->dev, true);
283 if (status != -EPROBE_DEFER) {
284 status = driver->probe(client, i2c_match_id(driver->id_table,
285 client));
286 if (status)
287 dev_pm_domain_detach(&client->dev, true);
288 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100289
Hans Verkuil50c33042008-03-12 14:15:00 +0100290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static int i2c_device_remove(struct device *dev)
294{
Jean Delvare51298d12009-09-18 22:45:45 +0200295 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200296 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100297 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200298
Jean Delvare51298d12009-09-18 22:45:45 +0200299 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200300 return 0;
301
302 driver = to_i2c_driver(dev->driver);
303 if (driver->remove) {
304 dev_dbg(dev, "remove\n");
305 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200306 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100307
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200308 dev_pm_domain_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
David Brownellf37dd802007-02-13 22:09:00 +0100312static void i2c_device_shutdown(struct device *dev)
313{
Jean Delvare51298d12009-09-18 22:45:45 +0200314 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100315 struct i2c_driver *driver;
316
Jean Delvare51298d12009-09-18 22:45:45 +0200317 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100318 return;
319 driver = to_i2c_driver(dev->driver);
320 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200321 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100322}
323
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200324#ifdef CONFIG_PM_SLEEP
325static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100326{
Jean Delvare51298d12009-09-18 22:45:45 +0200327 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100328 struct i2c_driver *driver;
329
Jean Delvare51298d12009-09-18 22:45:45 +0200330 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100331 return 0;
332 driver = to_i2c_driver(dev->driver);
333 if (!driver->suspend)
334 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200335 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100336}
337
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200338static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100339{
Jean Delvare51298d12009-09-18 22:45:45 +0200340 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100341 struct i2c_driver *driver;
342
Jean Delvare51298d12009-09-18 22:45:45 +0200343 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100344 return 0;
345 driver = to_i2c_driver(dev->driver);
346 if (!driver->resume)
347 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200348 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100349}
350
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200351static int i2c_device_pm_suspend(struct device *dev)
352{
353 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
354
Mark Brownd529de22011-01-14 22:03:49 +0100355 if (pm)
356 return pm_generic_suspend(dev);
357 else
358 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200359}
360
361static int i2c_device_pm_resume(struct device *dev)
362{
363 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200364
365 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100366 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200367 else
Mark Brownd529de22011-01-14 22:03:49 +0100368 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200369}
370
371static int i2c_device_pm_freeze(struct device *dev)
372{
373 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
374
Mark Brownd529de22011-01-14 22:03:49 +0100375 if (pm)
376 return pm_generic_freeze(dev);
377 else
378 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200379}
380
381static int i2c_device_pm_thaw(struct device *dev)
382{
383 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
384
Mark Brownd529de22011-01-14 22:03:49 +0100385 if (pm)
386 return pm_generic_thaw(dev);
387 else
388 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200389}
390
391static int i2c_device_pm_poweroff(struct device *dev)
392{
393 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
394
Mark Brownd529de22011-01-14 22:03:49 +0100395 if (pm)
396 return pm_generic_poweroff(dev);
397 else
398 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200399}
400
401static int i2c_device_pm_restore(struct device *dev)
402{
403 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200404
405 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100406 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200407 else
Mark Brownd529de22011-01-14 22:03:49 +0100408 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200409}
410#else /* !CONFIG_PM_SLEEP */
411#define i2c_device_pm_suspend NULL
412#define i2c_device_pm_resume NULL
413#define i2c_device_pm_freeze NULL
414#define i2c_device_pm_thaw NULL
415#define i2c_device_pm_poweroff NULL
416#define i2c_device_pm_restore NULL
417#endif /* !CONFIG_PM_SLEEP */
418
David Brownell9c1600e2007-05-01 23:26:31 +0200419static void i2c_client_dev_release(struct device *dev)
420{
421 kfree(to_i2c_client(dev));
422}
423
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100424static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200425show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200426{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200427 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
428 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200429}
430
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100431static ssize_t
432show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200433{
434 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800435 int len;
436
437 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
438 if (len != -ENODEV)
439 return len;
440
Jean Delvareeb8a7902008-05-18 20:49:41 +0200441 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200442}
443
Jean Delvare4f8cf822009-09-18 22:45:46 +0200444static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200445static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
446
447static struct attribute *i2c_dev_attrs[] = {
448 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200449 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200450 &dev_attr_modalias.attr,
451 NULL
452};
453
454static struct attribute_group i2c_dev_attr_group = {
455 .attrs = i2c_dev_attrs,
456};
457
458static const struct attribute_group *i2c_dev_attr_groups[] = {
459 &i2c_dev_attr_group,
460 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200461};
462
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100463static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100464 .suspend = i2c_device_pm_suspend,
465 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200466 .freeze = i2c_device_pm_freeze,
467 .thaw = i2c_device_pm_thaw,
468 .poweroff = i2c_device_pm_poweroff,
469 .restore = i2c_device_pm_restore,
470 SET_RUNTIME_PM_OPS(
471 pm_generic_runtime_suspend,
472 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200473 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200474 )
sonic zhang54067ee2009-12-14 21:17:30 +0100475};
476
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200477struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100478 .name = "i2c",
479 .match = i2c_device_match,
480 .probe = i2c_device_probe,
481 .remove = i2c_device_remove,
482 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100483 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000484};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200485EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000486
Jean Delvare51298d12009-09-18 22:45:45 +0200487static struct device_type i2c_client_type = {
488 .groups = i2c_dev_attr_groups,
489 .uevent = i2c_device_uevent,
490 .release = i2c_client_dev_release,
491};
492
David Brownell9b766b82008-01-27 18:14:51 +0100493
494/**
495 * i2c_verify_client - return parameter as i2c_client, or NULL
496 * @dev: device, probably from some driver model iterator
497 *
498 * When traversing the driver model tree, perhaps using driver model
499 * iterators like @device_for_each_child(), you can't assume very much
500 * about the nodes you find. Use this function to avoid oopses caused
501 * by wrongly treating some non-I2C device as an i2c_client.
502 */
503struct i2c_client *i2c_verify_client(struct device *dev)
504{
Jean Delvare51298d12009-09-18 22:45:45 +0200505 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100506 ? to_i2c_client(dev)
507 : NULL;
508}
509EXPORT_SYMBOL(i2c_verify_client);
510
511
Jean Delvare3a89db52010-06-03 11:33:52 +0200512/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300513 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200514static int i2c_check_client_addr_validity(const struct i2c_client *client)
515{
516 if (client->flags & I2C_CLIENT_TEN) {
517 /* 10-bit address, all values are valid */
518 if (client->addr > 0x3ff)
519 return -EINVAL;
520 } else {
521 /* 7-bit address, reject the general call address */
522 if (client->addr == 0x00 || client->addr > 0x7f)
523 return -EINVAL;
524 }
525 return 0;
526}
527
Jean Delvare656b8762010-06-03 11:33:53 +0200528/* And this is a strict address validity check, used when probing. If a
529 * device uses a reserved address, then it shouldn't be probed. 7-bit
530 * addressing is assumed, 10-bit address devices are rare and should be
531 * explicitly enumerated. */
532static int i2c_check_addr_validity(unsigned short addr)
533{
534 /*
535 * Reserved addresses per I2C specification:
536 * 0x00 General call address / START byte
537 * 0x01 CBUS address
538 * 0x02 Reserved for different bus format
539 * 0x03 Reserved for future purposes
540 * 0x04-0x07 Hs-mode master code
541 * 0x78-0x7b 10-bit slave addressing
542 * 0x7c-0x7f Reserved for future purposes
543 */
544 if (addr < 0x08 || addr > 0x77)
545 return -EINVAL;
546 return 0;
547}
548
Jean Delvare3b5f7942010-06-03 11:33:55 +0200549static int __i2c_check_addr_busy(struct device *dev, void *addrp)
550{
551 struct i2c_client *client = i2c_verify_client(dev);
552 int addr = *(int *)addrp;
553
554 if (client && client->addr == addr)
555 return -EBUSY;
556 return 0;
557}
558
Michael Lawnick08263742010-08-11 18:21:02 +0200559/* walk up mux tree */
560static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
561{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200562 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200563 int result;
564
565 result = device_for_each_child(&adapter->dev, &addr,
566 __i2c_check_addr_busy);
567
Jean Delvare97cc4d42010-10-24 18:16:57 +0200568 if (!result && parent)
569 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200570
571 return result;
572}
573
574/* recurse down mux tree */
575static int i2c_check_mux_children(struct device *dev, void *addrp)
576{
577 int result;
578
579 if (dev->type == &i2c_adapter_type)
580 result = device_for_each_child(dev, addrp,
581 i2c_check_mux_children);
582 else
583 result = __i2c_check_addr_busy(dev, addrp);
584
585 return result;
586}
587
Jean Delvare3b5f7942010-06-03 11:33:55 +0200588static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
589{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200590 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200591 int result = 0;
592
Jean Delvare97cc4d42010-10-24 18:16:57 +0200593 if (parent)
594 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200595
596 if (!result)
597 result = device_for_each_child(&adapter->dev, &addr,
598 i2c_check_mux_children);
599
600 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200601}
602
David Brownell9c1600e2007-05-01 23:26:31 +0200603/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200604 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
605 * @adapter: Target I2C bus segment
606 */
607void i2c_lock_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_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200613 else
614 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200615}
616EXPORT_SYMBOL_GPL(i2c_lock_adapter);
617
618/**
619 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
620 * @adapter: Target I2C bus segment
621 */
622static int i2c_trylock_adapter(struct i2c_adapter *adapter)
623{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200624 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
625
626 if (parent)
627 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200628 else
629 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200630}
631
632/**
633 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
634 * @adapter: Target I2C bus segment
635 */
636void i2c_unlock_adapter(struct i2c_adapter *adapter)
637{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200638 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
639
640 if (parent)
641 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200642 else
643 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200644}
645EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
646
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200647static void i2c_dev_set_name(struct i2c_adapter *adap,
648 struct i2c_client *client)
649{
650 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
651
652 if (adev) {
653 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
654 return;
655 }
656
657 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
658 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
659 client->addr | ((client->flags & I2C_CLIENT_TEN)
660 ? 0xa000 : 0));
661}
662
Jean Delvarefe61e072010-08-11 18:20:58 +0200663/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200664 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200665 * @adap: the adapter managing the device
666 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200667 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200668 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200669 * Create an i2c device. Binding is handled through driver model
670 * probe()/remove() methods. A driver may be bound to this device when we
671 * return from this function, or any later moment (e.g. maybe hotplugging will
672 * load the driver module). This call is not appropriate for use by mainboard
673 * initialization logic, which usually runs during an arch_initcall() long
674 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200675 *
676 * This returns the new i2c client, which may be saved for later use with
677 * i2c_unregister_device(); or NULL to indicate an error.
678 */
679struct i2c_client *
680i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
681{
682 struct i2c_client *client;
683 int status;
684
685 client = kzalloc(sizeof *client, GFP_KERNEL);
686 if (!client)
687 return NULL;
688
689 client->adapter = adap;
690
691 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200692
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200693 if (info->archdata)
694 client->dev.archdata = *info->archdata;
695
Marc Pignatee354252008-08-28 08:33:22 +0200696 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200697 client->addr = info->addr;
698 client->irq = info->irq;
699
David Brownell9c1600e2007-05-01 23:26:31 +0200700 strlcpy(client->name, info->type, sizeof(client->name));
701
Jean Delvare3a89db52010-06-03 11:33:52 +0200702 /* Check for address validity */
703 status = i2c_check_client_addr_validity(client);
704 if (status) {
705 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
706 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
707 goto out_err_silent;
708 }
709
Jean Delvaref8a227e2009-06-19 16:58:18 +0200710 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200711 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200712 if (status)
713 goto out_err;
714
715 client->dev.parent = &client->adapter->dev;
716 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200717 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700718 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100719 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200720
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200721 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200722 status = device_register(&client->dev);
723 if (status)
724 goto out_err;
725
Jean Delvaref8a227e2009-06-19 16:58:18 +0200726 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
727 client->name, dev_name(&client->dev));
728
David Brownell9c1600e2007-05-01 23:26:31 +0200729 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200730
731out_err:
732 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
733 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200734out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200735 kfree(client);
736 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200737}
738EXPORT_SYMBOL_GPL(i2c_new_device);
739
740
741/**
742 * i2c_unregister_device - reverse effect of i2c_new_device()
743 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200744 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200745 */
746void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200747{
David Brownella1d9e6e2007-05-01 23:26:30 +0200748 device_unregister(&client->dev);
749}
David Brownell9c1600e2007-05-01 23:26:31 +0200750EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200751
752
Jean Delvare60b129d2008-05-11 20:37:06 +0200753static const struct i2c_device_id dummy_id[] = {
754 { "dummy", 0 },
755 { },
756};
757
Jean Delvared2653e92008-04-29 23:11:39 +0200758static int dummy_probe(struct i2c_client *client,
759 const struct i2c_device_id *id)
760{
761 return 0;
762}
763
764static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100765{
766 return 0;
767}
768
769static struct i2c_driver dummy_driver = {
770 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200771 .probe = dummy_probe,
772 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200773 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100774};
775
776/**
777 * i2c_new_dummy - return a new i2c device bound to a dummy driver
778 * @adapter: the adapter managing the device
779 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100780 * Context: can sleep
781 *
782 * This returns an I2C client bound to the "dummy" driver, intended for use
783 * with devices that consume multiple addresses. Examples of such chips
784 * include various EEPROMS (like 24c04 and 24c08 models).
785 *
786 * These dummy devices have two main uses. First, most I2C and SMBus calls
787 * except i2c_transfer() need a client handle; the dummy will be that handle.
788 * And second, this prevents the specified address from being bound to a
789 * different driver.
790 *
791 * This returns the new i2c client, which should be saved for later use with
792 * i2c_unregister_device(); or NULL to indicate an error.
793 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100794struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100795{
796 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200797 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100798 };
799
David Brownelle9f13732008-01-27 18:14:52 +0100800 return i2c_new_device(adapter, &info);
801}
802EXPORT_SYMBOL_GPL(i2c_new_dummy);
803
David Brownellf37dd802007-02-13 22:09:00 +0100804/* ------------------------------------------------------------------------- */
805
David Brownell16ffadf2007-05-01 23:26:28 +0200806/* I2C bus adapters -- one roots each I2C or SMBUS segment */
807
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200808static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
David Brownellef2c83212007-05-01 23:26:28 +0200810 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 complete(&adap->dev_released);
812}
813
Jean Delvare99cd8e22009-06-19 16:58:20 +0200814/*
Jean Delvare390946b2012-09-10 10:14:02 +0200815 * This function is only needed for mutex_lock_nested, so it is never
816 * called unless locking correctness checking is enabled. Thus we
817 * make it inline to avoid a compiler warning. That's what gcc ends up
818 * doing anyway.
819 */
820static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
821{
822 unsigned int depth = 0;
823
824 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
825 depth++;
826
827 return depth;
828}
829
830/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200831 * Let users instantiate I2C devices through sysfs. This can be used when
832 * platform initialization code doesn't contain the proper data for
833 * whatever reason. Also useful for drivers that do device detection and
834 * detection fails, either because the device uses an unexpected address,
835 * or this is a compatible device with different ID register values.
836 *
837 * Parameter checking may look overzealous, but we really don't want
838 * the user to provide incorrect parameters.
839 */
840static ssize_t
841i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
842 const char *buf, size_t count)
843{
844 struct i2c_adapter *adap = to_i2c_adapter(dev);
845 struct i2c_board_info info;
846 struct i2c_client *client;
847 char *blank, end;
848 int res;
849
Jean Delvare99cd8e22009-06-19 16:58:20 +0200850 memset(&info, 0, sizeof(struct i2c_board_info));
851
852 blank = strchr(buf, ' ');
853 if (!blank) {
854 dev_err(dev, "%s: Missing parameters\n", "new_device");
855 return -EINVAL;
856 }
857 if (blank - buf > I2C_NAME_SIZE - 1) {
858 dev_err(dev, "%s: Invalid device name\n", "new_device");
859 return -EINVAL;
860 }
861 memcpy(info.type, buf, blank - buf);
862
863 /* Parse remaining parameters, reject extra parameters */
864 res = sscanf(++blank, "%hi%c", &info.addr, &end);
865 if (res < 1) {
866 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
867 return -EINVAL;
868 }
869 if (res > 1 && end != '\n') {
870 dev_err(dev, "%s: Extra parameters\n", "new_device");
871 return -EINVAL;
872 }
873
Jean Delvare99cd8e22009-06-19 16:58:20 +0200874 client = i2c_new_device(adap, &info);
875 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200876 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200877
878 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200879 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200880 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200881 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200882 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
883 info.type, info.addr);
884
885 return count;
886}
887
888/*
889 * And of course let the users delete the devices they instantiated, if
890 * they got it wrong. This interface can only be used to delete devices
891 * instantiated by i2c_sysfs_new_device above. This guarantees that we
892 * don't delete devices to which some kernel code still has references.
893 *
894 * Parameter checking may look overzealous, but we really don't want
895 * the user to delete the wrong device.
896 */
897static ssize_t
898i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
899 const char *buf, size_t count)
900{
901 struct i2c_adapter *adap = to_i2c_adapter(dev);
902 struct i2c_client *client, *next;
903 unsigned short addr;
904 char end;
905 int res;
906
907 /* Parse parameters, reject extra parameters */
908 res = sscanf(buf, "%hi%c", &addr, &end);
909 if (res < 1) {
910 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
911 return -EINVAL;
912 }
913 if (res > 1 && end != '\n') {
914 dev_err(dev, "%s: Extra parameters\n", "delete_device");
915 return -EINVAL;
916 }
917
918 /* Make sure the device was added through sysfs */
919 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200920 mutex_lock_nested(&adap->userspace_clients_lock,
921 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200922 list_for_each_entry_safe(client, next, &adap->userspace_clients,
923 detected) {
924 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200925 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
926 "delete_device", client->name, client->addr);
927
928 list_del(&client->detected);
929 i2c_unregister_device(client);
930 res = count;
931 break;
932 }
933 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200934 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200935
936 if (res < 0)
937 dev_err(dev, "%s: Can't find device in list\n",
938 "delete_device");
939 return res;
940}
941
Jean Delvare4f8cf822009-09-18 22:45:46 +0200942static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200943static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
944 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200945
946static struct attribute *i2c_adapter_attrs[] = {
947 &dev_attr_name.attr,
948 &dev_attr_new_device.attr,
949 &dev_attr_delete_device.attr,
950 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200951};
952
Jean Delvare4f8cf822009-09-18 22:45:46 +0200953static struct attribute_group i2c_adapter_attr_group = {
954 .attrs = i2c_adapter_attrs,
955};
956
957static const struct attribute_group *i2c_adapter_attr_groups[] = {
958 &i2c_adapter_attr_group,
959 NULL
960};
961
Michael Lawnick08263742010-08-11 18:21:02 +0200962struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200963 .groups = i2c_adapter_attr_groups,
964 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200965};
Michael Lawnick08263742010-08-11 18:21:02 +0200966EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Stephen Warren643dd092012-04-17 12:43:33 -0600968/**
969 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
970 * @dev: device, probably from some driver model iterator
971 *
972 * When traversing the driver model tree, perhaps using driver model
973 * iterators like @device_for_each_child(), you can't assume very much
974 * about the nodes you find. Use this function to avoid oopses caused
975 * by wrongly treating some non-I2C device as an i2c_adapter.
976 */
977struct i2c_adapter *i2c_verify_adapter(struct device *dev)
978{
979 return (dev->type == &i2c_adapter_type)
980 ? to_i2c_adapter(dev)
981 : NULL;
982}
983EXPORT_SYMBOL(i2c_verify_adapter);
984
Jean Delvare2bb50952009-09-18 22:45:46 +0200985#ifdef CONFIG_I2C_COMPAT
986static struct class_compat *i2c_adapter_compat_class;
987#endif
988
David Brownell9c1600e2007-05-01 23:26:31 +0200989static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
990{
991 struct i2c_devinfo *devinfo;
992
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200993 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200994 list_for_each_entry(devinfo, &__i2c_board_list, list) {
995 if (devinfo->busnum == adapter->nr
996 && !i2c_new_device(adapter,
997 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100998 dev_err(&adapter->dev,
999 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001000 devinfo->board_info.addr);
1001 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001002 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001003}
1004
Wolfram Sang687b81d2013-07-11 12:56:15 +01001005/* OF support code */
1006
1007#if IS_ENABLED(CONFIG_OF)
1008static void of_i2c_register_devices(struct i2c_adapter *adap)
1009{
1010 void *result;
1011 struct device_node *node;
1012
1013 /* Only register child devices if the adapter has a node pointer set */
1014 if (!adap->dev.of_node)
1015 return;
1016
1017 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1018
1019 for_each_available_child_of_node(adap->dev.of_node, node) {
1020 struct i2c_board_info info = {};
1021 struct dev_archdata dev_ad = {};
1022 const __be32 *addr;
1023 int len;
1024
1025 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1026
1027 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1028 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1029 node->full_name);
1030 continue;
1031 }
1032
1033 addr = of_get_property(node, "reg", &len);
1034 if (!addr || (len < sizeof(int))) {
1035 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1036 node->full_name);
1037 continue;
1038 }
1039
1040 info.addr = be32_to_cpup(addr);
1041 if (info.addr > (1 << 10) - 1) {
1042 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1043 info.addr, node->full_name);
1044 continue;
1045 }
1046
1047 info.irq = irq_of_parse_and_map(node, 0);
1048 info.of_node = of_node_get(node);
1049 info.archdata = &dev_ad;
1050
1051 if (of_get_property(node, "wakeup-source", NULL))
1052 info.flags |= I2C_CLIENT_WAKE;
1053
1054 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1055
1056 result = i2c_new_device(adap, &info);
1057 if (result == NULL) {
1058 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1059 node->full_name);
1060 of_node_put(node);
1061 irq_dispose_mapping(info.irq);
1062 continue;
1063 }
1064 }
1065}
1066
1067static int of_dev_node_match(struct device *dev, void *data)
1068{
1069 return dev->of_node == data;
1070}
1071
1072/* must call put_device() when done with returned i2c_client device */
1073struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1074{
1075 struct device *dev;
1076
1077 dev = bus_find_device(&i2c_bus_type, NULL, node,
1078 of_dev_node_match);
1079 if (!dev)
1080 return NULL;
1081
1082 return i2c_verify_client(dev);
1083}
1084EXPORT_SYMBOL(of_find_i2c_device_by_node);
1085
1086/* must call put_device() when done with returned i2c_adapter device */
1087struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1088{
1089 struct device *dev;
1090
1091 dev = bus_find_device(&i2c_bus_type, NULL, node,
1092 of_dev_node_match);
1093 if (!dev)
1094 return NULL;
1095
1096 return i2c_verify_adapter(dev);
1097}
1098EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1099#else
1100static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1101#endif /* CONFIG_OF */
1102
Jean Delvare69b00892009-12-06 17:06:27 +01001103static int i2c_do_add_adapter(struct i2c_driver *driver,
1104 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001105{
Jean Delvare4735c982008-07-14 22:38:36 +02001106 /* Detect supported devices on that bus, and instantiate them */
1107 i2c_detect(adap, driver);
1108
1109 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001110 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001111 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1112 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001113 dev_warn(&adap->dev, "Please use another way to instantiate "
1114 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001115 /* We ignore the return code; if it fails, too bad */
1116 driver->attach_adapter(adap);
1117 }
1118 return 0;
1119}
1120
Jean Delvare69b00892009-12-06 17:06:27 +01001121static int __process_new_adapter(struct device_driver *d, void *data)
1122{
1123 return i2c_do_add_adapter(to_i2c_driver(d), data);
1124}
1125
David Brownell6e13e642007-05-01 23:26:31 +02001126static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Jean Delvared6703282010-08-11 18:20:59 +02001128 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
David Brownell1d0b19c2008-10-14 17:30:05 +02001130 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001131 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1132 res = -EAGAIN;
1133 goto out_list;
1134 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001135
Jean Delvare2236baa2010-11-15 22:40:38 +01001136 /* Sanity checks */
1137 if (unlikely(adap->name[0] == '\0')) {
1138 pr_err("i2c-core: Attempt to register an adapter with "
1139 "no name!\n");
1140 return -EINVAL;
1141 }
1142 if (unlikely(!adap->algo)) {
1143 pr_err("i2c-core: Attempt to register adapter '%s' with "
1144 "no algo!\n", adap->name);
1145 return -EINVAL;
1146 }
1147
Mika Kuoppala194684e2009-12-06 17:06:22 +01001148 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001149 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001150 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Jean Delvare8fcfef62009-03-28 21:34:43 +01001152 /* Set default timeout to 1 second if not already set */
1153 if (adap->timeout == 0)
1154 adap->timeout = HZ;
1155
Kay Sievers27d9c182009-01-07 14:29:16 +01001156 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001157 adap->dev.bus = &i2c_bus_type;
1158 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001159 res = device_register(&adap->dev);
1160 if (res)
1161 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001163 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1164
Jean Delvare2bb50952009-09-18 22:45:46 +02001165#ifdef CONFIG_I2C_COMPAT
1166 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1167 adap->dev.parent);
1168 if (res)
1169 dev_warn(&adap->dev,
1170 "Failed to create compatibility class link\n");
1171#endif
1172
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301173 /* bus recovery specific initialization */
1174 if (adap->bus_recovery_info) {
1175 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1176
1177 if (!bri->recover_bus) {
1178 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1179 adap->bus_recovery_info = NULL;
1180 goto exit_recovery;
1181 }
1182
1183 /* Generic GPIO recovery */
1184 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1185 if (!gpio_is_valid(bri->scl_gpio)) {
1186 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1187 adap->bus_recovery_info = NULL;
1188 goto exit_recovery;
1189 }
1190
1191 if (gpio_is_valid(bri->sda_gpio))
1192 bri->get_sda = get_sda_gpio_value;
1193 else
1194 bri->get_sda = NULL;
1195
1196 bri->get_scl = get_scl_gpio_value;
1197 bri->set_scl = set_scl_gpio_value;
1198 } else if (!bri->set_scl || !bri->get_scl) {
1199 /* Generic SCL recovery */
1200 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1201 adap->bus_recovery_info = NULL;
1202 }
1203 }
1204
1205exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001206 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001207 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001208 acpi_i2c_register_devices(adap);
Lan Tianyu5d98e612014-05-20 20:59:23 +08001209 acpi_i2c_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001210
David Brownell6e13e642007-05-01 23:26:31 +02001211 if (adap->nr < __i2c_first_dynamic_bus_num)
1212 i2c_scan_static_board_info(adap);
1213
Jean Delvare4735c982008-07-14 22:38:36 +02001214 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001215 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001216 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001217 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001218
1219 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001220
Jean Delvareb119c6c2006-08-15 18:26:30 +02001221out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001222 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001223 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001224 mutex_unlock(&core_lock);
1225 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
David Brownell6e13e642007-05-01 23:26:31 +02001228/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001229 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1230 * @adap: the adapter to register (with adap->nr initialized)
1231 * Context: can sleep
1232 *
1233 * See i2c_add_numbered_adapter() for details.
1234 */
1235static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1236{
1237 int id;
1238
1239 mutex_lock(&core_lock);
1240 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1241 GFP_KERNEL);
1242 mutex_unlock(&core_lock);
1243 if (id < 0)
1244 return id == -ENOSPC ? -EBUSY : id;
1245
1246 return i2c_register_adapter(adap);
1247}
1248
1249/**
David Brownell6e13e642007-05-01 23:26:31 +02001250 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1251 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001252 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001253 *
1254 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001255 * doesn't matter or when its bus number is specified by an dt alias.
1256 * Examples of bases when the bus number doesn't matter: I2C adapters
1257 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001258 *
1259 * When this returns zero, a new bus number was allocated and stored
1260 * in adap->nr, and the specified adapter became available for clients.
1261 * Otherwise, a negative errno value is returned.
1262 */
1263int i2c_add_adapter(struct i2c_adapter *adapter)
1264{
Doug Andersonee5c2742013-03-01 08:57:31 -08001265 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001266 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001267
Doug Andersonee5c2742013-03-01 08:57:31 -08001268 if (dev->of_node) {
1269 id = of_alias_get_id(dev->of_node, "i2c");
1270 if (id >= 0) {
1271 adapter->nr = id;
1272 return __i2c_add_numbered_adapter(adapter);
1273 }
1274 }
1275
Jean Delvarecaada322008-01-27 18:14:49 +01001276 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001277 id = idr_alloc(&i2c_adapter_idr, adapter,
1278 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001279 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001280 if (id < 0)
1281 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001282
1283 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001284
David Brownell6e13e642007-05-01 23:26:31 +02001285 return i2c_register_adapter(adapter);
1286}
1287EXPORT_SYMBOL(i2c_add_adapter);
1288
1289/**
1290 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1291 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001292 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001293 *
1294 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001295 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1296 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001297 * is used to properly configure I2C devices.
1298 *
Grant Likely488bf312011-07-25 17:49:43 +02001299 * If the requested bus number is set to -1, then this function will behave
1300 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1301 *
David Brownell6e13e642007-05-01 23:26:31 +02001302 * If no devices have pre-been declared for this bus, then be sure to
1303 * register the adapter before any dynamically allocated ones. Otherwise
1304 * the required bus ID may not be available.
1305 *
1306 * When this returns zero, the specified adapter became available for
1307 * clients using the bus number provided in adap->nr. Also, the table
1308 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1309 * and the appropriate driver model device nodes are created. Otherwise, a
1310 * negative errno value is returned.
1311 */
1312int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1313{
Grant Likely488bf312011-07-25 17:49:43 +02001314 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1315 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001316
Doug Andersonee5c2742013-03-01 08:57:31 -08001317 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001318}
1319EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1320
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001321static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001322 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001323{
Jean Delvare4735c982008-07-14 22:38:36 +02001324 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001325
Jean Delvareacec2112009-03-28 21:34:40 +01001326 /* Remove the devices we created ourselves as the result of hardware
1327 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001328 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1329 if (client->adapter == adapter) {
1330 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1331 client->name, client->addr);
1332 list_del(&client->detected);
1333 i2c_unregister_device(client);
1334 }
1335 }
Jean Delvare026526f2008-01-27 18:14:49 +01001336}
1337
Jean Delvaree549c2b2009-06-19 16:58:19 +02001338static int __unregister_client(struct device *dev, void *dummy)
1339{
1340 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001341 if (client && strcmp(client->name, "dummy"))
1342 i2c_unregister_device(client);
1343 return 0;
1344}
1345
1346static int __unregister_dummy(struct device *dev, void *dummy)
1347{
1348 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001349 if (client)
1350 i2c_unregister_device(client);
1351 return 0;
1352}
1353
Jean Delvare69b00892009-12-06 17:06:27 +01001354static int __process_removed_adapter(struct device_driver *d, void *data)
1355{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001356 i2c_do_del_adapter(to_i2c_driver(d), data);
1357 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001358}
1359
David Brownelld64f73b2007-07-12 14:12:28 +02001360/**
1361 * i2c_del_adapter - unregister I2C adapter
1362 * @adap: the adapter being unregistered
1363 * Context: can sleep
1364 *
1365 * This unregisters an I2C adapter which was previously registered
1366 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1367 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001368void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001370 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001371 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001374 mutex_lock(&core_lock);
1375 found = idr_find(&i2c_adapter_idr, adap->nr);
1376 mutex_unlock(&core_lock);
1377 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001378 pr_debug("i2c-core: attempting to delete unregistered "
1379 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001380 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
Lan Tianyu5d98e612014-05-20 20:59:23 +08001383 acpi_i2c_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001384 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001385 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001386 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001387 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001388 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001390 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001391 mutex_lock_nested(&adap->userspace_clients_lock,
1392 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001393 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1394 detected) {
1395 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1396 client->addr);
1397 list_del(&client->detected);
1398 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001399 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001400 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001401
Jean Delvaree549c2b2009-06-19 16:58:19 +02001402 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001403 * check the returned value. This is a two-pass process, because
1404 * we can't remove the dummy devices during the first pass: they
1405 * could have been instantiated by real devices wishing to clean
1406 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001407 device_for_each_child(&adap->dev, NULL, __unregister_client);
1408 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Jean Delvare2bb50952009-09-18 22:45:46 +02001410#ifdef CONFIG_I2C_COMPAT
1411 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1412 adap->dev.parent);
1413#endif
1414
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001415 /* device name is gone after device_unregister */
1416 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /* clean up the sysfs representation */
1419 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* wait for sysfs to drop all references */
1423 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
David Brownell6e13e642007-05-01 23:26:31 +02001425 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001426 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001428 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001430 /* Clear the device structure in case this adapter is ever going to be
1431 added again */
1432 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
David Brownellc0564602007-05-01 23:26:31 +02001434EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
David Brownell7b4fbc52007-05-01 23:26:30 +02001436/* ------------------------------------------------------------------------- */
1437
Jean Delvare7ae31482011-03-20 14:50:52 +01001438int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1439{
1440 int res;
1441
1442 mutex_lock(&core_lock);
1443 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1444 mutex_unlock(&core_lock);
1445
1446 return res;
1447}
1448EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1449
Jean Delvare69b00892009-12-06 17:06:27 +01001450static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001451{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001452 if (dev->type != &i2c_adapter_type)
1453 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001454 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001455}
1456
David Brownell7b4fbc52007-05-01 23:26:30 +02001457/*
1458 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001459 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 */
1461
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001462int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001464 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
David Brownell1d0b19c2008-10-14 17:30:05 +02001466 /* Can't register until after driver model init */
1467 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1468 return -EAGAIN;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001471 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Jean Delvare729d6dd2009-06-19 16:58:18 +02001474 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001475 * will have called probe() for all matching-but-unbound devices.
1476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 res = driver_register(&driver->driver);
1478 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001479 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001480
Mark Brownf4e8db32011-01-14 22:03:50 +01001481 /* Drivers should switch to dev_pm_ops instead. */
1482 if (driver->suspend)
1483 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1484 driver->driver.name);
1485 if (driver->resume)
1486 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1487 driver->driver.name);
1488
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001489 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Jean Delvare4735c982008-07-14 22:38:36 +02001491 INIT_LIST_HEAD(&driver->clients);
1492 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001493 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001494
Jean Delvare7eebcb72006-02-05 23:28:21 +01001495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001497EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Jean Delvare69b00892009-12-06 17:06:27 +01001499static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001500{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001501 if (dev->type == &i2c_adapter_type)
1502 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1503 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001504}
1505
David Brownella1d9e6e2007-05-01 23:26:30 +02001506/**
1507 * i2c_del_driver - unregister I2C driver
1508 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001509 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001510 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001511void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Jean Delvare7ae31482011-03-20 14:50:52 +01001513 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001516 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
David Brownellc0564602007-05-01 23:26:31 +02001518EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
David Brownell7b4fbc52007-05-01 23:26:30 +02001520/* ------------------------------------------------------------------------- */
1521
Jean Delvaree48d3312008-01-27 18:14:48 +01001522/**
1523 * i2c_use_client - increments the reference count of the i2c client structure
1524 * @client: the client being referenced
1525 *
1526 * Each live reference to a client should be refcounted. The driver model does
1527 * that automatically as part of driver binding, so that most drivers don't
1528 * need to do this explicitly: they hold a reference until they're unbound
1529 * from the device.
1530 *
1531 * A pointer to the client with the incremented reference counter is returned.
1532 */
1533struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
David Brownell6ea438e2008-07-14 22:38:24 +02001535 if (client && get_device(&client->dev))
1536 return client;
1537 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
David Brownellc0564602007-05-01 23:26:31 +02001539EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jean Delvaree48d3312008-01-27 18:14:48 +01001541/**
1542 * i2c_release_client - release a use of the i2c client structure
1543 * @client: the client being no longer referenced
1544 *
1545 * Must be called when a user of a client is finished with it.
1546 */
1547void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
David Brownell6ea438e2008-07-14 22:38:24 +02001549 if (client)
1550 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
David Brownellc0564602007-05-01 23:26:31 +02001552EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
David Brownell9b766b82008-01-27 18:14:51 +01001554struct i2c_cmd_arg {
1555 unsigned cmd;
1556 void *arg;
1557};
1558
1559static int i2c_cmd(struct device *dev, void *_arg)
1560{
1561 struct i2c_client *client = i2c_verify_client(dev);
1562 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001563 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001564
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001565 if (!client || !client->dev.driver)
1566 return 0;
1567
1568 driver = to_i2c_driver(client->dev.driver);
1569 if (driver->command)
1570 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001571 return 0;
1572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1575{
David Brownell9b766b82008-01-27 18:14:51 +01001576 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
David Brownell9b766b82008-01-27 18:14:51 +01001578 cmd_arg.cmd = cmd;
1579 cmd_arg.arg = arg;
1580 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
David Brownellc0564602007-05-01 23:26:31 +02001582EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584static int __init i2c_init(void)
1585{
1586 int retval;
1587
1588 retval = bus_register(&i2c_bus_type);
1589 if (retval)
1590 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001591#ifdef CONFIG_I2C_COMPAT
1592 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1593 if (!i2c_adapter_compat_class) {
1594 retval = -ENOMEM;
1595 goto bus_err;
1596 }
1597#endif
David Brownelle9f13732008-01-27 18:14:52 +01001598 retval = i2c_add_driver(&dummy_driver);
1599 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001600 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001601 return 0;
1602
Jean Delvare2bb50952009-09-18 22:45:46 +02001603class_err:
1604#ifdef CONFIG_I2C_COMPAT
1605 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001606bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001607#endif
David Brownelle9f13732008-01-27 18:14:52 +01001608 bus_unregister(&i2c_bus_type);
1609 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
1612static void __exit i2c_exit(void)
1613{
David Brownelle9f13732008-01-27 18:14:52 +01001614 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001615#ifdef CONFIG_I2C_COMPAT
1616 class_compat_unregister(i2c_adapter_compat_class);
1617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001619 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
David Brownella10f9e72008-10-14 17:30:06 +02001622/* We must initialize early, because some subsystems register i2c drivers
1623 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1624 */
1625postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626module_exit(i2c_exit);
1627
1628/* ----------------------------------------------------
1629 * the functional interface to the i2c busses.
1630 * ----------------------------------------------------
1631 */
1632
David Brownella1cdeda2008-07-14 22:38:24 +02001633/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001634 * __i2c_transfer - unlocked flavor of i2c_transfer
1635 * @adap: Handle to I2C bus
1636 * @msgs: One or more messages to execute before STOP is issued to
1637 * terminate the operation; each message begins with a START.
1638 * @num: Number of messages to be executed.
1639 *
1640 * Returns negative errno, else the number of messages executed.
1641 *
1642 * Adapter lock must be held when calling this function. No debug logging
1643 * takes place. adap->algo->master_xfer existence isn't checked.
1644 */
1645int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1646{
1647 unsigned long orig_jiffies;
1648 int ret, try;
1649
David Howellsd9a83d62014-03-06 13:35:59 +00001650 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1651 * enabled. This is an efficient way of keeping the for-loop from
1652 * being executed when not needed.
1653 */
1654 if (static_key_false(&i2c_trace_msg)) {
1655 int i;
1656 for (i = 0; i < num; i++)
1657 if (msgs[i].flags & I2C_M_RD)
1658 trace_i2c_read(adap, &msgs[i], i);
1659 else
1660 trace_i2c_write(adap, &msgs[i], i);
1661 }
1662
Jean Delvareb37d2a32012-06-29 07:47:19 -03001663 /* Retry automatically on arbitration loss */
1664 orig_jiffies = jiffies;
1665 for (ret = 0, try = 0; try <= adap->retries; try++) {
1666 ret = adap->algo->master_xfer(adap, msgs, num);
1667 if (ret != -EAGAIN)
1668 break;
1669 if (time_after(jiffies, orig_jiffies + adap->timeout))
1670 break;
1671 }
1672
David Howellsd9a83d62014-03-06 13:35:59 +00001673 if (static_key_false(&i2c_trace_msg)) {
1674 int i;
1675 for (i = 0; i < ret; i++)
1676 if (msgs[i].flags & I2C_M_RD)
1677 trace_i2c_reply(adap, &msgs[i], i);
1678 trace_i2c_result(adap, i, ret);
1679 }
1680
Jean Delvareb37d2a32012-06-29 07:47:19 -03001681 return ret;
1682}
1683EXPORT_SYMBOL(__i2c_transfer);
1684
1685/**
David Brownella1cdeda2008-07-14 22:38:24 +02001686 * i2c_transfer - execute a single or combined I2C message
1687 * @adap: Handle to I2C bus
1688 * @msgs: One or more messages to execute before STOP is issued to
1689 * terminate the operation; each message begins with a START.
1690 * @num: Number of messages to be executed.
1691 *
1692 * Returns negative errno, else the number of messages executed.
1693 *
1694 * Note that there is no requirement that each message be sent to
1695 * the same slave address, although that is the most common model.
1696 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001697int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001699 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
David Brownella1cdeda2008-07-14 22:38:24 +02001701 /* REVISIT the fault reporting model here is weak:
1702 *
1703 * - When we get an error after receiving N bytes from a slave,
1704 * there is no way to report "N".
1705 *
1706 * - When we get a NAK after transmitting N bytes to a slave,
1707 * there is no way to report "N" ... or to let the master
1708 * continue executing the rest of this combined message, if
1709 * that's the appropriate response.
1710 *
1711 * - When for example "num" is two and we successfully complete
1712 * the first message but get an error part way through the
1713 * second, it's unclear whether that should be reported as
1714 * one (discarding status on the second message) or errno
1715 * (discarding status on the first one).
1716 */
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (adap->algo->master_xfer) {
1719#ifdef DEBUG
1720 for (ret = 0; ret < num; ret++) {
1721 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001722 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1723 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1724 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726#endif
1727
Mike Rapoportcea443a82008-01-27 18:14:50 +01001728 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001729 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001730 if (!ret)
1731 /* I2C activity is ongoing. */
1732 return -EAGAIN;
1733 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001734 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001735 }
1736
Jean Delvareb37d2a32012-06-29 07:47:19 -03001737 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001738 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 return ret;
1741 } else {
1742 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001743 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 }
1745}
David Brownellc0564602007-05-01 23:26:31 +02001746EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
David Brownella1cdeda2008-07-14 22:38:24 +02001748/**
1749 * i2c_master_send - issue a single I2C message in master transmit mode
1750 * @client: Handle to slave device
1751 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001752 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001753 *
1754 * Returns negative errno, or else the number of bytes written.
1755 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001756int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757{
1758 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001759 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 struct i2c_msg msg;
1761
Jean Delvare815f55f2005-05-07 22:58:46 +02001762 msg.addr = client->addr;
1763 msg.flags = client->flags & I2C_M_TEN;
1764 msg.len = count;
1765 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001766
Jean Delvare815f55f2005-05-07 22:58:46 +02001767 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001769 /*
1770 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1771 * transmitted, else error code.
1772 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001773 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
David Brownellc0564602007-05-01 23:26:31 +02001775EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
David Brownella1cdeda2008-07-14 22:38:24 +02001777/**
1778 * i2c_master_recv - issue a single I2C message in master receive mode
1779 * @client: Handle to slave device
1780 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001781 * @count: How many bytes to read, 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 read.
1784 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001785int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Farid Hammane7225acf2010-05-21 18:40:58 +02001787 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct i2c_msg msg;
1789 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Jean Delvare815f55f2005-05-07 22:58:46 +02001791 msg.addr = client->addr;
1792 msg.flags = client->flags & I2C_M_TEN;
1793 msg.flags |= I2C_M_RD;
1794 msg.len = count;
1795 msg.buf = buf;
1796
1797 ret = i2c_transfer(adap, &msg, 1);
1798
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001799 /*
1800 * If everything went ok (i.e. 1 msg received), return #bytes received,
1801 * else error code.
1802 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001803 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
David Brownellc0564602007-05-01 23:26:31 +02001805EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/* ----------------------------------------------------
1808 * the i2c address scanning function
1809 * Will not work for 10-bit addresses!
1810 * ----------------------------------------------------
1811 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001812
Jean Delvare63e4e802010-06-03 11:33:51 +02001813/*
1814 * Legacy default probe function, mostly relevant for SMBus. The default
1815 * probe method is a quick write, but it is known to corrupt the 24RF08
1816 * EEPROMs due to a state machine bug, and could also irreversibly
1817 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1818 * we use a short byte read instead. Also, some bus drivers don't implement
1819 * quick write, so we fallback to a byte read in that case too.
1820 * On x86, there is another special case for FSC hardware monitoring chips,
1821 * which want regular byte reads (address 0x73.) Fortunately, these are the
1822 * only known chips using this I2C address on PC hardware.
1823 * Returns 1 if probe succeeded, 0 if not.
1824 */
1825static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1826{
1827 int err;
1828 union i2c_smbus_data dummy;
1829
1830#ifdef CONFIG_X86
1831 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1832 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1833 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1834 I2C_SMBUS_BYTE_DATA, &dummy);
1835 else
1836#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001837 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1838 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001839 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1840 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001841 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1842 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1843 I2C_SMBUS_BYTE, &dummy);
1844 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001845 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1846 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001847 err = -EOPNOTSUPP;
1848 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001849
1850 return err >= 0;
1851}
1852
Jean Delvareccfbbd02009-12-06 17:06:25 +01001853static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001854 struct i2c_driver *driver)
1855{
1856 struct i2c_board_info info;
1857 struct i2c_adapter *adapter = temp_client->adapter;
1858 int addr = temp_client->addr;
1859 int err;
1860
1861 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001862 err = i2c_check_addr_validity(addr);
1863 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001864 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1865 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001866 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001867 }
1868
1869 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001870 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001871 return 0;
1872
Jean Delvareccfbbd02009-12-06 17:06:25 +01001873 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001874 if (!i2c_default_probe(adapter, addr))
1875 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001876
1877 /* Finally call the custom detection function */
1878 memset(&info, 0, sizeof(struct i2c_board_info));
1879 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001880 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001881 if (err) {
1882 /* -ENODEV is returned if the detection fails. We catch it
1883 here as this isn't an error. */
1884 return err == -ENODEV ? 0 : err;
1885 }
1886
1887 /* Consistency check */
1888 if (info.type[0] == '\0') {
1889 dev_err(&adapter->dev, "%s detection function provided "
1890 "no name for 0x%x\n", driver->driver.name,
1891 addr);
1892 } else {
1893 struct i2c_client *client;
1894
1895 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01001896 if (adapter->class & I2C_CLASS_DEPRECATED)
1897 dev_warn(&adapter->dev,
1898 "This adapter will soon drop class based instantiation of devices. "
1899 "Please make sure client 0x%02x gets instantiated by other means. "
1900 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
1901 info.addr);
1902
Jean Delvare4735c982008-07-14 22:38:36 +02001903 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1904 info.type, info.addr);
1905 client = i2c_new_device(adapter, &info);
1906 if (client)
1907 list_add_tail(&client->detected, &driver->clients);
1908 else
1909 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1910 info.type, info.addr);
1911 }
1912 return 0;
1913}
1914
1915static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1916{
Jean Delvarec3813d62009-12-14 21:17:25 +01001917 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001918 struct i2c_client *temp_client;
1919 int i, err = 0;
1920 int adap_id = i2c_adapter_id(adapter);
1921
Jean Delvarec3813d62009-12-14 21:17:25 +01001922 address_list = driver->address_list;
1923 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001924 return 0;
1925
Wolfram Sang45552272014-07-10 13:46:21 +02001926 /* Warn that the adapter lost class based instantiation */
1927 if (adapter->class == I2C_CLASS_DEPRECATED) {
1928 dev_dbg(&adapter->dev,
1929 "This adapter dropped support for I2C classes and "
1930 "won't auto-detect %s devices anymore. If you need it, check "
1931 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
1932 driver->driver.name);
1933 return 0;
1934 }
1935
Jean Delvare51b54ba2010-10-24 18:16:58 +02001936 /* Stop here if the classes do not match */
1937 if (!(adapter->class & driver->class))
1938 return 0;
1939
Jean Delvare4735c982008-07-14 22:38:36 +02001940 /* Set up a temporary client to help detect callback */
1941 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1942 if (!temp_client)
1943 return -ENOMEM;
1944 temp_client->adapter = adapter;
1945
Jean Delvarec3813d62009-12-14 21:17:25 +01001946 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001947 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001948 "addr 0x%02x\n", adap_id, address_list[i]);
1949 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001950 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001951 if (unlikely(err))
1952 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001953 }
1954
Jean Delvare4735c982008-07-14 22:38:36 +02001955 kfree(temp_client);
1956 return err;
1957}
1958
Jean Delvared44f19d2010-08-11 18:20:57 +02001959int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1960{
1961 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1962 I2C_SMBUS_QUICK, NULL) >= 0;
1963}
1964EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1965
Jean Delvare12b5053a2007-05-01 23:26:31 +02001966struct i2c_client *
1967i2c_new_probed_device(struct i2c_adapter *adap,
1968 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001969 unsigned short const *addr_list,
1970 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001971{
1972 int i;
1973
Jean Delvare8031d792010-08-11 18:21:00 +02001974 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001975 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001976
Jean Delvare12b5053a2007-05-01 23:26:31 +02001977 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1978 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02001979 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001980 dev_warn(&adap->dev, "Invalid 7-bit address "
1981 "0x%02x\n", addr_list[i]);
1982 continue;
1983 }
1984
1985 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001986 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02001987 dev_dbg(&adap->dev, "Address 0x%02x already in "
1988 "use, not probing\n", addr_list[i]);
1989 continue;
1990 }
1991
Jean Delvare63e4e802010-06-03 11:33:51 +02001992 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02001993 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02001994 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001995 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02001996
1997 if (addr_list[i] == I2C_CLIENT_END) {
1998 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1999 return NULL;
2000 }
2001
2002 info->addr = addr_list[i];
2003 return i2c_new_device(adap, info);
2004}
2005EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2006
Jean Delvared735b342011-03-20 14:50:52 +01002007struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002010
Jean Delvarecaada322008-01-27 18:14:49 +01002011 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002012 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002013 if (adapter && !try_module_get(adapter->owner))
2014 adapter = NULL;
2015
Jean Delvarecaada322008-01-27 18:14:49 +01002016 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002017 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
David Brownellc0564602007-05-01 23:26:31 +02002019EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021void i2c_put_adapter(struct i2c_adapter *adap)
2022{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002023 if (adap)
2024 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
David Brownellc0564602007-05-01 23:26:31 +02002026EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028/* The SMBus parts */
2029
David Brownell438d6c22006-12-10 21:21:31 +01002030#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002031static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002034
Farid Hammane7225acf2010-05-21 18:40:58 +02002035 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002036 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 data = data ^ POLY;
2038 data = data << 1;
2039 }
2040 return (u8)(data >> 8);
2041}
2042
Jean Delvare421ef472005-10-26 21:28:55 +02002043/* Incremental CRC8 over count bytes in the array pointed to by p */
2044static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 int i;
2047
Farid Hammane7225acf2010-05-21 18:40:58 +02002048 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002049 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return crc;
2051}
2052
Jean Delvare421ef472005-10-26 21:28:55 +02002053/* Assume a 7-bit address, which is reasonable for SMBus */
2054static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
Jean Delvare421ef472005-10-26 21:28:55 +02002056 /* The address will be sent first */
2057 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2058 pec = i2c_smbus_pec(pec, &addr, 1);
2059
2060 /* The data buffer follows */
2061 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Jean Delvare421ef472005-10-26 21:28:55 +02002064/* Used for write only transactions */
2065static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Jean Delvare421ef472005-10-26 21:28:55 +02002067 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2068 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Jean Delvare421ef472005-10-26 21:28:55 +02002071/* Return <0 on CRC error
2072 If there was a write before this read (most cases) we need to take the
2073 partial CRC from the write part into account.
2074 Note that this function does modify the message (we need to decrease the
2075 message length to hide the CRC byte from the caller). */
2076static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Jean Delvare421ef472005-10-26 21:28:55 +02002078 u8 rpec = msg->buf[--msg->len];
2079 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (rpec != cpec) {
2082 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2083 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002084 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
David Brownell438d6c22006-12-10 21:21:31 +01002086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
David Brownella1cdeda2008-07-14 22:38:24 +02002089/**
2090 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2091 * @client: Handle to slave device
2092 *
2093 * This executes the SMBus "receive byte" protocol, returning negative errno
2094 * else the byte received from the device.
2095 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002096s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002099 int status;
2100
2101 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2102 I2C_SMBUS_READ, 0,
2103 I2C_SMBUS_BYTE, &data);
2104 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
David Brownellc0564602007-05-01 23:26:31 +02002106EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
David Brownella1cdeda2008-07-14 22:38:24 +02002108/**
2109 * i2c_smbus_write_byte - SMBus "send byte" protocol
2110 * @client: Handle to slave device
2111 * @value: Byte to be sent
2112 *
2113 * This executes the SMBus "send byte" protocol, returning negative errno
2114 * else zero on success.
2115 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002116s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Farid Hammane7225acf2010-05-21 18:40:58 +02002118 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002119 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
David Brownellc0564602007-05-01 23:26:31 +02002121EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
David Brownella1cdeda2008-07-14 22:38:24 +02002123/**
2124 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2125 * @client: Handle to slave device
2126 * @command: Byte interpreted by slave
2127 *
2128 * This executes the SMBus "read byte" protocol, returning negative errno
2129 * else a data byte received from the device.
2130 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002131s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002134 int status;
2135
2136 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2137 I2C_SMBUS_READ, command,
2138 I2C_SMBUS_BYTE_DATA, &data);
2139 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
David Brownellc0564602007-05-01 23:26:31 +02002141EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
David Brownella1cdeda2008-07-14 22:38:24 +02002143/**
2144 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2145 * @client: Handle to slave device
2146 * @command: Byte interpreted by slave
2147 * @value: Byte being written
2148 *
2149 * This executes the SMBus "write byte" protocol, returning negative errno
2150 * else zero on success.
2151 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002152s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2153 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 union i2c_smbus_data data;
2156 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002157 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2158 I2C_SMBUS_WRITE, command,
2159 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
David Brownellc0564602007-05-01 23:26:31 +02002161EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
David Brownella1cdeda2008-07-14 22:38:24 +02002163/**
2164 * i2c_smbus_read_word_data - SMBus "read word" protocol
2165 * @client: Handle to slave device
2166 * @command: Byte interpreted by slave
2167 *
2168 * This executes the SMBus "read word" protocol, returning negative errno
2169 * else a 16-bit unsigned "word" received from the device.
2170 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002171s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002174 int status;
2175
2176 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2177 I2C_SMBUS_READ, command,
2178 I2C_SMBUS_WORD_DATA, &data);
2179 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
David Brownellc0564602007-05-01 23:26:31 +02002181EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
David Brownella1cdeda2008-07-14 22:38:24 +02002183/**
2184 * i2c_smbus_write_word_data - SMBus "write word" protocol
2185 * @client: Handle to slave device
2186 * @command: Byte interpreted by slave
2187 * @value: 16-bit "word" being written
2188 *
2189 * This executes the SMBus "write word" protocol, returning negative errno
2190 * else zero on success.
2191 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002192s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2193 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 union i2c_smbus_data data;
2196 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002197 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2198 I2C_SMBUS_WRITE, command,
2199 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
David Brownellc0564602007-05-01 23:26:31 +02002201EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
David Brownella64ec072007-10-13 23:56:31 +02002203/**
David Brownella1cdeda2008-07-14 22:38:24 +02002204 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002205 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002206 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002207 * @values: Byte array into which data will be read; big enough to hold
2208 * the data returned by the slave. SMBus allows at most 32 bytes.
2209 *
David Brownella1cdeda2008-07-14 22:38:24 +02002210 * This executes the SMBus "block read" protocol, returning negative errno
2211 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002212 *
2213 * Note that using this function requires that the client's adapter support
2214 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2215 * support this; its emulation through I2C messaging relies on a specific
2216 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2217 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002218s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002219 u8 *values)
2220{
2221 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002222 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002223
David Brownell24a5bb72008-07-14 22:38:23 +02002224 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2225 I2C_SMBUS_READ, command,
2226 I2C_SMBUS_BLOCK_DATA, &data);
2227 if (status)
2228 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002229
2230 memcpy(values, &data.block[1], data.block[0]);
2231 return data.block[0];
2232}
2233EXPORT_SYMBOL(i2c_smbus_read_block_data);
2234
David Brownella1cdeda2008-07-14 22:38:24 +02002235/**
2236 * i2c_smbus_write_block_data - SMBus "block write" protocol
2237 * @client: Handle to slave device
2238 * @command: Byte interpreted by slave
2239 * @length: Size of data block; SMBus allows at most 32 bytes
2240 * @values: Byte array which will be written.
2241 *
2242 * This executes the SMBus "block write" protocol, returning negative errno
2243 * else zero on success.
2244 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002245s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002246 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
2248 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (length > I2C_SMBUS_BLOCK_MAX)
2251 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002253 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002254 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2255 I2C_SMBUS_WRITE, command,
2256 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257}
David Brownellc0564602007-05-01 23:26:31 +02002258EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002261s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002262 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002265 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002266
Jean Delvare4b2643d2007-07-12 14:12:29 +02002267 if (length > I2C_SMBUS_BLOCK_MAX)
2268 length = I2C_SMBUS_BLOCK_MAX;
2269 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002270 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2271 I2C_SMBUS_READ, command,
2272 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2273 if (status < 0)
2274 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002275
2276 memcpy(values, &data.block[1], data.block[0]);
2277 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
David Brownellc0564602007-05-01 23:26:31 +02002279EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Jean Delvare0cc43a12011-01-10 22:11:23 +01002281s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002282 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002283{
2284 union i2c_smbus_data data;
2285
2286 if (length > I2C_SMBUS_BLOCK_MAX)
2287 length = I2C_SMBUS_BLOCK_MAX;
2288 data.block[0] = length;
2289 memcpy(data.block + 1, values, length);
2290 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2291 I2C_SMBUS_WRITE, command,
2292 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2293}
David Brownellc0564602007-05-01 23:26:31 +02002294EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002295
David Brownell438d6c22006-12-10 21:21:31 +01002296/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002298static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2299 unsigned short flags,
2300 char read_write, u8 command, int size,
2301 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
2303 /* So we need to generate a series of msgs. In the case of writing, we
2304 need to use only one message; when reading, we need two. We initialize
2305 most things with sane defaults, to keep the code below somewhat
2306 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002307 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2308 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002309 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002311 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002312 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002313 struct i2c_msg msg[2] = {
2314 {
2315 .addr = addr,
2316 .flags = flags,
2317 .len = 1,
2318 .buf = msgbuf0,
2319 }, {
2320 .addr = addr,
2321 .flags = flags | I2C_M_RD,
2322 .len = 0,
2323 .buf = msgbuf1,
2324 },
2325 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002328 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 case I2C_SMBUS_QUICK:
2330 msg[0].len = 0;
2331 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002332 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2333 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 num = 1;
2335 break;
2336 case I2C_SMBUS_BYTE:
2337 if (read_write == I2C_SMBUS_READ) {
2338 /* Special case: only a read! */
2339 msg[0].flags = I2C_M_RD | flags;
2340 num = 1;
2341 }
2342 break;
2343 case I2C_SMBUS_BYTE_DATA:
2344 if (read_write == I2C_SMBUS_READ)
2345 msg[1].len = 1;
2346 else {
2347 msg[0].len = 2;
2348 msgbuf0[1] = data->byte;
2349 }
2350 break;
2351 case I2C_SMBUS_WORD_DATA:
2352 if (read_write == I2C_SMBUS_READ)
2353 msg[1].len = 2;
2354 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002355 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002357 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359 break;
2360 case I2C_SMBUS_PROC_CALL:
2361 num = 2; /* Special case */
2362 read_write = I2C_SMBUS_READ;
2363 msg[0].len = 3;
2364 msg[1].len = 2;
2365 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002366 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 break;
2368 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002370 msg[1].flags |= I2C_M_RECV_LEN;
2371 msg[1].len = 1; /* block length will be added by
2372 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 } else {
2374 msg[0].len = data->block[0] + 2;
2375 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002376 dev_err(&adapter->dev,
2377 "Invalid block write size %d\n",
2378 data->block[0]);
2379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002381 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 msgbuf0[i] = data->block[i-1];
2383 }
2384 break;
2385 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002386 num = 2; /* Another special case */
2387 read_write = I2C_SMBUS_READ;
2388 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002389 dev_err(&adapter->dev,
2390 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002391 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002392 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002393 }
2394 msg[0].len = data->block[0] + 2;
2395 for (i = 1; i < msg[0].len; i++)
2396 msgbuf0[i] = data->block[i-1];
2397 msg[1].flags |= I2C_M_RECV_LEN;
2398 msg[1].len = 1; /* block length will be added by
2399 the underlying bus driver */
2400 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 case I2C_SMBUS_I2C_BLOCK_DATA:
2402 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002403 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 } else {
2405 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002406 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002407 dev_err(&adapter->dev,
2408 "Invalid block write size %d\n",
2409 data->block[0]);
2410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
2412 for (i = 1; i <= data->block[0]; i++)
2413 msgbuf0[i] = data->block[i];
2414 }
2415 break;
2416 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002417 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2418 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420
Jean Delvare421ef472005-10-26 21:28:55 +02002421 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2422 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2423 if (i) {
2424 /* Compute PEC if first message is a write */
2425 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002426 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002427 i2c_smbus_add_pec(&msg[0]);
2428 else /* Write followed by read */
2429 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2430 }
2431 /* Ask for PEC if last message is a read */
2432 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002433 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002434 }
2435
David Brownell24a5bb72008-07-14 22:38:23 +02002436 status = i2c_transfer(adapter, msg, num);
2437 if (status < 0)
2438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Jean Delvare421ef472005-10-26 21:28:55 +02002440 /* Check PEC if last message is a read */
2441 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002442 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2443 if (status < 0)
2444 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002445 }
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002448 switch (size) {
2449 case I2C_SMBUS_BYTE:
2450 data->byte = msgbuf0[0];
2451 break;
2452 case I2C_SMBUS_BYTE_DATA:
2453 data->byte = msgbuf1[0];
2454 break;
2455 case I2C_SMBUS_WORD_DATA:
2456 case I2C_SMBUS_PROC_CALL:
2457 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2458 break;
2459 case I2C_SMBUS_I2C_BLOCK_DATA:
2460 for (i = 0; i < data->block[0]; i++)
2461 data->block[i+1] = msgbuf1[i];
2462 break;
2463 case I2C_SMBUS_BLOCK_DATA:
2464 case I2C_SMBUS_BLOCK_PROC_CALL:
2465 for (i = 0; i < msgbuf1[0] + 1; i++)
2466 data->block[i] = msgbuf1[i];
2467 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 }
2469 return 0;
2470}
2471
David Brownella1cdeda2008-07-14 22:38:24 +02002472/**
2473 * i2c_smbus_xfer - execute SMBus protocol operations
2474 * @adapter: Handle to I2C bus
2475 * @addr: Address of SMBus slave on that bus
2476 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2477 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2478 * @command: Byte interpreted by slave, for protocols which use such bytes
2479 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2480 * @data: Data to be read or written
2481 *
2482 * This executes an SMBus protocol operation, and returns a negative
2483 * errno code else zero on success.
2484 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002485s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002486 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002487 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002489 unsigned long orig_jiffies;
2490 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
David Howells8a3259972014-03-06 13:36:06 +00002493 /* If enabled, the following two tracepoints are conditional on
2494 * read_write and protocol.
2495 */
2496 trace_smbus_write(adapter, addr, flags, read_write,
2497 command, protocol, data);
2498 trace_smbus_read(adapter, addr, flags, read_write,
2499 command, protocol);
2500
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002501 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002504 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002505
2506 /* Retry automatically on arbitration loss */
2507 orig_jiffies = jiffies;
2508 for (res = 0, try = 0; try <= adapter->retries; try++) {
2509 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2510 read_write, command,
2511 protocol, data);
2512 if (res != -EAGAIN)
2513 break;
2514 if (time_after(jiffies,
2515 orig_jiffies + adapter->timeout))
2516 break;
2517 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002518 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002520 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a3259972014-03-06 13:36:06 +00002521 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002522 /*
2523 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2524 * implement native support for the SMBus operation.
2525 */
2526 }
2527
David Howells8a3259972014-03-06 13:36:06 +00002528 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2529 command, protocol, data);
2530
2531trace:
2532 /* If enabled, the reply tracepoint is conditional on read_write. */
2533 trace_smbus_reply(adapter, addr, flags, read_write,
2534 command, protocol, data);
2535 trace_smbus_result(adapter, addr, flags, read_write,
2536 command, protocol, res);
2537
2538 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2543MODULE_DESCRIPTION("I2C-Bus main module");
2544MODULE_LICENSE("GPL");