blob: 66aa83b99383d3701e426616b1b17c7a3835223a [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 Rapoportcea443a2008-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
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300282 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200283 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sang72fa8182014-01-21 17:48:34 +0100284 if (status)
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300285 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sang72fa8182014-01-21 17:48:34 +0100286
Hans Verkuil50c33042008-03-12 14:15:00 +0100287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static int i2c_device_remove(struct device *dev)
291{
Jean Delvare51298d12009-09-18 22:45:45 +0200292 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200293 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100294 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200295
Jean Delvare51298d12009-09-18 22:45:45 +0200296 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200297 return 0;
298
299 driver = to_i2c_driver(dev->driver);
300 if (driver->remove) {
301 dev_dbg(dev, "remove\n");
302 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200303 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100304
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300305 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
David Brownellf37dd802007-02-13 22:09:00 +0100309static void i2c_device_shutdown(struct device *dev)
310{
Jean Delvare51298d12009-09-18 22:45:45 +0200311 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100312 struct i2c_driver *driver;
313
Jean Delvare51298d12009-09-18 22:45:45 +0200314 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100315 return;
316 driver = to_i2c_driver(dev->driver);
317 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200318 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100319}
320
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200321#ifdef CONFIG_PM_SLEEP
322static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100323{
Jean Delvare51298d12009-09-18 22:45:45 +0200324 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100325 struct i2c_driver *driver;
326
Jean Delvare51298d12009-09-18 22:45:45 +0200327 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100328 return 0;
329 driver = to_i2c_driver(dev->driver);
330 if (!driver->suspend)
331 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200332 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100333}
334
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200335static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100336{
Jean Delvare51298d12009-09-18 22:45:45 +0200337 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100338 struct i2c_driver *driver;
339
Jean Delvare51298d12009-09-18 22:45:45 +0200340 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100341 return 0;
342 driver = to_i2c_driver(dev->driver);
343 if (!driver->resume)
344 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200345 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100346}
347
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200348static int i2c_device_pm_suspend(struct device *dev)
349{
350 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
351
Mark Brownd529de22011-01-14 22:03:49 +0100352 if (pm)
353 return pm_generic_suspend(dev);
354 else
355 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200356}
357
358static int i2c_device_pm_resume(struct device *dev)
359{
360 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200361
362 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100363 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200364 else
Mark Brownd529de22011-01-14 22:03:49 +0100365 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200366}
367
368static int i2c_device_pm_freeze(struct device *dev)
369{
370 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
371
Mark Brownd529de22011-01-14 22:03:49 +0100372 if (pm)
373 return pm_generic_freeze(dev);
374 else
375 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200376}
377
378static int i2c_device_pm_thaw(struct device *dev)
379{
380 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
381
Mark Brownd529de22011-01-14 22:03:49 +0100382 if (pm)
383 return pm_generic_thaw(dev);
384 else
385 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200386}
387
388static int i2c_device_pm_poweroff(struct device *dev)
389{
390 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
391
Mark Brownd529de22011-01-14 22:03:49 +0100392 if (pm)
393 return pm_generic_poweroff(dev);
394 else
395 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200396}
397
398static int i2c_device_pm_restore(struct device *dev)
399{
400 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200401
402 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100403 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200404 else
Mark Brownd529de22011-01-14 22:03:49 +0100405 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200406}
407#else /* !CONFIG_PM_SLEEP */
408#define i2c_device_pm_suspend NULL
409#define i2c_device_pm_resume NULL
410#define i2c_device_pm_freeze NULL
411#define i2c_device_pm_thaw NULL
412#define i2c_device_pm_poweroff NULL
413#define i2c_device_pm_restore NULL
414#endif /* !CONFIG_PM_SLEEP */
415
David Brownell9c1600e2007-05-01 23:26:31 +0200416static void i2c_client_dev_release(struct device *dev)
417{
418 kfree(to_i2c_client(dev));
419}
420
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100421static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200422show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200423{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200424 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
425 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200426}
427
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100428static ssize_t
429show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200430{
431 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800432 int len;
433
434 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
435 if (len != -ENODEV)
436 return len;
437
Jean Delvareeb8a7902008-05-18 20:49:41 +0200438 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200439}
440
Jean Delvare4f8cf822009-09-18 22:45:46 +0200441static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200442static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
443
444static struct attribute *i2c_dev_attrs[] = {
445 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200446 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200447 &dev_attr_modalias.attr,
448 NULL
449};
450
451static struct attribute_group i2c_dev_attr_group = {
452 .attrs = i2c_dev_attrs,
453};
454
455static const struct attribute_group *i2c_dev_attr_groups[] = {
456 &i2c_dev_attr_group,
457 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200458};
459
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100460static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100461 .suspend = i2c_device_pm_suspend,
462 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200463 .freeze = i2c_device_pm_freeze,
464 .thaw = i2c_device_pm_thaw,
465 .poweroff = i2c_device_pm_poweroff,
466 .restore = i2c_device_pm_restore,
467 SET_RUNTIME_PM_OPS(
468 pm_generic_runtime_suspend,
469 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200470 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200471 )
sonic zhang54067ee2009-12-14 21:17:30 +0100472};
473
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200474struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100475 .name = "i2c",
476 .match = i2c_device_match,
477 .probe = i2c_device_probe,
478 .remove = i2c_device_remove,
479 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100480 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000481};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200482EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000483
Jean Delvare51298d12009-09-18 22:45:45 +0200484static struct device_type i2c_client_type = {
485 .groups = i2c_dev_attr_groups,
486 .uevent = i2c_device_uevent,
487 .release = i2c_client_dev_release,
488};
489
David Brownell9b766b82008-01-27 18:14:51 +0100490
491/**
492 * i2c_verify_client - return parameter as i2c_client, or NULL
493 * @dev: device, probably from some driver model iterator
494 *
495 * When traversing the driver model tree, perhaps using driver model
496 * iterators like @device_for_each_child(), you can't assume very much
497 * about the nodes you find. Use this function to avoid oopses caused
498 * by wrongly treating some non-I2C device as an i2c_client.
499 */
500struct i2c_client *i2c_verify_client(struct device *dev)
501{
Jean Delvare51298d12009-09-18 22:45:45 +0200502 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100503 ? to_i2c_client(dev)
504 : NULL;
505}
506EXPORT_SYMBOL(i2c_verify_client);
507
508
Jean Delvare3a89db52010-06-03 11:33:52 +0200509/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300510 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200511static int i2c_check_client_addr_validity(const struct i2c_client *client)
512{
513 if (client->flags & I2C_CLIENT_TEN) {
514 /* 10-bit address, all values are valid */
515 if (client->addr > 0x3ff)
516 return -EINVAL;
517 } else {
518 /* 7-bit address, reject the general call address */
519 if (client->addr == 0x00 || client->addr > 0x7f)
520 return -EINVAL;
521 }
522 return 0;
523}
524
Jean Delvare656b8762010-06-03 11:33:53 +0200525/* And this is a strict address validity check, used when probing. If a
526 * device uses a reserved address, then it shouldn't be probed. 7-bit
527 * addressing is assumed, 10-bit address devices are rare and should be
528 * explicitly enumerated. */
529static int i2c_check_addr_validity(unsigned short addr)
530{
531 /*
532 * Reserved addresses per I2C specification:
533 * 0x00 General call address / START byte
534 * 0x01 CBUS address
535 * 0x02 Reserved for different bus format
536 * 0x03 Reserved for future purposes
537 * 0x04-0x07 Hs-mode master code
538 * 0x78-0x7b 10-bit slave addressing
539 * 0x7c-0x7f Reserved for future purposes
540 */
541 if (addr < 0x08 || addr > 0x77)
542 return -EINVAL;
543 return 0;
544}
545
Jean Delvare3b5f7942010-06-03 11:33:55 +0200546static int __i2c_check_addr_busy(struct device *dev, void *addrp)
547{
548 struct i2c_client *client = i2c_verify_client(dev);
549 int addr = *(int *)addrp;
550
551 if (client && client->addr == addr)
552 return -EBUSY;
553 return 0;
554}
555
Michael Lawnick08263742010-08-11 18:21:02 +0200556/* walk up mux tree */
557static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
558{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200559 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200560 int result;
561
562 result = device_for_each_child(&adapter->dev, &addr,
563 __i2c_check_addr_busy);
564
Jean Delvare97cc4d42010-10-24 18:16:57 +0200565 if (!result && parent)
566 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200567
568 return result;
569}
570
571/* recurse down mux tree */
572static int i2c_check_mux_children(struct device *dev, void *addrp)
573{
574 int result;
575
576 if (dev->type == &i2c_adapter_type)
577 result = device_for_each_child(dev, addrp,
578 i2c_check_mux_children);
579 else
580 result = __i2c_check_addr_busy(dev, addrp);
581
582 return result;
583}
584
Jean Delvare3b5f7942010-06-03 11:33:55 +0200585static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
586{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200587 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200588 int result = 0;
589
Jean Delvare97cc4d42010-10-24 18:16:57 +0200590 if (parent)
591 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200592
593 if (!result)
594 result = device_for_each_child(&adapter->dev, &addr,
595 i2c_check_mux_children);
596
597 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200598}
599
David Brownell9c1600e2007-05-01 23:26:31 +0200600/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200601 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
602 * @adapter: Target I2C bus segment
603 */
604void i2c_lock_adapter(struct i2c_adapter *adapter)
605{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200606 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
607
608 if (parent)
609 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200610 else
611 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200612}
613EXPORT_SYMBOL_GPL(i2c_lock_adapter);
614
615/**
616 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
617 * @adapter: Target I2C bus segment
618 */
619static int i2c_trylock_adapter(struct i2c_adapter *adapter)
620{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200621 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
622
623 if (parent)
624 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200625 else
626 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200627}
628
629/**
630 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
631 * @adapter: Target I2C bus segment
632 */
633void i2c_unlock_adapter(struct i2c_adapter *adapter)
634{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200635 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
636
637 if (parent)
638 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200639 else
640 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200641}
642EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
643
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200644static void i2c_dev_set_name(struct i2c_adapter *adap,
645 struct i2c_client *client)
646{
647 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
648
649 if (adev) {
650 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
651 return;
652 }
653
654 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
655 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
656 client->addr | ((client->flags & I2C_CLIENT_TEN)
657 ? 0xa000 : 0));
658}
659
Jean Delvarefe61e072010-08-11 18:20:58 +0200660/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200661 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200662 * @adap: the adapter managing the device
663 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200664 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200665 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200666 * Create an i2c device. Binding is handled through driver model
667 * probe()/remove() methods. A driver may be bound to this device when we
668 * return from this function, or any later moment (e.g. maybe hotplugging will
669 * load the driver module). This call is not appropriate for use by mainboard
670 * initialization logic, which usually runs during an arch_initcall() long
671 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200672 *
673 * This returns the new i2c client, which may be saved for later use with
674 * i2c_unregister_device(); or NULL to indicate an error.
675 */
676struct i2c_client *
677i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
678{
679 struct i2c_client *client;
680 int status;
681
682 client = kzalloc(sizeof *client, GFP_KERNEL);
683 if (!client)
684 return NULL;
685
686 client->adapter = adap;
687
688 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200689
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200690 if (info->archdata)
691 client->dev.archdata = *info->archdata;
692
Marc Pignatee354252008-08-28 08:33:22 +0200693 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200694 client->addr = info->addr;
695 client->irq = info->irq;
696
David Brownell9c1600e2007-05-01 23:26:31 +0200697 strlcpy(client->name, info->type, sizeof(client->name));
698
Jean Delvare3a89db52010-06-03 11:33:52 +0200699 /* Check for address validity */
700 status = i2c_check_client_addr_validity(client);
701 if (status) {
702 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
703 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
704 goto out_err_silent;
705 }
706
Jean Delvaref8a227e2009-06-19 16:58:18 +0200707 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200708 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200709 if (status)
710 goto out_err;
711
712 client->dev.parent = &client->adapter->dev;
713 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200714 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700715 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100716 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200717
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200718 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200719 status = device_register(&client->dev);
720 if (status)
721 goto out_err;
722
Jean Delvaref8a227e2009-06-19 16:58:18 +0200723 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
724 client->name, dev_name(&client->dev));
725
David Brownell9c1600e2007-05-01 23:26:31 +0200726 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200727
728out_err:
729 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
730 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200731out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200732 kfree(client);
733 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200734}
735EXPORT_SYMBOL_GPL(i2c_new_device);
736
737
738/**
739 * i2c_unregister_device - reverse effect of i2c_new_device()
740 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200741 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200742 */
743void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200744{
David Brownella1d9e6e2007-05-01 23:26:30 +0200745 device_unregister(&client->dev);
746}
David Brownell9c1600e2007-05-01 23:26:31 +0200747EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200748
749
Jean Delvare60b129d2008-05-11 20:37:06 +0200750static const struct i2c_device_id dummy_id[] = {
751 { "dummy", 0 },
752 { },
753};
754
Jean Delvared2653e92008-04-29 23:11:39 +0200755static int dummy_probe(struct i2c_client *client,
756 const struct i2c_device_id *id)
757{
758 return 0;
759}
760
761static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100762{
763 return 0;
764}
765
766static struct i2c_driver dummy_driver = {
767 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200768 .probe = dummy_probe,
769 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200770 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100771};
772
773/**
774 * i2c_new_dummy - return a new i2c device bound to a dummy driver
775 * @adapter: the adapter managing the device
776 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100777 * Context: can sleep
778 *
779 * This returns an I2C client bound to the "dummy" driver, intended for use
780 * with devices that consume multiple addresses. Examples of such chips
781 * include various EEPROMS (like 24c04 and 24c08 models).
782 *
783 * These dummy devices have two main uses. First, most I2C and SMBus calls
784 * except i2c_transfer() need a client handle; the dummy will be that handle.
785 * And second, this prevents the specified address from being bound to a
786 * different driver.
787 *
788 * This returns the new i2c client, which should be saved for later use with
789 * i2c_unregister_device(); or NULL to indicate an error.
790 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100791struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100792{
793 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200794 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100795 };
796
David Brownelle9f13732008-01-27 18:14:52 +0100797 return i2c_new_device(adapter, &info);
798}
799EXPORT_SYMBOL_GPL(i2c_new_dummy);
800
David Brownellf37dd802007-02-13 22:09:00 +0100801/* ------------------------------------------------------------------------- */
802
David Brownell16ffadf2007-05-01 23:26:28 +0200803/* I2C bus adapters -- one roots each I2C or SMBUS segment */
804
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200805static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
David Brownellef2c83212007-05-01 23:26:28 +0200807 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 complete(&adap->dev_released);
809}
810
Jean Delvare99cd8e22009-06-19 16:58:20 +0200811/*
Jean Delvare390946b2012-09-10 10:14:02 +0200812 * This function is only needed for mutex_lock_nested, so it is never
813 * called unless locking correctness checking is enabled. Thus we
814 * make it inline to avoid a compiler warning. That's what gcc ends up
815 * doing anyway.
816 */
817static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
818{
819 unsigned int depth = 0;
820
821 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
822 depth++;
823
824 return depth;
825}
826
827/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200828 * Let users instantiate I2C devices through sysfs. This can be used when
829 * platform initialization code doesn't contain the proper data for
830 * whatever reason. Also useful for drivers that do device detection and
831 * detection fails, either because the device uses an unexpected address,
832 * or this is a compatible device with different ID register values.
833 *
834 * Parameter checking may look overzealous, but we really don't want
835 * the user to provide incorrect parameters.
836 */
837static ssize_t
838i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
839 const char *buf, size_t count)
840{
841 struct i2c_adapter *adap = to_i2c_adapter(dev);
842 struct i2c_board_info info;
843 struct i2c_client *client;
844 char *blank, end;
845 int res;
846
Jean Delvare99cd8e22009-06-19 16:58:20 +0200847 memset(&info, 0, sizeof(struct i2c_board_info));
848
849 blank = strchr(buf, ' ');
850 if (!blank) {
851 dev_err(dev, "%s: Missing parameters\n", "new_device");
852 return -EINVAL;
853 }
854 if (blank - buf > I2C_NAME_SIZE - 1) {
855 dev_err(dev, "%s: Invalid device name\n", "new_device");
856 return -EINVAL;
857 }
858 memcpy(info.type, buf, blank - buf);
859
860 /* Parse remaining parameters, reject extra parameters */
861 res = sscanf(++blank, "%hi%c", &info.addr, &end);
862 if (res < 1) {
863 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
864 return -EINVAL;
865 }
866 if (res > 1 && end != '\n') {
867 dev_err(dev, "%s: Extra parameters\n", "new_device");
868 return -EINVAL;
869 }
870
Jean Delvare99cd8e22009-06-19 16:58:20 +0200871 client = i2c_new_device(adap, &info);
872 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200873 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200874
875 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200876 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200877 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200878 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200879 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
880 info.type, info.addr);
881
882 return count;
883}
884
885/*
886 * And of course let the users delete the devices they instantiated, if
887 * they got it wrong. This interface can only be used to delete devices
888 * instantiated by i2c_sysfs_new_device above. This guarantees that we
889 * don't delete devices to which some kernel code still has references.
890 *
891 * Parameter checking may look overzealous, but we really don't want
892 * the user to delete the wrong device.
893 */
894static ssize_t
895i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
896 const char *buf, size_t count)
897{
898 struct i2c_adapter *adap = to_i2c_adapter(dev);
899 struct i2c_client *client, *next;
900 unsigned short addr;
901 char end;
902 int res;
903
904 /* Parse parameters, reject extra parameters */
905 res = sscanf(buf, "%hi%c", &addr, &end);
906 if (res < 1) {
907 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
908 return -EINVAL;
909 }
910 if (res > 1 && end != '\n') {
911 dev_err(dev, "%s: Extra parameters\n", "delete_device");
912 return -EINVAL;
913 }
914
915 /* Make sure the device was added through sysfs */
916 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200917 mutex_lock_nested(&adap->userspace_clients_lock,
918 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200919 list_for_each_entry_safe(client, next, &adap->userspace_clients,
920 detected) {
921 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200922 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
923 "delete_device", client->name, client->addr);
924
925 list_del(&client->detected);
926 i2c_unregister_device(client);
927 res = count;
928 break;
929 }
930 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200931 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200932
933 if (res < 0)
934 dev_err(dev, "%s: Can't find device in list\n",
935 "delete_device");
936 return res;
937}
938
Jean Delvare4f8cf822009-09-18 22:45:46 +0200939static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200940static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
941 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200942
943static struct attribute *i2c_adapter_attrs[] = {
944 &dev_attr_name.attr,
945 &dev_attr_new_device.attr,
946 &dev_attr_delete_device.attr,
947 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200948};
949
Jean Delvare4f8cf822009-09-18 22:45:46 +0200950static struct attribute_group i2c_adapter_attr_group = {
951 .attrs = i2c_adapter_attrs,
952};
953
954static const struct attribute_group *i2c_adapter_attr_groups[] = {
955 &i2c_adapter_attr_group,
956 NULL
957};
958
Michael Lawnick08263742010-08-11 18:21:02 +0200959struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200960 .groups = i2c_adapter_attr_groups,
961 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200962};
Michael Lawnick08263742010-08-11 18:21:02 +0200963EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Stephen Warren643dd092012-04-17 12:43:33 -0600965/**
966 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
967 * @dev: device, probably from some driver model iterator
968 *
969 * When traversing the driver model tree, perhaps using driver model
970 * iterators like @device_for_each_child(), you can't assume very much
971 * about the nodes you find. Use this function to avoid oopses caused
972 * by wrongly treating some non-I2C device as an i2c_adapter.
973 */
974struct i2c_adapter *i2c_verify_adapter(struct device *dev)
975{
976 return (dev->type == &i2c_adapter_type)
977 ? to_i2c_adapter(dev)
978 : NULL;
979}
980EXPORT_SYMBOL(i2c_verify_adapter);
981
Jean Delvare2bb50952009-09-18 22:45:46 +0200982#ifdef CONFIG_I2C_COMPAT
983static struct class_compat *i2c_adapter_compat_class;
984#endif
985
David Brownell9c1600e2007-05-01 23:26:31 +0200986static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
987{
988 struct i2c_devinfo *devinfo;
989
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200990 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200991 list_for_each_entry(devinfo, &__i2c_board_list, list) {
992 if (devinfo->busnum == adapter->nr
993 && !i2c_new_device(adapter,
994 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100995 dev_err(&adapter->dev,
996 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200997 devinfo->board_info.addr);
998 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200999 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001000}
1001
Wolfram Sang687b81d2013-07-11 12:56:15 +01001002/* OF support code */
1003
1004#if IS_ENABLED(CONFIG_OF)
1005static void of_i2c_register_devices(struct i2c_adapter *adap)
1006{
1007 void *result;
1008 struct device_node *node;
1009
1010 /* Only register child devices if the adapter has a node pointer set */
1011 if (!adap->dev.of_node)
1012 return;
1013
1014 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
1015
1016 for_each_available_child_of_node(adap->dev.of_node, node) {
1017 struct i2c_board_info info = {};
1018 struct dev_archdata dev_ad = {};
1019 const __be32 *addr;
1020 int len;
1021
1022 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1023
1024 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1025 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1026 node->full_name);
1027 continue;
1028 }
1029
1030 addr = of_get_property(node, "reg", &len);
1031 if (!addr || (len < sizeof(int))) {
1032 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1033 node->full_name);
1034 continue;
1035 }
1036
1037 info.addr = be32_to_cpup(addr);
1038 if (info.addr > (1 << 10) - 1) {
1039 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1040 info.addr, node->full_name);
1041 continue;
1042 }
1043
1044 info.irq = irq_of_parse_and_map(node, 0);
1045 info.of_node = of_node_get(node);
1046 info.archdata = &dev_ad;
1047
1048 if (of_get_property(node, "wakeup-source", NULL))
1049 info.flags |= I2C_CLIENT_WAKE;
1050
1051 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1052
1053 result = i2c_new_device(adap, &info);
1054 if (result == NULL) {
1055 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1056 node->full_name);
1057 of_node_put(node);
1058 irq_dispose_mapping(info.irq);
1059 continue;
1060 }
1061 }
1062}
1063
1064static int of_dev_node_match(struct device *dev, void *data)
1065{
1066 return dev->of_node == data;
1067}
1068
1069/* must call put_device() when done with returned i2c_client device */
1070struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1071{
1072 struct device *dev;
1073
1074 dev = bus_find_device(&i2c_bus_type, NULL, node,
1075 of_dev_node_match);
1076 if (!dev)
1077 return NULL;
1078
1079 return i2c_verify_client(dev);
1080}
1081EXPORT_SYMBOL(of_find_i2c_device_by_node);
1082
1083/* must call put_device() when done with returned i2c_adapter device */
1084struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1085{
1086 struct device *dev;
1087
1088 dev = bus_find_device(&i2c_bus_type, NULL, node,
1089 of_dev_node_match);
1090 if (!dev)
1091 return NULL;
1092
1093 return i2c_verify_adapter(dev);
1094}
1095EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1096#else
1097static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1098#endif /* CONFIG_OF */
1099
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001100/* ACPI support code */
1101
1102#if IS_ENABLED(CONFIG_ACPI)
1103static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1104{
1105 struct i2c_board_info *info = data;
1106
1107 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1108 struct acpi_resource_i2c_serialbus *sb;
1109
1110 sb = &ares->data.i2c_serial_bus;
1111 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1112 info->addr = sb->slave_address;
1113 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1114 info->flags |= I2C_CLIENT_TEN;
1115 }
1116 } else if (info->irq < 0) {
1117 struct resource r;
1118
1119 if (acpi_dev_resource_interrupt(ares, 0, &r))
1120 info->irq = r.start;
1121 }
1122
1123 /* Tell the ACPI core to skip this resource */
1124 return 1;
1125}
1126
1127static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1128 void *data, void **return_value)
1129{
1130 struct i2c_adapter *adapter = data;
1131 struct list_head resource_list;
1132 struct i2c_board_info info;
1133 struct acpi_device *adev;
1134 int ret;
1135
1136 if (acpi_bus_get_device(handle, &adev))
1137 return AE_OK;
1138 if (acpi_bus_get_status(adev) || !adev->status.present)
1139 return AE_OK;
1140
1141 memset(&info, 0, sizeof(info));
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001142 info.acpi_node.companion = adev;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001143 info.irq = -1;
1144
1145 INIT_LIST_HEAD(&resource_list);
1146 ret = acpi_dev_get_resources(adev, &resource_list,
1147 acpi_i2c_add_resource, &info);
1148 acpi_dev_free_resource_list(&resource_list);
1149
1150 if (ret < 0 || !info.addr)
1151 return AE_OK;
1152
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001153 adev->power.flags.ignore_parent = true;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001154 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1155 if (!i2c_new_device(adapter, &info)) {
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001156 adev->power.flags.ignore_parent = false;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001157 dev_err(&adapter->dev,
1158 "failed to add I2C device %s from ACPI\n",
1159 dev_name(&adev->dev));
1160 }
1161
1162 return AE_OK;
1163}
1164
1165/**
1166 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1167 * @adap: pointer to adapter
1168 *
1169 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1170 * namespace. When a device is found it will be added to the Linux device
1171 * model and bound to the corresponding ACPI handle.
1172 */
1173static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1174{
1175 acpi_handle handle;
1176 acpi_status status;
1177
Jean Delvare47b6e4772013-10-10 08:04:06 +02001178 if (!adap->dev.parent)
1179 return;
1180
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001181 handle = ACPI_HANDLE(adap->dev.parent);
1182 if (!handle)
1183 return;
1184
1185 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1186 acpi_i2c_add_device, NULL,
1187 adap, NULL);
1188 if (ACPI_FAILURE(status))
1189 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1190}
1191#else
1192static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1193#endif /* CONFIG_ACPI */
1194
Jean Delvare69b00892009-12-06 17:06:27 +01001195static int i2c_do_add_adapter(struct i2c_driver *driver,
1196 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001197{
Jean Delvare4735c982008-07-14 22:38:36 +02001198 /* Detect supported devices on that bus, and instantiate them */
1199 i2c_detect(adap, driver);
1200
1201 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001202 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001203 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1204 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001205 dev_warn(&adap->dev, "Please use another way to instantiate "
1206 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001207 /* We ignore the return code; if it fails, too bad */
1208 driver->attach_adapter(adap);
1209 }
1210 return 0;
1211}
1212
Jean Delvare69b00892009-12-06 17:06:27 +01001213static int __process_new_adapter(struct device_driver *d, void *data)
1214{
1215 return i2c_do_add_adapter(to_i2c_driver(d), data);
1216}
1217
David Brownell6e13e642007-05-01 23:26:31 +02001218static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Jean Delvared6703282010-08-11 18:20:59 +02001220 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
David Brownell1d0b19c2008-10-14 17:30:05 +02001222 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001223 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1224 res = -EAGAIN;
1225 goto out_list;
1226 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001227
Jean Delvare2236baa2010-11-15 22:40:38 +01001228 /* Sanity checks */
1229 if (unlikely(adap->name[0] == '\0')) {
1230 pr_err("i2c-core: Attempt to register an adapter with "
1231 "no name!\n");
1232 return -EINVAL;
1233 }
1234 if (unlikely(!adap->algo)) {
1235 pr_err("i2c-core: Attempt to register adapter '%s' with "
1236 "no algo!\n", adap->name);
1237 return -EINVAL;
1238 }
1239
Mika Kuoppala194684e2009-12-06 17:06:22 +01001240 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001241 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001242 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Jean Delvare8fcfef62009-03-28 21:34:43 +01001244 /* Set default timeout to 1 second if not already set */
1245 if (adap->timeout == 0)
1246 adap->timeout = HZ;
1247
Kay Sievers27d9c182009-01-07 14:29:16 +01001248 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001249 adap->dev.bus = &i2c_bus_type;
1250 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001251 res = device_register(&adap->dev);
1252 if (res)
1253 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001255 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1256
Jean Delvare2bb50952009-09-18 22:45:46 +02001257#ifdef CONFIG_I2C_COMPAT
1258 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1259 adap->dev.parent);
1260 if (res)
1261 dev_warn(&adap->dev,
1262 "Failed to create compatibility class link\n");
1263#endif
1264
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301265 /* bus recovery specific initialization */
1266 if (adap->bus_recovery_info) {
1267 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1268
1269 if (!bri->recover_bus) {
1270 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1271 adap->bus_recovery_info = NULL;
1272 goto exit_recovery;
1273 }
1274
1275 /* Generic GPIO recovery */
1276 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1277 if (!gpio_is_valid(bri->scl_gpio)) {
1278 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1279 adap->bus_recovery_info = NULL;
1280 goto exit_recovery;
1281 }
1282
1283 if (gpio_is_valid(bri->sda_gpio))
1284 bri->get_sda = get_sda_gpio_value;
1285 else
1286 bri->get_sda = NULL;
1287
1288 bri->get_scl = get_scl_gpio_value;
1289 bri->set_scl = set_scl_gpio_value;
1290 } else if (!bri->set_scl || !bri->get_scl) {
1291 /* Generic SCL recovery */
1292 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1293 adap->bus_recovery_info = NULL;
1294 }
1295 }
1296
1297exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001298 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001299 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001300 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001301
David Brownell6e13e642007-05-01 23:26:31 +02001302 if (adap->nr < __i2c_first_dynamic_bus_num)
1303 i2c_scan_static_board_info(adap);
1304
Jean Delvare4735c982008-07-14 22:38:36 +02001305 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001306 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001307 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001308 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001309
1310 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001311
Jean Delvareb119c6c2006-08-15 18:26:30 +02001312out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001313 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001314 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001315 mutex_unlock(&core_lock);
1316 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317}
1318
David Brownell6e13e642007-05-01 23:26:31 +02001319/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001320 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1321 * @adap: the adapter to register (with adap->nr initialized)
1322 * Context: can sleep
1323 *
1324 * See i2c_add_numbered_adapter() for details.
1325 */
1326static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1327{
1328 int id;
1329
1330 mutex_lock(&core_lock);
1331 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1332 GFP_KERNEL);
1333 mutex_unlock(&core_lock);
1334 if (id < 0)
1335 return id == -ENOSPC ? -EBUSY : id;
1336
1337 return i2c_register_adapter(adap);
1338}
1339
1340/**
David Brownell6e13e642007-05-01 23:26:31 +02001341 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1342 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001343 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001344 *
1345 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001346 * doesn't matter or when its bus number is specified by an dt alias.
1347 * Examples of bases when the bus number doesn't matter: I2C adapters
1348 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001349 *
1350 * When this returns zero, a new bus number was allocated and stored
1351 * in adap->nr, and the specified adapter became available for clients.
1352 * Otherwise, a negative errno value is returned.
1353 */
1354int i2c_add_adapter(struct i2c_adapter *adapter)
1355{
Doug Andersonee5c2742013-03-01 08:57:31 -08001356 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001357 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001358
Doug Andersonee5c2742013-03-01 08:57:31 -08001359 if (dev->of_node) {
1360 id = of_alias_get_id(dev->of_node, "i2c");
1361 if (id >= 0) {
1362 adapter->nr = id;
1363 return __i2c_add_numbered_adapter(adapter);
1364 }
1365 }
1366
Jean Delvarecaada322008-01-27 18:14:49 +01001367 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001368 id = idr_alloc(&i2c_adapter_idr, adapter,
1369 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001370 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001371 if (id < 0)
1372 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001373
1374 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001375
David Brownell6e13e642007-05-01 23:26:31 +02001376 return i2c_register_adapter(adapter);
1377}
1378EXPORT_SYMBOL(i2c_add_adapter);
1379
1380/**
1381 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1382 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001383 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001384 *
1385 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001386 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1387 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001388 * is used to properly configure I2C devices.
1389 *
Grant Likely488bf312011-07-25 17:49:43 +02001390 * If the requested bus number is set to -1, then this function will behave
1391 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1392 *
David Brownell6e13e642007-05-01 23:26:31 +02001393 * If no devices have pre-been declared for this bus, then be sure to
1394 * register the adapter before any dynamically allocated ones. Otherwise
1395 * the required bus ID may not be available.
1396 *
1397 * When this returns zero, the specified adapter became available for
1398 * clients using the bus number provided in adap->nr. Also, the table
1399 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1400 * and the appropriate driver model device nodes are created. Otherwise, a
1401 * negative errno value is returned.
1402 */
1403int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1404{
Grant Likely488bf312011-07-25 17:49:43 +02001405 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1406 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001407
Doug Andersonee5c2742013-03-01 08:57:31 -08001408 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001409}
1410EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1411
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001412static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001413 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001414{
Jean Delvare4735c982008-07-14 22:38:36 +02001415 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001416
Jean Delvareacec2112009-03-28 21:34:40 +01001417 /* Remove the devices we created ourselves as the result of hardware
1418 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001419 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1420 if (client->adapter == adapter) {
1421 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1422 client->name, client->addr);
1423 list_del(&client->detected);
1424 i2c_unregister_device(client);
1425 }
1426 }
Jean Delvare026526f2008-01-27 18:14:49 +01001427}
1428
Jean Delvaree549c2b2009-06-19 16:58:19 +02001429static int __unregister_client(struct device *dev, void *dummy)
1430{
1431 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001432 if (client && strcmp(client->name, "dummy"))
1433 i2c_unregister_device(client);
1434 return 0;
1435}
1436
1437static int __unregister_dummy(struct device *dev, void *dummy)
1438{
1439 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001440 if (client)
1441 i2c_unregister_device(client);
1442 return 0;
1443}
1444
Jean Delvare69b00892009-12-06 17:06:27 +01001445static int __process_removed_adapter(struct device_driver *d, void *data)
1446{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001447 i2c_do_del_adapter(to_i2c_driver(d), data);
1448 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001449}
1450
David Brownelld64f73b2007-07-12 14:12:28 +02001451/**
1452 * i2c_del_adapter - unregister I2C adapter
1453 * @adap: the adapter being unregistered
1454 * Context: can sleep
1455 *
1456 * This unregisters an I2C adapter which was previously registered
1457 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1458 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001459void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001461 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001462 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001465 mutex_lock(&core_lock);
1466 found = idr_find(&i2c_adapter_idr, adap->nr);
1467 mutex_unlock(&core_lock);
1468 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001469 pr_debug("i2c-core: attempting to delete unregistered "
1470 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001471 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
Jean Delvare026526f2008-01-27 18:14:49 +01001474 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001475 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001476 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001477 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001478 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001480 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001481 mutex_lock_nested(&adap->userspace_clients_lock,
1482 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001483 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1484 detected) {
1485 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1486 client->addr);
1487 list_del(&client->detected);
1488 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001489 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001490 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001491
Jean Delvaree549c2b2009-06-19 16:58:19 +02001492 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001493 * check the returned value. This is a two-pass process, because
1494 * we can't remove the dummy devices during the first pass: they
1495 * could have been instantiated by real devices wishing to clean
1496 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001497 device_for_each_child(&adap->dev, NULL, __unregister_client);
1498 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jean Delvare2bb50952009-09-18 22:45:46 +02001500#ifdef CONFIG_I2C_COMPAT
1501 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1502 adap->dev.parent);
1503#endif
1504
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001505 /* device name is gone after device_unregister */
1506 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* clean up the sysfs representation */
1509 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* wait for sysfs to drop all references */
1513 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
David Brownell6e13e642007-05-01 23:26:31 +02001515 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001516 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001518 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001520 /* Clear the device structure in case this adapter is ever going to be
1521 added again */
1522 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
David Brownellc0564602007-05-01 23:26:31 +02001524EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
David Brownell7b4fbc52007-05-01 23:26:30 +02001526/* ------------------------------------------------------------------------- */
1527
Jean Delvare7ae31482011-03-20 14:50:52 +01001528int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1529{
1530 int res;
1531
1532 mutex_lock(&core_lock);
1533 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1534 mutex_unlock(&core_lock);
1535
1536 return res;
1537}
1538EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1539
Jean Delvare69b00892009-12-06 17:06:27 +01001540static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001541{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001542 if (dev->type != &i2c_adapter_type)
1543 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001544 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001545}
1546
David Brownell7b4fbc52007-05-01 23:26:30 +02001547/*
1548 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001549 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 */
1551
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001552int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001554 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
David Brownell1d0b19c2008-10-14 17:30:05 +02001556 /* Can't register until after driver model init */
1557 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1558 return -EAGAIN;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001561 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Jean Delvare729d6dd2009-06-19 16:58:18 +02001564 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001565 * will have called probe() for all matching-but-unbound devices.
1566 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 res = driver_register(&driver->driver);
1568 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001569 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001570
Mark Brownf4e8db32011-01-14 22:03:50 +01001571 /* Drivers should switch to dev_pm_ops instead. */
1572 if (driver->suspend)
1573 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1574 driver->driver.name);
1575 if (driver->resume)
1576 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1577 driver->driver.name);
1578
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001579 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Jean Delvare4735c982008-07-14 22:38:36 +02001581 INIT_LIST_HEAD(&driver->clients);
1582 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001583 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001584
Jean Delvare7eebcb72006-02-05 23:28:21 +01001585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001587EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jean Delvare69b00892009-12-06 17:06:27 +01001589static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001590{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001591 if (dev->type == &i2c_adapter_type)
1592 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1593 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001594}
1595
David Brownella1d9e6e2007-05-01 23:26:30 +02001596/**
1597 * i2c_del_driver - unregister I2C driver
1598 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001599 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001600 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001601void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Jean Delvare7ae31482011-03-20 14:50:52 +01001603 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001606 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
David Brownellc0564602007-05-01 23:26:31 +02001608EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David Brownell7b4fbc52007-05-01 23:26:30 +02001610/* ------------------------------------------------------------------------- */
1611
Jean Delvaree48d3312008-01-27 18:14:48 +01001612/**
1613 * i2c_use_client - increments the reference count of the i2c client structure
1614 * @client: the client being referenced
1615 *
1616 * Each live reference to a client should be refcounted. The driver model does
1617 * that automatically as part of driver binding, so that most drivers don't
1618 * need to do this explicitly: they hold a reference until they're unbound
1619 * from the device.
1620 *
1621 * A pointer to the client with the incremented reference counter is returned.
1622 */
1623struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
David Brownell6ea438e2008-07-14 22:38:24 +02001625 if (client && get_device(&client->dev))
1626 return client;
1627 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
David Brownellc0564602007-05-01 23:26:31 +02001629EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Jean Delvaree48d3312008-01-27 18:14:48 +01001631/**
1632 * i2c_release_client - release a use of the i2c client structure
1633 * @client: the client being no longer referenced
1634 *
1635 * Must be called when a user of a client is finished with it.
1636 */
1637void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
David Brownell6ea438e2008-07-14 22:38:24 +02001639 if (client)
1640 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
David Brownellc0564602007-05-01 23:26:31 +02001642EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
David Brownell9b766b82008-01-27 18:14:51 +01001644struct i2c_cmd_arg {
1645 unsigned cmd;
1646 void *arg;
1647};
1648
1649static int i2c_cmd(struct device *dev, void *_arg)
1650{
1651 struct i2c_client *client = i2c_verify_client(dev);
1652 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001653 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001654
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001655 if (!client || !client->dev.driver)
1656 return 0;
1657
1658 driver = to_i2c_driver(client->dev.driver);
1659 if (driver->command)
1660 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001661 return 0;
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1665{
David Brownell9b766b82008-01-27 18:14:51 +01001666 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David Brownell9b766b82008-01-27 18:14:51 +01001668 cmd_arg.cmd = cmd;
1669 cmd_arg.arg = arg;
1670 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
David Brownellc0564602007-05-01 23:26:31 +02001672EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674static int __init i2c_init(void)
1675{
1676 int retval;
1677
1678 retval = bus_register(&i2c_bus_type);
1679 if (retval)
1680 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001681#ifdef CONFIG_I2C_COMPAT
1682 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1683 if (!i2c_adapter_compat_class) {
1684 retval = -ENOMEM;
1685 goto bus_err;
1686 }
1687#endif
David Brownelle9f13732008-01-27 18:14:52 +01001688 retval = i2c_add_driver(&dummy_driver);
1689 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001690 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001691 return 0;
1692
Jean Delvare2bb50952009-09-18 22:45:46 +02001693class_err:
1694#ifdef CONFIG_I2C_COMPAT
1695 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001696bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001697#endif
David Brownelle9f13732008-01-27 18:14:52 +01001698 bus_unregister(&i2c_bus_type);
1699 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702static void __exit i2c_exit(void)
1703{
David Brownelle9f13732008-01-27 18:14:52 +01001704 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001705#ifdef CONFIG_I2C_COMPAT
1706 class_compat_unregister(i2c_adapter_compat_class);
1707#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001709 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
David Brownella10f9e72008-10-14 17:30:06 +02001712/* We must initialize early, because some subsystems register i2c drivers
1713 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1714 */
1715postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716module_exit(i2c_exit);
1717
1718/* ----------------------------------------------------
1719 * the functional interface to the i2c busses.
1720 * ----------------------------------------------------
1721 */
1722
David Brownella1cdeda2008-07-14 22:38:24 +02001723/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001724 * __i2c_transfer - unlocked flavor of i2c_transfer
1725 * @adap: Handle to I2C bus
1726 * @msgs: One or more messages to execute before STOP is issued to
1727 * terminate the operation; each message begins with a START.
1728 * @num: Number of messages to be executed.
1729 *
1730 * Returns negative errno, else the number of messages executed.
1731 *
1732 * Adapter lock must be held when calling this function. No debug logging
1733 * takes place. adap->algo->master_xfer existence isn't checked.
1734 */
1735int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1736{
1737 unsigned long orig_jiffies;
1738 int ret, try;
1739
David Howellsd9a83d62014-03-06 13:35:59 +00001740 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1741 * enabled. This is an efficient way of keeping the for-loop from
1742 * being executed when not needed.
1743 */
1744 if (static_key_false(&i2c_trace_msg)) {
1745 int i;
1746 for (i = 0; i < num; i++)
1747 if (msgs[i].flags & I2C_M_RD)
1748 trace_i2c_read(adap, &msgs[i], i);
1749 else
1750 trace_i2c_write(adap, &msgs[i], i);
1751 }
1752
Jean Delvareb37d2a32012-06-29 07:47:19 -03001753 /* Retry automatically on arbitration loss */
1754 orig_jiffies = jiffies;
1755 for (ret = 0, try = 0; try <= adap->retries; try++) {
1756 ret = adap->algo->master_xfer(adap, msgs, num);
1757 if (ret != -EAGAIN)
1758 break;
1759 if (time_after(jiffies, orig_jiffies + adap->timeout))
1760 break;
1761 }
1762
David Howellsd9a83d62014-03-06 13:35:59 +00001763 if (static_key_false(&i2c_trace_msg)) {
1764 int i;
1765 for (i = 0; i < ret; i++)
1766 if (msgs[i].flags & I2C_M_RD)
1767 trace_i2c_reply(adap, &msgs[i], i);
1768 trace_i2c_result(adap, i, ret);
1769 }
1770
Jean Delvareb37d2a32012-06-29 07:47:19 -03001771 return ret;
1772}
1773EXPORT_SYMBOL(__i2c_transfer);
1774
1775/**
David Brownella1cdeda2008-07-14 22:38:24 +02001776 * i2c_transfer - execute a single or combined I2C message
1777 * @adap: Handle to I2C bus
1778 * @msgs: One or more messages to execute before STOP is issued to
1779 * terminate the operation; each message begins with a START.
1780 * @num: Number of messages to be executed.
1781 *
1782 * Returns negative errno, else the number of messages executed.
1783 *
1784 * Note that there is no requirement that each message be sent to
1785 * the same slave address, although that is the most common model.
1786 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001787int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001789 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
David Brownella1cdeda2008-07-14 22:38:24 +02001791 /* REVISIT the fault reporting model here is weak:
1792 *
1793 * - When we get an error after receiving N bytes from a slave,
1794 * there is no way to report "N".
1795 *
1796 * - When we get a NAK after transmitting N bytes to a slave,
1797 * there is no way to report "N" ... or to let the master
1798 * continue executing the rest of this combined message, if
1799 * that's the appropriate response.
1800 *
1801 * - When for example "num" is two and we successfully complete
1802 * the first message but get an error part way through the
1803 * second, it's unclear whether that should be reported as
1804 * one (discarding status on the second message) or errno
1805 * (discarding status on the first one).
1806 */
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (adap->algo->master_xfer) {
1809#ifdef DEBUG
1810 for (ret = 0; ret < num; ret++) {
1811 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001812 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1813 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1814 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 }
1816#endif
1817
Mike Rapoportcea443a2008-01-27 18:14:50 +01001818 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001819 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001820 if (!ret)
1821 /* I2C activity is ongoing. */
1822 return -EAGAIN;
1823 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001824 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001825 }
1826
Jean Delvareb37d2a32012-06-29 07:47:19 -03001827 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001828 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 return ret;
1831 } else {
1832 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001833 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835}
David Brownellc0564602007-05-01 23:26:31 +02001836EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
David Brownella1cdeda2008-07-14 22:38:24 +02001838/**
1839 * i2c_master_send - issue a single I2C message in master transmit mode
1840 * @client: Handle to slave device
1841 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001842 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001843 *
1844 * Returns negative errno, or else the number of bytes written.
1845 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001846int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001849 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct i2c_msg msg;
1851
Jean Delvare815f55f2005-05-07 22:58:46 +02001852 msg.addr = client->addr;
1853 msg.flags = client->flags & I2C_M_TEN;
1854 msg.len = count;
1855 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001856
Jean Delvare815f55f2005-05-07 22:58:46 +02001857 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001859 /*
1860 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1861 * transmitted, else error code.
1862 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001863 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
David Brownellc0564602007-05-01 23:26:31 +02001865EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
David Brownella1cdeda2008-07-14 22:38:24 +02001867/**
1868 * i2c_master_recv - issue a single I2C message in master receive mode
1869 * @client: Handle to slave device
1870 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001871 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001872 *
1873 * Returns negative errno, or else the number of bytes read.
1874 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001875int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
Farid Hammane7225acf2010-05-21 18:40:58 +02001877 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct i2c_msg msg;
1879 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Jean Delvare815f55f2005-05-07 22:58:46 +02001881 msg.addr = client->addr;
1882 msg.flags = client->flags & I2C_M_TEN;
1883 msg.flags |= I2C_M_RD;
1884 msg.len = count;
1885 msg.buf = buf;
1886
1887 ret = i2c_transfer(adap, &msg, 1);
1888
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001889 /*
1890 * If everything went ok (i.e. 1 msg received), return #bytes received,
1891 * else error code.
1892 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001893 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
David Brownellc0564602007-05-01 23:26:31 +02001895EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/* ----------------------------------------------------
1898 * the i2c address scanning function
1899 * Will not work for 10-bit addresses!
1900 * ----------------------------------------------------
1901 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001902
Jean Delvare63e4e802010-06-03 11:33:51 +02001903/*
1904 * Legacy default probe function, mostly relevant for SMBus. The default
1905 * probe method is a quick write, but it is known to corrupt the 24RF08
1906 * EEPROMs due to a state machine bug, and could also irreversibly
1907 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1908 * we use a short byte read instead. Also, some bus drivers don't implement
1909 * quick write, so we fallback to a byte read in that case too.
1910 * On x86, there is another special case for FSC hardware monitoring chips,
1911 * which want regular byte reads (address 0x73.) Fortunately, these are the
1912 * only known chips using this I2C address on PC hardware.
1913 * Returns 1 if probe succeeded, 0 if not.
1914 */
1915static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1916{
1917 int err;
1918 union i2c_smbus_data dummy;
1919
1920#ifdef CONFIG_X86
1921 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1922 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1923 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1924 I2C_SMBUS_BYTE_DATA, &dummy);
1925 else
1926#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001927 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1928 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001929 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1930 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001931 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1932 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1933 I2C_SMBUS_BYTE, &dummy);
1934 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001935 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1936 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001937 err = -EOPNOTSUPP;
1938 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001939
1940 return err >= 0;
1941}
1942
Jean Delvareccfbbd02009-12-06 17:06:25 +01001943static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001944 struct i2c_driver *driver)
1945{
1946 struct i2c_board_info info;
1947 struct i2c_adapter *adapter = temp_client->adapter;
1948 int addr = temp_client->addr;
1949 int err;
1950
1951 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001952 err = i2c_check_addr_validity(addr);
1953 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001954 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1955 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001956 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001957 }
1958
1959 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001960 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001961 return 0;
1962
Jean Delvareccfbbd02009-12-06 17:06:25 +01001963 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001964 if (!i2c_default_probe(adapter, addr))
1965 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001966
1967 /* Finally call the custom detection function */
1968 memset(&info, 0, sizeof(struct i2c_board_info));
1969 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001970 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001971 if (err) {
1972 /* -ENODEV is returned if the detection fails. We catch it
1973 here as this isn't an error. */
1974 return err == -ENODEV ? 0 : err;
1975 }
1976
1977 /* Consistency check */
1978 if (info.type[0] == '\0') {
1979 dev_err(&adapter->dev, "%s detection function provided "
1980 "no name for 0x%x\n", driver->driver.name,
1981 addr);
1982 } else {
1983 struct i2c_client *client;
1984
1985 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01001986 if (adapter->class & I2C_CLASS_DEPRECATED)
1987 dev_warn(&adapter->dev,
1988 "This adapter will soon drop class based instantiation of devices. "
1989 "Please make sure client 0x%02x gets instantiated by other means. "
1990 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
1991 info.addr);
1992
Jean Delvare4735c982008-07-14 22:38:36 +02001993 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1994 info.type, info.addr);
1995 client = i2c_new_device(adapter, &info);
1996 if (client)
1997 list_add_tail(&client->detected, &driver->clients);
1998 else
1999 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2000 info.type, info.addr);
2001 }
2002 return 0;
2003}
2004
2005static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2006{
Jean Delvarec3813d62009-12-14 21:17:25 +01002007 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002008 struct i2c_client *temp_client;
2009 int i, err = 0;
2010 int adap_id = i2c_adapter_id(adapter);
2011
Jean Delvarec3813d62009-12-14 21:17:25 +01002012 address_list = driver->address_list;
2013 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002014 return 0;
2015
Jean Delvare51b54ba2010-10-24 18:16:58 +02002016 /* Stop here if the classes do not match */
2017 if (!(adapter->class & driver->class))
2018 return 0;
2019
Jean Delvare4735c982008-07-14 22:38:36 +02002020 /* Set up a temporary client to help detect callback */
2021 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2022 if (!temp_client)
2023 return -ENOMEM;
2024 temp_client->adapter = adapter;
2025
Jean Delvarec3813d62009-12-14 21:17:25 +01002026 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02002027 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01002028 "addr 0x%02x\n", adap_id, address_list[i]);
2029 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002030 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002031 if (unlikely(err))
2032 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002033 }
2034
Jean Delvare4735c982008-07-14 22:38:36 +02002035 kfree(temp_client);
2036 return err;
2037}
2038
Jean Delvared44f19d2010-08-11 18:20:57 +02002039int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2040{
2041 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2042 I2C_SMBUS_QUICK, NULL) >= 0;
2043}
2044EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2045
Jean Delvare12b5053a2007-05-01 23:26:31 +02002046struct i2c_client *
2047i2c_new_probed_device(struct i2c_adapter *adap,
2048 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002049 unsigned short const *addr_list,
2050 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002051{
2052 int i;
2053
Jean Delvare8031d792010-08-11 18:21:00 +02002054 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002055 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002056
Jean Delvare12b5053a2007-05-01 23:26:31 +02002057 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2058 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002059 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002060 dev_warn(&adap->dev, "Invalid 7-bit address "
2061 "0x%02x\n", addr_list[i]);
2062 continue;
2063 }
2064
2065 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002066 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002067 dev_dbg(&adap->dev, "Address 0x%02x already in "
2068 "use, not probing\n", addr_list[i]);
2069 continue;
2070 }
2071
Jean Delvare63e4e802010-06-03 11:33:51 +02002072 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002073 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002074 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002075 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002076
2077 if (addr_list[i] == I2C_CLIENT_END) {
2078 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2079 return NULL;
2080 }
2081
2082 info->addr = addr_list[i];
2083 return i2c_new_device(adap, info);
2084}
2085EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2086
Jean Delvared735b342011-03-20 14:50:52 +01002087struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002090
Jean Delvarecaada322008-01-27 18:14:49 +01002091 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002092 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002093 if (adapter && !try_module_get(adapter->owner))
2094 adapter = NULL;
2095
Jean Delvarecaada322008-01-27 18:14:49 +01002096 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002097 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098}
David Brownellc0564602007-05-01 23:26:31 +02002099EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101void i2c_put_adapter(struct i2c_adapter *adap)
2102{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002103 if (adap)
2104 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
David Brownellc0564602007-05-01 23:26:31 +02002106EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108/* The SMBus parts */
2109
David Brownell438d6c22006-12-10 21:21:31 +01002110#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002111static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002114
Farid Hammane7225acf2010-05-21 18:40:58 +02002115 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002116 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 data = data ^ POLY;
2118 data = data << 1;
2119 }
2120 return (u8)(data >> 8);
2121}
2122
Jean Delvare421ef472005-10-26 21:28:55 +02002123/* Incremental CRC8 over count bytes in the array pointed to by p */
2124static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 int i;
2127
Farid Hammane7225acf2010-05-21 18:40:58 +02002128 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002129 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return crc;
2131}
2132
Jean Delvare421ef472005-10-26 21:28:55 +02002133/* Assume a 7-bit address, which is reasonable for SMBus */
2134static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Jean Delvare421ef472005-10-26 21:28:55 +02002136 /* The address will be sent first */
2137 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2138 pec = i2c_smbus_pec(pec, &addr, 1);
2139
2140 /* The data buffer follows */
2141 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Jean Delvare421ef472005-10-26 21:28:55 +02002144/* Used for write only transactions */
2145static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Jean Delvare421ef472005-10-26 21:28:55 +02002147 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2148 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Jean Delvare421ef472005-10-26 21:28:55 +02002151/* Return <0 on CRC error
2152 If there was a write before this read (most cases) we need to take the
2153 partial CRC from the write part into account.
2154 Note that this function does modify the message (we need to decrease the
2155 message length to hide the CRC byte from the caller). */
2156static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Jean Delvare421ef472005-10-26 21:28:55 +02002158 u8 rpec = msg->buf[--msg->len];
2159 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (rpec != cpec) {
2162 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2163 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002164 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
David Brownell438d6c22006-12-10 21:21:31 +01002166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167}
2168
David Brownella1cdeda2008-07-14 22:38:24 +02002169/**
2170 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2171 * @client: Handle to slave device
2172 *
2173 * This executes the SMBus "receive byte" protocol, returning negative errno
2174 * else the byte received from the device.
2175 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002176s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002179 int status;
2180
2181 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2182 I2C_SMBUS_READ, 0,
2183 I2C_SMBUS_BYTE, &data);
2184 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
David Brownellc0564602007-05-01 23:26:31 +02002186EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
David Brownella1cdeda2008-07-14 22:38:24 +02002188/**
2189 * i2c_smbus_write_byte - SMBus "send byte" protocol
2190 * @client: Handle to slave device
2191 * @value: Byte to be sent
2192 *
2193 * This executes the SMBus "send byte" protocol, returning negative errno
2194 * else zero on success.
2195 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002196s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Farid Hammane7225acf2010-05-21 18:40:58 +02002198 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002199 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
David Brownellc0564602007-05-01 23:26:31 +02002201EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
David Brownella1cdeda2008-07-14 22:38:24 +02002203/**
2204 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2205 * @client: Handle to slave device
2206 * @command: Byte interpreted by slave
2207 *
2208 * This executes the SMBus "read byte" protocol, returning negative errno
2209 * else a data byte received from the device.
2210 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002211s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002214 int status;
2215
2216 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2217 I2C_SMBUS_READ, command,
2218 I2C_SMBUS_BYTE_DATA, &data);
2219 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
David Brownellc0564602007-05-01 23:26:31 +02002221EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
David Brownella1cdeda2008-07-14 22:38:24 +02002223/**
2224 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2225 * @client: Handle to slave device
2226 * @command: Byte interpreted by slave
2227 * @value: Byte being written
2228 *
2229 * This executes the SMBus "write byte" protocol, returning negative errno
2230 * else zero on success.
2231 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002232s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2233 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 union i2c_smbus_data data;
2236 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002237 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2238 I2C_SMBUS_WRITE, command,
2239 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
David Brownellc0564602007-05-01 23:26:31 +02002241EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
David Brownella1cdeda2008-07-14 22:38:24 +02002243/**
2244 * i2c_smbus_read_word_data - SMBus "read word" protocol
2245 * @client: Handle to slave device
2246 * @command: Byte interpreted by slave
2247 *
2248 * This executes the SMBus "read word" protocol, returning negative errno
2249 * else a 16-bit unsigned "word" received from the device.
2250 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002251s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002254 int status;
2255
2256 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2257 I2C_SMBUS_READ, command,
2258 I2C_SMBUS_WORD_DATA, &data);
2259 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
David Brownellc0564602007-05-01 23:26:31 +02002261EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
David Brownella1cdeda2008-07-14 22:38:24 +02002263/**
2264 * i2c_smbus_write_word_data - SMBus "write word" protocol
2265 * @client: Handle to slave device
2266 * @command: Byte interpreted by slave
2267 * @value: 16-bit "word" being written
2268 *
2269 * This executes the SMBus "write word" protocol, returning negative errno
2270 * else zero on success.
2271 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002272s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2273 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274{
2275 union i2c_smbus_data data;
2276 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002277 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2278 I2C_SMBUS_WRITE, command,
2279 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
David Brownellc0564602007-05-01 23:26:31 +02002281EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
David Brownella64ec072007-10-13 23:56:31 +02002283/**
David Brownella1cdeda2008-07-14 22:38:24 +02002284 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002285 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002286 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002287 * @values: Byte array into which data will be read; big enough to hold
2288 * the data returned by the slave. SMBus allows at most 32 bytes.
2289 *
David Brownella1cdeda2008-07-14 22:38:24 +02002290 * This executes the SMBus "block read" protocol, returning negative errno
2291 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002292 *
2293 * Note that using this function requires that the client's adapter support
2294 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2295 * support this; its emulation through I2C messaging relies on a specific
2296 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2297 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002298s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002299 u8 *values)
2300{
2301 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002302 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002303
David Brownell24a5bb72008-07-14 22:38:23 +02002304 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2305 I2C_SMBUS_READ, command,
2306 I2C_SMBUS_BLOCK_DATA, &data);
2307 if (status)
2308 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002309
2310 memcpy(values, &data.block[1], data.block[0]);
2311 return data.block[0];
2312}
2313EXPORT_SYMBOL(i2c_smbus_read_block_data);
2314
David Brownella1cdeda2008-07-14 22:38:24 +02002315/**
2316 * i2c_smbus_write_block_data - SMBus "block write" protocol
2317 * @client: Handle to slave device
2318 * @command: Byte interpreted by slave
2319 * @length: Size of data block; SMBus allows at most 32 bytes
2320 * @values: Byte array which will be written.
2321 *
2322 * This executes the SMBus "block write" protocol, returning negative errno
2323 * else zero on success.
2324 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002325s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002326 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (length > I2C_SMBUS_BLOCK_MAX)
2331 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002333 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002334 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2335 I2C_SMBUS_WRITE, command,
2336 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
David Brownellc0564602007-05-01 23:26:31 +02002338EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
2340/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002341s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002342 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
2344 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002345 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002346
Jean Delvare4b2643d2007-07-12 14:12:29 +02002347 if (length > I2C_SMBUS_BLOCK_MAX)
2348 length = I2C_SMBUS_BLOCK_MAX;
2349 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002350 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2351 I2C_SMBUS_READ, command,
2352 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2353 if (status < 0)
2354 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002355
2356 memcpy(values, &data.block[1], data.block[0]);
2357 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
David Brownellc0564602007-05-01 23:26:31 +02002359EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Jean Delvare0cc43a12011-01-10 22:11:23 +01002361s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002362 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002363{
2364 union i2c_smbus_data data;
2365
2366 if (length > I2C_SMBUS_BLOCK_MAX)
2367 length = I2C_SMBUS_BLOCK_MAX;
2368 data.block[0] = length;
2369 memcpy(data.block + 1, values, length);
2370 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2371 I2C_SMBUS_WRITE, command,
2372 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2373}
David Brownellc0564602007-05-01 23:26:31 +02002374EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002375
David Brownell438d6c22006-12-10 21:21:31 +01002376/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002378static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2379 unsigned short flags,
2380 char read_write, u8 command, int size,
2381 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 /* So we need to generate a series of msgs. In the case of writing, we
2384 need to use only one message; when reading, we need two. We initialize
2385 most things with sane defaults, to keep the code below somewhat
2386 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002387 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2388 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002389 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002391 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002392 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002393 struct i2c_msg msg[2] = {
2394 {
2395 .addr = addr,
2396 .flags = flags,
2397 .len = 1,
2398 .buf = msgbuf0,
2399 }, {
2400 .addr = addr,
2401 .flags = flags | I2C_M_RD,
2402 .len = 0,
2403 .buf = msgbuf1,
2404 },
2405 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002408 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 case I2C_SMBUS_QUICK:
2410 msg[0].len = 0;
2411 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002412 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2413 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 num = 1;
2415 break;
2416 case I2C_SMBUS_BYTE:
2417 if (read_write == I2C_SMBUS_READ) {
2418 /* Special case: only a read! */
2419 msg[0].flags = I2C_M_RD | flags;
2420 num = 1;
2421 }
2422 break;
2423 case I2C_SMBUS_BYTE_DATA:
2424 if (read_write == I2C_SMBUS_READ)
2425 msg[1].len = 1;
2426 else {
2427 msg[0].len = 2;
2428 msgbuf0[1] = data->byte;
2429 }
2430 break;
2431 case I2C_SMBUS_WORD_DATA:
2432 if (read_write == I2C_SMBUS_READ)
2433 msg[1].len = 2;
2434 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002435 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002437 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439 break;
2440 case I2C_SMBUS_PROC_CALL:
2441 num = 2; /* Special case */
2442 read_write = I2C_SMBUS_READ;
2443 msg[0].len = 3;
2444 msg[1].len = 2;
2445 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002446 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 break;
2448 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002450 msg[1].flags |= I2C_M_RECV_LEN;
2451 msg[1].len = 1; /* block length will be added by
2452 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 } else {
2454 msg[0].len = data->block[0] + 2;
2455 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002456 dev_err(&adapter->dev,
2457 "Invalid block write size %d\n",
2458 data->block[0]);
2459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002461 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 msgbuf0[i] = data->block[i-1];
2463 }
2464 break;
2465 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002466 num = 2; /* Another special case */
2467 read_write = I2C_SMBUS_READ;
2468 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002469 dev_err(&adapter->dev,
2470 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002471 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002472 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002473 }
2474 msg[0].len = data->block[0] + 2;
2475 for (i = 1; i < msg[0].len; i++)
2476 msgbuf0[i] = data->block[i-1];
2477 msg[1].flags |= I2C_M_RECV_LEN;
2478 msg[1].len = 1; /* block length will be added by
2479 the underlying bus driver */
2480 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 case I2C_SMBUS_I2C_BLOCK_DATA:
2482 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002483 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 } else {
2485 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002486 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002487 dev_err(&adapter->dev,
2488 "Invalid block write size %d\n",
2489 data->block[0]);
2490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492 for (i = 1; i <= data->block[0]; i++)
2493 msgbuf0[i] = data->block[i];
2494 }
2495 break;
2496 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002497 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2498 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500
Jean Delvare421ef472005-10-26 21:28:55 +02002501 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2502 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2503 if (i) {
2504 /* Compute PEC if first message is a write */
2505 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002506 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002507 i2c_smbus_add_pec(&msg[0]);
2508 else /* Write followed by read */
2509 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2510 }
2511 /* Ask for PEC if last message is a read */
2512 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002513 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002514 }
2515
David Brownell24a5bb72008-07-14 22:38:23 +02002516 status = i2c_transfer(adapter, msg, num);
2517 if (status < 0)
2518 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Jean Delvare421ef472005-10-26 21:28:55 +02002520 /* Check PEC if last message is a read */
2521 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002522 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2523 if (status < 0)
2524 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002525 }
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002528 switch (size) {
2529 case I2C_SMBUS_BYTE:
2530 data->byte = msgbuf0[0];
2531 break;
2532 case I2C_SMBUS_BYTE_DATA:
2533 data->byte = msgbuf1[0];
2534 break;
2535 case I2C_SMBUS_WORD_DATA:
2536 case I2C_SMBUS_PROC_CALL:
2537 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2538 break;
2539 case I2C_SMBUS_I2C_BLOCK_DATA:
2540 for (i = 0; i < data->block[0]; i++)
2541 data->block[i+1] = msgbuf1[i];
2542 break;
2543 case I2C_SMBUS_BLOCK_DATA:
2544 case I2C_SMBUS_BLOCK_PROC_CALL:
2545 for (i = 0; i < msgbuf1[0] + 1; i++)
2546 data->block[i] = msgbuf1[i];
2547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
2549 return 0;
2550}
2551
David Brownella1cdeda2008-07-14 22:38:24 +02002552/**
2553 * i2c_smbus_xfer - execute SMBus protocol operations
2554 * @adapter: Handle to I2C bus
2555 * @addr: Address of SMBus slave on that bus
2556 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2557 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2558 * @command: Byte interpreted by slave, for protocols which use such bytes
2559 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2560 * @data: Data to be read or written
2561 *
2562 * This executes an SMBus protocol operation, and returns a negative
2563 * errno code else zero on success.
2564 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002565s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002566 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002567 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002569 unsigned long orig_jiffies;
2570 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
David Howells8a325992014-03-06 13:36:06 +00002573 /* If enabled, the following two tracepoints are conditional on
2574 * read_write and protocol.
2575 */
2576 trace_smbus_write(adapter, addr, flags, read_write,
2577 command, protocol, data);
2578 trace_smbus_read(adapter, addr, flags, read_write,
2579 command, protocol);
2580
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002581 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002584 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002585
2586 /* Retry automatically on arbitration loss */
2587 orig_jiffies = jiffies;
2588 for (res = 0, try = 0; try <= adapter->retries; try++) {
2589 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2590 read_write, command,
2591 protocol, data);
2592 if (res != -EAGAIN)
2593 break;
2594 if (time_after(jiffies,
2595 orig_jiffies + adapter->timeout))
2596 break;
2597 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002598 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002600 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
David Howells8a325992014-03-06 13:36:06 +00002601 goto trace;
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002602 /*
2603 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2604 * implement native support for the SMBus operation.
2605 */
2606 }
2607
David Howells8a325992014-03-06 13:36:06 +00002608 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2609 command, protocol, data);
2610
2611trace:
2612 /* If enabled, the reply tracepoint is conditional on read_write. */
2613 trace_smbus_reply(adapter, addr, flags, read_write,
2614 command, protocol, data);
2615 trace_smbus_result(adapter, addr, flags, read_write,
2616 command, protocol, res);
2617
2618 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2623MODULE_DESCRIPTION("I2C-Bus main module");
2624MODULE_LICENSE("GPL");