blob: bc9a294c07f794cd0db2b6ef63cb72b78f18fb30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* i2c-core.c - a device driver for the iic-bus interface */
2/* ------------------------------------------------------------------------- */
3/* Copyright (C) 1995-99 Simon G. Vogl
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
Jean Delvare5694f8a2012-03-26 21:47:19 +020017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 MA 02110-1301 USA. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/* ------------------------------------------------------------------------- */
20
Jan Engelhardt96de0e22007-10-19 23:21:04 +020021/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
Jean Delvare421ef472005-10-26 21:28:55 +020023 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
Michael Lawnick08263742010-08-11 18:21:02 +020024 Jean Delvare <khali@linux-fr.org>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
Wolfram Sang687b81d2013-07-11 12:56:15 +010026 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/kernel.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053034#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
Viresh Kumar5f9296b2012-02-28 18:26:31 +053036#include <linux/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/slab.h>
38#include <linux/i2c.h>
39#include <linux/init.h>
40#include <linux/idr.h>
Arjan van de Venb3585e42006-01-11 10:50:26 +010041#include <linux/mutex.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010042#include <linux/of.h>
Grant Likely959e85f2010-06-08 07:48:19 -060043#include <linux/of_device.h>
Wolfram Sang687b81d2013-07-11 12:56:15 +010044#include <linux/of_irq.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010045#include <linux/completion.h>
Mike Rapoportcea443a82008-01-27 18:14:50 +010046#include <linux/hardirq.h>
47#include <linux/irqflags.h>
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +020048#include <linux/rwsem.h>
Mark Brown6de468a2010-03-02 12:23:46 +010049#include <linux/pm_runtime.h>
Mika Westerberg907ddf82012-11-23 12:23:40 +010050#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/uaccess.h>
52
David Brownell9c1600e2007-05-01 23:26:31 +020053#include "i2c-core.h"
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jean Delvare6629dcf2010-05-04 11:09:28 +020056/* core_lock protects i2c_adapter_idr, and guarantees
Jean Delvare35fc37f2009-06-19 16:58:19 +020057 that device detection, deletion of detected devices, and attach_adapter
Lars-Peter Clausen19baba42013-03-09 08:16:44 +000058 calls are serialized */
Jean Delvarecaada322008-01-27 18:14:49 +010059static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_IDR(i2c_adapter_idr);
61
Jean Delvare4f8cf822009-09-18 22:45:46 +020062static struct device_type i2c_client_type;
Jean Delvare4735c982008-07-14 22:38:36 +020063static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010064
65/* ------------------------------------------------------------------------- */
66
Jean Delvared2653e92008-04-29 23:11:39 +020067static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
68 const struct i2c_client *client)
69{
70 while (id->name[0]) {
71 if (strcmp(client->name, id->name) == 0)
72 return id;
73 id++;
74 }
75 return NULL;
76}
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static int i2c_device_match(struct device *dev, struct device_driver *drv)
79{
Jean Delvare51298d12009-09-18 22:45:45 +020080 struct i2c_client *client = i2c_verify_client(dev);
81 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020082
Jean Delvare51298d12009-09-18 22:45:45 +020083 if (!client)
84 return 0;
85
Grant Likely959e85f2010-06-08 07:48:19 -060086 /* Attempt an OF style match */
87 if (of_driver_match_device(dev, drv))
88 return 1;
89
Mika Westerberg907ddf82012-11-23 12:23:40 +010090 /* Then ACPI style match */
91 if (acpi_driver_match_device(dev, drv))
92 return 1;
93
Jean Delvare51298d12009-09-18 22:45:45 +020094 driver = to_i2c_driver(drv);
Jean Delvared2653e92008-04-29 23:11:39 +020095 /* match on an id table if there is one */
96 if (driver->id_table)
97 return i2c_match_id(driver->id_table, client) != NULL;
98
Jean Delvareeb8a7902008-05-18 20:49:41 +020099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
103/* uevent helps with hotplug: modprobe -q $(MODALIAS) */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200104static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200105{
106 struct i2c_client *client = to_i2c_client(dev);
David Brownell7b4fbc52007-05-01 23:26:30 +0200107
Jean Delvareeb8a7902008-05-18 20:49:41 +0200108 if (add_uevent_var(env, "MODALIAS=%s%s",
109 I2C_MODULE_PREFIX, client->name))
110 return -ENOMEM;
David Brownell7b4fbc52007-05-01 23:26:30 +0200111 dev_dbg(dev, "uevent\n");
112 return 0;
113}
114
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530115/* i2c bus recovery routines */
116static int get_scl_gpio_value(struct i2c_adapter *adap)
117{
118 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
119}
120
121static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
122{
123 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
124}
125
126static int get_sda_gpio_value(struct i2c_adapter *adap)
127{
128 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
129}
130
131static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
132{
133 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
134 struct device *dev = &adap->dev;
135 int ret = 0;
136
137 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
138 GPIOF_OUT_INIT_HIGH, "i2c-scl");
139 if (ret) {
140 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
141 return ret;
142 }
143
144 if (bri->get_sda) {
145 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
146 /* work without SDA polling */
147 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
148 bri->sda_gpio);
149 bri->get_sda = NULL;
150 }
151 }
152
153 return ret;
154}
155
156static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
157{
158 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
159
160 if (bri->get_sda)
161 gpio_free(bri->sda_gpio);
162
163 gpio_free(bri->scl_gpio);
164}
165
166/*
167 * We are generating clock pulses. ndelay() determines durating of clk pulses.
168 * We will generate clock with rate 100 KHz and so duration of both clock levels
169 * is: delay in ns = (10^6 / 100) / 2
170 */
171#define RECOVERY_NDELAY 5000
172#define RECOVERY_CLK_CNT 9
173
174static int i2c_generic_recovery(struct i2c_adapter *adap)
175{
176 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
177 int i = 0, val = 1, ret = 0;
178
179 if (bri->prepare_recovery)
180 bri->prepare_recovery(bri);
181
182 /*
183 * By this time SCL is high, as we need to give 9 falling-rising edges
184 */
185 while (i++ < RECOVERY_CLK_CNT * 2) {
186 if (val) {
187 /* Break if SDA is high */
188 if (bri->get_sda && bri->get_sda(adap))
189 break;
190 /* SCL shouldn't be low here */
191 if (!bri->get_scl(adap)) {
192 dev_err(&adap->dev,
193 "SCL is stuck low, exit recovery\n");
194 ret = -EBUSY;
195 break;
196 }
197 }
198
199 val = !val;
200 bri->set_scl(adap, val);
201 ndelay(RECOVERY_NDELAY);
202 }
203
204 if (bri->unprepare_recovery)
205 bri->unprepare_recovery(bri);
206
207 return ret;
208}
209
210int i2c_generic_scl_recovery(struct i2c_adapter *adap)
211{
212 adap->bus_recovery_info->set_scl(adap, 1);
213 return i2c_generic_recovery(adap);
214}
215
216int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
217{
218 int ret;
219
220 ret = i2c_get_gpios_for_recovery(adap);
221 if (ret)
222 return ret;
223
224 ret = i2c_generic_recovery(adap);
225 i2c_put_gpios_for_recovery(adap);
226
227 return ret;
228}
229
230int i2c_recover_bus(struct i2c_adapter *adap)
231{
232 if (!adap->bus_recovery_info)
233 return -EOPNOTSUPP;
234
235 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
236 return adap->bus_recovery_info->recover_bus(adap);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int i2c_device_probe(struct device *dev)
240{
Jean Delvare51298d12009-09-18 22:45:45 +0200241 struct i2c_client *client = i2c_verify_client(dev);
242 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100243 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200244
Jean Delvare51298d12009-09-18 22:45:45 +0200245 if (!client)
246 return 0;
247
248 driver = to_i2c_driver(dev->driver);
Jean Delvaree0457442008-07-14 22:38:30 +0200249 if (!driver->probe || !driver->id_table)
David Brownell7b4fbc52007-05-01 23:26:30 +0200250 return -ENODEV;
251 client->driver = driver;
Marc Pignatee354252008-08-28 08:33:22 +0200252 if (!device_can_wakeup(&client->dev))
253 device_init_wakeup(&client->dev,
254 client->flags & I2C_CLIENT_WAKE);
David Brownell7b4fbc52007-05-01 23:26:30 +0200255 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200256
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300257 acpi_dev_pm_attach(&client->dev, true);
Jean Delvaree0457442008-07-14 22:38:30 +0200258 status = driver->probe(client, i2c_match_id(driver->id_table, client));
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200259 if (status) {
Hans Verkuil50c33042008-03-12 14:15:00 +0100260 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200261 i2c_set_clientdata(client, NULL);
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300262 acpi_dev_pm_detach(&client->dev, true);
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200263 }
Hans Verkuil50c33042008-03-12 14:15:00 +0100264 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267static int i2c_device_remove(struct device *dev)
268{
Jean Delvare51298d12009-09-18 22:45:45 +0200269 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200270 struct i2c_driver *driver;
271 int status;
272
Jean Delvare51298d12009-09-18 22:45:45 +0200273 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200274 return 0;
275
276 driver = to_i2c_driver(dev->driver);
277 if (driver->remove) {
278 dev_dbg(dev, "remove\n");
279 status = driver->remove(client);
280 } else {
281 dev->driver = NULL;
282 status = 0;
283 }
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200284 if (status == 0) {
David Brownella1d9e6e2007-05-01 23:26:30 +0200285 client->driver = NULL;
Wolfram Sange4a7b9b2010-05-04 11:09:27 +0200286 i2c_set_clientdata(client, NULL);
287 }
Lv Zhenga76e9bd2013-10-10 13:28:47 +0300288 acpi_dev_pm_detach(&client->dev, true);
David Brownella1d9e6e2007-05-01 23:26:30 +0200289 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
David Brownellf37dd802007-02-13 22:09:00 +0100292static void i2c_device_shutdown(struct device *dev)
293{
Jean Delvare51298d12009-09-18 22:45:45 +0200294 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100295 struct i2c_driver *driver;
296
Jean Delvare51298d12009-09-18 22:45:45 +0200297 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100298 return;
299 driver = to_i2c_driver(dev->driver);
300 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200301 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100302}
303
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200304#ifdef CONFIG_PM_SLEEP
305static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
David Brownellf37dd802007-02-13 22:09:00 +0100306{
Jean Delvare51298d12009-09-18 22:45:45 +0200307 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100308 struct i2c_driver *driver;
309
Jean Delvare51298d12009-09-18 22:45:45 +0200310 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100311 return 0;
312 driver = to_i2c_driver(dev->driver);
313 if (!driver->suspend)
314 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200315 return driver->suspend(client, mesg);
David Brownellf37dd802007-02-13 22:09:00 +0100316}
317
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200318static int i2c_legacy_resume(struct device *dev)
David Brownellf37dd802007-02-13 22:09:00 +0100319{
Jean Delvare51298d12009-09-18 22:45:45 +0200320 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100321 struct i2c_driver *driver;
322
Jean Delvare51298d12009-09-18 22:45:45 +0200323 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100324 return 0;
325 driver = to_i2c_driver(dev->driver);
326 if (!driver->resume)
327 return 0;
Jean Delvare51298d12009-09-18 22:45:45 +0200328 return driver->resume(client);
David Brownellf37dd802007-02-13 22:09:00 +0100329}
330
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200331static int i2c_device_pm_suspend(struct device *dev)
332{
333 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
334
Mark Brownd529de22011-01-14 22:03:49 +0100335 if (pm)
336 return pm_generic_suspend(dev);
337 else
338 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200339}
340
341static int i2c_device_pm_resume(struct device *dev)
342{
343 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200344
345 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100346 return pm_generic_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200347 else
Mark Brownd529de22011-01-14 22:03:49 +0100348 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200349}
350
351static int i2c_device_pm_freeze(struct device *dev)
352{
353 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
354
Mark Brownd529de22011-01-14 22:03:49 +0100355 if (pm)
356 return pm_generic_freeze(dev);
357 else
358 return i2c_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200359}
360
361static int i2c_device_pm_thaw(struct device *dev)
362{
363 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
364
Mark Brownd529de22011-01-14 22:03:49 +0100365 if (pm)
366 return pm_generic_thaw(dev);
367 else
368 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200369}
370
371static int i2c_device_pm_poweroff(struct device *dev)
372{
373 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
374
Mark Brownd529de22011-01-14 22:03:49 +0100375 if (pm)
376 return pm_generic_poweroff(dev);
377 else
378 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200379}
380
381static int i2c_device_pm_restore(struct device *dev)
382{
383 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200384
385 if (pm)
Mark Brownd529de22011-01-14 22:03:49 +0100386 return pm_generic_restore(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200387 else
Mark Brownd529de22011-01-14 22:03:49 +0100388 return i2c_legacy_resume(dev);
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200389}
390#else /* !CONFIG_PM_SLEEP */
391#define i2c_device_pm_suspend NULL
392#define i2c_device_pm_resume NULL
393#define i2c_device_pm_freeze NULL
394#define i2c_device_pm_thaw NULL
395#define i2c_device_pm_poweroff NULL
396#define i2c_device_pm_restore NULL
397#endif /* !CONFIG_PM_SLEEP */
398
David Brownell9c1600e2007-05-01 23:26:31 +0200399static void i2c_client_dev_release(struct device *dev)
400{
401 kfree(to_i2c_client(dev));
402}
403
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100404static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200405show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200406{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200407 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
408 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200409}
410
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100411static ssize_t
412show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200413{
414 struct i2c_client *client = to_i2c_client(dev);
Jean Delvareeb8a7902008-05-18 20:49:41 +0200415 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200416}
417
Jean Delvare4f8cf822009-09-18 22:45:46 +0200418static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
Jean Delvare51298d12009-09-18 22:45:45 +0200419static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
420
421static struct attribute *i2c_dev_attrs[] = {
422 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200423 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200424 &dev_attr_modalias.attr,
425 NULL
426};
427
428static struct attribute_group i2c_dev_attr_group = {
429 .attrs = i2c_dev_attrs,
430};
431
432static const struct attribute_group *i2c_dev_attr_groups[] = {
433 &i2c_dev_attr_group,
434 NULL
David Brownell7b4fbc52007-05-01 23:26:30 +0200435};
436
Tobias Klauser0b2c3682010-01-16 20:43:12 +0100437static const struct dev_pm_ops i2c_device_pm_ops = {
sonic zhang54067ee2009-12-14 21:17:30 +0100438 .suspend = i2c_device_pm_suspend,
439 .resume = i2c_device_pm_resume,
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200440 .freeze = i2c_device_pm_freeze,
441 .thaw = i2c_device_pm_thaw,
442 .poweroff = i2c_device_pm_poweroff,
443 .restore = i2c_device_pm_restore,
444 SET_RUNTIME_PM_OPS(
445 pm_generic_runtime_suspend,
446 pm_generic_runtime_resume,
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200447 NULL
Rafael J. Wysocki2f60ba72010-05-10 23:09:30 +0200448 )
sonic zhang54067ee2009-12-14 21:17:30 +0100449};
450
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200451struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100452 .name = "i2c",
453 .match = i2c_device_match,
454 .probe = i2c_device_probe,
455 .remove = i2c_device_remove,
456 .shutdown = i2c_device_shutdown,
sonic zhang54067ee2009-12-14 21:17:30 +0100457 .pm = &i2c_device_pm_ops,
Russell Kingb864c7d2006-01-05 14:37:50 +0000458};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200459EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000460
Jean Delvare51298d12009-09-18 22:45:45 +0200461static struct device_type i2c_client_type = {
462 .groups = i2c_dev_attr_groups,
463 .uevent = i2c_device_uevent,
464 .release = i2c_client_dev_release,
465};
466
David Brownell9b766b82008-01-27 18:14:51 +0100467
468/**
469 * i2c_verify_client - return parameter as i2c_client, or NULL
470 * @dev: device, probably from some driver model iterator
471 *
472 * When traversing the driver model tree, perhaps using driver model
473 * iterators like @device_for_each_child(), you can't assume very much
474 * about the nodes you find. Use this function to avoid oopses caused
475 * by wrongly treating some non-I2C device as an i2c_client.
476 */
477struct i2c_client *i2c_verify_client(struct device *dev)
478{
Jean Delvare51298d12009-09-18 22:45:45 +0200479 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100480 ? to_i2c_client(dev)
481 : NULL;
482}
483EXPORT_SYMBOL(i2c_verify_client);
484
485
Jean Delvare3a89db52010-06-03 11:33:52 +0200486/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300487 * are purposely not enforced, except for the general call address. */
Jean Delvare3a89db52010-06-03 11:33:52 +0200488static int i2c_check_client_addr_validity(const struct i2c_client *client)
489{
490 if (client->flags & I2C_CLIENT_TEN) {
491 /* 10-bit address, all values are valid */
492 if (client->addr > 0x3ff)
493 return -EINVAL;
494 } else {
495 /* 7-bit address, reject the general call address */
496 if (client->addr == 0x00 || client->addr > 0x7f)
497 return -EINVAL;
498 }
499 return 0;
500}
501
Jean Delvare656b8762010-06-03 11:33:53 +0200502/* And this is a strict address validity check, used when probing. If a
503 * device uses a reserved address, then it shouldn't be probed. 7-bit
504 * addressing is assumed, 10-bit address devices are rare and should be
505 * explicitly enumerated. */
506static int i2c_check_addr_validity(unsigned short addr)
507{
508 /*
509 * Reserved addresses per I2C specification:
510 * 0x00 General call address / START byte
511 * 0x01 CBUS address
512 * 0x02 Reserved for different bus format
513 * 0x03 Reserved for future purposes
514 * 0x04-0x07 Hs-mode master code
515 * 0x78-0x7b 10-bit slave addressing
516 * 0x7c-0x7f Reserved for future purposes
517 */
518 if (addr < 0x08 || addr > 0x77)
519 return -EINVAL;
520 return 0;
521}
522
Jean Delvare3b5f7942010-06-03 11:33:55 +0200523static int __i2c_check_addr_busy(struct device *dev, void *addrp)
524{
525 struct i2c_client *client = i2c_verify_client(dev);
526 int addr = *(int *)addrp;
527
528 if (client && client->addr == addr)
529 return -EBUSY;
530 return 0;
531}
532
Michael Lawnick08263742010-08-11 18:21:02 +0200533/* walk up mux tree */
534static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
535{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200536 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200537 int result;
538
539 result = device_for_each_child(&adapter->dev, &addr,
540 __i2c_check_addr_busy);
541
Jean Delvare97cc4d42010-10-24 18:16:57 +0200542 if (!result && parent)
543 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200544
545 return result;
546}
547
548/* recurse down mux tree */
549static int i2c_check_mux_children(struct device *dev, void *addrp)
550{
551 int result;
552
553 if (dev->type == &i2c_adapter_type)
554 result = device_for_each_child(dev, addrp,
555 i2c_check_mux_children);
556 else
557 result = __i2c_check_addr_busy(dev, addrp);
558
559 return result;
560}
561
Jean Delvare3b5f7942010-06-03 11:33:55 +0200562static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
563{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200564 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200565 int result = 0;
566
Jean Delvare97cc4d42010-10-24 18:16:57 +0200567 if (parent)
568 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200569
570 if (!result)
571 result = device_for_each_child(&adapter->dev, &addr,
572 i2c_check_mux_children);
573
574 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200575}
576
David Brownell9c1600e2007-05-01 23:26:31 +0200577/**
Jean Delvarefe61e072010-08-11 18:20:58 +0200578 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
579 * @adapter: Target I2C bus segment
580 */
581void i2c_lock_adapter(struct i2c_adapter *adapter)
582{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200583 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
584
585 if (parent)
586 i2c_lock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200587 else
588 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200589}
590EXPORT_SYMBOL_GPL(i2c_lock_adapter);
591
592/**
593 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
594 * @adapter: Target I2C bus segment
595 */
596static int i2c_trylock_adapter(struct i2c_adapter *adapter)
597{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200598 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
599
600 if (parent)
601 return i2c_trylock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200602 else
603 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200604}
605
606/**
607 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
608 * @adapter: Target I2C bus segment
609 */
610void i2c_unlock_adapter(struct i2c_adapter *adapter)
611{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200612 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
613
614 if (parent)
615 i2c_unlock_adapter(parent);
Michael Lawnick08263742010-08-11 18:21:02 +0200616 else
617 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200618}
619EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
620
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200621static void i2c_dev_set_name(struct i2c_adapter *adap,
622 struct i2c_client *client)
623{
624 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
625
626 if (adev) {
627 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
628 return;
629 }
630
631 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
632 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
633 client->addr | ((client->flags & I2C_CLIENT_TEN)
634 ? 0xa000 : 0));
635}
636
Jean Delvarefe61e072010-08-11 18:20:58 +0200637/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200638 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200639 * @adap: the adapter managing the device
640 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200641 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200642 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200643 * Create an i2c device. Binding is handled through driver model
644 * probe()/remove() methods. A driver may be bound to this device when we
645 * return from this function, or any later moment (e.g. maybe hotplugging will
646 * load the driver module). This call is not appropriate for use by mainboard
647 * initialization logic, which usually runs during an arch_initcall() long
648 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200649 *
650 * This returns the new i2c client, which may be saved for later use with
651 * i2c_unregister_device(); or NULL to indicate an error.
652 */
653struct i2c_client *
654i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
655{
656 struct i2c_client *client;
657 int status;
658
659 client = kzalloc(sizeof *client, GFP_KERNEL);
660 if (!client)
661 return NULL;
662
663 client->adapter = adap;
664
665 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200666
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200667 if (info->archdata)
668 client->dev.archdata = *info->archdata;
669
Marc Pignatee354252008-08-28 08:33:22 +0200670 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200671 client->addr = info->addr;
672 client->irq = info->irq;
673
David Brownell9c1600e2007-05-01 23:26:31 +0200674 strlcpy(client->name, info->type, sizeof(client->name));
675
Jean Delvare3a89db52010-06-03 11:33:52 +0200676 /* Check for address validity */
677 status = i2c_check_client_addr_validity(client);
678 if (status) {
679 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
680 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
681 goto out_err_silent;
682 }
683
Jean Delvaref8a227e2009-06-19 16:58:18 +0200684 /* Check for address business */
Jean Delvare3b5f7942010-06-03 11:33:55 +0200685 status = i2c_check_addr_busy(adap, client->addr);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200686 if (status)
687 goto out_err;
688
689 client->dev.parent = &client->adapter->dev;
690 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200691 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700692 client->dev.of_node = info->of_node;
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100693 ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200694
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200695 i2c_dev_set_name(adap, client);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200696 status = device_register(&client->dev);
697 if (status)
698 goto out_err;
699
Jean Delvaref8a227e2009-06-19 16:58:18 +0200700 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
701 client->name, dev_name(&client->dev));
702
David Brownell9c1600e2007-05-01 23:26:31 +0200703 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200704
705out_err:
706 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
707 "(%d)\n", client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200708out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200709 kfree(client);
710 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200711}
712EXPORT_SYMBOL_GPL(i2c_new_device);
713
714
715/**
716 * i2c_unregister_device - reverse effect of i2c_new_device()
717 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200718 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200719 */
720void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200721{
David Brownella1d9e6e2007-05-01 23:26:30 +0200722 device_unregister(&client->dev);
723}
David Brownell9c1600e2007-05-01 23:26:31 +0200724EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200725
726
Jean Delvare60b129d2008-05-11 20:37:06 +0200727static const struct i2c_device_id dummy_id[] = {
728 { "dummy", 0 },
729 { },
730};
731
Jean Delvared2653e92008-04-29 23:11:39 +0200732static int dummy_probe(struct i2c_client *client,
733 const struct i2c_device_id *id)
734{
735 return 0;
736}
737
738static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100739{
740 return 0;
741}
742
743static struct i2c_driver dummy_driver = {
744 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200745 .probe = dummy_probe,
746 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200747 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100748};
749
750/**
751 * i2c_new_dummy - return a new i2c device bound to a dummy driver
752 * @adapter: the adapter managing the device
753 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100754 * Context: can sleep
755 *
756 * This returns an I2C client bound to the "dummy" driver, intended for use
757 * with devices that consume multiple addresses. Examples of such chips
758 * include various EEPROMS (like 24c04 and 24c08 models).
759 *
760 * These dummy devices have two main uses. First, most I2C and SMBus calls
761 * except i2c_transfer() need a client handle; the dummy will be that handle.
762 * And second, this prevents the specified address from being bound to a
763 * different driver.
764 *
765 * This returns the new i2c client, which should be saved for later use with
766 * i2c_unregister_device(); or NULL to indicate an error.
767 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100768struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100769{
770 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200771 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100772 };
773
David Brownelle9f13732008-01-27 18:14:52 +0100774 return i2c_new_device(adapter, &info);
775}
776EXPORT_SYMBOL_GPL(i2c_new_dummy);
777
David Brownellf37dd802007-02-13 22:09:00 +0100778/* ------------------------------------------------------------------------- */
779
David Brownell16ffadf2007-05-01 23:26:28 +0200780/* I2C bus adapters -- one roots each I2C or SMBUS segment */
781
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200782static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
David Brownellef2c83212007-05-01 23:26:28 +0200784 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 complete(&adap->dev_released);
786}
787
Jean Delvare99cd8e22009-06-19 16:58:20 +0200788/*
Jean Delvare390946b2012-09-10 10:14:02 +0200789 * This function is only needed for mutex_lock_nested, so it is never
790 * called unless locking correctness checking is enabled. Thus we
791 * make it inline to avoid a compiler warning. That's what gcc ends up
792 * doing anyway.
793 */
794static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
795{
796 unsigned int depth = 0;
797
798 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
799 depth++;
800
801 return depth;
802}
803
804/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200805 * Let users instantiate I2C devices through sysfs. This can be used when
806 * platform initialization code doesn't contain the proper data for
807 * whatever reason. Also useful for drivers that do device detection and
808 * detection fails, either because the device uses an unexpected address,
809 * or this is a compatible device with different ID register values.
810 *
811 * Parameter checking may look overzealous, but we really don't want
812 * the user to provide incorrect parameters.
813 */
814static ssize_t
815i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
816 const char *buf, size_t count)
817{
818 struct i2c_adapter *adap = to_i2c_adapter(dev);
819 struct i2c_board_info info;
820 struct i2c_client *client;
821 char *blank, end;
822 int res;
823
Jean Delvare99cd8e22009-06-19 16:58:20 +0200824 memset(&info, 0, sizeof(struct i2c_board_info));
825
826 blank = strchr(buf, ' ');
827 if (!blank) {
828 dev_err(dev, "%s: Missing parameters\n", "new_device");
829 return -EINVAL;
830 }
831 if (blank - buf > I2C_NAME_SIZE - 1) {
832 dev_err(dev, "%s: Invalid device name\n", "new_device");
833 return -EINVAL;
834 }
835 memcpy(info.type, buf, blank - buf);
836
837 /* Parse remaining parameters, reject extra parameters */
838 res = sscanf(++blank, "%hi%c", &info.addr, &end);
839 if (res < 1) {
840 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
841 return -EINVAL;
842 }
843 if (res > 1 && end != '\n') {
844 dev_err(dev, "%s: Extra parameters\n", "new_device");
845 return -EINVAL;
846 }
847
Jean Delvare99cd8e22009-06-19 16:58:20 +0200848 client = i2c_new_device(adap, &info);
849 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200850 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200851
852 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200853 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200854 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200855 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200856 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
857 info.type, info.addr);
858
859 return count;
860}
861
862/*
863 * And of course let the users delete the devices they instantiated, if
864 * they got it wrong. This interface can only be used to delete devices
865 * instantiated by i2c_sysfs_new_device above. This guarantees that we
866 * don't delete devices to which some kernel code still has references.
867 *
868 * Parameter checking may look overzealous, but we really don't want
869 * the user to delete the wrong device.
870 */
871static ssize_t
872i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
873 const char *buf, size_t count)
874{
875 struct i2c_adapter *adap = to_i2c_adapter(dev);
876 struct i2c_client *client, *next;
877 unsigned short addr;
878 char end;
879 int res;
880
881 /* Parse parameters, reject extra parameters */
882 res = sscanf(buf, "%hi%c", &addr, &end);
883 if (res < 1) {
884 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
885 return -EINVAL;
886 }
887 if (res > 1 && end != '\n') {
888 dev_err(dev, "%s: Extra parameters\n", "delete_device");
889 return -EINVAL;
890 }
891
892 /* Make sure the device was added through sysfs */
893 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200894 mutex_lock_nested(&adap->userspace_clients_lock,
895 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +0200896 list_for_each_entry_safe(client, next, &adap->userspace_clients,
897 detected) {
898 if (client->addr == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +0200899 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
900 "delete_device", client->name, client->addr);
901
902 list_del(&client->detected);
903 i2c_unregister_device(client);
904 res = count;
905 break;
906 }
907 }
Jean Delvaredafc50d2010-08-11 18:21:01 +0200908 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200909
910 if (res < 0)
911 dev_err(dev, "%s: Can't find device in list\n",
912 "delete_device");
913 return res;
914}
915
Jean Delvare4f8cf822009-09-18 22:45:46 +0200916static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Alexander Sverdline9b526f2013-05-17 14:56:35 +0200917static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
918 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +0200919
920static struct attribute *i2c_adapter_attrs[] = {
921 &dev_attr_name.attr,
922 &dev_attr_new_device.attr,
923 &dev_attr_delete_device.attr,
924 NULL
David Brownell16ffadf2007-05-01 23:26:28 +0200925};
926
Jean Delvare4f8cf822009-09-18 22:45:46 +0200927static struct attribute_group i2c_adapter_attr_group = {
928 .attrs = i2c_adapter_attrs,
929};
930
931static const struct attribute_group *i2c_adapter_attr_groups[] = {
932 &i2c_adapter_attr_group,
933 NULL
934};
935
Michael Lawnick08263742010-08-11 18:21:02 +0200936struct device_type i2c_adapter_type = {
Jean Delvare4f8cf822009-09-18 22:45:46 +0200937 .groups = i2c_adapter_attr_groups,
938 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +0200939};
Michael Lawnick08263742010-08-11 18:21:02 +0200940EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Stephen Warren643dd092012-04-17 12:43:33 -0600942/**
943 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
944 * @dev: device, probably from some driver model iterator
945 *
946 * When traversing the driver model tree, perhaps using driver model
947 * iterators like @device_for_each_child(), you can't assume very much
948 * about the nodes you find. Use this function to avoid oopses caused
949 * by wrongly treating some non-I2C device as an i2c_adapter.
950 */
951struct i2c_adapter *i2c_verify_adapter(struct device *dev)
952{
953 return (dev->type == &i2c_adapter_type)
954 ? to_i2c_adapter(dev)
955 : NULL;
956}
957EXPORT_SYMBOL(i2c_verify_adapter);
958
Jean Delvare2bb50952009-09-18 22:45:46 +0200959#ifdef CONFIG_I2C_COMPAT
960static struct class_compat *i2c_adapter_compat_class;
961#endif
962
David Brownell9c1600e2007-05-01 23:26:31 +0200963static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
964{
965 struct i2c_devinfo *devinfo;
966
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200967 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200968 list_for_each_entry(devinfo, &__i2c_board_list, list) {
969 if (devinfo->busnum == adapter->nr
970 && !i2c_new_device(adapter,
971 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100972 dev_err(&adapter->dev,
973 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +0200974 devinfo->board_info.addr);
975 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +0200976 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +0200977}
978
Wolfram Sang687b81d2013-07-11 12:56:15 +0100979/* OF support code */
980
981#if IS_ENABLED(CONFIG_OF)
982static void of_i2c_register_devices(struct i2c_adapter *adap)
983{
984 void *result;
985 struct device_node *node;
986
987 /* Only register child devices if the adapter has a node pointer set */
988 if (!adap->dev.of_node)
989 return;
990
991 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
992
993 for_each_available_child_of_node(adap->dev.of_node, node) {
994 struct i2c_board_info info = {};
995 struct dev_archdata dev_ad = {};
996 const __be32 *addr;
997 int len;
998
999 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
1000
1001 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
1002 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
1003 node->full_name);
1004 continue;
1005 }
1006
1007 addr = of_get_property(node, "reg", &len);
1008 if (!addr || (len < sizeof(int))) {
1009 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
1010 node->full_name);
1011 continue;
1012 }
1013
1014 info.addr = be32_to_cpup(addr);
1015 if (info.addr > (1 << 10) - 1) {
1016 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
1017 info.addr, node->full_name);
1018 continue;
1019 }
1020
1021 info.irq = irq_of_parse_and_map(node, 0);
1022 info.of_node = of_node_get(node);
1023 info.archdata = &dev_ad;
1024
1025 if (of_get_property(node, "wakeup-source", NULL))
1026 info.flags |= I2C_CLIENT_WAKE;
1027
1028 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
1029
1030 result = i2c_new_device(adap, &info);
1031 if (result == NULL) {
1032 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
1033 node->full_name);
1034 of_node_put(node);
1035 irq_dispose_mapping(info.irq);
1036 continue;
1037 }
1038 }
1039}
1040
1041static int of_dev_node_match(struct device *dev, void *data)
1042{
1043 return dev->of_node == data;
1044}
1045
1046/* must call put_device() when done with returned i2c_client device */
1047struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
1048{
1049 struct device *dev;
1050
1051 dev = bus_find_device(&i2c_bus_type, NULL, node,
1052 of_dev_node_match);
1053 if (!dev)
1054 return NULL;
1055
1056 return i2c_verify_client(dev);
1057}
1058EXPORT_SYMBOL(of_find_i2c_device_by_node);
1059
1060/* must call put_device() when done with returned i2c_adapter device */
1061struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
1062{
1063 struct device *dev;
1064
1065 dev = bus_find_device(&i2c_bus_type, NULL, node,
1066 of_dev_node_match);
1067 if (!dev)
1068 return NULL;
1069
1070 return i2c_verify_adapter(dev);
1071}
1072EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
1073#else
1074static void of_i2c_register_devices(struct i2c_adapter *adap) { }
1075#endif /* CONFIG_OF */
1076
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001077/* ACPI support code */
1078
1079#if IS_ENABLED(CONFIG_ACPI)
1080static int acpi_i2c_add_resource(struct acpi_resource *ares, void *data)
1081{
1082 struct i2c_board_info *info = data;
1083
1084 if (ares->type == ACPI_RESOURCE_TYPE_SERIAL_BUS) {
1085 struct acpi_resource_i2c_serialbus *sb;
1086
1087 sb = &ares->data.i2c_serial_bus;
1088 if (sb->type == ACPI_RESOURCE_SERIAL_TYPE_I2C) {
1089 info->addr = sb->slave_address;
1090 if (sb->access_mode == ACPI_I2C_10BIT_MODE)
1091 info->flags |= I2C_CLIENT_TEN;
1092 }
1093 } else if (info->irq < 0) {
1094 struct resource r;
1095
1096 if (acpi_dev_resource_interrupt(ares, 0, &r))
1097 info->irq = r.start;
1098 }
1099
1100 /* Tell the ACPI core to skip this resource */
1101 return 1;
1102}
1103
1104static acpi_status acpi_i2c_add_device(acpi_handle handle, u32 level,
1105 void *data, void **return_value)
1106{
1107 struct i2c_adapter *adapter = data;
1108 struct list_head resource_list;
1109 struct i2c_board_info info;
1110 struct acpi_device *adev;
1111 int ret;
1112
1113 if (acpi_bus_get_device(handle, &adev))
1114 return AE_OK;
1115 if (acpi_bus_get_status(adev) || !adev->status.present)
1116 return AE_OK;
1117
1118 memset(&info, 0, sizeof(info));
Rafael J. Wysocki7b199812013-11-11 22:41:56 +01001119 info.acpi_node.companion = adev;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001120 info.irq = -1;
1121
1122 INIT_LIST_HEAD(&resource_list);
1123 ret = acpi_dev_get_resources(adev, &resource_list,
1124 acpi_i2c_add_resource, &info);
1125 acpi_dev_free_resource_list(&resource_list);
1126
1127 if (ret < 0 || !info.addr)
1128 return AE_OK;
1129
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001130 adev->power.flags.ignore_parent = true;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001131 strlcpy(info.type, dev_name(&adev->dev), sizeof(info.type));
1132 if (!i2c_new_device(adapter, &info)) {
Lv Zhenga76e9bd2013-10-10 13:28:47 +03001133 adev->power.flags.ignore_parent = false;
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001134 dev_err(&adapter->dev,
1135 "failed to add I2C device %s from ACPI\n",
1136 dev_name(&adev->dev));
1137 }
1138
1139 return AE_OK;
1140}
1141
1142/**
1143 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
1144 * @adap: pointer to adapter
1145 *
1146 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
1147 * namespace. When a device is found it will be added to the Linux device
1148 * model and bound to the corresponding ACPI handle.
1149 */
1150static void acpi_i2c_register_devices(struct i2c_adapter *adap)
1151{
1152 acpi_handle handle;
1153 acpi_status status;
1154
Jean Delvare47b6e4772013-10-10 08:04:06 +02001155 if (!adap->dev.parent)
1156 return;
1157
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001158 handle = ACPI_HANDLE(adap->dev.parent);
1159 if (!handle)
1160 return;
1161
1162 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
1163 acpi_i2c_add_device, NULL,
1164 adap, NULL);
1165 if (ACPI_FAILURE(status))
1166 dev_warn(&adap->dev, "failed to enumerate I2C slaves\n");
1167}
1168#else
1169static inline void acpi_i2c_register_devices(struct i2c_adapter *adap) {}
1170#endif /* CONFIG_ACPI */
1171
Jean Delvare69b00892009-12-06 17:06:27 +01001172static int i2c_do_add_adapter(struct i2c_driver *driver,
1173 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001174{
Jean Delvare4735c982008-07-14 22:38:36 +02001175 /* Detect supported devices on that bus, and instantiate them */
1176 i2c_detect(adap, driver);
1177
1178 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001179 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001180 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1181 driver->driver.name);
Jean Delvarefe6fc252011-03-20 14:50:53 +01001182 dev_warn(&adap->dev, "Please use another way to instantiate "
1183 "your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001184 /* We ignore the return code; if it fails, too bad */
1185 driver->attach_adapter(adap);
1186 }
1187 return 0;
1188}
1189
Jean Delvare69b00892009-12-06 17:06:27 +01001190static int __process_new_adapter(struct device_driver *d, void *data)
1191{
1192 return i2c_do_add_adapter(to_i2c_driver(d), data);
1193}
1194
David Brownell6e13e642007-05-01 23:26:31 +02001195static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Jean Delvared6703282010-08-11 18:20:59 +02001197 int res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
David Brownell1d0b19c2008-10-14 17:30:05 +02001199 /* Can't register until after driver model init */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001200 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1201 res = -EAGAIN;
1202 goto out_list;
1203 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001204
Jean Delvare2236baa2010-11-15 22:40:38 +01001205 /* Sanity checks */
1206 if (unlikely(adap->name[0] == '\0')) {
1207 pr_err("i2c-core: Attempt to register an adapter with "
1208 "no name!\n");
1209 return -EINVAL;
1210 }
1211 if (unlikely(!adap->algo)) {
1212 pr_err("i2c-core: Attempt to register adapter '%s' with "
1213 "no algo!\n", adap->name);
1214 return -EINVAL;
1215 }
1216
Mika Kuoppala194684e2009-12-06 17:06:22 +01001217 rt_mutex_init(&adap->bus_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001218 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001219 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Jean Delvare8fcfef62009-03-28 21:34:43 +01001221 /* Set default timeout to 1 second if not already set */
1222 if (adap->timeout == 0)
1223 adap->timeout = HZ;
1224
Kay Sievers27d9c182009-01-07 14:29:16 +01001225 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001226 adap->dev.bus = &i2c_bus_type;
1227 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001228 res = device_register(&adap->dev);
1229 if (res)
1230 goto out_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001232 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1233
Jean Delvare2bb50952009-09-18 22:45:46 +02001234#ifdef CONFIG_I2C_COMPAT
1235 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1236 adap->dev.parent);
1237 if (res)
1238 dev_warn(&adap->dev,
1239 "Failed to create compatibility class link\n");
1240#endif
1241
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301242 /* bus recovery specific initialization */
1243 if (adap->bus_recovery_info) {
1244 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1245
1246 if (!bri->recover_bus) {
1247 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1248 adap->bus_recovery_info = NULL;
1249 goto exit_recovery;
1250 }
1251
1252 /* Generic GPIO recovery */
1253 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1254 if (!gpio_is_valid(bri->scl_gpio)) {
1255 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1256 adap->bus_recovery_info = NULL;
1257 goto exit_recovery;
1258 }
1259
1260 if (gpio_is_valid(bri->sda_gpio))
1261 bri->get_sda = get_sda_gpio_value;
1262 else
1263 bri->get_sda = NULL;
1264
1265 bri->get_scl = get_scl_gpio_value;
1266 bri->set_scl = set_scl_gpio_value;
1267 } else if (!bri->set_scl || !bri->get_scl) {
1268 /* Generic SCL recovery */
1269 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1270 adap->bus_recovery_info = NULL;
1271 }
1272 }
1273
1274exit_recovery:
Jean Delvare729d6dd2009-06-19 16:58:18 +02001275 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001276 of_i2c_register_devices(adap);
Mika Westerberg55e71ed2013-08-21 17:28:23 +03001277 acpi_i2c_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001278
David Brownell6e13e642007-05-01 23:26:31 +02001279 if (adap->nr < __i2c_first_dynamic_bus_num)
1280 i2c_scan_static_board_info(adap);
1281
Jean Delvare4735c982008-07-14 22:38:36 +02001282 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001283 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001284 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001285 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001286
1287 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001288
Jean Delvareb119c6c2006-08-15 18:26:30 +02001289out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001290 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001291 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001292 mutex_unlock(&core_lock);
1293 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294}
1295
David Brownell6e13e642007-05-01 23:26:31 +02001296/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001297 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1298 * @adap: the adapter to register (with adap->nr initialized)
1299 * Context: can sleep
1300 *
1301 * See i2c_add_numbered_adapter() for details.
1302 */
1303static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1304{
1305 int id;
1306
1307 mutex_lock(&core_lock);
1308 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1309 GFP_KERNEL);
1310 mutex_unlock(&core_lock);
1311 if (id < 0)
1312 return id == -ENOSPC ? -EBUSY : id;
1313
1314 return i2c_register_adapter(adap);
1315}
1316
1317/**
David Brownell6e13e642007-05-01 23:26:31 +02001318 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1319 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001320 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001321 *
1322 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001323 * doesn't matter or when its bus number is specified by an dt alias.
1324 * Examples of bases when the bus number doesn't matter: I2C adapters
1325 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001326 *
1327 * When this returns zero, a new bus number was allocated and stored
1328 * in adap->nr, and the specified adapter became available for clients.
1329 * Otherwise, a negative errno value is returned.
1330 */
1331int i2c_add_adapter(struct i2c_adapter *adapter)
1332{
Doug Andersonee5c2742013-03-01 08:57:31 -08001333 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001334 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001335
Doug Andersonee5c2742013-03-01 08:57:31 -08001336 if (dev->of_node) {
1337 id = of_alias_get_id(dev->of_node, "i2c");
1338 if (id >= 0) {
1339 adapter->nr = id;
1340 return __i2c_add_numbered_adapter(adapter);
1341 }
1342 }
1343
Jean Delvarecaada322008-01-27 18:14:49 +01001344 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001345 id = idr_alloc(&i2c_adapter_idr, adapter,
1346 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001347 mutex_unlock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001348 if (id < 0)
1349 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001350
1351 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001352
David Brownell6e13e642007-05-01 23:26:31 +02001353 return i2c_register_adapter(adapter);
1354}
1355EXPORT_SYMBOL(i2c_add_adapter);
1356
1357/**
1358 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1359 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001360 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001361 *
1362 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001363 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1364 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001365 * is used to properly configure I2C devices.
1366 *
Grant Likely488bf312011-07-25 17:49:43 +02001367 * If the requested bus number is set to -1, then this function will behave
1368 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1369 *
David Brownell6e13e642007-05-01 23:26:31 +02001370 * If no devices have pre-been declared for this bus, then be sure to
1371 * register the adapter before any dynamically allocated ones. Otherwise
1372 * the required bus ID may not be available.
1373 *
1374 * When this returns zero, the specified adapter became available for
1375 * clients using the bus number provided in adap->nr. Also, the table
1376 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1377 * and the appropriate driver model device nodes are created. Otherwise, a
1378 * negative errno value is returned.
1379 */
1380int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1381{
Grant Likely488bf312011-07-25 17:49:43 +02001382 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1383 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001384
Doug Andersonee5c2742013-03-01 08:57:31 -08001385 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001386}
1387EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1388
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001389static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001390 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001391{
Jean Delvare4735c982008-07-14 22:38:36 +02001392 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001393
Jean Delvareacec2112009-03-28 21:34:40 +01001394 /* Remove the devices we created ourselves as the result of hardware
1395 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001396 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1397 if (client->adapter == adapter) {
1398 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1399 client->name, client->addr);
1400 list_del(&client->detected);
1401 i2c_unregister_device(client);
1402 }
1403 }
Jean Delvare026526f2008-01-27 18:14:49 +01001404}
1405
Jean Delvaree549c2b2009-06-19 16:58:19 +02001406static int __unregister_client(struct device *dev, void *dummy)
1407{
1408 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001409 if (client && strcmp(client->name, "dummy"))
1410 i2c_unregister_device(client);
1411 return 0;
1412}
1413
1414static int __unregister_dummy(struct device *dev, void *dummy)
1415{
1416 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001417 if (client)
1418 i2c_unregister_device(client);
1419 return 0;
1420}
1421
Jean Delvare69b00892009-12-06 17:06:27 +01001422static int __process_removed_adapter(struct device_driver *d, void *data)
1423{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001424 i2c_do_del_adapter(to_i2c_driver(d), data);
1425 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001426}
1427
David Brownelld64f73b2007-07-12 14:12:28 +02001428/**
1429 * i2c_del_adapter - unregister I2C adapter
1430 * @adap: the adapter being unregistered
1431 * Context: can sleep
1432 *
1433 * This unregisters an I2C adapter which was previously registered
1434 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1435 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001436void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001438 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001439 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001442 mutex_lock(&core_lock);
1443 found = idr_find(&i2c_adapter_idr, adap->nr);
1444 mutex_unlock(&core_lock);
1445 if (found != adap) {
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001446 pr_debug("i2c-core: attempting to delete unregistered "
1447 "adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
Jean Delvare026526f2008-01-27 18:14:49 +01001451 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001452 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001453 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001454 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001455 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001457 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001458 mutex_lock_nested(&adap->userspace_clients_lock,
1459 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001460 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1461 detected) {
1462 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1463 client->addr);
1464 list_del(&client->detected);
1465 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001466 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001467 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001468
Jean Delvaree549c2b2009-06-19 16:58:19 +02001469 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001470 * check the returned value. This is a two-pass process, because
1471 * we can't remove the dummy devices during the first pass: they
1472 * could have been instantiated by real devices wishing to clean
1473 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001474 device_for_each_child(&adap->dev, NULL, __unregister_client);
1475 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jean Delvare2bb50952009-09-18 22:45:46 +02001477#ifdef CONFIG_I2C_COMPAT
1478 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1479 adap->dev.parent);
1480#endif
1481
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001482 /* device name is gone after device_unregister */
1483 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* clean up the sysfs representation */
1486 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 /* wait for sysfs to drop all references */
1490 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
David Brownell6e13e642007-05-01 23:26:31 +02001492 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001493 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001495 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001497 /* Clear the device structure in case this adapter is ever going to be
1498 added again */
1499 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
David Brownellc0564602007-05-01 23:26:31 +02001501EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
David Brownell7b4fbc52007-05-01 23:26:30 +02001503/* ------------------------------------------------------------------------- */
1504
Jean Delvare7ae31482011-03-20 14:50:52 +01001505int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1506{
1507 int res;
1508
1509 mutex_lock(&core_lock);
1510 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1511 mutex_unlock(&core_lock);
1512
1513 return res;
1514}
1515EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1516
Jean Delvare69b00892009-12-06 17:06:27 +01001517static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001518{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001519 if (dev->type != &i2c_adapter_type)
1520 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001521 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001522}
1523
David Brownell7b4fbc52007-05-01 23:26:30 +02001524/*
1525 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001526 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001529int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001531 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
David Brownell1d0b19c2008-10-14 17:30:05 +02001533 /* Can't register until after driver model init */
1534 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1535 return -EAGAIN;
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001538 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 driver->driver.bus = &i2c_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jean Delvare729d6dd2009-06-19 16:58:18 +02001541 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001542 * will have called probe() for all matching-but-unbound devices.
1543 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 res = driver_register(&driver->driver);
1545 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001546 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001547
Mark Brownf4e8db32011-01-14 22:03:50 +01001548 /* Drivers should switch to dev_pm_ops instead. */
1549 if (driver->suspend)
1550 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1551 driver->driver.name);
1552 if (driver->resume)
1553 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1554 driver->driver.name);
1555
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001556 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Jean Delvare4735c982008-07-14 22:38:36 +02001558 INIT_LIST_HEAD(&driver->clients);
1559 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001560 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001561
Jean Delvare7eebcb72006-02-05 23:28:21 +01001562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001564EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Jean Delvare69b00892009-12-06 17:06:27 +01001566static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001567{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001568 if (dev->type == &i2c_adapter_type)
1569 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1570 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001571}
1572
David Brownella1d9e6e2007-05-01 23:26:30 +02001573/**
1574 * i2c_del_driver - unregister I2C driver
1575 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001576 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001577 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001578void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Jean Delvare7ae31482011-03-20 14:50:52 +01001580 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 driver_unregister(&driver->driver);
Laurent Riffard35d8b2e2005-11-26 20:34:05 +01001583 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
David Brownellc0564602007-05-01 23:26:31 +02001585EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
David Brownell7b4fbc52007-05-01 23:26:30 +02001587/* ------------------------------------------------------------------------- */
1588
Jean Delvaree48d3312008-01-27 18:14:48 +01001589/**
1590 * i2c_use_client - increments the reference count of the i2c client structure
1591 * @client: the client being referenced
1592 *
1593 * Each live reference to a client should be refcounted. The driver model does
1594 * that automatically as part of driver binding, so that most drivers don't
1595 * need to do this explicitly: they hold a reference until they're unbound
1596 * from the device.
1597 *
1598 * A pointer to the client with the incremented reference counter is returned.
1599 */
1600struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
David Brownell6ea438e2008-07-14 22:38:24 +02001602 if (client && get_device(&client->dev))
1603 return client;
1604 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
David Brownellc0564602007-05-01 23:26:31 +02001606EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Jean Delvaree48d3312008-01-27 18:14:48 +01001608/**
1609 * i2c_release_client - release a use of the i2c client structure
1610 * @client: the client being no longer referenced
1611 *
1612 * Must be called when a user of a client is finished with it.
1613 */
1614void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
David Brownell6ea438e2008-07-14 22:38:24 +02001616 if (client)
1617 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
David Brownellc0564602007-05-01 23:26:31 +02001619EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
David Brownell9b766b82008-01-27 18:14:51 +01001621struct i2c_cmd_arg {
1622 unsigned cmd;
1623 void *arg;
1624};
1625
1626static int i2c_cmd(struct device *dev, void *_arg)
1627{
1628 struct i2c_client *client = i2c_verify_client(dev);
1629 struct i2c_cmd_arg *arg = _arg;
1630
1631 if (client && client->driver && client->driver->command)
1632 client->driver->command(client, arg->cmd, arg->arg);
1633 return 0;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1637{
David Brownell9b766b82008-01-27 18:14:51 +01001638 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
David Brownell9b766b82008-01-27 18:14:51 +01001640 cmd_arg.cmd = cmd;
1641 cmd_arg.arg = arg;
1642 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
David Brownellc0564602007-05-01 23:26:31 +02001644EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646static int __init i2c_init(void)
1647{
1648 int retval;
1649
1650 retval = bus_register(&i2c_bus_type);
1651 if (retval)
1652 return retval;
Jean Delvare2bb50952009-09-18 22:45:46 +02001653#ifdef CONFIG_I2C_COMPAT
1654 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1655 if (!i2c_adapter_compat_class) {
1656 retval = -ENOMEM;
1657 goto bus_err;
1658 }
1659#endif
David Brownelle9f13732008-01-27 18:14:52 +01001660 retval = i2c_add_driver(&dummy_driver);
1661 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001662 goto class_err;
David Brownelle9f13732008-01-27 18:14:52 +01001663 return 0;
1664
Jean Delvare2bb50952009-09-18 22:45:46 +02001665class_err:
1666#ifdef CONFIG_I2C_COMPAT
1667 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001668bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001669#endif
David Brownelle9f13732008-01-27 18:14:52 +01001670 bus_unregister(&i2c_bus_type);
1671 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674static void __exit i2c_exit(void)
1675{
David Brownelle9f13732008-01-27 18:14:52 +01001676 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001677#ifdef CONFIG_I2C_COMPAT
1678 class_compat_unregister(i2c_adapter_compat_class);
1679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 bus_unregister(&i2c_bus_type);
1681}
1682
David Brownella10f9e72008-10-14 17:30:06 +02001683/* We must initialize early, because some subsystems register i2c drivers
1684 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1685 */
1686postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687module_exit(i2c_exit);
1688
1689/* ----------------------------------------------------
1690 * the functional interface to the i2c busses.
1691 * ----------------------------------------------------
1692 */
1693
David Brownella1cdeda2008-07-14 22:38:24 +02001694/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001695 * __i2c_transfer - unlocked flavor of i2c_transfer
1696 * @adap: Handle to I2C bus
1697 * @msgs: One or more messages to execute before STOP is issued to
1698 * terminate the operation; each message begins with a START.
1699 * @num: Number of messages to be executed.
1700 *
1701 * Returns negative errno, else the number of messages executed.
1702 *
1703 * Adapter lock must be held when calling this function. No debug logging
1704 * takes place. adap->algo->master_xfer existence isn't checked.
1705 */
1706int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1707{
1708 unsigned long orig_jiffies;
1709 int ret, try;
1710
1711 /* Retry automatically on arbitration loss */
1712 orig_jiffies = jiffies;
1713 for (ret = 0, try = 0; try <= adap->retries; try++) {
1714 ret = adap->algo->master_xfer(adap, msgs, num);
1715 if (ret != -EAGAIN)
1716 break;
1717 if (time_after(jiffies, orig_jiffies + adap->timeout))
1718 break;
1719 }
1720
1721 return ret;
1722}
1723EXPORT_SYMBOL(__i2c_transfer);
1724
1725/**
David Brownella1cdeda2008-07-14 22:38:24 +02001726 * i2c_transfer - execute a single or combined I2C message
1727 * @adap: Handle to I2C bus
1728 * @msgs: One or more messages to execute before STOP is issued to
1729 * terminate the operation; each message begins with a START.
1730 * @num: Number of messages to be executed.
1731 *
1732 * Returns negative errno, else the number of messages executed.
1733 *
1734 * Note that there is no requirement that each message be sent to
1735 * the same slave address, although that is the most common model.
1736 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001737int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001739 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
David Brownella1cdeda2008-07-14 22:38:24 +02001741 /* REVISIT the fault reporting model here is weak:
1742 *
1743 * - When we get an error after receiving N bytes from a slave,
1744 * there is no way to report "N".
1745 *
1746 * - When we get a NAK after transmitting N bytes to a slave,
1747 * there is no way to report "N" ... or to let the master
1748 * continue executing the rest of this combined message, if
1749 * that's the appropriate response.
1750 *
1751 * - When for example "num" is two and we successfully complete
1752 * the first message but get an error part way through the
1753 * second, it's unclear whether that should be reported as
1754 * one (discarding status on the second message) or errno
1755 * (discarding status on the first one).
1756 */
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 if (adap->algo->master_xfer) {
1759#ifdef DEBUG
1760 for (ret = 0; ret < num; ret++) {
1761 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
Jean Delvare209d27c2007-05-01 23:26:29 +02001762 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1763 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1764 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766#endif
1767
Mike Rapoportcea443a82008-01-27 18:14:50 +01001768 if (in_atomic() || irqs_disabled()) {
Jean Delvarefe61e072010-08-11 18:20:58 +02001769 ret = i2c_trylock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001770 if (!ret)
1771 /* I2C activity is ongoing. */
1772 return -EAGAIN;
1773 } else {
Jean Delvarefe61e072010-08-11 18:20:58 +02001774 i2c_lock_adapter(adap);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001775 }
1776
Jean Delvareb37d2a32012-06-29 07:47:19 -03001777 ret = __i2c_transfer(adap, msgs, num);
Jean Delvarefe61e072010-08-11 18:20:58 +02001778 i2c_unlock_adapter(adap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 return ret;
1781 } else {
1782 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001783 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
1785}
David Brownellc0564602007-05-01 23:26:31 +02001786EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
David Brownella1cdeda2008-07-14 22:38:24 +02001788/**
1789 * i2c_master_send - issue a single I2C message in master transmit mode
1790 * @client: Handle to slave device
1791 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001792 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001793 *
1794 * Returns negative errno, or else the number of bytes written.
1795 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001796int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
1798 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001799 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 struct i2c_msg msg;
1801
Jean Delvare815f55f2005-05-07 22:58:46 +02001802 msg.addr = client->addr;
1803 msg.flags = client->flags & I2C_M_TEN;
1804 msg.len = count;
1805 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001806
Jean Delvare815f55f2005-05-07 22:58:46 +02001807 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001809 /*
1810 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1811 * transmitted, else error code.
1812 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001813 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
David Brownellc0564602007-05-01 23:26:31 +02001815EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
David Brownella1cdeda2008-07-14 22:38:24 +02001817/**
1818 * i2c_master_recv - issue a single I2C message in master receive mode
1819 * @client: Handle to slave device
1820 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001821 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001822 *
1823 * Returns negative errno, or else the number of bytes read.
1824 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001825int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Farid Hammane7225acf2010-05-21 18:40:58 +02001827 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct i2c_msg msg;
1829 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Jean Delvare815f55f2005-05-07 22:58:46 +02001831 msg.addr = client->addr;
1832 msg.flags = client->flags & I2C_M_TEN;
1833 msg.flags |= I2C_M_RD;
1834 msg.len = count;
1835 msg.buf = buf;
1836
1837 ret = i2c_transfer(adap, &msg, 1);
1838
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001839 /*
1840 * If everything went ok (i.e. 1 msg received), return #bytes received,
1841 * else error code.
1842 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001843 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
David Brownellc0564602007-05-01 23:26:31 +02001845EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/* ----------------------------------------------------
1848 * the i2c address scanning function
1849 * Will not work for 10-bit addresses!
1850 * ----------------------------------------------------
1851 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001852
Jean Delvare63e4e802010-06-03 11:33:51 +02001853/*
1854 * Legacy default probe function, mostly relevant for SMBus. The default
1855 * probe method is a quick write, but it is known to corrupt the 24RF08
1856 * EEPROMs due to a state machine bug, and could also irreversibly
1857 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1858 * we use a short byte read instead. Also, some bus drivers don't implement
1859 * quick write, so we fallback to a byte read in that case too.
1860 * On x86, there is another special case for FSC hardware monitoring chips,
1861 * which want regular byte reads (address 0x73.) Fortunately, these are the
1862 * only known chips using this I2C address on PC hardware.
1863 * Returns 1 if probe succeeded, 0 if not.
1864 */
1865static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1866{
1867 int err;
1868 union i2c_smbus_data dummy;
1869
1870#ifdef CONFIG_X86
1871 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1872 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1873 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1874 I2C_SMBUS_BYTE_DATA, &dummy);
1875 else
1876#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001877 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1878 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001879 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1880 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001881 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1882 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1883 I2C_SMBUS_BYTE, &dummy);
1884 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001885 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1886 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001887 err = -EOPNOTSUPP;
1888 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001889
1890 return err >= 0;
1891}
1892
Jean Delvareccfbbd02009-12-06 17:06:25 +01001893static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02001894 struct i2c_driver *driver)
1895{
1896 struct i2c_board_info info;
1897 struct i2c_adapter *adapter = temp_client->adapter;
1898 int addr = temp_client->addr;
1899 int err;
1900
1901 /* Make sure the address is valid */
Jean Delvare656b8762010-06-03 11:33:53 +02001902 err = i2c_check_addr_validity(addr);
1903 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02001904 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1905 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02001906 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02001907 }
1908
1909 /* Skip if already in use */
Jean Delvare3b5f7942010-06-03 11:33:55 +02001910 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02001911 return 0;
1912
Jean Delvareccfbbd02009-12-06 17:06:25 +01001913 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02001914 if (!i2c_default_probe(adapter, addr))
1915 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02001916
1917 /* Finally call the custom detection function */
1918 memset(&info, 0, sizeof(struct i2c_board_info));
1919 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01001920 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02001921 if (err) {
1922 /* -ENODEV is returned if the detection fails. We catch it
1923 here as this isn't an error. */
1924 return err == -ENODEV ? 0 : err;
1925 }
1926
1927 /* Consistency check */
1928 if (info.type[0] == '\0') {
1929 dev_err(&adapter->dev, "%s detection function provided "
1930 "no name for 0x%x\n", driver->driver.name,
1931 addr);
1932 } else {
1933 struct i2c_client *client;
1934
1935 /* Detection succeeded, instantiate the device */
1936 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1937 info.type, info.addr);
1938 client = i2c_new_device(adapter, &info);
1939 if (client)
1940 list_add_tail(&client->detected, &driver->clients);
1941 else
1942 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1943 info.type, info.addr);
1944 }
1945 return 0;
1946}
1947
1948static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1949{
Jean Delvarec3813d62009-12-14 21:17:25 +01001950 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02001951 struct i2c_client *temp_client;
1952 int i, err = 0;
1953 int adap_id = i2c_adapter_id(adapter);
1954
Jean Delvarec3813d62009-12-14 21:17:25 +01001955 address_list = driver->address_list;
1956 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02001957 return 0;
1958
Jean Delvare51b54ba2010-10-24 18:16:58 +02001959 /* Stop here if the classes do not match */
1960 if (!(adapter->class & driver->class))
1961 return 0;
1962
Jean Delvare4735c982008-07-14 22:38:36 +02001963 /* Set up a temporary client to help detect callback */
1964 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1965 if (!temp_client)
1966 return -ENOMEM;
1967 temp_client->adapter = adapter;
1968
Jean Delvarec3813d62009-12-14 21:17:25 +01001969 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Jean Delvare4735c982008-07-14 22:38:36 +02001970 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
Jean Delvarec3813d62009-12-14 21:17:25 +01001971 "addr 0x%02x\n", adap_id, address_list[i]);
1972 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01001973 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02001974 if (unlikely(err))
1975 break;
Jean Delvare4735c982008-07-14 22:38:36 +02001976 }
1977
Jean Delvare4735c982008-07-14 22:38:36 +02001978 kfree(temp_client);
1979 return err;
1980}
1981
Jean Delvared44f19d2010-08-11 18:20:57 +02001982int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1983{
1984 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1985 I2C_SMBUS_QUICK, NULL) >= 0;
1986}
1987EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1988
Jean Delvare12b5053a2007-05-01 23:26:31 +02001989struct i2c_client *
1990i2c_new_probed_device(struct i2c_adapter *adap,
1991 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02001992 unsigned short const *addr_list,
1993 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02001994{
1995 int i;
1996
Jean Delvare8031d792010-08-11 18:21:00 +02001997 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02001998 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02001999
Jean Delvare12b5053a2007-05-01 23:26:31 +02002000 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2001 /* Check address validity */
Jean Delvare656b8762010-06-03 11:33:53 +02002002 if (i2c_check_addr_validity(addr_list[i]) < 0) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002003 dev_warn(&adap->dev, "Invalid 7-bit address "
2004 "0x%02x\n", addr_list[i]);
2005 continue;
2006 }
2007
2008 /* Check address availability */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002009 if (i2c_check_addr_busy(adap, addr_list[i])) {
Jean Delvare12b5053a2007-05-01 23:26:31 +02002010 dev_dbg(&adap->dev, "Address 0x%02x already in "
2011 "use, not probing\n", addr_list[i]);
2012 continue;
2013 }
2014
Jean Delvare63e4e802010-06-03 11:33:51 +02002015 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002016 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002017 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002018 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002019
2020 if (addr_list[i] == I2C_CLIENT_END) {
2021 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2022 return NULL;
2023 }
2024
2025 info->addr = addr_list[i];
2026 return i2c_new_device(adap, info);
2027}
2028EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2029
Jean Delvared735b342011-03-20 14:50:52 +01002030struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002033
Jean Delvarecaada322008-01-27 18:14:49 +01002034 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002035 adapter = idr_find(&i2c_adapter_idr, nr);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002036 if (adapter && !try_module_get(adapter->owner))
2037 adapter = NULL;
2038
Jean Delvarecaada322008-01-27 18:14:49 +01002039 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002040 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
David Brownellc0564602007-05-01 23:26:31 +02002042EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044void i2c_put_adapter(struct i2c_adapter *adap)
2045{
Sebastian Hesselbarthc66c4cc2013-08-01 14:10:46 +02002046 if (adap)
2047 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
David Brownellc0564602007-05-01 23:26:31 +02002049EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
2051/* The SMBus parts */
2052
David Brownell438d6c22006-12-10 21:21:31 +01002053#define POLY (0x1070U << 3)
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002054static u8 crc8(u16 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int i;
David Brownell438d6c22006-12-10 21:21:31 +01002057
Farid Hammane7225acf2010-05-21 18:40:58 +02002058 for (i = 0; i < 8; i++) {
David Brownell438d6c22006-12-10 21:21:31 +01002059 if (data & 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 data = data ^ POLY;
2061 data = data << 1;
2062 }
2063 return (u8)(data >> 8);
2064}
2065
Jean Delvare421ef472005-10-26 21:28:55 +02002066/* Incremental CRC8 over count bytes in the array pointed to by p */
2067static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 int i;
2070
Farid Hammane7225acf2010-05-21 18:40:58 +02002071 for (i = 0; i < count; i++)
Jean Delvare421ef472005-10-26 21:28:55 +02002072 crc = crc8((crc ^ p[i]) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return crc;
2074}
2075
Jean Delvare421ef472005-10-26 21:28:55 +02002076/* Assume a 7-bit address, which is reasonable for SMBus */
2077static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Jean Delvare421ef472005-10-26 21:28:55 +02002079 /* The address will be sent first */
2080 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
2081 pec = i2c_smbus_pec(pec, &addr, 1);
2082
2083 /* The data buffer follows */
2084 return i2c_smbus_pec(pec, msg->buf, msg->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Jean Delvare421ef472005-10-26 21:28:55 +02002087/* Used for write only transactions */
2088static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Jean Delvare421ef472005-10-26 21:28:55 +02002090 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
2091 msg->len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Jean Delvare421ef472005-10-26 21:28:55 +02002094/* Return <0 on CRC error
2095 If there was a write before this read (most cases) we need to take the
2096 partial CRC from the write part into account.
2097 Note that this function does modify the message (we need to decrease the
2098 message length to hide the CRC byte from the caller). */
2099static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Jean Delvare421ef472005-10-26 21:28:55 +02002101 u8 rpec = msg->buf[--msg->len];
2102 cpec = i2c_smbus_msg_pec(cpec, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (rpec != cpec) {
2105 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2106 rpec, cpec);
David Brownell24a5bb72008-07-14 22:38:23 +02002107 return -EBADMSG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
David Brownell438d6c22006-12-10 21:21:31 +01002109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
David Brownella1cdeda2008-07-14 22:38:24 +02002112/**
2113 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2114 * @client: Handle to slave device
2115 *
2116 * This executes the SMBus "receive byte" protocol, returning negative errno
2117 * else the byte received from the device.
2118 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002119s32 i2c_smbus_read_byte(const struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002122 int status;
2123
2124 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2125 I2C_SMBUS_READ, 0,
2126 I2C_SMBUS_BYTE, &data);
2127 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
David Brownellc0564602007-05-01 23:26:31 +02002129EXPORT_SYMBOL(i2c_smbus_read_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
David Brownella1cdeda2008-07-14 22:38:24 +02002131/**
2132 * i2c_smbus_write_byte - SMBus "send byte" protocol
2133 * @client: Handle to slave device
2134 * @value: Byte to be sent
2135 *
2136 * This executes the SMBus "send byte" protocol, returning negative errno
2137 * else zero on success.
2138 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002139s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Farid Hammane7225acf2010-05-21 18:40:58 +02002141 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
Jean Delvare421ef472005-10-26 21:28:55 +02002142 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
David Brownellc0564602007-05-01 23:26:31 +02002144EXPORT_SYMBOL(i2c_smbus_write_byte);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
David Brownella1cdeda2008-07-14 22:38:24 +02002146/**
2147 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2148 * @client: Handle to slave device
2149 * @command: Byte interpreted by slave
2150 *
2151 * This executes the SMBus "read byte" protocol, returning negative errno
2152 * else a data byte received from the device.
2153 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002154s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002157 int status;
2158
2159 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2160 I2C_SMBUS_READ, command,
2161 I2C_SMBUS_BYTE_DATA, &data);
2162 return (status < 0) ? status : data.byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
David Brownellc0564602007-05-01 23:26:31 +02002164EXPORT_SYMBOL(i2c_smbus_read_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
David Brownella1cdeda2008-07-14 22:38:24 +02002166/**
2167 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2168 * @client: Handle to slave device
2169 * @command: Byte interpreted by slave
2170 * @value: Byte being written
2171 *
2172 * This executes the SMBus "write byte" protocol, returning negative errno
2173 * else zero on success.
2174 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002175s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2176 u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 union i2c_smbus_data data;
2179 data.byte = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002180 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2181 I2C_SMBUS_WRITE, command,
2182 I2C_SMBUS_BYTE_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
David Brownellc0564602007-05-01 23:26:31 +02002184EXPORT_SYMBOL(i2c_smbus_write_byte_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
David Brownella1cdeda2008-07-14 22:38:24 +02002186/**
2187 * i2c_smbus_read_word_data - SMBus "read word" protocol
2188 * @client: Handle to slave device
2189 * @command: Byte interpreted by slave
2190 *
2191 * This executes the SMBus "read word" protocol, returning negative errno
2192 * else a 16-bit unsigned "word" received from the device.
2193 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002194s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002197 int status;
2198
2199 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2200 I2C_SMBUS_READ, command,
2201 I2C_SMBUS_WORD_DATA, &data);
2202 return (status < 0) ? status : data.word;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
David Brownellc0564602007-05-01 23:26:31 +02002204EXPORT_SYMBOL(i2c_smbus_read_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
David Brownella1cdeda2008-07-14 22:38:24 +02002206/**
2207 * i2c_smbus_write_word_data - SMBus "write word" protocol
2208 * @client: Handle to slave device
2209 * @command: Byte interpreted by slave
2210 * @value: 16-bit "word" being written
2211 *
2212 * This executes the SMBus "write word" protocol, returning negative errno
2213 * else zero on success.
2214 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002215s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2216 u16 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 union i2c_smbus_data data;
2219 data.word = value;
Farid Hammane7225acf2010-05-21 18:40:58 +02002220 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2221 I2C_SMBUS_WRITE, command,
2222 I2C_SMBUS_WORD_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
David Brownellc0564602007-05-01 23:26:31 +02002224EXPORT_SYMBOL(i2c_smbus_write_word_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
David Brownella64ec072007-10-13 23:56:31 +02002226/**
David Brownella1cdeda2008-07-14 22:38:24 +02002227 * i2c_smbus_read_block_data - SMBus "block read" protocol
David Brownella64ec072007-10-13 23:56:31 +02002228 * @client: Handle to slave device
David Brownella1cdeda2008-07-14 22:38:24 +02002229 * @command: Byte interpreted by slave
David Brownella64ec072007-10-13 23:56:31 +02002230 * @values: Byte array into which data will be read; big enough to hold
2231 * the data returned by the slave. SMBus allows at most 32 bytes.
2232 *
David Brownella1cdeda2008-07-14 22:38:24 +02002233 * This executes the SMBus "block read" protocol, returning negative errno
2234 * else the number of data bytes in the slave's response.
David Brownella64ec072007-10-13 23:56:31 +02002235 *
2236 * Note that using this function requires that the client's adapter support
2237 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2238 * support this; its emulation through I2C messaging relies on a specific
2239 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2240 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002241s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002242 u8 *values)
2243{
2244 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002245 int status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002246
David Brownell24a5bb72008-07-14 22:38:23 +02002247 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2248 I2C_SMBUS_READ, command,
2249 I2C_SMBUS_BLOCK_DATA, &data);
2250 if (status)
2251 return status;
Jean Delvareb86a1bc2007-05-01 23:26:34 +02002252
2253 memcpy(values, &data.block[1], data.block[0]);
2254 return data.block[0];
2255}
2256EXPORT_SYMBOL(i2c_smbus_read_block_data);
2257
David Brownella1cdeda2008-07-14 22:38:24 +02002258/**
2259 * i2c_smbus_write_block_data - SMBus "block write" protocol
2260 * @client: Handle to slave device
2261 * @command: Byte interpreted by slave
2262 * @length: Size of data block; SMBus allows at most 32 bytes
2263 * @values: Byte array which will be written.
2264 *
2265 * This executes the SMBus "block write" protocol, returning negative errno
2266 * else zero on success.
2267 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002268s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002269 u8 length, const u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 union i2c_smbus_data data;
Jean Delvare76560322006-01-18 23:14:55 +01002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (length > I2C_SMBUS_BLOCK_MAX)
2274 length = I2C_SMBUS_BLOCK_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 data.block[0] = length;
Jean Delvare76560322006-01-18 23:14:55 +01002276 memcpy(&data.block[1], values, length);
Farid Hammane7225acf2010-05-21 18:40:58 +02002277 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2278 I2C_SMBUS_WRITE, command,
2279 I2C_SMBUS_BLOCK_DATA, &data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
David Brownellc0564602007-05-01 23:26:31 +02002281EXPORT_SYMBOL(i2c_smbus_write_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
2283/* Returns the number of read bytes */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002284s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
Jean Delvare4b2643d2007-07-12 14:12:29 +02002285 u8 length, u8 *values)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 union i2c_smbus_data data;
David Brownell24a5bb72008-07-14 22:38:23 +02002288 int status;
Jean Delvare76560322006-01-18 23:14:55 +01002289
Jean Delvare4b2643d2007-07-12 14:12:29 +02002290 if (length > I2C_SMBUS_BLOCK_MAX)
2291 length = I2C_SMBUS_BLOCK_MAX;
2292 data.block[0] = length;
David Brownell24a5bb72008-07-14 22:38:23 +02002293 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2294 I2C_SMBUS_READ, command,
2295 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2296 if (status < 0)
2297 return status;
Jean Delvare76560322006-01-18 23:14:55 +01002298
2299 memcpy(values, &data.block[1], data.block[0]);
2300 return data.block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
David Brownellc0564602007-05-01 23:26:31 +02002302EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Jean Delvare0cc43a12011-01-10 22:11:23 +01002304s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
Krzysztof Halasa46f5ed72006-06-12 21:42:20 +02002305 u8 length, const u8 *values)
Jean Delvare21bbd692006-01-09 15:19:18 +11002306{
2307 union i2c_smbus_data data;
2308
2309 if (length > I2C_SMBUS_BLOCK_MAX)
2310 length = I2C_SMBUS_BLOCK_MAX;
2311 data.block[0] = length;
2312 memcpy(data.block + 1, values, length);
2313 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2314 I2C_SMBUS_WRITE, command,
2315 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2316}
David Brownellc0564602007-05-01 23:26:31 +02002317EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
Jean Delvare21bbd692006-01-09 15:19:18 +11002318
David Brownell438d6c22006-12-10 21:21:31 +01002319/* Simulate a SMBus command using the i2c protocol
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 No checking of parameters is done! */
Farid Hammane7225acf2010-05-21 18:40:58 +02002321static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2322 unsigned short flags,
2323 char read_write, u8 command, int size,
2324 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
2326 /* So we need to generate a series of msgs. In the case of writing, we
2327 need to use only one message; when reading, we need two. We initialize
2328 most things with sane defaults, to keep the code below somewhat
2329 simpler. */
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002330 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2331 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
Farid Hammane7225acf2010-05-21 18:40:58 +02002332 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int i;
Jean Delvare421ef472005-10-26 21:28:55 +02002334 u8 partial_pec = 0;
David Brownell24a5bb72008-07-14 22:38:23 +02002335 int status;
Shubhrajyoti D230da092012-10-05 22:23:52 +02002336 struct i2c_msg msg[2] = {
2337 {
2338 .addr = addr,
2339 .flags = flags,
2340 .len = 1,
2341 .buf = msgbuf0,
2342 }, {
2343 .addr = addr,
2344 .flags = flags | I2C_M_RD,
2345 .len = 0,
2346 .buf = msgbuf1,
2347 },
2348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 msgbuf0[0] = command;
Farid Hammane7225acf2010-05-21 18:40:58 +02002351 switch (size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 case I2C_SMBUS_QUICK:
2353 msg[0].len = 0;
2354 /* Special case: The read/write field is used as data */
Roel Kluinf29d2e02009-02-24 19:19:48 +01002355 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2356 I2C_M_RD : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 num = 1;
2358 break;
2359 case I2C_SMBUS_BYTE:
2360 if (read_write == I2C_SMBUS_READ) {
2361 /* Special case: only a read! */
2362 msg[0].flags = I2C_M_RD | flags;
2363 num = 1;
2364 }
2365 break;
2366 case I2C_SMBUS_BYTE_DATA:
2367 if (read_write == I2C_SMBUS_READ)
2368 msg[1].len = 1;
2369 else {
2370 msg[0].len = 2;
2371 msgbuf0[1] = data->byte;
2372 }
2373 break;
2374 case I2C_SMBUS_WORD_DATA:
2375 if (read_write == I2C_SMBUS_READ)
2376 msg[1].len = 2;
2377 else {
Farid Hammane7225acf2010-05-21 18:40:58 +02002378 msg[0].len = 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002380 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 break;
2383 case I2C_SMBUS_PROC_CALL:
2384 num = 2; /* Special case */
2385 read_write = I2C_SMBUS_READ;
2386 msg[0].len = 3;
2387 msg[1].len = 2;
2388 msgbuf0[1] = data->word & 0xff;
Jean Delvare7eff82c2006-09-03 22:24:00 +02002389 msgbuf0[2] = data->word >> 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 break;
2391 case I2C_SMBUS_BLOCK_DATA:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (read_write == I2C_SMBUS_READ) {
Jean Delvare209d27c2007-05-01 23:26:29 +02002393 msg[1].flags |= I2C_M_RECV_LEN;
2394 msg[1].len = 1; /* block length will be added by
2395 the underlying bus driver */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 } else {
2397 msg[0].len = data->block[0] + 2;
2398 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
David Brownell24a5bb72008-07-14 22:38:23 +02002399 dev_err(&adapter->dev,
2400 "Invalid block write size %d\n",
2401 data->block[0]);
2402 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
Hideki Iwamoto5c50d182005-09-25 17:01:11 +02002404 for (i = 1; i < msg[0].len; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 msgbuf0[i] = data->block[i-1];
2406 }
2407 break;
2408 case I2C_SMBUS_BLOCK_PROC_CALL:
Jean Delvare209d27c2007-05-01 23:26:29 +02002409 num = 2; /* Another special case */
2410 read_write = I2C_SMBUS_READ;
2411 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
David Brownell24a5bb72008-07-14 22:38:23 +02002412 dev_err(&adapter->dev,
2413 "Invalid block write size %d\n",
Jean Delvare209d27c2007-05-01 23:26:29 +02002414 data->block[0]);
David Brownell24a5bb72008-07-14 22:38:23 +02002415 return -EINVAL;
Jean Delvare209d27c2007-05-01 23:26:29 +02002416 }
2417 msg[0].len = data->block[0] + 2;
2418 for (i = 1; i < msg[0].len; i++)
2419 msgbuf0[i] = data->block[i-1];
2420 msg[1].flags |= I2C_M_RECV_LEN;
2421 msg[1].len = 1; /* block length will be added by
2422 the underlying bus driver */
2423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 case I2C_SMBUS_I2C_BLOCK_DATA:
2425 if (read_write == I2C_SMBUS_READ) {
Jean Delvare4b2643d2007-07-12 14:12:29 +02002426 msg[1].len = data->block[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 } else {
2428 msg[0].len = data->block[0] + 1;
Jean Delvare30dac742005-10-08 00:15:59 +02002429 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
David Brownell24a5bb72008-07-14 22:38:23 +02002430 dev_err(&adapter->dev,
2431 "Invalid block write size %d\n",
2432 data->block[0]);
2433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
2435 for (i = 1; i <= data->block[0]; i++)
2436 msgbuf0[i] = data->block[i];
2437 }
2438 break;
2439 default:
David Brownell24a5bb72008-07-14 22:38:23 +02002440 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2441 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Jean Delvare421ef472005-10-26 21:28:55 +02002444 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2445 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2446 if (i) {
2447 /* Compute PEC if first message is a write */
2448 if (!(msg[0].flags & I2C_M_RD)) {
David Brownell438d6c22006-12-10 21:21:31 +01002449 if (num == 1) /* Write only */
Jean Delvare421ef472005-10-26 21:28:55 +02002450 i2c_smbus_add_pec(&msg[0]);
2451 else /* Write followed by read */
2452 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2453 }
2454 /* Ask for PEC if last message is a read */
2455 if (msg[num-1].flags & I2C_M_RD)
David Brownell438d6c22006-12-10 21:21:31 +01002456 msg[num-1].len++;
Jean Delvare421ef472005-10-26 21:28:55 +02002457 }
2458
David Brownell24a5bb72008-07-14 22:38:23 +02002459 status = i2c_transfer(adapter, msg, num);
2460 if (status < 0)
2461 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Jean Delvare421ef472005-10-26 21:28:55 +02002463 /* Check PEC if last message is a read */
2464 if (i && (msg[num-1].flags & I2C_M_RD)) {
David Brownell24a5bb72008-07-14 22:38:23 +02002465 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2466 if (status < 0)
2467 return status;
Jean Delvare421ef472005-10-26 21:28:55 +02002468 }
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (read_write == I2C_SMBUS_READ)
Farid Hammane7225acf2010-05-21 18:40:58 +02002471 switch (size) {
2472 case I2C_SMBUS_BYTE:
2473 data->byte = msgbuf0[0];
2474 break;
2475 case I2C_SMBUS_BYTE_DATA:
2476 data->byte = msgbuf1[0];
2477 break;
2478 case I2C_SMBUS_WORD_DATA:
2479 case I2C_SMBUS_PROC_CALL:
2480 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2481 break;
2482 case I2C_SMBUS_I2C_BLOCK_DATA:
2483 for (i = 0; i < data->block[0]; i++)
2484 data->block[i+1] = msgbuf1[i];
2485 break;
2486 case I2C_SMBUS_BLOCK_DATA:
2487 case I2C_SMBUS_BLOCK_PROC_CALL:
2488 for (i = 0; i < msgbuf1[0] + 1; i++)
2489 data->block[i] = msgbuf1[i];
2490 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
2492 return 0;
2493}
2494
David Brownella1cdeda2008-07-14 22:38:24 +02002495/**
2496 * i2c_smbus_xfer - execute SMBus protocol operations
2497 * @adapter: Handle to I2C bus
2498 * @addr: Address of SMBus slave on that bus
2499 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2500 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2501 * @command: Byte interpreted by slave, for protocols which use such bytes
2502 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2503 * @data: Data to be read or written
2504 *
2505 * This executes an SMBus protocol operation, and returns a negative
2506 * errno code else zero on success.
2507 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002508s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
David Brownella1cdeda2008-07-14 22:38:24 +02002509 char read_write, u8 command, int protocol,
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002510 union i2c_smbus_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Clifford Wolf66b650f2009-06-15 18:01:46 +02002512 unsigned long orig_jiffies;
2513 int try;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 s32 res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Laurent Pinchartd47726c2012-07-24 14:13:59 +02002516 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 if (adapter->algo->smbus_xfer) {
Jean Delvarefe61e072010-08-11 18:20:58 +02002519 i2c_lock_adapter(adapter);
Clifford Wolf66b650f2009-06-15 18:01:46 +02002520
2521 /* Retry automatically on arbitration loss */
2522 orig_jiffies = jiffies;
2523 for (res = 0, try = 0; try <= adapter->retries; try++) {
2524 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2525 read_write, command,
2526 protocol, data);
2527 if (res != -EAGAIN)
2528 break;
2529 if (time_after(jiffies,
2530 orig_jiffies + adapter->timeout))
2531 break;
2532 }
Jean Delvarefe61e072010-08-11 18:20:58 +02002533 i2c_unlock_adapter(adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Laurent Pinchart72fc2c72012-07-24 14:13:59 +02002535 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2536 return res;
2537 /*
2538 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2539 * implement native support for the SMBus operation.
2540 */
2541 }
2542
2543 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2544 command, protocol, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546EXPORT_SYMBOL(i2c_smbus_xfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2549MODULE_DESCRIPTION("I2C-Bus main module");
2550MODULE_LICENSE("GPL");