blob: 5fb80b8962a2ad7d8e78dcee01df68cb639d3602 [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>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a2008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jean Delvare6629dcf2010-05-04 11:09:28 +020056/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020057 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000058 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010059static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_IDR(i2c_adapter_idr);
61
Jean Delvare4f8cf822009-09-18 22:45:46 +020062static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020063static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010064
65/* ------------------------------------------------------------------------- */
66
Jean Delvared2653e92008-04-29 23:11:39 +020067static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
68 const struct i2c_client *client)
69{
70 while (id->name[0]) {
71 if (strcmp(client->name, id->name) == 0)
72 return id;
73 id++;
74 }
75 return NULL;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int i2c_device_match(struct device *dev, struct device_driver *drv)
79{
Jean Delvare51298d12009-09-18 22:45:45 +020080 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020082
Jean Delvare51298d12009-09-18 22:45:45 +020083 if (!client)
84 return 0;
85
Grant Likely959e85f2010-06-08 07:48:19 -060086 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
88 return 1;
89
Mika Westerberg907ddf82012-11-23 12:23:40 +010090 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
93
Jean Delvare51298d12009-09-18 22:45:45 +020094 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020095 /* match on an id table if there is one */
96 if (driver->id_table)
97 return i2c_match_id(driver->id_table, client) != NULL;
98
Jean Delvareeb8a7902008-05-18 20:49:41 +020099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
103/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105{
106 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800107 int rc;
108
109 rc = acpi_device_uevent_modalias(dev, env);
110 if (rc != -ENODEV)
111 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200112
Jean Delvareeb8a7902008-05-18 20:49:41 +0200113 if (add_uevent_var(env, "MODALIAS=%s%s",
114 I2C_MODULE_PREFIX, client->name))
115 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200116 dev_dbg(dev, "uevent\n");
117 return 0;
118}
119
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530120/* i2c bus recovery routines */
121static int get_scl_gpio_value(struct i2c_adapter *adap)
122{
123 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
124}
125
126static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
127{
128 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
129}
130
131static int get_sda_gpio_value(struct i2c_adapter *adap)
132{
133 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
134}
135
136static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
137{
138 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
139 struct device *dev = &adap->dev;
140 int ret = 0;
141
142 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
143 GPIOF_OUT_INIT_HIGH, "i2c-scl");
144 if (ret) {
145 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
146 return ret;
147 }
148
149 if (bri->get_sda) {
150 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
151 /* work without SDA polling */
152 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
153 bri->sda_gpio);
154 bri->get_sda = NULL;
155 }
156 }
157
158 return ret;
159}
160
161static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
162{
163 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
164
165 if (bri->get_sda)
166 gpio_free(bri->sda_gpio);
167
168 gpio_free(bri->scl_gpio);
169}
170
171/*
172 * We are generating clock pulses. ndelay() determines durating of clk pulses.
173 * We will generate clock with rate 100 KHz and so duration of both clock levels
174 * is: delay in ns = (10^6 / 100) / 2
175 */
176#define RECOVERY_NDELAY 5000
177#define RECOVERY_CLK_CNT 9
178
179static int i2c_generic_recovery(struct i2c_adapter *adap)
180{
181 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
182 int i = 0, val = 1, ret = 0;
183
184 if (bri->prepare_recovery)
185 bri->prepare_recovery(bri);
186
187 /*
188 * By this time SCL is high, as we need to give 9 falling-rising edges
189 */
190 while (i++ < RECOVERY_CLK_CNT * 2) {
191 if (val) {
192 /* Break if SDA is high */
193 if (bri->get_sda && bri->get_sda(adap))
194 break;
195 /* SCL shouldn't be low here */
196 if (!bri->get_scl(adap)) {
197 dev_err(&adap->dev,
198 "SCL is stuck low, exit recovery\n");
199 ret = -EBUSY;
200 break;
201 }
202 }
203
204 val = !val;
205 bri->set_scl(adap, val);
206 ndelay(RECOVERY_NDELAY);
207 }
208
209 if (bri->unprepare_recovery)
210 bri->unprepare_recovery(bri);
211
212 return ret;
213}
214
215int i2c_generic_scl_recovery(struct i2c_adapter *adap)
216{
217 adap->bus_recovery_info->set_scl(adap, 1);
218 return i2c_generic_recovery(adap);
219}
220
221int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
222{
223 int ret;
224
225 ret = i2c_get_gpios_for_recovery(adap);
226 if (ret)
227 return ret;
228
229 ret = i2c_generic_recovery(adap);
230 i2c_put_gpios_for_recovery(adap);
231
232 return ret;
233}
234
235int i2c_recover_bus(struct i2c_adapter *adap)
236{
237 if (!adap->bus_recovery_info)
238 return -EOPNOTSUPP;
239
240 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
241 return adap->bus_recovery_info->recover_bus(adap);
242}
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static int i2c_device_probe(struct device *dev)
245{
Jean Delvare51298d12009-09-18 22:45:45 +0200246 struct i2c_client *client = i2c_verify_client(dev);
247 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100248 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200249
Jean Delvare51298d12009-09-18 22:45:45 +0200250 if (!client)
251 return 0;
252
253 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200254 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200255 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200256
Marc Pignatee354252008-08-28 08:33:22 +0200257 if (!device_can_wakeup(&client->dev))
258 device_init_wakeup(&client->dev,
259 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200260 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200261
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300262 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200263 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sang72fa8182014-01-21 17:48:34 +0100264 if (status)
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300265 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sang72fa8182014-01-21 17:48:34 +0100266
Hans Verkuil50c33042008-03-12 14:15:00 +0100267 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270static int i2c_device_remove(struct device *dev)
271{
Jean Delvare51298d12009-09-18 22:45:45 +0200272 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200273 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100274 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200275
Jean Delvare51298d12009-09-18 22:45:45 +0200276 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200277 return 0;
278
279 driver = to_i2c_driver(dev->driver);
280 if (driver->remove) {
281 dev_dbg(dev, "remove\n");
282 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200283 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100284
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300285 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200286 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
David Brownellf37dd802007-02-13 22:09:00 +0100289static void i2c_device_shutdown(struct device *dev)
290{
Jean Delvare51298d12009-09-18 22:45:45 +0200291 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100292 struct i2c_driver *driver;
293
Jean Delvare51298d12009-09-18 22:45:45 +0200294 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100295 return;
296 driver = to_i2c_driver(dev->driver);
297 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200298 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100299}
300
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200301#ifdef CONFIG_PM_SLEEP
302static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100303{
Jean Delvare51298d12009-09-18 22:45:45 +0200304 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100305 struct i2c_driver *driver;
306
Jean Delvare51298d12009-09-18 22:45:45 +0200307 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100308 return 0;
309 driver = to_i2c_driver(dev->driver);
310 if (!driver->suspend)
311 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200312 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100313}
314
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200315static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100316{
Jean Delvare51298d12009-09-18 22:45:45 +0200317 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100318 struct i2c_driver *driver;
319
Jean Delvare51298d12009-09-18 22:45:45 +0200320 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100321 return 0;
322 driver = to_i2c_driver(dev->driver);
323 if (!driver->resume)
324 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200325 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100326}
327
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200328static int i2c_device_pm_suspend(struct device *dev)
329{
330 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
331
Mark Brownd529de22011-01-14 22:03:49 +0100332 if (pm)
333 return pm_generic_suspend(dev);
334 else
335 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200336}
337
338static int i2c_device_pm_resume(struct device *dev)
339{
340 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200341
342 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100343 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200344 else
Mark Brownd529de22011-01-14 22:03:49 +0100345 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200346}
347
348static int i2c_device_pm_freeze(struct device *dev)
349{
350 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
351
Mark Brownd529de22011-01-14 22:03:49 +0100352 if (pm)
353 return pm_generic_freeze(dev);
354 else
355 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200356}
357
358static int i2c_device_pm_thaw(struct device *dev)
359{
360 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
361
Mark Brownd529de22011-01-14 22:03:49 +0100362 if (pm)
363 return pm_generic_thaw(dev);
364 else
365 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200366}
367
368static int i2c_device_pm_poweroff(struct device *dev)
369{
370 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
371
Mark Brownd529de22011-01-14 22:03:49 +0100372 if (pm)
373 return pm_generic_poweroff(dev);
374 else
375 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200376}
377
378static int i2c_device_pm_restore(struct device *dev)
379{
380 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200381
382 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100383 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200384 else
Mark Brownd529de22011-01-14 22:03:49 +0100385 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200386}
387#else /* !CONFIG_PM_SLEEP */
388#define i2c_device_pm_suspend NULL
389#define i2c_device_pm_resume NULL
390#define i2c_device_pm_freeze NULL
391#define i2c_device_pm_thaw NULL
392#define i2c_device_pm_poweroff NULL
393#define i2c_device_pm_restore NULL
394#endif /* !CONFIG_PM_SLEEP */
395
David Brownell9c1600e2007-05-01 23:26:31 +0200396static void i2c_client_dev_release(struct device *dev)
397{
398 kfree(to_i2c_client(dev));
399}
400
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100401static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200402show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200403{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200404 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
405 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200406}
407
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100408static ssize_t
409show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200410{
411 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800412 int len;
413
414 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
415 if (len != -ENODEV)
416 return len;
417
Jean Delvareeb8a7902008-05-18 20:49:41 +0200418 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200419}
420
Jean Delvare4f8cf822009-09-18 22:45:46 +0200421static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200422static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
423
424static struct attribute *i2c_dev_attrs[] = {
425 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200426 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200427 &dev_attr_modalias.attr,
428 NULL
429};
430
431static struct attribute_group i2c_dev_attr_group = {
432 .attrs = i2c_dev_attrs,
433};
434
435static const struct attribute_group *i2c_dev_attr_groups[] = {
436 &i2c_dev_attr_group,
437 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200438};
439
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100440static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100441 .suspend = i2c_device_pm_suspend,
442 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200443 .freeze = i2c_device_pm_freeze,
444 .thaw = i2c_device_pm_thaw,
445 .poweroff = i2c_device_pm_poweroff,
446 .restore = i2c_device_pm_restore,
447 SET_RUNTIME_PM_OPS(
448 pm_generic_runtime_suspend,
449 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200450 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200451 )
sonic zhang54067ee2009-12-14 21:17:30 +0100452};
453
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200454struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100455 .name = "i2c",
456 .match = i2c_device_match,
457 .probe = i2c_device_probe,
458 .remove = i2c_device_remove,
459 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100460 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000461};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200462EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000463
Jean Delvare51298d12009-09-18 22:45:45 +0200464static struct device_type i2c_client_type = {
465 .groups = i2c_dev_attr_groups,
466 .uevent = i2c_device_uevent,
467 .release = i2c_client_dev_release,
468};
469
David Brownell9b766b82008-01-27 18:14:51 +0100470
471/**
472 * i2c_verify_client - return parameter as i2c_client, or NULL
473 * @dev: device, probably from some driver model iterator
474 *
475 * When traversing the driver model tree, perhaps using driver model
476 * iterators like @device_for_each_child(), you can't assume very much
477 * about the nodes you find. Use this function to avoid oopses caused
478 * by wrongly treating some non-I2C device as an i2c_client.
479 */
480struct i2c_client *i2c_verify_client(struct device *dev)
481{
Jean Delvare51298d12009-09-18 22:45:45 +0200482 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100483 ? to_i2c_client(dev)
484 : NULL;
485}
486EXPORT_SYMBOL(i2c_verify_client);
487
488
Jean Delvare3a89db52010-06-03 11:33:52 +0200489/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300490 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200491static int i2c_check_client_addr_validity(const struct i2c_client *client)
492{
493 if (client->flags & I2C_CLIENT_TEN) {
494 /* 10-bit address, all values are valid */
495 if (client->addr > 0x3ff)
496 return -EINVAL;
497 } else {
498 /* 7-bit address, reject the general call address */
499 if (client->addr == 0x00 || client->addr > 0x7f)
500 return -EINVAL;
501 }
502 return 0;
503}
504
Jean Delvare656b8762010-06-03 11:33:53 +0200505/* And this is a strict address validity check, used when probing. If a
506 * device uses a reserved address, then it shouldn't be probed. 7-bit
507 * addressing is assumed, 10-bit address devices are rare and should be
508 * explicitly enumerated. */
509static int i2c_check_addr_validity(unsigned short addr)
510{
511 /*
512 * Reserved addresses per I2C specification:
513 * 0x00 General call address / START byte
514 * 0x01 CBUS address
515 * 0x02 Reserved for different bus format
516 * 0x03 Reserved for future purposes
517 * 0x04-0x07 Hs-mode master code
518 * 0x78-0x7b 10-bit slave addressing
519 * 0x7c-0x7f Reserved for future purposes
520 */
521 if (addr < 0x08 || addr > 0x77)
522 return -EINVAL;
523 return 0;
524}
525
Jean Delvare3b5f7942010-06-03 11:33:55 +0200526static int __i2c_check_addr_busy(struct device *dev, void *addrp)
527{
528 struct i2c_client *client = i2c_verify_client(dev);
529 int addr = *(int *)addrp;
530
531 if (client && client->addr == addr)
532 return -EBUSY;
533 return 0;
534}
535
Michael Lawnick08263742010-08-11 18:21:02 +0200536/* walk up mux tree */
537static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
538{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200539 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200540 int result;
541
542 result = device_for_each_child(&adapter->dev, &addr,
543 __i2c_check_addr_busy);
544
Jean Delvare97cc4d42010-10-24 18:16:57 +0200545 if (!result && parent)
546 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200547
548 return result;
549}
550
551/* recurse down mux tree */
552static int i2c_check_mux_children(struct device *dev, void *addrp)
553{
554 int result;
555
556 if (dev->type == &i2c_adapter_type)
557 result = device_for_each_child(dev, addrp,
558 i2c_check_mux_children);
559 else
560 result = __i2c_check_addr_busy(dev, addrp);
561
562 return result;
563}
564
Jean Delvare3b5f7942010-06-03 11:33:55 +0200565static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
566{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200567 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200568 int result = 0;
569
Jean Delvare97cc4d42010-10-24 18:16:57 +0200570 if (parent)
571 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200572
573 if (!result)
574 result = device_for_each_child(&adapter->dev, &addr,
575 i2c_check_mux_children);
576
577 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200578}
579
David Brownell9c1600e2007-05-01 23:26:31 +0200580/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200581 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
582 * @adapter: Target I2C bus segment
583 */
584void i2c_lock_adapter(struct i2c_adapter *adapter)
585{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200586 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
587
588 if (parent)
589 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200590 else
591 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200592}
593EXPORT_SYMBOL_GPL(i2c_lock_adapter);
594
595/**
596 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
597 * @adapter: Target I2C bus segment
598 */
599static int i2c_trylock_adapter(struct i2c_adapter *adapter)
600{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200601 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
602
603 if (parent)
604 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200605 else
606 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200607}
608
609/**
610 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
611 * @adapter: Target I2C bus segment
612 */
613void i2c_unlock_adapter(struct i2c_adapter *adapter)
614{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200615 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
616
617 if (parent)
618 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200619 else
620 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200621}
622EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
623
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200624static void i2c_dev_set_name(struct i2c_adapter *adap,
625 struct i2c_client *client)
626{
627 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
628
629 if (adev) {
630 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
631 return;
632 }
633
634 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
635 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
636 client->addr | ((client->flags & I2C_CLIENT_TEN)
637 ? 0xa000 : 0));
638}
639
Jean Delvarefe61e072010-08-11 18:20:58 +0200640/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200641 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200642 * @adap: the adapter managing the device
643 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200644 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200645 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200646 * Create an i2c device. Binding is handled through driver model
647 * probe()/remove() methods. A driver may be bound to this device when we
648 * return from this function, or any later moment (e.g. maybe hotplugging will
649 * load the driver module). This call is not appropriate for use by mainboard
650 * initialization logic, which usually runs during an arch_initcall() long
651 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200652 *
653 * This returns the new i2c client, which may be saved for later use with
654 * i2c_unregister_device(); or NULL to indicate an error.
655 */
656struct i2c_client *
657i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
658{
659 struct i2c_client *client;
660 int status;
661
662 client = kzalloc(sizeof *client, GFP_KERNEL);
663 if (!client)
664 return NULL;
665
666 client->adapter = adap;
667
668 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200669
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200670 if (info->archdata)
671 client->dev.archdata = *info->archdata;
672
Marc Pignatee354252008-08-28 08:33:22 +0200673 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200674 client->addr = info->addr;
675 client->irq = info->irq;
676
David Brownell9c1600e2007-05-01 23:26:31 +0200677 strlcpy(client->name, info->type, sizeof(client->name));
678
Jean Delvare3a89db52010-06-03 11:33:52 +0200679 /* Check for address validity */
680 status = i2c_check_client_addr_validity(client);
681 if (status) {
682 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
683 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
684 goto out_err_silent;
685 }
686
Jean Delvaref8a227e2009-06-19 16:58:18 +0200687 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200688 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200689 if (status)
690 goto out_err;
691
692 client->dev.parent = &client->adapter->dev;
693 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200694 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700695 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100696 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200697
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200698 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200699 status = device_register(&client->dev);
700 if (status)
701 goto out_err;
702
Jean Delvaref8a227e2009-06-19 16:58:18 +0200703 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
704 client->name, dev_name(&client->dev));
705
David Brownell9c1600e2007-05-01 23:26:31 +0200706 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200707
708out_err:
709 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
710 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200711out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200712 kfree(client);
713 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200714}
715EXPORT_SYMBOL_GPL(i2c_new_device);
716
717
718/**
719 * i2c_unregister_device - reverse effect of i2c_new_device()
720 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200721 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200722 */
723void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200724{
David Brownella1d9e6e2007-05-01 23:26:30 +0200725 device_unregister(&client->dev);
726}
David Brownell9c1600e2007-05-01 23:26:31 +0200727EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200728
729
Jean Delvare60b129d2008-05-11 20:37:06 +0200730static const struct i2c_device_id dummy_id[] = {
731 { "dummy", 0 },
732 { },
733};
734
Jean Delvared2653e92008-04-29 23:11:39 +0200735static int dummy_probe(struct i2c_client *client,
736 const struct i2c_device_id *id)
737{
738 return 0;
739}
740
741static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100742{
743 return 0;
744}
745
746static struct i2c_driver dummy_driver = {
747 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200748 .probe = dummy_probe,
749 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200750 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100751};
752
753/**
754 * i2c_new_dummy - return a new i2c device bound to a dummy driver
755 * @adapter: the adapter managing the device
756 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100757 * Context: can sleep
758 *
759 * This returns an I2C client bound to the "dummy" driver, intended for use
760 * with devices that consume multiple addresses. Examples of such chips
761 * include various EEPROMS (like 24c04 and 24c08 models).
762 *
763 * These dummy devices have two main uses. First, most I2C and SMBus calls
764 * except i2c_transfer() need a client handle; the dummy will be that handle.
765 * And second, this prevents the specified address from being bound to a
766 * different driver.
767 *
768 * This returns the new i2c client, which should be saved for later use with
769 * i2c_unregister_device(); or NULL to indicate an error.
770 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100771struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100772{
773 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200774 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100775 };
776
David Brownelle9f13732008-01-27 18:14:52 +0100777 return i2c_new_device(adapter, &info);
778}
779EXPORT_SYMBOL_GPL(i2c_new_dummy);
780
David Brownellf37dd802007-02-13 22:09:00 +0100781/* ------------------------------------------------------------------------- */
782
David Brownell16ffadf2007-05-01 23:26:28 +0200783/* I2C bus adapters -- one roots each I2C or SMBUS segment */
784
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200785static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
David Brownellef2c83212007-05-01 23:26:28 +0200787 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 complete(&adap->dev_released);
789}
790
Jean Delvare99cd8e22009-06-19 16:58:20 +0200791/*
Jean Delvare390946b2012-09-10 10:14:02 +0200792 * This function is only needed for mutex_lock_nested, so it is never
793 * called unless locking correctness checking is enabled. Thus we
794 * make it inline to avoid a compiler warning. That's what gcc ends up
795 * doing anyway.
796 */
797static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
798{
799 unsigned int depth = 0;
800
801 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
802 depth++;
803
804 return depth;
805}
806
807/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200808 * Let users instantiate I2C devices through sysfs. This can be used when
809 * platform initialization code doesn't contain the proper data for
810 * whatever reason. Also useful for drivers that do device detection and
811 * detection fails, either because the device uses an unexpected address,
812 * or this is a compatible device with different ID register values.
813 *
814 * Parameter checking may look overzealous, but we really don't want
815 * the user to provide incorrect parameters.
816 */
817static ssize_t
818i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
819 const char *buf, size_t count)
820{
821 struct i2c_adapter *adap = to_i2c_adapter(dev);
822 struct i2c_board_info info;
823 struct i2c_client *client;
824 char *blank, end;
825 int res;
826
Jean Delvare99cd8e22009-06-19 16:58:20 +0200827 memset(&info, 0, sizeof(struct i2c_board_info));
828
829 blank = strchr(buf, ' ');
830 if (!blank) {
831 dev_err(dev, "%s: Missing parameters\n", "new_device");
832 return -EINVAL;
833 }
834 if (blank - buf > I2C_NAME_SIZE - 1) {
835 dev_err(dev, "%s: Invalid device name\n", "new_device");
836 return -EINVAL;
837 }
838 memcpy(info.type, buf, blank - buf);
839
840 /* Parse remaining parameters, reject extra parameters */
841 res = sscanf(++blank, "%hi%c", &info.addr, &end);
842 if (res < 1) {
843 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
844 return -EINVAL;
845 }
846 if (res > 1 && end != '\n') {
847 dev_err(dev, "%s: Extra parameters\n", "new_device");
848 return -EINVAL;
849 }
850
Jean Delvare99cd8e22009-06-19 16:58:20 +0200851 client = i2c_new_device(adap, &info);
852 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200853 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200854
855 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200856 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200857 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200858 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200859 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
860 info.type, info.addr);
861
862 return count;
863}
864
865/*
866 * And of course let the users delete the devices they instantiated, if
867 * they got it wrong. This interface can only be used to delete devices
868 * instantiated by i2c_sysfs_new_device above. This guarantees that we
869 * don't delete devices to which some kernel code still has references.
870 *
871 * Parameter checking may look overzealous, but we really don't want
872 * the user to delete the wrong device.
873 */
874static ssize_t
875i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
876 const char *buf, size_t count)
877{
878 struct i2c_adapter *adap = to_i2c_adapter(dev);
879 struct i2c_client *client, *next;
880 unsigned short addr;
881 char end;
882 int res;
883
884 /* Parse parameters, reject extra parameters */
885 res = sscanf(buf, "%hi%c", &addr, &end);
886 if (res < 1) {
887 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
888 return -EINVAL;
889 }
890 if (res > 1 && end != '\n') {
891 dev_err(dev, "%s: Extra parameters\n", "delete_device");
892 return -EINVAL;
893 }
894
895 /* Make sure the device was added through sysfs */
896 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200897 mutex_lock_nested(&adap->userspace_clients_lock,
898 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200899 list_for_each_entry_safe(client, next, &adap->userspace_clients,
900 detected) {
901 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200902 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
903 "delete_device", client->name, client->addr);
904
905 list_del(&client->detected);
906 i2c_unregister_device(client);
907 res = count;
908 break;
909 }
910 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200911 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200912
913 if (res < 0)
914 dev_err(dev, "%s: Can't find device in list\n",
915 "delete_device");
916 return res;
917}
918
Jean Delvare4f8cf822009-09-18 22:45:46 +0200919static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200920static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
921 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200922
923static struct attribute *i2c_adapter_attrs[] = {
924 &dev_attr_name.attr,
925 &dev_attr_new_device.attr,
926 &dev_attr_delete_device.attr,
927 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200928};
929
Jean Delvare4f8cf822009-09-18 22:45:46 +0200930static struct attribute_group i2c_adapter_attr_group = {
931 .attrs = i2c_adapter_attrs,
932};
933
934static const struct attribute_group *i2c_adapter_attr_groups[] = {
935 &i2c_adapter_attr_group,
936 NULL
937};
938
Michael Lawnick08263742010-08-11 18:21:02 +0200939struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200940 .groups = i2c_adapter_attr_groups,
941 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200942};
Michael Lawnick08263742010-08-11 18:21:02 +0200943EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Stephen Warren643dd092012-04-17 12:43:33 -0600945/**
946 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
947 * @dev: device, probably from some driver model iterator
948 *
949 * When traversing the driver model tree, perhaps using driver model
950 * iterators like @device_for_each_child(), you can't assume very much
951 * about the nodes you find. Use this function to avoid oopses caused
952 * by wrongly treating some non-I2C device as an i2c_adapter.
953 */
954struct i2c_adapter *i2c_verify_adapter(struct device *dev)
955{
956 return (dev->type == &i2c_adapter_type)
957 ? to_i2c_adapter(dev)
958 : NULL;
959}
960EXPORT_SYMBOL(i2c_verify_adapter);
961
Jean Delvare2bb50952009-09-18 22:45:46 +0200962#ifdef CONFIG_I2C_COMPAT
963static struct class_compat *i2c_adapter_compat_class;
964#endif
965
David Brownell9c1600e2007-05-01 23:26:31 +0200966static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
967{
968 struct i2c_devinfo *devinfo;
969
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200970 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200971 list_for_each_entry(devinfo, &__i2c_board_list, list) {
972 if (devinfo->busnum == adapter->nr
973 && !i2c_new_device(adapter,
974 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100975 dev_err(&adapter->dev,
976 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200977 devinfo->board_info.addr);
978 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200979 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200980}
981
Wolfram Sang687b81d2013-07-11 12:56:15 +0100982/* OF support code */
983
984#if IS_ENABLED(CONFIG_OF)
985static void of_i2c_register_devices(struct i2c_adapter *adap)
986{
987 void *result;
988 struct device_node *node;
989
990 /* Only register child devices if the adapter has a node pointer set */
991 if (!adap->dev.of_node)
992 return;
993
994 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
995
996 for_each_available_child_of_node(adap->dev.of_node, node) {
997 struct i2c_board_info info = {};
998 struct dev_archdata dev_ad = {};
999 const __be32 *addr;
1000 int len;
1001
1002 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1003
1004 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1005 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1006 node->full_name);
1007 continue;
1008 }
1009
1010 addr = of_get_property(node, "reg", &len);
1011 if (!addr || (len < sizeof(int))) {
1012 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1013 node->full_name);
1014 continue;
1015 }
1016
1017 info.addr = be32_to_cpup(addr);
1018 if (info.addr > (1 << 10) - 1) {
1019 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1020 info.addr, node->full_name);
1021 continue;
1022 }
1023
1024 info.irq = irq_of_parse_and_map(node, 0);
1025 info.of_node = of_node_get(node);
1026 info.archdata = &dev_ad;
1027
1028 if (of_get_property(node, "wakeup-source", NULL))
1029 info.flags |= I2C_CLIENT_WAKE;
1030
1031 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1032
1033 result = i2c_new_device(adap, &info);
1034 if (result == NULL) {
1035 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1036 node->full_name);
1037 of_node_put(node);
1038 irq_dispose_mapping(info.irq);
1039 continue;
1040 }
1041 }
1042}
1043
1044static int of_dev_node_match(struct device *dev, void *data)
1045{
1046 return dev->of_node == data;
1047}
1048
1049/* must call put_device() when done with returned i2c_client device */
1050struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1051{
1052 struct device *dev;
1053
1054 dev = bus_find_device(&i2c_bus_type, NULL, node,
1055 of_dev_node_match);
1056 if (!dev)
1057 return NULL;
1058
1059 return i2c_verify_client(dev);
1060}
1061EXPORT_SYMBOL(of_find_i2c_device_by_node);
1062
1063/* must call put_device() when done with returned i2c_adapter device */
1064struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1065{
1066 struct device *dev;
1067
1068 dev = bus_find_device(&i2c_bus_type, NULL, node,
1069 of_dev_node_match);
1070 if (!dev)
1071 return NULL;
1072
1073 return i2c_verify_adapter(dev);
1074}
1075EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1076#else
1077static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1078#endif /* CONFIG_OF */
1079
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001080/* ACPI support code */
1081
1082#if IS_ENABLED(CONFIG_ACPI)
1083static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1084{
1085 struct i2c_board_info *info = data;
1086
1087 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1088 struct acpi_resource_i2c_serialbus *sb;
1089
1090 sb = &ares->data.i2c_serial_bus;
1091 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1092 info->addr = sb->slave_address;
1093 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1094 info->flags |= I2C_CLIENT_TEN;
1095 }
1096 } else if (info->irq < 0) {
1097 struct resource r;
1098
1099 if (acpi_dev_resource_interrupt(ares, 0, &r))
1100 info->irq = r.start;
1101 }
1102
1103 /* Tell the ACPI core to skip this resource */
1104 return 1;
1105}
1106
1107static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1108 void *data, void **return_value)
1109{
1110 struct i2c_adapter *adapter = data;
1111 struct list_head resource_list;
1112 struct i2c_board_info info;
1113 struct acpi_device *adev;
1114 int ret;
1115
1116 if (acpi_bus_get_device(handle, &adev))
1117 return AE_OK;
1118 if (acpi_bus_get_status(adev) || !adev->status.present)
1119 return AE_OK;
1120
1121 memset(&info, 0, sizeof(info));
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001122 info.acpi_node.companion = adev;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001123 info.irq = -1;
1124
1125 INIT_LIST_HEAD(&resource_list);
1126 ret = acpi_dev_get_resources(adev, &resource_list,
1127 acpi_i2c_add_resource, &info);
1128 acpi_dev_free_resource_list(&resource_list);
1129
1130 if (ret < 0 || !info.addr)
1131 return AE_OK;
1132
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001133 adev->power.flags.ignore_parent = true;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001134 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1135 if (!i2c_new_device(adapter, &info)) {
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001136 adev->power.flags.ignore_parent = false;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001137 dev_err(&adapter->dev,
1138 "failed to add I2C device %s from ACPI\n",
1139 dev_name(&adev->dev));
1140 }
1141
1142 return AE_OK;
1143}
1144
1145/**
1146 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1147 * @adap: pointer to adapter
1148 *
1149 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1150 * namespace. When a device is found it will be added to the Linux device
1151 * model and bound to the corresponding ACPI handle.
1152 */
1153static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1154{
1155 acpi_handle handle;
1156 acpi_status status;
1157
Jean Delvare47b6e4772013-10-10 08:04:06 +02001158 if (!adap->dev.parent)
1159 return;
1160
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001161 handle = ACPI_HANDLE(adap->dev.parent);
1162 if (!handle)
1163 return;
1164
1165 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1166 acpi_i2c_add_device, NULL,
1167 adap, NULL);
1168 if (ACPI_FAILURE(status))
1169 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1170}
1171#else
1172static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1173#endif /* CONFIG_ACPI */
1174
Jean Delvare69b00892009-12-06 17:06:27 +01001175static int i2c_do_add_adapter(struct i2c_driver *driver,
1176 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001177{
Jean Delvare4735c982008-07-14 22:38:36 +02001178 /* Detect supported devices on that bus, and instantiate them */
1179 i2c_detect(adap, driver);
1180
1181 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001182 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001183 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1184 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001185 dev_warn(&adap->dev, "Please use another way to instantiate "
1186 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001187 /* We ignore the return code; if it fails, too bad */
1188 driver->attach_adapter(adap);
1189 }
1190 return 0;
1191}
1192
Jean Delvare69b00892009-12-06 17:06:27 +01001193static int __process_new_adapter(struct device_driver *d, void *data)
1194{
1195 return i2c_do_add_adapter(to_i2c_driver(d), data);
1196}
1197
David Brownell6e13e642007-05-01 23:26:31 +02001198static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Jean Delvared6703282010-08-11 18:20:59 +02001200 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
David Brownell1d0b19c2008-10-14 17:30:05 +02001202 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001203 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1204 res = -EAGAIN;
1205 goto out_list;
1206 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001207
Jean Delvare2236baa2010-11-15 22:40:38 +01001208 /* Sanity checks */
1209 if (unlikely(adap->name[0] == '\0')) {
1210 pr_err("i2c-core: Attempt to register an adapter with "
1211 "no name!\n");
1212 return -EINVAL;
1213 }
1214 if (unlikely(!adap->algo)) {
1215 pr_err("i2c-core: Attempt to register adapter '%s' with "
1216 "no algo!\n", adap->name);
1217 return -EINVAL;
1218 }
1219
Mika Kuoppala194684e2009-12-06 17:06:22 +01001220 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001221 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001222 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Jean Delvare8fcfef62009-03-28 21:34:43 +01001224 /* Set default timeout to 1 second if not already set */
1225 if (adap->timeout == 0)
1226 adap->timeout = HZ;
1227
Kay Sievers27d9c182009-01-07 14:29:16 +01001228 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001229 adap->dev.bus = &i2c_bus_type;
1230 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001231 res = device_register(&adap->dev);
1232 if (res)
1233 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001235 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1236
Jean Delvare2bb50952009-09-18 22:45:46 +02001237#ifdef CONFIG_I2C_COMPAT
1238 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1239 adap->dev.parent);
1240 if (res)
1241 dev_warn(&adap->dev,
1242 "Failed to create compatibility class link\n");
1243#endif
1244
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301245 /* bus recovery specific initialization */
1246 if (adap->bus_recovery_info) {
1247 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1248
1249 if (!bri->recover_bus) {
1250 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1251 adap->bus_recovery_info = NULL;
1252 goto exit_recovery;
1253 }
1254
1255 /* Generic GPIO recovery */
1256 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1257 if (!gpio_is_valid(bri->scl_gpio)) {
1258 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1259 adap->bus_recovery_info = NULL;
1260 goto exit_recovery;
1261 }
1262
1263 if (gpio_is_valid(bri->sda_gpio))
1264 bri->get_sda = get_sda_gpio_value;
1265 else
1266 bri->get_sda = NULL;
1267
1268 bri->get_scl = get_scl_gpio_value;
1269 bri->set_scl = set_scl_gpio_value;
1270 } else if (!bri->set_scl || !bri->get_scl) {
1271 /* Generic SCL recovery */
1272 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1273 adap->bus_recovery_info = NULL;
1274 }
1275 }
1276
1277exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001278 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001279 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001280 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001281
David Brownell6e13e642007-05-01 23:26:31 +02001282 if (adap->nr < __i2c_first_dynamic_bus_num)
1283 i2c_scan_static_board_info(adap);
1284
Jean Delvare4735c982008-07-14 22:38:36 +02001285 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001286 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001287 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001288 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001289
1290 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001291
Jean Delvareb119c6c2006-08-15 18:26:30 +02001292out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001293 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001294 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001295 mutex_unlock(&core_lock);
1296 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
David Brownell6e13e642007-05-01 23:26:31 +02001299/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001300 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1301 * @adap: the adapter to register (with adap->nr initialized)
1302 * Context: can sleep
1303 *
1304 * See i2c_add_numbered_adapter() for details.
1305 */
1306static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1307{
1308 int id;
1309
1310 mutex_lock(&core_lock);
1311 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1312 GFP_KERNEL);
1313 mutex_unlock(&core_lock);
1314 if (id < 0)
1315 return id == -ENOSPC ? -EBUSY : id;
1316
1317 return i2c_register_adapter(adap);
1318}
1319
1320/**
David Brownell6e13e642007-05-01 23:26:31 +02001321 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1322 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001323 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001324 *
1325 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001326 * doesn't matter or when its bus number is specified by an dt alias.
1327 * Examples of bases when the bus number doesn't matter: I2C adapters
1328 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001329 *
1330 * When this returns zero, a new bus number was allocated and stored
1331 * in adap->nr, and the specified adapter became available for clients.
1332 * Otherwise, a negative errno value is returned.
1333 */
1334int i2c_add_adapter(struct i2c_adapter *adapter)
1335{
Doug Andersonee5c2742013-03-01 08:57:31 -08001336 struct device *dev = &adapter->dev;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001337 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001338
Doug Andersonee5c2742013-03-01 08:57:31 -08001339 if (dev->of_node) {
1340 id = of_alias_get_id(dev->of_node, "i2c");
1341 if (id >= 0) {
1342 adapter->nr = id;
1343 return __i2c_add_numbered_adapter(adapter);
1344 }
1345 }
1346
Jean Delvarecaada322008-01-27 18:14:49 +01001347 mutex_lock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001348 id = idr_alloc(&i2c_adapter_idr, adapter,
1349 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001350 mutex_unlock(&core_lock);
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001351 if (id < 0)
1352 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001353
1354 adapter->nr = id;
Tejun Heo4ae42b0f2013-02-27 17:04:15 -08001355
David Brownell6e13e642007-05-01 23:26:31 +02001356 return i2c_register_adapter(adapter);
1357}
1358EXPORT_SYMBOL(i2c_add_adapter);
1359
1360/**
1361 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1362 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001363 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001364 *
1365 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001366 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1367 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001368 * is used to properly configure I2C devices.
1369 *
Grant Likely488bf312011-07-25 17:49:43 +02001370 * If the requested bus number is set to -1, then this function will behave
1371 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1372 *
David Brownell6e13e642007-05-01 23:26:31 +02001373 * If no devices have pre-been declared for this bus, then be sure to
1374 * register the adapter before any dynamically allocated ones. Otherwise
1375 * the required bus ID may not be available.
1376 *
1377 * When this returns zero, the specified adapter became available for
1378 * clients using the bus number provided in adap->nr. Also, the table
1379 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1380 * and the appropriate driver model device nodes are created. Otherwise, a
1381 * negative errno value is returned.
1382 */
1383int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1384{
Grant Likely488bf312011-07-25 17:49:43 +02001385 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1386 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001387
Doug Andersonee5c2742013-03-01 08:57:31 -08001388 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001389}
1390EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1391
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001392static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001393 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001394{
Jean Delvare4735c982008-07-14 22:38:36 +02001395 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001396
Jean Delvareacec2112009-03-28 21:34:40 +01001397 /* Remove the devices we created ourselves as the result of hardware
1398 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001399 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1400 if (client->adapter == adapter) {
1401 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1402 client->name, client->addr);
1403 list_del(&client->detected);
1404 i2c_unregister_device(client);
1405 }
1406 }
Jean Delvare026526f2008-01-27 18:14:49 +01001407}
1408
Jean Delvaree549c2b2009-06-19 16:58:19 +02001409static int __unregister_client(struct device *dev, void *dummy)
1410{
1411 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001412 if (client && strcmp(client->name, "dummy"))
1413 i2c_unregister_device(client);
1414 return 0;
1415}
1416
1417static int __unregister_dummy(struct device *dev, void *dummy)
1418{
1419 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001420 if (client)
1421 i2c_unregister_device(client);
1422 return 0;
1423}
1424
Jean Delvare69b00892009-12-06 17:06:27 +01001425static int __process_removed_adapter(struct device_driver *d, void *data)
1426{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001427 i2c_do_del_adapter(to_i2c_driver(d), data);
1428 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001429}
1430
David Brownelld64f73b2007-07-12 14:12:28 +02001431/**
1432 * i2c_del_adapter - unregister I2C adapter
1433 * @adap: the adapter being unregistered
1434 * Context: can sleep
1435 *
1436 * This unregisters an I2C adapter which was previously registered
1437 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1438 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001439void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001441 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001442 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001445 mutex_lock(&core_lock);
1446 found = idr_find(&i2c_adapter_idr, adap->nr);
1447 mutex_unlock(&core_lock);
1448 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001449 pr_debug("i2c-core: attempting to delete unregistered "
1450 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001451 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Jean Delvare026526f2008-01-27 18:14:49 +01001454 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001455 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001456 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001457 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001458 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001460 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001461 mutex_lock_nested(&adap->userspace_clients_lock,
1462 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001463 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1464 detected) {
1465 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1466 client->addr);
1467 list_del(&client->detected);
1468 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001469 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001470 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001471
Jean Delvaree549c2b2009-06-19 16:58:19 +02001472 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001473 * check the returned value. This is a two-pass process, because
1474 * we can't remove the dummy devices during the first pass: they
1475 * could have been instantiated by real devices wishing to clean
1476 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001477 device_for_each_child(&adap->dev, NULL, __unregister_client);
1478 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jean Delvare2bb50952009-09-18 22:45:46 +02001480#ifdef CONFIG_I2C_COMPAT
1481 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1482 adap->dev.parent);
1483#endif
1484
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001485 /* device name is gone after device_unregister */
1486 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 /* clean up the sysfs representation */
1489 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /* wait for sysfs to drop all references */
1493 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
David Brownell6e13e642007-05-01 23:26:31 +02001495 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001496 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001498 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001500 /* Clear the device structure in case this adapter is ever going to be
1501 added again */
1502 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
David Brownellc0564602007-05-01 23:26:31 +02001504EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
David Brownell7b4fbc52007-05-01 23:26:30 +02001506/* ------------------------------------------------------------------------- */
1507
Jean Delvare7ae31482011-03-20 14:50:52 +01001508int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1509{
1510 int res;
1511
1512 mutex_lock(&core_lock);
1513 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1514 mutex_unlock(&core_lock);
1515
1516 return res;
1517}
1518EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1519
Jean Delvare69b00892009-12-06 17:06:27 +01001520static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001521{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001522 if (dev->type != &i2c_adapter_type)
1523 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001524 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001525}
1526
David Brownell7b4fbc52007-05-01 23:26:30 +02001527/*
1528 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001529 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 */
1531
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001532int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001534 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
David Brownell1d0b19c2008-10-14 17:30:05 +02001536 /* Can't register until after driver model init */
1537 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1538 return -EAGAIN;
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001541 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jean Delvare729d6dd2009-06-19 16:58:18 +02001544 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001545 * will have called probe() for all matching-but-unbound devices.
1546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 res = driver_register(&driver->driver);
1548 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001549 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001550
Mark Brownf4e8db32011-01-14 22:03:50 +01001551 /* Drivers should switch to dev_pm_ops instead. */
1552 if (driver->suspend)
1553 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1554 driver->driver.name);
1555 if (driver->resume)
1556 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1557 driver->driver.name);
1558
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001559 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Jean Delvare4735c982008-07-14 22:38:36 +02001561 INIT_LIST_HEAD(&driver->clients);
1562 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001563 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001564
Jean Delvare7eebcb72006-02-05 23:28:21 +01001565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001567EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Jean Delvare69b00892009-12-06 17:06:27 +01001569static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001570{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001571 if (dev->type == &i2c_adapter_type)
1572 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1573 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001574}
1575
David Brownella1d9e6e2007-05-01 23:26:30 +02001576/**
1577 * i2c_del_driver - unregister I2C driver
1578 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001579 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001580 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001581void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Jean Delvare7ae31482011-03-20 14:50:52 +01001583 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001586 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
David Brownellc0564602007-05-01 23:26:31 +02001588EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
David Brownell7b4fbc52007-05-01 23:26:30 +02001590/* ------------------------------------------------------------------------- */
1591
Jean Delvaree48d3312008-01-27 18:14:48 +01001592/**
1593 * i2c_use_client - increments the reference count of the i2c client structure
1594 * @client: the client being referenced
1595 *
1596 * Each live reference to a client should be refcounted. The driver model does
1597 * that automatically as part of driver binding, so that most drivers don't
1598 * need to do this explicitly: they hold a reference until they're unbound
1599 * from the device.
1600 *
1601 * A pointer to the client with the incremented reference counter is returned.
1602 */
1603struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
David Brownell6ea438e2008-07-14 22:38:24 +02001605 if (client && get_device(&client->dev))
1606 return client;
1607 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
David Brownellc0564602007-05-01 23:26:31 +02001609EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Jean Delvaree48d3312008-01-27 18:14:48 +01001611/**
1612 * i2c_release_client - release a use of the i2c client structure
1613 * @client: the client being no longer referenced
1614 *
1615 * Must be called when a user of a client is finished with it.
1616 */
1617void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
David Brownell6ea438e2008-07-14 22:38:24 +02001619 if (client)
1620 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
David Brownellc0564602007-05-01 23:26:31 +02001622EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David Brownell9b766b82008-01-27 18:14:51 +01001624struct i2c_cmd_arg {
1625 unsigned cmd;
1626 void *arg;
1627};
1628
1629static int i2c_cmd(struct device *dev, void *_arg)
1630{
1631 struct i2c_client *client = i2c_verify_client(dev);
1632 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001633 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001634
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001635 if (!client || !client->dev.driver)
1636 return 0;
1637
1638 driver = to_i2c_driver(client->dev.driver);
1639 if (driver->command)
1640 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001641 return 0;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1645{
David Brownell9b766b82008-01-27 18:14:51 +01001646 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
David Brownell9b766b82008-01-27 18:14:51 +01001648 cmd_arg.cmd = cmd;
1649 cmd_arg.arg = arg;
1650 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
David Brownellc0564602007-05-01 23:26:31 +02001652EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654static int __init i2c_init(void)
1655{
1656 int retval;
1657
1658 retval = bus_register(&i2c_bus_type);
1659 if (retval)
1660 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001661#ifdef CONFIG_I2C_COMPAT
1662 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1663 if (!i2c_adapter_compat_class) {
1664 retval = -ENOMEM;
1665 goto bus_err;
1666 }
1667#endif
David Brownelle9f13732008-01-27 18:14:52 +01001668 retval = i2c_add_driver(&dummy_driver);
1669 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001670 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001671 return 0;
1672
Jean Delvare2bb50952009-09-18 22:45:46 +02001673class_err:
1674#ifdef CONFIG_I2C_COMPAT
1675 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001676bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001677#endif
David Brownelle9f13732008-01-27 18:14:52 +01001678 bus_unregister(&i2c_bus_type);
1679 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
1682static void __exit i2c_exit(void)
1683{
David Brownelle9f13732008-01-27 18:14:52 +01001684 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001685#ifdef CONFIG_I2C_COMPAT
1686 class_compat_unregister(i2c_adapter_compat_class);
1687#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 bus_unregister(&i2c_bus_type);
1689}
1690
David Brownella10f9e72008-10-14 17:30:06 +02001691/* We must initialize early, because some subsystems register i2c drivers
1692 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1693 */
1694postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695module_exit(i2c_exit);
1696
1697/* ----------------------------------------------------
1698 * the functional interface to the i2c busses.
1699 * ----------------------------------------------------
1700 */
1701
David Brownella1cdeda2008-07-14 22:38:24 +02001702/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001703 * __i2c_transfer - unlocked flavor of i2c_transfer
1704 * @adap: Handle to I2C bus
1705 * @msgs: One or more messages to execute before STOP is issued to
1706 * terminate the operation; each message begins with a START.
1707 * @num: Number of messages to be executed.
1708 *
1709 * Returns negative errno, else the number of messages executed.
1710 *
1711 * Adapter lock must be held when calling this function. No debug logging
1712 * takes place. adap->algo->master_xfer existence isn't checked.
1713 */
1714int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1715{
1716 unsigned long orig_jiffies;
1717 int ret, try;
1718
1719 /* Retry automatically on arbitration loss */
1720 orig_jiffies = jiffies;
1721 for (ret = 0, try = 0; try <= adap->retries; try++) {
1722 ret = adap->algo->master_xfer(adap, msgs, num);
1723 if (ret != -EAGAIN)
1724 break;
1725 if (time_after(jiffies, orig_jiffies + adap->timeout))
1726 break;
1727 }
1728
1729 return ret;
1730}
1731EXPORT_SYMBOL(__i2c_transfer);
1732
1733/**
David Brownella1cdeda2008-07-14 22:38:24 +02001734 * i2c_transfer - execute a single or combined I2C message
1735 * @adap: Handle to I2C bus
1736 * @msgs: One or more messages to execute before STOP is issued to
1737 * terminate the operation; each message begins with a START.
1738 * @num: Number of messages to be executed.
1739 *
1740 * Returns negative errno, else the number of messages executed.
1741 *
1742 * Note that there is no requirement that each message be sent to
1743 * the same slave address, although that is the most common model.
1744 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001745int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001747 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
David Brownella1cdeda2008-07-14 22:38:24 +02001749 /* REVISIT the fault reporting model here is weak:
1750 *
1751 * - When we get an error after receiving N bytes from a slave,
1752 * there is no way to report "N".
1753 *
1754 * - When we get a NAK after transmitting N bytes to a slave,
1755 * there is no way to report "N" ... or to let the master
1756 * continue executing the rest of this combined message, if
1757 * that's the appropriate response.
1758 *
1759 * - When for example "num" is two and we successfully complete
1760 * the first message but get an error part way through the
1761 * second, it's unclear whether that should be reported as
1762 * one (discarding status on the second message) or errno
1763 * (discarding status on the first one).
1764 */
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 if (adap->algo->master_xfer) {
1767#ifdef DEBUG
1768 for (ret = 0; ret < num; ret++) {
1769 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001770 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1771 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1772 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774#endif
1775
Mike Rapoportcea443a2008-01-27 18:14:50 +01001776 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001777 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001778 if (!ret)
1779 /* I2C activity is ongoing. */
1780 return -EAGAIN;
1781 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001782 i2c_lock_adapter(adap);
Mike Rapoportcea443a2008-01-27 18:14:50 +01001783 }
1784
Jean Delvareb37d2a32012-06-29 07:47:19 -03001785 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001786 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 return ret;
1789 } else {
1790 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001791 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
1793}
David Brownellc0564602007-05-01 23:26:31 +02001794EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
David Brownella1cdeda2008-07-14 22:38:24 +02001796/**
1797 * i2c_master_send - issue a single I2C message in master transmit mode
1798 * @client: Handle to slave device
1799 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001800 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001801 *
1802 * Returns negative errno, or else the number of bytes written.
1803 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001804int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001807 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct i2c_msg msg;
1809
Jean Delvare815f55f2005-05-07 22:58:46 +02001810 msg.addr = client->addr;
1811 msg.flags = client->flags & I2C_M_TEN;
1812 msg.len = count;
1813 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001814
Jean Delvare815f55f2005-05-07 22:58:46 +02001815 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001817 /*
1818 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1819 * transmitted, else error code.
1820 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001821 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
David Brownellc0564602007-05-01 23:26:31 +02001823EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
David Brownella1cdeda2008-07-14 22:38:24 +02001825/**
1826 * i2c_master_recv - issue a single I2C message in master receive mode
1827 * @client: Handle to slave device
1828 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001829 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001830 *
1831 * Returns negative errno, or else the number of bytes read.
1832 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001833int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Farid Hammane7225acf2010-05-21 18:40:58 +02001835 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct i2c_msg msg;
1837 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Jean Delvare815f55f2005-05-07 22:58:46 +02001839 msg.addr = client->addr;
1840 msg.flags = client->flags & I2C_M_TEN;
1841 msg.flags |= I2C_M_RD;
1842 msg.len = count;
1843 msg.buf = buf;
1844
1845 ret = i2c_transfer(adap, &msg, 1);
1846
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001847 /*
1848 * If everything went ok (i.e. 1 msg received), return #bytes received,
1849 * else error code.
1850 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001851 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852}
David Brownellc0564602007-05-01 23:26:31 +02001853EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/* ----------------------------------------------------
1856 * the i2c address scanning function
1857 * Will not work for 10-bit addresses!
1858 * ----------------------------------------------------
1859 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001860
Jean Delvare63e4e802010-06-03 11:33:51 +02001861/*
1862 * Legacy default probe function, mostly relevant for SMBus. The default
1863 * probe method is a quick write, but it is known to corrupt the 24RF08
1864 * EEPROMs due to a state machine bug, and could also irreversibly
1865 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1866 * we use a short byte read instead. Also, some bus drivers don't implement
1867 * quick write, so we fallback to a byte read in that case too.
1868 * On x86, there is another special case for FSC hardware monitoring chips,
1869 * which want regular byte reads (address 0x73.) Fortunately, these are the
1870 * only known chips using this I2C address on PC hardware.
1871 * Returns 1 if probe succeeded, 0 if not.
1872 */
1873static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1874{
1875 int err;
1876 union i2c_smbus_data dummy;
1877
1878#ifdef CONFIG_X86
1879 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1880 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1881 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1882 I2C_SMBUS_BYTE_DATA, &dummy);
1883 else
1884#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001885 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1886 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001887 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1888 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001889 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1890 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1891 I2C_SMBUS_BYTE, &dummy);
1892 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001893 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1894 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001895 err = -EOPNOTSUPP;
1896 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001897
1898 return err >= 0;
1899}
1900
Jean Delvareccfbbd02009-12-06 17:06:25 +01001901static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001902 struct i2c_driver *driver)
1903{
1904 struct i2c_board_info info;
1905 struct i2c_adapter *adapter = temp_client->adapter;
1906 int addr = temp_client->addr;
1907 int err;
1908
1909 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001910 err = i2c_check_addr_validity(addr);
1911 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001912 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1913 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001914 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001915 }
1916
1917 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001918 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001919 return 0;
1920
Jean Delvareccfbbd02009-12-06 17:06:25 +01001921 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001922 if (!i2c_default_probe(adapter, addr))
1923 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001924
1925 /* Finally call the custom detection function */
1926 memset(&info, 0, sizeof(struct i2c_board_info));
1927 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001928 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001929 if (err) {
1930 /* -ENODEV is returned if the detection fails. We catch it
1931 here as this isn't an error. */
1932 return err == -ENODEV ? 0 : err;
1933 }
1934
1935 /* Consistency check */
1936 if (info.type[0] == '\0') {
1937 dev_err(&adapter->dev, "%s detection function provided "
1938 "no name for 0x%x\n", driver->driver.name,
1939 addr);
1940 } else {
1941 struct i2c_client *client;
1942
1943 /* Detection succeeded, instantiate the device */
1944 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1945 info.type, info.addr);
1946 client = i2c_new_device(adapter, &info);
1947 if (client)
1948 list_add_tail(&client->detected, &driver->clients);
1949 else
1950 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1951 info.type, info.addr);
1952 }
1953 return 0;
1954}
1955
1956static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1957{
Jean Delvarec3813d62009-12-14 21:17:25 +01001958 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001959 struct i2c_client *temp_client;
1960 int i, err = 0;
1961 int adap_id = i2c_adapter_id(adapter);
1962
Jean Delvarec3813d62009-12-14 21:17:25 +01001963 address_list = driver->address_list;
1964 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001965 return 0;
1966
Jean Delvare51b54ba2010-10-24 18:16:58 +02001967 /* Stop here if the classes do not match */
1968 if (!(adapter->class & driver->class))
1969 return 0;
1970
Jean Delvare4735c982008-07-14 22:38:36 +02001971 /* Set up a temporary client to help detect callback */
1972 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1973 if (!temp_client)
1974 return -ENOMEM;
1975 temp_client->adapter = adapter;
1976
Jean Delvarec3813d62009-12-14 21:17:25 +01001977 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001978 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001979 "addr 0x%02x\n", adap_id, address_list[i]);
1980 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001981 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001982 if (unlikely(err))
1983 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001984 }
1985
Jean Delvare4735c982008-07-14 22:38:36 +02001986 kfree(temp_client);
1987 return err;
1988}
1989
Jean Delvared44f19d2010-08-11 18:20:57 +02001990int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1991{
1992 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1993 I2C_SMBUS_QUICK, NULL) >= 0;
1994}
1995EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1996
Jean Delvare12b5053a2007-05-01 23:26:31 +02001997struct i2c_client *
1998i2c_new_probed_device(struct i2c_adapter *adap,
1999 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002000 unsigned short const *addr_list,
2001 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002002{
2003 int i;
2004
Jean Delvare8031d792010-08-11 18:21:00 +02002005 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002006 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002007
Jean Delvare12b5053a2007-05-01 23:26:31 +02002008 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2009 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002010 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002011 dev_warn(&adap->dev, "Invalid 7-bit address "
2012 "0x%02x\n", addr_list[i]);
2013 continue;
2014 }
2015
2016 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002017 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002018 dev_dbg(&adap->dev, "Address 0x%02x already in "
2019 "use, not probing\n", addr_list[i]);
2020 continue;
2021 }
2022
Jean Delvare63e4e802010-06-03 11:33:51 +02002023 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002024 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002025 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002026 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002027
2028 if (addr_list[i] == I2C_CLIENT_END) {
2029 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2030 return NULL;
2031 }
2032
2033 info->addr = addr_list[i];
2034 return i2c_new_device(adap, info);
2035}
2036EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2037
Jean Delvared735b342011-03-20 14:50:52 +01002038struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002041
Jean Delvarecaada322008-01-27 18:14:49 +01002042 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002043 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002044 if (adapter && !try_module_get(adapter->owner))
2045 adapter = NULL;
2046
Jean Delvarecaada322008-01-27 18:14:49 +01002047 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002048 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
David Brownellc0564602007-05-01 23:26:31 +02002050EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052void i2c_put_adapter(struct i2c_adapter *adap)
2053{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002054 if (adap)
2055 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056}
David Brownellc0564602007-05-01 23:26:31 +02002057EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059/* The SMBus parts */
2060
David Brownell438d6c22006-12-10 21:21:31 +01002061#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002062static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002065
Farid Hammane7225acf2010-05-21 18:40:58 +02002066 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002067 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 data = data ^ POLY;
2069 data = data << 1;
2070 }
2071 return (u8)(data >> 8);
2072}
2073
Jean Delvare421ef472005-10-26 21:28:55 +02002074/* Incremental CRC8 over count bytes in the array pointed to by p */
2075static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
2077 int i;
2078
Farid Hammane7225acf2010-05-21 18:40:58 +02002079 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002080 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return crc;
2082}
2083
Jean Delvare421ef472005-10-26 21:28:55 +02002084/* Assume a 7-bit address, which is reasonable for SMBus */
2085static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Jean Delvare421ef472005-10-26 21:28:55 +02002087 /* The address will be sent first */
2088 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2089 pec = i2c_smbus_pec(pec, &addr, 1);
2090
2091 /* The data buffer follows */
2092 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Jean Delvare421ef472005-10-26 21:28:55 +02002095/* Used for write only transactions */
2096static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Jean Delvare421ef472005-10-26 21:28:55 +02002098 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2099 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Jean Delvare421ef472005-10-26 21:28:55 +02002102/* Return <0 on CRC error
2103 If there was a write before this read (most cases) we need to take the
2104 partial CRC from the write part into account.
2105 Note that this function does modify the message (we need to decrease the
2106 message length to hide the CRC byte from the caller). */
2107static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Jean Delvare421ef472005-10-26 21:28:55 +02002109 u8 rpec = msg->buf[--msg->len];
2110 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (rpec != cpec) {
2113 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2114 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002115 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
David Brownell438d6c22006-12-10 21:21:31 +01002117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
David Brownella1cdeda2008-07-14 22:38:24 +02002120/**
2121 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2122 * @client: Handle to slave device
2123 *
2124 * This executes the SMBus "receive byte" protocol, returning negative errno
2125 * else the byte received from the device.
2126 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002127s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
2129 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002130 int status;
2131
2132 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2133 I2C_SMBUS_READ, 0,
2134 I2C_SMBUS_BYTE, &data);
2135 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
David Brownellc0564602007-05-01 23:26:31 +02002137EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
David Brownella1cdeda2008-07-14 22:38:24 +02002139/**
2140 * i2c_smbus_write_byte - SMBus "send byte" protocol
2141 * @client: Handle to slave device
2142 * @value: Byte to be sent
2143 *
2144 * This executes the SMBus "send byte" protocol, returning negative errno
2145 * else zero on success.
2146 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002147s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Farid Hammane7225acf2010-05-21 18:40:58 +02002149 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002150 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
David Brownellc0564602007-05-01 23:26:31 +02002152EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David Brownella1cdeda2008-07-14 22:38:24 +02002154/**
2155 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2156 * @client: Handle to slave device
2157 * @command: Byte interpreted by slave
2158 *
2159 * This executes the SMBus "read byte" protocol, returning negative errno
2160 * else a data byte received from the device.
2161 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002162s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002165 int status;
2166
2167 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2168 I2C_SMBUS_READ, command,
2169 I2C_SMBUS_BYTE_DATA, &data);
2170 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
David Brownellc0564602007-05-01 23:26:31 +02002172EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
David Brownella1cdeda2008-07-14 22:38:24 +02002174/**
2175 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2176 * @client: Handle to slave device
2177 * @command: Byte interpreted by slave
2178 * @value: Byte being written
2179 *
2180 * This executes the SMBus "write byte" protocol, returning negative errno
2181 * else zero on success.
2182 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002183s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2184 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 union i2c_smbus_data data;
2187 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002188 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2189 I2C_SMBUS_WRITE, command,
2190 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
David Brownellc0564602007-05-01 23:26:31 +02002192EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
David Brownella1cdeda2008-07-14 22:38:24 +02002194/**
2195 * i2c_smbus_read_word_data - SMBus "read word" protocol
2196 * @client: Handle to slave device
2197 * @command: Byte interpreted by slave
2198 *
2199 * This executes the SMBus "read word" protocol, returning negative errno
2200 * else a 16-bit unsigned "word" received from the device.
2201 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002202s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002205 int status;
2206
2207 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2208 I2C_SMBUS_READ, command,
2209 I2C_SMBUS_WORD_DATA, &data);
2210 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
David Brownellc0564602007-05-01 23:26:31 +02002212EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
David Brownella1cdeda2008-07-14 22:38:24 +02002214/**
2215 * i2c_smbus_write_word_data - SMBus "write word" protocol
2216 * @client: Handle to slave device
2217 * @command: Byte interpreted by slave
2218 * @value: 16-bit "word" being written
2219 *
2220 * This executes the SMBus "write word" protocol, returning negative errno
2221 * else zero on success.
2222 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002223s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2224 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 union i2c_smbus_data data;
2227 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002228 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2229 I2C_SMBUS_WRITE, command,
2230 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
David Brownellc0564602007-05-01 23:26:31 +02002232EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
David Brownella64ec072007-10-13 23:56:31 +02002234/**
David Brownella1cdeda2008-07-14 22:38:24 +02002235 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002236 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002237 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002238 * @values: Byte array into which data will be read; big enough to hold
2239 * the data returned by the slave. SMBus allows at most 32 bytes.
2240 *
David Brownella1cdeda2008-07-14 22:38:24 +02002241 * This executes the SMBus "block read" protocol, returning negative errno
2242 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002243 *
2244 * Note that using this function requires that the client's adapter support
2245 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2246 * support this; its emulation through I2C messaging relies on a specific
2247 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2248 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002249s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002250 u8 *values)
2251{
2252 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002253 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002254
David Brownell24a5bb72008-07-14 22:38:23 +02002255 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2256 I2C_SMBUS_READ, command,
2257 I2C_SMBUS_BLOCK_DATA, &data);
2258 if (status)
2259 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002260
2261 memcpy(values, &data.block[1], data.block[0]);
2262 return data.block[0];
2263}
2264EXPORT_SYMBOL(i2c_smbus_read_block_data);
2265
David Brownella1cdeda2008-07-14 22:38:24 +02002266/**
2267 * i2c_smbus_write_block_data - SMBus "block write" protocol
2268 * @client: Handle to slave device
2269 * @command: Byte interpreted by slave
2270 * @length: Size of data block; SMBus allows at most 32 bytes
2271 * @values: Byte array which will be written.
2272 *
2273 * This executes the SMBus "block write" protocol, returning negative errno
2274 * else zero on success.
2275 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002276s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002277 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278{
2279 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (length > I2C_SMBUS_BLOCK_MAX)
2282 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002284 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002285 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2286 I2C_SMBUS_WRITE, command,
2287 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
David Brownellc0564602007-05-01 23:26:31 +02002289EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002292s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002293 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294{
2295 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002296 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002297
Jean Delvare4b2643d2007-07-12 14:12:29 +02002298 if (length > I2C_SMBUS_BLOCK_MAX)
2299 length = I2C_SMBUS_BLOCK_MAX;
2300 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002301 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2302 I2C_SMBUS_READ, command,
2303 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2304 if (status < 0)
2305 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002306
2307 memcpy(values, &data.block[1], data.block[0]);
2308 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
David Brownellc0564602007-05-01 23:26:31 +02002310EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Jean Delvare0cc43a12011-01-10 22:11:23 +01002312s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002313 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002314{
2315 union i2c_smbus_data data;
2316
2317 if (length > I2C_SMBUS_BLOCK_MAX)
2318 length = I2C_SMBUS_BLOCK_MAX;
2319 data.block[0] = length;
2320 memcpy(data.block + 1, values, length);
2321 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2322 I2C_SMBUS_WRITE, command,
2323 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2324}
David Brownellc0564602007-05-01 23:26:31 +02002325EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002326
David Brownell438d6c22006-12-10 21:21:31 +01002327/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002329static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2330 unsigned short flags,
2331 char read_write, u8 command, int size,
2332 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 /* So we need to generate a series of msgs. In the case of writing, we
2335 need to use only one message; when reading, we need two. We initialize
2336 most things with sane defaults, to keep the code below somewhat
2337 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002338 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2339 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002340 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002342 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002343 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002344 struct i2c_msg msg[2] = {
2345 {
2346 .addr = addr,
2347 .flags = flags,
2348 .len = 1,
2349 .buf = msgbuf0,
2350 }, {
2351 .addr = addr,
2352 .flags = flags | I2C_M_RD,
2353 .len = 0,
2354 .buf = msgbuf1,
2355 },
2356 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002359 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 case I2C_SMBUS_QUICK:
2361 msg[0].len = 0;
2362 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002363 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2364 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 num = 1;
2366 break;
2367 case I2C_SMBUS_BYTE:
2368 if (read_write == I2C_SMBUS_READ) {
2369 /* Special case: only a read! */
2370 msg[0].flags = I2C_M_RD | flags;
2371 num = 1;
2372 }
2373 break;
2374 case I2C_SMBUS_BYTE_DATA:
2375 if (read_write == I2C_SMBUS_READ)
2376 msg[1].len = 1;
2377 else {
2378 msg[0].len = 2;
2379 msgbuf0[1] = data->byte;
2380 }
2381 break;
2382 case I2C_SMBUS_WORD_DATA:
2383 if (read_write == I2C_SMBUS_READ)
2384 msg[1].len = 2;
2385 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002386 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002388 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390 break;
2391 case I2C_SMBUS_PROC_CALL:
2392 num = 2; /* Special case */
2393 read_write = I2C_SMBUS_READ;
2394 msg[0].len = 3;
2395 msg[1].len = 2;
2396 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002397 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 break;
2399 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002401 msg[1].flags |= I2C_M_RECV_LEN;
2402 msg[1].len = 1; /* block length will be added by
2403 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 } else {
2405 msg[0].len = data->block[0] + 2;
2406 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002407 dev_err(&adapter->dev,
2408 "Invalid block write size %d\n",
2409 data->block[0]);
2410 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002412 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 msgbuf0[i] = data->block[i-1];
2414 }
2415 break;
2416 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002417 num = 2; /* Another special case */
2418 read_write = I2C_SMBUS_READ;
2419 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002420 dev_err(&adapter->dev,
2421 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002422 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002423 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002424 }
2425 msg[0].len = data->block[0] + 2;
2426 for (i = 1; i < msg[0].len; i++)
2427 msgbuf0[i] = data->block[i-1];
2428 msg[1].flags |= I2C_M_RECV_LEN;
2429 msg[1].len = 1; /* block length will be added by
2430 the underlying bus driver */
2431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 case I2C_SMBUS_I2C_BLOCK_DATA:
2433 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002434 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 } else {
2436 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002437 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002438 dev_err(&adapter->dev,
2439 "Invalid block write size %d\n",
2440 data->block[0]);
2441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443 for (i = 1; i <= data->block[0]; i++)
2444 msgbuf0[i] = data->block[i];
2445 }
2446 break;
2447 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002448 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2449 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 }
2451
Jean Delvare421ef472005-10-26 21:28:55 +02002452 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2453 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2454 if (i) {
2455 /* Compute PEC if first message is a write */
2456 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002457 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002458 i2c_smbus_add_pec(&msg[0]);
2459 else /* Write followed by read */
2460 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2461 }
2462 /* Ask for PEC if last message is a read */
2463 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002464 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002465 }
2466
David Brownell24a5bb72008-07-14 22:38:23 +02002467 status = i2c_transfer(adapter, msg, num);
2468 if (status < 0)
2469 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jean Delvare421ef472005-10-26 21:28:55 +02002471 /* Check PEC if last message is a read */
2472 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002473 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2474 if (status < 0)
2475 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002476 }
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002479 switch (size) {
2480 case I2C_SMBUS_BYTE:
2481 data->byte = msgbuf0[0];
2482 break;
2483 case I2C_SMBUS_BYTE_DATA:
2484 data->byte = msgbuf1[0];
2485 break;
2486 case I2C_SMBUS_WORD_DATA:
2487 case I2C_SMBUS_PROC_CALL:
2488 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2489 break;
2490 case I2C_SMBUS_I2C_BLOCK_DATA:
2491 for (i = 0; i < data->block[0]; i++)
2492 data->block[i+1] = msgbuf1[i];
2493 break;
2494 case I2C_SMBUS_BLOCK_DATA:
2495 case I2C_SMBUS_BLOCK_PROC_CALL:
2496 for (i = 0; i < msgbuf1[0] + 1; i++)
2497 data->block[i] = msgbuf1[i];
2498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
2500 return 0;
2501}
2502
David Brownella1cdeda2008-07-14 22:38:24 +02002503/**
2504 * i2c_smbus_xfer - execute SMBus protocol operations
2505 * @adapter: Handle to I2C bus
2506 * @addr: Address of SMBus slave on that bus
2507 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2508 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2509 * @command: Byte interpreted by slave, for protocols which use such bytes
2510 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2511 * @data: Data to be read or written
2512 *
2513 * This executes an SMBus protocol operation, and returns a negative
2514 * errno code else zero on success.
2515 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002516s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002517 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002518 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002520 unsigned long orig_jiffies;
2521 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002524 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002527 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002528
2529 /* Retry automatically on arbitration loss */
2530 orig_jiffies = jiffies;
2531 for (res = 0, try = 0; try <= adapter->retries; try++) {
2532 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2533 read_write, command,
2534 protocol, data);
2535 if (res != -EAGAIN)
2536 break;
2537 if (time_after(jiffies,
2538 orig_jiffies + adapter->timeout))
2539 break;
2540 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002541 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002543 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2544 return res;
2545 /*
2546 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2547 * implement native support for the SMBus operation.
2548 */
2549 }
2550
2551 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2552 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2557MODULE_DESCRIPTION("I2C-Bus main module");
2558MODULE_LICENSE("GPL");